3 This driver produces Extended SCSI Pass Thru Protocol instances for
6 The implementation is basic:
8 - No hotplug / hot-unplug.
10 - Although EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru() could be a good match
11 for multiple in-flight virtio-scsi requests, we stick to synchronous
14 - Timeouts are not supported for EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru().
16 - Only one channel is supported. (At the time of this writing, host-side
17 virtio-scsi supports a single channel too.)
19 - Only one request queue is used (for the one synchronous request).
21 - The ResetChannel() and ResetTargetLun() functions of
22 EFI_EXT_SCSI_PASS_THRU_PROTOCOL are not supported (which is allowed by the
23 UEFI 2.3.1 Errata C specification), although
24 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET could be a good match. That would
25 however require client code for the control queue, which is deemed
28 Copyright (C) 2012, Red Hat, Inc.
29 Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
30 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
32 This program and the accompanying materials are licensed and made available
33 under the terms and conditions of the BSD License which accompanies this
34 distribution. The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
38 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
42 #include <IndustryStandard/VirtioScsi.h>
43 #include <Library/BaseMemoryLib.h>
44 #include <Library/DebugLib.h>
45 #include <Library/MemoryAllocationLib.h>
46 #include <Library/UefiBootServicesTableLib.h>
47 #include <Library/UefiLib.h>
48 #include <Library/VirtioLib.h>
50 #include "VirtioScsi.h"
54 Convenience macros to read and write configuration elements of the
55 virtio-scsi VirtIo device.
57 The following macros make it possible to specify only the "core parameters"
58 for such accesses and to derive the rest. By the time VIRTIO_CFG_WRITE()
59 returns, the transaction will have been completed.
61 @param[in] Dev Pointer to the VSCSI_DEV structure.
63 @param[in] Field A field name from VSCSI_HDR, identifying the virtio-scsi
64 configuration item to access.
66 @param[in] Value (VIRTIO_CFG_WRITE() only.) The value to write to the
67 selected configuration item.
69 @param[out] Pointer (VIRTIO_CFG_READ() only.) The object to receive the
70 value read from the configuration item. Its type must be
71 one of UINT8, UINT16, UINT32, UINT64.
74 @return Status codes returned by Virtio->WriteDevice() / Virtio->ReadDevice().
78 #define VIRTIO_CFG_WRITE(Dev, Field, Value) ((Dev)->VirtIo->WriteDevice ( \
80 OFFSET_OF_VSCSI (Field), \
81 SIZE_OF_VSCSI (Field), \
85 #define VIRTIO_CFG_READ(Dev, Field, Pointer) ((Dev)->VirtIo->ReadDevice ( \
87 OFFSET_OF_VSCSI (Field), \
88 SIZE_OF_VSCSI (Field), \
95 // UEFI Spec 2.3.1 + Errata C, 14.7 Extended SCSI Pass Thru Protocol specifies
96 // the PassThru() interface. Beside returning a status code, the function must
97 // set some fields in the EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET in/out
98 // parameter on return. The following is a full list of those fields, for
99 // easier validation of PopulateRequest(), ParseResponse(), and
100 // ReportHostAdapterError() below.
102 // - InTransferLength
103 // - OutTransferLength
104 // - HostAdapterStatus
109 // On any return from the PassThru() interface, these fields must be set,
110 // except if the returned status code is explicitly exempt. (Actually the
111 // implementation here conservatively sets these fields even in case not all
112 // of them would be required by the specification.)
117 Populate a virtio-scsi request from the Extended SCSI Pass Thru Protocol
120 The caller is responsible for pre-zeroing the virtio-scsi request. The
121 Extended SCSI Pass Thru Protocol packet is modified, to be forwarded outwards
122 by VirtioScsiPassThru(), if invalid or unsupported parameters are detected.
124 @param[in] Dev The virtio-scsi host device the packet targets.
126 @param[in] Target The SCSI target controlled by the virtio-scsi host
129 @param[in] Lun The Logical Unit Number under the SCSI target.
131 @param[in out] Packet The Extended SCSI Pass Thru Protocol packet the
132 function translates to a virtio-scsi request. On
133 failure this parameter relays error contents.
135 @param[out] Request The pre-zeroed virtio-scsi request to populate. This
136 parameter is volatile-qualified because we expect the
137 caller to append it to a virtio ring, thus
138 assignments to Request must be visible when the
142 @retval EFI_SUCCESS The Extended SCSI Pass Thru Protocol packet was valid,
143 Request has been populated.
145 @return Otherwise, invalid or unsupported parameters were
146 detected. Status codes are meant for direct forwarding
147 by the EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru()
155 IN CONST VSCSI_DEV
*Dev
,
158 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
159 OUT
volatile VIRTIO_SCSI_REQ
*Request
166 // bidirectional transfer was requested, but the host doesn't support it
168 (Packet
->InTransferLength
> 0 && Packet
->OutTransferLength
> 0 &&
169 !Dev
->InOutSupported
) ||
172 // a target / LUN was addressed that's impossible to encode for the host
174 Target
> 0xFF || Lun
>= 0x4000 ||
177 // Command Descriptor Block bigger than VIRTIO_SCSI_CDB_SIZE
179 Packet
->CdbLength
> VIRTIO_SCSI_CDB_SIZE
||
182 // From virtio-0.9.5, 2.3.2 Descriptor Table:
183 // "no descriptor chain may be more than 2^32 bytes long in total".
185 (UINT64
) Packet
->InTransferLength
+ Packet
->OutTransferLength
> SIZE_1GB
189 // this error code doesn't require updates to the Packet output fields
191 return EFI_UNSUPPORTED
;
196 // addressed invalid device
198 Target
> Dev
->MaxTarget
|| Lun
> Dev
->MaxLun
||
201 // invalid direction (there doesn't seem to be a macro for the "no data
202 // transferred" "direction", eg. for TEST UNIT READY)
204 Packet
->DataDirection
> EFI_EXT_SCSI_DATA_DIRECTION_BIDIRECTIONAL
||
207 // trying to receive, but destination pointer is NULL, or contradicting
208 // transfer direction
210 (Packet
->InTransferLength
> 0 &&
211 (Packet
->InDataBuffer
== NULL
||
212 Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_WRITE
217 // trying to send, but source pointer is NULL, or contradicting transfer
220 (Packet
->OutTransferLength
> 0 &&
221 (Packet
->OutDataBuffer
== NULL
||
222 Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
228 // this error code doesn't require updates to the Packet output fields
230 return EFI_INVALID_PARAMETER
;
234 // Catch oversized requests eagerly. If this condition evaluates to false,
235 // then the combined size of a bidirectional request will not exceed the
236 // virtio-scsi device's transfer limit either.
238 if (ALIGN_VALUE (Packet
->OutTransferLength
, 512) / 512
239 > Dev
->MaxSectors
/ 2 ||
240 ALIGN_VALUE (Packet
->InTransferLength
, 512) / 512
241 > Dev
->MaxSectors
/ 2) {
242 Packet
->InTransferLength
= (Dev
->MaxSectors
/ 2) * 512;
243 Packet
->OutTransferLength
= (Dev
->MaxSectors
/ 2) * 512;
244 Packet
->HostAdapterStatus
=
245 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN
;
246 Packet
->TargetStatus
= EFI_EXT_SCSI_STATUS_TARGET_GOOD
;
247 Packet
->SenseDataLength
= 0;
248 return EFI_BAD_BUFFER_SIZE
;
252 // target & LUN encoding: see virtio-0.9.5, Appendix I: SCSI Host Device,
253 // Device Operation: request queues
256 Request
->Lun
[1] = (UINT8
) Target
;
257 Request
->Lun
[2] = (UINT8
) (((UINT32
)Lun
>> 8) | 0x40);
258 Request
->Lun
[3] = (UINT8
) Lun
;
261 // CopyMem() would cast away the "volatile" qualifier before access, which is
262 // undefined behavior (ISO C99 6.7.3p5)
264 for (Idx
= 0; Idx
< Packet
->CdbLength
; ++Idx
) {
265 Request
->Cdb
[Idx
] = ((UINT8
*) Packet
->Cdb
)[Idx
];
274 Parse the virtio-scsi device's response, translate it to an EFI status code,
275 and update the Extended SCSI Pass Thru Protocol packet, to be returned by
276 the EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru() implementation.
278 @param[in out] Packet The Extended SCSI Pass Thru Protocol packet that has
279 been translated to a virtio-scsi request with
280 PopulateRequest(), and processed by the host. On
281 output this parameter is updated with response or
284 @param[in] Response The virtio-scsi response structure to parse. We expect
285 it to come from a virtio ring, thus it is qualified
289 @return PassThru() status codes mandated by UEFI Spec 2.3.1 + Errata C, 14.7
290 Extended SCSI Pass Thru Protocol.
297 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
298 IN CONST
volatile VIRTIO_SCSI_RESP
*Response
301 UINTN ResponseSenseLen
;
305 // return sense data (length and contents) in all cases, truncated if needed
307 ResponseSenseLen
= MIN (Response
->SenseLen
, VIRTIO_SCSI_SENSE_SIZE
);
308 if (Packet
->SenseDataLength
> ResponseSenseLen
) {
309 Packet
->SenseDataLength
= (UINT8
) ResponseSenseLen
;
311 for (Idx
= 0; Idx
< Packet
->SenseDataLength
; ++Idx
) {
312 ((UINT8
*) Packet
->SenseData
)[Idx
] = Response
->Sense
[Idx
];
316 // Report actual transfer lengths. The logic below covers all three
317 // DataDirections (read, write, bidirectional).
321 // | write ^ @ Residual (unprocessed)
323 // -+- @ OutTransferLength -+- @ InTransferLength
327 // V @ OutTransferLength + InTransferLength -+- @ 0
329 if (Response
->Residual
<= Packet
->InTransferLength
) {
330 Packet
->InTransferLength
-= Response
->Residual
;
333 Packet
->OutTransferLength
-= Response
->Residual
- Packet
->InTransferLength
;
334 Packet
->InTransferLength
= 0;
338 // report target status in all cases
340 Packet
->TargetStatus
= Response
->Status
;
343 // host adapter status and function return value depend on virtio-scsi
346 switch (Response
->Response
) {
347 case VIRTIO_SCSI_S_OK
:
348 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK
;
351 case VIRTIO_SCSI_S_OVERRUN
:
352 Packet
->HostAdapterStatus
=
353 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_DATA_OVERRUN_UNDERRUN
;
356 case VIRTIO_SCSI_S_BAD_TARGET
:
358 // This is non-intuitive but explicitly required by the
359 // EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru() specification for
360 // disconnected (but otherwise valid) target / LUN addresses.
362 Packet
->HostAdapterStatus
=
363 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
;
366 case VIRTIO_SCSI_S_RESET
:
367 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_BUS_RESET
;
370 case VIRTIO_SCSI_S_BUSY
:
371 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OK
;
372 return EFI_NOT_READY
;
375 // Lump together the rest. The mapping for VIRTIO_SCSI_S_ABORTED is
376 // intentional as well, not an oversight.
378 case VIRTIO_SCSI_S_ABORTED
:
379 case VIRTIO_SCSI_S_TRANSPORT_FAILURE
:
380 case VIRTIO_SCSI_S_TARGET_FAILURE
:
381 case VIRTIO_SCSI_S_NEXUS_FAILURE
:
382 case VIRTIO_SCSI_S_FAILURE
:
384 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OTHER
;
387 return EFI_DEVICE_ERROR
;
393 The function can be used to create a fake host adapter error.
395 When VirtioScsiPassThru() is failed due to some reasons then this function
396 can be called to construct a host adapter error.
398 @param[out] Packet The Extended SCSI Pass Thru Protocol packet that the host
399 adapter error shall be placed in.
402 @retval EFI_DEVICE_ERROR The function returns this status code
403 unconditionally, to be propagated by
404 VirtioScsiPassThru().
409 ReportHostAdapterError (
410 OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
413 Packet
->InTransferLength
= 0;
414 Packet
->OutTransferLength
= 0;
415 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_OTHER
;
416 Packet
->TargetStatus
= EFI_EXT_SCSI_STATUS_TARGET_GOOD
;
417 Packet
->SenseDataLength
= 0;
418 return EFI_DEVICE_ERROR
;
423 // The next seven functions implement EFI_EXT_SCSI_PASS_THRU_PROTOCOL
424 // for the virtio-scsi HBA. Refer to UEFI Spec 2.3.1 + Errata C, sections
425 // - 14.1 SCSI Driver Model Overview,
426 // - 14.7 Extended SCSI Pass Thru Protocol.
432 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
435 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
436 IN EFI_EVENT Event OPTIONAL
442 volatile VIRTIO_SCSI_REQ Request
;
443 volatile VIRTIO_SCSI_RESP Response
;
444 DESC_INDICES Indices
;
446 ZeroMem ((VOID
*) &Request
, sizeof (Request
));
447 ZeroMem ((VOID
*) &Response
, sizeof (Response
));
449 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (This
);
450 CopyMem (&TargetValue
, Target
, sizeof TargetValue
);
452 Status
= PopulateRequest (Dev
, TargetValue
, Lun
, Packet
, &Request
);
453 if (EFI_ERROR (Status
)) {
457 VirtioPrepare (&Dev
->Ring
, &Indices
);
460 // preset a host status for ourselves that we do not accept as success
462 Response
.Response
= VIRTIO_SCSI_S_FAILURE
;
465 // ensured by VirtioScsiInit() -- this predicate, in combination with the
466 // lock-step progress, ensures we don't have to track free descriptors.
468 ASSERT (Dev
->Ring
.QueueSize
>= 4);
473 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) &Request
, sizeof Request
,
474 VRING_DESC_F_NEXT
, &Indices
);
477 // enqueue "dataout" if any
479 if (Packet
->OutTransferLength
> 0) {
480 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) Packet
->OutDataBuffer
,
481 Packet
->OutTransferLength
, VRING_DESC_F_NEXT
, &Indices
);
485 // enqueue Response, to be written by the host
487 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) &Response
, sizeof Response
,
488 VRING_DESC_F_WRITE
| (Packet
->InTransferLength
> 0 ?
489 VRING_DESC_F_NEXT
: 0),
493 // enqueue "datain" if any, to be written by the host
495 if (Packet
->InTransferLength
> 0) {
496 VirtioAppendDesc (&Dev
->Ring
, (UINTN
) Packet
->InDataBuffer
,
497 Packet
->InTransferLength
, VRING_DESC_F_WRITE
, &Indices
);
500 // If kicking the host fails, we must fake a host adapter error.
501 // EFI_NOT_READY would save us the effort, but it would also suggest that the
504 if (VirtioFlush (Dev
->VirtIo
, VIRTIO_SCSI_REQUEST_QUEUE
, &Dev
->Ring
,
505 &Indices
, NULL
) != EFI_SUCCESS
) {
506 return ReportHostAdapterError (Packet
);
509 return ParseResponse (Packet
, &Response
);
515 VirtioScsiGetNextTargetLun (
516 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
517 IN OUT UINT8
**TargetPointer
,
527 // the TargetPointer input parameter is unnecessarily a pointer-to-pointer
529 Target
= *TargetPointer
;
532 // Search for first non-0xFF byte. If not found, return first target & LUN.
534 for (Idx
= 0; Idx
< TARGET_MAX_BYTES
&& Target
[Idx
] == 0xFF; ++Idx
)
536 if (Idx
== TARGET_MAX_BYTES
) {
537 SetMem (Target
, TARGET_MAX_BYTES
, 0x00);
543 // see the TARGET_MAX_BYTES check in "VirtioScsi.h"
545 CopyMem (&LastTarget
, Target
, sizeof LastTarget
);
548 // increment (target, LUN) pair if valid on input
550 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (This
);
551 if (LastTarget
> Dev
->MaxTarget
|| *Lun
> Dev
->MaxLun
) {
552 return EFI_INVALID_PARAMETER
;
555 if (*Lun
< Dev
->MaxLun
) {
560 if (LastTarget
< Dev
->MaxTarget
) {
563 CopyMem (Target
, &LastTarget
, sizeof LastTarget
);
567 return EFI_NOT_FOUND
;
573 VirtioScsiBuildDevicePath (
574 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
577 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
582 SCSI_DEVICE_PATH
*ScsiDevicePath
;
584 if (DevicePath
== NULL
) {
585 return EFI_INVALID_PARAMETER
;
588 CopyMem (&TargetValue
, Target
, sizeof TargetValue
);
589 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (This
);
590 if (TargetValue
> Dev
->MaxTarget
|| Lun
> Dev
->MaxLun
|| Lun
> 0xFFFF) {
591 return EFI_NOT_FOUND
;
594 ScsiDevicePath
= AllocatePool (sizeof *ScsiDevicePath
);
595 if (ScsiDevicePath
== NULL
) {
596 return EFI_OUT_OF_RESOURCES
;
599 ScsiDevicePath
->Header
.Type
= MESSAGING_DEVICE_PATH
;
600 ScsiDevicePath
->Header
.SubType
= MSG_SCSI_DP
;
601 ScsiDevicePath
->Header
.Length
[0] = (UINT8
) sizeof *ScsiDevicePath
;
602 ScsiDevicePath
->Header
.Length
[1] = (UINT8
) (sizeof *ScsiDevicePath
>> 8);
603 ScsiDevicePath
->Pun
= TargetValue
;
604 ScsiDevicePath
->Lun
= (UINT16
) Lun
;
606 *DevicePath
= &ScsiDevicePath
->Header
;
613 VirtioScsiGetTargetLun (
614 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
615 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
616 OUT UINT8
**TargetPointer
,
620 SCSI_DEVICE_PATH
*ScsiDevicePath
;
624 if (DevicePath
== NULL
|| TargetPointer
== NULL
|| *TargetPointer
== NULL
||
626 return EFI_INVALID_PARAMETER
;
629 if (DevicePath
->Type
!= MESSAGING_DEVICE_PATH
||
630 DevicePath
->SubType
!= MSG_SCSI_DP
) {
631 return EFI_UNSUPPORTED
;
634 ScsiDevicePath
= (SCSI_DEVICE_PATH
*) DevicePath
;
635 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (This
);
636 if (ScsiDevicePath
->Pun
> Dev
->MaxTarget
||
637 ScsiDevicePath
->Lun
> Dev
->MaxLun
) {
638 return EFI_NOT_FOUND
;
642 // a) the TargetPointer input parameter is unnecessarily a pointer-to-pointer
643 // b) see the TARGET_MAX_BYTES check in "VirtioScsi.h"
644 // c) ScsiDevicePath->Pun is an UINT16
646 Target
= *TargetPointer
;
647 CopyMem (Target
, &ScsiDevicePath
->Pun
, 2);
648 SetMem (Target
+ 2, TARGET_MAX_BYTES
- 2, 0x00);
650 *Lun
= ScsiDevicePath
->Lun
;
657 VirtioScsiResetChannel (
658 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
661 return EFI_UNSUPPORTED
;
667 VirtioScsiResetTargetLun (
668 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
673 return EFI_UNSUPPORTED
;
679 VirtioScsiGetNextTarget (
680 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
681 IN OUT UINT8
**TargetPointer
690 // the TargetPointer input parameter is unnecessarily a pointer-to-pointer
692 Target
= *TargetPointer
;
695 // Search for first non-0xFF byte. If not found, return first target.
697 for (Idx
= 0; Idx
< TARGET_MAX_BYTES
&& Target
[Idx
] == 0xFF; ++Idx
)
699 if (Idx
== TARGET_MAX_BYTES
) {
700 SetMem (Target
, TARGET_MAX_BYTES
, 0x00);
705 // see the TARGET_MAX_BYTES check in "VirtioScsi.h"
707 CopyMem (&LastTarget
, Target
, sizeof LastTarget
);
710 // increment target if valid on input
712 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (This
);
713 if (LastTarget
> Dev
->MaxTarget
) {
714 return EFI_INVALID_PARAMETER
;
717 if (LastTarget
< Dev
->MaxTarget
) {
719 CopyMem (Target
, &LastTarget
, sizeof LastTarget
);
723 return EFI_NOT_FOUND
;
731 IN OUT VSCSI_DEV
*Dev
736 UINT64 RingBaseShift
;
738 UINT16 MaxChannel
; // for validation only
739 UINT32 NumQueues
; // for validation only
743 // Execute virtio-0.9.5, 2.2.1 Device Initialization Sequence.
745 NextDevStat
= 0; // step 1 -- reset device
746 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
747 if (EFI_ERROR (Status
)) {
751 NextDevStat
|= VSTAT_ACK
; // step 2 -- acknowledge device presence
752 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
753 if (EFI_ERROR (Status
)) {
757 NextDevStat
|= VSTAT_DRIVER
; // step 3 -- we know how to drive it
758 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
759 if (EFI_ERROR (Status
)) {
764 // Set Page Size - MMIO VirtIo Specific
766 Status
= Dev
->VirtIo
->SetPageSize (Dev
->VirtIo
, EFI_PAGE_SIZE
);
767 if (EFI_ERROR (Status
)) {
772 // step 4a -- retrieve and validate features
774 Status
= Dev
->VirtIo
->GetDeviceFeatures (Dev
->VirtIo
, &Features
);
775 if (EFI_ERROR (Status
)) {
778 Dev
->InOutSupported
= (BOOLEAN
) ((Features
& VIRTIO_SCSI_F_INOUT
) != 0);
780 Status
= VIRTIO_CFG_READ (Dev
, MaxChannel
, &MaxChannel
);
781 if (EFI_ERROR (Status
)) {
784 if (MaxChannel
!= 0) {
786 // this driver is for a single-channel virtio-scsi HBA
788 Status
= EFI_UNSUPPORTED
;
792 Status
= VIRTIO_CFG_READ (Dev
, NumQueues
, &NumQueues
);
793 if (EFI_ERROR (Status
)) {
797 Status
= EFI_UNSUPPORTED
;
801 Status
= VIRTIO_CFG_READ (Dev
, MaxTarget
, &Dev
->MaxTarget
);
802 if (EFI_ERROR (Status
)) {
805 if (Dev
->MaxTarget
> PcdGet16 (PcdVirtioScsiMaxTargetLimit
)) {
806 Dev
->MaxTarget
= PcdGet16 (PcdVirtioScsiMaxTargetLimit
);
809 Status
= VIRTIO_CFG_READ (Dev
, MaxLun
, &Dev
->MaxLun
);
810 if (EFI_ERROR (Status
)) {
813 if (Dev
->MaxLun
> PcdGet32 (PcdVirtioScsiMaxLunLimit
)) {
814 Dev
->MaxLun
= PcdGet32 (PcdVirtioScsiMaxLunLimit
);
817 Status
= VIRTIO_CFG_READ (Dev
, MaxSectors
, &Dev
->MaxSectors
);
818 if (EFI_ERROR (Status
)) {
821 if (Dev
->MaxSectors
< 2) {
823 // We must be able to halve it for bidirectional transfers
824 // (see EFI_BAD_BUFFER_SIZE in PopulateRequest()).
826 Status
= EFI_UNSUPPORTED
;
830 Features
&= VIRTIO_SCSI_F_INOUT
| VIRTIO_F_VERSION_1
;
833 // In virtio-1.0, feature negotiation is expected to complete before queue
834 // discovery, and the device can also reject the selected set of features.
836 if (Dev
->VirtIo
->Revision
>= VIRTIO_SPEC_REVISION (1, 0, 0)) {
837 Status
= Virtio10WriteFeatures (Dev
->VirtIo
, Features
, &NextDevStat
);
838 if (EFI_ERROR (Status
)) {
844 // step 4b -- allocate request virtqueue
846 Status
= Dev
->VirtIo
->SetQueueSel (Dev
->VirtIo
, VIRTIO_SCSI_REQUEST_QUEUE
);
847 if (EFI_ERROR (Status
)) {
850 Status
= Dev
->VirtIo
->GetQueueNumMax (Dev
->VirtIo
, &QueueSize
);
851 if (EFI_ERROR (Status
)) {
855 // VirtioScsiPassThru() uses at most four descriptors
858 Status
= EFI_UNSUPPORTED
;
862 Status
= VirtioRingInit (Dev
->VirtIo
, QueueSize
, &Dev
->Ring
);
863 if (EFI_ERROR (Status
)) {
868 // If anything fails from here on, we must release the ring resources
870 Status
= VirtioRingMap (
876 if (EFI_ERROR (Status
)) {
881 // Additional steps for MMIO: align the queue appropriately, and set the
882 // size. If anything fails from here on, we must unmap the ring resources.
884 Status
= Dev
->VirtIo
->SetQueueNum (Dev
->VirtIo
, QueueSize
);
885 if (EFI_ERROR (Status
)) {
889 Status
= Dev
->VirtIo
->SetQueueAlign (Dev
->VirtIo
, EFI_PAGE_SIZE
);
890 if (EFI_ERROR (Status
)) {
895 // step 4c -- Report GPFN (guest-physical frame number) of queue.
897 Status
= Dev
->VirtIo
->SetQueueAddress (
902 if (EFI_ERROR (Status
)) {
907 // step 5 -- Report understood features and guest-tuneables.
909 if (Dev
->VirtIo
->Revision
< VIRTIO_SPEC_REVISION (1, 0, 0)) {
910 Features
&= ~(UINT64
)VIRTIO_F_VERSION_1
;
911 Status
= Dev
->VirtIo
->SetGuestFeatures (Dev
->VirtIo
, Features
);
912 if (EFI_ERROR (Status
)) {
918 // We expect these maximum sizes from the host. Since they are
919 // guest-negotiable, ask for them rather than just checking them.
921 Status
= VIRTIO_CFG_WRITE (Dev
, CdbSize
, VIRTIO_SCSI_CDB_SIZE
);
922 if (EFI_ERROR (Status
)) {
925 Status
= VIRTIO_CFG_WRITE (Dev
, SenseSize
, VIRTIO_SCSI_SENSE_SIZE
);
926 if (EFI_ERROR (Status
)) {
931 // step 6 -- initialization complete
933 NextDevStat
|= VSTAT_DRIVER_OK
;
934 Status
= Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
935 if (EFI_ERROR (Status
)) {
940 // populate the exported interface's attributes
942 Dev
->PassThru
.Mode
= &Dev
->PassThruMode
;
943 Dev
->PassThru
.PassThru
= &VirtioScsiPassThru
;
944 Dev
->PassThru
.GetNextTargetLun
= &VirtioScsiGetNextTargetLun
;
945 Dev
->PassThru
.BuildDevicePath
= &VirtioScsiBuildDevicePath
;
946 Dev
->PassThru
.GetTargetLun
= &VirtioScsiGetTargetLun
;
947 Dev
->PassThru
.ResetChannel
= &VirtioScsiResetChannel
;
948 Dev
->PassThru
.ResetTargetLun
= &VirtioScsiResetTargetLun
;
949 Dev
->PassThru
.GetNextTarget
= &VirtioScsiGetNextTarget
;
952 // AdapterId is a target for which no handle will be created during bus scan.
953 // Prevent any conflict with real devices.
955 Dev
->PassThruMode
.AdapterId
= 0xFFFFFFFF;
958 // Set both physical and logical attributes for non-RAID SCSI channel. See
959 // Driver Writer's Guide for UEFI 2.3.1 v1.01, 20.1.5 Implementing Extended
960 // SCSI Pass Thru Protocol.
962 Dev
->PassThruMode
.Attributes
= EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
|
963 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
;
966 // no restriction on transfer buffer alignment
968 Dev
->PassThruMode
.IoAlign
= 0;
973 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RingMap
);
976 VirtioRingUninit (Dev
->VirtIo
, &Dev
->Ring
);
980 // Notify the host about our failure to setup: virtio-0.9.5, 2.2.2.1 Device
981 // Status. VirtIo access failure here should not mask the original error.
983 NextDevStat
|= VSTAT_FAILED
;
984 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, NextDevStat
);
986 Dev
->InOutSupported
= FALSE
;
991 return Status
; // reached only via Failed above
999 IN OUT VSCSI_DEV
*Dev
1003 // Reset the virtual device -- see virtio-0.9.5, 2.2.2.1 Device Status. When
1004 // VIRTIO_CFG_WRITE() returns, the host will have learned to stay away from
1005 // the old comms area.
1007 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
1009 Dev
->InOutSupported
= FALSE
;
1012 Dev
->MaxSectors
= 0;
1014 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RingMap
);
1015 VirtioRingUninit (Dev
->VirtIo
, &Dev
->Ring
);
1017 SetMem (&Dev
->PassThru
, sizeof Dev
->PassThru
, 0x00);
1018 SetMem (&Dev
->PassThruMode
, sizeof Dev
->PassThruMode
, 0x00);
1023 // Event notification function enqueued by ExitBootServices().
1029 VirtioScsiExitBoot (
1037 // Reset the device. This causes the hypervisor to forget about the virtio
1040 // We allocated said ring in EfiBootServicesData type memory, and code
1041 // executing after ExitBootServices() is permitted to overwrite it.
1044 Dev
->VirtIo
->SetDeviceStatus (Dev
->VirtIo
, 0);
1047 // Unmap the ring buffer so that hypervisor will not be able to get
1048 // readable data after device reset.
1050 Dev
->VirtIo
->UnmapSharedBuffer (Dev
->VirtIo
, Dev
->RingMap
);
1055 // Probe, start and stop functions of this driver, called by the DXE core for
1056 // specific devices.
1058 // The following specifications document these interfaces:
1059 // - Driver Writer's Guide for UEFI 2.3.1 v1.01, 9 Driver Binding Protocol
1060 // - UEFI Spec 2.3.1 + Errata C, 10.1 EFI Driver Binding Protocol
1062 // The implementation follows:
1063 // - Driver Writer's Guide for UEFI 2.3.1 v1.01
1064 // - 5.1.3.4 OpenProtocol() and CloseProtocol()
1065 // - UEFI Spec 2.3.1 + Errata C
1066 // - 6.3 Protocol Handler Services
1071 VirtioScsiDriverBindingSupported (
1072 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1073 IN EFI_HANDLE DeviceHandle
,
1074 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1078 VIRTIO_DEVICE_PROTOCOL
*VirtIo
;
1081 // Attempt to open the device with the VirtIo set of interfaces. On success,
1082 // the protocol is "instantiated" for the VirtIo device. Covers duplicate open
1083 // attempts (EFI_ALREADY_STARTED).
1085 Status
= gBS
->OpenProtocol (
1086 DeviceHandle
, // candidate device
1087 &gVirtioDeviceProtocolGuid
, // for generic VirtIo access
1088 (VOID
**)&VirtIo
, // handle to instantiate
1089 This
->DriverBindingHandle
, // requestor driver identity
1090 DeviceHandle
, // ControllerHandle, according to
1091 // the UEFI Driver Model
1092 EFI_OPEN_PROTOCOL_BY_DRIVER
// get exclusive VirtIo access to
1093 // the device; to be released
1095 if (EFI_ERROR (Status
)) {
1099 if (VirtIo
->SubSystemDeviceId
!= VIRTIO_SUBSYSTEM_SCSI_HOST
) {
1100 Status
= EFI_UNSUPPORTED
;
1104 // We needed VirtIo access only transitorily, to see whether we support the
1107 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1108 This
->DriverBindingHandle
, DeviceHandle
);
1115 VirtioScsiDriverBindingStart (
1116 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1117 IN EFI_HANDLE DeviceHandle
,
1118 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1124 Dev
= (VSCSI_DEV
*) AllocateZeroPool (sizeof *Dev
);
1126 return EFI_OUT_OF_RESOURCES
;
1129 Status
= gBS
->OpenProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1130 (VOID
**)&Dev
->VirtIo
, This
->DriverBindingHandle
,
1131 DeviceHandle
, EFI_OPEN_PROTOCOL_BY_DRIVER
);
1132 if (EFI_ERROR (Status
)) {
1133 goto FreeVirtioScsi
;
1137 // VirtIo access granted, configure virtio-scsi device.
1139 Status
= VirtioScsiInit (Dev
);
1140 if (EFI_ERROR (Status
)) {
1144 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
1145 &VirtioScsiExitBoot
, Dev
, &Dev
->ExitBoot
);
1146 if (EFI_ERROR (Status
)) {
1151 // Setup complete, attempt to export the driver instance's PassThru
1154 Dev
->Signature
= VSCSI_SIG
;
1155 Status
= gBS
->InstallProtocolInterface (&DeviceHandle
,
1156 &gEfiExtScsiPassThruProtocolGuid
, EFI_NATIVE_INTERFACE
,
1158 if (EFI_ERROR (Status
)) {
1165 gBS
->CloseEvent (Dev
->ExitBoot
);
1168 VirtioScsiUninit (Dev
);
1171 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1172 This
->DriverBindingHandle
, DeviceHandle
);
1183 VirtioScsiDriverBindingStop (
1184 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1185 IN EFI_HANDLE DeviceHandle
,
1186 IN UINTN NumberOfChildren
,
1187 IN EFI_HANDLE
*ChildHandleBuffer
1191 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*PassThru
;
1194 Status
= gBS
->OpenProtocol (
1195 DeviceHandle
, // candidate device
1196 &gEfiExtScsiPassThruProtocolGuid
, // retrieve the SCSI iface
1197 (VOID
**)&PassThru
, // target pointer
1198 This
->DriverBindingHandle
, // requestor driver ident.
1199 DeviceHandle
, // lookup req. for dev.
1200 EFI_OPEN_PROTOCOL_GET_PROTOCOL
// lookup only, no new ref.
1202 if (EFI_ERROR (Status
)) {
1206 Dev
= VIRTIO_SCSI_FROM_PASS_THRU (PassThru
);
1209 // Handle Stop() requests for in-use driver instances gracefully.
1211 Status
= gBS
->UninstallProtocolInterface (DeviceHandle
,
1212 &gEfiExtScsiPassThruProtocolGuid
, &Dev
->PassThru
);
1213 if (EFI_ERROR (Status
)) {
1217 gBS
->CloseEvent (Dev
->ExitBoot
);
1219 VirtioScsiUninit (Dev
);
1221 gBS
->CloseProtocol (DeviceHandle
, &gVirtioDeviceProtocolGuid
,
1222 This
->DriverBindingHandle
, DeviceHandle
);
1231 // The static object that groups the Supported() (ie. probe), Start() and
1232 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
1233 // C, 10.1 EFI Driver Binding Protocol.
1235 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding
= {
1236 &VirtioScsiDriverBindingSupported
,
1237 &VirtioScsiDriverBindingStart
,
1238 &VirtioScsiDriverBindingStop
,
1239 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
1240 NULL
, // ImageHandle, to be overwritten by
1241 // EfiLibInstallDriverBindingComponentName2() in VirtioScsiEntryPoint()
1242 NULL
// DriverBindingHandle, ditto
1247 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
1248 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
1249 // in English, for display on standard console devices. This is recommended for
1250 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
1251 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
1253 // Device type names ("Virtio SCSI Host Device") are not formatted because the
1254 // driver supports only that device type. Therefore the driver name suffices
1255 // for unambiguous identification.
1259 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
1260 { "eng;en", L
"Virtio SCSI Host Driver" },
1265 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
1269 VirtioScsiGetDriverName (
1270 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
1272 OUT CHAR16
**DriverName
1275 return LookupUnicodeString2 (
1277 This
->SupportedLanguages
,
1280 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
1286 VirtioScsiGetDeviceName (
1287 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
1288 IN EFI_HANDLE DeviceHandle
,
1289 IN EFI_HANDLE ChildHandle
,
1291 OUT CHAR16
**ControllerName
1294 return EFI_UNSUPPORTED
;
1298 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
1299 &VirtioScsiGetDriverName
,
1300 &VirtioScsiGetDeviceName
,
1301 "eng" // SupportedLanguages, ISO 639-2 language codes
1305 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2
= {
1306 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME
) &VirtioScsiGetDriverName
,
1307 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME
) &VirtioScsiGetDeviceName
,
1308 "en" // SupportedLanguages, RFC 4646 language codes
1313 // Entry point of this driver.
1317 VirtioScsiEntryPoint (
1318 IN EFI_HANDLE ImageHandle
,
1319 IN EFI_SYSTEM_TABLE
*SystemTable
1322 return EfiLibInstallDriverBindingComponentName2 (