2 The driver wrappers BlockMmio protocol instances to produce
3 Block I/O Protocol instances.
5 Copyright (c) 2007 - 2010, 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%x\n", Lba
, BufferSize
));
191 return ReadOrWriteBlocks (
203 Writes a specified number of blocks to the device.
205 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
206 It writes a specified number of blocks to the device.
207 All blocks are written, or an error is returned.
209 @param This Indicates a pointer to the calling context.
210 @param MediaId The media ID that the write request is for.
211 @param Lba The starting logical block address to be written.
212 @param BufferSize The size of the Buffer in bytes.
213 This must be a multiple of the intrinsic block size of the device.
214 @param Buffer Pointer to the source buffer for the data.
216 @retval EFI_SUCCESS The data were written correctly to the device.
217 @retval EFI_WRITE_PROTECTED The device cannot be written to.
218 @retval EFI_NO_MEDIA There is no media in the device.
219 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
220 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.
221 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic
222 block size of the device.
223 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
224 or the buffer is not on proper alignment.
230 IN EFI_BLOCK_IO_PROTOCOL
*This
,
237 DEBUG ((EFI_D_INFO
, "BlockIo (MMIO) WriteBlocks: lba=0x%lx, size=0x%x\n", Lba
, BufferSize
));
238 return ReadOrWriteBlocks (
249 Flushes all modified data to a physical block device.
251 @param This Indicates a pointer to the calling context.
253 @retval EFI_SUCCESS All outstanding data were written correctly to the device.
254 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.
255 @retval EFI_NO_MEDIA There is no media in the device.
261 IN EFI_BLOCK_IO_PROTOCOL
*This
269 Initialize data for device that does not support multiple LUNSs.
271 @param This The Driver Binding Protocol instance.
272 @param Controller The device to initialize.
273 @param BlockMmio Pointer to USB_MASS_TRANSPORT.
274 @param Context Parameter for USB_MASS_DEVICE.Context.
276 @retval EFI_SUCCESS Initialization succeeds.
277 @retval Other Initialization fails.
282 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
283 IN EFI_HANDLE Controller
287 BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*Private
;
288 BLOCK_MMIO_PROTOCOL
*BlockMmio
;
290 Private
= (BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*) AllocateZeroPool (sizeof (Private
));
291 ASSERT (Private
!= NULL
);
293 Status
= gBS
->LocateProtocol (
294 &gEfiCpuIo2ProtocolGuid
,
296 (VOID
**) &(Private
->CpuIo
)
298 ASSERT_EFI_ERROR (Status
);
300 Status
= gBS
->OpenProtocol (
302 &gBlockMmioProtocolGuid
,
303 (VOID
**) &BlockMmio
,
304 This
->DriverBindingHandle
,
306 EFI_OPEN_PROTOCOL_BY_DRIVER
308 if (EFI_ERROR (Status
)) {
309 DEBUG ((EFI_D_ERROR
, "BlockIoInit: OpenBlockMmioProtocol By Driver (%r)\n", Status
));
312 DEBUG ((EFI_D_INFO
, "BlockMmio: 0x%x\n", BlockMmio
));
313 DEBUG ((EFI_D_INFO
, "BlockMmio->Media->LastBlock: 0x%lx\n", BlockMmio
->Media
->LastBlock
));
315 Private
->Signature
= BLOCK_MMIO_TO_BLOCK_IO_SIGNATURE
;
316 Private
->Controller
= Controller
;
317 Private
->BlockMmio
= BlockMmio
;
318 Private
->BlockIo
.Media
= BlockMmio
->Media
;
319 Private
->BlockIo
.Reset
= BlockIoReset
;
320 Private
->BlockIo
.ReadBlocks
= BlockIoReadBlocks
;
321 Private
->BlockIo
.WriteBlocks
= BlockIoWriteBlocks
;
322 Private
->BlockIo
.FlushBlocks
= BlockIoFlushBlocks
;
324 DEBUG ((EFI_D_INFO
, "Private->BlockIo.Media->LastBlock: 0x%lx\n", Private
->BlockIo
.Media
->LastBlock
));
326 Status
= gBS
->InstallProtocolInterface (
328 &gEfiBlockIoProtocolGuid
,
329 EFI_NATIVE_INTERFACE
,
332 if (EFI_ERROR (Status
)) {
339 if (Private
!= NULL
) {
342 if (BlockMmio
!= NULL
) {
345 &gBlockMmioProtocolGuid
,
346 This
->DriverBindingHandle
,
355 Check whether the controller is a supported USB mass storage.
357 @param This The USB mass storage driver binding protocol.
358 @param Controller The controller handle to check.
359 @param RemainingDevicePath The remaining device path.
361 @retval EFI_SUCCESS The driver supports this controller.
362 @retval other This device isn't supported.
367 BlockIoDriverBindingSupported (
368 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
369 IN EFI_HANDLE Controller
,
370 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
374 BLOCK_MMIO_PROTOCOL
*BlockMmio
;
376 Status
= gBS
->OpenProtocol (
378 &gBlockMmioProtocolGuid
,
379 (VOID
**) &BlockMmio
,
380 This
->DriverBindingHandle
,
382 EFI_OPEN_PROTOCOL_BY_DRIVER
384 if (EFI_ERROR (Status
)) {
390 &gBlockMmioProtocolGuid
,
391 This
->DriverBindingHandle
,
399 Starts the USB mass storage device with this driver.
401 This function consumes USB I/O Portocol, intializes USB mass storage device,
402 installs Block I/O Protocol, and submits Asynchronous Interrupt
403 Transfer to manage the USB mass storage device.
405 @param This The USB mass storage driver binding protocol.
406 @param Controller The USB mass storage device to start on
407 @param RemainingDevicePath The remaining device path.
409 @retval EFI_SUCCESS This driver supports this device.
410 @retval EFI_UNSUPPORTED This driver does not support this device.
411 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.
412 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.
413 @retval EFI_ALREADY_STARTED This driver has been started.
418 BlockIoDriverBindingStart (
419 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
420 IN EFI_HANDLE Controller
,
421 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
426 Status
= BlockIoInit (This
, Controller
);
427 if (EFI_ERROR (Status
)) {
428 DEBUG ((EFI_D_ERROR
, "BlockIoDriverBindingStart: BlockIoInit (%r)\n", Status
));
432 DEBUG ((EFI_D_INIT
, "BlockIoDriverBindingStart: Successfully started\n"));
438 Stop controlling the device.
440 @param This The USB mass storage driver binding
441 @param Controller The device controller controlled by the driver.
442 @param NumberOfChildren The number of children of this device
443 @param ChildHandleBuffer The buffer of children handle.
445 @retval EFI_SUCCESS The driver stopped from controlling the device.
446 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
447 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.
448 @retval Others Failed to stop the driver
453 BlockIoDriverBindingStop (
454 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
455 IN EFI_HANDLE Controller
,
456 IN UINTN NumberOfChildren
,
457 IN EFI_HANDLE
*ChildHandleBuffer
461 BLOCK_MMIO_TO_BLOCK_IO_DEVICE
*Private
;
463 Private
= PRIVATE_FROM_BLOCK_IO (This
);
466 // Uninstall Block I/O protocol from the device handle,
467 // then call the transport protocol to stop itself.
469 Status
= gBS
->UninstallProtocolInterface (
471 &gEfiBlockIoProtocolGuid
,
474 if (EFI_ERROR (Status
)) {
480 &gBlockMmioProtocolGuid
,
481 This
->DriverBindingHandle
,
487 DEBUG ((EFI_D_INFO
, "Successfully stopped BlockIo on BlockMmio\n"));
492 Entrypoint of Block MMIO to Block IO Driver.
494 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
495 Protocol together with Component Name Protocols.
497 @param ImageHandle The firmware allocated handle for the EFI image.
498 @param SystemTable A pointer to the EFI System Table.
500 @retval EFI_SUCCESS The entry point is executed successfully.
505 BlockMmioToBlockIoEntryPoint (
506 IN EFI_HANDLE ImageHandle
,
507 IN EFI_SYSTEM_TABLE
*SystemTable
513 // Install driver binding protocol
515 Status
= EfiLibInstallDriverBindingComponentName2 (
518 &gBlockIoDriverBinding
,
520 &gBlockMmioToBlockIoComponentName
,
521 &gBlockMmioToBlockIoComponentName2
523 ASSERT_EFI_ERROR (Status
);