uas.c 20 KB

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