wme.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2004, Instant802 Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/types.h>
#include <net/ip.h>
#include <net/pkt_sched.h>

#include <net/mac80211.h>
#include "ieee80211_i.h"
#include "wme.h"

/* maximum number of hardware queues we support. */
Johannes Berg's avatar
Johannes Berg committed
22 23 24
#define QD_MAX_QUEUES (IEEE80211_MAX_AMPDU_QUEUES + IEEE80211_MAX_QUEUES)
/* current number of hardware queues we support. */
#define QD_NUM(hw) ((hw)->queues + (hw)->ampdu_queues)
25

Johannes Berg's avatar
Johannes Berg committed
26 27 28 29
/*
 * Default mapping in classifier to work with default
 * queue setup.
 */
30
const int ieee802_1d_to_ac[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
31 32 33

struct ieee80211_sched_data
{
Johannes Berg's avatar
Johannes Berg committed
34
	unsigned long qdisc_pool[BITS_TO_LONGS(QD_MAX_QUEUES)];
35
	struct tcf_proto *filter_list;
Johannes Berg's avatar
Johannes Berg committed
36 37
	struct Qdisc *queues[QD_MAX_QUEUES];
	struct sk_buff_head requeued[QD_MAX_QUEUES];
38 39
};

40
static const char llc_ip_hdr[8] = {0xAA, 0xAA, 0x3, 0, 0, 0, 0x08, 0};
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

/* given a data frame determine the 802.1p/1d tag to use */
static inline unsigned classify_1d(struct sk_buff *skb, struct Qdisc *qd)
{
	struct iphdr *ip;
	int dscp;
	int offset;

	struct ieee80211_sched_data *q = qdisc_priv(qd);
	struct tcf_result res = { -1, 0 };

	/* if there is a user set filter list, call out to that */
	if (q->filter_list) {
		tc_classify(skb, q->filter_list, &res);
		if (res.class != -1)
			return res.class;
	}

	/* skb->priority values from 256->263 are magic values to
	 * directly indicate a specific 802.1d priority.
	 * This is used to allow 802.1d priority to be passed directly in
	 * from VLAN tags, etc. */
	if (skb->priority >= 256 && skb->priority <= 263)
		return skb->priority - 256;

	/* check there is a valid IP header present */
67 68 69
	offset = ieee80211_get_hdrlen_from_skb(skb);
	if (skb->len < offset + sizeof(llc_ip_hdr) + sizeof(*ip) ||
	    memcmp(skb->data + offset, llc_ip_hdr, sizeof(llc_ip_hdr)))
70 71
		return 0;

72
	ip = (struct iphdr *) (skb->data + offset + sizeof(llc_ip_hdr));
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	dscp = ip->tos & 0xfc;
	if (dscp & 0x1c)
		return 0;
	return dscp >> 5;
}


static inline int wme_downgrade_ac(struct sk_buff *skb)
{
	switch (skb->priority) {
	case 6:
	case 7:
		skb->priority = 5; /* VO -> VI */
		return 0;
	case 4:
	case 5:
		skb->priority = 3; /* VI -> BE */
		return 0;
	case 0:
	case 3:
		skb->priority = 2; /* BE -> BK */
		return 0;
	default:
		return -1;
	}
}


/* positive return value indicates which queue to use
 * negative return value indicates to drop the frame */
Johannes Berg's avatar
Johannes Berg committed
104
static int classify80211(struct sk_buff *skb, struct Qdisc *qd)
105
{
106
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
107 108
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;

109
	if (!ieee80211_is_data(hdr->frame_control)) {
110 111
		/* management frames go on AC_VO queue, but are sent
		* without QoS control fields */
Johannes Berg's avatar
Johannes Berg committed
112
		return 0;
113 114
	}

115 116
	if (0 /* injected */) {
		/* use AC from radiotap */
117 118
	}

119
	if (!ieee80211_is_data_qos(hdr->frame_control)) {
120 121 122 123 124
		skb->priority = 0; /* required for correct WPA/11i MIC */
		return ieee802_1d_to_ac[skb->priority];
	}

	/* use the data classifier to determine what 802.1d tag the
125
	 * data frame has */
126 127
	skb->priority = classify_1d(skb, qd);

128
	/* in case we are a client verify acm is not set for this ac */
129 130
	while (unlikely(local->wmm_acm & BIT(skb->priority))) {
		if (wme_downgrade_ac(skb)) {
131
			/* No AC with lower priority has acm=0, drop packet. */
132 133 134 135 136 137 138 139 140 141 142
			return -1;
		}
	}

	/* look up which queue to use for frames with this 1d tag */
	return ieee802_1d_to_ac[skb->priority];
}


static int wme_qdiscop_enqueue(struct sk_buff *skb, struct Qdisc* qd)
{
143
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
Johannes Berg's avatar
Johannes Berg committed
144
	struct ieee80211_hw *hw = &local->hw;
145
	struct ieee80211_sched_data *q = qdisc_priv(qd);
146
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
147 148
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
	struct Qdisc *qdisc;
149
	struct sta_info *sta;
Johannes Berg's avatar
Johannes Berg committed
150
	int err, queue;
151
	u8 tid;
152

153
	if (info->flags & IEEE80211_TX_CTL_REQUEUE) {
154
		queue = skb_get_queue_mapping(skb);
155
		rcu_read_lock();
156
		sta = sta_info_get(local, hdr->addr1);
157
		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
158 159
		if (sta) {
			int ampdu_queue = sta->tid_to_tx_q[tid];
Johannes Berg's avatar
Johannes Berg committed
160
			if ((ampdu_queue < QD_NUM(hw)) &&
161
			    test_bit(ampdu_queue, q->qdisc_pool)) {
162
				queue = ampdu_queue;
163
				info->flags |= IEEE80211_TX_CTL_AMPDU;
164
			} else {
165
				info->flags &= ~IEEE80211_TX_CTL_AMPDU;
166 167
			}
		}
168
		rcu_read_unlock();
169
		skb_queue_tail(&q->requeued[queue], skb);
170 171 172 173 174 175
		qd->q.qlen++;
		return 0;
	}

	queue = classify80211(skb, qd);

Johannes Berg's avatar
Johannes Berg committed
176 177 178
	if (unlikely(queue >= local->hw.queues))
		queue = local->hw.queues - 1;

179 180
	/* now we know the 1d priority, fill in the QoS header if there is one
	 */
181 182
	if (ieee80211_is_data_qos(hdr->frame_control)) {
		u8 *p = ieee80211_get_qos_ctl(hdr);
183
		u8 ack_policy = 0;
184
		tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
185
		if (local->wifi_wme_noack_test)
186
			ack_policy |= QOS_CONTROL_ACK_POLICY_NOACK <<
187 188
					QOS_CONTROL_ACK_POLICY_SHIFT;
		/* qos header is 2 bytes, second reserved */
189
		*p++ = ack_policy | tid;
190
		*p = 0;
191

192 193
		rcu_read_lock();

194 195 196
		sta = sta_info_get(local, hdr->addr1);
		if (sta) {
			int ampdu_queue = sta->tid_to_tx_q[tid];
Johannes Berg's avatar
Johannes Berg committed
197 198
			if ((ampdu_queue < QD_NUM(hw)) &&
			    test_bit(ampdu_queue, q->qdisc_pool)) {
199
				queue = ampdu_queue;
200
				info->flags |= IEEE80211_TX_CTL_AMPDU;
201
			} else {
202
				info->flags &= ~IEEE80211_TX_CTL_AMPDU;
203 204
			}
		}
205 206

		rcu_read_unlock();
207 208
	}

Johannes Berg's avatar
Johannes Berg committed
209 210 211 212
	if (unlikely(queue < 0)) {
			kfree_skb(skb);
			err = NET_XMIT_DROP;
	} else {
213
		skb_set_queue_mapping(skb, queue);
Johannes Berg's avatar
Johannes Berg committed
214 215 216 217 218 219 220 221
		qdisc = q->queues[queue];
		err = qdisc->enqueue(skb, qdisc);
		if (err == NET_XMIT_SUCCESS) {
			qd->q.qlen++;
			qd->bstats.bytes += skb->len;
			qd->bstats.packets++;
			return NET_XMIT_SUCCESS;
		}
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	}
	qd->qstats.drops++;
	return err;
}


/* TODO: clean up the cases where master_hard_start_xmit
 * returns non 0 - it shouldn't ever do that. Once done we
 * can remove this function */
static int wme_qdiscop_requeue(struct sk_buff *skb, struct Qdisc* qd)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
	struct Qdisc *qdisc;
	int err;

	/* we recorded which queue to use earlier! */
238
	qdisc = q->queues[skb_get_queue_mapping(skb)];
239 240 241 242 243 244 245 246 247 248 249 250 251

	if ((err = qdisc->ops->requeue(skb, qdisc)) == 0) {
		qd->q.qlen++;
		return 0;
	}
	qd->qstats.drops++;
	return err;
}


