1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #include <linux/etherdevice.h>
5 #include <linux/platform_device.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 #include "mt7915.h"
9 #include "mcu.h"
10
mt7915_dev_running(struct mt7915_dev * dev)11 static bool mt7915_dev_running(struct mt7915_dev *dev)
12 {
13 struct mt7915_phy *phy;
14
15 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
16 return true;
17
18 phy = mt7915_ext_phy(dev);
19
20 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
21 }
22
mt7915_start(struct ieee80211_hw * hw)23 static int mt7915_start(struct ieee80211_hw *hw)
24 {
25 struct mt7915_dev *dev = mt7915_hw_dev(hw);
26 struct mt7915_phy *phy = mt7915_hw_phy(hw);
27 bool running;
28 int ret;
29
30 flush_work(&dev->init_work);
31
32 mutex_lock(&dev->mt76.mutex);
33
34 running = mt7915_dev_running(dev);
35
36 if (!running) {
37 ret = mt7915_mcu_set_pm(dev, 0, 0);
38 if (ret)
39 goto out;
40
41 ret = mt7915_mcu_set_mac(dev, 0, true, true);
42 if (ret)
43 goto out;
44
45 ret = mt7915_mcu_set_scs(dev, 0, true);
46 if (ret)
47 goto out;
48
49 mt7915_mac_enable_nf(dev, 0);
50 }
51
52 if (phy != &dev->phy) {
53 ret = mt7915_mcu_set_pm(dev, 1, 0);
54 if (ret)
55 goto out;
56
57 ret = mt7915_mcu_set_mac(dev, 1, true, true);
58 if (ret)
59 goto out;
60
61 ret = mt7915_mcu_set_scs(dev, 1, true);
62 if (ret)
63 goto out;
64
65 mt7915_mac_enable_nf(dev, 1);
66 }
67
68 ret = mt7915_mcu_set_rts_thresh(phy, 0x92b);
69 if (ret)
70 goto out;
71
72 ret = mt7915_mcu_set_sku_en(phy, true);
73 if (ret)
74 goto out;
75
76 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
77 if (ret)
78 goto out;
79
80 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
81
82 if (!mt76_testmode_enabled(phy->mt76))
83 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work,
84 MT7915_WATCHDOG_TIME);
85
86 if (!running)
87 mt7915_mac_reset_counters(phy);
88
89 out:
90 mutex_unlock(&dev->mt76.mutex);
91
92 return ret;
93 }
94
mt7915_stop(struct ieee80211_hw * hw)95 static void mt7915_stop(struct ieee80211_hw *hw)
96 {
97 struct mt7915_dev *dev = mt7915_hw_dev(hw);
98 struct mt7915_phy *phy = mt7915_hw_phy(hw);
99
100 cancel_delayed_work_sync(&phy->mt76->mac_work);
101
102 mutex_lock(&dev->mt76.mutex);
103
104 mt76_testmode_reset(phy->mt76, true);
105
106 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
107
108 if (phy != &dev->phy) {
109 mt7915_mcu_set_pm(dev, 1, 1);
110 mt7915_mcu_set_mac(dev, 1, false, false);
111 }
112
113 if (!mt7915_dev_running(dev)) {
114 mt7915_mcu_set_pm(dev, 0, 1);
115 mt7915_mcu_set_mac(dev, 0, false, false);
116 }
117
118 mutex_unlock(&dev->mt76.mutex);
119 }
120
get_free_idx(u32 mask,u8 start,u8 end)121 static inline int get_free_idx(u32 mask, u8 start, u8 end)
122 {
123 return ffs(~mask & GENMASK(end, start));
124 }
125
get_omac_idx(enum nl80211_iftype type,u64 mask)126 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
127 {
128 int i;
129
130 switch (type) {
131 case NL80211_IFTYPE_MESH_POINT:
132 case NL80211_IFTYPE_ADHOC:
133 case NL80211_IFTYPE_STATION:
134 /* prefer hw bssid slot 1-3 */
135 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
136 if (i)
137 return i - 1;
138
139 if (type != NL80211_IFTYPE_STATION)
140 break;
141
142 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
143 if (i)
144 return i - 1;
145
146 if (~mask & BIT(HW_BSSID_0))
147 return HW_BSSID_0;
148
149 break;
150 case NL80211_IFTYPE_MONITOR:
151 case NL80211_IFTYPE_AP:
152 /* ap uses hw bssid 0 and ext bssid */
153 if (~mask & BIT(HW_BSSID_0))
154 return HW_BSSID_0;
155
156 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
157 if (i)
158 return i - 1;
159
160 break;
161 default:
162 WARN_ON(1);
163 break;
164 }
165
166 return -1;
167 }
168
mt7915_init_bitrate_mask(struct ieee80211_vif * vif)169 static void mt7915_init_bitrate_mask(struct ieee80211_vif *vif)
170 {
171 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
172 int i;
173
174 for (i = 0; i < ARRAY_SIZE(mvif->bitrate_mask.control); i++) {
175 mvif->bitrate_mask.control[i].legacy = GENMASK(31, 0);
176 memset(mvif->bitrate_mask.control[i].ht_mcs, GENMASK(7, 0),
177 sizeof(mvif->bitrate_mask.control[i].ht_mcs));
178 memset(mvif->bitrate_mask.control[i].vht_mcs, GENMASK(15, 0),
179 sizeof(mvif->bitrate_mask.control[i].vht_mcs));
180 memset(mvif->bitrate_mask.control[i].he_mcs, GENMASK(15, 0),
181 sizeof(mvif->bitrate_mask.control[i].he_mcs));
182 }
183 }
184
mt7915_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)185 static int mt7915_add_interface(struct ieee80211_hw *hw,
186 struct ieee80211_vif *vif)
187 {
188 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
189 struct mt7915_dev *dev = mt7915_hw_dev(hw);
190 struct mt7915_phy *phy = mt7915_hw_phy(hw);
191 struct mt76_txq *mtxq;
192 bool ext_phy = phy != &dev->phy;
193 int idx, ret = 0;
194
195 mutex_lock(&dev->mt76.mutex);
196
197 mt76_testmode_reset(phy->mt76, true);
198
199 if (vif->type == NL80211_IFTYPE_MONITOR &&
200 is_zero_ether_addr(vif->addr))
201 phy->monitor_vif = vif;
202
203 mvif->idx = ffs(~dev->mt76.vif_mask) - 1;
204 if (mvif->idx >= MT7915_MAX_INTERFACES) {
205 ret = -ENOSPC;
206 goto out;
207 }
208
209 idx = get_omac_idx(vif->type, phy->omac_mask);
210 if (idx < 0) {
211 ret = -ENOSPC;
212 goto out;
213 }
214 mvif->omac_idx = idx;
215 mvif->phy = phy;
216 mvif->band_idx = ext_phy;
217
218 if (ext_phy)
219 mvif->wmm_idx = ext_phy * (MT7915_MAX_WMM_SETS / 2) +
220 mvif->idx % (MT7915_MAX_WMM_SETS / 2);
221 else
222 mvif->wmm_idx = mvif->idx % MT7915_MAX_WMM_SETS;
223
224 ret = mt7915_mcu_add_dev_info(phy, vif, true);
225 if (ret)
226 goto out;
227
228 dev->mt76.vif_mask |= BIT(mvif->idx);
229 phy->omac_mask |= BIT_ULL(mvif->omac_idx);
230
231 idx = MT7915_WTBL_RESERVED - mvif->idx;
232
233 INIT_LIST_HEAD(&mvif->sta.rc_list);
234 INIT_LIST_HEAD(&mvif->sta.stats_list);
235 INIT_LIST_HEAD(&mvif->sta.poll_list);
236 mvif->sta.wcid.idx = idx;
237 mvif->sta.wcid.ext_phy = mvif->band_idx;
238 mvif->sta.wcid.hw_key_idx = -1;
239 mvif->sta.wcid.tx_info |= MT_WCID_TX_INFO_SET;
240 mt7915_mac_wtbl_update(dev, idx,
241 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
242
243 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
244 if (vif->txq) {
245 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
246 mtxq->wcid = idx;
247 }
248
249 if (vif->type != NL80211_IFTYPE_AP &&
250 (!mvif->omac_idx || mvif->omac_idx > 3))
251 vif->offload_flags = 0;
252 vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
253
254 mt7915_init_bitrate_mask(vif);
255
256 out:
257 mutex_unlock(&dev->mt76.mutex);
258
259 return ret;
260 }
261
mt7915_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)262 static void mt7915_remove_interface(struct ieee80211_hw *hw,
263 struct ieee80211_vif *vif)
264 {
265 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
266 struct mt7915_sta *msta = &mvif->sta;
267 struct mt7915_dev *dev = mt7915_hw_dev(hw);
268 struct mt7915_phy *phy = mt7915_hw_phy(hw);
269 int idx = msta->wcid.idx;
270
271 mt7915_mcu_add_bss_info(phy, vif, false);
272 mt7915_mcu_add_sta(dev, vif, NULL, false);
273
274 mutex_lock(&dev->mt76.mutex);
275 mt76_testmode_reset(phy->mt76, true);
276 mutex_unlock(&dev->mt76.mutex);
277
278 if (vif == phy->monitor_vif)
279 phy->monitor_vif = NULL;
280
281 mt7915_mcu_add_dev_info(phy, vif, false);
282
283 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
284
285 mutex_lock(&dev->mt76.mutex);
286 dev->mt76.vif_mask &= ~BIT(mvif->idx);
287 phy->omac_mask &= ~BIT_ULL(mvif->omac_idx);
288 mutex_unlock(&dev->mt76.mutex);
289
290 spin_lock_bh(&dev->sta_poll_lock);
291 if (!list_empty(&msta->poll_list))
292 list_del_init(&msta->poll_list);
293 spin_unlock_bh(&dev->sta_poll_lock);
294 }
295
mt7915_init_dfs_state(struct mt7915_phy * phy)296 static void mt7915_init_dfs_state(struct mt7915_phy *phy)
297 {
298 struct mt76_phy *mphy = phy->mt76;
299 struct ieee80211_hw *hw = mphy->hw;
300 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
301
302 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
303 return;
304
305 if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
306 !(mphy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
307 return;
308
309 if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
310 mphy->chandef.width == chandef->width)
311 return;
312
313 phy->dfs_state = -1;
314 }
315
mt7915_set_channel(struct mt7915_phy * phy)316 int mt7915_set_channel(struct mt7915_phy *phy)
317 {
318 struct mt7915_dev *dev = phy->dev;
319 int ret;
320
321 cancel_delayed_work_sync(&phy->mt76->mac_work);
322
323 mutex_lock(&dev->mt76.mutex);
324 set_bit(MT76_RESET, &phy->mt76->state);
325
326 mt7915_init_dfs_state(phy);
327 mt76_set_channel(phy->mt76);
328
329 if (dev->flash_mode) {
330 ret = mt7915_mcu_apply_tx_dpd(phy);
331 if (ret)
332 goto out;
333 }
334
335 ret = mt7915_mcu_set_chan_info(phy, MCU_EXT_CMD(CHANNEL_SWITCH));
336 if (ret)
337 goto out;
338
339 mt7915_mac_set_timing(phy);
340 ret = mt7915_dfs_init_radar_detector(phy);
341 mt7915_mac_cca_stats_reset(phy);
342
343 mt7915_mac_reset_counters(phy);
344 phy->noise = 0;
345
346 out:
347 clear_bit(MT76_RESET, &phy->mt76->state);
348 mutex_unlock(&dev->mt76.mutex);
349
350 mt76_txq_schedule_all(phy->mt76);
351
352 if (!mt76_testmode_enabled(phy->mt76))
353 ieee80211_queue_delayed_work(phy->mt76->hw,
354 &phy->mt76->mac_work,
355 MT7915_WATCHDOG_TIME);
356
357 return ret;
358 }
359
mt7915_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)360 static int mt7915_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
361 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
362 struct ieee80211_key_conf *key)
363 {
364 struct mt7915_dev *dev = mt7915_hw_dev(hw);
365 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
366 struct mt7915_sta *msta = sta ? (struct mt7915_sta *)sta->drv_priv :
367 &mvif->sta;
368 struct mt76_wcid *wcid = &msta->wcid;
369 u8 *wcid_keyidx = &wcid->hw_key_idx;
370 int idx = key->keyidx;
371 int err = 0;
372
373 /* The hardware does not support per-STA RX GTK, fallback
374 * to software mode for these.
375 */
376 if ((vif->type == NL80211_IFTYPE_ADHOC ||
377 vif->type == NL80211_IFTYPE_MESH_POINT) &&
378 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
379 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
380 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
381 return -EOPNOTSUPP;
382
383 /* fall back to sw encryption for unsupported ciphers */
384 switch (key->cipher) {
385 case WLAN_CIPHER_SUITE_AES_CMAC:
386 wcid_keyidx = &wcid->hw_key_idx2;
387 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
388 break;
389 case WLAN_CIPHER_SUITE_TKIP:
390 case WLAN_CIPHER_SUITE_CCMP:
391 case WLAN_CIPHER_SUITE_CCMP_256:
392 case WLAN_CIPHER_SUITE_GCMP:
393 case WLAN_CIPHER_SUITE_GCMP_256:
394 case WLAN_CIPHER_SUITE_SMS4:
395 break;
396 case WLAN_CIPHER_SUITE_WEP40:
397 case WLAN_CIPHER_SUITE_WEP104:
398 default:
399 return -EOPNOTSUPP;
400 }
401
402 mutex_lock(&dev->mt76.mutex);
403
404 if (cmd == SET_KEY)
405 *wcid_keyidx = idx;
406 else if (idx == *wcid_keyidx)
407 *wcid_keyidx = -1;
408 else
409 goto out;
410
411 mt76_wcid_key_setup(&dev->mt76, wcid,
412 cmd == SET_KEY ? key : NULL);
413
414 err = mt7915_mcu_add_key(dev, vif, msta, key, cmd);
415
416 out:
417 mutex_unlock(&dev->mt76.mutex);
418
419 return err;
420 }
421
mt7915_config(struct ieee80211_hw * hw,u32 changed)422 static int mt7915_config(struct ieee80211_hw *hw, u32 changed)
423 {
424 struct mt7915_dev *dev = mt7915_hw_dev(hw);
425 struct mt7915_phy *phy = mt7915_hw_phy(hw);
426 bool band = phy != &dev->phy;
427 int ret;
428
429 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
430 #ifdef CONFIG_NL80211_TESTMODE
431 if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
432 mutex_lock(&dev->mt76.mutex);
433 mt76_testmode_reset(phy->mt76, false);
434 mutex_unlock(&dev->mt76.mutex);
435 }
436 #endif
437 ieee80211_stop_queues(hw);
438 ret = mt7915_set_channel(phy);
439 if (ret)
440 return ret;
441 ieee80211_wake_queues(hw);
442 }
443
444 if (changed & (IEEE80211_CONF_CHANGE_POWER |
445 IEEE80211_CONF_CHANGE_CHANNEL)) {
446 ret = mt7915_mcu_set_txpower_sku(phy);
447 if (ret)
448 return ret;
449 }
450
451 mutex_lock(&dev->mt76.mutex);
452
453 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
454 bool enabled = !!(hw->conf.flags & IEEE80211_CONF_MONITOR);
455
456 if (!enabled)
457 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
458 else
459 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
460
461 mt76_rmw_field(dev, MT_DMA_DCR0(band), MT_DMA_DCR0_RXD_G5_EN,
462 enabled);
463 mt76_testmode_reset(phy->mt76, true);
464 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
465 }
466
467 mutex_unlock(&dev->mt76.mutex);
468
469 return 0;
470 }
471
472 static int
mt7915_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)473 mt7915_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
474 const struct ieee80211_tx_queue_params *params)
475 {
476 struct mt7915_dev *dev = mt7915_hw_dev(hw);
477 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
478
479 /* no need to update right away, we'll get BSS_CHANGED_QOS */
480 queue = mt7915_lmac_mapping(dev, queue);
481 mvif->queue_params[queue] = *params;
482
483 return 0;
484 }
485
mt7915_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)486 static void mt7915_configure_filter(struct ieee80211_hw *hw,
487 unsigned int changed_flags,
488 unsigned int *total_flags,
489 u64 multicast)
490 {
491 struct mt7915_dev *dev = mt7915_hw_dev(hw);
492 struct mt7915_phy *phy = mt7915_hw_phy(hw);
493 bool band = phy != &dev->phy;
494 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
495 MT_WF_RFCR1_DROP_BF_POLL |
496 MT_WF_RFCR1_DROP_BA |
497 MT_WF_RFCR1_DROP_CFEND |
498 MT_WF_RFCR1_DROP_CFACK;
499 u32 flags = 0;
500
501 #define MT76_FILTER(_flag, _hw) do { \
502 flags |= *total_flags & FIF_##_flag; \
503 phy->rxfilter &= ~(_hw); \
504 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
505 } while (0)
506
507 mutex_lock(&dev->mt76.mutex);
508
509 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
510 MT_WF_RFCR_DROP_OTHER_BEACON |
511 MT_WF_RFCR_DROP_FRAME_REPORT |
512 MT_WF_RFCR_DROP_PROBEREQ |
513 MT_WF_RFCR_DROP_MCAST_FILTERED |
514 MT_WF_RFCR_DROP_MCAST |
515 MT_WF_RFCR_DROP_BCAST |
516 MT_WF_RFCR_DROP_DUPLICATE |
517 MT_WF_RFCR_DROP_A2_BSSID |
518 MT_WF_RFCR_DROP_UNWANTED_CTL |
519 MT_WF_RFCR_DROP_STBC_MULTI);
520
521 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
522 MT_WF_RFCR_DROP_A3_MAC |
523 MT_WF_RFCR_DROP_A3_BSSID);
524
525 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
526
527 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
528 MT_WF_RFCR_DROP_RTS |
529 MT_WF_RFCR_DROP_CTL_RSV |
530 MT_WF_RFCR_DROP_NDPA);
531
532 *total_flags = flags;
533 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
534
535 if (*total_flags & FIF_CONTROL)
536 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
537 else
538 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
539
540 mutex_unlock(&dev->mt76.mutex);
541 }
542
mt7915_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)543 static void mt7915_bss_info_changed(struct ieee80211_hw *hw,
544 struct ieee80211_vif *vif,
545 struct ieee80211_bss_conf *info,
546 u32 changed)
547 {
548 struct mt7915_phy *phy = mt7915_hw_phy(hw);
549 struct mt7915_dev *dev = mt7915_hw_dev(hw);
550
551 mutex_lock(&dev->mt76.mutex);
552
553 /*
554 * station mode uses BSSID to map the wlan entry to a peer,
555 * and then peer references bss_info_rfch to set bandwidth cap.
556 */
557 if (changed & BSS_CHANGED_BSSID &&
558 vif->type == NL80211_IFTYPE_STATION) {
559 bool join = !is_zero_ether_addr(info->bssid);
560
561 mt7915_mcu_add_bss_info(phy, vif, join);
562 mt7915_mcu_add_sta(dev, vif, NULL, join);
563 }
564
565 if (changed & BSS_CHANGED_ASSOC) {
566 mt7915_mcu_add_bss_info(phy, vif, info->assoc);
567 mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
568 }
569
570 if (changed & BSS_CHANGED_ERP_SLOT) {
571 int slottime = info->use_short_slot ? 9 : 20;
572
573 if (slottime != phy->slottime) {
574 phy->slottime = slottime;
575 mt7915_mac_set_timing(phy);
576 }
577 }
578
579 if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
580 mt7915_mcu_add_bss_info(phy, vif, true);
581 mt7915_mcu_add_sta(dev, vif, NULL, true);
582 }
583
584 /* ensure that enable txcmd_mode after bss_info */
585 if (changed & (BSS_CHANGED_QOS | BSS_CHANGED_BEACON_ENABLED))
586 mt7915_mcu_set_tx(dev, vif);
587
588 if (changed & BSS_CHANGED_HE_OBSS_PD)
589 mt7915_mcu_add_obss_spr(dev, vif, info->he_obss_pd.enable);
590
591 if (changed & (BSS_CHANGED_BEACON |
592 BSS_CHANGED_BEACON_ENABLED))
593 mt7915_mcu_add_beacon(hw, vif, info->enable_beacon);
594
595 mutex_unlock(&dev->mt76.mutex);
596 }
597
598 static void
mt7915_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)599 mt7915_channel_switch_beacon(struct ieee80211_hw *hw,
600 struct ieee80211_vif *vif,
601 struct cfg80211_chan_def *chandef)
602 {
603 struct mt7915_dev *dev = mt7915_hw_dev(hw);
604
605 mutex_lock(&dev->mt76.mutex);
606 mt7915_mcu_add_beacon(hw, vif, true);
607 mutex_unlock(&dev->mt76.mutex);
608 }
609
mt7915_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)610 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
611 struct ieee80211_sta *sta)
612 {
613 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
614 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
615 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
616 int ret, idx;
617
618 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7915_WTBL_STA - 1);
619 if (idx < 0)
620 return -ENOSPC;
621
622 INIT_LIST_HEAD(&msta->rc_list);
623 INIT_LIST_HEAD(&msta->stats_list);
624 INIT_LIST_HEAD(&msta->poll_list);
625 msta->vif = mvif;
626 msta->wcid.sta = 1;
627 msta->wcid.idx = idx;
628 msta->wcid.ext_phy = mvif->band_idx;
629 msta->wcid.tx_info |= MT_WCID_TX_INFO_SET;
630 msta->stats.jiffies = jiffies;
631
632 mt7915_mac_wtbl_update(dev, idx,
633 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
634
635 ret = mt7915_mcu_add_sta(dev, vif, sta, true);
636 if (ret)
637 return ret;
638
639 return mt7915_mcu_add_sta_adv(dev, vif, sta, true);
640 }
641
mt7915_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)642 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
643 struct ieee80211_sta *sta)
644 {
645 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
646 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
647
648 mt7915_mcu_add_sta_adv(dev, vif, sta, false);
649 mt7915_mcu_add_sta(dev, vif, sta, false);
650
651 mt7915_mac_wtbl_update(dev, msta->wcid.idx,
652 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
653
654 spin_lock_bh(&dev->sta_poll_lock);
655 if (!list_empty(&msta->poll_list))
656 list_del_init(&msta->poll_list);
657 if (!list_empty(&msta->stats_list))
658 list_del_init(&msta->stats_list);
659 if (!list_empty(&msta->rc_list))
660 list_del_init(&msta->rc_list);
661 spin_unlock_bh(&dev->sta_poll_lock);
662 }
663
mt7915_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)664 static void mt7915_tx(struct ieee80211_hw *hw,
665 struct ieee80211_tx_control *control,
666 struct sk_buff *skb)
667 {
668 struct mt7915_dev *dev = mt7915_hw_dev(hw);
669 struct mt76_phy *mphy = hw->priv;
670 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
671 struct ieee80211_vif *vif = info->control.vif;
672 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
673
674 if (control->sta) {
675 struct mt7915_sta *sta;
676
677 sta = (struct mt7915_sta *)control->sta->drv_priv;
678 wcid = &sta->wcid;
679 }
680
681 if (vif && !control->sta) {
682 struct mt7915_vif *mvif;
683
684 mvif = (struct mt7915_vif *)vif->drv_priv;
685 wcid = &mvif->sta.wcid;
686 }
687
688 mt76_tx(mphy, control->sta, wcid, skb);
689 }
690
mt7915_set_rts_threshold(struct ieee80211_hw * hw,u32 val)691 static int mt7915_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
692 {
693 struct mt7915_dev *dev = mt7915_hw_dev(hw);
694 struct mt7915_phy *phy = mt7915_hw_phy(hw);
695 int ret;
696
697 mutex_lock(&dev->mt76.mutex);
698 ret = mt7915_mcu_set_rts_thresh(phy, val);
699 mutex_unlock(&dev->mt76.mutex);
700
701 return ret;
702 }
703
704 static int
mt7915_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)705 mt7915_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
706 struct ieee80211_ampdu_params *params)
707 {
708 enum ieee80211_ampdu_mlme_action action = params->action;
709 struct mt7915_dev *dev = mt7915_hw_dev(hw);
710 struct ieee80211_sta *sta = params->sta;
711 struct ieee80211_txq *txq = sta->txq[params->tid];
712 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
713 u16 tid = params->tid;
714 u16 ssn = params->ssn;
715 struct mt76_txq *mtxq;
716 int ret = 0;
717
718 if (!txq)
719 return -EINVAL;
720
721 mtxq = (struct mt76_txq *)txq->drv_priv;
722
723 mutex_lock(&dev->mt76.mutex);
724 switch (action) {
725 case IEEE80211_AMPDU_RX_START:
726 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
727 params->buf_size);
728 ret = mt7915_mcu_add_rx_ba(dev, params, true);
729 break;
730 case IEEE80211_AMPDU_RX_STOP:
731 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
732 ret = mt7915_mcu_add_rx_ba(dev, params, false);
733 break;
734 case IEEE80211_AMPDU_TX_OPERATIONAL:
735 mtxq->aggr = true;
736 mtxq->send_bar = false;
737 ret = mt7915_mcu_add_tx_ba(dev, params, true);
738 break;
739 case IEEE80211_AMPDU_TX_STOP_FLUSH:
740 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
741 mtxq->aggr = false;
742 clear_bit(tid, &msta->ampdu_state);
743 ret = mt7915_mcu_add_tx_ba(dev, params, false);
744 break;
745 case IEEE80211_AMPDU_TX_START:
746 set_bit(tid, &msta->ampdu_state);
747 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
748 break;
749 case IEEE80211_AMPDU_TX_STOP_CONT:
750 mtxq->aggr = false;
751 clear_bit(tid, &msta->ampdu_state);
752 ret = mt7915_mcu_add_tx_ba(dev, params, false);
753 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
754 break;
755 }
756 mutex_unlock(&dev->mt76.mutex);
757
758 return ret;
759 }
760
761 static int
mt7915_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)762 mt7915_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
763 struct ieee80211_sta *sta)
764 {
765 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
766 IEEE80211_STA_NONE);
767 }
768
769 static int
mt7915_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)770 mt7915_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
771 struct ieee80211_sta *sta)
772 {
773 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
774 IEEE80211_STA_NOTEXIST);
775 }
776
777 static int
mt7915_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)778 mt7915_get_stats(struct ieee80211_hw *hw,
779 struct ieee80211_low_level_stats *stats)
780 {
781 struct mt7915_phy *phy = mt7915_hw_phy(hw);
782 struct mt7915_dev *dev = mt7915_hw_dev(hw);
783 struct mib_stats *mib = &phy->mib;
784
785 mutex_lock(&dev->mt76.mutex);
786 stats->dot11RTSSuccessCount = mib->rts_cnt;
787 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
788 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
789 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
790
791 memset(mib, 0, sizeof(*mib));
792
793 mutex_unlock(&dev->mt76.mutex);
794
795 return 0;
796 }
797
798 static u64
mt7915_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)799 mt7915_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
800 {
801 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
802 struct mt7915_dev *dev = mt7915_hw_dev(hw);
803 struct mt7915_phy *phy = mt7915_hw_phy(hw);
804 bool band = phy != &dev->phy;
805 union {
806 u64 t64;
807 u32 t32[2];
808 } tsf;
809 u16 n;
810
811 mutex_lock(&dev->mt76.mutex);
812
813 n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
814 /* TSF software read */
815 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
816 MT_LPON_TCR_SW_READ);
817 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0(band));
818 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1(band));
819
820 mutex_unlock(&dev->mt76.mutex);
821
822 return tsf.t64;
823 }
824
825 static void
mt7915_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)826 mt7915_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
827 u64 timestamp)
828 {
829 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
830 struct mt7915_dev *dev = mt7915_hw_dev(hw);
831 struct mt7915_phy *phy = mt7915_hw_phy(hw);
832 bool band = phy != &dev->phy;
833 union {
834 u64 t64;
835 u32 t32[2];
836 } tsf = { .t64 = timestamp, };
837 u16 n;
838
839 mutex_lock(&dev->mt76.mutex);
840
841 n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
842 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
843 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
844 /* TSF software overwrite */
845 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
846 MT_LPON_TCR_SW_WRITE);
847
848 mutex_unlock(&dev->mt76.mutex);
849 }
850
851 static void
mt7915_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)852 mt7915_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
853 s64 timestamp)
854 {
855 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
856 struct mt7915_dev *dev = mt7915_hw_dev(hw);
857 struct mt7915_phy *phy = mt7915_hw_phy(hw);
858 bool band = phy != &dev->phy;
859 union {
860 u64 t64;
861 u32 t32[2];
862 } tsf = { .t64 = timestamp, };
863 u16 n;
864
865 mutex_lock(&dev->mt76.mutex);
866
867 n = mvif->omac_idx > HW_BSSID_MAX ? HW_BSSID_0 : mvif->omac_idx;
868 mt76_wr(dev, MT_LPON_UTTR0(band), tsf.t32[0]);
869 mt76_wr(dev, MT_LPON_UTTR1(band), tsf.t32[1]);
870 /* TSF software adjust*/
871 mt76_rmw(dev, MT_LPON_TCR(band, n), MT_LPON_TCR_SW_MODE,
872 MT_LPON_TCR_SW_ADJUST);
873
874 mutex_unlock(&dev->mt76.mutex);
875 }
876
877 static void
mt7915_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)878 mt7915_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
879 {
880 struct mt7915_phy *phy = mt7915_hw_phy(hw);
881 struct mt7915_dev *dev = phy->dev;
882
883 mutex_lock(&dev->mt76.mutex);
884 phy->coverage_class = max_t(s16, coverage_class, 0);
885 mt7915_mac_set_timing(phy);
886 mutex_unlock(&dev->mt76.mutex);
887 }
888
889 static int
mt7915_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)890 mt7915_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
891 {
892 struct mt7915_dev *dev = mt7915_hw_dev(hw);
893 struct mt7915_phy *phy = mt7915_hw_phy(hw);
894 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
895 bool ext_phy = phy != &dev->phy;
896
897 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
898 return -EINVAL;
899
900 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
901 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
902
903 mutex_lock(&dev->mt76.mutex);
904
905 phy->mt76->antenna_mask = tx_ant;
906
907 if (ext_phy) {
908 if (dev->chainmask == 0xf)
909 tx_ant <<= 2;
910 else
911 tx_ant <<= 1;
912 }
913 phy->mt76->chainmask = tx_ant;
914
915 mt76_set_stream_caps(phy->mt76, true);
916 mt7915_set_stream_vht_txbf_caps(phy);
917 mt7915_set_stream_he_caps(phy);
918
919 mutex_unlock(&dev->mt76.mutex);
920
921 return 0;
922 }
923
mt7915_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)924 static void mt7915_sta_statistics(struct ieee80211_hw *hw,
925 struct ieee80211_vif *vif,
926 struct ieee80211_sta *sta,
927 struct station_info *sinfo)
928 {
929 struct mt7915_phy *phy = mt7915_hw_phy(hw);
930 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
931 struct mt7915_sta_stats *stats = &msta->stats;
932 struct rate_info rxrate = {};
933
934 if (!mt7915_mcu_get_rx_rate(phy, vif, sta, &rxrate)) {
935 sinfo->rxrate = rxrate;
936 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
937 }
938
939 if (!stats->tx_rate.legacy && !stats->tx_rate.flags)
940 return;
941
942 if (stats->tx_rate.legacy) {
943 sinfo->txrate.legacy = stats->tx_rate.legacy;
944 } else {
945 sinfo->txrate.mcs = stats->tx_rate.mcs;
946 sinfo->txrate.nss = stats->tx_rate.nss;
947 sinfo->txrate.bw = stats->tx_rate.bw;
948 sinfo->txrate.he_gi = stats->tx_rate.he_gi;
949 sinfo->txrate.he_dcm = stats->tx_rate.he_dcm;
950 sinfo->txrate.he_ru_alloc = stats->tx_rate.he_ru_alloc;
951 }
952 sinfo->txrate.flags = stats->tx_rate.flags;
953 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
954 }
955
mt7915_sta_rc_work(void * data,struct ieee80211_sta * sta)956 static void mt7915_sta_rc_work(void *data, struct ieee80211_sta *sta)
957 {
958 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
959 struct mt7915_dev *dev = msta->vif->phy->dev;
960 struct ieee80211_hw *hw = msta->vif->phy->mt76->hw;
961 u32 *changed = data;
962
963 spin_lock_bh(&dev->sta_poll_lock);
964 msta->stats.changed |= *changed;
965 if (list_empty(&msta->rc_list))
966 list_add_tail(&msta->rc_list, &dev->sta_rc_list);
967 spin_unlock_bh(&dev->sta_poll_lock);
968
969 ieee80211_queue_work(hw, &dev->rc_work);
970 }
971
mt7915_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)972 static void mt7915_sta_rc_update(struct ieee80211_hw *hw,
973 struct ieee80211_vif *vif,
974 struct ieee80211_sta *sta,
975 u32 changed)
976 {
977 mt7915_sta_rc_work(&changed, sta);
978 }
979
980 static int
mt7915_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)981 mt7915_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
982 const struct cfg80211_bitrate_mask *mask)
983 {
984 struct mt7915_vif *mvif = (struct mt7915_vif *)vif->drv_priv;
985 enum nl80211_band band = mvif->phy->mt76->chandef.chan->band;
986 u32 changed;
987
988 if (mask->control[band].gi == NL80211_TXRATE_FORCE_LGI)
989 return -EINVAL;
990
991 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
992 mvif->bitrate_mask = *mask;
993
994 /* Update firmware rate control to add a boundary on top of table
995 * to limit the rate selection for each peer, so when set bitrates
996 * vht-mcs-5 1:9, which actually means nss = 1 mcs = 0~9. This only
997 * applies to data frames as for the other mgmt, mcast, bcast still
998 * use legacy rates as it is.
999 */
1000 ieee80211_iterate_stations_atomic(hw, mt7915_sta_rc_work, &changed);
1001
1002 return 0;
1003 }
1004
mt7915_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1005 static void mt7915_sta_set_4addr(struct ieee80211_hw *hw,
1006 struct ieee80211_vif *vif,
1007 struct ieee80211_sta *sta,
1008 bool enabled)
1009 {
1010 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1011 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1012
1013 if (enabled)
1014 set_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1015 else
1016 clear_bit(MT_WCID_FLAG_4ADDR, &msta->wcid.flags);
1017
1018 mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
1019 }
1020
mt7915_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1021 static void mt7915_sta_set_decap_offload(struct ieee80211_hw *hw,
1022 struct ieee80211_vif *vif,
1023 struct ieee80211_sta *sta,
1024 bool enabled)
1025 {
1026 struct mt7915_dev *dev = mt7915_hw_dev(hw);
1027 struct mt7915_sta *msta = (struct mt7915_sta *)sta->drv_priv;
1028
1029 if (enabled)
1030 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1031 else
1032 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1033
1034 mt7915_mcu_sta_update_hdr_trans(dev, vif, sta);
1035 }
1036
1037 const struct ieee80211_ops mt7915_ops = {
1038 .tx = mt7915_tx,
1039 .start = mt7915_start,
1040 .stop = mt7915_stop,
1041 .add_interface = mt7915_add_interface,
1042 .remove_interface = mt7915_remove_interface,
1043 .config = mt7915_config,
1044 .conf_tx = mt7915_conf_tx,
1045 .configure_filter = mt7915_configure_filter,
1046 .bss_info_changed = mt7915_bss_info_changed,
1047 .sta_add = mt7915_sta_add,
1048 .sta_remove = mt7915_sta_remove,
1049 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1050 .sta_rc_update = mt7915_sta_rc_update,
1051 .set_key = mt7915_set_key,
1052 .ampdu_action = mt7915_ampdu_action,
1053 .set_rts_threshold = mt7915_set_rts_threshold,
1054 .wake_tx_queue = mt76_wake_tx_queue,
1055 .sw_scan_start = mt76_sw_scan,
1056 .sw_scan_complete = mt76_sw_scan_complete,
1057 .release_buffered_frames = mt76_release_buffered_frames,
1058 .get_txpower = mt76_get_txpower,
1059 .channel_switch_beacon = mt7915_channel_switch_beacon,
1060 .get_stats = mt7915_get_stats,
1061 .get_tsf = mt7915_get_tsf,
1062 .set_tsf = mt7915_set_tsf,
1063 .offset_tsf = mt7915_offset_tsf,
1064 .get_survey = mt76_get_survey,
1065 .get_antenna = mt76_get_antenna,
1066 .set_antenna = mt7915_set_antenna,
1067 .set_bitrate_mask = mt7915_set_bitrate_mask,
1068 .set_coverage_class = mt7915_set_coverage_class,
1069 .sta_statistics = mt7915_sta_statistics,
1070 .sta_set_4addr = mt7915_sta_set_4addr,
1071 .sta_set_decap_offload = mt7915_sta_set_decap_offload,
1072 CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1073 CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1074 #ifdef CONFIG_MAC80211_DEBUGFS
1075 .sta_add_debugfs = mt7915_sta_add_debugfs,
1076 #endif
1077 };
1078