add devicescape 802.11 stack
[openwrt/staging/mkresin.git] / openwrt / target / linux / package / ieee80211-dscape / src / wme.c
1 /*
2 * Copyright 2004, Instant802 Networks, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9 #include <linux/config.h>
10 #include <linux/version.h>
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
13 #include <linux/module.h>
14 #include <net/ip.h>
15
16 #include <net/ieee80211.h>
17 #include "ieee80211_i.h"
18 #include "wme.h"
19
20 #define CHILD_QDISC_OPS pfifo_qdisc_ops
21
22 static inline int WLAN_FC_IS_QOS_DATA(u16 fc)
23 {
24 return (fc & 0x8C) == 0x88;
25 }
26
27
28 ieee80211_txrx_result
29 ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
30 {
31 u8 *data = rx->skb->data;
32 int tid;
33
34 /* does the frame have a qos control field? */
35 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
36 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
37 /* frame has qos control */
38 tid = qc[0] & QOS_CONTROL_TID_MASK;
39 } else {
40 if (unlikely(WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_MGMT)) {
41 /* Separate TID for management frames */
42 tid = NUM_RX_DATA_QUEUES - 1;
43 } else {
44 /* no qos control present */
45 tid = 0; /* 802.1d - Best Effort */
46 }
47 }
48 #ifdef CONFIG_IEEE80211_DEBUG_COUNTERS
49 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
50 if (rx->sta) {
51 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
52 }
53 #endif /* CONFIG_IEEE80211_DEBUG_COUNTERS */
54
55 rx->u.rx.queue = tid;
56 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
57 * For now, set skb->priority to 0 for other cases. */
58 rx->skb->priority = (tid > 7) ? 0 : tid;
59
60 return TXRX_CONTINUE;
61 }
62
63
64 ieee80211_txrx_result
65 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
66 {
67 u16 fc = rx->fc;
68 u8 *data = rx->skb->data;
69 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
70
71 if (!WLAN_FC_IS_QOS_DATA(fc))
72 return TXRX_CONTINUE;
73
74 /* remove the qos control field, update frame type and meta-data */
75 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
76 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
77 /* change frame type to non QOS */
78 rx->fc = fc &= ~(WLAN_FC_STYPE_QOS_DATA << 4);
79 hdr->frame_control = cpu_to_le16(fc);
80
81 return TXRX_CONTINUE;
82 }
83
84
85 /* maximum number of hardware queues we support. */
86 #define TC_80211_MAX_QUEUES 8
87
88 struct ieee80211_sched_data
89 {
90 struct tcf_proto *filter_list;
91 struct Qdisc *queues[TC_80211_MAX_QUEUES];
92 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
93 };
94
95
96 /* given a data frame determine the 802.1p/1d tag to use */
97 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
98 {
99 struct iphdr *ip;
100 int dscp;
101 int offset;
102
103 #ifdef CONFIG_NET_SCHED
104 struct ieee80211_sched_data *q = qdisc_priv(qd);
105 struct tcf_result res = { -1, 0 };
106
107 /* if there is a user set filter list, call out to that */
108 if (q->filter_list) {
109 tc_classify(skb, q->filter_list, &res);
110 if (res.class != -1)
111 return res.class;
112 }
113 #endif /* CONFIG_NET_SCHED */
114
115 /* skb->priority values from 256->263 are magic values to
116 * directly indicate a specific 802.1d priority.
117 * This is used to allow 802.1d priority to be passed directly in
118 * from VLAN tags, etc. */
119 if (skb->priority >= 256 && skb->priority <= 263)
120 return skb->priority - 256;
121
122 /* check there is a valid IP header present */
123 offset = ieee80211_get_hdrlen_from_skb(skb) + 8 /* LLC + proto */;
124 if (skb->protocol != __constant_htons(ETH_P_IP) ||
125 skb->len < offset + sizeof(*ip))
126 return 0;
127
128 ip = (struct iphdr *) (skb->data + offset);
129
130 dscp = ip->tos & 0xfc;
131 switch (dscp) {
132 case 0x20:
133 return 2;
134 case 0x40:
135 return 1;
136 case 0x60:
137 return 3;
138 case 0x80:
139 return 4;
140 case 0xa0:
141 return 5;
142 case 0xc0:
143 return 6;
144 case 0xe0:
145 return 7;
146 default:
147 return 0;
148 }
149 }
150
151
152
153
154 static inline int wme_downgrade_ac(struct sk_buff *skb)
155 {
156 switch (skb->priority) {
157 case 6:
158 case 7:
159 skb->priority = 5; /* VO -> VI */
160 return 0;
161 case 4:
162 case 5:
163 skb->priority = 3; /* VI -> BE */
164 return 0;
165 case 0:
166 case 3:
167 skb->priority = 2; /* BE -> BK */
168 return 0;
169 default:
170 return -1;
171 }
172 }
173
174
175 /* positive return value indicates which queue to use
176 * negative return value indicates to drop the frame */
177 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
178 {
179 struct ieee80211_local *local = qd->dev->priv;
180 struct ieee80211_tx_packet_data *pkt_data =
181 (struct ieee80211_tx_packet_data *) skb->cb;
182 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
183 unsigned short fc = le16_to_cpu(hdr->frame_control);
184 int qos;
185 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
186
187 if (unlikely(pkt_data->magic != IEEE80211_CB_MAGIC))
188 return -1;
189
190 /* see if frame is data or non data frame */
191 if (unlikely(WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_DATA)) {
192 /* management frames go on AC_VO queue, but are sent
193 * without QoS control fields */
194 return IEEE80211_TX_QUEUE_DATA0;
195 }
196
197 if (unlikely(pkt_data->sdata->type == IEEE80211_SUB_IF_TYPE_MGMT)) {
198 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
199 * and they will include QoS control fields if
200 * the target STA is using WME. */
201 skb->priority = 7;
202 return ieee802_1d_to_ac[skb->priority];
203 }
204
205
206 /* is this a QoS frame? */
207 qos = fc & (WLAN_FC_STYPE_QOS_DATA << 4);
208
209
210 if (!qos) {
211 skb->priority = 0; /* required for correct WPA/11i MIC */
212 return ieee802_1d_to_ac[skb->priority];
213 }
214
215 /* use the data classifier to determine what 802.1d tag the
216 * data frame has */
217 skb->priority = classify_1d(skb, qd);
218
219
220 /* incase we are a client verify acm is not set for this ac */
221 for (; unlikely(local->wmm_acm & BIT(skb->priority)); )
222 {
223 if (wme_downgrade_ac(skb)) {
224 /* No AC with lower priority has acm=0,
225 * drop packet. */
226 return -1;
227 }
228 }
229
230 /* look up which queue to use for frames with this 1d tag */
231 return ieee802_1d_to_ac[skb->priority];
232 }
233
234
235 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
236 {
237 struct ieee80211_local *local = qd->dev->priv;
238 struct ieee80211_sched_data *q = qdisc_priv(qd);
239 struct ieee80211_tx_packet_data *pkt_data =
240 (struct ieee80211_tx_packet_data *) skb->cb;
241 struct ieee80211_tx_control *control = &pkt_data->control;
242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
243 unsigned short fc = le16_to_cpu(hdr->frame_control);
244 struct Qdisc *qdisc;
245 int err, queue;
246
247 if (control->requeue) {
248 skb_queue_tail(&q->requeued[control->queue], skb);
249 return 0;
250 }
251
252 queue = classify80211(skb, qd);
253
254 /* now we know the 1d priority, fill in the QoS header if there is one */
255 if (WLAN_FC_IS_QOS_DATA(fc)) {
256 struct qos_control *qc = (struct qos_control *) (skb->data + ieee80211_get_hdrlen(fc) - 2);
257 u8 *p = (u8 *) qc;
258 *p++ = 0; /* do this due to gcc's lack of optimization on bitfield ops */
259 *p = 0;
260 qc->tag1d = skb->priority;
261 if (local->wifi_wme_noack_test)
262 qc->ack_policy = 1;
263 }
264
265 if (unlikely(queue >= local->hw->queues)) {
266 #if 0
267 if (net_ratelimit()) {
268 printk(KERN_DEBUG "%s - queue=%d (hw does not "
269 "support) -> %d\n",
270 __func__, queue, local->hw->queues - 1);
271 }
272 #endif
273 queue = local->hw->queues - 1;
274 }
275
276 if (unlikely(queue < 0)) {
277 kfree_skb(skb);
278 err = NET_XMIT_DROP;
279 } else {
280 control->queue = (unsigned int) queue;
281 qdisc = q->queues[queue];
282 err = qdisc->enqueue(skb, qdisc);
283 if (err == NET_XMIT_SUCCESS) {
284 qd->q.qlen++;
285 qd->bstats.bytes += skb->len;
286 qd->bstats.packets++;
287 return NET_XMIT_SUCCESS;
288 }
289 }
290 qd->qstats.drops++;
291 return err;
292 }
293
294
295 /* TODO: clean up the cases where master_hard_start_xmit
296 * returns non 0 - it shouldn't ever do that. Once done we
297 * can remove this function */
298 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
299 {
300 struct ieee80211_sched_data *q = qdisc_priv(qd);
301 struct ieee80211_tx_packet_data *pkt_data =
302 (struct ieee80211_tx_packet_data *) skb->cb;
303 struct Qdisc *qdisc;
304 int err;
305
306 /* we recorded which queue to use earlier! */
307 qdisc = q->queues[pkt_data->control.queue];
308
309 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
310 qd->q.qlen++;
311 return 0;
312 }
313 qd->qstats.drops++;
314 return err;
315 }
316
317
318 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
319 {
320 struct ieee80211_sched_data *q = qdisc_priv(qd);
321 struct net_device *dev = qd->dev;
322 struct ieee80211_local *local = dev->priv;
323 struct ieee80211_hw *hw = local->hw;
324 struct ieee80211_tx_queue_stats stats;
325 struct sk_buff *skb;
326 struct Qdisc *qdisc;
327 int queue;
328
329 /* find which hardware queues have space in them */
330 hw->get_tx_stats(dev, &stats);
331
332 /* check all the h/w queues in numeric/priority order */
333 for (queue = 0; queue < hw->queues; queue++) {
334 /* see if there is room in this hardware queue */
335 if (stats.data[queue].len >= stats.data[queue].limit)
336 continue;
337
338 /* there is space - try and get a frame */
339 skb = skb_dequeue(&q->requeued[queue]);
340 if (skb)
341 return skb;
342
343 qdisc = q->queues[queue];
344 skb = qdisc->dequeue(qdisc);
345 if (skb) {
346 qd->q.qlen--;
347 return skb;
348 }
349 }
350 /* returning a NULL here when all the h/w queues are full means we
351 * never need to call netif_stop_queue in the driver */
352 return NULL;
353 }
354
355
356 static void wme_qdiscop_reset(struct Qdisc* qd)
357 {
358 struct ieee80211_sched_data *q = qdisc_priv(qd);
359 struct ieee80211_local *local = qd->dev->priv;
360 struct ieee80211_hw *hw = local->hw;
361 int queue;
362
363 /* QUESTION: should we have some hardware flush functionality here? */
364
365 for (queue = 0; queue < hw->queues; queue++) {
366 skb_queue_purge(&q->requeued[queue]);
367 qdisc_reset(q->queues[queue]);
368 }
369 qd->q.qlen = 0;
370 }
371
372
373 static void wme_qdiscop_destroy(struct Qdisc* qd)
374 {
375 struct ieee80211_sched_data *q = qdisc_priv(qd);
376 struct ieee80211_local *local = qd->dev->priv;
377 struct ieee80211_hw *hw = local->hw;
378 struct tcf_proto *tp;
379 int queue;
380
381 while ((tp = q->filter_list) != NULL) {
382 q->filter_list = tp->next;
383 tp->ops->destroy(tp);
384 }
385
386 for (queue=0; queue < hw->queues; queue++) {
387 skb_queue_purge(&q->requeued[queue]);
388 qdisc_destroy(q->queues[queue]);
389 q->queues[queue] = &noop_qdisc;
390 }
391 }
392
393
394 /* called whenever parameters are updated on existing qdisc */
395 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
396 {
397 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
398 */
399 /* check our options block is the right size */
400 /* copy any options to our local structure */
401 /* Ignore options block for now - always use static mapping
402 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
403
404 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
405 return -EINVAL;
406 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
407 */
408 return 0;
409 }
410
411
412 /* called during initial creation of qdisc on device */
413 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
414 {
415 struct ieee80211_sched_data *q = qdisc_priv(qd);
416 struct net_device *dev = qd->dev;
417 struct ieee80211_local *local = dev->priv;
418 int queues = local->hw->queues;
419 int err = 0, i;
420
421 /* check this device is an ieee80211 master type device */
422 if (dev->type != ARPHRD_IEEE80211)
423 return -EINVAL;
424
425 /* check that there is no qdisc currently attached to device
426 * this ensures that we will be the root qdisc. (I can't find a better
427 * way to test this explicitly) */
428 if (dev->qdisc_sleeping != &noop_qdisc)
429 return -EINVAL;
430
431 if (qd->flags & TCQ_F_INGRESS)
432 return -EINVAL;
433
434 /* if options were passed in, set them */
435 if (opt) {
436 err = wme_qdiscop_tune(qd, opt);
437 }
438
439 /* create child queues */
440 for (i = 0; i < queues; i++) {
441 skb_queue_head_init(&q->requeued[i]);
442 q->queues[i] = qdisc_create_dflt(qd->dev, &CHILD_QDISC_OPS);
443 if (q->queues[i] == 0) {
444 q->queues[i] = &noop_qdisc;
445 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
446 }
447 }
448
449 return err;
450 }
451
452 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
453 {
454 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
455 unsigned char *p = skb->tail;
456 struct tc_ieee80211_qopt opt;
457
458 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
459 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
460 */ return skb->len;
461 /*
462 rtattr_failure:
463 skb_trim(skb, p - skb->data);*/
464 return -1;
465 }
466
467
468 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
469 struct Qdisc *new, struct Qdisc **old)
470 {
471 struct ieee80211_sched_data *q = qdisc_priv(qd);
472 struct ieee80211_local *local = qd->dev->priv;
473 struct ieee80211_hw *hw = local->hw;
474 unsigned long queue = arg - 1;
475
476 if (queue >= hw->queues)
477 return -EINVAL;
478
479 if (new == NULL)
480 new = &noop_qdisc;
481
482 sch_tree_lock(qd);
483 *old = q->queues[queue];
484 q->queues[queue] = new;
485 qdisc_reset(*old);
486 sch_tree_unlock(qd);
487
488 return 0;
489 }
490
491
492 static struct Qdisc *
493 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
494 {
495 struct ieee80211_sched_data *q = qdisc_priv(qd);
496 struct ieee80211_local *local = qd->dev->priv;
497 struct ieee80211_hw *hw = local->hw;
498 unsigned long queue = arg - 1;
499
500 if (queue >= hw->queues)
501 return NULL;
502
503 return q->queues[queue];
504 }
505
506
507 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
508 {
509 struct ieee80211_local *local = qd->dev->priv;
510 struct ieee80211_hw *hw = local->hw;
511 unsigned long queue = TC_H_MIN(classid);
512
513 if (queue - 1 >= hw->queues)
514 return 0;
515
516 return queue;
517 }
518
519
520 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
521 u32 classid)
522 {
523 return wme_classop_get(qd, classid);
524 }
525
526
527 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
528 {
529 /* printk(KERN_DEBUG "entering %s\n", __func__); */
530 }
531
532
533 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
534 struct rtattr **tca, unsigned long *arg)
535 {
536 unsigned long cl = *arg;
537 struct ieee80211_local *local = qd->dev->priv;
538 struct ieee80211_hw *hw = local->hw;
539 /* printk(KERN_DEBUG "entering %s\n", __func__); */
540
541 if (cl - 1 > hw->queues)
542 return -ENOENT;
543
544 /* TODO: put code to program hardware queue parameters here,
545 * to allow programming from tc command line */
546
547 return 0;
548 }
549
550
551 /* we don't support deleting hardware queues
552 * when we add WMM-SA support - TSPECs may be deleted here */
553 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
554 {
555 struct ieee80211_local *local = qd->dev->priv;
556 struct ieee80211_hw *hw = local->hw;
557 /* printk(KERN_DEBUG "entering %s\n", __func__); */
558
559 if (cl - 1 > hw->queues)
560 return -ENOENT;
561 return 0;
562 }
563
564
565 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
566 struct sk_buff *skb, struct tcmsg *tcm)
567 {
568 struct ieee80211_sched_data *q = qdisc_priv(qd);
569 struct ieee80211_local *local = qd->dev->priv;
570 struct ieee80211_hw *hw = local->hw;
571 /* printk(KERN_DEBUG "entering %s\n", __func__); */
572
573 if (cl - 1 > hw->queues)
574 return -ENOENT;
575 tcm->tcm_handle = TC_H_MIN(cl);
576 tcm->tcm_parent = qd->handle;
577 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
578 return 0;
579 }
580
581
582 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
583 {
584 struct ieee80211_local *local = qd->dev->priv;
585 struct ieee80211_hw *hw = local->hw;
586 int queue;
587 /* printk(KERN_DEBUG "entering %s\n", __func__); */
588
589 if (arg->stop)
590 return;
591
592 for (queue = 0; queue < hw->queues; queue++) {
593 if (arg->count < arg->skip) {
594 arg->count++;
595 continue;
596 }
597 /* we should return classids for our internal queues here
598 * as well as the external ones */
599 if (arg->fn(qd, queue+1, arg) < 0) {
600 arg->stop = 1;
601 break;
602 }
603 arg->count++;
604 }
605 }
606
607
608 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
609 unsigned long cl)
610 {
611 struct ieee80211_sched_data *q = qdisc_priv(qd);
612 /* printk("entering %s\n", __func__); */
613
614 if (cl)
615 return NULL;
616
617 return &q->filter_list;
618 }
619
620
621 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
622 * - these are the operations on the classes */
623 static struct Qdisc_class_ops class_ops =
624 {
625 .graft = wme_classop_graft,
626 .leaf = wme_classop_leaf,
627
628 .get = wme_classop_get,
629 .put = wme_classop_put,
630 .change = wme_classop_change,
631 .delete = wme_classop_delete,
632 .walk = wme_classop_walk,
633
634 .tcf_chain = wme_classop_find_tcf,
635 .bind_tcf = wme_classop_bind,
636 .unbind_tcf = wme_classop_put,
637
638 .dump = wme_classop_dump_class,
639 };
640
641
642 /* queueing discipline operations */
643 static struct Qdisc_ops wme_qdisc_ops =
644 {
645 .next = NULL,
646 .cl_ops = &class_ops,
647 .id = "ieee80211",
648 .priv_size = sizeof(struct ieee80211_sched_data),
649
650 .enqueue = wme_qdiscop_enqueue,
651 .dequeue = wme_qdiscop_dequeue,
652 .requeue = wme_qdiscop_requeue,
653 .drop = NULL, /* drop not needed since we are always the root qdisc */
654
655 .init = wme_qdiscop_init,
656 .reset = wme_qdiscop_reset,
657 .destroy = wme_qdiscop_destroy,
658 .change = wme_qdiscop_tune,
659
660 .dump = wme_qdiscop_dump,
661 };
662
663
664 void ieee80211_install_qdisc(struct net_device *dev)
665 {
666 struct Qdisc *qdisc;
667
668 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops);
669 if (qdisc == NULL) {
670 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
671 return;
672 }
673
674 /* same handle as would be allocated by qdisc_alloc_handle() */
675 qdisc->handle = 0x80010000;
676
677 write_lock(&qdisc_tree_lock);
678 list_add_tail(&qdisc->list, &dev->qdisc_list);
679 dev->qdisc_sleeping = qdisc;
680 write_unlock(&qdisc_tree_lock);
681 }
682
683
684 int ieee80211_wme_register(void)
685 {
686 int err = 0;
687
688 #ifdef CONFIG_NET_SCHED
689 err = register_qdisc(&wme_qdisc_ops);
690 #endif
691 return err;
692 }
693
694
695 void ieee80211_wme_unregister(void)
696 {
697 #ifdef CONFIG_NET_SCHED
698 unregister_qdisc(&wme_qdisc_ops);
699 #endif
700 }