3 Copyright (c) 2014 - 2018, 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
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.
14 #ifndef _UFS_BLOCK_IO_PEI_H_
15 #define _UFS_BLOCK_IO_PEI_H_
19 #include <Ppi/UfsHostController.h>
20 #include <Ppi/BlockIo.h>
21 #include <Ppi/BlockIo2.h>
22 #include <Ppi/IoMmu.h>
23 #include <Ppi/EndOfPeiPhase.h>
25 #include <Library/DebugLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/IoLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/PeiServicesLib.h>
33 #include <IndustryStandard/Scsi.h>
38 #define UFS_PEIM_HC_SIG SIGNATURE_32 ('U', 'F', 'S', 'H')
40 #define UFS_PEIM_MAX_LUNS 8
43 UINT8 Lun
[UFS_PEIM_MAX_LUNS
];
44 UINT16 BitMask
:12; // Bit 0~7 is for common luns. Bit 8~11 is reserved for those well known luns
46 } UFS_PEIM_EXPOSED_LUNS
;
50 /// The timeout, in 100 ns units, to use for the execution of this SCSI
51 /// Request Packet. A Timeout value of 0 means that this function
52 /// will wait indefinitely for the SCSI Request Packet to execute. If
53 /// Timeout is greater than zero, then this function will return
54 /// EFI_TIMEOUT if the time required to execute the SCSI
55 /// Request Packet is greater than Timeout.
59 /// A pointer to the data buffer to transfer between the SCSI
60 /// controller and the SCSI device for read and bidirectional commands.
64 /// A pointer to the data buffer to transfer between the SCSI
65 /// controller and the SCSI device for write or bidirectional commands.
69 /// A pointer to the sense data that was generated by the execution of
70 /// the SCSI Request Packet.
74 /// A pointer to buffer that contains the Command Data Block to
75 /// send to the SCSI device specified by Target and Lun.
79 /// On Input, the size, in bytes, of InDataBuffer. On output, the
80 /// number of bytes transferred between the SCSI controller and the SCSI device.
82 UINT32 InTransferLength
;
84 /// On Input, the size, in bytes of OutDataBuffer. On Output, the
85 /// Number of bytes transferred between SCSI Controller and the SCSI device.
87 UINT32 OutTransferLength
;
89 /// The length, in bytes, of the buffer Cdb. The standard values are 6,
90 /// 10, 12, and 16, but other values are possible if a variable length CDB is used.
94 /// The direction of the data transfer. 0 for reads, 1 for writes. A
95 /// value of 2 is Reserved for Bi-Directional SCSI commands.
99 /// On input, the length in bytes of the SenseData buffer. On
100 /// output, the number of bytes written to the SenseData buffer.
102 UINT8 SenseDataLength
;
103 } UFS_SCSI_REQUEST_PACKET
;
105 typedef struct _UFS_PEIM_HC_PRIVATE_DATA
{
107 EFI_HANDLE Controller
;
109 UFS_PEIM_MEM_POOL
*Pool
;
111 EFI_PEI_RECOVERY_BLOCK_IO_PPI BlkIoPpi
;
112 EFI_PEI_RECOVERY_BLOCK_IO2_PPI BlkIo2Ppi
;
113 EFI_PEI_PPI_DESCRIPTOR BlkIoPpiList
;
114 EFI_PEI_PPI_DESCRIPTOR BlkIo2PpiList
;
115 EFI_PEI_BLOCK_IO2_MEDIA Media
[UFS_PEIM_MAX_LUNS
];
118 // EndOfPei callback is used to stop the UFS DMA operation
119 // after exit PEI phase.
121 EFI_PEI_NOTIFY_DESCRIPTOR EndOfPeiNotifyList
;
135 UFS_PEIM_EXPOSED_LUNS Luns
;
136 } UFS_PEIM_HC_PRIVATE_DATA
;
138 #define UFS_TIMEOUT MultU64x32((UINT64)(3), 10000000)
140 #define ROUNDUP8(x) (((x) % 8 == 0) ? (x) : ((x) / 8 + 1) * 8)
142 #define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) == 0)
144 #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG)
145 #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, BlkIo2Ppi, UFS_PEIM_HC_SIG)
146 #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, EndOfPeiNotifyList, UFS_PEIM_HC_SIG)
148 #define UFS_SCSI_OP_LENGTH_SIX 0x6
149 #define UFS_SCSI_OP_LENGTH_TEN 0xa
150 #define UFS_SCSI_OP_LENGTH_SIXTEEN 0x10
152 typedef struct _UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
{
160 UINT32 InTransferLength
;
161 UINT32 OutTransferLength
;
164 } UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
;
167 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
169 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
170 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.
171 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the
174 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
175 commands, InTransferLength bytes were transferred from
176 InDataBuffer. For write and bi-directional commands,
177 OutTransferLength bytes were transferred by
179 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
181 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
182 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
187 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
189 IN OUT UFS_SCSI_REQUEST_PACKET
*Packet
193 Initialize the UFS host controller.
195 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
197 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
198 @retval Others A device error occurred while initializing the controller.
203 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
207 Stop the UFS host controller.
209 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
211 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
212 @retval Others A device error occurred while stopping the controller.
217 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
221 Set specified flag to 1 on a UFS device.
223 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
224 @param[in] FlagId The ID of flag to be set.
226 @retval EFI_SUCCESS The flag was set successfully.
227 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.
228 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.
233 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
238 Read or write specified device descriptor of a UFS device.
240 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
241 @param[in] Read The boolean variable to show r/w direction.
242 @param[in] DescId The ID of device descriptor.
243 @param[in] Index The Index of device descriptor.
244 @param[in] Selector The Selector of device descriptor.
245 @param[in, out] Descriptor The buffer of device descriptor to be read or written.
246 @param[in] DescSize The size of device descriptor buffer.
248 @retval EFI_SUCCESS The device descriptor was read/written successfully.
249 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
250 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
255 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
260 IN OUT VOID
*Descriptor
,
265 Sends NOP IN cmd to a UFS device for initialization process request.
266 For more details, please refer to UFS 2.0 spec Figure 13.3.
268 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
270 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was
271 received successfully.
272 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.
273 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
274 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.
279 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
283 Gets the count of block I/O devices that one specific block driver detects.
285 This function is used for getting the count of block I/O devices that one
286 specific block driver detects. To the PEI ATAPI driver, it returns the number
287 of all the detected ATAPI devices it detects during the enumeration process.
288 To the PEI legacy floppy driver, it returns the number of all the legacy
289 devices it finds during its enumeration process. If no device is detected,
290 then the function will return zero.
292 @param[in] PeiServices General-purpose services that are available
294 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
296 @param[out] NumberBlockDevices The number of block I/O devices discovered.
298 @retval EFI_SUCCESS The operation performed successfully.
303 UfsBlockIoPeimGetDeviceNo (
304 IN EFI_PEI_SERVICES
**PeiServices
,
305 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
306 OUT UINTN
*NumberBlockDevices
310 Gets a block device's media information.
312 This function will provide the caller with the specified block device's media
313 information. If the media changes, calling this function will update the media
314 information accordingly.
316 @param[in] PeiServices General-purpose services that are available to every
318 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
319 @param[in] DeviceIndex Specifies the block device to which the function wants
320 to talk. Because the driver that implements Block I/O
321 PPIs will manage multiple block devices, the PPIs that
322 want to talk to a single device must specify the
323 device index that was assigned during the enumeration
324 process. This index is a number from one to
326 @param[out] MediaInfo The media information of the specified block media.
327 The caller is responsible for the ownership of this
331 The MediaInfo structure describes an enumeration of possible block device
332 types. This enumeration exists because no device paths are actually passed
333 across interfaces that describe the type or class of hardware that is publishing
334 the block I/O interface. This enumeration will allow for policy decisions
335 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
336 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
337 by a given device type, they should be reported in ascending order; this
338 order also applies to nested partitions, such as legacy MBR, where the
339 outermost partitions would have precedence in the reporting order. The
340 same logic applies to systems such as IDE that have precedence relationships
341 like "Master/Slave" or "Primary/Secondary". The master device should be
342 reported first, the slave second.
344 @retval EFI_SUCCESS Media information about the specified block device
345 was obtained successfully.
346 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
352 UfsBlockIoPeimGetMediaInfo (
353 IN EFI_PEI_SERVICES
**PeiServices
,
354 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
355 IN UINTN DeviceIndex
,
356 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
360 Reads the requested number of blocks from the specified block device.
362 The function reads the requested number of blocks from the device. All the
363 blocks are read, or an error is returned. If there is no media in the device,
364 the function returns EFI_NO_MEDIA.
366 @param[in] PeiServices General-purpose services that are available to
368 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
369 @param[in] DeviceIndex Specifies the block device to which the function wants
370 to talk. Because the driver that implements Block I/O
371 PPIs will manage multiple block devices, PPIs that
372 want to talk to a single device must specify the device
373 index that was assigned during the enumeration process.
374 This index is a number from one to NumberBlockDevices.
375 @param[in] StartLBA The starting logical block address (LBA) to read from
377 @param[in] BufferSize The size of the Buffer in bytes. This number must be
378 a multiple of the intrinsic block size of the device.
379 @param[out] Buffer A pointer to the destination buffer for the data.
380 The caller is responsible for the ownership of the
383 @retval EFI_SUCCESS The data was read correctly from the device.
384 @retval EFI_DEVICE_ERROR The device reported an error while attempting
385 to perform the read operation.
386 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
387 valid, or the buffer is not properly aligned.
388 @retval EFI_NO_MEDIA There is no media in the device.
389 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
390 the intrinsic block size of the device.
395 UfsBlockIoPeimReadBlocks (
396 IN EFI_PEI_SERVICES
**PeiServices
,
397 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
398 IN UINTN DeviceIndex
,
399 IN EFI_PEI_LBA StartLBA
,
405 Gets the count of block I/O devices that one specific block driver detects.
407 This function is used for getting the count of block I/O devices that one
408 specific block driver detects. To the PEI ATAPI driver, it returns the number
409 of all the detected ATAPI devices it detects during the enumeration process.
410 To the PEI legacy floppy driver, it returns the number of all the legacy
411 devices it finds during its enumeration process. If no device is detected,
412 then the function will return zero.
414 @param[in] PeiServices General-purpose services that are available
416 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
418 @param[out] NumberBlockDevices The number of block I/O devices discovered.
420 @retval EFI_SUCCESS The operation performed successfully.
425 UfsBlockIoPeimGetDeviceNo2 (
426 IN EFI_PEI_SERVICES
**PeiServices
,
427 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
428 OUT UINTN
*NumberBlockDevices
432 Gets a block device's media information.
434 This function will provide the caller with the specified block device's media
435 information. If the media changes, calling this function will update the media
436 information accordingly.
438 @param[in] PeiServices General-purpose services that are available to every
440 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
441 @param[in] DeviceIndex Specifies the block device to which the function wants
442 to talk. Because the driver that implements Block I/O
443 PPIs will manage multiple block devices, the PPIs that
444 want to talk to a single device must specify the
445 device index that was assigned during the enumeration
446 process. This index is a number from one to
448 @param[out] MediaInfo The media information of the specified block media.
449 The caller is responsible for the ownership of this
453 The MediaInfo structure describes an enumeration of possible block device
454 types. This enumeration exists because no device paths are actually passed
455 across interfaces that describe the type or class of hardware that is publishing
456 the block I/O interface. This enumeration will allow for policy decisions
457 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
458 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
459 by a given device type, they should be reported in ascending order; this
460 order also applies to nested partitions, such as legacy MBR, where the
461 outermost partitions would have precedence in the reporting order. The
462 same logic applies to systems such as IDE that have precedence relationships
463 like "Master/Slave" or "Primary/Secondary". The master device should be
464 reported first, the slave second.
466 @retval EFI_SUCCESS Media information about the specified block device
467 was obtained successfully.
468 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
474 UfsBlockIoPeimGetMediaInfo2 (
475 IN EFI_PEI_SERVICES
**PeiServices
,
476 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
477 IN UINTN DeviceIndex
,
478 OUT EFI_PEI_BLOCK_IO2_MEDIA
*MediaInfo
482 Reads the requested number of blocks from the specified block device.
484 The function reads the requested number of blocks from the device. All the
485 blocks are read, or an error is returned. If there is no media in the device,
486 the function returns EFI_NO_MEDIA.
488 @param[in] PeiServices General-purpose services that are available to
490 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
491 @param[in] DeviceIndex Specifies the block device to which the function wants
492 to talk. Because the driver that implements Block I/O
493 PPIs will manage multiple block devices, PPIs that
494 want to talk to a single device must specify the device
495 index that was assigned during the enumeration process.
496 This index is a number from one to NumberBlockDevices.
497 @param[in] StartLBA The starting logical block address (LBA) to read from
499 @param[in] BufferSize The size of the Buffer in bytes. This number must be
500 a multiple of the intrinsic block size of the device.
501 @param[out] Buffer A pointer to the destination buffer for the data.
502 The caller is responsible for the ownership of the
505 @retval EFI_SUCCESS The data was read correctly from the device.
506 @retval EFI_DEVICE_ERROR The device reported an error while attempting
507 to perform the read operation.
508 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
509 valid, or the buffer is not properly aligned.
510 @retval EFI_NO_MEDIA There is no media in the device.
511 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
512 the intrinsic block size of the device.
517 UfsBlockIoPeimReadBlocks2 (
518 IN EFI_PEI_SERVICES
**PeiServices
,
519 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
520 IN UINTN DeviceIndex
,
521 IN EFI_PEI_LBA StartLBA
,
527 Initialize the memory management pool for the host controller.
529 @param Private The Ufs Peim driver private data.
531 @retval EFI_SUCCESS The memory pool is initialized.
532 @retval Others Fail to init the memory pool.
537 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
541 Release the memory management pool.
543 @param Pool The memory pool to free.
545 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
546 @retval EFI_SUCCESS The memory pool is freed.
551 IN UFS_PEIM_MEM_POOL
*Pool
555 Allocate some memory from the host controller's memory pool
556 which can be used to communicate with host controller.
558 @param Pool The host controller's memory pool.
559 @param Size Size of the memory to allocate.
561 @return The allocated memory or NULL.
566 IN UFS_PEIM_MEM_POOL
*Pool
,
571 Free the allocated memory back to the memory pool.
573 @param Pool The memory pool of the host controller.
574 @param Mem The memory to free.
575 @param Size The size of the memory to free.
580 IN UFS_PEIM_MEM_POOL
*Pool
,
594 Provides the controller-specific addresses required to access system memory from a
597 @param Operation Indicates if the bus master is going to read or write to system memory.
598 @param HostAddress The system memory address to map to the PCI controller.
599 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
601 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
602 access the hosts HostAddress.
603 @param Mapping A resulting value to pass to Unmap().
605 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
606 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
607 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
608 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
609 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
614 IN EDKII_IOMMU_OPERATION Operation
,
615 IN VOID
*HostAddress
,
616 IN OUT UINTN
*NumberOfBytes
,
617 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
622 Completes the Map() operation and releases any corresponding resources.
624 @param Mapping The mapping value returned from Map().
626 @retval EFI_SUCCESS The range was unmapped.
627 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
628 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
636 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
637 OperationBusMasterCommonBuffer64 mapping.
639 @param Pages The number of pages to allocate.
640 @param HostAddress A pointer to store the base system memory address of the
642 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
643 access the hosts HostAddress.
644 @param Mapping A resulting value to pass to Unmap().
646 @retval EFI_SUCCESS The requested memory pages were allocated.
647 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
648 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
649 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
650 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
654 IoMmuAllocateBuffer (
656 OUT VOID
**HostAddress
,
657 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
662 Frees memory that was allocated with AllocateBuffer().
664 @param Pages The number of pages to free.
665 @param HostAddress The base system memory address of the allocated range.
666 @param Mapping The mapping value returned from Map().
668 @retval EFI_SUCCESS The requested memory pages were freed.
669 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
670 was not allocated with AllocateBuffer().
676 IN VOID
*HostAddress
,
681 One notified function to cleanup the allocated DMA buffers at the end of PEI.
683 @param[in] PeiServices Pointer to PEI Services Table.
684 @param[in] NotifyDescriptor Pointer to the descriptor for the Notification
685 event that caused this function to execute.
686 @param[in] Ppi Pointer to the PPI data associated with this function.
688 @retval EFI_SUCCESS The function completes successfully
694 IN EFI_PEI_SERVICES
**PeiServices
,
695 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,