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  */
8 
9 #include <linux/etherdevice.h>
10 #include <linux/platform_device.h>
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include "mt7615.h"
14 
15 static int mt7615_start(struct ieee80211_hw *hw)
16 {
17 	struct mt7615_dev *dev = hw->priv;
18 
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);
22 
23 	return 0;
24 }
25 
26 static void mt7615_stop(struct ieee80211_hw *hw)
27 {
28 	struct mt7615_dev *dev = hw->priv;
29 
30 	clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
31 	cancel_delayed_work_sync(&dev->mt76.mac_work);
32 }
33 
34 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
35 {
36 	int i;
37 
38 	switch (type) {
39 	case NL80211_IFTYPE_AP:
40 		/* ap use hw bssid 0 and ext bssid */
41 		if (~mask & BIT(HW_BSSID_0))
42 			return HW_BSSID_0;
43 
44 		for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
45 			if (~mask & BIT(i))
46 				return i;
47 
48 		break;
49 	case NL80211_IFTYPE_STATION:
50 		/* sta use hw bssid other than 0 */
51 		for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
52 			if (~mask & BIT(i))
53 				return i;
54 
55 		break;
56 	default:
57 		WARN_ON(1);
58 		break;
59 	};
60 
61 	return -1;
62 }
63 
64 static int mt7615_add_interface(struct ieee80211_hw *hw,
65 				struct ieee80211_vif *vif)
66 {
67 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
68 	struct mt7615_dev *dev = hw->priv;
69 	struct mt76_txq *mtxq;
70 	int idx, ret = 0;
71 
72 	mutex_lock(&dev->mt76.mutex);
73 
74 	mvif->idx = ffs(~dev->vif_mask) - 1;
75 	if (mvif->idx >= MT7615_MAX_INTERFACES) {
76 		ret = -ENOSPC;
77 		goto out;
78 	}
79 
80 	mvif->omac_idx = get_omac_idx(vif->type, dev->omac_mask);
81 	if (mvif->omac_idx < 0) {
82 		ret = -ENOSPC;
83 		goto out;
84 	}
85 
86 	/* TODO: DBDC support. Use band 0 and wmm 0 for now */
87 	mvif->band_idx = 0;
88 	mvif->wmm_idx = 0;
89 
90 	ret = mt7615_mcu_set_dev_info(dev, vif, 1);
91 	if (ret)
92 		goto out;
93 
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;
99 
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);
104 
105 out:
106 	mutex_unlock(&dev->mt76.mutex);
107 
108 	return ret;
109 }
110 
111 static void mt7615_remove_interface(struct ieee80211_hw *hw,
112 				    struct ieee80211_vif *vif)
113 {
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;
117 
118 	/* TODO: disable beacon for the bss */
119 
120 	mt7615_mcu_set_dev_info(dev, vif, 0);
121 
122 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
123 	mt76_txq_remove(&dev->mt76, vif->txq);
124 
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);
129 }
130 
131 static int mt7615_set_channel(struct mt7615_dev *dev,
132 			      struct cfg80211_chan_def *def)
133 {
134 	int ret;
135 
136 	cancel_delayed_work_sync(&dev->mt76.mac_work);
137 	set_bit(MT76_RESET, &dev->mt76.state);
138 
139 	mt76_set_channel(&dev->mt76);
140 
141 	ret = mt7615_mcu_set_channel(dev);
142 	if (ret)
143 		return ret;
144 
145 	clear_bit(MT76_RESET, &dev->mt76.state);
146 
147 	mt76_txq_schedule_all(&dev->mt76);
148 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
149 				     MT7615_WATCHDOG_TIME);
150 	return 0;
151 }
152 
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)
156 {
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 :
160 				  &mvif->sta;
161 	struct mt76_wcid *wcid = &msta->wcid;
162 	int idx = key->keyidx;
163 
164 	/* The hardware does not support per-STA RX GTK, fallback
165 	 * to software mode for these.
166 	 */
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))
172 		return -EOPNOTSUPP;
173 
174 	if (cmd == SET_KEY) {
175 		key->hw_key_idx = wcid->idx;
176 		wcid->hw_key_idx = idx;
177 	} else {
178 		if (idx == wcid->hw_key_idx)
179 			wcid->hw_key_idx = -1;
180 
181 		key = NULL;
182 	}
183 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
184 
185 	return mt7615_mcu_set_wtbl_key(dev, wcid->idx, key, cmd);
186 }
187 
188 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
189 {
190 	struct mt7615_dev *dev = hw->priv;
191 	int ret = 0;
192 
193 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
194 		mutex_lock(&dev->mt76.mutex);
195 
196 		ieee80211_stop_queues(hw);
197 		ret = mt7615_set_channel(dev, &hw->conf.chandef);
198 		ieee80211_wake_queues(hw);
199 
200 		mutex_unlock(&dev->mt76.mutex);
201 	}
202 
203 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
204 		mutex_lock(&dev->mt76.mutex);
205 
206 		if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
207 			dev->mt76.rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
208 		else
209 			dev->mt76.rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
210 
211 		mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
212 
213 		mutex_unlock(&dev->mt76.mutex);
214 	}
215 	return ret;
216 }
217 
218 static int
219 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
220 	       const struct ieee80211_tx_queue_params *params)
221 {
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,
228 	};
229 
230 	/* TODO: hw wmm_set 1~3 */
231 	return mt7615_mcu_set_wmm(dev, wmm_queue_map[queue], params);
232 }
233 
234 static void mt7615_configure_filter(struct ieee80211_hw *hw,
235 				    unsigned int changed_flags,
236 				    unsigned int *total_flags,
237 				    u64 multicast)
238 {
239 	struct mt7615_dev *dev = hw->priv;
240 	u32 flags = 0;
241 
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);	\
246 	} while (0)
247 
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);
259 
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);
263 
264 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
265 
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);
270 
271 	*total_flags = flags;
272 	mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
273 }
274 
275 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
276 				    struct ieee80211_vif *vif,
277 				    struct ieee80211_bss_conf *info,
278 				    u32 changed)
279 {
280 	struct mt7615_dev *dev = hw->priv;
281 
282 	mutex_lock(&dev->mt76.mutex);
283 
284 	/* TODO: sta mode connect/disconnect
285 	 * BSS_CHANGED_ASSOC | BSS_CHANGED_BSSID
286 	 */
287 
288 	/* TODO: update beacon content
289 	 * BSS_CHANGED_BEACON
290 	 */
291 
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);
298 		} else {
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);
303 		}
304 	}
305 
306 	mutex_unlock(&dev->mt76.mutex);
307 }
308 
309 int mt7615_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
310 		   struct ieee80211_sta *sta)
311 {
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;
315 	int idx;
316 
317 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
318 	if (idx < 0)
319 		return -ENOSPC;
320 
321 	msta->vif = mvif;
322 	msta->wcid.sta = 1;
323 	msta->wcid.idx = idx;
324 
325 	mt7615_mcu_add_wtbl(dev, vif, sta);
326 	mt7615_mcu_set_sta_rec(dev, vif, sta, 1);
327 
328 	return 0;
329 }
330 
331 void mt7615_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
332 		      struct ieee80211_sta *sta)
333 {
334 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
335 
336 	if (sta->ht_cap.ht_supported)
337 		mt7615_mcu_set_ht_cap(dev, vif, sta);
338 }
339 
340 void mt7615_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
341 		       struct ieee80211_sta *sta)
342 {
343 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
344 
345 	mt7615_mcu_set_sta_rec(dev, vif, sta, 0);
346 	mt7615_mcu_del_wtbl(dev, vif, sta);
347 }
348 
349 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
350 				       struct ieee80211_vif *vif,
351 				       struct ieee80211_sta *sta)
352 {
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);
356 	int i;
357 
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;
363 
364 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
365 			break;
366 	}
367 	msta->n_rates = i;
368 	mt7615_mcu_set_rates(dev, msta, NULL, msta->rates);
369 	msta->rate_probe = false;
370 	spin_unlock_bh(&dev->mt76.lock);
371 }
372 
373 static void mt7615_tx(struct ieee80211_hw *hw,
374 		      struct ieee80211_tx_control *control,
375 		      struct sk_buff *skb)
376 {
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;
381 
382 	if (control->sta) {
383 		struct mt7615_sta *sta;
384 
385 		sta = (struct mt7615_sta *)control->sta->drv_priv;
386 		wcid = &sta->wcid;
387 	}
388 
389 	if (vif && !control->sta) {
390 		struct mt7615_vif *mvif;
391 
392 		mvif = (struct mt7615_vif *)vif->drv_priv;
393 		wcid = &mvif->sta.wcid;
394 	}
395 
396 	mt76_tx(&dev->mt76, control->sta, wcid, skb);
397 }
398 
399 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
400 {
401 	struct mt7615_dev *dev = hw->priv;
402 
403 	mutex_lock(&dev->mt76.mutex);
404 	mt7615_mcu_set_rts_thresh(dev, val);
405 	mutex_unlock(&dev->mt76.mutex);
406 
407 	return 0;
408 }
409 
410 static int
411 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
412 		    struct ieee80211_ampdu_params *params)
413 {
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 = &params->ssn;
421 	struct mt76_txq *mtxq;
422 
423 	if (!txq)
424 		return -EINVAL;
425 
426 	mtxq = (struct mt76_txq *)txq->drv_priv;
427 
428 	switch (action) {
429 	case IEEE80211_AMPDU_RX_START:
430 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, *ssn,
431 				   params->buf_size);
432 		mt7615_mcu_set_rx_ba(dev, params, 1);
433 		break;
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);
437 		break;
438 	case IEEE80211_AMPDU_TX_OPERATIONAL:
439 		mtxq->aggr = true;
440 		mtxq->send_bar = false;
441 		mt7615_mcu_set_tx_ba(dev, params, 1);
442 		break;
443 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
444 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
445 		mtxq->aggr = false;
446 		ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
447 		mt7615_mcu_set_tx_ba(dev, params, 0);
448 		break;
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);
452 		break;
453 	case IEEE80211_AMPDU_TX_STOP_CONT:
454 		mtxq->aggr = false;
455 		mt7615_mcu_set_tx_ba(dev, params, 0);
456 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
457 		break;
458 	}
459 
460 	return 0;
461 }
462 
463 static void
464 mt7615_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
465 	       const u8 *mac)
466 {
467 	struct mt7615_dev *dev = hw->priv;
468 
469 	set_bit(MT76_SCANNING, &dev->mt76.state);
470 }
471 
472 static void
473 mt7615_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
474 {
475 	struct mt7615_dev *dev = hw->priv;
476 
477 	clear_bit(MT76_SCANNING, &dev->mt76.state);
478 }
479 
480 const struct ieee80211_ops mt7615_ops = {
481 	.tx = mt7615_tx,
482 	.start = mt7615_start,
483 	.stop = mt7615_stop,
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,
499 };
500