transport.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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 ([email protected])
  7. *
  8. * Developed with the assistance of:
  9. * (c) 2000 David L. Brown, Jr. ([email protected])
  10. * (c) 2000 Stephen J. Gowdy ([email protected])
  11. * (c) 2002 Alan Stern <[email protected]>
  12. *
  13. * Initial work by:
  14. * (c) 1999 Michael Gee ([email protected])
  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);
  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. goto 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. goto 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. usb_stor_xfer_error:
  390. if (act_len)
  391. *act_len = 0;
  392. return USB_STOR_XFER_ERROR;
  393. }
  394. /*
  395. * Common used function. Transfer a complete command
  396. * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
  397. */
  398. int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
  399. struct scsi_cmnd* srb)
  400. {
  401. unsigned int partial;
  402. int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
  403. scsi_sg_count(srb), scsi_bufflen(srb),
  404. &partial);
  405. scsi_set_resid(srb, scsi_bufflen(srb) - partial);
  406. return result;
  407. }
  408. EXPORT_SYMBOL_GPL(usb_stor_bulk_srb);
  409. /*
  410. * Transfer an entire SCSI command's worth of data payload over the bulk
  411. * pipe.
  412. *
  413. * Note that this uses usb_stor_bulk_transfer_buf() and
  414. * usb_stor_bulk_transfer_sglist() to achieve its goals --
  415. * this function simply determines whether we're going to use
  416. * scatter-gather or not, and acts appropriately.
  417. */
  418. int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
  419. void *buf, unsigned int length_left, int use_sg, int *residual)
  420. {
  421. int result;
  422. unsigned int partial;
  423. /* are we scatter-gathering? */
  424. if (use_sg) {
  425. /* use the usb core scatter-gather primitives */
  426. result = usb_stor_bulk_transfer_sglist(us, pipe,
  427. (struct scatterlist *) buf, use_sg,
  428. length_left, &partial);
  429. length_left -= partial;
  430. } else {
  431. /* no scatter-gather, just make the request */
  432. result = usb_stor_bulk_transfer_buf(us, pipe, buf,
  433. length_left, &partial);
  434. length_left -= partial;
  435. }
  436. /* store the residual and return the error code */
  437. if (residual)
  438. *residual = length_left;
  439. return result;
  440. }
  441. EXPORT_SYMBOL_GPL(usb_stor_bulk_transfer_sg);
  442. /***********************************************************************
  443. * Transport routines
  444. ***********************************************************************/
  445. /*
  446. * There are so many devices that report the capacity incorrectly,
  447. * this routine was written to counteract some of the resulting
  448. * problems.
  449. */
  450. static void last_sector_hacks(struct us_data *us, struct scsi_cmnd *srb)
  451. {
  452. struct gendisk *disk;
  453. struct scsi_disk *sdkp;
  454. u32 sector;
  455. /* To Report "Medium Error: Record Not Found */
  456. static unsigned char record_not_found[18] = {
  457. [0] = 0x70, /* current error */
  458. [2] = MEDIUM_ERROR, /* = 0x03 */
  459. [7] = 0x0a, /* additional length */
  460. [12] = 0x14 /* Record Not Found */
  461. };
  462. /*
  463. * If last-sector problems can't occur, whether because the
  464. * capacity was already decremented or because the device is
  465. * known to report the correct capacity, then we don't need
  466. * to do anything.
  467. */
  468. if (!us->use_last_sector_hacks)
  469. return;
  470. /* Was this command a READ(10) or a WRITE(10)? */
  471. if (srb->cmnd[0] != READ_10 && srb->cmnd[0] != WRITE_10)
  472. goto done;
  473. /* Did this command access the last sector? */
  474. sector = (srb->cmnd[2] << 24) | (srb->cmnd[3] << 16) |
  475. (srb->cmnd[4] << 8) | (srb->cmnd[5]);
  476. disk = scsi_cmd_to_rq(srb)->q->disk;
  477. if (!disk)
  478. goto done;
  479. sdkp = scsi_disk(disk);
  480. if (!sdkp)
  481. goto done;
  482. if (sector + 1 != sdkp->capacity)
  483. goto done;
  484. if (srb->result == SAM_STAT_GOOD && scsi_get_resid(srb) == 0) {
  485. /*
  486. * The command succeeded. We know this device doesn't
  487. * have the last-sector bug, so stop checking it.
  488. */
  489. us->use_last_sector_hacks = 0;
  490. } else {
  491. /*
  492. * The command failed. Allow up to 3 retries in case this
  493. * is some normal sort of failure. After that, assume the
  494. * capacity is wrong and we're trying to access the sector
  495. * beyond the end. Replace the result code and sense data
  496. * with values that will cause the SCSI core to fail the
  497. * command immediately, instead of going into an infinite
  498. * (or even just a very long) retry loop.
  499. */
  500. if (++us->last_sector_retries < 3)
  501. return;
  502. srb->result = SAM_STAT_CHECK_CONDITION;
  503. memcpy(srb->sense_buffer, record_not_found,
  504. sizeof(record_not_found));
  505. }
  506. done:
  507. /*
  508. * Don't reset the retry counter for TEST UNIT READY commands,
  509. * because they get issued after device resets which might be
  510. * caused by a failed last-sector access.
  511. */
  512. if (srb->cmnd[0] != TEST_UNIT_READY)
  513. us->last_sector_retries = 0;
  514. }
  515. /*
  516. * Invoke the transport and basic error-handling/recovery methods
  517. *
  518. * This is used by the protocol layers to actually send the message to
  519. * the device and receive the response.
  520. */
  521. void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
  522. {
  523. int need_auto_sense;
  524. int result;
  525. /* send the command to the transport layer */
  526. scsi_set_resid(srb, 0);
  527. result = us->transport(srb, us);
  528. /*
  529. * if the command gets aborted by the higher layers, we need to
  530. * short-circuit all other processing
  531. */
  532. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  533. usb_stor_dbg(us, "-- command was aborted\n");
  534. srb->result = DID_ABORT << 16;
  535. goto Handle_Errors;
  536. }
  537. /* if there is a transport error, reset and don't auto-sense */
  538. if (result == USB_STOR_TRANSPORT_ERROR) {
  539. usb_stor_dbg(us, "-- transport indicates error, resetting\n");
  540. srb->result = DID_ERROR << 16;
  541. goto Handle_Errors;
  542. }
  543. /* if the transport provided its own sense data, don't auto-sense */
  544. if (result == USB_STOR_TRANSPORT_NO_SENSE) {
  545. srb->result = SAM_STAT_CHECK_CONDITION;
  546. last_sector_hacks(us, srb);
  547. return;
  548. }
  549. srb->result = SAM_STAT_GOOD;
  550. /*
  551. * Determine if we need to auto-sense
  552. *
  553. * I normally don't use a flag like this, but it's almost impossible
  554. * to understand what's going on here if I don't.
  555. */
  556. need_auto_sense = 0;
  557. /*
  558. * If we're running the CB transport, which is incapable
  559. * of determining status on its own, we will auto-sense
  560. * unless the operation involved a data-in transfer. Devices
  561. * can signal most data-in errors by stalling the bulk-in pipe.
  562. */
  563. if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
  564. srb->sc_data_direction != DMA_FROM_DEVICE) {
  565. usb_stor_dbg(us, "-- CB transport device requiring auto-sense\n");
  566. need_auto_sense = 1;
  567. }
  568. /* Some devices (Kindle) require another command after SYNC CACHE */
  569. if ((us->fflags & US_FL_SENSE_AFTER_SYNC) &&
  570. srb->cmnd[0] == SYNCHRONIZE_CACHE) {
  571. usb_stor_dbg(us, "-- sense after SYNC CACHE\n");
  572. need_auto_sense = 1;
  573. }
  574. /*
  575. * If we have a failure, we're going to do a REQUEST_SENSE
  576. * automatically. Note that we differentiate between a command
  577. * "failure" and an "error" in the transport mechanism.
  578. */
  579. if (result == USB_STOR_TRANSPORT_FAILED) {
  580. usb_stor_dbg(us, "-- transport indicates command failure\n");
  581. need_auto_sense = 1;
  582. }
  583. /*
  584. * Determine if this device is SAT by seeing if the
  585. * command executed successfully. Otherwise we'll have
  586. * to wait for at least one CHECK_CONDITION to determine
  587. * SANE_SENSE support
  588. */
  589. if (unlikely((srb->cmnd[0] == ATA_16 || srb->cmnd[0] == ATA_12) &&
  590. result == USB_STOR_TRANSPORT_GOOD &&
  591. !(us->fflags & US_FL_SANE_SENSE) &&
  592. !(us->fflags & US_FL_BAD_SENSE) &&
  593. !(srb->cmnd[2] & 0x20))) {
  594. usb_stor_dbg(us, "-- SAT supported, increasing auto-sense\n");
  595. us->fflags |= US_FL_SANE_SENSE;
  596. }
  597. /*
  598. * A short transfer on a command where we don't expect it
  599. * is unusual, but it doesn't mean we need to auto-sense.
  600. */
  601. if ((scsi_get_resid(srb) > 0) &&
  602. !((srb->cmnd[0] == REQUEST_SENSE) ||
  603. (srb->cmnd[0] == INQUIRY) ||
  604. (srb->cmnd[0] == MODE_SENSE) ||
  605. (srb->cmnd[0] == LOG_SENSE) ||
  606. (srb->cmnd[0] == MODE_SENSE_10))) {
  607. usb_stor_dbg(us, "-- unexpectedly short transfer\n");
  608. }
  609. /* Now, if we need to do the auto-sense, let's do it */
  610. if (need_auto_sense) {
  611. int temp_result;
  612. struct scsi_eh_save ses;
  613. int sense_size = US_SENSE_SIZE;
  614. struct scsi_sense_hdr sshdr;
  615. const u8 *scdd;
  616. u8 fm_ili;
  617. /* device supports and needs bigger sense buffer */
  618. if (us->fflags & US_FL_SANE_SENSE)
  619. sense_size = ~0;
  620. Retry_Sense:
  621. usb_stor_dbg(us, "Issuing auto-REQUEST_SENSE\n");
  622. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
  623. /* FIXME: we must do the protocol translation here */
  624. if (us->subclass == USB_SC_RBC || us->subclass == USB_SC_SCSI ||
  625. us->subclass == USB_SC_CYP_ATACB)
  626. srb->cmd_len = 6;
  627. else
  628. srb->cmd_len = 12;
  629. /* issue the auto-sense command */
  630. scsi_set_resid(srb, 0);
  631. temp_result = us->transport(us->srb, us);
  632. /* let's clean up right away */
  633. scsi_eh_restore_cmnd(srb, &ses);
  634. if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
  635. usb_stor_dbg(us, "-- auto-sense aborted\n");
  636. srb->result = DID_ABORT << 16;
  637. /* If SANE_SENSE caused this problem, disable it */
  638. if (sense_size != US_SENSE_SIZE) {
  639. us->fflags &= ~US_FL_SANE_SENSE;
  640. us->fflags |= US_FL_BAD_SENSE;
  641. }
  642. goto Handle_Errors;
  643. }
  644. /*
  645. * Some devices claim to support larger sense but fail when
  646. * trying to request it. When a transport failure happens
  647. * using US_FS_SANE_SENSE, we always retry with a standard
  648. * (small) sense request. This fixes some USB GSM modems
  649. */
  650. if (temp_result == USB_STOR_TRANSPORT_FAILED &&
  651. sense_size != US_SENSE_SIZE) {
  652. usb_stor_dbg(us, "-- auto-sense failure, retry small sense\n");
  653. sense_size = US_SENSE_SIZE;
  654. us->fflags &= ~US_FL_SANE_SENSE;
  655. us->fflags |= US_FL_BAD_SENSE;
  656. goto Retry_Sense;
  657. }
  658. /* Other failures */
  659. if (temp_result != USB_STOR_TRANSPORT_GOOD) {
  660. usb_stor_dbg(us, "-- auto-sense failure\n");
  661. /*
  662. * we skip the reset if this happens to be a
  663. * multi-target device, since failure of an
  664. * auto-sense is perfectly valid
  665. */
  666. srb->result = DID_ERROR << 16;
  667. if (!(us->fflags & US_FL_SCM_MULT_TARG))
  668. goto Handle_Errors;
  669. return;
  670. }
  671. /*
  672. * If the sense data returned is larger than 18-bytes then we
  673. * assume this device supports requesting more in the future.
  674. * The response code must be 70h through 73h inclusive.
  675. */
  676. if (srb->sense_buffer[7] > (US_SENSE_SIZE - 8) &&
  677. !(us->fflags & US_FL_SANE_SENSE) &&
  678. !(us->fflags & US_FL_BAD_SENSE) &&
  679. (srb->sense_buffer[0] & 0x7C) == 0x70) {
  680. usb_stor_dbg(us, "-- SANE_SENSE support enabled\n");
  681. us->fflags |= US_FL_SANE_SENSE;
  682. /*
  683. * Indicate to the user that we truncated their sense
  684. * because we didn't know it supported larger sense.
  685. */
  686. usb_stor_dbg(us, "-- Sense data truncated to %i from %i\n",
  687. US_SENSE_SIZE,
  688. srb->sense_buffer[7] + 8);
  689. srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
  690. }
  691. scsi_normalize_sense(srb->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  692. &sshdr);
  693. usb_stor_dbg(us, "-- Result from auto-sense is %d\n",
  694. temp_result);
  695. usb_stor_dbg(us, "-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
  696. sshdr.response_code, sshdr.sense_key,
  697. sshdr.asc, sshdr.ascq);
  698. #ifdef CONFIG_USB_STORAGE_DEBUG
  699. usb_stor_show_sense(us, sshdr.sense_key, sshdr.asc, sshdr.ascq);
  700. #endif
  701. /* set the result so the higher layers expect this data */
  702. srb->result = SAM_STAT_CHECK_CONDITION;
  703. scdd = scsi_sense_desc_find(srb->sense_buffer,
  704. SCSI_SENSE_BUFFERSIZE, 4);
  705. fm_ili = (scdd ? scdd[3] : srb->sense_buffer[2]) & 0xA0;
  706. /*
  707. * We often get empty sense data. This could indicate that
  708. * everything worked or that there was an unspecified
  709. * problem. We have to decide which.
  710. */
  711. if (sshdr.sense_key == 0 && sshdr.asc == 0 && sshdr.ascq == 0 &&
  712. fm_ili == 0) {
  713. /*
  714. * If things are really okay, then let's show that.
  715. * Zero out the sense buffer so the higher layers
  716. * won't realize we did an unsolicited auto-sense.
  717. */
  718. if (result == USB_STOR_TRANSPORT_GOOD) {
  719. srb->result = SAM_STAT_GOOD;
  720. srb->sense_buffer[0] = 0x0;
  721. }
  722. /*
  723. * ATA-passthru commands use sense data to report
  724. * the command completion status, and often devices
  725. * return Check Condition status when nothing is
  726. * wrong.
  727. */
  728. else if (srb->cmnd[0] == ATA_16 ||
  729. srb->cmnd[0] == ATA_12) {
  730. /* leave the data alone */
  731. }
  732. /*
  733. * If there was a problem, report an unspecified
  734. * hardware error to prevent the higher layers from
  735. * entering an infinite retry loop.
  736. */
  737. else {
  738. srb->result = DID_ERROR << 16;
  739. if ((sshdr.response_code & 0x72) == 0x72)
  740. srb->sense_buffer[1] = HARDWARE_ERROR;
  741. else
  742. srb->sense_buffer[2] = HARDWARE_ERROR;
  743. }
  744. }
  745. }
  746. /*
  747. * Some devices don't work or return incorrect data the first
  748. * time they get a READ(10) command, or for the first READ(10)
  749. * after a media change. If the INITIAL_READ10 flag is set,
  750. * keep track of whether READ(10) commands succeed. If the
  751. * previous one succeeded and this one failed, set the REDO_READ10
  752. * flag to force a retry.
  753. */
  754. if (unlikely((us->fflags & US_FL_INITIAL_READ10) &&
  755. srb->cmnd[0] == READ_10)) {
  756. if (srb->result == SAM_STAT_GOOD) {
  757. set_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  758. } else if (test_bit(US_FLIDX_READ10_WORKED, &us->dflags)) {
  759. clear_bit(US_FLIDX_READ10_WORKED, &us->dflags);
  760. set_bit(US_FLIDX_REDO_READ10, &us->dflags);
  761. }
  762. /*
  763. * Next, if the REDO_READ10 flag is set, return a result
  764. * code that will cause the SCSI core to retry the READ(10)
  765. * command immediately.
  766. */
  767. if (test_bit(US_FLIDX_REDO_READ10, &us->dflags)) {
  768. clear_bit(US_FLIDX_REDO_READ10, &us->dflags);
  769. srb->result = DID_IMM_RETRY << 16;
  770. srb->sense_buffer[0] = 0;
  771. }
  772. }
  773. /* Did we transfer less than the minimum amount required? */
  774. if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
  775. scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
  776. srb->result = DID_ERROR << 16;
  777. last_sector_hacks(us, srb);
  778. return;
  779. /*
  780. * Error and abort processing: try to resynchronize with the device
  781. * by issuing a port reset. If that fails, try a class-specific
  782. * device reset.
  783. */
  784. Handle_Errors:
  785. /*
  786. * Set the RESETTING bit, and clear the ABORTING bit so that
  787. * the reset may proceed.
  788. */
  789. scsi_lock(us_to_host(us));
  790. set_bit(US_FLIDX_RESETTING, &us->dflags);
  791. clear_bit(US_FLIDX_ABORTING, &us->dflags);
  792. scsi_unlock(us_to_host(us));
  793. /*
  794. * We must release the device lock because the pre_reset routine
  795. * will want to acquire it.
  796. */
  797. mutex_unlock(&us->dev_mutex);
  798. result = usb_stor_port_reset(us);
  799. mutex_lock(&us->dev_mutex);
  800. if (result < 0) {
  801. scsi_lock(us_to_host(us));
  802. usb_stor_report_device_reset(us);
  803. scsi_unlock(us_to_host(us));
  804. us->transport_reset(us);
  805. }
  806. clear_bit(US_FLIDX_RESETTING, &us->dflags);
  807. last_sector_hacks(us, srb);
  808. }
  809. /* Stop the current URB transfer */
  810. void usb_stor_stop_transport(struct us_data *us)
  811. {
  812. /*
  813. * If the state machine is blocked waiting for an URB,
  814. * let's wake it up. The test_and_clear_bit() call
  815. * guarantees that if a URB has just been submitted,
  816. * it won't be cancelled more than once.
  817. */
  818. if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
  819. usb_stor_dbg(us, "-- cancelling URB\n");
  820. usb_unlink_urb(us->current_urb);
  821. }
  822. /* If we are waiting for a scatter-gather operation, cancel it. */
  823. if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
  824. usb_stor_dbg(us, "-- cancelling sg request\n");
  825. usb_sg_cancel(&us->current_sg);
  826. }
  827. }
  828. /*
  829. * Control/Bulk and Control/Bulk/Interrupt transport
  830. */
  831. int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
  832. {
  833. unsigned int transfer_length = scsi_bufflen(srb);
  834. unsigned int pipe = 0;
  835. int result;
  836. /* COMMAND STAGE */
  837. /* let's send the command via the control pipe */
  838. /*
  839. * Command is sometime (f.e. after scsi_eh_prep_cmnd) on the stack.
  840. * Stack may be vmallocated. So no DMA for us. Make a copy.
  841. */
  842. memcpy(us->iobuf, srb->cmnd, srb->cmd_len);
  843. result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  844. US_CBI_ADSC,
  845. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  846. us->ifnum, us->iobuf, srb->cmd_len);
  847. /* check the return code for the command */
  848. usb_stor_dbg(us, "Call to usb_stor_ctrl_transfer() returned %d\n",
  849. result);
  850. /* if we stalled the command, it means command failed */
  851. if (result == USB_STOR_XFER_STALLED) {
  852. return USB_STOR_TRANSPORT_FAILED;
  853. }
  854. /* Uh oh... serious problem here */
  855. if (result != USB_STOR_XFER_GOOD) {
  856. return USB_STOR_TRANSPORT_ERROR;
  857. }
  858. /* DATA STAGE */
  859. /* transfer the data payload for this command, if one exists*/
  860. if (transfer_length) {
  861. pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  862. us->recv_bulk_pipe : us->send_bulk_pipe;
  863. result = usb_stor_bulk_srb(us, pipe, srb);
  864. usb_stor_dbg(us, "CBI data stage result is 0x%x\n", result);
  865. /* if we stalled the data transfer it means command failed */
  866. if (result == USB_STOR_XFER_STALLED)
  867. return USB_STOR_TRANSPORT_FAILED;
  868. if (result > USB_STOR_XFER_STALLED)
  869. return USB_STOR_TRANSPORT_ERROR;
  870. }
  871. /* STATUS STAGE */
  872. /*
  873. * NOTE: CB does not have a status stage. Silly, I know. So
  874. * we have to catch this at a higher level.
  875. */
  876. if (us->protocol != USB_PR_CBI)
  877. return USB_STOR_TRANSPORT_GOOD;
  878. result = usb_stor_intr_transfer(us, us->iobuf, 2);
  879. usb_stor_dbg(us, "Got interrupt data (0x%x, 0x%x)\n",
  880. us->iobuf[0], us->iobuf[1]);
  881. if (result != USB_STOR_XFER_GOOD)
  882. return USB_STOR_TRANSPORT_ERROR;
  883. /*
  884. * UFI gives us ASC and ASCQ, like a request sense
  885. *
  886. * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
  887. * devices, so we ignore the information for those commands. Note
  888. * that this means we could be ignoring a real error on these
  889. * commands, but that can't be helped.
  890. */
  891. if (us->subclass == USB_SC_UFI) {
  892. if (srb->cmnd[0] == REQUEST_SENSE ||
  893. srb->cmnd[0] == INQUIRY)
  894. return USB_STOR_TRANSPORT_GOOD;
  895. if (us->iobuf[0])
  896. goto Failed;
  897. return USB_STOR_TRANSPORT_GOOD;
  898. }
  899. /*
  900. * If not UFI, we interpret the data as a result code
  901. * The first byte should always be a 0x0.
  902. *
  903. * Some bogus devices don't follow that rule. They stuff the ASC
  904. * into the first byte -- so if it's non-zero, call it a failure.
  905. */
  906. if (us->iobuf[0]) {
  907. usb_stor_dbg(us, "CBI IRQ data showed reserved bType 0x%x\n",
  908. us->iobuf[0]);
  909. goto Failed;
  910. }
  911. /* The second byte & 0x0F should be 0x0 for good, otherwise error */
  912. switch (us->iobuf[1] & 0x0F) {
  913. case 0x00:
  914. return USB_STOR_TRANSPORT_GOOD;
  915. case 0x01:
  916. goto Failed;
  917. }
  918. return USB_STOR_TRANSPORT_ERROR;
  919. /*
  920. * the CBI spec requires that the bulk pipe must be cleared
  921. * following any data-in/out command failure (section 2.4.3.1.3)
  922. */
  923. Failed:
  924. if (pipe)
  925. usb_stor_clear_halt(us, pipe);
  926. return USB_STOR_TRANSPORT_FAILED;
  927. }
  928. EXPORT_SYMBOL_GPL(usb_stor_CB_transport);
  929. /*
  930. * Bulk only transport
  931. */
  932. /* Determine what the maximum LUN supported is */
  933. int usb_stor_Bulk_max_lun(struct us_data *us)
  934. {
  935. int result;
  936. /* issue the command */
  937. us->iobuf[0] = 0;
  938. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  939. US_BULK_GET_MAX_LUN,
  940. USB_DIR_IN | USB_TYPE_CLASS |
  941. USB_RECIP_INTERFACE,
  942. 0, us->ifnum, us->iobuf, 1, 10*HZ);
  943. usb_stor_dbg(us, "GetMaxLUN command result is %d, data is %d\n",
  944. result, us->iobuf[0]);
  945. /*
  946. * If we have a successful request, return the result if valid. The
  947. * CBW LUN field is 4 bits wide, so the value reported by the device
  948. * should fit into that.
  949. */
  950. if (result > 0) {
  951. if (us->iobuf[0] < 16) {
  952. return us->iobuf[0];
  953. } else {
  954. dev_info(&us->pusb_intf->dev,
  955. "Max LUN %d is not valid, using 0 instead",
  956. us->iobuf[0]);
  957. }
  958. }
  959. /*
  960. * Some devices don't like GetMaxLUN. They may STALL the control
  961. * pipe, they may return a zero-length result, they may do nothing at
  962. * all and timeout, or they may fail in even more bizarrely creative
  963. * ways. In these cases the best approach is to use the default
  964. * value: only one LUN.
  965. */
  966. return 0;
  967. }
  968. int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
  969. {
  970. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  971. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  972. unsigned int transfer_length = scsi_bufflen(srb);
  973. unsigned int residue;
  974. int result;
  975. int fake_sense = 0;
  976. unsigned int cswlen;
  977. unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
  978. /* Take care of BULK32 devices; set extra byte to 0 */
  979. if (unlikely(us->fflags & US_FL_BULK32)) {
  980. cbwlen = 32;
  981. us->iobuf[31] = 0;
  982. }
  983. /* set up the command wrapper */
  984. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  985. bcb->DataTransferLength = cpu_to_le32(transfer_length);
  986. bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ?
  987. US_BULK_FLAG_IN : 0;
  988. bcb->Tag = ++us->tag;
  989. bcb->Lun = srb->device->lun;
  990. if (us->fflags & US_FL_SCM_MULT_TARG)
  991. bcb->Lun |= srb->device->id << 4;
  992. bcb->Length = srb->cmd_len;
  993. /* copy the command payload */
  994. memset(bcb->CDB, 0, sizeof(bcb->CDB));
  995. memcpy(bcb->CDB, srb->cmnd, bcb->Length);
  996. /* send it to out endpoint */
  997. usb_stor_dbg(us, "Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
  998. le32_to_cpu(bcb->Signature), bcb->Tag,
  999. le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
  1000. (bcb->Lun >> 4), (bcb->Lun & 0x0F),
  1001. bcb->Length);
  1002. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  1003. bcb, cbwlen, NULL);
  1004. usb_stor_dbg(us, "Bulk command transfer result=%d\n", result);
  1005. if (result != USB_STOR_XFER_GOOD)
  1006. return USB_STOR_TRANSPORT_ERROR;
  1007. /* DATA STAGE */
  1008. /* send/receive data payload, if there is any */
  1009. /*
  1010. * Some USB-IDE converter chips need a 100us delay between the
  1011. * command phase and the data phase. Some devices need a little
  1012. * more than that, probably because of clock rate inaccuracies.
  1013. */
  1014. if (unlikely(us->fflags & US_FL_GO_SLOW))
  1015. usleep_range(125, 150);
  1016. if (transfer_length) {
  1017. unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
  1018. us->recv_bulk_pipe : us->send_bulk_pipe;
  1019. result = usb_stor_bulk_srb(us, pipe, srb);
  1020. usb_stor_dbg(us, "Bulk data transfer result 0x%x\n", result);
  1021. if (result == USB_STOR_XFER_ERROR)
  1022. return USB_STOR_TRANSPORT_ERROR;
  1023. /*
  1024. * If the device tried to send back more data than the
  1025. * amount requested, the spec requires us to transfer
  1026. * the CSW anyway. Since there's no point retrying
  1027. * the command, we'll return fake sense data indicating
  1028. * Illegal Request, Invalid Field in CDB.
  1029. */
  1030. if (result == USB_STOR_XFER_LONG)
  1031. fake_sense = 1;
  1032. /*
  1033. * Sometimes a device will mistakenly skip the data phase
  1034. * and go directly to the status phase without sending a
  1035. * zero-length packet. If we get a 13-byte response here,
  1036. * check whether it really is a CSW.
  1037. */
  1038. if (result == USB_STOR_XFER_SHORT &&
  1039. srb->sc_data_direction == DMA_FROM_DEVICE &&
  1040. transfer_length - scsi_get_resid(srb) ==
  1041. US_BULK_CS_WRAP_LEN) {
  1042. struct scatterlist *sg = NULL;
  1043. unsigned int offset = 0;
  1044. if (usb_stor_access_xfer_buf((unsigned char *) bcs,
  1045. US_BULK_CS_WRAP_LEN, srb, &sg,
  1046. &offset, FROM_XFER_BUF) ==
  1047. US_BULK_CS_WRAP_LEN &&
  1048. bcs->Signature ==
  1049. cpu_to_le32(US_BULK_CS_SIGN)) {
  1050. usb_stor_dbg(us, "Device skipped data phase\n");
  1051. scsi_set_resid(srb, transfer_length);
  1052. goto skipped_data_phase;
  1053. }
  1054. }
  1055. }
  1056. /*
  1057. * See flow chart on pg 15 of the Bulk Only Transport spec for
  1058. * an explanation of how this code works.
  1059. */
  1060. /* get CSW for device status */
  1061. usb_stor_dbg(us, "Attempting to get CSW...\n");
  1062. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1063. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1064. /*
  1065. * Some broken devices add unnecessary zero-length packets to the
  1066. * end of their data transfers. Such packets show up as 0-length
  1067. * CSWs. If we encounter such a thing, try to read the CSW again.
  1068. */
  1069. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  1070. usb_stor_dbg(us, "Received 0-length CSW; retrying...\n");
  1071. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1072. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  1073. }
  1074. /* did the attempt to read the CSW fail? */
  1075. if (result == USB_STOR_XFER_STALLED) {
  1076. /* get the status again */
  1077. usb_stor_dbg(us, "Attempting to get CSW (2nd try)...\n");
  1078. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  1079. bcs, US_BULK_CS_WRAP_LEN, NULL);
  1080. }
  1081. /* if we still have a failure at this point, we're in trouble */
  1082. usb_stor_dbg(us, "Bulk status result = %d\n", result);
  1083. if (result != USB_STOR_XFER_GOOD)
  1084. return USB_STOR_TRANSPORT_ERROR;
  1085. skipped_data_phase:
  1086. /* check bulk status */
  1087. residue = le32_to_cpu(bcs->Residue);
  1088. usb_stor_dbg(us, "Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
  1089. le32_to_cpu(bcs->Signature), bcs->Tag,
  1090. residue, bcs->Status);
  1091. if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
  1092. bcs->Status > US_BULK_STAT_PHASE) {
  1093. usb_stor_dbg(us, "Bulk logical error\n");
  1094. return USB_STOR_TRANSPORT_ERROR;
  1095. }
  1096. /*
  1097. * Some broken devices report odd signatures, so we do not check them
  1098. * for validity against the spec. We store the first one we see,
  1099. * and check subsequent transfers for validity against this signature.
  1100. */
  1101. if (!us->bcs_signature) {
  1102. us->bcs_signature = bcs->Signature;
  1103. if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
  1104. usb_stor_dbg(us, "Learnt BCS signature 0x%08X\n",
  1105. le32_to_cpu(us->bcs_signature));
  1106. } else if (bcs->Signature != us->bcs_signature) {
  1107. usb_stor_dbg(us, "Signature mismatch: got %08X, expecting %08X\n",
  1108. le32_to_cpu(bcs->Signature),
  1109. le32_to_cpu(us->bcs_signature));
  1110. return USB_STOR_TRANSPORT_ERROR;
  1111. }
  1112. /*
  1113. * try to compute the actual residue, based on how much data
  1114. * was really transferred and what the device tells us
  1115. */
  1116. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  1117. /*
  1118. * Heuristically detect devices that generate bogus residues
  1119. * by seeing what happens with INQUIRY and READ CAPACITY
  1120. * commands.
  1121. */
  1122. if (bcs->Status == US_BULK_STAT_OK &&
  1123. scsi_get_resid(srb) == 0 &&
  1124. ((srb->cmnd[0] == INQUIRY &&
  1125. transfer_length == 36) ||
  1126. (srb->cmnd[0] == READ_CAPACITY &&
  1127. transfer_length == 8))) {
  1128. us->fflags |= US_FL_IGNORE_RESIDUE;
  1129. } else {
  1130. residue = min(residue, transfer_length);
  1131. scsi_set_resid(srb, max(scsi_get_resid(srb), residue));
  1132. }
  1133. }
  1134. /* based on the status code, we report good or bad */
  1135. switch (bcs->Status) {
  1136. case US_BULK_STAT_OK:
  1137. /* device babbled -- return fake sense data */
  1138. if (fake_sense) {
  1139. memcpy(srb->sense_buffer,
  1140. usb_stor_sense_invalidCDB,
  1141. sizeof(usb_stor_sense_invalidCDB));
  1142. return USB_STOR_TRANSPORT_NO_SENSE;
  1143. }
  1144. /* command good -- note that data could be short */
  1145. return USB_STOR_TRANSPORT_GOOD;
  1146. case US_BULK_STAT_FAIL:
  1147. /* command failed */
  1148. return USB_STOR_TRANSPORT_FAILED;
  1149. case US_BULK_STAT_PHASE:
  1150. /*
  1151. * phase error -- note that a transport reset will be
  1152. * invoked by the invoke_transport() function
  1153. */
  1154. return USB_STOR_TRANSPORT_ERROR;
  1155. }
  1156. /* we should never get here, but if we do, we're in trouble */
  1157. return USB_STOR_TRANSPORT_ERROR;
  1158. }
  1159. EXPORT_SYMBOL_GPL(usb_stor_Bulk_transport);
  1160. /***********************************************************************
  1161. * Reset routines
  1162. ***********************************************************************/
  1163. /*
  1164. * This is the common part of the device reset code.
  1165. *
  1166. * It's handy that every transport mechanism uses the control endpoint for
  1167. * resets.
  1168. *
  1169. * Basically, we send a reset with a 5-second timeout, so we don't get
  1170. * jammed attempting to do the reset.
  1171. */
  1172. static int usb_stor_reset_common(struct us_data *us,
  1173. u8 request, u8 requesttype,
  1174. u16 value, u16 index, void *data, u16 size)
  1175. {
  1176. int result;
  1177. int result2;
  1178. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1179. usb_stor_dbg(us, "No reset during disconnect\n");
  1180. return -EIO;
  1181. }
  1182. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  1183. request, requesttype, value, index, data, size,
  1184. 5*HZ);
  1185. if (result < 0) {
  1186. usb_stor_dbg(us, "Soft reset failed: %d\n", result);
  1187. return result;
  1188. }
  1189. /*
  1190. * Give the device some time to recover from the reset,
  1191. * but don't delay disconnect processing.
  1192. */
  1193. wait_event_interruptible_timeout(us->delay_wait,
  1194. test_bit(US_FLIDX_DISCONNECTING, &us->dflags),
  1195. HZ*6);
  1196. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1197. usb_stor_dbg(us, "Reset interrupted by disconnect\n");
  1198. return -EIO;
  1199. }
  1200. usb_stor_dbg(us, "Soft reset: clearing bulk-in endpoint halt\n");
  1201. result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
  1202. usb_stor_dbg(us, "Soft reset: clearing bulk-out endpoint halt\n");
  1203. result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
  1204. /* return a result code based on the result of the clear-halts */
  1205. if (result >= 0)
  1206. result = result2;
  1207. if (result < 0)
  1208. usb_stor_dbg(us, "Soft reset failed\n");
  1209. else
  1210. usb_stor_dbg(us, "Soft reset done\n");
  1211. return result;
  1212. }
  1213. /* This issues a CB[I] Reset to the device in question */
  1214. #define CB_RESET_CMD_SIZE 12
  1215. int usb_stor_CB_reset(struct us_data *us)
  1216. {
  1217. memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
  1218. us->iobuf[0] = SEND_DIAGNOSTIC;
  1219. us->iobuf[1] = 4;
  1220. return usb_stor_reset_common(us, US_CBI_ADSC,
  1221. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1222. 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
  1223. }
  1224. EXPORT_SYMBOL_GPL(usb_stor_CB_reset);
  1225. /*
  1226. * This issues a Bulk-only Reset to the device in question, including
  1227. * clearing the subsequent endpoint halts that may occur.
  1228. */
  1229. int usb_stor_Bulk_reset(struct us_data *us)
  1230. {
  1231. return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
  1232. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1233. 0, us->ifnum, NULL, 0);
  1234. }
  1235. EXPORT_SYMBOL_GPL(usb_stor_Bulk_reset);
  1236. /*
  1237. * Issue a USB port reset to the device. The caller must not hold
  1238. * us->dev_mutex.
  1239. */
  1240. int usb_stor_port_reset(struct us_data *us)
  1241. {
  1242. int result;
  1243. /*for these devices we must use the class specific method */
  1244. if (us->pusb_dev->quirks & USB_QUIRK_RESET)
  1245. return -EPERM;
  1246. result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
  1247. if (result < 0)
  1248. usb_stor_dbg(us, "unable to lock device for reset: %d\n",
  1249. result);
  1250. else {
  1251. /* Were we disconnected while waiting for the lock? */
  1252. if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
  1253. result = -EIO;
  1254. usb_stor_dbg(us, "No reset during disconnect\n");
  1255. } else {
  1256. result = usb_reset_device(us->pusb_dev);
  1257. usb_stor_dbg(us, "usb_reset_device returns %d\n",
  1258. result);
  1259. }
  1260. usb_unlock_device(us->pusb_dev);
  1261. }
  1262. return result;
  1263. }