rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * WUSB Wire Adapter: WLP interface
  3. * Driver for the Linux Network stack.
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * i1480u's RX handling is simple. i1480u will send the received
  24. * network packets broken up in fragments; 1 to N fragments make a
  25. * packet, we assemble them together and deliver the packet with netif_rx().
  26. *
  27. * Beacuse each USB transfer is a *single* fragment (except when the
  28. * transfer contains a first fragment), each URB called thus
  29. * back contains one or two fragments. So we queue N URBs, each with its own
  30. * fragment buffer. When a URB is done, we process it (adding to the
  31. * current skb from the fragment buffer until complete). Once
  32. * processed, we requeue the URB. There is always a bunch of URBs
  33. * ready to take data, so the intergap should be minimal.
  34. *
  35. * An URB's transfer buffer is the data field of a socket buffer. This
  36. * reduces copying as data can be passed directly to network layer. If a
  37. * complete packet or 1st fragment is received the URB's transfer buffer is
  38. * taken away from it and used to send data to the network layer. In this
  39. * case a new transfer buffer is allocated to the URB before being requeued.
  40. * If a "NEXT" or "LAST" fragment is received, the fragment contents is
  41. * appended to the RX packet under construction and the transfer buffer
  42. * is reused. To be able to use this buffer to assemble complete packets
  43. * we set each buffer's size to that of the MAX ethernet packet that can
  44. * be received. There is thus room for improvement in memory usage.
  45. *
  46. * When the max tx fragment size increases, we should be able to read
  47. * data into the skbs directly with very simple code.
  48. *
  49. * ROADMAP:
  50. *
  51. * ENTRY POINTS:
  52. *
  53. * i1480u_rx_setup(): setup RX context [from i1480u_open()]
  54. *
  55. * i1480u_rx_release(): release RX context [from i1480u_stop()]
  56. *
  57. * i1480u_rx_cb(): called when the RX USB URB receives a
  58. * packet. It removes the header and pushes it up
  59. * the Linux netdev stack with netif_rx().
  60. *
  61. * i1480u_rx_buffer()
  62. * i1480u_drop() and i1480u_fix()
  63. * i1480u_skb_deliver
  64. *
  65. */
  66. #include <linux/netdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include "i1480u-wlp.h"
  69. #define D_LOCAL 0
  70. #include <linux/uwb/debug.h>
  71. /**
  72. * Setup the RX context
  73. *
  74. * Each URB is provided with a transfer_buffer that is the data field
  75. * of a new socket buffer.
  76. */
  77. int i1480u_rx_setup(struct i1480u *i1480u)
  78. {
  79. int result, cnt;
  80. struct device *dev = &i1480u->usb_iface->dev;
  81. struct net_device *net_dev = i1480u->net_dev;
  82. struct usb_endpoint_descriptor *epd;
  83. struct sk_buff *skb;
  84. /* Alloc RX stuff */
  85. i1480u->rx_skb = NULL; /* not in process of receiving packet */
  86. result = -ENOMEM;
  87. epd = &i1480u->usb_iface->cur_altsetting->endpoint[1].desc;
  88. for (cnt = 0; cnt < i1480u_RX_BUFS; cnt++) {
  89. struct i1480u_rx_buf *rx_buf = &i1480u->rx_buf[cnt];
  90. rx_buf->i1480u = i1480u;
  91. skb = dev_alloc_skb(i1480u_MAX_RX_PKT_SIZE);
  92. if (!skb) {
  93. dev_err(dev,
  94. "RX: cannot allocate RX buffer %d\n", cnt);
  95. result = -ENOMEM;
  96. goto error;
  97. }
  98. skb->dev = net_dev;
  99. skb->ip_summed = CHECKSUM_NONE;
  100. skb_reserve(skb, 2);
  101. rx_buf->data = skb;
  102. rx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  103. if (unlikely(rx_buf->urb == NULL)) {
  104. dev_err(dev, "RX: cannot allocate URB %d\n", cnt);
  105. result = -ENOMEM;
  106. goto error;
  107. }
  108. usb_fill_bulk_urb(rx_buf->urb, i1480u->usb_dev,
  109. usb_rcvbulkpipe(i1480u->usb_dev, epd->bEndpointAddress),
  110. rx_buf->data->data, i1480u_MAX_RX_PKT_SIZE - 2,
  111. i1480u_rx_cb, rx_buf);
  112. result = usb_submit_urb(rx_buf->urb, GFP_NOIO);
  113. if (unlikely(result < 0)) {
  114. dev_err(dev, "RX: cannot submit URB %d: %d\n",
  115. cnt, result);
  116. goto error;
  117. }
  118. }
  119. return 0;
  120. error:
  121. i1480u_rx_release(i1480u);
  122. return result;
  123. }
  124. /** Release resources associated to the rx context */
  125. void i1480u_rx_release(struct i1480u *i1480u)
  126. {
  127. int cnt;
  128. for (cnt = 0; cnt < i1480u_RX_BUFS; cnt++) {
  129. if (i1480u->rx_buf[cnt].data)
  130. dev_kfree_skb(i1480u->rx_buf[cnt].data);
  131. if (i1480u->rx_buf[cnt].urb) {
  132. usb_kill_urb(i1480u->rx_buf[cnt].urb);
  133. usb_free_urb(i1480u->rx_buf[cnt].urb);
  134. }
  135. }
  136. if (i1480u->rx_skb != NULL)
  137. dev_kfree_skb(i1480u->rx_skb);
  138. }
  139. static
  140. void i1480u_rx_unlink_urbs(struct i1480u *i1480u)
  141. {
  142. int cnt;
  143. for (cnt = 0; cnt < i1480u_RX_BUFS; cnt++) {
  144. if (i1480u->rx_buf[cnt].urb)
  145. usb_unlink_urb(i1480u->rx_buf[cnt].urb);
  146. }
  147. }
  148. /** Fix an out-of-sequence packet */
  149. #define i1480u_fix(i1480u, msg...) \
  150. do { \
  151. if (printk_ratelimit()) \
  152. dev_err(&i1480u->usb_iface->dev, msg); \
  153. dev_kfree_skb_irq(i1480u->rx_skb); \
  154. i1480u->rx_skb = NULL; \
  155. i1480u->rx_untd_pkt_size = 0; \
  156. } while (0)
  157. /** Drop an out-of-sequence packet */
  158. #define i1480u_drop(i1480u, msg...) \
  159. do { \
  160. if (printk_ratelimit()) \
  161. dev_err(&i1480u->usb_iface->dev, msg); \
  162. i1480u->stats.rx_dropped++; \
  163. } while (0)
  164. /** Finalizes setting up the SKB and delivers it
  165. *
  166. * We first pass the incoming frame to WLP substack for verification. It
  167. * may also be a WLP association frame in which case WLP will take over the
  168. * processing. If WLP does not take it over it will still verify it, if the
  169. * frame is invalid the skb will be freed by WLP and we will not continue
  170. * parsing.
  171. * */
  172. static
  173. void i1480u_skb_deliver(struct i1480u *i1480u)
  174. {
  175. int should_parse;
  176. struct net_device *net_dev = i1480u->net_dev;
  177. struct device *dev = &i1480u->usb_iface->dev;
  178. d_printf(6, dev, "RX delivered pre skb(%p), %u bytes\n",
  179. i1480u->rx_skb, i1480u->rx_skb->len);
  180. d_dump(7, dev, i1480u->rx_skb->data, i1480u->rx_skb->len);
  181. should_parse = wlp_receive_frame(dev, &i1480u->wlp, i1480u->rx_skb,
  182. &i1480u->rx_srcaddr);
  183. if (!should_parse)
  184. goto out;
  185. i1480u->rx_skb->protocol = eth_type_trans(i1480u->rx_skb, net_dev);
  186. d_printf(5, dev, "RX delivered skb(%p), %u bytes\n",
  187. i1480u->rx_skb, i1480u->rx_skb->len);
  188. d_dump(7, dev, i1480u->rx_skb->data,
  189. i1480u->rx_skb->len > 72 ? 72 : i1480u->rx_skb->len);
  190. i1480u->stats.rx_packets++;
  191. i1480u->stats.rx_bytes += i1480u->rx_untd_pkt_size;
  192. net_dev->last_rx = jiffies;
  193. /* FIXME: flow control: check netif_rx() retval */
  194. netif_rx(i1480u->rx_skb); /* deliver */
  195. out:
  196. i1480u->rx_skb = NULL;
  197. i1480u->rx_untd_pkt_size = 0;
  198. }
  199. /**
  200. * Process a buffer of data received from the USB RX endpoint
  201. *
  202. * First fragment arrives with next or last fragment. All other fragments
  203. * arrive alone.
  204. *
  205. * /me hates long functions.
  206. */
  207. static
  208. void i1480u_rx_buffer(struct i1480u_rx_buf *rx_buf)
  209. {
  210. unsigned pkt_completed = 0; /* !0 when we got all pkt fragments */
  211. size_t untd_hdr_size, untd_frg_size;
  212. size_t i1480u_hdr_size;
  213. struct wlp_rx_hdr *i1480u_hdr = NULL;
  214. struct i1480u *i1480u = rx_buf->i1480u;
  215. struct sk_buff *skb = rx_buf->data;
  216. int size_left = rx_buf->urb->actual_length;
  217. void *ptr = rx_buf->urb->transfer_buffer; /* also rx_buf->data->data */
  218. struct untd_hdr *untd_hdr;
  219. struct net_device *net_dev = i1480u->net_dev;
  220. struct device *dev = &i1480u->usb_iface->dev;
  221. struct sk_buff *new_skb;
  222. #if 0
  223. dev_fnstart(dev,
  224. "(i1480u %p ptr %p size_left %zu)\n", i1480u, ptr, size_left);
  225. dev_err(dev, "RX packet, %zu bytes\n", size_left);
  226. dump_bytes(dev, ptr, size_left);
  227. #endif
  228. i1480u_hdr_size = sizeof(struct wlp_rx_hdr);
  229. while (size_left > 0) {
  230. if (pkt_completed) {
  231. i1480u_drop(i1480u, "RX: fragment follows completed"
  232. "packet in same buffer. Dropping\n");
  233. break;
  234. }
  235. untd_hdr = ptr;
  236. if (size_left < sizeof(*untd_hdr)) { /* Check the UNTD header */
  237. i1480u_drop(i1480u, "RX: short UNTD header! Dropping\n");
  238. goto out;
  239. }
  240. if (unlikely(untd_hdr_rx_tx(untd_hdr) == 0)) { /* Paranoia: TX set? */
  241. i1480u_drop(i1480u, "RX: TX bit set! Dropping\n");
  242. goto out;
  243. }
  244. switch (untd_hdr_type(untd_hdr)) { /* Check the UNTD header type */
  245. case i1480u_PKT_FRAG_1ST: {
  246. struct untd_hdr_1st *untd_hdr_1st = (void *) untd_hdr;
  247. dev_dbg(dev, "1st fragment\n");
  248. untd_hdr_size = sizeof(struct untd_hdr_1st);
  249. if (i1480u->rx_skb != NULL)
  250. i1480u_fix(i1480u, "RX: 1st fragment out of "
  251. "sequence! Fixing\n");
  252. if (size_left < untd_hdr_size + i1480u_hdr_size) {
  253. i1480u_drop(i1480u, "RX: short 1st fragment! "
  254. "Dropping\n");
  255. goto out;
  256. }
  257. i1480u->rx_untd_pkt_size = le16_to_cpu(untd_hdr->len)
  258. - i1480u_hdr_size;
  259. untd_frg_size = le16_to_cpu(untd_hdr_1st->fragment_len);
  260. if (size_left < untd_hdr_size + untd_frg_size) {
  261. i1480u_drop(i1480u,
  262. "RX: short payload! Dropping\n");
  263. goto out;
  264. }
  265. i1480u->rx_skb = skb;
  266. i1480u_hdr = (void *) untd_hdr_1st + untd_hdr_size;
  267. i1480u->rx_srcaddr = i1480u_hdr->srcaddr;
  268. skb_put(i1480u->rx_skb, untd_hdr_size + untd_frg_size);
  269. skb_pull(i1480u->rx_skb, untd_hdr_size + i1480u_hdr_size);
  270. stats_add_sample(&i1480u->lqe_stats, (s8) i1480u_hdr->LQI - 7);
  271. stats_add_sample(&i1480u->rssi_stats, i1480u_hdr->RSSI + 18);
  272. rx_buf->data = NULL; /* need to create new buffer */
  273. break;
  274. }
  275. case i1480u_PKT_FRAG_NXT: {
  276. dev_dbg(dev, "nxt fragment\n");
  277. untd_hdr_size = sizeof(struct untd_hdr_rst);
  278. if (i1480u->rx_skb == NULL) {
  279. i1480u_drop(i1480u, "RX: next fragment out of "
  280. "sequence! Dropping\n");
  281. goto out;
  282. }
  283. if (size_left < untd_hdr_size) {
  284. i1480u_drop(i1480u, "RX: short NXT fragment! "
  285. "Dropping\n");
  286. goto out;
  287. }
  288. untd_frg_size = le16_to_cpu(untd_hdr->len);
  289. if (size_left < untd_hdr_size + untd_frg_size) {
  290. i1480u_drop(i1480u,
  291. "RX: short payload! Dropping\n");
  292. goto out;
  293. }
  294. memmove(skb_put(i1480u->rx_skb, untd_frg_size),
  295. ptr + untd_hdr_size, untd_frg_size);
  296. break;
  297. }
  298. case i1480u_PKT_FRAG_LST: {
  299. dev_dbg(dev, "Lst fragment\n");
  300. untd_hdr_size = sizeof(struct untd_hdr_rst);
  301. if (i1480u->rx_skb == NULL) {
  302. i1480u_drop(i1480u, "RX: last fragment out of "
  303. "sequence! Dropping\n");
  304. goto out;
  305. }
  306. if (size_left < untd_hdr_size) {
  307. i1480u_drop(i1480u, "RX: short LST fragment! "
  308. "Dropping\n");
  309. goto out;
  310. }
  311. untd_frg_size = le16_to_cpu(untd_hdr->len);
  312. if (size_left < untd_frg_size + untd_hdr_size) {
  313. i1480u_drop(i1480u,
  314. "RX: short payload! Dropping\n");
  315. goto out;
  316. }
  317. memmove(skb_put(i1480u->rx_skb, untd_frg_size),
  318. ptr + untd_hdr_size, untd_frg_size);
  319. pkt_completed = 1;
  320. break;
  321. }
  322. case i1480u_PKT_FRAG_CMP: {
  323. dev_dbg(dev, "cmp fragment\n");
  324. untd_hdr_size = sizeof(struct untd_hdr_cmp);
  325. if (i1480u->rx_skb != NULL)
  326. i1480u_fix(i1480u, "RX: fix out-of-sequence CMP"
  327. " fragment!\n");
  328. if (size_left < untd_hdr_size + i1480u_hdr_size) {
  329. i1480u_drop(i1480u, "RX: short CMP fragment! "
  330. "Dropping\n");
  331. goto out;
  332. }
  333. i1480u->rx_untd_pkt_size = le16_to_cpu(untd_hdr->len);
  334. untd_frg_size = i1480u->rx_untd_pkt_size;
  335. if (size_left < i1480u->rx_untd_pkt_size + untd_hdr_size) {
  336. i1480u_drop(i1480u,
  337. "RX: short payload! Dropping\n");
  338. goto out;
  339. }
  340. i1480u->rx_skb = skb;
  341. i1480u_hdr = (void *) untd_hdr + untd_hdr_size;
  342. i1480u->rx_srcaddr = i1480u_hdr->srcaddr;
  343. stats_add_sample(&i1480u->lqe_stats, (s8) i1480u_hdr->LQI - 7);
  344. stats_add_sample(&i1480u->rssi_stats, i1480u_hdr->RSSI + 18);
  345. skb_put(i1480u->rx_skb, untd_hdr_size + i1480u->rx_untd_pkt_size);
  346. skb_pull(i1480u->rx_skb, untd_hdr_size + i1480u_hdr_size);
  347. rx_buf->data = NULL; /* for hand off skb to network stack */
  348. pkt_completed = 1;
  349. i1480u->rx_untd_pkt_size -= i1480u_hdr_size; /* accurate stat */
  350. break;
  351. }
  352. default:
  353. i1480u_drop(i1480u, "RX: unknown packet type %u! "
  354. "Dropping\n", untd_hdr_type(untd_hdr));
  355. goto out;
  356. }
  357. size_left -= untd_hdr_size + untd_frg_size;
  358. if (size_left > 0)
  359. ptr += untd_hdr_size + untd_frg_size;
  360. }
  361. if (pkt_completed)
  362. i1480u_skb_deliver(i1480u);
  363. out:
  364. /* recreate needed RX buffers*/
  365. if (rx_buf->data == NULL) {
  366. /* buffer is being used to receive packet, create new */
  367. new_skb = dev_alloc_skb(i1480u_MAX_RX_PKT_SIZE);
  368. if (!new_skb) {
  369. if (printk_ratelimit())
  370. dev_err(dev,
  371. "RX: cannot allocate RX buffer\n");
  372. } else {
  373. new_skb->dev = net_dev;
  374. new_skb->ip_summed = CHECKSUM_NONE;
  375. skb_reserve(new_skb, 2);
  376. rx_buf->data = new_skb;
  377. }
  378. }
  379. return;
  380. }
  381. /**
  382. * Called when an RX URB has finished receiving or has found some kind
  383. * of error condition.
  384. *
  385. * LIMITATIONS:
  386. *
  387. * - We read USB-transfers, each transfer contains a SINGLE fragment
  388. * (can contain a complete packet, or a 1st, next, or last fragment
  389. * of a packet).
  390. * Looks like a transfer can contain more than one fragment (07/18/06)
  391. *
  392. * - Each transfer buffer is the size of the maximum packet size (minus
  393. * headroom), i1480u_MAX_PKT_SIZE - 2
  394. *
  395. * - We always read the full USB-transfer, no partials.
  396. *
  397. * - Each transfer is read directly into a skb. This skb will be used to
  398. * send data to the upper layers if it is the first fragment or a complete
  399. * packet. In the other cases the data will be copied from the skb to
  400. * another skb that is being prepared for the upper layers from a prev
  401. * first fragment.
  402. *
  403. * It is simply too much of a pain. Gosh, there should be a unified
  404. * SG infrastructure for *everything* [so that I could declare a SG
  405. * buffer, pass it to USB for receiving, append some space to it if
  406. * I wish, receive more until I have the whole chunk, adapt
  407. * pointers on each fragment to remove hardware headers and then
  408. * attach that to an skbuff and netif_rx()].
  409. */
  410. void i1480u_rx_cb(struct urb *urb)
  411. {
  412. int result;
  413. int do_parse_buffer = 1;
  414. struct i1480u_rx_buf *rx_buf = urb->context;
  415. struct i1480u *i1480u = rx_buf->i1480u;
  416. struct device *dev = &i1480u->usb_iface->dev;
  417. unsigned long flags;
  418. u8 rx_buf_idx = rx_buf - i1480u->rx_buf;
  419. switch (urb->status) {
  420. case 0:
  421. break;
  422. case -ECONNRESET: /* Not an error, but a controlled situation; */
  423. case -ENOENT: /* (we killed the URB)...so, no broadcast */
  424. case -ESHUTDOWN: /* going away! */
  425. dev_err(dev, "RX URB[%u]: goind down %d\n",
  426. rx_buf_idx, urb->status);
  427. goto error;
  428. default:
  429. dev_err(dev, "RX URB[%u]: unknown status %d\n",
  430. rx_buf_idx, urb->status);
  431. if (edc_inc(&i1480u->rx_errors, EDC_MAX_ERRORS,
  432. EDC_ERROR_TIMEFRAME)) {
  433. dev_err(dev, "RX: max acceptable errors exceeded,"
  434. " resetting device.\n");
  435. i1480u_rx_unlink_urbs(i1480u);
  436. wlp_reset_all(&i1480u->wlp);
  437. goto error;
  438. }
  439. do_parse_buffer = 0;
  440. break;
  441. }
  442. spin_lock_irqsave(&i1480u->lock, flags);
  443. /* chew the data fragments, extract network packets */
  444. if (do_parse_buffer) {
  445. i1480u_rx_buffer(rx_buf);
  446. if (rx_buf->data) {
  447. rx_buf->urb->transfer_buffer = rx_buf->data->data;
  448. result = usb_submit_urb(rx_buf->urb, GFP_ATOMIC);
  449. if (result < 0) {
  450. dev_err(dev, "RX URB[%u]: cannot submit %d\n",
  451. rx_buf_idx, result);
  452. }
  453. }
  454. }
  455. spin_unlock_irqrestore(&i1480u->lock, flags);
  456. error:
  457. return;
  458. }