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) 2015 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <ilw@linux.intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *****************************************************************************/
63 #include <linux/etherdevice.h>
64 #include <linux/skbuff.h>
65 #include "iwl-trans.h"
66 #include "mvm.h"
67 #include "fw-api.h"
68 
69 static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
70 				   int queue, struct ieee80211_sta *sta)
71 {
72 	struct iwl_mvm_sta *mvmsta;
73 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
74 	struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
75 	struct iwl_mvm_key_pn *ptk_pn;
76 	int res;
77 	u8 tid, keyidx;
78 	u8 pn[IEEE80211_CCMP_PN_LEN];
79 	u8 *extiv;
80 
81 	/* do PN checking */
82 
83 	/* multicast and non-data only arrives on default queue */
84 	if (!ieee80211_is_data(hdr->frame_control) ||
85 	    is_multicast_ether_addr(hdr->addr1))
86 		return 0;
87 
88 	/* do not check PN for open AP */
89 	if (!(stats->flag & RX_FLAG_DECRYPTED))
90 		return 0;
91 
92 	/*
93 	 * avoid checking for default queue - we don't want to replicate
94 	 * all the logic that's necessary for checking the PN on fragmented
95 	 * frames, leave that to mac80211
96 	 */
97 	if (queue == 0)
98 		return 0;
99 
100 	/* if we are here - this for sure is either CCMP or GCMP */
101 	if (IS_ERR_OR_NULL(sta)) {
102 		IWL_ERR(mvm,
103 			"expected hw-decrypted unicast frame for station\n");
104 		return -1;
105 	}
106 
107 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
108 
109 	extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
110 	keyidx = extiv[3] >> 6;
111 
112 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
113 	if (!ptk_pn)
114 		return -1;
115 
116 	if (ieee80211_is_data_qos(hdr->frame_control))
117 		tid = ieee80211_get_tid(hdr);
118 	else
119 		tid = 0;
120 
121 	/* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
122 	if (tid >= IWL_MAX_TID_COUNT)
123 		return -1;
124 
125 	/* load pn */
126 	pn[0] = extiv[7];
127 	pn[1] = extiv[6];
128 	pn[2] = extiv[5];
129 	pn[3] = extiv[4];
130 	pn[4] = extiv[1];
131 	pn[5] = extiv[0];
132 
133 	res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
134 	if (res < 0)
135 		return -1;
136 	if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
137 		return -1;
138 
139 	memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
140 	stats->flag |= RX_FLAG_PN_VALIDATED;
141 
142 	return 0;
143 }
144 
145 /* iwl_mvm_create_skb Adds the rxb to a new skb */
146 static void iwl_mvm_create_skb(struct sk_buff *skb, struct ieee80211_hdr *hdr,
147 			       u16 len, u8 crypt_len,
148 			       struct iwl_rx_cmd_buffer *rxb)
149 {
150 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
151 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
152 	unsigned int headlen, fraglen, pad_len = 0;
153 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
154 
155 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
156 		len -= 2;
157 		pad_len = 2;
158 	}
159 
160 	/* If frame is small enough to fit in skb->head, pull it completely.
161 	 * If not, only pull ieee80211_hdr (including crypto if present, and
162 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
163 	 * splice() or TCP coalesce are more efficient.
164 	 *
165 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
166 	 * least 8 bytes (possibly more for mesh) we can do the same here
167 	 * to save the cost of doing it later. That still doesn't pull in
168 	 * the actual IP header since the typical case has a SNAP header.
169 	 * If the latter changes (there are efforts in the standards group
170 	 * to do so) we should revisit this and ieee80211_data_to_8023().
171 	 */
172 	headlen = (len <= skb_tailroom(skb)) ? len :
173 					       hdrlen + crypt_len + 8;
174 
175 	/* The firmware may align the packet to DWORD.
176 	 * The padding is inserted after the IV.
177 	 * After copying the header + IV skip the padding if
178 	 * present before copying packet data.
179 	 */
180 	hdrlen += crypt_len;
181 	skb_put_data(skb, hdr, hdrlen);
182 	skb_put_data(skb, (u8 *)hdr + hdrlen + pad_len, headlen - hdrlen);
183 
184 	fraglen = len - headlen;
185 
186 	if (fraglen) {
187 		int offset = (void *)hdr + headlen + pad_len -
188 			     rxb_addr(rxb) + rxb_offset(rxb);
189 
190 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
191 				fraglen, rxb->truesize);
192 	}
193 }
194 
195 /* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
196 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
197 					    struct napi_struct *napi,
198 					    struct sk_buff *skb, int queue,
199 					    struct ieee80211_sta *sta)
200 {
201 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
202 
203 	if (iwl_mvm_check_pn(mvm, skb, queue, sta)) {
204 		kfree_skb(skb);
205 	} else {
206 		unsigned int radiotap_len = 0;
207 
208 		if (rx_status->flag & RX_FLAG_RADIOTAP_HE)
209 			radiotap_len += sizeof(struct ieee80211_radiotap_he);
210 		if (rx_status->flag & RX_FLAG_RADIOTAP_HE_MU)
211 			radiotap_len += sizeof(struct ieee80211_radiotap_he_mu);
212 		__skb_push(skb, radiotap_len);
213 		ieee80211_rx_napi(mvm->hw, sta, skb, napi);
214 	}
215 }
216 
217 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
218 					struct ieee80211_rx_status *rx_status,
219 					u32 rate_n_flags, int energy_a,
220 					int energy_b)
221 {
222 	int max_energy;
223 	u32 rate_flags = rate_n_flags;
224 
225 	energy_a = energy_a ? -energy_a : S8_MIN;
226 	energy_b = energy_b ? -energy_b : S8_MIN;
227 	max_energy = max(energy_a, energy_b);
228 
229 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
230 			energy_a, energy_b, max_energy);
231 
232 	rx_status->signal = max_energy;
233 	rx_status->chains =
234 		(rate_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
235 	rx_status->chain_signal[0] = energy_a;
236 	rx_status->chain_signal[1] = energy_b;
237 	rx_status->chain_signal[2] = S8_MIN;
238 }
239 
240 static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
241 			     struct ieee80211_rx_status *stats, u16 phy_info,
242 			     struct iwl_rx_mpdu_desc *desc,
243 			     u32 pkt_flags, int queue, u8 *crypt_len)
244 {
245 	u16 status = le16_to_cpu(desc->status);
246 
247 	/*
248 	 * Drop UNKNOWN frames in aggregation, unless in monitor mode
249 	 * (where we don't have the keys).
250 	 * We limit this to aggregation because in TKIP this is a valid
251 	 * scenario, since we may not have the (correct) TTAK (phase 1
252 	 * key) in the firmware.
253 	 */
254 	if (phy_info & IWL_RX_MPDU_PHY_AMPDU &&
255 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
256 	    IWL_RX_MPDU_STATUS_SEC_UNKNOWN && !mvm->monitor_on)
257 		return -1;
258 
259 	if (!ieee80211_has_protected(hdr->frame_control) ||
260 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
261 	    IWL_RX_MPDU_STATUS_SEC_NONE)
262 		return 0;
263 
264 	/* TODO: handle packets encrypted with unknown alg */
265 
266 	switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
267 	case IWL_RX_MPDU_STATUS_SEC_CCM:
268 	case IWL_RX_MPDU_STATUS_SEC_GCM:
269 		BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
270 		/* alg is CCM: check MIC only */
271 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
272 			return -1;
273 
274 		stats->flag |= RX_FLAG_DECRYPTED;
275 		if (pkt_flags & FH_RSCSR_RADA_EN)
276 			stats->flag |= RX_FLAG_MIC_STRIPPED;
277 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
278 		return 0;
279 	case IWL_RX_MPDU_STATUS_SEC_TKIP:
280 		/* Don't drop the frame and decrypt it in SW */
281 		if (!fw_has_api(&mvm->fw->ucode_capa,
282 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
283 		    !(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
284 			return 0;
285 
286 		*crypt_len = IEEE80211_TKIP_IV_LEN;
287 		/* fall through if TTAK OK */
288 	case IWL_RX_MPDU_STATUS_SEC_WEP:
289 		if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
290 			return -1;
291 
292 		stats->flag |= RX_FLAG_DECRYPTED;
293 		if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
294 				IWL_RX_MPDU_STATUS_SEC_WEP)
295 			*crypt_len = IEEE80211_WEP_IV_LEN;
296 
297 		if (pkt_flags & FH_RSCSR_RADA_EN)
298 			stats->flag |= RX_FLAG_ICV_STRIPPED;
299 
300 		return 0;
301 	case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
302 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
303 			return -1;
304 		stats->flag |= RX_FLAG_DECRYPTED;
305 		return 0;
306 	default:
307 		/* Expected in monitor (not having the keys) */
308 		if (!mvm->monitor_on)
309 			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
310 	}
311 
312 	return 0;
313 }
314 
315 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
316 			    struct sk_buff *skb,
317 			    struct iwl_rx_mpdu_desc *desc)
318 {
319 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
320 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
321 	u16 flags = le16_to_cpu(desc->l3l4_flags);
322 	u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
323 			  IWL_RX_L3_PROTO_POS);
324 
325 	if (mvmvif->features & NETIF_F_RXCSUM &&
326 	    flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
327 	    (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
328 	     l3_prot == IWL_RX_L3_TYPE_IPV6 ||
329 	     l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
330 		skb->ip_summed = CHECKSUM_UNNECESSARY;
331 }
332 
333 /*
334  * returns true if a packet is a duplicate and should be dropped.
335  * Updates AMSDU PN tracking info
336  */
337 static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
338 			   struct ieee80211_rx_status *rx_status,
339 			   struct ieee80211_hdr *hdr,
340 			   struct iwl_rx_mpdu_desc *desc)
341 {
342 	struct iwl_mvm_sta *mvm_sta;
343 	struct iwl_mvm_rxq_dup_data *dup_data;
344 	u8 tid, sub_frame_idx;
345 
346 	if (WARN_ON(IS_ERR_OR_NULL(sta)))
347 		return false;
348 
349 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
350 	dup_data = &mvm_sta->dup_data[queue];
351 
352 	/*
353 	 * Drop duplicate 802.11 retransmissions
354 	 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
355 	 */
356 	if (ieee80211_is_ctl(hdr->frame_control) ||
357 	    ieee80211_is_qos_nullfunc(hdr->frame_control) ||
358 	    is_multicast_ether_addr(hdr->addr1)) {
359 		rx_status->flag |= RX_FLAG_DUP_VALIDATED;
360 		return false;
361 	}
362 
363 	if (ieee80211_is_data_qos(hdr->frame_control))
364 		/* frame has qos control */
365 		tid = ieee80211_get_tid(hdr);
366 	else
367 		tid = IWL_MAX_TID_COUNT;
368 
369 	/* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
370 	sub_frame_idx = desc->amsdu_info &
371 		IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
372 
373 	if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
374 		     dup_data->last_seq[tid] == hdr->seq_ctrl &&
375 		     dup_data->last_sub_frame[tid] >= sub_frame_idx))
376 		return true;
377 
378 	/* Allow same PN as the first subframe for following sub frames */
379 	if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
380 	    sub_frame_idx > dup_data->last_sub_frame[tid] &&
381 	    desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
382 		rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
383 
384 	dup_data->last_seq[tid] = hdr->seq_ctrl;
385 	dup_data->last_sub_frame[tid] = sub_frame_idx;
386 
387 	rx_status->flag |= RX_FLAG_DUP_VALIDATED;
388 
389 	return false;
390 }
391 
392 int iwl_mvm_notify_rx_queue(struct iwl_mvm *mvm, u32 rxq_mask,
393 			    const u8 *data, u32 count)
394 {
395 	struct iwl_rxq_sync_cmd *cmd;
396 	u32 data_size = sizeof(*cmd) + count;
397 	int ret;
398 
399 	/* should be DWORD aligned */
400 	if (WARN_ON(count & 3 || count > IWL_MULTI_QUEUE_SYNC_MSG_MAX_SIZE))
401 		return -EINVAL;
402 
403 	cmd = kzalloc(data_size, GFP_KERNEL);
404 	if (!cmd)
405 		return -ENOMEM;
406 
407 	cmd->rxq_mask = cpu_to_le32(rxq_mask);
408 	cmd->count =  cpu_to_le32(count);
409 	cmd->flags = 0;
410 	memcpy(cmd->payload, data, count);
411 
412 	ret = iwl_mvm_send_cmd_pdu(mvm,
413 				   WIDE_ID(DATA_PATH_GROUP,
414 					   TRIGGER_RX_QUEUES_NOTIF_CMD),
415 				   0, data_size, cmd);
416 
417 	kfree(cmd);
418 	return ret;
419 }
420 
421 /*
422  * Returns true if sn2 - buffer_size < sn1 < sn2.
423  * To be used only in order to compare reorder buffer head with NSSN.
424  * We fully trust NSSN unless it is behind us due to reorder timeout.
425  * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
426  */
427 static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
428 {
429 	return ieee80211_sn_less(sn1, sn2) &&
430 	       !ieee80211_sn_less(sn1, sn2 - buffer_size);
431 }
432 
433 #define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
434 
435 static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
436 				   struct ieee80211_sta *sta,
437 				   struct napi_struct *napi,
438 				   struct iwl_mvm_baid_data *baid_data,
439 				   struct iwl_mvm_reorder_buffer *reorder_buf,
440 				   u16 nssn)
441 {
442 	struct iwl_mvm_reorder_buf_entry *entries =
443 		&baid_data->entries[reorder_buf->queue *
444 				    baid_data->entries_per_queue];
445 	u16 ssn = reorder_buf->head_sn;
446 
447 	lockdep_assert_held(&reorder_buf->lock);
448 
449 	/* ignore nssn smaller than head sn - this can happen due to timeout */
450 	if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
451 		goto set_timer;
452 
453 	while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
454 		int index = ssn % reorder_buf->buf_size;
455 		struct sk_buff_head *skb_list = &entries[index].e.frames;
456 		struct sk_buff *skb;
457 
458 		ssn = ieee80211_sn_inc(ssn);
459 
460 		/*
461 		 * Empty the list. Will have more than one frame for A-MSDU.
462 		 * Empty list is valid as well since nssn indicates frames were
463 		 * received.
464 		 */
465 		while ((skb = __skb_dequeue(skb_list))) {
466 			iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
467 							reorder_buf->queue,
468 							sta);
469 			reorder_buf->num_stored--;
470 		}
471 	}
472 	reorder_buf->head_sn = nssn;
473 
474 set_timer:
475 	if (reorder_buf->num_stored && !reorder_buf->removed) {
476 		u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
477 
478 		while (skb_queue_empty(&entries[index].e.frames))
479 			index = (index + 1) % reorder_buf->buf_size;
480 		/* modify timer to match next frame's expiration time */
481 		mod_timer(&reorder_buf->reorder_timer,
482 			  entries[index].e.reorder_time + 1 +
483 			  RX_REORDER_BUF_TIMEOUT_MQ);
484 	} else {
485 		del_timer(&reorder_buf->reorder_timer);
486 	}
487 }
488 
489 void iwl_mvm_reorder_timer_expired(struct timer_list *t)
490 {
491 	struct iwl_mvm_reorder_buffer *buf = from_timer(buf, t, reorder_timer);
492 	struct iwl_mvm_baid_data *baid_data =
493 		iwl_mvm_baid_data_from_reorder_buf(buf);
494 	struct iwl_mvm_reorder_buf_entry *entries =
495 		&baid_data->entries[buf->queue * baid_data->entries_per_queue];
496 	int i;
497 	u16 sn = 0, index = 0;
498 	bool expired = false;
499 	bool cont = false;
500 
501 	spin_lock(&buf->lock);
502 
503 	if (!buf->num_stored || buf->removed) {
504 		spin_unlock(&buf->lock);
505 		return;
506 	}
507 
508 	for (i = 0; i < buf->buf_size ; i++) {
509 		index = (buf->head_sn + i) % buf->buf_size;
510 
511 		if (skb_queue_empty(&entries[index].e.frames)) {
512 			/*
513 			 * If there is a hole and the next frame didn't expire
514 			 * we want to break and not advance SN
515 			 */
516 			cont = false;
517 			continue;
518 		}
519 		if (!cont &&
520 		    !time_after(jiffies, entries[index].e.reorder_time +
521 					 RX_REORDER_BUF_TIMEOUT_MQ))
522 			break;
523 
524 		expired = true;
525 		/* continue until next hole after this expired frames */
526 		cont = true;
527 		sn = ieee80211_sn_add(buf->head_sn, i + 1);
528 	}
529 
530 	if (expired) {
531 		struct ieee80211_sta *sta;
532 		struct iwl_mvm_sta *mvmsta;
533 		u8 sta_id = baid_data->sta_id;
534 
535 		rcu_read_lock();
536 		sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[sta_id]);
537 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
538 
539 		/* SN is set to the last expired frame + 1 */
540 		IWL_DEBUG_HT(buf->mvm,
541 			     "Releasing expired frames for sta %u, sn %d\n",
542 			     sta_id, sn);
543 		iwl_mvm_event_frame_timeout_callback(buf->mvm, mvmsta->vif,
544 						     sta, baid_data->tid);
545 		iwl_mvm_release_frames(buf->mvm, sta, NULL, baid_data, buf, sn);
546 		rcu_read_unlock();
547 	} else {
548 		/*
549 		 * If no frame expired and there are stored frames, index is now
550 		 * pointing to the first unexpired frame - modify timer
551 		 * accordingly to this frame.
552 		 */
553 		mod_timer(&buf->reorder_timer,
554 			  entries[index].e.reorder_time +
555 			  1 + RX_REORDER_BUF_TIMEOUT_MQ);
556 	}
557 	spin_unlock(&buf->lock);
558 }
559 
560 static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
561 			   struct iwl_mvm_delba_data *data)
562 {
563 	struct iwl_mvm_baid_data *ba_data;
564 	struct ieee80211_sta *sta;
565 	struct iwl_mvm_reorder_buffer *reorder_buf;
566 	u8 baid = data->baid;
567 
568 	if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
569 		return;
570 
571 	rcu_read_lock();
572 
573 	ba_data = rcu_dereference(mvm->baid_map[baid]);
574 	if (WARN_ON_ONCE(!ba_data))
575 		goto out;
576 
577 	sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
578 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
579 		goto out;
580 
581 	reorder_buf = &ba_data->reorder_buf[queue];
582 
583 	/* release all frames that are in the reorder buffer to the stack */
584 	spin_lock_bh(&reorder_buf->lock);
585 	iwl_mvm_release_frames(mvm, sta, NULL, ba_data, reorder_buf,
586 			       ieee80211_sn_add(reorder_buf->head_sn,
587 						reorder_buf->buf_size));
588 	spin_unlock_bh(&reorder_buf->lock);
589 	del_timer_sync(&reorder_buf->reorder_timer);
590 
591 out:
592 	rcu_read_unlock();
593 }
594 
595 void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
596 			    int queue)
597 {
598 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
599 	struct iwl_rxq_sync_notification *notif;
600 	struct iwl_mvm_internal_rxq_notif *internal_notif;
601 
602 	notif = (void *)pkt->data;
603 	internal_notif = (void *)notif->payload;
604 
605 	if (internal_notif->sync &&
606 	    mvm->queue_sync_cookie != internal_notif->cookie) {
607 		WARN_ONCE(1, "Received expired RX queue sync message\n");
608 		return;
609 	}
610 
611 	switch (internal_notif->type) {
612 	case IWL_MVM_RXQ_EMPTY:
613 		break;
614 	case IWL_MVM_RXQ_NOTIF_DEL_BA:
615 		iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
616 		break;
617 	default:
618 		WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
619 	}
620 
621 	if (internal_notif->sync &&
622 	    !atomic_dec_return(&mvm->queue_sync_counter))
623 		wake_up(&mvm->rx_sync_waitq);
624 }
625 
626 /*
627  * Returns true if the MPDU was buffered\dropped, false if it should be passed
628  * to upper layer.
629  */
630 static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
631 			    struct napi_struct *napi,
632 			    int queue,
633 			    struct ieee80211_sta *sta,
634 			    struct sk_buff *skb,
635 			    struct iwl_rx_mpdu_desc *desc)
636 {
637 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
638 	struct iwl_mvm_sta *mvm_sta;
639 	struct iwl_mvm_baid_data *baid_data;
640 	struct iwl_mvm_reorder_buffer *buffer;
641 	struct sk_buff *tail;
642 	u32 reorder = le32_to_cpu(desc->reorder_data);
643 	bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
644 	bool last_subframe =
645 		desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
646 	u8 tid = ieee80211_get_tid(hdr);
647 	u8 sub_frame_idx = desc->amsdu_info &
648 			   IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
649 	struct iwl_mvm_reorder_buf_entry *entries;
650 	int index;
651 	u16 nssn, sn;
652 	u8 baid;
653 
654 	baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
655 		IWL_RX_MPDU_REORDER_BAID_SHIFT;
656 
657 	/*
658 	 * This also covers the case of receiving a Block Ack Request
659 	 * outside a BA session; we'll pass it to mac80211 and that
660 	 * then sends a delBA action frame.
661 	 */
662 	if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
663 		return false;
664 
665 	/* no sta yet */
666 	if (WARN_ONCE(IS_ERR_OR_NULL(sta),
667 		      "Got valid BAID without a valid station assigned\n"))
668 		return false;
669 
670 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
671 
672 	/* not a data packet or a bar */
673 	if (!ieee80211_is_back_req(hdr->frame_control) &&
674 	    (!ieee80211_is_data_qos(hdr->frame_control) ||
675 	     is_multicast_ether_addr(hdr->addr1)))
676 		return false;
677 
678 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
679 		return false;
680 
681 	baid_data = rcu_dereference(mvm->baid_map[baid]);
682 	if (!baid_data) {
683 		IWL_DEBUG_RX(mvm,
684 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
685 			      baid, reorder);
686 		return false;
687 	}
688 
689 	if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
690 		 "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
691 		 baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
692 		 tid))
693 		return false;
694 
695 	nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
696 	sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
697 		IWL_RX_MPDU_REORDER_SN_SHIFT;
698 
699 	buffer = &baid_data->reorder_buf[queue];
700 	entries = &baid_data->entries[queue * baid_data->entries_per_queue];
701 
702 	spin_lock_bh(&buffer->lock);
703 
704 	if (!buffer->valid) {
705 		if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
706 			spin_unlock_bh(&buffer->lock);
707 			return false;
708 		}
709 		buffer->valid = true;
710 	}
711 
712 	if (ieee80211_is_back_req(hdr->frame_control)) {
713 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer, nssn);
714 		goto drop;
715 	}
716 
717 	/*
718 	 * If there was a significant jump in the nssn - adjust.
719 	 * If the SN is smaller than the NSSN it might need to first go into
720 	 * the reorder buffer, in which case we just release up to it and the
721 	 * rest of the function will take care of storing it and releasing up to
722 	 * the nssn
723 	 */
724 	if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
725 				buffer->buf_size) ||
726 	    !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
727 		u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
728 
729 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
730 				       min_sn);
731 	}
732 
733 	/* drop any oudated packets */
734 	if (ieee80211_sn_less(sn, buffer->head_sn))
735 		goto drop;
736 
737 	/* release immediately if allowed by nssn and no stored frames */
738 	if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
739 		if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
740 				       buffer->buf_size) &&
741 		   (!amsdu || last_subframe))
742 			buffer->head_sn = nssn;
743 		/* No need to update AMSDU last SN - we are moving the head */
744 		spin_unlock_bh(&buffer->lock);
745 		return false;
746 	}
747 
748 	/*
749 	 * release immediately if there are no stored frames, and the sn is
750 	 * equal to the head.
751 	 * This can happen due to reorder timer, where NSSN is behind head_sn.
752 	 * When we released everything, and we got the next frame in the
753 	 * sequence, according to the NSSN we can't release immediately,
754 	 * while technically there is no hole and we can move forward.
755 	 */
756 	if (!buffer->num_stored && sn == buffer->head_sn) {
757 		if (!amsdu || last_subframe)
758 			buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
759 		/* No need to update AMSDU last SN - we are moving the head */
760 		spin_unlock_bh(&buffer->lock);
761 		return false;
762 	}
763 
764 	index = sn % buffer->buf_size;
765 
766 	/*
767 	 * Check if we already stored this frame
768 	 * As AMSDU is either received or not as whole, logic is simple:
769 	 * If we have frames in that position in the buffer and the last frame
770 	 * originated from AMSDU had a different SN then it is a retransmission.
771 	 * If it is the same SN then if the subframe index is incrementing it
772 	 * is the same AMSDU - otherwise it is a retransmission.
773 	 */
774 	tail = skb_peek_tail(&entries[index].e.frames);
775 	if (tail && !amsdu)
776 		goto drop;
777 	else if (tail && (sn != buffer->last_amsdu ||
778 			  buffer->last_sub_index >= sub_frame_idx))
779 		goto drop;
780 
781 	/* put in reorder buffer */
782 	__skb_queue_tail(&entries[index].e.frames, skb);
783 	buffer->num_stored++;
784 	entries[index].e.reorder_time = jiffies;
785 
786 	if (amsdu) {
787 		buffer->last_amsdu = sn;
788 		buffer->last_sub_index = sub_frame_idx;
789 	}
790 
791 	/*
792 	 * We cannot trust NSSN for AMSDU sub-frames that are not the last.
793 	 * The reason is that NSSN advances on the first sub-frame, and may
794 	 * cause the reorder buffer to advance before all the sub-frames arrive.
795 	 * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
796 	 * SN 1. NSSN for first sub frame will be 3 with the result of driver
797 	 * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
798 	 * already ahead and it will be dropped.
799 	 * If the last sub-frame is not on this queue - we will get frame
800 	 * release notification with up to date NSSN.
801 	 */
802 	if (!amsdu || last_subframe)
803 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer, nssn);
804 
805 	spin_unlock_bh(&buffer->lock);
806 	return true;
807 
808 drop:
809 	kfree_skb(skb);
810 	spin_unlock_bh(&buffer->lock);
811 	return true;
812 }
813 
814 static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
815 				    u32 reorder_data, u8 baid)
816 {
817 	unsigned long now = jiffies;
818 	unsigned long timeout;
819 	struct iwl_mvm_baid_data *data;
820 
821 	rcu_read_lock();
822 
823 	data = rcu_dereference(mvm->baid_map[baid]);
824 	if (!data) {
825 		IWL_DEBUG_RX(mvm,
826 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
827 			      baid, reorder_data);
828 		goto out;
829 	}
830 
831 	if (!data->timeout)
832 		goto out;
833 
834 	timeout = data->timeout;
835 	/*
836 	 * Do not update last rx all the time to avoid cache bouncing
837 	 * between the rx queues.
838 	 * Update it every timeout. Worst case is the session will
839 	 * expire after ~ 2 * timeout, which doesn't matter that much.
840 	 */
841 	if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
842 		/* Update is atomic */
843 		data->last_rx = now;
844 
845 out:
846 	rcu_read_unlock();
847 }
848 
849 static void iwl_mvm_flip_address(u8 *addr)
850 {
851 	int i;
852 	u8 mac_addr[ETH_ALEN];
853 
854 	for (i = 0; i < ETH_ALEN; i++)
855 		mac_addr[i] = addr[ETH_ALEN - i - 1];
856 	ether_addr_copy(addr, mac_addr);
857 }
858 
859 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
860 			struct iwl_rx_cmd_buffer *rxb, int queue)
861 {
862 	struct ieee80211_rx_status *rx_status;
863 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
864 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
865 	struct ieee80211_hdr *hdr;
866 	u32 len = le16_to_cpu(desc->mpdu_len);
867 	u32 rate_n_flags, gp2_on_air_rise;
868 	u16 phy_info = le16_to_cpu(desc->phy_info);
869 	struct ieee80211_sta *sta = NULL;
870 	struct sk_buff *skb;
871 	u8 crypt_len = 0, channel, energy_a, energy_b;
872 	struct ieee80211_radiotap_he *he = NULL;
873 	struct ieee80211_radiotap_he_mu *he_mu = NULL;
874 	u32 he_type = 0xffffffff;
875 	/* this is invalid e.g. because puncture type doesn't allow 0b11 */
876 #define HE_PHY_DATA_INVAL ((u64)-1)
877 	u64 he_phy_data = HE_PHY_DATA_INVAL;
878 	size_t desc_size;
879 
880 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
881 		return;
882 
883 	if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) {
884 		rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
885 		channel = desc->v3.channel;
886 		gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
887 		energy_a = desc->v3.energy_a;
888 		energy_b = desc->v3.energy_b;
889 		desc_size = sizeof(*desc);
890 	} else {
891 		rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
892 		channel = desc->v1.channel;
893 		gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
894 		energy_a = desc->v1.energy_a;
895 		energy_b = desc->v1.energy_b;
896 		desc_size = IWL_RX_DESC_SIZE_V1;
897 	}
898 
899 	hdr = (void *)(pkt->data + desc_size);
900 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
901 	 * ieee80211_hdr pulled.
902 	 */
903 	skb = alloc_skb(128, GFP_ATOMIC);
904 	if (!skb) {
905 		IWL_ERR(mvm, "alloc_skb failed\n");
906 		return;
907 	}
908 
909 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
910 		/*
911 		 * If the device inserted padding it means that (it thought)
912 		 * the 802.11 header wasn't a multiple of 4 bytes long. In
913 		 * this case, reserve two bytes at the start of the SKB to
914 		 * align the payload properly in case we end up copying it.
915 		 */
916 		skb_reserve(skb, 2);
917 	}
918 
919 	rx_status = IEEE80211_SKB_RXCB(skb);
920 
921 	if (rate_n_flags & RATE_MCS_HE_MSK) {
922 		static const struct ieee80211_radiotap_he known = {
923 			.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
924 					     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
925 					     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
926 					     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
927 			.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
928 					     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
929 		};
930 		static const struct ieee80211_radiotap_he_mu mu_known = {
931 			.flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
932 					      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
933 					      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
934 					      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
935 			.flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN),
936 		};
937 		unsigned int radiotap_len = 0;
938 
939 		he = skb_put_data(skb, &known, sizeof(known));
940 		radiotap_len += sizeof(known);
941 		rx_status->flag |= RX_FLAG_RADIOTAP_HE;
942 
943 		he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
944 
945 		if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) {
946 			if (mvm->trans->cfg->device_family >=
947 			    IWL_DEVICE_FAMILY_22560)
948 				he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
949 			else
950 				he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
951 
952 			if (he_type == RATE_MCS_HE_TYPE_MU) {
953 				he_mu = skb_put_data(skb, &mu_known,
954 						     sizeof(mu_known));
955 				radiotap_len += sizeof(mu_known);
956 				rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
957 			}
958 		}
959 
960 		/* temporarily hide the radiotap data */
961 		__skb_pull(skb, radiotap_len);
962 	}
963 
964 	rx_status = IEEE80211_SKB_RXCB(skb);
965 
966 	if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, phy_info, desc,
967 			      le32_to_cpu(pkt->len_n_flags), queue,
968 			      &crypt_len)) {
969 		kfree_skb(skb);
970 		return;
971 	}
972 
973 	/*
974 	 * Keep packets with CRC errors (and with overrun) for monitor mode
975 	 * (otherwise the firmware discards them) but mark them as bad.
976 	 */
977 	if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
978 	    !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
979 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
980 			     le16_to_cpu(desc->status));
981 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
982 	}
983 	/* set the preamble flag if appropriate */
984 	if (phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
985 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
986 
987 	if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
988 		u64 tsf_on_air_rise;
989 
990 		if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
991 			tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
992 		else
993 			tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
994 
995 		rx_status->mactime = tsf_on_air_rise;
996 		/* TSF as indicated by the firmware is at INA time */
997 		rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
998 	} else if (he_type == RATE_MCS_HE_TYPE_SU) {
999 		u64 he_phy_data;
1000 
1001 		if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
1002 			he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
1003 		else
1004 			he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
1005 
1006 		he->data1 |=
1007 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1008 		if (FIELD_GET(IWL_RX_HE_PHY_UPLINK,
1009 			      he_phy_data))
1010 			he->data3 |=
1011 				cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1012 
1013 		if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1014 			rx_status->ampdu_reference = mvm->ampdu_ref;
1015 			mvm->ampdu_ref++;
1016 
1017 			rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1018 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1019 			if (FIELD_GET(IWL_RX_HE_PHY_DELIM_EOF,
1020 				      he_phy_data))
1021 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1022 		}
1023 	} else if (he_mu && he_phy_data != HE_PHY_DATA_INVAL) {
1024 		he_mu->flags1 |=
1025 			le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,
1026 						   he_phy_data),
1027 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1028 		he_mu->flags1 |=
1029 			le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_DCM,
1030 						   he_phy_data),
1031 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1032 		he_mu->flags1 |=
1033 			le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_MCS_MASK,
1034 						   he_phy_data),
1035 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1036 		he_mu->flags2 |=
1037 			le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_COMPRESSION,
1038 						   he_phy_data),
1039 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1040 		he_mu->flags2 |=
1041 			le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_PREAMBLE_PUNC_TYPE_MASK,
1042 						   he_phy_data),
1043 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1044 	}
1045 	rx_status->device_timestamp = gp2_on_air_rise;
1046 	rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1047 		NL80211_BAND_2GHZ;
1048 	rx_status->freq = ieee80211_channel_to_frequency(channel,
1049 							 rx_status->band);
1050 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1051 				    energy_b);
1052 
1053 	/* update aggregation data for monitor sake on default queue */
1054 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1055 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1056 		u64 he_phy_data;
1057 
1058 		if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
1059 			he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
1060 		else
1061 			he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
1062 
1063 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1064 		rx_status->ampdu_reference = mvm->ampdu_ref;
1065 		/* toggle is switched whenever new aggregation starts */
1066 		if (toggle_bit != mvm->ampdu_toggle) {
1067 			mvm->ampdu_ref++;
1068 			mvm->ampdu_toggle = toggle_bit;
1069 
1070 			if (he_phy_data != HE_PHY_DATA_INVAL &&
1071 			    he_type == RATE_MCS_HE_TYPE_MU) {
1072 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1073 				if (FIELD_GET(IWL_RX_HE_PHY_DELIM_EOF,
1074 					      he_phy_data))
1075 					rx_status->flag |=
1076 						RX_FLAG_AMPDU_EOF_BIT;
1077 			}
1078 		}
1079 	}
1080 
1081 	rcu_read_lock();
1082 
1083 	if (desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
1084 		u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
1085 
1086 		if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
1087 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
1088 			if (IS_ERR(sta))
1089 				sta = NULL;
1090 		}
1091 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
1092 		/*
1093 		 * This is fine since we prevent two stations with the same
1094 		 * address from being added.
1095 		 */
1096 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
1097 	}
1098 
1099 	if (sta) {
1100 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1101 		struct ieee80211_vif *tx_blocked_vif =
1102 			rcu_dereference(mvm->csa_tx_blocked_vif);
1103 		u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
1104 			       IWL_RX_MPDU_REORDER_BAID_MASK) >>
1105 			       IWL_RX_MPDU_REORDER_BAID_SHIFT);
1106 
1107 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
1108 		    !is_multicast_ether_addr(hdr->addr1) &&
1109 		    ieee80211_is_data(hdr->frame_control) &&
1110 		    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1111 			schedule_delayed_work(&mvm->tcm.work, 0);
1112 
1113 		/*
1114 		 * We have tx blocked stations (with CS bit). If we heard
1115 		 * frames from a blocked station on a new channel we can
1116 		 * TX to it again.
1117 		 */
1118 		if (unlikely(tx_blocked_vif) &&
1119 		    tx_blocked_vif == mvmsta->vif) {
1120 			struct iwl_mvm_vif *mvmvif =
1121 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1122 
1123 			if (mvmvif->csa_target_freq == rx_status->freq)
1124 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
1125 								 false);
1126 		}
1127 
1128 		rs_update_last_rssi(mvm, mvmsta, rx_status);
1129 
1130 		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
1131 		    ieee80211_is_beacon(hdr->frame_control)) {
1132 			struct iwl_fw_dbg_trigger_tlv *trig;
1133 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
1134 			bool trig_check;
1135 			s32 rssi;
1136 
1137 			trig = iwl_fw_dbg_get_trigger(mvm->fw,
1138 						      FW_DBG_TRIGGER_RSSI);
1139 			rssi_trig = (void *)trig->data;
1140 			rssi = le32_to_cpu(rssi_trig->rssi);
1141 
1142 			trig_check =
1143 				iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
1144 							      ieee80211_vif_to_wdev(mvmsta->vif),
1145 							      trig);
1146 			if (trig_check && rx_status->signal < rssi)
1147 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1148 							NULL);
1149 		}
1150 
1151 		if (ieee80211_is_data(hdr->frame_control))
1152 			iwl_mvm_rx_csum(sta, skb, desc);
1153 
1154 		if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
1155 			kfree_skb(skb);
1156 			goto out;
1157 		}
1158 
1159 		/*
1160 		 * Our hardware de-aggregates AMSDUs but copies the mac header
1161 		 * as it to the de-aggregated MPDUs. We need to turn off the
1162 		 * AMSDU bit in the QoS control ourselves.
1163 		 * In addition, HW reverses addr3 and addr4 - reverse it back.
1164 		 */
1165 		if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
1166 		    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
1167 			u8 *qc = ieee80211_get_qos_ctl(hdr);
1168 
1169 			*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1170 
1171 			if (mvm->trans->cfg->device_family ==
1172 			    IWL_DEVICE_FAMILY_9000) {
1173 				iwl_mvm_flip_address(hdr->addr3);
1174 
1175 				if (ieee80211_has_a4(hdr->frame_control))
1176 					iwl_mvm_flip_address(hdr->addr4);
1177 			}
1178 		}
1179 		if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
1180 			u32 reorder_data = le32_to_cpu(desc->reorder_data);
1181 
1182 			iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
1183 		}
1184 	}
1185 
1186 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1187 	case RATE_MCS_CHAN_WIDTH_20:
1188 		break;
1189 	case RATE_MCS_CHAN_WIDTH_40:
1190 		rx_status->bw = RATE_INFO_BW_40;
1191 		break;
1192 	case RATE_MCS_CHAN_WIDTH_80:
1193 		rx_status->bw = RATE_INFO_BW_80;
1194 		break;
1195 	case RATE_MCS_CHAN_WIDTH_160:
1196 		rx_status->bw = RATE_INFO_BW_160;
1197 		break;
1198 	}
1199 
1200 	if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
1201 	    rate_n_flags & RATE_MCS_HE_106T_MSK) {
1202 		rx_status->bw = RATE_INFO_BW_HE_RU;
1203 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1204 	}
1205 
1206 	if (rate_n_flags & RATE_MCS_HE_MSK &&
1207 	    phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD &&
1208 	    he_type == RATE_MCS_HE_TYPE_MU) {
1209 		/*
1210 		 * Unfortunately, we have to leave the mac80211 data
1211 		 * incorrect for the case that we receive an HE-MU
1212 		 * transmission and *don't* have the he_mu pointer,
1213 		 * i.e. we don't have the phy data (due to the bits
1214 		 * being used for TSF). This shouldn't happen though
1215 		 * as management frames where we need the TSF/timers
1216 		 * are not be transmitted in HE-MU, I think.
1217 		 */
1218 		u8 ru = FIELD_GET(IWL_RX_HE_PHY_RU_ALLOC_MASK, he_phy_data);
1219 		u8 offs = 0;
1220 
1221 		rx_status->bw = RATE_INFO_BW_HE_RU;
1222 
1223 		switch (ru) {
1224 		case 0 ... 36:
1225 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1226 			offs = ru;
1227 			break;
1228 		case 37 ... 52:
1229 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1230 			offs = ru - 37;
1231 			break;
1232 		case 53 ... 60:
1233 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1234 			offs = ru - 53;
1235 			break;
1236 		case 61 ... 64:
1237 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1238 			offs = ru - 61;
1239 			break;
1240 		case 65 ... 66:
1241 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1242 			offs = ru - 65;
1243 			break;
1244 		case 67:
1245 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1246 			break;
1247 		case 68:
1248 			rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1249 			break;
1250 		}
1251 		he->data2 |=
1252 			le16_encode_bits(offs,
1253 					 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1254 		he->data2 |=
1255 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN);
1256 		if (he_phy_data & IWL_RX_HE_PHY_RU_ALLOC_SEC80)
1257 			he->data2 |=
1258 				cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1259 	} else if (he) {
1260 		he->data1 |=
1261 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1262 	}
1263 
1264 	if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
1265 	    rate_n_flags & RATE_MCS_SGI_MSK)
1266 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1267 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
1268 		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
1269 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
1270 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
1271 	if (rate_n_flags & RATE_MCS_HT_MSK) {
1272 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1273 				RATE_MCS_STBC_POS;
1274 		rx_status->encoding = RX_ENC_HT;
1275 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
1276 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1277 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
1278 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1279 				RATE_MCS_STBC_POS;
1280 		rx_status->nss =
1281 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
1282 						RATE_VHT_MCS_NSS_POS) + 1;
1283 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
1284 		rx_status->encoding = RX_ENC_VHT;
1285 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1286 		if (rate_n_flags & RATE_MCS_BF_MSK)
1287 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
1288 	} else if (he) {
1289 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1290 				RATE_MCS_STBC_POS;
1291 		rx_status->nss =
1292 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
1293 						RATE_VHT_MCS_NSS_POS) + 1;
1294 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
1295 		rx_status->encoding = RX_ENC_HE;
1296 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1297 		if (rate_n_flags & RATE_MCS_BF_MSK)
1298 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
1299 
1300 		rx_status->he_dcm =
1301 			!!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
1302 
1303 #define CHECK_TYPE(F)							\
1304 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
1305 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1306 
1307 		CHECK_TYPE(SU);
1308 		CHECK_TYPE(EXT_SU);
1309 		CHECK_TYPE(MU);
1310 		CHECK_TYPE(TRIG);
1311 
1312 		he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
1313 
1314 		if (rate_n_flags & RATE_MCS_BF_POS)
1315 			he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
1316 
1317 		switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
1318 			RATE_MCS_HE_GI_LTF_POS) {
1319 		case 0:
1320 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1321 			break;
1322 		case 1:
1323 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1324 			break;
1325 		case 2:
1326 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1327 			break;
1328 		case 3:
1329 			if (rate_n_flags & RATE_MCS_SGI_MSK)
1330 				rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1331 			else
1332 				rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1333 			break;
1334 		}
1335 
1336 		switch (he_type) {
1337 		case RATE_MCS_HE_TYPE_SU: {
1338 			u16 val;
1339 
1340 			/* LTF syms correspond to streams */
1341 			he->data2 |=
1342 				cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1343 			switch (rx_status->nss) {
1344 			case 1:
1345 				val = 0;
1346 				break;
1347 			case 2:
1348 				val = 1;
1349 				break;
1350 			case 3:
1351 			case 4:
1352 				val = 2;
1353 				break;
1354 			case 5:
1355 			case 6:
1356 				val = 3;
1357 				break;
1358 			case 7:
1359 			case 8:
1360 				val = 4;
1361 				break;
1362 			default:
1363 				WARN_ONCE(1, "invalid nss: %d\n",
1364 					  rx_status->nss);
1365 				val = 0;
1366 			}
1367 			he->data5 |=
1368 				le16_encode_bits(val,
1369 						 IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1370 			}
1371 			break;
1372 		case RATE_MCS_HE_TYPE_MU: {
1373 			u16 val;
1374 			u64 he_phy_data;
1375 
1376 			if (mvm->trans->cfg->device_family >=
1377 			    IWL_DEVICE_FAMILY_22560)
1378 				he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
1379 			else
1380 				he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
1381 
1382 			if (he_phy_data == HE_PHY_DATA_INVAL)
1383 				break;
1384 
1385 			val = FIELD_GET(IWL_RX_HE_PHY_HE_LTF_NUM_MASK,
1386 					he_phy_data);
1387 
1388 			he->data2 |=
1389 				cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1390 			he->data5 |=
1391 				cpu_to_le16(FIELD_PREP(
1392 					IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS,
1393 					val));
1394 			}
1395 			break;
1396 		case RATE_MCS_HE_TYPE_EXT_SU:
1397 		case RATE_MCS_HE_TYPE_TRIG:
1398 			/* not supported yet */
1399 			break;
1400 		}
1401 	} else {
1402 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
1403 							       rx_status->band);
1404 
1405 		if (WARN(rate < 0 || rate > 0xFF,
1406 			 "Invalid rate flags 0x%x, band %d,\n",
1407 			 rate_n_flags, rx_status->band)) {
1408 			kfree_skb(skb);
1409 			goto out;
1410 		}
1411 		rx_status->rate_idx = rate;
1412 
1413 	}
1414 
1415 	/* management stuff on default queue */
1416 	if (!queue) {
1417 		if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
1418 			      ieee80211_is_probe_resp(hdr->frame_control)) &&
1419 			     mvm->sched_scan_pass_all ==
1420 			     SCHED_SCAN_PASS_ALL_ENABLED))
1421 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
1422 
1423 		if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
1424 			     ieee80211_is_probe_resp(hdr->frame_control)))
1425 			rx_status->boottime_ns = ktime_get_boot_ns();
1426 	}
1427 
1428 	iwl_mvm_create_skb(skb, hdr, len, crypt_len, rxb);
1429 	if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
1430 		iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
1431 out:
1432 	rcu_read_unlock();
1433 }
1434 
1435 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
1436 			      struct iwl_rx_cmd_buffer *rxb, int queue)
1437 {
1438 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1439 	struct iwl_frame_release *release = (void *)pkt->data;
1440 	struct ieee80211_sta *sta;
1441 	struct iwl_mvm_reorder_buffer *reorder_buf;
1442 	struct iwl_mvm_baid_data *ba_data;
1443 
1444 	int baid = release->baid;
1445 
1446 	IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
1447 		     release->baid, le16_to_cpu(release->nssn));
1448 
1449 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1450 		return;
1451 
1452 	rcu_read_lock();
1453 
1454 	ba_data = rcu_dereference(mvm->baid_map[baid]);
1455 	if (WARN_ON_ONCE(!ba_data))
1456 		goto out;
1457 
1458 	sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
1459 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1460 		goto out;
1461 
1462 	reorder_buf = &ba_data->reorder_buf[queue];
1463 
1464 	spin_lock_bh(&reorder_buf->lock);
1465 	iwl_mvm_release_frames(mvm, sta, napi, ba_data, reorder_buf,
1466 			       le16_to_cpu(release->nssn));
1467 	spin_unlock_bh(&reorder_buf->lock);
1468 
1469 out:
1470 	rcu_read_unlock();
1471 }
1472