uas.c 32 KB

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