static struct sk_buff *wme_qdiscop_dequeue(struct Qdisc* qd)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
252
	struct net_device *dev = qdisc_dev(qd);
253 254 255 256 257 258 259
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_hw *hw = &local->hw;
	struct sk_buff *skb;
	struct Qdisc *qdisc;
	int queue;

	/* check all the h/w queues in numeric/priority order */
Johannes Berg's avatar
Johannes Berg committed
260
	for (queue = 0; queue < QD_NUM(hw); queue++) {
261
		/* see if there is room in this hardware queue */
262 263
		if (__netif_subqueue_stopped(local->mdev, queue) ||
		    !test_bit(queue, q->qdisc_pool))
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
			continue;

		/* there is space - try and get a frame */
		skb = skb_dequeue(&q->requeued[queue]);
		if (skb) {
			qd->q.qlen--;
			return skb;
		}

		qdisc = q->queues[queue];
		skb = qdisc->dequeue(qdisc);
		if (skb) {
			qd->q.qlen--;
			return skb;
		}
	}
	/* returning a NULL here when all the h/w queues are full means we
	 * never need to call netif_stop_queue in the driver */
	return NULL;
}


static void wme_qdiscop_reset(struct Qdisc* qd)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
289
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
290 291 292 293 294
	struct ieee80211_hw *hw = &local->hw;
	int queue;

	/* QUESTION: should we have some hardware flush functionality here? */

