]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/BlockMmioToBlockIoDxe/BlockIo.c
cf68ceaae9ec875fd93d691d57354a02c6890afe
[mirror_edk2.git] / OvmfPkg / BlockMmioToBlockIoDxe / BlockIo.c
1 /** @file
2 The driver wrappers BlockMmio protocol instances to produce
3 Block I/O Protocol instances.
4
5 Copyright (c) 2007 - 2010, Intel Corporation
6 All rights reserved. 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
10
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.
13
14 **/
15
16 #include "BlockIo.h"
17
18 EFI_DRIVER_BINDING_PROTOCOL gBlockIoDriverBinding = {
19 BlockIoDriverBindingSupported,
20 BlockIoDriverBindingStart,
21 BlockIoDriverBindingStop,
22 0x11,
23 NULL,
24 NULL
25 };
26
27 /**
28 Reset the block device.
29
30 This function implements EFI_BLOCK_IO_PROTOCOL.Reset().
31 It resets the block device hardware.
32 ExtendedVerification is ignored in this implementation.
33
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.
37
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.
40
41 **/
42 EFI_STATUS
43 EFIAPI
44 BlockIoReset (
45 IN EFI_BLOCK_IO_PROTOCOL *This,
46 IN BOOLEAN ExtendedVerification
47 )
48 {
49 return EFI_SUCCESS;
50 }
51
52 /**
53 Reads the requested number of blocks from the device.
54
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.
58
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.
67
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.
75
76 **/
77 EFI_STATUS
78 EFIAPI
79 ReadOrWriteBlocks (
80 IN EFI_BLOCK_IO_PROTOCOL *This,
81 IN BOOLEAN ReadData,
82 IN UINT32 MediaId,
83 IN EFI_LBA Lba,
84 IN UINTN BufferSize,
85 OUT VOID *Buffer
86 )
87 {
88 EFI_STATUS Status;
89 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;
90 UINTN TotalBlock;
91 EFI_BLOCK_IO_MEDIA *Media;
92 UINT64 Address;
93 UINTN Count;
94 EFI_CPU_IO_PROTOCOL_IO_MEM CpuAccessFunction;
95
96 //
97 // First, validate the parameters
98 //
99 if ((Buffer == NULL) || (BufferSize == 0)) {
100 return EFI_INVALID_PARAMETER;
101 }
102
103 //
104 // Get private data structure
105 //
106 Private = PRIVATE_FROM_BLOCK_IO (This);
107 Media = Private->BlockMmio->Media;
108
109 //
110 // BufferSize must be a multiple of the intrinsic block size of the device.
111 //
112 if (ModU64x32 (BufferSize, Media->BlockSize) != 0) {
113 return EFI_BAD_BUFFER_SIZE;
114 }
115
116 TotalBlock = (UINTN) DivU64x32 (BufferSize, Media->BlockSize);
117
118 //
119 // Make sure the range to read is valid.
120 //
121 if (Lba + TotalBlock - 1 > Media->LastBlock) {
122 return EFI_INVALID_PARAMETER;
123 }
124
125 if (!(Media->MediaPresent)) {
126 return EFI_NO_MEDIA;
127 }
128
129 if (MediaId != Media->MediaId) {
130 return EFI_MEDIA_CHANGED;
131 }
132
133 Address = Private->BlockMmio->BaseAddress;
134 Address += MultU64x32 (Lba, Media->BlockSize);
135
136 Count = BufferSize >> 3;
137
138 if (ReadData) {
139 CpuAccessFunction = Private->CpuIo->Mem.Read;
140 } else {
141 CpuAccessFunction = Private->CpuIo->Mem.Write;
142 }
143
144 Status = (CpuAccessFunction) (
145 Private->CpuIo,
146 EfiCpuIoWidthUint64,
147 Address,
148 Count,
149 Buffer
150 );
151
152 return Status;
153 }
154
155
156 /**
157 Reads the requested number of blocks from the device.
158
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.
162
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.
170
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.
178
179 **/
180 EFI_STATUS
181 EFIAPI
182 BlockIoReadBlocks (
183 IN EFI_BLOCK_IO_PROTOCOL *This,
184 IN UINT32 MediaId,
185 IN EFI_LBA Lba,
186 IN UINTN BufferSize,
187 OUT VOID *Buffer
188 )
189 {
190 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) ReadBlocks: lba=0x%lx, size=0x%x\n", Lba, BufferSize));
191 return ReadOrWriteBlocks (
192 This,
193 TRUE,
194 MediaId,
195 Lba,
196 BufferSize,
197 Buffer
198 );
199 }
200
201
202 /**
203 Writes a specified number of blocks to the device.
204
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.
208
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.
215
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.
225
226 **/
227 EFI_STATUS
228 EFIAPI
229 BlockIoWriteBlocks (
230 IN EFI_BLOCK_IO_PROTOCOL *This,
231 IN UINT32 MediaId,
232 IN EFI_LBA Lba,
233 IN UINTN BufferSize,
234 IN VOID *Buffer
235 )
236 {
237 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) WriteBlocks: lba=0x%lx, size=0x%x\n", Lba, BufferSize));
238 return ReadOrWriteBlocks (
239 This,
240 FALSE,
241 MediaId,
242 Lba,
243 BufferSize,
244 Buffer
245 );
246 }
247
248 /**
249 Flushes all modified data to a physical block device.
250
251 @param This Indicates a pointer to the calling context.
252
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.
256
257 **/
258 EFI_STATUS
259 EFIAPI
260 BlockIoFlushBlocks (
261 IN EFI_BLOCK_IO_PROTOCOL *This
262 )
263 {
264 return EFI_SUCCESS;
265 }
266
267
268 /**
269 Initialize data for device that does not support multiple LUNSs.
270
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.
275
276 @retval EFI_SUCCESS Initialization succeeds.
277 @retval Other Initialization fails.
278
279 **/
280 EFI_STATUS
281 BlockIoInit (
282 IN EFI_DRIVER_BINDING_PROTOCOL *This,
283 IN EFI_HANDLE Controller
284 )
285 {
286 EFI_STATUS Status;
287 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;
288 BLOCK_MMIO_PROTOCOL *BlockMmio;
289
290 Private = (BLOCK_MMIO_TO_BLOCK_IO_DEVICE*) AllocateZeroPool (sizeof (Private));
291 ASSERT (Private != NULL);
292
293 Status = gBS->LocateProtocol (
294 &gEfiCpuIo2ProtocolGuid,
295 NULL,
296 (VOID **) &(Private->CpuIo)
297 );
298 ASSERT_EFI_ERROR (Status);
299
300 Status = gBS->OpenProtocol (
301 Controller,
302 &gBlockMmioProtocolGuid,
303 (VOID **) &BlockMmio,
304 This->DriverBindingHandle,
305 Controller,
306 EFI_OPEN_PROTOCOL_BY_DRIVER
307 );
308 if (EFI_ERROR (Status)) {
309 DEBUG ((EFI_D_ERROR, "BlockIoInit: OpenBlockMmioProtocol By Driver (%r)\n", Status));
310 goto ON_ERROR;
311 }
312 DEBUG ((EFI_D_INFO, "BlockMmio: 0x%x\n", BlockMmio));
313 DEBUG ((EFI_D_INFO, "BlockMmio->Media->LastBlock: 0x%lx\n", BlockMmio->Media->LastBlock));
314
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;
323
324 DEBUG ((EFI_D_INFO, "Private->BlockIo.Media->LastBlock: 0x%lx\n", Private->BlockIo.Media->LastBlock));
325
326 Status = gBS->InstallProtocolInterface (
327 &Controller,
328 &gEfiBlockIoProtocolGuid,
329 EFI_NATIVE_INTERFACE,
330 &Private->BlockIo
331 );
332 if (EFI_ERROR (Status)) {
333 goto ON_ERROR;
334 }
335
336 return EFI_SUCCESS;
337
338 ON_ERROR:
339 if (Private != NULL) {
340 FreePool (Private);
341 }
342 if (BlockMmio != NULL) {
343 gBS->CloseProtocol (
344 Controller,
345 &gBlockMmioProtocolGuid,
346 This->DriverBindingHandle,
347 Controller
348 );
349 }
350 return Status;
351 }
352
353
354 /**
355 Check whether the controller is a supported USB mass storage.
356
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.
360
361 @retval EFI_SUCCESS The driver supports this controller.
362 @retval other This device isn't supported.
363
364 **/
365 EFI_STATUS
366 EFIAPI
367 BlockIoDriverBindingSupported (
368 IN EFI_DRIVER_BINDING_PROTOCOL *This,
369 IN EFI_HANDLE Controller,
370 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
371 )
372 {
373 EFI_STATUS Status;
374 BLOCK_MMIO_PROTOCOL *BlockMmio;
375
376 Status = gBS->OpenProtocol (
377 Controller,
378 &gBlockMmioProtocolGuid,
379 (VOID **) &BlockMmio,
380 This->DriverBindingHandle,
381 Controller,
382 EFI_OPEN_PROTOCOL_BY_DRIVER
383 );
384 if (EFI_ERROR (Status)) {
385 return Status;
386 }
387
388 gBS->CloseProtocol (
389 Controller,
390 &gBlockMmioProtocolGuid,
391 This->DriverBindingHandle,
392 Controller
393 );
394
395 return Status;
396 }
397
398 /**
399 Starts the USB mass storage device with this driver.
400
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.
404
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.
408
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.
414
415 **/
416 EFI_STATUS
417 EFIAPI
418 BlockIoDriverBindingStart (
419 IN EFI_DRIVER_BINDING_PROTOCOL *This,
420 IN EFI_HANDLE Controller,
421 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
422 )
423 {
424 EFI_STATUS Status;
425
426 Status = BlockIoInit (This, Controller);
427 if (EFI_ERROR (Status)) {
428 DEBUG ((EFI_D_ERROR, "BlockIoDriverBindingStart: BlockIoInit (%r)\n", Status));
429 return Status;
430 }
431
432 DEBUG ((EFI_D_INIT, "BlockIoDriverBindingStart: Successfully started\n"));
433 return Status;
434 }
435
436
437 /**
438 Stop controlling the device.
439
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.
444
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
449
450 **/
451 EFI_STATUS
452 EFIAPI
453 BlockIoDriverBindingStop (
454 IN EFI_DRIVER_BINDING_PROTOCOL *This,
455 IN EFI_HANDLE Controller,
456 IN UINTN NumberOfChildren,
457 IN EFI_HANDLE *ChildHandleBuffer
458 )
459 {
460 EFI_STATUS Status;
461 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;
462
463 Private = PRIVATE_FROM_BLOCK_IO (This);
464
465 //
466 // Uninstall Block I/O protocol from the device handle,
467 // then call the transport protocol to stop itself.
468 //
469 Status = gBS->UninstallProtocolInterface (
470 Controller,
471 &gEfiBlockIoProtocolGuid,
472 &Private->BlockIo
473 );
474 if (EFI_ERROR (Status)) {
475 return Status;
476 }
477
478 gBS->CloseProtocol (
479 Controller,
480 &gBlockMmioProtocolGuid,
481 This->DriverBindingHandle,
482 Controller
483 );
484
485 FreePool (Private);
486
487 DEBUG ((EFI_D_INFO, "Successfully stopped BlockIo on BlockMmio\n"));
488 return EFI_SUCCESS;
489 }
490
491 /**
492 Entrypoint of Block MMIO to Block IO Driver.
493
494 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding
495 Protocol together with Component Name Protocols.
496
497 @param ImageHandle The firmware allocated handle for the EFI image.
498 @param SystemTable A pointer to the EFI System Table.
499
500 @retval EFI_SUCCESS The entry point is executed successfully.
501
502 **/
503 EFI_STATUS
504 EFIAPI
505 BlockMmioToBlockIoEntryPoint (
506 IN EFI_HANDLE ImageHandle,
507 IN EFI_SYSTEM_TABLE *SystemTable
508 )
509 {
510 EFI_STATUS Status;
511
512 //
513 // Install driver binding protocol
514 //
515 Status = EfiLibInstallDriverBindingComponentName2 (
516 ImageHandle,
517 SystemTable,
518 &gBlockIoDriverBinding,
519 ImageHandle,
520 &gBlockMmioToBlockIoComponentName,
521 &gBlockMmioToBlockIoComponentName2
522 );
523 ASSERT_EFI_ERROR (Status);
524
525 return EFI_SUCCESS;
526 }