uas.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * USB Attached SCSI
  3. * Note that this is not the same as the USB Mass Storage driver
  4. *
  5. * Copyright Matthew Wilcox for Intel Corp, 2010
  6. * Copyright Sarah Sharp for Intel Corp, 2010
  7. *
  8. * Distributed under the terms of the GNU GPL, version two.
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include <linux/usb/hcd.h>
  16. #include <linux/usb/storage.h>
  17. #include <linux/usb/uas.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_dbg.h>
  20. #include <scsi/scsi_cmnd.h>
  21. #include <scsi/scsi_device.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi_tcq.h>
  24. /*
  25. * The r00-r01c specs define this version of the SENSE IU data structure.
  26. * It's still in use by several different firmware releases.
  27. */
  28. struct sense_iu_old {
  29. __u8 iu_id;
  30. __u8 rsvd1;
  31. __be16 tag;
  32. __be16 len;
  33. __u8 status;
  34. __u8 service_response;
  35. __u8 sense[SCSI_SENSE_BUFFERSIZE];
  36. };
  37. struct uas_dev_info {
  38. struct usb_interface *intf;
  39. struct usb_device *udev;
  40. struct usb_anchor sense_urbs;
  41. struct usb_anchor data_urbs;
  42. int qdepth;
  43. unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
  44. unsigned use_streams:1;
  45. unsigned uas_sense_old:1;
  46. struct scsi_cmnd *cmnd;
  47. };
  48. enum {
  49. SUBMIT_STATUS_URB = (1 << 1),
  50. ALLOC_DATA_IN_URB = (1 << 2),
  51. SUBMIT_DATA_IN_URB = (1 << 3),
  52. ALLOC_DATA_OUT_URB = (1 << 4),
  53. SUBMIT_DATA_OUT_URB = (1 << 5),
  54. ALLOC_CMD_URB = (1 << 6),
  55. SUBMIT_CMD_URB = (1 << 7),
  56. COMMAND_INFLIGHT = (1 << 8),
  57. DATA_IN_URB_INFLIGHT = (1 << 9),
  58. DATA_OUT_URB_INFLIGHT = (1 << 10),
  59. COMMAND_COMPLETED = (1 << 11),
  60. };
  61. /* Overrides scsi_pointer */
  62. struct uas_cmd_info {
  63. unsigned int state;
  64. unsigned int stream;
  65. struct urb *cmd_urb;
  66. struct urb *data_in_urb;
  67. struct urb *data_out_urb;
  68. struct list_head list;
  69. };
  70. /* I hate forward declarations, but I actually have a loop */
  71. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  72. struct uas_dev_info *devinfo, gfp_t gfp);
  73. static void uas_do_work(struct work_struct *work);
  74. static DECLARE_WORK(uas_work, uas_do_work);
  75. static DEFINE_SPINLOCK(uas_work_lock);
  76. static LIST_HEAD(uas_work_list);
  77. static void uas_do_work(struct work_struct *work)
  78. {
  79. struct uas_cmd_info *cmdinfo;
  80. struct uas_cmd_info *temp;
  81. struct list_head list;
  82. int err;
  83. spin_lock_irq(&uas_work_lock);
  84. list_replace_init(&uas_work_list, &list);
  85. spin_unlock_irq(&uas_work_lock);
  86. list_for_each_entry_safe(cmdinfo, temp, &list, list) {
  87. struct scsi_pointer *scp = (void *)cmdinfo;
  88. struct scsi_cmnd *cmnd = container_of(scp,
  89. struct scsi_cmnd, SCp);
  90. err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
  91. if (err) {
  92. list_del(&cmdinfo->list);
  93. spin_lock_irq(&uas_work_lock);
  94. list_add_tail(&cmdinfo->list, &uas_work_list);
  95. spin_unlock_irq(&uas_work_lock);
  96. schedule_work(&uas_work);
  97. }
  98. }
  99. }
  100. static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
  101. {
  102. struct sense_iu *sense_iu = urb->transfer_buffer;
  103. struct scsi_device *sdev = cmnd->device;
  104. if (urb->actual_length > 16) {
  105. unsigned len = be16_to_cpup(&sense_iu->len);
  106. if (len + 16 != urb->actual_length) {
  107. int newlen = min(len + 16, urb->actual_length) - 16;
  108. if (newlen < 0)
  109. newlen = 0;
  110. sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
  111. "disagrees with IU sense data length %d, "
  112. "using %d bytes of sense data\n", __func__,
  113. urb->actual_length, len, newlen);
  114. len = newlen;
  115. }
  116. memcpy(cmnd->sense_buffer, sense_iu->sense, len);
  117. }
  118. cmnd->result = sense_iu->status;
  119. }
  120. static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
  121. {
  122. struct sense_iu_old *sense_iu = urb->transfer_buffer;
  123. struct scsi_device *sdev = cmnd->device;
  124. if (urb->actual_length > 8) {
  125. unsigned len = be16_to_cpup(&sense_iu->len) - 2;
  126. if (len + 8 != urb->actual_length) {
  127. int newlen = min(len + 8, urb->actual_length) - 8;
  128. if (newlen < 0)
  129. newlen = 0;
  130. sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
  131. "disagrees with IU sense data length %d, "
  132. "using %d bytes of sense data\n", __func__,
  133. urb->actual_length, len, newlen);
  134. len = newlen;
  135. }
  136. memcpy(cmnd->sense_buffer, sense_iu->sense, len);
  137. }
  138. cmnd->result = sense_iu->status;
  139. }
  140. static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
  141. {
  142. struct uas_cmd_info *ci = (void *)&cmnd->SCp;
  143. scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
  144. "%s%s%s%s%s%s%s%s%s%s%s\n",
  145. caller, cmnd, cmnd->request->tag,
  146. (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
  147. (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
  148. (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
  149. (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
  150. (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
  151. (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
  152. (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
  153. (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
  154. (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
  155. (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
  156. (ci->state & COMMAND_COMPLETED) ? " done" : "");
  157. }
  158. static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
  159. {
  160. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  161. if (cmdinfo->state & (COMMAND_INFLIGHT |
  162. DATA_IN_URB_INFLIGHT |
  163. DATA_OUT_URB_INFLIGHT))
  164. return -EBUSY;
  165. BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
  166. cmdinfo->state |= COMMAND_COMPLETED;
  167. usb_free_urb(cmdinfo->data_in_urb);
  168. usb_free_urb(cmdinfo->data_out_urb);
  169. cmnd->scsi_done(cmnd);
  170. return 0;
  171. }
  172. static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
  173. unsigned direction)
  174. {
  175. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  176. int err;
  177. cmdinfo->state |= direction | SUBMIT_STATUS_URB;
  178. err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
  179. if (err) {
  180. spin_lock(&uas_work_lock);
  181. list_add_tail(&cmdinfo->list, &uas_work_list);
  182. spin_unlock(&uas_work_lock);
  183. schedule_work(&uas_work);
  184. }
  185. }
  186. static void uas_stat_cmplt(struct urb *urb)
  187. {
  188. struct iu *iu = urb->transfer_buffer;
  189. struct Scsi_Host *shost = urb->context;
  190. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  191. struct scsi_cmnd *cmnd;
  192. struct uas_cmd_info *cmdinfo;
  193. u16 tag;
  194. if (urb->status) {
  195. dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
  196. usb_free_urb(urb);
  197. return;
  198. }
  199. tag = be16_to_cpup(&iu->tag) - 1;
  200. if (tag == 0)
  201. cmnd = devinfo->cmnd;
  202. else
  203. cmnd = scsi_host_find_tag(shost, tag - 1);
  204. if (!cmnd) {
  205. usb_free_urb(urb);
  206. return;
  207. }
  208. cmdinfo = (void *)&cmnd->SCp;
  209. switch (iu->iu_id) {
  210. case IU_ID_STATUS:
  211. if (devinfo->cmnd == cmnd)
  212. devinfo->cmnd = NULL;
  213. if (urb->actual_length < 16)
  214. devinfo->uas_sense_old = 1;
  215. if (devinfo->uas_sense_old)
  216. uas_sense_old(urb, cmnd);
  217. else
  218. uas_sense(urb, cmnd);
  219. if (cmnd->result != 0) {
  220. /* cancel data transfers on error */
  221. if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
  222. usb_unlink_urb(cmdinfo->data_in_urb);
  223. if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
  224. usb_unlink_urb(cmdinfo->data_out_urb);
  225. }
  226. cmdinfo->state &= ~COMMAND_INFLIGHT;
  227. uas_try_complete(cmnd, __func__);
  228. break;
  229. case IU_ID_READ_READY:
  230. uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
  231. break;
  232. case IU_ID_WRITE_READY:
  233. uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
  234. break;
  235. default:
  236. scmd_printk(KERN_ERR, cmnd,
  237. "Bogus IU (%d) received on status pipe\n", iu->iu_id);
  238. }
  239. usb_free_urb(urb);
  240. }
  241. static void uas_data_cmplt(struct urb *urb)
  242. {
  243. struct scsi_cmnd *cmnd = urb->context;
  244. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  245. struct scsi_data_buffer *sdb = NULL;
  246. if (cmdinfo->data_in_urb == urb) {
  247. sdb = scsi_in(cmnd);
  248. cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
  249. } else if (cmdinfo->data_out_urb == urb) {
  250. sdb = scsi_out(cmnd);
  251. cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
  252. }
  253. BUG_ON(sdb == NULL);
  254. if (urb->status) {
  255. /* error: no data transfered */
  256. sdb->resid = sdb->length;
  257. } else {
  258. sdb->resid = sdb->length - urb->actual_length;
  259. }
  260. uas_try_complete(cmnd, __func__);
  261. }
  262. static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  263. unsigned int pipe, u16 stream_id,
  264. struct scsi_cmnd *cmnd,
  265. enum dma_data_direction dir)
  266. {
  267. struct usb_device *udev = devinfo->udev;
  268. struct urb *urb = usb_alloc_urb(0, gfp);
  269. struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
  270. ? scsi_in(cmnd) : scsi_out(cmnd);
  271. if (!urb)
  272. goto out;
  273. usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
  274. uas_data_cmplt, cmnd);
  275. if (devinfo->use_streams)
  276. urb->stream_id = stream_id;
  277. urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
  278. urb->sg = sdb->table.sgl;
  279. out:
  280. return urb;
  281. }
  282. static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  283. struct Scsi_Host *shost, u16 stream_id)
  284. {
  285. struct usb_device *udev = devinfo->udev;
  286. struct urb *urb = usb_alloc_urb(0, gfp);
  287. struct sense_iu *iu;
  288. if (!urb)
  289. goto out;
  290. iu = kzalloc(sizeof(*iu), gfp);
  291. if (!iu)
  292. goto free;
  293. usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
  294. uas_stat_cmplt, shost);
  295. urb->stream_id = stream_id;
  296. urb->transfer_flags |= URB_FREE_BUFFER;
  297. out:
  298. return urb;
  299. free:
  300. usb_free_urb(urb);
  301. return NULL;
  302. }
  303. static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  304. struct scsi_cmnd *cmnd, u16 stream_id)
  305. {
  306. struct usb_device *udev = devinfo->udev;
  307. struct scsi_device *sdev = cmnd->device;
  308. struct urb *urb = usb_alloc_urb(0, gfp);
  309. struct command_iu *iu;
  310. int len;
  311. if (!urb)
  312. goto out;
  313. len = cmnd->cmd_len - 16;
  314. if (len < 0)
  315. len = 0;
  316. len = ALIGN(len, 4);
  317. iu = kzalloc(sizeof(*iu) + len, gfp);
  318. if (!iu)
  319. goto free;
  320. iu->iu_id = IU_ID_COMMAND;
  321. if (blk_rq_tagged(cmnd->request))
  322. iu->tag = cpu_to_be16(cmnd->request->tag + 2);
  323. else
  324. iu->tag = cpu_to_be16(1);
  325. iu->prio_attr = UAS_SIMPLE_TAG;
  326. iu->len = len;
  327. int_to_scsilun(sdev->lun, &iu->lun);
  328. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  329. usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
  330. usb_free_urb, NULL);
  331. urb->transfer_flags |= URB_FREE_BUFFER;
  332. out:
  333. return urb;
  334. free:
  335. usb_free_urb(urb);
  336. return NULL;
  337. }
  338. /*
  339. * Why should I request the Status IU before sending the Command IU? Spec
  340. * says to, but also says the device may receive them in any order. Seems
  341. * daft to me.
  342. */
  343. static int uas_submit_sense_urb(struct Scsi_Host *shost,
  344. gfp_t gfp, unsigned int stream)
  345. {
  346. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  347. struct urb *urb;
  348. urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
  349. if (!urb)
  350. return SCSI_MLQUEUE_DEVICE_BUSY;
  351. if (usb_submit_urb(urb, gfp)) {
  352. shost_printk(KERN_INFO, shost,
  353. "sense urb submission failure\n");
  354. usb_free_urb(urb);
  355. return SCSI_MLQUEUE_DEVICE_BUSY;
  356. }
  357. usb_anchor_urb(urb, &devinfo->sense_urbs);
  358. return 0;
  359. }
  360. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  361. struct uas_dev_info *devinfo, gfp_t gfp)
  362. {
  363. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  364. int err;
  365. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  366. err = uas_submit_sense_urb(cmnd->device->host, gfp,
  367. cmdinfo->stream);
  368. if (err) {
  369. return err;
  370. }
  371. cmdinfo->state &= ~SUBMIT_STATUS_URB;
  372. }
  373. if (cmdinfo->state & ALLOC_DATA_IN_URB) {
  374. cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
  375. devinfo->data_in_pipe, cmdinfo->stream,
  376. cmnd, DMA_FROM_DEVICE);
  377. if (!cmdinfo->data_in_urb)
  378. return SCSI_MLQUEUE_DEVICE_BUSY;
  379. cmdinfo->state &= ~ALLOC_DATA_IN_URB;
  380. }
  381. if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
  382. if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
  383. scmd_printk(KERN_INFO, cmnd,
  384. "data in urb submission failure\n");
  385. return SCSI_MLQUEUE_DEVICE_BUSY;
  386. }
  387. cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
  388. cmdinfo->state |= DATA_IN_URB_INFLIGHT;
  389. usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
  390. }
  391. if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
  392. cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
  393. devinfo->data_out_pipe, cmdinfo->stream,
  394. cmnd, DMA_TO_DEVICE);
  395. if (!cmdinfo->data_out_urb)
  396. return SCSI_MLQUEUE_DEVICE_BUSY;
  397. cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
  398. }
  399. if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
  400. if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
  401. scmd_printk(KERN_INFO, cmnd,
  402. "data out urb submission failure\n");
  403. return SCSI_MLQUEUE_DEVICE_BUSY;
  404. }
  405. cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
  406. cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
  407. usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
  408. }
  409. if (cmdinfo->state & ALLOC_CMD_URB) {
  410. cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
  411. cmdinfo->stream);
  412. if (!cmdinfo->cmd_urb)
  413. return SCSI_MLQUEUE_DEVICE_BUSY;
  414. cmdinfo->state &= ~ALLOC_CMD_URB;
  415. }
  416. if (cmdinfo->state & SUBMIT_CMD_URB) {
  417. if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
  418. scmd_printk(KERN_INFO, cmnd,
  419. "cmd urb submission failure\n");
  420. return SCSI_MLQUEUE_DEVICE_BUSY;
  421. }
  422. cmdinfo->state &= ~SUBMIT_CMD_URB;
  423. cmdinfo->state |= COMMAND_INFLIGHT;
  424. }
  425. return 0;
  426. }
  427. static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
  428. void (*done)(struct scsi_cmnd *))
  429. {
  430. struct scsi_device *sdev = cmnd->device;
  431. struct uas_dev_info *devinfo = sdev->hostdata;
  432. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  433. int err;
  434. BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
  435. if (devinfo->cmnd)
  436. return SCSI_MLQUEUE_DEVICE_BUSY;
  437. if (blk_rq_tagged(cmnd->request)) {
  438. cmdinfo->stream = cmnd->request->tag + 2;
  439. } else {
  440. devinfo->cmnd = cmnd;
  441. cmdinfo->stream = 1;
  442. }
  443. cmnd->scsi_done = done;
  444. cmdinfo->state = SUBMIT_STATUS_URB |
  445. ALLOC_CMD_URB | SUBMIT_CMD_URB;
  446. switch (cmnd->sc_data_direction) {
  447. case DMA_FROM_DEVICE:
  448. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  449. break;
  450. case DMA_BIDIRECTIONAL:
  451. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  452. case DMA_TO_DEVICE:
  453. cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
  454. case DMA_NONE:
  455. break;
  456. }
  457. if (!devinfo->use_streams) {
  458. cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
  459. cmdinfo->stream = 0;
  460. }
  461. err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
  462. if (err) {
  463. /* If we did nothing, give up now */
  464. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  465. return SCSI_MLQUEUE_DEVICE_BUSY;
  466. }
  467. spin_lock(&uas_work_lock);
  468. list_add_tail(&cmdinfo->list, &uas_work_list);
  469. spin_unlock(&uas_work_lock);
  470. schedule_work(&uas_work);
  471. }
  472. return 0;
  473. }
  474. static DEF_SCSI_QCMD(uas_queuecommand)
  475. static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
  476. {
  477. uas_log_cmd_state(cmnd, __func__);
  478. /* XXX: Send ABORT TASK Task Management command */
  479. return FAILED;
  480. }
  481. static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
  482. {
  483. struct scsi_device *sdev = cmnd->device;
  484. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  485. cmnd->request->tag);
  486. /* XXX: Send LOGICAL UNIT RESET Task Management command */
  487. return FAILED;
  488. }
  489. static int uas_eh_target_reset_handler(struct scsi_cmnd *cmnd)
  490. {
  491. struct scsi_device *sdev = cmnd->device;
  492. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  493. cmnd->request->tag);
  494. /* XXX: Can we reset just the one USB interface?
  495. * Would calling usb_set_interface() have the right effect?
  496. */
  497. return FAILED;
  498. }
  499. static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
  500. {
  501. struct scsi_device *sdev = cmnd->device;
  502. struct uas_dev_info *devinfo = sdev->hostdata;
  503. struct usb_device *udev = devinfo->udev;
  504. sdev_printk(KERN_INFO, sdev, "%s tag %d\n", __func__,
  505. cmnd->request->tag);
  506. if (usb_reset_device(udev))
  507. return SUCCESS;
  508. return FAILED;
  509. }
  510. static int uas_slave_alloc(struct scsi_device *sdev)
  511. {
  512. sdev->hostdata = (void *)sdev->host->hostdata[0];
  513. return 0;
  514. }
  515. static int uas_slave_configure(struct scsi_device *sdev)
  516. {
  517. struct uas_dev_info *devinfo = sdev->hostdata;
  518. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  519. scsi_activate_tcq(sdev, devinfo->qdepth - 2);
  520. return 0;
  521. }
  522. static struct scsi_host_template uas_host_template = {
  523. .module = THIS_MODULE,
  524. .name = "uas",
  525. .queuecommand = uas_queuecommand,
  526. .slave_alloc = uas_slave_alloc,
  527. .slave_configure = uas_slave_configure,
  528. .eh_abort_handler = uas_eh_abort_handler,
  529. .eh_device_reset_handler = uas_eh_device_reset_handler,
  530. .eh_target_reset_handler = uas_eh_target_reset_handler,
  531. .eh_bus_reset_handler = uas_eh_bus_reset_handler,
  532. .can_queue = 65536, /* Is there a limit on the _host_ ? */
  533. .this_id = -1,
  534. .sg_tablesize = SG_NONE,
  535. .cmd_per_lun = 1, /* until we override it */
  536. .skip_settle_delay = 1,
  537. .ordered_tag = 1,
  538. };
  539. static struct usb_device_id uas_usb_ids[] = {
  540. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
  541. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
  542. /* 0xaa is a prototype device I happen to have access to */
  543. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
  544. { }
  545. };
  546. MODULE_DEVICE_TABLE(usb, uas_usb_ids);
  547. static int uas_is_interface(struct usb_host_interface *intf)
  548. {
  549. return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
  550. intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
  551. intf->desc.bInterfaceProtocol == USB_PR_UAS);
  552. }
  553. static int uas_isnt_supported(struct usb_device *udev)
  554. {
  555. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  556. dev_warn(&udev->dev, "The driver for the USB controller %s does not "
  557. "support scatter-gather which is\n",
  558. hcd->driver->description);
  559. dev_warn(&udev->dev, "required by the UAS driver. Please try an"
  560. "alternative USB controller if you wish to use UAS.\n");
  561. return -ENODEV;
  562. }
  563. static int uas_switch_interface(struct usb_device *udev,
  564. struct usb_interface *intf)
  565. {
  566. int i;
  567. int sg_supported = udev->bus->sg_tablesize != 0;
  568. for (i = 0; i < intf->num_altsetting; i++) {
  569. struct usb_host_interface *alt = &intf->altsetting[i];
  570. if (uas_is_interface(alt)) {
  571. if (!sg_supported)
  572. return uas_isnt_supported(udev);
  573. return usb_set_interface(udev,
  574. alt->desc.bInterfaceNumber,
  575. alt->desc.bAlternateSetting);
  576. }
  577. }
  578. return -ENODEV;
  579. }
  580. static void uas_configure_endpoints(struct uas_dev_info *devinfo)
  581. {
  582. struct usb_host_endpoint *eps[4] = { };
  583. struct usb_interface *intf = devinfo->intf;
  584. struct usb_device *udev = devinfo->udev;
  585. struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
  586. unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
  587. devinfo->uas_sense_old = 0;
  588. devinfo->cmnd = NULL;
  589. for (i = 0; i < n_endpoints; i++) {
  590. unsigned char *extra = endpoint[i].extra;
  591. int len = endpoint[i].extralen;
  592. while (len > 1) {
  593. if (extra[1] == USB_DT_PIPE_USAGE) {
  594. unsigned pipe_id = extra[2];
  595. if (pipe_id > 0 && pipe_id < 5)
  596. eps[pipe_id - 1] = &endpoint[i];
  597. break;
  598. }
  599. len -= extra[0];
  600. extra += extra[0];
  601. }
  602. }
  603. /*
  604. * Assume that if we didn't find a control pipe descriptor, we're
  605. * using a device with old firmware that happens to be set up like
  606. * this.
  607. */
  608. if (!eps[0]) {
  609. devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
  610. devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
  611. devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
  612. devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
  613. eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  614. eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  615. eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  616. } else {
  617. devinfo->cmd_pipe = usb_sndbulkpipe(udev,
  618. eps[0]->desc.bEndpointAddress);
  619. devinfo->status_pipe = usb_rcvbulkpipe(udev,
  620. eps[1]->desc.bEndpointAddress);
  621. devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
  622. eps[2]->desc.bEndpointAddress);
  623. devinfo->data_out_pipe = usb_sndbulkpipe(udev,
  624. eps[3]->desc.bEndpointAddress);
  625. }
  626. devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
  627. GFP_KERNEL);
  628. if (devinfo->qdepth < 0) {
  629. devinfo->qdepth = 256;
  630. devinfo->use_streams = 0;
  631. } else {
  632. devinfo->use_streams = 1;
  633. }
  634. }
  635. static void uas_free_streams(struct uas_dev_info *devinfo)
  636. {
  637. struct usb_device *udev = devinfo->udev;
  638. struct usb_host_endpoint *eps[3];
  639. eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  640. eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  641. eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  642. usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
  643. }
  644. /*
  645. * XXX: What I'd like to do here is register a SCSI host for each USB host in
  646. * the system. Follow usb-storage's design of registering a SCSI host for
  647. * each USB device for the moment. Can implement this by walking up the
  648. * USB hierarchy until we find a USB host.
  649. */
  650. static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
  651. {
  652. int result;
  653. struct Scsi_Host *shost;
  654. struct uas_dev_info *devinfo;
  655. struct usb_device *udev = interface_to_usbdev(intf);
  656. if (uas_switch_interface(udev, intf))
  657. return -ENODEV;
  658. devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
  659. if (!devinfo)
  660. return -ENOMEM;
  661. result = -ENOMEM;
  662. shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
  663. if (!shost)
  664. goto free;
  665. shost->max_cmd_len = 16 + 252;
  666. shost->max_id = 1;
  667. shost->sg_tablesize = udev->bus->sg_tablesize;
  668. devinfo->intf = intf;
  669. devinfo->udev = udev;
  670. init_usb_anchor(&devinfo->sense_urbs);
  671. init_usb_anchor(&devinfo->data_urbs);
  672. uas_configure_endpoints(devinfo);
  673. result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
  674. if (result)
  675. goto free;
  676. result = scsi_add_host(shost, &intf->dev);
  677. if (result)
  678. goto deconfig_eps;
  679. shost->hostdata[0] = (unsigned long)devinfo;
  680. scsi_scan_host(shost);
  681. usb_set_intfdata(intf, shost);
  682. return result;
  683. deconfig_eps:
  684. uas_free_streams(devinfo);
  685. free:
  686. kfree(devinfo);
  687. if (shost)
  688. scsi_host_put(shost);
  689. return result;
  690. }
  691. static int uas_pre_reset(struct usb_interface *intf)
  692. {
  693. /* XXX: Need to return 1 if it's not our device in error handling */
  694. return 0;
  695. }
  696. static int uas_post_reset(struct usb_interface *intf)
  697. {
  698. /* XXX: Need to return 1 if it's not our device in error handling */
  699. return 0;
  700. }
  701. static void uas_disconnect(struct usb_interface *intf)
  702. {
  703. struct Scsi_Host *shost = usb_get_intfdata(intf);
  704. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  705. scsi_remove_host(shost);
  706. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  707. usb_kill_anchored_urbs(&devinfo->data_urbs);
  708. uas_free_streams(devinfo);
  709. kfree(devinfo);
  710. }
  711. /*
  712. * XXX: Should this plug into libusual so we can auto-upgrade devices from
  713. * Bulk-Only to UAS?
  714. */
  715. static struct usb_driver uas_driver = {
  716. .name = "uas",
  717. .probe = uas_probe,
  718. .disconnect = uas_disconnect,
  719. .pre_reset = uas_pre_reset,
  720. .post_reset = uas_post_reset,
  721. .id_table = uas_usb_ids,
  722. };
  723. module_usb_driver(uas_driver);
  724. MODULE_LICENSE("GPL");
  725. MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");