Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / tx.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * 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; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <linux/ieee80211.h>
64 #include <linux/etherdevice.h>
65
66 #include "iwl-trans.h"
67 #include "iwl-eeprom-parse.h"
68 #include "mvm.h"
69 #include "sta.h"
70
71 /*
72  * Sets most of the Tx cmd's fields
73  */
74 static void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
75                                struct iwl_tx_cmd *tx_cmd,
76                                struct ieee80211_tx_info *info, u8 sta_id)
77 {
78         struct ieee80211_hdr *hdr = (void *)skb->data;
79         __le16 fc = hdr->frame_control;
80         u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
81         u32 len = skb->len + FCS_LEN;
82
83         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
84                 tx_flags |= TX_CMD_FLG_ACK;
85         else
86                 tx_flags &= ~TX_CMD_FLG_ACK;
87
88         if (ieee80211_is_probe_resp(fc))
89                 tx_flags |= TX_CMD_FLG_TSF;
90         else if (ieee80211_is_back_req(fc))
91                 tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
92
93         /* High prio packet (wrt. BT coex) if it is EAPOL, MCAST or MGMT */
94         if (info->band == IEEE80211_BAND_2GHZ &&
95             (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO ||
96              is_multicast_ether_addr(hdr->addr1) ||
97              ieee80211_is_back_req(fc) || ieee80211_is_mgmt(fc)))
98                 tx_flags |= TX_CMD_FLG_BT_DIS;
99
100         if (ieee80211_has_morefrags(fc))
101                 tx_flags |= TX_CMD_FLG_MORE_FRAG;
102
103         if (ieee80211_is_data_qos(fc)) {
104                 u8 *qc = ieee80211_get_qos_ctl(hdr);
105                 tx_cmd->tid_tspec = qc[0] & 0xf;
106                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
107         } else {
108                 tx_cmd->tid_tspec = IWL_TID_NON_QOS;
109                 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
110                         tx_flags |= TX_CMD_FLG_SEQ_CTL;
111                 else
112                         tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
113         }
114
115         if (ieee80211_is_mgmt(fc)) {
116                 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
117                         tx_cmd->pm_frame_timeout = cpu_to_le16(3);
118                 else
119                         tx_cmd->pm_frame_timeout = cpu_to_le16(2);
120
121                 /* The spec allows Action frames in A-MPDU, we don't support
122                  * it
123                  */
124                 WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
125         } else if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
126                 tx_cmd->pm_frame_timeout = cpu_to_le16(2);
127         } else {
128                 tx_cmd->pm_frame_timeout = 0;
129         }
130
131         if (info->flags & IEEE80211_TX_CTL_AMPDU)
132                 tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
133
134         if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
135             !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
136                 tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
137
138         tx_cmd->driver_txop = 0;
139         tx_cmd->tx_flags = cpu_to_le32(tx_flags);
140         /* Total # bytes to be transmitted */
141         tx_cmd->len = cpu_to_le16((u16)skb->len);
142         tx_cmd->next_frame_len = 0;
143         tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
144         tx_cmd->sta_id = sta_id;
145 }
146
147 /*
148  * Sets the fields in the Tx cmd that are rate related
149  */
150 static void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm,
151                                     struct iwl_tx_cmd *tx_cmd,
152                                     struct ieee80211_tx_info *info,
153                                     struct ieee80211_sta *sta,
154                                     __le16 fc)
155 {
156         u32 rate_flags;
157         int rate_idx;
158         u8 rate_plcp;
159
160         /* Set retry limit on RTS packets */
161         tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
162
163         /* Set retry limit on DATA packets and Probe Responses*/
164         if (ieee80211_is_probe_resp(fc)) {
165                 tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
166                 tx_cmd->rts_retry_limit =
167                         min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
168         } else if (ieee80211_is_back_req(fc)) {
169                 tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
170         } else {
171                 tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
172         }
173
174         /*
175          * for data packets, rate info comes from the table inside the fw. This
176          * table is controlled by LINK_QUALITY commands
177          */
178
179         if (ieee80211_is_data(fc) && sta) {
180                 tx_cmd->initial_rate_index = 0;
181                 tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
182                 return;
183         } else if (ieee80211_is_back_req(fc)) {
184                 tx_cmd->tx_flags |=
185                         cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
186         }
187
188         /* HT rate doesn't make sense for a non data frame */
189         WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
190                   "Got an HT rate for a non data frame 0x%x\n",
191                   info->control.rates[0].flags);
192
193         rate_idx = info->control.rates[0].idx;
194         /* if the rate isn't a well known legacy rate, take the lowest one */
195         if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT_LEGACY)
196                 rate_idx = rate_lowest_index(
197                                 &mvm->nvm_data->bands[info->band], sta);
198
199         /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
200         if (info->band == IEEE80211_BAND_5GHZ)
201                 rate_idx += IWL_FIRST_OFDM_RATE;
202
203         /* For 2.4 GHZ band, check that there is no need to remap */
204         BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
205
206         /* Get PLCP rate for tx_cmd->rate_n_flags */
207         rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
208
209         mvm->mgmt_last_antenna_idx =
210                 iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
211                                      mvm->mgmt_last_antenna_idx);
212         rate_flags = BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
213
214         /* Set CCK flag as needed */
215         if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
216                 rate_flags |= RATE_MCS_CCK_MSK;
217
218         /* Set the rate in the TX cmd */
219         tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
220 }
221
222 /*
223  * Sets the fields in the Tx cmd that are crypto related
224  */
225 static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
226                                       struct ieee80211_tx_info *info,
227                                       struct iwl_tx_cmd *tx_cmd,
228                                       struct sk_buff *skb_frag)
229 {
230         struct ieee80211_key_conf *keyconf = info->control.hw_key;
231
232         switch (keyconf->cipher) {
233         case WLAN_CIPHER_SUITE_CCMP:
234                 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
235                 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
236                 if (info->flags & IEEE80211_TX_CTL_AMPDU)
237                         tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_CCMP_AGG);
238                 break;
239
240         case WLAN_CIPHER_SUITE_TKIP:
241                 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
242                 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
243                 break;
244
245         case WLAN_CIPHER_SUITE_WEP104:
246                 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
247                 /* fall through */
248         case WLAN_CIPHER_SUITE_WEP40:
249                 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
250                         ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
251                           TX_CMD_SEC_WEP_KEY_IDX_MSK);
252
253                 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
254                 break;
255         default:
256                 tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
257         }
258 }
259
260 /*
261  * Allocates and sets the Tx cmd the driver data pointers in the skb
262  */
263 static struct iwl_device_cmd *
264 iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
265                       struct ieee80211_sta *sta, u8 sta_id)
266 {
267         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
268         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
269         struct iwl_device_cmd *dev_cmd;
270         struct iwl_tx_cmd *tx_cmd;
271
272         dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
273
274         if (unlikely(!dev_cmd))
275                 return NULL;
276
277         memset(dev_cmd, 0, sizeof(*dev_cmd));
278         dev_cmd->hdr.cmd = TX_CMD;
279         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
280
281         if (info->control.hw_key)
282                 iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb);
283
284         iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
285
286         iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
287
288         memset(&info->status, 0, sizeof(info->status));
289
290         info->driver_data[0] = NULL;
291         info->driver_data[1] = dev_cmd;
292
293         return dev_cmd;
294 }
295
296 int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
297 {
298         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
299         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
300         struct iwl_device_cmd *dev_cmd;
301         struct iwl_tx_cmd *tx_cmd;
302         u8 sta_id;
303
304         if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU))
305                 return -1;
306
307         if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
308                          (!info->control.vif ||
309                           info->hw_queue != info->control.vif->cab_queue)))
310                 return -1;
311
312         /*
313          * If the interface on which frame is sent is the P2P_DEVICE
314          * or an AP/GO interface use the broadcast station associated
315          * with it; otherwise use the AUX station.
316          */
317         if (info->control.vif &&
318             (info->control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
319              info->control.vif->type == NL80211_IFTYPE_AP)) {
320                 struct iwl_mvm_vif *mvmvif =
321                         iwl_mvm_vif_from_mac80211(info->control.vif);
322                 sta_id = mvmvif->bcast_sta.sta_id;
323         } else {
324                 sta_id = mvm->aux_sta.sta_id;
325         }
326
327         IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info->hw_queue);
328
329         dev_cmd = iwl_mvm_set_tx_params(mvm, skb, NULL, sta_id);
330         if (!dev_cmd)
331                 return -1;
332
333         /* From now on, we cannot access info->control */
334         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
335
336         /* Copy MAC header from skb into command buffer */
337         memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(hdr->frame_control));
338
339         if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info->hw_queue)) {
340                 iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
341                 return -1;
342         }
343
344         return 0;
345 }
346
347 /*
348  * Sets the fields in the Tx cmd that are crypto related
349  */
350 int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
351                    struct ieee80211_sta *sta)
352 {
353         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
354         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
355         struct iwl_mvm_sta *mvmsta;
356         struct iwl_device_cmd *dev_cmd;
357         struct iwl_tx_cmd *tx_cmd;
358         __le16 fc;
359         u16 seq_number = 0;
360         u8 tid = IWL_MAX_TID_COUNT;
361         u8 txq_id = info->hw_queue;
362         bool is_data_qos = false, is_ampdu = false;
363
364         mvmsta = iwl_mvm_sta_from_mac80211(sta);
365         fc = hdr->frame_control;
366
367         if (WARN_ON_ONCE(!mvmsta))
368                 return -1;
369
370         if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
371                 return -1;
372
373         dev_cmd = iwl_mvm_set_tx_params(mvm, skb, sta, mvmsta->sta_id);
374         if (!dev_cmd)
375                 goto drop;
376
377         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
378         /* From now on, we cannot access info->control */
379
380         /*
381          * we handle that entirely ourselves -- for uAPSD the firmware
382          * will always send a notification, and for PS-Poll responses
383          * we'll notify mac80211 when getting frame status
384          */
385         info->flags &= ~IEEE80211_TX_STATUS_EOSP;
386
387         spin_lock(&mvmsta->lock);
388
389         if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
390                 u8 *qc = NULL;
391                 qc = ieee80211_get_qos_ctl(hdr);
392                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
393                 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
394                         goto drop_unlock_sta;
395
396                 seq_number = mvmsta->tid_data[tid].seq_number;
397                 seq_number &= IEEE80211_SCTL_SEQ;
398                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
399                 hdr->seq_ctrl |= cpu_to_le16(seq_number);
400                 is_data_qos = true;
401                 is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
402         }
403
404         /* Copy MAC header from skb into command buffer */
405         memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(fc));
406
407         WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
408
409         if (is_ampdu) {
410                 if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
411                         goto drop_unlock_sta;
412                 txq_id = mvmsta->tid_data[tid].txq_id;
413         }
414
415         IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
416                      tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
417
418         if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
419                 goto drop_unlock_sta;
420
421         if (is_data_qos && !ieee80211_has_morefrags(fc))
422                 mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
423
424         spin_unlock(&mvmsta->lock);
425
426         if (txq_id < mvm->first_agg_queue)
427                 atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
428
429         return 0;
430
431 drop_unlock_sta:
432         iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
433         spin_unlock(&mvmsta->lock);
434 drop:
435         return -1;
436 }
437
438 static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
439                                       struct ieee80211_sta *sta, u8 tid)
440 {
441         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
442         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
443         struct ieee80211_vif *vif = mvmsta->vif;
444
445         lockdep_assert_held(&mvmsta->lock);
446
447         if ((tid_data->state == IWL_AGG_ON ||
448              tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
449             iwl_mvm_tid_queued(tid_data) == 0) {
450                 /*
451                  * Now that this aggregation queue is empty tell mac80211 so it
452                  * knows we no longer have frames buffered for the station on
453                  * this TID (for the TIM bitmap calculation.)
454                  */
455                 ieee80211_sta_set_buffered(sta, tid, false);
456         }
457
458         if (tid_data->ssn != tid_data->next_reclaimed)
459                 return;
460
461         switch (tid_data->state) {
462         case IWL_EMPTYING_HW_QUEUE_ADDBA:
463                 IWL_DEBUG_TX_QUEUES(mvm,
464                                     "Can continue addBA flow ssn = next_recl = %d\n",
465                                     tid_data->next_reclaimed);
466                 tid_data->state = IWL_AGG_STARTING;
467                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
468                 break;
469
470         case IWL_EMPTYING_HW_QUEUE_DELBA:
471                 IWL_DEBUG_TX_QUEUES(mvm,
472                                     "Can continue DELBA flow ssn = next_recl = %d\n",
473                                     tid_data->next_reclaimed);
474                 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
475                 tid_data->state = IWL_AGG_OFF;
476                 /*
477                  * we can't hold the mutex - but since we are after a sequence
478                  * point (call to iwl_trans_txq_disable), so we don't even need
479                  * a memory barrier.
480                  */
481                 mvm->queue_to_mac80211[tid_data->txq_id] =
482                                         IWL_INVALID_MAC80211_QUEUE;
483                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
484                 break;
485
486         default:
487                 break;
488         }
489 }
490
491 #ifdef CONFIG_IWLWIFI_DEBUG
492 const char *iwl_mvm_get_tx_fail_reason(u32 status)
493 {
494 #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
495 #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
496
497         switch (status & TX_STATUS_MSK) {
498         case TX_STATUS_SUCCESS:
499                 return "SUCCESS";
500         TX_STATUS_POSTPONE(DELAY);
501         TX_STATUS_POSTPONE(FEW_BYTES);
502         TX_STATUS_POSTPONE(BT_PRIO);
503         TX_STATUS_POSTPONE(QUIET_PERIOD);
504         TX_STATUS_POSTPONE(CALC_TTAK);
505         TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
506         TX_STATUS_FAIL(SHORT_LIMIT);
507         TX_STATUS_FAIL(LONG_LIMIT);
508         TX_STATUS_FAIL(UNDERRUN);
509         TX_STATUS_FAIL(DRAIN_FLOW);
510         TX_STATUS_FAIL(RFKILL_FLUSH);
511         TX_STATUS_FAIL(LIFE_EXPIRE);
512         TX_STATUS_FAIL(DEST_PS);
513         TX_STATUS_FAIL(HOST_ABORTED);
514         TX_STATUS_FAIL(BT_RETRY);
515         TX_STATUS_FAIL(STA_INVALID);
516         TX_STATUS_FAIL(FRAG_DROPPED);
517         TX_STATUS_FAIL(TID_DISABLE);
518         TX_STATUS_FAIL(FIFO_FLUSHED);
519         TX_STATUS_FAIL(SMALL_CF_POLL);
520         TX_STATUS_FAIL(FW_DROP);
521         TX_STATUS_FAIL(STA_COLOR_MISMATCH);
522         }
523
524         return "UNKNOWN";
525
526 #undef TX_STATUS_FAIL
527 #undef TX_STATUS_POSTPONE
528 }
529 #endif /* CONFIG_IWLWIFI_DEBUG */
530
531 void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
532                                enum ieee80211_band band,
533                                struct ieee80211_tx_rate *r)
534 {
535         if (rate_n_flags & RATE_HT_MCS_GF_MSK)
536                 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
537         switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
538         case RATE_MCS_CHAN_WIDTH_20:
539                 break;
540         case RATE_MCS_CHAN_WIDTH_40:
541                 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
542                 break;
543         case RATE_MCS_CHAN_WIDTH_80:
544                 r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
545                 break;
546         case RATE_MCS_CHAN_WIDTH_160:
547                 r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
548                 break;
549         }
550         if (rate_n_flags & RATE_MCS_SGI_MSK)
551                 r->flags |= IEEE80211_TX_RC_SHORT_GI;
552         if (rate_n_flags & RATE_MCS_HT_MSK) {
553                 r->flags |= IEEE80211_TX_RC_MCS;
554                 r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
555         } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
556                 ieee80211_rate_set_vht(
557                         r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
558                         ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
559                                                 RATE_VHT_MCS_NSS_POS) + 1);
560                 r->flags |= IEEE80211_TX_RC_VHT_MCS;
561         } else {
562                 r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
563                                                              band);
564         }
565 }
566
567 /**
568  * translate ucode response to mac80211 tx status control values
569  */
570 static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
571                                         struct ieee80211_tx_info *info)
572 {
573         struct ieee80211_tx_rate *r = &info->status.rates[0];
574
575         info->status.antenna =
576                 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
577         iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
578 }
579
580 static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
581                                      struct iwl_rx_packet *pkt)
582 {
583         struct ieee80211_sta *sta;
584         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
585         int txq_id = SEQ_TO_QUEUE(sequence);
586         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
587         int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
588         int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
589         u32 status = le16_to_cpu(tx_resp->status.status);
590         u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
591         struct iwl_mvm_sta *mvmsta;
592         struct sk_buff_head skbs;
593         u8 skb_freed = 0;
594         u16 next_reclaimed, seq_ctl;
595
596         __skb_queue_head_init(&skbs);
597
598         seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
599
600         /* we can free until ssn % q.n_bd not inclusive */
601         iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
602
603         while (!skb_queue_empty(&skbs)) {
604                 struct sk_buff *skb = __skb_dequeue(&skbs);
605                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
606
607                 skb_freed++;
608
609                 iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
610
611                 memset(&info->status, 0, sizeof(info->status));
612
613                 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
614
615                 /* inform mac80211 about what happened with the frame */
616                 switch (status & TX_STATUS_MSK) {
617                 case TX_STATUS_SUCCESS:
618                 case TX_STATUS_DIRECT_DONE:
619                         info->flags |= IEEE80211_TX_STAT_ACK;
620                         break;
621                 case TX_STATUS_FAIL_DEST_PS:
622                         info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
623                         break;
624                 default:
625                         break;
626                 }
627
628                 info->status.rates[0].count = tx_resp->failure_frame + 1;
629                 iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
630                                             info);
631
632                 /* Single frame failure in an AMPDU queue => send BAR */
633                 if (txq_id >= mvm->first_agg_queue &&
634                     !(info->flags & IEEE80211_TX_STAT_ACK))
635                         info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
636
637                 /* W/A FW bug: seq_ctl is wrong when the status isn't success */
638                 if (status != TX_STATUS_SUCCESS) {
639                         struct ieee80211_hdr *hdr = (void *)skb->data;
640                         seq_ctl = le16_to_cpu(hdr->seq_ctrl);
641                 }
642
643                 ieee80211_tx_status_ni(mvm->hw, skb);
644         }
645
646         if (txq_id >= mvm->first_agg_queue) {
647                 /* If this is an aggregation queue, we use the ssn since:
648                  * ssn = wifi seq_num % 256.
649                  * The seq_ctl is the sequence control of the packet to which
650                  * this Tx response relates. But if there is a hole in the
651                  * bitmap of the BA we received, this Tx response may allow to
652                  * reclaim the hole and all the subsequent packets that were
653                  * already acked. In that case, seq_ctl != ssn, and the next
654                  * packet to be reclaimed will be ssn and not seq_ctl. In that
655                  * case, several packets will be reclaimed even if
656                  * frame_count = 1.
657                  *
658                  * The ssn is the index (% 256) of the latest packet that has
659                  * treated (acked / dropped) + 1.
660                  */
661                 next_reclaimed = ssn;
662         } else {
663                 /* The next packet to be reclaimed is the one after this one */
664                 next_reclaimed = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
665         }
666
667         IWL_DEBUG_TX_REPLY(mvm,
668                            "TXQ %d status %s (0x%08x)\n",
669                            txq_id, iwl_mvm_get_tx_fail_reason(status), status);
670
671         IWL_DEBUG_TX_REPLY(mvm,
672                            "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
673                            le32_to_cpu(tx_resp->initial_rate),
674                            tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
675                            ssn, next_reclaimed, seq_ctl);
676
677         rcu_read_lock();
678
679         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
680         /*
681          * sta can't be NULL otherwise it'd mean that the sta has been freed in
682          * the firmware while we still have packets for it in the Tx queues.
683          */
684         if (WARN_ON_ONCE(!sta))
685                 goto out;
686
687         if (!IS_ERR(sta)) {
688                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
689
690                 if (tid != IWL_TID_NON_QOS) {
691                         struct iwl_mvm_tid_data *tid_data =
692                                 &mvmsta->tid_data[tid];
693
694                         spin_lock_bh(&mvmsta->lock);
695                         tid_data->next_reclaimed = next_reclaimed;
696                         IWL_DEBUG_TX_REPLY(mvm, "Next reclaimed packet:%d\n",
697                                            next_reclaimed);
698                         iwl_mvm_check_ratid_empty(mvm, sta, tid);
699                         spin_unlock_bh(&mvmsta->lock);
700                 }
701
702                 if (mvmsta->next_status_eosp) {
703                         mvmsta->next_status_eosp = false;
704                         ieee80211_sta_eosp(sta);
705                 }
706         } else {
707                 mvmsta = NULL;
708         }
709
710         /*
711          * If the txq is not an AMPDU queue, there is no chance we freed
712          * several skbs. Check that out...
713          */
714         if (txq_id >= mvm->first_agg_queue)
715                 goto out;
716
717         /* We can't free more than one frame at once on a shared queue */
718         WARN_ON(skb_freed > 1);
719
720         /* If we have still frames from this STA nothing to do here */
721         if (!atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id]))
722                 goto out;
723
724         if (mvmsta && mvmsta->vif->type == NL80211_IFTYPE_AP) {
725                 /*
726                  * If there are no pending frames for this STA, notify
727                  * mac80211 that this station can go to sleep in its
728                  * STA table.
729                  * If mvmsta is not NULL, sta is valid.
730                  */
731                 ieee80211_sta_block_awake(mvm->hw, sta, false);
732         }
733
734         if (PTR_ERR(sta) == -EBUSY || PTR_ERR(sta) == -ENOENT) {
735                 /*
736                  * We are draining and this was the last packet - pre_rcu_remove
737                  * has been called already. We might be after the
738                  * synchronize_net already.
739                  * Don't rely on iwl_mvm_rm_sta to see the empty Tx queues.
740                  */
741                 set_bit(sta_id, mvm->sta_drained);
742                 schedule_work(&mvm->sta_drained_wk);
743         }
744
745 out:
746         rcu_read_unlock();
747 }
748
749 #ifdef CONFIG_IWLWIFI_DEBUG
750 #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
751 static const char *iwl_get_agg_tx_status(u16 status)
752 {
753         switch (status & AGG_TX_STATE_STATUS_MSK) {
754         AGG_TX_STATE_(TRANSMITTED);
755         AGG_TX_STATE_(UNDERRUN);
756         AGG_TX_STATE_(BT_PRIO);
757         AGG_TX_STATE_(FEW_BYTES);
758         AGG_TX_STATE_(ABORT);
759         AGG_TX_STATE_(LAST_SENT_TTL);
760         AGG_TX_STATE_(LAST_SENT_TRY_CNT);
761         AGG_TX_STATE_(LAST_SENT_BT_KILL);
762         AGG_TX_STATE_(SCD_QUERY);
763         AGG_TX_STATE_(TEST_BAD_CRC32);
764         AGG_TX_STATE_(RESPONSE);
765         AGG_TX_STATE_(DUMP_TX);
766         AGG_TX_STATE_(DELAY_TX);
767         }
768
769         return "UNKNOWN";
770 }
771
772 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
773                                       struct iwl_rx_packet *pkt)
774 {
775         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
776         struct agg_tx_status *frame_status = &tx_resp->status;
777         int i;
778
779         for (i = 0; i < tx_resp->frame_count; i++) {
780                 u16 fstatus = le16_to_cpu(frame_status[i].status);
781
782                 IWL_DEBUG_TX_REPLY(mvm,
783                                    "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
784                                    iwl_get_agg_tx_status(fstatus),
785                                    fstatus & AGG_TX_STATE_STATUS_MSK,
786                                    (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
787                                         AGG_TX_STATE_TRY_CNT_POS,
788                                    le16_to_cpu(frame_status[i].sequence));
789         }
790 }
791 #else
792 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
793                                       struct iwl_rx_packet *pkt)
794 {}
795 #endif /* CONFIG_IWLWIFI_DEBUG */
796
797 static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
798                                   struct iwl_rx_packet *pkt)
799 {
800         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
801         int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
802         int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
803         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
804         struct ieee80211_sta *sta;
805
806         if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < mvm->first_agg_queue))
807                 return;
808
809         if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
810                 return;
811
812         iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
813
814         rcu_read_lock();
815
816         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
817
818         if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
819                 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
820                 mvmsta->tid_data[tid].rate_n_flags =
821                         le32_to_cpu(tx_resp->initial_rate);
822         }
823
824         rcu_read_unlock();
825 }
826
827 int iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
828                       struct iwl_device_cmd *cmd)
829 {
830         struct iwl_rx_packet *pkt = rxb_addr(rxb);
831         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
832
833         if (tx_resp->frame_count == 1)
834                 iwl_mvm_rx_tx_cmd_single(mvm, pkt);
835         else
836                 iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
837
838         return 0;
839 }
840
841 int iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
842                         struct iwl_device_cmd *cmd)
843 {
844         struct iwl_rx_packet *pkt = rxb_addr(rxb);
845         struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
846         struct sk_buff_head reclaimed_skbs;
847         struct iwl_mvm_tid_data *tid_data;
848         struct ieee80211_tx_info *info;
849         struct ieee80211_sta *sta;
850         struct iwl_mvm_sta *mvmsta;
851         struct ieee80211_hdr *hdr;
852         struct sk_buff *skb;
853         int sta_id, tid, freed;
854
855         /* "flow" corresponds to Tx queue */
856         u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
857
858         /* "ssn" is start of block-ack Tx window, corresponds to index
859          * (in Tx queue's circular buffer) of first TFD/frame in window */
860         u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
861
862         sta_id = ba_notif->sta_id;
863         tid = ba_notif->tid;
864
865         rcu_read_lock();
866
867         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
868
869         /* Reclaiming frames for a station that has been deleted ? */
870         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
871                 rcu_read_unlock();
872                 return 0;
873         }
874
875         mvmsta = iwl_mvm_sta_from_mac80211(sta);
876         tid_data = &mvmsta->tid_data[tid];
877
878         if (WARN_ONCE(tid_data->txq_id != scd_flow, "Q %d, tid %d, flow %d",
879                       tid_data->txq_id, tid, scd_flow)) {
880                 rcu_read_unlock();
881                 return 0;
882         }
883
884         spin_lock_bh(&mvmsta->lock);
885
886         __skb_queue_head_init(&reclaimed_skbs);
887
888         /*
889          * Release all TFDs before the SSN, i.e. all TFDs in front of
890          * block-ack window (we assume that they've been successfully
891          * transmitted ... if not, it's too late anyway).
892          */
893         iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
894                           &reclaimed_skbs);
895
896         IWL_DEBUG_TX_REPLY(mvm,
897                            "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
898                            (u8 *)&ba_notif->sta_addr_lo32,
899                            ba_notif->sta_id);
900         IWL_DEBUG_TX_REPLY(mvm,
901                            "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
902                            ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
903                            (unsigned long long)le64_to_cpu(ba_notif->bitmap),
904                            scd_flow, ba_resp_scd_ssn, ba_notif->txed,
905                            ba_notif->txed_2_done);
906
907         tid_data->next_reclaimed = ba_resp_scd_ssn;
908
909         iwl_mvm_check_ratid_empty(mvm, sta, tid);
910
911         freed = 0;
912
913         skb_queue_walk(&reclaimed_skbs, skb) {
914                 hdr = (struct ieee80211_hdr *)skb->data;
915
916                 if (ieee80211_is_data_qos(hdr->frame_control))
917                         freed++;
918                 else
919                         WARN_ON_ONCE(1);
920
921                 info = IEEE80211_SKB_CB(skb);
922                 iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
923
924                 if (freed == 1) {
925                         /* this is the first skb we deliver in this batch */
926                         /* put the rate scaling data there */
927                         info = IEEE80211_SKB_CB(skb);
928                         memset(&info->status, 0, sizeof(info->status));
929                         info->flags |= IEEE80211_TX_STAT_ACK;
930                         info->flags |= IEEE80211_TX_STAT_AMPDU;
931                         info->status.ampdu_ack_len = ba_notif->txed_2_done;
932                         info->status.ampdu_len = ba_notif->txed;
933                         iwl_mvm_hwrate_to_tx_status(tid_data->rate_n_flags,
934                                                     info);
935                 }
936         }
937
938         spin_unlock_bh(&mvmsta->lock);
939
940         rcu_read_unlock();
941
942         while (!skb_queue_empty(&reclaimed_skbs)) {
943                 skb = __skb_dequeue(&reclaimed_skbs);
944                 ieee80211_tx_status_ni(mvm->hw, skb);
945         }
946
947         return 0;
948 }
949
950 int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, bool sync)
951 {
952         int ret;
953         struct iwl_tx_path_flush_cmd flush_cmd = {
954                 .queues_ctl = cpu_to_le32(tfd_msk),
955                 .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
956         };
957
958         u32 flags = sync ? CMD_SYNC : CMD_ASYNC;
959
960         ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
961                                    sizeof(flush_cmd), &flush_cmd);
962         if (ret)
963                 IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
964         return ret;
965 }