Merge remote-tracking branch 'asoc/topic/rcar' into asoc-next
[cascardo/linux.git] / drivers / net / wireless / intel / 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  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016        Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24  * USA
25  *
26  * The full GNU General Public License is included in this distribution
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  *
43  *  * Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  *  * Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in
47  *    the documentation and/or other materials provided with the
48  *    distribution.
49  *  * Neither the name Intel Corporation nor the names of its
50  *    contributors may be used to endorse or promote products derived
51  *    from this software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
63  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  *****************************************************************************/
66 #include <linux/ieee80211.h>
67 #include <linux/etherdevice.h>
68 #include <linux/tcp.h>
69 #include <net/ip.h>
70
71 #include "iwl-trans.h"
72 #include "iwl-eeprom-parse.h"
73 #include "mvm.h"
74 #include "sta.h"
75 #include "fw-dbg.h"
76
77 static void
78 iwl_mvm_bar_check_trigger(struct iwl_mvm *mvm, const u8 *addr,
79                           u16 tid, u16 ssn)
80 {
81         struct iwl_fw_dbg_trigger_tlv *trig;
82         struct iwl_fw_dbg_trigger_ba *ba_trig;
83
84         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
85                 return;
86
87         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
88         ba_trig = (void *)trig->data;
89
90         if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
91                 return;
92
93         if (!(le16_to_cpu(ba_trig->tx_bar) & BIT(tid)))
94                 return;
95
96         iwl_mvm_fw_dbg_collect_trig(mvm, trig,
97                                     "BAR sent to %pM, tid %d, ssn %d",
98                                     addr, tid, ssn);
99 }
100
101 /*
102  * Sets most of the Tx cmd's fields
103  */
104 void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
105                         struct iwl_tx_cmd *tx_cmd,
106                         struct ieee80211_tx_info *info, u8 sta_id)
107 {
108         struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
109         struct ieee80211_hdr *hdr = (void *)skb->data;
110         __le16 fc = hdr->frame_control;
111         u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
112         u32 len = skb->len + FCS_LEN;
113         u8 ac;
114
115         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
116                 tx_flags |= TX_CMD_FLG_ACK;
117         else
118                 tx_flags &= ~TX_CMD_FLG_ACK;
119
120         if (ieee80211_is_probe_resp(fc))
121                 tx_flags |= TX_CMD_FLG_TSF;
122
123         if (ieee80211_has_morefrags(fc))
124                 tx_flags |= TX_CMD_FLG_MORE_FRAG;
125
126         if (ieee80211_is_data_qos(fc)) {
127                 u8 *qc = ieee80211_get_qos_ctl(hdr);
128                 tx_cmd->tid_tspec = qc[0] & 0xf;
129                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
130         } else if (ieee80211_is_back_req(fc)) {
131                 struct ieee80211_bar *bar = (void *)skb->data;
132                 u16 control = le16_to_cpu(bar->control);
133                 u16 ssn = le16_to_cpu(bar->start_seq_num);
134
135                 tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
136                 tx_cmd->tid_tspec = (control &
137                                      IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
138                         IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
139                 WARN_ON_ONCE(tx_cmd->tid_tspec >= IWL_MAX_TID_COUNT);
140                 iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
141                                           ssn);
142         } else {
143                 tx_cmd->tid_tspec = IWL_TID_NON_QOS;
144                 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
145                         tx_flags |= TX_CMD_FLG_SEQ_CTL;
146                 else
147                         tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
148         }
149
150         /* Default to 0 (BE) when tid_spec is set to IWL_TID_NON_QOS */
151         if (tx_cmd->tid_tspec < IWL_MAX_TID_COUNT)
152                 ac = tid_to_mac80211_ac[tx_cmd->tid_tspec];
153         else
154                 ac = tid_to_mac80211_ac[0];
155
156         tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
157                         TX_CMD_FLG_BT_PRIO_POS;
158
159         if (ieee80211_is_mgmt(fc)) {
160                 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
161                         tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_ASSOC);
162                 else if (ieee80211_is_action(fc))
163                         tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
164                 else
165                         tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
166
167                 /* The spec allows Action frames in A-MPDU, we don't support
168                  * it
169                  */
170                 WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
171         } else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
172                 tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
173         } else {
174                 tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
175         }
176
177         if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
178             !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
179                 tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
180
181         if (fw_has_capa(&mvm->fw->ucode_capa,
182                         IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
183             ieee80211_action_contains_tpc(skb))
184                 tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;
185
186         tx_cmd->tx_flags = cpu_to_le32(tx_flags);
187         /* Total # bytes to be transmitted */
188         tx_cmd->len = cpu_to_le16((u16)skb->len +
189                 (uintptr_t)skb_info->driver_data[0]);
190         tx_cmd->next_frame_len = 0;
191         tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
192         tx_cmd->sta_id = sta_id;
193 }
194
195 /*
196  * Sets the fields in the Tx cmd that are rate related
197  */
198 void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm, struct iwl_tx_cmd *tx_cmd,
199                             struct ieee80211_tx_info *info,
200                             struct ieee80211_sta *sta, __le16 fc)
201 {
202         u32 rate_flags;
203         int rate_idx;
204         u8 rate_plcp;
205
206         /* Set retry limit on RTS packets */
207         tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
208
209         /* Set retry limit on DATA packets and Probe Responses*/
210         if (ieee80211_is_probe_resp(fc)) {
211                 tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
212                 tx_cmd->rts_retry_limit =
213                         min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
214         } else if (ieee80211_is_back_req(fc)) {
215                 tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
216         } else {
217                 tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
218         }
219
220         /*
221          * for data packets, rate info comes from the table inside the fw. This
222          * table is controlled by LINK_QUALITY commands
223          */
224
225         if (ieee80211_is_data(fc) && sta) {
226                 tx_cmd->initial_rate_index = 0;
227                 tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
228                 return;
229         } else if (ieee80211_is_back_req(fc)) {
230                 tx_cmd->tx_flags |=
231                         cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
232         }
233
234         /* HT rate doesn't make sense for a non data frame */
235         WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
236                   "Got an HT rate (flags:0x%x/mcs:%d) for a non data frame (fc:0x%x)\n",
237                   info->control.rates[0].flags,
238                   info->control.rates[0].idx,
239                   le16_to_cpu(fc));
240
241         rate_idx = info->control.rates[0].idx;
242         /* if the rate isn't a well known legacy rate, take the lowest one */
243         if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT_LEGACY)
244                 rate_idx = rate_lowest_index(
245                                 &mvm->nvm_data->bands[info->band], sta);
246
247         /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
248         if (info->band == IEEE80211_BAND_5GHZ)
249                 rate_idx += IWL_FIRST_OFDM_RATE;
250
251         /* For 2.4 GHZ band, check that there is no need to remap */
252         BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
253
254         /* Get PLCP rate for tx_cmd->rate_n_flags */
255         rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
256
257         mvm->mgmt_last_antenna_idx =
258                 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
259                                      mvm->mgmt_last_antenna_idx);
260
261         if (info->band == IEEE80211_BAND_2GHZ &&
262             !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
263                 rate_flags = mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;
264         else
265                 rate_flags =
266                         BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
267
268         /* Set CCK flag as needed */
269         if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
270                 rate_flags |= RATE_MCS_CCK_MSK;
271
272         /* Set the rate in the TX cmd */
273         tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
274 }
275
276 /*
277  * Sets the fields in the Tx cmd that are crypto related
278  */
279 static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
280                                       struct ieee80211_tx_info *info,
281                                       struct iwl_tx_cmd *tx_cmd,
282                                       struct sk_buff *skb_frag,
283                                       int hdrlen)
284 {
285         struct ieee80211_key_conf *keyconf = info->control.hw_key;
286         u8 *crypto_hdr = skb_frag->data + hdrlen;
287         u64 pn;
288
289         switch (keyconf->cipher) {
290         case WLAN_CIPHER_SUITE_CCMP:
291         case WLAN_CIPHER_SUITE_CCMP_256:
292                 iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
293                 pn = atomic64_inc_return(&keyconf->tx_pn);
294                 crypto_hdr[0] = pn;
295                 crypto_hdr[2] = 0;
296                 crypto_hdr[3] = 0x20 | (keyconf->keyidx << 6);
297                 crypto_hdr[1] = pn >> 8;
298                 crypto_hdr[4] = pn >> 16;
299                 crypto_hdr[5] = pn >> 24;
300                 crypto_hdr[6] = pn >> 32;
301                 crypto_hdr[7] = pn >> 40;
302                 break;
303
304         case WLAN_CIPHER_SUITE_TKIP:
305                 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
306                 pn = atomic64_inc_return(&keyconf->tx_pn);
307                 ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
308                 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
309                 break;
310
311         case WLAN_CIPHER_SUITE_WEP104:
312                 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
313                 /* fall through */
314         case WLAN_CIPHER_SUITE_WEP40:
315                 tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
316                         ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
317                           TX_CMD_SEC_WEP_KEY_IDX_MSK);
318
319                 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
320                 break;
321         default:
322                 tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
323         }
324 }
325
326 /*
327  * Allocates and sets the Tx cmd the driver data pointers in the skb
328  */
329 static struct iwl_device_cmd *
330 iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
331                       struct ieee80211_tx_info *info, int hdrlen,
332                       struct ieee80211_sta *sta, u8 sta_id)
333 {
334         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
335         struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
336         struct iwl_device_cmd *dev_cmd;
337         struct iwl_tx_cmd *tx_cmd;
338
339         dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
340
341         if (unlikely(!dev_cmd))
342                 return NULL;
343
344         memset(dev_cmd, 0, sizeof(*dev_cmd));
345         dev_cmd->hdr.cmd = TX_CMD;
346         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
347
348         if (info->control.hw_key)
349                 iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
350
351         iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
352
353         iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
354
355         memset(&skb_info->status, 0, sizeof(skb_info->status));
356         memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
357
358         skb_info->driver_data[1] = dev_cmd;
359
360         return dev_cmd;
361 }
362
363 int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
364 {
365         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
366         struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
367         struct ieee80211_tx_info info;
368         struct iwl_device_cmd *dev_cmd;
369         struct iwl_tx_cmd *tx_cmd;
370         u8 sta_id;
371         int hdrlen = ieee80211_hdrlen(hdr->frame_control);
372
373         memcpy(&info, skb->cb, sizeof(info));
374
375         if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
376                 return -1;
377
378         if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
379                          (!info.control.vif ||
380                           info.hw_queue != info.control.vif->cab_queue)))
381                 return -1;
382
383         /* This holds the amsdu headers length */
384         skb_info->driver_data[0] = (void *)(uintptr_t)0;
385
386         /*
387          * IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets that can be used
388          * in 2 different types of vifs, P2P & STATION. P2P uses the offchannel
389          * queue. STATION (HS2.0) uses the auxiliary context of the FW,
390          * and hence needs to be sent on the aux queue
391          */
392         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
393             info.control.vif->type == NL80211_IFTYPE_STATION)
394                 IEEE80211_SKB_CB(skb)->hw_queue = mvm->aux_queue;
395
396         /*
397          * If the interface on which the frame is sent is the P2P_DEVICE
398          * or an AP/GO interface use the broadcast station associated
399          * with it; otherwise if the interface is a managed interface
400          * use the AP station associated with it for multicast traffic
401          * (this is not possible for unicast packets as a TLDS discovery
402          * response are sent without a station entry); otherwise use the
403          * AUX station.
404          */
405         sta_id = mvm->aux_sta.sta_id;
406         if (info.control.vif) {
407                 struct iwl_mvm_vif *mvmvif =
408                         iwl_mvm_vif_from_mac80211(info.control.vif);
409
410                 if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
411                     info.control.vif->type == NL80211_IFTYPE_AP)
412                         sta_id = mvmvif->bcast_sta.sta_id;
413                 else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
414                          is_multicast_ether_addr(hdr->addr1)) {
415                         u8 ap_sta_id = ACCESS_ONCE(mvmvif->ap_sta_id);
416
417                         if (ap_sta_id != IWL_MVM_STATION_COUNT)
418                                 sta_id = ap_sta_id;
419                 }
420         }
421
422         IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info.hw_queue);
423
424         dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
425         if (!dev_cmd)
426                 return -1;
427
428         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
429
430         /* Copy MAC header from skb into command buffer */
431         memcpy(tx_cmd->hdr, hdr, hdrlen);
432
433         if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info.hw_queue)) {
434                 iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
435                 return -1;
436         }
437
438         /*
439          * Increase the pending frames counter, so that later when a reply comes
440          * in and the counter is decreased - we don't start getting negative
441          * values.
442          * Note that we don't need to make sure it isn't agg'd, since we're
443          * TXing non-sta
444          */
445         atomic_inc(&mvm->pending_frames[sta_id]);
446
447         return 0;
448 }
449
450 #ifdef CONFIG_INET
451 static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
452                           struct ieee80211_tx_info *info,
453                           struct ieee80211_sta *sta,
454                           struct sk_buff_head *mpdus_skb)
455 {
456         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
457         struct ieee80211_hdr *hdr = (void *)skb->data;
458         unsigned int mss = skb_shinfo(skb)->gso_size;
459         struct sk_buff *tmp, *next;
460         char cb[sizeof(skb->cb)];
461         unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
462         bool ipv4 = (skb->protocol == htons(ETH_P_IP));
463         u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;
464         u16 amsdu_add, snap_ip_tcp, pad, i = 0;
465         unsigned int dbg_max_amsdu_len;
466         u8 *qc, tid, txf;
467
468         snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
469                 tcp_hdrlen(skb);
470
471         qc = ieee80211_get_qos_ctl(hdr);
472         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
473         if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
474                 return -EINVAL;
475
476         if (!sta->max_amsdu_len ||
477             !ieee80211_is_data_qos(hdr->frame_control) ||
478             !mvmsta->tlc_amsdu) {
479                 num_subframes = 1;
480                 pad = 0;
481                 goto segment;
482         }
483
484         /*
485          * No need to lock amsdu_in_ampdu_allowed since it can't be modified
486          * during an BA session.
487          */
488         if (info->flags & IEEE80211_TX_CTL_AMPDU &&
489             !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed) {
490                 num_subframes = 1;
491                 pad = 0;
492                 goto segment;
493         }
494
495         max_amsdu_len = sta->max_amsdu_len;
496         dbg_max_amsdu_len = ACCESS_ONCE(mvm->max_amsdu_len);
497
498         /* the Tx FIFO to which this A-MSDU will be routed */
499         txf = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
500
501         /*
502          * Don't send an AMSDU that will be longer than the TXF.
503          * Add a security margin of 256 for the TX command + headers.
504          * We also want to have the start of the next packet inside the
505          * fifo to be able to send bursts.
506          */
507         max_amsdu_len = min_t(unsigned int, max_amsdu_len,
508                               mvm->shared_mem_cfg.txfifo_size[txf] - 256);
509
510         if (dbg_max_amsdu_len)
511                 max_amsdu_len = min_t(unsigned int, max_amsdu_len,
512                                       dbg_max_amsdu_len);
513
514         /*
515          * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
516          * supported. This is a spec requirement (IEEE 802.11-2015
517          * section 8.7.3 NOTE 3).
518          */
519         if (info->flags & IEEE80211_TX_CTL_AMPDU &&
520             !sta->vht_cap.vht_supported)
521                 max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);
522
523         /* Sub frame header + SNAP + IP header + TCP header + MSS */
524         subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
525         pad = (4 - subf_len) & 0x3;
526
527         /*
528          * If we have N subframes in the A-MSDU, then the A-MSDU's size is
529          * N * subf_len + (N - 1) * pad.
530          */
531         num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
532         if (num_subframes > 1)
533                 *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
534
535         tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
536                 tcp_hdrlen(skb) + skb->data_len;
537
538         /*
539          * Make sure we have enough TBs for the A-MSDU:
540          *      2 for each subframe
541          *      1 more for each fragment
542          *      1 more for the potential data in the header
543          */
544         num_subframes =
545                 min_t(unsigned int, num_subframes,
546                       (mvm->trans->max_skb_frags - 1 -
547                        skb_shinfo(skb)->nr_frags) / 2);
548
549         /* This skb fits in one single A-MSDU */
550         if (num_subframes * mss >= tcp_payload_len) {
551                 struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
552
553                 /*
554                  * Compute the length of all the data added for the A-MSDU.
555                  * This will be used to compute the length to write in the TX
556                  * command. We have: SNAP + IP + TCP for n -1 subframes and
557                  * ETH header for n subframes. Note that the original skb
558                  * already had one set of SNAP / IP / TCP headers.
559                  */
560                 num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
561                 amsdu_add = num_subframes * sizeof(struct ethhdr) +
562                         (num_subframes - 1) * (snap_ip_tcp + pad);
563                 /* This holds the amsdu headers length */
564                 skb_info->driver_data[0] = (void *)(uintptr_t)amsdu_add;
565
566                 __skb_queue_tail(mpdus_skb, skb);
567                 return 0;
568         }
569
570         /*
571          * Trick the segmentation function to make it
572          * create SKBs that can fit into one A-MSDU.
573          */
574 segment:
575         skb_shinfo(skb)->gso_size = num_subframes * mss;
576         memcpy(cb, skb->cb, sizeof(cb));
577
578         next = skb_gso_segment(skb, NETIF_F_CSUM_MASK | NETIF_F_SG);
579         skb_shinfo(skb)->gso_size = mss;
580         if (WARN_ON_ONCE(IS_ERR(next)))
581                 return -EINVAL;
582         else if (next)
583                 consume_skb(skb);
584
585         while (next) {
586                 tmp = next;
587                 next = tmp->next;
588
589                 memcpy(tmp->cb, cb, sizeof(tmp->cb));
590                 /*
591                  * Compute the length of all the data added for the A-MSDU.
592                  * This will be used to compute the length to write in the TX
593                  * command. We have: SNAP + IP + TCP for n -1 subframes and
594                  * ETH header for n subframes.
595                  */
596                 tcp_payload_len = skb_tail_pointer(tmp) -
597                         skb_transport_header(tmp) -
598                         tcp_hdrlen(tmp) + tmp->data_len;
599
600                 if (ipv4)
601                         ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);
602
603                 if (tcp_payload_len > mss) {
604                         struct ieee80211_tx_info *skb_info =
605                                 IEEE80211_SKB_CB(tmp);
606
607                         num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
608                         amsdu_add = num_subframes * sizeof(struct ethhdr) +
609                                 (num_subframes - 1) * (snap_ip_tcp + pad);
610                         skb_info->driver_data[0] =
611                                 (void *)(uintptr_t)amsdu_add;
612                         skb_shinfo(tmp)->gso_size = mss;
613                 } else {
614                         qc = ieee80211_get_qos_ctl((void *)tmp->data);
615
616                         if (ipv4)
617                                 ip_send_check(ip_hdr(tmp));
618                         *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
619                         skb_shinfo(tmp)->gso_size = 0;
620                 }
621
622                 tmp->prev = NULL;
623                 tmp->next = NULL;
624
625                 __skb_queue_tail(mpdus_skb, tmp);
626                 i++;
627         }
628
629         return 0;
630 }
631 #else /* CONFIG_INET */
632 static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
633                           struct ieee80211_tx_info *info,
634                           struct ieee80211_sta *sta,
635                           struct sk_buff_head *mpdus_skb)
636 {
637         /* Impossible to get TSO with CONFIG_INET */
638         WARN_ON(1);
639
640         return -1;
641 }
642 #endif
643
644 /*
645  * Sets the fields in the Tx cmd that are crypto related
646  */
647 static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
648                            struct ieee80211_tx_info *info,
649                            struct ieee80211_sta *sta)
650 {
651         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
652         struct iwl_mvm_sta *mvmsta;
653         struct iwl_device_cmd *dev_cmd;
654         struct iwl_tx_cmd *tx_cmd;
655         __le16 fc;
656         u16 seq_number = 0;
657         u8 tid = IWL_MAX_TID_COUNT;
658         u8 txq_id = info->hw_queue;
659         bool is_data_qos = false, is_ampdu = false;
660         int hdrlen;
661
662         mvmsta = iwl_mvm_sta_from_mac80211(sta);
663         fc = hdr->frame_control;
664         hdrlen = ieee80211_hdrlen(fc);
665
666         if (WARN_ON_ONCE(!mvmsta))
667                 return -1;
668
669         if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
670                 return -1;
671
672         dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
673                                         sta, mvmsta->sta_id);
674         if (!dev_cmd)
675                 goto drop;
676
677         tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
678         /* From now on, we cannot access info->control */
679
680         /*
681          * we handle that entirely ourselves -- for uAPSD the firmware
682          * will always send a notification, and for PS-Poll responses
683          * we'll notify mac80211 when getting frame status
684          */
685         info->flags &= ~IEEE80211_TX_STATUS_EOSP;
686
687         spin_lock(&mvmsta->lock);
688
689         if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
690                 u8 *qc = NULL;
691                 qc = ieee80211_get_qos_ctl(hdr);
692                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
693                 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
694                         goto drop_unlock_sta;
695
696                 seq_number = mvmsta->tid_data[tid].seq_number;
697                 seq_number &= IEEE80211_SCTL_SEQ;
698                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
699                 hdr->seq_ctrl |= cpu_to_le16(seq_number);
700                 is_data_qos = true;
701                 is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
702         }
703
704         /* Copy MAC header from skb into command buffer */
705         memcpy(tx_cmd->hdr, hdr, hdrlen);
706
707         WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
708
709         if (sta->tdls) {
710                 /* default to TID 0 for non-QoS packets */
711                 u8 tdls_tid = tid == IWL_MAX_TID_COUNT ? 0 : tid;
712
713                 txq_id = mvmsta->hw_queue[tid_to_mac80211_ac[tdls_tid]];
714         }
715
716         if (is_ampdu) {
717                 if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
718                         goto drop_unlock_sta;
719                 txq_id = mvmsta->tid_data[tid].txq_id;
720         }
721
722         IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
723                      tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
724
725         if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
726                 goto drop_unlock_sta;
727
728         if (is_data_qos && !ieee80211_has_morefrags(fc))
729                 mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
730
731         spin_unlock(&mvmsta->lock);
732
733         if (txq_id < mvm->first_agg_queue)
734                 atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
735
736         return 0;
737
738 drop_unlock_sta:
739         iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
740         spin_unlock(&mvmsta->lock);
741 drop:
742         return -1;
743 }
744
745 int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
746                    struct ieee80211_sta *sta)
747 {
748         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
749         struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
750         struct ieee80211_tx_info info;
751         struct sk_buff_head mpdus_skbs;
752         unsigned int payload_len;
753         int ret;
754
755         if (WARN_ON_ONCE(!mvmsta))
756                 return -1;
757
758         if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
759                 return -1;
760
761         memcpy(&info, skb->cb, sizeof(info));
762
763         /* This holds the amsdu headers length */
764         skb_info->driver_data[0] = (void *)(uintptr_t)0;
765
766         if (!skb_is_gso(skb))
767                 return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
768
769         payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
770                 tcp_hdrlen(skb) + skb->data_len;
771
772         if (payload_len <= skb_shinfo(skb)->gso_size)
773                 return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
774
775         __skb_queue_head_init(&mpdus_skbs);
776
777         ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
778         if (ret)
779                 return ret;
780
781         if (WARN_ON(skb_queue_empty(&mpdus_skbs)))
782                 return ret;
783
784         while (!skb_queue_empty(&mpdus_skbs)) {
785                 skb = __skb_dequeue(&mpdus_skbs);
786
787                 ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
788                 if (ret) {
789                         __skb_queue_purge(&mpdus_skbs);
790                         return ret;
791                 }
792         }
793
794         return 0;
795 }
796
797 static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
798                                       struct ieee80211_sta *sta, u8 tid)
799 {
800         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
801         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
802         struct ieee80211_vif *vif = mvmsta->vif;
803
804         lockdep_assert_held(&mvmsta->lock);
805
806         if ((tid_data->state == IWL_AGG_ON ||
807              tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
808             iwl_mvm_tid_queued(tid_data) == 0) {
809                 /*
810                  * Now that this aggregation queue is empty tell mac80211 so it
811                  * knows we no longer have frames buffered for the station on
812                  * this TID (for the TIM bitmap calculation.)
813                  */
814                 ieee80211_sta_set_buffered(sta, tid, false);
815         }
816
817         if (tid_data->ssn != tid_data->next_reclaimed)
818                 return;
819
820         switch (tid_data->state) {
821         case IWL_EMPTYING_HW_QUEUE_ADDBA:
822                 IWL_DEBUG_TX_QUEUES(mvm,
823                                     "Can continue addBA flow ssn = next_recl = %d\n",
824                                     tid_data->next_reclaimed);
825                 tid_data->state = IWL_AGG_STARTING;
826                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
827                 break;
828
829         case IWL_EMPTYING_HW_QUEUE_DELBA:
830                 IWL_DEBUG_TX_QUEUES(mvm,
831                                     "Can continue DELBA flow ssn = next_recl = %d\n",
832                                     tid_data->next_reclaimed);
833                 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
834                                     vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
835                                     CMD_ASYNC);
836                 tid_data->state = IWL_AGG_OFF;
837                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
838                 break;
839
840         default:
841                 break;
842         }
843 }
844
845 #ifdef CONFIG_IWLWIFI_DEBUG
846 const char *iwl_mvm_get_tx_fail_reason(u32 status)
847 {
848 #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
849 #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
850
851         switch (status & TX_STATUS_MSK) {
852         case TX_STATUS_SUCCESS:
853                 return "SUCCESS";
854         TX_STATUS_POSTPONE(DELAY);
855         TX_STATUS_POSTPONE(FEW_BYTES);
856         TX_STATUS_POSTPONE(BT_PRIO);
857         TX_STATUS_POSTPONE(QUIET_PERIOD);
858         TX_STATUS_POSTPONE(CALC_TTAK);
859         TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
860         TX_STATUS_FAIL(SHORT_LIMIT);
861         TX_STATUS_FAIL(LONG_LIMIT);
862         TX_STATUS_FAIL(UNDERRUN);
863         TX_STATUS_FAIL(DRAIN_FLOW);
864         TX_STATUS_FAIL(RFKILL_FLUSH);
865         TX_STATUS_FAIL(LIFE_EXPIRE);
866         TX_STATUS_FAIL(DEST_PS);
867         TX_STATUS_FAIL(HOST_ABORTED);
868         TX_STATUS_FAIL(BT_RETRY);
869         TX_STATUS_FAIL(STA_INVALID);
870         TX_STATUS_FAIL(FRAG_DROPPED);
871         TX_STATUS_FAIL(TID_DISABLE);
872         TX_STATUS_FAIL(FIFO_FLUSHED);
873         TX_STATUS_FAIL(SMALL_CF_POLL);
874         TX_STATUS_FAIL(FW_DROP);
875         TX_STATUS_FAIL(STA_COLOR_MISMATCH);
876         }
877
878         return "UNKNOWN";
879
880 #undef TX_STATUS_FAIL
881 #undef TX_STATUS_POSTPONE
882 }
883 #endif /* CONFIG_IWLWIFI_DEBUG */
884
885 void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
886                                enum ieee80211_band band,
887                                struct ieee80211_tx_rate *r)
888 {
889         if (rate_n_flags & RATE_HT_MCS_GF_MSK)
890                 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
891         switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
892         case RATE_MCS_CHAN_WIDTH_20:
893                 break;
894         case RATE_MCS_CHAN_WIDTH_40:
895                 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
896                 break;
897         case RATE_MCS_CHAN_WIDTH_80:
898                 r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
899                 break;
900         case RATE_MCS_CHAN_WIDTH_160:
901                 r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
902                 break;
903         }
904         if (rate_n_flags & RATE_MCS_SGI_MSK)
905                 r->flags |= IEEE80211_TX_RC_SHORT_GI;
906         if (rate_n_flags & RATE_MCS_HT_MSK) {
907                 r->flags |= IEEE80211_TX_RC_MCS;
908                 r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
909         } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
910                 ieee80211_rate_set_vht(
911                         r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
912                         ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
913                                                 RATE_VHT_MCS_NSS_POS) + 1);
914                 r->flags |= IEEE80211_TX_RC_VHT_MCS;
915         } else {
916                 r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
917                                                              band);
918         }
919 }
920
921 /**
922  * translate ucode response to mac80211 tx status control values
923  */
924 static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
925                                         struct ieee80211_tx_info *info)
926 {
927         struct ieee80211_tx_rate *r = &info->status.rates[0];
928
929         info->status.antenna =
930                 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
931         iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
932 }
933
934 static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
935                                             u32 status)
936 {
937         struct iwl_fw_dbg_trigger_tlv *trig;
938         struct iwl_fw_dbg_trigger_tx_status *status_trig;
939         int i;
940
941         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TX_STATUS))
942                 return;
943
944         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TX_STATUS);
945         status_trig = (void *)trig->data;
946
947         if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
948                 return;
949
950         for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
951                 /* don't collect on status 0 */
952                 if (!status_trig->statuses[i].status)
953                         break;
954
955                 if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
956                         continue;
957
958                 iwl_mvm_fw_dbg_collect_trig(mvm, trig,
959                                             "Tx status %d was received",
960                                             status & TX_STATUS_MSK);
961                 break;
962         }
963 }
964
965 static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
966                                      struct iwl_rx_packet *pkt)
967 {
968         struct ieee80211_sta *sta;
969         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
970         int txq_id = SEQ_TO_QUEUE(sequence);
971         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
972         int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
973         int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
974         u32 status = le16_to_cpu(tx_resp->status.status);
975         u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
976         struct iwl_mvm_sta *mvmsta;
977         struct sk_buff_head skbs;
978         u8 skb_freed = 0;
979         u16 next_reclaimed, seq_ctl;
980         bool is_ndp = false;
981
982         __skb_queue_head_init(&skbs);
983
984         seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
985
986         /* we can free until ssn % q.n_bd not inclusive */
987         iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
988
989         while (!skb_queue_empty(&skbs)) {
990                 struct sk_buff *skb = __skb_dequeue(&skbs);
991                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
992
993                 skb_freed++;
994
995                 iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
996
997                 memset(&info->status, 0, sizeof(info->status));
998
999                 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1000
1001                 /* inform mac80211 about what happened with the frame */
1002                 switch (status & TX_STATUS_MSK) {
1003                 case TX_STATUS_SUCCESS:
1004                 case TX_STATUS_DIRECT_DONE:
1005                         info->flags |= IEEE80211_TX_STAT_ACK;
1006                         break;
1007                 case TX_STATUS_FAIL_DEST_PS:
1008                         info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1009                         break;
1010                 default:
1011                         break;
1012                 }
1013
1014                 iwl_mvm_tx_status_check_trigger(mvm, status);
1015
1016                 info->status.rates[0].count = tx_resp->failure_frame + 1;
1017                 iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
1018                                             info);
1019                 info->status.status_driver_data[1] =
1020                         (void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
1021
1022                 /* Single frame failure in an AMPDU queue => send BAR */
1023                 if (txq_id >= mvm->first_agg_queue &&
1024                     !(info->flags & IEEE80211_TX_STAT_ACK) &&
1025                     !(info->flags & IEEE80211_TX_STAT_TX_FILTERED))
1026                         info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1027
1028                 /* W/A FW bug: seq_ctl is wrong when the status isn't success */
1029                 if (status != TX_STATUS_SUCCESS) {
1030                         struct ieee80211_hdr *hdr = (void *)skb->data;
1031                         seq_ctl = le16_to_cpu(hdr->seq_ctrl);
1032                 }
1033
1034                 if (unlikely(!seq_ctl)) {
1035                         struct ieee80211_hdr *hdr = (void *)skb->data;
1036
1037                         /*
1038                          * If it is an NDP, we can't update next_reclaim since
1039                          * its sequence control is 0. Note that for that same
1040                          * reason, NDPs are never sent to A-MPDU'able queues
1041                          * so that we can never have more than one freed frame
1042                          * for a single Tx resonse (see WARN_ON below).
1043                          */
1044                         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
1045                                 is_ndp = true;
1046                 }
1047
1048                 /*
1049                  * TODO: this is not accurate if we are freeing more than one
1050                  * packet.
1051                  */
1052                 info->status.tx_time =
1053                         le16_to_cpu(tx_resp->wireless_media_time);
1054                 BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
1055                 info->status.status_driver_data[0] =
1056                                 (void *)(uintptr_t)tx_resp->reduced_tpc;
1057
1058                 ieee80211_tx_status(mvm->hw, skb);
1059         }
1060
1061         if (txq_id >= mvm->first_agg_queue) {
1062                 /* If this is an aggregation queue, we use the ssn since:
1063                  * ssn = wifi seq_num % 256.
1064                  * The seq_ctl is the sequence control of the packet to which
1065                  * this Tx response relates. But if there is a hole in the
1066                  * bitmap of the BA we received, this Tx response may allow to
1067                  * reclaim the hole and all the subsequent packets that were
1068                  * already acked. In that case, seq_ctl != ssn, and the next
1069                  * packet to be reclaimed will be ssn and not seq_ctl. In that
1070                  * case, several packets will be reclaimed even if
1071                  * frame_count = 1.
1072                  *
1073                  * The ssn is the index (% 256) of the latest packet that has
1074                  * treated (acked / dropped) + 1.
1075                  */
1076                 next_reclaimed = ssn;
1077         } else {
1078                 /* The next packet to be reclaimed is the one after this one */
1079                 next_reclaimed = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
1080         }
1081
1082         IWL_DEBUG_TX_REPLY(mvm,
1083                            "TXQ %d status %s (0x%08x)\n",
1084                            txq_id, iwl_mvm_get_tx_fail_reason(status), status);
1085
1086         IWL_DEBUG_TX_REPLY(mvm,
1087                            "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
1088                            le32_to_cpu(tx_resp->initial_rate),
1089                            tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
1090                            ssn, next_reclaimed, seq_ctl);
1091
1092         rcu_read_lock();
1093
1094         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1095         /*
1096          * sta can't be NULL otherwise it'd mean that the sta has been freed in
1097          * the firmware while we still have packets for it in the Tx queues.
1098          */
1099         if (WARN_ON_ONCE(!sta))
1100                 goto out;
1101
1102         if (!IS_ERR(sta)) {
1103                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1104
1105                 if (tid != IWL_TID_NON_QOS) {
1106                         struct iwl_mvm_tid_data *tid_data =
1107                                 &mvmsta->tid_data[tid];
1108                         bool send_eosp_ndp = false;
1109
1110                         spin_lock_bh(&mvmsta->lock);
1111                         if (!is_ndp) {
1112                                 tid_data->next_reclaimed = next_reclaimed;
1113                                 IWL_DEBUG_TX_REPLY(mvm,
1114                                                    "Next reclaimed packet:%d\n",
1115                                                    next_reclaimed);
1116                         } else {
1117                                 IWL_DEBUG_TX_REPLY(mvm,
1118                                                    "NDP - don't update next_reclaimed\n");
1119                         }
1120
1121                         iwl_mvm_check_ratid_empty(mvm, sta, tid);
1122
1123                         if (mvmsta->sleep_tx_count) {
1124                                 mvmsta->sleep_tx_count--;
1125                                 if (mvmsta->sleep_tx_count &&
1126                                     !iwl_mvm_tid_queued(tid_data)) {
1127                                         /*
1128                                          * The number of frames in the queue
1129                                          * dropped to 0 even if we sent less
1130                                          * frames than we thought we had on the
1131                                          * Tx queue.
1132                                          * This means we had holes in the BA
1133                                          * window that we just filled, ask
1134                                          * mac80211 to send EOSP since the
1135                                          * firmware won't know how to do that.
1136                                          * Send NDP and the firmware will send
1137                                          * EOSP notification that will trigger
1138                                          * a call to ieee80211_sta_eosp().
1139                                          */
1140                                         send_eosp_ndp = true;
1141                                 }
1142                         }
1143
1144                         spin_unlock_bh(&mvmsta->lock);
1145                         if (send_eosp_ndp) {
1146                                 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
1147                                         IEEE80211_FRAME_RELEASE_UAPSD,
1148                                         1, tid, false, false);
1149                                 mvmsta->sleep_tx_count = 0;
1150                                 ieee80211_send_eosp_nullfunc(sta, tid);
1151                         }
1152                 }
1153
1154                 if (mvmsta->next_status_eosp) {
1155                         mvmsta->next_status_eosp = false;
1156                         ieee80211_sta_eosp(sta);
1157                 }
1158         } else {
1159                 mvmsta = NULL;
1160         }
1161
1162         /*
1163          * If the txq is not an AMPDU queue, there is no chance we freed
1164          * several skbs. Check that out...
1165          */
1166         if (txq_id >= mvm->first_agg_queue)
1167                 goto out;
1168
1169         /* We can't free more than one frame at once on a shared queue */
1170         WARN_ON(skb_freed > 1);
1171
1172         /* If we have still frames for this STA nothing to do here */
1173         if (!atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id]))
1174                 goto out;
1175
1176         if (mvmsta && mvmsta->vif->type == NL80211_IFTYPE_AP) {
1177
1178                 /*
1179                  * If there are no pending frames for this STA and
1180                  * the tx to this station is not disabled, notify
1181                  * mac80211 that this station can now wake up in its
1182                  * STA table.
1183                  * If mvmsta is not NULL, sta is valid.
1184                  */
1185
1186                 spin_lock_bh(&mvmsta->lock);
1187
1188                 if (!mvmsta->disable_tx)
1189                         ieee80211_sta_block_awake(mvm->hw, sta, false);
1190
1191                 spin_unlock_bh(&mvmsta->lock);
1192         }
1193
1194         if (PTR_ERR(sta) == -EBUSY || PTR_ERR(sta) == -ENOENT) {
1195                 /*
1196                  * We are draining and this was the last packet - pre_rcu_remove
1197                  * has been called already. We might be after the
1198                  * synchronize_net already.
1199                  * Don't rely on iwl_mvm_rm_sta to see the empty Tx queues.
1200                  */
1201                 set_bit(sta_id, mvm->sta_drained);
1202                 schedule_work(&mvm->sta_drained_wk);
1203         }
1204
1205 out:
1206         rcu_read_unlock();
1207 }
1208
1209 #ifdef CONFIG_IWLWIFI_DEBUG
1210 #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
1211 static const char *iwl_get_agg_tx_status(u16 status)
1212 {
1213         switch (status & AGG_TX_STATE_STATUS_MSK) {
1214         AGG_TX_STATE_(TRANSMITTED);
1215         AGG_TX_STATE_(UNDERRUN);
1216         AGG_TX_STATE_(BT_PRIO);
1217         AGG_TX_STATE_(FEW_BYTES);
1218         AGG_TX_STATE_(ABORT);
1219         AGG_TX_STATE_(LAST_SENT_TTL);
1220         AGG_TX_STATE_(LAST_SENT_TRY_CNT);
1221         AGG_TX_STATE_(LAST_SENT_BT_KILL);
1222         AGG_TX_STATE_(SCD_QUERY);
1223         AGG_TX_STATE_(TEST_BAD_CRC32);
1224         AGG_TX_STATE_(RESPONSE);
1225         AGG_TX_STATE_(DUMP_TX);
1226         AGG_TX_STATE_(DELAY_TX);
1227         }
1228
1229         return "UNKNOWN";
1230 }
1231
1232 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
1233                                       struct iwl_rx_packet *pkt)
1234 {
1235         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1236         struct agg_tx_status *frame_status = &tx_resp->status;
1237         int i;
1238
1239         for (i = 0; i < tx_resp->frame_count; i++) {
1240                 u16 fstatus = le16_to_cpu(frame_status[i].status);
1241
1242                 IWL_DEBUG_TX_REPLY(mvm,
1243                                    "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
1244                                    iwl_get_agg_tx_status(fstatus),
1245                                    fstatus & AGG_TX_STATE_STATUS_MSK,
1246                                    (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
1247                                         AGG_TX_STATE_TRY_CNT_POS,
1248                                    le16_to_cpu(frame_status[i].sequence));
1249         }
1250 }
1251 #else
1252 static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
1253                                       struct iwl_rx_packet *pkt)
1254 {}
1255 #endif /* CONFIG_IWLWIFI_DEBUG */
1256
1257 static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
1258                                   struct iwl_rx_packet *pkt)
1259 {
1260         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1261         int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
1262         int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
1263         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1264         struct ieee80211_sta *sta;
1265
1266         if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < mvm->first_agg_queue))
1267                 return;
1268
1269         if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
1270                 return;
1271
1272         iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
1273
1274         rcu_read_lock();
1275
1276         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1277
1278         if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
1279                 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1280                 mvmsta->tid_data[tid].rate_n_flags =
1281                         le32_to_cpu(tx_resp->initial_rate);
1282                 mvmsta->tid_data[tid].tx_time =
1283                         le16_to_cpu(tx_resp->wireless_media_time);
1284         }
1285
1286         rcu_read_unlock();
1287 }
1288
1289 void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1290 {
1291         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1292         struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
1293
1294         if (tx_resp->frame_count == 1)
1295                 iwl_mvm_rx_tx_cmd_single(mvm, pkt);
1296         else
1297                 iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
1298 }
1299
1300 static void iwl_mvm_tx_info_from_ba_notif(struct ieee80211_tx_info *info,
1301                                           struct iwl_mvm_ba_notif *ba_notif,
1302                                           struct iwl_mvm_tid_data *tid_data)
1303 {
1304         info->flags |= IEEE80211_TX_STAT_AMPDU;
1305         info->status.ampdu_ack_len = ba_notif->txed_2_done;
1306         info->status.ampdu_len = ba_notif->txed;
1307         iwl_mvm_hwrate_to_tx_status(tid_data->rate_n_flags,
1308                                     info);
1309         /* TODO: not accounted if the whole A-MPDU failed */
1310         info->status.tx_time = tid_data->tx_time;
1311         info->status.status_driver_data[0] =
1312                 (void *)(uintptr_t)ba_notif->reduced_txp;
1313         info->status.status_driver_data[1] =
1314                 (void *)(uintptr_t)tid_data->rate_n_flags;
1315 }
1316
1317 void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1318 {
1319         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1320         struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
1321         struct sk_buff_head reclaimed_skbs;
1322         struct iwl_mvm_tid_data *tid_data;
1323         struct ieee80211_sta *sta;
1324         struct iwl_mvm_sta *mvmsta;
1325         struct sk_buff *skb;
1326         int sta_id, tid, freed;
1327         /* "flow" corresponds to Tx queue */
1328         u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
1329         /* "ssn" is start of block-ack Tx window, corresponds to index
1330          * (in Tx queue's circular buffer) of first TFD/frame in window */
1331         u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
1332
1333         sta_id = ba_notif->sta_id;
1334         tid = ba_notif->tid;
1335
1336         if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT ||
1337                       tid >= IWL_MAX_TID_COUNT,
1338                       "sta_id %d tid %d", sta_id, tid))
1339                 return;
1340
1341         rcu_read_lock();
1342
1343         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1344
1345         /* Reclaiming frames for a station that has been deleted ? */
1346         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
1347                 rcu_read_unlock();
1348                 return;
1349         }
1350
1351         mvmsta = iwl_mvm_sta_from_mac80211(sta);
1352         tid_data = &mvmsta->tid_data[tid];
1353
1354         if (tid_data->txq_id != scd_flow) {
1355                 IWL_ERR(mvm,
1356                         "invalid BA notification: Q %d, tid %d, flow %d\n",
1357                         tid_data->txq_id, tid, scd_flow);
1358                 rcu_read_unlock();
1359                 return;
1360         }
1361
1362         spin_lock_bh(&mvmsta->lock);
1363
1364         __skb_queue_head_init(&reclaimed_skbs);
1365
1366         /*
1367          * Release all TFDs before the SSN, i.e. all TFDs in front of
1368          * block-ack window (we assume that they've been successfully
1369          * transmitted ... if not, it's too late anyway).
1370          */
1371         iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
1372                           &reclaimed_skbs);
1373
1374         IWL_DEBUG_TX_REPLY(mvm,
1375                            "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
1376                            (u8 *)&ba_notif->sta_addr_lo32,
1377                            ba_notif->sta_id);
1378         IWL_DEBUG_TX_REPLY(mvm,
1379                            "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
1380                            ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
1381                            (unsigned long long)le64_to_cpu(ba_notif->bitmap),
1382                            scd_flow, ba_resp_scd_ssn, ba_notif->txed,
1383                            ba_notif->txed_2_done);
1384
1385         IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
1386                            ba_notif->reduced_txp);
1387         tid_data->next_reclaimed = ba_resp_scd_ssn;
1388
1389         iwl_mvm_check_ratid_empty(mvm, sta, tid);
1390
1391         freed = 0;
1392
1393         skb_queue_walk(&reclaimed_skbs, skb) {
1394                 struct ieee80211_hdr *hdr = (void *)skb->data;
1395                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1396
1397                 if (ieee80211_is_data_qos(hdr->frame_control))
1398                         freed++;
1399                 else
1400                         WARN_ON_ONCE(1);
1401
1402                 iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
1403
1404                 memset(&info->status, 0, sizeof(info->status));
1405                 /* Packet was transmitted successfully, failures come as single
1406                  * frames because before failing a frame the firmware transmits
1407                  * it without aggregation at least once.
1408                  */
1409                 info->flags |= IEEE80211_TX_STAT_ACK;
1410
1411                 /* this is the first skb we deliver in this batch */
1412                 /* put the rate scaling data there */
1413                 if (freed == 1)
1414                         iwl_mvm_tx_info_from_ba_notif(info, ba_notif, tid_data);
1415         }
1416
1417         spin_unlock_bh(&mvmsta->lock);
1418
1419         /* We got a BA notif with 0 acked or scd_ssn didn't progress which is
1420          * possible (i.e. first MPDU in the aggregation wasn't acked)
1421          * Still it's important to update RS about sent vs. acked.
1422          */
1423         if (skb_queue_empty(&reclaimed_skbs)) {
1424                 struct ieee80211_tx_info ba_info = {};
1425                 struct ieee80211_chanctx_conf *chanctx_conf = NULL;
1426
1427                 if (mvmsta->vif)
1428                         chanctx_conf =
1429                                 rcu_dereference(mvmsta->vif->chanctx_conf);
1430
1431                 if (WARN_ON_ONCE(!chanctx_conf))
1432                         goto out;
1433
1434                 ba_info.band = chanctx_conf->def.chan->band;
1435                 iwl_mvm_tx_info_from_ba_notif(&ba_info, ba_notif, tid_data);
1436
1437                 IWL_DEBUG_TX_REPLY(mvm, "No reclaim. Update rs directly\n");
1438                 iwl_mvm_rs_tx_status(mvm, sta, tid, &ba_info);
1439         }
1440
1441 out:
1442         rcu_read_unlock();
1443
1444         while (!skb_queue_empty(&reclaimed_skbs)) {
1445                 skb = __skb_dequeue(&reclaimed_skbs);
1446                 ieee80211_tx_status(mvm->hw, skb);
1447         }
1448 }
1449
1450 /*
1451  * Note that there are transports that buffer frames before they reach
1452  * the firmware. This means that after flush_tx_path is called, the
1453  * queue might not be empty. The race-free way to handle this is to:
1454  * 1) set the station as draining
1455  * 2) flush the Tx path
1456  * 3) wait for the transport queues to be empty
1457  */
1458 int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, u32 flags)
1459 {
1460         int ret;
1461         struct iwl_tx_path_flush_cmd flush_cmd = {
1462                 .queues_ctl = cpu_to_le32(tfd_msk),
1463                 .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
1464         };
1465
1466         ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
1467                                    sizeof(flush_cmd), &flush_cmd);
1468         if (ret)
1469                 IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
1470         return ret;
1471 }