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