1203c4805SLuis R. Rodriguez /*
25b68138eSSujith Manoharan * Copyright (c) 2008-2011 Atheros Communications Inc.
3203c4805SLuis R. Rodriguez *
4203c4805SLuis R. Rodriguez * Permission to use, copy, modify, and/or distribute this software for any
5203c4805SLuis R. Rodriguez * purpose with or without fee is hereby granted, provided that the above
6203c4805SLuis R. Rodriguez * copyright notice and this permission notice appear in all copies.
7203c4805SLuis R. Rodriguez *
8203c4805SLuis R. Rodriguez * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9203c4805SLuis R. Rodriguez * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10203c4805SLuis R. Rodriguez * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11203c4805SLuis R. Rodriguez * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12203c4805SLuis R. Rodriguez * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13203c4805SLuis R. Rodriguez * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14203c4805SLuis R. Rodriguez * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15203c4805SLuis R. Rodriguez */
16203c4805SLuis R. Rodriguez
17b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h>
18203c4805SLuis R. Rodriguez #include "ath9k.h"
19b622a720SLuis R. Rodriguez #include "ar9003_mac.h"
20203c4805SLuis R. Rodriguez
211a04d59dSFelix Fietkau #define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb))
22b5c80475SFelix Fietkau
ath9k_check_auto_sleep(struct ath_softc * sc)23ededf1f8SVasanthakumar Thiagarajan static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
24ededf1f8SVasanthakumar Thiagarajan {
25ededf1f8SVasanthakumar Thiagarajan return sc->ps_enabled &&
26ededf1f8SVasanthakumar Thiagarajan (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
27ededf1f8SVasanthakumar Thiagarajan }
28ededf1f8SVasanthakumar Thiagarajan
29203c4805SLuis R. Rodriguez /*
30203c4805SLuis R. Rodriguez * Setup and link descriptors.
31203c4805SLuis R. Rodriguez *
32203c4805SLuis R. Rodriguez * 11N: we can no longer afford to self link the last descriptor.
33203c4805SLuis R. Rodriguez * MAC acknowledges BA status as long as it copies frames to host
34203c4805SLuis R. Rodriguez * buffer (or rx fifo). This can incorrectly acknowledge packets
35203c4805SLuis R. Rodriguez * to a sender if last desc is self-linked.
36203c4805SLuis R. Rodriguez */
ath_rx_buf_link(struct ath_softc * sc,struct ath_rxbuf * bf,bool flush)377dd74f5fSFelix Fietkau static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf,
387dd74f5fSFelix Fietkau bool flush)
39203c4805SLuis R. Rodriguez {
40203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah;
41cc861f74SLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
42203c4805SLuis R. Rodriguez struct ath_desc *ds;
43203c4805SLuis R. Rodriguez struct sk_buff *skb;
44203c4805SLuis R. Rodriguez
45203c4805SLuis R. Rodriguez ds = bf->bf_desc;
46203c4805SLuis R. Rodriguez ds->ds_link = 0; /* link to null */
47203c4805SLuis R. Rodriguez ds->ds_data = bf->bf_buf_addr;
48203c4805SLuis R. Rodriguez
49203c4805SLuis R. Rodriguez /* virtual addr of the beginning of the buffer. */
50203c4805SLuis R. Rodriguez skb = bf->bf_mpdu;
519680e8a3SLuis R. Rodriguez BUG_ON(skb == NULL);
52203c4805SLuis R. Rodriguez ds->ds_vdata = skb->data;
53203c4805SLuis R. Rodriguez
54cc861f74SLuis R. Rodriguez /*
55cc861f74SLuis R. Rodriguez * setup rx descriptors. The rx_bufsize here tells the hardware
56203c4805SLuis R. Rodriguez * how much data it can DMA to us and that we are prepared
57cc861f74SLuis R. Rodriguez * to process
58cc861f74SLuis R. Rodriguez */
59203c4805SLuis R. Rodriguez ath9k_hw_setuprxdesc(ah, ds,
60cc861f74SLuis R. Rodriguez common->rx_bufsize,
61203c4805SLuis R. Rodriguez 0);
62203c4805SLuis R. Rodriguez
637dd74f5fSFelix Fietkau if (sc->rx.rxlink)
64203c4805SLuis R. Rodriguez *sc->rx.rxlink = bf->bf_daddr;
657dd74f5fSFelix Fietkau else if (!flush)
667dd74f5fSFelix Fietkau ath9k_hw_putrxbuf(ah, bf->bf_daddr);
67203c4805SLuis R. Rodriguez
68203c4805SLuis R. Rodriguez sc->rx.rxlink = &ds->ds_link;
69203c4805SLuis R. Rodriguez }
70203c4805SLuis R. Rodriguez
ath_rx_buf_relink(struct ath_softc * sc,struct ath_rxbuf * bf,bool flush)717dd74f5fSFelix Fietkau static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf,
727dd74f5fSFelix Fietkau bool flush)
73e96542e5SFelix Fietkau {
74e96542e5SFelix Fietkau if (sc->rx.buf_hold)
757dd74f5fSFelix Fietkau ath_rx_buf_link(sc, sc->rx.buf_hold, flush);
76e96542e5SFelix Fietkau
77e96542e5SFelix Fietkau sc->rx.buf_hold = bf;
78e96542e5SFelix Fietkau }
79e96542e5SFelix Fietkau
ath_setdefantenna(struct ath_softc * sc,u32 antenna)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
ath_opmode_init(struct ath_softc * sc)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
ath_rx_edma_buf_link(struct ath_softc * sc,enum ath9k_rx_qtype qtype)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;
1161a04d59dSFelix Fietkau struct ath_rxbuf *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
1221a04d59dSFelix Fietkau bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
123b5c80475SFelix Fietkau list_del_init(&bf->list);
124b5c80475SFelix Fietkau
125b5c80475SFelix Fietkau skb = bf->bf_mpdu;
126b5c80475SFelix Fietkau
127b5c80475SFelix Fietkau memset(skb->data, 0, ah->caps.rx_status_len);
128b5c80475SFelix Fietkau dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
129b5c80475SFelix Fietkau ah->caps.rx_status_len, DMA_TO_DEVICE);
130b5c80475SFelix Fietkau
131b5c80475SFelix Fietkau SKB_CB_ATHBUF(skb) = bf;
132b5c80475SFelix Fietkau ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
13307236bf3SSujith Manoharan __skb_queue_tail(&rx_edma->rx_fifo, skb);
134b5c80475SFelix Fietkau
135b5c80475SFelix Fietkau return true;
136b5c80475SFelix Fietkau }
137b5c80475SFelix Fietkau
ath_rx_addbuffer_edma(struct ath_softc * sc,enum ath9k_rx_qtype qtype)138b5c80475SFelix Fietkau static void ath_rx_addbuffer_edma(struct ath_softc *sc,
1397a897203SSujith Manoharan enum ath9k_rx_qtype qtype)
140b5c80475SFelix Fietkau {
141b5c80475SFelix Fietkau struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1421a04d59dSFelix Fietkau struct ath_rxbuf *bf, *tbf;
143b5c80475SFelix Fietkau
144b5c80475SFelix Fietkau if (list_empty(&sc->rx.rxbuf)) {
145d2182b69SJoe Perches ath_dbg(common, QUEUE, "No free rx buf available\n");
146b5c80475SFelix Fietkau return;
147b5c80475SFelix Fietkau }
148b5c80475SFelix Fietkau
1496a01f0c0SMohammed Shafi Shajakhan list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
150b5c80475SFelix Fietkau if (!ath_rx_edma_buf_link(sc, qtype))
151b5c80475SFelix Fietkau break;
152b5c80475SFelix Fietkau
153b5c80475SFelix Fietkau }
154b5c80475SFelix Fietkau
ath_rx_remove_buffer(struct ath_softc * sc,enum ath9k_rx_qtype qtype)155b5c80475SFelix Fietkau static void ath_rx_remove_buffer(struct ath_softc *sc,
156b5c80475SFelix Fietkau enum ath9k_rx_qtype qtype)
157b5c80475SFelix Fietkau {
1581a04d59dSFelix Fietkau struct ath_rxbuf *bf;
159b5c80475SFelix Fietkau struct ath_rx_edma *rx_edma;
160b5c80475SFelix Fietkau struct sk_buff *skb;
161b5c80475SFelix Fietkau
162b5c80475SFelix Fietkau rx_edma = &sc->rx.rx_edma[qtype];
163b5c80475SFelix Fietkau
16407236bf3SSujith Manoharan while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
165b5c80475SFelix Fietkau bf = SKB_CB_ATHBUF(skb);
166b5c80475SFelix Fietkau BUG_ON(!bf);
167b5c80475SFelix Fietkau list_add_tail(&bf->list, &sc->rx.rxbuf);
168b5c80475SFelix Fietkau }
169b5c80475SFelix Fietkau }
170b5c80475SFelix Fietkau
ath_rx_edma_cleanup(struct ath_softc * sc)171b5c80475SFelix Fietkau static void ath_rx_edma_cleanup(struct ath_softc *sc)
172b5c80475SFelix Fietkau {
173ba542385SMohammed Shafi Shajakhan struct ath_hw *ah = sc->sc_ah;
174ba542385SMohammed Shafi Shajakhan struct ath_common *common = ath9k_hw_common(ah);
1751a04d59dSFelix Fietkau struct ath_rxbuf *bf;
176b5c80475SFelix Fietkau
177b5c80475SFelix Fietkau ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
178b5c80475SFelix Fietkau ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
179b5c80475SFelix Fietkau
180b5c80475SFelix Fietkau list_for_each_entry(bf, &sc->rx.rxbuf, list) {
181ba542385SMohammed Shafi Shajakhan if (bf->bf_mpdu) {
182ba542385SMohammed Shafi Shajakhan dma_unmap_single(sc->dev, bf->bf_buf_addr,
183ba542385SMohammed Shafi Shajakhan common->rx_bufsize,
184ba542385SMohammed Shafi Shajakhan DMA_BIDIRECTIONAL);
185b5c80475SFelix Fietkau dev_kfree_skb_any(bf->bf_mpdu);
186ba542385SMohammed Shafi Shajakhan bf->bf_buf_addr = 0;
187ba542385SMohammed Shafi Shajakhan bf->bf_mpdu = NULL;
188ba542385SMohammed Shafi Shajakhan }
189b5c80475SFelix Fietkau }
190b5c80475SFelix Fietkau }
191b5c80475SFelix Fietkau
ath_rx_edma_init_queue(struct ath_rx_edma * rx_edma,int size)192b5c80475SFelix Fietkau static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
193b5c80475SFelix Fietkau {
1945d07cca2SSujith Manoharan __skb_queue_head_init(&rx_edma->rx_fifo);
195b5c80475SFelix Fietkau rx_edma->rx_fifo_hwsize = size;
196b5c80475SFelix Fietkau }
197b5c80475SFelix Fietkau
ath_rx_edma_init(struct ath_softc * sc,int nbufs)198b5c80475SFelix Fietkau static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
199b5c80475SFelix Fietkau {
200b5c80475SFelix Fietkau struct ath_common *common = ath9k_hw_common(sc->sc_ah);
201b5c80475SFelix Fietkau struct ath_hw *ah = sc->sc_ah;
202b5c80475SFelix Fietkau struct sk_buff *skb;
2031a04d59dSFelix Fietkau struct ath_rxbuf *bf;
204b5c80475SFelix Fietkau int error = 0, i;
205b5c80475SFelix Fietkau u32 size;
206b5c80475SFelix Fietkau
207b5c80475SFelix Fietkau ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
208b5c80475SFelix Fietkau ah->caps.rx_status_len);
209b5c80475SFelix Fietkau
210b5c80475SFelix Fietkau ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
211b5c80475SFelix Fietkau ah->caps.rx_lp_qdepth);
212b5c80475SFelix Fietkau ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
213b5c80475SFelix Fietkau ah->caps.rx_hp_qdepth);
214b5c80475SFelix Fietkau
2151a04d59dSFelix Fietkau size = sizeof(struct ath_rxbuf) * nbufs;
216b81950b1SFelix Fietkau bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
217b5c80475SFelix Fietkau if (!bf)
218b5c80475SFelix Fietkau return -ENOMEM;
219b5c80475SFelix Fietkau
220b5c80475SFelix Fietkau INIT_LIST_HEAD(&sc->rx.rxbuf);
221b5c80475SFelix Fietkau
222b5c80475SFelix Fietkau for (i = 0; i < nbufs; i++, bf++) {
223b5c80475SFelix Fietkau skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
224b5c80475SFelix Fietkau if (!skb) {
225b5c80475SFelix Fietkau error = -ENOMEM;
226b5c80475SFelix Fietkau goto rx_init_fail;
227b5c80475SFelix Fietkau }
228b5c80475SFelix Fietkau
229b5c80475SFelix Fietkau memset(skb->data, 0, common->rx_bufsize);
230b5c80475SFelix Fietkau bf->bf_mpdu = skb;
231b5c80475SFelix Fietkau
232b5c80475SFelix Fietkau bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
233b5c80475SFelix Fietkau common->rx_bufsize,
234b5c80475SFelix Fietkau DMA_BIDIRECTIONAL);
235b5c80475SFelix Fietkau if (unlikely(dma_mapping_error(sc->dev,
236b5c80475SFelix Fietkau bf->bf_buf_addr))) {
237b5c80475SFelix Fietkau dev_kfree_skb_any(skb);
238b5c80475SFelix Fietkau bf->bf_mpdu = NULL;
2396cf9e995SBen Greear bf->bf_buf_addr = 0;
2403800276aSJoe Perches ath_err(common,
241b5c80475SFelix Fietkau "dma_mapping_error() on RX init\n");
242b5c80475SFelix Fietkau error = -ENOMEM;
243b5c80475SFelix Fietkau goto rx_init_fail;
244b5c80475SFelix Fietkau }
245b5c80475SFelix Fietkau
246b5c80475SFelix Fietkau list_add_tail(&bf->list, &sc->rx.rxbuf);
247b5c80475SFelix Fietkau }
248b5c80475SFelix Fietkau
249b5c80475SFelix Fietkau return 0;
250b5c80475SFelix Fietkau
251b5c80475SFelix Fietkau rx_init_fail:
252b5c80475SFelix Fietkau ath_rx_edma_cleanup(sc);
253b5c80475SFelix Fietkau return error;
254b5c80475SFelix Fietkau }
255b5c80475SFelix Fietkau
ath_edma_start_recv(struct ath_softc * sc)256b5c80475SFelix Fietkau static void ath_edma_start_recv(struct ath_softc *sc)
257b5c80475SFelix Fietkau {
258b5c80475SFelix Fietkau ath9k_hw_rxena(sc->sc_ah);
2597a897203SSujith Manoharan ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
2607a897203SSujith Manoharan ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
261b5c80475SFelix Fietkau ath_opmode_init(sc);
262fbbcd146SFelix Fietkau ath9k_hw_startpcureceive(sc->sc_ah, sc->cur_chan->offchannel);
263b5c80475SFelix Fietkau }
264b5c80475SFelix Fietkau
ath_edma_stop_recv(struct ath_softc * sc)265b5c80475SFelix Fietkau static void ath_edma_stop_recv(struct ath_softc *sc)
266b5c80475SFelix Fietkau {
267b5c80475SFelix Fietkau ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
268b5c80475SFelix Fietkau ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
269b5c80475SFelix Fietkau }
270b5c80475SFelix Fietkau
ath_rx_init(struct ath_softc * sc,int nbufs)271203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs)
272203c4805SLuis R. Rodriguez {
27327c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah);
274203c4805SLuis R. Rodriguez struct sk_buff *skb;
2751a04d59dSFelix Fietkau struct ath_rxbuf *bf;
276203c4805SLuis R. Rodriguez int error = 0;
277203c4805SLuis R. Rodriguez
2784bdd1e97SLuis R. Rodriguez spin_lock_init(&sc->sc_pcu_lock);
279203c4805SLuis R. Rodriguez
2800d95521eSFelix Fietkau common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
2810d95521eSFelix Fietkau sc->sc_ah->caps.rx_status_len;
2820d95521eSFelix Fietkau
283e87f3d53SSujith Manoharan if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
284b5c80475SFelix Fietkau return ath_rx_edma_init(sc, nbufs);
285e87f3d53SSujith Manoharan
286d2182b69SJoe Perches ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
287cc861f74SLuis R. Rodriguez common->cachelsz, common->rx_bufsize);
288203c4805SLuis R. Rodriguez
289203c4805SLuis R. Rodriguez /* Initialize rx descriptors */
290203c4805SLuis R. Rodriguez
291203c4805SLuis R. Rodriguez error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
2924adfcdedSVasanthakumar Thiagarajan "rx", nbufs, 1, 0);
293203c4805SLuis R. Rodriguez if (error != 0) {
2943800276aSJoe Perches ath_err(common,
295b5c80475SFelix Fietkau "failed to allocate rx descriptors: %d\n",
296b5c80475SFelix Fietkau error);
297203c4805SLuis R. Rodriguez goto err;
298203c4805SLuis R. Rodriguez }
299203c4805SLuis R. Rodriguez
300203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) {
301b5c80475SFelix Fietkau skb = ath_rxbuf_alloc(common, common->rx_bufsize,
302b5c80475SFelix Fietkau GFP_KERNEL);
303203c4805SLuis R. Rodriguez if (skb == NULL) {
304203c4805SLuis R. Rodriguez error = -ENOMEM;
305203c4805SLuis R. Rodriguez goto err;
306203c4805SLuis R. Rodriguez }
307203c4805SLuis R. Rodriguez
308203c4805SLuis R. Rodriguez bf->bf_mpdu = skb;
309203c4805SLuis R. Rodriguez bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
310cc861f74SLuis R. Rodriguez common->rx_bufsize,
311203c4805SLuis R. Rodriguez DMA_FROM_DEVICE);
312203c4805SLuis R. Rodriguez if (unlikely(dma_mapping_error(sc->dev,
313203c4805SLuis R. Rodriguez bf->bf_buf_addr))) {
314203c4805SLuis R. Rodriguez dev_kfree_skb_any(skb);
315203c4805SLuis R. Rodriguez bf->bf_mpdu = NULL;
3166cf9e995SBen Greear bf->bf_buf_addr = 0;
3173800276aSJoe Perches ath_err(common,
318203c4805SLuis R. Rodriguez "dma_mapping_error() on RX init\n");
319203c4805SLuis R. Rodriguez error = -ENOMEM;
320203c4805SLuis R. Rodriguez goto err;
321203c4805SLuis R. Rodriguez }
322203c4805SLuis R. Rodriguez }
323203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL;
324203c4805SLuis R. Rodriguez err:
325203c4805SLuis R. Rodriguez if (error)
326203c4805SLuis R. Rodriguez ath_rx_cleanup(sc);
327203c4805SLuis R. Rodriguez
328203c4805SLuis R. Rodriguez return error;
329203c4805SLuis R. Rodriguez }
330203c4805SLuis R. Rodriguez
ath_rx_cleanup(struct ath_softc * sc)331203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc)
332203c4805SLuis R. Rodriguez {
333cc861f74SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah;
334cc861f74SLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
335203c4805SLuis R. Rodriguez struct sk_buff *skb;
3361a04d59dSFelix Fietkau struct ath_rxbuf *bf;
337203c4805SLuis R. Rodriguez
338b5c80475SFelix Fietkau if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
339b5c80475SFelix Fietkau ath_rx_edma_cleanup(sc);
340b5c80475SFelix Fietkau return;
341e87f3d53SSujith Manoharan }
342e87f3d53SSujith Manoharan
343203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) {
344203c4805SLuis R. Rodriguez skb = bf->bf_mpdu;
345203c4805SLuis R. Rodriguez if (skb) {
346203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr,
347b5c80475SFelix Fietkau common->rx_bufsize,
348b5c80475SFelix Fietkau DMA_FROM_DEVICE);
349203c4805SLuis R. Rodriguez dev_kfree_skb(skb);
3506cf9e995SBen Greear bf->bf_buf_addr = 0;
3516cf9e995SBen Greear bf->bf_mpdu = NULL;
352203c4805SLuis R. Rodriguez }
353203c4805SLuis R. Rodriguez }
354203c4805SLuis R. Rodriguez }
355203c4805SLuis R. Rodriguez
356203c4805SLuis R. Rodriguez /*
357203c4805SLuis R. Rodriguez * Calculate the receive filter according to the
358203c4805SLuis R. Rodriguez * operating mode and state:
359203c4805SLuis R. Rodriguez *
360203c4805SLuis R. Rodriguez * o always accept unicast, broadcast, and multicast traffic
361203c4805SLuis R. Rodriguez * o maintain current state of phy error reception (the hal
362203c4805SLuis R. Rodriguez * may enable phy error frames for noise immunity work)
363203c4805SLuis R. Rodriguez * o probe request frames are accepted only when operating in
364203c4805SLuis R. Rodriguez * hostap, adhoc, or monitor modes
365203c4805SLuis R. Rodriguez * o enable promiscuous mode according to the interface state
366203c4805SLuis R. Rodriguez * o accept beacons:
367203c4805SLuis R. Rodriguez * - when operating in adhoc mode so the 802.11 layer creates
368203c4805SLuis R. Rodriguez * node table entries for peers,
369203c4805SLuis R. Rodriguez * - when operating in station mode for collecting rssi data when
370203c4805SLuis R. Rodriguez * the station is otherwise quiet, or
371203c4805SLuis R. Rodriguez * - when operating as a repeater so we see repeater-sta beacons
372203c4805SLuis R. Rodriguez * - when scanning
373203c4805SLuis R. Rodriguez */
374203c4805SLuis R. Rodriguez
ath_calcrxfilter(struct ath_softc * sc)375203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc)
376203c4805SLuis R. Rodriguez {
37778b21949SFelix Fietkau struct ath_common *common = ath9k_hw_common(sc->sc_ah);
378203c4805SLuis R. Rodriguez u32 rfilt;
379203c4805SLuis R. Rodriguez
38097f2645fSMasahiro Yamada if (IS_ENABLED(CONFIG_ATH9K_TX99))
38189f927afSLuis R. Rodriguez return 0;
38289f927afSLuis R. Rodriguez
383ac06697cSFelix Fietkau rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
384203c4805SLuis R. Rodriguez | ATH9K_RX_FILTER_MCAST;
385203c4805SLuis R. Rodriguez
38673e4937dSZefir Kurtisi /* if operating on a DFS channel, enable radar pulse detection */
38773e4937dSZefir Kurtisi if (sc->hw->conf.radar_enabled)
38873e4937dSZefir Kurtisi rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
38973e4937dSZefir Kurtisi
390fce34430SSujith Manoharan spin_lock_bh(&sc->chan_lock);
391fce34430SSujith Manoharan
392fce34430SSujith Manoharan if (sc->cur_chan->rxfilter & FIF_PROBE_REQ)
393203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROBEREQ;
394203c4805SLuis R. Rodriguez
3952e286947SFelix Fietkau if (sc->sc_ah->is_monitoring)
396203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROM;
397203c4805SLuis R. Rodriguez
39835c273eaSLorenzo Bianconi if ((sc->cur_chan->rxfilter & FIF_CONTROL) ||
39935c273eaSLorenzo Bianconi sc->sc_ah->dynack.enabled)
400203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_CONTROL;
401203c4805SLuis R. Rodriguez
402203c4805SLuis R. Rodriguez if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
403ca529c93SSujith Manoharan (sc->cur_chan->nvifs <= 1) &&
404fce34430SSujith Manoharan !(sc->cur_chan->rxfilter & FIF_BCN_PRBRESP_PROMISC))
405203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MYBEACON;
406862a336cSJan Kaisrlik else if (sc->sc_ah->opmode != NL80211_IFTYPE_OCB)
407203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_BEACON;
408203c4805SLuis R. Rodriguez
409264bbec8SFelix Fietkau if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
410fce34430SSujith Manoharan (sc->cur_chan->rxfilter & FIF_PSPOLL))
411203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PSPOLL;
412203c4805SLuis R. Rodriguez
4133d1132d0SSujith Manoharan if (sc->cur_chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
4147ea310beSSujith rfilt |= ATH9K_RX_FILTER_COMP_BAR;
4157ea310beSSujith
416*c01c320dSJouni Malinen if (sc->cur_chan->nvifs > 1 ||
417*c01c320dSJouni Malinen (sc->cur_chan->rxfilter & (FIF_OTHER_BSS | FIF_MCAST_ACTION))) {
418a549459cSThomas Wagner /* This is needed for older chips */
419a549459cSThomas Wagner if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
4205eb6ba83SJavier Cardona rfilt |= ATH9K_RX_FILTER_PROM;
421203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
422203c4805SLuis R. Rodriguez }
423203c4805SLuis R. Rodriguez
424ede6a5e7SMiaoqing Pan if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah) ||
425ede6a5e7SMiaoqing Pan AR_SREV_9561(sc->sc_ah))
426b3d7aa43SGabor Juhos rfilt |= ATH9K_RX_FILTER_4ADDRESS;
427b3d7aa43SGabor Juhos
428f0b2c30aSMiaoqing Pan if (AR_SREV_9462(sc->sc_ah) || AR_SREV_9565(sc->sc_ah))
429f0b2c30aSMiaoqing Pan rfilt |= ATH9K_RX_FILTER_CONTROL_WRAPPER;
430f0b2c30aSMiaoqing Pan
431499afaccSSujith Manoharan if (ath9k_is_chanctx_enabled() &&
43278b21949SFelix Fietkau test_bit(ATH_OP_SCANNING, &common->op_flags))
43378b21949SFelix Fietkau rfilt |= ATH9K_RX_FILTER_BEACON;
43478b21949SFelix Fietkau
435fce34430SSujith Manoharan spin_unlock_bh(&sc->chan_lock);
436fce34430SSujith Manoharan
437203c4805SLuis R. Rodriguez return rfilt;
438203c4805SLuis R. Rodriguez
439203c4805SLuis R. Rodriguez }
440203c4805SLuis R. Rodriguez
ath_startrecv(struct ath_softc * sc)44119ec477fSSujith Manoharan void ath_startrecv(struct ath_softc *sc)
442203c4805SLuis R. Rodriguez {
443203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah;
4441a04d59dSFelix Fietkau struct ath_rxbuf *bf, *tbf;
445203c4805SLuis R. Rodriguez
446b5c80475SFelix Fietkau if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
447b5c80475SFelix Fietkau ath_edma_start_recv(sc);
44819ec477fSSujith Manoharan return;
449b5c80475SFelix Fietkau }
450b5c80475SFelix Fietkau
451203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf))
452203c4805SLuis R. Rodriguez goto start_recv;
453203c4805SLuis R. Rodriguez
454e96542e5SFelix Fietkau sc->rx.buf_hold = NULL;
455203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL;
456203c4805SLuis R. Rodriguez list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
4577dd74f5fSFelix Fietkau ath_rx_buf_link(sc, bf, false);
458203c4805SLuis R. Rodriguez }
459203c4805SLuis R. Rodriguez
460203c4805SLuis R. Rodriguez /* We could have deleted elements so the list may be empty now */
461203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf))
462203c4805SLuis R. Rodriguez goto start_recv;
463203c4805SLuis R. Rodriguez
4641a04d59dSFelix Fietkau bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
465203c4805SLuis R. Rodriguez ath9k_hw_putrxbuf(ah, bf->bf_daddr);
466203c4805SLuis R. Rodriguez ath9k_hw_rxena(ah);
467203c4805SLuis R. Rodriguez
468203c4805SLuis R. Rodriguez start_recv:
469203c4805SLuis R. Rodriguez ath_opmode_init(sc);
470fbbcd146SFelix Fietkau ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel);
471203c4805SLuis R. Rodriguez }
472203c4805SLuis R. Rodriguez
ath_flushrecv(struct ath_softc * sc)4734b883f02SFelix Fietkau static void ath_flushrecv(struct ath_softc *sc)
4744b883f02SFelix Fietkau {
4754b883f02SFelix Fietkau if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
4764b883f02SFelix Fietkau ath_rx_tasklet(sc, 1, true);
4774b883f02SFelix Fietkau ath_rx_tasklet(sc, 1, false);
4784b883f02SFelix Fietkau }
4794b883f02SFelix Fietkau
ath_stoprecv(struct ath_softc * sc)480203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc)
481203c4805SLuis R. Rodriguez {
482203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah;
4835882da02SFelix Fietkau bool stopped, reset = false;
484203c4805SLuis R. Rodriguez
485d47844a0SFelix Fietkau ath9k_hw_abortpcurecv(ah);
486203c4805SLuis R. Rodriguez ath9k_hw_setrxfilter(ah, 0);
4875882da02SFelix Fietkau stopped = ath9k_hw_stopdmarecv(ah, &reset);
488b5c80475SFelix Fietkau
4894b883f02SFelix Fietkau ath_flushrecv(sc);
4904b883f02SFelix Fietkau
491b5c80475SFelix Fietkau if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
492b5c80475SFelix Fietkau ath_edma_stop_recv(sc);
493b5c80475SFelix Fietkau else
494203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL;
495203c4805SLuis R. Rodriguez
496d584747bSRajkumar Manoharan if (!(ah->ah_flags & AH_UNPLUGGED) &&
497d584747bSRajkumar Manoharan unlikely(!stopped)) {
498e60ac9c7SFelix Fietkau ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
499e60ac9c7SFelix Fietkau "Failed to stop Rx DMA\n");
500e60ac9c7SFelix Fietkau RESET_STAT_INC(sc, RESET_RX_DMA_ERROR);
501d7fd1b50SBen Greear }
5022232d31bSFelix Fietkau return stopped && !reset;
503203c4805SLuis R. Rodriguez }
504203c4805SLuis R. Rodriguez
ath_beacon_dtim_pending_cab(struct sk_buff * skb)505cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
506cc65965cSJouni Malinen {
507cc65965cSJouni Malinen /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
508cc65965cSJouni Malinen struct ieee80211_mgmt *mgmt;
509cc65965cSJouni Malinen u8 *pos, *end, id, elen;
510cc65965cSJouni Malinen struct ieee80211_tim_ie *tim;
511cc65965cSJouni Malinen
512cc65965cSJouni Malinen mgmt = (struct ieee80211_mgmt *)skb->data;
513cc65965cSJouni Malinen pos = mgmt->u.beacon.variable;
514cc65965cSJouni Malinen end = skb->data + skb->len;
515cc65965cSJouni Malinen
516cc65965cSJouni Malinen while (pos + 2 < end) {
517cc65965cSJouni Malinen id = *pos++;
518cc65965cSJouni Malinen elen = *pos++;
519cc65965cSJouni Malinen if (pos + elen > end)
520cc65965cSJouni Malinen break;
521cc65965cSJouni Malinen
522cc65965cSJouni Malinen if (id == WLAN_EID_TIM) {
523cc65965cSJouni Malinen if (elen < sizeof(*tim))
524cc65965cSJouni Malinen break;
525cc65965cSJouni Malinen tim = (struct ieee80211_tim_ie *) pos;
526cc65965cSJouni Malinen if (tim->dtim_count != 0)
527cc65965cSJouni Malinen break;
528cc65965cSJouni Malinen return tim->bitmap_ctrl & 0x01;
529cc65965cSJouni Malinen }
530cc65965cSJouni Malinen
531cc65965cSJouni Malinen pos += elen;
532cc65965cSJouni Malinen }
533cc65965cSJouni Malinen
534cc65965cSJouni Malinen return false;
535cc65965cSJouni Malinen }
536cc65965cSJouni Malinen
ath_rx_ps_beacon(struct ath_softc * sc,struct sk_buff * skb)537cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
538cc65965cSJouni Malinen {
5391510718dSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah);
54048bf43faSSujith Manoharan bool skip_beacon = false;
541cc65965cSJouni Malinen
542cc65965cSJouni Malinen if (skb->len < 24 + 8 + 2 + 2)
543cc65965cSJouni Malinen return;
544cc65965cSJouni Malinen
5451b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
546293dc5dfSGabor Juhos
5471b04b930SSujith if (sc->ps_flags & PS_BEACON_SYNC) {
5481b04b930SSujith sc->ps_flags &= ~PS_BEACON_SYNC;
549d2182b69SJoe Perches ath_dbg(common, PS,
5501a6404a1SSujith Manoharan "Reconfigure beacon timers based on synchronized timestamp\n");
55148bf43faSSujith Manoharan
552853854d6SSujith Manoharan #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
55348bf43faSSujith Manoharan if (ath9k_is_chanctx_enabled()) {
55448bf43faSSujith Manoharan if (sc->cur_chan == &sc->offchannel.chan)
55548bf43faSSujith Manoharan skip_beacon = true;
55648bf43faSSujith Manoharan }
557853854d6SSujith Manoharan #endif
55848bf43faSSujith Manoharan
55948bf43faSSujith Manoharan if (!skip_beacon &&
56048bf43faSSujith Manoharan !(WARN_ON_ONCE(sc->cur_chan->beacon.beacon_interval == 0)))
561ef4ad633SSujith Manoharan ath9k_set_beacon(sc);
562c7dd40c9SSujith Manoharan
563c7dd40c9SSujith Manoharan ath9k_p2p_beacon_sync(sc);
564ccdfeab6SJouni Malinen }
565ccdfeab6SJouni Malinen
566cc65965cSJouni Malinen if (ath_beacon_dtim_pending_cab(skb)) {
567cc65965cSJouni Malinen /*
568cc65965cSJouni Malinen * Remain awake waiting for buffered broadcast/multicast
56958f5fffdSGabor Juhos * frames. If the last broadcast/multicast frame is not
57058f5fffdSGabor Juhos * received properly, the next beacon frame will work as
57158f5fffdSGabor Juhos * a backup trigger for returning into NETWORK SLEEP state,
57258f5fffdSGabor Juhos * so we are waiting for it as well.
573cc65965cSJouni Malinen */
574d2182b69SJoe Perches ath_dbg(common, PS,
575226afe68SJoe Perches "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
5761b04b930SSujith sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
577cc65965cSJouni Malinen return;
578cc65965cSJouni Malinen }
579cc65965cSJouni Malinen
5801b04b930SSujith if (sc->ps_flags & PS_WAIT_FOR_CAB) {
581cc65965cSJouni Malinen /*
582cc65965cSJouni Malinen * This can happen if a broadcast frame is dropped or the AP
583cc65965cSJouni Malinen * fails to send a frame indicating that all CAB frames have
584cc65965cSJouni Malinen * been delivered.
585cc65965cSJouni Malinen */
5861b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_CAB;
587d2182b69SJoe Perches ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
588cc65965cSJouni Malinen }
589cc65965cSJouni Malinen }
590cc65965cSJouni Malinen
ath_rx_ps(struct ath_softc * sc,struct sk_buff * skb,bool mybeacon)591f73c604cSRajkumar Manoharan static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
592cc65965cSJouni Malinen {
593cc65965cSJouni Malinen struct ieee80211_hdr *hdr;
594c46917bbSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah);
595cc65965cSJouni Malinen
596cc65965cSJouni Malinen hdr = (struct ieee80211_hdr *)skb->data;
597cc65965cSJouni Malinen
598cc65965cSJouni Malinen /* Process Beacon and CAB receive in PS state */
599ededf1f8SVasanthakumar Thiagarajan if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
60007c15a3fSSujith Manoharan && mybeacon) {
601cc65965cSJouni Malinen ath_rx_ps_beacon(sc, skb);
60207c15a3fSSujith Manoharan } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
603cc65965cSJouni Malinen (ieee80211_is_data(hdr->frame_control) ||
604cc65965cSJouni Malinen ieee80211_is_action(hdr->frame_control)) &&
605cc65965cSJouni Malinen is_multicast_ether_addr(hdr->addr1) &&
606cc65965cSJouni Malinen !ieee80211_has_moredata(hdr->frame_control)) {
607cc65965cSJouni Malinen /*
608cc65965cSJouni Malinen * No more broadcast/multicast frames to be received at this
609cc65965cSJouni Malinen * point.
610cc65965cSJouni Malinen */
6113fac6dfdSSenthil Balasubramanian sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
612d2182b69SJoe Perches ath_dbg(common, PS,
613c46917bbSLuis R. Rodriguez "All PS CAB frames received, back to sleep\n");
6141b04b930SSujith } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
6159a23f9caSJouni Malinen !is_multicast_ether_addr(hdr->addr1) &&
6169a23f9caSJouni Malinen !ieee80211_has_morefrags(hdr->frame_control)) {
6171b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
618d2182b69SJoe Perches ath_dbg(common, PS,
619226afe68SJoe Perches "Going back to sleep after having received PS-Poll data (0x%lx)\n",
6201b04b930SSujith sc->ps_flags & (PS_WAIT_FOR_BEACON |
6211b04b930SSujith PS_WAIT_FOR_CAB |
6221b04b930SSujith PS_WAIT_FOR_PSPOLL_DATA |
6231b04b930SSujith PS_WAIT_FOR_TX_ACK));
624cc65965cSJouni Malinen }
625cc65965cSJouni Malinen }
626cc65965cSJouni Malinen
ath_edma_get_buffers(struct ath_softc * sc,enum ath9k_rx_qtype qtype,struct ath_rx_status * rs,struct ath_rxbuf ** dest)627b5c80475SFelix Fietkau static bool ath_edma_get_buffers(struct ath_softc *sc,
6283a2923e8SFelix Fietkau enum ath9k_rx_qtype qtype,
6293a2923e8SFelix Fietkau struct ath_rx_status *rs,
6301a04d59dSFelix Fietkau struct ath_rxbuf **dest)
631203c4805SLuis R. Rodriguez {
632b5c80475SFelix Fietkau struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
633203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah;
63427c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
635b5c80475SFelix Fietkau struct sk_buff *skb;
6361a04d59dSFelix Fietkau struct ath_rxbuf *bf;
637b5c80475SFelix Fietkau int ret;
638203c4805SLuis R. Rodriguez
639b5c80475SFelix Fietkau skb = skb_peek(&rx_edma->rx_fifo);
640b5c80475SFelix Fietkau if (!skb)
641b5c80475SFelix Fietkau return false;
642203c4805SLuis R. Rodriguez
643b5c80475SFelix Fietkau bf = SKB_CB_ATHBUF(skb);
644b5c80475SFelix Fietkau BUG_ON(!bf);
645b5c80475SFelix Fietkau
646ce9426d1SMing Lei dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
647b5c80475SFelix Fietkau common->rx_bufsize, DMA_FROM_DEVICE);
648b5c80475SFelix Fietkau
6493a2923e8SFelix Fietkau ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
650ce9426d1SMing Lei if (ret == -EINPROGRESS) {
651ce9426d1SMing Lei /*let device gain the buffer again*/
652ce9426d1SMing Lei dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
653ce9426d1SMing Lei common->rx_bufsize, DMA_FROM_DEVICE);
654b5c80475SFelix Fietkau return false;
655ce9426d1SMing Lei }
656b5c80475SFelix Fietkau
657b5c80475SFelix Fietkau __skb_unlink(skb, &rx_edma->rx_fifo);
658b5c80475SFelix Fietkau if (ret == -EINVAL) {
659b5c80475SFelix Fietkau /* corrupt descriptor, skip this one and the following one */
660b5c80475SFelix Fietkau list_add_tail(&bf->list, &sc->rx.rxbuf);
661b5c80475SFelix Fietkau ath_rx_edma_buf_link(sc, qtype);
662b5c80475SFelix Fietkau
6633a2923e8SFelix Fietkau skb = skb_peek(&rx_edma->rx_fifo);
6643a2923e8SFelix Fietkau if (skb) {
665b5c80475SFelix Fietkau bf = SKB_CB_ATHBUF(skb);
666b5c80475SFelix Fietkau BUG_ON(!bf);
667b5c80475SFelix Fietkau
668b5c80475SFelix Fietkau __skb_unlink(skb, &rx_edma->rx_fifo);
669b5c80475SFelix Fietkau list_add_tail(&bf->list, &sc->rx.rxbuf);
670b5c80475SFelix Fietkau ath_rx_edma_buf_link(sc, qtype);
671b5c80475SFelix Fietkau }
6726bb51c70STom Hughes
6736bb51c70STom Hughes bf = NULL;
6743a2923e8SFelix Fietkau }
675b5c80475SFelix Fietkau
6763a2923e8SFelix Fietkau *dest = bf;
677b5c80475SFelix Fietkau return true;
678b5c80475SFelix Fietkau }
679b5c80475SFelix Fietkau
ath_edma_get_next_rx_buf(struct ath_softc * sc,struct ath_rx_status * rs,enum ath9k_rx_qtype qtype)6801a04d59dSFelix Fietkau static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
681b5c80475SFelix Fietkau struct ath_rx_status *rs,
682b5c80475SFelix Fietkau enum ath9k_rx_qtype qtype)
683b5c80475SFelix Fietkau {
6841a04d59dSFelix Fietkau struct ath_rxbuf *bf = NULL;
685b5c80475SFelix Fietkau
6863a2923e8SFelix Fietkau while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
6873a2923e8SFelix Fietkau if (!bf)
6883a2923e8SFelix Fietkau continue;
689b5c80475SFelix Fietkau
690b5c80475SFelix Fietkau return bf;
691b5c80475SFelix Fietkau }
6923a2923e8SFelix Fietkau return NULL;
6933a2923e8SFelix Fietkau }
694b5c80475SFelix Fietkau
ath_get_next_rx_buf(struct ath_softc * sc,struct ath_rx_status * rs)6951a04d59dSFelix Fietkau static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
696b5c80475SFelix Fietkau struct ath_rx_status *rs)
697b5c80475SFelix Fietkau {
698b5c80475SFelix Fietkau struct ath_hw *ah = sc->sc_ah;
699b5c80475SFelix Fietkau struct ath_common *common = ath9k_hw_common(ah);
700b5c80475SFelix Fietkau struct ath_desc *ds;
7011a04d59dSFelix Fietkau struct ath_rxbuf *bf;
702b5c80475SFelix Fietkau int ret;
703203c4805SLuis R. Rodriguez
704203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) {
705203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL;
706b5c80475SFelix Fietkau return NULL;
707203c4805SLuis R. Rodriguez }
708203c4805SLuis R. Rodriguez
7091a04d59dSFelix Fietkau bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
710e96542e5SFelix Fietkau if (bf == sc->rx.buf_hold)
711e96542e5SFelix Fietkau return NULL;
712e96542e5SFelix Fietkau
713203c4805SLuis R. Rodriguez ds = bf->bf_desc;
714203c4805SLuis R. Rodriguez
715203c4805SLuis R. Rodriguez /*
716203c4805SLuis R. Rodriguez * Must provide the virtual address of the current
717203c4805SLuis R. Rodriguez * descriptor, the physical address, and the virtual
718203c4805SLuis R. Rodriguez * address of the next descriptor in the h/w chain.
719203c4805SLuis R. Rodriguez * This allows the HAL to look ahead to see if the
720203c4805SLuis R. Rodriguez * hardware is done with a descriptor by checking the
721203c4805SLuis R. Rodriguez * done bit in the following descriptor and the address
722203c4805SLuis R. Rodriguez * of the current descriptor the DMA engine is working
723203c4805SLuis R. Rodriguez * on. All this is necessary because of our use of
724203c4805SLuis R. Rodriguez * a self-linked list to avoid rx overruns.
725203c4805SLuis R. Rodriguez */
7263de21116SRajkumar Manoharan ret = ath9k_hw_rxprocdesc(ah, ds, rs);
727b5c80475SFelix Fietkau if (ret == -EINPROGRESS) {
72829bffa96SFelix Fietkau struct ath_rx_status trs;
7291a04d59dSFelix Fietkau struct ath_rxbuf *tbf;
730203c4805SLuis R. Rodriguez struct ath_desc *tds;
731203c4805SLuis R. Rodriguez
73229bffa96SFelix Fietkau memset(&trs, 0, sizeof(trs));
733203c4805SLuis R. Rodriguez if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
734203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL;
735b5c80475SFelix Fietkau return NULL;
736203c4805SLuis R. Rodriguez }
737203c4805SLuis R. Rodriguez
7381a04d59dSFelix Fietkau tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
739203c4805SLuis R. Rodriguez
740203c4805SLuis R. Rodriguez /*
741203c4805SLuis R. Rodriguez * On some hardware the descriptor status words could
742203c4805SLuis R. Rodriguez * get corrupted, including the done bit. Because of
743203c4805SLuis R. Rodriguez * this, check if the next descriptor's done bit is
744203c4805SLuis R. Rodriguez * set or not.
745203c4805SLuis R. Rodriguez *
746203c4805SLuis R. Rodriguez * If the next descriptor's done bit is set, the current
747203c4805SLuis R. Rodriguez * descriptor has been corrupted. Force s/w to discard
748203c4805SLuis R. Rodriguez * this descriptor and continue...
749203c4805SLuis R. Rodriguez */
750203c4805SLuis R. Rodriguez
751203c4805SLuis R. Rodriguez tds = tbf->bf_desc;
7523de21116SRajkumar Manoharan ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
753b5c80475SFelix Fietkau if (ret == -EINPROGRESS)
754b5c80475SFelix Fietkau return NULL;
755723e7113SFelix Fietkau
756723e7113SFelix Fietkau /*
757b7b146c9SFelix Fietkau * Re-check previous descriptor, in case it has been filled
758b7b146c9SFelix Fietkau * in the mean time.
759b7b146c9SFelix Fietkau */
760b7b146c9SFelix Fietkau ret = ath9k_hw_rxprocdesc(ah, ds, rs);
761b7b146c9SFelix Fietkau if (ret == -EINPROGRESS) {
762b7b146c9SFelix Fietkau /*
763723e7113SFelix Fietkau * mark descriptor as zero-length and set the 'more'
764723e7113SFelix Fietkau * flag to ensure that both buffers get discarded
765723e7113SFelix Fietkau */
766723e7113SFelix Fietkau rs->rs_datalen = 0;
767723e7113SFelix Fietkau rs->rs_more = true;
768203c4805SLuis R. Rodriguez }
769b7b146c9SFelix Fietkau }
770203c4805SLuis R. Rodriguez
771a3dc48e8SFelix Fietkau list_del(&bf->list);
772b5c80475SFelix Fietkau if (!bf->bf_mpdu)
773b5c80475SFelix Fietkau return bf;
774203c4805SLuis R. Rodriguez
775203c4805SLuis R. Rodriguez /*
776203c4805SLuis R. Rodriguez * Synchronize the DMA transfer with CPU before
777203c4805SLuis R. Rodriguez * 1. accessing the frame
778203c4805SLuis R. Rodriguez * 2. requeueing the same buffer to h/w
779203c4805SLuis R. Rodriguez */
780ce9426d1SMing Lei dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
781cc861f74SLuis R. Rodriguez common->rx_bufsize,
782203c4805SLuis R. Rodriguez DMA_FROM_DEVICE);
783203c4805SLuis R. Rodriguez
784b5c80475SFelix Fietkau return bf;
785b5c80475SFelix Fietkau }
786b5c80475SFelix Fietkau
ath9k_process_tsf(struct ath_rx_status * rs,struct ieee80211_rx_status * rxs,u64 tsf)787e0dd1a96SSujith Manoharan static void ath9k_process_tsf(struct ath_rx_status *rs,
788e0dd1a96SSujith Manoharan struct ieee80211_rx_status *rxs,
789e0dd1a96SSujith Manoharan u64 tsf)
790e0dd1a96SSujith Manoharan {
791e0dd1a96SSujith Manoharan u32 tsf_lower = tsf & 0xffffffff;
792e0dd1a96SSujith Manoharan
793e0dd1a96SSujith Manoharan rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
794e0dd1a96SSujith Manoharan if (rs->rs_tstamp > tsf_lower &&
795e0dd1a96SSujith Manoharan unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
796e0dd1a96SSujith Manoharan rxs->mactime -= 0x100000000ULL;
797e0dd1a96SSujith Manoharan
798e0dd1a96SSujith Manoharan if (rs->rs_tstamp < tsf_lower &&
799e0dd1a96SSujith Manoharan unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
800e0dd1a96SSujith Manoharan rxs->mactime += 0x100000000ULL;
801e0dd1a96SSujith Manoharan }
802e0dd1a96SSujith Manoharan
803d435700fSSujith /*
804d435700fSSujith * For Decrypt or Demic errors, we only mark packet status here and always push
805d435700fSSujith * up the frame up to let mac80211 handle the actual error case, be it no
806d435700fSSujith * decryption key or real decryption error. This let us keep statistics there.
807d435700fSSujith */
ath9k_rx_skb_preprocess(struct ath_softc * sc,struct sk_buff * skb,struct ath_rx_status * rx_stats,struct ieee80211_rx_status * rx_status,bool * decrypt_error,u64 tsf)808723e7113SFelix Fietkau static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
8096f38482eSSujith Manoharan struct sk_buff *skb,
810d435700fSSujith struct ath_rx_status *rx_stats,
811d435700fSSujith struct ieee80211_rx_status *rx_status,
812e0dd1a96SSujith Manoharan bool *decrypt_error, u64 tsf)
813d435700fSSujith {
814723e7113SFelix Fietkau struct ieee80211_hw *hw = sc->hw;
815723e7113SFelix Fietkau struct ath_hw *ah = sc->sc_ah;
816723e7113SFelix Fietkau struct ath_common *common = ath9k_hw_common(ah);
8176f38482eSSujith Manoharan struct ieee80211_hdr *hdr;
818723e7113SFelix Fietkau bool discard_current = sc->rx.discard_next;
819df5c4150SZefir Kurtisi bool is_phyerr;
820723e7113SFelix Fietkau
8215871d2d7SSujith Manoharan /*
8225871d2d7SSujith Manoharan * Discard corrupt descriptors which are marked in
8235871d2d7SSujith Manoharan * ath_get_next_rx_buf().
8245871d2d7SSujith Manoharan */
825723e7113SFelix Fietkau if (discard_current)
826b7b146c9SFelix Fietkau goto corrupt;
827b7b146c9SFelix Fietkau
828b7b146c9SFelix Fietkau sc->rx.discard_next = false;
829f749b946SFelix Fietkau
830d435700fSSujith /*
8313c0efb74SFelix Fietkau * Discard zero-length packets and packets smaller than an ACK
832df5c4150SZefir Kurtisi * which are not PHY_ERROR (short radar pulses have a length of 3)
8335871d2d7SSujith Manoharan */
834df5c4150SZefir Kurtisi is_phyerr = rx_stats->rs_status & ATH9K_RXERR_PHY;
835df5c4150SZefir Kurtisi if (!rx_stats->rs_datalen ||
836df5c4150SZefir Kurtisi (rx_stats->rs_datalen < 10 && !is_phyerr)) {
83772569b7bSArnd Bergmann RX_STAT_INC(sc, rx_len_err);
838b7b146c9SFelix Fietkau goto corrupt;
8395871d2d7SSujith Manoharan }
8405871d2d7SSujith Manoharan
8415871d2d7SSujith Manoharan /*
8425871d2d7SSujith Manoharan * rs_status follows rs_datalen so if rs_datalen is too large
8435871d2d7SSujith Manoharan * we can take a hint that hardware corrupted it, so ignore
8445871d2d7SSujith Manoharan * those frames.
8455871d2d7SSujith Manoharan */
8465871d2d7SSujith Manoharan if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
84772569b7bSArnd Bergmann RX_STAT_INC(sc, rx_len_err);
848b7b146c9SFelix Fietkau goto corrupt;
8495871d2d7SSujith Manoharan }
8505871d2d7SSujith Manoharan
8514a470647SSujith Manoharan /* Only use status info from the last fragment */
8524a470647SSujith Manoharan if (rx_stats->rs_more)
8534a470647SSujith Manoharan return 0;
8544a470647SSujith Manoharan
855b0925595SSujith Manoharan /*
856b0925595SSujith Manoharan * Return immediately if the RX descriptor has been marked
857b0925595SSujith Manoharan * as corrupt based on the various error bits.
858b0925595SSujith Manoharan *
859b0925595SSujith Manoharan * This is different from the other corrupt descriptor
860b0925595SSujith Manoharan * condition handled above.
861b0925595SSujith Manoharan */
862b7b146c9SFelix Fietkau if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
863b7b146c9SFelix Fietkau goto corrupt;
864b0925595SSujith Manoharan
8656f38482eSSujith Manoharan hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
8666f38482eSSujith Manoharan
867e0dd1a96SSujith Manoharan ath9k_process_tsf(rx_stats, rx_status, tsf);
8685e85a32aSSujith Manoharan ath_debug_stat_rx(sc, rx_stats);
869e0dd1a96SSujith Manoharan
8705871d2d7SSujith Manoharan /*
8716b87d71cSSujith Manoharan * Process PHY errors and return so that the packet
8726b87d71cSSujith Manoharan * can be dropped.
8736b87d71cSSujith Manoharan */
8746b87d71cSSujith Manoharan if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
87587fedb97SZefir Kurtisi /*
87687fedb97SZefir Kurtisi * DFS and spectral are mutually exclusive
87787fedb97SZefir Kurtisi *
87887fedb97SZefir Kurtisi * Since some chips use PHYERR_RADAR as indication for both, we
87987fedb97SZefir Kurtisi * need to double check which feature is enabled to prevent
88087fedb97SZefir Kurtisi * feeding spectral or dfs-detector with wrong frames.
88187fedb97SZefir Kurtisi */
88287fedb97SZefir Kurtisi if (hw->conf.radar_enabled) {
88387fedb97SZefir Kurtisi ath9k_dfs_process_phyerr(sc, hdr, rx_stats,
88487fedb97SZefir Kurtisi rx_status->mactime);
88587fedb97SZefir Kurtisi } else if (sc->spec_priv.spectral_mode != SPECTRAL_DISABLED &&
88687fedb97SZefir Kurtisi ath_cmn_process_fft(&sc->spec_priv, hdr, rx_stats,
88787fedb97SZefir Kurtisi rx_status->mactime)) {
88872569b7bSArnd Bergmann RX_STAT_INC(sc, rx_spectral);
88987fedb97SZefir Kurtisi }
890b7b146c9SFelix Fietkau return -EINVAL;
8916b87d71cSSujith Manoharan }
8926b87d71cSSujith Manoharan
8936b87d71cSSujith Manoharan /*
894d435700fSSujith * everything but the rate is checked here, the rate check is done
895d435700fSSujith * separately to avoid doing two lookups for a rate for each frame.
896d435700fSSujith */
897fce34430SSujith Manoharan spin_lock_bh(&sc->chan_lock);
898fce34430SSujith Manoharan if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error,
899fce34430SSujith Manoharan sc->cur_chan->rxfilter)) {
900fce34430SSujith Manoharan spin_unlock_bh(&sc->chan_lock);
901b7b146c9SFelix Fietkau return -EINVAL;
902fce34430SSujith Manoharan }
903fce34430SSujith Manoharan spin_unlock_bh(&sc->chan_lock);
904d435700fSSujith
9051cc47a5bSOleksij Rempel if (ath_is_mybeacon(common, hdr)) {
90672569b7bSArnd Bergmann RX_STAT_INC(sc, rx_beacons);
9071cc47a5bSOleksij Rempel rx_stats->is_mybeacon = true;
9081cc47a5bSOleksij Rempel }
9096f38482eSSujith Manoharan
910ff9a93f2SSujith Manoharan /*
911ff9a93f2SSujith Manoharan * This shouldn't happen, but have a safety check anyway.
912ff9a93f2SSujith Manoharan */
913b7b146c9SFelix Fietkau if (WARN_ON(!ah->curchan))
914b7b146c9SFelix Fietkau return -EINVAL;
915ff9a93f2SSujith Manoharan
91612746036SOleksij Rempel if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
91712746036SOleksij Rempel /*
91812746036SOleksij Rempel * No valid hardware bitrate found -- we should not get here
91912746036SOleksij Rempel * because hardware has already validated this frame as OK.
92012746036SOleksij Rempel */
92112746036SOleksij Rempel ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
92212746036SOleksij Rempel rx_stats->rs_rate);
92372569b7bSArnd Bergmann RX_STAT_INC(sc, rx_rate_err);
924b7b146c9SFelix Fietkau return -EINVAL;
9257c5c73cdSSujith Manoharan }
926d435700fSSujith
92727babf9fSSujith Manoharan if (ath9k_is_chanctx_enabled()) {
92870b06dacSSujith Manoharan if (rx_stats->is_mybeacon)
929a2b28601SSujith Manoharan ath_chanctx_beacon_recv_ev(sc,
93027babf9fSSujith Manoharan ATH_CHANCTX_EVENT_BEACON_RECEIVED);
93127babf9fSSujith Manoharan }
93258b57375SFelix Fietkau
93332efb0ccSOleksij Rempel ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
93474a97755SSujith Manoharan
935ff9a93f2SSujith Manoharan rx_status->band = ah->curchan->chan->band;
936ff9a93f2SSujith Manoharan rx_status->freq = ah->curchan->chan->center_freq;
937d435700fSSujith rx_status->antenna = rx_stats->rs_antenna;
93896d21371SThomas Pedersen rx_status->flag |= RX_FLAG_MACTIME_END;
939d435700fSSujith
940a5525d9cSSujith Manoharan #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
941a5525d9cSSujith Manoharan if (ieee80211_is_data_present(hdr->frame_control) &&
942a5525d9cSSujith Manoharan !ieee80211_is_qos_nullfunc(hdr->frame_control))
943a5525d9cSSujith Manoharan sc->rx.num_pkts++;
944a5525d9cSSujith Manoharan #endif
945a5525d9cSSujith Manoharan
946b7b146c9SFelix Fietkau return 0;
947b7b146c9SFelix Fietkau
948b7b146c9SFelix Fietkau corrupt:
949b7b146c9SFelix Fietkau sc->rx.discard_next = rx_stats->rs_more;
950b7b146c9SFelix Fietkau return -EINVAL;
951d435700fSSujith }
952d435700fSSujith
953c3124df7SSujith Manoharan /*
954c3124df7SSujith Manoharan * Run the LNA combining algorithm only in these cases:
955c3124df7SSujith Manoharan *
956c3124df7SSujith Manoharan * Standalone WLAN cards with both LNA/Antenna diversity
957c3124df7SSujith Manoharan * enabled in the EEPROM.
958c3124df7SSujith Manoharan *
959c3124df7SSujith Manoharan * WLAN+BT cards which are in the supported card list
960c3124df7SSujith Manoharan * in ath_pci_id_table and the user has loaded the
961c3124df7SSujith Manoharan * driver with "bt_ant_diversity" set to true.
962c3124df7SSujith Manoharan */
ath9k_antenna_check(struct ath_softc * sc,struct ath_rx_status * rs)963c3124df7SSujith Manoharan static void ath9k_antenna_check(struct ath_softc *sc,
964c3124df7SSujith Manoharan struct ath_rx_status *rs)
965c3124df7SSujith Manoharan {
966c3124df7SSujith Manoharan struct ath_hw *ah = sc->sc_ah;
967c3124df7SSujith Manoharan struct ath9k_hw_capabilities *pCap = &ah->caps;
968c3124df7SSujith Manoharan struct ath_common *common = ath9k_hw_common(ah);
969c3124df7SSujith Manoharan
970c3124df7SSujith Manoharan if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
971c3124df7SSujith Manoharan return;
972c3124df7SSujith Manoharan
973c3124df7SSujith Manoharan /*
974c3124df7SSujith Manoharan * Change the default rx antenna if rx diversity
975c3124df7SSujith Manoharan * chooses the other antenna 3 times in a row.
976c3124df7SSujith Manoharan */
977c3124df7SSujith Manoharan if (sc->rx.defant != rs->rs_antenna) {
978c3124df7SSujith Manoharan if (++sc->rx.rxotherant >= 3)
979c3124df7SSujith Manoharan ath_setdefantenna(sc, rs->rs_antenna);
980c3124df7SSujith Manoharan } else {
981c3124df7SSujith Manoharan sc->rx.rxotherant = 0;
982c3124df7SSujith Manoharan }
983c3124df7SSujith Manoharan
984c3124df7SSujith Manoharan if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
985c3124df7SSujith Manoharan if (common->bt_ant_diversity)
986c3124df7SSujith Manoharan ath_ant_comb_scan(sc, rs);
987c3124df7SSujith Manoharan } else {
988c3124df7SSujith Manoharan ath_ant_comb_scan(sc, rs);
989c3124df7SSujith Manoharan }
990c3124df7SSujith Manoharan }
991c3124df7SSujith Manoharan
ath9k_apply_ampdu_details(struct ath_softc * sc,struct ath_rx_status * rs,struct ieee80211_rx_status * rxs)99221fbbca3SChristian Lamparter static void ath9k_apply_ampdu_details(struct ath_softc *sc,
99321fbbca3SChristian Lamparter struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
99421fbbca3SChristian Lamparter {
99521fbbca3SChristian Lamparter if (rs->rs_isaggr) {
99621fbbca3SChristian Lamparter rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
99721fbbca3SChristian Lamparter
99821fbbca3SChristian Lamparter rxs->ampdu_reference = sc->rx.ampdu_ref;
99921fbbca3SChristian Lamparter
100021fbbca3SChristian Lamparter if (!rs->rs_moreaggr) {
100121fbbca3SChristian Lamparter rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
100221fbbca3SChristian Lamparter sc->rx.ampdu_ref++;
100321fbbca3SChristian Lamparter }
100421fbbca3SChristian Lamparter
100521fbbca3SChristian Lamparter if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
100621fbbca3SChristian Lamparter rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
100721fbbca3SChristian Lamparter }
100821fbbca3SChristian Lamparter }
100921fbbca3SChristian Lamparter
ath_rx_count_airtime(struct ath_softc * sc,struct ath_rx_status * rs,struct sk_buff * skb)101063fefa05SToke Høiland-Jørgensen static void ath_rx_count_airtime(struct ath_softc *sc,
101163fefa05SToke Høiland-Jørgensen struct ath_rx_status *rs,
101263fefa05SToke Høiland-Jørgensen struct sk_buff *skb)
101363fefa05SToke Høiland-Jørgensen {
101463fefa05SToke Høiland-Jørgensen struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
101563fefa05SToke Høiland-Jørgensen struct ath_hw *ah = sc->sc_ah;
101663fefa05SToke Høiland-Jørgensen struct ath_common *common = ath9k_hw_common(ah);
101763fefa05SToke Høiland-Jørgensen struct ieee80211_sta *sta;
101863fefa05SToke Høiland-Jørgensen struct ieee80211_rx_status *rxs;
101963fefa05SToke Høiland-Jørgensen const struct ieee80211_rate *rate;
102063fefa05SToke Høiland-Jørgensen bool is_sgi, is_40, is_sp;
102163fefa05SToke Høiland-Jørgensen int phy;
102263fefa05SToke Høiland-Jørgensen u16 len = rs->rs_datalen;
102363fefa05SToke Høiland-Jørgensen u32 airtime = 0;
102403af21d6SYueHaibing u8 tidno;
102563fefa05SToke Høiland-Jørgensen
102663fefa05SToke Høiland-Jørgensen if (!ieee80211_is_data(hdr->frame_control))
102763fefa05SToke Høiland-Jørgensen return;
102863fefa05SToke Høiland-Jørgensen
102963fefa05SToke Høiland-Jørgensen rcu_read_lock();
103063fefa05SToke Høiland-Jørgensen
103163fefa05SToke Høiland-Jørgensen sta = ieee80211_find_sta_by_ifaddr(sc->hw, hdr->addr2, NULL);
103263fefa05SToke Høiland-Jørgensen if (!sta)
103363fefa05SToke Høiland-Jørgensen goto exit;
103463fefa05SToke Høiland-Jørgensen tidno = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
103563fefa05SToke Høiland-Jørgensen
103663fefa05SToke Høiland-Jørgensen rxs = IEEE80211_SKB_RXCB(skb);
103763fefa05SToke Høiland-Jørgensen
10387fdd69c5SJohannes Berg is_sgi = !!(rxs->enc_flags & RX_ENC_FLAG_SHORT_GI);
1039da6a4352SJohannes Berg is_40 = !!(rxs->bw == RATE_INFO_BW_40);
10407fdd69c5SJohannes Berg is_sp = !!(rxs->enc_flags & RX_ENC_FLAG_SHORTPRE);
104163fefa05SToke Høiland-Jørgensen
1042da6a4352SJohannes Berg if (!!(rxs->encoding == RX_ENC_HT)) {
104363fefa05SToke Høiland-Jørgensen /* MCS rates */
104463fefa05SToke Høiland-Jørgensen
104563fefa05SToke Høiland-Jørgensen airtime += ath_pkt_duration(sc, rxs->rate_idx, len,
104663fefa05SToke Høiland-Jørgensen is_40, is_sgi, is_sp);
104763fefa05SToke Høiland-Jørgensen } else {
104863fefa05SToke Høiland-Jørgensen
104963fefa05SToke Høiland-Jørgensen phy = IS_CCK_RATE(rs->rs_rate) ? WLAN_RC_PHY_CCK : WLAN_RC_PHY_OFDM;
105063fefa05SToke Høiland-Jørgensen rate = &common->sbands[rxs->band].bitrates[rxs->rate_idx];
105163fefa05SToke Høiland-Jørgensen airtime += ath9k_hw_computetxtime(ah, phy, rate->bitrate * 100,
105263fefa05SToke Høiland-Jørgensen len, rxs->rate_idx, is_sp);
105363fefa05SToke Høiland-Jørgensen }
105463fefa05SToke Høiland-Jørgensen
105589cea749SToke Høiland-Jørgensen ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
105663fefa05SToke Høiland-Jørgensen exit:
105763fefa05SToke Høiland-Jørgensen rcu_read_unlock();
105863fefa05SToke Høiland-Jørgensen }
105963fefa05SToke Høiland-Jørgensen
ath_rx_tasklet(struct ath_softc * sc,int flush,bool hp)1060b5c80475SFelix Fietkau int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1061b5c80475SFelix Fietkau {
10621a04d59dSFelix Fietkau struct ath_rxbuf *bf;
10630d95521eSFelix Fietkau struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1064b5c80475SFelix Fietkau struct ieee80211_rx_status *rxs;
1065b5c80475SFelix Fietkau struct ath_hw *ah = sc->sc_ah;
1066b5c80475SFelix Fietkau struct ath_common *common = ath9k_hw_common(ah);
10677545daf4SFelix Fietkau struct ieee80211_hw *hw = sc->hw;
1068b5c80475SFelix Fietkau int retval;
1069b5c80475SFelix Fietkau struct ath_rx_status rs;
1070b5c80475SFelix Fietkau enum ath9k_rx_qtype qtype;
1071b5c80475SFelix Fietkau bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1072b5c80475SFelix Fietkau int dma_type;
1073a6d2055bSFelix Fietkau u64 tsf = 0;
10748ab2cd09SLuis R. Rodriguez unsigned long flags;
10752e1cd495SFelix Fietkau dma_addr_t new_buf_addr;
1076c82552c5STim Harvey unsigned int budget = 512;
1077982e0395SLorenzo Bianconi struct ieee80211_hdr *hdr;
1078b5c80475SFelix Fietkau
1079b5c80475SFelix Fietkau if (edma)
1080b5c80475SFelix Fietkau dma_type = DMA_BIDIRECTIONAL;
108156824223SMing Lei else
108256824223SMing Lei dma_type = DMA_FROM_DEVICE;
1083b5c80475SFelix Fietkau
1084b5c80475SFelix Fietkau qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1085b5c80475SFelix Fietkau
1086a6d2055bSFelix Fietkau tsf = ath9k_hw_gettsf64(ah);
1087a6d2055bSFelix Fietkau
1088b5c80475SFelix Fietkau do {
1089e1352fdeSLorenzo Bianconi bool decrypt_error = false;
1090b5c80475SFelix Fietkau
1091b5c80475SFelix Fietkau memset(&rs, 0, sizeof(rs));
1092b5c80475SFelix Fietkau if (edma)
1093b5c80475SFelix Fietkau bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1094b5c80475SFelix Fietkau else
1095b5c80475SFelix Fietkau bf = ath_get_next_rx_buf(sc, &rs);
1096b5c80475SFelix Fietkau
1097b5c80475SFelix Fietkau if (!bf)
1098b5c80475SFelix Fietkau break;
1099b5c80475SFelix Fietkau
1100b5c80475SFelix Fietkau skb = bf->bf_mpdu;
1101b5c80475SFelix Fietkau if (!skb)
1102b5c80475SFelix Fietkau continue;
1103b5c80475SFelix Fietkau
11040d95521eSFelix Fietkau /*
11050d95521eSFelix Fietkau * Take frame header from the first fragment and RX status from
11060d95521eSFelix Fietkau * the last one.
11070d95521eSFelix Fietkau */
11080d95521eSFelix Fietkau if (sc->rx.frag)
11090d95521eSFelix Fietkau hdr_skb = sc->rx.frag;
11100d95521eSFelix Fietkau else
11110d95521eSFelix Fietkau hdr_skb = skb;
11120d95521eSFelix Fietkau
1113f6307ddaSSujith Manoharan rxs = IEEE80211_SKB_RXCB(hdr_skb);
1114ffb1c56aSAshok Nagarajan memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1115ffb1c56aSAshok Nagarajan
11166f38482eSSujith Manoharan retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
1117e0dd1a96SSujith Manoharan &decrypt_error, tsf);
111883c76570SZefir Kurtisi if (retval)
111983c76570SZefir Kurtisi goto requeue_drop_frag;
112083c76570SZefir Kurtisi
1121203c4805SLuis R. Rodriguez /* Ensure we always have an skb to requeue once we are done
1122203c4805SLuis R. Rodriguez * processing the current buffer's skb */
1123cc861f74SLuis R. Rodriguez requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1124203c4805SLuis R. Rodriguez
1125203c4805SLuis R. Rodriguez /* If there is no memory we ignore the current RX'd frame,
1126203c4805SLuis R. Rodriguez * tell hardware it can give us a new frame using the old
1127203c4805SLuis R. Rodriguez * skb and put it at the tail of the sc->rx.rxbuf list for
1128203c4805SLuis R. Rodriguez * processing. */
112915072189SBen Greear if (!requeue_skb) {
113072569b7bSArnd Bergmann RX_STAT_INC(sc, rx_oom_err);
11310d95521eSFelix Fietkau goto requeue_drop_frag;
113215072189SBen Greear }
1133203c4805SLuis R. Rodriguez
11342e1cd495SFelix Fietkau /* We will now give hardware our shiny new allocated skb */
11352e1cd495SFelix Fietkau new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
11362e1cd495SFelix Fietkau common->rx_bufsize, dma_type);
11372e1cd495SFelix Fietkau if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
11382e1cd495SFelix Fietkau dev_kfree_skb_any(requeue_skb);
11392e1cd495SFelix Fietkau goto requeue_drop_frag;
11402e1cd495SFelix Fietkau }
11412e1cd495SFelix Fietkau
1142203c4805SLuis R. Rodriguez /* Unmap the frame */
1143203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr,
11442e1cd495SFelix Fietkau common->rx_bufsize, dma_type);
1145203c4805SLuis R. Rodriguez
1146176f0e84SSujith Manoharan bf->bf_mpdu = requeue_skb;
1147176f0e84SSujith Manoharan bf->bf_buf_addr = new_buf_addr;
1148176f0e84SSujith Manoharan
1149b5c80475SFelix Fietkau skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1150b5c80475SFelix Fietkau if (ah->caps.rx_status_len)
1151b5c80475SFelix Fietkau skb_pull(skb, ah->caps.rx_status_len);
1152203c4805SLuis R. Rodriguez
11530d95521eSFelix Fietkau if (!rs.rs_more)
11545a078fcbSOleksij Rempel ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
1155c9b14170SLuis R. Rodriguez rxs, decrypt_error);
1156203c4805SLuis R. Rodriguez
11570d95521eSFelix Fietkau if (rs.rs_more) {
115872569b7bSArnd Bergmann RX_STAT_INC(sc, rx_frags);
11590d95521eSFelix Fietkau /*
11600d95521eSFelix Fietkau * rs_more indicates chained descriptors which can be
11610d95521eSFelix Fietkau * used to link buffers together for a sort of
11620d95521eSFelix Fietkau * scatter-gather operation.
11630d95521eSFelix Fietkau */
11640d95521eSFelix Fietkau if (sc->rx.frag) {
11650d95521eSFelix Fietkau /* too many fragments - cannot handle frame */
11660d95521eSFelix Fietkau dev_kfree_skb_any(sc->rx.frag);
11670d95521eSFelix Fietkau dev_kfree_skb_any(skb);
116872569b7bSArnd Bergmann RX_STAT_INC(sc, rx_too_many_frags_err);
11690d95521eSFelix Fietkau skb = NULL;
11700d95521eSFelix Fietkau }
11710d95521eSFelix Fietkau sc->rx.frag = skb;
11720d95521eSFelix Fietkau goto requeue;
11730d95521eSFelix Fietkau }
11740d95521eSFelix Fietkau
11750d95521eSFelix Fietkau if (sc->rx.frag) {
11760d95521eSFelix Fietkau int space = skb->len - skb_tailroom(hdr_skb);
11770d95521eSFelix Fietkau
11780d95521eSFelix Fietkau if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
11790d95521eSFelix Fietkau dev_kfree_skb(skb);
118072569b7bSArnd Bergmann RX_STAT_INC(sc, rx_oom_err);
11810d95521eSFelix Fietkau goto requeue_drop_frag;
11820d95521eSFelix Fietkau }
11830d95521eSFelix Fietkau
1184b5447ff9SEric Dumazet sc->rx.frag = NULL;
1185b5447ff9SEric Dumazet
11860d95521eSFelix Fietkau skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
11870d95521eSFelix Fietkau skb->len);
11880d95521eSFelix Fietkau dev_kfree_skb_any(skb);
11890d95521eSFelix Fietkau skb = hdr_skb;
11900d95521eSFelix Fietkau }
11910d95521eSFelix Fietkau
119266760eacSFelix Fietkau if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
119366760eacSFelix Fietkau skb_trim(skb, skb->len - 8);
119466760eacSFelix Fietkau
11958ab2cd09SLuis R. Rodriguez spin_lock_irqsave(&sc->sc_pm_lock, flags);
1196aaef24b4SMohammed Shafi Shajakhan if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
11971b04b930SSujith PS_WAIT_FOR_CAB |
1198aaef24b4SMohammed Shafi Shajakhan PS_WAIT_FOR_PSPOLL_DATA)) ||
1199cedc7e3dSMohammed Shafi Shajakhan ath9k_check_auto_sleep(sc))
1200f73c604cSRajkumar Manoharan ath_rx_ps(sc, skb, rs.is_mybeacon);
12018ab2cd09SLuis R. Rodriguez spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1202cc65965cSJouni Malinen
1203c3124df7SSujith Manoharan ath9k_antenna_check(sc, &rs);
120421fbbca3SChristian Lamparter ath9k_apply_ampdu_details(sc, &rs, rxs);
1205350e2dcbSSujith Manoharan ath_debug_rate_stats(sc, &rs, skb);
120663fefa05SToke Høiland-Jørgensen ath_rx_count_airtime(sc, &rs, skb);
120721fbbca3SChristian Lamparter
1208982e0395SLorenzo Bianconi hdr = (struct ieee80211_hdr *)skb->data;
1209982e0395SLorenzo Bianconi if (ieee80211_is_ack(hdr->frame_control))
1210982e0395SLorenzo Bianconi ath_dynack_sample_ack_ts(sc->sc_ah, skb, rs.rs_tstamp);
1211982e0395SLorenzo Bianconi
12127545daf4SFelix Fietkau ieee80211_rx(hw, skb);
1213cc65965cSJouni Malinen
12140d95521eSFelix Fietkau requeue_drop_frag:
12150d95521eSFelix Fietkau if (sc->rx.frag) {
12160d95521eSFelix Fietkau dev_kfree_skb_any(sc->rx.frag);
12170d95521eSFelix Fietkau sc->rx.frag = NULL;
12180d95521eSFelix Fietkau }
1219203c4805SLuis R. Rodriguez requeue:
1220b5c80475SFelix Fietkau list_add_tail(&bf->list, &sc->rx.rxbuf);
1221a3dc48e8SFelix Fietkau
12227dd74f5fSFelix Fietkau if (!edma) {
12237dd74f5fSFelix Fietkau ath_rx_buf_relink(sc, bf, flush);
12243a758134STim Harvey if (!flush)
122595294973SFelix Fietkau ath9k_hw_rxena(ah);
12267dd74f5fSFelix Fietkau } else if (!flush) {
12277dd74f5fSFelix Fietkau ath_rx_edma_buf_link(sc, qtype);
1228b5c80475SFelix Fietkau }
1229c82552c5STim Harvey
1230c82552c5STim Harvey if (!budget--)
1231c82552c5STim Harvey break;
1232203c4805SLuis R. Rodriguez } while (1);
1233203c4805SLuis R. Rodriguez
123429ab0b36SRajkumar Manoharan if (!(ah->imask & ATH9K_INT_RXEOL)) {
123529ab0b36SRajkumar Manoharan ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
123672d874c6SFelix Fietkau ath9k_hw_set_interrupts(ah);
123729ab0b36SRajkumar Manoharan }
123829ab0b36SRajkumar Manoharan
1239203c4805SLuis R. Rodriguez return 0;
1240203c4805SLuis R. Rodriguez }
1241