]>
Commit | Line | Data |
---|---|---|
1 | #include "qemu/osdep.h" | |
2 | #include "hw/hw.h" | |
3 | #include "qapi/error.h" | |
4 | #include "qemu/error-report.h" | |
5 | #include "hw/scsi/scsi.h" | |
6 | #include "block/scsi.h" | |
7 | #include "hw/qdev.h" | |
8 | #include "sysemu/block-backend.h" | |
9 | #include "sysemu/blockdev.h" | |
10 | #include "trace.h" | |
11 | #include "sysemu/dma.h" | |
12 | #include "qemu/cutils.h" | |
13 | ||
14 | static char *scsibus_get_dev_path(DeviceState *dev); | |
15 | static char *scsibus_get_fw_dev_path(DeviceState *dev); | |
16 | static void scsi_req_dequeue(SCSIRequest *req); | |
17 | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len); | |
18 | static void scsi_target_free_buf(SCSIRequest *req); | |
19 | ||
20 | static Property scsi_props[] = { | |
21 | DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), | |
22 | DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), | |
23 | DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), | |
24 | DEFINE_PROP_END_OF_LIST(), | |
25 | }; | |
26 | ||
27 | static void scsi_bus_class_init(ObjectClass *klass, void *data) | |
28 | { | |
29 | BusClass *k = BUS_CLASS(klass); | |
30 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); | |
31 | ||
32 | k->get_dev_path = scsibus_get_dev_path; | |
33 | k->get_fw_dev_path = scsibus_get_fw_dev_path; | |
34 | hc->unplug = qdev_simple_device_unplug_cb; | |
35 | } | |
36 | ||
37 | static const TypeInfo scsi_bus_info = { | |
38 | .name = TYPE_SCSI_BUS, | |
39 | .parent = TYPE_BUS, | |
40 | .instance_size = sizeof(SCSIBus), | |
41 | .class_init = scsi_bus_class_init, | |
42 | .interfaces = (InterfaceInfo[]) { | |
43 | { TYPE_HOTPLUG_HANDLER }, | |
44 | { } | |
45 | } | |
46 | }; | |
47 | static int next_scsi_bus; | |
48 | ||
49 | static void scsi_device_realize(SCSIDevice *s, Error **errp) | |
50 | { | |
51 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
52 | if (sc->realize) { | |
53 | sc->realize(s, errp); | |
54 | } | |
55 | } | |
56 | ||
57 | int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, | |
58 | void *hba_private) | |
59 | { | |
60 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | |
61 | int rc; | |
62 | ||
63 | assert(cmd->len == 0); | |
64 | rc = scsi_req_parse_cdb(dev, cmd, buf); | |
65 | if (bus->info->parse_cdb) { | |
66 | rc = bus->info->parse_cdb(dev, cmd, buf, hba_private); | |
67 | } | |
68 | return rc; | |
69 | } | |
70 | ||
71 | static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun, | |
72 | uint8_t *buf, void *hba_private) | |
73 | { | |
74 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
75 | if (sc->alloc_req) { | |
76 | return sc->alloc_req(s, tag, lun, buf, hba_private); | |
77 | } | |
78 | ||
79 | return NULL; | |
80 | } | |
81 | ||
82 | void scsi_device_unit_attention_reported(SCSIDevice *s) | |
83 | { | |
84 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
85 | if (sc->unit_attention_reported) { | |
86 | sc->unit_attention_reported(s); | |
87 | } | |
88 | } | |
89 | ||
90 | /* Create a scsi bus, and attach devices to it. */ | |
91 | void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, | |
92 | const SCSIBusInfo *info, const char *bus_name) | |
93 | { | |
94 | qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name); | |
95 | bus->busnr = next_scsi_bus++; | |
96 | bus->info = info; | |
97 | qbus_set_bus_hotplug_handler(BUS(bus), &error_abort); | |
98 | } | |
99 | ||
100 | static void scsi_dma_restart_bh(void *opaque) | |
101 | { | |
102 | SCSIDevice *s = opaque; | |
103 | SCSIRequest *req, *next; | |
104 | ||
105 | qemu_bh_delete(s->bh); | |
106 | s->bh = NULL; | |
107 | ||
108 | aio_context_acquire(blk_get_aio_context(s->conf.blk)); | |
109 | QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { | |
110 | scsi_req_ref(req); | |
111 | if (req->retry) { | |
112 | req->retry = false; | |
113 | switch (req->cmd.mode) { | |
114 | case SCSI_XFER_FROM_DEV: | |
115 | case SCSI_XFER_TO_DEV: | |
116 | scsi_req_continue(req); | |
117 | break; | |
118 | case SCSI_XFER_NONE: | |
119 | scsi_req_dequeue(req); | |
120 | scsi_req_enqueue(req); | |
121 | break; | |
122 | } | |
123 | } | |
124 | scsi_req_unref(req); | |
125 | } | |
126 | aio_context_release(blk_get_aio_context(s->conf.blk)); | |
127 | } | |
128 | ||
129 | void scsi_req_retry(SCSIRequest *req) | |
130 | { | |
131 | /* No need to save a reference, because scsi_dma_restart_bh just | |
132 | * looks at the request list. */ | |
133 | req->retry = true; | |
134 | } | |
135 | ||
136 | static void scsi_dma_restart_cb(void *opaque, int running, RunState state) | |
137 | { | |
138 | SCSIDevice *s = opaque; | |
139 | ||
140 | if (!running) { | |
141 | return; | |
142 | } | |
143 | if (!s->bh) { | |
144 | AioContext *ctx = blk_get_aio_context(s->conf.blk); | |
145 | s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s); | |
146 | qemu_bh_schedule(s->bh); | |
147 | } | |
148 | } | |
149 | ||
150 | static void scsi_qdev_realize(DeviceState *qdev, Error **errp) | |
151 | { | |
152 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
153 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | |
154 | SCSIDevice *d; | |
155 | Error *local_err = NULL; | |
156 | ||
157 | if (dev->channel > bus->info->max_channel) { | |
158 | error_setg(errp, "bad scsi channel id: %d", dev->channel); | |
159 | return; | |
160 | } | |
161 | if (dev->id != -1 && dev->id > bus->info->max_target) { | |
162 | error_setg(errp, "bad scsi device id: %d", dev->id); | |
163 | return; | |
164 | } | |
165 | if (dev->lun != -1 && dev->lun > bus->info->max_lun) { | |
166 | error_setg(errp, "bad scsi device lun: %d", dev->lun); | |
167 | return; | |
168 | } | |
169 | ||
170 | if (dev->id == -1) { | |
171 | int id = -1; | |
172 | if (dev->lun == -1) { | |
173 | dev->lun = 0; | |
174 | } | |
175 | do { | |
176 | d = scsi_device_find(bus, dev->channel, ++id, dev->lun); | |
177 | } while (d && d->lun == dev->lun && id < bus->info->max_target); | |
178 | if (d && d->lun == dev->lun) { | |
179 | error_setg(errp, "no free target"); | |
180 | return; | |
181 | } | |
182 | dev->id = id; | |
183 | } else if (dev->lun == -1) { | |
184 | int lun = -1; | |
185 | do { | |
186 | d = scsi_device_find(bus, dev->channel, dev->id, ++lun); | |
187 | } while (d && d->lun == lun && lun < bus->info->max_lun); | |
188 | if (d && d->lun == lun) { | |
189 | error_setg(errp, "no free lun"); | |
190 | return; | |
191 | } | |
192 | dev->lun = lun; | |
193 | } else { | |
194 | d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); | |
195 | assert(d); | |
196 | if (d->lun == dev->lun && dev != d) { | |
197 | error_setg(errp, "lun already used by '%s'", d->qdev.id); | |
198 | return; | |
199 | } | |
200 | } | |
201 | ||
202 | QTAILQ_INIT(&dev->requests); | |
203 | scsi_device_realize(dev, &local_err); | |
204 | if (local_err) { | |
205 | error_propagate(errp, local_err); | |
206 | return; | |
207 | } | |
208 | dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, | |
209 | dev); | |
210 | } | |
211 | ||
212 | static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp) | |
213 | { | |
214 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
215 | ||
216 | if (dev->vmsentry) { | |
217 | qemu_del_vm_change_state_handler(dev->vmsentry); | |
218 | } | |
219 | ||
220 | scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE)); | |
221 | blockdev_mark_auto_del(dev->conf.blk); | |
222 | } | |
223 | ||
224 | /* handle legacy '-drive if=scsi,...' cmd line args */ | |
225 | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, | |
226 | int unit, bool removable, int bootindex, | |
227 | const char *serial, Error **errp) | |
228 | { | |
229 | const char *driver; | |
230 | char *name; | |
231 | DeviceState *dev; | |
232 | Error *err = NULL; | |
233 | ||
234 | driver = blk_is_sg(blk) ? "scsi-generic" : "scsi-disk"; | |
235 | dev = qdev_create(&bus->qbus, driver); | |
236 | name = g_strdup_printf("legacy[%d]", unit); | |
237 | object_property_add_child(OBJECT(bus), name, OBJECT(dev), NULL); | |
238 | g_free(name); | |
239 | ||
240 | qdev_prop_set_uint32(dev, "scsi-id", unit); | |
241 | if (bootindex >= 0) { | |
242 | object_property_set_int(OBJECT(dev), bootindex, "bootindex", | |
243 | &error_abort); | |
244 | } | |
245 | if (object_property_find(OBJECT(dev), "removable", NULL)) { | |
246 | qdev_prop_set_bit(dev, "removable", removable); | |
247 | } | |
248 | if (serial && object_property_find(OBJECT(dev), "serial", NULL)) { | |
249 | qdev_prop_set_string(dev, "serial", serial); | |
250 | } | |
251 | qdev_prop_set_drive(dev, "drive", blk, &err); | |
252 | if (err) { | |
253 | error_propagate(errp, err); | |
254 | object_unparent(OBJECT(dev)); | |
255 | return NULL; | |
256 | } | |
257 | object_property_set_bool(OBJECT(dev), true, "realized", &err); | |
258 | if (err != NULL) { | |
259 | error_propagate(errp, err); | |
260 | object_unparent(OBJECT(dev)); | |
261 | return NULL; | |
262 | } | |
263 | return SCSI_DEVICE(dev); | |
264 | } | |
265 | ||
266 | void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp) | |
267 | { | |
268 | Location loc; | |
269 | DriveInfo *dinfo; | |
270 | int unit; | |
271 | Error *err = NULL; | |
272 | ||
273 | loc_push_none(&loc); | |
274 | for (unit = 0; unit <= bus->info->max_target; unit++) { | |
275 | dinfo = drive_get(IF_SCSI, bus->busnr, unit); | |
276 | if (dinfo == NULL) { | |
277 | continue; | |
278 | } | |
279 | qemu_opts_loc_restore(dinfo->opts); | |
280 | scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo), | |
281 | unit, false, -1, NULL, &err); | |
282 | if (err != NULL) { | |
283 | error_propagate(errp, err); | |
284 | break; | |
285 | } | |
286 | } | |
287 | loc_pop(&loc); | |
288 | } | |
289 | ||
290 | static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf) | |
291 | { | |
292 | scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | |
293 | scsi_req_complete(req, CHECK_CONDITION); | |
294 | return 0; | |
295 | } | |
296 | ||
297 | static const struct SCSIReqOps reqops_invalid_field = { | |
298 | .size = sizeof(SCSIRequest), | |
299 | .send_command = scsi_invalid_field | |
300 | }; | |
301 | ||
302 | /* SCSIReqOps implementation for invalid commands. */ | |
303 | ||
304 | static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf) | |
305 | { | |
306 | scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); | |
307 | scsi_req_complete(req, CHECK_CONDITION); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | static const struct SCSIReqOps reqops_invalid_opcode = { | |
312 | .size = sizeof(SCSIRequest), | |
313 | .send_command = scsi_invalid_command | |
314 | }; | |
315 | ||
316 | /* SCSIReqOps implementation for unit attention conditions. */ | |
317 | ||
318 | static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf) | |
319 | { | |
320 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { | |
321 | scsi_req_build_sense(req, req->dev->unit_attention); | |
322 | } else if (req->bus->unit_attention.key == UNIT_ATTENTION) { | |
323 | scsi_req_build_sense(req, req->bus->unit_attention); | |
324 | } | |
325 | scsi_req_complete(req, CHECK_CONDITION); | |
326 | return 0; | |
327 | } | |
328 | ||
329 | static const struct SCSIReqOps reqops_unit_attention = { | |
330 | .size = sizeof(SCSIRequest), | |
331 | .send_command = scsi_unit_attention | |
332 | }; | |
333 | ||
334 | /* SCSIReqOps implementation for REPORT LUNS and for commands sent to | |
335 | an invalid LUN. */ | |
336 | ||
337 | typedef struct SCSITargetReq SCSITargetReq; | |
338 | ||
339 | struct SCSITargetReq { | |
340 | SCSIRequest req; | |
341 | int len; | |
342 | uint8_t *buf; | |
343 | int buf_len; | |
344 | }; | |
345 | ||
346 | static void store_lun(uint8_t *outbuf, int lun) | |
347 | { | |
348 | if (lun < 256) { | |
349 | outbuf[1] = lun; | |
350 | return; | |
351 | } | |
352 | outbuf[1] = (lun & 255); | |
353 | outbuf[0] = (lun >> 8) | 0x40; | |
354 | } | |
355 | ||
356 | static bool scsi_target_emulate_report_luns(SCSITargetReq *r) | |
357 | { | |
358 | BusChild *kid; | |
359 | int i, len, n; | |
360 | int channel, id; | |
361 | bool found_lun0; | |
362 | ||
363 | if (r->req.cmd.xfer < 16) { | |
364 | return false; | |
365 | } | |
366 | if (r->req.cmd.buf[2] > 2) { | |
367 | return false; | |
368 | } | |
369 | channel = r->req.dev->channel; | |
370 | id = r->req.dev->id; | |
371 | found_lun0 = false; | |
372 | n = 0; | |
373 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { | |
374 | DeviceState *qdev = kid->child; | |
375 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
376 | ||
377 | if (dev->channel == channel && dev->id == id) { | |
378 | if (dev->lun == 0) { | |
379 | found_lun0 = true; | |
380 | } | |
381 | n += 8; | |
382 | } | |
383 | } | |
384 | if (!found_lun0) { | |
385 | n += 8; | |
386 | } | |
387 | ||
388 | scsi_target_alloc_buf(&r->req, n + 8); | |
389 | ||
390 | len = MIN(n + 8, r->req.cmd.xfer & ~7); | |
391 | memset(r->buf, 0, len); | |
392 | stl_be_p(&r->buf[0], n); | |
393 | i = found_lun0 ? 8 : 16; | |
394 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { | |
395 | DeviceState *qdev = kid->child; | |
396 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
397 | ||
398 | if (dev->channel == channel && dev->id == id) { | |
399 | store_lun(&r->buf[i], dev->lun); | |
400 | i += 8; | |
401 | } | |
402 | } | |
403 | assert(i == n + 8); | |
404 | r->len = len; | |
405 | return true; | |
406 | } | |
407 | ||
408 | static bool scsi_target_emulate_inquiry(SCSITargetReq *r) | |
409 | { | |
410 | assert(r->req.dev->lun != r->req.lun); | |
411 | ||
412 | scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN); | |
413 | ||
414 | if (r->req.cmd.buf[1] & 0x2) { | |
415 | /* Command support data - optional, not implemented */ | |
416 | return false; | |
417 | } | |
418 | ||
419 | if (r->req.cmd.buf[1] & 0x1) { | |
420 | /* Vital product data */ | |
421 | uint8_t page_code = r->req.cmd.buf[2]; | |
422 | r->buf[r->len++] = page_code ; /* this page */ | |
423 | r->buf[r->len++] = 0x00; | |
424 | ||
425 | switch (page_code) { | |
426 | case 0x00: /* Supported page codes, mandatory */ | |
427 | { | |
428 | int pages; | |
429 | pages = r->len++; | |
430 | r->buf[r->len++] = 0x00; /* list of supported pages (this page) */ | |
431 | r->buf[pages] = r->len - pages - 1; /* number of pages */ | |
432 | break; | |
433 | } | |
434 | default: | |
435 | return false; | |
436 | } | |
437 | /* done with EVPD */ | |
438 | assert(r->len < r->buf_len); | |
439 | r->len = MIN(r->req.cmd.xfer, r->len); | |
440 | return true; | |
441 | } | |
442 | ||
443 | /* Standard INQUIRY data */ | |
444 | if (r->req.cmd.buf[2] != 0) { | |
445 | return false; | |
446 | } | |
447 | ||
448 | /* PAGE CODE == 0 */ | |
449 | r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN); | |
450 | memset(r->buf, 0, r->len); | |
451 | if (r->req.lun != 0) { | |
452 | r->buf[0] = TYPE_NO_LUN; | |
453 | } else { | |
454 | r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE; | |
455 | r->buf[2] = 5; /* Version */ | |
456 | r->buf[3] = 2 | 0x10; /* HiSup, response data format */ | |
457 | r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */ | |
458 | r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */ | |
459 | memcpy(&r->buf[8], "QEMU ", 8); | |
460 | memcpy(&r->buf[16], "QEMU TARGET ", 16); | |
461 | pstrcpy((char *) &r->buf[32], 4, qemu_hw_version()); | |
462 | } | |
463 | return true; | |
464 | } | |
465 | ||
466 | static size_t scsi_sense_len(SCSIRequest *req) | |
467 | { | |
468 | if (req->dev->type == TYPE_SCANNER) | |
469 | return SCSI_SENSE_LEN_SCANNER; | |
470 | else | |
471 | return SCSI_SENSE_LEN; | |
472 | } | |
473 | ||
474 | static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf) | |
475 | { | |
476 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
477 | ||
478 | switch (buf[0]) { | |
479 | case REPORT_LUNS: | |
480 | if (!scsi_target_emulate_report_luns(r)) { | |
481 | goto illegal_request; | |
482 | } | |
483 | break; | |
484 | case INQUIRY: | |
485 | if (!scsi_target_emulate_inquiry(r)) { | |
486 | goto illegal_request; | |
487 | } | |
488 | break; | |
489 | case REQUEST_SENSE: | |
490 | scsi_target_alloc_buf(&r->req, scsi_sense_len(req)); | |
491 | r->len = scsi_device_get_sense(r->req.dev, r->buf, | |
492 | MIN(req->cmd.xfer, r->buf_len), | |
493 | (req->cmd.buf[1] & 1) == 0); | |
494 | if (r->req.dev->sense_is_ua) { | |
495 | scsi_device_unit_attention_reported(req->dev); | |
496 | r->req.dev->sense_len = 0; | |
497 | r->req.dev->sense_is_ua = false; | |
498 | } | |
499 | break; | |
500 | case TEST_UNIT_READY: | |
501 | break; | |
502 | default: | |
503 | scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED)); | |
504 | scsi_req_complete(req, CHECK_CONDITION); | |
505 | return 0; | |
506 | illegal_request: | |
507 | scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | |
508 | scsi_req_complete(req, CHECK_CONDITION); | |
509 | return 0; | |
510 | } | |
511 | ||
512 | if (!r->len) { | |
513 | scsi_req_complete(req, GOOD); | |
514 | } | |
515 | return r->len; | |
516 | } | |
517 | ||
518 | static void scsi_target_read_data(SCSIRequest *req) | |
519 | { | |
520 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
521 | uint32_t n; | |
522 | ||
523 | n = r->len; | |
524 | if (n > 0) { | |
525 | r->len = 0; | |
526 | scsi_req_data(&r->req, n); | |
527 | } else { | |
528 | scsi_req_complete(&r->req, GOOD); | |
529 | } | |
530 | } | |
531 | ||
532 | static uint8_t *scsi_target_get_buf(SCSIRequest *req) | |
533 | { | |
534 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
535 | ||
536 | return r->buf; | |
537 | } | |
538 | ||
539 | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len) | |
540 | { | |
541 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
542 | ||
543 | r->buf = g_malloc(len); | |
544 | r->buf_len = len; | |
545 | ||
546 | return r->buf; | |
547 | } | |
548 | ||
549 | static void scsi_target_free_buf(SCSIRequest *req) | |
550 | { | |
551 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
552 | ||
553 | g_free(r->buf); | |
554 | } | |
555 | ||
556 | static const struct SCSIReqOps reqops_target_command = { | |
557 | .size = sizeof(SCSITargetReq), | |
558 | .send_command = scsi_target_send_command, | |
559 | .read_data = scsi_target_read_data, | |
560 | .get_buf = scsi_target_get_buf, | |
561 | .free_req = scsi_target_free_buf, | |
562 | }; | |
563 | ||
564 | ||
565 | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, | |
566 | uint32_t tag, uint32_t lun, void *hba_private) | |
567 | { | |
568 | SCSIRequest *req; | |
569 | SCSIBus *bus = scsi_bus_from_device(d); | |
570 | BusState *qbus = BUS(bus); | |
571 | const int memset_off = offsetof(SCSIRequest, sense) | |
572 | + sizeof(req->sense); | |
573 | ||
574 | req = g_malloc(reqops->size); | |
575 | memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off); | |
576 | req->refcount = 1; | |
577 | req->bus = bus; | |
578 | req->dev = d; | |
579 | req->tag = tag; | |
580 | req->lun = lun; | |
581 | req->hba_private = hba_private; | |
582 | req->status = -1; | |
583 | req->ops = reqops; | |
584 | object_ref(OBJECT(d)); | |
585 | object_ref(OBJECT(qbus->parent)); | |
586 | notifier_list_init(&req->cancel_notifiers); | |
587 | trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); | |
588 | return req; | |
589 | } | |
590 | ||
591 | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, | |
592 | uint8_t *buf, void *hba_private) | |
593 | { | |
594 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); | |
595 | const SCSIReqOps *ops; | |
596 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d); | |
597 | SCSIRequest *req; | |
598 | SCSICommand cmd = { .len = 0 }; | |
599 | int ret; | |
600 | ||
601 | if ((d->unit_attention.key == UNIT_ATTENTION || | |
602 | bus->unit_attention.key == UNIT_ATTENTION) && | |
603 | (buf[0] != INQUIRY && | |
604 | buf[0] != REPORT_LUNS && | |
605 | buf[0] != GET_CONFIGURATION && | |
606 | buf[0] != GET_EVENT_STATUS_NOTIFICATION && | |
607 | ||
608 | /* | |
609 | * If we already have a pending unit attention condition, | |
610 | * report this one before triggering another one. | |
611 | */ | |
612 | !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) { | |
613 | ops = &reqops_unit_attention; | |
614 | } else if (lun != d->lun || | |
615 | buf[0] == REPORT_LUNS || | |
616 | (buf[0] == REQUEST_SENSE && d->sense_len)) { | |
617 | ops = &reqops_target_command; | |
618 | } else { | |
619 | ops = NULL; | |
620 | } | |
621 | ||
622 | if (ops != NULL || !sc->parse_cdb) { | |
623 | ret = scsi_req_parse_cdb(d, &cmd, buf); | |
624 | } else { | |
625 | ret = sc->parse_cdb(d, &cmd, buf, hba_private); | |
626 | } | |
627 | ||
628 | if (ret != 0) { | |
629 | trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]); | |
630 | req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private); | |
631 | } else { | |
632 | assert(cmd.len != 0); | |
633 | trace_scsi_req_parsed(d->id, lun, tag, buf[0], | |
634 | cmd.mode, cmd.xfer); | |
635 | if (cmd.lba != -1) { | |
636 | trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0], | |
637 | cmd.lba); | |
638 | } | |
639 | ||
640 | if (cmd.xfer > INT32_MAX) { | |
641 | req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private); | |
642 | } else if (ops) { | |
643 | req = scsi_req_alloc(ops, d, tag, lun, hba_private); | |
644 | } else { | |
645 | req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); | |
646 | } | |
647 | } | |
648 | ||
649 | req->cmd = cmd; | |
650 | req->resid = req->cmd.xfer; | |
651 | ||
652 | switch (buf[0]) { | |
653 | case INQUIRY: | |
654 | trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]); | |
655 | break; | |
656 | case TEST_UNIT_READY: | |
657 | trace_scsi_test_unit_ready(d->id, lun, tag); | |
658 | break; | |
659 | case REPORT_LUNS: | |
660 | trace_scsi_report_luns(d->id, lun, tag); | |
661 | break; | |
662 | case REQUEST_SENSE: | |
663 | trace_scsi_request_sense(d->id, lun, tag); | |
664 | break; | |
665 | default: | |
666 | break; | |
667 | } | |
668 | ||
669 | return req; | |
670 | } | |
671 | ||
672 | uint8_t *scsi_req_get_buf(SCSIRequest *req) | |
673 | { | |
674 | return req->ops->get_buf(req); | |
675 | } | |
676 | ||
677 | static void scsi_clear_unit_attention(SCSIRequest *req) | |
678 | { | |
679 | SCSISense *ua; | |
680 | if (req->dev->unit_attention.key != UNIT_ATTENTION && | |
681 | req->bus->unit_attention.key != UNIT_ATTENTION) { | |
682 | return; | |
683 | } | |
684 | ||
685 | /* | |
686 | * If an INQUIRY command enters the enabled command state, | |
687 | * the device server shall [not] clear any unit attention condition; | |
688 | * See also MMC-6, paragraphs 6.5 and 6.6.2. | |
689 | */ | |
690 | if (req->cmd.buf[0] == INQUIRY || | |
691 | req->cmd.buf[0] == GET_CONFIGURATION || | |
692 | req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) { | |
693 | return; | |
694 | } | |
695 | ||
696 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { | |
697 | ua = &req->dev->unit_attention; | |
698 | } else { | |
699 | ua = &req->bus->unit_attention; | |
700 | } | |
701 | ||
702 | /* | |
703 | * If a REPORT LUNS command enters the enabled command state, [...] | |
704 | * the device server shall clear any pending unit attention condition | |
705 | * with an additional sense code of REPORTED LUNS DATA HAS CHANGED. | |
706 | */ | |
707 | if (req->cmd.buf[0] == REPORT_LUNS && | |
708 | !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc && | |
709 | ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) { | |
710 | return; | |
711 | } | |
712 | ||
713 | *ua = SENSE_CODE(NO_SENSE); | |
714 | } | |
715 | ||
716 | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) | |
717 | { | |
718 | int ret; | |
719 | ||
720 | assert(len >= 14); | |
721 | if (!req->sense_len) { | |
722 | return 0; | |
723 | } | |
724 | ||
725 | ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true); | |
726 | ||
727 | /* | |
728 | * FIXME: clearing unit attention conditions upon autosense should be done | |
729 | * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b | |
730 | * (SAM-5, 5.14). | |
731 | * | |
732 | * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and | |
733 | * 10b for HBAs that do not support it (do not call scsi_req_get_sense). | |
734 | * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b. | |
735 | */ | |
736 | if (req->dev->sense_is_ua) { | |
737 | scsi_device_unit_attention_reported(req->dev); | |
738 | req->dev->sense_len = 0; | |
739 | req->dev->sense_is_ua = false; | |
740 | } | |
741 | return ret; | |
742 | } | |
743 | ||
744 | int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed) | |
745 | { | |
746 | return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed); | |
747 | } | |
748 | ||
749 | void scsi_req_build_sense(SCSIRequest *req, SCSISense sense) | |
750 | { | |
751 | trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag, | |
752 | sense.key, sense.asc, sense.ascq); | |
753 | memset(req->sense, 0, 18); | |
754 | req->sense[0] = 0x70; | |
755 | req->sense[2] = sense.key; | |
756 | req->sense[7] = 10; | |
757 | req->sense[12] = sense.asc; | |
758 | req->sense[13] = sense.ascq; | |
759 | req->sense_len = 18; | |
760 | } | |
761 | ||
762 | static void scsi_req_enqueue_internal(SCSIRequest *req) | |
763 | { | |
764 | assert(!req->enqueued); | |
765 | scsi_req_ref(req); | |
766 | if (req->bus->info->get_sg_list) { | |
767 | req->sg = req->bus->info->get_sg_list(req); | |
768 | } else { | |
769 | req->sg = NULL; | |
770 | } | |
771 | req->enqueued = true; | |
772 | QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); | |
773 | } | |
774 | ||
775 | int32_t scsi_req_enqueue(SCSIRequest *req) | |
776 | { | |
777 | int32_t rc; | |
778 | ||
779 | assert(!req->retry); | |
780 | scsi_req_enqueue_internal(req); | |
781 | scsi_req_ref(req); | |
782 | rc = req->ops->send_command(req, req->cmd.buf); | |
783 | scsi_req_unref(req); | |
784 | return rc; | |
785 | } | |
786 | ||
787 | static void scsi_req_dequeue(SCSIRequest *req) | |
788 | { | |
789 | trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); | |
790 | req->retry = false; | |
791 | if (req->enqueued) { | |
792 | QTAILQ_REMOVE(&req->dev->requests, req, next); | |
793 | req->enqueued = false; | |
794 | scsi_req_unref(req); | |
795 | } | |
796 | } | |
797 | ||
798 | static int scsi_get_performance_length(int num_desc, int type, int data_type) | |
799 | { | |
800 | /* MMC-6, paragraph 6.7. */ | |
801 | switch (type) { | |
802 | case 0: | |
803 | if ((data_type & 3) == 0) { | |
804 | /* Each descriptor is as in Table 295 - Nominal performance. */ | |
805 | return 16 * num_desc + 8; | |
806 | } else { | |
807 | /* Each descriptor is as in Table 296 - Exceptions. */ | |
808 | return 6 * num_desc + 8; | |
809 | } | |
810 | case 1: | |
811 | case 4: | |
812 | case 5: | |
813 | return 8 * num_desc + 8; | |
814 | case 2: | |
815 | return 2048 * num_desc + 8; | |
816 | case 3: | |
817 | return 16 * num_desc + 8; | |
818 | default: | |
819 | return 8; | |
820 | } | |
821 | } | |
822 | ||
823 | static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf) | |
824 | { | |
825 | int byte_block = (buf[2] >> 2) & 0x1; | |
826 | int type = (buf[2] >> 4) & 0x1; | |
827 | int xfer_unit; | |
828 | ||
829 | if (byte_block) { | |
830 | if (type) { | |
831 | xfer_unit = dev->blocksize; | |
832 | } else { | |
833 | xfer_unit = 512; | |
834 | } | |
835 | } else { | |
836 | xfer_unit = 1; | |
837 | } | |
838 | ||
839 | return xfer_unit; | |
840 | } | |
841 | ||
842 | static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf) | |
843 | { | |
844 | int length = buf[2] & 0x3; | |
845 | int xfer; | |
846 | int unit = ata_passthrough_xfer_unit(dev, buf); | |
847 | ||
848 | switch (length) { | |
849 | case 0: | |
850 | case 3: /* USB-specific. */ | |
851 | default: | |
852 | xfer = 0; | |
853 | break; | |
854 | case 1: | |
855 | xfer = buf[3]; | |
856 | break; | |
857 | case 2: | |
858 | xfer = buf[4]; | |
859 | break; | |
860 | } | |
861 | ||
862 | return xfer * unit; | |
863 | } | |
864 | ||
865 | static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf) | |
866 | { | |
867 | int extend = buf[1] & 0x1; | |
868 | int length = buf[2] & 0x3; | |
869 | int xfer; | |
870 | int unit = ata_passthrough_xfer_unit(dev, buf); | |
871 | ||
872 | switch (length) { | |
873 | case 0: | |
874 | case 3: /* USB-specific. */ | |
875 | default: | |
876 | xfer = 0; | |
877 | break; | |
878 | case 1: | |
879 | xfer = buf[4]; | |
880 | xfer |= (extend ? buf[3] << 8 : 0); | |
881 | break; | |
882 | case 2: | |
883 | xfer = buf[6]; | |
884 | xfer |= (extend ? buf[5] << 8 : 0); | |
885 | break; | |
886 | } | |
887 | ||
888 | return xfer * unit; | |
889 | } | |
890 | ||
891 | uint32_t scsi_data_cdb_xfer(uint8_t *buf) | |
892 | { | |
893 | if ((buf[0] >> 5) == 0 && buf[4] == 0) { | |
894 | return 256; | |
895 | } else { | |
896 | return scsi_cdb_xfer(buf); | |
897 | } | |
898 | } | |
899 | ||
900 | uint32_t scsi_cdb_xfer(uint8_t *buf) | |
901 | { | |
902 | switch (buf[0] >> 5) { | |
903 | case 0: | |
904 | return buf[4]; | |
905 | break; | |
906 | case 1: | |
907 | case 2: | |
908 | return lduw_be_p(&buf[7]); | |
909 | break; | |
910 | case 4: | |
911 | return ldl_be_p(&buf[10]) & 0xffffffffULL; | |
912 | break; | |
913 | case 5: | |
914 | return ldl_be_p(&buf[6]) & 0xffffffffULL; | |
915 | break; | |
916 | default: | |
917 | return -1; | |
918 | } | |
919 | } | |
920 | ||
921 | static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | |
922 | { | |
923 | cmd->xfer = scsi_cdb_xfer(buf); | |
924 | switch (buf[0]) { | |
925 | case TEST_UNIT_READY: | |
926 | case REWIND: | |
927 | case START_STOP: | |
928 | case SET_CAPACITY: | |
929 | case WRITE_FILEMARKS: | |
930 | case WRITE_FILEMARKS_16: | |
931 | case SPACE: | |
932 | case RESERVE: | |
933 | case RELEASE: | |
934 | case ERASE: | |
935 | case ALLOW_MEDIUM_REMOVAL: | |
936 | case SEEK_10: | |
937 | case SYNCHRONIZE_CACHE: | |
938 | case SYNCHRONIZE_CACHE_16: | |
939 | case LOCATE_16: | |
940 | case LOCK_UNLOCK_CACHE: | |
941 | case SET_CD_SPEED: | |
942 | case SET_LIMITS: | |
943 | case WRITE_LONG_10: | |
944 | case UPDATE_BLOCK: | |
945 | case RESERVE_TRACK: | |
946 | case SET_READ_AHEAD: | |
947 | case PRE_FETCH: | |
948 | case PRE_FETCH_16: | |
949 | case ALLOW_OVERWRITE: | |
950 | cmd->xfer = 0; | |
951 | break; | |
952 | case VERIFY_10: | |
953 | case VERIFY_12: | |
954 | case VERIFY_16: | |
955 | if ((buf[1] & 2) == 0) { | |
956 | cmd->xfer = 0; | |
957 | } else if ((buf[1] & 4) != 0) { | |
958 | cmd->xfer = 1; | |
959 | } | |
960 | cmd->xfer *= dev->blocksize; | |
961 | break; | |
962 | case MODE_SENSE: | |
963 | break; | |
964 | case WRITE_SAME_10: | |
965 | case WRITE_SAME_16: | |
966 | cmd->xfer = dev->blocksize; | |
967 | break; | |
968 | case READ_CAPACITY_10: | |
969 | cmd->xfer = 8; | |
970 | break; | |
971 | case READ_BLOCK_LIMITS: | |
972 | cmd->xfer = 6; | |
973 | break; | |
974 | case SEND_VOLUME_TAG: | |
975 | /* GPCMD_SET_STREAMING from multimedia commands. */ | |
976 | if (dev->type == TYPE_ROM) { | |
977 | cmd->xfer = buf[10] | (buf[9] << 8); | |
978 | } else { | |
979 | cmd->xfer = buf[9] | (buf[8] << 8); | |
980 | } | |
981 | break; | |
982 | case WRITE_6: | |
983 | /* length 0 means 256 blocks */ | |
984 | if (cmd->xfer == 0) { | |
985 | cmd->xfer = 256; | |
986 | } | |
987 | /* fall through */ | |
988 | case WRITE_10: | |
989 | case WRITE_VERIFY_10: | |
990 | case WRITE_12: | |
991 | case WRITE_VERIFY_12: | |
992 | case WRITE_16: | |
993 | case WRITE_VERIFY_16: | |
994 | cmd->xfer *= dev->blocksize; | |
995 | break; | |
996 | case READ_6: | |
997 | case READ_REVERSE: | |
998 | /* length 0 means 256 blocks */ | |
999 | if (cmd->xfer == 0) { | |
1000 | cmd->xfer = 256; | |
1001 | } | |
1002 | /* fall through */ | |
1003 | case READ_10: | |
1004 | case READ_12: | |
1005 | case READ_16: | |
1006 | cmd->xfer *= dev->blocksize; | |
1007 | break; | |
1008 | case FORMAT_UNIT: | |
1009 | /* MMC mandates the parameter list to be 12-bytes long. Parameters | |
1010 | * for block devices are restricted to the header right now. */ | |
1011 | if (dev->type == TYPE_ROM && (buf[1] & 16)) { | |
1012 | cmd->xfer = 12; | |
1013 | } else { | |
1014 | cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4); | |
1015 | } | |
1016 | break; | |
1017 | case INQUIRY: | |
1018 | case RECEIVE_DIAGNOSTIC: | |
1019 | case SEND_DIAGNOSTIC: | |
1020 | cmd->xfer = buf[4] | (buf[3] << 8); | |
1021 | break; | |
1022 | case READ_CD: | |
1023 | case READ_BUFFER: | |
1024 | case WRITE_BUFFER: | |
1025 | case SEND_CUE_SHEET: | |
1026 | cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | |
1027 | break; | |
1028 | case PERSISTENT_RESERVE_OUT: | |
1029 | cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL; | |
1030 | break; | |
1031 | case ERASE_12: | |
1032 | if (dev->type == TYPE_ROM) { | |
1033 | /* MMC command GET PERFORMANCE. */ | |
1034 | cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8), | |
1035 | buf[10], buf[1] & 0x1f); | |
1036 | } | |
1037 | break; | |
1038 | case MECHANISM_STATUS: | |
1039 | case READ_DVD_STRUCTURE: | |
1040 | case SEND_DVD_STRUCTURE: | |
1041 | case MAINTENANCE_OUT: | |
1042 | case MAINTENANCE_IN: | |
1043 | if (dev->type == TYPE_ROM) { | |
1044 | /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */ | |
1045 | cmd->xfer = buf[9] | (buf[8] << 8); | |
1046 | } | |
1047 | break; | |
1048 | case ATA_PASSTHROUGH_12: | |
1049 | if (dev->type == TYPE_ROM) { | |
1050 | /* BLANK command of MMC */ | |
1051 | cmd->xfer = 0; | |
1052 | } else { | |
1053 | cmd->xfer = ata_passthrough_12_xfer(dev, buf); | |
1054 | } | |
1055 | break; | |
1056 | case ATA_PASSTHROUGH_16: | |
1057 | cmd->xfer = ata_passthrough_16_xfer(dev, buf); | |
1058 | break; | |
1059 | } | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | |
1064 | { | |
1065 | switch (buf[0]) { | |
1066 | /* stream commands */ | |
1067 | case ERASE_12: | |
1068 | case ERASE_16: | |
1069 | cmd->xfer = 0; | |
1070 | break; | |
1071 | case READ_6: | |
1072 | case READ_REVERSE: | |
1073 | case RECOVER_BUFFERED_DATA: | |
1074 | case WRITE_6: | |
1075 | cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); | |
1076 | if (buf[1] & 0x01) { /* fixed */ | |
1077 | cmd->xfer *= dev->blocksize; | |
1078 | } | |
1079 | break; | |
1080 | case READ_16: | |
1081 | case READ_REVERSE_16: | |
1082 | case VERIFY_16: | |
1083 | case WRITE_16: | |
1084 | cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16); | |
1085 | if (buf[1] & 0x01) { /* fixed */ | |
1086 | cmd->xfer *= dev->blocksize; | |
1087 | } | |
1088 | break; | |
1089 | case REWIND: | |
1090 | case LOAD_UNLOAD: | |
1091 | cmd->xfer = 0; | |
1092 | break; | |
1093 | case SPACE_16: | |
1094 | cmd->xfer = buf[13] | (buf[12] << 8); | |
1095 | break; | |
1096 | case READ_POSITION: | |
1097 | switch (buf[1] & 0x1f) /* operation code */ { | |
1098 | case SHORT_FORM_BLOCK_ID: | |
1099 | case SHORT_FORM_VENDOR_SPECIFIC: | |
1100 | cmd->xfer = 20; | |
1101 | break; | |
1102 | case LONG_FORM: | |
1103 | cmd->xfer = 32; | |
1104 | break; | |
1105 | case EXTENDED_FORM: | |
1106 | cmd->xfer = buf[8] | (buf[7] << 8); | |
1107 | break; | |
1108 | default: | |
1109 | return -1; | |
1110 | } | |
1111 | ||
1112 | break; | |
1113 | case FORMAT_UNIT: | |
1114 | cmd->xfer = buf[4] | (buf[3] << 8); | |
1115 | break; | |
1116 | /* generic commands */ | |
1117 | default: | |
1118 | return scsi_req_xfer(cmd, dev, buf); | |
1119 | } | |
1120 | return 0; | |
1121 | } | |
1122 | ||
1123 | static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | |
1124 | { | |
1125 | switch (buf[0]) { | |
1126 | /* medium changer commands */ | |
1127 | case EXCHANGE_MEDIUM: | |
1128 | case INITIALIZE_ELEMENT_STATUS: | |
1129 | case INITIALIZE_ELEMENT_STATUS_WITH_RANGE: | |
1130 | case MOVE_MEDIUM: | |
1131 | case POSITION_TO_ELEMENT: | |
1132 | cmd->xfer = 0; | |
1133 | break; | |
1134 | case READ_ELEMENT_STATUS: | |
1135 | cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16); | |
1136 | break; | |
1137 | ||
1138 | /* generic commands */ | |
1139 | default: | |
1140 | return scsi_req_xfer(cmd, dev, buf); | |
1141 | } | |
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | |
1146 | { | |
1147 | switch (buf[0]) { | |
1148 | /* Scanner commands */ | |
1149 | case OBJECT_POSITION: | |
1150 | cmd->xfer = 0; | |
1151 | break; | |
1152 | case SCAN: | |
1153 | cmd->xfer = buf[4]; | |
1154 | break; | |
1155 | case READ_10: | |
1156 | case SEND: | |
1157 | case GET_WINDOW: | |
1158 | case SET_WINDOW: | |
1159 | cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | |
1160 | break; | |
1161 | default: | |
1162 | /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */ | |
1163 | return scsi_req_xfer(cmd, dev, buf); | |
1164 | } | |
1165 | ||
1166 | return 0; | |
1167 | } | |
1168 | ||
1169 | static void scsi_cmd_xfer_mode(SCSICommand *cmd) | |
1170 | { | |
1171 | if (!cmd->xfer) { | |
1172 | cmd->mode = SCSI_XFER_NONE; | |
1173 | return; | |
1174 | } | |
1175 | switch (cmd->buf[0]) { | |
1176 | case WRITE_6: | |
1177 | case WRITE_10: | |
1178 | case WRITE_VERIFY_10: | |
1179 | case WRITE_12: | |
1180 | case WRITE_VERIFY_12: | |
1181 | case WRITE_16: | |
1182 | case WRITE_VERIFY_16: | |
1183 | case VERIFY_10: | |
1184 | case VERIFY_12: | |
1185 | case VERIFY_16: | |
1186 | case COPY: | |
1187 | case COPY_VERIFY: | |
1188 | case COMPARE: | |
1189 | case CHANGE_DEFINITION: | |
1190 | case LOG_SELECT: | |
1191 | case MODE_SELECT: | |
1192 | case MODE_SELECT_10: | |
1193 | case SEND_DIAGNOSTIC: | |
1194 | case WRITE_BUFFER: | |
1195 | case FORMAT_UNIT: | |
1196 | case REASSIGN_BLOCKS: | |
1197 | case SEARCH_EQUAL: | |
1198 | case SEARCH_HIGH: | |
1199 | case SEARCH_LOW: | |
1200 | case UPDATE_BLOCK: | |
1201 | case WRITE_LONG_10: | |
1202 | case WRITE_SAME_10: | |
1203 | case WRITE_SAME_16: | |
1204 | case UNMAP: | |
1205 | case SEARCH_HIGH_12: | |
1206 | case SEARCH_EQUAL_12: | |
1207 | case SEARCH_LOW_12: | |
1208 | case MEDIUM_SCAN: | |
1209 | case SEND_VOLUME_TAG: | |
1210 | case SEND_CUE_SHEET: | |
1211 | case SEND_DVD_STRUCTURE: | |
1212 | case PERSISTENT_RESERVE_OUT: | |
1213 | case MAINTENANCE_OUT: | |
1214 | case SET_WINDOW: | |
1215 | case SCAN: | |
1216 | /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for | |
1217 | * non-scanner devices, so we only get here for SCAN and not for START_STOP. | |
1218 | */ | |
1219 | cmd->mode = SCSI_XFER_TO_DEV; | |
1220 | break; | |
1221 | case ATA_PASSTHROUGH_12: | |
1222 | case ATA_PASSTHROUGH_16: | |
1223 | /* T_DIR */ | |
1224 | cmd->mode = (cmd->buf[2] & 0x8) ? | |
1225 | SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV; | |
1226 | break; | |
1227 | default: | |
1228 | cmd->mode = SCSI_XFER_FROM_DEV; | |
1229 | break; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | static uint64_t scsi_cmd_lba(SCSICommand *cmd) | |
1234 | { | |
1235 | uint8_t *buf = cmd->buf; | |
1236 | uint64_t lba; | |
1237 | ||
1238 | switch (buf[0] >> 5) { | |
1239 | case 0: | |
1240 | lba = ldl_be_p(&buf[0]) & 0x1fffff; | |
1241 | break; | |
1242 | case 1: | |
1243 | case 2: | |
1244 | case 5: | |
1245 | lba = ldl_be_p(&buf[2]) & 0xffffffffULL; | |
1246 | break; | |
1247 | case 4: | |
1248 | lba = ldq_be_p(&buf[2]); | |
1249 | break; | |
1250 | default: | |
1251 | lba = -1; | |
1252 | ||
1253 | } | |
1254 | return lba; | |
1255 | } | |
1256 | ||
1257 | int scsi_cdb_length(uint8_t *buf) { | |
1258 | int cdb_len; | |
1259 | ||
1260 | switch (buf[0] >> 5) { | |
1261 | case 0: | |
1262 | cdb_len = 6; | |
1263 | break; | |
1264 | case 1: | |
1265 | case 2: | |
1266 | cdb_len = 10; | |
1267 | break; | |
1268 | case 4: | |
1269 | cdb_len = 16; | |
1270 | break; | |
1271 | case 5: | |
1272 | cdb_len = 12; | |
1273 | break; | |
1274 | default: | |
1275 | cdb_len = -1; | |
1276 | } | |
1277 | return cdb_len; | |
1278 | } | |
1279 | ||
1280 | int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf) | |
1281 | { | |
1282 | int rc; | |
1283 | int len; | |
1284 | ||
1285 | cmd->lba = -1; | |
1286 | len = scsi_cdb_length(buf); | |
1287 | if (len < 0) { | |
1288 | return -1; | |
1289 | } | |
1290 | ||
1291 | cmd->len = len; | |
1292 | switch (dev->type) { | |
1293 | case TYPE_TAPE: | |
1294 | rc = scsi_req_stream_xfer(cmd, dev, buf); | |
1295 | break; | |
1296 | case TYPE_MEDIUM_CHANGER: | |
1297 | rc = scsi_req_medium_changer_xfer(cmd, dev, buf); | |
1298 | break; | |
1299 | case TYPE_SCANNER: | |
1300 | rc = scsi_req_scanner_length(cmd, dev, buf); | |
1301 | break; | |
1302 | default: | |
1303 | rc = scsi_req_xfer(cmd, dev, buf); | |
1304 | break; | |
1305 | } | |
1306 | ||
1307 | if (rc != 0) | |
1308 | return rc; | |
1309 | ||
1310 | memcpy(cmd->buf, buf, cmd->len); | |
1311 | scsi_cmd_xfer_mode(cmd); | |
1312 | cmd->lba = scsi_cmd_lba(cmd); | |
1313 | return 0; | |
1314 | } | |
1315 | ||
1316 | void scsi_device_report_change(SCSIDevice *dev, SCSISense sense) | |
1317 | { | |
1318 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | |
1319 | ||
1320 | scsi_device_set_ua(dev, sense); | |
1321 | if (bus->info->change) { | |
1322 | bus->info->change(bus, dev, sense); | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * Predefined sense codes | |
1328 | */ | |
1329 | ||
1330 | /* No sense data available */ | |
1331 | const struct SCSISense sense_code_NO_SENSE = { | |
1332 | .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00 | |
1333 | }; | |
1334 | ||
1335 | /* LUN not ready, Manual intervention required */ | |
1336 | const struct SCSISense sense_code_LUN_NOT_READY = { | |
1337 | .key = NOT_READY, .asc = 0x04, .ascq = 0x03 | |
1338 | }; | |
1339 | ||
1340 | /* LUN not ready, Medium not present */ | |
1341 | const struct SCSISense sense_code_NO_MEDIUM = { | |
1342 | .key = NOT_READY, .asc = 0x3a, .ascq = 0x00 | |
1343 | }; | |
1344 | ||
1345 | /* LUN not ready, medium removal prevented */ | |
1346 | const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = { | |
1347 | .key = NOT_READY, .asc = 0x53, .ascq = 0x02 | |
1348 | }; | |
1349 | ||
1350 | /* Hardware error, internal target failure */ | |
1351 | const struct SCSISense sense_code_TARGET_FAILURE = { | |
1352 | .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00 | |
1353 | }; | |
1354 | ||
1355 | /* Illegal request, invalid command operation code */ | |
1356 | const struct SCSISense sense_code_INVALID_OPCODE = { | |
1357 | .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00 | |
1358 | }; | |
1359 | ||
1360 | /* Illegal request, LBA out of range */ | |
1361 | const struct SCSISense sense_code_LBA_OUT_OF_RANGE = { | |
1362 | .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00 | |
1363 | }; | |
1364 | ||
1365 | /* Illegal request, Invalid field in CDB */ | |
1366 | const struct SCSISense sense_code_INVALID_FIELD = { | |
1367 | .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00 | |
1368 | }; | |
1369 | ||
1370 | /* Illegal request, Invalid field in parameter list */ | |
1371 | const struct SCSISense sense_code_INVALID_PARAM = { | |
1372 | .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00 | |
1373 | }; | |
1374 | ||
1375 | /* Illegal request, Parameter list length error */ | |
1376 | const struct SCSISense sense_code_INVALID_PARAM_LEN = { | |
1377 | .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00 | |
1378 | }; | |
1379 | ||
1380 | /* Illegal request, LUN not supported */ | |
1381 | const struct SCSISense sense_code_LUN_NOT_SUPPORTED = { | |
1382 | .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00 | |
1383 | }; | |
1384 | ||
1385 | /* Illegal request, Saving parameters not supported */ | |
1386 | const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = { | |
1387 | .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00 | |
1388 | }; | |
1389 | ||
1390 | /* Illegal request, Incompatible medium installed */ | |
1391 | const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = { | |
1392 | .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00 | |
1393 | }; | |
1394 | ||
1395 | /* Illegal request, medium removal prevented */ | |
1396 | const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = { | |
1397 | .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02 | |
1398 | }; | |
1399 | ||
1400 | /* Illegal request, Invalid Transfer Tag */ | |
1401 | const struct SCSISense sense_code_INVALID_TAG = { | |
1402 | .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01 | |
1403 | }; | |
1404 | ||
1405 | /* Command aborted, I/O process terminated */ | |
1406 | const struct SCSISense sense_code_IO_ERROR = { | |
1407 | .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06 | |
1408 | }; | |
1409 | ||
1410 | /* Command aborted, I_T Nexus loss occurred */ | |
1411 | const struct SCSISense sense_code_I_T_NEXUS_LOSS = { | |
1412 | .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07 | |
1413 | }; | |
1414 | ||
1415 | /* Command aborted, Logical Unit failure */ | |
1416 | const struct SCSISense sense_code_LUN_FAILURE = { | |
1417 | .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01 | |
1418 | }; | |
1419 | ||
1420 | /* Command aborted, Overlapped Commands Attempted */ | |
1421 | const struct SCSISense sense_code_OVERLAPPED_COMMANDS = { | |
1422 | .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00 | |
1423 | }; | |
1424 | ||
1425 | /* Unit attention, Capacity data has changed */ | |
1426 | const struct SCSISense sense_code_CAPACITY_CHANGED = { | |
1427 | .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09 | |
1428 | }; | |
1429 | ||
1430 | /* Unit attention, Power on, reset or bus device reset occurred */ | |
1431 | const struct SCSISense sense_code_RESET = { | |
1432 | .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00 | |
1433 | }; | |
1434 | ||
1435 | /* Unit attention, No medium */ | |
1436 | const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = { | |
1437 | .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00 | |
1438 | }; | |
1439 | ||
1440 | /* Unit attention, Medium may have changed */ | |
1441 | const struct SCSISense sense_code_MEDIUM_CHANGED = { | |
1442 | .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00 | |
1443 | }; | |
1444 | ||
1445 | /* Unit attention, Reported LUNs data has changed */ | |
1446 | const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = { | |
1447 | .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e | |
1448 | }; | |
1449 | ||
1450 | /* Unit attention, Device internal reset */ | |
1451 | const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = { | |
1452 | .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04 | |
1453 | }; | |
1454 | ||
1455 | /* Data Protection, Write Protected */ | |
1456 | const struct SCSISense sense_code_WRITE_PROTECTED = { | |
1457 | .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00 | |
1458 | }; | |
1459 | ||
1460 | /* Data Protection, Space Allocation Failed Write Protect */ | |
1461 | const struct SCSISense sense_code_SPACE_ALLOC_FAILED = { | |
1462 | .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x07 | |
1463 | }; | |
1464 | ||
1465 | /* | |
1466 | * scsi_build_sense | |
1467 | * | |
1468 | * Convert between fixed and descriptor sense buffers | |
1469 | */ | |
1470 | int scsi_build_sense(uint8_t *in_buf, int in_len, | |
1471 | uint8_t *buf, int len, bool fixed) | |
1472 | { | |
1473 | bool fixed_in; | |
1474 | SCSISense sense; | |
1475 | if (!fixed && len < 8) { | |
1476 | return 0; | |
1477 | } | |
1478 | ||
1479 | if (in_len == 0) { | |
1480 | sense.key = NO_SENSE; | |
1481 | sense.asc = 0; | |
1482 | sense.ascq = 0; | |
1483 | } else { | |
1484 | fixed_in = (in_buf[0] & 2) == 0; | |
1485 | ||
1486 | if (fixed == fixed_in) { | |
1487 | memcpy(buf, in_buf, MIN(len, in_len)); | |
1488 | return MIN(len, in_len); | |
1489 | } | |
1490 | ||
1491 | if (fixed_in) { | |
1492 | sense.key = in_buf[2]; | |
1493 | sense.asc = in_buf[12]; | |
1494 | sense.ascq = in_buf[13]; | |
1495 | } else { | |
1496 | sense.key = in_buf[1]; | |
1497 | sense.asc = in_buf[2]; | |
1498 | sense.ascq = in_buf[3]; | |
1499 | } | |
1500 | } | |
1501 | ||
1502 | memset(buf, 0, len); | |
1503 | if (fixed) { | |
1504 | /* Return fixed format sense buffer */ | |
1505 | buf[0] = 0x70; | |
1506 | buf[2] = sense.key; | |
1507 | buf[7] = 10; | |
1508 | buf[12] = sense.asc; | |
1509 | buf[13] = sense.ascq; | |
1510 | return MIN(len, SCSI_SENSE_LEN); | |
1511 | } else { | |
1512 | /* Return descriptor format sense buffer */ | |
1513 | buf[0] = 0x72; | |
1514 | buf[1] = sense.key; | |
1515 | buf[2] = sense.asc; | |
1516 | buf[3] = sense.ascq; | |
1517 | return 8; | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | const char *scsi_command_name(uint8_t cmd) | |
1522 | { | |
1523 | static const char *names[] = { | |
1524 | [ TEST_UNIT_READY ] = "TEST_UNIT_READY", | |
1525 | [ REWIND ] = "REWIND", | |
1526 | [ REQUEST_SENSE ] = "REQUEST_SENSE", | |
1527 | [ FORMAT_UNIT ] = "FORMAT_UNIT", | |
1528 | [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS", | |
1529 | [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS", | |
1530 | /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */ | |
1531 | [ READ_6 ] = "READ_6", | |
1532 | [ WRITE_6 ] = "WRITE_6", | |
1533 | [ SET_CAPACITY ] = "SET_CAPACITY", | |
1534 | [ READ_REVERSE ] = "READ_REVERSE", | |
1535 | [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS", | |
1536 | [ SPACE ] = "SPACE", | |
1537 | [ INQUIRY ] = "INQUIRY", | |
1538 | [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA", | |
1539 | [ MAINTENANCE_IN ] = "MAINTENANCE_IN", | |
1540 | [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT", | |
1541 | [ MODE_SELECT ] = "MODE_SELECT", | |
1542 | [ RESERVE ] = "RESERVE", | |
1543 | [ RELEASE ] = "RELEASE", | |
1544 | [ COPY ] = "COPY", | |
1545 | [ ERASE ] = "ERASE", | |
1546 | [ MODE_SENSE ] = "MODE_SENSE", | |
1547 | [ START_STOP ] = "START_STOP/LOAD_UNLOAD", | |
1548 | /* LOAD_UNLOAD and START_STOP use the same operation code */ | |
1549 | [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC", | |
1550 | [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC", | |
1551 | [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL", | |
1552 | [ READ_CAPACITY_10 ] = "READ_CAPACITY_10", | |
1553 | [ READ_10 ] = "READ_10", | |
1554 | [ WRITE_10 ] = "WRITE_10", | |
1555 | [ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT", | |
1556 | /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */ | |
1557 | [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10", | |
1558 | [ VERIFY_10 ] = "VERIFY_10", | |
1559 | [ SEARCH_HIGH ] = "SEARCH_HIGH", | |
1560 | [ SEARCH_EQUAL ] = "SEARCH_EQUAL", | |
1561 | [ SEARCH_LOW ] = "SEARCH_LOW", | |
1562 | [ SET_LIMITS ] = "SET_LIMITS", | |
1563 | [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION", | |
1564 | /* READ_POSITION and PRE_FETCH use the same operation code */ | |
1565 | [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE", | |
1566 | [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE", | |
1567 | [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE", | |
1568 | /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */ | |
1569 | [ MEDIUM_SCAN ] = "MEDIUM_SCAN", | |
1570 | [ COMPARE ] = "COMPARE", | |
1571 | [ COPY_VERIFY ] = "COPY_VERIFY", | |
1572 | [ WRITE_BUFFER ] = "WRITE_BUFFER", | |
1573 | [ READ_BUFFER ] = "READ_BUFFER", | |
1574 | [ UPDATE_BLOCK ] = "UPDATE_BLOCK", | |
1575 | [ READ_LONG_10 ] = "READ_LONG_10", | |
1576 | [ WRITE_LONG_10 ] = "WRITE_LONG_10", | |
1577 | [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION", | |
1578 | [ WRITE_SAME_10 ] = "WRITE_SAME_10", | |
1579 | [ UNMAP ] = "UNMAP", | |
1580 | [ READ_TOC ] = "READ_TOC", | |
1581 | [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT", | |
1582 | [ SANITIZE ] = "SANITIZE", | |
1583 | [ GET_CONFIGURATION ] = "GET_CONFIGURATION", | |
1584 | [ LOG_SELECT ] = "LOG_SELECT", | |
1585 | [ LOG_SENSE ] = "LOG_SENSE", | |
1586 | [ MODE_SELECT_10 ] = "MODE_SELECT_10", | |
1587 | [ RESERVE_10 ] = "RESERVE_10", | |
1588 | [ RELEASE_10 ] = "RELEASE_10", | |
1589 | [ MODE_SENSE_10 ] = "MODE_SENSE_10", | |
1590 | [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN", | |
1591 | [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT", | |
1592 | [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16", | |
1593 | [ EXTENDED_COPY ] = "EXTENDED_COPY", | |
1594 | [ ATA_PASSTHROUGH_16 ] = "ATA_PASSTHROUGH_16", | |
1595 | [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN", | |
1596 | [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT", | |
1597 | [ READ_16 ] = "READ_16", | |
1598 | [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE", | |
1599 | [ WRITE_16 ] = "WRITE_16", | |
1600 | [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16", | |
1601 | [ VERIFY_16 ] = "VERIFY_16", | |
1602 | [ PRE_FETCH_16 ] = "PRE_FETCH_16", | |
1603 | [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16", | |
1604 | /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */ | |
1605 | [ LOCATE_16 ] = "LOCATE_16", | |
1606 | [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16", | |
1607 | /* ERASE_16 and WRITE_SAME_16 use the same operation code */ | |
1608 | [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16", | |
1609 | [ WRITE_LONG_16 ] = "WRITE_LONG_16", | |
1610 | [ REPORT_LUNS ] = "REPORT_LUNS", | |
1611 | [ ATA_PASSTHROUGH_12 ] = "BLANK/ATA_PASSTHROUGH_12", | |
1612 | [ MOVE_MEDIUM ] = "MOVE_MEDIUM", | |
1613 | [ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM", | |
1614 | [ READ_12 ] = "READ_12", | |
1615 | [ WRITE_12 ] = "WRITE_12", | |
1616 | [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE", | |
1617 | /* ERASE_12 and GET_PERFORMANCE use the same operation code */ | |
1618 | [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12", | |
1619 | [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12", | |
1620 | [ VERIFY_12 ] = "VERIFY_12", | |
1621 | [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12", | |
1622 | [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12", | |
1623 | [ SEARCH_LOW_12 ] = "SEARCH_LOW_12", | |
1624 | [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS", | |
1625 | [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING", | |
1626 | /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */ | |
1627 | [ READ_CD ] = "READ_CD", | |
1628 | [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12", | |
1629 | [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE", | |
1630 | [ RESERVE_TRACK ] = "RESERVE_TRACK", | |
1631 | [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET", | |
1632 | [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE", | |
1633 | [ SET_CD_SPEED ] = "SET_CD_SPEED", | |
1634 | [ SET_READ_AHEAD ] = "SET_READ_AHEAD", | |
1635 | [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE", | |
1636 | [ MECHANISM_STATUS ] = "MECHANISM_STATUS", | |
1637 | [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION", | |
1638 | [ READ_DISC_INFORMATION ] = "READ_DISC_INFORMATION", | |
1639 | }; | |
1640 | ||
1641 | if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) | |
1642 | return "*UNKNOWN*"; | |
1643 | return names[cmd]; | |
1644 | } | |
1645 | ||
1646 | SCSIRequest *scsi_req_ref(SCSIRequest *req) | |
1647 | { | |
1648 | assert(req->refcount > 0); | |
1649 | req->refcount++; | |
1650 | return req; | |
1651 | } | |
1652 | ||
1653 | void scsi_req_unref(SCSIRequest *req) | |
1654 | { | |
1655 | assert(req->refcount > 0); | |
1656 | if (--req->refcount == 0) { | |
1657 | BusState *qbus = req->dev->qdev.parent_bus; | |
1658 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus); | |
1659 | ||
1660 | if (bus->info->free_request && req->hba_private) { | |
1661 | bus->info->free_request(bus, req->hba_private); | |
1662 | } | |
1663 | if (req->ops->free_req) { | |
1664 | req->ops->free_req(req); | |
1665 | } | |
1666 | object_unref(OBJECT(req->dev)); | |
1667 | object_unref(OBJECT(qbus->parent)); | |
1668 | g_free(req); | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | /* Tell the device that we finished processing this chunk of I/O. It | |
1673 | will start the next chunk or complete the command. */ | |
1674 | void scsi_req_continue(SCSIRequest *req) | |
1675 | { | |
1676 | if (req->io_canceled) { | |
1677 | trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag); | |
1678 | return; | |
1679 | } | |
1680 | trace_scsi_req_continue(req->dev->id, req->lun, req->tag); | |
1681 | if (req->cmd.mode == SCSI_XFER_TO_DEV) { | |
1682 | req->ops->write_data(req); | |
1683 | } else { | |
1684 | req->ops->read_data(req); | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | /* Called by the devices when data is ready for the HBA. The HBA should | |
1689 | start a DMA operation to read or fill the device's data buffer. | |
1690 | Once it completes, calling scsi_req_continue will restart I/O. */ | |
1691 | void scsi_req_data(SCSIRequest *req, int len) | |
1692 | { | |
1693 | uint8_t *buf; | |
1694 | if (req->io_canceled) { | |
1695 | trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); | |
1696 | return; | |
1697 | } | |
1698 | trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); | |
1699 | assert(req->cmd.mode != SCSI_XFER_NONE); | |
1700 | if (!req->sg) { | |
1701 | req->resid -= len; | |
1702 | req->bus->info->transfer_data(req, len); | |
1703 | return; | |
1704 | } | |
1705 | ||
1706 | /* If the device calls scsi_req_data and the HBA specified a | |
1707 | * scatter/gather list, the transfer has to happen in a single | |
1708 | * step. */ | |
1709 | assert(!req->dma_started); | |
1710 | req->dma_started = true; | |
1711 | ||
1712 | buf = scsi_req_get_buf(req); | |
1713 | if (req->cmd.mode == SCSI_XFER_FROM_DEV) { | |
1714 | req->resid = dma_buf_read(buf, len, req->sg); | |
1715 | } else { | |
1716 | req->resid = dma_buf_write(buf, len, req->sg); | |
1717 | } | |
1718 | scsi_req_continue(req); | |
1719 | } | |
1720 | ||
1721 | void scsi_req_print(SCSIRequest *req) | |
1722 | { | |
1723 | FILE *fp = stderr; | |
1724 | int i; | |
1725 | ||
1726 | fprintf(fp, "[%s id=%d] %s", | |
1727 | req->dev->qdev.parent_bus->name, | |
1728 | req->dev->id, | |
1729 | scsi_command_name(req->cmd.buf[0])); | |
1730 | for (i = 1; i < req->cmd.len; i++) { | |
1731 | fprintf(fp, " 0x%02x", req->cmd.buf[i]); | |
1732 | } | |
1733 | switch (req->cmd.mode) { | |
1734 | case SCSI_XFER_NONE: | |
1735 | fprintf(fp, " - none\n"); | |
1736 | break; | |
1737 | case SCSI_XFER_FROM_DEV: | |
1738 | fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer); | |
1739 | break; | |
1740 | case SCSI_XFER_TO_DEV: | |
1741 | fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer); | |
1742 | break; | |
1743 | default: | |
1744 | fprintf(fp, " - Oops\n"); | |
1745 | break; | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | void scsi_req_complete(SCSIRequest *req, int status) | |
1750 | { | |
1751 | assert(req->status == -1); | |
1752 | req->status = status; | |
1753 | ||
1754 | assert(req->sense_len <= sizeof(req->sense)); | |
1755 | if (status == GOOD) { | |
1756 | req->sense_len = 0; | |
1757 | } | |
1758 | ||
1759 | if (req->sense_len) { | |
1760 | memcpy(req->dev->sense, req->sense, req->sense_len); | |
1761 | req->dev->sense_len = req->sense_len; | |
1762 | req->dev->sense_is_ua = (req->ops == &reqops_unit_attention); | |
1763 | } else { | |
1764 | req->dev->sense_len = 0; | |
1765 | req->dev->sense_is_ua = false; | |
1766 | } | |
1767 | ||
1768 | /* | |
1769 | * Unit attention state is now stored in the device's sense buffer | |
1770 | * if the HBA didn't do autosense. Clear the pending unit attention | |
1771 | * flags. | |
1772 | */ | |
1773 | scsi_clear_unit_attention(req); | |
1774 | ||
1775 | scsi_req_ref(req); | |
1776 | scsi_req_dequeue(req); | |
1777 | req->bus->info->complete(req, req->status, req->resid); | |
1778 | ||
1779 | /* Cancelled requests might end up being completed instead of cancelled */ | |
1780 | notifier_list_notify(&req->cancel_notifiers, req); | |
1781 | scsi_req_unref(req); | |
1782 | } | |
1783 | ||
1784 | /* Called by the devices when the request is canceled. */ | |
1785 | void scsi_req_cancel_complete(SCSIRequest *req) | |
1786 | { | |
1787 | assert(req->io_canceled); | |
1788 | if (req->bus->info->cancel) { | |
1789 | req->bus->info->cancel(req); | |
1790 | } | |
1791 | notifier_list_notify(&req->cancel_notifiers, req); | |
1792 | scsi_req_unref(req); | |
1793 | } | |
1794 | ||
1795 | /* Cancel @req asynchronously. @notifier is added to @req's cancellation | |
1796 | * notifier list, the bus will be notified the requests cancellation is | |
1797 | * completed. | |
1798 | * */ | |
1799 | void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier) | |
1800 | { | |
1801 | trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); | |
1802 | if (notifier) { | |
1803 | notifier_list_add(&req->cancel_notifiers, notifier); | |
1804 | } | |
1805 | if (req->io_canceled) { | |
1806 | /* A blk_aio_cancel_async is pending; when it finishes, | |
1807 | * scsi_req_cancel_complete will be called and will | |
1808 | * call the notifier we just added. Just wait for that. | |
1809 | */ | |
1810 | assert(req->aiocb); | |
1811 | return; | |
1812 | } | |
1813 | /* Dropped in scsi_req_cancel_complete. */ | |
1814 | scsi_req_ref(req); | |
1815 | scsi_req_dequeue(req); | |
1816 | req->io_canceled = true; | |
1817 | if (req->aiocb) { | |
1818 | blk_aio_cancel_async(req->aiocb); | |
1819 | } else { | |
1820 | scsi_req_cancel_complete(req); | |
1821 | } | |
1822 | } | |
1823 | ||
1824 | void scsi_req_cancel(SCSIRequest *req) | |
1825 | { | |
1826 | trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); | |
1827 | if (!req->enqueued) { | |
1828 | return; | |
1829 | } | |
1830 | assert(!req->io_canceled); | |
1831 | /* Dropped in scsi_req_cancel_complete. */ | |
1832 | scsi_req_ref(req); | |
1833 | scsi_req_dequeue(req); | |
1834 | req->io_canceled = true; | |
1835 | if (req->aiocb) { | |
1836 | blk_aio_cancel(req->aiocb); | |
1837 | } else { | |
1838 | scsi_req_cancel_complete(req); | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | static int scsi_ua_precedence(SCSISense sense) | |
1843 | { | |
1844 | if (sense.key != UNIT_ATTENTION) { | |
1845 | return INT_MAX; | |
1846 | } | |
1847 | if (sense.asc == 0x29 && sense.ascq == 0x04) { | |
1848 | /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */ | |
1849 | return 1; | |
1850 | } else if (sense.asc == 0x3F && sense.ascq == 0x01) { | |
1851 | /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */ | |
1852 | return 2; | |
1853 | } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) { | |
1854 | /* These two go with "all others". */ | |
1855 | ; | |
1856 | } else if (sense.asc == 0x29 && sense.ascq <= 0x07) { | |
1857 | /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0 | |
1858 | * POWER ON OCCURRED = 1 | |
1859 | * SCSI BUS RESET OCCURRED = 2 | |
1860 | * BUS DEVICE RESET FUNCTION OCCURRED = 3 | |
1861 | * I_T NEXUS LOSS OCCURRED = 7 | |
1862 | */ | |
1863 | return sense.ascq; | |
1864 | } else if (sense.asc == 0x2F && sense.ascq == 0x01) { | |
1865 | /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */ | |
1866 | return 8; | |
1867 | } | |
1868 | return (sense.asc << 8) | sense.ascq; | |
1869 | } | |
1870 | ||
1871 | void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense) | |
1872 | { | |
1873 | int prec1, prec2; | |
1874 | if (sense.key != UNIT_ATTENTION) { | |
1875 | return; | |
1876 | } | |
1877 | trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key, | |
1878 | sense.asc, sense.ascq); | |
1879 | ||
1880 | /* | |
1881 | * Override a pre-existing unit attention condition, except for a more | |
1882 | * important reset condition. | |
1883 | */ | |
1884 | prec1 = scsi_ua_precedence(sdev->unit_attention); | |
1885 | prec2 = scsi_ua_precedence(sense); | |
1886 | if (prec2 < prec1) { | |
1887 | sdev->unit_attention = sense; | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) | |
1892 | { | |
1893 | SCSIRequest *req; | |
1894 | ||
1895 | aio_context_acquire(blk_get_aio_context(sdev->conf.blk)); | |
1896 | while (!QTAILQ_EMPTY(&sdev->requests)) { | |
1897 | req = QTAILQ_FIRST(&sdev->requests); | |
1898 | scsi_req_cancel_async(req, NULL); | |
1899 | } | |
1900 | blk_drain(sdev->conf.blk); | |
1901 | aio_context_release(blk_get_aio_context(sdev->conf.blk)); | |
1902 | scsi_device_set_ua(sdev, sense); | |
1903 | } | |
1904 | ||
1905 | static char *scsibus_get_dev_path(DeviceState *dev) | |
1906 | { | |
1907 | SCSIDevice *d = SCSI_DEVICE(dev); | |
1908 | DeviceState *hba = dev->parent_bus->parent; | |
1909 | char *id; | |
1910 | char *path; | |
1911 | ||
1912 | id = qdev_get_dev_path(hba); | |
1913 | if (id) { | |
1914 | path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun); | |
1915 | } else { | |
1916 | path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun); | |
1917 | } | |
1918 | g_free(id); | |
1919 | return path; | |
1920 | } | |
1921 | ||
1922 | static char *scsibus_get_fw_dev_path(DeviceState *dev) | |
1923 | { | |
1924 | SCSIDevice *d = SCSI_DEVICE(dev); | |
1925 | return g_strdup_printf("channel@%x/%s@%x,%x", d->channel, | |
1926 | qdev_fw_name(dev), d->id, d->lun); | |
1927 | } | |
1928 | ||
1929 | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) | |
1930 | { | |
1931 | BusChild *kid; | |
1932 | SCSIDevice *target_dev = NULL; | |
1933 | ||
1934 | QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) { | |
1935 | DeviceState *qdev = kid->child; | |
1936 | SCSIDevice *dev = SCSI_DEVICE(qdev); | |
1937 | ||
1938 | if (dev->channel == channel && dev->id == id) { | |
1939 | if (dev->lun == lun) { | |
1940 | return dev; | |
1941 | } | |
1942 | target_dev = dev; | |
1943 | } | |
1944 | } | |
1945 | return target_dev; | |
1946 | } | |
1947 | ||
1948 | /* SCSI request list. For simplicity, pv points to the whole device */ | |
1949 | ||
1950 | static int put_scsi_requests(QEMUFile *f, void *pv, size_t size, | |
1951 | VMStateField *field, QJSON *vmdesc) | |
1952 | { | |
1953 | SCSIDevice *s = pv; | |
1954 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
1955 | SCSIRequest *req; | |
1956 | ||
1957 | QTAILQ_FOREACH(req, &s->requests, next) { | |
1958 | assert(!req->io_canceled); | |
1959 | assert(req->status == -1); | |
1960 | assert(req->enqueued); | |
1961 | ||
1962 | qemu_put_sbyte(f, req->retry ? 1 : 2); | |
1963 | qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf)); | |
1964 | qemu_put_be32s(f, &req->tag); | |
1965 | qemu_put_be32s(f, &req->lun); | |
1966 | if (bus->info->save_request) { | |
1967 | bus->info->save_request(f, req); | |
1968 | } | |
1969 | if (req->ops->save_request) { | |
1970 | req->ops->save_request(f, req); | |
1971 | } | |
1972 | } | |
1973 | qemu_put_sbyte(f, 0); | |
1974 | ||
1975 | return 0; | |
1976 | } | |
1977 | ||
1978 | static int get_scsi_requests(QEMUFile *f, void *pv, size_t size, | |
1979 | VMStateField *field) | |
1980 | { | |
1981 | SCSIDevice *s = pv; | |
1982 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
1983 | int8_t sbyte; | |
1984 | ||
1985 | while ((sbyte = qemu_get_sbyte(f)) > 0) { | |
1986 | uint8_t buf[SCSI_CMD_BUF_SIZE]; | |
1987 | uint32_t tag; | |
1988 | uint32_t lun; | |
1989 | SCSIRequest *req; | |
1990 | ||
1991 | qemu_get_buffer(f, buf, sizeof(buf)); | |
1992 | qemu_get_be32s(f, &tag); | |
1993 | qemu_get_be32s(f, &lun); | |
1994 | req = scsi_req_new(s, tag, lun, buf, NULL); | |
1995 | req->retry = (sbyte == 1); | |
1996 | if (bus->info->load_request) { | |
1997 | req->hba_private = bus->info->load_request(f, req); | |
1998 | } | |
1999 | if (req->ops->load_request) { | |
2000 | req->ops->load_request(f, req); | |
2001 | } | |
2002 | ||
2003 | /* Just restart it later. */ | |
2004 | scsi_req_enqueue_internal(req); | |
2005 | ||
2006 | /* At this point, the request will be kept alive by the reference | |
2007 | * added by scsi_req_enqueue_internal, so we can release our reference. | |
2008 | * The HBA of course will add its own reference in the load_request | |
2009 | * callback if it needs to hold on the SCSIRequest. | |
2010 | */ | |
2011 | scsi_req_unref(req); | |
2012 | } | |
2013 | ||
2014 | return 0; | |
2015 | } | |
2016 | ||
2017 | static const VMStateInfo vmstate_info_scsi_requests = { | |
2018 | .name = "scsi-requests", | |
2019 | .get = get_scsi_requests, | |
2020 | .put = put_scsi_requests, | |
2021 | }; | |
2022 | ||
2023 | static bool scsi_sense_state_needed(void *opaque) | |
2024 | { | |
2025 | SCSIDevice *s = opaque; | |
2026 | ||
2027 | return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD; | |
2028 | } | |
2029 | ||
2030 | static const VMStateDescription vmstate_scsi_sense_state = { | |
2031 | .name = "SCSIDevice/sense", | |
2032 | .version_id = 1, | |
2033 | .minimum_version_id = 1, | |
2034 | .needed = scsi_sense_state_needed, | |
2035 | .fields = (VMStateField[]) { | |
2036 | VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, | |
2037 | SCSI_SENSE_BUF_SIZE_OLD, | |
2038 | SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD), | |
2039 | VMSTATE_END_OF_LIST() | |
2040 | } | |
2041 | }; | |
2042 | ||
2043 | const VMStateDescription vmstate_scsi_device = { | |
2044 | .name = "SCSIDevice", | |
2045 | .version_id = 1, | |
2046 | .minimum_version_id = 1, | |
2047 | .fields = (VMStateField[]) { | |
2048 | VMSTATE_UINT8(unit_attention.key, SCSIDevice), | |
2049 | VMSTATE_UINT8(unit_attention.asc, SCSIDevice), | |
2050 | VMSTATE_UINT8(unit_attention.ascq, SCSIDevice), | |
2051 | VMSTATE_BOOL(sense_is_ua, SCSIDevice), | |
2052 | VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD), | |
2053 | VMSTATE_UINT32(sense_len, SCSIDevice), | |
2054 | { | |
2055 | .name = "requests", | |
2056 | .version_id = 0, | |
2057 | .field_exists = NULL, | |
2058 | .size = 0, /* ouch */ | |
2059 | .info = &vmstate_info_scsi_requests, | |
2060 | .flags = VMS_SINGLE, | |
2061 | .offset = 0, | |
2062 | }, | |
2063 | VMSTATE_END_OF_LIST() | |
2064 | }, | |
2065 | .subsections = (const VMStateDescription*[]) { | |
2066 | &vmstate_scsi_sense_state, | |
2067 | NULL | |
2068 | } | |
2069 | }; | |
2070 | ||
2071 | static void scsi_device_class_init(ObjectClass *klass, void *data) | |
2072 | { | |
2073 | DeviceClass *k = DEVICE_CLASS(klass); | |
2074 | set_bit(DEVICE_CATEGORY_STORAGE, k->categories); | |
2075 | k->bus_type = TYPE_SCSI_BUS; | |
2076 | k->realize = scsi_qdev_realize; | |
2077 | k->unrealize = scsi_qdev_unrealize; | |
2078 | k->props = scsi_props; | |
2079 | } | |
2080 | ||
2081 | static void scsi_dev_instance_init(Object *obj) | |
2082 | { | |
2083 | DeviceState *dev = DEVICE(obj); | |
2084 | SCSIDevice *s = SCSI_DEVICE(dev); | |
2085 | ||
2086 | device_add_bootindex_property(obj, &s->conf.bootindex, | |
2087 | "bootindex", NULL, | |
2088 | &s->qdev, NULL); | |
2089 | } | |
2090 | ||
2091 | static const TypeInfo scsi_device_type_info = { | |
2092 | .name = TYPE_SCSI_DEVICE, | |
2093 | .parent = TYPE_DEVICE, | |
2094 | .instance_size = sizeof(SCSIDevice), | |
2095 | .abstract = true, | |
2096 | .class_size = sizeof(SCSIDeviceClass), | |
2097 | .class_init = scsi_device_class_init, | |
2098 | .instance_init = scsi_dev_instance_init, | |
2099 | }; | |
2100 | ||
2101 | static void scsi_register_types(void) | |
2102 | { | |
2103 | type_register_static(&scsi_bus_info); | |
2104 | type_register_static(&scsi_device_type_info); | |
2105 | } | |
2106 | ||
2107 | type_init(scsi_register_types) |