uas.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /*
  2. * USB Attached SCSI
  3. * Note that this is not the same as the USB Mass Storage driver
  4. *
  5. * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2016
  6. * Copyright Matthew Wilcox for Intel Corp, 2010
  7. * Copyright Sarah Sharp for Intel Corp, 2010
  8. *
  9. * Distributed under the terms of the GNU GPL, version two.
  10. */
  11. #include <linux/blkdev.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb_usual.h>
  17. #include <linux/usb/hcd.h>
  18. #include <linux/usb/storage.h>
  19. #include <linux/usb/uas.h>
  20. #include <scsi/scsi.h>
  21. #include <scsi/scsi_eh.h>
  22. #include <scsi/scsi_dbg.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_tcq.h>
  27. #include "uas-detect.h"
  28. #include "scsiglue.h"
  29. #define MAX_CMNDS 256
  30. struct uas_dev_info {
  31. struct usb_interface *intf;
  32. struct usb_device *udev;
  33. struct usb_anchor cmd_urbs;
  34. struct usb_anchor sense_urbs;
  35. struct usb_anchor data_urbs;
  36. unsigned long flags;
  37. int qdepth, resetting;
  38. unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
  39. unsigned use_streams:1;
  40. unsigned shutdown:1;
  41. struct scsi_cmnd *cmnd[MAX_CMNDS];
  42. spinlock_t lock;
  43. struct work_struct work;
  44. };
  45. enum {
  46. SUBMIT_STATUS_URB = (1 << 1),
  47. ALLOC_DATA_IN_URB = (1 << 2),
  48. SUBMIT_DATA_IN_URB = (1 << 3),
  49. ALLOC_DATA_OUT_URB = (1 << 4),
  50. SUBMIT_DATA_OUT_URB = (1 << 5),
  51. ALLOC_CMD_URB = (1 << 6),
  52. SUBMIT_CMD_URB = (1 << 7),
  53. COMMAND_INFLIGHT = (1 << 8),
  54. DATA_IN_URB_INFLIGHT = (1 << 9),
  55. DATA_OUT_URB_INFLIGHT = (1 << 10),
  56. COMMAND_ABORTED = (1 << 11),
  57. IS_IN_WORK_LIST = (1 << 12),
  58. };
  59. /* Overrides scsi_pointer */
  60. struct uas_cmd_info {
  61. unsigned int state;
  62. unsigned int uas_tag;
  63. struct urb *cmd_urb;
  64. struct urb *data_in_urb;
  65. struct urb *data_out_urb;
  66. };
  67. /* I hate forward declarations, but I actually have a loop */
  68. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  69. struct uas_dev_info *devinfo, gfp_t gfp);
  70. static void uas_do_work(struct work_struct *work);
  71. static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
  72. static void uas_free_streams(struct uas_dev_info *devinfo);
  73. static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
  74. int status);
  75. static void uas_do_work(struct work_struct *work)
  76. {
  77. struct uas_dev_info *devinfo =
  78. container_of(work, struct uas_dev_info, work);
  79. struct uas_cmd_info *cmdinfo;
  80. struct scsi_cmnd *cmnd;
  81. unsigned long flags;
  82. int i, err;
  83. spin_lock_irqsave(&devinfo->lock, flags);
  84. if (devinfo->resetting)
  85. goto out;
  86. for (i = 0; i < devinfo->qdepth; i++) {
  87. if (!devinfo->cmnd[i])
  88. continue;
  89. cmnd = devinfo->cmnd[i];
  90. cmdinfo = (void *)&cmnd->SCp;
  91. if (!(cmdinfo->state & IS_IN_WORK_LIST))
  92. continue;
  93. err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
  94. if (!err)
  95. cmdinfo->state &= ~IS_IN_WORK_LIST;
  96. else
  97. schedule_work(&devinfo->work);
  98. }
  99. out:
  100. spin_unlock_irqrestore(&devinfo->lock, flags);
  101. }
  102. static void uas_add_work(struct uas_cmd_info *cmdinfo)
  103. {
  104. struct scsi_pointer *scp = (void *)cmdinfo;
  105. struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
  106. struct uas_dev_info *devinfo = cmnd->device->hostdata;
  107. lockdep_assert_held(&devinfo->lock);
  108. cmdinfo->state |= IS_IN_WORK_LIST;
  109. schedule_work(&devinfo->work);
  110. }
  111. static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
  112. {
  113. struct uas_cmd_info *cmdinfo;
  114. struct scsi_cmnd *cmnd;
  115. unsigned long flags;
  116. int i, err;
  117. spin_lock_irqsave(&devinfo->lock, flags);
  118. for (i = 0; i < devinfo->qdepth; i++) {
  119. if (!devinfo->cmnd[i])
  120. continue;
  121. cmnd = devinfo->cmnd[i];
  122. cmdinfo = (void *)&cmnd->SCp;
  123. uas_log_cmd_state(cmnd, __func__, 0);
  124. /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
  125. cmdinfo->state &= ~COMMAND_INFLIGHT;
  126. cmnd->result = result << 16;
  127. err = uas_try_complete(cmnd, __func__);
  128. WARN_ON(err != 0);
  129. }
  130. spin_unlock_irqrestore(&devinfo->lock, flags);
  131. }
  132. static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
  133. {
  134. struct sense_iu *sense_iu = urb->transfer_buffer;
  135. struct scsi_device *sdev = cmnd->device;
  136. if (urb->actual_length > 16) {
  137. unsigned len = be16_to_cpup(&sense_iu->len);
  138. if (len + 16 != urb->actual_length) {
  139. int newlen = min(len + 16, urb->actual_length) - 16;
  140. if (newlen < 0)
  141. newlen = 0;
  142. sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
  143. "disagrees with IU sense data length %d, "
  144. "using %d bytes of sense data\n", __func__,
  145. urb->actual_length, len, newlen);
  146. len = newlen;
  147. }
  148. memcpy(cmnd->sense_buffer, sense_iu->sense, len);
  149. }
  150. cmnd->result = sense_iu->status;
  151. }
  152. static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
  153. int status)
  154. {
  155. struct uas_cmd_info *ci = (void *)&cmnd->SCp;
  156. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  157. scmd_printk(KERN_INFO, cmnd,
  158. "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
  159. prefix, status, cmdinfo->uas_tag,
  160. (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
  161. (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
  162. (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
  163. (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
  164. (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
  165. (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
  166. (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
  167. (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
  168. (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
  169. (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
  170. (ci->state & COMMAND_ABORTED) ? " abort" : "",
  171. (ci->state & IS_IN_WORK_LIST) ? " work" : "");
  172. scsi_print_command(cmnd);
  173. }
  174. static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
  175. {
  176. struct uas_cmd_info *cmdinfo;
  177. if (!cmnd)
  178. return;
  179. cmdinfo = (void *)&cmnd->SCp;
  180. if (cmdinfo->state & SUBMIT_CMD_URB)
  181. usb_free_urb(cmdinfo->cmd_urb);
  182. /* data urbs may have never gotten their submit flag set */
  183. if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
  184. usb_free_urb(cmdinfo->data_in_urb);
  185. if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
  186. usb_free_urb(cmdinfo->data_out_urb);
  187. }
  188. static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
  189. {
  190. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  191. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  192. lockdep_assert_held(&devinfo->lock);
  193. if (cmdinfo->state & (COMMAND_INFLIGHT |
  194. DATA_IN_URB_INFLIGHT |
  195. DATA_OUT_URB_INFLIGHT |
  196. COMMAND_ABORTED))
  197. return -EBUSY;
  198. devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
  199. uas_free_unsubmitted_urbs(cmnd);
  200. cmnd->scsi_done(cmnd);
  201. return 0;
  202. }
  203. static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
  204. unsigned direction)
  205. {
  206. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  207. int err;
  208. cmdinfo->state |= direction | SUBMIT_STATUS_URB;
  209. err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
  210. if (err) {
  211. uas_add_work(cmdinfo);
  212. }
  213. }
  214. static void uas_stat_cmplt(struct urb *urb)
  215. {
  216. struct iu *iu = urb->transfer_buffer;
  217. struct Scsi_Host *shost = urb->context;
  218. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  219. struct urb *data_in_urb = NULL;
  220. struct urb *data_out_urb = NULL;
  221. struct scsi_cmnd *cmnd;
  222. struct uas_cmd_info *cmdinfo;
  223. unsigned long flags;
  224. unsigned int idx;
  225. int status = urb->status;
  226. spin_lock_irqsave(&devinfo->lock, flags);
  227. if (devinfo->resetting)
  228. goto out;
  229. if (status) {
  230. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  231. dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
  232. goto out;
  233. }
  234. idx = be16_to_cpup(&iu->tag) - 1;
  235. if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
  236. dev_err(&urb->dev->dev,
  237. "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
  238. goto out;
  239. }
  240. cmnd = devinfo->cmnd[idx];
  241. cmdinfo = (void *)&cmnd->SCp;
  242. if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
  243. uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
  244. goto out;
  245. }
  246. switch (iu->iu_id) {
  247. case IU_ID_STATUS:
  248. uas_sense(urb, cmnd);
  249. if (cmnd->result != 0) {
  250. /* cancel data transfers on error */
  251. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  252. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  253. }
  254. cmdinfo->state &= ~COMMAND_INFLIGHT;
  255. uas_try_complete(cmnd, __func__);
  256. break;
  257. case IU_ID_READ_READY:
  258. if (!cmdinfo->data_in_urb ||
  259. (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
  260. uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
  261. break;
  262. }
  263. uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
  264. break;
  265. case IU_ID_WRITE_READY:
  266. if (!cmdinfo->data_out_urb ||
  267. (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
  268. uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
  269. break;
  270. }
  271. uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
  272. break;
  273. case IU_ID_RESPONSE:
  274. uas_log_cmd_state(cmnd, "unexpected response iu",
  275. ((struct response_iu *)iu)->response_code);
  276. /* Error, cancel data transfers */
  277. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  278. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  279. cmdinfo->state &= ~COMMAND_INFLIGHT;
  280. cmnd->result = DID_ERROR << 16;
  281. uas_try_complete(cmnd, __func__);
  282. break;
  283. default:
  284. uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
  285. }
  286. out:
  287. usb_free_urb(urb);
  288. spin_unlock_irqrestore(&devinfo->lock, flags);
  289. /* Unlinking of data urbs must be done without holding the lock */
  290. if (data_in_urb) {
  291. usb_unlink_urb(data_in_urb);
  292. usb_put_urb(data_in_urb);
  293. }
  294. if (data_out_urb) {
  295. usb_unlink_urb(data_out_urb);
  296. usb_put_urb(data_out_urb);
  297. }
  298. }
  299. static void uas_data_cmplt(struct urb *urb)
  300. {
  301. struct scsi_cmnd *cmnd = urb->context;
  302. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  303. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  304. struct scsi_data_buffer *sdb = NULL;
  305. unsigned long flags;
  306. int status = urb->status;
  307. spin_lock_irqsave(&devinfo->lock, flags);
  308. if (cmdinfo->data_in_urb == urb) {
  309. sdb = scsi_in(cmnd);
  310. cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
  311. cmdinfo->data_in_urb = NULL;
  312. } else if (cmdinfo->data_out_urb == urb) {
  313. sdb = scsi_out(cmnd);
  314. cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
  315. cmdinfo->data_out_urb = NULL;
  316. }
  317. if (sdb == NULL) {
  318. WARN_ON_ONCE(1);
  319. goto out;
  320. }
  321. if (devinfo->resetting)
  322. goto out;
  323. /* Data urbs should not complete before the cmd urb is submitted */
  324. if (cmdinfo->state & SUBMIT_CMD_URB) {
  325. uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
  326. goto out;
  327. }
  328. if (status) {
  329. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  330. uas_log_cmd_state(cmnd, "data cmplt err", status);
  331. /* error: no data transfered */
  332. sdb->resid = sdb->length;
  333. } else {
  334. sdb->resid = sdb->length - urb->actual_length;
  335. }
  336. uas_try_complete(cmnd, __func__);
  337. out:
  338. usb_free_urb(urb);
  339. spin_unlock_irqrestore(&devinfo->lock, flags);
  340. }
  341. static void uas_cmd_cmplt(struct urb *urb)
  342. {
  343. if (urb->status)
  344. dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
  345. usb_free_urb(urb);
  346. }
  347. static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  348. struct scsi_cmnd *cmnd,
  349. enum dma_data_direction dir)
  350. {
  351. struct usb_device *udev = devinfo->udev;
  352. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  353. struct urb *urb = usb_alloc_urb(0, gfp);
  354. struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
  355. ? scsi_in(cmnd) : scsi_out(cmnd);
  356. unsigned int pipe = (dir == DMA_FROM_DEVICE)
  357. ? devinfo->data_in_pipe : devinfo->data_out_pipe;
  358. if (!urb)
  359. goto out;
  360. usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
  361. uas_data_cmplt, cmnd);
  362. if (devinfo->use_streams)
  363. urb->stream_id = cmdinfo->uas_tag;
  364. urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
  365. urb->sg = sdb->table.sgl;
  366. out:
  367. return urb;
  368. }
  369. static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  370. struct scsi_cmnd *cmnd)
  371. {
  372. struct usb_device *udev = devinfo->udev;
  373. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  374. struct urb *urb = usb_alloc_urb(0, gfp);
  375. struct sense_iu *iu;
  376. if (!urb)
  377. goto out;
  378. iu = kzalloc(sizeof(*iu), gfp);
  379. if (!iu)
  380. goto free;
  381. usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
  382. uas_stat_cmplt, cmnd->device->host);
  383. if (devinfo->use_streams)
  384. urb->stream_id = cmdinfo->uas_tag;
  385. urb->transfer_flags |= URB_FREE_BUFFER;
  386. out:
  387. return urb;
  388. free:
  389. usb_free_urb(urb);
  390. return NULL;
  391. }
  392. static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  393. struct scsi_cmnd *cmnd)
  394. {
  395. struct usb_device *udev = devinfo->udev;
  396. struct scsi_device *sdev = cmnd->device;
  397. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  398. struct urb *urb = usb_alloc_urb(0, gfp);
  399. struct command_iu *iu;
  400. int len;
  401. if (!urb)
  402. goto out;
  403. len = cmnd->cmd_len - 16;
  404. if (len < 0)
  405. len = 0;
  406. len = ALIGN(len, 4);
  407. iu = kzalloc(sizeof(*iu) + len, gfp);
  408. if (!iu)
  409. goto free;
  410. iu->iu_id = IU_ID_COMMAND;
  411. iu->tag = cpu_to_be16(cmdinfo->uas_tag);
  412. iu->prio_attr = UAS_SIMPLE_TAG;
  413. iu->len = len;
  414. int_to_scsilun(sdev->lun, &iu->lun);
  415. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  416. usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
  417. uas_cmd_cmplt, NULL);
  418. urb->transfer_flags |= URB_FREE_BUFFER;
  419. out:
  420. return urb;
  421. free:
  422. usb_free_urb(urb);
  423. return NULL;
  424. }
  425. /*
  426. * Why should I request the Status IU before sending the Command IU? Spec
  427. * says to, but also says the device may receive them in any order. Seems
  428. * daft to me.
  429. */
  430. static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
  431. {
  432. struct uas_dev_info *devinfo = cmnd->device->hostdata;
  433. struct urb *urb;
  434. int err;
  435. urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
  436. if (!urb)
  437. return NULL;
  438. usb_anchor_urb(urb, &devinfo->sense_urbs);
  439. err = usb_submit_urb(urb, gfp);
  440. if (err) {
  441. usb_unanchor_urb(urb);
  442. uas_log_cmd_state(cmnd, "sense submit err", err);
  443. usb_free_urb(urb);
  444. return NULL;
  445. }
  446. return urb;
  447. }
  448. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  449. struct uas_dev_info *devinfo, gfp_t gfp)
  450. {
  451. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  452. struct urb *urb;
  453. int err;
  454. lockdep_assert_held(&devinfo->lock);
  455. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  456. urb = uas_submit_sense_urb(cmnd, gfp);
  457. if (!urb)
  458. return SCSI_MLQUEUE_DEVICE_BUSY;
  459. cmdinfo->state &= ~SUBMIT_STATUS_URB;
  460. }
  461. if (cmdinfo->state & ALLOC_DATA_IN_URB) {
  462. cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
  463. cmnd, DMA_FROM_DEVICE);
  464. if (!cmdinfo->data_in_urb)
  465. return SCSI_MLQUEUE_DEVICE_BUSY;
  466. cmdinfo->state &= ~ALLOC_DATA_IN_URB;
  467. }
  468. if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
  469. usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
  470. err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
  471. if (err) {
  472. usb_unanchor_urb(cmdinfo->data_in_urb);
  473. uas_log_cmd_state(cmnd, "data in submit err", err);
  474. return SCSI_MLQUEUE_DEVICE_BUSY;
  475. }
  476. cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
  477. cmdinfo->state |= DATA_IN_URB_INFLIGHT;
  478. }
  479. if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
  480. cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
  481. cmnd, DMA_TO_DEVICE);
  482. if (!cmdinfo->data_out_urb)
  483. return SCSI_MLQUEUE_DEVICE_BUSY;
  484. cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
  485. }
  486. if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
  487. usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
  488. err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
  489. if (err) {
  490. usb_unanchor_urb(cmdinfo->data_out_urb);
  491. uas_log_cmd_state(cmnd, "data out submit err", err);
  492. return SCSI_MLQUEUE_DEVICE_BUSY;
  493. }
  494. cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
  495. cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
  496. }
  497. if (cmdinfo->state & ALLOC_CMD_URB) {
  498. cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
  499. if (!cmdinfo->cmd_urb)
  500. return SCSI_MLQUEUE_DEVICE_BUSY;
  501. cmdinfo->state &= ~ALLOC_CMD_URB;
  502. }
  503. if (cmdinfo->state & SUBMIT_CMD_URB) {
  504. usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
  505. err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
  506. if (err) {
  507. usb_unanchor_urb(cmdinfo->cmd_urb);
  508. uas_log_cmd_state(cmnd, "cmd submit err", err);
  509. return SCSI_MLQUEUE_DEVICE_BUSY;
  510. }
  511. cmdinfo->cmd_urb = NULL;
  512. cmdinfo->state &= ~SUBMIT_CMD_URB;
  513. cmdinfo->state |= COMMAND_INFLIGHT;
  514. }
  515. return 0;
  516. }
  517. static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
  518. void (*done)(struct scsi_cmnd *))
  519. {
  520. struct scsi_device *sdev = cmnd->device;
  521. struct uas_dev_info *devinfo = sdev->hostdata;
  522. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  523. unsigned long flags;
  524. int idx, err;
  525. BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
  526. /* Re-check scsi_block_requests now that we've the host-lock */
  527. if (cmnd->device->host->host_self_blocked)
  528. return SCSI_MLQUEUE_DEVICE_BUSY;
  529. if ((devinfo->flags & US_FL_NO_ATA_1X) &&
  530. (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
  531. memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
  532. sizeof(usb_stor_sense_invalidCDB));
  533. cmnd->result = SAM_STAT_CHECK_CONDITION;
  534. cmnd->scsi_done(cmnd);
  535. return 0;
  536. }
  537. spin_lock_irqsave(&devinfo->lock, flags);
  538. if (devinfo->resetting) {
  539. cmnd->result = DID_ERROR << 16;
  540. cmnd->scsi_done(cmnd);
  541. spin_unlock_irqrestore(&devinfo->lock, flags);
  542. return 0;
  543. }
  544. /* Find a free uas-tag */
  545. for (idx = 0; idx < devinfo->qdepth; idx++) {
  546. if (!devinfo->cmnd[idx])
  547. break;
  548. }
  549. if (idx == devinfo->qdepth) {
  550. spin_unlock_irqrestore(&devinfo->lock, flags);
  551. return SCSI_MLQUEUE_DEVICE_BUSY;
  552. }
  553. cmnd->scsi_done = done;
  554. memset(cmdinfo, 0, sizeof(*cmdinfo));
  555. cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
  556. cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
  557. switch (cmnd->sc_data_direction) {
  558. case DMA_FROM_DEVICE:
  559. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  560. break;
  561. case DMA_BIDIRECTIONAL:
  562. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  563. case DMA_TO_DEVICE:
  564. cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
  565. case DMA_NONE:
  566. break;
  567. }
  568. if (!devinfo->use_streams)
  569. cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
  570. err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
  571. if (err) {
  572. /* If we did nothing, give up now */
  573. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  574. spin_unlock_irqrestore(&devinfo->lock, flags);
  575. return SCSI_MLQUEUE_DEVICE_BUSY;
  576. }
  577. uas_add_work(cmdinfo);
  578. }
  579. devinfo->cmnd[idx] = cmnd;
  580. spin_unlock_irqrestore(&devinfo->lock, flags);
  581. return 0;
  582. }
  583. static DEF_SCSI_QCMD(uas_queuecommand)
  584. /*
  585. * For now we do not support actually sending an abort to the device, so
  586. * this eh always fails. Still we must define it to make sure that we've
  587. * dropped all references to the cmnd in question once this function exits.
  588. */
  589. static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
  590. {
  591. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  592. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  593. struct urb *data_in_urb = NULL;
  594. struct urb *data_out_urb = NULL;
  595. unsigned long flags;
  596. spin_lock_irqsave(&devinfo->lock, flags);
  597. uas_log_cmd_state(cmnd, __func__, 0);
  598. /* Ensure that try_complete does not call scsi_done */
  599. cmdinfo->state |= COMMAND_ABORTED;
  600. /* Drop all refs to this cmnd, kill data urbs to break their ref */
  601. devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
  602. if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
  603. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  604. if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
  605. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  606. uas_free_unsubmitted_urbs(cmnd);
  607. spin_unlock_irqrestore(&devinfo->lock, flags);
  608. if (data_in_urb) {
  609. usb_kill_urb(data_in_urb);
  610. usb_put_urb(data_in_urb);
  611. }
  612. if (data_out_urb) {
  613. usb_kill_urb(data_out_urb);
  614. usb_put_urb(data_out_urb);
  615. }
  616. return FAILED;
  617. }
  618. static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
  619. {
  620. struct scsi_device *sdev = cmnd->device;
  621. struct uas_dev_info *devinfo = sdev->hostdata;
  622. struct usb_device *udev = devinfo->udev;
  623. unsigned long flags;
  624. int err;
  625. err = usb_lock_device_for_reset(udev, devinfo->intf);
  626. if (err) {
  627. shost_printk(KERN_ERR, sdev->host,
  628. "%s FAILED to get lock err %d\n", __func__, err);
  629. return FAILED;
  630. }
  631. shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
  632. spin_lock_irqsave(&devinfo->lock, flags);
  633. devinfo->resetting = 1;
  634. spin_unlock_irqrestore(&devinfo->lock, flags);
  635. usb_kill_anchored_urbs(&devinfo->cmd_urbs);
  636. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  637. usb_kill_anchored_urbs(&devinfo->data_urbs);
  638. uas_zap_pending(devinfo, DID_RESET);
  639. err = usb_reset_device(udev);
  640. spin_lock_irqsave(&devinfo->lock, flags);
  641. devinfo->resetting = 0;
  642. spin_unlock_irqrestore(&devinfo->lock, flags);
  643. usb_unlock_device(udev);
  644. if (err) {
  645. shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
  646. __func__, err);
  647. return FAILED;
  648. }
  649. shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
  650. return SUCCESS;
  651. }
  652. static int uas_target_alloc(struct scsi_target *starget)
  653. {
  654. struct uas_dev_info *devinfo = (struct uas_dev_info *)
  655. dev_to_shost(starget->dev.parent)->hostdata;
  656. if (devinfo->flags & US_FL_NO_REPORT_LUNS)
  657. starget->no_report_luns = 1;
  658. return 0;
  659. }
  660. static int uas_slave_alloc(struct scsi_device *sdev)
  661. {
  662. struct uas_dev_info *devinfo =
  663. (struct uas_dev_info *)sdev->host->hostdata;
  664. sdev->hostdata = devinfo;
  665. /* USB has unusual DMA-alignment requirements: Although the
  666. * starting address of each scatter-gather element doesn't matter,
  667. * the length of each element except the last must be divisible
  668. * by the Bulk maxpacket value. There's currently no way to
  669. * express this by block-layer constraints, so we'll cop out
  670. * and simply require addresses to be aligned at 512-byte
  671. * boundaries. This is okay since most block I/O involves
  672. * hardware sectors that are multiples of 512 bytes in length,
  673. * and since host controllers up through USB 2.0 have maxpacket
  674. * values no larger than 512.
  675. *
  676. * But it doesn't suffice for Wireless USB, where Bulk maxpacket
  677. * values can be as large as 2048. To make that work properly
  678. * will require changes to the block layer.
  679. */
  680. blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
  681. if (devinfo->flags & US_FL_MAX_SECTORS_64)
  682. blk_queue_max_hw_sectors(sdev->request_queue, 64);
  683. else if (devinfo->flags & US_FL_MAX_SECTORS_240)
  684. blk_queue_max_hw_sectors(sdev->request_queue, 240);
  685. return 0;
  686. }
  687. static int uas_slave_configure(struct scsi_device *sdev)
  688. {
  689. struct uas_dev_info *devinfo = sdev->hostdata;
  690. if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
  691. sdev->no_report_opcodes = 1;
  692. /* A few buggy USB-ATA bridges don't understand FUA */
  693. if (devinfo->flags & US_FL_BROKEN_FUA)
  694. sdev->broken_fua = 1;
  695. scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
  696. return 0;
  697. }
  698. static struct scsi_host_template uas_host_template = {
  699. .module = THIS_MODULE,
  700. .name = "uas",
  701. .queuecommand = uas_queuecommand,
  702. .target_alloc = uas_target_alloc,
  703. .slave_alloc = uas_slave_alloc,
  704. .slave_configure = uas_slave_configure,
  705. .eh_abort_handler = uas_eh_abort_handler,
  706. .eh_bus_reset_handler = uas_eh_bus_reset_handler,
  707. .can_queue = MAX_CMNDS,
  708. .this_id = -1,
  709. .sg_tablesize = SG_NONE,
  710. .skip_settle_delay = 1,
  711. };
  712. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  713. vendorName, productName, useProtocol, useTransport, \
  714. initFunction, flags) \
  715. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  716. .driver_info = (flags) }
  717. static struct usb_device_id uas_usb_ids[] = {
  718. # include "unusual_uas.h"
  719. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
  720. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
  721. { }
  722. };
  723. MODULE_DEVICE_TABLE(usb, uas_usb_ids);
  724. #undef UNUSUAL_DEV
  725. static int uas_switch_interface(struct usb_device *udev,
  726. struct usb_interface *intf)
  727. {
  728. struct usb_host_interface *alt;
  729. alt = uas_find_uas_alt_setting(intf);
  730. if (!alt)
  731. return -ENODEV;
  732. return usb_set_interface(udev, alt->desc.bInterfaceNumber,
  733. alt->desc.bAlternateSetting);
  734. }
  735. static int uas_configure_endpoints(struct uas_dev_info *devinfo)
  736. {
  737. struct usb_host_endpoint *eps[4] = { };
  738. struct usb_device *udev = devinfo->udev;
  739. int r;
  740. r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
  741. if (r)
  742. return r;
  743. devinfo->cmd_pipe = usb_sndbulkpipe(udev,
  744. usb_endpoint_num(&eps[0]->desc));
  745. devinfo->status_pipe = usb_rcvbulkpipe(udev,
  746. usb_endpoint_num(&eps[1]->desc));
  747. devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
  748. usb_endpoint_num(&eps[2]->desc));
  749. devinfo->data_out_pipe = usb_sndbulkpipe(udev,
  750. usb_endpoint_num(&eps[3]->desc));
  751. if (udev->speed < USB_SPEED_SUPER) {
  752. devinfo->qdepth = 32;
  753. devinfo->use_streams = 0;
  754. } else {
  755. devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
  756. 3, MAX_CMNDS, GFP_NOIO);
  757. if (devinfo->qdepth < 0)
  758. return devinfo->qdepth;
  759. devinfo->use_streams = 1;
  760. }
  761. return 0;
  762. }
  763. static void uas_free_streams(struct uas_dev_info *devinfo)
  764. {
  765. struct usb_device *udev = devinfo->udev;
  766. struct usb_host_endpoint *eps[3];
  767. eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  768. eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  769. eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  770. usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
  771. }
  772. static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
  773. {
  774. int result = -ENOMEM;
  775. struct Scsi_Host *shost = NULL;
  776. struct uas_dev_info *devinfo;
  777. struct usb_device *udev = interface_to_usbdev(intf);
  778. unsigned long dev_flags;
  779. if (!uas_use_uas_driver(intf, id, &dev_flags))
  780. return -ENODEV;
  781. if (uas_switch_interface(udev, intf))
  782. return -ENODEV;
  783. shost = scsi_host_alloc(&uas_host_template,
  784. sizeof(struct uas_dev_info));
  785. if (!shost)
  786. goto set_alt0;
  787. shost->max_cmd_len = 16 + 252;
  788. shost->max_id = 1;
  789. shost->max_lun = 256;
  790. shost->max_channel = 0;
  791. shost->sg_tablesize = udev->bus->sg_tablesize;
  792. devinfo = (struct uas_dev_info *)shost->hostdata;
  793. devinfo->intf = intf;
  794. devinfo->udev = udev;
  795. devinfo->resetting = 0;
  796. devinfo->shutdown = 0;
  797. devinfo->flags = dev_flags;
  798. init_usb_anchor(&devinfo->cmd_urbs);
  799. init_usb_anchor(&devinfo->sense_urbs);
  800. init_usb_anchor(&devinfo->data_urbs);
  801. spin_lock_init(&devinfo->lock);
  802. INIT_WORK(&devinfo->work, uas_do_work);
  803. result = uas_configure_endpoints(devinfo);
  804. if (result)
  805. goto set_alt0;
  806. /*
  807. * 1 tag is reserved for untagged commands +
  808. * 1 tag to avoid off by one errors in some bridge firmwares
  809. */
  810. shost->can_queue = devinfo->qdepth - 2;
  811. usb_set_intfdata(intf, shost);
  812. result = scsi_add_host(shost, &intf->dev);
  813. if (result)
  814. goto free_streams;
  815. scsi_scan_host(shost);
  816. return result;
  817. free_streams:
  818. uas_free_streams(devinfo);
  819. usb_set_intfdata(intf, NULL);
  820. set_alt0:
  821. usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
  822. if (shost)
  823. scsi_host_put(shost);
  824. return result;
  825. }
  826. static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
  827. {
  828. unsigned long flags;
  829. int i, r = 1;
  830. spin_lock_irqsave(&devinfo->lock, flags);
  831. for (i = 0; i < devinfo->qdepth; i++) {
  832. if (devinfo->cmnd[i]) {
  833. r = 0; /* Not empty */
  834. break;
  835. }
  836. }
  837. spin_unlock_irqrestore(&devinfo->lock, flags);
  838. return r;
  839. }
  840. /*
  841. * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
  842. * get empty while there still is more work to do due to sense-urbs completing
  843. * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
  844. */
  845. static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
  846. {
  847. unsigned long start_time;
  848. int r;
  849. start_time = jiffies;
  850. do {
  851. flush_work(&devinfo->work);
  852. r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
  853. if (r == 0)
  854. return -ETIME;
  855. r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
  856. if (r == 0)
  857. return -ETIME;
  858. if (time_after(jiffies, start_time + 5 * HZ))
  859. return -ETIME;
  860. } while (!uas_cmnd_list_empty(devinfo));
  861. return 0;
  862. }
  863. static int uas_pre_reset(struct usb_interface *intf)
  864. {
  865. struct Scsi_Host *shost = usb_get_intfdata(intf);
  866. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  867. unsigned long flags;
  868. if (devinfo->shutdown)
  869. return 0;
  870. /* Block new requests */
  871. spin_lock_irqsave(shost->host_lock, flags);
  872. scsi_block_requests(shost);
  873. spin_unlock_irqrestore(shost->host_lock, flags);
  874. if (uas_wait_for_pending_cmnds(devinfo) != 0) {
  875. shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
  876. scsi_unblock_requests(shost);
  877. return 1;
  878. }
  879. uas_free_streams(devinfo);
  880. return 0;
  881. }
  882. static int uas_post_reset(struct usb_interface *intf)
  883. {
  884. struct Scsi_Host *shost = usb_get_intfdata(intf);
  885. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  886. unsigned long flags;
  887. int err;
  888. if (devinfo->shutdown)
  889. return 0;
  890. err = uas_configure_endpoints(devinfo);
  891. if (err && err != -ENODEV)
  892. shost_printk(KERN_ERR, shost,
  893. "%s: alloc streams error %d after reset",
  894. __func__, err);
  895. /* we must unblock the host in every case lest we deadlock */
  896. spin_lock_irqsave(shost->host_lock, flags);
  897. scsi_report_bus_reset(shost, 0);
  898. spin_unlock_irqrestore(shost->host_lock, flags);
  899. scsi_unblock_requests(shost);
  900. return err ? 1 : 0;
  901. }
  902. static int uas_suspend(struct usb_interface *intf, pm_message_t message)
  903. {
  904. struct Scsi_Host *shost = usb_get_intfdata(intf);
  905. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  906. if (uas_wait_for_pending_cmnds(devinfo) != 0) {
  907. shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
  908. return -ETIME;
  909. }
  910. return 0;
  911. }
  912. static int uas_resume(struct usb_interface *intf)
  913. {
  914. return 0;
  915. }
  916. static int uas_reset_resume(struct usb_interface *intf)
  917. {
  918. struct Scsi_Host *shost = usb_get_intfdata(intf);
  919. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  920. unsigned long flags;
  921. int err;
  922. err = uas_configure_endpoints(devinfo);
  923. if (err) {
  924. shost_printk(KERN_ERR, shost,
  925. "%s: alloc streams error %d after reset",
  926. __func__, err);
  927. return -EIO;
  928. }
  929. spin_lock_irqsave(shost->host_lock, flags);
  930. scsi_report_bus_reset(shost, 0);
  931. spin_unlock_irqrestore(shost->host_lock, flags);
  932. return 0;
  933. }
  934. static void uas_disconnect(struct usb_interface *intf)
  935. {
  936. struct Scsi_Host *shost = usb_get_intfdata(intf);
  937. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  938. unsigned long flags;
  939. spin_lock_irqsave(&devinfo->lock, flags);
  940. devinfo->resetting = 1;
  941. spin_unlock_irqrestore(&devinfo->lock, flags);
  942. cancel_work_sync(&devinfo->work);
  943. usb_kill_anchored_urbs(&devinfo->cmd_urbs);
  944. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  945. usb_kill_anchored_urbs(&devinfo->data_urbs);
  946. uas_zap_pending(devinfo, DID_NO_CONNECT);
  947. scsi_remove_host(shost);
  948. uas_free_streams(devinfo);
  949. scsi_host_put(shost);
  950. }
  951. /*
  952. * Put the device back in usb-storage mode on shutdown, as some BIOS-es
  953. * hang on reboot when the device is still in uas mode. Note the reset is
  954. * necessary as some devices won't revert to usb-storage mode without it.
  955. */
  956. static void uas_shutdown(struct device *dev)
  957. {
  958. struct usb_interface *intf = to_usb_interface(dev);
  959. struct usb_device *udev = interface_to_usbdev(intf);
  960. struct Scsi_Host *shost = usb_get_intfdata(intf);
  961. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  962. if (system_state != SYSTEM_RESTART)
  963. return;
  964. devinfo->shutdown = 1;
  965. uas_free_streams(devinfo);
  966. usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
  967. usb_reset_device(udev);
  968. }
  969. static struct usb_driver uas_driver = {
  970. .name = "uas",
  971. .probe = uas_probe,
  972. .disconnect = uas_disconnect,
  973. .pre_reset = uas_pre_reset,
  974. .post_reset = uas_post_reset,
  975. .suspend = uas_suspend,
  976. .resume = uas_resume,
  977. .reset_resume = uas_reset_resume,
  978. .drvwrap.driver.shutdown = uas_shutdown,
  979. .id_table = uas_usb_ids,
  980. };
  981. module_usb_driver(uas_driver);
  982. MODULE_LICENSE("GPL");
  983. MODULE_AUTHOR(
  984. "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");