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