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