aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/iwlwifi/iwl-agn-tx.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/wireless/iwlwifi/iwl-agn-tx.c')
-rw-r--r--drivers/net/wireless/iwlwifi/iwl-agn-tx.c980
1 files changed, 980 insertions, 0 deletions
diff --git a/drivers/net/wireless/iwlwifi/iwl-agn-tx.c b/drivers/net/wireless/iwlwifi/iwl-agn-tx.c
new file mode 100644
index 00000000000..475f9d4f56e
--- /dev/null
+++ b/drivers/net/wireless/iwlwifi/iwl-agn-tx.c
@@ -0,0 +1,980 @@
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34
35#include "iwl-dev.h"
36#include "iwl-core.h"
37#include "iwl-sta.h"
38#include "iwl-io.h"
39#include "iwl-helpers.h"
40#include "iwl-agn-hw.h"
41#include "iwl-agn.h"
42#include "iwl-trans.h"
43
44/*
45 * mac80211 queues, ACs, hardware queues, FIFOs.
46 *
47 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
48 *
49 * Mac80211 uses the following numbers, which we get as from it
50 * by way of skb_get_queue_mapping(skb):
51 *
52 * VO 0
53 * VI 1
54 * BE 2
55 * BK 3
56 *
57 *
58 * Regular (not A-MPDU) frames are put into hardware queues corresponding
59 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
60 * own queue per aggregation session (RA/TID combination), such queues are
61 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
62 * order to map frames to the right queue, we also need an AC->hw queue
63 * mapping. This is implemented here.
64 *
65 * Due to the way hw queues are set up (by the hw specific modules like
66 * iwl-4965.c, iwl-5000.c etc.), the AC->hw queue mapping is the identity
67 * mapping.
68 */
69
70static const u8 tid_to_ac[] = {
71 IEEE80211_AC_BE,
72 IEEE80211_AC_BK,
73 IEEE80211_AC_BK,
74 IEEE80211_AC_BE,
75 IEEE80211_AC_VI,
76 IEEE80211_AC_VI,
77 IEEE80211_AC_VO,
78 IEEE80211_AC_VO
79};
80
81static inline int get_ac_from_tid(u16 tid)
82{
83 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
84 return tid_to_ac[tid];
85
86 /* no support for TIDs 8-15 yet */
87 return -EINVAL;
88}
89
90static inline int get_fifo_from_tid(struct iwl_rxon_context *ctx, u16 tid)
91{
92 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
93 return ctx->ac_to_fifo[tid_to_ac[tid]];
94
95 /* no support for TIDs 8-15 yet */
96 return -EINVAL;
97}
98
99static int iwlagn_txq_agg_enable(struct iwl_priv *priv, int txq_id, int sta_id,
100 int tid)
101{
102 if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
103 (IWLAGN_FIRST_AMPDU_QUEUE +
104 priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
105 IWL_WARN(priv,
106 "queue number out of range: %d, must be %d to %d\n",
107 txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
108 IWLAGN_FIRST_AMPDU_QUEUE +
109 priv->cfg->base_params->num_of_ampdu_queues - 1);
110 return -EINVAL;
111 }
112
113 /* Modify device's station table to Tx this TID */
114 return iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
115}
116
117static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
118 struct ieee80211_tx_info *info,
119 __le16 fc, __le32 *tx_flags)
120{
121 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
122 info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
123 info->flags & IEEE80211_TX_CTL_AMPDU)
124 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
125}
126
127/*
128 * handle build REPLY_TX command notification.
129 */
130static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
131 struct sk_buff *skb,
132 struct iwl_tx_cmd *tx_cmd,
133 struct ieee80211_tx_info *info,
134 struct ieee80211_hdr *hdr,
135 u8 std_id)
136{
137 __le16 fc = hdr->frame_control;
138 __le32 tx_flags = tx_cmd->tx_flags;
139
140 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
141
142 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
143 tx_flags |= TX_CMD_FLG_ACK_MSK;
144 else
145 tx_flags &= ~TX_CMD_FLG_ACK_MSK;
146
147 if (ieee80211_is_probe_resp(fc))
148 tx_flags |= TX_CMD_FLG_TSF_MSK;
149 else if (ieee80211_is_back_req(fc))
150 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
151 else if (info->band == IEEE80211_BAND_2GHZ &&
152 priv->cfg->bt_params &&
153 priv->cfg->bt_params->advanced_bt_coexist &&
154 (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
155 ieee80211_is_reassoc_req(fc) ||
156 skb->protocol == cpu_to_be16(ETH_P_PAE)))
157 tx_flags |= TX_CMD_FLG_IGNORE_BT;
158
159
160 tx_cmd->sta_id = std_id;
161 if (ieee80211_has_morefrags(fc))
162 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
163
164 if (ieee80211_is_data_qos(fc)) {
165 u8 *qc = ieee80211_get_qos_ctl(hdr);
166 tx_cmd->tid_tspec = qc[0] & 0xf;
167 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
168 } else {
169 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
170 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
171 else
172 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
173 }
174
175 iwlagn_tx_cmd_protection(priv, info, fc, &tx_flags);
176
177 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
178 if (ieee80211_is_mgmt(fc)) {
179 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
180 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
181 else
182 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
183 } else {
184 tx_cmd->timeout.pm_frame_timeout = 0;
185 }
186
187 tx_cmd->driver_txop = 0;
188 tx_cmd->tx_flags = tx_flags;
189 tx_cmd->next_frame_len = 0;
190}
191
192#define RTS_DFAULT_RETRY_LIMIT 60
193
194static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
195 struct iwl_tx_cmd *tx_cmd,
196 struct ieee80211_tx_info *info,
197 __le16 fc)
198{
199 u32 rate_flags;
200 int rate_idx;
201 u8 rts_retry_limit;
202 u8 data_retry_limit;
203 u8 rate_plcp;
204
205 /* Set retry limit on DATA packets and Probe Responses*/
206 if (ieee80211_is_probe_resp(fc))
207 data_retry_limit = 3;
208 else
209 data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
210 tx_cmd->data_retry_limit = data_retry_limit;
211
212 /* Set retry limit on RTS packets */
213 rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
214 if (data_retry_limit < rts_retry_limit)
215 rts_retry_limit = data_retry_limit;
216 tx_cmd->rts_retry_limit = rts_retry_limit;
217
218 /* DATA packets will use the uCode station table for rate/antenna
219 * selection */
220 if (ieee80211_is_data(fc)) {
221 tx_cmd->initial_rate_index = 0;
222 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
223#ifdef CONFIG_IWLWIFI_DEVICE_SVTOOL
224 if (priv->tm_fixed_rate) {
225 /*
226 * rate overwrite by testmode
227 * we not only send lq command to change rate
228 * we also re-enforce per data pkt base.
229 */
230 tx_cmd->tx_flags &= ~TX_CMD_FLG_STA_RATE_MSK;
231 memcpy(&tx_cmd->rate_n_flags, &priv->tm_fixed_rate,
232 sizeof(tx_cmd->rate_n_flags));
233 }
234#endif
235 return;
236 }
237
238 /**
239 * If the current TX rate stored in mac80211 has the MCS bit set, it's
240 * not really a TX rate. Thus, we use the lowest supported rate for
241 * this band. Also use the lowest supported rate if the stored rate
242 * index is invalid.
243 */
244 rate_idx = info->control.rates[0].idx;
245 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
246 (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
247 rate_idx = rate_lowest_index(&priv->bands[info->band],
248 info->control.sta);
249 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
250 if (info->band == IEEE80211_BAND_5GHZ)
251 rate_idx += IWL_FIRST_OFDM_RATE;
252 /* Get PLCP rate for tx_cmd->rate_n_flags */
253 rate_plcp = iwl_rates[rate_idx].plcp;
254 /* Zero out flags for this packet */
255 rate_flags = 0;
256
257 /* Set CCK flag as needed */
258 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
259 rate_flags |= RATE_MCS_CCK_MSK;
260
261 /* Set up antennas */
262 if (priv->cfg->bt_params &&
263 priv->cfg->bt_params->advanced_bt_coexist &&
264 priv->bt_full_concurrent) {
265 /* operated as 1x1 in full concurrency mode */
266 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
267 first_antenna(priv->hw_params.valid_tx_ant));
268 } else
269 priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
270 priv->hw_params.valid_tx_ant);
271 rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
272
273 /* Set the rate in the TX cmd */
274 tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
275}
276
277static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
278 struct ieee80211_tx_info *info,
279 struct iwl_tx_cmd *tx_cmd,
280 struct sk_buff *skb_frag,
281 int sta_id)
282{
283 struct ieee80211_key_conf *keyconf = info->control.hw_key;
284
285 switch (keyconf->cipher) {
286 case WLAN_CIPHER_SUITE_CCMP:
287 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
288 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
289 if (info->flags & IEEE80211_TX_CTL_AMPDU)
290 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
291 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
292 break;
293
294 case WLAN_CIPHER_SUITE_TKIP:
295 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
296 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
297 IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
298 break;
299
300 case WLAN_CIPHER_SUITE_WEP104:
301 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
302 /* fall through */
303 case WLAN_CIPHER_SUITE_WEP40:
304 tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
305 (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
306
307 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
308
309 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
310 "with key %d\n", keyconf->keyidx);
311 break;
312
313 default:
314 IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
315 break;
316 }
317}
318
319/*
320 * start REPLY_TX command process
321 */
322int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
323{
324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
325 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
326 struct iwl_station_priv *sta_priv = NULL;
327 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
328 struct iwl_tx_cmd *tx_cmd;
329 int txq_id;
330
331 u16 seq_number = 0;
332 __le16 fc;
333 u8 hdr_len;
334 u16 len;
335 u8 sta_id;
336 u8 tid = 0;
337 unsigned long flags;
338 bool is_agg = false;
339
340 /*
341 * If the frame needs to go out off-channel, then
342 * we'll have put the PAN context to that channel,
343 * so make the frame go out there.
344 */
345 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
346 ctx = &priv->contexts[IWL_RXON_CTX_PAN];
347 else if (info->control.vif)
348 ctx = iwl_rxon_ctx_from_vif(info->control.vif);
349
350 spin_lock_irqsave(&priv->lock, flags);
351 if (iwl_is_rfkill(priv)) {
352 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
353 goto drop_unlock_priv;
354 }
355
356 fc = hdr->frame_control;
357
358#ifdef CONFIG_IWLWIFI_DEBUG
359 if (ieee80211_is_auth(fc))
360 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
361 else if (ieee80211_is_assoc_req(fc))
362 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
363 else if (ieee80211_is_reassoc_req(fc))
364 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
365#endif
366
367 hdr_len = ieee80211_hdrlen(fc);
368
369 /* For management frames use broadcast id to do not break aggregation */
370 if (!ieee80211_is_data(fc))
371 sta_id = ctx->bcast_sta_id;
372 else {
373 /* Find index into station table for destination station */
374 sta_id = iwl_sta_id_or_broadcast(priv, ctx, info->control.sta);
375 if (sta_id == IWL_INVALID_STATION) {
376 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
377 hdr->addr1);
378 goto drop_unlock_priv;
379 }
380 }
381
382 IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
383
384 if (info->control.sta)
385 sta_priv = (void *)info->control.sta->drv_priv;
386
387 if (sta_priv && sta_priv->asleep &&
388 (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)) {
389 /*
390 * This sends an asynchronous command to the device,
391 * but we can rely on it being processed before the
392 * next frame is processed -- and the next frame to
393 * this station is the one that will consume this
394 * counter.
395 * For now set the counter to just 1 since we do not
396 * support uAPSD yet.
397 */
398 iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
399 }
400
401 /*
402 * Send this frame after DTIM -- there's a special queue
403 * reserved for this for contexts that support AP mode.
404 */
405 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
406 txq_id = ctx->mcast_queue;
407 /*
408 * The microcode will clear the more data
409 * bit in the last frame it transmits.
410 */
411 hdr->frame_control |=
412 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
413 } else
414 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
415
416 /* irqs already disabled/saved above when locking priv->lock */
417 spin_lock(&priv->sta_lock);
418
419 if (ieee80211_is_data_qos(fc)) {
420 u8 *qc = NULL;
421 qc = ieee80211_get_qos_ctl(hdr);
422 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
423
424 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT))
425 goto drop_unlock_sta;
426
427 seq_number = priv->stations[sta_id].tid[tid].seq_number;
428 seq_number &= IEEE80211_SCTL_SEQ;
429 hdr->seq_ctrl = hdr->seq_ctrl &
430 cpu_to_le16(IEEE80211_SCTL_FRAG);
431 hdr->seq_ctrl |= cpu_to_le16(seq_number);
432 seq_number += 0x10;
433 /* aggregation is on for this <sta,tid> */
434 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
435 priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
436 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
437 is_agg = true;
438 }
439 }
440
441 tx_cmd = trans_get_tx_cmd(&priv->trans, txq_id);
442 if (unlikely(!tx_cmd))
443 goto drop_unlock_sta;
444
445 /* Copy MAC header from skb into command buffer */
446 memcpy(tx_cmd->hdr, hdr, hdr_len);
447
448 /* Total # bytes to be transmitted */
449 len = (u16)skb->len;
450 tx_cmd->len = cpu_to_le16(len);
451
452 if (info->control.hw_key)
453 iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
454
455 /* TODO need this for burst mode later on */
456 iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
457 iwl_dbg_log_tx_data_frame(priv, len, hdr);
458
459 iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
460
461 iwl_update_stats(priv, true, fc, len);
462
463 if (trans_tx(&priv->trans, skb, tx_cmd, txq_id, fc, is_agg, ctx))
464 goto drop_unlock_sta;
465
466 if (ieee80211_is_data_qos(fc)) {
467 priv->stations[sta_id].tid[tid].tfds_in_queue++;
468 if (!ieee80211_has_morefrags(fc))
469 priv->stations[sta_id].tid[tid].seq_number = seq_number;
470 }
471
472 spin_unlock(&priv->sta_lock);
473 spin_unlock_irqrestore(&priv->lock, flags);
474
475 /*
476 * Avoid atomic ops if it isn't an associated client.
477 * Also, if this is a packet for aggregation, don't
478 * increase the counter because the ucode will stop
479 * aggregation queues when their respective station
480 * goes to sleep.
481 */
482 if (sta_priv && sta_priv->client && !is_agg)
483 atomic_inc(&sta_priv->pending_frames);
484
485 return 0;
486
487drop_unlock_sta:
488 spin_unlock(&priv->sta_lock);
489drop_unlock_priv:
490 spin_unlock_irqrestore(&priv->lock, flags);
491 return -1;
492}
493
494/*
495 * Find first available (lowest unused) Tx Queue, mark it "active".
496 * Called only when finding queue for aggregation.
497 * Should never return anything < 7, because they should already
498 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
499 */
500static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv)
501{
502 int txq_id;
503
504 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
505 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
506 return txq_id;
507 return -1;
508}
509
510int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
511 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
512{
513 int sta_id;
514 int tx_fifo;
515 int txq_id;
516 int ret;
517 unsigned long flags;
518 struct iwl_tid_data *tid_data;
519
520 tx_fifo = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
521 if (unlikely(tx_fifo < 0))
522 return tx_fifo;
523
524 IWL_DEBUG_HT(priv, "TX AGG request on ra = %pM tid = %d\n",
525 sta->addr, tid);
526
527 sta_id = iwl_sta_id(sta);
528 if (sta_id == IWL_INVALID_STATION) {
529 IWL_ERR(priv, "Start AGG on invalid station\n");
530 return -ENXIO;
531 }
532 if (unlikely(tid >= MAX_TID_COUNT))
533 return -EINVAL;
534
535 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
536 IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
537 return -ENXIO;
538 }
539
540 txq_id = iwlagn_txq_ctx_activate_free(priv);
541 if (txq_id == -1) {
542 IWL_ERR(priv, "No free aggregation queue available\n");
543 return -ENXIO;
544 }
545
546 spin_lock_irqsave(&priv->sta_lock, flags);
547 tid_data = &priv->stations[sta_id].tid[tid];
548 *ssn = SEQ_TO_SN(tid_data->seq_number);
549 tid_data->agg.txq_id = txq_id;
550 tid_data->agg.tx_fifo = tx_fifo;
551 iwl_set_swq_id(&priv->txq[txq_id], get_ac_from_tid(tid), txq_id);
552 spin_unlock_irqrestore(&priv->sta_lock, flags);
553
554 ret = iwlagn_txq_agg_enable(priv, txq_id, sta_id, tid);
555 if (ret)
556 return ret;
557
558 spin_lock_irqsave(&priv->sta_lock, flags);
559 tid_data = &priv->stations[sta_id].tid[tid];
560 if (tid_data->tfds_in_queue == 0) {
561 IWL_DEBUG_HT(priv, "HW queue is empty\n");
562 tid_data->agg.state = IWL_AGG_ON;
563 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
564 } else {
565 IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
566 tid_data->tfds_in_queue);
567 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
568 }
569 spin_unlock_irqrestore(&priv->sta_lock, flags);
570 return ret;
571}
572
573int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
574 struct ieee80211_sta *sta, u16 tid)
575{
576 int tx_fifo_id, txq_id, sta_id, ssn;
577 struct iwl_tid_data *tid_data;
578 int write_ptr, read_ptr;
579 unsigned long flags;
580
581 tx_fifo_id = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
582 if (unlikely(tx_fifo_id < 0))
583 return tx_fifo_id;
584
585 sta_id = iwl_sta_id(sta);
586
587 if (sta_id == IWL_INVALID_STATION) {
588 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
589 return -ENXIO;
590 }
591
592 spin_lock_irqsave(&priv->sta_lock, flags);
593
594 tid_data = &priv->stations[sta_id].tid[tid];
595 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
596 txq_id = tid_data->agg.txq_id;
597
598 switch (priv->stations[sta_id].tid[tid].agg.state) {
599 case IWL_EMPTYING_HW_QUEUE_ADDBA:
600 /*
601 * This can happen if the peer stops aggregation
602 * again before we've had a chance to drain the
603 * queue we selected previously, i.e. before the
604 * session was really started completely.
605 */
606 IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
607 goto turn_off;
608 case IWL_AGG_ON:
609 break;
610 default:
611 IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
612 }
613
614 write_ptr = priv->txq[txq_id].q.write_ptr;
615 read_ptr = priv->txq[txq_id].q.read_ptr;
616
617 /* The queue is not empty */
618 if (write_ptr != read_ptr) {
619 IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
620 priv->stations[sta_id].tid[tid].agg.state =
621 IWL_EMPTYING_HW_QUEUE_DELBA;
622 spin_unlock_irqrestore(&priv->sta_lock, flags);
623 return 0;
624 }
625
626 IWL_DEBUG_HT(priv, "HW queue is empty\n");
627 turn_off:
628 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
629
630 /* do not restore/save irqs */
631 spin_unlock(&priv->sta_lock);
632 spin_lock(&priv->lock);
633
634 /*
635 * the only reason this call can fail is queue number out of range,
636 * which can happen if uCode is reloaded and all the station
637 * information are lost. if it is outside the range, there is no need
638 * to deactivate the uCode queue, just return "success" to allow
639 * mac80211 to clean up it own data.
640 */
641 trans_txq_agg_disable(&priv->trans, txq_id, ssn, tx_fifo_id);
642 spin_unlock_irqrestore(&priv->lock, flags);
643
644 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
645
646 return 0;
647}
648
649int iwlagn_txq_check_empty(struct iwl_priv *priv,
650 int sta_id, u8 tid, int txq_id)
651{
652 struct iwl_queue *q = &priv->txq[txq_id].q;
653 u8 *addr = priv->stations[sta_id].sta.sta.addr;
654 struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
655 struct iwl_rxon_context *ctx;
656
657 ctx = &priv->contexts[priv->stations[sta_id].ctxid];
658
659 lockdep_assert_held(&priv->sta_lock);
660
661 switch (priv->stations[sta_id].tid[tid].agg.state) {
662 case IWL_EMPTYING_HW_QUEUE_DELBA:
663 /* We are reclaiming the last packet of the */
664 /* aggregated HW queue */
665 if ((txq_id == tid_data->agg.txq_id) &&
666 (q->read_ptr == q->write_ptr)) {
667 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
668 int tx_fifo = get_fifo_from_tid(ctx, tid);
669 IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
670 trans_txq_agg_disable(&priv->trans, txq_id,
671 ssn, tx_fifo);
672 tid_data->agg.state = IWL_AGG_OFF;
673 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
674 }
675 break;
676 case IWL_EMPTYING_HW_QUEUE_ADDBA:
677 /* We are reclaiming the last packet of the queue */
678 if (tid_data->tfds_in_queue == 0) {
679 IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
680 tid_data->agg.state = IWL_AGG_ON;
681 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
682 }
683 break;
684 }
685
686 return 0;
687}
688
689static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
690 struct iwl_rxon_context *ctx,
691 const u8 *addr1)
692{
693 struct ieee80211_sta *sta;
694 struct iwl_station_priv *sta_priv;
695
696 rcu_read_lock();
697 sta = ieee80211_find_sta(ctx->vif, addr1);
698 if (sta) {
699 sta_priv = (void *)sta->drv_priv;
700 /* avoid atomic ops if this isn't a client */
701 if (sta_priv->client &&
702 atomic_dec_return(&sta_priv->pending_frames) == 0)
703 ieee80211_sta_block_awake(priv->hw, sta, false);
704 }
705 rcu_read_unlock();
706}
707
708static void iwlagn_tx_status(struct iwl_priv *priv, struct iwl_tx_info *tx_info,
709 bool is_agg)
710{
711 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
712
713 if (!is_agg)
714 iwlagn_non_agg_tx_status(priv, tx_info->ctx, hdr->addr1);
715
716 ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb);
717}
718
719int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
720{
721 struct iwl_tx_queue *txq = &priv->txq[txq_id];
722 struct iwl_queue *q = &txq->q;
723 struct iwl_tx_info *tx_info;
724 int nfreed = 0;
725 struct ieee80211_hdr *hdr;
726
727 if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
728 IWL_ERR(priv, "%s: Read index for DMA queue txq id (%d), "
729 "index %d is out of range [0-%d] %d %d.\n", __func__,
730 txq_id, index, q->n_bd, q->write_ptr, q->read_ptr);
731 return 0;
732 }
733
734 for (index = iwl_queue_inc_wrap(index, q->n_bd);
735 q->read_ptr != index;
736 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
737
738 tx_info = &txq->txb[txq->q.read_ptr];
739
740 if (WARN_ON_ONCE(tx_info->skb == NULL))
741 continue;
742
743 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
744 if (ieee80211_is_data_qos(hdr->frame_control))
745 nfreed++;
746
747 iwlagn_tx_status(priv, tx_info,
748 txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
749 tx_info->skb = NULL;
750
751 iwlagn_txq_inval_byte_cnt_tbl(priv, txq);
752
753 iwlagn_txq_free_tfd(priv, txq, txq->q.read_ptr);
754 }
755 return nfreed;
756}
757
758/**
759 * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack
760 *
761 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
762 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
763 */
764static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv,
765 struct iwl_ht_agg *agg,
766 struct iwl_compressed_ba_resp *ba_resp)
767
768{
769 int sh;
770 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
771 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
772 struct ieee80211_tx_info *info;
773 u64 bitmap, sent_bitmap;
774
775 if (unlikely(!agg->wait_for_ba)) {
776 if (unlikely(ba_resp->bitmap))
777 IWL_ERR(priv, "Received BA when not expected\n");
778 return -EINVAL;
779 }
780
781 /* Mark that the expected block-ack response arrived */
782 agg->wait_for_ba = 0;
783 IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
784
785 /* Calculate shift to align block-ack bits with our Tx window bits */
786 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
787 if (sh < 0)
788 sh += 0x100;
789
790 /*
791 * Check for success or failure according to the
792 * transmitted bitmap and block-ack bitmap
793 */
794 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
795 sent_bitmap = bitmap & agg->bitmap;
796
797 /* Sanity check values reported by uCode */
798 if (ba_resp->txed_2_done > ba_resp->txed) {
799 IWL_DEBUG_TX_REPLY(priv,
800 "bogus sent(%d) and ack(%d) count\n",
801 ba_resp->txed, ba_resp->txed_2_done);
802 /*
803 * set txed_2_done = txed,
804 * so it won't impact rate scale
805 */
806 ba_resp->txed = ba_resp->txed_2_done;
807 }
808 IWL_DEBUG_HT(priv, "agg frames sent:%d, acked:%d\n",
809 ba_resp->txed, ba_resp->txed_2_done);
810
811 /* Find the first ACKed frame to store the TX status */
812 while (sent_bitmap && !(sent_bitmap & 1)) {
813 agg->start_idx = (agg->start_idx + 1) & 0xff;
814 sent_bitmap >>= 1;
815 }
816
817 info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
818 memset(&info->status, 0, sizeof(info->status));
819 info->flags |= IEEE80211_TX_STAT_ACK;
820 info->flags |= IEEE80211_TX_STAT_AMPDU;
821 info->status.ampdu_ack_len = ba_resp->txed_2_done;
822 info->status.ampdu_len = ba_resp->txed;
823 iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
824
825 return 0;
826}
827
828/**
829 * translate ucode response to mac80211 tx status control values
830 */
831void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
832 struct ieee80211_tx_info *info)
833{
834 struct ieee80211_tx_rate *r = &info->control.rates[0];
835
836 info->antenna_sel_tx =
837 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
838 if (rate_n_flags & RATE_MCS_HT_MSK)
839 r->flags |= IEEE80211_TX_RC_MCS;
840 if (rate_n_flags & RATE_MCS_GF_MSK)
841 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
842 if (rate_n_flags & RATE_MCS_HT40_MSK)
843 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
844 if (rate_n_flags & RATE_MCS_DUP_MSK)
845 r->flags |= IEEE80211_TX_RC_DUP_DATA;
846 if (rate_n_flags & RATE_MCS_SGI_MSK)
847 r->flags |= IEEE80211_TX_RC_SHORT_GI;
848 r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
849}
850
851/**
852 * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
853 *
854 * Handles block-acknowledge notification from device, which reports success
855 * of frames sent via aggregation.
856 */
857void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
858 struct iwl_rx_mem_buffer *rxb)
859{
860 struct iwl_rx_packet *pkt = rxb_addr(rxb);
861 struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
862 struct iwl_tx_queue *txq = NULL;
863 struct iwl_ht_agg *agg;
864 int index;
865 int sta_id;
866 int tid;
867 unsigned long flags;
868
869 /* "flow" corresponds to Tx queue */
870 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
871
872 /* "ssn" is start of block-ack Tx window, corresponds to index
873 * (in Tx queue's circular buffer) of first TFD/frame in window */
874 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
875
876 if (scd_flow >= priv->hw_params.max_txq_num) {
877 IWL_ERR(priv,
878 "BUG_ON scd_flow is bigger than number of queues\n");
879 return;
880 }
881
882 txq = &priv->txq[scd_flow];
883 sta_id = ba_resp->sta_id;
884 tid = ba_resp->tid;
885 agg = &priv->stations[sta_id].tid[tid].agg;
886 if (unlikely(agg->txq_id != scd_flow)) {
887 /*
888 * FIXME: this is a uCode bug which need to be addressed,
889 * log the information and return for now!
890 * since it is possible happen very often and in order
891 * not to fill the syslog, don't enable the logging by default
892 */
893 IWL_DEBUG_TX_REPLY(priv,
894 "BA scd_flow %d does not match txq_id %d\n",
895 scd_flow, agg->txq_id);
896 return;
897 }
898
899 /* Find index just before block-ack window */
900 index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
901
902 spin_lock_irqsave(&priv->sta_lock, flags);
903
904 IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
905 "sta_id = %d\n",
906 agg->wait_for_ba,
907 (u8 *) &ba_resp->sta_addr_lo32,
908 ba_resp->sta_id);
909 IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
910 "%d, scd_ssn = %d\n",
911 ba_resp->tid,
912 ba_resp->seq_ctl,
913 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
914 ba_resp->scd_flow,
915 ba_resp->scd_ssn);
916 IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
917 agg->start_idx,
918 (unsigned long long)agg->bitmap);
919
920 /* Update driver's record of ACK vs. not for each frame in window */
921 iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp);
922
923 /* Release all TFDs before the SSN, i.e. all TFDs in front of
924 * block-ack window (we assume that they've been successfully
925 * transmitted ... if not, it's too late anyway). */
926 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
927 /* calculate mac80211 ampdu sw queue to wake */
928 int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index);
929 iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
930
931 if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
932 priv->mac80211_registered &&
933 (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
934 iwl_wake_queue(priv, txq);
935
936 iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow);
937 }
938
939 spin_unlock_irqrestore(&priv->sta_lock, flags);
940}
941
942#ifdef CONFIG_IWLWIFI_DEBUG
943const char *iwl_get_tx_fail_reason(u32 status)
944{
945#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
946#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
947
948 switch (status & TX_STATUS_MSK) {
949 case TX_STATUS_SUCCESS:
950 return "SUCCESS";
951 TX_STATUS_POSTPONE(DELAY);
952 TX_STATUS_POSTPONE(FEW_BYTES);
953 TX_STATUS_POSTPONE(BT_PRIO);
954 TX_STATUS_POSTPONE(QUIET_PERIOD);
955 TX_STATUS_POSTPONE(CALC_TTAK);
956 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
957 TX_STATUS_FAIL(SHORT_LIMIT);
958 TX_STATUS_FAIL(LONG_LIMIT);
959 TX_STATUS_FAIL(FIFO_UNDERRUN);
960 TX_STATUS_FAIL(DRAIN_FLOW);
961 TX_STATUS_FAIL(RFKILL_FLUSH);
962 TX_STATUS_FAIL(LIFE_EXPIRE);
963 TX_STATUS_FAIL(DEST_PS);
964 TX_STATUS_FAIL(HOST_ABORTED);
965 TX_STATUS_FAIL(BT_RETRY);
966 TX_STATUS_FAIL(STA_INVALID);
967 TX_STATUS_FAIL(FRAG_DROPPED);
968 TX_STATUS_FAIL(TID_DISABLE);
969 TX_STATUS_FAIL(FIFO_FLUSHED);
970 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
971 TX_STATUS_FAIL(PASSIVE_NO_RX);
972 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
973 }
974
975 return "UNKNOWN";
976
977#undef TX_STATUS_FAIL
978#undef TX_STATUS_POSTPONE
979}
980#endif /* CONFIG_IWLWIFI_DEBUG */