Commit | Line | Data |
---|---|---|
f078f209 | 1 | /* |
cee075a2 | 2 | * Copyright (c) 2008-2009 Atheros Communications Inc. |
f078f209 LR |
3 | * |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
394cf0a1 | 17 | #include "ath9k.h" |
b622a720 | 18 | #include "ar9003_mac.h" |
f078f209 | 19 | |
b5c80475 FF |
20 | #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb)) |
21 | ||
102885a5 VT |
22 | static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta, |
23 | int mindelta, int main_rssi_avg, | |
24 | int alt_rssi_avg, int pkt_count) | |
25 | { | |
26 | return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) && | |
27 | (alt_rssi_avg > main_rssi_avg + maxdelta)) || | |
28 | (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50); | |
29 | } | |
30 | ||
ededf1f8 VT |
31 | static inline bool ath9k_check_auto_sleep(struct ath_softc *sc) |
32 | { | |
33 | return sc->ps_enabled && | |
34 | (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP); | |
35 | } | |
36 | ||
bce048d7 JM |
37 | static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc, |
38 | struct ieee80211_hdr *hdr) | |
39 | { | |
c52f33d0 JM |
40 | struct ieee80211_hw *hw = sc->pri_wiphy->hw; |
41 | int i; | |
42 | ||
43 | spin_lock_bh(&sc->wiphy_lock); | |
44 | for (i = 0; i < sc->num_sec_wiphy; i++) { | |
45 | struct ath_wiphy *aphy = sc->sec_wiphy[i]; | |
46 | if (aphy == NULL) | |
47 | continue; | |
48 | if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr) | |
49 | == 0) { | |
50 | hw = aphy->hw; | |
51 | break; | |
52 | } | |
53 | } | |
54 | spin_unlock_bh(&sc->wiphy_lock); | |
55 | return hw; | |
bce048d7 JM |
56 | } |
57 | ||
f078f209 LR |
58 | /* |
59 | * Setup and link descriptors. | |
60 | * | |
61 | * 11N: we can no longer afford to self link the last descriptor. | |
62 | * MAC acknowledges BA status as long as it copies frames to host | |
63 | * buffer (or rx fifo). This can incorrectly acknowledge packets | |
64 | * to a sender if last desc is self-linked. | |
f078f209 | 65 | */ |
f078f209 LR |
66 | static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf) |
67 | { | |
cbe61d8a | 68 | struct ath_hw *ah = sc->sc_ah; |
cc861f74 | 69 | struct ath_common *common = ath9k_hw_common(ah); |
f078f209 LR |
70 | struct ath_desc *ds; |
71 | struct sk_buff *skb; | |
72 | ||
73 | ATH_RXBUF_RESET(bf); | |
74 | ||
75 | ds = bf->bf_desc; | |
be0418ad | 76 | ds->ds_link = 0; /* link to null */ |
f078f209 LR |
77 | ds->ds_data = bf->bf_buf_addr; |
78 | ||
be0418ad | 79 | /* virtual addr of the beginning of the buffer. */ |
f078f209 | 80 | skb = bf->bf_mpdu; |
9680e8a3 | 81 | BUG_ON(skb == NULL); |
f078f209 LR |
82 | ds->ds_vdata = skb->data; |
83 | ||
cc861f74 LR |
84 | /* |
85 | * setup rx descriptors. The rx_bufsize here tells the hardware | |
b4b6cda2 | 86 | * how much data it can DMA to us and that we are prepared |
cc861f74 LR |
87 | * to process |
88 | */ | |
b77f483f | 89 | ath9k_hw_setuprxdesc(ah, ds, |
cc861f74 | 90 | common->rx_bufsize, |
f078f209 LR |
91 | 0); |
92 | ||
b77f483f | 93 | if (sc->rx.rxlink == NULL) |
f078f209 LR |
94 | ath9k_hw_putrxbuf(ah, bf->bf_daddr); |
95 | else | |
b77f483f | 96 | *sc->rx.rxlink = bf->bf_daddr; |
f078f209 | 97 | |
b77f483f | 98 | sc->rx.rxlink = &ds->ds_link; |
f078f209 LR |
99 | ath9k_hw_rxena(ah); |
100 | } | |
101 | ||
ff37e337 S |
102 | static void ath_setdefantenna(struct ath_softc *sc, u32 antenna) |
103 | { | |
104 | /* XXX block beacon interrupts */ | |
105 | ath9k_hw_setantenna(sc->sc_ah, antenna); | |
b77f483f S |
106 | sc->rx.defant = antenna; |
107 | sc->rx.rxotherant = 0; | |
ff37e337 S |
108 | } |
109 | ||
f078f209 LR |
110 | static void ath_opmode_init(struct ath_softc *sc) |
111 | { | |
cbe61d8a | 112 | struct ath_hw *ah = sc->sc_ah; |
1510718d LR |
113 | struct ath_common *common = ath9k_hw_common(ah); |
114 | ||
f078f209 LR |
115 | u32 rfilt, mfilt[2]; |
116 | ||
117 | /* configure rx filter */ | |
118 | rfilt = ath_calcrxfilter(sc); | |
119 | ath9k_hw_setrxfilter(ah, rfilt); | |
120 | ||
121 | /* configure bssid mask */ | |
364734fa | 122 | ath_hw_setbssidmask(common); |
f078f209 LR |
123 | |
124 | /* configure operational mode */ | |
125 | ath9k_hw_setopmode(ah); | |
126 | ||
f078f209 LR |
127 | /* calculate and install multicast filter */ |
128 | mfilt[0] = mfilt[1] = ~0; | |
f078f209 | 129 | ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]); |
f078f209 LR |
130 | } |
131 | ||
b5c80475 FF |
132 | static bool ath_rx_edma_buf_link(struct ath_softc *sc, |
133 | enum ath9k_rx_qtype qtype) | |
f078f209 | 134 | { |
b5c80475 FF |
135 | struct ath_hw *ah = sc->sc_ah; |
136 | struct ath_rx_edma *rx_edma; | |
f078f209 LR |
137 | struct sk_buff *skb; |
138 | struct ath_buf *bf; | |
f078f209 | 139 | |
b5c80475 FF |
140 | rx_edma = &sc->rx.rx_edma[qtype]; |
141 | if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize) | |
142 | return false; | |
f078f209 | 143 | |
b5c80475 FF |
144 | bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); |
145 | list_del_init(&bf->list); | |
f078f209 | 146 | |
b5c80475 FF |
147 | skb = bf->bf_mpdu; |
148 | ||
149 | ATH_RXBUF_RESET(bf); | |
150 | memset(skb->data, 0, ah->caps.rx_status_len); | |
151 | dma_sync_single_for_device(sc->dev, bf->bf_buf_addr, | |
152 | ah->caps.rx_status_len, DMA_TO_DEVICE); | |
f078f209 | 153 | |
b5c80475 FF |
154 | SKB_CB_ATHBUF(skb) = bf; |
155 | ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype); | |
156 | skb_queue_tail(&rx_edma->rx_fifo, skb); | |
f078f209 | 157 | |
b5c80475 FF |
158 | return true; |
159 | } | |
160 | ||
161 | static void ath_rx_addbuffer_edma(struct ath_softc *sc, | |
162 | enum ath9k_rx_qtype qtype, int size) | |
163 | { | |
b5c80475 FF |
164 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
165 | u32 nbuf = 0; | |
166 | ||
b5c80475 | 167 | if (list_empty(&sc->rx.rxbuf)) { |
226afe68 | 168 | ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n"); |
b5c80475 | 169 | return; |
797fe5cb | 170 | } |
f078f209 | 171 | |
b5c80475 FF |
172 | while (!list_empty(&sc->rx.rxbuf)) { |
173 | nbuf++; | |
174 | ||
175 | if (!ath_rx_edma_buf_link(sc, qtype)) | |
176 | break; | |
177 | ||
178 | if (nbuf >= size) | |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | static void ath_rx_remove_buffer(struct ath_softc *sc, | |
184 | enum ath9k_rx_qtype qtype) | |
185 | { | |
186 | struct ath_buf *bf; | |
187 | struct ath_rx_edma *rx_edma; | |
188 | struct sk_buff *skb; | |
189 | ||
190 | rx_edma = &sc->rx.rx_edma[qtype]; | |
191 | ||
192 | while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) { | |
193 | bf = SKB_CB_ATHBUF(skb); | |
194 | BUG_ON(!bf); | |
195 | list_add_tail(&bf->list, &sc->rx.rxbuf); | |
196 | } | |
197 | } | |
198 | ||
199 | static void ath_rx_edma_cleanup(struct ath_softc *sc) | |
200 | { | |
201 | struct ath_buf *bf; | |
202 | ||
203 | ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP); | |
204 | ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP); | |
205 | ||
797fe5cb | 206 | list_for_each_entry(bf, &sc->rx.rxbuf, list) { |
b5c80475 FF |
207 | if (bf->bf_mpdu) |
208 | dev_kfree_skb_any(bf->bf_mpdu); | |
209 | } | |
210 | ||
211 | INIT_LIST_HEAD(&sc->rx.rxbuf); | |
212 | ||
213 | kfree(sc->rx.rx_bufptr); | |
214 | sc->rx.rx_bufptr = NULL; | |
215 | } | |
216 | ||
217 | static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size) | |
218 | { | |
219 | skb_queue_head_init(&rx_edma->rx_fifo); | |
220 | skb_queue_head_init(&rx_edma->rx_buffers); | |
221 | rx_edma->rx_fifo_hwsize = size; | |
222 | } | |
223 | ||
224 | static int ath_rx_edma_init(struct ath_softc *sc, int nbufs) | |
225 | { | |
226 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
227 | struct ath_hw *ah = sc->sc_ah; | |
228 | struct sk_buff *skb; | |
229 | struct ath_buf *bf; | |
230 | int error = 0, i; | |
231 | u32 size; | |
232 | ||
233 | ||
234 | common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN + | |
235 | ah->caps.rx_status_len, | |
236 | min(common->cachelsz, (u16)64)); | |
237 | ||
238 | ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize - | |
239 | ah->caps.rx_status_len); | |
240 | ||
241 | ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP], | |
242 | ah->caps.rx_lp_qdepth); | |
243 | ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP], | |
244 | ah->caps.rx_hp_qdepth); | |
245 | ||
246 | size = sizeof(struct ath_buf) * nbufs; | |
247 | bf = kzalloc(size, GFP_KERNEL); | |
248 | if (!bf) | |
249 | return -ENOMEM; | |
250 | ||
251 | INIT_LIST_HEAD(&sc->rx.rxbuf); | |
252 | sc->rx.rx_bufptr = bf; | |
253 | ||
254 | for (i = 0; i < nbufs; i++, bf++) { | |
cc861f74 | 255 | skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL); |
b5c80475 | 256 | if (!skb) { |
797fe5cb | 257 | error = -ENOMEM; |
b5c80475 | 258 | goto rx_init_fail; |
f078f209 | 259 | } |
f078f209 | 260 | |
b5c80475 | 261 | memset(skb->data, 0, common->rx_bufsize); |
797fe5cb | 262 | bf->bf_mpdu = skb; |
b5c80475 | 263 | |
797fe5cb | 264 | bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, |
cc861f74 | 265 | common->rx_bufsize, |
b5c80475 | 266 | DMA_BIDIRECTIONAL); |
797fe5cb | 267 | if (unlikely(dma_mapping_error(sc->dev, |
b5c80475 FF |
268 | bf->bf_buf_addr))) { |
269 | dev_kfree_skb_any(skb); | |
270 | bf->bf_mpdu = NULL; | |
6cf9e995 | 271 | bf->bf_buf_addr = 0; |
3800276a | 272 | ath_err(common, |
b5c80475 FF |
273 | "dma_mapping_error() on RX init\n"); |
274 | error = -ENOMEM; | |
275 | goto rx_init_fail; | |
276 | } | |
277 | ||
278 | list_add_tail(&bf->list, &sc->rx.rxbuf); | |
279 | } | |
280 | ||
281 | return 0; | |
282 | ||
283 | rx_init_fail: | |
284 | ath_rx_edma_cleanup(sc); | |
285 | return error; | |
286 | } | |
287 | ||
288 | static void ath_edma_start_recv(struct ath_softc *sc) | |
289 | { | |
290 | spin_lock_bh(&sc->rx.rxbuflock); | |
291 | ||
292 | ath9k_hw_rxena(sc->sc_ah); | |
293 | ||
294 | ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP, | |
295 | sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize); | |
296 | ||
297 | ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP, | |
298 | sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize); | |
299 | ||
b5c80475 FF |
300 | ath_opmode_init(sc); |
301 | ||
48a6a468 | 302 | ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL)); |
7583c550 LR |
303 | |
304 | spin_unlock_bh(&sc->rx.rxbuflock); | |
b5c80475 FF |
305 | } |
306 | ||
307 | static void ath_edma_stop_recv(struct ath_softc *sc) | |
308 | { | |
b5c80475 FF |
309 | ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP); |
310 | ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP); | |
b5c80475 FF |
311 | } |
312 | ||
313 | int ath_rx_init(struct ath_softc *sc, int nbufs) | |
314 | { | |
315 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
316 | struct sk_buff *skb; | |
317 | struct ath_buf *bf; | |
318 | int error = 0; | |
319 | ||
4bdd1e97 | 320 | spin_lock_init(&sc->sc_pcu_lock); |
b5c80475 FF |
321 | sc->sc_flags &= ~SC_OP_RXFLUSH; |
322 | spin_lock_init(&sc->rx.rxbuflock); | |
323 | ||
324 | if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) { | |
325 | return ath_rx_edma_init(sc, nbufs); | |
326 | } else { | |
327 | common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN, | |
328 | min(common->cachelsz, (u16)64)); | |
329 | ||
226afe68 JP |
330 | ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n", |
331 | common->cachelsz, common->rx_bufsize); | |
b5c80475 FF |
332 | |
333 | /* Initialize rx descriptors */ | |
334 | ||
335 | error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf, | |
4adfcded | 336 | "rx", nbufs, 1, 0); |
b5c80475 | 337 | if (error != 0) { |
3800276a JP |
338 | ath_err(common, |
339 | "failed to allocate rx descriptors: %d\n", | |
340 | error); | |
797fe5cb S |
341 | goto err; |
342 | } | |
b5c80475 FF |
343 | |
344 | list_for_each_entry(bf, &sc->rx.rxbuf, list) { | |
345 | skb = ath_rxbuf_alloc(common, common->rx_bufsize, | |
346 | GFP_KERNEL); | |
347 | if (skb == NULL) { | |
348 | error = -ENOMEM; | |
349 | goto err; | |
350 | } | |
351 | ||
352 | bf->bf_mpdu = skb; | |
353 | bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, | |
354 | common->rx_bufsize, | |
355 | DMA_FROM_DEVICE); | |
356 | if (unlikely(dma_mapping_error(sc->dev, | |
357 | bf->bf_buf_addr))) { | |
358 | dev_kfree_skb_any(skb); | |
359 | bf->bf_mpdu = NULL; | |
6cf9e995 | 360 | bf->bf_buf_addr = 0; |
3800276a JP |
361 | ath_err(common, |
362 | "dma_mapping_error() on RX init\n"); | |
b5c80475 FF |
363 | error = -ENOMEM; |
364 | goto err; | |
365 | } | |
b5c80475 FF |
366 | } |
367 | sc->rx.rxlink = NULL; | |
797fe5cb | 368 | } |
f078f209 | 369 | |
797fe5cb | 370 | err: |
f078f209 LR |
371 | if (error) |
372 | ath_rx_cleanup(sc); | |
373 | ||
374 | return error; | |
375 | } | |
376 | ||
f078f209 LR |
377 | void ath_rx_cleanup(struct ath_softc *sc) |
378 | { | |
cc861f74 LR |
379 | struct ath_hw *ah = sc->sc_ah; |
380 | struct ath_common *common = ath9k_hw_common(ah); | |
f078f209 LR |
381 | struct sk_buff *skb; |
382 | struct ath_buf *bf; | |
383 | ||
b5c80475 FF |
384 | if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) { |
385 | ath_rx_edma_cleanup(sc); | |
386 | return; | |
387 | } else { | |
388 | list_for_each_entry(bf, &sc->rx.rxbuf, list) { | |
389 | skb = bf->bf_mpdu; | |
390 | if (skb) { | |
391 | dma_unmap_single(sc->dev, bf->bf_buf_addr, | |
392 | common->rx_bufsize, | |
393 | DMA_FROM_DEVICE); | |
394 | dev_kfree_skb(skb); | |
6cf9e995 BG |
395 | bf->bf_buf_addr = 0; |
396 | bf->bf_mpdu = NULL; | |
b5c80475 | 397 | } |
051b9191 | 398 | } |
f078f209 | 399 | |
b5c80475 FF |
400 | if (sc->rx.rxdma.dd_desc_len != 0) |
401 | ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf); | |
402 | } | |
f078f209 LR |
403 | } |
404 | ||
405 | /* | |
406 | * Calculate the receive filter according to the | |
407 | * operating mode and state: | |
408 | * | |
409 | * o always accept unicast, broadcast, and multicast traffic | |
410 | * o maintain current state of phy error reception (the hal | |
411 | * may enable phy error frames for noise immunity work) | |
412 | * o probe request frames are accepted only when operating in | |
413 | * hostap, adhoc, or monitor modes | |
414 | * o enable promiscuous mode according to the interface state | |
415 | * o accept beacons: | |
416 | * - when operating in adhoc mode so the 802.11 layer creates | |
417 | * node table entries for peers, | |
418 | * - when operating in station mode for collecting rssi data when | |
419 | * the station is otherwise quiet, or | |
420 | * - when operating as a repeater so we see repeater-sta beacons | |
421 | * - when scanning | |
422 | */ | |
423 | ||
424 | u32 ath_calcrxfilter(struct ath_softc *sc) | |
425 | { | |
426 | #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR) | |
7dcfdcd9 | 427 | |
f078f209 LR |
428 | u32 rfilt; |
429 | ||
430 | rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE) | |
431 | | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST | |
432 | | ATH9K_RX_FILTER_MCAST; | |
433 | ||
9c1d8e4a | 434 | if (sc->rx.rxfilter & FIF_PROBE_REQ) |
f078f209 LR |
435 | rfilt |= ATH9K_RX_FILTER_PROBEREQ; |
436 | ||
217ba9da JM |
437 | /* |
438 | * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station | |
439 | * mode interface or when in monitor mode. AP mode does not need this | |
440 | * since it receives all in-BSS frames anyway. | |
441 | */ | |
2660b81a | 442 | if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) && |
b77f483f | 443 | (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) || |
5f841b41 | 444 | (sc->sc_ah->is_monitoring)) |
f078f209 | 445 | rfilt |= ATH9K_RX_FILTER_PROM; |
f078f209 | 446 | |
d42c6b71 S |
447 | if (sc->rx.rxfilter & FIF_CONTROL) |
448 | rfilt |= ATH9K_RX_FILTER_CONTROL; | |
449 | ||
dbaaa147 | 450 | if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) && |
cfda6695 | 451 | (sc->nvifs <= 1) && |
dbaaa147 VT |
452 | !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)) |
453 | rfilt |= ATH9K_RX_FILTER_MYBEACON; | |
454 | else | |
f078f209 LR |
455 | rfilt |= ATH9K_RX_FILTER_BEACON; |
456 | ||
7a37081e | 457 | if ((AR_SREV_9280_20_OR_LATER(sc->sc_ah) || |
e17f83ea | 458 | AR_SREV_9285_12_OR_LATER(sc->sc_ah)) && |
66afad01 SB |
459 | (sc->sc_ah->opmode == NL80211_IFTYPE_AP) && |
460 | (sc->rx.rxfilter & FIF_PSPOLL)) | |
dbaaa147 | 461 | rfilt |= ATH9K_RX_FILTER_PSPOLL; |
be0418ad | 462 | |
7ea310be S |
463 | if (conf_is_ht(&sc->hw->conf)) |
464 | rfilt |= ATH9K_RX_FILTER_COMP_BAR; | |
465 | ||
cfda6695 BG |
466 | if (sc->sec_wiphy || (sc->nvifs > 1) || |
467 | (sc->rx.rxfilter & FIF_OTHER_BSS)) { | |
5eb6ba83 JC |
468 | /* The following may also be needed for other older chips */ |
469 | if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160) | |
470 | rfilt |= ATH9K_RX_FILTER_PROM; | |
b93bce2a JM |
471 | rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL; |
472 | } | |
473 | ||
f078f209 | 474 | return rfilt; |
7dcfdcd9 | 475 | |
f078f209 LR |
476 | #undef RX_FILTER_PRESERVE |
477 | } | |
478 | ||
f078f209 LR |
479 | int ath_startrecv(struct ath_softc *sc) |
480 | { | |
cbe61d8a | 481 | struct ath_hw *ah = sc->sc_ah; |
f078f209 LR |
482 | struct ath_buf *bf, *tbf; |
483 | ||
b5c80475 FF |
484 | if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) { |
485 | ath_edma_start_recv(sc); | |
486 | return 0; | |
487 | } | |
488 | ||
b77f483f S |
489 | spin_lock_bh(&sc->rx.rxbuflock); |
490 | if (list_empty(&sc->rx.rxbuf)) | |
f078f209 LR |
491 | goto start_recv; |
492 | ||
b77f483f S |
493 | sc->rx.rxlink = NULL; |
494 | list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) { | |
f078f209 LR |
495 | ath_rx_buf_link(sc, bf); |
496 | } | |
497 | ||
498 | /* We could have deleted elements so the list may be empty now */ | |
b77f483f | 499 | if (list_empty(&sc->rx.rxbuf)) |
f078f209 LR |
500 | goto start_recv; |
501 | ||
b77f483f | 502 | bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); |
f078f209 | 503 | ath9k_hw_putrxbuf(ah, bf->bf_daddr); |
be0418ad | 504 | ath9k_hw_rxena(ah); |
f078f209 LR |
505 | |
506 | start_recv: | |
be0418ad | 507 | ath_opmode_init(sc); |
48a6a468 | 508 | ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL)); |
be0418ad | 509 | |
7583c550 LR |
510 | spin_unlock_bh(&sc->rx.rxbuflock); |
511 | ||
f078f209 LR |
512 | return 0; |
513 | } | |
514 | ||
f078f209 LR |
515 | bool ath_stoprecv(struct ath_softc *sc) |
516 | { | |
cbe61d8a | 517 | struct ath_hw *ah = sc->sc_ah; |
f078f209 LR |
518 | bool stopped; |
519 | ||
1e450285 | 520 | spin_lock_bh(&sc->rx.rxbuflock); |
d47844a0 | 521 | ath9k_hw_abortpcurecv(ah); |
be0418ad S |
522 | ath9k_hw_setrxfilter(ah, 0); |
523 | stopped = ath9k_hw_stopdmarecv(ah); | |
b5c80475 FF |
524 | |
525 | if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) | |
526 | ath_edma_stop_recv(sc); | |
527 | else | |
528 | sc->rx.rxlink = NULL; | |
1e450285 | 529 | spin_unlock_bh(&sc->rx.rxbuflock); |
be0418ad | 530 | |
d584747b RM |
531 | if (!(ah->ah_flags & AH_UNPLUGGED) && |
532 | unlikely(!stopped)) { | |
d7fd1b50 BG |
533 | ath_err(ath9k_hw_common(sc->sc_ah), |
534 | "Could not stop RX, we could be " | |
535 | "confusing the DMA engine when we start RX up\n"); | |
536 | ATH_DBG_WARN_ON_ONCE(!stopped); | |
537 | } | |
f078f209 LR |
538 | return stopped; |
539 | } | |
540 | ||
f078f209 LR |
541 | void ath_flushrecv(struct ath_softc *sc) |
542 | { | |
98deeea0 | 543 | sc->sc_flags |= SC_OP_RXFLUSH; |
b5c80475 FF |
544 | if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) |
545 | ath_rx_tasklet(sc, 1, true); | |
546 | ath_rx_tasklet(sc, 1, false); | |
98deeea0 | 547 | sc->sc_flags &= ~SC_OP_RXFLUSH; |
f078f209 LR |
548 | } |
549 | ||
cc65965c JM |
550 | static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb) |
551 | { | |
552 | /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */ | |
553 | struct ieee80211_mgmt *mgmt; | |
554 | u8 *pos, *end, id, elen; | |
555 | struct ieee80211_tim_ie *tim; | |
556 | ||
557 | mgmt = (struct ieee80211_mgmt *)skb->data; | |
558 | pos = mgmt->u.beacon.variable; | |
559 | end = skb->data + skb->len; | |
560 | ||
561 | while (pos + 2 < end) { | |
562 | id = *pos++; | |
563 | elen = *pos++; | |
564 | if (pos + elen > end) | |
565 | break; | |
566 | ||
567 | if (id == WLAN_EID_TIM) { | |
568 | if (elen < sizeof(*tim)) | |
569 | break; | |
570 | tim = (struct ieee80211_tim_ie *) pos; | |
571 | if (tim->dtim_count != 0) | |
572 | break; | |
573 | return tim->bitmap_ctrl & 0x01; | |
574 | } | |
575 | ||
576 | pos += elen; | |
577 | } | |
578 | ||
579 | return false; | |
580 | } | |
581 | ||
cc65965c JM |
582 | static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb) |
583 | { | |
584 | struct ieee80211_mgmt *mgmt; | |
1510718d | 585 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
cc65965c JM |
586 | |
587 | if (skb->len < 24 + 8 + 2 + 2) | |
588 | return; | |
589 | ||
590 | mgmt = (struct ieee80211_mgmt *)skb->data; | |
4801416c BG |
591 | if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) { |
592 | /* TODO: This doesn't work well if you have stations | |
593 | * associated to two different APs because curbssid | |
594 | * is just the last AP that any of the stations associated | |
595 | * with. | |
596 | */ | |
cc65965c | 597 | return; /* not from our current AP */ |
4801416c | 598 | } |
cc65965c | 599 | |
1b04b930 | 600 | sc->ps_flags &= ~PS_WAIT_FOR_BEACON; |
293dc5df | 601 | |
1b04b930 S |
602 | if (sc->ps_flags & PS_BEACON_SYNC) { |
603 | sc->ps_flags &= ~PS_BEACON_SYNC; | |
226afe68 JP |
604 | ath_dbg(common, ATH_DBG_PS, |
605 | "Reconfigure Beacon timers based on timestamp from the AP\n"); | |
ccdfeab6 JM |
606 | ath_beacon_config(sc, NULL); |
607 | } | |
608 | ||
cc65965c JM |
609 | if (ath_beacon_dtim_pending_cab(skb)) { |
610 | /* | |
611 | * Remain awake waiting for buffered broadcast/multicast | |
58f5fffd GJ |
612 | * frames. If the last broadcast/multicast frame is not |
613 | * received properly, the next beacon frame will work as | |
614 | * a backup trigger for returning into NETWORK SLEEP state, | |
615 | * so we are waiting for it as well. | |
cc65965c | 616 | */ |
226afe68 JP |
617 | ath_dbg(common, ATH_DBG_PS, |
618 | "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n"); | |
1b04b930 | 619 | sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON; |
cc65965c JM |
620 | return; |
621 | } | |
622 | ||
1b04b930 | 623 | if (sc->ps_flags & PS_WAIT_FOR_CAB) { |
cc65965c JM |
624 | /* |
625 | * This can happen if a broadcast frame is dropped or the AP | |
626 | * fails to send a frame indicating that all CAB frames have | |
627 | * been delivered. | |
628 | */ | |
1b04b930 | 629 | sc->ps_flags &= ~PS_WAIT_FOR_CAB; |
226afe68 JP |
630 | ath_dbg(common, ATH_DBG_PS, |
631 | "PS wait for CAB frames timed out\n"); | |
cc65965c | 632 | } |
cc65965c JM |
633 | } |
634 | ||
635 | static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb) | |
636 | { | |
637 | struct ieee80211_hdr *hdr; | |
c46917bb | 638 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
cc65965c JM |
639 | |
640 | hdr = (struct ieee80211_hdr *)skb->data; | |
641 | ||
642 | /* Process Beacon and CAB receive in PS state */ | |
ededf1f8 VT |
643 | if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc)) |
644 | && ieee80211_is_beacon(hdr->frame_control)) | |
cc65965c | 645 | ath_rx_ps_beacon(sc, skb); |
1b04b930 | 646 | else if ((sc->ps_flags & PS_WAIT_FOR_CAB) && |
cc65965c JM |
647 | (ieee80211_is_data(hdr->frame_control) || |
648 | ieee80211_is_action(hdr->frame_control)) && | |
649 | is_multicast_ether_addr(hdr->addr1) && | |
650 | !ieee80211_has_moredata(hdr->frame_control)) { | |
cc65965c JM |
651 | /* |
652 | * No more broadcast/multicast frames to be received at this | |
653 | * point. | |
654 | */ | |
3fac6dfd | 655 | sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON); |
226afe68 JP |
656 | ath_dbg(common, ATH_DBG_PS, |
657 | "All PS CAB frames received, back to sleep\n"); | |
1b04b930 | 658 | } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) && |
9a23f9ca JM |
659 | !is_multicast_ether_addr(hdr->addr1) && |
660 | !ieee80211_has_morefrags(hdr->frame_control)) { | |
1b04b930 | 661 | sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA; |
226afe68 JP |
662 | ath_dbg(common, ATH_DBG_PS, |
663 | "Going back to sleep after having received PS-Poll data (0x%lx)\n", | |
1b04b930 S |
664 | sc->ps_flags & (PS_WAIT_FOR_BEACON | |
665 | PS_WAIT_FOR_CAB | | |
666 | PS_WAIT_FOR_PSPOLL_DATA | | |
667 | PS_WAIT_FOR_TX_ACK)); | |
cc65965c JM |
668 | } |
669 | } | |
670 | ||
b4afffc0 | 671 | static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw, |
cae6b74d | 672 | struct ath_softc *sc, struct sk_buff *skb) |
9d64a3cf JM |
673 | { |
674 | struct ieee80211_hdr *hdr; | |
675 | ||
676 | hdr = (struct ieee80211_hdr *)skb->data; | |
677 | ||
678 | /* Send the frame to mac80211 */ | |
679 | if (is_multicast_ether_addr(hdr->addr1)) { | |
680 | int i; | |
681 | /* | |
682 | * Deliver broadcast/multicast frames to all suitable | |
683 | * virtual wiphys. | |
684 | */ | |
685 | /* TODO: filter based on channel configuration */ | |
686 | for (i = 0; i < sc->num_sec_wiphy; i++) { | |
687 | struct ath_wiphy *aphy = sc->sec_wiphy[i]; | |
688 | struct sk_buff *nskb; | |
689 | if (aphy == NULL) | |
690 | continue; | |
691 | nskb = skb_copy(skb, GFP_ATOMIC); | |
5ca42627 LR |
692 | if (!nskb) |
693 | continue; | |
694 | ieee80211_rx(aphy->hw, nskb); | |
9d64a3cf | 695 | } |
f1d58c25 | 696 | ieee80211_rx(sc->hw, skb); |
5ca42627 | 697 | } else |
9d64a3cf | 698 | /* Deliver unicast frames based on receiver address */ |
b4afffc0 | 699 | ieee80211_rx(hw, skb); |
9d64a3cf JM |
700 | } |
701 | ||
b5c80475 FF |
702 | static bool ath_edma_get_buffers(struct ath_softc *sc, |
703 | enum ath9k_rx_qtype qtype) | |
f078f209 | 704 | { |
b5c80475 FF |
705 | struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype]; |
706 | struct ath_hw *ah = sc->sc_ah; | |
707 | struct ath_common *common = ath9k_hw_common(ah); | |
708 | struct sk_buff *skb; | |
709 | struct ath_buf *bf; | |
710 | int ret; | |
711 | ||
712 | skb = skb_peek(&rx_edma->rx_fifo); | |
713 | if (!skb) | |
714 | return false; | |
715 | ||
716 | bf = SKB_CB_ATHBUF(skb); | |
717 | BUG_ON(!bf); | |
718 | ||
ce9426d1 | 719 | dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, |
b5c80475 FF |
720 | common->rx_bufsize, DMA_FROM_DEVICE); |
721 | ||
722 | ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data); | |
ce9426d1 ML |
723 | if (ret == -EINPROGRESS) { |
724 | /*let device gain the buffer again*/ | |
725 | dma_sync_single_for_device(sc->dev, bf->bf_buf_addr, | |
726 | common->rx_bufsize, DMA_FROM_DEVICE); | |
b5c80475 | 727 | return false; |
ce9426d1 | 728 | } |
b5c80475 FF |
729 | |
730 | __skb_unlink(skb, &rx_edma->rx_fifo); | |
731 | if (ret == -EINVAL) { | |
732 | /* corrupt descriptor, skip this one and the following one */ | |
733 | list_add_tail(&bf->list, &sc->rx.rxbuf); | |
734 | ath_rx_edma_buf_link(sc, qtype); | |
735 | skb = skb_peek(&rx_edma->rx_fifo); | |
736 | if (!skb) | |
737 | return true; | |
738 | ||
739 | bf = SKB_CB_ATHBUF(skb); | |
740 | BUG_ON(!bf); | |
741 | ||
742 | __skb_unlink(skb, &rx_edma->rx_fifo); | |
743 | list_add_tail(&bf->list, &sc->rx.rxbuf); | |
744 | ath_rx_edma_buf_link(sc, qtype); | |
083e3e8d | 745 | return true; |
b5c80475 FF |
746 | } |
747 | skb_queue_tail(&rx_edma->rx_buffers, skb); | |
748 | ||
749 | return true; | |
750 | } | |
f078f209 | 751 | |
b5c80475 FF |
752 | static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc, |
753 | struct ath_rx_status *rs, | |
754 | enum ath9k_rx_qtype qtype) | |
755 | { | |
756 | struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype]; | |
757 | struct sk_buff *skb; | |
be0418ad | 758 | struct ath_buf *bf; |
b5c80475 FF |
759 | |
760 | while (ath_edma_get_buffers(sc, qtype)); | |
761 | skb = __skb_dequeue(&rx_edma->rx_buffers); | |
762 | if (!skb) | |
763 | return NULL; | |
764 | ||
765 | bf = SKB_CB_ATHBUF(skb); | |
766 | ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data); | |
767 | return bf; | |
768 | } | |
769 | ||
770 | static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc, | |
771 | struct ath_rx_status *rs) | |
772 | { | |
773 | struct ath_hw *ah = sc->sc_ah; | |
774 | struct ath_common *common = ath9k_hw_common(ah); | |
f078f209 | 775 | struct ath_desc *ds; |
b5c80475 FF |
776 | struct ath_buf *bf; |
777 | int ret; | |
778 | ||
779 | if (list_empty(&sc->rx.rxbuf)) { | |
780 | sc->rx.rxlink = NULL; | |
781 | return NULL; | |
782 | } | |
783 | ||
784 | bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); | |
785 | ds = bf->bf_desc; | |
786 | ||
787 | /* | |
788 | * Must provide the virtual address of the current | |
789 | * descriptor, the physical address, and the virtual | |
790 | * address of the next descriptor in the h/w chain. | |
791 | * This allows the HAL to look ahead to see if the | |
792 | * hardware is done with a descriptor by checking the | |
793 | * done bit in the following descriptor and the address | |
794 | * of the current descriptor the DMA engine is working | |
795 | * on. All this is necessary because of our use of | |
796 | * a self-linked list to avoid rx overruns. | |
797 | */ | |
798 | ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0); | |
799 | if (ret == -EINPROGRESS) { | |
800 | struct ath_rx_status trs; | |
801 | struct ath_buf *tbf; | |
802 | struct ath_desc *tds; | |
803 | ||
804 | memset(&trs, 0, sizeof(trs)); | |
805 | if (list_is_last(&bf->list, &sc->rx.rxbuf)) { | |
806 | sc->rx.rxlink = NULL; | |
807 | return NULL; | |
808 | } | |
809 | ||
810 | tbf = list_entry(bf->list.next, struct ath_buf, list); | |
811 | ||
812 | /* | |
813 | * On some hardware the descriptor status words could | |
814 | * get corrupted, including the done bit. Because of | |
815 | * this, check if the next descriptor's done bit is | |
816 | * set or not. | |
817 | * | |
818 | * If the next descriptor's done bit is set, the current | |
819 | * descriptor has been corrupted. Force s/w to discard | |
820 | * this descriptor and continue... | |
821 | */ | |
822 | ||
823 | tds = tbf->bf_desc; | |
824 | ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0); | |
825 | if (ret == -EINPROGRESS) | |
826 | return NULL; | |
827 | } | |
828 | ||
829 | if (!bf->bf_mpdu) | |
830 | return bf; | |
831 | ||
832 | /* | |
833 | * Synchronize the DMA transfer with CPU before | |
834 | * 1. accessing the frame | |
835 | * 2. requeueing the same buffer to h/w | |
836 | */ | |
ce9426d1 | 837 | dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, |
b5c80475 FF |
838 | common->rx_bufsize, |
839 | DMA_FROM_DEVICE); | |
840 | ||
841 | return bf; | |
842 | } | |
843 | ||
d435700f S |
844 | /* Assumes you've already done the endian to CPU conversion */ |
845 | static bool ath9k_rx_accept(struct ath_common *common, | |
9f167f64 | 846 | struct ieee80211_hdr *hdr, |
d435700f S |
847 | struct ieee80211_rx_status *rxs, |
848 | struct ath_rx_status *rx_stats, | |
849 | bool *decrypt_error) | |
850 | { | |
38852b20 SB |
851 | #define is_mc_or_valid_tkip_keyix ((is_mc || \ |
852 | (rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \ | |
853 | test_bit(rx_stats->rs_keyix, common->tkip_keymap)))) | |
854 | ||
d435700f | 855 | struct ath_hw *ah = common->ah; |
d435700f | 856 | __le16 fc; |
b7b1b512 | 857 | u8 rx_status_len = ah->caps.rx_status_len; |
d435700f | 858 | |
d435700f S |
859 | fc = hdr->frame_control; |
860 | ||
861 | if (!rx_stats->rs_datalen) | |
862 | return false; | |
863 | /* | |
864 | * rs_status follows rs_datalen so if rs_datalen is too large | |
865 | * we can take a hint that hardware corrupted it, so ignore | |
866 | * those frames. | |
867 | */ | |
b7b1b512 | 868 | if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) |
d435700f S |
869 | return false; |
870 | ||
871 | /* | |
872 | * rs_more indicates chained descriptors which can be used | |
873 | * to link buffers together for a sort of scatter-gather | |
874 | * operation. | |
875 | * reject the frame, we don't support scatter-gather yet and | |
876 | * the frame is probably corrupt anyway | |
877 | */ | |
878 | if (rx_stats->rs_more) | |
879 | return false; | |
880 | ||
881 | /* | |
882 | * The rx_stats->rs_status will not be set until the end of the | |
883 | * chained descriptors so it can be ignored if rs_more is set. The | |
884 | * rs_more will be false at the last element of the chained | |
885 | * descriptors. | |
886 | */ | |
887 | if (rx_stats->rs_status != 0) { | |
888 | if (rx_stats->rs_status & ATH9K_RXERR_CRC) | |
889 | rxs->flag |= RX_FLAG_FAILED_FCS_CRC; | |
890 | if (rx_stats->rs_status & ATH9K_RXERR_PHY) | |
891 | return false; | |
892 | ||
893 | if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) { | |
894 | *decrypt_error = true; | |
895 | } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) { | |
38852b20 | 896 | bool is_mc; |
56363dde FF |
897 | /* |
898 | * The MIC error bit is only valid if the frame | |
899 | * is not a control frame or fragment, and it was | |
900 | * decrypted using a valid TKIP key. | |
901 | */ | |
38852b20 SB |
902 | is_mc = !!is_multicast_ether_addr(hdr->addr1); |
903 | ||
56363dde FF |
904 | if (!ieee80211_is_ctl(fc) && |
905 | !ieee80211_has_morefrags(fc) && | |
906 | !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) && | |
38852b20 | 907 | is_mc_or_valid_tkip_keyix) |
d435700f | 908 | rxs->flag |= RX_FLAG_MMIC_ERROR; |
56363dde FF |
909 | else |
910 | rx_stats->rs_status &= ~ATH9K_RXERR_MIC; | |
d435700f S |
911 | } |
912 | /* | |
913 | * Reject error frames with the exception of | |
914 | * decryption and MIC failures. For monitor mode, | |
915 | * we also ignore the CRC error. | |
916 | */ | |
5f841b41 | 917 | if (ah->is_monitoring) { |
d435700f S |
918 | if (rx_stats->rs_status & |
919 | ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC | | |
920 | ATH9K_RXERR_CRC)) | |
921 | return false; | |
922 | } else { | |
923 | if (rx_stats->rs_status & | |
924 | ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) { | |
925 | return false; | |
926 | } | |
927 | } | |
928 | } | |
929 | return true; | |
930 | } | |
931 | ||
932 | static int ath9k_process_rate(struct ath_common *common, | |
933 | struct ieee80211_hw *hw, | |
934 | struct ath_rx_status *rx_stats, | |
9f167f64 | 935 | struct ieee80211_rx_status *rxs) |
d435700f S |
936 | { |
937 | struct ieee80211_supported_band *sband; | |
938 | enum ieee80211_band band; | |
939 | unsigned int i = 0; | |
940 | ||
941 | band = hw->conf.channel->band; | |
942 | sband = hw->wiphy->bands[band]; | |
943 | ||
944 | if (rx_stats->rs_rate & 0x80) { | |
945 | /* HT rate */ | |
946 | rxs->flag |= RX_FLAG_HT; | |
947 | if (rx_stats->rs_flags & ATH9K_RX_2040) | |
948 | rxs->flag |= RX_FLAG_40MHZ; | |
949 | if (rx_stats->rs_flags & ATH9K_RX_GI) | |
950 | rxs->flag |= RX_FLAG_SHORT_GI; | |
951 | rxs->rate_idx = rx_stats->rs_rate & 0x7f; | |
952 | return 0; | |
953 | } | |
954 | ||
955 | for (i = 0; i < sband->n_bitrates; i++) { | |
956 | if (sband->bitrates[i].hw_value == rx_stats->rs_rate) { | |
957 | rxs->rate_idx = i; | |
958 | return 0; | |
959 | } | |
960 | if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) { | |
961 | rxs->flag |= RX_FLAG_SHORTPRE; | |
962 | rxs->rate_idx = i; | |
963 | return 0; | |
964 | } | |
965 | } | |
966 | ||
967 | /* | |
968 | * No valid hardware bitrate found -- we should not get here | |
969 | * because hardware has already validated this frame as OK. | |
970 | */ | |
226afe68 JP |
971 | ath_dbg(common, ATH_DBG_XMIT, |
972 | "unsupported hw bitrate detected 0x%02x using 1 Mbit\n", | |
973 | rx_stats->rs_rate); | |
d435700f S |
974 | |
975 | return -EINVAL; | |
976 | } | |
977 | ||
978 | static void ath9k_process_rssi(struct ath_common *common, | |
979 | struct ieee80211_hw *hw, | |
9f167f64 | 980 | struct ieee80211_hdr *hdr, |
d435700f S |
981 | struct ath_rx_status *rx_stats) |
982 | { | |
9fa23e17 | 983 | struct ath_wiphy *aphy = hw->priv; |
d435700f | 984 | struct ath_hw *ah = common->ah; |
9fa23e17 | 985 | int last_rssi; |
d435700f S |
986 | __le16 fc; |
987 | ||
9fa23e17 FF |
988 | if (ah->opmode != NL80211_IFTYPE_STATION) |
989 | return; | |
990 | ||
d435700f | 991 | fc = hdr->frame_control; |
9fa23e17 | 992 | if (!ieee80211_is_beacon(fc) || |
4801416c BG |
993 | compare_ether_addr(hdr->addr3, common->curbssid)) { |
994 | /* TODO: This doesn't work well if you have stations | |
995 | * associated to two different APs because curbssid | |
996 | * is just the last AP that any of the stations associated | |
997 | * with. | |
998 | */ | |
9fa23e17 | 999 | return; |
4801416c | 1000 | } |
d435700f | 1001 | |
9fa23e17 FF |
1002 | if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr) |
1003 | ATH_RSSI_LPF(aphy->last_rssi, rx_stats->rs_rssi); | |
d435700f | 1004 | |
9fa23e17 | 1005 | last_rssi = aphy->last_rssi; |
d435700f S |
1006 | if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER)) |
1007 | rx_stats->rs_rssi = ATH_EP_RND(last_rssi, | |
1008 | ATH_RSSI_EP_MULTIPLIER); | |
1009 | if (rx_stats->rs_rssi < 0) | |
1010 | rx_stats->rs_rssi = 0; | |
1011 | ||
1012 | /* Update Beacon RSSI, this is used by ANI. */ | |
9fa23e17 | 1013 | ah->stats.avgbrssi = rx_stats->rs_rssi; |
d435700f S |
1014 | } |
1015 | ||
1016 | /* | |
1017 | * For Decrypt or Demic errors, we only mark packet status here and always push | |
1018 | * up the frame up to let mac80211 handle the actual error case, be it no | |
1019 | * decryption key or real decryption error. This let us keep statistics there. | |
1020 | */ | |
1021 | static int ath9k_rx_skb_preprocess(struct ath_common *common, | |
1022 | struct ieee80211_hw *hw, | |
9f167f64 | 1023 | struct ieee80211_hdr *hdr, |
d435700f S |
1024 | struct ath_rx_status *rx_stats, |
1025 | struct ieee80211_rx_status *rx_status, | |
1026 | bool *decrypt_error) | |
1027 | { | |
d435700f S |
1028 | memset(rx_status, 0, sizeof(struct ieee80211_rx_status)); |
1029 | ||
1030 | /* | |
1031 | * everything but the rate is checked here, the rate check is done | |
1032 | * separately to avoid doing two lookups for a rate for each frame. | |
1033 | */ | |
9f167f64 | 1034 | if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error)) |
d435700f S |
1035 | return -EINVAL; |
1036 | ||
9f167f64 | 1037 | ath9k_process_rssi(common, hw, hdr, rx_stats); |
d435700f | 1038 | |
9f167f64 | 1039 | if (ath9k_process_rate(common, hw, rx_stats, rx_status)) |
d435700f S |
1040 | return -EINVAL; |
1041 | ||
d435700f S |
1042 | rx_status->band = hw->conf.channel->band; |
1043 | rx_status->freq = hw->conf.channel->center_freq; | |
1044 | rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi; | |
1045 | rx_status->antenna = rx_stats->rs_antenna; | |
1046 | rx_status->flag |= RX_FLAG_TSFT; | |
1047 | ||
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | static void ath9k_rx_skb_postprocess(struct ath_common *common, | |
1052 | struct sk_buff *skb, | |
1053 | struct ath_rx_status *rx_stats, | |
1054 | struct ieee80211_rx_status *rxs, | |
1055 | bool decrypt_error) | |
1056 | { | |
1057 | struct ath_hw *ah = common->ah; | |
1058 | struct ieee80211_hdr *hdr; | |
1059 | int hdrlen, padpos, padsize; | |
1060 | u8 keyix; | |
1061 | __le16 fc; | |
1062 | ||
1063 | /* see if any padding is done by the hw and remove it */ | |
1064 | hdr = (struct ieee80211_hdr *) skb->data; | |
1065 | hdrlen = ieee80211_get_hdrlen_from_skb(skb); | |
1066 | fc = hdr->frame_control; | |
1067 | padpos = ath9k_cmn_padpos(hdr->frame_control); | |
1068 | ||
1069 | /* The MAC header is padded to have 32-bit boundary if the | |
1070 | * packet payload is non-zero. The general calculation for | |
1071 | * padsize would take into account odd header lengths: | |
1072 | * padsize = (4 - padpos % 4) % 4; However, since only | |
1073 | * even-length headers are used, padding can only be 0 or 2 | |
1074 | * bytes and we can optimize this a bit. In addition, we must | |
1075 | * not try to remove padding from short control frames that do | |
1076 | * not have payload. */ | |
1077 | padsize = padpos & 3; | |
1078 | if (padsize && skb->len>=padpos+padsize+FCS_LEN) { | |
1079 | memmove(skb->data + padsize, skb->data, padpos); | |
1080 | skb_pull(skb, padsize); | |
1081 | } | |
1082 | ||
1083 | keyix = rx_stats->rs_keyix; | |
1084 | ||
1085 | if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error && | |
1086 | ieee80211_has_protected(fc)) { | |
1087 | rxs->flag |= RX_FLAG_DECRYPTED; | |
1088 | } else if (ieee80211_has_protected(fc) | |
1089 | && !decrypt_error && skb->len >= hdrlen + 4) { | |
1090 | keyix = skb->data[hdrlen + 3] >> 6; | |
1091 | ||
1092 | if (test_bit(keyix, common->keymap)) | |
1093 | rxs->flag |= RX_FLAG_DECRYPTED; | |
1094 | } | |
1095 | if (ah->sw_mgmt_crypto && | |
1096 | (rxs->flag & RX_FLAG_DECRYPTED) && | |
1097 | ieee80211_is_mgmt(fc)) | |
1098 | /* Use software decrypt for management frames. */ | |
1099 | rxs->flag &= ~RX_FLAG_DECRYPTED; | |
1100 | } | |
b5c80475 | 1101 | |
102885a5 VT |
1102 | static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb, |
1103 | struct ath_hw_antcomb_conf ant_conf, | |
1104 | int main_rssi_avg) | |
1105 | { | |
1106 | antcomb->quick_scan_cnt = 0; | |
1107 | ||
1108 | if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2) | |
1109 | antcomb->rssi_lna2 = main_rssi_avg; | |
1110 | else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1) | |
1111 | antcomb->rssi_lna1 = main_rssi_avg; | |
1112 | ||
1113 | switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) { | |
1114 | case (0x10): /* LNA2 A-B */ | |
1115 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1116 | antcomb->first_quick_scan_conf = | |
1117 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1118 | antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1; | |
1119 | break; | |
1120 | case (0x20): /* LNA1 A-B */ | |
1121 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1122 | antcomb->first_quick_scan_conf = | |
1123 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1124 | antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2; | |
1125 | break; | |
1126 | case (0x21): /* LNA1 LNA2 */ | |
1127 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2; | |
1128 | antcomb->first_quick_scan_conf = | |
1129 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1130 | antcomb->second_quick_scan_conf = | |
1131 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1132 | break; | |
1133 | case (0x12): /* LNA2 LNA1 */ | |
1134 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1; | |
1135 | antcomb->first_quick_scan_conf = | |
1136 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1137 | antcomb->second_quick_scan_conf = | |
1138 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1139 | break; | |
1140 | case (0x13): /* LNA2 A+B */ | |
1141 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1142 | antcomb->first_quick_scan_conf = | |
1143 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1144 | antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1; | |
1145 | break; | |
1146 | case (0x23): /* LNA1 A+B */ | |
1147 | antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1148 | antcomb->first_quick_scan_conf = | |
1149 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1150 | antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2; | |
1151 | break; | |
1152 | default: | |
1153 | break; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb, | |
1158 | struct ath_hw_antcomb_conf *div_ant_conf, | |
1159 | int main_rssi_avg, int alt_rssi_avg, | |
1160 | int alt_ratio) | |
1161 | { | |
1162 | /* alt_good */ | |
1163 | switch (antcomb->quick_scan_cnt) { | |
1164 | case 0: | |
1165 | /* set alt to main, and alt to first conf */ | |
1166 | div_ant_conf->main_lna_conf = antcomb->main_conf; | |
1167 | div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf; | |
1168 | break; | |
1169 | case 1: | |
1170 | /* set alt to main, and alt to first conf */ | |
1171 | div_ant_conf->main_lna_conf = antcomb->main_conf; | |
1172 | div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf; | |
1173 | antcomb->rssi_first = main_rssi_avg; | |
1174 | antcomb->rssi_second = alt_rssi_avg; | |
1175 | ||
1176 | if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) { | |
1177 | /* main is LNA1 */ | |
1178 | if (ath_is_alt_ant_ratio_better(alt_ratio, | |
1179 | ATH_ANT_DIV_COMB_LNA1_DELTA_HI, | |
1180 | ATH_ANT_DIV_COMB_LNA1_DELTA_LOW, | |
1181 | main_rssi_avg, alt_rssi_avg, | |
1182 | antcomb->total_pkt_count)) | |
1183 | antcomb->first_ratio = true; | |
1184 | else | |
1185 | antcomb->first_ratio = false; | |
1186 | } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) { | |
1187 | if (ath_is_alt_ant_ratio_better(alt_ratio, | |
1188 | ATH_ANT_DIV_COMB_LNA1_DELTA_MID, | |
1189 | ATH_ANT_DIV_COMB_LNA1_DELTA_LOW, | |
1190 | main_rssi_avg, alt_rssi_avg, | |
1191 | antcomb->total_pkt_count)) | |
1192 | antcomb->first_ratio = true; | |
1193 | else | |
1194 | antcomb->first_ratio = false; | |
1195 | } else { | |
1196 | if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) && | |
1197 | (alt_rssi_avg > main_rssi_avg + | |
1198 | ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) || | |
1199 | (alt_rssi_avg > main_rssi_avg)) && | |
1200 | (antcomb->total_pkt_count > 50)) | |
1201 | antcomb->first_ratio = true; | |
1202 | else | |
1203 | antcomb->first_ratio = false; | |
1204 | } | |
1205 | break; | |
1206 | case 2: | |
1207 | antcomb->alt_good = false; | |
1208 | antcomb->scan_not_start = false; | |
1209 | antcomb->scan = false; | |
1210 | antcomb->rssi_first = main_rssi_avg; | |
1211 | antcomb->rssi_third = alt_rssi_avg; | |
1212 | ||
1213 | if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) | |
1214 | antcomb->rssi_lna1 = alt_rssi_avg; | |
1215 | else if (antcomb->second_quick_scan_conf == | |
1216 | ATH_ANT_DIV_COMB_LNA2) | |
1217 | antcomb->rssi_lna2 = alt_rssi_avg; | |
1218 | else if (antcomb->second_quick_scan_conf == | |
1219 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) { | |
1220 | if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) | |
1221 | antcomb->rssi_lna2 = main_rssi_avg; | |
1222 | else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) | |
1223 | antcomb->rssi_lna1 = main_rssi_avg; | |
1224 | } | |
1225 | ||
1226 | if (antcomb->rssi_lna2 > antcomb->rssi_lna1 + | |
1227 | ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA) | |
1228 | div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2; | |
1229 | else | |
1230 | div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1; | |
1231 | ||
1232 | if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) { | |
1233 | if (ath_is_alt_ant_ratio_better(alt_ratio, | |
1234 | ATH_ANT_DIV_COMB_LNA1_DELTA_HI, | |
1235 | ATH_ANT_DIV_COMB_LNA1_DELTA_LOW, | |
1236 | main_rssi_avg, alt_rssi_avg, | |
1237 | antcomb->total_pkt_count)) | |
1238 | antcomb->second_ratio = true; | |
1239 | else | |
1240 | antcomb->second_ratio = false; | |
1241 | } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) { | |
1242 | if (ath_is_alt_ant_ratio_better(alt_ratio, | |
1243 | ATH_ANT_DIV_COMB_LNA1_DELTA_MID, | |
1244 | ATH_ANT_DIV_COMB_LNA1_DELTA_LOW, | |
1245 | main_rssi_avg, alt_rssi_avg, | |
1246 | antcomb->total_pkt_count)) | |
1247 | antcomb->second_ratio = true; | |
1248 | else | |
1249 | antcomb->second_ratio = false; | |
1250 | } else { | |
1251 | if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) && | |
1252 | (alt_rssi_avg > main_rssi_avg + | |
1253 | ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) || | |
1254 | (alt_rssi_avg > main_rssi_avg)) && | |
1255 | (antcomb->total_pkt_count > 50)) | |
1256 | antcomb->second_ratio = true; | |
1257 | else | |
1258 | antcomb->second_ratio = false; | |
1259 | } | |
1260 | ||
1261 | /* set alt to the conf with maximun ratio */ | |
1262 | if (antcomb->first_ratio && antcomb->second_ratio) { | |
1263 | if (antcomb->rssi_second > antcomb->rssi_third) { | |
1264 | /* first alt*/ | |
1265 | if ((antcomb->first_quick_scan_conf == | |
1266 | ATH_ANT_DIV_COMB_LNA1) || | |
1267 | (antcomb->first_quick_scan_conf == | |
1268 | ATH_ANT_DIV_COMB_LNA2)) | |
1269 | /* Set alt LNA1 or LNA2*/ | |
1270 | if (div_ant_conf->main_lna_conf == | |
1271 | ATH_ANT_DIV_COMB_LNA2) | |
1272 | div_ant_conf->alt_lna_conf = | |
1273 | ATH_ANT_DIV_COMB_LNA1; | |
1274 | else | |
1275 | div_ant_conf->alt_lna_conf = | |
1276 | ATH_ANT_DIV_COMB_LNA2; | |
1277 | else | |
1278 | /* Set alt to A+B or A-B */ | |
1279 | div_ant_conf->alt_lna_conf = | |
1280 | antcomb->first_quick_scan_conf; | |
1281 | } else if ((antcomb->second_quick_scan_conf == | |
1282 | ATH_ANT_DIV_COMB_LNA1) || | |
1283 | (antcomb->second_quick_scan_conf == | |
1284 | ATH_ANT_DIV_COMB_LNA2)) { | |
1285 | /* Set alt LNA1 or LNA2 */ | |
1286 | if (div_ant_conf->main_lna_conf == | |
1287 | ATH_ANT_DIV_COMB_LNA2) | |
1288 | div_ant_conf->alt_lna_conf = | |
1289 | ATH_ANT_DIV_COMB_LNA1; | |
1290 | else | |
1291 | div_ant_conf->alt_lna_conf = | |
1292 | ATH_ANT_DIV_COMB_LNA2; | |
1293 | } else { | |
1294 | /* Set alt to A+B or A-B */ | |
1295 | div_ant_conf->alt_lna_conf = | |
1296 | antcomb->second_quick_scan_conf; | |
1297 | } | |
1298 | } else if (antcomb->first_ratio) { | |
1299 | /* first alt */ | |
1300 | if ((antcomb->first_quick_scan_conf == | |
1301 | ATH_ANT_DIV_COMB_LNA1) || | |
1302 | (antcomb->first_quick_scan_conf == | |
1303 | ATH_ANT_DIV_COMB_LNA2)) | |
1304 | /* Set alt LNA1 or LNA2 */ | |
1305 | if (div_ant_conf->main_lna_conf == | |
1306 | ATH_ANT_DIV_COMB_LNA2) | |
1307 | div_ant_conf->alt_lna_conf = | |
1308 | ATH_ANT_DIV_COMB_LNA1; | |
1309 | else | |
1310 | div_ant_conf->alt_lna_conf = | |
1311 | ATH_ANT_DIV_COMB_LNA2; | |
1312 | else | |
1313 | /* Set alt to A+B or A-B */ | |
1314 | div_ant_conf->alt_lna_conf = | |
1315 | antcomb->first_quick_scan_conf; | |
1316 | } else if (antcomb->second_ratio) { | |
1317 | /* second alt */ | |
1318 | if ((antcomb->second_quick_scan_conf == | |
1319 | ATH_ANT_DIV_COMB_LNA1) || | |
1320 | (antcomb->second_quick_scan_conf == | |
1321 | ATH_ANT_DIV_COMB_LNA2)) | |
1322 | /* Set alt LNA1 or LNA2 */ | |
1323 | if (div_ant_conf->main_lna_conf == | |
1324 | ATH_ANT_DIV_COMB_LNA2) | |
1325 | div_ant_conf->alt_lna_conf = | |
1326 | ATH_ANT_DIV_COMB_LNA1; | |
1327 | else | |
1328 | div_ant_conf->alt_lna_conf = | |
1329 | ATH_ANT_DIV_COMB_LNA2; | |
1330 | else | |
1331 | /* Set alt to A+B or A-B */ | |
1332 | div_ant_conf->alt_lna_conf = | |
1333 | antcomb->second_quick_scan_conf; | |
1334 | } else { | |
1335 | /* main is largest */ | |
1336 | if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) || | |
1337 | (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)) | |
1338 | /* Set alt LNA1 or LNA2 */ | |
1339 | if (div_ant_conf->main_lna_conf == | |
1340 | ATH_ANT_DIV_COMB_LNA2) | |
1341 | div_ant_conf->alt_lna_conf = | |
1342 | ATH_ANT_DIV_COMB_LNA1; | |
1343 | else | |
1344 | div_ant_conf->alt_lna_conf = | |
1345 | ATH_ANT_DIV_COMB_LNA2; | |
1346 | else | |
1347 | /* Set alt to A+B or A-B */ | |
1348 | div_ant_conf->alt_lna_conf = antcomb->main_conf; | |
1349 | } | |
1350 | break; | |
1351 | default: | |
1352 | break; | |
1353 | } | |
1354 | } | |
1355 | ||
9bad82b8 | 1356 | static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf) |
102885a5 VT |
1357 | { |
1358 | /* Adjust the fast_div_bias based on main and alt lna conf */ | |
1359 | switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) { | |
1360 | case (0x01): /* A-B LNA2 */ | |
1361 | ant_conf->fast_div_bias = 0x3b; | |
1362 | break; | |
1363 | case (0x02): /* A-B LNA1 */ | |
1364 | ant_conf->fast_div_bias = 0x3d; | |
1365 | break; | |
1366 | case (0x03): /* A-B A+B */ | |
1367 | ant_conf->fast_div_bias = 0x1; | |
1368 | break; | |
1369 | case (0x10): /* LNA2 A-B */ | |
1370 | ant_conf->fast_div_bias = 0x7; | |
1371 | break; | |
1372 | case (0x12): /* LNA2 LNA1 */ | |
1373 | ant_conf->fast_div_bias = 0x2; | |
1374 | break; | |
1375 | case (0x13): /* LNA2 A+B */ | |
1376 | ant_conf->fast_div_bias = 0x7; | |
1377 | break; | |
1378 | case (0x20): /* LNA1 A-B */ | |
1379 | ant_conf->fast_div_bias = 0x6; | |
1380 | break; | |
1381 | case (0x21): /* LNA1 LNA2 */ | |
1382 | ant_conf->fast_div_bias = 0x0; | |
1383 | break; | |
1384 | case (0x23): /* LNA1 A+B */ | |
1385 | ant_conf->fast_div_bias = 0x6; | |
1386 | break; | |
1387 | case (0x30): /* A+B A-B */ | |
1388 | ant_conf->fast_div_bias = 0x1; | |
1389 | break; | |
1390 | case (0x31): /* A+B LNA2 */ | |
1391 | ant_conf->fast_div_bias = 0x3b; | |
1392 | break; | |
1393 | case (0x32): /* A+B LNA1 */ | |
1394 | ant_conf->fast_div_bias = 0x3d; | |
1395 | break; | |
1396 | default: | |
1397 | break; | |
1398 | } | |
1399 | } | |
1400 | ||
1401 | /* Antenna diversity and combining */ | |
1402 | static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs) | |
1403 | { | |
1404 | struct ath_hw_antcomb_conf div_ant_conf; | |
1405 | struct ath_ant_comb *antcomb = &sc->ant_comb; | |
1406 | int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set; | |
1407 | int curr_main_set, curr_bias; | |
1408 | int main_rssi = rs->rs_rssi_ctl0; | |
1409 | int alt_rssi = rs->rs_rssi_ctl1; | |
1410 | int rx_ant_conf, main_ant_conf; | |
1411 | bool short_scan = false; | |
1412 | ||
1413 | rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) & | |
1414 | ATH_ANT_RX_MASK; | |
1415 | main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) & | |
1416 | ATH_ANT_RX_MASK; | |
1417 | ||
1418 | /* Record packet only when alt_rssi is positive */ | |
1419 | if (alt_rssi > 0) { | |
1420 | antcomb->total_pkt_count++; | |
1421 | antcomb->main_total_rssi += main_rssi; | |
1422 | antcomb->alt_total_rssi += alt_rssi; | |
1423 | if (main_ant_conf == rx_ant_conf) | |
1424 | antcomb->main_recv_cnt++; | |
1425 | else | |
1426 | antcomb->alt_recv_cnt++; | |
1427 | } | |
1428 | ||
1429 | /* Short scan check */ | |
1430 | if (antcomb->scan && antcomb->alt_good) { | |
1431 | if (time_after(jiffies, antcomb->scan_start_time + | |
1432 | msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR))) | |
1433 | short_scan = true; | |
1434 | else | |
1435 | if (antcomb->total_pkt_count == | |
1436 | ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) { | |
1437 | alt_ratio = ((antcomb->alt_recv_cnt * 100) / | |
1438 | antcomb->total_pkt_count); | |
1439 | if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO) | |
1440 | short_scan = true; | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) || | |
1445 | rs->rs_moreaggr) && !short_scan) | |
1446 | return; | |
1447 | ||
1448 | if (antcomb->total_pkt_count) { | |
1449 | alt_ratio = ((antcomb->alt_recv_cnt * 100) / | |
1450 | antcomb->total_pkt_count); | |
1451 | main_rssi_avg = (antcomb->main_total_rssi / | |
1452 | antcomb->total_pkt_count); | |
1453 | alt_rssi_avg = (antcomb->alt_total_rssi / | |
1454 | antcomb->total_pkt_count); | |
1455 | } | |
1456 | ||
1457 | ||
1458 | ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf); | |
1459 | curr_alt_set = div_ant_conf.alt_lna_conf; | |
1460 | curr_main_set = div_ant_conf.main_lna_conf; | |
1461 | curr_bias = div_ant_conf.fast_div_bias; | |
1462 | ||
1463 | antcomb->count++; | |
1464 | ||
1465 | if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) { | |
1466 | if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) { | |
1467 | ath_lnaconf_alt_good_scan(antcomb, div_ant_conf, | |
1468 | main_rssi_avg); | |
1469 | antcomb->alt_good = true; | |
1470 | } else { | |
1471 | antcomb->alt_good = false; | |
1472 | } | |
1473 | ||
1474 | antcomb->count = 0; | |
1475 | antcomb->scan = true; | |
1476 | antcomb->scan_not_start = true; | |
1477 | } | |
1478 | ||
1479 | if (!antcomb->scan) { | |
1480 | if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) { | |
1481 | if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) { | |
1482 | /* Switch main and alt LNA */ | |
1483 | div_ant_conf.main_lna_conf = | |
1484 | ATH_ANT_DIV_COMB_LNA2; | |
1485 | div_ant_conf.alt_lna_conf = | |
1486 | ATH_ANT_DIV_COMB_LNA1; | |
1487 | } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) { | |
1488 | div_ant_conf.main_lna_conf = | |
1489 | ATH_ANT_DIV_COMB_LNA1; | |
1490 | div_ant_conf.alt_lna_conf = | |
1491 | ATH_ANT_DIV_COMB_LNA2; | |
1492 | } | |
1493 | ||
1494 | goto div_comb_done; | |
1495 | } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) && | |
1496 | (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) { | |
1497 | /* Set alt to another LNA */ | |
1498 | if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) | |
1499 | div_ant_conf.alt_lna_conf = | |
1500 | ATH_ANT_DIV_COMB_LNA1; | |
1501 | else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) | |
1502 | div_ant_conf.alt_lna_conf = | |
1503 | ATH_ANT_DIV_COMB_LNA2; | |
1504 | ||
1505 | goto div_comb_done; | |
1506 | } | |
1507 | ||
1508 | if ((alt_rssi_avg < (main_rssi_avg + | |
1509 | ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA))) | |
1510 | goto div_comb_done; | |
1511 | } | |
1512 | ||
1513 | if (!antcomb->scan_not_start) { | |
1514 | switch (curr_alt_set) { | |
1515 | case ATH_ANT_DIV_COMB_LNA2: | |
1516 | antcomb->rssi_lna2 = alt_rssi_avg; | |
1517 | antcomb->rssi_lna1 = main_rssi_avg; | |
1518 | antcomb->scan = true; | |
1519 | /* set to A+B */ | |
1520 | div_ant_conf.main_lna_conf = | |
1521 | ATH_ANT_DIV_COMB_LNA1; | |
1522 | div_ant_conf.alt_lna_conf = | |
1523 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1524 | break; | |
1525 | case ATH_ANT_DIV_COMB_LNA1: | |
1526 | antcomb->rssi_lna1 = alt_rssi_avg; | |
1527 | antcomb->rssi_lna2 = main_rssi_avg; | |
1528 | antcomb->scan = true; | |
1529 | /* set to A+B */ | |
1530 | div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2; | |
1531 | div_ant_conf.alt_lna_conf = | |
1532 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1533 | break; | |
1534 | case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2: | |
1535 | antcomb->rssi_add = alt_rssi_avg; | |
1536 | antcomb->scan = true; | |
1537 | /* set to A-B */ | |
1538 | div_ant_conf.alt_lna_conf = | |
1539 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1540 | break; | |
1541 | case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2: | |
1542 | antcomb->rssi_sub = alt_rssi_avg; | |
1543 | antcomb->scan = false; | |
1544 | if (antcomb->rssi_lna2 > | |
1545 | (antcomb->rssi_lna1 + | |
1546 | ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) { | |
1547 | /* use LNA2 as main LNA */ | |
1548 | if ((antcomb->rssi_add > antcomb->rssi_lna1) && | |
1549 | (antcomb->rssi_add > antcomb->rssi_sub)) { | |
1550 | /* set to A+B */ | |
1551 | div_ant_conf.main_lna_conf = | |
1552 | ATH_ANT_DIV_COMB_LNA2; | |
1553 | div_ant_conf.alt_lna_conf = | |
1554 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1555 | } else if (antcomb->rssi_sub > | |
1556 | antcomb->rssi_lna1) { | |
1557 | /* set to A-B */ | |
1558 | div_ant_conf.main_lna_conf = | |
1559 | ATH_ANT_DIV_COMB_LNA2; | |
1560 | div_ant_conf.alt_lna_conf = | |
1561 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1562 | } else { | |
1563 | /* set to LNA1 */ | |
1564 | div_ant_conf.main_lna_conf = | |
1565 | ATH_ANT_DIV_COMB_LNA2; | |
1566 | div_ant_conf.alt_lna_conf = | |
1567 | ATH_ANT_DIV_COMB_LNA1; | |
1568 | } | |
1569 | } else { | |
1570 | /* use LNA1 as main LNA */ | |
1571 | if ((antcomb->rssi_add > antcomb->rssi_lna2) && | |
1572 | (antcomb->rssi_add > antcomb->rssi_sub)) { | |
1573 | /* set to A+B */ | |
1574 | div_ant_conf.main_lna_conf = | |
1575 | ATH_ANT_DIV_COMB_LNA1; | |
1576 | div_ant_conf.alt_lna_conf = | |
1577 | ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2; | |
1578 | } else if (antcomb->rssi_sub > | |
1579 | antcomb->rssi_lna1) { | |
1580 | /* set to A-B */ | |
1581 | div_ant_conf.main_lna_conf = | |
1582 | ATH_ANT_DIV_COMB_LNA1; | |
1583 | div_ant_conf.alt_lna_conf = | |
1584 | ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2; | |
1585 | } else { | |
1586 | /* set to LNA2 */ | |
1587 | div_ant_conf.main_lna_conf = | |
1588 | ATH_ANT_DIV_COMB_LNA1; | |
1589 | div_ant_conf.alt_lna_conf = | |
1590 | ATH_ANT_DIV_COMB_LNA2; | |
1591 | } | |
1592 | } | |
1593 | break; | |
1594 | default: | |
1595 | break; | |
1596 | } | |
1597 | } else { | |
1598 | if (!antcomb->alt_good) { | |
1599 | antcomb->scan_not_start = false; | |
1600 | /* Set alt to another LNA */ | |
1601 | if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) { | |
1602 | div_ant_conf.main_lna_conf = | |
1603 | ATH_ANT_DIV_COMB_LNA2; | |
1604 | div_ant_conf.alt_lna_conf = | |
1605 | ATH_ANT_DIV_COMB_LNA1; | |
1606 | } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) { | |
1607 | div_ant_conf.main_lna_conf = | |
1608 | ATH_ANT_DIV_COMB_LNA1; | |
1609 | div_ant_conf.alt_lna_conf = | |
1610 | ATH_ANT_DIV_COMB_LNA2; | |
1611 | } | |
1612 | goto div_comb_done; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf, | |
1617 | main_rssi_avg, alt_rssi_avg, | |
1618 | alt_ratio); | |
1619 | ||
1620 | antcomb->quick_scan_cnt++; | |
1621 | ||
1622 | div_comb_done: | |
1623 | ath_ant_div_conf_fast_divbias(&div_ant_conf); | |
1624 | ||
1625 | ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf); | |
1626 | ||
1627 | antcomb->scan_start_time = jiffies; | |
1628 | antcomb->total_pkt_count = 0; | |
1629 | antcomb->main_total_rssi = 0; | |
1630 | antcomb->alt_total_rssi = 0; | |
1631 | antcomb->main_recv_cnt = 0; | |
1632 | antcomb->alt_recv_cnt = 0; | |
1633 | } | |
1634 | ||
b5c80475 FF |
1635 | int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp) |
1636 | { | |
1637 | struct ath_buf *bf; | |
cb71d9ba | 1638 | struct sk_buff *skb = NULL, *requeue_skb; |
5ca42627 | 1639 | struct ieee80211_rx_status *rxs; |
cbe61d8a | 1640 | struct ath_hw *ah = sc->sc_ah; |
27c51f1a | 1641 | struct ath_common *common = ath9k_hw_common(ah); |
b4afffc0 | 1642 | /* |
cae6b74d | 1643 | * The hw can technically differ from common->hw when using ath9k |
b4afffc0 LR |
1644 | * virtual wiphy so to account for that we iterate over the active |
1645 | * wiphys and find the appropriate wiphy and therefore hw. | |
1646 | */ | |
1647 | struct ieee80211_hw *hw = NULL; | |
be0418ad | 1648 | struct ieee80211_hdr *hdr; |
c9b14170 | 1649 | int retval; |
be0418ad | 1650 | bool decrypt_error = false; |
29bffa96 | 1651 | struct ath_rx_status rs; |
b5c80475 FF |
1652 | enum ath9k_rx_qtype qtype; |
1653 | bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA); | |
1654 | int dma_type; | |
5c6dd921 | 1655 | u8 rx_status_len = ah->caps.rx_status_len; |
a6d2055b FF |
1656 | u64 tsf = 0; |
1657 | u32 tsf_lower = 0; | |
8ab2cd09 | 1658 | unsigned long flags; |
be0418ad | 1659 | |
b5c80475 | 1660 | if (edma) |
b5c80475 | 1661 | dma_type = DMA_BIDIRECTIONAL; |
56824223 ML |
1662 | else |
1663 | dma_type = DMA_FROM_DEVICE; | |
b5c80475 FF |
1664 | |
1665 | qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP; | |
b77f483f | 1666 | spin_lock_bh(&sc->rx.rxbuflock); |
f078f209 | 1667 | |
a6d2055b FF |
1668 | tsf = ath9k_hw_gettsf64(ah); |
1669 | tsf_lower = tsf & 0xffffffff; | |
1670 | ||
f078f209 LR |
1671 | do { |
1672 | /* If handling rx interrupt and flush is in progress => exit */ | |
98deeea0 | 1673 | if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0)) |
f078f209 LR |
1674 | break; |
1675 | ||
29bffa96 | 1676 | memset(&rs, 0, sizeof(rs)); |
b5c80475 FF |
1677 | if (edma) |
1678 | bf = ath_edma_get_next_rx_buf(sc, &rs, qtype); | |
1679 | else | |
1680 | bf = ath_get_next_rx_buf(sc, &rs); | |
f078f209 | 1681 | |
b5c80475 FF |
1682 | if (!bf) |
1683 | break; | |
f078f209 | 1684 | |
f078f209 | 1685 | skb = bf->bf_mpdu; |
be0418ad | 1686 | if (!skb) |
f078f209 | 1687 | continue; |
f078f209 | 1688 | |
5c6dd921 | 1689 | hdr = (struct ieee80211_hdr *) (skb->data + rx_status_len); |
5ca42627 LR |
1690 | rxs = IEEE80211_SKB_RXCB(skb); |
1691 | ||
b4afffc0 LR |
1692 | hw = ath_get_virt_hw(sc, hdr); |
1693 | ||
29bffa96 | 1694 | ath_debug_stat_rx(sc, &rs); |
1395d3f0 | 1695 | |
f078f209 | 1696 | /* |
be0418ad S |
1697 | * If we're asked to flush receive queue, directly |
1698 | * chain it back at the queue without processing it. | |
f078f209 | 1699 | */ |
be0418ad | 1700 | if (flush) |
cb71d9ba | 1701 | goto requeue; |
f078f209 | 1702 | |
c8f3b721 JF |
1703 | retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs, |
1704 | rxs, &decrypt_error); | |
1705 | if (retval) | |
1706 | goto requeue; | |
1707 | ||
a6d2055b FF |
1708 | rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp; |
1709 | if (rs.rs_tstamp > tsf_lower && | |
1710 | unlikely(rs.rs_tstamp - tsf_lower > 0x10000000)) | |
1711 | rxs->mactime -= 0x100000000ULL; | |
1712 | ||
1713 | if (rs.rs_tstamp < tsf_lower && | |
1714 | unlikely(tsf_lower - rs.rs_tstamp > 0x10000000)) | |
1715 | rxs->mactime += 0x100000000ULL; | |
1716 | ||
cb71d9ba LR |
1717 | /* Ensure we always have an skb to requeue once we are done |
1718 | * processing the current buffer's skb */ | |
cc861f74 | 1719 | requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC); |
cb71d9ba LR |
1720 | |
1721 | /* If there is no memory we ignore the current RX'd frame, | |
1722 | * tell hardware it can give us a new frame using the old | |
b77f483f | 1723 | * skb and put it at the tail of the sc->rx.rxbuf list for |
cb71d9ba LR |
1724 | * processing. */ |
1725 | if (!requeue_skb) | |
1726 | goto requeue; | |
f078f209 | 1727 | |
9bf9fca8 | 1728 | /* Unmap the frame */ |
7da3c55c | 1729 | dma_unmap_single(sc->dev, bf->bf_buf_addr, |
cc861f74 | 1730 | common->rx_bufsize, |
b5c80475 | 1731 | dma_type); |
f078f209 | 1732 | |
b5c80475 FF |
1733 | skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len); |
1734 | if (ah->caps.rx_status_len) | |
1735 | skb_pull(skb, ah->caps.rx_status_len); | |
be0418ad | 1736 | |
d435700f S |
1737 | ath9k_rx_skb_postprocess(common, skb, &rs, |
1738 | rxs, decrypt_error); | |
be0418ad | 1739 | |
cb71d9ba LR |
1740 | /* We will now give hardware our shiny new allocated skb */ |
1741 | bf->bf_mpdu = requeue_skb; | |
7da3c55c | 1742 | bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data, |
cc861f74 | 1743 | common->rx_bufsize, |
b5c80475 | 1744 | dma_type); |
7da3c55c | 1745 | if (unlikely(dma_mapping_error(sc->dev, |
f8316df1 LR |
1746 | bf->bf_buf_addr))) { |
1747 | dev_kfree_skb_any(requeue_skb); | |
1748 | bf->bf_mpdu = NULL; | |
6cf9e995 | 1749 | bf->bf_buf_addr = 0; |
3800276a | 1750 | ath_err(common, "dma_mapping_error() on RX\n"); |
cae6b74d | 1751 | ath_rx_send_to_mac80211(hw, sc, skb); |
f8316df1 LR |
1752 | break; |
1753 | } | |
f078f209 LR |
1754 | |
1755 | /* | |
1756 | * change the default rx antenna if rx diversity chooses the | |
1757 | * other antenna 3 times in a row. | |
1758 | */ | |
29bffa96 | 1759 | if (sc->rx.defant != rs.rs_antenna) { |
b77f483f | 1760 | if (++sc->rx.rxotherant >= 3) |
29bffa96 | 1761 | ath_setdefantenna(sc, rs.rs_antenna); |
f078f209 | 1762 | } else { |
b77f483f | 1763 | sc->rx.rxotherant = 0; |
f078f209 | 1764 | } |
3cbb5dd7 | 1765 | |
8ab2cd09 | 1766 | spin_lock_irqsave(&sc->sc_pm_lock, flags); |
aaef24b4 MSS |
1767 | |
1768 | if ((sc->ps_flags & (PS_WAIT_FOR_BEACON | | |
ededf1f8 | 1769 | PS_WAIT_FOR_CAB | |
aaef24b4 MSS |
1770 | PS_WAIT_FOR_PSPOLL_DATA)) || |
1771 | unlikely(ath9k_check_auto_sleep(sc))) | |
cc65965c | 1772 | ath_rx_ps(sc, skb); |
8ab2cd09 | 1773 | spin_unlock_irqrestore(&sc->sc_pm_lock, flags); |
cc65965c | 1774 | |
102885a5 VT |
1775 | if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) |
1776 | ath_ant_comb_scan(sc, &rs); | |
1777 | ||
cae6b74d | 1778 | ath_rx_send_to_mac80211(hw, sc, skb); |
cc65965c | 1779 | |
cb71d9ba | 1780 | requeue: |
b5c80475 FF |
1781 | if (edma) { |
1782 | list_add_tail(&bf->list, &sc->rx.rxbuf); | |
1783 | ath_rx_edma_buf_link(sc, qtype); | |
1784 | } else { | |
1785 | list_move_tail(&bf->list, &sc->rx.rxbuf); | |
1786 | ath_rx_buf_link(sc, bf); | |
1787 | } | |
be0418ad S |
1788 | } while (1); |
1789 | ||
b77f483f | 1790 | spin_unlock_bh(&sc->rx.rxbuflock); |
f078f209 LR |
1791 | |
1792 | return 0; | |
f078f209 | 1793 | } |