• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008-2011 Atheros Communications Inc.
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 
17 #include <linux/dma-mapping.h>
18 #include <linux/relay.h>
19 #include "ath9k.h"
20 #include "ar9003_mac.h"
21 
22 #define SKB_CB_ATHBUF(__skb)	(*((struct ath_buf **)__skb->cb))
23 
ath9k_check_auto_sleep(struct ath_softc * sc)24 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
25 {
26 	return sc->ps_enabled &&
27 	       (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
28 }
29 
30 /*
31  * Setup and link descriptors.
32  *
33  * 11N: we can no longer afford to self link the last descriptor.
34  * MAC acknowledges BA status as long as it copies frames to host
35  * buffer (or rx fifo). This can incorrectly acknowledge packets
36  * to a sender if last desc is self-linked.
37  */
ath_rx_buf_link(struct ath_softc * sc,struct ath_buf * bf)38 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
39 {
40 	struct ath_hw *ah = sc->sc_ah;
41 	struct ath_common *common = ath9k_hw_common(ah);
42 	struct ath_desc *ds;
43 	struct sk_buff *skb;
44 
45 	ATH_RXBUF_RESET(bf);
46 
47 	ds = bf->bf_desc;
48 	ds->ds_link = 0; /* link to null */
49 	ds->ds_data = bf->bf_buf_addr;
50 
51 	/* virtual addr of the beginning of the buffer. */
52 	skb = bf->bf_mpdu;
53 	BUG_ON(skb == NULL);
54 	ds->ds_vdata = skb->data;
55 
56 	/*
57 	 * setup rx descriptors. The rx_bufsize here tells the hardware
58 	 * how much data it can DMA to us and that we are prepared
59 	 * to process
60 	 */
61 	ath9k_hw_setuprxdesc(ah, ds,
62 			     common->rx_bufsize,
63 			     0);
64 
65 	if (sc->rx.rxlink == NULL)
66 		ath9k_hw_putrxbuf(ah, bf->bf_daddr);
67 	else
68 		*sc->rx.rxlink = bf->bf_daddr;
69 
70 	sc->rx.rxlink = &ds->ds_link;
71 }
72 
ath_setdefantenna(struct ath_softc * sc,u32 antenna)73 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
74 {
75 	/* XXX block beacon interrupts */
76 	ath9k_hw_setantenna(sc->sc_ah, antenna);
77 	sc->rx.defant = antenna;
78 	sc->rx.rxotherant = 0;
79 }
80 
ath_opmode_init(struct ath_softc * sc)81 static void ath_opmode_init(struct ath_softc *sc)
82 {
83 	struct ath_hw *ah = sc->sc_ah;
84 	struct ath_common *common = ath9k_hw_common(ah);
85 
86 	u32 rfilt, mfilt[2];
87 
88 	/* configure rx filter */
89 	rfilt = ath_calcrxfilter(sc);
90 	ath9k_hw_setrxfilter(ah, rfilt);
91 
92 	/* configure bssid mask */
93 	ath_hw_setbssidmask(common);
94 
95 	/* configure operational mode */
96 	ath9k_hw_setopmode(ah);
97 
98 	/* calculate and install multicast filter */
99 	mfilt[0] = mfilt[1] = ~0;
100 	ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
101 }
102 
ath_rx_edma_buf_link(struct ath_softc * sc,enum ath9k_rx_qtype qtype)103 static bool ath_rx_edma_buf_link(struct ath_softc *sc,
104 				 enum ath9k_rx_qtype qtype)
105 {
106 	struct ath_hw *ah = sc->sc_ah;
107 	struct ath_rx_edma *rx_edma;
108 	struct sk_buff *skb;
109 	struct ath_buf *bf;
110 
111 	rx_edma = &sc->rx.rx_edma[qtype];
112 	if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
113 		return false;
114 
115 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
116 	list_del_init(&bf->list);
117 
118 	skb = bf->bf_mpdu;
119 
120 	ATH_RXBUF_RESET(bf);
121 	memset(skb->data, 0, ah->caps.rx_status_len);
122 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
123 				ah->caps.rx_status_len, DMA_TO_DEVICE);
124 
125 	SKB_CB_ATHBUF(skb) = bf;
126 	ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
127 	__skb_queue_tail(&rx_edma->rx_fifo, skb);
128 
129 	return true;
130 }
131 
ath_rx_addbuffer_edma(struct ath_softc * sc,enum ath9k_rx_qtype qtype)132 static void ath_rx_addbuffer_edma(struct ath_softc *sc,
133 				  enum ath9k_rx_qtype qtype)
134 {
135 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
136 	struct ath_buf *bf, *tbf;
137 
138 	if (list_empty(&sc->rx.rxbuf)) {
139 		ath_dbg(common, QUEUE, "No free rx buf available\n");
140 		return;
141 	}
142 
143 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
144 		if (!ath_rx_edma_buf_link(sc, qtype))
145 			break;
146 
147 }
148 
ath_rx_remove_buffer(struct ath_softc * sc,enum ath9k_rx_qtype qtype)149 static void ath_rx_remove_buffer(struct ath_softc *sc,
150 				 enum ath9k_rx_qtype qtype)
151 {
152 	struct ath_buf *bf;
153 	struct ath_rx_edma *rx_edma;
154 	struct sk_buff *skb;
155 
156 	rx_edma = &sc->rx.rx_edma[qtype];
157 
158 	while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
159 		bf = SKB_CB_ATHBUF(skb);
160 		BUG_ON(!bf);
161 		list_add_tail(&bf->list, &sc->rx.rxbuf);
162 	}
163 }
164 
ath_rx_edma_cleanup(struct ath_softc * sc)165 static void ath_rx_edma_cleanup(struct ath_softc *sc)
166 {
167 	struct ath_hw *ah = sc->sc_ah;
168 	struct ath_common *common = ath9k_hw_common(ah);
169 	struct ath_buf *bf;
170 
171 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
172 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
173 
174 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
175 		if (bf->bf_mpdu) {
176 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
177 					common->rx_bufsize,
178 					DMA_BIDIRECTIONAL);
179 			dev_kfree_skb_any(bf->bf_mpdu);
180 			bf->bf_buf_addr = 0;
181 			bf->bf_mpdu = NULL;
182 		}
183 	}
184 }
185 
ath_rx_edma_init_queue(struct ath_rx_edma * rx_edma,int size)186 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
187 {
188 	skb_queue_head_init(&rx_edma->rx_fifo);
189 	rx_edma->rx_fifo_hwsize = size;
190 }
191 
ath_rx_edma_init(struct ath_softc * sc,int nbufs)192 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
193 {
194 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
195 	struct ath_hw *ah = sc->sc_ah;
196 	struct sk_buff *skb;
197 	struct ath_buf *bf;
198 	int error = 0, i;
199 	u32 size;
200 
201 	ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
202 				    ah->caps.rx_status_len);
203 
204 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
205 			       ah->caps.rx_lp_qdepth);
206 	ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
207 			       ah->caps.rx_hp_qdepth);
208 
209 	size = sizeof(struct ath_buf) * nbufs;
210 	bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
211 	if (!bf)
212 		return -ENOMEM;
213 
214 	INIT_LIST_HEAD(&sc->rx.rxbuf);
215 
216 	for (i = 0; i < nbufs; i++, bf++) {
217 		skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
218 		if (!skb) {
219 			error = -ENOMEM;
220 			goto rx_init_fail;
221 		}
222 
223 		memset(skb->data, 0, common->rx_bufsize);
224 		bf->bf_mpdu = skb;
225 
226 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
227 						 common->rx_bufsize,
228 						 DMA_BIDIRECTIONAL);
229 		if (unlikely(dma_mapping_error(sc->dev,
230 						bf->bf_buf_addr))) {
231 				dev_kfree_skb_any(skb);
232 				bf->bf_mpdu = NULL;
233 				bf->bf_buf_addr = 0;
234 				ath_err(common,
235 					"dma_mapping_error() on RX init\n");
236 				error = -ENOMEM;
237 				goto rx_init_fail;
238 		}
239 
240 		list_add_tail(&bf->list, &sc->rx.rxbuf);
241 	}
242 
243 	return 0;
244 
245 rx_init_fail:
246 	ath_rx_edma_cleanup(sc);
247 	return error;
248 }
249 
ath_edma_start_recv(struct ath_softc * sc)250 static void ath_edma_start_recv(struct ath_softc *sc)
251 {
252 	ath9k_hw_rxena(sc->sc_ah);
253 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
254 	ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
255 	ath_opmode_init(sc);
256 	ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
257 }
258 
ath_edma_stop_recv(struct ath_softc * sc)259 static void ath_edma_stop_recv(struct ath_softc *sc)
260 {
261 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
262 	ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
263 }
264 
ath_rx_init(struct ath_softc * sc,int nbufs)265 int ath_rx_init(struct ath_softc *sc, int nbufs)
266 {
267 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
268 	struct sk_buff *skb;
269 	struct ath_buf *bf;
270 	int error = 0;
271 
272 	spin_lock_init(&sc->sc_pcu_lock);
273 
274 	common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
275 			     sc->sc_ah->caps.rx_status_len;
276 
277 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
278 		return ath_rx_edma_init(sc, nbufs);
279 
280 	ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
281 		common->cachelsz, common->rx_bufsize);
282 
283 	/* Initialize rx descriptors */
284 
285 	error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
286 				  "rx", nbufs, 1, 0);
287 	if (error != 0) {
288 		ath_err(common,
289 			"failed to allocate rx descriptors: %d\n",
290 			error);
291 		goto err;
292 	}
293 
294 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
295 		skb = ath_rxbuf_alloc(common, common->rx_bufsize,
296 				      GFP_KERNEL);
297 		if (skb == NULL) {
298 			error = -ENOMEM;
299 			goto err;
300 		}
301 
302 		bf->bf_mpdu = skb;
303 		bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
304 						 common->rx_bufsize,
305 						 DMA_FROM_DEVICE);
306 		if (unlikely(dma_mapping_error(sc->dev,
307 					       bf->bf_buf_addr))) {
308 			dev_kfree_skb_any(skb);
309 			bf->bf_mpdu = NULL;
310 			bf->bf_buf_addr = 0;
311 			ath_err(common,
312 				"dma_mapping_error() on RX init\n");
313 			error = -ENOMEM;
314 			goto err;
315 		}
316 	}
317 	sc->rx.rxlink = NULL;
318 err:
319 	if (error)
320 		ath_rx_cleanup(sc);
321 
322 	return error;
323 }
324 
ath_rx_cleanup(struct ath_softc * sc)325 void ath_rx_cleanup(struct ath_softc *sc)
326 {
327 	struct ath_hw *ah = sc->sc_ah;
328 	struct ath_common *common = ath9k_hw_common(ah);
329 	struct sk_buff *skb;
330 	struct ath_buf *bf;
331 
332 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
333 		ath_rx_edma_cleanup(sc);
334 		return;
335 	}
336 
337 	list_for_each_entry(bf, &sc->rx.rxbuf, list) {
338 		skb = bf->bf_mpdu;
339 		if (skb) {
340 			dma_unmap_single(sc->dev, bf->bf_buf_addr,
341 					 common->rx_bufsize,
342 					 DMA_FROM_DEVICE);
343 			dev_kfree_skb(skb);
344 			bf->bf_buf_addr = 0;
345 			bf->bf_mpdu = NULL;
346 		}
347 	}
348 }
349 
350 /*
351  * Calculate the receive filter according to the
352  * operating mode and state:
353  *
354  * o always accept unicast, broadcast, and multicast traffic
355  * o maintain current state of phy error reception (the hal
356  *   may enable phy error frames for noise immunity work)
357  * o probe request frames are accepted only when operating in
358  *   hostap, adhoc, or monitor modes
359  * o enable promiscuous mode according to the interface state
360  * o accept beacons:
361  *   - when operating in adhoc mode so the 802.11 layer creates
362  *     node table entries for peers,
363  *   - when operating in station mode for collecting rssi data when
364  *     the station is otherwise quiet, or
365  *   - when operating as a repeater so we see repeater-sta beacons
366  *   - when scanning
367  */
368 
ath_calcrxfilter(struct ath_softc * sc)369 u32 ath_calcrxfilter(struct ath_softc *sc)
370 {
371 	u32 rfilt;
372 
373 	rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
374 		| ATH9K_RX_FILTER_MCAST;
375 
376 	/* if operating on a DFS channel, enable radar pulse detection */
377 	if (sc->hw->conf.radar_enabled)
378 		rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
379 
380 	if (sc->rx.rxfilter & FIF_PROBE_REQ)
381 		rfilt |= ATH9K_RX_FILTER_PROBEREQ;
382 
383 	/*
384 	 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
385 	 * mode interface or when in monitor mode. AP mode does not need this
386 	 * since it receives all in-BSS frames anyway.
387 	 */
388 	if (sc->sc_ah->is_monitoring)
389 		rfilt |= ATH9K_RX_FILTER_PROM;
390 
391 	if (sc->rx.rxfilter & FIF_CONTROL)
392 		rfilt |= ATH9K_RX_FILTER_CONTROL;
393 
394 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
395 	    (sc->nvifs <= 1) &&
396 	    !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
397 		rfilt |= ATH9K_RX_FILTER_MYBEACON;
398 	else
399 		rfilt |= ATH9K_RX_FILTER_BEACON;
400 
401 	if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
402 	    (sc->rx.rxfilter & FIF_PSPOLL))
403 		rfilt |= ATH9K_RX_FILTER_PSPOLL;
404 
405 	if (conf_is_ht(&sc->hw->conf))
406 		rfilt |= ATH9K_RX_FILTER_COMP_BAR;
407 
408 	if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
409 		/* This is needed for older chips */
410 		if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
411 			rfilt |= ATH9K_RX_FILTER_PROM;
412 		rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
413 	}
414 
415 	if (AR_SREV_9550(sc->sc_ah))
416 		rfilt |= ATH9K_RX_FILTER_4ADDRESS;
417 
418 	return rfilt;
419 
420 }
421 
ath_startrecv(struct ath_softc * sc)422 int ath_startrecv(struct ath_softc *sc)
423 {
424 	struct ath_hw *ah = sc->sc_ah;
425 	struct ath_buf *bf, *tbf;
426 
427 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
428 		ath_edma_start_recv(sc);
429 		return 0;
430 	}
431 
432 	if (list_empty(&sc->rx.rxbuf))
433 		goto start_recv;
434 
435 	sc->rx.rxlink = NULL;
436 	list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
437 		ath_rx_buf_link(sc, bf);
438 	}
439 
440 	/* We could have deleted elements so the list may be empty now */
441 	if (list_empty(&sc->rx.rxbuf))
442 		goto start_recv;
443 
444 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
445 	ath9k_hw_putrxbuf(ah, bf->bf_daddr);
446 	ath9k_hw_rxena(ah);
447 
448 start_recv:
449 	ath_opmode_init(sc);
450 	ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
451 
452 	return 0;
453 }
454 
ath_flushrecv(struct ath_softc * sc)455 static void ath_flushrecv(struct ath_softc *sc)
456 {
457 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
458 		ath_rx_tasklet(sc, 1, true);
459 	ath_rx_tasklet(sc, 1, false);
460 }
461 
ath_stoprecv(struct ath_softc * sc)462 bool ath_stoprecv(struct ath_softc *sc)
463 {
464 	struct ath_hw *ah = sc->sc_ah;
465 	bool stopped, reset = false;
466 
467 	ath9k_hw_abortpcurecv(ah);
468 	ath9k_hw_setrxfilter(ah, 0);
469 	stopped = ath9k_hw_stopdmarecv(ah, &reset);
470 
471 	ath_flushrecv(sc);
472 
473 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
474 		ath_edma_stop_recv(sc);
475 	else
476 		sc->rx.rxlink = NULL;
477 
478 	if (!(ah->ah_flags & AH_UNPLUGGED) &&
479 	    unlikely(!stopped)) {
480 		ath_err(ath9k_hw_common(sc->sc_ah),
481 			"Could not stop RX, we could be "
482 			"confusing the DMA engine when we start RX up\n");
483 		ATH_DBG_WARN_ON_ONCE(!stopped);
484 	}
485 	return stopped && !reset;
486 }
487 
ath_beacon_dtim_pending_cab(struct sk_buff * skb)488 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
489 {
490 	/* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
491 	struct ieee80211_mgmt *mgmt;
492 	u8 *pos, *end, id, elen;
493 	struct ieee80211_tim_ie *tim;
494 
495 	mgmt = (struct ieee80211_mgmt *)skb->data;
496 	pos = mgmt->u.beacon.variable;
497 	end = skb->data + skb->len;
498 
499 	while (pos + 2 < end) {
500 		id = *pos++;
501 		elen = *pos++;
502 		if (pos + elen > end)
503 			break;
504 
505 		if (id == WLAN_EID_TIM) {
506 			if (elen < sizeof(*tim))
507 				break;
508 			tim = (struct ieee80211_tim_ie *) pos;
509 			if (tim->dtim_count != 0)
510 				break;
511 			return tim->bitmap_ctrl & 0x01;
512 		}
513 
514 		pos += elen;
515 	}
516 
517 	return false;
518 }
519 
ath_rx_ps_beacon(struct ath_softc * sc,struct sk_buff * skb)520 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
521 {
522 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
523 
524 	if (skb->len < 24 + 8 + 2 + 2)
525 		return;
526 
527 	sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
528 
529 	if (sc->ps_flags & PS_BEACON_SYNC) {
530 		sc->ps_flags &= ~PS_BEACON_SYNC;
531 		ath_dbg(common, PS,
532 			"Reconfigure beacon timers based on synchronized timestamp\n");
533 		ath9k_set_beacon(sc);
534 	}
535 
536 	if (ath_beacon_dtim_pending_cab(skb)) {
537 		/*
538 		 * Remain awake waiting for buffered broadcast/multicast
539 		 * frames. If the last broadcast/multicast frame is not
540 		 * received properly, the next beacon frame will work as
541 		 * a backup trigger for returning into NETWORK SLEEP state,
542 		 * so we are waiting for it as well.
543 		 */
544 		ath_dbg(common, PS,
545 			"Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
546 		sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
547 		return;
548 	}
549 
550 	if (sc->ps_flags & PS_WAIT_FOR_CAB) {
551 		/*
552 		 * This can happen if a broadcast frame is dropped or the AP
553 		 * fails to send a frame indicating that all CAB frames have
554 		 * been delivered.
555 		 */
556 		sc->ps_flags &= ~PS_WAIT_FOR_CAB;
557 		ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
558 	}
559 }
560 
ath_rx_ps(struct ath_softc * sc,struct sk_buff * skb,bool mybeacon)561 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
562 {
563 	struct ieee80211_hdr *hdr;
564 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
565 
566 	hdr = (struct ieee80211_hdr *)skb->data;
567 
568 	/* Process Beacon and CAB receive in PS state */
569 	if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
570 	    && mybeacon) {
571 		ath_rx_ps_beacon(sc, skb);
572 	} else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
573 		   (ieee80211_is_data(hdr->frame_control) ||
574 		    ieee80211_is_action(hdr->frame_control)) &&
575 		   is_multicast_ether_addr(hdr->addr1) &&
576 		   !ieee80211_has_moredata(hdr->frame_control)) {
577 		/*
578 		 * No more broadcast/multicast frames to be received at this
579 		 * point.
580 		 */
581 		sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
582 		ath_dbg(common, PS,
583 			"All PS CAB frames received, back to sleep\n");
584 	} else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
585 		   !is_multicast_ether_addr(hdr->addr1) &&
586 		   !ieee80211_has_morefrags(hdr->frame_control)) {
587 		sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
588 		ath_dbg(common, PS,
589 			"Going back to sleep after having received PS-Poll data (0x%lx)\n",
590 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
591 					PS_WAIT_FOR_CAB |
592 					PS_WAIT_FOR_PSPOLL_DATA |
593 					PS_WAIT_FOR_TX_ACK));
594 	}
595 }
596 
ath_edma_get_buffers(struct ath_softc * sc,enum ath9k_rx_qtype qtype,struct ath_rx_status * rs,struct ath_buf ** dest)597 static bool ath_edma_get_buffers(struct ath_softc *sc,
598 				 enum ath9k_rx_qtype qtype,
599 				 struct ath_rx_status *rs,
600 				 struct ath_buf **dest)
601 {
602 	struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
603 	struct ath_hw *ah = sc->sc_ah;
604 	struct ath_common *common = ath9k_hw_common(ah);
605 	struct sk_buff *skb;
606 	struct ath_buf *bf;
607 	int ret;
608 
609 	skb = skb_peek(&rx_edma->rx_fifo);
610 	if (!skb)
611 		return false;
612 
613 	bf = SKB_CB_ATHBUF(skb);
614 	BUG_ON(!bf);
615 
616 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
617 				common->rx_bufsize, DMA_FROM_DEVICE);
618 
619 	ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
620 	if (ret == -EINPROGRESS) {
621 		/*let device gain the buffer again*/
622 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
623 				common->rx_bufsize, DMA_FROM_DEVICE);
624 		return false;
625 	}
626 
627 	__skb_unlink(skb, &rx_edma->rx_fifo);
628 	if (ret == -EINVAL) {
629 		/* corrupt descriptor, skip this one and the following one */
630 		list_add_tail(&bf->list, &sc->rx.rxbuf);
631 		ath_rx_edma_buf_link(sc, qtype);
632 
633 		skb = skb_peek(&rx_edma->rx_fifo);
634 		if (skb) {
635 			bf = SKB_CB_ATHBUF(skb);
636 			BUG_ON(!bf);
637 
638 			__skb_unlink(skb, &rx_edma->rx_fifo);
639 			list_add_tail(&bf->list, &sc->rx.rxbuf);
640 			ath_rx_edma_buf_link(sc, qtype);
641 		}
642 
643 		bf = NULL;
644 	}
645 
646 	*dest = bf;
647 	return true;
648 }
649 
ath_edma_get_next_rx_buf(struct ath_softc * sc,struct ath_rx_status * rs,enum ath9k_rx_qtype qtype)650 static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
651 						struct ath_rx_status *rs,
652 						enum ath9k_rx_qtype qtype)
653 {
654 	struct ath_buf *bf = NULL;
655 
656 	while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
657 		if (!bf)
658 			continue;
659 
660 		return bf;
661 	}
662 	return NULL;
663 }
664 
ath_get_next_rx_buf(struct ath_softc * sc,struct ath_rx_status * rs)665 static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
666 					   struct ath_rx_status *rs)
667 {
668 	struct ath_hw *ah = sc->sc_ah;
669 	struct ath_common *common = ath9k_hw_common(ah);
670 	struct ath_desc *ds;
671 	struct ath_buf *bf;
672 	int ret;
673 
674 	if (list_empty(&sc->rx.rxbuf)) {
675 		sc->rx.rxlink = NULL;
676 		return NULL;
677 	}
678 
679 	bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
680 	ds = bf->bf_desc;
681 
682 	/*
683 	 * Must provide the virtual address of the current
684 	 * descriptor, the physical address, and the virtual
685 	 * address of the next descriptor in the h/w chain.
686 	 * This allows the HAL to look ahead to see if the
687 	 * hardware is done with a descriptor by checking the
688 	 * done bit in the following descriptor and the address
689 	 * of the current descriptor the DMA engine is working
690 	 * on.  All this is necessary because of our use of
691 	 * a self-linked list to avoid rx overruns.
692 	 */
693 	ret = ath9k_hw_rxprocdesc(ah, ds, rs);
694 	if (ret == -EINPROGRESS) {
695 		struct ath_rx_status trs;
696 		struct ath_buf *tbf;
697 		struct ath_desc *tds;
698 
699 		memset(&trs, 0, sizeof(trs));
700 		if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
701 			sc->rx.rxlink = NULL;
702 			return NULL;
703 		}
704 
705 		tbf = list_entry(bf->list.next, struct ath_buf, list);
706 
707 		/*
708 		 * On some hardware the descriptor status words could
709 		 * get corrupted, including the done bit. Because of
710 		 * this, check if the next descriptor's done bit is
711 		 * set or not.
712 		 *
713 		 * If the next descriptor's done bit is set, the current
714 		 * descriptor has been corrupted. Force s/w to discard
715 		 * this descriptor and continue...
716 		 */
717 
718 		tds = tbf->bf_desc;
719 		ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
720 		if (ret == -EINPROGRESS)
721 			return NULL;
722 
723 		/*
724 		 * mark descriptor as zero-length and set the 'more'
725 		 * flag to ensure that both buffers get discarded
726 		 */
727 		rs->rs_datalen = 0;
728 		rs->rs_more = true;
729 	}
730 
731 	list_del(&bf->list);
732 	if (!bf->bf_mpdu)
733 		return bf;
734 
735 	/*
736 	 * Synchronize the DMA transfer with CPU before
737 	 * 1. accessing the frame
738 	 * 2. requeueing the same buffer to h/w
739 	 */
740 	dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
741 			common->rx_bufsize,
742 			DMA_FROM_DEVICE);
743 
744 	return bf;
745 }
746 
747 /* Assumes you've already done the endian to CPU conversion */
ath9k_rx_accept(struct ath_common * common,struct ieee80211_hdr * hdr,struct ieee80211_rx_status * rxs,struct ath_rx_status * rx_stats,bool * decrypt_error)748 static bool ath9k_rx_accept(struct ath_common *common,
749 			    struct ieee80211_hdr *hdr,
750 			    struct ieee80211_rx_status *rxs,
751 			    struct ath_rx_status *rx_stats,
752 			    bool *decrypt_error)
753 {
754 	struct ath_softc *sc = (struct ath_softc *) common->priv;
755 	bool is_mc, is_valid_tkip, strip_mic, mic_error;
756 	struct ath_hw *ah = common->ah;
757 	__le16 fc;
758 	u8 rx_status_len = ah->caps.rx_status_len;
759 
760 	fc = hdr->frame_control;
761 
762 	is_mc = !!is_multicast_ether_addr(hdr->addr1);
763 	is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
764 		test_bit(rx_stats->rs_keyix, common->tkip_keymap);
765 	strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
766 		ieee80211_has_protected(fc) &&
767 		!(rx_stats->rs_status &
768 		(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
769 		 ATH9K_RXERR_KEYMISS));
770 
771 	/*
772 	 * Key miss events are only relevant for pairwise keys where the
773 	 * descriptor does contain a valid key index. This has been observed
774 	 * mostly with CCMP encryption.
775 	 */
776 	if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
777 	    !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
778 		rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
779 
780 	if (!rx_stats->rs_datalen) {
781 		RX_STAT_INC(rx_len_err);
782 		return false;
783 	}
784 
785         /*
786          * rs_status follows rs_datalen so if rs_datalen is too large
787          * we can take a hint that hardware corrupted it, so ignore
788          * those frames.
789          */
790 	if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
791 		RX_STAT_INC(rx_len_err);
792 		return false;
793 	}
794 
795 	/* Only use error bits from the last fragment */
796 	if (rx_stats->rs_more)
797 		return true;
798 
799 	mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
800 		!ieee80211_has_morefrags(fc) &&
801 		!(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
802 		(rx_stats->rs_status & ATH9K_RXERR_MIC);
803 
804 	/*
805 	 * The rx_stats->rs_status will not be set until the end of the
806 	 * chained descriptors so it can be ignored if rs_more is set. The
807 	 * rs_more will be false at the last element of the chained
808 	 * descriptors.
809 	 */
810 	if (rx_stats->rs_status != 0) {
811 		u8 status_mask;
812 
813 		if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
814 			rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
815 			mic_error = false;
816 		}
817 		if (rx_stats->rs_status & ATH9K_RXERR_PHY)
818 			return false;
819 
820 		if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
821 		    (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
822 			*decrypt_error = true;
823 			mic_error = false;
824 		}
825 
826 		/*
827 		 * Reject error frames with the exception of
828 		 * decryption and MIC failures. For monitor mode,
829 		 * we also ignore the CRC error.
830 		 */
831 		status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
832 			      ATH9K_RXERR_KEYMISS;
833 
834 		if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
835 			status_mask |= ATH9K_RXERR_CRC;
836 
837 		if (rx_stats->rs_status & ~status_mask)
838 			return false;
839 	}
840 
841 	/*
842 	 * For unicast frames the MIC error bit can have false positives,
843 	 * so all MIC error reports need to be validated in software.
844 	 * False negatives are not common, so skip software verification
845 	 * if the hardware considers the MIC valid.
846 	 */
847 	if (strip_mic)
848 		rxs->flag |= RX_FLAG_MMIC_STRIPPED;
849 	else if (is_mc && mic_error)
850 		rxs->flag |= RX_FLAG_MMIC_ERROR;
851 
852 	return true;
853 }
854 
ath9k_process_rate(struct ath_common * common,struct ieee80211_hw * hw,struct ath_rx_status * rx_stats,struct ieee80211_rx_status * rxs)855 static int ath9k_process_rate(struct ath_common *common,
856 			      struct ieee80211_hw *hw,
857 			      struct ath_rx_status *rx_stats,
858 			      struct ieee80211_rx_status *rxs)
859 {
860 	struct ieee80211_supported_band *sband;
861 	enum ieee80211_band band;
862 	unsigned int i = 0;
863 	struct ath_softc __maybe_unused *sc = common->priv;
864 
865 	band = hw->conf.chandef.chan->band;
866 	sband = hw->wiphy->bands[band];
867 
868 	if (rx_stats->rs_rate & 0x80) {
869 		/* HT rate */
870 		rxs->flag |= RX_FLAG_HT;
871 		if (rx_stats->rs_flags & ATH9K_RX_2040)
872 			rxs->flag |= RX_FLAG_40MHZ;
873 		if (rx_stats->rs_flags & ATH9K_RX_GI)
874 			rxs->flag |= RX_FLAG_SHORT_GI;
875 		rxs->rate_idx = rx_stats->rs_rate & 0x7f;
876 		return 0;
877 	}
878 
879 	for (i = 0; i < sband->n_bitrates; i++) {
880 		if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
881 			rxs->rate_idx = i;
882 			return 0;
883 		}
884 		if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
885 			rxs->flag |= RX_FLAG_SHORTPRE;
886 			rxs->rate_idx = i;
887 			return 0;
888 		}
889 	}
890 
891 	/*
892 	 * No valid hardware bitrate found -- we should not get here
893 	 * because hardware has already validated this frame as OK.
894 	 */
895 	ath_dbg(common, ANY,
896 		"unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
897 		rx_stats->rs_rate);
898 	RX_STAT_INC(rx_rate_err);
899 	return -EINVAL;
900 }
901 
ath9k_process_rssi(struct ath_common * common,struct ieee80211_hw * hw,struct ieee80211_hdr * hdr,struct ath_rx_status * rx_stats)902 static void ath9k_process_rssi(struct ath_common *common,
903 			       struct ieee80211_hw *hw,
904 			       struct ieee80211_hdr *hdr,
905 			       struct ath_rx_status *rx_stats)
906 {
907 	struct ath_softc *sc = hw->priv;
908 	struct ath_hw *ah = common->ah;
909 	int last_rssi;
910 	int rssi = rx_stats->rs_rssi;
911 
912 	if (!rx_stats->is_mybeacon ||
913 	    ((ah->opmode != NL80211_IFTYPE_STATION) &&
914 	     (ah->opmode != NL80211_IFTYPE_ADHOC)))
915 		return;
916 
917 	if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
918 		ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
919 
920 	last_rssi = sc->last_rssi;
921 	if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
922 		rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
923 	if (rssi < 0)
924 		rssi = 0;
925 
926 	/* Update Beacon RSSI, this is used by ANI. */
927 	ah->stats.avgbrssi = rssi;
928 }
929 
930 /*
931  * For Decrypt or Demic errors, we only mark packet status here and always push
932  * up the frame up to let mac80211 handle the actual error case, be it no
933  * decryption key or real decryption error. This let us keep statistics there.
934  */
ath9k_rx_skb_preprocess(struct ath_softc * sc,struct ieee80211_hdr * hdr,struct ath_rx_status * rx_stats,struct ieee80211_rx_status * rx_status,bool * decrypt_error)935 static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
936 				   struct ieee80211_hdr *hdr,
937 				   struct ath_rx_status *rx_stats,
938 				   struct ieee80211_rx_status *rx_status,
939 				   bool *decrypt_error)
940 {
941 	struct ieee80211_hw *hw = sc->hw;
942 	struct ath_hw *ah = sc->sc_ah;
943 	struct ath_common *common = ath9k_hw_common(ah);
944 	bool discard_current = sc->rx.discard_next;
945 
946 	sc->rx.discard_next = rx_stats->rs_more;
947 	if (discard_current)
948 		return -EINVAL;
949 
950 	/*
951 	 * everything but the rate is checked here, the rate check is done
952 	 * separately to avoid doing two lookups for a rate for each frame.
953 	 */
954 	if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
955 		return -EINVAL;
956 
957 	/* Only use status info from the last fragment */
958 	if (rx_stats->rs_more)
959 		return 0;
960 
961 	ath9k_process_rssi(common, hw, hdr, rx_stats);
962 
963 	if (ath9k_process_rate(common, hw, rx_stats, rx_status))
964 		return -EINVAL;
965 
966 	rx_status->band = hw->conf.chandef.chan->band;
967 	rx_status->freq = hw->conf.chandef.chan->center_freq;
968 	rx_status->signal = ah->noise + rx_stats->rs_rssi;
969 	rx_status->antenna = rx_stats->rs_antenna;
970 	rx_status->flag |= RX_FLAG_MACTIME_END;
971 	if (rx_stats->rs_moreaggr)
972 		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
973 
974 	sc->rx.discard_next = false;
975 	return 0;
976 }
977 
ath9k_rx_skb_postprocess(struct ath_common * common,struct sk_buff * skb,struct ath_rx_status * rx_stats,struct ieee80211_rx_status * rxs,bool decrypt_error)978 static void ath9k_rx_skb_postprocess(struct ath_common *common,
979 				     struct sk_buff *skb,
980 				     struct ath_rx_status *rx_stats,
981 				     struct ieee80211_rx_status *rxs,
982 				     bool decrypt_error)
983 {
984 	struct ath_hw *ah = common->ah;
985 	struct ieee80211_hdr *hdr;
986 	int hdrlen, padpos, padsize;
987 	u8 keyix;
988 	__le16 fc;
989 
990 	/* see if any padding is done by the hw and remove it */
991 	hdr = (struct ieee80211_hdr *) skb->data;
992 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
993 	fc = hdr->frame_control;
994 	padpos = ieee80211_hdrlen(fc);
995 
996 	/* The MAC header is padded to have 32-bit boundary if the
997 	 * packet payload is non-zero. The general calculation for
998 	 * padsize would take into account odd header lengths:
999 	 * padsize = (4 - padpos % 4) % 4; However, since only
1000 	 * even-length headers are used, padding can only be 0 or 2
1001 	 * bytes and we can optimize this a bit. In addition, we must
1002 	 * not try to remove padding from short control frames that do
1003 	 * not have payload. */
1004 	padsize = padpos & 3;
1005 	if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1006 		memmove(skb->data + padsize, skb->data, padpos);
1007 		skb_pull(skb, padsize);
1008 	}
1009 
1010 	keyix = rx_stats->rs_keyix;
1011 
1012 	if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1013 	    ieee80211_has_protected(fc)) {
1014 		rxs->flag |= RX_FLAG_DECRYPTED;
1015 	} else if (ieee80211_has_protected(fc)
1016 		   && !decrypt_error && skb->len >= hdrlen + 4) {
1017 		keyix = skb->data[hdrlen + 3] >> 6;
1018 
1019 		if (test_bit(keyix, common->keymap))
1020 			rxs->flag |= RX_FLAG_DECRYPTED;
1021 	}
1022 	if (ah->sw_mgmt_crypto &&
1023 	    (rxs->flag & RX_FLAG_DECRYPTED) &&
1024 	    ieee80211_is_mgmt(fc))
1025 		/* Use software decrypt for management frames. */
1026 		rxs->flag &= ~RX_FLAG_DECRYPTED;
1027 }
1028 
1029 #ifdef CONFIG_ATH9K_DEBUGFS
fix_rssi_inv_only(u8 rssi_val)1030 static s8 fix_rssi_inv_only(u8 rssi_val)
1031 {
1032 	if (rssi_val == 128)
1033 		rssi_val = 0;
1034 	return (s8) rssi_val;
1035 }
1036 #endif
1037 
1038 /* returns 1 if this was a spectral frame, even if not handled. */
ath_process_fft(struct ath_softc * sc,struct ieee80211_hdr * hdr,struct ath_rx_status * rs,u64 tsf)1039 static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
1040 			   struct ath_rx_status *rs, u64 tsf)
1041 {
1042 #ifdef CONFIG_ATH9K_DEBUGFS
1043 	struct ath_hw *ah = sc->sc_ah;
1044 	u8 bins[SPECTRAL_HT20_NUM_BINS];
1045 	u8 *vdata = (u8 *)hdr;
1046 	struct fft_sample_ht20 fft_sample;
1047 	struct ath_radar_info *radar_info;
1048 	struct ath_ht20_mag_info *mag_info;
1049 	int len = rs->rs_datalen;
1050 	int dc_pos;
1051 	u16 length, max_magnitude;
1052 
1053 	/* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
1054 	 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
1055 	 * yet, but this is supposed to be possible as well.
1056 	 */
1057 	if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
1058 	    rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
1059 	    rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
1060 		return 0;
1061 
1062 	/* check if spectral scan bit is set. This does not have to be checked
1063 	 * if received through a SPECTRAL phy error, but shouldn't hurt.
1064 	 */
1065 	radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
1066 	if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
1067 		return 0;
1068 
1069 	/* Variation in the data length is possible and will be fixed later.
1070 	 * Note that we only support HT20 for now.
1071 	 *
1072 	 * TODO: add HT20_40 support as well.
1073 	 */
1074 	if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1075 	    (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
1076 		return 1;
1077 
1078 	fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
1079 	length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1080 	fft_sample.tlv.length = __cpu_to_be16(length);
1081 
1082 	fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
1083 	fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1084 	fft_sample.noise = ah->noise;
1085 
1086 	switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1087 	case 0:
1088 		/* length correct, nothing to do. */
1089 		memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1090 		break;
1091 	case -1:
1092 		/* first byte missing, duplicate it. */
1093 		memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1094 		bins[0] = vdata[0];
1095 		break;
1096 	case 2:
1097 		/* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1098 		memcpy(bins, vdata, 30);
1099 		bins[30] = vdata[31];
1100 		memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1101 		break;
1102 	case 1:
1103 		/* MAC added 2 extra bytes AND first byte is missing. */
1104 		bins[0] = vdata[0];
1105 		memcpy(&bins[0], vdata, 30);
1106 		bins[31] = vdata[31];
1107 		memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1108 		break;
1109 	default:
1110 		return 1;
1111 	}
1112 
1113 	/* DC value (value in the middle) is the blind spot of the spectral
1114 	 * sample and invalid, interpolate it.
1115 	 */
1116 	dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1117 	bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1118 
1119 	/* mag data is at the end of the frame, in front of radar_info */
1120 	mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1121 
1122 	/* copy raw bins without scaling them */
1123 	memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
1124 	fft_sample.max_exp = mag_info->max_exp & 0xf;
1125 
1126 	max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1127 	fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
1128 	fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1129 	fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
1130 	fft_sample.tsf = __cpu_to_be64(tsf);
1131 
1132 	ath_debug_send_fft_sample(sc, &fft_sample.tlv);
1133 	return 1;
1134 #else
1135 	return 0;
1136 #endif
1137 }
1138 
ath9k_apply_ampdu_details(struct ath_softc * sc,struct ath_rx_status * rs,struct ieee80211_rx_status * rxs)1139 static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1140 	struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1141 {
1142 	if (rs->rs_isaggr) {
1143 		rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1144 
1145 		rxs->ampdu_reference = sc->rx.ampdu_ref;
1146 
1147 		if (!rs->rs_moreaggr) {
1148 			rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1149 			sc->rx.ampdu_ref++;
1150 		}
1151 
1152 		if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1153 			rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1154 	}
1155 }
1156 
ath_rx_tasklet(struct ath_softc * sc,int flush,bool hp)1157 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1158 {
1159 	struct ath_buf *bf;
1160 	struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1161 	struct ieee80211_rx_status *rxs;
1162 	struct ath_hw *ah = sc->sc_ah;
1163 	struct ath_common *common = ath9k_hw_common(ah);
1164 	struct ieee80211_hw *hw = sc->hw;
1165 	struct ieee80211_hdr *hdr;
1166 	int retval;
1167 	struct ath_rx_status rs;
1168 	enum ath9k_rx_qtype qtype;
1169 	bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1170 	int dma_type;
1171 	u8 rx_status_len = ah->caps.rx_status_len;
1172 	u64 tsf = 0;
1173 	u32 tsf_lower = 0;
1174 	unsigned long flags;
1175 	dma_addr_t new_buf_addr;
1176 
1177 	if (edma)
1178 		dma_type = DMA_BIDIRECTIONAL;
1179 	else
1180 		dma_type = DMA_FROM_DEVICE;
1181 
1182 	qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1183 
1184 	tsf = ath9k_hw_gettsf64(ah);
1185 	tsf_lower = tsf & 0xffffffff;
1186 
1187 	do {
1188 		bool decrypt_error = false;
1189 
1190 		memset(&rs, 0, sizeof(rs));
1191 		if (edma)
1192 			bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1193 		else
1194 			bf = ath_get_next_rx_buf(sc, &rs);
1195 
1196 		if (!bf)
1197 			break;
1198 
1199 		skb = bf->bf_mpdu;
1200 		if (!skb)
1201 			continue;
1202 
1203 		/*
1204 		 * Take frame header from the first fragment and RX status from
1205 		 * the last one.
1206 		 */
1207 		if (sc->rx.frag)
1208 			hdr_skb = sc->rx.frag;
1209 		else
1210 			hdr_skb = skb;
1211 
1212 		hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1213 		rxs = IEEE80211_SKB_RXCB(hdr_skb);
1214 		if (ieee80211_is_beacon(hdr->frame_control)) {
1215 			RX_STAT_INC(rx_beacons);
1216 			if (!is_zero_ether_addr(common->curbssid) &&
1217 			    ether_addr_equal(hdr->addr3, common->curbssid))
1218 				rs.is_mybeacon = true;
1219 			else
1220 				rs.is_mybeacon = false;
1221 		}
1222 		else
1223 			rs.is_mybeacon = false;
1224 
1225 		if (ieee80211_is_data_present(hdr->frame_control) &&
1226 		    !ieee80211_is_qos_nullfunc(hdr->frame_control))
1227 			sc->rx.num_pkts++;
1228 
1229 		ath_debug_stat_rx(sc, &rs);
1230 
1231 		memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1232 
1233 		rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1234 		if (rs.rs_tstamp > tsf_lower &&
1235 		    unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1236 			rxs->mactime -= 0x100000000ULL;
1237 
1238 		if (rs.rs_tstamp < tsf_lower &&
1239 		    unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1240 			rxs->mactime += 0x100000000ULL;
1241 
1242 		if (rs.rs_phyerr == ATH9K_PHYERR_RADAR)
1243 			ath9k_dfs_process_phyerr(sc, hdr, &rs, rxs->mactime);
1244 
1245 		if (rs.rs_status & ATH9K_RXERR_PHY) {
1246 			if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
1247 				RX_STAT_INC(rx_spectral);
1248 				goto requeue_drop_frag;
1249 			}
1250 		}
1251 
1252 		retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
1253 						 &decrypt_error);
1254 		if (retval)
1255 			goto requeue_drop_frag;
1256 
1257 		if (rs.is_mybeacon) {
1258 			sc->hw_busy_count = 0;
1259 			ath_start_rx_poll(sc, 3);
1260 		}
1261 		/* Ensure we always have an skb to requeue once we are done
1262 		 * processing the current buffer's skb */
1263 		requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1264 
1265 		/* If there is no memory we ignore the current RX'd frame,
1266 		 * tell hardware it can give us a new frame using the old
1267 		 * skb and put it at the tail of the sc->rx.rxbuf list for
1268 		 * processing. */
1269 		if (!requeue_skb) {
1270 			RX_STAT_INC(rx_oom_err);
1271 			goto requeue_drop_frag;
1272 		}
1273 
1274 		/* We will now give hardware our shiny new allocated skb */
1275 		new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1276 					      common->rx_bufsize, dma_type);
1277 		if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1278 			dev_kfree_skb_any(requeue_skb);
1279 			goto requeue_drop_frag;
1280 		}
1281 
1282 		/* Unmap the frame */
1283 		dma_unmap_single(sc->dev, bf->bf_buf_addr,
1284 				 common->rx_bufsize, dma_type);
1285 
1286 		bf->bf_mpdu = requeue_skb;
1287 		bf->bf_buf_addr = new_buf_addr;
1288 
1289 		skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1290 		if (ah->caps.rx_status_len)
1291 			skb_pull(skb, ah->caps.rx_status_len);
1292 
1293 		if (!rs.rs_more)
1294 			ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1295 						 rxs, decrypt_error);
1296 
1297 		if (rs.rs_more) {
1298 			RX_STAT_INC(rx_frags);
1299 			/*
1300 			 * rs_more indicates chained descriptors which can be
1301 			 * used to link buffers together for a sort of
1302 			 * scatter-gather operation.
1303 			 */
1304 			if (sc->rx.frag) {
1305 				/* too many fragments - cannot handle frame */
1306 				dev_kfree_skb_any(sc->rx.frag);
1307 				dev_kfree_skb_any(skb);
1308 				RX_STAT_INC(rx_too_many_frags_err);
1309 				skb = NULL;
1310 			}
1311 			sc->rx.frag = skb;
1312 			goto requeue;
1313 		}
1314 		if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
1315 			goto requeue_drop_frag;
1316 
1317 		if (sc->rx.frag) {
1318 			int space = skb->len - skb_tailroom(hdr_skb);
1319 
1320 			if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1321 				dev_kfree_skb(skb);
1322 				RX_STAT_INC(rx_oom_err);
1323 				goto requeue_drop_frag;
1324 			}
1325 
1326 			sc->rx.frag = NULL;
1327 
1328 			skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1329 						  skb->len);
1330 			dev_kfree_skb_any(skb);
1331 			skb = hdr_skb;
1332 		}
1333 
1334 
1335 		if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1336 
1337 			/*
1338 			 * change the default rx antenna if rx diversity
1339 			 * chooses the other antenna 3 times in a row.
1340 			 */
1341 			if (sc->rx.defant != rs.rs_antenna) {
1342 				if (++sc->rx.rxotherant >= 3)
1343 					ath_setdefantenna(sc, rs.rs_antenna);
1344 			} else {
1345 				sc->rx.rxotherant = 0;
1346 			}
1347 
1348 		}
1349 
1350 		if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1351 			skb_trim(skb, skb->len - 8);
1352 
1353 		spin_lock_irqsave(&sc->sc_pm_lock, flags);
1354 		if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
1355 				     PS_WAIT_FOR_CAB |
1356 				     PS_WAIT_FOR_PSPOLL_DATA)) ||
1357 		    ath9k_check_auto_sleep(sc))
1358 			ath_rx_ps(sc, skb, rs.is_mybeacon);
1359 		spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1360 
1361 		if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
1362 			ath_ant_comb_scan(sc, &rs);
1363 
1364 		ath9k_apply_ampdu_details(sc, &rs, rxs);
1365 
1366 		ieee80211_rx(hw, skb);
1367 
1368 requeue_drop_frag:
1369 		if (sc->rx.frag) {
1370 			dev_kfree_skb_any(sc->rx.frag);
1371 			sc->rx.frag = NULL;
1372 		}
1373 requeue:
1374 		list_add_tail(&bf->list, &sc->rx.rxbuf);
1375 		if (flush)
1376 			continue;
1377 
1378 		if (edma) {
1379 			ath_rx_edma_buf_link(sc, qtype);
1380 		} else {
1381 			ath_rx_buf_link(sc, bf);
1382 			ath9k_hw_rxena(ah);
1383 		}
1384 	} while (1);
1385 
1386 	if (!(ah->imask & ATH9K_INT_RXEOL)) {
1387 		ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
1388 		ath9k_hw_set_interrupts(ah);
1389 	}
1390 
1391 	return 0;
1392 }
1393