uas.c 34 KB

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