1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3 *
4 * Author: Roy Luo <royluo@google.com>
5 * Ryder Lee <ryder.lee@mediatek.com>
6 * Felix Fietkau <nbd@nbd.name>
7 * Lorenzo Bianconi <lorenzo@kernel.org>
8 */
9
10 #include <linux/etherdevice.h>
11 #include <linux/module.h>
12 #include "mt7615.h"
13 #include "mcu.h"
14
mt7615_dev_running(struct mt7615_dev * dev)15 static bool mt7615_dev_running(struct mt7615_dev *dev)
16 {
17 struct mt7615_phy *phy;
18
19 if (test_bit(MT76_STATE_RUNNING, &dev->mphy.state))
20 return true;
21
22 phy = mt7615_ext_phy(dev);
23
24 return phy && test_bit(MT76_STATE_RUNNING, &phy->mt76->state);
25 }
26
mt7615_start(struct ieee80211_hw * hw)27 static int mt7615_start(struct ieee80211_hw *hw)
28 {
29 struct mt7615_dev *dev = mt7615_hw_dev(hw);
30 struct mt7615_phy *phy = mt7615_hw_phy(hw);
31 unsigned long timeout;
32 bool running;
33 int ret;
34
35 if (!mt7615_wait_for_mcu_init(dev))
36 return -EIO;
37
38 mt7615_mutex_acquire(dev);
39
40 running = mt7615_dev_running(dev);
41
42 if (!running) {
43 ret = mt7615_mcu_set_pm(dev, 0, 0);
44 if (ret)
45 goto out;
46
47 ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, true, false);
48 if (ret)
49 goto out;
50
51 mt7615_mac_enable_nf(dev, 0);
52 }
53
54 if (phy != &dev->phy) {
55 ret = mt7615_mcu_set_pm(dev, 1, 0);
56 if (ret)
57 goto out;
58
59 ret = mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, true, false);
60 if (ret)
61 goto out;
62
63 mt7615_mac_enable_nf(dev, 1);
64 }
65
66 if (mt7615_firmware_offload(dev)) {
67 ret = mt76_connac_mcu_set_channel_domain(phy->mt76);
68 if (ret)
69 goto out;
70
71 ret = mt76_connac_mcu_set_rate_txpower(phy->mt76);
72 if (ret)
73 goto out;
74 }
75
76 ret = mt7615_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 timeout = mt7615_get_macwork_timeout(dev);
83 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
84
85 if (!running)
86 mt7615_mac_reset_counters(dev);
87
88 out:
89 mt7615_mutex_release(dev);
90
91 return ret;
92 }
93
mt7615_stop(struct ieee80211_hw * hw)94 static void mt7615_stop(struct ieee80211_hw *hw)
95 {
96 struct mt7615_dev *dev = mt7615_hw_dev(hw);
97 struct mt7615_phy *phy = mt7615_hw_phy(hw);
98
99 cancel_delayed_work_sync(&phy->mt76->mac_work);
100 del_timer_sync(&phy->roc_timer);
101 cancel_work_sync(&phy->roc_work);
102
103 cancel_delayed_work_sync(&dev->pm.ps_work);
104 cancel_work_sync(&dev->pm.wake_work);
105
106 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
107
108 mt7615_mutex_acquire(dev);
109
110 mt76_testmode_reset(phy->mt76, true);
111
112 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
113 cancel_delayed_work_sync(&phy->scan_work);
114
115 if (phy != &dev->phy) {
116 mt7615_mcu_set_pm(dev, 1, 1);
117 mt76_connac_mcu_set_mac_enable(&dev->mt76, 1, false, false);
118 }
119
120 if (!mt7615_dev_running(dev)) {
121 mt7615_mcu_set_pm(dev, 0, 1);
122 mt76_connac_mcu_set_mac_enable(&dev->mt76, 0, false, false);
123 }
124
125 mt7615_mutex_release(dev);
126 }
127
get_free_idx(u32 mask,u8 start,u8 end)128 static inline int get_free_idx(u32 mask, u8 start, u8 end)
129 {
130 return ffs(~mask & GENMASK(end, start));
131 }
132
get_omac_idx(enum nl80211_iftype type,u64 mask)133 static int get_omac_idx(enum nl80211_iftype type, u64 mask)
134 {
135 int i;
136
137 switch (type) {
138 case NL80211_IFTYPE_STATION:
139 /* prefer hw bssid slot 1-3 */
140 i = get_free_idx(mask, HW_BSSID_1, HW_BSSID_3);
141 if (i)
142 return i - 1;
143
144 if (type != NL80211_IFTYPE_STATION)
145 break;
146
147 /* next, try to find a free repeater entry for the sta */
148 i = get_free_idx(mask >> REPEATER_BSSID_START, 0,
149 REPEATER_BSSID_MAX - REPEATER_BSSID_START);
150 if (i)
151 return i + 32 - 1;
152
153 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
154 if (i)
155 return i - 1;
156
157 if (~mask & BIT(HW_BSSID_0))
158 return HW_BSSID_0;
159
160 break;
161 case NL80211_IFTYPE_ADHOC:
162 case NL80211_IFTYPE_MESH_POINT:
163 case NL80211_IFTYPE_MONITOR:
164 case NL80211_IFTYPE_AP:
165 /* ap uses hw bssid 0 and ext bssid */
166 if (~mask & BIT(HW_BSSID_0))
167 return HW_BSSID_0;
168
169 i = get_free_idx(mask, EXT_BSSID_1, EXT_BSSID_MAX);
170 if (i)
171 return i - 1;
172
173 break;
174 default:
175 WARN_ON(1);
176 break;
177 }
178
179 return -1;
180 }
181
mt7615_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)182 static int mt7615_add_interface(struct ieee80211_hw *hw,
183 struct ieee80211_vif *vif)
184 {
185 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
186 struct mt7615_dev *dev = mt7615_hw_dev(hw);
187 struct mt7615_phy *phy = mt7615_hw_phy(hw);
188 struct mt76_txq *mtxq;
189 bool ext_phy = phy != &dev->phy;
190 int idx, ret = 0;
191
192 mt7615_mutex_acquire(dev);
193
194 mt76_testmode_reset(phy->mt76, true);
195
196 if (vif->type == NL80211_IFTYPE_MONITOR &&
197 is_zero_ether_addr(vif->addr))
198 phy->monitor_vif = vif;
199
200 mvif->mt76.idx = ffs(~dev->mt76.vif_mask) - 1;
201 if (mvif->mt76.idx >= MT7615_MAX_INTERFACES) {
202 ret = -ENOSPC;
203 goto out;
204 }
205
206 idx = get_omac_idx(vif->type, dev->omac_mask);
207 if (idx < 0) {
208 ret = -ENOSPC;
209 goto out;
210 }
211 mvif->mt76.omac_idx = idx;
212
213 mvif->mt76.band_idx = ext_phy;
214 mvif->mt76.wmm_idx = vif->type != NL80211_IFTYPE_AP;
215 if (ext_phy)
216 mvif->mt76.wmm_idx += 2;
217
218 dev->mt76.vif_mask |= BIT(mvif->mt76.idx);
219 dev->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
220 phy->omac_mask |= BIT_ULL(mvif->mt76.omac_idx);
221
222 ret = mt7615_mcu_set_dbdc(dev);
223 if (ret)
224 goto out;
225
226 idx = MT7615_WTBL_RESERVED - mvif->mt76.idx;
227
228 INIT_LIST_HEAD(&mvif->sta.poll_list);
229 mvif->sta.wcid.idx = idx;
230 mvif->sta.wcid.ext_phy = mvif->mt76.band_idx;
231 mvif->sta.wcid.hw_key_idx = -1;
232 mt7615_mac_wtbl_update(dev, idx,
233 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
234
235 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
236 if (vif->txq) {
237 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
238 mtxq->wcid = idx;
239 }
240
241 ret = mt7615_mcu_add_dev_info(phy, vif, true);
242 out:
243 mt7615_mutex_release(dev);
244
245 return ret;
246 }
247
mt7615_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)248 static void mt7615_remove_interface(struct ieee80211_hw *hw,
249 struct ieee80211_vif *vif)
250 {
251 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
252 struct mt7615_sta *msta = &mvif->sta;
253 struct mt7615_dev *dev = mt7615_hw_dev(hw);
254 struct mt7615_phy *phy = mt7615_hw_phy(hw);
255 int idx = msta->wcid.idx;
256
257 mt7615_mutex_acquire(dev);
258
259 mt7615_mcu_add_bss_info(phy, vif, NULL, false);
260 mt7615_mcu_sta_add(phy, vif, NULL, false);
261
262 mt76_testmode_reset(phy->mt76, true);
263 if (vif == phy->monitor_vif)
264 phy->monitor_vif = NULL;
265
266 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
267
268 mt7615_mcu_add_dev_info(phy, vif, false);
269
270 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
271
272 dev->mt76.vif_mask &= ~BIT(mvif->mt76.idx);
273 dev->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
274 phy->omac_mask &= ~BIT_ULL(mvif->mt76.omac_idx);
275
276 mt7615_mutex_release(dev);
277
278 spin_lock_bh(&dev->sta_poll_lock);
279 if (!list_empty(&msta->poll_list))
280 list_del_init(&msta->poll_list);
281 spin_unlock_bh(&dev->sta_poll_lock);
282 }
283
mt7615_init_dfs_state(struct mt7615_phy * phy)284 static void mt7615_init_dfs_state(struct mt7615_phy *phy)
285 {
286 struct mt76_phy *mphy = phy->mt76;
287 struct ieee80211_hw *hw = mphy->hw;
288 struct cfg80211_chan_def *chandef = &hw->conf.chandef;
289
290 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
291 return;
292
293 if (!(chandef->chan->flags & IEEE80211_CHAN_RADAR) &&
294 !(mphy->chandef.chan->flags & IEEE80211_CHAN_RADAR))
295 return;
296
297 if (mphy->chandef.chan->center_freq == chandef->chan->center_freq &&
298 mphy->chandef.width == chandef->width)
299 return;
300
301 phy->dfs_state = -1;
302 }
303
mt7615_set_channel(struct mt7615_phy * phy)304 int mt7615_set_channel(struct mt7615_phy *phy)
305 {
306 struct mt7615_dev *dev = phy->dev;
307 bool ext_phy = phy != &dev->phy;
308 int ret;
309
310 cancel_delayed_work_sync(&phy->mt76->mac_work);
311
312 mt7615_mutex_acquire(dev);
313
314 set_bit(MT76_RESET, &phy->mt76->state);
315
316 mt7615_init_dfs_state(phy);
317 mt76_set_channel(phy->mt76);
318
319 if (is_mt7615(&dev->mt76) && dev->flash_eeprom) {
320 ret = mt7615_mcu_apply_rx_dcoc(phy);
321 if (ret)
322 goto out;
323
324 ret = mt7615_mcu_apply_tx_dpd(phy);
325 if (ret)
326 goto out;
327 }
328
329 ret = mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD_CHANNEL_SWITCH);
330 if (ret)
331 goto out;
332
333 mt7615_mac_set_timing(phy);
334 ret = mt7615_dfs_init_radar_detector(phy);
335 if (ret)
336 goto out;
337
338 mt7615_mac_cca_stats_reset(phy);
339 ret = mt7615_mcu_set_sku_en(phy, true);
340 if (ret)
341 goto out;
342
343 mt7615_mac_reset_counters(dev);
344 phy->noise = 0;
345 phy->chfreq = mt76_rr(dev, MT_CHFREQ(ext_phy));
346
347 out:
348 clear_bit(MT76_RESET, &phy->mt76->state);
349
350 mt7615_mutex_release(dev);
351
352 mt76_worker_schedule(&dev->mt76.tx_worker);
353 if (!mt76_testmode_enabled(phy->mt76)) {
354 unsigned long timeout = mt7615_get_macwork_timeout(dev);
355
356 ieee80211_queue_delayed_work(phy->mt76->hw,
357 &phy->mt76->mac_work, timeout);
358 }
359
360 return ret;
361 }
362
mt7615_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)363 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
364 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
365 struct ieee80211_key_conf *key)
366 {
367 struct mt7615_dev *dev = mt7615_hw_dev(hw);
368 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
369 struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
370 &mvif->sta;
371 struct mt76_wcid *wcid = &msta->wcid;
372 int idx = key->keyidx, err = 0;
373 u8 *wcid_keyidx = &wcid->hw_key_idx;
374
375 /* The hardware does not support per-STA RX GTK, fallback
376 * to software mode for these.
377 */
378 if ((vif->type == NL80211_IFTYPE_ADHOC ||
379 vif->type == NL80211_IFTYPE_MESH_POINT) &&
380 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
381 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
382 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
383 return -EOPNOTSUPP;
384
385 /* fall back to sw encryption for unsupported ciphers */
386 switch (key->cipher) {
387 case WLAN_CIPHER_SUITE_AES_CMAC:
388 wcid_keyidx = &wcid->hw_key_idx2;
389 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
390 break;
391 case WLAN_CIPHER_SUITE_TKIP:
392 case WLAN_CIPHER_SUITE_CCMP:
393 case WLAN_CIPHER_SUITE_CCMP_256:
394 case WLAN_CIPHER_SUITE_GCMP:
395 case WLAN_CIPHER_SUITE_GCMP_256:
396 case WLAN_CIPHER_SUITE_SMS4:
397 break;
398 case WLAN_CIPHER_SUITE_WEP40:
399 case WLAN_CIPHER_SUITE_WEP104:
400 default:
401 return -EOPNOTSUPP;
402 }
403
404 mt7615_mutex_acquire(dev);
405
406 if (cmd == SET_KEY)
407 *wcid_keyidx = idx;
408 else if (idx == *wcid_keyidx)
409 *wcid_keyidx = -1;
410 else
411 goto out;
412
413 mt76_wcid_key_setup(&dev->mt76, wcid,
414 cmd == SET_KEY ? key : NULL);
415
416 if (mt76_is_mmio(&dev->mt76))
417 err = mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
418 else
419 err = __mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
420
421 out:
422 mt7615_mutex_release(dev);
423
424 return err;
425 }
426
mt7615_config(struct ieee80211_hw * hw,u32 changed)427 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
428 {
429 struct mt7615_dev *dev = mt7615_hw_dev(hw);
430 struct mt7615_phy *phy = mt7615_hw_phy(hw);
431 bool band = phy != &dev->phy;
432 int ret = 0;
433
434 if (changed & (IEEE80211_CONF_CHANGE_CHANNEL |
435 IEEE80211_CONF_CHANGE_POWER)) {
436 #ifdef CONFIG_NL80211_TESTMODE
437 if (phy->mt76->test.state != MT76_TM_STATE_OFF) {
438 mt7615_mutex_acquire(dev);
439 mt76_testmode_reset(phy->mt76, false);
440 mt7615_mutex_release(dev);
441 }
442 #endif
443 ieee80211_stop_queues(hw);
444 ret = mt7615_set_channel(phy);
445 ieee80211_wake_queues(hw);
446 }
447
448 mt7615_mutex_acquire(dev);
449
450 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
451 mt76_testmode_reset(phy->mt76, true);
452
453 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
454 phy->rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
455 else
456 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
457
458 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
459 }
460
461 mt7615_mutex_release(dev);
462
463 return ret;
464 }
465
466 static int
mt7615_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)467 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
468 const struct ieee80211_tx_queue_params *params)
469 {
470 struct mt76_vif *mvif = (struct mt76_vif *)vif->drv_priv;
471 struct mt7615_dev *dev = mt7615_hw_dev(hw);
472 int err;
473
474 mt7615_mutex_acquire(dev);
475
476 queue = mt7615_lmac_mapping(dev, queue);
477 queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
478 err = mt7615_mcu_set_wmm(dev, queue, params);
479
480 mt7615_mutex_release(dev);
481
482 return err;
483 }
484
mt7615_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)485 static void mt7615_configure_filter(struct ieee80211_hw *hw,
486 unsigned int changed_flags,
487 unsigned int *total_flags,
488 u64 multicast)
489 {
490 struct mt7615_dev *dev = mt7615_hw_dev(hw);
491 struct mt7615_phy *phy = mt7615_hw_phy(hw);
492 bool band = phy != &dev->phy;
493
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 mt7615_mutex_acquire(dev);
502
503 #define MT76_FILTER(_flag, _hw) do { \
504 flags |= *total_flags & FIF_##_flag; \
505 phy->rxfilter &= ~(_hw); \
506 if (!mt76_testmode_enabled(phy->mt76)) \
507 phy->rxfilter |= !(flags & FIF_##_flag) * (_hw);\
508 } while (0)
509
510 phy->rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
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 if (phy->n_beacon_vif || !mt7615_firmware_offload(dev))
522 phy->rxfilter &= ~MT_WF_RFCR_DROP_OTHER_BEACON;
523
524 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
525 MT_WF_RFCR_DROP_A3_MAC |
526 MT_WF_RFCR_DROP_A3_BSSID);
527
528 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
529
530 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
531 MT_WF_RFCR_DROP_RTS |
532 MT_WF_RFCR_DROP_CTL_RSV |
533 MT_WF_RFCR_DROP_NDPA);
534
535 *total_flags = flags;
536 mt76_wr(dev, MT_WF_RFCR(band), phy->rxfilter);
537
538 if (*total_flags & FIF_CONTROL)
539 mt76_clear(dev, MT_WF_RFCR1(band), ctl_flags);
540 else
541 mt76_set(dev, MT_WF_RFCR1(band), ctl_flags);
542
543 mt7615_mutex_release(dev);
544 }
545
mt7615_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)546 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
547 struct ieee80211_vif *vif,
548 struct ieee80211_bss_conf *info,
549 u32 changed)
550 {
551 struct mt7615_dev *dev = mt7615_hw_dev(hw);
552 struct mt7615_phy *phy = mt7615_hw_phy(hw);
553
554 mt7615_mutex_acquire(dev);
555
556 if (changed & BSS_CHANGED_ERP_SLOT) {
557 int slottime = info->use_short_slot ? 9 : 20;
558
559 if (slottime != phy->slottime) {
560 phy->slottime = slottime;
561 mt7615_mac_set_timing(phy);
562 }
563 }
564
565 if (changed & BSS_CHANGED_BEACON_ENABLED && info->enable_beacon) {
566 mt7615_mcu_add_bss_info(phy, vif, NULL, true);
567 mt7615_mcu_sta_add(phy, vif, NULL, true);
568
569 if (vif->p2p)
570 mt7615_mcu_set_p2p_oppps(hw, vif);
571 }
572
573 if (changed & (BSS_CHANGED_BEACON |
574 BSS_CHANGED_BEACON_ENABLED))
575 mt7615_mcu_add_beacon(dev, hw, vif, info->enable_beacon);
576
577 if (changed & BSS_CHANGED_PS)
578 mt76_connac_mcu_set_vif_ps(&dev->mt76, vif);
579
580 if ((changed & BSS_CHANGED_ARP_FILTER) &&
581 mt7615_firmware_offload(dev)) {
582 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
583
584 mt76_connac_mcu_update_arp_filter(&dev->mt76, &mvif->mt76,
585 info);
586 }
587
588 if (changed & BSS_CHANGED_ASSOC)
589 mt7615_mac_set_beacon_filter(phy, vif, info->assoc);
590
591 mt7615_mutex_release(dev);
592 }
593
594 static void
mt7615_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)595 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
596 struct ieee80211_vif *vif,
597 struct cfg80211_chan_def *chandef)
598 {
599 struct mt7615_dev *dev = mt7615_hw_dev(hw);
600
601 mt7615_mutex_acquire(dev);
602 mt7615_mcu_add_beacon(dev, hw, vif, true);
603 mt7615_mutex_release(dev);
604 }
605
mt7615_mac_sta_add(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)606 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
607 struct ieee80211_sta *sta)
608 {
609 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
610 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
611 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
612 struct mt7615_phy *phy;
613 int idx, err;
614
615 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
616 if (idx < 0)
617 return -ENOSPC;
618
619 INIT_LIST_HEAD(&msta->poll_list);
620 msta->vif = mvif;
621 msta->wcid.sta = 1;
622 msta->wcid.idx = idx;
623 msta->wcid.ext_phy = mvif->mt76.band_idx;
624
625 phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
626 err = mt76_connac_pm_wake(phy->mt76, &dev->pm);
627 if (err)
628 return err;
629
630 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
631 err = mt7615_mcu_add_bss_info(phy, vif, sta, true);
632 if (err)
633 return err;
634 }
635
636 mt7615_mac_wtbl_update(dev, idx,
637 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
638 err = mt7615_mcu_sta_add(&dev->phy, vif, sta, true);
639 if (err)
640 return err;
641
642 mt76_connac_power_save_sched(phy->mt76, &dev->pm);
643
644 return err;
645 }
646 EXPORT_SYMBOL_GPL(mt7615_mac_sta_add);
647
mt7615_mac_sta_remove(struct mt76_dev * mdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta)648 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
649 struct ieee80211_sta *sta)
650 {
651 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
652 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
653 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
654 struct mt7615_phy *phy;
655
656 mt76_connac_free_pending_tx_skbs(&dev->pm, &msta->wcid);
657
658 phy = mvif->mt76.band_idx ? mt7615_ext_phy(dev) : &dev->phy;
659 mt76_connac_pm_wake(phy->mt76, &dev->pm);
660
661 mt7615_mcu_sta_add(&dev->phy, vif, sta, false);
662 mt7615_mac_wtbl_update(dev, msta->wcid.idx,
663 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
664 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
665 mt7615_mcu_add_bss_info(phy, vif, sta, false);
666
667 spin_lock_bh(&dev->sta_poll_lock);
668 if (!list_empty(&msta->poll_list))
669 list_del_init(&msta->poll_list);
670 spin_unlock_bh(&dev->sta_poll_lock);
671
672 mt76_connac_power_save_sched(phy->mt76, &dev->pm);
673 }
674 EXPORT_SYMBOL_GPL(mt7615_mac_sta_remove);
675
mt7615_sta_rate_tbl_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)676 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
677 struct ieee80211_vif *vif,
678 struct ieee80211_sta *sta)
679 {
680 struct mt7615_dev *dev = mt7615_hw_dev(hw);
681 struct mt7615_phy *phy = mt7615_hw_phy(hw);
682 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
683 struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
684 int i;
685
686 if (!sta_rates)
687 return;
688
689 spin_lock_bh(&dev->mt76.lock);
690 for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
691 msta->rates[i].idx = sta_rates->rate[i].idx;
692 msta->rates[i].count = sta_rates->rate[i].count;
693 msta->rates[i].flags = sta_rates->rate[i].flags;
694
695 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
696 break;
697 }
698 msta->n_rates = i;
699 if (mt76_connac_pm_ref(phy->mt76, &dev->pm)) {
700 mt7615_mac_set_rates(phy, msta, NULL, msta->rates);
701 mt76_connac_pm_unref(phy->mt76, &dev->pm);
702 }
703 spin_unlock_bh(&dev->mt76.lock);
704 }
705
mt7615_tx_worker(struct mt76_worker * w)706 void mt7615_tx_worker(struct mt76_worker *w)
707 {
708 struct mt7615_dev *dev = container_of(w, struct mt7615_dev,
709 mt76.tx_worker);
710
711 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) {
712 queue_work(dev->mt76.wq, &dev->pm.wake_work);
713 return;
714 }
715
716 mt76_tx_worker_run(&dev->mt76);
717 mt76_connac_pm_unref(&dev->mphy, &dev->pm);
718 }
719
mt7615_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)720 static void mt7615_tx(struct ieee80211_hw *hw,
721 struct ieee80211_tx_control *control,
722 struct sk_buff *skb)
723 {
724 struct mt7615_dev *dev = mt7615_hw_dev(hw);
725 struct mt76_phy *mphy = hw->priv;
726 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
727 struct ieee80211_vif *vif = info->control.vif;
728 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
729 struct mt7615_sta *msta = NULL;
730 int qid;
731
732 if (control->sta) {
733 msta = (struct mt7615_sta *)control->sta->drv_priv;
734 wcid = &msta->wcid;
735 }
736
737 if (vif && !control->sta) {
738 struct mt7615_vif *mvif;
739
740 mvif = (struct mt7615_vif *)vif->drv_priv;
741 msta = &mvif->sta;
742 wcid = &msta->wcid;
743 }
744
745 if (mt76_connac_pm_ref(mphy, &dev->pm)) {
746 mt76_tx(mphy, control->sta, wcid, skb);
747 mt76_connac_pm_unref(mphy, &dev->pm);
748 return;
749 }
750
751 qid = skb_get_queue_mapping(skb);
752 if (qid >= MT_TXQ_PSD) {
753 qid = IEEE80211_AC_BE;
754 skb_set_queue_mapping(skb, qid);
755 }
756
757 mt76_connac_pm_queue_skb(hw, &dev->pm, wcid, skb);
758 }
759
mt7615_set_rts_threshold(struct ieee80211_hw * hw,u32 val)760 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
761 {
762 struct mt7615_dev *dev = mt7615_hw_dev(hw);
763 struct mt7615_phy *phy = mt7615_hw_phy(hw);
764 int err, band = phy != &dev->phy;
765
766 mt7615_mutex_acquire(dev);
767 err = mt76_connac_mcu_set_rts_thresh(&dev->mt76, val, band);
768 mt7615_mutex_release(dev);
769
770 return err;
771 }
772
773 static int
mt7615_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)774 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
775 struct ieee80211_ampdu_params *params)
776 {
777 enum ieee80211_ampdu_mlme_action action = params->action;
778 struct mt7615_dev *dev = mt7615_hw_dev(hw);
779 struct ieee80211_sta *sta = params->sta;
780 struct ieee80211_txq *txq = sta->txq[params->tid];
781 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
782 u16 tid = params->tid;
783 u16 ssn = params->ssn;
784 struct mt76_txq *mtxq;
785 int ret = 0;
786
787 if (!txq)
788 return -EINVAL;
789
790 mtxq = (struct mt76_txq *)txq->drv_priv;
791
792 mt7615_mutex_acquire(dev);
793
794 switch (action) {
795 case IEEE80211_AMPDU_RX_START:
796 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
797 params->buf_size);
798 ret = mt7615_mcu_add_rx_ba(dev, params, true);
799 break;
800 case IEEE80211_AMPDU_RX_STOP:
801 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
802 ret = mt7615_mcu_add_rx_ba(dev, params, false);
803 break;
804 case IEEE80211_AMPDU_TX_OPERATIONAL:
805 mtxq->aggr = true;
806 mtxq->send_bar = false;
807 ret = mt7615_mcu_add_tx_ba(dev, params, true);
808 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
809 ieee80211_send_bar(vif, sta->addr, tid,
810 IEEE80211_SN_TO_SEQ(ssn));
811 break;
812 case IEEE80211_AMPDU_TX_STOP_FLUSH:
813 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
814 mtxq->aggr = false;
815 ret = mt7615_mcu_add_tx_ba(dev, params, false);
816 break;
817 case IEEE80211_AMPDU_TX_START:
818 ssn = mt7615_mac_get_sta_tid_sn(dev, msta->wcid.idx, tid);
819 params->ssn = ssn;
820 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
821 break;
822 case IEEE80211_AMPDU_TX_STOP_CONT:
823 mtxq->aggr = false;
824 ret = mt7615_mcu_add_tx_ba(dev, params, false);
825 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
826 break;
827 }
828 mt7615_mutex_release(dev);
829
830 return ret;
831 }
832
833 static int
mt7615_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)834 mt7615_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
835 struct ieee80211_sta *sta)
836 {
837 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST,
838 IEEE80211_STA_NONE);
839 }
840
841 static int
mt7615_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)842 mt7615_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
843 struct ieee80211_sta *sta)
844 {
845 return mt76_sta_state(hw, vif, sta, IEEE80211_STA_NONE,
846 IEEE80211_STA_NOTEXIST);
847 }
848
849 static int
mt7615_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)850 mt7615_get_stats(struct ieee80211_hw *hw,
851 struct ieee80211_low_level_stats *stats)
852 {
853 struct mt7615_phy *phy = mt7615_hw_phy(hw);
854 struct mib_stats *mib = &phy->mib;
855
856 mt7615_mutex_acquire(phy->dev);
857
858 stats->dot11RTSSuccessCount = mib->rts_cnt;
859 stats->dot11RTSFailureCount = mib->rts_retries_cnt;
860 stats->dot11FCSErrorCount = mib->fcs_err_cnt;
861 stats->dot11ACKFailureCount = mib->ack_fail_cnt;
862
863 memset(mib, 0, sizeof(*mib));
864
865 mt7615_mutex_release(phy->dev);
866
867 return 0;
868 }
869
870 static u64
mt7615_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)871 mt7615_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
872 {
873 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
874 struct mt7615_dev *dev = mt7615_hw_dev(hw);
875 union {
876 u64 t64;
877 u32 t32[2];
878 } tsf;
879 u16 idx = mvif->mt76.omac_idx;
880 u32 reg;
881
882 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
883 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
884
885 mt7615_mutex_acquire(dev);
886
887 /* TSF read */
888 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_READ);
889 tsf.t32[0] = mt76_rr(dev, MT_LPON_UTTR0);
890 tsf.t32[1] = mt76_rr(dev, MT_LPON_UTTR1);
891
892 mt7615_mutex_release(dev);
893
894 return tsf.t64;
895 }
896
897 static void
mt7615_set_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 timestamp)898 mt7615_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
899 u64 timestamp)
900 {
901 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
902 struct mt7615_dev *dev = mt7615_hw_dev(hw);
903 union {
904 u64 t64;
905 u32 t32[2];
906 } tsf = { .t64 = timestamp, };
907 u16 idx = mvif->mt76.omac_idx;
908 u32 reg;
909
910 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
911 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
912
913 mt7615_mutex_acquire(dev);
914
915 mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
916 mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
917 /* TSF software overwrite */
918 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_WRITE);
919
920 mt7615_mutex_release(dev);
921 }
922
923 static void
mt7615_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 timestamp)924 mt7615_offset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
925 s64 timestamp)
926 {
927 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
928 struct mt7615_dev *dev = mt7615_hw_dev(hw);
929 union {
930 u64 t64;
931 u32 t32[2];
932 } tsf = { .t64 = timestamp, };
933 u16 idx = mvif->mt76.omac_idx;
934 u32 reg;
935
936 idx = idx > HW_BSSID_MAX ? HW_BSSID_0 : idx;
937 reg = idx > 1 ? MT_LPON_TCR2(idx): MT_LPON_TCR0(idx);
938
939 mt7615_mutex_acquire(dev);
940
941 mt76_wr(dev, MT_LPON_UTTR0, tsf.t32[0]);
942 mt76_wr(dev, MT_LPON_UTTR1, tsf.t32[1]);
943 /* TSF software adjust*/
944 mt76_rmw(dev, reg, MT_LPON_TCR_MODE, MT_LPON_TCR_ADJUST);
945
946 mt7615_mutex_release(dev);
947 }
948
949 static void
mt7615_set_coverage_class(struct ieee80211_hw * hw,s16 coverage_class)950 mt7615_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
951 {
952 struct mt7615_phy *phy = mt7615_hw_phy(hw);
953 struct mt7615_dev *dev = phy->dev;
954
955 mt7615_mutex_acquire(dev);
956 phy->coverage_class = max_t(s16, coverage_class, 0);
957 mt7615_mac_set_timing(phy);
958 mt7615_mutex_release(dev);
959 }
960
961 static int
mt7615_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)962 mt7615_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
963 {
964 struct mt7615_dev *dev = mt7615_hw_dev(hw);
965 struct mt7615_phy *phy = mt7615_hw_phy(hw);
966 int max_nss = hweight8(hw->wiphy->available_antennas_tx);
967 bool ext_phy = phy != &dev->phy;
968
969 if (!tx_ant || tx_ant != rx_ant || ffs(tx_ant) > max_nss)
970 return -EINVAL;
971
972 if ((BIT(hweight8(tx_ant)) - 1) != tx_ant)
973 tx_ant = BIT(ffs(tx_ant) - 1) - 1;
974
975 mt7615_mutex_acquire(dev);
976
977 phy->mt76->antenna_mask = tx_ant;
978 if (ext_phy) {
979 if (dev->chainmask == 0xf)
980 tx_ant <<= 2;
981 else
982 tx_ant <<= 1;
983 }
984 phy->mt76->chainmask = tx_ant;
985
986 mt76_set_stream_caps(phy->mt76, true);
987
988 mt7615_mutex_release(dev);
989
990 return 0;
991 }
992
mt7615_roc_iter(void * priv,u8 * mac,struct ieee80211_vif * vif)993 static void mt7615_roc_iter(void *priv, u8 *mac,
994 struct ieee80211_vif *vif)
995 {
996 struct mt7615_phy *phy = priv;
997
998 mt7615_mcu_set_roc(phy, vif, NULL, 0);
999 }
1000
mt7615_roc_work(struct work_struct * work)1001 void mt7615_roc_work(struct work_struct *work)
1002 {
1003 struct mt7615_phy *phy;
1004
1005 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1006 roc_work);
1007
1008 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1009 return;
1010
1011 mt7615_mutex_acquire(phy->dev);
1012 ieee80211_iterate_active_interfaces(phy->mt76->hw,
1013 IEEE80211_IFACE_ITER_RESUME_ALL,
1014 mt7615_roc_iter, phy);
1015 mt7615_mutex_release(phy->dev);
1016 ieee80211_remain_on_channel_expired(phy->mt76->hw);
1017 }
1018
mt7615_roc_timer(struct timer_list * timer)1019 void mt7615_roc_timer(struct timer_list *timer)
1020 {
1021 struct mt7615_phy *phy = from_timer(phy, timer, roc_timer);
1022
1023 ieee80211_queue_work(phy->mt76->hw, &phy->roc_work);
1024 }
1025
mt7615_scan_work(struct work_struct * work)1026 void mt7615_scan_work(struct work_struct *work)
1027 {
1028 struct mt7615_phy *phy;
1029
1030 phy = (struct mt7615_phy *)container_of(work, struct mt7615_phy,
1031 scan_work.work);
1032
1033 while (true) {
1034 struct mt7615_mcu_rxd *rxd;
1035 struct sk_buff *skb;
1036
1037 spin_lock_bh(&phy->dev->mt76.lock);
1038 skb = __skb_dequeue(&phy->scan_event_list);
1039 spin_unlock_bh(&phy->dev->mt76.lock);
1040
1041 if (!skb)
1042 break;
1043
1044 rxd = (struct mt7615_mcu_rxd *)skb->data;
1045 if (rxd->eid == MCU_EVENT_SCHED_SCAN_DONE) {
1046 ieee80211_sched_scan_results(phy->mt76->hw);
1047 } else if (test_and_clear_bit(MT76_HW_SCANNING,
1048 &phy->mt76->state)) {
1049 struct cfg80211_scan_info info = {
1050 .aborted = false,
1051 };
1052
1053 ieee80211_scan_completed(phy->mt76->hw, &info);
1054 }
1055 dev_kfree_skb(skb);
1056 }
1057 }
1058
1059 static int
mt7615_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * req)1060 mt7615_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1061 struct ieee80211_scan_request *req)
1062 {
1063 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1064 struct mt76_phy *mphy = hw->priv;
1065 int err;
1066
1067 /* fall-back to sw-scan */
1068 if (!mt7615_firmware_offload(dev))
1069 return 1;
1070
1071 mt7615_mutex_acquire(dev);
1072 err = mt76_connac_mcu_hw_scan(mphy, vif, req);
1073 mt7615_mutex_release(dev);
1074
1075 return err;
1076 }
1077
1078 static void
mt7615_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1079 mt7615_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1080 {
1081 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1082 struct mt76_phy *mphy = hw->priv;
1083
1084 mt7615_mutex_acquire(dev);
1085 mt76_connac_mcu_cancel_hw_scan(mphy, vif);
1086 mt7615_mutex_release(dev);
1087 }
1088
1089 static int
mt7615_start_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * req,struct ieee80211_scan_ies * ies)1090 mt7615_start_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1091 struct cfg80211_sched_scan_request *req,
1092 struct ieee80211_scan_ies *ies)
1093 {
1094 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1095 struct mt76_phy *mphy = hw->priv;
1096 int err;
1097
1098 if (!mt7615_firmware_offload(dev))
1099 return -EOPNOTSUPP;
1100
1101 mt7615_mutex_acquire(dev);
1102
1103 err = mt76_connac_mcu_sched_scan_req(mphy, vif, req);
1104 if (err < 0)
1105 goto out;
1106
1107 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, true);
1108 out:
1109 mt7615_mutex_release(dev);
1110
1111 return err;
1112 }
1113
1114 static int
mt7615_stop_sched_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1115 mt7615_stop_sched_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1116 {
1117 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1118 struct mt76_phy *mphy = hw->priv;
1119 int err;
1120
1121 if (!mt7615_firmware_offload(dev))
1122 return -EOPNOTSUPP;
1123
1124 mt7615_mutex_acquire(dev);
1125 err = mt76_connac_mcu_sched_scan_enable(mphy, vif, false);
1126 mt7615_mutex_release(dev);
1127
1128 return err;
1129 }
1130
mt7615_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)1131 static int mt7615_remain_on_channel(struct ieee80211_hw *hw,
1132 struct ieee80211_vif *vif,
1133 struct ieee80211_channel *chan,
1134 int duration,
1135 enum ieee80211_roc_type type)
1136 {
1137 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1138 int err;
1139
1140 if (test_and_set_bit(MT76_STATE_ROC, &phy->mt76->state))
1141 return 0;
1142
1143 mt7615_mutex_acquire(phy->dev);
1144
1145 err = mt7615_mcu_set_roc(phy, vif, chan, duration);
1146 if (err < 0) {
1147 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1148 goto out;
1149 }
1150
1151 if (!wait_event_timeout(phy->roc_wait, phy->roc_grant, HZ)) {
1152 mt7615_mcu_set_roc(phy, vif, NULL, 0);
1153 clear_bit(MT76_STATE_ROC, &phy->mt76->state);
1154 err = -ETIMEDOUT;
1155 }
1156
1157 out:
1158 mt7615_mutex_release(phy->dev);
1159
1160 return err;
1161 }
1162
mt7615_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1163 static int mt7615_cancel_remain_on_channel(struct ieee80211_hw *hw,
1164 struct ieee80211_vif *vif)
1165 {
1166 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1167 int err;
1168
1169 if (!test_and_clear_bit(MT76_STATE_ROC, &phy->mt76->state))
1170 return 0;
1171
1172 del_timer_sync(&phy->roc_timer);
1173 cancel_work_sync(&phy->roc_work);
1174
1175 mt7615_mutex_acquire(phy->dev);
1176 err = mt7615_mcu_set_roc(phy, vif, NULL, 0);
1177 mt7615_mutex_release(phy->dev);
1178
1179 return err;
1180 }
1181
mt7615_sta_set_decap_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)1182 static void mt7615_sta_set_decap_offload(struct ieee80211_hw *hw,
1183 struct ieee80211_vif *vif,
1184 struct ieee80211_sta *sta,
1185 bool enabled)
1186 {
1187 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1188 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
1189
1190 mt7615_mutex_acquire(dev);
1191
1192 if (enabled)
1193 set_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1194 else
1195 clear_bit(MT_WCID_FLAG_HDR_TRANS, &msta->wcid.flags);
1196
1197 mt7615_mcu_set_sta_decap_offload(dev, vif, sta);
1198
1199 mt7615_mutex_release(dev);
1200 }
1201
1202 #ifdef CONFIG_PM
mt7615_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1203 static int mt7615_suspend(struct ieee80211_hw *hw,
1204 struct cfg80211_wowlan *wowlan)
1205 {
1206 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1207 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1208 int err = 0;
1209
1210 cancel_delayed_work_sync(&dev->pm.ps_work);
1211 mt76_connac_free_pending_tx_skbs(&dev->pm, NULL);
1212
1213 mt7615_mutex_acquire(dev);
1214
1215 clear_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1216 cancel_delayed_work_sync(&phy->scan_work);
1217 cancel_delayed_work_sync(&phy->mt76->mac_work);
1218
1219 set_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1220 ieee80211_iterate_active_interfaces(hw,
1221 IEEE80211_IFACE_ITER_RESUME_ALL,
1222 mt76_connac_mcu_set_suspend_iter,
1223 phy->mt76);
1224
1225 if (!mt7615_dev_running(dev))
1226 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, true);
1227
1228 mt7615_mutex_release(dev);
1229
1230 return err;
1231 }
1232
mt7615_resume(struct ieee80211_hw * hw)1233 static int mt7615_resume(struct ieee80211_hw *hw)
1234 {
1235 struct mt7615_phy *phy = mt7615_hw_phy(hw);
1236 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1237 unsigned long timeout;
1238 bool running;
1239
1240 mt7615_mutex_acquire(dev);
1241
1242 running = mt7615_dev_running(dev);
1243 set_bit(MT76_STATE_RUNNING, &phy->mt76->state);
1244
1245 if (!running) {
1246 int err;
1247
1248 err = mt76_connac_mcu_set_hif_suspend(&dev->mt76, false);
1249 if (err < 0) {
1250 mt7615_mutex_release(dev);
1251 return err;
1252 }
1253 }
1254
1255 clear_bit(MT76_STATE_SUSPEND, &phy->mt76->state);
1256 ieee80211_iterate_active_interfaces(hw,
1257 IEEE80211_IFACE_ITER_RESUME_ALL,
1258 mt76_connac_mcu_set_suspend_iter,
1259 phy->mt76);
1260
1261 timeout = mt7615_get_macwork_timeout(dev);
1262 ieee80211_queue_delayed_work(hw, &phy->mt76->mac_work, timeout);
1263
1264 mt7615_mutex_release(dev);
1265
1266 return 0;
1267 }
1268
mt7615_set_wakeup(struct ieee80211_hw * hw,bool enabled)1269 static void mt7615_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1270 {
1271 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1272 struct mt76_dev *mdev = &dev->mt76;
1273
1274 device_set_wakeup_enable(mdev->dev, enabled);
1275 }
1276
mt7615_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)1277 static void mt7615_set_rekey_data(struct ieee80211_hw *hw,
1278 struct ieee80211_vif *vif,
1279 struct cfg80211_gtk_rekey_data *data)
1280 {
1281 struct mt7615_dev *dev = mt7615_hw_dev(hw);
1282
1283 mt7615_mutex_acquire(dev);
1284 mt76_connac_mcu_update_gtk_rekey(hw, vif, data);
1285 mt7615_mutex_release(dev);
1286 }
1287 #endif /* CONFIG_PM */
1288
1289 const struct ieee80211_ops mt7615_ops = {
1290 .tx = mt7615_tx,
1291 .start = mt7615_start,
1292 .stop = mt7615_stop,
1293 .add_interface = mt7615_add_interface,
1294 .remove_interface = mt7615_remove_interface,
1295 .config = mt7615_config,
1296 .conf_tx = mt7615_conf_tx,
1297 .configure_filter = mt7615_configure_filter,
1298 .bss_info_changed = mt7615_bss_info_changed,
1299 .sta_add = mt7615_sta_add,
1300 .sta_remove = mt7615_sta_remove,
1301 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
1302 .set_key = mt7615_set_key,
1303 .sta_set_decap_offload = mt7615_sta_set_decap_offload,
1304 .ampdu_action = mt7615_ampdu_action,
1305 .set_rts_threshold = mt7615_set_rts_threshold,
1306 .wake_tx_queue = mt76_wake_tx_queue,
1307 .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
1308 .sw_scan_start = mt76_sw_scan,
1309 .sw_scan_complete = mt76_sw_scan_complete,
1310 .release_buffered_frames = mt76_release_buffered_frames,
1311 .get_txpower = mt76_get_txpower,
1312 .channel_switch_beacon = mt7615_channel_switch_beacon,
1313 .get_stats = mt7615_get_stats,
1314 .get_tsf = mt7615_get_tsf,
1315 .set_tsf = mt7615_set_tsf,
1316 .offset_tsf = mt7615_offset_tsf,
1317 .get_survey = mt76_get_survey,
1318 .get_antenna = mt76_get_antenna,
1319 .set_antenna = mt7615_set_antenna,
1320 .set_coverage_class = mt7615_set_coverage_class,
1321 .hw_scan = mt7615_hw_scan,
1322 .cancel_hw_scan = mt7615_cancel_hw_scan,
1323 .sched_scan_start = mt7615_start_sched_scan,
1324 .sched_scan_stop = mt7615_stop_sched_scan,
1325 .remain_on_channel = mt7615_remain_on_channel,
1326 .cancel_remain_on_channel = mt7615_cancel_remain_on_channel,
1327 CFG80211_TESTMODE_CMD(mt76_testmode_cmd)
1328 CFG80211_TESTMODE_DUMP(mt76_testmode_dump)
1329 #ifdef CONFIG_PM
1330 .suspend = mt7615_suspend,
1331 .resume = mt7615_resume,
1332 .set_wakeup = mt7615_set_wakeup,
1333 .set_rekey_data = mt7615_set_rekey_data,
1334 #endif /* CONFIG_PM */
1335 };
1336 EXPORT_SYMBOL_GPL(mt7615_ops);
1337
1338 MODULE_LICENSE("Dual BSD/GPL");
1339