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