Virtio SCSI driver

Submitted by:	Bryan Venteicher  bryanv at daemoninthecloset dot org
Reviewed by:	grehan
This commit is contained in:
Peter Grehan 2012-10-11 23:41:18 +00:00
parent 1dbd8bc927
commit 2f001371c3
10 changed files with 2878 additions and 1 deletions

View File

@ -531,6 +531,7 @@ MAN= aac.4 \
${_virtio.4} \
${_virtio_balloon.4} \
${_virtio_blk.4} \
${_virtio_scsi.4} \
vkbd.4 \
vlan.4 \
vpo.4 \
@ -768,6 +769,7 @@ _nxge.4= nxge.4
_virtio.4= virtio.4
_virtio_balloon.4=virtio_balloon.4
_virtio_blk.4= virtio_blk.4
_virtio_scsi.4= virtio_scsi.4
_vtnet.4= vtnet.4
_vxge.4= vxge.4
_padlock.4= padlock.4

View File

@ -72,6 +72,10 @@ device driver.
An emulated disk controller is provided by the
.Xr virtio_blk 4
device driver.
.It Nm SCSI
An emulated SCSI HBA is provided by the
.Xr virtio_scsi 4
device driver.
.It Nm Balloon
A pseudo-device to allow the VM to release memory back to the hypervisor is
provided by the
@ -81,6 +85,7 @@ device driver.
.Sh SEE ALSO
.Xr virtio_balloon 4 ,
.Xr virtio_blk 4 ,
.Xr virtio_scsi 4 ,
.Xr vtnet 4
.Sh HISTORY
Support for VirtIO first appeared in

View File

