]>
Commit | Line | Data |
---|---|---|
d52affa7 | 1 | #include "hw.h" |
2f792016 | 2 | #include "qemu-error.h" |
43b443b6 | 3 | #include "scsi.h" |
2ec749cb | 4 | #include "scsi-defs.h" |
d52affa7 | 5 | #include "qdev.h" |
2446333c | 6 | #include "blockdev.h" |
5138efec | 7 | #include "trace.h" |
3d5aba97 | 8 | #include "dma.h" |
d52affa7 | 9 | |
baa1bd89 | 10 | static char *scsibus_get_dev_path(DeviceState *dev); |
db07c0f8 | 11 | static char *scsibus_get_fw_dev_path(DeviceState *dev); |
afa46c46 | 12 | static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf); |
71544d30 | 13 | static void scsi_req_dequeue(SCSIRequest *req); |
db07c0f8 | 14 | |
d52affa7 GH |
15 | static struct BusInfo scsi_bus_info = { |
16 | .name = "SCSI", | |
17 | .size = sizeof(SCSIBus), | |
baa1bd89 | 18 | .get_dev_path = scsibus_get_dev_path, |
db07c0f8 | 19 | .get_fw_dev_path = scsibus_get_fw_dev_path, |
d52affa7 | 20 | .props = (Property[]) { |
0d3545e7 | 21 | DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), |
d52affa7 | 22 | DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), |
7e0380b9 | 23 | DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), |
d52affa7 GH |
24 | DEFINE_PROP_END_OF_LIST(), |
25 | }, | |
26 | }; | |
27 | static int next_scsi_bus; | |
28 | ||
b9eea3e6 AL |
29 | static int scsi_device_init(SCSIDevice *s) |
30 | { | |
31 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
32 | if (sc->init) { | |
33 | return sc->init(s); | |
34 | } | |
35 | return 0; | |
36 | } | |
37 | ||
38 | static void scsi_device_destroy(SCSIDevice *s) | |
39 | { | |
40 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
41 | if (sc->destroy) { | |
42 | sc->destroy(s); | |
43 | } | |
44 | } | |
45 | ||
46 | static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun, | |
47 | uint8_t *buf, void *hba_private) | |
48 | { | |
49 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
50 | if (sc->alloc_req) { | |
51 | return sc->alloc_req(s, tag, lun, buf, hba_private); | |
52 | } | |
53 | ||
54 | return NULL; | |
55 | } | |
56 | ||
57 | static void scsi_device_unit_attention_reported(SCSIDevice *s) | |
58 | { | |
59 | SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | |
60 | if (sc->unit_attention_reported) { | |
61 | sc->unit_attention_reported(s); | |
62 | } | |
63 | } | |
64 | ||
d52affa7 | 65 | /* Create a scsi bus, and attach devices to it. */ |
afd4030c | 66 | void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info) |
d52affa7 | 67 | { |
ca9c39fa | 68 | qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL); |
d52affa7 | 69 | bus->busnr = next_scsi_bus++; |
afd4030c | 70 | bus->info = info; |
cb23117b | 71 | bus->qbus.allow_hotplug = 1; |
d52affa7 GH |
72 | } |
73 | ||
71544d30 PB |
74 | static void scsi_dma_restart_bh(void *opaque) |
75 | { | |
76 | SCSIDevice *s = opaque; | |
77 | SCSIRequest *req, *next; | |
78 | ||
79 | qemu_bh_delete(s->bh); | |
80 | s->bh = NULL; | |
81 | ||
82 | QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { | |
83 | scsi_req_ref(req); | |
84 | if (req->retry) { | |
85 | req->retry = false; | |
86 | switch (req->cmd.mode) { | |
87 | case SCSI_XFER_FROM_DEV: | |
88 | case SCSI_XFER_TO_DEV: | |
89 | scsi_req_continue(req); | |
90 | break; | |
91 | case SCSI_XFER_NONE: | |
5d0d2467 | 92 | assert(!req->sg); |
71544d30 PB |
93 | scsi_req_dequeue(req); |
94 | scsi_req_enqueue(req); | |
95 | break; | |
96 | } | |
97 | } | |
98 | scsi_req_unref(req); | |
99 | } | |
100 | } | |
101 | ||
102 | void scsi_req_retry(SCSIRequest *req) | |
103 | { | |
104 | /* No need to save a reference, because scsi_dma_restart_bh just | |
105 | * looks at the request list. */ | |
106 | req->retry = true; | |
107 | } | |
108 | ||
109 | static void scsi_dma_restart_cb(void *opaque, int running, RunState state) | |
110 | { | |
111 | SCSIDevice *s = opaque; | |
112 | ||
113 | if (!running) { | |
114 | return; | |
115 | } | |
116 | if (!s->bh) { | |
117 | s->bh = qemu_bh_new(scsi_dma_restart_bh, s); | |
118 | qemu_bh_schedule(s->bh); | |
119 | } | |
120 | } | |
121 | ||
d307af79 | 122 | static int scsi_qdev_init(DeviceState *qdev) |
d52affa7 | 123 | { |
b9eea3e6 | 124 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
d52affa7 | 125 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); |
7e0380b9 | 126 | SCSIDevice *d; |
01985dcf | 127 | int rc = -1; |
d52affa7 | 128 | |
0d3545e7 PB |
129 | if (dev->channel > bus->info->max_channel) { |
130 | error_report("bad scsi channel id: %d", dev->channel); | |
131 | goto err; | |
132 | } | |
7e0380b9 | 133 | if (dev->id != -1 && dev->id > bus->info->max_target) { |
1ecda02b | 134 | error_report("bad scsi device id: %d", dev->id); |
d52affa7 GH |
135 | goto err; |
136 | } | |
d3d250bd PB |
137 | if (dev->lun != -1 && dev->lun > bus->info->max_lun) { |
138 | error_report("bad scsi device lun: %d", dev->lun); | |
139 | goto err; | |
140 | } | |
d52affa7 | 141 | |
7e0380b9 PB |
142 | if (dev->id == -1) { |
143 | int id = -1; | |
144 | if (dev->lun == -1) { | |
145 | dev->lun = 0; | |
146 | } | |
147 | do { | |
0d3545e7 | 148 | d = scsi_device_find(bus, dev->channel, ++id, dev->lun); |
d3d250bd PB |
149 | } while (d && d->lun == dev->lun && id < bus->info->max_target); |
150 | if (d && d->lun == dev->lun) { | |
7e0380b9 PB |
151 | error_report("no free target"); |
152 | goto err; | |
153 | } | |
154 | dev->id = id; | |
155 | } else if (dev->lun == -1) { | |
156 | int lun = -1; | |
157 | do { | |
0d3545e7 | 158 | d = scsi_device_find(bus, dev->channel, dev->id, ++lun); |
7e0380b9 | 159 | } while (d && d->lun == lun && lun < bus->info->max_lun); |
d3d250bd | 160 | if (d && d->lun == lun) { |
7e0380b9 PB |
161 | error_report("no free lun"); |
162 | goto err; | |
163 | } | |
164 | dev->lun = lun; | |
165 | } else { | |
0d3545e7 | 166 | d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); |
d3d250bd PB |
167 | assert(d); |
168 | if (d->lun == dev->lun && dev != d) { | |
7e0380b9 PB |
169 | qdev_free(&d->qdev); |
170 | } | |
d52affa7 | 171 | } |
d52affa7 | 172 | |
9af99d98 | 173 | QTAILQ_INIT(&dev->requests); |
b9eea3e6 | 174 | rc = scsi_device_init(dev); |
71544d30 PB |
175 | if (rc == 0) { |
176 | dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, | |
177 | dev); | |
178 | } | |
d52affa7 GH |
179 | |
180 | err: | |
01985dcf GH |
181 | return rc; |
182 | } | |
183 | ||
184 | static int scsi_qdev_exit(DeviceState *qdev) | |
185 | { | |
b9eea3e6 | 186 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
01985dcf | 187 | |
71544d30 PB |
188 | if (dev->vmsentry) { |
189 | qemu_del_vm_change_state_handler(dev->vmsentry); | |
190 | } | |
b9eea3e6 | 191 | scsi_device_destroy(dev); |
01985dcf | 192 | return 0; |
d52affa7 GH |
193 | } |
194 | ||
d52affa7 | 195 | /* handle legacy '-drive if=scsi,...' cmd line args */ |
2d1fd261 | 196 | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, |
ce4e7e46 | 197 | int unit, bool removable, int bootindex) |
d52affa7 GH |
198 | { |
199 | const char *driver; | |
200 | DeviceState *dev; | |
201 | ||
f8b6cc00 | 202 | driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk"; |
d52affa7 GH |
203 | dev = qdev_create(&bus->qbus, driver); |
204 | qdev_prop_set_uint32(dev, "scsi-id", unit); | |
ce4e7e46 PB |
205 | if (bootindex >= 0) { |
206 | qdev_prop_set_int32(dev, "bootindex", bootindex); | |
207 | } | |
2d1fd261 SH |
208 | if (qdev_prop_exists(dev, "removable")) { |
209 | qdev_prop_set_bit(dev, "removable", removable); | |
210 | } | |
18846dee MA |
211 | if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) { |
212 | qdev_free(dev); | |
213 | return NULL; | |
214 | } | |
33e66b86 MA |
215 | if (qdev_init(dev) < 0) |
216 | return NULL; | |
b9eea3e6 | 217 | return SCSI_DEVICE(dev); |
d52affa7 GH |
218 | } |
219 | ||
fa66b909 | 220 | int scsi_bus_legacy_handle_cmdline(SCSIBus *bus) |
d52affa7 | 221 | { |
42e766a2 | 222 | Location loc; |
d52affa7 | 223 | DriveInfo *dinfo; |
fa66b909 | 224 | int res = 0, unit; |
d52affa7 | 225 | |
42e766a2 | 226 | loc_push_none(&loc); |
d3d250bd | 227 | for (unit = 0; unit <= bus->info->max_target; unit++) { |
d52affa7 GH |
228 | dinfo = drive_get(IF_SCSI, bus->busnr, unit); |
229 | if (dinfo == NULL) { | |
230 | continue; | |
231 | } | |
42e766a2 | 232 | qemu_opts_loc_restore(dinfo->opts); |
ce4e7e46 | 233 | if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) { |
fa66b909 MA |
234 | res = -1; |
235 | break; | |
236 | } | |
d52affa7 | 237 | } |
42e766a2 | 238 | loc_pop(&loc); |
fa66b909 | 239 | return res; |
d52affa7 | 240 | } |
89b08ae1 | 241 | |
afa46c46 PB |
242 | /* SCSIReqOps implementation for invalid commands. */ |
243 | ||
244 | static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf) | |
245 | { | |
246 | scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); | |
247 | scsi_req_complete(req, CHECK_CONDITION); | |
248 | return 0; | |
249 | } | |
250 | ||
adcf2754 | 251 | static const struct SCSIReqOps reqops_invalid_opcode = { |
afa46c46 PB |
252 | .size = sizeof(SCSIRequest), |
253 | .send_command = scsi_invalid_command | |
254 | }; | |
255 | ||
6dc06f08 PB |
256 | /* SCSIReqOps implementation for unit attention conditions. */ |
257 | ||
258 | static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf) | |
259 | { | |
260 | if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) { | |
261 | scsi_req_build_sense(req, req->dev->unit_attention); | |
262 | } else if (req->bus->unit_attention.key == UNIT_ATTENTION) { | |
263 | scsi_req_build_sense(req, req->bus->unit_attention); | |
264 | } | |
265 | scsi_req_complete(req, CHECK_CONDITION); | |
266 | return 0; | |
267 | } | |
268 | ||
adcf2754 | 269 | static const struct SCSIReqOps reqops_unit_attention = { |
6dc06f08 PB |
270 | .size = sizeof(SCSIRequest), |
271 | .send_command = scsi_unit_attention | |
272 | }; | |
273 | ||
fdaef069 PB |
274 | /* SCSIReqOps implementation for REPORT LUNS and for commands sent to |
275 | an invalid LUN. */ | |
276 | ||
277 | typedef struct SCSITargetReq SCSITargetReq; | |
278 | ||
279 | struct SCSITargetReq { | |
280 | SCSIRequest req; | |
281 | int len; | |
ba74307c | 282 | uint8_t buf[2056]; |
fdaef069 PB |
283 | }; |
284 | ||
285 | static void store_lun(uint8_t *outbuf, int lun) | |
286 | { | |
287 | if (lun < 256) { | |
288 | outbuf[1] = lun; | |
289 | return; | |
290 | } | |
291 | outbuf[1] = (lun & 255); | |
292 | outbuf[0] = (lun >> 8) | 0x40; | |
293 | } | |
294 | ||
295 | static bool scsi_target_emulate_report_luns(SCSITargetReq *r) | |
296 | { | |
ba74307c PB |
297 | DeviceState *qdev; |
298 | int i, len, n; | |
0d3545e7 | 299 | int channel, id; |
ba74307c PB |
300 | bool found_lun0; |
301 | ||
fdaef069 PB |
302 | if (r->req.cmd.xfer < 16) { |
303 | return false; | |
304 | } | |
305 | if (r->req.cmd.buf[2] > 2) { | |
306 | return false; | |
307 | } | |
0d3545e7 | 308 | channel = r->req.dev->channel; |
ba74307c PB |
309 | id = r->req.dev->id; |
310 | found_lun0 = false; | |
311 | n = 0; | |
312 | QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) { | |
b9eea3e6 | 313 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 314 | |
0d3545e7 | 315 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
316 | if (dev->lun == 0) { |
317 | found_lun0 = true; | |
318 | } | |
319 | n += 8; | |
320 | } | |
321 | } | |
322 | if (!found_lun0) { | |
323 | n += 8; | |
324 | } | |
325 | len = MIN(n + 8, r->req.cmd.xfer & ~7); | |
326 | if (len > sizeof(r->buf)) { | |
327 | /* TODO: > 256 LUNs? */ | |
328 | return false; | |
329 | } | |
330 | ||
fdaef069 | 331 | memset(r->buf, 0, len); |
ba74307c PB |
332 | stl_be_p(&r->buf, n); |
333 | i = found_lun0 ? 8 : 16; | |
334 | QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) { | |
b9eea3e6 | 335 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
ba74307c | 336 | |
0d3545e7 | 337 | if (dev->channel == channel && dev->id == id) { |
ba74307c PB |
338 | store_lun(&r->buf[i], dev->lun); |
339 | i += 8; | |
340 | } | |
fdaef069 | 341 | } |
ba74307c PB |
342 | assert(i == n + 8); |
343 | r->len = len; | |
fdaef069 PB |
344 | return true; |
345 | } | |
346 | ||
347 | static bool scsi_target_emulate_inquiry(SCSITargetReq *r) | |
348 | { | |
349 | assert(r->req.dev->lun != r->req.lun); | |
350 | if (r->req.cmd.buf[1] & 0x2) { | |
351 | /* Command support data - optional, not implemented */ | |
352 | return false; | |
353 | } | |
354 | ||
355 | if (r->req.cmd.buf[1] & 0x1) { | |
356 | /* Vital product data */ | |
357 | uint8_t page_code = r->req.cmd.buf[2]; | |
358 | if (r->req.cmd.xfer < 4) { | |
359 | return false; | |
360 | } | |
361 | ||
362 | r->buf[r->len++] = page_code ; /* this page */ | |
363 | r->buf[r->len++] = 0x00; | |
364 | ||
365 | switch (page_code) { | |
366 | case 0x00: /* Supported page codes, mandatory */ | |
367 | { | |
368 | int pages; | |
369 | pages = r->len++; | |
370 | r->buf[r->len++] = 0x00; /* list of supported pages (this page) */ | |
371 | r->buf[pages] = r->len - pages - 1; /* number of pages */ | |
372 | break; | |
373 | } | |
374 | default: | |
375 | return false; | |
376 | } | |
377 | /* done with EVPD */ | |
378 | assert(r->len < sizeof(r->buf)); | |
379 | r->len = MIN(r->req.cmd.xfer, r->len); | |
380 | return true; | |
381 | } | |
382 | ||
383 | /* Standard INQUIRY data */ | |
384 | if (r->req.cmd.buf[2] != 0) { | |
385 | return false; | |
386 | } | |
387 | ||
388 | /* PAGE CODE == 0 */ | |
389 | if (r->req.cmd.xfer < 5) { | |
9fac25bf | 390 | return false; |
fdaef069 PB |
391 | } |
392 | ||
393 | r->len = MIN(r->req.cmd.xfer, 36); | |
394 | memset(r->buf, 0, r->len); | |
395 | if (r->req.lun != 0) { | |
396 | r->buf[0] = TYPE_NO_LUN; | |
397 | } else { | |
398 | r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE; | |
399 | r->buf[2] = 5; /* Version */ | |
400 | r->buf[3] = 2 | 0x10; /* HiSup, response data format */ | |
401 | r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */ | |
afd4030c | 402 | r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */ |
fdaef069 PB |
403 | memcpy(&r->buf[8], "QEMU ", 8); |
404 | memcpy(&r->buf[16], "QEMU TARGET ", 16); | |
405 | strncpy((char *) &r->buf[32], QEMU_VERSION, 4); | |
406 | } | |
407 | return true; | |
408 | } | |
409 | ||
410 | static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf) | |
411 | { | |
412 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
413 | ||
414 | switch (buf[0]) { | |
415 | case REPORT_LUNS: | |
416 | if (!scsi_target_emulate_report_luns(r)) { | |
417 | goto illegal_request; | |
418 | } | |
419 | break; | |
420 | case INQUIRY: | |
421 | if (!scsi_target_emulate_inquiry(r)) { | |
422 | goto illegal_request; | |
423 | } | |
424 | break; | |
739df215 PB |
425 | case REQUEST_SENSE: |
426 | if (req->cmd.xfer < 4) { | |
427 | goto illegal_request; | |
428 | } | |
8b2a04ee PB |
429 | r->len = scsi_device_get_sense(r->req.dev, r->buf, |
430 | MIN(req->cmd.xfer, sizeof r->buf), | |
739df215 | 431 | (req->cmd.buf[1] & 1) == 0); |
3653d8c4 | 432 | if (r->req.dev->sense_is_ua) { |
b9eea3e6 | 433 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
434 | r->req.dev->sense_len = 0; |
435 | r->req.dev->sense_is_ua = false; | |
436 | } | |
739df215 | 437 | break; |
fdaef069 PB |
438 | default: |
439 | scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED)); | |
440 | scsi_req_complete(req, CHECK_CONDITION); | |
441 | return 0; | |
442 | illegal_request: | |
443 | scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | |
444 | scsi_req_complete(req, CHECK_CONDITION); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | if (!r->len) { | |
449 | scsi_req_complete(req, GOOD); | |
450 | } | |
451 | return r->len; | |
452 | } | |
453 | ||
454 | static void scsi_target_read_data(SCSIRequest *req) | |
455 | { | |
456 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
457 | uint32_t n; | |
458 | ||
459 | n = r->len; | |
460 | if (n > 0) { | |
461 | r->len = 0; | |
462 | scsi_req_data(&r->req, n); | |
463 | } else { | |
464 | scsi_req_complete(&r->req, GOOD); | |
465 | } | |
466 | } | |
467 | ||
468 | static uint8_t *scsi_target_get_buf(SCSIRequest *req) | |
469 | { | |
470 | SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | |
471 | ||
472 | return r->buf; | |
473 | } | |
474 | ||
adcf2754 | 475 | static const struct SCSIReqOps reqops_target_command = { |
fdaef069 PB |
476 | .size = sizeof(SCSITargetReq), |
477 | .send_command = scsi_target_send_command, | |
478 | .read_data = scsi_target_read_data, | |
479 | .get_buf = scsi_target_get_buf, | |
480 | }; | |
481 | ||
482 | ||
adcf2754 PB |
483 | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, |
484 | uint32_t tag, uint32_t lun, void *hba_private) | |
89b08ae1 GH |
485 | { |
486 | SCSIRequest *req; | |
487 | ||
7267c094 | 488 | req = g_malloc0(reqops->size); |
5c6c0e51 | 489 | req->refcount = 1; |
89b08ae1 GH |
490 | req->bus = scsi_bus_from_device(d); |
491 | req->dev = d; | |
492 | req->tag = tag; | |
493 | req->lun = lun; | |
c5bf71a9 | 494 | req->hba_private = hba_private; |
ed3a34a3 | 495 | req->status = -1; |
b45ef674 | 496 | req->sense_len = 0; |
8dbd4574 | 497 | req->ops = reqops; |
5138efec | 498 | trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); |
89b08ae1 GH |
499 | return req; |
500 | } | |
501 | ||
c5bf71a9 | 502 | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, |
c39ce112 | 503 | uint8_t *buf, void *hba_private) |
43a2b339 | 504 | { |
6dc06f08 | 505 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); |
c39ce112 | 506 | SCSIRequest *req; |
afa46c46 PB |
507 | SCSICommand cmd; |
508 | ||
509 | if (scsi_req_parse(&cmd, d, buf) != 0) { | |
510 | trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]); | |
511 | req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private); | |
512 | } else { | |
513 | trace_scsi_req_parsed(d->id, lun, tag, buf[0], | |
514 | cmd.mode, cmd.xfer); | |
3b6ffe50 | 515 | if (cmd.lba != -1) { |
afa46c46 PB |
516 | trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0], |
517 | cmd.lba); | |
518 | } | |
fdaef069 | 519 | |
6dc06f08 PB |
520 | if ((d->unit_attention.key == UNIT_ATTENTION || |
521 | bus->unit_attention.key == UNIT_ATTENTION) && | |
522 | (buf[0] != INQUIRY && | |
523 | buf[0] != REPORT_LUNS && | |
524 | buf[0] != GET_CONFIGURATION && | |
3653d8c4 PB |
525 | buf[0] != GET_EVENT_STATUS_NOTIFICATION && |
526 | ||
527 | /* | |
528 | * If we already have a pending unit attention condition, | |
529 | * report this one before triggering another one. | |
530 | */ | |
531 | !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) { | |
6dc06f08 PB |
532 | req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun, |
533 | hba_private); | |
534 | } else if (lun != d->lun || | |
739df215 | 535 | buf[0] == REPORT_LUNS || |
f3b338ef | 536 | (buf[0] == REQUEST_SENSE && (d->sense_len || cmd.xfer < 4))) { |
fdaef069 PB |
537 | req = scsi_req_alloc(&reqops_target_command, d, tag, lun, |
538 | hba_private); | |
539 | } else { | |
b9eea3e6 | 540 | req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); |
fdaef069 | 541 | } |
afa46c46 PB |
542 | } |
543 | ||
544 | req->cmd = cmd; | |
01e95455 PB |
545 | req->resid = req->cmd.xfer; |
546 | ||
98254542 PB |
547 | switch (buf[0]) { |
548 | case INQUIRY: | |
549 | trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]); | |
550 | break; | |
551 | case TEST_UNIT_READY: | |
552 | trace_scsi_test_unit_ready(d->id, lun, tag); | |
553 | break; | |
554 | case REPORT_LUNS: | |
555 | trace_scsi_report_luns(d->id, lun, tag); | |
556 | break; | |
557 | case REQUEST_SENSE: | |
558 | trace_scsi_request_sense(d->id, lun, tag); | |
559 | break; | |
560 | default: | |
561 | break; | |
562 | } | |
563 | ||
c39ce112 | 564 | return req; |
43a2b339 PB |
565 | } |
566 | ||
0c34459b PB |
567 | uint8_t *scsi_req_get_buf(SCSIRequest *req) |
568 | { | |
12010e7b | 569 | return req->ops->get_buf(req); |
0c34459b PB |
570 | } |
571 | ||
6dc06f08 PB |
572 | static void scsi_clear_unit_attention(SCSIRequest *req) |
573 | { | |
574 | SCSISense *ua; | |
575 | if (req->dev->unit_attention.key != UNIT_ATTENTION && | |
576 | req->bus->unit_attention.key != UNIT_ATTENTION) { | |
577 | return; | |
578 | } | |
579 | ||
580 | /* | |
581 | * If an INQUIRY command enters the enabled command state, | |
582 | * the device server shall [not] clear any unit attention condition; | |
583 | * See also MMC-6, paragraphs 6.5 and 6.6.2. | |
584 | */ | |
585 | if (req->cmd.buf[0] == INQUIRY || | |
586 | req->cmd.buf[0] == GET_CONFIGURATION || | |
587 | req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) { | |
588 | return; | |
589 | } | |
590 | ||
591 | if (req->dev->unit_attention.key == UNIT_ATTENTION) { | |
592 | ua = &req->dev->unit_attention; | |
593 | } else { | |
594 | ua = &req->bus->unit_attention; | |
595 | } | |
596 | ||
597 | /* | |
598 | * If a REPORT LUNS command enters the enabled command state, [...] | |
599 | * the device server shall clear any pending unit attention condition | |
600 | * with an additional sense code of REPORTED LUNS DATA HAS CHANGED. | |
601 | */ | |
602 | if (req->cmd.buf[0] == REPORT_LUNS && | |
603 | !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc && | |
604 | ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) { | |
605 | return; | |
606 | } | |
607 | ||
608 | *ua = SENSE_CODE(NO_SENSE); | |
609 | } | |
610 | ||
74382217 HR |
611 | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) |
612 | { | |
6dc06f08 PB |
613 | int ret; |
614 | ||
b45ef674 PB |
615 | assert(len >= 14); |
616 | if (!req->sense_len) { | |
74382217 HR |
617 | return 0; |
618 | } | |
6dc06f08 PB |
619 | |
620 | ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true); | |
621 | ||
622 | /* | |
623 | * FIXME: clearing unit attention conditions upon autosense should be done | |
624 | * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b | |
625 | * (SAM-5, 5.14). | |
626 | * | |
627 | * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and | |
628 | * 10b for HBAs that do not support it (do not call scsi_req_get_sense). | |
3653d8c4 | 629 | * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b. |
6dc06f08 | 630 | */ |
3653d8c4 | 631 | if (req->dev->sense_is_ua) { |
b9eea3e6 | 632 | scsi_device_unit_attention_reported(req->dev); |
3653d8c4 PB |
633 | req->dev->sense_len = 0; |
634 | req->dev->sense_is_ua = false; | |
635 | } | |
6dc06f08 | 636 | return ret; |
b45ef674 PB |
637 | } |
638 | ||
639 | int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed) | |
640 | { | |
641 | return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed); | |
642 | } | |
643 | ||
644 | void scsi_req_build_sense(SCSIRequest *req, SCSISense sense) | |
645 | { | |
646 | trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag, | |
647 | sense.key, sense.asc, sense.ascq); | |
648 | memset(req->sense, 0, 18); | |
649 | req->sense[0] = 0xf0; | |
650 | req->sense[2] = sense.key; | |
eae31cb9 | 651 | req->sense[7] = 10; |
b45ef674 PB |
652 | req->sense[12] = sense.asc; |
653 | req->sense[13] = sense.ascq; | |
654 | req->sense_len = 18; | |
74382217 HR |
655 | } |
656 | ||
63f740dd | 657 | static void scsi_req_enqueue_internal(SCSIRequest *req) |
89b08ae1 | 658 | { |
5c6c0e51 HR |
659 | assert(!req->enqueued); |
660 | scsi_req_ref(req); | |
3d5aba97 PB |
661 | if (req->bus->info->get_sg_list) { |
662 | req->sg = req->bus->info->get_sg_list(req); | |
663 | } else { | |
664 | req->sg = NULL; | |
665 | } | |
5c6c0e51 HR |
666 | req->enqueued = true; |
667 | QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); | |
63f740dd | 668 | } |
fc4f0754 | 669 | |
63f740dd PB |
670 | int32_t scsi_req_enqueue(SCSIRequest *req) |
671 | { | |
672 | int32_t rc; | |
673 | ||
674 | assert(!req->retry); | |
675 | scsi_req_enqueue_internal(req); | |
fc4f0754 | 676 | scsi_req_ref(req); |
c39ce112 | 677 | rc = req->ops->send_command(req, req->cmd.buf); |
fc4f0754 PB |
678 | scsi_req_unref(req); |
679 | return rc; | |
89b08ae1 GH |
680 | } |
681 | ||
a1f0cce2 | 682 | static void scsi_req_dequeue(SCSIRequest *req) |
e8637c90 | 683 | { |
5138efec | 684 | trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); |
71544d30 | 685 | req->retry = false; |
e8637c90 JK |
686 | if (req->enqueued) { |
687 | QTAILQ_REMOVE(&req->dev->requests, req, next); | |
688 | req->enqueued = false; | |
ad2d30f7 | 689 | scsi_req_unref(req); |
e8637c90 JK |
690 | } |
691 | } | |
692 | ||
06b86357 PB |
693 | static int scsi_get_performance_length(int num_desc, int type, int data_type) |
694 | { | |
695 | /* MMC-6, paragraph 6.7. */ | |
696 | switch (type) { | |
697 | case 0: | |
698 | if ((data_type & 3) == 0) { | |
699 | /* Each descriptor is as in Table 295 - Nominal performance. */ | |
700 | return 16 * num_desc + 8; | |
701 | } else { | |
702 | /* Each descriptor is as in Table 296 - Exceptions. */ | |
703 | return 6 * num_desc + 8; | |
704 | } | |
705 | case 1: | |
706 | case 4: | |
707 | case 5: | |
708 | return 8 * num_desc + 8; | |
709 | case 2: | |
710 | return 2048 * num_desc + 8; | |
711 | case 3: | |
712 | return 16 * num_desc + 8; | |
713 | default: | |
714 | return 8; | |
715 | } | |
716 | } | |
717 | ||
2599aece | 718 | static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
2ec749cb | 719 | { |
2599aece | 720 | switch (buf[0] >> 5) { |
2ec749cb | 721 | case 0: |
2599aece PB |
722 | cmd->xfer = buf[4]; |
723 | cmd->len = 6; | |
2ec749cb | 724 | /* length 0 means 256 blocks */ |
2599aece PB |
725 | if (cmd->xfer == 0) { |
726 | cmd->xfer = 256; | |
727 | } | |
2ec749cb GH |
728 | break; |
729 | case 1: | |
730 | case 2: | |
bd5da232 | 731 | cmd->xfer = lduw_be_p(&buf[7]); |
2599aece | 732 | cmd->len = 10; |
2ec749cb GH |
733 | break; |
734 | case 4: | |
06b86357 | 735 | cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL; |
2599aece | 736 | cmd->len = 16; |
2ec749cb GH |
737 | break; |
738 | case 5: | |
06b86357 | 739 | cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL; |
2599aece | 740 | cmd->len = 12; |
2ec749cb GH |
741 | break; |
742 | default: | |
743 | return -1; | |
744 | } | |
745 | ||
2599aece | 746 | switch (buf[0]) { |
2ec749cb | 747 | case TEST_UNIT_READY: |
5e30a07d | 748 | case REWIND: |
2ec749cb | 749 | case START_STOP: |
00a01ad4 | 750 | case SET_CAPACITY: |
2ec749cb | 751 | case WRITE_FILEMARKS: |
06b86357 | 752 | case WRITE_FILEMARKS_16: |
2ec749cb | 753 | case SPACE: |
a5e3d9ef BK |
754 | case RESERVE: |
755 | case RELEASE: | |
2ec749cb GH |
756 | case ERASE: |
757 | case ALLOW_MEDIUM_REMOVAL: | |
5e30a07d | 758 | case VERIFY_10: |
2ec749cb GH |
759 | case SEEK_10: |
760 | case SYNCHRONIZE_CACHE: | |
06b86357 PB |
761 | case SYNCHRONIZE_CACHE_16: |
762 | case LOCATE_16: | |
2ec749cb GH |
763 | case LOCK_UNLOCK_CACHE: |
764 | case LOAD_UNLOAD: | |
765 | case SET_CD_SPEED: | |
766 | case SET_LIMITS: | |
5e30a07d | 767 | case WRITE_LONG_10: |
2ec749cb GH |
768 | case MOVE_MEDIUM: |
769 | case UPDATE_BLOCK: | |
06b86357 PB |
770 | case RESERVE_TRACK: |
771 | case SET_READ_AHEAD: | |
772 | case PRE_FETCH: | |
773 | case PRE_FETCH_16: | |
774 | case ALLOW_OVERWRITE: | |
2599aece | 775 | cmd->xfer = 0; |
2ec749cb GH |
776 | break; |
777 | case MODE_SENSE: | |
778 | break; | |
5e30a07d | 779 | case WRITE_SAME_10: |
2599aece | 780 | cmd->xfer = 1; |
2ec749cb | 781 | break; |
5e30a07d | 782 | case READ_CAPACITY_10: |
2599aece | 783 | cmd->xfer = 8; |
2ec749cb GH |
784 | break; |
785 | case READ_BLOCK_LIMITS: | |
2599aece | 786 | cmd->xfer = 6; |
2ec749cb | 787 | break; |
2ec749cb | 788 | case SEND_VOLUME_TAG: |
06b86357 PB |
789 | /* GPCMD_SET_STREAMING from multimedia commands. */ |
790 | if (dev->type == TYPE_ROM) { | |
791 | cmd->xfer = buf[10] | (buf[9] << 8); | |
792 | } else { | |
793 | cmd->xfer = buf[9] | (buf[8] << 8); | |
794 | } | |
2ec749cb GH |
795 | break; |
796 | case WRITE_10: | |
5e30a07d | 797 | case WRITE_VERIFY_10: |
2ec749cb GH |
798 | case WRITE_6: |
799 | case WRITE_12: | |
800 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
801 | case WRITE_16: |
802 | case WRITE_VERIFY_16: | |
2599aece | 803 | cmd->xfer *= dev->blocksize; |
2ec749cb GH |
804 | break; |
805 | case READ_10: | |
806 | case READ_6: | |
807 | case READ_REVERSE: | |
808 | case RECOVER_BUFFERED_DATA: | |
809 | case READ_12: | |
bd536cf3 | 810 | case READ_16: |
2599aece | 811 | cmd->xfer *= dev->blocksize; |
2ec749cb | 812 | break; |
06b86357 PB |
813 | case FORMAT_UNIT: |
814 | /* MMC mandates the parameter list to be 12-bytes long. Parameters | |
815 | * for block devices are restricted to the header right now. */ | |
816 | if (dev->type == TYPE_ROM && (buf[1] & 16)) { | |
817 | cmd->xfer = 12; | |
818 | } else { | |
819 | cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4); | |
820 | } | |
821 | break; | |
2ec749cb | 822 | case INQUIRY: |
06b86357 PB |
823 | case RECEIVE_DIAGNOSTIC: |
824 | case SEND_DIAGNOSTIC: | |
2599aece | 825 | cmd->xfer = buf[4] | (buf[3] << 8); |
2ec749cb | 826 | break; |
06b86357 PB |
827 | case READ_CD: |
828 | case READ_BUFFER: | |
829 | case WRITE_BUFFER: | |
830 | case SEND_CUE_SHEET: | |
831 | cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | |
832 | break; | |
833 | case PERSISTENT_RESERVE_OUT: | |
834 | cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL; | |
835 | break; | |
836 | case ERASE_12: | |
837 | if (dev->type == TYPE_ROM) { | |
838 | /* MMC command GET PERFORMANCE. */ | |
839 | cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8), | |
840 | buf[10], buf[1] & 0x1f); | |
841 | } | |
842 | break; | |
843 | case MECHANISM_STATUS: | |
844 | case READ_DVD_STRUCTURE: | |
845 | case SEND_DVD_STRUCTURE: | |
c7126d5b NB |
846 | case MAINTENANCE_OUT: |
847 | case MAINTENANCE_IN: | |
2599aece | 848 | if (dev->type == TYPE_ROM) { |
c7126d5b | 849 | /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */ |
2599aece | 850 | cmd->xfer = buf[9] | (buf[8] << 8); |
c7126d5b NB |
851 | } |
852 | break; | |
2ec749cb GH |
853 | } |
854 | return 0; | |
855 | } | |
856 | ||
2599aece | 857 | static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
2ec749cb | 858 | { |
2599aece | 859 | switch (buf[0]) { |
2ec749cb | 860 | /* stream commands */ |
06b86357 PB |
861 | case ERASE_12: |
862 | case ERASE_16: | |
863 | cmd->xfer = 0; | |
864 | break; | |
2ec749cb GH |
865 | case READ_6: |
866 | case READ_REVERSE: | |
867 | case RECOVER_BUFFERED_DATA: | |
868 | case WRITE_6: | |
2599aece PB |
869 | cmd->len = 6; |
870 | cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); | |
871 | if (buf[1] & 0x01) { /* fixed */ | |
872 | cmd->xfer *= dev->blocksize; | |
873 | } | |
2ec749cb GH |
874 | break; |
875 | case REWIND: | |
876 | case START_STOP: | |
2599aece PB |
877 | cmd->len = 6; |
878 | cmd->xfer = 0; | |
2ec749cb | 879 | break; |
06b86357 PB |
880 | case SPACE_16: |
881 | cmd->xfer = buf[13] | (buf[12] << 8); | |
882 | break; | |
883 | case READ_POSITION: | |
884 | cmd->xfer = buf[8] | (buf[7] << 8); | |
885 | break; | |
886 | case FORMAT_UNIT: | |
887 | cmd->xfer = buf[4] | (buf[3] << 8); | |
888 | break; | |
2ec749cb GH |
889 | /* generic commands */ |
890 | default: | |
2599aece | 891 | return scsi_req_length(cmd, dev, buf); |
2ec749cb GH |
892 | } |
893 | return 0; | |
894 | } | |
895 | ||
2599aece | 896 | static void scsi_cmd_xfer_mode(SCSICommand *cmd) |
97a06435 | 897 | { |
2599aece | 898 | switch (cmd->buf[0]) { |
97a06435 GH |
899 | case WRITE_6: |
900 | case WRITE_10: | |
5e30a07d | 901 | case WRITE_VERIFY_10: |
97a06435 GH |
902 | case WRITE_12: |
903 | case WRITE_VERIFY_12: | |
bd536cf3 GH |
904 | case WRITE_16: |
905 | case WRITE_VERIFY_16: | |
97a06435 GH |
906 | case COPY: |
907 | case COPY_VERIFY: | |
908 | case COMPARE: | |
909 | case CHANGE_DEFINITION: | |
910 | case LOG_SELECT: | |
911 | case MODE_SELECT: | |
912 | case MODE_SELECT_10: | |
913 | case SEND_DIAGNOSTIC: | |
914 | case WRITE_BUFFER: | |
915 | case FORMAT_UNIT: | |
916 | case REASSIGN_BLOCKS: | |
97a06435 GH |
917 | case SEARCH_EQUAL: |
918 | case SEARCH_HIGH: | |
919 | case SEARCH_LOW: | |
920 | case UPDATE_BLOCK: | |
5e30a07d HR |
921 | case WRITE_LONG_10: |
922 | case WRITE_SAME_10: | |
97a06435 GH |
923 | case SEARCH_HIGH_12: |
924 | case SEARCH_EQUAL_12: | |
925 | case SEARCH_LOW_12: | |
97a06435 GH |
926 | case MEDIUM_SCAN: |
927 | case SEND_VOLUME_TAG: | |
06b86357 PB |
928 | case SEND_CUE_SHEET: |
929 | case SEND_DVD_STRUCTURE: | |
01bedeba | 930 | case PERSISTENT_RESERVE_OUT: |
c7126d5b | 931 | case MAINTENANCE_OUT: |
2599aece | 932 | cmd->mode = SCSI_XFER_TO_DEV; |
97a06435 GH |
933 | break; |
934 | default: | |
2599aece PB |
935 | if (cmd->xfer) |
936 | cmd->mode = SCSI_XFER_FROM_DEV; | |
97a06435 | 937 | else { |
2599aece | 938 | cmd->mode = SCSI_XFER_NONE; |
97a06435 GH |
939 | } |
940 | break; | |
941 | } | |
942 | } | |
943 | ||
2599aece | 944 | static uint64_t scsi_cmd_lba(SCSICommand *cmd) |
2ec749cb | 945 | { |
2599aece | 946 | uint8_t *buf = cmd->buf; |
2ec749cb GH |
947 | uint64_t lba; |
948 | ||
949 | switch (buf[0] >> 5) { | |
950 | case 0: | |
bd5da232 | 951 | lba = ldl_be_p(&buf[0]) & 0x1fffff; |
2ec749cb GH |
952 | break; |
953 | case 1: | |
954 | case 2: | |
bd5da232 | 955 | case 5: |
06b86357 | 956 | lba = ldl_be_p(&buf[2]) & 0xffffffffULL; |
2ec749cb GH |
957 | break; |
958 | case 4: | |
bd5da232 | 959 | lba = ldq_be_p(&buf[2]); |
2ec749cb GH |
960 | break; |
961 | default: | |
962 | lba = -1; | |
963 | ||
964 | } | |
965 | return lba; | |
966 | } | |
967 | ||
afa46c46 | 968 | int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) |
2ec749cb GH |
969 | { |
970 | int rc; | |
971 | ||
afa46c46 PB |
972 | if (dev->type == TYPE_TAPE) { |
973 | rc = scsi_req_stream_length(cmd, dev, buf); | |
2ec749cb | 974 | } else { |
afa46c46 | 975 | rc = scsi_req_length(cmd, dev, buf); |
2ec749cb GH |
976 | } |
977 | if (rc != 0) | |
978 | return rc; | |
979 | ||
afa46c46 PB |
980 | memcpy(cmd->buf, buf, cmd->len); |
981 | scsi_cmd_xfer_mode(cmd); | |
982 | cmd->lba = scsi_cmd_lba(cmd); | |
2ec749cb GH |
983 | return 0; |
984 | } | |
ed3a34a3 | 985 | |
a1f0cce2 HR |
986 | /* |
987 | * Predefined sense codes | |
988 | */ | |
989 | ||
990 | /* No sense data available */ | |
991 | const struct SCSISense sense_code_NO_SENSE = { | |
992 | .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00 | |
993 | }; | |
994 | ||
995 | /* LUN not ready, Manual intervention required */ | |
996 | const struct SCSISense sense_code_LUN_NOT_READY = { | |
997 | .key = NOT_READY, .asc = 0x04, .ascq = 0x03 | |
998 | }; | |
999 | ||
1000 | /* LUN not ready, Medium not present */ | |
1001 | const struct SCSISense sense_code_NO_MEDIUM = { | |
1002 | .key = NOT_READY, .asc = 0x3a, .ascq = 0x00 | |
1003 | }; | |
1004 | ||
68bb01f3 MA |
1005 | /* LUN not ready, medium removal prevented */ |
1006 | const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = { | |
1007 | .key = NOT_READY, .asc = 0x53, .ascq = 0x00 | |
1008 | }; | |
1009 | ||
a1f0cce2 HR |
1010 | /* Hardware error, internal target failure */ |
1011 | const struct SCSISense sense_code_TARGET_FAILURE = { | |
1012 | .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00 | |
1013 | }; | |
1014 | ||
1015 | /* Illegal request, invalid command operation code */ | |
1016 | const struct SCSISense sense_code_INVALID_OPCODE = { | |
1017 | .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00 | |
1018 | }; | |
1019 | ||
1020 | /* Illegal request, LBA out of range */ | |
1021 | const struct SCSISense sense_code_LBA_OUT_OF_RANGE = { | |
1022 | .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00 | |
1023 | }; | |
1024 | ||
1025 | /* Illegal request, Invalid field in CDB */ | |
1026 | const struct SCSISense sense_code_INVALID_FIELD = { | |
1027 | .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00 | |
1028 | }; | |
1029 | ||
1030 | /* Illegal request, LUN not supported */ | |
1031 | const struct SCSISense sense_code_LUN_NOT_SUPPORTED = { | |
1032 | .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00 | |
1033 | }; | |
1034 | ||
a872a304 PB |
1035 | /* Illegal request, Saving parameters not supported */ |
1036 | const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = { | |
1037 | .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00 | |
1038 | }; | |
1039 | ||
1040 | /* Illegal request, Incompatible medium installed */ | |
67cc61e4 | 1041 | const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = { |
a872a304 PB |
1042 | .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00 |
1043 | }; | |
1044 | ||
68bb01f3 MA |
1045 | /* Illegal request, medium removal prevented */ |
1046 | const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = { | |
1047 | .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00 | |
1048 | }; | |
1049 | ||
a1f0cce2 HR |
1050 | /* Command aborted, I/O process terminated */ |
1051 | const struct SCSISense sense_code_IO_ERROR = { | |
1052 | .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06 | |
1053 | }; | |
1054 | ||
1055 | /* Command aborted, I_T Nexus loss occurred */ | |
1056 | const struct SCSISense sense_code_I_T_NEXUS_LOSS = { | |
1057 | .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07 | |
1058 | }; | |
1059 | ||
1060 | /* Command aborted, Logical Unit failure */ | |
1061 | const struct SCSISense sense_code_LUN_FAILURE = { | |
1062 | .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01 | |
1063 | }; | |
1064 | ||
a872a304 PB |
1065 | /* Unit attention, Power on, reset or bus device reset occurred */ |
1066 | const struct SCSISense sense_code_RESET = { | |
1067 | .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00 | |
1068 | }; | |
1069 | ||
8a9c16f6 PB |
1070 | /* Unit attention, No medium */ |
1071 | const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = { | |
1072 | .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00 | |
1073 | }; | |
1074 | ||
a872a304 PB |
1075 | /* Unit attention, Medium may have changed */ |
1076 | const struct SCSISense sense_code_MEDIUM_CHANGED = { | |
1077 | .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00 | |
1078 | }; | |
1079 | ||
1080 | /* Unit attention, Reported LUNs data has changed */ | |
1081 | const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = { | |
1082 | .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e | |
1083 | }; | |
1084 | ||
1085 | /* Unit attention, Device internal reset */ | |
1086 | const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = { | |
1087 | .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04 | |
1088 | }; | |
1089 | ||
a1f0cce2 HR |
1090 | /* |
1091 | * scsi_build_sense | |
1092 | * | |
b45ef674 | 1093 | * Convert between fixed and descriptor sense buffers |
a1f0cce2 | 1094 | */ |
b45ef674 PB |
1095 | int scsi_build_sense(uint8_t *in_buf, int in_len, |
1096 | uint8_t *buf, int len, bool fixed) | |
a1f0cce2 | 1097 | { |
b45ef674 PB |
1098 | bool fixed_in; |
1099 | SCSISense sense; | |
a1f0cce2 HR |
1100 | if (!fixed && len < 8) { |
1101 | return 0; | |
1102 | } | |
1103 | ||
b45ef674 PB |
1104 | if (in_len == 0) { |
1105 | sense.key = NO_SENSE; | |
1106 | sense.asc = 0; | |
1107 | sense.ascq = 0; | |
1108 | } else { | |
1109 | fixed_in = (in_buf[0] & 2) == 0; | |
1110 | ||
1111 | if (fixed == fixed_in) { | |
1112 | memcpy(buf, in_buf, MIN(len, in_len)); | |
1113 | return MIN(len, in_len); | |
1114 | } | |
1115 | ||
1116 | if (fixed_in) { | |
1117 | sense.key = in_buf[2]; | |
1118 | sense.asc = in_buf[12]; | |
1119 | sense.ascq = in_buf[13]; | |
1120 | } else { | |
1121 | sense.key = in_buf[1]; | |
1122 | sense.asc = in_buf[2]; | |
1123 | sense.ascq = in_buf[3]; | |
1124 | } | |
1125 | } | |
1126 | ||
a1f0cce2 HR |
1127 | memset(buf, 0, len); |
1128 | if (fixed) { | |
1129 | /* Return fixed format sense buffer */ | |
1130 | buf[0] = 0xf0; | |
1131 | buf[2] = sense.key; | |
eae31cb9 | 1132 | buf[7] = 10; |
a1f0cce2 HR |
1133 | buf[12] = sense.asc; |
1134 | buf[13] = sense.ascq; | |
1135 | return MIN(len, 18); | |
1136 | } else { | |
1137 | /* Return descriptor format sense buffer */ | |
1138 | buf[0] = 0x72; | |
1139 | buf[1] = sense.key; | |
1140 | buf[2] = sense.asc; | |
1141 | buf[3] = sense.ascq; | |
1142 | return 8; | |
1143 | } | |
1144 | } | |
1145 | ||
ec766865 GH |
1146 | static const char *scsi_command_name(uint8_t cmd) |
1147 | { | |
1148 | static const char *names[] = { | |
1149 | [ TEST_UNIT_READY ] = "TEST_UNIT_READY", | |
5e30a07d | 1150 | [ REWIND ] = "REWIND", |
ec766865 GH |
1151 | [ REQUEST_SENSE ] = "REQUEST_SENSE", |
1152 | [ FORMAT_UNIT ] = "FORMAT_UNIT", | |
1153 | [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS", | |
1154 | [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS", | |
1155 | [ READ_6 ] = "READ_6", | |
1156 | [ WRITE_6 ] = "WRITE_6", | |
00a01ad4 | 1157 | [ SET_CAPACITY ] = "SET_CAPACITY", |
ec766865 GH |
1158 | [ READ_REVERSE ] = "READ_REVERSE", |
1159 | [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS", | |
1160 | [ SPACE ] = "SPACE", | |
1161 | [ INQUIRY ] = "INQUIRY", | |
1162 | [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA", | |
c7126d5b NB |
1163 | [ MAINTENANCE_IN ] = "MAINTENANCE_IN", |
1164 | [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT", | |
ec766865 GH |
1165 | [ MODE_SELECT ] = "MODE_SELECT", |
1166 | [ RESERVE ] = "RESERVE", | |
1167 | [ RELEASE ] = "RELEASE", | |
1168 | [ COPY ] = "COPY", | |
1169 | [ ERASE ] = "ERASE", | |
1170 | [ MODE_SENSE ] = "MODE_SENSE", | |
1171 | [ START_STOP ] = "START_STOP", | |
1172 | [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC", | |
1173 | [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC", | |
1174 | [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL", | |
5e30a07d | 1175 | [ READ_CAPACITY_10 ] = "READ_CAPACITY_10", |
ec766865 GH |
1176 | [ READ_10 ] = "READ_10", |
1177 | [ WRITE_10 ] = "WRITE_10", | |
1178 | [ SEEK_10 ] = "SEEK_10", | |
5e30a07d HR |
1179 | [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10", |
1180 | [ VERIFY_10 ] = "VERIFY_10", | |
ec766865 GH |
1181 | [ SEARCH_HIGH ] = "SEARCH_HIGH", |
1182 | [ SEARCH_EQUAL ] = "SEARCH_EQUAL", | |
1183 | [ SEARCH_LOW ] = "SEARCH_LOW", | |
1184 | [ SET_LIMITS ] = "SET_LIMITS", | |
00a01ad4 | 1185 | [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION", |
545557d4 | 1186 | /* READ_POSITION and PRE_FETCH use the same operation code */ |
ec766865 GH |
1187 | [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE", |
1188 | [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE", | |
1189 | [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA", | |
1190 | [ MEDIUM_SCAN ] = "MEDIUM_SCAN", | |
1191 | [ COMPARE ] = "COMPARE", | |
1192 | [ COPY_VERIFY ] = "COPY_VERIFY", | |
1193 | [ WRITE_BUFFER ] = "WRITE_BUFFER", | |
1194 | [ READ_BUFFER ] = "READ_BUFFER", | |
1195 | [ UPDATE_BLOCK ] = "UPDATE_BLOCK", | |
5e30a07d HR |
1196 | [ READ_LONG_10 ] = "READ_LONG_10", |
1197 | [ WRITE_LONG_10 ] = "WRITE_LONG_10", | |
ec766865 | 1198 | [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION", |
5e30a07d HR |
1199 | [ WRITE_SAME_10 ] = "WRITE_SAME_10", |
1200 | [ UNMAP ] = "UNMAP", | |
ec766865 | 1201 | [ READ_TOC ] = "READ_TOC", |
5e30a07d HR |
1202 | [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT", |
1203 | [ GET_CONFIGURATION ] = "GET_CONFIGURATION", | |
ec766865 GH |
1204 | [ LOG_SELECT ] = "LOG_SELECT", |
1205 | [ LOG_SENSE ] = "LOG_SENSE", | |
1206 | [ MODE_SELECT_10 ] = "MODE_SELECT_10", | |
1207 | [ RESERVE_10 ] = "RESERVE_10", | |
1208 | [ RELEASE_10 ] = "RELEASE_10", | |
1209 | [ MODE_SENSE_10 ] = "MODE_SENSE_10", | |
1210 | [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN", | |
1211 | [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT", | |
5e30a07d HR |
1212 | [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16", |
1213 | [ EXTENDED_COPY ] = "EXTENDED_COPY", | |
1214 | [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH", | |
1215 | [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN", | |
1216 | [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT", | |
1217 | [ READ_16 ] = "READ_16", | |
1218 | [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE", | |
1219 | [ WRITE_16 ] = "WRITE_16", | |
1220 | [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16", | |
1221 | [ VERIFY_16 ] = "VERIFY_16", | |
00a01ad4 PB |
1222 | [ PRE_FETCH_16 ] = "PRE_FETCH_16", |
1223 | [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16", | |
1224 | /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */ | |
5e30a07d | 1225 | [ LOCATE_16 ] = "LOCATE_16", |
00a01ad4 | 1226 | [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16", |
48bb9f53 | 1227 | /* ERASE_16 and WRITE_SAME_16 use the same operation code */ |
f6515262 | 1228 | [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16", |
5e30a07d HR |
1229 | [ WRITE_LONG_16 ] = "WRITE_LONG_16", |
1230 | [ REPORT_LUNS ] = "REPORT_LUNS", | |
1231 | [ BLANK ] = "BLANK", | |
ec766865 | 1232 | [ MOVE_MEDIUM ] = "MOVE_MEDIUM", |
5e30a07d | 1233 | [ LOAD_UNLOAD ] = "LOAD_UNLOAD", |
ec766865 GH |
1234 | [ READ_12 ] = "READ_12", |
1235 | [ WRITE_12 ] = "WRITE_12", | |
00a01ad4 PB |
1236 | [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE", |
1237 | /* ERASE_12 and GET_PERFORMANCE use the same operation code */ | |
f6515262 | 1238 | [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12", |
ec766865 | 1239 | [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12", |
5e30a07d | 1240 | [ VERIFY_12 ] = "VERIFY_12", |
ec766865 GH |
1241 | [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12", |
1242 | [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12", | |
1243 | [ SEARCH_LOW_12 ] = "SEARCH_LOW_12", | |
1244 | [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS", | |
00a01ad4 PB |
1245 | [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING", |
1246 | /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */ | |
1247 | [ READ_CD ] = "READ_CD", | |
5e30a07d | 1248 | [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12", |
00a01ad4 PB |
1249 | [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE", |
1250 | [ RESERVE_TRACK ] = "RESERVE_TRACK", | |
1251 | [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET", | |
1252 | [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE", | |
ec766865 | 1253 | [ SET_CD_SPEED ] = "SET_CD_SPEED", |
00a01ad4 PB |
1254 | [ SET_READ_AHEAD ] = "SET_READ_AHEAD", |
1255 | [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE", | |
1256 | [ MECHANISM_STATUS ] = "MECHANISM_STATUS", | |
ec766865 GH |
1257 | }; |
1258 | ||
1259 | if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL) | |
1260 | return "*UNKNOWN*"; | |
1261 | return names[cmd]; | |
1262 | } | |
1263 | ||
ad2d30f7 PB |
1264 | SCSIRequest *scsi_req_ref(SCSIRequest *req) |
1265 | { | |
1266 | req->refcount++; | |
1267 | return req; | |
1268 | } | |
1269 | ||
1270 | void scsi_req_unref(SCSIRequest *req) | |
1271 | { | |
1272 | if (--req->refcount == 0) { | |
12010e7b PB |
1273 | if (req->ops->free_req) { |
1274 | req->ops->free_req(req); | |
ad2d30f7 | 1275 | } |
7267c094 | 1276 | g_free(req); |
ad2d30f7 PB |
1277 | } |
1278 | } | |
1279 | ||
ad3376cc PB |
1280 | /* Tell the device that we finished processing this chunk of I/O. It |
1281 | will start the next chunk or complete the command. */ | |
1282 | void scsi_req_continue(SCSIRequest *req) | |
1283 | { | |
1284 | trace_scsi_req_continue(req->dev->id, req->lun, req->tag); | |
1285 | if (req->cmd.mode == SCSI_XFER_TO_DEV) { | |
12010e7b | 1286 | req->ops->write_data(req); |
ad3376cc | 1287 | } else { |
12010e7b | 1288 | req->ops->read_data(req); |
ad3376cc PB |
1289 | } |
1290 | } | |
1291 | ||
ab9adc88 PB |
1292 | /* Called by the devices when data is ready for the HBA. The HBA should |
1293 | start a DMA operation to read or fill the device's data buffer. | |
ad3376cc | 1294 | Once it completes, calling scsi_req_continue will restart I/O. */ |
ab9adc88 PB |
1295 | void scsi_req_data(SCSIRequest *req, int len) |
1296 | { | |
3d5aba97 | 1297 | uint8_t *buf; |
e88c591d PB |
1298 | if (req->io_canceled) { |
1299 | trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); | |
01e95455 | 1300 | return; |
e88c591d | 1301 | } |
01e95455 PB |
1302 | trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); |
1303 | assert(req->cmd.mode != SCSI_XFER_NONE); | |
3d5aba97 PB |
1304 | if (!req->sg) { |
1305 | req->resid -= len; | |
1306 | req->bus->info->transfer_data(req, len); | |
1307 | return; | |
1308 | } | |
1309 | ||
1310 | /* If the device calls scsi_req_data and the HBA specified a | |
1311 | * scatter/gather list, the transfer has to happen in a single | |
1312 | * step. */ | |
1313 | assert(!req->dma_started); | |
1314 | req->dma_started = true; | |
1315 | ||
1316 | buf = scsi_req_get_buf(req); | |
1317 | if (req->cmd.mode == SCSI_XFER_FROM_DEV) { | |
1318 | req->resid = dma_buf_read(buf, len, req->sg); | |
1319 | } else { | |
1320 | req->resid = dma_buf_write(buf, len, req->sg); | |
1321 | } | |
1322 | scsi_req_continue(req); | |
ab9adc88 PB |
1323 | } |
1324 | ||
ec766865 GH |
1325 | void scsi_req_print(SCSIRequest *req) |
1326 | { | |
1327 | FILE *fp = stderr; | |
1328 | int i; | |
1329 | ||
1330 | fprintf(fp, "[%s id=%d] %s", | |
1331 | req->dev->qdev.parent_bus->name, | |
1332 | req->dev->id, | |
1333 | scsi_command_name(req->cmd.buf[0])); | |
1334 | for (i = 1; i < req->cmd.len; i++) { | |
1335 | fprintf(fp, " 0x%02x", req->cmd.buf[i]); | |
1336 | } | |
1337 | switch (req->cmd.mode) { | |
1338 | case SCSI_XFER_NONE: | |
1339 | fprintf(fp, " - none\n"); | |
1340 | break; | |
1341 | case SCSI_XFER_FROM_DEV: | |
1342 | fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer); | |
1343 | break; | |
1344 | case SCSI_XFER_TO_DEV: | |
1345 | fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer); | |
1346 | break; | |
1347 | default: | |
1348 | fprintf(fp, " - Oops\n"); | |
1349 | break; | |
1350 | } | |
1351 | } | |
1352 | ||
682a9b21 | 1353 | void scsi_req_complete(SCSIRequest *req, int status) |
ed3a34a3 | 1354 | { |
682a9b21 PB |
1355 | assert(req->status == -1); |
1356 | req->status = status; | |
b45ef674 PB |
1357 | |
1358 | assert(req->sense_len < sizeof(req->sense)); | |
1359 | if (status == GOOD) { | |
1360 | req->sense_len = 0; | |
1361 | } | |
1362 | ||
1363 | if (req->sense_len) { | |
1364 | memcpy(req->dev->sense, req->sense, req->sense_len); | |
3653d8c4 PB |
1365 | req->dev->sense_len = req->sense_len; |
1366 | req->dev->sense_is_ua = (req->ops == &reqops_unit_attention); | |
1367 | } else { | |
1368 | req->dev->sense_len = 0; | |
1369 | req->dev->sense_is_ua = false; | |
b45ef674 | 1370 | } |
b45ef674 | 1371 | |
6dc06f08 PB |
1372 | /* |
1373 | * Unit attention state is now stored in the device's sense buffer | |
1374 | * if the HBA didn't do autosense. Clear the pending unit attention | |
1375 | * flags. | |
1376 | */ | |
1377 | scsi_clear_unit_attention(req); | |
1378 | ||
ad2d30f7 | 1379 | scsi_req_ref(req); |
e8637c90 | 1380 | scsi_req_dequeue(req); |
01e95455 | 1381 | req->bus->info->complete(req, req->status, req->resid); |
ad2d30f7 | 1382 | scsi_req_unref(req); |
ed3a34a3 | 1383 | } |
db07c0f8 | 1384 | |
94d3f98a PB |
1385 | void scsi_req_cancel(SCSIRequest *req) |
1386 | { | |
e88c591d PB |
1387 | if (!req->enqueued) { |
1388 | return; | |
94d3f98a PB |
1389 | } |
1390 | scsi_req_ref(req); | |
1391 | scsi_req_dequeue(req); | |
e88c591d PB |
1392 | req->io_canceled = true; |
1393 | if (req->ops->cancel_io) { | |
1394 | req->ops->cancel_io(req); | |
1395 | } | |
afd4030c PB |
1396 | if (req->bus->info->cancel) { |
1397 | req->bus->info->cancel(req); | |
94d3f98a PB |
1398 | } |
1399 | scsi_req_unref(req); | |
1400 | } | |
1401 | ||
19d110ab PB |
1402 | void scsi_req_abort(SCSIRequest *req, int status) |
1403 | { | |
e88c591d PB |
1404 | if (!req->enqueued) { |
1405 | return; | |
1406 | } | |
1407 | scsi_req_ref(req); | |
1408 | scsi_req_dequeue(req); | |
1409 | req->io_canceled = true; | |
12010e7b PB |
1410 | if (req->ops->cancel_io) { |
1411 | req->ops->cancel_io(req); | |
19d110ab | 1412 | } |
682a9b21 | 1413 | scsi_req_complete(req, status); |
e88c591d | 1414 | scsi_req_unref(req); |
19d110ab PB |
1415 | } |
1416 | ||
c7b48872 | 1417 | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) |
c557e889 PB |
1418 | { |
1419 | SCSIRequest *req; | |
1420 | ||
1421 | while (!QTAILQ_EMPTY(&sdev->requests)) { | |
1422 | req = QTAILQ_FIRST(&sdev->requests); | |
94d3f98a | 1423 | scsi_req_cancel(req); |
c557e889 | 1424 | } |
c7b48872 | 1425 | sdev->unit_attention = sense; |
c557e889 PB |
1426 | } |
1427 | ||
baa1bd89 PB |
1428 | static char *scsibus_get_dev_path(DeviceState *dev) |
1429 | { | |
1430 | SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev); | |
1431 | DeviceState *hba = dev->parent_bus->parent; | |
1432 | char *id = NULL; | |
b7c8c35f | 1433 | char *path; |
baa1bd89 PB |
1434 | |
1435 | if (hba && hba->parent_bus && hba->parent_bus->info->get_dev_path) { | |
1436 | id = hba->parent_bus->info->get_dev_path(hba); | |
1437 | } | |
1438 | if (id) { | |
b7c8c35f | 1439 | path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun); |
baa1bd89 | 1440 | } else { |
b7c8c35f | 1441 | path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun); |
baa1bd89 | 1442 | } |
b7c8c35f PB |
1443 | g_free(id); |
1444 | return path; | |
baa1bd89 PB |
1445 | } |
1446 | ||
db07c0f8 GN |
1447 | static char *scsibus_get_fw_dev_path(DeviceState *dev) |
1448 | { | |
b9eea3e6 | 1449 | SCSIDevice *d = SCSI_DEVICE(dev); |
db07c0f8 | 1450 | char path[100]; |
db07c0f8 | 1451 | |
795928f6 PB |
1452 | snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel, |
1453 | qdev_fw_name(dev), d->id, d->lun); | |
db07c0f8 | 1454 | |
f48a7a6e PB |
1455 | return strdup(path); |
1456 | } | |
1457 | ||
0d3545e7 | 1458 | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) |
f48a7a6e PB |
1459 | { |
1460 | DeviceState *qdev; | |
1461 | SCSIDevice *target_dev = NULL; | |
db07c0f8 | 1462 | |
f48a7a6e | 1463 | QTAILQ_FOREACH_REVERSE(qdev, &bus->qbus.children, ChildrenHead, sibling) { |
b9eea3e6 | 1464 | SCSIDevice *dev = SCSI_DEVICE(qdev); |
db07c0f8 | 1465 | |
0d3545e7 | 1466 | if (dev->channel == channel && dev->id == id) { |
f48a7a6e PB |
1467 | if (dev->lun == lun) { |
1468 | return dev; | |
1469 | } | |
1470 | target_dev = dev; | |
1471 | } | |
1472 | } | |
1473 | return target_dev; | |
db07c0f8 | 1474 | } |
b9eea3e6 | 1475 | |
63f740dd PB |
1476 | /* SCSI request list. For simplicity, pv points to the whole device */ |
1477 | ||
1478 | static void put_scsi_requests(QEMUFile *f, void *pv, size_t size) | |
1479 | { | |
1480 | SCSIDevice *s = pv; | |
1481 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
1482 | SCSIRequest *req; | |
1483 | ||
1484 | QTAILQ_FOREACH(req, &s->requests, next) { | |
1485 | assert(!req->io_canceled); | |
1486 | assert(req->status == -1); | |
1487 | assert(req->retry); | |
1488 | assert(req->enqueued); | |
1489 | ||
1490 | qemu_put_sbyte(f, 1); | |
1491 | qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf)); | |
1492 | qemu_put_be32s(f, &req->tag); | |
1493 | qemu_put_be32s(f, &req->lun); | |
1494 | if (bus->info->save_request) { | |
1495 | bus->info->save_request(f, req); | |
1496 | } | |
1497 | if (req->ops->save_request) { | |
1498 | req->ops->save_request(f, req); | |
1499 | } | |
1500 | } | |
1501 | qemu_put_sbyte(f, 0); | |
1502 | } | |
1503 | ||
1504 | static int get_scsi_requests(QEMUFile *f, void *pv, size_t size) | |
1505 | { | |
1506 | SCSIDevice *s = pv; | |
1507 | SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | |
1508 | ||
1509 | while (qemu_get_sbyte(f)) { | |
1510 | uint8_t buf[SCSI_CMD_BUF_SIZE]; | |
1511 | uint32_t tag; | |
1512 | uint32_t lun; | |
1513 | SCSIRequest *req; | |
1514 | ||
1515 | qemu_get_buffer(f, buf, sizeof(buf)); | |
1516 | qemu_get_be32s(f, &tag); | |
1517 | qemu_get_be32s(f, &lun); | |
1518 | req = scsi_req_new(s, tag, lun, buf, NULL); | |
1519 | if (bus->info->load_request) { | |
1520 | req->hba_private = bus->info->load_request(f, req); | |
1521 | } | |
1522 | if (req->ops->load_request) { | |
1523 | req->ops->load_request(f, req); | |
1524 | } | |
1525 | ||
1526 | /* Just restart it later. */ | |
1527 | req->retry = true; | |
1528 | scsi_req_enqueue_internal(req); | |
1529 | ||
1530 | /* At this point, the request will be kept alive by the reference | |
1531 | * added by scsi_req_enqueue_internal, so we can release our reference. | |
1532 | * The HBA of course will add its own reference in the load_request | |
1533 | * callback if it needs to hold on the SCSIRequest. | |
1534 | */ | |
1535 | scsi_req_unref(req); | |
1536 | } | |
1537 | ||
1538 | return 0; | |
1539 | } | |
1540 | ||
1541 | const VMStateInfo vmstate_info_scsi_requests = { | |
1542 | .name = "scsi-requests", | |
1543 | .get = get_scsi_requests, | |
1544 | .put = put_scsi_requests, | |
1545 | }; | |
1546 | ||
1547 | const VMStateDescription vmstate_scsi_device = { | |
1548 | .name = "SCSIDevice", | |
1549 | .version_id = 1, | |
1550 | .minimum_version_id = 1, | |
1551 | .minimum_version_id_old = 1, | |
1552 | .fields = (VMStateField[]) { | |
1553 | VMSTATE_UINT8(unit_attention.key, SCSIDevice), | |
1554 | VMSTATE_UINT8(unit_attention.asc, SCSIDevice), | |
1555 | VMSTATE_UINT8(unit_attention.ascq, SCSIDevice), | |
1556 | VMSTATE_BOOL(sense_is_ua, SCSIDevice), | |
1557 | VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE), | |
1558 | VMSTATE_UINT32(sense_len, SCSIDevice), | |
1559 | { | |
1560 | .name = "requests", | |
1561 | .version_id = 0, | |
1562 | .field_exists = NULL, | |
1563 | .size = 0, /* ouch */ | |
1564 | .info = &vmstate_info_scsi_requests, | |
1565 | .flags = VMS_SINGLE, | |
1566 | .offset = 0, | |
1567 | }, | |
1568 | VMSTATE_END_OF_LIST() | |
1569 | } | |
1570 | }; | |
1571 | ||
39bffca2 AL |
1572 | static void scsi_device_class_init(ObjectClass *klass, void *data) |
1573 | { | |
1574 | DeviceClass *k = DEVICE_CLASS(klass); | |
1575 | k->bus_info = &scsi_bus_info; | |
1576 | k->init = scsi_qdev_init; | |
1577 | k->unplug = qdev_simple_unplug_cb; | |
1578 | k->exit = scsi_qdev_exit; | |
1579 | } | |
1580 | ||
b9eea3e6 AL |
1581 | static TypeInfo scsi_device_type_info = { |
1582 | .name = TYPE_SCSI_DEVICE, | |
1583 | .parent = TYPE_DEVICE, | |
1584 | .instance_size = sizeof(SCSIDevice), | |
1585 | .abstract = true, | |
1586 | .class_size = sizeof(SCSIDeviceClass), | |
39bffca2 | 1587 | .class_init = scsi_device_class_init, |
b9eea3e6 AL |
1588 | }; |
1589 | ||
83f7d43a | 1590 | static void scsi_register_types(void) |
b9eea3e6 AL |
1591 | { |
1592 | type_register_static(&scsi_device_type_info); | |
1593 | } | |
1594 | ||
83f7d43a | 1595 | type_init(scsi_register_types) |