2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <linux/etherdevice.h>
20 mt76_mac_process_tx_rate(struct ieee80211_tx_rate *txrate, u16 rate)
22 u8 idx = MT76_GET(MT_TXWI_RATE_MCS, rate);
28 switch (MT76_GET(MT_TXWI_RATE_PHY_MODE, rate)) {
29 case MT_PHY_TYPE_OFDM:
30 txrate->idx = idx + 4;
38 case MT_PHY_TYPE_HT_GF:
39 txrate->flags |= IEEE80211_TX_RC_GREEN_FIELD;
42 txrate->flags |= IEEE80211_TX_RC_MCS;
50 if (MT76_GET(MT_TXWI_RATE_BW, rate) == MT_PHY_BW_40)
51 txrate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
53 if (rate & MT_TXWI_RATE_SGI)
54 txrate->flags |= IEEE80211_TX_RC_SHORT_GI;
58 mt76_mac_fill_tx_status(struct mt7601u_dev *dev, struct ieee80211_tx_info *info,
59 struct mt76_tx_status *st)
61 struct ieee80211_tx_rate *rate = info->status.rates;
62 int cur_idx, last_rate;
65 last_rate = min_t(int, st->retry, IEEE80211_TX_MAX_RATES - 1);
66 mt76_mac_process_tx_rate(&rate[last_rate], st->rate);
67 if (last_rate < IEEE80211_TX_MAX_RATES - 1)
68 rate[last_rate + 1].idx = -1;
70 cur_idx = rate[last_rate].idx + st->retry;
71 for (i = 0; i <= last_rate; i++) {
72 rate[i].flags = rate[last_rate].flags;
73 rate[i].idx = max_t(int, 0, cur_idx - i);
78 rate[last_rate - 1].count = st->retry + 1 - last_rate;
80 info->status.ampdu_len = 1;
81 info->status.ampdu_ack_len = st->success;
84 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
87 info->flags |= IEEE80211_TX_CTL_AMPDU |
88 IEEE80211_TX_STAT_AMPDU;
91 info->flags |= IEEE80211_TX_CTL_NO_ACK;
93 info->flags |= IEEE80211_TX_STAT_ACK;
96 u16 mt76_mac_tx_rate_val(struct mt7601u_dev *dev,
97 const struct ieee80211_tx_rate *rate, u8 *nss_val)
104 if (rate->flags & IEEE80211_TX_RC_MCS) {
105 rate_idx = rate->idx;
106 nss = 1 + (rate->idx >> 3);
107 phy = MT_PHY_TYPE_HT;
108 if (rate->flags & IEEE80211_TX_RC_GREEN_FIELD)
109 phy = MT_PHY_TYPE_HT_GF;
110 if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
113 const struct ieee80211_rate *r;
114 int band = dev->chandef.chan->band;
117 r = &dev->hw->wiphy->bands[band]->bitrates[rate->idx];
118 if (rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
119 val = r->hw_value_short;
124 rate_idx = val & 0xff;
128 rateval = MT76_SET(MT_RXWI_RATE_MCS, rate_idx);
129 rateval |= MT76_SET(MT_RXWI_RATE_PHY, phy);
130 rateval |= MT76_SET(MT_RXWI_RATE_BW, bw);
131 if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
132 rateval |= MT_RXWI_RATE_SGI;
138 void mt76_mac_wcid_set_rate(struct mt7601u_dev *dev, struct mt76_wcid *wcid,
139 const struct ieee80211_tx_rate *rate)
143 spin_lock_irqsave(&dev->lock, flags);
144 wcid->tx_rate = mt76_mac_tx_rate_val(dev, rate, &wcid->tx_rate_nss);
145 wcid->tx_rate_set = true;
146 spin_unlock_irqrestore(&dev->lock, flags);
149 struct mt76_tx_status mt7601u_mac_fetch_tx_status(struct mt7601u_dev *dev)
151 struct mt76_tx_status stat = {};
154 val = mt7601u_rr(dev, MT_TX_STAT_FIFO);
155 stat.valid = !!(val & MT_TX_STAT_FIFO_VALID);
156 stat.success = !!(val & MT_TX_STAT_FIFO_SUCCESS);
157 stat.aggr = !!(val & MT_TX_STAT_FIFO_AGGR);
158 stat.ack_req = !!(val & MT_TX_STAT_FIFO_ACKREQ);
159 stat.pktid = MT76_GET(MT_TX_STAT_FIFO_PID_TYPE, val);
160 stat.wcid = MT76_GET(MT_TX_STAT_FIFO_WCID, val);
161 stat.rate = MT76_GET(MT_TX_STAT_FIFO_RATE, val);
166 void mt76_send_tx_status(struct mt7601u_dev *dev, struct mt76_tx_status *stat)
168 struct ieee80211_tx_info info = {};
169 struct ieee80211_sta *sta = NULL;
170 struct mt76_wcid *wcid = NULL;
174 if (stat->wcid < ARRAY_SIZE(dev->wcid))
175 wcid = rcu_dereference(dev->wcid[stat->wcid]);
178 msta = container_of(wcid, struct mt76_sta, wcid);
179 sta = container_of(msta, struct ieee80211_sta,
183 mt76_mac_fill_tx_status(dev, &info, stat);
184 ieee80211_tx_status_noskb(dev->hw, sta, &info);
188 void mt7601u_mac_set_protection(struct mt7601u_dev *dev, bool legacy_prot,
191 int mode = ht_mode & IEEE80211_HT_OP_MODE_PROTECTION;
192 bool non_gf = !!(ht_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
197 prot[0] = MT_PROT_NAV_SHORT |
198 MT_PROT_TXOP_ALLOW_ALL |
202 prot[1] |= MT_PROT_CTRL_CTS2SELF;
204 prot[2] = prot[4] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_BW20;
205 prot[3] = prot[5] = MT_PROT_NAV_SHORT | MT_PROT_TXOP_ALLOW_ALL;
208 prot[2] |= MT_PROT_RATE_CCK_11;
209 prot[3] |= MT_PROT_RATE_CCK_11;
210 prot[4] |= MT_PROT_RATE_CCK_11;
211 prot[5] |= MT_PROT_RATE_CCK_11;
213 prot[2] |= MT_PROT_RATE_OFDM_24;
214 prot[3] |= MT_PROT_RATE_DUP_OFDM_24;
215 prot[4] |= MT_PROT_RATE_OFDM_24;
216 prot[5] |= MT_PROT_RATE_DUP_OFDM_24;
220 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
223 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
224 ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
227 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
228 ht_rts[1] = ht_rts[3] = true;
231 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
232 ht_rts[0] = ht_rts[1] = ht_rts[2] = ht_rts[3] = true;
237 ht_rts[2] = ht_rts[3] = true;
239 for (i = 0; i < 4; i++)
241 prot[i + 2] |= MT_PROT_CTRL_RTS_CTS;
243 for (i = 0; i < 6; i++)
244 mt7601u_wr(dev, MT_CCK_PROT_CFG + i * 4, prot[i]);
247 void mt7601u_mac_set_short_preamble(struct mt7601u_dev *dev, bool short_preamb)
250 mt76_set(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
252 mt76_clear(dev, MT_AUTO_RSP_CFG, MT_AUTO_RSP_PREAMB_SHORT);
255 void mt7601u_mac_config_tsf(struct mt7601u_dev *dev, bool enable, int interval)
257 u32 val = mt7601u_rr(dev, MT_BEACON_TIME_CFG);
259 val &= ~(MT_BEACON_TIME_CFG_TIMER_EN |
260 MT_BEACON_TIME_CFG_SYNC_MODE |
261 MT_BEACON_TIME_CFG_TBTT_EN);
264 mt7601u_wr(dev, MT_BEACON_TIME_CFG, val);
268 val &= ~MT_BEACON_TIME_CFG_INTVAL;
269 val |= MT76_SET(MT_BEACON_TIME_CFG_INTVAL, interval << 4) |
270 MT_BEACON_TIME_CFG_TIMER_EN |
271 MT_BEACON_TIME_CFG_SYNC_MODE |
272 MT_BEACON_TIME_CFG_TBTT_EN;
275 static void mt7601u_check_mac_err(struct mt7601u_dev *dev)
277 u32 val = mt7601u_rr(dev, 0x10f4);
279 if (!(val & BIT(29)) || !(val & (BIT(7) | BIT(5))))
282 dev_err(dev->dev, "Error: MAC specific condition occurred\n");
284 mt76_set(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
286 mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_RESET_CSR);
289 void mt7601u_mac_work(struct work_struct *work)
291 struct mt7601u_dev *dev = container_of(work, struct mt7601u_dev,
298 { MT_RX_STA_CNT0, 3, dev->stats.rx_stat },
299 { MT_TX_STA_CNT0, 3, dev->stats.tx_stat },
300 { MT_TX_AGG_STAT, 1, dev->stats.aggr_stat },
301 { MT_MPDU_DENSITY_CNT, 1, dev->stats.zero_len_del },
302 { MT_TX_AGG_CNT_BASE0, 8, &dev->stats.aggr_n[0] },
303 { MT_TX_AGG_CNT_BASE1, 8, &dev->stats.aggr_n[16] },
308 /* Note: using MCU_RANDOM_READ is actually slower then reading all the
309 * registers by hand. MCU takes ca. 20ms to complete read of 24
310 * registers while reading them one by one will takes roughly
317 for (i = 0; i < ARRAY_SIZE(spans); i++)
318 for (j = 0; j < spans[i].span; j++) {
319 u32 val = mt7601u_rr(dev, spans[i].addr_base + j * 4);
321 spans[i].stat_base[j * 2] += val & 0xffff;
322 spans[i].stat_base[j * 2 + 1] += val >> 16;
324 /* Calculate average AMPDU length */
325 if (spans[i].addr_base != MT_TX_AGG_CNT_BASE0 &&
326 spans[i].addr_base != MT_TX_AGG_CNT_BASE1)
329 n += (val >> 16) + (val & 0xffff);
330 sum += (val & 0xffff) * (1 + k * 2) +
331 (val >> 16) * (2 + k * 2);
335 atomic_set(&dev->avg_ampdu_len, n ? DIV_ROUND_CLOSEST(sum, n) : 1);
337 mt7601u_check_mac_err(dev);
339 ieee80211_queue_delayed_work(dev->hw, &dev->mac_work, 10 * HZ);
343 mt7601u_mac_wcid_setup(struct mt7601u_dev *dev, u8 idx, u8 vif_idx, u8 *mac)
345 u8 zmac[ETH_ALEN] = {};
348 attr = MT76_SET(MT_WCID_ATTR_BSS_IDX, vif_idx & 7) |
349 MT76_SET(MT_WCID_ATTR_BSS_IDX_EXT, !!(vif_idx & 8));
351 mt76_wr(dev, MT_WCID_ATTR(idx), attr);
354 memcpy(zmac, mac, sizeof(zmac));
356 mt7601u_addr_wr(dev, MT_WCID_ADDR(idx), zmac);
359 void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev *dev)
361 struct ieee80211_sta *sta;
362 struct mt76_wcid *wcid;
368 for (i = 0; i < ARRAY_SIZE(dev->wcid); i++) {
369 wcid = rcu_dereference(dev->wcid[i]);
373 msta = container_of(wcid, struct mt76_sta, wcid);
374 sta = container_of(msta, struct ieee80211_sta, drv_priv);
376 min_factor = min(min_factor, sta->ht_cap.ampdu_factor);
380 mt7601u_wr(dev, MT_MAX_LEN_CFG, 0xa0fff |
381 MT76_SET(MT_MAX_LEN_CFG_AMPDU, min_factor));
385 mt76_mac_process_rate(struct ieee80211_rx_status *status, u16 rate)
387 u8 idx = MT76_GET(MT_RXWI_RATE_MCS, rate);
389 switch (MT76_GET(MT_RXWI_RATE_PHY, rate)) {
390 case MT_PHY_TYPE_OFDM:
391 if (WARN_ON(idx >= 8))
395 status->rate_idx = idx;
397 case MT_PHY_TYPE_CCK:
400 status->flag |= RX_FLAG_SHORTPRE;
403 if (WARN_ON(idx >= 4))
406 status->rate_idx = idx;
408 case MT_PHY_TYPE_HT_GF:
409 status->flag |= RX_FLAG_HT_GF;
412 status->flag |= RX_FLAG_HT;
413 status->rate_idx = idx;
420 if (rate & MT_RXWI_RATE_SGI)
421 status->flag |= RX_FLAG_SHORT_GI;
423 if (rate & MT_RXWI_RATE_STBC)
424 status->flag |= 1 << RX_FLAG_STBC_SHIFT;
426 if (rate & MT_RXWI_RATE_BW)
427 status->flag |= RX_FLAG_40MHZ;
431 mt7601u_rx_monitor_beacon(struct mt7601u_dev *dev, struct mt7601u_rxwi *rxwi,
434 dev->bcn_freq_off = rxwi->freq_off;
435 dev->bcn_phy_mode = MT76_GET(MT_RXWI_RATE_PHY, rate);
436 dev->avg_rssi = (dev->avg_rssi * 15) / 16 + (rssi << 8);
440 mt7601u_rx_is_our_beacon(struct mt7601u_dev *dev, u8 *data)
442 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
444 return ieee80211_is_beacon(hdr->frame_control) &&
445 ether_addr_equal(hdr->addr2, dev->ap_bssid);
448 u32 mt76_mac_process_rx(struct mt7601u_dev *dev, struct sk_buff *skb,
451 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
452 struct mt7601u_rxwi *rxwi = rxi;
453 u32 len, ctl = le32_to_cpu(rxwi->ctl);
454 u16 rate = le16_to_cpu(rxwi->rate);
457 len = MT76_GET(MT_RXWI_CTL_MPDU_LEN, ctl);
461 if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_DECRYPT)) {
462 status->flag |= RX_FLAG_DECRYPTED;
463 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
466 status->chains = BIT(0);
467 rssi = mt7601u_phy_get_rssi(dev, rxwi, rate);
468 status->chain_signal[0] = status->signal = rssi;
469 status->freq = dev->chandef.chan->center_freq;
470 status->band = dev->chandef.chan->band;
472 mt76_mac_process_rate(status, rate);
474 spin_lock_bh(&dev->con_mon_lock);
475 if (mt7601u_rx_is_our_beacon(dev, data))
476 mt7601u_rx_monitor_beacon(dev, rxwi, rate, rssi);
477 else if (rxwi->rxinfo & cpu_to_le32(MT_RXINFO_U2M))
478 dev->avg_rssi = (dev->avg_rssi * 15) / 16 + (rssi << 8);
479 spin_unlock_bh(&dev->con_mon_lock);
484 static enum mt76_cipher_type
485 mt76_mac_get_key_info(struct ieee80211_key_conf *key, u8 *key_data)
487 memset(key_data, 0, 32);
489 return MT_CIPHER_NONE;
491 if (key->keylen > 32)
492 return MT_CIPHER_NONE;
494 memcpy(key_data, key->key, key->keylen);
496 switch (key->cipher) {
497 case WLAN_CIPHER_SUITE_WEP40:
498 return MT_CIPHER_WEP40;
499 case WLAN_CIPHER_SUITE_WEP104:
500 return MT_CIPHER_WEP104;
501 case WLAN_CIPHER_SUITE_TKIP:
502 return MT_CIPHER_TKIP;
503 case WLAN_CIPHER_SUITE_CCMP:
504 return MT_CIPHER_AES_CCMP;
506 return MT_CIPHER_NONE;
510 int mt76_mac_wcid_set_key(struct mt7601u_dev *dev, u8 idx,
511 struct ieee80211_key_conf *key)
513 enum mt76_cipher_type cipher;
518 cipher = mt76_mac_get_key_info(key, key_data);
519 if (cipher == MT_CIPHER_NONE && key)
522 trace_set_key(dev, idx);
524 mt7601u_wr_copy(dev, MT_WCID_KEY(idx), key_data, sizeof(key_data));
526 memset(iv_data, 0, sizeof(iv_data));
528 iv_data[3] = key->keyidx << 6;
529 if (cipher >= MT_CIPHER_TKIP) {
530 /* Note: start with 1 to comply with spec,
531 * (see comment on common/cmm_wpa.c:4291).
537 mt7601u_wr_copy(dev, MT_WCID_IV(idx), iv_data, sizeof(iv_data));
539 val = mt7601u_rr(dev, MT_WCID_ATTR(idx));
540 val &= ~MT_WCID_ATTR_PKEY_MODE & ~MT_WCID_ATTR_PKEY_MODE_EXT;
541 val |= MT76_SET(MT_WCID_ATTR_PKEY_MODE, cipher & 7) |
542 MT76_SET(MT_WCID_ATTR_PKEY_MODE_EXT, cipher >> 3);
543 val &= ~MT_WCID_ATTR_PAIRWISE;
544 val |= MT_WCID_ATTR_PAIRWISE *
545 !!(key && key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
546 mt7601u_wr(dev, MT_WCID_ATTR(idx), val);
551 int mt76_mac_shared_key_setup(struct mt7601u_dev *dev, u8 vif_idx, u8 key_idx,
552 struct ieee80211_key_conf *key)
554 enum mt76_cipher_type cipher;
558 cipher = mt76_mac_get_key_info(key, key_data);
559 if (cipher == MT_CIPHER_NONE && key)
562 trace_set_shared_key(dev, vif_idx, key_idx);
564 mt7601u_wr_copy(dev, MT_SKEY(vif_idx, key_idx),
565 key_data, sizeof(key_data));
567 val = mt76_rr(dev, MT_SKEY_MODE(vif_idx));
568 val &= ~(MT_SKEY_MODE_MASK << MT_SKEY_MODE_SHIFT(vif_idx, key_idx));
569 val |= cipher << MT_SKEY_MODE_SHIFT(vif_idx, key_idx);
570 mt76_wr(dev, MT_SKEY_MODE(vif_idx), val);