Johannes Berg's avatar
Johannes Berg committed
295
	for (queue = 0; queue < QD_NUM(hw); queue++) {
296 297 298 299 300 301 302 303 304 305
		skb_queue_purge(&q->requeued[queue]);
		qdisc_reset(q->queues[queue]);
	}
	qd->q.qlen = 0;
}


static void wme_qdiscop_destroy(struct Qdisc* qd)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
306
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
307 308 309
	struct ieee80211_hw *hw = &local->hw;
	int queue;

310
	tcf_destroy_chain(&q->filter_list);
311

Johannes Berg's avatar
Johannes Berg committed
312
	for (queue = 0; queue < QD_NUM(hw); queue++) {
313 314 315 316 317 318 319 320
		skb_queue_purge(&q->requeued[queue]);
		qdisc_destroy(q->queues[queue]);
		q->queues[queue] = &noop_qdisc;
	}
}


/* called whenever parameters are updated on existing qdisc */
321
static int wme_qdiscop_tune(struct Qdisc *qd, struct nlattr *opt)
322 323 324 325 326 327
{
	return 0;
}


/* called during initial creation of qdisc on device */
328
static int wme_qdiscop_init(struct Qdisc *qd, struct nlattr *opt)
329 330
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
331
	struct net_device *dev = qdisc_dev(qd);
332
	struct ieee80211_local *local;
Johannes Berg's avatar
Johannes Berg committed
333
	struct ieee80211_hw *hw;
