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 /* ap use hw bssid 0 and ext bssid */
41 if (~mask
& BIT(HW_BSSID_0
))
44 for (i
= EXT_BSSID_1
; i
< EXT_BSSID_END
; i
++)
49 case NL80211_IFTYPE_STATION
:
50 /* sta use hw bssid other than 0 */
51 for (i
= HW_BSSID_1
; i
< HW_BSSID_MAX
; i
++)
64 static int mt7615_add_interface(struct ieee80211_hw
*hw
,
65 struct ieee80211_vif
*vif
)
67 struct mt7615_vif
*mvif
= (struct mt7615_vif
*)vif
->drv_priv
;
68 struct mt7615_dev
*dev
= hw
->priv
;
69 struct mt76_txq
*mtxq
;
72 mutex_lock(&dev
->mt76
.mutex
);
74 mvif
->idx
= ffs(~dev
->vif_mask
) - 1;
75 if (mvif
->idx
>= MT7615_MAX_INTERFACES
) {
80 mvif
->omac_idx
= get_omac_idx(vif
->type
, dev
->omac_mask
);
81 if (mvif
->omac_idx
< 0) {
86 /* TODO: DBDC support. Use band 0 and wmm 0 for now */
90 ret
= mt7615_mcu_set_dev_info(dev
, vif
, 1);
94 dev
->vif_mask
|= BIT(mvif
->idx
);
95 dev
->omac_mask
|= BIT(mvif
->omac_idx
);
96 idx
= MT7615_WTBL_RESERVED
- 1 - mvif
->idx
;
97 mvif
->sta
.wcid
.idx
= idx
;
98 mvif
->sta
.wcid
.hw_key_idx
= -1;
100 rcu_assign_pointer(dev
->mt76
.wcid
[idx
], &mvif
->sta
.wcid
);
101 mtxq
= (struct mt76_txq
*)vif
->txq
->drv_priv
;
102 mtxq
->wcid
= &mvif
->sta
.wcid
;
103 mt76_txq_init(&dev
->mt76
, vif
->txq
);
106 mutex_unlock(&dev
->mt76
.mutex
);
111 static void mt7615_remove_interface(struct ieee80211_hw
*hw
,
112 struct ieee80211_vif
*vif
)
114 struct mt7615_vif
*mvif
= (struct mt7615_vif
*)vif
->drv_priv
;
115 struct mt7615_dev
*dev
= hw
->priv
;
116 int idx
= mvif
->sta
.wcid
.idx
;
118 /* TODO: disable beacon for the bss */
120 mt7615_mcu_set_dev_info(dev
, vif
, 0);
122 rcu_assign_pointer(dev
->mt76
.wcid
[idx
], NULL
);
123 mt76_txq_remove(&dev
->mt76
, vif
->txq
);
125 mutex_lock(&dev
->mt76
.mutex
);
126 dev
->vif_mask
&= ~BIT(mvif
->idx
);
127 dev
->omac_mask
&= ~BIT(mvif
->omac_idx
);
128 mutex_unlock(&dev
->mt76
.mutex
);
131 static int mt7615_set_channel(struct mt7615_dev
*dev
,
132 struct cfg80211_chan_def
*def
)
136 cancel_delayed_work_sync(&dev
->mt76
.mac_work
);
137 set_bit(MT76_RESET
, &dev
->mt76
.state
);
139 mt76_set_channel(&dev
->mt76
);
141 ret
= mt7615_mcu_set_channel(dev
);
145 clear_bit(MT76_RESET
, &dev
->mt76
.state
);
147 mt76_txq_schedule_all(&dev
->mt76
);
148 ieee80211_queue_delayed_work(mt76_hw(dev
), &dev
->mt76
.mac_work
,
149 MT7615_WATCHDOG_TIME
);
153 static int mt7615_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
154 struct ieee80211_vif
*vif
, struct ieee80211_sta
*sta
,
155 struct ieee80211_key_conf
*key
)
157 struct mt7615_dev
*dev
= hw
->priv
;
158 struct mt7615_vif
*mvif
= (struct mt7615_vif
*)vif
->drv_priv
;
159 struct mt7615_sta
*msta
= sta
? (struct mt7615_sta
*)sta
->drv_priv
:
161 struct mt76_wcid
*wcid
= &msta
->wcid
;
162 int idx
= key
->keyidx
;
164 /* The hardware does not support per-STA RX GTK, fallback
165 * to software mode for these.
167 if ((vif
->type
== NL80211_IFTYPE_ADHOC
||
168 vif
->type
== NL80211_IFTYPE_MESH_POINT
) &&
169 (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
||
170 key
->cipher
== WLAN_CIPHER_SUITE_CCMP
) &&
171 !(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
174 if (cmd
== SET_KEY
) {
175 key
->hw_key_idx
= wcid
->idx
;
176 wcid
->hw_key_idx
= idx
;
178 if (idx
== wcid
->hw_key_idx
)
179 wcid
->hw_key_idx
= -1;
183 mt76_wcid_key_setup(&dev
->mt76
, wcid
, key
);
185 return mt7615_mcu_set_wtbl_key(dev
, wcid
->idx
, key
, cmd
);
188 static int mt7615_config(struct ieee80211_hw
*hw
, u32 changed
)
190 struct mt7615_dev
*dev
= hw
->priv
;
193 if (changed
& IEEE80211_CONF_CHANGE_CHANNEL
) {
194 mutex_lock(&dev
->mt76
.mutex
);
196 ieee80211_stop_queues(hw
);
197 ret
= mt7615_set_channel(dev
, &hw
->conf
.chandef
);
198 ieee80211_wake_queues(hw
);
200 mutex_unlock(&dev
->mt76
.mutex
);
203 if (changed
& IEEE80211_CONF_CHANGE_MONITOR
) {
204 mutex_lock(&dev
->mt76
.mutex
);
206 if (!(hw
->conf
.flags
& IEEE80211_CONF_MONITOR
))
207 dev
->mt76
.rxfilter
|= MT_WF_RFCR_DROP_OTHER_UC
;
209 dev
->mt76
.rxfilter
&= ~MT_WF_RFCR_DROP_OTHER_UC
;
211 mt76_wr(dev
, MT_WF_RFCR
, dev
->mt76
.rxfilter
);
213 mutex_unlock(&dev
->mt76
.mutex
);
219 mt7615_conf_tx(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
, u16 queue
,
220 const struct ieee80211_tx_queue_params
*params
)
222 struct mt7615_dev
*dev
= hw
->priv
;
223 static const u8 wmm_queue_map
[] = {
224 [IEEE80211_AC_BK
] = 0,
225 [IEEE80211_AC_BE
] = 1,
226 [IEEE80211_AC_VI
] = 2,
227 [IEEE80211_AC_VO
] = 3,
230 /* TODO: hw wmm_set 1~3 */
231 return mt7615_mcu_set_wmm(dev
, wmm_queue_map
[queue
], params
);
234 static void mt7615_configure_filter(struct ieee80211_hw
*hw
,
235 unsigned int changed_flags
,
236 unsigned int *total_flags
,
239 struct mt7615_dev
*dev
= hw
->priv
;
242 #define MT76_FILTER(_flag, _hw) do { \
243 flags |= *total_flags & FIF_##_flag; \
244 dev->mt76.rxfilter &= ~(_hw); \
245 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
248 dev
->mt76
.rxfilter
&= ~(MT_WF_RFCR_DROP_OTHER_BSS
|
249 MT_WF_RFCR_DROP_OTHER_BEACON
|
250 MT_WF_RFCR_DROP_FRAME_REPORT
|
251 MT_WF_RFCR_DROP_PROBEREQ
|
252 MT_WF_RFCR_DROP_MCAST_FILTERED
|
253 MT_WF_RFCR_DROP_MCAST
|
254 MT_WF_RFCR_DROP_BCAST
|
255 MT_WF_RFCR_DROP_DUPLICATE
|
256 MT_WF_RFCR_DROP_A2_BSSID
|
257 MT_WF_RFCR_DROP_UNWANTED_CTL
|
258 MT_WF_RFCR_DROP_STBC_MULTI
);
260 MT76_FILTER(OTHER_BSS
, MT_WF_RFCR_DROP_OTHER_TIM
|
261 MT_WF_RFCR_DROP_A3_MAC
|
262 MT_WF_RFCR_DROP_A3_BSSID
);
264 MT76_FILTER(FCSFAIL
, MT_WF_RFCR_DROP_FCSFAIL
);
266 MT76_FILTER(CONTROL
, MT_WF_RFCR_DROP_CTS
|
267 MT_WF_RFCR_DROP_RTS
|
268 MT_WF_RFCR_DROP_CTL_RSV
|
269 MT_WF_RFCR_DROP_NDPA
);
271 *total_flags
= flags
;
272 mt76_wr(dev
, MT_WF_RFCR
, dev
->mt76
.rxfilter
);
275 static void mt7615_bss_info_changed(struct ieee80211_hw
*hw
,
276 struct ieee80211_vif
*vif
,
277 struct ieee80211_bss_conf
*info
,
280 struct mt7615_dev
*dev
= hw
->priv
;
282 mutex_lock(&dev
->mt76
.mutex
);
284 /* TODO: sta mode connect/disconnect
285 * BSS_CHANGED_ASSOC | BSS_CHANGED_BSSID
288 /* TODO: update beacon content
292 if (changed
& BSS_CHANGED_BEACON_ENABLED
) {
293 if (info
->enable_beacon
) {
294 mt7615_mcu_set_bss_info(dev
, vif
, 1);
295 mt7615_mcu_add_wtbl_bmc(dev
, vif
);
296 mt7615_mcu_set_sta_rec_bmc(dev
, vif
, 1);
297 mt7615_mcu_set_bcn(dev
, vif
, 1);
299 mt7615_mcu_set_sta_rec_bmc(dev
, vif
, 0);
300 mt7615_mcu_del_wtbl_bmc(dev
, vif
);
301 mt7615_mcu_set_bss_info(dev
, vif
, 0);
302 mt7615_mcu_set_bcn(dev
, vif
, 0);
306 mutex_unlock(&dev
->mt76
.mutex
);
309 int mt7615_sta_add(struct mt76_dev
*mdev
, struct ieee80211_vif
*vif
,
310 struct ieee80211_sta
*sta
)
312 struct mt7615_dev
*dev
= container_of(mdev
, struct mt7615_dev
, mt76
);
313 struct mt7615_sta
*msta
= (struct mt7615_sta
*)sta
->drv_priv
;
314 struct mt7615_vif
*mvif
= (struct mt7615_vif
*)vif
->drv_priv
;
317 idx
= mt76_wcid_alloc(dev
->mt76
.wcid_mask
, MT7615_WTBL_STA
- 1);
323 msta
->wcid
.idx
= idx
;
325 mt7615_mcu_add_wtbl(dev
, vif
, sta
);
326 mt7615_mcu_set_sta_rec(dev
, vif
, sta
, 1);
331 void mt7615_sta_assoc(struct mt76_dev
*mdev
, struct ieee80211_vif
*vif
,
332 struct ieee80211_sta
*sta
)
334 struct mt7615_dev
*dev
= container_of(mdev
, struct mt7615_dev
, mt76
);
336 if (sta
->ht_cap
.ht_supported
)
337 mt7615_mcu_set_ht_cap(dev
, vif
, sta
);
340 void mt7615_sta_remove(struct mt76_dev
*mdev
, struct ieee80211_vif
*vif
,
341 struct ieee80211_sta
*sta
)
343 struct mt7615_dev
*dev
= container_of(mdev
, struct mt7615_dev
, mt76
);
345 mt7615_mcu_set_sta_rec(dev
, vif
, sta
, 0);
346 mt7615_mcu_del_wtbl(dev
, vif
, sta
);
349 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw
*hw
,
350 struct ieee80211_vif
*vif
,
351 struct ieee80211_sta
*sta
)
353 struct mt7615_dev
*dev
= hw
->priv
;
354 struct mt7615_sta
*msta
= (struct mt7615_sta
*)sta
->drv_priv
;
355 struct ieee80211_sta_rates
*sta_rates
= rcu_dereference(sta
->rates
);
358 spin_lock_bh(&dev
->mt76
.lock
);
359 for (i
= 0; i
< ARRAY_SIZE(msta
->rates
); i
++) {
360 msta
->rates
[i
].idx
= sta_rates
->rate
[i
].idx
;
361 msta
->rates
[i
].count
= sta_rates
->rate
[i
].count
;
362 msta
->rates
[i
].flags
= sta_rates
->rate
[i
].flags
;
364 if (msta
->rates
[i
].idx
< 0 || !msta
->rates
[i
].count
)
368 mt7615_mcu_set_rates(dev
, msta
, NULL
, msta
->rates
);
369 msta
->rate_probe
= false;
370 spin_unlock_bh(&dev
->mt76
.lock
);
373 static void mt7615_tx(struct ieee80211_hw
*hw
,
374 struct ieee80211_tx_control
*control
,
377 struct mt7615_dev
*dev
= hw
->priv
;
378 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
379 struct ieee80211_vif
*vif
= info
->control
.vif
;
380 struct mt76_wcid
*wcid
= &dev
->mt76
.global_wcid
;
383 struct mt7615_sta
*sta
;
385 sta
= (struct mt7615_sta
*)control
->sta
->drv_priv
;
389 if (vif
&& !control
->sta
) {
390 struct mt7615_vif
*mvif
;
392 mvif
= (struct mt7615_vif
*)vif
->drv_priv
;
393 wcid
= &mvif
->sta
.wcid
;
396 mt76_tx(&dev
->mt76
, control
->sta
, wcid
, skb
);
399 static int mt7615_set_rts_threshold(struct ieee80211_hw
*hw
, u32 val
)
401 struct mt7615_dev
*dev
= hw
->priv
;
403 mutex_lock(&dev
->mt76
.mutex
);
404 mt7615_mcu_set_rts_thresh(dev
, val
);
405 mutex_unlock(&dev
->mt76
.mutex
);
411 mt7615_ampdu_action(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
412 struct ieee80211_ampdu_params
*params
)
414 enum ieee80211_ampdu_mlme_action action
= params
->action
;
415 struct mt7615_dev
*dev
= hw
->priv
;
416 struct ieee80211_sta
*sta
= params
->sta
;
417 struct ieee80211_txq
*txq
= sta
->txq
[params
->tid
];
418 struct mt7615_sta
*msta
= (struct mt7615_sta
*)sta
->drv_priv
;
419 u16 tid
= params
->tid
;
420 u16
*ssn
= ¶ms
->ssn
;
421 struct mt76_txq
*mtxq
;
426 mtxq
= (struct mt76_txq
*)txq
->drv_priv
;
429 case IEEE80211_AMPDU_RX_START
:
430 mt76_rx_aggr_start(&dev
->mt76
, &msta
->wcid
, tid
, *ssn
,
432 mt7615_mcu_set_rx_ba(dev
, params
, 1);
434 case IEEE80211_AMPDU_RX_STOP
:
435 mt76_rx_aggr_stop(&dev
->mt76
, &msta
->wcid
, tid
);
436 mt7615_mcu_set_rx_ba(dev
, params
, 0);
438 case IEEE80211_AMPDU_TX_OPERATIONAL
:
440 mtxq
->send_bar
= false;
441 mt7615_mcu_set_tx_ba(dev
, params
, 1);
443 case IEEE80211_AMPDU_TX_STOP_FLUSH
:
444 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
:
446 ieee80211_send_bar(vif
, sta
->addr
, tid
, mtxq
->agg_ssn
);
447 mt7615_mcu_set_tx_ba(dev
, params
, 0);
449 case IEEE80211_AMPDU_TX_START
:
450 mtxq
->agg_ssn
= IEEE80211_SN_TO_SEQ(*ssn
);
451 ieee80211_start_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
453 case IEEE80211_AMPDU_TX_STOP_CONT
:
455 mt7615_mcu_set_tx_ba(dev
, params
, 0);
456 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
464 mt7615_sw_scan(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
467 struct mt7615_dev
*dev
= hw
->priv
;
469 set_bit(MT76_SCANNING
, &dev
->mt76
.state
);
473 mt7615_sw_scan_complete(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
475 struct mt7615_dev
*dev
= hw
->priv
;
477 clear_bit(MT76_SCANNING
, &dev
->mt76
.state
);
480 const struct ieee80211_ops mt7615_ops
= {
482 .start
= mt7615_start
,
484 .add_interface
= mt7615_add_interface
,
485 .remove_interface
= mt7615_remove_interface
,
486 .config
= mt7615_config
,
487 .conf_tx
= mt7615_conf_tx
,
488 .configure_filter
= mt7615_configure_filter
,
489 .bss_info_changed
= mt7615_bss_info_changed
,
490 .sta_state
= mt76_sta_state
,
491 .set_key
= mt7615_set_key
,
492 .ampdu_action
= mt7615_ampdu_action
,
493 .set_rts_threshold
= mt7615_set_rts_threshold
,
494 .wake_tx_queue
= mt76_wake_tx_queue
,
495 .sta_rate_tbl_update
= mt7615_sta_rate_tbl_update
,
496 .sw_scan_start
= mt7615_sw_scan
,
497 .sw_scan_complete
= mt7615_sw_scan_complete
,
498 .release_buffered_frames
= mt76_release_buffered_frames
,