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