]>
Commit | Line | Data |
---|---|---|
a4ab4792 | 1 | #include "qemu/osdep.h" |
83c9f4ca | 2 | #include "hw/hw.h" |
da34e65c | 3 | #include "qapi/error.h" |
1de7afc9 | 4 | #include "qemu/error-report.h" |
922a01a0 | 5 | #include "qemu/option.h" |
0d09e41a | 6 | #include "hw/scsi/scsi.h" |
08e2c9f1 | 7 | #include "scsi/constants.h" |
83c9f4ca | 8 | #include "hw/qdev.h" |
fa1d36df | 9 | #include "sysemu/block-backend.h" |
9c17d615 | 10 | #include "sysemu/blockdev.h" |
5138efec | 11 | #include "trace.h" |
9c17d615 | 12 | #include "sysemu/dma.h" |
f348b6d1 | 13 | #include "qemu/cutils.h" |
d52affa7 | 14 | |
baa1bd89 | 15 | static char *scsibus_get_dev_path(DeviceState *dev); |
db07c0f8 | 16 | static char *scsibus_get_fw_dev_path(DeviceState *dev); |
71544d30 | 17 | static void scsi_req_dequeue(SCSIRequest *req); |
84642435 AH |
18 | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len); |
19 | static void scsi_target_free_buf(SCSIRequest *req); | |
db07c0f8 | 20 | |
3cb75a7c PB |
21 | static Property scsi_props[] = { |
22 | DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), | |
23 | DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), | |
24 | DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), | |
25 | DEFINE_PROP_END_OF_LIST(), | |
26 | }; | |
27 | ||
0d936928 AL |
28 | static void scsi_bus_class_init(ObjectClass *klass, void *data) |
29 | { | |
30 | BusClass *k = BUS_CLASS(klass); | |
bddd763a | 31 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); |
0d936928 AL |
32 | |
33 | k->get_dev_path = scsibus_get_dev_path; | |
34 | k->get_fw_dev_path = scsibus_get_fw_dev_path; | |
bddd763a | 35 | hc->unplug = qdev_simple_device_unplug_cb; |
0d936928 AL |
36 | } |
37 | ||
38 | static const TypeInfo scsi_bus_info = { | |
39 | .name = TYPE_SCSI_BUS, | |
40 | .parent = TYPE_BUS, | |
41 | .instance_size = sizeof(SCSIBus), | |
42 | .class_init = scsi_bus_class_init, | |
bddd763a IM |
43 | .interfaces = (InterfaceInfo[]) { |
44 | { TYPE_HOTPLUG_HANDLER }, | |
45 | { } | |
46 | } | |
d52affa7 GH |
47 | }; |
48 | static int next_scsi_bus; | |
49 | ||
a818a4b6 | 50 | static void scsi_device_realize(SCSIDevice *s, Error **errp) |
b9eea3e6 AL |
51 | { |
52 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
a818a4b6 FZ |
53 | if (sc->realize) { |
54 | sc->realize(s, errp); | |
b9eea3e6 | 55 | } |
b9eea3e6 AL |
56 | } |
57 | ||
ff34c32c PB |
58 | int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, |
59 | void *hba_private) | |
60 | { | |
61 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | |
62 | int rc; | |
63 | ||
64 | assert(cmd->len == 0); | |
65 | rc = scsi_req_parse_cdb(dev, cmd, buf); | |
66 | if (bus->info->parse_cdb) { | |
67 | rc = bus->info->parse_cdb(dev, cmd, buf, hba_private); | |
68 | } | |
69 | return rc; | |
70 | } | |
71 | ||
b9eea3e6 AL |
72 | static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun, |
73 | uint8_t *buf, void *hba_private) | |
74 | { | |
75 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
76 | if (sc->alloc_req) { | |
77 | return sc->alloc_req(s, tag, lun, buf, hba_private); | |
78 | } | |
79 | ||
80 | return NULL; | |
81 | } | |
82 | ||
8d72db68 | 83 | void scsi_device_unit_attention_reported(SCSIDevice *s) |
b9eea3e6 AL |
84 | { |
85 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
86 | if (sc->unit_attention_reported) { | |
87 | sc->unit_attention_reported(s); | |
88 | } | |
89 | } | |
90 | ||
d52affa7 | 91 | /* Create a scsi bus, and attach devices to it. */ |
b1187b51 AF |
92 | void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, |
93 | const SCSIBusInfo *info, const char *bus_name) | |
d52affa7 | 94 | { |
fb17dfe0 | 95 | qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name); |
d52affa7 | 96 | bus->busnr = next_scsi_bus++; |
afd4030c | 97 | bus->info = info; |
bddd763a | 98 | qbus_set_bus_hotplug_handler(BUS(bus), &error_abort); |
d52affa7 GH |
99 | } |
100 | ||
71544d30 PB |
101 | static void scsi_dma_restart_bh(void *opaque) |
102 | { | |
103 | SCSIDevice *s = opaque; | |
104 | SCSIRequest *req, *next; | |
105 | ||
106 | qemu_bh_delete(s->bh); | |
107 | s->bh = NULL; | |
108 | ||
1919631e | 109 | aio_context_acquire(blk_get_aio_context(s->conf.blk)); |
71544d30 PB |
110 | QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { |
111 | scsi_req_ref(req); | |
112 | if (req->retry) { | |
113 | req->retry = false; | |
114 | switch (req->cmd.mode) { | |
115 | case SCSI_XFER_FROM_DEV: | |
116 | case SCSI_XFER_TO_DEV: | |
117 | scsi_req_continue(req); | |
118 | break; | |
119 | case SCSI_XFER_NONE: | |
120 | scsi_req_dequeue(req); | |
121 | scsi_req_enqueue(req); | |
122 | break; | |
123 | } | |
124 | } | |
125 | scsi_req_unref(req); | |
126 | } | |
1919631e | 127 | aio_context_release(blk_get_aio_context(s->conf.blk)); |
71544d30 PB |
128 | } |
129 | ||
130 | void scsi_req_retry(SCSIRequest *req) | |
131 | { | |
132 | /* No need to save a reference, because scsi_dma_restart_bh just | |
133 | * looks at the request list. */ | |
134 | req->retry = true; | |
135 | } | |
136 | ||
137 | static void scsi_dma_restart_cb(void *opaque, int running, RunState state) | |
138 | { | |
139 | SCSIDevice *s = opaque; | |
140 | ||
141 | if (!running) { | |
142 | return; | |
143 | } | |
144 | if (!s->bh) { | |
d223c104 PB |
145 | AioContext *ctx = blk_get_aio_context(s->conf.blk); |
146 | s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s); | |
71544d30 PB |
147 | qemu_bh_schedule(s->bh); |
148 | } | |
149 | } | |
150 | ||
a818a4b6 | 151 | static void scsi_qdev_realize(DeviceState *qdev, Error **errp) |
d52affa7 | 152 | { |
b9eea3e6 | 153 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
d52affa7 | 154 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); |
7e0380b9 | 155 | SCSIDevice *d; |
a818a4b6 | 156 | Error *local_err = NULL; |
d52affa7 | 157 | |
0d3545e7 | 158 | if (dev->channel > bus->info->max_channel) { |
a818a4b6 FZ |
159 | error_setg(errp, "bad scsi channel id: %d", dev->channel); |
160 | return; | |
0d3545e7 | 161 | } |
7e0380b9 | 162 | if (dev->id != -1 && dev->id > bus->info->max_target) { |
a818a4b6 FZ |
163 | error_setg(errp, "bad scsi device id: %d", dev->id); |
164 | return; | |
d52affa7 | 165 | } |
d3d250bd | 166 | if (dev->lun != -1 && dev->lun > bus->info->max_lun) { |
a818a4b6 FZ |
167 | error_setg(errp, "bad scsi device lun: %d", dev->lun); |
168 | return; | |
d3d250bd | 169 | } |
d52affa7 | 170 | |
7e0380b9 PB |
171 | if (dev->id == -1) { |
172 | int id = -1; | |
173 | if (dev->lun == -1) { | |
174 | dev->lun = 0; | |
175 | } | |
176 | do { | |
0d3545e7 | 177 | d = scsi_device_find(bus, dev->channel, ++id, dev->lun); |
d3d250bd PB |
178 | } while (d && d->lun == dev->lun && id < bus->info->max_target); |
179 | if (d && d->lun == dev->lun) { | |
a818a4b6 FZ |
180 | error_setg(errp, "no free target"); |
181 | return; | |
7e0380b9 PB |
182 | } |
183 | dev->id = id; | |
184 | } else if (dev->lun == -1) { | |
185 | int lun = -1; | |
186 | do { | |
0d3545e7 | 187 | d = scsi_device_find(bus, dev->channel, dev->id, ++lun); |
7e0380b9 | 188 | } while (d && d->lun == lun && lun < bus->info->max_lun); |
d3d250bd | 189 | if (d && d->lun == lun) { |
a818a4b6 FZ |
190 | error_setg(errp, "no free lun"); |
191 | return; | |
7e0380b9 PB |
192 | } |
193 | dev->lun = lun; | |
194 | } else { | |
0d3545e7 | 195 | d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); |
d3d250bd PB |
196 | assert(d); |
197 | if (d->lun == dev->lun && dev != d) { | |
a818a4b6 FZ |
198 | error_setg(errp, "lun already used by '%s'", d->qdev.id); |
199 | return; | |
7e0380b9 | 200 | } |
d52affa7 | 201 | } |
d52affa7 | 202 | |
9af99d98 | 203 | QTAILQ_INIT(&dev->requests); |
a818a4b6 FZ |
204 | scsi_device_realize(dev, &local_err); |
205 | if (local_err) { | |
206 | error_propagate(errp, local_err); | |
207 | return; | |
71544d30 | 208 | } |
a818a4b6 FZ |
209 | dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, |
210 | dev); | |
01985dcf GH |
211 | } |
212 | ||
a818a4b6 | 213 | static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp) |
01985dcf | 214 | { |
b9eea3e6 | 215 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
01985dcf | 216 | |
71544d30 PB |
217 | if (dev->vmsentry) { |
218 | qemu_del_vm_change_state_handler(dev->vmsentry); | |
219 | } | |
fb7b5c0d PB |
220 | |
221 | scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE)); | |
222 | blockdev_mark_auto_del(dev->conf.blk); | |
d52affa7 GH |
223 | } |
224 | ||
d52affa7 | 225 | /* handle legacy '-drive if=scsi,...' cmd line args */ |
4be74634 | 226 | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, |
76534da7 | 227 | int unit, bool removable, int bootindex, |
395b9539 | 228 | bool share_rw, |
b8efb36b KW |
229 | BlockdevOnError rerror, |
230 | BlockdevOnError werror, | |
caad4eb3 | 231 | const char *serial, Error **errp) |
d52affa7 GH |
232 | { |
233 | const char *driver; | |
22647504 | 234 | char *name; |
d52affa7 | 235 | DeviceState *dev; |
caad4eb3 | 236 | Error *err = NULL; |
d52affa7 | 237 | |
4be74634 | 238 | driver = blk_is_sg(blk) ? "scsi-generic" : "scsi-disk"; |
d52affa7 | 239 | dev = qdev_create(&bus->qbus, driver); |
22647504 PB |
240 | name = g_strdup_printf("legacy[%d]", unit); |
241 | object_property_add_child(OBJECT(bus), name, OBJECT(dev), NULL); | |
242 | g_free(name); | |
243 | ||
d52affa7 | 244 | qdev_prop_set_uint32(dev, "scsi-id", unit); |
ce4e7e46 | 245 | if (bootindex >= 0) { |
8dece34f GA |
246 | object_property_set_int(OBJECT(dev), bootindex, "bootindex", |
247 | &error_abort); | |
ce4e7e46 | 248 | } |
89bfe000 | 249 | if (object_property_find(OBJECT(dev), "removable", NULL)) { |
2d1fd261 SH |
250 | qdev_prop_set_bit(dev, "removable", removable); |
251 | } | |
c24e7517 | 252 | if (serial && object_property_find(OBJECT(dev), "serial", NULL)) { |
76534da7 KW |
253 | qdev_prop_set_string(dev, "serial", serial); |
254 | } | |
9b3d111a MA |
255 | qdev_prop_set_drive(dev, "drive", blk, &err); |
256 | if (err) { | |
390e90a9 | 257 | error_propagate(errp, err); |
02a5c4c9 | 258 | object_unparent(OBJECT(dev)); |
18846dee MA |
259 | return NULL; |
260 | } | |
395b9539 FZ |
261 | object_property_set_bool(OBJECT(dev), share_rw, "share-rw", &err); |
262 | if (err != NULL) { | |
263 | error_propagate(errp, err); | |
264 | object_unparent(OBJECT(dev)); | |
265 | return NULL; | |
266 | } | |
b8efb36b KW |
267 | |
268 | qdev_prop_set_enum(dev, "rerror", rerror); | |
269 | qdev_prop_set_enum(dev, "werror", werror); | |
270 | ||
caad4eb3 AF |
271 | object_property_set_bool(OBJECT(dev), true, "realized", &err); |
272 | if (err != NULL) { | |
273 | error_propagate(errp, err); | |
02a5c4c9 | 274 | object_unparent(OBJECT(dev)); |
33e66b86 | 275 | return NULL; |
caad4eb3 | 276 | } |
b9eea3e6 | 277 | return SCSI_DEVICE(dev); |
d52affa7 GH |
278 | } |
279 | ||
14545097 | 280 | void scsi_bus_legacy_handle_cmdline(SCSIBus *bus) |
d52affa7 | 281 | { |
42e766a2 | 282 | Location loc; |
d52affa7 | 283 | DriveInfo *dinfo; |
caad4eb3 | 284 | int unit; |
d52affa7 | 285 | |
42e766a2 | 286 | loc_push_none(&loc); |
d3d250bd | 287 | for (unit = 0; unit <= bus->info->max_target; unit++) { |
d52affa7 GH |
288 | dinfo = drive_get(IF_SCSI, bus->busnr, unit); |
289 | if (dinfo == NULL) { | |
290 | continue; | |
291 | } | |
42e766a2 | 292 | qemu_opts_loc_restore(dinfo->opts); |
4be74634 | 293 | scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo), |
b8efb36b KW |
294 | unit, false, -1, false, |
295 | BLOCKDEV_ON_ERROR_AUTO, | |
296 | BLOCKDEV_ON_ERROR_AUTO, | |
297 | NULL, &error_fatal); | |
d52affa7 | 298 | } |
42e766a2 | 299 | loc_pop(&loc); |
d52affa7 | 300 | } |
89b08ae1 | 301 | |
12a08998 PB |
302 | static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf) |
303 | { | |
304 | scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | |
305 | scsi_req_complete(req, CHECK_CONDITION); | |
306 | return 0; | |
307 | } | |
308 | ||
309 | static const struct SCSIReqOps reqops_invalid_field = { | |
310 | .size = sizeof(SCSIRequest), | |
311 | .send_command = scsi_invalid_field | |
312 | }; | |
313 | ||
afa46c46 PB |
314 | /* SCSIReqOps implementation for invalid commands. */ |
315 | ||
316 | static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf) | |
317 | { | |
318 | scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); | |
319 | scsi_req_complete(req, CHECK_CONDITION); | |
320 | return 0; | |
321 | } | |
322 | ||
adcf2754 | 323 | static const struct SCSIReqOps reqops_invalid_opcode = { |
afa46c46 PB |
324 | .size = sizeof(SCSIRequest), |
325 | .send_command = scsi_invalid_command | |
326 | }; | |
327 | ||
6dc06f08 PB |
328 | /* SCSIReqOps implementation for unit attention conditions. */ |
329 | ||
330 | static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf) | |
331 | { | |
0bf8264e | 332 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { |
6dc06f08 PB |
333 | scsi_req_build_sense(req, req->dev->unit_attention); |
334 | } else if (req->bus->unit_attention.key == UNIT_ATTENTION) { | |
335 | scsi_req_build_sense(req, req->bus->unit_attention); | |
336 | } | |
337 | scsi_req_complete(req, CHECK_CONDITION); | |
338 | return 0; | |
339 | } | |
340 | ||
adcf2754 | 341 | static const struct SCSIReqOps reqops_unit_attention = { |
6dc06f08 PB |
342 | .size = sizeof(SCSIRequest), |
343 | .send_command = scsi_unit_attention | |
344 | }; | |
345 | ||
fdaef069 PB |
346 | /* SCSIReqOps implementation for REPORT LUNS and for commands sent to |
347 | an invalid LUN. */ | |
348 | ||
349 | typedef struct SCSITargetReq SCSITargetReq; | |
350 | ||
351 | struct SCSITargetReq { | |
352 | SCSIRequest req; | |
353 | int len; | |
84642435 AH |
354 | uint8_t *buf; |
355 | int buf_len; | |
fdaef069 PB |
356 | }; |
357 | ||
358 | static void store_lun(uint8_t *outbuf, int lun) | |
359 | { | |
360 | if (lun < 256) { | |
361 | outbuf[1] = lun; | |
362 | return; | |
363 | } | |
364 | outbuf[1] = (lun & 255); | |
365 | outbuf[0] = (lun >> 8) | 0x40; | |
366 | } | |
367 | ||
368 | static bool scsi_target_emulate_report_luns(SCSITargetReq *r) | |
369 | { | |
0866aca1 | 370 | BusChild *kid; |
ba74307c | 371 | int i, len, n; |
0d3545e7 | 372 | int channel, id; |
ba74307c PB |
373 | bool found_lun0; |
374 | ||
fdaef069 PB |
375 | if (r->req.cmd.xfer < 16) { |
376 | return false; | |
377 | } | |
378 | if (r->req.cmd.buf[2] > 2) { | |
379 | return false; | |
380 | } | |
0d3545e7 | 381 | channel = r->req.dev->channel; |
ba74307c PB |
382 | id = r->req.dev->id; |
383 | found_lun0 = false; | |
384 | n = 0; | |
0866aca1 AL |
385 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
386 | DeviceState *qdev = kid->child; | |
b9eea3e6 | 387 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 388 | |
0d3545e7 | 389 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
390 | if (dev->lun == 0) { |
391 | found_lun0 = true; | |
392 | } | |
393 | n += 8; | |
394 | } | |
395 | } | |
396 | if (!found_lun0) { | |
397 | n += 8; | |
398 | } | |
ba74307c | 399 | |
84642435 AH |
400 | scsi_target_alloc_buf(&r->req, n + 8); |
401 | ||
402 | len = MIN(n + 8, r->req.cmd.xfer & ~7); | |
fdaef069 | 403 | memset(r->buf, 0, len); |
84642435 | 404 | stl_be_p(&r->buf[0], n); |
ba74307c | 405 | i = found_lun0 ? 8 : 16; |
0866aca1 AL |
406 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
407 | DeviceState *qdev = kid->child; | |
b9eea3e6 | 408 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 409 | |
0d3545e7 | 410 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
411 | store_lun(&r->buf[i], dev->lun); |
412 | i += 8; | |
413 | } | |
fdaef069 | 414 | } |
ba74307c PB |
415 | assert(i == n + 8); |
416 | r->len = len; | |
fdaef069 PB |
417 | return true; |
418 | } | |
419 | ||
420 | static bool scsi_target_emulate_inquiry(SCSITargetReq *r) | |
421 | { | |
422 | assert(r->req.dev->lun != r->req.lun); | |
84642435 AH |
423 | |
424 | scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN); | |
425 | ||
fdaef069 PB |
426 | if (r->req.cmd.buf[1] & 0x2) { |
427 | /* Command support data - optional, not implemented */ | |
428 | return false; | |
429 | } | |
430 | ||
431 | if (r->req.cmd.buf[1] & 0x1) { | |
432 | /* Vital product data */ | |
433 | uint8_t page_code = r->req.cmd.buf[2]; | |
fdaef069 PB |
434 | r->buf[r->len++] = page_code ; /* this page */ |
435 | r->buf[r->len++] = 0x00; | |
436 | ||
437 | switch (page_code) { | |
438 | case 0x00: /* Supported page codes, mandatory */ | |
439 | { | |
440 | int pages; | |
441 | pages = r->len++; | |
442 | r->buf[r->len++] = 0x00; /* list of supported pages (this page) */ | |
443 | r->buf[pages] = r->len - pages - 1; /* number of pages */ | |
444 | break; | |
445 | } | |
446 | default: | |
447 | return false; | |
448 | } | |
449 | /* done with EVPD */ | |
84642435 | 450 | assert(r->len < r->buf_len); |
fdaef069 PB |
451 | r->len = MIN(r->req.cmd.xfer, r->len); |
452 | return true; | |
453 | } | |
454 | ||
455 | /* Standard INQUIRY data */ | |
456 | if (r->req.cmd.buf[2] != 0) { | |
457 | return false; | |
458 | } | |
459 | ||
460 | /* PAGE CODE == 0 */ | |
84642435 | 461 | r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN); |
fdaef069 PB |
462 | memset(r->buf, 0, r->len); |
463 | if (r->req.lun != 0) { | |
464 | r->buf[0] = TYPE_NO_LUN; | |
465 | } else { | |
466 | r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE; | |
467 | r->buf[2] = 5; /* Version */ | |
468 | r->buf[3] = 2 | 0x10; /* HiSup, response data format */ | |
469 | r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */ | |
afd4030c | 470 | r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */ |
fdaef069 PB |
471 | memcpy(&r->buf[8], "QEMU ", 8); |
472 | memcpy(&r->buf[16], "QEMU TARGET ", 16); | |
35c2c8dc | 473 | pstrcpy((char *) &r->buf[32], 4, qemu_hw_version()); |
fdaef069 PB |
474 | } |
475 | return true; | |
476 | } | |
477 | ||
6959e508 JL |
478 | static size_t scsi_sense_len(SCSIRequest *req) |
479 | { | |
480 | if (req->dev->type == TYPE_SCANNER) | |
481 | return SCSI_SENSE_LEN_SCANNER; | |
482 | else | |
483 | return SCSI_SENSE_LEN; | |
484 | } | |
485 | ||
fdaef069 PB |
486 | static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf) |
487 | { | |
488 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
b07fbce6 | 489 | int fixed_sense = (req->cmd.buf[1] & 1) == 0; |
fdaef069 | 490 | |
b07fbce6 HR |
491 | if (req->lun != 0 && |
492 | buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) { | |
ded6ddc5 HR |
493 | scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED)); |
494 | scsi_req_complete(req, CHECK_CONDITION); | |
495 | return 0; | |
496 | } | |
fdaef069 PB |
497 | switch (buf[0]) { |
498 | case REPORT_LUNS: | |
499 | if (!scsi_target_emulate_report_luns(r)) { | |
500 | goto illegal_request; | |
501 | } | |
502 | break; | |
503 | case INQUIRY: | |
504 | if (!scsi_target_emulate_inquiry(r)) { | |
505 | goto illegal_request; | |
506 | } | |
507 | break; | |
739df215 | 508 | case REQUEST_SENSE: |
6959e508 | 509 | scsi_target_alloc_buf(&r->req, scsi_sense_len(req)); |
b07fbce6 HR |
510 | if (req->lun != 0) { |
511 | const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED); | |
512 | ||
f68d98b2 PB |
513 | r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer, |
514 | sense, fixed_sense); | |
b07fbce6 HR |
515 | } else { |
516 | r->len = scsi_device_get_sense(r->req.dev, r->buf, | |
517 | MIN(req->cmd.xfer, r->buf_len), | |
518 | fixed_sense); | |
519 | } | |
3653d8c4 | 520 | if (r->req.dev->sense_is_ua) { |
b9eea3e6 | 521 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
522 | r->req.dev->sense_len = 0; |
523 | r->req.dev->sense_is_ua = false; | |
524 | } | |
739df215 | 525 | break; |
1cb27d92 PB |
526 | case TEST_UNIT_READY: |
527 | break; | |
fdaef069 | 528 | default: |
ded6ddc5 | 529 | scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); |
fdaef069 PB |
530 | scsi_req_complete(req, CHECK_CONDITION); |
531 | return 0; | |
532 | illegal_request: | |
533 | scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | |
534 | scsi_req_complete(req, CHECK_CONDITION); | |
535 | return 0; | |
536 | } | |
537 | ||
538 | if (!r->len) { | |
539 | scsi_req_complete(req, GOOD); | |
540 | } | |
541 | return r->len; | |
542 | } | |
543 | ||
544 | static void scsi_target_read_data(SCSIRequest *req) | |
545 | { | |
546 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
547 | uint32_t n; | |
548 | ||
549 | n = r->len; | |
550 | if (n > 0) { | |
551 | r->len = 0; | |
552 | scsi_req_data(&r->req, n); | |
553 | } else { | |
554 | scsi_req_complete(&r->req, GOOD); | |
555 | } | |
556 | } | |
557 | ||
558 | static uint8_t *scsi_target_get_buf(SCSIRequest *req) | |
559 | { | |
560 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
561 | ||
562 | return r->buf; | |
563 | } | |
564 | ||
84642435 AH |
565 | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len) |
566 | { | |
567 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
568 | ||
569 | r->buf = g_malloc(len); | |
570 | r->buf_len = len; | |
571 | ||
572 | return r->buf; | |
573 | } | |
574 | ||
575 | static void scsi_target_free_buf(SCSIRequest *req) | |
576 | { | |
577 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
578 | ||
579 | g_free(r->buf); | |
580 | } | |
581 | ||
adcf2754 | 582 | static const struct SCSIReqOps reqops_target_command = { |
fdaef069 PB |
583 | .size = sizeof(SCSITargetReq), |
584 | .send_command = scsi_target_send_command, | |
585 | .read_data = scsi_target_read_data, | |
586 | .get_buf = scsi_target_get_buf, | |
84642435 | 587 | .free_req = scsi_target_free_buf, |
fdaef069 PB |
588 | }; |
589 | ||
590 | ||
adcf2754 PB |
591 | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, |
592 | uint32_t tag, uint32_t lun, void *hba_private) | |
89b08ae1 GH |
593 | { |
594 | SCSIRequest *req; | |
cac3c384 PB |
595 | SCSIBus *bus = scsi_bus_from_device(d); |
596 | BusState *qbus = BUS(bus); | |
61e68b3f FZ |
597 | const int memset_off = offsetof(SCSIRequest, sense) |
598 | + sizeof(req->sense); | |
89b08ae1 | 599 | |
633dccb4 | 600 | req = g_malloc(reqops->size); |
61e68b3f | 601 | memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off); |
5c6c0e51 | 602 | req->refcount = 1; |
cac3c384 | 603 | req->bus = bus; |
89b08ae1 GH |
604 | req->dev = d; |
605 | req->tag = tag; | |
606 | req->lun = lun; | |
c5bf71a9 | 607 | req->hba_private = hba_private; |
ed3a34a3 | 608 | req->status = -1; |
8dbd4574 | 609 | req->ops = reqops; |
cac3c384 PB |
610 | object_ref(OBJECT(d)); |
611 | object_ref(OBJECT(qbus->parent)); | |
8e0a9320 | 612 | notifier_list_init(&req->cancel_notifiers); |
5138efec | 613 | trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); |
89b08ae1 GH |
614 | return req; |
615 | } | |
616 | ||
c5bf71a9 | 617 | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, |
c39ce112 | 618 | uint8_t *buf, void *hba_private) |
43a2b339 | 619 | { |
6dc06f08 | 620 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); |
769998a1 | 621 | const SCSIReqOps *ops; |
ff34c32c | 622 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d); |
c39ce112 | 623 | SCSIRequest *req; |
769998a1 PB |
624 | SCSICommand cmd = { .len = 0 }; |
625 | int ret; | |
626 | ||
627 | if ((d->unit_attention.key == UNIT_ATTENTION || | |
628 | bus->unit_attention.key == UNIT_ATTENTION) && | |
629 | (buf[0] != INQUIRY && | |
630 | buf[0] != REPORT_LUNS && | |
631 | buf[0] != GET_CONFIGURATION && | |
632 | buf[0] != GET_EVENT_STATUS_NOTIFICATION && | |
633 | ||
634 | /* | |
635 | * If we already have a pending unit attention condition, | |
636 | * report this one before triggering another one. | |
637 | */ | |
638 | !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) { | |
639 | ops = &reqops_unit_attention; | |
640 | } else if (lun != d->lun || | |
641 | buf[0] == REPORT_LUNS || | |
642 | (buf[0] == REQUEST_SENSE && d->sense_len)) { | |
643 | ops = &reqops_target_command; | |
644 | } else { | |
645 | ops = NULL; | |
646 | } | |
afa46c46 | 647 | |
ff34c32c PB |
648 | if (ops != NULL || !sc->parse_cdb) { |
649 | ret = scsi_req_parse_cdb(d, &cmd, buf); | |
650 | } else { | |
651 | ret = sc->parse_cdb(d, &cmd, buf, hba_private); | |
652 | } | |
653 | ||
769998a1 | 654 | if (ret != 0) { |
afa46c46 PB |
655 | trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]); |
656 | req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private); | |
657 | } else { | |
769998a1 | 658 | assert(cmd.len != 0); |
afa46c46 PB |
659 | trace_scsi_req_parsed(d->id, lun, tag, buf[0], |
660 | cmd.mode, cmd.xfer); | |
3b6ffe50 | 661 | if (cmd.lba != -1) { |
afa46c46 PB |
662 | trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0], |
663 | cmd.lba); | |
664 | } | |
fdaef069 | 665 | |
12a08998 PB |
666 | if (cmd.xfer > INT32_MAX) { |
667 | req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private); | |
769998a1 PB |
668 | } else if (ops) { |
669 | req = scsi_req_alloc(ops, d, tag, lun, hba_private); | |
fdaef069 | 670 | } else { |
b9eea3e6 | 671 | req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); |
fdaef069 | 672 | } |
afa46c46 PB |
673 | } |
674 | ||
675 | req->cmd = cmd; | |
01e95455 PB |
676 | req->resid = req->cmd.xfer; |
677 | ||
98254542 PB |
678 | switch (buf[0]) { |
679 | case INQUIRY: | |
680 | trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]); | |
681 | break; | |
682 | case TEST_UNIT_READY: | |
683 | trace_scsi_test_unit_ready(d->id, lun, tag); | |
684 | break; | |
685 | case REPORT_LUNS: | |
686 | trace_scsi_report_luns(d->id, lun, tag); | |
687 | break; | |
688 | case REQUEST_SENSE: | |
689 | trace_scsi_request_sense(d->id, lun, tag); | |
690 | break; | |
691 | default: | |
692 | break; | |
693 | } | |
694 | ||
c39ce112 | 695 | return req; |
43a2b339 PB |
696 | } |
697 | ||
0c34459b PB |
698 | uint8_t *scsi_req_get_buf(SCSIRequest *req) |
699 | { | |
12010e7b | 700 | return req->ops->get_buf(req); |
0c34459b PB |
701 | } |
702 | ||
6dc06f08 PB |
703 | static void scsi_clear_unit_attention(SCSIRequest *req) |
704 | { | |
705 | SCSISense *ua; | |
706 | if (req->dev->unit_attention.key != UNIT_ATTENTION && | |
707 | req->bus->unit_attention.key != UNIT_ATTENTION) { | |
708 | return; | |
709 | } | |
710 | ||
711 | /* | |
712 | * If an INQUIRY command enters the enabled command state, | |
713 | * the device server shall [not] clear any unit attention condition; | |
714 | * See also MMC-6, paragraphs 6.5 and 6.6.2. | |
715 | */ | |
716 | if (req->cmd.buf[0] == INQUIRY || | |
717 | req->cmd.buf[0] == GET_CONFIGURATION || | |
718 | req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) { | |
719 | return; | |
720 | } | |
721 | ||
722 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { | |
723 | ua = &req->dev->unit_attention; | |
724 | } else { | |
725 | ua = &req->bus->unit_attention; | |
726 | } | |
727 | ||
728 | /* | |
729 | * If a REPORT LUNS command enters the enabled command state, [...] | |
730 | * the device server shall clear any pending unit attention condition | |
731 | * with an additional sense code of REPORTED LUNS DATA HAS CHANGED. | |
732 | */ | |
733 | if (req->cmd.buf[0] == REPORT_LUNS && | |
734 | !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc && | |
735 | ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) { | |
736 | return; | |
737 | } | |
738 | ||
739 | *ua = SENSE_CODE(NO_SENSE); | |
740 | } | |
741 | ||
74382217 HR |
742 | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) |
743 | { | |
6dc06f08 PB |
744 | int ret; |
745 | ||
b45ef674 PB |
746 | assert(len >= 14); |
747 | if (!req->sense_len) { | |
74382217 HR |
748 | return 0; |
749 | } | |
6dc06f08 | 750 | |
37b6045c | 751 | ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true); |
6dc06f08 PB |
752 | |
753 | /* | |
754 | * FIXME: clearing unit attention conditions upon autosense should be done | |
755 | * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b | |
756 | * (SAM-5, 5.14). | |
757 | * | |
758 | * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and | |
759 | * 10b for HBAs that do not support it (do not call scsi_req_get_sense). | |
3653d8c4 | 760 | * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b. |
6dc06f08 | 761 | */ |
3653d8c4 | 762 | if (req->dev->sense_is_ua) { |
b9eea3e6 | 763 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
764 | req->dev->sense_len = 0; |
765 | req->dev->sense_is_ua = false; | |
766 | } | |
6dc06f08 | 767 | return ret; |
b45ef674 PB |
768 | } |
769 | ||
770 | int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed) | |
771 | { | |
37b6045c | 772 | return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed); |
b45ef674 PB |
773 | } |
774 | ||
775 | void scsi_req_build_sense(SCSIRequest *req, SCSISense sense) | |
776 | { | |
777 | trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag, | |
778 | sense.key, sense.asc, sense.ascq); | |
a3760467 | 779 | req->sense_len = scsi_build_sense(req->sense, sense); |
74382217 HR |
780 | } |
781 | ||
63f740dd | 782 | static void scsi_req_enqueue_internal(SCSIRequest *req) |
89b08ae1 | 783 | { |
5c6c0e51 HR |
784 | assert(!req->enqueued); |
785 | scsi_req_ref(req); | |
3d5aba97 PB |
786 | if (req->bus->info->get_sg_list) { |
787 | req->sg = req->bus->info->get_sg_list(req); | |
788 | } else { | |
789 | req->sg = NULL; | |
790 | } | |
5c6c0e51 HR |
791 | req->enqueued = true; |
792 | QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); | |
63f740dd | 793 | } |
fc4f0754 | 794 | |
63f740dd PB |
795 | int32_t scsi_req_enqueue(SCSIRequest *req) |
796 | { | |
797 | int32_t rc; | |
798 | ||
799 | assert(!req->retry); | |
800 | scsi_req_enqueue_internal(req); | |
fc4f0754 | 801 | scsi_req_ref(req); |
c39ce112 | 802 | rc = req->ops->send_command(req, req->cmd.buf); |
fc4f0754 PB |
803 | scsi_req_unref(req); |
804 | return rc; | |
89b08ae1 GH |
805 | } |
806 | ||
a1f0cce2 | 807 | static void scsi_req_dequeue(SCSIRequest *req) |
e8637c90 | 808 | { |
5138efec | 809 | trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); |
71544d30 | 810 | req->retry = false; |
e8637c90 JK |
811 | if (req->enqueued) { |
812 | QTAILQ_REMOVE(&req->dev->requests, req, next); | |
813 | req->enqueued = false; | |
ad2d30f7 | 814 | scsi_req_unref(req); |
e8637c90 JK |
815 | } |
816 | } | |
817 | ||
06b86357 PB |
818 | static int scsi_get_performance_length(int num_desc, int type, int data_type) |
819 | { | |
820 | /* MMC-6, paragraph 6.7. */ | |
821 | switch (type) { | |
822 | case 0: | |
823 | if ((data_type & 3) == 0) { | |
824 | /* Each descriptor is as in Table 295 - Nominal performance. */ | |
825 | return 16 * num_desc + 8; | |
826 | } else { | |
827 | /* Each descriptor is as in Table 296 - Exceptions. */ | |
828 | return 6 * num_desc + 8; | |
829 | } | |
830 | case 1: | |
831 | case 4: | |
832 | case 5: | |
833 | return 8 * num_desc + 8; | |
834 | case 2: | |
835 | return 2048 * num_desc + 8; | |
836 | case 3: | |
837 | return 16 * num_desc + 8; | |
838 | default: | |
839 | return 8; | |
840 | } | |
841 | } | |
842 | ||
e4b65262 CM |
843 | static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf) |
844 | { | |
845 | int byte_block = (buf[2] >> 2) & 0x1; | |
846 | int type = (buf[2] >> 4) & 0x1; | |
847 | int xfer_unit; | |
848 | ||
849 | if (byte_block) { | |
850 | if (type) { | |
851 | xfer_unit = dev->blocksize; | |
852 | } else { | |
853 | xfer_unit = 512; | |
854 | } | |
855 | } else { | |
856 | xfer_unit = 1; | |
857 | } | |
858 | ||
859 | return xfer_unit; | |
860 | } | |
861 | ||
1894df02 | 862 | static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf) |
e4b65262 CM |
863 | { |
864 | int length = buf[2] & 0x3; | |
865 | int xfer; | |
866 | int unit = ata_passthrough_xfer_unit(dev, buf); | |
867 | ||
868 | switch (length) { | |
869 | case 0: | |
870 | case 3: /* USB-specific. */ | |
d83c951c | 871 | default: |
e4b65262 CM |
872 | xfer = 0; |
873 | break; | |
874 | case 1: | |
875 | xfer = buf[3]; | |
876 | break; | |
877 | case 2: | |
878 | xfer = buf[4]; | |
879 | break; | |
880 | } | |
881 | ||
882 | return xfer * unit; | |
883 | } | |
884 | ||
1894df02 | 885 | static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf) |
e4b65262 CM |
886 | { |
887 | int extend = buf[1] & 0x1; | |
888 | int length = buf[2] & 0x3; | |
889 | int xfer; | |
890 | int unit = ata_passthrough_xfer_unit(dev, buf); | |
891 | ||
892 | switch (length) { | |
893 | case 0: | |
894 | case 3: /* USB-specific. */ | |
d83c951c | 895 | default: |
e4b65262 CM |
896 | xfer = 0; |
897 | break; | |
898 | case 1: | |
899 | xfer = buf[4]; | |
900 | xfer |= (extend ? buf[3] << 8 : 0); | |
901 | break; | |
902 | case 2: | |
903 | xfer = buf[6]; | |
904 | xfer |= (extend ? buf[5] << 8 : 0); | |
905 | break; | |
906 | } | |
907 | ||
908 | return xfer * unit; | |
909 | } | |
910 | ||
1894df02 | 911 | static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
bb729f75 | 912 | { |
1894df02 | 913 | cmd->xfer = scsi_cdb_xfer(buf); |
2599aece | 914 | switch (buf[0]) { |
2ec749cb | 915 | case TEST_UNIT_READY: |
5e30a07d | 916 | case REWIND: |
2ec749cb | 917 | case START_STOP: |
00a01ad4 | 918 | case SET_CAPACITY: |
2ec749cb | 919 | case WRITE_FILEMARKS: |
06b86357 | 920 | case WRITE_FILEMARKS_16: |
2ec749cb | 921 | case SPACE: |
a5e3d9ef BK |
922 | case RESERVE: |
923 | case RELEASE: | |
2ec749cb GH |
924 | case ERASE: |
925 | case ALLOW_MEDIUM_REMOVAL: | |
2ec749cb GH |
926 | case SEEK_10: |
927 | case SYNCHRONIZE_CACHE: | |
06b86357 PB |
928 | case SYNCHRONIZE_CACHE_16: |
929 | case LOCATE_16: | |
2ec749cb | 930 | case LOCK_UNLOCK_CACHE: |
2ec749cb GH |
931 | case SET_CD_SPEED: |
932 | case SET_LIMITS: | |
5e30a07d | 933 | case WRITE_LONG_10: |
2ec749cb | 934 | case UPDATE_BLOCK: |
06b86357 PB |
935 | case RESERVE_TRACK: |
936 | case SET_READ_AHEAD: | |
937 | case PRE_FETCH: | |
938 | case PRE_FETCH_16: | |
939 | case ALLOW_OVERWRITE: | |
2599aece | 940 | cmd->xfer = 0; |
2ec749cb | 941 | break; |
d12ad44c PB |
942 | case VERIFY_10: |
943 | case VERIFY_12: | |
944 | case VERIFY_16: | |
945 | if ((buf[1] & 2) == 0) { | |
946 | cmd->xfer = 0; | |
7ef8cf9a | 947 | } else if ((buf[1] & 4) != 0) { |
d12ad44c PB |
948 | cmd->xfer = 1; |
949 | } | |
950 | cmd->xfer *= dev->blocksize; | |
951 | break; | |
2ec749cb GH |
952 | case MODE_SENSE: |
953 | break; | |
5e30a07d | 954 | case WRITE_SAME_10: |
a5ee9085 | 955 | case WRITE_SAME_16: |
4397a018 | 956 | cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize; |
2ec749cb | 957 | break; |
5e30a07d | 958 | case READ_CAPACITY_10: |
2599aece | 959 | cmd->xfer = 8; |
2ec749cb GH |
960 | break; |
961 | case READ_BLOCK_LIMITS: | |
2599aece | 962 | cmd->xfer = 6; |
2ec749cb | 963 | break; |
2ec749cb | 964 | case SEND_VOLUME_TAG: |
06b86357 PB |
965 | /* GPCMD_SET_STREAMING from multimedia commands. */ |
966 | if (dev->type == TYPE_ROM) { | |
967 | cmd->xfer = buf[10] | (buf[9] << 8); | |
968 | } else { | |
969 | cmd->xfer = buf[9] | (buf[8] << 8); | |
970 | } | |
2ec749cb | 971 | break; |
f62d0594 PB |
972 | case WRITE_6: |
973 | /* length 0 means 256 blocks */ | |
974 | if (cmd->xfer == 0) { | |
975 | cmd->xfer = 256; | |
976 | } | |
c4ce4c4b | 977 | /* fall through */ |
2ec749cb | 978 | case WRITE_10: |
5e30a07d | 979 | case WRITE_VERIFY_10: |
2ec749cb GH |
980 | case WRITE_12: |
981 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
982 | case WRITE_16: |
983 | case WRITE_VERIFY_16: | |
2599aece | 984 | cmd->xfer *= dev->blocksize; |
2ec749cb | 985 | break; |
2ec749cb GH |
986 | case READ_6: |
987 | case READ_REVERSE: | |
f62d0594 PB |
988 | /* length 0 means 256 blocks */ |
989 | if (cmd->xfer == 0) { | |
990 | cmd->xfer = 256; | |
991 | } | |
c4ce4c4b | 992 | /* fall through */ |
f62d0594 | 993 | case READ_10: |
2ec749cb | 994 | case READ_12: |
bd536cf3 | 995 | case READ_16: |
2599aece | 996 | cmd->xfer *= dev->blocksize; |
2ec749cb | 997 | break; |
06b86357 PB |
998 | case FORMAT_UNIT: |
999 | /* MMC mandates the parameter list to be 12-bytes long. Parameters | |
1000 | * for block devices are restricted to the header right now. */ | |
1001 | if (dev->type == TYPE_ROM && (buf[1] & 16)) { | |
1002 | cmd->xfer = 12; | |
1003 | } else { | |
1004 | cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4); | |
1005 | } | |
1006 | break; | |
2ec749cb | 1007 | case INQUIRY: |
06b86357 PB |
1008 | case RECEIVE_DIAGNOSTIC: |
1009 | case SEND_DIAGNOSTIC: | |
2599aece | 1010 | cmd->xfer = buf[4] | (buf[3] << 8); |
2ec749cb | 1011 | break; |
06b86357 PB |
1012 | case READ_CD: |
1013 | case READ_BUFFER: | |
1014 | case WRITE_BUFFER: | |
1015 | case SEND_CUE_SHEET: | |
1016 | cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | |
1017 | break; | |
1018 | case PERSISTENT_RESERVE_OUT: | |
1019 | cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL; | |
1020 | break; | |
1021 | case ERASE_12: | |
1022 | if (dev->type == TYPE_ROM) { | |
1023 | /* MMC command GET PERFORMANCE. */ | |
1024 | cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8), | |
1025 | buf[10], buf[1] & 0x1f); | |
1026 | } | |
1027 | break; | |
1028 | case MECHANISM_STATUS: | |
1029 | case READ_DVD_STRUCTURE: | |
1030 | case SEND_DVD_STRUCTURE: | |
c7126d5b NB |
1031 | case MAINTENANCE_OUT: |
1032 | case MAINTENANCE_IN: | |
2599aece | 1033 | if (dev->type == TYPE_ROM) { |
c7126d5b | 1034 | /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */ |
2599aece | 1035 | cmd->xfer = buf[9] | (buf[8] << 8); |
c7126d5b NB |
1036 | } |
1037 | break; | |
e4b65262 CM |
1038 | case ATA_PASSTHROUGH_12: |
1039 | if (dev->type == TYPE_ROM) { | |
1040 | /* BLANK command of MMC */ | |
1041 | cmd->xfer = 0; | |
1042 | } else { | |
1894df02 | 1043 | cmd->xfer = ata_passthrough_12_xfer(dev, buf); |
e4b65262 CM |
1044 | } |
1045 | break; | |
1046 | case ATA_PASSTHROUGH_16: | |
1894df02 | 1047 | cmd->xfer = ata_passthrough_16_xfer(dev, buf); |
e4b65262 | 1048 | break; |
2ec749cb GH |
1049 | } |
1050 | return 0; | |
1051 | } | |
1052 | ||
1894df02 | 1053 | static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
2ec749cb | 1054 | { |
2599aece | 1055 | switch (buf[0]) { |
2ec749cb | 1056 | /* stream commands */ |
06b86357 PB |
1057 | case ERASE_12: |
1058 | case ERASE_16: | |
1059 | cmd->xfer = 0; | |
1060 | break; | |
2ec749cb GH |
1061 | case READ_6: |
1062 | case READ_REVERSE: | |
1063 | case RECOVER_BUFFERED_DATA: | |
1064 | case WRITE_6: | |
2599aece PB |
1065 | cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); |
1066 | if (buf[1] & 0x01) { /* fixed */ | |
1067 | cmd->xfer *= dev->blocksize; | |
1068 | } | |
2ec749cb | 1069 | break; |
065c2599 PB |
1070 | case READ_16: |
1071 | case READ_REVERSE_16: | |
1072 | case VERIFY_16: | |
1073 | case WRITE_16: | |
065c2599 PB |
1074 | cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16); |
1075 | if (buf[1] & 0x01) { /* fixed */ | |
1076 | cmd->xfer *= dev->blocksize; | |
1077 | } | |
1078 | break; | |
2ec749cb | 1079 | case REWIND: |
15e58a21 | 1080 | case LOAD_UNLOAD: |
2599aece | 1081 | cmd->xfer = 0; |
2ec749cb | 1082 | break; |
06b86357 PB |
1083 | case SPACE_16: |
1084 | cmd->xfer = buf[13] | (buf[12] << 8); | |
1085 | break; | |
1086 | case READ_POSITION: | |
9ce1bb2d CH |
1087 | switch (buf[1] & 0x1f) /* operation code */ { |
1088 | case SHORT_FORM_BLOCK_ID: | |
1089 | case SHORT_FORM_VENDOR_SPECIFIC: | |
1090 | cmd->xfer = 20; | |
1091 | break; | |
1092 | case LONG_FORM: | |
1093 | cmd->xfer = 32; | |
1094 | break; | |
1095 | case EXTENDED_FORM: | |
1096 | cmd->xfer = buf[8] | (buf[7] << 8); | |
1097 | break; | |
1098 | default: | |
1099 | return -1; | |
1100 | } | |
1101 | ||
06b86357 PB |
1102 | break; |
1103 | case FORMAT_UNIT: | |
1104 | cmd->xfer = buf[4] | (buf[3] << 8); | |
1105 | break; | |
2ec749cb GH |
1106 | /* generic commands */ |
1107 | default: | |
1894df02 | 1108 | return scsi_req_xfer(cmd, dev, buf); |
2ec749cb GH |
1109 | } |
1110 | return 0; | |
1111 | } | |
1112 | ||
1894df02 | 1113 | static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
40723a99 CH |
1114 | { |
1115 | switch (buf[0]) { | |
1116 | /* medium changer commands */ | |
1117 | case EXCHANGE_MEDIUM: | |
1118 | case INITIALIZE_ELEMENT_STATUS: | |
1119 | case INITIALIZE_ELEMENT_STATUS_WITH_RANGE: | |
1120 | case MOVE_MEDIUM: | |
1121 | case POSITION_TO_ELEMENT: | |
1122 | cmd->xfer = 0; | |
1123 | break; | |
1124 | case READ_ELEMENT_STATUS: | |
1125 | cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16); | |
1126 | break; | |
1127 | ||
1128 | /* generic commands */ | |
1129 | default: | |
1894df02 | 1130 | return scsi_req_xfer(cmd, dev, buf); |
40723a99 CH |
1131 | } |
1132 | return 0; | |
1133 | } | |
1134 | ||
297b044a JL |
1135 | static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
1136 | { | |
1137 | switch (buf[0]) { | |
1138 | /* Scanner commands */ | |
1139 | case OBJECT_POSITION: | |
1140 | cmd->xfer = 0; | |
1141 | break; | |
1142 | case SCAN: | |
1143 | cmd->xfer = buf[4]; | |
1144 | break; | |
1145 | case READ_10: | |
1146 | case SEND: | |
1147 | case GET_WINDOW: | |
1148 | case SET_WINDOW: | |
1149 | cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | |
1150 | break; | |
1151 | default: | |
1152 | /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */ | |
1153 | return scsi_req_xfer(cmd, dev, buf); | |
1154 | } | |
1155 | ||
1156 | return 0; | |
1157 | } | |
40723a99 | 1158 | |
2599aece | 1159 | static void scsi_cmd_xfer_mode(SCSICommand *cmd) |
97a06435 | 1160 | { |
a5ee9085 PB |
1161 | if (!cmd->xfer) { |
1162 | cmd->mode = SCSI_XFER_NONE; | |
1163 | return; | |
1164 | } | |
2599aece | 1165 | switch (cmd->buf[0]) { |
97a06435 GH |
1166 | case WRITE_6: |
1167 | case WRITE_10: | |
5e30a07d | 1168 | case WRITE_VERIFY_10: |
97a06435 GH |
1169 | case WRITE_12: |
1170 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
1171 | case WRITE_16: |
1172 | case WRITE_VERIFY_16: | |
d12ad44c PB |
1173 | case VERIFY_10: |
1174 | case VERIFY_12: | |
1175 | case VERIFY_16: | |
97a06435 GH |
1176 | case COPY: |
1177 | case COPY_VERIFY: | |
1178 | case COMPARE: | |
1179 | case CHANGE_DEFINITION: | |
1180 | case LOG_SELECT: | |
1181 | case MODE_SELECT: | |
1182 | case MODE_SELECT_10: | |
1183 | case SEND_DIAGNOSTIC: | |
1184 | case WRITE_BUFFER: | |
1185 | case FORMAT_UNIT: | |
1186 | case REASSIGN_BLOCKS: | |
97a06435 GH |
1187 | case SEARCH_EQUAL: |
1188 | case SEARCH_HIGH: | |
1189 | case SEARCH_LOW: | |
1190 | case UPDATE_BLOCK: | |
5e30a07d HR |
1191 | case WRITE_LONG_10: |
1192 | case WRITE_SAME_10: | |
a5ee9085 | 1193 | case WRITE_SAME_16: |
381b634c | 1194 | case UNMAP: |
97a06435 GH |
1195 | case SEARCH_HIGH_12: |
1196 | case SEARCH_EQUAL_12: | |
1197 | case SEARCH_LOW_12: | |
97a06435 GH |
1198 | case MEDIUM_SCAN: |
1199 | case SEND_VOLUME_TAG: | |
06b86357 PB |
1200 | case SEND_CUE_SHEET: |
1201 | case SEND_DVD_STRUCTURE: | |
01bedeba | 1202 | case PERSISTENT_RESERVE_OUT: |
c7126d5b | 1203 | case MAINTENANCE_OUT: |
297b044a JL |
1204 | case SET_WINDOW: |
1205 | case SCAN: | |
1206 | /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for | |
1207 | * non-scanner devices, so we only get here for SCAN and not for START_STOP. | |
1208 | */ | |
2599aece | 1209 | cmd->mode = SCSI_XFER_TO_DEV; |
97a06435 | 1210 | break; |
e4b65262 CM |
1211 | case ATA_PASSTHROUGH_12: |
1212 | case ATA_PASSTHROUGH_16: | |
1213 | /* T_DIR */ | |
1214 | cmd->mode = (cmd->buf[2] & 0x8) ? | |
1215 | SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV; | |
1216 | break; | |
97a06435 | 1217 | default: |
a5ee9085 | 1218 | cmd->mode = SCSI_XFER_FROM_DEV; |
97a06435 GH |
1219 | break; |
1220 | } | |
1221 | } | |
1222 | ||
1894df02 HR |
1223 | int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf) |
1224 | { | |
1225 | int rc; | |
c170aad8 | 1226 | int len; |
1894df02 HR |
1227 | |
1228 | cmd->lba = -1; | |
c170aad8 PB |
1229 | len = scsi_cdb_length(buf); |
1230 | if (len < 0) { | |
1231 | return -1; | |
1232 | } | |
28b70c9d | 1233 | |
c170aad8 | 1234 | cmd->len = len; |
40723a99 CH |
1235 | switch (dev->type) { |
1236 | case TYPE_TAPE: | |
1894df02 | 1237 | rc = scsi_req_stream_xfer(cmd, dev, buf); |
40723a99 CH |
1238 | break; |
1239 | case TYPE_MEDIUM_CHANGER: | |
1894df02 | 1240 | rc = scsi_req_medium_changer_xfer(cmd, dev, buf); |
40723a99 | 1241 | break; |
297b044a JL |
1242 | case TYPE_SCANNER: |
1243 | rc = scsi_req_scanner_length(cmd, dev, buf); | |
1244 | break; | |
40723a99 | 1245 | default: |
1894df02 | 1246 | rc = scsi_req_xfer(cmd, dev, buf); |
40723a99 | 1247 | break; |
2ec749cb | 1248 | } |
40723a99 | 1249 | |
2ec749cb GH |
1250 | if (rc != 0) |
1251 | return rc; | |
1252 | ||
afa46c46 PB |
1253 | memcpy(cmd->buf, buf, cmd->len); |
1254 | scsi_cmd_xfer_mode(cmd); | |
1255 | cmd->lba = scsi_cmd_lba(cmd); | |
2ec749cb GH |
1256 | return 0; |
1257 | } | |
ed3a34a3 | 1258 | |
53200fad PB |
1259 | void scsi_device_report_change(SCSIDevice *dev, SCSISense sense) |
1260 | { | |
1261 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | |
1262 | ||
1263 | scsi_device_set_ua(dev, sense); | |
1264 | if (bus->info->change) { | |
1265 | bus->info->change(bus, dev, sense); | |
1266 | } | |
1267 | } | |
1268 | ||
ad2d30f7 PB |
1269 | SCSIRequest *scsi_req_ref(SCSIRequest *req) |
1270 | { | |
8e86b93c | 1271 | assert(req->refcount > 0); |
ad2d30f7 PB |
1272 | req->refcount++; |
1273 | return req; | |
1274 | } | |
1275 | ||
1276 | void scsi_req_unref(SCSIRequest *req) | |
1277 | { | |
68bd348a | 1278 | assert(req->refcount > 0); |
ad2d30f7 | 1279 | if (--req->refcount == 0) { |
cac3c384 PB |
1280 | BusState *qbus = req->dev->qdev.parent_bus; |
1281 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus); | |
1282 | ||
8e86b93c PB |
1283 | if (bus->info->free_request && req->hba_private) { |
1284 | bus->info->free_request(bus, req->hba_private); | |
1285 | } | |
12010e7b PB |
1286 | if (req->ops->free_req) { |
1287 | req->ops->free_req(req); | |
ad2d30f7 | 1288 | } |
cac3c384 PB |
1289 | object_unref(OBJECT(req->dev)); |
1290 | object_unref(OBJECT(qbus->parent)); | |
633dccb4 | 1291 | g_free(req); |
ad2d30f7 PB |
1292 | } |
1293 | } | |
1294 | ||
ad3376cc PB |
1295 | /* Tell the device that we finished processing this chunk of I/O. It |
1296 | will start the next chunk or complete the command. */ | |
1297 | void scsi_req_continue(SCSIRequest *req) | |
1298 | { | |
6f6710aa PB |
1299 | if (req->io_canceled) { |
1300 | trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag); | |
1301 | return; | |
1302 | } | |
ad3376cc PB |
1303 | trace_scsi_req_continue(req->dev->id, req->lun, req->tag); |
1304 | if (req->cmd.mode == SCSI_XFER_TO_DEV) { | |
12010e7b | 1305 | req->ops->write_data(req); |
ad3376cc | 1306 | } else { |
12010e7b | 1307 | req->ops->read_data(req); |
ad3376cc PB |
1308 | } |
1309 | } | |
1310 | ||
ab9adc88 PB |
1311 | /* Called by the devices when data is ready for the HBA. The HBA should |
1312 | start a DMA operation to read or fill the device's data buffer. | |
ad3376cc | 1313 | Once it completes, calling scsi_req_continue will restart I/O. */ |
ab9adc88 PB |
1314 | void scsi_req_data(SCSIRequest *req, int len) |
1315 | { | |
3d5aba97 | 1316 | uint8_t *buf; |
e88c591d PB |
1317 | if (req->io_canceled) { |
1318 | trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); | |
01e95455 | 1319 | return; |
e88c591d | 1320 | } |
01e95455 PB |
1321 | trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); |
1322 | assert(req->cmd.mode != SCSI_XFER_NONE); | |
3d5aba97 PB |
1323 | if (!req->sg) { |
1324 | req->resid -= len; | |
1325 | req->bus->info->transfer_data(req, len); | |
1326 | return; | |
1327 | } | |
1328 | ||
1329 | /* If the device calls scsi_req_data and the HBA specified a | |
1330 | * scatter/gather list, the transfer has to happen in a single | |
1331 | * step. */ | |
1332 | assert(!req->dma_started); | |
1333 | req->dma_started = true; | |
1334 | ||
1335 | buf = scsi_req_get_buf(req); | |
1336 | if (req->cmd.mode == SCSI_XFER_FROM_DEV) { | |
1337 | req->resid = dma_buf_read(buf, len, req->sg); | |
1338 | } else { | |
1339 | req->resid = dma_buf_write(buf, len, req->sg); | |
1340 | } | |
1341 | scsi_req_continue(req); | |
ab9adc88 PB |
1342 | } |
1343 | ||
ec766865 GH |
1344 | void scsi_req_print(SCSIRequest *req) |
1345 | { | |
1346 | FILE *fp = stderr; | |
1347 | int i; | |
1348 | ||
1349 | fprintf(fp, "[%s id=%d] %s", | |
1350 | req->dev->qdev.parent_bus->name, | |
1351 | req->dev->id, | |
1352 | scsi_command_name(req->cmd.buf[0])); | |
1353 | for (i = 1; i < req->cmd.len; i++) { | |
1354 | fprintf(fp, " 0x%02x", req->cmd.buf[i]); | |
1355 | } | |
1356 | switch (req->cmd.mode) { | |
1357 | case SCSI_XFER_NONE: | |
1358 | fprintf(fp, " - none\n"); | |
1359 | break; | |
1360 | case SCSI_XFER_FROM_DEV: | |
1361 | fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer); | |
1362 | break; | |
1363 | case SCSI_XFER_TO_DEV: | |
1364 | fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer); | |
1365 | break; | |
1366 | default: | |
1367 | fprintf(fp, " - Oops\n"); | |
1368 | break; | |
1369 | } | |
1370 | } | |
1371 | ||
682a9b21 | 1372 | void scsi_req_complete(SCSIRequest *req, int status) |
ed3a34a3 | 1373 | { |
682a9b21 PB |
1374 | assert(req->status == -1); |
1375 | req->status = status; | |
b45ef674 | 1376 | |
335f560f | 1377 | assert(req->sense_len <= sizeof(req->sense)); |
b45ef674 PB |
1378 | if (status == GOOD) { |
1379 | req->sense_len = 0; | |
1380 | } | |
1381 | ||
1382 | if (req->sense_len) { | |
1383 | memcpy(req->dev->sense, req->sense, req->sense_len); | |
3653d8c4 PB |
1384 | req->dev->sense_len = req->sense_len; |
1385 | req->dev->sense_is_ua = (req->ops == &reqops_unit_attention); | |
1386 | } else { | |
1387 | req->dev->sense_len = 0; | |
1388 | req->dev->sense_is_ua = false; | |
b45ef674 | 1389 | } |
b45ef674 | 1390 | |
6dc06f08 PB |
1391 | /* |
1392 | * Unit attention state is now stored in the device's sense buffer | |
1393 | * if the HBA didn't do autosense. Clear the pending unit attention | |
1394 | * flags. | |
1395 | */ | |
1396 | scsi_clear_unit_attention(req); | |
1397 | ||
ad2d30f7 | 1398 | scsi_req_ref(req); |
e8637c90 | 1399 | scsi_req_dequeue(req); |
01e95455 | 1400 | req->bus->info->complete(req, req->status, req->resid); |
8e0a9320 FZ |
1401 | |
1402 | /* Cancelled requests might end up being completed instead of cancelled */ | |
1403 | notifier_list_notify(&req->cancel_notifiers, req); | |
ad2d30f7 | 1404 | scsi_req_unref(req); |
ed3a34a3 | 1405 | } |
db07c0f8 | 1406 | |
d5776465 FZ |
1407 | /* Called by the devices when the request is canceled. */ |
1408 | void scsi_req_cancel_complete(SCSIRequest *req) | |
1409 | { | |
1410 | assert(req->io_canceled); | |
1411 | if (req->bus->info->cancel) { | |
1412 | req->bus->info->cancel(req); | |
1413 | } | |
8e0a9320 | 1414 | notifier_list_notify(&req->cancel_notifiers, req); |
d5776465 FZ |
1415 | scsi_req_unref(req); |
1416 | } | |
1417 | ||
8e0a9320 FZ |
1418 | /* Cancel @req asynchronously. @notifier is added to @req's cancellation |
1419 | * notifier list, the bus will be notified the requests cancellation is | |
1420 | * completed. | |
1421 | * */ | |
1422 | void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier) | |
1423 | { | |
1424 | trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); | |
1425 | if (notifier) { | |
1426 | notifier_list_add(&req->cancel_notifiers, notifier); | |
1427 | } | |
3daa4107 PB |
1428 | if (req->io_canceled) { |
1429 | /* A blk_aio_cancel_async is pending; when it finishes, | |
1430 | * scsi_req_cancel_complete will be called and will | |
1431 | * call the notifier we just added. Just wait for that. | |
1432 | */ | |
1433 | assert(req->aiocb); | |
1434 | return; | |
1435 | } | |
1436 | /* Dropped in scsi_req_cancel_complete. */ | |
8e0a9320 FZ |
1437 | scsi_req_ref(req); |
1438 | scsi_req_dequeue(req); | |
1439 | req->io_canceled = true; | |
1440 | if (req->aiocb) { | |
4be74634 | 1441 | blk_aio_cancel_async(req->aiocb); |
2aeba9d8 FZ |
1442 | } else { |
1443 | scsi_req_cancel_complete(req); | |
8e0a9320 FZ |
1444 | } |
1445 | } | |
1446 | ||
94d3f98a PB |
1447 | void scsi_req_cancel(SCSIRequest *req) |
1448 | { | |
814589c4 | 1449 | trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); |
e88c591d PB |
1450 | if (!req->enqueued) { |
1451 | return; | |
94d3f98a | 1452 | } |
3daa4107 PB |
1453 | assert(!req->io_canceled); |
1454 | /* Dropped in scsi_req_cancel_complete. */ | |
94d3f98a PB |
1455 | scsi_req_ref(req); |
1456 | scsi_req_dequeue(req); | |
e88c591d | 1457 | req->io_canceled = true; |
a83cfd12 | 1458 | if (req->aiocb) { |
4be74634 | 1459 | blk_aio_cancel(req->aiocb); |
488eef2f PB |
1460 | } else { |
1461 | scsi_req_cancel_complete(req); | |
e88c591d | 1462 | } |
94d3f98a PB |
1463 | } |
1464 | ||
e48e84ea PB |
1465 | static int scsi_ua_precedence(SCSISense sense) |
1466 | { | |
1467 | if (sense.key != UNIT_ATTENTION) { | |
1468 | return INT_MAX; | |
1469 | } | |
1470 | if (sense.asc == 0x29 && sense.ascq == 0x04) { | |
1471 | /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */ | |
1472 | return 1; | |
1473 | } else if (sense.asc == 0x3F && sense.ascq == 0x01) { | |
1474 | /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */ | |
1475 | return 2; | |
1476 | } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) { | |
1477 | /* These two go with "all others". */ | |
1478 | ; | |
1479 | } else if (sense.asc == 0x29 && sense.ascq <= 0x07) { | |
1480 | /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0 | |
1481 | * POWER ON OCCURRED = 1 | |
1482 | * SCSI BUS RESET OCCURRED = 2 | |
1483 | * BUS DEVICE RESET FUNCTION OCCURRED = 3 | |
1484 | * I_T NEXUS LOSS OCCURRED = 7 | |
1485 | */ | |
1486 | return sense.ascq; | |
1487 | } else if (sense.asc == 0x2F && sense.ascq == 0x01) { | |
1488 | /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */ | |
1489 | return 8; | |
1490 | } | |
1491 | return (sense.asc << 8) | sense.ascq; | |
1492 | } | |
1493 | ||
1494 | void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense) | |
1495 | { | |
1496 | int prec1, prec2; | |
1497 | if (sense.key != UNIT_ATTENTION) { | |
1498 | return; | |
1499 | } | |
1500 | trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key, | |
1501 | sense.asc, sense.ascq); | |
1502 | ||
1503 | /* | |
1504 | * Override a pre-existing unit attention condition, except for a more | |
1505 | * important reset condition. | |
1506 | */ | |
1507 | prec1 = scsi_ua_precedence(sdev->unit_attention); | |
1508 | prec2 = scsi_ua_precedence(sense); | |
1509 | if (prec2 < prec1) { | |
1510 | sdev->unit_attention = sense; | |
1511 | } | |
1512 | } | |
1513 | ||
c7b48872 | 1514 | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) |
c557e889 PB |
1515 | { |
1516 | SCSIRequest *req; | |
1517 | ||
8aad35f6 | 1518 | aio_context_acquire(blk_get_aio_context(sdev->conf.blk)); |
c557e889 PB |
1519 | while (!QTAILQ_EMPTY(&sdev->requests)) { |
1520 | req = QTAILQ_FIRST(&sdev->requests); | |
8aad35f6 | 1521 | scsi_req_cancel_async(req, NULL); |
c557e889 | 1522 | } |
8aad35f6 PB |
1523 | blk_drain(sdev->conf.blk); |
1524 | aio_context_release(blk_get_aio_context(sdev->conf.blk)); | |
e48e84ea | 1525 | scsi_device_set_ua(sdev, sense); |
c557e889 PB |
1526 | } |
1527 | ||
baa1bd89 PB |
1528 | static char *scsibus_get_dev_path(DeviceState *dev) |
1529 | { | |
e1dc6815 | 1530 | SCSIDevice *d = SCSI_DEVICE(dev); |
baa1bd89 | 1531 | DeviceState *hba = dev->parent_bus->parent; |
09e5ab63 | 1532 | char *id; |
b7c8c35f | 1533 | char *path; |
baa1bd89 | 1534 | |
09e5ab63 | 1535 | id = qdev_get_dev_path(hba); |
baa1bd89 | 1536 | if (id) { |
b7c8c35f | 1537 | path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun); |
baa1bd89 | 1538 | } else { |
b7c8c35f | 1539 | path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun); |
baa1bd89 | 1540 | } |
b7c8c35f PB |
1541 | g_free(id); |
1542 | return path; | |
baa1bd89 PB |
1543 | } |
1544 | ||
db07c0f8 GN |
1545 | static char *scsibus_get_fw_dev_path(DeviceState *dev) |
1546 | { | |
b9eea3e6 | 1547 | SCSIDevice *d = SCSI_DEVICE(dev); |
a5cf8262 JM |
1548 | return g_strdup_printf("channel@%x/%s@%x,%x", d->channel, |
1549 | qdev_fw_name(dev), d->id, d->lun); | |
f48a7a6e PB |
1550 | } |
1551 | ||
0d3545e7 | 1552 | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) |
f48a7a6e | 1553 | { |
0866aca1 | 1554 | BusChild *kid; |
f48a7a6e | 1555 | SCSIDevice *target_dev = NULL; |
db07c0f8 | 1556 | |
eae3eb3e | 1557 | QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, sibling) { |
0866aca1 | 1558 | DeviceState *qdev = kid->child; |
b9eea3e6 | 1559 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
db07c0f8 | 1560 | |
0d3545e7 | 1561 | if (dev->channel == channel && dev->id == id) { |
f48a7a6e PB |
1562 | if (dev->lun == lun) { |
1563 | return dev; | |
1564 | } | |
1565 | target_dev = dev; | |
1566 | } | |
1567 | } | |
1568 | return target_dev; | |
db07c0f8 | 1569 | } |
b9eea3e6 | 1570 | |
63f740dd PB |
1571 | /* SCSI request list. For simplicity, pv points to the whole device */ |
1572 | ||
2c21ee76 | 1573 | static int put_scsi_requests(QEMUFile *f, void *pv, size_t size, |
03fee66f | 1574 | const VMStateField *field, QJSON *vmdesc) |
63f740dd PB |
1575 | { |
1576 | SCSIDevice *s = pv; | |
1577 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
1578 | SCSIRequest *req; | |
1579 | ||
1580 | QTAILQ_FOREACH(req, &s->requests, next) { | |
1581 | assert(!req->io_canceled); | |
1582 | assert(req->status == -1); | |
63f740dd PB |
1583 | assert(req->enqueued); |
1584 | ||
18eef3bc | 1585 | qemu_put_sbyte(f, req->retry ? 1 : 2); |
63f740dd PB |
1586 | qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf)); |
1587 | qemu_put_be32s(f, &req->tag); | |
1588 | qemu_put_be32s(f, &req->lun); | |
1589 | if (bus->info->save_request) { | |
1590 | bus->info->save_request(f, req); | |
1591 | } | |
1592 | if (req->ops->save_request) { | |
1593 | req->ops->save_request(f, req); | |
1594 | } | |
1595 | } | |
1596 | qemu_put_sbyte(f, 0); | |
2c21ee76 JD |
1597 | |
1598 | return 0; | |
63f740dd PB |
1599 | } |
1600 | ||
2c21ee76 | 1601 | static int get_scsi_requests(QEMUFile *f, void *pv, size_t size, |
03fee66f | 1602 | const VMStateField *field) |
63f740dd PB |
1603 | { |
1604 | SCSIDevice *s = pv; | |
1605 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
18eef3bc | 1606 | int8_t sbyte; |
63f740dd | 1607 | |
18eef3bc | 1608 | while ((sbyte = qemu_get_sbyte(f)) > 0) { |
63f740dd PB |
1609 | uint8_t buf[SCSI_CMD_BUF_SIZE]; |
1610 | uint32_t tag; | |
1611 | uint32_t lun; | |
1612 | SCSIRequest *req; | |
1613 | ||
1614 | qemu_get_buffer(f, buf, sizeof(buf)); | |
1615 | qemu_get_be32s(f, &tag); | |
1616 | qemu_get_be32s(f, &lun); | |
1617 | req = scsi_req_new(s, tag, lun, buf, NULL); | |
18eef3bc | 1618 | req->retry = (sbyte == 1); |
63f740dd PB |
1619 | if (bus->info->load_request) { |
1620 | req->hba_private = bus->info->load_request(f, req); | |
1621 | } | |
1622 | if (req->ops->load_request) { | |
1623 | req->ops->load_request(f, req); | |
1624 | } | |
1625 | ||
1626 | /* Just restart it later. */ | |
63f740dd PB |
1627 | scsi_req_enqueue_internal(req); |
1628 | ||
1629 | /* At this point, the request will be kept alive by the reference | |
1630 | * added by scsi_req_enqueue_internal, so we can release our reference. | |
1631 | * The HBA of course will add its own reference in the load_request | |
1632 | * callback if it needs to hold on the SCSIRequest. | |
1633 | */ | |
1634 | scsi_req_unref(req); | |
1635 | } | |
1636 | ||
1637 | return 0; | |
1638 | } | |
1639 | ||
12badfc2 | 1640 | static const VMStateInfo vmstate_info_scsi_requests = { |
63f740dd PB |
1641 | .name = "scsi-requests", |
1642 | .get = get_scsi_requests, | |
1643 | .put = put_scsi_requests, | |
1644 | }; | |
1645 | ||
2e323f03 FZ |
1646 | static bool scsi_sense_state_needed(void *opaque) |
1647 | { | |
1648 | SCSIDevice *s = opaque; | |
1649 | ||
1650 | return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD; | |
1651 | } | |
1652 | ||
1653 | static const VMStateDescription vmstate_scsi_sense_state = { | |
1654 | .name = "SCSIDevice/sense", | |
1655 | .version_id = 1, | |
1656 | .minimum_version_id = 1, | |
5cd8cada | 1657 | .needed = scsi_sense_state_needed, |
d49805ae | 1658 | .fields = (VMStateField[]) { |
2e323f03 FZ |
1659 | VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, |
1660 | SCSI_SENSE_BUF_SIZE_OLD, | |
1661 | SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD), | |
1662 | VMSTATE_END_OF_LIST() | |
1663 | } | |
1664 | }; | |
1665 | ||
63f740dd PB |
1666 | const VMStateDescription vmstate_scsi_device = { |
1667 | .name = "SCSIDevice", | |
1668 | .version_id = 1, | |
1669 | .minimum_version_id = 1, | |
63f740dd PB |
1670 | .fields = (VMStateField[]) { |
1671 | VMSTATE_UINT8(unit_attention.key, SCSIDevice), | |
1672 | VMSTATE_UINT8(unit_attention.asc, SCSIDevice), | |
1673 | VMSTATE_UINT8(unit_attention.ascq, SCSIDevice), | |
1674 | VMSTATE_BOOL(sense_is_ua, SCSIDevice), | |
2e323f03 | 1675 | VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD), |
63f740dd PB |
1676 | VMSTATE_UINT32(sense_len, SCSIDevice), |
1677 | { | |
1678 | .name = "requests", | |
1679 | .version_id = 0, | |
1680 | .field_exists = NULL, | |
1681 | .size = 0, /* ouch */ | |
1682 | .info = &vmstate_info_scsi_requests, | |
1683 | .flags = VMS_SINGLE, | |
1684 | .offset = 0, | |
1685 | }, | |
1686 | VMSTATE_END_OF_LIST() | |
2e323f03 | 1687 | }, |
5cd8cada JQ |
1688 | .subsections = (const VMStateDescription*[]) { |
1689 | &vmstate_scsi_sense_state, | |
1690 | NULL | |
63f740dd PB |
1691 | } |
1692 | }; | |
1693 | ||
39bffca2 AL |
1694 | static void scsi_device_class_init(ObjectClass *klass, void *data) |
1695 | { | |
1696 | DeviceClass *k = DEVICE_CLASS(klass); | |
125ee0ed | 1697 | set_bit(DEVICE_CATEGORY_STORAGE, k->categories); |
a818a4b6 FZ |
1698 | k->bus_type = TYPE_SCSI_BUS; |
1699 | k->realize = scsi_qdev_realize; | |
a818a4b6 FZ |
1700 | k->unrealize = scsi_qdev_unrealize; |
1701 | k->props = scsi_props; | |
39bffca2 AL |
1702 | } |
1703 | ||
44fb6337 GA |
1704 | static void scsi_dev_instance_init(Object *obj) |
1705 | { | |
1706 | DeviceState *dev = DEVICE(obj); | |
e1dc6815 | 1707 | SCSIDevice *s = SCSI_DEVICE(dev); |
44fb6337 GA |
1708 | |
1709 | device_add_bootindex_property(obj, &s->conf.bootindex, | |
1710 | "bootindex", NULL, | |
1711 | &s->qdev, NULL); | |
1712 | } | |
1713 | ||
8c43a6f0 | 1714 | static const TypeInfo scsi_device_type_info = { |
b9eea3e6 AL |
1715 | .name = TYPE_SCSI_DEVICE, |
1716 | .parent = TYPE_DEVICE, | |
1717 | .instance_size = sizeof(SCSIDevice), | |
1718 | .abstract = true, | |
1719 | .class_size = sizeof(SCSIDeviceClass), | |
39bffca2 | 1720 | .class_init = scsi_device_class_init, |
44fb6337 | 1721 | .instance_init = scsi_dev_instance_init, |
b9eea3e6 AL |
1722 | }; |
1723 | ||
83f7d43a | 1724 | static void scsi_register_types(void) |
b9eea3e6 | 1725 | { |
0d936928 | 1726 | type_register_static(&scsi_bus_info); |
b9eea3e6 AL |
1727 | type_register_static(&scsi_device_type_info); |
1728 | } | |
1729 | ||
83f7d43a | 1730 | type_init(scsi_register_types) |