]>
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" |
0d09e41a PB |
5 | #include "hw/scsi/scsi.h" |
6 | #include "block/scsi.h" | |
83c9f4ca | 7 | #include "hw/qdev.h" |
fa1d36df | 8 | #include "sysemu/block-backend.h" |
9c17d615 | 9 | #include "sysemu/blockdev.h" |
5138efec | 10 | #include "trace.h" |
9c17d615 | 11 | #include "sysemu/dma.h" |
f348b6d1 | 12 | #include "qemu/cutils.h" |
d52affa7 | 13 | |
baa1bd89 | 14 | static char *scsibus_get_dev_path(DeviceState *dev); |
db07c0f8 | 15 | static char *scsibus_get_fw_dev_path(DeviceState *dev); |
71544d30 | 16 | static void scsi_req_dequeue(SCSIRequest *req); |
84642435 AH |
17 | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len); |
18 | static void scsi_target_free_buf(SCSIRequest *req); | |
db07c0f8 | 19 | |
3cb75a7c PB |
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 | ||
0d936928 AL |
27 | static void scsi_bus_class_init(ObjectClass *klass, void *data) |
28 | { | |
29 | BusClass *k = BUS_CLASS(klass); | |
bddd763a | 30 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); |
0d936928 AL |
31 | |
32 | k->get_dev_path = scsibus_get_dev_path; | |
33 | k->get_fw_dev_path = scsibus_get_fw_dev_path; | |
bddd763a | 34 | hc->unplug = qdev_simple_device_unplug_cb; |
0d936928 AL |
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, | |
bddd763a IM |
42 | .interfaces = (InterfaceInfo[]) { |
43 | { TYPE_HOTPLUG_HANDLER }, | |
44 | { } | |
45 | } | |
d52affa7 GH |
46 | }; |
47 | static int next_scsi_bus; | |
48 | ||
a818a4b6 | 49 | static void scsi_device_realize(SCSIDevice *s, Error **errp) |
b9eea3e6 AL |
50 | { |
51 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
a818a4b6 FZ |
52 | if (sc->realize) { |
53 | sc->realize(s, errp); | |
b9eea3e6 | 54 | } |
b9eea3e6 AL |
55 | } |
56 | ||
ff34c32c PB |
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 | ||
b9eea3e6 AL |
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 | ||
8d72db68 | 82 | void scsi_device_unit_attention_reported(SCSIDevice *s) |
b9eea3e6 AL |
83 | { |
84 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
85 | if (sc->unit_attention_reported) { | |
86 | sc->unit_attention_reported(s); | |
87 | } | |
88 | } | |
89 | ||
d52affa7 | 90 | /* Create a scsi bus, and attach devices to it. */ |
b1187b51 AF |
91 | void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, |
92 | const SCSIBusInfo *info, const char *bus_name) | |
d52affa7 | 93 | { |
fb17dfe0 | 94 | qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name); |
d52affa7 | 95 | bus->busnr = next_scsi_bus++; |
afd4030c | 96 | bus->info = info; |
bddd763a | 97 | qbus_set_bus_hotplug_handler(BUS(bus), &error_abort); |
d52affa7 GH |
98 | } |
99 | ||
71544d30 PB |
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 | ||
1919631e | 108 | aio_context_acquire(blk_get_aio_context(s->conf.blk)); |
71544d30 PB |
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 | } | |
1919631e | 126 | aio_context_release(blk_get_aio_context(s->conf.blk)); |
71544d30 PB |
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) { | |
d223c104 PB |
144 | AioContext *ctx = blk_get_aio_context(s->conf.blk); |
145 | s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s); | |
71544d30 PB |
146 | qemu_bh_schedule(s->bh); |
147 | } | |
148 | } | |
149 | ||
a818a4b6 | 150 | static void scsi_qdev_realize(DeviceState *qdev, Error **errp) |
d52affa7 | 151 | { |
b9eea3e6 | 152 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
d52affa7 | 153 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); |
7e0380b9 | 154 | SCSIDevice *d; |
a818a4b6 | 155 | Error *local_err = NULL; |
d52affa7 | 156 | |
0d3545e7 | 157 | if (dev->channel > bus->info->max_channel) { |
a818a4b6 FZ |
158 | error_setg(errp, "bad scsi channel id: %d", dev->channel); |
159 | return; | |
0d3545e7 | 160 | } |
7e0380b9 | 161 | if (dev->id != -1 && dev->id > bus->info->max_target) { |
a818a4b6 FZ |
162 | error_setg(errp, "bad scsi device id: %d", dev->id); |
163 | return; | |
d52affa7 | 164 | } |
d3d250bd | 165 | if (dev->lun != -1 && dev->lun > bus->info->max_lun) { |
a818a4b6 FZ |
166 | error_setg(errp, "bad scsi device lun: %d", dev->lun); |
167 | return; | |
d3d250bd | 168 | } |
d52affa7 | 169 | |
7e0380b9 PB |
170 | if (dev->id == -1) { |
171 | int id = -1; | |
172 | if (dev->lun == -1) { | |
173 | dev->lun = 0; | |
174 | } | |
175 | do { | |
0d3545e7 | 176 | d = scsi_device_find(bus, dev->channel, ++id, dev->lun); |
d3d250bd PB |
177 | } while (d && d->lun == dev->lun && id < bus->info->max_target); |
178 | if (d && d->lun == dev->lun) { | |
a818a4b6 FZ |
179 | error_setg(errp, "no free target"); |
180 | return; | |
7e0380b9 PB |
181 | } |
182 | dev->id = id; | |
183 | } else if (dev->lun == -1) { | |
184 | int lun = -1; | |
185 | do { | |
0d3545e7 | 186 | d = scsi_device_find(bus, dev->channel, dev->id, ++lun); |
7e0380b9 | 187 | } while (d && d->lun == lun && lun < bus->info->max_lun); |
d3d250bd | 188 | if (d && d->lun == lun) { |
a818a4b6 FZ |
189 | error_setg(errp, "no free lun"); |
190 | return; | |
7e0380b9 PB |
191 | } |
192 | dev->lun = lun; | |
193 | } else { | |
0d3545e7 | 194 | d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); |
d3d250bd PB |
195 | assert(d); |
196 | if (d->lun == dev->lun && dev != d) { | |
a818a4b6 FZ |
197 | error_setg(errp, "lun already used by '%s'", d->qdev.id); |
198 | return; | |
7e0380b9 | 199 | } |
d52affa7 | 200 | } |
d52affa7 | 201 | |
9af99d98 | 202 | QTAILQ_INIT(&dev->requests); |
a818a4b6 FZ |
203 | scsi_device_realize(dev, &local_err); |
204 | if (local_err) { | |
205 | error_propagate(errp, local_err); | |
206 | return; | |
71544d30 | 207 | } |
a818a4b6 FZ |
208 | dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, |
209 | dev); | |
01985dcf GH |
210 | } |
211 | ||
a818a4b6 | 212 | static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp) |
01985dcf | 213 | { |
b9eea3e6 | 214 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
01985dcf | 215 | |
71544d30 PB |
216 | if (dev->vmsentry) { |
217 | qemu_del_vm_change_state_handler(dev->vmsentry); | |
218 | } | |
fb7b5c0d PB |
219 | |
220 | scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE)); | |
221 | blockdev_mark_auto_del(dev->conf.blk); | |
d52affa7 GH |
222 | } |
223 | ||
d52affa7 | 224 | /* handle legacy '-drive if=scsi,...' cmd line args */ |
4be74634 | 225 | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, |
76534da7 | 226 | int unit, bool removable, int bootindex, |
caad4eb3 | 227 | const char *serial, Error **errp) |
d52affa7 GH |
228 | { |
229 | const char *driver; | |
22647504 | 230 | char *name; |
d52affa7 | 231 | DeviceState *dev; |
caad4eb3 | 232 | Error *err = NULL; |
d52affa7 | 233 | |
4be74634 | 234 | driver = blk_is_sg(blk) ? "scsi-generic" : "scsi-disk"; |
d52affa7 | 235 | dev = qdev_create(&bus->qbus, driver); |
22647504 PB |
236 | name = g_strdup_printf("legacy[%d]", unit); |
237 | object_property_add_child(OBJECT(bus), name, OBJECT(dev), NULL); | |
238 | g_free(name); | |
239 | ||
d52affa7 | 240 | qdev_prop_set_uint32(dev, "scsi-id", unit); |
ce4e7e46 | 241 | if (bootindex >= 0) { |
8dece34f GA |
242 | object_property_set_int(OBJECT(dev), bootindex, "bootindex", |
243 | &error_abort); | |
ce4e7e46 | 244 | } |
89bfe000 | 245 | if (object_property_find(OBJECT(dev), "removable", NULL)) { |
2d1fd261 SH |
246 | qdev_prop_set_bit(dev, "removable", removable); |
247 | } | |
c24e7517 | 248 | if (serial && object_property_find(OBJECT(dev), "serial", NULL)) { |
76534da7 KW |
249 | qdev_prop_set_string(dev, "serial", serial); |
250 | } | |
9b3d111a MA |
251 | qdev_prop_set_drive(dev, "drive", blk, &err); |
252 | if (err) { | |
390e90a9 | 253 | error_propagate(errp, err); |
02a5c4c9 | 254 | object_unparent(OBJECT(dev)); |
18846dee MA |
255 | return NULL; |
256 | } | |
caad4eb3 AF |
257 | object_property_set_bool(OBJECT(dev), true, "realized", &err); |
258 | if (err != NULL) { | |
259 | error_propagate(errp, err); | |
02a5c4c9 | 260 | object_unparent(OBJECT(dev)); |
33e66b86 | 261 | return NULL; |
caad4eb3 | 262 | } |
b9eea3e6 | 263 | return SCSI_DEVICE(dev); |
d52affa7 GH |
264 | } |
265 | ||
caad4eb3 | 266 | void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp) |
d52affa7 | 267 | { |
42e766a2 | 268 | Location loc; |
d52affa7 | 269 | DriveInfo *dinfo; |
caad4eb3 AF |
270 | int unit; |
271 | Error *err = NULL; | |
d52affa7 | 272 | |
42e766a2 | 273 | loc_push_none(&loc); |
d3d250bd | 274 | for (unit = 0; unit <= bus->info->max_target; unit++) { |
d52affa7 GH |
275 | dinfo = drive_get(IF_SCSI, bus->busnr, unit); |
276 | if (dinfo == NULL) { | |
277 | continue; | |
278 | } | |
42e766a2 | 279 | qemu_opts_loc_restore(dinfo->opts); |
4be74634 | 280 | scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo), |
fa1d36df | 281 | unit, false, -1, NULL, &err); |
caad4eb3 AF |
282 | if (err != NULL) { |
283 | error_propagate(errp, err); | |
fa66b909 MA |
284 | break; |
285 | } | |
d52affa7 | 286 | } |
42e766a2 | 287 | loc_pop(&loc); |
d52affa7 | 288 | } |
89b08ae1 | 289 | |
12a08998 PB |
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 | ||
afa46c46 PB |
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 | ||
adcf2754 | 311 | static const struct SCSIReqOps reqops_invalid_opcode = { |
afa46c46 PB |
312 | .size = sizeof(SCSIRequest), |
313 | .send_command = scsi_invalid_command | |
314 | }; | |
315 | ||
6dc06f08 PB |
316 | /* SCSIReqOps implementation for unit attention conditions. */ |
317 | ||
318 | static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf) | |
319 | { | |
0bf8264e | 320 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { |
6dc06f08 PB |
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 | ||
adcf2754 | 329 | static const struct SCSIReqOps reqops_unit_attention = { |
6dc06f08 PB |
330 | .size = sizeof(SCSIRequest), |
331 | .send_command = scsi_unit_attention | |
332 | }; | |
333 | ||
fdaef069 PB |
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; | |
84642435 AH |
342 | uint8_t *buf; |
343 | int buf_len; | |
fdaef069 PB |
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 | { | |
0866aca1 | 358 | BusChild *kid; |
ba74307c | 359 | int i, len, n; |
0d3545e7 | 360 | int channel, id; |
ba74307c PB |
361 | bool found_lun0; |
362 | ||
fdaef069 PB |
363 | if (r->req.cmd.xfer < 16) { |
364 | return false; | |
365 | } | |
366 | if (r->req.cmd.buf[2] > 2) { | |
367 | return false; | |
368 | } | |
0d3545e7 | 369 | channel = r->req.dev->channel; |
ba74307c PB |
370 | id = r->req.dev->id; |
371 | found_lun0 = false; | |
372 | n = 0; | |
0866aca1 AL |
373 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
374 | DeviceState *qdev = kid->child; | |
b9eea3e6 | 375 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 376 | |
0d3545e7 | 377 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
378 | if (dev->lun == 0) { |
379 | found_lun0 = true; | |
380 | } | |
381 | n += 8; | |
382 | } | |
383 | } | |
384 | if (!found_lun0) { | |
385 | n += 8; | |
386 | } | |
ba74307c | 387 | |
84642435 AH |
388 | scsi_target_alloc_buf(&r->req, n + 8); |
389 | ||
390 | len = MIN(n + 8, r->req.cmd.xfer & ~7); | |
fdaef069 | 391 | memset(r->buf, 0, len); |
84642435 | 392 | stl_be_p(&r->buf[0], n); |
ba74307c | 393 | i = found_lun0 ? 8 : 16; |
0866aca1 AL |
394 | QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { |
395 | DeviceState *qdev = kid->child; | |
b9eea3e6 | 396 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 397 | |
0d3545e7 | 398 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
399 | store_lun(&r->buf[i], dev->lun); |
400 | i += 8; | |
401 | } | |
fdaef069 | 402 | } |
ba74307c PB |
403 | assert(i == n + 8); |
404 | r->len = len; | |
fdaef069 PB |
405 | return true; |
406 | } | |
407 | ||
408 | static bool scsi_target_emulate_inquiry(SCSITargetReq *r) | |
409 | { | |
410 | assert(r->req.dev->lun != r->req.lun); | |
84642435 AH |
411 | |
412 | scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN); | |
413 | ||
fdaef069 PB |
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]; | |
fdaef069 PB |
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 */ | |
84642435 | 438 | assert(r->len < r->buf_len); |
fdaef069 PB |
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 */ | |
84642435 | 449 | r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN); |
fdaef069 PB |
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 */ | |
afd4030c | 458 | r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */ |
fdaef069 PB |
459 | memcpy(&r->buf[8], "QEMU ", 8); |
460 | memcpy(&r->buf[16], "QEMU TARGET ", 16); | |
35c2c8dc | 461 | pstrcpy((char *) &r->buf[32], 4, qemu_hw_version()); |
fdaef069 PB |
462 | } |
463 | return true; | |
464 | } | |
465 | ||
6959e508 JL |
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 | ||
fdaef069 PB |
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; | |
739df215 | 489 | case REQUEST_SENSE: |
6959e508 | 490 | scsi_target_alloc_buf(&r->req, scsi_sense_len(req)); |
8b2a04ee | 491 | r->len = scsi_device_get_sense(r->req.dev, r->buf, |
84642435 | 492 | MIN(req->cmd.xfer, r->buf_len), |
739df215 | 493 | (req->cmd.buf[1] & 1) == 0); |
3653d8c4 | 494 | if (r->req.dev->sense_is_ua) { |
b9eea3e6 | 495 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
496 | r->req.dev->sense_len = 0; |
497 | r->req.dev->sense_is_ua = false; | |
498 | } | |
739df215 | 499 | break; |
1cb27d92 PB |
500 | case TEST_UNIT_READY: |
501 | break; | |
fdaef069 PB |
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 | ||
84642435 AH |
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 | ||
adcf2754 | 556 | static const struct SCSIReqOps reqops_target_command = { |
fdaef069 PB |
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, | |
84642435 | 561 | .free_req = scsi_target_free_buf, |
fdaef069 PB |
562 | }; |
563 | ||
564 | ||
adcf2754 PB |
565 | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, |
566 | uint32_t tag, uint32_t lun, void *hba_private) | |
89b08ae1 GH |
567 | { |
568 | SCSIRequest *req; | |
cac3c384 PB |
569 | SCSIBus *bus = scsi_bus_from_device(d); |
570 | BusState *qbus = BUS(bus); | |
61e68b3f FZ |
571 | const int memset_off = offsetof(SCSIRequest, sense) |
572 | + sizeof(req->sense); | |
89b08ae1 | 573 | |
633dccb4 | 574 | req = g_malloc(reqops->size); |
61e68b3f | 575 | memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off); |
5c6c0e51 | 576 | req->refcount = 1; |
cac3c384 | 577 | req->bus = bus; |
89b08ae1 GH |
578 | req->dev = d; |
579 | req->tag = tag; | |
580 | req->lun = lun; | |
c5bf71a9 | 581 | req->hba_private = hba_private; |
ed3a34a3 | 582 | req->status = -1; |
8dbd4574 | 583 | req->ops = reqops; |
cac3c384 PB |
584 | object_ref(OBJECT(d)); |
585 | object_ref(OBJECT(qbus->parent)); | |
8e0a9320 | 586 | notifier_list_init(&req->cancel_notifiers); |
5138efec | 587 | trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); |
89b08ae1 GH |
588 | return req; |
589 | } | |
590 | ||
c5bf71a9 | 591 | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, |
c39ce112 | 592 | uint8_t *buf, void *hba_private) |
43a2b339 | 593 | { |
6dc06f08 | 594 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); |
769998a1 | 595 | const SCSIReqOps *ops; |
ff34c32c | 596 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d); |
c39ce112 | 597 | SCSIRequest *req; |
769998a1 PB |
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 | } | |
afa46c46 | 621 | |
ff34c32c PB |
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 | ||
769998a1 | 628 | if (ret != 0) { |
afa46c46 PB |
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 { | |
769998a1 | 632 | assert(cmd.len != 0); |
afa46c46 PB |
633 | trace_scsi_req_parsed(d->id, lun, tag, buf[0], |
634 | cmd.mode, cmd.xfer); | |
3b6ffe50 | 635 | if (cmd.lba != -1) { |
afa46c46 PB |
636 | trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0], |
637 | cmd.lba); | |
638 | } | |
fdaef069 | 639 | |
12a08998 PB |
640 | if (cmd.xfer > INT32_MAX) { |
641 | req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private); | |
769998a1 PB |
642 | } else if (ops) { |
643 | req = scsi_req_alloc(ops, d, tag, lun, hba_private); | |
fdaef069 | 644 | } else { |
b9eea3e6 | 645 | req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); |
fdaef069 | 646 | } |
afa46c46 PB |
647 | } |
648 | ||
649 | req->cmd = cmd; | |
01e95455 PB |
650 | req->resid = req->cmd.xfer; |
651 | ||
98254542 PB |
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 | ||
c39ce112 | 669 | return req; |
43a2b339 PB |
670 | } |
671 | ||
0c34459b PB |
672 | uint8_t *scsi_req_get_buf(SCSIRequest *req) |
673 | { | |
12010e7b | 674 | return req->ops->get_buf(req); |
0c34459b PB |
675 | } |
676 | ||
6dc06f08 PB |
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 | ||
74382217 HR |
716 | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) |
717 | { | |
6dc06f08 PB |
718 | int ret; |
719 | ||
b45ef674 PB |
720 | assert(len >= 14); |
721 | if (!req->sense_len) { | |
74382217 HR |
722 | return 0; |
723 | } | |
6dc06f08 PB |
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). | |
3653d8c4 | 734 | * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b. |
6dc06f08 | 735 | */ |
3653d8c4 | 736 | if (req->dev->sense_is_ua) { |
b9eea3e6 | 737 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
738 | req->dev->sense_len = 0; |
739 | req->dev->sense_is_ua = false; | |
740 | } | |
6dc06f08 | 741 | return ret; |
b45ef674 PB |
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); | |
77e4743c | 754 | req->sense[0] = 0x70; |
b45ef674 | 755 | req->sense[2] = sense.key; |
eae31cb9 | 756 | req->sense[7] = 10; |
b45ef674 PB |
757 | req->sense[12] = sense.asc; |
758 | req->sense[13] = sense.ascq; | |
759 | req->sense_len = 18; | |
74382217 HR |
760 | } |
761 | ||
63f740dd | 762 | static void scsi_req_enqueue_internal(SCSIRequest *req) |
89b08ae1 | 763 | { |
5c6c0e51 HR |
764 | assert(!req->enqueued); |
765 | scsi_req_ref(req); | |
3d5aba97 PB |
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 | } | |
5c6c0e51 HR |
771 | req->enqueued = true; |
772 | QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); | |
63f740dd | 773 | } |
fc4f0754 | 774 | |
63f740dd PB |
775 | int32_t scsi_req_enqueue(SCSIRequest *req) |
776 | { | |
777 | int32_t rc; | |
778 | ||
779 | assert(!req->retry); | |
780 | scsi_req_enqueue_internal(req); | |
fc4f0754 | 781 | scsi_req_ref(req); |
c39ce112 | 782 | rc = req->ops->send_command(req, req->cmd.buf); |
fc4f0754 PB |
783 | scsi_req_unref(req); |
784 | return rc; | |
89b08ae1 GH |
785 | } |
786 | ||
a1f0cce2 | 787 | static void scsi_req_dequeue(SCSIRequest *req) |
e8637c90 | 788 | { |
5138efec | 789 | trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); |
71544d30 | 790 | req->retry = false; |
e8637c90 JK |
791 | if (req->enqueued) { |
792 | QTAILQ_REMOVE(&req->dev->requests, req, next); | |
793 | req->enqueued = false; | |
ad2d30f7 | 794 | scsi_req_unref(req); |
e8637c90 JK |
795 | } |
796 | } | |
797 | ||
06b86357 PB |
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 | ||
e4b65262 CM |
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 | ||
1894df02 | 842 | static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf) |
e4b65262 CM |
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. */ | |
d83c951c | 851 | default: |
e4b65262 CM |
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 | ||
1894df02 | 865 | static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf) |
e4b65262 CM |
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. */ | |
d83c951c | 875 | default: |
e4b65262 CM |
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 | ||
1894df02 | 891 | uint32_t scsi_data_cdb_xfer(uint8_t *buf) |
bb729f75 PB |
892 | { |
893 | if ((buf[0] >> 5) == 0 && buf[4] == 0) { | |
894 | return 256; | |
895 | } else { | |
1894df02 | 896 | return scsi_cdb_xfer(buf); |
bb729f75 PB |
897 | } |
898 | } | |
899 | ||
1894df02 | 900 | uint32_t scsi_cdb_xfer(uint8_t *buf) |
2ec749cb | 901 | { |
2599aece | 902 | switch (buf[0] >> 5) { |
2ec749cb | 903 | case 0: |
bb729f75 | 904 | return buf[4]; |
2ec749cb GH |
905 | break; |
906 | case 1: | |
907 | case 2: | |
bb729f75 | 908 | return lduw_be_p(&buf[7]); |
2ec749cb GH |
909 | break; |
910 | case 4: | |
bb729f75 | 911 | return ldl_be_p(&buf[10]) & 0xffffffffULL; |
2ec749cb GH |
912 | break; |
913 | case 5: | |
bb729f75 | 914 | return ldl_be_p(&buf[6]) & 0xffffffffULL; |
2ec749cb GH |
915 | break; |
916 | default: | |
917 | return -1; | |
918 | } | |
bb729f75 | 919 | } |
2ec749cb | 920 | |
1894df02 | 921 | static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
bb729f75 | 922 | { |
1894df02 | 923 | cmd->xfer = scsi_cdb_xfer(buf); |
2599aece | 924 | switch (buf[0]) { |
2ec749cb | 925 | case TEST_UNIT_READY: |
5e30a07d | 926 | case REWIND: |
2ec749cb | 927 | case START_STOP: |
00a01ad4 | 928 | case SET_CAPACITY: |
2ec749cb | 929 | case WRITE_FILEMARKS: |
06b86357 | 930 | case WRITE_FILEMARKS_16: |
2ec749cb | 931 | case SPACE: |
a5e3d9ef BK |
932 | case RESERVE: |
933 | case RELEASE: | |
2ec749cb GH |
934 | case ERASE: |
935 | case ALLOW_MEDIUM_REMOVAL: | |
2ec749cb GH |
936 | case SEEK_10: |
937 | case SYNCHRONIZE_CACHE: | |
06b86357 PB |
938 | case SYNCHRONIZE_CACHE_16: |
939 | case LOCATE_16: | |
2ec749cb | 940 | case LOCK_UNLOCK_CACHE: |
2ec749cb GH |
941 | case SET_CD_SPEED: |
942 | case SET_LIMITS: | |
5e30a07d | 943 | case WRITE_LONG_10: |
2ec749cb | 944 | case UPDATE_BLOCK: |
06b86357 PB |
945 | case RESERVE_TRACK: |
946 | case SET_READ_AHEAD: | |
947 | case PRE_FETCH: | |
948 | case PRE_FETCH_16: | |
949 | case ALLOW_OVERWRITE: | |
2599aece | 950 | cmd->xfer = 0; |
2ec749cb | 951 | break; |
d12ad44c PB |
952 | case VERIFY_10: |
953 | case VERIFY_12: | |
954 | case VERIFY_16: | |
955 | if ((buf[1] & 2) == 0) { | |
956 | cmd->xfer = 0; | |
7ef8cf9a | 957 | } else if ((buf[1] & 4) != 0) { |
d12ad44c PB |
958 | cmd->xfer = 1; |
959 | } | |
960 | cmd->xfer *= dev->blocksize; | |
961 | break; | |
2ec749cb GH |
962 | case MODE_SENSE: |
963 | break; | |
5e30a07d | 964 | case WRITE_SAME_10: |
a5ee9085 PB |
965 | case WRITE_SAME_16: |
966 | cmd->xfer = dev->blocksize; | |
2ec749cb | 967 | break; |
5e30a07d | 968 | case READ_CAPACITY_10: |
2599aece | 969 | cmd->xfer = 8; |
2ec749cb GH |
970 | break; |
971 | case READ_BLOCK_LIMITS: | |
2599aece | 972 | cmd->xfer = 6; |
2ec749cb | 973 | break; |
2ec749cb | 974 | case SEND_VOLUME_TAG: |
06b86357 PB |
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 | } | |
2ec749cb | 981 | break; |
f62d0594 PB |
982 | case WRITE_6: |
983 | /* length 0 means 256 blocks */ | |
984 | if (cmd->xfer == 0) { | |
985 | cmd->xfer = 256; | |
986 | } | |
c4ce4c4b | 987 | /* fall through */ |
2ec749cb | 988 | case WRITE_10: |
5e30a07d | 989 | case WRITE_VERIFY_10: |
2ec749cb GH |
990 | case WRITE_12: |
991 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
992 | case WRITE_16: |
993 | case WRITE_VERIFY_16: | |
2599aece | 994 | cmd->xfer *= dev->blocksize; |
2ec749cb | 995 | break; |
2ec749cb GH |
996 | case READ_6: |
997 | case READ_REVERSE: | |
f62d0594 PB |
998 | /* length 0 means 256 blocks */ |
999 | if (cmd->xfer == 0) { | |
1000 | cmd->xfer = 256; | |
1001 | } | |
c4ce4c4b | 1002 | /* fall through */ |
f62d0594 | 1003 | case READ_10: |
2ec749cb | 1004 | case READ_12: |
bd536cf3 | 1005 | case READ_16: |
2599aece | 1006 | cmd->xfer *= dev->blocksize; |
2ec749cb | 1007 | break; |
06b86357 PB |
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; | |
2ec749cb | 1017 | case INQUIRY: |
06b86357 PB |
1018 | case RECEIVE_DIAGNOSTIC: |
1019 | case SEND_DIAGNOSTIC: | |
2599aece | 1020 | cmd->xfer = buf[4] | (buf[3] << 8); |
2ec749cb | 1021 | break; |
06b86357 PB |
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: | |
c7126d5b NB |
1041 | case MAINTENANCE_OUT: |
1042 | case MAINTENANCE_IN: | |
2599aece | 1043 | if (dev->type == TYPE_ROM) { |
c7126d5b | 1044 | /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */ |
2599aece | 1045 | cmd->xfer = buf[9] | (buf[8] << 8); |
c7126d5b NB |
1046 | } |
1047 | break; | |
e4b65262 CM |
1048 | case ATA_PASSTHROUGH_12: |
1049 | if (dev->type == TYPE_ROM) { | |
1050 | /* BLANK command of MMC */ | |
1051 | cmd->xfer = 0; | |
1052 | } else { | |
1894df02 | 1053 | cmd->xfer = ata_passthrough_12_xfer(dev, buf); |
e4b65262 CM |
1054 | } |
1055 | break; | |
1056 | case ATA_PASSTHROUGH_16: | |
1894df02 | 1057 | cmd->xfer = ata_passthrough_16_xfer(dev, buf); |
e4b65262 | 1058 | break; |
2ec749cb GH |
1059 | } |
1060 | return 0; | |
1061 | } | |
1062 | ||
1894df02 | 1063 | static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
2ec749cb | 1064 | { |
2599aece | 1065 | switch (buf[0]) { |
2ec749cb | 1066 | /* stream commands */ |
06b86357 PB |
1067 | case ERASE_12: |
1068 | case ERASE_16: | |
1069 | cmd->xfer = 0; | |
1070 | break; | |
2ec749cb GH |
1071 | case READ_6: |
1072 | case READ_REVERSE: | |
1073 | case RECOVER_BUFFERED_DATA: | |
1074 | case WRITE_6: | |
2599aece PB |
1075 | cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); |
1076 | if (buf[1] & 0x01) { /* fixed */ | |
1077 | cmd->xfer *= dev->blocksize; | |
1078 | } | |
2ec749cb | 1079 | break; |
065c2599 PB |
1080 | case READ_16: |
1081 | case READ_REVERSE_16: | |
1082 | case VERIFY_16: | |
1083 | case WRITE_16: | |
065c2599 PB |
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; | |
2ec749cb | 1089 | case REWIND: |
15e58a21 | 1090 | case LOAD_UNLOAD: |
2599aece | 1091 | cmd->xfer = 0; |
2ec749cb | 1092 | break; |
06b86357 PB |
1093 | case SPACE_16: |
1094 | cmd->xfer = buf[13] | (buf[12] << 8); | |
1095 | break; | |
1096 | case READ_POSITION: | |
9ce1bb2d CH |
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 | ||
06b86357 PB |
1112 | break; |
1113 | case FORMAT_UNIT: | |
1114 | cmd->xfer = buf[4] | (buf[3] << 8); | |
1115 | break; | |
2ec749cb GH |
1116 | /* generic commands */ |
1117 | default: | |
1894df02 | 1118 | return scsi_req_xfer(cmd, dev, buf); |
2ec749cb GH |
1119 | } |
1120 | return 0; | |
1121 | } | |
1122 | ||
1894df02 | 1123 | static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
40723a99 CH |
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: | |
1894df02 | 1140 | return scsi_req_xfer(cmd, dev, buf); |
40723a99 CH |
1141 | } |
1142 | return 0; | |
1143 | } | |
1144 | ||
297b044a JL |
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 | } | |
40723a99 | 1168 | |
2599aece | 1169 | static void scsi_cmd_xfer_mode(SCSICommand *cmd) |
97a06435 | 1170 | { |
a5ee9085 PB |
1171 | if (!cmd->xfer) { |
1172 | cmd->mode = SCSI_XFER_NONE; | |
1173 | return; | |
1174 | } | |
2599aece | 1175 | switch (cmd->buf[0]) { |
97a06435 GH |
1176 | case WRITE_6: |
1177 | case WRITE_10: | |
5e30a07d | 1178 | case WRITE_VERIFY_10: |
97a06435 GH |
1179 | case WRITE_12: |
1180 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
1181 | case WRITE_16: |
1182 | case WRITE_VERIFY_16: | |
d12ad44c PB |
1183 | case VERIFY_10: |
1184 | case VERIFY_12: | |
1185 | case VERIFY_16: | |
97a06435 GH |
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: | |
97a06435 GH |
1197 | case SEARCH_EQUAL: |
1198 | case SEARCH_HIGH: | |
1199 | case SEARCH_LOW: | |
1200 | case UPDATE_BLOCK: | |
5e30a07d HR |
1201 | case WRITE_LONG_10: |
1202 | case WRITE_SAME_10: | |
a5ee9085 | 1203 | case WRITE_SAME_16: |
381b634c | 1204 | case UNMAP: |
97a06435 GH |
1205 | case SEARCH_HIGH_12: |
1206 | case SEARCH_EQUAL_12: | |
1207 | case SEARCH_LOW_12: | |
97a06435 GH |
1208 | case MEDIUM_SCAN: |
1209 | case SEND_VOLUME_TAG: | |
06b86357 PB |
1210 | case SEND_CUE_SHEET: |
1211 | case SEND_DVD_STRUCTURE: | |
01bedeba | 1212 | case PERSISTENT_RESERVE_OUT: |
c7126d5b | 1213 | case MAINTENANCE_OUT: |
297b044a JL |
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 | */ | |
2599aece | 1219 | cmd->mode = SCSI_XFER_TO_DEV; |
97a06435 | 1220 | break; |
e4b65262 CM |
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; | |
97a06435 | 1227 | default: |
a5ee9085 | 1228 | cmd->mode = SCSI_XFER_FROM_DEV; |
97a06435 GH |
1229 | break; |
1230 | } | |
1231 | } | |
1232 | ||
2599aece | 1233 | static uint64_t scsi_cmd_lba(SCSICommand *cmd) |
2ec749cb | 1234 | { |
2599aece | 1235 | uint8_t *buf = cmd->buf; |
2ec749cb GH |
1236 | uint64_t lba; |
1237 | ||
1238 | switch (buf[0] >> 5) { | |
1239 | case 0: | |
bd5da232 | 1240 | lba = ldl_be_p(&buf[0]) & 0x1fffff; |
2ec749cb GH |
1241 | break; |
1242 | case 1: | |
1243 | case 2: | |
bd5da232 | 1244 | case 5: |
06b86357 | 1245 | lba = ldl_be_p(&buf[2]) & 0xffffffffULL; |
2ec749cb GH |
1246 | break; |
1247 | case 4: | |
bd5da232 | 1248 | lba = ldq_be_p(&buf[2]); |
2ec749cb GH |
1249 | break; |
1250 | default: | |
1251 | lba = -1; | |
1252 | ||
1253 | } | |
1254 | return lba; | |
1255 | } | |
1256 | ||
1894df02 HR |
1257 | int scsi_cdb_length(uint8_t *buf) { |
1258 | int cdb_len; | |
2ec749cb | 1259 | |
28b70c9d PB |
1260 | switch (buf[0] >> 5) { |
1261 | case 0: | |
1894df02 | 1262 | cdb_len = 6; |
28b70c9d PB |
1263 | break; |
1264 | case 1: | |
1265 | case 2: | |
1894df02 | 1266 | cdb_len = 10; |
28b70c9d PB |
1267 | break; |
1268 | case 4: | |
1894df02 | 1269 | cdb_len = 16; |
28b70c9d PB |
1270 | break; |
1271 | case 5: | |
1894df02 | 1272 | cdb_len = 12; |
28b70c9d PB |
1273 | break; |
1274 | default: | |
1894df02 | 1275 | cdb_len = -1; |
28b70c9d | 1276 | } |
1894df02 HR |
1277 | return cdb_len; |
1278 | } | |
1279 | ||
1280 | int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf) | |
1281 | { | |
1282 | int rc; | |
c170aad8 | 1283 | int len; |
1894df02 HR |
1284 | |
1285 | cmd->lba = -1; | |
c170aad8 PB |
1286 | len = scsi_cdb_length(buf); |
1287 | if (len < 0) { | |
1288 | return -1; | |
1289 | } | |
28b70c9d | 1290 | |
c170aad8 | 1291 | cmd->len = len; |
40723a99 CH |
1292 | switch (dev->type) { |
1293 | case TYPE_TAPE: | |
1894df02 | 1294 | rc = scsi_req_stream_xfer(cmd, dev, buf); |
40723a99 CH |
1295 | break; |
1296 | case TYPE_MEDIUM_CHANGER: | |
1894df02 | 1297 | rc = scsi_req_medium_changer_xfer(cmd, dev, buf); |
40723a99 | 1298 | break; |
297b044a JL |
1299 | case TYPE_SCANNER: |
1300 | rc = scsi_req_scanner_length(cmd, dev, buf); | |
1301 | break; | |
40723a99 | 1302 | default: |
1894df02 | 1303 | rc = scsi_req_xfer(cmd, dev, buf); |
40723a99 | 1304 | break; |
2ec749cb | 1305 | } |
40723a99 | 1306 | |
2ec749cb GH |
1307 | if (rc != 0) |
1308 | return rc; | |
1309 | ||
afa46c46 PB |
1310 | memcpy(cmd->buf, buf, cmd->len); |
1311 | scsi_cmd_xfer_mode(cmd); | |
1312 | cmd->lba = scsi_cmd_lba(cmd); | |
2ec749cb GH |
1313 | return 0; |
1314 | } | |
ed3a34a3 | 1315 | |
53200fad PB |
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 | ||
a1f0cce2 HR |
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 | ||
68bb01f3 MA |
1345 | /* LUN not ready, medium removal prevented */ |
1346 | const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = { | |
93161b69 | 1347 | .key = NOT_READY, .asc = 0x53, .ascq = 0x02 |
68bb01f3 MA |
1348 | }; |
1349 | ||
a1f0cce2 HR |
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 | ||
380feaff PB |
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 | ||
a1f0cce2 HR |
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 | ||
a872a304 PB |
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 */ | |
67cc61e4 | 1391 | const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = { |
a872a304 PB |
1392 | .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00 |
1393 | }; | |
1394 | ||
68bb01f3 MA |
1395 | /* Illegal request, medium removal prevented */ |
1396 | const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = { | |
93161b69 | 1397 | .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02 |
68bb01f3 MA |
1398 | }; |
1399 | ||
9ec557bd HG |
1400 | /* Illegal request, Invalid Transfer Tag */ |
1401 | const struct SCSISense sense_code_INVALID_TAG = { | |
1402 | .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01 | |
1403 | }; | |
1404 | ||
a1f0cce2 HR |
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 | ||
9ec557bd HG |
1420 | /* Command aborted, Overlapped Commands Attempted */ |
1421 | const struct SCSISense sense_code_OVERLAPPED_COMMANDS = { | |
1422 | .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00 | |
1423 | }; | |
1424 | ||
aaebacef PB |
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 | ||
a872a304 PB |
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 | ||
8a9c16f6 PB |
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 | ||
a872a304 PB |
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 | ||
6a8a685c RS |
1455 | /* Data Protection, Write Protected */ |
1456 | const struct SCSISense sense_code_WRITE_PROTECTED = { | |
1457 | .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00 | |
1458 | }; | |
1459 | ||
703dd81a PB |
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 | ||
a1f0cce2 HR |
1465 | /* |
1466 | * scsi_build_sense | |
1467 | * | |
b45ef674 | 1468 | * Convert between fixed and descriptor sense buffers |
a1f0cce2 | 1469 | */ |
b45ef674 PB |
1470 | int scsi_build_sense(uint8_t *in_buf, int in_len, |
1471 | uint8_t *buf, int len, bool fixed) | |
a1f0cce2 | 1472 | { |
b45ef674 PB |
1473 | bool fixed_in; |
1474 | SCSISense sense; | |
a1f0cce2 HR |
1475 | if (!fixed && len < 8) { |
1476 | return 0; | |
1477 | } | |
1478 | ||
b45ef674 PB |
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 | ||
a1f0cce2 HR |
1502 | memset(buf, 0, len); |
1503 | if (fixed) { | |
1504 | /* Return fixed format sense buffer */ | |
77e4743c | 1505 | buf[0] = 0x70; |
a1f0cce2 | 1506 | buf[2] = sense.key; |
eae31cb9 | 1507 | buf[7] = 10; |
a1f0cce2 HR |
1508 | buf[12] = sense.asc; |
1509 | buf[13] = sense.ascq; | |
84642435 | 1510 | return MIN(len, SCSI_SENSE_LEN); |
a1f0cce2 HR |
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 | ||
b9e77bc7 | 1521 | const char *scsi_command_name(uint8_t cmd) |
ec766865 GH |
1522 | { |
1523 | static const char *names[] = { | |
1524 | [ TEST_UNIT_READY ] = "TEST_UNIT_READY", | |
5e30a07d | 1525 | [ REWIND ] = "REWIND", |
ec766865 GH |
1526 | [ REQUEST_SENSE ] = "REQUEST_SENSE", |
1527 | [ FORMAT_UNIT ] = "FORMAT_UNIT", | |
1528 | [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS", | |
40723a99 CH |
1529 | [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS", |
1530 | /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */ | |
ec766865 GH |
1531 | [ READ_6 ] = "READ_6", |
1532 | [ WRITE_6 ] = "WRITE_6", | |
00a01ad4 | 1533 | [ SET_CAPACITY ] = "SET_CAPACITY", |
ec766865 GH |
1534 | [ READ_REVERSE ] = "READ_REVERSE", |
1535 | [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS", | |
1536 | [ SPACE ] = "SPACE", | |
1537 | [ INQUIRY ] = "INQUIRY", | |
1538 | [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA", | |
c7126d5b NB |
1539 | [ MAINTENANCE_IN ] = "MAINTENANCE_IN", |
1540 | [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT", | |
ec766865 GH |
1541 | [ MODE_SELECT ] = "MODE_SELECT", |
1542 | [ RESERVE ] = "RESERVE", | |
1543 | [ RELEASE ] = "RELEASE", | |
1544 | [ COPY ] = "COPY", | |
1545 | [ ERASE ] = "ERASE", | |
1546 | [ MODE_SENSE ] = "MODE_SENSE", | |
15e58a21 CH |
1547 | [ START_STOP ] = "START_STOP/LOAD_UNLOAD", |
1548 | /* LOAD_UNLOAD and START_STOP use the same operation code */ | |
ec766865 GH |
1549 | [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC", |
1550 | [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC", | |
1551 | [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL", | |
5e30a07d | 1552 | [ READ_CAPACITY_10 ] = "READ_CAPACITY_10", |
ec766865 GH |
1553 | [ READ_10 ] = "READ_10", |
1554 | [ WRITE_10 ] = "WRITE_10", | |
40723a99 CH |
1555 | [ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT", |
1556 | /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */ | |
5e30a07d HR |
1557 | [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10", |
1558 | [ VERIFY_10 ] = "VERIFY_10", | |
ec766865 GH |
1559 | [ SEARCH_HIGH ] = "SEARCH_HIGH", |
1560 | [ SEARCH_EQUAL ] = "SEARCH_EQUAL", | |
1561 | [ SEARCH_LOW ] = "SEARCH_LOW", | |
1562 | [ SET_LIMITS ] = "SET_LIMITS", | |
00a01ad4 | 1563 | [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION", |
545557d4 | 1564 | /* READ_POSITION and PRE_FETCH use the same operation code */ |
ec766865 GH |
1565 | [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE", |
1566 | [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE", | |
40723a99 CH |
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 */ | |
ec766865 GH |
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", | |
5e30a07d HR |
1575 | [ READ_LONG_10 ] = "READ_LONG_10", |
1576 | [ WRITE_LONG_10 ] = "WRITE_LONG_10", | |
ec766865 | 1577 | [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION", |
5e30a07d HR |
1578 | [ WRITE_SAME_10 ] = "WRITE_SAME_10", |
1579 | [ UNMAP ] = "UNMAP", | |
ec766865 | 1580 | [ READ_TOC ] = "READ_TOC", |
5e30a07d | 1581 | [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT", |
3e46d87d | 1582 | [ SANITIZE ] = "SANITIZE", |
5e30a07d | 1583 | [ GET_CONFIGURATION ] = "GET_CONFIGURATION", |
ec766865 GH |
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", | |
5e30a07d HR |
1592 | [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16", |
1593 | [ EXTENDED_COPY ] = "EXTENDED_COPY", | |
e4b65262 | 1594 | [ ATA_PASSTHROUGH_16 ] = "ATA_PASSTHROUGH_16", |
5e30a07d HR |
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", | |
00a01ad4 PB |
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 */ | |
5e30a07d | 1605 | [ LOCATE_16 ] = "LOCATE_16", |
00a01ad4 | 1606 | [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16", |
48bb9f53 | 1607 | /* ERASE_16 and WRITE_SAME_16 use the same operation code */ |
f6515262 | 1608 | [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16", |
5e30a07d HR |
1609 | [ WRITE_LONG_16 ] = "WRITE_LONG_16", |
1610 | [ REPORT_LUNS ] = "REPORT_LUNS", | |
e4b65262 | 1611 | [ ATA_PASSTHROUGH_12 ] = "BLANK/ATA_PASSTHROUGH_12", |
ec766865 | 1612 | [ MOVE_MEDIUM ] = "MOVE_MEDIUM", |
40723a99 | 1613 | [ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM", |
ec766865 GH |
1614 | [ READ_12 ] = "READ_12", |
1615 | [ WRITE_12 ] = "WRITE_12", | |
00a01ad4 PB |
1616 | [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE", |
1617 | /* ERASE_12 and GET_PERFORMANCE use the same operation code */ | |
f6515262 | 1618 | [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12", |
ec766865 | 1619 | [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12", |
5e30a07d | 1620 | [ VERIFY_12 ] = "VERIFY_12", |
ec766865 GH |
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", | |
00a01ad4 PB |
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", | |
5e30a07d | 1628 | [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12", |
00a01ad4 PB |
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", | |
ec766865 | 1633 | [ SET_CD_SPEED ] = "SET_CD_SPEED", |
00a01ad4 PB |
1634 | [ SET_READ_AHEAD ] = "SET_READ_AHEAD", |
1635 | [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE", | |
1636 | [ MECHANISM_STATUS ] = "MECHANISM_STATUS", | |
b9e77bc7 AK |
1637 | [ GET_EVENT_STATUS_NOTIFICATION ] = "GET_EVENT_STATUS_NOTIFICATION", |
1638 | [ READ_DISC_INFORMATION ] = "READ_DISC_INFORMATION", | |
ec766865 GH |
1639 | }; |
1640 | ||
1641 | if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) | |
1642 | return "*UNKNOWN*"; | |
1643 | return names[cmd]; | |
1644 | } | |
1645 | ||
ad2d30f7 PB |
1646 | SCSIRequest *scsi_req_ref(SCSIRequest *req) |
1647 | { | |
8e86b93c | 1648 | assert(req->refcount > 0); |
ad2d30f7 PB |
1649 | req->refcount++; |
1650 | return req; | |
1651 | } | |
1652 | ||
1653 | void scsi_req_unref(SCSIRequest *req) | |
1654 | { | |
68bd348a | 1655 | assert(req->refcount > 0); |
ad2d30f7 | 1656 | if (--req->refcount == 0) { |
cac3c384 PB |
1657 | BusState *qbus = req->dev->qdev.parent_bus; |
1658 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus); | |
1659 | ||
8e86b93c PB |
1660 | if (bus->info->free_request && req->hba_private) { |
1661 | bus->info->free_request(bus, req->hba_private); | |
1662 | } | |
12010e7b PB |
1663 | if (req->ops->free_req) { |
1664 | req->ops->free_req(req); | |
ad2d30f7 | 1665 | } |
cac3c384 PB |
1666 | object_unref(OBJECT(req->dev)); |
1667 | object_unref(OBJECT(qbus->parent)); | |
633dccb4 | 1668 | g_free(req); |
ad2d30f7 PB |
1669 | } |
1670 | } | |
1671 | ||
ad3376cc PB |
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 | { | |
6f6710aa PB |
1676 | if (req->io_canceled) { |
1677 | trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag); | |
1678 | return; | |
1679 | } | |
ad3376cc PB |
1680 | trace_scsi_req_continue(req->dev->id, req->lun, req->tag); |
1681 | if (req->cmd.mode == SCSI_XFER_TO_DEV) { | |
12010e7b | 1682 | req->ops->write_data(req); |
ad3376cc | 1683 | } else { |
12010e7b | 1684 | req->ops->read_data(req); |
ad3376cc PB |
1685 | } |
1686 | } | |
1687 | ||
ab9adc88 PB |
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. | |
ad3376cc | 1690 | Once it completes, calling scsi_req_continue will restart I/O. */ |
ab9adc88 PB |
1691 | void scsi_req_data(SCSIRequest *req, int len) |
1692 | { | |
3d5aba97 | 1693 | uint8_t *buf; |
e88c591d PB |
1694 | if (req->io_canceled) { |
1695 | trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); | |
01e95455 | 1696 | return; |
e88c591d | 1697 | } |
01e95455 PB |
1698 | trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); |
1699 | assert(req->cmd.mode != SCSI_XFER_NONE); | |
3d5aba97 PB |
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); | |
ab9adc88 PB |
1719 | } |
1720 | ||
ec766865 GH |
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 | ||
682a9b21 | 1749 | void scsi_req_complete(SCSIRequest *req, int status) |
ed3a34a3 | 1750 | { |
682a9b21 PB |
1751 | assert(req->status == -1); |
1752 | req->status = status; | |
b45ef674 | 1753 | |
335f560f | 1754 | assert(req->sense_len <= sizeof(req->sense)); |
b45ef674 PB |
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); | |
3653d8c4 PB |
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; | |
b45ef674 | 1766 | } |
b45ef674 | 1767 | |
6dc06f08 PB |
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 | ||
ad2d30f7 | 1775 | scsi_req_ref(req); |
e8637c90 | 1776 | scsi_req_dequeue(req); |
01e95455 | 1777 | req->bus->info->complete(req, req->status, req->resid); |
8e0a9320 FZ |
1778 | |
1779 | /* Cancelled requests might end up being completed instead of cancelled */ | |
1780 | notifier_list_notify(&req->cancel_notifiers, req); | |
ad2d30f7 | 1781 | scsi_req_unref(req); |
ed3a34a3 | 1782 | } |
db07c0f8 | 1783 | |
d5776465 FZ |
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 | } | |
8e0a9320 | 1791 | notifier_list_notify(&req->cancel_notifiers, req); |
d5776465 FZ |
1792 | scsi_req_unref(req); |
1793 | } | |
1794 | ||
8e0a9320 FZ |
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 | } | |
3daa4107 PB |
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. */ | |
8e0a9320 FZ |
1814 | scsi_req_ref(req); |
1815 | scsi_req_dequeue(req); | |
1816 | req->io_canceled = true; | |
1817 | if (req->aiocb) { | |
4be74634 | 1818 | blk_aio_cancel_async(req->aiocb); |
2aeba9d8 FZ |
1819 | } else { |
1820 | scsi_req_cancel_complete(req); | |
8e0a9320 FZ |
1821 | } |
1822 | } | |
1823 | ||
94d3f98a PB |
1824 | void scsi_req_cancel(SCSIRequest *req) |
1825 | { | |
814589c4 | 1826 | trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); |
e88c591d PB |
1827 | if (!req->enqueued) { |
1828 | return; | |
94d3f98a | 1829 | } |
3daa4107 PB |
1830 | assert(!req->io_canceled); |
1831 | /* Dropped in scsi_req_cancel_complete. */ | |
94d3f98a PB |
1832 | scsi_req_ref(req); |
1833 | scsi_req_dequeue(req); | |
e88c591d | 1834 | req->io_canceled = true; |
a83cfd12 | 1835 | if (req->aiocb) { |
4be74634 | 1836 | blk_aio_cancel(req->aiocb); |
488eef2f PB |
1837 | } else { |
1838 | scsi_req_cancel_complete(req); | |
e88c591d | 1839 | } |
94d3f98a PB |
1840 | } |
1841 | ||
e48e84ea PB |
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 | ||
c7b48872 | 1891 | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) |
c557e889 PB |
1892 | { |
1893 | SCSIRequest *req; | |
1894 | ||
8aad35f6 | 1895 | aio_context_acquire(blk_get_aio_context(sdev->conf.blk)); |
c557e889 PB |
1896 | while (!QTAILQ_EMPTY(&sdev->requests)) { |
1897 | req = QTAILQ_FIRST(&sdev->requests); | |
8aad35f6 | 1898 | scsi_req_cancel_async(req, NULL); |
c557e889 | 1899 | } |
8aad35f6 PB |
1900 | blk_drain(sdev->conf.blk); |
1901 | aio_context_release(blk_get_aio_context(sdev->conf.blk)); | |
e48e84ea | 1902 | scsi_device_set_ua(sdev, sense); |
c557e889 PB |
1903 | } |
1904 | ||
baa1bd89 PB |
1905 | static char *scsibus_get_dev_path(DeviceState *dev) |
1906 | { | |
e1dc6815 | 1907 | SCSIDevice *d = SCSI_DEVICE(dev); |
baa1bd89 | 1908 | DeviceState *hba = dev->parent_bus->parent; |
09e5ab63 | 1909 | char *id; |
b7c8c35f | 1910 | char *path; |
baa1bd89 | 1911 | |
09e5ab63 | 1912 | id = qdev_get_dev_path(hba); |
baa1bd89 | 1913 | if (id) { |
b7c8c35f | 1914 | path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun); |
baa1bd89 | 1915 | } else { |
b7c8c35f | 1916 | path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun); |
baa1bd89 | 1917 | } |
b7c8c35f PB |
1918 | g_free(id); |
1919 | return path; | |
baa1bd89 PB |
1920 | } |
1921 | ||
db07c0f8 GN |
1922 | static char *scsibus_get_fw_dev_path(DeviceState *dev) |
1923 | { | |
b9eea3e6 | 1924 | SCSIDevice *d = SCSI_DEVICE(dev); |
a5cf8262 JM |
1925 | return g_strdup_printf("channel@%x/%s@%x,%x", d->channel, |
1926 | qdev_fw_name(dev), d->id, d->lun); | |
f48a7a6e PB |
1927 | } |
1928 | ||
0d3545e7 | 1929 | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) |
f48a7a6e | 1930 | { |
0866aca1 | 1931 | BusChild *kid; |
f48a7a6e | 1932 | SCSIDevice *target_dev = NULL; |
db07c0f8 | 1933 | |
0866aca1 AL |
1934 | QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) { |
1935 | DeviceState *qdev = kid->child; | |
b9eea3e6 | 1936 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
db07c0f8 | 1937 | |
0d3545e7 | 1938 | if (dev->channel == channel && dev->id == id) { |
f48a7a6e PB |
1939 | if (dev->lun == lun) { |
1940 | return dev; | |
1941 | } | |
1942 | target_dev = dev; | |
1943 | } | |
1944 | } | |
1945 | return target_dev; | |
db07c0f8 | 1946 | } |
b9eea3e6 | 1947 | |
63f740dd PB |
1948 | /* SCSI request list. For simplicity, pv points to the whole device */ |
1949 | ||
2c21ee76 JD |
1950 | static int put_scsi_requests(QEMUFile *f, void *pv, size_t size, |
1951 | VMStateField *field, QJSON *vmdesc) | |
63f740dd PB |
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); | |
63f740dd PB |
1960 | assert(req->enqueued); |
1961 | ||
18eef3bc | 1962 | qemu_put_sbyte(f, req->retry ? 1 : 2); |
63f740dd PB |
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); | |
2c21ee76 JD |
1974 | |
1975 | return 0; | |
63f740dd PB |
1976 | } |
1977 | ||
2c21ee76 JD |
1978 | static int get_scsi_requests(QEMUFile *f, void *pv, size_t size, |
1979 | VMStateField *field) | |
63f740dd PB |
1980 | { |
1981 | SCSIDevice *s = pv; | |
1982 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
18eef3bc | 1983 | int8_t sbyte; |
63f740dd | 1984 | |
18eef3bc | 1985 | while ((sbyte = qemu_get_sbyte(f)) > 0) { |
63f740dd PB |
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); | |
18eef3bc | 1995 | req->retry = (sbyte == 1); |
63f740dd PB |
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. */ | |
63f740dd PB |
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 | ||
12badfc2 | 2017 | static const VMStateInfo vmstate_info_scsi_requests = { |
63f740dd PB |
2018 | .name = "scsi-requests", |
2019 | .get = get_scsi_requests, | |
2020 | .put = put_scsi_requests, | |
2021 | }; | |
2022 | ||
2e323f03 FZ |
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, | |
5cd8cada | 2034 | .needed = scsi_sense_state_needed, |
d49805ae | 2035 | .fields = (VMStateField[]) { |
2e323f03 FZ |
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 | ||
63f740dd PB |
2043 | const VMStateDescription vmstate_scsi_device = { |
2044 | .name = "SCSIDevice", | |
2045 | .version_id = 1, | |
2046 | .minimum_version_id = 1, | |
63f740dd PB |
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), | |
2e323f03 | 2052 | VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD), |
63f740dd PB |
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() | |
2e323f03 | 2064 | }, |
5cd8cada JQ |
2065 | .subsections = (const VMStateDescription*[]) { |
2066 | &vmstate_scsi_sense_state, | |
2067 | NULL | |
63f740dd PB |
2068 | } |
2069 | }; | |
2070 | ||
39bffca2 AL |
2071 | static void scsi_device_class_init(ObjectClass *klass, void *data) |
2072 | { | |
2073 | DeviceClass *k = DEVICE_CLASS(klass); | |
125ee0ed | 2074 | set_bit(DEVICE_CATEGORY_STORAGE, k->categories); |
a818a4b6 FZ |
2075 | k->bus_type = TYPE_SCSI_BUS; |
2076 | k->realize = scsi_qdev_realize; | |
a818a4b6 FZ |
2077 | k->unrealize = scsi_qdev_unrealize; |
2078 | k->props = scsi_props; | |
39bffca2 AL |
2079 | } |
2080 | ||
44fb6337 GA |
2081 | static void scsi_dev_instance_init(Object *obj) |
2082 | { | |
2083 | DeviceState *dev = DEVICE(obj); | |
e1dc6815 | 2084 | SCSIDevice *s = SCSI_DEVICE(dev); |
44fb6337 GA |
2085 | |
2086 | device_add_bootindex_property(obj, &s->conf.bootindex, | |
2087 | "bootindex", NULL, | |
2088 | &s->qdev, NULL); | |
2089 | } | |
2090 | ||
8c43a6f0 | 2091 | static const TypeInfo scsi_device_type_info = { |
b9eea3e6 AL |
2092 | .name = TYPE_SCSI_DEVICE, |
2093 | .parent = TYPE_DEVICE, | |
2094 | .instance_size = sizeof(SCSIDevice), | |
2095 | .abstract = true, | |
2096 | .class_size = sizeof(SCSIDeviceClass), | |
39bffca2 | 2097 | .class_init = scsi_device_class_init, |
44fb6337 | 2098 | .instance_init = scsi_dev_instance_init, |
b9eea3e6 AL |
2099 | }; |
2100 | ||
83f7d43a | 2101 | static void scsi_register_types(void) |
b9eea3e6 | 2102 | { |
0d936928 | 2103 | type_register_static(&scsi_bus_info); |
b9eea3e6 AL |
2104 | type_register_static(&scsi_device_type_info); |
2105 | } | |
2106 | ||
83f7d43a | 2107 | type_init(scsi_register_types) |