xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision 8ab2cd09fecc8819bbaee2d0fd8f3a092d866ce3)
1203c4805SLuis R. Rodriguez /*
2203c4805SLuis R. Rodriguez  * Copyright (c) 2008-2009 Atheros Communications Inc.
3203c4805SLuis R. Rodriguez  *
4203c4805SLuis R. Rodriguez  * Permission to use, copy, modify, and/or distribute this software for any
5203c4805SLuis R. Rodriguez  * purpose with or without fee is hereby granted, provided that the above
6203c4805SLuis R. Rodriguez  * copyright notice and this permission notice appear in all copies.
7203c4805SLuis R. Rodriguez  *
8203c4805SLuis R. Rodriguez  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9203c4805SLuis R. Rodriguez  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10203c4805SLuis R. Rodriguez  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11203c4805SLuis R. Rodriguez  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12203c4805SLuis R. Rodriguez  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13203c4805SLuis R. Rodriguez  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14203c4805SLuis R. Rodriguez  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15203c4805SLuis R. Rodriguez  */
16203c4805SLuis R. Rodriguez 
17203c4805SLuis R. Rodriguez #include "ath9k.h"
18b622a720SLuis R. Rodriguez #include "ar9003_mac.h"
19203c4805SLuis R. Rodriguez 
20b5c80475SFelix Fietkau #define SKB_CB_ATHBUF(__skb)	(*((struct ath_buf **)__skb->cb))
21b5c80475SFelix Fietkau 
22102885a5SVasanthakumar Thiagarajan static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
23102885a5SVasanthakumar Thiagarajan 					       int mindelta, int main_rssi_avg,
24102885a5SVasanthakumar Thiagarajan 					       int alt_rssi_avg, int pkt_count)
25102885a5SVasanthakumar Thiagarajan {
26102885a5SVasanthakumar Thiagarajan 	return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
27102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + maxdelta)) ||
28102885a5SVasanthakumar Thiagarajan 		(alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
29102885a5SVasanthakumar Thiagarajan }
30102885a5SVasanthakumar Thiagarajan 
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 */
12213b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
123203c4805SLuis R. Rodriguez 
124203c4805SLuis R. Rodriguez 	/* configure operational mode */
125203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
126203c4805SLuis R. Rodriguez 
127203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
128203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
129203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
130203c4805SLuis R. Rodriguez }
131203c4805SLuis R. Rodriguez 
132b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
133b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
134b5c80475SFelix Fietkau {
135b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
136b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
137b5c80475SFelix Fietkau 	struct sk_buff *skb;
138b5c80475SFelix Fietkau 	struct ath_buf *bf;
139b5c80475SFelix Fietkau 
140b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
141b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
142b5c80475SFelix Fietkau 		return false;
143b5c80475SFelix Fietkau 
144b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
145b5c80475SFelix Fietkau 	list_del_init(&bf->list);
146b5c80475SFelix Fietkau 
147b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
148b5c80475SFelix Fietkau 
149b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
150b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
151b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
152b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
153b5c80475SFelix Fietkau 
154b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
155b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
156b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
157b5c80475SFelix Fietkau 
158b5c80475SFelix Fietkau 	return true;
159b5c80475SFelix Fietkau }
160b5c80475SFelix Fietkau 
161b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
162b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
163b5c80475SFelix Fietkau {
164b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
165b5c80475SFelix Fietkau 	u32 nbuf = 0;
166b5c80475SFelix Fietkau 
167b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
168b5c80475SFelix Fietkau 		ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
169b5c80475SFelix Fietkau 		return;
170b5c80475SFelix Fietkau 	}
171b5c80475SFelix Fietkau 
172b5c80475SFelix Fietkau 	while (!list_empty(&sc->rx.rxbuf)) {
173b5c80475SFelix Fietkau 		nbuf++;
174b5c80475SFelix Fietkau 
175b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
176b5c80475SFelix Fietkau 			break;
177b5c80475SFelix Fietkau 
178b5c80475SFelix Fietkau 		if (nbuf >= size)
179b5c80475SFelix Fietkau 			break;
180b5c80475SFelix Fietkau 	}
181b5c80475SFelix Fietkau }
182b5c80475SFelix Fietkau 
183b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
184b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
185b5c80475SFelix Fietkau {
186b5c80475SFelix Fietkau 	struct ath_buf *bf;
187b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
188b5c80475SFelix Fietkau 	struct sk_buff *skb;
189b5c80475SFelix Fietkau 
190b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
191b5c80475SFelix Fietkau 
192b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
193b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
194b5c80475SFelix Fietkau 		BUG_ON(!bf);
195b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
196b5c80475SFelix Fietkau 	}
197b5c80475SFelix Fietkau }
198b5c80475SFelix Fietkau 
199b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
200b5c80475SFelix Fietkau {
201b5c80475SFelix Fietkau 	struct ath_buf *bf;
202b5c80475SFelix Fietkau 
203b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
204b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
205b5c80475SFelix Fietkau 
206b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
207b5c80475SFelix Fietkau 		if (bf->bf_mpdu)
208b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
209b5c80475SFelix Fietkau 	}
210b5c80475SFelix Fietkau 
211b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
212b5c80475SFelix Fietkau 
213b5c80475SFelix Fietkau 	kfree(sc->rx.rx_bufptr);
214b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = NULL;
215b5c80475SFelix Fietkau }
216b5c80475SFelix Fietkau 
217b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
218b5c80475SFelix Fietkau {
219b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
220b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_buffers);
221b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
222b5c80475SFelix Fietkau }
223b5c80475SFelix Fietkau 
224b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
225b5c80475SFelix Fietkau {
226b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
227b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
228b5c80475SFelix Fietkau 	struct sk_buff *skb;
229b5c80475SFelix Fietkau 	struct ath_buf *bf;
230b5c80475SFelix Fietkau 	int error = 0, i;
231b5c80475SFelix Fietkau 	u32 size;
232b5c80475SFelix Fietkau 
233b5c80475SFelix Fietkau 
234b5c80475SFelix Fietkau 	common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
235b5c80475SFelix Fietkau 				     ah->caps.rx_status_len,
236b5c80475SFelix Fietkau 				     min(common->cachelsz, (u16)64));
237b5c80475SFelix Fietkau 
238b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
239b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
240b5c80475SFelix Fietkau 
241b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
242b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
243b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
244b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
245b5c80475SFelix Fietkau 
246b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
247b5c80475SFelix Fietkau 	bf = kzalloc(size, GFP_KERNEL);
248b5c80475SFelix Fietkau 	if (!bf)
249b5c80475SFelix Fietkau 		return -ENOMEM;
250b5c80475SFelix Fietkau 
251b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
252b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = bf;
253b5c80475SFelix Fietkau 
254b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
255b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
256b5c80475SFelix Fietkau 		if (!skb) {
257b5c80475SFelix Fietkau 			error = -ENOMEM;
258b5c80475SFelix Fietkau 			goto rx_init_fail;
259b5c80475SFelix Fietkau 		}
260b5c80475SFelix Fietkau 
261b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
262b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
263b5c80475SFelix Fietkau 
264b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
265b5c80475SFelix Fietkau 						 common->rx_bufsize,
266b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
267b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
268b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
269b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
270b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
271b5c80475SFelix Fietkau 				ath_print(common, ATH_DBG_FATAL,
272b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
273b5c80475SFelix Fietkau 				error = -ENOMEM;
274b5c80475SFelix Fietkau 				goto rx_init_fail;
275b5c80475SFelix Fietkau 		}
276b5c80475SFelix Fietkau 
277b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
278b5c80475SFelix Fietkau 	}
279b5c80475SFelix Fietkau 
280b5c80475SFelix Fietkau 	return 0;
281b5c80475SFelix Fietkau 
282b5c80475SFelix Fietkau rx_init_fail:
283b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
284b5c80475SFelix Fietkau 	return error;
285b5c80475SFelix Fietkau }
286b5c80475SFelix Fietkau 
287b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
288b5c80475SFelix Fietkau {
289b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
290b5c80475SFelix Fietkau 
291b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
292b5c80475SFelix Fietkau 
293b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
294b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
295b5c80475SFelix Fietkau 
296b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
297b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
298b5c80475SFelix Fietkau 
299b5c80475SFelix Fietkau 	spin_unlock_bh(&sc->rx.rxbuflock);
300b5c80475SFelix Fietkau 
301b5c80475SFelix Fietkau 	ath_opmode_init(sc);
302b5c80475SFelix Fietkau 
30340346b66SLuis R. Rodriguez 	ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_SCANNING));
304b5c80475SFelix Fietkau }
305b5c80475SFelix Fietkau 
306b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
307b5c80475SFelix Fietkau {
308b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
309b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
310b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
311b5c80475SFelix Fietkau 	spin_unlock_bh(&sc->rx.rxbuflock);
312b5c80475SFelix Fietkau }
313b5c80475SFelix Fietkau 
314203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
315203c4805SLuis R. Rodriguez {
31627c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
317203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
318203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
319203c4805SLuis R. Rodriguez 	int error = 0;
320203c4805SLuis R. Rodriguez 
321203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxflushlock);
322203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
323203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxbuflock);
324203c4805SLuis R. Rodriguez 
325b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
326b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
327b5c80475SFelix Fietkau 	} else {
328cc861f74SLuis R. Rodriguez 		common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
32927c51f1aSLuis R. Rodriguez 				min(common->cachelsz, (u16)64));
330203c4805SLuis R. Rodriguez 
331c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
332cc861f74SLuis R. Rodriguez 				common->cachelsz, common->rx_bufsize);
333203c4805SLuis R. Rodriguez 
334203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
335203c4805SLuis R. Rodriguez 
336203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
3374adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
338203c4805SLuis R. Rodriguez 		if (error != 0) {
339c46917bbSLuis R. Rodriguez 			ath_print(common, ATH_DBG_FATAL,
340b5c80475SFelix Fietkau 				  "failed to allocate rx descriptors: %d\n",
341b5c80475SFelix Fietkau 				  error);
342203c4805SLuis R. Rodriguez 			goto err;
343203c4805SLuis R. Rodriguez 		}
344203c4805SLuis R. Rodriguez 
345203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
346b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
347b5c80475SFelix Fietkau 					      GFP_KERNEL);
348203c4805SLuis R. Rodriguez 			if (skb == NULL) {
349203c4805SLuis R. Rodriguez 				error = -ENOMEM;
350203c4805SLuis R. Rodriguez 				goto err;
351203c4805SLuis R. Rodriguez 			}
352203c4805SLuis R. Rodriguez 
353203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
354203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
355cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
356203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
357203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
358203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
359203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
360203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
361c46917bbSLuis R. Rodriguez 				ath_print(common, ATH_DBG_FATAL,
362203c4805SLuis R. Rodriguez 					  "dma_mapping_error() on RX init\n");
363203c4805SLuis R. Rodriguez 				error = -ENOMEM;
364203c4805SLuis R. Rodriguez 				goto err;
365203c4805SLuis R. Rodriguez 			}
366203c4805SLuis R. Rodriguez 			bf->bf_dmacontext = bf->bf_buf_addr;
367203c4805SLuis R. Rodriguez 		}
368203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
369b5c80475SFelix Fietkau 	}
370203c4805SLuis R. Rodriguez 
371203c4805SLuis R. Rodriguez err:
372203c4805SLuis R. Rodriguez 	if (error)
373203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
374203c4805SLuis R. Rodriguez 
375203c4805SLuis R. Rodriguez 	return error;
376203c4805SLuis R. Rodriguez }
377203c4805SLuis R. Rodriguez 
378203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
379203c4805SLuis R. Rodriguez {
380cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
381cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
382203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
383203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
384203c4805SLuis R. Rodriguez 
385b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
386b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
387b5c80475SFelix Fietkau 		return;
388b5c80475SFelix Fietkau 	} else {
389203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
390203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
391203c4805SLuis R. Rodriguez 			if (skb) {
392203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
393b5c80475SFelix Fietkau 						common->rx_bufsize,
394b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
395203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
396203c4805SLuis R. Rodriguez 			}
397203c4805SLuis R. Rodriguez 		}
398203c4805SLuis R. Rodriguez 
399203c4805SLuis R. Rodriguez 		if (sc->rx.rxdma.dd_desc_len != 0)
400203c4805SLuis R. Rodriguez 			ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
401203c4805SLuis R. Rodriguez 	}
402b5c80475SFelix Fietkau }
403203c4805SLuis R. Rodriguez 
404203c4805SLuis R. Rodriguez /*
405203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
406203c4805SLuis R. Rodriguez  * operating mode and state:
407203c4805SLuis R. Rodriguez  *
408203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
409203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
410203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
411203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
412203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
413203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
414203c4805SLuis R. Rodriguez  * o accept beacons:
415203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
416203c4805SLuis R. Rodriguez  *     node table entries for peers,
417203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
418203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
419203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
420203c4805SLuis R. Rodriguez  *   - when scanning
421203c4805SLuis R. Rodriguez  */
422203c4805SLuis R. Rodriguez 
423203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
424203c4805SLuis R. Rodriguez {
425203c4805SLuis R. Rodriguez #define	RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
426203c4805SLuis R. Rodriguez 
427203c4805SLuis R. Rodriguez 	u32 rfilt;
428203c4805SLuis R. Rodriguez 
429203c4805SLuis R. Rodriguez 	rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
430203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
431203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
432203c4805SLuis R. Rodriguez 
433203c4805SLuis R. Rodriguez 	/* If not a STA, enable processing of Probe Requests */
434203c4805SLuis R. Rodriguez 	if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
435203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
436203c4805SLuis R. Rodriguez 
437203c4805SLuis R. Rodriguez 	/*
438203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
439203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
440203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
441203c4805SLuis R. Rodriguez 	 */
442203c4805SLuis R. Rodriguez 	if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
443203c4805SLuis R. Rodriguez 	     (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
444203c4805SLuis R. Rodriguez 	    (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
445203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
446203c4805SLuis R. Rodriguez 
447203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
448203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
449203c4805SLuis R. Rodriguez 
450203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
451cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
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 
466cfda6695SBen Greear 	if (sc->sec_wiphy || (sc->nvifs > 1) ||
467cfda6695SBen Greear 	    (sc->rx.rxfilter & FIF_OTHER_BSS)) {
4685eb6ba83SJavier Cardona 		/* The following may also be needed for other older chips */
4695eb6ba83SJavier Cardona 		if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
4705eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
471203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
472203c4805SLuis R. Rodriguez 	}
473203c4805SLuis R. Rodriguez 
474203c4805SLuis R. Rodriguez 	return rfilt;
475203c4805SLuis R. Rodriguez 
476203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE
477203c4805SLuis R. Rodriguez }
478203c4805SLuis R. Rodriguez 
479203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
480203c4805SLuis R. Rodriguez {
481203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
482203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
483203c4805SLuis R. Rodriguez 
484b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
485b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
486b5c80475SFelix Fietkau 		return 0;
487b5c80475SFelix Fietkau 	}
488b5c80475SFelix Fietkau 
489203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
490203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
491203c4805SLuis R. Rodriguez 		goto start_recv;
492203c4805SLuis R. Rodriguez 
493203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
494203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
495203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
496203c4805SLuis R. Rodriguez 	}
497203c4805SLuis R. Rodriguez 
498203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
499203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
500203c4805SLuis R. Rodriguez 		goto start_recv;
501203c4805SLuis R. Rodriguez 
502203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
503203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
504203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
505203c4805SLuis R. Rodriguez 
506203c4805SLuis R. Rodriguez start_recv:
507203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
508203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
50940346b66SLuis R. Rodriguez 	ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_SCANNING));
510203c4805SLuis R. Rodriguez 
511203c4805SLuis R. Rodriguez 	return 0;
512203c4805SLuis R. Rodriguez }
513203c4805SLuis R. Rodriguez 
514203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
515203c4805SLuis R. Rodriguez {
516203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
517203c4805SLuis R. Rodriguez 	bool stopped;
518203c4805SLuis R. Rodriguez 
519203c4805SLuis R. Rodriguez 	ath9k_hw_stoppcurecv(ah);
520203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
521203c4805SLuis R. Rodriguez 	stopped = ath9k_hw_stopdmarecv(ah);
522b5c80475SFelix Fietkau 
523b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
524b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
525b5c80475SFelix Fietkau 	else
526203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
527203c4805SLuis R. Rodriguez 
528203c4805SLuis R. Rodriguez 	return stopped;
529203c4805SLuis R. Rodriguez }
530203c4805SLuis R. Rodriguez 
531203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc)
532203c4805SLuis R. Rodriguez {
533203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxflushlock);
534203c4805SLuis R. Rodriguez 	sc->sc_flags |= SC_OP_RXFLUSH;
535b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
536b5c80475SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
537b5c80475SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
538203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
539203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxflushlock);
540203c4805SLuis R. Rodriguez }
541203c4805SLuis R. Rodriguez 
542cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
543cc65965cSJouni Malinen {
544cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
545cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
546cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
547cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
548cc65965cSJouni Malinen 
549cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
550cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
551cc65965cSJouni Malinen 	end = skb->data + skb->len;
552cc65965cSJouni Malinen 
553cc65965cSJouni Malinen 	while (pos + 2 < end) {
554cc65965cSJouni Malinen 		id = *pos++;
555cc65965cSJouni Malinen 		elen = *pos++;
556cc65965cSJouni Malinen 		if (pos + elen > end)
557cc65965cSJouni Malinen 			break;
558cc65965cSJouni Malinen 
559cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
560cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
561cc65965cSJouni Malinen 				break;
562cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
563cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
564cc65965cSJouni Malinen 				break;
565cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
566cc65965cSJouni Malinen 		}
567cc65965cSJouni Malinen 
568cc65965cSJouni Malinen 		pos += elen;
569cc65965cSJouni Malinen 	}
570cc65965cSJouni Malinen 
571cc65965cSJouni Malinen 	return false;
572cc65965cSJouni Malinen }
573cc65965cSJouni Malinen 
574cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
575cc65965cSJouni Malinen {
576cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
5771510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
578cc65965cSJouni Malinen 
579cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
580cc65965cSJouni Malinen 		return;
581cc65965cSJouni Malinen 
582cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
5831510718dSLuis R. Rodriguez 	if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
584cc65965cSJouni Malinen 		return; /* not from our current AP */
585cc65965cSJouni Malinen 
5861b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
587293dc5dfSGabor Juhos 
5881b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5891b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
590c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
591c46917bbSLuis R. Rodriguez 			  "Reconfigure Beacon timers based on "
592ccdfeab6SJouni Malinen 			  "timestamp from the AP\n");
593ccdfeab6SJouni Malinen 		ath_beacon_config(sc, NULL);
594ccdfeab6SJouni Malinen 	}
595ccdfeab6SJouni Malinen 
596cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
597cc65965cSJouni Malinen 		/*
598cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
59958f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
60058f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
60158f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
60258f5fffdSGabor Juhos 		 * so we are waiting for it as well.
603cc65965cSJouni Malinen 		 */
604c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
605cc65965cSJouni Malinen 			  "buffered broadcast/multicast frame(s)\n");
6061b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
607cc65965cSJouni Malinen 		return;
608cc65965cSJouni Malinen 	}
609cc65965cSJouni Malinen 
6101b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
611cc65965cSJouni Malinen 		/*
612cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
613cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
614cc65965cSJouni Malinen 		 * been delivered.
615cc65965cSJouni Malinen 		 */
6161b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
617c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
618c46917bbSLuis R. Rodriguez 			  "PS wait for CAB frames timed out\n");
619cc65965cSJouni Malinen 	}
620cc65965cSJouni Malinen }
621cc65965cSJouni Malinen 
622cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
623cc65965cSJouni Malinen {
624cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
625c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
626cc65965cSJouni Malinen 
627cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
628cc65965cSJouni Malinen 
629cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
630ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
631ededf1f8SVasanthakumar Thiagarajan 	    && ieee80211_is_beacon(hdr->frame_control))
632cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
6331b04b930SSujith 	else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
634cc65965cSJouni Malinen 		 (ieee80211_is_data(hdr->frame_control) ||
635cc65965cSJouni Malinen 		  ieee80211_is_action(hdr->frame_control)) &&
636cc65965cSJouni Malinen 		 is_multicast_ether_addr(hdr->addr1) &&
637cc65965cSJouni Malinen 		 !ieee80211_has_moredata(hdr->frame_control)) {
638cc65965cSJouni Malinen 		/*
639cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
640cc65965cSJouni Malinen 		 * point.
641cc65965cSJouni Malinen 		 */
6421b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
643c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
644c46917bbSLuis R. Rodriguez 			  "All PS CAB frames received, back to sleep\n");
6451b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6469a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
6479a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
6481b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
649c46917bbSLuis R. Rodriguez 		ath_print(common, ATH_DBG_PS,
650c46917bbSLuis R. Rodriguez 			  "Going back to sleep after having received "
651f643e51dSPavel Roskin 			  "PS-Poll data (0x%lx)\n",
6521b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
6531b04b930SSujith 					PS_WAIT_FOR_CAB |
6541b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6551b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
656cc65965cSJouni Malinen 	}
657cc65965cSJouni Malinen }
658cc65965cSJouni Malinen 
659b4afffc0SLuis R. Rodriguez static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
660b4afffc0SLuis R. Rodriguez 				    struct ath_softc *sc, struct sk_buff *skb,
6615ca42627SLuis R. Rodriguez 				    struct ieee80211_rx_status *rxs)
6629d64a3cfSJouni Malinen {
6639d64a3cfSJouni Malinen 	struct ieee80211_hdr *hdr;
6649d64a3cfSJouni Malinen 
6659d64a3cfSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
6669d64a3cfSJouni Malinen 
6679d64a3cfSJouni Malinen 	/* Send the frame to mac80211 */
6689d64a3cfSJouni Malinen 	if (is_multicast_ether_addr(hdr->addr1)) {
6699d64a3cfSJouni Malinen 		int i;
6709d64a3cfSJouni Malinen 		/*
6719d64a3cfSJouni Malinen 		 * Deliver broadcast/multicast frames to all suitable
6729d64a3cfSJouni Malinen 		 * virtual wiphys.
6739d64a3cfSJouni Malinen 		 */
6749d64a3cfSJouni Malinen 		/* TODO: filter based on channel configuration */
6759d64a3cfSJouni Malinen 		for (i = 0; i < sc->num_sec_wiphy; i++) {
6769d64a3cfSJouni Malinen 			struct ath_wiphy *aphy = sc->sec_wiphy[i];
6779d64a3cfSJouni Malinen 			struct sk_buff *nskb;
6789d64a3cfSJouni Malinen 			if (aphy == NULL)
6799d64a3cfSJouni Malinen 				continue;
6809d64a3cfSJouni Malinen 			nskb = skb_copy(skb, GFP_ATOMIC);
6815ca42627SLuis R. Rodriguez 			if (!nskb)
6825ca42627SLuis R. Rodriguez 				continue;
683f1d58c25SJohannes Berg 			ieee80211_rx(aphy->hw, nskb);
6849d64a3cfSJouni Malinen 		}
685f1d58c25SJohannes Berg 		ieee80211_rx(sc->hw, skb);
6865ca42627SLuis R. Rodriguez 	} else
6879d64a3cfSJouni Malinen 		/* Deliver unicast frames based on receiver address */
688b4afffc0SLuis R. Rodriguez 		ieee80211_rx(hw, skb);
6899d64a3cfSJouni Malinen }
6909d64a3cfSJouni Malinen 
691b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
692b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
693203c4805SLuis R. Rodriguez {
694b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
695203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
69627c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
697b5c80475SFelix Fietkau 	struct sk_buff *skb;
698b5c80475SFelix Fietkau 	struct ath_buf *bf;
699b5c80475SFelix Fietkau 	int ret;
700203c4805SLuis R. Rodriguez 
701b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
702b5c80475SFelix Fietkau 	if (!skb)
703b5c80475SFelix Fietkau 		return false;
704203c4805SLuis R. Rodriguez 
705b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
706b5c80475SFelix Fietkau 	BUG_ON(!bf);
707b5c80475SFelix Fietkau 
708ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
709b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
710b5c80475SFelix Fietkau 
711b5c80475SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
712ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
713ce9426d1SMing Lei 		/*let device gain the buffer again*/
714ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
715ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
716b5c80475SFelix Fietkau 		return false;
717ce9426d1SMing Lei 	}
718b5c80475SFelix Fietkau 
719b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
720b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
721b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
722b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
723b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
724b5c80475SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
725b5c80475SFelix Fietkau 		if (!skb)
726b5c80475SFelix Fietkau 			return true;
727b5c80475SFelix Fietkau 
728b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
729b5c80475SFelix Fietkau 		BUG_ON(!bf);
730b5c80475SFelix Fietkau 
731b5c80475SFelix Fietkau 		__skb_unlink(skb, &rx_edma->rx_fifo);
732b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
733b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
734083e3e8dSVasanthakumar Thiagarajan 		return true;
735b5c80475SFelix Fietkau 	}
736b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_buffers, skb);
737b5c80475SFelix Fietkau 
738b5c80475SFelix Fietkau 	return true;
739b5c80475SFelix Fietkau }
740b5c80475SFelix Fietkau 
741b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
742b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
743b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
744b5c80475SFelix Fietkau {
745b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
746b5c80475SFelix Fietkau 	struct sk_buff *skb;
747b5c80475SFelix Fietkau 	struct ath_buf *bf;
748b5c80475SFelix Fietkau 
749b5c80475SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype));
750b5c80475SFelix Fietkau 	skb = __skb_dequeue(&rx_edma->rx_buffers);
751b5c80475SFelix Fietkau 	if (!skb)
752b5c80475SFelix Fietkau 		return NULL;
753b5c80475SFelix Fietkau 
754b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
755b5c80475SFelix Fietkau 	ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
756b5c80475SFelix Fietkau 	return bf;
757b5c80475SFelix Fietkau }
758b5c80475SFelix Fietkau 
759b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
760b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
761b5c80475SFelix Fietkau {
762b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
763b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
764b5c80475SFelix Fietkau 	struct ath_desc *ds;
765b5c80475SFelix Fietkau 	struct ath_buf *bf;
766b5c80475SFelix Fietkau 	int ret;
767203c4805SLuis R. Rodriguez 
768203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
769203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
770b5c80475SFelix Fietkau 		return NULL;
771203c4805SLuis R. Rodriguez 	}
772203c4805SLuis R. Rodriguez 
773203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
774203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
775203c4805SLuis R. Rodriguez 
776203c4805SLuis R. Rodriguez 	/*
777203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
778203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
779203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
780203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
781203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
782203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
783203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
784203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
785203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
786203c4805SLuis R. Rodriguez 	 */
787b5c80475SFelix Fietkau 	ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
788b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
78929bffa96SFelix Fietkau 		struct ath_rx_status trs;
790203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
791203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
792203c4805SLuis R. Rodriguez 
79329bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
794203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
795203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
796b5c80475SFelix Fietkau 			return NULL;
797203c4805SLuis R. Rodriguez 		}
798203c4805SLuis R. Rodriguez 
799203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
800203c4805SLuis R. Rodriguez 
801203c4805SLuis R. Rodriguez 		/*
802203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
803203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
804203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
805203c4805SLuis R. Rodriguez 		 * set or not.
806203c4805SLuis R. Rodriguez 		 *
807203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
808203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
809203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
810203c4805SLuis R. Rodriguez 		 */
811203c4805SLuis R. Rodriguez 
812203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
813b5c80475SFelix Fietkau 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
814b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
815b5c80475SFelix Fietkau 			return NULL;
816203c4805SLuis R. Rodriguez 	}
817203c4805SLuis R. Rodriguez 
818b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
819b5c80475SFelix Fietkau 		return bf;
820203c4805SLuis R. Rodriguez 
821203c4805SLuis R. Rodriguez 	/*
822203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
823203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
824203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
825203c4805SLuis R. Rodriguez 	 */
826ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
827cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
828203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
829203c4805SLuis R. Rodriguez 
830b5c80475SFelix Fietkau 	return bf;
831b5c80475SFelix Fietkau }
832b5c80475SFelix Fietkau 
833d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
834d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
8359f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
836d435700fSSujith 			    struct ieee80211_rx_status *rxs,
837d435700fSSujith 			    struct ath_rx_status *rx_stats,
838d435700fSSujith 			    bool *decrypt_error)
839d435700fSSujith {
840d435700fSSujith 	struct ath_hw *ah = common->ah;
841d435700fSSujith 	__le16 fc;
842b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
843d435700fSSujith 
844d435700fSSujith 	fc = hdr->frame_control;
845d435700fSSujith 
846d435700fSSujith 	if (!rx_stats->rs_datalen)
847d435700fSSujith 		return false;
848d435700fSSujith         /*
849d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
850d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
851d435700fSSujith          * those frames.
852d435700fSSujith          */
853b7b1b512SVasanthakumar Thiagarajan 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
854d435700fSSujith 		return false;
855d435700fSSujith 
856d435700fSSujith 	/*
857d435700fSSujith 	 * rs_more indicates chained descriptors which can be used
858d435700fSSujith 	 * to link buffers together for a sort of scatter-gather
859d435700fSSujith 	 * operation.
860d435700fSSujith 	 * reject the frame, we don't support scatter-gather yet and
861d435700fSSujith 	 * the frame is probably corrupt anyway
862d435700fSSujith 	 */
863d435700fSSujith 	if (rx_stats->rs_more)
864d435700fSSujith 		return false;
865d435700fSSujith 
866d435700fSSujith 	/*
867d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
868d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
869d435700fSSujith 	 * rs_more will be false at the last element of the chained
870d435700fSSujith 	 * descriptors.
871d435700fSSujith 	 */
872d435700fSSujith 	if (rx_stats->rs_status != 0) {
873d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_CRC)
874d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
875d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
876d435700fSSujith 			return false;
877d435700fSSujith 
878d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
879d435700fSSujith 			*decrypt_error = true;
880d435700fSSujith 		} else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
881d435700fSSujith 			/*
88256363ddeSFelix Fietkau 			 * The MIC error bit is only valid if the frame
88356363ddeSFelix Fietkau 			 * is not a control frame or fragment, and it was
88456363ddeSFelix Fietkau 			 * decrypted using a valid TKIP key.
885d435700fSSujith 			 */
88656363ddeSFelix Fietkau 			if (!ieee80211_is_ctl(fc) &&
88756363ddeSFelix Fietkau 			    !ieee80211_has_morefrags(fc) &&
88856363ddeSFelix Fietkau 			    !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
88956363ddeSFelix Fietkau 			    test_bit(rx_stats->rs_keyix, common->tkip_keymap))
890d435700fSSujith 				rxs->flag |= RX_FLAG_MMIC_ERROR;
89156363ddeSFelix Fietkau 			else
89256363ddeSFelix Fietkau 				rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
893d435700fSSujith 		}
894d435700fSSujith 		/*
895d435700fSSujith 		 * Reject error frames with the exception of
896d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
897d435700fSSujith 		 * we also ignore the CRC error.
898d435700fSSujith 		 */
899d435700fSSujith 		if (ah->opmode == NL80211_IFTYPE_MONITOR) {
900d435700fSSujith 			if (rx_stats->rs_status &
901d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
902d435700fSSujith 			      ATH9K_RXERR_CRC))
903d435700fSSujith 				return false;
904d435700fSSujith 		} else {
905d435700fSSujith 			if (rx_stats->rs_status &
906d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
907d435700fSSujith 				return false;
908d435700fSSujith 			}
909d435700fSSujith 		}
910d435700fSSujith 	}
911d435700fSSujith 	return true;
912d435700fSSujith }
913d435700fSSujith 
914d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
915d435700fSSujith 			      struct ieee80211_hw *hw,
916d435700fSSujith 			      struct ath_rx_status *rx_stats,
9179f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
918d435700fSSujith {
919d435700fSSujith 	struct ieee80211_supported_band *sband;
920d435700fSSujith 	enum ieee80211_band band;
921d435700fSSujith 	unsigned int i = 0;
922d435700fSSujith 
923d435700fSSujith 	band = hw->conf.channel->band;
924d435700fSSujith 	sband = hw->wiphy->bands[band];
925d435700fSSujith 
926d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
927d435700fSSujith 		/* HT rate */
928d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
929d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
930d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
931d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
932d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
933d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
934d435700fSSujith 		return 0;
935d435700fSSujith 	}
936d435700fSSujith 
937d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
938d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
939d435700fSSujith 			rxs->rate_idx = i;
940d435700fSSujith 			return 0;
941d435700fSSujith 		}
942d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
943d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
944d435700fSSujith 			rxs->rate_idx = i;
945d435700fSSujith 			return 0;
946d435700fSSujith 		}
947d435700fSSujith 	}
948d435700fSSujith 
949d435700fSSujith 	/*
950d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
951d435700fSSujith 	 * because hardware has already validated this frame as OK.
952d435700fSSujith 	 */
953d435700fSSujith 	ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
954d435700fSSujith 		  "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
955d435700fSSujith 
956d435700fSSujith 	return -EINVAL;
957d435700fSSujith }
958d435700fSSujith 
959d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
960d435700fSSujith 			       struct ieee80211_hw *hw,
9619f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
962d435700fSSujith 			       struct ath_rx_status *rx_stats)
963d435700fSSujith {
964d435700fSSujith 	struct ath_hw *ah = common->ah;
965d435700fSSujith 	struct ieee80211_sta *sta;
966d435700fSSujith 	struct ath_node *an;
967d435700fSSujith 	int last_rssi = ATH_RSSI_DUMMY_MARKER;
968d435700fSSujith 	__le16 fc;
969d435700fSSujith 
970d435700fSSujith 	fc = hdr->frame_control;
971d435700fSSujith 
972d435700fSSujith 	rcu_read_lock();
973d435700fSSujith 	/*
974d435700fSSujith 	 * XXX: use ieee80211_find_sta! This requires quite a bit of work
975d435700fSSujith 	 * under the current ath9k virtual wiphy implementation as we have
976d435700fSSujith 	 * no way of tying a vif to wiphy. Typically vifs are attached to
977d435700fSSujith 	 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
978d435700fSSujith 	 * wiphy you'd have to iterate over every wiphy and each sdata.
979d435700fSSujith 	 */
980d435700fSSujith 	sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
981d435700fSSujith 	if (sta) {
982d435700fSSujith 		an = (struct ath_node *) sta->drv_priv;
983d435700fSSujith 		if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
984d435700fSSujith 		   !rx_stats->rs_moreaggr)
985d435700fSSujith 			ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
986d435700fSSujith 		last_rssi = an->last_rssi;
987d435700fSSujith 	}
988d435700fSSujith 	rcu_read_unlock();
989d435700fSSujith 
990d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
991d435700fSSujith 		rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
992d435700fSSujith 					      ATH_RSSI_EP_MULTIPLIER);
993d435700fSSujith 	if (rx_stats->rs_rssi < 0)
994d435700fSSujith 		rx_stats->rs_rssi = 0;
995d435700fSSujith 
996d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
997d435700fSSujith 	if (ieee80211_is_beacon(fc))
998d435700fSSujith 		ah->stats.avgbrssi = rx_stats->rs_rssi;
999d435700fSSujith }
1000d435700fSSujith 
1001d435700fSSujith /*
1002d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
1003d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
1004d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
1005d435700fSSujith  */
1006d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
1007d435700fSSujith 				   struct ieee80211_hw *hw,
10089f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
1009d435700fSSujith 				   struct ath_rx_status *rx_stats,
1010d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
1011d435700fSSujith 				   bool *decrypt_error)
1012d435700fSSujith {
1013d435700fSSujith 	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
1014d435700fSSujith 
1015d435700fSSujith 	/*
1016d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
1017d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
1018d435700fSSujith 	 */
10199f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
1020d435700fSSujith 		return -EINVAL;
1021d435700fSSujith 
10229f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
1023d435700fSSujith 
10249f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1025d435700fSSujith 		return -EINVAL;
1026d435700fSSujith 
1027d435700fSSujith 	rx_status->band = hw->conf.channel->band;
1028d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
1029d435700fSSujith 	rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1030d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
1031d435700fSSujith 	rx_status->flag |= RX_FLAG_TSFT;
1032d435700fSSujith 
1033d435700fSSujith 	return 0;
1034d435700fSSujith }
1035d435700fSSujith 
1036d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
1037d435700fSSujith 				     struct sk_buff *skb,
1038d435700fSSujith 				     struct ath_rx_status *rx_stats,
1039d435700fSSujith 				     struct ieee80211_rx_status *rxs,
1040d435700fSSujith 				     bool decrypt_error)
1041d435700fSSujith {
1042d435700fSSujith 	struct ath_hw *ah = common->ah;
1043d435700fSSujith 	struct ieee80211_hdr *hdr;
1044d435700fSSujith 	int hdrlen, padpos, padsize;
1045d435700fSSujith 	u8 keyix;
1046d435700fSSujith 	__le16 fc;
1047d435700fSSujith 
1048d435700fSSujith 	/* see if any padding is done by the hw and remove it */
1049d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1050d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1051d435700fSSujith 	fc = hdr->frame_control;
1052d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
1053d435700fSSujith 
1054d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1055d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1056d435700fSSujith 	 * padsize would take into account odd header lengths:
1057d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1058d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1059d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1060d435700fSSujith 	 * not try to remove padding from short control frames that do
1061d435700fSSujith 	 * not have payload. */
1062d435700fSSujith 	padsize = padpos & 3;
1063d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1064d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1065d435700fSSujith 		skb_pull(skb, padsize);
1066d435700fSSujith 	}
1067d435700fSSujith 
1068d435700fSSujith 	keyix = rx_stats->rs_keyix;
1069d435700fSSujith 
1070d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1071d435700fSSujith 	    ieee80211_has_protected(fc)) {
1072d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1073d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1074d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1075d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1076d435700fSSujith 
1077d435700fSSujith 		if (test_bit(keyix, common->keymap))
1078d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1079d435700fSSujith 	}
1080d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1081d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1082d435700fSSujith 	    ieee80211_is_mgmt(fc))
1083d435700fSSujith 		/* Use software decrypt for management frames. */
1084d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1085d435700fSSujith }
1086b5c80475SFelix Fietkau 
1087102885a5SVasanthakumar Thiagarajan static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1088102885a5SVasanthakumar Thiagarajan 				      struct ath_hw_antcomb_conf ant_conf,
1089102885a5SVasanthakumar Thiagarajan 				      int main_rssi_avg)
1090102885a5SVasanthakumar Thiagarajan {
1091102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt = 0;
1092102885a5SVasanthakumar Thiagarajan 
1093102885a5SVasanthakumar Thiagarajan 	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1094102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna2 = main_rssi_avg;
1095102885a5SVasanthakumar Thiagarajan 	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1096102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna1 = main_rssi_avg;
1097102885a5SVasanthakumar Thiagarajan 
1098102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1099102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1100102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1101102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1102102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1103102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1104102885a5SVasanthakumar Thiagarajan 		break;
1105102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1106102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1107102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1108102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1109102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1110102885a5SVasanthakumar Thiagarajan 		break;
1111102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1112102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1113102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1114102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1115102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1116102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1117102885a5SVasanthakumar Thiagarajan 		break;
1118102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1119102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1120102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1121102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1122102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1123102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1124102885a5SVasanthakumar Thiagarajan 		break;
1125102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1126102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1127102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1128102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1129102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1130102885a5SVasanthakumar Thiagarajan 		break;
1131102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1132102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1133102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1134102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1135102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1136102885a5SVasanthakumar Thiagarajan 		break;
1137102885a5SVasanthakumar Thiagarajan 	default:
1138102885a5SVasanthakumar Thiagarajan 		break;
1139102885a5SVasanthakumar Thiagarajan 	}
1140102885a5SVasanthakumar Thiagarajan }
1141102885a5SVasanthakumar Thiagarajan 
1142102885a5SVasanthakumar Thiagarajan static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1143102885a5SVasanthakumar Thiagarajan 				struct ath_hw_antcomb_conf *div_ant_conf,
1144102885a5SVasanthakumar Thiagarajan 				int main_rssi_avg, int alt_rssi_avg,
1145102885a5SVasanthakumar Thiagarajan 				int alt_ratio)
1146102885a5SVasanthakumar Thiagarajan {
1147102885a5SVasanthakumar Thiagarajan 	/* alt_good */
1148102885a5SVasanthakumar Thiagarajan 	switch (antcomb->quick_scan_cnt) {
1149102885a5SVasanthakumar Thiagarajan 	case 0:
1150102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1151102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1152102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1153102885a5SVasanthakumar Thiagarajan 		break;
1154102885a5SVasanthakumar Thiagarajan 	case 1:
1155102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1156102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1157102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1158102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1159102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_second = alt_rssi_avg;
1160102885a5SVasanthakumar Thiagarajan 
1161102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1162102885a5SVasanthakumar Thiagarajan 			/* main is LNA1 */
1163102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1164102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1165102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1166102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1167102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1168102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1169102885a5SVasanthakumar Thiagarajan 			else
1170102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1171102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1172102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1173102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1174102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1175102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1176102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1177102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1178102885a5SVasanthakumar Thiagarajan 			else
1179102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1180102885a5SVasanthakumar Thiagarajan 		} else {
1181102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1182102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1183102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1184102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1185102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1186102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1187102885a5SVasanthakumar Thiagarajan 			else
1188102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1189102885a5SVasanthakumar Thiagarajan 		}
1190102885a5SVasanthakumar Thiagarajan 		break;
1191102885a5SVasanthakumar Thiagarajan 	case 2:
1192102885a5SVasanthakumar Thiagarajan 		antcomb->alt_good = false;
1193102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = false;
1194102885a5SVasanthakumar Thiagarajan 		antcomb->scan = false;
1195102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1196102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_third = alt_rssi_avg;
1197102885a5SVasanthakumar Thiagarajan 
1198102885a5SVasanthakumar Thiagarajan 		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1199102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1200102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1201102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA2)
1202102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1203102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1204102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1205102885a5SVasanthakumar Thiagarajan 			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1206102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna2 = main_rssi_avg;
1207102885a5SVasanthakumar Thiagarajan 			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1208102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna1 = main_rssi_avg;
1209102885a5SVasanthakumar Thiagarajan 		}
1210102885a5SVasanthakumar Thiagarajan 
1211102885a5SVasanthakumar Thiagarajan 		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1212102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1213102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1214102885a5SVasanthakumar Thiagarajan 		else
1215102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1216102885a5SVasanthakumar Thiagarajan 
1217102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1218102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1219102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1220102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1221102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1222102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1223102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1224102885a5SVasanthakumar Thiagarajan 			else
1225102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1226102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1227102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1228102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1229102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1230102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1231102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1232102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1233102885a5SVasanthakumar Thiagarajan 			else
1234102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1235102885a5SVasanthakumar Thiagarajan 		} else {
1236102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1237102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1238102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1239102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1240102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1241102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1242102885a5SVasanthakumar Thiagarajan 			else
1243102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1244102885a5SVasanthakumar Thiagarajan 		}
1245102885a5SVasanthakumar Thiagarajan 
1246102885a5SVasanthakumar Thiagarajan 		/* set alt to the conf with maximun ratio */
1247102885a5SVasanthakumar Thiagarajan 		if (antcomb->first_ratio && antcomb->second_ratio) {
1248102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_second > antcomb->rssi_third) {
1249102885a5SVasanthakumar Thiagarajan 				/* first alt*/
1250102885a5SVasanthakumar Thiagarajan 				if ((antcomb->first_quick_scan_conf ==
1251102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA1) ||
1252102885a5SVasanthakumar Thiagarajan 				    (antcomb->first_quick_scan_conf ==
1253102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2))
1254102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2*/
1255102885a5SVasanthakumar Thiagarajan 					if (div_ant_conf->main_lna_conf ==
1256102885a5SVasanthakumar Thiagarajan 					    ATH_ANT_DIV_COMB_LNA2)
1257102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1258102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1259102885a5SVasanthakumar Thiagarajan 					else
1260102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1261102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1262102885a5SVasanthakumar Thiagarajan 				else
1263102885a5SVasanthakumar Thiagarajan 					/* Set alt to A+B or A-B */
1264102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1265102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1266102885a5SVasanthakumar Thiagarajan 			} else if ((antcomb->second_quick_scan_conf ==
1267102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA1) ||
1268102885a5SVasanthakumar Thiagarajan 				   (antcomb->second_quick_scan_conf ==
1269102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA2)) {
1270102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1271102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1272102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1273102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1274102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1275102885a5SVasanthakumar Thiagarajan 				else
1276102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1277102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1278102885a5SVasanthakumar Thiagarajan 			} else {
1279102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1280102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1281102885a5SVasanthakumar Thiagarajan 					antcomb->second_quick_scan_conf;
1282102885a5SVasanthakumar Thiagarajan 			}
1283102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->first_ratio) {
1284102885a5SVasanthakumar Thiagarajan 			/* first alt */
1285102885a5SVasanthakumar Thiagarajan 			if ((antcomb->first_quick_scan_conf ==
1286102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1287102885a5SVasanthakumar Thiagarajan 			    (antcomb->first_quick_scan_conf ==
1288102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1289102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2 */
1290102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1291102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1292102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1293102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1294102885a5SVasanthakumar Thiagarajan 				else
1295102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1296102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1297102885a5SVasanthakumar Thiagarajan 			else
1298102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1299102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1300102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1301102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->second_ratio) {
1302102885a5SVasanthakumar Thiagarajan 				/* second alt */
1303102885a5SVasanthakumar Thiagarajan 			if ((antcomb->second_quick_scan_conf ==
1304102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1305102885a5SVasanthakumar Thiagarajan 			    (antcomb->second_quick_scan_conf ==
1306102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1307102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1308102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1309102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1310102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1311102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1312102885a5SVasanthakumar Thiagarajan 				else
1313102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1314102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1315102885a5SVasanthakumar Thiagarajan 			else
1316102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1317102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1318102885a5SVasanthakumar Thiagarajan 						antcomb->second_quick_scan_conf;
1319102885a5SVasanthakumar Thiagarajan 		} else {
1320102885a5SVasanthakumar Thiagarajan 			/* main is largest */
1321102885a5SVasanthakumar Thiagarajan 			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1322102885a5SVasanthakumar Thiagarajan 			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1323102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1324102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1325102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1326102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1327102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1328102885a5SVasanthakumar Thiagarajan 				else
1329102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1330102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1331102885a5SVasanthakumar Thiagarajan 			else
1332102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1333102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf = antcomb->main_conf;
1334102885a5SVasanthakumar Thiagarajan 		}
1335102885a5SVasanthakumar Thiagarajan 		break;
1336102885a5SVasanthakumar Thiagarajan 	default:
1337102885a5SVasanthakumar Thiagarajan 		break;
1338102885a5SVasanthakumar Thiagarajan 	}
1339102885a5SVasanthakumar Thiagarajan }
1340102885a5SVasanthakumar Thiagarajan 
13419bad82b8SJohn W. Linville static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf)
1342102885a5SVasanthakumar Thiagarajan {
1343102885a5SVasanthakumar Thiagarajan 	/* Adjust the fast_div_bias based on main and alt lna conf */
1344102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
1345102885a5SVasanthakumar Thiagarajan 	case (0x01): /* A-B LNA2 */
1346102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1347102885a5SVasanthakumar Thiagarajan 		break;
1348102885a5SVasanthakumar Thiagarajan 	case (0x02): /* A-B LNA1 */
1349102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1350102885a5SVasanthakumar Thiagarajan 		break;
1351102885a5SVasanthakumar Thiagarajan 	case (0x03): /* A-B A+B */
1352102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1353102885a5SVasanthakumar Thiagarajan 		break;
1354102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1355102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1356102885a5SVasanthakumar Thiagarajan 		break;
1357102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1358102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x2;
1359102885a5SVasanthakumar Thiagarajan 		break;
1360102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1361102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1362102885a5SVasanthakumar Thiagarajan 		break;
1363102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1364102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1365102885a5SVasanthakumar Thiagarajan 		break;
1366102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1367102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x0;
1368102885a5SVasanthakumar Thiagarajan 		break;
1369102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1370102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1371102885a5SVasanthakumar Thiagarajan 		break;
1372102885a5SVasanthakumar Thiagarajan 	case (0x30): /* A+B A-B */
1373102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1374102885a5SVasanthakumar Thiagarajan 		break;
1375102885a5SVasanthakumar Thiagarajan 	case (0x31): /* A+B LNA2 */
1376102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1377102885a5SVasanthakumar Thiagarajan 		break;
1378102885a5SVasanthakumar Thiagarajan 	case (0x32): /* A+B LNA1 */
1379102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1380102885a5SVasanthakumar Thiagarajan 		break;
1381102885a5SVasanthakumar Thiagarajan 	default:
1382102885a5SVasanthakumar Thiagarajan 		break;
1383102885a5SVasanthakumar Thiagarajan 	}
1384102885a5SVasanthakumar Thiagarajan }
1385102885a5SVasanthakumar Thiagarajan 
1386102885a5SVasanthakumar Thiagarajan /* Antenna diversity and combining */
1387102885a5SVasanthakumar Thiagarajan static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1388102885a5SVasanthakumar Thiagarajan {
1389102885a5SVasanthakumar Thiagarajan 	struct ath_hw_antcomb_conf div_ant_conf;
1390102885a5SVasanthakumar Thiagarajan 	struct ath_ant_comb *antcomb = &sc->ant_comb;
1391102885a5SVasanthakumar Thiagarajan 	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
1392102885a5SVasanthakumar Thiagarajan 	int curr_main_set, curr_bias;
1393102885a5SVasanthakumar Thiagarajan 	int main_rssi = rs->rs_rssi_ctl0;
1394102885a5SVasanthakumar Thiagarajan 	int alt_rssi = rs->rs_rssi_ctl1;
1395102885a5SVasanthakumar Thiagarajan 	int rx_ant_conf,  main_ant_conf;
1396102885a5SVasanthakumar Thiagarajan 	bool short_scan = false;
1397102885a5SVasanthakumar Thiagarajan 
1398102885a5SVasanthakumar Thiagarajan 	rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1399102885a5SVasanthakumar Thiagarajan 		       ATH_ANT_RX_MASK;
1400102885a5SVasanthakumar Thiagarajan 	main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1401102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_RX_MASK;
1402102885a5SVasanthakumar Thiagarajan 
1403102885a5SVasanthakumar Thiagarajan 	/* Record packet only when alt_rssi is positive */
1404102885a5SVasanthakumar Thiagarajan 	if (alt_rssi > 0) {
1405102885a5SVasanthakumar Thiagarajan 		antcomb->total_pkt_count++;
1406102885a5SVasanthakumar Thiagarajan 		antcomb->main_total_rssi += main_rssi;
1407102885a5SVasanthakumar Thiagarajan 		antcomb->alt_total_rssi  += alt_rssi;
1408102885a5SVasanthakumar Thiagarajan 		if (main_ant_conf == rx_ant_conf)
1409102885a5SVasanthakumar Thiagarajan 			antcomb->main_recv_cnt++;
1410102885a5SVasanthakumar Thiagarajan 		else
1411102885a5SVasanthakumar Thiagarajan 			antcomb->alt_recv_cnt++;
1412102885a5SVasanthakumar Thiagarajan 	}
1413102885a5SVasanthakumar Thiagarajan 
1414102885a5SVasanthakumar Thiagarajan 	/* Short scan check */
1415102885a5SVasanthakumar Thiagarajan 	if (antcomb->scan && antcomb->alt_good) {
1416102885a5SVasanthakumar Thiagarajan 		if (time_after(jiffies, antcomb->scan_start_time +
1417102885a5SVasanthakumar Thiagarajan 		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1418102885a5SVasanthakumar Thiagarajan 			short_scan = true;
1419102885a5SVasanthakumar Thiagarajan 		else
1420102885a5SVasanthakumar Thiagarajan 			if (antcomb->total_pkt_count ==
1421102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1422102885a5SVasanthakumar Thiagarajan 				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1423102885a5SVasanthakumar Thiagarajan 					    antcomb->total_pkt_count);
1424102885a5SVasanthakumar Thiagarajan 				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1425102885a5SVasanthakumar Thiagarajan 					short_scan = true;
1426102885a5SVasanthakumar Thiagarajan 			}
1427102885a5SVasanthakumar Thiagarajan 	}
1428102885a5SVasanthakumar Thiagarajan 
1429102885a5SVasanthakumar Thiagarajan 	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1430102885a5SVasanthakumar Thiagarajan 	    rs->rs_moreaggr) && !short_scan)
1431102885a5SVasanthakumar Thiagarajan 		return;
1432102885a5SVasanthakumar Thiagarajan 
1433102885a5SVasanthakumar Thiagarajan 	if (antcomb->total_pkt_count) {
1434102885a5SVasanthakumar Thiagarajan 		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1435102885a5SVasanthakumar Thiagarajan 			     antcomb->total_pkt_count);
1436102885a5SVasanthakumar Thiagarajan 		main_rssi_avg = (antcomb->main_total_rssi /
1437102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1438102885a5SVasanthakumar Thiagarajan 		alt_rssi_avg = (antcomb->alt_total_rssi /
1439102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1440102885a5SVasanthakumar Thiagarajan 	}
1441102885a5SVasanthakumar Thiagarajan 
1442102885a5SVasanthakumar Thiagarajan 
1443102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1444102885a5SVasanthakumar Thiagarajan 	curr_alt_set = div_ant_conf.alt_lna_conf;
1445102885a5SVasanthakumar Thiagarajan 	curr_main_set = div_ant_conf.main_lna_conf;
1446102885a5SVasanthakumar Thiagarajan 	curr_bias = div_ant_conf.fast_div_bias;
1447102885a5SVasanthakumar Thiagarajan 
1448102885a5SVasanthakumar Thiagarajan 	antcomb->count++;
1449102885a5SVasanthakumar Thiagarajan 
1450102885a5SVasanthakumar Thiagarajan 	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1451102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1452102885a5SVasanthakumar Thiagarajan 			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1453102885a5SVasanthakumar Thiagarajan 						  main_rssi_avg);
1454102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = true;
1455102885a5SVasanthakumar Thiagarajan 		} else {
1456102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = false;
1457102885a5SVasanthakumar Thiagarajan 		}
1458102885a5SVasanthakumar Thiagarajan 
1459102885a5SVasanthakumar Thiagarajan 		antcomb->count = 0;
1460102885a5SVasanthakumar Thiagarajan 		antcomb->scan = true;
1461102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = true;
1462102885a5SVasanthakumar Thiagarajan 	}
1463102885a5SVasanthakumar Thiagarajan 
1464102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan) {
1465102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1466102885a5SVasanthakumar Thiagarajan 			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1467102885a5SVasanthakumar Thiagarajan 				/* Switch main and alt LNA */
1468102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1469102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1470102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1471102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1472102885a5SVasanthakumar Thiagarajan 			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1473102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1474102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1475102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1476102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1477102885a5SVasanthakumar Thiagarajan 			}
1478102885a5SVasanthakumar Thiagarajan 
1479102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1480102885a5SVasanthakumar Thiagarajan 		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1481102885a5SVasanthakumar Thiagarajan 			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1482102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1483102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1484102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1485102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1486102885a5SVasanthakumar Thiagarajan 			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1487102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1488102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1489102885a5SVasanthakumar Thiagarajan 
1490102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1491102885a5SVasanthakumar Thiagarajan 		}
1492102885a5SVasanthakumar Thiagarajan 
1493102885a5SVasanthakumar Thiagarajan 		if ((alt_rssi_avg < (main_rssi_avg +
1494102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
1495102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1496102885a5SVasanthakumar Thiagarajan 	}
1497102885a5SVasanthakumar Thiagarajan 
1498102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan_not_start) {
1499102885a5SVasanthakumar Thiagarajan 		switch (curr_alt_set) {
1500102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA2:
1501102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1502102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = main_rssi_avg;
1503102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1504102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1505102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf =
1506102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1;
1507102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1508102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1509102885a5SVasanthakumar Thiagarajan 			break;
1510102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1:
1511102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1512102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = main_rssi_avg;
1513102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1514102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1515102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1516102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1517102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1518102885a5SVasanthakumar Thiagarajan 			break;
1519102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1520102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_add = alt_rssi_avg;
1521102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1522102885a5SVasanthakumar Thiagarajan 			/* set to A-B */
1523102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf =
1524102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1525102885a5SVasanthakumar Thiagarajan 			break;
1526102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1527102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_sub = alt_rssi_avg;
1528102885a5SVasanthakumar Thiagarajan 			antcomb->scan = false;
1529102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_lna2 >
1530102885a5SVasanthakumar Thiagarajan 			    (antcomb->rssi_lna1 +
1531102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1532102885a5SVasanthakumar Thiagarajan 				/* use LNA2 as main LNA */
1533102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1534102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1535102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1536102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1537102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1538102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1539102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1540102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1541102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1542102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1543102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1544102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1545102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1546102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1547102885a5SVasanthakumar Thiagarajan 				} else {
1548102885a5SVasanthakumar Thiagarajan 					/* set to LNA1 */
1549102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1550102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1551102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1552102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1553102885a5SVasanthakumar Thiagarajan 				}
1554102885a5SVasanthakumar Thiagarajan 			} else {
1555102885a5SVasanthakumar Thiagarajan 				/* use LNA1 as main LNA */
1556102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1557102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1558102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1559102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1560102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1561102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1562102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1563102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1564102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1565102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1566102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1567102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1568102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1569102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1570102885a5SVasanthakumar Thiagarajan 				} else {
1571102885a5SVasanthakumar Thiagarajan 					/* set to LNA2 */
1572102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1573102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1574102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1575102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1576102885a5SVasanthakumar Thiagarajan 				}
1577102885a5SVasanthakumar Thiagarajan 			}
1578102885a5SVasanthakumar Thiagarajan 			break;
1579102885a5SVasanthakumar Thiagarajan 		default:
1580102885a5SVasanthakumar Thiagarajan 			break;
1581102885a5SVasanthakumar Thiagarajan 		}
1582102885a5SVasanthakumar Thiagarajan 	} else {
1583102885a5SVasanthakumar Thiagarajan 		if (!antcomb->alt_good) {
1584102885a5SVasanthakumar Thiagarajan 			antcomb->scan_not_start = false;
1585102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1586102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1587102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1588102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1589102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1590102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1591102885a5SVasanthakumar Thiagarajan 			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1592102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1593102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1594102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1595102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1596102885a5SVasanthakumar Thiagarajan 			}
1597102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1598102885a5SVasanthakumar Thiagarajan 		}
1599102885a5SVasanthakumar Thiagarajan 	}
1600102885a5SVasanthakumar Thiagarajan 
1601102885a5SVasanthakumar Thiagarajan 	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1602102885a5SVasanthakumar Thiagarajan 					   main_rssi_avg, alt_rssi_avg,
1603102885a5SVasanthakumar Thiagarajan 					   alt_ratio);
1604102885a5SVasanthakumar Thiagarajan 
1605102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt++;
1606102885a5SVasanthakumar Thiagarajan 
1607102885a5SVasanthakumar Thiagarajan div_comb_done:
1608102885a5SVasanthakumar Thiagarajan 	ath_ant_div_conf_fast_divbias(&div_ant_conf);
1609102885a5SVasanthakumar Thiagarajan 
1610102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1611102885a5SVasanthakumar Thiagarajan 
1612102885a5SVasanthakumar Thiagarajan 	antcomb->scan_start_time = jiffies;
1613102885a5SVasanthakumar Thiagarajan 	antcomb->total_pkt_count = 0;
1614102885a5SVasanthakumar Thiagarajan 	antcomb->main_total_rssi = 0;
1615102885a5SVasanthakumar Thiagarajan 	antcomb->alt_total_rssi = 0;
1616102885a5SVasanthakumar Thiagarajan 	antcomb->main_recv_cnt = 0;
1617102885a5SVasanthakumar Thiagarajan 	antcomb->alt_recv_cnt = 0;
1618102885a5SVasanthakumar Thiagarajan }
1619102885a5SVasanthakumar Thiagarajan 
1620b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1621b5c80475SFelix Fietkau {
1622b5c80475SFelix Fietkau 	struct ath_buf *bf;
1623b5c80475SFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb;
1624b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1625b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1626b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
1627b5c80475SFelix Fietkau 	/*
1628b5c80475SFelix Fietkau 	 * The hw can techncically differ from common->hw when using ath9k
1629b5c80475SFelix Fietkau 	 * virtual wiphy so to account for that we iterate over the active
1630b5c80475SFelix Fietkau 	 * wiphys and find the appropriate wiphy and therefore hw.
1631b5c80475SFelix Fietkau 	 */
1632b5c80475SFelix Fietkau 	struct ieee80211_hw *hw = NULL;
1633b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1634b5c80475SFelix Fietkau 	int retval;
1635b5c80475SFelix Fietkau 	bool decrypt_error = false;
1636b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1637b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1638b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1639b5c80475SFelix Fietkau 	int dma_type;
16405c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1641a6d2055bSFelix Fietkau 	u64 tsf = 0;
1642a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
1643*8ab2cd09SLuis R. Rodriguez 	unsigned long flags;
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 
1752*8ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1753ededf1f8SVasanthakumar Thiagarajan 		if (unlikely(ath9k_check_auto_sleep(sc) ||
1754ededf1f8SVasanthakumar Thiagarajan 			     (sc->ps_flags & (PS_WAIT_FOR_BEACON |
17551b04b930SSujith 					      PS_WAIT_FOR_CAB |
1756ededf1f8SVasanthakumar Thiagarajan 					      PS_WAIT_FOR_PSPOLL_DATA))))
1757cc65965cSJouni Malinen 			ath_rx_ps(sc, skb);
1758*8ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1759cc65965cSJouni Malinen 
1760102885a5SVasanthakumar Thiagarajan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1761102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1762102885a5SVasanthakumar Thiagarajan 
17635ca42627SLuis R. Rodriguez 		ath_rx_send_to_mac80211(hw, sc, skb, rxs);
1764cc65965cSJouni Malinen 
1765203c4805SLuis R. Rodriguez requeue:
1766b5c80475SFelix Fietkau 		if (edma) {
1767b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
1768b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1769b5c80475SFelix Fietkau 		} else {
1770203c4805SLuis R. Rodriguez 			list_move_tail(&bf->list, &sc->rx.rxbuf);
1771203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
1772b5c80475SFelix Fietkau 		}
1773203c4805SLuis R. Rodriguez 	} while (1);
1774203c4805SLuis R. Rodriguez 
1775203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
1776203c4805SLuis R. Rodriguez 
1777203c4805SLuis R. Rodriguez 	return 0;
1778203c4805SLuis R. Rodriguez }
1779