uas.c 19 KB

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