xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision 5e85a32aca03aba3ce7e7123943b4529d2969a95)
1203c4805SLuis R. Rodriguez /*
25b68138eSSujith Manoharan  * Copyright (c) 2008-2011 Atheros Communications Inc.
3203c4805SLuis R. Rodriguez  *
4203c4805SLuis R. Rodriguez  * Permission to use, copy, modify, and/or distribute this software for any
5203c4805SLuis R. Rodriguez  * purpose with or without fee is hereby granted, provided that the above
6203c4805SLuis R. Rodriguez  * copyright notice and this permission notice appear in all copies.
7203c4805SLuis R. Rodriguez  *
8203c4805SLuis R. Rodriguez  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9203c4805SLuis R. Rodriguez  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10203c4805SLuis R. Rodriguez  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11203c4805SLuis R. Rodriguez  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12203c4805SLuis R. Rodriguez  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13203c4805SLuis R. Rodriguez  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14203c4805SLuis R. Rodriguez  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15203c4805SLuis R. Rodriguez  */
16203c4805SLuis R. Rodriguez 
17b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h>
18e93d083fSSimon Wunderlich #include <linux/relay.h>
19203c4805SLuis R. Rodriguez #include "ath9k.h"
20b622a720SLuis R. Rodriguez #include "ar9003_mac.h"
21203c4805SLuis R. Rodriguez 
22b5c80475SFelix Fietkau #define SKB_CB_ATHBUF(__skb)	(*((struct ath_buf **)__skb->cb))
23b5c80475SFelix Fietkau 
24ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
25ededf1f8SVasanthakumar Thiagarajan {
26ededf1f8SVasanthakumar Thiagarajan 	return sc->ps_enabled &&
27ededf1f8SVasanthakumar Thiagarajan 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
28ededf1f8SVasanthakumar Thiagarajan }
29ededf1f8SVasanthakumar Thiagarajan 
30203c4805SLuis R. Rodriguez /*
31203c4805SLuis R. Rodriguez  * Setup and link descriptors.
32203c4805SLuis R. Rodriguez  *
33203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
34203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
35203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
36203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
37203c4805SLuis R. Rodriguez  */
38203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
39203c4805SLuis R. Rodriguez {
40203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
41cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
42203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
43203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
44203c4805SLuis R. Rodriguez 
45203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
46203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
47203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
48203c4805SLuis R. Rodriguez 
49203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
50203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
519680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
52203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
53203c4805SLuis R. Rodriguez 
54cc861f74SLuis R. Rodriguez 	/*
55cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
56203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
57cc861f74SLuis R. Rodriguez 	 * to process
58cc861f74SLuis R. Rodriguez 	 */
59203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
60cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
61203c4805SLuis R. Rodriguez 			     0);
62203c4805SLuis R. Rodriguez 
63203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
64203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
65203c4805SLuis R. Rodriguez 	else
66203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
67203c4805SLuis R. Rodriguez 
68203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
69203c4805SLuis R. Rodriguez }
70203c4805SLuis R. Rodriguez 
71e96542e5SFelix Fietkau static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_buf *bf)
72e96542e5SFelix Fietkau {
73e96542e5SFelix Fietkau 	if (sc->rx.buf_hold)
74e96542e5SFelix Fietkau 		ath_rx_buf_link(sc, sc->rx.buf_hold);
75e96542e5SFelix Fietkau 
76e96542e5SFelix Fietkau 	sc->rx.buf_hold = bf;
77e96542e5SFelix Fietkau }
78e96542e5SFelix Fietkau 
79203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
80203c4805SLuis R. Rodriguez {
81203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
82203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
83203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
84203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
85203c4805SLuis R. Rodriguez }
86203c4805SLuis R. Rodriguez 
87203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
88203c4805SLuis R. Rodriguez {
89203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
901510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
911510718dSLuis R. Rodriguez 
92203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
93203c4805SLuis R. Rodriguez 
94203c4805SLuis R. Rodriguez 	/* configure rx filter */
95203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
96203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
97203c4805SLuis R. Rodriguez 
98203c4805SLuis R. Rodriguez 	/* configure bssid mask */
9913b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
100203c4805SLuis R. Rodriguez 
101203c4805SLuis R. Rodriguez 	/* configure operational mode */
102203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
103203c4805SLuis R. Rodriguez 
104203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
105203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
106203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
107203c4805SLuis R. Rodriguez }
108203c4805SLuis R. Rodriguez 
109b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
110b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
111b5c80475SFelix Fietkau {
112b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
113b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
114b5c80475SFelix Fietkau 	struct sk_buff *skb;
115b5c80475SFelix Fietkau 	struct ath_buf *bf;
116b5c80475SFelix Fietkau 
117b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
118b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
119b5c80475SFelix Fietkau 		return false;
120b5c80475SFelix Fietkau 
121b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
122b5c80475SFelix Fietkau 	list_del_init(&bf->list);
123b5c80475SFelix Fietkau 
124b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
125b5c80475SFelix Fietkau 
126b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
127b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
128b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
129b5c80475SFelix Fietkau 
130b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
131b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
13207236bf3SSujith Manoharan 	__skb_queue_tail(&rx_edma->rx_fifo, skb);
133b5c80475SFelix Fietkau 
134b5c80475SFelix Fietkau 	return true;
135b5c80475SFelix Fietkau }
136b5c80475SFelix Fietkau 
137b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
1387a897203SSujith Manoharan 				  enum ath9k_rx_qtype qtype)
139b5c80475SFelix Fietkau {
140b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1416a01f0c0SMohammed Shafi Shajakhan 	struct ath_buf *bf, *tbf;
142b5c80475SFelix Fietkau 
143b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
144d2182b69SJoe Perches 		ath_dbg(common, QUEUE, "No free rx buf available\n");
145b5c80475SFelix Fietkau 		return;
146b5c80475SFelix Fietkau 	}
147b5c80475SFelix Fietkau 
1486a01f0c0SMohammed Shafi Shajakhan 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
149b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
150b5c80475SFelix Fietkau 			break;
151b5c80475SFelix Fietkau 
152b5c80475SFelix Fietkau }
153b5c80475SFelix Fietkau 
154b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
155b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
156b5c80475SFelix Fietkau {
157b5c80475SFelix Fietkau 	struct ath_buf *bf;
158b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
159b5c80475SFelix Fietkau 	struct sk_buff *skb;
160b5c80475SFelix Fietkau 
161b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
162b5c80475SFelix Fietkau 
16307236bf3SSujith Manoharan 	while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
164b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
165b5c80475SFelix Fietkau 		BUG_ON(!bf);
166b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
167b5c80475SFelix Fietkau 	}
168b5c80475SFelix Fietkau }
169b5c80475SFelix Fietkau 
170b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
171b5c80475SFelix Fietkau {
172ba542385SMohammed Shafi Shajakhan 	struct ath_hw *ah = sc->sc_ah;
173ba542385SMohammed Shafi Shajakhan 	struct ath_common *common = ath9k_hw_common(ah);
174b5c80475SFelix Fietkau 	struct ath_buf *bf;
175b5c80475SFelix Fietkau 
176b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
177b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
178b5c80475SFelix Fietkau 
179b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
180ba542385SMohammed Shafi Shajakhan 		if (bf->bf_mpdu) {
181ba542385SMohammed Shafi Shajakhan 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
182ba542385SMohammed Shafi Shajakhan 					common->rx_bufsize,
183ba542385SMohammed Shafi Shajakhan 					DMA_BIDIRECTIONAL);
184b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
185ba542385SMohammed Shafi Shajakhan 			bf->bf_buf_addr = 0;
186ba542385SMohammed Shafi Shajakhan 			bf->bf_mpdu = NULL;
187ba542385SMohammed Shafi Shajakhan 		}
188b5c80475SFelix Fietkau 	}
189b5c80475SFelix Fietkau }
190b5c80475SFelix Fietkau 
191b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
192b5c80475SFelix Fietkau {
193b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
194b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
195b5c80475SFelix Fietkau }
196b5c80475SFelix Fietkau 
197b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
198b5c80475SFelix Fietkau {
199b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
200b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
201b5c80475SFelix Fietkau 	struct sk_buff *skb;
202b5c80475SFelix Fietkau 	struct ath_buf *bf;
203b5c80475SFelix Fietkau 	int error = 0, i;
204b5c80475SFelix Fietkau 	u32 size;
205b5c80475SFelix Fietkau 
206b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
207b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
208b5c80475SFelix Fietkau 
209b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
210b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
211b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
212b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
213b5c80475SFelix Fietkau 
214b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
215b81950b1SFelix Fietkau 	bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
216b5c80475SFelix Fietkau 	if (!bf)
217b5c80475SFelix Fietkau 		return -ENOMEM;
218b5c80475SFelix Fietkau 
219b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
220b5c80475SFelix Fietkau 
221b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
222b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
223b5c80475SFelix Fietkau 		if (!skb) {
224b5c80475SFelix Fietkau 			error = -ENOMEM;
225b5c80475SFelix Fietkau 			goto rx_init_fail;
226b5c80475SFelix Fietkau 		}
227b5c80475SFelix Fietkau 
228b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
229b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
230b5c80475SFelix Fietkau 
231b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
232b5c80475SFelix Fietkau 						 common->rx_bufsize,
233b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
234b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
235b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
236b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
237b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2386cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2393800276aSJoe Perches 				ath_err(common,
240b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
241b5c80475SFelix Fietkau 				error = -ENOMEM;
242b5c80475SFelix Fietkau 				goto rx_init_fail;
243b5c80475SFelix Fietkau 		}
244b5c80475SFelix Fietkau 
245b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
246b5c80475SFelix Fietkau 	}
247b5c80475SFelix Fietkau 
248b5c80475SFelix Fietkau 	return 0;
249b5c80475SFelix Fietkau 
250b5c80475SFelix Fietkau rx_init_fail:
251b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
252b5c80475SFelix Fietkau 	return error;
253b5c80475SFelix Fietkau }
254b5c80475SFelix Fietkau 
255b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
256b5c80475SFelix Fietkau {
257b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
2587a897203SSujith Manoharan 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
2597a897203SSujith Manoharan 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
260b5c80475SFelix Fietkau 	ath_opmode_init(sc);
2614cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
262b5c80475SFelix Fietkau }
263b5c80475SFelix Fietkau 
264b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
265b5c80475SFelix Fietkau {
266b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
267b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
268b5c80475SFelix Fietkau }
269b5c80475SFelix Fietkau 
270203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
271203c4805SLuis R. Rodriguez {
27227c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
273203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
274203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
275203c4805SLuis R. Rodriguez 	int error = 0;
276203c4805SLuis R. Rodriguez 
2774bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
278203c4805SLuis R. Rodriguez 
2790d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
2800d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
2810d95521eSFelix Fietkau 
282e87f3d53SSujith Manoharan 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
283b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
284e87f3d53SSujith Manoharan 
285d2182b69SJoe Perches 	ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
286cc861f74SLuis R. Rodriguez 		common->cachelsz, common->rx_bufsize);
287203c4805SLuis R. Rodriguez 
288203c4805SLuis R. Rodriguez 	/* Initialize rx descriptors */
289203c4805SLuis R. Rodriguez 
290203c4805SLuis R. Rodriguez 	error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
2914adfcdedSVasanthakumar Thiagarajan 				  "rx", nbufs, 1, 0);
292203c4805SLuis R. Rodriguez 	if (error != 0) {
2933800276aSJoe Perches 		ath_err(common,
294b5c80475SFelix Fietkau 			"failed to allocate rx descriptors: %d\n",
295b5c80475SFelix Fietkau 			error);
296203c4805SLuis R. Rodriguez 		goto err;
297203c4805SLuis R. Rodriguez 	}
298203c4805SLuis R. Rodriguez 
299203c4805SLuis R. Rodriguez 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
300b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize,
301b5c80475SFelix Fietkau 				      GFP_KERNEL);
302203c4805SLuis R. Rodriguez 		if (skb == NULL) {
303203c4805SLuis R. Rodriguez 			error = -ENOMEM;
304203c4805SLuis R. Rodriguez 			goto err;
305203c4805SLuis R. Rodriguez 		}
306203c4805SLuis R. Rodriguez 
307203c4805SLuis R. Rodriguez 		bf->bf_mpdu = skb;
308203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
309cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
310203c4805SLuis R. Rodriguez 						 DMA_FROM_DEVICE);
311203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
312203c4805SLuis R. Rodriguez 					       bf->bf_buf_addr))) {
313203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(skb);
314203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
3156cf9e995SBen Greear 			bf->bf_buf_addr = 0;
3163800276aSJoe Perches 			ath_err(common,
317203c4805SLuis R. Rodriguez 				"dma_mapping_error() on RX init\n");
318203c4805SLuis R. Rodriguez 			error = -ENOMEM;
319203c4805SLuis R. Rodriguez 			goto err;
320203c4805SLuis R. Rodriguez 		}
321203c4805SLuis R. Rodriguez 	}
322203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
323203c4805SLuis R. Rodriguez err:
324203c4805SLuis R. Rodriguez 	if (error)
325203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
326203c4805SLuis R. Rodriguez 
327203c4805SLuis R. Rodriguez 	return error;
328203c4805SLuis R. Rodriguez }
329203c4805SLuis R. Rodriguez 
330203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
331203c4805SLuis R. Rodriguez {
332cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
333cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
334203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
335203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
336203c4805SLuis R. Rodriguez 
337b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
338b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
339b5c80475SFelix Fietkau 		return;
340e87f3d53SSujith Manoharan 	}
341e87f3d53SSujith Manoharan 
342203c4805SLuis R. Rodriguez 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
343203c4805SLuis R. Rodriguez 		skb = bf->bf_mpdu;
344203c4805SLuis R. Rodriguez 		if (skb) {
345203c4805SLuis R. Rodriguez 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
346b5c80475SFelix Fietkau 					 common->rx_bufsize,
347b5c80475SFelix Fietkau 					 DMA_FROM_DEVICE);
348203c4805SLuis R. Rodriguez 			dev_kfree_skb(skb);
3496cf9e995SBen Greear 			bf->bf_buf_addr = 0;
3506cf9e995SBen Greear 			bf->bf_mpdu = NULL;
351203c4805SLuis R. Rodriguez 		}
352203c4805SLuis R. Rodriguez 	}
353203c4805SLuis R. Rodriguez }
354203c4805SLuis R. Rodriguez 
355203c4805SLuis R. Rodriguez /*
356203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
357203c4805SLuis R. Rodriguez  * operating mode and state:
358203c4805SLuis R. Rodriguez  *
359203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
360203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
361203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
362203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
363203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
364203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
365203c4805SLuis R. Rodriguez  * o accept beacons:
366203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
367203c4805SLuis R. Rodriguez  *     node table entries for peers,
368203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
369203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
370203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
371203c4805SLuis R. Rodriguez  *   - when scanning
372203c4805SLuis R. Rodriguez  */
373203c4805SLuis R. Rodriguez 
374203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
375203c4805SLuis R. Rodriguez {
376203c4805SLuis R. Rodriguez 	u32 rfilt;
377203c4805SLuis R. Rodriguez 
378ac06697cSFelix Fietkau 	rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
379203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
380203c4805SLuis R. Rodriguez 
38173e4937dSZefir Kurtisi 	/* if operating on a DFS channel, enable radar pulse detection */
38273e4937dSZefir Kurtisi 	if (sc->hw->conf.radar_enabled)
38373e4937dSZefir Kurtisi 		rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
38473e4937dSZefir Kurtisi 
3859c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
386203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
387203c4805SLuis R. Rodriguez 
388203c4805SLuis R. Rodriguez 	/*
389203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
390203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
391203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
392203c4805SLuis R. Rodriguez 	 */
3932e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
394203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
395203c4805SLuis R. Rodriguez 
396203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
397203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
398203c4805SLuis R. Rodriguez 
399203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
400cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
401203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
402203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
403203c4805SLuis R. Rodriguez 	else
404203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
405203c4805SLuis R. Rodriguez 
406264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
40766afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
408203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
409203c4805SLuis R. Rodriguez 
4107ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4117ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4127ea310beSSujith 
4137545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
414a549459cSThomas Wagner 		/* This is needed for older chips */
415a549459cSThomas Wagner 		if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
4165eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
417203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
418203c4805SLuis R. Rodriguez 	}
419203c4805SLuis R. Rodriguez 
420b3d7aa43SGabor Juhos 	if (AR_SREV_9550(sc->sc_ah))
421b3d7aa43SGabor Juhos 		rfilt |= ATH9K_RX_FILTER_4ADDRESS;
422b3d7aa43SGabor Juhos 
423203c4805SLuis R. Rodriguez 	return rfilt;
424203c4805SLuis R. Rodriguez 
425203c4805SLuis R. Rodriguez }
426203c4805SLuis R. Rodriguez 
427203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
428203c4805SLuis R. Rodriguez {
429203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
430203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
431203c4805SLuis R. Rodriguez 
432b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
433b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
434b5c80475SFelix Fietkau 		return 0;
435b5c80475SFelix Fietkau 	}
436b5c80475SFelix Fietkau 
437203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
438203c4805SLuis R. Rodriguez 		goto start_recv;
439203c4805SLuis R. Rodriguez 
440e96542e5SFelix Fietkau 	sc->rx.buf_hold = NULL;
441203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
442203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
443203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
444203c4805SLuis R. Rodriguez 	}
445203c4805SLuis R. Rodriguez 
446203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
447203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
448203c4805SLuis R. Rodriguez 		goto start_recv;
449203c4805SLuis R. Rodriguez 
450203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
451203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
452203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
453203c4805SLuis R. Rodriguez 
454203c4805SLuis R. Rodriguez start_recv:
455203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
4564cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
457203c4805SLuis R. Rodriguez 
458203c4805SLuis R. Rodriguez 	return 0;
459203c4805SLuis R. Rodriguez }
460203c4805SLuis R. Rodriguez 
4614b883f02SFelix Fietkau static void ath_flushrecv(struct ath_softc *sc)
4624b883f02SFelix Fietkau {
4634b883f02SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
4644b883f02SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
4654b883f02SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
4664b883f02SFelix Fietkau }
4674b883f02SFelix Fietkau 
468203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
469203c4805SLuis R. Rodriguez {
470203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
4715882da02SFelix Fietkau 	bool stopped, reset = false;
472203c4805SLuis R. Rodriguez 
473d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
474203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
4755882da02SFelix Fietkau 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
476b5c80475SFelix Fietkau 
4774b883f02SFelix Fietkau 	ath_flushrecv(sc);
4784b883f02SFelix Fietkau 
479b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
480b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
481b5c80475SFelix Fietkau 	else
482203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
483203c4805SLuis R. Rodriguez 
484d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
485d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
486d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
487d7fd1b50SBen Greear 			"Could not stop RX, we could be "
48878a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
489d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
490d7fd1b50SBen Greear 	}
4912232d31bSFelix Fietkau 	return stopped && !reset;
492203c4805SLuis R. Rodriguez }
493203c4805SLuis R. Rodriguez 
494cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
495cc65965cSJouni Malinen {
496cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
497cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
498cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
499cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
500cc65965cSJouni Malinen 
501cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
502cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
503cc65965cSJouni Malinen 	end = skb->data + skb->len;
504cc65965cSJouni Malinen 
505cc65965cSJouni Malinen 	while (pos + 2 < end) {
506cc65965cSJouni Malinen 		id = *pos++;
507cc65965cSJouni Malinen 		elen = *pos++;
508cc65965cSJouni Malinen 		if (pos + elen > end)
509cc65965cSJouni Malinen 			break;
510cc65965cSJouni Malinen 
511cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
512cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
513cc65965cSJouni Malinen 				break;
514cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
515cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
516cc65965cSJouni Malinen 				break;
517cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
518cc65965cSJouni Malinen 		}
519cc65965cSJouni Malinen 
520cc65965cSJouni Malinen 		pos += elen;
521cc65965cSJouni Malinen 	}
522cc65965cSJouni Malinen 
523cc65965cSJouni Malinen 	return false;
524cc65965cSJouni Malinen }
525cc65965cSJouni Malinen 
526cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
527cc65965cSJouni Malinen {
5281510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
529cc65965cSJouni Malinen 
530cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
531cc65965cSJouni Malinen 		return;
532cc65965cSJouni Malinen 
5331b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
534293dc5dfSGabor Juhos 
5351b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5361b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
537d2182b69SJoe Perches 		ath_dbg(common, PS,
5381a6404a1SSujith Manoharan 			"Reconfigure beacon timers based on synchronized timestamp\n");
539ef4ad633SSujith Manoharan 		ath9k_set_beacon(sc);
540ccdfeab6SJouni Malinen 	}
541ccdfeab6SJouni Malinen 
542cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
543cc65965cSJouni Malinen 		/*
544cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
54558f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
54658f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
54758f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
54858f5fffdSGabor Juhos 		 * so we are waiting for it as well.
549cc65965cSJouni Malinen 		 */
550d2182b69SJoe Perches 		ath_dbg(common, PS,
551226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
5521b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
553cc65965cSJouni Malinen 		return;
554cc65965cSJouni Malinen 	}
555cc65965cSJouni Malinen 
5561b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
557cc65965cSJouni Malinen 		/*
558cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
559cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
560cc65965cSJouni Malinen 		 * been delivered.
561cc65965cSJouni Malinen 		 */
5621b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
563d2182b69SJoe Perches 		ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
564cc65965cSJouni Malinen 	}
565cc65965cSJouni Malinen }
566cc65965cSJouni Malinen 
567f73c604cSRajkumar Manoharan static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
568cc65965cSJouni Malinen {
569cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
570c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
571cc65965cSJouni Malinen 
572cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
573cc65965cSJouni Malinen 
574cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
575ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
57607c15a3fSSujith Manoharan 	    && mybeacon) {
577cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
57807c15a3fSSujith Manoharan 	} else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
579cc65965cSJouni Malinen 		   (ieee80211_is_data(hdr->frame_control) ||
580cc65965cSJouni Malinen 		    ieee80211_is_action(hdr->frame_control)) &&
581cc65965cSJouni Malinen 		   is_multicast_ether_addr(hdr->addr1) &&
582cc65965cSJouni Malinen 		   !ieee80211_has_moredata(hdr->frame_control)) {
583cc65965cSJouni Malinen 		/*
584cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
585cc65965cSJouni Malinen 		 * point.
586cc65965cSJouni Malinen 		 */
5873fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
588d2182b69SJoe Perches 		ath_dbg(common, PS,
589c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
5901b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
5919a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
5929a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
5931b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
594d2182b69SJoe Perches 		ath_dbg(common, PS,
595226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
5961b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
5971b04b930SSujith 					PS_WAIT_FOR_CAB |
5981b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
5991b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
600cc65965cSJouni Malinen 	}
601cc65965cSJouni Malinen }
602cc65965cSJouni Malinen 
603b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
6043a2923e8SFelix Fietkau 				 enum ath9k_rx_qtype qtype,
6053a2923e8SFelix Fietkau 				 struct ath_rx_status *rs,
6063a2923e8SFelix Fietkau 				 struct ath_buf **dest)
607203c4805SLuis R. Rodriguez {
608b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
609203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
61027c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
611b5c80475SFelix Fietkau 	struct sk_buff *skb;
612b5c80475SFelix Fietkau 	struct ath_buf *bf;
613b5c80475SFelix Fietkau 	int ret;
614203c4805SLuis R. Rodriguez 
615b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
616b5c80475SFelix Fietkau 	if (!skb)
617b5c80475SFelix Fietkau 		return false;
618203c4805SLuis R. Rodriguez 
619b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
620b5c80475SFelix Fietkau 	BUG_ON(!bf);
621b5c80475SFelix Fietkau 
622ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
623b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
624b5c80475SFelix Fietkau 
6253a2923e8SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
626ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
627ce9426d1SMing Lei 		/*let device gain the buffer again*/
628ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
629ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
630b5c80475SFelix Fietkau 		return false;
631ce9426d1SMing Lei 	}
632b5c80475SFelix Fietkau 
633b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
634b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
635b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
636b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
637b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
638b5c80475SFelix Fietkau 
6393a2923e8SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
6403a2923e8SFelix Fietkau 		if (skb) {
641b5c80475SFelix Fietkau 			bf = SKB_CB_ATHBUF(skb);
642b5c80475SFelix Fietkau 			BUG_ON(!bf);
643b5c80475SFelix Fietkau 
644b5c80475SFelix Fietkau 			__skb_unlink(skb, &rx_edma->rx_fifo);
645b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
646b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
647b5c80475SFelix Fietkau 		}
6486bb51c70STom Hughes 
6496bb51c70STom Hughes 		bf = NULL;
6503a2923e8SFelix Fietkau 	}
651b5c80475SFelix Fietkau 
6523a2923e8SFelix Fietkau 	*dest = bf;
653b5c80475SFelix Fietkau 	return true;
654b5c80475SFelix Fietkau }
655b5c80475SFelix Fietkau 
656b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
657b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
658b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
659b5c80475SFelix Fietkau {
6603a2923e8SFelix Fietkau 	struct ath_buf *bf = NULL;
661b5c80475SFelix Fietkau 
6623a2923e8SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
6633a2923e8SFelix Fietkau 		if (!bf)
6643a2923e8SFelix Fietkau 			continue;
665b5c80475SFelix Fietkau 
666b5c80475SFelix Fietkau 		return bf;
667b5c80475SFelix Fietkau 	}
6683a2923e8SFelix Fietkau 	return NULL;
6693a2923e8SFelix Fietkau }
670b5c80475SFelix Fietkau 
671b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
672b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
673b5c80475SFelix Fietkau {
674b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
675b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
676b5c80475SFelix Fietkau 	struct ath_desc *ds;
677b5c80475SFelix Fietkau 	struct ath_buf *bf;
678b5c80475SFelix Fietkau 	int ret;
679203c4805SLuis R. Rodriguez 
680203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
681203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
682b5c80475SFelix Fietkau 		return NULL;
683203c4805SLuis R. Rodriguez 	}
684203c4805SLuis R. Rodriguez 
685203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
686e96542e5SFelix Fietkau 	if (bf == sc->rx.buf_hold)
687e96542e5SFelix Fietkau 		return NULL;
688e96542e5SFelix Fietkau 
689203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
690203c4805SLuis R. Rodriguez 
691203c4805SLuis R. Rodriguez 	/*
692203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
693203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
694203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
695203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
696203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
697203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
698203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
699203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
700203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
701203c4805SLuis R. Rodriguez 	 */
7023de21116SRajkumar Manoharan 	ret = ath9k_hw_rxprocdesc(ah, ds, rs);
703b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
70429bffa96SFelix Fietkau 		struct ath_rx_status trs;
705203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
706203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
707203c4805SLuis R. Rodriguez 
70829bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
709203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
710203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
711b5c80475SFelix Fietkau 			return NULL;
712203c4805SLuis R. Rodriguez 		}
713203c4805SLuis R. Rodriguez 
714203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
715203c4805SLuis R. Rodriguez 
716203c4805SLuis R. Rodriguez 		/*
717203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
718203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
719203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
720203c4805SLuis R. Rodriguez 		 * set or not.
721203c4805SLuis R. Rodriguez 		 *
722203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
723203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
724203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
725203c4805SLuis R. Rodriguez 		 */
726203c4805SLuis R. Rodriguez 
727203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
7283de21116SRajkumar Manoharan 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
729b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
730b5c80475SFelix Fietkau 			return NULL;
731723e7113SFelix Fietkau 
732723e7113SFelix Fietkau 		/*
733723e7113SFelix Fietkau 		 * mark descriptor as zero-length and set the 'more'
734723e7113SFelix Fietkau 		 * flag to ensure that both buffers get discarded
735723e7113SFelix Fietkau 		 */
736723e7113SFelix Fietkau 		rs->rs_datalen = 0;
737723e7113SFelix Fietkau 		rs->rs_more = true;
738203c4805SLuis R. Rodriguez 	}
739203c4805SLuis R. Rodriguez 
740a3dc48e8SFelix Fietkau 	list_del(&bf->list);
741b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
742b5c80475SFelix Fietkau 		return bf;
743203c4805SLuis R. Rodriguez 
744203c4805SLuis R. Rodriguez 	/*
745203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
746203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
747203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
748203c4805SLuis R. Rodriguez 	 */
749ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
750cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
751203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
752203c4805SLuis R. Rodriguez 
753b5c80475SFelix Fietkau 	return bf;
754b5c80475SFelix Fietkau }
755b5c80475SFelix Fietkau 
756d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
757d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
7589f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
759d435700fSSujith 			    struct ieee80211_rx_status *rxs,
760d435700fSSujith 			    struct ath_rx_status *rx_stats,
761d435700fSSujith 			    bool *decrypt_error)
762d435700fSSujith {
763ec205999SFelix Fietkau 	struct ath_softc *sc = (struct ath_softc *) common->priv;
76466760eacSFelix Fietkau 	bool is_mc, is_valid_tkip, strip_mic, mic_error;
765d435700fSSujith 	struct ath_hw *ah = common->ah;
766d435700fSSujith 	__le16 fc;
767d435700fSSujith 
768d435700fSSujith 	fc = hdr->frame_control;
769d435700fSSujith 
77066760eacSFelix Fietkau 	is_mc = !!is_multicast_ether_addr(hdr->addr1);
77166760eacSFelix Fietkau 	is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
77266760eacSFelix Fietkau 		test_bit(rx_stats->rs_keyix, common->tkip_keymap);
773152e585dSBill Jordan 	strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
7742a5783b8SMichael Liang 		ieee80211_has_protected(fc) &&
775152e585dSBill Jordan 		!(rx_stats->rs_status &
776846d9363SFelix Fietkau 		(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
777846d9363SFelix Fietkau 		 ATH9K_RXERR_KEYMISS));
77866760eacSFelix Fietkau 
779f88373faSFelix Fietkau 	/*
780f88373faSFelix Fietkau 	 * Key miss events are only relevant for pairwise keys where the
781f88373faSFelix Fietkau 	 * descriptor does contain a valid key index. This has been observed
782f88373faSFelix Fietkau 	 * mostly with CCMP encryption.
783f88373faSFelix Fietkau 	 */
784bed3d9c0SFelix Fietkau 	if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
785bed3d9c0SFelix Fietkau 	    !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
786f88373faSFelix Fietkau 		rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
787f88373faSFelix Fietkau 
78866760eacSFelix Fietkau 	mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
78966760eacSFelix Fietkau 		!ieee80211_has_morefrags(fc) &&
79066760eacSFelix Fietkau 		!(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
79166760eacSFelix Fietkau 		(rx_stats->rs_status & ATH9K_RXERR_MIC);
79266760eacSFelix Fietkau 
793d435700fSSujith 	/*
794d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
795d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
796d435700fSSujith 	 * rs_more will be false at the last element of the chained
797d435700fSSujith 	 * descriptors.
798d435700fSSujith 	 */
799d435700fSSujith 	if (rx_stats->rs_status != 0) {
800846d9363SFelix Fietkau 		u8 status_mask;
801846d9363SFelix Fietkau 
80266760eacSFelix Fietkau 		if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
803d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
80466760eacSFelix Fietkau 			mic_error = false;
80566760eacSFelix Fietkau 		}
806d435700fSSujith 
807846d9363SFelix Fietkau 		if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
808846d9363SFelix Fietkau 		    (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
809d435700fSSujith 			*decrypt_error = true;
81066760eacSFelix Fietkau 			mic_error = false;
811d435700fSSujith 		}
81266760eacSFelix Fietkau 
813d435700fSSujith 		/*
814d435700fSSujith 		 * Reject error frames with the exception of
815d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
816d435700fSSujith 		 * we also ignore the CRC error.
817d435700fSSujith 		 */
818846d9363SFelix Fietkau 		status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
819846d9363SFelix Fietkau 			      ATH9K_RXERR_KEYMISS;
820846d9363SFelix Fietkau 
821ec205999SFelix Fietkau 		if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
822846d9363SFelix Fietkau 			status_mask |= ATH9K_RXERR_CRC;
823846d9363SFelix Fietkau 
824846d9363SFelix Fietkau 		if (rx_stats->rs_status & ~status_mask)
825d435700fSSujith 			return false;
826d435700fSSujith 	}
82766760eacSFelix Fietkau 
82866760eacSFelix Fietkau 	/*
82966760eacSFelix Fietkau 	 * For unicast frames the MIC error bit can have false positives,
83066760eacSFelix Fietkau 	 * so all MIC error reports need to be validated in software.
83166760eacSFelix Fietkau 	 * False negatives are not common, so skip software verification
83266760eacSFelix Fietkau 	 * if the hardware considers the MIC valid.
83366760eacSFelix Fietkau 	 */
83466760eacSFelix Fietkau 	if (strip_mic)
83566760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_STRIPPED;
83666760eacSFelix Fietkau 	else if (is_mc && mic_error)
83766760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_ERROR;
83866760eacSFelix Fietkau 
839d435700fSSujith 	return true;
840d435700fSSujith }
841d435700fSSujith 
842d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
843d435700fSSujith 			      struct ieee80211_hw *hw,
844d435700fSSujith 			      struct ath_rx_status *rx_stats,
8459f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
846d435700fSSujith {
847d435700fSSujith 	struct ieee80211_supported_band *sband;
848d435700fSSujith 	enum ieee80211_band band;
849d435700fSSujith 	unsigned int i = 0;
850990e08a0SBen Greear 	struct ath_softc __maybe_unused *sc = common->priv;
851d435700fSSujith 
852675a0b04SKarl Beldan 	band = hw->conf.chandef.chan->band;
853d435700fSSujith 	sband = hw->wiphy->bands[band];
854d435700fSSujith 
855d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
856d435700fSSujith 		/* HT rate */
857d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
858ab276103SOleksij Rempel 		rxs->flag |= rx_stats->flag;
859d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
860d435700fSSujith 		return 0;
861d435700fSSujith 	}
862d435700fSSujith 
863d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
864d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
865d435700fSSujith 			rxs->rate_idx = i;
866d435700fSSujith 			return 0;
867d435700fSSujith 		}
868d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
869d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
870d435700fSSujith 			rxs->rate_idx = i;
871d435700fSSujith 			return 0;
872d435700fSSujith 		}
873d435700fSSujith 	}
874d435700fSSujith 
875d435700fSSujith 	/*
876d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
877d435700fSSujith 	 * because hardware has already validated this frame as OK.
878d435700fSSujith 	 */
879d2182b69SJoe Perches 	ath_dbg(common, ANY,
880226afe68SJoe Perches 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
881226afe68SJoe Perches 		rx_stats->rs_rate);
88215072189SBen Greear 	RX_STAT_INC(rx_rate_err);
883d435700fSSujith 	return -EINVAL;
884d435700fSSujith }
885d435700fSSujith 
886d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
887d435700fSSujith 			       struct ieee80211_hw *hw,
8889f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
889d435700fSSujith 			       struct ath_rx_status *rx_stats)
890d435700fSSujith {
8919ac58615SFelix Fietkau 	struct ath_softc *sc = hw->priv;
892d435700fSSujith 	struct ath_hw *ah = common->ah;
8939fa23e17SFelix Fietkau 	int last_rssi;
8942ef16755SFelix Fietkau 	int rssi = rx_stats->rs_rssi;
895d435700fSSujith 
896cf3af748SRajkumar Manoharan 	if (!rx_stats->is_mybeacon ||
897cf3af748SRajkumar Manoharan 	    ((ah->opmode != NL80211_IFTYPE_STATION) &&
898cf3af748SRajkumar Manoharan 	     (ah->opmode != NL80211_IFTYPE_ADHOC)))
8999fa23e17SFelix Fietkau 		return;
9009fa23e17SFelix Fietkau 
9019fa23e17SFelix Fietkau 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9029ac58615SFelix Fietkau 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
903686b9cb9SBen Greear 
9049ac58615SFelix Fietkau 	last_rssi = sc->last_rssi;
905d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
9062ef16755SFelix Fietkau 		rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
9072ef16755SFelix Fietkau 	if (rssi < 0)
9082ef16755SFelix Fietkau 		rssi = 0;
909d435700fSSujith 
910d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
9112ef16755SFelix Fietkau 	ah->stats.avgbrssi = rssi;
912d435700fSSujith }
913d435700fSSujith 
914e0dd1a96SSujith Manoharan static void ath9k_process_tsf(struct ath_rx_status *rs,
915e0dd1a96SSujith Manoharan 			      struct ieee80211_rx_status *rxs,
916e0dd1a96SSujith Manoharan 			      u64 tsf)
917e0dd1a96SSujith Manoharan {
918e0dd1a96SSujith Manoharan 	u32 tsf_lower = tsf & 0xffffffff;
919e0dd1a96SSujith Manoharan 
920e0dd1a96SSujith Manoharan 	rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
921e0dd1a96SSujith Manoharan 	if (rs->rs_tstamp > tsf_lower &&
922e0dd1a96SSujith Manoharan 	    unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
923e0dd1a96SSujith Manoharan 		rxs->mactime -= 0x100000000ULL;
924e0dd1a96SSujith Manoharan 
925e0dd1a96SSujith Manoharan 	if (rs->rs_tstamp < tsf_lower &&
926e0dd1a96SSujith Manoharan 	    unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
927e0dd1a96SSujith Manoharan 		rxs->mactime += 0x100000000ULL;
928e0dd1a96SSujith Manoharan }
929e0dd1a96SSujith Manoharan 
9303105b672SSujith Manoharan #ifdef CONFIG_ATH9K_DEBUGFS
9313105b672SSujith Manoharan static s8 fix_rssi_inv_only(u8 rssi_val)
9323105b672SSujith Manoharan {
9333105b672SSujith Manoharan 	if (rssi_val == 128)
9343105b672SSujith Manoharan 		rssi_val = 0;
9353105b672SSujith Manoharan 	return (s8) rssi_val;
9363105b672SSujith Manoharan }
9373105b672SSujith Manoharan #endif
9383105b672SSujith Manoharan 
9393105b672SSujith Manoharan /* returns 1 if this was a spectral frame, even if not handled. */
9403105b672SSujith Manoharan static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
9413105b672SSujith Manoharan 			   struct ath_rx_status *rs, u64 tsf)
9423105b672SSujith Manoharan {
9433105b672SSujith Manoharan #ifdef CONFIG_ATH9K_DEBUGFS
9443105b672SSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
9453105b672SSujith Manoharan 	u8 bins[SPECTRAL_HT20_NUM_BINS];
9463105b672SSujith Manoharan 	u8 *vdata = (u8 *)hdr;
9473105b672SSujith Manoharan 	struct fft_sample_ht20 fft_sample;
9483105b672SSujith Manoharan 	struct ath_radar_info *radar_info;
9493105b672SSujith Manoharan 	struct ath_ht20_mag_info *mag_info;
9503105b672SSujith Manoharan 	int len = rs->rs_datalen;
9513105b672SSujith Manoharan 	int dc_pos;
9523105b672SSujith Manoharan 	u16 length, max_magnitude;
9533105b672SSujith Manoharan 
9543105b672SSujith Manoharan 	/* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
9553105b672SSujith Manoharan 	 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
9563105b672SSujith Manoharan 	 * yet, but this is supposed to be possible as well.
9573105b672SSujith Manoharan 	 */
9583105b672SSujith Manoharan 	if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
9593105b672SSujith Manoharan 	    rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
9603105b672SSujith Manoharan 	    rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
9613105b672SSujith Manoharan 		return 0;
9623105b672SSujith Manoharan 
9633105b672SSujith Manoharan 	/* check if spectral scan bit is set. This does not have to be checked
9643105b672SSujith Manoharan 	 * if received through a SPECTRAL phy error, but shouldn't hurt.
9653105b672SSujith Manoharan 	 */
9663105b672SSujith Manoharan 	radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
9673105b672SSujith Manoharan 	if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
9683105b672SSujith Manoharan 		return 0;
9693105b672SSujith Manoharan 
9703105b672SSujith Manoharan 	/* Variation in the data length is possible and will be fixed later.
9713105b672SSujith Manoharan 	 * Note that we only support HT20 for now.
9723105b672SSujith Manoharan 	 *
9733105b672SSujith Manoharan 	 * TODO: add HT20_40 support as well.
9743105b672SSujith Manoharan 	 */
9753105b672SSujith Manoharan 	if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
9763105b672SSujith Manoharan 	    (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
9773105b672SSujith Manoharan 		return 1;
9783105b672SSujith Manoharan 
9793105b672SSujith Manoharan 	fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
9803105b672SSujith Manoharan 	length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
9813105b672SSujith Manoharan 	fft_sample.tlv.length = __cpu_to_be16(length);
9823105b672SSujith Manoharan 
9833105b672SSujith Manoharan 	fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
9843105b672SSujith Manoharan 	fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
9853105b672SSujith Manoharan 	fft_sample.noise = ah->noise;
9863105b672SSujith Manoharan 
9873105b672SSujith Manoharan 	switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
9883105b672SSujith Manoharan 	case 0:
9893105b672SSujith Manoharan 		/* length correct, nothing to do. */
9903105b672SSujith Manoharan 		memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
9913105b672SSujith Manoharan 		break;
9923105b672SSujith Manoharan 	case -1:
9933105b672SSujith Manoharan 		/* first byte missing, duplicate it. */
9943105b672SSujith Manoharan 		memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
9953105b672SSujith Manoharan 		bins[0] = vdata[0];
9963105b672SSujith Manoharan 		break;
9973105b672SSujith Manoharan 	case 2:
9983105b672SSujith Manoharan 		/* MAC added 2 extra bytes at bin 30 and 32, remove them. */
9993105b672SSujith Manoharan 		memcpy(bins, vdata, 30);
10003105b672SSujith Manoharan 		bins[30] = vdata[31];
10013105b672SSujith Manoharan 		memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
10023105b672SSujith Manoharan 		break;
10033105b672SSujith Manoharan 	case 1:
10043105b672SSujith Manoharan 		/* MAC added 2 extra bytes AND first byte is missing. */
10053105b672SSujith Manoharan 		bins[0] = vdata[0];
10063105b672SSujith Manoharan 		memcpy(&bins[0], vdata, 30);
10073105b672SSujith Manoharan 		bins[31] = vdata[31];
10083105b672SSujith Manoharan 		memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
10093105b672SSujith Manoharan 		break;
10103105b672SSujith Manoharan 	default:
10113105b672SSujith Manoharan 		return 1;
10123105b672SSujith Manoharan 	}
10133105b672SSujith Manoharan 
10143105b672SSujith Manoharan 	/* DC value (value in the middle) is the blind spot of the spectral
10153105b672SSujith Manoharan 	 * sample and invalid, interpolate it.
10163105b672SSujith Manoharan 	 */
10173105b672SSujith Manoharan 	dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
10183105b672SSujith Manoharan 	bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
10193105b672SSujith Manoharan 
10203105b672SSujith Manoharan 	/* mag data is at the end of the frame, in front of radar_info */
10213105b672SSujith Manoharan 	mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
10223105b672SSujith Manoharan 
10233105b672SSujith Manoharan 	/* copy raw bins without scaling them */
10243105b672SSujith Manoharan 	memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
10253105b672SSujith Manoharan 	fft_sample.max_exp = mag_info->max_exp & 0xf;
10263105b672SSujith Manoharan 
10273105b672SSujith Manoharan 	max_magnitude = spectral_max_magnitude(mag_info->all_bins);
10283105b672SSujith Manoharan 	fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
10293105b672SSujith Manoharan 	fft_sample.max_index = spectral_max_index(mag_info->all_bins);
10303105b672SSujith Manoharan 	fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
10313105b672SSujith Manoharan 	fft_sample.tsf = __cpu_to_be64(tsf);
10323105b672SSujith Manoharan 
10333105b672SSujith Manoharan 	ath_debug_send_fft_sample(sc, &fft_sample.tlv);
10343105b672SSujith Manoharan 	return 1;
10353105b672SSujith Manoharan #else
10363105b672SSujith Manoharan 	return 0;
10373105b672SSujith Manoharan #endif
10383105b672SSujith Manoharan }
10393105b672SSujith Manoharan 
1040d435700fSSujith /*
1041d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
1042d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
1043d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
1044d435700fSSujith  */
1045723e7113SFelix Fietkau static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
10469f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
1047d435700fSSujith 				   struct ath_rx_status *rx_stats,
1048d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
1049e0dd1a96SSujith Manoharan 				   bool *decrypt_error, u64 tsf)
1050d435700fSSujith {
1051723e7113SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1052723e7113SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1053723e7113SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
1054723e7113SFelix Fietkau 	bool discard_current = sc->rx.discard_next;
1055723e7113SFelix Fietkau 
10565871d2d7SSujith Manoharan 	/*
10575871d2d7SSujith Manoharan 	 * Discard corrupt descriptors which are marked in
10585871d2d7SSujith Manoharan 	 * ath_get_next_rx_buf().
10595871d2d7SSujith Manoharan 	 */
1060723e7113SFelix Fietkau 	sc->rx.discard_next = rx_stats->rs_more;
1061723e7113SFelix Fietkau 	if (discard_current)
1062723e7113SFelix Fietkau 		return -EINVAL;
1063f749b946SFelix Fietkau 
1064d435700fSSujith 	/*
10655871d2d7SSujith Manoharan 	 * Discard zero-length packets.
10665871d2d7SSujith Manoharan 	 */
10675871d2d7SSujith Manoharan 	if (!rx_stats->rs_datalen) {
10685871d2d7SSujith Manoharan 		RX_STAT_INC(rx_len_err);
10695871d2d7SSujith Manoharan 		return -EINVAL;
10705871d2d7SSujith Manoharan 	}
10715871d2d7SSujith Manoharan 
10725871d2d7SSujith Manoharan         /*
10735871d2d7SSujith Manoharan          * rs_status follows rs_datalen so if rs_datalen is too large
10745871d2d7SSujith Manoharan          * we can take a hint that hardware corrupted it, so ignore
10755871d2d7SSujith Manoharan          * those frames.
10765871d2d7SSujith Manoharan          */
10775871d2d7SSujith Manoharan 	if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
10785871d2d7SSujith Manoharan 		RX_STAT_INC(rx_len_err);
10795871d2d7SSujith Manoharan 		return -EINVAL;
10805871d2d7SSujith Manoharan 	}
10815871d2d7SSujith Manoharan 
10824a470647SSujith Manoharan 	/* Only use status info from the last fragment */
10834a470647SSujith Manoharan 	if (rx_stats->rs_more)
10844a470647SSujith Manoharan 		return 0;
10854a470647SSujith Manoharan 
1086e0dd1a96SSujith Manoharan 	ath9k_process_tsf(rx_stats, rx_status, tsf);
1087*5e85a32aSSujith Manoharan 	ath_debug_stat_rx(sc, rx_stats);
1088e0dd1a96SSujith Manoharan 
10895871d2d7SSujith Manoharan 	/*
10906b87d71cSSujith Manoharan 	 * Process PHY errors and return so that the packet
10916b87d71cSSujith Manoharan 	 * can be dropped.
10926b87d71cSSujith Manoharan 	 */
10936b87d71cSSujith Manoharan 	if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
10946b87d71cSSujith Manoharan 		ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
10956b87d71cSSujith Manoharan 		if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
10966b87d71cSSujith Manoharan 			RX_STAT_INC(rx_spectral);
10976b87d71cSSujith Manoharan 
10986b87d71cSSujith Manoharan 		return -EINVAL;
10996b87d71cSSujith Manoharan 	}
11006b87d71cSSujith Manoharan 
11016b87d71cSSujith Manoharan 	/*
1102d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
1103d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
1104d435700fSSujith 	 */
11059f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
1106d435700fSSujith 		return -EINVAL;
1107d435700fSSujith 
11089f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1109d435700fSSujith 		return -EINVAL;
1110d435700fSSujith 
111174a97755SSujith Manoharan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
111274a97755SSujith Manoharan 
1113675a0b04SKarl Beldan 	rx_status->band = hw->conf.chandef.chan->band;
1114675a0b04SKarl Beldan 	rx_status->freq = hw->conf.chandef.chan->center_freq;
1115f749b946SFelix Fietkau 	rx_status->signal = ah->noise + rx_stats->rs_rssi;
1116d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
111796d21371SThomas Pedersen 	rx_status->flag |= RX_FLAG_MACTIME_END;
11182ef16755SFelix Fietkau 	if (rx_stats->rs_moreaggr)
11192ef16755SFelix Fietkau 		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1120d435700fSSujith 
1121723e7113SFelix Fietkau 	sc->rx.discard_next = false;
1122d435700fSSujith 	return 0;
1123d435700fSSujith }
1124d435700fSSujith 
1125d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
1126d435700fSSujith 				     struct sk_buff *skb,
1127d435700fSSujith 				     struct ath_rx_status *rx_stats,
1128d435700fSSujith 				     struct ieee80211_rx_status *rxs,
1129d435700fSSujith 				     bool decrypt_error)
1130d435700fSSujith {
1131d435700fSSujith 	struct ath_hw *ah = common->ah;
1132d435700fSSujith 	struct ieee80211_hdr *hdr;
1133d435700fSSujith 	int hdrlen, padpos, padsize;
1134d435700fSSujith 	u8 keyix;
1135d435700fSSujith 	__le16 fc;
1136d435700fSSujith 
1137d435700fSSujith 	/* see if any padding is done by the hw and remove it */
1138d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1139d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1140d435700fSSujith 	fc = hdr->frame_control;
1141c60c9929SFelix Fietkau 	padpos = ieee80211_hdrlen(fc);
1142d435700fSSujith 
1143d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1144d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1145d435700fSSujith 	 * padsize would take into account odd header lengths:
1146d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1147d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1148d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1149d435700fSSujith 	 * not try to remove padding from short control frames that do
1150d435700fSSujith 	 * not have payload. */
1151d435700fSSujith 	padsize = padpos & 3;
1152d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1153d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1154d435700fSSujith 		skb_pull(skb, padsize);
1155d435700fSSujith 	}
1156d435700fSSujith 
1157d435700fSSujith 	keyix = rx_stats->rs_keyix;
1158d435700fSSujith 
1159d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1160d435700fSSujith 	    ieee80211_has_protected(fc)) {
1161d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1162d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1163d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1164d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1165d435700fSSujith 
1166d435700fSSujith 		if (test_bit(keyix, common->keymap))
1167d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1168d435700fSSujith 	}
1169d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1170d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1171d435700fSSujith 	    ieee80211_is_mgmt(fc))
1172d435700fSSujith 		/* Use software decrypt for management frames. */
1173d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1174d435700fSSujith }
1175b5c80475SFelix Fietkau 
117621fbbca3SChristian Lamparter static void ath9k_apply_ampdu_details(struct ath_softc *sc,
117721fbbca3SChristian Lamparter 	struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
117821fbbca3SChristian Lamparter {
117921fbbca3SChristian Lamparter 	if (rs->rs_isaggr) {
118021fbbca3SChristian Lamparter 		rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
118121fbbca3SChristian Lamparter 
118221fbbca3SChristian Lamparter 		rxs->ampdu_reference = sc->rx.ampdu_ref;
118321fbbca3SChristian Lamparter 
118421fbbca3SChristian Lamparter 		if (!rs->rs_moreaggr) {
118521fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
118621fbbca3SChristian Lamparter 			sc->rx.ampdu_ref++;
118721fbbca3SChristian Lamparter 		}
118821fbbca3SChristian Lamparter 
118921fbbca3SChristian Lamparter 		if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
119021fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
119121fbbca3SChristian Lamparter 	}
119221fbbca3SChristian Lamparter }
119321fbbca3SChristian Lamparter 
1194f6307ddaSSujith Manoharan static bool ath9k_is_mybeacon(struct ath_softc *sc, struct sk_buff *skb)
1195f6307ddaSSujith Manoharan {
1196f6307ddaSSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
1197f6307ddaSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(ah);
1198f6307ddaSSujith Manoharan 	struct ieee80211_hdr *hdr;
1199f6307ddaSSujith Manoharan 
1200f6307ddaSSujith Manoharan 	hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
1201f6307ddaSSujith Manoharan 
1202f6307ddaSSujith Manoharan 	if (ieee80211_is_beacon(hdr->frame_control)) {
1203f6307ddaSSujith Manoharan 		RX_STAT_INC(rx_beacons);
1204f6307ddaSSujith Manoharan 		if (!is_zero_ether_addr(common->curbssid) &&
1205f6307ddaSSujith Manoharan 		    ether_addr_equal(hdr->addr3, common->curbssid))
1206f6307ddaSSujith Manoharan 			return true;
1207f6307ddaSSujith Manoharan 	}
1208f6307ddaSSujith Manoharan 
1209f6307ddaSSujith Manoharan 	return false;
1210f6307ddaSSujith Manoharan }
1211f6307ddaSSujith Manoharan 
1212b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1213b5c80475SFelix Fietkau {
1214b5c80475SFelix Fietkau 	struct ath_buf *bf;
12150d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1216b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1217b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
121816fe28e9SSujith Manoharan 	struct ath9k_hw_capabilities *pCap = &ah->caps;
1219b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
12207545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1221b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1222b5c80475SFelix Fietkau 	int retval;
1223b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1224b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1225b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1226b5c80475SFelix Fietkau 	int dma_type;
1227a6d2055bSFelix Fietkau 	u64 tsf = 0;
12288ab2cd09SLuis R. Rodriguez 	unsigned long flags;
12292e1cd495SFelix Fietkau 	dma_addr_t new_buf_addr;
1230b5c80475SFelix Fietkau 
1231b5c80475SFelix Fietkau 	if (edma)
1232b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
123356824223SMing Lei 	else
123456824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1235b5c80475SFelix Fietkau 
1236b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1237b5c80475SFelix Fietkau 
1238a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1239a6d2055bSFelix Fietkau 
1240b5c80475SFelix Fietkau 	do {
1241e1352fdeSLorenzo Bianconi 		bool decrypt_error = false;
1242b5c80475SFelix Fietkau 
1243b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1244b5c80475SFelix Fietkau 		if (edma)
1245b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1246b5c80475SFelix Fietkau 		else
1247b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1248b5c80475SFelix Fietkau 
1249b5c80475SFelix Fietkau 		if (!bf)
1250b5c80475SFelix Fietkau 			break;
1251b5c80475SFelix Fietkau 
1252b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1253b5c80475SFelix Fietkau 		if (!skb)
1254b5c80475SFelix Fietkau 			continue;
1255b5c80475SFelix Fietkau 
12560d95521eSFelix Fietkau 		/*
12570d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
12580d95521eSFelix Fietkau 		 * the last one.
12590d95521eSFelix Fietkau 		 */
12600d95521eSFelix Fietkau 		if (sc->rx.frag)
12610d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
12620d95521eSFelix Fietkau 		else
12630d95521eSFelix Fietkau 			hdr_skb = skb;
12640d95521eSFelix Fietkau 
1265f6307ddaSSujith Manoharan 		rs.is_mybeacon = ath9k_is_mybeacon(sc, hdr_skb);
1266f6307ddaSSujith Manoharan 
1267f6307ddaSSujith Manoharan 		hdr = (struct ieee80211_hdr *) (hdr_skb->data +
1268f6307ddaSSujith Manoharan 						ah->caps.rx_status_len);
12695ca42627SLuis R. Rodriguez 
1270be41b052SMohammed Shafi Shajakhan 		if (ieee80211_is_data_present(hdr->frame_control) &&
1271be41b052SMohammed Shafi Shajakhan 		    !ieee80211_is_qos_nullfunc(hdr->frame_control))
12726995fb80SRajkumar Manoharan 			sc->rx.num_pkts++;
1273be41b052SMohammed Shafi Shajakhan 
1274f6307ddaSSujith Manoharan 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
1275ffb1c56aSAshok Nagarajan 		memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1276ffb1c56aSAshok Nagarajan 
1277723e7113SFelix Fietkau 		retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
1278e0dd1a96SSujith Manoharan 						 &decrypt_error, tsf);
127983c76570SZefir Kurtisi 		if (retval)
128083c76570SZefir Kurtisi 			goto requeue_drop_frag;
128183c76570SZefir Kurtisi 
128201e18918SRajkumar Manoharan 		if (rs.is_mybeacon) {
128301e18918SRajkumar Manoharan 			sc->hw_busy_count = 0;
128401e18918SRajkumar Manoharan 			ath_start_rx_poll(sc, 3);
128501e18918SRajkumar Manoharan 		}
1286203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1287203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1288cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1289203c4805SLuis R. Rodriguez 
1290203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1291203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1292203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1293203c4805SLuis R. Rodriguez 		 * processing. */
129415072189SBen Greear 		if (!requeue_skb) {
129515072189SBen Greear 			RX_STAT_INC(rx_oom_err);
12960d95521eSFelix Fietkau 			goto requeue_drop_frag;
129715072189SBen Greear 		}
1298203c4805SLuis R. Rodriguez 
12992e1cd495SFelix Fietkau 		/* We will now give hardware our shiny new allocated skb */
13002e1cd495SFelix Fietkau 		new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
13012e1cd495SFelix Fietkau 					      common->rx_bufsize, dma_type);
13022e1cd495SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
13032e1cd495SFelix Fietkau 			dev_kfree_skb_any(requeue_skb);
13042e1cd495SFelix Fietkau 			goto requeue_drop_frag;
13052e1cd495SFelix Fietkau 		}
13062e1cd495SFelix Fietkau 
1307203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1308203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
13092e1cd495SFelix Fietkau 				 common->rx_bufsize, dma_type);
1310203c4805SLuis R. Rodriguez 
1311176f0e84SSujith Manoharan 		bf->bf_mpdu = requeue_skb;
1312176f0e84SSujith Manoharan 		bf->bf_buf_addr = new_buf_addr;
1313176f0e84SSujith Manoharan 
1314b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1315b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1316b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1317203c4805SLuis R. Rodriguez 
13180d95521eSFelix Fietkau 		if (!rs.rs_more)
13190d95521eSFelix Fietkau 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1320c9b14170SLuis R. Rodriguez 						 rxs, decrypt_error);
1321203c4805SLuis R. Rodriguez 
13220d95521eSFelix Fietkau 		if (rs.rs_more) {
132315072189SBen Greear 			RX_STAT_INC(rx_frags);
13240d95521eSFelix Fietkau 			/*
13250d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
13260d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
13270d95521eSFelix Fietkau 			 * scatter-gather operation.
13280d95521eSFelix Fietkau 			 */
13290d95521eSFelix Fietkau 			if (sc->rx.frag) {
13300d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
13310d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
13320d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
133315072189SBen Greear 				RX_STAT_INC(rx_too_many_frags_err);
13340d95521eSFelix Fietkau 				skb = NULL;
13350d95521eSFelix Fietkau 			}
13360d95521eSFelix Fietkau 			sc->rx.frag = skb;
13370d95521eSFelix Fietkau 			goto requeue;
13380d95521eSFelix Fietkau 		}
13393747c3eeSFelix Fietkau 		if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
13403747c3eeSFelix Fietkau 			goto requeue_drop_frag;
13410d95521eSFelix Fietkau 
13420d95521eSFelix Fietkau 		if (sc->rx.frag) {
13430d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
13440d95521eSFelix Fietkau 
13450d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
13460d95521eSFelix Fietkau 				dev_kfree_skb(skb);
134715072189SBen Greear 				RX_STAT_INC(rx_oom_err);
13480d95521eSFelix Fietkau 				goto requeue_drop_frag;
13490d95521eSFelix Fietkau 			}
13500d95521eSFelix Fietkau 
1351b5447ff9SEric Dumazet 			sc->rx.frag = NULL;
1352b5447ff9SEric Dumazet 
13530d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
13540d95521eSFelix Fietkau 						  skb->len);
13550d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
13560d95521eSFelix Fietkau 			skb = hdr_skb;
13570d95521eSFelix Fietkau 		}
13580d95521eSFelix Fietkau 
135966760eacSFelix Fietkau 		if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
136066760eacSFelix Fietkau 			skb_trim(skb, skb->len - 8);
136166760eacSFelix Fietkau 
13628ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1363aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
13641b04b930SSujith 				     PS_WAIT_FOR_CAB |
1365aaef24b4SMohammed Shafi Shajakhan 				     PS_WAIT_FOR_PSPOLL_DATA)) ||
1366cedc7e3dSMohammed Shafi Shajakhan 		    ath9k_check_auto_sleep(sc))
1367f73c604cSRajkumar Manoharan 			ath_rx_ps(sc, skb, rs.is_mybeacon);
13688ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1369cc65965cSJouni Malinen 
137016fe28e9SSujith Manoharan 		/*
137116fe28e9SSujith Manoharan 		 * Run the LNA combining algorithm only in these cases:
137216fe28e9SSujith Manoharan 		 *
137316fe28e9SSujith Manoharan 		 * Standalone WLAN cards with both LNA/Antenna diversity
137416fe28e9SSujith Manoharan 		 * enabled in the EEPROM.
137516fe28e9SSujith Manoharan 		 *
137616fe28e9SSujith Manoharan 		 * WLAN+BT cards which are in the supported card list
137716fe28e9SSujith Manoharan 		 * in ath_pci_id_table and the user has loaded the
137816fe28e9SSujith Manoharan 		 * driver with "bt_ant_diversity" set to true.
137916fe28e9SSujith Manoharan 		 */
138016fe28e9SSujith Manoharan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
138116fe28e9SSujith Manoharan 			/*
138216fe28e9SSujith Manoharan 			 * Change the default rx antenna if rx diversity
138316fe28e9SSujith Manoharan 			 * chooses the other antenna 3 times in a row.
138416fe28e9SSujith Manoharan 			 */
138516fe28e9SSujith Manoharan 			if (sc->rx.defant != rs.rs_antenna) {
138616fe28e9SSujith Manoharan 				if (++sc->rx.rxotherant >= 3)
138716fe28e9SSujith Manoharan 					ath_setdefantenna(sc, rs.rs_antenna);
138816fe28e9SSujith Manoharan 			} else {
138916fe28e9SSujith Manoharan 				sc->rx.rxotherant = 0;
139016fe28e9SSujith Manoharan 			}
139116fe28e9SSujith Manoharan 
139216fe28e9SSujith Manoharan 			if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
139316fe28e9SSujith Manoharan 				if (common->bt_ant_diversity)
1394102885a5SVasanthakumar Thiagarajan 					ath_ant_comb_scan(sc, &rs);
139516fe28e9SSujith Manoharan 			} else {
139616fe28e9SSujith Manoharan 				ath_ant_comb_scan(sc, &rs);
139716fe28e9SSujith Manoharan 			}
139816fe28e9SSujith Manoharan 		}
1399102885a5SVasanthakumar Thiagarajan 
140021fbbca3SChristian Lamparter 		ath9k_apply_ampdu_details(sc, &rs, rxs);
140121fbbca3SChristian Lamparter 
14027545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1403cc65965cSJouni Malinen 
14040d95521eSFelix Fietkau requeue_drop_frag:
14050d95521eSFelix Fietkau 		if (sc->rx.frag) {
14060d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
14070d95521eSFelix Fietkau 			sc->rx.frag = NULL;
14080d95521eSFelix Fietkau 		}
1409203c4805SLuis R. Rodriguez requeue:
1410b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
1411a3dc48e8SFelix Fietkau 		if (flush)
1412a3dc48e8SFelix Fietkau 			continue;
1413a3dc48e8SFelix Fietkau 
1414a3dc48e8SFelix Fietkau 		if (edma) {
1415b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1416b5c80475SFelix Fietkau 		} else {
1417e96542e5SFelix Fietkau 			ath_rx_buf_relink(sc, bf);
141895294973SFelix Fietkau 			ath9k_hw_rxena(ah);
1419b5c80475SFelix Fietkau 		}
1420203c4805SLuis R. Rodriguez 	} while (1);
1421203c4805SLuis R. Rodriguez 
142229ab0b36SRajkumar Manoharan 	if (!(ah->imask & ATH9K_INT_RXEOL)) {
142329ab0b36SRajkumar Manoharan 		ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
142472d874c6SFelix Fietkau 		ath9k_hw_set_interrupts(ah);
142529ab0b36SRajkumar Manoharan 	}
142629ab0b36SRajkumar Manoharan 
1427203c4805SLuis R. Rodriguez 	return 0;
1428203c4805SLuis R. Rodriguez }
1429