uas.c 34 KB

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