1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
4 * Author: Roy Luo <royluo@google.com>
5 * Ryder Lee <ryder.lee@mediatek.com>
6 * Felix Fietkau <nbd@nbd.name>
9 #include <linux/etherdevice.h>
10 #include <linux/platform_device.h>
11 #include <linux/pci.h>
12 #include <linux/module.h>
15 static int mt7615_start(struct ieee80211_hw *hw)
17 struct mt7615_dev *dev = hw->priv;
19 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
20 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
21 MT7615_WATCHDOG_TIME);
26 static void mt7615_stop(struct ieee80211_hw *hw)
28 struct mt7615_dev *dev = hw->priv;
30 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
31 cancel_delayed_work_sync(&dev->mt76.mac_work);
34 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
39 case NL80211_IFTYPE_AP:
40 case NL80211_IFTYPE_MESH_POINT:
41 /* ap use hw bssid 0 and ext bssid */
42 if (~mask & BIT(HW_BSSID_0))
45 for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
50 case NL80211_IFTYPE_STATION:
51 /* sta use hw bssid other than 0 */
52 for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
65 static int mt7615_add_interface(struct ieee80211_hw *hw,
66 struct ieee80211_vif *vif)
68 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
69 struct mt7615_dev *dev = hw->priv;
70 struct mt76_txq *mtxq;
73 mutex_lock(&dev->mt76.mutex);
75 mvif->idx = ffs(~dev->vif_mask) - 1;
76 if (mvif->idx >= MT7615_MAX_INTERFACES) {
81 idx = get_omac_idx(vif->type, dev->omac_mask);
88 /* TODO: DBDC support. Use band 0 and wmm 0 for now */
92 ret = mt7615_mcu_set_dev_info(dev, vif, 1);
96 dev->vif_mask |= BIT(mvif->idx);
97 dev->omac_mask |= BIT(mvif->omac_idx);
98 idx = MT7615_WTBL_RESERVED - mvif->idx;
99 mvif->sta.wcid.idx = idx;
100 mvif->sta.wcid.hw_key_idx = -1;
102 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
103 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
104 mtxq->wcid = &mvif->sta.wcid;
105 mt76_txq_init(&dev->mt76, vif->txq);
108 mutex_unlock(&dev->mt76.mutex);
113 static void mt7615_remove_interface(struct ieee80211_hw *hw,
114 struct ieee80211_vif *vif)
116 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
117 struct mt7615_dev *dev = hw->priv;
118 int idx = mvif->sta.wcid.idx;
120 /* TODO: disable beacon for the bss */
122 mt7615_mcu_set_dev_info(dev, vif, 0);
124 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
125 mt76_txq_remove(&dev->mt76, vif->txq);
127 mutex_lock(&dev->mt76.mutex);
128 dev->vif_mask &= ~BIT(mvif->idx);
129 dev->omac_mask &= ~BIT(mvif->omac_idx);
130 mutex_unlock(&dev->mt76.mutex);
133 static int mt7615_set_channel(struct mt7615_dev *dev)
137 cancel_delayed_work_sync(&dev->mt76.mac_work);
138 set_bit(MT76_RESET, &dev->mt76.state);
140 mt7615_dfs_check_channel(dev);
142 mt76_set_channel(&dev->mt76);
144 ret = mt7615_mcu_set_channel(dev);
148 ret = mt7615_dfs_init_radar_detector(dev);
152 clear_bit(MT76_RESET, &dev->mt76.state);
154 mt76_txq_schedule_all(&dev->mt76);
155 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
156 MT7615_WATCHDOG_TIME);
160 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
161 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
162 struct ieee80211_key_conf *key)
164 struct mt7615_dev *dev = hw->priv;
165 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
166 struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
168 struct mt76_wcid *wcid = &msta->wcid;
169 int idx = key->keyidx;
171 /* The hardware does not support per-STA RX GTK, fallback
172 * to software mode for these.
174 if ((vif->type == NL80211_IFTYPE_ADHOC ||
175 vif->type == NL80211_IFTYPE_MESH_POINT) &&
176 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
177 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
178 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
181 /* fall back to sw encryption for unsupported ciphers */
182 switch (key->cipher) {
183 case WLAN_CIPHER_SUITE_WEP40:
184 case WLAN_CIPHER_SUITE_WEP104:
185 case WLAN_CIPHER_SUITE_TKIP:
186 case WLAN_CIPHER_SUITE_CCMP:
187 case WLAN_CIPHER_SUITE_CCMP_256:
188 case WLAN_CIPHER_SUITE_GCMP:
189 case WLAN_CIPHER_SUITE_GCMP_256:
190 case WLAN_CIPHER_SUITE_SMS4:
196 if (cmd == SET_KEY) {
197 key->hw_key_idx = wcid->idx;
198 wcid->hw_key_idx = idx;
200 if (idx == wcid->hw_key_idx)
201 wcid->hw_key_idx = -1;
205 mt76_wcid_key_setup(&dev->mt76, wcid, key);
207 return mt7615_mcu_set_wtbl_key(dev, wcid->idx, key, cmd);
210 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
212 struct mt7615_dev *dev = hw->priv;
215 mutex_lock(&dev->mt76.mutex);
217 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
218 ieee80211_stop_queues(hw);
219 ret = mt7615_set_channel(dev);
220 ieee80211_wake_queues(hw);
223 if (changed & IEEE80211_CONF_CHANGE_POWER)
224 ret = mt7615_mcu_set_tx_power(dev);
226 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
227 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
228 dev->mt76.rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
230 dev->mt76.rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
232 mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
235 mutex_unlock(&dev->mt76.mutex);
241 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
242 const struct ieee80211_tx_queue_params *params)
244 struct mt7615_dev *dev = hw->priv;
245 static const u8 wmm_queue_map[] = {
246 [IEEE80211_AC_BK] = 0,
247 [IEEE80211_AC_BE] = 1,
248 [IEEE80211_AC_VI] = 2,
249 [IEEE80211_AC_VO] = 3,
252 /* TODO: hw wmm_set 1~3 */
253 return mt7615_mcu_set_wmm(dev, wmm_queue_map[queue], params);
256 static void mt7615_configure_filter(struct ieee80211_hw *hw,
257 unsigned int changed_flags,
258 unsigned int *total_flags,
261 struct mt7615_dev *dev = hw->priv;
264 #define MT76_FILTER(_flag, _hw) do { \
265 flags |= *total_flags & FIF_##_flag; \
266 dev->mt76.rxfilter &= ~(_hw); \
267 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
270 dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
271 MT_WF_RFCR_DROP_OTHER_BEACON |
272 MT_WF_RFCR_DROP_FRAME_REPORT |
273 MT_WF_RFCR_DROP_PROBEREQ |
274 MT_WF_RFCR_DROP_MCAST_FILTERED |
275 MT_WF_RFCR_DROP_MCAST |
276 MT_WF_RFCR_DROP_BCAST |
277 MT_WF_RFCR_DROP_DUPLICATE |
278 MT_WF_RFCR_DROP_A2_BSSID |
279 MT_WF_RFCR_DROP_UNWANTED_CTL |
280 MT_WF_RFCR_DROP_STBC_MULTI);
282 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
283 MT_WF_RFCR_DROP_A3_MAC |
284 MT_WF_RFCR_DROP_A3_BSSID);
286 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
288 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
289 MT_WF_RFCR_DROP_RTS |
290 MT_WF_RFCR_DROP_CTL_RSV |
291 MT_WF_RFCR_DROP_NDPA);
293 *total_flags = flags;
294 mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
297 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
298 struct ieee80211_vif *vif,
299 struct ieee80211_bss_conf *info,
302 struct mt7615_dev *dev = hw->priv;
304 mutex_lock(&dev->mt76.mutex);
306 if (changed & BSS_CHANGED_ASSOC)
307 mt7615_mcu_set_bss_info(dev, vif, info->assoc);
309 /* TODO: update beacon content
313 if (changed & BSS_CHANGED_BEACON_ENABLED) {
314 mt7615_mcu_set_bss_info(dev, vif, info->enable_beacon);
315 mt7615_mcu_wtbl_bmc(dev, vif, info->enable_beacon);
316 mt7615_mcu_set_sta_rec_bmc(dev, vif, info->enable_beacon);
317 mt7615_mcu_set_bcn(dev, vif, info->enable_beacon);
320 mutex_unlock(&dev->mt76.mutex);
324 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
325 struct ieee80211_vif *vif,
326 struct cfg80211_chan_def *chandef)
328 struct mt7615_dev *dev = hw->priv;
330 mutex_lock(&dev->mt76.mutex);
331 mt7615_mcu_set_bcn(dev, vif, true);
332 mutex_unlock(&dev->mt76.mutex);
335 int mt7615_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
336 struct ieee80211_sta *sta)
338 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
339 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
340 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
343 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
349 msta->wcid.idx = idx;
351 mt7615_mcu_add_wtbl(dev, vif, sta);
352 mt7615_mcu_set_sta_rec(dev, vif, sta, 1);
357 void mt7615_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
358 struct ieee80211_sta *sta)
360 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
362 if (sta->ht_cap.ht_supported)
363 mt7615_mcu_set_ht_cap(dev, vif, sta);
366 void mt7615_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
367 struct ieee80211_sta *sta)
369 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
371 mt7615_mcu_set_sta_rec(dev, vif, sta, 0);
372 mt7615_mcu_del_wtbl(dev, sta);
375 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
376 struct ieee80211_vif *vif,
377 struct ieee80211_sta *sta)
379 struct mt7615_dev *dev = hw->priv;
380 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
381 struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
384 spin_lock_bh(&dev->mt76.lock);
385 for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
386 msta->rates[i].idx = sta_rates->rate[i].idx;
387 msta->rates[i].count = sta_rates->rate[i].count;
388 msta->rates[i].flags = sta_rates->rate[i].flags;
390 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
394 mt7615_mac_set_rates(dev, msta, NULL, msta->rates);
395 msta->rate_probe = false;
396 spin_unlock_bh(&dev->mt76.lock);
399 static void mt7615_tx(struct ieee80211_hw *hw,
400 struct ieee80211_tx_control *control,
403 struct mt7615_dev *dev = hw->priv;
404 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
405 struct ieee80211_vif *vif = info->control.vif;
406 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
409 struct mt7615_sta *sta;
411 sta = (struct mt7615_sta *)control->sta->drv_priv;
415 if (vif && !control->sta) {
416 struct mt7615_vif *mvif;
418 mvif = (struct mt7615_vif *)vif->drv_priv;
419 wcid = &mvif->sta.wcid;
422 mt76_tx(&dev->mt76, control->sta, wcid, skb);
425 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
427 struct mt7615_dev *dev = hw->priv;
429 mutex_lock(&dev->mt76.mutex);
430 mt7615_mcu_set_rts_thresh(dev, val);
431 mutex_unlock(&dev->mt76.mutex);
437 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
438 struct ieee80211_ampdu_params *params)
440 enum ieee80211_ampdu_mlme_action action = params->action;
441 struct mt7615_dev *dev = hw->priv;
442 struct ieee80211_sta *sta = params->sta;
443 struct ieee80211_txq *txq = sta->txq[params->tid];
444 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
445 u16 tid = params->tid;
446 u16 *ssn = ¶ms->ssn;
447 struct mt76_txq *mtxq;
452 mtxq = (struct mt76_txq *)txq->drv_priv;
455 case IEEE80211_AMPDU_RX_START:
456 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, *ssn,
458 mt7615_mcu_set_rx_ba(dev, params, 1);
460 case IEEE80211_AMPDU_RX_STOP:
461 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
462 mt7615_mcu_set_rx_ba(dev, params, 0);
464 case IEEE80211_AMPDU_TX_OPERATIONAL:
466 mtxq->send_bar = false;
467 mt7615_mcu_set_tx_ba(dev, params, 1);
469 case IEEE80211_AMPDU_TX_STOP_FLUSH:
470 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
472 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
473 mt7615_mcu_set_tx_ba(dev, params, 0);
475 case IEEE80211_AMPDU_TX_START:
476 mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(*ssn);
477 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
479 case IEEE80211_AMPDU_TX_STOP_CONT:
481 mt7615_mcu_set_tx_ba(dev, params, 0);
482 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
490 mt7615_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
493 struct mt7615_dev *dev = hw->priv;
495 set_bit(MT76_SCANNING, &dev->mt76.state);
499 mt7615_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
501 struct mt7615_dev *dev = hw->priv;
503 clear_bit(MT76_SCANNING, &dev->mt76.state);
506 const struct ieee80211_ops mt7615_ops = {
508 .start = mt7615_start,
510 .add_interface = mt7615_add_interface,
511 .remove_interface = mt7615_remove_interface,
512 .config = mt7615_config,
513 .conf_tx = mt7615_conf_tx,
514 .configure_filter = mt7615_configure_filter,
515 .bss_info_changed = mt7615_bss_info_changed,
516 .sta_state = mt76_sta_state,
517 .set_key = mt7615_set_key,
518 .ampdu_action = mt7615_ampdu_action,
519 .set_rts_threshold = mt7615_set_rts_threshold,
520 .wake_tx_queue = mt76_wake_tx_queue,
521 .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
522 .sw_scan_start = mt7615_sw_scan,
523 .sw_scan_complete = mt7615_sw_scan_complete,
524 .release_buffered_frames = mt76_release_buffered_frames,
525 .get_txpower = mt76_get_txpower,
526 .channel_switch_beacon = mt7615_channel_switch_beacon,