2 The driver wrappers BlockMmio protocol instances to produce
3 Block I/O Protocol instances.
5 Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_DRIVER_BINDING_PROTOCOL gBlockIoDriverBinding
= {
19 BlockIoDriverBindingSupported
,
20 BlockIoDriverBindingStart
,
21 BlockIoDriverBindingStop
,
28 Reset the block device.
30 This function implements EFI_BLOCK_IO_PROTOCOL.Reset().
31 It resets the block device hardware.
32 ExtendedVerification is ignored in this implementation.
34 @param This Indicates a pointer to the calling context.
35 @param ExtendedVerification Indicates that the driver may perform a more exhaustive
36 verification operation of the device during reset.
38 @retval EFI_SUCCESS The block device was reset.
39 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.
45 IN EFI_BLOCK_IO_PROTOCOL
*This
,
46 IN BOOLEAN ExtendedVerification
53 Reads the requested number of blocks from the device.
55 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
56 It reads the requested number of blocks from the device.
57 All the blocks are read, or an error is returned.
59 @param This Indicates a pointer to the calling context.
60 @param ReadData If TRUE then read data. If FALSE then write data.
61 @param MediaId The media ID that the read request is for.
62 @param Lba The starting logical block address to read from on the device.
63 @param BufferSize The size of the Buffer in bytes.
64 This must be a multiple of the intrinsic block size of the device.
65 @param Buffer A pointer to the destination buffer for the data. The caller is
66 responsible for either having implicit or explicit ownership of the buffer.
68 @retval EFI_SUCCESS The data was read correctly from the device.
69 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.
70 @retval EFI_NO_MEDIA There is no media in the device.
71 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
72 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.
73 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
74 or the buffer is not on proper alignment.
80 IN EFI_BLOCK_IO_PROTOCOL
*This
,
89 BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*Private
;
91 EFI_BLOCK_IO_MEDIA
*Media
;
94 EFI_CPU_IO_PROTOCOL_IO_MEM CpuAccessFunction
;
97 // First, validate the parameters
99 if ((Buffer
== NULL
) || (BufferSize
== 0)) {
100 return EFI_INVALID_PARAMETER
;
104 // Get private data structure
106 Private
= PRIVATE_FROM_BLOCK_IO (This
);
107 Media
= Private
->BlockMmio
->Media
;
110 // BufferSize must be a multiple of the intrinsic block size of the device.
112 if (ModU64x32 (BufferSize
, Media
->BlockSize
) != 0) {
113 return EFI_BAD_BUFFER_SIZE
;
116 TotalBlock
= (UINTN
) DivU64x32 (BufferSize
, Media
->BlockSize
);
119 // Make sure the range to read is valid.
121 if (Lba
+ TotalBlock
- 1 > Media
->LastBlock
) {
122 return EFI_INVALID_PARAMETER
;
125 if (!(Media
->MediaPresent
)) {
129 if (MediaId
!= Media
->MediaId
) {
130 return EFI_MEDIA_CHANGED
;
133 Address
= Private
->BlockMmio
->BaseAddress
;
134 Address
+= MultU64x32 (Lba
, Media
->BlockSize
);
136 Count
= BufferSize
>> 3;
139 CpuAccessFunction
= Private
->CpuIo
->Mem
.Read
;
141 CpuAccessFunction
= Private
->CpuIo
->Mem
.Write
;
144 Status
= (CpuAccessFunction
) (
157 Reads the requested number of blocks from the device.
159 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
160 It reads the requested number of blocks from the device.
161 All the blocks are read, or an error is returned.
163 @param This Indicates a pointer to the calling context.
164 @param MediaId The media ID that the read request is for.
165 @param Lba The starting logical block address to read from on the device.
166 @param BufferSize The size of the Buffer in bytes.
167 This must be a multiple of the intrinsic block size of the device.
168 @param Buffer A pointer to the destination buffer for the data. The caller is
169 responsible for either having implicit or explicit ownership of the buffer.
171 @retval EFI_SUCCESS The data was read correctly from the device.
172 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.
173 @retval EFI_NO_MEDIA There is no media in the device.
174 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
175 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.
176 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
177 or the buffer is not on proper alignment.
183 IN EFI_BLOCK_IO_PROTOCOL
*This
,
190 DEBUG ((EFI_D_INFO
, "BlockIo (MMIO) ReadBlocks: lba=0x%Lx, size=0x%Lx\n",
191 Lba
, (UINT64
)BufferSize
));
192 return ReadOrWriteBlocks (
204 Writes a specified number of blocks to the device.
206 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
207 It writes a specified number of blocks to the device.
208 All blocks are written, or an error is returned.
210 @param This Indicates a pointer to the calling context.
211 @param MediaId The media ID that the write request is for.
212 @param Lba The starting logical block address to be written.
213 @param BufferSize The size of the Buffer in bytes.
214 This must be a multiple of the intrinsic block size of the device.
215 @param Buffer Pointer to the source buffer for the data.
217 @retval EFI_SUCCESS The data were written correctly to the device.
218 @retval EFI_WRITE_PROTECTED The device cannot be written to.
219 @retval EFI_NO_MEDIA There is no media in the device.
220 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
221 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
222 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
223 block size of the device.
224 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
225 or the buffer is not on proper alignment.
231 IN EFI_BLOCK_IO_PROTOCOL
*This
,
238 DEBUG ((EFI_D_INFO
, "BlockIo (MMIO) WriteBlocks: lba=0x%Lx, size=0x%Lx\n",
239 Lba
, (UINT64
)BufferSize
));
240 return ReadOrWriteBlocks (
251 Flushes all modified data to a physical block device.
253 @param This Indicates a pointer to the calling context.
255 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
256 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
257 @retval EFI_NO_MEDIA There is no media in the device.
263 IN EFI_BLOCK_IO_PROTOCOL
*This
271 Initialize data for device that does not support multiple LUNSs.
273 @param This The Driver Binding Protocol instance.
274 @param Controller The device to initialize.
275 @param BlockMmio Pointer to USB_MASS_TRANSPORT.
276 @param Context Parameter for USB_MASS_DEVICE.Context.
278 @retval EFI_SUCCESS Initialization succeeds.
279 @retval Other Initialization fails.
284 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
285 IN EFI_HANDLE Controller
289 BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*Private
;
290 BLOCK_MMIO_PROTOCOL
*BlockMmio
;
292 Private
= (BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*) AllocateZeroPool (sizeof (*Private
));
293 ASSERT (Private
!= NULL
);
295 Status
= gBS
->LocateProtocol (
296 &gEfiCpuIo2ProtocolGuid
,
298 (VOID
**) &(Private
->CpuIo
)
300 ASSERT_EFI_ERROR (Status
);
302 Status
= gBS
->OpenProtocol (
304 &gBlockMmioProtocolGuid
,
305 (VOID
**) &BlockMmio
,
306 This
->DriverBindingHandle
,
308 EFI_OPEN_PROTOCOL_BY_DRIVER
310 if (EFI_ERROR (Status
)) {
311 DEBUG ((EFI_D_ERROR
, "BlockIoInit: OpenBlockMmioProtocol By Driver (%r)\n", Status
));
314 DEBUG ((EFI_D_INFO
, "BlockMmio: %p\n", BlockMmio
));
315 DEBUG ((EFI_D_INFO
, "BlockMmio->Media->LastBlock: 0x%lx\n", BlockMmio
->Media
->LastBlock
));
317 Private
->Signature
= BLOCK_MMIO_TO_BLOCK_IO_SIGNATURE
;
318 Private
->Controller
= Controller
;
319 Private
->BlockMmio
= BlockMmio
;
320 Private
->BlockIo
.Media
= BlockMmio
->Media
;
321 Private
->BlockIo
.Reset
= BlockIoReset
;
322 Private
->BlockIo
.ReadBlocks
= BlockIoReadBlocks
;
323 Private
->BlockIo
.WriteBlocks
= BlockIoWriteBlocks
;
324 Private
->BlockIo
.FlushBlocks
= BlockIoFlushBlocks
;
326 DEBUG ((EFI_D_INFO
, "Private->BlockIo.Media->LastBlock: 0x%lx\n", Private
->BlockIo
.Media
->LastBlock
));
328 Status
= gBS
->InstallProtocolInterface (
330 &gEfiBlockIoProtocolGuid
,
331 EFI_NATIVE_INTERFACE
,
334 if (EFI_ERROR (Status
)) {
341 if (Private
!= NULL
) {
344 if (BlockMmio
!= NULL
) {
347 &gBlockMmioProtocolGuid
,
348 This
->DriverBindingHandle
,
357 Check whether the controller is a supported USB mass storage.
359 @param This The USB mass storage driver binding protocol.
360 @param Controller The controller handle to check.
361 @param RemainingDevicePath The remaining device path.
363 @retval EFI_SUCCESS The driver supports this controller.
364 @retval other This device isn't supported.
369 BlockIoDriverBindingSupported (
370 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
371 IN EFI_HANDLE Controller
,
372 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
376 BLOCK_MMIO_PROTOCOL
*BlockMmio
;
378 Status
= gBS
->OpenProtocol (
380 &gBlockMmioProtocolGuid
,
381 (VOID
**) &BlockMmio
,
382 This
->DriverBindingHandle
,
384 EFI_OPEN_PROTOCOL_BY_DRIVER
386 if (EFI_ERROR (Status
)) {
392 &gBlockMmioProtocolGuid
,
393 This
->DriverBindingHandle
,
401 Starts the USB mass storage device with this driver.
403 This function consumes USB I/O Portocol, intializes USB mass storage device,
404 installs Block I/O Protocol, and submits Asynchronous Interrupt
405 Transfer to manage the USB mass storage device.
407 @param This The USB mass storage driver binding protocol.
408 @param Controller The USB mass storage device to start on
409 @param RemainingDevicePath The remaining device path.
411 @retval EFI_SUCCESS This driver supports this device.
412 @retval EFI_UNSUPPORTED This driver does not support this device.
413 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
414 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
415 @retval EFI_ALREADY_STARTED This driver has been started.
420 BlockIoDriverBindingStart (
421 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
422 IN EFI_HANDLE Controller
,
423 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
428 Status
= BlockIoInit (This
, Controller
);
429 if (EFI_ERROR (Status
)) {
430 DEBUG ((EFI_D_ERROR
, "BlockIoDriverBindingStart: BlockIoInit (%r)\n", Status
));
434 DEBUG ((EFI_D_INIT
, "BlockIoDriverBindingStart: Successfully started\n"));
440 Stop controlling the device.
442 @param This The USB mass storage driver binding
443 @param Controller The device controller controlled by the driver.
444 @param NumberOfChildren The number of children of this device
445 @param ChildHandleBuffer The buffer of children handle.
447 @retval EFI_SUCCESS The driver stopped from controlling the device.
448 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
449 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
450 @retval Others Failed to stop the driver
455 BlockIoDriverBindingStop (
456 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
457 IN EFI_HANDLE Controller
,
458 IN UINTN NumberOfChildren
,
459 IN EFI_HANDLE
*ChildHandleBuffer
463 BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*Private
;
465 Private
= PRIVATE_FROM_BLOCK_IO (This
);
468 // Uninstall Block I/O protocol from the device handle,
469 // then call the transport protocol to stop itself.
471 Status
= gBS
->UninstallProtocolInterface (
473 &gEfiBlockIoProtocolGuid
,
476 if (EFI_ERROR (Status
)) {
482 &gBlockMmioProtocolGuid
,
483 This
->DriverBindingHandle
,
489 DEBUG ((EFI_D_INFO
, "Successfully stopped BlockIo on BlockMmio\n"));
494 Entrypoint of Block MMIO to Block IO Driver.
496 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
497 Protocol together with Component Name Protocols.
499 @param ImageHandle The firmware allocated handle for the EFI image.
500 @param SystemTable A pointer to the EFI System Table.
502 @retval EFI_SUCCESS The entry point is executed successfully.
507 BlockMmioToBlockIoEntryPoint (
508 IN EFI_HANDLE ImageHandle
,
509 IN EFI_SYSTEM_TABLE
*SystemTable
515 // Install driver binding protocol
517 Status
= EfiLibInstallDriverBindingComponentName2 (
520 &gBlockIoDriverBinding
,
522 &gBlockMmioToBlockIoComponentName
,
523 &gBlockMmioToBlockIoComponentName2
525 ASSERT_EFI_ERROR (Status
);