transport.c 44 KB

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