@ -0,0 +1,92 @@
.\" Copyright (c) 2012 Bryan Venteicher
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd June 24, 2012
.Dt VIRTIO_SCSI 4
.Os
.Sh NAME
.Nm virtio_scsi
.Nd VirtIO SCSI driver
.Sh SYNOPSIS
To compile this driver into the kernel,
place the following lines in your
kernel configuration file:
.Bd -ragged -offset indent
.Cd "device virtio_scsi"
.Ed
.Pp
Alternatively, to load the driver as a
module at boot time, place the following line in
.Xr loader.conf 5 :
.Bd -literal -offset indent
virtio_scsi_load="YES"
.Ed
.Sh DESCRIPTION
The
.Nm
device driver provides support for VirtIO SCSI devices.
.Sh LOADER TUNABLES
Tunables can be set at the
.Xr loader 8
prompt before booting the kernel or stored in
.Xr loader.conf 5 .
.Bl -tag -width "xxxxxx"
.It Va hw.vtscsi.bus_reset_disable
In the initial QEMU release with VirtIO SCSI support, in-flight
operations were not aborted when stopping the device, rendering
bus reset ineffective. This tunable disables attempts to issue
reset bus commands. The default value is 1.
.El
.Sh DEBUGGING
To enable debugging prints from the
.Nm
driver, set the
.Bd -literal -offset indent
hw.vtscsi.X.debug_level
.Ed
.Pp
variable, where X is the adapter number, either in
.Xr loader.conf 5
or via
.Xr sysctl 8 .
The following bits have the described effects:
.Bl -tag -width 6n -offset indent
.It 0x01
Enable informational prints.
.It 0x02
Enable prints for driver errors.
.It 0x04
Enable tracing prints.
.El
.Sh SEE ALSO
.Xr virtio 4
.Sh HISTORY
The
.Nm
driver was written by
.An Bryan Venteicher Aq bryanv@daemoninthecloset.org .
It first appeared in
.Fx 10.0 .

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,152 @@
/*-
* This header is BSD licensed so anyone can use the definitions to implement
* compatible drivers/servers.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _VIRTIO_SCSI_H
#define _VIRTIO_SCSI_H
/* Feature bits */
#define VIRTIO_SCSI_F_INOUT 0x0001 /* Single request can contain both
* read and write buffers */
#define VIRTIO_SCSI_F_HOTPLUG 0x0002 /* Host should enable hot plug/unplug
* of new LUNs and targets.
*/
#define VIRTIO_SCSI_CDB_SIZE 32
#define VIRTIO_SCSI_SENSE_SIZE 96
/* SCSI command request, followed by data-out */
struct virtio_scsi_cmd_req {
uint8_t lun[8]; /* Logical Unit Number */
uint64_t tag; /* Command identifier */
uint8_t task_attr; /* Task attribute */
uint8_t prio;
uint8_t crn;
uint8_t cdb[VIRTIO_SCSI_CDB_SIZE];
} __packed;
/* Response, followed by sense data and data-in */
struct virtio_scsi_cmd_resp {
uint32_t sense_len; /* Sense data length */
uint32_t resid; /* Residual bytes in data buffer */
uint16_t status_qualifier; /* Status qualifier */
uint8_t status; /* Command completion status */
uint8_t response; /* Response values */
uint8_t sense[VIRTIO_SCSI_SENSE_SIZE];
} __packed;
/* Task Management Request */
struct virtio_scsi_ctrl_tmf_req {
uint32_t type;
uint32_t subtype;
uint8_t lun[8];
uint64_t tag;
} __packed;
struct virtio_scsi_ctrl_tmf_resp {
uint8_t response;
} __packed;
/* Asynchronous notification query/subscription */
struct virtio_scsi_ctrl_an_req {
uint32_t type;
uint8_t lun[8];
uint32_t event_requested;
} __packed;
struct virtio_scsi_ctrl_an_resp {
uint32_t event_actual;
uint8_t response;
} __packed;
struct virtio_scsi_event {
uint32_t event;
uint8_t lun[8];
uint32_t reason;
} __packed;
struct virtio_scsi_config {
uint32_t num_queues;
uint32_t seg_max;
uint32_t max_sectors;
uint32_t cmd_per_lun;
uint32_t event_info_size;
uint32_t sense_size;
uint32_t cdb_size;
uint16_t max_channel;
uint16_t max_target;
uint32_t max_lun;
} __packed;
/* Response codes */
#define VIRTIO_SCSI_S_OK 0
#define VIRTIO_SCSI_S_FUNCTION_COMPLETE 0
#define VIRTIO_SCSI_S_OVERRUN 1
#define VIRTIO_SCSI_S_ABORTED 2
#define VIRTIO_SCSI_S_BAD_TARGET 3
#define VIRTIO_SCSI_S_RESET 4
#define VIRTIO_SCSI_S_BUSY 5
#define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6
#define VIRTIO_SCSI_S_TARGET_FAILURE 7
#define VIRTIO_SCSI_S_NEXUS_FAILURE 8
#define VIRTIO_SCSI_S_FAILURE 9
#define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10
#define VIRTIO_SCSI_S_FUNCTION_REJECTED 11
#define VIRTIO_SCSI_S_INCORRECT_LUN 12
/* Controlq type codes. */
#define VIRTIO_SCSI_T_TMF 0
#define VIRTIO_SCSI_T_AN_QUERY 1
#define VIRTIO_SCSI_T_AN_SUBSCRIBE 2
/* Valid TMF subtypes. */
#define VIRTIO_SCSI_T_TMF_ABORT_TASK 0
#define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1
#define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2
#define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3
#define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4
#define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5
#define VIRTIO_SCSI_T_TMF_QUERY_TASK 6
#define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7
/* Events. */
#define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000
#define VIRTIO_SCSI_T_NO_EVENT 0
#define VIRTIO_SCSI_T_TRANSPORT_RESET 1
#define VIRTIO_SCSI_T_ASYNC_NOTIFY 2
/* Reasons of transport reset event */
#define VIRTIO_SCSI_EVT_RESET_HARD 0
#define VIRTIO_SCSI_EVT_RESET_RESCAN 1
#define VIRTIO_SCSI_EVT_RESET_REMOVED 2
#define VIRTIO_SCSI_S_SIMPLE 0
#define VIRTIO_SCSI_S_ORDERED 1
#define VIRTIO_SCSI_S_HEAD 2
#define VIRTIO_SCSI_S_ACA 3
#endif /* _VIRTIO_SCSI_H */

View File

