1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
#include "hw.h"
#include "sysemu.h"
#include "scsi.h"
#include "scsi-defs.h"
#include "block.h"
#include "qdev.h"
static struct BusInfo scsi_bus_info = {
.name = "SCSI",
.size = sizeof(SCSIBus),
.props = (Property[]) {
DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
DEFINE_PROP_END_OF_LIST(),
},
};
static int next_scsi_bus;
/* Create a scsi bus, and attach devices to it. */
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
scsi_completionfn complete)
{
qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
bus->busnr = next_scsi_bus++;
bus->tcq = tcq;
bus->ndev = ndev;
bus->complete = complete;
bus->qbus.allow_hotplug = 1;
}
static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
{
SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
int rc = -1;
if (dev->id == -1) {
for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
if (bus->devs[dev->id] == NULL)
break;
}
}
if (dev->id >= bus->ndev) {
qemu_error("bad scsi device id: %d\n", dev->id);
goto err;
}
if (bus->devs[dev->id]) {
qdev_free(&bus->devs[dev->id]->qdev);
}
bus->devs[dev->id] = dev;
dev->info = info;
QTAILQ_INIT(&dev->requests);
rc = dev->info->init(dev);
if (rc != 0) {
bus->devs[dev->id] = NULL;
}
err:
return rc;
}
static int scsi_qdev_exit(DeviceState *qdev)
{
SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
assert(bus->devs[dev->id] != NULL);
if (bus->devs[dev->id]->info->destroy) {
bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
}
bus->devs[dev->id] = NULL;
return 0;
}
void scsi_qdev_register(SCSIDeviceInfo *info)
{
info->qdev.bus_info = &scsi_bus_info;
info->qdev.init = scsi_qdev_init;
info->qdev.unplug = qdev_simple_unplug_cb;
info->qdev.exit = scsi_qdev_exit;
qdev_register(&info->qdev);
}
/* handle legacy '-drive if=scsi,...' cmd line args */
/* FIXME callers should check for failure, but don't */
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, DriveInfo *dinfo, int unit)
{
const char *driver;
DeviceState *dev;
driver = bdrv_is_sg(dinfo->bdrv) ? "scsi-generic" : "scsi-disk";
dev = qdev_create(&bus->qbus, driver);
qdev_prop_set_uint32(dev, "scsi-id", unit);
qdev_prop_set_drive(dev, "drive", dinfo);
if (qdev_init(dev) < 0)
return NULL;
return DO_UPCAST(SCSIDevice, qdev, dev);
}
void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
{
DriveInfo *dinfo;
int unit;
for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
dinfo = drive_get(IF_SCSI, bus->busnr, unit);
if (dinfo == NULL) {
continue;
}
scsi_bus_legacy_add_drive(bus, dinfo, unit);
}
}
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
{
SCSIRequest *req;
req = qemu_mallocz(size);
req->bus = scsi_bus_from_device(d);
req->dev = d;
req->tag = tag;
req->lun = lun;
QTAILQ_INSERT_TAIL(&d->requests, req, next);
return req;
}
SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
{
SCSIRequest *req;
QTAILQ_FOREACH(req, &d->requests, next) {
if (req->tag == tag) {
return req;
}
}
return NULL;
}
void scsi_req_free(SCSIRequest *req)
{
QTAILQ_REMOVE(&req->dev->requests, req, next);
qemu_free(req);
}
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
{
switch (cmd[0] >> 5) {
case 0:
req->cmd.xfer = cmd[4];
req->cmd.len = 6;
/* length 0 means 256 blocks */
if (req->cmd.xfer == 0)
req->cmd.xfer = 256;
break;
case 1:
case 2:
req->cmd.xfer = cmd[8] | (cmd[7] << 8);
req->cmd.len = 10;
break;
case 4:
req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
req->cmd.len = 16;
break;
case 5:
req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
req->cmd.len = 12;
break;
default:
return -1;
}
switch(cmd[0]) {
case TEST_UNIT_READY:
case REZERO_UNIT:
case START_STOP:
case SEEK_6:
case WRITE_FILEMARKS:
case SPACE:
case ERASE:
case ALLOW_MEDIUM_REMOVAL:
case VERIFY:
case SEEK_10:
case SYNCHRONIZE_CACHE:
case LOCK_UNLOCK_CACHE:
case LOAD_UNLOAD:
case SET_CD_SPEED:
case SET_LIMITS:
case WRITE_LONG:
case MOVE_MEDIUM:
case UPDATE_BLOCK:
req->cmd.xfer = 0;
break;
case MODE_SENSE:
break;
case WRITE_SAME:
req->cmd.xfer = 1;
break;
case READ_CAPACITY:
req->cmd.xfer = 8;
break;
case READ_BLOCK_LIMITS:
req->cmd.xfer = 6;
break;
case READ_POSITION:
req->cmd.xfer = 20;
break;
case SEND_VOLUME_TAG:
req->cmd.xfer *= 40;
break;
case MEDIUM_SCAN:
req->cmd.xfer *= 8;
break;
case WRITE_10:
case WRITE_VERIFY:
case WRITE_6:
case WRITE_12:
case WRITE_VERIFY_12:
req->cmd.xfer *= req->dev->blocksize;
break;
case READ_10:
case READ_6:
case READ_REVERSE:
case RECOVER_BUFFERED_DATA:
case READ_12:
req->cmd.xfer *= req->dev->blocksize;
break;
case INQUIRY:
req->cmd.xfer = cmd[4] | (cmd[3] << 8);
break;
}
return 0;
}
static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
{
switch(cmd[0]) {
/* stream commands */
case READ_6:
case READ_REVERSE:
case RECOVER_BUFFERED_DATA:
case WRITE_6:
req->cmd.len = 6;
req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
if (cmd[1] & 0x01) /* fixed */
req->cmd.xfer *= req->dev->blocksize;
break;
case REWIND:
case START_STOP:
req->cmd.len = 6;
req->cmd.xfer = 0;
break;
/* generic commands */
default:
return scsi_req_length(req, cmd);
}
return 0;
}
static uint64_t scsi_req_lba(SCSIRequest *req)
{
uint8_t *buf = req->cmd.buf;
uint64_t lba;
switch (buf[0] >> 5) {
case 0:
lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
(((uint64_t) buf[1] & 0x1f) << 16);
break;
case 1:
case 2:
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
break;
case 4:
lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
break;
case 5:
lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
break;
default:
lba = -1;
}
return lba;
}
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
{
int rc;
if (req->dev->type == TYPE_TAPE) {
rc = scsi_req_stream_length(req, buf);
} else {
rc = scsi_req_length(req, buf);
}
if (rc != 0)
return rc;
memcpy(req->cmd.buf, buf, req->cmd.len);
req->cmd.lba = scsi_req_lba(req);
return 0;
}
|