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 "ath9k.h" 19 #include "ar9003_mac.h" 20 21 #define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb)) 22 23 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc) 24 { 25 return sc->ps_enabled && 26 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP); 27 } 28 29 /* 30 * Setup and link descriptors. 31 * 32 * 11N: we can no longer afford to self link the last descriptor. 33 * MAC acknowledges BA status as long as it copies frames to host 34 * buffer (or rx fifo). This can incorrectly acknowledge packets 35 * to a sender if last desc is self-linked. 36 */ 37 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf, 38 bool flush) 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 ds = bf->bf_desc; 46 ds->ds_link = 0; /* link to null */ 47 ds->ds_data = bf->bf_buf_addr; 48 49 /* virtual addr of the beginning of the buffer. */ 50 skb = bf->bf_mpdu; 51 BUG_ON(skb == NULL); 52 ds->ds_vdata = skb->data; 53 54 /* 55 * setup rx descriptors. The rx_bufsize here tells the hardware 56 * how much data it can DMA to us and that we are prepared 57 * to process 58 */ 59 ath9k_hw_setuprxdesc(ah, ds, 60 common->rx_bufsize, 61 0); 62 63 if (sc->rx.rxlink) 64 *sc->rx.rxlink = bf->bf_daddr; 65 else if (!flush) 66 ath9k_hw_putrxbuf(ah, bf->bf_daddr); 67 68 sc->rx.rxlink = &ds->ds_link; 69 } 70 71 static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf, 72 bool flush) 73 { 74 if (sc->rx.buf_hold) 75 ath_rx_buf_link(sc, sc->rx.buf_hold, flush); 76 77 sc->rx.buf_hold = bf; 78 } 79 80 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna) 81 { 82 /* XXX block beacon interrupts */ 83 ath9k_hw_setantenna(sc->sc_ah, antenna); 84 sc->rx.defant = antenna; 85 sc->rx.rxotherant = 0; 86 } 87 88 static void ath_opmode_init(struct ath_softc *sc) 89 { 90 struct ath_hw *ah = sc->sc_ah; 91 struct ath_common *common = ath9k_hw_common(ah); 92 93 u32 rfilt, mfilt[2]; 94 95 /* configure rx filter */ 96 rfilt = ath_calcrxfilter(sc); 97 ath9k_hw_setrxfilter(ah, rfilt); 98 99 /* configure bssid mask */ 100 ath_hw_setbssidmask(common); 101 102 /* configure operational mode */ 103 ath9k_hw_setopmode(ah); 104 105 /* calculate and install multicast filter */ 106 mfilt[0] = mfilt[1] = ~0; 107 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]); 108 } 109 110 static bool ath_rx_edma_buf_link(struct ath_softc *sc, 111 enum ath9k_rx_qtype qtype) 112 { 113 struct ath_hw *ah = sc->sc_ah; 114 struct ath_rx_edma *rx_edma; 115 struct sk_buff *skb; 116 struct ath_rxbuf *bf; 117 118 rx_edma = &sc->rx.rx_edma[qtype]; 119 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize) 120 return false; 121 122 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list); 123 list_del_init(&bf->list); 124 125 skb = bf->bf_mpdu; 126 127 memset(skb->data, 0, ah->caps.rx_status_len); 128 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr, 129 ah->caps.rx_status_len, DMA_TO_DEVICE); 130 131 SKB_CB_ATHBUF(skb) = bf; 132 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype); 133 __skb_queue_tail(&rx_edma->rx_fifo, skb); 134 135 return true; 136 } 137 138 static void ath_rx_addbuffer_edma(struct ath_softc *sc, 139 enum ath9k_rx_qtype qtype) 140 { 141 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 142 struct ath_rxbuf *bf, *tbf; 143 144 if (list_empty(&sc->rx.rxbuf)) { 145 ath_dbg(common, QUEUE, "No free rx buf available\n"); 146 return; 147 } 148 149 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) 150 if (!ath_rx_edma_buf_link(sc, qtype)) 151 break; 152 153 } 154 155 static void ath_rx_remove_buffer(struct ath_softc *sc, 156 enum ath9k_rx_qtype qtype) 157 { 158 struct ath_rxbuf *bf; 159 struct ath_rx_edma *rx_edma; 160 struct sk_buff *skb; 161 162 rx_edma = &sc->rx.rx_edma[qtype]; 163 164 while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) { 165 bf = SKB_CB_ATHBUF(skb); 166 BUG_ON(!bf); 167 list_add_tail(&bf->list, &sc->rx.rxbuf); 168 } 169 } 170 171 static void ath_rx_edma_cleanup(struct ath_softc *sc) 172 { 173 struct ath_hw *ah = sc->sc_ah; 174 struct ath_common *common = ath9k_hw_common(ah); 175 struct ath_rxbuf *bf; 176 177 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP); 178 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP); 179 180 list_for_each_entry(bf, &sc->rx.rxbuf, list) { 181 if (bf->bf_mpdu) { 182 dma_unmap_single(sc->dev, bf->bf_buf_addr, 183 common->rx_bufsize, 184 DMA_BIDIRECTIONAL); 185 dev_kfree_skb_any(bf->bf_mpdu); 186 bf->bf_buf_addr = 0; 187 bf->bf_mpdu = NULL; 188 } 189 } 190 } 191 192 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size) 193 { 194 __skb_queue_head_init(&rx_edma->rx_fifo); 195 rx_edma->rx_fifo_hwsize = size; 196 } 197 198 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs) 199 { 200 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 201 struct ath_hw *ah = sc->sc_ah; 202 struct sk_buff *skb; 203 struct ath_rxbuf *bf; 204 int error = 0, i; 205 u32 size; 206 207 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize - 208 ah->caps.rx_status_len); 209 210 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP], 211 ah->caps.rx_lp_qdepth); 212 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP], 213 ah->caps.rx_hp_qdepth); 214 215 size = sizeof(struct ath_rxbuf) * nbufs; 216 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL); 217 if (!bf) 218 return -ENOMEM; 219 220 INIT_LIST_HEAD(&sc->rx.rxbuf); 221 222 for (i = 0; i < nbufs; i++, bf++) { 223 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL); 224 if (!skb) { 225 error = -ENOMEM; 226 goto rx_init_fail; 227 } 228 229 memset(skb->data, 0, common->rx_bufsize); 230 bf->bf_mpdu = skb; 231 232 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, 233 common->rx_bufsize, 234 DMA_BIDIRECTIONAL); 235 if (unlikely(dma_mapping_error(sc->dev, 236 bf->bf_buf_addr))) { 237 dev_kfree_skb_any(skb); 238 bf->bf_mpdu = NULL; 239 bf->bf_buf_addr = 0; 240 ath_err(common, 241 "dma_mapping_error() on RX init\n"); 242 error = -ENOMEM; 243 goto rx_init_fail; 244 } 245 246 list_add_tail(&bf->list, &sc->rx.rxbuf); 247 } 248 249 return 0; 250 251 rx_init_fail: 252 ath_rx_edma_cleanup(sc); 253 return error; 254 } 255 256 static void ath_edma_start_recv(struct ath_softc *sc) 257 { 258 ath9k_hw_rxena(sc->sc_ah); 259 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP); 260 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP); 261 ath_opmode_init(sc); 262 ath9k_hw_startpcureceive(sc->sc_ah, sc->cur_chan->offchannel); 263 } 264 265 static void ath_edma_stop_recv(struct ath_softc *sc) 266 { 267 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP); 268 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP); 269 } 270 271 int ath_rx_init(struct ath_softc *sc, int nbufs) 272 { 273 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 274 struct sk_buff *skb; 275 struct ath_rxbuf *bf; 276 int error = 0; 277 278 spin_lock_init(&sc->sc_pcu_lock); 279 280 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 + 281 sc->sc_ah->caps.rx_status_len; 282 283 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) 284 return ath_rx_edma_init(sc, nbufs); 285 286 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n", 287 common->cachelsz, common->rx_bufsize); 288 289 /* Initialize rx descriptors */ 290 291 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf, 292 "rx", nbufs, 1, 0); 293 if (error != 0) { 294 ath_err(common, 295 "failed to allocate rx descriptors: %d\n", 296 error); 297 goto err; 298 } 299 300 list_for_each_entry(bf, &sc->rx.rxbuf, list) { 301 skb = ath_rxbuf_alloc(common, common->rx_bufsize, 302 GFP_KERNEL); 303 if (skb == NULL) { 304 error = -ENOMEM; 305 goto err; 306 } 307 308 bf->bf_mpdu = skb; 309 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, 310 common->rx_bufsize, 311 DMA_FROM_DEVICE); 312 if (unlikely(dma_mapping_error(sc->dev, 313 bf->bf_buf_addr))) { 314 dev_kfree_skb_any(skb); 315 bf->bf_mpdu = NULL; 316 bf->bf_buf_addr = 0; 317 ath_err(common, 318 "dma_mapping_error() on RX init\n"); 319 error = -ENOMEM; 320 goto err; 321 } 322 } 323 sc->rx.rxlink = NULL; 324 err: 325 if (error) 326 ath_rx_cleanup(sc); 327 328 return error; 329 } 330 331 void ath_rx_cleanup(struct ath_softc *sc) 332 { 333 struct ath_hw *ah = sc->sc_ah; 334 struct ath_common *common = ath9k_hw_common(ah); 335 struct sk_buff *skb; 336 struct ath_rxbuf *bf; 337 338 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) { 339 ath_rx_edma_cleanup(sc); 340 return; 341 } 342 343 list_for_each_entry(bf, &sc->rx.rxbuf, list) { 344 skb = bf->bf_mpdu; 345 if (skb) { 346 dma_unmap_single(sc->dev, bf->bf_buf_addr, 347 common->rx_bufsize, 348 DMA_FROM_DEVICE); 349 dev_kfree_skb(skb); 350 bf->bf_buf_addr = 0; 351 bf->bf_mpdu = NULL; 352 } 353 } 354 } 355 356 /* 357 * Calculate the receive filter according to the 358 * operating mode and state: 359 * 360 * o always accept unicast, broadcast, and multicast traffic 361 * o maintain current state of phy error reception (the hal 362 * may enable phy error frames for noise immunity work) 363 * o probe request frames are accepted only when operating in 364 * hostap, adhoc, or monitor modes 365 * o enable promiscuous mode according to the interface state 366 * o accept beacons: 367 * - when operating in adhoc mode so the 802.11 layer creates 368 * node table entries for peers, 369 * - when operating in station mode for collecting rssi data when 370 * the station is otherwise quiet, or 371 * - when operating as a repeater so we see repeater-sta beacons 372 * - when scanning 373 */ 374 375 u32 ath_calcrxfilter(struct ath_softc *sc) 376 { 377 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 378 u32 rfilt; 379 380 if (config_enabled(CONFIG_ATH9K_TX99)) 381 return 0; 382 383 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST 384 | ATH9K_RX_FILTER_MCAST; 385 386 /* if operating on a DFS channel, enable radar pulse detection */ 387 if (sc->hw->conf.radar_enabled) 388 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR; 389 390 if (sc->rx.rxfilter & FIF_PROBE_REQ) 391 rfilt |= ATH9K_RX_FILTER_PROBEREQ; 392 393 /* 394 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station 395 * mode interface or when in monitor mode. AP mode does not need this 396 * since it receives all in-BSS frames anyway. 397 */ 398 if (sc->sc_ah->is_monitoring) 399 rfilt |= ATH9K_RX_FILTER_PROM; 400 401 if (sc->rx.rxfilter & FIF_CONTROL) 402 rfilt |= ATH9K_RX_FILTER_CONTROL; 403 404 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) && 405 (sc->nvifs <= 1) && 406 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)) 407 rfilt |= ATH9K_RX_FILTER_MYBEACON; 408 else 409 rfilt |= ATH9K_RX_FILTER_BEACON; 410 411 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) || 412 (sc->rx.rxfilter & FIF_PSPOLL)) 413 rfilt |= ATH9K_RX_FILTER_PSPOLL; 414 415 if (conf_is_ht(&sc->hw->conf)) 416 rfilt |= ATH9K_RX_FILTER_COMP_BAR; 417 418 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) { 419 /* This is needed for older chips */ 420 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160) 421 rfilt |= ATH9K_RX_FILTER_PROM; 422 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL; 423 } 424 425 if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah)) 426 rfilt |= ATH9K_RX_FILTER_4ADDRESS; 427 428 if (ath9k_use_chanctx && 429 test_bit(ATH_OP_SCANNING, &common->op_flags)) 430 rfilt |= ATH9K_RX_FILTER_BEACON; 431 432 return rfilt; 433 434 } 435 436 int ath_startrecv(struct ath_softc *sc) 437 { 438 struct ath_hw *ah = sc->sc_ah; 439 struct ath_rxbuf *bf, *tbf; 440 441 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) { 442 ath_edma_start_recv(sc); 443 return 0; 444 } 445 446 if (list_empty(&sc->rx.rxbuf)) 447 goto start_recv; 448 449 sc->rx.buf_hold = NULL; 450 sc->rx.rxlink = NULL; 451 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) { 452 ath_rx_buf_link(sc, bf, false); 453 } 454 455 /* We could have deleted elements so the list may be empty now */ 456 if (list_empty(&sc->rx.rxbuf)) 457 goto start_recv; 458 459 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list); 460 ath9k_hw_putrxbuf(ah, bf->bf_daddr); 461 ath9k_hw_rxena(ah); 462 463 start_recv: 464 ath_opmode_init(sc); 465 ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel); 466 467 return 0; 468 } 469 470 static void ath_flushrecv(struct ath_softc *sc) 471 { 472 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) 473 ath_rx_tasklet(sc, 1, true); 474 ath_rx_tasklet(sc, 1, false); 475 } 476 477 bool ath_stoprecv(struct ath_softc *sc) 478 { 479 struct ath_hw *ah = sc->sc_ah; 480 bool stopped, reset = false; 481 482 ath9k_hw_abortpcurecv(ah); 483 ath9k_hw_setrxfilter(ah, 0); 484 stopped = ath9k_hw_stopdmarecv(ah, &reset); 485 486 ath_flushrecv(sc); 487 488 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) 489 ath_edma_stop_recv(sc); 490 else 491 sc->rx.rxlink = NULL; 492 493 if (!(ah->ah_flags & AH_UNPLUGGED) && 494 unlikely(!stopped)) { 495 ath_err(ath9k_hw_common(sc->sc_ah), 496 "Could not stop RX, we could be " 497 "confusing the DMA engine when we start RX up\n"); 498 ATH_DBG_WARN_ON_ONCE(!stopped); 499 } 500 return stopped && !reset; 501 } 502 503 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb) 504 { 505 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */ 506 struct ieee80211_mgmt *mgmt; 507 u8 *pos, *end, id, elen; 508 struct ieee80211_tim_ie *tim; 509 510 mgmt = (struct ieee80211_mgmt *)skb->data; 511 pos = mgmt->u.beacon.variable; 512 end = skb->data + skb->len; 513 514 while (pos + 2 < end) { 515 id = *pos++; 516 elen = *pos++; 517 if (pos + elen > end) 518 break; 519 520 if (id == WLAN_EID_TIM) { 521 if (elen < sizeof(*tim)) 522 break; 523 tim = (struct ieee80211_tim_ie *) pos; 524 if (tim->dtim_count != 0) 525 break; 526 return tim->bitmap_ctrl & 0x01; 527 } 528 529 pos += elen; 530 } 531 532 return false; 533 } 534 535 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb) 536 { 537 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 538 539 if (skb->len < 24 + 8 + 2 + 2) 540 return; 541 542 sc->ps_flags &= ~PS_WAIT_FOR_BEACON; 543 544 if (sc->ps_flags & PS_BEACON_SYNC) { 545 sc->ps_flags &= ~PS_BEACON_SYNC; 546 ath_dbg(common, PS, 547 "Reconfigure beacon timers based on synchronized timestamp\n"); 548 if (!(WARN_ON_ONCE(sc->cur_chan->beacon.beacon_interval == 0))) 549 ath9k_set_beacon(sc); 550 if (sc->p2p_ps_vif) 551 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif); 552 } 553 554 if (ath_beacon_dtim_pending_cab(skb)) { 555 /* 556 * Remain awake waiting for buffered broadcast/multicast 557 * frames. If the last broadcast/multicast frame is not 558 * received properly, the next beacon frame will work as 559 * a backup trigger for returning into NETWORK SLEEP state, 560 * so we are waiting for it as well. 561 */ 562 ath_dbg(common, PS, 563 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n"); 564 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON; 565 return; 566 } 567 568 if (sc->ps_flags & PS_WAIT_FOR_CAB) { 569 /* 570 * This can happen if a broadcast frame is dropped or the AP 571 * fails to send a frame indicating that all CAB frames have 572 * been delivered. 573 */ 574 sc->ps_flags &= ~PS_WAIT_FOR_CAB; 575 ath_dbg(common, PS, "PS wait for CAB frames timed out\n"); 576 } 577 } 578 579 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon) 580 { 581 struct ieee80211_hdr *hdr; 582 struct ath_common *common = ath9k_hw_common(sc->sc_ah); 583 584 hdr = (struct ieee80211_hdr *)skb->data; 585 586 /* Process Beacon and CAB receive in PS state */ 587 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc)) 588 && mybeacon) { 589 ath_rx_ps_beacon(sc, skb); 590 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) && 591 (ieee80211_is_data(hdr->frame_control) || 592 ieee80211_is_action(hdr->frame_control)) && 593 is_multicast_ether_addr(hdr->addr1) && 594 !ieee80211_has_moredata(hdr->frame_control)) { 595 /* 596 * No more broadcast/multicast frames to be received at this 597 * point. 598 */ 599 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON); 600 ath_dbg(common, PS, 601 "All PS CAB frames received, back to sleep\n"); 602 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) && 603 !is_multicast_ether_addr(hdr->addr1) && 604 !ieee80211_has_morefrags(hdr->frame_control)) { 605 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA; 606 ath_dbg(common, PS, 607 "Going back to sleep after having received PS-Poll data (0x%lx)\n", 608 sc->ps_flags & (PS_WAIT_FOR_BEACON | 609 PS_WAIT_FOR_CAB | 610 PS_WAIT_FOR_PSPOLL_DATA | 611 PS_WAIT_FOR_TX_ACK)); 612 } 613 } 614 615 static bool ath_edma_get_buffers(struct ath_softc *sc, 616 enum ath9k_rx_qtype qtype, 617 struct ath_rx_status *rs, 618 struct ath_rxbuf **dest) 619 { 620 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype]; 621 struct ath_hw *ah = sc->sc_ah; 622 struct ath_common *common = ath9k_hw_common(ah); 623 struct sk_buff *skb; 624 struct ath_rxbuf *bf; 625 int ret; 626 627 skb = skb_peek(&rx_edma->rx_fifo); 628 if (!skb) 629 return false; 630 631 bf = SKB_CB_ATHBUF(skb); 632 BUG_ON(!bf); 633 634 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, 635 common->rx_bufsize, DMA_FROM_DEVICE); 636 637 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data); 638 if (ret == -EINPROGRESS) { 639 /*let device gain the buffer again*/ 640 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr, 641 common->rx_bufsize, DMA_FROM_DEVICE); 642 return false; 643 } 644 645 __skb_unlink(skb, &rx_edma->rx_fifo); 646 if (ret == -EINVAL) { 647 /* corrupt descriptor, skip this one and the following one */ 648 list_add_tail(&bf->list, &sc->rx.rxbuf); 649 ath_rx_edma_buf_link(sc, qtype); 650 651 skb = skb_peek(&rx_edma->rx_fifo); 652 if (skb) { 653 bf = SKB_CB_ATHBUF(skb); 654 BUG_ON(!bf); 655 656 __skb_unlink(skb, &rx_edma->rx_fifo); 657 list_add_tail(&bf->list, &sc->rx.rxbuf); 658 ath_rx_edma_buf_link(sc, qtype); 659 } 660 661 bf = NULL; 662 } 663 664 *dest = bf; 665 return true; 666 } 667 668 static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc, 669 struct ath_rx_status *rs, 670 enum ath9k_rx_qtype qtype) 671 { 672 struct ath_rxbuf *bf = NULL; 673 674 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) { 675 if (!bf) 676 continue; 677 678 return bf; 679 } 680 return NULL; 681 } 682 683 static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc, 684 struct ath_rx_status *rs) 685 { 686 struct ath_hw *ah = sc->sc_ah; 687 struct ath_common *common = ath9k_hw_common(ah); 688 struct ath_desc *ds; 689 struct ath_rxbuf *bf; 690 int ret; 691 692 if (list_empty(&sc->rx.rxbuf)) { 693 sc->rx.rxlink = NULL; 694 return NULL; 695 } 696 697 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list); 698 if (bf == sc->rx.buf_hold) 699 return NULL; 700 701 ds = bf->bf_desc; 702 703 /* 704 * Must provide the virtual address of the current 705 * descriptor, the physical address, and the virtual 706 * address of the next descriptor in the h/w chain. 707 * This allows the HAL to look ahead to see if the 708 * hardware is done with a descriptor by checking the 709 * done bit in the following descriptor and the address 710 * of the current descriptor the DMA engine is working 711 * on. All this is necessary because of our use of 712 * a self-linked list to avoid rx overruns. 713 */ 714 ret = ath9k_hw_rxprocdesc(ah, ds, rs); 715 if (ret == -EINPROGRESS) { 716 struct ath_rx_status trs; 717 struct ath_rxbuf *tbf; 718 struct ath_desc *tds; 719 720 memset(&trs, 0, sizeof(trs)); 721 if (list_is_last(&bf->list, &sc->rx.rxbuf)) { 722 sc->rx.rxlink = NULL; 723 return NULL; 724 } 725 726 tbf = list_entry(bf->list.next, struct ath_rxbuf, list); 727 728 /* 729 * On some hardware the descriptor status words could 730 * get corrupted, including the done bit. Because of 731 * this, check if the next descriptor's done bit is 732 * set or not. 733 * 734 * If the next descriptor's done bit is set, the current 735 * descriptor has been corrupted. Force s/w to discard 736 * this descriptor and continue... 737 */ 738 739 tds = tbf->bf_desc; 740 ret = ath9k_hw_rxprocdesc(ah, tds, &trs); 741 if (ret == -EINPROGRESS) 742 return NULL; 743 744 /* 745 * Re-check previous descriptor, in case it has been filled 746 * in the mean time. 747 */ 748 ret = ath9k_hw_rxprocdesc(ah, ds, rs); 749 if (ret == -EINPROGRESS) { 750 /* 751 * mark descriptor as zero-length and set the 'more' 752 * flag to ensure that both buffers get discarded 753 */ 754 rs->rs_datalen = 0; 755 rs->rs_more = true; 756 } 757 } 758 759 list_del(&bf->list); 760 if (!bf->bf_mpdu) 761 return bf; 762 763 /* 764 * Synchronize the DMA transfer with CPU before 765 * 1. accessing the frame 766 * 2. requeueing the same buffer to h/w 767 */ 768 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, 769 common->rx_bufsize, 770 DMA_FROM_DEVICE); 771 772 return bf; 773 } 774 775 static void ath9k_process_tsf(struct ath_rx_status *rs, 776 struct ieee80211_rx_status *rxs, 777 u64 tsf) 778 { 779 u32 tsf_lower = tsf & 0xffffffff; 780 781 rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp; 782 if (rs->rs_tstamp > tsf_lower && 783 unlikely(rs->rs_tstamp - tsf_lower > 0x10000000)) 784 rxs->mactime -= 0x100000000ULL; 785 786 if (rs->rs_tstamp < tsf_lower && 787 unlikely(tsf_lower - rs->rs_tstamp > 0x10000000)) 788 rxs->mactime += 0x100000000ULL; 789 } 790 791 /* 792 * For Decrypt or Demic errors, we only mark packet status here and always push 793 * up the frame up to let mac80211 handle the actual error case, be it no 794 * decryption key or real decryption error. This let us keep statistics there. 795 */ 796 static int ath9k_rx_skb_preprocess(struct ath_softc *sc, 797 struct sk_buff *skb, 798 struct ath_rx_status *rx_stats, 799 struct ieee80211_rx_status *rx_status, 800 bool *decrypt_error, u64 tsf) 801 { 802 struct ieee80211_hw *hw = sc->hw; 803 struct ath_hw *ah = sc->sc_ah; 804 struct ath_common *common = ath9k_hw_common(ah); 805 struct ieee80211_hdr *hdr; 806 bool discard_current = sc->rx.discard_next; 807 808 /* 809 * Discard corrupt descriptors which are marked in 810 * ath_get_next_rx_buf(). 811 */ 812 if (discard_current) 813 goto corrupt; 814 815 sc->rx.discard_next = false; 816 817 /* 818 * Discard zero-length packets. 819 */ 820 if (!rx_stats->rs_datalen) { 821 RX_STAT_INC(rx_len_err); 822 goto corrupt; 823 } 824 825 /* 826 * rs_status follows rs_datalen so if rs_datalen is too large 827 * we can take a hint that hardware corrupted it, so ignore 828 * those frames. 829 */ 830 if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) { 831 RX_STAT_INC(rx_len_err); 832 goto corrupt; 833 } 834 835 /* Only use status info from the last fragment */ 836 if (rx_stats->rs_more) 837 return 0; 838 839 /* 840 * Return immediately if the RX descriptor has been marked 841 * as corrupt based on the various error bits. 842 * 843 * This is different from the other corrupt descriptor 844 * condition handled above. 845 */ 846 if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC) 847 goto corrupt; 848 849 hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len); 850 851 ath9k_process_tsf(rx_stats, rx_status, tsf); 852 ath_debug_stat_rx(sc, rx_stats); 853 854 /* 855 * Process PHY errors and return so that the packet 856 * can be dropped. 857 */ 858 if (rx_stats->rs_status & ATH9K_RXERR_PHY) { 859 ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime); 860 if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime)) 861 RX_STAT_INC(rx_spectral); 862 863 return -EINVAL; 864 } 865 866 /* 867 * everything but the rate is checked here, the rate check is done 868 * separately to avoid doing two lookups for a rate for each frame. 869 */ 870 if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error, sc->rx.rxfilter)) 871 return -EINVAL; 872 873 if (ath_is_mybeacon(common, hdr)) { 874 RX_STAT_INC(rx_beacons); 875 rx_stats->is_mybeacon = true; 876 } 877 878 /* 879 * This shouldn't happen, but have a safety check anyway. 880 */ 881 if (WARN_ON(!ah->curchan)) 882 return -EINVAL; 883 884 if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) { 885 /* 886 * No valid hardware bitrate found -- we should not get here 887 * because hardware has already validated this frame as OK. 888 */ 889 ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n", 890 rx_stats->rs_rate); 891 RX_STAT_INC(rx_rate_err); 892 return -EINVAL; 893 } 894 895 if (rx_stats->is_mybeacon) { 896 sc->sched.next_tbtt = rx_stats->rs_tstamp; 897 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_BEACON_RECEIVED); 898 } 899 900 ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status); 901 902 rx_status->band = ah->curchan->chan->band; 903 rx_status->freq = ah->curchan->chan->center_freq; 904 rx_status->antenna = rx_stats->rs_antenna; 905 rx_status->flag |= RX_FLAG_MACTIME_END; 906 907 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT 908 if (ieee80211_is_data_present(hdr->frame_control) && 909 !ieee80211_is_qos_nullfunc(hdr->frame_control)) 910 sc->rx.num_pkts++; 911 #endif 912 913 return 0; 914 915 corrupt: 916 sc->rx.discard_next = rx_stats->rs_more; 917 return -EINVAL; 918 } 919 920 /* 921 * Run the LNA combining algorithm only in these cases: 922 * 923 * Standalone WLAN cards with both LNA/Antenna diversity 924 * enabled in the EEPROM. 925 * 926 * WLAN+BT cards which are in the supported card list 927 * in ath_pci_id_table and the user has loaded the 928 * driver with "bt_ant_diversity" set to true. 929 */ 930 static void ath9k_antenna_check(struct ath_softc *sc, 931 struct ath_rx_status *rs) 932 { 933 struct ath_hw *ah = sc->sc_ah; 934 struct ath9k_hw_capabilities *pCap = &ah->caps; 935 struct ath_common *common = ath9k_hw_common(ah); 936 937 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)) 938 return; 939 940 /* 941 * Change the default rx antenna if rx diversity 942 * chooses the other antenna 3 times in a row. 943 */ 944 if (sc->rx.defant != rs->rs_antenna) { 945 if (++sc->rx.rxotherant >= 3) 946 ath_setdefantenna(sc, rs->rs_antenna); 947 } else { 948 sc->rx.rxotherant = 0; 949 } 950 951 if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) { 952 if (common->bt_ant_diversity) 953 ath_ant_comb_scan(sc, rs); 954 } else { 955 ath_ant_comb_scan(sc, rs); 956 } 957 } 958 959 static void ath9k_apply_ampdu_details(struct ath_softc *sc, 960 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs) 961 { 962 if (rs->rs_isaggr) { 963 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN; 964 965 rxs->ampdu_reference = sc->rx.ampdu_ref; 966 967 if (!rs->rs_moreaggr) { 968 rxs->flag |= RX_FLAG_AMPDU_IS_LAST; 969 sc->rx.ampdu_ref++; 970 } 971 972 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE) 973 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR; 974 } 975 } 976 977 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp) 978 { 979 struct ath_rxbuf *bf; 980 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb; 981 struct ieee80211_rx_status *rxs; 982 struct ath_hw *ah = sc->sc_ah; 983 struct ath_common *common = ath9k_hw_common(ah); 984 struct ieee80211_hw *hw = sc->hw; 985 int retval; 986 struct ath_rx_status rs; 987 enum ath9k_rx_qtype qtype; 988 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA); 989 int dma_type; 990 u64 tsf = 0; 991 unsigned long flags; 992 dma_addr_t new_buf_addr; 993 unsigned int budget = 512; 994 995 if (edma) 996 dma_type = DMA_BIDIRECTIONAL; 997 else 998 dma_type = DMA_FROM_DEVICE; 999 1000 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP; 1001 1002 tsf = ath9k_hw_gettsf64(ah); 1003 1004 do { 1005 bool decrypt_error = false; 1006 1007 memset(&rs, 0, sizeof(rs)); 1008 if (edma) 1009 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype); 1010 else 1011 bf = ath_get_next_rx_buf(sc, &rs); 1012 1013 if (!bf) 1014 break; 1015 1016 skb = bf->bf_mpdu; 1017 if (!skb) 1018 continue; 1019 1020 /* 1021 * Take frame header from the first fragment and RX status from 1022 * the last one. 1023 */ 1024 if (sc->rx.frag) 1025 hdr_skb = sc->rx.frag; 1026 else 1027 hdr_skb = skb; 1028 1029 rxs = IEEE80211_SKB_RXCB(hdr_skb); 1030 memset(rxs, 0, sizeof(struct ieee80211_rx_status)); 1031 1032 retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs, 1033 &decrypt_error, tsf); 1034 if (retval) 1035 goto requeue_drop_frag; 1036 1037 /* Ensure we always have an skb to requeue once we are done 1038 * processing the current buffer's skb */ 1039 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC); 1040 1041 /* If there is no memory we ignore the current RX'd frame, 1042 * tell hardware it can give us a new frame using the old 1043 * skb and put it at the tail of the sc->rx.rxbuf list for 1044 * processing. */ 1045 if (!requeue_skb) { 1046 RX_STAT_INC(rx_oom_err); 1047 goto requeue_drop_frag; 1048 } 1049 1050 /* We will now give hardware our shiny new allocated skb */ 1051 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data, 1052 common->rx_bufsize, dma_type); 1053 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) { 1054 dev_kfree_skb_any(requeue_skb); 1055 goto requeue_drop_frag; 1056 } 1057 1058 /* Unmap the frame */ 1059 dma_unmap_single(sc->dev, bf->bf_buf_addr, 1060 common->rx_bufsize, dma_type); 1061 1062 bf->bf_mpdu = requeue_skb; 1063 bf->bf_buf_addr = new_buf_addr; 1064 1065 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len); 1066 if (ah->caps.rx_status_len) 1067 skb_pull(skb, ah->caps.rx_status_len); 1068 1069 if (!rs.rs_more) 1070 ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs, 1071 rxs, decrypt_error); 1072 1073 if (rs.rs_more) { 1074 RX_STAT_INC(rx_frags); 1075 /* 1076 * rs_more indicates chained descriptors which can be 1077 * used to link buffers together for a sort of 1078 * scatter-gather operation. 1079 */ 1080 if (sc->rx.frag) { 1081 /* too many fragments - cannot handle frame */ 1082 dev_kfree_skb_any(sc->rx.frag); 1083 dev_kfree_skb_any(skb); 1084 RX_STAT_INC(rx_too_many_frags_err); 1085 skb = NULL; 1086 } 1087 sc->rx.frag = skb; 1088 goto requeue; 1089 } 1090 1091 if (sc->rx.frag) { 1092 int space = skb->len - skb_tailroom(hdr_skb); 1093 1094 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) { 1095 dev_kfree_skb(skb); 1096 RX_STAT_INC(rx_oom_err); 1097 goto requeue_drop_frag; 1098 } 1099 1100 sc->rx.frag = NULL; 1101 1102 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len), 1103 skb->len); 1104 dev_kfree_skb_any(skb); 1105 skb = hdr_skb; 1106 } 1107 1108 if (rxs->flag & RX_FLAG_MMIC_STRIPPED) 1109 skb_trim(skb, skb->len - 8); 1110 1111 spin_lock_irqsave(&sc->sc_pm_lock, flags); 1112 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON | 1113 PS_WAIT_FOR_CAB | 1114 PS_WAIT_FOR_PSPOLL_DATA)) || 1115 ath9k_check_auto_sleep(sc)) 1116 ath_rx_ps(sc, skb, rs.is_mybeacon); 1117 spin_unlock_irqrestore(&sc->sc_pm_lock, flags); 1118 1119 ath9k_antenna_check(sc, &rs); 1120 ath9k_apply_ampdu_details(sc, &rs, rxs); 1121 ath_debug_rate_stats(sc, &rs, skb); 1122 1123 ieee80211_rx(hw, skb); 1124 1125 requeue_drop_frag: 1126 if (sc->rx.frag) { 1127 dev_kfree_skb_any(sc->rx.frag); 1128 sc->rx.frag = NULL; 1129 } 1130 requeue: 1131 list_add_tail(&bf->list, &sc->rx.rxbuf); 1132 1133 if (!edma) { 1134 ath_rx_buf_relink(sc, bf, flush); 1135 if (!flush) 1136 ath9k_hw_rxena(ah); 1137 } else if (!flush) { 1138 ath_rx_edma_buf_link(sc, qtype); 1139 } 1140 1141 if (!budget--) 1142 break; 1143 } while (1); 1144 1145 if (!(ah->imask & ATH9K_INT_RXEOL)) { 1146 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN); 1147 ath9k_hw_set_interrupts(ah); 1148 } 1149 1150 return 0; 1151 } 1152