xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision c82552c5b0cb1735dbcbad78b1ffc6d3c212dc56)
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>
18203c4805SLuis R. Rodriguez #include "ath9k.h"
19b622a720SLuis R. Rodriguez #include "ar9003_mac.h"
20203c4805SLuis R. Rodriguez 
211a04d59dSFelix Fietkau #define SKB_CB_ATHBUF(__skb)	(*((struct ath_rxbuf **)__skb->cb))
22b5c80475SFelix Fietkau 
23ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
24ededf1f8SVasanthakumar Thiagarajan {
25ededf1f8SVasanthakumar Thiagarajan 	return sc->ps_enabled &&
26ededf1f8SVasanthakumar Thiagarajan 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
27ededf1f8SVasanthakumar Thiagarajan }
28ededf1f8SVasanthakumar Thiagarajan 
29203c4805SLuis R. Rodriguez /*
30203c4805SLuis R. Rodriguez  * Setup and link descriptors.
31203c4805SLuis R. Rodriguez  *
32203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
33203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
34203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
35203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
36203c4805SLuis R. Rodriguez  */
371a04d59dSFelix Fietkau static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf)
38203c4805SLuis R. Rodriguez {
39203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
40cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
41203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
42203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
43203c4805SLuis R. Rodriguez 
44203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
45203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
46203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
47203c4805SLuis R. Rodriguez 
48203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
49203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
509680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
51203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
52203c4805SLuis R. Rodriguez 
53cc861f74SLuis R. Rodriguez 	/*
54cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
55203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
56cc861f74SLuis R. Rodriguez 	 * to process
57cc861f74SLuis R. Rodriguez 	 */
58203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
59cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
60203c4805SLuis R. Rodriguez 			     0);
61203c4805SLuis R. Rodriguez 
62203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
63203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
64203c4805SLuis R. Rodriguez 	else
65203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
66203c4805SLuis R. Rodriguez 
67203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
68203c4805SLuis R. Rodriguez }
69203c4805SLuis R. Rodriguez 
701a04d59dSFelix Fietkau static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf)
71e96542e5SFelix Fietkau {
72e96542e5SFelix Fietkau 	if (sc->rx.buf_hold)
73e96542e5SFelix Fietkau 		ath_rx_buf_link(sc, sc->rx.buf_hold);
74e96542e5SFelix Fietkau 
75e96542e5SFelix Fietkau 	sc->rx.buf_hold = bf;
76e96542e5SFelix Fietkau }
77e96542e5SFelix Fietkau 
78203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
79203c4805SLuis R. Rodriguez {
80203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
81203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
82203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
83203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
84203c4805SLuis R. Rodriguez }
85203c4805SLuis R. Rodriguez 
86203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
87203c4805SLuis R. Rodriguez {
88203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
891510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
901510718dSLuis R. Rodriguez 
91203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
92203c4805SLuis R. Rodriguez 
93203c4805SLuis R. Rodriguez 	/* configure rx filter */
94203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
95203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
96203c4805SLuis R. Rodriguez 
97203c4805SLuis R. Rodriguez 	/* configure bssid mask */
9813b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
99203c4805SLuis R. Rodriguez 
100203c4805SLuis R. Rodriguez 	/* configure operational mode */
101203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
102203c4805SLuis R. Rodriguez 
103203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
104203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
105203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
106203c4805SLuis R. Rodriguez }
107203c4805SLuis R. Rodriguez 
108b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
109b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
110b5c80475SFelix Fietkau {
111b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
112b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
113b5c80475SFelix Fietkau 	struct sk_buff *skb;
1141a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
115b5c80475SFelix Fietkau 
116b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
117b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
118b5c80475SFelix Fietkau 		return false;
119b5c80475SFelix Fietkau 
1201a04d59dSFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
121b5c80475SFelix Fietkau 	list_del_init(&bf->list);
122b5c80475SFelix Fietkau 
123b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
124b5c80475SFelix Fietkau 
125b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
126b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
127b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
128b5c80475SFelix Fietkau 
129b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
130b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
13107236bf3SSujith Manoharan 	__skb_queue_tail(&rx_edma->rx_fifo, skb);
132b5c80475SFelix Fietkau 
133b5c80475SFelix Fietkau 	return true;
134b5c80475SFelix Fietkau }
135b5c80475SFelix Fietkau 
136b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
1377a897203SSujith Manoharan 				  enum ath9k_rx_qtype qtype)
138b5c80475SFelix Fietkau {
139b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1401a04d59dSFelix Fietkau 	struct ath_rxbuf *bf, *tbf;
141b5c80475SFelix Fietkau 
142b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
143d2182b69SJoe Perches 		ath_dbg(common, QUEUE, "No free rx buf available\n");
144b5c80475SFelix Fietkau 		return;
145b5c80475SFelix Fietkau 	}
146b5c80475SFelix Fietkau 
1476a01f0c0SMohammed Shafi Shajakhan 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
148b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
149b5c80475SFelix Fietkau 			break;
150b5c80475SFelix Fietkau 
151b5c80475SFelix Fietkau }
152b5c80475SFelix Fietkau 
153b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
154b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
155b5c80475SFelix Fietkau {
1561a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
157b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
158b5c80475SFelix Fietkau 	struct sk_buff *skb;
159b5c80475SFelix Fietkau 
160b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
161b5c80475SFelix Fietkau 
16207236bf3SSujith Manoharan 	while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
163b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
164b5c80475SFelix Fietkau 		BUG_ON(!bf);
165b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
166b5c80475SFelix Fietkau 	}
167b5c80475SFelix Fietkau }
168b5c80475SFelix Fietkau 
169b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
170b5c80475SFelix Fietkau {
171ba542385SMohammed Shafi Shajakhan 	struct ath_hw *ah = sc->sc_ah;
172ba542385SMohammed Shafi Shajakhan 	struct ath_common *common = ath9k_hw_common(ah);
1731a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
174b5c80475SFelix Fietkau 
175b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
176b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
177b5c80475SFelix Fietkau 
178b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
179ba542385SMohammed Shafi Shajakhan 		if (bf->bf_mpdu) {
180ba542385SMohammed Shafi Shajakhan 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
181ba542385SMohammed Shafi Shajakhan 					common->rx_bufsize,
182ba542385SMohammed Shafi Shajakhan 					DMA_BIDIRECTIONAL);
183b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
184ba542385SMohammed Shafi Shajakhan 			bf->bf_buf_addr = 0;
185ba542385SMohammed Shafi Shajakhan 			bf->bf_mpdu = NULL;
186ba542385SMohammed Shafi Shajakhan 		}
187b5c80475SFelix Fietkau 	}
188b5c80475SFelix Fietkau }
189b5c80475SFelix Fietkau 
190b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
191b5c80475SFelix Fietkau {
1925d07cca2SSujith Manoharan 	__skb_queue_head_init(&rx_edma->rx_fifo);
193b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
194b5c80475SFelix Fietkau }
195b5c80475SFelix Fietkau 
196b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
197b5c80475SFelix Fietkau {
198b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
199b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
200b5c80475SFelix Fietkau 	struct sk_buff *skb;
2011a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
202b5c80475SFelix Fietkau 	int error = 0, i;
203b5c80475SFelix Fietkau 	u32 size;
204b5c80475SFelix Fietkau 
205b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
206b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
207b5c80475SFelix Fietkau 
208b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
209b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
210b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
211b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
212b5c80475SFelix Fietkau 
2131a04d59dSFelix Fietkau 	size = sizeof(struct ath_rxbuf) * nbufs;
214b81950b1SFelix Fietkau 	bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
215b5c80475SFelix Fietkau 	if (!bf)
216b5c80475SFelix Fietkau 		return -ENOMEM;
217b5c80475SFelix Fietkau 
218b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
219b5c80475SFelix Fietkau 
220b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
221b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
222b5c80475SFelix Fietkau 		if (!skb) {
223b5c80475SFelix Fietkau 			error = -ENOMEM;
224b5c80475SFelix Fietkau 			goto rx_init_fail;
225b5c80475SFelix Fietkau 		}
226b5c80475SFelix Fietkau 
227b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
228b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
229b5c80475SFelix Fietkau 
230b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
231b5c80475SFelix Fietkau 						 common->rx_bufsize,
232b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
233b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
234b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
235b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
236b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2376cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2383800276aSJoe Perches 				ath_err(common,
239b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
240b5c80475SFelix Fietkau 				error = -ENOMEM;
241b5c80475SFelix Fietkau 				goto rx_init_fail;
242b5c80475SFelix Fietkau 		}
243b5c80475SFelix Fietkau 
244b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
245b5c80475SFelix Fietkau 	}
246b5c80475SFelix Fietkau 
247b5c80475SFelix Fietkau 	return 0;
248b5c80475SFelix Fietkau 
249b5c80475SFelix Fietkau rx_init_fail:
250b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
251b5c80475SFelix Fietkau 	return error;
252b5c80475SFelix Fietkau }
253b5c80475SFelix Fietkau 
254b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
255b5c80475SFelix Fietkau {
256b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
2577a897203SSujith Manoharan 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
2587a897203SSujith Manoharan 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
259b5c80475SFelix Fietkau 	ath_opmode_init(sc);
2604cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
261b5c80475SFelix Fietkau }
262b5c80475SFelix Fietkau 
263b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
264b5c80475SFelix Fietkau {
265b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
266b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
267b5c80475SFelix Fietkau }
268b5c80475SFelix Fietkau 
269203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
270203c4805SLuis R. Rodriguez {
27127c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
272203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
2731a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
274203c4805SLuis R. Rodriguez 	int error = 0;
275203c4805SLuis R. Rodriguez 
2764bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
277203c4805SLuis R. Rodriguez 
2780d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
2790d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
2800d95521eSFelix Fietkau 
281e87f3d53SSujith Manoharan 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
282b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
283e87f3d53SSujith Manoharan 
284d2182b69SJoe Perches 	ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
285cc861f74SLuis R. Rodriguez 		common->cachelsz, common->rx_bufsize);
286203c4805SLuis R. Rodriguez 
287203c4805SLuis R. Rodriguez 	/* Initialize rx descriptors */
288203c4805SLuis R. Rodriguez 
289203c4805SLuis R. Rodriguez 	error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
2904adfcdedSVasanthakumar Thiagarajan 				  "rx", nbufs, 1, 0);
291203c4805SLuis R. Rodriguez 	if (error != 0) {
2923800276aSJoe Perches 		ath_err(common,
293b5c80475SFelix Fietkau 			"failed to allocate rx descriptors: %d\n",
294b5c80475SFelix Fietkau 			error);
295203c4805SLuis R. Rodriguez 		goto err;
296203c4805SLuis R. Rodriguez 	}
297203c4805SLuis R. Rodriguez 
298203c4805SLuis R. Rodriguez 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
299b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize,
300b5c80475SFelix Fietkau 				      GFP_KERNEL);
301203c4805SLuis R. Rodriguez 		if (skb == NULL) {
302203c4805SLuis R. Rodriguez 			error = -ENOMEM;
303203c4805SLuis R. Rodriguez 			goto err;
304203c4805SLuis R. Rodriguez 		}
305203c4805SLuis R. Rodriguez 
306203c4805SLuis R. Rodriguez 		bf->bf_mpdu = skb;
307203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
308cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
309203c4805SLuis R. Rodriguez 						 DMA_FROM_DEVICE);
310203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
311203c4805SLuis R. Rodriguez 					       bf->bf_buf_addr))) {
312203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(skb);
313203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
3146cf9e995SBen Greear 			bf->bf_buf_addr = 0;
3153800276aSJoe Perches 			ath_err(common,
316203c4805SLuis R. Rodriguez 				"dma_mapping_error() on RX init\n");
317203c4805SLuis R. Rodriguez 			error = -ENOMEM;
318203c4805SLuis R. Rodriguez 			goto err;
319203c4805SLuis R. Rodriguez 		}
320203c4805SLuis R. Rodriguez 	}
321203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
322203c4805SLuis R. Rodriguez err:
323203c4805SLuis R. Rodriguez 	if (error)
324203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
325203c4805SLuis R. Rodriguez 
326203c4805SLuis R. Rodriguez 	return error;
327203c4805SLuis R. Rodriguez }
328203c4805SLuis R. Rodriguez 
329203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
330203c4805SLuis R. Rodriguez {
331cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
332cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
333203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
3341a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
335203c4805SLuis R. Rodriguez 
336b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
337b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
338b5c80475SFelix Fietkau 		return;
339e87f3d53SSujith Manoharan 	}
340e87f3d53SSujith Manoharan 
341203c4805SLuis R. Rodriguez 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
342203c4805SLuis R. Rodriguez 		skb = bf->bf_mpdu;
343203c4805SLuis R. Rodriguez 		if (skb) {
344203c4805SLuis R. Rodriguez 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
345b5c80475SFelix Fietkau 					 common->rx_bufsize,
346b5c80475SFelix Fietkau 					 DMA_FROM_DEVICE);
347203c4805SLuis R. Rodriguez 			dev_kfree_skb(skb);
3486cf9e995SBen Greear 			bf->bf_buf_addr = 0;
3496cf9e995SBen Greear 			bf->bf_mpdu = NULL;
350203c4805SLuis R. Rodriguez 		}
351203c4805SLuis R. Rodriguez 	}
352203c4805SLuis R. Rodriguez }
353203c4805SLuis R. Rodriguez 
354203c4805SLuis R. Rodriguez /*
355203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
356203c4805SLuis R. Rodriguez  * operating mode and state:
357203c4805SLuis R. Rodriguez  *
358203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
359203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
360203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
361203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
362203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
363203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
364203c4805SLuis R. Rodriguez  * o accept beacons:
365203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
366203c4805SLuis R. Rodriguez  *     node table entries for peers,
367203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
368203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
369203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
370203c4805SLuis R. Rodriguez  *   - when scanning
371203c4805SLuis R. Rodriguez  */
372203c4805SLuis R. Rodriguez 
373203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
374203c4805SLuis R. Rodriguez {
375203c4805SLuis R. Rodriguez 	u32 rfilt;
376203c4805SLuis R. Rodriguez 
37789f927afSLuis R. Rodriguez 	if (config_enabled(CONFIG_ATH9K_TX99))
37889f927afSLuis R. Rodriguez 		return 0;
37989f927afSLuis R. Rodriguez 
380ac06697cSFelix Fietkau 	rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
381203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
382203c4805SLuis R. Rodriguez 
38373e4937dSZefir Kurtisi 	/* if operating on a DFS channel, enable radar pulse detection */
38473e4937dSZefir Kurtisi 	if (sc->hw->conf.radar_enabled)
38573e4937dSZefir Kurtisi 		rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
38673e4937dSZefir Kurtisi 
3879c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
388203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
389203c4805SLuis R. Rodriguez 
390203c4805SLuis R. Rodriguez 	/*
391203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
392203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
393203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
394203c4805SLuis R. Rodriguez 	 */
3952e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
396203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
397203c4805SLuis R. Rodriguez 
398203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
399203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
400203c4805SLuis R. Rodriguez 
401203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
402cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
403203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
404203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
405203c4805SLuis R. Rodriguez 	else
406203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
407203c4805SLuis R. Rodriguez 
408264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
40966afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
410203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
411203c4805SLuis R. Rodriguez 
4127ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4137ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4147ea310beSSujith 
4157545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
416a549459cSThomas Wagner 		/* This is needed for older chips */
417a549459cSThomas Wagner 		if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
4185eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
419203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
420203c4805SLuis R. Rodriguez 	}
421203c4805SLuis R. Rodriguez 
4222c323058SSujith Manoharan 	if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah))
423b3d7aa43SGabor Juhos 		rfilt |= ATH9K_RX_FILTER_4ADDRESS;
424b3d7aa43SGabor Juhos 
425203c4805SLuis R. Rodriguez 	return rfilt;
426203c4805SLuis R. Rodriguez 
427203c4805SLuis R. Rodriguez }
428203c4805SLuis R. Rodriguez 
429203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
430203c4805SLuis R. Rodriguez {
431203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
4321a04d59dSFelix Fietkau 	struct ath_rxbuf *bf, *tbf;
433203c4805SLuis R. Rodriguez 
434b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
435b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
436b5c80475SFelix Fietkau 		return 0;
437b5c80475SFelix Fietkau 	}
438b5c80475SFelix Fietkau 
439203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
440203c4805SLuis R. Rodriguez 		goto start_recv;
441203c4805SLuis R. Rodriguez 
442e96542e5SFelix Fietkau 	sc->rx.buf_hold = NULL;
443203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
444203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
445203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
446203c4805SLuis R. Rodriguez 	}
447203c4805SLuis R. Rodriguez 
448203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
449203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
450203c4805SLuis R. Rodriguez 		goto start_recv;
451203c4805SLuis R. Rodriguez 
4521a04d59dSFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
453203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
454203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
455203c4805SLuis R. Rodriguez 
456203c4805SLuis R. Rodriguez start_recv:
457203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
4584cb54fa3SSujith Manoharan 	ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
459203c4805SLuis R. Rodriguez 
460203c4805SLuis R. Rodriguez 	return 0;
461203c4805SLuis R. Rodriguez }
462203c4805SLuis R. Rodriguez 
4634b883f02SFelix Fietkau static void ath_flushrecv(struct ath_softc *sc)
4644b883f02SFelix Fietkau {
4654b883f02SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
4664b883f02SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
4674b883f02SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
4684b883f02SFelix Fietkau }
4694b883f02SFelix Fietkau 
470203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
471203c4805SLuis R. Rodriguez {
472203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
4735882da02SFelix Fietkau 	bool stopped, reset = false;
474203c4805SLuis R. Rodriguez 
475d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
476203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
4775882da02SFelix Fietkau 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
478b5c80475SFelix Fietkau 
4794b883f02SFelix Fietkau 	ath_flushrecv(sc);
4804b883f02SFelix Fietkau 
481b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
482b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
483b5c80475SFelix Fietkau 	else
484203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
485203c4805SLuis R. Rodriguez 
486d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
487d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
488d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
489d7fd1b50SBen Greear 			"Could not stop RX, we could be "
49078a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
491d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
492d7fd1b50SBen Greear 	}
4932232d31bSFelix Fietkau 	return stopped && !reset;
494203c4805SLuis R. Rodriguez }
495203c4805SLuis R. Rodriguez 
496cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
497cc65965cSJouni Malinen {
498cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
499cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
500cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
501cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
502cc65965cSJouni Malinen 
503cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
504cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
505cc65965cSJouni Malinen 	end = skb->data + skb->len;
506cc65965cSJouni Malinen 
507cc65965cSJouni Malinen 	while (pos + 2 < end) {
508cc65965cSJouni Malinen 		id = *pos++;
509cc65965cSJouni Malinen 		elen = *pos++;
510cc65965cSJouni Malinen 		if (pos + elen > end)
511cc65965cSJouni Malinen 			break;
512cc65965cSJouni Malinen 
513cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
514cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
515cc65965cSJouni Malinen 				break;
516cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
517cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
518cc65965cSJouni Malinen 				break;
519cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
520cc65965cSJouni Malinen 		}
521cc65965cSJouni Malinen 
522cc65965cSJouni Malinen 		pos += elen;
523cc65965cSJouni Malinen 	}
524cc65965cSJouni Malinen 
525cc65965cSJouni Malinen 	return false;
526cc65965cSJouni Malinen }
527cc65965cSJouni Malinen 
528cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
529cc65965cSJouni Malinen {
5301510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
531cc65965cSJouni Malinen 
532cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
533cc65965cSJouni Malinen 		return;
534cc65965cSJouni Malinen 
5351b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
536293dc5dfSGabor Juhos 
5371b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5381b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
539d2182b69SJoe Perches 		ath_dbg(common, PS,
5401a6404a1SSujith Manoharan 			"Reconfigure beacon timers based on synchronized timestamp\n");
541ef4ad633SSujith Manoharan 		ath9k_set_beacon(sc);
542ccdfeab6SJouni Malinen 	}
543ccdfeab6SJouni Malinen 
544cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
545cc65965cSJouni Malinen 		/*
546cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
54758f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
54858f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
54958f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
55058f5fffdSGabor Juhos 		 * so we are waiting for it as well.
551cc65965cSJouni Malinen 		 */
552d2182b69SJoe Perches 		ath_dbg(common, PS,
553226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
5541b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
555cc65965cSJouni Malinen 		return;
556cc65965cSJouni Malinen 	}
557cc65965cSJouni Malinen 
5581b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
559cc65965cSJouni Malinen 		/*
560cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
561cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
562cc65965cSJouni Malinen 		 * been delivered.
563cc65965cSJouni Malinen 		 */
5641b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
565d2182b69SJoe Perches 		ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
566cc65965cSJouni Malinen 	}
567cc65965cSJouni Malinen }
568cc65965cSJouni Malinen 
569f73c604cSRajkumar Manoharan static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
570cc65965cSJouni Malinen {
571cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
572c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
573cc65965cSJouni Malinen 
574cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
575cc65965cSJouni Malinen 
576cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
577ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
57807c15a3fSSujith Manoharan 	    && mybeacon) {
579cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
58007c15a3fSSujith Manoharan 	} else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
581cc65965cSJouni Malinen 		   (ieee80211_is_data(hdr->frame_control) ||
582cc65965cSJouni Malinen 		    ieee80211_is_action(hdr->frame_control)) &&
583cc65965cSJouni Malinen 		   is_multicast_ether_addr(hdr->addr1) &&
584cc65965cSJouni Malinen 		   !ieee80211_has_moredata(hdr->frame_control)) {
585cc65965cSJouni Malinen 		/*
586cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
587cc65965cSJouni Malinen 		 * point.
588cc65965cSJouni Malinen 		 */
5893fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
590d2182b69SJoe Perches 		ath_dbg(common, PS,
591c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
5921b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
5939a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
5949a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
5951b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
596d2182b69SJoe Perches 		ath_dbg(common, PS,
597226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
5981b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
5991b04b930SSujith 					PS_WAIT_FOR_CAB |
6001b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6011b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
602cc65965cSJouni Malinen 	}
603cc65965cSJouni Malinen }
604cc65965cSJouni Malinen 
605b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
6063a2923e8SFelix Fietkau 				 enum ath9k_rx_qtype qtype,
6073a2923e8SFelix Fietkau 				 struct ath_rx_status *rs,
6081a04d59dSFelix Fietkau 				 struct ath_rxbuf **dest)
609203c4805SLuis R. Rodriguez {
610b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
611203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
61227c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
613b5c80475SFelix Fietkau 	struct sk_buff *skb;
6141a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
615b5c80475SFelix Fietkau 	int ret;
616203c4805SLuis R. Rodriguez 
617b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
618b5c80475SFelix Fietkau 	if (!skb)
619b5c80475SFelix Fietkau 		return false;
620203c4805SLuis R. Rodriguez 
621b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
622b5c80475SFelix Fietkau 	BUG_ON(!bf);
623b5c80475SFelix Fietkau 
624ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
625b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
626b5c80475SFelix Fietkau 
6273a2923e8SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
628ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
629ce9426d1SMing Lei 		/*let device gain the buffer again*/
630ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
631ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
632b5c80475SFelix Fietkau 		return false;
633ce9426d1SMing Lei 	}
634b5c80475SFelix Fietkau 
635b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
636b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
637b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
638b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
639b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
640b5c80475SFelix Fietkau 
6413a2923e8SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
6423a2923e8SFelix Fietkau 		if (skb) {
643b5c80475SFelix Fietkau 			bf = SKB_CB_ATHBUF(skb);
644b5c80475SFelix Fietkau 			BUG_ON(!bf);
645b5c80475SFelix Fietkau 
646b5c80475SFelix Fietkau 			__skb_unlink(skb, &rx_edma->rx_fifo);
647b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
648b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
649b5c80475SFelix Fietkau 		}
6506bb51c70STom Hughes 
6516bb51c70STom Hughes 		bf = NULL;
6523a2923e8SFelix Fietkau 	}
653b5c80475SFelix Fietkau 
6543a2923e8SFelix Fietkau 	*dest = bf;
655b5c80475SFelix Fietkau 	return true;
656b5c80475SFelix Fietkau }
657b5c80475SFelix Fietkau 
6581a04d59dSFelix Fietkau static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
659b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
660b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
661b5c80475SFelix Fietkau {
6621a04d59dSFelix Fietkau 	struct ath_rxbuf *bf = NULL;
663b5c80475SFelix Fietkau 
6643a2923e8SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
6653a2923e8SFelix Fietkau 		if (!bf)
6663a2923e8SFelix Fietkau 			continue;
667b5c80475SFelix Fietkau 
668b5c80475SFelix Fietkau 		return bf;
669b5c80475SFelix Fietkau 	}
6703a2923e8SFelix Fietkau 	return NULL;
6713a2923e8SFelix Fietkau }
672b5c80475SFelix Fietkau 
6731a04d59dSFelix Fietkau static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
674b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
675b5c80475SFelix Fietkau {
676b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
677b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
678b5c80475SFelix Fietkau 	struct ath_desc *ds;
6791a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
680b5c80475SFelix Fietkau 	int ret;
681203c4805SLuis R. Rodriguez 
682203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
683203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
684b5c80475SFelix Fietkau 		return NULL;
685203c4805SLuis R. Rodriguez 	}
686203c4805SLuis R. Rodriguez 
6871a04d59dSFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
688e96542e5SFelix Fietkau 	if (bf == sc->rx.buf_hold)
689e96542e5SFelix Fietkau 		return NULL;
690e96542e5SFelix Fietkau 
691203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
692203c4805SLuis R. Rodriguez 
693203c4805SLuis R. Rodriguez 	/*
694203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
695203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
696203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
697203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
698203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
699203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
700203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
701203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
702203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
703203c4805SLuis R. Rodriguez 	 */
7043de21116SRajkumar Manoharan 	ret = ath9k_hw_rxprocdesc(ah, ds, rs);
705b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
70629bffa96SFelix Fietkau 		struct ath_rx_status trs;
7071a04d59dSFelix Fietkau 		struct ath_rxbuf *tbf;
708203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
709203c4805SLuis R. Rodriguez 
71029bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
711203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
712203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
713b5c80475SFelix Fietkau 			return NULL;
714203c4805SLuis R. Rodriguez 		}
715203c4805SLuis R. Rodriguez 
7161a04d59dSFelix Fietkau 		tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
717203c4805SLuis R. Rodriguez 
718203c4805SLuis R. Rodriguez 		/*
719203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
720203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
721203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
722203c4805SLuis R. Rodriguez 		 * set or not.
723203c4805SLuis R. Rodriguez 		 *
724203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
725203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
726203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
727203c4805SLuis R. Rodriguez 		 */
728203c4805SLuis R. Rodriguez 
729203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
7303de21116SRajkumar Manoharan 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
731b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
732b5c80475SFelix Fietkau 			return NULL;
733723e7113SFelix Fietkau 
734723e7113SFelix Fietkau 		/*
735b7b146c9SFelix Fietkau 		 * Re-check previous descriptor, in case it has been filled
736b7b146c9SFelix Fietkau 		 * in the mean time.
737b7b146c9SFelix Fietkau 		 */
738b7b146c9SFelix Fietkau 		ret = ath9k_hw_rxprocdesc(ah, ds, rs);
739b7b146c9SFelix Fietkau 		if (ret == -EINPROGRESS) {
740b7b146c9SFelix Fietkau 			/*
741723e7113SFelix Fietkau 			 * mark descriptor as zero-length and set the 'more'
742723e7113SFelix Fietkau 			 * flag to ensure that both buffers get discarded
743723e7113SFelix Fietkau 			 */
744723e7113SFelix Fietkau 			rs->rs_datalen = 0;
745723e7113SFelix Fietkau 			rs->rs_more = true;
746203c4805SLuis R. Rodriguez 		}
747b7b146c9SFelix Fietkau 	}
748203c4805SLuis R. Rodriguez 
749a3dc48e8SFelix Fietkau 	list_del(&bf->list);
750b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
751b5c80475SFelix Fietkau 		return bf;
752203c4805SLuis R. Rodriguez 
753203c4805SLuis R. Rodriguez 	/*
754203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
755203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
756203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
757203c4805SLuis R. Rodriguez 	 */
758ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
759cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
760203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
761203c4805SLuis R. Rodriguez 
762b5c80475SFelix Fietkau 	return bf;
763b5c80475SFelix Fietkau }
764b5c80475SFelix Fietkau 
765e0dd1a96SSujith Manoharan static void ath9k_process_tsf(struct ath_rx_status *rs,
766e0dd1a96SSujith Manoharan 			      struct ieee80211_rx_status *rxs,
767e0dd1a96SSujith Manoharan 			      u64 tsf)
768e0dd1a96SSujith Manoharan {
769e0dd1a96SSujith Manoharan 	u32 tsf_lower = tsf & 0xffffffff;
770e0dd1a96SSujith Manoharan 
771e0dd1a96SSujith Manoharan 	rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
772e0dd1a96SSujith Manoharan 	if (rs->rs_tstamp > tsf_lower &&
773e0dd1a96SSujith Manoharan 	    unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
774e0dd1a96SSujith Manoharan 		rxs->mactime -= 0x100000000ULL;
775e0dd1a96SSujith Manoharan 
776e0dd1a96SSujith Manoharan 	if (rs->rs_tstamp < tsf_lower &&
777e0dd1a96SSujith Manoharan 	    unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
778e0dd1a96SSujith Manoharan 		rxs->mactime += 0x100000000ULL;
779e0dd1a96SSujith Manoharan }
780e0dd1a96SSujith Manoharan 
781d435700fSSujith /*
782d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
783d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
784d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
785d435700fSSujith  */
786723e7113SFelix Fietkau static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
7876f38482eSSujith Manoharan 				   struct sk_buff *skb,
788d435700fSSujith 				   struct ath_rx_status *rx_stats,
789d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
790e0dd1a96SSujith Manoharan 				   bool *decrypt_error, u64 tsf)
791d435700fSSujith {
792723e7113SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
793723e7113SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
794723e7113SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
7956f38482eSSujith Manoharan 	struct ieee80211_hdr *hdr;
796723e7113SFelix Fietkau 	bool discard_current = sc->rx.discard_next;
797723e7113SFelix Fietkau 
7985871d2d7SSujith Manoharan 	/*
7995871d2d7SSujith Manoharan 	 * Discard corrupt descriptors which are marked in
8005871d2d7SSujith Manoharan 	 * ath_get_next_rx_buf().
8015871d2d7SSujith Manoharan 	 */
802723e7113SFelix Fietkau 	if (discard_current)
803b7b146c9SFelix Fietkau 		goto corrupt;
804b7b146c9SFelix Fietkau 
805b7b146c9SFelix Fietkau 	sc->rx.discard_next = false;
806f749b946SFelix Fietkau 
807d435700fSSujith 	/*
8085871d2d7SSujith Manoharan 	 * Discard zero-length packets.
8095871d2d7SSujith Manoharan 	 */
8105871d2d7SSujith Manoharan 	if (!rx_stats->rs_datalen) {
8115871d2d7SSujith Manoharan 		RX_STAT_INC(rx_len_err);
812b7b146c9SFelix Fietkau 		goto corrupt;
8135871d2d7SSujith Manoharan 	}
8145871d2d7SSujith Manoharan 
8155871d2d7SSujith Manoharan 	/*
8165871d2d7SSujith Manoharan 	 * rs_status follows rs_datalen so if rs_datalen is too large
8175871d2d7SSujith Manoharan 	 * we can take a hint that hardware corrupted it, so ignore
8185871d2d7SSujith Manoharan 	 * those frames.
8195871d2d7SSujith Manoharan 	 */
8205871d2d7SSujith Manoharan 	if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
8215871d2d7SSujith Manoharan 		RX_STAT_INC(rx_len_err);
822b7b146c9SFelix Fietkau 		goto corrupt;
8235871d2d7SSujith Manoharan 	}
8245871d2d7SSujith Manoharan 
8254a470647SSujith Manoharan 	/* Only use status info from the last fragment */
8264a470647SSujith Manoharan 	if (rx_stats->rs_more)
8274a470647SSujith Manoharan 		return 0;
8284a470647SSujith Manoharan 
829b0925595SSujith Manoharan 	/*
830b0925595SSujith Manoharan 	 * Return immediately if the RX descriptor has been marked
831b0925595SSujith Manoharan 	 * as corrupt based on the various error bits.
832b0925595SSujith Manoharan 	 *
833b0925595SSujith Manoharan 	 * This is different from the other corrupt descriptor
834b0925595SSujith Manoharan 	 * condition handled above.
835b0925595SSujith Manoharan 	 */
836b7b146c9SFelix Fietkau 	if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
837b7b146c9SFelix Fietkau 		goto corrupt;
838b0925595SSujith Manoharan 
8396f38482eSSujith Manoharan 	hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
8406f38482eSSujith Manoharan 
841e0dd1a96SSujith Manoharan 	ath9k_process_tsf(rx_stats, rx_status, tsf);
8425e85a32aSSujith Manoharan 	ath_debug_stat_rx(sc, rx_stats);
843e0dd1a96SSujith Manoharan 
8445871d2d7SSujith Manoharan 	/*
8456b87d71cSSujith Manoharan 	 * Process PHY errors and return so that the packet
8466b87d71cSSujith Manoharan 	 * can be dropped.
8476b87d71cSSujith Manoharan 	 */
8486b87d71cSSujith Manoharan 	if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
8496b87d71cSSujith Manoharan 		ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
8506b87d71cSSujith Manoharan 		if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
8516b87d71cSSujith Manoharan 			RX_STAT_INC(rx_spectral);
8526b87d71cSSujith Manoharan 
853b7b146c9SFelix Fietkau 		return -EINVAL;
8546b87d71cSSujith Manoharan 	}
8556b87d71cSSujith Manoharan 
8566b87d71cSSujith Manoharan 	/*
857d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
858d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
859d435700fSSujith 	 */
860f3b6a488SJohn W. Linville 	if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error, sc->rx.rxfilter))
861b7b146c9SFelix Fietkau 		return -EINVAL;
862d435700fSSujith 
8631cc47a5bSOleksij Rempel 	if (ath_is_mybeacon(common, hdr)) {
8641cc47a5bSOleksij Rempel 		RX_STAT_INC(rx_beacons);
8651cc47a5bSOleksij Rempel 		rx_stats->is_mybeacon = true;
8661cc47a5bSOleksij Rempel 	}
8676f38482eSSujith Manoharan 
868ff9a93f2SSujith Manoharan 	/*
869ff9a93f2SSujith Manoharan 	 * This shouldn't happen, but have a safety check anyway.
870ff9a93f2SSujith Manoharan 	 */
871b7b146c9SFelix Fietkau 	if (WARN_ON(!ah->curchan))
872b7b146c9SFelix Fietkau 		return -EINVAL;
873ff9a93f2SSujith Manoharan 
87412746036SOleksij Rempel 	if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
87512746036SOleksij Rempel 		/*
87612746036SOleksij Rempel 		 * No valid hardware bitrate found -- we should not get here
87712746036SOleksij Rempel 		 * because hardware has already validated this frame as OK.
87812746036SOleksij Rempel 		 */
87912746036SOleksij Rempel 		ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
88012746036SOleksij Rempel 			rx_stats->rs_rate);
88112746036SOleksij Rempel 		RX_STAT_INC(rx_rate_err);
882b7b146c9SFelix Fietkau 		return -EINVAL;
8837c5c73cdSSujith Manoharan 	}
884d435700fSSujith 
88532efb0ccSOleksij Rempel 	ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
88674a97755SSujith Manoharan 
887ff9a93f2SSujith Manoharan 	rx_status->band = ah->curchan->chan->band;
888ff9a93f2SSujith Manoharan 	rx_status->freq = ah->curchan->chan->center_freq;
889d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
89096d21371SThomas Pedersen 	rx_status->flag |= RX_FLAG_MACTIME_END;
891d435700fSSujith 
892a5525d9cSSujith Manoharan #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
893a5525d9cSSujith Manoharan 	if (ieee80211_is_data_present(hdr->frame_control) &&
894a5525d9cSSujith Manoharan 	    !ieee80211_is_qos_nullfunc(hdr->frame_control))
895a5525d9cSSujith Manoharan 		sc->rx.num_pkts++;
896a5525d9cSSujith Manoharan #endif
897a5525d9cSSujith Manoharan 
898b7b146c9SFelix Fietkau 	return 0;
899b7b146c9SFelix Fietkau 
900b7b146c9SFelix Fietkau corrupt:
901b7b146c9SFelix Fietkau 	sc->rx.discard_next = rx_stats->rs_more;
902b7b146c9SFelix Fietkau 	return -EINVAL;
903d435700fSSujith }
904d435700fSSujith 
905c3124df7SSujith Manoharan /*
906c3124df7SSujith Manoharan  * Run the LNA combining algorithm only in these cases:
907c3124df7SSujith Manoharan  *
908c3124df7SSujith Manoharan  * Standalone WLAN cards with both LNA/Antenna diversity
909c3124df7SSujith Manoharan  * enabled in the EEPROM.
910c3124df7SSujith Manoharan  *
911c3124df7SSujith Manoharan  * WLAN+BT cards which are in the supported card list
912c3124df7SSujith Manoharan  * in ath_pci_id_table and the user has loaded the
913c3124df7SSujith Manoharan  * driver with "bt_ant_diversity" set to true.
914c3124df7SSujith Manoharan  */
915c3124df7SSujith Manoharan static void ath9k_antenna_check(struct ath_softc *sc,
916c3124df7SSujith Manoharan 				struct ath_rx_status *rs)
917c3124df7SSujith Manoharan {
918c3124df7SSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
919c3124df7SSujith Manoharan 	struct ath9k_hw_capabilities *pCap = &ah->caps;
920c3124df7SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(ah);
921c3124df7SSujith Manoharan 
922c3124df7SSujith Manoharan 	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
923c3124df7SSujith Manoharan 		return;
924c3124df7SSujith Manoharan 
925c3124df7SSujith Manoharan 	/*
926c3124df7SSujith Manoharan 	 * Change the default rx antenna if rx diversity
927c3124df7SSujith Manoharan 	 * chooses the other antenna 3 times in a row.
928c3124df7SSujith Manoharan 	 */
929c3124df7SSujith Manoharan 	if (sc->rx.defant != rs->rs_antenna) {
930c3124df7SSujith Manoharan 		if (++sc->rx.rxotherant >= 3)
931c3124df7SSujith Manoharan 			ath_setdefantenna(sc, rs->rs_antenna);
932c3124df7SSujith Manoharan 	} else {
933c3124df7SSujith Manoharan 		sc->rx.rxotherant = 0;
934c3124df7SSujith Manoharan 	}
935c3124df7SSujith Manoharan 
936c3124df7SSujith Manoharan 	if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
937c3124df7SSujith Manoharan 		if (common->bt_ant_diversity)
938c3124df7SSujith Manoharan 			ath_ant_comb_scan(sc, rs);
939c3124df7SSujith Manoharan 	} else {
940c3124df7SSujith Manoharan 		ath_ant_comb_scan(sc, rs);
941c3124df7SSujith Manoharan 	}
942c3124df7SSujith Manoharan }
943c3124df7SSujith Manoharan 
94421fbbca3SChristian Lamparter static void ath9k_apply_ampdu_details(struct ath_softc *sc,
94521fbbca3SChristian Lamparter 	struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
94621fbbca3SChristian Lamparter {
94721fbbca3SChristian Lamparter 	if (rs->rs_isaggr) {
94821fbbca3SChristian Lamparter 		rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
94921fbbca3SChristian Lamparter 
95021fbbca3SChristian Lamparter 		rxs->ampdu_reference = sc->rx.ampdu_ref;
95121fbbca3SChristian Lamparter 
95221fbbca3SChristian Lamparter 		if (!rs->rs_moreaggr) {
95321fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
95421fbbca3SChristian Lamparter 			sc->rx.ampdu_ref++;
95521fbbca3SChristian Lamparter 		}
95621fbbca3SChristian Lamparter 
95721fbbca3SChristian Lamparter 		if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
95821fbbca3SChristian Lamparter 			rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
95921fbbca3SChristian Lamparter 	}
96021fbbca3SChristian Lamparter }
96121fbbca3SChristian Lamparter 
962b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
963b5c80475SFelix Fietkau {
9641a04d59dSFelix Fietkau 	struct ath_rxbuf *bf;
9650d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
966b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
967b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
968b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
9697545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
970b5c80475SFelix Fietkau 	int retval;
971b5c80475SFelix Fietkau 	struct ath_rx_status rs;
972b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
973b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
974b5c80475SFelix Fietkau 	int dma_type;
975a6d2055bSFelix Fietkau 	u64 tsf = 0;
9768ab2cd09SLuis R. Rodriguez 	unsigned long flags;
9772e1cd495SFelix Fietkau 	dma_addr_t new_buf_addr;
978*c82552c5STim Harvey 	unsigned int budget = 512;
979b5c80475SFelix Fietkau 
980b5c80475SFelix Fietkau 	if (edma)
981b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
98256824223SMing Lei 	else
98356824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
984b5c80475SFelix Fietkau 
985b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
986b5c80475SFelix Fietkau 
987a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
988a6d2055bSFelix Fietkau 
989b5c80475SFelix Fietkau 	do {
990e1352fdeSLorenzo Bianconi 		bool decrypt_error = false;
991b5c80475SFelix Fietkau 
992b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
993b5c80475SFelix Fietkau 		if (edma)
994b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
995b5c80475SFelix Fietkau 		else
996b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
997b5c80475SFelix Fietkau 
998b5c80475SFelix Fietkau 		if (!bf)
999b5c80475SFelix Fietkau 			break;
1000b5c80475SFelix Fietkau 
1001b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1002b5c80475SFelix Fietkau 		if (!skb)
1003b5c80475SFelix Fietkau 			continue;
1004b5c80475SFelix Fietkau 
10050d95521eSFelix Fietkau 		/*
10060d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
10070d95521eSFelix Fietkau 		 * the last one.
10080d95521eSFelix Fietkau 		 */
10090d95521eSFelix Fietkau 		if (sc->rx.frag)
10100d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
10110d95521eSFelix Fietkau 		else
10120d95521eSFelix Fietkau 			hdr_skb = skb;
10130d95521eSFelix Fietkau 
1014f6307ddaSSujith Manoharan 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
1015ffb1c56aSAshok Nagarajan 		memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1016ffb1c56aSAshok Nagarajan 
10176f38482eSSujith Manoharan 		retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
1018e0dd1a96SSujith Manoharan 						 &decrypt_error, tsf);
101983c76570SZefir Kurtisi 		if (retval)
102083c76570SZefir Kurtisi 			goto requeue_drop_frag;
102183c76570SZefir Kurtisi 
1022203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1023203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1024cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1025203c4805SLuis R. Rodriguez 
1026203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1027203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1028203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1029203c4805SLuis R. Rodriguez 		 * processing. */
103015072189SBen Greear 		if (!requeue_skb) {
103115072189SBen Greear 			RX_STAT_INC(rx_oom_err);
10320d95521eSFelix Fietkau 			goto requeue_drop_frag;
103315072189SBen Greear 		}
1034203c4805SLuis R. Rodriguez 
10352e1cd495SFelix Fietkau 		/* We will now give hardware our shiny new allocated skb */
10362e1cd495SFelix Fietkau 		new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
10372e1cd495SFelix Fietkau 					      common->rx_bufsize, dma_type);
10382e1cd495SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
10392e1cd495SFelix Fietkau 			dev_kfree_skb_any(requeue_skb);
10402e1cd495SFelix Fietkau 			goto requeue_drop_frag;
10412e1cd495SFelix Fietkau 		}
10422e1cd495SFelix Fietkau 
1043203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1044203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
10452e1cd495SFelix Fietkau 				 common->rx_bufsize, dma_type);
1046203c4805SLuis R. Rodriguez 
1047176f0e84SSujith Manoharan 		bf->bf_mpdu = requeue_skb;
1048176f0e84SSujith Manoharan 		bf->bf_buf_addr = new_buf_addr;
1049176f0e84SSujith Manoharan 
1050b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1051b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1052b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1053203c4805SLuis R. Rodriguez 
10540d95521eSFelix Fietkau 		if (!rs.rs_more)
10555a078fcbSOleksij Rempel 			ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
1056c9b14170SLuis R. Rodriguez 						     rxs, decrypt_error);
1057203c4805SLuis R. Rodriguez 
10580d95521eSFelix Fietkau 		if (rs.rs_more) {
105915072189SBen Greear 			RX_STAT_INC(rx_frags);
10600d95521eSFelix Fietkau 			/*
10610d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
10620d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
10630d95521eSFelix Fietkau 			 * scatter-gather operation.
10640d95521eSFelix Fietkau 			 */
10650d95521eSFelix Fietkau 			if (sc->rx.frag) {
10660d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
10670d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
10680d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
106915072189SBen Greear 				RX_STAT_INC(rx_too_many_frags_err);
10700d95521eSFelix Fietkau 				skb = NULL;
10710d95521eSFelix Fietkau 			}
10720d95521eSFelix Fietkau 			sc->rx.frag = skb;
10730d95521eSFelix Fietkau 			goto requeue;
10740d95521eSFelix Fietkau 		}
10750d95521eSFelix Fietkau 
10760d95521eSFelix Fietkau 		if (sc->rx.frag) {
10770d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
10780d95521eSFelix Fietkau 
10790d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
10800d95521eSFelix Fietkau 				dev_kfree_skb(skb);
108115072189SBen Greear 				RX_STAT_INC(rx_oom_err);
10820d95521eSFelix Fietkau 				goto requeue_drop_frag;
10830d95521eSFelix Fietkau 			}
10840d95521eSFelix Fietkau 
1085b5447ff9SEric Dumazet 			sc->rx.frag = NULL;
1086b5447ff9SEric Dumazet 
10870d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
10880d95521eSFelix Fietkau 						  skb->len);
10890d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
10900d95521eSFelix Fietkau 			skb = hdr_skb;
10910d95521eSFelix Fietkau 		}
10920d95521eSFelix Fietkau 
109366760eacSFelix Fietkau 		if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
109466760eacSFelix Fietkau 			skb_trim(skb, skb->len - 8);
109566760eacSFelix Fietkau 
10968ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1097aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
10981b04b930SSujith 				     PS_WAIT_FOR_CAB |
1099aaef24b4SMohammed Shafi Shajakhan 				     PS_WAIT_FOR_PSPOLL_DATA)) ||
1100cedc7e3dSMohammed Shafi Shajakhan 		    ath9k_check_auto_sleep(sc))
1101f73c604cSRajkumar Manoharan 			ath_rx_ps(sc, skb, rs.is_mybeacon);
11028ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1103cc65965cSJouni Malinen 
1104c3124df7SSujith Manoharan 		ath9k_antenna_check(sc, &rs);
110521fbbca3SChristian Lamparter 		ath9k_apply_ampdu_details(sc, &rs, rxs);
1106350e2dcbSSujith Manoharan 		ath_debug_rate_stats(sc, &rs, skb);
110721fbbca3SChristian Lamparter 
11087545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1109cc65965cSJouni Malinen 
11100d95521eSFelix Fietkau requeue_drop_frag:
11110d95521eSFelix Fietkau 		if (sc->rx.frag) {
11120d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
11130d95521eSFelix Fietkau 			sc->rx.frag = NULL;
11140d95521eSFelix Fietkau 		}
1115203c4805SLuis R. Rodriguez requeue:
1116b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
1117a3dc48e8SFelix Fietkau 
1118a3dc48e8SFelix Fietkau 		if (edma) {
1119b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1120b5c80475SFelix Fietkau 		} else {
1121e96542e5SFelix Fietkau 			ath_rx_buf_relink(sc, bf);
11223a758134STim Harvey 			if (!flush)
112395294973SFelix Fietkau 				ath9k_hw_rxena(ah);
1124b5c80475SFelix Fietkau 		}
1125*c82552c5STim Harvey 
1126*c82552c5STim Harvey 		if (!budget--)
1127*c82552c5STim Harvey 			break;
1128203c4805SLuis R. Rodriguez 	} while (1);
1129203c4805SLuis R. Rodriguez 
113029ab0b36SRajkumar Manoharan 	if (!(ah->imask & ATH9K_INT_RXEOL)) {
113129ab0b36SRajkumar Manoharan 		ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
113272d874c6SFelix Fietkau 		ath9k_hw_set_interrupts(ah);
113329ab0b36SRajkumar Manoharan 	}
113429ab0b36SRajkumar Manoharan 
1135203c4805SLuis R. Rodriguez 	return 0;
1136203c4805SLuis R. Rodriguez }
1137