uas.c 20 KB

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