master
c 1,017 lines 29.6 KB
Raw
1 /*
2 * UAS (USB Attached SCSI) emulation
3 *
4 * Copyright Red Hat, Inc. 2012
5 *
6 * Author: Gerd Hoffmann <kraxel@redhat.com>
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
10 */
11
12 #include "qemu/osdep.h"
13 #include "qemu/option.h"
14 #include "qemu/config-file.h"
15 #include "trace.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
19 #include "qemu/log.h"
20
21 #include "hw/usb/usb.h"
22 #include "migration/vmstate.h"
23 #include "desc.h"
24 #include "hw/core/qdev-properties.h"
25 #include "hw/scsi/scsi.h"
26 #include "scsi/constants.h"
27 #include "qom/object.h"
28
29 /* --------------------------------------------------------------------- */
30
31 #define UAS_UI_COMMAND 0x01
32 #define UAS_UI_SENSE 0x03
33 #define UAS_UI_RESPONSE 0x04
34 #define UAS_UI_TASK_MGMT 0x05
35 #define UAS_UI_READ_READY 0x06
36 #define UAS_UI_WRITE_READY 0x07
37
38 #define UAS_RC_TMF_COMPLETE 0x00
39 #define UAS_RC_INVALID_INFO_UNIT 0x02
40 #define UAS_RC_TMF_NOT_SUPPORTED 0x04
41 #define UAS_RC_TMF_FAILED 0x05
42 #define UAS_RC_TMF_SUCCEEDED 0x08
43 #define UAS_RC_INCORRECT_LUN 0x09
44 #define UAS_RC_OVERLAPPED_TAG 0x0a
45
46 #define UAS_TMF_ABORT_TASK 0x01
47 #define UAS_TMF_ABORT_TASK_SET 0x02
48 #define UAS_TMF_CLEAR_TASK_SET 0x04
49 #define UAS_TMF_LOGICAL_UNIT_RESET 0x08
50 #define UAS_TMF_I_T_NEXUS_RESET 0x10
51 #define UAS_TMF_CLEAR_ACA 0x40
52 #define UAS_TMF_QUERY_TASK 0x80
53 #define UAS_TMF_QUERY_TASK_SET 0x81
54 #define UAS_TMF_QUERY_ASYNC_EVENT 0x82
55
56 #define UAS_PIPE_ID_COMMAND 0x01
57 #define UAS_PIPE_ID_STATUS 0x02
58 #define UAS_PIPE_ID_DATA_IN 0x03
59 #define UAS_PIPE_ID_DATA_OUT 0x04
60
61 typedef struct {
62 uint8_t id;
63 uint8_t reserved;
64 uint16_t tag;
65 } QEMU_PACKED uas_iu_header;
66
67 typedef struct {
68 uint8_t prio_taskattr; /* 6:3 priority, 2:0 task attribute */
69 uint8_t reserved_1;
70 uint8_t add_cdb_length; /* 7:2 additional adb length (dwords) */
71 uint8_t reserved_2;
72 uint64_t lun;
73 uint8_t cdb[16];
74 uint8_t add_cdb[1];
75 } QEMU_PACKED uas_iu_command;
76
77 typedef struct {
78 uint16_t status_qualifier;
79 uint8_t status;
80 uint8_t reserved[7];
81 uint16_t sense_length;
82 uint8_t sense_data[18];
83 } QEMU_PACKED uas_iu_sense;
84
85 typedef struct {
86 uint8_t add_response_info[3];
87 uint8_t response_code;
88 } QEMU_PACKED uas_iu_response;
89
90 typedef struct {
91 uint8_t function;
92 uint8_t reserved;
93 uint16_t task_tag;
94 uint64_t lun;
95 } QEMU_PACKED uas_iu_task_mgmt;
96
97 typedef struct {
98 uas_iu_header hdr;
99 union {
100 uas_iu_command command;
101 uas_iu_sense sense;
102 uas_iu_task_mgmt task;
103 uas_iu_response response;
104 };
105 } QEMU_PACKED uas_iu;
106
107 /* --------------------------------------------------------------------- */
108
109 #define UAS_STREAM_BM_ATTR 4
110 #define UAS_MAX_STREAMS (1 << UAS_STREAM_BM_ATTR)
111
112 typedef struct UASRequest UASRequest;
113 typedef struct UASStatus UASStatus;
114
115 #define TYPE_USB_UAS "usb-uas"
116 OBJECT_DECLARE_SIMPLE_TYPE(UASDevice, USB_UAS)
117
118 struct UASDevice {
119 USBDevice dev;
120 SCSIBus bus;
121 QEMUBH *status_bh;
122 QTAILQ_HEAD(, UASStatus) results;
123 QTAILQ_HEAD(, UASRequest) requests;
124
125 /* properties */
126 uint32_t requestlog;
127
128 /* usb 2.0 only */
129 USBPacket *status2;
130 UASRequest *datain2;
131 UASRequest *dataout2;
132
133 /* usb 3.0 only */
134 USBPacket *data3[UAS_MAX_STREAMS + 1];
135 USBPacket *status3[UAS_MAX_STREAMS + 1];
136 };
137
138 struct UASRequest {
139 uint16_t tag;
140 uint64_t lun;
141 UASDevice *uas;
142 SCSIDevice *dev;
143 SCSIRequest *req;
144 USBPacket *data;
145 bool data_async;
146 bool active;
147 bool complete;
148 uint32_t buf_off;
149 uint32_t buf_size;
150 uint32_t data_off;
151 uint32_t data_size;
152 QTAILQ_ENTRY(UASRequest) next;
153 };
154
155 struct UASStatus {
156 uint32_t stream;
157 uas_iu status;
158 uint32_t length;
159 QTAILQ_ENTRY(UASStatus) next;
160 };
161
162 /* --------------------------------------------------------------------- */
163
164 enum {
165 STR_MANUFACTURER = 1,
166 STR_PRODUCT,
167 STR_SERIALNUMBER,
168 STR_CONFIG_HIGH,
169 STR_CONFIG_SUPER,
170 };
171
172 static const USBDescStrings desc_strings = {
173 [STR_MANUFACTURER] = "QEMU",
174 [STR_PRODUCT] = "USB Attached SCSI HBA",
175 [STR_SERIALNUMBER] = "27842",
176 [STR_CONFIG_HIGH] = "High speed config (usb 2.0)",
177 [STR_CONFIG_SUPER] = "Super speed config (usb 3.0)",
178 };
179
180 static const USBDescIface desc_iface_high = {
181 .bInterfaceNumber = 0,
182 .bNumEndpoints = 4,
183 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
184 .bInterfaceSubClass = 0x06, /* SCSI */
185 .bInterfaceProtocol = 0x62, /* UAS */
186 .eps = (USBDescEndpoint[]) {
187 {
188 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_COMMAND,
189 .bmAttributes = USB_ENDPOINT_XFER_BULK,
190 .wMaxPacketSize = 512,
191 .extra = (uint8_t[]) {
192 0x04, /* u8 bLength */
193 0x24, /* u8 bDescriptorType */
194 UAS_PIPE_ID_COMMAND,
195 0x00, /* u8 bReserved */
196 },
197 },{
198 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_STATUS,
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200 .wMaxPacketSize = 512,
201 .extra = (uint8_t[]) {
202 0x04, /* u8 bLength */
203 0x24, /* u8 bDescriptorType */
204 UAS_PIPE_ID_STATUS,
205 0x00, /* u8 bReserved */
206 },
207 },{
208 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_DATA_IN,
209 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 .wMaxPacketSize = 512,
211 .extra = (uint8_t[]) {
212 0x04, /* u8 bLength */
213 0x24, /* u8 bDescriptorType */
214 UAS_PIPE_ID_DATA_IN,
215 0x00, /* u8 bReserved */
216 },
217 },{
218 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_DATA_OUT,
219 .bmAttributes = USB_ENDPOINT_XFER_BULK,
220 .wMaxPacketSize = 512,
221 .extra = (uint8_t[]) {
222 0x04, /* u8 bLength */
223 0x24, /* u8 bDescriptorType */
224 UAS_PIPE_ID_DATA_OUT,
225 0x00, /* u8 bReserved */
226 },
227 },
228 }
229 };
230
231 static const USBDescIface desc_iface_super = {
232 .bInterfaceNumber = 0,
233 .bNumEndpoints = 4,
234 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
235 .bInterfaceSubClass = 0x06, /* SCSI */
236 .bInterfaceProtocol = 0x62, /* UAS */
237 .eps = (USBDescEndpoint[]) {
238 {
239 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_COMMAND,
240 .bmAttributes = USB_ENDPOINT_XFER_BULK,
241 .wMaxPacketSize = 1024,
242 .bMaxBurst = 15,
243 .extra = (uint8_t[]) {
244 0x04, /* u8 bLength */
245 0x24, /* u8 bDescriptorType */
246 UAS_PIPE_ID_COMMAND,
247 0x00, /* u8 bReserved */
248 },
249 },{
250 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_STATUS,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
252 .wMaxPacketSize = 1024,
253 .bMaxBurst = 15,
254 .bmAttributes_super = UAS_STREAM_BM_ATTR,
255 .extra = (uint8_t[]) {
256 0x04, /* u8 bLength */
257 0x24, /* u8 bDescriptorType */
258 UAS_PIPE_ID_STATUS,
259 0x00, /* u8 bReserved */
260 },
261 },{
262 .bEndpointAddress = USB_DIR_IN | UAS_PIPE_ID_DATA_IN,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK,
264 .wMaxPacketSize = 1024,
265 .bMaxBurst = 15,
266 .bmAttributes_super = UAS_STREAM_BM_ATTR,
267 .extra = (uint8_t[]) {
268 0x04, /* u8 bLength */
269 0x24, /* u8 bDescriptorType */
270 UAS_PIPE_ID_DATA_IN,
271 0x00, /* u8 bReserved */
272 },
273 },{
274 .bEndpointAddress = USB_DIR_OUT | UAS_PIPE_ID_DATA_OUT,
275 .bmAttributes = USB_ENDPOINT_XFER_BULK,
276 .wMaxPacketSize = 1024,
277 .bMaxBurst = 15,
278 .bmAttributes_super = UAS_STREAM_BM_ATTR,
279 .extra = (uint8_t[]) {
280 0x04, /* u8 bLength */
281 0x24, /* u8 bDescriptorType */
282 UAS_PIPE_ID_DATA_OUT,
283 0x00, /* u8 bReserved */
284 },
285 },
286 }
287 };
288
289 static const USBDescDevice desc_device_high = {
290 .bcdUSB = 0x0200,
291 .bMaxPacketSize0 = 64,
292 .bNumConfigurations = 1,
293 .confs = (USBDescConfig[]) {
294 {
295 .bNumInterfaces = 1,
296 .bConfigurationValue = 1,
297 .iConfiguration = STR_CONFIG_HIGH,
298 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
299 .nif = 1,
300 .ifs = &desc_iface_high,
301 },
302 },
303 };
304
305 static const USBDescDevice desc_device_super = {
306 .bcdUSB = 0x0300,
307 .bMaxPacketSize0 = 9,
308 .bNumConfigurations = 1,
309 .confs = (USBDescConfig[]) {
310 {
311 .bNumInterfaces = 1,
312 .bConfigurationValue = 1,
313 .iConfiguration = STR_CONFIG_SUPER,
314 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
315 .nif = 1,
316 .ifs = &desc_iface_super,
317 },
318 },
319 };
320
321 static const USBDesc desc = {
322 .id = {
323 .idVendor = 0x46f4, /* CRC16() of "QEMU" */
324 .idProduct = 0x0003,
325 .bcdDevice = 0,
326 .iManufacturer = STR_MANUFACTURER,
327 .iProduct = STR_PRODUCT,
328 .iSerialNumber = STR_SERIALNUMBER,
329 },
330 .high = &desc_device_high,
331 .super = &desc_device_super,
332 .str = desc_strings,
333 };
334
335 /* --------------------------------------------------------------------- */
336
337 static bool uas_using_streams(UASDevice *uas)
338 {
339 return uas->dev.speed == USB_SPEED_SUPER;
340 }
341
342 /* --------------------------------------------------------------------- */
343
344 static UASStatus *usb_uas_alloc_status(UASDevice *uas, uint8_t id, uint16_t tag)
345 {
346 UASStatus *st = g_new0(UASStatus, 1);
347
348 st->status.hdr.id = id;
349 st->status.hdr.tag = cpu_to_be16(tag);
350 st->length = sizeof(uas_iu_header);
351 if (uas_using_streams(uas)) {
352 st->stream = tag;
353 }
354 return st;
355 }
356
357 static void usb_uas_send_status_bh(void *opaque)
358 {
359 UASDevice *uas = opaque;
360 UASStatus *st;
361 USBPacket *p;
362 uint32_t length;
363
364 while ((st = QTAILQ_FIRST(&uas->results)) != NULL) {
365 if (uas_using_streams(uas)) {
366 assert(st->stream <= UAS_MAX_STREAMS);
367 p = uas->status3[st->stream];
368 uas->status3[st->stream] = NULL;
369 } else {
370 p = uas->status2;
371 uas->status2 = NULL;
372 }
373 if (p == NULL) {
374 break;
375 }
376
377 length = st->length;
378 if (length > p->iov.size) {
379 qemu_log_mask(LOG_GUEST_ERROR,
380 "usb uas: packet (%zd) too small for status (%d)\n",
381 p->iov.size, length);
382 length = p->iov.size;
383 }
384 usb_packet_copy(p, &st->status, length);
385 QTAILQ_REMOVE(&uas->results, st, next);
386 g_free(st);
387
388 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
389 usb_packet_complete(&uas->dev, p);
390 }
391 }
392
393 static void usb_uas_queue_status(UASDevice *uas, UASStatus *st, int length)
394 {
395 USBPacket *p;
396
397 if (uas_using_streams(uas)) {
398 assert(st->stream <= UAS_MAX_STREAMS);
399 p = uas->status3[st->stream];
400 } else {
401 p = uas->status2;
402 }
403
404 st->length += length;
405 QTAILQ_INSERT_TAIL(&uas->results, st, next);
406 if (p) {
407 /*
408 * Just schedule bh make sure any in-flight data transaction
409 * is finished before completing (sending) the status packet.
410 */
411 qemu_bh_schedule(uas->status_bh);
412 } else {
413 USBEndpoint *ep = usb_ep_get(&uas->dev, USB_TOKEN_IN,
414 UAS_PIPE_ID_STATUS);
415 usb_wakeup(ep, st->stream);
416 }
417 }
418
419 static void usb_uas_queue_response(UASDevice *uas, uint16_t tag, uint8_t code)
420 {
421 UASStatus *st = usb_uas_alloc_status(uas, UAS_UI_RESPONSE, tag);
422
423 trace_usb_uas_response(uas->dev.addr, tag, code);
424 st->status.response.response_code = code;
425 usb_uas_queue_status(uas, st, sizeof(uas_iu_response));
426 }
427
428 static void usb_uas_queue_sense(UASRequest *req, uint8_t status)
429 {
430 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_SENSE, req->tag);
431 int len, slen = 0;
432
433 trace_usb_uas_sense(req->uas->dev.addr, req->tag, status);
434 st->status.sense.status = status;
435 st->status.sense.status_qualifier = cpu_to_be16(0);
436 if (status != GOOD) {
437 slen = scsi_req_get_sense(req->req, st->status.sense.sense_data,
438 sizeof(st->status.sense.sense_data));
439 st->status.sense.sense_length = cpu_to_be16(slen);
440 }
441 len = sizeof(uas_iu_sense) - sizeof(st->status.sense.sense_data) + slen;
442 usb_uas_queue_status(req->uas, st, len);
443 }
444
445 static void usb_uas_queue_fake_sense(UASDevice *uas, uint16_t tag,
446 struct SCSISense sense)
447 {
448 UASStatus *st = usb_uas_alloc_status(uas, UAS_UI_SENSE, tag);
449 int len, slen = 0;
450
451 st->status.sense.status = CHECK_CONDITION;
452 st->status.sense.status_qualifier = cpu_to_be16(0);
453 st->status.sense.sense_data[0] = 0x70;
454 st->status.sense.sense_data[2] = sense.key;
455 st->status.sense.sense_data[7] = 10;
456 st->status.sense.sense_data[12] = sense.asc;
457 st->status.sense.sense_data[13] = sense.ascq;
458 slen = 18;
459 len = sizeof(uas_iu_sense) - sizeof(st->status.sense.sense_data) + slen;
460 usb_uas_queue_status(uas, st, len);
461 }
462
463 static void usb_uas_queue_read_ready(UASRequest *req)
464 {
465 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_READ_READY,
466 req->tag);
467
468 trace_usb_uas_read_ready(req->uas->dev.addr, req->tag);
469 usb_uas_queue_status(req->uas, st, 0);
470 }
471
472 static void usb_uas_queue_write_ready(UASRequest *req)
473 {
474 UASStatus *st = usb_uas_alloc_status(req->uas, UAS_UI_WRITE_READY,
475 req->tag);
476
477 trace_usb_uas_write_ready(req->uas->dev.addr, req->tag);
478 usb_uas_queue_status(req->uas, st, 0);
479 }
480
481 /* --------------------------------------------------------------------- */
482
483 static int usb_uas_get_lun(uint64_t lun64)
484 {
485 return (lun64 >> 48) & 0xff;
486 }
487
488 static SCSIDevice *usb_uas_get_dev(UASDevice *uas, uint64_t lun64)
489 {
490 if ((lun64 >> 56) != 0x00) {
491 return NULL;
492 }
493 return scsi_device_find(&uas->bus, 0, 0, usb_uas_get_lun(lun64));
494 }
495
496 static void usb_uas_complete_data_packet(UASRequest *req)
497 {
498 USBPacket *p;
499
500 if (!req->data_async) {
501 return;
502 }
503 p = req->data;
504 req->data = NULL;
505 req->data_async = false;
506 p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
507 usb_packet_complete(&req->uas->dev, p);
508 }
509
510 static void usb_uas_copy_data(UASRequest *req)
511 {
512 uint32_t length;
513
514 length = MIN(req->buf_size - req->buf_off,
515 req->data->iov.size - req->data->actual_length);
516 trace_usb_uas_xfer_data(req->uas->dev.addr, req->tag, length,
517 req->data->actual_length, req->data->iov.size,
518 req->buf_off, req->buf_size);
519 usb_packet_copy(req->data, scsi_req_get_buf(req->req) + req->buf_off,
520 length);
521 req->buf_off += length;
522 req->data_off += length;
523
524 if (req->data->actual_length == req->data->iov.size) {
525 usb_uas_complete_data_packet(req);
526 }
527 if (req->buf_size && req->buf_off == req->buf_size) {
528 req->buf_off = 0;
529 req->buf_size = 0;
530 scsi_req_continue(req->req);
531 }
532 }
533
534 static void usb_uas_start_next_transfer(UASDevice *uas)
535 {
536 UASRequest *req;
537
538 if (uas_using_streams(uas)) {
539 return;
540 }
541
542 QTAILQ_FOREACH(req, &uas->requests, next) {
543 if (req->active || req->complete) {
544 continue;
545 }
546 if (req->req->cmd.mode == SCSI_XFER_FROM_DEV && uas->datain2 == NULL) {
547 uas->datain2 = req;
548 usb_uas_queue_read_ready(req);
549 req->active = true;
550 return;
551 }
552 if (req->req->cmd.mode == SCSI_XFER_TO_DEV && uas->dataout2 == NULL) {
553 uas->dataout2 = req;
554 usb_uas_queue_write_ready(req);
555 req->active = true;
556 return;
557 }
558 }
559 }
560
561 static UASRequest *usb_uas_alloc_request(UASDevice *uas, uas_iu *iu)
562 {
563 UASRequest *req;
564
565 req = g_new0(UASRequest, 1);
566 req->uas = uas;
567 req->tag = be16_to_cpu(iu->hdr.tag);
568 req->lun = be64_to_cpu(iu->command.lun);
569 req->dev = usb_uas_get_dev(req->uas, req->lun);
570 return req;
571 }
572
573 static void usb_uas_scsi_free_request(SCSIBus *bus, void *priv)
574 {
575 UASRequest *req = priv;
576 UASDevice *uas = req->uas;
577
578 if (req == uas->datain2) {
579 uas->datain2 = NULL;
580 }
581 if (req == uas->dataout2) {
582 uas->dataout2 = NULL;
583 }
584 QTAILQ_REMOVE(&uas->requests, req, next);
585 g_free(req);
586 usb_uas_start_next_transfer(uas);
587 }
588
589 static UASRequest *usb_uas_find_request(UASDevice *uas, uint16_t tag)
590 {
591 UASRequest *req;
592
593 QTAILQ_FOREACH(req, &uas->requests, next) {
594 if (req->tag == tag) {
595 return req;
596 }
597 }
598 return NULL;
599 }
600
601 static void usb_uas_scsi_transfer_data(SCSIRequest *r, uint32_t len)
602 {
603 UASRequest *req = r->hba_private;
604
605 trace_usb_uas_scsi_data(req->uas->dev.addr, req->tag, len);
606 req->buf_off = 0;
607 req->buf_size = len;
608 if (req->data) {
609 usb_uas_copy_data(req);
610 } else {
611 usb_uas_start_next_transfer(req->uas);
612 }
613 }
614
615 static void usb_uas_scsi_command_complete(SCSIRequest *r, size_t resid)
616 {
617 UASRequest *req = r->hba_private;
618
619 trace_usb_uas_scsi_complete(req->uas->dev.addr, req->tag, r->status, resid);
620 req->complete = true;
621 if (req->data) {
622 usb_uas_complete_data_packet(req);
623 }
624 usb_uas_queue_sense(req, r->status);
625 scsi_req_unref(req->req);
626 }
627
628 static void usb_uas_scsi_request_cancelled(SCSIRequest *r)
629 {
630 UASRequest *req = r->hba_private;
631
632 /* FIXME: queue notification to status pipe? */
633 scsi_req_unref(req->req);
634 }
635
636 static const struct SCSIBusInfo usb_uas_scsi_info = {
637 .tcq = true,
638 .max_target = 0,
639 .max_lun = 255,
640
641 .transfer_data = usb_uas_scsi_transfer_data,
642 .complete = usb_uas_scsi_command_complete,
643 .cancel = usb_uas_scsi_request_cancelled,
644 .free_request = usb_uas_scsi_free_request,
645 };
646
647 /* --------------------------------------------------------------------- */
648
649 static void usb_uas_handle_reset(USBDevice *dev)
650 {
651 UASDevice *uas = USB_UAS(dev);
652 UASRequest *req, *nreq;
653 UASStatus *st, *nst;
654
655 trace_usb_uas_reset(dev->addr);
656 QTAILQ_FOREACH_SAFE(req, &uas->requests, next, nreq) {
657 scsi_req_cancel(req->req);
658 }
659 QTAILQ_FOREACH_SAFE(st, &uas->results, next, nst) {
660 QTAILQ_REMOVE(&uas->results, st, next);
661 g_free(st);
662 }
663 }
664
665 static void usb_uas_handle_control(USBDevice *dev, USBPacket *p,
666 int request, int value, int index, int length, uint8_t *data)
667 {
668 int ret;
669
670 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
671 if (ret >= 0) {
672 return;
673 }
674 error_report("%s: unhandled control request (req 0x%x, val 0x%x, idx 0x%x",
675 __func__, request, value, index);
676 p->status = USB_RET_STALL;
677 }
678
679 static void usb_uas_cancel_io(USBDevice *dev, USBPacket *p)
680 {
681 UASDevice *uas = USB_UAS(dev);
682 UASRequest *req, *nreq;
683 int i;
684
685 if (uas->status2 == p) {
686 uas->status2 = NULL;
687 qemu_bh_cancel(uas->status_bh);
688 return;
689 }
690 if (uas_using_streams(uas)) {
691 for (i = 0; i <= UAS_MAX_STREAMS; i++) {
692 if (uas->status3[i] == p) {
693 uas->status3[i] = NULL;
694 return;
695 }
696 if (uas->data3[i] == p) {
697 uas->data3[i] = NULL;
698 return;
699 }
700 }
701 }
702 QTAILQ_FOREACH_SAFE(req, &uas->requests, next, nreq) {
703 if (req->data == p) {
704 req->data = NULL;
705 return;
706 }
707 }
708 assert(!"canceled usb packet not found");
709 }
710
711 static void usb_uas_command(UASDevice *uas, uas_iu *iu)
712 {
713 UASRequest *req;
714 uint32_t len;
715 uint16_t tag = be16_to_cpu(iu->hdr.tag);
716 size_t cdb_len = sizeof(iu->command.cdb) + iu->command.add_cdb_length;
717
718 if (uas_using_streams(uas) && tag > UAS_MAX_STREAMS) {
719 /*
720 * Our status delivery only works with valid tags, so in case the
721 * stream ID is out of bounds, we have to return immediately here
722 * without sending a fake sense_code_INVALID_TAG to the guest.
723 */
724 qemu_log_mask(LOG_GUEST_ERROR,
725 "invalid tag 0x%x for USB UAS command\n", tag);
726 return;
727 }
728
729 if (iu->command.add_cdb_length > 0) {
730 qemu_log_mask(LOG_UNIMP, "additional adb length not yet supported\n");
731 goto unsupported_len;
732 }
733
734 req = usb_uas_find_request(uas, tag);
735 if (req) {
736 goto overlapped_tag;
737 }
738 req = usb_uas_alloc_request(uas, iu);
739 if (req->dev == NULL) {
740 goto bad_target;
741 }
742
743 trace_usb_uas_command(uas->dev.addr, req->tag,
744 usb_uas_get_lun(req->lun),
745 req->lun >> 32, req->lun & 0xffffffff);
746 QTAILQ_INSERT_TAIL(&uas->requests, req, next);
747 if (uas_using_streams(uas) && uas->data3[req->tag] != NULL) {
748 req->data = uas->data3[req->tag];
749 req->data_async = true;
750 uas->data3[req->tag] = NULL;
751 }
752
753 req->req = scsi_req_new(req->dev, req->tag,
754 usb_uas_get_lun(req->lun),
755 iu->command.cdb, cdb_len, req);
756 if (uas->requestlog) {
757 scsi_req_print(req->req);
758 }
759 len = scsi_req_enqueue(req->req);
760 if (len) {
761 req->data_size = len;
762 scsi_req_continue(req->req);
763 }
764 return;
765
766 unsupported_len:
767 usb_uas_queue_fake_sense(uas, tag, sense_code_INVALID_PARAM_VALUE);
768 return;
769
770 overlapped_tag:
771 usb_uas_queue_fake_sense(uas, tag, sense_code_OVERLAPPED_COMMANDS);
772 return;
773
774 bad_target:
775 usb_uas_queue_fake_sense(uas, tag, sense_code_LUN_NOT_SUPPORTED);
776 g_free(req);
777 }
778
779 static void usb_uas_task(UASDevice *uas, uas_iu *iu)
780 {
781 uint16_t tag = be16_to_cpu(iu->hdr.tag);
782 uint64_t lun64 = be64_to_cpu(iu->task.lun);
783 SCSIDevice *dev = usb_uas_get_dev(uas, lun64);
784 int lun = usb_uas_get_lun(lun64);
785 UASRequest *req;
786 uint16_t task_tag;
787
788 if (uas_using_streams(uas) && tag > UAS_MAX_STREAMS) {
789 goto invalid_tag;
790 }
791 req = usb_uas_find_request(uas, be16_to_cpu(iu->hdr.tag));
792 if (req) {
793 goto overlapped_tag;
794 }
795 if (dev == NULL) {
796 goto incorrect_lun;
797 }
798
799 switch (iu->task.function) {
800 case UAS_TMF_ABORT_TASK:
801 task_tag = be16_to_cpu(iu->task.task_tag);
802 trace_usb_uas_tmf_abort_task(uas->dev.addr, tag, task_tag);
803 req = usb_uas_find_request(uas, task_tag);
804 if (req && req->dev == dev) {
805 scsi_req_cancel(req->req);
806 }
807 usb_uas_queue_response(uas, tag, UAS_RC_TMF_COMPLETE);
808 break;
809
810 case UAS_TMF_LOGICAL_UNIT_RESET:
811 trace_usb_uas_tmf_logical_unit_reset(uas->dev.addr, tag, lun);
812 device_cold_reset(&dev->qdev);
813 usb_uas_queue_response(uas, tag, UAS_RC_TMF_COMPLETE);
814 break;
815
816 default:
817 trace_usb_uas_tmf_unsupported(uas->dev.addr, tag, iu->task.function);
818 usb_uas_queue_response(uas, tag, UAS_RC_TMF_NOT_SUPPORTED);
819 break;
820 }
821 return;
822
823 invalid_tag:
824 usb_uas_queue_response(uas, tag, UAS_RC_INVALID_INFO_UNIT);
825 return;
826
827 overlapped_tag:
828 usb_uas_queue_response(uas, req->tag, UAS_RC_OVERLAPPED_TAG);
829 return;
830
831 incorrect_lun:
832 usb_uas_queue_response(uas, tag, UAS_RC_INCORRECT_LUN);
833 }
834
835 static void usb_uas_handle_data(USBDevice *dev, USBPacket *p)
836 {
837 UASDevice *uas = USB_UAS(dev);
838 uas_iu iu;
839 UASStatus *st;
840 UASRequest *req;
841 int length;
842
843 switch (p->ep->nr) {
844 case UAS_PIPE_ID_COMMAND:
845 length = MIN(sizeof(iu), p->iov.size);
846 usb_packet_copy(p, &iu, length);
847 switch (iu.hdr.id) {
848 case UAS_UI_COMMAND:
849 usb_uas_command(uas, &iu);
850 break;
851 case UAS_UI_TASK_MGMT:
852 usb_uas_task(uas, &iu);
853 break;
854 default:
855 error_report("%s: unknown command iu: id 0x%x",
856 __func__, iu.hdr.id);
857 p->status = USB_RET_STALL;
858 break;
859 }
860 break;
861 case UAS_PIPE_ID_STATUS:
862 if (p->stream > UAS_MAX_STREAMS) {
863 goto err_stream;
864 }
865 if (p->stream) {
866 QTAILQ_FOREACH(st, &uas->results, next) {
867 if (st->stream == p->stream) {
868 break;
869 }
870 }
871 if (st == NULL) {
872 assert(uas->status3[p->stream] == NULL);
873 uas->status3[p->stream] = p;
874 p->status = USB_RET_ASYNC;
875 break;
876 }
877 } else {
878 st = QTAILQ_FIRST(&uas->results);
879 if (st == NULL) {
880 assert(uas->status2 == NULL);
881 uas->status2 = p;
882 p->status = USB_RET_ASYNC;
883 break;
884 }
885 }
886 length = st->length;
887 if (length > p->iov.size) {
888 qemu_log_mask(LOG_GUEST_ERROR,
889 "usb uas: packet (%zd) too small for status (%d)\n",
890 p->iov.size, length);
891 length = p->iov.size;
892 }
893 usb_packet_copy(p, &st->status, length);
894 QTAILQ_REMOVE(&uas->results, st, next);
895 g_free(st);
896 break;
897 case UAS_PIPE_ID_DATA_IN:
898 case UAS_PIPE_ID_DATA_OUT:
899 if (p->stream > UAS_MAX_STREAMS) {
900 goto err_stream;
901 }
902 if (p->stream) {
903 req = usb_uas_find_request(uas, p->stream);
904 } else {
905 req = (p->ep->nr == UAS_PIPE_ID_DATA_IN)
906 ? uas->datain2 : uas->dataout2;
907 }
908 if (req == NULL) {
909 if (p->stream) {
910 assert(uas->data3[p->stream] == NULL);
911 uas->data3[p->stream] = p;
912 p->status = USB_RET_ASYNC;
913 break;
914 } else {
915 error_report("%s: no inflight request", __func__);
916 p->status = USB_RET_STALL;
917 break;
918 }
919 }
920 scsi_req_ref(req->req);
921 req->data = p;
922 usb_uas_copy_data(req);
923 if (p->actual_length == p->iov.size || req->complete) {
924 req->data = NULL;
925 } else {
926 req->data_async = true;
927 p->status = USB_RET_ASYNC;
928 }
929 scsi_req_unref(req->req);
930 usb_uas_start_next_transfer(uas);
931 break;
932 default:
933 error_report("%s: invalid endpoint %d", __func__, p->ep->nr);
934 p->status = USB_RET_STALL;
935 break;
936 }
937 return;
938
939 err_stream:
940 error_report("%s: invalid stream %d", __func__, p->stream);
941 p->status = USB_RET_STALL;
942 }
943
944 static void usb_uas_unrealize(USBDevice *dev)
945 {
946 UASDevice *uas = USB_UAS(dev);
947
948 qemu_bh_delete(uas->status_bh);
949 }
950
951 static void usb_uas_realize(USBDevice *dev, Error **errp)
952 {
953 UASDevice *uas = USB_UAS(dev);
954 DeviceState *d = DEVICE(dev);
955
956 usb_desc_create_serial(dev);
957 usb_desc_init(dev);
958 if (d->hotplugged) {
959 uas->dev.auto_attach = 0;
960 }
961
962 QTAILQ_INIT(&uas->results);
963 QTAILQ_INIT(&uas->requests);
964 uas->status_bh = qemu_bh_new_guarded(usb_uas_send_status_bh, uas,
965 &d->mem_reentrancy_guard);
966
967 dev->flags |= (1 << USB_DEV_FLAG_IS_SCSI_STORAGE);
968 scsi_bus_init(&uas->bus, sizeof(uas->bus), DEVICE(dev), &usb_uas_scsi_info);
969 }
970
971 static const VMStateDescription vmstate_usb_uas = {
972 .name = "usb-uas",
973 .unmigratable = 1,
974 .fields = (const VMStateField[]) {
975 VMSTATE_USB_DEVICE(dev, UASDevice),
976 VMSTATE_END_OF_LIST()
977 }
978 };
979
980 static const Property uas_properties[] = {
981 DEFINE_PROP_UINT32("log-scsi-req", UASDevice, requestlog, 0),
982 };
983
984 static void usb_uas_class_initfn(ObjectClass *klass, const void *data)
985 {
986 DeviceClass *dc = DEVICE_CLASS(klass);
987 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
988
989 uc->realize = usb_uas_realize;
990 uc->product_desc = desc_strings[STR_PRODUCT];
991 uc->usb_desc = &desc;
992 uc->cancel_packet = usb_uas_cancel_io;
993 uc->handle_attach = usb_desc_attach;
994 uc->handle_reset = usb_uas_handle_reset;
995 uc->handle_control = usb_uas_handle_control;
996 uc->handle_data = usb_uas_handle_data;
997 uc->unrealize = usb_uas_unrealize;
998 uc->attached_settable = true;
999 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1000 dc->fw_name = "storage";
1001 dc->vmsd = &vmstate_usb_uas;
1002 device_class_set_props(dc, uas_properties);
1003 }
1004
1005 static const TypeInfo uas_info = {
1006 .name = TYPE_USB_UAS,
1007 .parent = TYPE_USB_DEVICE,
1008 .instance_size = sizeof(UASDevice),
1009 .class_init = usb_uas_class_initfn,
1010 };
1011
1012 static void usb_uas_register_types(void)
1013 {
1014 type_register_static(&uas_info);
1015 }
1016
1017 type_init(usb_uas_register_types)