uas.c 34 KB

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