uas.c 21 KB

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