1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57#include "i1480u-wlp.h"
58#define D_LOCAL 5
59#include <linux/uwb/debug.h>
60
61enum {
62
63 i1480u_MAX_PL_SIZE = i1480u_MAX_FRG_SIZE
64 - sizeof(struct untd_hdr_rst),
65};
66
67
68static
69void i1480u_tx_free(struct i1480u_tx *wtx)
70{
71 kfree(wtx->buf);
72 if (wtx->skb)
73 dev_kfree_skb_irq(wtx->skb);
74 usb_free_urb(wtx->urb);
75 kfree(wtx);
76}
77
78static
79void i1480u_tx_destroy(struct i1480u *i1480u, struct i1480u_tx *wtx)
80{
81 unsigned long flags;
82 spin_lock_irqsave(&i1480u->tx_list_lock, flags);
83 list_del(&wtx->list_node);
84 i1480u_tx_free(wtx);
85 spin_unlock_irqrestore(&i1480u->tx_list_lock, flags);
86}
87
88static
89void i1480u_tx_unlink_urbs(struct i1480u *i1480u)
90{
91 unsigned long flags;
92 struct i1480u_tx *wtx, *next;
93
94 spin_lock_irqsave(&i1480u->tx_list_lock, flags);
95 list_for_each_entry_safe(wtx, next, &i1480u->tx_list, list_node) {
96 usb_unlink_urb(wtx->urb);
97 }
98 spin_unlock_irqrestore(&i1480u->tx_list_lock, flags);
99}
100
101
102
103
104
105
106
107
108
109
110static
111void i1480u_tx_cb(struct urb *urb)
112{
113 struct i1480u_tx *wtx = urb->context;
114 struct i1480u *i1480u = wtx->i1480u;
115 struct net_device *net_dev = i1480u->net_dev;
116 struct device *dev = &i1480u->usb_iface->dev;
117 unsigned long flags;
118
119 switch (urb->status) {
120 case 0:
121 spin_lock_irqsave(&i1480u->lock, flags);
122 i1480u->stats.tx_packets++;
123 i1480u->stats.tx_bytes += urb->actual_length;
124 spin_unlock_irqrestore(&i1480u->lock, flags);
125 break;
126 case -ECONNRESET:
127 case -ENOENT:
128 dev_dbg(dev, "notif endp: reset/noent %d\n", urb->status);
129 netif_stop_queue(net_dev);
130 break;
131 case -ESHUTDOWN:
132 dev_dbg(dev, "notif endp: down %d\n", urb->status);
133 netif_stop_queue(net_dev);
134 break;
135 default:
136 dev_err(dev, "TX: unknown URB status %d\n", urb->status);
137 if (edc_inc(&i1480u->tx_errors, EDC_MAX_ERRORS,
138 EDC_ERROR_TIMEFRAME)) {
139 dev_err(dev, "TX: max acceptable errors exceeded."
140 "Reset device.\n");
141 netif_stop_queue(net_dev);
142 i1480u_tx_unlink_urbs(i1480u);
143 wlp_reset_all(&i1480u->wlp);
144 }
145 break;
146 }
147 i1480u_tx_destroy(i1480u, wtx);
148 if (atomic_dec_return(&i1480u->tx_inflight.count)
149 <= i1480u->tx_inflight.threshold
150 && netif_queue_stopped(net_dev)
151 && i1480u->tx_inflight.threshold != 0) {
152 if (d_test(2) && printk_ratelimit())
153 d_printf(2, dev, "Restart queue. \n");
154 netif_start_queue(net_dev);
155 atomic_inc(&i1480u->tx_inflight.restart_count);
156 }
157 return;
158}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193static
194int i1480u_tx_create_n(struct i1480u_tx *wtx, struct sk_buff *skb,
195 gfp_t gfp_mask)
196{
197 int result;
198 void *pl;
199 size_t pl_size;
200
201 void *pl_itr, *buf_itr;
202 size_t pl_size_left, frgs, pl_size_1st, frg_pl_size = 0;
203 struct untd_hdr_1st *untd_hdr_1st;
204 struct wlp_tx_hdr *wlp_tx_hdr;
205 struct untd_hdr_rst *untd_hdr_rst;
206
207 wtx->skb = NULL;
208 pl = skb->data;
209 pl_itr = pl;
210 pl_size = skb->len;
211 pl_size_left = pl_size;
212
213
214 pl_size_1st = i1480u_MAX_FRG_SIZE
215 - sizeof(struct untd_hdr_1st) - sizeof(struct wlp_tx_hdr);
216 BUG_ON(pl_size_1st > pl_size);
217 pl_size_left -= pl_size_1st;
218
219
220
221 frgs = (pl_size_left + i1480u_MAX_PL_SIZE - 1) / i1480u_MAX_PL_SIZE;
222
223
224
225
226 result = -ENOMEM;
227 wtx->buf_size = sizeof(*untd_hdr_1st)
228 + sizeof(*wlp_tx_hdr)
229 + frgs * sizeof(*untd_hdr_rst)
230 + pl_size;
231 wtx->buf = kmalloc(wtx->buf_size, gfp_mask);
232 if (wtx->buf == NULL)
233 goto error_buf_alloc;
234
235 buf_itr = wtx->buf;
236
237 untd_hdr_1st = buf_itr;
238 buf_itr += sizeof(*untd_hdr_1st);
239 untd_hdr_set_type(&untd_hdr_1st->hdr, i1480u_PKT_FRAG_1ST);
240 untd_hdr_set_rx_tx(&untd_hdr_1st->hdr, 0);
241 untd_hdr_1st->hdr.len = cpu_to_le16(pl_size + sizeof(*wlp_tx_hdr));
242 untd_hdr_1st->fragment_len =
243 cpu_to_le16(pl_size_1st + sizeof(*wlp_tx_hdr));
244 memset(untd_hdr_1st->padding, 0, sizeof(untd_hdr_1st->padding));
245
246 wlp_tx_hdr = wtx->wlp_tx_hdr = buf_itr;
247 buf_itr += sizeof(*wlp_tx_hdr);
248
249 memcpy(buf_itr, pl_itr, pl_size_1st);
250 pl_itr += pl_size_1st;
251 buf_itr += pl_size_1st;
252
253
254 result = -EINVAL;
255 while (pl_size_left > 0) {
256 d_printf(5, NULL, "ITR HDR: pl_size_left %zu buf_itr %zu\n",
257 pl_size_left, buf_itr - wtx->buf);
258 if (buf_itr + sizeof(*untd_hdr_rst) - wtx->buf
259 > wtx->buf_size) {
260 printk(KERN_ERR "BUG: no space for header\n");
261 goto error_bug;
262 }
263 d_printf(5, NULL, "ITR HDR 2: pl_size_left %zu buf_itr %zu\n",
264 pl_size_left, buf_itr - wtx->buf);
265 untd_hdr_rst = buf_itr;
266 buf_itr += sizeof(*untd_hdr_rst);
267 if (pl_size_left > i1480u_MAX_PL_SIZE) {
268 frg_pl_size = i1480u_MAX_PL_SIZE;
269 untd_hdr_set_type(&untd_hdr_rst->hdr, i1480u_PKT_FRAG_NXT);
270 } else {
271 frg_pl_size = pl_size_left;
272 untd_hdr_set_type(&untd_hdr_rst->hdr, i1480u_PKT_FRAG_LST);
273 }
274 d_printf(5, NULL,
275 "ITR PL: pl_size_left %zu buf_itr %zu frg_pl_size %zu\n",
276 pl_size_left, buf_itr - wtx->buf, frg_pl_size);
277 untd_hdr_set_rx_tx(&untd_hdr_rst->hdr, 0);
278 untd_hdr_rst->hdr.len = cpu_to_le16(frg_pl_size);
279 untd_hdr_rst->padding = 0;
280 if (buf_itr + frg_pl_size - wtx->buf
281 > wtx->buf_size) {
282 printk(KERN_ERR "BUG: no space for payload\n");
283 goto error_bug;
284 }
285 memcpy(buf_itr, pl_itr, frg_pl_size);
286 buf_itr += frg_pl_size;
287 pl_itr += frg_pl_size;
288 pl_size_left -= frg_pl_size;
289 d_printf(5, NULL,
290 "ITR PL 2: pl_size_left %zu buf_itr %zu frg_pl_size %zu\n",
291 pl_size_left, buf_itr - wtx->buf, frg_pl_size);
292 }
293 dev_kfree_skb_irq(skb);
294 return 0;
295
296error_bug:
297 printk(KERN_ERR
298 "BUG: skb %u bytes\n"
299 "BUG: frg_pl_size %zd i1480u_MAX_FRG_SIZE %u\n"
300 "BUG: buf_itr %zu buf_size %zu pl_size_left %zu\n",
301 skb->len,
302 frg_pl_size, i1480u_MAX_FRG_SIZE,
303 buf_itr - wtx->buf, wtx->buf_size, pl_size_left);
304
305 kfree(wtx->buf);
306error_buf_alloc:
307 return result;
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326static
327int i1480u_tx_create_1(struct i1480u_tx *wtx, struct sk_buff *skb,
328 gfp_t gfp_mask)
329{
330 struct untd_hdr_cmp *untd_hdr_cmp;
331 struct wlp_tx_hdr *wlp_tx_hdr;
332
333 wtx->buf = NULL;
334 wtx->skb = skb;
335 BUG_ON(skb_headroom(skb) < sizeof(*wlp_tx_hdr));
336 wlp_tx_hdr = (void *) __skb_push(skb, sizeof(*wlp_tx_hdr));
337 wtx->wlp_tx_hdr = wlp_tx_hdr;
338 BUG_ON(skb_headroom(skb) < sizeof(*untd_hdr_cmp));
339 untd_hdr_cmp = (void *) __skb_push(skb, sizeof(*untd_hdr_cmp));
340
341 untd_hdr_set_type(&untd_hdr_cmp->hdr, i1480u_PKT_FRAG_CMP);
342 untd_hdr_set_rx_tx(&untd_hdr_cmp->hdr, 0);
343 untd_hdr_cmp->hdr.len = cpu_to_le16(skb->len - sizeof(*untd_hdr_cmp));
344 untd_hdr_cmp->padding = 0;
345 return 0;
346}
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379static
380struct i1480u_tx *i1480u_tx_create(struct i1480u *i1480u,
381 struct sk_buff *skb, gfp_t gfp_mask)
382{
383 int result;
384 struct usb_endpoint_descriptor *epd;
385 int usb_pipe;
386 unsigned long flags;
387
388 struct i1480u_tx *wtx;
389 const size_t pl_max_size =
390 i1480u_MAX_FRG_SIZE - sizeof(struct untd_hdr_cmp)
391 - sizeof(struct wlp_tx_hdr);
392
393 wtx = kmalloc(sizeof(*wtx), gfp_mask);
394 if (wtx == NULL)
395 goto error_wtx_alloc;
396 wtx->urb = usb_alloc_urb(0, gfp_mask);
397 if (wtx->urb == NULL)
398 goto error_urb_alloc;
399 epd = &i1480u->usb_iface->cur_altsetting->endpoint[2].desc;
400 usb_pipe = usb_sndbulkpipe(i1480u->usb_dev, epd->bEndpointAddress);
401
402 if (skb->len > pl_max_size) {
403 result = i1480u_tx_create_n(wtx, skb, gfp_mask);
404 if (result < 0)
405 goto error_create;
406 usb_fill_bulk_urb(wtx->urb, i1480u->usb_dev, usb_pipe,
407 wtx->buf, wtx->buf_size, i1480u_tx_cb, wtx);
408 } else {
409 result = i1480u_tx_create_1(wtx, skb, gfp_mask);
410 if (result < 0)
411 goto error_create;
412 usb_fill_bulk_urb(wtx->urb, i1480u->usb_dev, usb_pipe,
413 skb->data, skb->len, i1480u_tx_cb, wtx);
414 }
415 spin_lock_irqsave(&i1480u->tx_list_lock, flags);
416 list_add(&wtx->list_node, &i1480u->tx_list);
417 spin_unlock_irqrestore(&i1480u->tx_list_lock, flags);
418 return wtx;
419
420error_create:
421 kfree(wtx->urb);
422error_urb_alloc:
423 kfree(wtx);
424error_wtx_alloc:
425 return NULL;
426}
427
428
429
430
431
432
433
434
435
436
437
438
439
440int i1480u_xmit_frame(struct wlp *wlp, struct sk_buff *skb,
441 struct uwb_dev_addr *dst)
442{
443 int result = -ENXIO;
444 struct i1480u *i1480u = container_of(wlp, struct i1480u, wlp);
445 struct device *dev = &i1480u->usb_iface->dev;
446 struct net_device *net_dev = i1480u->net_dev;
447 struct i1480u_tx *wtx;
448 struct wlp_tx_hdr *wlp_tx_hdr;
449 static unsigned char dev_bcast[2] = { 0xff, 0xff };
450#if 0
451 int lockup = 50;
452#endif
453
454 d_fnstart(6, dev, "(skb %p (%u), net_dev %p)\n", skb, skb->len,
455 net_dev);
456 BUG_ON(i1480u->wlp.rc == NULL);
457 if ((net_dev->flags & IFF_UP) == 0)
458 goto out;
459 result = -EBUSY;
460 if (atomic_read(&i1480u->tx_inflight.count) >= i1480u->tx_inflight.max) {
461 if (d_test(2) && printk_ratelimit())
462 d_printf(2, dev, "Max frames in flight "
463 "stopping queue.\n");
464 netif_stop_queue(net_dev);
465 goto error_max_inflight;
466 }
467 result = -ENOMEM;
468 wtx = i1480u_tx_create(i1480u, skb, GFP_ATOMIC);
469 if (unlikely(wtx == NULL)) {
470 if (printk_ratelimit())
471 dev_err(dev, "TX: no memory for WLP TX URB,"
472 "dropping packet (in flight %d)\n",
473 atomic_read(&i1480u->tx_inflight.count));
474 netif_stop_queue(net_dev);
475 goto error_wtx_alloc;
476 }
477 wtx->i1480u = i1480u;
478
479
480
481
482 wlp_tx_hdr = wtx->wlp_tx_hdr;
483 *wlp_tx_hdr = i1480u->options.def_tx_hdr;
484 wlp_tx_hdr->dstaddr = *dst;
485 if (!memcmp(&wlp_tx_hdr->dstaddr, dev_bcast, sizeof(dev_bcast))
486 && (wlp_tx_hdr_delivery_id_type(wlp_tx_hdr) & WLP_DRP)) {
487
488
489 wlp_tx_hdr_set_delivery_id_type(wlp_tx_hdr, i1480u->options.pca_base_priority);
490 }
491
492#if 0
493 dev_info(dev, "TX delivering skb -> USB, %zu bytes\n", skb->len);
494 dump_bytes(dev, skb->data, skb->len > 72 ? 72 : skb->len);
495#endif
496#if 0
497
498 if (lockup && ((i1480u->stats.tx_packets + 1) % lockup) == 0) {
499
500 net_dev->trans_start = jiffies;
501 netif_stop_queue(net_dev);
502 dev_err(dev, "Simulate lockup at %ld\n", jiffies);
503 return result;
504 }
505#endif
506
507 result = usb_submit_urb(wtx->urb, GFP_ATOMIC);
508 if (result < 0) {
509 dev_err(dev, "TX: cannot submit URB: %d\n", result);
510
511 wtx->skb = NULL;
512 goto error_tx_urb_submit;
513 }
514 atomic_inc(&i1480u->tx_inflight.count);
515 net_dev->trans_start = jiffies;
516 d_fnend(6, dev, "(skb %p (%u), net_dev %p) = %d\n", skb, skb->len,
517 net_dev, result);
518 return result;
519
520error_tx_urb_submit:
521 i1480u_tx_destroy(i1480u, wtx);
522error_wtx_alloc:
523error_max_inflight:
524out:
525 d_fnend(6, dev, "(skb %p (%u), net_dev %p) = %d\n", skb, skb->len,
526 net_dev, result);
527 return result;
528}
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547int i1480u_hard_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
548{
549 int result;
550 struct i1480u *i1480u = netdev_priv(net_dev);
551 struct device *dev = &i1480u->usb_iface->dev;
552 struct uwb_dev_addr dst;
553
554 d_fnstart(6, dev, "(skb %p (%u), net_dev %p)\n", skb, skb->len,
555 net_dev);
556 BUG_ON(i1480u->wlp.rc == NULL);
557 if ((net_dev->flags & IFF_UP) == 0)
558 goto error;
559 result = wlp_prepare_tx_frame(dev, &i1480u->wlp, skb, &dst);
560 if (result < 0) {
561 dev_err(dev, "WLP verification of TX frame failed (%d). "
562 "Dropping packet.\n", result);
563 goto error;
564 } else if (result == 1) {
565 d_printf(6, dev, "WLP will transmit frame. \n");
566
567
568 goto out;
569 }
570 d_printf(6, dev, "Transmitting frame. \n");
571 result = i1480u_xmit_frame(&i1480u->wlp, skb, &dst);
572 if (result < 0) {
573 dev_err(dev, "Frame TX failed (%d).\n", result);
574 goto error;
575 }
576 d_fnend(6, dev, "(skb %p (%u), net_dev %p) = %d\n", skb, skb->len,
577 net_dev, result);
578 return NETDEV_TX_OK;
579error:
580 dev_kfree_skb_any(skb);
581 i1480u->stats.tx_dropped++;
582out:
583 d_fnend(6, dev, "(skb %p (%u), net_dev %p) = %d\n", skb, skb->len,
584 net_dev, result);
585 return NETDEV_TX_OK;
586}
587
588
589
590
591
592
593void i1480u_tx_timeout(struct net_device *net_dev)
594{
595 struct i1480u *i1480u = netdev_priv(net_dev);
596
597 wlp_reset_all(&i1480u->wlp);
598}
599
600
601void i1480u_tx_release(struct i1480u *i1480u)
602{
603 unsigned long flags;
604 struct i1480u_tx *wtx, *next;
605 int count = 0, empty;
606
607 spin_lock_irqsave(&i1480u->tx_list_lock, flags);
608 list_for_each_entry_safe(wtx, next, &i1480u->tx_list, list_node) {
609 count++;
610 usb_unlink_urb(wtx->urb);
611 }
612 spin_unlock_irqrestore(&i1480u->tx_list_lock, flags);
613 count = count*10;
614
615
616
617
618
619
620
621
622 while (1) {
623 spin_lock_irqsave(&i1480u->tx_list_lock, flags);
624 empty = list_empty(&i1480u->tx_list);
625 spin_unlock_irqrestore(&i1480u->tx_list_lock, flags);
626 if (empty)
627 break;
628 count--;
629 BUG_ON(count == 0);
630 msleep(20);
631 }
632}
633