2 * WUSB Wire Adapter: WLP interface
3 * Deal with TX (massaging data to transmit, handling it)
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
23 * Transmission engine. Get an skb, create from that a WLP transmit
24 * context, add a WLP TX header (which we keep prefilled in the
25 * device's instance), fill out the target-specific fields and
32 * i1480u_tx_release(): called by i1480u_disconnect() to release
33 * pending tx contexts.
35 * i1480u_tx_cb(): callback for TX contexts (USB URBs)
36 * i1480u_tx_destroy():
38 * i1480u_tx_timeout(): called for timeout handling from the
41 * i1480u_hard_start_xmit(): called for transmitting an skb from
42 * the network stack. Will interact with WLP
43 * substack to verify and prepare frame.
44 * i1480u_xmit_frame(): actual transmission on hardware
46 * i1480u_tx_create() Creates TX context
47 * i1480u_tx_create_1() For packets in 1 fragment
48 * i1480u_tx_create_n() For packets in >1 fragments
52 * - FIXME: rewrite using usb_sg_*(), add asynch support to
53 * usb_sg_*(). It might not make too much sense as most of
54 * the times the MTU will be smaller than one page...
57 #include <linux/slab.h>
58 #include "i1480u-wlp.h"
61 /* This is only for Next and Last TX packets */
62 i1480u_MAX_PL_SIZE
= i1480u_MAX_FRG_SIZE
63 - sizeof(struct untd_hdr_rst
),
66 /* Free resources allocated to a i1480u tx context. */
68 void i1480u_tx_free(struct i1480u_tx
*wtx
)
72 dev_kfree_skb_irq(wtx
->skb
);
73 usb_free_urb(wtx
->urb
);
78 void i1480u_tx_destroy(struct i1480u
*i1480u
, struct i1480u_tx
*wtx
)
81 spin_lock_irqsave(&i1480u
->tx_list_lock
, flags
); /* not active any more */
82 list_del(&wtx
->list_node
);
84 spin_unlock_irqrestore(&i1480u
->tx_list_lock
, flags
);
88 void i1480u_tx_unlink_urbs(struct i1480u
*i1480u
)
91 struct i1480u_tx
*wtx
, *next
;
93 spin_lock_irqsave(&i1480u
->tx_list_lock
, flags
);
94 list_for_each_entry_safe(wtx
, next
, &i1480u
->tx_list
, list_node
) {
95 usb_unlink_urb(wtx
->urb
);
97 spin_unlock_irqrestore(&i1480u
->tx_list_lock
, flags
);
102 * Callback for a completed tx USB URB.
106 * - FIXME: recover errors more gracefully
107 * - FIXME: handle NAKs (I dont think they come here) for flow ctl
110 void i1480u_tx_cb(struct urb
*urb
)
112 struct i1480u_tx
*wtx
= urb
->context
;
113 struct i1480u
*i1480u
= wtx
->i1480u
;
114 struct net_device
*net_dev
= i1480u
->net_dev
;
115 struct device
*dev
= &i1480u
->usb_iface
->dev
;
118 switch (urb
->status
) {
120 spin_lock_irqsave(&i1480u
->lock
, flags
);
121 net_dev
->stats
.tx_packets
++;
122 net_dev
->stats
.tx_bytes
+= urb
->actual_length
;
123 spin_unlock_irqrestore(&i1480u
->lock
, flags
);
125 case -ECONNRESET
: /* Not an error, but a controlled situation; */
126 case -ENOENT
: /* (we killed the URB)...so, no broadcast */
127 dev_dbg(dev
, "notif endp: reset/noent %d\n", urb
->status
);
128 netif_stop_queue(net_dev
);
130 case -ESHUTDOWN
: /* going away! */
131 dev_dbg(dev
, "notif endp: down %d\n", urb
->status
);
132 netif_stop_queue(net_dev
);
135 dev_err(dev
, "TX: unknown URB status %d\n", urb
->status
);
136 if (edc_inc(&i1480u
->tx_errors
, EDC_MAX_ERRORS
,
137 EDC_ERROR_TIMEFRAME
)) {
138 dev_err(dev
, "TX: max acceptable errors exceeded."
140 netif_stop_queue(net_dev
);
141 i1480u_tx_unlink_urbs(i1480u
);
142 wlp_reset_all(&i1480u
->wlp
);
146 i1480u_tx_destroy(i1480u
, wtx
);
147 if (atomic_dec_return(&i1480u
->tx_inflight
.count
)
148 <= i1480u
->tx_inflight
.threshold
149 && netif_queue_stopped(net_dev
)
150 && i1480u
->tx_inflight
.threshold
!= 0) {
151 netif_start_queue(net_dev
);
152 atomic_inc(&i1480u
->tx_inflight
.restart_count
);
159 * Given a buffer that doesn't fit in a single fragment, create an
160 * scatter/gather structure for delivery to the USB pipe.
162 * Implements functionality of i1480u_tx_create().
164 * @wtx: tx descriptor
166 * @gfp_mask: gfp allocation mask
167 * @returns: Pointer to @wtx if ok, NULL on error.
169 * Sorry, TOO LONG a function, but breaking it up is kind of hard
171 * This will break the buffer in chunks smaller than
172 * i1480u_MAX_FRG_SIZE (including the header) and add proper headers
176 * i1480 tx header | fragment 1
178 * nxt header \ fragment 2
182 * last header \ fragment 3
183 * last fragment data /
185 * This does not fill the i1480 TX header, it is left up to the
186 * caller to do that; you can get it from @wtx->wlp_tx_hdr.
188 * This function consumes the skb unless there is an error.
191 int i1480u_tx_create_n(struct i1480u_tx
*wtx
, struct sk_buff
*skb
,
198 void *pl_itr
, *buf_itr
;
199 size_t pl_size_left
, frgs
, pl_size_1st
, frg_pl_size
= 0;
200 struct untd_hdr_1st
*untd_hdr_1st
;
201 struct wlp_tx_hdr
*wlp_tx_hdr
;
202 struct untd_hdr_rst
*untd_hdr_rst
;
208 pl_size_left
= pl_size
; /* payload size */
209 /* First fragment; fits as much as i1480u_MAX_FRG_SIZE minus
211 pl_size_1st
= i1480u_MAX_FRG_SIZE
212 - sizeof(struct untd_hdr_1st
) - sizeof(struct wlp_tx_hdr
);
213 BUG_ON(pl_size_1st
> pl_size
);
214 pl_size_left
-= pl_size_1st
;
215 /* The rest have an smaller header (no i1480 TX header). We
216 * need to break up the payload in blocks smaller than
217 * i1480u_MAX_PL_SIZE (payload excluding header). */
218 frgs
= (pl_size_left
+ i1480u_MAX_PL_SIZE
- 1) / i1480u_MAX_PL_SIZE
;
219 /* Allocate space for the new buffer. In this new buffer we'll
220 * place the headers followed by the data fragment, headers,
221 * data fragments, etc..
224 wtx
->buf_size
= sizeof(*untd_hdr_1st
)
225 + sizeof(*wlp_tx_hdr
)
226 + frgs
* sizeof(*untd_hdr_rst
)
228 wtx
->buf
= kmalloc(wtx
->buf_size
, gfp_mask
);
229 if (wtx
->buf
== NULL
)
230 goto error_buf_alloc
;
232 buf_itr
= wtx
->buf
; /* We got the space, let's fill it up */
233 /* Fill 1st fragment */
234 untd_hdr_1st
= buf_itr
;
235 buf_itr
+= sizeof(*untd_hdr_1st
);
236 untd_hdr_set_type(&untd_hdr_1st
->hdr
, i1480u_PKT_FRAG_1ST
);
237 untd_hdr_set_rx_tx(&untd_hdr_1st
->hdr
, 0);
238 untd_hdr_1st
->hdr
.len
= cpu_to_le16(pl_size
+ sizeof(*wlp_tx_hdr
));
239 untd_hdr_1st
->fragment_len
=
240 cpu_to_le16(pl_size_1st
+ sizeof(*wlp_tx_hdr
));
241 memset(untd_hdr_1st
->padding
, 0, sizeof(untd_hdr_1st
->padding
));
242 /* Set up i1480 header info */
243 wlp_tx_hdr
= wtx
->wlp_tx_hdr
= buf_itr
;
244 buf_itr
+= sizeof(*wlp_tx_hdr
);
245 /* Copy the first fragment */
246 memcpy(buf_itr
, pl_itr
, pl_size_1st
);
247 pl_itr
+= pl_size_1st
;
248 buf_itr
+= pl_size_1st
;
250 /* Now do each remaining fragment */
252 while (pl_size_left
> 0) {
253 if (buf_itr
+ sizeof(*untd_hdr_rst
) - wtx
->buf
255 printk(KERN_ERR
"BUG: no space for header\n");
258 untd_hdr_rst
= buf_itr
;
259 buf_itr
+= sizeof(*untd_hdr_rst
);
260 if (pl_size_left
> i1480u_MAX_PL_SIZE
) {
261 frg_pl_size
= i1480u_MAX_PL_SIZE
;
262 untd_hdr_set_type(&untd_hdr_rst
->hdr
, i1480u_PKT_FRAG_NXT
);
264 frg_pl_size
= pl_size_left
;
265 untd_hdr_set_type(&untd_hdr_rst
->hdr
, i1480u_PKT_FRAG_LST
);
267 untd_hdr_set_rx_tx(&untd_hdr_rst
->hdr
, 0);
268 untd_hdr_rst
->hdr
.len
= cpu_to_le16(frg_pl_size
);
269 untd_hdr_rst
->padding
= 0;
270 if (buf_itr
+ frg_pl_size
- wtx
->buf
272 printk(KERN_ERR
"BUG: no space for payload\n");
275 memcpy(buf_itr
, pl_itr
, frg_pl_size
);
276 buf_itr
+= frg_pl_size
;
277 pl_itr
+= frg_pl_size
;
278 pl_size_left
-= frg_pl_size
;
280 dev_kfree_skb_irq(skb
);
285 "BUG: skb %u bytes\n"
286 "BUG: frg_pl_size %zd i1480u_MAX_FRG_SIZE %u\n"
287 "BUG: buf_itr %zu buf_size %zu pl_size_left %zu\n",
289 frg_pl_size
, i1480u_MAX_FRG_SIZE
,
290 buf_itr
- wtx
->buf
, wtx
->buf_size
, pl_size_left
);
299 * Given a buffer that fits in a single fragment, fill out a @wtx
300 * struct for transmitting it down the USB pipe.
302 * Uses the fact that we have space reserved in front of the skbuff
303 * for hardware headers :]
305 * This does not fill the i1480 TX header, it is left up to the
306 * caller to do that; you can get it from @wtx->wlp_tx_hdr.
308 * @pl: pointer to payload data
309 * @pl_size: size of the payuload
311 * This function does not consume the @skb.
314 int i1480u_tx_create_1(struct i1480u_tx
*wtx
, struct sk_buff
*skb
,
317 struct untd_hdr_cmp
*untd_hdr_cmp
;
318 struct wlp_tx_hdr
*wlp_tx_hdr
;
322 BUG_ON(skb_headroom(skb
) < sizeof(*wlp_tx_hdr
));
323 wlp_tx_hdr
= (void *) __skb_push(skb
, sizeof(*wlp_tx_hdr
));
324 wtx
->wlp_tx_hdr
= wlp_tx_hdr
;
325 BUG_ON(skb_headroom(skb
) < sizeof(*untd_hdr_cmp
));
326 untd_hdr_cmp
= (void *) __skb_push(skb
, sizeof(*untd_hdr_cmp
));
328 untd_hdr_set_type(&untd_hdr_cmp
->hdr
, i1480u_PKT_FRAG_CMP
);
329 untd_hdr_set_rx_tx(&untd_hdr_cmp
->hdr
, 0);
330 untd_hdr_cmp
->hdr
.len
= cpu_to_le16(skb
->len
- sizeof(*untd_hdr_cmp
));
331 untd_hdr_cmp
->padding
= 0;
337 * Given a skb to transmit, massage it to become palatable for the TX pipe
339 * This will break the buffer in chunks smaller than
340 * i1480u_MAX_FRG_SIZE and add proper headers to each.
343 * i1480 tx header | fragment 1
345 * nxt header \ fragment 2
349 * last header \ fragment 3
350 * last fragment data /
352 * Each fragment will be always smaller or equal to i1480u_MAX_FRG_SIZE.
354 * If the first fragment is smaller than i1480u_MAX_FRG_SIZE, then the
355 * following is composed:
358 * i1480 tx header | single fragment
361 * We were going to use s/g support, but because the interface is
362 * synch and at the end there is plenty of overhead to do it, it
363 * didn't seem that worth for data that is going to be smaller than
367 struct i1480u_tx
*i1480u_tx_create(struct i1480u
*i1480u
,
368 struct sk_buff
*skb
, gfp_t gfp_mask
)
371 struct usb_endpoint_descriptor
*epd
;
375 struct i1480u_tx
*wtx
;
376 const size_t pl_max_size
=
377 i1480u_MAX_FRG_SIZE
- sizeof(struct untd_hdr_cmp
)
378 - sizeof(struct wlp_tx_hdr
);
380 wtx
= kmalloc(sizeof(*wtx
), gfp_mask
);
382 goto error_wtx_alloc
;
383 wtx
->urb
= usb_alloc_urb(0, gfp_mask
);
384 if (wtx
->urb
== NULL
)
385 goto error_urb_alloc
;
386 epd
= &i1480u
->usb_iface
->cur_altsetting
->endpoint
[2].desc
;
387 usb_pipe
= usb_sndbulkpipe(i1480u
->usb_dev
, epd
->bEndpointAddress
);
388 /* Fits in a single complete packet or need to split? */
389 if (skb
->len
> pl_max_size
) {
390 result
= i1480u_tx_create_n(wtx
, skb
, gfp_mask
);
393 usb_fill_bulk_urb(wtx
->urb
, i1480u
->usb_dev
, usb_pipe
,
394 wtx
->buf
, wtx
->buf_size
, i1480u_tx_cb
, wtx
);
396 result
= i1480u_tx_create_1(wtx
, skb
, gfp_mask
);
399 usb_fill_bulk_urb(wtx
->urb
, i1480u
->usb_dev
, usb_pipe
,
400 skb
->data
, skb
->len
, i1480u_tx_cb
, wtx
);
402 spin_lock_irqsave(&i1480u
->tx_list_lock
, flags
);
403 list_add(&wtx
->list_node
, &i1480u
->tx_list
);
404 spin_unlock_irqrestore(&i1480u
->tx_list_lock
, flags
);
416 * Actual fragmentation and transmission of frame
418 * @wlp: WLP substack data structure
419 * @skb: To be transmitted
420 * @dst: Device address of destination
421 * @returns: 0 on success, <0 on failure
423 * This function can also be called directly (not just from
424 * hard_start_xmit), so we also check here if the interface is up before
425 * taking sending anything.
427 int i1480u_xmit_frame(struct wlp
*wlp
, struct sk_buff
*skb
,
428 struct uwb_dev_addr
*dst
)
431 struct i1480u
*i1480u
= container_of(wlp
, struct i1480u
, wlp
);
432 struct device
*dev
= &i1480u
->usb_iface
->dev
;
433 struct net_device
*net_dev
= i1480u
->net_dev
;
434 struct i1480u_tx
*wtx
;
435 struct wlp_tx_hdr
*wlp_tx_hdr
;
436 static unsigned char dev_bcast
[2] = { 0xff, 0xff };
438 BUG_ON(i1480u
->wlp
.rc
== NULL
);
439 if ((net_dev
->flags
& IFF_UP
) == 0)
442 if (atomic_read(&i1480u
->tx_inflight
.count
) >= i1480u
->tx_inflight
.max
) {
443 netif_stop_queue(net_dev
);
444 goto error_max_inflight
;
447 wtx
= i1480u_tx_create(i1480u
, skb
, GFP_ATOMIC
);
448 if (unlikely(wtx
== NULL
)) {
449 if (printk_ratelimit())
450 dev_err(dev
, "TX: no memory for WLP TX URB,"
451 "dropping packet (in flight %d)\n",
452 atomic_read(&i1480u
->tx_inflight
.count
));
453 netif_stop_queue(net_dev
);
454 goto error_wtx_alloc
;
456 wtx
->i1480u
= i1480u
;
457 /* Fill out the i1480 header; @i1480u->def_tx_hdr read without
458 * locking. We do so because they are kind of orthogonal to
459 * each other (and thus not changed in an atomic batch).
460 * The ETH header is right after the WLP TX header. */
461 wlp_tx_hdr
= wtx
->wlp_tx_hdr
;
462 *wlp_tx_hdr
= i1480u
->options
.def_tx_hdr
;
463 wlp_tx_hdr
->dstaddr
= *dst
;
464 if (!memcmp(&wlp_tx_hdr
->dstaddr
, dev_bcast
, sizeof(dev_bcast
))
465 && (wlp_tx_hdr_delivery_id_type(wlp_tx_hdr
) & WLP_DRP
)) {
466 /*Broadcast message directed to DRP host. Send as best effort
468 wlp_tx_hdr_set_delivery_id_type(wlp_tx_hdr
, i1480u
->options
.pca_base_priority
);
471 result
= usb_submit_urb(wtx
->urb
, GFP_ATOMIC
); /* Go baby */
473 dev_err(dev
, "TX: cannot submit URB: %d\n", result
);
474 /* We leave the freeing of skb to calling function */
476 goto error_tx_urb_submit
;
478 atomic_inc(&i1480u
->tx_inflight
.count
);
479 net_dev
->trans_start
= jiffies
;
483 i1480u_tx_destroy(i1480u
, wtx
);
492 * Transmit an skb Called when an skbuf has to be transmitted
494 * The skb is first passed to WLP substack to ensure this is a valid
495 * frame. If valid the device address of destination will be filled and
496 * the WLP header prepended to the skb. If this step fails we fake sending
497 * the frame, if we return an error the network stack will just keep trying.
499 * Broadcast frames inside a WSS needs to be treated special as multicast is
500 * not supported. A broadcast frame is sent as unicast to each member of the
501 * WSS - this is done by the WLP substack when it finds a broadcast frame.
502 * So, we test if the WLP substack took over the skb and only transmit it
503 * if it has not (been taken over).
505 * @net_dev->xmit_lock is held
507 netdev_tx_t
i1480u_hard_start_xmit(struct sk_buff
*skb
,
508 struct net_device
*net_dev
)
511 struct i1480u
*i1480u
= netdev_priv(net_dev
);
512 struct device
*dev
= &i1480u
->usb_iface
->dev
;
513 struct uwb_dev_addr dst
;
515 if ((net_dev
->flags
& IFF_UP
) == 0)
517 result
= wlp_prepare_tx_frame(dev
, &i1480u
->wlp
, skb
, &dst
);
519 dev_err(dev
, "WLP verification of TX frame failed (%d). "
520 "Dropping packet.\n", result
);
522 } else if (result
== 1) {
523 /* trans_start time will be set when WLP actually transmits
527 result
= i1480u_xmit_frame(&i1480u
->wlp
, skb
, &dst
);
529 dev_err(dev
, "Frame TX failed (%d).\n", result
);
534 dev_kfree_skb_any(skb
);
535 net_dev
->stats
.tx_dropped
++;
542 * Called when a pkt transmission doesn't complete in a reasonable period
543 * Device reset may sleep - do it outside of interrupt context (delayed)
545 void i1480u_tx_timeout(struct net_device
*net_dev
)
547 struct i1480u
*i1480u
= netdev_priv(net_dev
);
549 wlp_reset_all(&i1480u
->wlp
);
553 void i1480u_tx_release(struct i1480u
*i1480u
)
556 struct i1480u_tx
*wtx
, *next
;
557 int count
= 0, empty
;
559 spin_lock_irqsave(&i1480u
->tx_list_lock
, flags
);
560 list_for_each_entry_safe(wtx
, next
, &i1480u
->tx_list
, list_node
) {
562 usb_unlink_urb(wtx
->urb
);
564 spin_unlock_irqrestore(&i1480u
->tx_list_lock
, flags
);
565 count
= count
*10; /* i1480ut 200ms per unlinked urb (intervals of 20ms) */
567 * We don't like this sollution too much (dirty as it is), but
568 * it is cheaper than putting a refcount on each i1480u_tx and
569 * i1480uting for all of them to go away...
571 * Called when no more packets can be added to tx_list
572 * so can i1480ut for it to be empty.
575 spin_lock_irqsave(&i1480u
->tx_list_lock
, flags
);
576 empty
= list_empty(&i1480u
->tx_list
);
577 spin_unlock_irqrestore(&i1480u
->tx_list_lock
, flags
);