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