]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/VirtioBlkDxe/VirtioBlk.c
OvmfPkg: VirtioLib: add Virtio10WriteFeatures() function
[mirror_edk2.git] / OvmfPkg / VirtioBlkDxe / VirtioBlk.c
... / ...
CommitLineData
1/** @file\r
2\r
3 This driver produces Block I/O Protocol instances for virtio-blk devices.\r
4\r
5 The implementation is basic:\r
6\r
7 - No attach/detach (ie. removable media).\r
8\r
9 - Although the non-blocking interfaces of EFI_BLOCK_IO2_PROTOCOL could be a\r
10 good match for multiple in-flight virtio-blk requests, we stick to\r
11 synchronous requests and EFI_BLOCK_IO_PROTOCOL for now.\r
12\r
13 Copyright (C) 2012, Red Hat, Inc.\r
14 Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>\r
15\r
16 This program and the accompanying materials are licensed and made available\r
17 under the terms and conditions of the BSD License which accompanies this\r
18 distribution. The full text of the license may be found at\r
19 http://opensource.org/licenses/bsd-license.php\r
20\r
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
22 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
23\r
24**/\r
25\r
26#include <IndustryStandard/VirtioBlk.h>\r
27#include <Library/BaseMemoryLib.h>\r
28#include <Library/DebugLib.h>\r
29#include <Library/MemoryAllocationLib.h>\r
30#include <Library/UefiBootServicesTableLib.h>\r
31#include <Library/UefiLib.h>\r
32#include <Library/VirtioLib.h>\r
33\r
34#include "VirtioBlk.h"\r
35\r
36/**\r
37\r
38 Convenience macros to read and write region 0 IO space elements of the\r
39 virtio-blk device, for configuration purposes.\r
40\r
41 The following macros make it possible to specify only the "core parameters"\r
42 for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()\r
43 returns, the transaction will have been completed.\r
44\r
45 @param[in] Dev Pointer to the VBLK_DEV structure whose VirtIo space\r
46 we're accessing. Dev->VirtIo must be valid.\r
47\r
48 @param[in] Field A field name from VBLK_HDR, identifying the virtio-blk\r
49 configuration item to access.\r
50\r
51 @param[in] Value (VIRTIO_CFG_WRITE() only.) The value to write to the\r
52 selected configuration item.\r
53\r
54 @param[out] Pointer (VIRTIO_CFG_READ() only.) The object to receive the\r
55 value read from the configuration item. Its type must be\r
56 one of UINT8, UINT16, UINT32, UINT64.\r
57\r
58\r
59 @return Status code returned by Virtio->WriteDevice() /\r
60 Virtio->ReadDevice().\r
61\r
62**/\r
63\r
64#define VIRTIO_CFG_WRITE(Dev, Field, Value) ((Dev)->VirtIo->WriteDevice ( \\r
65 (Dev)->VirtIo, \\r
66 OFFSET_OF_VBLK (Field), \\r
67 SIZE_OF_VBLK (Field), \\r
68 (Value) \\r
69 ))\r
70\r
71#define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice ( \\r
72 (Dev)->VirtIo, \\r
73 OFFSET_OF_VBLK (Field), \\r
74 SIZE_OF_VBLK (Field), \\r
75 sizeof *(Pointer), \\r
76 (Pointer) \\r
77 ))\r
78\r
79\r
80//\r
81// UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol\r
82// Driver Writer's Guide for UEFI 2.3.1 v1.01,\r
83// 24.2 Block I/O Protocol Implementations\r
84//\r
85EFI_STATUS\r
86EFIAPI\r
87VirtioBlkReset (\r
88 IN EFI_BLOCK_IO_PROTOCOL *This,\r
89 IN BOOLEAN ExtendedVerification\r
90 )\r
91{\r
92 //\r
93 // If we managed to initialize and install the driver, then the device is\r
94 // working correctly.\r
95 //\r
96 return EFI_SUCCESS;\r
97}\r
98\r
99/**\r
100\r
101 Verify correctness of the read/write (not flush) request submitted to the\r
102 EFI_BLOCK_IO_PROTOCOL instance.\r
103\r
104 This function provides most verification steps described in:\r
105\r
106 UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
107 Protocol,\r
108 - EFI_BLOCK_IO_PROTOCOL.ReadBlocks()\r
109 - EFI_BLOCK_IO_PROTOCOL.WriteBlocks()\r
110\r
111 Driver Writer's Guide for UEFI 2.3.1 v1.01,\r
112 - 24.2.2. ReadBlocks() and ReadBlocksEx() Implementation\r
113 - 24.2.3 WriteBlocks() and WriteBlockEx() Implementation\r
114\r
115 Request sizes are limited to 1 GB (checked). This is not a practical\r
116 limitation, just conformance to virtio-0.9.5, 2.3.2 Descriptor Table: "no\r
117 descriptor chain may be more than 2^32 bytes long in total".\r
118\r
119 Some Media characteristics are hardcoded in VirtioBlkInit() below (like\r
120 non-removable media, no restriction on buffer alignment etc); we rely on\r
121 those here without explicit mention.\r
122\r
123 @param[in] Media The EFI_BLOCK_IO_MEDIA characteristics for\r
124 this driver instance, extracted from the\r
125 underlying virtio-blk device at initialization\r
126 time. We validate the request against this set\r
127 of attributes.\r
128\r
129\r
130 @param[in] Lba Logical Block Address: number of logical\r
131 blocks to skip from the beginning of the\r
132 device.\r
133\r
134 @param[in] PositiveBufferSize Size of buffer to transfer, in bytes. The\r
135 caller is responsible to ensure this parameter\r
136 is positive.\r
137\r
138 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to\r
139 device.\r
140\r
141\r
142 @@return Validation result to be forwarded outwards by\r
143 ReadBlocks() and WriteBlocks, as required by\r
144 the specs above.\r
145\r
146**/\r
147STATIC\r
148EFI_STATUS\r
149EFIAPI\r
150VerifyReadWriteRequest (\r
151 IN EFI_BLOCK_IO_MEDIA *Media,\r
152 IN EFI_LBA Lba,\r
153 IN UINTN PositiveBufferSize,\r
154 IN BOOLEAN RequestIsWrite\r
155 )\r
156{\r
157 UINTN BlockCount;\r
158\r
159 ASSERT (PositiveBufferSize > 0);\r
160\r
161 if (PositiveBufferSize > SIZE_1GB ||\r
162 PositiveBufferSize % Media->BlockSize > 0) {\r
163 return EFI_BAD_BUFFER_SIZE;\r
164 }\r
165 BlockCount = PositiveBufferSize / Media->BlockSize;\r
166\r
167 //\r
168 // Avoid unsigned wraparound on either side in the second comparison.\r
169 //\r
170 if (Lba > Media->LastBlock || BlockCount - 1 > Media->LastBlock - Lba) {\r
171 return EFI_INVALID_PARAMETER;\r
172 }\r
173\r
174 if (RequestIsWrite && Media->ReadOnly) {\r
175 return EFI_WRITE_PROTECTED;\r
176 }\r
177\r
178 return EFI_SUCCESS;\r
179}\r
180\r
181\r
182\r
183\r
184/**\r
185\r
186 Format a read / write / flush request as three consecutive virtio\r
187 descriptors, push them to the host, and poll for the response.\r
188\r
189 This is the main workhorse function. Two use cases are supported, read/write\r
190 and flush. The function may only be called after the request parameters have\r
191 been verified by\r
192 - specific checks in ReadBlocks() / WriteBlocks() / FlushBlocks(), and\r
193 - VerifyReadWriteRequest() (for read/write only).\r
194\r
195 Parameters handled commonly:\r
196\r
197 @param[in] Dev The virtio-blk device the request is targeted\r
198 at.\r
199\r
200 Flush request:\r
201\r
202 @param[in] Lba Must be zero.\r
203\r
204 @param[in] BufferSize Must be zero.\r
205\r
206 @param[in out] Buffer Ignored by the function.\r
207\r
208 @param[in] RequestIsWrite Must be TRUE.\r
209\r
210 Read/Write request:\r
211\r
212 @param[in] Lba Logical Block Address: number of logical blocks\r
213 to skip from the beginning of the device.\r
214\r
215 @param[in] BufferSize Size of buffer to transfer, in bytes. The caller\r
216 is responsible to ensure this parameter is\r
217 positive.\r
218\r
219 @param[in out] Buffer The guest side area to read data from the device\r
220 into, or write data to the device from.\r
221\r
222 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to\r
223 device.\r
224\r
225 Return values are common to both use cases, and are appropriate to be\r
226 forwarded by the EFI_BLOCK_IO_PROTOCOL functions (ReadBlocks(),\r
227 WriteBlocks(), FlushBlocks()).\r
228\r
229\r
230 @retval EFI_SUCCESS Transfer complete.\r
231\r
232 @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or\r
233 unable to parse host response, or host response\r
234 is not VIRTIO_BLK_S_OK.\r
235\r
236**/\r
237\r
238STATIC\r
239EFI_STATUS\r
240EFIAPI\r
241SynchronousRequest (\r
242 IN VBLK_DEV *Dev,\r
243 IN EFI_LBA Lba,\r
244 IN UINTN BufferSize,\r
245 IN OUT volatile VOID *Buffer,\r
246 IN BOOLEAN RequestIsWrite\r
247 )\r
248{\r
249 UINT32 BlockSize;\r
250 volatile VIRTIO_BLK_REQ Request;\r
251 volatile UINT8 HostStatus;\r
252 DESC_INDICES Indices;\r
253\r
254 BlockSize = Dev->BlockIoMedia.BlockSize;\r
255\r
256 //\r
257 // ensured by VirtioBlkInit()\r
258 //\r
259 ASSERT (BlockSize > 0);\r
260 ASSERT (BlockSize % 512 == 0);\r
261\r
262 //\r
263 // ensured by contract above, plus VerifyReadWriteRequest()\r
264 //\r
265 ASSERT (BufferSize % BlockSize == 0);\r
266\r
267 //\r
268 // Prepare virtio-blk request header, setting zero size for flush.\r
269 // IO Priority is homogeneously 0.\r
270 //\r
271 Request.Type = RequestIsWrite ?\r
272 (BufferSize == 0 ? VIRTIO_BLK_T_FLUSH : VIRTIO_BLK_T_OUT) :\r
273 VIRTIO_BLK_T_IN;\r
274 Request.IoPrio = 0;\r
275 Request.Sector = MultU64x32(Lba, BlockSize / 512);\r
276\r
277 VirtioPrepare (&Dev->Ring, &Indices);\r
278\r
279 //\r
280 // preset a host status for ourselves that we do not accept as success\r
281 //\r
282 HostStatus = VIRTIO_BLK_S_IOERR;\r
283\r
284 //\r
285 // ensured by VirtioBlkInit() -- this predicate, in combination with the\r
286 // lock-step progress, ensures we don't have to track free descriptors.\r
287 //\r
288 ASSERT (Dev->Ring.QueueSize >= 3);\r
289\r
290 //\r
291 // virtio-blk header in first desc\r
292 //\r
293 VirtioAppendDesc (&Dev->Ring, (UINTN) &Request, sizeof Request,\r
294 VRING_DESC_F_NEXT, &Indices);\r
295\r
296 //\r
297 // data buffer for read/write in second desc\r
298 //\r
299 if (BufferSize > 0) {\r
300 //\r
301 // From virtio-0.9.5, 2.3.2 Descriptor Table:\r
302 // "no descriptor chain may be more than 2^32 bytes long in total".\r
303 //\r
304 // The predicate is ensured by the call contract above (for flush), or\r
305 // VerifyReadWriteRequest() (for read/write). It also implies that\r
306 // converting BufferSize to UINT32 will not truncate it.\r
307 //\r
308 ASSERT (BufferSize <= SIZE_1GB);\r
309\r
310 //\r
311 // VRING_DESC_F_WRITE is interpreted from the host's point of view.\r
312 //\r
313 VirtioAppendDesc (&Dev->Ring, (UINTN) Buffer, (UINT32) BufferSize,\r
314 VRING_DESC_F_NEXT | (RequestIsWrite ? 0 : VRING_DESC_F_WRITE),\r
315 &Indices);\r
316 }\r
317\r
318 //\r
319 // host status in last (second or third) desc\r
320 //\r
321 VirtioAppendDesc (&Dev->Ring, (UINTN) &HostStatus, sizeof HostStatus,\r
322 VRING_DESC_F_WRITE, &Indices);\r
323\r
324 //\r
325 // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D).\r
326 //\r
327 if (VirtioFlush (Dev->VirtIo, 0, &Dev->Ring, &Indices,\r
328 NULL) == EFI_SUCCESS &&\r
329 HostStatus == VIRTIO_BLK_S_OK) {\r
330 return EFI_SUCCESS;\r
331 }\r
332\r
333 return EFI_DEVICE_ERROR;\r
334}\r
335\r
336\r
337/**\r
338\r
339 ReadBlocks() operation for virtio-blk.\r
340\r
341 See\r
342 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
343 Protocol, EFI_BLOCK_IO_PROTOCOL.ReadBlocks().\r
344 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.2. ReadBlocks() and\r
345 ReadBlocksEx() Implementation.\r
346\r
347 Parameter checks and conformant return values are implemented in\r
348 VerifyReadWriteRequest() and SynchronousRequest().\r
349\r
350 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,\r
351 successfully.\r
352\r
353**/\r
354\r
355EFI_STATUS\r
356EFIAPI\r
357VirtioBlkReadBlocks (\r
358 IN EFI_BLOCK_IO_PROTOCOL *This,\r
359 IN UINT32 MediaId,\r
360 IN EFI_LBA Lba,\r
361 IN UINTN BufferSize,\r
362 OUT VOID *Buffer\r
363 )\r
364{\r
365 VBLK_DEV *Dev;\r
366 EFI_STATUS Status;\r
367\r
368 if (BufferSize == 0) {\r
369 return EFI_SUCCESS;\r
370 }\r
371\r
372 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
373 Status = VerifyReadWriteRequest (\r
374 &Dev->BlockIoMedia,\r
375 Lba,\r
376 BufferSize,\r
377 FALSE // RequestIsWrite\r
378 );\r
379 if (EFI_ERROR (Status)) {\r
380 return Status;\r
381 }\r
382\r
383 return SynchronousRequest (\r
384 Dev,\r
385 Lba,\r
386 BufferSize,\r
387 Buffer,\r
388 FALSE // RequestIsWrite\r
389 );\r
390}\r
391\r
392/**\r
393\r
394 WriteBlocks() operation for virtio-blk.\r
395\r
396 See\r
397 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
398 Protocol, EFI_BLOCK_IO_PROTOCOL.WriteBlocks().\r
399 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.3 WriteBlocks() and\r
400 WriteBlockEx() Implementation.\r
401\r
402 Parameter checks and conformant return values are implemented in\r
403 VerifyReadWriteRequest() and SynchronousRequest().\r
404\r
405 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,\r
406 successfully.\r
407\r
408**/\r
409\r
410EFI_STATUS\r
411EFIAPI\r
412VirtioBlkWriteBlocks (\r
413 IN EFI_BLOCK_IO_PROTOCOL *This,\r
414 IN UINT32 MediaId,\r
415 IN EFI_LBA Lba,\r
416 IN UINTN BufferSize,\r
417 IN VOID *Buffer\r
418 )\r
419{\r
420 VBLK_DEV *Dev;\r
421 EFI_STATUS Status;\r
422\r
423 if (BufferSize == 0) {\r
424 return EFI_SUCCESS;\r
425 }\r
426\r
427 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
428 Status = VerifyReadWriteRequest (\r
429 &Dev->BlockIoMedia,\r
430 Lba,\r
431 BufferSize,\r
432 TRUE // RequestIsWrite\r
433 );\r
434 if (EFI_ERROR (Status)) {\r
435 return Status;\r
436 }\r
437\r
438 return SynchronousRequest (\r
439 Dev,\r
440 Lba,\r
441 BufferSize,\r
442 Buffer,\r
443 TRUE // RequestIsWrite\r
444 );\r
445}\r
446\r
447\r
448/**\r
449\r
450 FlushBlocks() operation for virtio-blk.\r
451\r
452 See\r
453 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O\r
454 Protocol, EFI_BLOCK_IO_PROTOCOL.FlushBlocks().\r
455 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.4 FlushBlocks() and\r
456 FlushBlocksEx() Implementation.\r
457\r
458 If the underlying virtio-blk device doesn't support flushing (ie.\r
459 write-caching), then this function should not be called by higher layers,\r
460 according to EFI_BLOCK_IO_MEDIA characteristics set in VirtioBlkInit().\r
461 Should they do nonetheless, we do nothing, successfully.\r
462\r
463**/\r
464\r
465EFI_STATUS\r
466EFIAPI\r
467VirtioBlkFlushBlocks (\r
468 IN EFI_BLOCK_IO_PROTOCOL *This\r
469 )\r
470{\r
471 VBLK_DEV *Dev;\r
472\r
473 Dev = VIRTIO_BLK_FROM_BLOCK_IO (This);\r
474 return Dev->BlockIoMedia.WriteCaching ?\r
475 SynchronousRequest (\r
476 Dev,\r
477 0, // Lba\r
478 0, // BufferSize\r
479 NULL, // Buffer\r
480 TRUE // RequestIsWrite\r
481 ) :\r
482 EFI_SUCCESS;\r
483}\r
484\r
485\r
486/**\r
487\r
488 Device probe function for this driver.\r
489\r
490 The DXE core calls this function for any given device in order to see if the\r
491 driver can drive the device.\r
492\r
493 Specs relevant in the general sense:\r
494\r
495 - UEFI Spec 2.3.1 + Errata C:\r
496 - 6.3 Protocol Handler Services -- for accessing the underlying device\r
497 - 10.1 EFI Driver Binding Protocol -- for exporting ourselves\r
498\r
499 - Driver Writer's Guide for UEFI 2.3.1 v1.01:\r
500 - 5.1.3.4 OpenProtocol() and CloseProtocol() -- for accessing the\r
501 underlying device\r
502 - 9 Driver Binding Protocol -- for exporting ourselves\r
503\r
504 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
505 incorporating this driver (independently of\r
506 any device).\r
507\r
508 @param[in] DeviceHandle The device to probe.\r
509\r
510 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r
511\r
512\r
513 @retval EFI_SUCCESS The driver supports the device being probed.\r
514\r
515 @retval EFI_UNSUPPORTED Based on virtio-blk discovery, we do not support\r
516 the device.\r
517\r
518 @return Error codes from the OpenProtocol() boot service or\r
519 the VirtIo protocol.\r
520\r
521**/\r
522\r
523EFI_STATUS\r
524EFIAPI\r
525VirtioBlkDriverBindingSupported (\r
526 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
527 IN EFI_HANDLE DeviceHandle,\r
528 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
529 )\r
530{\r
531 EFI_STATUS Status;\r
532 VIRTIO_DEVICE_PROTOCOL *VirtIo;\r
533\r
534 //\r
535 // Attempt to open the device with the VirtIo set of interfaces. On success,\r
536 // the protocol is "instantiated" for the VirtIo device. Covers duplicate\r
537 // open attempts (EFI_ALREADY_STARTED).\r
538 //\r
539 Status = gBS->OpenProtocol (\r
540 DeviceHandle, // candidate device\r
541 &gVirtioDeviceProtocolGuid, // for generic VirtIo access\r
542 (VOID **)&VirtIo, // handle to instantiate\r
543 This->DriverBindingHandle, // requestor driver identity\r
544 DeviceHandle, // ControllerHandle, according to\r
545 // the UEFI Driver Model\r
546 EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive VirtIo access to\r
547 // the device; to be released\r
548 );\r
549 if (EFI_ERROR (Status)) {\r
550 return Status;\r
551 }\r
552\r
553 if (VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_BLOCK_DEVICE) {\r
554 Status = EFI_UNSUPPORTED;\r
555 }\r
556\r
557 //\r
558 // We needed VirtIo access only transitorily, to see whether we support the\r
559 // device or not.\r
560 //\r
561 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
562 This->DriverBindingHandle, DeviceHandle);\r
563 return Status;\r
564}\r
565\r
566\r
567/**\r
568\r
569 Set up all BlockIo and virtio-blk aspects of this driver for the specified\r
570 device.\r
571\r
572 @param[in out] Dev The driver instance to configure. The caller is\r
573 responsible for Dev->VirtIo's validity (ie. working IO\r
574 access to the underlying virtio-blk device).\r
575\r
576 @retval EFI_SUCCESS Setup complete.\r
577\r
578 @retval EFI_UNSUPPORTED The driver is unable to work with the virtio ring or\r
579 virtio-blk attributes the host provides.\r
580\r
581 @return Error codes from VirtioRingInit() or\r
582 VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE().\r
583\r
584**/\r
585\r
586STATIC\r
587EFI_STATUS\r
588EFIAPI\r
589VirtioBlkInit (\r
590 IN OUT VBLK_DEV *Dev\r
591 )\r
592{\r
593 UINT8 NextDevStat;\r
594 EFI_STATUS Status;\r
595\r
596 UINT64 Features;\r
597 UINT64 NumSectors;\r
598 UINT32 BlockSize;\r
599 UINT8 PhysicalBlockExp;\r
600 UINT8 AlignmentOffset;\r
601 UINT32 OptIoSize;\r
602 UINT16 QueueSize;\r
603\r
604 PhysicalBlockExp = 0;\r
605 AlignmentOffset = 0;\r
606 OptIoSize = 0;\r
607\r
608 //\r
609 // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.\r
610 //\r
611 NextDevStat = 0; // step 1 -- reset device\r
612 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
613 if (EFI_ERROR (Status)) {\r
614 goto Failed;\r
615 }\r
616\r
617 NextDevStat |= VSTAT_ACK; // step 2 -- acknowledge device presence\r
618 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
619 if (EFI_ERROR (Status)) {\r
620 goto Failed;\r
621 }\r
622\r
623 NextDevStat |= VSTAT_DRIVER; // step 3 -- we know how to drive it\r
624 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
625 if (EFI_ERROR (Status)) {\r
626 goto Failed;\r
627 }\r
628\r
629 //\r
630 // Set Page Size - MMIO VirtIo Specific\r
631 //\r
632 Status = Dev->VirtIo->SetPageSize (Dev->VirtIo, EFI_PAGE_SIZE);\r
633 if (EFI_ERROR (Status)) {\r
634 goto Failed;\r
635 }\r
636\r
637 //\r
638 // step 4a -- retrieve and validate features\r
639 //\r
640 Status = Dev->VirtIo->GetDeviceFeatures (Dev->VirtIo, &Features);\r
641 if (EFI_ERROR (Status)) {\r
642 goto Failed;\r
643 }\r
644\r
645 Status = VIRTIO_CFG_READ (Dev, Capacity, &NumSectors);\r
646 if (EFI_ERROR (Status)) {\r
647 goto Failed;\r
648 }\r
649 if (NumSectors == 0) {\r
650 Status = EFI_UNSUPPORTED;\r
651 goto Failed;\r
652 }\r
653\r
654 if (Features & VIRTIO_BLK_F_BLK_SIZE) {\r
655 Status = VIRTIO_CFG_READ (Dev, BlkSize, &BlockSize);\r
656 if (EFI_ERROR (Status)) {\r
657 goto Failed;\r
658 }\r
659 if (BlockSize == 0 || BlockSize % 512 != 0 ||\r
660 ModU64x32 (NumSectors, BlockSize / 512) != 0) {\r
661 //\r
662 // We can only handle a logical block consisting of whole sectors,\r
663 // and only a disk composed of whole logical blocks.\r
664 //\r
665 Status = EFI_UNSUPPORTED;\r
666 goto Failed;\r
667 }\r
668 }\r
669 else {\r
670 BlockSize = 512;\r
671 }\r
672\r
673 if (Features & VIRTIO_BLK_F_TOPOLOGY) {\r
674 Status = VIRTIO_CFG_READ (Dev, Topology.PhysicalBlockExp,\r
675 &PhysicalBlockExp);\r
676 if (EFI_ERROR (Status)) {\r
677 goto Failed;\r
678 }\r
679 if (PhysicalBlockExp >= 32) {\r
680 Status = EFI_UNSUPPORTED;\r
681 goto Failed;\r
682 }\r
683\r
684 Status = VIRTIO_CFG_READ (Dev, Topology.AlignmentOffset, &AlignmentOffset);\r
685 if (EFI_ERROR (Status)) {\r
686 goto Failed;\r
687 }\r
688\r
689 Status = VIRTIO_CFG_READ (Dev, Topology.OptIoSize, &OptIoSize);\r
690 if (EFI_ERROR (Status)) {\r
691 goto Failed;\r
692 }\r
693 }\r
694\r
695 //\r
696 // step 4b -- allocate virtqueue\r
697 //\r
698 Status = Dev->VirtIo->SetQueueSel (Dev->VirtIo, 0);\r
699 if (EFI_ERROR (Status)) {\r
700 goto Failed;\r
701 }\r
702 Status = Dev->VirtIo->GetQueueNumMax (Dev->VirtIo, &QueueSize);\r
703 if (EFI_ERROR (Status)) {\r
704 goto Failed;\r
705 }\r
706 if (QueueSize < 3) { // SynchronousRequest() uses at most three descriptors\r
707 Status = EFI_UNSUPPORTED;\r
708 goto Failed;\r
709 }\r
710\r
711 Status = VirtioRingInit (QueueSize, &Dev->Ring);\r
712 if (EFI_ERROR (Status)) {\r
713 goto Failed;\r
714 }\r
715\r
716 //\r
717 // Additional steps for MMIO: align the queue appropriately, and set the\r
718 // size. If anything fails from here on, we must release the ring resources.\r
719 //\r
720 Status = Dev->VirtIo->SetQueueNum (Dev->VirtIo, QueueSize);\r
721 if (EFI_ERROR (Status)) {\r
722 goto ReleaseQueue;\r
723 }\r
724\r
725 Status = Dev->VirtIo->SetQueueAlign (Dev->VirtIo, EFI_PAGE_SIZE);\r
726 if (EFI_ERROR (Status)) {\r
727 goto ReleaseQueue;\r
728 }\r
729\r
730 //\r
731 // step 4c -- Report GPFN (guest-physical frame number) of queue.\r
732 //\r
733 Status = Dev->VirtIo->SetQueueAddress (Dev->VirtIo, &Dev->Ring);\r
734 if (EFI_ERROR (Status)) {\r
735 goto ReleaseQueue;\r
736 }\r
737\r
738\r
739 //\r
740 // step 5 -- Report understood features.\r
741 //\r
742 Features &= VIRTIO_BLK_F_BLK_SIZE | VIRTIO_BLK_F_TOPOLOGY | VIRTIO_BLK_F_RO |\r
743 VIRTIO_BLK_F_FLUSH;\r
744 Status = Dev->VirtIo->SetGuestFeatures (Dev->VirtIo, Features);\r
745 if (EFI_ERROR (Status)) {\r
746 goto ReleaseQueue;\r
747 }\r
748\r
749 //\r
750 // step 6 -- initialization complete\r
751 //\r
752 NextDevStat |= VSTAT_DRIVER_OK;\r
753 Status = Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
754 if (EFI_ERROR (Status)) {\r
755 goto ReleaseQueue;\r
756 }\r
757\r
758 //\r
759 // Populate the exported interface's attributes; see UEFI spec v2.4, 12.9 EFI\r
760 // Block I/O Protocol.\r
761 //\r
762 Dev->BlockIo.Revision = 0;\r
763 Dev->BlockIo.Media = &Dev->BlockIoMedia;\r
764 Dev->BlockIo.Reset = &VirtioBlkReset;\r
765 Dev->BlockIo.ReadBlocks = &VirtioBlkReadBlocks;\r
766 Dev->BlockIo.WriteBlocks = &VirtioBlkWriteBlocks;\r
767 Dev->BlockIo.FlushBlocks = &VirtioBlkFlushBlocks;\r
768 Dev->BlockIoMedia.MediaId = 0;\r
769 Dev->BlockIoMedia.RemovableMedia = FALSE;\r
770 Dev->BlockIoMedia.MediaPresent = TRUE;\r
771 Dev->BlockIoMedia.LogicalPartition = FALSE;\r
772 Dev->BlockIoMedia.ReadOnly = (BOOLEAN) ((Features & VIRTIO_BLK_F_RO) != 0);\r
773 Dev->BlockIoMedia.WriteCaching = (BOOLEAN) ((Features & VIRTIO_BLK_F_FLUSH) != 0);\r
774 Dev->BlockIoMedia.BlockSize = BlockSize;\r
775 Dev->BlockIoMedia.IoAlign = 0;\r
776 Dev->BlockIoMedia.LastBlock = DivU64x32 (NumSectors,\r
777 BlockSize / 512) - 1;\r
778\r
779 DEBUG ((DEBUG_INFO, "%a: LbaSize=0x%x[B] NumBlocks=0x%Lx[Lba]\n",\r
780 __FUNCTION__, Dev->BlockIoMedia.BlockSize,\r
781 Dev->BlockIoMedia.LastBlock + 1));\r
782\r
783 if (Features & VIRTIO_BLK_F_TOPOLOGY) {\r
784 Dev->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;\r
785\r
786 Dev->BlockIoMedia.LowestAlignedLba = AlignmentOffset;\r
787 Dev->BlockIoMedia.LogicalBlocksPerPhysicalBlock = 1u << PhysicalBlockExp;\r
788 Dev->BlockIoMedia.OptimalTransferLengthGranularity = OptIoSize;\r
789\r
790 DEBUG ((DEBUG_INFO, "%a: FirstAligned=0x%Lx[Lba] PhysBlkSize=0x%x[Lba]\n",\r
791 __FUNCTION__, Dev->BlockIoMedia.LowestAlignedLba,\r
792 Dev->BlockIoMedia.LogicalBlocksPerPhysicalBlock));\r
793 DEBUG ((DEBUG_INFO, "%a: OptimalTransferLengthGranularity=0x%x[Lba]\n",\r
794 __FUNCTION__, Dev->BlockIoMedia.OptimalTransferLengthGranularity));\r
795 }\r
796 return EFI_SUCCESS;\r
797\r
798ReleaseQueue:\r
799 VirtioRingUninit (&Dev->Ring);\r
800\r
801Failed:\r
802 //\r
803 // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device\r
804 // Status. VirtIo access failure here should not mask the original error.\r
805 //\r
806 NextDevStat |= VSTAT_FAILED;\r
807 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, NextDevStat);\r
808\r
809 return Status; // reached only via Failed above\r
810}\r
811\r
812\r
813/**\r
814\r
815 Uninitialize the internals of a virtio-blk device that has been successfully\r
816 set up with VirtioBlkInit().\r
817\r
818 @param[in out] Dev The device to clean up.\r
819\r
820**/\r
821\r
822STATIC\r
823VOID\r
824EFIAPI\r
825VirtioBlkUninit (\r
826 IN OUT VBLK_DEV *Dev\r
827 )\r
828{\r
829 //\r
830 // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When\r
831 // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from\r
832 // the old comms area.\r
833 //\r
834 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
835\r
836 VirtioRingUninit (&Dev->Ring);\r
837\r
838 SetMem (&Dev->BlockIo, sizeof Dev->BlockIo, 0x00);\r
839 SetMem (&Dev->BlockIoMedia, sizeof Dev->BlockIoMedia, 0x00);\r
840}\r
841\r
842\r
843/**\r
844\r
845 Event notification function enqueued by ExitBootServices().\r
846\r
847 @param[in] Event Event whose notification function is being invoked.\r
848\r
849 @param[in] Context Pointer to the VBLK_DEV structure.\r
850\r
851**/\r
852\r
853STATIC\r
854VOID\r
855EFIAPI\r
856VirtioBlkExitBoot (\r
857 IN EFI_EVENT Event,\r
858 IN VOID *Context\r
859 )\r
860{\r
861 VBLK_DEV *Dev;\r
862\r
863 //\r
864 // Reset the device. This causes the hypervisor to forget about the virtio\r
865 // ring.\r
866 //\r
867 // We allocated said ring in EfiBootServicesData type memory, and code\r
868 // executing after ExitBootServices() is permitted to overwrite it.\r
869 //\r
870 Dev = Context;\r
871 Dev->VirtIo->SetDeviceStatus (Dev->VirtIo, 0);\r
872}\r
873\r
874/**\r
875\r
876 After we've pronounced support for a specific device in\r
877 DriverBindingSupported(), we start managing said device (passed in by the\r
878 Driver Exeuction Environment) with the following service.\r
879\r
880 See DriverBindingSupported() for specification references.\r
881\r
882 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
883 incorporating this driver (independently of\r
884 any device).\r
885\r
886 @param[in] DeviceHandle The supported device to drive.\r
887\r
888 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r
889\r
890\r
891 @retval EFI_SUCCESS Driver instance has been created and\r
892 initialized for the virtio-blk device, it\r
893 is now accessibla via EFI_BLOCK_IO_PROTOCOL.\r
894\r
895 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
896\r
897 @return Error codes from the OpenProtocol() boot\r
898 service, the VirtIo protocol, VirtioBlkInit(),\r
899 or the InstallProtocolInterface() boot service.\r
900\r
901**/\r
902\r
903EFI_STATUS\r
904EFIAPI\r
905VirtioBlkDriverBindingStart (\r
906 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
907 IN EFI_HANDLE DeviceHandle,\r
908 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
909 )\r
910{\r
911 VBLK_DEV *Dev;\r
912 EFI_STATUS Status;\r
913\r
914 Dev = (VBLK_DEV *) AllocateZeroPool (sizeof *Dev);\r
915 if (Dev == NULL) {\r
916 return EFI_OUT_OF_RESOURCES;\r
917 }\r
918\r
919 Status = gBS->OpenProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
920 (VOID **)&Dev->VirtIo, This->DriverBindingHandle,\r
921 DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r
922 if (EFI_ERROR (Status)) {\r
923 goto FreeVirtioBlk;\r
924 }\r
925\r
926 //\r
927 // VirtIo access granted, configure virtio-blk device.\r
928 //\r
929 Status = VirtioBlkInit (Dev);\r
930 if (EFI_ERROR (Status)) {\r
931 goto CloseVirtIo;\r
932 }\r
933\r
934 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
935 &VirtioBlkExitBoot, Dev, &Dev->ExitBoot);\r
936 if (EFI_ERROR (Status)) {\r
937 goto UninitDev;\r
938 }\r
939\r
940 //\r
941 // Setup complete, attempt to export the driver instance's BlockIo interface.\r
942 //\r
943 Dev->Signature = VBLK_SIG;\r
944 Status = gBS->InstallProtocolInterface (&DeviceHandle,\r
945 &gEfiBlockIoProtocolGuid, EFI_NATIVE_INTERFACE,\r
946 &Dev->BlockIo);\r
947 if (EFI_ERROR (Status)) {\r
948 goto CloseExitBoot;\r
949 }\r
950\r
951 return EFI_SUCCESS;\r
952\r
953CloseExitBoot:\r
954 gBS->CloseEvent (Dev->ExitBoot);\r
955\r
956UninitDev:\r
957 VirtioBlkUninit (Dev);\r
958\r
959CloseVirtIo:\r
960 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
961 This->DriverBindingHandle, DeviceHandle);\r
962\r
963FreeVirtioBlk:\r
964 FreePool (Dev);\r
965\r
966 return Status;\r
967}\r
968\r
969\r
970/**\r
971\r
972 Stop driving a virtio-blk device and remove its BlockIo interface.\r
973\r
974 This function replays the success path of DriverBindingStart() in reverse.\r
975 The host side virtio-blk device is reset, so that the OS boot loader or the\r
976 OS may reinitialize it.\r
977\r
978 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r
979 incorporating this driver (independently of any\r
980 device).\r
981\r
982 @param[in] DeviceHandle Stop driving this device.\r
983\r
984 @param[in] NumberOfChildren Since this function belongs to a device driver\r
985 only (as opposed to a bus driver), the caller\r
986 environment sets NumberOfChildren to zero, and\r
987 we ignore it.\r
988\r
989 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).\r
990\r
991**/\r
992\r
993EFI_STATUS\r
994EFIAPI\r
995VirtioBlkDriverBindingStop (\r
996 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
997 IN EFI_HANDLE DeviceHandle,\r
998 IN UINTN NumberOfChildren,\r
999 IN EFI_HANDLE *ChildHandleBuffer\r
1000 )\r
1001{\r
1002 EFI_STATUS Status;\r
1003 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
1004 VBLK_DEV *Dev;\r
1005\r
1006 Status = gBS->OpenProtocol (\r
1007 DeviceHandle, // candidate device\r
1008 &gEfiBlockIoProtocolGuid, // retrieve the BlockIo iface\r
1009 (VOID **)&BlockIo, // target pointer\r
1010 This->DriverBindingHandle, // requestor driver identity\r
1011 DeviceHandle, // requesting lookup for dev.\r
1012 EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no ref. added\r
1013 );\r
1014 if (EFI_ERROR (Status)) {\r
1015 return Status;\r
1016 }\r
1017\r
1018 Dev = VIRTIO_BLK_FROM_BLOCK_IO (BlockIo);\r
1019\r
1020 //\r
1021 // Handle Stop() requests for in-use driver instances gracefully.\r
1022 //\r
1023 Status = gBS->UninstallProtocolInterface (DeviceHandle,\r
1024 &gEfiBlockIoProtocolGuid, &Dev->BlockIo);\r
1025 if (EFI_ERROR (Status)) {\r
1026 return Status;\r
1027 }\r
1028\r
1029 gBS->CloseEvent (Dev->ExitBoot);\r
1030\r
1031 VirtioBlkUninit (Dev);\r
1032\r
1033 gBS->CloseProtocol (DeviceHandle, &gVirtioDeviceProtocolGuid,\r
1034 This->DriverBindingHandle, DeviceHandle);\r
1035\r
1036 FreePool (Dev);\r
1037\r
1038 return EFI_SUCCESS;\r
1039}\r
1040\r
1041\r
1042//\r
1043// The static object that groups the Supported() (ie. probe), Start() and\r
1044// Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata\r
1045// C, 10.1 EFI Driver Binding Protocol.\r
1046//\r
1047STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {\r
1048 &VirtioBlkDriverBindingSupported,\r
1049 &VirtioBlkDriverBindingStart,\r
1050 &VirtioBlkDriverBindingStop,\r
1051 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers\r
1052 NULL, // ImageHandle, to be overwritten by\r
1053 // EfiLibInstallDriverBindingComponentName2() in VirtioBlkEntryPoint()\r
1054 NULL // DriverBindingHandle, ditto\r
1055};\r
1056\r
1057\r
1058//\r
1059// The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and\r
1060// EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name\r
1061// in English, for display on standard console devices. This is recommended for\r
1062// UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's\r
1063// Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.\r
1064//\r
1065// Device type names ("Virtio Block Device") are not formatted because the\r
1066// driver supports only that device type. Therefore the driver name suffices\r
1067// for unambiguous identification.\r
1068//\r
1069\r
1070STATIC\r
1071EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {\r
1072 { "eng;en", L"Virtio Block Driver" },\r
1073 { NULL, NULL }\r
1074};\r
1075\r
1076STATIC\r
1077EFI_COMPONENT_NAME_PROTOCOL gComponentName;\r
1078\r
1079EFI_STATUS\r
1080EFIAPI\r
1081VirtioBlkGetDriverName (\r
1082 IN EFI_COMPONENT_NAME_PROTOCOL *This,\r
1083 IN CHAR8 *Language,\r
1084 OUT CHAR16 **DriverName\r
1085 )\r
1086{\r
1087 return LookupUnicodeString2 (\r
1088 Language,\r
1089 This->SupportedLanguages,\r
1090 mDriverNameTable,\r
1091 DriverName,\r
1092 (BOOLEAN)(This == &gComponentName) // Iso639Language\r
1093 );\r
1094}\r
1095\r
1096EFI_STATUS\r
1097EFIAPI\r
1098VirtioBlkGetDeviceName (\r
1099 IN EFI_COMPONENT_NAME_PROTOCOL *This,\r
1100 IN EFI_HANDLE DeviceHandle,\r
1101 IN EFI_HANDLE ChildHandle,\r
1102 IN CHAR8 *Language,\r
1103 OUT CHAR16 **ControllerName\r
1104 )\r
1105{\r
1106 return EFI_UNSUPPORTED;\r
1107}\r
1108\r
1109STATIC\r
1110EFI_COMPONENT_NAME_PROTOCOL gComponentName = {\r
1111 &VirtioBlkGetDriverName,\r
1112 &VirtioBlkGetDeviceName,\r
1113 "eng" // SupportedLanguages, ISO 639-2 language codes\r
1114};\r
1115\r
1116STATIC\r
1117EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {\r
1118 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) &VirtioBlkGetDriverName,\r
1119 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) &VirtioBlkGetDeviceName,\r
1120 "en" // SupportedLanguages, RFC 4646 language codes\r
1121};\r
1122\r
1123\r
1124//\r
1125// Entry point of this driver.\r
1126//\r
1127EFI_STATUS\r
1128EFIAPI\r
1129VirtioBlkEntryPoint (\r
1130 IN EFI_HANDLE ImageHandle,\r
1131 IN EFI_SYSTEM_TABLE *SystemTable\r
1132 )\r
1133{\r
1134 return EfiLibInstallDriverBindingComponentName2 (\r
1135 ImageHandle,\r
1136 SystemTable,\r
1137 &gDriverBinding,\r
1138 ImageHandle,\r
1139 &gComponentName,\r
1140 &gComponentName2\r
1141 );\r
1142}\r
1143\r