transport.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for USB Mass Storage compliant devices
  4. *
  5. * Current development and maintenance by:
  6. * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  7. *
  8. * Developed with the assistance of:
  9. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  10. * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
  11. * (c) 2002 Alan Stern <stern@rowland.org>
  12. *
  13. * Initial work by:
  14. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  15. *
  16. * This driver is based on the 'USB Mass Storage Class' document. This
  17. * describes in detail the protocol used to communicate with such
  18. * devices. Clearly, the designers had SCSI and ATAPI commands in
  19. * mind when they created this document. The commands are all very
  20. * similar to commands in the SCSI-II and ATAPI specifications.
  21. *
  22. * It is important to note that in a number of cases this class
  23. * exhibits class-specific exemptions from the USB specification.
  24. * Notably the usage of NAK, STALL and ACK differs from the norm, in
  25. * that they are used to communicate wait, failed and OK on commands.
  26. *
  27. * Also, for certain devices, the interrupt endpoint is used to convey
  28. * status of a command.
  29. */
  30. #include <linux/sched.h>
  31. #include <linux/gfp.h>
  32. #include <linux/errno.h>
  33. #include <linux/export.h>
  34. #include <linux/usb/quirks.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_eh.h>
  37. #include <scsi/scsi_device.h>
  38. #include "usb.h"
  39. #include "transport.h"
  40. #include "protocol.h"
  41. #include "scsiglue.h"
  42. #include "debug.h"
  43. #include <linux/blkdev.h>
  44. #include "../../scsi/sd.h"
  45. /***********************************************************************
  46. * Data transfer routines
  47. ***********************************************************************/
  48. /*
  49. * This is subtle, so pay attention:
  50. * ---------------------------------
  51. * We're very concerned about races with a command abort. Hanging this code
  52. * is a sure fire way to hang the kernel. (Note that this discussion applies
  53. * only to transactions resulting from a scsi queued-command, since only
  54. * these transactions are subject to a scsi abort. Other transactions, such
  55. * as those occurring during device-specific initialization, must be handled
  56. * by a separate code path.)
  57. *
  58. * The abort function (usb_storage_command_abort() in scsiglue.c) first
  59. * sets the machine state and the ABORTING bit in us->dflags to prevent
  60. * new URBs from being submitted. It then calls usb_stor_stop_transport()
  61. * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
  62. * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
  63. * bit is tested to see if the current_sg scatter-gather request needs to be
  64. * stopped. The timeout callback routine does much the same thing.
  65. *
  66. * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
  67. * prevent new URBs from being submitted, and usb_stor_stop_transport() is
  68. * called to stop any ongoing requests.
  69. *
  70. * The submit function first verifies that the submitting is allowed
  71. * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
  72. * completes without errors, and only then sets the URB_ACTIVE bit. This
  73. * prevents the stop_transport() function from trying to cancel the URB
  74. * while the submit call is underway. Next, the submit function must test
  75. * the flags to see if an abort or disconnect occurred during the submission
  76. * or before the URB_ACTIVE bit was set. If so, it's essential to cancel
  77. * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
  78. * is still set). Either way, the function must then wait for the URB to
  79. * finish. Note that the URB can still be in progress even after a call to
  80. * usb_unlink_urb() returns.
  81. *
  82. * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
  83. * either the stop_transport() function or the submitting function
  84. * is guaranteed to call usb_unlink_urb() for an active URB,
  85. * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
  86. * called more than once or from being called during usb_submit_urb().
  87. */
  88. /*
  89. * This is the completion handler which will wake us up when an URB
  90. * completes.
  91. */
  92. static void usb_stor_blocking_completion(struct urb *urb)
  93. {
  94. struct completion *urb_done_ptr = urb->context;
  95. complete(urb_done_ptr);
  96. }
  97. /*
  98. * This is the common part of the URB message submission code
  99. *
  100. * All URBs from the usb-storage driver involved in handling a queued scsi
  101. * command _must_ pass through this function (or something like it) for the
  102. * abort mechanisms to work properly.
  103. */
  104. static int usb_stor_msg_common(struct us_data *us, int timeout)
  105. {
  106. struct completion urb_done;
  107. long timeleft;
  108. int status;
  109. /* don't submit URBs during abort processing */
  110. if (test_bit(US_FLIDX_ABORTING, &us->dflags))
  111. return -EIO;
  112. /* set up data structures for the wakeup system */
  113. init_completion(&urb_done);
  114. /* fill the common fields in the URB */
  115. us->current_urb->context = &urb_done;
  116. us->current_urb->transfer_flags = 0;
  117. /*
  118. * we assume that if transfer_buffer isn't us->iobuf then it
  119. * hasn't been mapped for DMA. Yes, this is clunky, but it's
  120. * easier than always having the caller tell us whether the
  121. * transfer buffer has already been mapped.
  122. */
  123. if (us->current_urb->transfer_buffer == us->iobuf)
  124. us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  125. us->current_urb->transfer_dma = us->iobuf_dma;
  126. /* submit the URB */
  127. status = usb_submit_urb(us->current_urb, GFP_NOIO);
  128. if (status) {
  129. /* something went wrong */
  130. return status;
  131. }
  132. /*
  133. * since the URB has been submitted successfully, it's now okay
  134. * to cancel it
  135. */
  136. set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
  137. /* did an abort occur during the submission? */
  138. if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
  139. /* cancel the URB, if it hasn't been cancelled already */
  140. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
  141. usb_stor_dbg(us, "-- cancelling URB\n");
  142. usb_unlink_urb(us->current_urb);
  143. }
  144. }
  145. /* wait for the completion of the URB */
  146. timeleft = wait_for_completion_interruptible_timeout(
  147. &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
  148. clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
  149. if (timeleft <= 0) {
  150. usb_stor_dbg(us, "%s -- cancelling URB\n",
  151. timeleft == 0 ? "Timeout" : "Signal");
  152. usb_kill_urb(us->current_urb);
  153. }
  154. /* return the URB status */
  155. return us->current_urb->status;
  156. }
  157. /*
  158. * Transfer one control message, with timeouts, and allowing early
  159. * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
  160. */
  161. int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
  162. u8 request, u8 requesttype, u16 value, u16 index,
  163. void *data, u16 size, int timeout)
  164. {
  165. int status;
  166. usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  167. request, requesttype, value, index, size);
  168. /* fill in the devrequest structure */
  169. us->cr->bRequestType = requesttype;
  170. us->cr->bRequest = request;
  171. us->cr->wValue = cpu_to_le16(value);
  172. us->cr->wIndex = cpu_to_le16(index);
  173. us->cr->wLength = cpu_to_le16(size);
  174. /* fill and submit the URB */
  175. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  176. (unsigned char*) us->cr, data, size,
  177. usb_stor_blocking_completion, NULL);
  178. status = usb_stor_msg_common(us, timeout);
  179. /* return the actual length of the data transferred if no error */
  180. if (status == 0)
  181. status = us->current_urb->actual_length;
  182. return status;
  183. }
  184. EXPORT_SYMBOL_GPL(usb_stor_control_msg);
  185. /*
  186. * This is a version of usb_clear_halt() that allows early termination and
  187. * doesn't read the status from the device -- this is because some devices
  188. * crash their internal firmware when the status is requested after a halt.
  189. *
  190. * A definitive list of these 'bad' devices is too difficult to maintain or
  191. * make complete enough to be useful. This problem was first observed on the
  192. * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
  193. * MacOS nor Windows checks the status after clearing a halt.
  194. *
  195. * Since many vendors in this space limit their testing to interoperability
  196. * with these two OSes, specification violations like this one are common.
  197. */
  198. int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
  199. {
  200. int result;
  201. int endp = usb_pipeendpoint(pipe);
  202. if (usb_pipein (pipe))
  203. endp |= USB_DIR_IN;
  204. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  205. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
  206. USB_ENDPOINT_HALT, endp,
  207. NULL, 0, 3*HZ);
  208. if (result >= 0)
  209. usb_reset_endpoint(us->pusb_dev, endp);
  210. usb_stor_dbg(us, "result = %d\n", result);
  211. return result;
  212. }
  213. EXPORT_SYMBOL_GPL(usb_stor_clear_halt);
  214. /*
  215. * Interpret the results of a URB transfer
  216. *
  217. * This function prints appropriate debugging messages, clears halts on
  218. * non-control endpoints, and translates the status to the corresponding
  219. * USB_STOR_XFER_xxx return code.
  220. */
  221. static int interpret_urb_result(struct us_data *us, unsigned int pipe,
  222. unsigned int length, int result, unsigned int partial)
  223. {
  224. usb_stor_dbg(us, "Status code %d; transferred %u/%u\n",
  225. result, partial, length);
  226. switch (result) {
  227. /* no error code; did we send all the data? */
  228. case 0:
  229. if (partial != length) {
  230. usb_stor_dbg(us, "-- short transfer\n");
  231. return USB_STOR_XFER_SHORT;
  232. }
  233. usb_stor_dbg(us, "-- transfer complete\n");
  234. return USB_STOR_XFER_GOOD;
  235. /* stalled */
  236. case -EPIPE:
  237. /*
  238. * for control endpoints, (used by CB[I]) a stall indicates
  239. * a failed command
  240. */
  241. if (usb_pipecontrol(pipe)) {
  242. usb_stor_dbg(us, "-- stall on control pipe\n");
  243. return USB_STOR_XFER_STALLED;
  244. }
  245. /* for other sorts of endpoint, clear the stall */
  246. usb_stor_dbg(us, "clearing endpoint halt for pipe 0x%x\n",
  247. pipe);
  248. if (usb_stor_clear_halt(us, pipe) < 0)
  249. return USB_STOR_XFER_ERROR;
  250. return USB_STOR_XFER_STALLED;
  251. /* babble - the device tried to send more than we wanted to read */
  252. case -EOVERFLOW:
  253. usb_stor_dbg(us, "-- babble\n");
  254. return USB_STOR_XFER_LONG;
  255. /* the transfer was cancelled by abort, disconnect, or timeout */
  256. case -ECONNRESET:
  257. usb_stor_dbg(us, "-- transfer cancelled\n");
  258. return USB_STOR_XFER_ERROR;
  259. /* short scatter-gather read transfer */
  260. case -EREMOTEIO:
  261. usb_stor_dbg(us, "-- short read transfer\n");
  262. return USB_STOR_XFER_SHORT;
  263. /* abort or disconnect in progress */
  264. case -EIO:
  265. usb_stor_dbg(us, "-- abort or disconnect in progress\n");
  266. return USB_STOR_XFER_ERROR;
  267. /* the catch-all error case */
  268. default:
  269. usb_stor_dbg(us, "-- unknown error\n");
  270. return USB_STOR_XFER_ERROR;
  271. }
  272. }
  273. /*
  274. * Transfer one control message, without timeouts, but allowing early
  275. * termination. Return codes are USB_STOR_XFER_xxx.
  276. */
  277. int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
  278. u8 request, u8 requesttype, u16 value, u16 index,
  279. void *data, u16 size)
  280. {
  281. int result;
  282. usb_stor_dbg(us, "rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
  283. request, requesttype, value, index, size);
  284. /* fill in the devrequest structure */
  285. us->cr->bRequestType = requesttype;
  286. us->cr->bRequest = request;
  287. us->cr->wValue = cpu_to_le16(value);
  288. us->cr->wIndex = cpu_to_le16(index);
  289. us->cr->wLength = cpu_to_le16(size);
  290. /* fill and submit the URB */
  291. usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
  292. (unsigned char*) us->cr, data, size,
  293. usb_stor_blocking_completion, NULL);
  294. result = usb_stor_msg_common(us, 0);
  295. return interpret_urb_result(us, pipe, size, result,
  296. us->current_urb->actual_length);
  297. }
  298. EXPORT_SYMBOL_GPL(usb_stor_ctrl_transfer);
  299. /*
  300. * Receive one interrupt buffer, without timeouts, but allowing early
  301. * termination. Return codes are USB_STOR_XFER_xxx.
  302. *
  303. * This routine always uses us->recv_intr_pipe as the pipe and
  304. * us->ep_bInterval as the interrupt interval.
  305. */
  306. static int usb_stor_intr_transfer(struct us_data *us, void *buf,
  307. unsigned int length)
  308. {
  309. int result;
  310. unsigned int pipe = us->recv_intr_pipe;
  311. unsigned int maxp;
  312. usb_stor_dbg(us, "xfer %u bytes\n", length);
  313. /* calculate the max packet size */
  314. maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
  315. if (maxp > length)
  316. maxp = length;
  317. /* fill and submit the URB */
  318. usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
  319. maxp, usb_stor_blocking_completion, NULL,
  320. us->ep_bInterval);
  321. result = usb_stor_msg_common(us, 0);
  322. return interpret_urb_result(us, pipe, length, result,
  323. us->current_urb->actual_length);
  324. }
  325. /*
  326. * Transfer one buffer via bulk pipe, without timeouts, but allowing early
  327. * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
  328. * stalls during the transfer, the halt is automatically cleared.
  329. */
  330. int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
  331. void *buf, unsigned int length, unsigned int *act_len)
  332. {
  333. int result;
  334. usb_stor_dbg(us, "xfer %u bytes\n", length);
  335. /* fill and submit the URB */
  336. usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
  337. usb_stor_blocking_completion, NULL);
  338. result = usb_stor_msg_common(us, 0);
  339. /* store the actual length of the data transferred */
  340. if (act_len)
  341. *act_len = us->current_urb->actual_length;
  342. return interpret_urb_result(us, pipe, length, result,
  343. us->current_urb->actual_length);
  344. }
  345. EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_buf);
  346. /*
  347. * Transfer a scatter-gather list via bulk transfer
  348. *
  349. * This function does basically the same thing as usb_stor_bulk_transfer_buf()
  350. * above, but it uses the usbcore scatter-gather library.
  351. */
  352. static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
  353. struct scatterlist *sg, int num_sg, unsigned int length,
  354. unsigned int *act_len)
  355. {
  356. int result;
  357. /* don't submit s-g requests during abort processing */
  358. if (test_bit(US_FLIDX_ABORTING, &us->dflags))
  359. return USB_STOR_XFER_ERROR;
  360. /* initialize the scatter-gather request block */
  361. usb_stor_dbg(us, "xfer %u bytes, %d entries\n", length, num_sg);
  362. result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
  363. sg, num_sg, length, GFP_NOIO);
  364. if (result) {
  365. usb_stor_dbg(us, "usb_sg_init returned %d\n", result);
  366. return USB_STOR_XFER_ERROR;
  367. }
  368. /*
  369. * since the block has been initialized successfully, it's now
  370. * okay to cancel it
  371. */
  372. set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
  373. /* did an abort occur during the submission? */
  374. if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
  375. /* cancel the request, if it hasn't been cancelled already */
  376. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
  377. usb_stor_dbg(us, "-- cancelling sg request\n");
  378. usb_sg_cancel(&us->current_sg);
  379. }
  380. }
  381. /* wait for the completion of the transfer */
  382. usb_sg_wait(&us->current_sg);
  383. clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
  384. result = us->current_sg.status;
  385. if (act_len)
  386. *act_len = us->current_sg.bytes;
  387. return interpret_urb_result(us, pipe, length, result,
  388. us->current_sg.bytes);
  389. }
  390. /*
  391. * Common used function. Transfer a complete command
  392. * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
  393. */
  394. int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
  395. struct scsi_cmnd* srb)
  396. {
  397. unsigned int partial;
  398. int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
  399. scsi_sg_count(srb), scsi_bufflen(srb),
  400. &partial);
  401. scsi_set_resid(srb, scsi_bufflen(srb) - partial);
  402. return result;
  403. }
  404. EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
  405. /*
  406. * Transfer an entire SCSI command's worth of data payload over the bulk
  407. * pipe.
  408. *
  409. * Note that this uses usb_stor_bulk_transfer_buf() and
  410. * usb_stor_bulk_transfer_sglist() to achieve its goals --
  411. * this function simply determines whether we're going to use
  412. * scatter-gather or not, and acts appropriately.
  413. */
  414. int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
  415. void *buf, unsigned int length_left, int use_sg, int *residual)
  416. {
  417. int result;
  418. unsigned int partial;
  419. /* are we scatter-gathering? */
  420. if (use_sg) {
  421. /* use the usb core scatter-gather primitives */
  422. result = usb_stor_bulk_transfer_sglist(us, pipe,
  423. (struct scatterlist *) buf, use_sg,
  424. length_left, &partial);
  425. length_left -= partial;
  426. } else {
  427. /* no scatter-gather, just make the request */
  428. result = usb_stor_bulk_transfer_buf(us, pipe, buf,
  429. length_left, &partial);
  430. length_left -= partial;
  431. }
  432. /* store the residual and return the error code */
  433. if (residual)
  434. *residual = length_left;
  435. return result;
  436. }
  437. EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
  438. /***********************************************************************
  439. * Transport routines
  440. ***********************************************************************/
  441. /*
  442. * There are so many devices that report the capacity incorrectly,
  443. * this routine was written to counteract some of the resulting
  444. * problems.
  445. */
  446. static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
  447. {
  448. struct gendisk *disk;
  449. struct scsi_disk *sdkp;
  450. u32 sector;
  451. /* To Report "Medium Error: Record Not Found */
  452. static unsigned char record_not_found[18] = {
  453. [0] = 0x70, /* current error */
  454. [2] = MEDIUM_ERROR, /* = 0x03 */
  455. [7] = 0x0a, /* additional length */
  456. [12] = 0x14 /* Record Not Found */
  457. };
  458. /*
  459. * If last-sector problems can't occur, whether because the
  460. * capacity was already decremented or because the device is
  461. * known to report the correct capacity, then we don't need
  462. * to do anything.
  463. */
  464. if (!us->use_last_sector_hacks)
  465. return;
  466. /* Was this command a READ(10) or a WRITE(10)? */
  467. if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
  468. goto done;
  469. /* Did this command access the last sector? */
  470. sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
  471. (srb->cmnd[4] << 8) | (srb->cmnd[5]);
  472. disk = srb->request->rq_disk;
  473. if (!disk)
  474. goto done;
  475. sdkp = scsi_disk(disk);
  476. if (!sdkp)
  477. goto done;
  478. if (sector + 1 != sdkp->capacity)
  479. goto done;
  480. if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
  481. /*
  482. * The command succeeded. We know this device doesn't
  483. * have the last-sector bug, so stop checking it.
  484. */
  485. us->use_last_sector_hacks = 0;
  486. } else {
  487. /*
  488. * The command failed. Allow up to 3 retries in case this
  489. * is some normal sort of failure. After that, assume the
  490. * capacity is wrong and we're trying to access the sector
  491. * beyond the end. Replace the result code and sense data
  492. * with values that will cause the SCSI core to fail the
  493. * command immediately, instead of going into an infinite
  494. * (or even just a very long) retry loop.
  495. */
  496. if (++us->last_sector_retries < 3)
  497. return;
  498. srb->result = SAM_STAT_CHECK_CONDITION;
  499. memcpy(srb->sense_buffer, record_not_found,
  500. sizeof(record_not_found));
  501. }
  502. done:
  503. /*
  504. * Don't reset the retry counter for TEST UNIT READY commands,
  505. * because they get issued after device resets which might be
  506. * caused by a failed last-sector access.
  507. */
  508. if (srb->cmnd[0] != TEST_UNIT_READY)
  509. us->last_sector_retries = 0;
  510. }
  511. /*
  512. * Invoke the transport and basic error-handling/recovery methods
  513. *
  514. * This is used by the protocol layers to actually send the message to
  515. * the device and receive the response.
  516. */
  517. void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  518. {
  519. int need_auto_sense;
  520. int result;
  521. /* send the command to the transport layer */
  522. scsi_set_resid(srb, 0);
  523. result = us->transport(srb, us);
  524. /*
  525. * if the command gets aborted by the higher layers, we need to
  526. * short-circuit all other processing
  527. */
  528. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  529. usb_stor_dbg(us, "-- command was aborted\n");
  530. srb->result = DID_ABORT << 16;
  531. goto Handle_Errors;
  532. }
  533. /* if there is a transport error, reset and don't auto-sense */
  534. if (result == USB_STOR_TRANSPORT_ERROR) {
  535. usb_stor_dbg(us, "-- transport indicates error, resetting\n");
  536. srb->result = DID_ERROR << 16;
  537. goto Handle_Errors;
  538. }
  539. /* if the transport provided its own sense data, don't auto-sense */
  540. if (result == USB_STOR_TRANSPORT_NO_SENSE) {
  541. srb->result = SAM_STAT_CHECK_CONDITION;
  542. last_sector_hacks(us, srb);
  543. return;
  544. }
  545. srb->result = SAM_STAT_GOOD;
  546. /*
  547. * Determine if we need to auto-sense
  548. *
  549. * I normally don't use a flag like this, but it's almost impossible
  550. * to understand what's going on here if I don't.
  551. */
  552. need_auto_sense = 0;
  553. /*
  554. * If we're running the CB transport, which is incapable
  555. * of determining status on its own, we will auto-sense
  556. * unless the operation involved a data-in transfer. Devices
  557. * can signal most data-in errors by stalling the bulk-in pipe.
  558. */
  559. if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
  560. srb->sc_data_direction != DMA_FROM_DEVICE) {
  561. usb_stor_dbg(us, "-- CB transport device requiring auto-sense\n");
  562. need_auto_sense = 1;
  563. }
  564. /*
  565. * If we have a failure, we're going to do a REQUEST_SENSE
  566. * automatically. Note that we differentiate between a command
  567. * "failure" and an "error" in the transport mechanism.
  568. */
  569. if (result == USB_STOR_TRANSPORT_FAILED) {
  570. usb_stor_dbg(us, "-- transport indicates command failure\n");
  571. need_auto_sense = 1;
  572. }
  573. /*
  574. * Determine if this device is SAT by seeing if the
  575. * command executed successfully. Otherwise we'll have
  576. * to wait for at least one CHECK_CONDITION to determine
  577. * SANE_SENSE support
  578. */
  579. if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
  580. result == USB_STOR_TRANSPORT_GOOD &&
  581. !(us->fflags & US_FL_SANE_SENSE) &&
  582. !(us->fflags & US_FL_BAD_SENSE) &&
  583. !(srb->cmnd[2] & 0x20))) {
  584. usb_stor_dbg(us, "-- SAT supported, increasing auto-sense\n");
  585. us->fflags |= US_FL_SANE_SENSE;
  586. }
  587. /*
  588. * A short transfer on a command where we don't expect it
  589. * is unusual, but it doesn't mean we need to auto-sense.
  590. */
  591. if ((scsi_get_resid(srb) > 0) &&
  592. !((srb->cmnd[0] == REQUEST_SENSE) ||
  593. (srb->cmnd[0] == INQUIRY) ||
  594. (srb->cmnd[0] == MODE_SENSE) ||
  595. (srb->cmnd[0] == LOG_SENSE) ||
  596. (srb->cmnd[0] == MODE_SENSE_10))) {
  597. usb_stor_dbg(us, "-- unexpectedly short transfer\n");
  598. }
  599. /* Now, if we need to do the auto-sense, let's do it */
  600. if (need_auto_sense) {
  601. int temp_result;
  602. struct scsi_eh_save ses;
  603. int sense_size = US_SENSE_SIZE;
  604. struct scsi_sense_hdr sshdr;
  605. const u8 *scdd;
  606. u8 fm_ili;
  607. /* device supports and needs bigger sense buffer */
  608. if (us->fflags & US_FL_SANE_SENSE)
  609. sense_size = ~0;
  610. Retry_Sense:
  611. usb_stor_dbg(us, "Issuing auto-REQUEST_SENSE\n");
  612. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
  613. /* FIXME: we must do the protocol translation here */
  614. if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
  615. us->subclass == USB_SC_CYP_ATACB)
  616. srb->cmd_len = 6;
  617. else
  618. srb->cmd_len = 12;
  619. /* issue the auto-sense command */
  620. scsi_set_resid(srb, 0);
  621. temp_result = us->transport(us->srb, us);
  622. /* let's clean up right away */
  623. scsi_eh_restore_cmnd(srb, &ses);
  624. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  625. usb_stor_dbg(us, "-- auto-sense aborted\n");
  626. srb->result = DID_ABORT << 16;
  627. /* If SANE_SENSE caused this problem, disable it */
  628. if (sense_size != US_SENSE_SIZE) {
  629. us->fflags &= ~US_FL_SANE_SENSE;
  630. us->fflags |= US_FL_BAD_SENSE;
  631. }
  632. goto Handle_Errors;
  633. }
  634. /*
  635. * Some devices claim to support larger sense but fail when
  636. * trying to request it. When a transport failure happens
  637. * using US_FS_SANE_SENSE, we always retry with a standard
  638. * (small) sense request. This fixes some USB GSM modems
  639. */
  640. if (temp_result == USB_STOR_TRANSPORT_FAILED &&
  641. sense_size != US_SENSE_SIZE) {
  642. usb_stor_dbg(us, "-- auto-sense failure, retry small sense\n");
  643. sense_size = US_SENSE_SIZE;
  644. us->fflags &= ~US_FL_SANE_SENSE;
  645. us->fflags |= US_FL_BAD_SENSE;
  646. goto Retry_Sense;
  647. }
  648. /* Other failures */
  649. if (temp_result != USB_STOR_TRANSPORT_GOOD) {
  650. usb_stor_dbg(us, "-- auto-sense failure\n");
  651. /*
  652. * we skip the reset if this happens to be a
  653. * multi-target device, since failure of an
  654. * auto-sense is perfectly valid
  655. */
  656. srb->result = DID_ERROR << 16;
  657. if (!(us->fflags & US_FL_SCM_MULT_TARG))
  658. goto Handle_Errors;
  659. return;
  660. }
  661. /*
  662. * If the sense data returned is larger than 18-bytes then we
  663. * assume this device supports requesting more in the future.
  664. * The response code must be 70h through 73h inclusive.
  665. */
  666. if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
  667. !(us->fflags & US_FL_SANE_SENSE) &&
  668. !(us->fflags & US_FL_BAD_SENSE) &&
  669. (srb->sense_buffer[0] & 0x7C) == 0x70) {
  670. usb_stor_dbg(us, "-- SANE_SENSE support enabled\n");
  671. us->fflags |= US_FL_SANE_SENSE;
  672. /*
  673. * Indicate to the user that we truncated their sense
  674. * because we didn't know it supported larger sense.
  675. */
  676. usb_stor_dbg(us, "-- Sense data truncated to %i from %i\n",
  677. US_SENSE_SIZE,
  678. srb->sense_buffer[7] + 8);
  679. srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
  680. }
  681. scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  682. &sshdr);
  683. usb_stor_dbg(us, "-- Result from auto-sense is %d\n",
  684. temp_result);
  685. usb_stor_dbg(us, "-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
  686. sshdr.response_code, sshdr.sense_key,
  687. sshdr.asc, sshdr.ascq);
  688. #ifdef CONFIG_USB_STORAGE_DEBUG
  689. usb_stor_show_sense(us, sshdr.sense_key, sshdr.asc, sshdr.ascq);
  690. #endif
  691. /* set the result so the higher layers expect this data */
  692. srb->result = SAM_STAT_CHECK_CONDITION;
  693. scdd = scsi_sense_desc_find(srb->sense_buffer,
  694. SCSI_SENSE_BUFFERSIZE, 4);
  695. fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
  696. /*
  697. * We often get empty sense data. This could indicate that
  698. * everything worked or that there was an unspecified
  699. * problem. We have to decide which.
  700. */
  701. if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
  702. fm_ili == 0) {
  703. /*
  704. * If things are really okay, then let's show that.
  705. * Zero out the sense buffer so the higher layers
  706. * won't realize we did an unsolicited auto-sense.
  707. */
  708. if (result == USB_STOR_TRANSPORT_GOOD) {
  709. srb->result = SAM_STAT_GOOD;
  710. srb->sense_buffer[0] = 0x0;
  711. }
  712. /*
  713. * ATA-passthru commands use sense data to report
  714. * the command completion status, and often devices
  715. * return Check Condition status when nothing is
  716. * wrong.
  717. */
  718. else if (srb->cmnd[0] == ATA_16 ||
  719. srb->cmnd[0] == ATA_12) {
  720. /* leave the data alone */
  721. }
  722. /*
  723. * If there was a problem, report an unspecified
  724. * hardware error to prevent the higher layers from
  725. * entering an infinite retry loop.
  726. */
  727. else {
  728. srb->result = DID_ERROR << 16;
  729. if ((sshdr.response_code & 0x72) == 0x72)
  730. srb->sense_buffer[1] = HARDWARE_ERROR;
  731. else
  732. srb->sense_buffer[2] = HARDWARE_ERROR;
  733. }
  734. }
  735. }
  736. /*
  737. * Some devices don't work or return incorrect data the first
  738. * time they get a READ(10) command, or for the first READ(10)
  739. * after a media change. If the INITIAL_READ10 flag is set,
  740. * keep track of whether READ(10) commands succeed. If the
  741. * previous one succeeded and this one failed, set the REDO_READ10
  742. * flag to force a retry.
  743. */
  744. if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
  745. srb->cmnd[0] == READ_10)) {
  746. if (srb->result == SAM_STAT_GOOD) {
  747. set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  748. } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
  749. clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  750. set_bit(US_FLIDX_REDO_READ10, &us->dflags);
  751. }
  752. /*
  753. * Next, if the REDO_READ10 flag is set, return a result
  754. * code that will cause the SCSI core to retry the READ(10)
  755. * command immediately.
  756. */
  757. if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
  758. clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
  759. srb->result = DID_IMM_RETRY << 16;
  760. srb->sense_buffer[0] = 0;
  761. }
  762. }
  763. /* Did we transfer less than the minimum amount required? */
  764. if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
  765. scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
  766. srb->result = DID_ERROR << 16;
  767. last_sector_hacks(us, srb);
  768. return;
  769. /*
  770. * Error and abort processing: try to resynchronize with the device
  771. * by issuing a port reset. If that fails, try a class-specific
  772. * device reset.
  773. */
  774. Handle_Errors:
  775. /*
  776. * Set the RESETTING bit, and clear the ABORTING bit so that
  777. * the reset may proceed.
  778. */
  779. scsi_lock(us_to_host(us));
  780. set_bit(US_FLIDX_RESETTING, &us->dflags);
  781. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  782. scsi_unlock(us_to_host(us));
  783. /*
  784. * We must release the device lock because the pre_reset routine
  785. * will want to acquire it.
  786. */
  787. mutex_unlock(&us->dev_mutex);
  788. result = usb_stor_port_reset(us);
  789. mutex_lock(&us->dev_mutex);
  790. if (result < 0) {
  791. scsi_lock(us_to_host(us));
  792. usb_stor_report_device_reset(us);
  793. scsi_unlock(us_to_host(us));
  794. us->transport_reset(us);
  795. }
  796. clear_bit(US_FLIDX_RESETTING, &us->dflags);
  797. last_sector_hacks(us, srb);
  798. }
  799. /* Stop the current URB transfer */
  800. void usb_stor_stop_transport(struct us_data *us)
  801. {
  802. /*
  803. * If the state machine is blocked waiting for an URB,
  804. * let's wake it up. The test_and_clear_bit() call
  805. * guarantees that if a URB has just been submitted,
  806. * it won't be cancelled more than once.
  807. */
  808. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
  809. usb_stor_dbg(us, "-- cancelling URB\n");
  810. usb_unlink_urb(us->current_urb);
  811. }
  812. /* If we are waiting for a scatter-gather operation, cancel it. */
  813. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
  814. usb_stor_dbg(us, "-- cancelling sg request\n");
  815. usb_sg_cancel(&us->current_sg);
  816. }
  817. }
  818. /*
  819. * Control/Bulk and Control/Bulk/Interrupt transport
  820. */
  821. int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
  822. {
  823. unsigned int transfer_length = scsi_bufflen(srb);
  824. unsigned int pipe = 0;
  825. int result;
  826. /* COMMAND STAGE */
  827. /* let's send the command via the control pipe */
  828. /*
  829. * Command is sometime (f.e. after scsi_eh_prep_cmnd) on the stack.
  830. * Stack may be vmallocated. So no DMA for us. Make a copy.
  831. */
  832. memcpy(us->iobuf, srb->cmnd, srb->cmd_len);
  833. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  834. US_CBI_ADSC,
  835. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  836. us->ifnum, us->iobuf, srb->cmd_len);
  837. /* check the return code for the command */
  838. usb_stor_dbg(us, "Call to usb_stor_ctrl_transfer() returned %d\n",
  839. result);
  840. /* if we stalled the command, it means command failed */
  841. if (result == USB_STOR_XFER_STALLED) {
  842. return USB_STOR_TRANSPORT_FAILED;
  843. }
  844. /* Uh oh... serious problem here */
  845. if (result != USB_STOR_XFER_GOOD) {
  846. return USB_STOR_TRANSPORT_ERROR;
  847. }
  848. /* DATA STAGE */
  849. /* transfer the data payload for this command, if one exists*/
  850. if (transfer_length) {
  851. pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  852. us->recv_bulk_pipe : us->send_bulk_pipe;
  853. result = usb_stor_bulk_srb(us, pipe, srb);
  854. usb_stor_dbg(us, "CBI data stage result is 0x%x\n", result);
  855. /* if we stalled the data transfer it means command failed */
  856. if (result == USB_STOR_XFER_STALLED)
  857. return USB_STOR_TRANSPORT_FAILED;
  858. if (result > USB_STOR_XFER_STALLED)
  859. return USB_STOR_TRANSPORT_ERROR;
  860. }
  861. /* STATUS STAGE */
  862. /*
  863. * NOTE: CB does not have a status stage. Silly, I know. So
  864. * we have to catch this at a higher level.
  865. */
  866. if (us->protocol != USB_PR_CBI)
  867. return USB_STOR_TRANSPORT_GOOD;
  868. result = usb_stor_intr_transfer(us, us->iobuf, 2);
  869. usb_stor_dbg(us, "Got interrupt data (0x%x, 0x%x)\n",
  870. us->iobuf[0], us->iobuf[1]);
  871. if (result != USB_STOR_XFER_GOOD)
  872. return USB_STOR_TRANSPORT_ERROR;
  873. /*
  874. * UFI gives us ASC and ASCQ, like a request sense
  875. *
  876. * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
  877. * devices, so we ignore the information for those commands. Note
  878. * that this means we could be ignoring a real error on these
  879. * commands, but that can't be helped.
  880. */
  881. if (us->subclass == USB_SC_UFI) {
  882. if (srb->cmnd[0] == REQUEST_SENSE ||
  883. srb->cmnd[0] == INQUIRY)
  884. return USB_STOR_TRANSPORT_GOOD;
  885. if (us->iobuf[0])
  886. goto Failed;
  887. return USB_STOR_TRANSPORT_GOOD;
  888. }
  889. /*
  890. * If not UFI, we interpret the data as a result code
  891. * The first byte should always be a 0x0.
  892. *
  893. * Some bogus devices don't follow that rule. They stuff the ASC
  894. * into the first byte -- so if it's non-zero, call it a failure.
  895. */
  896. if (us->iobuf[0]) {
  897. usb_stor_dbg(us, "CBI IRQ data showed reserved bType 0x%x\n",
  898. us->iobuf[0]);
  899. goto Failed;
  900. }
  901. /* The second byte & 0x0F should be 0x0 for good, otherwise error */
  902. switch (us->iobuf[1] & 0x0F) {
  903. case 0x00:
  904. return USB_STOR_TRANSPORT_GOOD;
  905. case 0x01:
  906. goto Failed;
  907. }
  908. return USB_STOR_TRANSPORT_ERROR;
  909. /*
  910. * the CBI spec requires that the bulk pipe must be cleared
  911. * following any data-in/out command failure (section 2.4.3.1.3)
  912. */
  913. Failed:
  914. if (pipe)
  915. usb_stor_clear_halt(us, pipe);
  916. return USB_STOR_TRANSPORT_FAILED;
  917. }
  918. EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
  919. /*
  920. * Bulk only transport
  921. */
  922. /* Determine what the maximum LUN supported is */
  923. int usb_stor_Bulk_max_lun(struct us_data *us)
  924. {
  925. int result;
  926. /* issue the command */
  927. us->iobuf[0] = 0;
  928. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  929. US_BULK_GET_MAX_LUN,
  930. USB_DIR_IN | USB_TYPE_CLASS |
  931. USB_RECIP_INTERFACE,
  932. 0, us->ifnum, us->iobuf, 1, 10*HZ);
  933. usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
  934. result, us->iobuf[0]);
  935. /*
  936. * If we have a successful request, return the result if valid. The
  937. * CBW LUN field is 4 bits wide, so the value reported by the device
  938. * should fit into that.
  939. */
  940. if (result > 0) {
  941. if (us->iobuf[0] < 16) {
  942. return us->iobuf[0];
  943. } else {
  944. dev_info(&us->pusb_intf->dev,
  945. "Max LUN %d is not valid, using 0 instead",
  946. us->iobuf[0]);
  947. }
  948. }
  949. /*
  950. * Some devices don't like GetMaxLUN. They may STALL the control
  951. * pipe, they may return a zero-length result, they may do nothing at
  952. * all and timeout, or they may fail in even more bizarrely creative
  953. * ways. In these cases the best approach is to use the default
  954. * value: only one LUN.
  955. */
  956. return 0;
  957. }
  958. int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
  959. {
  960. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  961. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  962. unsigned int transfer_length = scsi_bufflen(srb);
  963. unsigned int residue;
  964. int result;
  965. int fake_sense = 0;
  966. unsigned int cswlen;
  967. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  968. /* Take care of BULK32 devices; set extra byte to 0 */
  969. if (unlikely(us->fflags & US_FL_BULK32)) {
  970. cbwlen = 32;
  971. us->iobuf[31] = 0;
  972. }
  973. /* set up the command wrapper */
  974. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  975. bcb->DataTransferLength = cpu_to_le32(transfer_length);
  976. bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
  977. US_BULK_FLAG_IN : 0;
  978. bcb->Tag = ++us->tag;
  979. bcb->Lun = srb->device->lun;
  980. if (us->fflags & US_FL_SCM_MULT_TARG)
  981. bcb->Lun |= srb->device->id << 4;
  982. bcb->Length = srb->cmd_len;
  983. /* copy the command payload */
  984. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  985. memcpy(bcb->CDB, srb->cmnd, bcb->Length);
  986. /* send it to out endpoint */
  987. usb_stor_dbg(us, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
  988. le32_to_cpu(bcb->Signature), bcb->Tag,
  989. le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
  990. (bcb->Lun >> 4), (bcb->Lun & 0x0F),
  991. bcb->Length);
  992. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  993. bcb, cbwlen, NULL);
  994. usb_stor_dbg(us, "Bulk command transfer result=%d\n", result);
  995. if (result != USB_STOR_XFER_GOOD)
  996. return USB_STOR_TRANSPORT_ERROR;
  997. /* DATA STAGE */
  998. /* send/receive data payload, if there is any */
  999. /*
  1000. * Some USB-IDE converter chips need a 100us delay between the
  1001. * command phase and the data phase. Some devices need a little
  1002. * more than that, probably because of clock rate inaccuracies.
  1003. */
  1004. if (unlikely(us->fflags & US_FL_GO_SLOW))
  1005. usleep_range(125, 150);
  1006. if (transfer_length) {
  1007. unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  1008. us->recv_bulk_pipe : us->send_bulk_pipe;
  1009. result = usb_stor_bulk_srb(us, pipe, srb);
  1010. usb_stor_dbg(us, "Bulk data transfer result 0x%x\n", result);
  1011. if (result == USB_STOR_XFER_ERROR)
  1012. return USB_STOR_TRANSPORT_ERROR;
  1013. /*
  1014. * If the device tried to send back more data than the
  1015. * amount requested, the spec requires us to transfer
  1016. * the CSW anyway. Since there's no point retrying the
  1017. * the command, we'll return fake sense data indicating
  1018. * Illegal Request, Invalid Field in CDB.
  1019. */
  1020. if (result == USB_STOR_XFER_LONG)
  1021. fake_sense = 1;
  1022. /*
  1023. * Sometimes a device will mistakenly skip the data phase
  1024. * and go directly to the status phase without sending a
  1025. * zero-length packet. If we get a 13-byte response here,
  1026. * check whether it really is a CSW.
  1027. */
  1028. if (result == USB_STOR_XFER_SHORT &&
  1029. srb->sc_data_direction == DMA_FROM_DEVICE &&
  1030. transfer_length - scsi_get_resid(srb) ==
  1031. US_BULK_CS_WRAP_LEN) {
  1032. struct scatterlist *sg = NULL;
  1033. unsigned int offset = 0;
  1034. if (usb_stor_access_xfer_buf((unsigned char *) bcs,
  1035. US_BULK_CS_WRAP_LEN, srb, &sg,
  1036. &offset, FROM_XFER_BUF) ==
  1037. US_BULK_CS_WRAP_LEN &&
  1038. bcs->Signature ==
  1039. cpu_to_le32(US_BULK_CS_SIGN)) {
  1040. usb_stor_dbg(us, "Device skipped data phase\n");
  1041. scsi_set_resid(srb, transfer_length);
  1042. goto skipped_data_phase;
  1043. }
  1044. }
  1045. }
  1046. /*
  1047. * See flow chart on pg 15 of the Bulk Only Transport spec for
  1048. * an explanation of how this code works.
  1049. */
  1050. /* get CSW for device status */
  1051. usb_stor_dbg(us, "Attempting to get CSW...\n");
  1052. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1053. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1054. /*
  1055. * Some broken devices add unnecessary zero-length packets to the
  1056. * end of their data transfers. Such packets show up as 0-length
  1057. * CSWs. If we encounter such a thing, try to read the CSW again.
  1058. */
  1059. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  1060. usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
  1061. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1062. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1063. }
  1064. /* did the attempt to read the CSW fail? */
  1065. if (result == USB_STOR_XFER_STALLED) {
  1066. /* get the status again */
  1067. usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
  1068. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1069. bcs, US_BULK_CS_WRAP_LEN, NULL);
  1070. }
  1071. /* if we still have a failure at this point, we're in trouble */
  1072. usb_stor_dbg(us, "Bulk status result = %d\n", result);
  1073. if (result != USB_STOR_XFER_GOOD)
  1074. return USB_STOR_TRANSPORT_ERROR;
  1075. skipped_data_phase:
  1076. /* check bulk status */
  1077. residue = le32_to_cpu(bcs->Residue);
  1078. usb_stor_dbg(us, "Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
  1079. le32_to_cpu(bcs->Signature), bcs->Tag,
  1080. residue, bcs->Status);
  1081. if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
  1082. bcs->Status > US_BULK_STAT_PHASE) {
  1083. usb_stor_dbg(us, "Bulk logical error\n");
  1084. return USB_STOR_TRANSPORT_ERROR;
  1085. }
  1086. /*
  1087. * Some broken devices report odd signatures, so we do not check them
  1088. * for validity against the spec. We store the first one we see,
  1089. * and check subsequent transfers for validity against this signature.
  1090. */
  1091. if (!us->bcs_signature) {
  1092. us->bcs_signature = bcs->Signature;
  1093. if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
  1094. usb_stor_dbg(us, "Learnt BCS signature 0x%08X\n",
  1095. le32_to_cpu(us->bcs_signature));
  1096. } else if (bcs->Signature != us->bcs_signature) {
  1097. usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
  1098. le32_to_cpu(bcs->Signature),
  1099. le32_to_cpu(us->bcs_signature));
  1100. return USB_STOR_TRANSPORT_ERROR;
  1101. }
  1102. /*
  1103. * try to compute the actual residue, based on how much data
  1104. * was really transferred and what the device tells us
  1105. */
  1106. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  1107. /*
  1108. * Heuristically detect devices that generate bogus residues
  1109. * by seeing what happens with INQUIRY and READ CAPACITY
  1110. * commands.
  1111. */
  1112. if (bcs->Status == US_BULK_STAT_OK &&
  1113. scsi_get_resid(srb) == 0 &&
  1114. ((srb->cmnd[0] == INQUIRY &&
  1115. transfer_length == 36) ||
  1116. (srb->cmnd[0] == READ_CAPACITY &&
  1117. transfer_length == 8))) {
  1118. us->fflags |= US_FL_IGNORE_RESIDUE;
  1119. } else {
  1120. residue = min(residue, transfer_length);
  1121. scsi_set_resid(srb, max(scsi_get_resid(srb),
  1122. (int) residue));
  1123. }
  1124. }
  1125. /* based on the status code, we report good or bad */
  1126. switch (bcs->Status) {
  1127. case US_BULK_STAT_OK:
  1128. /* device babbled -- return fake sense data */
  1129. if (fake_sense) {
  1130. memcpy(srb->sense_buffer,
  1131. usb_stor_sense_invalidCDB,
  1132. sizeof(usb_stor_sense_invalidCDB));
  1133. return USB_STOR_TRANSPORT_NO_SENSE;
  1134. }
  1135. /* command good -- note that data could be short */
  1136. return USB_STOR_TRANSPORT_GOOD;
  1137. case US_BULK_STAT_FAIL:
  1138. /* command failed */
  1139. return USB_STOR_TRANSPORT_FAILED;
  1140. case US_BULK_STAT_PHASE:
  1141. /*
  1142. * phase error -- note that a transport reset will be
  1143. * invoked by the invoke_transport() function
  1144. */
  1145. return USB_STOR_TRANSPORT_ERROR;
  1146. }
  1147. /* we should never get here, but if we do, we're in trouble */
  1148. return USB_STOR_TRANSPORT_ERROR;
  1149. }
  1150. EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
  1151. /***********************************************************************
  1152. * Reset routines
  1153. ***********************************************************************/
  1154. /*
  1155. * This is the common part of the device reset code.
  1156. *
  1157. * It's handy that every transport mechanism uses the control endpoint for
  1158. * resets.
  1159. *
  1160. * Basically, we send a reset with a 5-second timeout, so we don't get
  1161. * jammed attempting to do the reset.
  1162. */
  1163. static int usb_stor_reset_common(struct us_data *us,
  1164. u8 request, u8 requesttype,
  1165. u16 value, u16 index, void *data, u16 size)
  1166. {
  1167. int result;
  1168. int result2;
  1169. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1170. usb_stor_dbg(us, "No reset during disconnect\n");
  1171. return -EIO;
  1172. }
  1173. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  1174. request, requesttype, value, index, data, size,
  1175. 5*HZ);
  1176. if (result < 0) {
  1177. usb_stor_dbg(us, "Soft reset failed: %d\n", result);
  1178. return result;
  1179. }
  1180. /*
  1181. * Give the device some time to recover from the reset,
  1182. * but don't delay disconnect processing.
  1183. */
  1184. wait_event_interruptible_timeout(us->delay_wait,
  1185. test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
  1186. HZ*6);
  1187. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1188. usb_stor_dbg(us, "Reset interrupted by disconnect\n");
  1189. return -EIO;
  1190. }
  1191. usb_stor_dbg(us, "Soft reset: clearing bulk-in endpoint halt\n");
  1192. result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
  1193. usb_stor_dbg(us, "Soft reset: clearing bulk-out endpoint halt\n");
  1194. result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
  1195. /* return a result code based on the result of the clear-halts */
  1196. if (result >= 0)
  1197. result = result2;
  1198. if (result < 0)
  1199. usb_stor_dbg(us, "Soft reset failed\n");
  1200. else
  1201. usb_stor_dbg(us, "Soft reset done\n");
  1202. return result;
  1203. }
  1204. /* This issues a CB[I] Reset to the device in question */
  1205. #define CB_RESET_CMD_SIZE 12
  1206. int usb_stor_CB_reset(struct us_data *us)
  1207. {
  1208. memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
  1209. us->iobuf[0] = SEND_DIAGNOSTIC;
  1210. us->iobuf[1] = 4;
  1211. return usb_stor_reset_common(us, US_CBI_ADSC,
  1212. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1213. 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
  1214. }
  1215. EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
  1216. /*
  1217. * This issues a Bulk-only Reset to the device in question, including
  1218. * clearing the subsequent endpoint halts that may occur.
  1219. */
  1220. int usb_stor_Bulk_reset(struct us_data *us)
  1221. {
  1222. return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
  1223. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1224. 0, us->ifnum, NULL, 0);
  1225. }
  1226. EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
  1227. /*
  1228. * Issue a USB port reset to the device. The caller must not hold
  1229. * us->dev_mutex.
  1230. */
  1231. int usb_stor_port_reset(struct us_data *us)
  1232. {
  1233. int result;
  1234. /*for these devices we must use the class specific method */
  1235. if (us->pusb_dev->quirks & USB_QUIRK_RESET)
  1236. return -EPERM;
  1237. result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
  1238. if (result < 0)
  1239. usb_stor_dbg(us, "unable to lock device for reset: %d\n",
  1240. result);
  1241. else {
  1242. /* Were we disconnected while waiting for the lock? */
  1243. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1244. result = -EIO;
  1245. usb_stor_dbg(us, "No reset during disconnect\n");
  1246. } else {
  1247. result = usb_reset_device(us->pusb_dev);
  1248. usb_stor_dbg(us, "usb_reset_device returns %d\n",
  1249. result);
  1250. }
  1251. usb_unlock_device(us->pusb_dev);
  1252. }
  1253. return result;
  1254. }