blob: 9c0b150d5b8e2a20b3373d91b733362f083df63a [file] [log] [blame]
Christian Lampartera84fab32010-09-06 01:09:20 +02001/*
2 * Atheros CARL9170 driver
3 *
4 * 802.11 xmit & status routines
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
22 *
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
26 *
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 */
39
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/module.h>
43#include <linux/etherdevice.h>
44#include <net/mac80211.h>
45#include "carl9170.h"
46#include "hw.h"
47#include "cmd.h"
48
49static inline unsigned int __carl9170_get_queue(struct ar9170 *ar,
50 unsigned int queue)
51{
52 if (unlikely(modparam_noht)) {
53 return queue;
54 } else {
55 /*
56 * This is just another workaround, until
57 * someone figures out how to get QoS and
58 * AMPDU to play nicely together.
59 */
60
61 return 2; /* AC_BE */
62 }
63}
64
65static inline unsigned int carl9170_get_queue(struct ar9170 *ar,
66 struct sk_buff *skb)
67{
68 return __carl9170_get_queue(ar, skb_get_queue_mapping(skb));
69}
70
71static bool is_mem_full(struct ar9170 *ar)
72{
73 return (DIV_ROUND_UP(IEEE80211_MAX_FRAME_LEN, ar->fw.mem_block_size) >
74 atomic_read(&ar->mem_free_blocks));
75}
76
77static void carl9170_tx_accounting(struct ar9170 *ar, struct sk_buff *skb)
78{
79 int queue, i;
80 bool mem_full;
81
82 atomic_inc(&ar->tx_total_queued);
83
84 queue = skb_get_queue_mapping(skb);
85 spin_lock_bh(&ar->tx_stats_lock);
86
87 /*
88 * The driver has to accept the frame, regardless if the queue is
89 * full to the brim, or not. We have to do the queuing internally,
90 * since mac80211 assumes that a driver which can operate with
91 * aggregated frames does not reject frames for this reason.
92 */
93 ar->tx_stats[queue].len++;
94 ar->tx_stats[queue].count++;
95
96 mem_full = is_mem_full(ar);
97 for (i = 0; i < ar->hw->queues; i++) {
98 if (mem_full || ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
99 ieee80211_stop_queue(ar->hw, i);
100 ar->queue_stop_timeout[i] = jiffies;
101 }
102 }
103
104 spin_unlock_bh(&ar->tx_stats_lock);
105}
106
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200107/* 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
Christian Lampartera84fab32010-09-06 01:09:20 +0200157static void carl9170_tx_accounting_free(struct ar9170 *ar, struct sk_buff *skb)
158{
Christian Lampartera84fab32010-09-06 01:09:20 +0200159 int queue;
160
Christian Lampartera84fab32010-09-06 01:09:20 +0200161 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);
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200186
Christian Lampartera84fab32010-09-06 01:09:20 +0200187 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(
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200280 offsetof(struct ieee80211_tx_info, status.ack_signal) != 20);
Christian Lampartera84fab32010-09-06 01:09:20 +0200281
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200282 memset(&txinfo->status.ack_signal, 0,
Christian Lampartera84fab32010-09-06 01:09:20 +0200283 sizeof(struct ieee80211_tx_info) -
Thomas Huehne3e1a0b2012-07-02 19:46:16 +0200284 offsetof(struct ieee80211_tx_info, status.ack_signal));
Christian Lampartera84fab32010-09-06 01:09:20 +0200285
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) {
Christian Lamparter041fb8f2010-11-06 14:07:10 +0100294 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;
Christian Lamparter94d55d62011-10-21 18:38:56 +0200299 } else if ((txinfo->flags & IEEE80211_TX_STAT_ACK) &&
300 !(txinfo->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)) {
Christian Lampartera84fab32010-09-06 01:09:20 +0200301 /*
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.
Christian Lamparter94d55d62011-10-21 18:38:56 +0200312 *
313 * 4. mac80211 is not particularly interested in
314 * feedback either [CTL_REQ_TX_STATUS not set]
Christian Lampartera84fab32010-09-06 01:09:20 +0200315 */
316
Christian Lamparterfd67a722011-11-19 19:30:20 +0100317 ieee80211_free_txskb(ar->hw, skb);
Christian Lampartera84fab32010-09-06 01:09:20 +0200318 return;
319 } else {
320 /*
Christian Lamparter94d55d62011-10-21 18:38:56 +0200321 * Either the frame transmission has failed or
322 * mac80211 requested tx status.
Christian Lampartera84fab32010-09-06 01:09:20 +0200323 */
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;
Christian Lampartera84fab32010-09-06 01:09:20 +0200384 struct ieee80211_sta *sta;
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200385 struct carl9170_sta_info *sta_info;
Christian Lampartera84fab32010-09-06 01:09:20 +0200386 struct carl9170_sta_tid *tid_info;
Christian Lampartera84fab32010-09-06 01:09:20 +0200387 u8 tid;
388
389 if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
Christian Lamparter041fb8f2010-11-06 14:07:10 +0100390 txinfo->flags & IEEE80211_TX_CTL_INJECTED ||
391 (!(super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_AGGR))))
Christian Lampartera84fab32010-09-06 01:09:20 +0200392 return;
393
Christian Lampartera84fab32010-09-06 01:09:20 +0200394 rcu_read_lock();
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200395 sta = __carl9170_get_tx_sta(ar, skb);
Christian Lampartera84fab32010-09-06 01:09:20 +0200396 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;
Christian Lamparter24047e22011-03-29 13:43:14 +0200412 sta_info->stats[tid].req = false;
Christian Lampartera84fab32010-09-06 01:09:20 +0200413 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
Christian Lamparter24047e22011-03-29 13:43:14 +0200421 if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
422 sta_info->stats[tid].req = true;
423
Christian Lampartera84fab32010-09-06 01:09:20 +0200424 if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
Christian Lamparter041fb8f2010-11-06 14:07:10 +0100425 super->s.rix = sta_info->stats[tid].ampdu_len;
426 super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
Christian Lampartera84fab32010-09-06 01:09:20 +0200427 txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
Christian Lamparter24047e22011-03-29 13:43:14 +0200428 if (sta_info->stats[tid].req)
429 txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
430
Christian Lampartera84fab32010-09-06 01:09:20 +0200431 sta_info->stats[tid].clear = true;
432 }
433 spin_unlock_bh(&tid_info->lock);
434
435out_rcu:
436 rcu_read_unlock();
437}
438
Christian Lamparterc9122c02012-07-07 21:13:59 +0200439static void carl9170_tx_bar_status(struct ar9170 *ar, struct sk_buff *skb,
440 struct ieee80211_tx_info *tx_info)
441{
442 struct _carl9170_tx_superframe *super = (void *) skb->data;
443 struct ieee80211_bar *bar = (void *) super->frame_data;
444
445 /*
446 * Unlike all other frames, the status report for BARs does
447 * not directly come from the hardware as it is incapable of
448 * matching a BA to a previously send BAR.
449 * Instead the RX-path will scan for incoming BAs and set the
450 * IEEE80211_TX_STAT_ACK if it sees one that was likely
451 * caused by a BAR from us.
452 */
453
454 if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
455 !(tx_info->flags & IEEE80211_TX_STAT_ACK)) {
456 struct carl9170_bar_list_entry *entry;
457 int queue = skb_get_queue_mapping(skb);
458
459 rcu_read_lock();
460 list_for_each_entry_rcu(entry, &ar->bar_list[queue], list) {
461 if (entry->skb == skb) {
462 spin_lock_bh(&ar->bar_list_lock[queue]);
463 list_del_rcu(&entry->list);
464 spin_unlock_bh(&ar->bar_list_lock[queue]);
465 kfree_rcu(entry, head);
466 goto out;
467 }
468 }
469
470 WARN(1, "bar not found in %d - ra:%pM ta:%pM c:%x ssn:%x\n",
471 queue, bar->ra, bar->ta, bar->control,
472 bar->start_seq_num);
473out:
474 rcu_read_unlock();
475 }
476}
477
Christian Lampartera84fab32010-09-06 01:09:20 +0200478void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
479 const bool success)
480{
481 struct ieee80211_tx_info *txinfo;
482
483 carl9170_tx_accounting_free(ar, skb);
484
485 txinfo = IEEE80211_SKB_CB(skb);
486
Christian Lamparterc9122c02012-07-07 21:13:59 +0200487 carl9170_tx_bar_status(ar, skb, txinfo);
488
Christian Lampartera84fab32010-09-06 01:09:20 +0200489 if (success)
490 txinfo->flags |= IEEE80211_TX_STAT_ACK;
491 else
492 ar->tx_ack_failures++;
493
494 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
495 carl9170_tx_status_process_ampdu(ar, skb, txinfo);
496
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200497 carl9170_tx_ps_unblock(ar, skb);
Christian Lampartera84fab32010-09-06 01:09:20 +0200498 carl9170_tx_put_skb(skb);
499}
500
501/* This function may be called form any context */
502void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
503{
504 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
505
506 atomic_dec(&ar->tx_total_pending);
507
508 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
509 atomic_dec(&ar->tx_ampdu_upload);
510
511 if (carl9170_tx_put_skb(skb))
512 tasklet_hi_schedule(&ar->usb_tasklet);
513}
514
515static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
516 struct sk_buff_head *queue)
517{
518 struct sk_buff *skb;
519
520 spin_lock_bh(&queue->lock);
521 skb_queue_walk(queue, skb) {
522 struct _carl9170_tx_superframe *txc = (void *) skb->data;
523
524 if (txc->s.cookie != cookie)
525 continue;
526
527 __skb_unlink(skb, queue);
528 spin_unlock_bh(&queue->lock);
529
530 carl9170_release_dev_space(ar, skb);
531 return skb;
532 }
533 spin_unlock_bh(&queue->lock);
534
535 return NULL;
536}
537
538static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
539 unsigned int tries, struct ieee80211_tx_info *txinfo)
540{
541 unsigned int i;
542
543 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
544 if (txinfo->status.rates[i].idx < 0)
545 break;
546
547 if (i == rix) {
548 txinfo->status.rates[i].count = tries;
549 i++;
550 break;
551 }
552 }
553
554 for (; i < IEEE80211_TX_MAX_RATES; i++) {
555 txinfo->status.rates[i].idx = -1;
556 txinfo->status.rates[i].count = 0;
557 }
558}
559
560static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
561{
562 int i;
563 struct sk_buff *skb;
564 struct ieee80211_tx_info *txinfo;
565 struct carl9170_tx_info *arinfo;
566 bool restart = false;
567
568 for (i = 0; i < ar->hw->queues; i++) {
569 spin_lock_bh(&ar->tx_status[i].lock);
570
571 skb = skb_peek(&ar->tx_status[i]);
572
573 if (!skb)
574 goto next;
575
576 txinfo = IEEE80211_SKB_CB(skb);
577 arinfo = (void *) txinfo->rate_driver_data;
578
579 if (time_is_before_jiffies(arinfo->timeout +
580 msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
581 restart = true;
582
583next:
584 spin_unlock_bh(&ar->tx_status[i].lock);
585 }
586
587 if (restart) {
588 /*
589 * At least one queue has been stuck for long enough.
590 * Give the device a kick and hope it gets back to
591 * work.
592 *
593 * possible reasons may include:
594 * - frames got lost/corrupted (bad connection to the device)
595 * - stalled rx processing/usb controller hiccups
596 * - firmware errors/bugs
597 * - every bug you can think of.
598 * - all bugs you can't...
599 * - ...
600 */
601 carl9170_restart(ar, CARL9170_RR_STUCK_TX);
602 }
603}
604
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200605static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
606{
607 struct carl9170_sta_tid *iter;
608 struct sk_buff *skb;
609 struct ieee80211_tx_info *txinfo;
610 struct carl9170_tx_info *arinfo;
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200611 struct ieee80211_sta *sta;
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200612
613 rcu_read_lock();
614 list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
615 if (iter->state < CARL9170_TID_STATE_IDLE)
616 continue;
617
618 spin_lock_bh(&iter->lock);
619 skb = skb_peek(&iter->queue);
620 if (!skb)
621 goto unlock;
622
623 txinfo = IEEE80211_SKB_CB(skb);
624 arinfo = (void *)txinfo->rate_driver_data;
625 if (time_is_after_jiffies(arinfo->timeout +
626 msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
627 goto unlock;
628
Christian Lampartercaf1eae2011-04-24 17:44:19 +0200629 sta = __carl9170_get_tx_sta(ar, skb);
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200630 if (WARN_ON(!sta))
631 goto unlock;
632
633 ieee80211_stop_tx_ba_session(sta, iter->tid);
634unlock:
635 spin_unlock_bh(&iter->lock);
636
637 }
638 rcu_read_unlock();
639}
640
Christian Lampartera84fab32010-09-06 01:09:20 +0200641void carl9170_tx_janitor(struct work_struct *work)
642{
643 struct ar9170 *ar = container_of(work, struct ar9170,
644 tx_janitor.work);
645 if (!IS_STARTED(ar))
646 return;
647
648 ar->tx_janitor_last_run = jiffies;
649
650 carl9170_check_queue_stop_timeout(ar);
Christian Lamparter2a6cef52010-10-29 23:41:16 +0200651 carl9170_tx_ampdu_timeout(ar);
Christian Lampartera84fab32010-09-06 01:09:20 +0200652
653 if (!atomic_read(&ar->tx_total_queued))
654 return;
655
656 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
657 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
658}
659
660static void __carl9170_tx_process_status(struct ar9170 *ar,
661 const uint8_t cookie, const uint8_t info)
662{
663 struct sk_buff *skb;
664 struct ieee80211_tx_info *txinfo;
Christian Lampartera84fab32010-09-06 01:09:20 +0200665 unsigned int r, t, q;
666 bool success = true;
667
668 q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
669
670 skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
671 if (!skb) {
672 /*
673 * We have lost the race to another thread.
674 */
675
676 return ;
677 }
678
679 txinfo = IEEE80211_SKB_CB(skb);
Christian Lampartera84fab32010-09-06 01:09:20 +0200680
681 if (!(info & CARL9170_TX_STATUS_SUCCESS))
682 success = false;
683
684 r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
685 t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
686
687 carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
688 carl9170_tx_status(ar, skb, success);
689}
690
691void carl9170_tx_process_status(struct ar9170 *ar,
692 const struct carl9170_rsp *cmd)
693{
694 unsigned int i;
695
696 for (i = 0; i < cmd->hdr.ext; i++) {
697 if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
698 print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
699 (void *) cmd, cmd->hdr.len + 4);
700 break;
701 }
702
703 __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
704 cmd->_tx_status[i].info);
705 }
706}
707
Christian Lamparter58296562011-07-16 17:30:19 +0200708static void carl9170_tx_rate_tpc_chains(struct ar9170 *ar,
709 struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate,
710 unsigned int *phyrate, unsigned int *tpc, unsigned int *chains)
711{
712 struct ieee80211_rate *rate = NULL;
713 u8 *txpower;
714 unsigned int idx;
715
716 idx = txrate->idx;
717 *tpc = 0;
718 *phyrate = 0;
719
720 if (txrate->flags & IEEE80211_TX_RC_MCS) {
721 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
722 /* +1 dBm for HT40 */
723 *tpc += 2;
724
725 if (info->band == IEEE80211_BAND_2GHZ)
726 txpower = ar->power_2G_ht40;
727 else
728 txpower = ar->power_5G_ht40;
729 } else {
730 if (info->band == IEEE80211_BAND_2GHZ)
731 txpower = ar->power_2G_ht20;
732 else
733 txpower = ar->power_5G_ht20;
734 }
735
736 *phyrate = txrate->idx;
737 *tpc += txpower[idx & 7];
738 } else {
739 if (info->band == IEEE80211_BAND_2GHZ) {
740 if (idx < 4)
741 txpower = ar->power_2G_cck;
742 else
743 txpower = ar->power_2G_ofdm;
744 } else {
745 txpower = ar->power_5G_leg;
746 idx += 4;
747 }
748
749 rate = &__carl9170_ratetable[idx];
750 *tpc += txpower[(rate->hw_value & 0x30) >> 4];
751 *phyrate = rate->hw_value & 0xf;
752 }
753
754 if (ar->eeprom.tx_mask == 1) {
755 *chains = AR9170_TX_PHY_TXCHAIN_1;
756 } else {
757 if (!(txrate->flags & IEEE80211_TX_RC_MCS) &&
758 rate && rate->bitrate >= 360)
759 *chains = AR9170_TX_PHY_TXCHAIN_1;
760 else
761 *chains = AR9170_TX_PHY_TXCHAIN_2;
762 }
Christian Lamparter67e43de2012-01-21 16:59:10 +0100763
764 *tpc = min_t(unsigned int, *tpc, ar->hw->conf.power_level * 2);
Christian Lamparter58296562011-07-16 17:30:19 +0200765}
766
Christian Lampartera84fab32010-09-06 01:09:20 +0200767static __le32 carl9170_tx_physet(struct ar9170 *ar,
768 struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
769{
Christian Lamparter58296562011-07-16 17:30:19 +0200770 unsigned int power = 0, chains = 0, phyrate = 0;
Christian Lampartera84fab32010-09-06 01:09:20 +0200771 __le32 tmp;
772
773 tmp = cpu_to_le32(0);
774
775 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
776 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
777 AR9170_TX_PHY_BW_S);
778 /* this works because 40 MHz is 2 and dup is 3 */
779 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
780 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
781 AR9170_TX_PHY_BW_S);
782
783 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
784 tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
785
786 if (txrate->flags & IEEE80211_TX_RC_MCS) {
Christian Lamparter58296562011-07-16 17:30:19 +0200787 SET_VAL(AR9170_TX_PHY_MCS, phyrate, txrate->idx);
Christian Lampartera84fab32010-09-06 01:09:20 +0200788
789 /* heavy clip control */
Christian Lamparter58296562011-07-16 17:30:19 +0200790 tmp |= cpu_to_le32((txrate->idx & 0x7) <<
Christian Lampartera84fab32010-09-06 01:09:20 +0200791 AR9170_TX_PHY_TX_HEAVY_CLIP_S);
792
Christian Lampartera84fab32010-09-06 01:09:20 +0200793 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
794
795 /*
796 * green field preamble does not work.
797 *
798 * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
799 * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
800 */
801 } else {
Christian Lamparter58296562011-07-16 17:30:19 +0200802 if (info->band == IEEE80211_BAND_2GHZ) {
803 if (txrate->idx <= AR9170_TX_PHY_RATE_CCK_11M)
804 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_CCK);
805 else
806 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
Christian Lampartera84fab32010-09-06 01:09:20 +0200807 } else {
Christian Lamparter58296562011-07-16 17:30:19 +0200808 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
Christian Lampartera84fab32010-09-06 01:09:20 +0200809 }
810
Christian Lampartera84fab32010-09-06 01:09:20 +0200811 /*
812 * short preamble seems to be broken too.
813 *
814 * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
815 * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
816 */
817 }
Christian Lamparter58296562011-07-16 17:30:19 +0200818 carl9170_tx_rate_tpc_chains(ar, info, txrate,
819 &phyrate, &power, &chains);
Christian Lampartera84fab32010-09-06 01:09:20 +0200820
Christian Lamparter58296562011-07-16 17:30:19 +0200821 tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_MCS, phyrate));
822 tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TX_PWR, power));
823 tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TXCHAIN, chains));
Christian Lampartera84fab32010-09-06 01:09:20 +0200824 return tmp;
825}
826
827static bool carl9170_tx_rts_check(struct ar9170 *ar,
828 struct ieee80211_tx_rate *rate,
829 bool ampdu, bool multi)
830{
831 switch (ar->erp_mode) {
832 case CARL9170_ERP_AUTO:
833 if (ampdu)
834 break;
835
836 case CARL9170_ERP_MAC80211:
837 if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
838 break;
839
840 case CARL9170_ERP_RTS:
841 if (likely(!multi))
842 return true;
843
844 default:
845 break;
846 }
847
848 return false;
849}
850
851static bool carl9170_tx_cts_check(struct ar9170 *ar,
852 struct ieee80211_tx_rate *rate)
853{
854 switch (ar->erp_mode) {
855 case CARL9170_ERP_AUTO:
856 case CARL9170_ERP_MAC80211:
857 if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
858 break;
859
860 case CARL9170_ERP_CTS:
861 return true;
862
863 default:
864 break;
865 }
866
867 return false;
868}
869
Thomas Huehn36323f82012-07-23 21:33:42 +0200870static int carl9170_tx_prepare(struct ar9170 *ar,
871 struct ieee80211_sta *sta,
872 struct sk_buff *skb)
Christian Lampartera84fab32010-09-06 01:09:20 +0200873{
874 struct ieee80211_hdr *hdr;
875 struct _carl9170_tx_superframe *txc;
876 struct carl9170_vif_info *cvif;
877 struct ieee80211_tx_info *info;
878 struct ieee80211_tx_rate *txrate;
Christian Lampartera84fab32010-09-06 01:09:20 +0200879 struct carl9170_tx_info *arinfo;
880 unsigned int hw_queue;
881 int i;
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200882 __le16 mac_tmp;
883 u16 len;
Christian Lampartera84fab32010-09-06 01:09:20 +0200884 bool ampdu, no_ack;
885
886 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
887 BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
888 CARL9170_TX_SUPERDESC_LEN);
889
890 BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
891 AR9170_TX_HWDESC_LEN);
892
893 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
894
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200895 BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
896 ((CARL9170_TX_SUPER_MISC_VIF_ID >>
897 CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
898
Christian Lampartera84fab32010-09-06 01:09:20 +0200899 hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
900
901 hdr = (void *)skb->data;
902 info = IEEE80211_SKB_CB(skb);
903 len = skb->len;
904
905 /*
906 * Note: If the frame was sent through a monitor interface,
907 * the ieee80211_vif pointer can be NULL.
908 */
909 if (likely(info->control.vif))
910 cvif = (void *) info->control.vif->drv_priv;
911 else
912 cvif = NULL;
913
Christian Lampartera84fab32010-09-06 01:09:20 +0200914 txc = (void *)skb_push(skb, sizeof(*txc));
915 memset(txc, 0, sizeof(*txc));
916
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200917 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
918
919 if (likely(cvif))
920 SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
921
922 if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
923 txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
924
Christian Lamparteraa324522011-01-23 00:18:28 +0100925 if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
926 txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
927
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200928 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
929 txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
930
931 mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
932 AR9170_TX_MAC_BACKOFF);
Christian Lamparter5c5e1382010-11-26 23:29:23 +0100933 mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200934 AR9170_TX_MAC_QOS);
935
Christian Lampartera84fab32010-09-06 01:09:20 +0200936 no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200937 if (unlikely(no_ack))
938 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
Christian Lampartera84fab32010-09-06 01:09:20 +0200939
940 if (info->control.hw_key) {
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200941 len += info->control.hw_key->icv_len;
Christian Lampartera84fab32010-09-06 01:09:20 +0200942
943 switch (info->control.hw_key->cipher) {
944 case WLAN_CIPHER_SUITE_WEP40:
945 case WLAN_CIPHER_SUITE_WEP104:
946 case WLAN_CIPHER_SUITE_TKIP:
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200947 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
Christian Lampartera84fab32010-09-06 01:09:20 +0200948 break;
949 case WLAN_CIPHER_SUITE_CCMP:
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200950 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
Christian Lampartera84fab32010-09-06 01:09:20 +0200951 break;
952 default:
953 WARN_ON(1);
954 goto err_out;
955 }
956 }
957
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200958 ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
959 if (ampdu) {
960 unsigned int density, factor;
Christian Lampartera84fab32010-09-06 01:09:20 +0200961
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200962 if (unlikely(!sta || !cvif))
963 goto err_out;
Christian Lampartera84fab32010-09-06 01:09:20 +0200964
Christian Lamparter041fb8f2010-11-06 14:07:10 +0100965 factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
966 density = sta->ht_cap.ampdu_density;
Christian Lampartera84fab32010-09-06 01:09:20 +0200967
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200968 if (density) {
969 /*
970 * Watch out!
971 *
972 * Otus uses slightly different density values than
973 * those from the 802.11n spec.
974 */
Christian Lampartera84fab32010-09-06 01:09:20 +0200975
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200976 density = max_t(unsigned int, density + 1, 7u);
977 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200978
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200979 SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
980 txc->s.ampdu_settings, density);
Christian Lampartera84fab32010-09-06 01:09:20 +0200981
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200982 SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
983 txc->s.ampdu_settings, factor);
Christian Lampartera84fab32010-09-06 01:09:20 +0200984
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200985 for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
Christian Lampartera84fab32010-09-06 01:09:20 +0200986 txrate = &info->control.rates[i];
Christian Lamparter4bd437e2010-09-27 01:36:38 +0200987 if (txrate->idx >= 0) {
988 txc->s.ri[i] =
989 CARL9170_TX_SUPER_RI_AMPDU;
990
991 if (WARN_ON(!(txrate->flags &
992 IEEE80211_TX_RC_MCS))) {
993 /*
994 * Not sure if it's even possible
995 * to aggregate non-ht rates with
996 * this HW.
997 */
998 goto err_out;
999 }
Christian Lampartera84fab32010-09-06 01:09:20 +02001000 continue;
Christian Lamparter4bd437e2010-09-27 01:36:38 +02001001 }
Christian Lampartera84fab32010-09-06 01:09:20 +02001002
1003 txrate->idx = 0;
1004 txrate->count = ar->hw->max_rate_tries;
1005 }
Christian Lamparter4bd437e2010-09-27 01:36:38 +02001006
1007 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
Christian Lampartera84fab32010-09-06 01:09:20 +02001008 }
1009
1010 /*
1011 * NOTE: For the first rate, the ERP & AMPDU flags are directly
1012 * taken from mac_control. For all fallback rate, the firmware
1013 * updates the mac_control flags from the rate info field.
1014 */
1015 for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
1016 txrate = &info->control.rates[i];
1017 if (txrate->idx < 0)
1018 break;
1019
1020 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
1021 txrate->count);
1022
1023 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
1024 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
1025 CARL9170_TX_SUPER_RI_ERP_PROT_S);
1026 else if (carl9170_tx_cts_check(ar, txrate))
1027 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
1028 CARL9170_TX_SUPER_RI_ERP_PROT_S);
1029
Christian Lampartera84fab32010-09-06 01:09:20 +02001030 txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
1031 }
1032
Christian Lamparter4bd437e2010-09-27 01:36:38 +02001033 txrate = &info->control.rates[0];
1034 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
Christian Lampartera84fab32010-09-06 01:09:20 +02001035
Christian Lamparter4bd437e2010-09-27 01:36:38 +02001036 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
1037 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
1038 else if (carl9170_tx_cts_check(ar, txrate))
1039 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
Christian Lampartera84fab32010-09-06 01:09:20 +02001040
Christian Lamparter4bd437e2010-09-27 01:36:38 +02001041 txc->s.len = cpu_to_le16(skb->len);
1042 txc->f.length = cpu_to_le16(len + FCS_LEN);
1043 txc->f.mac_control = mac_tmp;
1044 txc->f.phy_control = carl9170_tx_physet(ar, info, txrate);
Christian Lampartera84fab32010-09-06 01:09:20 +02001045
1046 arinfo = (void *)info->rate_driver_data;
1047 arinfo->timeout = jiffies;
1048 arinfo->ar = ar;
1049 kref_init(&arinfo->ref);
1050 return 0;
1051
1052err_out:
1053 skb_pull(skb, sizeof(*txc));
1054 return -EINVAL;
1055}
1056
1057static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
1058{
1059 struct _carl9170_tx_superframe *super;
1060
1061 super = (void *) skb->data;
1062 super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
1063}
1064
1065static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
1066{
1067 struct _carl9170_tx_superframe *super;
1068 int tmp;
1069
1070 super = (void *) skb->data;
1071
1072 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
1073 CARL9170_TX_SUPER_AMPDU_DENSITY_S;
1074
1075 /*
1076 * If you haven't noticed carl9170_tx_prepare has already filled
1077 * in all ampdu spacing & factor parameters.
1078 * Now it's the time to check whenever the settings have to be
1079 * updated by the firmware, or if everything is still the same.
1080 *
1081 * There's no sane way to handle different density values with
1082 * this hardware, so we may as well just do the compare in the
1083 * driver.
1084 */
1085
1086 if (tmp != ar->current_density) {
1087 ar->current_density = tmp;
1088 super->s.ampdu_settings |=
1089 CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
1090 }
1091
1092 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
1093 CARL9170_TX_SUPER_AMPDU_FACTOR_S;
1094
1095 if (tmp != ar->current_factor) {
1096 ar->current_factor = tmp;
1097 super->s.ampdu_settings |=
1098 CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
1099 }
1100}
1101
1102static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
1103 struct sk_buff *_src)
1104{
1105 struct _carl9170_tx_superframe *dest, *src;
1106
1107 dest = (void *) _dest->data;
1108 src = (void *) _src->data;
1109
1110 /*
1111 * The mac80211 rate control algorithm expects that all MPDUs in
1112 * an AMPDU share the same tx vectors.
1113 * This is not really obvious right now, because the hardware
1114 * does the AMPDU setup according to its own rulebook.
1115 * Our nicely assembled, strictly monotonic increasing mpdu
1116 * chains will be broken up, mashed back together...
1117 */
1118
1119 return (dest->f.phy_control == src->f.phy_control);
1120}
1121
1122static void carl9170_tx_ampdu(struct ar9170 *ar)
1123{
1124 struct sk_buff_head agg;
1125 struct carl9170_sta_tid *tid_info;
1126 struct sk_buff *skb, *first;
1127 unsigned int i = 0, done_ampdus = 0;
1128 u16 seq, queue, tmpssn;
1129
1130 atomic_inc(&ar->tx_ampdu_scheduler);
1131 ar->tx_ampdu_schedule = false;
1132
1133 if (atomic_read(&ar->tx_ampdu_upload))
1134 return;
1135
1136 if (!ar->tx_ampdu_list_len)
1137 return;
1138
1139 __skb_queue_head_init(&agg);
1140
1141 rcu_read_lock();
1142 tid_info = rcu_dereference(ar->tx_ampdu_iter);
1143 if (WARN_ON_ONCE(!tid_info)) {
1144 rcu_read_unlock();
1145 return;
1146 }
1147
1148retry:
1149 list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
1150 i++;
1151
1152 if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
1153 continue;
1154
1155 queue = TID_TO_WME_AC(tid_info->tid);
1156
1157 spin_lock_bh(&tid_info->lock);
Christian Lamparter042c53f2010-09-26 21:48:31 +02001158 if (tid_info->state != CARL9170_TID_STATE_XMIT)
1159 goto processed;
Christian Lampartera84fab32010-09-06 01:09:20 +02001160
1161 tid_info->counter++;
1162 first = skb_peek(&tid_info->queue);
1163 tmpssn = carl9170_get_seq(first);
1164 seq = tid_info->snx;
1165
1166 if (unlikely(tmpssn != seq)) {
1167 tid_info->state = CARL9170_TID_STATE_IDLE;
1168
1169 goto processed;
1170 }
1171
1172 while ((skb = skb_peek(&tid_info->queue))) {
1173 /* strict 0, 1, ..., n - 1, n frame sequence order */
1174 if (unlikely(carl9170_get_seq(skb) != seq))
1175 break;
1176
1177 /* don't upload more than AMPDU FACTOR allows. */
1178 if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
1179 (tid_info->max - 1)))
1180 break;
1181
1182 if (!carl9170_tx_rate_check(ar, skb, first))
1183 break;
1184
1185 atomic_inc(&ar->tx_ampdu_upload);
1186 tid_info->snx = seq = SEQ_NEXT(seq);
1187 __skb_unlink(skb, &tid_info->queue);
1188
1189 __skb_queue_tail(&agg, skb);
1190
1191 if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
1192 break;
1193 }
1194
1195 if (skb_queue_empty(&tid_info->queue) ||
1196 carl9170_get_seq(skb_peek(&tid_info->queue)) !=
1197 tid_info->snx) {
1198 /*
1199 * stop TID, if A-MPDU frames are still missing,
1200 * or whenever the queue is empty.
1201 */
1202
1203 tid_info->state = CARL9170_TID_STATE_IDLE;
1204 }
1205 done_ampdus++;
1206
1207processed:
1208 spin_unlock_bh(&tid_info->lock);
1209
1210 if (skb_queue_empty(&agg))
1211 continue;
1212
1213 /* apply ampdu spacing & factor settings */
1214 carl9170_set_ampdu_params(ar, skb_peek(&agg));
1215
1216 /* set aggregation push bit */
1217 carl9170_set_immba(ar, skb_peek_tail(&agg));
1218
1219 spin_lock_bh(&ar->tx_pending[queue].lock);
1220 skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
1221 spin_unlock_bh(&ar->tx_pending[queue].lock);
1222 ar->tx_schedule = true;
1223 }
1224 if ((done_ampdus++ == 0) && (i++ == 0))
1225 goto retry;
1226
1227 rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
1228 rcu_read_unlock();
1229}
1230
1231static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
1232 struct sk_buff_head *queue)
1233{
1234 struct sk_buff *skb;
1235 struct ieee80211_tx_info *info;
1236 struct carl9170_tx_info *arinfo;
1237
1238 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1239
1240 spin_lock_bh(&queue->lock);
1241 skb = skb_peek(queue);
1242 if (unlikely(!skb))
1243 goto err_unlock;
1244
1245 if (carl9170_alloc_dev_space(ar, skb))
1246 goto err_unlock;
1247
1248 __skb_unlink(skb, queue);
1249 spin_unlock_bh(&queue->lock);
1250
1251 info = IEEE80211_SKB_CB(skb);
1252 arinfo = (void *) info->rate_driver_data;
1253
1254 arinfo->timeout = jiffies;
Christian Lampartera84fab32010-09-06 01:09:20 +02001255 return skb;
1256
1257err_unlock:
1258 spin_unlock_bh(&queue->lock);
1259 return NULL;
1260}
1261
1262void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
1263{
1264 struct _carl9170_tx_superframe *super;
1265 uint8_t q = 0;
1266
1267 ar->tx_dropped++;
1268
1269 super = (void *)skb->data;
1270 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
1271 ar9170_qmap[carl9170_get_queue(ar, skb)]);
1272 __carl9170_tx_process_status(ar, super->s.cookie, q);
1273}
1274
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001275static bool carl9170_tx_ps_drop(struct ar9170 *ar, struct sk_buff *skb)
1276{
1277 struct ieee80211_sta *sta;
1278 struct carl9170_sta_info *sta_info;
Christian Lamparter9926a6752012-02-25 21:36:36 +01001279 struct ieee80211_tx_info *tx_info;
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001280
1281 rcu_read_lock();
1282 sta = __carl9170_get_tx_sta(ar, skb);
1283 if (!sta)
1284 goto out_rcu;
1285
1286 sta_info = (void *) sta->drv_priv;
Christian Lamparter9926a6752012-02-25 21:36:36 +01001287 tx_info = IEEE80211_SKB_CB(skb);
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001288
Christian Lamparter9926a6752012-02-25 21:36:36 +01001289 if (unlikely(sta_info->sleeping) &&
John W. Linvillec288ec62012-02-29 15:08:33 -05001290 !(tx_info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
Christian Lamparter9926a6752012-02-25 21:36:36 +01001291 IEEE80211_TX_CTL_CLEAR_PS_FILT))) {
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001292 rcu_read_unlock();
1293
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001294 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1295 atomic_dec(&ar->tx_ampdu_upload);
1296
1297 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Nicolas Cavallari992d5252012-02-23 16:53:34 +01001298 carl9170_release_dev_space(ar, skb);
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001299 carl9170_tx_status(ar, skb, false);
1300 return true;
1301 }
1302
1303out_rcu:
1304 rcu_read_unlock();
1305 return false;
1306}
1307
Christian Lamparterc9122c02012-07-07 21:13:59 +02001308static void carl9170_bar_check(struct ar9170 *ar, struct sk_buff *skb)
1309{
1310 struct _carl9170_tx_superframe *super = (void *) skb->data;
1311 struct ieee80211_bar *bar = (void *) super->frame_data;
1312
1313 if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
1314 skb->len >= sizeof(struct ieee80211_bar)) {
1315 struct carl9170_bar_list_entry *entry;
1316 unsigned int queue = skb_get_queue_mapping(skb);
1317
1318 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1319 if (!WARN_ON_ONCE(!entry)) {
1320 entry->skb = skb;
1321 spin_lock_bh(&ar->bar_list_lock[queue]);
1322 list_add_tail_rcu(&entry->list, &ar->bar_list[queue]);
1323 spin_unlock_bh(&ar->bar_list_lock[queue]);
1324 }
1325 }
1326}
1327
Christian Lampartera84fab32010-09-06 01:09:20 +02001328static void carl9170_tx(struct ar9170 *ar)
1329{
1330 struct sk_buff *skb;
1331 unsigned int i, q;
1332 bool schedule_garbagecollector = false;
1333
1334 ar->tx_schedule = false;
1335
1336 if (unlikely(!IS_STARTED(ar)))
1337 return;
1338
1339 carl9170_usb_handle_tx_err(ar);
1340
1341 for (i = 0; i < ar->hw->queues; i++) {
1342 while (!skb_queue_empty(&ar->tx_pending[i])) {
1343 skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
1344 if (unlikely(!skb))
1345 break;
1346
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001347 if (unlikely(carl9170_tx_ps_drop(ar, skb)))
1348 continue;
1349
Christian Lamparterc9122c02012-07-07 21:13:59 +02001350 carl9170_bar_check(ar, skb);
1351
Christian Lampartera84fab32010-09-06 01:09:20 +02001352 atomic_inc(&ar->tx_total_pending);
1353
1354 q = __carl9170_get_queue(ar, i);
1355 /*
1356 * NB: tx_status[i] vs. tx_status[q],
1357 * TODO: Move into pick_skb or alloc_dev_space.
1358 */
1359 skb_queue_tail(&ar->tx_status[q], skb);
1360
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001361 /*
1362 * increase ref count to "2".
1363 * Ref counting is the easiest way to solve the
1364 * race between the urb's completion routine:
1365 * carl9170_tx_callback
1366 * and wlan tx status functions:
1367 * carl9170_tx_status/janitor.
1368 */
1369 carl9170_tx_get_skb(skb);
1370
Christian Lampartera84fab32010-09-06 01:09:20 +02001371 carl9170_usb_tx(ar, skb);
1372 schedule_garbagecollector = true;
1373 }
1374 }
1375
1376 if (!schedule_garbagecollector)
1377 return;
1378
1379 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
1380 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
1381}
1382
1383static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
1384 struct ieee80211_sta *sta, struct sk_buff *skb)
1385{
Christian Lampartera9ab2112010-11-20 16:53:26 +01001386 struct _carl9170_tx_superframe *super = (void *) skb->data;
Christian Lampartera84fab32010-09-06 01:09:20 +02001387 struct carl9170_sta_info *sta_info;
1388 struct carl9170_sta_tid *agg;
1389 struct sk_buff *iter;
Christian Lampartera84fab32010-09-06 01:09:20 +02001390 u16 tid, seq, qseq, off;
1391 bool run = false;
1392
1393 tid = carl9170_get_tid(skb);
1394 seq = carl9170_get_seq(skb);
1395 sta_info = (void *) sta->drv_priv;
1396
1397 rcu_read_lock();
1398 agg = rcu_dereference(sta_info->agg[tid]);
Christian Lampartera84fab32010-09-06 01:09:20 +02001399
1400 if (!agg)
1401 goto err_unlock_rcu;
1402
1403 spin_lock_bh(&agg->lock);
1404 if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
1405 goto err_unlock;
1406
1407 /* check if sequence is within the BA window */
1408 if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
1409 goto err_unlock;
1410
1411 if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
1412 goto err_unlock;
1413
1414 off = SEQ_DIFF(seq, agg->bsn);
1415 if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
1416 goto err_unlock;
1417
1418 if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
1419 __skb_queue_tail(&agg->queue, skb);
1420 agg->hsn = seq;
1421 goto queued;
1422 }
1423
1424 skb_queue_reverse_walk(&agg->queue, iter) {
1425 qseq = carl9170_get_seq(iter);
1426
1427 if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
1428 __skb_queue_after(&agg->queue, iter, skb);
1429 goto queued;
1430 }
1431 }
1432
1433 __skb_queue_head(&agg->queue, skb);
1434queued:
1435
1436 if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
1437 if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
1438 agg->state = CARL9170_TID_STATE_XMIT;
1439 run = true;
1440 }
1441 }
1442
1443 spin_unlock_bh(&agg->lock);
1444 rcu_read_unlock();
1445
1446 return run;
1447
1448err_unlock:
1449 spin_unlock_bh(&agg->lock);
1450
1451err_unlock_rcu:
1452 rcu_read_unlock();
Christian Lamparter041fb8f2010-11-06 14:07:10 +01001453 super->f.mac_control &= ~cpu_to_le16(AR9170_TX_MAC_AGGR);
Christian Lampartera84fab32010-09-06 01:09:20 +02001454 carl9170_tx_status(ar, skb, false);
1455 ar->tx_dropped++;
1456 return false;
1457}
1458
Thomas Huehn36323f82012-07-23 21:33:42 +02001459void carl9170_op_tx(struct ieee80211_hw *hw,
1460 struct ieee80211_tx_control *control,
1461 struct sk_buff *skb)
Christian Lampartera84fab32010-09-06 01:09:20 +02001462{
1463 struct ar9170 *ar = hw->priv;
1464 struct ieee80211_tx_info *info;
Thomas Huehn36323f82012-07-23 21:33:42 +02001465 struct ieee80211_sta *sta = control->sta;
Christian Lampartera84fab32010-09-06 01:09:20 +02001466 bool run;
1467
1468 if (unlikely(!IS_STARTED(ar)))
1469 goto err_free;
1470
1471 info = IEEE80211_SKB_CB(skb);
Christian Lampartera84fab32010-09-06 01:09:20 +02001472
Thomas Huehn36323f82012-07-23 21:33:42 +02001473 if (unlikely(carl9170_tx_prepare(ar, sta, skb)))
Christian Lampartera84fab32010-09-06 01:09:20 +02001474 goto err_free;
1475
1476 carl9170_tx_accounting(ar, skb);
1477 /*
1478 * from now on, one has to use carl9170_tx_status to free
1479 * all ressouces which are associated with the frame.
1480 */
1481
Christian Lampartercaf1eae2011-04-24 17:44:19 +02001482 if (sta) {
1483 struct carl9170_sta_info *stai = (void *) sta->drv_priv;
1484 atomic_inc(&stai->pending_frames);
1485 }
1486
Christian Lampartera84fab32010-09-06 01:09:20 +02001487 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
Christian Lamparter1981e8812012-12-03 18:48:05 +01001488 /* to static code analyzers and reviewers:
1489 * mac80211 guarantees that a valid "sta"
1490 * reference is present, if a frame is to
1491 * be part of an ampdu. Hence any extra
1492 * sta == NULL checks are redundant in this
1493 * special case.
1494 */
Christian Lampartera84fab32010-09-06 01:09:20 +02001495 run = carl9170_tx_ampdu_queue(ar, sta, skb);
1496 if (run)
1497 carl9170_tx_ampdu(ar);
1498
1499 } else {
1500 unsigned int queue = skb_get_queue_mapping(skb);
1501
1502 skb_queue_tail(&ar->tx_pending[queue], skb);
1503 }
1504
1505 carl9170_tx(ar);
Johannes Berg7bb45682011-02-24 14:42:06 +01001506 return;
Christian Lampartera84fab32010-09-06 01:09:20 +02001507
1508err_free:
1509 ar->tx_dropped++;
Christian Lamparterfd67a722011-11-19 19:30:20 +01001510 ieee80211_free_txskb(ar->hw, skb);
Christian Lampartera84fab32010-09-06 01:09:20 +02001511}
1512
1513void carl9170_tx_scheduler(struct ar9170 *ar)
1514{
1515
1516 if (ar->tx_ampdu_schedule)
1517 carl9170_tx_ampdu(ar);
1518
1519 if (ar->tx_schedule)
1520 carl9170_tx(ar);
1521}
Christian Lamparter943c3392011-07-16 17:25:14 +02001522
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001523/* caller has to take rcu_read_lock */
1524static struct carl9170_vif_info *carl9170_pick_beaconing_vif(struct ar9170 *ar)
1525{
1526 struct carl9170_vif_info *cvif;
1527 int i = 1;
1528
1529 /* The AR9170 hardware has no fancy beacon queue or some
1530 * other scheduling mechanism. So, the driver has to make
1531 * due by setting the two beacon timers (pretbtt and tbtt)
1532 * once and then swapping the beacon address in the HW's
1533 * register file each time the pretbtt fires.
1534 */
1535
1536 cvif = rcu_dereference(ar->beacon_iter);
1537 if (ar->vifs > 0 && cvif) {
1538 do {
1539 list_for_each_entry_continue_rcu(cvif, &ar->vif_list,
1540 list) {
1541 if (cvif->active && cvif->enable_beacon)
1542 goto out;
1543 }
1544 } while (ar->beacon_enabled && i--);
1545 }
1546
1547out:
1548 rcu_assign_pointer(ar->beacon_iter, cvif);
1549 return cvif;
1550}
1551
1552static bool carl9170_tx_beacon_physet(struct ar9170 *ar, struct sk_buff *skb,
1553 u32 *ht1, u32 *plcp)
1554{
1555 struct ieee80211_tx_info *txinfo;
1556 struct ieee80211_tx_rate *rate;
1557 unsigned int power, chains;
1558 bool ht_rate;
1559
1560 txinfo = IEEE80211_SKB_CB(skb);
1561 rate = &txinfo->control.rates[0];
1562 ht_rate = !!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS);
1563 carl9170_tx_rate_tpc_chains(ar, txinfo, rate, plcp, &power, &chains);
1564
1565 *ht1 = AR9170_MAC_BCN_HT1_TX_ANT0;
1566 if (chains == AR9170_TX_PHY_TXCHAIN_2)
1567 *ht1 |= AR9170_MAC_BCN_HT1_TX_ANT1;
1568 SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL, *ht1, 7);
1569 SET_VAL(AR9170_MAC_BCN_HT1_TPC, *ht1, power);
1570 SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK, *ht1, chains);
1571
1572 if (ht_rate) {
1573 *ht1 |= AR9170_MAC_BCN_HT1_HT_EN;
1574 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
1575 *plcp |= AR9170_MAC_BCN_HT2_SGI;
1576
1577 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1578 *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_SHARED;
1579 *plcp |= AR9170_MAC_BCN_HT2_BW40;
1580 } else if (rate->flags & IEEE80211_TX_RC_DUP_DATA) {
1581 *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_DUP;
1582 *plcp |= AR9170_MAC_BCN_HT2_BW40;
1583 }
1584
1585 SET_VAL(AR9170_MAC_BCN_HT2_LEN, *plcp, skb->len + FCS_LEN);
1586 } else {
1587 if (*plcp <= AR9170_TX_PHY_RATE_CCK_11M)
1588 *plcp |= ((skb->len + FCS_LEN) << (3 + 16)) + 0x0400;
1589 else
1590 *plcp |= ((skb->len + FCS_LEN) << 16) + 0x0010;
1591 }
1592
1593 return ht_rate;
1594}
1595
Christian Lamparter943c3392011-07-16 17:25:14 +02001596int carl9170_update_beacon(struct ar9170 *ar, const bool submit)
1597{
1598 struct sk_buff *skb = NULL;
1599 struct carl9170_vif_info *cvif;
Christian Lamparter943c3392011-07-16 17:25:14 +02001600 __le32 *data, *old = NULL;
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001601 u32 word, ht1, plcp, off, addr, len;
Christian Lamparter943c3392011-07-16 17:25:14 +02001602 int i = 0, err = 0;
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001603 bool ht_rate;
Christian Lamparter943c3392011-07-16 17:25:14 +02001604
1605 rcu_read_lock();
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001606 cvif = carl9170_pick_beaconing_vif(ar);
1607 if (!cvif)
Christian Lamparter943c3392011-07-16 17:25:14 +02001608 goto out_unlock;
1609
Christian Lamparter943c3392011-07-16 17:25:14 +02001610 skb = ieee80211_beacon_get_tim(ar->hw, carl9170_get_vif(cvif),
1611 NULL, NULL);
1612
1613 if (!skb) {
1614 err = -ENOMEM;
1615 goto err_free;
1616 }
1617
Christian Lamparter943c3392011-07-16 17:25:14 +02001618 spin_lock_bh(&ar->beacon_lock);
1619 data = (__le32 *)skb->data;
1620 if (cvif->beacon)
1621 old = (__le32 *)cvif->beacon->data;
1622
1623 off = cvif->id * AR9170_MAC_BCN_LENGTH_MAX;
1624 addr = ar->fw.beacon_addr + off;
1625 len = roundup(skb->len + FCS_LEN, 4);
1626
1627 if ((off + len) > ar->fw.beacon_max_len) {
1628 if (net_ratelimit()) {
1629 wiphy_err(ar->hw->wiphy, "beacon does not "
1630 "fit into device memory!\n");
1631 }
1632 err = -EINVAL;
1633 goto err_unlock;
1634 }
1635
1636 if (len > AR9170_MAC_BCN_LENGTH_MAX) {
1637 if (net_ratelimit()) {
1638 wiphy_err(ar->hw->wiphy, "no support for beacons "
1639 "bigger than %d (yours:%d).\n",
1640 AR9170_MAC_BCN_LENGTH_MAX, len);
1641 }
1642
1643 err = -EMSGSIZE;
1644 goto err_unlock;
1645 }
1646
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001647 ht_rate = carl9170_tx_beacon_physet(ar, skb, &ht1, &plcp);
Christian Lamparter943c3392011-07-16 17:25:14 +02001648
1649 carl9170_async_regwrite_begin(ar);
Christian Lamparter58296562011-07-16 17:30:19 +02001650 carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1, ht1);
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001651 if (ht_rate)
Christian Lamparter58296562011-07-16 17:30:19 +02001652 carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2, plcp);
Christian Lamparter1f1d96542012-12-22 15:05:26 +01001653 else
1654 carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP, plcp);
Christian Lamparter943c3392011-07-16 17:25:14 +02001655
1656 for (i = 0; i < DIV_ROUND_UP(skb->len, 4); i++) {
1657 /*
1658 * XXX: This accesses beyond skb data for up
1659 * to the last 3 bytes!!
1660 */
1661
1662 if (old && (data[i] == old[i]))
1663 continue;
1664
1665 word = le32_to_cpu(data[i]);
1666 carl9170_async_regwrite(addr + 4 * i, word);
1667 }
1668 carl9170_async_regwrite_finish();
1669
1670 dev_kfree_skb_any(cvif->beacon);
1671 cvif->beacon = NULL;
1672
1673 err = carl9170_async_regwrite_result();
1674 if (!err)
1675 cvif->beacon = skb;
1676 spin_unlock_bh(&ar->beacon_lock);
1677 if (err)
1678 goto err_free;
1679
1680 if (submit) {
1681 err = carl9170_bcn_ctrl(ar, cvif->id,
1682 CARL9170_BCN_CTRL_CAB_TRIGGER,
1683 addr, skb->len + FCS_LEN);
1684
1685 if (err)
1686 goto err_free;
1687 }
1688out_unlock:
1689 rcu_read_unlock();
1690 return 0;
1691
1692err_unlock:
1693 spin_unlock_bh(&ar->beacon_lock);
1694
1695err_free:
1696 rcu_read_unlock();
1697 dev_kfree_skb_any(skb);
1698 return err;
1699}