]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h
MdeModulePkg: Add UFS (Universal Flash Storage) Stack
[mirror_edk2.git] / MdeModulePkg / Bus / Ufs / UfsBlockIoPei / UfsBlockIoPei.h
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 #ifndef _UFS_BLOCK_IO_PEI_H_
15 #define _UFS_BLOCK_IO_PEI_H_
16
17 #include <PiPei.h>
18
19 #include <Ppi/UfsHostController.h>
20 #include <Ppi/BlockIo.h>
21
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>
29
30 #include <IndustryStandard/Scsi.h>
31
32 #include "UfsHci.h"
33 #include "UfsHcMem.h"
34
35 #define UFS_PEIM_HC_SIG SIGNATURE_32 ('U', 'F', 'S', 'H')
36
37 #define UFS_PEIM_MAX_LUNS 8
38
39 typedef struct {
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
42 UINT16 Rsvd:4;
43 } UFS_PEIM_EXPOSED_LUNS;
44
45 typedef struct {
46 ///
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.
53 ///
54 UINT64 Timeout;
55 ///
56 /// A pointer to the data buffer to transfer between the SCSI
57 /// controller and the SCSI device for read and bidirectional commands.
58 ///
59 VOID *InDataBuffer;
60 ///
61 /// A pointer to the data buffer to transfer between the SCSI
62 /// controller and the SCSI device for write or bidirectional commands.
63 ///
64 VOID *OutDataBuffer;
65 ///
66 /// A pointer to the sense data that was generated by the execution of
67 /// the SCSI Request Packet.
68 ///
69 VOID *SenseData;
70 ///
71 /// A pointer to buffer that contains the Command Data Block to
72 /// send to the SCSI device specified by Target and Lun.
73 ///
74 VOID *Cdb;
75 ///
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.
78 ///
79 UINT32 InTransferLength;
80 ///
81 /// On Input, the size, in bytes of OutDataBuffer. On Output, the
82 /// Number of bytes transferred between SCSI Controller and the SCSI device.
83 ///
84 UINT32 OutTransferLength;
85 ///
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.
88 ///
89 UINT8 CdbLength;
90 ///
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.
93 ///
94 UINT8 DataDirection;
95 ///
96 /// On input, the length in bytes of the SenseData buffer. On
97 /// output, the number of bytes written to the SenseData buffer.
98 ///
99 UINT8 SenseDataLength;
100 } UFS_SCSI_REQUEST_PACKET;
101
102 typedef struct _UFS_PEIM_HC_PRIVATE_DATA {
103 UINT32 Signature;
104 EFI_HANDLE Controller;
105
106 UFS_PEIM_MEM_POOL *Pool;
107
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];
111
112 UINTN UfsHcBase;
113 UINT32 Capabilities;
114
115 UINT8 TaskTag;
116
117 VOID *UtpTrlBase;
118 UINT8 Nutrs;
119 VOID *UtpTmrlBase;
120 UINT8 Nutmrs;
121
122 UFS_PEIM_EXPOSED_LUNS Luns;
123 } UFS_PEIM_HC_PRIVATE_DATA;
124
125 #define UFS_TIMEOUT MultU64x32((UINT64)(3), 10000000)
126
127 #define ROUNDUP8(x) (((x) % 8 == 0) ? (x) : ((x) / 8 + 1) * 8)
128
129 #define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) == 0)
130
131 #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVATE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG)
132
133 #define UFS_SCSI_OP_LENGTH_SIX 0x6
134 #define UFS_SCSI_OP_LENGTH_TEN 0xa
135 #define UFS_SCSI_OP_LENGTH_SIXTEEN 0x10
136
137 typedef struct _UFS_DEVICE_MANAGEMENT_REQUEST_PACKET {
138 UINT64 Timeout;
139 VOID *InDataBuffer;
140 VOID *OutDataBuffer;
141 UINT8 Opcode;
142 UINT8 DescId;
143 UINT8 Index;
144 UINT8 Selector;
145 UINT32 InTransferLength;
146 UINT32 OutTransferLength;
147 UINT8 DataDirection;
148 UINT8 Ocs;
149 } UFS_DEVICE_MANAGEMENT_REQUEST_PACKET;
150
151 /**
152 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
153
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
157 UFS device.
158
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
163 OutDataBuffer.
164 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
165 Packet.
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.
168
169 **/
170 EFI_STATUS
171 UfsExecScsiCmds (
172 IN UFS_PEIM_HC_PRIVATE_DATA *Private,
173 IN UINT8 Lun,
174 IN OUT UFS_SCSI_REQUEST_PACKET *Packet
175 );
176
177 /**
178 Initialize the UFS host controller.
179
180 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
181
182 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
183 @retval Others A device error occurred while initializing the controller.
184
185 **/
186 EFI_STATUS
187 UfsControllerInit (
188 IN UFS_PEIM_HC_PRIVATE_DATA *Private
189 );
190
191 /**
192 Stop the UFS host controller.
193
194 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
195
196 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
197 @retval Others A device error occurred while stopping the controller.
198
199 **/
200 EFI_STATUS
201 UfsControllerStop (
202 IN UFS_PEIM_HC_PRIVATE_DATA *Private
203 );
204
205 /**
206 Set specified flag to 1 on a UFS device.
207
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.
210
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.
214
215 **/
216 EFI_STATUS
217 UfsSetFlag (
218 IN UFS_PEIM_HC_PRIVATE_DATA *Private,
219 IN UINT8 FlagId
220 );
221
222 /**
223 Read or write specified device descriptor of a UFS device.
224
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.
232
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.
236
237 **/
238 EFI_STATUS
239 UfsRwDeviceDesc (
240 IN UFS_PEIM_HC_PRIVATE_DATA *Private,
241 IN BOOLEAN Read,
242 IN UINT8 DescId,
243 IN UINT8 Index,
244 IN UINT8 Selector,
245 IN OUT VOID *Descriptor,
246 IN UINT32 DescSize
247 );
248
249 /**
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.
252
253 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
254
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.
260
261 **/
262 EFI_STATUS
263 UfsExecNopCmds (
264 IN UFS_PEIM_HC_PRIVATE_DATA *Private
265 );
266
267 /**
268 Gets the count of block I/O devices that one specific block driver detects.
269
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.
276
277 @param[in] PeiServices General-purpose services that are available
278 to every PEIM.
279 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
280 instance.
281 @param[out] NumberBlockDevices The number of block I/O devices discovered.
282
283 @retval EFI_SUCCESS The operation performed successfully.
284
285 **/
286 EFI_STATUS
287 EFIAPI
288 UfsBlockIoPeimGetDeviceNo (
289 IN EFI_PEI_SERVICES **PeiServices,
290 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
291 OUT UINTN *NumberBlockDevices
292 );
293
294 /**
295 Gets a block device's media information.
296
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.
300
301 @param[in] PeiServices General-purpose services that are available to every
302 PEIM
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
310 NumberBlockDevices.
311 @param[out] MediaInfo The media information of the specified block media.
312 The caller is responsible for the ownership of this
313 data structure.
314
315 @par Note:
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.
328
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
332 error.
333
334 **/
335 EFI_STATUS
336 EFIAPI
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
342 );
343
344 /**
345 Reads the requested number of blocks from the specified block device.
346
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.
350
351 @param[in] PeiServices General-purpose services that are available to
352 every PEIM.
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
361 on the device
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
366 buffer.
367
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.
376
377 **/
378 EFI_STATUS
379 EFIAPI
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,
385 IN UINTN BufferSize,
386 OUT VOID *Buffer
387 );
388
389 /**
390 Initialize the memory management pool for the host controller.
391
392 @param Private The Ufs Peim driver private data.
393
394 @retval EFI_SUCCESS The memory pool is initialized.
395 @retval Others Fail to init the memory pool.
396
397 **/
398 EFI_STATUS
399 UfsPeimInitMemPool (
400 IN UFS_PEIM_HC_PRIVATE_DATA *Private
401 );
402
403 /**
404 Allocate some memory from the host controller's memory pool
405 which can be used to communicate with host controller.
406
407 @param Pool The host controller's memory pool.
408 @param Size Size of the memory to allocate.
409
410 @return The allocated memory or NULL.
411
412 **/
413 VOID *
414 UfsPeimAllocateMem (
415 IN UFS_PEIM_MEM_POOL *Pool,
416 IN UINTN Size
417 );
418
419 /**
420 Free the allocated memory back to the memory pool.
421
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.
425
426 **/
427 VOID
428 UfsPeimFreeMem (
429 IN UFS_PEIM_MEM_POOL *Pool,
430 IN VOID *Mem,
431 IN UINTN Size
432 );
433
434 #endif