uas.c 26 KB

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