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