uas.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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 = BIT(1),
  47. ALLOC_DATA_IN_URB = BIT(2),
  48. SUBMIT_DATA_IN_URB = BIT(3),
  49. ALLOC_DATA_OUT_URB = BIT(4),
  50. SUBMIT_DATA_OUT_URB = BIT(5),
  51. ALLOC_CMD_URB = BIT(6),
  52. SUBMIT_CMD_URB = BIT(7),
  53. COMMAND_INFLIGHT = BIT(8),
  54. DATA_IN_URB_INFLIGHT = BIT(9),
  55. DATA_OUT_URB_INFLIGHT = BIT(10),
  56. COMMAND_ABORTED = BIT(11),
  57. IS_IN_WORK_LIST = BIT(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);
  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);
  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);
  210. if (err) {
  211. uas_add_work(cmdinfo);
  212. }
  213. }
  214. static bool uas_evaluate_response_iu(struct response_iu *riu, struct scsi_cmnd *cmnd)
  215. {
  216. u8 response_code = riu->response_code;
  217. switch (response_code) {
  218. case RC_INCORRECT_LUN:
  219. cmnd->result = DID_BAD_TARGET << 16;
  220. break;
  221. case RC_TMF_SUCCEEDED:
  222. cmnd->result = DID_OK << 16;
  223. break;
  224. case RC_TMF_NOT_SUPPORTED:
  225. cmnd->result = DID_TARGET_FAILURE << 16;
  226. break;
  227. default:
  228. uas_log_cmd_state(cmnd, "response iu", response_code);
  229. cmnd->result = DID_ERROR << 16;
  230. break;
  231. }
  232. return response_code == RC_TMF_SUCCEEDED;
  233. }
  234. static void uas_stat_cmplt(struct urb *urb)
  235. {
  236. struct iu *iu = urb->transfer_buffer;
  237. struct Scsi_Host *shost = urb->context;
  238. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  239. struct urb *data_in_urb = NULL;
  240. struct urb *data_out_urb = NULL;
  241. struct scsi_cmnd *cmnd;
  242. struct uas_cmd_info *cmdinfo;
  243. unsigned long flags;
  244. unsigned int idx;
  245. int status = urb->status;
  246. bool success;
  247. spin_lock_irqsave(&devinfo->lock, flags);
  248. if (devinfo->resetting)
  249. goto out;
  250. if (status) {
  251. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  252. dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
  253. goto out;
  254. }
  255. idx = be16_to_cpup(&iu->tag) - 1;
  256. if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
  257. dev_err(&urb->dev->dev,
  258. "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
  259. goto out;
  260. }
  261. cmnd = devinfo->cmnd[idx];
  262. cmdinfo = (void *)&cmnd->SCp;
  263. if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
  264. uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
  265. goto out;
  266. }
  267. switch (iu->iu_id) {
  268. case IU_ID_STATUS:
  269. uas_sense(urb, cmnd);
  270. if (cmnd->result != 0) {
  271. /* cancel data transfers on error */
  272. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  273. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  274. }
  275. cmdinfo->state &= ~COMMAND_INFLIGHT;
  276. uas_try_complete(cmnd, __func__);
  277. break;
  278. case IU_ID_READ_READY:
  279. if (!cmdinfo->data_in_urb ||
  280. (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
  281. uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
  282. break;
  283. }
  284. uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
  285. break;
  286. case IU_ID_WRITE_READY:
  287. if (!cmdinfo->data_out_urb ||
  288. (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
  289. uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
  290. break;
  291. }
  292. uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
  293. break;
  294. case IU_ID_RESPONSE:
  295. cmdinfo->state &= ~COMMAND_INFLIGHT;
  296. success = uas_evaluate_response_iu((struct response_iu *)iu, cmnd);
  297. if (!success) {
  298. /* Error, cancel data transfers */
  299. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  300. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  301. }
  302. uas_try_complete(cmnd, __func__);
  303. break;
  304. default:
  305. uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
  306. }
  307. out:
  308. usb_free_urb(urb);
  309. spin_unlock_irqrestore(&devinfo->lock, flags);
  310. /* Unlinking of data urbs must be done without holding the lock */
  311. if (data_in_urb) {
  312. usb_unlink_urb(data_in_urb);
  313. usb_put_urb(data_in_urb);
  314. }
  315. if (data_out_urb) {
  316. usb_unlink_urb(data_out_urb);
  317. usb_put_urb(data_out_urb);
  318. }
  319. }
  320. static void uas_data_cmplt(struct urb *urb)
  321. {
  322. struct scsi_cmnd *cmnd = urb->context;
  323. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  324. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  325. struct scsi_data_buffer *sdb = NULL;
  326. unsigned long flags;
  327. int status = urb->status;
  328. spin_lock_irqsave(&devinfo->lock, flags);
  329. if (cmdinfo->data_in_urb == urb) {
  330. sdb = scsi_in(cmnd);
  331. cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
  332. cmdinfo->data_in_urb = NULL;
  333. } else if (cmdinfo->data_out_urb == urb) {
  334. sdb = scsi_out(cmnd);
  335. cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
  336. cmdinfo->data_out_urb = NULL;
  337. }
  338. if (sdb == NULL) {
  339. WARN_ON_ONCE(1);
  340. goto out;
  341. }
  342. if (devinfo->resetting)
  343. goto out;
  344. /* Data urbs should not complete before the cmd urb is submitted */
  345. if (cmdinfo->state & SUBMIT_CMD_URB) {
  346. uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
  347. goto out;
  348. }
  349. if (status) {
  350. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  351. uas_log_cmd_state(cmnd, "data cmplt err", status);
  352. /* error: no data transfered */
  353. sdb->resid = sdb->length;
  354. } else {
  355. sdb->resid = sdb->length - urb->actual_length;
  356. }
  357. uas_try_complete(cmnd, __func__);
  358. out:
  359. usb_free_urb(urb);
  360. spin_unlock_irqrestore(&devinfo->lock, flags);
  361. }
  362. static void uas_cmd_cmplt(struct urb *urb)
  363. {
  364. if (urb->status)
  365. dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
  366. usb_free_urb(urb);
  367. }
  368. static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  369. struct scsi_cmnd *cmnd,
  370. enum dma_data_direction dir)
  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 scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
  376. ? scsi_in(cmnd) : scsi_out(cmnd);
  377. unsigned int pipe = (dir == DMA_FROM_DEVICE)
  378. ? devinfo->data_in_pipe : devinfo->data_out_pipe;
  379. if (!urb)
  380. goto out;
  381. usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
  382. uas_data_cmplt, cmnd);
  383. if (devinfo->use_streams)
  384. urb->stream_id = cmdinfo->uas_tag;
  385. urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
  386. urb->sg = sdb->table.sgl;
  387. out:
  388. return urb;
  389. }
  390. static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  391. struct scsi_cmnd *cmnd)
  392. {
  393. struct usb_device *udev = devinfo->udev;
  394. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  395. struct urb *urb = usb_alloc_urb(0, gfp);
  396. struct sense_iu *iu;
  397. if (!urb)
  398. goto out;
  399. iu = kzalloc(sizeof(*iu), gfp);
  400. if (!iu)
  401. goto free;
  402. usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
  403. uas_stat_cmplt, cmnd->device->host);
  404. if (devinfo->use_streams)
  405. urb->stream_id = cmdinfo->uas_tag;
  406. urb->transfer_flags |= URB_FREE_BUFFER;
  407. out:
  408. return urb;
  409. free:
  410. usb_free_urb(urb);
  411. return NULL;
  412. }
  413. static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  414. struct scsi_cmnd *cmnd)
  415. {
  416. struct usb_device *udev = devinfo->udev;
  417. struct scsi_device *sdev = cmnd->device;
  418. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  419. struct urb *urb = usb_alloc_urb(0, gfp);
  420. struct command_iu *iu;
  421. int len;
  422. if (!urb)
  423. goto out;
  424. len = cmnd->cmd_len - 16;
  425. if (len < 0)
  426. len = 0;
  427. len = ALIGN(len, 4);
  428. iu = kzalloc(sizeof(*iu) + len, gfp);
  429. if (!iu)
  430. goto free;
  431. iu->iu_id = IU_ID_COMMAND;
  432. iu->tag = cpu_to_be16(cmdinfo->uas_tag);
  433. iu->prio_attr = UAS_SIMPLE_TAG;
  434. iu->len = len;
  435. int_to_scsilun(sdev->lun, &iu->lun);
  436. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  437. usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
  438. uas_cmd_cmplt, NULL);
  439. urb->transfer_flags |= URB_FREE_BUFFER;
  440. out:
  441. return urb;
  442. free:
  443. usb_free_urb(urb);
  444. return NULL;
  445. }
  446. /*
  447. * Why should I request the Status IU before sending the Command IU? Spec
  448. * says to, but also says the device may receive them in any order. Seems
  449. * daft to me.
  450. */
  451. static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
  452. {
  453. struct uas_dev_info *devinfo = cmnd->device->hostdata;
  454. struct urb *urb;
  455. int err;
  456. urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
  457. if (!urb)
  458. return NULL;
  459. usb_anchor_urb(urb, &devinfo->sense_urbs);
  460. err = usb_submit_urb(urb, gfp);
  461. if (err) {
  462. usb_unanchor_urb(urb);
  463. uas_log_cmd_state(cmnd, "sense submit err", err);
  464. usb_free_urb(urb);
  465. return NULL;
  466. }
  467. return urb;
  468. }
  469. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  470. struct uas_dev_info *devinfo)
  471. {
  472. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  473. struct urb *urb;
  474. int err;
  475. lockdep_assert_held(&devinfo->lock);
  476. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  477. urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC);
  478. if (!urb)
  479. return SCSI_MLQUEUE_DEVICE_BUSY;
  480. cmdinfo->state &= ~SUBMIT_STATUS_URB;
  481. }
  482. if (cmdinfo->state & ALLOC_DATA_IN_URB) {
  483. cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
  484. cmnd, DMA_FROM_DEVICE);
  485. if (!cmdinfo->data_in_urb)
  486. return SCSI_MLQUEUE_DEVICE_BUSY;
  487. cmdinfo->state &= ~ALLOC_DATA_IN_URB;
  488. }
  489. if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
  490. usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
  491. err = usb_submit_urb(cmdinfo->data_in_urb, GFP_ATOMIC);
  492. if (err) {
  493. usb_unanchor_urb(cmdinfo->data_in_urb);
  494. uas_log_cmd_state(cmnd, "data in submit err", err);
  495. return SCSI_MLQUEUE_DEVICE_BUSY;
  496. }
  497. cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
  498. cmdinfo->state |= DATA_IN_URB_INFLIGHT;
  499. }
  500. if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
  501. cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, GFP_ATOMIC,
  502. cmnd, DMA_TO_DEVICE);
  503. if (!cmdinfo->data_out_urb)
  504. return SCSI_MLQUEUE_DEVICE_BUSY;
  505. cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
  506. }
  507. if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
  508. usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
  509. err = usb_submit_urb(cmdinfo->data_out_urb, GFP_ATOMIC);
  510. if (err) {
  511. usb_unanchor_urb(cmdinfo->data_out_urb);
  512. uas_log_cmd_state(cmnd, "data out submit err", err);
  513. return SCSI_MLQUEUE_DEVICE_BUSY;
  514. }
  515. cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
  516. cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
  517. }
  518. if (cmdinfo->state & ALLOC_CMD_URB) {
  519. cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, GFP_ATOMIC, cmnd);
  520. if (!cmdinfo->cmd_urb)
  521. return SCSI_MLQUEUE_DEVICE_BUSY;
  522. cmdinfo->state &= ~ALLOC_CMD_URB;
  523. }
  524. if (cmdinfo->state & SUBMIT_CMD_URB) {
  525. usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
  526. err = usb_submit_urb(cmdinfo->cmd_urb, GFP_ATOMIC);
  527. if (err) {
  528. usb_unanchor_urb(cmdinfo->cmd_urb);
  529. uas_log_cmd_state(cmnd, "cmd submit err", err);
  530. return SCSI_MLQUEUE_DEVICE_BUSY;
  531. }
  532. cmdinfo->cmd_urb = NULL;
  533. cmdinfo->state &= ~SUBMIT_CMD_URB;
  534. cmdinfo->state |= COMMAND_INFLIGHT;
  535. }
  536. return 0;
  537. }
  538. static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
  539. void (*done)(struct scsi_cmnd *))
  540. {
  541. struct scsi_device *sdev = cmnd->device;
  542. struct uas_dev_info *devinfo = sdev->hostdata;
  543. struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
  544. unsigned long flags;
  545. int idx, err;
  546. BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
  547. /* Re-check scsi_block_requests now that we've the host-lock */
  548. if (cmnd->device->host->host_self_blocked)
  549. return SCSI_MLQUEUE_DEVICE_BUSY;
  550. if ((devinfo->flags & US_FL_NO_ATA_1X) &&
  551. (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
  552. memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
  553. sizeof(usb_stor_sense_invalidCDB));
  554. cmnd->result = SAM_STAT_CHECK_CONDITION;
  555. cmnd->scsi_done(cmnd);
  556. return 0;
  557. }
  558. spin_lock_irqsave(&devinfo->lock, flags);
  559. if (devinfo->resetting) {
  560. cmnd->result = DID_ERROR << 16;
  561. cmnd->scsi_done(cmnd);
  562. spin_unlock_irqrestore(&devinfo->lock, flags);
  563. return 0;
  564. }
  565. /* Find a free uas-tag */
  566. for (idx = 0; idx < devinfo->qdepth; idx++) {
  567. if (!devinfo->cmnd[idx])
  568. break;
  569. }
  570. if (idx == devinfo->qdepth) {
  571. spin_unlock_irqrestore(&devinfo->lock, flags);
  572. return SCSI_MLQUEUE_DEVICE_BUSY;
  573. }
  574. cmnd->scsi_done = done;
  575. memset(cmdinfo, 0, sizeof(*cmdinfo));
  576. cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
  577. cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
  578. switch (cmnd->sc_data_direction) {
  579. case DMA_FROM_DEVICE:
  580. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  581. break;
  582. case DMA_BIDIRECTIONAL:
  583. cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
  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_bus_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_bus_reset_handler = uas_eh_bus_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");