3 This driver produces Block I/O Protocol instances for virtio-blk devices.
5 The implementation is basic:
7 - No attach/detach (ie. removable media).
9 - Although the non-blocking interfaces of EFI_BLOCK_IO2_PROTOCOL could be a
10 good match for multiple in-flight virtio-blk requests, we stick to
11 synchronous requests and EFI_BLOCK_IO_PROTOCOL for now.
13 Copyright (C) 2012, Red Hat, Inc.
14 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
16 This program and the accompanying materials are licensed and made available
17 under the terms and conditions of the BSD License which accompanies this
18 distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
22 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 #include <IndustryStandard/VirtioBlk.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/DebugLib.h>
29 #include <Library/MemoryAllocationLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiLib.h>
32 #include <Library/VirtioLib.h>
34 #include "VirtioBlk.h"
38 Convenience macros to read and write region 0 IO space elements of the
39 virtio-blk device, for configuration purposes.
41 The following macros make it possible to specify only the "core parameters"
42 for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()
43 returns, the transaction will have been completed.
45 @param[in] Dev Pointer to the VBLK_DEV structure whose VirtIo space
46 we're accessing. Dev->VirtIo must be valid.
48 @param[in] Field A field name from VBLK_HDR, identifying the virtio-blk
49 configuration item to access.
51 @param[in] Value (VIRTIO_CFG_WRITE() only.) The value to write to the
52 selected configuration item.
54 @param[out] Pointer (VIRTIO_CFG_READ() only.) The object to receive the
55 value read from the configuration item. Its type must be
56 one of UINT8, UINT16, UINT32, UINT64.
59 @return Status code returned by VirtioWriteDevice() / VirtioReadDevice().
63 #define VIRTIO_CFG_WRITE(Dev, Field, Value) (VirtioWriteDevice ( \
65 OFFSET_OF_VBLK (Field), \
66 SIZE_OF_VBLK (Field), \
70 #define VIRTIO_CFG_READ(Dev, Field, Pointer) (VirtioReadDevice ( \
72 OFFSET_OF_VBLK (Field), \
73 SIZE_OF_VBLK (Field), \
80 // UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol
81 // Driver Writer's Guide for UEFI 2.3.1 v1.01,
82 // 24.2 Block I/O Protocol Implementations
87 IN EFI_BLOCK_IO_PROTOCOL
*This
,
88 IN BOOLEAN ExtendedVerification
92 // If we managed to initialize and install the driver, then the device is
100 Verify correctness of the read/write (not flush) request submitted to the
101 EFI_BLOCK_IO_PROTOCOL instance.
103 This function provides most verification steps described in:
105 UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
107 - EFI_BLOCK_IO_PROTOCOL.ReadBlocks()
108 - EFI_BLOCK_IO_PROTOCOL.WriteBlocks()
110 Driver Writer's Guide for UEFI 2.3.1 v1.01,
111 - 24.2.2. ReadBlocks() and ReadBlocksEx() Implementation
112 - 24.2.3 WriteBlocks() and WriteBlockEx() Implementation
114 Request sizes are limited to 1 GB (checked). This is not a practical
115 limitation, just conformance to virtio-0.9.5, 2.3.2 Descriptor Table: "no
116 descriptor chain may be more than 2^32 bytes long in total".
118 Some Media characteristics are hardcoded in VirtioBlkInit() below (like
119 non-removable media, no restriction on buffer alignment etc); we rely on
120 those here without explicit mention.
122 @param[in] Media The EFI_BLOCK_IO_MEDIA characteristics for
123 this driver instance, extracted from the
124 underlying virtio-blk device at initialization
125 time. We validate the request against this set
129 @param[in] Lba Logical Block Address: number of logical
130 blocks to skip from the beginning of the
133 @param[in] PositiveBufferSize Size of buffer to transfer, in bytes. The
134 caller is responsible to ensure this parameter
137 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to
141 @@return Validation result to be forwarded outwards by
142 ReadBlocks() and WriteBlocks, as required by
149 VerifyReadWriteRequest (
150 IN EFI_BLOCK_IO_MEDIA
*Media
,
152 IN UINTN PositiveBufferSize
,
153 IN BOOLEAN RequestIsWrite
158 ASSERT (PositiveBufferSize
> 0);
160 if (PositiveBufferSize
> SIZE_1GB
||
161 PositiveBufferSize
% Media
->BlockSize
> 0) {
162 return EFI_BAD_BUFFER_SIZE
;
164 BlockCount
= PositiveBufferSize
/ Media
->BlockSize
;
167 // Avoid unsigned wraparound on either side in the second comparison.
169 if (Lba
> Media
->LastBlock
|| BlockCount
- 1 > Media
->LastBlock
- Lba
) {
170 return EFI_INVALID_PARAMETER
;
173 if (RequestIsWrite
&& Media
->ReadOnly
) {
174 return EFI_WRITE_PROTECTED
;
185 Format a read / write / flush request as three consecutive virtio
186 descriptors, push them to the host, and poll for the response.
188 This is the main workhorse function. Two use cases are supported, read/write
189 and flush. The function may only be called after the request parameters have
191 - specific checks in ReadBlocks() / WriteBlocks() / FlushBlocks(), and
192 - VerifyReadWriteRequest() (for read/write only).
194 Parameters handled commonly:
196 @param[in] Dev The virtio-blk device the request is targeted
201 @param[in] Lba Must be zero.
203 @param[in] BufferSize Must be zero.
205 @param[in out] Buffer Ignored by the function.
207 @param[in] RequestIsWrite Must be TRUE.
211 @param[in] Lba Logical Block Address: number of logical blocks
212 to skip from the beginning of the device.
214 @param[in] BufferSize Size of buffer to transfer, in bytes. The caller
215 is responsible to ensure this parameter is
218 @param[in out] Buffer The guest side area to read data from the device
219 into, or write data to the device from.
221 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to
224 Return values are common to both use cases, and are appropriate to be
225 forwarded by the EFI_BLOCK_IO_PROTOCOL functions (ReadBlocks(),
226 WriteBlocks(), FlushBlocks()).
229 @retval EFI_SUCCESS Transfer complete.
231 @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or
232 unable to parse host response, or host response
233 is not VIRTIO_BLK_S_OK.
244 IN OUT
volatile VOID
*Buffer
,
245 IN BOOLEAN RequestIsWrite
249 volatile VIRTIO_BLK_REQ Request
;
250 volatile UINT8 HostStatus
;
251 DESC_INDICES Indices
;
253 BlockSize
= Dev
->BlockIoMedia
.BlockSize
;
256 // ensured by VirtioBlkInit()
258 ASSERT (BlockSize
> 0);
259 ASSERT (BlockSize
% 512 == 0);
262 // ensured by contract above, plus VerifyReadWriteRequest()
264 ASSERT (BufferSize
% BlockSize
== 0);
267 // Prepare virtio-blk request header, setting zero size for flush.
268 // IO Priority is homogeneously 0.
270 Request
.Type
= RequestIsWrite
?
271 (BufferSize
== 0 ? VIRTIO_BLK_T_FLUSH
: VIRTIO_BLK_T_OUT
) :
274 Request
.Sector
= MultU64x32(Lba
, BlockSize
/ 512);
276 VirtioPrepare (&Dev
->Ring
, &Indices
);
279 // preset a host status for ourselves that we do not accept as success
281 HostStatus
= VIRTIO_BLK_S_IOERR
;
284 // ensured by VirtioBlkInit() -- this predicate, in combination with the
285 // lock-step progress, ensures we don't have to track free descriptors.
287 ASSERT (Dev
->Ring
.QueueSize
>= 3);
290 // virtio-blk header in first desc
292 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) &Request
, sizeof Request
,
293 VRING_DESC_F_NEXT
, &Indices
);
296 // data buffer for read/write in second desc
298 if (BufferSize
> 0) {
300 // From virtio-0.9.5, 2.3.2 Descriptor Table:
301 // "no descriptor chain may be more than 2^32 bytes long in total".
303 // The predicate is ensured by the call contract above (for flush), or
304 // VerifyReadWriteRequest() (for read/write). It also implies that
305 // converting BufferSize to UINT32 will not truncate it.
307 ASSERT (BufferSize
<= SIZE_1GB
);
310 // VRING_DESC_F_WRITE is interpreted from the host's point of view.
312 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) Buffer
, (UINT32
) BufferSize
,
313 VRING_DESC_F_NEXT
| (RequestIsWrite
? 0 : VRING_DESC_F_WRITE
),
318 // host status in last (second or third) desc
320 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) &HostStatus
, sizeof HostStatus
,
321 VRING_DESC_F_WRITE
, &Indices
);
324 // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D).
326 if (VirtioFlush (Dev
->VirtIo
, 0, &Dev
->Ring
, &Indices
) == EFI_SUCCESS
&&
327 HostStatus
== VIRTIO_BLK_S_OK
) {
331 return EFI_DEVICE_ERROR
;
337 ReadBlocks() operation for virtio-blk.
340 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
341 Protocol, EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
342 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.2. ReadBlocks() and
343 ReadBlocksEx() Implementation.
345 Parameter checks and conformant return values are implemented in
346 VerifyReadWriteRequest() and SynchronousRequest().
348 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,
355 VirtioBlkReadBlocks (
356 IN EFI_BLOCK_IO_PROTOCOL
*This
,
366 if (BufferSize
== 0) {
370 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
371 Status
= VerifyReadWriteRequest (
375 FALSE
// RequestIsWrite
377 if (EFI_ERROR (Status
)) {
381 return SynchronousRequest (
386 FALSE
// RequestIsWrite
392 WriteBlocks() operation for virtio-blk.
395 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
396 Protocol, EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
397 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.3 WriteBlocks() and
398 WriteBlockEx() Implementation.
400 Parameter checks and conformant return values are implemented in
401 VerifyReadWriteRequest() and SynchronousRequest().
403 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,
410 VirtioBlkWriteBlocks (
411 IN EFI_BLOCK_IO_PROTOCOL
*This
,
421 if (BufferSize
== 0) {
425 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
426 Status
= VerifyReadWriteRequest (
430 TRUE
// RequestIsWrite
432 if (EFI_ERROR (Status
)) {
436 return SynchronousRequest (
441 TRUE
// RequestIsWrite
448 FlushBlocks() operation for virtio-blk.
451 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
452 Protocol, EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
453 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.4 FlushBlocks() and
454 FlushBlocksEx() Implementation.
456 If the underlying virtio-blk device doesn't support flushing (ie.
457 write-caching), then this function should not be called by higher layers,
458 according to EFI_BLOCK_IO_MEDIA characteristics set in VirtioBlkInit().
459 Should they do nonetheless, we do nothing, successfully.
465 VirtioBlkFlushBlocks (
466 IN EFI_BLOCK_IO_PROTOCOL
*This
471 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
472 return Dev
->BlockIoMedia
.WriteCaching
?
478 TRUE
// RequestIsWrite
486 Device probe function for this driver.
488 The DXE core calls this function for any given device in order to see if the
489 driver can drive the device.
491 Specs relevant in the general sense:
493 - UEFI Spec 2.3.1 + Errata C:
494 - 6.3 Protocol Handler Services -- for accessing the underlying device
495 - 10.1 EFI Driver Binding Protocol -- for exporting ourselves
497 - Driver Writer's Guide for UEFI 2.3.1 v1.01:
498 - 5.1.3.4 OpenProtocol() and CloseProtocol() -- for accessing the
500 - 9 Driver Binding Protocol -- for exporting ourselves
502 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
503 incorporating this driver (independently of
506 @param[in] DeviceHandle The device to probe.
508 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
511 @retval EFI_SUCCESS The driver supports the device being probed.
513 @retval EFI_UNSUPPORTED Based on virtio-blk discovery, we do not support
516 @return Error codes from the OpenProtocol() boot service or
523 VirtioBlkDriverBindingSupported (
524 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
525 IN EFI_HANDLE DeviceHandle
,
526 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
530 VIRTIO_DEVICE_PROTOCOL
*VirtIo
;
533 // Attempt to open the device with the VirtIo set of interfaces. On success,
534 // the protocol is "instantiated" for the VirtIo device. Covers duplicate open
535 // attempts (EFI_ALREADY_STARTED).
537 Status
= gBS
->OpenProtocol (
538 DeviceHandle
, // candidate device
539 &gVirtioDeviceProtocolGuid
, // for generic VirtIo access
540 (VOID
**)&VirtIo
, // handle to instantiate
541 This
->DriverBindingHandle
, // requestor driver identity
542 DeviceHandle
, // ControllerHandle, according to
543 // the UEFI Driver Model
544 EFI_OPEN_PROTOCOL_BY_DRIVER
// get exclusive VirtIo access to
545 // the device; to be released
547 if (EFI_ERROR (Status
)) {
551 if (VirtIo
->SubSystemDeviceId
!= VIRTIO_SUBSYSTEM_BLOCK_DEVICE
) {
552 Status
= EFI_UNSUPPORTED
;
556 // We needed VirtIo access only transitorily, to see whether we support the
559 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
560 This
->DriverBindingHandle
, DeviceHandle
);
567 Set up all BlockIo and virtio-blk aspects of this driver for the specified
570 @param[in out] Dev The driver instance to configure. The caller is
571 responsible for Dev->VirtIo's validity (ie. working IO
572 access to the underlying virtio-blk device).
574 @retval EFI_SUCCESS Setup complete.
576 @retval EFI_UNSUPPORTED The driver is unable to work with the virtio ring or
577 virtio-blk attributes the host provides.
579 @return Error codes from VirtioRingInit() or
580 VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE().
600 // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.
602 NextDevStat
= 0; // step 1 -- reset device
603 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
604 if (EFI_ERROR (Status
)) {
608 NextDevStat
|= VSTAT_ACK
; // step 2 -- acknowledge device presence
609 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
610 if (EFI_ERROR (Status
)) {
614 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
615 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
616 if (EFI_ERROR (Status
)) {
621 // Set Page Size - MMIO VirtIo Specific
623 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
624 if (EFI_ERROR (Status
)) {
629 // step 4a -- retrieve and validate features
631 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
632 if (EFI_ERROR (Status
)) {
636 Status
= VIRTIO_CFG_READ (Dev
, Capacity
, &NumSectors
);
637 if (EFI_ERROR (Status
)) {
640 if (NumSectors
== 0) {
641 Status
= EFI_UNSUPPORTED
;
645 if (Features
& VIRTIO_BLK_F_BLK_SIZE
) {
646 Status
= VIRTIO_CFG_READ (Dev
, BlkSize
, &BlockSize
);
647 if (EFI_ERROR (Status
)) {
650 if (BlockSize
== 0 || BlockSize
% 512 != 0 ||
651 ModU64x32 (NumSectors
, BlockSize
/ 512) != 0) {
653 // We can only handle a logical block consisting of whole sectors,
654 // and only a disk composed of whole logical blocks.
656 Status
= EFI_UNSUPPORTED
;
665 // step 4b -- allocate virtqueue
667 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, 0);
668 if (EFI_ERROR (Status
)) {
671 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
672 if (EFI_ERROR (Status
)) {
675 if (QueueSize
< 3) { // SynchronousRequest() uses at most three descriptors
676 Status
= EFI_UNSUPPORTED
;
680 Status
= VirtioRingInit (QueueSize
, &Dev
->Ring
);
681 if (EFI_ERROR (Status
)) {
686 // Additional steps for MMIO: align the queue appropriately, and set the
687 // size. If anything fails from here on, we must release the ring resources.
689 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
690 if (EFI_ERROR (Status
)) {
694 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
695 if (EFI_ERROR (Status
)) {
700 // step 4c -- Report GPFN (guest-physical frame number) of queue.
702 Status
= Dev
->VirtIo
->SetQueueAddress (Dev
->VirtIo
,
703 (UINT32
)(UINTN
) Dev
->Ring
.Base
>> EFI_PAGE_SHIFT
);
704 if (EFI_ERROR (Status
)) {
710 // step 5 -- Report understood features. There are no virtio-blk specific
711 // features to negotiate in virtio-0.9.5, plus we do not want any of the
712 // device-independent (known or unknown) VIRTIO_F_* capabilities (see
715 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, 0);
716 if (EFI_ERROR (Status
)) {
721 // step 6 -- initialization complete
723 NextDevStat
|= VSTAT_DRIVER_OK
;
724 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
725 if (EFI_ERROR (Status
)) {
730 // Populate the exported interface's attributes; see UEFI spec v2.3.1 +
731 // Errata C, 12.8 EFI Block I/O Protocol. We stick to the lowest possible
732 // EFI_BLOCK_IO_PROTOCOL revision for now.
734 Dev
->BlockIo
.Revision
= 0;
735 Dev
->BlockIo
.Media
= &Dev
->BlockIoMedia
;
736 Dev
->BlockIo
.Reset
= &VirtioBlkReset
;
737 Dev
->BlockIo
.ReadBlocks
= &VirtioBlkReadBlocks
;
738 Dev
->BlockIo
.WriteBlocks
= &VirtioBlkWriteBlocks
;
739 Dev
->BlockIo
.FlushBlocks
= &VirtioBlkFlushBlocks
;
740 Dev
->BlockIoMedia
.MediaId
= 0;
741 Dev
->BlockIoMedia
.RemovableMedia
= FALSE
;
742 Dev
->BlockIoMedia
.MediaPresent
= TRUE
;
743 Dev
->BlockIoMedia
.LogicalPartition
= FALSE
;
744 Dev
->BlockIoMedia
.ReadOnly
= !!(Features
& VIRTIO_BLK_F_RO
);
745 Dev
->BlockIoMedia
.WriteCaching
= !!(Features
& VIRTIO_BLK_F_FLUSH
);
746 Dev
->BlockIoMedia
.BlockSize
= BlockSize
;
747 Dev
->BlockIoMedia
.IoAlign
= 0;
748 Dev
->BlockIoMedia
.LastBlock
= DivU64x32 (NumSectors
,
749 BlockSize
/ 512) - 1;
753 VirtioRingUninit (&Dev
->Ring
);
757 // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device
758 // Status. VirtIo access failure here should not mask the original error.
760 NextDevStat
|= VSTAT_FAILED
;
761 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
763 return Status
; // reached only via Failed above
769 Uninitialize the internals of a virtio-blk device that has been successfully
770 set up with VirtioBlkInit().
772 @param[in out] Dev The device to clean up.
784 // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When
785 // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from
786 // the old comms area.
788 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
790 VirtioRingUninit (&Dev
->Ring
);
792 SetMem (&Dev
->BlockIo
, sizeof Dev
->BlockIo
, 0x00);
793 SetMem (&Dev
->BlockIoMedia
, sizeof Dev
->BlockIoMedia
, 0x00);
799 After we've pronounced support for a specific device in
800 DriverBindingSupported(), we start managing said device (passed in by the
801 Driver Exeuction Environment) with the following service.
803 See DriverBindingSupported() for specification references.
805 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
806 incorporating this driver (independently of
809 @param[in] DeviceHandle The supported device to drive.
811 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
814 @retval EFI_SUCCESS Driver instance has been created and
815 initialized for the virtio-blk device, it
816 is now accessibla via EFI_BLOCK_IO_PROTOCOL.
818 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
820 @return Error codes from the OpenProtocol() boot
821 service, the VirtIo protocol, VirtioBlkInit(),
822 or the InstallProtocolInterface() boot service.
828 VirtioBlkDriverBindingStart (
829 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
830 IN EFI_HANDLE DeviceHandle
,
831 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
837 Dev
= (VBLK_DEV
*) AllocateZeroPool (sizeof *Dev
);
839 return EFI_OUT_OF_RESOURCES
;
842 Status
= gBS
->OpenProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
843 (VOID
**)&Dev
->VirtIo
, This
->DriverBindingHandle
,
844 DeviceHandle
, EFI_OPEN_PROTOCOL_BY_DRIVER
);
845 if (EFI_ERROR (Status
)) {
850 // VirtIo access granted, configure virtio-blk device.
852 Status
= VirtioBlkInit (Dev
);
853 if (EFI_ERROR (Status
)) {
858 // Setup complete, attempt to export the driver instance's BlockIo interface.
860 Dev
->Signature
= VBLK_SIG
;
861 Status
= gBS
->InstallProtocolInterface (&DeviceHandle
,
862 &gEfiBlockIoProtocolGuid
, EFI_NATIVE_INTERFACE
,
864 if (EFI_ERROR (Status
)) {
871 VirtioBlkUninit (Dev
);
874 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
875 This
->DriverBindingHandle
, DeviceHandle
);
886 Stop driving a virtio-blk device and remove its BlockIo interface.
888 This function replays the success path of DriverBindingStart() in reverse.
889 The host side virtio-blk device is reset, so that the OS boot loader or the
890 OS may reinitialize it.
892 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
893 incorporating this driver (independently of any
896 @param[in] DeviceHandle Stop driving this device.
898 @param[in] NumberOfChildren Since this function belongs to a device driver
899 only (as opposed to a bus driver), the caller
900 environment sets NumberOfChildren to zero, and
903 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
909 VirtioBlkDriverBindingStop (
910 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
911 IN EFI_HANDLE DeviceHandle
,
912 IN UINTN NumberOfChildren
,
913 IN EFI_HANDLE
*ChildHandleBuffer
917 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
920 Status
= gBS
->OpenProtocol (
921 DeviceHandle
, // candidate device
922 &gEfiBlockIoProtocolGuid
, // retrieve the BlockIo iface
923 (VOID
**)&BlockIo
, // target pointer
924 This
->DriverBindingHandle
, // requestor driver identity
925 DeviceHandle
, // requesting lookup for dev.
926 EFI_OPEN_PROTOCOL_GET_PROTOCOL
// lookup only, no ref. added
928 if (EFI_ERROR (Status
)) {
932 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (BlockIo
);
935 // Handle Stop() requests for in-use driver instances gracefully.
937 Status
= gBS
->UninstallProtocolInterface (DeviceHandle
,
938 &gEfiBlockIoProtocolGuid
, &Dev
->BlockIo
);
939 if (EFI_ERROR (Status
)) {
943 VirtioBlkUninit (Dev
);
945 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
946 This
->DriverBindingHandle
, DeviceHandle
);
955 // The static object that groups the Supported() (ie. probe), Start() and
956 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
957 // C, 10.1 EFI Driver Binding Protocol.
959 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding
= {
960 &VirtioBlkDriverBindingSupported
,
961 &VirtioBlkDriverBindingStart
,
962 &VirtioBlkDriverBindingStop
,
963 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
964 NULL
, // ImageHandle, to be overwritten by
965 // EfiLibInstallDriverBindingComponentName2() in VirtioBlkEntryPoint()
966 NULL
// DriverBindingHandle, ditto
971 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
972 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
973 // in English, for display on standard console devices. This is recommended for
974 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
975 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
977 // Device type names ("Virtio Block Device") are not formatted because the
978 // driver supports only that device type. Therefore the driver name suffices
979 // for unambiguous identification.
983 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
984 { "eng;en", L
"Virtio Block Driver" },
989 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
993 VirtioBlkGetDriverName (
994 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
996 OUT CHAR16
**DriverName
999 return LookupUnicodeString2 (
1001 This
->SupportedLanguages
,
1004 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
1010 VirtioBlkGetDeviceName (
1011 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
1012 IN EFI_HANDLE DeviceHandle
,
1013 IN EFI_HANDLE ChildHandle
,
1015 OUT CHAR16
**ControllerName
1018 return EFI_UNSUPPORTED
;
1022 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
1023 &VirtioBlkGetDriverName
,
1024 &VirtioBlkGetDeviceName
,
1025 "eng" // SupportedLanguages, ISO 639-2 language codes
1029 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2
= {
1030 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME
) &VirtioBlkGetDriverName
,
1031 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME
) &VirtioBlkGetDeviceName
,
1032 "en" // SupportedLanguages, RFC 4646 language codes
1037 // Entry point of this driver.
1041 VirtioBlkEntryPoint (
1042 IN EFI_HANDLE ImageHandle
,
1043 IN EFI_SYSTEM_TABLE
*SystemTable
1046 return EfiLibInstallDriverBindingComponentName2 (