2
0

dev-uas.c 29 KB

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