334 335 336 337 338 339 340
	int err = 0, i;

	/* check that device is a mac80211 device */
	if (!dev->ieee80211_ptr ||
	    dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
		return -EINVAL;

Johannes Berg's avatar
Johannes Berg committed
341 342 343 344 345
	local = wdev_priv(dev->ieee80211_ptr);
	hw = &local->hw;

	/* only allow on master dev */
	if (dev != local->mdev)
346 347
		return -EINVAL;

Johannes Berg's avatar
Johannes Berg committed
348 349
	/* ensure that we are root qdisc */
	if (qd->parent != TC_H_ROOT)
350 351 352 353 354 355
		return -EINVAL;

	if (qd->flags & TCQ_F_INGRESS)
		return -EINVAL;

	/* if options were passed in, set them */
Johannes Berg's avatar
Johannes Berg committed
356
	if (opt)
357 358 359
		err = wme_qdiscop_tune(qd, opt);

	/* create child queues */
Johannes Berg's avatar
Johannes Berg committed
360
	for (i = 0; i < QD_NUM(hw); i++) {
361
		skb_queue_head_init(&q->requeued[i]);
362
		q->queues[i] = qdisc_create_dflt(qdisc_dev(qd), qd->dev_queue,
363
						 &pfifo_qdisc_ops,
364
						 qd->handle);
365
		if (!q->queues[i]) {
366
			q->queues[i] = &noop_qdisc;
367 368
			printk(KERN_ERR "%s child qdisc %i creation failed\n",
			       dev->name, i);
369 370 371
		}
	}

Johannes Berg's avatar
Johannes Berg committed
372 373
	/* non-aggregation queues: reserve/mark as used */
	for (i = 0; i < local->hw.queues; i++)
374
		set_bit(i, q->qdisc_pool);
375

376 377 378 379 380 381 382 383 384 385 386 387 388
	return err;
}

static int wme_qdiscop_dump(struct Qdisc *qd, struct sk_buff *skb)
{
	return -1;
}


static int wme_classop_graft(struct Qdisc *qd, unsigned long arg,
			     struct Qdisc *new, struct Qdisc **old)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
389
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
390 391 392
	struct ieee80211_hw *hw = &local->hw;
	unsigned long queue = arg - 1;

Johannes Berg's avatar
Johannes Berg committed
393
	if (queue >= QD_NUM(hw))
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		return -EINVAL;

	if (!new)
		new = &noop_qdisc;

	sch_tree_lock(qd);
	*old = q->queues[queue];
	q->queues[queue] = new;
	qdisc_reset(*old);
	sch_tree_unlock(qd);

	return 0;
}


static struct Qdisc *
wme_classop_leaf(struct Qdisc *qd, unsigned long arg)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
413
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
414 415 416
	struct ieee80211_hw *hw = &local->hw;
	unsigned long queue = arg - 1;

Johannes Berg's avatar
Johannes Berg committed
417
	if (queue >= QD_NUM(hw))
418 419 420 421 422 423 424 425
		return NULL;

	return q->queues[queue];
}


static unsigned long wme_classop_get(struct Qdisc *qd, u32 classid)
{
426
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
427 428 429
	struct ieee80211_hw *hw = &local->hw;
	unsigned long queue = TC_H_MIN(classid);

Johannes Berg's avatar
Johannes Berg committed
430
	if (queue - 1 >= QD_NUM(hw))
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		return 0;

	return queue;
}


static unsigned long wme_classop_bind(struct Qdisc *qd, unsigned long parent,
				      u32 classid)
{
	return wme_classop_get(qd, classid);
}


static void wme_classop_put(struct Qdisc *q, unsigned long cl)
{
}


static int wme_classop_change(struct Qdisc *qd, u32 handle, u32 parent,
450
			      struct nlattr **tca, unsigned long *arg)
451 452
{
	unsigned long cl = *arg;
453
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
454 455
	struct ieee80211_hw *hw = &local->hw;

Johannes Berg's avatar
Johannes Berg committed
456
	if (cl - 1 > QD_NUM(hw))
457 458 459 460 461 462 463 464 465 466 467 468 469
		return -ENOENT;

	/* TODO: put code to program hardware queue parameters here,
	 * to allow programming from tc command line */

	return 0;
}


