usb-rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * USB RX handling
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *
  35. * Intel Corporation <linux-wimax@intel.com>
  36. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  37. * - Initial implementation
  38. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  39. * - Use skb_clone(), break up processing in chunks
  40. * - Split transport/device specific
  41. * - Make buffer size dynamic to exert less memory pressure
  42. *
  43. *
  44. * This handles the RX path on USB.
  45. *
  46. * When a notification is received that says 'there is RX data ready',
  47. * we call i2400mu_rx_kick(); that wakes up the RX kthread, which
  48. * reads a buffer from USB and passes it to i2400m_rx() in the generic
  49. * handling code. The RX buffer has an specific format that is
  50. * described in rx.c.
  51. *
  52. * We use a kernel thread in a loop because:
  53. *
  54. * - we want to be able to call the USB power management get/put
  55. * functions (blocking) before each transaction.
  56. *
  57. * - We might get a lot of notifications and we don't want to submit
  58. * a zillion reads; by serializing, we are throttling.
  59. *
  60. * - RX data processing can get heavy enough so that it is not
  61. * appropriate for doing it in the USB callback; thus we run it in a
  62. * process context.
  63. *
  64. * We provide a read buffer of an arbitrary size (short of a page); if
  65. * the callback reports -EOVERFLOW, it means it was too small, so we
  66. * just double the size and retry (being careful to append, as
  67. * sometimes the device provided some data). Every now and then we
  68. * check if the average packet size is smaller than the current packet
  69. * size and if so, we halve it. At the end, the size of the
  70. * preallocated buffer should be following the average received
  71. * transaction size, adapting dynamically to it.
  72. *
  73. * ROADMAP
  74. *
  75. * i2400mu_rx_kick() Called from notif.c when we get a
  76. * 'data ready' notification
  77. * i2400mu_rxd() Kernel RX daemon
  78. * i2400mu_rx() Receive USB data
  79. * i2400m_rx() Send data to generic i2400m RX handling
  80. *
  81. * i2400mu_rx_setup() called from i2400mu_bus_dev_start()
  82. *
  83. * i2400mu_rx_release() called from i2400mu_bus_dev_stop()
  84. */
  85. #include <linux/workqueue.h>
  86. #include <linux/slab.h>
  87. #include <linux/usb.h>
  88. #include "i2400m-usb.h"
  89. #define D_SUBMODULE rx
  90. #include "usb-debug-levels.h"
  91. /*
  92. * Dynamic RX size
  93. *
  94. * We can't let the rx_size be a multiple of 512 bytes (the RX
  95. * endpoint's max packet size). On some USB host controllers (we
  96. * haven't been able to fully characterize which), if the device is
  97. * about to send (for example) X bytes and we only post a buffer to
  98. * receive n*512, it will fail to mark that as babble (so that
  99. * i2400mu_rx() [case -EOVERFLOW] can resize the buffer and get the
  100. * rest).
  101. *
  102. * So on growing or shrinking, if it is a multiple of the
  103. * maxpacketsize, we remove some (instead of incresing some, so in a
  104. * buddy allocator we try to waste less space).
  105. *
  106. * Note we also need a hook for this on i2400mu_rx() -- when we do the
  107. * first read, we are sure we won't hit this spot because
  108. * i240mm->rx_size has been set properly. However, if we have to
  109. * double because of -EOVERFLOW, when we launch the read to get the
  110. * rest of the data, we *have* to make sure that also is not a
  111. * multiple of the max_pkt_size.
  112. */
  113. static
  114. size_t i2400mu_rx_size_grow(struct i2400mu *i2400mu)
  115. {
  116. struct device *dev = &i2400mu->usb_iface->dev;
  117. size_t rx_size;
  118. const size_t max_pkt_size = 512;
  119. rx_size = 2 * i2400mu->rx_size;
  120. if (rx_size % max_pkt_size == 0) {
  121. rx_size -= 8;
  122. d_printf(1, dev,
  123. "RX: expected size grew to %zu [adjusted -8] "
  124. "from %zu\n",
  125. rx_size, i2400mu->rx_size);
  126. } else
  127. d_printf(1, dev,
  128. "RX: expected size grew to %zu from %zu\n",
  129. rx_size, i2400mu->rx_size);
  130. return rx_size;
  131. }
  132. static
  133. void i2400mu_rx_size_maybe_shrink(struct i2400mu *i2400mu)
  134. {
  135. const size_t max_pkt_size = 512;
  136. struct device *dev = &i2400mu->usb_iface->dev;
  137. if (unlikely(i2400mu->rx_size_cnt >= 100
  138. && i2400mu->rx_size_auto_shrink)) {
  139. size_t avg_rx_size =
  140. i2400mu->rx_size_acc / i2400mu->rx_size_cnt;
  141. size_t new_rx_size = i2400mu->rx_size / 2;
  142. if (avg_rx_size < new_rx_size) {
  143. if (new_rx_size % max_pkt_size == 0) {
  144. new_rx_size -= 8;
  145. d_printf(1, dev,
  146. "RX: expected size shrank to %zu "
  147. "[adjusted -8] from %zu\n",
  148. new_rx_size, i2400mu->rx_size);
  149. } else
  150. d_printf(1, dev,
  151. "RX: expected size shrank to %zu "
  152. "from %zu\n",
  153. new_rx_size, i2400mu->rx_size);
  154. i2400mu->rx_size = new_rx_size;
  155. i2400mu->rx_size_cnt = 0;
  156. i2400mu->rx_size_acc = i2400mu->rx_size;
  157. }
  158. }
  159. }
  160. /*
  161. * Receive a message with payloads from the USB bus into an skb
  162. *
  163. * @i2400mu: USB device descriptor
  164. * @rx_skb: skb where to place the received message
  165. *
  166. * Deals with all the USB-specifics of receiving, dynamically
  167. * increasing the buffer size if so needed. Returns the payload in the
  168. * skb, ready to process. On a zero-length packet, we retry.
  169. *
  170. * On soft USB errors, we retry (until they become too frequent and
  171. * then are promoted to hard); on hard USB errors, we reset the
  172. * device. On other errors (skb realloacation, we just drop it and
  173. * hope for the next invocation to solve it).
  174. *
  175. * Returns: pointer to the skb if ok, ERR_PTR on error.
  176. * NOTE: this function might realloc the skb (if it is too small),
  177. * so always update with the one returned.
  178. * ERR_PTR() is < 0 on error.
  179. * Will return NULL if it cannot reallocate -- this can be
  180. * considered a transient retryable error.
  181. */
  182. static
  183. struct sk_buff *i2400mu_rx(struct i2400mu *i2400mu, struct sk_buff *rx_skb)
  184. {
  185. int result = 0;
  186. struct device *dev = &i2400mu->usb_iface->dev;
  187. int usb_pipe, read_size, rx_size, do_autopm;
  188. struct usb_endpoint_descriptor *epd;
  189. const size_t max_pkt_size = 512;
  190. d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
  191. do_autopm = atomic_read(&i2400mu->do_autopm);
  192. result = do_autopm ?
  193. usb_autopm_get_interface(i2400mu->usb_iface) : 0;
  194. if (result < 0) {
  195. dev_err(dev, "RX: can't get autopm: %d\n", result);
  196. do_autopm = 0;
  197. }
  198. epd = usb_get_epd(i2400mu->usb_iface, i2400mu->endpoint_cfg.bulk_in);
  199. usb_pipe = usb_rcvbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  200. retry:
  201. rx_size = skb_end_pointer(rx_skb) - rx_skb->data - rx_skb->len;
  202. if (unlikely(rx_size % max_pkt_size == 0)) {
  203. rx_size -= 8;
  204. d_printf(1, dev, "RX: rx_size adapted to %d [-8]\n", rx_size);
  205. }
  206. result = usb_bulk_msg(
  207. i2400mu->usb_dev, usb_pipe, rx_skb->data + rx_skb->len,
  208. rx_size, &read_size, 200);
  209. usb_mark_last_busy(i2400mu->usb_dev);
  210. switch (result) {
  211. case 0:
  212. if (read_size == 0)
  213. goto retry; /* ZLP, just resubmit */
  214. skb_put(rx_skb, read_size);
  215. break;
  216. case -EPIPE:
  217. /*
  218. * Stall -- maybe the device is choking with our
  219. * requests. Clear it and give it some time. If they
  220. * happen to often, it might be another symptom, so we
  221. * reset.
  222. *
  223. * No error handling for usb_clear_halt(0; if it
  224. * works, the retry works; if it fails, this switch
  225. * does the error handling for us.
  226. */
  227. if (edc_inc(&i2400mu->urb_edc,
  228. 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  229. dev_err(dev, "BM-CMD: too many stalls in "
  230. "URB; resetting device\n");
  231. goto do_reset;
  232. }
  233. usb_clear_halt(i2400mu->usb_dev, usb_pipe);
  234. msleep(10); /* give the device some time */
  235. goto retry;
  236. case -EINVAL: /* while removing driver */
  237. case -ENODEV: /* dev disconnect ... */
  238. case -ENOENT: /* just ignore it */
  239. case -ESHUTDOWN:
  240. case -ECONNRESET:
  241. break;
  242. case -EOVERFLOW: { /* too small, reallocate */
  243. struct sk_buff *new_skb;
  244. rx_size = i2400mu_rx_size_grow(i2400mu);
  245. if (rx_size <= (1 << 16)) /* cap it */
  246. i2400mu->rx_size = rx_size;
  247. else if (printk_ratelimit()) {
  248. dev_err(dev, "BUG? rx_size up to %d\n", rx_size);
  249. result = -EINVAL;
  250. goto out;
  251. }
  252. skb_put(rx_skb, read_size);
  253. new_skb = skb_copy_expand(rx_skb, 0, rx_size - rx_skb->len,
  254. GFP_KERNEL);
  255. if (new_skb == NULL) {
  256. if (printk_ratelimit())
  257. dev_err(dev, "RX: Can't reallocate skb to %d; "
  258. "RX dropped\n", rx_size);
  259. kfree_skb(rx_skb);
  260. rx_skb = NULL;
  261. goto out; /* drop it...*/
  262. }
  263. kfree_skb(rx_skb);
  264. rx_skb = new_skb;
  265. i2400mu->rx_size_cnt = 0;
  266. i2400mu->rx_size_acc = i2400mu->rx_size;
  267. d_printf(1, dev, "RX: size changed to %d, received %d, "
  268. "copied %d, capacity %ld\n",
  269. rx_size, read_size, rx_skb->len,
  270. (long) skb_end_offset(new_skb));
  271. goto retry;
  272. }
  273. /* In most cases, it happens due to the hardware scheduling a
  274. * read when there was no data - unfortunately, we have no way
  275. * to tell this timeout from a USB timeout. So we just ignore
  276. * it. */
  277. case -ETIMEDOUT:
  278. dev_err(dev, "RX: timeout: %d\n", result);
  279. result = 0;
  280. break;
  281. default: /* Any error */
  282. if (edc_inc(&i2400mu->urb_edc,
  283. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
  284. goto error_reset;
  285. dev_err(dev, "RX: error receiving URB: %d, retrying\n", result);
  286. goto retry;
  287. }
  288. out:
  289. if (do_autopm)
  290. usb_autopm_put_interface(i2400mu->usb_iface);
  291. d_fnend(4, dev, "(i2400mu %p) = %p\n", i2400mu, rx_skb);
  292. return rx_skb;
  293. error_reset:
  294. dev_err(dev, "RX: maximum errors in URB exceeded; "
  295. "resetting device\n");
  296. do_reset:
  297. usb_queue_reset_device(i2400mu->usb_iface);
  298. rx_skb = ERR_PTR(result);
  299. goto out;
  300. }
  301. /*
  302. * Kernel thread for USB reception of data
  303. *
  304. * This thread waits for a kick; once kicked, it will allocate an skb
  305. * and receive a single message to it from USB (using
  306. * i2400mu_rx()). Once received, it is passed to the generic i2400m RX
  307. * code for processing.
  308. *
  309. * When done processing, it runs some dirty statistics to verify if
  310. * the last 100 messages received were smaller than half of the
  311. * current RX buffer size. In that case, the RX buffer size is
  312. * halved. This will helps lowering the pressure on the memory
  313. * allocator.
  314. *
  315. * Hard errors force the thread to exit.
  316. */
  317. static
  318. int i2400mu_rxd(void *_i2400mu)
  319. {
  320. int result = 0;
  321. struct i2400mu *i2400mu = _i2400mu;
  322. struct i2400m *i2400m = &i2400mu->i2400m;
  323. struct device *dev = &i2400mu->usb_iface->dev;
  324. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  325. size_t pending;
  326. int rx_size;
  327. struct sk_buff *rx_skb;
  328. unsigned long flags;
  329. d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
  330. spin_lock_irqsave(&i2400m->rx_lock, flags);
  331. BUG_ON(i2400mu->rx_kthread != NULL);
  332. i2400mu->rx_kthread = current;
  333. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  334. while (1) {
  335. d_printf(2, dev, "RX: waiting for messages\n");
  336. pending = 0;
  337. wait_event_interruptible(
  338. i2400mu->rx_wq,
  339. (kthread_should_stop() /* check this first! */
  340. || (pending = atomic_read(&i2400mu->rx_pending_count)))
  341. );
  342. if (kthread_should_stop())
  343. break;
  344. if (pending == 0)
  345. continue;
  346. rx_size = i2400mu->rx_size;
  347. d_printf(2, dev, "RX: reading up to %d bytes\n", rx_size);
  348. rx_skb = __netdev_alloc_skb(net_dev, rx_size, GFP_KERNEL);
  349. if (rx_skb == NULL) {
  350. dev_err(dev, "RX: can't allocate skb [%d bytes]\n",
  351. rx_size);
  352. msleep(50); /* give it some time? */
  353. continue;
  354. }
  355. /* Receive the message with the payloads */
  356. rx_skb = i2400mu_rx(i2400mu, rx_skb);
  357. result = PTR_ERR(rx_skb);
  358. if (IS_ERR(rx_skb))
  359. goto out;
  360. atomic_dec(&i2400mu->rx_pending_count);
  361. if (rx_skb == NULL || rx_skb->len == 0) {
  362. /* some "ignorable" condition */
  363. kfree_skb(rx_skb);
  364. continue;
  365. }
  366. /* Deliver the message to the generic i2400m code */
  367. i2400mu->rx_size_cnt++;
  368. i2400mu->rx_size_acc += rx_skb->len;
  369. result = i2400m_rx(i2400m, rx_skb);
  370. if (result == -EIO
  371. && edc_inc(&i2400mu->urb_edc,
  372. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  373. goto error_reset;
  374. }
  375. /* Maybe adjust RX buffer size */
  376. i2400mu_rx_size_maybe_shrink(i2400mu);
  377. }
  378. result = 0;
  379. out:
  380. spin_lock_irqsave(&i2400m->rx_lock, flags);
  381. i2400mu->rx_kthread = NULL;
  382. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  383. d_fnend(4, dev, "(i2400mu %p) = %d\n", i2400mu, result);
  384. return result;
  385. error_reset:
  386. dev_err(dev, "RX: maximum errors in received buffer exceeded; "
  387. "resetting device\n");
  388. usb_queue_reset_device(i2400mu->usb_iface);
  389. goto out;
  390. }
  391. /*
  392. * Start reading from the device
  393. *
  394. * @i2400m: device instance
  395. *
  396. * Notify the RX thread that there is data pending.
  397. */
  398. void i2400mu_rx_kick(struct i2400mu *i2400mu)
  399. {
  400. struct i2400m *i2400m = &i2400mu->i2400m;
  401. struct device *dev = &i2400mu->usb_iface->dev;
  402. d_fnstart(3, dev, "(i2400mu %p)\n", i2400m);
  403. atomic_inc(&i2400mu->rx_pending_count);
  404. wake_up_all(&i2400mu->rx_wq);
  405. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  406. }
  407. int i2400mu_rx_setup(struct i2400mu *i2400mu)
  408. {
  409. int result = 0;
  410. struct i2400m *i2400m = &i2400mu->i2400m;
  411. struct device *dev = &i2400mu->usb_iface->dev;
  412. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  413. struct task_struct *kthread;
  414. kthread = kthread_run(i2400mu_rxd, i2400mu, "%s-rx",
  415. wimax_dev->name);
  416. /* the kthread function sets i2400mu->rx_thread */
  417. if (IS_ERR(kthread)) {
  418. result = PTR_ERR(kthread);
  419. dev_err(dev, "RX: cannot start thread: %d\n", result);
  420. }
  421. return result;
  422. }
  423. void i2400mu_rx_release(struct i2400mu *i2400mu)
  424. {
  425. unsigned long flags;
  426. struct i2400m *i2400m = &i2400mu->i2400m;
  427. struct device *dev = i2400m_dev(i2400m);
  428. struct task_struct *kthread;
  429. spin_lock_irqsave(&i2400m->rx_lock, flags);
  430. kthread = i2400mu->rx_kthread;
  431. i2400mu->rx_kthread = NULL;
  432. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  433. if (kthread)
  434. kthread_stop(kthread);
  435. else
  436. d_printf(1, dev, "RX: kthread had already exited\n");
  437. }