blob: 42b102487a7c34871980e3a44d2917a0d3899700 [file] [log] [blame]
Stefan Hajnoczi973abc72011-02-11 08:40:59 +00001/*
2 * Virtio SCSI HBA
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#ifndef _QEMU_VIRTIO_SCSI_H
15#define _QEMU_VIRTIO_SCSI_H
16
Paolo Bonzini0d09e412013-02-05 17:06:20 +010017#include "hw/virtio/virtio.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010018#include "hw/pci/pci.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010019#include "hw/scsi/scsi.h"
Stefan Hajnoczi973abc72011-02-11 08:40:59 +000020
Paolo Bonzini292c8e52013-03-29 01:08:15 +000021#define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common"
22#define VIRTIO_SCSI_COMMON(obj) \
23 OBJECT_CHECK(VirtIOSCSICommon, (obj), TYPE_VIRTIO_SCSI_COMMON)
24
KONRAD Fredericf574fa82013-04-09 12:04:48 +020025#define TYPE_VIRTIO_SCSI "virtio-scsi-device"
KONRAD Frederic3ab1dfd2013-03-21 15:15:14 +010026#define VIRTIO_SCSI(obj) \
27 OBJECT_CHECK(VirtIOSCSI, (obj), TYPE_VIRTIO_SCSI)
28
29
Stefan Hajnoczi973abc72011-02-11 08:40:59 +000030/* The ID for virtio_scsi */
31#define VIRTIO_ID_SCSI 8
32
Paolo Bonzini07a52982012-08-20 15:23:28 +020033/* Feature Bits */
34#define VIRTIO_SCSI_F_INOUT 0
35#define VIRTIO_SCSI_F_HOTPLUG 1
36#define VIRTIO_SCSI_F_CHANGE 2
37
Paolo Bonzini292c8e52013-03-29 01:08:15 +000038#define VIRTIO_SCSI_VQ_SIZE 128
39#define VIRTIO_SCSI_CDB_SIZE 32
40#define VIRTIO_SCSI_SENSE_SIZE 96
41#define VIRTIO_SCSI_MAX_CHANNEL 0
42#define VIRTIO_SCSI_MAX_TARGET 255
43#define VIRTIO_SCSI_MAX_LUN 16383
44
45/* Response codes */
46#define VIRTIO_SCSI_S_OK 0
47#define VIRTIO_SCSI_S_OVERRUN 1
48#define VIRTIO_SCSI_S_ABORTED 2
49#define VIRTIO_SCSI_S_BAD_TARGET 3
50#define VIRTIO_SCSI_S_RESET 4
51#define VIRTIO_SCSI_S_BUSY 5
52#define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6
53#define VIRTIO_SCSI_S_TARGET_FAILURE 7
54#define VIRTIO_SCSI_S_NEXUS_FAILURE 8
55#define VIRTIO_SCSI_S_FAILURE 9
56#define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10
57#define VIRTIO_SCSI_S_FUNCTION_REJECTED 11
58#define VIRTIO_SCSI_S_INCORRECT_LUN 12
59
60/* Controlq type codes. */
61#define VIRTIO_SCSI_T_TMF 0
62#define VIRTIO_SCSI_T_AN_QUERY 1
63#define VIRTIO_SCSI_T_AN_SUBSCRIBE 2
64
65/* Valid TMF subtypes. */
66#define VIRTIO_SCSI_T_TMF_ABORT_TASK 0
67#define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1
68#define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2
69#define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3
70#define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4
71#define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5
72#define VIRTIO_SCSI_T_TMF_QUERY_TASK 6
73#define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7
74
75/* Events. */
76#define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000
77#define VIRTIO_SCSI_T_NO_EVENT 0
78#define VIRTIO_SCSI_T_TRANSPORT_RESET 1
79#define VIRTIO_SCSI_T_ASYNC_NOTIFY 2
80#define VIRTIO_SCSI_T_PARAM_CHANGE 3
81
82/* Reasons for transport reset event */
83#define VIRTIO_SCSI_EVT_RESET_HARD 0
84#define VIRTIO_SCSI_EVT_RESET_RESCAN 1
85#define VIRTIO_SCSI_EVT_RESET_REMOVED 2
86
87/* SCSI command request, followed by data-out */
88typedef struct {
89 uint8_t lun[8]; /* Logical Unit Number */
90 uint64_t tag; /* Command identifier */
91 uint8_t task_attr; /* Task attribute */
92 uint8_t prio;
93 uint8_t crn;
94 uint8_t cdb[];
95} QEMU_PACKED VirtIOSCSICmdReq;
96
97/* Response, followed by sense data and data-in */
98typedef struct {
99 uint32_t sense_len; /* Sense data length */
100 uint32_t resid; /* Residual bytes in data buffer */
101 uint16_t status_qualifier; /* Status qualifier */
102 uint8_t status; /* Command completion status */
103 uint8_t response; /* Response values */
104 uint8_t sense[];
105} QEMU_PACKED VirtIOSCSICmdResp;
106
107/* Task Management Request */
108typedef struct {
109 uint32_t type;
110 uint32_t subtype;
111 uint8_t lun[8];
112 uint64_t tag;
113} QEMU_PACKED VirtIOSCSICtrlTMFReq;
114
115typedef struct {
116 uint8_t response;
117} QEMU_PACKED VirtIOSCSICtrlTMFResp;
118
119/* Asynchronous notification query/subscription */
120typedef struct {
121 uint32_t type;
122 uint8_t lun[8];
123 uint32_t event_requested;
124} QEMU_PACKED VirtIOSCSICtrlANReq;
125
126typedef struct {
127 uint32_t event_actual;
128 uint8_t response;
129} QEMU_PACKED VirtIOSCSICtrlANResp;
130
131typedef struct {
132 uint32_t event;
133 uint8_t lun[8];
134 uint32_t reason;
135} QEMU_PACKED VirtIOSCSIEvent;
136
137typedef struct {
138 uint32_t num_queues;
139 uint32_t seg_max;
140 uint32_t max_sectors;
141 uint32_t cmd_per_lun;
142 uint32_t event_info_size;
143 uint32_t sense_size;
144 uint32_t cdb_size;
145 uint16_t max_channel;
146 uint16_t max_target;
147 uint32_t max_lun;
148} QEMU_PACKED VirtIOSCSIConfig;
149
Stefan Hajnoczi973abc72011-02-11 08:40:59 +0000150struct VirtIOSCSIConf {
151 uint32_t num_queues;
152 uint32_t max_sectors;
153 uint32_t cmd_per_lun;
Nicholas Bellinger5e9be922013-03-29 01:08:16 +0000154 char *vhostfd;
155 char *wwpn;
Stefan Hajnoczi973abc72011-02-11 08:40:59 +0000156};
157
Paolo Bonzini292c8e52013-03-29 01:08:15 +0000158typedef struct VirtIOSCSICommon {
KONRAD Frederic0ac8e132013-03-21 15:15:18 +0100159 VirtIODevice parent_obj;
KONRAD Frederic394e2e42013-03-21 15:15:11 +0100160 VirtIOSCSIConf conf;
KONRAD Fredericf1b24e82013-03-18 17:37:18 +0100161
KONRAD Fredericf1b24e82013-03-18 17:37:18 +0100162 uint32_t sense_size;
163 uint32_t cdb_size;
KONRAD Fredericf1b24e82013-03-18 17:37:18 +0100164 VirtQueue *ctrl_vq;
165 VirtQueue *event_vq;
KONRAD Frederic22219522013-03-21 15:15:12 +0100166 VirtQueue **cmd_vqs;
Paolo Bonzini292c8e52013-03-29 01:08:15 +0000167} VirtIOSCSICommon;
168
169typedef struct {
170 VirtIOSCSICommon parent_obj;
171
172 SCSIBus bus;
173 int resetting;
174 bool events_dropped;
KONRAD Fredericf1b24e82013-03-18 17:37:18 +0100175} VirtIOSCSI;
176
KONRAD Frederic4bfeb182013-03-21 15:15:13 +0100177#define DEFINE_VIRTIO_SCSI_PROPERTIES(_state, _conf_field) \
178 DEFINE_PROP_UINT32("num_queues", _state, _conf_field.num_queues, 1), \
179 DEFINE_PROP_UINT32("max_sectors", _state, _conf_field.max_sectors, 0xFFFF),\
180 DEFINE_PROP_UINT32("cmd_per_lun", _state, _conf_field.cmd_per_lun, 128)
181
182#define DEFINE_VIRTIO_SCSI_FEATURES(_state, _feature_field) \
183 DEFINE_VIRTIO_COMMON_FEATURES(_state, _feature_field), \
184 DEFINE_PROP_BIT("hotplug", _state, _feature_field, VIRTIO_SCSI_F_HOTPLUG, \
185 true), \
186 DEFINE_PROP_BIT("param_change", _state, _feature_field, \
187 VIRTIO_SCSI_F_CHANGE, true)
Stefan Hajnoczi973abc72011-02-11 08:40:59 +0000188
Andreas Färber71a65202013-07-30 03:19:55 +0200189void virtio_scsi_common_realize(DeviceState *dev, Error **errp);
Andreas Färber306ec6c2013-07-30 03:50:44 +0200190void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp);
Paolo Bonzini292c8e52013-03-29 01:08:15 +0000191
Stefan Hajnoczi973abc72011-02-11 08:40:59 +0000192#endif /* _QEMU_VIRTIO_SCSI_H */