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