blob: b27969c418126b6c9bd2a37e6a98ed473fb3f448 [file] [log] [blame]
Christian Lampartera84fab32010-09-06 01:09:20 +02001/*
2 * Atheros CARL9170 driver
3 *
4 * 802.11 xmit & status routines
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
22 *
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
26 *
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 */
39
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/module.h>
43#include <linux/etherdevice.h>
44#include <net/mac80211.h>
45#include "carl9170.h"
46#include "hw.h"
47#include "cmd.h"
48
49static inline unsigned int __carl9170_get_queue(struct ar9170 *ar,
50 unsigned int queue)
51{
52 if (unlikely(modparam_noht)) {
53 return queue;
54 } else {
55 /*
56 * This is just another workaround, until
57 * someone figures out how to get QoS and
58 * AMPDU to play nicely together.
59 */
60
61 return 2; /* AC_BE */
62 }
63}
64
65static inline unsigned int carl9170_get_queue(struct ar9170 *ar,
66 struct sk_buff *skb)
67{
68 return __carl9170_get_queue(ar, skb_get_queue_mapping(skb));
69}
70
71static bool is_mem_full(struct ar9170 *ar)
72{
73 return (DIV_ROUND_UP(IEEE80211_MAX_FRAME_LEN, ar->fw.mem_block_size) >
74 atomic_read(&ar->mem_free_blocks));
75}
76
77static void carl9170_tx_accounting(struct ar9170 *ar, struct sk_buff *skb)
78{
79 int queue, i;
80 bool mem_full;
81
82 atomic_inc(&ar->tx_total_queued);
83
84 queue = skb_get_queue_mapping(skb);
85 spin_lock_bh(&ar->tx_stats_lock);
86
87 /*
88 * The driver has to accept the frame, regardless if the queue is
89 * full to the brim, or not. We have to do the queuing internally,
90 * since mac80211 assumes that a driver which can operate with
91 * aggregated frames does not reject frames for this reason.
92 */
93 ar->tx_stats[queue].len++;
94 ar->tx_stats[queue].count++;
95
96 mem_full = is_mem_full(ar);
97 for (i = 0; i < ar->hw->queues; i++) {
98 if (mem_full || ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
99 ieee80211_stop_queue(ar->hw, i);
100 ar->queue_stop_timeout[i] = jiffies;
101 }
102 }
103
104 spin_unlock_bh(&ar->tx_stats_lock);
105}
106
107static void carl9170_tx_accounting_free(struct ar9170 *ar, struct sk_buff *skb)
108{
109 struct ieee80211_tx_info *txinfo;
110 int queue;
111
112 txinfo = IEEE80211_SKB_CB(skb);
113 queue = skb_get_queue_mapping(skb);
114
115 spin_lock_bh(&ar->tx_stats_lock);
116
117 ar->tx_stats[queue].len--;
118
119 if (!is_mem_full(ar)) {
120 unsigned int i;
121 for (i = 0; i < ar->hw->queues; i++) {
122 if (ar->tx_stats[i].len >= CARL9170_NUM_TX_LIMIT_SOFT)
123 continue;
124
125 if (ieee80211_queue_stopped(ar->hw, i)) {
126 unsigned long tmp;
127
128 tmp = jiffies - ar->queue_stop_timeout[i];
129 if (tmp > ar->max_queue_stop_timeout[i])
130 ar->max_queue_stop_timeout[i] = tmp;
131 }
132
133 ieee80211_wake_queue(ar->hw, i);
134 }
135 }
136
137 spin_unlock_bh(&ar->tx_stats_lock);
138 if (atomic_dec_and_test(&ar->tx_total_queued))
139 complete(&ar->tx_flush);
140}
141
142static int carl9170_alloc_dev_space(struct ar9170 *ar, struct sk_buff *skb)
143{
144 struct _carl9170_tx_superframe *super = (void *) skb->data;
145 unsigned int chunks;
146 int cookie = -1;
147
148 atomic_inc(&ar->mem_allocs);
149
150 chunks = DIV_ROUND_UP(skb->len, ar->fw.mem_block_size);
151 if (unlikely(atomic_sub_return(chunks, &ar->mem_free_blocks) < 0)) {
152 atomic_add(chunks, &ar->mem_free_blocks);
153 return -ENOSPC;
154 }
155
156 spin_lock_bh(&ar->mem_lock);
157 cookie = bitmap_find_free_region(ar->mem_bitmap, ar->fw.mem_blocks, 0);
158 spin_unlock_bh(&ar->mem_lock);
159
160 if (unlikely(cookie < 0)) {
161 atomic_add(chunks, &ar->mem_free_blocks);
162 return -ENOSPC;
163 }
164
165 super = (void *) skb->data;
166
167 /*
168 * Cookie #0 serves two special purposes:
169 * 1. The firmware might use it generate BlockACK frames
170 * in responds of an incoming BlockAckReqs.
171 *
172 * 2. Prevent double-free bugs.
173 */
174 super->s.cookie = (u8) cookie + 1;
175 return 0;
176}
177
178static void carl9170_release_dev_space(struct ar9170 *ar, struct sk_buff *skb)
179{
180 struct _carl9170_tx_superframe *super = (void *) skb->data;
181 int cookie;
182
183 /* make a local copy of the cookie */
184 cookie = super->s.cookie;
185 /* invalidate cookie */
186 super->s.cookie = 0;
187
188 /*
189 * Do a out-of-bounds check on the cookie:
190 *
191 * * cookie "0" is reserved and won't be assigned to any
192 * out-going frame. Internally however, it is used to
193 * mark no longer/un-accounted frames and serves as a
194 * cheap way of preventing frames from being freed
195 * twice by _accident_. NB: There is a tiny race...
196 *
197 * * obviously, cookie number is limited by the amount
198 * of available memory blocks, so the number can
199 * never execeed the mem_blocks count.
200 */
201 if (unlikely(WARN_ON_ONCE(cookie == 0) ||
202 WARN_ON_ONCE(cookie > ar->fw.mem_blocks)))
203 return;
204
205 atomic_add(DIV_ROUND_UP(skb->len, ar->fw.mem_block_size),
206 &ar->mem_free_blocks);
207
208 spin_lock_bh(&ar->mem_lock);
209 bitmap_release_region(ar->mem_bitmap, cookie - 1, 0);
210 spin_unlock_bh(&ar->mem_lock);
211}
212
213/* Called from any context */
214static void carl9170_tx_release(struct kref *ref)
215{
216 struct ar9170 *ar;
217 struct carl9170_tx_info *arinfo;
218 struct ieee80211_tx_info *txinfo;
219 struct sk_buff *skb;
220
221 arinfo = container_of(ref, struct carl9170_tx_info, ref);
222 txinfo = container_of((void *) arinfo, struct ieee80211_tx_info,
223 rate_driver_data);
224 skb = container_of((void *) txinfo, struct sk_buff, cb);
225
226 ar = arinfo->ar;
227 if (WARN_ON_ONCE(!ar))
228 return;
229
230 BUILD_BUG_ON(
231 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len) != 23);
232
233 memset(&txinfo->status.ampdu_ack_len, 0,
234 sizeof(struct ieee80211_tx_info) -
235 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
236
237 if (atomic_read(&ar->tx_total_queued))
238 ar->tx_schedule = true;
239
240 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
241 if (!atomic_read(&ar->tx_ampdu_upload))
242 ar->tx_ampdu_schedule = true;
243
244 if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
245 txinfo->status.ampdu_len = txinfo->pad[0];
246 txinfo->status.ampdu_ack_len = txinfo->pad[1];
247 txinfo->pad[0] = txinfo->pad[1] = 0;
248 } else if (txinfo->flags & IEEE80211_TX_STAT_ACK) {
249 /*
250 * drop redundant tx_status reports:
251 *
252 * 1. ampdu_ack_len of the final tx_status does
253 * include the feedback of this particular frame.
254 *
255 * 2. tx_status_irqsafe only queues up to 128
256 * tx feedback reports and discards the rest.
257 *
258 * 3. minstrel_ht is picky, it only accepts
259 * reports of frames with the TX_STATUS_AMPDU flag.
260 */
261
262 dev_kfree_skb_any(skb);
263 return;
264 } else {
265 /*
266 * Frame has failed, but we want to keep it in
267 * case it was lost due to a power-state
268 * transition.
269 */
270 }
271 }
272
273 skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
274 ieee80211_tx_status_irqsafe(ar->hw, skb);
275}
276
277void carl9170_tx_get_skb(struct sk_buff *skb)
278{
279 struct carl9170_tx_info *arinfo = (void *)
280 (IEEE80211_SKB_CB(skb))->rate_driver_data;
281 kref_get(&arinfo->ref);
282}
283
284int carl9170_tx_put_skb(struct sk_buff *skb)
285{
286 struct carl9170_tx_info *arinfo = (void *)
287 (IEEE80211_SKB_CB(skb))->rate_driver_data;
288
289 return kref_put(&arinfo->ref, carl9170_tx_release);
290}
291
292/* Caller must hold the tid_info->lock & rcu_read_lock */
293static void carl9170_tx_shift_bm(struct ar9170 *ar,
294 struct carl9170_sta_tid *tid_info, u16 seq)
295{
296 u16 off;
297
298 off = SEQ_DIFF(seq, tid_info->bsn);
299
300 if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
301 return;
302
303 /*
304 * Sanity check. For each MPDU we set the bit in bitmap and
305 * clear it once we received the tx_status.
306 * But if the bit is already cleared then we've been bitten
307 * by a bug.
308 */
309 WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
310
311 off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
312 if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
313 return;
314
315 if (!bitmap_empty(tid_info->bitmap, off))
316 off = find_first_bit(tid_info->bitmap, off);
317
318 tid_info->bsn += off;
319 tid_info->bsn &= 0x0fff;
320
321 bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
322 off, CARL9170_BAW_BITS);
323}
324
325static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
326 struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
327{
328 struct _carl9170_tx_superframe *super = (void *) skb->data;
329 struct ieee80211_hdr *hdr = (void *) super->frame_data;
330 struct ieee80211_tx_info *tx_info;
331 struct carl9170_tx_info *ar_info;
332 struct carl9170_sta_info *sta_info;
333 struct ieee80211_sta *sta;
334 struct carl9170_sta_tid *tid_info;
335 struct ieee80211_vif *vif;
336 unsigned int vif_id;
337 u8 tid;
338
339 if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
340 txinfo->flags & IEEE80211_TX_CTL_INJECTED)
341 return;
342
343 tx_info = IEEE80211_SKB_CB(skb);
344 ar_info = (void *) tx_info->rate_driver_data;
345
346 vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
347 CARL9170_TX_SUPER_MISC_VIF_ID_S;
348
349 if (WARN_ON_ONCE(vif_id >= AR9170_MAX_VIRTUAL_MAC))
350 return;
351
352 rcu_read_lock();
353 vif = rcu_dereference(ar->vif_priv[vif_id].vif);
354 if (unlikely(!vif))
355 goto out_rcu;
356
357 /*
358 * Normally we should use wrappers like ieee80211_get_DA to get
359 * the correct peer ieee80211_sta.
360 *
361 * But there is a problem with indirect traffic (broadcasts, or
362 * data which is designated for other stations) in station mode.
363 * The frame will be directed to the AP for distribution and not
364 * to the actual destination.
365 */
366 sta = ieee80211_find_sta(vif, hdr->addr1);
367 if (unlikely(!sta))
368 goto out_rcu;
369
370 tid = get_tid_h(hdr);
371
372 sta_info = (void *) sta->drv_priv;
373 tid_info = rcu_dereference(sta_info->agg[tid]);
374 if (!tid_info)
375 goto out_rcu;
376
377 spin_lock_bh(&tid_info->lock);
378 if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
379 carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
380
381 if (sta_info->stats[tid].clear) {
382 sta_info->stats[tid].clear = false;
383 sta_info->stats[tid].ampdu_len = 0;
384 sta_info->stats[tid].ampdu_ack_len = 0;
385 }
386
387 sta_info->stats[tid].ampdu_len++;
388 if (txinfo->status.rates[0].count == 1)
389 sta_info->stats[tid].ampdu_ack_len++;
390
391 if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
392 txinfo->pad[0] = sta_info->stats[tid].ampdu_len;
393 txinfo->pad[1] = sta_info->stats[tid].ampdu_ack_len;
394 txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
395 sta_info->stats[tid].clear = true;
396 }
397 spin_unlock_bh(&tid_info->lock);
398
399out_rcu:
400 rcu_read_unlock();
401}
402
403void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
404 const bool success)
405{
406 struct ieee80211_tx_info *txinfo;
407
408 carl9170_tx_accounting_free(ar, skb);
409
410 txinfo = IEEE80211_SKB_CB(skb);
411
412 if (success)
413 txinfo->flags |= IEEE80211_TX_STAT_ACK;
414 else
415 ar->tx_ack_failures++;
416
417 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
418 carl9170_tx_status_process_ampdu(ar, skb, txinfo);
419
420 carl9170_tx_put_skb(skb);
421}
422
423/* This function may be called form any context */
424void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
425{
426 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
427
428 atomic_dec(&ar->tx_total_pending);
429
430 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
431 atomic_dec(&ar->tx_ampdu_upload);
432
433 if (carl9170_tx_put_skb(skb))
434 tasklet_hi_schedule(&ar->usb_tasklet);
435}
436
437static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
438 struct sk_buff_head *queue)
439{
440 struct sk_buff *skb;
441
442 spin_lock_bh(&queue->lock);
443 skb_queue_walk(queue, skb) {
444 struct _carl9170_tx_superframe *txc = (void *) skb->data;
445
446 if (txc->s.cookie != cookie)
447 continue;
448
449 __skb_unlink(skb, queue);
450 spin_unlock_bh(&queue->lock);
451
452 carl9170_release_dev_space(ar, skb);
453 return skb;
454 }
455 spin_unlock_bh(&queue->lock);
456
457 return NULL;
458}
459
460static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
461 unsigned int tries, struct ieee80211_tx_info *txinfo)
462{
463 unsigned int i;
464
465 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
466 if (txinfo->status.rates[i].idx < 0)
467 break;
468
469 if (i == rix) {
470 txinfo->status.rates[i].count = tries;
471 i++;
472 break;
473 }
474 }
475
476 for (; i < IEEE80211_TX_MAX_RATES; i++) {
477 txinfo->status.rates[i].idx = -1;
478 txinfo->status.rates[i].count = 0;
479 }
480}
481
482static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
483{
484 int i;
485 struct sk_buff *skb;
486 struct ieee80211_tx_info *txinfo;
487 struct carl9170_tx_info *arinfo;
488 bool restart = false;
489
490 for (i = 0; i < ar->hw->queues; i++) {
491 spin_lock_bh(&ar->tx_status[i].lock);
492
493 skb = skb_peek(&ar->tx_status[i]);
494
495 if (!skb)
496 goto next;
497
498 txinfo = IEEE80211_SKB_CB(skb);
499 arinfo = (void *) txinfo->rate_driver_data;
500
501 if (time_is_before_jiffies(arinfo->timeout +
502 msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
503 restart = true;
504
505next:
506 spin_unlock_bh(&ar->tx_status[i].lock);
507 }
508
509 if (restart) {
510 /*
511 * At least one queue has been stuck for long enough.
512 * Give the device a kick and hope it gets back to
513 * work.
514 *
515 * possible reasons may include:
516 * - frames got lost/corrupted (bad connection to the device)
517 * - stalled rx processing/usb controller hiccups
518 * - firmware errors/bugs
519 * - every bug you can think of.
520 * - all bugs you can't...
521 * - ...
522 */
523 carl9170_restart(ar, CARL9170_RR_STUCK_TX);
524 }
525}
526
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200527static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
528{
529 struct carl9170_sta_tid *iter;
530 struct sk_buff *skb;
531 struct ieee80211_tx_info *txinfo;
532 struct carl9170_tx_info *arinfo;
533 struct _carl9170_tx_superframe *super;
534 struct ieee80211_sta *sta;
535 struct ieee80211_vif *vif;
536 struct ieee80211_hdr *hdr;
537 unsigned int vif_id;
538
539 rcu_read_lock();
540 list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
541 if (iter->state < CARL9170_TID_STATE_IDLE)
542 continue;
543
544 spin_lock_bh(&iter->lock);
545 skb = skb_peek(&iter->queue);
546 if (!skb)
547 goto unlock;
548
549 txinfo = IEEE80211_SKB_CB(skb);
550 arinfo = (void *)txinfo->rate_driver_data;
551 if (time_is_after_jiffies(arinfo->timeout +
552 msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
553 goto unlock;
554
555 super = (void *) skb->data;
556 hdr = (void *) super->frame_data;
557
558 vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
559 CARL9170_TX_SUPER_MISC_VIF_ID_S;
560
561 if (WARN_ON(vif_id >= AR9170_MAX_VIRTUAL_MAC))
562 goto unlock;
563
564 vif = rcu_dereference(ar->vif_priv[vif_id].vif);
565 if (WARN_ON(!vif))
566 goto unlock;
567
568 sta = ieee80211_find_sta(vif, hdr->addr1);
569 if (WARN_ON(!sta))
570 goto unlock;
571
572 ieee80211_stop_tx_ba_session(sta, iter->tid);
573unlock:
574 spin_unlock_bh(&iter->lock);
575
576 }
577 rcu_read_unlock();
578}
579
Christian Lampartera84fab32010-09-06 01:09:20 +0200580void carl9170_tx_janitor(struct work_struct *work)
581{
582 struct ar9170 *ar = container_of(work, struct ar9170,
583 tx_janitor.work);
584 if (!IS_STARTED(ar))
585 return;
586
587 ar->tx_janitor_last_run = jiffies;
588
589 carl9170_check_queue_stop_timeout(ar);
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200590 carl9170_tx_ampdu_timeout(ar);
Christian Lampartera84fab32010-09-06 01:09:20 +0200591
592 if (!atomic_read(&ar->tx_total_queued))
593 return;
594
595 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
596 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
597}
598
599static void __carl9170_tx_process_status(struct ar9170 *ar,
600 const uint8_t cookie, const uint8_t info)
601{
602 struct sk_buff *skb;
603 struct ieee80211_tx_info *txinfo;
604 struct carl9170_tx_info *arinfo;
605 unsigned int r, t, q;
606 bool success = true;
607
608 q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
609
610 skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
611 if (!skb) {
612 /*
613 * We have lost the race to another thread.
614 */
615
616 return ;
617 }
618
619 txinfo = IEEE80211_SKB_CB(skb);
620 arinfo = (void *) txinfo->rate_driver_data;
621
622 if (!(info & CARL9170_TX_STATUS_SUCCESS))
623 success = false;
624
625 r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
626 t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
627
628 carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
629 carl9170_tx_status(ar, skb, success);
630}
631
632void carl9170_tx_process_status(struct ar9170 *ar,
633 const struct carl9170_rsp *cmd)
634{
635 unsigned int i;
636
637 for (i = 0; i < cmd->hdr.ext; i++) {
638 if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
639 print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
640 (void *) cmd, cmd->hdr.len + 4);
641 break;
642 }
643
644 __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
645 cmd->_tx_status[i].info);
646 }
647}
648
649static __le32 carl9170_tx_physet(struct ar9170 *ar,
650 struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
651{
652 struct ieee80211_rate *rate = NULL;
653 u32 power, chains;
654 __le32 tmp;
655
656 tmp = cpu_to_le32(0);
657
658 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
659 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
660 AR9170_TX_PHY_BW_S);
661 /* this works because 40 MHz is 2 and dup is 3 */
662 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
663 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
664 AR9170_TX_PHY_BW_S);
665
666 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
667 tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
668
669 if (txrate->flags & IEEE80211_TX_RC_MCS) {
670 u32 r = txrate->idx;
671 u8 *txpower;
672
673 /* heavy clip control */
674 tmp |= cpu_to_le32((r & 0x7) <<
675 AR9170_TX_PHY_TX_HEAVY_CLIP_S);
676
677 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
678 if (info->band == IEEE80211_BAND_5GHZ)
679 txpower = ar->power_5G_ht40;
680 else
681 txpower = ar->power_2G_ht40;
682 } else {
683 if (info->band == IEEE80211_BAND_5GHZ)
684 txpower = ar->power_5G_ht20;
685 else
686 txpower = ar->power_2G_ht20;
687 }
688
689 power = txpower[r & 7];
690
691 /* +1 dBm for HT40 */
692 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
693 power += 2;
694
695 r <<= AR9170_TX_PHY_MCS_S;
696 BUG_ON(r & ~AR9170_TX_PHY_MCS);
697
698 tmp |= cpu_to_le32(r & AR9170_TX_PHY_MCS);
699 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
700
701 /*
702 * green field preamble does not work.
703 *
704 * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
705 * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
706 */
707 } else {
708 u8 *txpower;
709 u32 mod;
710 u32 phyrate;
711 u8 idx = txrate->idx;
712
713 if (info->band != IEEE80211_BAND_2GHZ) {
714 idx += 4;
715 txpower = ar->power_5G_leg;
716 mod = AR9170_TX_PHY_MOD_OFDM;
717 } else {
718 if (idx < 4) {
719 txpower = ar->power_2G_cck;
720 mod = AR9170_TX_PHY_MOD_CCK;
721 } else {
722 mod = AR9170_TX_PHY_MOD_OFDM;
723 txpower = ar->power_2G_ofdm;
724 }
725 }
726
727 rate = &__carl9170_ratetable[idx];
728
729 phyrate = rate->hw_value & 0xF;
730 power = txpower[(rate->hw_value & 0x30) >> 4];
731 phyrate <<= AR9170_TX_PHY_MCS_S;
732
733 tmp |= cpu_to_le32(mod);
734 tmp |= cpu_to_le32(phyrate);
735
736 /*
737 * short preamble seems to be broken too.
738 *
739 * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
740 * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
741 */
742 }
743 power <<= AR9170_TX_PHY_TX_PWR_S;
744 power &= AR9170_TX_PHY_TX_PWR;
745 tmp |= cpu_to_le32(power);
746
747 /* set TX chains */
748 if (ar->eeprom.tx_mask == 1) {
749 chains = AR9170_TX_PHY_TXCHAIN_1;
750 } else {
751 chains = AR9170_TX_PHY_TXCHAIN_2;
752
753 /* >= 36M legacy OFDM - use only one chain */
754 if (rate && rate->bitrate >= 360 &&
755 !(txrate->flags & IEEE80211_TX_RC_MCS))
756 chains = AR9170_TX_PHY_TXCHAIN_1;
757 }
758 tmp |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_S);
759
760 return tmp;
761}
762
763static bool carl9170_tx_rts_check(struct ar9170 *ar,
764 struct ieee80211_tx_rate *rate,
765 bool ampdu, bool multi)
766{
767 switch (ar->erp_mode) {
768 case CARL9170_ERP_AUTO:
769 if (ampdu)
770 break;
771
772 case CARL9170_ERP_MAC80211:
773 if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
774 break;
775
776 case CARL9170_ERP_RTS:
777 if (likely(!multi))
778 return true;
779
780 default:
781 break;
782 }
783
784 return false;
785}
786
787static bool carl9170_tx_cts_check(struct ar9170 *ar,
788 struct ieee80211_tx_rate *rate)
789{
790 switch (ar->erp_mode) {
791 case CARL9170_ERP_AUTO:
792 case CARL9170_ERP_MAC80211:
793 if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
794 break;
795
796 case CARL9170_ERP_CTS:
797 return true;
798
799 default:
800 break;
801 }
802
803 return false;
804}
805
806static int carl9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
807{
808 struct ieee80211_hdr *hdr;
809 struct _carl9170_tx_superframe *txc;
810 struct carl9170_vif_info *cvif;
811 struct ieee80211_tx_info *info;
812 struct ieee80211_tx_rate *txrate;
813 struct ieee80211_sta *sta;
814 struct carl9170_tx_info *arinfo;
815 unsigned int hw_queue;
816 int i;
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200817 __le16 mac_tmp;
818 u16 len;
Christian Lampartera84fab32010-09-06 01:09:20 +0200819 bool ampdu, no_ack;
820
821 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
822 BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
823 CARL9170_TX_SUPERDESC_LEN);
824
825 BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
826 AR9170_TX_HWDESC_LEN);
827
828 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
829
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200830 BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
831 ((CARL9170_TX_SUPER_MISC_VIF_ID >>
832 CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
833
Christian Lampartera84fab32010-09-06 01:09:20 +0200834 hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
835
836 hdr = (void *)skb->data;
837 info = IEEE80211_SKB_CB(skb);
838 len = skb->len;
839
840 /*
841 * Note: If the frame was sent through a monitor interface,
842 * the ieee80211_vif pointer can be NULL.
843 */
844 if (likely(info->control.vif))
845 cvif = (void *) info->control.vif->drv_priv;
846 else
847 cvif = NULL;
848
849 sta = info->control.sta;
850
851 txc = (void *)skb_push(skb, sizeof(*txc));
852 memset(txc, 0, sizeof(*txc));
853
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200854 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
855
856 if (likely(cvif))
857 SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
858
859 if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
860 txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
861
862 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
863 txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
864
865 mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
866 AR9170_TX_MAC_BACKOFF);
867 mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &&
868 AR9170_TX_MAC_QOS);
869
Christian Lampartera84fab32010-09-06 01:09:20 +0200870 no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200871 if (unlikely(no_ack))
872 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
Christian Lampartera84fab32010-09-06 01:09:20 +0200873
874 if (info->control.hw_key) {
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200875 len += info->control.hw_key->icv_len;
Christian Lampartera84fab32010-09-06 01:09:20 +0200876
877 switch (info->control.hw_key->cipher) {
878 case WLAN_CIPHER_SUITE_WEP40:
879 case WLAN_CIPHER_SUITE_WEP104:
880 case WLAN_CIPHER_SUITE_TKIP:
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200881 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
Christian Lampartera84fab32010-09-06 01:09:20 +0200882 break;
883 case WLAN_CIPHER_SUITE_CCMP:
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200884 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
Christian Lampartera84fab32010-09-06 01:09:20 +0200885 break;
886 default:
887 WARN_ON(1);
888 goto err_out;
889 }
890 }
891
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200892 ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
893 if (ampdu) {
894 unsigned int density, factor;
Christian Lampartera84fab32010-09-06 01:09:20 +0200895
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200896 if (unlikely(!sta || !cvif))
897 goto err_out;
Christian Lampartera84fab32010-09-06 01:09:20 +0200898
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200899 factor = min_t(unsigned int, 1u,
900 info->control.sta->ht_cap.ampdu_factor);
Christian Lampartera84fab32010-09-06 01:09:20 +0200901
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200902 density = info->control.sta->ht_cap.ampdu_density;
Christian Lampartera84fab32010-09-06 01:09:20 +0200903
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200904 if (density) {
905 /*
906 * Watch out!
907 *
908 * Otus uses slightly different density values than
909 * those from the 802.11n spec.
910 */
Christian Lampartera84fab32010-09-06 01:09:20 +0200911
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200912 density = max_t(unsigned int, density + 1, 7u);
913 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200914
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200915 SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
916 txc->s.ampdu_settings, density);
Christian Lampartera84fab32010-09-06 01:09:20 +0200917
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200918 SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
919 txc->s.ampdu_settings, factor);
Christian Lampartera84fab32010-09-06 01:09:20 +0200920
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200921 for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
Christian Lampartera84fab32010-09-06 01:09:20 +0200922 txrate = &info->control.rates[i];
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200923 if (txrate->idx >= 0) {
924 txc->s.ri[i] =
925 CARL9170_TX_SUPER_RI_AMPDU;
926
927 if (WARN_ON(!(txrate->flags &
928 IEEE80211_TX_RC_MCS))) {
929 /*
930 * Not sure if it's even possible
931 * to aggregate non-ht rates with
932 * this HW.
933 */
934 goto err_out;
935 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200936 continue;
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200937 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200938
939 txrate->idx = 0;
940 txrate->count = ar->hw->max_rate_tries;
941 }
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200942
943 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
Christian Lampartera84fab32010-09-06 01:09:20 +0200944 }
945
946 /*
947 * NOTE: For the first rate, the ERP & AMPDU flags are directly
948 * taken from mac_control. For all fallback rate, the firmware
949 * updates the mac_control flags from the rate info field.
950 */
951 for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
952 txrate = &info->control.rates[i];
953 if (txrate->idx < 0)
954 break;
955
956 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
957 txrate->count);
958
959 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
960 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
961 CARL9170_TX_SUPER_RI_ERP_PROT_S);
962 else if (carl9170_tx_cts_check(ar, txrate))
963 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
964 CARL9170_TX_SUPER_RI_ERP_PROT_S);
965
Christian Lampartera84fab32010-09-06 01:09:20 +0200966 txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
967 }
968
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200969 txrate = &info->control.rates[0];
970 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
Christian Lampartera84fab32010-09-06 01:09:20 +0200971
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200972 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
973 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
974 else if (carl9170_tx_cts_check(ar, txrate))
975 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
Christian Lampartera84fab32010-09-06 01:09:20 +0200976
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200977 txc->s.len = cpu_to_le16(skb->len);
978 txc->f.length = cpu_to_le16(len + FCS_LEN);
979 txc->f.mac_control = mac_tmp;
980 txc->f.phy_control = carl9170_tx_physet(ar, info, txrate);
Christian Lampartera84fab32010-09-06 01:09:20 +0200981
982 arinfo = (void *)info->rate_driver_data;
983 arinfo->timeout = jiffies;
984 arinfo->ar = ar;
985 kref_init(&arinfo->ref);
986 return 0;
987
988err_out:
989 skb_pull(skb, sizeof(*txc));
990 return -EINVAL;
991}
992
993static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
994{
995 struct _carl9170_tx_superframe *super;
996
997 super = (void *) skb->data;
998 super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
999}
1000
1001static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
1002{
1003 struct _carl9170_tx_superframe *super;
1004 int tmp;
1005
1006 super = (void *) skb->data;
1007
1008 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
1009 CARL9170_TX_SUPER_AMPDU_DENSITY_S;
1010
1011 /*
1012 * If you haven't noticed carl9170_tx_prepare has already filled
1013 * in all ampdu spacing & factor parameters.
1014 * Now it's the time to check whenever the settings have to be
1015 * updated by the firmware, or if everything is still the same.
1016 *
1017 * There's no sane way to handle different density values with
1018 * this hardware, so we may as well just do the compare in the
1019 * driver.
1020 */
1021
1022 if (tmp != ar->current_density) {
1023 ar->current_density = tmp;
1024 super->s.ampdu_settings |=
1025 CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
1026 }
1027
1028 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
1029 CARL9170_TX_SUPER_AMPDU_FACTOR_S;
1030
1031 if (tmp != ar->current_factor) {
1032 ar->current_factor = tmp;
1033 super->s.ampdu_settings |=
1034 CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
1035 }
1036}
1037
1038static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
1039 struct sk_buff *_src)
1040{
1041 struct _carl9170_tx_superframe *dest, *src;
1042
1043 dest = (void *) _dest->data;
1044 src = (void *) _src->data;
1045
1046 /*
1047 * The mac80211 rate control algorithm expects that all MPDUs in
1048 * an AMPDU share the same tx vectors.
1049 * This is not really obvious right now, because the hardware
1050 * does the AMPDU setup according to its own rulebook.
1051 * Our nicely assembled, strictly monotonic increasing mpdu
1052 * chains will be broken up, mashed back together...
1053 */
1054
1055 return (dest->f.phy_control == src->f.phy_control);
1056}
1057
1058static void carl9170_tx_ampdu(struct ar9170 *ar)
1059{
1060 struct sk_buff_head agg;
1061 struct carl9170_sta_tid *tid_info;
1062 struct sk_buff *skb, *first;
1063 unsigned int i = 0, done_ampdus = 0;
1064 u16 seq, queue, tmpssn;
1065
1066 atomic_inc(&ar->tx_ampdu_scheduler);
1067 ar->tx_ampdu_schedule = false;
1068
1069 if (atomic_read(&ar->tx_ampdu_upload))
1070 return;
1071
1072 if (!ar->tx_ampdu_list_len)
1073 return;
1074
1075 __skb_queue_head_init(&agg);
1076
1077 rcu_read_lock();
1078 tid_info = rcu_dereference(ar->tx_ampdu_iter);
1079 if (WARN_ON_ONCE(!tid_info)) {
1080 rcu_read_unlock();
1081 return;
1082 }
1083
1084retry:
1085 list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
1086 i++;
1087
1088 if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
1089 continue;
1090
1091 queue = TID_TO_WME_AC(tid_info->tid);
1092
1093 spin_lock_bh(&tid_info->lock);
Christian Lamparter042c53f2010-09-26 21:48:31 +02001094 if (tid_info->state != CARL9170_TID_STATE_XMIT)
1095 goto processed;
Christian Lampartera84fab32010-09-06 01:09:20 +02001096
1097 tid_info->counter++;
1098 first = skb_peek(&tid_info->queue);
1099 tmpssn = carl9170_get_seq(first);
1100 seq = tid_info->snx;
1101
1102 if (unlikely(tmpssn != seq)) {
1103 tid_info->state = CARL9170_TID_STATE_IDLE;
1104
1105 goto processed;
1106 }
1107
1108 while ((skb = skb_peek(&tid_info->queue))) {
1109 /* strict 0, 1, ..., n - 1, n frame sequence order */
1110 if (unlikely(carl9170_get_seq(skb) != seq))
1111 break;
1112
1113 /* don't upload more than AMPDU FACTOR allows. */
1114 if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
1115 (tid_info->max - 1)))
1116 break;
1117
1118 if (!carl9170_tx_rate_check(ar, skb, first))
1119 break;
1120
1121 atomic_inc(&ar->tx_ampdu_upload);
1122 tid_info->snx = seq = SEQ_NEXT(seq);
1123 __skb_unlink(skb, &tid_info->queue);
1124
1125 __skb_queue_tail(&agg, skb);
1126
1127 if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
1128 break;
1129 }
1130
1131 if (skb_queue_empty(&tid_info->queue) ||
1132 carl9170_get_seq(skb_peek(&tid_info->queue)) !=
1133 tid_info->snx) {
1134 /*
1135 * stop TID, if A-MPDU frames are still missing,
1136 * or whenever the queue is empty.
1137 */
1138
1139 tid_info->state = CARL9170_TID_STATE_IDLE;
1140 }
1141 done_ampdus++;
1142
1143processed:
1144 spin_unlock_bh(&tid_info->lock);
1145
1146 if (skb_queue_empty(&agg))
1147 continue;
1148
1149 /* apply ampdu spacing & factor settings */
1150 carl9170_set_ampdu_params(ar, skb_peek(&agg));
1151
1152 /* set aggregation push bit */
1153 carl9170_set_immba(ar, skb_peek_tail(&agg));
1154
1155 spin_lock_bh(&ar->tx_pending[queue].lock);
1156 skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
1157 spin_unlock_bh(&ar->tx_pending[queue].lock);
1158 ar->tx_schedule = true;
1159 }
1160 if ((done_ampdus++ == 0) && (i++ == 0))
1161 goto retry;
1162
1163 rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
1164 rcu_read_unlock();
1165}
1166
1167static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
1168 struct sk_buff_head *queue)
1169{
1170 struct sk_buff *skb;
1171 struct ieee80211_tx_info *info;
1172 struct carl9170_tx_info *arinfo;
1173
1174 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1175
1176 spin_lock_bh(&queue->lock);
1177 skb = skb_peek(queue);
1178 if (unlikely(!skb))
1179 goto err_unlock;
1180
1181 if (carl9170_alloc_dev_space(ar, skb))
1182 goto err_unlock;
1183
1184 __skb_unlink(skb, queue);
1185 spin_unlock_bh(&queue->lock);
1186
1187 info = IEEE80211_SKB_CB(skb);
1188 arinfo = (void *) info->rate_driver_data;
1189
1190 arinfo->timeout = jiffies;
1191
1192 /*
1193 * increase ref count to "2".
1194 * Ref counting is the easiest way to solve the race between
1195 * the the urb's completion routine: carl9170_tx_callback and
1196 * wlan tx status functions: carl9170_tx_status/janitor.
1197 */
1198 carl9170_tx_get_skb(skb);
1199
1200 return skb;
1201
1202err_unlock:
1203 spin_unlock_bh(&queue->lock);
1204 return NULL;
1205}
1206
1207void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
1208{
1209 struct _carl9170_tx_superframe *super;
1210 uint8_t q = 0;
1211
1212 ar->tx_dropped++;
1213
1214 super = (void *)skb->data;
1215 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
1216 ar9170_qmap[carl9170_get_queue(ar, skb)]);
1217 __carl9170_tx_process_status(ar, super->s.cookie, q);
1218}
1219
1220static void carl9170_tx(struct ar9170 *ar)
1221{
1222 struct sk_buff *skb;
1223 unsigned int i, q;
1224 bool schedule_garbagecollector = false;
1225
1226 ar->tx_schedule = false;
1227
1228 if (unlikely(!IS_STARTED(ar)))
1229 return;
1230
1231 carl9170_usb_handle_tx_err(ar);
1232
1233 for (i = 0; i < ar->hw->queues; i++) {
1234 while (!skb_queue_empty(&ar->tx_pending[i])) {
1235 skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
1236 if (unlikely(!skb))
1237 break;
1238
1239 atomic_inc(&ar->tx_total_pending);
1240
1241 q = __carl9170_get_queue(ar, i);
1242 /*
1243 * NB: tx_status[i] vs. tx_status[q],
1244 * TODO: Move into pick_skb or alloc_dev_space.
1245 */
1246 skb_queue_tail(&ar->tx_status[q], skb);
1247
1248 carl9170_usb_tx(ar, skb);
1249 schedule_garbagecollector = true;
1250 }
1251 }
1252
1253 if (!schedule_garbagecollector)
1254 return;
1255
1256 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
1257 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
1258}
1259
1260static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
1261 struct ieee80211_sta *sta, struct sk_buff *skb)
1262{
1263 struct carl9170_sta_info *sta_info;
1264 struct carl9170_sta_tid *agg;
1265 struct sk_buff *iter;
1266 unsigned int max;
1267 u16 tid, seq, qseq, off;
1268 bool run = false;
1269
1270 tid = carl9170_get_tid(skb);
1271 seq = carl9170_get_seq(skb);
1272 sta_info = (void *) sta->drv_priv;
1273
1274 rcu_read_lock();
1275 agg = rcu_dereference(sta_info->agg[tid]);
1276 max = sta_info->ampdu_max_len;
1277
1278 if (!agg)
1279 goto err_unlock_rcu;
1280
1281 spin_lock_bh(&agg->lock);
1282 if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
1283 goto err_unlock;
1284
1285 /* check if sequence is within the BA window */
1286 if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
1287 goto err_unlock;
1288
1289 if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
1290 goto err_unlock;
1291
1292 off = SEQ_DIFF(seq, agg->bsn);
1293 if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
1294 goto err_unlock;
1295
1296 if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
1297 __skb_queue_tail(&agg->queue, skb);
1298 agg->hsn = seq;
1299 goto queued;
1300 }
1301
1302 skb_queue_reverse_walk(&agg->queue, iter) {
1303 qseq = carl9170_get_seq(iter);
1304
1305 if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
1306 __skb_queue_after(&agg->queue, iter, skb);
1307 goto queued;
1308 }
1309 }
1310
1311 __skb_queue_head(&agg->queue, skb);
1312queued:
1313
1314 if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
1315 if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
1316 agg->state = CARL9170_TID_STATE_XMIT;
1317 run = true;
1318 }
1319 }
1320
1321 spin_unlock_bh(&agg->lock);
1322 rcu_read_unlock();
1323
1324 return run;
1325
1326err_unlock:
1327 spin_unlock_bh(&agg->lock);
1328
1329err_unlock_rcu:
1330 rcu_read_unlock();
1331 carl9170_tx_status(ar, skb, false);
1332 ar->tx_dropped++;
1333 return false;
1334}
1335
1336int carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1337{
1338 struct ar9170 *ar = hw->priv;
1339 struct ieee80211_tx_info *info;
1340 struct ieee80211_sta *sta;
1341 bool run;
1342
1343 if (unlikely(!IS_STARTED(ar)))
1344 goto err_free;
1345
1346 info = IEEE80211_SKB_CB(skb);
1347 sta = info->control.sta;
1348
1349 if (unlikely(carl9170_tx_prepare(ar, skb)))
1350 goto err_free;
1351
1352 carl9170_tx_accounting(ar, skb);
1353 /*
1354 * from now on, one has to use carl9170_tx_status to free
1355 * all ressouces which are associated with the frame.
1356 */
1357
1358 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
1359 if (WARN_ON_ONCE(!sta))
1360 goto err_free;
1361
1362 run = carl9170_tx_ampdu_queue(ar, sta, skb);
1363 if (run)
1364 carl9170_tx_ampdu(ar);
1365
1366 } else {
1367 unsigned int queue = skb_get_queue_mapping(skb);
1368
1369 skb_queue_tail(&ar->tx_pending[queue], skb);
1370 }
1371
1372 carl9170_tx(ar);
1373 return NETDEV_TX_OK;
1374
1375err_free:
1376 ar->tx_dropped++;
1377 dev_kfree_skb_any(skb);
1378 return NETDEV_TX_OK;
1379}
1380
1381void carl9170_tx_scheduler(struct ar9170 *ar)
1382{
1383
1384 if (ar->tx_ampdu_schedule)
1385 carl9170_tx_ampdu(ar);
1386
1387 if (ar->tx_schedule)
1388 carl9170_tx(ar);
1389}