• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @file
2 
3   Copyright (c) 2015, 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 _SD_BLOCK_IO_PEI_H_
15 #define _SD_BLOCK_IO_PEI_H_
16 
17 #include <PiPei.h>
18 
19 #include <Ppi/SdMmcHostController.h>
20 #include <Ppi/BlockIo.h>
21 #include <Ppi/BlockIo2.h>
22 
23 #include <Library/DebugLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/IoLib.h>
28 #include <Library/TimerLib.h>
29 #include <Library/PeiServicesLib.h>
30 
31 #include <IndustryStandard/Sd.h>
32 
33 typedef struct _SD_PEIM_HC_PRIVATE_DATA SD_PEIM_HC_PRIVATE_DATA;
34 typedef struct _SD_PEIM_HC_SLOT         SD_PEIM_HC_SLOT;
35 typedef struct _SD_TRB                  SD_TRB;
36 
37 #include "SdHci.h"
38 #include "SdHcMem.h"
39 
40 #define SD_PEIM_SIG               SIGNATURE_32 ('S', 'D', 'C', 'P')
41 #define SD_PEIM_SLOT_SIG          SIGNATURE_32 ('S', 'D', 'C', 'S')
42 
43 #define SD_PEIM_MAX_SLOTS         6
44 
45 struct _SD_PEIM_HC_SLOT {
46   UINT32                            Signature;
47   EFI_PEI_BLOCK_IO2_MEDIA           Media;
48 
49   UINTN                             SdHcBase;
50   SD_HC_SLOT_CAP                    Capability;
51   SD_CSD                            Csd;
52   BOOLEAN                           SectorAddressing;
53   SD_PEIM_HC_PRIVATE_DATA           *Private;
54 };
55 
56 struct _SD_PEIM_HC_PRIVATE_DATA {
57   UINT32                            Signature;
58   SD_PEIM_MEM_POOL                  *Pool;
59   EFI_PEI_RECOVERY_BLOCK_IO_PPI     BlkIoPpi;
60   EFI_PEI_RECOVERY_BLOCK_IO2_PPI    BlkIo2Ppi;
61   EFI_PEI_PPI_DESCRIPTOR            BlkIoPpiList;
62   EFI_PEI_PPI_DESCRIPTOR            BlkIo2PpiList;
63   SD_PEIM_HC_SLOT                   Slot[SD_PEIM_MAX_SLOTS];
64   UINT8                             SlotNum;
65   UINT8                             TotalBlkIoDevices;
66 };
67 
68 #define SD_TIMEOUT                  MultU64x32((UINT64)(3), 1000000)
69 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIoPpi, SD_PEIM_SIG)
70 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIo2Ppi, SD_PEIM_SIG)
71 
72 struct _SD_TRB {
73   SD_PEIM_HC_SLOT                   *Slot;
74   UINT16                            BlockSize;
75 
76   SD_COMMAND_PACKET                 *Packet;
77   VOID                              *Data;
78   UINT32                            DataLen;
79   BOOLEAN                           Read;
80   SD_HC_TRANSFER_MODE               Mode;
81 
82   UINT64                            Timeout;
83 
84   SD_HC_ADMA_DESC_LINE              *AdmaDesc;
85   UINTN                             AdmaDescSize;
86 };
87 
88 /**
89   Gets the count of block I/O devices that one specific block driver detects.
90 
91   This function is used for getting the count of block I/O devices that one
92   specific block driver detects.  To the PEI ATAPI driver, it returns the number
93   of all the detected ATAPI devices it detects during the enumeration process.
94   To the PEI legacy floppy driver, it returns the number of all the legacy
95   devices it finds during its enumeration process. If no device is detected,
96   then the function will return zero.
97 
98   @param[in]  PeiServices          General-purpose services that are available
99                                    to every PEIM.
100   @param[in]  This                 Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
101                                    instance.
102   @param[out] NumberBlockDevices   The number of block I/O devices discovered.
103 
104   @retval     EFI_SUCCESS          The operation performed successfully.
105 
106 **/
107 EFI_STATUS
108 EFIAPI
109 SdBlockIoPeimGetDeviceNo (
110   IN  EFI_PEI_SERVICES               **PeiServices,
111   IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI  *This,
112   OUT UINTN                          *NumberBlockDevices
113   );
114 
115 /**
116   Gets a block device's media information.
117 
118   This function will provide the caller with the specified block device's media
119   information. If the media changes, calling this function will update the media
120   information accordingly.
121 
122   @param[in]  PeiServices   General-purpose services that are available to every
123                             PEIM
124   @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
125   @param[in]  DeviceIndex   Specifies the block device to which the function wants
126                             to talk. Because the driver that implements Block I/O
127                             PPIs will manage multiple block devices, the PPIs that
128                             want to talk to a single device must specify the
129                             device index that was assigned during the enumeration
130                             process. This index is a number from one to
131                             NumberBlockDevices.
132   @param[out] MediaInfo     The media information of the specified block media.
133                             The caller is responsible for the ownership of this
134                             data structure.
135 
136   @par Note:
137       The MediaInfo structure describes an enumeration of possible block device
138       types.  This enumeration exists because no device paths are actually passed
139       across interfaces that describe the type or class of hardware that is publishing
140       the block I/O interface. This enumeration will allow for policy decisions
141       in the Recovery PEIM, such as "Try to recover from legacy floppy first,
142       LS-120 second, CD-ROM third." If there are multiple partitions abstracted
143       by a given device type, they should be reported in ascending order; this
144       order also applies to nested partitions, such as legacy MBR, where the
145       outermost partitions would have precedence in the reporting order. The
146       same logic applies to systems such as IDE that have precedence relationships
147       like "Master/Slave" or "Primary/Secondary". The master device should be
148       reported first, the slave second.
149 
150   @retval EFI_SUCCESS        Media information about the specified block device
151                              was obtained successfully.
152   @retval EFI_DEVICE_ERROR   Cannot get the media information due to a hardware
153                              error.
154 
155 **/
156 EFI_STATUS
157 EFIAPI
158 SdBlockIoPeimGetMediaInfo (
159   IN  EFI_PEI_SERVICES               **PeiServices,
160   IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI  *This,
161   IN  UINTN                          DeviceIndex,
162   OUT EFI_PEI_BLOCK_IO_MEDIA         *MediaInfo
163   );
164 
165 /**
166   Reads the requested number of blocks from the specified block device.
167 
168   The function reads the requested number of blocks from the device. All the
169   blocks are read, or an error is returned. If there is no media in the device,
170   the function returns EFI_NO_MEDIA.
171 
172   @param[in]  PeiServices   General-purpose services that are available to
173                             every PEIM.
174   @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
175   @param[in]  DeviceIndex   Specifies the block device to which the function wants
176                             to talk. Because the driver that implements Block I/O
177                             PPIs will manage multiple block devices, PPIs that
178                             want to talk to a single device must specify the device
179                             index that was assigned during the enumeration process.
180                             This index is a number from one to NumberBlockDevices.
181   @param[in]  StartLBA      The starting logical block address (LBA) to read from
182                             on the device
183   @param[in]  BufferSize    The size of the Buffer in bytes. This number must be
184                             a multiple of the intrinsic block size of the device.
185   @param[out] Buffer        A pointer to the destination buffer for the data.
186                             The caller is responsible for the ownership of the
187                             buffer.
188 
189   @retval EFI_SUCCESS             The data was read correctly from the device.
190   @retval EFI_DEVICE_ERROR        The device reported an error while attempting
191                                   to perform the read operation.
192   @retval EFI_INVALID_PARAMETER   The read request contains LBAs that are not
193                                   valid, or the buffer is not properly aligned.
194   @retval EFI_NO_MEDIA            There is no media in the device.
195   @retval EFI_BAD_BUFFER_SIZE     The BufferSize parameter is not a multiple of
196                                   the intrinsic block size of the device.
197 
198 **/
199 EFI_STATUS
200 EFIAPI
201 SdBlockIoPeimReadBlocks (
202   IN  EFI_PEI_SERVICES               **PeiServices,
203   IN  EFI_PEI_RECOVERY_BLOCK_IO_PPI  *This,
204   IN  UINTN                          DeviceIndex,
205   IN  EFI_PEI_LBA                    StartLBA,
206   IN  UINTN                          BufferSize,
207   OUT VOID                           *Buffer
208   );
209 
210 /**
211   Gets the count of block I/O devices that one specific block driver detects.
212 
213   This function is used for getting the count of block I/O devices that one
214   specific block driver detects.  To the PEI ATAPI driver, it returns the number
215   of all the detected ATAPI devices it detects during the enumeration process.
216   To the PEI legacy floppy driver, it returns the number of all the legacy
217   devices it finds during its enumeration process. If no device is detected,
218   then the function will return zero.
219 
220   @param[in]  PeiServices          General-purpose services that are available
221                                    to every PEIM.
222   @param[in]  This                 Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
223                                    instance.
224   @param[out] NumberBlockDevices   The number of block I/O devices discovered.
225 
226   @retval     EFI_SUCCESS          The operation performed successfully.
227 
228 **/
229 EFI_STATUS
230 EFIAPI
231 SdBlockIoPeimGetDeviceNo2 (
232   IN  EFI_PEI_SERVICES               **PeiServices,
233   IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
234   OUT UINTN                          *NumberBlockDevices
235   );
236 
237 /**
238   Gets a block device's media information.
239 
240   This function will provide the caller with the specified block device's media
241   information. If the media changes, calling this function will update the media
242   information accordingly.
243 
244   @param[in]  PeiServices   General-purpose services that are available to every
245                             PEIM
246   @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
247   @param[in]  DeviceIndex   Specifies the block device to which the function wants
248                             to talk. Because the driver that implements Block I/O
249                             PPIs will manage multiple block devices, the PPIs that
250                             want to talk to a single device must specify the
251                             device index that was assigned during the enumeration
252                             process. This index is a number from one to
253                             NumberBlockDevices.
254   @param[out] MediaInfo     The media information of the specified block media.
255                             The caller is responsible for the ownership of this
256                             data structure.
257 
258   @par Note:
259       The MediaInfo structure describes an enumeration of possible block device
260       types.  This enumeration exists because no device paths are actually passed
261       across interfaces that describe the type or class of hardware that is publishing
262       the block I/O interface. This enumeration will allow for policy decisions
263       in the Recovery PEIM, such as "Try to recover from legacy floppy first,
264       LS-120 second, CD-ROM third." If there are multiple partitions abstracted
265       by a given device type, they should be reported in ascending order; this
266       order also applies to nested partitions, such as legacy MBR, where the
267       outermost partitions would have precedence in the reporting order. The
268       same logic applies to systems such as IDE that have precedence relationships
269       like "Master/Slave" or "Primary/Secondary". The master device should be
270       reported first, the slave second.
271 
272   @retval EFI_SUCCESS        Media information about the specified block device
273                              was obtained successfully.
274   @retval EFI_DEVICE_ERROR   Cannot get the media information due to a hardware
275                              error.
276 
277 **/
278 EFI_STATUS
279 EFIAPI
280 SdBlockIoPeimGetMediaInfo2 (
281   IN  EFI_PEI_SERVICES               **PeiServices,
282   IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
283   IN  UINTN                          DeviceIndex,
284   OUT EFI_PEI_BLOCK_IO2_MEDIA        *MediaInfo
285   );
286 
287 /**
288   Reads the requested number of blocks from the specified block device.
289 
290   The function reads the requested number of blocks from the device. All the
291   blocks are read, or an error is returned. If there is no media in the device,
292   the function returns EFI_NO_MEDIA.
293 
294   @param[in]  PeiServices   General-purpose services that are available to
295                             every PEIM.
296   @param[in]  This          Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
297   @param[in]  DeviceIndex   Specifies the block device to which the function wants
298                             to talk. Because the driver that implements Block I/O
299                             PPIs will manage multiple block devices, PPIs that
300                             want to talk to a single device must specify the device
301                             index that was assigned during the enumeration process.
302                             This index is a number from one to NumberBlockDevices.
303   @param[in]  StartLBA      The starting logical block address (LBA) to read from
304                             on the device
305   @param[in]  BufferSize    The size of the Buffer in bytes. This number must be
306                             a multiple of the intrinsic block size of the device.
307   @param[out] Buffer        A pointer to the destination buffer for the data.
308                             The caller is responsible for the ownership of the
309                             buffer.
310 
311   @retval EFI_SUCCESS             The data was read correctly from the device.
312   @retval EFI_DEVICE_ERROR        The device reported an error while attempting
313                                   to perform the read operation.
314   @retval EFI_INVALID_PARAMETER   The read request contains LBAs that are not
315                                   valid, or the buffer is not properly aligned.
316   @retval EFI_NO_MEDIA            There is no media in the device.
317   @retval EFI_BAD_BUFFER_SIZE     The BufferSize parameter is not a multiple of
318                                   the intrinsic block size of the device.
319 
320 **/
321 EFI_STATUS
322 EFIAPI
323 SdBlockIoPeimReadBlocks2 (
324   IN  EFI_PEI_SERVICES               **PeiServices,
325   IN  EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
326   IN  UINTN                          DeviceIndex,
327   IN  EFI_PEI_LBA                    StartLBA,
328   IN  UINTN                          BufferSize,
329   OUT VOID                           *Buffer
330   );
331 
332 /**
333   Initialize the memory management pool for the host controller.
334 
335   @param  Private               The Sd Peim driver private data.
336 
337   @retval EFI_SUCCESS           The memory pool is initialized.
338   @retval Others                Fail to init the memory pool.
339 
340 **/
341 EFI_STATUS
342 SdPeimInitMemPool (
343   IN  SD_PEIM_HC_PRIVATE_DATA      *Private
344   );
345 
346 /**
347   Allocate some memory from the host controller's memory pool
348   which can be used to communicate with host controller.
349 
350   @param  Pool      The host controller's memory pool.
351   @param  Size      Size of the memory to allocate.
352 
353   @return The allocated memory or NULL.
354 
355 **/
356 VOID *
357 SdPeimAllocateMem (
358   IN  SD_PEIM_MEM_POOL        *Pool,
359   IN  UINTN                    Size
360   );
361 
362 /**
363   Free the allocated memory back to the memory pool.
364 
365   @param  Pool           The memory pool of the host controller.
366   @param  Mem            The memory to free.
367   @param  Size           The size of the memory to free.
368 
369 **/
370 VOID
371 SdPeimFreeMem (
372   IN SD_PEIM_MEM_POOL    *Pool,
373   IN VOID                 *Mem,
374   IN UINTN                Size
375   );
376 
377 #endif
378