xref: /openbmc/linux/drivers/net/wireless/ath/ath9k/recv.c (revision 21e8ee6d207f6d384689571101436eb9070c22ca)
1203c4805SLuis R. Rodriguez /*
2203c4805SLuis R. Rodriguez  * Copyright (c) 2008-2009 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 
17203c4805SLuis R. Rodriguez #include "ath9k.h"
18b622a720SLuis R. Rodriguez #include "ar9003_mac.h"
19203c4805SLuis R. Rodriguez 
20b5c80475SFelix Fietkau #define SKB_CB_ATHBUF(__skb)	(*((struct ath_buf **)__skb->cb))
21b5c80475SFelix Fietkau 
22102885a5SVasanthakumar Thiagarajan static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
23102885a5SVasanthakumar Thiagarajan 					       int mindelta, int main_rssi_avg,
24102885a5SVasanthakumar Thiagarajan 					       int alt_rssi_avg, int pkt_count)
25102885a5SVasanthakumar Thiagarajan {
26102885a5SVasanthakumar Thiagarajan 	return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
27102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + maxdelta)) ||
28102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
29102885a5SVasanthakumar Thiagarajan }
30102885a5SVasanthakumar Thiagarajan 
31b85c5734SMohammed Shafi Shajakhan static inline bool ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
32b85c5734SMohammed Shafi Shajakhan 					int curr_main_set, int curr_alt_set,
33b85c5734SMohammed Shafi Shajakhan 					int alt_rssi_avg, int main_rssi_avg)
34b85c5734SMohammed Shafi Shajakhan {
35b85c5734SMohammed Shafi Shajakhan 	bool result = false;
36b85c5734SMohammed Shafi Shajakhan 	switch (div_group) {
37b85c5734SMohammed Shafi Shajakhan 	case 0:
38b85c5734SMohammed Shafi Shajakhan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
39b85c5734SMohammed Shafi Shajakhan 			result = true;
40b85c5734SMohammed Shafi Shajakhan 		break;
41b85c5734SMohammed Shafi Shajakhan 	case 1:
42b85c5734SMohammed Shafi Shajakhan 		if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
43b85c5734SMohammed Shafi Shajakhan 			(curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
44b85c5734SMohammed Shafi Shajakhan 				(alt_rssi_avg >= (main_rssi_avg - 5))) ||
45b85c5734SMohammed Shafi Shajakhan 			((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
46b85c5734SMohammed Shafi Shajakhan 			(curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
47b85c5734SMohammed Shafi Shajakhan 				(alt_rssi_avg >= (main_rssi_avg - 2)))) &&
48b85c5734SMohammed Shafi Shajakhan 							(alt_rssi_avg >= 4))
49b85c5734SMohammed Shafi Shajakhan 			result = true;
50b85c5734SMohammed Shafi Shajakhan 		else
51b85c5734SMohammed Shafi Shajakhan 			result = false;
52b85c5734SMohammed Shafi Shajakhan 		break;
53b85c5734SMohammed Shafi Shajakhan 	}
54b85c5734SMohammed Shafi Shajakhan 
55b85c5734SMohammed Shafi Shajakhan 	return result;
56b85c5734SMohammed Shafi Shajakhan }
57b85c5734SMohammed Shafi Shajakhan 
58ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
59ededf1f8SVasanthakumar Thiagarajan {
60ededf1f8SVasanthakumar Thiagarajan 	return sc->ps_enabled &&
61ededf1f8SVasanthakumar Thiagarajan 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
62ededf1f8SVasanthakumar Thiagarajan }
63ededf1f8SVasanthakumar Thiagarajan 
64203c4805SLuis R. Rodriguez /*
65203c4805SLuis R. Rodriguez  * Setup and link descriptors.
66203c4805SLuis R. Rodriguez  *
67203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
68203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
69203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
70203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
71203c4805SLuis R. Rodriguez  */
72203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
73203c4805SLuis R. Rodriguez {
74203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
75cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
76203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
77203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
78203c4805SLuis R. Rodriguez 
79203c4805SLuis R. Rodriguez 	ATH_RXBUF_RESET(bf);
80203c4805SLuis R. Rodriguez 
81203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
82203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
83203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
84203c4805SLuis R. Rodriguez 
85203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
86203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
879680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
88203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
89203c4805SLuis R. Rodriguez 
90cc861f74SLuis R. Rodriguez 	/*
91cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
92203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
93cc861f74SLuis R. Rodriguez 	 * to process
94cc861f74SLuis R. Rodriguez 	 */
95203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
96cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
97203c4805SLuis R. Rodriguez 			     0);
98203c4805SLuis R. Rodriguez 
99203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
100203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
101203c4805SLuis R. Rodriguez 	else
102203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
103203c4805SLuis R. Rodriguez 
104203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
105203c4805SLuis R. Rodriguez }
106203c4805SLuis R. Rodriguez 
107203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
108203c4805SLuis R. Rodriguez {
109203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
110203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
111203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
112203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
113203c4805SLuis R. Rodriguez }
114203c4805SLuis R. Rodriguez 
115203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
116203c4805SLuis R. Rodriguez {
117203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
1181510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
1191510718dSLuis R. Rodriguez 
120203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
121203c4805SLuis R. Rodriguez 
122203c4805SLuis R. Rodriguez 	/* configure rx filter */
123203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
124203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
125203c4805SLuis R. Rodriguez 
126203c4805SLuis R. Rodriguez 	/* configure bssid mask */
12713b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
128203c4805SLuis R. Rodriguez 
129203c4805SLuis R. Rodriguez 	/* configure operational mode */
130203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
131203c4805SLuis R. Rodriguez 
132203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
133203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
134203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
135203c4805SLuis R. Rodriguez }
136203c4805SLuis R. Rodriguez 
137b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
138b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
139b5c80475SFelix Fietkau {
140b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
141b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
142b5c80475SFelix Fietkau 	struct sk_buff *skb;
143b5c80475SFelix Fietkau 	struct ath_buf *bf;
144b5c80475SFelix Fietkau 
145b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
146b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
147b5c80475SFelix Fietkau 		return false;
148b5c80475SFelix Fietkau 
149b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
150b5c80475SFelix Fietkau 	list_del_init(&bf->list);
151b5c80475SFelix Fietkau 
152b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
153b5c80475SFelix Fietkau 
154b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
155b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
156b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
157b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
158b5c80475SFelix Fietkau 
159b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
160b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
161b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
162b5c80475SFelix Fietkau 
163b5c80475SFelix Fietkau 	return true;
164b5c80475SFelix Fietkau }
165b5c80475SFelix Fietkau 
166b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
167b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
168b5c80475SFelix Fietkau {
169b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
170b5c80475SFelix Fietkau 	u32 nbuf = 0;
171b5c80475SFelix Fietkau 
172b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
173226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
174b5c80475SFelix Fietkau 		return;
175b5c80475SFelix Fietkau 	}
176b5c80475SFelix Fietkau 
177b5c80475SFelix Fietkau 	while (!list_empty(&sc->rx.rxbuf)) {
178b5c80475SFelix Fietkau 		nbuf++;
179b5c80475SFelix Fietkau 
180b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
181b5c80475SFelix Fietkau 			break;
182b5c80475SFelix Fietkau 
183b5c80475SFelix Fietkau 		if (nbuf >= size)
184b5c80475SFelix Fietkau 			break;
185b5c80475SFelix Fietkau 	}
186b5c80475SFelix Fietkau }
187b5c80475SFelix Fietkau 
188b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
189b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
190b5c80475SFelix Fietkau {
191b5c80475SFelix Fietkau 	struct ath_buf *bf;
192b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
193b5c80475SFelix Fietkau 	struct sk_buff *skb;
194b5c80475SFelix Fietkau 
195b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
196b5c80475SFelix Fietkau 
197b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
198b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
199b5c80475SFelix Fietkau 		BUG_ON(!bf);
200b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
201b5c80475SFelix Fietkau 	}
202b5c80475SFelix Fietkau }
203b5c80475SFelix Fietkau 
204b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
205b5c80475SFelix Fietkau {
206b5c80475SFelix Fietkau 	struct ath_buf *bf;
207b5c80475SFelix Fietkau 
208b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
209b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
210b5c80475SFelix Fietkau 
211b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
212b5c80475SFelix Fietkau 		if (bf->bf_mpdu)
213b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
214b5c80475SFelix Fietkau 	}
215b5c80475SFelix Fietkau 
216b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
217b5c80475SFelix Fietkau 
218b5c80475SFelix Fietkau 	kfree(sc->rx.rx_bufptr);
219b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = NULL;
220b5c80475SFelix Fietkau }
221b5c80475SFelix Fietkau 
222b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
223b5c80475SFelix Fietkau {
224b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
225b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_buffers);
226b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
227b5c80475SFelix Fietkau }
228b5c80475SFelix Fietkau 
229b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
230b5c80475SFelix Fietkau {
231b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
232b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
233b5c80475SFelix Fietkau 	struct sk_buff *skb;
234b5c80475SFelix Fietkau 	struct ath_buf *bf;
235b5c80475SFelix Fietkau 	int error = 0, i;
236b5c80475SFelix Fietkau 	u32 size;
237b5c80475SFelix Fietkau 
238b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
239b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
240b5c80475SFelix Fietkau 
241b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
242b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
243b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
244b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
245b5c80475SFelix Fietkau 
246b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
247b5c80475SFelix Fietkau 	bf = kzalloc(size, GFP_KERNEL);
248b5c80475SFelix Fietkau 	if (!bf)
249b5c80475SFelix Fietkau 		return -ENOMEM;
250b5c80475SFelix Fietkau 
251b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
252b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = bf;
253b5c80475SFelix Fietkau 
254b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
255b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
256b5c80475SFelix Fietkau 		if (!skb) {
257b5c80475SFelix Fietkau 			error = -ENOMEM;
258b5c80475SFelix Fietkau 			goto rx_init_fail;
259b5c80475SFelix Fietkau 		}
260b5c80475SFelix Fietkau 
261b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
262b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
263b5c80475SFelix Fietkau 
264b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
265b5c80475SFelix Fietkau 						 common->rx_bufsize,
266b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
267b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
268b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
269b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
270b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2716cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2723800276aSJoe Perches 				ath_err(common,
273b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
274b5c80475SFelix Fietkau 				error = -ENOMEM;
275b5c80475SFelix Fietkau 				goto rx_init_fail;
276b5c80475SFelix Fietkau 		}
277b5c80475SFelix Fietkau 
278b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
279b5c80475SFelix Fietkau 	}
280b5c80475SFelix Fietkau 
281b5c80475SFelix Fietkau 	return 0;
282b5c80475SFelix Fietkau 
283b5c80475SFelix Fietkau rx_init_fail:
284b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
285b5c80475SFelix Fietkau 	return error;
286b5c80475SFelix Fietkau }
287b5c80475SFelix Fietkau 
288b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
289b5c80475SFelix Fietkau {
290b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
291b5c80475SFelix Fietkau 
292b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
293b5c80475SFelix Fietkau 
294b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
295b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
296b5c80475SFelix Fietkau 
297b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
298b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
299b5c80475SFelix Fietkau 
300b5c80475SFelix Fietkau 	ath_opmode_init(sc);
301b5c80475SFelix Fietkau 
30248a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
3037583c550SLuis R. Rodriguez 
3047583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
305b5c80475SFelix Fietkau }
306b5c80475SFelix Fietkau 
307b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
308b5c80475SFelix Fietkau {
309b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
310b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
311b5c80475SFelix Fietkau }
312b5c80475SFelix Fietkau 
313203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
314203c4805SLuis R. Rodriguez {
31527c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
316203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
317203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
318203c4805SLuis R. Rodriguez 	int error = 0;
319203c4805SLuis R. Rodriguez 
3204bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
321203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
322203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxbuflock);
323203c4805SLuis R. Rodriguez 
3240d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
3250d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
3260d95521eSFelix Fietkau 
327b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
328b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
329b5c80475SFelix Fietkau 	} else {
330226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
331cc861f74SLuis R. Rodriguez 			common->cachelsz, common->rx_bufsize);
332203c4805SLuis R. Rodriguez 
333203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
334203c4805SLuis R. Rodriguez 
335203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
3364adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
337203c4805SLuis R. Rodriguez 		if (error != 0) {
3383800276aSJoe Perches 			ath_err(common,
339b5c80475SFelix Fietkau 				"failed to allocate rx descriptors: %d\n",
340b5c80475SFelix Fietkau 				error);
341203c4805SLuis R. Rodriguez 			goto err;
342203c4805SLuis R. Rodriguez 		}
343203c4805SLuis R. Rodriguez 
344203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
346b5c80475SFelix Fietkau 					      GFP_KERNEL);
347203c4805SLuis R. Rodriguez 			if (skb == NULL) {
348203c4805SLuis R. Rodriguez 				error = -ENOMEM;
349203c4805SLuis R. Rodriguez 				goto err;
350203c4805SLuis R. Rodriguez 			}
351203c4805SLuis R. Rodriguez 
352203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
353203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
354cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
355203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
356203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
357203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
358203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
359203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
3606cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3613800276aSJoe Perches 				ath_err(common,
362203c4805SLuis R. Rodriguez 					"dma_mapping_error() on RX init\n");
363203c4805SLuis R. Rodriguez 				error = -ENOMEM;
364203c4805SLuis R. Rodriguez 				goto err;
365203c4805SLuis R. Rodriguez 			}
366203c4805SLuis R. Rodriguez 		}
367203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
368b5c80475SFelix Fietkau 	}
369203c4805SLuis R. Rodriguez 
370203c4805SLuis R. Rodriguez err:
371203c4805SLuis R. Rodriguez 	if (error)
372203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
373203c4805SLuis R. Rodriguez 
374203c4805SLuis R. Rodriguez 	return error;
375203c4805SLuis R. Rodriguez }
376203c4805SLuis R. Rodriguez 
377203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
378203c4805SLuis R. Rodriguez {
379cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
380cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
381203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
382203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
383203c4805SLuis R. Rodriguez 
384b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
385b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
386b5c80475SFelix Fietkau 		return;
387b5c80475SFelix Fietkau 	} else {
388203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
389203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
390203c4805SLuis R. Rodriguez 			if (skb) {
391203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
392b5c80475SFelix Fietkau 						common->rx_bufsize,
393b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
394203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
3956cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3966cf9e995SBen Greear 				bf->bf_mpdu = NULL;
397203c4805SLuis R. Rodriguez 			}
398203c4805SLuis R. Rodriguez 		}
399203c4805SLuis R. Rodriguez 
400203c4805SLuis R. Rodriguez 		if (sc->rx.rxdma.dd_desc_len != 0)
401203c4805SLuis R. Rodriguez 			ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
402203c4805SLuis R. Rodriguez 	}
403b5c80475SFelix Fietkau }
404203c4805SLuis R. Rodriguez 
405203c4805SLuis R. Rodriguez /*
406203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
407203c4805SLuis R. Rodriguez  * operating mode and state:
408203c4805SLuis R. Rodriguez  *
409203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
410203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
411203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
412203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
413203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
414203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
415203c4805SLuis R. Rodriguez  * o accept beacons:
416203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
417203c4805SLuis R. Rodriguez  *     node table entries for peers,
418203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
419203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
420203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
421203c4805SLuis R. Rodriguez  *   - when scanning
422203c4805SLuis R. Rodriguez  */
423203c4805SLuis R. Rodriguez 
424203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
425203c4805SLuis R. Rodriguez {
426203c4805SLuis R. Rodriguez #define	RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
427203c4805SLuis R. Rodriguez 
428203c4805SLuis R. Rodriguez 	u32 rfilt;
429203c4805SLuis R. Rodriguez 
430203c4805SLuis R. Rodriguez 	rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
431203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
432203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
433203c4805SLuis R. Rodriguez 
4349c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
435203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
436203c4805SLuis R. Rodriguez 
437203c4805SLuis R. Rodriguez 	/*
438203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
439203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
440203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
441203c4805SLuis R. Rodriguez 	 */
4422e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
443203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
444203c4805SLuis R. Rodriguez 
445203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
446203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
447203c4805SLuis R. Rodriguez 
448203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
449cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
450203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
451203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
452203c4805SLuis R. Rodriguez 	else
453203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
454203c4805SLuis R. Rodriguez 
455264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
45666afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
457203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
458203c4805SLuis R. Rodriguez 
4597ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4607ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4617ea310beSSujith 
4627545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
4635eb6ba83SJavier Cardona 		/* The following may also be needed for other older chips */
4645eb6ba83SJavier Cardona 		if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
4655eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
466203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
467203c4805SLuis R. Rodriguez 	}
468203c4805SLuis R. Rodriguez 
469203c4805SLuis R. Rodriguez 	return rfilt;
470203c4805SLuis R. Rodriguez 
471203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE
472203c4805SLuis R. Rodriguez }
473203c4805SLuis R. Rodriguez 
474203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
475203c4805SLuis R. Rodriguez {
476203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
477203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
478203c4805SLuis R. Rodriguez 
479b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
480b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
481b5c80475SFelix Fietkau 		return 0;
482b5c80475SFelix Fietkau 	}
483b5c80475SFelix Fietkau 
484203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
485203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
486203c4805SLuis R. Rodriguez 		goto start_recv;
487203c4805SLuis R. Rodriguez 
488203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
489203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
490203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
491203c4805SLuis R. Rodriguez 	}
492203c4805SLuis R. Rodriguez 
493203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
494203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
495203c4805SLuis R. Rodriguez 		goto start_recv;
496203c4805SLuis R. Rodriguez 
497203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
498203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
499203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
500203c4805SLuis R. Rodriguez 
501203c4805SLuis R. Rodriguez start_recv:
502203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
50348a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
504203c4805SLuis R. Rodriguez 
5057583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
5067583c550SLuis R. Rodriguez 
507203c4805SLuis R. Rodriguez 	return 0;
508203c4805SLuis R. Rodriguez }
509203c4805SLuis R. Rodriguez 
510203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
511203c4805SLuis R. Rodriguez {
512203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
5135882da02SFelix Fietkau 	bool stopped, reset = false;
514203c4805SLuis R. Rodriguez 
5151e450285SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
516d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
517203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
5185882da02SFelix Fietkau 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
519b5c80475SFelix Fietkau 
520b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
521b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
522b5c80475SFelix Fietkau 	else
523203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
5241e450285SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
525203c4805SLuis R. Rodriguez 
526d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
527d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
528d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
529d7fd1b50SBen Greear 			"Could not stop RX, we could be "
53078a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
531d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
532d7fd1b50SBen Greear 	}
5332232d31bSFelix Fietkau 	return stopped && !reset;
534203c4805SLuis R. Rodriguez }
535203c4805SLuis R. Rodriguez 
536203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc)
537203c4805SLuis R. Rodriguez {
538203c4805SLuis R. Rodriguez 	sc->sc_flags |= SC_OP_RXFLUSH;
539b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
540b5c80475SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
541b5c80475SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
542203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
543203c4805SLuis R. Rodriguez }
544203c4805SLuis R. Rodriguez 
545cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
546cc65965cSJouni Malinen {
547cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
548cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
549cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
550cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
551cc65965cSJouni Malinen 
552cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
553cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
554cc65965cSJouni Malinen 	end = skb->data + skb->len;
555cc65965cSJouni Malinen 
556cc65965cSJouni Malinen 	while (pos + 2 < end) {
557cc65965cSJouni Malinen 		id = *pos++;
558cc65965cSJouni Malinen 		elen = *pos++;
559cc65965cSJouni Malinen 		if (pos + elen > end)
560cc65965cSJouni Malinen 			break;
561cc65965cSJouni Malinen 
562cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
563cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
564cc65965cSJouni Malinen 				break;
565cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
566cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
567cc65965cSJouni Malinen 				break;
568cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
569cc65965cSJouni Malinen 		}
570cc65965cSJouni Malinen 
571cc65965cSJouni Malinen 		pos += elen;
572cc65965cSJouni Malinen 	}
573cc65965cSJouni Malinen 
574cc65965cSJouni Malinen 	return false;
575cc65965cSJouni Malinen }
576cc65965cSJouni Malinen 
577cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
578cc65965cSJouni Malinen {
579cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
5801510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
581cc65965cSJouni Malinen 
582cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
583cc65965cSJouni Malinen 		return;
584cc65965cSJouni Malinen 
585cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
5864801416cSBen Greear 	if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
5874801416cSBen Greear 		/* TODO:  This doesn't work well if you have stations
5884801416cSBen Greear 		 * associated to two different APs because curbssid
5894801416cSBen Greear 		 * is just the last AP that any of the stations associated
5904801416cSBen Greear 		 * with.
5914801416cSBen Greear 		 */
592cc65965cSJouni Malinen 		return; /* not from our current AP */
5934801416cSBen Greear 	}
594cc65965cSJouni Malinen 
5951b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
596293dc5dfSGabor Juhos 
5971b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5981b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
599226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
600226afe68SJoe Perches 			"Reconfigure Beacon timers based on timestamp from the AP\n");
60199e4d43aSRajkumar Manoharan 		ath_set_beacon(sc);
602deb75188SRajkumar Manoharan 		sc->ps_flags &= ~PS_TSFOOR_SYNC;
603ccdfeab6SJouni Malinen 	}
604ccdfeab6SJouni Malinen 
605cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
606cc65965cSJouni Malinen 		/*
607cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
60858f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
60958f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
61058f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
61158f5fffdSGabor Juhos 		 * so we are waiting for it as well.
612cc65965cSJouni Malinen 		 */
613226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
614226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
6151b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
616cc65965cSJouni Malinen 		return;
617cc65965cSJouni Malinen 	}
618cc65965cSJouni Malinen 
6191b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
620cc65965cSJouni Malinen 		/*
621cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
622cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
623cc65965cSJouni Malinen 		 * been delivered.
624cc65965cSJouni Malinen 		 */
6251b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
626226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
627c46917bbSLuis R. Rodriguez 			"PS wait for CAB frames timed out\n");
628cc65965cSJouni Malinen 	}
629cc65965cSJouni Malinen }
630cc65965cSJouni Malinen 
631cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
632cc65965cSJouni Malinen {
633cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
634c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
635cc65965cSJouni Malinen 
636cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
637cc65965cSJouni Malinen 
638cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
639ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
640ededf1f8SVasanthakumar Thiagarajan 	    && ieee80211_is_beacon(hdr->frame_control))
641cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
6421b04b930SSujith 	else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
643cc65965cSJouni Malinen 		 (ieee80211_is_data(hdr->frame_control) ||
644cc65965cSJouni Malinen 		  ieee80211_is_action(hdr->frame_control)) &&
645cc65965cSJouni Malinen 		 is_multicast_ether_addr(hdr->addr1) &&
646cc65965cSJouni Malinen 		 !ieee80211_has_moredata(hdr->frame_control)) {
647cc65965cSJouni Malinen 		/*
648cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
649cc65965cSJouni Malinen 		 * point.
650cc65965cSJouni Malinen 		 */
6513fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
652226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
653c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
6541b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6559a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
6569a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
6571b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
658226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
659226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
6601b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
6611b04b930SSujith 					PS_WAIT_FOR_CAB |
6621b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6631b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
664cc65965cSJouni Malinen 	}
665cc65965cSJouni Malinen }
666cc65965cSJouni Malinen 
667b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
668b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
669203c4805SLuis R. Rodriguez {
670b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
671203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
67227c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
673b5c80475SFelix Fietkau 	struct sk_buff *skb;
674b5c80475SFelix Fietkau 	struct ath_buf *bf;
675b5c80475SFelix Fietkau 	int ret;
676203c4805SLuis R. Rodriguez 
677b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
678b5c80475SFelix Fietkau 	if (!skb)
679b5c80475SFelix Fietkau 		return false;
680203c4805SLuis R. Rodriguez 
681b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
682b5c80475SFelix Fietkau 	BUG_ON(!bf);
683b5c80475SFelix Fietkau 
684ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
685b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
686b5c80475SFelix Fietkau 
687b5c80475SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
688ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
689ce9426d1SMing Lei 		/*let device gain the buffer again*/
690ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
691ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
692b5c80475SFelix Fietkau 		return false;
693ce9426d1SMing Lei 	}
694b5c80475SFelix Fietkau 
695b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
696b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
697b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
698b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
699b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
700b5c80475SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
701b5c80475SFelix Fietkau 		if (!skb)
702b5c80475SFelix Fietkau 			return true;
703b5c80475SFelix Fietkau 
704b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
705b5c80475SFelix Fietkau 		BUG_ON(!bf);
706b5c80475SFelix Fietkau 
707b5c80475SFelix Fietkau 		__skb_unlink(skb, &rx_edma->rx_fifo);
708b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
709b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
710083e3e8dSVasanthakumar Thiagarajan 		return true;
711b5c80475SFelix Fietkau 	}
712b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_buffers, skb);
713b5c80475SFelix Fietkau 
714b5c80475SFelix Fietkau 	return true;
715b5c80475SFelix Fietkau }
716b5c80475SFelix Fietkau 
717b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
718b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
719b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
720b5c80475SFelix Fietkau {
721b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
722b5c80475SFelix Fietkau 	struct sk_buff *skb;
723b5c80475SFelix Fietkau 	struct ath_buf *bf;
724b5c80475SFelix Fietkau 
725b5c80475SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype));
726b5c80475SFelix Fietkau 	skb = __skb_dequeue(&rx_edma->rx_buffers);
727b5c80475SFelix Fietkau 	if (!skb)
728b5c80475SFelix Fietkau 		return NULL;
729b5c80475SFelix Fietkau 
730b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
731b5c80475SFelix Fietkau 	ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
732b5c80475SFelix Fietkau 	return bf;
733b5c80475SFelix Fietkau }
734b5c80475SFelix Fietkau 
735b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
736b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
737b5c80475SFelix Fietkau {
738b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
739b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
740b5c80475SFelix Fietkau 	struct ath_desc *ds;
741b5c80475SFelix Fietkau 	struct ath_buf *bf;
742b5c80475SFelix Fietkau 	int ret;
743203c4805SLuis R. Rodriguez 
744203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
745203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
746b5c80475SFelix Fietkau 		return NULL;
747203c4805SLuis R. Rodriguez 	}
748203c4805SLuis R. Rodriguez 
749203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
750203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
751203c4805SLuis R. Rodriguez 
752203c4805SLuis R. Rodriguez 	/*
753203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
754203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
755203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
756203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
757203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
758203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
759203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
760203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
761203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
762203c4805SLuis R. Rodriguez 	 */
763b5c80475SFelix Fietkau 	ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
764b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
76529bffa96SFelix Fietkau 		struct ath_rx_status trs;
766203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
767203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
768203c4805SLuis R. Rodriguez 
76929bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
770203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
771203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
772b5c80475SFelix Fietkau 			return NULL;
773203c4805SLuis R. Rodriguez 		}
774203c4805SLuis R. Rodriguez 
775203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
776203c4805SLuis R. Rodriguez 
777203c4805SLuis R. Rodriguez 		/*
778203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
779203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
780203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
781203c4805SLuis R. Rodriguez 		 * set or not.
782203c4805SLuis R. Rodriguez 		 *
783203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
784203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
785203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
786203c4805SLuis R. Rodriguez 		 */
787203c4805SLuis R. Rodriguez 
788203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
789b5c80475SFelix Fietkau 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
790b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
791b5c80475SFelix Fietkau 			return NULL;
792203c4805SLuis R. Rodriguez 	}
793203c4805SLuis R. Rodriguez 
794b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
795b5c80475SFelix Fietkau 		return bf;
796203c4805SLuis R. Rodriguez 
797203c4805SLuis R. Rodriguez 	/*
798203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
799203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
800203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
801203c4805SLuis R. Rodriguez 	 */
802ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
803cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
804203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
805203c4805SLuis R. Rodriguez 
806b5c80475SFelix Fietkau 	return bf;
807b5c80475SFelix Fietkau }
808b5c80475SFelix Fietkau 
809d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
810d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
8119f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
812d435700fSSujith 			    struct ieee80211_rx_status *rxs,
813d435700fSSujith 			    struct ath_rx_status *rx_stats,
814d435700fSSujith 			    bool *decrypt_error)
815d435700fSSujith {
81638852b20SSenthil Balasubramanian #define is_mc_or_valid_tkip_keyix ((is_mc ||			\
81738852b20SSenthil Balasubramanian 		(rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
81838852b20SSenthil Balasubramanian 		test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
81938852b20SSenthil Balasubramanian 
820d435700fSSujith 	struct ath_hw *ah = common->ah;
821d435700fSSujith 	__le16 fc;
822b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
823d435700fSSujith 
824d435700fSSujith 	fc = hdr->frame_control;
825d435700fSSujith 
826d435700fSSujith 	if (!rx_stats->rs_datalen)
827d435700fSSujith 		return false;
828d435700fSSujith         /*
829d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
830d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
831d435700fSSujith          * those frames.
832d435700fSSujith          */
833b7b1b512SVasanthakumar Thiagarajan 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
834d435700fSSujith 		return false;
835d435700fSSujith 
8360d95521eSFelix Fietkau 	/* Only use error bits from the last fragment */
837d435700fSSujith 	if (rx_stats->rs_more)
8380d95521eSFelix Fietkau 		return true;
839d435700fSSujith 
840d435700fSSujith 	/*
841d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
842d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
843d435700fSSujith 	 * rs_more will be false at the last element of the chained
844d435700fSSujith 	 * descriptors.
845d435700fSSujith 	 */
846d435700fSSujith 	if (rx_stats->rs_status != 0) {
847d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_CRC)
848d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
849d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
850d435700fSSujith 			return false;
851d435700fSSujith 
852d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
853d435700fSSujith 			*decrypt_error = true;
854d435700fSSujith 		} else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
85538852b20SSenthil Balasubramanian 			bool is_mc;
856d435700fSSujith 			/*
85756363ddeSFelix Fietkau 			 * The MIC error bit is only valid if the frame
85856363ddeSFelix Fietkau 			 * is not a control frame or fragment, and it was
85956363ddeSFelix Fietkau 			 * decrypted using a valid TKIP key.
860d435700fSSujith 			 */
86138852b20SSenthil Balasubramanian 			is_mc = !!is_multicast_ether_addr(hdr->addr1);
86238852b20SSenthil Balasubramanian 
86356363ddeSFelix Fietkau 			if (!ieee80211_is_ctl(fc) &&
86456363ddeSFelix Fietkau 			    !ieee80211_has_morefrags(fc) &&
86556363ddeSFelix Fietkau 			    !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
86638852b20SSenthil Balasubramanian 			    is_mc_or_valid_tkip_keyix)
867d435700fSSujith 				rxs->flag |= RX_FLAG_MMIC_ERROR;
86856363ddeSFelix Fietkau 			else
86956363ddeSFelix Fietkau 				rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
870d435700fSSujith 		}
871d435700fSSujith 		/*
872d435700fSSujith 		 * Reject error frames with the exception of
873d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
874d435700fSSujith 		 * we also ignore the CRC error.
875d435700fSSujith 		 */
8765f841b41SRajkumar Manoharan 		if (ah->is_monitoring) {
877d435700fSSujith 			if (rx_stats->rs_status &
878d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
879d435700fSSujith 			      ATH9K_RXERR_CRC))
880d435700fSSujith 				return false;
881d435700fSSujith 		} else {
882d435700fSSujith 			if (rx_stats->rs_status &
883d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
884d435700fSSujith 				return false;
885d435700fSSujith 			}
886d435700fSSujith 		}
887d435700fSSujith 	}
888d435700fSSujith 	return true;
889d435700fSSujith }
890d435700fSSujith 
891d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
892d435700fSSujith 			      struct ieee80211_hw *hw,
893d435700fSSujith 			      struct ath_rx_status *rx_stats,
8949f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
895d435700fSSujith {
896d435700fSSujith 	struct ieee80211_supported_band *sband;
897d435700fSSujith 	enum ieee80211_band band;
898d435700fSSujith 	unsigned int i = 0;
899d435700fSSujith 
900d435700fSSujith 	band = hw->conf.channel->band;
901d435700fSSujith 	sband = hw->wiphy->bands[band];
902d435700fSSujith 
903d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
904d435700fSSujith 		/* HT rate */
905d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
906d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
907d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
908d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
909d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
910d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
911d435700fSSujith 		return 0;
912d435700fSSujith 	}
913d435700fSSujith 
914d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
915d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
916d435700fSSujith 			rxs->rate_idx = i;
917d435700fSSujith 			return 0;
918d435700fSSujith 		}
919d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
920d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
921d435700fSSujith 			rxs->rate_idx = i;
922d435700fSSujith 			return 0;
923d435700fSSujith 		}
924d435700fSSujith 	}
925d435700fSSujith 
926d435700fSSujith 	/*
927d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
928d435700fSSujith 	 * because hardware has already validated this frame as OK.
929d435700fSSujith 	 */
930226afe68SJoe Perches 	ath_dbg(common, ATH_DBG_XMIT,
931226afe68SJoe Perches 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
932226afe68SJoe Perches 		rx_stats->rs_rate);
933d435700fSSujith 
934d435700fSSujith 	return -EINVAL;
935d435700fSSujith }
936d435700fSSujith 
937d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
938d435700fSSujith 			       struct ieee80211_hw *hw,
9399f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
940d435700fSSujith 			       struct ath_rx_status *rx_stats)
941d435700fSSujith {
9429ac58615SFelix Fietkau 	struct ath_softc *sc = hw->priv;
943d435700fSSujith 	struct ath_hw *ah = common->ah;
9449fa23e17SFelix Fietkau 	int last_rssi;
945d435700fSSujith 	__le16 fc;
946d435700fSSujith 
9472b892a98SRajkumar Manoharan 	if ((ah->opmode != NL80211_IFTYPE_STATION) &&
9482b892a98SRajkumar Manoharan 	    (ah->opmode != NL80211_IFTYPE_ADHOC))
9499fa23e17SFelix Fietkau 		return;
9509fa23e17SFelix Fietkau 
951d435700fSSujith 	fc = hdr->frame_control;
9529fa23e17SFelix Fietkau 	if (!ieee80211_is_beacon(fc) ||
9534801416cSBen Greear 	    compare_ether_addr(hdr->addr3, common->curbssid)) {
9544801416cSBen Greear 		/* TODO:  This doesn't work well if you have stations
9554801416cSBen Greear 		 * associated to two different APs because curbssid
9564801416cSBen Greear 		 * is just the last AP that any of the stations associated
9574801416cSBen Greear 		 * with.
9584801416cSBen Greear 		 */
9599fa23e17SFelix Fietkau 		return;
9604801416cSBen Greear 	}
961d435700fSSujith 
9629fa23e17SFelix Fietkau 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9639ac58615SFelix Fietkau 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
964686b9cb9SBen Greear 
9659ac58615SFelix Fietkau 	last_rssi = sc->last_rssi;
966d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
967d435700fSSujith 		rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
968d435700fSSujith 					      ATH_RSSI_EP_MULTIPLIER);
969d435700fSSujith 	if (rx_stats->rs_rssi < 0)
970d435700fSSujith 		rx_stats->rs_rssi = 0;
971d435700fSSujith 
972d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
973d435700fSSujith 	ah->stats.avgbrssi = rx_stats->rs_rssi;
974d435700fSSujith }
975d435700fSSujith 
976d435700fSSujith /*
977d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
978d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
979d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
980d435700fSSujith  */
981d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
982d435700fSSujith 				   struct ieee80211_hw *hw,
9839f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
984d435700fSSujith 				   struct ath_rx_status *rx_stats,
985d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
986d435700fSSujith 				   bool *decrypt_error)
987d435700fSSujith {
988d435700fSSujith 	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
989d435700fSSujith 
990d435700fSSujith 	/*
991d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
992d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
993d435700fSSujith 	 */
9949f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
995d435700fSSujith 		return -EINVAL;
996d435700fSSujith 
9970d95521eSFelix Fietkau 	/* Only use status info from the last fragment */
9980d95521eSFelix Fietkau 	if (rx_stats->rs_more)
9990d95521eSFelix Fietkau 		return 0;
10000d95521eSFelix Fietkau 
10019f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
1002d435700fSSujith 
10039f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1004d435700fSSujith 		return -EINVAL;
1005d435700fSSujith 
1006d435700fSSujith 	rx_status->band = hw->conf.channel->band;
1007d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
1008d435700fSSujith 	rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1009d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
10106ebacbb7SJohannes Berg 	rx_status->flag |= RX_FLAG_MACTIME_MPDU;
1011d435700fSSujith 
1012d435700fSSujith 	return 0;
1013d435700fSSujith }
1014d435700fSSujith 
1015d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
1016d435700fSSujith 				     struct sk_buff *skb,
1017d435700fSSujith 				     struct ath_rx_status *rx_stats,
1018d435700fSSujith 				     struct ieee80211_rx_status *rxs,
1019d435700fSSujith 				     bool decrypt_error)
1020d435700fSSujith {
1021d435700fSSujith 	struct ath_hw *ah = common->ah;
1022d435700fSSujith 	struct ieee80211_hdr *hdr;
1023d435700fSSujith 	int hdrlen, padpos, padsize;
1024d435700fSSujith 	u8 keyix;
1025d435700fSSujith 	__le16 fc;
1026d435700fSSujith 
1027d435700fSSujith 	/* see if any padding is done by the hw and remove it */
1028d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1029d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1030d435700fSSujith 	fc = hdr->frame_control;
1031d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
1032d435700fSSujith 
1033d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1034d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1035d435700fSSujith 	 * padsize would take into account odd header lengths:
1036d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1037d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1038d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1039d435700fSSujith 	 * not try to remove padding from short control frames that do
1040d435700fSSujith 	 * not have payload. */
1041d435700fSSujith 	padsize = padpos & 3;
1042d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1043d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1044d435700fSSujith 		skb_pull(skb, padsize);
1045d435700fSSujith 	}
1046d435700fSSujith 
1047d435700fSSujith 	keyix = rx_stats->rs_keyix;
1048d435700fSSujith 
1049d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1050d435700fSSujith 	    ieee80211_has_protected(fc)) {
1051d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1052d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1053d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1054d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1055d435700fSSujith 
1056d435700fSSujith 		if (test_bit(keyix, common->keymap))
1057d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1058d435700fSSujith 	}
1059d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1060d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1061d435700fSSujith 	    ieee80211_is_mgmt(fc))
1062d435700fSSujith 		/* Use software decrypt for management frames. */
1063d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1064d435700fSSujith }
1065b5c80475SFelix Fietkau 
1066102885a5SVasanthakumar Thiagarajan static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1067102885a5SVasanthakumar Thiagarajan 				      struct ath_hw_antcomb_conf ant_conf,
1068102885a5SVasanthakumar Thiagarajan 				      int main_rssi_avg)
1069102885a5SVasanthakumar Thiagarajan {
1070102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt = 0;
1071102885a5SVasanthakumar Thiagarajan 
1072102885a5SVasanthakumar Thiagarajan 	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1073102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna2 = main_rssi_avg;
1074102885a5SVasanthakumar Thiagarajan 	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1075102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna1 = main_rssi_avg;
1076102885a5SVasanthakumar Thiagarajan 
1077102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1078102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1079102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1080102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1081102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1082102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1083102885a5SVasanthakumar Thiagarajan 		break;
1084102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1085102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1086102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1087102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1088102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1089102885a5SVasanthakumar Thiagarajan 		break;
1090102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1091102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1092102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1093102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1094102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1095102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1096102885a5SVasanthakumar Thiagarajan 		break;
1097102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1098102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1099102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1100102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1101102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1102102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1103102885a5SVasanthakumar Thiagarajan 		break;
1104102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1105102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1106102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1107102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1108102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1109102885a5SVasanthakumar Thiagarajan 		break;
1110102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1111102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1112102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1113102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1114102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1115102885a5SVasanthakumar Thiagarajan 		break;
1116102885a5SVasanthakumar Thiagarajan 	default:
1117102885a5SVasanthakumar Thiagarajan 		break;
1118102885a5SVasanthakumar Thiagarajan 	}
1119102885a5SVasanthakumar Thiagarajan }
1120102885a5SVasanthakumar Thiagarajan 
1121102885a5SVasanthakumar Thiagarajan static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1122102885a5SVasanthakumar Thiagarajan 				struct ath_hw_antcomb_conf *div_ant_conf,
1123102885a5SVasanthakumar Thiagarajan 				int main_rssi_avg, int alt_rssi_avg,
1124102885a5SVasanthakumar Thiagarajan 				int alt_ratio)
1125102885a5SVasanthakumar Thiagarajan {
1126102885a5SVasanthakumar Thiagarajan 	/* alt_good */
1127102885a5SVasanthakumar Thiagarajan 	switch (antcomb->quick_scan_cnt) {
1128102885a5SVasanthakumar Thiagarajan 	case 0:
1129102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1130102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1131102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1132102885a5SVasanthakumar Thiagarajan 		break;
1133102885a5SVasanthakumar Thiagarajan 	case 1:
1134102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1135102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1136102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1137102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1138102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_second = alt_rssi_avg;
1139102885a5SVasanthakumar Thiagarajan 
1140102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1141102885a5SVasanthakumar Thiagarajan 			/* main is LNA1 */
1142102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1143102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1144102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1145102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1146102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1147102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1148102885a5SVasanthakumar Thiagarajan 			else
1149102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1150102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1151102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1152102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1153102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1154102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1155102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1156102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1157102885a5SVasanthakumar Thiagarajan 			else
1158102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1159102885a5SVasanthakumar Thiagarajan 		} else {
1160102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1161102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1162102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1163102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1164102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1165102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1166102885a5SVasanthakumar Thiagarajan 			else
1167102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1168102885a5SVasanthakumar Thiagarajan 		}
1169102885a5SVasanthakumar Thiagarajan 		break;
1170102885a5SVasanthakumar Thiagarajan 	case 2:
1171102885a5SVasanthakumar Thiagarajan 		antcomb->alt_good = false;
1172102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = false;
1173102885a5SVasanthakumar Thiagarajan 		antcomb->scan = false;
1174102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1175102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_third = alt_rssi_avg;
1176102885a5SVasanthakumar Thiagarajan 
1177102885a5SVasanthakumar Thiagarajan 		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1178102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1179102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1180102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA2)
1181102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1182102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1183102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1184102885a5SVasanthakumar Thiagarajan 			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1185102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna2 = main_rssi_avg;
1186102885a5SVasanthakumar Thiagarajan 			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1187102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna1 = main_rssi_avg;
1188102885a5SVasanthakumar Thiagarajan 		}
1189102885a5SVasanthakumar Thiagarajan 
1190102885a5SVasanthakumar Thiagarajan 		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1191102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1192102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1193102885a5SVasanthakumar Thiagarajan 		else
1194102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1195102885a5SVasanthakumar Thiagarajan 
1196102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1197102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1198102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1199102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1200102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1201102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1202102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1203102885a5SVasanthakumar Thiagarajan 			else
1204102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1205102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1206102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1207102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1208102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1209102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1210102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1211102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1212102885a5SVasanthakumar Thiagarajan 			else
1213102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1214102885a5SVasanthakumar Thiagarajan 		} else {
1215102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1216102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1217102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1218102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1219102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1220102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1221102885a5SVasanthakumar Thiagarajan 			else
1222102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1223102885a5SVasanthakumar Thiagarajan 		}
1224102885a5SVasanthakumar Thiagarajan 
1225102885a5SVasanthakumar Thiagarajan 		/* set alt to the conf with maximun ratio */
1226102885a5SVasanthakumar Thiagarajan 		if (antcomb->first_ratio && antcomb->second_ratio) {
1227102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_second > antcomb->rssi_third) {
1228102885a5SVasanthakumar Thiagarajan 				/* first alt*/
1229102885a5SVasanthakumar Thiagarajan 				if ((antcomb->first_quick_scan_conf ==
1230102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA1) ||
1231102885a5SVasanthakumar Thiagarajan 				    (antcomb->first_quick_scan_conf ==
1232102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2))
1233102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2*/
1234102885a5SVasanthakumar Thiagarajan 					if (div_ant_conf->main_lna_conf ==
1235102885a5SVasanthakumar Thiagarajan 					    ATH_ANT_DIV_COMB_LNA2)
1236102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1237102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1238102885a5SVasanthakumar Thiagarajan 					else
1239102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1240102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1241102885a5SVasanthakumar Thiagarajan 				else
1242102885a5SVasanthakumar Thiagarajan 					/* Set alt to A+B or A-B */
1243102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1244102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1245102885a5SVasanthakumar Thiagarajan 			} else if ((antcomb->second_quick_scan_conf ==
1246102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA1) ||
1247102885a5SVasanthakumar Thiagarajan 				   (antcomb->second_quick_scan_conf ==
1248102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA2)) {
1249102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1250102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1251102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1252102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1253102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1254102885a5SVasanthakumar Thiagarajan 				else
1255102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1256102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1257102885a5SVasanthakumar Thiagarajan 			} else {
1258102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1259102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1260102885a5SVasanthakumar Thiagarajan 					antcomb->second_quick_scan_conf;
1261102885a5SVasanthakumar Thiagarajan 			}
1262102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->first_ratio) {
1263102885a5SVasanthakumar Thiagarajan 			/* first alt */
1264102885a5SVasanthakumar Thiagarajan 			if ((antcomb->first_quick_scan_conf ==
1265102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1266102885a5SVasanthakumar Thiagarajan 			    (antcomb->first_quick_scan_conf ==
1267102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1268102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2 */
1269102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1270102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1271102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1272102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1273102885a5SVasanthakumar Thiagarajan 				else
1274102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1275102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1276102885a5SVasanthakumar Thiagarajan 			else
1277102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1278102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1279102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1280102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->second_ratio) {
1281102885a5SVasanthakumar Thiagarajan 				/* second alt */
1282102885a5SVasanthakumar Thiagarajan 			if ((antcomb->second_quick_scan_conf ==
1283102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1284102885a5SVasanthakumar Thiagarajan 			    (antcomb->second_quick_scan_conf ==
1285102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1286102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1287102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1288102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1289102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1290102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1291102885a5SVasanthakumar Thiagarajan 				else
1292102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1293102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1294102885a5SVasanthakumar Thiagarajan 			else
1295102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1296102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1297102885a5SVasanthakumar Thiagarajan 						antcomb->second_quick_scan_conf;
1298102885a5SVasanthakumar Thiagarajan 		} else {
1299102885a5SVasanthakumar Thiagarajan 			/* main is largest */
1300102885a5SVasanthakumar Thiagarajan 			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1301102885a5SVasanthakumar Thiagarajan 			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1302102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1303102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1304102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1305102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1306102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1307102885a5SVasanthakumar Thiagarajan 				else
1308102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1309102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1310102885a5SVasanthakumar Thiagarajan 			else
1311102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1312102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf = antcomb->main_conf;
1313102885a5SVasanthakumar Thiagarajan 		}
1314102885a5SVasanthakumar Thiagarajan 		break;
1315102885a5SVasanthakumar Thiagarajan 	default:
1316102885a5SVasanthakumar Thiagarajan 		break;
1317102885a5SVasanthakumar Thiagarajan 	}
1318102885a5SVasanthakumar Thiagarajan }
1319102885a5SVasanthakumar Thiagarajan 
13203e9a212aSMohammed Shafi Shajakhan static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
13213e9a212aSMohammed Shafi Shajakhan 		struct ath_ant_comb *antcomb, int alt_ratio)
1322102885a5SVasanthakumar Thiagarajan {
13233e9a212aSMohammed Shafi Shajakhan 	if (ant_conf->div_group == 0) {
1324102885a5SVasanthakumar Thiagarajan 		/* Adjust the fast_div_bias based on main and alt lna conf */
13253e9a212aSMohammed Shafi Shajakhan 		switch ((ant_conf->main_lna_conf << 4) |
13263e9a212aSMohammed Shafi Shajakhan 				ant_conf->alt_lna_conf) {
1327102885a5SVasanthakumar Thiagarajan 		case (0x01): /* A-B LNA2 */
1328102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3b;
1329102885a5SVasanthakumar Thiagarajan 			break;
1330102885a5SVasanthakumar Thiagarajan 		case (0x02): /* A-B LNA1 */
1331102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3d;
1332102885a5SVasanthakumar Thiagarajan 			break;
1333102885a5SVasanthakumar Thiagarajan 		case (0x03): /* A-B A+B */
1334102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x1;
1335102885a5SVasanthakumar Thiagarajan 			break;
1336102885a5SVasanthakumar Thiagarajan 		case (0x10): /* LNA2 A-B */
1337102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x7;
1338102885a5SVasanthakumar Thiagarajan 			break;
1339102885a5SVasanthakumar Thiagarajan 		case (0x12): /* LNA2 LNA1 */
1340102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x2;
1341102885a5SVasanthakumar Thiagarajan 			break;
1342102885a5SVasanthakumar Thiagarajan 		case (0x13): /* LNA2 A+B */
1343102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x7;
1344102885a5SVasanthakumar Thiagarajan 			break;
1345102885a5SVasanthakumar Thiagarajan 		case (0x20): /* LNA1 A-B */
1346102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x6;
1347102885a5SVasanthakumar Thiagarajan 			break;
1348102885a5SVasanthakumar Thiagarajan 		case (0x21): /* LNA1 LNA2 */
1349102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x0;
1350102885a5SVasanthakumar Thiagarajan 			break;
1351102885a5SVasanthakumar Thiagarajan 		case (0x23): /* LNA1 A+B */
1352102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x6;
1353102885a5SVasanthakumar Thiagarajan 			break;
1354102885a5SVasanthakumar Thiagarajan 		case (0x30): /* A+B A-B */
1355102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x1;
1356102885a5SVasanthakumar Thiagarajan 			break;
1357102885a5SVasanthakumar Thiagarajan 		case (0x31): /* A+B LNA2 */
1358102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3b;
1359102885a5SVasanthakumar Thiagarajan 			break;
1360102885a5SVasanthakumar Thiagarajan 		case (0x32): /* A+B LNA1 */
1361102885a5SVasanthakumar Thiagarajan 			ant_conf->fast_div_bias = 0x3d;
1362102885a5SVasanthakumar Thiagarajan 			break;
1363102885a5SVasanthakumar Thiagarajan 		default:
1364102885a5SVasanthakumar Thiagarajan 			break;
1365102885a5SVasanthakumar Thiagarajan 		}
13663e9a212aSMohammed Shafi Shajakhan 	} else if (ant_conf->div_group == 2) {
13673e9a212aSMohammed Shafi Shajakhan 		/* Adjust the fast_div_bias based on main and alt_lna_conf */
13683e9a212aSMohammed Shafi Shajakhan 		switch ((ant_conf->main_lna_conf << 4) |
13693e9a212aSMohammed Shafi Shajakhan 				ant_conf->alt_lna_conf) {
13703e9a212aSMohammed Shafi Shajakhan 		case (0x01): /* A-B LNA2 */
13713e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
13723e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
13733e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
13743e9a212aSMohammed Shafi Shajakhan 			break;
13753e9a212aSMohammed Shafi Shajakhan 		case (0x02): /* A-B LNA1 */
13763e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
13773e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
13783e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
13793e9a212aSMohammed Shafi Shajakhan 			break;
13803e9a212aSMohammed Shafi Shajakhan 		case (0x03): /* A-B A+B */
13813e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
13823e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
13833e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
13843e9a212aSMohammed Shafi Shajakhan 			break;
13853e9a212aSMohammed Shafi Shajakhan 		case (0x10): /* LNA2 A-B */
13863e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
13873e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
13883e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
13893e9a212aSMohammed Shafi Shajakhan 			else
13903e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
13913e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
13923e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
13933e9a212aSMohammed Shafi Shajakhan 			break;
13943e9a212aSMohammed Shafi Shajakhan 		case (0x12): /* LNA2 LNA1 */
13953e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
13963e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
13973e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
13983e9a212aSMohammed Shafi Shajakhan 			break;
13993e9a212aSMohammed Shafi Shajakhan 		case (0x13): /* LNA2 A+B */
14003e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14013e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14023e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14033e9a212aSMohammed Shafi Shajakhan 			else
14043e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
14053e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14063e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14073e9a212aSMohammed Shafi Shajakhan 			break;
14083e9a212aSMohammed Shafi Shajakhan 		case (0x20): /* LNA1 A-B */
14093e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14103e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14113e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14123e9a212aSMohammed Shafi Shajakhan 			else
14133e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
14143e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14153e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14163e9a212aSMohammed Shafi Shajakhan 			break;
14173e9a212aSMohammed Shafi Shajakhan 		case (0x21): /* LNA1 LNA2 */
14183e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14193e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14203e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14213e9a212aSMohammed Shafi Shajakhan 			break;
14223e9a212aSMohammed Shafi Shajakhan 		case (0x23): /* LNA1 A+B */
14233e9a212aSMohammed Shafi Shajakhan 			if (!(antcomb->scan) &&
14243e9a212aSMohammed Shafi Shajakhan 				(alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
14253e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x1;
14263e9a212aSMohammed Shafi Shajakhan 			else
14273e9a212aSMohammed Shafi Shajakhan 				ant_conf->fast_div_bias = 0x2;
14283e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14293e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14303e9a212aSMohammed Shafi Shajakhan 			break;
14313e9a212aSMohammed Shafi Shajakhan 		case (0x30): /* A+B A-B */
14323e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14333e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14343e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14353e9a212aSMohammed Shafi Shajakhan 			break;
14363e9a212aSMohammed Shafi Shajakhan 		case (0x31): /* A+B LNA2 */
14373e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14383e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14393e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14403e9a212aSMohammed Shafi Shajakhan 			break;
14413e9a212aSMohammed Shafi Shajakhan 		case (0x32): /* A+B LNA1 */
14423e9a212aSMohammed Shafi Shajakhan 			ant_conf->fast_div_bias = 0x1;
14433e9a212aSMohammed Shafi Shajakhan 			ant_conf->main_gaintb = 0;
14443e9a212aSMohammed Shafi Shajakhan 			ant_conf->alt_gaintb = 0;
14453e9a212aSMohammed Shafi Shajakhan 			break;
14463e9a212aSMohammed Shafi Shajakhan 		default:
14473e9a212aSMohammed Shafi Shajakhan 			break;
14483e9a212aSMohammed Shafi Shajakhan 		}
14493e9a212aSMohammed Shafi Shajakhan 
14503e9a212aSMohammed Shafi Shajakhan 	}
14513e9a212aSMohammed Shafi Shajakhan 
1452102885a5SVasanthakumar Thiagarajan }
1453102885a5SVasanthakumar Thiagarajan 
1454102885a5SVasanthakumar Thiagarajan /* Antenna diversity and combining */
1455102885a5SVasanthakumar Thiagarajan static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1456102885a5SVasanthakumar Thiagarajan {
1457102885a5SVasanthakumar Thiagarajan 	struct ath_hw_antcomb_conf div_ant_conf;
1458102885a5SVasanthakumar Thiagarajan 	struct ath_ant_comb *antcomb = &sc->ant_comb;
1459102885a5SVasanthakumar Thiagarajan 	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
14600ff2b5c0SSujith Manoharan 	int curr_main_set;
1461102885a5SVasanthakumar Thiagarajan 	int main_rssi = rs->rs_rssi_ctl0;
1462102885a5SVasanthakumar Thiagarajan 	int alt_rssi = rs->rs_rssi_ctl1;
1463102885a5SVasanthakumar Thiagarajan 	int rx_ant_conf,  main_ant_conf;
1464102885a5SVasanthakumar Thiagarajan 	bool short_scan = false;
1465102885a5SVasanthakumar Thiagarajan 
1466102885a5SVasanthakumar Thiagarajan 	rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1467102885a5SVasanthakumar Thiagarajan 		       ATH_ANT_RX_MASK;
1468102885a5SVasanthakumar Thiagarajan 	main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1469102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_RX_MASK;
1470102885a5SVasanthakumar Thiagarajan 
1471*21e8ee6dSMohammed Shafi Shajakhan 	/* Record packet only when both main_rssi and  alt_rssi is positive */
1472*21e8ee6dSMohammed Shafi Shajakhan 	if (main_rssi > 0 && alt_rssi > 0) {
1473102885a5SVasanthakumar Thiagarajan 		antcomb->total_pkt_count++;
1474102885a5SVasanthakumar Thiagarajan 		antcomb->main_total_rssi += main_rssi;
1475102885a5SVasanthakumar Thiagarajan 		antcomb->alt_total_rssi  += alt_rssi;
1476102885a5SVasanthakumar Thiagarajan 		if (main_ant_conf == rx_ant_conf)
1477102885a5SVasanthakumar Thiagarajan 			antcomb->main_recv_cnt++;
1478102885a5SVasanthakumar Thiagarajan 		else
1479102885a5SVasanthakumar Thiagarajan 			antcomb->alt_recv_cnt++;
1480102885a5SVasanthakumar Thiagarajan 	}
1481102885a5SVasanthakumar Thiagarajan 
1482102885a5SVasanthakumar Thiagarajan 	/* Short scan check */
1483102885a5SVasanthakumar Thiagarajan 	if (antcomb->scan && antcomb->alt_good) {
1484102885a5SVasanthakumar Thiagarajan 		if (time_after(jiffies, antcomb->scan_start_time +
1485102885a5SVasanthakumar Thiagarajan 		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1486102885a5SVasanthakumar Thiagarajan 			short_scan = true;
1487102885a5SVasanthakumar Thiagarajan 		else
1488102885a5SVasanthakumar Thiagarajan 			if (antcomb->total_pkt_count ==
1489102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1490102885a5SVasanthakumar Thiagarajan 				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1491102885a5SVasanthakumar Thiagarajan 					    antcomb->total_pkt_count);
1492102885a5SVasanthakumar Thiagarajan 				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1493102885a5SVasanthakumar Thiagarajan 					short_scan = true;
1494102885a5SVasanthakumar Thiagarajan 			}
1495102885a5SVasanthakumar Thiagarajan 	}
1496102885a5SVasanthakumar Thiagarajan 
1497102885a5SVasanthakumar Thiagarajan 	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1498102885a5SVasanthakumar Thiagarajan 	    rs->rs_moreaggr) && !short_scan)
1499102885a5SVasanthakumar Thiagarajan 		return;
1500102885a5SVasanthakumar Thiagarajan 
1501102885a5SVasanthakumar Thiagarajan 	if (antcomb->total_pkt_count) {
1502102885a5SVasanthakumar Thiagarajan 		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1503102885a5SVasanthakumar Thiagarajan 			     antcomb->total_pkt_count);
1504102885a5SVasanthakumar Thiagarajan 		main_rssi_avg = (antcomb->main_total_rssi /
1505102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1506102885a5SVasanthakumar Thiagarajan 		alt_rssi_avg = (antcomb->alt_total_rssi /
1507102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1508102885a5SVasanthakumar Thiagarajan 	}
1509102885a5SVasanthakumar Thiagarajan 
1510102885a5SVasanthakumar Thiagarajan 
1511102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1512102885a5SVasanthakumar Thiagarajan 	curr_alt_set = div_ant_conf.alt_lna_conf;
1513102885a5SVasanthakumar Thiagarajan 	curr_main_set = div_ant_conf.main_lna_conf;
1514102885a5SVasanthakumar Thiagarajan 
1515102885a5SVasanthakumar Thiagarajan 	antcomb->count++;
1516102885a5SVasanthakumar Thiagarajan 
1517102885a5SVasanthakumar Thiagarajan 	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1518102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1519102885a5SVasanthakumar Thiagarajan 			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1520102885a5SVasanthakumar Thiagarajan 						  main_rssi_avg);
1521102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = true;
1522102885a5SVasanthakumar Thiagarajan 		} else {
1523102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = false;
1524102885a5SVasanthakumar Thiagarajan 		}
1525102885a5SVasanthakumar Thiagarajan 
1526102885a5SVasanthakumar Thiagarajan 		antcomb->count = 0;
1527102885a5SVasanthakumar Thiagarajan 		antcomb->scan = true;
1528102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = true;
1529102885a5SVasanthakumar Thiagarajan 	}
1530102885a5SVasanthakumar Thiagarajan 
1531102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan) {
1532b85c5734SMohammed Shafi Shajakhan 		if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1533b85c5734SMohammed Shafi Shajakhan 					alt_ratio, curr_main_set, curr_alt_set,
1534b85c5734SMohammed Shafi Shajakhan 					alt_rssi_avg, main_rssi_avg)) {
1535102885a5SVasanthakumar Thiagarajan 			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1536102885a5SVasanthakumar Thiagarajan 				/* Switch main and alt LNA */
1537102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1538102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1539102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1540102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1541102885a5SVasanthakumar Thiagarajan 			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1542102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1543102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1544102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1545102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1546102885a5SVasanthakumar Thiagarajan 			}
1547102885a5SVasanthakumar Thiagarajan 
1548102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1549102885a5SVasanthakumar Thiagarajan 		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1550102885a5SVasanthakumar Thiagarajan 			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1551102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1552102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1553102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1554102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1555102885a5SVasanthakumar Thiagarajan 			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1556102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1557102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1558102885a5SVasanthakumar Thiagarajan 
1559102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1560102885a5SVasanthakumar Thiagarajan 		}
1561102885a5SVasanthakumar Thiagarajan 
1562102885a5SVasanthakumar Thiagarajan 		if ((alt_rssi_avg < (main_rssi_avg +
15638afbcc8bSMohammed Shafi Shajakhan 						div_ant_conf.lna1_lna2_delta)))
1564102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1565102885a5SVasanthakumar Thiagarajan 	}
1566102885a5SVasanthakumar Thiagarajan 
1567102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan_not_start) {
1568102885a5SVasanthakumar Thiagarajan 		switch (curr_alt_set) {
1569102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA2:
1570102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1571102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = main_rssi_avg;
1572102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1573102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1574102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf =
1575102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1;
1576102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1577102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1578102885a5SVasanthakumar Thiagarajan 			break;
1579102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1:
1580102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1581102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = main_rssi_avg;
1582102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1583102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1584102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1585102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1586102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1587102885a5SVasanthakumar Thiagarajan 			break;
1588102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1589102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_add = alt_rssi_avg;
1590102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1591102885a5SVasanthakumar Thiagarajan 			/* set to A-B */
1592102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf =
1593102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1594102885a5SVasanthakumar Thiagarajan 			break;
1595102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1596102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_sub = alt_rssi_avg;
1597102885a5SVasanthakumar Thiagarajan 			antcomb->scan = false;
1598102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_lna2 >
1599102885a5SVasanthakumar Thiagarajan 			    (antcomb->rssi_lna1 +
1600102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1601102885a5SVasanthakumar Thiagarajan 				/* use LNA2 as main LNA */
1602102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1603102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1604102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1605102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1606102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1607102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1608102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1609102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1610102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1611102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1612102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1613102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1614102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1615102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1616102885a5SVasanthakumar Thiagarajan 				} else {
1617102885a5SVasanthakumar Thiagarajan 					/* set to LNA1 */
1618102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1619102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1620102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1621102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1622102885a5SVasanthakumar Thiagarajan 				}
1623102885a5SVasanthakumar Thiagarajan 			} else {
1624102885a5SVasanthakumar Thiagarajan 				/* use LNA1 as main LNA */
1625102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1626102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1627102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1628102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1629102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1630102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1631102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1632102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1633102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1634102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1635102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1636102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1637102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1638102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1639102885a5SVasanthakumar Thiagarajan 				} else {
1640102885a5SVasanthakumar Thiagarajan 					/* set to LNA2 */
1641102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1642102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1643102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1644102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1645102885a5SVasanthakumar Thiagarajan 				}
1646102885a5SVasanthakumar Thiagarajan 			}
1647102885a5SVasanthakumar Thiagarajan 			break;
1648102885a5SVasanthakumar Thiagarajan 		default:
1649102885a5SVasanthakumar Thiagarajan 			break;
1650102885a5SVasanthakumar Thiagarajan 		}
1651102885a5SVasanthakumar Thiagarajan 	} else {
1652102885a5SVasanthakumar Thiagarajan 		if (!antcomb->alt_good) {
1653102885a5SVasanthakumar Thiagarajan 			antcomb->scan_not_start = false;
1654102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1655102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1656102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1657102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1658102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1659102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1660102885a5SVasanthakumar Thiagarajan 			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1661102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1662102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1663102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1664102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1665102885a5SVasanthakumar Thiagarajan 			}
1666102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1667102885a5SVasanthakumar Thiagarajan 		}
1668102885a5SVasanthakumar Thiagarajan 	}
1669102885a5SVasanthakumar Thiagarajan 
1670102885a5SVasanthakumar Thiagarajan 	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1671102885a5SVasanthakumar Thiagarajan 					   main_rssi_avg, alt_rssi_avg,
1672102885a5SVasanthakumar Thiagarajan 					   alt_ratio);
1673102885a5SVasanthakumar Thiagarajan 
1674102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt++;
1675102885a5SVasanthakumar Thiagarajan 
1676102885a5SVasanthakumar Thiagarajan div_comb_done:
16773e9a212aSMohammed Shafi Shajakhan 	ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
1678102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1679102885a5SVasanthakumar Thiagarajan 
1680102885a5SVasanthakumar Thiagarajan 	antcomb->scan_start_time = jiffies;
1681102885a5SVasanthakumar Thiagarajan 	antcomb->total_pkt_count = 0;
1682102885a5SVasanthakumar Thiagarajan 	antcomb->main_total_rssi = 0;
1683102885a5SVasanthakumar Thiagarajan 	antcomb->alt_total_rssi = 0;
1684102885a5SVasanthakumar Thiagarajan 	antcomb->main_recv_cnt = 0;
1685102885a5SVasanthakumar Thiagarajan 	antcomb->alt_recv_cnt = 0;
1686102885a5SVasanthakumar Thiagarajan }
1687102885a5SVasanthakumar Thiagarajan 
1688b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1689b5c80475SFelix Fietkau {
1690b5c80475SFelix Fietkau 	struct ath_buf *bf;
16910d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1692b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1693b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1694b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
1695b5c80475SFelix Fietkau 	/*
1696cae6b74dSMohammed Shafi Shajakhan 	 * The hw can technically differ from common->hw when using ath9k
1697b5c80475SFelix Fietkau 	 * virtual wiphy so to account for that we iterate over the active
1698b5c80475SFelix Fietkau 	 * wiphys and find the appropriate wiphy and therefore hw.
1699b5c80475SFelix Fietkau 	 */
17007545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1701b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1702b5c80475SFelix Fietkau 	int retval;
1703b5c80475SFelix Fietkau 	bool decrypt_error = false;
1704b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1705b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1706b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1707b5c80475SFelix Fietkau 	int dma_type;
17085c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1709a6d2055bSFelix Fietkau 	u64 tsf = 0;
1710a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
17118ab2cd09SLuis R. Rodriguez 	unsigned long flags;
1712b5c80475SFelix Fietkau 
1713b5c80475SFelix Fietkau 	if (edma)
1714b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
171556824223SMing Lei 	else
171656824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1717b5c80475SFelix Fietkau 
1718b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1719b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
1720b5c80475SFelix Fietkau 
1721a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1722a6d2055bSFelix Fietkau 	tsf_lower = tsf & 0xffffffff;
1723a6d2055bSFelix Fietkau 
1724b5c80475SFelix Fietkau 	do {
1725b5c80475SFelix Fietkau 		/* If handling rx interrupt and flush is in progress => exit */
1726b5c80475SFelix Fietkau 		if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1727b5c80475SFelix Fietkau 			break;
1728b5c80475SFelix Fietkau 
1729b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1730b5c80475SFelix Fietkau 		if (edma)
1731b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1732b5c80475SFelix Fietkau 		else
1733b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1734b5c80475SFelix Fietkau 
1735b5c80475SFelix Fietkau 		if (!bf)
1736b5c80475SFelix Fietkau 			break;
1737b5c80475SFelix Fietkau 
1738b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1739b5c80475SFelix Fietkau 		if (!skb)
1740b5c80475SFelix Fietkau 			continue;
1741b5c80475SFelix Fietkau 
17420d95521eSFelix Fietkau 		/*
17430d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
17440d95521eSFelix Fietkau 		 * the last one.
17450d95521eSFelix Fietkau 		 */
17460d95521eSFelix Fietkau 		if (sc->rx.frag)
17470d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
17480d95521eSFelix Fietkau 		else
17490d95521eSFelix Fietkau 			hdr_skb = skb;
17500d95521eSFelix Fietkau 
17510d95521eSFelix Fietkau 		hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
17520d95521eSFelix Fietkau 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
17535ca42627SLuis R. Rodriguez 
175429bffa96SFelix Fietkau 		ath_debug_stat_rx(sc, &rs);
17551395d3f0SSujith 
1756203c4805SLuis R. Rodriguez 		/*
1757203c4805SLuis R. Rodriguez 		 * If we're asked to flush receive queue, directly
1758203c4805SLuis R. Rodriguez 		 * chain it back at the queue without processing it.
1759203c4805SLuis R. Rodriguez 		 */
1760203c4805SLuis R. Rodriguez 		if (flush)
17610d95521eSFelix Fietkau 			goto requeue_drop_frag;
1762203c4805SLuis R. Rodriguez 
1763c8f3b721SJan Friedrich 		retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1764c8f3b721SJan Friedrich 						 rxs, &decrypt_error);
1765c8f3b721SJan Friedrich 		if (retval)
17660d95521eSFelix Fietkau 			goto requeue_drop_frag;
1767c8f3b721SJan Friedrich 
1768a6d2055bSFelix Fietkau 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1769a6d2055bSFelix Fietkau 		if (rs.rs_tstamp > tsf_lower &&
1770a6d2055bSFelix Fietkau 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1771a6d2055bSFelix Fietkau 			rxs->mactime -= 0x100000000ULL;
1772a6d2055bSFelix Fietkau 
1773a6d2055bSFelix Fietkau 		if (rs.rs_tstamp < tsf_lower &&
1774a6d2055bSFelix Fietkau 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1775a6d2055bSFelix Fietkau 			rxs->mactime += 0x100000000ULL;
1776a6d2055bSFelix Fietkau 
1777203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1778203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1779cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1780203c4805SLuis R. Rodriguez 
1781203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1782203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1783203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1784203c4805SLuis R. Rodriguez 		 * processing. */
1785203c4805SLuis R. Rodriguez 		if (!requeue_skb)
17860d95521eSFelix Fietkau 			goto requeue_drop_frag;
1787203c4805SLuis R. Rodriguez 
1788203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1789203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1790cc861f74SLuis R. Rodriguez 				 common->rx_bufsize,
1791b5c80475SFelix Fietkau 				 dma_type);
1792203c4805SLuis R. Rodriguez 
1793b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1794b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1795b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1796203c4805SLuis R. Rodriguez 
17970d95521eSFelix Fietkau 		if (!rs.rs_more)
17980d95521eSFelix Fietkau 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1799c9b14170SLuis R. Rodriguez 						 rxs, decrypt_error);
1800203c4805SLuis R. Rodriguez 
1801203c4805SLuis R. Rodriguez 		/* We will now give hardware our shiny new allocated skb */
1802203c4805SLuis R. Rodriguez 		bf->bf_mpdu = requeue_skb;
1803203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1804cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
1805b5c80475SFelix Fietkau 						 dma_type);
1806203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
1807203c4805SLuis R. Rodriguez 			  bf->bf_buf_addr))) {
1808203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(requeue_skb);
1809203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
18106cf9e995SBen Greear 			bf->bf_buf_addr = 0;
18113800276aSJoe Perches 			ath_err(common, "dma_mapping_error() on RX\n");
18127545daf4SFelix Fietkau 			ieee80211_rx(hw, skb);
1813203c4805SLuis R. Rodriguez 			break;
1814203c4805SLuis R. Rodriguez 		}
1815203c4805SLuis R. Rodriguez 
18160d95521eSFelix Fietkau 		if (rs.rs_more) {
18170d95521eSFelix Fietkau 			/*
18180d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
18190d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
18200d95521eSFelix Fietkau 			 * scatter-gather operation.
18210d95521eSFelix Fietkau 			 */
18220d95521eSFelix Fietkau 			if (sc->rx.frag) {
18230d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
18240d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
18250d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
18260d95521eSFelix Fietkau 				skb = NULL;
18270d95521eSFelix Fietkau 			}
18280d95521eSFelix Fietkau 			sc->rx.frag = skb;
18290d95521eSFelix Fietkau 			goto requeue;
18300d95521eSFelix Fietkau 		}
18310d95521eSFelix Fietkau 
18320d95521eSFelix Fietkau 		if (sc->rx.frag) {
18330d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
18340d95521eSFelix Fietkau 
18350d95521eSFelix Fietkau 			sc->rx.frag = NULL;
18360d95521eSFelix Fietkau 
18370d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
18380d95521eSFelix Fietkau 				dev_kfree_skb(skb);
18390d95521eSFelix Fietkau 				goto requeue_drop_frag;
18400d95521eSFelix Fietkau 			}
18410d95521eSFelix Fietkau 
18420d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
18430d95521eSFelix Fietkau 						  skb->len);
18440d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
18450d95521eSFelix Fietkau 			skb = hdr_skb;
18460d95521eSFelix Fietkau 		}
18470d95521eSFelix Fietkau 
1848203c4805SLuis R. Rodriguez 		/*
1849203c4805SLuis R. Rodriguez 		 * change the default rx antenna if rx diversity chooses the
1850203c4805SLuis R. Rodriguez 		 * other antenna 3 times in a row.
1851203c4805SLuis R. Rodriguez 		 */
185229bffa96SFelix Fietkau 		if (sc->rx.defant != rs.rs_antenna) {
1853203c4805SLuis R. Rodriguez 			if (++sc->rx.rxotherant >= 3)
185429bffa96SFelix Fietkau 				ath_setdefantenna(sc, rs.rs_antenna);
1855203c4805SLuis R. Rodriguez 		} else {
1856203c4805SLuis R. Rodriguez 			sc->rx.rxotherant = 0;
1857203c4805SLuis R. Rodriguez 		}
1858203c4805SLuis R. Rodriguez 
18598ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1860aaef24b4SMohammed Shafi Shajakhan 
1861aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
18621b04b930SSujith 					      PS_WAIT_FOR_CAB |
1863aaef24b4SMohammed Shafi Shajakhan 					      PS_WAIT_FOR_PSPOLL_DATA)) ||
1864cedc7e3dSMohammed Shafi Shajakhan 						ath9k_check_auto_sleep(sc))
1865cc65965cSJouni Malinen 			ath_rx_ps(sc, skb);
18668ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1867cc65965cSJouni Malinen 
1868102885a5SVasanthakumar Thiagarajan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1869102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1870102885a5SVasanthakumar Thiagarajan 
18717545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1872cc65965cSJouni Malinen 
18730d95521eSFelix Fietkau requeue_drop_frag:
18740d95521eSFelix Fietkau 		if (sc->rx.frag) {
18750d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
18760d95521eSFelix Fietkau 			sc->rx.frag = NULL;
18770d95521eSFelix Fietkau 		}
1878203c4805SLuis R. Rodriguez requeue:
1879b5c80475SFelix Fietkau 		if (edma) {
1880b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
1881b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1882b5c80475SFelix Fietkau 		} else {
1883203c4805SLuis R. Rodriguez 			list_move_tail(&bf->list, &sc->rx.rxbuf);
1884203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
188595294973SFelix Fietkau 			ath9k_hw_rxena(ah);
1886b5c80475SFelix Fietkau 		}
1887203c4805SLuis R. Rodriguez 	} while (1);
1888203c4805SLuis R. Rodriguez 
1889203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
1890203c4805SLuis R. Rodriguez 
1891203c4805SLuis R. Rodriguez 	return 0;
1892203c4805SLuis R. Rodriguez }
1893