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