@ -0,0 +1,221 @@
/*-
* Copyright (c) 2012, Bryan Venteicher <bryanv@daemoninthecloset.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _VIRTIO_SCSIVAR_H
#define _VIRTIO_SCSIVAR_H
struct vtscsi_softc;
struct vtscsi_request;
typedef void vtscsi_request_cb_t(struct vtscsi_softc *,
struct vtscsi_request *);
struct vtscsi_statistics {
unsigned long scsi_cmd_timeouts;
unsigned long dequeue_no_requests;
};
struct vtscsi_softc {
device_t vtscsi_dev;
struct mtx vtscsi_mtx;
uint64_t vtscsi_features;
uint16_t vtscsi_flags;
#define VTSCSI_FLAG_INDIRECT 0x0001
#define VTSCSI_FLAG_BIDIRECTIONAL 0x0002
#define VTSCSI_FLAG_HOTPLUG 0x0004
#define VTSCSI_FLAG_RESET 0x0008
#define VTSCSI_FLAG_DETACH 0x0010
uint16_t vtscsi_frozen;
#define VTSCSI_FROZEN_NO_REQUESTS 0x01
#define VTSCSI_FROZEN_REQUEST_VQ_FULL 0x02
struct sglist *vtscsi_sglist;
struct virtqueue *vtscsi_control_vq;
struct virtqueue *vtscsi_event_vq;
struct virtqueue *vtscsi_request_vq;
struct taskqueue *vtscsi_tq;
struct task vtscsi_control_intr_task;
struct task vtscsi_event_intr_task;
struct task vtscsi_request_intr_task;
struct cam_sim *vtscsi_sim;
struct cam_path *vtscsi_path;
int vtscsi_debug;
int vtscsi_nrequests;
int vtscsi_max_nsegs;
int vtscsi_event_buf_size;
TAILQ_HEAD(,vtscsi_request)
vtscsi_req_free;
uint16_t vtscsi_max_channel;
uint16_t vtscsi_max_target;
uint32_t vtscsi_max_lun;
#define VTSCSI_NUM_EVENT_BUFS 4
struct virtio_scsi_event
vtscsi_event_bufs[VTSCSI_NUM_EVENT_BUFS];
struct vtscsi_statistics vtscsi_stats;
};
enum vtscsi_request_state {
VTSCSI_REQ_STATE_FREE,
VTSCSI_REQ_STATE_INUSE,
VTSCSI_REQ_STATE_ABORTED,
VTSCSI_REQ_STATE_TIMEDOUT
};
struct vtscsi_request {
struct vtscsi_softc *vsr_softc;
union ccb *vsr_ccb;
vtscsi_request_cb_t *vsr_complete;
void *vsr_ptr0;
/* Request when aborting a timedout command. */
#define vsr_timedout_req vsr_ptr0
enum vtscsi_request_state vsr_state;
uint16_t vsr_flags;
#define VTSCSI_REQ_FLAG_POLLED 0x01
#define VTSCSI_REQ_FLAG_COMPLETE 0x02
#define VTSCSI_REQ_FLAG_TIMEOUT_SET 0x04
union {
struct virtio_scsi_cmd_req cmd;
struct virtio_scsi_ctrl_tmf_req tmf;
struct virtio_scsi_ctrl_an_req an;
} vsr_ureq;
#define vsr_cmd_req vsr_ureq.cmd
#define vsr_tmf_req vsr_ureq.tmf
#define vsr_an_req vsr_ureq.an
/* Make request and response non-contiguous. */
uint32_t vsr_pad;
union {
struct virtio_scsi_cmd_resp cmd;
struct virtio_scsi_ctrl_tmf_resp tmf;
struct virtio_scsi_ctrl_an_resp an;
} vsr_uresp;
#define vsr_cmd_resp vsr_uresp.cmd
#define vsr_tmf_resp vsr_uresp.tmf
#define vsr_an_resp vsr_uresp.an
struct callout vsr_callout;
TAILQ_ENTRY(vtscsi_request) vsr_link;
};
/* Private field in the CCB header that points to our request. */
#define ccbh_vtscsi_req spriv_ptr0
/* Features desired/implemented by this driver. */
#define VTSCSI_FEATURES \
(VIRTIO_SCSI_F_HOTPLUG | \
VIRTIO_RING_F_INDIRECT_DESC)
#define VTSCSI_MTX(_sc) &(_sc)->vtscsi_mtx
#define VTSCSI_LOCK_INIT(_sc, _name) mtx_init(VTSCSI_MTX(_sc), _name, \
"VTSCSI Lock", MTX_DEF)
#define VTSCSI_LOCK(_sc) mtx_lock(VTSCSI_MTX(_sc))
#define VTSCSI_UNLOCK(_sc) mtx_unlock(VTSCSI_MTX(_sc))
#define VTSCSI_LOCK_OWNED(_sc) mtx_assert(VTSCSI_MTX(_sc), MA_OWNED)
#define VTSCSI_LOCK_NOTOWNED(_sc) mtx_assert(VTSCSI_MTX(_sc), MA_NOTOWNED)
#define VTSCSI_LOCK_DESTROY(_sc) mtx_destroy(VTSCSI_MTX(_sc))
/*
* Reasons for either freezing or thawing the SIMQ.
*
* VirtIO SCSI is a bit unique in the sense that SCSI and TMF
* commands go over different queues. Both queues are fed by
* the same SIMQ, but we only freeze the SIMQ when the request
* (SCSI) virtqueue is full, not caring if the control (TMF)
* virtqueue unlikely gets full. However, both queues share the
* same pool of requests, so the completion of a TMF command
* could cause the SIMQ to be unfrozen.
*/
#define VTSCSI_REQUEST 0x01
#define VTSCSI_REQUEST_VQ 0x02
/* Debug trace levels. */
#define VTSCSI_INFO 0x01
#define VTSCSI_ERROR 0x02
#define VTSCSI_TRACE 0x04
#define vtscsi_dprintf(_sc, _level, _msg, _args ...) do { \
if ((_sc)->vtscsi_debug & (_level)) \
device_printf((_sc)->vtscsi_dev, "%s: "_msg, \
__FUNCTION__, ##_args); \
} while (0)
#define vtscsi_dprintf_req(_req, _level, _msg, _args ...) do { \
struct vtscsi_softc *__sc = (_req)->vsr_softc; \
if ((__sc)->vtscsi_debug & (_level)) \
vtscsi_printf_req(_req, __FUNCTION__, _msg, ##_args); \
} while (0)
/*
* Set the status field in a CCB, optionally clearing non CCB_STATUS_* flags.
*/
#define vtscsi_set_ccb_status(_ccbh, _status, _mask) do { \
KASSERT(((_mask) & CAM_STATUS_MASK) == 0, \
("%s:%d bad mask: 0x%x", __FUNCTION__, __LINE__, (_mask))); \
(_ccbh)->status &= ~(CAM_STATUS_MASK | (_mask)); \
(_ccbh)->status |= (_status); \
} while (0)
/*
* One segment each for the request and the response.
*/
#define VTSCSI_MIN_SEGMENTS 2
/*
* Allocate additional requests for internal use such
* as TM commands (e.g. aborting timedout commands).
*/
#define VTSCSI_RESERVED_REQUESTS 10
/*
* Specification doesn't say, use traditional SCSI default.
*/
#define VTSCSI_INITIATOR_ID 7
/*
* How to wait (or not) for request completion.
*/
#define VTSCSI_EXECUTE_ASYNC 0
#define VTSCSI_EXECUTE_POLL 1
#endif /* _VIRTIO_SCSIVAR_H */

