uas.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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_devinfo.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. u64 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. struct work_struct scan_work; /* for async scanning */
  45. };
  46. enum {
  47. SUBMIT_STATUS_URB = BIT(1),
  48. ALLOC_DATA_IN_URB = BIT(2),
  49. SUBMIT_DATA_IN_URB = BIT(3),
  50. ALLOC_DATA_OUT_URB = BIT(4),
  51. SUBMIT_DATA_OUT_URB = BIT(5),
  52. ALLOC_CMD_URB = BIT(6),
  53. SUBMIT_CMD_URB = BIT(7),
  54. COMMAND_INFLIGHT = BIT(8),
  55. DATA_IN_URB_INFLIGHT = BIT(9),
  56. DATA_OUT_URB_INFLIGHT = BIT(10),
  57. COMMAND_ABORTED = BIT(11),
  58. IS_IN_WORK_LIST = BIT(12),
  59. };
  60. /* Overrides scsi_pointer */
  61. struct uas_cmd_info {
  62. unsigned int state;
  63. unsigned int uas_tag;
  64. struct urb *cmd_urb;
  65. struct urb *data_in_urb;
  66. struct urb *data_out_urb;
  67. };
  68. /* I hate forward declarations, but I actually have a loop */
  69. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  70. struct uas_dev_info *devinfo);
  71. static void uas_do_work(struct work_struct *work);
  72. static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
  73. static void uas_free_streams(struct uas_dev_info *devinfo);
  74. static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
  75. int status);
  76. /*
  77. * This driver needs its own workqueue, as we need to control memory allocation.
  78. *
  79. * In the course of error handling and power management uas_wait_for_pending_cmnds()
  80. * needs to flush pending work items. In these contexts we cannot allocate memory
  81. * by doing block IO as we would deadlock. For the same reason we cannot wait
  82. * for anything allocating memory not heeding these constraints.
  83. *
  84. * So we have to control all work items that can be on the workqueue we flush.
  85. * Hence we cannot share a queue and need our own.
  86. */
  87. static struct workqueue_struct *workqueue;
  88. static void uas_do_work(struct work_struct *work)
  89. {
  90. struct uas_dev_info *devinfo =
  91. container_of(work, struct uas_dev_info, work);
  92. struct uas_cmd_info *cmdinfo;
  93. struct scsi_cmnd *cmnd;
  94. unsigned long flags;
  95. int i, err;
  96. spin_lock_irqsave(&devinfo->lock, flags);
  97. if (devinfo->resetting)
  98. goto out;
  99. for (i = 0; i < devinfo->qdepth; i++) {
  100. if (!devinfo->cmnd[i])
  101. continue;
  102. cmnd = devinfo->cmnd[i];
  103. cmdinfo = scsi_cmd_priv(cmnd);
  104. if (!(cmdinfo->state & IS_IN_WORK_LIST))
  105. continue;
  106. err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
  107. if (!err)
  108. cmdinfo->state &= ~IS_IN_WORK_LIST;
  109. else
  110. queue_work(workqueue, &devinfo->work);
  111. }
  112. out:
  113. spin_unlock_irqrestore(&devinfo->lock, flags);
  114. }
  115. static void uas_scan_work(struct work_struct *work)
  116. {
  117. struct uas_dev_info *devinfo =
  118. container_of(work, struct uas_dev_info, scan_work);
  119. struct Scsi_Host *shost = usb_get_intfdata(devinfo->intf);
  120. dev_dbg(&devinfo->intf->dev, "starting scan\n");
  121. scsi_scan_host(shost);
  122. dev_dbg(&devinfo->intf->dev, "scan complete\n");
  123. }
  124. static void uas_add_work(struct scsi_cmnd *cmnd)
  125. {
  126. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  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 = scsi_cmd_priv(cmnd);
  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 = scsi_cmd_priv(cmnd);
  177. if (status == -ENODEV) /* too late */
  178. return;
  179. scmd_printk(KERN_INFO, cmnd,
  180. "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
  181. prefix, status, ci->uas_tag,
  182. (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
  183. (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
  184. (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
  185. (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
  186. (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
  187. (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
  188. (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
  189. (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
  190. (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
  191. (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
  192. (ci->state & COMMAND_ABORTED) ? " abort" : "",
  193. (ci->state & IS_IN_WORK_LIST) ? " work" : "");
  194. scsi_print_command(cmnd);
  195. }
  196. static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
  197. {
  198. struct uas_cmd_info *cmdinfo;
  199. if (!cmnd)
  200. return;
  201. cmdinfo = scsi_cmd_priv(cmnd);
  202. if (cmdinfo->state & SUBMIT_CMD_URB)
  203. usb_free_urb(cmdinfo->cmd_urb);
  204. /* data urbs may have never gotten their submit flag set */
  205. if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
  206. usb_free_urb(cmdinfo->data_in_urb);
  207. if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
  208. usb_free_urb(cmdinfo->data_out_urb);
  209. }
  210. static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
  211. {
  212. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  213. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  214. lockdep_assert_held(&devinfo->lock);
  215. if (cmdinfo->state & (COMMAND_INFLIGHT |
  216. DATA_IN_URB_INFLIGHT |
  217. DATA_OUT_URB_INFLIGHT |
  218. COMMAND_ABORTED))
  219. return -EBUSY;
  220. devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
  221. uas_free_unsubmitted_urbs(cmnd);
  222. scsi_done(cmnd);
  223. return 0;
  224. }
  225. static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
  226. unsigned direction)
  227. {
  228. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  229. int err;
  230. cmdinfo->state |= direction | SUBMIT_STATUS_URB;
  231. err = uas_submit_urbs(cmnd, cmnd->device->hostdata);
  232. if (err) {
  233. uas_add_work(cmnd);
  234. }
  235. }
  236. static bool uas_evaluate_response_iu(struct response_iu *riu, struct scsi_cmnd *cmnd)
  237. {
  238. u8 response_code = riu->response_code;
  239. switch (response_code) {
  240. case RC_INCORRECT_LUN:
  241. set_host_byte(cmnd, DID_BAD_TARGET);
  242. break;
  243. case RC_TMF_SUCCEEDED:
  244. set_host_byte(cmnd, DID_OK);
  245. break;
  246. case RC_TMF_NOT_SUPPORTED:
  247. set_host_byte(cmnd, DID_BAD_TARGET);
  248. break;
  249. default:
  250. uas_log_cmd_state(cmnd, "response iu", response_code);
  251. set_host_byte(cmnd, DID_ERROR);
  252. break;
  253. }
  254. return response_code == RC_TMF_SUCCEEDED;
  255. }
  256. static void uas_stat_cmplt(struct urb *urb)
  257. {
  258. struct iu *iu = urb->transfer_buffer;
  259. struct Scsi_Host *shost = urb->context;
  260. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  261. struct urb *data_in_urb = NULL;
  262. struct urb *data_out_urb = NULL;
  263. struct scsi_cmnd *cmnd;
  264. struct uas_cmd_info *cmdinfo;
  265. unsigned long flags;
  266. unsigned int idx;
  267. int status = urb->status;
  268. bool success;
  269. spin_lock_irqsave(&devinfo->lock, flags);
  270. if (devinfo->resetting)
  271. goto out;
  272. if (status) {
  273. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  274. dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
  275. goto out;
  276. }
  277. idx = be16_to_cpup(&iu->tag) - 1;
  278. if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
  279. dev_err(&urb->dev->dev,
  280. "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
  281. goto out;
  282. }
  283. cmnd = devinfo->cmnd[idx];
  284. cmdinfo = scsi_cmd_priv(cmnd);
  285. if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
  286. uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
  287. goto out;
  288. }
  289. switch (iu->iu_id) {
  290. case IU_ID_STATUS:
  291. uas_sense(urb, cmnd);
  292. if (cmnd->result != 0) {
  293. /* cancel data transfers on error */
  294. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  295. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  296. }
  297. cmdinfo->state &= ~COMMAND_INFLIGHT;
  298. uas_try_complete(cmnd, __func__);
  299. break;
  300. case IU_ID_READ_READY:
  301. if (!cmdinfo->data_in_urb ||
  302. (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
  303. uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
  304. break;
  305. }
  306. uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
  307. break;
  308. case IU_ID_WRITE_READY:
  309. if (!cmdinfo->data_out_urb ||
  310. (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
  311. uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
  312. break;
  313. }
  314. uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
  315. break;
  316. case IU_ID_RESPONSE:
  317. cmdinfo->state &= ~COMMAND_INFLIGHT;
  318. success = uas_evaluate_response_iu((struct response_iu *)iu, cmnd);
  319. if (!success) {
  320. /* Error, cancel data transfers */
  321. data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
  322. data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
  323. }
  324. uas_try_complete(cmnd, __func__);
  325. break;
  326. default:
  327. uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
  328. }
  329. out:
  330. usb_free_urb(urb);
  331. spin_unlock_irqrestore(&devinfo->lock, flags);
  332. /* Unlinking of data urbs must be done without holding the lock */
  333. if (data_in_urb) {
  334. usb_unlink_urb(data_in_urb);
  335. usb_put_urb(data_in_urb);
  336. }
  337. if (data_out_urb) {
  338. usb_unlink_urb(data_out_urb);
  339. usb_put_urb(data_out_urb);
  340. }
  341. }
  342. static void uas_data_cmplt(struct urb *urb)
  343. {
  344. struct scsi_cmnd *cmnd = urb->context;
  345. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  346. struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
  347. struct scsi_data_buffer *sdb = &cmnd->sdb;
  348. unsigned long flags;
  349. int status = urb->status;
  350. spin_lock_irqsave(&devinfo->lock, flags);
  351. if (cmdinfo->data_in_urb == urb) {
  352. cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
  353. cmdinfo->data_in_urb = NULL;
  354. } else if (cmdinfo->data_out_urb == urb) {
  355. cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
  356. cmdinfo->data_out_urb = NULL;
  357. }
  358. if (devinfo->resetting)
  359. goto out;
  360. /* Data urbs should not complete before the cmd urb is submitted */
  361. if (cmdinfo->state & SUBMIT_CMD_URB) {
  362. uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
  363. goto out;
  364. }
  365. if (status) {
  366. if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
  367. uas_log_cmd_state(cmnd, "data cmplt err", status);
  368. /* error: no data transfered */
  369. scsi_set_resid(cmnd, sdb->length);
  370. set_host_byte(cmnd, DID_ERROR);
  371. } else {
  372. scsi_set_resid(cmnd, sdb->length - urb->actual_length);
  373. }
  374. uas_try_complete(cmnd, __func__);
  375. out:
  376. usb_free_urb(urb);
  377. spin_unlock_irqrestore(&devinfo->lock, flags);
  378. }
  379. static void uas_cmd_cmplt(struct urb *urb)
  380. {
  381. if (urb->status)
  382. dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
  383. usb_free_urb(urb);
  384. }
  385. static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  386. struct scsi_cmnd *cmnd,
  387. enum dma_data_direction dir)
  388. {
  389. struct usb_device *udev = devinfo->udev;
  390. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  391. struct urb *urb = usb_alloc_urb(0, gfp);
  392. struct scsi_data_buffer *sdb = &cmnd->sdb;
  393. unsigned int pipe = (dir == DMA_FROM_DEVICE)
  394. ? devinfo->data_in_pipe : devinfo->data_out_pipe;
  395. if (!urb)
  396. goto out;
  397. usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
  398. uas_data_cmplt, cmnd);
  399. if (devinfo->use_streams)
  400. urb->stream_id = cmdinfo->uas_tag;
  401. urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
  402. urb->sg = sdb->table.sgl;
  403. out:
  404. return urb;
  405. }
  406. static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  407. struct scsi_cmnd *cmnd)
  408. {
  409. struct usb_device *udev = devinfo->udev;
  410. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  411. struct urb *urb = usb_alloc_urb(0, gfp);
  412. struct sense_iu *iu;
  413. if (!urb)
  414. goto out;
  415. iu = kzalloc(sizeof(*iu), gfp);
  416. if (!iu)
  417. goto free;
  418. usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
  419. uas_stat_cmplt, cmnd->device->host);
  420. if (devinfo->use_streams)
  421. urb->stream_id = cmdinfo->uas_tag;
  422. urb->transfer_flags |= URB_FREE_BUFFER;
  423. out:
  424. return urb;
  425. free:
  426. usb_free_urb(urb);
  427. return NULL;
  428. }
  429. static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
  430. struct scsi_cmnd *cmnd)
  431. {
  432. struct usb_device *udev = devinfo->udev;
  433. struct scsi_device *sdev = cmnd->device;
  434. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  435. struct urb *urb = usb_alloc_urb(0, gfp);
  436. struct command_iu *iu;
  437. int len;
  438. if (!urb)
  439. goto out;
  440. len = cmnd->cmd_len - 16;
  441. if (len < 0)
  442. len = 0;
  443. len = ALIGN(len, 4);
  444. iu = kzalloc(sizeof(*iu) + len, gfp);
  445. if (!iu)
  446. goto free;
  447. iu->iu_id = IU_ID_COMMAND;
  448. iu->tag = cpu_to_be16(cmdinfo->uas_tag);
  449. iu->prio_attr = UAS_SIMPLE_TAG;
  450. iu->len = len;
  451. int_to_scsilun(sdev->lun, &iu->lun);
  452. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  453. usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
  454. uas_cmd_cmplt, NULL);
  455. urb->transfer_flags |= URB_FREE_BUFFER;
  456. out:
  457. return urb;
  458. free:
  459. usb_free_urb(urb);
  460. return NULL;
  461. }
  462. /*
  463. * Why should I request the Status IU before sending the Command IU? Spec
  464. * says to, but also says the device may receive them in any order. Seems
  465. * daft to me.
  466. */
  467. static int uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
  468. {
  469. struct uas_dev_info *devinfo = cmnd->device->hostdata;
  470. struct urb *urb;
  471. int err;
  472. urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
  473. if (!urb)
  474. return -ENOMEM;
  475. usb_anchor_urb(urb, &devinfo->sense_urbs);
  476. err = usb_submit_urb(urb, gfp);
  477. if (err) {
  478. usb_unanchor_urb(urb);
  479. uas_log_cmd_state(cmnd, "sense submit err", err);
  480. usb_free_urb(urb);
  481. }
  482. return err;
  483. }
  484. static int uas_submit_urbs(struct scsi_cmnd *cmnd,
  485. struct uas_dev_info *devinfo)
  486. {
  487. struct uas_cmd_info *cmdinfo = scsi_cmd_priv(cmnd);
  488. int err;
  489. lockdep_assert_held(&devinfo->lock);
  490. if (cmdinfo->state & SUBMIT_STATUS_URB) {
  491. err = uas_submit_sense_urb(cmnd, GFP_ATOMIC);
  492. if (err)
  493. return err;
  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 -ENOMEM;
  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 err;
  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 -ENOMEM;
  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 err;
  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 -ENOMEM;
  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 err;
  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_NO_CONNECT);
  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. /*
  709. * Some USB storage devices reset if the IO advice hints grouping mode
  710. * page is queried. Hence skip that mode page.
  711. */
  712. sdev->sdev_bflags |= BLIST_SKIP_IO_HINTS;
  713. sdev->hostdata = devinfo;
  714. return 0;
  715. }
  716. static int uas_device_configure(struct scsi_device *sdev,
  717. struct queue_limits *lim)
  718. {
  719. struct uas_dev_info *devinfo = sdev->hostdata;
  720. if (devinfo->flags & US_FL_MAX_SECTORS_64)
  721. lim->max_hw_sectors = 64;
  722. else if (devinfo->flags & US_FL_MAX_SECTORS_240)
  723. lim->max_hw_sectors = 240;
  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 report generic values until the media has been
  755. * accessed. Force a READ(10) prior to querying device
  756. * characteristics.
  757. */
  758. sdev->read_before_ms = 1;
  759. /*
  760. * Some devices don't like MODE SENSE with page=0x3f,
  761. * which is the command used for checking if a device
  762. * is write-protected. Now that we tell the sd driver
  763. * to do a 192-byte transfer with this command the
  764. * majority of devices work fine, but a few still can't
  765. * handle it. The sd driver will simply assume those
  766. * devices are write-enabled.
  767. */
  768. if (devinfo->flags & US_FL_NO_WP_DETECT)
  769. sdev->skip_ms_page_3f = 1;
  770. scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
  771. return 0;
  772. }
  773. static const struct scsi_host_template uas_host_template = {
  774. .module = THIS_MODULE,
  775. .name = "uas",
  776. .queuecommand = uas_queuecommand,
  777. .target_alloc = uas_target_alloc,
  778. .slave_alloc = uas_slave_alloc,
  779. .device_configure = uas_device_configure,
  780. .eh_abort_handler = uas_eh_abort_handler,
  781. .eh_device_reset_handler = uas_eh_device_reset_handler,
  782. .this_id = -1,
  783. .skip_settle_delay = 1,
  784. /*
  785. * The protocol has no requirements on alignment in the strict sense.
  786. * Controllers may or may not have alignment restrictions.
  787. * As this is not exported, we use an extremely conservative guess.
  788. */
  789. .dma_alignment = 511,
  790. .dma_boundary = PAGE_SIZE - 1,
  791. .cmd_size = sizeof(struct uas_cmd_info),
  792. };
  793. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  794. vendorName, productName, useProtocol, useTransport, \
  795. initFunction, flags) \
  796. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  797. .driver_info = (flags) }
  798. static const struct usb_device_id uas_usb_ids[] = {
  799. # include "unusual_uas.h"
  800. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
  801. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
  802. { }
  803. };
  804. MODULE_DEVICE_TABLE(usb, uas_usb_ids);
  805. #undef UNUSUAL_DEV
  806. static int uas_switch_interface(struct usb_device *udev,
  807. struct usb_interface *intf)
  808. {
  809. struct usb_host_interface *alt;
  810. alt = uas_find_uas_alt_setting(intf);
  811. if (!alt)
  812. return -ENODEV;
  813. return usb_set_interface(udev, alt->desc.bInterfaceNumber,
  814. alt->desc.bAlternateSetting);
  815. }
  816. static int uas_configure_endpoints(struct uas_dev_info *devinfo)
  817. {
  818. struct usb_host_endpoint *eps[4] = { };
  819. struct usb_device *udev = devinfo->udev;
  820. int r;
  821. r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
  822. if (r)
  823. return r;
  824. devinfo->cmd_pipe = usb_sndbulkpipe(udev,
  825. usb_endpoint_num(&eps[0]->desc));
  826. devinfo->status_pipe = usb_rcvbulkpipe(udev,
  827. usb_endpoint_num(&eps[1]->desc));
  828. devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
  829. usb_endpoint_num(&eps[2]->desc));
  830. devinfo->data_out_pipe = usb_sndbulkpipe(udev,
  831. usb_endpoint_num(&eps[3]->desc));
  832. if (udev->speed < USB_SPEED_SUPER) {
  833. devinfo->qdepth = 32;
  834. devinfo->use_streams = 0;
  835. } else {
  836. devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
  837. 3, MAX_CMNDS, GFP_NOIO);
  838. if (devinfo->qdepth < 0)
  839. return devinfo->qdepth;
  840. devinfo->use_streams = 1;
  841. }
  842. return 0;
  843. }
  844. static void uas_free_streams(struct uas_dev_info *devinfo)
  845. {
  846. struct usb_device *udev = devinfo->udev;
  847. struct usb_host_endpoint *eps[3];
  848. eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
  849. eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
  850. eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
  851. usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
  852. }
  853. static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
  854. {
  855. int result = -ENOMEM;
  856. struct Scsi_Host *shost = NULL;
  857. struct uas_dev_info *devinfo;
  858. struct usb_device *udev = interface_to_usbdev(intf);
  859. u64 dev_flags;
  860. if (!uas_use_uas_driver(intf, id, &dev_flags))
  861. return -ENODEV;
  862. if (uas_switch_interface(udev, intf))
  863. return -ENODEV;
  864. shost = scsi_host_alloc(&uas_host_template,
  865. sizeof(struct uas_dev_info));
  866. if (!shost)
  867. goto set_alt0;
  868. shost->max_cmd_len = 16 + 252;
  869. shost->max_id = 1;
  870. shost->max_lun = 256;
  871. shost->max_channel = 0;
  872. shost->sg_tablesize = udev->bus->sg_tablesize;
  873. devinfo = (struct uas_dev_info *)shost->hostdata;
  874. devinfo->intf = intf;
  875. devinfo->udev = udev;
  876. devinfo->resetting = 0;
  877. devinfo->shutdown = 0;
  878. devinfo->flags = dev_flags;
  879. init_usb_anchor(&devinfo->cmd_urbs);
  880. init_usb_anchor(&devinfo->sense_urbs);
  881. init_usb_anchor(&devinfo->data_urbs);
  882. spin_lock_init(&devinfo->lock);
  883. INIT_WORK(&devinfo->work, uas_do_work);
  884. INIT_WORK(&devinfo->scan_work, uas_scan_work);
  885. result = uas_configure_endpoints(devinfo);
  886. if (result)
  887. goto set_alt0;
  888. /*
  889. * 1 tag is reserved for untagged commands +
  890. * 1 tag to avoid off by one errors in some bridge firmwares
  891. */
  892. shost->can_queue = devinfo->qdepth - 2;
  893. usb_set_intfdata(intf, shost);
  894. result = scsi_add_host(shost, &intf->dev);
  895. if (result)
  896. goto free_streams;
  897. /* Submit the delayed_work for SCSI-device scanning */
  898. schedule_work(&devinfo->scan_work);
  899. return result;
  900. free_streams:
  901. uas_free_streams(devinfo);
  902. usb_set_intfdata(intf, NULL);
  903. set_alt0:
  904. usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
  905. if (shost)
  906. scsi_host_put(shost);
  907. return result;
  908. }
  909. static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
  910. {
  911. unsigned long flags;
  912. int i, r = 1;
  913. spin_lock_irqsave(&devinfo->lock, flags);
  914. for (i = 0; i < devinfo->qdepth; i++) {
  915. if (devinfo->cmnd[i]) {
  916. r = 0; /* Not empty */
  917. break;
  918. }
  919. }
  920. spin_unlock_irqrestore(&devinfo->lock, flags);
  921. return r;
  922. }
  923. /*
  924. * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
  925. * get empty while there still is more work to do due to sense-urbs completing
  926. * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
  927. */
  928. static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
  929. {
  930. unsigned long start_time;
  931. int r;
  932. start_time = jiffies;
  933. do {
  934. flush_work(&devinfo->work);
  935. r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
  936. if (r == 0)
  937. return -ETIME;
  938. r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
  939. if (r == 0)
  940. return -ETIME;
  941. if (time_after(jiffies, start_time + 5 * HZ))
  942. return -ETIME;
  943. } while (!uas_cmnd_list_empty(devinfo));
  944. return 0;
  945. }
  946. static int uas_pre_reset(struct usb_interface *intf)
  947. {
  948. struct Scsi_Host *shost = usb_get_intfdata(intf);
  949. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  950. unsigned long flags;
  951. if (devinfo->shutdown)
  952. return 0;
  953. /* Block new requests */
  954. spin_lock_irqsave(shost->host_lock, flags);
  955. scsi_block_requests(shost);
  956. spin_unlock_irqrestore(shost->host_lock, flags);
  957. if (uas_wait_for_pending_cmnds(devinfo) != 0) {
  958. shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
  959. scsi_unblock_requests(shost);
  960. return 1;
  961. }
  962. uas_free_streams(devinfo);
  963. return 0;
  964. }
  965. static int uas_post_reset(struct usb_interface *intf)
  966. {
  967. struct Scsi_Host *shost = usb_get_intfdata(intf);
  968. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  969. unsigned long flags;
  970. int err;
  971. if (devinfo->shutdown)
  972. return 0;
  973. err = uas_configure_endpoints(devinfo);
  974. if (err && err != -ENODEV)
  975. shost_printk(KERN_ERR, shost,
  976. "%s: alloc streams error %d after reset",
  977. __func__, err);
  978. /* we must unblock the host in every case lest we deadlock */
  979. spin_lock_irqsave(shost->host_lock, flags);
  980. scsi_report_bus_reset(shost, 0);
  981. spin_unlock_irqrestore(shost->host_lock, flags);
  982. scsi_unblock_requests(shost);
  983. return err ? 1 : 0;
  984. }
  985. static int uas_suspend(struct usb_interface *intf, pm_message_t message)
  986. {
  987. struct Scsi_Host *shost = usb_get_intfdata(intf);
  988. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  989. if (uas_wait_for_pending_cmnds(devinfo) != 0) {
  990. shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
  991. return -ETIME;
  992. }
  993. return 0;
  994. }
  995. static int uas_resume(struct usb_interface *intf)
  996. {
  997. return 0;
  998. }
  999. static int uas_reset_resume(struct usb_interface *intf)
  1000. {
  1001. struct Scsi_Host *shost = usb_get_intfdata(intf);
  1002. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  1003. unsigned long flags;
  1004. int err;
  1005. err = uas_configure_endpoints(devinfo);
  1006. if (err) {
  1007. shost_printk(KERN_ERR, shost,
  1008. "%s: alloc streams error %d after reset",
  1009. __func__, err);
  1010. return -EIO;
  1011. }
  1012. spin_lock_irqsave(shost->host_lock, flags);
  1013. scsi_report_bus_reset(shost, 0);
  1014. spin_unlock_irqrestore(shost->host_lock, flags);
  1015. return 0;
  1016. }
  1017. static void uas_disconnect(struct usb_interface *intf)
  1018. {
  1019. struct Scsi_Host *shost = usb_get_intfdata(intf);
  1020. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&devinfo->lock, flags);
  1023. devinfo->resetting = 1;
  1024. spin_unlock_irqrestore(&devinfo->lock, flags);
  1025. cancel_work_sync(&devinfo->work);
  1026. usb_kill_anchored_urbs(&devinfo->cmd_urbs);
  1027. usb_kill_anchored_urbs(&devinfo->sense_urbs);
  1028. usb_kill_anchored_urbs(&devinfo->data_urbs);
  1029. uas_zap_pending(devinfo, DID_NO_CONNECT);
  1030. /*
  1031. * Prevent SCSI scanning (if it hasn't started yet)
  1032. * or wait for the SCSI-scanning routine to stop.
  1033. */
  1034. cancel_work_sync(&devinfo->scan_work);
  1035. scsi_remove_host(shost);
  1036. uas_free_streams(devinfo);
  1037. scsi_host_put(shost);
  1038. }
  1039. /*
  1040. * Put the device back in usb-storage mode on shutdown, as some BIOS-es
  1041. * hang on reboot when the device is still in uas mode. Note the reset is
  1042. * necessary as some devices won't revert to usb-storage mode without it.
  1043. */
  1044. static void uas_shutdown(struct usb_interface *intf)
  1045. {
  1046. struct usb_device *udev = interface_to_usbdev(intf);
  1047. struct Scsi_Host *shost = usb_get_intfdata(intf);
  1048. struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
  1049. if (system_state != SYSTEM_RESTART)
  1050. return;
  1051. devinfo->shutdown = 1;
  1052. uas_free_streams(devinfo);
  1053. usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
  1054. usb_reset_device(udev);
  1055. }
  1056. static struct usb_driver uas_driver = {
  1057. .name = "uas",
  1058. .probe = uas_probe,
  1059. .disconnect = uas_disconnect,
  1060. .pre_reset = uas_pre_reset,
  1061. .post_reset = uas_post_reset,
  1062. .suspend = uas_suspend,
  1063. .resume = uas_resume,
  1064. .reset_resume = uas_reset_resume,
  1065. .shutdown = uas_shutdown,
  1066. .id_table = uas_usb_ids,
  1067. };
  1068. static int __init uas_init(void)
  1069. {
  1070. int rv;
  1071. workqueue = alloc_workqueue("uas", WQ_MEM_RECLAIM, 0);
  1072. if (!workqueue)
  1073. return -ENOMEM;
  1074. rv = usb_register(&uas_driver);
  1075. if (rv) {
  1076. destroy_workqueue(workqueue);
  1077. return -ENOMEM;
  1078. }
  1079. return 0;
  1080. }
  1081. static void __exit uas_exit(void)
  1082. {
  1083. usb_deregister(&uas_driver);
  1084. destroy_workqueue(workqueue);
  1085. }
  1086. module_init(uas_init);
  1087. module_exit(uas_exit);
  1088. MODULE_DESCRIPTION("USB Attached SCSI driver");
  1089. MODULE_LICENSE("GPL");
  1090. MODULE_IMPORT_NS(USB_STORAGE);
  1091. MODULE_AUTHOR(
  1092. "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");