228-more_usb_fixes.patch 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. --- a/drivers/scsi/hosts.c
  2. +++ b/drivers/scsi/hosts.c
  3. @@ -107,8 +107,21 @@ scsi_unregister(struct Scsi_Host * sh){
  4. if (shn) shn->host_registered = 0;
  5. /* else {} : This should not happen, we should panic here... */
  6. + /* If we are removing the last host registered, it is safe to reuse
  7. + * its host number (this avoids "holes" at boot time) (DB)
  8. + * It is also safe to reuse those of numbers directly below which have
  9. + * been released earlier (to avoid some holes in numbering).
  10. + */
  11. + if(sh->host_no == max_scsi_hosts - 1) {
  12. + while(--max_scsi_hosts >= next_scsi_host) {
  13. + shpnt = scsi_hostlist;
  14. + while(shpnt && shpnt->host_no != max_scsi_hosts - 1)
  15. + shpnt = shpnt->next;
  16. + if(shpnt)
  17. + break;
  18. + }
  19. + }
  20. next_scsi_host--;
  21. -
  22. kfree((char *) sh);
  23. }
  24. --- a/drivers/usb/hcd.c
  25. +++ b/drivers/usb/hcd.c
  26. @@ -1105,7 +1105,8 @@ static int hcd_submit_urb (struct urb *u
  27. break;
  28. case PIPE_BULK:
  29. allowed |= USB_DISABLE_SPD | USB_QUEUE_BULK
  30. - | USB_ZERO_PACKET | URB_NO_INTERRUPT;
  31. + | USB_ZERO_PACKET | URB_NO_INTERRUPT
  32. + | URB_NO_TRANSFER_DMA_MAP;
  33. break;
  34. case PIPE_INTERRUPT:
  35. allowed |= USB_DISABLE_SPD;
  36. @@ -1212,7 +1213,8 @@ static int hcd_submit_urb (struct urb *u
  37. urb->setup_packet,
  38. sizeof (struct usb_ctrlrequest),
  39. PCI_DMA_TODEVICE);
  40. - if (urb->transfer_buffer_length != 0)
  41. + if (urb->transfer_buffer_length != 0
  42. + && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
  43. urb->transfer_dma = pci_map_single (
  44. hcd->pdev,
  45. urb->transfer_buffer,
  46. --- a/drivers/usb/host/ehci-hcd.c
  47. +++ b/drivers/usb/host/ehci-hcd.c
  48. @@ -399,6 +399,27 @@ static int ehci_start (struct usb_hcd *h
  49. ehci_mem_cleanup (ehci);
  50. return retval;
  51. }
  52. +
  53. +{
  54. + int misc_reg;
  55. + u32 vendor_id;
  56. +
  57. + pci_read_config_dword (ehci->hcd.pdev, PCI_VENDOR_ID, &vendor_id);
  58. + if (vendor_id == 0x31041106) {
  59. + /* VIA 6212 */
  60. + printk(KERN_INFO "EHCI: Enabling VIA 6212 workarounds\n", misc_reg);
  61. + pci_read_config_byte(ehci->hcd.pdev, 0x49, &misc_reg);
  62. + misc_reg &= ~0x20;
  63. + pci_write_config_byte(ehci->hcd.pdev, 0x49, misc_reg);
  64. + pci_read_config_byte(ehci->hcd.pdev, 0x49, &misc_reg);
  65. +
  66. + pci_read_config_byte(ehci->hcd.pdev, 0x4b, &misc_reg);
  67. + misc_reg |= 0x20;
  68. + pci_write_config_byte(ehci->hcd.pdev, 0x4b, misc_reg);
  69. + pci_read_config_byte(ehci->hcd.pdev, 0x4b, &misc_reg);
  70. + }
  71. +}
  72. +
  73. writel (INTR_MASK, &ehci->regs->intr_enable);
  74. writel (ehci->periodic_dma, &ehci->regs->frame_list);
  75. --- a/drivers/usb/host/ehci-q.c
  76. +++ b/drivers/usb/host/ehci-q.c
  77. @@ -791,6 +791,8 @@ static void qh_link_async (struct ehci_h
  78. writel (cmd, &ehci->regs->command);
  79. ehci->hcd.state = USB_STATE_RUNNING;
  80. /* posted write need not be known to HC yet ... */
  81. +
  82. + timer_action (ehci, TIMER_IO_WATCHDOG);
  83. }
  84. }
  85. --- a/drivers/usb/host/usb-uhci.c
  86. +++ b/drivers/usb/host/usb-uhci.c
  87. @@ -3034,6 +3034,21 @@ uhci_pci_probe (struct pci_dev *dev, con
  88. pci_set_master(dev);
  89. + {
  90. + u8 misc_reg;
  91. + u32 vendor_id;
  92. +
  93. + pci_read_config_dword (dev, PCI_VENDOR_ID, &vendor_id);
  94. + if (vendor_id == 0x30381106) {
  95. + /* VIA 6212 */
  96. + printk(KERN_INFO "UHCI: Enabling VIA 6212 workarounds\n");
  97. + pci_read_config_byte(dev, 0x41, &misc_reg);
  98. + misc_reg &= ~0x10;
  99. + pci_write_config_byte(dev, 0x41, misc_reg);
  100. + pci_read_config_byte(dev, 0x41, &misc_reg);
  101. + }
  102. + }
  103. +
  104. /* Search for the IO base address.. */
  105. for (i = 0; i < 6; i++) {
  106. --- a/drivers/usb/storage/transport.c
  107. +++ b/drivers/usb/storage/transport.c
  108. @@ -54,6 +54,22 @@
  109. #include <linux/sched.h>
  110. #include <linux/errno.h>
  111. #include <linux/slab.h>
  112. +#include <linux/pci.h>
  113. +#include "../hcd.h"
  114. +
  115. +/* These definitions mirror those in pci.h, so they can be used
  116. + * interchangeably with their PCI_ counterparts */
  117. +enum dma_data_direction {
  118. + DMA_BIDIRECTIONAL = 0,
  119. + DMA_TO_DEVICE = 1,
  120. + DMA_FROM_DEVICE = 2,
  121. + DMA_NONE = 3,
  122. +};
  123. +
  124. +#define dma_map_sg(d,s,n,dir) pci_map_sg(d,s,n,dir)
  125. +#define dma_unmap_sg(d,s,n,dir) pci_unmap_sg(d,s,n,dir)
  126. +
  127. +
  128. /***********************************************************************
  129. * Helper routines
  130. @@ -554,6 +570,543 @@ int usb_stor_transfer_partial(struct us_
  131. return US_BULK_TRANSFER_SHORT;
  132. }
  133. +/*-------------------------------------------------------------------*/
  134. +/**
  135. + * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
  136. + * @dev: device to which the scatterlist will be mapped
  137. + * @pipe: endpoint defining the mapping direction
  138. + * @sg: the scatterlist to unmap
  139. + * @n_hw_ents: the positive return value from usb_buffer_map_sg
  140. + *
  141. + * Reverses the effect of usb_buffer_map_sg().
  142. + */
  143. +static void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  144. + struct scatterlist *sg, int n_hw_ents)
  145. +{
  146. + struct usb_bus *bus;
  147. + struct usb_hcd *hcd;
  148. + struct pci_dev *pdev;
  149. +
  150. + if (!dev
  151. + || !(bus = dev->bus)
  152. + || !(hcd = bus->hcpriv)
  153. + || !(pdev = hcd->pdev)
  154. + || !pdev->dma_mask)
  155. + return;
  156. +
  157. + dma_unmap_sg (pdev, sg, n_hw_ents,
  158. + usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  159. +}
  160. +
  161. +/**
  162. + * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
  163. + * @dev: device to which the scatterlist will be mapped
  164. + * @pipe: endpoint defining the mapping direction
  165. + * @sg: the scatterlist to map
  166. + * @nents: the number of entries in the scatterlist
  167. + *
  168. + * Return value is either < 0 (indicating no buffers could be mapped), or
  169. + * the number of DMA mapping array entries in the scatterlist.
  170. + *
  171. + * The caller is responsible for placing the resulting DMA addresses from
  172. + * the scatterlist into URB transfer buffer pointers, and for setting the
  173. + * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
  174. + *
  175. + * Top I/O rates come from queuing URBs, instead of waiting for each one
  176. + * to complete before starting the next I/O. This is particularly easy
  177. + * to do with scatterlists. Just allocate and submit one URB for each DMA
  178. + * mapping entry returned, stopping on the first error or when all succeed.
  179. + * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
  180. + *
  181. + * This call would normally be used when translating scatterlist requests,
  182. + * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
  183. + * may be able to coalesce mappings for improved I/O efficiency.
  184. + *
  185. + * Reverse the effect of this call with usb_buffer_unmap_sg().
  186. + */
  187. +static int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  188. + struct scatterlist *sg, int nents)
  189. +{
  190. + struct usb_bus *bus;
  191. + struct usb_hcd *hcd;
  192. + struct pci_dev *pdev;
  193. +
  194. + if (!dev
  195. + || usb_pipecontrol (pipe)
  196. + || !(bus = dev->bus)
  197. + || !(hcd = bus->hcpriv)
  198. + || !(pdev = hcd->pdev)
  199. + || !pdev->dma_mask)
  200. + return -1;
  201. +
  202. + // FIXME generic api broken like pci, can't report errors
  203. + return dma_map_sg (pdev, sg, nents,
  204. + usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  205. +}
  206. +
  207. +static void sg_clean (struct usb_sg_request *io)
  208. +{
  209. + struct usb_hcd *hcd = io->dev->bus->hcpriv;
  210. + struct pci_dev *pdev = hcd->pdev;
  211. +
  212. + if (io->urbs) {
  213. + while (io->entries--)
  214. + usb_free_urb (io->urbs [io->entries]);
  215. + kfree (io->urbs);
  216. + io->urbs = 0;
  217. + }
  218. + if (pdev->dma_mask != 0)
  219. + usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
  220. + io->dev = 0;
  221. +}
  222. +
  223. +static void sg_complete (struct urb *urb)
  224. +{
  225. + struct usb_sg_request *io = (struct usb_sg_request *) urb->context;
  226. +
  227. + spin_lock (&io->lock);
  228. +
  229. + /* In 2.5 we require hcds' endpoint queues not to progress after fault
  230. + * reports, until the completion callback (this!) returns. That lets
  231. + * device driver code (like this routine) unlink queued urbs first,
  232. + * if it needs to, since the HC won't work on them at all. So it's
  233. + * not possible for page N+1 to overwrite page N, and so on.
  234. + *
  235. + * That's only for "hard" faults; "soft" faults (unlinks) sometimes
  236. + * complete before the HCD can get requests away from hardware,
  237. + * though never during cleanup after a hard fault.
  238. + */
  239. + if (io->status
  240. + && (io->status != -ECONNRESET
  241. + || urb->status != -ECONNRESET)
  242. + && urb->actual_length) {
  243. + US_DEBUGP("Error: %s ep%d%s scatterlist error %d/%d\n",
  244. + io->dev->devpath,
  245. + usb_pipeendpoint (urb->pipe),
  246. + usb_pipein (urb->pipe) ? "in" : "out",
  247. + urb->status, io->status);
  248. + // BUG ();
  249. + }
  250. +
  251. + if (urb->status && urb->status != -ECONNRESET) {
  252. + int i, found, status;
  253. +
  254. + io->status = urb->status;
  255. +
  256. + /* the previous urbs, and this one, completed already.
  257. + * unlink pending urbs so they won't rx/tx bad data.
  258. + */
  259. + for (i = 0, found = 0; i < io->entries; i++) {
  260. + if (!io->urbs [i])
  261. + continue;
  262. + if (found) {
  263. + status = usb_unlink_urb (io->urbs [i]);
  264. + if (status != -EINPROGRESS && status != -EBUSY)
  265. + US_DEBUGP("Error: %s, unlink --> %d\n", __FUNCTION__, status);
  266. + } else if (urb == io->urbs [i])
  267. + found = 1;
  268. + }
  269. + }
  270. + urb->dev = 0;
  271. +
  272. + /* on the last completion, signal usb_sg_wait() */
  273. + io->bytes += urb->actual_length;
  274. + io->count--;
  275. + if (!io->count)
  276. + complete (&io->complete);
  277. +
  278. + spin_unlock (&io->lock);
  279. +}
  280. +
  281. +/**
  282. + * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
  283. + * @io: request block being initialized. until usb_sg_wait() returns,
  284. + * treat this as a pointer to an opaque block of memory,
  285. + * @dev: the usb device that will send or receive the data
  286. + * @pipe: endpoint "pipe" used to transfer the data
  287. + * @period: polling rate for interrupt endpoints, in frames or
  288. + * (for high speed endpoints) microframes; ignored for bulk
  289. + * @sg: scatterlist entries
  290. + * @nents: how many entries in the scatterlist
  291. + * @length: how many bytes to send from the scatterlist, or zero to
  292. + * send every byte identified in the list.
  293. + * @mem_flags: SLAB_* flags affecting memory allocations in this call
  294. + *
  295. + * Returns zero for success, else a negative errno value. This initializes a
  296. + * scatter/gather request, allocating resources such as I/O mappings and urb
  297. + * memory (except maybe memory used by USB controller drivers).
  298. + *
  299. + * The request must be issued using usb_sg_wait(), which waits for the I/O to
  300. + * complete (or to be canceled) and then cleans up all resources allocated by
  301. + * usb_sg_init().
  302. + *
  303. + * The request may be canceled with usb_sg_cancel(), either before or after
  304. + * usb_sg_wait() is called.
  305. + */
  306. +int usb_sg_init (
  307. + struct usb_sg_request *io,
  308. + struct usb_device *dev,
  309. + unsigned pipe,
  310. + unsigned period,
  311. + struct scatterlist *sg,
  312. + int nents,
  313. + size_t length,
  314. + int mem_flags
  315. +)
  316. +{
  317. + int i;
  318. + int urb_flags;
  319. + int dma;
  320. + struct usb_hcd *hcd;
  321. +
  322. + hcd = dev->bus->hcpriv;
  323. +
  324. + if (!io || !dev || !sg
  325. + || usb_pipecontrol (pipe)
  326. + || usb_pipeisoc (pipe)
  327. + || nents <= 0)
  328. + return -EINVAL;
  329. +
  330. + spin_lock_init (&io->lock);
  331. + io->dev = dev;
  332. + io->pipe = pipe;
  333. + io->sg = sg;
  334. + io->nents = nents;
  335. +
  336. + /* not all host controllers use DMA (like the mainstream pci ones);
  337. + * they can use PIO (sl811) or be software over another transport.
  338. + */
  339. + dma = (hcd->pdev->dma_mask != 0);
  340. + if (dma)
  341. + io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
  342. + else
  343. + io->entries = nents;
  344. +
  345. + /* initialize all the urbs we'll use */
  346. + if (io->entries <= 0)
  347. + return io->entries;
  348. +
  349. + io->count = 0;
  350. + io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
  351. + if (!io->urbs)
  352. + goto nomem;
  353. +
  354. + urb_flags = USB_ASYNC_UNLINK | URB_NO_INTERRUPT | URB_NO_TRANSFER_DMA_MAP;
  355. + if (usb_pipein (pipe))
  356. + urb_flags |= URB_SHORT_NOT_OK;
  357. +
  358. + for (i = 0; i < io->entries; i++, io->count = i) {
  359. + unsigned len;
  360. +
  361. + io->urbs [i] = usb_alloc_urb (0);
  362. + if (!io->urbs [i]) {
  363. + io->entries = i;
  364. + goto nomem;
  365. + }
  366. +
  367. + io->urbs [i]->dev = 0;
  368. + io->urbs [i]->pipe = pipe;
  369. + io->urbs [i]->interval = period;
  370. + io->urbs [i]->transfer_flags = urb_flags;
  371. +
  372. + io->urbs [i]->complete = sg_complete;
  373. + io->urbs [i]->context = io;
  374. + io->urbs [i]->status = -EINPROGRESS;
  375. + io->urbs [i]->actual_length = 0;
  376. +
  377. + if (dma) {
  378. + /* hc may use _only_ transfer_dma */
  379. + io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
  380. + len = sg_dma_len (sg + i);
  381. + } else {
  382. + /* hc may use _only_ transfer_buffer */
  383. + io->urbs [i]->transfer_buffer =
  384. + page_address (sg [i].page) + sg [i].offset;
  385. + len = sg [i].length;
  386. + }
  387. +
  388. + if (length) {
  389. + len = min_t (unsigned, len, length);
  390. + length -= len;
  391. + if (length == 0)
  392. + io->entries = i + 1;
  393. + }
  394. + io->urbs [i]->transfer_buffer_length = len;
  395. + }
  396. + io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
  397. +
  398. + /* transaction state */
  399. + io->status = 0;
  400. + io->bytes = 0;
  401. + init_completion (&io->complete);
  402. + return 0;
  403. +
  404. +nomem:
  405. + sg_clean (io);
  406. + return -ENOMEM;
  407. +}
  408. +
  409. +/**
  410. + * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
  411. + * @io: request block, initialized with usb_sg_init()
  412. + *
  413. + * This stops a request after it has been started by usb_sg_wait().
  414. + * It can also prevents one initialized by usb_sg_init() from starting,
  415. + * so that call just frees resources allocated to the request.
  416. + */
  417. +void usb_sg_cancel (struct usb_sg_request *io)
  418. +{
  419. + unsigned long flags;
  420. +
  421. + spin_lock_irqsave (&io->lock, flags);
  422. +
  423. + /* shut everything down, if it didn't already */
  424. + if (!io->status) {
  425. + int i;
  426. +
  427. + io->status = -ECONNRESET;
  428. + for (i = 0; i < io->entries; i++) {
  429. + int retval;
  430. +
  431. + if (!io->urbs [i]->dev)
  432. + continue;
  433. + retval = usb_unlink_urb (io->urbs [i]);
  434. + if (retval != -EINPROGRESS && retval != -EBUSY)
  435. + US_DEBUGP("WARNING: %s, unlink --> %d\n", __FUNCTION__, retval);
  436. + }
  437. + }
  438. + spin_unlock_irqrestore (&io->lock, flags);
  439. +}
  440. +
  441. +/**
  442. + * usb_sg_wait - synchronously execute scatter/gather request
  443. + * @io: request block handle, as initialized with usb_sg_init().
  444. + * some fields become accessible when this call returns.
  445. + * Context: !in_interrupt ()
  446. + *
  447. + * This function blocks until the specified I/O operation completes. It
  448. + * leverages the grouping of the related I/O requests to get good transfer
  449. + * rates, by queueing the requests. At higher speeds, such queuing can
  450. + * significantly improve USB throughput.
  451. + *
  452. + * There are three kinds of completion for this function.
  453. + * (1) success, where io->status is zero. The number of io->bytes
  454. + * transferred is as requested.
  455. + * (2) error, where io->status is a negative errno value. The number
  456. + * of io->bytes transferred before the error is usually less
  457. + * than requested, and can be nonzero.
  458. + * (3) cancelation, a type of error with status -ECONNRESET that
  459. + * is initiated by usb_sg_cancel().
  460. + *
  461. + * When this function returns, all memory allocated through usb_sg_init() or
  462. + * this call will have been freed. The request block parameter may still be
  463. + * passed to usb_sg_cancel(), or it may be freed. It could also be
  464. + * reinitialized and then reused.
  465. + *
  466. + * Data Transfer Rates:
  467. + *
  468. + * Bulk transfers are valid for full or high speed endpoints.
  469. + * The best full speed data rate is 19 packets of 64 bytes each
  470. + * per frame, or 1216 bytes per millisecond.
  471. + * The best high speed data rate is 13 packets of 512 bytes each
  472. + * per microframe, or 52 KBytes per millisecond.
  473. + *
  474. + * The reason to use interrupt transfers through this API would most likely
  475. + * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
  476. + * could be transferred. That capability is less useful for low or full
  477. + * speed interrupt endpoints, which allow at most one packet per millisecond,
  478. + * of at most 8 or 64 bytes (respectively).
  479. + */
  480. +void usb_sg_wait (struct usb_sg_request *io)
  481. +{
  482. + int i, entries = io->entries;
  483. +
  484. + /* queue the urbs. */
  485. + spin_lock_irq (&io->lock);
  486. + for (i = 0; i < entries && !io->status; i++) {
  487. + int retval;
  488. +
  489. + io->urbs [i]->dev = io->dev;
  490. + retval = usb_submit_urb (io->urbs [i]);
  491. +
  492. + /* after we submit, let completions or cancelations fire;
  493. + * we handshake using io->status.
  494. + */
  495. + spin_unlock_irq (&io->lock);
  496. + switch (retval) {
  497. + /* maybe we retrying will recover */
  498. + case -ENXIO: // hc didn't queue this one
  499. + case -EAGAIN:
  500. + case -ENOMEM:
  501. + io->urbs [i]->dev = 0;
  502. + retval = 0;
  503. + i--;
  504. + yield ();
  505. + break;
  506. +
  507. + /* no error? continue immediately.
  508. + *
  509. + * NOTE: to work better with UHCI (4K I/O buffer may
  510. + * need 3K of TDs) it may be good to limit how many
  511. + * URBs are queued at once; N milliseconds?
  512. + */
  513. + case 0:
  514. + cpu_relax ();
  515. + break;
  516. +
  517. + /* fail any uncompleted urbs */
  518. + default:
  519. + spin_lock_irq (&io->lock);
  520. + io->count -= entries - i;
  521. + if (io->status == -EINPROGRESS)
  522. + io->status = retval;
  523. + if (io->count == 0)
  524. + complete (&io->complete);
  525. + spin_unlock_irq (&io->lock);
  526. +
  527. + io->urbs [i]->dev = 0;
  528. + io->urbs [i]->status = retval;
  529. +
  530. + US_DEBUGP("%s, submit --> %d\n", __FUNCTION__, retval);
  531. + usb_sg_cancel (io);
  532. + }
  533. + spin_lock_irq (&io->lock);
  534. + if (retval && io->status == -ECONNRESET)
  535. + io->status = retval;
  536. + }
  537. + spin_unlock_irq (&io->lock);
  538. +
  539. + /* OK, yes, this could be packaged as non-blocking.
  540. + * So could the submit loop above ... but it's easier to
  541. + * solve neither problem than to solve both!
  542. + */
  543. + wait_for_completion (&io->complete);
  544. +
  545. + sg_clean (io);
  546. +}
  547. +
  548. +/*
  549. + * Interpret the results of a URB transfer
  550. + *
  551. + * This function prints appropriate debugging messages, clears halts on
  552. + * non-control endpoints, and translates the status to the corresponding
  553. + * USB_STOR_XFER_xxx return code.
  554. + */
  555. +static int interpret_urb_result(struct us_data *us, unsigned int pipe,
  556. + unsigned int length, int result, unsigned int partial)
  557. +{
  558. + US_DEBUGP("Status code %d; transferred %u/%u\n",
  559. + result, partial, length);
  560. + switch (result) {
  561. +
  562. + /* no error code; did we send all the data? */
  563. + case 0:
  564. + if (partial != length) {
  565. + US_DEBUGP("-- short transfer\n");
  566. + return USB_STOR_XFER_SHORT;
  567. + }
  568. +
  569. + US_DEBUGP("-- transfer complete\n");
  570. + return USB_STOR_XFER_GOOD;
  571. +
  572. + /* stalled */
  573. + case -EPIPE:
  574. + /* for control endpoints, (used by CB[I]) a stall indicates
  575. + * a failed command */
  576. + if (usb_pipecontrol(pipe)) {
  577. + US_DEBUGP("-- stall on control pipe\n");
  578. + return USB_STOR_XFER_STALLED;
  579. + }
  580. +
  581. + /* for other sorts of endpoint, clear the stall */
  582. + US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
  583. + if (usb_stor_clear_halt(us, pipe) < 0)
  584. + return USB_STOR_XFER_ERROR;
  585. + return USB_STOR_XFER_STALLED;
  586. +
  587. + /* timeout or excessively long NAK */
  588. + case -ETIMEDOUT:
  589. + US_DEBUGP("-- timeout or NAK\n");
  590. + return USB_STOR_XFER_ERROR;
  591. +
  592. + /* babble - the device tried to send more than we wanted to read */
  593. + case -EOVERFLOW:
  594. + US_DEBUGP("-- babble\n");
  595. + return USB_STOR_XFER_LONG;
  596. +
  597. + /* the transfer was cancelled by abort, disconnect, or timeout */
  598. + case -ECONNRESET:
  599. + US_DEBUGP("-- transfer cancelled\n");
  600. + return USB_STOR_XFER_ERROR;
  601. +
  602. + /* short scatter-gather read transfer */
  603. + case -EREMOTEIO:
  604. + US_DEBUGP("-- short read transfer\n");
  605. + return USB_STOR_XFER_SHORT;
  606. +
  607. + /* abort or disconnect in progress */
  608. + case -EIO:
  609. + US_DEBUGP("-- abort or disconnect in progress\n");
  610. + return USB_STOR_XFER_ERROR;
  611. +
  612. + /* the catch-all error case */
  613. + default:
  614. + US_DEBUGP("-- unknown error\n");
  615. + return USB_STOR_XFER_ERROR;
  616. + }
  617. +}
  618. +
  619. +/*
  620. + * Transfer a scatter-gather list via bulk transfer
  621. + *
  622. + * This function does basically the same thing as usb_stor_bulk_msg()
  623. + * above, but it uses the usbcore scatter-gather library.
  624. + */
  625. +int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
  626. + struct scatterlist *sg, int num_sg, unsigned int length,
  627. + unsigned int *act_len)
  628. +{
  629. + int result;
  630. +
  631. + /* don't submit s-g requests during abort/disconnect processing */
  632. + if (us->flags & ABORTING_OR_DISCONNECTING)
  633. + return USB_STOR_XFER_ERROR;
  634. +
  635. + /* initialize the scatter-gather request block */
  636. + US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
  637. + length, num_sg);
  638. + result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
  639. + sg, num_sg, length, SLAB_NOIO);
  640. + if (result) {
  641. + US_DEBUGP("usb_sg_init returned %d\n", result);
  642. + return USB_STOR_XFER_ERROR;
  643. + }
  644. +
  645. + /* since the block has been initialized successfully, it's now
  646. + * okay to cancel it */
  647. + set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
  648. +
  649. + /* did an abort/disconnect occur during the submission? */
  650. + if (us->flags & ABORTING_OR_DISCONNECTING) {
  651. +
  652. + /* cancel the request, if it hasn't been cancelled already */
  653. + if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
  654. + US_DEBUGP("-- cancelling sg request\n");
  655. + usb_sg_cancel(&us->current_sg);
  656. + }
  657. + }
  658. +
  659. + /* wait for the completion of the transfer */
  660. + usb_sg_wait(&us->current_sg);
  661. + clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
  662. +
  663. + result = us->current_sg.status;
  664. + if (act_len)
  665. + *act_len = us->current_sg.bytes;
  666. + return interpret_urb_result(us, pipe, length, result,
  667. + us->current_sg.bytes);
  668. +}
  669. +
  670. /*
  671. * Transfer an entire SCSI command's worth of data payload over the bulk
  672. * pipe.
  673. @@ -569,6 +1122,8 @@ void usb_stor_transfer(Scsi_Cmnd *srb, s
  674. struct scatterlist *sg;
  675. unsigned int total_transferred = 0;
  676. unsigned int transfer_amount;
  677. + unsigned int partial;
  678. + unsigned int pipe;
  679. /* calculate how much we want to transfer */
  680. transfer_amount = usb_stor_transfer_length(srb);
  681. @@ -585,23 +1140,34 @@ void usb_stor_transfer(Scsi_Cmnd *srb, s
  682. * make the appropriate requests for each, until done
  683. */
  684. sg = (struct scatterlist *) srb->request_buffer;
  685. - for (i = 0; i < srb->use_sg; i++) {
  686. -
  687. - /* transfer the lesser of the next buffer or the
  688. - * remaining data */
  689. - if (transfer_amount - total_transferred >=
  690. - sg[i].length) {
  691. - result = usb_stor_transfer_partial(us,
  692. - sg[i].address, sg[i].length);
  693. - total_transferred += sg[i].length;
  694. - } else
  695. - result = usb_stor_transfer_partial(us,
  696. - sg[i].address,
  697. - transfer_amount - total_transferred);
  698. -
  699. - /* if we get an error, end the loop here */
  700. - if (result)
  701. - break;
  702. + if (us->pusb_dev->speed == USB_SPEED_HIGH) {
  703. + /* calculate the appropriate pipe information */
  704. + if (us->srb->sc_data_direction == SCSI_DATA_READ)
  705. + pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  706. + else
  707. + pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  708. + /* use the usb core scatter-gather primitives */
  709. + result = usb_stor_bulk_transfer_sglist(us, pipe,
  710. + sg, srb->use_sg, transfer_amount, &partial);
  711. + } else {
  712. + for (i = 0; i < srb->use_sg; i++) {
  713. +
  714. + /* transfer the lesser of the next buffer or the
  715. + * remaining data */
  716. + if (transfer_amount - total_transferred >=
  717. + sg[i].length) {
  718. + result = usb_stor_transfer_partial(us,
  719. + sg[i].address, sg[i].length);
  720. + total_transferred += sg[i].length;
  721. + } else
  722. + result = usb_stor_transfer_partial(us,
  723. + sg[i].address,
  724. + transfer_amount - total_transferred);
  725. +
  726. + /* if we get an error, end the loop here */
  727. + if (result)
  728. + break;
  729. + }
  730. }
  731. }
  732. else
  733. --- a/drivers/usb/storage/transport.h
  734. +++ b/drivers/usb/storage/transport.h
  735. @@ -127,6 +127,16 @@ struct bulk_cs_wrap {
  736. #define US_BULK_TRANSFER_ABORTED 3 /* transfer canceled */
  737. /*
  738. + * usb_stor_bulk_transfer_xxx() return codes, in order of severity
  739. + */
  740. +
  741. +#define USB_STOR_XFER_GOOD 0 /* good transfer */
  742. +#define USB_STOR_XFER_SHORT 1 /* transferred less than expected */
  743. +#define USB_STOR_XFER_STALLED 2 /* endpoint stalled */
  744. +#define USB_STOR_XFER_LONG 3 /* device tried to send too much */
  745. +#define USB_STOR_XFER_ERROR 4 /* transfer died in the middle */
  746. +
  747. +/*
  748. * Transport return codes
  749. */
  750. --- a/drivers/usb/storage/usb.h
  751. +++ b/drivers/usb/storage/usb.h
  752. @@ -111,6 +111,60 @@ typedef int (*trans_reset)(struct us_dat
  753. typedef void (*proto_cmnd)(Scsi_Cmnd*, struct us_data*);
  754. typedef void (*extra_data_destructor)(void *); /* extra data destructor */
  755. +/* Dynamic flag definitions: used in set_bit() etc. */
  756. +#define US_FLIDX_URB_ACTIVE 18 /* 0x00040000 current_urb is in use */
  757. +#define US_FLIDX_SG_ACTIVE 19 /* 0x00080000 current_sg is in use */
  758. +#define US_FLIDX_ABORTING 20 /* 0x00100000 abort is in progress */
  759. +#define US_FLIDX_DISCONNECTING 21 /* 0x00200000 disconnect in progress */
  760. +#define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \
  761. + (1UL << US_FLIDX_DISCONNECTING))
  762. +#define US_FLIDX_RESETTING 22 /* 0x00400000 device reset in progress */
  763. +
  764. +/* processing state machine states */
  765. +#define US_STATE_IDLE 1
  766. +#define US_STATE_RUNNING 2
  767. +#define US_STATE_RESETTING 3
  768. +#define US_STATE_ABORTING 4
  769. +
  770. +/**
  771. + * struct usb_sg_request - support for scatter/gather I/O
  772. + * @status: zero indicates success, else negative errno
  773. + * @bytes: counts bytes transferred.
  774. + *
  775. + * These requests are initialized using usb_sg_init(), and then are used
  776. + * as request handles passed to usb_sg_wait() or usb_sg_cancel(). Most
  777. + * members of the request object aren't for driver access.
  778. + *
  779. + * The status and bytecount values are valid only after usb_sg_wait()
  780. + * returns. If the status is zero, then the bytecount matches the total
  781. + * from the request.
  782. + *
  783. + * After an error completion, drivers may need to clear a halt condition
  784. + * on the endpoint.
  785. + */
  786. +struct usb_sg_request {
  787. + int status;
  788. + size_t bytes;
  789. +
  790. + /*
  791. + * members below are private to usbcore,
  792. + * and are not provided for driver access!
  793. + */
  794. + spinlock_t lock;
  795. +
  796. + struct usb_device *dev;
  797. + int pipe;
  798. + struct scatterlist *sg;
  799. + int nents;
  800. +
  801. + int entries;
  802. + struct urb **urbs;
  803. +
  804. + int count;
  805. + struct completion complete;
  806. +};
  807. +
  808. +
  809. /* we allocate one of these for every device that we remember */
  810. struct us_data {
  811. struct us_data *next; /* next device */
  812. @@ -171,6 +225,7 @@ struct us_data {
  813. struct urb *current_urb; /* non-int USB requests */
  814. struct completion current_done; /* the done flag */
  815. unsigned int tag; /* tag for bulk CBW/CSW */
  816. + struct usb_sg_request current_sg; /* scatter-gather req. */
  817. /* the semaphore for sleeping the control thread */
  818. struct semaphore sema; /* to sleep thread on */
  819. --- a/include/linux/usb.h
  820. +++ b/include/linux/usb.h
  821. @@ -483,6 +483,8 @@ struct usb_driver {
  822. #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt needed */
  823. /* ... less overhead for QUEUE_BULK */
  824. #define USB_TIMEOUT_KILLED 0x1000 // only set by HCD!
  825. +#define URB_NO_TRANSFER_DMA_MAP 0x0400 /* urb->transfer_dma valid on submit */
  826. +#define URB_NO_SETUP_DMA_MAP 0x0800 /* urb->setup_dma valid on submit */
  827. struct iso_packet_descriptor
  828. {