uas.c 34 KB

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