]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/dev-storage.c
qdev: Drop qbus_set_hotplug_handler() parameter @errp
[mirror_qemu.git] / hw / usb / dev-storage.c
CommitLineData
5fafdf24 1/*
2e5d83bb
PB
2 * USB Mass Storage Device emulation
3 *
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the LGPL.
2e5d83bb
PB
8 */
9
e532b2e0 10#include "qemu/osdep.h"
da34e65c 11#include "qapi/error.h"
d49b6836 12#include "qemu/error-report.h"
0b8fa32f 13#include "qemu/module.h"
1de7afc9
PB
14#include "qemu/option.h"
15#include "qemu/config-file.h"
f1ae32a1 16#include "hw/usb.h"
463581a8 17#include "desc.h"
a27bd6c7 18#include "hw/qdev-properties.h"
0d09e41a 19#include "hw/scsi/scsi.h"
d6454270 20#include "migration/vmstate.h"
9c17d615 21#include "sysemu/sysemu.h"
fa1d36df 22#include "sysemu/block-backend.h"
89f0762d 23#include "qapi/visitor.h"
f348b6d1 24#include "qemu/cutils.h"
2e5d83bb
PB
25
26//#define DEBUG_MSD
27
28#ifdef DEBUG_MSD
001faf32
BS
29#define DPRINTF(fmt, ...) \
30do { printf("usb-msd: " fmt , ## __VA_ARGS__); } while (0)
2e5d83bb 31#else
001faf32 32#define DPRINTF(fmt, ...) do {} while(0)
2e5d83bb
PB
33#endif
34
35/* USB requests. */
36#define MassStorageReset 0xff
37#define GetMaxLun 0xfe
38
39enum USBMSDMode {
40 USB_MSDM_CBW, /* Command Block. */
94843f66 41 USB_MSDM_DATAOUT, /* Transfer data to device. */
2e5d83bb
PB
42 USB_MSDM_DATAIN, /* Transfer data from device. */
43 USB_MSDM_CSW /* Command Status. */
44};
45
92a114f6
GH
46struct usb_msd_csw {
47 uint32_t sig;
48 uint32_t tag;
49 uint32_t residue;
50 uint8_t status;
51};
52
2e5d83bb
PB
53typedef struct {
54 USBDevice dev;
55 enum USBMSDMode mode;
1dc90367 56 uint32_t scsi_off;
a917d384 57 uint32_t scsi_len;
2e5d83bb 58 uint32_t data_len;
92a114f6 59 struct usb_msd_csw csw;
5c6c0e51 60 SCSIRequest *req;
ca9c39fa 61 SCSIBus bus;
34707333
GH
62 /* For async completion. */
63 USBPacket *packet;
64 /* usb-storage only */
428c149b 65 BlockConf conf;
6bb7b867 66 uint32_t removable;
89f0762d 67 SCSIDevice *scsi_dev;
2e5d83bb
PB
68} MSDState;
69
79e2590c
GA
70#define TYPE_USB_STORAGE "usb-storage-dev"
71#define USB_STORAGE_DEV(obj) OBJECT_CHECK(MSDState, (obj), TYPE_USB_STORAGE)
72
a917d384
PB
73struct usb_msd_cbw {
74 uint32_t sig;
75 uint32_t tag;
76 uint32_t data_len;
77 uint8_t flags;
78 uint8_t lun;
79 uint8_t cmd_len;
80 uint8_t cmd[16];
81};
82
81bfd2f2
GH
83enum {
84 STR_MANUFACTURER = 1,
85 STR_PRODUCT,
86 STR_SERIALNUMBER,
ca0c730d
GH
87 STR_CONFIG_FULL,
88 STR_CONFIG_HIGH,
79b40459 89 STR_CONFIG_SUPER,
2e5d83bb
PB
90};
91
81bfd2f2 92static const USBDescStrings desc_strings = {
93bfef4c 93 [STR_MANUFACTURER] = "QEMU",
81bfd2f2
GH
94 [STR_PRODUCT] = "QEMU USB HARDDRIVE",
95 [STR_SERIALNUMBER] = "1",
ca0c730d
GH
96 [STR_CONFIG_FULL] = "Full speed config (usb 1.1)",
97 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
79b40459 98 [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
81bfd2f2
GH
99};
100
ca0c730d 101static const USBDescIface desc_iface_full = {
81bfd2f2
GH
102 .bInterfaceNumber = 0,
103 .bNumEndpoints = 2,
104 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
105 .bInterfaceSubClass = 0x06, /* SCSI */
106 .bInterfaceProtocol = 0x50, /* Bulk */
107 .eps = (USBDescEndpoint[]) {
108 {
109 .bEndpointAddress = USB_DIR_IN | 0x01,
110 .bmAttributes = USB_ENDPOINT_XFER_BULK,
111 .wMaxPacketSize = 64,
112 },{
113 .bEndpointAddress = USB_DIR_OUT | 0x02,
114 .bmAttributes = USB_ENDPOINT_XFER_BULK,
115 .wMaxPacketSize = 64,
116 },
117 }
118};
119
ca0c730d
GH
120static const USBDescDevice desc_device_full = {
121 .bcdUSB = 0x0200,
81bfd2f2
GH
122 .bMaxPacketSize0 = 8,
123 .bNumConfigurations = 1,
124 .confs = (USBDescConfig[]) {
125 {
126 .bNumInterfaces = 1,
127 .bConfigurationValue = 1,
ca0c730d 128 .iConfiguration = STR_CONFIG_FULL,
bd93976a 129 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
add75088 130 .nif = 1,
ca0c730d
GH
131 .ifs = &desc_iface_full,
132 },
133 },
134};
135
136static const USBDescIface desc_iface_high = {
137 .bInterfaceNumber = 0,
138 .bNumEndpoints = 2,
139 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
140 .bInterfaceSubClass = 0x06, /* SCSI */
141 .bInterfaceProtocol = 0x50, /* Bulk */
142 .eps = (USBDescEndpoint[]) {
143 {
144 .bEndpointAddress = USB_DIR_IN | 0x01,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = 512,
147 },{
148 .bEndpointAddress = USB_DIR_OUT | 0x02,
149 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 .wMaxPacketSize = 512,
151 },
152 }
153};
154
155static const USBDescDevice desc_device_high = {
156 .bcdUSB = 0x0200,
157 .bMaxPacketSize0 = 64,
158 .bNumConfigurations = 1,
159 .confs = (USBDescConfig[]) {
160 {
161 .bNumInterfaces = 1,
162 .bConfigurationValue = 1,
163 .iConfiguration = STR_CONFIG_HIGH,
bd93976a 164 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
add75088 165 .nif = 1,
ca0c730d 166 .ifs = &desc_iface_high,
81bfd2f2
GH
167 },
168 },
169};
170
79b40459
GH
171static const USBDescIface desc_iface_super = {
172 .bInterfaceNumber = 0,
173 .bNumEndpoints = 2,
174 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
175 .bInterfaceSubClass = 0x06, /* SCSI */
176 .bInterfaceProtocol = 0x50, /* Bulk */
177 .eps = (USBDescEndpoint[]) {
178 {
179 .bEndpointAddress = USB_DIR_IN | 0x01,
180 .bmAttributes = USB_ENDPOINT_XFER_BULK,
181 .wMaxPacketSize = 1024,
182 .bMaxBurst = 15,
183 },{
184 .bEndpointAddress = USB_DIR_OUT | 0x02,
185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
186 .wMaxPacketSize = 1024,
187 .bMaxBurst = 15,
188 },
189 }
190};
191
192static const USBDescDevice desc_device_super = {
193 .bcdUSB = 0x0300,
194 .bMaxPacketSize0 = 9,
195 .bNumConfigurations = 1,
196 .confs = (USBDescConfig[]) {
197 {
198 .bNumInterfaces = 1,
199 .bConfigurationValue = 1,
200 .iConfiguration = STR_CONFIG_SUPER,
bd93976a 201 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
79b40459
GH
202 .nif = 1,
203 .ifs = &desc_iface_super,
204 },
205 },
206};
207
81bfd2f2
GH
208static const USBDesc desc = {
209 .id = {
db80358a
RT
210 .idVendor = 0x46f4, /* CRC16() of "QEMU" */
211 .idProduct = 0x0001,
81bfd2f2
GH
212 .bcdDevice = 0,
213 .iManufacturer = STR_MANUFACTURER,
214 .iProduct = STR_PRODUCT,
215 .iSerialNumber = STR_SERIALNUMBER,
216 },
79b40459
GH
217 .full = &desc_device_full,
218 .high = &desc_device_high,
219 .super = &desc_device_super,
220 .str = desc_strings,
2e5d83bb
PB
221};
222
29c74f76 223static void usb_msd_copy_data(MSDState *s, USBPacket *p)
a917d384
PB
224{
225 uint32_t len;
9a77a0f5 226 len = p->iov.size - p->actual_length;
a917d384
PB
227 if (len > s->scsi_len)
228 len = s->scsi_len;
1dc90367 229 usb_packet_copy(p, scsi_req_get_buf(s->req) + s->scsi_off, len);
a917d384 230 s->scsi_len -= len;
1dc90367 231 s->scsi_off += len;
7ad3d51e
PZ
232 if (len > s->data_len) {
233 len = s->data_len;
234 }
a917d384 235 s->data_len -= len;
fa7935c1 236 if (s->scsi_len == 0 || s->data_len == 0) {
ad3376cc 237 scsi_req_continue(s->req);
a917d384
PB
238 }
239}
240
ab4797ad 241static void usb_msd_send_status(MSDState *s, USBPacket *p)
a917d384 242{
ab4797ad 243 int len;
a917d384 244
e04da7c3 245 DPRINTF("Command status %d tag 0x%x, len %zd\n",
e2854bf3 246 s->csw.status, le32_to_cpu(s->csw.tag), p->iov.size);
92a114f6 247
e2854bf3 248 assert(s->csw.sig == cpu_to_le32(0x53425355));
92a114f6
GH
249 len = MIN(sizeof(s->csw), p->iov.size);
250 usb_packet_copy(p, &s->csw, len);
251 memset(&s->csw, 0, sizeof(s->csw));
a917d384
PB
252}
253
1e6ed80b
GH
254static void usb_msd_packet_complete(MSDState *s)
255{
256 USBPacket *p = s->packet;
257
258 /* Set s->packet to NULL before calling usb_packet_complete
259 because another request may be issued before
260 usb_packet_complete returns. */
261 DPRINTF("Packet complete %p\n", p);
262 s->packet = NULL;
263 usb_packet_complete(&s->dev, p);
264}
265
aba1f023 266static void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
2e5d83bb 267{
5c6c0e51 268 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
a917d384 269 USBPacket *p = s->packet;
4d611c9a 270
ad3376cc 271 assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode == SCSI_XFER_TO_DEV));
aba1f023 272 s->scsi_len = len;
1dc90367 273 s->scsi_off = 0;
a917d384 274 if (p) {
29c74f76
GH
275 usb_msd_copy_data(s, p);
276 p = s->packet;
9a77a0f5
HG
277 if (p && p->actual_length == p->iov.size) {
278 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
1e6ed80b 279 usb_msd_packet_complete(s);
a917d384 280 }
4d611c9a 281 }
2e5d83bb
PB
282}
283
01e95455 284static void usb_msd_command_complete(SCSIRequest *req, uint32_t status, size_t resid)
c6df7102
PB
285{
286 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
287 USBPacket *p = s->packet;
288
7b863f41 289 DPRINTF("Command complete %d tag 0x%x\n", status, req->tag);
92a114f6
GH
290
291 s->csw.sig = cpu_to_le32(0x53425355);
7b863f41 292 s->csw.tag = cpu_to_le32(req->tag);
0659879e 293 s->csw.residue = cpu_to_le32(s->data_len);
414c4604 294 s->csw.status = status != 0;
92a114f6 295
c6df7102
PB
296 if (s->packet) {
297 if (s->data_len == 0 && s->mode == USB_MSDM_DATAOUT) {
298 /* A deferred packet with no write data remaining must be
299 the status read packet. */
300 usb_msd_send_status(s, p);
301 s->mode = USB_MSDM_CBW;
54414218
GH
302 } else if (s->mode == USB_MSDM_CSW) {
303 usb_msd_send_status(s, p);
304 s->mode = USB_MSDM_CBW;
c6df7102
PB
305 } else {
306 if (s->data_len) {
9a77a0f5 307 int len = (p->iov.size - p->actual_length);
29c74f76 308 usb_packet_skip(p, len);
7ad3d51e
PZ
309 if (len > s->data_len) {
310 len = s->data_len;
311 }
29c74f76 312 s->data_len -= len;
c6df7102
PB
313 }
314 if (s->data_len == 0) {
315 s->mode = USB_MSDM_CSW;
316 }
317 }
9a77a0f5 318 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
1e6ed80b 319 usb_msd_packet_complete(s);
c6df7102
PB
320 } else if (s->data_len == 0) {
321 s->mode = USB_MSDM_CSW;
322 }
323 scsi_req_unref(req);
324 s->req = NULL;
325}
326
94d3f98a
PB
327static void usb_msd_request_cancelled(SCSIRequest *req)
328{
329 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
330
331 if (req == s->req) {
332 scsi_req_unref(s->req);
333 s->req = NULL;
94d3f98a
PB
334 s->scsi_len = 0;
335 }
336}
337
059809e4 338static void usb_msd_handle_reset(USBDevice *dev)
2e5d83bb
PB
339{
340 MSDState *s = (MSDState *)dev;
341
342 DPRINTF("Reset\n");
24a5bbe1
GH
343 if (s->req) {
344 scsi_req_cancel(s->req);
345 }
346 assert(s->req == NULL);
347
348 if (s->packet) {
9a77a0f5 349 s->packet->status = USB_RET_STALL;
1e6ed80b 350 usb_msd_packet_complete(s);
24a5bbe1
GH
351 }
352
2e5d83bb 353 s->mode = USB_MSDM_CBW;
2e5d83bb
PB
354}
355
9a77a0f5 356static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 357 int request, int value, int index, int length, uint8_t *data)
2e5d83bb
PB
358{
359 MSDState *s = (MSDState *)dev;
34707333
GH
360 SCSIDevice *scsi_dev;
361 int ret, maxlun;
2e5d83bb 362
007fd62f 363 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
81bfd2f2 364 if (ret >= 0) {
9a77a0f5 365 return;
81bfd2f2
GH
366 }
367
2e5d83bb 368 switch (request) {
2e5d83bb 369 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
e5322f76 370 break;
2e5d83bb 371 /* Class specific requests. */
f3571b1a 372 case ClassInterfaceOutRequest | MassStorageReset:
2e5d83bb
PB
373 /* Reset state ready for the next CBW. */
374 s->mode = USB_MSDM_CBW;
2e5d83bb 375 break;
f3571b1a 376 case ClassInterfaceRequest | GetMaxLun:
34707333
GH
377 maxlun = 0;
378 for (;;) {
379 scsi_dev = scsi_device_find(&s->bus, 0, 0, maxlun+1);
380 if (scsi_dev == NULL) {
381 break;
382 }
383 if (scsi_dev->lun != maxlun+1) {
384 break;
385 }
386 maxlun++;
387 }
388 DPRINTF("MaxLun %d\n", maxlun);
389 data[0] = maxlun;
9a77a0f5 390 p->actual_length = 1;
2e5d83bb
PB
391 break;
392 default:
9a77a0f5 393 p->status = USB_RET_STALL;
2e5d83bb
PB
394 break;
395 }
2e5d83bb
PB
396}
397
eb5e680a 398static void usb_msd_cancel_io(USBDevice *dev, USBPacket *p)
4d611c9a 399{
79e2590c 400 MSDState *s = USB_STORAGE_DEV(dev);
d3ac1a87 401
6d7aeeeb
GH
402 assert(s->packet == p);
403 s->packet = NULL;
404
d3ac1a87
GH
405 if (s->req) {
406 scsi_req_cancel(s->req);
407 }
4d611c9a
PB
408}
409
9a77a0f5 410static void usb_msd_handle_data(USBDevice *dev, USBPacket *p)
2e5d83bb
PB
411{
412 MSDState *s = (MSDState *)dev;
7b863f41 413 uint32_t tag;
2e5d83bb 414 struct usb_msd_cbw cbw;
079d0b7f 415 uint8_t devep = p->ep->nr;
34707333 416 SCSIDevice *scsi_dev;
9db7c414 417 uint32_t len;
2e5d83bb 418
4d611c9a 419 switch (p->pid) {
2e5d83bb
PB
420 case USB_TOKEN_OUT:
421 if (devep != 2)
422 goto fail;
423
424 switch (s->mode) {
425 case USB_MSDM_CBW:
29c74f76 426 if (p->iov.size != 31) {
f5dc5978 427 error_report("usb-msd: Bad CBW size");
2e5d83bb
PB
428 goto fail;
429 }
29c74f76 430 usb_packet_copy(p, &cbw, 31);
2e5d83bb 431 if (le32_to_cpu(cbw.sig) != 0x43425355) {
f5dc5978
GA
432 error_report("usb-msd: Bad signature %08x",
433 le32_to_cpu(cbw.sig));
2e5d83bb
PB
434 goto fail;
435 }
436 DPRINTF("Command on LUN %d\n", cbw.lun);
34707333
GH
437 scsi_dev = scsi_device_find(&s->bus, 0, 0, cbw.lun);
438 if (scsi_dev == NULL) {
f5dc5978 439 error_report("usb-msd: Bad LUN %d", cbw.lun);
2e5d83bb
PB
440 goto fail;
441 }
7b863f41 442 tag = le32_to_cpu(cbw.tag);
2e5d83bb
PB
443 s->data_len = le32_to_cpu(cbw.data_len);
444 if (s->data_len == 0) {
445 s->mode = USB_MSDM_CSW;
446 } else if (cbw.flags & 0x80) {
447 s->mode = USB_MSDM_DATAIN;
448 } else {
449 s->mode = USB_MSDM_DATAOUT;
450 }
451 DPRINTF("Command tag 0x%x flags %08x len %d data %d\n",
7b863f41 452 tag, cbw.flags, cbw.cmd_len, s->data_len);
0659879e 453 assert(le32_to_cpu(s->csw.residue) == 0);
ef0bdf77 454 s->scsi_len = 0;
34707333 455 s->req = scsi_req_new(scsi_dev, tag, cbw.lun, cbw.cmd, NULL);
06f9847d
GH
456#ifdef DEBUG_MSD
457 scsi_req_print(s->req);
458#endif
9db7c414
GH
459 len = scsi_req_enqueue(s->req);
460 if (len) {
ad3376cc 461 scsi_req_continue(s->req);
a917d384 462 }
2e5d83bb
PB
463 break;
464
465 case USB_MSDM_DATAOUT:
29c74f76
GH
466 DPRINTF("Data out %zd/%d\n", p->iov.size, s->data_len);
467 if (p->iov.size > s->data_len) {
2e5d83bb 468 goto fail;
29c74f76 469 }
2e5d83bb 470
a917d384 471 if (s->scsi_len) {
29c74f76 472 usb_msd_copy_data(s, p);
a917d384 473 }
0659879e 474 if (le32_to_cpu(s->csw.residue)) {
9a77a0f5 475 int len = p->iov.size - p->actual_length;
29c74f76
GH
476 if (len) {
477 usb_packet_skip(p, len);
7ad3d51e
PZ
478 if (len > s->data_len) {
479 len = s->data_len;
480 }
29c74f76
GH
481 s->data_len -= len;
482 if (s->data_len == 0) {
483 s->mode = USB_MSDM_CSW;
484 }
485 }
a917d384 486 }
9a77a0f5 487 if (p->actual_length < p->iov.size) {
06f9847d 488 DPRINTF("Deferring packet %p [wait data-out]\n", p);
4d611c9a 489 s->packet = p;
9a77a0f5 490 p->status = USB_RET_ASYNC;
4d611c9a 491 }
2e5d83bb
PB
492 break;
493
494 default:
29c74f76 495 DPRINTF("Unexpected write (len %zd)\n", p->iov.size);
2e5d83bb
PB
496 goto fail;
497 }
498 break;
499
500 case USB_TOKEN_IN:
501 if (devep != 1)
502 goto fail;
503
504 switch (s->mode) {
a917d384 505 case USB_MSDM_DATAOUT:
29c74f76 506 if (s->data_len != 0 || p->iov.size < 13) {
a917d384 507 goto fail;
29c74f76 508 }
a917d384 509 /* Waiting for SCSI write to complete. */
a917d384 510 s->packet = p;
9a77a0f5 511 p->status = USB_RET_ASYNC;
a917d384
PB
512 break;
513
2e5d83bb 514 case USB_MSDM_CSW:
29c74f76 515 if (p->iov.size < 13) {
2e5d83bb 516 goto fail;
29c74f76 517 }
2e5d83bb 518
59310659
GH
519 if (s->req) {
520 /* still in flight */
06f9847d 521 DPRINTF("Deferring packet %p [wait status]\n", p);
59310659 522 s->packet = p;
9a77a0f5 523 p->status = USB_RET_ASYNC;
59310659
GH
524 } else {
525 usb_msd_send_status(s, p);
526 s->mode = USB_MSDM_CBW;
59310659 527 }
2e5d83bb
PB
528 break;
529
530 case USB_MSDM_DATAIN:
29c74f76
GH
531 DPRINTF("Data in %zd/%d, scsi_len %d\n",
532 p->iov.size, s->data_len, s->scsi_len);
a917d384 533 if (s->scsi_len) {
29c74f76 534 usb_msd_copy_data(s, p);
a917d384 535 }
0659879e 536 if (le32_to_cpu(s->csw.residue)) {
9a77a0f5 537 int len = p->iov.size - p->actual_length;
29c74f76
GH
538 if (len) {
539 usb_packet_skip(p, len);
7ad3d51e
PZ
540 if (len > s->data_len) {
541 len = s->data_len;
542 }
29c74f76
GH
543 s->data_len -= len;
544 if (s->data_len == 0) {
545 s->mode = USB_MSDM_CSW;
546 }
547 }
a917d384 548 }
7ad3d51e
PZ
549 if (p->actual_length < p->iov.size && (p->short_not_ok ||
550 s->scsi_len >= p->ep->max_packet_size)) {
06f9847d 551 DPRINTF("Deferring packet %p [wait data-in]\n", p);
4d611c9a 552 s->packet = p;
9a77a0f5 553 p->status = USB_RET_ASYNC;
4d611c9a 554 }
2e5d83bb
PB
555 break;
556
557 default:
29c74f76 558 DPRINTF("Unexpected read (len %zd)\n", p->iov.size);
2e5d83bb
PB
559 goto fail;
560 }
561 break;
562
563 default:
564 DPRINTF("Bad token\n");
565 fail:
9a77a0f5 566 p->status = USB_RET_STALL;
2e5d83bb
PB
567 break;
568 }
2e5d83bb
PB
569}
570
5de88b1d
GH
571static void *usb_msd_load_request(QEMUFile *f, SCSIRequest *req)
572{
573 MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
574
575 /* nothing to load, just store req in our state struct */
576 assert(s->req == NULL);
577 scsi_req_ref(req);
578 s->req = req;
579 return NULL;
580}
581
34707333 582static const struct SCSIBusInfo usb_msd_scsi_info_storage = {
afd4030c 583 .tcq = false,
7e0380b9
PB
584 .max_target = 0,
585 .max_lun = 0,
afd4030c 586
c6df7102 587 .transfer_data = usb_msd_transfer_data,
94d3f98a 588 .complete = usb_msd_command_complete,
5de88b1d
GH
589 .cancel = usb_msd_request_cancelled,
590 .load_request = usb_msd_load_request,
cfdc1bb0
PB
591};
592
34707333
GH
593static const struct SCSIBusInfo usb_msd_scsi_info_bot = {
594 .tcq = false,
595 .max_target = 0,
596 .max_lun = 15,
597
598 .transfer_data = usb_msd_transfer_data,
599 .complete = usb_msd_command_complete,
600 .cancel = usb_msd_request_cancelled,
601 .load_request = usb_msd_load_request,
602};
603
6db3ea39 604static void usb_msd_storage_realize(USBDevice *dev, Error **errp)
806b6024 605{
79e2590c 606 MSDState *s = USB_STORAGE_DEV(dev);
4be74634 607 BlockBackend *blk = s->conf.blk;
34707333 608 SCSIDevice *scsi_dev;
806b6024 609
4be74634 610 if (!blk) {
5a882e40
GA
611 error_setg(errp, "drive property not set");
612 return;
7fc2f2c0
GH
613 }
614
c56ee92f
RK
615 if (!blkconf_blocksizes(&s->conf, errp)) {
616 return;
617 }
618
ceff3e1f
MZ
619 if (!blkconf_apply_backend_options(&s->conf, blk_is_read_only(blk), true,
620 errp)) {
a17c17a2
KW
621 return;
622 }
911525db 623
14bafc54
MA
624 /*
625 * Hack alert: this pretends to be a block device, but it's really
626 * a SCSI bus that can serve only a single device, which it
18846dee
MA
627 * creates automatically. But first it needs to detach from its
628 * blockdev, or else scsi_bus_legacy_add_drive() dies when it
8daea510
KW
629 * attaches again. We also need to take another reference so that
630 * blk_detach_dev() doesn't free blk while we still need it.
14bafc54
MA
631 *
632 * The hack is probably a bad idea.
633 */
8daea510 634 blk_ref(blk);
4ffebe23 635 blk_detach_dev(blk, DEVICE(s));
4be74634 636 s->conf.blk = NULL;
14bafc54 637
71938a09 638 usb_desc_create_serial(dev);
a980a065 639 usb_desc_init(dev);
b1187b51
AF
640 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
641 &usb_msd_scsi_info_storage, NULL);
4be74634 642 scsi_dev = scsi_bus_legacy_add_drive(&s->bus, blk, 0, !!s->removable,
395b9539 643 s->conf.bootindex, s->conf.share_rw,
b8efb36b 644 s->conf.rerror, s->conf.werror,
395b9539 645 dev->serial,
ceff3e1f 646 errp);
8daea510 647 blk_unref(blk);
34707333 648 if (!scsi_dev) {
5a882e40 649 return;
fa66b909 650 }
7fc2f2c0 651 usb_msd_handle_reset(dev);
89f0762d 652 s->scsi_dev = scsi_dev;
806b6024
GH
653}
654
6db3ea39 655static void usb_msd_bot_realize(USBDevice *dev, Error **errp)
34707333 656{
79e2590c 657 MSDState *s = USB_STORAGE_DEV(dev);
b78ecd09 658 DeviceState *d = DEVICE(dev);
34707333
GH
659
660 usb_desc_create_serial(dev);
661 usb_desc_init(dev);
b78ecd09
GH
662 if (d->hotplugged) {
663 s->dev.auto_attach = 0;
664 }
665
b1187b51
AF
666 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(dev),
667 &usb_msd_scsi_info_bot, NULL);
34707333 668 usb_msd_handle_reset(dev);
34707333
GH
669}
670
f54b6563
GH
671static const VMStateDescription vmstate_usb_msd = {
672 .name = "usb-storage",
f54b6563
GH
673 .version_id = 1,
674 .minimum_version_id = 1,
6e3d652a 675 .fields = (VMStateField[]) {
f54b6563 676 VMSTATE_USB_DEVICE(dev, MSDState),
5de88b1d
GH
677 VMSTATE_UINT32(mode, MSDState),
678 VMSTATE_UINT32(scsi_len, MSDState),
679 VMSTATE_UINT32(scsi_off, MSDState),
680 VMSTATE_UINT32(data_len, MSDState),
681 VMSTATE_UINT32(csw.sig, MSDState),
682 VMSTATE_UINT32(csw.tag, MSDState),
683 VMSTATE_UINT32(csw.residue, MSDState),
684 VMSTATE_UINT8(csw.status, MSDState),
f54b6563
GH
685 VMSTATE_END_OF_LIST()
686 }
687};
688
39bffca2
AL
689static Property msd_properties[] = {
690 DEFINE_BLOCK_PROPERTIES(MSDState, conf),
b8efb36b 691 DEFINE_BLOCK_ERROR_PROPERTIES(MSDState, conf),
39bffca2
AL
692 DEFINE_PROP_BIT("removable", MSDState, removable, 0, false),
693 DEFINE_PROP_END_OF_LIST(),
694};
695
79e2590c 696static void usb_msd_class_initfn_common(ObjectClass *klass, void *data)
62aed765 697{
39bffca2 698 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
699 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
700
62aed765
AL
701 uc->product_desc = "QEMU USB MSD";
702 uc->usb_desc = &desc;
62aed765
AL
703 uc->cancel_packet = usb_msd_cancel_io;
704 uc->handle_attach = usb_desc_attach;
705 uc->handle_reset = usb_msd_handle_reset;
706 uc->handle_control = usb_msd_handle_control;
707 uc->handle_data = usb_msd_handle_data;
125ee0ed 708 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
39bffca2
AL
709 dc->fw_name = "storage";
710 dc->vmsd = &vmstate_usb_msd;
34707333
GH
711}
712
6db3ea39 713static void usb_msd_class_storage_initfn(ObjectClass *klass, void *data)
34707333
GH
714{
715 DeviceClass *dc = DEVICE_CLASS(klass);
716 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
717
6db3ea39 718 uc->realize = usb_msd_storage_realize;
4f67d30b 719 device_class_set_props(dc, msd_properties);
34707333
GH
720}
721
d7bce999
EB
722static void usb_msd_get_bootindex(Object *obj, Visitor *v, const char *name,
723 void *opaque, Error **errp)
89f0762d
GA
724{
725 USBDevice *dev = USB_DEVICE(obj);
79e2590c 726 MSDState *s = USB_STORAGE_DEV(dev);
89f0762d 727
51e72bc1 728 visit_type_int32(v, name, &s->conf.bootindex, errp);
89f0762d
GA
729}
730
d7bce999
EB
731static void usb_msd_set_bootindex(Object *obj, Visitor *v, const char *name,
732 void *opaque, Error **errp)
89f0762d
GA
733{
734 USBDevice *dev = USB_DEVICE(obj);
79e2590c 735 MSDState *s = USB_STORAGE_DEV(dev);
89f0762d
GA
736 int32_t boot_index;
737 Error *local_err = NULL;
738
51e72bc1 739 visit_type_int32(v, name, &boot_index, &local_err);
89f0762d
GA
740 if (local_err) {
741 goto out;
742 }
743 /* check whether bootindex is present in fw_boot_order list */
744 check_boot_index(boot_index, &local_err);
745 if (local_err) {
746 goto out;
747 }
748 /* change bootindex to a new one */
749 s->conf.bootindex = boot_index;
750
751 if (s->scsi_dev) {
752 object_property_set_int(OBJECT(s->scsi_dev), boot_index, "bootindex",
753 &error_abort);
754 }
755
756out:
621ff94d 757 error_propagate(errp, local_err);
89f0762d
GA
758}
759
79e2590c
GA
760static const TypeInfo usb_storage_dev_type_info = {
761 .name = TYPE_USB_STORAGE,
762 .parent = TYPE_USB_DEVICE,
763 .instance_size = sizeof(MSDState),
764 .abstract = true,
765 .class_init = usb_msd_class_initfn_common,
766};
767
89f0762d
GA
768static void usb_msd_instance_init(Object *obj)
769{
770 object_property_add(obj, "bootindex", "int32",
771 usb_msd_get_bootindex,
d2623129 772 usb_msd_set_bootindex, NULL, NULL);
89f0762d
GA
773 object_property_set_int(obj, -1, "bootindex", NULL);
774}
775
6db3ea39 776static void usb_msd_class_bot_initfn(ObjectClass *klass, void *data)
34707333
GH
777{
778 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
779
6db3ea39 780 uc->realize = usb_msd_bot_realize;
b78ecd09 781 uc->attached_settable = true;
62aed765
AL
782}
783
8c43a6f0 784static const TypeInfo msd_info = {
39bffca2 785 .name = "usb-storage",
79e2590c 786 .parent = TYPE_USB_STORAGE,
6db3ea39 787 .class_init = usb_msd_class_storage_initfn,
89f0762d 788 .instance_init = usb_msd_instance_init,
34707333
GH
789};
790
791static const TypeInfo bot_info = {
792 .name = "usb-bot",
79e2590c 793 .parent = TYPE_USB_STORAGE,
6db3ea39 794 .class_init = usb_msd_class_bot_initfn,
806b6024
GH
795};
796
83f7d43a 797static void usb_msd_register_types(void)
806b6024 798{
79e2590c 799 type_register_static(&usb_storage_dev_type_info);
39bffca2 800 type_register_static(&msd_info);
34707333 801 type_register_static(&bot_info);
806b6024 802}
83f7d43a
AF
803
804type_init(usb_msd_register_types)