1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
|
/** @file
Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "UfsBlockIoPei.h"
//
// Template for UFS HC Peim Private Data.
//
UFS_PEIM_HC_PRIVATE_DATA gUfsHcPeimTemplate = {
UFS_PEIM_HC_SIG, // Signature
NULL, // Controller
NULL, // Pool
{ // BlkIoPpi
UfsBlockIoPeimGetDeviceNo,
UfsBlockIoPeimGetMediaInfo,
UfsBlockIoPeimReadBlocks
},
{ // BlkIoPpiList
EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
&gEfiPeiVirtualBlockIoPpiGuid,
NULL
},
{ // Media
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
},
{
UfsDevice,
TRUE,
0,
0x1000
}
},
0, // UfsHcBase
0, // Capabilities
0, // TaskTag
0, // UtpTrlBase
0, // Nutrs
0, // UtpTmrlBase
0, // Nutmrs
{ // Luns
{
UFS_LUN_0, // Ufs Common Lun 0
UFS_LUN_1, // Ufs Common Lun 1
UFS_LUN_2, // Ufs Common Lun 2
UFS_LUN_3, // Ufs Common Lun 3
UFS_LUN_4, // Ufs Common Lun 4
UFS_LUN_5, // Ufs Common Lun 5
UFS_LUN_6, // Ufs Common Lun 6
UFS_LUN_7, // Ufs Common Lun 7
},
0x0000, // By default exposing all Luns.
0x0
}
};
/**
Execute Request Sense SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[out] DataBuffer A pointer to output sense data.
@param[out] DataBufferLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimRequestSense (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
OUT VOID *DataBuffer,
OUT UINT32 *DataBufferLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIX];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_REQUEST_SENSE;
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.DataDirection = UfsDataIn;
Packet.InDataBuffer = DataBuffer;
Packet.InTransferLength = *DataBufferLength;
Packet.SenseData = NULL;
Packet.SenseDataLength = 0;
Status = UfsExecScsiCmds (Private,(UINT8)Lun, &Packet);
if (!EFI_ERROR (Status)) {
*DataBufferLength = Packet.InTransferLength;
}
return Status;
}
/**
Execute TEST UNITY READY SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimTestUnitReady (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIX];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_TEST_UNIT_READY;
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.DataDirection = UfsNoData;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private,(UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
return Status;
}
/**
Execute INQUIRY SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[out] Inquiry A pointer to Inquiry data buffer.
@param[out] InquiryLengths The length of output Inquiry data.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimInquiry (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
OUT VOID *Inquiry,
OUT UINT32 *InquiryLength,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIX];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_INQUIRY;
Cdb[4] = sizeof (EFI_SCSI_INQUIRY_DATA);
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.InDataBuffer = Inquiry;
Packet.InTransferLength = *InquiryLength;
Packet.DataDirection = UfsDataIn;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private, (UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
if (!EFI_ERROR (Status)) {
*InquiryLength = Packet.InTransferLength;
}
return Status;
}
/**
Execute READ CAPACITY(10) SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[out] DataBuffer A pointer to READ_CAPACITY data buffer.
@param[out] DataLength The length of output READ_CAPACITY data.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimReadCapacity (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
OUT VOID *DataBuffer,
OUT UINT32 *DataLength,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_TEN];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_READ_CAPACITY;
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.InDataBuffer = DataBuffer;
Packet.InTransferLength = *DataLength;
Packet.DataDirection = UfsDataIn;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private, (UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
if (!EFI_ERROR (Status)) {
*DataLength = Packet.InTransferLength;
}
return Status;
}
/**
Execute READ CAPACITY(16) SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[out] DataBuffer A pointer to READ_CAPACITY data buffer.
@param[out] DataLength The length of output READ_CAPACITY data.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimReadCapacity16 (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
OUT VOID *DataBuffer,
OUT UINT32 *DataLength,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIXTEEN];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_READ_CAPACITY16;
Cdb[1] = 0x10; // Service Action should be 0x10 for UFS device.
Cdb[13] = 0x20; // The maximum number of bytes for returned data.
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.InDataBuffer = DataBuffer;
Packet.InTransferLength = *DataLength;
Packet.DataDirection = UfsDataIn;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private, (UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
if (!EFI_ERROR (Status)) {
*DataLength = Packet.InTransferLength;
}
return Status;
}
/**
Execute READ (10) SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[in] StartLba The start LBA.
@param[in] SectorNum The sector number to be read.
@param[out] DataBuffer A pointer to data buffer.
@param[out] DataLength The length of output data.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimRead10 (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
IN UINTN StartLba,
IN UINT32 SectorNum,
OUT VOID *DataBuffer,
OUT UINT32 *DataLength,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_TEN];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_READ10;
WriteUnaligned32 ((UINT32 *)&Cdb[2], SwapBytes32 ((UINT32) StartLba));
WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 ((UINT16) SectorNum));
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.InDataBuffer = DataBuffer;
Packet.InTransferLength = *DataLength;
Packet.DataDirection = UfsDataIn;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private, (UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
if (!EFI_ERROR (Status)) {
*DataLength = Packet.InTransferLength;
}
return Status;
}
/**
Execute READ (16) SCSI command on a specific UFS device.
@param[in] Private A pointer to UFS_PEIM_HC_PRIVATE_DATA data structure.
@param[in] Lun The lun on which the SCSI cmd executed.
@param[in] StartLba The start LBA.
@param[in] SectorNum The sector number to be read.
@param[out] DataBuffer A pointer to data buffer.
@param[out] DataLength The length of output data.
@param[out] SenseData A pointer to output sense data.
@param[out] SenseDataLength The length of output sense data.
@retval EFI_SUCCESS The command executed successfully.
@retval EFI_DEVICE_ERROR A device error occurred while attempting to send SCSI Request Packet.
@retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
**/
EFI_STATUS
UfsPeimRead16 (
IN UFS_PEIM_HC_PRIVATE_DATA *Private,
IN UINTN Lun,
IN UINTN StartLba,
IN UINT32 SectorNum,
OUT VOID *DataBuffer,
OUT UINT32 *DataLength,
OUT VOID *SenseData, OPTIONAL
OUT UINT8 *SenseDataLength
)
{
UFS_SCSI_REQUEST_PACKET Packet;
UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIXTEEN];
EFI_STATUS Status;
ZeroMem (&Packet, sizeof (UFS_SCSI_REQUEST_PACKET));
ZeroMem (Cdb, sizeof (Cdb));
Cdb[0] = EFI_SCSI_OP_READ16;
WriteUnaligned64 ((UINT64 *)&Cdb[2], SwapBytes64 (StartLba));
WriteUnaligned32 ((UINT32 *)&Cdb[10], SwapBytes32 (SectorNum));
Packet.Timeout = UFS_TIMEOUT;
Packet.Cdb = Cdb;
Packet.CdbLength = sizeof (Cdb);
Packet.InDataBuffer = DataBuffer;
Packet.InTransferLength = *DataLength;
Packet.DataDirection = UfsDataIn;
Packet.SenseData = SenseData;
Packet.SenseDataLength = *SenseDataLength;
Status = UfsExecScsiCmds (Private, (UINT8)Lun, &Packet);
if (*SenseDataLength != 0) {
*SenseDataLength = Packet.SenseDataLength;
}
if (!EFI_ERROR (Status)) {
*DataLength = Packet.InTransferLength;
}
return Status;
}
/**
Parsing Sense Keys from sense data.
@param Media The pointer of EFI_PEI_BLOCK_IO_MEDIA
@param SenseData The pointer of EFI_SCSI_SENSE_DATA
@param NeedRetry The pointer of action which indicates what is need to retry
@retval EFI_DEVICE_ERROR Indicates that error occurs
@retval EFI_SUCCESS Successfully to complete the parsing
**/
EFI_STATUS
UfsPeimParsingSenseKeys (
IN EFI_PEI_BLOCK_IO_MEDIA *Media,
IN EFI_SCSI_SENSE_DATA *SenseData,
OUT BOOLEAN *NeedRetry
)
{
if ((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
(SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA)) {
Media->MediaPresent = FALSE;
*NeedRetry = FALSE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Is No Media\n"));
return EFI_DEVICE_ERROR;
}
if ((SenseData->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
(SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE)) {
*NeedRetry = TRUE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Is Media Change\n"));
return EFI_SUCCESS;
}
if ((SenseData->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
(SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_RESET)) {
*NeedRetry = TRUE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Was Reset Before\n"));
return EFI_SUCCESS;
}
if ((SenseData->Sense_Key == EFI_SCSI_SK_MEDIUM_ERROR) ||
((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
(SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN))) {
*NeedRetry = FALSE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Media Error\n"));
return EFI_DEVICE_ERROR;
}
if (SenseData->Sense_Key == EFI_SCSI_SK_HARDWARE_ERROR) {
*NeedRetry = FALSE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Hardware Error\n"));
return EFI_DEVICE_ERROR;
}
if ((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
(SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_NOT_READY) &&
(SenseData->Addnl_Sense_Code_Qualifier == EFI_SCSI_ASCQ_IN_PROGRESS)) {
*NeedRetry = TRUE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Was Reset Before\n"));
return EFI_SUCCESS;
}
*NeedRetry = FALSE;
DEBUG ((EFI_D_VERBOSE, "UfsBlockIoPei: Sense Key = 0x%x ASC = 0x%x!\n", SenseData->Sense_Key, SenseData->Addnl_Sense_Code));
return EFI_DEVICE_ERROR;
}
/**
Gets the count of block I/O devices that one specific block driver detects.
This function is used for getting the count of block I/O devices that one
specific block driver detects. To the PEI ATAPI driver, it returns the number
of all the detected ATAPI devices it detects during the enumeration process.
To the PEI legacy floppy driver, it returns the number of all the legacy
devices it finds during its enumeration process. If no device is detected,
then the function will return zero.
@param[in] PeiServices General-purpose services that are available
to every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
instance.
@param[out] NumberBlockDevices The number of block I/O devices discovered.
@retval EFI_SUCCESS The operation performed successfully.
**/
EFI_STATUS
EFIAPI
UfsBlockIoPeimGetDeviceNo (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
OUT UINTN *NumberBlockDevices
)
{
//
// For Ufs device, it has up to 8 normal Luns plus some well-known Luns.
// At PEI phase, we will only expose normal Luns to user.
// For those disabled Lun, when user try to access it, the operation would fail.
//
*NumberBlockDevices = UFS_PEIM_MAX_LUNS;
return EFI_SUCCESS;
}
/**
Gets a block device's media information.
This function will provide the caller with the specified block device's media
information. If the media changes, calling this function will update the media
information accordingly.
@param[in] PeiServices General-purpose services that are available to every
PEIM
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, the PPIs that
want to talk to a single device must specify the
device index that was assigned during the enumeration
process. This index is a number from one to
NumberBlockDevices.
@param[out] MediaInfo The media information of the specified block media.
The caller is responsible for the ownership of this
data structure.
@par Note:
The MediaInfo structure describes an enumeration of possible block device
types. This enumeration exists because no device paths are actually passed
across interfaces that describe the type or class of hardware that is publishing
the block I/O interface. This enumeration will allow for policy decisions
in the Recovery PEIM, such as "Try to recover from legacy floppy first,
LS-120 second, CD-ROM third." If there are multiple partitions abstracted
by a given device type, they should be reported in ascending order; this
order also applies to nested partitions, such as legacy MBR, where the
outermost partitions would have precedence in the reporting order. The
same logic applies to systems such as IDE that have precedence relationships
like "Master/Slave" or "Primary/Secondary". The master device should be
reported first, the slave second.
@retval EFI_SUCCESS Media information about the specified block device
was obtained successfully.
@retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
error.
**/
EFI_STATUS
EFIAPI
UfsBlockIoPeimGetMediaInfo (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
IN UINTN DeviceIndex,
OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
)
{
EFI_STATUS Status;
UFS_PEIM_HC_PRIVATE_DATA *Private;
EFI_SCSI_SENSE_DATA SenseData;
UINT8 SenseDataLength;
EFI_SCSI_DISK_CAPACITY_DATA Capacity;
EFI_SCSI_DISK_CAPACITY_DATA16 Capacity16;
UINTN DataLength;
BOOLEAN NeedRetry;
Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS (This);
NeedRetry = TRUE;
if (DeviceIndex >= UFS_PEIM_MAX_LUNS) {
return EFI_INVALID_PARAMETER;
}
if ((Private->Luns.BitMask & (BIT0 << DeviceIndex)) == 0) {
return EFI_ACCESS_DENIED;
}
ZeroMem (&SenseData, sizeof (SenseData));
ZeroMem (&Capacity, sizeof (Capacity));
ZeroMem (&Capacity16, sizeof (Capacity16));
SenseDataLength = sizeof (SenseData);
//
// First test unit ready
//
do {
Status = UfsPeimTestUnitReady (
Private,
DeviceIndex,
&SenseData,
&SenseDataLength
);
if (!EFI_ERROR (Status)) {
break;
}
if (SenseDataLength == 0) {
continue;
}
Status = UfsPeimParsingSenseKeys (&(Private->Media[DeviceIndex]), &SenseData, &NeedRetry);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
} while (NeedRetry);
DataLength = sizeof (EFI_SCSI_DISK_CAPACITY_DATA);
SenseDataLength = 0;
Status = UfsPeimReadCapacity (Private, DeviceIndex, &Capacity, (UINT32 *)&DataLength, NULL, &SenseDataLength);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
if ((Capacity.LastLba3 == 0xff) && (Capacity.LastLba2 == 0xff) &&
(Capacity.LastLba1 == 0xff) && (Capacity.LastLba0 == 0xff)) {
DataLength = sizeof (EFI_SCSI_DISK_CAPACITY_DATA16);
SenseDataLength = 0;
Status = UfsPeimReadCapacity16 (Private, DeviceIndex, &Capacity16, (UINT32 *)&DataLength, NULL, &SenseDataLength);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
MediaInfo->LastBlock = (Capacity16.LastLba3 << 24) | (Capacity16.LastLba2 << 16) | (Capacity16.LastLba1 << 8) | Capacity16.LastLba0;
MediaInfo->LastBlock |= ((UINT64)Capacity16.LastLba7 << 56) | ((UINT64)Capacity16.LastLba6 << 48) | ((UINT64)Capacity16.LastLba5 << 40) | ((UINT64)Capacity16.LastLba4 << 32);
MediaInfo->BlockSize = (Capacity16.BlockSize3 << 24) | (Capacity16.BlockSize2 << 16) | (Capacity16.BlockSize1 << 8) | Capacity16.BlockSize0;
} else {
MediaInfo->LastBlock = (Capacity.LastLba3 << 24) | (Capacity.LastLba2 << 16) | (Capacity.LastLba1 << 8) | Capacity.LastLba0;
MediaInfo->BlockSize = (Capacity.BlockSize3 << 24) | (Capacity.BlockSize2 << 16) | (Capacity.BlockSize1 << 8) | Capacity.BlockSize0;
}
MediaInfo->DeviceType = UfsDevice;
MediaInfo->MediaPresent = TRUE;
return EFI_SUCCESS;
}
/**
Reads the requested number of blocks from the specified block device.
The function reads the requested number of blocks from the device. All the
blocks are read, or an error is returned. If there is no media in the device,
the function returns EFI_NO_MEDIA.
@param[in] PeiServices General-purpose services that are available to
every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, PPIs that
want to talk to a single device must specify the device
index that was assigned during the enumeration process.
This index is a number from one to NumberBlockDevices.
@param[in] StartLBA The starting logical block address (LBA) to read from
on the device
@param[in] BufferSize The size of the Buffer in bytes. This number must be
a multiple of the intrinsic block size of the device.
@param[out] Buffer A pointer to the destination buffer for the data.
The caller is responsible for the ownership of the
buffer.
@retval EFI_SUCCESS The data was read correctly from the device.
@retval EFI_DEVICE_ERROR The device reported an error while attempting
to perform the read operation.
@retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
valid, or the buffer is not properly aligned.
@retval EFI_NO_MEDIA There is no media in the device.
@retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
the intrinsic block size of the device.
**/
EFI_STATUS
EFIAPI
UfsBlockIoPeimReadBlocks (
IN EFI_PEI_SERVICES **PeiServices,
IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
IN UINTN DeviceIndex,
IN EFI_PEI_LBA StartLBA,
IN UINTN BufferSize,
OUT VOID *Buffer
)
{
EFI_STATUS Status;
UINTN BlockSize;
UINTN NumberOfBlocks;
UFS_PEIM_HC_PRIVATE_DATA *Private;
EFI_SCSI_SENSE_DATA SenseData;
UINT8 SenseDataLength;
BOOLEAN NeedRetry;
Status = EFI_SUCCESS;
NeedRetry = TRUE;
Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS (This);
ZeroMem (&SenseData, sizeof (SenseData));
SenseDataLength = sizeof (SenseData);
//
// Check parameters
//
if (Buffer == NULL) {
return EFI_INVALID_PARAMETER;
}
if (BufferSize == 0) {
return EFI_SUCCESS;
}
if (DeviceIndex >= UFS_PEIM_MAX_LUNS) {
return EFI_INVALID_PARAMETER;
}
if ((Private->Luns.BitMask & (BIT0 << DeviceIndex)) == 0) {
return EFI_ACCESS_DENIED;
}
BlockSize = Private->Media[DeviceIndex].BlockSize;
if (BufferSize % BlockSize != 0) {
Status = EFI_BAD_BUFFER_SIZE;
}
if (StartLBA > Private->Media[DeviceIndex].LastBlock) {
Status = EFI_INVALID_PARAMETER;
}
NumberOfBlocks = BufferSize / BlockSize;
do {
Status = UfsPeimTestUnitReady (
Private,
DeviceIndex,
&SenseData,
&SenseDataLength
);
if (!EFI_ERROR (Status)) {
break;
}
if (SenseDataLength == 0) {
continue;
}
Status = UfsPeimParsingSenseKeys (&(Private->Media[DeviceIndex]), &SenseData, &NeedRetry);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
} while (NeedRetry);
SenseDataLength = 0;
if (Private->Media[DeviceIndex].LastBlock != ~((UINTN)0)) {
Status = UfsPeimRead10 (
Private,
DeviceIndex,
(UINT32)StartLBA,
(UINT32)NumberOfBlocks,
Buffer,
(UINT32 *)&BufferSize,
NULL,
&SenseDataLength
);
} else {
Status = UfsPeimRead16 (
Private,
DeviceIndex,
(UINT32)StartLBA,
(UINT32)NumberOfBlocks,
Buffer,
(UINT32 *)&BufferSize,
NULL,
&SenseDataLength
);
}
return Status;
}
/**
The user code starts with this function.
@param FileHandle Handle of the file being invoked.
@param PeiServices Describes the list of possible PEI Services.
@retval EFI_SUCCESS The driver is successfully initialized.
@retval Others Can't initialize the driver.
**/
EFI_STATUS
EFIAPI
InitializeUfsBlockIoPeim (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
UFS_PEIM_HC_PRIVATE_DATA *Private;
EDKII_UFS_HOST_CONTROLLER_PPI *UfsHcPpi;
UINT32 Index;
UFS_CONFIG_DESC Config;
UINTN MmioBase;
UINT8 Controller;
//
// Shadow this PEIM to run from memory
//
if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle))) {
return EFI_SUCCESS;
}
//
// locate ufs host controller PPI
//
Status = PeiServicesLocatePpi (
&gEdkiiPeiUfsHostControllerPpiGuid,
0,
NULL,
(VOID **) &UfsHcPpi
);
if (EFI_ERROR (Status)) {
return EFI_DEVICE_ERROR;
}
Controller = 0;
MmioBase = 0;
while (TRUE) {
Status = UfsHcPpi->GetUfsHcMmioBar (UfsHcPpi, Controller, &MmioBase);
//
// When status is error, meant no controller is found
//
if (EFI_ERROR (Status)) {
break;
}
Private = AllocateCopyPool (sizeof (UFS_PEIM_HC_PRIVATE_DATA), &gUfsHcPeimTemplate);
if (Private == NULL) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
Private->BlkIoPpiList.Ppi = &Private->BlkIoPpi;
Private->UfsHcBase = MmioBase;
//
// Initialize the memory pool which will be used in all transactions.
//
Status = UfsPeimInitMemPool (Private);
if (EFI_ERROR (Status)) {
Status = EFI_OUT_OF_RESOURCES;
break;
}
//
// Initialize UFS Host Controller H/W.
//
Status = UfsControllerInit (Private);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "UfsDevicePei: Host Controller Initialization Error, Status = %r\n", Status));
Controller++;
continue;
}
//
// UFS 2.0 spec Section 13.1.3.3:
// At the end of the UFS Interconnect Layer initialization on both host and device side,
// the host shall send a NOP OUT UPIU to verify that the device UTP Layer is ready.
//
Status = UfsExecNopCmds (Private);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Ufs Sending NOP IN command Error, Status = %r\n", Status));
Controller++;
continue;
}
//
// The host enables the device initialization completion by setting fDeviceInit flag.
//
Status = UfsSetFlag (Private, UfsFlagDevInit);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Ufs Set fDeviceInit Flag Error, Status = %r\n", Status));
Controller++;
continue;
}
//
// Get Ufs Device's Lun Info by reading Configuration Descriptor.
//
Status = UfsRwDeviceDesc (Private, TRUE, UfsConfigDesc, 0, 0, &Config, sizeof (UFS_CONFIG_DESC));
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Ufs Get Configuration Descriptor Error, Status = %r\n", Status));
Controller++;
continue;
}
for (Index = 0; Index < UFS_PEIM_MAX_LUNS; Index++) {
if (Config.UnitDescConfParams[Index].LunEn != 0) {
Private->Luns.BitMask |= (BIT0 << Index);
DEBUG ((EFI_D_INFO, "Ufs %d Lun %d is enabled\n", Controller, Index));
}
}
Status = PeiServicesInstallPpi (&Private->BlkIoPpiList);
Controller++;
}
return EFI_SUCCESS;
}
|