Simplify semaphore implementation
[linux-2.6/mini2440.git] / net / mac80211 / wme.c
blob4e236599dd31cf0831f99157877571c120834d9c
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 /* maximum number of hardware queues we support. */
22 #define TC_80211_MAX_QUEUES 8
24 struct ieee80211_sched_data
26 struct tcf_proto *filter_list;
27 struct Qdisc *queues[TC_80211_MAX_QUEUES];
28 struct sk_buff_head requeued[TC_80211_MAX_QUEUES];
31 static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
33 /* given a data frame determine the 802.1p/1d tag to use */
34 static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
36 struct iphdr *ip;
37 int dscp;
38 int offset;
40 struct ieee80211_sched_data *q = qdisc_priv(qd);
41 struct tcf_result res = { -1, 0 };
43 /* if there is a user set filter list, call out to that */
44 if (q->filter_list) {
45 tc_classify(skb, q->filter_list, &res);
46 if (res.class != -1)
47 return res.class;
50 /* skb->priority values from 256->263 are magic values to
51 * directly indicate a specific 802.1d priority.
52 * This is used to allow 802.1d priority to be passed directly in
53 * from VLAN tags, etc. */
54 if (skb->priority >= 256 && skb->priority <= 263)
55 return skb->priority - 256;
57 /* check there is a valid IP header present */
58 offset = ieee80211_get_hdrlen_from_skb(skb);
59 if (skb->len < offset + sizeof(llc_ip_hdr) + sizeof(*ip) ||
60 memcmp(skb->data + offset, llc_ip_hdr, sizeof(llc_ip_hdr)))
61 return 0;
63 ip = (struct iphdr *) (skb->data + offset + sizeof(llc_ip_hdr));
65 dscp = ip->tos & 0xfc;
66 if (dscp & 0x1c)
67 return 0;
68 return dscp >> 5;
72 static inline int wme_downgrade_ac(struct sk_buff *skb)
74 switch (skb->priority) {
75 case 6:
76 case 7:
77 skb->priority = 5; /* VO -> VI */
78 return 0;
79 case 4:
80 case 5:
81 skb->priority = 3; /* VI -> BE */
82 return 0;
83 case 0:
84 case 3:
85 skb->priority = 2; /* BE -> BK */
86 return 0;
87 default:
88 return -1;
93 /* positive return value indicates which queue to use
94 * negative return value indicates to drop the frame */
95 static inline int classify80211(struct sk_buff *skb, struct Qdisc *qd)
97 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
98 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
99 unsigned short fc = le16_to_cpu(hdr->frame_control);
100 int qos;
101 const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
103 /* see if frame is data or non data frame */
104 if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)) {
105 /* management frames go on AC_VO queue, but are sent
106 * without QoS control fields */
107 return IEEE80211_TX_QUEUE_DATA0;
110 if (0 /* injected */) {
111 /* use AC from radiotap */
114 /* is this a QoS frame? */
115 qos = fc & IEEE80211_STYPE_QOS_DATA;
117 if (!qos) {
118 skb->priority = 0; /* required for correct WPA/11i MIC */
119 return ieee802_1d_to_ac[skb->priority];
122 /* use the data classifier to determine what 802.1d tag the
123 * data frame has */
124 skb->priority = classify_1d(skb, qd);
126 /* in case we are a client verify acm is not set for this ac */
127 while (unlikely(local->wmm_acm & BIT(skb->priority))) {
128 if (wme_downgrade_ac(skb)) {
129 /* No AC with lower priority has acm=0, drop packet. */
130 return -1;
134 /* look up which queue to use for frames with this 1d tag */
135 return ieee802_1d_to_ac[skb->priority];
139 static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
141 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
142 struct ieee80211_sched_data *q = qdisc_priv(qd);
143 struct ieee80211_tx_packet_data *pkt_data =
144 (struct ieee80211_tx_packet_data *) skb->cb;
145 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
146 unsigned short fc = le16_to_cpu(hdr->frame_control);
147 struct Qdisc *qdisc;
148 int err, queue;
150 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE) {
151 skb_queue_tail(&q->requeued[pkt_data->queue], skb);
152 qd->q.qlen++;
153 return 0;
156 queue = classify80211(skb, qd);
158 /* now we know the 1d priority, fill in the QoS header if there is one
160 if (WLAN_FC_IS_QOS_DATA(fc)) {
161 u8 *p = skb->data + ieee80211_get_hdrlen(fc) - 2;
162 u8 qos_hdr = skb->priority & QOS_CONTROL_TAG1D_MASK;
163 if (local->wifi_wme_noack_test)
164 qos_hdr |= QOS_CONTROL_ACK_POLICY_NOACK <<
165 QOS_CONTROL_ACK_POLICY_SHIFT;
166 /* qos header is 2 bytes, second reserved */
167 *p = qos_hdr;
168 p++;
169 *p = 0;
172 if (unlikely(queue >= local->hw.queues)) {
173 #if 0
174 if (net_ratelimit()) {
175 printk(KERN_DEBUG "%s - queue=%d (hw does not "
176 "support) -> %d\n",
177 __func__, queue, local->hw.queues - 1);
179 #endif
180 queue = local->hw.queues - 1;
183 if (unlikely(queue < 0)) {
184 kfree_skb(skb);
185 err = NET_XMIT_DROP;
186 } else {
187 pkt_data->queue = (unsigned int) queue;
188 qdisc = q->queues[queue];
189 err = qdisc->enqueue(skb, qdisc);
190 if (err == NET_XMIT_SUCCESS) {
191 qd->q.qlen++;
192 qd->bstats.bytes += skb->len;
193 qd->bstats.packets++;
194 return NET_XMIT_SUCCESS;
197 qd->qstats.drops++;
198 return err;
202 /* TODO: clean up the cases where master_hard_start_xmit
203 * returns non 0 - it shouldn't ever do that. Once done we
204 * can remove this function */
205 static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
207 struct ieee80211_sched_data *q = qdisc_priv(qd);
208 struct ieee80211_tx_packet_data *pkt_data =
209 (struct ieee80211_tx_packet_data *) skb->cb;
210 struct Qdisc *qdisc;
211 int err;
213 /* we recorded which queue to use earlier! */
214 qdisc = q->queues[pkt_data->queue];
216 if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
217 qd->q.qlen++;
218 return 0;
220 qd->qstats.drops++;
221 return err;
225 static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
227 struct ieee80211_sched_data *q = qdisc_priv(qd);
228 struct net_device *dev = qd->dev;
229 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
230 struct ieee80211_hw *hw = &local->hw;
231 struct sk_buff *skb;
232 struct Qdisc *qdisc;
233 int queue;
235 /* check all the h/w queues in numeric/priority order */
236 for (queue = 0; queue < hw->queues; queue++) {
237 /* see if there is room in this hardware queue */
238 if (test_bit(IEEE80211_LINK_STATE_XOFF,
239 &local->state[queue]) ||
240 test_bit(IEEE80211_LINK_STATE_PENDING,
241 &local->state[queue]))
242 continue;
244 /* there is space - try and get a frame */
245 skb = skb_dequeue(&q->requeued[queue]);
246 if (skb) {
247 qd->q.qlen--;
248 return skb;
251 qdisc = q->queues[queue];
252 skb = qdisc->dequeue(qdisc);
253 if (skb) {
254 qd->q.qlen--;
255 return skb;
258 /* returning a NULL here when all the h/w queues are full means we
259 * never need to call netif_stop_queue in the driver */
260 return NULL;
264 static void wme_qdiscop_reset(struct Qdisc* qd)
266 struct ieee80211_sched_data *q = qdisc_priv(qd);
267 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
268 struct ieee80211_hw *hw = &local->hw;
269 int queue;
271 /* QUESTION: should we have some hardware flush functionality here? */
273 for (queue = 0; queue < hw->queues; queue++) {
274 skb_queue_purge(&q->requeued[queue]);
275 qdisc_reset(q->queues[queue]);
277 qd->q.qlen = 0;
281 static void wme_qdiscop_destroy(struct Qdisc* qd)
283 struct ieee80211_sched_data *q = qdisc_priv(qd);
284 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
285 struct ieee80211_hw *hw = &local->hw;
286 int queue;
288 tcf_destroy_chain(q->filter_list);
289 q->filter_list = NULL;
291 for (queue=0; queue < hw->queues; queue++) {
292 skb_queue_purge(&q->requeued[queue]);
293 qdisc_destroy(q->queues[queue]);
294 q->queues[queue] = &noop_qdisc;
299 /* called whenever parameters are updated on existing qdisc */
300 static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
302 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
304 /* check our options block is the right size */
305 /* copy any options to our local structure */
306 /* Ignore options block for now - always use static mapping
307 struct tc_ieee80211_qopt *qopt = nla_data(opt);
309 if (opt->nla_len < nla_attr_size(sizeof(*qopt)))
310 return -EINVAL;
311 memcpy(q->tag2queue, qopt->tag2queue, sizeof(qopt->tag2queue));
313 return 0;
317 /* called during initial creation of qdisc on device */
318 static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
320 struct ieee80211_sched_data *q = qdisc_priv(qd);
321 struct net_device *dev = qd->dev;
322 struct ieee80211_local *local;
323 int queues;
324 int err = 0, i;
326 /* check that device is a mac80211 device */
327 if (!dev->ieee80211_ptr ||
328 dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
329 return -EINVAL;
331 /* check this device is an ieee80211 master type device */
332 if (dev->type != ARPHRD_IEEE80211)
333 return -EINVAL;
335 /* check that there is no qdisc currently attached to device
336 * this ensures that we will be the root qdisc. (I can't find a better
337 * way to test this explicitly) */
338 if (dev->qdisc_sleeping != &noop_qdisc)
339 return -EINVAL;
341 if (qd->flags & TCQ_F_INGRESS)
342 return -EINVAL;
344 local = wdev_priv(dev->ieee80211_ptr);
345 queues = local->hw.queues;
347 /* if options were passed in, set them */
348 if (opt) {
349 err = wme_qdiscop_tune(qd, opt);
352 /* create child queues */
353 for (i = 0; i < queues; i++) {
354 skb_queue_head_init(&q->requeued[i]);
355 q->queues[i] = qdisc_create_dflt(qd->dev, &pfifo_qdisc_ops,
356 qd->handle);
357 if (!q->queues[i]) {
358 q->queues[i] = &noop_qdisc;
359 printk(KERN_ERR "%s child qdisc %i creation failed", dev->name, i);
363 return err;
366 static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
368 /* struct ieee80211_sched_data *q = qdisc_priv(qd);
369 unsigned char *p = skb->tail;
370 struct tc_ieee80211_qopt opt;
372 memcpy(&opt.tag2queue, q->tag2queue, TC_80211_MAX_TAG + 1);
373 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
374 */ return skb->len;
376 nla_put_failure:
377 skb_trim(skb, p - skb->data);*/
378 return -1;
382 static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
383 struct Qdisc *new, struct Qdisc **old)
385 struct ieee80211_sched_data *q = qdisc_priv(qd);
386 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
387 struct ieee80211_hw *hw = &local->hw;
388 unsigned long queue = arg - 1;
390 if (queue >= hw->queues)
391 return -EINVAL;
393 if (!new)
394 new = &noop_qdisc;
396 sch_tree_lock(qd);
397 *old = q->queues[queue];
398 q->queues[queue] = new;
399 qdisc_reset(*old);
400 sch_tree_unlock(qd);
402 return 0;
406 static struct Qdisc *
407 wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
409 struct ieee80211_sched_data *q = qdisc_priv(qd);
410 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
411 struct ieee80211_hw *hw = &local->hw;
412 unsigned long queue = arg - 1;
414 if (queue >= hw->queues)
415 return NULL;
417 return q->queues[queue];
421 static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
423 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
424 struct ieee80211_hw *hw = &local->hw;
425 unsigned long queue = TC_H_MIN(classid);
427 if (queue - 1 >= hw->queues)
428 return 0;
430 return queue;
434 static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
435 u32 classid)
437 return wme_classop_get(qd, classid);
441 static void wme_classop_put(struct Qdisc *q, unsigned long cl)
446 static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
447 struct nlattr **tca, unsigned long *arg)
449 unsigned long cl = *arg;
450 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
451 struct ieee80211_hw *hw = &local->hw;
453 if (cl - 1 > hw->queues)
454 return -ENOENT;
456 /* TODO: put code to program hardware queue parameters here,
457 * to allow programming from tc command line */
459 return 0;
463 /* we don't support deleting hardware queues
464 * when we add WMM-SA support - TSPECs may be deleted here */
465 static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
467 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
468 struct ieee80211_hw *hw = &local->hw;
470 if (cl - 1 > hw->queues)
471 return -ENOENT;
472 return 0;
476 static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
477 struct sk_buff *skb, struct tcmsg *tcm)
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;
483 if (cl - 1 > hw->queues)
484 return -ENOENT;
485 tcm->tcm_handle = TC_H_MIN(cl);
486 tcm->tcm_parent = qd->handle;
487 tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
488 return 0;
492 static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
494 struct ieee80211_local *local = wdev_priv(qd->dev->ieee80211_ptr);
495 struct ieee80211_hw *hw = &local->hw;
496 int queue;
498 if (arg->stop)
499 return;
501 for (queue = 0; queue < hw->queues; queue++) {
502 if (arg->count < arg->skip) {
503 arg->count++;
504 continue;
506 /* we should return classids for our internal queues here
507 * as well as the external ones */
508 if (arg->fn(qd, queue+1, arg) < 0) {
509 arg->stop = 1;
510 break;
512 arg->count++;
517 static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
518 unsigned long cl)
520 struct ieee80211_sched_data *q = qdisc_priv(qd);
522 if (cl)
523 return NULL;
525 return &q->filter_list;
529 /* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
530 * - these are the operations on the classes */
531 static const struct Qdisc_class_ops class_ops =
533 .graft = wme_classop_graft,
534 .leaf = wme_classop_leaf,
536 .get = wme_classop_get,
537 .put = wme_classop_put,
538 .change = wme_classop_change,
539 .delete = wme_classop_delete,
540 .walk = wme_classop_walk,
542 .tcf_chain = wme_classop_find_tcf,
543 .bind_tcf = wme_classop_bind,
544 .unbind_tcf = wme_classop_put,
546 .dump = wme_classop_dump_class,
550 /* queueing discipline operations */
551 static struct Qdisc_ops wme_qdisc_ops __read_mostly =
553 .next = NULL,
554 .cl_ops = &class_ops,
555 .id = "ieee80211",
556 .priv_size = sizeof(struct ieee80211_sched_data),
558 .enqueue = wme_qdiscop_enqueue,
559 .dequeue = wme_qdiscop_dequeue,
560 .requeue = wme_qdiscop_requeue,
561 .drop = NULL, /* drop not needed since we are always the root qdisc */
563 .init = wme_qdiscop_init,
564 .reset = wme_qdiscop_reset,
565 .destroy = wme_qdiscop_destroy,
566 .change = wme_qdiscop_tune,
568 .dump = wme_qdiscop_dump,
572 void ieee80211_install_qdisc(struct net_device *dev)
574 struct Qdisc *qdisc;
576 qdisc = qdisc_create_dflt(dev, &wme_qdisc_ops, TC_H_ROOT);
577 if (!qdisc) {
578 printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
579 return;
582 /* same handle as would be allocated by qdisc_alloc_handle() */
583 qdisc->handle = 0x80010000;
585 qdisc_lock_tree(dev);
586 list_add_tail(&qdisc->list, &dev->qdisc_list);
587 dev->qdisc_sleeping = qdisc;
588 qdisc_unlock_tree(dev);
592 int ieee80211_qdisc_installed(struct net_device *dev)
594 return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
598 int ieee80211_wme_register(void)
600 return register_qdisc(&wme_qdisc_ops);
604 void ieee80211_wme_unregister(void)
606 unregister_qdisc(&wme_qdisc_ops);