uas.c 29 KB

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