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