View File

@ -58,6 +58,7 @@ static struct virtio_ident {
{ VIRTIO_ID_ENTROPY, "Entropy" },
{ VIRTIO_ID_BALLOON, "Balloon" },
{ VIRTIO_ID_IOMEMORY, "IOMemory" },
{ VIRTIO_ID_SCSI, "SCSI" },
{ VIRTIO_ID_9P, "9P Transport" },
{ 0, NULL }

View File

@ -40,6 +40,7 @@ struct vq_alloc_info;
#define VIRTIO_ID_ENTROPY 0x04
#define VIRTIO_ID_BALLOON 0x05
#define VIRTIO_ID_IOMEMORY 0x06
#define VIRTIO_ID_SCSI 0x08
#define VIRTIO_ID_9P 0x09
/* Status byte for guest to report progress. */

View File

@ -23,6 +23,6 @@
# SUCH DAMAGE.
#
SUBDIR= virtio pci network block balloon
SUBDIR= virtio pci network block balloon scsi
.include <bsd.subdir.mk>

View File

@ -0,0 +1,36 @@
#
# $FreeBSD$
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
.PATH: ${.CURDIR}/../../../dev/virtio/scsi
KMOD= virtio_scsi
SRCS= virtio_scsi.c
SRCS+= virtio_bus_if.h virtio_if.h
SRCS+= bus_if.h device_if.h opt_scsi.h opt_cam.h
MFILES= kern/bus_if.m kern/device_if.m \
dev/virtio/virtio_bus_if.m dev/virtio/virtio_if.m
.include <bsd.kmod.mk>