uas.c 22 KB

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