SUNRPC: Fix a deadlock in rpc_client_register()
[cascardo/linux.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         int queue;
160
161         queue = skb_get_queue_mapping(skb);
162
163         spin_lock_bh(&ar->tx_stats_lock);
164
165         ar->tx_stats[queue].len--;
166
167         if (!is_mem_full(ar)) {
168                 unsigned int i;
169                 for (i = 0; i < ar->hw->queues; i++) {
170                         if (ar->tx_stats[i].len >= CARL9170_NUM_TX_LIMIT_SOFT)
171                                 continue;
172
173                         if (ieee80211_queue_stopped(ar->hw, i)) {
174                                 unsigned long tmp;
175
176                                 tmp = jiffies - ar->queue_stop_timeout[i];
177                                 if (tmp > ar->max_queue_stop_timeout[i])
178                                         ar->max_queue_stop_timeout[i] = tmp;
179                         }
180
181                         ieee80211_wake_queue(ar->hw, i);
182                 }
183         }
184
185         spin_unlock_bh(&ar->tx_stats_lock);
186
187         if (atomic_dec_and_test(&ar->tx_total_queued))
188                 complete(&ar->tx_flush);
189 }
190
191 static int carl9170_alloc_dev_space(struct ar9170 *ar, struct sk_buff *skb)
192 {
193         struct _carl9170_tx_superframe *super = (void *) skb->data;
194         unsigned int chunks;
195         int cookie = -1;
196
197         atomic_inc(&ar->mem_allocs);
198
199         chunks = DIV_ROUND_UP(skb->len, ar->fw.mem_block_size);
200         if (unlikely(atomic_sub_return(chunks, &ar->mem_free_blocks) < 0)) {
201                 atomic_add(chunks, &ar->mem_free_blocks);
202                 return -ENOSPC;
203         }
204
205         spin_lock_bh(&ar->mem_lock);
206         cookie = bitmap_find_free_region(ar->mem_bitmap, ar->fw.mem_blocks, 0);
207         spin_unlock_bh(&ar->mem_lock);
208
209         if (unlikely(cookie < 0)) {
210                 atomic_add(chunks, &ar->mem_free_blocks);
211                 return -ENOSPC;
212         }
213
214         super = (void *) skb->data;
215
216         /*
217          * Cookie #0 serves two special purposes:
218          *  1. The firmware might use it generate BlockACK frames
219          *     in responds of an incoming BlockAckReqs.
220          *
221          *  2. Prevent double-free bugs.
222          */
223         super->s.cookie = (u8) cookie + 1;
224         return 0;
225 }
226
227 static void carl9170_release_dev_space(struct ar9170 *ar, struct sk_buff *skb)
228 {
229         struct _carl9170_tx_superframe *super = (void *) skb->data;
230         int cookie;
231
232         /* make a local copy of the cookie */
233         cookie = super->s.cookie;
234         /* invalidate cookie */
235         super->s.cookie = 0;
236
237         /*
238          * Do a out-of-bounds check on the cookie:
239          *
240          *  * cookie "0" is reserved and won't be assigned to any
241          *    out-going frame. Internally however, it is used to
242          *    mark no longer/un-accounted frames and serves as a
243          *    cheap way of preventing frames from being freed
244          *    twice by _accident_. NB: There is a tiny race...
245          *
246          *  * obviously, cookie number is limited by the amount
247          *    of available memory blocks, so the number can
248          *    never execeed the mem_blocks count.
249          */
250         if (unlikely(WARN_ON_ONCE(cookie == 0) ||
251             WARN_ON_ONCE(cookie > ar->fw.mem_blocks)))
252                 return;
253
254         atomic_add(DIV_ROUND_UP(skb->len, ar->fw.mem_block_size),
255                    &ar->mem_free_blocks);
256
257         spin_lock_bh(&ar->mem_lock);
258         bitmap_release_region(ar->mem_bitmap, cookie - 1, 0);
259         spin_unlock_bh(&ar->mem_lock);
260 }
261
262 /* Called from any context */
263 static void carl9170_tx_release(struct kref *ref)
264 {
265         struct ar9170 *ar;
266         struct carl9170_tx_info *arinfo;
267         struct ieee80211_tx_info *txinfo;
268         struct sk_buff *skb;
269
270         arinfo = container_of(ref, struct carl9170_tx_info, ref);
271         txinfo = container_of((void *) arinfo, struct ieee80211_tx_info,
272                               rate_driver_data);
273         skb = container_of((void *) txinfo, struct sk_buff, cb);
274
275         ar = arinfo->ar;
276         if (WARN_ON_ONCE(!ar))
277                 return;
278
279         BUILD_BUG_ON(
280             offsetof(struct ieee80211_tx_info, status.ack_signal) != 20);
281
282         memset(&txinfo->status.ack_signal, 0,
283                sizeof(struct ieee80211_tx_info) -
284                offsetof(struct ieee80211_tx_info, status.ack_signal));
285
286         if (atomic_read(&ar->tx_total_queued))
287                 ar->tx_schedule = true;
288
289         if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
290                 if (!atomic_read(&ar->tx_ampdu_upload))
291                         ar->tx_ampdu_schedule = true;
292
293                 if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
294                         struct _carl9170_tx_superframe *super;
295
296                         super = (void *)skb->data;
297                         txinfo->status.ampdu_len = super->s.rix;
298                         txinfo->status.ampdu_ack_len = super->s.cnt;
299                 } else if ((txinfo->flags & IEEE80211_TX_STAT_ACK) &&
300                            !(txinfo->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)) {
301                         /*
302                          * drop redundant tx_status reports:
303                          *
304                          * 1. ampdu_ack_len of the final tx_status does
305                          *    include the feedback of this particular frame.
306                          *
307                          * 2. tx_status_irqsafe only queues up to 128
308                          *    tx feedback reports and discards the rest.
309                          *
310                          * 3. minstrel_ht is picky, it only accepts
311                          *    reports of frames with the TX_STATUS_AMPDU flag.
312                          *
313                          * 4. mac80211 is not particularly interested in
314                          *    feedback either [CTL_REQ_TX_STATUS not set]
315                          */
316
317                         ieee80211_free_txskb(ar->hw, skb);
318                         return;
319                 } else {
320                         /*
321                          * Either the frame transmission has failed or
322                          * mac80211 requested tx status.
323                          */
324                 }
325         }
326
327         skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
328         ieee80211_tx_status_irqsafe(ar->hw, skb);
329 }
330
331 void carl9170_tx_get_skb(struct sk_buff *skb)
332 {
333         struct carl9170_tx_info *arinfo = (void *)
334                 (IEEE80211_SKB_CB(skb))->rate_driver_data;
335         kref_get(&arinfo->ref);
336 }
337
338 int carl9170_tx_put_skb(struct sk_buff *skb)
339 {
340         struct carl9170_tx_info *arinfo = (void *)
341                 (IEEE80211_SKB_CB(skb))->rate_driver_data;
342
343         return kref_put(&arinfo->ref, carl9170_tx_release);
344 }
345
346 /* Caller must hold the tid_info->lock & rcu_read_lock */
347 static void carl9170_tx_shift_bm(struct ar9170 *ar,
348         struct carl9170_sta_tid *tid_info, u16 seq)
349 {
350         u16 off;
351
352         off = SEQ_DIFF(seq, tid_info->bsn);
353
354         if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
355                 return;
356
357         /*
358          * Sanity check. For each MPDU we set the bit in bitmap and
359          * clear it once we received the tx_status.
360          * But if the bit is already cleared then we've been bitten
361          * by a bug.
362          */
363         WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
364
365         off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
366         if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
367                 return;
368
369         if (!bitmap_empty(tid_info->bitmap, off))
370                 off = find_first_bit(tid_info->bitmap, off);
371
372         tid_info->bsn += off;
373         tid_info->bsn &= 0x0fff;
374
375         bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
376                            off, CARL9170_BAW_BITS);
377 }
378
379 static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
380         struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
381 {
382         struct _carl9170_tx_superframe *super = (void *) skb->data;
383         struct ieee80211_hdr *hdr = (void *) super->frame_data;
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                 return;
392
393         rcu_read_lock();
394         sta = __carl9170_get_tx_sta(ar, skb);
395         if (unlikely(!sta))
396                 goto out_rcu;
397
398         tid = get_tid_h(hdr);
399
400         sta_info = (void *) sta->drv_priv;
401         tid_info = rcu_dereference(sta_info->agg[tid]);
402         if (!tid_info)
403                 goto out_rcu;
404
405         spin_lock_bh(&tid_info->lock);
406         if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
407                 carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
408
409         if (sta_info->stats[tid].clear) {
410                 sta_info->stats[tid].clear = false;
411                 sta_info->stats[tid].req = false;
412                 sta_info->stats[tid].ampdu_len = 0;
413                 sta_info->stats[tid].ampdu_ack_len = 0;
414         }
415
416         sta_info->stats[tid].ampdu_len++;
417         if (txinfo->status.rates[0].count == 1)
418                 sta_info->stats[tid].ampdu_ack_len++;
419
420         if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
421                 sta_info->stats[tid].req = true;
422
423         if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
424                 super->s.rix = sta_info->stats[tid].ampdu_len;
425                 super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
426                 txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
427                 if (sta_info->stats[tid].req)
428                         txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
429
430                 sta_info->stats[tid].clear = true;
431         }
432         spin_unlock_bh(&tid_info->lock);
433
434 out_rcu:
435         rcu_read_unlock();
436 }
437
438 static void carl9170_tx_bar_status(struct ar9170 *ar, struct sk_buff *skb,
439         struct ieee80211_tx_info *tx_info)
440 {
441         struct _carl9170_tx_superframe *super = (void *) skb->data;
442         struct ieee80211_bar *bar = (void *) super->frame_data;
443
444         /*
445          * Unlike all other frames, the status report for BARs does
446          * not directly come from the hardware as it is incapable of
447          * matching a BA to a previously send BAR.
448          * Instead the RX-path will scan for incoming BAs and set the
449          * IEEE80211_TX_STAT_ACK if it sees one that was likely
450          * caused by a BAR from us.
451          */
452
453         if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
454            !(tx_info->flags & IEEE80211_TX_STAT_ACK)) {
455                 struct carl9170_bar_list_entry *entry;
456                 int queue = skb_get_queue_mapping(skb);
457
458                 rcu_read_lock();
459                 list_for_each_entry_rcu(entry, &ar->bar_list[queue], list) {
460                         if (entry->skb == skb) {
461                                 spin_lock_bh(&ar->bar_list_lock[queue]);
462                                 list_del_rcu(&entry->list);
463                                 spin_unlock_bh(&ar->bar_list_lock[queue]);
464                                 kfree_rcu(entry, head);
465                                 goto out;
466                         }
467                 }
468
469                 WARN(1, "bar not found in %d - ra:%pM ta:%pM c:%x ssn:%x\n",
470                        queue, bar->ra, bar->ta, bar->control,
471                         bar->start_seq_num);
472 out:
473                 rcu_read_unlock();
474         }
475 }
476
477 void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
478                         const bool success)
479 {
480         struct ieee80211_tx_info *txinfo;
481
482         carl9170_tx_accounting_free(ar, skb);
483
484         txinfo = IEEE80211_SKB_CB(skb);
485
486         carl9170_tx_bar_status(ar, skb, txinfo);
487
488         if (success)
489                 txinfo->flags |= IEEE80211_TX_STAT_ACK;
490         else
491                 ar->tx_ack_failures++;
492
493         if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
494                 carl9170_tx_status_process_ampdu(ar, skb, txinfo);
495
496         carl9170_tx_ps_unblock(ar, skb);
497         carl9170_tx_put_skb(skb);
498 }
499
500 /* This function may be called form any context */
501 void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
502 {
503         struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
504
505         atomic_dec(&ar->tx_total_pending);
506
507         if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
508                 atomic_dec(&ar->tx_ampdu_upload);
509
510         if (carl9170_tx_put_skb(skb))
511                 tasklet_hi_schedule(&ar->usb_tasklet);
512 }
513
514 static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
515                                                struct sk_buff_head *queue)
516 {
517         struct sk_buff *skb;
518
519         spin_lock_bh(&queue->lock);
520         skb_queue_walk(queue, skb) {
521                 struct _carl9170_tx_superframe *txc = (void *) skb->data;
522
523                 if (txc->s.cookie != cookie)
524                         continue;
525
526                 __skb_unlink(skb, queue);
527                 spin_unlock_bh(&queue->lock);
528
529                 carl9170_release_dev_space(ar, skb);
530                 return skb;
531         }
532         spin_unlock_bh(&queue->lock);
533
534         return NULL;
535 }
536
537 static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
538         unsigned int tries, struct ieee80211_tx_info *txinfo)
539 {
540         unsigned int i;
541
542         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
543                 if (txinfo->status.rates[i].idx < 0)
544                         break;
545
546                 if (i == rix) {
547                         txinfo->status.rates[i].count = tries;
548                         i++;
549                         break;
550                 }
551         }
552
553         for (; i < IEEE80211_TX_MAX_RATES; i++) {
554                 txinfo->status.rates[i].idx = -1;
555                 txinfo->status.rates[i].count = 0;
556         }
557 }
558
559 static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
560 {
561         int i;
562         struct sk_buff *skb;
563         struct ieee80211_tx_info *txinfo;
564         struct carl9170_tx_info *arinfo;
565         bool restart = false;
566
567         for (i = 0; i < ar->hw->queues; i++) {
568                 spin_lock_bh(&ar->tx_status[i].lock);
569
570                 skb = skb_peek(&ar->tx_status[i]);
571
572                 if (!skb)
573                         goto next;
574
575                 txinfo = IEEE80211_SKB_CB(skb);
576                 arinfo = (void *) txinfo->rate_driver_data;
577
578                 if (time_is_before_jiffies(arinfo->timeout +
579                     msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
580                         restart = true;
581
582 next:
583                 spin_unlock_bh(&ar->tx_status[i].lock);
584         }
585
586         if (restart) {
587                 /*
588                  * At least one queue has been stuck for long enough.
589                  * Give the device a kick and hope it gets back to
590                  * work.
591                  *
592                  * possible reasons may include:
593                  *  - frames got lost/corrupted (bad connection to the device)
594                  *  - stalled rx processing/usb controller hiccups
595                  *  - firmware errors/bugs
596                  *  - every bug you can think of.
597                  *  - all bugs you can't...
598                  *  - ...
599                  */
600                 carl9170_restart(ar, CARL9170_RR_STUCK_TX);
601         }
602 }
603
604 static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
605 {
606         struct carl9170_sta_tid *iter;
607         struct sk_buff *skb;
608         struct ieee80211_tx_info *txinfo;
609         struct carl9170_tx_info *arinfo;
610         struct ieee80211_sta *sta;
611
612         rcu_read_lock();
613         list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
614                 if (iter->state < CARL9170_TID_STATE_IDLE)
615                         continue;
616
617                 spin_lock_bh(&iter->lock);
618                 skb = skb_peek(&iter->queue);
619                 if (!skb)
620                         goto unlock;
621
622                 txinfo = IEEE80211_SKB_CB(skb);
623                 arinfo = (void *)txinfo->rate_driver_data;
624                 if (time_is_after_jiffies(arinfo->timeout +
625                     msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
626                         goto unlock;
627
628                 sta = __carl9170_get_tx_sta(ar, skb);
629                 if (WARN_ON(!sta))
630                         goto unlock;
631
632                 ieee80211_stop_tx_ba_session(sta, iter->tid);
633 unlock:
634                 spin_unlock_bh(&iter->lock);
635
636         }
637         rcu_read_unlock();
638 }
639
640 void carl9170_tx_janitor(struct work_struct *work)
641 {
642         struct ar9170 *ar = container_of(work, struct ar9170,
643                                          tx_janitor.work);
644         if (!IS_STARTED(ar))
645                 return;
646
647         ar->tx_janitor_last_run = jiffies;
648
649         carl9170_check_queue_stop_timeout(ar);
650         carl9170_tx_ampdu_timeout(ar);
651
652         if (!atomic_read(&ar->tx_total_queued))
653                 return;
654
655         ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
656                 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
657 }
658
659 static void __carl9170_tx_process_status(struct ar9170 *ar,
660         const uint8_t cookie, const uint8_t info)
661 {
662         struct sk_buff *skb;
663         struct ieee80211_tx_info *txinfo;
664         unsigned int r, t, q;
665         bool success = true;
666
667         q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
668
669         skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
670         if (!skb) {
671                 /*
672                  * We have lost the race to another thread.
673                  */
674
675                 return ;
676         }
677
678         txinfo = IEEE80211_SKB_CB(skb);
679
680         if (!(info & CARL9170_TX_STATUS_SUCCESS))
681                 success = false;
682
683         r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
684         t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
685
686         carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
687         carl9170_tx_status(ar, skb, success);
688 }
689
690 void carl9170_tx_process_status(struct ar9170 *ar,
691                                 const struct carl9170_rsp *cmd)
692 {
693         unsigned int i;
694
695         for (i = 0;  i < cmd->hdr.ext; i++) {
696                 if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
697                         print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
698                                              (void *) cmd, cmd->hdr.len + 4);
699                         break;
700                 }
701
702                 __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
703                                              cmd->_tx_status[i].info);
704         }
705 }
706
707 static void carl9170_tx_rate_tpc_chains(struct ar9170 *ar,
708         struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate,
709         unsigned int *phyrate, unsigned int *tpc, unsigned int *chains)
710 {
711         struct ieee80211_rate *rate = NULL;
712         u8 *txpower;
713         unsigned int idx;
714
715         idx = txrate->idx;
716         *tpc = 0;
717         *phyrate = 0;
718
719         if (txrate->flags & IEEE80211_TX_RC_MCS) {
720                 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
721                         /* +1 dBm for HT40 */
722                         *tpc += 2;
723
724                         if (info->band == IEEE80211_BAND_2GHZ)
725                                 txpower = ar->power_2G_ht40;
726                         else
727                                 txpower = ar->power_5G_ht40;
728                 } else {
729                         if (info->band == IEEE80211_BAND_2GHZ)
730                                 txpower = ar->power_2G_ht20;
731                         else
732                                 txpower = ar->power_5G_ht20;
733                 }
734
735                 *phyrate = txrate->idx;
736                 *tpc += txpower[idx & 7];
737         } else {
738                 if (info->band == IEEE80211_BAND_2GHZ) {
739                         if (idx < 4)
740                                 txpower = ar->power_2G_cck;
741                         else
742                                 txpower = ar->power_2G_ofdm;
743                 } else {
744                         txpower = ar->power_5G_leg;
745                         idx += 4;
746                 }
747
748                 rate = &__carl9170_ratetable[idx];
749                 *tpc += txpower[(rate->hw_value & 0x30) >> 4];
750                 *phyrate = rate->hw_value & 0xf;
751         }
752
753         if (ar->eeprom.tx_mask == 1) {
754                 *chains = AR9170_TX_PHY_TXCHAIN_1;
755         } else {
756                 if (!(txrate->flags & IEEE80211_TX_RC_MCS) &&
757                     rate && rate->bitrate >= 360)
758                         *chains = AR9170_TX_PHY_TXCHAIN_1;
759                 else
760                         *chains = AR9170_TX_PHY_TXCHAIN_2;
761         }
762
763         *tpc = min_t(unsigned int, *tpc, ar->hw->conf.power_level * 2);
764 }
765
766 static __le32 carl9170_tx_physet(struct ar9170 *ar,
767         struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
768 {
769         unsigned int power = 0, chains = 0, phyrate = 0;
770         __le32 tmp;
771
772         tmp = cpu_to_le32(0);
773
774         if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
775                 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
776                         AR9170_TX_PHY_BW_S);
777         /* this works because 40 MHz is 2 and dup is 3 */
778         if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
779                 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
780                         AR9170_TX_PHY_BW_S);
781
782         if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
783                 tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
784
785         if (txrate->flags & IEEE80211_TX_RC_MCS) {
786                 SET_VAL(AR9170_TX_PHY_MCS, phyrate, txrate->idx);
787
788                 /* heavy clip control */
789                 tmp |= cpu_to_le32((txrate->idx & 0x7) <<
790                         AR9170_TX_PHY_TX_HEAVY_CLIP_S);
791
792                 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
793
794                 /*
795                  * green field preamble does not work.
796                  *
797                  * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
798                  * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
799                  */
800         } else {
801                 if (info->band == IEEE80211_BAND_2GHZ) {
802                         if (txrate->idx <= AR9170_TX_PHY_RATE_CCK_11M)
803                                 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_CCK);
804                         else
805                                 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
806                 } else {
807                         tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
808                 }
809
810                 /*
811                  * short preamble seems to be broken too.
812                  *
813                  * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
814                  *      tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
815                  */
816         }
817         carl9170_tx_rate_tpc_chains(ar, info, txrate,
818                                     &phyrate, &power, &chains);
819
820         tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_MCS, phyrate));
821         tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TX_PWR, power));
822         tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TXCHAIN, chains));
823         return tmp;
824 }
825
826 static bool carl9170_tx_rts_check(struct ar9170 *ar,
827                                   struct ieee80211_tx_rate *rate,
828                                   bool ampdu, bool multi)
829 {
830         switch (ar->erp_mode) {
831         case CARL9170_ERP_AUTO:
832                 if (ampdu)
833                         break;
834
835         case CARL9170_ERP_MAC80211:
836                 if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
837                         break;
838
839         case CARL9170_ERP_RTS:
840                 if (likely(!multi))
841                         return true;
842
843         default:
844                 break;
845         }
846
847         return false;
848 }
849
850 static bool carl9170_tx_cts_check(struct ar9170 *ar,
851                                   struct ieee80211_tx_rate *rate)
852 {
853         switch (ar->erp_mode) {
854         case CARL9170_ERP_AUTO:
855         case CARL9170_ERP_MAC80211:
856                 if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
857                         break;
858
859         case CARL9170_ERP_CTS:
860                 return true;
861
862         default:
863                 break;
864         }
865
866         return false;
867 }
868
869 static int carl9170_tx_prepare(struct ar9170 *ar,
870                                struct ieee80211_sta *sta,
871                                struct sk_buff *skb)
872 {
873         struct ieee80211_hdr *hdr;
874         struct _carl9170_tx_superframe *txc;
875         struct carl9170_vif_info *cvif;
876         struct ieee80211_tx_info *info;
877         struct ieee80211_tx_rate *txrate;
878         struct carl9170_tx_info *arinfo;
879         unsigned int hw_queue;
880         int i;
881         __le16 mac_tmp;
882         u16 len;
883         bool ampdu, no_ack;
884
885         BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
886         BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
887                      CARL9170_TX_SUPERDESC_LEN);
888
889         BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
890                      AR9170_TX_HWDESC_LEN);
891
892         BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
893
894         BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
895                 ((CARL9170_TX_SUPER_MISC_VIF_ID >>
896                  CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
897
898         hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
899
900         hdr = (void *)skb->data;
901         info = IEEE80211_SKB_CB(skb);
902         len = skb->len;
903
904         /*
905          * Note: If the frame was sent through a monitor interface,
906          * the ieee80211_vif pointer can be NULL.
907          */
908         if (likely(info->control.vif))
909                 cvif = (void *) info->control.vif->drv_priv;
910         else
911                 cvif = NULL;
912
913         txc = (void *)skb_push(skb, sizeof(*txc));
914         memset(txc, 0, sizeof(*txc));
915
916         SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
917
918         if (likely(cvif))
919                 SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
920
921         if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
922                 txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
923
924         if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
925                 txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
926
927         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
928                 txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
929
930         mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
931                               AR9170_TX_MAC_BACKOFF);
932         mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
933                                AR9170_TX_MAC_QOS);
934
935         no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
936         if (unlikely(no_ack))
937                 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
938
939         if (info->control.hw_key) {
940                 len += info->control.hw_key->icv_len;
941
942                 switch (info->control.hw_key->cipher) {
943                 case WLAN_CIPHER_SUITE_WEP40:
944                 case WLAN_CIPHER_SUITE_WEP104:
945                 case WLAN_CIPHER_SUITE_TKIP:
946                         mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
947                         break;
948                 case WLAN_CIPHER_SUITE_CCMP:
949                         mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
950                         break;
951                 default:
952                         WARN_ON(1);
953                         goto err_out;
954                 }
955         }
956
957         ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
958         if (ampdu) {
959                 unsigned int density, factor;
960
961                 if (unlikely(!sta || !cvif))
962                         goto err_out;
963
964                 factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
965                 density = sta->ht_cap.ampdu_density;
966
967                 if (density) {
968                         /*
969                          * Watch out!
970                          *
971                          * Otus uses slightly different density values than
972                          * those from the 802.11n spec.
973                          */
974
975                         density = max_t(unsigned int, density + 1, 7u);
976                 }
977
978                 SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
979                         txc->s.ampdu_settings, density);
980
981                 SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
982                         txc->s.ampdu_settings, factor);
983         }
984
985         /*
986          * NOTE: For the first rate, the ERP & AMPDU flags are directly
987          * taken from mac_control. For all fallback rate, the firmware
988          * updates the mac_control flags from the rate info field.
989          */
990         for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
991                 __le32 phy_set;
992                 txrate = &info->control.rates[i];
993                 if (txrate->idx < 0)
994                         break;
995
996                 phy_set = carl9170_tx_physet(ar, info, txrate);
997                 if (i == 0) {
998                         /* first rate - part of the hw's frame header */
999                         txc->f.phy_control = phy_set;
1000
1001                         if (ampdu && txrate->flags & IEEE80211_TX_RC_MCS)
1002                                 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
1003                         if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
1004                                 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
1005                         else if (carl9170_tx_cts_check(ar, txrate))
1006                                 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
1007
1008                 } else {
1009                         /* fallback rates are stored in the firmware's
1010                          * retry rate set array.
1011                          */
1012                         txc->s.rr[i - 1] = phy_set;
1013                 }
1014
1015                 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
1016                         txrate->count);
1017
1018                 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
1019                         txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
1020                                 CARL9170_TX_SUPER_RI_ERP_PROT_S);
1021                 else if (carl9170_tx_cts_check(ar, txrate))
1022                         txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
1023                                 CARL9170_TX_SUPER_RI_ERP_PROT_S);
1024
1025                 if (ampdu && (txrate->flags & IEEE80211_TX_RC_MCS))
1026                         txc->s.ri[i] |= CARL9170_TX_SUPER_RI_AMPDU;
1027         }
1028
1029         txc->s.len = cpu_to_le16(skb->len);
1030         txc->f.length = cpu_to_le16(len + FCS_LEN);
1031         txc->f.mac_control = mac_tmp;
1032
1033         arinfo = (void *)info->rate_driver_data;
1034         arinfo->timeout = jiffies;
1035         arinfo->ar = ar;
1036         kref_init(&arinfo->ref);
1037         return 0;
1038
1039 err_out:
1040         skb_pull(skb, sizeof(*txc));
1041         return -EINVAL;
1042 }
1043
1044 static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
1045 {
1046         struct _carl9170_tx_superframe *super;
1047
1048         super = (void *) skb->data;
1049         super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
1050 }
1051
1052 static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
1053 {
1054         struct _carl9170_tx_superframe *super;
1055         int tmp;
1056
1057         super = (void *) skb->data;
1058
1059         tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
1060                 CARL9170_TX_SUPER_AMPDU_DENSITY_S;
1061
1062         /*
1063          * If you haven't noticed carl9170_tx_prepare has already filled
1064          * in all ampdu spacing & factor parameters.
1065          * Now it's the time to check whenever the settings have to be
1066          * updated by the firmware, or if everything is still the same.
1067          *
1068          * There's no sane way to handle different density values with
1069          * this hardware, so we may as well just do the compare in the
1070          * driver.
1071          */
1072
1073         if (tmp != ar->current_density) {
1074                 ar->current_density = tmp;
1075                 super->s.ampdu_settings |=
1076                         CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
1077         }
1078
1079         tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
1080                 CARL9170_TX_SUPER_AMPDU_FACTOR_S;
1081
1082         if (tmp != ar->current_factor) {
1083                 ar->current_factor = tmp;
1084                 super->s.ampdu_settings |=
1085                         CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
1086         }
1087 }
1088
1089 static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
1090                                    struct sk_buff *_src)
1091 {
1092         struct _carl9170_tx_superframe *dest, *src;
1093
1094         dest = (void *) _dest->data;
1095         src = (void *) _src->data;
1096
1097         /*
1098          * The mac80211 rate control algorithm expects that all MPDUs in
1099          * an AMPDU share the same tx vectors.
1100          * This is not really obvious right now, because the hardware
1101          * does the AMPDU setup according to its own rulebook.
1102          * Our nicely assembled, strictly monotonic increasing mpdu
1103          * chains will be broken up, mashed back together...
1104          */
1105
1106         return (dest->f.phy_control == src->f.phy_control);
1107 }
1108
1109 static void carl9170_tx_ampdu(struct ar9170 *ar)
1110 {
1111         struct sk_buff_head agg;
1112         struct carl9170_sta_tid *tid_info;
1113         struct sk_buff *skb, *first;
1114         unsigned int i = 0, done_ampdus = 0;
1115         u16 seq, queue, tmpssn;
1116
1117         atomic_inc(&ar->tx_ampdu_scheduler);
1118         ar->tx_ampdu_schedule = false;
1119
1120         if (atomic_read(&ar->tx_ampdu_upload))
1121                 return;
1122
1123         if (!ar->tx_ampdu_list_len)
1124                 return;
1125
1126         __skb_queue_head_init(&agg);
1127
1128         rcu_read_lock();
1129         tid_info = rcu_dereference(ar->tx_ampdu_iter);
1130         if (WARN_ON_ONCE(!tid_info)) {
1131                 rcu_read_unlock();
1132                 return;
1133         }
1134
1135 retry:
1136         list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
1137                 i++;
1138
1139                 if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
1140                         continue;
1141
1142                 queue = TID_TO_WME_AC(tid_info->tid);
1143
1144                 spin_lock_bh(&tid_info->lock);
1145                 if (tid_info->state != CARL9170_TID_STATE_XMIT)
1146                         goto processed;
1147
1148                 tid_info->counter++;
1149                 first = skb_peek(&tid_info->queue);
1150                 tmpssn = carl9170_get_seq(first);
1151                 seq = tid_info->snx;
1152
1153                 if (unlikely(tmpssn != seq)) {
1154                         tid_info->state = CARL9170_TID_STATE_IDLE;
1155
1156                         goto processed;
1157                 }
1158
1159                 while ((skb = skb_peek(&tid_info->queue))) {
1160                         /* strict 0, 1, ..., n - 1, n frame sequence order */
1161                         if (unlikely(carl9170_get_seq(skb) != seq))
1162                                 break;
1163
1164                         /* don't upload more than AMPDU FACTOR allows. */
1165                         if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
1166                             (tid_info->max - 1)))
1167                                 break;
1168
1169                         if (!carl9170_tx_rate_check(ar, skb, first))
1170                                 break;
1171
1172                         atomic_inc(&ar->tx_ampdu_upload);
1173                         tid_info->snx = seq = SEQ_NEXT(seq);
1174                         __skb_unlink(skb, &tid_info->queue);
1175
1176                         __skb_queue_tail(&agg, skb);
1177
1178                         if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
1179                                 break;
1180                 }
1181
1182                 if (skb_queue_empty(&tid_info->queue) ||
1183                     carl9170_get_seq(skb_peek(&tid_info->queue)) !=
1184                     tid_info->snx) {
1185                         /*
1186                          * stop TID, if A-MPDU frames are still missing,
1187                          * or whenever the queue is empty.
1188                          */
1189
1190                         tid_info->state = CARL9170_TID_STATE_IDLE;
1191                 }
1192                 done_ampdus++;
1193
1194 processed:
1195                 spin_unlock_bh(&tid_info->lock);
1196
1197                 if (skb_queue_empty(&agg))
1198                         continue;
1199
1200                 /* apply ampdu spacing & factor settings */
1201                 carl9170_set_ampdu_params(ar, skb_peek(&agg));
1202
1203                 /* set aggregation push bit */
1204                 carl9170_set_immba(ar, skb_peek_tail(&agg));
1205
1206                 spin_lock_bh(&ar->tx_pending[queue].lock);
1207                 skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
1208                 spin_unlock_bh(&ar->tx_pending[queue].lock);
1209                 ar->tx_schedule = true;
1210         }
1211         if ((done_ampdus++ == 0) && (i++ == 0))
1212                 goto retry;
1213
1214         rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
1215         rcu_read_unlock();
1216 }
1217
1218 static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
1219                                             struct sk_buff_head *queue)
1220 {
1221         struct sk_buff *skb;
1222         struct ieee80211_tx_info *info;
1223         struct carl9170_tx_info *arinfo;
1224
1225         BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1226
1227         spin_lock_bh(&queue->lock);
1228         skb = skb_peek(queue);
1229         if (unlikely(!skb))
1230                 goto err_unlock;
1231
1232         if (carl9170_alloc_dev_space(ar, skb))
1233                 goto err_unlock;
1234
1235         __skb_unlink(skb, queue);
1236         spin_unlock_bh(&queue->lock);
1237
1238         info = IEEE80211_SKB_CB(skb);
1239         arinfo = (void *) info->rate_driver_data;
1240
1241         arinfo->timeout = jiffies;
1242         return skb;
1243
1244 err_unlock:
1245         spin_unlock_bh(&queue->lock);
1246         return NULL;
1247 }
1248
1249 void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
1250 {
1251         struct _carl9170_tx_superframe *super;
1252         uint8_t q = 0;
1253
1254         ar->tx_dropped++;
1255
1256         super = (void *)skb->data;
1257         SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
1258                 ar9170_qmap[carl9170_get_queue(ar, skb)]);
1259         __carl9170_tx_process_status(ar, super->s.cookie, q);
1260 }
1261
1262 static bool carl9170_tx_ps_drop(struct ar9170 *ar, struct sk_buff *skb)
1263 {
1264         struct ieee80211_sta *sta;
1265         struct carl9170_sta_info *sta_info;
1266         struct ieee80211_tx_info *tx_info;
1267
1268         rcu_read_lock();
1269         sta = __carl9170_get_tx_sta(ar, skb);
1270         if (!sta)
1271                 goto out_rcu;
1272
1273         sta_info = (void *) sta->drv_priv;
1274         tx_info = IEEE80211_SKB_CB(skb);
1275
1276         if (unlikely(sta_info->sleeping) &&
1277             !(tx_info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
1278                                 IEEE80211_TX_CTL_CLEAR_PS_FILT))) {
1279                 rcu_read_unlock();
1280
1281                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1282                         atomic_dec(&ar->tx_ampdu_upload);
1283
1284                 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1285                 carl9170_release_dev_space(ar, skb);
1286                 carl9170_tx_status(ar, skb, false);
1287                 return true;
1288         }
1289
1290 out_rcu:
1291         rcu_read_unlock();
1292         return false;
1293 }
1294
1295 static void carl9170_bar_check(struct ar9170 *ar, struct sk_buff *skb)
1296 {
1297         struct _carl9170_tx_superframe *super = (void *) skb->data;
1298         struct ieee80211_bar *bar = (void *) super->frame_data;
1299
1300         if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
1301             skb->len >= sizeof(struct ieee80211_bar)) {
1302                 struct carl9170_bar_list_entry *entry;
1303                 unsigned int queue = skb_get_queue_mapping(skb);
1304
1305                 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1306                 if (!WARN_ON_ONCE(!entry)) {
1307                         entry->skb = skb;
1308                         spin_lock_bh(&ar->bar_list_lock[queue]);
1309                         list_add_tail_rcu(&entry->list, &ar->bar_list[queue]);
1310                         spin_unlock_bh(&ar->bar_list_lock[queue]);
1311                 }
1312         }
1313 }
1314
1315 static void carl9170_tx(struct ar9170 *ar)
1316 {
1317         struct sk_buff *skb;
1318         unsigned int i, q;
1319         bool schedule_garbagecollector = false;
1320
1321         ar->tx_schedule = false;
1322
1323         if (unlikely(!IS_STARTED(ar)))
1324                 return;
1325
1326         carl9170_usb_handle_tx_err(ar);
1327
1328         for (i = 0; i < ar->hw->queues; i++) {
1329                 while (!skb_queue_empty(&ar->tx_pending[i])) {
1330                         skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
1331                         if (unlikely(!skb))
1332                                 break;
1333
1334                         if (unlikely(carl9170_tx_ps_drop(ar, skb)))
1335                                 continue;
1336
1337                         carl9170_bar_check(ar, skb);
1338
1339                         atomic_inc(&ar->tx_total_pending);
1340
1341                         q = __carl9170_get_queue(ar, i);
1342                         /*
1343                          * NB: tx_status[i] vs. tx_status[q],
1344                          * TODO: Move into pick_skb or alloc_dev_space.
1345                          */
1346                         skb_queue_tail(&ar->tx_status[q], skb);
1347
1348                         /*
1349                          * increase ref count to "2".
1350                          * Ref counting is the easiest way to solve the
1351                          * race between the urb's completion routine:
1352                          *      carl9170_tx_callback
1353                          * and wlan tx status functions:
1354                          *      carl9170_tx_status/janitor.
1355                          */
1356                         carl9170_tx_get_skb(skb);
1357
1358                         carl9170_usb_tx(ar, skb);
1359                         schedule_garbagecollector = true;
1360                 }
1361         }
1362
1363         if (!schedule_garbagecollector)
1364                 return;
1365
1366         ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
1367                 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
1368 }
1369
1370 static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
1371         struct ieee80211_sta *sta, struct sk_buff *skb,
1372         struct ieee80211_tx_info *txinfo)
1373 {
1374         struct carl9170_sta_info *sta_info;
1375         struct carl9170_sta_tid *agg;
1376         struct sk_buff *iter;
1377         u16 tid, seq, qseq, off;
1378         bool run = false;
1379
1380         tid = carl9170_get_tid(skb);
1381         seq = carl9170_get_seq(skb);
1382         sta_info = (void *) sta->drv_priv;
1383
1384         rcu_read_lock();
1385         agg = rcu_dereference(sta_info->agg[tid]);
1386
1387         if (!agg)
1388                 goto err_unlock_rcu;
1389
1390         spin_lock_bh(&agg->lock);
1391         if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
1392                 goto err_unlock;
1393
1394         /* check if sequence is within the BA window */
1395         if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
1396                 goto err_unlock;
1397
1398         if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
1399                 goto err_unlock;
1400
1401         off = SEQ_DIFF(seq, agg->bsn);
1402         if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
1403                 goto err_unlock;
1404
1405         if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
1406                 __skb_queue_tail(&agg->queue, skb);
1407                 agg->hsn = seq;
1408                 goto queued;
1409         }
1410
1411         skb_queue_reverse_walk(&agg->queue, iter) {
1412                 qseq = carl9170_get_seq(iter);
1413
1414                 if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
1415                         __skb_queue_after(&agg->queue, iter, skb);
1416                         goto queued;
1417                 }
1418         }
1419
1420         __skb_queue_head(&agg->queue, skb);
1421 queued:
1422
1423         if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
1424                 if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
1425                         agg->state = CARL9170_TID_STATE_XMIT;
1426                         run = true;
1427                 }
1428         }
1429
1430         spin_unlock_bh(&agg->lock);
1431         rcu_read_unlock();
1432
1433         return run;
1434
1435 err_unlock:
1436         spin_unlock_bh(&agg->lock);
1437
1438 err_unlock_rcu:
1439         rcu_read_unlock();
1440         txinfo->flags &= ~IEEE80211_TX_CTL_AMPDU;
1441         carl9170_tx_status(ar, skb, false);
1442         ar->tx_dropped++;
1443         return false;
1444 }
1445
1446 void carl9170_op_tx(struct ieee80211_hw *hw,
1447                     struct ieee80211_tx_control *control,
1448                     struct sk_buff *skb)
1449 {
1450         struct ar9170 *ar = hw->priv;
1451         struct ieee80211_tx_info *info;
1452         struct ieee80211_sta *sta = control->sta;
1453         bool run;
1454
1455         if (unlikely(!IS_STARTED(ar)))
1456                 goto err_free;
1457
1458         info = IEEE80211_SKB_CB(skb);
1459
1460         if (unlikely(carl9170_tx_prepare(ar, sta, skb)))
1461                 goto err_free;
1462
1463         carl9170_tx_accounting(ar, skb);
1464         /*
1465          * from now on, one has to use carl9170_tx_status to free
1466          * all ressouces which are associated with the frame.
1467          */
1468
1469         if (sta) {
1470                 struct carl9170_sta_info *stai = (void *) sta->drv_priv;
1471                 atomic_inc(&stai->pending_frames);
1472         }
1473
1474         if (info->flags & IEEE80211_TX_CTL_AMPDU) {
1475                 /* to static code analyzers and reviewers:
1476                  * mac80211 guarantees that a valid "sta"
1477                  * reference is present, if a frame is to
1478                  * be part of an ampdu. Hence any extra
1479                  * sta == NULL checks are redundant in this
1480                  * special case.
1481                  */
1482                 run = carl9170_tx_ampdu_queue(ar, sta, skb, info);
1483                 if (run)
1484                         carl9170_tx_ampdu(ar);
1485
1486         } else {
1487                 unsigned int queue = skb_get_queue_mapping(skb);
1488
1489                 skb_queue_tail(&ar->tx_pending[queue], skb);
1490         }
1491
1492         carl9170_tx(ar);
1493         return;
1494
1495 err_free:
1496         ar->tx_dropped++;
1497         ieee80211_free_txskb(ar->hw, skb);
1498 }
1499
1500 void carl9170_tx_scheduler(struct ar9170 *ar)
1501 {
1502
1503         if (ar->tx_ampdu_schedule)
1504                 carl9170_tx_ampdu(ar);
1505
1506         if (ar->tx_schedule)
1507                 carl9170_tx(ar);
1508 }
1509
1510 /* caller has to take rcu_read_lock */
1511 static struct carl9170_vif_info *carl9170_pick_beaconing_vif(struct ar9170 *ar)
1512 {
1513         struct carl9170_vif_info *cvif;
1514         int i = 1;
1515
1516         /* The AR9170 hardware has no fancy beacon queue or some
1517          * other scheduling mechanism. So, the driver has to make
1518          * due by setting the two beacon timers (pretbtt and tbtt)
1519          * once and then swapping the beacon address in the HW's
1520          * register file each time the pretbtt fires.
1521          */
1522
1523         cvif = rcu_dereference(ar->beacon_iter);
1524         if (ar->vifs > 0 && cvif) {
1525                 do {
1526                         list_for_each_entry_continue_rcu(cvif, &ar->vif_list,
1527                                                          list) {
1528                                 if (cvif->active && cvif->enable_beacon)
1529                                         goto out;
1530                         }
1531                 } while (ar->beacon_enabled && i--);
1532         }
1533
1534 out:
1535         rcu_assign_pointer(ar->beacon_iter, cvif);
1536         return cvif;
1537 }
1538
1539 static bool carl9170_tx_beacon_physet(struct ar9170 *ar, struct sk_buff *skb,
1540                                       u32 *ht1, u32 *plcp)
1541 {
1542         struct ieee80211_tx_info *txinfo;
1543         struct ieee80211_tx_rate *rate;
1544         unsigned int power, chains;
1545         bool ht_rate;
1546
1547         txinfo = IEEE80211_SKB_CB(skb);
1548         rate = &txinfo->control.rates[0];
1549         ht_rate = !!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS);
1550         carl9170_tx_rate_tpc_chains(ar, txinfo, rate, plcp, &power, &chains);
1551
1552         *ht1 = AR9170_MAC_BCN_HT1_TX_ANT0;
1553         if (chains == AR9170_TX_PHY_TXCHAIN_2)
1554                 *ht1 |= AR9170_MAC_BCN_HT1_TX_ANT1;
1555         SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL, *ht1, 7);
1556         SET_VAL(AR9170_MAC_BCN_HT1_TPC, *ht1, power);
1557         SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK, *ht1, chains);
1558
1559         if (ht_rate) {
1560                 *ht1 |= AR9170_MAC_BCN_HT1_HT_EN;
1561                 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1562                         *plcp |= AR9170_MAC_BCN_HT2_SGI;
1563
1564                 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1565                         *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_SHARED;
1566                         *plcp |= AR9170_MAC_BCN_HT2_BW40;
1567                 } else if (rate->flags & IEEE80211_TX_RC_DUP_DATA) {
1568                         *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_DUP;
1569                         *plcp |= AR9170_MAC_BCN_HT2_BW40;
1570                 }
1571
1572                 SET_VAL(AR9170_MAC_BCN_HT2_LEN, *plcp, skb->len + FCS_LEN);
1573         } else {
1574                 if (*plcp <= AR9170_TX_PHY_RATE_CCK_11M)
1575                         *plcp |= ((skb->len + FCS_LEN) << (3 + 16)) + 0x0400;
1576                 else
1577                         *plcp |= ((skb->len + FCS_LEN) << 16) + 0x0010;
1578         }
1579
1580         return ht_rate;
1581 }
1582
1583 int carl9170_update_beacon(struct ar9170 *ar, const bool submit)
1584 {
1585         struct sk_buff *skb = NULL;
1586         struct carl9170_vif_info *cvif;
1587         __le32 *data, *old = NULL;
1588         u32 word, ht1, plcp, off, addr, len;
1589         int i = 0, err = 0;
1590         bool ht_rate;
1591
1592         rcu_read_lock();
1593         cvif = carl9170_pick_beaconing_vif(ar);
1594         if (!cvif)
1595                 goto out_unlock;
1596
1597         skb = ieee80211_beacon_get_tim(ar->hw, carl9170_get_vif(cvif),
1598                 NULL, NULL);
1599
1600         if (!skb) {
1601                 err = -ENOMEM;
1602                 goto err_free;
1603         }
1604
1605         spin_lock_bh(&ar->beacon_lock);
1606         data = (__le32 *)skb->data;
1607         if (cvif->beacon)
1608                 old = (__le32 *)cvif->beacon->data;
1609
1610         off = cvif->id * AR9170_MAC_BCN_LENGTH_MAX;
1611         addr = ar->fw.beacon_addr + off;
1612         len = roundup(skb->len + FCS_LEN, 4);
1613
1614         if ((off + len) > ar->fw.beacon_max_len) {
1615                 if (net_ratelimit()) {
1616                         wiphy_err(ar->hw->wiphy, "beacon does not "
1617                                   "fit into device memory!\n");
1618                 }
1619                 err = -EINVAL;
1620                 goto err_unlock;
1621         }
1622
1623         if (len > AR9170_MAC_BCN_LENGTH_MAX) {
1624                 if (net_ratelimit()) {
1625                         wiphy_err(ar->hw->wiphy, "no support for beacons "
1626                                 "bigger than %d (yours:%d).\n",
1627                                  AR9170_MAC_BCN_LENGTH_MAX, len);
1628                 }
1629
1630                 err = -EMSGSIZE;
1631                 goto err_unlock;
1632         }
1633
1634         ht_rate = carl9170_tx_beacon_physet(ar, skb, &ht1, &plcp);
1635
1636         carl9170_async_regwrite_begin(ar);
1637         carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1, ht1);
1638         if (ht_rate)
1639                 carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2, plcp);
1640         else
1641                 carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP, plcp);
1642
1643         for (i = 0; i < DIV_ROUND_UP(skb->len, 4); i++) {
1644                 /*
1645                  * XXX: This accesses beyond skb data for up
1646                  *      to the last 3 bytes!!
1647                  */
1648
1649                 if (old && (data[i] == old[i]))
1650                         continue;
1651
1652                 word = le32_to_cpu(data[i]);
1653                 carl9170_async_regwrite(addr + 4 * i, word);
1654         }
1655         carl9170_async_regwrite_finish();
1656
1657         dev_kfree_skb_any(cvif->beacon);
1658         cvif->beacon = NULL;
1659
1660         err = carl9170_async_regwrite_result();
1661         if (!err)
1662                 cvif->beacon = skb;
1663         spin_unlock_bh(&ar->beacon_lock);
1664         if (err)
1665                 goto err_free;
1666
1667         if (submit) {
1668                 err = carl9170_bcn_ctrl(ar, cvif->id,
1669                                         CARL9170_BCN_CTRL_CAB_TRIGGER,
1670                                         addr, skb->len + FCS_LEN);
1671
1672                 if (err)
1673                         goto err_free;
1674         }
1675 out_unlock:
1676         rcu_read_unlock();
1677         return 0;
1678
1679 err_unlock:
1680         spin_unlock_bh(&ar->beacon_lock);
1681
1682 err_free:
1683         rcu_read_unlock();
1684         dev_kfree_skb_any(skb);
1685         return err;
1686 }