]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/BlockMmioToBlockIoDxe/BlockIo.c
Update the copyright notice format
[mirror_edk2.git] / OvmfPkg / BlockMmioToBlockIoDxe / BlockIo.c
CommitLineData
efd82c57 1/** @file\r
2 The driver wrappers BlockMmio protocol instances to produce\r
3 Block I/O Protocol instances.\r
4\r
56d7640a
HT
5 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
6 This program and the accompanying materials\r
efd82c57 7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "BlockIo.h"\r
17\r
18EFI_DRIVER_BINDING_PROTOCOL gBlockIoDriverBinding = {\r
19 BlockIoDriverBindingSupported,\r
20 BlockIoDriverBindingStart,\r
21 BlockIoDriverBindingStop,\r
22 0x11,\r
23 NULL,\r
24 NULL\r
25};\r
26\r
27/**\r
28 Reset the block device.\r
29\r
30 This function implements EFI_BLOCK_IO_PROTOCOL.Reset(). \r
31 It resets the block device hardware.\r
32 ExtendedVerification is ignored in this implementation.\r
33\r
34 @param This Indicates a pointer to the calling context.\r
35 @param ExtendedVerification Indicates that the driver may perform a more exhaustive\r
36 verification operation of the device during reset.\r
37\r
38 @retval EFI_SUCCESS The block device was reset.\r
39 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and could not be reset.\r
40\r
41**/\r
42EFI_STATUS\r
43EFIAPI\r
44BlockIoReset (\r
45 IN EFI_BLOCK_IO_PROTOCOL *This,\r
46 IN BOOLEAN ExtendedVerification\r
47 )\r
48{\r
49 return EFI_SUCCESS;\r
50}\r
51\r
52/**\r
53 Reads the requested number of blocks from the device.\r
54\r
55 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks(). \r
56 It reads the requested number of blocks from the device.\r
57 All the blocks are read, or an error is returned.\r
58\r
59 @param This Indicates a pointer to the calling context.\r
60 @param ReadData If TRUE then read data. If FALSE then write data.\r
61 @param MediaId The media ID that the read request is for.\r
62 @param Lba The starting logical block address to read from on the device.\r
63 @param BufferSize The size of the Buffer in bytes.\r
64 This must be a multiple of the intrinsic block size of the device.\r
65 @param Buffer A pointer to the destination buffer for the data. The caller is\r
66 responsible for either having implicit or explicit ownership of the buffer.\r
67\r
68 @retval EFI_SUCCESS The data was read correctly from the device.\r
69 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.\r
70 @retval EFI_NO_MEDIA There is no media in the device.\r
71 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
72 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.\r
73 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
74 or the buffer is not on proper alignment.\r
75\r
76**/\r
77EFI_STATUS\r
78EFIAPI\r
79ReadOrWriteBlocks (\r
80 IN EFI_BLOCK_IO_PROTOCOL *This,\r
81 IN BOOLEAN ReadData,\r
82 IN UINT32 MediaId,\r
83 IN EFI_LBA Lba,\r
84 IN UINTN BufferSize,\r
85 OUT VOID *Buffer\r
86 )\r
87{\r
88 EFI_STATUS Status;\r
89 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
90 UINTN TotalBlock;\r
91 EFI_BLOCK_IO_MEDIA *Media;\r
92 UINT64 Address;\r
93 UINTN Count;\r
94 EFI_CPU_IO_PROTOCOL_IO_MEM CpuAccessFunction;\r
95\r
96 //\r
97 // First, validate the parameters\r
98 //\r
99 if ((Buffer == NULL) || (BufferSize == 0)) {\r
100 return EFI_INVALID_PARAMETER;\r
101 }\r
102\r
103 //\r
104 // Get private data structure\r
105 //\r
106 Private = PRIVATE_FROM_BLOCK_IO (This);\r
107 Media = Private->BlockMmio->Media;\r
108\r
109 //\r
110 // BufferSize must be a multiple of the intrinsic block size of the device.\r
111 //\r
f46d494a 112 if (ModU64x32 (BufferSize, Media->BlockSize) != 0) {\r
efd82c57 113 return EFI_BAD_BUFFER_SIZE;\r
114 }\r
115\r
f46d494a 116 TotalBlock = (UINTN) DivU64x32 (BufferSize, Media->BlockSize);\r
efd82c57 117\r
118 //\r
119 // Make sure the range to read is valid.\r
120 //\r
121 if (Lba + TotalBlock - 1 > Media->LastBlock) {\r
122 return EFI_INVALID_PARAMETER;\r
123 }\r
124\r
125 if (!(Media->MediaPresent)) {\r
126 return EFI_NO_MEDIA;\r
127 }\r
128\r
129 if (MediaId != Media->MediaId) {\r
130 return EFI_MEDIA_CHANGED;\r
131 }\r
132\r
133 Address = Private->BlockMmio->BaseAddress;\r
f46d494a 134 Address += MultU64x32 (Lba, Media->BlockSize);\r
efd82c57 135\r
f46d494a 136 Count = BufferSize >> 3;\r
efd82c57 137\r
138 if (ReadData) {\r
139 CpuAccessFunction = Private->CpuIo->Mem.Read;\r
140 } else {\r
141 CpuAccessFunction = Private->CpuIo->Mem.Write;\r
142 }\r
143\r
144 Status = (CpuAccessFunction) (\r
145 Private->CpuIo,\r
146 EfiCpuIoWidthUint64,\r
147 Address,\r
148 Count,\r
149 Buffer\r
150 );\r
151\r
152 return Status;\r
153}\r
154\r
155\r
156/**\r
157 Reads the requested number of blocks from the device.\r
158\r
159 This function implements EFI_BLOCK_IO_PROTOCOL.ReadBlocks(). \r
160 It reads the requested number of blocks from the device.\r
161 All the blocks are read, or an error is returned.\r
162\r
163 @param This Indicates a pointer to the calling context.\r
164 @param MediaId The media ID that the read request is for.\r
165 @param Lba The starting logical block address to read from on the device.\r
166 @param BufferSize The size of the Buffer in bytes.\r
167 This must be a multiple of the intrinsic block size of the device.\r
168 @param Buffer A pointer to the destination buffer for the data. The caller is\r
169 responsible for either having implicit or explicit ownership of the buffer.\r
170\r
171 @retval EFI_SUCCESS The data was read correctly from the device.\r
172 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the read operation.\r
173 @retval EFI_NO_MEDIA There is no media in the device.\r
174 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
175 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic block size of the device.\r
176 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,\r
177 or the buffer is not on proper alignment.\r
178\r
179**/\r
180EFI_STATUS\r
181EFIAPI\r
182BlockIoReadBlocks (\r
183 IN EFI_BLOCK_IO_PROTOCOL *This,\r
184 IN UINT32 MediaId,\r
185 IN EFI_LBA Lba,\r
186 IN UINTN BufferSize,\r
187 OUT VOID *Buffer\r
188 )\r
189{\r
190 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) ReadBlocks: lba=0x%lx, size=0x%x\n", Lba, BufferSize));\r
191 return ReadOrWriteBlocks (\r
192 This,\r
193 TRUE,\r
194 MediaId,\r
195 Lba,\r
196 BufferSize,\r
197 Buffer\r
198 );\r
199}\r
200\r
201\r
202/**\r
203 Writes a specified number of blocks to the device.\r
204\r
205 This function implements EFI_BLOCK_IO_PROTOCOL.WriteBlocks(). \r
206 It writes a specified number of blocks to the device.\r
207 All blocks are written, or an error is returned.\r
208\r
209 @param This Indicates a pointer to the calling context.\r
210 @param MediaId The media ID that the write request is for.\r
211 @param Lba The starting logical block address to be written.\r
212 @param BufferSize The size of the Buffer in bytes.\r
213 This must be a multiple of the intrinsic block size of the device.\r
214 @param Buffer Pointer to the source buffer for the data.\r
215\r
216 @retval EFI_SUCCESS The data were written correctly to the device.\r
217 @retval EFI_WRITE_PROTECTED The device cannot be written to.\r
218 @retval EFI_NO_MEDIA There is no media in the device.\r
219 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
220 @retval EFI_DEVICE_ERROR The device reported an error while attempting to perform the write operation.\r
221 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the intrinsic\r
222 block size of the device.\r
223 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,\r
224 or the buffer is not on proper alignment.\r
225\r
226**/\r
227EFI_STATUS\r
228EFIAPI\r
229BlockIoWriteBlocks (\r
230 IN EFI_BLOCK_IO_PROTOCOL *This,\r
231 IN UINT32 MediaId,\r
232 IN EFI_LBA Lba,\r
233 IN UINTN BufferSize,\r
234 IN VOID *Buffer\r
235 )\r
236{\r
237 DEBUG ((EFI_D_INFO, "BlockIo (MMIO) WriteBlocks: lba=0x%lx, size=0x%x\n", Lba, BufferSize));\r
238 return ReadOrWriteBlocks (\r
239 This,\r
240 FALSE,\r
241 MediaId,\r
242 Lba,\r
243 BufferSize,\r
244 Buffer\r
245 );\r
246}\r
247\r
248/**\r
249 Flushes all modified data to a physical block device.\r
250\r
251 @param This Indicates a pointer to the calling context.\r
252\r
253 @retval EFI_SUCCESS All outstanding data were written correctly to the device.\r
254 @retval EFI_DEVICE_ERROR The device reported an error while attempting to write data.\r
255 @retval EFI_NO_MEDIA There is no media in the device.\r
256\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260BlockIoFlushBlocks (\r
261 IN EFI_BLOCK_IO_PROTOCOL *This\r
262 )\r
263{\r
264 return EFI_SUCCESS;\r
265}\r
266\r
267\r
268/**\r
269 Initialize data for device that does not support multiple LUNSs.\r
270\r
271 @param This The Driver Binding Protocol instance.\r
272 @param Controller The device to initialize.\r
273 @param BlockMmio Pointer to USB_MASS_TRANSPORT.\r
274 @param Context Parameter for USB_MASS_DEVICE.Context.\r
275\r
276 @retval EFI_SUCCESS Initialization succeeds.\r
277 @retval Other Initialization fails.\r
278\r
279**/\r
280EFI_STATUS\r
281BlockIoInit (\r
282 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
283 IN EFI_HANDLE Controller\r
284 )\r
285{\r
286 EFI_STATUS Status;\r
287 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
288 BLOCK_MMIO_PROTOCOL *BlockMmio;\r
289\r
290 Private = (BLOCK_MMIO_TO_BLOCK_IO_DEVICE*) AllocateZeroPool (sizeof (Private));\r
291 ASSERT (Private != NULL);\r
292\r
293 Status = gBS->LocateProtocol (\r
294 &gEfiCpuIo2ProtocolGuid,\r
295 NULL,\r
296 (VOID **) &(Private->CpuIo)\r
297 );\r
298 ASSERT_EFI_ERROR (Status);\r
299\r
300 Status = gBS->OpenProtocol (\r
301 Controller,\r
302 &gBlockMmioProtocolGuid,\r
303 (VOID **) &BlockMmio,\r
304 This->DriverBindingHandle,\r
305 Controller,\r
306 EFI_OPEN_PROTOCOL_BY_DRIVER\r
307 );\r
308 if (EFI_ERROR (Status)) {\r
309 DEBUG ((EFI_D_ERROR, "BlockIoInit: OpenBlockMmioProtocol By Driver (%r)\n", Status));\r
310 goto ON_ERROR;\r
311 }\r
312 DEBUG ((EFI_D_INFO, "BlockMmio: 0x%x\n", BlockMmio));\r
313 DEBUG ((EFI_D_INFO, "BlockMmio->Media->LastBlock: 0x%lx\n", BlockMmio->Media->LastBlock));\r
314 \r
315 Private->Signature = BLOCK_MMIO_TO_BLOCK_IO_SIGNATURE;\r
316 Private->Controller = Controller;\r
317 Private->BlockMmio = BlockMmio;\r
318 Private->BlockIo.Media = BlockMmio->Media;\r
319 Private->BlockIo.Reset = BlockIoReset;\r
320 Private->BlockIo.ReadBlocks = BlockIoReadBlocks;\r
321 Private->BlockIo.WriteBlocks = BlockIoWriteBlocks;\r
322 Private->BlockIo.FlushBlocks = BlockIoFlushBlocks;\r
323\r
324 DEBUG ((EFI_D_INFO, "Private->BlockIo.Media->LastBlock: 0x%lx\n", Private->BlockIo.Media->LastBlock));\r
325\r
326 Status = gBS->InstallProtocolInterface (\r
327 &Controller,\r
328 &gEfiBlockIoProtocolGuid,\r
329 EFI_NATIVE_INTERFACE,\r
330 &Private->BlockIo\r
331 );\r
332 if (EFI_ERROR (Status)) {\r
333 goto ON_ERROR;\r
334 }\r
335\r
336 return EFI_SUCCESS;\r
337\r
338ON_ERROR:\r
339 if (Private != NULL) {\r
340 FreePool (Private);\r
341 }\r
342 if (BlockMmio != NULL) {\r
343 gBS->CloseProtocol (\r
344 Controller,\r
345 &gBlockMmioProtocolGuid,\r
346 This->DriverBindingHandle,\r
347 Controller\r
348 );\r
349 }\r
350 return Status; \r
351}\r
352\r
353\r
354/**\r
355 Check whether the controller is a supported USB mass storage.\r
356\r
357 @param This The USB mass storage driver binding protocol.\r
358 @param Controller The controller handle to check.\r
359 @param RemainingDevicePath The remaining device path.\r
360\r
361 @retval EFI_SUCCESS The driver supports this controller.\r
362 @retval other This device isn't supported.\r
363\r
364**/\r
365EFI_STATUS\r
366EFIAPI\r
367BlockIoDriverBindingSupported (\r
368 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
369 IN EFI_HANDLE Controller,\r
370 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
371 )\r
372{\r
373 EFI_STATUS Status;\r
374 BLOCK_MMIO_PROTOCOL *BlockMmio;\r
375\r
376 Status = gBS->OpenProtocol (\r
377 Controller,\r
378 &gBlockMmioProtocolGuid,\r
379 (VOID **) &BlockMmio,\r
380 This->DriverBindingHandle,\r
381 Controller,\r
382 EFI_OPEN_PROTOCOL_BY_DRIVER\r
383 );\r
384 if (EFI_ERROR (Status)) {\r
385 return Status;\r
386 }\r
387\r
388 gBS->CloseProtocol (\r
389 Controller,\r
390 &gBlockMmioProtocolGuid,\r
391 This->DriverBindingHandle,\r
392 Controller\r
393 );\r
394\r
395 return Status;\r
396}\r
397\r
398/**\r
399 Starts the USB mass storage device with this driver.\r
400\r
401 This function consumes USB I/O Portocol, intializes USB mass storage device,\r
402 installs Block I/O Protocol, and submits Asynchronous Interrupt\r
403 Transfer to manage the USB mass storage device.\r
404\r
405 @param This The USB mass storage driver binding protocol.\r
406 @param Controller The USB mass storage device to start on\r
407 @param RemainingDevicePath The remaining device path.\r
408\r
409 @retval EFI_SUCCESS This driver supports this device.\r
410 @retval EFI_UNSUPPORTED This driver does not support this device.\r
411 @retval EFI_DEVICE_ERROR This driver cannot be started due to device Error.\r
412 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
413 @retval EFI_ALREADY_STARTED This driver has been started.\r
414\r
415**/\r
416EFI_STATUS\r
417EFIAPI\r
418BlockIoDriverBindingStart (\r
419 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
420 IN EFI_HANDLE Controller,\r
421 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
422 )\r
423{\r
424 EFI_STATUS Status;\r
425 \r
426 Status = BlockIoInit (This, Controller);\r
427 if (EFI_ERROR (Status)) {\r
428 DEBUG ((EFI_D_ERROR, "BlockIoDriverBindingStart: BlockIoInit (%r)\n", Status));\r
429 return Status;\r
430 }\r
431\r
432 DEBUG ((EFI_D_INIT, "BlockIoDriverBindingStart: Successfully started\n"));\r
433 return Status;\r
434}\r
435\r
436\r
437/**\r
438 Stop controlling the device.\r
439\r
440 @param This The USB mass storage driver binding\r
441 @param Controller The device controller controlled by the driver.\r
442 @param NumberOfChildren The number of children of this device\r
443 @param ChildHandleBuffer The buffer of children handle.\r
444\r
445 @retval EFI_SUCCESS The driver stopped from controlling the device.\r
446 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
447 @retval EFI_UNSUPPORTED Block I/O Protocol is not installed on Controller.\r
448 @retval Others Failed to stop the driver\r
449\r
450**/\r
451EFI_STATUS\r
452EFIAPI\r
453BlockIoDriverBindingStop (\r
454 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
455 IN EFI_HANDLE Controller,\r
456 IN UINTN NumberOfChildren,\r
457 IN EFI_HANDLE *ChildHandleBuffer\r
458 )\r
459{\r
460 EFI_STATUS Status;\r
461 BLOCK_MMIO_TO_BLOCK_IO_DEVICE *Private;\r
462\r
463 Private = PRIVATE_FROM_BLOCK_IO (This);\r
464\r
465 //\r
466 // Uninstall Block I/O protocol from the device handle,\r
467 // then call the transport protocol to stop itself.\r
468 //\r
469 Status = gBS->UninstallProtocolInterface (\r
470 Controller,\r
471 &gEfiBlockIoProtocolGuid,\r
472 &Private->BlockIo\r
473 );\r
474 if (EFI_ERROR (Status)) {\r
475 return Status;\r
476 }\r
477\r
478 gBS->CloseProtocol (\r
479 Controller,\r
480 &gBlockMmioProtocolGuid,\r
481 This->DriverBindingHandle,\r
482 Controller\r
483 );\r
484\r
485 FreePool (Private);\r
486 \r
487 DEBUG ((EFI_D_INFO, "Successfully stopped BlockIo on BlockMmio\n"));\r
488 return EFI_SUCCESS;\r
489}\r
490\r
491/**\r
492 Entrypoint of Block MMIO to Block IO Driver.\r
493\r
494 This function is the entrypoint of USB Mass Storage Driver. It installs Driver Binding\r
495 Protocol together with Component Name Protocols.\r
496\r
497 @param ImageHandle The firmware allocated handle for the EFI image.\r
498 @param SystemTable A pointer to the EFI System Table.\r
499\r
500 @retval EFI_SUCCESS The entry point is executed successfully.\r
501\r
502**/\r
503EFI_STATUS\r
504EFIAPI\r
505BlockMmioToBlockIoEntryPoint (\r
506 IN EFI_HANDLE ImageHandle,\r
507 IN EFI_SYSTEM_TABLE *SystemTable\r
508 )\r
509{\r
510 EFI_STATUS Status;\r
511\r
512 //\r
513 // Install driver binding protocol\r
514 //\r
515 Status = EfiLibInstallDriverBindingComponentName2 (\r
516 ImageHandle,\r
517 SystemTable,\r
518 &gBlockIoDriverBinding,\r
519 ImageHandle,\r
520 &gBlockMmioToBlockIoComponentName,\r
521 &gBlockMmioToBlockIoComponentName2\r
522 );\r
523 ASSERT_EFI_ERROR (Status);\r
524\r
525 return EFI_SUCCESS;\r
526}\r