xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision f88373fa47f3ce6590fdfaa742d0ddacc2ae017f)
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 
21b5c80475SFelix Fietkau #define SKB_CB_ATHBUF(__skb)	(*((struct ath_buf **)__skb->cb))
22b5c80475SFelix Fietkau 
23102885a5SVasanthakumar Thiagarajan static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
24102885a5SVasanthakumar Thiagarajan 					       int mindelta, int main_rssi_avg,
25102885a5SVasanthakumar Thiagarajan 					       int alt_rssi_avg, int pkt_count)
26102885a5SVasanthakumar Thiagarajan {
27102885a5SVasanthakumar Thiagarajan 	return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
28102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + maxdelta)) ||
29102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
30102885a5SVasanthakumar Thiagarajan }
31102885a5SVasanthakumar Thiagarajan 
32b85c5734SMohammed Shafi Shajakhan static inline bool ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
33b85c5734SMohammed Shafi Shajakhan 					int curr_main_set, int curr_alt_set,
34b85c5734SMohammed Shafi Shajakhan 					int alt_rssi_avg, int main_rssi_avg)
35b85c5734SMohammed Shafi Shajakhan {
36b85c5734SMohammed Shafi Shajakhan 	bool result = false;
37b85c5734SMohammed Shafi Shajakhan 	switch (div_group) {
38b85c5734SMohammed Shafi Shajakhan 	case 0:
39b85c5734SMohammed Shafi Shajakhan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
40b85c5734SMohammed Shafi Shajakhan 			result = true;
41b85c5734SMohammed Shafi Shajakhan 		break;
42b85c5734SMohammed Shafi Shajakhan 	case 1:
4366ce235aSGabor Juhos 	case 2:
44b85c5734SMohammed Shafi Shajakhan 		if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
45b85c5734SMohammed Shafi Shajakhan 			(curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
46b85c5734SMohammed Shafi Shajakhan 				(alt_rssi_avg >= (main_rssi_avg - 5))) ||
47b85c5734SMohammed Shafi Shajakhan 			((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
48b85c5734SMohammed Shafi Shajakhan 			(curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
49b85c5734SMohammed Shafi Shajakhan 				(alt_rssi_avg >= (main_rssi_avg - 2)))) &&
50b85c5734SMohammed Shafi Shajakhan 							(alt_rssi_avg >= 4))
51b85c5734SMohammed Shafi Shajakhan 			result = true;
52b85c5734SMohammed Shafi Shajakhan 		else
53b85c5734SMohammed Shafi Shajakhan 			result = false;
54b85c5734SMohammed Shafi Shajakhan 		break;
55b85c5734SMohammed Shafi Shajakhan 	}
56b85c5734SMohammed Shafi Shajakhan 
57b85c5734SMohammed Shafi Shajakhan 	return result;
58b85c5734SMohammed Shafi Shajakhan }
59b85c5734SMohammed Shafi Shajakhan 
60ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
61ededf1f8SVasanthakumar Thiagarajan {
62ededf1f8SVasanthakumar Thiagarajan 	return sc->ps_enabled &&
63ededf1f8SVasanthakumar Thiagarajan 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
64ededf1f8SVasanthakumar Thiagarajan }
65ededf1f8SVasanthakumar Thiagarajan 
66203c4805SLuis R. Rodriguez /*
67203c4805SLuis R. Rodriguez  * Setup and link descriptors.
68203c4805SLuis R. Rodriguez  *
69203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
70203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
71203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
72203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
73203c4805SLuis R. Rodriguez  */
74203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
75203c4805SLuis R. Rodriguez {
76203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
77cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
78203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
79203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
80203c4805SLuis R. Rodriguez 
81203c4805SLuis R. Rodriguez 	ATH_RXBUF_RESET(bf);
82203c4805SLuis R. Rodriguez 
83203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
84203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
85203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
86203c4805SLuis R. Rodriguez 
87203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
88203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
899680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
90203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
91203c4805SLuis R. Rodriguez 
92cc861f74SLuis R. Rodriguez 	/*
93cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
94203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
95cc861f74SLuis R. Rodriguez 	 * to process
96cc861f74SLuis R. Rodriguez 	 */
97203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
98cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
99203c4805SLuis R. Rodriguez 			     0);
100203c4805SLuis R. Rodriguez 
101203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
102203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
103203c4805SLuis R. Rodriguez 	else
104203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
105203c4805SLuis R. Rodriguez 
106203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
107203c4805SLuis R. Rodriguez }
108203c4805SLuis R. Rodriguez 
109203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
110203c4805SLuis R. Rodriguez {
111203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
112203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
113203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
114203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
115203c4805SLuis R. Rodriguez }
116203c4805SLuis R. Rodriguez 
117203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
118203c4805SLuis R. Rodriguez {
119203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
1201510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
1211510718dSLuis R. Rodriguez 
122203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
123203c4805SLuis R. Rodriguez 
124203c4805SLuis R. Rodriguez 	/* configure rx filter */
125203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
126203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
127203c4805SLuis R. Rodriguez 
128203c4805SLuis R. Rodriguez 	/* configure bssid mask */
12913b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
130203c4805SLuis R. Rodriguez 
131203c4805SLuis R. Rodriguez 	/* configure operational mode */
132203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
133203c4805SLuis R. Rodriguez 
134203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
135203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
136203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
137203c4805SLuis R. Rodriguez }
138203c4805SLuis R. Rodriguez 
139b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
140b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
141b5c80475SFelix Fietkau {
142b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
143b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
144b5c80475SFelix Fietkau 	struct sk_buff *skb;
145b5c80475SFelix Fietkau 	struct ath_buf *bf;
146b5c80475SFelix Fietkau 
147b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
148b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
149b5c80475SFelix Fietkau 		return false;
150b5c80475SFelix Fietkau 
151b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
152b5c80475SFelix Fietkau 	list_del_init(&bf->list);
153b5c80475SFelix Fietkau 
154b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
155b5c80475SFelix Fietkau 
156b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
157b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
158b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
159b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
160b5c80475SFelix Fietkau 
161b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
162b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
163b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
164b5c80475SFelix Fietkau 
165b5c80475SFelix Fietkau 	return true;
166b5c80475SFelix Fietkau }
167b5c80475SFelix Fietkau 
168b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
169b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
170b5c80475SFelix Fietkau {
171b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
172b5c80475SFelix Fietkau 	u32 nbuf = 0;
173b5c80475SFelix Fietkau 
174b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
175d2182b69SJoe Perches 		ath_dbg(common, QUEUE, "No free rx buf available\n");
176b5c80475SFelix Fietkau 		return;
177b5c80475SFelix Fietkau 	}
178b5c80475SFelix Fietkau 
179b5c80475SFelix Fietkau 	while (!list_empty(&sc->rx.rxbuf)) {
180b5c80475SFelix Fietkau 		nbuf++;
181b5c80475SFelix Fietkau 
182b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
183b5c80475SFelix Fietkau 			break;
184b5c80475SFelix Fietkau 
185b5c80475SFelix Fietkau 		if (nbuf >= size)
186b5c80475SFelix Fietkau 			break;
187b5c80475SFelix Fietkau 	}
188b5c80475SFelix Fietkau }
189b5c80475SFelix Fietkau 
190b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
191b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
192b5c80475SFelix Fietkau {
193b5c80475SFelix Fietkau 	struct ath_buf *bf;
194b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
195b5c80475SFelix Fietkau 	struct sk_buff *skb;
196b5c80475SFelix Fietkau 
197b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
198b5c80475SFelix Fietkau 
199b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
200b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
201b5c80475SFelix Fietkau 		BUG_ON(!bf);
202b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
203b5c80475SFelix Fietkau 	}
204b5c80475SFelix Fietkau }
205b5c80475SFelix Fietkau 
206b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
207b5c80475SFelix Fietkau {
208ba542385SMohammed Shafi Shajakhan 	struct ath_hw *ah = sc->sc_ah;
209ba542385SMohammed Shafi Shajakhan 	struct ath_common *common = ath9k_hw_common(ah);
210b5c80475SFelix Fietkau 	struct ath_buf *bf;
211b5c80475SFelix Fietkau 
212b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
213b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
214b5c80475SFelix Fietkau 
215b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
216ba542385SMohammed Shafi Shajakhan 		if (bf->bf_mpdu) {
217ba542385SMohammed Shafi Shajakhan 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
218ba542385SMohammed Shafi Shajakhan 					common->rx_bufsize,
219ba542385SMohammed Shafi Shajakhan 					DMA_BIDIRECTIONAL);
220b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
221ba542385SMohammed Shafi Shajakhan 			bf->bf_buf_addr = 0;
222ba542385SMohammed Shafi Shajakhan 			bf->bf_mpdu = NULL;
223ba542385SMohammed Shafi Shajakhan 		}
224b5c80475SFelix Fietkau 	}
225b5c80475SFelix Fietkau 
226b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
227b5c80475SFelix Fietkau 
228b5c80475SFelix Fietkau 	kfree(sc->rx.rx_bufptr);
229b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = NULL;
230b5c80475SFelix Fietkau }
231b5c80475SFelix Fietkau 
232b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
233b5c80475SFelix Fietkau {
234b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
235b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_buffers);
236b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
237b5c80475SFelix Fietkau }
238b5c80475SFelix Fietkau 
239b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
240b5c80475SFelix Fietkau {
241b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
242b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
243b5c80475SFelix Fietkau 	struct sk_buff *skb;
244b5c80475SFelix Fietkau 	struct ath_buf *bf;
245b5c80475SFelix Fietkau 	int error = 0, i;
246b5c80475SFelix Fietkau 	u32 size;
247b5c80475SFelix Fietkau 
248b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
249b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
250b5c80475SFelix Fietkau 
251b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
252b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
253b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
254b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
255b5c80475SFelix Fietkau 
256b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
257b5c80475SFelix Fietkau 	bf = kzalloc(size, GFP_KERNEL);
258b5c80475SFelix Fietkau 	if (!bf)
259b5c80475SFelix Fietkau 		return -ENOMEM;
260b5c80475SFelix Fietkau 
261b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
262b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = bf;
263b5c80475SFelix Fietkau 
264b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
265b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
266b5c80475SFelix Fietkau 		if (!skb) {
267b5c80475SFelix Fietkau 			error = -ENOMEM;
268b5c80475SFelix Fietkau 			goto rx_init_fail;
269b5c80475SFelix Fietkau 		}
270b5c80475SFelix Fietkau 
271b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
272b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
273b5c80475SFelix Fietkau 
274b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
275b5c80475SFelix Fietkau 						 common->rx_bufsize,
276b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
277b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
278b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
279b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
280b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2816cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2823800276aSJoe Perches 				ath_err(common,
283b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
284b5c80475SFelix Fietkau 				error = -ENOMEM;
285b5c80475SFelix Fietkau 				goto rx_init_fail;
286b5c80475SFelix Fietkau 		}
287b5c80475SFelix Fietkau 
288b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
289b5c80475SFelix Fietkau 	}
290b5c80475SFelix Fietkau 
291b5c80475SFelix Fietkau 	return 0;
292b5c80475SFelix Fietkau 
293b5c80475SFelix Fietkau rx_init_fail:
294b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
295b5c80475SFelix Fietkau 	return error;
296b5c80475SFelix Fietkau }
297b5c80475SFelix Fietkau 
298b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
299b5c80475SFelix Fietkau {
300b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
301b5c80475SFelix Fietkau 
302b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
303b5c80475SFelix Fietkau 
304b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
305b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
306b5c80475SFelix Fietkau 
307b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
308b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
309b5c80475SFelix Fietkau 
310b5c80475SFelix Fietkau 	ath_opmode_init(sc);
311b5c80475SFelix Fietkau 
31248a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
3137583c550SLuis R. Rodriguez 
3147583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
315b5c80475SFelix Fietkau }
316b5c80475SFelix Fietkau 
317b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
318b5c80475SFelix Fietkau {
319b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
320b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
321b5c80475SFelix Fietkau }
322b5c80475SFelix Fietkau 
323203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
324203c4805SLuis R. Rodriguez {
32527c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
326203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
327203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
328203c4805SLuis R. Rodriguez 	int error = 0;
329203c4805SLuis R. Rodriguez 
3304bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
331203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
332203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxbuflock);
333203c4805SLuis R. Rodriguez 
3340d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
3350d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
3360d95521eSFelix Fietkau 
337b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
338b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
339b5c80475SFelix Fietkau 	} else {
340d2182b69SJoe Perches 		ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
341cc861f74SLuis R. Rodriguez 			common->cachelsz, common->rx_bufsize);
342203c4805SLuis R. Rodriguez 
343203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
344203c4805SLuis R. Rodriguez 
345203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
3464adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
347203c4805SLuis R. Rodriguez 		if (error != 0) {
3483800276aSJoe Perches 			ath_err(common,
349b5c80475SFelix Fietkau 				"failed to allocate rx descriptors: %d\n",
350b5c80475SFelix Fietkau 				error);
351203c4805SLuis R. Rodriguez 			goto err;
352203c4805SLuis R. Rodriguez 		}
353203c4805SLuis R. Rodriguez 
354203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
355b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
356b5c80475SFelix Fietkau 					      GFP_KERNEL);
357203c4805SLuis R. Rodriguez 			if (skb == NULL) {
358203c4805SLuis R. Rodriguez 				error = -ENOMEM;
359203c4805SLuis R. Rodriguez 				goto err;
360203c4805SLuis R. Rodriguez 			}
361203c4805SLuis R. Rodriguez 
362203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
363203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
364cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
365203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
366203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
367203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
368203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
369203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
3706cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3713800276aSJoe Perches 				ath_err(common,
372203c4805SLuis R. Rodriguez 					"dma_mapping_error() on RX init\n");
373203c4805SLuis R. Rodriguez 				error = -ENOMEM;
374203c4805SLuis R. Rodriguez 				goto err;
375203c4805SLuis R. Rodriguez 			}
376203c4805SLuis R. Rodriguez 		}
377203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
378b5c80475SFelix Fietkau 	}
379203c4805SLuis R. Rodriguez 
380203c4805SLuis R. Rodriguez err:
381203c4805SLuis R. Rodriguez 	if (error)
382203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
383203c4805SLuis R. Rodriguez 
384203c4805SLuis R. Rodriguez 	return error;
385203c4805SLuis R. Rodriguez }
386203c4805SLuis R. Rodriguez 
387203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
388203c4805SLuis R. Rodriguez {
389cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
390cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
391203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
392203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
393203c4805SLuis R. Rodriguez 
394b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
395b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
396b5c80475SFelix Fietkau 		return;
397b5c80475SFelix Fietkau 	} else {
398203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
399203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
400203c4805SLuis R. Rodriguez 			if (skb) {
401203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
402b5c80475SFelix Fietkau 						common->rx_bufsize,
403b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
404203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
4056cf9e995SBen Greear 				bf->bf_buf_addr = 0;
4066cf9e995SBen Greear 				bf->bf_mpdu = NULL;
407203c4805SLuis R. Rodriguez 			}
408203c4805SLuis R. Rodriguez 		}
409203c4805SLuis R. Rodriguez 
410203c4805SLuis R. Rodriguez 		if (sc->rx.rxdma.dd_desc_len != 0)
411203c4805SLuis R. Rodriguez 			ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
412203c4805SLuis R. Rodriguez 	}
413b5c80475SFelix Fietkau }
414203c4805SLuis R. Rodriguez 
415203c4805SLuis R. Rodriguez /*
416203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
417203c4805SLuis R. Rodriguez  * operating mode and state:
418203c4805SLuis R. Rodriguez  *
419203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
420203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
421203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
422203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
423203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
424203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
425203c4805SLuis R. Rodriguez  * o accept beacons:
426203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
427203c4805SLuis R. Rodriguez  *     node table entries for peers,
428203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
429203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
430203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
431203c4805SLuis R. Rodriguez  *   - when scanning
432203c4805SLuis R. Rodriguez  */
433203c4805SLuis R. Rodriguez 
434203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
435203c4805SLuis R. Rodriguez {
436203c4805SLuis R. Rodriguez 	u32 rfilt;
437203c4805SLuis R. Rodriguez 
438ac06697cSFelix Fietkau 	rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
439203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
440203c4805SLuis R. Rodriguez 
4419c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
442203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
443203c4805SLuis R. Rodriguez 
444203c4805SLuis R. Rodriguez 	/*
445203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
446203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
447203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
448203c4805SLuis R. Rodriguez 	 */
4492e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
450203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
451203c4805SLuis R. Rodriguez 
452203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
453203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
454203c4805SLuis R. Rodriguez 
455203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
456cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
457203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
458203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
459203c4805SLuis R. Rodriguez 	else
460203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
461203c4805SLuis R. Rodriguez 
462264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
46366afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
464203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
465203c4805SLuis R. Rodriguez 
4667ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4677ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4687ea310beSSujith 
4697545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
4705eb6ba83SJavier Cardona 		/* The following may also be needed for other older chips */
4715eb6ba83SJavier Cardona 		if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
4725eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
473203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
474203c4805SLuis R. Rodriguez 	}
475203c4805SLuis R. Rodriguez 
476203c4805SLuis R. Rodriguez 	return rfilt;
477203c4805SLuis R. Rodriguez 
478203c4805SLuis R. Rodriguez }
479203c4805SLuis R. Rodriguez 
480203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
481203c4805SLuis R. Rodriguez {
482203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
483203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
484203c4805SLuis R. Rodriguez 
485b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
486b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
487b5c80475SFelix Fietkau 		return 0;
488b5c80475SFelix Fietkau 	}
489b5c80475SFelix Fietkau 
490203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
491203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
492203c4805SLuis R. Rodriguez 		goto start_recv;
493203c4805SLuis R. Rodriguez 
494203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
495203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
496203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
497203c4805SLuis R. Rodriguez 	}
498203c4805SLuis R. Rodriguez 
499203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
500203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
501203c4805SLuis R. Rodriguez 		goto start_recv;
502203c4805SLuis R. Rodriguez 
503203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
504203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
505203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
506203c4805SLuis R. Rodriguez 
507203c4805SLuis R. Rodriguez start_recv:
508203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
50948a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
510203c4805SLuis R. Rodriguez 
5117583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
5127583c550SLuis R. Rodriguez 
513203c4805SLuis R. Rodriguez 	return 0;
514203c4805SLuis R. Rodriguez }
515203c4805SLuis R. Rodriguez 
516203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
517203c4805SLuis R. Rodriguez {
518203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
5195882da02SFelix Fietkau 	bool stopped, reset = false;
520203c4805SLuis R. Rodriguez 
5211e450285SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
522d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
523203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
5245882da02SFelix Fietkau 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
525b5c80475SFelix Fietkau 
526b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
527b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
528b5c80475SFelix Fietkau 	else
529203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
5301e450285SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
531203c4805SLuis R. Rodriguez 
532d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
533d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
534d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
535d7fd1b50SBen Greear 			"Could not stop RX, we could be "
53678a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
537d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
538d7fd1b50SBen Greear 	}
5392232d31bSFelix Fietkau 	return stopped && !reset;
540203c4805SLuis R. Rodriguez }
541203c4805SLuis R. Rodriguez 
542203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc)
543203c4805SLuis R. Rodriguez {
544203c4805SLuis R. Rodriguez 	sc->sc_flags |= SC_OP_RXFLUSH;
545b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
546b5c80475SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
547b5c80475SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
548203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
549203c4805SLuis R. Rodriguez }
550203c4805SLuis R. Rodriguez 
551cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
552cc65965cSJouni Malinen {
553cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
554cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
555cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
556cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
557cc65965cSJouni Malinen 
558cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
559cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
560cc65965cSJouni Malinen 	end = skb->data + skb->len;
561cc65965cSJouni Malinen 
562cc65965cSJouni Malinen 	while (pos + 2 < end) {
563cc65965cSJouni Malinen 		id = *pos++;
564cc65965cSJouni Malinen 		elen = *pos++;
565cc65965cSJouni Malinen 		if (pos + elen > end)
566cc65965cSJouni Malinen 			break;
567cc65965cSJouni Malinen 
568cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
569cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
570cc65965cSJouni Malinen 				break;
571cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
572cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
573cc65965cSJouni Malinen 				break;
574cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
575cc65965cSJouni Malinen 		}
576cc65965cSJouni Malinen 
577cc65965cSJouni Malinen 		pos += elen;
578cc65965cSJouni Malinen 	}
579cc65965cSJouni Malinen 
580cc65965cSJouni Malinen 	return false;
581cc65965cSJouni Malinen }
582cc65965cSJouni Malinen 
583cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
584cc65965cSJouni Malinen {
5851510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
586cc65965cSJouni Malinen 
587cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
588cc65965cSJouni Malinen 		return;
589cc65965cSJouni Malinen 
5901b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
591293dc5dfSGabor Juhos 
5921b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5931b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
594d2182b69SJoe Perches 		ath_dbg(common, PS,
595226afe68SJoe Perches 			"Reconfigure Beacon timers based on timestamp from the AP\n");
59699e4d43aSRajkumar Manoharan 		ath_set_beacon(sc);
597ccdfeab6SJouni Malinen 	}
598ccdfeab6SJouni Malinen 
599cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
600cc65965cSJouni Malinen 		/*
601cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
60258f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
60358f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
60458f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
60558f5fffdSGabor Juhos 		 * so we are waiting for it as well.
606cc65965cSJouni Malinen 		 */
607d2182b69SJoe Perches 		ath_dbg(common, PS,
608226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
6091b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
610cc65965cSJouni Malinen 		return;
611cc65965cSJouni Malinen 	}
612cc65965cSJouni Malinen 
6131b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
614cc65965cSJouni Malinen 		/*
615cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
616cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
617cc65965cSJouni Malinen 		 * been delivered.
618cc65965cSJouni Malinen 		 */
6191b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
620d2182b69SJoe Perches 		ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
621cc65965cSJouni Malinen 	}
622cc65965cSJouni Malinen }
623cc65965cSJouni Malinen 
624f73c604cSRajkumar Manoharan static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
625cc65965cSJouni Malinen {
626cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
627c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
628cc65965cSJouni Malinen 
629cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
630cc65965cSJouni Malinen 
631cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
632ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
633f73c604cSRajkumar Manoharan 	    && mybeacon)
634cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
6351b04b930SSujith 	else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
636cc65965cSJouni Malinen 		 (ieee80211_is_data(hdr->frame_control) ||
637cc65965cSJouni Malinen 		  ieee80211_is_action(hdr->frame_control)) &&
638cc65965cSJouni Malinen 		 is_multicast_ether_addr(hdr->addr1) &&
639cc65965cSJouni Malinen 		 !ieee80211_has_moredata(hdr->frame_control)) {
640cc65965cSJouni Malinen 		/*
641cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
642cc65965cSJouni Malinen 		 * point.
643cc65965cSJouni Malinen 		 */
6443fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
645d2182b69SJoe Perches 		ath_dbg(common, PS,
646c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
6471b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6489a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
6499a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
6501b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
651d2182b69SJoe Perches 		ath_dbg(common, PS,
652226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
6531b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
6541b04b930SSujith 					PS_WAIT_FOR_CAB |
6551b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6561b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
657cc65965cSJouni Malinen 	}
658cc65965cSJouni Malinen }
659cc65965cSJouni Malinen 
660b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
661b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
662203c4805SLuis R. Rodriguez {
663b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
664203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
66527c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
666b5c80475SFelix Fietkau 	struct sk_buff *skb;
667b5c80475SFelix Fietkau 	struct ath_buf *bf;
668b5c80475SFelix Fietkau 	int ret;
669203c4805SLuis R. Rodriguez 
670b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
671b5c80475SFelix Fietkau 	if (!skb)
672b5c80475SFelix Fietkau 		return false;
673203c4805SLuis R. Rodriguez 
674b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
675b5c80475SFelix Fietkau 	BUG_ON(!bf);
676b5c80475SFelix Fietkau 
677ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
678b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
679b5c80475SFelix Fietkau 
680b5c80475SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
681ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
682ce9426d1SMing Lei 		/*let device gain the buffer again*/
683ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
684ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
685b5c80475SFelix Fietkau 		return false;
686ce9426d1SMing Lei 	}
687b5c80475SFelix Fietkau 
688b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
689b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
690b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
691b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
692b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
693b5c80475SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
694b5c80475SFelix Fietkau 		if (!skb)
695b5c80475SFelix Fietkau 			return true;
696b5c80475SFelix Fietkau 
697b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
698b5c80475SFelix Fietkau 		BUG_ON(!bf);
699b5c80475SFelix Fietkau 
700b5c80475SFelix Fietkau 		__skb_unlink(skb, &rx_edma->rx_fifo);
701b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
702b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
703083e3e8dSVasanthakumar Thiagarajan 		return true;
704b5c80475SFelix Fietkau 	}
705b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_buffers, skb);
706b5c80475SFelix Fietkau 
707b5c80475SFelix Fietkau 	return true;
708b5c80475SFelix Fietkau }
709b5c80475SFelix Fietkau 
710b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
711b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
712b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
713b5c80475SFelix Fietkau {
714b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
715b5c80475SFelix Fietkau 	struct sk_buff *skb;
716b5c80475SFelix Fietkau 	struct ath_buf *bf;
717b5c80475SFelix Fietkau 
718b5c80475SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype));
719b5c80475SFelix Fietkau 	skb = __skb_dequeue(&rx_edma->rx_buffers);
720b5c80475SFelix Fietkau 	if (!skb)
721b5c80475SFelix Fietkau 		return NULL;
722b5c80475SFelix Fietkau 
723b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
724b5c80475SFelix Fietkau 	ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
725b5c80475SFelix Fietkau 	return bf;
726b5c80475SFelix Fietkau }
727b5c80475SFelix Fietkau 
728b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
729b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
730b5c80475SFelix Fietkau {
731b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
732b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
733b5c80475SFelix Fietkau 	struct ath_desc *ds;
734b5c80475SFelix Fietkau 	struct ath_buf *bf;
735b5c80475SFelix Fietkau 	int ret;
736203c4805SLuis R. Rodriguez 
737203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
738203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
739b5c80475SFelix Fietkau 		return NULL;
740203c4805SLuis R. Rodriguez 	}
741203c4805SLuis R. Rodriguez 
742203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
743203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
744203c4805SLuis R. Rodriguez 
745203c4805SLuis R. Rodriguez 	/*
746203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
747203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
748203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
749203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
750203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
751203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
752203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
753203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
754203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
755203c4805SLuis R. Rodriguez 	 */
7563de21116SRajkumar Manoharan 	ret = ath9k_hw_rxprocdesc(ah, ds, rs);
757b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
75829bffa96SFelix Fietkau 		struct ath_rx_status trs;
759203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
760203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
761203c4805SLuis R. Rodriguez 
76229bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
763203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
764203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
765b5c80475SFelix Fietkau 			return NULL;
766203c4805SLuis R. Rodriguez 		}
767203c4805SLuis R. Rodriguez 
768203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
769203c4805SLuis R. Rodriguez 
770203c4805SLuis R. Rodriguez 		/*
771203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
772203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
773203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
774203c4805SLuis R. Rodriguez 		 * set or not.
775203c4805SLuis R. Rodriguez 		 *
776203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
777203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
778203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
779203c4805SLuis R. Rodriguez 		 */
780203c4805SLuis R. Rodriguez 
781203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
7823de21116SRajkumar Manoharan 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
783b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
784b5c80475SFelix Fietkau 			return NULL;
785203c4805SLuis R. Rodriguez 	}
786203c4805SLuis R. Rodriguez 
787b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
788b5c80475SFelix Fietkau 		return bf;
789203c4805SLuis R. Rodriguez 
790203c4805SLuis R. Rodriguez 	/*
791203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
792203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
793203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
794203c4805SLuis R. Rodriguez 	 */
795ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
796cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
797203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
798203c4805SLuis R. Rodriguez 
799b5c80475SFelix Fietkau 	return bf;
800b5c80475SFelix Fietkau }
801b5c80475SFelix Fietkau 
802d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
803d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
8049f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
805d435700fSSujith 			    struct ieee80211_rx_status *rxs,
806d435700fSSujith 			    struct ath_rx_status *rx_stats,
807d435700fSSujith 			    bool *decrypt_error)
808d435700fSSujith {
809ec205999SFelix Fietkau 	struct ath_softc *sc = (struct ath_softc *) common->priv;
81066760eacSFelix Fietkau 	bool is_mc, is_valid_tkip, strip_mic, mic_error;
811d435700fSSujith 	struct ath_hw *ah = common->ah;
812d435700fSSujith 	__le16 fc;
813b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
814d435700fSSujith 
815d435700fSSujith 	fc = hdr->frame_control;
816d435700fSSujith 
81766760eacSFelix Fietkau 	is_mc = !!is_multicast_ether_addr(hdr->addr1);
81866760eacSFelix Fietkau 	is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
81966760eacSFelix Fietkau 		test_bit(rx_stats->rs_keyix, common->tkip_keymap);
820152e585dSBill Jordan 	strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
821152e585dSBill Jordan 		!(rx_stats->rs_status &
822846d9363SFelix Fietkau 		(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
823846d9363SFelix Fietkau 		 ATH9K_RXERR_KEYMISS));
82466760eacSFelix Fietkau 
825*f88373faSFelix Fietkau 	/*
826*f88373faSFelix Fietkau 	 * Key miss events are only relevant for pairwise keys where the
827*f88373faSFelix Fietkau 	 * descriptor does contain a valid key index. This has been observed
828*f88373faSFelix Fietkau 	 * mostly with CCMP encryption.
829*f88373faSFelix Fietkau 	 */
830*f88373faSFelix Fietkau 	if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID)
831*f88373faSFelix Fietkau 		rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
832*f88373faSFelix Fietkau 
833d435700fSSujith 	if (!rx_stats->rs_datalen)
834d435700fSSujith 		return false;
835d435700fSSujith         /*
836d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
837d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
838d435700fSSujith          * those frames.
839d435700fSSujith          */
840b7b1b512SVasanthakumar Thiagarajan 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
841d435700fSSujith 		return false;
842d435700fSSujith 
8430d95521eSFelix Fietkau 	/* Only use error bits from the last fragment */
844d435700fSSujith 	if (rx_stats->rs_more)
8450d95521eSFelix Fietkau 		return true;
846d435700fSSujith 
84766760eacSFelix Fietkau 	mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
84866760eacSFelix Fietkau 		!ieee80211_has_morefrags(fc) &&
84966760eacSFelix Fietkau 		!(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
85066760eacSFelix Fietkau 		(rx_stats->rs_status & ATH9K_RXERR_MIC);
85166760eacSFelix Fietkau 
852d435700fSSujith 	/*
853d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
854d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
855d435700fSSujith 	 * rs_more will be false at the last element of the chained
856d435700fSSujith 	 * descriptors.
857d435700fSSujith 	 */
858d435700fSSujith 	if (rx_stats->rs_status != 0) {
859846d9363SFelix Fietkau 		u8 status_mask;
860846d9363SFelix Fietkau 
86166760eacSFelix Fietkau 		if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
862d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
86366760eacSFelix Fietkau 			mic_error = false;
86466760eacSFelix Fietkau 		}
865d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
866d435700fSSujith 			return false;
867d435700fSSujith 
868846d9363SFelix Fietkau 		if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
869846d9363SFelix Fietkau 		    (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
870d435700fSSujith 			*decrypt_error = true;
87166760eacSFelix Fietkau 			mic_error = false;
872d435700fSSujith 		}
87366760eacSFelix Fietkau 
874d435700fSSujith 		/*
875d435700fSSujith 		 * Reject error frames with the exception of
876d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
877d435700fSSujith 		 * we also ignore the CRC error.
878d435700fSSujith 		 */
879846d9363SFelix Fietkau 		status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
880846d9363SFelix Fietkau 			      ATH9K_RXERR_KEYMISS;
881846d9363SFelix Fietkau 
882ec205999SFelix Fietkau 		if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
883846d9363SFelix Fietkau 			status_mask |= ATH9K_RXERR_CRC;
884846d9363SFelix Fietkau 
885846d9363SFelix Fietkau 		if (rx_stats->rs_status & ~status_mask)
886d435700fSSujith 			return false;
887d435700fSSujith 	}
88866760eacSFelix Fietkau 
88966760eacSFelix Fietkau 	/*
89066760eacSFelix Fietkau 	 * For unicast frames the MIC error bit can have false positives,
89166760eacSFelix Fietkau 	 * so all MIC error reports need to be validated in software.
89266760eacSFelix Fietkau 	 * False negatives are not common, so skip software verification
89366760eacSFelix Fietkau 	 * if the hardware considers the MIC valid.
89466760eacSFelix Fietkau 	 */
89566760eacSFelix Fietkau 	if (strip_mic)
89666760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_STRIPPED;
89766760eacSFelix Fietkau 	else if (is_mc && mic_error)
89866760eacSFelix Fietkau 		rxs->flag |= RX_FLAG_MMIC_ERROR;
89966760eacSFelix Fietkau 
900d435700fSSujith 	return true;
901d435700fSSujith }
902d435700fSSujith 
903d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
904d435700fSSujith 			      struct ieee80211_hw *hw,
905d435700fSSujith 			      struct ath_rx_status *rx_stats,
9069f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
907d435700fSSujith {
908d435700fSSujith 	struct ieee80211_supported_band *sband;
909d435700fSSujith 	enum ieee80211_band band;
910d435700fSSujith 	unsigned int i = 0;
911d435700fSSujith 
912d435700fSSujith 	band = hw->conf.channel->band;
913d435700fSSujith 	sband = hw->wiphy->bands[band];
914d435700fSSujith 
915d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
916d435700fSSujith 		/* HT rate */
917d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
918d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
919d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
920d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
921d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
922d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
923d435700fSSujith 		return 0;
924d435700fSSujith 	}
925d435700fSSujith 
926d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
927d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
928d435700fSSujith 			rxs->rate_idx = i;
929d435700fSSujith 			return 0;
930d435700fSSujith 		}
931d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
932d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
933d435700fSSujith 			rxs->rate_idx = i;
934d435700fSSujith 			return 0;
935d435700fSSujith 		}
936d435700fSSujith 	}
937d435700fSSujith 
938d435700fSSujith 	/*
939d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
940d435700fSSujith 	 * because hardware has already validated this frame as OK.
941d435700fSSujith 	 */
942d2182b69SJoe Perches 	ath_dbg(common, ANY,
943226afe68SJoe Perches 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
944226afe68SJoe Perches 		rx_stats->rs_rate);
945d435700fSSujith 
946d435700fSSujith 	return -EINVAL;
947d435700fSSujith }
948d435700fSSujith 
949d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
950d435700fSSujith 			       struct ieee80211_hw *hw,
9519f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
952d435700fSSujith 			       struct ath_rx_status *rx_stats)
953d435700fSSujith {
9549ac58615SFelix Fietkau 	struct ath_softc *sc = hw->priv;
955d435700fSSujith 	struct ath_hw *ah = common->ah;
9569fa23e17SFelix Fietkau 	int last_rssi;
957d435700fSSujith 
958cf3af748SRajkumar Manoharan 	if (!rx_stats->is_mybeacon ||
959cf3af748SRajkumar Manoharan 	    ((ah->opmode != NL80211_IFTYPE_STATION) &&
960cf3af748SRajkumar Manoharan 	     (ah->opmode != NL80211_IFTYPE_ADHOC)))
9619fa23e17SFelix Fietkau 		return;
9629fa23e17SFelix Fietkau 
9639fa23e17SFelix Fietkau 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9649ac58615SFelix Fietkau 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
965686b9cb9SBen Greear 
9669ac58615SFelix Fietkau 	last_rssi = sc->last_rssi;
967d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
968d435700fSSujith 		rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
969d435700fSSujith 					      ATH_RSSI_EP_MULTIPLIER);
970d435700fSSujith 	if (rx_stats->rs_rssi < 0)
971d435700fSSujith 		rx_stats->rs_rssi = 0;
972d435700fSSujith 
973d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
974d435700fSSujith 	ah->stats.avgbrssi = rx_stats->rs_rssi;
975d435700fSSujith }
976d435700fSSujith 
977d435700fSSujith /*
978d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
979d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
980d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
981d435700fSSujith  */
982d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
983d435700fSSujith 				   struct ieee80211_hw *hw,
9849f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
985d435700fSSujith 				   struct ath_rx_status *rx_stats,
986d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
987d435700fSSujith 				   bool *decrypt_error)
988d435700fSSujith {
989f749b946SFelix Fietkau 	struct ath_hw *ah = common->ah;
990f749b946SFelix Fietkau 
991d435700fSSujith 	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
992d435700fSSujith 
993d435700fSSujith 	/*
994d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
995d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
996d435700fSSujith 	 */
9979f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
998d435700fSSujith 		return -EINVAL;
999d435700fSSujith 
10000d95521eSFelix Fietkau 	/* Only use status info from the last fragment */
10010d95521eSFelix Fietkau 	if (rx_stats->rs_more)
10020d95521eSFelix Fietkau 		return 0;
10030d95521eSFelix Fietkau 
10049f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
1005d435700fSSujith 
10069f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1007d435700fSSujith 		return -EINVAL;
1008d435700fSSujith 
1009d435700fSSujith 	rx_status->band = hw->conf.channel->band;
1010d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
1011f749b946SFelix Fietkau 	rx_status->signal = ah->noise + rx_stats->rs_rssi;
1012d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
10136ebacbb7SJohannes Berg 	rx_status->flag |= RX_FLAG_MACTIME_MPDU;
1014d435700fSSujith 
1015d435700fSSujith 	return 0;
1016d435700fSSujith }
1017d435700fSSujith 
1018d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
1019d435700fSSujith 				     struct sk_buff *skb,
1020d435700fSSujith 				     struct ath_rx_status *rx_stats,
1021d435700fSSujith 				     struct ieee80211_rx_status *rxs,
1022d435700fSSujith 				     bool decrypt_error)
1023d435700fSSujith {
1024d435700fSSujith 	struct ath_hw *ah = common->ah;
1025d435700fSSujith 	struct ieee80211_hdr *hdr;
1026d435700fSSujith 	int hdrlen, padpos, padsize;
1027d435700fSSujith 	u8 keyix;
1028d435700fSSujith 	__le16 fc;
1029d435700fSSujith 
1030d435700fSSujith 	/* see if any padding is done by the hw and remove it */
1031d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1032d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1033d435700fSSujith 	fc = hdr->frame_control;
1034d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
1035d435700fSSujith 
1036d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1037d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1038d435700fSSujith 	 * padsize would take into account odd header lengths:
1039d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1040d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1041d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1042d435700fSSujith 	 * not try to remove padding from short control frames that do
1043d435700fSSujith 	 * not have payload. */
1044d435700fSSujith 	padsize = padpos & 3;
1045d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1046d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1047d435700fSSujith 		skb_pull(skb, padsize);
1048d435700fSSujith 	}
1049d435700fSSujith 
1050d435700fSSujith 	keyix = rx_stats->rs_keyix;
1051d435700fSSujith 
1052d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1053d435700fSSujith 	    ieee80211_has_protected(fc)) {
1054d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1055d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1056d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1057d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1058d435700fSSujith 
1059d435700fSSujith 		if (test_bit(keyix, common->keymap))
1060d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1061d435700fSSujith 	}
1062d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1063d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1064d435700fSSujith 	    ieee80211_is_mgmt(fc))
1065d435700fSSujith 		/* Use software decrypt for management frames. */
1066d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1067d435700fSSujith }
1068b5c80475SFelix Fietkau 
1069102885a5SVasanthakumar Thiagarajan static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1070102885a5SVasanthakumar Thiagarajan 				      struct ath_hw_antcomb_conf ant_conf,
1071102885a5SVasanthakumar Thiagarajan 				      int main_rssi_avg)
1072102885a5SVasanthakumar Thiagarajan {
1073102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt = 0;
1074102885a5SVasanthakumar Thiagarajan 
1075102885a5SVasanthakumar Thiagarajan 	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1076102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna2 = main_rssi_avg;
1077102885a5SVasanthakumar Thiagarajan 	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1078102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna1 = main_rssi_avg;
1079102885a5SVasanthakumar Thiagarajan 
1080102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1081223c5a87SGabor Juhos 	case 0x10: /* LNA2 A-B */
1082102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1083102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1084102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1085102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1086102885a5SVasanthakumar Thiagarajan 		break;
1087223c5a87SGabor Juhos 	case 0x20: /* LNA1 A-B */
1088102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1089102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1090102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1091102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1092102885a5SVasanthakumar Thiagarajan 		break;
1093223c5a87SGabor Juhos 	case 0x21: /* LNA1 LNA2 */
1094102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1095102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1096102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1097102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1098102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1099102885a5SVasanthakumar Thiagarajan 		break;
1100223c5a87SGabor Juhos 	case 0x12: /* LNA2 LNA1 */
1101102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1102102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1103102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1104102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1105102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1106102885a5SVasanthakumar Thiagarajan 		break;
1107223c5a87SGabor Juhos 	case 0x13: /* LNA2 A+B */
1108102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1109102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1110102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1111102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1112102885a5SVasanthakumar Thiagarajan 		break;
1113223c5a87SGabor Juhos 	case 0x23: /* LNA1 A+B */
1114102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1115102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1116102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1117102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1118102885a5SVasanthakumar Thiagarajan 		break;
1119102885a5SVasanthakumar Thiagarajan 	default:
1120102885a5SVasanthakumar Thiagarajan 		break;
1121102885a5SVasanthakumar Thiagarajan 	}
1122102885a5SVasanthakumar Thiagarajan }
1123102885a5SVasanthakumar Thiagarajan 
1124102885a5SVasanthakumar Thiagarajan static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1125102885a5SVasanthakumar Thiagarajan 				struct ath_hw_antcomb_conf *div_ant_conf,
1126102885a5SVasanthakumar Thiagarajan 				int main_rssi_avg, int alt_rssi_avg,
1127102885a5SVasanthakumar Thiagarajan 				int alt_ratio)
1128102885a5SVasanthakumar Thiagarajan {
1129102885a5SVasanthakumar Thiagarajan 	/* alt_good */
1130102885a5SVasanthakumar Thiagarajan 	switch (antcomb->quick_scan_cnt) {
1131102885a5SVasanthakumar Thiagarajan 	case 0:
1132102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1133102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1134102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1135102885a5SVasanthakumar Thiagarajan 		break;
1136102885a5SVasanthakumar Thiagarajan 	case 1:
1137102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1138102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1139102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1140102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1141102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_second = alt_rssi_avg;
1142102885a5SVasanthakumar Thiagarajan 
1143102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1144102885a5SVasanthakumar Thiagarajan 			/* main is LNA1 */
1145102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1146102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1147102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1148102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1149102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1150102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1151102885a5SVasanthakumar Thiagarajan 			else
1152102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1153102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1154102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1155102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1156102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1157102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1158102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1159102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1160102885a5SVasanthakumar Thiagarajan 			else
1161102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1162102885a5SVasanthakumar Thiagarajan 		} else {
1163102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1164102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1165102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1166102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1167102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1168102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1169102885a5SVasanthakumar Thiagarajan 			else
1170102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1171102885a5SVasanthakumar Thiagarajan 		}
1172102885a5SVasanthakumar Thiagarajan 		break;
1173102885a5SVasanthakumar Thiagarajan 	case 2:
1174102885a5SVasanthakumar Thiagarajan 		antcomb->alt_good = false;
1175102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = false;
1176102885a5SVasanthakumar Thiagarajan 		antcomb->scan = false;
1177102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1178102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_third = alt_rssi_avg;
1179102885a5SVasanthakumar Thiagarajan 
1180102885a5SVasanthakumar Thiagarajan 		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1181102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1182102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1183102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA2)
1184102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1185102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1186102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1187102885a5SVasanthakumar Thiagarajan 			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1188102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna2 = main_rssi_avg;
1189102885a5SVasanthakumar Thiagarajan 			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1190102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna1 = main_rssi_avg;
1191102885a5SVasanthakumar Thiagarajan 		}
1192102885a5SVasanthakumar Thiagarajan 
1193102885a5SVasanthakumar Thiagarajan 		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1194102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1195102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1196102885a5SVasanthakumar Thiagarajan 		else
1197102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1198102885a5SVasanthakumar Thiagarajan 
1199102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1200102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1201102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1202102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1203102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1204102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1205102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1206102885a5SVasanthakumar Thiagarajan 			else
1207102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1208102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1209102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1210102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1211102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1212102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1213102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1214102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1215102885a5SVasanthakumar Thiagarajan 			else
1216102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1217102885a5SVasanthakumar Thiagarajan 		} else {
1218102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1219102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1220102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1221102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1222102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1223102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1224102885a5SVasanthakumar Thiagarajan 			else
1225102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1226102885a5SVasanthakumar Thiagarajan 		}
1227102885a5SVasanthakumar Thiagarajan 
1228102885a5SVasanthakumar Thiagarajan 		/* set alt to the conf with maximun ratio */
1229102885a5SVasanthakumar Thiagarajan 		if (antcomb->first_ratio && antcomb->second_ratio) {
1230102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_second > antcomb->rssi_third) {
1231102885a5SVasanthakumar Thiagarajan 				/* first alt*/
1232102885a5SVasanthakumar Thiagarajan 				if ((antcomb->first_quick_scan_conf ==
1233102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA1) ||
1234102885a5SVasanthakumar Thiagarajan 				    (antcomb->first_quick_scan_conf ==
1235102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2))
1236102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2*/
1237102885a5SVasanthakumar Thiagarajan 					if (div_ant_conf->main_lna_conf ==
1238102885a5SVasanthakumar Thiagarajan 					    ATH_ANT_DIV_COMB_LNA2)
1239102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1240102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1241102885a5SVasanthakumar Thiagarajan 					else
1242102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1243102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1244102885a5SVasanthakumar Thiagarajan 				else
1245102885a5SVasanthakumar Thiagarajan 					/* Set alt to A+B or A-B */
1246102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1247102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1248102885a5SVasanthakumar Thiagarajan 			} else if ((antcomb->second_quick_scan_conf ==
1249102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA1) ||
1250102885a5SVasanthakumar Thiagarajan 				   (antcomb->second_quick_scan_conf ==
1251102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA2)) {
1252102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1253102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1254102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1255102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1256102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1257102885a5SVasanthakumar Thiagarajan 				else
1258102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1259102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1260102885a5SVasanthakumar Thiagarajan 			} else {
1261102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1262102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1263102885a5SVasanthakumar Thiagarajan 					antcomb->second_quick_scan_conf;
1264102885a5SVasanthakumar Thiagarajan 			}
1265102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->first_ratio) {
1266102885a5SVasanthakumar Thiagarajan 			/* first alt */
1267102885a5SVasanthakumar Thiagarajan 			if ((antcomb->first_quick_scan_conf ==
1268102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1269102885a5SVasanthakumar Thiagarajan 			    (antcomb->first_quick_scan_conf ==
1270102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1271102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2 */
1272102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1273102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1274102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1275102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1276102885a5SVasanthakumar Thiagarajan 				else
1277102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1278102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1279102885a5SVasanthakumar Thiagarajan 			else
1280102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1281102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1282102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1283102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->second_ratio) {
1284102885a5SVasanthakumar Thiagarajan 				/* second alt */
1285102885a5SVasanthakumar Thiagarajan 			if ((antcomb->second_quick_scan_conf ==
1286102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1287102885a5SVasanthakumar Thiagarajan 			    (antcomb->second_quick_scan_conf ==
1288102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1289102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1290102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1291102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1292102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1293102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1294102885a5SVasanthakumar Thiagarajan 				else
1295102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1296102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1297102885a5SVasanthakumar Thiagarajan 			else
1298102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1299102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1300102885a5SVasanthakumar Thiagarajan 						antcomb->second_quick_scan_conf;
1301102885a5SVasanthakumar Thiagarajan 		} else {
1302102885a5SVasanthakumar Thiagarajan 			/* main is largest */
1303102885a5SVasanthakumar Thiagarajan 			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1304102885a5SVasanthakumar Thiagarajan 			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1305102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1306102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1307102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1308102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1309102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1310102885a5SVasanthakumar Thiagarajan 				else
1311102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1312102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1313102885a5SVasanthakumar Thiagarajan 			else
1314102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1315102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf = antcomb->main_conf;
1316102885a5SVasanthakumar Thiagarajan 		}
1317102885a5SVasanthakumar Thiagarajan 		break;
1318102885a5SVasanthakumar Thiagarajan 	default:
1319102885a5SVasanthakumar Thiagarajan 		break;
1320102885a5SVasanthakumar Thiagarajan 	}
1321102885a5SVasanthakumar Thiagarajan }
1322102885a5SVasanthakumar Thiagarajan 
13233e9a212aSMohammed Shafi Shajakhan static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
13243e9a212aSMohammed Shafi Shajakhan 		struct ath_ant_comb *antcomb, int alt_ratio)
1325102885a5SVasanthakumar Thiagarajan {
13263e9a212aSMohammed Shafi Shajakhan 	if (ant_conf->div_group == 0) {
1327102885a5SVasanthakumar Thiagarajan 		/* Adjust the fast_div_bias based on main and alt lna conf */
13283e9a212aSMohammed Shafi Shajakhan 		switch ((ant_conf->main_lna_conf << 4) |
13293e9a212aSMohammed Shafi Shajakhan 				ant_conf->alt_lna_conf) {
1330223c5a87SGabor Juhos 		case 0x01: /* A-B LNA2 */
1331102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3b;
1332102885a5SVasanthakumar Thiagarajan 			break;
1333223c5a87SGabor Juhos 		case 0x02: /* A-B LNA1 */
1334102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3d;
1335102885a5SVasanthakumar Thiagarajan 			break;
1336223c5a87SGabor Juhos 		case 0x03: /* A-B A+B */
1337102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x1;
1338102885a5SVasanthakumar Thiagarajan 			break;
1339223c5a87SGabor Juhos 		case 0x10: /* LNA2 A-B */
1340102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x7;
1341102885a5SVasanthakumar Thiagarajan 			break;
1342223c5a87SGabor Juhos 		case 0x12: /* LNA2 LNA1 */
1343102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x2;
1344102885a5SVasanthakumar Thiagarajan 			break;
1345223c5a87SGabor Juhos 		case 0x13: /* LNA2 A+B */
1346102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x7;
1347102885a5SVasanthakumar Thiagarajan 			break;
1348223c5a87SGabor Juhos 		case 0x20: /* LNA1 A-B */
1349102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x6;
1350102885a5SVasanthakumar Thiagarajan 			break;
1351223c5a87SGabor Juhos 		case 0x21: /* LNA1 LNA2 */
1352102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x0;
1353102885a5SVasanthakumar Thiagarajan 			break;
1354223c5a87SGabor Juhos 		case 0x23: /* LNA1 A+B */
1355102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x6;
1356102885a5SVasanthakumar Thiagarajan 			break;
1357223c5a87SGabor Juhos 		case 0x30: /* A+B A-B */
1358102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x1;
1359102885a5SVasanthakumar Thiagarajan 			break;
1360223c5a87SGabor Juhos 		case 0x31: /* A+B LNA2 */
1361102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3b;
1362102885a5SVasanthakumar Thiagarajan 			break;
1363223c5a87SGabor Juhos 		case 0x32: /* A+B LNA1 */
1364102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3d;
1365102885a5SVasanthakumar Thiagarajan 			break;
1366102885a5SVasanthakumar Thiagarajan 		default:
1367102885a5SVasanthakumar Thiagarajan 			break;
1368102885a5SVasanthakumar Thiagarajan 		}
1369e7ef5bc0SGabor Juhos 	} else if (ant_conf->div_group == 1) {
1370e7ef5bc0SGabor Juhos 		/* Adjust the fast_div_bias based on main and alt_lna_conf */
1371e7ef5bc0SGabor Juhos 		switch ((ant_conf->main_lna_conf << 4) |
1372e7ef5bc0SGabor Juhos 			ant_conf->alt_lna_conf) {
1373e7ef5bc0SGabor Juhos 		case 0x01: /* A-B LNA2 */
1374e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1375e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1376e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1377e7ef5bc0SGabor Juhos 			break;
1378e7ef5bc0SGabor Juhos 		case 0x02: /* A-B LNA1 */
1379e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1380e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1381e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1382e7ef5bc0SGabor Juhos 			break;
1383e7ef5bc0SGabor Juhos 		case 0x03: /* A-B A+B */
1384e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1385e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1386e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1387e7ef5bc0SGabor Juhos 			break;
1388e7ef5bc0SGabor Juhos 		case 0x10: /* LNA2 A-B */
1389e7ef5bc0SGabor Juhos 			if (!(antcomb->scan) &&
1390e7ef5bc0SGabor Juhos 			    (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1391e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x3f;
1392e7ef5bc0SGabor Juhos 			else
1393e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x1;
1394e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1395e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1396e7ef5bc0SGabor Juhos 			break;
1397e7ef5bc0SGabor Juhos 		case 0x12: /* LNA2 LNA1 */
1398e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1399e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1400e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1401e7ef5bc0SGabor Juhos 			break;
1402e7ef5bc0SGabor Juhos 		case 0x13: /* LNA2 A+B */
1403e7ef5bc0SGabor Juhos 			if (!(antcomb->scan) &&
1404e7ef5bc0SGabor Juhos 			    (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1405e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x3f;
1406e7ef5bc0SGabor Juhos 			else
1407e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x1;
1408e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1409e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1410e7ef5bc0SGabor Juhos 			break;
1411e7ef5bc0SGabor Juhos 		case 0x20: /* LNA1 A-B */
1412e7ef5bc0SGabor Juhos 			if (!(antcomb->scan) &&
1413e7ef5bc0SGabor Juhos 			    (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1414e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x3f;
1415e7ef5bc0SGabor Juhos 			else
1416e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x1;
1417e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1418e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1419e7ef5bc0SGabor Juhos 			break;
1420e7ef5bc0SGabor Juhos 		case 0x21: /* LNA1 LNA2 */
1421e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1422e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1423e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1424e7ef5bc0SGabor Juhos 			break;
1425e7ef5bc0SGabor Juhos 		case 0x23: /* LNA1 A+B */
1426e7ef5bc0SGabor Juhos 			if (!(antcomb->scan) &&
1427e7ef5bc0SGabor Juhos 			    (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1428e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x3f;
1429e7ef5bc0SGabor Juhos 			else
1430e7ef5bc0SGabor Juhos 				ant_conf->fast_div_bias = 0x1;
1431e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1432e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1433e7ef5bc0SGabor Juhos 			break;
1434e7ef5bc0SGabor Juhos 		case 0x30: /* A+B A-B */
1435e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1436e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1437e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1438e7ef5bc0SGabor Juhos 			break;
1439e7ef5bc0SGabor Juhos 		case 0x31: /* A+B LNA2 */
1440e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1441e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1442e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1443e7ef5bc0SGabor Juhos 			break;
1444e7ef5bc0SGabor Juhos 		case 0x32: /* A+B LNA1 */
1445e7ef5bc0SGabor Juhos 			ant_conf->fast_div_bias = 0x1;
1446e7ef5bc0SGabor Juhos 			ant_conf->main_gaintb = 0;
1447e7ef5bc0SGabor Juhos 			ant_conf->alt_gaintb = 0;
1448e7ef5bc0SGabor Juhos 			break;
1449e7ef5bc0SGabor Juhos 		default:
1450e7ef5bc0SGabor Juhos 			break;
1451e7ef5bc0SGabor Juhos 		}
14523e9a212aSMohammed Shafi Shajakhan 	} else if (ant_conf->div_group == 2) {
14533e9a212aSMohammed Shafi Shajakhan 		/* Adjust the fast_div_bias based on main and alt_lna_conf */
14543e9a212aSMohammed Shafi Shajakhan 		switch ((ant_conf->main_lna_conf << 4) |
14553e9a212aSMohammed Shafi Shajakhan 				ant_conf->alt_lna_conf) {
1456223c5a87SGabor Juhos 		case 0x01: /* A-B LNA2 */
14573e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14583e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14593e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14603e9a212aSMohammed Shafi Shajakhan 			break;
1461223c5a87SGabor Juhos 		case 0x02: /* A-B LNA1 */
14623e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14633e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14643e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14653e9a212aSMohammed Shafi Shajakhan 			break;
1466223c5a87SGabor Juhos 		case 0x03: /* A-B A+B */
14673e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14683e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14693e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14703e9a212aSMohammed Shafi Shajakhan 			break;
1471223c5a87SGabor Juhos 		case 0x10: /* LNA2 A-B */
14723e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14733e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14743e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14753e9a212aSMohammed Shafi Shajakhan 			else
14763e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
14773e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14783e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14793e9a212aSMohammed Shafi Shajakhan 			break;
1480223c5a87SGabor Juhos 		case 0x12: /* LNA2 LNA1 */
14813e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14823e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14833e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14843e9a212aSMohammed Shafi Shajakhan 			break;
1485223c5a87SGabor Juhos 		case 0x13: /* LNA2 A+B */
14863e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14873e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14883e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14893e9a212aSMohammed Shafi Shajakhan 			else
14903e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
14913e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14923e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14933e9a212aSMohammed Shafi Shajakhan 			break;
1494223c5a87SGabor Juhos 		case 0x20: /* LNA1 A-B */
14953e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14963e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14973e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14983e9a212aSMohammed Shafi Shajakhan 			else
14993e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
15003e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15013e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15023e9a212aSMohammed Shafi Shajakhan 			break;
1503223c5a87SGabor Juhos 		case 0x21: /* LNA1 LNA2 */
15043e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
15053e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15063e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15073e9a212aSMohammed Shafi Shajakhan 			break;
1508223c5a87SGabor Juhos 		case 0x23: /* LNA1 A+B */
15093e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
15103e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
15113e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
15123e9a212aSMohammed Shafi Shajakhan 			else
15133e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
15143e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15153e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15163e9a212aSMohammed Shafi Shajakhan 			break;
1517223c5a87SGabor Juhos 		case 0x30: /* A+B A-B */
15183e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
15193e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15203e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15213e9a212aSMohammed Shafi Shajakhan 			break;
1522223c5a87SGabor Juhos 		case 0x31: /* A+B LNA2 */
15233e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
15243e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15253e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15263e9a212aSMohammed Shafi Shajakhan 			break;
1527223c5a87SGabor Juhos 		case 0x32: /* A+B LNA1 */
15283e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
15293e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
15303e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
15313e9a212aSMohammed Shafi Shajakhan 			break;
15323e9a212aSMohammed Shafi Shajakhan 		default:
15333e9a212aSMohammed Shafi Shajakhan 			break;
15343e9a212aSMohammed Shafi Shajakhan 		}
15353e9a212aSMohammed Shafi Shajakhan 	}
1536102885a5SVasanthakumar Thiagarajan }
1537102885a5SVasanthakumar Thiagarajan 
1538102885a5SVasanthakumar Thiagarajan /* Antenna diversity and combining */
1539102885a5SVasanthakumar Thiagarajan static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1540102885a5SVasanthakumar Thiagarajan {
1541102885a5SVasanthakumar Thiagarajan 	struct ath_hw_antcomb_conf div_ant_conf;
1542102885a5SVasanthakumar Thiagarajan 	struct ath_ant_comb *antcomb = &sc->ant_comb;
1543102885a5SVasanthakumar Thiagarajan 	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
15440ff2b5c0SSujith Manoharan 	int curr_main_set;
1545102885a5SVasanthakumar Thiagarajan 	int main_rssi = rs->rs_rssi_ctl0;
1546102885a5SVasanthakumar Thiagarajan 	int alt_rssi = rs->rs_rssi_ctl1;
1547102885a5SVasanthakumar Thiagarajan 	int rx_ant_conf,  main_ant_conf;
1548102885a5SVasanthakumar Thiagarajan 	bool short_scan = false;
1549102885a5SVasanthakumar Thiagarajan 
1550102885a5SVasanthakumar Thiagarajan 	rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1551102885a5SVasanthakumar Thiagarajan 		       ATH_ANT_RX_MASK;
1552102885a5SVasanthakumar Thiagarajan 	main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1553102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_RX_MASK;
1554102885a5SVasanthakumar Thiagarajan 
155521e8ee6dSMohammed Shafi Shajakhan 	/* Record packet only when both main_rssi and  alt_rssi is positive */
155621e8ee6dSMohammed Shafi Shajakhan 	if (main_rssi > 0 && alt_rssi > 0) {
1557102885a5SVasanthakumar Thiagarajan 		antcomb->total_pkt_count++;
1558102885a5SVasanthakumar Thiagarajan 		antcomb->main_total_rssi += main_rssi;
1559102885a5SVasanthakumar Thiagarajan 		antcomb->alt_total_rssi  += alt_rssi;
1560102885a5SVasanthakumar Thiagarajan 		if (main_ant_conf == rx_ant_conf)
1561102885a5SVasanthakumar Thiagarajan 			antcomb->main_recv_cnt++;
1562102885a5SVasanthakumar Thiagarajan 		else
1563102885a5SVasanthakumar Thiagarajan 			antcomb->alt_recv_cnt++;
1564102885a5SVasanthakumar Thiagarajan 	}
1565102885a5SVasanthakumar Thiagarajan 
1566102885a5SVasanthakumar Thiagarajan 	/* Short scan check */
1567102885a5SVasanthakumar Thiagarajan 	if (antcomb->scan && antcomb->alt_good) {
1568102885a5SVasanthakumar Thiagarajan 		if (time_after(jiffies, antcomb->scan_start_time +
1569102885a5SVasanthakumar Thiagarajan 		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1570102885a5SVasanthakumar Thiagarajan 			short_scan = true;
1571102885a5SVasanthakumar Thiagarajan 		else
1572102885a5SVasanthakumar Thiagarajan 			if (antcomb->total_pkt_count ==
1573102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1574102885a5SVasanthakumar Thiagarajan 				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1575102885a5SVasanthakumar Thiagarajan 					    antcomb->total_pkt_count);
1576102885a5SVasanthakumar Thiagarajan 				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1577102885a5SVasanthakumar Thiagarajan 					short_scan = true;
1578102885a5SVasanthakumar Thiagarajan 			}
1579102885a5SVasanthakumar Thiagarajan 	}
1580102885a5SVasanthakumar Thiagarajan 
1581102885a5SVasanthakumar Thiagarajan 	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1582102885a5SVasanthakumar Thiagarajan 	    rs->rs_moreaggr) && !short_scan)
1583102885a5SVasanthakumar Thiagarajan 		return;
1584102885a5SVasanthakumar Thiagarajan 
1585102885a5SVasanthakumar Thiagarajan 	if (antcomb->total_pkt_count) {
1586102885a5SVasanthakumar Thiagarajan 		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1587102885a5SVasanthakumar Thiagarajan 			     antcomb->total_pkt_count);
1588102885a5SVasanthakumar Thiagarajan 		main_rssi_avg = (antcomb->main_total_rssi /
1589102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1590102885a5SVasanthakumar Thiagarajan 		alt_rssi_avg = (antcomb->alt_total_rssi /
1591102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1592102885a5SVasanthakumar Thiagarajan 	}
1593102885a5SVasanthakumar Thiagarajan 
1594102885a5SVasanthakumar Thiagarajan 
1595102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1596102885a5SVasanthakumar Thiagarajan 	curr_alt_set = div_ant_conf.alt_lna_conf;
1597102885a5SVasanthakumar Thiagarajan 	curr_main_set = div_ant_conf.main_lna_conf;
1598102885a5SVasanthakumar Thiagarajan 
1599102885a5SVasanthakumar Thiagarajan 	antcomb->count++;
1600102885a5SVasanthakumar Thiagarajan 
1601102885a5SVasanthakumar Thiagarajan 	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1602102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1603102885a5SVasanthakumar Thiagarajan 			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1604102885a5SVasanthakumar Thiagarajan 						  main_rssi_avg);
1605102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = true;
1606102885a5SVasanthakumar Thiagarajan 		} else {
1607102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = false;
1608102885a5SVasanthakumar Thiagarajan 		}
1609102885a5SVasanthakumar Thiagarajan 
1610102885a5SVasanthakumar Thiagarajan 		antcomb->count = 0;
1611102885a5SVasanthakumar Thiagarajan 		antcomb->scan = true;
1612102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = true;
1613102885a5SVasanthakumar Thiagarajan 	}
1614102885a5SVasanthakumar Thiagarajan 
1615102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan) {
1616b85c5734SMohammed Shafi Shajakhan 		if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1617b85c5734SMohammed Shafi Shajakhan 					alt_ratio, curr_main_set, curr_alt_set,
1618b85c5734SMohammed Shafi Shajakhan 					alt_rssi_avg, main_rssi_avg)) {
1619102885a5SVasanthakumar Thiagarajan 			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1620102885a5SVasanthakumar Thiagarajan 				/* Switch main and alt LNA */
1621102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1622102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1623102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1624102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1625102885a5SVasanthakumar Thiagarajan 			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1626102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1627102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1628102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1629102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1630102885a5SVasanthakumar Thiagarajan 			}
1631102885a5SVasanthakumar Thiagarajan 
1632102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1633102885a5SVasanthakumar Thiagarajan 		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1634102885a5SVasanthakumar Thiagarajan 			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1635102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1636102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1637102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1638102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1639102885a5SVasanthakumar Thiagarajan 			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1640102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1641102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1642102885a5SVasanthakumar Thiagarajan 
1643102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1644102885a5SVasanthakumar Thiagarajan 		}
1645102885a5SVasanthakumar Thiagarajan 
1646102885a5SVasanthakumar Thiagarajan 		if ((alt_rssi_avg < (main_rssi_avg +
16478afbcc8bSMohammed Shafi Shajakhan 						div_ant_conf.lna1_lna2_delta)))
1648102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1649102885a5SVasanthakumar Thiagarajan 	}
1650102885a5SVasanthakumar Thiagarajan 
1651102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan_not_start) {
1652102885a5SVasanthakumar Thiagarajan 		switch (curr_alt_set) {
1653102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA2:
1654102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1655102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = main_rssi_avg;
1656102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1657102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1658102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf =
1659102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1;
1660102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1661102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1662102885a5SVasanthakumar Thiagarajan 			break;
1663102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1:
1664102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1665102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = main_rssi_avg;
1666102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1667102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1668102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1669102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1670102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1671102885a5SVasanthakumar Thiagarajan 			break;
1672102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1673102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_add = alt_rssi_avg;
1674102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1675102885a5SVasanthakumar Thiagarajan 			/* set to A-B */
1676102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf =
1677102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1678102885a5SVasanthakumar Thiagarajan 			break;
1679102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1680102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_sub = alt_rssi_avg;
1681102885a5SVasanthakumar Thiagarajan 			antcomb->scan = false;
1682102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_lna2 >
1683102885a5SVasanthakumar Thiagarajan 			    (antcomb->rssi_lna1 +
1684102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1685102885a5SVasanthakumar Thiagarajan 				/* use LNA2 as main LNA */
1686102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1687102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1688102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1689102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1690102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1691102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1692102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1693102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1694102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1695102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1696102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1697102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1698102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1699102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1700102885a5SVasanthakumar Thiagarajan 				} else {
1701102885a5SVasanthakumar Thiagarajan 					/* set to LNA1 */
1702102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1703102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1704102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1705102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1706102885a5SVasanthakumar Thiagarajan 				}
1707102885a5SVasanthakumar Thiagarajan 			} else {
1708102885a5SVasanthakumar Thiagarajan 				/* use LNA1 as main LNA */
1709102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1710102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1711102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1712102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1713102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1714102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1715102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1716102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1717102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1718102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1719102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1720102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1721102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1722102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1723102885a5SVasanthakumar Thiagarajan 				} else {
1724102885a5SVasanthakumar Thiagarajan 					/* set to LNA2 */
1725102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1726102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1727102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1728102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1729102885a5SVasanthakumar Thiagarajan 				}
1730102885a5SVasanthakumar Thiagarajan 			}
1731102885a5SVasanthakumar Thiagarajan 			break;
1732102885a5SVasanthakumar Thiagarajan 		default:
1733102885a5SVasanthakumar Thiagarajan 			break;
1734102885a5SVasanthakumar Thiagarajan 		}
1735102885a5SVasanthakumar Thiagarajan 	} else {
1736102885a5SVasanthakumar Thiagarajan 		if (!antcomb->alt_good) {
1737102885a5SVasanthakumar Thiagarajan 			antcomb->scan_not_start = false;
1738102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1739102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1740102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1741102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1742102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1743102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1744102885a5SVasanthakumar Thiagarajan 			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1745102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1746102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1747102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1748102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1749102885a5SVasanthakumar Thiagarajan 			}
1750102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1751102885a5SVasanthakumar Thiagarajan 		}
1752102885a5SVasanthakumar Thiagarajan 	}
1753102885a5SVasanthakumar Thiagarajan 
1754102885a5SVasanthakumar Thiagarajan 	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1755102885a5SVasanthakumar Thiagarajan 					   main_rssi_avg, alt_rssi_avg,
1756102885a5SVasanthakumar Thiagarajan 					   alt_ratio);
1757102885a5SVasanthakumar Thiagarajan 
1758102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt++;
1759102885a5SVasanthakumar Thiagarajan 
1760102885a5SVasanthakumar Thiagarajan div_comb_done:
17613e9a212aSMohammed Shafi Shajakhan 	ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
1762102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1763102885a5SVasanthakumar Thiagarajan 
1764102885a5SVasanthakumar Thiagarajan 	antcomb->scan_start_time = jiffies;
1765102885a5SVasanthakumar Thiagarajan 	antcomb->total_pkt_count = 0;
1766102885a5SVasanthakumar Thiagarajan 	antcomb->main_total_rssi = 0;
1767102885a5SVasanthakumar Thiagarajan 	antcomb->alt_total_rssi = 0;
1768102885a5SVasanthakumar Thiagarajan 	antcomb->main_recv_cnt = 0;
1769102885a5SVasanthakumar Thiagarajan 	antcomb->alt_recv_cnt = 0;
1770102885a5SVasanthakumar Thiagarajan }
1771102885a5SVasanthakumar Thiagarajan 
1772b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1773b5c80475SFelix Fietkau {
1774b5c80475SFelix Fietkau 	struct ath_buf *bf;
17750d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1776b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1777b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1778b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
17797545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1780b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1781b5c80475SFelix Fietkau 	int retval;
1782b5c80475SFelix Fietkau 	bool decrypt_error = false;
1783b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1784b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1785b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1786b5c80475SFelix Fietkau 	int dma_type;
17875c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1788a6d2055bSFelix Fietkau 	u64 tsf = 0;
1789a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
17908ab2cd09SLuis R. Rodriguez 	unsigned long flags;
1791b5c80475SFelix Fietkau 
1792b5c80475SFelix Fietkau 	if (edma)
1793b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
179456824223SMing Lei 	else
179556824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1796b5c80475SFelix Fietkau 
1797b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1798b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
1799b5c80475SFelix Fietkau 
1800a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1801a6d2055bSFelix Fietkau 	tsf_lower = tsf & 0xffffffff;
1802a6d2055bSFelix Fietkau 
1803b5c80475SFelix Fietkau 	do {
1804b5c80475SFelix Fietkau 		/* If handling rx interrupt and flush is in progress => exit */
1805b5c80475SFelix Fietkau 		if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1806b5c80475SFelix Fietkau 			break;
1807b5c80475SFelix Fietkau 
1808b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1809b5c80475SFelix Fietkau 		if (edma)
1810b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1811b5c80475SFelix Fietkau 		else
1812b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1813b5c80475SFelix Fietkau 
1814b5c80475SFelix Fietkau 		if (!bf)
1815b5c80475SFelix Fietkau 			break;
1816b5c80475SFelix Fietkau 
1817b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1818b5c80475SFelix Fietkau 		if (!skb)
1819b5c80475SFelix Fietkau 			continue;
1820b5c80475SFelix Fietkau 
18210d95521eSFelix Fietkau 		/*
18220d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
18230d95521eSFelix Fietkau 		 * the last one.
18240d95521eSFelix Fietkau 		 */
18250d95521eSFelix Fietkau 		if (sc->rx.frag)
18260d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
18270d95521eSFelix Fietkau 		else
18280d95521eSFelix Fietkau 			hdr_skb = skb;
18290d95521eSFelix Fietkau 
18300d95521eSFelix Fietkau 		hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
18310d95521eSFelix Fietkau 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
1832cf3af748SRajkumar Manoharan 		if (ieee80211_is_beacon(hdr->frame_control) &&
1833356cb55dSMohammed Shafi Shajakhan 		    !is_zero_ether_addr(common->curbssid) &&
1834cf3af748SRajkumar Manoharan 		    !compare_ether_addr(hdr->addr3, common->curbssid))
1835cf3af748SRajkumar Manoharan 			rs.is_mybeacon = true;
1836cf3af748SRajkumar Manoharan 		else
1837cf3af748SRajkumar Manoharan 			rs.is_mybeacon = false;
18385ca42627SLuis R. Rodriguez 
183929bffa96SFelix Fietkau 		ath_debug_stat_rx(sc, &rs);
18401395d3f0SSujith 
1841203c4805SLuis R. Rodriguez 		/*
1842203c4805SLuis R. Rodriguez 		 * If we're asked to flush receive queue, directly
1843203c4805SLuis R. Rodriguez 		 * chain it back at the queue without processing it.
1844203c4805SLuis R. Rodriguez 		 */
18453483288cSFelix Fietkau 		if (sc->sc_flags & SC_OP_RXFLUSH)
18460d95521eSFelix Fietkau 			goto requeue_drop_frag;
1847203c4805SLuis R. Rodriguez 
1848a6d2055bSFelix Fietkau 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1849a6d2055bSFelix Fietkau 		if (rs.rs_tstamp > tsf_lower &&
1850a6d2055bSFelix Fietkau 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1851a6d2055bSFelix Fietkau 			rxs->mactime -= 0x100000000ULL;
1852a6d2055bSFelix Fietkau 
1853a6d2055bSFelix Fietkau 		if (rs.rs_tstamp < tsf_lower &&
1854a6d2055bSFelix Fietkau 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1855a6d2055bSFelix Fietkau 			rxs->mactime += 0x100000000ULL;
1856a6d2055bSFelix Fietkau 
185783c76570SZefir Kurtisi 		retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
185883c76570SZefir Kurtisi 						 rxs, &decrypt_error);
185983c76570SZefir Kurtisi 		if (retval)
186083c76570SZefir Kurtisi 			goto requeue_drop_frag;
186183c76570SZefir Kurtisi 
1862203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1863203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1864cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1865203c4805SLuis R. Rodriguez 
1866203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1867203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1868203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1869203c4805SLuis R. Rodriguez 		 * processing. */
1870203c4805SLuis R. Rodriguez 		if (!requeue_skb)
18710d95521eSFelix Fietkau 			goto requeue_drop_frag;
1872203c4805SLuis R. Rodriguez 
1873203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1874203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1875cc861f74SLuis R. Rodriguez 				 common->rx_bufsize,
1876b5c80475SFelix Fietkau 				 dma_type);
1877203c4805SLuis R. Rodriguez 
1878b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1879b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1880b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1881203c4805SLuis R. Rodriguez 
18820d95521eSFelix Fietkau 		if (!rs.rs_more)
18830d95521eSFelix Fietkau 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1884c9b14170SLuis R. Rodriguez 						 rxs, decrypt_error);
1885203c4805SLuis R. Rodriguez 
1886203c4805SLuis R. Rodriguez 		/* We will now give hardware our shiny new allocated skb */
1887203c4805SLuis R. Rodriguez 		bf->bf_mpdu = requeue_skb;
1888203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1889cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
1890b5c80475SFelix Fietkau 						 dma_type);
1891203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
1892203c4805SLuis R. Rodriguez 			  bf->bf_buf_addr))) {
1893203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(requeue_skb);
1894203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
18956cf9e995SBen Greear 			bf->bf_buf_addr = 0;
18963800276aSJoe Perches 			ath_err(common, "dma_mapping_error() on RX\n");
18977545daf4SFelix Fietkau 			ieee80211_rx(hw, skb);
1898203c4805SLuis R. Rodriguez 			break;
1899203c4805SLuis R. Rodriguez 		}
1900203c4805SLuis R. Rodriguez 
19010d95521eSFelix Fietkau 		if (rs.rs_more) {
19020d95521eSFelix Fietkau 			/*
19030d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
19040d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
19050d95521eSFelix Fietkau 			 * scatter-gather operation.
19060d95521eSFelix Fietkau 			 */
19070d95521eSFelix Fietkau 			if (sc->rx.frag) {
19080d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
19090d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
19100d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
19110d95521eSFelix Fietkau 				skb = NULL;
19120d95521eSFelix Fietkau 			}
19130d95521eSFelix Fietkau 			sc->rx.frag = skb;
19140d95521eSFelix Fietkau 			goto requeue;
19150d95521eSFelix Fietkau 		}
19160d95521eSFelix Fietkau 
19170d95521eSFelix Fietkau 		if (sc->rx.frag) {
19180d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
19190d95521eSFelix Fietkau 
19200d95521eSFelix Fietkau 			sc->rx.frag = NULL;
19210d95521eSFelix Fietkau 
19220d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
19230d95521eSFelix Fietkau 				dev_kfree_skb(skb);
19240d95521eSFelix Fietkau 				goto requeue_drop_frag;
19250d95521eSFelix Fietkau 			}
19260d95521eSFelix Fietkau 
19270d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
19280d95521eSFelix Fietkau 						  skb->len);
19290d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
19300d95521eSFelix Fietkau 			skb = hdr_skb;
19310d95521eSFelix Fietkau 		}
19320d95521eSFelix Fietkau 
1933eb840a80SMohammed Shafi Shajakhan 
1934eb840a80SMohammed Shafi Shajakhan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1935eb840a80SMohammed Shafi Shajakhan 
1936203c4805SLuis R. Rodriguez 			/*
1937eb840a80SMohammed Shafi Shajakhan 			 * change the default rx antenna if rx diversity
1938eb840a80SMohammed Shafi Shajakhan 			 * chooses the other antenna 3 times in a row.
1939203c4805SLuis R. Rodriguez 			 */
194029bffa96SFelix Fietkau 			if (sc->rx.defant != rs.rs_antenna) {
1941203c4805SLuis R. Rodriguez 				if (++sc->rx.rxotherant >= 3)
194229bffa96SFelix Fietkau 					ath_setdefantenna(sc, rs.rs_antenna);
1943203c4805SLuis R. Rodriguez 			} else {
1944203c4805SLuis R. Rodriguez 				sc->rx.rxotherant = 0;
1945203c4805SLuis R. Rodriguez 			}
1946203c4805SLuis R. Rodriguez 
1947eb840a80SMohammed Shafi Shajakhan 		}
1948eb840a80SMohammed Shafi Shajakhan 
194966760eacSFelix Fietkau 		if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
195066760eacSFelix Fietkau 			skb_trim(skb, skb->len - 8);
195166760eacSFelix Fietkau 
19528ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1953aaef24b4SMohammed Shafi Shajakhan 
1954aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
19551b04b930SSujith 				     PS_WAIT_FOR_CAB |
1956aaef24b4SMohammed Shafi Shajakhan 				     PS_WAIT_FOR_PSPOLL_DATA)) ||
1957cedc7e3dSMohammed Shafi Shajakhan 		    ath9k_check_auto_sleep(sc))
1958f73c604cSRajkumar Manoharan 			ath_rx_ps(sc, skb, rs.is_mybeacon);
19598ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1960cc65965cSJouni Malinen 
196143c35284SFelix Fietkau 		if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
1962102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1963102885a5SVasanthakumar Thiagarajan 
19647545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1965cc65965cSJouni Malinen 
19660d95521eSFelix Fietkau requeue_drop_frag:
19670d95521eSFelix Fietkau 		if (sc->rx.frag) {
19680d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
19690d95521eSFelix Fietkau 			sc->rx.frag = NULL;
19700d95521eSFelix Fietkau 		}
1971203c4805SLuis R. Rodriguez requeue:
1972b5c80475SFelix Fietkau 		if (edma) {
1973b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
1974b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1975b5c80475SFelix Fietkau 		} else {
1976203c4805SLuis R. Rodriguez 			list_move_tail(&bf->list, &sc->rx.rxbuf);
1977203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
19783483288cSFelix Fietkau 			if (!flush)
197995294973SFelix Fietkau 				ath9k_hw_rxena(ah);
1980b5c80475SFelix Fietkau 		}
1981203c4805SLuis R. Rodriguez 	} while (1);
1982203c4805SLuis R. Rodriguez 
1983203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
1984203c4805SLuis R. Rodriguez 
198529ab0b36SRajkumar Manoharan 	if (!(ah->imask & ATH9K_INT_RXEOL)) {
198629ab0b36SRajkumar Manoharan 		ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
198772d874c6SFelix Fietkau 		ath9k_hw_set_interrupts(ah);
198829ab0b36SRajkumar Manoharan 	}
198929ab0b36SRajkumar Manoharan 
1990203c4805SLuis R. Rodriguez 	return 0;
1991203c4805SLuis R. Rodriguez }
1992