1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
9 #include <linux/etherdevice.h>
11 void mt7601u_set_macaddr(struct mt7601u_dev
*dev
, const u8
*addr
)
13 ether_addr_copy(dev
->macaddr
, addr
);
15 if (!is_valid_ether_addr(dev
->macaddr
)) {
16 eth_random_addr(dev
->macaddr
);
18 "Invalid MAC address, using random address %pM\n",
22 mt76_wr(dev
, MT_MAC_ADDR_DW0
, get_unaligned_le32(dev
->macaddr
));
23 mt76_wr(dev
, MT_MAC_ADDR_DW1
, get_unaligned_le16(dev
->macaddr
+ 4) |
24 FIELD_PREP(MT_MAC_ADDR_DW1_U2ME_MASK
, 0xff));
28 mt76_mac_process_tx_rate(struct ieee80211_tx_rate
*txrate
, u16 rate
)
30 u8 idx
= FIELD_GET(MT_TXWI_RATE_MCS
, rate
);
36 switch (FIELD_GET(MT_TXWI_RATE_PHY_MODE
, rate
)) {
37 case MT_PHY_TYPE_OFDM
:
38 txrate
->idx
= idx
+ 4;
46 case MT_PHY_TYPE_HT_GF
:
47 txrate
->flags
|= IEEE80211_TX_RC_GREEN_FIELD
;
50 txrate
->flags
|= IEEE80211_TX_RC_MCS
;
58 if (FIELD_GET(MT_TXWI_RATE_BW
, rate
) == MT_PHY_BW_40
)
59 txrate
->flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
61 if (rate
& MT_TXWI_RATE_SGI
)
62 txrate
->flags
|= IEEE80211_TX_RC_SHORT_GI
;
66 mt76_mac_fill_tx_status(struct mt7601u_dev
*dev
, struct ieee80211_tx_info
*info
,
67 struct mt76_tx_status
*st
)
69 struct ieee80211_tx_rate
*rate
= info
->status
.rates
;
70 int cur_idx
, last_rate
;
73 last_rate
= min_t(int, st
->retry
, IEEE80211_TX_MAX_RATES
- 1);
74 mt76_mac_process_tx_rate(&rate
[last_rate
], st
->rate
);
75 if (last_rate
< IEEE80211_TX_MAX_RATES
- 1)
76 rate
[last_rate
+ 1].idx
= -1;
78 cur_idx
= rate
[last_rate
].idx
+ st
->retry
;
79 for (i
= 0; i
<= last_rate
; i
++) {
80 rate
[i
].flags
= rate
[last_rate
].flags
;
81 rate
[i
].idx
= max_t(int, 0, cur_idx
- i
);
86 rate
[last_rate
- 1].count
= st
->retry
+ 1 - last_rate
;
88 info
->status
.ampdu_len
= 1;
89 info
->status
.ampdu_ack_len
= st
->success
;
92 info
->flags
|= IEEE80211_TX_CTL_RATE_CTRL_PROBE
;
95 info
->flags
|= IEEE80211_TX_CTL_AMPDU
|
96 IEEE80211_TX_STAT_AMPDU
;
99 info
->flags
|= IEEE80211_TX_CTL_NO_ACK
;
100 else if (st
->success
)
101 info
->flags
|= IEEE80211_TX_STAT_ACK
;
104 u16
mt76_mac_tx_rate_val(struct mt7601u_dev
*dev
,
105 const struct ieee80211_tx_rate
*rate
, u8
*nss_val
)
112 if (rate
->flags
& IEEE80211_TX_RC_MCS
) {
113 rate_idx
= rate
->idx
;
114 nss
= 1 + (rate
->idx
>> 3);
115 phy
= MT_PHY_TYPE_HT
;
116 if (rate
->flags
& IEEE80211_TX_RC_GREEN_FIELD
)
117 phy
= MT_PHY_TYPE_HT_GF
;
118 if (rate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
121 const struct ieee80211_rate
*r
;
122 int band
= dev
->chandef
.chan
->band
;
125 r
= &dev
->hw
->wiphy
->bands
[band
]->bitrates
[rate
->idx
];
126 if (rate
->flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
127 val
= r
->hw_value_short
;
132 rate_idx
= val
& 0xff;
136 rateval
= FIELD_PREP(MT_RXWI_RATE_MCS
, rate_idx
);
137 rateval
|= FIELD_PREP(MT_RXWI_RATE_PHY
, phy
);
138 rateval
|= FIELD_PREP(MT_RXWI_RATE_BW
, bw
);
139 if (rate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
140 rateval
|= MT_RXWI_RATE_SGI
;
146 void mt76_mac_wcid_set_rate(struct mt7601u_dev
*dev
, struct mt76_wcid
*wcid
,
147 const struct ieee80211_tx_rate
*rate
)
151 spin_lock_irqsave(&dev
->lock
, flags
);
152 wcid
->tx_rate
= mt76_mac_tx_rate_val(dev
, rate
, &wcid
->tx_rate_nss
);
153 wcid
->tx_rate_set
= true;
154 spin_unlock_irqrestore(&dev
->lock
, flags
);
157 struct mt76_tx_status
mt7601u_mac_fetch_tx_status(struct mt7601u_dev
*dev
)
159 struct mt76_tx_status stat
= {};
162 val
= mt7601u_rr(dev
, MT_TX_STAT_FIFO
);
163 stat
.valid
= !!(val
& MT_TX_STAT_FIFO_VALID
);
164 stat
.success
= !!(val
& MT_TX_STAT_FIFO_SUCCESS
);
165 stat
.aggr
= !!(val
& MT_TX_STAT_FIFO_AGGR
);
166 stat
.ack_req
= !!(val
& MT_TX_STAT_FIFO_ACKREQ
);
167 stat
.pktid
= FIELD_GET(MT_TX_STAT_FIFO_PID_TYPE
, val
);
168 stat
.wcid
= FIELD_GET(MT_TX_STAT_FIFO_WCID
, val
);
169 stat
.rate
= FIELD_GET(MT_TX_STAT_FIFO_RATE
, val
);
174 void mt76_send_tx_status(struct mt7601u_dev
*dev
, struct mt76_tx_status
*stat
)
176 struct ieee80211_tx_info info
= {};
177 struct ieee80211_sta
*sta
= NULL
;
178 struct mt76_wcid
*wcid
= NULL
;
182 if (stat
->wcid
< ARRAY_SIZE(dev
->wcid
))
183 wcid
= rcu_dereference(dev
->wcid
[stat
->wcid
]);
186 msta
= container_of(wcid
, struct mt76_sta
, wcid
);
187 sta
= container_of(msta
, struct ieee80211_sta
,
191 mt76_mac_fill_tx_status(dev
, &info
, stat
);
193 spin_lock_bh(&dev
->mac_lock
);
194 ieee80211_tx_status_noskb(dev
->hw
, sta
, &info
);
195 spin_unlock_bh(&dev
->mac_lock
);
200 void mt7601u_mac_set_protection(struct mt7601u_dev
*dev
, bool legacy_prot
,
203 int mode
= ht_mode
& IEEE80211_HT_OP_MODE_PROTECTION
;
204 bool non_gf
= !!(ht_mode
& IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT
);
209 prot
[0] = MT_PROT_NAV_SHORT
|
210 MT_PROT_TXOP_ALLOW_ALL
|
214 prot
[1] |= MT_PROT_CTRL_CTS2SELF
;
216 prot
[2] = prot
[4] = MT_PROT_NAV_SHORT
| MT_PROT_TXOP_ALLOW_BW20
;
217 prot
[3] = prot
[5] = MT_PROT_NAV_SHORT
| MT_PROT_TXOP_ALLOW_ALL
;
220 prot
[2] |= MT_PROT_RATE_CCK_11
;
221 prot
[3] |= MT_PROT_RATE_CCK_11
;
222 prot
[4] |= MT_PROT_RATE_CCK_11
;
223 prot
[5] |= MT_PROT_RATE_CCK_11
;
225 prot
[2] |= MT_PROT_RATE_OFDM_24
;
226 prot
[3] |= MT_PROT_RATE_DUP_OFDM_24
;
227 prot
[4] |= MT_PROT_RATE_OFDM_24
;
228 prot
[5] |= MT_PROT_RATE_DUP_OFDM_24
;
232 case IEEE80211_HT_OP_MODE_PROTECTION_NONE
:
235 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER
:
236 ht_rts
[0] = ht_rts
[1] = ht_rts
[2] = ht_rts
[3] = true;
239 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ
:
240 ht_rts
[1] = ht_rts
[3] = true;
243 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED
:
244 ht_rts
[0] = ht_rts
[1] = ht_rts
[2] = ht_rts
[3] = true;
249 ht_rts
[2] = ht_rts
[3] = true;
251 for (i
= 0; i
< 4; i
++)
253 prot
[i
+ 2] |= MT_PROT_CTRL_RTS_CTS
;
255 for (i
= 0; i
< 6; i
++)
256 mt7601u_wr(dev
, MT_CCK_PROT_CFG
+ i
* 4, prot
[i
]);
259 void mt7601u_mac_set_short_preamble(struct mt7601u_dev
*dev
, bool short_preamb
)
262 mt76_set(dev
, MT_AUTO_RSP_CFG
, MT_AUTO_RSP_PREAMB_SHORT
);
264 mt76_clear(dev
, MT_AUTO_RSP_CFG
, MT_AUTO_RSP_PREAMB_SHORT
);
267 void mt7601u_mac_config_tsf(struct mt7601u_dev
*dev
, bool enable
, int interval
)
269 u32 val
= mt7601u_rr(dev
, MT_BEACON_TIME_CFG
);
271 val
&= ~(MT_BEACON_TIME_CFG_TIMER_EN
|
272 MT_BEACON_TIME_CFG_SYNC_MODE
|
273 MT_BEACON_TIME_CFG_TBTT_EN
);
276 mt7601u_wr(dev
, MT_BEACON_TIME_CFG
, val
);
280 val
&= ~MT_BEACON_TIME_CFG_INTVAL
;
281 val
|= FIELD_PREP(MT_BEACON_TIME_CFG_INTVAL
, interval
<< 4) |
282 MT_BEACON_TIME_CFG_TIMER_EN
|
283 MT_BEACON_TIME_CFG_SYNC_MODE
|
284 MT_BEACON_TIME_CFG_TBTT_EN
;
287 static void mt7601u_check_mac_err(struct mt7601u_dev
*dev
)
289 u32 val
= mt7601u_rr(dev
, 0x10f4);
291 if (!(val
& BIT(29)) || !(val
& (BIT(7) | BIT(5))))
294 dev_err(dev
->dev
, "Error: MAC specific condition occurred\n");
296 mt76_set(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_RESET_CSR
);
298 mt76_clear(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_RESET_CSR
);
301 void mt7601u_mac_work(struct work_struct
*work
)
303 struct mt7601u_dev
*dev
= container_of(work
, struct mt7601u_dev
,
310 { MT_RX_STA_CNT0
, 3, dev
->stats
.rx_stat
},
311 { MT_TX_STA_CNT0
, 3, dev
->stats
.tx_stat
},
312 { MT_TX_AGG_STAT
, 1, dev
->stats
.aggr_stat
},
313 { MT_MPDU_DENSITY_CNT
, 1, dev
->stats
.zero_len_del
},
314 { MT_TX_AGG_CNT_BASE0
, 8, &dev
->stats
.aggr_n
[0] },
315 { MT_TX_AGG_CNT_BASE1
, 8, &dev
->stats
.aggr_n
[16] },
320 /* Note: using MCU_RANDOM_READ is actually slower then reading all the
321 * registers by hand. MCU takes ca. 20ms to complete read of 24
322 * registers while reading them one by one will takes roughly
329 for (i
= 0; i
< ARRAY_SIZE(spans
); i
++)
330 for (j
= 0; j
< spans
[i
].span
; j
++) {
331 u32 val
= mt7601u_rr(dev
, spans
[i
].addr_base
+ j
* 4);
333 spans
[i
].stat_base
[j
* 2] += val
& 0xffff;
334 spans
[i
].stat_base
[j
* 2 + 1] += val
>> 16;
336 /* Calculate average AMPDU length */
337 if (spans
[i
].addr_base
!= MT_TX_AGG_CNT_BASE0
&&
338 spans
[i
].addr_base
!= MT_TX_AGG_CNT_BASE1
)
341 n
+= (val
>> 16) + (val
& 0xffff);
342 sum
+= (val
& 0xffff) * (1 + k
* 2) +
343 (val
>> 16) * (2 + k
* 2);
347 atomic_set(&dev
->avg_ampdu_len
, n
? DIV_ROUND_CLOSEST(sum
, n
) : 1);
349 mt7601u_check_mac_err(dev
);
351 ieee80211_queue_delayed_work(dev
->hw
, &dev
->mac_work
, 10 * HZ
);
355 mt7601u_mac_wcid_setup(struct mt7601u_dev
*dev
, u8 idx
, u8 vif_idx
, u8
*mac
)
357 u8 zmac
[ETH_ALEN
] = {};
360 attr
= FIELD_PREP(MT_WCID_ATTR_BSS_IDX
, vif_idx
& 7) |
361 FIELD_PREP(MT_WCID_ATTR_BSS_IDX_EXT
, !!(vif_idx
& 8));
363 mt76_wr(dev
, MT_WCID_ATTR(idx
), attr
);
366 memcpy(zmac
, mac
, sizeof(zmac
));
368 mt7601u_addr_wr(dev
, MT_WCID_ADDR(idx
), zmac
);
371 void mt7601u_mac_set_ampdu_factor(struct mt7601u_dev
*dev
)
373 struct ieee80211_sta
*sta
;
374 struct mt76_wcid
*wcid
;
380 for (i
= 0; i
< ARRAY_SIZE(dev
->wcid
); i
++) {
381 wcid
= rcu_dereference(dev
->wcid
[i
]);
385 msta
= container_of(wcid
, struct mt76_sta
, wcid
);
386 sta
= container_of(msta
, struct ieee80211_sta
, drv_priv
);
388 min_factor
= min(min_factor
, sta
->ht_cap
.ampdu_factor
);
392 mt7601u_wr(dev
, MT_MAX_LEN_CFG
, 0xa0fff |
393 FIELD_PREP(MT_MAX_LEN_CFG_AMPDU
, min_factor
));
397 mt76_mac_process_rate(struct ieee80211_rx_status
*status
, u16 rate
)
399 u8 idx
= FIELD_GET(MT_RXWI_RATE_MCS
, rate
);
401 switch (FIELD_GET(MT_RXWI_RATE_PHY
, rate
)) {
402 case MT_PHY_TYPE_OFDM
:
403 if (WARN_ON(idx
>= 8))
407 status
->rate_idx
= idx
;
409 case MT_PHY_TYPE_CCK
:
412 status
->enc_flags
|= RX_ENC_FLAG_SHORTPRE
;
415 if (WARN_ON(idx
>= 4))
418 status
->rate_idx
= idx
;
420 case MT_PHY_TYPE_HT_GF
:
421 status
->enc_flags
|= RX_ENC_FLAG_HT_GF
;
424 status
->encoding
= RX_ENC_HT
;
425 status
->rate_idx
= idx
;
432 if (rate
& MT_RXWI_RATE_SGI
)
433 status
->enc_flags
|= RX_ENC_FLAG_SHORT_GI
;
435 if (rate
& MT_RXWI_RATE_STBC
)
436 status
->enc_flags
|= 1 << RX_ENC_FLAG_STBC_SHIFT
;
438 if (rate
& MT_RXWI_RATE_BW
)
439 status
->bw
= RATE_INFO_BW_40
;
443 mt7601u_rx_monitor_beacon(struct mt7601u_dev
*dev
, struct mt7601u_rxwi
*rxwi
,
446 dev
->bcn_freq_off
= rxwi
->freq_off
;
447 dev
->bcn_phy_mode
= FIELD_GET(MT_RXWI_RATE_PHY
, rate
);
448 ewma_rssi_add(&dev
->avg_rssi
, -rssi
);
452 mt7601u_rx_is_our_beacon(struct mt7601u_dev
*dev
, u8
*data
)
454 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)data
;
456 return ieee80211_is_beacon(hdr
->frame_control
) &&
457 ether_addr_equal(hdr
->addr2
, dev
->ap_bssid
);
460 u32
mt76_mac_process_rx(struct mt7601u_dev
*dev
, struct sk_buff
*skb
,
463 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
464 struct mt7601u_rxwi
*rxwi
= rxi
;
465 u32 len
, ctl
= le32_to_cpu(rxwi
->ctl
);
466 u16 rate
= le16_to_cpu(rxwi
->rate
);
469 len
= FIELD_GET(MT_RXWI_CTL_MPDU_LEN
, ctl
);
473 if (rxwi
->rxinfo
& cpu_to_le32(MT_RXINFO_DECRYPT
)) {
474 status
->flag
|= RX_FLAG_DECRYPTED
;
475 status
->flag
|= RX_FLAG_MMIC_STRIPPED
;
476 status
->flag
|= RX_FLAG_MIC_STRIPPED
;
477 status
->flag
|= RX_FLAG_ICV_STRIPPED
;
478 status
->flag
|= RX_FLAG_IV_STRIPPED
;
480 /* let mac80211 take care of PN validation since apparently
481 * the hardware does not support it
483 if (rxwi
->rxinfo
& cpu_to_le32(MT_RXINFO_PN_LEN
))
484 status
->flag
&= ~RX_FLAG_IV_STRIPPED
;
486 status
->chains
= BIT(0);
487 rssi
= mt7601u_phy_get_rssi(dev
, rxwi
, rate
);
488 status
->chain_signal
[0] = status
->signal
= rssi
;
489 status
->freq
= dev
->chandef
.chan
->center_freq
;
490 status
->band
= dev
->chandef
.chan
->band
;
492 mt76_mac_process_rate(status
, rate
);
494 spin_lock_bh(&dev
->con_mon_lock
);
495 if (mt7601u_rx_is_our_beacon(dev
, data
))
496 mt7601u_rx_monitor_beacon(dev
, rxwi
, rate
, rssi
);
497 else if (rxwi
->rxinfo
& cpu_to_le32(MT_RXINFO_U2M
))
498 ewma_rssi_add(&dev
->avg_rssi
, -rssi
);
499 spin_unlock_bh(&dev
->con_mon_lock
);
504 static enum mt76_cipher_type
505 mt76_mac_get_key_info(struct ieee80211_key_conf
*key
, u8
*key_data
)
507 memset(key_data
, 0, 32);
509 return MT_CIPHER_NONE
;
511 if (key
->keylen
> 32)
512 return MT_CIPHER_NONE
;
514 memcpy(key_data
, key
->key
, key
->keylen
);
516 switch (key
->cipher
) {
517 case WLAN_CIPHER_SUITE_WEP40
:
518 return MT_CIPHER_WEP40
;
519 case WLAN_CIPHER_SUITE_WEP104
:
520 return MT_CIPHER_WEP104
;
521 case WLAN_CIPHER_SUITE_TKIP
:
522 return MT_CIPHER_TKIP
;
523 case WLAN_CIPHER_SUITE_CCMP
:
524 return MT_CIPHER_AES_CCMP
;
526 return MT_CIPHER_NONE
;
530 int mt76_mac_wcid_set_key(struct mt7601u_dev
*dev
, u8 idx
,
531 struct ieee80211_key_conf
*key
)
533 enum mt76_cipher_type cipher
;
538 cipher
= mt76_mac_get_key_info(key
, key_data
);
539 if (cipher
== MT_CIPHER_NONE
&& key
)
542 trace_set_key(dev
, idx
);
544 mt7601u_wr_copy(dev
, MT_WCID_KEY(idx
), key_data
, sizeof(key_data
));
546 memset(iv_data
, 0, sizeof(iv_data
));
548 iv_data
[3] = key
->keyidx
<< 6;
549 if (cipher
>= MT_CIPHER_TKIP
) {
550 /* Note: start with 1 to comply with spec,
551 * (see comment on common/cmm_wpa.c:4291).
557 mt7601u_wr_copy(dev
, MT_WCID_IV(idx
), iv_data
, sizeof(iv_data
));
559 val
= mt7601u_rr(dev
, MT_WCID_ATTR(idx
));
560 val
&= ~MT_WCID_ATTR_PKEY_MODE
& ~MT_WCID_ATTR_PKEY_MODE_EXT
;
561 val
|= FIELD_PREP(MT_WCID_ATTR_PKEY_MODE
, cipher
& 7) |
562 FIELD_PREP(MT_WCID_ATTR_PKEY_MODE_EXT
, cipher
>> 3);
563 val
&= ~MT_WCID_ATTR_PAIRWISE
;
564 val
|= MT_WCID_ATTR_PAIRWISE
*
565 !!(key
&& key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
);
566 mt7601u_wr(dev
, MT_WCID_ATTR(idx
), val
);
571 int mt76_mac_shared_key_setup(struct mt7601u_dev
*dev
, u8 vif_idx
, u8 key_idx
,
572 struct ieee80211_key_conf
*key
)
574 enum mt76_cipher_type cipher
;
578 cipher
= mt76_mac_get_key_info(key
, key_data
);
579 if (cipher
== MT_CIPHER_NONE
&& key
)
582 trace_set_shared_key(dev
, vif_idx
, key_idx
);
584 mt7601u_wr_copy(dev
, MT_SKEY(vif_idx
, key_idx
),
585 key_data
, sizeof(key_data
));
587 val
= mt76_rr(dev
, MT_SKEY_MODE(vif_idx
));
588 val
&= ~(MT_SKEY_MODE_MASK
<< MT_SKEY_MODE_SHIFT(vif_idx
, key_idx
));
589 val
|= cipher
<< MT_SKEY_MODE_SHIFT(vif_idx
, key_idx
);
590 mt76_wr(dev
, MT_SKEY_MODE(vif_idx
), val
);