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
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>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/IoLib.h>
27 #include <Library/TimerLib.h>
28 #include <Library/PeiServicesLib.h>
30 #include <IndustryStandard/Scsi.h>
35 #define UFS_PEIM_HC_SIG SIGNATURE_32 ('U', 'F', 'S', 'H')
37 #define UFS_PEIM_MAX_LUNS 8
40 UINT8 Lun
[UFS_PEIM_MAX_LUNS
];
41 UINT16 BitMask
:12; // Bit 0~7 is for common luns. Bit 8~11 is reserved for those well known luns
43 } UFS_PEIM_EXPOSED_LUNS
;
47 /// The timeout, in 100 ns units, to use for the execution of this SCSI
48 /// Request Packet. A Timeout value of 0 means that this function
49 /// will wait indefinitely for the SCSI Request Packet to execute. If
50 /// Timeout is greater than zero, then this function will return
51 /// EFI_TIMEOUT if the time required to execute the SCSI
52 /// Request Packet is greater than Timeout.
56 /// A pointer to the data buffer to transfer between the SCSI
57 /// controller and the SCSI device for read and bidirectional commands.
61 /// A pointer to the data buffer to transfer between the SCSI
62 /// controller and the SCSI device for write or bidirectional commands.
66 /// A pointer to the sense data that was generated by the execution of
67 /// the SCSI Request Packet.
71 /// A pointer to buffer that contains the Command Data Block to
72 /// send to the SCSI device specified by Target and Lun.
76 /// On Input, the size, in bytes, of InDataBuffer. On output, the
77 /// number of bytes transferred between the SCSI controller and the SCSI device.
79 UINT32 InTransferLength
;
81 /// On Input, the size, in bytes of OutDataBuffer. On Output, the
82 /// Number of bytes transferred between SCSI Controller and the SCSI device.
84 UINT32 OutTransferLength
;
86 /// The length, in bytes, of the buffer Cdb. The standard values are 6,
87 /// 10, 12, and 16, but other values are possible if a variable length CDB is used.
91 /// The direction of the data transfer. 0 for reads, 1 for writes. A
92 /// value of 2 is Reserved for Bi-Directional SCSI commands.
96 /// On input, the length in bytes of the SenseData buffer. On
97 /// output, the number of bytes written to the SenseData buffer.
99 UINT8 SenseDataLength
;
100 } UFS_SCSI_REQUEST_PACKET
;
102 typedef struct _UFS_PEIM_HC_PRIVATE_DATA
{
104 EFI_HANDLE Controller
;
106 UFS_PEIM_MEM_POOL
*Pool
;
108 EFI_PEI_RECOVERY_BLOCK_IO_PPI BlkIoPpi
;
109 EFI_PEI_PPI_DESCRIPTOR BlkIoPpiList
;
110 EFI_PEI_BLOCK_IO_MEDIA Media
[UFS_PEIM_MAX_LUNS
];
122 UFS_PEIM_EXPOSED_LUNS Luns
;
123 } UFS_PEIM_HC_PRIVATE_DATA
;
125 #define UFS_TIMEOUT MultU64x32((UINT64)(3), 10000000)
127 #define ROUNDUP8(x) (((x) % 8 == 0) ? (x) : ((x) / 8 + 1) * 8)
129 #define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) == 0)
131 #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG)
133 #define UFS_SCSI_OP_LENGTH_SIX 0x6
134 #define UFS_SCSI_OP_LENGTH_TEN 0xa
135 #define UFS_SCSI_OP_LENGTH_SIXTEEN 0x10
137 typedef struct _UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
{
145 UINT32 InTransferLength
;
146 UINT32 OutTransferLength
;
149 } UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
;
152 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
154 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
155 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.
156 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the
159 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
160 commands, InTransferLength bytes were transferred from
161 InDataBuffer. For write and bi-directional commands,
162 OutTransferLength bytes were transferred by
164 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
166 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
167 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
172 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
174 IN OUT UFS_SCSI_REQUEST_PACKET
*Packet
178 Initialize the UFS host controller.
180 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
182 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
183 @retval Others A device error occurred while initializing the controller.
188 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
192 Stop the UFS host controller.
194 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
196 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
197 @retval Others A device error occurred while stopping the controller.
202 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
206 Set specified flag to 1 on a UFS device.
208 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
209 @param[in] FlagId The ID of flag to be set.
211 @retval EFI_SUCCESS The flag was set successfully.
212 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.
213 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.
218 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
223 Read or write specified device descriptor of a UFS device.
225 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
226 @param[in] Read The boolean variable to show r/w direction.
227 @param[in] DescId The ID of device descriptor.
228 @param[in] Index The Index of device descriptor.
229 @param[in] Selector The Selector of device descriptor.
230 @param[in, out] Descriptor The buffer of device descriptor to be read or written.
231 @param[in] DescSize The size of device descriptor buffer.
233 @retval EFI_SUCCESS The device descriptor was read/written successfully.
234 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
235 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
240 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
245 IN OUT VOID
*Descriptor
,
250 Sends NOP IN cmd to a UFS device for initialization process request.
251 For more details, please refer to UFS 2.0 spec Figure 13.3.
253 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
255 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was
256 received successfully.
257 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.
258 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
259 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.
264 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
268 Gets the count of block I/O devices that one specific block driver detects.
270 This function is used for getting the count of block I/O devices that one
271 specific block driver detects. To the PEI ATAPI driver, it returns the number
272 of all the detected ATAPI devices it detects during the enumeration process.
273 To the PEI legacy floppy driver, it returns the number of all the legacy
274 devices it finds during its enumeration process. If no device is detected,
275 then the function will return zero.
277 @param[in] PeiServices General-purpose services that are available
279 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
281 @param[out] NumberBlockDevices The number of block I/O devices discovered.
283 @retval EFI_SUCCESS The operation performed successfully.
288 UfsBlockIoPeimGetDeviceNo (
289 IN EFI_PEI_SERVICES
**PeiServices
,
290 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
291 OUT UINTN
*NumberBlockDevices
295 Gets a block device's media information.
297 This function will provide the caller with the specified block device's media
298 information. If the media changes, calling this function will update the media
299 information accordingly.
301 @param[in] PeiServices General-purpose services that are available to every
303 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
304 @param[in] DeviceIndex Specifies the block device to which the function wants
305 to talk. Because the driver that implements Block I/O
306 PPIs will manage multiple block devices, the PPIs that
307 want to talk to a single device must specify the
308 device index that was assigned during the enumeration
309 process. This index is a number from one to
311 @param[out] MediaInfo The media information of the specified block media.
312 The caller is responsible for the ownership of this
316 The MediaInfo structure describes an enumeration of possible block device
317 types. This enumeration exists because no device paths are actually passed
318 across interfaces that describe the type or class of hardware that is publishing
319 the block I/O interface. This enumeration will allow for policy decisions
320 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
321 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
322 by a given device type, they should be reported in ascending order; this
323 order also applies to nested partitions, such as legacy MBR, where the
324 outermost partitions would have precedence in the reporting order. The
325 same logic applies to systems such as IDE that have precedence relationships
326 like "Master/Slave" or "Primary/Secondary". The master device should be
327 reported first, the slave second.
329 @retval EFI_SUCCESS Media information about the specified block device
330 was obtained successfully.
331 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
337 UfsBlockIoPeimGetMediaInfo (
338 IN EFI_PEI_SERVICES
**PeiServices
,
339 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
340 IN UINTN DeviceIndex
,
341 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
345 Reads the requested number of blocks from the specified block device.
347 The function reads the requested number of blocks from the device. All the
348 blocks are read, or an error is returned. If there is no media in the device,
349 the function returns EFI_NO_MEDIA.
351 @param[in] PeiServices General-purpose services that are available to
353 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
354 @param[in] DeviceIndex Specifies the block device to which the function wants
355 to talk. Because the driver that implements Block I/O
356 PPIs will manage multiple block devices, PPIs that
357 want to talk to a single device must specify the device
358 index that was assigned during the enumeration process.
359 This index is a number from one to NumberBlockDevices.
360 @param[in] StartLBA The starting logical block address (LBA) to read from
362 @param[in] BufferSize The size of the Buffer in bytes. This number must be
363 a multiple of the intrinsic block size of the device.
364 @param[out] Buffer A pointer to the destination buffer for the data.
365 The caller is responsible for the ownership of the
368 @retval EFI_SUCCESS The data was read correctly from the device.
369 @retval EFI_DEVICE_ERROR The device reported an error while attempting
370 to perform the read operation.
371 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
372 valid, or the buffer is not properly aligned.
373 @retval EFI_NO_MEDIA There is no media in the device.
374 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
375 the intrinsic block size of the device.
380 UfsBlockIoPeimReadBlocks (
381 IN EFI_PEI_SERVICES
**PeiServices
,
382 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
383 IN UINTN DeviceIndex
,
384 IN EFI_PEI_LBA StartLBA
,
390 Initialize the memory management pool for the host controller.
392 @param Private The Ufs Peim driver private data.
394 @retval EFI_SUCCESS The memory pool is initialized.
395 @retval Others Fail to init the memory pool.
400 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
404 Allocate some memory from the host controller's memory pool
405 which can be used to communicate with host controller.
407 @param Pool The host controller's memory pool.
408 @param Size Size of the memory to allocate.
410 @return The allocated memory or NULL.
415 IN UFS_PEIM_MEM_POOL
*Pool
,
420 Free the allocated memory back to the memory pool.
422 @param Pool The memory pool of the host controller.
423 @param Mem The memory to free.
424 @param Size The size of the memory to free.
429 IN UFS_PEIM_MEM_POOL
*Pool
,