xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision 102885a5d114abad8f9d4101f94ce5b28c232231)
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 
22*102885a5SVasanthakumar Thiagarajan static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
23*102885a5SVasanthakumar Thiagarajan 					       int mindelta, int main_rssi_avg,
24*102885a5SVasanthakumar Thiagarajan 					       int alt_rssi_avg, int pkt_count)
25*102885a5SVasanthakumar Thiagarajan {
26*102885a5SVasanthakumar Thiagarajan 	return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
27*102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + maxdelta)) ||
28*102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
29*102885a5SVasanthakumar Thiagarajan }
30*102885a5SVasanthakumar Thiagarajan 
31ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
32ededf1f8SVasanthakumar Thiagarajan {
33ededf1f8SVasanthakumar Thiagarajan 	return sc->ps_enabled &&
34ededf1f8SVasanthakumar Thiagarajan 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
35ededf1f8SVasanthakumar Thiagarajan }
36ededf1f8SVasanthakumar Thiagarajan 
37203c4805SLuis R. Rodriguez static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
38203c4805SLuis R. Rodriguez 					     struct ieee80211_hdr *hdr)
39203c4805SLuis R. Rodriguez {
40203c4805SLuis R. Rodriguez 	struct ieee80211_hw *hw = sc->pri_wiphy->hw;
41203c4805SLuis R. Rodriguez 	int i;
42203c4805SLuis R. Rodriguez 
43203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->wiphy_lock);
44203c4805SLuis R. Rodriguez 	for (i = 0; i < sc->num_sec_wiphy; i++) {
45203c4805SLuis R. Rodriguez 		struct ath_wiphy *aphy = sc->sec_wiphy[i];
46203c4805SLuis R. Rodriguez 		if (aphy == NULL)
47203c4805SLuis R. Rodriguez 			continue;
48203c4805SLuis R. Rodriguez 		if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
49203c4805SLuis R. Rodriguez 		    == 0) {
50203c4805SLuis R. Rodriguez 			hw = aphy->hw;
51203c4805SLuis R. Rodriguez 			break;
52203c4805SLuis R. Rodriguez 		}
53203c4805SLuis R. Rodriguez 	}
54203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->wiphy_lock);
55203c4805SLuis R. Rodriguez 	return hw;
56203c4805SLuis R. Rodriguez }
57203c4805SLuis R. Rodriguez 
58203c4805SLuis R. Rodriguez /*
59203c4805SLuis R. Rodriguez  * Setup and link descriptors.
60203c4805SLuis R. Rodriguez  *
61203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
62203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
63203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
64203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
65203c4805SLuis R. Rodriguez  */
66203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
67203c4805SLuis R. Rodriguez {
68203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
69cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
70203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
71203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
72203c4805SLuis R. Rodriguez 
73203c4805SLuis R. Rodriguez 	ATH_RXBUF_RESET(bf);
74203c4805SLuis R. Rodriguez 
75203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
76203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
77203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
78203c4805SLuis R. Rodriguez 
79203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
80203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
819680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
82203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
83203c4805SLuis R. Rodriguez 
84cc861f74SLuis R. Rodriguez 	/*
85cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
86203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
87cc861f74SLuis R. Rodriguez 	 * to process
88cc861f74SLuis R. Rodriguez 	 */
89203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
90cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
91203c4805SLuis R. Rodriguez 			     0);
92203c4805SLuis R. Rodriguez 
93203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
94203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
95203c4805SLuis R. Rodriguez 	else
96203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
97203c4805SLuis R. Rodriguez 
98203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
99203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
100203c4805SLuis R. Rodriguez }
101203c4805SLuis R. Rodriguez 
102203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
103203c4805SLuis R. Rodriguez {
104203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
105203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
106203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
107203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
108203c4805SLuis R. Rodriguez }
109203c4805SLuis R. Rodriguez 
110203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
111203c4805SLuis R. Rodriguez {
112203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
1131510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
1141510718dSLuis R. Rodriguez 
115203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
116203c4805SLuis R. Rodriguez 
117203c4805SLuis R. Rodriguez 	/* configure rx filter */
118203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
119203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
120203c4805SLuis R. Rodriguez 
121203c4805SLuis R. Rodriguez 	/* configure bssid mask */
122203c4805SLuis R. Rodriguez 	if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
12313b81559SLuis R. Rodriguez 		ath_hw_setbssidmask(common);
124203c4805SLuis R. Rodriguez 
125203c4805SLuis R. Rodriguez 	/* configure operational mode */
126203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
127203c4805SLuis R. Rodriguez 
128203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
129203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
130203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
131203c4805SLuis R. Rodriguez }
132203c4805SLuis R. Rodriguez 
133b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
134b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
135b5c80475SFelix Fietkau {
136b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
137b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
138b5c80475SFelix Fietkau 	struct sk_buff *skb;
139b5c80475SFelix Fietkau 	struct ath_buf *bf;
140b5c80475SFelix Fietkau 
141b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
142b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
143b5c80475SFelix Fietkau 		return false;
144b5c80475SFelix Fietkau 
145b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
146b5c80475SFelix Fietkau 	list_del_init(&bf->list);
147b5c80475SFelix Fietkau 
148b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
149b5c80475SFelix Fietkau 
150b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
151b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
152b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
153b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
154b5c80475SFelix Fietkau 
155b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
156b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
157b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
158b5c80475SFelix Fietkau 
159b5c80475SFelix Fietkau 	return true;
160b5c80475SFelix Fietkau }
161b5c80475SFelix Fietkau 
162b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
163b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
164b5c80475SFelix Fietkau {
165b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
166b5c80475SFelix Fietkau 	u32 nbuf = 0;
167b5c80475SFelix Fietkau 
168b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
169b5c80475SFelix Fietkau 		ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
170b5c80475SFelix Fietkau 		return;
171b5c80475SFelix Fietkau 	}
172b5c80475SFelix Fietkau 
173b5c80475SFelix Fietkau 	while (!list_empty(&sc->rx.rxbuf)) {
174b5c80475SFelix Fietkau 		nbuf++;
175b5c80475SFelix Fietkau 
176b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
177b5c80475SFelix Fietkau 			break;
178b5c80475SFelix Fietkau 
179b5c80475SFelix Fietkau 		if (nbuf >= size)
180b5c80475SFelix Fietkau 			break;
181b5c80475SFelix Fietkau 	}
182b5c80475SFelix Fietkau }
183b5c80475SFelix Fietkau 
184b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
185b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
186b5c80475SFelix Fietkau {
187b5c80475SFelix Fietkau 	struct ath_buf *bf;
188b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
189b5c80475SFelix Fietkau 	struct sk_buff *skb;
190b5c80475SFelix Fietkau 
191b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
192b5c80475SFelix Fietkau 
193b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
194b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
195b5c80475SFelix Fietkau 		BUG_ON(!bf);
196b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
197b5c80475SFelix Fietkau 	}
198b5c80475SFelix Fietkau }
199b5c80475SFelix Fietkau 
200b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
201b5c80475SFelix Fietkau {
202b5c80475SFelix Fietkau 	struct ath_buf *bf;
203b5c80475SFelix Fietkau 
204b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
205b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
206b5c80475SFelix Fietkau 
207b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
208b5c80475SFelix Fietkau 		if (bf->bf_mpdu)
209b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
210b5c80475SFelix Fietkau 	}
211b5c80475SFelix Fietkau 
212b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
213b5c80475SFelix Fietkau 
214b5c80475SFelix Fietkau 	kfree(sc->rx.rx_bufptr);
215b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = NULL;
216b5c80475SFelix Fietkau }
217b5c80475SFelix Fietkau 
218b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
219b5c80475SFelix Fietkau {
220b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
221b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_buffers);
222b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
223b5c80475SFelix Fietkau }
224b5c80475SFelix Fietkau 
225b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
226b5c80475SFelix Fietkau {
227b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
228b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
229b5c80475SFelix Fietkau 	struct sk_buff *skb;
230b5c80475SFelix Fietkau 	struct ath_buf *bf;
231b5c80475SFelix Fietkau 	int error = 0, i;
232b5c80475SFelix Fietkau 	u32 size;
233b5c80475SFelix Fietkau 
234b5c80475SFelix Fietkau 
235b5c80475SFelix Fietkau 	common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
236b5c80475SFelix Fietkau 				     ah->caps.rx_status_len,
237b5c80475SFelix Fietkau 				     min(common->cachelsz, (u16)64));
238b5c80475SFelix Fietkau 
239b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
240b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
241b5c80475SFelix Fietkau 
242b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
243b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
244b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
245b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
246b5c80475SFelix Fietkau 
247b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
248b5c80475SFelix Fietkau 	bf = kzalloc(size, GFP_KERNEL);
249b5c80475SFelix Fietkau 	if (!bf)
250b5c80475SFelix Fietkau 		return -ENOMEM;
251b5c80475SFelix Fietkau 
252b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
253b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = bf;
254b5c80475SFelix Fietkau 
255b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
256b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
257b5c80475SFelix Fietkau 		if (!skb) {
258b5c80475SFelix Fietkau 			error = -ENOMEM;
259b5c80475SFelix Fietkau 			goto rx_init_fail;
260b5c80475SFelix Fietkau 		}
261b5c80475SFelix Fietkau 
262b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
263b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
264b5c80475SFelix Fietkau 
265b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
266b5c80475SFelix Fietkau 						 common->rx_bufsize,
267b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
268b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
269b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
270b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
271b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
272b5c80475SFelix Fietkau 				ath_print(common, ATH_DBG_FATAL,
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 	spin_unlock_bh(&sc->rx.rxbuflock);
301b5c80475SFelix Fietkau 
302b5c80475SFelix Fietkau 	ath_opmode_init(sc);
303b5c80475SFelix Fietkau 
30440346b66SLuis R. Rodriguez 	ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_SCANNING));
305b5c80475SFelix Fietkau }
306b5c80475SFelix Fietkau 
307b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
308b5c80475SFelix Fietkau {
309b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
310b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
311b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
312b5c80475SFelix Fietkau 	spin_unlock_bh(&sc->rx.rxbuflock);
313b5c80475SFelix Fietkau }
314b5c80475SFelix Fietkau 
315203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
316203c4805SLuis R. Rodriguez {
31727c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
318203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
319203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
320203c4805SLuis R. Rodriguez 	int error = 0;
321203c4805SLuis R. Rodriguez 
322203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxflushlock);
323203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
324203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxbuflock);
325203c4805SLuis R. Rodriguez 
326b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
327b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
328b5c80475SFelix Fietkau 	} else {
329cc861f74SLuis R. Rodriguez 		common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
33027c51f1aSLuis R. Rodriguez 				min(common->cachelsz, (u16)64));
331203c4805SLuis R. Rodriguez 
332c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
333cc861f74SLuis R. Rodriguez 				common->cachelsz, common->rx_bufsize);
334203c4805SLuis R. Rodriguez 
335203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
336203c4805SLuis R. Rodriguez 
337203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
3384adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
339203c4805SLuis R. Rodriguez 		if (error != 0) {
340c46917bbSLuis R. Rodriguez 			ath_print(common, ATH_DBG_FATAL,
341b5c80475SFelix Fietkau 				  "failed to allocate rx descriptors: %d\n",
342b5c80475SFelix Fietkau 				  error);
343203c4805SLuis R. Rodriguez 			goto err;
344203c4805SLuis R. Rodriguez 		}
345203c4805SLuis R. Rodriguez 
346203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
347b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
348b5c80475SFelix Fietkau 					      GFP_KERNEL);
349203c4805SLuis R. Rodriguez 			if (skb == NULL) {
350203c4805SLuis R. Rodriguez 				error = -ENOMEM;
351203c4805SLuis R. Rodriguez 				goto err;
352203c4805SLuis R. Rodriguez 			}
353203c4805SLuis R. Rodriguez 
354203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
355203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
356cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
357203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
358203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
359203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
360203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
361203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
362c46917bbSLuis R. Rodriguez 				ath_print(common, ATH_DBG_FATAL,
363203c4805SLuis R. Rodriguez 					  "dma_mapping_error() on RX init\n");
364203c4805SLuis R. Rodriguez 				error = -ENOMEM;
365203c4805SLuis R. Rodriguez 				goto err;
366203c4805SLuis R. Rodriguez 			}
367203c4805SLuis R. Rodriguez 			bf->bf_dmacontext = bf->bf_buf_addr;
368203c4805SLuis R. Rodriguez 		}
369203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
370b5c80475SFelix Fietkau 	}
371203c4805SLuis R. Rodriguez 
372203c4805SLuis R. Rodriguez err:
373203c4805SLuis R. Rodriguez 	if (error)
374203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
375203c4805SLuis R. Rodriguez 
376203c4805SLuis R. Rodriguez 	return error;
377203c4805SLuis R. Rodriguez }
378203c4805SLuis R. Rodriguez 
379203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
380203c4805SLuis R. Rodriguez {
381cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
382cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
383203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
384203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
385203c4805SLuis R. Rodriguez 
386b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
387b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
388b5c80475SFelix Fietkau 		return;
389b5c80475SFelix Fietkau 	} else {
390203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
391203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
392203c4805SLuis R. Rodriguez 			if (skb) {
393203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
394b5c80475SFelix Fietkau 						common->rx_bufsize,
395b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
396203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
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 
434203c4805SLuis R. Rodriguez 	/* If not a STA, enable processing of Probe Requests */
435203c4805SLuis R. Rodriguez 	if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
436203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
437203c4805SLuis R. Rodriguez 
438203c4805SLuis R. Rodriguez 	/*
439203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
440203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
441203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
442203c4805SLuis R. Rodriguez 	 */
443203c4805SLuis R. Rodriguez 	if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
444203c4805SLuis R. Rodriguez 	     (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
445203c4805SLuis R. Rodriguez 	    (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
446203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
447203c4805SLuis R. Rodriguez 
448203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
449203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
450203c4805SLuis R. Rodriguez 
451203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
452203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
453203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
454203c4805SLuis R. Rodriguez 	else
455203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
456203c4805SLuis R. Rodriguez 
45766afad01SSenthil Balasubramanian 	if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) ||
45866afad01SSenthil Balasubramanian 	    AR_SREV_9285_10_OR_LATER(sc->sc_ah)) &&
45966afad01SSenthil Balasubramanian 	    (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
46066afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
461203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
462203c4805SLuis R. Rodriguez 
4637ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4647ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4657ea310beSSujith 
4665eb6ba83SJavier Cardona 	if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
467203c4805SLuis R. Rodriguez 		/* TODO: only needed if more than one BSSID is in use in
468203c4805SLuis R. Rodriguez 		 * station/adhoc mode */
4695eb6ba83SJavier Cardona 		/* The following may also be needed for other older chips */
4705eb6ba83SJavier Cardona 		if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
4715eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
472203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
473203c4805SLuis R. Rodriguez 	}
474203c4805SLuis R. Rodriguez 
475203c4805SLuis R. Rodriguez 	return rfilt;
476203c4805SLuis R. Rodriguez 
477203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE
478203c4805SLuis R. Rodriguez }
479203c4805SLuis R. Rodriguez 
480203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
481203c4805SLuis R. Rodriguez {
482203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
483203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
484203c4805SLuis R. Rodriguez 
485b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
486b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
487b5c80475SFelix Fietkau 		return 0;
488b5c80475SFelix Fietkau 	}
489b5c80475SFelix Fietkau 
490203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
491203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
492203c4805SLuis R. Rodriguez 		goto start_recv;
493203c4805SLuis R. Rodriguez 
494203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
495203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
496203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
497203c4805SLuis R. Rodriguez 	}
498203c4805SLuis R. Rodriguez 
499203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
500203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
501203c4805SLuis R. Rodriguez 		goto start_recv;
502203c4805SLuis R. Rodriguez 
503203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
504203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
505203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
506203c4805SLuis R. Rodriguez 
507203c4805SLuis R. Rodriguez start_recv:
508203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
509203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
51040346b66SLuis R. Rodriguez 	ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_SCANNING));
511203c4805SLuis R. Rodriguez 
512203c4805SLuis R. Rodriguez 	return 0;
513203c4805SLuis R. Rodriguez }
514203c4805SLuis R. Rodriguez 
515203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
516203c4805SLuis R. Rodriguez {
517203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
518203c4805SLuis R. Rodriguez 	bool stopped;
519203c4805SLuis R. Rodriguez 
520203c4805SLuis R. Rodriguez 	ath9k_hw_stoppcurecv(ah);
521203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
522203c4805SLuis R. Rodriguez 	stopped = ath9k_hw_stopdmarecv(ah);
523b5c80475SFelix Fietkau 
524b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
525b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
526b5c80475SFelix Fietkau 	else
527203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
528203c4805SLuis R. Rodriguez 
529203c4805SLuis R. Rodriguez 	return stopped;
530203c4805SLuis R. Rodriguez }
531203c4805SLuis R. Rodriguez 
532203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc)
533203c4805SLuis R. Rodriguez {
534203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxflushlock);
535203c4805SLuis R. Rodriguez 	sc->sc_flags |= SC_OP_RXFLUSH;
536b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
537b5c80475SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
538b5c80475SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
539203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
540203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxflushlock);
541203c4805SLuis R. Rodriguez }
542203c4805SLuis R. Rodriguez 
543cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
544cc65965cSJouni Malinen {
545cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
546cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
547cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
548cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
549cc65965cSJouni Malinen 
550cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
551cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
552cc65965cSJouni Malinen 	end = skb->data + skb->len;
553cc65965cSJouni Malinen 
554cc65965cSJouni Malinen 	while (pos + 2 < end) {
555cc65965cSJouni Malinen 		id = *pos++;
556cc65965cSJouni Malinen 		elen = *pos++;
557cc65965cSJouni Malinen 		if (pos + elen > end)
558cc65965cSJouni Malinen 			break;
559cc65965cSJouni Malinen 
560cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
561cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
562cc65965cSJouni Malinen 				break;
563cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
564cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
565cc65965cSJouni Malinen 				break;
566cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
567cc65965cSJouni Malinen 		}
568cc65965cSJouni Malinen 
569cc65965cSJouni Malinen 		pos += elen;
570cc65965cSJouni Malinen 	}
571cc65965cSJouni Malinen 
572cc65965cSJouni Malinen 	return false;
573cc65965cSJouni Malinen }
574cc65965cSJouni Malinen 
575cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
576cc65965cSJouni Malinen {
577cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
5781510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
579cc65965cSJouni Malinen 
580cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
581cc65965cSJouni Malinen 		return;
582cc65965cSJouni Malinen 
583cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
5841510718dSLuis R. Rodriguez 	if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
585cc65965cSJouni Malinen 		return; /* not from our current AP */
586cc65965cSJouni Malinen 
5871b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
588293dc5dfSGabor Juhos 
5891b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5901b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
591c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
592c46917bbSLuis R. Rodriguez 			  "Reconfigure Beacon timers based on "
593ccdfeab6SJouni Malinen 			  "timestamp from the AP\n");
594ccdfeab6SJouni Malinen 		ath_beacon_config(sc, NULL);
595ccdfeab6SJouni Malinen 	}
596ccdfeab6SJouni Malinen 
597cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
598cc65965cSJouni Malinen 		/*
599cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
60058f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
60158f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
60258f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
60358f5fffdSGabor Juhos 		 * so we are waiting for it as well.
604cc65965cSJouni Malinen 		 */
605c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
606cc65965cSJouni Malinen 			  "buffered broadcast/multicast frame(s)\n");
6071b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
608cc65965cSJouni Malinen 		return;
609cc65965cSJouni Malinen 	}
610cc65965cSJouni Malinen 
6111b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
612cc65965cSJouni Malinen 		/*
613cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
614cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
615cc65965cSJouni Malinen 		 * been delivered.
616cc65965cSJouni Malinen 		 */
6171b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
618c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
619c46917bbSLuis R. Rodriguez 			  "PS wait for CAB frames timed out\n");
620cc65965cSJouni Malinen 	}
621cc65965cSJouni Malinen }
622cc65965cSJouni Malinen 
623cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
624cc65965cSJouni Malinen {
625cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
626c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
627cc65965cSJouni Malinen 
628cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
629cc65965cSJouni Malinen 
630cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
631ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
632ededf1f8SVasanthakumar Thiagarajan 	    && ieee80211_is_beacon(hdr->frame_control))
633cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
6341b04b930SSujith 	else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
635cc65965cSJouni Malinen 		 (ieee80211_is_data(hdr->frame_control) ||
636cc65965cSJouni Malinen 		  ieee80211_is_action(hdr->frame_control)) &&
637cc65965cSJouni Malinen 		 is_multicast_ether_addr(hdr->addr1) &&
638cc65965cSJouni Malinen 		 !ieee80211_has_moredata(hdr->frame_control)) {
639cc65965cSJouni Malinen 		/*
640cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
641cc65965cSJouni Malinen 		 * point.
642cc65965cSJouni Malinen 		 */
6431b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
644c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
645c46917bbSLuis R. Rodriguez 			  "All PS CAB frames received, back to sleep\n");
6461b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6479a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
6489a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
6491b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
650c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
651c46917bbSLuis R. Rodriguez 			  "Going back to sleep after having received "
652f643e51dSPavel Roskin 			  "PS-Poll data (0x%lx)\n",
6531b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
6541b04b930SSujith 					PS_WAIT_FOR_CAB |
6551b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6561b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
657cc65965cSJouni Malinen 	}
658cc65965cSJouni Malinen }
659cc65965cSJouni Malinen 
660b4afffc0SLuis R. Rodriguez static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
661b4afffc0SLuis R. Rodriguez 				    struct ath_softc *sc, struct sk_buff *skb,
6625ca42627SLuis R. Rodriguez 				    struct ieee80211_rx_status *rxs)
6639d64a3cfSJouni Malinen {
6649d64a3cfSJouni Malinen 	struct ieee80211_hdr *hdr;
6659d64a3cfSJouni Malinen 
6669d64a3cfSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
6679d64a3cfSJouni Malinen 
6689d64a3cfSJouni Malinen 	/* Send the frame to mac80211 */
6699d64a3cfSJouni Malinen 	if (is_multicast_ether_addr(hdr->addr1)) {
6709d64a3cfSJouni Malinen 		int i;
6719d64a3cfSJouni Malinen 		/*
6729d64a3cfSJouni Malinen 		 * Deliver broadcast/multicast frames to all suitable
6739d64a3cfSJouni Malinen 		 * virtual wiphys.
6749d64a3cfSJouni Malinen 		 */
6759d64a3cfSJouni Malinen 		/* TODO: filter based on channel configuration */
6769d64a3cfSJouni Malinen 		for (i = 0; i < sc->num_sec_wiphy; i++) {
6779d64a3cfSJouni Malinen 			struct ath_wiphy *aphy = sc->sec_wiphy[i];
6789d64a3cfSJouni Malinen 			struct sk_buff *nskb;
6799d64a3cfSJouni Malinen 			if (aphy == NULL)
6809d64a3cfSJouni Malinen 				continue;
6819d64a3cfSJouni Malinen 			nskb = skb_copy(skb, GFP_ATOMIC);
6825ca42627SLuis R. Rodriguez 			if (!nskb)
6835ca42627SLuis R. Rodriguez 				continue;
684f1d58c25SJohannes Berg 			ieee80211_rx(aphy->hw, nskb);
6859d64a3cfSJouni Malinen 		}
686f1d58c25SJohannes Berg 		ieee80211_rx(sc->hw, skb);
6875ca42627SLuis R. Rodriguez 	} else
6889d64a3cfSJouni Malinen 		/* Deliver unicast frames based on receiver address */
689b4afffc0SLuis R. Rodriguez 		ieee80211_rx(hw, skb);
6909d64a3cfSJouni Malinen }
6919d64a3cfSJouni Malinen 
692b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
693b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
694203c4805SLuis R. Rodriguez {
695b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
696203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
69727c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
698b5c80475SFelix Fietkau 	struct sk_buff *skb;
699b5c80475SFelix Fietkau 	struct ath_buf *bf;
700b5c80475SFelix Fietkau 	int ret;
701203c4805SLuis R. Rodriguez 
702b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
703b5c80475SFelix Fietkau 	if (!skb)
704b5c80475SFelix Fietkau 		return false;
705203c4805SLuis R. Rodriguez 
706b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
707b5c80475SFelix Fietkau 	BUG_ON(!bf);
708b5c80475SFelix Fietkau 
709ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
710b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
711b5c80475SFelix Fietkau 
712b5c80475SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
713ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
714ce9426d1SMing Lei 		/*let device gain the buffer again*/
715ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
716ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
717b5c80475SFelix Fietkau 		return false;
718ce9426d1SMing Lei 	}
719b5c80475SFelix Fietkau 
720b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
721b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
722b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
723b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
724b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
725b5c80475SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
726b5c80475SFelix Fietkau 		if (!skb)
727b5c80475SFelix Fietkau 			return true;
728b5c80475SFelix Fietkau 
729b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
730b5c80475SFelix Fietkau 		BUG_ON(!bf);
731b5c80475SFelix Fietkau 
732b5c80475SFelix Fietkau 		__skb_unlink(skb, &rx_edma->rx_fifo);
733b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
734b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
735083e3e8dSVasanthakumar Thiagarajan 		return true;
736b5c80475SFelix Fietkau 	}
737b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_buffers, skb);
738b5c80475SFelix Fietkau 
739b5c80475SFelix Fietkau 	return true;
740b5c80475SFelix Fietkau }
741b5c80475SFelix Fietkau 
742b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
743b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
744b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
745b5c80475SFelix Fietkau {
746b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
747b5c80475SFelix Fietkau 	struct sk_buff *skb;
748b5c80475SFelix Fietkau 	struct ath_buf *bf;
749b5c80475SFelix Fietkau 
750b5c80475SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype));
751b5c80475SFelix Fietkau 	skb = __skb_dequeue(&rx_edma->rx_buffers);
752b5c80475SFelix Fietkau 	if (!skb)
753b5c80475SFelix Fietkau 		return NULL;
754b5c80475SFelix Fietkau 
755b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
756b5c80475SFelix Fietkau 	ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
757b5c80475SFelix Fietkau 	return bf;
758b5c80475SFelix Fietkau }
759b5c80475SFelix Fietkau 
760b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
761b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
762b5c80475SFelix Fietkau {
763b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
764b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
765b5c80475SFelix Fietkau 	struct ath_desc *ds;
766b5c80475SFelix Fietkau 	struct ath_buf *bf;
767b5c80475SFelix Fietkau 	int ret;
768203c4805SLuis R. Rodriguez 
769203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
770203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
771b5c80475SFelix Fietkau 		return NULL;
772203c4805SLuis R. Rodriguez 	}
773203c4805SLuis R. Rodriguez 
774203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
775203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
776203c4805SLuis R. Rodriguez 
777203c4805SLuis R. Rodriguez 	/*
778203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
779203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
780203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
781203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
782203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
783203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
784203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
785203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
786203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
787203c4805SLuis R. Rodriguez 	 */
788b5c80475SFelix Fietkau 	ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
789b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
79029bffa96SFelix Fietkau 		struct ath_rx_status trs;
791203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
792203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
793203c4805SLuis R. Rodriguez 
79429bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
795203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
796203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
797b5c80475SFelix Fietkau 			return NULL;
798203c4805SLuis R. Rodriguez 		}
799203c4805SLuis R. Rodriguez 
800203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
801203c4805SLuis R. Rodriguez 
802203c4805SLuis R. Rodriguez 		/*
803203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
804203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
805203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
806203c4805SLuis R. Rodriguez 		 * set or not.
807203c4805SLuis R. Rodriguez 		 *
808203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
809203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
810203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
811203c4805SLuis R. Rodriguez 		 */
812203c4805SLuis R. Rodriguez 
813203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
814b5c80475SFelix Fietkau 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
815b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
816b5c80475SFelix Fietkau 			return NULL;
817203c4805SLuis R. Rodriguez 	}
818203c4805SLuis R. Rodriguez 
819b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
820b5c80475SFelix Fietkau 		return bf;
821203c4805SLuis R. Rodriguez 
822203c4805SLuis R. Rodriguez 	/*
823203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
824203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
825203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
826203c4805SLuis R. Rodriguez 	 */
827ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
828cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
829203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
830203c4805SLuis R. Rodriguez 
831b5c80475SFelix Fietkau 	return bf;
832b5c80475SFelix Fietkau }
833b5c80475SFelix Fietkau 
834d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
835d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
8369f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
837d435700fSSujith 			    struct ieee80211_rx_status *rxs,
838d435700fSSujith 			    struct ath_rx_status *rx_stats,
839d435700fSSujith 			    bool *decrypt_error)
840d435700fSSujith {
841d435700fSSujith 	struct ath_hw *ah = common->ah;
842d435700fSSujith 	__le16 fc;
843b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
844d435700fSSujith 
845d435700fSSujith 	fc = hdr->frame_control;
846d435700fSSujith 
847d435700fSSujith 	if (!rx_stats->rs_datalen)
848d435700fSSujith 		return false;
849d435700fSSujith         /*
850d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
851d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
852d435700fSSujith          * those frames.
853d435700fSSujith          */
854b7b1b512SVasanthakumar Thiagarajan 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
855d435700fSSujith 		return false;
856d435700fSSujith 
857d435700fSSujith 	/*
858d435700fSSujith 	 * rs_more indicates chained descriptors which can be used
859d435700fSSujith 	 * to link buffers together for a sort of scatter-gather
860d435700fSSujith 	 * operation.
861d435700fSSujith 	 * reject the frame, we don't support scatter-gather yet and
862d435700fSSujith 	 * the frame is probably corrupt anyway
863d435700fSSujith 	 */
864d435700fSSujith 	if (rx_stats->rs_more)
865d435700fSSujith 		return false;
866d435700fSSujith 
867d435700fSSujith 	/*
868d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
869d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
870d435700fSSujith 	 * rs_more will be false at the last element of the chained
871d435700fSSujith 	 * descriptors.
872d435700fSSujith 	 */
873d435700fSSujith 	if (rx_stats->rs_status != 0) {
874d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_CRC)
875d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
876d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
877d435700fSSujith 			return false;
878d435700fSSujith 
879d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
880d435700fSSujith 			*decrypt_error = true;
881d435700fSSujith 		} else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
882d435700fSSujith 			/*
88356363ddeSFelix Fietkau 			 * The MIC error bit is only valid if the frame
88456363ddeSFelix Fietkau 			 * is not a control frame or fragment, and it was
88556363ddeSFelix Fietkau 			 * decrypted using a valid TKIP key.
886d435700fSSujith 			 */
88756363ddeSFelix Fietkau 			if (!ieee80211_is_ctl(fc) &&
88856363ddeSFelix Fietkau 			    !ieee80211_has_morefrags(fc) &&
88956363ddeSFelix Fietkau 			    !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
89056363ddeSFelix Fietkau 			    test_bit(rx_stats->rs_keyix, common->tkip_keymap))
891d435700fSSujith 				rxs->flag |= RX_FLAG_MMIC_ERROR;
89256363ddeSFelix Fietkau 			else
89356363ddeSFelix Fietkau 				rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
894d435700fSSujith 		}
895d435700fSSujith 		/*
896d435700fSSujith 		 * Reject error frames with the exception of
897d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
898d435700fSSujith 		 * we also ignore the CRC error.
899d435700fSSujith 		 */
900d435700fSSujith 		if (ah->opmode == NL80211_IFTYPE_MONITOR) {
901d435700fSSujith 			if (rx_stats->rs_status &
902d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
903d435700fSSujith 			      ATH9K_RXERR_CRC))
904d435700fSSujith 				return false;
905d435700fSSujith 		} else {
906d435700fSSujith 			if (rx_stats->rs_status &
907d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
908d435700fSSujith 				return false;
909d435700fSSujith 			}
910d435700fSSujith 		}
911d435700fSSujith 	}
912d435700fSSujith 	return true;
913d435700fSSujith }
914d435700fSSujith 
915d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
916d435700fSSujith 			      struct ieee80211_hw *hw,
917d435700fSSujith 			      struct ath_rx_status *rx_stats,
9189f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
919d435700fSSujith {
920d435700fSSujith 	struct ieee80211_supported_band *sband;
921d435700fSSujith 	enum ieee80211_band band;
922d435700fSSujith 	unsigned int i = 0;
923d435700fSSujith 
924d435700fSSujith 	band = hw->conf.channel->band;
925d435700fSSujith 	sband = hw->wiphy->bands[band];
926d435700fSSujith 
927d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
928d435700fSSujith 		/* HT rate */
929d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
930d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
931d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
932d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
933d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
934d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
935d435700fSSujith 		return 0;
936d435700fSSujith 	}
937d435700fSSujith 
938d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
939d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
940d435700fSSujith 			rxs->rate_idx = i;
941d435700fSSujith 			return 0;
942d435700fSSujith 		}
943d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
944d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
945d435700fSSujith 			rxs->rate_idx = i;
946d435700fSSujith 			return 0;
947d435700fSSujith 		}
948d435700fSSujith 	}
949d435700fSSujith 
950d435700fSSujith 	/*
951d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
952d435700fSSujith 	 * because hardware has already validated this frame as OK.
953d435700fSSujith 	 */
954d435700fSSujith 	ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
955d435700fSSujith 		  "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
956d435700fSSujith 
957d435700fSSujith 	return -EINVAL;
958d435700fSSujith }
959d435700fSSujith 
960d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
961d435700fSSujith 			       struct ieee80211_hw *hw,
9629f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
963d435700fSSujith 			       struct ath_rx_status *rx_stats)
964d435700fSSujith {
965d435700fSSujith 	struct ath_hw *ah = common->ah;
966d435700fSSujith 	struct ieee80211_sta *sta;
967d435700fSSujith 	struct ath_node *an;
968d435700fSSujith 	int last_rssi = ATH_RSSI_DUMMY_MARKER;
969d435700fSSujith 	__le16 fc;
970d435700fSSujith 
971d435700fSSujith 	fc = hdr->frame_control;
972d435700fSSujith 
973d435700fSSujith 	rcu_read_lock();
974d435700fSSujith 	/*
975d435700fSSujith 	 * XXX: use ieee80211_find_sta! This requires quite a bit of work
976d435700fSSujith 	 * under the current ath9k virtual wiphy implementation as we have
977d435700fSSujith 	 * no way of tying a vif to wiphy. Typically vifs are attached to
978d435700fSSujith 	 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
979d435700fSSujith 	 * wiphy you'd have to iterate over every wiphy and each sdata.
980d435700fSSujith 	 */
981d435700fSSujith 	sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
982d435700fSSujith 	if (sta) {
983d435700fSSujith 		an = (struct ath_node *) sta->drv_priv;
984d435700fSSujith 		if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
985d435700fSSujith 		   !rx_stats->rs_moreaggr)
986d435700fSSujith 			ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
987d435700fSSujith 		last_rssi = an->last_rssi;
988d435700fSSujith 	}
989d435700fSSujith 	rcu_read_unlock();
990d435700fSSujith 
991d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
992d435700fSSujith 		rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
993d435700fSSujith 					      ATH_RSSI_EP_MULTIPLIER);
994d435700fSSujith 	if (rx_stats->rs_rssi < 0)
995d435700fSSujith 		rx_stats->rs_rssi = 0;
996d435700fSSujith 
997d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
998d435700fSSujith 	if (ieee80211_is_beacon(fc))
999d435700fSSujith 		ah->stats.avgbrssi = rx_stats->rs_rssi;
1000d435700fSSujith }
1001d435700fSSujith 
1002d435700fSSujith /*
1003d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
1004d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
1005d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
1006d435700fSSujith  */
1007d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
1008d435700fSSujith 				   struct ieee80211_hw *hw,
10099f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
1010d435700fSSujith 				   struct ath_rx_status *rx_stats,
1011d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
1012d435700fSSujith 				   bool *decrypt_error)
1013d435700fSSujith {
1014d435700fSSujith 	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
1015d435700fSSujith 
1016d435700fSSujith 	/*
1017d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
1018d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
1019d435700fSSujith 	 */
10209f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
1021d435700fSSujith 		return -EINVAL;
1022d435700fSSujith 
10239f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
1024d435700fSSujith 
10259f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1026d435700fSSujith 		return -EINVAL;
1027d435700fSSujith 
1028d435700fSSujith 	rx_status->band = hw->conf.channel->band;
1029d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
1030d435700fSSujith 	rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1031d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
1032d435700fSSujith 	rx_status->flag |= RX_FLAG_TSFT;
1033d435700fSSujith 
1034d435700fSSujith 	return 0;
1035d435700fSSujith }
1036d435700fSSujith 
1037d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
1038d435700fSSujith 				     struct sk_buff *skb,
1039d435700fSSujith 				     struct ath_rx_status *rx_stats,
1040d435700fSSujith 				     struct ieee80211_rx_status *rxs,
1041d435700fSSujith 				     bool decrypt_error)
1042d435700fSSujith {
1043d435700fSSujith 	struct ath_hw *ah = common->ah;
1044d435700fSSujith 	struct ieee80211_hdr *hdr;
1045d435700fSSujith 	int hdrlen, padpos, padsize;
1046d435700fSSujith 	u8 keyix;
1047d435700fSSujith 	__le16 fc;
1048d435700fSSujith 
1049d435700fSSujith 	/* see if any padding is done by the hw and remove it */
1050d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1051d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1052d435700fSSujith 	fc = hdr->frame_control;
1053d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
1054d435700fSSujith 
1055d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1056d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1057d435700fSSujith 	 * padsize would take into account odd header lengths:
1058d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1059d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1060d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1061d435700fSSujith 	 * not try to remove padding from short control frames that do
1062d435700fSSujith 	 * not have payload. */
1063d435700fSSujith 	padsize = padpos & 3;
1064d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1065d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1066d435700fSSujith 		skb_pull(skb, padsize);
1067d435700fSSujith 	}
1068d435700fSSujith 
1069d435700fSSujith 	keyix = rx_stats->rs_keyix;
1070d435700fSSujith 
1071d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1072d435700fSSujith 	    ieee80211_has_protected(fc)) {
1073d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1074d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1075d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1076d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1077d435700fSSujith 
1078d435700fSSujith 		if (test_bit(keyix, common->keymap))
1079d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1080d435700fSSujith 	}
1081d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1082d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1083d435700fSSujith 	    ieee80211_is_mgmt(fc))
1084d435700fSSujith 		/* Use software decrypt for management frames. */
1085d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1086d435700fSSujith }
1087b5c80475SFelix Fietkau 
1088*102885a5SVasanthakumar Thiagarajan static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1089*102885a5SVasanthakumar Thiagarajan 				      struct ath_hw_antcomb_conf ant_conf,
1090*102885a5SVasanthakumar Thiagarajan 				      int main_rssi_avg)
1091*102885a5SVasanthakumar Thiagarajan {
1092*102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt = 0;
1093*102885a5SVasanthakumar Thiagarajan 
1094*102885a5SVasanthakumar Thiagarajan 	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1095*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna2 = main_rssi_avg;
1096*102885a5SVasanthakumar Thiagarajan 	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1097*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna1 = main_rssi_avg;
1098*102885a5SVasanthakumar Thiagarajan 
1099*102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1100*102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1101*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1102*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1103*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1104*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1105*102885a5SVasanthakumar Thiagarajan 		break;
1106*102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1107*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1108*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1109*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1110*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1111*102885a5SVasanthakumar Thiagarajan 		break;
1112*102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1113*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1114*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1115*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1116*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1117*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1118*102885a5SVasanthakumar Thiagarajan 		break;
1119*102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1120*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1121*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1122*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1123*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1124*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1125*102885a5SVasanthakumar Thiagarajan 		break;
1126*102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1127*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1128*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1129*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1130*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1131*102885a5SVasanthakumar Thiagarajan 		break;
1132*102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1133*102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1134*102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1135*102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1136*102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1137*102885a5SVasanthakumar Thiagarajan 		break;
1138*102885a5SVasanthakumar Thiagarajan 	default:
1139*102885a5SVasanthakumar Thiagarajan 		break;
1140*102885a5SVasanthakumar Thiagarajan 	}
1141*102885a5SVasanthakumar Thiagarajan }
1142*102885a5SVasanthakumar Thiagarajan 
1143*102885a5SVasanthakumar Thiagarajan static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1144*102885a5SVasanthakumar Thiagarajan 				struct ath_hw_antcomb_conf *div_ant_conf,
1145*102885a5SVasanthakumar Thiagarajan 				int main_rssi_avg, int alt_rssi_avg,
1146*102885a5SVasanthakumar Thiagarajan 				int alt_ratio)
1147*102885a5SVasanthakumar Thiagarajan {
1148*102885a5SVasanthakumar Thiagarajan 	/* alt_good */
1149*102885a5SVasanthakumar Thiagarajan 	switch (antcomb->quick_scan_cnt) {
1150*102885a5SVasanthakumar Thiagarajan 	case 0:
1151*102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1152*102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1153*102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1154*102885a5SVasanthakumar Thiagarajan 		break;
1155*102885a5SVasanthakumar Thiagarajan 	case 1:
1156*102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1157*102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1158*102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1159*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1160*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_second = alt_rssi_avg;
1161*102885a5SVasanthakumar Thiagarajan 
1162*102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1163*102885a5SVasanthakumar Thiagarajan 			/* main is LNA1 */
1164*102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1165*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1166*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1167*102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1168*102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1169*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1170*102885a5SVasanthakumar Thiagarajan 			else
1171*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1172*102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1173*102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1174*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1175*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1176*102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1177*102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1178*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1179*102885a5SVasanthakumar Thiagarajan 			else
1180*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1181*102885a5SVasanthakumar Thiagarajan 		} else {
1182*102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1183*102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1184*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1185*102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1186*102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1187*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1188*102885a5SVasanthakumar Thiagarajan 			else
1189*102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1190*102885a5SVasanthakumar Thiagarajan 		}
1191*102885a5SVasanthakumar Thiagarajan 		break;
1192*102885a5SVasanthakumar Thiagarajan 	case 2:
1193*102885a5SVasanthakumar Thiagarajan 		antcomb->alt_good = false;
1194*102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = false;
1195*102885a5SVasanthakumar Thiagarajan 		antcomb->scan = false;
1196*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1197*102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_third = alt_rssi_avg;
1198*102885a5SVasanthakumar Thiagarajan 
1199*102885a5SVasanthakumar Thiagarajan 		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1200*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1201*102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1202*102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA2)
1203*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1204*102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1205*102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1206*102885a5SVasanthakumar Thiagarajan 			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1207*102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna2 = main_rssi_avg;
1208*102885a5SVasanthakumar Thiagarajan 			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1209*102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna1 = main_rssi_avg;
1210*102885a5SVasanthakumar Thiagarajan 		}
1211*102885a5SVasanthakumar Thiagarajan 
1212*102885a5SVasanthakumar Thiagarajan 		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1213*102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1214*102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1215*102885a5SVasanthakumar Thiagarajan 		else
1216*102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1217*102885a5SVasanthakumar Thiagarajan 
1218*102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1219*102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1220*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1221*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1222*102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1223*102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1224*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1225*102885a5SVasanthakumar Thiagarajan 			else
1226*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1227*102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1228*102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1229*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1230*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1231*102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1232*102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1233*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1234*102885a5SVasanthakumar Thiagarajan 			else
1235*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1236*102885a5SVasanthakumar Thiagarajan 		} else {
1237*102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1238*102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1239*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1240*102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1241*102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1242*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1243*102885a5SVasanthakumar Thiagarajan 			else
1244*102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1245*102885a5SVasanthakumar Thiagarajan 		}
1246*102885a5SVasanthakumar Thiagarajan 
1247*102885a5SVasanthakumar Thiagarajan 		/* set alt to the conf with maximun ratio */
1248*102885a5SVasanthakumar Thiagarajan 		if (antcomb->first_ratio && antcomb->second_ratio) {
1249*102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_second > antcomb->rssi_third) {
1250*102885a5SVasanthakumar Thiagarajan 				/* first alt*/
1251*102885a5SVasanthakumar Thiagarajan 				if ((antcomb->first_quick_scan_conf ==
1252*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA1) ||
1253*102885a5SVasanthakumar Thiagarajan 				    (antcomb->first_quick_scan_conf ==
1254*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2))
1255*102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2*/
1256*102885a5SVasanthakumar Thiagarajan 					if (div_ant_conf->main_lna_conf ==
1257*102885a5SVasanthakumar Thiagarajan 					    ATH_ANT_DIV_COMB_LNA2)
1258*102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1259*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1260*102885a5SVasanthakumar Thiagarajan 					else
1261*102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1262*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1263*102885a5SVasanthakumar Thiagarajan 				else
1264*102885a5SVasanthakumar Thiagarajan 					/* Set alt to A+B or A-B */
1265*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1266*102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1267*102885a5SVasanthakumar Thiagarajan 			} else if ((antcomb->second_quick_scan_conf ==
1268*102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA1) ||
1269*102885a5SVasanthakumar Thiagarajan 				   (antcomb->second_quick_scan_conf ==
1270*102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA2)) {
1271*102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1272*102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1273*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1274*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1275*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1276*102885a5SVasanthakumar Thiagarajan 				else
1277*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1278*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1279*102885a5SVasanthakumar Thiagarajan 			} else {
1280*102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1281*102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1282*102885a5SVasanthakumar Thiagarajan 					antcomb->second_quick_scan_conf;
1283*102885a5SVasanthakumar Thiagarajan 			}
1284*102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->first_ratio) {
1285*102885a5SVasanthakumar Thiagarajan 			/* first alt */
1286*102885a5SVasanthakumar Thiagarajan 			if ((antcomb->first_quick_scan_conf ==
1287*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1288*102885a5SVasanthakumar Thiagarajan 			    (antcomb->first_quick_scan_conf ==
1289*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1290*102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2 */
1291*102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1292*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1293*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1294*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1295*102885a5SVasanthakumar Thiagarajan 				else
1296*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1297*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1298*102885a5SVasanthakumar Thiagarajan 			else
1299*102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1300*102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1301*102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1302*102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->second_ratio) {
1303*102885a5SVasanthakumar Thiagarajan 				/* second alt */
1304*102885a5SVasanthakumar Thiagarajan 			if ((antcomb->second_quick_scan_conf ==
1305*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1306*102885a5SVasanthakumar Thiagarajan 			    (antcomb->second_quick_scan_conf ==
1307*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1308*102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1309*102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1310*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1311*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1312*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1313*102885a5SVasanthakumar Thiagarajan 				else
1314*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1315*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1316*102885a5SVasanthakumar Thiagarajan 			else
1317*102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1318*102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1319*102885a5SVasanthakumar Thiagarajan 						antcomb->second_quick_scan_conf;
1320*102885a5SVasanthakumar Thiagarajan 		} else {
1321*102885a5SVasanthakumar Thiagarajan 			/* main is largest */
1322*102885a5SVasanthakumar Thiagarajan 			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1323*102885a5SVasanthakumar Thiagarajan 			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1324*102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1325*102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1326*102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1327*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1328*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1329*102885a5SVasanthakumar Thiagarajan 				else
1330*102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1331*102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1332*102885a5SVasanthakumar Thiagarajan 			else
1333*102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1334*102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf = antcomb->main_conf;
1335*102885a5SVasanthakumar Thiagarajan 		}
1336*102885a5SVasanthakumar Thiagarajan 		break;
1337*102885a5SVasanthakumar Thiagarajan 	default:
1338*102885a5SVasanthakumar Thiagarajan 		break;
1339*102885a5SVasanthakumar Thiagarajan 	}
1340*102885a5SVasanthakumar Thiagarajan }
1341*102885a5SVasanthakumar Thiagarajan 
1342*102885a5SVasanthakumar Thiagarajan void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf)
1343*102885a5SVasanthakumar Thiagarajan {
1344*102885a5SVasanthakumar Thiagarajan 	/* Adjust the fast_div_bias based on main and alt lna conf */
1345*102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
1346*102885a5SVasanthakumar Thiagarajan 	case (0x01): /* A-B LNA2 */
1347*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1348*102885a5SVasanthakumar Thiagarajan 		break;
1349*102885a5SVasanthakumar Thiagarajan 	case (0x02): /* A-B LNA1 */
1350*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1351*102885a5SVasanthakumar Thiagarajan 		break;
1352*102885a5SVasanthakumar Thiagarajan 	case (0x03): /* A-B A+B */
1353*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1354*102885a5SVasanthakumar Thiagarajan 		break;
1355*102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1356*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1357*102885a5SVasanthakumar Thiagarajan 		break;
1358*102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1359*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x2;
1360*102885a5SVasanthakumar Thiagarajan 		break;
1361*102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1362*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1363*102885a5SVasanthakumar Thiagarajan 		break;
1364*102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1365*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1366*102885a5SVasanthakumar Thiagarajan 		break;
1367*102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1368*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x0;
1369*102885a5SVasanthakumar Thiagarajan 		break;
1370*102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1371*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1372*102885a5SVasanthakumar Thiagarajan 		break;
1373*102885a5SVasanthakumar Thiagarajan 	case (0x30): /* A+B A-B */
1374*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1375*102885a5SVasanthakumar Thiagarajan 		break;
1376*102885a5SVasanthakumar Thiagarajan 	case (0x31): /* A+B LNA2 */
1377*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1378*102885a5SVasanthakumar Thiagarajan 		break;
1379*102885a5SVasanthakumar Thiagarajan 	case (0x32): /* A+B LNA1 */
1380*102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1381*102885a5SVasanthakumar Thiagarajan 		break;
1382*102885a5SVasanthakumar Thiagarajan 	default:
1383*102885a5SVasanthakumar Thiagarajan 		break;
1384*102885a5SVasanthakumar Thiagarajan 	}
1385*102885a5SVasanthakumar Thiagarajan }
1386*102885a5SVasanthakumar Thiagarajan 
1387*102885a5SVasanthakumar Thiagarajan /* Antenna diversity and combining */
1388*102885a5SVasanthakumar Thiagarajan static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1389*102885a5SVasanthakumar Thiagarajan {
1390*102885a5SVasanthakumar Thiagarajan 	struct ath_hw_antcomb_conf div_ant_conf;
1391*102885a5SVasanthakumar Thiagarajan 	struct ath_ant_comb *antcomb = &sc->ant_comb;
1392*102885a5SVasanthakumar Thiagarajan 	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
1393*102885a5SVasanthakumar Thiagarajan 	int curr_main_set, curr_bias;
1394*102885a5SVasanthakumar Thiagarajan 	int main_rssi = rs->rs_rssi_ctl0;
1395*102885a5SVasanthakumar Thiagarajan 	int alt_rssi = rs->rs_rssi_ctl1;
1396*102885a5SVasanthakumar Thiagarajan 	int rx_ant_conf,  main_ant_conf;
1397*102885a5SVasanthakumar Thiagarajan 	bool short_scan = false;
1398*102885a5SVasanthakumar Thiagarajan 
1399*102885a5SVasanthakumar Thiagarajan 	rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1400*102885a5SVasanthakumar Thiagarajan 		       ATH_ANT_RX_MASK;
1401*102885a5SVasanthakumar Thiagarajan 	main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1402*102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_RX_MASK;
1403*102885a5SVasanthakumar Thiagarajan 
1404*102885a5SVasanthakumar Thiagarajan 	/* Record packet only when alt_rssi is positive */
1405*102885a5SVasanthakumar Thiagarajan 	if (alt_rssi > 0) {
1406*102885a5SVasanthakumar Thiagarajan 		antcomb->total_pkt_count++;
1407*102885a5SVasanthakumar Thiagarajan 		antcomb->main_total_rssi += main_rssi;
1408*102885a5SVasanthakumar Thiagarajan 		antcomb->alt_total_rssi  += alt_rssi;
1409*102885a5SVasanthakumar Thiagarajan 		if (main_ant_conf == rx_ant_conf)
1410*102885a5SVasanthakumar Thiagarajan 			antcomb->main_recv_cnt++;
1411*102885a5SVasanthakumar Thiagarajan 		else
1412*102885a5SVasanthakumar Thiagarajan 			antcomb->alt_recv_cnt++;
1413*102885a5SVasanthakumar Thiagarajan 	}
1414*102885a5SVasanthakumar Thiagarajan 
1415*102885a5SVasanthakumar Thiagarajan 	/* Short scan check */
1416*102885a5SVasanthakumar Thiagarajan 	if (antcomb->scan && antcomb->alt_good) {
1417*102885a5SVasanthakumar Thiagarajan 		if (time_after(jiffies, antcomb->scan_start_time +
1418*102885a5SVasanthakumar Thiagarajan 		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1419*102885a5SVasanthakumar Thiagarajan 			short_scan = true;
1420*102885a5SVasanthakumar Thiagarajan 		else
1421*102885a5SVasanthakumar Thiagarajan 			if (antcomb->total_pkt_count ==
1422*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1423*102885a5SVasanthakumar Thiagarajan 				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1424*102885a5SVasanthakumar Thiagarajan 					    antcomb->total_pkt_count);
1425*102885a5SVasanthakumar Thiagarajan 				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1426*102885a5SVasanthakumar Thiagarajan 					short_scan = true;
1427*102885a5SVasanthakumar Thiagarajan 			}
1428*102885a5SVasanthakumar Thiagarajan 	}
1429*102885a5SVasanthakumar Thiagarajan 
1430*102885a5SVasanthakumar Thiagarajan 	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1431*102885a5SVasanthakumar Thiagarajan 	    rs->rs_moreaggr) && !short_scan)
1432*102885a5SVasanthakumar Thiagarajan 		return;
1433*102885a5SVasanthakumar Thiagarajan 
1434*102885a5SVasanthakumar Thiagarajan 	if (antcomb->total_pkt_count) {
1435*102885a5SVasanthakumar Thiagarajan 		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1436*102885a5SVasanthakumar Thiagarajan 			     antcomb->total_pkt_count);
1437*102885a5SVasanthakumar Thiagarajan 		main_rssi_avg = (antcomb->main_total_rssi /
1438*102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1439*102885a5SVasanthakumar Thiagarajan 		alt_rssi_avg = (antcomb->alt_total_rssi /
1440*102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1441*102885a5SVasanthakumar Thiagarajan 	}
1442*102885a5SVasanthakumar Thiagarajan 
1443*102885a5SVasanthakumar Thiagarajan 
1444*102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1445*102885a5SVasanthakumar Thiagarajan 	curr_alt_set = div_ant_conf.alt_lna_conf;
1446*102885a5SVasanthakumar Thiagarajan 	curr_main_set = div_ant_conf.main_lna_conf;
1447*102885a5SVasanthakumar Thiagarajan 	curr_bias = div_ant_conf.fast_div_bias;
1448*102885a5SVasanthakumar Thiagarajan 
1449*102885a5SVasanthakumar Thiagarajan 	antcomb->count++;
1450*102885a5SVasanthakumar Thiagarajan 
1451*102885a5SVasanthakumar Thiagarajan 	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1452*102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1453*102885a5SVasanthakumar Thiagarajan 			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1454*102885a5SVasanthakumar Thiagarajan 						  main_rssi_avg);
1455*102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = true;
1456*102885a5SVasanthakumar Thiagarajan 		} else {
1457*102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = false;
1458*102885a5SVasanthakumar Thiagarajan 		}
1459*102885a5SVasanthakumar Thiagarajan 
1460*102885a5SVasanthakumar Thiagarajan 		antcomb->count = 0;
1461*102885a5SVasanthakumar Thiagarajan 		antcomb->scan = true;
1462*102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = true;
1463*102885a5SVasanthakumar Thiagarajan 	}
1464*102885a5SVasanthakumar Thiagarajan 
1465*102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan) {
1466*102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1467*102885a5SVasanthakumar Thiagarajan 			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1468*102885a5SVasanthakumar Thiagarajan 				/* Switch main and alt LNA */
1469*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1470*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1471*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1472*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1473*102885a5SVasanthakumar Thiagarajan 			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1474*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1475*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1476*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1477*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1478*102885a5SVasanthakumar Thiagarajan 			}
1479*102885a5SVasanthakumar Thiagarajan 
1480*102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1481*102885a5SVasanthakumar Thiagarajan 		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1482*102885a5SVasanthakumar Thiagarajan 			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1483*102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1484*102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1485*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1486*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1487*102885a5SVasanthakumar Thiagarajan 			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1488*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1489*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1490*102885a5SVasanthakumar Thiagarajan 
1491*102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1492*102885a5SVasanthakumar Thiagarajan 		}
1493*102885a5SVasanthakumar Thiagarajan 
1494*102885a5SVasanthakumar Thiagarajan 		if ((alt_rssi_avg < (main_rssi_avg +
1495*102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
1496*102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1497*102885a5SVasanthakumar Thiagarajan 	}
1498*102885a5SVasanthakumar Thiagarajan 
1499*102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan_not_start) {
1500*102885a5SVasanthakumar Thiagarajan 		switch (curr_alt_set) {
1501*102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA2:
1502*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1503*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = main_rssi_avg;
1504*102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1505*102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1506*102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf =
1507*102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1;
1508*102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1509*102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1510*102885a5SVasanthakumar Thiagarajan 			break;
1511*102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1:
1512*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1513*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = main_rssi_avg;
1514*102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1515*102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1516*102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1517*102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1518*102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1519*102885a5SVasanthakumar Thiagarajan 			break;
1520*102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1521*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_add = alt_rssi_avg;
1522*102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1523*102885a5SVasanthakumar Thiagarajan 			/* set to A-B */
1524*102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf =
1525*102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1526*102885a5SVasanthakumar Thiagarajan 			break;
1527*102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1528*102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_sub = alt_rssi_avg;
1529*102885a5SVasanthakumar Thiagarajan 			antcomb->scan = false;
1530*102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_lna2 >
1531*102885a5SVasanthakumar Thiagarajan 			    (antcomb->rssi_lna1 +
1532*102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1533*102885a5SVasanthakumar Thiagarajan 				/* use LNA2 as main LNA */
1534*102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1535*102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1536*102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1537*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1538*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1539*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1540*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1541*102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1542*102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1543*102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1544*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1545*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1546*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1547*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1548*102885a5SVasanthakumar Thiagarajan 				} else {
1549*102885a5SVasanthakumar Thiagarajan 					/* set to LNA1 */
1550*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1551*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1552*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1553*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1554*102885a5SVasanthakumar Thiagarajan 				}
1555*102885a5SVasanthakumar Thiagarajan 			} else {
1556*102885a5SVasanthakumar Thiagarajan 				/* use LNA1 as main LNA */
1557*102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1558*102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1559*102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1560*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1561*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1562*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1563*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1564*102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1565*102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1566*102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1567*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1568*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1569*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1570*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1571*102885a5SVasanthakumar Thiagarajan 				} else {
1572*102885a5SVasanthakumar Thiagarajan 					/* set to LNA2 */
1573*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1574*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1575*102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1576*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1577*102885a5SVasanthakumar Thiagarajan 				}
1578*102885a5SVasanthakumar Thiagarajan 			}
1579*102885a5SVasanthakumar Thiagarajan 			break;
1580*102885a5SVasanthakumar Thiagarajan 		default:
1581*102885a5SVasanthakumar Thiagarajan 			break;
1582*102885a5SVasanthakumar Thiagarajan 		}
1583*102885a5SVasanthakumar Thiagarajan 	} else {
1584*102885a5SVasanthakumar Thiagarajan 		if (!antcomb->alt_good) {
1585*102885a5SVasanthakumar Thiagarajan 			antcomb->scan_not_start = false;
1586*102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1587*102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1588*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1589*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1590*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1591*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1592*102885a5SVasanthakumar Thiagarajan 			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1593*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1594*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1595*102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1596*102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1597*102885a5SVasanthakumar Thiagarajan 			}
1598*102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1599*102885a5SVasanthakumar Thiagarajan 		}
1600*102885a5SVasanthakumar Thiagarajan 	}
1601*102885a5SVasanthakumar Thiagarajan 
1602*102885a5SVasanthakumar Thiagarajan 	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1603*102885a5SVasanthakumar Thiagarajan 					   main_rssi_avg, alt_rssi_avg,
1604*102885a5SVasanthakumar Thiagarajan 					   alt_ratio);
1605*102885a5SVasanthakumar Thiagarajan 
1606*102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt++;
1607*102885a5SVasanthakumar Thiagarajan 
1608*102885a5SVasanthakumar Thiagarajan div_comb_done:
1609*102885a5SVasanthakumar Thiagarajan 	ath_ant_div_conf_fast_divbias(&div_ant_conf);
1610*102885a5SVasanthakumar Thiagarajan 
1611*102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1612*102885a5SVasanthakumar Thiagarajan 
1613*102885a5SVasanthakumar Thiagarajan 	antcomb->scan_start_time = jiffies;
1614*102885a5SVasanthakumar Thiagarajan 	antcomb->total_pkt_count = 0;
1615*102885a5SVasanthakumar Thiagarajan 	antcomb->main_total_rssi = 0;
1616*102885a5SVasanthakumar Thiagarajan 	antcomb->alt_total_rssi = 0;
1617*102885a5SVasanthakumar Thiagarajan 	antcomb->main_recv_cnt = 0;
1618*102885a5SVasanthakumar Thiagarajan 	antcomb->alt_recv_cnt = 0;
1619*102885a5SVasanthakumar Thiagarajan }
1620*102885a5SVasanthakumar Thiagarajan 
1621b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1622b5c80475SFelix Fietkau {
1623b5c80475SFelix Fietkau 	struct ath_buf *bf;
1624b5c80475SFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb;
1625b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1626b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1627b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
1628b5c80475SFelix Fietkau 	/*
1629b5c80475SFelix Fietkau 	 * The hw can techncically differ from common->hw when using ath9k
1630b5c80475SFelix Fietkau 	 * virtual wiphy so to account for that we iterate over the active
1631b5c80475SFelix Fietkau 	 * wiphys and find the appropriate wiphy and therefore hw.
1632b5c80475SFelix Fietkau 	 */
1633b5c80475SFelix Fietkau 	struct ieee80211_hw *hw = NULL;
1634b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1635b5c80475SFelix Fietkau 	int retval;
1636b5c80475SFelix Fietkau 	bool decrypt_error = false;
1637b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1638b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1639b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1640b5c80475SFelix Fietkau 	int dma_type;
16415c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1642a6d2055bSFelix Fietkau 	u64 tsf = 0;
1643a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
1644b5c80475SFelix Fietkau 
1645b5c80475SFelix Fietkau 	if (edma)
1646b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
164756824223SMing Lei 	else
164856824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1649b5c80475SFelix Fietkau 
1650b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1651b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
1652b5c80475SFelix Fietkau 
1653a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1654a6d2055bSFelix Fietkau 	tsf_lower = tsf & 0xffffffff;
1655a6d2055bSFelix Fietkau 
1656b5c80475SFelix Fietkau 	do {
1657b5c80475SFelix Fietkau 		/* If handling rx interrupt and flush is in progress => exit */
1658b5c80475SFelix Fietkau 		if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1659b5c80475SFelix Fietkau 			break;
1660b5c80475SFelix Fietkau 
1661b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1662b5c80475SFelix Fietkau 		if (edma)
1663b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1664b5c80475SFelix Fietkau 		else
1665b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1666b5c80475SFelix Fietkau 
1667b5c80475SFelix Fietkau 		if (!bf)
1668b5c80475SFelix Fietkau 			break;
1669b5c80475SFelix Fietkau 
1670b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1671b5c80475SFelix Fietkau 		if (!skb)
1672b5c80475SFelix Fietkau 			continue;
1673b5c80475SFelix Fietkau 
16745c6dd921SVasanthakumar Thiagarajan 		hdr = (struct ieee80211_hdr *) (skb->data + rx_status_len);
16755ca42627SLuis R. Rodriguez 		rxs =  IEEE80211_SKB_RXCB(skb);
16765ca42627SLuis R. Rodriguez 
1677b4afffc0SLuis R. Rodriguez 		hw = ath_get_virt_hw(sc, hdr);
1678b4afffc0SLuis R. Rodriguez 
167929bffa96SFelix Fietkau 		ath_debug_stat_rx(sc, &rs);
16801395d3f0SSujith 
1681203c4805SLuis R. Rodriguez 		/*
1682203c4805SLuis R. Rodriguez 		 * If we're asked to flush receive queue, directly
1683203c4805SLuis R. Rodriguez 		 * chain it back at the queue without processing it.
1684203c4805SLuis R. Rodriguez 		 */
1685203c4805SLuis R. Rodriguez 		if (flush)
1686203c4805SLuis R. Rodriguez 			goto requeue;
1687203c4805SLuis R. Rodriguez 
1688c8f3b721SJan Friedrich 		retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1689c8f3b721SJan Friedrich 						 rxs, &decrypt_error);
1690c8f3b721SJan Friedrich 		if (retval)
1691c8f3b721SJan Friedrich 			goto requeue;
1692c8f3b721SJan Friedrich 
1693a6d2055bSFelix Fietkau 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1694a6d2055bSFelix Fietkau 		if (rs.rs_tstamp > tsf_lower &&
1695a6d2055bSFelix Fietkau 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1696a6d2055bSFelix Fietkau 			rxs->mactime -= 0x100000000ULL;
1697a6d2055bSFelix Fietkau 
1698a6d2055bSFelix Fietkau 		if (rs.rs_tstamp < tsf_lower &&
1699a6d2055bSFelix Fietkau 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1700a6d2055bSFelix Fietkau 			rxs->mactime += 0x100000000ULL;
1701a6d2055bSFelix Fietkau 
1702203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1703203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1704cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1705203c4805SLuis R. Rodriguez 
1706203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1707203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1708203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1709203c4805SLuis R. Rodriguez 		 * processing. */
1710203c4805SLuis R. Rodriguez 		if (!requeue_skb)
1711203c4805SLuis R. Rodriguez 			goto requeue;
1712203c4805SLuis R. Rodriguez 
1713203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1714203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1715cc861f74SLuis R. Rodriguez 				 common->rx_bufsize,
1716b5c80475SFelix Fietkau 				 dma_type);
1717203c4805SLuis R. Rodriguez 
1718b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1719b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1720b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1721203c4805SLuis R. Rodriguez 
1722d435700fSSujith 		ath9k_rx_skb_postprocess(common, skb, &rs,
1723c9b14170SLuis R. Rodriguez 					 rxs, decrypt_error);
1724203c4805SLuis R. Rodriguez 
1725203c4805SLuis R. Rodriguez 		/* We will now give hardware our shiny new allocated skb */
1726203c4805SLuis R. Rodriguez 		bf->bf_mpdu = requeue_skb;
1727203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1728cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
1729b5c80475SFelix Fietkau 						 dma_type);
1730203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
1731203c4805SLuis R. Rodriguez 			  bf->bf_buf_addr))) {
1732203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(requeue_skb);
1733203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
1734c46917bbSLuis R. Rodriguez 			ath_print(common, ATH_DBG_FATAL,
1735203c4805SLuis R. Rodriguez 				  "dma_mapping_error() on RX\n");
17365ca42627SLuis R. Rodriguez 			ath_rx_send_to_mac80211(hw, sc, skb, rxs);
1737203c4805SLuis R. Rodriguez 			break;
1738203c4805SLuis R. Rodriguez 		}
1739203c4805SLuis R. Rodriguez 		bf->bf_dmacontext = bf->bf_buf_addr;
1740203c4805SLuis R. Rodriguez 
1741203c4805SLuis R. Rodriguez 		/*
1742203c4805SLuis R. Rodriguez 		 * change the default rx antenna if rx diversity chooses the
1743203c4805SLuis R. Rodriguez 		 * other antenna 3 times in a row.
1744203c4805SLuis R. Rodriguez 		 */
174529bffa96SFelix Fietkau 		if (sc->rx.defant != rs.rs_antenna) {
1746203c4805SLuis R. Rodriguez 			if (++sc->rx.rxotherant >= 3)
174729bffa96SFelix Fietkau 				ath_setdefantenna(sc, rs.rs_antenna);
1748203c4805SLuis R. Rodriguez 		} else {
1749203c4805SLuis R. Rodriguez 			sc->rx.rxotherant = 0;
1750203c4805SLuis R. Rodriguez 		}
1751203c4805SLuis R. Rodriguez 
1752ededf1f8SVasanthakumar Thiagarajan 		if (unlikely(ath9k_check_auto_sleep(sc) ||
1753ededf1f8SVasanthakumar Thiagarajan 			     (sc->ps_flags & (PS_WAIT_FOR_BEACON |
17541b04b930SSujith 					      PS_WAIT_FOR_CAB |
1755ededf1f8SVasanthakumar Thiagarajan 					      PS_WAIT_FOR_PSPOLL_DATA))))
1756cc65965cSJouni Malinen 			ath_rx_ps(sc, skb);
1757cc65965cSJouni Malinen 
1758*102885a5SVasanthakumar Thiagarajan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1759*102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1760*102885a5SVasanthakumar Thiagarajan 
17615ca42627SLuis R. Rodriguez 		ath_rx_send_to_mac80211(hw, sc, skb, rxs);
1762cc65965cSJouni Malinen 
1763203c4805SLuis R. Rodriguez requeue:
1764b5c80475SFelix Fietkau 		if (edma) {
1765b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
1766b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1767b5c80475SFelix Fietkau 		} else {
1768203c4805SLuis R. Rodriguez 			list_move_tail(&bf->list, &sc->rx.rxbuf);
1769203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
1770b5c80475SFelix Fietkau 		}
1771203c4805SLuis R. Rodriguez 	} while (1);
1772203c4805SLuis R. Rodriguez 
1773203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
1774203c4805SLuis R. Rodriguez 
1775203c4805SLuis R. Rodriguez 	return 0;
1776203c4805SLuis R. Rodriguez }
1777