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 - 2018, Intel Corporation. All rights reserved.<BR>
15 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
17 This program and the accompanying materials are licensed and made available
18 under the terms and conditions of the BSD License which accompanies this
19 distribution. The full text of the license may be found at
20 http://opensource.org/licenses/bsd-license.php
22 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
23 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <IndustryStandard/VirtioBlk.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/MemoryAllocationLib.h>
31 #include <Library/UefiBootServicesTableLib.h>
32 #include <Library/UefiLib.h>
33 #include <Library/VirtioLib.h>
35 #include "VirtioBlk.h"
39 Convenience macros to read and write region 0 IO space elements of the
40 virtio-blk device, for configuration purposes.
42 The following macros make it possible to specify only the "core parameters"
43 for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()
44 returns, the transaction will have been completed.
46 @param[in] Dev Pointer to the VBLK_DEV structure whose VirtIo space
47 we're accessing. Dev->VirtIo must be valid.
49 @param[in] Field A field name from VBLK_HDR, identifying the virtio-blk
50 configuration item to access.
52 @param[in] Value (VIRTIO_CFG_WRITE() only.) The value to write to the
53 selected configuration item.
55 @param[out] Pointer (VIRTIO_CFG_READ() only.) The object to receive the
56 value read from the configuration item. Its type must be
57 one of UINT8, UINT16, UINT32, UINT64.
60 @return Status code returned by Virtio->WriteDevice() /
65 #define VIRTIO_CFG_WRITE(Dev, Field, Value) ((Dev)->VirtIo->WriteDevice ( \
67 OFFSET_OF_VBLK (Field), \
68 SIZE_OF_VBLK (Field), \
72 #define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice ( \
74 OFFSET_OF_VBLK (Field), \
75 SIZE_OF_VBLK (Field), \
82 // UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol
83 // Driver Writer's Guide for UEFI 2.3.1 v1.01,
84 // 24.2 Block I/O Protocol Implementations
89 IN EFI_BLOCK_IO_PROTOCOL
*This
,
90 IN BOOLEAN ExtendedVerification
94 // If we managed to initialize and install the driver, then the device is
102 Verify correctness of the read/write (not flush) request submitted to the
103 EFI_BLOCK_IO_PROTOCOL instance.
105 This function provides most verification steps described in:
107 UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
109 - EFI_BLOCK_IO_PROTOCOL.ReadBlocks()
110 - EFI_BLOCK_IO_PROTOCOL.WriteBlocks()
112 Driver Writer's Guide for UEFI 2.3.1 v1.01,
113 - 24.2.2. ReadBlocks() and ReadBlocksEx() Implementation
114 - 24.2.3 WriteBlocks() and WriteBlockEx() Implementation
116 Request sizes are limited to 1 GB (checked). This is not a practical
117 limitation, just conformance to virtio-0.9.5, 2.3.2 Descriptor Table: "no
118 descriptor chain may be more than 2^32 bytes long in total".
120 Some Media characteristics are hardcoded in VirtioBlkInit() below (like
121 non-removable media, no restriction on buffer alignment etc); we rely on
122 those here without explicit mention.
124 @param[in] Media The EFI_BLOCK_IO_MEDIA characteristics for
125 this driver instance, extracted from the
126 underlying virtio-blk device at initialization
127 time. We validate the request against this set
131 @param[in] Lba Logical Block Address: number of logical
132 blocks to skip from the beginning of the
135 @param[in] PositiveBufferSize Size of buffer to transfer, in bytes. The
136 caller is responsible to ensure this parameter
139 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to
143 @@return Validation result to be forwarded outwards by
144 ReadBlocks() and WriteBlocks, as required by
151 VerifyReadWriteRequest (
152 IN EFI_BLOCK_IO_MEDIA
*Media
,
154 IN UINTN PositiveBufferSize
,
155 IN BOOLEAN RequestIsWrite
160 ASSERT (PositiveBufferSize
> 0);
162 if (PositiveBufferSize
> SIZE_1GB
||
163 PositiveBufferSize
% Media
->BlockSize
> 0) {
164 return EFI_BAD_BUFFER_SIZE
;
166 BlockCount
= PositiveBufferSize
/ Media
->BlockSize
;
169 // Avoid unsigned wraparound on either side in the second comparison.
171 if (Lba
> Media
->LastBlock
|| BlockCount
- 1 > Media
->LastBlock
- Lba
) {
172 return EFI_INVALID_PARAMETER
;
175 if (RequestIsWrite
&& Media
->ReadOnly
) {
176 return EFI_WRITE_PROTECTED
;
187 Format a read / write / flush request as three consecutive virtio
188 descriptors, push them to the host, and poll for the response.
190 This is the main workhorse function. Two use cases are supported, read/write
191 and flush. The function may only be called after the request parameters have
193 - specific checks in ReadBlocks() / WriteBlocks() / FlushBlocks(), and
194 - VerifyReadWriteRequest() (for read/write only).
196 Parameters handled commonly:
198 @param[in] Dev The virtio-blk device the request is targeted
203 @param[in] Lba Must be zero.
205 @param[in] BufferSize Must be zero.
207 @param[in out] Buffer Ignored by the function.
209 @param[in] RequestIsWrite Must be TRUE.
213 @param[in] Lba Logical Block Address: number of logical blocks
214 to skip from the beginning of the device.
216 @param[in] BufferSize Size of buffer to transfer, in bytes. The caller
217 is responsible to ensure this parameter is
220 @param[in out] Buffer The guest side area to read data from the device
221 into, or write data to the device from.
223 @param[in] RequestIsWrite TRUE iff data transfer goes from guest to
226 Return values are common to both use cases, and are appropriate to be
227 forwarded by the EFI_BLOCK_IO_PROTOCOL functions (ReadBlocks(),
228 WriteBlocks(), FlushBlocks()).
231 @retval EFI_SUCCESS Transfer complete.
233 @retval EFI_DEVICE_ERROR Failed to notify host side via VirtIo write, or
234 unable to parse host response, or host response
235 is not VIRTIO_BLK_S_OK or failed to map Buffer
236 for a bus master operation.
247 IN OUT
volatile VOID
*Buffer
,
248 IN BOOLEAN RequestIsWrite
252 volatile VIRTIO_BLK_REQ Request
;
253 volatile UINT8
*HostStatus
;
254 VOID
*HostStatusBuffer
;
255 DESC_INDICES Indices
;
256 VOID
*RequestMapping
;
259 EFI_PHYSICAL_ADDRESS BufferDeviceAddress
;
260 EFI_PHYSICAL_ADDRESS HostStatusDeviceAddress
;
261 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
263 EFI_STATUS UnmapStatus
;
265 BlockSize
= Dev
->BlockIoMedia
.BlockSize
;
268 // Set BufferMapping and BufferDeviceAddress to suppress incorrect
269 // compiler/analyzer warnings.
271 BufferMapping
= NULL
;
272 BufferDeviceAddress
= 0;
275 // ensured by VirtioBlkInit()
277 ASSERT (BlockSize
> 0);
278 ASSERT (BlockSize
% 512 == 0);
281 // ensured by contract above, plus VerifyReadWriteRequest()
283 ASSERT (BufferSize
% BlockSize
== 0);
286 // Prepare virtio-blk request header, setting zero size for flush.
287 // IO Priority is homogeneously 0.
289 Request
.Type
= RequestIsWrite
?
290 (BufferSize
== 0 ? VIRTIO_BLK_T_FLUSH
: VIRTIO_BLK_T_OUT
) :
293 Request
.Sector
= MultU64x32(Lba
, BlockSize
/ 512);
296 // Host status is bi-directional (we preset with a value and expect the
297 // device to update it). Allocate a host status buffer which can be mapped
298 // to access equally by both processor and the device.
300 Status
= Dev
->VirtIo
->AllocateSharedPages (
302 EFI_SIZE_TO_PAGES (sizeof *HostStatus
),
305 if (EFI_ERROR (Status
)) {
306 return EFI_DEVICE_ERROR
;
309 HostStatus
= HostStatusBuffer
;
312 // Map virtio-blk request header (must be done after request header is
315 Status
= VirtioMapAllBytesInSharedBuffer (
317 VirtioOperationBusMasterRead
,
320 &RequestDeviceAddress
,
323 if (EFI_ERROR (Status
)) {
324 Status
= EFI_DEVICE_ERROR
;
325 goto FreeHostStatusBuffer
;
331 if (BufferSize
> 0) {
332 Status
= VirtioMapAllBytesInSharedBuffer (
335 VirtioOperationBusMasterRead
:
336 VirtioOperationBusMasterWrite
),
339 &BufferDeviceAddress
,
342 if (EFI_ERROR (Status
)) {
343 Status
= EFI_DEVICE_ERROR
;
344 goto UnmapRequestBuffer
;
349 // preset a host status for ourselves that we do not accept as success
351 *HostStatus
= VIRTIO_BLK_S_IOERR
;
354 // Map the Status Buffer with VirtioOperationBusMasterCommonBuffer so that
355 // both processor and device can access it.
357 Status
= VirtioMapAllBytesInSharedBuffer (
359 VirtioOperationBusMasterCommonBuffer
,
362 &HostStatusDeviceAddress
,
365 if (EFI_ERROR (Status
)) {
366 Status
= EFI_DEVICE_ERROR
;
367 goto UnmapDataBuffer
;
370 VirtioPrepare (&Dev
->Ring
, &Indices
);
373 // ensured by VirtioBlkInit() -- this predicate, in combination with the
374 // lock-step progress, ensures we don't have to track free descriptors.
376 ASSERT (Dev
->Ring
.QueueSize
>= 3);
379 // virtio-blk header in first desc
383 RequestDeviceAddress
,
390 // data buffer for read/write in second desc
392 if (BufferSize
> 0) {
394 // From virtio-0.9.5, 2.3.2 Descriptor Table:
395 // "no descriptor chain may be more than 2^32 bytes long in total".
397 // The predicate is ensured by the call contract above (for flush), or
398 // VerifyReadWriteRequest() (for read/write). It also implies that
399 // converting BufferSize to UINT32 will not truncate it.
401 ASSERT (BufferSize
<= SIZE_1GB
);
404 // VRING_DESC_F_WRITE is interpreted from the host's point of view.
410 VRING_DESC_F_NEXT
| (RequestIsWrite
? 0 : VRING_DESC_F_WRITE
),
416 // host status in last (second or third) desc
420 HostStatusDeviceAddress
,
427 // virtio-blk's only virtqueue is #0, called "requestq" (see Appendix D).
429 if (VirtioFlush (Dev
->VirtIo
, 0, &Dev
->Ring
, &Indices
,
430 NULL
) == EFI_SUCCESS
&&
431 *HostStatus
== VIRTIO_BLK_S_OK
) {
432 Status
= EFI_SUCCESS
;
434 Status
= EFI_DEVICE_ERROR
;
437 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, StatusMapping
);
440 if (BufferSize
> 0) {
441 UnmapStatus
= Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, BufferMapping
);
442 if (EFI_ERROR (UnmapStatus
) && !RequestIsWrite
&& !EFI_ERROR (Status
)) {
444 // Data from the bus master may not reach the caller; fail the request.
446 Status
= EFI_DEVICE_ERROR
;
451 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, RequestMapping
);
453 FreeHostStatusBuffer
:
454 Dev
->VirtIo
->FreeSharedPages (
456 EFI_SIZE_TO_PAGES (sizeof *HostStatus
),
466 ReadBlocks() operation for virtio-blk.
469 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
470 Protocol, EFI_BLOCK_IO_PROTOCOL.ReadBlocks().
471 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.2. ReadBlocks() and
472 ReadBlocksEx() Implementation.
474 Parameter checks and conformant return values are implemented in
475 VerifyReadWriteRequest() and SynchronousRequest().
477 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,
484 VirtioBlkReadBlocks (
485 IN EFI_BLOCK_IO_PROTOCOL
*This
,
495 if (BufferSize
== 0) {
499 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
500 Status
= VerifyReadWriteRequest (
504 FALSE
// RequestIsWrite
506 if (EFI_ERROR (Status
)) {
510 return SynchronousRequest (
515 FALSE
// RequestIsWrite
521 WriteBlocks() operation for virtio-blk.
524 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
525 Protocol, EFI_BLOCK_IO_PROTOCOL.WriteBlocks().
526 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.3 WriteBlocks() and
527 WriteBlockEx() Implementation.
529 Parameter checks and conformant return values are implemented in
530 VerifyReadWriteRequest() and SynchronousRequest().
532 A zero BufferSize doesn't seem to be prohibited, so do nothing in that case,
539 VirtioBlkWriteBlocks (
540 IN EFI_BLOCK_IO_PROTOCOL
*This
,
550 if (BufferSize
== 0) {
554 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
555 Status
= VerifyReadWriteRequest (
559 TRUE
// RequestIsWrite
561 if (EFI_ERROR (Status
)) {
565 return SynchronousRequest (
570 TRUE
// RequestIsWrite
577 FlushBlocks() operation for virtio-blk.
580 - UEFI Spec 2.3.1 + Errata C, 12.8 EFI Block I/O Protocol, 12.8 EFI Block I/O
581 Protocol, EFI_BLOCK_IO_PROTOCOL.FlushBlocks().
582 - Driver Writer's Guide for UEFI 2.3.1 v1.01, 24.2.4 FlushBlocks() and
583 FlushBlocksEx() Implementation.
585 If the underlying virtio-blk device doesn't support flushing (ie.
586 write-caching), then this function should not be called by higher layers,
587 according to EFI_BLOCK_IO_MEDIA characteristics set in VirtioBlkInit().
588 Should they do nonetheless, we do nothing, successfully.
594 VirtioBlkFlushBlocks (
595 IN EFI_BLOCK_IO_PROTOCOL
*This
600 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (This
);
601 return Dev
->BlockIoMedia
.WriteCaching
?
607 TRUE
// RequestIsWrite
615 Device probe function for this driver.
617 The DXE core calls this function for any given device in order to see if the
618 driver can drive the device.
620 Specs relevant in the general sense:
622 - UEFI Spec 2.3.1 + Errata C:
623 - 6.3 Protocol Handler Services -- for accessing the underlying device
624 - 10.1 EFI Driver Binding Protocol -- for exporting ourselves
626 - Driver Writer's Guide for UEFI 2.3.1 v1.01:
627 - 5.1.3.4 OpenProtocol() and CloseProtocol() -- for accessing the
629 - 9 Driver Binding Protocol -- for exporting ourselves
631 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
632 incorporating this driver (independently of
635 @param[in] DeviceHandle The device to probe.
637 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
640 @retval EFI_SUCCESS The driver supports the device being probed.
642 @retval EFI_UNSUPPORTED Based on virtio-blk discovery, we do not support
645 @return Error codes from the OpenProtocol() boot service or
652 VirtioBlkDriverBindingSupported (
653 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
654 IN EFI_HANDLE DeviceHandle
,
655 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
659 VIRTIO_DEVICE_PROTOCOL
*VirtIo
;
662 // Attempt to open the device with the VirtIo set of interfaces. On success,
663 // the protocol is "instantiated" for the VirtIo device. Covers duplicate
664 // open attempts (EFI_ALREADY_STARTED).
666 Status
= gBS
->OpenProtocol (
667 DeviceHandle
, // candidate device
668 &gVirtioDeviceProtocolGuid
, // for generic VirtIo access
669 (VOID
**)&VirtIo
, // handle to instantiate
670 This
->DriverBindingHandle
, // requestor driver identity
671 DeviceHandle
, // ControllerHandle, according to
672 // the UEFI Driver Model
673 EFI_OPEN_PROTOCOL_BY_DRIVER
// get exclusive VirtIo access to
674 // the device; to be released
676 if (EFI_ERROR (Status
)) {
680 if (VirtIo
->SubSystemDeviceId
!= VIRTIO_SUBSYSTEM_BLOCK_DEVICE
) {
681 Status
= EFI_UNSUPPORTED
;
685 // We needed VirtIo access only transitorily, to see whether we support the
688 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
689 This
->DriverBindingHandle
, DeviceHandle
);
696 Set up all BlockIo and virtio-blk aspects of this driver for the specified
699 @param[in out] Dev The driver instance to configure. The caller is
700 responsible for Dev->VirtIo's validity (ie. working IO
701 access to the underlying virtio-blk device).
703 @retval EFI_SUCCESS Setup complete.
705 @retval EFI_UNSUPPORTED The driver is unable to work with the virtio ring or
706 virtio-blk attributes the host provides.
708 @return Error codes from VirtioRingInit() or
709 VIRTIO_CFG_READ() / VIRTIO_CFG_WRITE or
727 UINT8 PhysicalBlockExp
;
728 UINT8 AlignmentOffset
;
731 UINT64 RingBaseShift
;
733 PhysicalBlockExp
= 0;
738 // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.
740 NextDevStat
= 0; // step 1 -- reset device
741 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
742 if (EFI_ERROR (Status
)) {
746 NextDevStat
|= VSTAT_ACK
; // step 2 -- acknowledge device presence
747 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
748 if (EFI_ERROR (Status
)) {
752 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
753 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
754 if (EFI_ERROR (Status
)) {
759 // Set Page Size - MMIO VirtIo Specific
761 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
762 if (EFI_ERROR (Status
)) {
767 // step 4a -- retrieve and validate features
769 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
770 if (EFI_ERROR (Status
)) {
774 Status
= VIRTIO_CFG_READ (Dev
, Capacity
, &NumSectors
);
775 if (EFI_ERROR (Status
)) {
778 if (NumSectors
== 0) {
779 Status
= EFI_UNSUPPORTED
;
783 if (Features
& VIRTIO_BLK_F_BLK_SIZE
) {
784 Status
= VIRTIO_CFG_READ (Dev
, BlkSize
, &BlockSize
);
785 if (EFI_ERROR (Status
)) {
788 if (BlockSize
== 0 || BlockSize
% 512 != 0 ||
789 ModU64x32 (NumSectors
, BlockSize
/ 512) != 0) {
791 // We can only handle a logical block consisting of whole sectors,
792 // and only a disk composed of whole logical blocks.
794 Status
= EFI_UNSUPPORTED
;
802 if (Features
& VIRTIO_BLK_F_TOPOLOGY
) {
803 Status
= VIRTIO_CFG_READ (Dev
, Topology
.PhysicalBlockExp
,
805 if (EFI_ERROR (Status
)) {
808 if (PhysicalBlockExp
>= 32) {
809 Status
= EFI_UNSUPPORTED
;
813 Status
= VIRTIO_CFG_READ (Dev
, Topology
.AlignmentOffset
, &AlignmentOffset
);
814 if (EFI_ERROR (Status
)) {
818 Status
= VIRTIO_CFG_READ (Dev
, Topology
.OptIoSize
, &OptIoSize
);
819 if (EFI_ERROR (Status
)) {
824 Features
&= VIRTIO_BLK_F_BLK_SIZE
| VIRTIO_BLK_F_TOPOLOGY
| VIRTIO_BLK_F_RO
|
825 VIRTIO_BLK_F_FLUSH
| VIRTIO_F_VERSION_1
|
826 VIRTIO_F_IOMMU_PLATFORM
;
829 // In virtio-1.0, feature negotiation is expected to complete before queue
830 // discovery, and the device can also reject the selected set of features.
832 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
833 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
834 if (EFI_ERROR (Status
)) {
840 // step 4b -- allocate virtqueue
842 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, 0);
843 if (EFI_ERROR (Status
)) {
846 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
847 if (EFI_ERROR (Status
)) {
850 if (QueueSize
< 3) { // SynchronousRequest() uses at most three descriptors
851 Status
= EFI_UNSUPPORTED
;
855 Status
= VirtioRingInit (Dev
->VirtIo
, QueueSize
, &Dev
->Ring
);
856 if (EFI_ERROR (Status
)) {
861 // If anything fails from here on, we must release the ring resources
863 Status
= VirtioRingMap (
869 if (EFI_ERROR (Status
)) {
874 // Additional steps for MMIO: align the queue appropriately, and set the
875 // size. If anything fails from here on, we must unmap the ring resources.
877 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
878 if (EFI_ERROR (Status
)) {
882 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
883 if (EFI_ERROR (Status
)) {
888 // step 4c -- Report GPFN (guest-physical frame number) of queue.
890 Status
= Dev
->VirtIo
->SetQueueAddress (
895 if (EFI_ERROR (Status
)) {
901 // step 5 -- Report understood features.
903 if (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) {
904 Features
&= ~(UINT64
)(VIRTIO_F_VERSION_1
| VIRTIO_F_IOMMU_PLATFORM
);
905 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
906 if (EFI_ERROR (Status
)) {
912 // step 6 -- initialization complete
914 NextDevStat
|= VSTAT_DRIVER_OK
;
915 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
916 if (EFI_ERROR (Status
)) {
921 // Populate the exported interface's attributes; see UEFI spec v2.4, 12.9 EFI
922 // Block I/O Protocol.
924 Dev
->BlockIo
.Revision
= 0;
925 Dev
->BlockIo
.Media
= &Dev
->BlockIoMedia
;
926 Dev
->BlockIo
.Reset
= &VirtioBlkReset
;
927 Dev
->BlockIo
.ReadBlocks
= &VirtioBlkReadBlocks
;
928 Dev
->BlockIo
.WriteBlocks
= &VirtioBlkWriteBlocks
;
929 Dev
->BlockIo
.FlushBlocks
= &VirtioBlkFlushBlocks
;
930 Dev
->BlockIoMedia
.MediaId
= 0;
931 Dev
->BlockIoMedia
.RemovableMedia
= FALSE
;
932 Dev
->BlockIoMedia
.MediaPresent
= TRUE
;
933 Dev
->BlockIoMedia
.LogicalPartition
= FALSE
;
934 Dev
->BlockIoMedia
.ReadOnly
= (BOOLEAN
) ((Features
& VIRTIO_BLK_F_RO
) != 0);
935 Dev
->BlockIoMedia
.WriteCaching
= (BOOLEAN
) ((Features
& VIRTIO_BLK_F_FLUSH
) != 0);
936 Dev
->BlockIoMedia
.BlockSize
= BlockSize
;
937 Dev
->BlockIoMedia
.IoAlign
= 0;
938 Dev
->BlockIoMedia
.LastBlock
= DivU64x32 (NumSectors
,
939 BlockSize
/ 512) - 1;
941 DEBUG ((DEBUG_INFO
, "%a: LbaSize=0x%x[B] NumBlocks=0x%Lx[Lba]\n",
942 __FUNCTION__
, Dev
->BlockIoMedia
.BlockSize
,
943 Dev
->BlockIoMedia
.LastBlock
+ 1));
945 if (Features
& VIRTIO_BLK_F_TOPOLOGY
) {
946 Dev
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION3
;
948 Dev
->BlockIoMedia
.LowestAlignedLba
= AlignmentOffset
;
949 Dev
->BlockIoMedia
.LogicalBlocksPerPhysicalBlock
= 1u << PhysicalBlockExp
;
950 Dev
->BlockIoMedia
.OptimalTransferLengthGranularity
= OptIoSize
;
952 DEBUG ((DEBUG_INFO
, "%a: FirstAligned=0x%Lx[Lba] PhysBlkSize=0x%x[Lba]\n",
953 __FUNCTION__
, Dev
->BlockIoMedia
.LowestAlignedLba
,
954 Dev
->BlockIoMedia
.LogicalBlocksPerPhysicalBlock
));
955 DEBUG ((DEBUG_INFO
, "%a: OptimalTransferLengthGranularity=0x%x[Lba]\n",
956 __FUNCTION__
, Dev
->BlockIoMedia
.OptimalTransferLengthGranularity
));
961 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RingMap
);
964 VirtioRingUninit (Dev
->VirtIo
, &Dev
->Ring
);
968 // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device
969 // Status. VirtIo access failure here should not mask the original error.
971 NextDevStat
|= VSTAT_FAILED
;
972 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
974 return Status
; // reached only via Failed above
980 Uninitialize the internals of a virtio-blk device that has been successfully
981 set up with VirtioBlkInit().
983 @param[in out] Dev The device to clean up.
995 // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When
996 // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from
997 // the old comms area.
999 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
1001 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RingMap
);
1002 VirtioRingUninit (Dev
->VirtIo
, &Dev
->Ring
);
1004 SetMem (&Dev
->BlockIo
, sizeof Dev
->BlockIo
, 0x00);
1005 SetMem (&Dev
->BlockIoMedia
, sizeof Dev
->BlockIoMedia
, 0x00);
1011 Event notification function enqueued by ExitBootServices().
1013 @param[in] Event Event whose notification function is being invoked.
1015 @param[in] Context Pointer to the VBLK_DEV structure.
1029 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
1031 // Reset the device. This causes the hypervisor to forget about the virtio
1034 // We allocated said ring in EfiBootServicesData type memory, and code
1035 // executing after ExitBootServices() is permitted to overwrite it.
1038 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
1043 After we've pronounced support for a specific device in
1044 DriverBindingSupported(), we start managing said device (passed in by the
1045 Driver Execution Environment) with the following service.
1047 See DriverBindingSupported() for specification references.
1049 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
1050 incorporating this driver (independently of
1053 @param[in] DeviceHandle The supported device to drive.
1055 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
1058 @retval EFI_SUCCESS Driver instance has been created and
1059 initialized for the virtio-blk device, it
1060 is now accessible via EFI_BLOCK_IO_PROTOCOL.
1062 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
1064 @return Error codes from the OpenProtocol() boot
1065 service, the VirtIo protocol, VirtioBlkInit(),
1066 or the InstallProtocolInterface() boot service.
1072 VirtioBlkDriverBindingStart (
1073 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1074 IN EFI_HANDLE DeviceHandle
,
1075 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1081 Dev
= (VBLK_DEV
*) AllocateZeroPool (sizeof *Dev
);
1083 return EFI_OUT_OF_RESOURCES
;
1086 Status
= gBS
->OpenProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1087 (VOID
**)&Dev
->VirtIo
, This
->DriverBindingHandle
,
1088 DeviceHandle
, EFI_OPEN_PROTOCOL_BY_DRIVER
);
1089 if (EFI_ERROR (Status
)) {
1094 // VirtIo access granted, configure virtio-blk device.
1096 Status
= VirtioBlkInit (Dev
);
1097 if (EFI_ERROR (Status
)) {
1101 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
1102 &VirtioBlkExitBoot
, Dev
, &Dev
->ExitBoot
);
1103 if (EFI_ERROR (Status
)) {
1108 // Setup complete, attempt to export the driver instance's BlockIo interface.
1110 Dev
->Signature
= VBLK_SIG
;
1111 Status
= gBS
->InstallProtocolInterface (&DeviceHandle
,
1112 &gEfiBlockIoProtocolGuid
, EFI_NATIVE_INTERFACE
,
1114 if (EFI_ERROR (Status
)) {
1121 gBS
->CloseEvent (Dev
->ExitBoot
);
1124 VirtioBlkUninit (Dev
);
1127 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1128 This
->DriverBindingHandle
, DeviceHandle
);
1139 Stop driving a virtio-blk device and remove its BlockIo interface.
1141 This function replays the success path of DriverBindingStart() in reverse.
1142 The host side virtio-blk device is reset, so that the OS boot loader or the
1143 OS may reinitialize it.
1145 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
1146 incorporating this driver (independently of any
1149 @param[in] DeviceHandle Stop driving this device.
1151 @param[in] NumberOfChildren Since this function belongs to a device driver
1152 only (as opposed to a bus driver), the caller
1153 environment sets NumberOfChildren to zero, and
1156 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
1162 VirtioBlkDriverBindingStop (
1163 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1164 IN EFI_HANDLE DeviceHandle
,
1165 IN UINTN NumberOfChildren
,
1166 IN EFI_HANDLE
*ChildHandleBuffer
1170 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
1173 Status
= gBS
->OpenProtocol (
1174 DeviceHandle
, // candidate device
1175 &gEfiBlockIoProtocolGuid
, // retrieve the BlockIo iface
1176 (VOID
**)&BlockIo
, // target pointer
1177 This
->DriverBindingHandle
, // requestor driver identity
1178 DeviceHandle
, // requesting lookup for dev.
1179 EFI_OPEN_PROTOCOL_GET_PROTOCOL
// lookup only, no ref. added
1181 if (EFI_ERROR (Status
)) {
1185 Dev
= VIRTIO_BLK_FROM_BLOCK_IO (BlockIo
);
1188 // Handle Stop() requests for in-use driver instances gracefully.
1190 Status
= gBS
->UninstallProtocolInterface (DeviceHandle
,
1191 &gEfiBlockIoProtocolGuid
, &Dev
->BlockIo
);
1192 if (EFI_ERROR (Status
)) {
1196 gBS
->CloseEvent (Dev
->ExitBoot
);
1198 VirtioBlkUninit (Dev
);
1200 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1201 This
->DriverBindingHandle
, DeviceHandle
);
1210 // The static object that groups the Supported() (ie. probe), Start() and
1211 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
1212 // C, 10.1 EFI Driver Binding Protocol.
1214 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding
= {
1215 &VirtioBlkDriverBindingSupported
,
1216 &VirtioBlkDriverBindingStart
,
1217 &VirtioBlkDriverBindingStop
,
1218 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
1219 NULL
, // ImageHandle, to be overwritten by
1220 // EfiLibInstallDriverBindingComponentName2() in VirtioBlkEntryPoint()
1221 NULL
// DriverBindingHandle, ditto
1226 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
1227 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
1228 // in English, for display on standard console devices. This is recommended for
1229 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
1230 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
1232 // Device type names ("Virtio Block Device") are not formatted because the
1233 // driver supports only that device type. Therefore the driver name suffices
1234 // for unambiguous identification.
1238 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
1239 { "eng;en", L
"Virtio Block Driver" },
1244 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
1248 VirtioBlkGetDriverName (
1249 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
1251 OUT CHAR16
**DriverName
1254 return LookupUnicodeString2 (
1256 This
->SupportedLanguages
,
1259 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
1265 VirtioBlkGetDeviceName (
1266 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
1267 IN EFI_HANDLE DeviceHandle
,
1268 IN EFI_HANDLE ChildHandle
,
1270 OUT CHAR16
**ControllerName
1273 return EFI_UNSUPPORTED
;
1277 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
1278 &VirtioBlkGetDriverName
,
1279 &VirtioBlkGetDeviceName
,
1280 "eng" // SupportedLanguages, ISO 639-2 language codes
1284 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2
= {
1285 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME
) &VirtioBlkGetDriverName
,
1286 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME
) &VirtioBlkGetDeviceName
,
1287 "en" // SupportedLanguages, RFC 4646 language codes
1292 // Entry point of this driver.
1296 VirtioBlkEntryPoint (
1297 IN EFI_HANDLE ImageHandle
,
1298 IN EFI_SYSTEM_TABLE
*SystemTable
1301 return EfiLibInstallDriverBindingComponentName2 (