]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c
b12404aacb11fefe8a130b35932acf5caf842982
[mirror_edk2.git] / MdeModulePkg / Bus / Ufs / UfsPassThruDxe / UfsPassThru.c
1 /** @file
2
3 Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #include "UfsPassThru.h"
9
10 //
11 // Template for Ufs Pass Thru private data.
12 //
13 UFS_PASS_THRU_PRIVATE_DATA gUfsPassThruTemplate = {
14 UFS_PASS_THRU_SIG, // Signature
15 NULL, // Handle
16 { // ExtScsiPassThruMode
17 0xFFFFFFFF,
18 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO,
19 sizeof (UINTN)
20 },
21 { // ExtScsiPassThru
22 NULL,
23 UfsPassThruPassThru,
24 UfsPassThruGetNextTargetLun,
25 UfsPassThruBuildDevicePath,
26 UfsPassThruGetTargetLun,
27 UfsPassThruResetChannel,
28 UfsPassThruResetTargetLun,
29 UfsPassThruGetNextTarget
30 },
31 { // UfsDevConfig
32 UfsRwUfsDescriptor,
33 UfsRwUfsFlag,
34 UfsRwUfsAttribute
35 },
36 0, // UfsHostController
37 0, // UfsHcBase
38 {0, 0}, // UfsHcInfo
39 {NULL, NULL}, // UfsHcDriverInterface
40 0, // TaskTag
41 0, // UtpTrlBase
42 0, // Nutrs
43 0, // TrlMapping
44 0, // UtpTmrlBase
45 0, // Nutmrs
46 0, // TmrlMapping
47 { // Luns
48 {
49 UFS_LUN_0, // Ufs Common Lun 0
50 UFS_LUN_1, // Ufs Common Lun 1
51 UFS_LUN_2, // Ufs Common Lun 2
52 UFS_LUN_3, // Ufs Common Lun 3
53 UFS_LUN_4, // Ufs Common Lun 4
54 UFS_LUN_5, // Ufs Common Lun 5
55 UFS_LUN_6, // Ufs Common Lun 6
56 UFS_LUN_7, // Ufs Common Lun 7
57 UFS_WLUN_REPORT_LUNS, // Ufs Reports Luns Well Known Lun
58 UFS_WLUN_UFS_DEV, // Ufs Device Well Known Lun
59 UFS_WLUN_BOOT, // Ufs Boot Well Known Lun
60 UFS_WLUN_RPMB // RPMB Well Known Lun
61 },
62 0x0000, // By default don't expose any Luns.
63 0x0
64 },
65 NULL, // TimerEvent
66 { // Queue
67 NULL,
68 NULL
69 }
70 };
71
72 EFI_DRIVER_BINDING_PROTOCOL gUfsPassThruDriverBinding = {
73 UfsPassThruDriverBindingSupported,
74 UfsPassThruDriverBindingStart,
75 UfsPassThruDriverBindingStop,
76 0x10,
77 NULL,
78 NULL
79 };
80
81 UFS_DEVICE_PATH mUfsDevicePathTemplate = {
82 {
83 MESSAGING_DEVICE_PATH,
84 MSG_UFS_DP,
85 {
86 (UINT8) (sizeof (UFS_DEVICE_PATH)),
87 (UINT8) ((sizeof (UFS_DEVICE_PATH)) >> 8)
88 }
89 },
90 0,
91 0
92 };
93
94 UINT8 mUfsTargetId[TARGET_MAX_BYTES];
95
96 GLOBAL_REMOVE_IF_UNREFERENCED EDKII_UFS_HC_PLATFORM_PROTOCOL *mUfsHcPlatform;
97
98 /**
99 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
100 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
101 nonblocking I/O functionality is optional.
102
103 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
104 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
105 the id of the SCSI device to send the SCSI Request Packet. Each
106 transport driver may choose to utilize a subset of this size to suit the needs
107 of transport target representation. For example, a Fibre Channel driver
108 may use only 8 bytes (WWN) to represent an FC target.
109 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
110 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
111 specified by Target and Lun.
112 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
113 I/O is performed. If Event is NULL, then blocking I/O is performed. If
114 Event is not NULL and non blocking I/O is supported, then
115 nonblocking I/O is performed, and Event will be signaled when the
116 SCSI Request Packet completes.
117
118 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
119 commands, InTransferLength bytes were transferred from
120 InDataBuffer. For write and bi-directional commands,
121 OutTransferLength bytes were transferred by
122 OutDataBuffer.
123 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
124 could be transferred is returned in InTransferLength. For write
125 and bi-directional commands, OutTransferLength bytes were
126 transferred by OutDataBuffer.
127 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
128 SCSI Request Packets already queued. The caller may retry again later.
129 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
130 Packet.
131 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
132 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
133 by the host adapter. This includes the case of Bi-directional SCSI
134 commands not supported by the implementation. The SCSI Request
135 Packet was not sent, so no additional status information is available.
136 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
137
138 **/
139 EFI_STATUS
140 EFIAPI
141 UfsPassThruPassThru (
142 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
143 IN UINT8 *Target,
144 IN UINT64 Lun,
145 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,
146 IN EFI_EVENT Event OPTIONAL
147 )
148 {
149 EFI_STATUS Status;
150 UFS_PASS_THRU_PRIVATE_DATA *Private;
151 UINT8 UfsLun;
152 UINT16 Index;
153
154 Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
155
156 if ((Packet == NULL) || (Packet->Cdb == NULL)) {
157 return EFI_INVALID_PARAMETER;
158 }
159
160 //
161 // Don't support variable length CDB
162 //
163 if ((Packet->CdbLength != 6) && (Packet->CdbLength != 10) &&
164 (Packet->CdbLength != 12) && (Packet->CdbLength != 16)) {
165 return EFI_INVALID_PARAMETER;
166 }
167
168 if ((Packet->SenseDataLength != 0) && (Packet->SenseData == NULL)) {
169 return EFI_INVALID_PARAMETER;
170 }
171
172 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {
173 return EFI_INVALID_PARAMETER;
174 }
175
176 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {
177 return EFI_INVALID_PARAMETER;
178 }
179
180 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {
181 return EFI_INVALID_PARAMETER;
182 }
183
184 //
185 // For UFS 2.0 compatible device, 0 is always used to represent the location of the UFS device.
186 //
187 SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);
188 if ((Target == NULL) || (CompareMem(Target, mUfsTargetId, TARGET_MAX_BYTES) != 0)) {
189 return EFI_INVALID_PARAMETER;
190 }
191
192 //
193 // UFS 2.0 spec Section 10.6.7 - Translation of 8-bit UFS LUN to 64-bit SCSI LUN Address
194 // 0xC1 in the first 8 bits of the 64-bit address indicates a well known LUN address in the SAM SCSI format.
195 // The second 8 bits of the 64-bit address saves the corresponding 8-bit UFS LUN.
196 //
197 if ((UINT8)Lun == UFS_WLUN_PREFIX) {
198 UfsLun = BIT7 | (((UINT8*)&Lun)[1] & 0xFF);
199 } else if ((UINT8)Lun == 0) {
200 UfsLun = ((UINT8*)&Lun)[1] & 0xFF;
201 } else {
202 return EFI_INVALID_PARAMETER;
203 }
204
205 for (Index = 0; Index < UFS_MAX_LUNS; Index++) {
206 if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {
207 continue;
208 }
209
210 if (Private->Luns.Lun[Index] == UfsLun) {
211 break;
212 }
213 }
214
215 if (Index == UFS_MAX_LUNS) {
216 return EFI_INVALID_PARAMETER;
217 }
218
219 Status = UfsExecScsiCmds (Private, UfsLun, Packet, Event);
220
221 return Status;
222 }
223
224 /**
225 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
226 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
227 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
228 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
229 channel.
230
231 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
232 @param Target On input, a pointer to the Target ID (an array of size
233 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
234 On output, a pointer to the Target ID (an array of
235 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
236 channel. An input value of 0xF(all bytes in the array are 0xF) in the
237 Target array retrieves the Target ID of the first SCSI device present on a
238 SCSI channel.
239 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
240 channel. On output, a pointer to the LUN of the next SCSI device present
241 on a SCSI channel.
242
243 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
244 channel was returned in Target and Lun.
245 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
246 not returned on a previous call to GetNextTargetLun().
247 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
248
249 **/
250 EFI_STATUS
251 EFIAPI
252 UfsPassThruGetNextTargetLun (
253 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
254 IN OUT UINT8 **Target,
255 IN OUT UINT64 *Lun
256 )
257 {
258 UFS_PASS_THRU_PRIVATE_DATA *Private;
259 UINT8 UfsLun;
260 UINT16 Index;
261 UINT16 Next;
262
263 Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
264
265 if (Target == NULL || Lun == NULL) {
266 return EFI_INVALID_PARAMETER;
267 }
268
269 if (*Target == NULL) {
270 return EFI_INVALID_PARAMETER;
271 }
272
273 UfsLun = 0;
274 SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0xFF);
275 if (CompareMem (*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {
276 //
277 // If the array is all 0xFF's, return the first exposed Lun to caller.
278 //
279 SetMem (*Target, TARGET_MAX_BYTES, 0x00);
280 for (Index = 0; Index < UFS_MAX_LUNS; Index++) {
281 if ((Private->Luns.BitMask & (BIT0 << Index)) != 0) {
282 UfsLun = Private->Luns.Lun[Index];
283 break;
284 }
285 }
286 if (Index != UFS_MAX_LUNS) {
287 *Lun = 0;
288 if ((UfsLun & BIT7) == BIT7) {
289 ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;
290 ((UINT8*)Lun)[1] = UfsLun & ~BIT7;
291 } else {
292 ((UINT8*)Lun)[1] = UfsLun;
293 }
294 return EFI_SUCCESS;
295 } else {
296 return EFI_NOT_FOUND;
297 }
298 }
299
300 SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);
301 if (CompareMem (*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {
302 if (((UINT8*)Lun)[0] == UFS_WLUN_PREFIX) {
303 UfsLun = BIT7 | (((UINT8*)Lun)[1] & 0xFF);
304 } else if (((UINT8*)Lun)[0] == 0) {
305 UfsLun = ((UINT8*)Lun)[1] & 0xFF;
306 } else {
307 return EFI_NOT_FOUND;
308 }
309
310 for (Index = 0; Index < UFS_MAX_LUNS; Index++) {
311 if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {
312 continue;
313 }
314
315 if (Private->Luns.Lun[Index] != UfsLun) {
316 continue;
317 }
318
319 for (Next = Index + 1; Next < UFS_MAX_LUNS; Next++) {
320 if ((Private->Luns.BitMask & (BIT0 << Next)) != 0) {
321 UfsLun = Private->Luns.Lun[Next];
322 break;
323 }
324 }
325
326 if (Next == UFS_MAX_LUNS) {
327 return EFI_NOT_FOUND;
328 } else {
329 break;
330 }
331 }
332
333 if (Index != UFS_MAX_LUNS) {
334 *Lun = 0;
335 if ((UfsLun & BIT7) == BIT7) {
336 ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;
337 ((UINT8*)Lun)[1] = UfsLun & ~BIT7;
338 } else {
339 ((UINT8*)Lun)[1] = UfsLun;
340 }
341 return EFI_SUCCESS;
342 } else {
343 return EFI_NOT_FOUND;
344 }
345 }
346
347 return EFI_NOT_FOUND;
348 }
349
350 /**
351 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
352
353 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
354 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
355 Target ID of the SCSI device for which a device path node is to be
356 allocated and built. Transport drivers may chose to utilize a subset of
357 this size to suit the representation of targets. For example, a Fibre
358 Channel driver may use only 8 bytes (WWN) in the array to represent a
359 FC target.
360 @param Lun The LUN of the SCSI device for which a device path node is to be
361 allocated and built.
362 @param DevicePath A pointer to a single device path node that describes the SCSI device
363 specified by Target and Lun. This function is responsible for
364 allocating the buffer DevicePath with the boot service
365 AllocatePool(). It is the caller's responsibility to free
366 DevicePath when the caller is finished with DevicePath.
367
368 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
369 Target and Lun was allocated and returned in
370 DevicePath.
371 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
372 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
373 on the SCSI channel.
374 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
375
376 **/
377 EFI_STATUS
378 EFIAPI
379 UfsPassThruBuildDevicePath (
380 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
381 IN UINT8 *Target,
382 IN UINT64 Lun,
383 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
384 )
385 {
386 UFS_PASS_THRU_PRIVATE_DATA *Private;
387 EFI_DEV_PATH *DevicePathNode;
388 UINT8 UfsLun;
389 UINT16 Index;
390
391 Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
392
393 //
394 // Validate parameters passed in.
395 //
396 SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);
397 if (CompareMem (Target, mUfsTargetId, TARGET_MAX_BYTES) != 0) {
398 return EFI_INVALID_PARAMETER;
399 }
400
401 if ((UINT8)Lun == UFS_WLUN_PREFIX) {
402 UfsLun = BIT7 | (((UINT8*)&Lun)[1] & 0xFF);
403 } else if ((UINT8)Lun == 0) {
404 UfsLun = ((UINT8*)&Lun)[1] & 0xFF;
405 } else {
406 return EFI_NOT_FOUND;
407 }
408
409 for (Index = 0; Index < UFS_MAX_LUNS; Index++) {
410 if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {
411 continue;
412 }
413
414 if (Private->Luns.Lun[Index] == UfsLun) {
415 break;
416 }
417 }
418
419 if (Index == UFS_MAX_LUNS) {
420 return EFI_NOT_FOUND;
421 }
422
423 DevicePathNode = AllocateCopyPool (sizeof (UFS_DEVICE_PATH), &mUfsDevicePathTemplate);
424 if (DevicePathNode == NULL) {
425 return EFI_OUT_OF_RESOURCES;
426 }
427
428 DevicePathNode->Ufs.Pun = 0;
429 DevicePathNode->Ufs.Lun = UfsLun;
430
431 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;
432
433 return EFI_SUCCESS;
434 }
435
436 /**
437 Used to translate a device path node to a Target ID and LUN.
438
439 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
440 @param DevicePath A pointer to a single device path node that describes the SCSI device
441 on the SCSI channel.
442 @param Target A pointer to the Target Array which represents the ID of a SCSI device
443 on the SCSI channel.
444 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
445
446 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
447 LUN, and they were returned in Target and Lun.
448 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
449 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
450 does not exist.
451 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
452 DevicePath.
453
454 **/
455 EFI_STATUS
456 EFIAPI
457 UfsPassThruGetTargetLun (
458 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
459 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
460 OUT UINT8 **Target,
461 OUT UINT64 *Lun
462 )
463 {
464 UFS_PASS_THRU_PRIVATE_DATA *Private;
465 EFI_DEV_PATH *DevicePathNode;
466 UINT8 Pun;
467 UINT8 UfsLun;
468 UINT16 Index;
469
470 Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
471
472 //
473 // Validate parameters passed in.
474 //
475 if (DevicePath == NULL || Target == NULL || Lun == NULL) {
476 return EFI_INVALID_PARAMETER;
477 }
478
479 if (*Target == NULL) {
480 return EFI_INVALID_PARAMETER;
481 }
482
483 //
484 // Check whether the DevicePath belongs to UFS_DEVICE_PATH
485 //
486 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) || (DevicePath->SubType != MSG_UFS_DP) ||
487 (DevicePathNodeLength(DevicePath) != sizeof(UFS_DEVICE_PATH))) {
488 return EFI_UNSUPPORTED;
489 }
490
491 DevicePathNode = (EFI_DEV_PATH *) DevicePath;
492
493 Pun = (UINT8) DevicePathNode->Ufs.Pun;
494 UfsLun = (UINT8) DevicePathNode->Ufs.Lun;
495
496 if (Pun != 0) {
497 return EFI_NOT_FOUND;
498 }
499
500 for (Index = 0; Index < UFS_MAX_LUNS; Index++) {
501 if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {
502 continue;
503 }
504
505 if (Private->Luns.Lun[Index] == UfsLun) {
506 break;
507 }
508 }
509
510 if (Index == UFS_MAX_LUNS) {
511 return EFI_NOT_FOUND;
512 }
513
514 SetMem (*Target, TARGET_MAX_BYTES, 0x00);
515 *Lun = 0;
516 if ((UfsLun & BIT7) == BIT7) {
517 ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;
518 ((UINT8*)Lun)[1] = UfsLun & ~BIT7;
519 } else {
520 ((UINT8*)Lun)[1] = UfsLun;
521 }
522 return EFI_SUCCESS;
523 }
524
525 /**
526 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
527
528 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
529
530 @retval EFI_SUCCESS The SCSI channel was reset.
531 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
532 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
533 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
534
535 **/
536 EFI_STATUS
537 EFIAPI
538 UfsPassThruResetChannel (
539 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This
540 )
541 {
542 //
543 // Return success directly then upper layer driver could think reset channel operation is done.
544 //
545 return EFI_SUCCESS;
546 }
547
548 /**
549 Resets a SCSI logical unit that is connected to a SCSI channel.
550
551 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
552 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
553 target port ID of the SCSI device containing the SCSI logical unit to
554 reset. Transport drivers may chose to utilize a subset of this array to suit
555 the representation of their targets.
556 @param Lun The LUN of the SCSI device to reset.
557
558 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
559 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
560 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
561 specified by Target and Lun.
562 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
563 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
564 specified by Target and Lun.
565
566 **/
567 EFI_STATUS
568 EFIAPI
569 UfsPassThruResetTargetLun (
570 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
571 IN UINT8 *Target,
572 IN UINT64 Lun
573 )
574 {
575 //
576 // Return success directly then upper layer driver could think reset target LUN operation is done.
577 //
578 return EFI_SUCCESS;
579 }
580
581 /**
582 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
583 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
584 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
585 see if a SCSI device is actually present at that location on the SCSI channel.
586
587 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
588 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
589 On output, a pointer to the Target ID (an array of
590 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
591 channel. An input value of 0xF(all bytes in the array are 0xF) in the
592 Target array retrieves the Target ID of the first SCSI device present on a
593 SCSI channel.
594
595 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
596 channel was returned in Target.
597 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
598 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
599 returned on a previous call to GetNextTarget().
600 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
601
602 **/
603 EFI_STATUS
604 EFIAPI
605 UfsPassThruGetNextTarget (
606 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
607 IN OUT UINT8 **Target
608 )
609 {
610 if (Target == NULL || *Target == NULL) {
611 return EFI_INVALID_PARAMETER;
612 }
613
614 SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0xFF);
615 if (CompareMem(*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {
616 SetMem (*Target, TARGET_MAX_BYTES, 0x00);
617 return EFI_SUCCESS;
618 }
619
620 return EFI_NOT_FOUND;
621 }
622
623 /**
624 Tests to see if this driver supports a given controller. If a child device is provided,
625 it further tests to see if this driver supports creating a handle for the specified child device.
626
627 This function checks to see if the driver specified by This supports the device specified by
628 ControllerHandle. Drivers will typically use the device path attached to
629 ControllerHandle and/or the services from the bus I/O abstraction attached to
630 ControllerHandle to determine if the driver supports ControllerHandle. This function
631 may be called many times during platform initialization. In order to reduce boot times, the tests
632 performed by this function must be very small, and take as little time as possible to execute. This
633 function must not change the state of any hardware devices, and this function must be aware that the
634 device specified by ControllerHandle may already be managed by the same driver or a
635 different driver. This function must match its calls to AllocatePages() with FreePages(),
636 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
637 Since ControllerHandle may have been previously started by the same driver, if a protocol is
638 already in the opened state, then it must not be closed with CloseProtocol(). This is required
639 to guarantee the state of ControllerHandle is not modified by this function.
640
641 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
642 @param[in] ControllerHandle The handle of the controller to test. This handle
643 must support a protocol interface that supplies
644 an I/O abstraction to the driver.
645 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
646 parameter is ignored by device drivers, and is optional for bus
647 drivers. For bus drivers, if this parameter is not NULL, then
648 the bus driver must determine if the bus controller specified
649 by ControllerHandle and the child controller specified
650 by RemainingDevicePath are both supported by this
651 bus driver.
652
653 @retval EFI_SUCCESS The device specified by ControllerHandle and
654 RemainingDevicePath is supported by the driver specified by This.
655 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
656 RemainingDevicePath is already being managed by the driver
657 specified by This.
658 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
659 RemainingDevicePath is already being managed by a different
660 driver or an application that requires exclusive access.
661 Currently not implemented.
662 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
663 RemainingDevicePath is not supported by the driver specified by This.
664 **/
665 EFI_STATUS
666 EFIAPI
667 UfsPassThruDriverBindingSupported (
668 IN EFI_DRIVER_BINDING_PROTOCOL *This,
669 IN EFI_HANDLE Controller,
670 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
671 )
672 {
673 EFI_STATUS Status;
674 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
675 EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHostController;
676
677 //
678 // Ufs Pass Thru driver is a device driver, and should ingore the
679 // "RemainingDevicePath" according to UEFI spec
680 //
681 Status = gBS->OpenProtocol (
682 Controller,
683 &gEfiDevicePathProtocolGuid,
684 (VOID *) &ParentDevicePath,
685 This->DriverBindingHandle,
686 Controller,
687 EFI_OPEN_PROTOCOL_BY_DRIVER
688 );
689 if (EFI_ERROR (Status)) {
690 //
691 // EFI_ALREADY_STARTED is also an error
692 //
693 return Status;
694 }
695 //
696 // Close the protocol because we don't use it here
697 //
698 gBS->CloseProtocol (
699 Controller,
700 &gEfiDevicePathProtocolGuid,
701 This->DriverBindingHandle,
702 Controller
703 );
704
705 Status = gBS->OpenProtocol (
706 Controller,
707 &gEdkiiUfsHostControllerProtocolGuid,
708 (VOID **) &UfsHostController,
709 This->DriverBindingHandle,
710 Controller,
711 EFI_OPEN_PROTOCOL_BY_DRIVER
712 );
713
714 if (EFI_ERROR (Status)) {
715 //
716 // EFI_ALREADY_STARTED is also an error
717 //
718 return Status;
719 }
720
721 //
722 // Close the I/O Abstraction(s) used to perform the supported test
723 //
724 gBS->CloseProtocol (
725 Controller,
726 &gEdkiiUfsHostControllerProtocolGuid,
727 This->DriverBindingHandle,
728 Controller
729 );
730
731 return EFI_SUCCESS;
732 }
733
734 /**
735 Finishes device initialization by setting fDeviceInit flag and waiting untill device responds by
736 clearing it.
737
738 @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA.
739
740 @retval EFI_SUCCESS The operation succeeds.
741 @retval Others The operation fails.
742
743 **/
744 EFI_STATUS
745 UfsFinishDeviceInitialization (
746 IN UFS_PASS_THRU_PRIVATE_DATA *Private
747 )
748 {
749 EFI_STATUS Status;
750 UINT8 DeviceInitStatus;
751 UINT8 Timeout;
752
753 DeviceInitStatus = 0xFF;
754
755 //
756 // The host enables the device initialization completion by setting fDeviceInit flag.
757 //
758 Status = UfsSetFlag (Private, UfsFlagDevInit);
759 if (EFI_ERROR (Status)) {
760 return Status;
761 }
762
763 Timeout = 5;
764 do {
765 Status = UfsReadFlag (Private, UfsFlagDevInit, &DeviceInitStatus);
766 if (EFI_ERROR (Status)) {
767 return Status;
768 }
769 MicroSecondDelay (1);
770 Timeout--;
771 } while (DeviceInitStatus != 0 && Timeout != 0);
772
773 return EFI_SUCCESS;
774 }
775
776 /**
777 Starts a device controller or a bus controller.
778
779 The Start() function is designed to be invoked from the EFI boot service ConnectController().
780 As a result, much of the error checking on the parameters to Start() has been moved into this
781 common boot service. It is legal to call Start() from other locations,
782 but the following calling restrictions must be followed or the system behavior will not be deterministic.
783 1. ControllerHandle must be a valid EFI_HANDLE.
784 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
785 EFI_DEVICE_PATH_PROTOCOL.
786 3. Prior to calling Start(), the Supported() function for the driver specified by This must
787 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
788
789 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
790 @param[in] ControllerHandle The handle of the controller to start. This handle
791 must support a protocol interface that supplies
792 an I/O abstraction to the driver.
793 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
794 parameter is ignored by device drivers, and is optional for bus
795 drivers. For a bus driver, if this parameter is NULL, then handles
796 for all the children of Controller are created by this driver.
797 If this parameter is not NULL and the first Device Path Node is
798 not the End of Device Path Node, then only the handle for the
799 child device specified by the first Device Path Node of
800 RemainingDevicePath is created by this driver.
801 If the first Device Path Node of RemainingDevicePath is
802 the End of Device Path Node, no child handle is created by this
803 driver.
804
805 @retval EFI_SUCCESS The device was started.
806 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
807 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
808 @retval Others The driver failded to start the device.
809
810 **/
811 EFI_STATUS
812 EFIAPI
813 UfsPassThruDriverBindingStart (
814 IN EFI_DRIVER_BINDING_PROTOCOL *This,
815 IN EFI_HANDLE Controller,
816 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
817 )
818 {
819 EFI_STATUS Status;
820 EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;
821 UFS_PASS_THRU_PRIVATE_DATA *Private;
822 UINTN UfsHcBase;
823 UINT32 Index;
824 UFS_UNIT_DESC UnitDescriptor;
825 UINT32 UnitDescriptorSize;
826
827 Status = EFI_SUCCESS;
828 UfsHc = NULL;
829 Private = NULL;
830 UfsHcBase = 0;
831
832 DEBUG ((DEBUG_INFO, "==UfsPassThru Start== Controller = %x\n", Controller));
833
834 Status = gBS->OpenProtocol (
835 Controller,
836 &gEdkiiUfsHostControllerProtocolGuid,
837 (VOID **) &UfsHc,
838 This->DriverBindingHandle,
839 Controller,
840 EFI_OPEN_PROTOCOL_BY_DRIVER
841 );
842
843 if (EFI_ERROR (Status)) {
844 DEBUG ((DEBUG_ERROR, "Open Ufs Host Controller Protocol Error, Status = %r\n", Status));
845 goto Error;
846 }
847
848 //
849 // Get the UFS Host Controller MMIO Bar Base Address.
850 //
851 Status = UfsHc->GetUfsHcMmioBar (UfsHc, &UfsHcBase);
852 if (EFI_ERROR (Status)) {
853 DEBUG ((DEBUG_ERROR, "Get Ufs Host Controller Mmio Bar Error, Status = %r\n", Status));
854 goto Error;
855 }
856
857 //
858 // Initialize Ufs Pass Thru private data for managed UFS Host Controller.
859 //
860 Private = AllocateCopyPool (sizeof (UFS_PASS_THRU_PRIVATE_DATA), &gUfsPassThruTemplate);
861 if (Private == NULL) {
862 DEBUG ((DEBUG_ERROR, "Unable to allocate Ufs Pass Thru private data\n"));
863 Status = EFI_OUT_OF_RESOURCES;
864 goto Error;
865 }
866
867 Private->ExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode;
868 Private->UfsHostController = UfsHc;
869 Private->UfsHcBase = UfsHcBase;
870 Private->Handle = Controller;
871 Private->UfsHcDriverInterface.UfsHcProtocol = UfsHc;
872 Private->UfsHcDriverInterface.UfsExecUicCommand = UfsHcDriverInterfaceExecUicCommand;
873 InitializeListHead (&Private->Queue);
874
875 //
876 // This has to be done before initializing UfsHcInfo or calling the UfsControllerInit
877 //
878 if (mUfsHcPlatform == NULL) {
879 Status = gBS->LocateProtocol (&gEdkiiUfsHcPlatformProtocolGuid, NULL, (VOID**)&mUfsHcPlatform);
880 if (EFI_ERROR (Status)) {
881 DEBUG ((DEBUG_INFO, "No UfsHcPlatformProtocol present\n"));
882 }
883 }
884
885 Status = GetUfsHcInfo (Private);
886 if (EFI_ERROR (Status)) {
887 DEBUG ((DEBUG_ERROR, "Failed to initialize UfsHcInfo\n"));
888 goto Error;
889 }
890
891 //
892 // Initialize UFS Host Controller H/W.
893 //
894 Status = UfsControllerInit (Private);
895 if (EFI_ERROR (Status)) {
896 DEBUG ((DEBUG_ERROR, "Ufs Host Controller Initialization Error, Status = %r\n", Status));
897 goto Error;
898 }
899
900 //
901 // UFS 2.0 spec Section 13.1.3.3:
902 // At the end of the UFS Interconnect Layer initialization on both host and device side,
903 // the host shall send a NOP OUT UPIU to verify that the device UTP Layer is ready.
904 //
905 Status = UfsExecNopCmds (Private);
906 if (EFI_ERROR (Status)) {
907 DEBUG ((DEBUG_ERROR, "Ufs Sending NOP IN command Error, Status = %r\n", Status));
908 goto Error;
909 }
910
911 Status = UfsFinishDeviceInitialization (Private);
912 if (EFI_ERROR (Status)) {
913 DEBUG ((DEBUG_ERROR, "Device failed to finish initialization, Status = %r\n", Status));
914 goto Error;
915 }
916
917 //
918 // Check if 8 common luns are active and set corresponding bit mask.
919 // TODO: Parse device descriptor to decide if exposing RPMB LUN to upper layer for authentication access.
920 //
921 UnitDescriptorSize = sizeof (UFS_UNIT_DESC);
922 for (Index = 0; Index < 8; Index++) {
923 Status = UfsRwDeviceDesc (Private, TRUE, UfsUnitDesc, (UINT8) Index, 0, &UnitDescriptor, &UnitDescriptorSize);
924 if (EFI_ERROR (Status)) {
925 DEBUG ((DEBUG_ERROR, "Failed to read unit descriptor, index = %X, status = %r\n", Index, Status));
926 continue;
927 }
928 if (UnitDescriptor.LunEn == 0x1) {
929 DEBUG ((DEBUG_INFO, "UFS LUN %X is enabled\n", Index));
930 Private->Luns.BitMask |= (BIT0 << Index);
931 }
932 }
933
934 //
935 // Start the asynchronous interrupt monitor
936 //
937 Status = gBS->CreateEvent (
938 EVT_TIMER | EVT_NOTIFY_SIGNAL,
939 TPL_NOTIFY,
940 ProcessAsyncTaskList,
941 Private,
942 &Private->TimerEvent
943 );
944 if (EFI_ERROR (Status)) {
945 DEBUG ((DEBUG_ERROR, "Ufs Create Async Tasks Event Error, Status = %r\n", Status));
946 goto Error;
947 }
948
949 Status = gBS->SetTimer (
950 Private->TimerEvent,
951 TimerPeriodic,
952 UFS_HC_ASYNC_TIMER
953 );
954 if (EFI_ERROR (Status)) {
955 DEBUG ((DEBUG_ERROR, "Ufs Set Periodic Timer Error, Status = %r\n", Status));
956 goto Error;
957 }
958
959 Status = gBS->InstallMultipleProtocolInterfaces (
960 &Controller,
961 &gEfiExtScsiPassThruProtocolGuid,
962 &(Private->ExtScsiPassThru),
963 &gEfiUfsDeviceConfigProtocolGuid,
964 &(Private->UfsDevConfig),
965 NULL
966 );
967 ASSERT_EFI_ERROR (Status);
968
969 return EFI_SUCCESS;
970
971 Error:
972 if (Private != NULL) {
973 if (Private->TmrlMapping != NULL) {
974 UfsHc->Unmap (UfsHc, Private->TmrlMapping);
975 }
976 if (Private->UtpTmrlBase != NULL) {
977 UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutmrs * sizeof (UTP_TMRD)), Private->UtpTmrlBase);
978 }
979
980 if (Private->TrlMapping != NULL) {
981 UfsHc->Unmap (UfsHc, Private->TrlMapping);
982 }
983 if (Private->UtpTrlBase != NULL) {
984 UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutrs * sizeof (UTP_TMRD)), Private->UtpTrlBase);
985 }
986
987 if (Private->TimerEvent != NULL) {
988 gBS->CloseEvent (Private->TimerEvent);
989 }
990
991 FreePool (Private);
992 }
993
994 if (UfsHc != NULL) {
995 gBS->CloseProtocol (
996 Controller,
997 &gEdkiiUfsHostControllerProtocolGuid,
998 This->DriverBindingHandle,
999 Controller
1000 );
1001 }
1002
1003 return Status;
1004 }
1005
1006 /**
1007 Stops a device controller or a bus controller.
1008
1009 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1010 As a result, much of the error checking on the parameters to Stop() has been moved
1011 into this common boot service. It is legal to call Stop() from other locations,
1012 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1013 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
1014 same driver's Start() function.
1015 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1016 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1017 Start() function, and the Start() function must have called OpenProtocol() on
1018 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1019
1020 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1021 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1022 support a bus specific I/O protocol for the driver
1023 to use to stop the device.
1024 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1025 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1026 if NumberOfChildren is 0.
1027
1028 @retval EFI_SUCCESS The device was stopped.
1029 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1030
1031 **/
1032 EFI_STATUS
1033 EFIAPI
1034 UfsPassThruDriverBindingStop (
1035 IN EFI_DRIVER_BINDING_PROTOCOL *This,
1036 IN EFI_HANDLE Controller,
1037 IN UINTN NumberOfChildren,
1038 IN EFI_HANDLE *ChildHandleBuffer
1039 )
1040 {
1041 EFI_STATUS Status;
1042 UFS_PASS_THRU_PRIVATE_DATA *Private;
1043 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
1044 EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;
1045 UFS_PASS_THRU_TRANS_REQ *TransReq;
1046 LIST_ENTRY *Entry;
1047 LIST_ENTRY *NextEntry;
1048
1049 DEBUG ((DEBUG_INFO, "==UfsPassThru Stop== Controller Controller = %x\n", Controller));
1050
1051 Status = gBS->OpenProtocol (
1052 Controller,
1053 &gEfiExtScsiPassThruProtocolGuid,
1054 (VOID **) &ExtScsiPassThru,
1055 This->DriverBindingHandle,
1056 Controller,
1057 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1058 );
1059
1060 if (EFI_ERROR (Status)) {
1061 return EFI_DEVICE_ERROR;
1062 }
1063
1064 Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (ExtScsiPassThru);
1065 UfsHc = Private->UfsHostController;
1066
1067 //
1068 // Cleanup the resources of I/O requests in the async I/O queue
1069 //
1070 if (!IsListEmpty(&Private->Queue)) {
1071 EFI_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->Queue) {
1072 TransReq = UFS_PASS_THRU_TRANS_REQ_FROM_THIS (Entry);
1073
1074 //
1075 // TODO: Should find/add a proper host adapter return status for this
1076 // case.
1077 //
1078 TransReq->Packet->HostAdapterStatus =
1079 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR;
1080
1081 SignalCallerEvent (Private, TransReq);
1082 }
1083 }
1084
1085 Status = gBS->UninstallMultipleProtocolInterfaces (
1086 Controller,
1087 &gEfiExtScsiPassThruProtocolGuid,
1088 &(Private->ExtScsiPassThru),
1089 &gEfiUfsDeviceConfigProtocolGuid,
1090 &(Private->UfsDevConfig),
1091 NULL
1092 );
1093
1094 if (EFI_ERROR (Status)) {
1095 return EFI_DEVICE_ERROR;
1096 }
1097
1098 //
1099 // Stop Ufs Host Controller
1100 //
1101 Status = UfsControllerStop (Private);
1102 ASSERT_EFI_ERROR (Status);
1103
1104 if (Private->TmrlMapping != NULL) {
1105 UfsHc->Unmap (UfsHc, Private->TmrlMapping);
1106 }
1107 if (Private->UtpTmrlBase != NULL) {
1108 UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutmrs * sizeof (UTP_TMRD)), Private->UtpTmrlBase);
1109 }
1110
1111 if (Private->TrlMapping != NULL) {
1112 UfsHc->Unmap (UfsHc, Private->TrlMapping);
1113 }
1114 if (Private->UtpTrlBase != NULL) {
1115 UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutrs * sizeof (UTP_TMRD)), Private->UtpTrlBase);
1116 }
1117
1118 if (Private->TimerEvent != NULL) {
1119 gBS->CloseEvent (Private->TimerEvent);
1120 }
1121
1122 FreePool (Private);
1123
1124 //
1125 // Close protocols opened by UfsPassThru controller driver
1126 //
1127 gBS->CloseProtocol (
1128 Controller,
1129 &gEdkiiUfsHostControllerProtocolGuid,
1130 This->DriverBindingHandle,
1131 Controller
1132 );
1133
1134 return Status;
1135 }
1136
1137
1138 /**
1139 The user Entry Point for module UfsPassThru. The user code starts with this function.
1140
1141 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1142 @param[in] SystemTable A pointer to the EFI System Table.
1143
1144 @retval EFI_SUCCESS The entry point is executed successfully.
1145 @retval other Some error occurs when executing this entry point.
1146
1147 **/
1148 EFI_STATUS
1149 EFIAPI
1150 InitializeUfsPassThru (
1151 IN EFI_HANDLE ImageHandle,
1152 IN EFI_SYSTEM_TABLE *SystemTable
1153 )
1154 {
1155 EFI_STATUS Status;
1156
1157 //
1158 // Install driver model protocol(s).
1159 //
1160 Status = EfiLibInstallDriverBindingComponentName2 (
1161 ImageHandle,
1162 SystemTable,
1163 &gUfsPassThruDriverBinding,
1164 ImageHandle,
1165 &gUfsPassThruComponentName,
1166 &gUfsPassThruComponentName2
1167 );
1168 ASSERT_EFI_ERROR (Status);
1169
1170 return Status;
1171 }