/* we don't support deleting hardware queues
 * when we add WMM-SA support - TSPECs may be deleted here */
static int wme_classop_delete(struct Qdisc *qd, unsigned long cl)
{
470
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
471 472
	struct ieee80211_hw *hw = &local->hw;

Johannes Berg's avatar
Johannes Berg committed
473
	if (cl - 1 > QD_NUM(hw))
474 475 476 477 478 479 480 481 482
		return -ENOENT;
	return 0;
}


static int wme_classop_dump_class(struct Qdisc *qd, unsigned long cl,
				  struct sk_buff *skb, struct tcmsg *tcm)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);
483
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
484 485
	struct ieee80211_hw *hw = &local->hw;

Johannes Berg's avatar
Johannes Berg committed
486
	if (cl - 1 > QD_NUM(hw))
487 488 489 490 491 492 493 494 495 496
		return -ENOENT;
	tcm->tcm_handle = TC_H_MIN(cl);
	tcm->tcm_parent = qd->handle;
	tcm->tcm_info = q->queues[cl-1]->handle; /* do we need this? */
	return 0;
}


static void wme_classop_walk(struct Qdisc *qd, struct qdisc_walker *arg)
{
497
	struct ieee80211_local *local = wdev_priv(qdisc_dev(qd)->ieee80211_ptr);
498 499 500 501 502 503
	struct ieee80211_hw *hw = &local->hw;
	int queue;

	if (arg->stop)
		return;

Johannes Berg's avatar
Johannes Berg committed
504
	for (queue = 0; queue < QD_NUM(hw); queue++) {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		if (arg->count < arg->skip) {
			arg->count++;
			continue;
		}
		/* we should return classids for our internal queues here
		 * as well as the external ones */
		if (arg->fn(qd, queue+1, arg) < 0) {
			arg->stop = 1;
			break;
		}
		arg->count++;
	}
}


static struct tcf_proto ** wme_classop_find_tcf(struct Qdisc *qd,
						unsigned long cl)
{
	struct ieee80211_sched_data *q = qdisc_priv(qd);

	if (cl)
		return NULL;

	return &q->filter_list;
}


/* this qdisc is classful (i.e. has classes, some of which may have leaf qdiscs attached)
 * - these are the operations on the classes */
534
static const struct Qdisc_class_ops class_ops =
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
{
	.graft = wme_classop_graft,
	.leaf = wme_classop_leaf,

	.get = wme_classop_get,
	.put = wme_classop_put,
	.change = wme_classop_change,
	.delete = wme_classop_delete,
	.walk = wme_classop_walk,

	.tcf_chain = wme_classop_find_tcf,
	.bind_tcf = wme_classop_bind,
	.unbind_tcf = wme_classop_put,

	.dump = wme_classop_dump_class,
};


/* queueing discipline operations */
554
static struct Qdisc_ops wme_qdisc_ops __read_mostly =
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
{
	.next = NULL,
	.cl_ops = &class_ops,
	.id = "ieee80211",
	.priv_size = sizeof(struct ieee80211_sched_data),

	.enqueue = wme_qdiscop_enqueue,
	.dequeue = wme_qdiscop_dequeue,
	.requeue = wme_qdiscop_requeue,
	.drop = NULL, /* drop not needed since we are always the root qdisc */

	.init = wme_qdiscop_init,
	.reset = wme_qdiscop_reset,
	.destroy = wme_qdiscop_destroy,
	.change = wme_qdiscop_tune,

	.dump = wme_qdiscop_dump,
};


void ieee80211_install_qdisc(struct net_device *dev)
{
	struct Qdisc *qdisc;

579 580
	qdisc = qdisc_create_dflt(dev, &dev->tx_queue,
				  &wme_qdisc_ops, TC_H_ROOT);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	if (!qdisc) {
		printk(KERN_ERR "%s: qdisc installation failed\n", dev->name);
		return;
	}

	/* same handle as would be allocated by qdisc_alloc_handle() */
	qdisc->handle = 0x80010000;

	qdisc_lock_tree(dev);
	list_add_tail(&qdisc->list, &dev->qdisc_list);
	dev->qdisc_sleeping = qdisc;
	qdisc_unlock_tree(dev);
}


