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