2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
23 int get_hw_qnum(u16 queue, int *hwq_map)
27 return hwq_map[ATH9K_WME_AC_VO];
29 return hwq_map[ATH9K_WME_AC_VI];
31 return hwq_map[ATH9K_WME_AC_BE];
33 return hwq_map[ATH9K_WME_AC_BK];
35 return hwq_map[ATH9K_WME_AC_BE];
39 int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
40 struct ath9k_tx_queue_info *qinfo)
42 struct ath_hw *ah = priv->ah;
44 struct ath9k_tx_queue_info qi;
46 ath9k_hw_get_txq_props(ah, qnum, &qi);
48 qi.tqi_aifs = qinfo->tqi_aifs;
49 qi.tqi_cwmin = qinfo->tqi_cwmin / 2; /* XXX */
50 qi.tqi_cwmax = qinfo->tqi_cwmax;
51 qi.tqi_burstTime = qinfo->tqi_burstTime;
52 qi.tqi_readyTime = qinfo->tqi_readyTime;
54 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
55 ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
56 "Unable to update hardware queue %u!\n", qnum);
59 ath9k_hw_resettxqueue(ah, qnum);
65 int ath9k_htc_tx_start(struct ath9k_htc_priv *priv, struct sk_buff *skb)
67 struct ieee80211_hdr *hdr;
68 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
69 struct ieee80211_sta *sta = tx_info->control.sta;
70 struct ath9k_htc_sta *ista;
71 struct ath9k_htc_vif *avp;
72 struct ath9k_htc_tx_ctl tx_ctl;
73 enum htc_endpoint_id epid;
79 hdr = (struct ieee80211_hdr *) skb->data;
80 fc = hdr->frame_control;
82 avp = (struct ath9k_htc_vif *) tx_info->control.vif->drv_priv;
84 ista = (struct ath9k_htc_sta *) sta->drv_priv;
85 sta_idx = ista->index;
90 memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
92 if (ieee80211_is_data(fc)) {
93 struct tx_frame_hdr tx_hdr;
96 memset(&tx_hdr, 0, sizeof(struct tx_frame_hdr));
98 tx_hdr.node_idx = sta_idx;
99 tx_hdr.vif_idx = avp->index;
101 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
102 tx_ctl.type = ATH9K_HTC_AMPDU;
103 tx_hdr.data_type = ATH9K_HTC_AMPDU;
105 tx_ctl.type = ATH9K_HTC_NORMAL;
106 tx_hdr.data_type = ATH9K_HTC_NORMAL;
109 if (ieee80211_is_data(fc)) {
110 qc = ieee80211_get_qos_ctl(hdr);
111 tx_hdr.tidno = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
114 /* Check for RTS protection */
115 if (priv->hw->wiphy->rts_threshold != (u32) -1)
116 if (skb->len > priv->hw->wiphy->rts_threshold)
117 tx_hdr.flags |= ATH9K_HTC_TX_RTSCTS;
120 if (!(tx_hdr.flags & ATH9K_HTC_TX_RTSCTS) &&
121 (priv->op_flags & OP_PROTECT_ENABLE))
122 tx_hdr.flags |= ATH9K_HTC_TX_CTSONLY;
124 tx_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
125 if (tx_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
126 tx_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
128 tx_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
130 tx_fhdr = skb_push(skb, sizeof(tx_hdr));
131 memcpy(tx_fhdr, (u8 *) &tx_hdr, sizeof(tx_hdr));
133 qnum = skb_get_queue_mapping(skb);
134 hw_qnum = get_hw_qnum(qnum, priv->hwq_map);
138 epid = priv->data_be_ep;
141 epid = priv->data_vi_ep;
144 epid = priv->data_vo_ep;
148 epid = priv->data_bk_ep;
152 struct tx_mgmt_hdr mgmt_hdr;
154 memset(&mgmt_hdr, 0, sizeof(struct tx_mgmt_hdr));
156 tx_ctl.type = ATH9K_HTC_NORMAL;
158 mgmt_hdr.node_idx = sta_idx;
159 mgmt_hdr.vif_idx = avp->index;
163 mgmt_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
164 if (mgmt_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
165 mgmt_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
167 mgmt_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
169 tx_fhdr = skb_push(skb, sizeof(mgmt_hdr));
170 memcpy(tx_fhdr, (u8 *) &mgmt_hdr, sizeof(mgmt_hdr));
171 epid = priv->mgmt_ep;
174 return htc_send(priv->htc, skb, epid, &tx_ctl);
177 void ath9k_tx_tasklet(unsigned long data)
179 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
180 struct ieee80211_sta *sta;
181 struct ieee80211_hdr *hdr;
182 struct ieee80211_tx_info *tx_info;
183 struct sk_buff *skb = NULL;
186 while ((skb = skb_dequeue(&priv->tx_queue)) != NULL) {
188 hdr = (struct ieee80211_hdr *) skb->data;
189 fc = hdr->frame_control;
190 tx_info = IEEE80211_SKB_CB(skb);
192 memset(&tx_info->status, 0, sizeof(tx_info->status));
196 sta = ieee80211_find_sta(priv->vif, hdr->addr1);
199 ieee80211_tx_status(priv->hw, skb);
203 /* Check if we need to start aggregation */
205 if (sta && conf_is_ht(&priv->hw->conf) &&
206 (priv->op_flags & OP_TXAGGR)
207 && !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
208 if (ieee80211_is_data_qos(fc)) {
210 struct ath9k_htc_sta *ista;
212 qc = ieee80211_get_qos_ctl(hdr);
214 ista = (struct ath9k_htc_sta *)sta->drv_priv;
216 if ((tid < ATH9K_HTC_MAX_TID) &&
217 ista->tid_state[tid] == AGGR_STOP) {
218 ieee80211_start_tx_ba_session(sta, tid);
219 ista->tid_state[tid] = AGGR_PROGRESS;
226 /* Send status to mac80211 */
227 ieee80211_tx_status(priv->hw, skb);
230 /* Wake TX queues if needed */
231 spin_lock_bh(&priv->tx_lock);
232 if (priv->tx_queues_stop) {
233 priv->tx_queues_stop = false;
234 spin_unlock_bh(&priv->tx_lock);
235 ath_print(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
236 "Waking up TX queues\n");
237 ieee80211_wake_queues(priv->hw);
240 spin_unlock_bh(&priv->tx_lock);
243 void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
244 enum htc_endpoint_id ep_id, bool txok)
246 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) drv_priv;
247 struct ieee80211_tx_info *tx_info;
252 if (ep_id == priv->mgmt_ep)
253 skb_pull(skb, sizeof(struct tx_mgmt_hdr));
255 /* TODO: Check for cab/uapsd/data */
256 skb_pull(skb, sizeof(struct tx_frame_hdr));
258 tx_info = IEEE80211_SKB_CB(skb);
261 tx_info->flags |= IEEE80211_TX_STAT_ACK;
263 skb_queue_tail(&priv->tx_queue, skb);
264 tasklet_schedule(&priv->tx_tasklet);
267 int ath9k_tx_init(struct ath9k_htc_priv *priv)
269 skb_queue_head_init(&priv->tx_queue);
273 void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
278 bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv,
279 enum ath9k_tx_queue_subtype subtype)
281 struct ath_hw *ah = priv->ah;
282 struct ath_common *common = ath9k_hw_common(ah);
283 struct ath9k_tx_queue_info qi;
286 memset(&qi, 0, sizeof(qi));
288 qi.tqi_subtype = subtype;
289 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
290 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
291 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
292 qi.tqi_physCompBuf = 0;
293 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | TXQ_FLAG_TXDESCINT_ENABLE;
295 qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
299 if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
300 ath_print(common, ATH_DBG_FATAL,
301 "qnum %u out of range, max %u!\n",
302 qnum, (unsigned int)ARRAY_SIZE(priv->hwq_map));
303 ath9k_hw_releasetxqueue(ah, qnum);
307 priv->hwq_map[subtype] = qnum;
316 * Calculate the RX filter to be set in the HW.
318 u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
320 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
322 struct ath_hw *ah = priv->ah;
325 rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
326 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
327 | ATH9K_RX_FILTER_MCAST;
329 /* If not a STA, enable processing of Probe Requests */
330 if (ah->opmode != NL80211_IFTYPE_STATION)
331 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
334 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
335 * mode interface or when in monitor mode. AP mode does not need this
336 * since it receives all in-BSS frames anyway.
338 if (((ah->opmode != NL80211_IFTYPE_AP) &&
339 (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
340 (ah->opmode == NL80211_IFTYPE_MONITOR))
341 rfilt |= ATH9K_RX_FILTER_PROM;
343 if (priv->rxfilter & FIF_CONTROL)
344 rfilt |= ATH9K_RX_FILTER_CONTROL;
346 if ((ah->opmode == NL80211_IFTYPE_STATION) &&
347 !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
348 rfilt |= ATH9K_RX_FILTER_MYBEACON;
350 rfilt |= ATH9K_RX_FILTER_BEACON;
352 if (conf_is_ht(&priv->hw->conf))
353 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
357 #undef RX_FILTER_PRESERVE
361 * Recv initialization for opmode change.
363 static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
365 struct ath_hw *ah = priv->ah;
366 struct ath_common *common = ath9k_hw_common(ah);
370 /* configure rx filter */
371 rfilt = ath9k_htc_calcrxfilter(priv);
372 ath9k_hw_setrxfilter(ah, rfilt);
374 /* configure bssid mask */
375 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
376 ath_hw_setbssidmask(common);
378 /* configure operational mode */
379 ath9k_hw_setopmode(ah);
381 /* Handle any link-level address change. */
382 ath9k_hw_setmac(ah, common->macaddr);
384 /* calculate and install multicast filter */
385 mfilt[0] = mfilt[1] = ~0;
386 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
389 void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
391 ath9k_hw_rxena(priv->ah);
392 ath9k_htc_opmode_init(priv);
393 ath9k_hw_startpcureceive(priv->ah);
394 priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
397 static void ath9k_process_rate(struct ieee80211_hw *hw,
398 struct ieee80211_rx_status *rxs,
399 u8 rx_rate, u8 rs_flags)
401 struct ieee80211_supported_band *sband;
402 enum ieee80211_band band;
405 if (rx_rate & 0x80) {
407 rxs->flag |= RX_FLAG_HT;
408 if (rs_flags & ATH9K_RX_2040)
409 rxs->flag |= RX_FLAG_40MHZ;
410 if (rs_flags & ATH9K_RX_GI)
411 rxs->flag |= RX_FLAG_SHORT_GI;
412 rxs->rate_idx = rx_rate & 0x7f;
416 band = hw->conf.channel->band;
417 sband = hw->wiphy->bands[band];
419 for (i = 0; i < sband->n_bitrates; i++) {
420 if (sband->bitrates[i].hw_value == rx_rate) {
424 if (sband->bitrates[i].hw_value_short == rx_rate) {
426 rxs->flag |= RX_FLAG_SHORTPRE;
433 static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
434 struct ath9k_htc_rxbuf *rxbuf,
435 struct ieee80211_rx_status *rx_status)
438 struct ieee80211_hdr *hdr;
439 struct ieee80211_hw *hw = priv->hw;
440 struct sk_buff *skb = rxbuf->skb;
441 struct ath_common *common = ath9k_hw_common(priv->ah);
442 int hdrlen, padpos, padsize;
443 int last_rssi = ATH_RSSI_DUMMY_MARKER;
446 hdr = (struct ieee80211_hdr *)skb->data;
447 fc = hdr->frame_control;
448 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
450 padpos = ath9k_cmn_padpos(fc);
452 padsize = padpos & 3;
453 if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
454 memmove(skb->data + padsize, skb->data, padpos);
455 skb_pull(skb, padsize);
458 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
460 if (rxbuf->rxstatus.rs_status != 0) {
461 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
462 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
463 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
466 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
468 } else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
469 if (ieee80211_is_ctl(fc))
471 * Sometimes, we get invalid
472 * MIC failures on valid control frames.
473 * Remove these mic errors.
475 rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
477 rx_status->flag |= RX_FLAG_MMIC_ERROR;
481 * Reject error frames with the exception of
482 * decryption and MIC failures. For monitor mode,
483 * we also ignore the CRC error.
485 if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
486 if (rxbuf->rxstatus.rs_status &
487 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
491 if (rxbuf->rxstatus.rs_status &
492 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
498 if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
500 keyix = rxbuf->rxstatus.rs_keyix;
501 if (keyix != ATH9K_RXKEYIX_INVALID) {
502 rx_status->flag |= RX_FLAG_DECRYPTED;
503 } else if (ieee80211_has_protected(fc) &&
504 skb->len >= hdrlen + 4) {
505 keyix = skb->data[hdrlen + 3] >> 6;
506 if (test_bit(keyix, common->keymap))
507 rx_status->flag |= RX_FLAG_DECRYPTED;
511 ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
512 rxbuf->rxstatus.rs_flags);
514 if (priv->op_flags & OP_ASSOCIATED) {
515 if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
516 !rxbuf->rxstatus.rs_moreaggr)
517 ATH_RSSI_LPF(priv->rx.last_rssi,
518 rxbuf->rxstatus.rs_rssi);
520 last_rssi = priv->rx.last_rssi;
522 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
523 rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
524 ATH_RSSI_EP_MULTIPLIER);
526 if (rxbuf->rxstatus.rs_rssi < 0)
527 rxbuf->rxstatus.rs_rssi = 0;
529 if (ieee80211_is_beacon(fc))
530 priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
533 rx_status->mactime = rxbuf->rxstatus.rs_tstamp;
534 rx_status->band = hw->conf.channel->band;
535 rx_status->freq = hw->conf.channel->center_freq;
536 rx_status->signal = rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
537 rx_status->antenna = rxbuf->rxstatus.rs_antenna;
538 rx_status->flag |= RX_FLAG_TSFT;
547 * FIXME: Handle FLUSH later on.
549 void ath9k_rx_tasklet(unsigned long data)
551 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
552 struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
553 struct ieee80211_rx_status rx_status;
556 struct ieee80211_hdr *hdr;
559 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
560 list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
561 if (tmp_buf->in_process) {
568 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
575 if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
576 dev_kfree_skb_any(rxbuf->skb);
580 memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
581 sizeof(struct ieee80211_rx_status));
583 hdr = (struct ieee80211_hdr *) skb->data;
585 if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
586 ieee80211_queue_work(priv->hw, &priv->ps_work);
588 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
590 ieee80211_rx(priv->hw, skb);
592 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
594 rxbuf->in_process = false;
596 list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
598 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
603 void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
604 enum htc_endpoint_id ep_id)
606 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
607 struct ath_hw *ah = priv->ah;
608 struct ath_common *common = ath9k_hw_common(ah);
609 struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
610 struct ath_htc_rx_status *rxstatus;
613 spin_lock(&priv->rx.rxbuflock);
614 list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
615 if (!tmp_buf->in_process) {
620 spin_unlock(&priv->rx.rxbuflock);
623 ath_print(common, ATH_DBG_ANY,
624 "No free RX buffer\n");
629 if (len <= HTC_RX_FRAME_HEADER_SIZE) {
630 ath_print(common, ATH_DBG_FATAL,
631 "Corrupted RX frame, dropping\n");
635 rxstatus = (struct ath_htc_rx_status *)skb->data;
637 rxstatus->rs_tstamp = be64_to_cpu(rxstatus->rs_tstamp);
638 rxstatus->rs_datalen = be16_to_cpu(rxstatus->rs_datalen);
639 rxstatus->evm0 = be32_to_cpu(rxstatus->evm0);
640 rxstatus->evm1 = be32_to_cpu(rxstatus->evm1);
641 rxstatus->evm2 = be32_to_cpu(rxstatus->evm2);
643 if (rxstatus->rs_datalen - (len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
644 ath_print(common, ATH_DBG_FATAL,
645 "Corrupted RX data len, dropping "
646 "(epid: %d, dlen: %d, skblen: %d)\n",
647 ep_id, rxstatus->rs_datalen, len);
651 spin_lock(&priv->rx.rxbuflock);
652 memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
653 skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
655 rxbuf->in_process = true;
656 spin_unlock(&priv->rx.rxbuflock);
658 tasklet_schedule(&priv->rx_tasklet);
661 dev_kfree_skb_any(skb);
665 /* FIXME: Locking for cleanup/init */
667 void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
669 struct ath9k_htc_rxbuf *rxbuf, *tbuf;
671 list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
672 list_del(&rxbuf->list);
674 dev_kfree_skb_any(rxbuf->skb);
679 int ath9k_rx_init(struct ath9k_htc_priv *priv)
681 struct ath_hw *ah = priv->ah;
682 struct ath_common *common = ath9k_hw_common(ah);
683 struct ath9k_htc_rxbuf *rxbuf;
686 INIT_LIST_HEAD(&priv->rx.rxbuf);
687 spin_lock_init(&priv->rx.rxbuflock);
689 for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
690 rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
692 ath_print(common, ATH_DBG_FATAL,
693 "Unable to allocate RX buffers\n");
696 list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
702 ath9k_rx_cleanup(priv);