xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision 128243740923b2b14d16be2545233a75ba4a2c0f)
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 	ATH_RXBUF_RESET(bf);
46203c4805SLuis R. Rodriguez 
47203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
48203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
49203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
50203c4805SLuis R. Rodriguez 
51203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
52203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
539680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
54203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
55203c4805SLuis R. Rodriguez 
56cc861f74SLuis R. Rodriguez 	/*
57cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
58203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
59cc861f74SLuis R. Rodriguez 	 * to process
60cc861f74SLuis R. Rodriguez 	 */
61203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
62cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
63203c4805SLuis R. Rodriguez 			     0);
64203c4805SLuis R. Rodriguez 
65203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
66203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
67203c4805SLuis R. Rodriguez 	else
68203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
69203c4805SLuis R. Rodriguez 
70203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
71203c4805SLuis R. Rodriguez }
72203c4805SLuis R. Rodriguez 
73203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
74203c4805SLuis R. Rodriguez {
75203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
76203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
77203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
78203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
79203c4805SLuis R. Rodriguez }
80203c4805SLuis R. Rodriguez 
81203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
82203c4805SLuis R. Rodriguez {
83203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
841510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
851510718dSLuis R. Rodriguez 
86203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
87203c4805SLuis R. Rodriguez 
88203c4805SLuis R. Rodriguez 	/* configure rx filter */
89203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
90203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
91203c4805SLuis R. Rodriguez 
92203c4805SLuis R. Rodriguez 	/* configure bssid mask */
9313b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
94203c4805SLuis R. Rodriguez 
95203c4805SLuis R. Rodriguez 	/* configure operational mode */
96203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
97203c4805SLuis R. Rodriguez 
98203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
99203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
100203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
101203c4805SLuis R. Rodriguez }
102203c4805SLuis R. Rodriguez 
103b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
104b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
105b5c80475SFelix Fietkau {
106b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
107b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
108b5c80475SFelix Fietkau 	struct sk_buff *skb;
109b5c80475SFelix Fietkau 	struct ath_buf *bf;
110b5c80475SFelix Fietkau 
111b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
112b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
113b5c80475SFelix Fietkau 		return false;
114b5c80475SFelix Fietkau 
115b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
116b5c80475SFelix Fietkau 	list_del_init(&bf->list);
117b5c80475SFelix Fietkau 
118b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
119b5c80475SFelix Fietkau 
120b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
121b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
122b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
123b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
124b5c80475SFelix Fietkau 
125b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
126b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
127b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
128b5c80475SFelix Fietkau 
129b5c80475SFelix Fietkau 	return true;
130b5c80475SFelix Fietkau }
131b5c80475SFelix Fietkau 
132b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
133b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
134b5c80475SFelix Fietkau {
135b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1366a01f0c0SMohammed Shafi Shajakhan 	struct ath_buf *bf, *tbf;
137b5c80475SFelix Fietkau 
138b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
139d2182b69SJoe Perches 		ath_dbg(common, QUEUE, "No free rx buf available\n");
140b5c80475SFelix Fietkau 		return;
141b5c80475SFelix Fietkau 	}
142b5c80475SFelix Fietkau 
1436a01f0c0SMohammed Shafi Shajakhan 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
144b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
145b5c80475SFelix Fietkau 			break;
146b5c80475SFelix Fietkau 
147b5c80475SFelix Fietkau }
148b5c80475SFelix Fietkau 
149b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
150b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
151b5c80475SFelix Fietkau {
152b5c80475SFelix Fietkau 	struct ath_buf *bf;
153b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
154b5c80475SFelix Fietkau 	struct sk_buff *skb;
155b5c80475SFelix Fietkau 
156b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
157b5c80475SFelix Fietkau 
158b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
159b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
160b5c80475SFelix Fietkau 		BUG_ON(!bf);
161b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
162b5c80475SFelix Fietkau 	}
163b5c80475SFelix Fietkau }
164b5c80475SFelix Fietkau 
165b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
166b5c80475SFelix Fietkau {
167ba542385SMohammed Shafi Shajakhan 	struct ath_hw *ah = sc->sc_ah;
168ba542385SMohammed Shafi Shajakhan 	struct ath_common *common = ath9k_hw_common(ah);
169b5c80475SFelix Fietkau 	struct ath_buf *bf;
170b5c80475SFelix Fietkau 
171b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
172b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
173b5c80475SFelix Fietkau 
174b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
175ba542385SMohammed Shafi Shajakhan 		if (bf->bf_mpdu) {
176ba542385SMohammed Shafi Shajakhan 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
177ba542385SMohammed Shafi Shajakhan 					common->rx_bufsize,
178ba542385SMohammed Shafi Shajakhan 					DMA_BIDIRECTIONAL);
179b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
180ba542385SMohammed Shafi Shajakhan 			bf->bf_buf_addr = 0;
181ba542385SMohammed Shafi Shajakhan 			bf->bf_mpdu = NULL;
182ba542385SMohammed Shafi Shajakhan 		}
183b5c80475SFelix Fietkau 	}
184b5c80475SFelix Fietkau }
185b5c80475SFelix Fietkau 
186b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
187b5c80475SFelix Fietkau {
188b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
189b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
190b5c80475SFelix Fietkau }
191b5c80475SFelix Fietkau 
192b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
193b5c80475SFelix Fietkau {
194b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
195b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
196b5c80475SFelix Fietkau 	struct sk_buff *skb;
197b5c80475SFelix Fietkau 	struct ath_buf *bf;
198b5c80475SFelix Fietkau 	int error = 0, i;
199b5c80475SFelix Fietkau 	u32 size;
200b5c80475SFelix Fietkau 
201b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
202b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
203b5c80475SFelix Fietkau 
204b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
205b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
206b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
207b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
208b5c80475SFelix Fietkau 
209b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
210b81950b1SFelix Fietkau 	bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
211b5c80475SFelix Fietkau 	if (!bf)
212b5c80475SFelix Fietkau 		return -ENOMEM;
213b5c80475SFelix Fietkau 
214b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
215b5c80475SFelix Fietkau 
216b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
217b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
218b5c80475SFelix Fietkau 		if (!skb) {
219b5c80475SFelix Fietkau 			error = -ENOMEM;
220b5c80475SFelix Fietkau 			goto rx_init_fail;
221b5c80475SFelix Fietkau 		}
222b5c80475SFelix Fietkau 
223b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
224b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
225b5c80475SFelix Fietkau 
226b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
227b5c80475SFelix Fietkau 						 common->rx_bufsize,
228b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
229b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
230b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
231b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
232b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2336cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2343800276aSJoe Perches 				ath_err(common,
235b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
236b5c80475SFelix Fietkau 				error = -ENOMEM;
237b5c80475SFelix Fietkau 				goto rx_init_fail;
238b5c80475SFelix Fietkau 		}
239b5c80475SFelix Fietkau 
240b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
241b5c80475SFelix Fietkau 	}
242b5c80475SFelix Fietkau 
243b5c80475SFelix Fietkau 	return 0;
244b5c80475SFelix Fietkau 
245b5c80475SFelix Fietkau rx_init_fail:
246b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
247b5c80475SFelix Fietkau 	return error;
248b5c80475SFelix Fietkau }
249b5c80475SFelix Fietkau 
250b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
251b5c80475SFelix Fietkau {
252b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
253b5c80475SFelix Fietkau 
254b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
255b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
256b5c80475SFelix Fietkau 
257b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
258b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
259b5c80475SFelix Fietkau 
260b5c80475SFelix Fietkau 	ath_opmode_init(sc);
261b5c80475SFelix Fietkau 
2624cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
263b5c80475SFelix Fietkau }
264b5c80475SFelix Fietkau 
265b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
266b5c80475SFelix Fietkau {
267b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
268b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
269b5c80475SFelix Fietkau }
270b5c80475SFelix Fietkau 
271203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
272203c4805SLuis R. Rodriguez {
27327c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
274203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
275203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
276203c4805SLuis R. Rodriguez 	int error = 0;
277203c4805SLuis R. Rodriguez 
2784bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
279203c4805SLuis R. Rodriguez 
2800d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
2810d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
2820d95521eSFelix Fietkau 
283b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
284b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
285b5c80475SFelix Fietkau 	} else {
286d2182b69SJoe Perches 		ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
287cc861f74SLuis R. Rodriguez 			common->cachelsz, common->rx_bufsize);
288203c4805SLuis R. Rodriguez 
289203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
290203c4805SLuis R. Rodriguez 
291203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
2924adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
293203c4805SLuis R. Rodriguez 		if (error != 0) {
2943800276aSJoe Perches 			ath_err(common,
295b5c80475SFelix Fietkau 				"failed to allocate rx descriptors: %d\n",
296b5c80475SFelix Fietkau 				error);
297203c4805SLuis R. Rodriguez 			goto err;
298203c4805SLuis R. Rodriguez 		}
299203c4805SLuis R. Rodriguez 
300203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
301b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
302b5c80475SFelix Fietkau 					      GFP_KERNEL);
303203c4805SLuis R. Rodriguez 			if (skb == NULL) {
304203c4805SLuis R. Rodriguez 				error = -ENOMEM;
305203c4805SLuis R. Rodriguez 				goto err;
306203c4805SLuis R. Rodriguez 			}
307203c4805SLuis R. Rodriguez 
308203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
309203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
310cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
311203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
312203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
313203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
314203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
315203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
3166cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3173800276aSJoe Perches 				ath_err(common,
318203c4805SLuis R. Rodriguez 					"dma_mapping_error() on RX init\n");
319203c4805SLuis R. Rodriguez 				error = -ENOMEM;
320203c4805SLuis R. Rodriguez 				goto err;
321203c4805SLuis R. Rodriguez 			}
322203c4805SLuis R. Rodriguez 		}
323203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
324b5c80475SFelix Fietkau 	}
325203c4805SLuis R. Rodriguez 
326203c4805SLuis R. Rodriguez err:
327203c4805SLuis R. Rodriguez 	if (error)
328203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
329203c4805SLuis R. Rodriguez 
330203c4805SLuis R. Rodriguez 	return error;
331203c4805SLuis R. Rodriguez }
332203c4805SLuis R. Rodriguez 
333203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
334203c4805SLuis R. Rodriguez {
335cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
336cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
337203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
338203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
339203c4805SLuis R. Rodriguez 
340b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
341b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
342b5c80475SFelix Fietkau 		return;
343b5c80475SFelix Fietkau 	} else {
344203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
346203c4805SLuis R. Rodriguez 			if (skb) {
347203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
348b5c80475SFelix Fietkau 						common->rx_bufsize,
349b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
350203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
3516cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3526cf9e995SBen Greear 				bf->bf_mpdu = NULL;
353203c4805SLuis R. Rodriguez 			}
354203c4805SLuis R. Rodriguez 		}
355203c4805SLuis R. Rodriguez 	}
356b5c80475SFelix Fietkau }
357203c4805SLuis R. Rodriguez 
358203c4805SLuis R. Rodriguez /*
359203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
360203c4805SLuis R. Rodriguez  * operating mode and state:
361203c4805SLuis R. Rodriguez  *
362203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
363203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
364203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
365203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
366203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
367203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
368203c4805SLuis R. Rodriguez  * o accept beacons:
369203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
370203c4805SLuis R. Rodriguez  *     node table entries for peers,
371203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
372203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
373203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
374203c4805SLuis R. Rodriguez  *   - when scanning
375203c4805SLuis R. Rodriguez  */
376203c4805SLuis R. Rodriguez 
377203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
378203c4805SLuis R. Rodriguez {
379203c4805SLuis R. Rodriguez 	u32 rfilt;
380203c4805SLuis R. Rodriguez 
381ac06697cSFelix Fietkau 	rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
382203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
383203c4805SLuis R. Rodriguez 
3849c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
385203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
386203c4805SLuis R. Rodriguez 
387203c4805SLuis R. Rodriguez 	/*
388203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
389203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
390203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
391203c4805SLuis R. Rodriguez 	 */
3922e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
393203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
394203c4805SLuis R. Rodriguez 
395203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
396203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
397203c4805SLuis R. Rodriguez 
398203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
399cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
400203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
401203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
402203c4805SLuis R. Rodriguez 	else
403203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
404203c4805SLuis R. Rodriguez 
405264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
40666afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
407203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
408203c4805SLuis R. Rodriguez 
4097ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4107ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4117ea310beSSujith 
4127545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
413a549459cSThomas Wagner 		/* This is needed for older chips */
414a549459cSThomas Wagner 		if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
4155eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
416203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
417203c4805SLuis R. Rodriguez 	}
418203c4805SLuis R. Rodriguez 
419b3d7aa43SGabor Juhos 	if (AR_SREV_9550(sc->sc_ah))
420b3d7aa43SGabor Juhos 		rfilt |= ATH9K_RX_FILTER_4ADDRESS;
421b3d7aa43SGabor Juhos 
422203c4805SLuis R. Rodriguez 	return rfilt;
423203c4805SLuis R. Rodriguez 
424203c4805SLuis R. Rodriguez }
425203c4805SLuis R. Rodriguez 
426203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
427203c4805SLuis R. Rodriguez {
428203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
429203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
430203c4805SLuis R. Rodriguez 
431b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
432b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
433b5c80475SFelix Fietkau 		return 0;
434b5c80475SFelix Fietkau 	}
435b5c80475SFelix Fietkau 
436203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
437203c4805SLuis R. Rodriguez 		goto start_recv;
438203c4805SLuis R. Rodriguez 
439203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
440203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
441203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
442203c4805SLuis R. Rodriguez 	}
443203c4805SLuis R. Rodriguez 
444203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
445203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
446203c4805SLuis R. Rodriguez 		goto start_recv;
447203c4805SLuis R. Rodriguez 
448203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
449203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
450203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
451203c4805SLuis R. Rodriguez 
452203c4805SLuis R. Rodriguez start_recv:
453203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
4544cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
455203c4805SLuis R. Rodriguez 
456203c4805SLuis R. Rodriguez 	return 0;
457203c4805SLuis R. Rodriguez }
458203c4805SLuis R. Rodriguez 
4594b883f02SFelix Fietkau static void ath_flushrecv(struct ath_softc *sc)
4604b883f02SFelix Fietkau {
4614b883f02SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
4624b883f02SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
4634b883f02SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
4644b883f02SFelix Fietkau }
4654b883f02SFelix Fietkau 
466203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
467203c4805SLuis R. Rodriguez {
468203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
4695882da02SFelix Fietkau 	bool stopped, reset = false;
470203c4805SLuis R. Rodriguez 
471d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
472203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
4735882da02SFelix Fietkau 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
474b5c80475SFelix Fietkau 
4754b883f02SFelix Fietkau 	ath_flushrecv(sc);
4764b883f02SFelix Fietkau 
477b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
478b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
479b5c80475SFelix Fietkau 	else
480203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
481203c4805SLuis R. Rodriguez 
482d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
483d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
484d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
485d7fd1b50SBen Greear 			"Could not stop RX, we could be "
48678a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
487d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
488d7fd1b50SBen Greear 	}
4892232d31bSFelix Fietkau 	return stopped && !reset;
490203c4805SLuis R. Rodriguez }
491203c4805SLuis R. Rodriguez 
492cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
493cc65965cSJouni Malinen {
494cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
495cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
496cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
497cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
498cc65965cSJouni Malinen 
499cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
500cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
501cc65965cSJouni Malinen 	end = skb->data + skb->len;
502cc65965cSJouni Malinen 
503cc65965cSJouni Malinen 	while (pos + 2 < end) {
504cc65965cSJouni Malinen 		id = *pos++;
505cc65965cSJouni Malinen 		elen = *pos++;
506cc65965cSJouni Malinen 		if (pos + elen > end)
507cc65965cSJouni Malinen 			break;
508cc65965cSJouni Malinen 
509cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
510cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
511cc65965cSJouni Malinen 				break;
512cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
513cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
514cc65965cSJouni Malinen 				break;
515cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
516cc65965cSJouni Malinen 		}
517cc65965cSJouni Malinen 
518cc65965cSJouni Malinen 		pos += elen;
519cc65965cSJouni Malinen 	}
520cc65965cSJouni Malinen 
521cc65965cSJouni Malinen 	return false;
522cc65965cSJouni Malinen }
523cc65965cSJouni Malinen 
524cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
525cc65965cSJouni Malinen {
5261510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
527cc65965cSJouni Malinen 
528cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
529cc65965cSJouni Malinen 		return;
530cc65965cSJouni Malinen 
5311b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
532293dc5dfSGabor Juhos 
5331b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5341b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
535d2182b69SJoe Perches 		ath_dbg(common, PS,
536226afe68SJoe Perches 			"Reconfigure Beacon timers based on timestamp from the AP\n");
537ef4ad633SSujith Manoharan 		ath9k_set_beacon(sc);
538ccdfeab6SJouni Malinen 	}
539ccdfeab6SJouni Malinen 
540cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
541cc65965cSJouni Malinen 		/*
542cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
54358f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
54458f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
54558f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
54658f5fffdSGabor Juhos 		 * so we are waiting for it as well.
547cc65965cSJouni Malinen 		 */
548d2182b69SJoe Perches 		ath_dbg(common, PS,
549226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
5501b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
551cc65965cSJouni Malinen 		return;
552cc65965cSJouni Malinen 	}
553cc65965cSJouni Malinen 
5541b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
555cc65965cSJouni Malinen 		/*
556cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
557cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
558cc65965cSJouni Malinen 		 * been delivered.
559cc65965cSJouni Malinen 		 */
5601b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
561d2182b69SJoe Perches 		ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
562cc65965cSJouni Malinen 	}
563cc65965cSJouni Malinen }
564cc65965cSJouni Malinen 
565f73c604cSRajkumar Manoharan static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
566cc65965cSJouni Malinen {
567cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
568c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
569cc65965cSJouni Malinen 
570cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
571cc65965cSJouni Malinen 
572cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
573ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
57407c15a3fSSujith Manoharan 	    && mybeacon) {
575cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
57607c15a3fSSujith Manoharan 	} else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
577cc65965cSJouni Malinen 		   (ieee80211_is_data(hdr->frame_control) ||
578cc65965cSJouni Malinen 		    ieee80211_is_action(hdr->frame_control)) &&
579cc65965cSJouni Malinen 		   is_multicast_ether_addr(hdr->addr1) &&
580cc65965cSJouni Malinen 		   !ieee80211_has_moredata(hdr->frame_control)) {
581cc65965cSJouni Malinen 		/*
582cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
583cc65965cSJouni Malinen 		 * point.
584cc65965cSJouni Malinen 		 */
5853fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
586d2182b69SJoe Perches 		ath_dbg(common, PS,
587c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
5881b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
5899a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
5909a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
5911b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
592d2182b69SJoe Perches 		ath_dbg(common, PS,
593226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
5941b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
5951b04b930SSujith 					PS_WAIT_FOR_CAB |
5961b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
5971b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
598cc65965cSJouni Malinen 	}
599cc65965cSJouni Malinen }
600cc65965cSJouni Malinen 
601b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
6023a2923e8SFelix Fietkau 				 enum ath9k_rx_qtype qtype,
6033a2923e8SFelix Fietkau 				 struct ath_rx_status *rs,
6043a2923e8SFelix Fietkau 				 struct ath_buf **dest)
605203c4805SLuis R. Rodriguez {
606b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
607203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
60827c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
609b5c80475SFelix Fietkau 	struct sk_buff *skb;
610b5c80475SFelix Fietkau 	struct ath_buf *bf;
611b5c80475SFelix Fietkau 	int ret;
612203c4805SLuis R. Rodriguez 
613b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
614b5c80475SFelix Fietkau 	if (!skb)
615b5c80475SFelix Fietkau 		return false;
616203c4805SLuis R. Rodriguez 
617b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
618b5c80475SFelix Fietkau 	BUG_ON(!bf);
619b5c80475SFelix Fietkau 
620ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
621b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
622b5c80475SFelix Fietkau 
6233a2923e8SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
624ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
625ce9426d1SMing Lei 		/*let device gain the buffer again*/
626ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
627ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
628b5c80475SFelix Fietkau 		return false;
629ce9426d1SMing Lei 	}
630b5c80475SFelix Fietkau 
631b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
632b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
633b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
634b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
635b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
636b5c80475SFelix Fietkau 
6373a2923e8SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
6383a2923e8SFelix Fietkau 		if (skb) {
639b5c80475SFelix Fietkau 			bf = SKB_CB_ATHBUF(skb);
640b5c80475SFelix Fietkau 			BUG_ON(!bf);
641b5c80475SFelix Fietkau 
642b5c80475SFelix Fietkau 			__skb_unlink(skb, &rx_edma->rx_fifo);
643b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
644b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
645b5c80475SFelix Fietkau 		}
6466bb51c70STom Hughes 
6476bb51c70STom Hughes 		bf = NULL;
6483a2923e8SFelix Fietkau 	}
649b5c80475SFelix Fietkau 
6503a2923e8SFelix Fietkau 	*dest = bf;
651b5c80475SFelix Fietkau 	return true;
652b5c80475SFelix Fietkau }
653b5c80475SFelix Fietkau 
654b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
655b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
656b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
657b5c80475SFelix Fietkau {
6583a2923e8SFelix Fietkau 	struct ath_buf *bf = NULL;
659b5c80475SFelix Fietkau 
6603a2923e8SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
6613a2923e8SFelix Fietkau 		if (!bf)
6623a2923e8SFelix Fietkau 			continue;
663b5c80475SFelix Fietkau 
664b5c80475SFelix Fietkau 		return bf;
665b5c80475SFelix Fietkau 	}
6663a2923e8SFelix Fietkau 	return NULL;
6673a2923e8SFelix Fietkau }
668b5c80475SFelix Fietkau 
669b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
670b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
671b5c80475SFelix Fietkau {
672b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
673b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
674b5c80475SFelix Fietkau 	struct ath_desc *ds;
675b5c80475SFelix Fietkau 	struct ath_buf *bf;
676b5c80475SFelix Fietkau 	int ret;
677203c4805SLuis R. Rodriguez 
678203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
679203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
680b5c80475SFelix Fietkau 		return NULL;
681203c4805SLuis R. Rodriguez 	}
682203c4805SLuis R. Rodriguez 
683203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
684203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
685203c4805SLuis R. Rodriguez 
686203c4805SLuis R. Rodriguez 	/*
687203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
688203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
689203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
690203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
691203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
692203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
693203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
694203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
695203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
696203c4805SLuis R. Rodriguez 	 */
6973de21116SRajkumar Manoharan 	ret = ath9k_hw_rxprocdesc(ah, ds, rs);
698b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
69929bffa96SFelix Fietkau 		struct ath_rx_status trs;
700203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
701203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
702203c4805SLuis R. Rodriguez 
70329bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
704203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
705203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
706b5c80475SFelix Fietkau 			return NULL;
707203c4805SLuis R. Rodriguez 		}
708203c4805SLuis R. Rodriguez 
709203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
710203c4805SLuis R. Rodriguez 
711203c4805SLuis R. Rodriguez 		/*
712203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
713203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
714203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
715203c4805SLuis R. Rodriguez 		 * set or not.
716203c4805SLuis R. Rodriguez 		 *
717203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
718203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
719203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
720203c4805SLuis R. Rodriguez 		 */
721203c4805SLuis R. Rodriguez 
722203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
7233de21116SRajkumar Manoharan 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
724b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
725b5c80475SFelix Fietkau 			return NULL;
726203c4805SLuis R. Rodriguez 	}
727203c4805SLuis R. Rodriguez 
728a3dc48e8SFelix Fietkau 	list_del(&bf->list);
729b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
730b5c80475SFelix Fietkau 		return bf;
731203c4805SLuis R. Rodriguez 
732203c4805SLuis R. Rodriguez 	/*
733203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
734203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
735203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
736203c4805SLuis R. Rodriguez 	 */
737ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
738cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
739203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
740203c4805SLuis R. Rodriguez 
741b5c80475SFelix Fietkau 	return bf;
742b5c80475SFelix Fietkau }
743b5c80475SFelix Fietkau 
744d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
745d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
7469f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
747d435700fSSujith 			    struct ieee80211_rx_status *rxs,
748d435700fSSujith 			    struct ath_rx_status *rx_stats,
749d435700fSSujith 			    bool *decrypt_error)
750d435700fSSujith {
751ec205999SFelix Fietkau 	struct ath_softc *sc = (struct ath_softc *) common->priv;
75266760eacSFelix Fietkau 	bool is_mc, is_valid_tkip, strip_mic, mic_error;
753d435700fSSujith 	struct ath_hw *ah = common->ah;
754d435700fSSujith 	__le16 fc;
755b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
756d435700fSSujith 
757d435700fSSujith 	fc = hdr->frame_control;
758d435700fSSujith 
75966760eacSFelix Fietkau 	is_mc = !!is_multicast_ether_addr(hdr->addr1);
76066760eacSFelix Fietkau 	is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
76166760eacSFelix Fietkau 		test_bit(rx_stats->rs_keyix, common->tkip_keymap);
762152e585dSBill Jordan 	strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
7632a5783b8SMichael Liang 		ieee80211_has_protected(fc) &&
764152e585dSBill Jordan 		!(rx_stats->rs_status &
765846d9363SFelix Fietkau 		(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
766846d9363SFelix Fietkau 		 ATH9K_RXERR_KEYMISS));
76766760eacSFelix Fietkau 
768f88373faSFelix Fietkau 	/*
769f88373faSFelix Fietkau 	 * Key miss events are only relevant for pairwise keys where the
770f88373faSFelix Fietkau 	 * descriptor does contain a valid key index. This has been observed
771f88373faSFelix Fietkau 	 * mostly with CCMP encryption.
772f88373faSFelix Fietkau 	 */
773bed3d9c0SFelix Fietkau 	if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
774bed3d9c0SFelix Fietkau 	    !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
775f88373faSFelix Fietkau 		rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
776f88373faSFelix Fietkau 
77715072189SBen Greear 	if (!rx_stats->rs_datalen) {
77815072189SBen Greear 		RX_STAT_INC(rx_len_err);
779d435700fSSujith 		return false;
78015072189SBen Greear 	}
78115072189SBen Greear 
782d435700fSSujith         /*
783d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
784d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
785d435700fSSujith          * those frames.
786d435700fSSujith          */
78715072189SBen Greear 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
78815072189SBen Greear 		RX_STAT_INC(rx_len_err);
789d435700fSSujith 		return false;
79015072189SBen Greear 	}
791d435700fSSujith 
7920d95521eSFelix Fietkau 	/* Only use error bits from the last fragment */
793d435700fSSujith 	if (rx_stats->rs_more)
7940d95521eSFelix Fietkau 		return true;
795d435700fSSujith 
79666760eacSFelix Fietkau 	mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
79766760eacSFelix Fietkau 		!ieee80211_has_morefrags(fc) &&
79866760eacSFelix Fietkau 		!(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
79966760eacSFelix Fietkau 		(rx_stats->rs_status & ATH9K_RXERR_MIC);
80066760eacSFelix Fietkau 
801d435700fSSujith 	/*
802d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
803d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
804d435700fSSujith 	 * rs_more will be false at the last element of the chained
805d435700fSSujith 	 * descriptors.
806d435700fSSujith 	 */
807d435700fSSujith 	if (rx_stats->rs_status != 0) {
808846d9363SFelix Fietkau 		u8 status_mask;
809846d9363SFelix Fietkau 
81066760eacSFelix Fietkau 		if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
811d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
81266760eacSFelix Fietkau 			mic_error = false;
81366760eacSFelix Fietkau 		}
814d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
815d435700fSSujith 			return false;
816d435700fSSujith 
817846d9363SFelix Fietkau 		if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
818846d9363SFelix Fietkau 		    (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
819d435700fSSujith 			*decrypt_error = true;
82066760eacSFelix Fietkau 			mic_error = false;
821d435700fSSujith 		}
82266760eacSFelix Fietkau 
823d435700fSSujith 		/*
824d435700fSSujith 		 * Reject error frames with the exception of
825d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
826d435700fSSujith 		 * we also ignore the CRC error.
827d435700fSSujith 		 */
828846d9363SFelix Fietkau 		status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
829846d9363SFelix Fietkau 			      ATH9K_RXERR_KEYMISS;
830846d9363SFelix Fietkau 
831ec205999SFelix Fietkau 		if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
832846d9363SFelix Fietkau 			status_mask |= ATH9K_RXERR_CRC;
833846d9363SFelix Fietkau 
834846d9363SFelix Fietkau 		if (rx_stats->rs_status & ~status_mask)
835d435700fSSujith 			return false;
836d435700fSSujith 	}
83766760eacSFelix Fietkau 
83866760eacSFelix Fietkau 	/*
83966760eacSFelix Fietkau 	 * For unicast frames the MIC error bit can have false positives,
84066760eacSFelix Fietkau 	 * so all MIC error reports need to be validated in software.
84166760eacSFelix Fietkau 	 * False negatives are not common, so skip software verification
84266760eacSFelix Fietkau 	 * if the hardware considers the MIC valid.
84366760eacSFelix Fietkau 	 */
84466760eacSFelix Fietkau 	if (strip_mic)
84566760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_STRIPPED;
84666760eacSFelix Fietkau 	else if (is_mc && mic_error)
84766760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_ERROR;
84866760eacSFelix Fietkau 
849d435700fSSujith 	return true;
850d435700fSSujith }
851d435700fSSujith 
852d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
853d435700fSSujith 			      struct ieee80211_hw *hw,
854d435700fSSujith 			      struct ath_rx_status *rx_stats,
8559f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
856d435700fSSujith {
857d435700fSSujith 	struct ieee80211_supported_band *sband;
858d435700fSSujith 	enum ieee80211_band band;
859d435700fSSujith 	unsigned int i = 0;
860990e08a0SBen Greear 	struct ath_softc __maybe_unused *sc = common->priv;
861d435700fSSujith 
862d435700fSSujith 	band = hw->conf.channel->band;
863d435700fSSujith 	sband = hw->wiphy->bands[band];
864d435700fSSujith 
865d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
866d435700fSSujith 		/* HT rate */
867d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
868d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
869d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
870d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
871d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
872d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
873d435700fSSujith 		return 0;
874d435700fSSujith 	}
875d435700fSSujith 
876d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
877d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
878d435700fSSujith 			rxs->rate_idx = i;
879d435700fSSujith 			return 0;
880d435700fSSujith 		}
881d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
882d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
883d435700fSSujith 			rxs->rate_idx = i;
884d435700fSSujith 			return 0;
885d435700fSSujith 		}
886d435700fSSujith 	}
887d435700fSSujith 
888d435700fSSujith 	/*
889d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
890d435700fSSujith 	 * because hardware has already validated this frame as OK.
891d435700fSSujith 	 */
892d2182b69SJoe Perches 	ath_dbg(common, ANY,
893226afe68SJoe Perches 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
894226afe68SJoe Perches 		rx_stats->rs_rate);
89515072189SBen Greear 	RX_STAT_INC(rx_rate_err);
896d435700fSSujith 	return -EINVAL;
897d435700fSSujith }
898d435700fSSujith 
899d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
900d435700fSSujith 			       struct ieee80211_hw *hw,
9019f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
902d435700fSSujith 			       struct ath_rx_status *rx_stats)
903d435700fSSujith {
9049ac58615SFelix Fietkau 	struct ath_softc *sc = hw->priv;
905d435700fSSujith 	struct ath_hw *ah = common->ah;
9069fa23e17SFelix Fietkau 	int last_rssi;
9072ef16755SFelix Fietkau 	int rssi = rx_stats->rs_rssi;
908d435700fSSujith 
909cf3af748SRajkumar Manoharan 	if (!rx_stats->is_mybeacon ||
910cf3af748SRajkumar Manoharan 	    ((ah->opmode != NL80211_IFTYPE_STATION) &&
911cf3af748SRajkumar Manoharan 	     (ah->opmode != NL80211_IFTYPE_ADHOC)))
9129fa23e17SFelix Fietkau 		return;
9139fa23e17SFelix Fietkau 
9149fa23e17SFelix Fietkau 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9159ac58615SFelix Fietkau 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
916686b9cb9SBen Greear 
9179ac58615SFelix Fietkau 	last_rssi = sc->last_rssi;
918d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
9192ef16755SFelix Fietkau 		rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
9202ef16755SFelix Fietkau 	if (rssi < 0)
9212ef16755SFelix Fietkau 		rssi = 0;
922d435700fSSujith 
923d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
9242ef16755SFelix Fietkau 	ah->stats.avgbrssi = rssi;
925d435700fSSujith }
926d435700fSSujith 
927d435700fSSujith /*
928d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
929d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
930d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
931d435700fSSujith  */
932d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
933d435700fSSujith 				   struct ieee80211_hw *hw,
9349f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
935d435700fSSujith 				   struct ath_rx_status *rx_stats,
936d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
937d435700fSSujith 				   bool *decrypt_error)
938d435700fSSujith {
939f749b946SFelix Fietkau 	struct ath_hw *ah = common->ah;
940f749b946SFelix Fietkau 
941d435700fSSujith 	/*
942d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
943d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
944d435700fSSujith 	 */
9459f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
946d435700fSSujith 		return -EINVAL;
947d435700fSSujith 
9480d95521eSFelix Fietkau 	/* Only use status info from the last fragment */
9490d95521eSFelix Fietkau 	if (rx_stats->rs_more)
9500d95521eSFelix Fietkau 		return 0;
9510d95521eSFelix Fietkau 
9529f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
953d435700fSSujith 
9549f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
955d435700fSSujith 		return -EINVAL;
956d435700fSSujith 
957d435700fSSujith 	rx_status->band = hw->conf.channel->band;
958d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
959f749b946SFelix Fietkau 	rx_status->signal = ah->noise + rx_stats->rs_rssi;
960d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
96196d21371SThomas Pedersen 	rx_status->flag |= RX_FLAG_MACTIME_END;
9622ef16755SFelix Fietkau 	if (rx_stats->rs_moreaggr)
9632ef16755SFelix Fietkau 		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
964d435700fSSujith 
965d435700fSSujith 	return 0;
966d435700fSSujith }
967d435700fSSujith 
968d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
969d435700fSSujith 				     struct sk_buff *skb,
970d435700fSSujith 				     struct ath_rx_status *rx_stats,
971d435700fSSujith 				     struct ieee80211_rx_status *rxs,
972d435700fSSujith 				     bool decrypt_error)
973d435700fSSujith {
974d435700fSSujith 	struct ath_hw *ah = common->ah;
975d435700fSSujith 	struct ieee80211_hdr *hdr;
976d435700fSSujith 	int hdrlen, padpos, padsize;
977d435700fSSujith 	u8 keyix;
978d435700fSSujith 	__le16 fc;
979d435700fSSujith 
980d435700fSSujith 	/* see if any padding is done by the hw and remove it */
981d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
982d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
983d435700fSSujith 	fc = hdr->frame_control;
984d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
985d435700fSSujith 
986d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
987d435700fSSujith 	 * packet payload is non-zero. The general calculation for
988d435700fSSujith 	 * padsize would take into account odd header lengths:
989d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
990d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
991d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
992d435700fSSujith 	 * not try to remove padding from short control frames that do
993d435700fSSujith 	 * not have payload. */
994d435700fSSujith 	padsize = padpos & 3;
995d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
996d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
997d435700fSSujith 		skb_pull(skb, padsize);
998d435700fSSujith 	}
999d435700fSSujith 
1000d435700fSSujith 	keyix = rx_stats->rs_keyix;
1001d435700fSSujith 
1002d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1003d435700fSSujith 	    ieee80211_has_protected(fc)) {
1004d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1005d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1006d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1007d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1008d435700fSSujith 
1009d435700fSSujith 		if (test_bit(keyix, common->keymap))
1010d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1011d435700fSSujith 	}
1012d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1013d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1014d435700fSSujith 	    ieee80211_is_mgmt(fc))
1015d435700fSSujith 		/* Use software decrypt for management frames. */
1016d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1017d435700fSSujith }
1018b5c80475SFelix Fietkau 
1019ab2e2fc8SSven Eckelmann #ifdef CONFIG_ATH9K_DEBUGFS
1020e93d083fSSimon Wunderlich static s8 fix_rssi_inv_only(u8 rssi_val)
1021e93d083fSSimon Wunderlich {
1022e93d083fSSimon Wunderlich 	if (rssi_val == 128)
1023e93d083fSSimon Wunderlich 		rssi_val = 0;
1024e93d083fSSimon Wunderlich 	return (s8) rssi_val;
1025e93d083fSSimon Wunderlich }
1026ab2e2fc8SSven Eckelmann #endif
1027e93d083fSSimon Wunderlich 
10289b99e665SSimon Wunderlich /* returns 1 if this was a spectral frame, even if not handled. */
10299b99e665SSimon Wunderlich static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
1030e93d083fSSimon Wunderlich 			   struct ath_rx_status *rs, u64 tsf)
1031e93d083fSSimon Wunderlich {
1032bd2ffe14SSven Eckelmann #ifdef CONFIG_ATH9K_DEBUGFS
1033e93d083fSSimon Wunderlich 	struct ath_hw *ah = sc->sc_ah;
1034e93d083fSSimon Wunderlich 	u8 bins[SPECTRAL_HT20_NUM_BINS];
1035e93d083fSSimon Wunderlich 	u8 *vdata = (u8 *)hdr;
1036e93d083fSSimon Wunderlich 	struct fft_sample_ht20 fft_sample;
1037e93d083fSSimon Wunderlich 	struct ath_radar_info *radar_info;
1038e93d083fSSimon Wunderlich 	struct ath_ht20_mag_info *mag_info;
1039e93d083fSSimon Wunderlich 	int len = rs->rs_datalen;
10404ab0b0aaSSven Eckelmann 	int dc_pos;
1041*12824374SSven Eckelmann 	u16 length, max_magnitude;
1042e93d083fSSimon Wunderlich 
1043e93d083fSSimon Wunderlich 	/* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
1044e93d083fSSimon Wunderlich 	 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
1045e93d083fSSimon Wunderlich 	 * yet, but this is supposed to be possible as well.
1046e93d083fSSimon Wunderlich 	 */
1047e93d083fSSimon Wunderlich 	if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
1048e93d083fSSimon Wunderlich 	    rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
1049e93d083fSSimon Wunderlich 	    rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
10509b99e665SSimon Wunderlich 		return 0;
10519b99e665SSimon Wunderlich 
10529b99e665SSimon Wunderlich 	/* check if spectral scan bit is set. This does not have to be checked
10539b99e665SSimon Wunderlich 	 * if received through a SPECTRAL phy error, but shouldn't hurt.
10549b99e665SSimon Wunderlich 	 */
10559b99e665SSimon Wunderlich 	radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
10569b99e665SSimon Wunderlich 	if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
10579b99e665SSimon Wunderlich 		return 0;
1058e93d083fSSimon Wunderlich 
1059e93d083fSSimon Wunderlich 	/* Variation in the data length is possible and will be fixed later.
1060e93d083fSSimon Wunderlich 	 * Note that we only support HT20 for now.
1061e93d083fSSimon Wunderlich 	 *
1062e93d083fSSimon Wunderlich 	 * TODO: add HT20_40 support as well.
1063e93d083fSSimon Wunderlich 	 */
1064e93d083fSSimon Wunderlich 	if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1065e93d083fSSimon Wunderlich 	    (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
10669b99e665SSimon Wunderlich 		return 1;
1067e93d083fSSimon Wunderlich 
1068e93d083fSSimon Wunderlich 	fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
1069*12824374SSven Eckelmann 	length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1070*12824374SSven Eckelmann 	fft_sample.tlv.length = __cpu_to_be16(length);
1071e93d083fSSimon Wunderlich 
10724ab0b0aaSSven Eckelmann 	fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
1073e93d083fSSimon Wunderlich 	fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1074e93d083fSSimon Wunderlich 	fft_sample.noise = ah->noise;
1075e93d083fSSimon Wunderlich 
1076e93d083fSSimon Wunderlich 	switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1077e93d083fSSimon Wunderlich 	case 0:
1078e93d083fSSimon Wunderlich 		/* length correct, nothing to do. */
1079e93d083fSSimon Wunderlich 		memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1080e93d083fSSimon Wunderlich 		break;
1081e93d083fSSimon Wunderlich 	case -1:
1082e93d083fSSimon Wunderlich 		/* first byte missing, duplicate it. */
1083e93d083fSSimon Wunderlich 		memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1084e93d083fSSimon Wunderlich 		bins[0] = vdata[0];
1085e93d083fSSimon Wunderlich 		break;
1086e93d083fSSimon Wunderlich 	case 2:
1087e93d083fSSimon Wunderlich 		/* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1088e93d083fSSimon Wunderlich 		memcpy(bins, vdata, 30);
1089e93d083fSSimon Wunderlich 		bins[30] = vdata[31];
1090e93d083fSSimon Wunderlich 		memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1091e93d083fSSimon Wunderlich 		break;
1092e93d083fSSimon Wunderlich 	case 1:
1093e93d083fSSimon Wunderlich 		/* MAC added 2 extra bytes AND first byte is missing. */
1094e93d083fSSimon Wunderlich 		bins[0] = vdata[0];
1095e93d083fSSimon Wunderlich 		memcpy(&bins[0], vdata, 30);
1096e93d083fSSimon Wunderlich 		bins[31] = vdata[31];
1097e93d083fSSimon Wunderlich 		memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1098e93d083fSSimon Wunderlich 		break;
1099e93d083fSSimon Wunderlich 	default:
11009b99e665SSimon Wunderlich 		return 1;
1101e93d083fSSimon Wunderlich 	}
1102e93d083fSSimon Wunderlich 
1103e93d083fSSimon Wunderlich 	/* DC value (value in the middle) is the blind spot of the spectral
1104e93d083fSSimon Wunderlich 	 * sample and invalid, interpolate it.
1105e93d083fSSimon Wunderlich 	 */
1106e93d083fSSimon Wunderlich 	dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1107e93d083fSSimon Wunderlich 	bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1108e93d083fSSimon Wunderlich 
1109e93d083fSSimon Wunderlich 	/* mag data is at the end of the frame, in front of radar_info */
1110e93d083fSSimon Wunderlich 	mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1111e93d083fSSimon Wunderlich 
11124ab0b0aaSSven Eckelmann 	/* copy raw bins without scaling them */
11134ab0b0aaSSven Eckelmann 	memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
11144ab0b0aaSSven Eckelmann 	fft_sample.max_exp = mag_info->max_exp & 0xf;
1115e93d083fSSimon Wunderlich 
1116*12824374SSven Eckelmann 	max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1117*12824374SSven Eckelmann 	fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
1118e93d083fSSimon Wunderlich 	fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1119e93d083fSSimon Wunderlich 	fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
11204ab0b0aaSSven Eckelmann 	fft_sample.tsf = __cpu_to_be64(tsf);
1121e93d083fSSimon Wunderlich 
1122e93d083fSSimon Wunderlich 	ath_debug_send_fft_sample(sc, &fft_sample.tlv);
11239b99e665SSimon Wunderlich 	return 1;
11249b99e665SSimon Wunderlich #else
11259b99e665SSimon Wunderlich 	return 0;
1126e93d083fSSimon Wunderlich #endif
1127e93d083fSSimon Wunderlich }
1128e93d083fSSimon Wunderlich 
112921fbbca3SChristian Lamparter static void ath9k_apply_ampdu_details(struct ath_softc *sc,
113021fbbca3SChristian Lamparter 	struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
113121fbbca3SChristian Lamparter {
113221fbbca3SChristian Lamparter 	if (rs->rs_isaggr) {
113321fbbca3SChristian Lamparter 		rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
113421fbbca3SChristian Lamparter 
113521fbbca3SChristian Lamparter 		rxs->ampdu_reference = sc->rx.ampdu_ref;
113621fbbca3SChristian Lamparter 
113721fbbca3SChristian Lamparter 		if (!rs->rs_moreaggr) {
113821fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
113921fbbca3SChristian Lamparter 			sc->rx.ampdu_ref++;
114021fbbca3SChristian Lamparter 		}
114121fbbca3SChristian Lamparter 
114221fbbca3SChristian Lamparter 		if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
114321fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
114421fbbca3SChristian Lamparter 	}
114521fbbca3SChristian Lamparter }
114621fbbca3SChristian Lamparter 
1147b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1148b5c80475SFelix Fietkau {
1149b5c80475SFelix Fietkau 	struct ath_buf *bf;
11500d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1151b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1152b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1153b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
11547545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1155b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1156b5c80475SFelix Fietkau 	int retval;
1157b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1158b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1159b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1160b5c80475SFelix Fietkau 	int dma_type;
11615c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1162a6d2055bSFelix Fietkau 	u64 tsf = 0;
1163a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
11648ab2cd09SLuis R. Rodriguez 	unsigned long flags;
1165b5c80475SFelix Fietkau 
1166b5c80475SFelix Fietkau 	if (edma)
1167b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
116856824223SMing Lei 	else
116956824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1170b5c80475SFelix Fietkau 
1171b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1172b5c80475SFelix Fietkau 
1173a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1174a6d2055bSFelix Fietkau 	tsf_lower = tsf & 0xffffffff;
1175a6d2055bSFelix Fietkau 
1176b5c80475SFelix Fietkau 	do {
1177e1352fdeSLorenzo Bianconi 		bool decrypt_error = false;
1178b5c80475SFelix Fietkau 
1179b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1180b5c80475SFelix Fietkau 		if (edma)
1181b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1182b5c80475SFelix Fietkau 		else
1183b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1184b5c80475SFelix Fietkau 
1185b5c80475SFelix Fietkau 		if (!bf)
1186b5c80475SFelix Fietkau 			break;
1187b5c80475SFelix Fietkau 
1188b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1189b5c80475SFelix Fietkau 		if (!skb)
1190b5c80475SFelix Fietkau 			continue;
1191b5c80475SFelix Fietkau 
11920d95521eSFelix Fietkau 		/*
11930d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
11940d95521eSFelix Fietkau 		 * the last one.
11950d95521eSFelix Fietkau 		 */
11960d95521eSFelix Fietkau 		if (sc->rx.frag)
11970d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
11980d95521eSFelix Fietkau 		else
11990d95521eSFelix Fietkau 			hdr_skb = skb;
12000d95521eSFelix Fietkau 
12010d95521eSFelix Fietkau 		hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
12020d95521eSFelix Fietkau 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
120315072189SBen Greear 		if (ieee80211_is_beacon(hdr->frame_control)) {
120415072189SBen Greear 			RX_STAT_INC(rx_beacons);
120515072189SBen Greear 			if (!is_zero_ether_addr(common->curbssid) &&
12062e42e474SJoe Perches 			    ether_addr_equal(hdr->addr3, common->curbssid))
1207cf3af748SRajkumar Manoharan 				rs.is_mybeacon = true;
1208cf3af748SRajkumar Manoharan 			else
1209cf3af748SRajkumar Manoharan 				rs.is_mybeacon = false;
121015072189SBen Greear 		}
121115072189SBen Greear 		else
121215072189SBen Greear 			rs.is_mybeacon = false;
12135ca42627SLuis R. Rodriguez 
1214be41b052SMohammed Shafi Shajakhan 		if (ieee80211_is_data_present(hdr->frame_control) &&
1215be41b052SMohammed Shafi Shajakhan 		    !ieee80211_is_qos_nullfunc(hdr->frame_control))
12166995fb80SRajkumar Manoharan 			sc->rx.num_pkts++;
1217be41b052SMohammed Shafi Shajakhan 
121829bffa96SFelix Fietkau 		ath_debug_stat_rx(sc, &rs);
12191395d3f0SSujith 
1220ffb1c56aSAshok Nagarajan 		memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1221ffb1c56aSAshok Nagarajan 
1222a6d2055bSFelix Fietkau 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1223a6d2055bSFelix Fietkau 		if (rs.rs_tstamp > tsf_lower &&
1224a6d2055bSFelix Fietkau 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1225a6d2055bSFelix Fietkau 			rxs->mactime -= 0x100000000ULL;
1226a6d2055bSFelix Fietkau 
1227a6d2055bSFelix Fietkau 		if (rs.rs_tstamp < tsf_lower &&
1228a6d2055bSFelix Fietkau 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1229a6d2055bSFelix Fietkau 			rxs->mactime += 0x100000000ULL;
1230a6d2055bSFelix Fietkau 
12319b99e665SSimon Wunderlich 		if (rs.rs_status & ATH9K_RXERR_PHY) {
12329b99e665SSimon Wunderlich 			if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
12339b99e665SSimon Wunderlich 				RX_STAT_INC(rx_spectral);
12349b99e665SSimon Wunderlich 				goto requeue_drop_frag;
12359b99e665SSimon Wunderlich 			}
12369b99e665SSimon Wunderlich 		}
1237e93d083fSSimon Wunderlich 
123883c76570SZefir Kurtisi 		retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
123983c76570SZefir Kurtisi 						 rxs, &decrypt_error);
124083c76570SZefir Kurtisi 		if (retval)
124183c76570SZefir Kurtisi 			goto requeue_drop_frag;
124283c76570SZefir Kurtisi 
124301e18918SRajkumar Manoharan 		if (rs.is_mybeacon) {
124401e18918SRajkumar Manoharan 			sc->hw_busy_count = 0;
124501e18918SRajkumar Manoharan 			ath_start_rx_poll(sc, 3);
124601e18918SRajkumar Manoharan 		}
1247203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1248203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1249cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1250203c4805SLuis R. Rodriguez 
1251203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1252203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1253203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1254203c4805SLuis R. Rodriguez 		 * processing. */
125515072189SBen Greear 		if (!requeue_skb) {
125615072189SBen Greear 			RX_STAT_INC(rx_oom_err);
12570d95521eSFelix Fietkau 			goto requeue_drop_frag;
125815072189SBen Greear 		}
1259203c4805SLuis R. Rodriguez 
1260203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1261203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1262cc861f74SLuis R. Rodriguez 				 common->rx_bufsize,
1263b5c80475SFelix Fietkau 				 dma_type);
1264203c4805SLuis R. Rodriguez 
1265b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1266b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1267b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1268203c4805SLuis R. Rodriguez 
12690d95521eSFelix Fietkau 		if (!rs.rs_more)
12700d95521eSFelix Fietkau 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1271c9b14170SLuis R. Rodriguez 						 rxs, decrypt_error);
1272203c4805SLuis R. Rodriguez 
1273203c4805SLuis R. Rodriguez 		/* We will now give hardware our shiny new allocated skb */
1274203c4805SLuis R. Rodriguez 		bf->bf_mpdu = requeue_skb;
1275203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1276cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
1277b5c80475SFelix Fietkau 						 dma_type);
1278203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
1279203c4805SLuis R. Rodriguez 			  bf->bf_buf_addr))) {
1280203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(requeue_skb);
1281203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
12826cf9e995SBen Greear 			bf->bf_buf_addr = 0;
12833800276aSJoe Perches 			ath_err(common, "dma_mapping_error() on RX\n");
12847545daf4SFelix Fietkau 			ieee80211_rx(hw, skb);
1285203c4805SLuis R. Rodriguez 			break;
1286203c4805SLuis R. Rodriguez 		}
1287203c4805SLuis R. Rodriguez 
12880d95521eSFelix Fietkau 		if (rs.rs_more) {
128915072189SBen Greear 			RX_STAT_INC(rx_frags);
12900d95521eSFelix Fietkau 			/*
12910d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
12920d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
12930d95521eSFelix Fietkau 			 * scatter-gather operation.
12940d95521eSFelix Fietkau 			 */
12950d95521eSFelix Fietkau 			if (sc->rx.frag) {
12960d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
12970d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
12980d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
129915072189SBen Greear 				RX_STAT_INC(rx_too_many_frags_err);
13000d95521eSFelix Fietkau 				skb = NULL;
13010d95521eSFelix Fietkau 			}
13020d95521eSFelix Fietkau 			sc->rx.frag = skb;
13030d95521eSFelix Fietkau 			goto requeue;
13040d95521eSFelix Fietkau 		}
13050d95521eSFelix Fietkau 
13060d95521eSFelix Fietkau 		if (sc->rx.frag) {
13070d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
13080d95521eSFelix Fietkau 
13090d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
13100d95521eSFelix Fietkau 				dev_kfree_skb(skb);
131115072189SBen Greear 				RX_STAT_INC(rx_oom_err);
13120d95521eSFelix Fietkau 				goto requeue_drop_frag;
13130d95521eSFelix Fietkau 			}
13140d95521eSFelix Fietkau 
1315b5447ff9SEric Dumazet 			sc->rx.frag = NULL;
1316b5447ff9SEric Dumazet 
13170d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
13180d95521eSFelix Fietkau 						  skb->len);
13190d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
13200d95521eSFelix Fietkau 			skb = hdr_skb;
13210d95521eSFelix Fietkau 		}
13220d95521eSFelix Fietkau 
1323eb840a80SMohammed Shafi Shajakhan 
1324eb840a80SMohammed Shafi Shajakhan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1325eb840a80SMohammed Shafi Shajakhan 
1326203c4805SLuis R. Rodriguez 			/*
1327eb840a80SMohammed Shafi Shajakhan 			 * change the default rx antenna if rx diversity
1328eb840a80SMohammed Shafi Shajakhan 			 * chooses the other antenna 3 times in a row.
1329203c4805SLuis R. Rodriguez 			 */
133029bffa96SFelix Fietkau 			if (sc->rx.defant != rs.rs_antenna) {
1331203c4805SLuis R. Rodriguez 				if (++sc->rx.rxotherant >= 3)
133229bffa96SFelix Fietkau 					ath_setdefantenna(sc, rs.rs_antenna);
1333203c4805SLuis R. Rodriguez 			} else {
1334203c4805SLuis R. Rodriguez 				sc->rx.rxotherant = 0;
1335203c4805SLuis R. Rodriguez 			}
1336203c4805SLuis R. Rodriguez 
1337eb840a80SMohammed Shafi Shajakhan 		}
1338eb840a80SMohammed Shafi Shajakhan 
133966760eacSFelix Fietkau 		if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
134066760eacSFelix Fietkau 			skb_trim(skb, skb->len - 8);
134166760eacSFelix Fietkau 
13428ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1343aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
13441b04b930SSujith 				     PS_WAIT_FOR_CAB |
1345aaef24b4SMohammed Shafi Shajakhan 				     PS_WAIT_FOR_PSPOLL_DATA)) ||
1346cedc7e3dSMohammed Shafi Shajakhan 		    ath9k_check_auto_sleep(sc))
1347f73c604cSRajkumar Manoharan 			ath_rx_ps(sc, skb, rs.is_mybeacon);
13488ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1349cc65965cSJouni Malinen 
135043c35284SFelix Fietkau 		if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
1351102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1352102885a5SVasanthakumar Thiagarajan 
135321fbbca3SChristian Lamparter 		ath9k_apply_ampdu_details(sc, &rs, rxs);
135421fbbca3SChristian Lamparter 
13557545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1356cc65965cSJouni Malinen 
13570d95521eSFelix Fietkau requeue_drop_frag:
13580d95521eSFelix Fietkau 		if (sc->rx.frag) {
13590d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
13600d95521eSFelix Fietkau 			sc->rx.frag = NULL;
13610d95521eSFelix Fietkau 		}
1362203c4805SLuis R. Rodriguez requeue:
1363b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
1364a3dc48e8SFelix Fietkau 		if (flush)
1365a3dc48e8SFelix Fietkau 			continue;
1366a3dc48e8SFelix Fietkau 
1367a3dc48e8SFelix Fietkau 		if (edma) {
1368b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1369b5c80475SFelix Fietkau 		} else {
1370203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
137195294973SFelix Fietkau 			ath9k_hw_rxena(ah);
1372b5c80475SFelix Fietkau 		}
1373203c4805SLuis R. Rodriguez 	} while (1);
1374203c4805SLuis R. Rodriguez 
137529ab0b36SRajkumar Manoharan 	if (!(ah->imask & ATH9K_INT_RXEOL)) {
137629ab0b36SRajkumar Manoharan 		ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
137772d874c6SFelix Fietkau 		ath9k_hw_set_interrupts(ah);
137829ab0b36SRajkumar Manoharan 	}
137929ab0b36SRajkumar Manoharan 
1380203c4805SLuis R. Rodriguez 	return 0;
1381203c4805SLuis R. Rodriguez }
1382