uas.c 19 KB

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