int ieee80211_qdisc_installed(struct net_device *dev)
{
	return dev->qdisc_sleeping->ops == &wme_qdisc_ops;
}


int ieee80211_wme_register(void)
{
	return register_qdisc(&wme_qdisc_ops);
}


void ieee80211_wme_unregister(void)
{
	unregister_qdisc(&wme_qdisc_ops);
}
612 613 614 615 616 617 618 619 620 621

int ieee80211_ht_agg_queue_add(struct ieee80211_local *local,
			struct sta_info *sta, u16 tid)
{
	int i;
	struct ieee80211_sched_data *q =
			qdisc_priv(local->mdev->qdisc_sleeping);
	DECLARE_MAC_BUF(mac);

	/* prepare the filter and save it for the SW queue
Johannes Berg's avatar
Johannes Berg committed
622 623 624 625
	 * matching the received HW queue */

	if (!local->hw.ampdu_queues)
		return -EPERM;
626 627

	/* try to get a Qdisc from the pool */
Johannes Berg's avatar
Johannes Berg committed
628
	for (i = local->hw.queues; i < QD_NUM(&local->hw); i++)
629
		if (!test_and_set_bit(i, q->qdisc_pool)) {
630 631 632 633 634 635 636 637 638 639
			ieee80211_stop_queue(local_to_hw(local), i);
			sta->tid_to_tx_q[tid] = i;

			/* IF there are already pending packets
			 * on this tid first we need to drain them
			 * on the previous queue
			 * since HT is strict in order */
#ifdef CONFIG_MAC80211_HT_DEBUG
			if (net_ratelimit())
				printk(KERN_DEBUG "allocated aggregation queue"
640
					" %d tid %d addr %s pool=0x%lX\n",
641
					i, tid, print_mac(mac, sta->addr),
642
					q->qdisc_pool[0]);
643 644 645 646 647 648 649 650
#endif /* CONFIG_MAC80211_HT_DEBUG */
			return 0;
		}

	return -EAGAIN;
}

/**
651
 * the caller needs to hold local->mdev->tx_queue.lock
652 653 654 655 656
 */
void ieee80211_ht_agg_queue_remove(struct ieee80211_local *local,
				   struct sta_info *sta, u16 tid,
				   u8 requeue)
{
Johannes Berg's avatar
Johannes Berg committed
657
	struct ieee80211_hw *hw = &local->hw;
658 659 660 661 662
	struct ieee80211_sched_data *q =
		qdisc_priv(local->mdev->qdisc_sleeping);
	int agg_queue = sta->tid_to_tx_q[tid];

	/* return the qdisc to the pool */
663
	clear_bit(agg_queue, q->qdisc_pool);
Johannes Berg's avatar
Johannes Berg committed
664
	sta->tid_to_tx_q[tid] = QD_NUM(hw);
665 666 667 668 669 670 671 672 673 674 675 676 677

	if (requeue)
		ieee80211_requeue(local, agg_queue);
	else
		q->queues[agg_queue]->ops->reset(q->queues[agg_queue]);
}

void ieee80211_requeue(struct ieee80211_local *local, int queue)
{
	struct Qdisc *root_qd = local->mdev->qdisc_sleeping;
	struct ieee80211_sched_data *q = qdisc_priv(root_qd);
	struct Qdisc *qdisc = q->queues[queue];
	struct sk_buff *skb = NULL;
678
	u32 len;
679 680 681 682 683 684 685 686 687 688 689 690 691

	if (!qdisc || !qdisc->dequeue)
		return;

	for (len = qdisc->q.qlen; len > 0; len--) {
		skb = qdisc->dequeue(qdisc);
		root_qd->q.qlen--;
		/* packet will be classified again and */
		/* skb->packet_data->queue will be overridden if needed */
		if (skb)
			wme_qdiscop_enqueue(skb, root_qd);
	}
}