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