1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
4 #include <linux/etherdevice.h>
5 #include <linux/timekeeping.h>
10 #define to_rssi(field, rxv) ((FIELD_GET(field, rxv) - 220) / 2)
12 #define HE_BITS(f) cpu_to_le16(IEEE80211_RADIOTAP_HE_##f)
13 #define HE_PREP(f, m, v) le16_encode_bits(le32_get_bits(v, MT_CRXV_HE_##m),\
14 IEEE80211_RADIOTAP_HE_##f)
16 static const struct mt7915_dfs_radar_spec etsi_radar_specs = {
17 .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
19 [5] = { 1, 0, 6, 32, 28, 0, 990, 5010, 17, 1, 1 },
20 [6] = { 1, 0, 9, 32, 28, 0, 615, 5010, 27, 1, 1 },
21 [7] = { 1, 0, 15, 32, 28, 0, 240, 445, 27, 1, 1 },
22 [8] = { 1, 0, 12, 32, 28, 0, 240, 510, 42, 1, 1 },
23 [9] = { 1, 1, 0, 0, 0, 0, 2490, 3343, 14, 0, 0, 12, 32, 28, { }, 126 },
24 [10] = { 1, 1, 0, 0, 0, 0, 2490, 3343, 14, 0, 0, 15, 32, 24, { }, 126 },
25 [11] = { 1, 1, 0, 0, 0, 0, 823, 2510, 14, 0, 0, 18, 32, 28, { }, 54 },
26 [12] = { 1, 1, 0, 0, 0, 0, 823, 2510, 14, 0, 0, 27, 32, 24, { }, 54 },
30 static const struct mt7915_dfs_radar_spec fcc_radar_specs = {
31 .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
33 [0] = { 1, 0, 8, 32, 28, 0, 508, 3076, 13, 1, 1 },
34 [1] = { 1, 0, 12, 32, 28, 0, 140, 240, 17, 1, 1 },
35 [2] = { 1, 0, 8, 32, 28, 0, 190, 510, 22, 1, 1 },
36 [3] = { 1, 0, 6, 32, 28, 0, 190, 510, 32, 1, 1 },
37 [4] = { 1, 0, 9, 255, 28, 0, 323, 343, 13, 1, 32 },
41 static const struct mt7915_dfs_radar_spec jp_radar_specs = {
42 .pulse_th = { 110, -10, -80, 40, 5200, 128, 5200 },
44 [0] = { 1, 0, 8, 32, 28, 0, 508, 3076, 13, 1, 1 },
45 [1] = { 1, 0, 12, 32, 28, 0, 140, 240, 17, 1, 1 },
46 [2] = { 1, 0, 8, 32, 28, 0, 190, 510, 22, 1, 1 },
47 [3] = { 1, 0, 6, 32, 28, 0, 190, 510, 32, 1, 1 },
48 [4] = { 1, 0, 9, 255, 28, 0, 323, 343, 13, 1, 32 },
49 [13] = { 1, 0, 7, 32, 28, 0, 3836, 3856, 14, 1, 1 },
50 [14] = { 1, 0, 6, 32, 28, 0, 615, 5010, 110, 1, 1 },
51 [15] = { 1, 1, 0, 0, 0, 0, 15, 5010, 110, 0, 0, 12, 32, 28 },
55 static struct mt76_wcid *mt7915_rx_get_wcid(struct mt7915_dev *dev,
56 u16 idx, bool unicast)
58 struct mt7915_sta *sta;
59 struct mt76_wcid *wcid;
61 if (idx >= ARRAY_SIZE(dev->mt76.wcid))
64 wcid = rcu_dereference(dev->mt76.wcid[idx]);
71 sta = container_of(wcid, struct mt7915_sta, wcid);
75 return &sta->vif->sta.wcid;
78 void mt7915_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps)
82 bool mt7915_mac_wtbl_update(struct mt7915_dev *dev, int idx, u32 mask)
84 mt76_rmw(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_WLAN_IDX,
85 FIELD_PREP(MT_WTBL_UPDATE_WLAN_IDX, idx) | mask);
87 return mt76_poll(dev, MT_WTBL_UPDATE, MT_WTBL_UPDATE_BUSY,
91 static u32 mt7915_mac_wtbl_lmac_addr(struct mt7915_dev *dev, u16 wcid)
93 mt76_wr(dev, MT_WTBLON_TOP_WDUCR,
94 FIELD_PREP(MT_WTBLON_TOP_WDUCR_GROUP, (wcid >> 7)));
96 return MT_WTBL_LMAC_OFFS(wcid, 0);
99 /* TODO: use txfree airtime info to avoid runtime accessing in the long run */
100 static void mt7915_mac_sta_poll(struct mt7915_dev *dev)
102 static const u8 ac_to_tid[] = {
103 [IEEE80211_AC_BE] = 0,
104 [IEEE80211_AC_BK] = 1,
105 [IEEE80211_AC_VI] = 4,
106 [IEEE80211_AC_VO] = 6
108 struct ieee80211_sta *sta;
109 struct mt7915_sta *msta;
110 u32 tx_time[IEEE80211_NUM_ACS], rx_time[IEEE80211_NUM_ACS];
111 LIST_HEAD(sta_poll_list);
114 spin_lock_bh(&dev->sta_poll_lock);
115 list_splice_init(&dev->sta_poll_list, &sta_poll_list);
116 spin_unlock_bh(&dev->sta_poll_lock);
125 spin_lock_bh(&dev->sta_poll_lock);
126 if (list_empty(&sta_poll_list)) {
127 spin_unlock_bh(&dev->sta_poll_lock);
130 msta = list_first_entry(&sta_poll_list,
131 struct mt7915_sta, poll_list);
132 list_del_init(&msta->poll_list);
133 spin_unlock_bh(&dev->sta_poll_lock);
135 idx = msta->wcid.idx;
136 addr = mt7915_mac_wtbl_lmac_addr(dev, idx) + 20 * 4;
138 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
139 u32 tx_last = msta->airtime_ac[i];
140 u32 rx_last = msta->airtime_ac[i + 4];
142 msta->airtime_ac[i] = mt76_rr(dev, addr);
143 msta->airtime_ac[i + 4] = mt76_rr(dev, addr + 4);
145 tx_time[i] = msta->airtime_ac[i] - tx_last;
146 rx_time[i] = msta->airtime_ac[i + 4] - rx_last;
148 if ((tx_last | rx_last) & BIT(30))
155 mt7915_mac_wtbl_update(dev, idx,
156 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
157 memset(msta->airtime_ac, 0, sizeof(msta->airtime_ac));
163 sta = container_of((void *)msta, struct ieee80211_sta,
165 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
166 u8 q = mt7915_lmac_mapping(dev, i);
167 u32 tx_cur = tx_time[q];
168 u32 rx_cur = rx_time[q];
169 u8 tid = ac_to_tid[i];
171 if (!tx_cur && !rx_cur)
174 ieee80211_sta_register_airtime(sta, tid, tx_cur,
183 mt7915_mac_decode_he_radiotap_ru(struct mt76_rx_status *status,
184 struct ieee80211_radiotap_he *he,
190 ru_l = FIELD_GET(MT_PRXV_HE_RU_ALLOC_L, le32_to_cpu(rxv[0]));
191 ru_h = FIELD_GET(MT_PRXV_HE_RU_ALLOC_H, le32_to_cpu(rxv[1]));
192 ru = (u8)(ru_l | ru_h << 4);
194 status->bw = RATE_INFO_BW_HE_RU;
198 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
202 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
206 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
210 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
214 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
218 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
221 status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
225 he->data1 |= HE_BITS(DATA1_BW_RU_ALLOC_KNOWN);
226 he->data2 |= HE_BITS(DATA2_RU_OFFSET_KNOWN) |
227 le16_encode_bits(offs,
228 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
232 mt7915_mac_decode_he_radiotap(struct sk_buff *skb,
233 struct mt76_rx_status *status,
234 __le32 *rxv, u32 phy)
236 /* TODO: struct ieee80211_radiotap_he_mu */
237 static const struct ieee80211_radiotap_he known = {
238 .data1 = HE_BITS(DATA1_DATA_MCS_KNOWN) |
239 HE_BITS(DATA1_DATA_DCM_KNOWN) |
240 HE_BITS(DATA1_STBC_KNOWN) |
241 HE_BITS(DATA1_CODING_KNOWN) |
242 HE_BITS(DATA1_LDPC_XSYMSEG_KNOWN) |
243 HE_BITS(DATA1_DOPPLER_KNOWN) |
244 HE_BITS(DATA1_BSS_COLOR_KNOWN),
245 .data2 = HE_BITS(DATA2_GI_KNOWN) |
246 HE_BITS(DATA2_TXBF_KNOWN) |
247 HE_BITS(DATA2_PE_DISAMBIG_KNOWN) |
248 HE_BITS(DATA2_TXOP_KNOWN),
250 struct ieee80211_radiotap_he *he = NULL;
251 u32 ltf_size = le32_get_bits(rxv[2], MT_CRXV_HE_LTF_SIZE) + 1;
253 he = skb_push(skb, sizeof(known));
254 memcpy(he, &known, sizeof(known));
256 he->data3 = HE_PREP(DATA3_BSS_COLOR, BSS_COLOR, rxv[14]) |
257 HE_PREP(DATA3_LDPC_XSYMSEG, LDPC_EXT_SYM, rxv[2]);
258 he->data5 = HE_PREP(DATA5_PE_DISAMBIG, PE_DISAMBIG, rxv[2]) |
259 le16_encode_bits(ltf_size,
260 IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
261 he->data6 = HE_PREP(DATA6_TXOP, TXOP_DUR, rxv[14]) |
262 HE_PREP(DATA6_DOPPLER, DOPPLER, rxv[14]);
265 case MT_PHY_TYPE_HE_SU:
266 he->data1 |= HE_BITS(DATA1_FORMAT_SU) |
267 HE_BITS(DATA1_UL_DL_KNOWN) |
268 HE_BITS(DATA1_BEAM_CHANGE_KNOWN) |
269 HE_BITS(DATA1_SPTL_REUSE_KNOWN);
271 he->data3 |= HE_PREP(DATA3_BEAM_CHANGE, BEAM_CHNG, rxv[14]) |
272 HE_PREP(DATA3_UL_DL, UPLINK, rxv[2]);
273 he->data4 |= HE_PREP(DATA4_SU_MU_SPTL_REUSE, SR_MASK, rxv[11]);
275 case MT_PHY_TYPE_HE_EXT_SU:
276 he->data1 |= HE_BITS(DATA1_FORMAT_EXT_SU) |
277 HE_BITS(DATA1_UL_DL_KNOWN);
279 he->data3 |= HE_PREP(DATA3_UL_DL, UPLINK, rxv[2]);
281 case MT_PHY_TYPE_HE_MU:
282 he->data1 |= HE_BITS(DATA1_FORMAT_MU) |
283 HE_BITS(DATA1_UL_DL_KNOWN) |
284 HE_BITS(DATA1_SPTL_REUSE_KNOWN);
286 he->data3 |= HE_PREP(DATA3_UL_DL, UPLINK, rxv[2]);
287 he->data4 |= HE_PREP(DATA4_SU_MU_SPTL_REUSE, SR_MASK, rxv[11]);
289 mt7915_mac_decode_he_radiotap_ru(status, he, rxv);
291 case MT_PHY_TYPE_HE_TB:
292 he->data1 |= HE_BITS(DATA1_FORMAT_TRIG) |
293 HE_BITS(DATA1_SPTL_REUSE_KNOWN) |
294 HE_BITS(DATA1_SPTL_REUSE2_KNOWN) |
295 HE_BITS(DATA1_SPTL_REUSE3_KNOWN) |
296 HE_BITS(DATA1_SPTL_REUSE4_KNOWN);
298 he->data4 |= HE_PREP(DATA4_TB_SPTL_REUSE1, SR_MASK, rxv[11]) |
299 HE_PREP(DATA4_TB_SPTL_REUSE2, SR1_MASK, rxv[11]) |
300 HE_PREP(DATA4_TB_SPTL_REUSE3, SR2_MASK, rxv[11]) |
301 HE_PREP(DATA4_TB_SPTL_REUSE4, SR3_MASK, rxv[11]);
303 mt7915_mac_decode_he_radiotap_ru(status, he, rxv);
310 int mt7915_mac_fill_rx(struct mt7915_dev *dev, struct sk_buff *skb)
312 struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
313 struct mt76_phy *mphy = &dev->mt76.phy;
314 struct mt7915_phy *phy = &dev->phy;
315 struct ieee80211_supported_band *sband;
316 struct ieee80211_hdr *hdr;
317 __le32 *rxd = (__le32 *)skb->data;
320 u32 rxd1 = le32_to_cpu(rxd[1]);
321 u32 rxd2 = le32_to_cpu(rxd[2]);
322 u32 rxd3 = le32_to_cpu(rxd[3]);
323 bool unicast, insert_ccmp_hdr = false;
327 memset(status, 0, sizeof(*status));
329 if (rxd1 & MT_RXD1_NORMAL_BAND_IDX) {
330 mphy = dev->mt76.phy2;
335 status->ext_phy = true;
338 if (!test_bit(MT76_STATE_RUNNING, &mphy->state))
341 unicast = FIELD_GET(MT_RXD3_NORMAL_ADDR_TYPE, rxd3) == MT_RXD3_NORMAL_U2M;
342 idx = FIELD_GET(MT_RXD1_NORMAL_WLAN_IDX, rxd1);
343 status->wcid = mt7915_rx_get_wcid(dev, idx, unicast);
346 struct mt7915_sta *msta;
348 msta = container_of(status->wcid, struct mt7915_sta, wcid);
349 spin_lock_bh(&dev->sta_poll_lock);
350 if (list_empty(&msta->poll_list))
351 list_add_tail(&msta->poll_list, &dev->sta_poll_list);
352 spin_unlock_bh(&dev->sta_poll_lock);
355 status->freq = mphy->chandef.chan->center_freq;
356 status->band = mphy->chandef.chan->band;
357 if (status->band == NL80211_BAND_5GHZ)
358 sband = &mphy->sband_5g.sband;
360 sband = &mphy->sband_2g.sband;
362 if (!sband->channels)
365 if (rxd1 & MT_RXD1_NORMAL_FCS_ERR)
366 status->flag |= RX_FLAG_FAILED_FCS_CRC;
368 if (rxd1 & MT_RXD1_NORMAL_TKIP_MIC_ERR)
369 status->flag |= RX_FLAG_MMIC_ERROR;
371 if (FIELD_GET(MT_RXD1_NORMAL_SEC_MODE, rxd1) != 0 &&
372 !(rxd1 & (MT_RXD1_NORMAL_CLM | MT_RXD1_NORMAL_CM))) {
373 status->flag |= RX_FLAG_DECRYPTED;
374 status->flag |= RX_FLAG_IV_STRIPPED;
375 status->flag |= RX_FLAG_MMIC_STRIPPED | RX_FLAG_MIC_STRIPPED;
378 if (!(rxd2 & MT_RXD2_NORMAL_NON_AMPDU)) {
379 status->flag |= RX_FLAG_AMPDU_DETAILS;
381 /* all subframes of an A-MPDU have the same timestamp */
382 if (phy->rx_ampdu_ts != rxd[14]) {
383 if (!++phy->ampdu_ref)
386 phy->rx_ampdu_ts = rxd[14];
388 status->ampdu_ref = phy->ampdu_ref;
391 remove_pad = FIELD_GET(MT_RXD2_NORMAL_HDR_OFFSET, rxd2);
393 if (rxd2 & MT_RXD2_NORMAL_MAX_LEN_ERROR)
397 if (rxd1 & MT_RXD1_NORMAL_GROUP_4) {
399 if ((u8 *)rxd - skb->data >= skb->len)
403 if (rxd1 & MT_RXD1_NORMAL_GROUP_1) {
404 u8 *data = (u8 *)rxd;
406 if (status->flag & RX_FLAG_DECRYPTED) {
407 status->iv[0] = data[5];
408 status->iv[1] = data[4];
409 status->iv[2] = data[3];
410 status->iv[3] = data[2];
411 status->iv[4] = data[1];
412 status->iv[5] = data[0];
414 insert_ccmp_hdr = FIELD_GET(MT_RXD2_NORMAL_FRAG, rxd2);
417 if ((u8 *)rxd - skb->data >= skb->len)
421 if (rxd1 & MT_RXD1_NORMAL_GROUP_2) {
423 if ((u8 *)rxd - skb->data >= skb->len)
427 /* RXD Group 3 - P-RXV */
428 if (rxd1 & MT_RXD1_NORMAL_GROUP_3) {
433 if ((u8 *)rxd - skb->data >= skb->len)
436 v0 = le32_to_cpu(rxv[0]);
437 v1 = le32_to_cpu(rxv[1]);
438 v2 = le32_to_cpu(rxv[2]);
440 if (v0 & MT_PRXV_HT_AD_CODE)
441 status->enc_flags |= RX_ENC_FLAG_LDPC;
443 status->chains = mphy->antenna_mask;
444 status->chain_signal[0] = to_rssi(MT_PRXV_RCPI0, v1);
445 status->chain_signal[1] = to_rssi(MT_PRXV_RCPI1, v1);
446 status->chain_signal[2] = to_rssi(MT_PRXV_RCPI2, v1);
447 status->chain_signal[3] = to_rssi(MT_PRXV_RCPI3, v1);
448 status->signal = status->chain_signal[0];
450 for (i = 1; i < hweight8(mphy->antenna_mask); i++) {
451 if (!(status->chains & BIT(i)))
454 status->signal = max(status->signal,
455 status->chain_signal[i]);
458 /* RXD Group 5 - C-RXV */
459 if (rxd1 & MT_RXD1_NORMAL_GROUP_5) {
460 u8 stbc = FIELD_GET(MT_CRXV_HT_STBC, v2);
461 u8 gi = FIELD_GET(MT_CRXV_HT_SHORT_GI, v2);
465 if ((u8 *)rxd - skb->data >= skb->len)
468 idx = i = FIELD_GET(MT_PRXV_TX_RATE, v0);
469 mode = FIELD_GET(MT_CRXV_TX_MODE, v2);
472 case MT_PHY_TYPE_CCK:
475 case MT_PHY_TYPE_OFDM:
476 i = mt76_get_rate(&dev->mt76, sband, i, cck);
478 case MT_PHY_TYPE_HT_GF:
480 status->encoding = RX_ENC_HT;
484 case MT_PHY_TYPE_VHT:
486 FIELD_GET(MT_PRXV_NSTS, v0) + 1;
487 status->encoding = RX_ENC_VHT;
491 case MT_PHY_TYPE_HE_MU:
492 status->flag |= RX_FLAG_RADIOTAP_HE_MU;
494 case MT_PHY_TYPE_HE_SU:
495 case MT_PHY_TYPE_HE_EXT_SU:
496 case MT_PHY_TYPE_HE_TB:
498 FIELD_GET(MT_PRXV_NSTS, v0) + 1;
499 status->encoding = RX_ENC_HE;
500 status->flag |= RX_FLAG_RADIOTAP_HE;
503 if (gi <= NL80211_RATE_INFO_HE_GI_3_2)
506 status->he_dcm = !!(idx & MT_PRXV_TX_DCM);
511 status->rate_idx = i;
513 switch (FIELD_GET(MT_CRXV_FRAME_MODE, v2)) {
514 case IEEE80211_STA_RX_BW_20:
516 case IEEE80211_STA_RX_BW_40:
517 if (mode & MT_PHY_TYPE_HE_EXT_SU &&
518 (idx & MT_PRXV_TX_ER_SU_106T)) {
519 status->bw = RATE_INFO_BW_HE_RU;
521 NL80211_RATE_INFO_HE_RU_ALLOC_106;
523 status->bw = RATE_INFO_BW_40;
526 case IEEE80211_STA_RX_BW_80:
527 status->bw = RATE_INFO_BW_80;
529 case IEEE80211_STA_RX_BW_160:
530 status->bw = RATE_INFO_BW_160;
536 status->enc_flags |= RX_ENC_FLAG_STBC_MASK * stbc;
537 if (mode < MT_PHY_TYPE_HE_SU && gi)
538 status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
542 skb_pull(skb, (u8 *)rxd - skb->data + 2 * remove_pad);
544 if (insert_ccmp_hdr) {
545 u8 key_id = FIELD_GET(MT_RXD1_NORMAL_KEY_ID, rxd1);
547 mt76_insert_ccmp_hdr(skb, key_id);
550 if (rxv && status->flag & RX_FLAG_RADIOTAP_HE)
551 mt7915_mac_decode_he_radiotap(skb, status, rxv, mode);
553 hdr = mt76_skb_get_hdr(skb);
554 if (!status->wcid || !ieee80211_is_data_qos(hdr->frame_control))
557 status->aggr = unicast &&
558 !ieee80211_is_qos_nullfunc(hdr->frame_control);
559 status->tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
560 status->seqno = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
565 void mt7915_mac_write_txwi(struct mt7915_dev *dev, __le32 *txwi,
566 struct sk_buff *skb, struct mt76_wcid *wcid,
567 struct ieee80211_key_conf *key, bool beacon)
569 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
570 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
571 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
572 bool multicast = is_multicast_ether_addr(hdr->addr1);
573 struct ieee80211_vif *vif = info->control.vif;
574 struct mt76_phy *mphy = &dev->mphy;
575 bool ext_phy = info->hw_queue & MT_TX_HW_QUEUE_EXT_PHY;
576 u8 fc_type, fc_stype, p_fmt, q_idx, omac_idx = 0, wmm_idx = 0;
577 __le16 fc = hdr->frame_control;
578 u16 tx_count = 15, seqno = 0;
579 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
583 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
585 omac_idx = mvif->omac_idx;
586 wmm_idx = mvif->wmm_idx;
589 if (ext_phy && dev->mt76.phy2)
590 mphy = dev->mt76.phy2;
592 fc_type = (le16_to_cpu(fc) & IEEE80211_FCTL_FTYPE) >> 2;
593 fc_stype = (le16_to_cpu(fc) & IEEE80211_FCTL_STYPE) >> 4;
600 p_fmt = MT_TX_TYPE_FW;
601 q_idx = MT_LMAC_BCN0;
602 } else if (skb_get_queue_mapping(skb) >= MT_TXQ_PSD) {
603 p_fmt = MT_TX_TYPE_CT;
604 q_idx = MT_LMAC_ALTX0;
606 p_fmt = MT_TX_TYPE_CT;
607 q_idx = wmm_idx * MT7915_MAX_WMM_SETS +
608 mt7915_lmac_mapping(dev, skb_get_queue_mapping(skb));
611 if (ieee80211_is_action(fc) &&
612 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
613 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ) {
614 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
616 txwi[5] |= cpu_to_le32(MT_TXD5_ADD_BA);
617 tid = (capab >> 2) & IEEE80211_QOS_CTL_TID_MASK;
618 } else if (ieee80211_is_back_req(hdr->frame_control)) {
619 struct ieee80211_bar *bar = (struct ieee80211_bar *)hdr;
620 u16 control = le16_to_cpu(bar->control);
622 tid = FIELD_GET(IEEE80211_BAR_CTRL_TID_INFO_MASK, control);
625 val = FIELD_PREP(MT_TXD0_TX_BYTES, skb->len + MT_TXD_SIZE) |
626 FIELD_PREP(MT_TXD0_PKT_FMT, p_fmt) |
627 FIELD_PREP(MT_TXD0_Q_IDX, q_idx);
628 txwi[0] = cpu_to_le32(val);
630 val = MT_TXD1_LONG_FORMAT |
631 FIELD_PREP(MT_TXD1_WLAN_IDX, wcid->idx) |
632 FIELD_PREP(MT_TXD1_HDR_FORMAT, MT_HDR_FORMAT_802_11) |
633 FIELD_PREP(MT_TXD1_HDR_INFO,
634 ieee80211_get_hdrlen_from_skb(skb) / 2) |
635 FIELD_PREP(MT_TXD1_TID, tid) |
636 FIELD_PREP(MT_TXD1_OWN_MAC, omac_idx);
638 if (ext_phy && q_idx >= MT_LMAC_ALTX0 && q_idx <= MT_LMAC_BCN0)
641 txwi[1] = cpu_to_le32(val);
643 val = FIELD_PREP(MT_TXD2_FRAME_TYPE, fc_type) |
644 FIELD_PREP(MT_TXD2_SUB_TYPE, fc_stype) |
645 FIELD_PREP(MT_TXD2_MULTICAST, multicast);
647 if (multicast && ieee80211_is_robust_mgmt_frame(skb) &&
648 key->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
652 txwi[3] = cpu_to_le32(MT_TXD3_PROTECT_FRAME);
657 txwi[2] = cpu_to_le32(val);
659 if (!ieee80211_is_data(fc) || multicast) {
662 /* hardware won't add HTC for mgmt/ctrl frame */
663 txwi[2] |= cpu_to_le32(MT_TXD2_FIX_RATE | MT_TXD2_HTC_VLD);
665 if (mphy->chandef.chan->band == NL80211_BAND_5GHZ)
666 rate = MT7915_5G_RATE_DEFAULT;
668 rate = MT7915_2G_RATE_DEFAULT;
670 val = MT_TXD6_FIXED_BW |
671 FIELD_PREP(MT_TXD6_TX_RATE, rate);
672 txwi[6] |= cpu_to_le32(val);
673 txwi[3] |= cpu_to_le32(MT_TXD3_BA_DISABLE);
676 if (!ieee80211_is_beacon(fc))
677 txwi[3] |= cpu_to_le32(MT_TXD3_SW_POWER_MGMT);
681 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
682 txwi[3] |= cpu_to_le32(MT_TXD3_NO_ACK);
684 val = FIELD_PREP(MT_TXD7_TYPE, fc_type) |
685 FIELD_PREP(MT_TXD7_SUB_TYPE, fc_stype);
687 val |= MT_TXD7_HW_AMSDU;
688 txwi[7] = cpu_to_le32(val);
690 val = FIELD_PREP(MT_TXD3_REM_TX_COUNT, tx_count);
691 if (info->flags & IEEE80211_TX_CTL_INJECTED) {
692 seqno = le16_to_cpu(hdr->seq_ctrl);
694 if (ieee80211_is_back_req(hdr->frame_control)) {
695 struct ieee80211_bar *bar;
697 bar = (struct ieee80211_bar *)skb->data;
698 seqno = le16_to_cpu(bar->start_seq_num);
701 val |= MT_TXD3_SN_VALID |
702 FIELD_PREP(MT_TXD3_SEQ, IEEE80211_SEQ_TO_SN(seqno));
704 txwi[3] |= cpu_to_le32(val);
707 int mt7915_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
708 enum mt76_txq_id qid, struct mt76_wcid *wcid,
709 struct ieee80211_sta *sta,
710 struct mt76_tx_info *tx_info)
712 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
713 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
714 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
715 struct ieee80211_key_conf *key = info->control.hw_key;
716 struct ieee80211_vif *vif = info->control.vif;
717 struct mt76_tx_cb *cb = mt76_tx_skb_cb(tx_info->skb);
718 struct mt76_txwi_cache *t;
719 struct mt7915_txp *txp;
720 int id, i, nbuf = tx_info->nbuf - 1;
721 u8 *txwi = (u8 *)txwi_ptr;
724 wcid = &dev->mt76.global_wcid;
726 cb->wcid = wcid->idx;
728 mt7915_mac_write_txwi(dev, txwi_ptr, tx_info->skb, wcid, key,
731 txp = (struct mt7915_txp *)(txwi + MT_TXD_SIZE);
732 for (i = 0; i < nbuf; i++) {
733 txp->buf[i] = cpu_to_le32(tx_info->buf[i + 1].addr);
734 txp->len[i] = cpu_to_le16(tx_info->buf[i + 1].len);
738 /* pass partial skb header to fw */
739 tx_info->buf[1].len = MT_CT_PARSE_LEN;
740 tx_info->buf[1].skip_unmap = true;
741 tx_info->nbuf = MT_CT_DMA_BUF_NUM;
743 txp->flags = cpu_to_le16(MT_CT_INFO_APPLY_TXD);
746 txp->flags |= cpu_to_le16(MT_CT_INFO_NONE_CIPHER_FRAME);
748 if (ieee80211_is_mgmt(hdr->frame_control))
749 txp->flags |= cpu_to_le16(MT_CT_INFO_MGMT_FRAME);
752 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
754 txp->bss_idx = mvif->idx;
757 t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);
758 t->skb = tx_info->skb;
760 spin_lock_bh(&dev->token_lock);
761 id = idr_alloc(&dev->token, t, 0, MT7915_TOKEN_SIZE, GFP_ATOMIC);
762 spin_unlock_bh(&dev->token_lock);
766 txp->token = cpu_to_le16(id);
767 txp->rept_wds_wcid = 0xff;
768 tx_info->skb = DMA_DUMMY_DATA;
774 mt7915_tx_check_aggr(struct ieee80211_sta *sta, __le32 *txwi)
776 struct mt7915_sta *msta;
780 if (!sta || !sta->ht_cap.ht_supported)
783 tid = FIELD_GET(MT_TXD1_TID, le32_to_cpu(txwi[1]));
784 if (tid >= 6) /* skip VO queue */
787 val = le32_to_cpu(txwi[2]);
788 fc = FIELD_GET(MT_TXD2_FRAME_TYPE, val) << 2 |
789 FIELD_GET(MT_TXD2_SUB_TYPE, val) << 4;
790 if (unlikely(fc != (IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_DATA)))
793 msta = (struct mt7915_sta *)sta->drv_priv;
794 if (!test_and_set_bit(tid, &msta->ampdu_state))
795 ieee80211_start_tx_ba_session(sta, tid, 0);
799 mt7915_tx_status(struct ieee80211_sta *sta, struct ieee80211_hw *hw,
800 struct ieee80211_tx_info *info, struct sk_buff *skb)
802 struct ieee80211_tx_status status = {
811 struct mt7915_sta *msta;
813 msta = (struct mt7915_sta *)sta->drv_priv;
814 status.rate = &msta->stats.tx_rate;
817 /* use status_ext to report HE rate */
818 ieee80211_tx_status_ext(hw, &status);
822 mt7915_tx_complete_status(struct mt76_dev *mdev, struct sk_buff *skb,
823 struct ieee80211_sta *sta, u8 stat)
825 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
826 struct ieee80211_hw *hw;
828 hw = mt76_tx_status_get_hw(mdev, skb);
830 if (info->flags & IEEE80211_TX_CTL_AMPDU)
831 info->flags |= IEEE80211_TX_STAT_AMPDU;
834 ieee80211_tx_info_clear_status(info);
836 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
837 info->flags |= IEEE80211_TX_STAT_ACK;
839 info->status.tx_time = 0;
841 if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) {
842 mt7915_tx_status(sta, hw, info, skb);
846 if (sta || !(info->flags & IEEE80211_TX_CTL_NO_ACK))
847 mt7915_tx_status(sta, hw, info, NULL);
849 ieee80211_free_txskb(hw, skb);
852 void mt7915_txp_skb_unmap(struct mt76_dev *dev,
853 struct mt76_txwi_cache *t)
855 struct mt7915_txp *txp;
858 txp = mt7915_txwi_to_txp(dev, t);
859 for (i = 1; i < txp->nbuf; i++)
860 dma_unmap_single(dev->dev, le32_to_cpu(txp->buf[i]),
861 le16_to_cpu(txp->len[i]), DMA_TO_DEVICE);
864 void mt7915_mac_tx_free(struct mt7915_dev *dev, struct sk_buff *skb)
866 struct mt7915_tx_free *free = (struct mt7915_tx_free *)skb->data;
867 struct mt76_dev *mdev = &dev->mt76;
868 struct mt76_txwi_cache *txwi;
869 struct ieee80211_sta *sta = NULL;
872 /* clean DMA queues and unmap buffers first */
873 mt76_queue_tx_cleanup(dev, MT_TXQ_PSD, false);
874 mt76_queue_tx_cleanup(dev, MT_TXQ_BE, false);
877 * TODO: MT_TX_FREE_LATENCY is msdu time from the TXD is queued into PLE,
878 * to the time ack is received or dropped by hw (air + hw queue time).
879 * Should avoid accessing WTBL to get Tx airtime, and use it instead.
881 count = FIELD_GET(MT_TX_FREE_MSDU_CNT, le16_to_cpu(free->ctrl));
882 for (i = 0; i < count; i++) {
883 u32 msdu, info = le32_to_cpu(free->info[i]);
887 * 1'b1: new wcid pair.
888 * 1'b0: msdu_id with the same 'wcid pair' as above.
890 if (info & MT_TX_FREE_PAIR) {
891 struct mt7915_sta *msta;
892 struct mt7915_phy *phy;
893 struct mt76_wcid *wcid;
897 idx = FIELD_GET(MT_TX_FREE_WLAN_ID, info);
898 wcid = rcu_dereference(dev->mt76.wcid[idx]);
899 sta = wcid_to_sta(wcid);
903 msta = container_of(wcid, struct mt7915_sta, wcid);
904 phy = msta->vif->phy;
905 spin_lock_bh(&dev->sta_poll_lock);
906 if (list_empty(&msta->stats_list))
907 list_add_tail(&msta->stats_list, &phy->stats_list);
908 if (list_empty(&msta->poll_list))
909 list_add_tail(&msta->poll_list, &dev->sta_poll_list);
910 spin_unlock_bh(&dev->sta_poll_lock);
913 msdu = FIELD_GET(MT_TX_FREE_MSDU_ID, info);
914 stat = FIELD_GET(MT_TX_FREE_STATUS, info);
916 spin_lock_bh(&dev->token_lock);
917 txwi = idr_remove(&dev->token, msdu);
918 spin_unlock_bh(&dev->token_lock);
923 mt7915_txp_skb_unmap(mdev, txwi);
925 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txwi->skb);
926 void *txwi_ptr = mt76_get_txwi_ptr(mdev, txwi);
928 if (likely(txwi->skb->protocol != cpu_to_be16(ETH_P_PAE)))
929 mt7915_tx_check_aggr(sta, txwi_ptr);
931 if (sta && !info->tx_time_est) {
932 struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
935 pending = atomic_dec_return(&wcid->non_aql_packets);
937 atomic_cmpxchg(&wcid->non_aql_packets, pending, 0);
940 mt7915_tx_complete_status(mdev, txwi->skb, sta, stat);
944 mt76_put_txwi(mdev, txwi);
948 mt7915_mac_sta_poll(dev);
949 mt76_worker_schedule(&dev->mt76.tx_worker);
952 void mt7915_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e)
954 struct mt7915_dev *dev;
957 dev_kfree_skb_any(e->skb);
961 dev = container_of(mdev, struct mt7915_dev, mt76);
964 if (e->skb == DMA_DUMMY_DATA) {
965 struct mt76_txwi_cache *t;
966 struct mt7915_txp *txp;
968 txp = mt7915_txwi_to_txp(mdev, e->txwi);
970 spin_lock_bh(&dev->token_lock);
971 t = idr_remove(&dev->token, le16_to_cpu(txp->token));
972 spin_unlock_bh(&dev->token_lock);
973 e->skb = t ? t->skb : NULL;
977 struct mt76_tx_cb *cb = mt76_tx_skb_cb(e->skb);
978 struct mt76_wcid *wcid;
980 wcid = rcu_dereference(dev->mt76.wcid[cb->wcid]);
982 mt7915_tx_complete_status(mdev, e->skb, wcid_to_sta(wcid), 0);
986 void mt7915_mac_cca_stats_reset(struct mt7915_phy *phy)
988 struct mt7915_dev *dev = phy->dev;
989 bool ext_phy = phy != &dev->phy;
990 u32 reg = MT_WF_PHY_RX_CTRL1(ext_phy);
992 mt7915_l2_clear(dev, reg, MT_WF_PHY_RX_CTRL1_STSCNT_EN);
993 mt7915_l2_set(dev, reg, BIT(11) | BIT(9));
996 void mt7915_mac_reset_counters(struct mt7915_phy *phy)
998 struct mt7915_dev *dev = phy->dev;
999 bool ext_phy = phy != &dev->phy;
1002 for (i = 0; i < 4; i++) {
1003 mt76_rr(dev, MT_TX_AGG_CNT(ext_phy, i));
1004 mt76_rr(dev, MT_TX_AGG_CNT2(ext_phy, i));
1008 dev->mt76.phy2->survey_time = ktime_get_boottime();
1009 i = ARRAY_SIZE(dev->mt76.aggr_stats) / 2;
1011 dev->mt76.phy.survey_time = ktime_get_boottime();
1014 memset(&dev->mt76.aggr_stats[i], 0, sizeof(dev->mt76.aggr_stats) / 2);
1016 /* reset airtime counters */
1017 mt76_rr(dev, MT_MIB_SDR9(ext_phy));
1018 mt76_rr(dev, MT_MIB_SDR36(ext_phy));
1019 mt76_rr(dev, MT_MIB_SDR37(ext_phy));
1021 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(ext_phy),
1022 MT_WF_RMAC_MIB_RXTIME_CLR);
1023 mt76_set(dev, MT_WF_RMAC_MIB_AIRTIME0(ext_phy),
1024 MT_WF_RMAC_MIB_RXTIME_CLR);
1027 void mt7915_mac_set_timing(struct mt7915_phy *phy)
1029 s16 coverage_class = phy->coverage_class;
1030 struct mt7915_dev *dev = phy->dev;
1031 bool ext_phy = phy != &dev->phy;
1032 u32 val, reg_offset;
1033 u32 cck = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 231) |
1034 FIELD_PREP(MT_TIMEOUT_VAL_CCA, 48);
1035 u32 ofdm = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, 60) |
1036 FIELD_PREP(MT_TIMEOUT_VAL_CCA, 28);
1038 bool is_5ghz = phy->mt76->chandef.chan->band == NL80211_BAND_5GHZ;
1040 if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
1049 coverage_class = max_t(s16, dev->phy.coverage_class,
1052 struct mt7915_phy *phy_ext = mt7915_ext_phy(dev);
1055 coverage_class = max_t(s16, phy_ext->coverage_class,
1058 mt76_set(dev, MT_ARB_SCR(ext_phy),
1059 MT_ARB_SCR_TX_DISABLE | MT_ARB_SCR_RX_DISABLE);
1062 offset = 3 * coverage_class;
1063 reg_offset = FIELD_PREP(MT_TIMEOUT_VAL_PLCP, offset) |
1064 FIELD_PREP(MT_TIMEOUT_VAL_CCA, offset);
1066 mt76_wr(dev, MT_TMAC_CDTR(ext_phy), cck + reg_offset);
1067 mt76_wr(dev, MT_TMAC_ODTR(ext_phy), ofdm + reg_offset);
1068 mt76_wr(dev, MT_TMAC_ICR0(ext_phy),
1069 FIELD_PREP(MT_IFS_EIFS, 360) |
1070 FIELD_PREP(MT_IFS_RIFS, 2) |
1071 FIELD_PREP(MT_IFS_SIFS, sifs) |
1072 FIELD_PREP(MT_IFS_SLOT, phy->slottime));
1074 if (phy->slottime < 20 || is_5ghz)
1075 val = MT7915_CFEND_RATE_DEFAULT;
1077 val = MT7915_CFEND_RATE_11B;
1079 mt76_rmw_field(dev, MT_AGG_ACR0(ext_phy), MT_AGG_ACR_CFEND_RATE, val);
1080 mt76_clear(dev, MT_ARB_SCR(ext_phy),
1081 MT_ARB_SCR_TX_DISABLE | MT_ARB_SCR_RX_DISABLE);
1085 * TODO: mib counters are read-clear and there're many HE functionalities need
1086 * such info, hence firmware prepares a task to read the fields out to a shared
1087 * structure. User should switch to use event format to avoid race condition.
1090 mt7915_phy_update_channel(struct mt76_phy *mphy, int idx)
1092 struct mt7915_dev *dev = container_of(mphy->dev, struct mt7915_dev, mt76);
1093 struct mt76_channel_state *state;
1094 u64 busy_time, tx_time, rx_time, obss_time;
1096 busy_time = mt76_get_field(dev, MT_MIB_SDR9(idx),
1097 MT_MIB_SDR9_BUSY_MASK);
1098 tx_time = mt76_get_field(dev, MT_MIB_SDR36(idx),
1099 MT_MIB_SDR36_TXTIME_MASK);
1100 rx_time = mt76_get_field(dev, MT_MIB_SDR37(idx),
1101 MT_MIB_SDR37_RXTIME_MASK);
1102 obss_time = mt76_get_field(dev, MT_WF_RMAC_MIB_AIRTIME14(idx),
1103 MT_MIB_OBSSTIME_MASK);
1105 /* TODO: state->noise */
1106 state = mphy->chan_state;
1107 state->cc_busy += busy_time;
1108 state->cc_tx += tx_time;
1109 state->cc_rx += rx_time + obss_time;
1110 state->cc_bss_rx += rx_time;
1113 void mt7915_update_channel(struct mt76_dev *mdev)
1115 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
1117 mt7915_phy_update_channel(&mdev->phy, 0);
1119 mt7915_phy_update_channel(mdev->phy2, 1);
1121 /* reset obss airtime */
1122 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(0), MT_WF_RMAC_MIB_RXTIME_CLR);
1124 mt76_set(dev, MT_WF_RMAC_MIB_TIME0(1),
1125 MT_WF_RMAC_MIB_RXTIME_CLR);
1129 mt7915_wait_reset_state(struct mt7915_dev *dev, u32 state)
1133 ret = wait_event_timeout(dev->reset_wait,
1134 (READ_ONCE(dev->reset_state) & state),
1135 MT7915_RESET_TIMEOUT);
1137 WARN(!ret, "Timeout waiting for MCU reset state %x\n", state);
1142 mt7915_update_vif_beacon(void *priv, u8 *mac, struct ieee80211_vif *vif)
1144 struct ieee80211_hw *hw = priv;
1146 mt7915_mcu_add_beacon(hw, vif, vif->bss_conf.enable_beacon);
1150 mt7915_update_beacons(struct mt7915_dev *dev)
1152 ieee80211_iterate_active_interfaces(dev->mt76.hw,
1153 IEEE80211_IFACE_ITER_RESUME_ALL,
1154 mt7915_update_vif_beacon, dev->mt76.hw);
1156 if (!dev->mt76.phy2)
1159 ieee80211_iterate_active_interfaces(dev->mt76.phy2->hw,
1160 IEEE80211_IFACE_ITER_RESUME_ALL,
1161 mt7915_update_vif_beacon, dev->mt76.phy2->hw);
1165 mt7915_dma_reset(struct mt7915_dev *dev)
1169 mt76_clear(dev, MT_WFDMA0_GLO_CFG,
1170 MT_WFDMA0_GLO_CFG_TX_DMA_EN | MT_WFDMA0_GLO_CFG_RX_DMA_EN);
1171 mt76_clear(dev, MT_WFDMA1_GLO_CFG,
1172 MT_WFDMA1_GLO_CFG_TX_DMA_EN | MT_WFDMA1_GLO_CFG_RX_DMA_EN);
1173 usleep_range(1000, 2000);
1175 for (i = 0; i < __MT_TXQ_MAX; i++)
1176 mt76_queue_tx_cleanup(dev, i, true);
1178 mt76_for_each_q_rx(&dev->mt76, i) {
1179 mt76_queue_rx_reset(dev, i);
1182 /* re-init prefetch settings after reset */
1183 mt7915_dma_prefetch(dev);
1185 mt76_set(dev, MT_WFDMA0_GLO_CFG,
1186 MT_WFDMA0_GLO_CFG_TX_DMA_EN | MT_WFDMA0_GLO_CFG_RX_DMA_EN);
1187 mt76_set(dev, MT_WFDMA1_GLO_CFG,
1188 MT_WFDMA1_GLO_CFG_TX_DMA_EN | MT_WFDMA1_GLO_CFG_RX_DMA_EN);
1191 /* system error recovery */
1192 void mt7915_mac_reset_work(struct work_struct *work)
1194 struct mt7915_phy *phy2;
1195 struct mt76_phy *ext_phy;
1196 struct mt7915_dev *dev;
1198 dev = container_of(work, struct mt7915_dev, reset_work);
1199 ext_phy = dev->mt76.phy2;
1200 phy2 = ext_phy ? ext_phy->priv : NULL;
1202 if (!(READ_ONCE(dev->reset_state) & MT_MCU_CMD_STOP_DMA))
1205 ieee80211_stop_queues(mt76_hw(dev));
1207 ieee80211_stop_queues(ext_phy->hw);
1209 set_bit(MT76_RESET, &dev->mphy.state);
1210 set_bit(MT76_MCU_RESET, &dev->mphy.state);
1211 wake_up(&dev->mt76.mcu.wait);
1212 cancel_delayed_work_sync(&dev->phy.mac_work);
1214 cancel_delayed_work_sync(&phy2->mac_work);
1216 /* lock/unlock all queues to ensure that no tx is pending */
1217 mt76_txq_schedule_all(&dev->mphy);
1219 mt76_txq_schedule_all(ext_phy);
1221 mt76_worker_disable(&dev->mt76.tx_worker);
1222 napi_disable(&dev->mt76.napi[0]);
1223 napi_disable(&dev->mt76.napi[1]);
1224 napi_disable(&dev->mt76.napi[2]);
1225 napi_disable(&dev->mt76.tx_napi);
1227 mutex_lock(&dev->mt76.mutex);
1229 mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_STOPPED);
1231 if (mt7915_wait_reset_state(dev, MT_MCU_CMD_RESET_DONE)) {
1232 mt7915_dma_reset(dev);
1234 mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_DMA_INIT);
1235 mt7915_wait_reset_state(dev, MT_MCU_CMD_RECOVERY_DONE);
1238 clear_bit(MT76_MCU_RESET, &dev->mphy.state);
1239 clear_bit(MT76_RESET, &dev->mphy.state);
1241 mt76_worker_enable(&dev->mt76.tx_worker);
1242 napi_enable(&dev->mt76.tx_napi);
1243 napi_schedule(&dev->mt76.tx_napi);
1245 napi_enable(&dev->mt76.napi[0]);
1246 napi_schedule(&dev->mt76.napi[0]);
1248 napi_enable(&dev->mt76.napi[1]);
1249 napi_schedule(&dev->mt76.napi[1]);
1251 napi_enable(&dev->mt76.napi[2]);
1252 napi_schedule(&dev->mt76.napi[2]);
1254 ieee80211_wake_queues(mt76_hw(dev));
1256 ieee80211_wake_queues(ext_phy->hw);
1258 mt76_wr(dev, MT_MCU_INT_EVENT, MT_MCU_INT_EVENT_RESET_DONE);
1259 mt7915_wait_reset_state(dev, MT_MCU_CMD_NORMAL_STATE);
1261 mutex_unlock(&dev->mt76.mutex);
1263 mt7915_update_beacons(dev);
1265 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->phy.mac_work,
1266 MT7915_WATCHDOG_TIME);
1268 ieee80211_queue_delayed_work(ext_phy->hw, &phy2->mac_work,
1269 MT7915_WATCHDOG_TIME);
1273 mt7915_mac_update_mib_stats(struct mt7915_phy *phy)
1275 struct mt7915_dev *dev = phy->dev;
1276 struct mib_stats *mib = &phy->mib;
1277 bool ext_phy = phy != &dev->phy;
1278 int i, aggr0, aggr1;
1280 memset(mib, 0, sizeof(*mib));
1282 mib->fcs_err_cnt = mt76_get_field(dev, MT_MIB_SDR3(ext_phy),
1283 MT_MIB_SDR3_FCS_ERR_MASK);
1285 aggr0 = ext_phy ? ARRAY_SIZE(dev->mt76.aggr_stats) / 2 : 0;
1286 for (i = 0, aggr1 = aggr0 + 4; i < 4; i++) {
1289 val = mt76_rr(dev, MT_MIB_MB_SDR1(ext_phy, i));
1291 val2 = FIELD_GET(MT_MIB_ACK_FAIL_COUNT_MASK, val);
1292 if (val2 > mib->ack_fail_cnt)
1293 mib->ack_fail_cnt = val2;
1295 val2 = FIELD_GET(MT_MIB_BA_MISS_COUNT_MASK, val);
1296 if (val2 > mib->ba_miss_cnt)
1297 mib->ba_miss_cnt = val2;
1299 val = mt76_rr(dev, MT_MIB_MB_SDR0(ext_phy, i));
1300 val2 = FIELD_GET(MT_MIB_RTS_RETRIES_COUNT_MASK, val);
1301 if (val2 > mib->rts_retries_cnt) {
1302 mib->rts_cnt = FIELD_GET(MT_MIB_RTS_COUNT_MASK, val);
1303 mib->rts_retries_cnt = val2;
1306 val = mt76_rr(dev, MT_TX_AGG_CNT(ext_phy, i));
1307 val2 = mt76_rr(dev, MT_TX_AGG_CNT2(ext_phy, i));
1309 dev->mt76.aggr_stats[aggr0++] += val & 0xffff;
1310 dev->mt76.aggr_stats[aggr0++] += val >> 16;
1311 dev->mt76.aggr_stats[aggr1++] += val2 & 0xffff;
1312 dev->mt76.aggr_stats[aggr1++] += val2 >> 16;
1317 mt7915_mac_sta_stats_work(struct mt7915_phy *phy)
1319 struct mt7915_dev *dev = phy->dev;
1320 struct mt7915_sta *msta;
1323 spin_lock_bh(&dev->sta_poll_lock);
1324 list_splice_init(&phy->stats_list, &list);
1326 while (!list_empty(&list)) {
1327 msta = list_first_entry(&list, struct mt7915_sta, stats_list);
1328 list_del_init(&msta->stats_list);
1329 spin_unlock_bh(&dev->sta_poll_lock);
1331 /* use MT_TX_FREE_RATE to report Tx rate for further devices */
1332 mt7915_mcu_get_rate_info(dev, RATE_CTRL_RU_INFO, msta->wcid.idx);
1334 spin_lock_bh(&dev->sta_poll_lock);
1337 spin_unlock_bh(&dev->sta_poll_lock);
1340 void mt7915_mac_sta_rc_work(struct work_struct *work)
1342 struct mt7915_dev *dev = container_of(work, struct mt7915_dev, rc_work);
1343 struct ieee80211_sta *sta;
1344 struct ieee80211_vif *vif;
1345 struct mt7915_sta *msta;
1349 spin_lock_bh(&dev->sta_poll_lock);
1350 list_splice_init(&dev->sta_rc_list, &list);
1352 while (!list_empty(&list)) {
1353 msta = list_first_entry(&list, struct mt7915_sta, rc_list);
1354 list_del_init(&msta->rc_list);
1355 changed = msta->stats.changed;
1356 msta->stats.changed = 0;
1357 spin_unlock_bh(&dev->sta_poll_lock);
1359 sta = container_of((void *)msta, struct ieee80211_sta, drv_priv);
1360 vif = container_of((void *)msta->vif, struct ieee80211_vif, drv_priv);
1362 if (changed & (IEEE80211_RC_SUPP_RATES_CHANGED |
1363 IEEE80211_RC_NSS_CHANGED |
1364 IEEE80211_RC_BW_CHANGED))
1365 mt7915_mcu_add_rate_ctrl(dev, vif, sta);
1367 if (changed & IEEE80211_RC_SMPS_CHANGED)
1368 mt7915_mcu_add_smps(dev, vif, sta);
1370 spin_lock_bh(&dev->sta_poll_lock);
1373 spin_unlock_bh(&dev->sta_poll_lock);
1376 void mt7915_mac_work(struct work_struct *work)
1378 struct mt7915_phy *phy;
1379 struct mt76_dev *mdev;
1381 phy = (struct mt7915_phy *)container_of(work, struct mt7915_phy,
1383 mdev = &phy->dev->mt76;
1385 mutex_lock(&mdev->mutex);
1387 mt76_update_survey(mdev);
1388 if (++phy->mac_work_count == 5) {
1389 phy->mac_work_count = 0;
1391 mt7915_mac_update_mib_stats(phy);
1394 if (++phy->sta_work_count == 10) {
1395 phy->sta_work_count = 0;
1396 mt7915_mac_sta_stats_work(phy);
1399 mutex_unlock(&mdev->mutex);
1401 ieee80211_queue_delayed_work(phy->mt76->hw, &phy->mac_work,
1402 MT7915_WATCHDOG_TIME);
1405 static void mt7915_dfs_stop_radar_detector(struct mt7915_phy *phy)
1407 struct mt7915_dev *dev = phy->dev;
1409 if (phy->rdd_state & BIT(0))
1410 mt7915_mcu_rdd_cmd(dev, RDD_STOP, 0, MT_RX_SEL0, 0);
1411 if (phy->rdd_state & BIT(1))
1412 mt7915_mcu_rdd_cmd(dev, RDD_STOP, 1, MT_RX_SEL0, 0);
1415 static int mt7915_dfs_start_rdd(struct mt7915_dev *dev, int chain)
1419 err = mt7915_mcu_rdd_cmd(dev, RDD_START, chain, MT_RX_SEL0, 0);
1423 return mt7915_mcu_rdd_cmd(dev, RDD_DET_MODE, chain, MT_RX_SEL0, 1);
1426 static int mt7915_dfs_start_radar_detector(struct mt7915_phy *phy)
1428 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
1429 struct mt7915_dev *dev = phy->dev;
1430 bool ext_phy = phy != &dev->phy;
1434 err = mt7915_mcu_rdd_cmd(dev, RDD_CAC_START, ext_phy, MT_RX_SEL0, 0);
1438 err = mt7915_dfs_start_rdd(dev, ext_phy);
1442 phy->rdd_state |= BIT(ext_phy);
1444 if (chandef->width == NL80211_CHAN_WIDTH_160 ||
1445 chandef->width == NL80211_CHAN_WIDTH_80P80) {
1446 err = mt7915_dfs_start_rdd(dev, 1);
1450 phy->rdd_state |= BIT(1);
1457 mt7915_dfs_init_radar_specs(struct mt7915_phy *phy)
1459 const struct mt7915_dfs_radar_spec *radar_specs;
1460 struct mt7915_dev *dev = phy->dev;
1463 switch (dev->mt76.region) {
1464 case NL80211_DFS_FCC:
1465 radar_specs = &fcc_radar_specs;
1466 err = mt7915_mcu_set_fcc5_lpn(dev, 8);
1470 case NL80211_DFS_ETSI:
1471 radar_specs = &etsi_radar_specs;
1473 case NL80211_DFS_JP:
1474 radar_specs = &jp_radar_specs;
1480 for (i = 0; i < ARRAY_SIZE(radar_specs->radar_pattern); i++) {
1481 err = mt7915_mcu_set_radar_th(dev, i,
1482 &radar_specs->radar_pattern[i]);
1487 return mt7915_mcu_set_pulse_th(dev, &radar_specs->pulse_th);
1490 int mt7915_dfs_init_radar_detector(struct mt7915_phy *phy)
1492 struct cfg80211_chan_def *chandef = &phy->mt76->chandef;
1493 struct mt7915_dev *dev = phy->dev;
1494 bool ext_phy = phy != &dev->phy;
1497 if (dev->mt76.region == NL80211_DFS_UNSET) {
1498 phy->dfs_state = -1;
1505 if (test_bit(MT76_SCANNING, &phy->mt76->state))
1508 if (phy->dfs_state == chandef->chan->dfs_state)
1511 err = mt7915_dfs_init_radar_specs(phy);
1513 phy->dfs_state = -1;
1517 phy->dfs_state = chandef->chan->dfs_state;
1519 if (chandef->chan->flags & IEEE80211_CHAN_RADAR) {
1520 if (chandef->chan->dfs_state != NL80211_DFS_AVAILABLE)
1521 return mt7915_dfs_start_radar_detector(phy);
1523 return mt7915_mcu_rdd_cmd(dev, RDD_CAC_END, ext_phy,
1528 err = mt7915_mcu_rdd_cmd(dev, RDD_NORMAL_START, ext_phy,
1533 mt7915_dfs_stop_radar_detector(phy);