transport.c 44 KB

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