ACPI: thinkpad-acpi: fix regression on HKEY LID event handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / wme.c
blob7ab82b376e1bc92f8d656bfa30f7a3cf4b2b1152
1 /*
2 * Copyright 2004, Instant802 Networks, Inc.
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 */
9 #include <linux/netdevice.h>
10 #include <linux/skbuff.h>
11 #include <linux/module.h>
12 #include <linux/if_arp.h>
13 #include <linux/types.h>
14 #include <net/ip.h>
15 #include <net/pkt_sched.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "wme.h"
21 static inline int WLAN_FC_IS_QOS_DATA(u16 fc)
23 return (fc & 0x8C) == 0x88;
27 ieee80211_txrx_result
28 ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
30 u8 *data = rx->skb->data;
31 int tid;
33 /* does the frame have a qos control field? */
34 if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
35 u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
36 /* frame has qos control */
37 tid = qc[0] & QOS_CONTROL_TID_MASK;
38 } else {
39 if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
40 /* Separate TID for management frames */
41 tid = NUM_RX_DATA_QUEUES - 1;
42 } else {
43 /* no qos control present */
44 tid = 0; /* 802.1d - Best Effort */
47 #ifdef CONFIG_MAC80211_DEBUG_COUNTERS
48 I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
49 if (rx->sta) {
50 I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
52 #endif /* CONFIG_MAC80211_DEBUG_COUNTERS */
54 rx->u.rx.queue = tid;
55 /* Set skb->priority to 1d tag if highest order bit of TID is not set.
56 * For now, set skb->priority to 0 for other cases. */
57 rx->skb->priority = (tid > 7) ? 0 : tid;
59 return TXRX_CONTINUE;
63 ieee80211_txrx_result
64 ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
66 u16 fc = rx->fc;
67 u8 *data = rx->skb->data;
68 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
70 if (!WLAN_FC_IS_QOS_DATA(fc))
71 return TXRX_CONTINUE;
73 /* remove the qos control field, update frame type and meta-data */
74 memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
75 hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
76 /* change frame type to non QOS */
77 rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
78 hdr->frame_control = cpu_to_le16(fc);
80 return TXRX_CONTINUE;
84 #ifdef CONFIG_NET_SCHED
85 /* maximum number of hardware queues we support. */
86 #define TC_80211_MAX_QUEUES 8
88 struct ieee80211_sched_data
90 struct tcf_proto *filter_list;
91 struct Qdisc *queues[TC_80211_MAX_QUEUES];
92 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
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)
99 struct iphdr *ip;
100 int dscp;
101 int offset;
103 struct ieee80211_sched_data *q = qdisc_priv(qd);
104 struct tcf_result res = { -1, 0 };
106 /* if there is a user set filter list, call out to that */
107 if (q->filter_list) {
108 tc_classify(skb, q->filter_list, &res);
109 if (res.class != -1)
110 return res.class;
113 /* skb->priority values from 256->263 are magic values to
114 * directly indicate a specific 802.1d priority.
115 * This is used to allow 802.1d priority to be passed directly in
116 * from VLAN tags, etc. */
117 if (skb->priority >= 256 && skb->priority <= 263)
118 return skb->priority - 256;
120 /* check there is a valid IP header present */
121 offset = ieee80211_get_hdrlen_from_skb(skb) + 8 /* LLC + proto */;
122 if (skb->protocol != __constant_htons(ETH_P_IP) ||
123 skb->len < offset + sizeof(*ip))
124 return 0;
126 ip = (struct iphdr *) (skb->data + offset);
128 dscp = ip->tos & 0xfc;
129 if (dscp & 0x1c)
130 return 0;
131 return dscp >> 5;
135 static inline int wme_downgrade_ac(struct sk_buff *skb)
137 switch (skb->priority) {
138 case 6:
139 case 7:
140 skb->priority = 5; /* VO -> VI */
141 return 0;
142 case 4:
143 case 5:
144 skb->priority = 3; /* VI -> BE */
145 return 0;
146 case 0:
147 case 3:
148 skb->priority = 2; /* BE -> BK */
149 return 0;
150 default:
151 return -1;
156 /* positive return value indicates which queue to use
157 * negative return value indicates to drop the frame */
158 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
160 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
161 struct ieee80211_tx_packet_data *pkt_data =
162 (struct ieee80211_tx_packet_data *) skb->cb;
163 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
164 unsigned short fc = le16_to_cpu(hdr->frame_control);
165 int qos;
166 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
168 /* see if frame is data or non data frame */
169 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
170 /* management frames go on AC_VO queue, but are sent
171 * without QoS control fields */
172 return IEEE80211_TX_QUEUE_DATA0;
175 if (unlikely(pkt_data->mgmt_iface)) {
176 /* Data frames from hostapd (mainly, EAPOL) use AC_VO
177 * and they will include QoS control fields if
178 * the target STA is using WME. */
179 skb->priority = 7;
180 return ieee802_1d_to_ac[skb->priority];
183 /* is this a QoS frame? */
184 qos = fc & IEEE80211_STYPE_QOS_DATA;
186 if (!qos) {
187 skb->priority = 0; /* required for correct WPA/11i MIC */
188 return ieee802_1d_to_ac[skb->priority];
191 /* use the data classifier to determine what 802.1d tag the
192 * data frame has */
193 skb->priority = classify_1d(skb, qd);
195 /* incase we are a client verify acm is not set for this ac */
196 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
197 if (wme_downgrade_ac(skb)) {
198 /* No AC with lower priority has acm=0,
199 * drop packet. */
200 return -1;
204 /* look up which queue to use for frames with this 1d tag */
205 return ieee802_1d_to_ac[skb->priority];
209 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
211 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
212 struct ieee80211_sched_data *q = qdisc_priv(qd);
213 struct ieee80211_tx_packet_data *pkt_data =
214 (struct ieee80211_tx_packet_data *) skb->cb;
215 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
216 unsigned short fc = le16_to_cpu(hdr->frame_control);
217 struct Qdisc *qdisc;
218 int err, queue;
220 if (pkt_data->requeue) {
221 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
222 qd->q.qlen++;
223 return 0;
226 queue = classify80211(skb, qd);
228 /* now we know the 1d priority, fill in the QoS header if there is one
230 if (WLAN_FC_IS_QOS_DATA(fc)) {
231 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
232 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
233 if (local->wifi_wme_noack_test)
234 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
235 QOS_CONTROL_ACK_POLICY_SHIFT;
236 /* qos header is 2 bytes, second reserved */
237 *p = qos_hdr;
238 p++;
239 *p = 0;
242 if (unlikely(queue >= local->hw.queues)) {
243 #if 0
244 if (net_ratelimit()) {
245 printk(KERN_DEBUG "%s - queue=%d (hw does not "
246 "support) -> %d\n",
247 __func__, queue, local->hw.queues - 1);
249 #endif
250 queue = local->hw.queues - 1;
253 if (unlikely(queue < 0)) {
254 kfree_skb(skb);
255 err = NET_XMIT_DROP;
256 } else {
257 pkt_data->queue = (unsigned int) queue;
258 qdisc = q->queues[queue];
259 err = qdisc->enqueue(skb, qdisc);
260 if (err == NET_XMIT_SUCCESS) {
261 qd->q.qlen++;
262 qd->bstats.bytes += skb->len;
263 qd->bstats.packets++;
264 return NET_XMIT_SUCCESS;
267 qd->qstats.drops++;
268 return err;
272 /* TODO: clean up the cases where master_hard_start_xmit
273 * returns non 0 - it shouldn't ever do that. Once done we
274 * can remove this function */
275 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
277 struct ieee80211_sched_data *q = qdisc_priv(qd);
278 struct ieee80211_tx_packet_data *pkt_data =
279 (struct ieee80211_tx_packet_data *) skb->cb;
280 struct Qdisc *qdisc;
281 int err;
283 /* we recorded which queue to use earlier! */
284 qdisc = q->queues[pkt_data->queue];
286 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
287 qd->q.qlen++;
288 return 0;
290 qd->qstats.drops++;
291 return err;
295 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
297 struct ieee80211_sched_data *q = qdisc_priv(qd);
298 struct net_device *dev = qd->dev;
299 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
300 struct ieee80211_hw *hw = &local->hw;
301 struct sk_buff *skb;
302 struct Qdisc *qdisc;
303 int queue;
305 /* check all the h/w queues in numeric/priority order */
306 for (queue = 0; queue < hw->queues; queue++) {
307 /* see if there is room in this hardware queue */
308 if (test_bit(IEEE80211_LINK_STATE_XOFF,
309 &local->state[queue]) ||
310 test_bit(IEEE80211_LINK_STATE_PENDING,
311 &local->state[queue]))
312 continue;
314 /* there is space - try and get a frame */
315 skb = skb_dequeue(&q->requeued[queue]);
316 if (skb) {
317 qd->q.qlen--;
318 return skb;
321 qdisc = q->queues[queue];
322 skb = qdisc->dequeue(qdisc);
323 if (skb) {
324 qd->q.qlen--;
325 return skb;
328 /* returning a NULL here when all the h/w queues are full means we
329 * never need to call netif_stop_queue in the driver */
330 return NULL;
334 static void wme_qdiscop_reset(struct Qdisc* qd)
336 struct ieee80211_sched_data *q = qdisc_priv(qd);
337 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
338 struct ieee80211_hw *hw = &local->hw;
339 int queue;
341 /* QUESTION: should we have some hardware flush functionality here? */
343 for (queue = 0; queue < hw->queues; queue++) {
344 skb_queue_purge(&q->requeued[queue]);
345 qdisc_reset(q->queues[queue]);
347 qd->q.qlen = 0;
351 static void wme_qdiscop_destroy(struct Qdisc* qd)
353 struct ieee80211_sched_data *q = qdisc_priv(qd);
354 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
355 struct ieee80211_hw *hw = &local->hw;
356 int queue;
358 tcf_destroy_chain(q->filter_list);
359 q->filter_list = NULL;
361 for (queue=0; queue < hw->queues; queue++) {
362 skb_queue_purge(&q->requeued[queue]);
363 qdisc_destroy(q->queues[queue]);
364 q->queues[queue] = &noop_qdisc;
369 /* called whenever parameters are updated on existing qdisc */
370 static int wme_qdiscop_tune(struct Qdisc *qd, struct rtattr *opt)
372 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
374 /* check our options block is the right size */
375 /* copy any options to our local structure */
376 /* Ignore options block for now - always use static mapping
377 struct tc_ieee80211_qopt *qopt = RTA_DATA(opt);
379 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
380 return -EINVAL;
381 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
383 return 0;
387 /* called during initial creation of qdisc on device */
388 static int wme_qdiscop_init(struct Qdisc *qd, struct rtattr *opt)
390 struct ieee80211_sched_data *q = qdisc_priv(qd);
391 struct net_device *dev = qd->dev;
392 struct ieee80211_local *local;
393 int queues;
394 int err = 0, i;
396 /* check that device is a mac80211 device */
397 if (!dev->ieee80211_ptr ||
398 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
399 return -EINVAL;
401 /* check this device is an ieee80211 master type device */
402 if (dev->type != ARPHRD_IEEE80211)
403 return -EINVAL;
405 /* check that there is no qdisc currently attached to device
406 * this ensures that we will be the root qdisc. (I can't find a better
407 * way to test this explicitly) */
408 if (dev->qdisc_sleeping != &noop_qdisc)
409 return -EINVAL;
411 if (qd->flags & TCQ_F_INGRESS)
412 return -EINVAL;
414 local = wdev_priv(dev->ieee80211_ptr);
415 queues = local->hw.queues;
417 /* if options were passed in, set them */
418 if (opt) {
419 err = wme_qdiscop_tune(qd, opt);
422 /* create child queues */
423 for (i = 0; i < queues; i++) {
424 skb_queue_head_init(&q->requeued[i]);
425 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
426 qd->handle);
427 if (!q->queues[i]) {
428 q->queues[i] = &noop_qdisc;
429 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
433 return err;
436 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
438 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
439 unsigned char *p = skb->tail;
440 struct tc_ieee80211_qopt opt;
442 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
443 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
444 */ return skb->len;
446 rtattr_failure:
447 skb_trim(skb, p - skb->data);*/
448 return -1;
452 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
453 struct Qdisc *new, struct Qdisc **old)
455 struct ieee80211_sched_data *q = qdisc_priv(qd);
456 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
457 struct ieee80211_hw *hw = &local->hw;
458 unsigned long queue = arg - 1;
460 if (queue >= hw->queues)
461 return -EINVAL;
463 if (!new)
464 new = &noop_qdisc;
466 sch_tree_lock(qd);
467 *old = q->queues[queue];
468 q->queues[queue] = new;
469 qdisc_reset(*old);
470 sch_tree_unlock(qd);
472 return 0;
476 static struct Qdisc *
477 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
479 struct ieee80211_sched_data *q = qdisc_priv(qd);
480 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
481 struct ieee80211_hw *hw = &local->hw;
482 unsigned long queue = arg - 1;
484 if (queue >= hw->queues)
485 return NULL;
487 return q->queues[queue];
491 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
493 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
494 struct ieee80211_hw *hw = &local->hw;
495 unsigned long queue = TC_H_MIN(classid);
497 if (queue - 1 >= hw->queues)
498 return 0;
500 return queue;
504 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
505 u32 classid)
507 return wme_classop_get(qd, classid);
511 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
516 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
517 struct rtattr **tca, unsigned long *arg)
519 unsigned long cl = *arg;
520 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
521 struct ieee80211_hw *hw = &local->hw;
523 if (cl - 1 > hw->queues)
524 return -ENOENT;
526 /* TODO: put code to program hardware queue parameters here,
527 * to allow programming from tc command line */
529 return 0;
533 /* we don't support deleting hardware queues
534 * when we add WMM-SA support - TSPECs may be deleted here */
535 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
537 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
538 struct ieee80211_hw *hw = &local->hw;
540 if (cl - 1 > hw->queues)
541 return -ENOENT;
542 return 0;
546 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
547 struct sk_buff *skb, struct tcmsg *tcm)
549 struct ieee80211_sched_data *q = qdisc_priv(qd);
550 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
551 struct ieee80211_hw *hw = &local->hw;
553 if (cl - 1 > hw->queues)
554 return -ENOENT;
555 tcm->tcm_handle = TC_H_MIN(cl);
556 tcm->tcm_parent = qd->handle;
557 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
558 return 0;
562 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
564 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
565 struct ieee80211_hw *hw = &local->hw;
566 int queue;
568 if (arg->stop)
569 return;
571 for (queue = 0; queue < hw->queues; queue++) {
572 if (arg->count < arg->skip) {
573 arg->count++;
574 continue;
576 /* we should return classids for our internal queues here
577 * as well as the external ones */
578 if (arg->fn(qd, queue+1, arg) < 0) {
579 arg->stop = 1;
580 break;
582 arg->count++;
587 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
588 unsigned long cl)
590 struct ieee80211_sched_data *q = qdisc_priv(qd);
592 if (cl)
593 return NULL;
595 return &q->filter_list;
599 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
600 * - these are the operations on the classes */
601 static struct Qdisc_class_ops class_ops =
603 .graft = wme_classop_graft,
604 .leaf = wme_classop_leaf,
606 .get = wme_classop_get,
607 .put = wme_classop_put,
608 .change = wme_classop_change,
609 .delete = wme_classop_delete,
610 .walk = wme_classop_walk,
612 .tcf_chain = wme_classop_find_tcf,
613 .bind_tcf = wme_classop_bind,
614 .unbind_tcf = wme_classop_put,
616 .dump = wme_classop_dump_class,
620 /* queueing discipline operations */
621 static struct Qdisc_ops wme_qdisc_ops =
623 .next = NULL,
624 .cl_ops = &class_ops,
625 .id = "ieee80211",
626 .priv_size = sizeof(struct ieee80211_sched_data),
628 .enqueue = wme_qdiscop_enqueue,
629 .dequeue = wme_qdiscop_dequeue,
630 .requeue = wme_qdiscop_requeue,
631 .drop = NULL, /* drop not needed since we are always the root qdisc */
633 .init = wme_qdiscop_init,
634 .reset = wme_qdiscop_reset,
635 .destroy = wme_qdiscop_destroy,
636 .change = wme_qdiscop_tune,
638 .dump = wme_qdiscop_dump,
642 void ieee80211_install_qdisc(struct net_device *dev)
644 struct Qdisc *qdisc;
646 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
647 if (!qdisc) {
648 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
649 return;
652 /* same handle as would be allocated by qdisc_alloc_handle() */
653 qdisc->handle = 0x80010000;
655 qdisc_lock_tree(dev);
656 list_add_tail(&qdisc->list, &dev->qdisc_list);
657 dev->qdisc_sleeping = qdisc;
658 qdisc_unlock_tree(dev);
662 int ieee80211_qdisc_installed(struct net_device *dev)
664 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
668 int ieee80211_wme_register(void)
670 return register_qdisc(&wme_qdisc_ops);
674 void ieee80211_wme_unregister(void)
676 unregister_qdisc(&wme_qdisc_ops);
678 #endif /* CONFIG_NET_SCHED */