2 * Atheros CARL9170 driver
4 * 802.11 xmit & status routines
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
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.
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.
40 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <linux/etherdevice.h>
43 #include <net/mac80211.h>
48 static inline unsigned int __carl9170_get_queue(struct ar9170
*ar
,
51 if (unlikely(modparam_noht
)) {
55 * This is just another workaround, until
56 * someone figures out how to get QoS and
57 * AMPDU to play nicely together.
64 static inline unsigned int carl9170_get_queue(struct ar9170
*ar
,
67 return __carl9170_get_queue(ar
, skb_get_queue_mapping(skb
));
70 static bool is_mem_full(struct ar9170
*ar
)
72 return (DIV_ROUND_UP(IEEE80211_MAX_FRAME_LEN
, ar
->fw
.mem_block_size
) >
73 atomic_read(&ar
->mem_free_blocks
));
76 static void carl9170_tx_accounting(struct ar9170
*ar
, struct sk_buff
*skb
)
81 atomic_inc(&ar
->tx_total_queued
);
83 queue
= skb_get_queue_mapping(skb
);
84 spin_lock_bh(&ar
->tx_stats_lock
);
87 * The driver has to accept the frame, regardless if the queue is
88 * full to the brim, or not. We have to do the queuing internally,
89 * since mac80211 assumes that a driver which can operate with
90 * aggregated frames does not reject frames for this reason.
92 ar
->tx_stats
[queue
].len
++;
93 ar
->tx_stats
[queue
].count
++;
95 mem_full
= is_mem_full(ar
);
96 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
97 if (mem_full
|| ar
->tx_stats
[i
].len
>= ar
->tx_stats
[i
].limit
) {
98 ieee80211_stop_queue(ar
->hw
, i
);
99 ar
->queue_stop_timeout
[i
] = jiffies
;
103 spin_unlock_bh(&ar
->tx_stats_lock
);
106 /* needs rcu_read_lock */
107 static struct ieee80211_sta
*__carl9170_get_tx_sta(struct ar9170
*ar
,
110 struct _carl9170_tx_superframe
*super
= (void *) skb
->data
;
111 struct ieee80211_hdr
*hdr
= (void *) super
->frame_data
;
112 struct ieee80211_vif
*vif
;
115 vif_id
= (super
->s
.misc
& CARL9170_TX_SUPER_MISC_VIF_ID
) >>
116 CARL9170_TX_SUPER_MISC_VIF_ID_S
;
118 if (WARN_ON_ONCE(vif_id
>= AR9170_MAX_VIRTUAL_MAC
))
121 vif
= rcu_dereference(ar
->vif_priv
[vif_id
].vif
);
126 * Normally we should use wrappers like ieee80211_get_DA to get
127 * the correct peer ieee80211_sta.
129 * But there is a problem with indirect traffic (broadcasts, or
130 * data which is designated for other stations) in station mode.
131 * The frame will be directed to the AP for distribution and not
132 * to the actual destination.
135 return ieee80211_find_sta(vif
, hdr
->addr1
);
138 static void carl9170_tx_ps_unblock(struct ar9170
*ar
, struct sk_buff
*skb
)
140 struct ieee80211_sta
*sta
;
141 struct carl9170_sta_info
*sta_info
;
144 sta
= __carl9170_get_tx_sta(ar
, skb
);
148 sta_info
= (struct carl9170_sta_info
*) sta
->drv_priv
;
149 if (atomic_dec_return(&sta_info
->pending_frames
) == 0)
150 ieee80211_sta_block_awake(ar
->hw
, sta
, false);
156 static void carl9170_tx_accounting_free(struct ar9170
*ar
, struct sk_buff
*skb
)
160 queue
= skb_get_queue_mapping(skb
);
162 spin_lock_bh(&ar
->tx_stats_lock
);
164 ar
->tx_stats
[queue
].len
--;
166 if (!is_mem_full(ar
)) {
168 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
169 if (ar
->tx_stats
[i
].len
>= CARL9170_NUM_TX_LIMIT_SOFT
)
172 if (ieee80211_queue_stopped(ar
->hw
, i
)) {
175 tmp
= jiffies
- ar
->queue_stop_timeout
[i
];
176 if (tmp
> ar
->max_queue_stop_timeout
[i
])
177 ar
->max_queue_stop_timeout
[i
] = tmp
;
180 ieee80211_wake_queue(ar
->hw
, i
);
184 spin_unlock_bh(&ar
->tx_stats_lock
);
186 if (atomic_dec_and_test(&ar
->tx_total_queued
))
187 complete(&ar
->tx_flush
);
190 static int carl9170_alloc_dev_space(struct ar9170
*ar
, struct sk_buff
*skb
)
192 struct _carl9170_tx_superframe
*super
;
196 atomic_inc(&ar
->mem_allocs
);
198 chunks
= DIV_ROUND_UP(skb
->len
, ar
->fw
.mem_block_size
);
199 if (unlikely(atomic_sub_return(chunks
, &ar
->mem_free_blocks
) < 0)) {
200 atomic_add(chunks
, &ar
->mem_free_blocks
);
204 spin_lock_bh(&ar
->mem_lock
);
205 cookie
= bitmap_find_free_region(ar
->mem_bitmap
, ar
->fw
.mem_blocks
, 0);
206 spin_unlock_bh(&ar
->mem_lock
);
208 if (unlikely(cookie
< 0)) {
209 atomic_add(chunks
, &ar
->mem_free_blocks
);
213 super
= (void *) skb
->data
;
216 * Cookie #0 serves two special purposes:
217 * 1. The firmware might use it generate BlockACK frames
218 * in responds of an incoming BlockAckReqs.
220 * 2. Prevent double-free bugs.
222 super
->s
.cookie
= (u8
) cookie
+ 1;
226 static void carl9170_release_dev_space(struct ar9170
*ar
, struct sk_buff
*skb
)
228 struct _carl9170_tx_superframe
*super
= (void *) skb
->data
;
231 /* make a local copy of the cookie */
232 cookie
= super
->s
.cookie
;
233 /* invalidate cookie */
237 * Do a out-of-bounds check on the cookie:
239 * * cookie "0" is reserved and won't be assigned to any
240 * out-going frame. Internally however, it is used to
241 * mark no longer/un-accounted frames and serves as a
242 * cheap way of preventing frames from being freed
243 * twice by _accident_. NB: There is a tiny race...
245 * * obviously, cookie number is limited by the amount
246 * of available memory blocks, so the number can
247 * never execeed the mem_blocks count.
249 if (WARN_ON_ONCE(cookie
== 0) ||
250 WARN_ON_ONCE(cookie
> ar
->fw
.mem_blocks
))
253 atomic_add(DIV_ROUND_UP(skb
->len
, ar
->fw
.mem_block_size
),
254 &ar
->mem_free_blocks
);
256 spin_lock_bh(&ar
->mem_lock
);
257 bitmap_release_region(ar
->mem_bitmap
, cookie
- 1, 0);
258 spin_unlock_bh(&ar
->mem_lock
);
261 /* Called from any context */
262 static void carl9170_tx_release(struct kref
*ref
)
265 struct carl9170_tx_info
*arinfo
;
266 struct ieee80211_tx_info
*txinfo
;
269 arinfo
= container_of(ref
, struct carl9170_tx_info
, ref
);
270 txinfo
= container_of((void *) arinfo
, struct ieee80211_tx_info
,
272 skb
= container_of((void *) txinfo
, struct sk_buff
, cb
);
275 if (WARN_ON_ONCE(!ar
))
279 * This does not call ieee80211_tx_info_clear_status() because
280 * carl9170_tx_fill_rateinfo() has filled the rate information
281 * before we get to this point.
283 memset(&txinfo
->pad
, 0, sizeof(txinfo
->pad
));
284 memset(&txinfo
->rate_driver_data
, 0, sizeof(txinfo
->rate_driver_data
));
286 if (atomic_read(&ar
->tx_total_queued
))
287 ar
->tx_schedule
= true;
289 if (txinfo
->flags
& IEEE80211_TX_CTL_AMPDU
) {
290 if (!atomic_read(&ar
->tx_ampdu_upload
))
291 ar
->tx_ampdu_schedule
= true;
293 if (txinfo
->flags
& IEEE80211_TX_STAT_AMPDU
) {
294 struct _carl9170_tx_superframe
*super
;
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
)) {
302 * drop redundant tx_status reports:
304 * 1. ampdu_ack_len of the final tx_status does
305 * include the feedback of this particular frame.
307 * 2. tx_status_irqsafe only queues up to 128
308 * tx feedback reports and discards the rest.
310 * 3. minstrel_ht is picky, it only accepts
311 * reports of frames with the TX_STATUS_AMPDU flag.
313 * 4. mac80211 is not particularly interested in
314 * feedback either [CTL_REQ_TX_STATUS not set]
317 ieee80211_free_txskb(ar
->hw
, skb
);
321 * Either the frame transmission has failed or
322 * mac80211 requested tx status.
327 skb_pull(skb
, sizeof(struct _carl9170_tx_superframe
));
328 ieee80211_tx_status_irqsafe(ar
->hw
, skb
);
331 void carl9170_tx_get_skb(struct sk_buff
*skb
)
333 struct carl9170_tx_info
*arinfo
= (void *)
334 (IEEE80211_SKB_CB(skb
))->rate_driver_data
;
335 kref_get(&arinfo
->ref
);
338 int carl9170_tx_put_skb(struct sk_buff
*skb
)
340 struct carl9170_tx_info
*arinfo
= (void *)
341 (IEEE80211_SKB_CB(skb
))->rate_driver_data
;
343 return kref_put(&arinfo
->ref
, carl9170_tx_release
);
346 /* Caller must hold the tid_info->lock & rcu_read_lock */
347 static void carl9170_tx_shift_bm(struct ar9170
*ar
,
348 struct carl9170_sta_tid
*tid_info
, u16 seq
)
352 off
= SEQ_DIFF(seq
, tid_info
->bsn
);
354 if (WARN_ON_ONCE(off
>= CARL9170_BAW_BITS
))
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
363 WARN_ON_ONCE(!test_and_clear_bit(off
, tid_info
->bitmap
));
365 off
= SEQ_DIFF(tid_info
->snx
, tid_info
->bsn
);
366 if (WARN_ON_ONCE(off
>= CARL9170_BAW_BITS
))
369 if (!bitmap_empty(tid_info
->bitmap
, off
))
370 off
= find_first_bit(tid_info
->bitmap
, off
);
372 tid_info
->bsn
+= off
;
373 tid_info
->bsn
&= 0x0fff;
375 bitmap_shift_right(tid_info
->bitmap
, tid_info
->bitmap
,
376 off
, CARL9170_BAW_BITS
);
379 static void carl9170_tx_status_process_ampdu(struct ar9170
*ar
,
380 struct sk_buff
*skb
, struct ieee80211_tx_info
*txinfo
)
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
;
389 if (!(txinfo
->flags
& IEEE80211_TX_CTL_AMPDU
) ||
390 txinfo
->flags
& IEEE80211_TX_CTL_INJECTED
)
394 sta
= __carl9170_get_tx_sta(ar
, skb
);
398 tid
= ieee80211_get_tid(hdr
);
400 sta_info
= (void *) sta
->drv_priv
;
401 tid_info
= rcu_dereference(sta_info
->agg
[tid
]);
405 spin_lock_bh(&tid_info
->lock
);
406 if (likely(tid_info
->state
>= CARL9170_TID_STATE_IDLE
))
407 carl9170_tx_shift_bm(ar
, tid_info
, get_seq_h(hdr
));
409 if (sta_info
->stats
[tid
].clear
) {
410 sta_info
->stats
[tid
].clear
= false;
411 sta_info
->stats
[tid
].req
= false;
412 sta_info
->stats
[tid
].ampdu_len
= 0;
413 sta_info
->stats
[tid
].ampdu_ack_len
= 0;
416 sta_info
->stats
[tid
].ampdu_len
++;
417 if (txinfo
->status
.rates
[0].count
== 1)
418 sta_info
->stats
[tid
].ampdu_ack_len
++;
420 if (!(txinfo
->flags
& IEEE80211_TX_STAT_ACK
))
421 sta_info
->stats
[tid
].req
= true;
423 if (super
->f
.mac_control
& cpu_to_le16(AR9170_TX_MAC_IMM_BA
)) {
424 super
->s
.rix
= sta_info
->stats
[tid
].ampdu_len
;
425 super
->s
.cnt
= sta_info
->stats
[tid
].ampdu_ack_len
;
426 txinfo
->flags
|= IEEE80211_TX_STAT_AMPDU
;
427 if (sta_info
->stats
[tid
].req
)
428 txinfo
->flags
|= IEEE80211_TX_STAT_AMPDU_NO_BACK
;
430 sta_info
->stats
[tid
].clear
= true;
432 spin_unlock_bh(&tid_info
->lock
);
438 static void carl9170_tx_bar_status(struct ar9170
*ar
, struct sk_buff
*skb
,
439 struct ieee80211_tx_info
*tx_info
)
441 struct _carl9170_tx_superframe
*super
= (void *) skb
->data
;
442 struct ieee80211_bar
*bar
= (void *) super
->frame_data
;
445 * Unlike all other frames, the status report for BARs does
446 * not directly come from the hardware as it is incapable of
447 * matching a BA to a previously send BAR.
448 * Instead the RX-path will scan for incoming BAs and set the
449 * IEEE80211_TX_STAT_ACK if it sees one that was likely
450 * caused by a BAR from us.
453 if (unlikely(ieee80211_is_back_req(bar
->frame_control
)) &&
454 !(tx_info
->flags
& IEEE80211_TX_STAT_ACK
)) {
455 struct carl9170_bar_list_entry
*entry
;
456 int queue
= skb_get_queue_mapping(skb
);
459 list_for_each_entry_rcu(entry
, &ar
->bar_list
[queue
], list
) {
460 if (entry
->skb
== skb
) {
461 spin_lock_bh(&ar
->bar_list_lock
[queue
]);
462 list_del_rcu(&entry
->list
);
463 spin_unlock_bh(&ar
->bar_list_lock
[queue
]);
464 kfree_rcu(entry
, head
);
469 WARN(1, "bar not found in %d - ra:%pM ta:%pM c:%x ssn:%x\n",
470 queue
, bar
->ra
, bar
->ta
, bar
->control
,
477 void carl9170_tx_status(struct ar9170
*ar
, struct sk_buff
*skb
,
480 struct ieee80211_tx_info
*txinfo
;
482 carl9170_tx_accounting_free(ar
, skb
);
484 txinfo
= IEEE80211_SKB_CB(skb
);
486 carl9170_tx_bar_status(ar
, skb
, txinfo
);
489 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
491 ar
->tx_ack_failures
++;
493 if (txinfo
->flags
& IEEE80211_TX_CTL_AMPDU
)
494 carl9170_tx_status_process_ampdu(ar
, skb
, txinfo
);
496 carl9170_tx_ps_unblock(ar
, skb
);
497 carl9170_tx_put_skb(skb
);
500 /* This function may be called form any context */
501 void carl9170_tx_callback(struct ar9170
*ar
, struct sk_buff
*skb
)
503 struct ieee80211_tx_info
*txinfo
= IEEE80211_SKB_CB(skb
);
505 atomic_dec(&ar
->tx_total_pending
);
507 if (txinfo
->flags
& IEEE80211_TX_CTL_AMPDU
)
508 atomic_dec(&ar
->tx_ampdu_upload
);
510 if (carl9170_tx_put_skb(skb
))
511 tasklet_hi_schedule(&ar
->usb_tasklet
);
514 static struct sk_buff
*carl9170_get_queued_skb(struct ar9170
*ar
, u8 cookie
,
515 struct sk_buff_head
*queue
)
519 spin_lock_bh(&queue
->lock
);
520 skb_queue_walk(queue
, skb
) {
521 struct _carl9170_tx_superframe
*txc
= (void *) skb
->data
;
523 if (txc
->s
.cookie
!= cookie
)
526 __skb_unlink(skb
, queue
);
527 spin_unlock_bh(&queue
->lock
);
529 carl9170_release_dev_space(ar
, skb
);
532 spin_unlock_bh(&queue
->lock
);
537 static void carl9170_tx_fill_rateinfo(struct ar9170
*ar
, unsigned int rix
,
538 unsigned int tries
, struct ieee80211_tx_info
*txinfo
)
542 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
543 if (txinfo
->status
.rates
[i
].idx
< 0)
547 txinfo
->status
.rates
[i
].count
= tries
;
553 for (; i
< IEEE80211_TX_MAX_RATES
; i
++) {
554 txinfo
->status
.rates
[i
].idx
= -1;
555 txinfo
->status
.rates
[i
].count
= 0;
559 static void carl9170_check_queue_stop_timeout(struct ar9170
*ar
)
563 struct ieee80211_tx_info
*txinfo
;
564 struct carl9170_tx_info
*arinfo
;
565 bool restart
= false;
567 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
568 spin_lock_bh(&ar
->tx_status
[i
].lock
);
570 skb
= skb_peek(&ar
->tx_status
[i
]);
575 txinfo
= IEEE80211_SKB_CB(skb
);
576 arinfo
= (void *) txinfo
->rate_driver_data
;
578 if (time_is_before_jiffies(arinfo
->timeout
+
579 msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT
)) == true)
583 spin_unlock_bh(&ar
->tx_status
[i
].lock
);
588 * At least one queue has been stuck for long enough.
589 * Give the device a kick and hope it gets back to
592 * possible reasons may include:
593 * - frames got lost/corrupted (bad connection to the device)
594 * - stalled rx processing/usb controller hiccups
595 * - firmware errors/bugs
596 * - every bug you can think of.
597 * - all bugs you can't...
600 carl9170_restart(ar
, CARL9170_RR_STUCK_TX
);
604 static void carl9170_tx_ampdu_timeout(struct ar9170
*ar
)
606 struct carl9170_sta_tid
*iter
;
608 struct ieee80211_tx_info
*txinfo
;
609 struct carl9170_tx_info
*arinfo
;
610 struct ieee80211_sta
*sta
;
613 list_for_each_entry_rcu(iter
, &ar
->tx_ampdu_list
, list
) {
614 if (iter
->state
< CARL9170_TID_STATE_IDLE
)
617 spin_lock_bh(&iter
->lock
);
618 skb
= skb_peek(&iter
->queue
);
622 txinfo
= IEEE80211_SKB_CB(skb
);
623 arinfo
= (void *)txinfo
->rate_driver_data
;
624 if (time_is_after_jiffies(arinfo
->timeout
+
625 msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT
)))
632 ieee80211_stop_tx_ba_session(sta
, iter
->tid
);
634 spin_unlock_bh(&iter
->lock
);
640 void carl9170_tx_janitor(struct work_struct
*work
)
642 struct ar9170
*ar
= container_of(work
, struct ar9170
,
647 ar
->tx_janitor_last_run
= jiffies
;
649 carl9170_check_queue_stop_timeout(ar
);
650 carl9170_tx_ampdu_timeout(ar
);
652 if (!atomic_read(&ar
->tx_total_queued
))
655 ieee80211_queue_delayed_work(ar
->hw
, &ar
->tx_janitor
,
656 msecs_to_jiffies(CARL9170_TX_TIMEOUT
));
659 static void __carl9170_tx_process_status(struct ar9170
*ar
,
660 const uint8_t cookie
, const uint8_t info
)
663 struct ieee80211_tx_info
*txinfo
;
664 unsigned int r
, t
, q
;
667 q
= ar9170_qmap(info
& CARL9170_TX_STATUS_QUEUE
);
669 skb
= carl9170_get_queued_skb(ar
, cookie
, &ar
->tx_status
[q
]);
672 * We have lost the race to another thread.
678 txinfo
= IEEE80211_SKB_CB(skb
);
680 if (!(info
& CARL9170_TX_STATUS_SUCCESS
))
683 r
= (info
& CARL9170_TX_STATUS_RIX
) >> CARL9170_TX_STATUS_RIX_S
;
684 t
= (info
& CARL9170_TX_STATUS_TRIES
) >> CARL9170_TX_STATUS_TRIES_S
;
686 carl9170_tx_fill_rateinfo(ar
, r
, t
, txinfo
);
687 carl9170_tx_status(ar
, skb
, success
);
690 void carl9170_tx_process_status(struct ar9170
*ar
,
691 const struct carl9170_rsp
*cmd
)
695 for (i
= 0; i
< cmd
->hdr
.ext
; i
++) {
696 if (WARN_ON(i
> ((cmd
->hdr
.len
/ 2) + 1))) {
697 print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE
,
698 (void *) cmd
, cmd
->hdr
.len
+ 4);
702 __carl9170_tx_process_status(ar
, cmd
->_tx_status
[i
].cookie
,
703 cmd
->_tx_status
[i
].info
);
707 static void carl9170_tx_rate_tpc_chains(struct ar9170
*ar
,
708 struct ieee80211_tx_info
*info
, struct ieee80211_tx_rate
*txrate
,
709 unsigned int *phyrate
, unsigned int *tpc
, unsigned int *chains
)
711 struct ieee80211_rate
*rate
= NULL
;
719 if (txrate
->flags
& IEEE80211_TX_RC_MCS
) {
720 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
) {
721 /* +1 dBm for HT40 */
724 if (info
->band
== NL80211_BAND_2GHZ
)
725 txpower
= ar
->power_2G_ht40
;
727 txpower
= ar
->power_5G_ht40
;
729 if (info
->band
== NL80211_BAND_2GHZ
)
730 txpower
= ar
->power_2G_ht20
;
732 txpower
= ar
->power_5G_ht20
;
735 *phyrate
= txrate
->idx
;
736 *tpc
+= txpower
[idx
& 7];
738 if (info
->band
== NL80211_BAND_2GHZ
) {
740 txpower
= ar
->power_2G_cck
;
742 txpower
= ar
->power_2G_ofdm
;
744 txpower
= ar
->power_5G_leg
;
748 rate
= &__carl9170_ratetable
[idx
];
749 *tpc
+= txpower
[(rate
->hw_value
& 0x30) >> 4];
750 *phyrate
= rate
->hw_value
& 0xf;
753 if (ar
->eeprom
.tx_mask
== 1) {
754 *chains
= AR9170_TX_PHY_TXCHAIN_1
;
756 if (!(txrate
->flags
& IEEE80211_TX_RC_MCS
) &&
757 rate
&& rate
->bitrate
>= 360)
758 *chains
= AR9170_TX_PHY_TXCHAIN_1
;
760 *chains
= AR9170_TX_PHY_TXCHAIN_2
;
763 *tpc
= min_t(unsigned int, *tpc
, ar
->hw
->conf
.power_level
* 2);
766 static __le32
carl9170_tx_physet(struct ar9170
*ar
,
767 struct ieee80211_tx_info
*info
, struct ieee80211_tx_rate
*txrate
)
769 unsigned int power
= 0, chains
= 0, phyrate
= 0;
772 tmp
= cpu_to_le32(0);
774 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
775 tmp
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ
<<
777 /* this works because 40 MHz is 2 and dup is 3 */
778 if (txrate
->flags
& IEEE80211_TX_RC_DUP_DATA
)
779 tmp
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP
<<
782 if (txrate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
783 tmp
|= cpu_to_le32(AR9170_TX_PHY_SHORT_GI
);
785 if (txrate
->flags
& IEEE80211_TX_RC_MCS
) {
786 SET_VAL(AR9170_TX_PHY_MCS
, phyrate
, txrate
->idx
);
788 /* heavy clip control */
789 tmp
|= cpu_to_le32((txrate
->idx
& 0x7) <<
790 AR9170_TX_PHY_TX_HEAVY_CLIP_S
);
792 tmp
|= cpu_to_le32(AR9170_TX_PHY_MOD_HT
);
795 * green field preamble does not work.
797 * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
798 * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
801 if (info
->band
== NL80211_BAND_2GHZ
) {
802 if (txrate
->idx
<= AR9170_TX_PHY_RATE_CCK_11M
)
803 tmp
|= cpu_to_le32(AR9170_TX_PHY_MOD_CCK
);
805 tmp
|= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM
);
807 tmp
|= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM
);
811 * short preamble seems to be broken too.
813 * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
814 * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
817 carl9170_tx_rate_tpc_chains(ar
, info
, txrate
,
818 &phyrate
, &power
, &chains
);
820 tmp
|= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_MCS
, phyrate
));
821 tmp
|= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TX_PWR
, power
));
822 tmp
|= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TXCHAIN
, chains
));
826 static bool carl9170_tx_rts_check(struct ar9170
*ar
,
827 struct ieee80211_tx_rate
*rate
,
828 bool ampdu
, bool multi
)
830 switch (ar
->erp_mode
) {
831 case CARL9170_ERP_AUTO
:
836 case CARL9170_ERP_MAC80211
:
837 if (!(rate
->flags
& IEEE80211_TX_RC_USE_RTS_CTS
))
841 case CARL9170_ERP_RTS
:
853 static bool carl9170_tx_cts_check(struct ar9170
*ar
,
854 struct ieee80211_tx_rate
*rate
)
856 switch (ar
->erp_mode
) {
857 case CARL9170_ERP_AUTO
:
858 case CARL9170_ERP_MAC80211
:
859 if (!(rate
->flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
))
863 case CARL9170_ERP_CTS
:
873 static void carl9170_tx_get_rates(struct ar9170
*ar
,
874 struct ieee80211_vif
*vif
,
875 struct ieee80211_sta
*sta
,
878 struct ieee80211_tx_info
*info
;
880 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES
< CARL9170_TX_MAX_RATES
);
881 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES
> IEEE80211_TX_RATE_TABLE_SIZE
);
883 info
= IEEE80211_SKB_CB(skb
);
885 ieee80211_get_tx_rates(vif
, sta
, skb
,
887 IEEE80211_TX_MAX_RATES
);
890 static void carl9170_tx_apply_rateset(struct ar9170
*ar
,
891 struct ieee80211_tx_info
*sinfo
,
894 struct ieee80211_tx_rate
*txrate
;
895 struct ieee80211_tx_info
*info
;
896 struct _carl9170_tx_superframe
*txc
= (void *) skb
->data
;
901 info
= IEEE80211_SKB_CB(skb
);
902 ampdu
= !!(info
->flags
& IEEE80211_TX_CTL_AMPDU
);
903 no_ack
= !!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
);
905 /* Set the rate control probe flag for all (sub-) frames.
906 * This is because the TX_STATS_AMPDU flag is only set on
907 * the last frame, so it has to be inherited.
909 info
->flags
|= (sinfo
->flags
& IEEE80211_TX_CTL_RATE_CTRL_PROBE
);
911 /* NOTE: For the first rate, the ERP & AMPDU flags are directly
912 * taken from mac_control. For all fallback rate, the firmware
913 * updates the mac_control flags from the rate info field.
915 for (i
= 0; i
< CARL9170_TX_MAX_RATES
; i
++) {
918 txrate
= &sinfo
->control
.rates
[i
];
922 phy_set
= carl9170_tx_physet(ar
, info
, txrate
);
924 __le16 mac_tmp
= cpu_to_le16(0);
926 /* first rate - part of the hw's frame header */
927 txc
->f
.phy_control
= phy_set
;
929 if (ampdu
&& txrate
->flags
& IEEE80211_TX_RC_MCS
)
930 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_AGGR
);
932 if (carl9170_tx_rts_check(ar
, txrate
, ampdu
, no_ack
))
933 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_PROT_RTS
);
934 else if (carl9170_tx_cts_check(ar
, txrate
))
935 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_PROT_CTS
);
937 txc
->f
.mac_control
|= mac_tmp
;
939 /* fallback rates are stored in the firmware's
940 * retry rate set array.
942 txc
->s
.rr
[i
- 1] = phy_set
;
945 SET_VAL(CARL9170_TX_SUPER_RI_TRIES
, txc
->s
.ri
[i
],
948 if (carl9170_tx_rts_check(ar
, txrate
, ampdu
, no_ack
))
949 txc
->s
.ri
[i
] |= (AR9170_TX_MAC_PROT_RTS
<<
950 CARL9170_TX_SUPER_RI_ERP_PROT_S
);
951 else if (carl9170_tx_cts_check(ar
, txrate
))
952 txc
->s
.ri
[i
] |= (AR9170_TX_MAC_PROT_CTS
<<
953 CARL9170_TX_SUPER_RI_ERP_PROT_S
);
955 if (ampdu
&& (txrate
->flags
& IEEE80211_TX_RC_MCS
))
956 txc
->s
.ri
[i
] |= CARL9170_TX_SUPER_RI_AMPDU
;
960 static int carl9170_tx_prepare(struct ar9170
*ar
,
961 struct ieee80211_sta
*sta
,
964 struct ieee80211_hdr
*hdr
;
965 struct _carl9170_tx_superframe
*txc
;
966 struct carl9170_vif_info
*cvif
;
967 struct ieee80211_tx_info
*info
;
968 struct carl9170_tx_info
*arinfo
;
969 unsigned int hw_queue
;
973 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
974 BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc
) !=
975 CARL9170_TX_SUPERDESC_LEN
);
977 BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc
) !=
978 AR9170_TX_HWDESC_LEN
);
980 BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC
>
981 ((CARL9170_TX_SUPER_MISC_VIF_ID
>>
982 CARL9170_TX_SUPER_MISC_VIF_ID_S
) + 1));
984 hw_queue
= ar9170_qmap(carl9170_get_queue(ar
, skb
));
986 hdr
= (void *)skb
->data
;
987 info
= IEEE80211_SKB_CB(skb
);
991 * Note: If the frame was sent through a monitor interface,
992 * the ieee80211_vif pointer can be NULL.
994 if (likely(info
->control
.vif
))
995 cvif
= (void *) info
->control
.vif
->drv_priv
;
999 txc
= skb_push(skb
, sizeof(*txc
));
1000 memset(txc
, 0, sizeof(*txc
));
1002 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE
, txc
->s
.misc
, hw_queue
);
1005 SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID
, txc
->s
.misc
, cvif
->id
);
1007 if (unlikely(info
->flags
& IEEE80211_TX_CTL_SEND_AFTER_DTIM
))
1008 txc
->s
.misc
|= CARL9170_TX_SUPER_MISC_CAB
;
1010 if (unlikely(info
->flags
& IEEE80211_TX_CTL_ASSIGN_SEQ
))
1011 txc
->s
.misc
|= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ
;
1013 if (unlikely(ieee80211_is_probe_resp(hdr
->frame_control
)))
1014 txc
->s
.misc
|= CARL9170_TX_SUPER_MISC_FILL_IN_TSF
;
1016 mac_tmp
= cpu_to_le16(AR9170_TX_MAC_HW_DURATION
|
1017 AR9170_TX_MAC_BACKOFF
);
1018 mac_tmp
|= cpu_to_le16((hw_queue
<< AR9170_TX_MAC_QOS_S
) &
1021 if (unlikely(info
->flags
& IEEE80211_TX_CTL_NO_ACK
))
1022 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_NO_ACK
);
1024 if (info
->control
.hw_key
) {
1025 len
+= info
->control
.hw_key
->icv_len
;
1027 switch (info
->control
.hw_key
->cipher
) {
1028 case WLAN_CIPHER_SUITE_WEP40
:
1029 case WLAN_CIPHER_SUITE_WEP104
:
1030 case WLAN_CIPHER_SUITE_TKIP
:
1031 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4
);
1033 case WLAN_CIPHER_SUITE_CCMP
:
1034 mac_tmp
|= cpu_to_le16(AR9170_TX_MAC_ENCR_AES
);
1042 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
1043 unsigned int density
, factor
;
1045 if (unlikely(!sta
|| !cvif
))
1048 factor
= min_t(unsigned int, 1u,
1049 sta
->deflink
.ht_cap
.ampdu_factor
);
1050 density
= sta
->deflink
.ht_cap
.ampdu_density
;
1056 * Otus uses slightly different density values than
1057 * those from the 802.11n spec.
1060 density
= max_t(unsigned int, density
+ 1, 7u);
1063 SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY
,
1064 txc
->s
.ampdu_settings
, density
);
1066 SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR
,
1067 txc
->s
.ampdu_settings
, factor
);
1070 txc
->s
.len
= cpu_to_le16(skb
->len
);
1071 txc
->f
.length
= cpu_to_le16(len
+ FCS_LEN
);
1072 txc
->f
.mac_control
= mac_tmp
;
1074 arinfo
= (void *)info
->rate_driver_data
;
1075 arinfo
->timeout
= jiffies
;
1077 kref_init(&arinfo
->ref
);
1081 skb_pull(skb
, sizeof(*txc
));
1085 static void carl9170_set_immba(struct ar9170
*ar
, struct sk_buff
*skb
)
1087 struct _carl9170_tx_superframe
*super
;
1089 super
= (void *) skb
->data
;
1090 super
->f
.mac_control
|= cpu_to_le16(AR9170_TX_MAC_IMM_BA
);
1093 static void carl9170_set_ampdu_params(struct ar9170
*ar
, struct sk_buff
*skb
)
1095 struct _carl9170_tx_superframe
*super
;
1098 super
= (void *) skb
->data
;
1100 tmp
= (super
->s
.ampdu_settings
& CARL9170_TX_SUPER_AMPDU_DENSITY
) <<
1101 CARL9170_TX_SUPER_AMPDU_DENSITY_S
;
1104 * If you haven't noticed carl9170_tx_prepare has already filled
1105 * in all ampdu spacing & factor parameters.
1106 * Now it's the time to check whenever the settings have to be
1107 * updated by the firmware, or if everything is still the same.
1109 * There's no sane way to handle different density values with
1110 * this hardware, so we may as well just do the compare in the
1114 if (tmp
!= ar
->current_density
) {
1115 ar
->current_density
= tmp
;
1116 super
->s
.ampdu_settings
|=
1117 CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY
;
1120 tmp
= (super
->s
.ampdu_settings
& CARL9170_TX_SUPER_AMPDU_FACTOR
) <<
1121 CARL9170_TX_SUPER_AMPDU_FACTOR_S
;
1123 if (tmp
!= ar
->current_factor
) {
1124 ar
->current_factor
= tmp
;
1125 super
->s
.ampdu_settings
|=
1126 CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR
;
1130 static void carl9170_tx_ampdu(struct ar9170
*ar
)
1132 struct sk_buff_head agg
;
1133 struct carl9170_sta_tid
*tid_info
;
1134 struct sk_buff
*skb
, *first
;
1135 struct ieee80211_tx_info
*tx_info_first
;
1136 unsigned int i
= 0, done_ampdus
= 0;
1137 u16 seq
, queue
, tmpssn
;
1139 atomic_inc(&ar
->tx_ampdu_scheduler
);
1140 ar
->tx_ampdu_schedule
= false;
1142 if (atomic_read(&ar
->tx_ampdu_upload
))
1145 if (!ar
->tx_ampdu_list_len
)
1148 __skb_queue_head_init(&agg
);
1151 tid_info
= rcu_dereference(ar
->tx_ampdu_iter
);
1152 if (WARN_ON_ONCE(!tid_info
)) {
1158 list_for_each_entry_continue_rcu(tid_info
, &ar
->tx_ampdu_list
, list
) {
1161 if (tid_info
->state
< CARL9170_TID_STATE_PROGRESS
)
1164 queue
= TID_TO_WME_AC(tid_info
->tid
);
1166 spin_lock_bh(&tid_info
->lock
);
1167 if (tid_info
->state
!= CARL9170_TID_STATE_XMIT
)
1170 tid_info
->counter
++;
1171 first
= skb_peek(&tid_info
->queue
);
1172 tmpssn
= carl9170_get_seq(first
);
1173 seq
= tid_info
->snx
;
1175 if (unlikely(tmpssn
!= seq
)) {
1176 tid_info
->state
= CARL9170_TID_STATE_IDLE
;
1181 tx_info_first
= NULL
;
1182 while ((skb
= skb_peek(&tid_info
->queue
))) {
1183 /* strict 0, 1, ..., n - 1, n frame sequence order */
1184 if (unlikely(carl9170_get_seq(skb
) != seq
))
1187 /* don't upload more than AMPDU FACTOR allows. */
1188 if (unlikely(SEQ_DIFF(tid_info
->snx
, tid_info
->bsn
) >=
1189 (tid_info
->max
- 1)))
1192 if (!tx_info_first
) {
1193 carl9170_tx_get_rates(ar
, tid_info
->vif
,
1194 tid_info
->sta
, first
);
1195 tx_info_first
= IEEE80211_SKB_CB(first
);
1198 carl9170_tx_apply_rateset(ar
, tx_info_first
, skb
);
1200 atomic_inc(&ar
->tx_ampdu_upload
);
1201 tid_info
->snx
= seq
= SEQ_NEXT(seq
);
1202 __skb_unlink(skb
, &tid_info
->queue
);
1204 __skb_queue_tail(&agg
, skb
);
1206 if (skb_queue_len(&agg
) >= CARL9170_NUM_TX_AGG_MAX
)
1210 if (skb_queue_empty(&tid_info
->queue
) ||
1211 carl9170_get_seq(skb_peek(&tid_info
->queue
)) !=
1213 /* stop TID, if A-MPDU frames are still missing,
1214 * or whenever the queue is empty.
1217 tid_info
->state
= CARL9170_TID_STATE_IDLE
;
1222 spin_unlock_bh(&tid_info
->lock
);
1224 if (skb_queue_empty(&agg
))
1227 /* apply ampdu spacing & factor settings */
1228 carl9170_set_ampdu_params(ar
, skb_peek(&agg
));
1230 /* set aggregation push bit */
1231 carl9170_set_immba(ar
, skb_peek_tail(&agg
));
1233 spin_lock_bh(&ar
->tx_pending
[queue
].lock
);
1234 skb_queue_splice_tail_init(&agg
, &ar
->tx_pending
[queue
]);
1235 spin_unlock_bh(&ar
->tx_pending
[queue
].lock
);
1236 ar
->tx_schedule
= true;
1238 if ((done_ampdus
++ == 0) && (i
++ == 0))
1241 rcu_assign_pointer(ar
->tx_ampdu_iter
, tid_info
);
1245 static struct sk_buff
*carl9170_tx_pick_skb(struct ar9170
*ar
,
1246 struct sk_buff_head
*queue
)
1248 struct sk_buff
*skb
;
1249 struct ieee80211_tx_info
*info
;
1250 struct carl9170_tx_info
*arinfo
;
1252 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
1254 spin_lock_bh(&queue
->lock
);
1255 skb
= skb_peek(queue
);
1259 if (carl9170_alloc_dev_space(ar
, skb
))
1262 __skb_unlink(skb
, queue
);
1263 spin_unlock_bh(&queue
->lock
);
1265 info
= IEEE80211_SKB_CB(skb
);
1266 arinfo
= (void *) info
->rate_driver_data
;
1268 arinfo
->timeout
= jiffies
;
1272 spin_unlock_bh(&queue
->lock
);
1276 void carl9170_tx_drop(struct ar9170
*ar
, struct sk_buff
*skb
)
1278 struct _carl9170_tx_superframe
*super
;
1283 super
= (void *)skb
->data
;
1284 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE
, q
,
1285 ar9170_qmap(carl9170_get_queue(ar
, skb
)));
1286 __carl9170_tx_process_status(ar
, super
->s
.cookie
, q
);
1289 static bool carl9170_tx_ps_drop(struct ar9170
*ar
, struct sk_buff
*skb
)
1291 struct ieee80211_sta
*sta
;
1292 struct carl9170_sta_info
*sta_info
;
1293 struct ieee80211_tx_info
*tx_info
;
1296 sta
= __carl9170_get_tx_sta(ar
, skb
);
1300 sta_info
= (void *) sta
->drv_priv
;
1301 tx_info
= IEEE80211_SKB_CB(skb
);
1303 if (unlikely(sta_info
->sleeping
) &&
1304 !(tx_info
->flags
& (IEEE80211_TX_CTL_NO_PS_BUFFER
|
1305 IEEE80211_TX_CTL_CLEAR_PS_FILT
))) {
1308 if (tx_info
->flags
& IEEE80211_TX_CTL_AMPDU
)
1309 atomic_dec(&ar
->tx_ampdu_upload
);
1311 tx_info
->flags
|= IEEE80211_TX_STAT_TX_FILTERED
;
1312 carl9170_release_dev_space(ar
, skb
);
1313 carl9170_tx_status(ar
, skb
, false);
1322 static void carl9170_bar_check(struct ar9170
*ar
, struct sk_buff
*skb
)
1324 struct _carl9170_tx_superframe
*super
= (void *) skb
->data
;
1325 struct ieee80211_bar
*bar
= (void *) super
->frame_data
;
1327 if (unlikely(ieee80211_is_back_req(bar
->frame_control
)) &&
1328 skb
->len
>= sizeof(struct ieee80211_bar
)) {
1329 struct carl9170_bar_list_entry
*entry
;
1330 unsigned int queue
= skb_get_queue_mapping(skb
);
1332 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
1333 if (!WARN_ON_ONCE(!entry
)) {
1335 spin_lock_bh(&ar
->bar_list_lock
[queue
]);
1336 list_add_tail_rcu(&entry
->list
, &ar
->bar_list
[queue
]);
1337 spin_unlock_bh(&ar
->bar_list_lock
[queue
]);
1342 static void carl9170_tx(struct ar9170
*ar
)
1344 struct sk_buff
*skb
;
1346 bool schedule_garbagecollector
= false;
1348 ar
->tx_schedule
= false;
1350 if (unlikely(!IS_STARTED(ar
)))
1353 carl9170_usb_handle_tx_err(ar
);
1355 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
1356 while (!skb_queue_empty(&ar
->tx_pending
[i
])) {
1357 skb
= carl9170_tx_pick_skb(ar
, &ar
->tx_pending
[i
]);
1361 if (unlikely(carl9170_tx_ps_drop(ar
, skb
)))
1364 carl9170_bar_check(ar
, skb
);
1366 atomic_inc(&ar
->tx_total_pending
);
1368 q
= __carl9170_get_queue(ar
, i
);
1370 * NB: tx_status[i] vs. tx_status[q],
1371 * TODO: Move into pick_skb or alloc_dev_space.
1373 skb_queue_tail(&ar
->tx_status
[q
], skb
);
1376 * increase ref count to "2".
1377 * Ref counting is the easiest way to solve the
1378 * race between the urb's completion routine:
1379 * carl9170_tx_callback
1380 * and wlan tx status functions:
1381 * carl9170_tx_status/janitor.
1383 carl9170_tx_get_skb(skb
);
1385 carl9170_usb_tx(ar
, skb
);
1386 schedule_garbagecollector
= true;
1390 if (!schedule_garbagecollector
)
1393 ieee80211_queue_delayed_work(ar
->hw
, &ar
->tx_janitor
,
1394 msecs_to_jiffies(CARL9170_TX_TIMEOUT
));
1397 static bool carl9170_tx_ampdu_queue(struct ar9170
*ar
,
1398 struct ieee80211_sta
*sta
, struct sk_buff
*skb
,
1399 struct ieee80211_tx_info
*txinfo
)
1401 struct carl9170_sta_info
*sta_info
;
1402 struct carl9170_sta_tid
*agg
;
1403 struct sk_buff
*iter
;
1404 u16 tid
, seq
, qseq
, off
;
1407 tid
= carl9170_get_tid(skb
);
1408 seq
= carl9170_get_seq(skb
);
1409 sta_info
= (void *) sta
->drv_priv
;
1412 agg
= rcu_dereference(sta_info
->agg
[tid
]);
1415 goto err_unlock_rcu
;
1417 spin_lock_bh(&agg
->lock
);
1418 if (unlikely(agg
->state
< CARL9170_TID_STATE_IDLE
))
1421 /* check if sequence is within the BA window */
1422 if (unlikely(!BAW_WITHIN(agg
->bsn
, CARL9170_BAW_BITS
, seq
)))
1425 if (WARN_ON_ONCE(!BAW_WITHIN(agg
->snx
, CARL9170_BAW_BITS
, seq
)))
1428 off
= SEQ_DIFF(seq
, agg
->bsn
);
1429 if (WARN_ON_ONCE(test_and_set_bit(off
, agg
->bitmap
)))
1432 if (likely(BAW_WITHIN(agg
->hsn
, CARL9170_BAW_BITS
, seq
))) {
1433 __skb_queue_tail(&agg
->queue
, skb
);
1438 skb_queue_reverse_walk(&agg
->queue
, iter
) {
1439 qseq
= carl9170_get_seq(iter
);
1441 if (BAW_WITHIN(qseq
, CARL9170_BAW_BITS
, seq
)) {
1442 __skb_queue_after(&agg
->queue
, iter
, skb
);
1447 __skb_queue_head(&agg
->queue
, skb
);
1450 if (unlikely(agg
->state
!= CARL9170_TID_STATE_XMIT
)) {
1451 if (agg
->snx
== carl9170_get_seq(skb_peek(&agg
->queue
))) {
1452 agg
->state
= CARL9170_TID_STATE_XMIT
;
1457 spin_unlock_bh(&agg
->lock
);
1463 spin_unlock_bh(&agg
->lock
);
1467 txinfo
->flags
&= ~IEEE80211_TX_CTL_AMPDU
;
1468 carl9170_tx_status(ar
, skb
, false);
1473 void carl9170_op_tx(struct ieee80211_hw
*hw
,
1474 struct ieee80211_tx_control
*control
,
1475 struct sk_buff
*skb
)
1477 struct ar9170
*ar
= hw
->priv
;
1478 struct ieee80211_tx_info
*info
;
1479 struct ieee80211_sta
*sta
= control
->sta
;
1480 struct ieee80211_vif
*vif
;
1483 if (unlikely(!IS_STARTED(ar
)))
1486 info
= IEEE80211_SKB_CB(skb
);
1487 vif
= info
->control
.vif
;
1489 if (unlikely(carl9170_tx_prepare(ar
, sta
, skb
)))
1492 carl9170_tx_accounting(ar
, skb
);
1494 * from now on, one has to use carl9170_tx_status to free
1495 * all ressouces which are associated with the frame.
1499 struct carl9170_sta_info
*stai
= (void *) sta
->drv_priv
;
1500 atomic_inc(&stai
->pending_frames
);
1503 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
1504 /* to static code analyzers and reviewers:
1505 * mac80211 guarantees that a valid "sta"
1506 * reference is present, if a frame is to
1507 * be part of an ampdu. Hence any extra
1508 * sta == NULL checks are redundant in this
1511 run
= carl9170_tx_ampdu_queue(ar
, sta
, skb
, info
);
1513 carl9170_tx_ampdu(ar
);
1516 unsigned int queue
= skb_get_queue_mapping(skb
);
1518 carl9170_tx_get_rates(ar
, vif
, sta
, skb
);
1519 carl9170_tx_apply_rateset(ar
, info
, skb
);
1520 skb_queue_tail(&ar
->tx_pending
[queue
], skb
);
1528 ieee80211_free_txskb(ar
->hw
, skb
);
1531 void carl9170_tx_scheduler(struct ar9170
*ar
)
1534 if (ar
->tx_ampdu_schedule
)
1535 carl9170_tx_ampdu(ar
);
1537 if (ar
->tx_schedule
)
1541 /* caller has to take rcu_read_lock */
1542 static struct carl9170_vif_info
*carl9170_pick_beaconing_vif(struct ar9170
*ar
)
1544 struct carl9170_vif_info
*cvif
;
1547 /* The AR9170 hardware has no fancy beacon queue or some
1548 * other scheduling mechanism. So, the driver has to make
1549 * due by setting the two beacon timers (pretbtt and tbtt)
1550 * once and then swapping the beacon address in the HW's
1551 * register file each time the pretbtt fires.
1554 cvif
= rcu_dereference(ar
->beacon_iter
);
1555 if (ar
->vifs
> 0 && cvif
) {
1557 list_for_each_entry_continue_rcu(cvif
, &ar
->vif_list
,
1559 if (cvif
->active
&& cvif
->enable_beacon
)
1562 } while (ar
->beacon_enabled
&& i
--);
1564 /* no entry found in list */
1569 RCU_INIT_POINTER(ar
->beacon_iter
, cvif
);
1573 static bool carl9170_tx_beacon_physet(struct ar9170
*ar
, struct sk_buff
*skb
,
1574 u32
*ht1
, u32
*plcp
)
1576 struct ieee80211_tx_info
*txinfo
;
1577 struct ieee80211_tx_rate
*rate
;
1578 unsigned int power
, chains
;
1581 txinfo
= IEEE80211_SKB_CB(skb
);
1582 rate
= &txinfo
->control
.rates
[0];
1583 ht_rate
= !!(txinfo
->control
.rates
[0].flags
& IEEE80211_TX_RC_MCS
);
1584 carl9170_tx_rate_tpc_chains(ar
, txinfo
, rate
, plcp
, &power
, &chains
);
1586 *ht1
= AR9170_MAC_BCN_HT1_TX_ANT0
;
1587 if (chains
== AR9170_TX_PHY_TXCHAIN_2
)
1588 *ht1
|= AR9170_MAC_BCN_HT1_TX_ANT1
;
1589 SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL
, *ht1
, 7);
1590 SET_VAL(AR9170_MAC_BCN_HT1_TPC
, *ht1
, power
);
1591 SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK
, *ht1
, chains
);
1594 *ht1
|= AR9170_MAC_BCN_HT1_HT_EN
;
1595 if (rate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
1596 *plcp
|= AR9170_MAC_BCN_HT2_SGI
;
1598 if (rate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
) {
1599 *ht1
|= AR9170_MAC_BCN_HT1_BWC_40M_SHARED
;
1600 *plcp
|= AR9170_MAC_BCN_HT2_BW40
;
1601 } else if (rate
->flags
& IEEE80211_TX_RC_DUP_DATA
) {
1602 *ht1
|= AR9170_MAC_BCN_HT1_BWC_40M_DUP
;
1603 *plcp
|= AR9170_MAC_BCN_HT2_BW40
;
1606 SET_VAL(AR9170_MAC_BCN_HT2_LEN
, *plcp
, skb
->len
+ FCS_LEN
);
1608 if (*plcp
<= AR9170_TX_PHY_RATE_CCK_11M
)
1609 *plcp
|= ((skb
->len
+ FCS_LEN
) << (3 + 16)) + 0x0400;
1611 *plcp
|= ((skb
->len
+ FCS_LEN
) << 16) + 0x0010;
1617 int carl9170_update_beacon(struct ar9170
*ar
, const bool submit
)
1619 struct sk_buff
*skb
= NULL
;
1620 struct carl9170_vif_info
*cvif
;
1621 __le32
*data
, *old
= NULL
;
1622 u32 word
, ht1
, plcp
, off
, addr
, len
;
1627 cvif
= carl9170_pick_beaconing_vif(ar
);
1631 skb
= ieee80211_beacon_get_tim(ar
->hw
, carl9170_get_vif(cvif
),
1639 spin_lock_bh(&ar
->beacon_lock
);
1640 data
= (__le32
*)skb
->data
;
1642 old
= (__le32
*)cvif
->beacon
->data
;
1644 off
= cvif
->id
* AR9170_MAC_BCN_LENGTH_MAX
;
1645 addr
= ar
->fw
.beacon_addr
+ off
;
1646 len
= roundup(skb
->len
+ FCS_LEN
, 4);
1648 if ((off
+ len
) > ar
->fw
.beacon_max_len
) {
1649 if (net_ratelimit()) {
1650 wiphy_err(ar
->hw
->wiphy
, "beacon does not "
1651 "fit into device memory!\n");
1657 if (len
> AR9170_MAC_BCN_LENGTH_MAX
) {
1658 if (net_ratelimit()) {
1659 wiphy_err(ar
->hw
->wiphy
, "no support for beacons "
1660 "bigger than %d (yours:%d).\n",
1661 AR9170_MAC_BCN_LENGTH_MAX
, len
);
1668 ht_rate
= carl9170_tx_beacon_physet(ar
, skb
, &ht1
, &plcp
);
1670 carl9170_async_regwrite_begin(ar
);
1671 carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1
, ht1
);
1673 carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2
, plcp
);
1675 carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP
, plcp
);
1677 for (i
= 0; i
< DIV_ROUND_UP(skb
->len
, 4); i
++) {
1679 * XXX: This accesses beyond skb data for up
1680 * to the last 3 bytes!!
1683 if (old
&& (data
[i
] == old
[i
]))
1686 word
= le32_to_cpu(data
[i
]);
1687 carl9170_async_regwrite(addr
+ 4 * i
, word
);
1689 carl9170_async_regwrite_finish();
1691 dev_kfree_skb_any(cvif
->beacon
);
1692 cvif
->beacon
= NULL
;
1694 err
= carl9170_async_regwrite_result();
1697 spin_unlock_bh(&ar
->beacon_lock
);
1702 err
= carl9170_bcn_ctrl(ar
, cvif
->id
,
1703 CARL9170_BCN_CTRL_CAB_TRIGGER
,
1704 addr
, skb
->len
+ FCS_LEN
);
1714 spin_unlock_bh(&ar
->beacon_lock
);
1718 dev_kfree_skb_any(skb
);