xref: /linux/drivers/net/wireless/ath/ath9k/recv.c (revision cedc7e3d0c847d602d2970120d0e4cca72f364a4)
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 /*
38203c4805SLuis R. Rodriguez  * Setup and link descriptors.
39203c4805SLuis R. Rodriguez  *
40203c4805SLuis R. Rodriguez  * 11N: we can no longer afford to self link the last descriptor.
41203c4805SLuis R. Rodriguez  * MAC acknowledges BA status as long as it copies frames to host
42203c4805SLuis R. Rodriguez  * buffer (or rx fifo). This can incorrectly acknowledge packets
43203c4805SLuis R. Rodriguez  * to a sender if last desc is self-linked.
44203c4805SLuis R. Rodriguez  */
45203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
46203c4805SLuis R. Rodriguez {
47203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
48cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
49203c4805SLuis R. Rodriguez 	struct ath_desc *ds;
50203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
51203c4805SLuis R. Rodriguez 
52203c4805SLuis R. Rodriguez 	ATH_RXBUF_RESET(bf);
53203c4805SLuis R. Rodriguez 
54203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
55203c4805SLuis R. Rodriguez 	ds->ds_link = 0; /* link to null */
56203c4805SLuis R. Rodriguez 	ds->ds_data = bf->bf_buf_addr;
57203c4805SLuis R. Rodriguez 
58203c4805SLuis R. Rodriguez 	/* virtual addr of the beginning of the buffer. */
59203c4805SLuis R. Rodriguez 	skb = bf->bf_mpdu;
609680e8a3SLuis R. Rodriguez 	BUG_ON(skb == NULL);
61203c4805SLuis R. Rodriguez 	ds->ds_vdata = skb->data;
62203c4805SLuis R. Rodriguez 
63cc861f74SLuis R. Rodriguez 	/*
64cc861f74SLuis R. Rodriguez 	 * setup rx descriptors. The rx_bufsize here tells the hardware
65203c4805SLuis R. Rodriguez 	 * how much data it can DMA to us and that we are prepared
66cc861f74SLuis R. Rodriguez 	 * to process
67cc861f74SLuis R. Rodriguez 	 */
68203c4805SLuis R. Rodriguez 	ath9k_hw_setuprxdesc(ah, ds,
69cc861f74SLuis R. Rodriguez 			     common->rx_bufsize,
70203c4805SLuis R. Rodriguez 			     0);
71203c4805SLuis R. Rodriguez 
72203c4805SLuis R. Rodriguez 	if (sc->rx.rxlink == NULL)
73203c4805SLuis R. Rodriguez 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
74203c4805SLuis R. Rodriguez 	else
75203c4805SLuis R. Rodriguez 		*sc->rx.rxlink = bf->bf_daddr;
76203c4805SLuis R. Rodriguez 
77203c4805SLuis R. Rodriguez 	sc->rx.rxlink = &ds->ds_link;
78203c4805SLuis R. Rodriguez }
79203c4805SLuis R. Rodriguez 
80203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
81203c4805SLuis R. Rodriguez {
82203c4805SLuis R. Rodriguez 	/* XXX block beacon interrupts */
83203c4805SLuis R. Rodriguez 	ath9k_hw_setantenna(sc->sc_ah, antenna);
84203c4805SLuis R. Rodriguez 	sc->rx.defant = antenna;
85203c4805SLuis R. Rodriguez 	sc->rx.rxotherant = 0;
86203c4805SLuis R. Rodriguez }
87203c4805SLuis R. Rodriguez 
88203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc)
89203c4805SLuis R. Rodriguez {
90203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
911510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
921510718dSLuis R. Rodriguez 
93203c4805SLuis R. Rodriguez 	u32 rfilt, mfilt[2];
94203c4805SLuis R. Rodriguez 
95203c4805SLuis R. Rodriguez 	/* configure rx filter */
96203c4805SLuis R. Rodriguez 	rfilt = ath_calcrxfilter(sc);
97203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, rfilt);
98203c4805SLuis R. Rodriguez 
99203c4805SLuis R. Rodriguez 	/* configure bssid mask */
10013b81559SLuis R. Rodriguez 	ath_hw_setbssidmask(common);
101203c4805SLuis R. Rodriguez 
102203c4805SLuis R. Rodriguez 	/* configure operational mode */
103203c4805SLuis R. Rodriguez 	ath9k_hw_setopmode(ah);
104203c4805SLuis R. Rodriguez 
105203c4805SLuis R. Rodriguez 	/* calculate and install multicast filter */
106203c4805SLuis R. Rodriguez 	mfilt[0] = mfilt[1] = ~0;
107203c4805SLuis R. Rodriguez 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
108203c4805SLuis R. Rodriguez }
109203c4805SLuis R. Rodriguez 
110b5c80475SFelix Fietkau static bool ath_rx_edma_buf_link(struct ath_softc *sc,
111b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
112b5c80475SFelix Fietkau {
113b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
114b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
115b5c80475SFelix Fietkau 	struct sk_buff *skb;
116b5c80475SFelix Fietkau 	struct ath_buf *bf;
117b5c80475SFelix Fietkau 
118b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
119b5c80475SFelix Fietkau 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
120b5c80475SFelix Fietkau 		return false;
121b5c80475SFelix Fietkau 
122b5c80475SFelix Fietkau 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
123b5c80475SFelix Fietkau 	list_del_init(&bf->list);
124b5c80475SFelix Fietkau 
125b5c80475SFelix Fietkau 	skb = bf->bf_mpdu;
126b5c80475SFelix Fietkau 
127b5c80475SFelix Fietkau 	ATH_RXBUF_RESET(bf);
128b5c80475SFelix Fietkau 	memset(skb->data, 0, ah->caps.rx_status_len);
129b5c80475SFelix Fietkau 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
130b5c80475SFelix Fietkau 				ah->caps.rx_status_len, DMA_TO_DEVICE);
131b5c80475SFelix Fietkau 
132b5c80475SFelix Fietkau 	SKB_CB_ATHBUF(skb) = bf;
133b5c80475SFelix Fietkau 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
134b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_fifo, skb);
135b5c80475SFelix Fietkau 
136b5c80475SFelix Fietkau 	return true;
137b5c80475SFelix Fietkau }
138b5c80475SFelix Fietkau 
139b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
140b5c80475SFelix Fietkau 				  enum ath9k_rx_qtype qtype, int size)
141b5c80475SFelix Fietkau {
142b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
143b5c80475SFelix Fietkau 	u32 nbuf = 0;
144b5c80475SFelix Fietkau 
145b5c80475SFelix Fietkau 	if (list_empty(&sc->rx.rxbuf)) {
146226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
147b5c80475SFelix Fietkau 		return;
148b5c80475SFelix Fietkau 	}
149b5c80475SFelix Fietkau 
150b5c80475SFelix Fietkau 	while (!list_empty(&sc->rx.rxbuf)) {
151b5c80475SFelix Fietkau 		nbuf++;
152b5c80475SFelix Fietkau 
153b5c80475SFelix Fietkau 		if (!ath_rx_edma_buf_link(sc, qtype))
154b5c80475SFelix Fietkau 			break;
155b5c80475SFelix Fietkau 
156b5c80475SFelix Fietkau 		if (nbuf >= size)
157b5c80475SFelix Fietkau 			break;
158b5c80475SFelix Fietkau 	}
159b5c80475SFelix Fietkau }
160b5c80475SFelix Fietkau 
161b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
162b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
163b5c80475SFelix Fietkau {
164b5c80475SFelix Fietkau 	struct ath_buf *bf;
165b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma;
166b5c80475SFelix Fietkau 	struct sk_buff *skb;
167b5c80475SFelix Fietkau 
168b5c80475SFelix Fietkau 	rx_edma = &sc->rx.rx_edma[qtype];
169b5c80475SFelix Fietkau 
170b5c80475SFelix Fietkau 	while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
171b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
172b5c80475SFelix Fietkau 		BUG_ON(!bf);
173b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
174b5c80475SFelix Fietkau 	}
175b5c80475SFelix Fietkau }
176b5c80475SFelix Fietkau 
177b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
178b5c80475SFelix Fietkau {
179b5c80475SFelix Fietkau 	struct ath_buf *bf;
180b5c80475SFelix Fietkau 
181b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
182b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
183b5c80475SFelix Fietkau 
184b5c80475SFelix Fietkau 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
185b5c80475SFelix Fietkau 		if (bf->bf_mpdu)
186b5c80475SFelix Fietkau 			dev_kfree_skb_any(bf->bf_mpdu);
187b5c80475SFelix Fietkau 	}
188b5c80475SFelix Fietkau 
189b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
190b5c80475SFelix Fietkau 
191b5c80475SFelix Fietkau 	kfree(sc->rx.rx_bufptr);
192b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = NULL;
193b5c80475SFelix Fietkau }
194b5c80475SFelix Fietkau 
195b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
196b5c80475SFelix Fietkau {
197b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_fifo);
198b5c80475SFelix Fietkau 	skb_queue_head_init(&rx_edma->rx_buffers);
199b5c80475SFelix Fietkau 	rx_edma->rx_fifo_hwsize = size;
200b5c80475SFelix Fietkau }
201b5c80475SFelix Fietkau 
202b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
203b5c80475SFelix Fietkau {
204b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
205b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
206b5c80475SFelix Fietkau 	struct sk_buff *skb;
207b5c80475SFelix Fietkau 	struct ath_buf *bf;
208b5c80475SFelix Fietkau 	int error = 0, i;
209b5c80475SFelix Fietkau 	u32 size;
210b5c80475SFelix Fietkau 
211b5c80475SFelix Fietkau 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
212b5c80475SFelix Fietkau 				    ah->caps.rx_status_len);
213b5c80475SFelix Fietkau 
214b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
215b5c80475SFelix Fietkau 			       ah->caps.rx_lp_qdepth);
216b5c80475SFelix Fietkau 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
217b5c80475SFelix Fietkau 			       ah->caps.rx_hp_qdepth);
218b5c80475SFelix Fietkau 
219b5c80475SFelix Fietkau 	size = sizeof(struct ath_buf) * nbufs;
220b5c80475SFelix Fietkau 	bf = kzalloc(size, GFP_KERNEL);
221b5c80475SFelix Fietkau 	if (!bf)
222b5c80475SFelix Fietkau 		return -ENOMEM;
223b5c80475SFelix Fietkau 
224b5c80475SFelix Fietkau 	INIT_LIST_HEAD(&sc->rx.rxbuf);
225b5c80475SFelix Fietkau 	sc->rx.rx_bufptr = bf;
226b5c80475SFelix Fietkau 
227b5c80475SFelix Fietkau 	for (i = 0; i < nbufs; i++, bf++) {
228b5c80475SFelix Fietkau 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
229b5c80475SFelix Fietkau 		if (!skb) {
230b5c80475SFelix Fietkau 			error = -ENOMEM;
231b5c80475SFelix Fietkau 			goto rx_init_fail;
232b5c80475SFelix Fietkau 		}
233b5c80475SFelix Fietkau 
234b5c80475SFelix Fietkau 		memset(skb->data, 0, common->rx_bufsize);
235b5c80475SFelix Fietkau 		bf->bf_mpdu = skb;
236b5c80475SFelix Fietkau 
237b5c80475SFelix Fietkau 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
238b5c80475SFelix Fietkau 						 common->rx_bufsize,
239b5c80475SFelix Fietkau 						 DMA_BIDIRECTIONAL);
240b5c80475SFelix Fietkau 		if (unlikely(dma_mapping_error(sc->dev,
241b5c80475SFelix Fietkau 						bf->bf_buf_addr))) {
242b5c80475SFelix Fietkau 				dev_kfree_skb_any(skb);
243b5c80475SFelix Fietkau 				bf->bf_mpdu = NULL;
2446cf9e995SBen Greear 				bf->bf_buf_addr = 0;
2453800276aSJoe Perches 				ath_err(common,
246b5c80475SFelix Fietkau 					"dma_mapping_error() on RX init\n");
247b5c80475SFelix Fietkau 				error = -ENOMEM;
248b5c80475SFelix Fietkau 				goto rx_init_fail;
249b5c80475SFelix Fietkau 		}
250b5c80475SFelix Fietkau 
251b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
252b5c80475SFelix Fietkau 	}
253b5c80475SFelix Fietkau 
254b5c80475SFelix Fietkau 	return 0;
255b5c80475SFelix Fietkau 
256b5c80475SFelix Fietkau rx_init_fail:
257b5c80475SFelix Fietkau 	ath_rx_edma_cleanup(sc);
258b5c80475SFelix Fietkau 	return error;
259b5c80475SFelix Fietkau }
260b5c80475SFelix Fietkau 
261b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
262b5c80475SFelix Fietkau {
263b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
264b5c80475SFelix Fietkau 
265b5c80475SFelix Fietkau 	ath9k_hw_rxena(sc->sc_ah);
266b5c80475SFelix Fietkau 
267b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
268b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
269b5c80475SFelix Fietkau 
270b5c80475SFelix Fietkau 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
271b5c80475SFelix Fietkau 			      sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
272b5c80475SFelix Fietkau 
273b5c80475SFelix Fietkau 	ath_opmode_init(sc);
274b5c80475SFelix Fietkau 
27548a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
2767583c550SLuis R. Rodriguez 
2777583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
278b5c80475SFelix Fietkau }
279b5c80475SFelix Fietkau 
280b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
281b5c80475SFelix Fietkau {
282b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
283b5c80475SFelix Fietkau 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
284b5c80475SFelix Fietkau }
285b5c80475SFelix Fietkau 
286203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
287203c4805SLuis R. Rodriguez {
28827c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
289203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
290203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
291203c4805SLuis R. Rodriguez 	int error = 0;
292203c4805SLuis R. Rodriguez 
2934bdd1e97SLuis R. Rodriguez 	spin_lock_init(&sc->sc_pcu_lock);
294203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
295203c4805SLuis R. Rodriguez 	spin_lock_init(&sc->rx.rxbuflock);
296203c4805SLuis R. Rodriguez 
2970d95521eSFelix Fietkau 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
2980d95521eSFelix Fietkau 			     sc->sc_ah->caps.rx_status_len;
2990d95521eSFelix Fietkau 
300b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
301b5c80475SFelix Fietkau 		return ath_rx_edma_init(sc, nbufs);
302b5c80475SFelix Fietkau 	} else {
303226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
304cc861f74SLuis R. Rodriguez 			common->cachelsz, common->rx_bufsize);
305203c4805SLuis R. Rodriguez 
306203c4805SLuis R. Rodriguez 		/* Initialize rx descriptors */
307203c4805SLuis R. Rodriguez 
308203c4805SLuis R. Rodriguez 		error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
3094adfcdedSVasanthakumar Thiagarajan 				"rx", nbufs, 1, 0);
310203c4805SLuis R. Rodriguez 		if (error != 0) {
3113800276aSJoe Perches 			ath_err(common,
312b5c80475SFelix Fietkau 				"failed to allocate rx descriptors: %d\n",
313b5c80475SFelix Fietkau 				error);
314203c4805SLuis R. Rodriguez 			goto err;
315203c4805SLuis R. Rodriguez 		}
316203c4805SLuis R. Rodriguez 
317203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
318b5c80475SFelix Fietkau 			skb = ath_rxbuf_alloc(common, common->rx_bufsize,
319b5c80475SFelix Fietkau 					      GFP_KERNEL);
320203c4805SLuis R. Rodriguez 			if (skb == NULL) {
321203c4805SLuis R. Rodriguez 				error = -ENOMEM;
322203c4805SLuis R. Rodriguez 				goto err;
323203c4805SLuis R. Rodriguez 			}
324203c4805SLuis R. Rodriguez 
325203c4805SLuis R. Rodriguez 			bf->bf_mpdu = skb;
326203c4805SLuis R. Rodriguez 			bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
327cc861f74SLuis R. Rodriguez 					common->rx_bufsize,
328203c4805SLuis R. Rodriguez 					DMA_FROM_DEVICE);
329203c4805SLuis R. Rodriguez 			if (unlikely(dma_mapping_error(sc->dev,
330203c4805SLuis R. Rodriguez 							bf->bf_buf_addr))) {
331203c4805SLuis R. Rodriguez 				dev_kfree_skb_any(skb);
332203c4805SLuis R. Rodriguez 				bf->bf_mpdu = NULL;
3336cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3343800276aSJoe Perches 				ath_err(common,
335203c4805SLuis R. Rodriguez 					"dma_mapping_error() on RX init\n");
336203c4805SLuis R. Rodriguez 				error = -ENOMEM;
337203c4805SLuis R. Rodriguez 				goto err;
338203c4805SLuis R. Rodriguez 			}
339203c4805SLuis R. Rodriguez 		}
340203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
341b5c80475SFelix Fietkau 	}
342203c4805SLuis R. Rodriguez 
343203c4805SLuis R. Rodriguez err:
344203c4805SLuis R. Rodriguez 	if (error)
345203c4805SLuis R. Rodriguez 		ath_rx_cleanup(sc);
346203c4805SLuis R. Rodriguez 
347203c4805SLuis R. Rodriguez 	return error;
348203c4805SLuis R. Rodriguez }
349203c4805SLuis R. Rodriguez 
350203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
351203c4805SLuis R. Rodriguez {
352cc861f74SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
353cc861f74SLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
354203c4805SLuis R. Rodriguez 	struct sk_buff *skb;
355203c4805SLuis R. Rodriguez 	struct ath_buf *bf;
356203c4805SLuis R. Rodriguez 
357b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
358b5c80475SFelix Fietkau 		ath_rx_edma_cleanup(sc);
359b5c80475SFelix Fietkau 		return;
360b5c80475SFelix Fietkau 	} else {
361203c4805SLuis R. Rodriguez 		list_for_each_entry(bf, &sc->rx.rxbuf, list) {
362203c4805SLuis R. Rodriguez 			skb = bf->bf_mpdu;
363203c4805SLuis R. Rodriguez 			if (skb) {
364203c4805SLuis R. Rodriguez 				dma_unmap_single(sc->dev, bf->bf_buf_addr,
365b5c80475SFelix Fietkau 						common->rx_bufsize,
366b5c80475SFelix Fietkau 						DMA_FROM_DEVICE);
367203c4805SLuis R. Rodriguez 				dev_kfree_skb(skb);
3686cf9e995SBen Greear 				bf->bf_buf_addr = 0;
3696cf9e995SBen Greear 				bf->bf_mpdu = NULL;
370203c4805SLuis R. Rodriguez 			}
371203c4805SLuis R. Rodriguez 		}
372203c4805SLuis R. Rodriguez 
373203c4805SLuis R. Rodriguez 		if (sc->rx.rxdma.dd_desc_len != 0)
374203c4805SLuis R. Rodriguez 			ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
375203c4805SLuis R. Rodriguez 	}
376b5c80475SFelix Fietkau }
377203c4805SLuis R. Rodriguez 
378203c4805SLuis R. Rodriguez /*
379203c4805SLuis R. Rodriguez  * Calculate the receive filter according to the
380203c4805SLuis R. Rodriguez  * operating mode and state:
381203c4805SLuis R. Rodriguez  *
382203c4805SLuis R. Rodriguez  * o always accept unicast, broadcast, and multicast traffic
383203c4805SLuis R. Rodriguez  * o maintain current state of phy error reception (the hal
384203c4805SLuis R. Rodriguez  *   may enable phy error frames for noise immunity work)
385203c4805SLuis R. Rodriguez  * o probe request frames are accepted only when operating in
386203c4805SLuis R. Rodriguez  *   hostap, adhoc, or monitor modes
387203c4805SLuis R. Rodriguez  * o enable promiscuous mode according to the interface state
388203c4805SLuis R. Rodriguez  * o accept beacons:
389203c4805SLuis R. Rodriguez  *   - when operating in adhoc mode so the 802.11 layer creates
390203c4805SLuis R. Rodriguez  *     node table entries for peers,
391203c4805SLuis R. Rodriguez  *   - when operating in station mode for collecting rssi data when
392203c4805SLuis R. Rodriguez  *     the station is otherwise quiet, or
393203c4805SLuis R. Rodriguez  *   - when operating as a repeater so we see repeater-sta beacons
394203c4805SLuis R. Rodriguez  *   - when scanning
395203c4805SLuis R. Rodriguez  */
396203c4805SLuis R. Rodriguez 
397203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
398203c4805SLuis R. Rodriguez {
399203c4805SLuis R. Rodriguez #define	RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
400203c4805SLuis R. Rodriguez 
401203c4805SLuis R. Rodriguez 	u32 rfilt;
402203c4805SLuis R. Rodriguez 
403203c4805SLuis R. Rodriguez 	rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
404203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
405203c4805SLuis R. Rodriguez 		| ATH9K_RX_FILTER_MCAST;
406203c4805SLuis R. Rodriguez 
4079c1d8e4aSJouni Malinen 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
408203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
409203c4805SLuis R. Rodriguez 
410203c4805SLuis R. Rodriguez 	/*
411203c4805SLuis R. Rodriguez 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
412203c4805SLuis R. Rodriguez 	 * mode interface or when in monitor mode. AP mode does not need this
413203c4805SLuis R. Rodriguez 	 * since it receives all in-BSS frames anyway.
414203c4805SLuis R. Rodriguez 	 */
4152e286947SFelix Fietkau 	if (sc->sc_ah->is_monitoring)
416203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PROM;
417203c4805SLuis R. Rodriguez 
418203c4805SLuis R. Rodriguez 	if (sc->rx.rxfilter & FIF_CONTROL)
419203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_CONTROL;
420203c4805SLuis R. Rodriguez 
421203c4805SLuis R. Rodriguez 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
422cfda6695SBen Greear 	    (sc->nvifs <= 1) &&
423203c4805SLuis R. Rodriguez 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
424203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
425203c4805SLuis R. Rodriguez 	else
426203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_BEACON;
427203c4805SLuis R. Rodriguez 
428264bbec8SFelix Fietkau 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
42966afad01SSenthil Balasubramanian 	    (sc->rx.rxfilter & FIF_PSPOLL))
430203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
431203c4805SLuis R. Rodriguez 
4327ea310beSSujith 	if (conf_is_ht(&sc->hw->conf))
4337ea310beSSujith 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4347ea310beSSujith 
4357545daf4SFelix Fietkau 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
4365eb6ba83SJavier Cardona 		/* The following may also be needed for other older chips */
4375eb6ba83SJavier Cardona 		if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
4385eb6ba83SJavier Cardona 			rfilt |= ATH9K_RX_FILTER_PROM;
439203c4805SLuis R. Rodriguez 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
440203c4805SLuis R. Rodriguez 	}
441203c4805SLuis R. Rodriguez 
442203c4805SLuis R. Rodriguez 	return rfilt;
443203c4805SLuis R. Rodriguez 
444203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE
445203c4805SLuis R. Rodriguez }
446203c4805SLuis R. Rodriguez 
447203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc)
448203c4805SLuis R. Rodriguez {
449203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
450203c4805SLuis R. Rodriguez 	struct ath_buf *bf, *tbf;
451203c4805SLuis R. Rodriguez 
452b5c80475SFelix Fietkau 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
453b5c80475SFelix Fietkau 		ath_edma_start_recv(sc);
454b5c80475SFelix Fietkau 		return 0;
455b5c80475SFelix Fietkau 	}
456b5c80475SFelix Fietkau 
457203c4805SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
458203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
459203c4805SLuis R. Rodriguez 		goto start_recv;
460203c4805SLuis R. Rodriguez 
461203c4805SLuis R. Rodriguez 	sc->rx.rxlink = NULL;
462203c4805SLuis R. Rodriguez 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
463203c4805SLuis R. Rodriguez 		ath_rx_buf_link(sc, bf);
464203c4805SLuis R. Rodriguez 	}
465203c4805SLuis R. Rodriguez 
466203c4805SLuis R. Rodriguez 	/* We could have deleted elements so the list may be empty now */
467203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf))
468203c4805SLuis R. Rodriguez 		goto start_recv;
469203c4805SLuis R. Rodriguez 
470203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
471203c4805SLuis R. Rodriguez 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
472203c4805SLuis R. Rodriguez 	ath9k_hw_rxena(ah);
473203c4805SLuis R. Rodriguez 
474203c4805SLuis R. Rodriguez start_recv:
475203c4805SLuis R. Rodriguez 	ath_opmode_init(sc);
47648a6a468SLuis R. Rodriguez 	ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
477203c4805SLuis R. Rodriguez 
4787583c550SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
4797583c550SLuis R. Rodriguez 
480203c4805SLuis R. Rodriguez 	return 0;
481203c4805SLuis R. Rodriguez }
482203c4805SLuis R. Rodriguez 
483203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
484203c4805SLuis R. Rodriguez {
485203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
486203c4805SLuis R. Rodriguez 	bool stopped;
487203c4805SLuis R. Rodriguez 
4881e450285SLuis R. Rodriguez 	spin_lock_bh(&sc->rx.rxbuflock);
489d47844a0SFelix Fietkau 	ath9k_hw_abortpcurecv(ah);
490203c4805SLuis R. Rodriguez 	ath9k_hw_setrxfilter(ah, 0);
491203c4805SLuis R. Rodriguez 	stopped = ath9k_hw_stopdmarecv(ah);
492b5c80475SFelix Fietkau 
493b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
494b5c80475SFelix Fietkau 		ath_edma_stop_recv(sc);
495b5c80475SFelix Fietkau 	else
496203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
4971e450285SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
498203c4805SLuis R. Rodriguez 
499d584747bSRajkumar Manoharan 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
500d584747bSRajkumar Manoharan 	    unlikely(!stopped)) {
501d7fd1b50SBen Greear 		ath_err(ath9k_hw_common(sc->sc_ah),
502d7fd1b50SBen Greear 			"Could not stop RX, we could be "
50378a7685eSLuis R. Rodriguez 			"confusing the DMA engine when we start RX up\n");
504d7fd1b50SBen Greear 		ATH_DBG_WARN_ON_ONCE(!stopped);
505d7fd1b50SBen Greear 	}
506203c4805SLuis R. Rodriguez 	return stopped;
507203c4805SLuis R. Rodriguez }
508203c4805SLuis R. Rodriguez 
509203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc)
510203c4805SLuis R. Rodriguez {
511203c4805SLuis R. Rodriguez 	sc->sc_flags |= SC_OP_RXFLUSH;
512b5c80475SFelix Fietkau 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
513b5c80475SFelix Fietkau 		ath_rx_tasklet(sc, 1, true);
514b5c80475SFelix Fietkau 	ath_rx_tasklet(sc, 1, false);
515203c4805SLuis R. Rodriguez 	sc->sc_flags &= ~SC_OP_RXFLUSH;
516203c4805SLuis R. Rodriguez }
517203c4805SLuis R. Rodriguez 
518cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
519cc65965cSJouni Malinen {
520cc65965cSJouni Malinen 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
521cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
522cc65965cSJouni Malinen 	u8 *pos, *end, id, elen;
523cc65965cSJouni Malinen 	struct ieee80211_tim_ie *tim;
524cc65965cSJouni Malinen 
525cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
526cc65965cSJouni Malinen 	pos = mgmt->u.beacon.variable;
527cc65965cSJouni Malinen 	end = skb->data + skb->len;
528cc65965cSJouni Malinen 
529cc65965cSJouni Malinen 	while (pos + 2 < end) {
530cc65965cSJouni Malinen 		id = *pos++;
531cc65965cSJouni Malinen 		elen = *pos++;
532cc65965cSJouni Malinen 		if (pos + elen > end)
533cc65965cSJouni Malinen 			break;
534cc65965cSJouni Malinen 
535cc65965cSJouni Malinen 		if (id == WLAN_EID_TIM) {
536cc65965cSJouni Malinen 			if (elen < sizeof(*tim))
537cc65965cSJouni Malinen 				break;
538cc65965cSJouni Malinen 			tim = (struct ieee80211_tim_ie *) pos;
539cc65965cSJouni Malinen 			if (tim->dtim_count != 0)
540cc65965cSJouni Malinen 				break;
541cc65965cSJouni Malinen 			return tim->bitmap_ctrl & 0x01;
542cc65965cSJouni Malinen 		}
543cc65965cSJouni Malinen 
544cc65965cSJouni Malinen 		pos += elen;
545cc65965cSJouni Malinen 	}
546cc65965cSJouni Malinen 
547cc65965cSJouni Malinen 	return false;
548cc65965cSJouni Malinen }
549cc65965cSJouni Malinen 
550cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
551cc65965cSJouni Malinen {
552cc65965cSJouni Malinen 	struct ieee80211_mgmt *mgmt;
5531510718dSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
554cc65965cSJouni Malinen 
555cc65965cSJouni Malinen 	if (skb->len < 24 + 8 + 2 + 2)
556cc65965cSJouni Malinen 		return;
557cc65965cSJouni Malinen 
558cc65965cSJouni Malinen 	mgmt = (struct ieee80211_mgmt *)skb->data;
5594801416cSBen Greear 	if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
5604801416cSBen Greear 		/* TODO:  This doesn't work well if you have stations
5614801416cSBen Greear 		 * associated to two different APs because curbssid
5624801416cSBen Greear 		 * is just the last AP that any of the stations associated
5634801416cSBen Greear 		 * with.
5644801416cSBen Greear 		 */
565cc65965cSJouni Malinen 		return; /* not from our current AP */
5664801416cSBen Greear 	}
567cc65965cSJouni Malinen 
5681b04b930SSujith 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
569293dc5dfSGabor Juhos 
5701b04b930SSujith 	if (sc->ps_flags & PS_BEACON_SYNC) {
5711b04b930SSujith 		sc->ps_flags &= ~PS_BEACON_SYNC;
572226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
573226afe68SJoe Perches 			"Reconfigure Beacon timers based on timestamp from the AP\n");
57499e4d43aSRajkumar Manoharan 		ath_set_beacon(sc);
575ccdfeab6SJouni Malinen 	}
576ccdfeab6SJouni Malinen 
577cc65965cSJouni Malinen 	if (ath_beacon_dtim_pending_cab(skb)) {
578cc65965cSJouni Malinen 		/*
579cc65965cSJouni Malinen 		 * Remain awake waiting for buffered broadcast/multicast
58058f5fffdSGabor Juhos 		 * frames. If the last broadcast/multicast frame is not
58158f5fffdSGabor Juhos 		 * received properly, the next beacon frame will work as
58258f5fffdSGabor Juhos 		 * a backup trigger for returning into NETWORK SLEEP state,
58358f5fffdSGabor Juhos 		 * so we are waiting for it as well.
584cc65965cSJouni Malinen 		 */
585226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
586226afe68SJoe Perches 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
5871b04b930SSujith 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
588cc65965cSJouni Malinen 		return;
589cc65965cSJouni Malinen 	}
590cc65965cSJouni Malinen 
5911b04b930SSujith 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
592cc65965cSJouni Malinen 		/*
593cc65965cSJouni Malinen 		 * This can happen if a broadcast frame is dropped or the AP
594cc65965cSJouni Malinen 		 * fails to send a frame indicating that all CAB frames have
595cc65965cSJouni Malinen 		 * been delivered.
596cc65965cSJouni Malinen 		 */
5971b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
598226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
599c46917bbSLuis R. Rodriguez 			"PS wait for CAB frames timed out\n");
600cc65965cSJouni Malinen 	}
601cc65965cSJouni Malinen }
602cc65965cSJouni Malinen 
603cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
604cc65965cSJouni Malinen {
605cc65965cSJouni Malinen 	struct ieee80211_hdr *hdr;
606c46917bbSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
607cc65965cSJouni Malinen 
608cc65965cSJouni Malinen 	hdr = (struct ieee80211_hdr *)skb->data;
609cc65965cSJouni Malinen 
610cc65965cSJouni Malinen 	/* Process Beacon and CAB receive in PS state */
611ededf1f8SVasanthakumar Thiagarajan 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
612ededf1f8SVasanthakumar Thiagarajan 	    && ieee80211_is_beacon(hdr->frame_control))
613cc65965cSJouni Malinen 		ath_rx_ps_beacon(sc, skb);
6141b04b930SSujith 	else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
615cc65965cSJouni Malinen 		 (ieee80211_is_data(hdr->frame_control) ||
616cc65965cSJouni Malinen 		  ieee80211_is_action(hdr->frame_control)) &&
617cc65965cSJouni Malinen 		 is_multicast_ether_addr(hdr->addr1) &&
618cc65965cSJouni Malinen 		 !ieee80211_has_moredata(hdr->frame_control)) {
619cc65965cSJouni Malinen 		/*
620cc65965cSJouni Malinen 		 * No more broadcast/multicast frames to be received at this
621cc65965cSJouni Malinen 		 * point.
622cc65965cSJouni Malinen 		 */
6233fac6dfdSSenthil Balasubramanian 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
624226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
625c46917bbSLuis R. Rodriguez 			"All PS CAB frames received, back to sleep\n");
6261b04b930SSujith 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6279a23f9caSJouni Malinen 		   !is_multicast_ether_addr(hdr->addr1) &&
6289a23f9caSJouni Malinen 		   !ieee80211_has_morefrags(hdr->frame_control)) {
6291b04b930SSujith 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
630226afe68SJoe Perches 		ath_dbg(common, ATH_DBG_PS,
631226afe68SJoe Perches 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
6321b04b930SSujith 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
6331b04b930SSujith 					PS_WAIT_FOR_CAB |
6341b04b930SSujith 					PS_WAIT_FOR_PSPOLL_DATA |
6351b04b930SSujith 					PS_WAIT_FOR_TX_ACK));
636cc65965cSJouni Malinen 	}
637cc65965cSJouni Malinen }
638cc65965cSJouni Malinen 
639b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
640b5c80475SFelix Fietkau 				 enum ath9k_rx_qtype qtype)
641203c4805SLuis R. Rodriguez {
642b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
643203c4805SLuis R. Rodriguez 	struct ath_hw *ah = sc->sc_ah;
64427c51f1aSLuis R. Rodriguez 	struct ath_common *common = ath9k_hw_common(ah);
645b5c80475SFelix Fietkau 	struct sk_buff *skb;
646b5c80475SFelix Fietkau 	struct ath_buf *bf;
647b5c80475SFelix Fietkau 	int ret;
648203c4805SLuis R. Rodriguez 
649b5c80475SFelix Fietkau 	skb = skb_peek(&rx_edma->rx_fifo);
650b5c80475SFelix Fietkau 	if (!skb)
651b5c80475SFelix Fietkau 		return false;
652203c4805SLuis R. Rodriguez 
653b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
654b5c80475SFelix Fietkau 	BUG_ON(!bf);
655b5c80475SFelix Fietkau 
656ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
657b5c80475SFelix Fietkau 				common->rx_bufsize, DMA_FROM_DEVICE);
658b5c80475SFelix Fietkau 
659b5c80475SFelix Fietkau 	ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
660ce9426d1SMing Lei 	if (ret == -EINPROGRESS) {
661ce9426d1SMing Lei 		/*let device gain the buffer again*/
662ce9426d1SMing Lei 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
663ce9426d1SMing Lei 				common->rx_bufsize, DMA_FROM_DEVICE);
664b5c80475SFelix Fietkau 		return false;
665ce9426d1SMing Lei 	}
666b5c80475SFelix Fietkau 
667b5c80475SFelix Fietkau 	__skb_unlink(skb, &rx_edma->rx_fifo);
668b5c80475SFelix Fietkau 	if (ret == -EINVAL) {
669b5c80475SFelix Fietkau 		/* corrupt descriptor, skip this one and the following one */
670b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
671b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
672b5c80475SFelix Fietkau 		skb = skb_peek(&rx_edma->rx_fifo);
673b5c80475SFelix Fietkau 		if (!skb)
674b5c80475SFelix Fietkau 			return true;
675b5c80475SFelix Fietkau 
676b5c80475SFelix Fietkau 		bf = SKB_CB_ATHBUF(skb);
677b5c80475SFelix Fietkau 		BUG_ON(!bf);
678b5c80475SFelix Fietkau 
679b5c80475SFelix Fietkau 		__skb_unlink(skb, &rx_edma->rx_fifo);
680b5c80475SFelix Fietkau 		list_add_tail(&bf->list, &sc->rx.rxbuf);
681b5c80475SFelix Fietkau 		ath_rx_edma_buf_link(sc, qtype);
682083e3e8dSVasanthakumar Thiagarajan 		return true;
683b5c80475SFelix Fietkau 	}
684b5c80475SFelix Fietkau 	skb_queue_tail(&rx_edma->rx_buffers, skb);
685b5c80475SFelix Fietkau 
686b5c80475SFelix Fietkau 	return true;
687b5c80475SFelix Fietkau }
688b5c80475SFelix Fietkau 
689b5c80475SFelix Fietkau static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
690b5c80475SFelix Fietkau 						struct ath_rx_status *rs,
691b5c80475SFelix Fietkau 						enum ath9k_rx_qtype qtype)
692b5c80475SFelix Fietkau {
693b5c80475SFelix Fietkau 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
694b5c80475SFelix Fietkau 	struct sk_buff *skb;
695b5c80475SFelix Fietkau 	struct ath_buf *bf;
696b5c80475SFelix Fietkau 
697b5c80475SFelix Fietkau 	while (ath_edma_get_buffers(sc, qtype));
698b5c80475SFelix Fietkau 	skb = __skb_dequeue(&rx_edma->rx_buffers);
699b5c80475SFelix Fietkau 	if (!skb)
700b5c80475SFelix Fietkau 		return NULL;
701b5c80475SFelix Fietkau 
702b5c80475SFelix Fietkau 	bf = SKB_CB_ATHBUF(skb);
703b5c80475SFelix Fietkau 	ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
704b5c80475SFelix Fietkau 	return bf;
705b5c80475SFelix Fietkau }
706b5c80475SFelix Fietkau 
707b5c80475SFelix Fietkau static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
708b5c80475SFelix Fietkau 					   struct ath_rx_status *rs)
709b5c80475SFelix Fietkau {
710b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
711b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
712b5c80475SFelix Fietkau 	struct ath_desc *ds;
713b5c80475SFelix Fietkau 	struct ath_buf *bf;
714b5c80475SFelix Fietkau 	int ret;
715203c4805SLuis R. Rodriguez 
716203c4805SLuis R. Rodriguez 	if (list_empty(&sc->rx.rxbuf)) {
717203c4805SLuis R. Rodriguez 		sc->rx.rxlink = NULL;
718b5c80475SFelix Fietkau 		return NULL;
719203c4805SLuis R. Rodriguez 	}
720203c4805SLuis R. Rodriguez 
721203c4805SLuis R. Rodriguez 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
722203c4805SLuis R. Rodriguez 	ds = bf->bf_desc;
723203c4805SLuis R. Rodriguez 
724203c4805SLuis R. Rodriguez 	/*
725203c4805SLuis R. Rodriguez 	 * Must provide the virtual address of the current
726203c4805SLuis R. Rodriguez 	 * descriptor, the physical address, and the virtual
727203c4805SLuis R. Rodriguez 	 * address of the next descriptor in the h/w chain.
728203c4805SLuis R. Rodriguez 	 * This allows the HAL to look ahead to see if the
729203c4805SLuis R. Rodriguez 	 * hardware is done with a descriptor by checking the
730203c4805SLuis R. Rodriguez 	 * done bit in the following descriptor and the address
731203c4805SLuis R. Rodriguez 	 * of the current descriptor the DMA engine is working
732203c4805SLuis R. Rodriguez 	 * on.  All this is necessary because of our use of
733203c4805SLuis R. Rodriguez 	 * a self-linked list to avoid rx overruns.
734203c4805SLuis R. Rodriguez 	 */
735b5c80475SFelix Fietkau 	ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
736b5c80475SFelix Fietkau 	if (ret == -EINPROGRESS) {
73729bffa96SFelix Fietkau 		struct ath_rx_status trs;
738203c4805SLuis R. Rodriguez 		struct ath_buf *tbf;
739203c4805SLuis R. Rodriguez 		struct ath_desc *tds;
740203c4805SLuis R. Rodriguez 
74129bffa96SFelix Fietkau 		memset(&trs, 0, sizeof(trs));
742203c4805SLuis R. Rodriguez 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
743203c4805SLuis R. Rodriguez 			sc->rx.rxlink = NULL;
744b5c80475SFelix Fietkau 			return NULL;
745203c4805SLuis R. Rodriguez 		}
746203c4805SLuis R. Rodriguez 
747203c4805SLuis R. Rodriguez 		tbf = list_entry(bf->list.next, struct ath_buf, list);
748203c4805SLuis R. Rodriguez 
749203c4805SLuis R. Rodriguez 		/*
750203c4805SLuis R. Rodriguez 		 * On some hardware the descriptor status words could
751203c4805SLuis R. Rodriguez 		 * get corrupted, including the done bit. Because of
752203c4805SLuis R. Rodriguez 		 * this, check if the next descriptor's done bit is
753203c4805SLuis R. Rodriguez 		 * set or not.
754203c4805SLuis R. Rodriguez 		 *
755203c4805SLuis R. Rodriguez 		 * If the next descriptor's done bit is set, the current
756203c4805SLuis R. Rodriguez 		 * descriptor has been corrupted. Force s/w to discard
757203c4805SLuis R. Rodriguez 		 * this descriptor and continue...
758203c4805SLuis R. Rodriguez 		 */
759203c4805SLuis R. Rodriguez 
760203c4805SLuis R. Rodriguez 		tds = tbf->bf_desc;
761b5c80475SFelix Fietkau 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
762b5c80475SFelix Fietkau 		if (ret == -EINPROGRESS)
763b5c80475SFelix Fietkau 			return NULL;
764203c4805SLuis R. Rodriguez 	}
765203c4805SLuis R. Rodriguez 
766b5c80475SFelix Fietkau 	if (!bf->bf_mpdu)
767b5c80475SFelix Fietkau 		return bf;
768203c4805SLuis R. Rodriguez 
769203c4805SLuis R. Rodriguez 	/*
770203c4805SLuis R. Rodriguez 	 * Synchronize the DMA transfer with CPU before
771203c4805SLuis R. Rodriguez 	 * 1. accessing the frame
772203c4805SLuis R. Rodriguez 	 * 2. requeueing the same buffer to h/w
773203c4805SLuis R. Rodriguez 	 */
774ce9426d1SMing Lei 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
775cc861f74SLuis R. Rodriguez 			common->rx_bufsize,
776203c4805SLuis R. Rodriguez 			DMA_FROM_DEVICE);
777203c4805SLuis R. Rodriguez 
778b5c80475SFelix Fietkau 	return bf;
779b5c80475SFelix Fietkau }
780b5c80475SFelix Fietkau 
781d435700fSSujith /* Assumes you've already done the endian to CPU conversion */
782d435700fSSujith static bool ath9k_rx_accept(struct ath_common *common,
7839f167f64SVasanthakumar Thiagarajan 			    struct ieee80211_hdr *hdr,
784d435700fSSujith 			    struct ieee80211_rx_status *rxs,
785d435700fSSujith 			    struct ath_rx_status *rx_stats,
786d435700fSSujith 			    bool *decrypt_error)
787d435700fSSujith {
78838852b20SSenthil Balasubramanian #define is_mc_or_valid_tkip_keyix ((is_mc ||			\
78938852b20SSenthil Balasubramanian 		(rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
79038852b20SSenthil Balasubramanian 		test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
79138852b20SSenthil Balasubramanian 
792d435700fSSujith 	struct ath_hw *ah = common->ah;
793d435700fSSujith 	__le16 fc;
794b7b1b512SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
795d435700fSSujith 
796d435700fSSujith 	fc = hdr->frame_control;
797d435700fSSujith 
798d435700fSSujith 	if (!rx_stats->rs_datalen)
799d435700fSSujith 		return false;
800d435700fSSujith         /*
801d435700fSSujith          * rs_status follows rs_datalen so if rs_datalen is too large
802d435700fSSujith          * we can take a hint that hardware corrupted it, so ignore
803d435700fSSujith          * those frames.
804d435700fSSujith          */
805b7b1b512SVasanthakumar Thiagarajan 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
806d435700fSSujith 		return false;
807d435700fSSujith 
8080d95521eSFelix Fietkau 	/* Only use error bits from the last fragment */
809d435700fSSujith 	if (rx_stats->rs_more)
8100d95521eSFelix Fietkau 		return true;
811d435700fSSujith 
812d435700fSSujith 	/*
813d435700fSSujith 	 * The rx_stats->rs_status will not be set until the end of the
814d435700fSSujith 	 * chained descriptors so it can be ignored if rs_more is set. The
815d435700fSSujith 	 * rs_more will be false at the last element of the chained
816d435700fSSujith 	 * descriptors.
817d435700fSSujith 	 */
818d435700fSSujith 	if (rx_stats->rs_status != 0) {
819d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_CRC)
820d435700fSSujith 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
821d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
822d435700fSSujith 			return false;
823d435700fSSujith 
824d435700fSSujith 		if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
825d435700fSSujith 			*decrypt_error = true;
826d435700fSSujith 		} else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
82738852b20SSenthil Balasubramanian 			bool is_mc;
828d435700fSSujith 			/*
82956363ddeSFelix Fietkau 			 * The MIC error bit is only valid if the frame
83056363ddeSFelix Fietkau 			 * is not a control frame or fragment, and it was
83156363ddeSFelix Fietkau 			 * decrypted using a valid TKIP key.
832d435700fSSujith 			 */
83338852b20SSenthil Balasubramanian 			is_mc = !!is_multicast_ether_addr(hdr->addr1);
83438852b20SSenthil Balasubramanian 
83556363ddeSFelix Fietkau 			if (!ieee80211_is_ctl(fc) &&
83656363ddeSFelix Fietkau 			    !ieee80211_has_morefrags(fc) &&
83756363ddeSFelix Fietkau 			    !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
83838852b20SSenthil Balasubramanian 			    is_mc_or_valid_tkip_keyix)
839d435700fSSujith 				rxs->flag |= RX_FLAG_MMIC_ERROR;
84056363ddeSFelix Fietkau 			else
84156363ddeSFelix Fietkau 				rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
842d435700fSSujith 		}
843d435700fSSujith 		/*
844d435700fSSujith 		 * Reject error frames with the exception of
845d435700fSSujith 		 * decryption and MIC failures. For monitor mode,
846d435700fSSujith 		 * we also ignore the CRC error.
847d435700fSSujith 		 */
8485f841b41SRajkumar Manoharan 		if (ah->is_monitoring) {
849d435700fSSujith 			if (rx_stats->rs_status &
850d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
851d435700fSSujith 			      ATH9K_RXERR_CRC))
852d435700fSSujith 				return false;
853d435700fSSujith 		} else {
854d435700fSSujith 			if (rx_stats->rs_status &
855d435700fSSujith 			    ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
856d435700fSSujith 				return false;
857d435700fSSujith 			}
858d435700fSSujith 		}
859d435700fSSujith 	}
860d435700fSSujith 	return true;
861d435700fSSujith }
862d435700fSSujith 
863d435700fSSujith static int ath9k_process_rate(struct ath_common *common,
864d435700fSSujith 			      struct ieee80211_hw *hw,
865d435700fSSujith 			      struct ath_rx_status *rx_stats,
8669f167f64SVasanthakumar Thiagarajan 			      struct ieee80211_rx_status *rxs)
867d435700fSSujith {
868d435700fSSujith 	struct ieee80211_supported_band *sband;
869d435700fSSujith 	enum ieee80211_band band;
870d435700fSSujith 	unsigned int i = 0;
871d435700fSSujith 
872d435700fSSujith 	band = hw->conf.channel->band;
873d435700fSSujith 	sband = hw->wiphy->bands[band];
874d435700fSSujith 
875d435700fSSujith 	if (rx_stats->rs_rate & 0x80) {
876d435700fSSujith 		/* HT rate */
877d435700fSSujith 		rxs->flag |= RX_FLAG_HT;
878d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_2040)
879d435700fSSujith 			rxs->flag |= RX_FLAG_40MHZ;
880d435700fSSujith 		if (rx_stats->rs_flags & ATH9K_RX_GI)
881d435700fSSujith 			rxs->flag |= RX_FLAG_SHORT_GI;
882d435700fSSujith 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
883d435700fSSujith 		return 0;
884d435700fSSujith 	}
885d435700fSSujith 
886d435700fSSujith 	for (i = 0; i < sband->n_bitrates; i++) {
887d435700fSSujith 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
888d435700fSSujith 			rxs->rate_idx = i;
889d435700fSSujith 			return 0;
890d435700fSSujith 		}
891d435700fSSujith 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
892d435700fSSujith 			rxs->flag |= RX_FLAG_SHORTPRE;
893d435700fSSujith 			rxs->rate_idx = i;
894d435700fSSujith 			return 0;
895d435700fSSujith 		}
896d435700fSSujith 	}
897d435700fSSujith 
898d435700fSSujith 	/*
899d435700fSSujith 	 * No valid hardware bitrate found -- we should not get here
900d435700fSSujith 	 * because hardware has already validated this frame as OK.
901d435700fSSujith 	 */
902226afe68SJoe Perches 	ath_dbg(common, ATH_DBG_XMIT,
903226afe68SJoe Perches 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
904226afe68SJoe Perches 		rx_stats->rs_rate);
905d435700fSSujith 
906d435700fSSujith 	return -EINVAL;
907d435700fSSujith }
908d435700fSSujith 
909d435700fSSujith static void ath9k_process_rssi(struct ath_common *common,
910d435700fSSujith 			       struct ieee80211_hw *hw,
9119f167f64SVasanthakumar Thiagarajan 			       struct ieee80211_hdr *hdr,
912d435700fSSujith 			       struct ath_rx_status *rx_stats)
913d435700fSSujith {
9149ac58615SFelix Fietkau 	struct ath_softc *sc = hw->priv;
915d435700fSSujith 	struct ath_hw *ah = common->ah;
9169fa23e17SFelix Fietkau 	int last_rssi;
917d435700fSSujith 	__le16 fc;
918d435700fSSujith 
9199fa23e17SFelix Fietkau 	if (ah->opmode != NL80211_IFTYPE_STATION)
9209fa23e17SFelix Fietkau 		return;
9219fa23e17SFelix Fietkau 
922d435700fSSujith 	fc = hdr->frame_control;
9239fa23e17SFelix Fietkau 	if (!ieee80211_is_beacon(fc) ||
9244801416cSBen Greear 	    compare_ether_addr(hdr->addr3, common->curbssid)) {
9254801416cSBen Greear 		/* TODO:  This doesn't work well if you have stations
9264801416cSBen Greear 		 * associated to two different APs because curbssid
9274801416cSBen Greear 		 * is just the last AP that any of the stations associated
9284801416cSBen Greear 		 * with.
9294801416cSBen Greear 		 */
9309fa23e17SFelix Fietkau 		return;
9314801416cSBen Greear 	}
932d435700fSSujith 
9339fa23e17SFelix Fietkau 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9349ac58615SFelix Fietkau 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
935686b9cb9SBen Greear 
9369ac58615SFelix Fietkau 	last_rssi = sc->last_rssi;
937d435700fSSujith 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
938d435700fSSujith 		rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
939d435700fSSujith 					      ATH_RSSI_EP_MULTIPLIER);
940d435700fSSujith 	if (rx_stats->rs_rssi < 0)
941d435700fSSujith 		rx_stats->rs_rssi = 0;
942d435700fSSujith 
943d435700fSSujith 	/* Update Beacon RSSI, this is used by ANI. */
944d435700fSSujith 	ah->stats.avgbrssi = rx_stats->rs_rssi;
945d435700fSSujith }
946d435700fSSujith 
947d435700fSSujith /*
948d435700fSSujith  * For Decrypt or Demic errors, we only mark packet status here and always push
949d435700fSSujith  * up the frame up to let mac80211 handle the actual error case, be it no
950d435700fSSujith  * decryption key or real decryption error. This let us keep statistics there.
951d435700fSSujith  */
952d435700fSSujith static int ath9k_rx_skb_preprocess(struct ath_common *common,
953d435700fSSujith 				   struct ieee80211_hw *hw,
9549f167f64SVasanthakumar Thiagarajan 				   struct ieee80211_hdr *hdr,
955d435700fSSujith 				   struct ath_rx_status *rx_stats,
956d435700fSSujith 				   struct ieee80211_rx_status *rx_status,
957d435700fSSujith 				   bool *decrypt_error)
958d435700fSSujith {
959d435700fSSujith 	memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
960d435700fSSujith 
961d435700fSSujith 	/*
962d435700fSSujith 	 * everything but the rate is checked here, the rate check is done
963d435700fSSujith 	 * separately to avoid doing two lookups for a rate for each frame.
964d435700fSSujith 	 */
9659f167f64SVasanthakumar Thiagarajan 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
966d435700fSSujith 		return -EINVAL;
967d435700fSSujith 
9680d95521eSFelix Fietkau 	/* Only use status info from the last fragment */
9690d95521eSFelix Fietkau 	if (rx_stats->rs_more)
9700d95521eSFelix Fietkau 		return 0;
9710d95521eSFelix Fietkau 
9729f167f64SVasanthakumar Thiagarajan 	ath9k_process_rssi(common, hw, hdr, rx_stats);
973d435700fSSujith 
9749f167f64SVasanthakumar Thiagarajan 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
975d435700fSSujith 		return -EINVAL;
976d435700fSSujith 
977d435700fSSujith 	rx_status->band = hw->conf.channel->band;
978d435700fSSujith 	rx_status->freq = hw->conf.channel->center_freq;
979d435700fSSujith 	rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
980d435700fSSujith 	rx_status->antenna = rx_stats->rs_antenna;
9816ebacbb7SJohannes Berg 	rx_status->flag |= RX_FLAG_MACTIME_MPDU;
982d435700fSSujith 
983d435700fSSujith 	return 0;
984d435700fSSujith }
985d435700fSSujith 
986d435700fSSujith static void ath9k_rx_skb_postprocess(struct ath_common *common,
987d435700fSSujith 				     struct sk_buff *skb,
988d435700fSSujith 				     struct ath_rx_status *rx_stats,
989d435700fSSujith 				     struct ieee80211_rx_status *rxs,
990d435700fSSujith 				     bool decrypt_error)
991d435700fSSujith {
992d435700fSSujith 	struct ath_hw *ah = common->ah;
993d435700fSSujith 	struct ieee80211_hdr *hdr;
994d435700fSSujith 	int hdrlen, padpos, padsize;
995d435700fSSujith 	u8 keyix;
996d435700fSSujith 	__le16 fc;
997d435700fSSujith 
998d435700fSSujith 	/* see if any padding is done by the hw and remove it */
999d435700fSSujith 	hdr = (struct ieee80211_hdr *) skb->data;
1000d435700fSSujith 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1001d435700fSSujith 	fc = hdr->frame_control;
1002d435700fSSujith 	padpos = ath9k_cmn_padpos(hdr->frame_control);
1003d435700fSSujith 
1004d435700fSSujith 	/* The MAC header is padded to have 32-bit boundary if the
1005d435700fSSujith 	 * packet payload is non-zero. The general calculation for
1006d435700fSSujith 	 * padsize would take into account odd header lengths:
1007d435700fSSujith 	 * padsize = (4 - padpos % 4) % 4; However, since only
1008d435700fSSujith 	 * even-length headers are used, padding can only be 0 or 2
1009d435700fSSujith 	 * bytes and we can optimize this a bit. In addition, we must
1010d435700fSSujith 	 * not try to remove padding from short control frames that do
1011d435700fSSujith 	 * not have payload. */
1012d435700fSSujith 	padsize = padpos & 3;
1013d435700fSSujith 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1014d435700fSSujith 		memmove(skb->data + padsize, skb->data, padpos);
1015d435700fSSujith 		skb_pull(skb, padsize);
1016d435700fSSujith 	}
1017d435700fSSujith 
1018d435700fSSujith 	keyix = rx_stats->rs_keyix;
1019d435700fSSujith 
1020d435700fSSujith 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1021d435700fSSujith 	    ieee80211_has_protected(fc)) {
1022d435700fSSujith 		rxs->flag |= RX_FLAG_DECRYPTED;
1023d435700fSSujith 	} else if (ieee80211_has_protected(fc)
1024d435700fSSujith 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1025d435700fSSujith 		keyix = skb->data[hdrlen + 3] >> 6;
1026d435700fSSujith 
1027d435700fSSujith 		if (test_bit(keyix, common->keymap))
1028d435700fSSujith 			rxs->flag |= RX_FLAG_DECRYPTED;
1029d435700fSSujith 	}
1030d435700fSSujith 	if (ah->sw_mgmt_crypto &&
1031d435700fSSujith 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1032d435700fSSujith 	    ieee80211_is_mgmt(fc))
1033d435700fSSujith 		/* Use software decrypt for management frames. */
1034d435700fSSujith 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1035d435700fSSujith }
1036b5c80475SFelix Fietkau 
1037102885a5SVasanthakumar Thiagarajan static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1038102885a5SVasanthakumar Thiagarajan 				      struct ath_hw_antcomb_conf ant_conf,
1039102885a5SVasanthakumar Thiagarajan 				      int main_rssi_avg)
1040102885a5SVasanthakumar Thiagarajan {
1041102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt = 0;
1042102885a5SVasanthakumar Thiagarajan 
1043102885a5SVasanthakumar Thiagarajan 	if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1044102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna2 = main_rssi_avg;
1045102885a5SVasanthakumar Thiagarajan 	else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1046102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_lna1 = main_rssi_avg;
1047102885a5SVasanthakumar Thiagarajan 
1048102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1049102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1050102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1051102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1052102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1053102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1054102885a5SVasanthakumar Thiagarajan 		break;
1055102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1056102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1057102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1058102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1059102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1060102885a5SVasanthakumar Thiagarajan 		break;
1061102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1062102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1063102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1064102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1065102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1066102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1067102885a5SVasanthakumar Thiagarajan 		break;
1068102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1069102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1070102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1071102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1072102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf =
1073102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1074102885a5SVasanthakumar Thiagarajan 		break;
1075102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1076102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1077102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1078102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1079102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1080102885a5SVasanthakumar Thiagarajan 		break;
1081102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1082102885a5SVasanthakumar Thiagarajan 		antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1083102885a5SVasanthakumar Thiagarajan 		antcomb->first_quick_scan_conf =
1084102885a5SVasanthakumar Thiagarajan 			ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1085102885a5SVasanthakumar Thiagarajan 		antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1086102885a5SVasanthakumar Thiagarajan 		break;
1087102885a5SVasanthakumar Thiagarajan 	default:
1088102885a5SVasanthakumar Thiagarajan 		break;
1089102885a5SVasanthakumar Thiagarajan 	}
1090102885a5SVasanthakumar Thiagarajan }
1091102885a5SVasanthakumar Thiagarajan 
1092102885a5SVasanthakumar Thiagarajan static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1093102885a5SVasanthakumar Thiagarajan 				struct ath_hw_antcomb_conf *div_ant_conf,
1094102885a5SVasanthakumar Thiagarajan 				int main_rssi_avg, int alt_rssi_avg,
1095102885a5SVasanthakumar Thiagarajan 				int alt_ratio)
1096102885a5SVasanthakumar Thiagarajan {
1097102885a5SVasanthakumar Thiagarajan 	/* alt_good */
1098102885a5SVasanthakumar Thiagarajan 	switch (antcomb->quick_scan_cnt) {
1099102885a5SVasanthakumar Thiagarajan 	case 0:
1100102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1101102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1102102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1103102885a5SVasanthakumar Thiagarajan 		break;
1104102885a5SVasanthakumar Thiagarajan 	case 1:
1105102885a5SVasanthakumar Thiagarajan 		/* set alt to main, and alt to first conf */
1106102885a5SVasanthakumar Thiagarajan 		div_ant_conf->main_lna_conf = antcomb->main_conf;
1107102885a5SVasanthakumar Thiagarajan 		div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1108102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1109102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_second = alt_rssi_avg;
1110102885a5SVasanthakumar Thiagarajan 
1111102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1112102885a5SVasanthakumar Thiagarajan 			/* main is LNA1 */
1113102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1114102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1115102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1116102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1117102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1118102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1119102885a5SVasanthakumar Thiagarajan 			else
1120102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1121102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1122102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1123102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1124102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1125102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1126102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1127102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1128102885a5SVasanthakumar Thiagarajan 			else
1129102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1130102885a5SVasanthakumar Thiagarajan 		} else {
1131102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1132102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1133102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1134102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1135102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1136102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = true;
1137102885a5SVasanthakumar Thiagarajan 			else
1138102885a5SVasanthakumar Thiagarajan 				antcomb->first_ratio = false;
1139102885a5SVasanthakumar Thiagarajan 		}
1140102885a5SVasanthakumar Thiagarajan 		break;
1141102885a5SVasanthakumar Thiagarajan 	case 2:
1142102885a5SVasanthakumar Thiagarajan 		antcomb->alt_good = false;
1143102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = false;
1144102885a5SVasanthakumar Thiagarajan 		antcomb->scan = false;
1145102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_first = main_rssi_avg;
1146102885a5SVasanthakumar Thiagarajan 		antcomb->rssi_third = alt_rssi_avg;
1147102885a5SVasanthakumar Thiagarajan 
1148102885a5SVasanthakumar Thiagarajan 		if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1149102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1150102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1151102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA2)
1152102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1153102885a5SVasanthakumar Thiagarajan 		else if (antcomb->second_quick_scan_conf ==
1154102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1155102885a5SVasanthakumar Thiagarajan 			if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1156102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna2 = main_rssi_avg;
1157102885a5SVasanthakumar Thiagarajan 			else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1158102885a5SVasanthakumar Thiagarajan 				antcomb->rssi_lna1 = main_rssi_avg;
1159102885a5SVasanthakumar Thiagarajan 		}
1160102885a5SVasanthakumar Thiagarajan 
1161102885a5SVasanthakumar Thiagarajan 		if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1162102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1163102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1164102885a5SVasanthakumar Thiagarajan 		else
1165102885a5SVasanthakumar Thiagarajan 			div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1166102885a5SVasanthakumar Thiagarajan 
1167102885a5SVasanthakumar Thiagarajan 		if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1168102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1169102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1170102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1171102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1172102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1173102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1174102885a5SVasanthakumar Thiagarajan 			else
1175102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1176102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1177102885a5SVasanthakumar Thiagarajan 			if (ath_is_alt_ant_ratio_better(alt_ratio,
1178102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1179102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1180102885a5SVasanthakumar Thiagarajan 						main_rssi_avg, alt_rssi_avg,
1181102885a5SVasanthakumar Thiagarajan 						antcomb->total_pkt_count))
1182102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1183102885a5SVasanthakumar Thiagarajan 			else
1184102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1185102885a5SVasanthakumar Thiagarajan 		} else {
1186102885a5SVasanthakumar Thiagarajan 			if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1187102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg +
1188102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1189102885a5SVasanthakumar Thiagarajan 			    (alt_rssi_avg > main_rssi_avg)) &&
1190102885a5SVasanthakumar Thiagarajan 			    (antcomb->total_pkt_count > 50))
1191102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = true;
1192102885a5SVasanthakumar Thiagarajan 			else
1193102885a5SVasanthakumar Thiagarajan 				antcomb->second_ratio = false;
1194102885a5SVasanthakumar Thiagarajan 		}
1195102885a5SVasanthakumar Thiagarajan 
1196102885a5SVasanthakumar Thiagarajan 		/* set alt to the conf with maximun ratio */
1197102885a5SVasanthakumar Thiagarajan 		if (antcomb->first_ratio && antcomb->second_ratio) {
1198102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_second > antcomb->rssi_third) {
1199102885a5SVasanthakumar Thiagarajan 				/* first alt*/
1200102885a5SVasanthakumar Thiagarajan 				if ((antcomb->first_quick_scan_conf ==
1201102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA1) ||
1202102885a5SVasanthakumar Thiagarajan 				    (antcomb->first_quick_scan_conf ==
1203102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2))
1204102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2*/
1205102885a5SVasanthakumar Thiagarajan 					if (div_ant_conf->main_lna_conf ==
1206102885a5SVasanthakumar Thiagarajan 					    ATH_ANT_DIV_COMB_LNA2)
1207102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1208102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1209102885a5SVasanthakumar Thiagarajan 					else
1210102885a5SVasanthakumar Thiagarajan 						div_ant_conf->alt_lna_conf =
1211102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1212102885a5SVasanthakumar Thiagarajan 				else
1213102885a5SVasanthakumar Thiagarajan 					/* Set alt to A+B or A-B */
1214102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1215102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1216102885a5SVasanthakumar Thiagarajan 			} else if ((antcomb->second_quick_scan_conf ==
1217102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA1) ||
1218102885a5SVasanthakumar Thiagarajan 				   (antcomb->second_quick_scan_conf ==
1219102885a5SVasanthakumar Thiagarajan 				   ATH_ANT_DIV_COMB_LNA2)) {
1220102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1221102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1222102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1223102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1224102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1225102885a5SVasanthakumar Thiagarajan 				else
1226102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1227102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1228102885a5SVasanthakumar Thiagarajan 			} else {
1229102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1230102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1231102885a5SVasanthakumar Thiagarajan 					antcomb->second_quick_scan_conf;
1232102885a5SVasanthakumar Thiagarajan 			}
1233102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->first_ratio) {
1234102885a5SVasanthakumar Thiagarajan 			/* first alt */
1235102885a5SVasanthakumar Thiagarajan 			if ((antcomb->first_quick_scan_conf ==
1236102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1237102885a5SVasanthakumar Thiagarajan 			    (antcomb->first_quick_scan_conf ==
1238102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1239102885a5SVasanthakumar Thiagarajan 					/* Set alt LNA1 or LNA2 */
1240102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1241102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1242102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1243102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1244102885a5SVasanthakumar Thiagarajan 				else
1245102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1246102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1247102885a5SVasanthakumar Thiagarajan 			else
1248102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1249102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1250102885a5SVasanthakumar Thiagarajan 						antcomb->first_quick_scan_conf;
1251102885a5SVasanthakumar Thiagarajan 		} else if (antcomb->second_ratio) {
1252102885a5SVasanthakumar Thiagarajan 				/* second alt */
1253102885a5SVasanthakumar Thiagarajan 			if ((antcomb->second_quick_scan_conf ==
1254102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1) ||
1255102885a5SVasanthakumar Thiagarajan 			    (antcomb->second_quick_scan_conf ==
1256102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA2))
1257102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1258102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1259102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1260102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1261102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1262102885a5SVasanthakumar Thiagarajan 				else
1263102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1264102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1265102885a5SVasanthakumar Thiagarajan 			else
1266102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1267102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf =
1268102885a5SVasanthakumar Thiagarajan 						antcomb->second_quick_scan_conf;
1269102885a5SVasanthakumar Thiagarajan 		} else {
1270102885a5SVasanthakumar Thiagarajan 			/* main is largest */
1271102885a5SVasanthakumar Thiagarajan 			if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1272102885a5SVasanthakumar Thiagarajan 			    (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1273102885a5SVasanthakumar Thiagarajan 				/* Set alt LNA1 or LNA2 */
1274102885a5SVasanthakumar Thiagarajan 				if (div_ant_conf->main_lna_conf ==
1275102885a5SVasanthakumar Thiagarajan 				    ATH_ANT_DIV_COMB_LNA2)
1276102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1277102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA1;
1278102885a5SVasanthakumar Thiagarajan 				else
1279102885a5SVasanthakumar Thiagarajan 					div_ant_conf->alt_lna_conf =
1280102885a5SVasanthakumar Thiagarajan 							ATH_ANT_DIV_COMB_LNA2;
1281102885a5SVasanthakumar Thiagarajan 			else
1282102885a5SVasanthakumar Thiagarajan 				/* Set alt to A+B or A-B */
1283102885a5SVasanthakumar Thiagarajan 				div_ant_conf->alt_lna_conf = antcomb->main_conf;
1284102885a5SVasanthakumar Thiagarajan 		}
1285102885a5SVasanthakumar Thiagarajan 		break;
1286102885a5SVasanthakumar Thiagarajan 	default:
1287102885a5SVasanthakumar Thiagarajan 		break;
1288102885a5SVasanthakumar Thiagarajan 	}
1289102885a5SVasanthakumar Thiagarajan }
1290102885a5SVasanthakumar Thiagarajan 
12919bad82b8SJohn W. Linville static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf)
1292102885a5SVasanthakumar Thiagarajan {
1293102885a5SVasanthakumar Thiagarajan 	/* Adjust the fast_div_bias based on main and alt lna conf */
1294102885a5SVasanthakumar Thiagarajan 	switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
1295102885a5SVasanthakumar Thiagarajan 	case (0x01): /* A-B LNA2 */
1296102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1297102885a5SVasanthakumar Thiagarajan 		break;
1298102885a5SVasanthakumar Thiagarajan 	case (0x02): /* A-B LNA1 */
1299102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1300102885a5SVasanthakumar Thiagarajan 		break;
1301102885a5SVasanthakumar Thiagarajan 	case (0x03): /* A-B A+B */
1302102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1303102885a5SVasanthakumar Thiagarajan 		break;
1304102885a5SVasanthakumar Thiagarajan 	case (0x10): /* LNA2 A-B */
1305102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1306102885a5SVasanthakumar Thiagarajan 		break;
1307102885a5SVasanthakumar Thiagarajan 	case (0x12): /* LNA2 LNA1 */
1308102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x2;
1309102885a5SVasanthakumar Thiagarajan 		break;
1310102885a5SVasanthakumar Thiagarajan 	case (0x13): /* LNA2 A+B */
1311102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x7;
1312102885a5SVasanthakumar Thiagarajan 		break;
1313102885a5SVasanthakumar Thiagarajan 	case (0x20): /* LNA1 A-B */
1314102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1315102885a5SVasanthakumar Thiagarajan 		break;
1316102885a5SVasanthakumar Thiagarajan 	case (0x21): /* LNA1 LNA2 */
1317102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x0;
1318102885a5SVasanthakumar Thiagarajan 		break;
1319102885a5SVasanthakumar Thiagarajan 	case (0x23): /* LNA1 A+B */
1320102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x6;
1321102885a5SVasanthakumar Thiagarajan 		break;
1322102885a5SVasanthakumar Thiagarajan 	case (0x30): /* A+B A-B */
1323102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x1;
1324102885a5SVasanthakumar Thiagarajan 		break;
1325102885a5SVasanthakumar Thiagarajan 	case (0x31): /* A+B LNA2 */
1326102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3b;
1327102885a5SVasanthakumar Thiagarajan 		break;
1328102885a5SVasanthakumar Thiagarajan 	case (0x32): /* A+B LNA1 */
1329102885a5SVasanthakumar Thiagarajan 		ant_conf->fast_div_bias = 0x3d;
1330102885a5SVasanthakumar Thiagarajan 		break;
1331102885a5SVasanthakumar Thiagarajan 	default:
1332102885a5SVasanthakumar Thiagarajan 		break;
1333102885a5SVasanthakumar Thiagarajan 	}
1334102885a5SVasanthakumar Thiagarajan }
1335102885a5SVasanthakumar Thiagarajan 
1336102885a5SVasanthakumar Thiagarajan /* Antenna diversity and combining */
1337102885a5SVasanthakumar Thiagarajan static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1338102885a5SVasanthakumar Thiagarajan {
1339102885a5SVasanthakumar Thiagarajan 	struct ath_hw_antcomb_conf div_ant_conf;
1340102885a5SVasanthakumar Thiagarajan 	struct ath_ant_comb *antcomb = &sc->ant_comb;
1341102885a5SVasanthakumar Thiagarajan 	int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
13420ff2b5c0SSujith Manoharan 	int curr_main_set;
1343102885a5SVasanthakumar Thiagarajan 	int main_rssi = rs->rs_rssi_ctl0;
1344102885a5SVasanthakumar Thiagarajan 	int alt_rssi = rs->rs_rssi_ctl1;
1345102885a5SVasanthakumar Thiagarajan 	int rx_ant_conf,  main_ant_conf;
1346102885a5SVasanthakumar Thiagarajan 	bool short_scan = false;
1347102885a5SVasanthakumar Thiagarajan 
1348102885a5SVasanthakumar Thiagarajan 	rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1349102885a5SVasanthakumar Thiagarajan 		       ATH_ANT_RX_MASK;
1350102885a5SVasanthakumar Thiagarajan 	main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1351102885a5SVasanthakumar Thiagarajan 			 ATH_ANT_RX_MASK;
1352102885a5SVasanthakumar Thiagarajan 
1353102885a5SVasanthakumar Thiagarajan 	/* Record packet only when alt_rssi is positive */
1354102885a5SVasanthakumar Thiagarajan 	if (alt_rssi > 0) {
1355102885a5SVasanthakumar Thiagarajan 		antcomb->total_pkt_count++;
1356102885a5SVasanthakumar Thiagarajan 		antcomb->main_total_rssi += main_rssi;
1357102885a5SVasanthakumar Thiagarajan 		antcomb->alt_total_rssi  += alt_rssi;
1358102885a5SVasanthakumar Thiagarajan 		if (main_ant_conf == rx_ant_conf)
1359102885a5SVasanthakumar Thiagarajan 			antcomb->main_recv_cnt++;
1360102885a5SVasanthakumar Thiagarajan 		else
1361102885a5SVasanthakumar Thiagarajan 			antcomb->alt_recv_cnt++;
1362102885a5SVasanthakumar Thiagarajan 	}
1363102885a5SVasanthakumar Thiagarajan 
1364102885a5SVasanthakumar Thiagarajan 	/* Short scan check */
1365102885a5SVasanthakumar Thiagarajan 	if (antcomb->scan && antcomb->alt_good) {
1366102885a5SVasanthakumar Thiagarajan 		if (time_after(jiffies, antcomb->scan_start_time +
1367102885a5SVasanthakumar Thiagarajan 		    msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1368102885a5SVasanthakumar Thiagarajan 			short_scan = true;
1369102885a5SVasanthakumar Thiagarajan 		else
1370102885a5SVasanthakumar Thiagarajan 			if (antcomb->total_pkt_count ==
1371102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1372102885a5SVasanthakumar Thiagarajan 				alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1373102885a5SVasanthakumar Thiagarajan 					    antcomb->total_pkt_count);
1374102885a5SVasanthakumar Thiagarajan 				if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1375102885a5SVasanthakumar Thiagarajan 					short_scan = true;
1376102885a5SVasanthakumar Thiagarajan 			}
1377102885a5SVasanthakumar Thiagarajan 	}
1378102885a5SVasanthakumar Thiagarajan 
1379102885a5SVasanthakumar Thiagarajan 	if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1380102885a5SVasanthakumar Thiagarajan 	    rs->rs_moreaggr) && !short_scan)
1381102885a5SVasanthakumar Thiagarajan 		return;
1382102885a5SVasanthakumar Thiagarajan 
1383102885a5SVasanthakumar Thiagarajan 	if (antcomb->total_pkt_count) {
1384102885a5SVasanthakumar Thiagarajan 		alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1385102885a5SVasanthakumar Thiagarajan 			     antcomb->total_pkt_count);
1386102885a5SVasanthakumar Thiagarajan 		main_rssi_avg = (antcomb->main_total_rssi /
1387102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1388102885a5SVasanthakumar Thiagarajan 		alt_rssi_avg = (antcomb->alt_total_rssi /
1389102885a5SVasanthakumar Thiagarajan 				 antcomb->total_pkt_count);
1390102885a5SVasanthakumar Thiagarajan 	}
1391102885a5SVasanthakumar Thiagarajan 
1392102885a5SVasanthakumar Thiagarajan 
1393102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1394102885a5SVasanthakumar Thiagarajan 	curr_alt_set = div_ant_conf.alt_lna_conf;
1395102885a5SVasanthakumar Thiagarajan 	curr_main_set = div_ant_conf.main_lna_conf;
1396102885a5SVasanthakumar Thiagarajan 
1397102885a5SVasanthakumar Thiagarajan 	antcomb->count++;
1398102885a5SVasanthakumar Thiagarajan 
1399102885a5SVasanthakumar Thiagarajan 	if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1400102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1401102885a5SVasanthakumar Thiagarajan 			ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1402102885a5SVasanthakumar Thiagarajan 						  main_rssi_avg);
1403102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = true;
1404102885a5SVasanthakumar Thiagarajan 		} else {
1405102885a5SVasanthakumar Thiagarajan 			antcomb->alt_good = false;
1406102885a5SVasanthakumar Thiagarajan 		}
1407102885a5SVasanthakumar Thiagarajan 
1408102885a5SVasanthakumar Thiagarajan 		antcomb->count = 0;
1409102885a5SVasanthakumar Thiagarajan 		antcomb->scan = true;
1410102885a5SVasanthakumar Thiagarajan 		antcomb->scan_not_start = true;
1411102885a5SVasanthakumar Thiagarajan 	}
1412102885a5SVasanthakumar Thiagarajan 
1413102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan) {
1414102885a5SVasanthakumar Thiagarajan 		if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1415102885a5SVasanthakumar Thiagarajan 			if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1416102885a5SVasanthakumar Thiagarajan 				/* Switch main and alt LNA */
1417102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1418102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1419102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1420102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1421102885a5SVasanthakumar Thiagarajan 			} else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1422102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1423102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1424102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf  =
1425102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1426102885a5SVasanthakumar Thiagarajan 			}
1427102885a5SVasanthakumar Thiagarajan 
1428102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1429102885a5SVasanthakumar Thiagarajan 		} else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1430102885a5SVasanthakumar Thiagarajan 			   (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1431102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1432102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1433102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1434102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1435102885a5SVasanthakumar Thiagarajan 			else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1436102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1437102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1438102885a5SVasanthakumar Thiagarajan 
1439102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1440102885a5SVasanthakumar Thiagarajan 		}
1441102885a5SVasanthakumar Thiagarajan 
1442102885a5SVasanthakumar Thiagarajan 		if ((alt_rssi_avg < (main_rssi_avg +
1443102885a5SVasanthakumar Thiagarajan 		    ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
1444102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1445102885a5SVasanthakumar Thiagarajan 	}
1446102885a5SVasanthakumar Thiagarajan 
1447102885a5SVasanthakumar Thiagarajan 	if (!antcomb->scan_not_start) {
1448102885a5SVasanthakumar Thiagarajan 		switch (curr_alt_set) {
1449102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA2:
1450102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = alt_rssi_avg;
1451102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = main_rssi_avg;
1452102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1453102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1454102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf =
1455102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1;
1456102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1457102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1458102885a5SVasanthakumar Thiagarajan 			break;
1459102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1:
1460102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna1 = alt_rssi_avg;
1461102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_lna2 = main_rssi_avg;
1462102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1463102885a5SVasanthakumar Thiagarajan 			/* set to A+B */
1464102885a5SVasanthakumar Thiagarajan 			div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1465102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf  =
1466102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1467102885a5SVasanthakumar Thiagarajan 			break;
1468102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1469102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_add = alt_rssi_avg;
1470102885a5SVasanthakumar Thiagarajan 			antcomb->scan = true;
1471102885a5SVasanthakumar Thiagarajan 			/* set to A-B */
1472102885a5SVasanthakumar Thiagarajan 			div_ant_conf.alt_lna_conf =
1473102885a5SVasanthakumar Thiagarajan 				ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1474102885a5SVasanthakumar Thiagarajan 			break;
1475102885a5SVasanthakumar Thiagarajan 		case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1476102885a5SVasanthakumar Thiagarajan 			antcomb->rssi_sub = alt_rssi_avg;
1477102885a5SVasanthakumar Thiagarajan 			antcomb->scan = false;
1478102885a5SVasanthakumar Thiagarajan 			if (antcomb->rssi_lna2 >
1479102885a5SVasanthakumar Thiagarajan 			    (antcomb->rssi_lna1 +
1480102885a5SVasanthakumar Thiagarajan 			    ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1481102885a5SVasanthakumar Thiagarajan 				/* use LNA2 as main LNA */
1482102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1483102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1484102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1485102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1486102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1487102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1488102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1489102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1490102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1491102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1492102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1493102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1494102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1495102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1496102885a5SVasanthakumar Thiagarajan 				} else {
1497102885a5SVasanthakumar Thiagarajan 					/* set to LNA1 */
1498102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1499102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1500102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1501102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1502102885a5SVasanthakumar Thiagarajan 				}
1503102885a5SVasanthakumar Thiagarajan 			} else {
1504102885a5SVasanthakumar Thiagarajan 				/* use LNA1 as main LNA */
1505102885a5SVasanthakumar Thiagarajan 				if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1506102885a5SVasanthakumar Thiagarajan 				    (antcomb->rssi_add > antcomb->rssi_sub)) {
1507102885a5SVasanthakumar Thiagarajan 					/* set to A+B */
1508102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1509102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1510102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf  =
1511102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1512102885a5SVasanthakumar Thiagarajan 				} else if (antcomb->rssi_sub >
1513102885a5SVasanthakumar Thiagarajan 					   antcomb->rssi_lna1) {
1514102885a5SVasanthakumar Thiagarajan 					/* set to A-B */
1515102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1516102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1517102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1518102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1519102885a5SVasanthakumar Thiagarajan 				} else {
1520102885a5SVasanthakumar Thiagarajan 					/* set to LNA2 */
1521102885a5SVasanthakumar Thiagarajan 					div_ant_conf.main_lna_conf =
1522102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1523102885a5SVasanthakumar Thiagarajan 					div_ant_conf.alt_lna_conf =
1524102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1525102885a5SVasanthakumar Thiagarajan 				}
1526102885a5SVasanthakumar Thiagarajan 			}
1527102885a5SVasanthakumar Thiagarajan 			break;
1528102885a5SVasanthakumar Thiagarajan 		default:
1529102885a5SVasanthakumar Thiagarajan 			break;
1530102885a5SVasanthakumar Thiagarajan 		}
1531102885a5SVasanthakumar Thiagarajan 	} else {
1532102885a5SVasanthakumar Thiagarajan 		if (!antcomb->alt_good) {
1533102885a5SVasanthakumar Thiagarajan 			antcomb->scan_not_start = false;
1534102885a5SVasanthakumar Thiagarajan 			/* Set alt to another LNA */
1535102885a5SVasanthakumar Thiagarajan 			if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
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;
1540102885a5SVasanthakumar Thiagarajan 			} else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1541102885a5SVasanthakumar Thiagarajan 				div_ant_conf.main_lna_conf =
1542102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA1;
1543102885a5SVasanthakumar Thiagarajan 				div_ant_conf.alt_lna_conf =
1544102885a5SVasanthakumar Thiagarajan 						ATH_ANT_DIV_COMB_LNA2;
1545102885a5SVasanthakumar Thiagarajan 			}
1546102885a5SVasanthakumar Thiagarajan 			goto div_comb_done;
1547102885a5SVasanthakumar Thiagarajan 		}
1548102885a5SVasanthakumar Thiagarajan 	}
1549102885a5SVasanthakumar Thiagarajan 
1550102885a5SVasanthakumar Thiagarajan 	ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1551102885a5SVasanthakumar Thiagarajan 					   main_rssi_avg, alt_rssi_avg,
1552102885a5SVasanthakumar Thiagarajan 					   alt_ratio);
1553102885a5SVasanthakumar Thiagarajan 
1554102885a5SVasanthakumar Thiagarajan 	antcomb->quick_scan_cnt++;
1555102885a5SVasanthakumar Thiagarajan 
1556102885a5SVasanthakumar Thiagarajan div_comb_done:
1557102885a5SVasanthakumar Thiagarajan 	ath_ant_div_conf_fast_divbias(&div_ant_conf);
1558102885a5SVasanthakumar Thiagarajan 
1559102885a5SVasanthakumar Thiagarajan 	ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1560102885a5SVasanthakumar Thiagarajan 
1561102885a5SVasanthakumar Thiagarajan 	antcomb->scan_start_time = jiffies;
1562102885a5SVasanthakumar Thiagarajan 	antcomb->total_pkt_count = 0;
1563102885a5SVasanthakumar Thiagarajan 	antcomb->main_total_rssi = 0;
1564102885a5SVasanthakumar Thiagarajan 	antcomb->alt_total_rssi = 0;
1565102885a5SVasanthakumar Thiagarajan 	antcomb->main_recv_cnt = 0;
1566102885a5SVasanthakumar Thiagarajan 	antcomb->alt_recv_cnt = 0;
1567102885a5SVasanthakumar Thiagarajan }
1568102885a5SVasanthakumar Thiagarajan 
1569b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1570b5c80475SFelix Fietkau {
1571b5c80475SFelix Fietkau 	struct ath_buf *bf;
15720d95521eSFelix Fietkau 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1573b5c80475SFelix Fietkau 	struct ieee80211_rx_status *rxs;
1574b5c80475SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
1575b5c80475SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
1576b5c80475SFelix Fietkau 	/*
1577cae6b74dSMohammed Shafi Shajakhan 	 * The hw can technically differ from common->hw when using ath9k
1578b5c80475SFelix Fietkau 	 * virtual wiphy so to account for that we iterate over the active
1579b5c80475SFelix Fietkau 	 * wiphys and find the appropriate wiphy and therefore hw.
1580b5c80475SFelix Fietkau 	 */
15817545daf4SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
1582b5c80475SFelix Fietkau 	struct ieee80211_hdr *hdr;
1583b5c80475SFelix Fietkau 	int retval;
1584b5c80475SFelix Fietkau 	bool decrypt_error = false;
1585b5c80475SFelix Fietkau 	struct ath_rx_status rs;
1586b5c80475SFelix Fietkau 	enum ath9k_rx_qtype qtype;
1587b5c80475SFelix Fietkau 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1588b5c80475SFelix Fietkau 	int dma_type;
15895c6dd921SVasanthakumar Thiagarajan 	u8 rx_status_len = ah->caps.rx_status_len;
1590a6d2055bSFelix Fietkau 	u64 tsf = 0;
1591a6d2055bSFelix Fietkau 	u32 tsf_lower = 0;
15928ab2cd09SLuis R. Rodriguez 	unsigned long flags;
1593b5c80475SFelix Fietkau 
1594b5c80475SFelix Fietkau 	if (edma)
1595b5c80475SFelix Fietkau 		dma_type = DMA_BIDIRECTIONAL;
159656824223SMing Lei 	else
159756824223SMing Lei 		dma_type = DMA_FROM_DEVICE;
1598b5c80475SFelix Fietkau 
1599b5c80475SFelix Fietkau 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1600b5c80475SFelix Fietkau 	spin_lock_bh(&sc->rx.rxbuflock);
1601b5c80475SFelix Fietkau 
1602a6d2055bSFelix Fietkau 	tsf = ath9k_hw_gettsf64(ah);
1603a6d2055bSFelix Fietkau 	tsf_lower = tsf & 0xffffffff;
1604a6d2055bSFelix Fietkau 
1605b5c80475SFelix Fietkau 	do {
1606b5c80475SFelix Fietkau 		/* If handling rx interrupt and flush is in progress => exit */
1607b5c80475SFelix Fietkau 		if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1608b5c80475SFelix Fietkau 			break;
1609b5c80475SFelix Fietkau 
1610b5c80475SFelix Fietkau 		memset(&rs, 0, sizeof(rs));
1611b5c80475SFelix Fietkau 		if (edma)
1612b5c80475SFelix Fietkau 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1613b5c80475SFelix Fietkau 		else
1614b5c80475SFelix Fietkau 			bf = ath_get_next_rx_buf(sc, &rs);
1615b5c80475SFelix Fietkau 
1616b5c80475SFelix Fietkau 		if (!bf)
1617b5c80475SFelix Fietkau 			break;
1618b5c80475SFelix Fietkau 
1619b5c80475SFelix Fietkau 		skb = bf->bf_mpdu;
1620b5c80475SFelix Fietkau 		if (!skb)
1621b5c80475SFelix Fietkau 			continue;
1622b5c80475SFelix Fietkau 
16230d95521eSFelix Fietkau 		/*
16240d95521eSFelix Fietkau 		 * Take frame header from the first fragment and RX status from
16250d95521eSFelix Fietkau 		 * the last one.
16260d95521eSFelix Fietkau 		 */
16270d95521eSFelix Fietkau 		if (sc->rx.frag)
16280d95521eSFelix Fietkau 			hdr_skb = sc->rx.frag;
16290d95521eSFelix Fietkau 		else
16300d95521eSFelix Fietkau 			hdr_skb = skb;
16310d95521eSFelix Fietkau 
16320d95521eSFelix Fietkau 		hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
16330d95521eSFelix Fietkau 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
16345ca42627SLuis R. Rodriguez 
163529bffa96SFelix Fietkau 		ath_debug_stat_rx(sc, &rs);
16361395d3f0SSujith 
1637203c4805SLuis R. Rodriguez 		/*
1638203c4805SLuis R. Rodriguez 		 * If we're asked to flush receive queue, directly
1639203c4805SLuis R. Rodriguez 		 * chain it back at the queue without processing it.
1640203c4805SLuis R. Rodriguez 		 */
1641203c4805SLuis R. Rodriguez 		if (flush)
16420d95521eSFelix Fietkau 			goto requeue_drop_frag;
1643203c4805SLuis R. Rodriguez 
1644c8f3b721SJan Friedrich 		retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1645c8f3b721SJan Friedrich 						 rxs, &decrypt_error);
1646c8f3b721SJan Friedrich 		if (retval)
16470d95521eSFelix Fietkau 			goto requeue_drop_frag;
1648c8f3b721SJan Friedrich 
1649a6d2055bSFelix Fietkau 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1650a6d2055bSFelix Fietkau 		if (rs.rs_tstamp > tsf_lower &&
1651a6d2055bSFelix Fietkau 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1652a6d2055bSFelix Fietkau 			rxs->mactime -= 0x100000000ULL;
1653a6d2055bSFelix Fietkau 
1654a6d2055bSFelix Fietkau 		if (rs.rs_tstamp < tsf_lower &&
1655a6d2055bSFelix Fietkau 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1656a6d2055bSFelix Fietkau 			rxs->mactime += 0x100000000ULL;
1657a6d2055bSFelix Fietkau 
1658203c4805SLuis R. Rodriguez 		/* Ensure we always have an skb to requeue once we are done
1659203c4805SLuis R. Rodriguez 		 * processing the current buffer's skb */
1660cc861f74SLuis R. Rodriguez 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1661203c4805SLuis R. Rodriguez 
1662203c4805SLuis R. Rodriguez 		/* If there is no memory we ignore the current RX'd frame,
1663203c4805SLuis R. Rodriguez 		 * tell hardware it can give us a new frame using the old
1664203c4805SLuis R. Rodriguez 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1665203c4805SLuis R. Rodriguez 		 * processing. */
1666203c4805SLuis R. Rodriguez 		if (!requeue_skb)
16670d95521eSFelix Fietkau 			goto requeue_drop_frag;
1668203c4805SLuis R. Rodriguez 
1669203c4805SLuis R. Rodriguez 		/* Unmap the frame */
1670203c4805SLuis R. Rodriguez 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1671cc861f74SLuis R. Rodriguez 				 common->rx_bufsize,
1672b5c80475SFelix Fietkau 				 dma_type);
1673203c4805SLuis R. Rodriguez 
1674b5c80475SFelix Fietkau 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1675b5c80475SFelix Fietkau 		if (ah->caps.rx_status_len)
1676b5c80475SFelix Fietkau 			skb_pull(skb, ah->caps.rx_status_len);
1677203c4805SLuis R. Rodriguez 
16780d95521eSFelix Fietkau 		if (!rs.rs_more)
16790d95521eSFelix Fietkau 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1680c9b14170SLuis R. Rodriguez 						 rxs, decrypt_error);
1681203c4805SLuis R. Rodriguez 
1682203c4805SLuis R. Rodriguez 		/* We will now give hardware our shiny new allocated skb */
1683203c4805SLuis R. Rodriguez 		bf->bf_mpdu = requeue_skb;
1684203c4805SLuis R. Rodriguez 		bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1685cc861f74SLuis R. Rodriguez 						 common->rx_bufsize,
1686b5c80475SFelix Fietkau 						 dma_type);
1687203c4805SLuis R. Rodriguez 		if (unlikely(dma_mapping_error(sc->dev,
1688203c4805SLuis R. Rodriguez 			  bf->bf_buf_addr))) {
1689203c4805SLuis R. Rodriguez 			dev_kfree_skb_any(requeue_skb);
1690203c4805SLuis R. Rodriguez 			bf->bf_mpdu = NULL;
16916cf9e995SBen Greear 			bf->bf_buf_addr = 0;
16923800276aSJoe Perches 			ath_err(common, "dma_mapping_error() on RX\n");
16937545daf4SFelix Fietkau 			ieee80211_rx(hw, skb);
1694203c4805SLuis R. Rodriguez 			break;
1695203c4805SLuis R. Rodriguez 		}
1696203c4805SLuis R. Rodriguez 
16970d95521eSFelix Fietkau 		if (rs.rs_more) {
16980d95521eSFelix Fietkau 			/*
16990d95521eSFelix Fietkau 			 * rs_more indicates chained descriptors which can be
17000d95521eSFelix Fietkau 			 * used to link buffers together for a sort of
17010d95521eSFelix Fietkau 			 * scatter-gather operation.
17020d95521eSFelix Fietkau 			 */
17030d95521eSFelix Fietkau 			if (sc->rx.frag) {
17040d95521eSFelix Fietkau 				/* too many fragments - cannot handle frame */
17050d95521eSFelix Fietkau 				dev_kfree_skb_any(sc->rx.frag);
17060d95521eSFelix Fietkau 				dev_kfree_skb_any(skb);
17070d95521eSFelix Fietkau 				skb = NULL;
17080d95521eSFelix Fietkau 			}
17090d95521eSFelix Fietkau 			sc->rx.frag = skb;
17100d95521eSFelix Fietkau 			goto requeue;
17110d95521eSFelix Fietkau 		}
17120d95521eSFelix Fietkau 
17130d95521eSFelix Fietkau 		if (sc->rx.frag) {
17140d95521eSFelix Fietkau 			int space = skb->len - skb_tailroom(hdr_skb);
17150d95521eSFelix Fietkau 
17160d95521eSFelix Fietkau 			sc->rx.frag = NULL;
17170d95521eSFelix Fietkau 
17180d95521eSFelix Fietkau 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
17190d95521eSFelix Fietkau 				dev_kfree_skb(skb);
17200d95521eSFelix Fietkau 				goto requeue_drop_frag;
17210d95521eSFelix Fietkau 			}
17220d95521eSFelix Fietkau 
17230d95521eSFelix Fietkau 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
17240d95521eSFelix Fietkau 						  skb->len);
17250d95521eSFelix Fietkau 			dev_kfree_skb_any(skb);
17260d95521eSFelix Fietkau 			skb = hdr_skb;
17270d95521eSFelix Fietkau 		}
17280d95521eSFelix Fietkau 
1729203c4805SLuis R. Rodriguez 		/*
1730203c4805SLuis R. Rodriguez 		 * change the default rx antenna if rx diversity chooses the
1731203c4805SLuis R. Rodriguez 		 * other antenna 3 times in a row.
1732203c4805SLuis R. Rodriguez 		 */
173329bffa96SFelix Fietkau 		if (sc->rx.defant != rs.rs_antenna) {
1734203c4805SLuis R. Rodriguez 			if (++sc->rx.rxotherant >= 3)
173529bffa96SFelix Fietkau 				ath_setdefantenna(sc, rs.rs_antenna);
1736203c4805SLuis R. Rodriguez 		} else {
1737203c4805SLuis R. Rodriguez 			sc->rx.rxotherant = 0;
1738203c4805SLuis R. Rodriguez 		}
1739203c4805SLuis R. Rodriguez 
17408ab2cd09SLuis R. Rodriguez 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1741aaef24b4SMohammed Shafi Shajakhan 
1742aaef24b4SMohammed Shafi Shajakhan 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
17431b04b930SSujith 					      PS_WAIT_FOR_CAB |
1744aaef24b4SMohammed Shafi Shajakhan 					      PS_WAIT_FOR_PSPOLL_DATA)) ||
1745*cedc7e3dSMohammed Shafi Shajakhan 						ath9k_check_auto_sleep(sc))
1746cc65965cSJouni Malinen 			ath_rx_ps(sc, skb);
17478ab2cd09SLuis R. Rodriguez 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1748cc65965cSJouni Malinen 
1749102885a5SVasanthakumar Thiagarajan 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1750102885a5SVasanthakumar Thiagarajan 			ath_ant_comb_scan(sc, &rs);
1751102885a5SVasanthakumar Thiagarajan 
17527545daf4SFelix Fietkau 		ieee80211_rx(hw, skb);
1753cc65965cSJouni Malinen 
17540d95521eSFelix Fietkau requeue_drop_frag:
17550d95521eSFelix Fietkau 		if (sc->rx.frag) {
17560d95521eSFelix Fietkau 			dev_kfree_skb_any(sc->rx.frag);
17570d95521eSFelix Fietkau 			sc->rx.frag = NULL;
17580d95521eSFelix Fietkau 		}
1759203c4805SLuis R. Rodriguez requeue:
1760b5c80475SFelix Fietkau 		if (edma) {
1761b5c80475SFelix Fietkau 			list_add_tail(&bf->list, &sc->rx.rxbuf);
1762b5c80475SFelix Fietkau 			ath_rx_edma_buf_link(sc, qtype);
1763b5c80475SFelix Fietkau 		} else {
1764203c4805SLuis R. Rodriguez 			list_move_tail(&bf->list, &sc->rx.rxbuf);
1765203c4805SLuis R. Rodriguez 			ath_rx_buf_link(sc, bf);
176695294973SFelix Fietkau 			ath9k_hw_rxena(ah);
1767b5c80475SFelix Fietkau 		}
1768203c4805SLuis R. Rodriguez 	} while (1);
1769203c4805SLuis R. Rodriguez 
1770203c4805SLuis R. Rodriguez 	spin_unlock_bh(&sc->rx.rxbuflock);
1771203c4805SLuis R. Rodriguez 
1772203c4805SLuis R. Rodriguez 	return 0;
1773203c4805SLuis R. Rodriguez }
1774