uas.c 31 KB

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