transport.c 44 KB

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