uas.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. if (devinfo->resetting) {
  573. cmnd->result = DID_ERROR << 16;
  574. cmnd->scsi_done(cmnd);
  575. return 0;
  576. }
  577. spin_lock_irqsave(&devinfo->lock, flags);
  578. if (devinfo->cmnd) {
  579. spin_unlock_irqrestore(&devinfo->lock, flags);
  580. return SCSI_MLQUEUE_DEVICE_BUSY;
  581. }
  582. if (blk_rq_tagged(cmnd->request)) {
  583. cmdinfo->stream = cmnd->request->tag + 2;
  584. } else {
  585. devinfo->cmnd = cmnd;
  586. cmdinfo->stream = 1;
  587. }
  588. cmnd->scsi_done = done;
  589. cmdinfo->state = SUBMIT_STATUS_URB |
  590. ALLOC_CMD_URB | SUBMIT_CMD_URB;
  591. switch (cmnd->sc_data_direction) {
  592. case DMA_FROM_DEVICE:
  593. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  594. break;
  595. case DMA_BIDIRECTIONAL:
  596. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  597. case DMA_TO_DEVICE:
  598. cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
  599. case DMA_NONE:
  600. break;
  601. }
  602. if (!devinfo->use_streams) {
  603. cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
  604. cmdinfo->stream = 0;
  605. }
  606. err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
  607. if (err) {
  608. /* If we did nothing, give up now */
  609. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  610. spin_unlock_irqrestore(&devinfo->lock, flags);
  611. return SCSI_MLQUEUE_DEVICE_BUSY;
  612. }
  613. spin_lock(&uas_work_lock);
  614. list_add_tail(&cmdinfo->list, &uas_work_list);
  615. cmdinfo->state |= IS_IN_WORK_LIST;
  616. spin_unlock(&uas_work_lock);
  617. schedule_work(&uas_work);
  618. }
  619. spin_unlock_irqrestore(&devinfo->lock, flags);
  620. return 0;
  621. }
  622. static DEF_SCSI_QCMD(uas_queuecommand)
  623. static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
  624. const char *fname, u8 function)
  625. {
  626. struct Scsi_Host *shost = cmnd->device->host;
  627. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  628. u16 tag = devinfo->qdepth - 1;
  629. unsigned long flags;
  630. spin_lock_irqsave(&devinfo->lock, flags);
  631. memset(&devinfo->response, 0, sizeof(devinfo->response));
  632. if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
  633. shost_printk(KERN_INFO, shost,
  634. "%s: %s: submit sense urb failed\n",
  635. __func__, fname);
  636. spin_unlock_irqrestore(&devinfo->lock, flags);
  637. return FAILED;
  638. }
  639. if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
  640. shost_printk(KERN_INFO, shost,
  641. "%s: %s: submit task mgmt urb failed\n",
  642. __func__, fname);
  643. spin_unlock_irqrestore(&devinfo->lock, flags);
  644. return FAILED;
  645. }
  646. spin_unlock_irqrestore(&devinfo->lock, flags);
  647. if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
  648. shost_printk(KERN_INFO, shost,
  649. "%s: %s timed out\n", __func__, fname);
  650. return FAILED;
  651. }
  652. if (be16_to_cpu(devinfo->response.tag) != tag) {
  653. shost_printk(KERN_INFO, shost,
  654. "%s: %s failed (wrong tag %d/%d)\n", __func__,
  655. fname, be16_to_cpu(devinfo->response.tag), tag);
  656. return FAILED;
  657. }
  658. if (devinfo->response.response_code != RC_TMF_COMPLETE) {
  659. shost_printk(KERN_INFO, shost,
  660. "%s: %s failed (rc 0x%x)\n", __func__,
  661. fname, devinfo->response.response_code);
  662. return FAILED;
  663. }
  664. return SUCCESS;
  665. }
  666. static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
  667. {
  668. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  669. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  670. unsigned long flags;
  671. int ret;
  672. uas_log_cmd_state(cmnd, __func__);
  673. spin_lock_irqsave(&devinfo->lock, flags);
  674. cmdinfo->state |= COMMAND_ABORTED;
  675. if (cmdinfo->state & IS_IN_WORK_LIST) {
  676. spin_lock(&uas_work_lock);
  677. list_del(&cmdinfo->list);
  678. cmdinfo->state &= ~IS_IN_WORK_LIST;
  679. spin_unlock(&uas_work_lock);
  680. }
  681. if (cmdinfo->state & COMMAND_INFLIGHT) {
  682. spin_unlock_irqrestore(&devinfo->lock, flags);
  683. ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
  684. } else {
  685. spin_unlock_irqrestore(&devinfo->lock, flags);
  686. uas_unlink_data_urbs(devinfo, cmdinfo);
  687. spin_lock_irqsave(&devinfo->lock, flags);
  688. uas_try_complete(cmnd, __func__);
  689. spin_unlock_irqrestore(&devinfo->lock, flags);
  690. ret = SUCCESS;
  691. }
  692. return ret;
  693. }
  694. static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
  695. {
  696. sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
  697. return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
  698. TMF_LOGICAL_UNIT_RESET);
  699. }
  700. static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
  701. {
  702. struct scsi_device *sdev = cmnd->device;
  703. struct uas_dev_info *devinfo = sdev->hostdata;
  704. struct usb_device *udev = devinfo->udev;
  705. int err;
  706. devinfo->resetting = 1;
  707. uas_abort_work(devinfo);
  708. usb_kill_anchored_urbs(&devinfo->cmd_urbs);
  709. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  710. usb_kill_anchored_urbs(&devinfo->data_urbs);
  711. err = usb_reset_device(udev);
  712. devinfo->resetting = 0;
  713. if (err) {
  714. shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
  715. return FAILED;
  716. }
  717. shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
  718. return SUCCESS;
  719. }
  720. static int uas_slave_alloc(struct scsi_device *sdev)
  721. {
  722. sdev->hostdata = (void *)sdev->host->hostdata[0];
  723. return 0;
  724. }
  725. static int uas_slave_configure(struct scsi_device *sdev)
  726. {
  727. struct uas_dev_info *devinfo = sdev->hostdata;
  728. scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
  729. scsi_activate_tcq(sdev, devinfo->qdepth - 3);
  730. return 0;
  731. }
  732. static struct scsi_host_template uas_host_template = {
  733. .module = THIS_MODULE,
  734. .name = "uas",
  735. .queuecommand = uas_queuecommand,
  736. .slave_alloc = uas_slave_alloc,
  737. .slave_configure = uas_slave_configure,
  738. .eh_abort_handler = uas_eh_abort_handler,
  739. .eh_device_reset_handler = uas_eh_device_reset_handler,
  740. .eh_bus_reset_handler = uas_eh_bus_reset_handler,
  741. .can_queue = 65536, /* Is there a limit on the _host_ ? */
  742. .this_id = -1,
  743. .sg_tablesize = SG_NONE,
  744. .cmd_per_lun = 1, /* until we override it */
  745. .skip_settle_delay = 1,
  746. .ordered_tag = 1,
  747. };
  748. static struct usb_device_id uas_usb_ids[] = {
  749. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
  750. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
  751. /* 0xaa is a prototype device I happen to have access to */
  752. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
  753. { }
  754. };
  755. MODULE_DEVICE_TABLE(usb, uas_usb_ids);
  756. static int uas_is_interface(struct usb_host_interface *intf)
  757. {
  758. return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
  759. intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
  760. intf->desc.bInterfaceProtocol == USB_PR_UAS);
  761. }
  762. static int uas_isnt_supported(struct usb_device *udev)
  763. {
  764. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  765. dev_warn(&udev->dev, "The driver for the USB controller %s does not "
  766. "support scatter-gather which is\n",
  767. hcd->driver->description);
  768. dev_warn(&udev->dev, "required by the UAS driver. Please try an"
  769. "alternative USB controller if you wish to use UAS.\n");
  770. return -ENODEV;
  771. }
  772. static int uas_switch_interface(struct usb_device *udev,
  773. struct usb_interface *intf)
  774. {
  775. int i;
  776. int sg_supported = udev->bus->sg_tablesize != 0;
  777. for (i = 0; i < intf->num_altsetting; i++) {
  778. struct usb_host_interface *alt = &intf->altsetting[i];
  779. if (uas_is_interface(alt)) {
  780. if (!sg_supported)
  781. return uas_isnt_supported(udev);
  782. return usb_set_interface(udev,
  783. alt->desc.bInterfaceNumber,
  784. alt->desc.bAlternateSetting);
  785. }
  786. }
  787. return -ENODEV;
  788. }
  789. static void uas_configure_endpoints(struct uas_dev_info *devinfo)
  790. {
  791. struct usb_host_endpoint *eps[4] = { };
  792. struct usb_interface *intf = devinfo->intf;
  793. struct usb_device *udev = devinfo->udev;
  794. struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
  795. unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
  796. devinfo->uas_sense_old = 0;
  797. devinfo->cmnd = NULL;
  798. for (i = 0; i < n_endpoints; i++) {
  799. unsigned char *extra = endpoint[i].extra;
  800. int len = endpoint[i].extralen;
  801. while (len > 1) {
  802. if (extra[1] == USB_DT_PIPE_USAGE) {
  803. unsigned pipe_id = extra[2];
  804. if (pipe_id > 0 && pipe_id < 5)
  805. eps[pipe_id - 1] = &endpoint[i];
  806. break;
  807. }
  808. len -= extra[0];
  809. extra += extra[0];
  810. }
  811. }
  812. /*
  813. * Assume that if we didn't find a control pipe descriptor, we're
  814. * using a device with old firmware that happens to be set up like
  815. * this.
  816. */
  817. if (!eps[0]) {
  818. devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
  819. devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
  820. devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
  821. devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
  822. eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  823. eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  824. eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  825. } else {
  826. devinfo->cmd_pipe = usb_sndbulkpipe(udev,
  827. eps[0]->desc.bEndpointAddress);
  828. devinfo->status_pipe = usb_rcvbulkpipe(udev,
  829. eps[1]->desc.bEndpointAddress);
  830. devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
  831. eps[2]->desc.bEndpointAddress);
  832. devinfo->data_out_pipe = usb_sndbulkpipe(udev,
  833. eps[3]->desc.bEndpointAddress);
  834. }
  835. devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
  836. GFP_KERNEL);
  837. if (devinfo->qdepth < 0) {
  838. devinfo->qdepth = 256;
  839. devinfo->use_streams = 0;
  840. } else {
  841. devinfo->use_streams = 1;
  842. }
  843. }
  844. static void uas_free_streams(struct uas_dev_info *devinfo)
  845. {
  846. struct usb_device *udev = devinfo->udev;
  847. struct usb_host_endpoint *eps[3];
  848. eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  849. eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  850. eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  851. usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
  852. }
  853. /*
  854. * XXX: What I'd like to do here is register a SCSI host for each USB host in
  855. * the system. Follow usb-storage's design of registering a SCSI host for
  856. * each USB device for the moment. Can implement this by walking up the
  857. * USB hierarchy until we find a USB host.
  858. */
  859. static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
  860. {
  861. int result;
  862. struct Scsi_Host *shost;
  863. struct uas_dev_info *devinfo;
  864. struct usb_device *udev = interface_to_usbdev(intf);
  865. if (uas_switch_interface(udev, intf))
  866. return -ENODEV;
  867. devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
  868. if (!devinfo)
  869. return -ENOMEM;
  870. result = -ENOMEM;
  871. shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
  872. if (!shost)
  873. goto free;
  874. shost->max_cmd_len = 16 + 252;
  875. shost->max_id = 1;
  876. shost->max_lun = 256;
  877. shost->max_channel = 0;
  878. shost->sg_tablesize = udev->bus->sg_tablesize;
  879. devinfo->intf = intf;
  880. devinfo->udev = udev;
  881. devinfo->resetting = 0;
  882. init_usb_anchor(&devinfo->cmd_urbs);
  883. init_usb_anchor(&devinfo->sense_urbs);
  884. init_usb_anchor(&devinfo->data_urbs);
  885. spin_lock_init(&devinfo->lock);
  886. uas_configure_endpoints(devinfo);
  887. result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3);
  888. if (result)
  889. goto free;
  890. result = scsi_add_host(shost, &intf->dev);
  891. if (result)
  892. goto deconfig_eps;
  893. shost->hostdata[0] = (unsigned long)devinfo;
  894. scsi_scan_host(shost);
  895. usb_set_intfdata(intf, shost);
  896. return result;
  897. deconfig_eps:
  898. uas_free_streams(devinfo);
  899. free:
  900. kfree(devinfo);
  901. if (shost)
  902. scsi_host_put(shost);
  903. return result;
  904. }
  905. static int uas_pre_reset(struct usb_interface *intf)
  906. {
  907. /* XXX: Need to return 1 if it's not our device in error handling */
  908. return 0;
  909. }
  910. static int uas_post_reset(struct usb_interface *intf)
  911. {
  912. /* XXX: Need to return 1 if it's not our device in error handling */
  913. return 0;
  914. }
  915. static void uas_disconnect(struct usb_interface *intf)
  916. {
  917. struct Scsi_Host *shost = usb_get_intfdata(intf);
  918. struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
  919. devinfo->resetting = 1;
  920. uas_abort_work(devinfo);
  921. usb_kill_anchored_urbs(&devinfo->cmd_urbs);
  922. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  923. usb_kill_anchored_urbs(&devinfo->data_urbs);
  924. scsi_remove_host(shost);
  925. uas_free_streams(devinfo);
  926. kfree(devinfo);
  927. }
  928. /*
  929. * XXX: Should this plug into libusual so we can auto-upgrade devices from
  930. * Bulk-Only to UAS?
  931. */
  932. static struct usb_driver uas_driver = {
  933. .name = "uas",
  934. .probe = uas_probe,
  935. .disconnect = uas_disconnect,
  936. .pre_reset = uas_pre_reset,
  937. .post_reset = uas_post_reset,
  938. .id_table = uas_usb_ids,
  939. };
  940. module_usb_driver(uas_driver);
  941. MODULE_LICENSE("GPL");
  942. MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");