]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h
MdeModulePkg: Add UFS (Universal Flash Storage) Stack
[mirror_edk2.git] / MdeModulePkg / Bus / Ufs / UfsBlockIoPei / UfsBlockIoPei.h
CommitLineData
0591696e
FT
1/** @file\r
2\r
3 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
4 This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12**/\r
13\r
14#ifndef _UFS_BLOCK_IO_PEI_H_\r
15#define _UFS_BLOCK_IO_PEI_H_\r
16\r
17#include <PiPei.h>\r
18\r
19#include <Ppi/UfsHostController.h>\r
20#include <Ppi/BlockIo.h>\r
21\r
22#include <Library/DebugLib.h>\r
23#include <Library/BaseLib.h>\r
24#include <Library/BaseMemoryLib.h>\r
25#include <Library/MemoryAllocationLib.h>\r
26#include <Library/IoLib.h>\r
27#include <Library/TimerLib.h>\r
28#include <Library/PeiServicesLib.h>\r
29\r
30#include <IndustryStandard/Scsi.h>\r
31\r
32#include "UfsHci.h"\r
33#include "UfsHcMem.h"\r
34\r
35#define UFS_PEIM_HC_SIG SIGNATURE_32 ('U', 'F', 'S', 'H')\r
36\r
37#define UFS_PEIM_MAX_LUNS 8\r
38\r
39typedef struct {\r
40 UINT8 Lun[UFS_PEIM_MAX_LUNS];\r
41 UINT16 BitMask:12; // Bit 0~7 is for common luns. Bit 8~11 is reserved for those well known luns\r
42 UINT16 Rsvd:4;\r
43} UFS_PEIM_EXPOSED_LUNS;\r
44\r
45typedef struct {\r
46 ///\r
47 /// The timeout, in 100 ns units, to use for the execution of this SCSI\r
48 /// Request Packet. A Timeout value of 0 means that this function\r
49 /// will wait indefinitely for the SCSI Request Packet to execute. If\r
50 /// Timeout is greater than zero, then this function will return\r
51 /// EFI_TIMEOUT if the time required to execute the SCSI\r
52 /// Request Packet is greater than Timeout.\r
53 ///\r
54 UINT64 Timeout;\r
55 ///\r
56 /// A pointer to the data buffer to transfer between the SCSI\r
57 /// controller and the SCSI device for read and bidirectional commands.\r
58 ///\r
59 VOID *InDataBuffer;\r
60 ///\r
61 /// A pointer to the data buffer to transfer between the SCSI\r
62 /// controller and the SCSI device for write or bidirectional commands.\r
63 ///\r
64 VOID *OutDataBuffer;\r
65 ///\r
66 /// A pointer to the sense data that was generated by the execution of\r
67 /// the SCSI Request Packet.\r
68 ///\r
69 VOID *SenseData;\r
70 ///\r
71 /// A pointer to buffer that contains the Command Data Block to\r
72 /// send to the SCSI device specified by Target and Lun.\r
73 ///\r
74 VOID *Cdb;\r
75 ///\r
76 /// On Input, the size, in bytes, of InDataBuffer. On output, the\r
77 /// number of bytes transferred between the SCSI controller and the SCSI device.\r
78 ///\r
79 UINT32 InTransferLength;\r
80 ///\r
81 /// On Input, the size, in bytes of OutDataBuffer. On Output, the\r
82 /// Number of bytes transferred between SCSI Controller and the SCSI device.\r
83 ///\r
84 UINT32 OutTransferLength;\r
85 ///\r
86 /// The length, in bytes, of the buffer Cdb. The standard values are 6,\r
87 /// 10, 12, and 16, but other values are possible if a variable length CDB is used.\r
88 ///\r
89 UINT8 CdbLength;\r
90 ///\r
91 /// The direction of the data transfer. 0 for reads, 1 for writes. A\r
92 /// value of 2 is Reserved for Bi-Directional SCSI commands.\r
93 ///\r
94 UINT8 DataDirection;\r
95 ///\r
96 /// On input, the length in bytes of the SenseData buffer. On\r
97 /// output, the number of bytes written to the SenseData buffer.\r
98 ///\r
99 UINT8 SenseDataLength;\r
100} UFS_SCSI_REQUEST_PACKET;\r
101\r
102typedef struct _UFS_PEIM_HC_PRIVATE_DATA { \r
103 UINT32 Signature;\r
104 EFI_HANDLE Controller;\r
105\r
106 UFS_PEIM_MEM_POOL *Pool;\r
107\r
108 EFI_PEI_RECOVERY_BLOCK_IO_PPI BlkIoPpi;\r
109 EFI_PEI_PPI_DESCRIPTOR BlkIoPpiList;\r
110 EFI_PEI_BLOCK_IO_MEDIA Media[UFS_PEIM_MAX_LUNS];\r
111\r
112 UINTN UfsHcBase;\r
113 UINT32 Capabilities;\r
114\r
115 UINT8 TaskTag;\r
116\r
117 VOID *UtpTrlBase;\r
118 UINT8 Nutrs;\r
119 VOID *UtpTmrlBase;\r
120 UINT8 Nutmrs;\r
121\r
122 UFS_PEIM_EXPOSED_LUNS Luns;\r
123} UFS_PEIM_HC_PRIVATE_DATA;\r
124\r
125#define UFS_TIMEOUT MultU64x32((UINT64)(3), 10000000)\r
126\r
127#define ROUNDUP8(x) (((x) % 8 == 0) ? (x) : ((x) / 8 + 1) * 8)\r
128\r
129#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) == 0)\r
130\r
131#define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG)\r
132\r
133#define UFS_SCSI_OP_LENGTH_SIX 0x6\r
134#define UFS_SCSI_OP_LENGTH_TEN 0xa\r
135#define UFS_SCSI_OP_LENGTH_SIXTEEN 0x10\r
136\r
137typedef struct _UFS_DEVICE_MANAGEMENT_REQUEST_PACKET {\r
138 UINT64 Timeout;\r
139 VOID *InDataBuffer;\r
140 VOID *OutDataBuffer;\r
141 UINT8 Opcode;\r
142 UINT8 DescId;\r
143 UINT8 Index;\r
144 UINT8 Selector;\r
145 UINT32 InTransferLength;\r
146 UINT32 OutTransferLength;\r
147 UINT8 DataDirection;\r
148 UINT8 Ocs;\r
149} UFS_DEVICE_MANAGEMENT_REQUEST_PACKET;\r
150\r
151/**\r
152 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.\r
153\r
154 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
155 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.\r
156 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the\r
157 UFS device.\r
158\r
159 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional\r
160 commands, InTransferLength bytes were transferred from\r
161 InDataBuffer. For write and bi-directional commands,\r
162 OutTransferLength bytes were transferred by\r
163 OutDataBuffer.\r
164 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request\r
165 Packet.\r
166 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.\r
167 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.\r
168\r
169**/\r
170EFI_STATUS\r
171UfsExecScsiCmds (\r
172 IN UFS_PEIM_HC_PRIVATE_DATA *Private,\r
173 IN UINT8 Lun,\r
174 IN OUT UFS_SCSI_REQUEST_PACKET *Packet\r
175 );\r
176\r
177/**\r
178 Initialize the UFS host controller.\r
179\r
180 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
181\r
182 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.\r
183 @retval Others A device error occurred while initializing the controller.\r
184\r
185**/\r
186EFI_STATUS\r
187UfsControllerInit (\r
188 IN UFS_PEIM_HC_PRIVATE_DATA *Private\r
189 );\r
190\r
191/**\r
192 Stop the UFS host controller.\r
193\r
194 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
195\r
196 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.\r
197 @retval Others A device error occurred while stopping the controller.\r
198\r
199**/\r
200EFI_STATUS\r
201UfsControllerStop (\r
202 IN UFS_PEIM_HC_PRIVATE_DATA *Private\r
203 );\r
204\r
205/**\r
206 Set specified flag to 1 on a UFS device.\r
207\r
208 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
209 @param[in] FlagId The ID of flag to be set.\r
210\r
211 @retval EFI_SUCCESS The flag was set successfully.\r
212 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.\r
213 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.\r
214\r
215**/\r
216EFI_STATUS\r
217UfsSetFlag (\r
218 IN UFS_PEIM_HC_PRIVATE_DATA *Private,\r
219 IN UINT8 FlagId\r
220 );\r
221\r
222/**\r
223 Read or write specified device descriptor of a UFS device.\r
224\r
225 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
226 @param[in] Read The boolean variable to show r/w direction.\r
227 @param[in] DescId The ID of device descriptor.\r
228 @param[in] Index The Index of device descriptor.\r
229 @param[in] Selector The Selector of device descriptor.\r
230 @param[in, out] Descriptor The buffer of device descriptor to be read or written.\r
231 @param[in] DescSize The size of device descriptor buffer.\r
232\r
233 @retval EFI_SUCCESS The device descriptor was read/written successfully.\r
234 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.\r
235 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.\r
236\r
237**/\r
238EFI_STATUS\r
239UfsRwDeviceDesc (\r
240 IN UFS_PEIM_HC_PRIVATE_DATA *Private,\r
241 IN BOOLEAN Read,\r
242 IN UINT8 DescId,\r
243 IN UINT8 Index,\r
244 IN UINT8 Selector,\r
245 IN OUT VOID *Descriptor,\r
246 IN UINT32 DescSize\r
247 );\r
248\r
249/**\r
250 Sends NOP IN cmd to a UFS device for initialization process request.\r
251 For more details, please refer to UFS 2.0 spec Figure 13.3.\r
252\r
253 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.\r
254\r
255 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was\r
256 received successfully.\r
257 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.\r
258 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.\r
259 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.\r
260\r
261**/\r
262EFI_STATUS\r
263UfsExecNopCmds (\r
264 IN UFS_PEIM_HC_PRIVATE_DATA *Private\r
265 );\r
266\r
267/**\r
268 Gets the count of block I/O devices that one specific block driver detects.\r
269\r
270 This function is used for getting the count of block I/O devices that one \r
271 specific block driver detects. To the PEI ATAPI driver, it returns the number\r
272 of all the detected ATAPI devices it detects during the enumeration process. \r
273 To the PEI legacy floppy driver, it returns the number of all the legacy \r
274 devices it finds during its enumeration process. If no device is detected, \r
275 then the function will return zero. \r
276 \r
277 @param[in] PeiServices General-purpose services that are available \r
278 to every PEIM.\r
279 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI \r
280 instance.\r
281 @param[out] NumberBlockDevices The number of block I/O devices discovered.\r
282\r
283 @retval EFI_SUCCESS The operation performed successfully.\r
284\r
285**/\r
286EFI_STATUS\r
287EFIAPI\r
288UfsBlockIoPeimGetDeviceNo (\r
289 IN EFI_PEI_SERVICES **PeiServices,\r
290 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
291 OUT UINTN *NumberBlockDevices\r
292 );\r
293\r
294/**\r
295 Gets a block device's media information.\r
296\r
297 This function will provide the caller with the specified block device's media \r
298 information. If the media changes, calling this function will update the media \r
299 information accordingly.\r
300\r
301 @param[in] PeiServices General-purpose services that are available to every\r
302 PEIM\r
303 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
304 @param[in] DeviceIndex Specifies the block device to which the function wants \r
305 to talk. Because the driver that implements Block I/O \r
306 PPIs will manage multiple block devices, the PPIs that \r
307 want to talk to a single device must specify the \r
308 device index that was assigned during the enumeration\r
309 process. This index is a number from one to \r
310 NumberBlockDevices.\r
311 @param[out] MediaInfo The media information of the specified block media. \r
312 The caller is responsible for the ownership of this \r
313 data structure.\r
314\r
315 @par Note: \r
316 The MediaInfo structure describes an enumeration of possible block device \r
317 types. This enumeration exists because no device paths are actually passed \r
318 across interfaces that describe the type or class of hardware that is publishing \r
319 the block I/O interface. This enumeration will allow for policy decisions\r
320 in the Recovery PEIM, such as "Try to recover from legacy floppy first, \r
321 LS-120 second, CD-ROM third." If there are multiple partitions abstracted \r
322 by a given device type, they should be reported in ascending order; this \r
323 order also applies to nested partitions, such as legacy MBR, where the \r
324 outermost partitions would have precedence in the reporting order. The \r
325 same logic applies to systems such as IDE that have precedence relationships \r
326 like "Master/Slave" or "Primary/Secondary". The master device should be \r
327 reported first, the slave second.\r
328 \r
329 @retval EFI_SUCCESS Media information about the specified block device \r
330 was obtained successfully.\r
331 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware \r
332 error.\r
333\r
334**/\r
335EFI_STATUS\r
336EFIAPI\r
337UfsBlockIoPeimGetMediaInfo (\r
338 IN EFI_PEI_SERVICES **PeiServices,\r
339 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
340 IN UINTN DeviceIndex,\r
341 OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo\r
342 );\r
343\r
344/**\r
345 Reads the requested number of blocks from the specified block device.\r
346\r
347 The function reads the requested number of blocks from the device. All the \r
348 blocks are read, or an error is returned. If there is no media in the device,\r
349 the function returns EFI_NO_MEDIA.\r
350\r
351 @param[in] PeiServices General-purpose services that are available to \r
352 every PEIM.\r
353 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.\r
354 @param[in] DeviceIndex Specifies the block device to which the function wants \r
355 to talk. Because the driver that implements Block I/O \r
356 PPIs will manage multiple block devices, PPIs that \r
357 want to talk to a single device must specify the device \r
358 index that was assigned during the enumeration process. \r
359 This index is a number from one to NumberBlockDevices.\r
360 @param[in] StartLBA The starting logical block address (LBA) to read from\r
361 on the device\r
362 @param[in] BufferSize The size of the Buffer in bytes. This number must be\r
363 a multiple of the intrinsic block size of the device.\r
364 @param[out] Buffer A pointer to the destination buffer for the data.\r
365 The caller is responsible for the ownership of the \r
366 buffer.\r
367 \r
368 @retval EFI_SUCCESS The data was read correctly from the device.\r
369 @retval EFI_DEVICE_ERROR The device reported an error while attempting \r
370 to perform the read operation.\r
371 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not \r
372 valid, or the buffer is not properly aligned.\r
373 @retval EFI_NO_MEDIA There is no media in the device.\r
374 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of\r
375 the intrinsic block size of the device.\r
376\r
377**/\r
378EFI_STATUS\r
379EFIAPI\r
380UfsBlockIoPeimReadBlocks (\r
381 IN EFI_PEI_SERVICES **PeiServices,\r
382 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,\r
383 IN UINTN DeviceIndex,\r
384 IN EFI_PEI_LBA StartLBA,\r
385 IN UINTN BufferSize,\r
386 OUT VOID *Buffer\r
387 );\r
388\r
389/**\r
390 Initialize the memory management pool for the host controller.\r
391 \r
392 @param Private The Ufs Peim driver private data.\r
393\r
394 @retval EFI_SUCCESS The memory pool is initialized.\r
395 @retval Others Fail to init the memory pool.\r
396\r
397**/\r
398EFI_STATUS\r
399UfsPeimInitMemPool (\r
400 IN UFS_PEIM_HC_PRIVATE_DATA *Private\r
401 );\r
402\r
403/**\r
404 Allocate some memory from the host controller's memory pool\r
405 which can be used to communicate with host controller.\r
406 \r
407 @param Pool The host controller's memory pool.\r
408 @param Size Size of the memory to allocate.\r
409\r
410 @return The allocated memory or NULL.\r
411\r
412**/\r
413VOID *\r
414UfsPeimAllocateMem (\r
415 IN UFS_PEIM_MEM_POOL *Pool,\r
416 IN UINTN Size\r
417 );\r
418\r
419/**\r
420 Free the allocated memory back to the memory pool.\r
421\r
422 @param Pool The memory pool of the host controller.\r
423 @param Mem The memory to free.\r
424 @param Size The size of the memory to free.\r
425\r
426**/\r
427VOID\r
428UfsPeimFreeMem (\r
429 IN UFS_PEIM_MEM_POOL *Pool,\r
430 IN VOID *Mem,\r
431 IN UINTN Size\r
432 );\r
433\r
434#endif\r