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 	case NL80211_IFTYPE_MESH_POINT:
41 		/* ap use hw bssid 0 and ext bssid */
42 		if (~mask & BIT(HW_BSSID_0))
43 			return HW_BSSID_0;
44 
45 		for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
46 			if (~mask & BIT(i))
47 				return i;
48 
49 		break;
50 	case NL80211_IFTYPE_STATION:
51 		/* sta use hw bssid other than 0 */
52 		for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
53 			if (~mask & BIT(i))
54 				return i;
55 
56 		break;
57 	default:
58 		WARN_ON(1);
59 		break;
60 	};
61 
62 	return -1;
63 }
64 
65 static int mt7615_add_interface(struct ieee80211_hw *hw,
66 				struct ieee80211_vif *vif)
67 {
68 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
69 	struct mt7615_dev *dev = hw->priv;
70 	struct mt76_txq *mtxq;
71 	int idx, ret = 0;
72 
73 	mutex_lock(&dev->mt76.mutex);
74 
75 	mvif->idx = ffs(~dev->vif_mask) - 1;
76 	if (mvif->idx >= MT7615_MAX_INTERFACES) {
77 		ret = -ENOSPC;
78 		goto out;
79 	}
80 
81 	idx = get_omac_idx(vif->type, dev->omac_mask);
82 	if (idx < 0) {
83 		ret = -ENOSPC;
84 		goto out;
85 	}
86 	mvif->omac_idx = idx;
87 
88 	/* TODO: DBDC support. Use band 0 and wmm 0 for now */
89 	mvif->band_idx = 0;
90 	mvif->wmm_idx = 0;
91 
92 	ret = mt7615_mcu_set_dev_info(dev, vif, 1);
93 	if (ret)
94 		goto out;
95 
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;
101 
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);
106 
107 out:
108 	mutex_unlock(&dev->mt76.mutex);
109 
110 	return ret;
111 }
112 
113 static void mt7615_remove_interface(struct ieee80211_hw *hw,
114 				    struct ieee80211_vif *vif)
115 {
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;
119 
120 	/* TODO: disable beacon for the bss */
121 
122 	mt7615_mcu_set_dev_info(dev, vif, 0);
123 
124 	rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
125 	mt76_txq_remove(&dev->mt76, vif->txq);
126 
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);
131 }
132 
133 static int mt7615_set_channel(struct mt7615_dev *dev)
134 {
135 	int ret;
136 
137 	cancel_delayed_work_sync(&dev->mt76.mac_work);
138 	set_bit(MT76_RESET, &dev->mt76.state);
139 
140 	mt76_set_channel(&dev->mt76);
141 
142 	ret = mt7615_mcu_set_channel(dev);
143 	if (ret)
144 		return ret;
145 
146 	clear_bit(MT76_RESET, &dev->mt76.state);
147 
148 	mt76_txq_schedule_all(&dev->mt76);
149 	ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
150 				     MT7615_WATCHDOG_TIME);
151 	return 0;
152 }
153 
154 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
155 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
156 			  struct ieee80211_key_conf *key)
157 {
158 	struct mt7615_dev *dev = hw->priv;
159 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
160 	struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
161 				  &mvif->sta;
162 	struct mt76_wcid *wcid = &msta->wcid;
163 	int idx = key->keyidx;
164 
165 	/* The hardware does not support per-STA RX GTK, fallback
166 	 * to software mode for these.
167 	 */
168 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
169 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
170 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
171 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
172 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
173 		return -EOPNOTSUPP;
174 
175 	if (cmd == SET_KEY) {
176 		key->hw_key_idx = wcid->idx;
177 		wcid->hw_key_idx = idx;
178 	} else {
179 		if (idx == wcid->hw_key_idx)
180 			wcid->hw_key_idx = -1;
181 
182 		key = NULL;
183 	}
184 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
185 
186 	return mt7615_mcu_set_wtbl_key(dev, wcid->idx, key, cmd);
187 }
188 
189 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
190 {
191 	struct mt7615_dev *dev = hw->priv;
192 	int ret = 0;
193 
194 	mutex_lock(&dev->mt76.mutex);
195 
196 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
197 		ieee80211_stop_queues(hw);
198 		ret = mt7615_set_channel(dev);
199 		ieee80211_wake_queues(hw);
200 	}
201 
202 	if (changed & IEEE80211_CONF_CHANGE_POWER)
203 		ret = mt7615_mcu_set_tx_power(dev);
204 
205 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
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 
214 	mutex_unlock(&dev->mt76.mutex);
215 
216 	return ret;
217 }
218 
219 static int
220 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
221 	       const struct ieee80211_tx_queue_params *params)
222 {
223 	struct mt7615_dev *dev = hw->priv;
224 	static const u8 wmm_queue_map[] = {
225 		[IEEE80211_AC_BK] = 0,
226 		[IEEE80211_AC_BE] = 1,
227 		[IEEE80211_AC_VI] = 2,
228 		[IEEE80211_AC_VO] = 3,
229 	};
230 
231 	/* TODO: hw wmm_set 1~3 */
232 	return mt7615_mcu_set_wmm(dev, wmm_queue_map[queue], params);
233 }
234 
235 static void mt7615_configure_filter(struct ieee80211_hw *hw,
236 				    unsigned int changed_flags,
237 				    unsigned int *total_flags,
238 				    u64 multicast)
239 {
240 	struct mt7615_dev *dev = hw->priv;
241 	u32 flags = 0;
242 
243 #define MT76_FILTER(_flag, _hw) do { \
244 		flags |= *total_flags & FIF_##_flag;			\
245 		dev->mt76.rxfilter &= ~(_hw);				\
246 		dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
247 	} while (0)
248 
249 	dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
250 				MT_WF_RFCR_DROP_OTHER_BEACON |
251 				MT_WF_RFCR_DROP_FRAME_REPORT |
252 				MT_WF_RFCR_DROP_PROBEREQ |
253 				MT_WF_RFCR_DROP_MCAST_FILTERED |
254 				MT_WF_RFCR_DROP_MCAST |
255 				MT_WF_RFCR_DROP_BCAST |
256 				MT_WF_RFCR_DROP_DUPLICATE |
257 				MT_WF_RFCR_DROP_A2_BSSID |
258 				MT_WF_RFCR_DROP_UNWANTED_CTL |
259 				MT_WF_RFCR_DROP_STBC_MULTI);
260 
261 	MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
262 			       MT_WF_RFCR_DROP_A3_MAC |
263 			       MT_WF_RFCR_DROP_A3_BSSID);
264 
265 	MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
266 
267 	MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
268 			     MT_WF_RFCR_DROP_RTS |
269 			     MT_WF_RFCR_DROP_CTL_RSV |
270 			     MT_WF_RFCR_DROP_NDPA);
271 
272 	*total_flags = flags;
273 	mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
274 }
275 
276 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
277 				    struct ieee80211_vif *vif,
278 				    struct ieee80211_bss_conf *info,
279 				    u32 changed)
280 {
281 	struct mt7615_dev *dev = hw->priv;
282 
283 	mutex_lock(&dev->mt76.mutex);
284 
285 	if (changed & BSS_CHANGED_ASSOC)
286 		mt7615_mcu_set_bss_info(dev, vif, info->assoc);
287 
288 	/* TODO: update beacon content
289 	 * BSS_CHANGED_BEACON
290 	 */
291 
292 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
293 		mt7615_mcu_set_bss_info(dev, vif, info->enable_beacon);
294 		mt7615_mcu_wtbl_bmc(dev, vif, info->enable_beacon);
295 		mt7615_mcu_set_sta_rec_bmc(dev, vif, info->enable_beacon);
296 		mt7615_mcu_set_bcn(dev, vif, info->enable_beacon);
297 	}
298 
299 	mutex_unlock(&dev->mt76.mutex);
300 }
301 
302 int mt7615_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
303 		   struct ieee80211_sta *sta)
304 {
305 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
306 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
307 	struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
308 	int idx;
309 
310 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
311 	if (idx < 0)
312 		return -ENOSPC;
313 
314 	msta->vif = mvif;
315 	msta->wcid.sta = 1;
316 	msta->wcid.idx = idx;
317 
318 	mt7615_mcu_add_wtbl(dev, vif, sta);
319 	mt7615_mcu_set_sta_rec(dev, vif, sta, 1);
320 
321 	return 0;
322 }
323 
324 void mt7615_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
325 		      struct ieee80211_sta *sta)
326 {
327 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
328 
329 	if (sta->ht_cap.ht_supported)
330 		mt7615_mcu_set_ht_cap(dev, vif, sta);
331 }
332 
333 void mt7615_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
334 		       struct ieee80211_sta *sta)
335 {
336 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
337 
338 	mt7615_mcu_set_sta_rec(dev, vif, sta, 0);
339 	mt7615_mcu_del_wtbl(dev, sta);
340 }
341 
342 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
343 				       struct ieee80211_vif *vif,
344 				       struct ieee80211_sta *sta)
345 {
346 	struct mt7615_dev *dev = hw->priv;
347 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
348 	struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
349 	int i;
350 
351 	spin_lock_bh(&dev->mt76.lock);
352 	for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
353 		msta->rates[i].idx = sta_rates->rate[i].idx;
354 		msta->rates[i].count = sta_rates->rate[i].count;
355 		msta->rates[i].flags = sta_rates->rate[i].flags;
356 
357 		if (msta->rates[i].idx < 0 || !msta->rates[i].count)
358 			break;
359 	}
360 	msta->n_rates = i;
361 	mt7615_mcu_set_rates(dev, msta, NULL, msta->rates);
362 	msta->rate_probe = false;
363 	spin_unlock_bh(&dev->mt76.lock);
364 }
365 
366 static void mt7615_tx(struct ieee80211_hw *hw,
367 		      struct ieee80211_tx_control *control,
368 		      struct sk_buff *skb)
369 {
370 	struct mt7615_dev *dev = hw->priv;
371 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
372 	struct ieee80211_vif *vif = info->control.vif;
373 	struct mt76_wcid *wcid = &dev->mt76.global_wcid;
374 
375 	if (control->sta) {
376 		struct mt7615_sta *sta;
377 
378 		sta = (struct mt7615_sta *)control->sta->drv_priv;
379 		wcid = &sta->wcid;
380 	}
381 
382 	if (vif && !control->sta) {
383 		struct mt7615_vif *mvif;
384 
385 		mvif = (struct mt7615_vif *)vif->drv_priv;
386 		wcid = &mvif->sta.wcid;
387 	}
388 
389 	mt76_tx(&dev->mt76, control->sta, wcid, skb);
390 }
391 
392 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
393 {
394 	struct mt7615_dev *dev = hw->priv;
395 
396 	mutex_lock(&dev->mt76.mutex);
397 	mt7615_mcu_set_rts_thresh(dev, val);
398 	mutex_unlock(&dev->mt76.mutex);
399 
400 	return 0;
401 }
402 
403 static int
404 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
405 		    struct ieee80211_ampdu_params *params)
406 {
407 	enum ieee80211_ampdu_mlme_action action = params->action;
408 	struct mt7615_dev *dev = hw->priv;
409 	struct ieee80211_sta *sta = params->sta;
410 	struct ieee80211_txq *txq = sta->txq[params->tid];
411 	struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
412 	u16 tid = params->tid;
413 	u16 *ssn = &params->ssn;
414 	struct mt76_txq *mtxq;
415 
416 	if (!txq)
417 		return -EINVAL;
418 
419 	mtxq = (struct mt76_txq *)txq->drv_priv;
420 
421 	switch (action) {
422 	case IEEE80211_AMPDU_RX_START:
423 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, *ssn,
424 				   params->buf_size);
425 		mt7615_mcu_set_rx_ba(dev, params, 1);
426 		break;
427 	case IEEE80211_AMPDU_RX_STOP:
428 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
429 		mt7615_mcu_set_rx_ba(dev, params, 0);
430 		break;
431 	case IEEE80211_AMPDU_TX_OPERATIONAL:
432 		mtxq->aggr = true;
433 		mtxq->send_bar = false;
434 		mt7615_mcu_set_tx_ba(dev, params, 1);
435 		break;
436 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
437 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
438 		mtxq->aggr = false;
439 		ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
440 		mt7615_mcu_set_tx_ba(dev, params, 0);
441 		break;
442 	case IEEE80211_AMPDU_TX_START:
443 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(*ssn);
444 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
445 		break;
446 	case IEEE80211_AMPDU_TX_STOP_CONT:
447 		mtxq->aggr = false;
448 		mt7615_mcu_set_tx_ba(dev, params, 0);
449 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
450 		break;
451 	}
452 
453 	return 0;
454 }
455 
456 static void
457 mt7615_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
458 	       const u8 *mac)
459 {
460 	struct mt7615_dev *dev = hw->priv;
461 
462 	set_bit(MT76_SCANNING, &dev->mt76.state);
463 }
464 
465 static void
466 mt7615_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
467 {
468 	struct mt7615_dev *dev = hw->priv;
469 
470 	clear_bit(MT76_SCANNING, &dev->mt76.state);
471 }
472 
473 const struct ieee80211_ops mt7615_ops = {
474 	.tx = mt7615_tx,
475 	.start = mt7615_start,
476 	.stop = mt7615_stop,
477 	.add_interface = mt7615_add_interface,
478 	.remove_interface = mt7615_remove_interface,
479 	.config = mt7615_config,
480 	.conf_tx = mt7615_conf_tx,
481 	.configure_filter = mt7615_configure_filter,
482 	.bss_info_changed = mt7615_bss_info_changed,
483 	.sta_state = mt76_sta_state,
484 	.set_key = mt7615_set_key,
485 	.ampdu_action = mt7615_ampdu_action,
486 	.set_rts_threshold = mt7615_set_rts_threshold,
487 	.wake_tx_queue = mt76_wake_tx_queue,
488 	.sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
489 	.sw_scan_start = mt7615_sw_scan,
490 	.sw_scan_complete = mt7615_sw_scan_complete,
491 	.release_buffered_frames = mt76_release_buffered_frames,
492 	.get_txpower = mt76_get_txpower,
493 };
494