1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
4  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
5  */
6 
7 #include <linux/module.h>
8 #include "mt76x02.h"
9 
10 #define CCK_RATE(_idx, _rate) {					\
11 	.bitrate = _rate,					\
12 	.flags = IEEE80211_RATE_SHORT_PREAMBLE,			\
13 	.hw_value = (MT_PHY_TYPE_CCK << 8) | (_idx),		\
14 	.hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + (_idx)),	\
15 }
16 
17 #define OFDM_RATE(_idx, _rate) {				\
18 	.bitrate = _rate,					\
19 	.hw_value = (MT_PHY_TYPE_OFDM << 8) | (_idx),		\
20 	.hw_value_short = (MT_PHY_TYPE_OFDM << 8) | (_idx),	\
21 }
22 
23 struct ieee80211_rate mt76x02_rates[] = {
24 	CCK_RATE(0, 10),
25 	CCK_RATE(1, 20),
26 	CCK_RATE(2, 55),
27 	CCK_RATE(3, 110),
28 	OFDM_RATE(0, 60),
29 	OFDM_RATE(1, 90),
30 	OFDM_RATE(2, 120),
31 	OFDM_RATE(3, 180),
32 	OFDM_RATE(4, 240),
33 	OFDM_RATE(5, 360),
34 	OFDM_RATE(6, 480),
35 	OFDM_RATE(7, 540),
36 };
37 EXPORT_SYMBOL_GPL(mt76x02_rates);
38 
39 static const struct ieee80211_iface_limit mt76x02_if_limits[] = {
40 	{
41 		.max = 1,
42 		.types = BIT(NL80211_IFTYPE_ADHOC)
43 	}, {
44 		.max = 8,
45 		.types = BIT(NL80211_IFTYPE_STATION) |
46 #ifdef CONFIG_MAC80211_MESH
47 			 BIT(NL80211_IFTYPE_MESH_POINT) |
48 #endif
49 			 BIT(NL80211_IFTYPE_AP)
50 	 },
51 };
52 
53 static const struct ieee80211_iface_limit mt76x02u_if_limits[] = {
54 	{
55 		.max = 1,
56 		.types = BIT(NL80211_IFTYPE_ADHOC)
57 	}, {
58 		.max = 2,
59 		.types = BIT(NL80211_IFTYPE_STATION) |
60 #ifdef CONFIG_MAC80211_MESH
61 			 BIT(NL80211_IFTYPE_MESH_POINT) |
62 #endif
63 			 BIT(NL80211_IFTYPE_AP)
64 	},
65 };
66 
67 static const struct ieee80211_iface_combination mt76x02_if_comb[] = {
68 	{
69 		.limits = mt76x02_if_limits,
70 		.n_limits = ARRAY_SIZE(mt76x02_if_limits),
71 		.max_interfaces = 8,
72 		.num_different_channels = 1,
73 		.beacon_int_infra_match = true,
74 		.radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
75 				       BIT(NL80211_CHAN_WIDTH_20) |
76 				       BIT(NL80211_CHAN_WIDTH_40) |
77 				       BIT(NL80211_CHAN_WIDTH_80),
78 	}
79 };
80 
81 static const struct ieee80211_iface_combination mt76x02u_if_comb[] = {
82 	{
83 		.limits = mt76x02u_if_limits,
84 		.n_limits = ARRAY_SIZE(mt76x02u_if_limits),
85 		.max_interfaces = 2,
86 		.num_different_channels = 1,
87 		.beacon_int_infra_match = true,
88 	}
89 };
90 
91 static void
92 mt76x02_led_set_config(struct mt76_dev *mdev, u8 delay_on,
93 		       u8 delay_off)
94 {
95 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev,
96 					       mt76);
97 	u32 val;
98 
99 	val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xff) |
100 	      FIELD_PREP(MT_LED_STATUS_OFF, delay_off) |
101 	      FIELD_PREP(MT_LED_STATUS_ON, delay_on);
102 
103 	mt76_wr(dev, MT_LED_S0(mdev->led_pin), val);
104 	mt76_wr(dev, MT_LED_S1(mdev->led_pin), val);
105 
106 	val = MT_LED_CTRL_REPLAY(mdev->led_pin) |
107 	      MT_LED_CTRL_KICK(mdev->led_pin);
108 	if (mdev->led_al)
109 		val |= MT_LED_CTRL_POLARITY(mdev->led_pin);
110 	mt76_wr(dev, MT_LED_CTRL, val);
111 }
112 
113 static int
114 mt76x02_led_set_blink(struct led_classdev *led_cdev,
115 		      unsigned long *delay_on,
116 		      unsigned long *delay_off)
117 {
118 	struct mt76_dev *mdev = container_of(led_cdev, struct mt76_dev,
119 					     led_cdev);
120 	u8 delta_on, delta_off;
121 
122 	delta_off = max_t(u8, *delay_off / 10, 1);
123 	delta_on = max_t(u8, *delay_on / 10, 1);
124 
125 	mt76x02_led_set_config(mdev, delta_on, delta_off);
126 
127 	return 0;
128 }
129 
130 static void
131 mt76x02_led_set_brightness(struct led_classdev *led_cdev,
132 			   enum led_brightness brightness)
133 {
134 	struct mt76_dev *mdev = container_of(led_cdev, struct mt76_dev,
135 					     led_cdev);
136 
137 	if (!brightness)
138 		mt76x02_led_set_config(mdev, 0, 0xff);
139 	else
140 		mt76x02_led_set_config(mdev, 0xff, 0);
141 }
142 
143 void mt76x02_init_device(struct mt76x02_dev *dev)
144 {
145 	struct ieee80211_hw *hw = mt76_hw(dev);
146 	struct wiphy *wiphy = hw->wiphy;
147 
148 	INIT_DELAYED_WORK(&dev->mt76.mac_work, mt76x02_mac_work);
149 
150 	hw->queues = 4;
151 	hw->max_rates = 1;
152 	hw->max_report_rates = 7;
153 	hw->max_rate_tries = 1;
154 	hw->extra_tx_headroom = 2;
155 
156 	if (mt76_is_usb(&dev->mt76)) {
157 		hw->extra_tx_headroom += sizeof(struct mt76x02_txwi) +
158 					 MT_DMA_HDR_LEN;
159 		wiphy->iface_combinations = mt76x02u_if_comb;
160 		wiphy->n_iface_combinations = ARRAY_SIZE(mt76x02u_if_comb);
161 	} else {
162 		INIT_DELAYED_WORK(&dev->wdt_work, mt76x02_wdt_work);
163 
164 		mt76x02_dfs_init_detector(dev);
165 
166 		wiphy->reg_notifier = mt76x02_regd_notifier;
167 		wiphy->iface_combinations = mt76x02_if_comb;
168 		wiphy->n_iface_combinations = ARRAY_SIZE(mt76x02_if_comb);
169 
170 		/* init led callbacks */
171 		if (IS_ENABLED(CONFIG_MT76_LEDS)) {
172 			dev->mt76.led_cdev.brightness_set =
173 					mt76x02_led_set_brightness;
174 			dev->mt76.led_cdev.blink_set = mt76x02_led_set_blink;
175 		}
176 	}
177 
178 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
179 
180 	hw->sta_data_size = sizeof(struct mt76x02_sta);
181 	hw->vif_data_size = sizeof(struct mt76x02_vif);
182 
183 	ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
184 	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
185 
186 	dev->mt76.global_wcid.idx = 255;
187 	dev->mt76.global_wcid.hw_key_idx = -1;
188 	dev->slottime = 9;
189 
190 	if (is_mt76x2(dev)) {
191 		dev->mphy.sband_2g.sband.ht_cap.cap |=
192 				IEEE80211_HT_CAP_LDPC_CODING;
193 		dev->mphy.sband_5g.sband.ht_cap.cap |=
194 				IEEE80211_HT_CAP_LDPC_CODING;
195 		dev->chainmask = 0x202;
196 		dev->mphy.antenna_mask = 3;
197 	} else {
198 		dev->chainmask = 0x101;
199 		dev->mphy.antenna_mask = 1;
200 	}
201 }
202 EXPORT_SYMBOL_GPL(mt76x02_init_device);
203 
204 void mt76x02_configure_filter(struct ieee80211_hw *hw,
205 			      unsigned int changed_flags,
206 			      unsigned int *total_flags, u64 multicast)
207 {
208 	struct mt76x02_dev *dev = hw->priv;
209 	u32 flags = 0;
210 
211 #define MT76_FILTER(_flag, _hw) do { \
212 		flags |= *total_flags & FIF_##_flag;			\
213 		dev->mt76.rxfilter &= ~(_hw);				\
214 		dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
215 	} while (0)
216 
217 	mutex_lock(&dev->mt76.mutex);
218 
219 	dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
220 
221 	MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
222 	MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
223 	MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
224 			     MT_RX_FILTR_CFG_CTS |
225 			     MT_RX_FILTR_CFG_CFEND |
226 			     MT_RX_FILTR_CFG_CFACK |
227 			     MT_RX_FILTR_CFG_BA |
228 			     MT_RX_FILTR_CFG_CTRL_RSV);
229 	MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
230 
231 	*total_flags = flags;
232 	mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
233 
234 	mutex_unlock(&dev->mt76.mutex);
235 }
236 EXPORT_SYMBOL_GPL(mt76x02_configure_filter);
237 
238 int mt76x02_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
239 		    struct ieee80211_sta *sta)
240 {
241 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
242 	struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
243 	struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
244 	int idx = 0;
245 
246 	memset(msta, 0, sizeof(*msta));
247 
248 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, ARRAY_SIZE(dev->mt76.wcid));
249 	if (idx < 0)
250 		return -ENOSPC;
251 
252 	msta->vif = mvif;
253 	msta->wcid.sta = 1;
254 	msta->wcid.idx = idx;
255 	msta->wcid.hw_key_idx = -1;
256 	mt76x02_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
257 	mt76x02_mac_wcid_set_drop(dev, idx, false);
258 	ewma_pktlen_init(&msta->pktlen);
259 
260 	if (vif->type == NL80211_IFTYPE_AP)
261 		set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
262 
263 	return 0;
264 }
265 EXPORT_SYMBOL_GPL(mt76x02_sta_add);
266 
267 void mt76x02_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
268 			struct ieee80211_sta *sta)
269 {
270 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
271 	struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
272 	int idx = wcid->idx;
273 
274 	mt76x02_mac_wcid_set_drop(dev, idx, true);
275 	mt76x02_mac_wcid_setup(dev, idx, 0, NULL);
276 }
277 EXPORT_SYMBOL_GPL(mt76x02_sta_remove);
278 
279 static void
280 mt76x02_vif_init(struct mt76x02_dev *dev, struct ieee80211_vif *vif,
281 		 unsigned int idx)
282 {
283 	struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
284 	struct mt76_txq *mtxq;
285 
286 	memset(mvif, 0, sizeof(*mvif));
287 
288 	mvif->idx = idx;
289 	mvif->group_wcid.idx = MT_VIF_WCID(idx);
290 	mvif->group_wcid.hw_key_idx = -1;
291 	mtxq = (struct mt76_txq *)vif->txq->drv_priv;
292 	mtxq->wcid = &mvif->group_wcid;
293 
294 	mt76_txq_init(&dev->mt76, vif->txq);
295 }
296 
297 int
298 mt76x02_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
299 {
300 	struct mt76x02_dev *dev = hw->priv;
301 	unsigned int idx = 0;
302 
303 	/* Allow to change address in HW if we create first interface. */
304 	if (!dev->vif_mask &&
305 	    (((vif->addr[0] ^ dev->mt76.macaddr[0]) & ~GENMASK(4, 1)) ||
306 	     memcmp(vif->addr + 1, dev->mt76.macaddr + 1, ETH_ALEN - 1)))
307 		mt76x02_mac_setaddr(dev, vif->addr);
308 
309 	if (vif->addr[0] & BIT(1))
310 		idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
311 
312 	/*
313 	 * Client mode typically only has one configurable BSSID register,
314 	 * which is used for bssidx=0. This is linked to the MAC address.
315 	 * Since mac80211 allows changing interface types, and we cannot
316 	 * force the use of the primary MAC address for a station mode
317 	 * interface, we need some other way of configuring a per-interface
318 	 * remote BSSID.
319 	 * The hardware provides an AP-Client feature, where bssidx 0-7 are
320 	 * used for AP mode and bssidx 8-15 for client mode.
321 	 * We shift the station interface bss index by 8 to force the
322 	 * hardware to recognize the BSSID.
323 	 * The resulting bssidx mismatch for unicast frames is ignored by hw.
324 	 */
325 	if (vif->type == NL80211_IFTYPE_STATION)
326 		idx += 8;
327 
328 	/* vif is already set or idx is 8 for AP/Mesh/... */
329 	if (dev->vif_mask & BIT(idx) ||
330 	    (vif->type != NL80211_IFTYPE_STATION && idx > 7))
331 		return -EBUSY;
332 
333 	dev->vif_mask |= BIT(idx);
334 
335 	mt76x02_vif_init(dev, vif, idx);
336 	return 0;
337 }
338 EXPORT_SYMBOL_GPL(mt76x02_add_interface);
339 
340 void mt76x02_remove_interface(struct ieee80211_hw *hw,
341 			      struct ieee80211_vif *vif)
342 {
343 	struct mt76x02_dev *dev = hw->priv;
344 	struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
345 
346 	mt76_txq_remove(&dev->mt76, vif->txq);
347 	dev->vif_mask &= ~BIT(mvif->idx);
348 }
349 EXPORT_SYMBOL_GPL(mt76x02_remove_interface);
350 
351 int mt76x02_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
352 			 struct ieee80211_ampdu_params *params)
353 {
354 	enum ieee80211_ampdu_mlme_action action = params->action;
355 	struct ieee80211_sta *sta = params->sta;
356 	struct mt76x02_dev *dev = hw->priv;
357 	struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
358 	struct ieee80211_txq *txq = sta->txq[params->tid];
359 	u16 tid = params->tid;
360 	u16 ssn = params->ssn;
361 	struct mt76_txq *mtxq;
362 	int ret = 0;
363 
364 	if (!txq)
365 		return -EINVAL;
366 
367 	mtxq = (struct mt76_txq *)txq->drv_priv;
368 
369 	mutex_lock(&dev->mt76.mutex);
370 	switch (action) {
371 	case IEEE80211_AMPDU_RX_START:
372 		mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid,
373 				   ssn, params->buf_size);
374 		mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
375 		break;
376 	case IEEE80211_AMPDU_RX_STOP:
377 		mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
378 		mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4,
379 			   BIT(16 + tid));
380 		break;
381 	case IEEE80211_AMPDU_TX_OPERATIONAL:
382 		mtxq->aggr = true;
383 		mtxq->send_bar = false;
384 		ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
385 		break;
386 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
387 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
388 		mtxq->aggr = false;
389 		break;
390 	case IEEE80211_AMPDU_TX_START:
391 		mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
392 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
393 		break;
394 	case IEEE80211_AMPDU_TX_STOP_CONT:
395 		mtxq->aggr = false;
396 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
397 		break;
398 	}
399 	mutex_unlock(&dev->mt76.mutex);
400 
401 	return ret;
402 }
403 EXPORT_SYMBOL_GPL(mt76x02_ampdu_action);
404 
405 int mt76x02_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
406 		    struct ieee80211_vif *vif, struct ieee80211_sta *sta,
407 		    struct ieee80211_key_conf *key)
408 {
409 	struct mt76x02_dev *dev = hw->priv;
410 	struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
411 	struct mt76x02_sta *msta;
412 	struct mt76_wcid *wcid;
413 	int idx = key->keyidx;
414 	int ret;
415 
416 	/* fall back to sw encryption for unsupported ciphers */
417 	switch (key->cipher) {
418 	case WLAN_CIPHER_SUITE_WEP40:
419 	case WLAN_CIPHER_SUITE_WEP104:
420 	case WLAN_CIPHER_SUITE_TKIP:
421 	case WLAN_CIPHER_SUITE_CCMP:
422 		break;
423 	default:
424 		return -EOPNOTSUPP;
425 	}
426 
427 	/*
428 	 * The hardware does not support per-STA RX GTK, fall back
429 	 * to software mode for these.
430 	 */
431 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
432 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
433 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
434 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
435 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
436 		return -EOPNOTSUPP;
437 
438 	/*
439 	 * In USB AP mode, broadcast/multicast frames are setup in beacon
440 	 * data registers and sent via HW beacons engine, they require to
441 	 * be already encrypted.
442 	 */
443 	if (mt76_is_usb(&dev->mt76) &&
444 	    vif->type == NL80211_IFTYPE_AP &&
445 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
446 		return -EOPNOTSUPP;
447 
448 	msta = sta ? (struct mt76x02_sta *)sta->drv_priv : NULL;
449 	wcid = msta ? &msta->wcid : &mvif->group_wcid;
450 
451 	if (cmd == SET_KEY) {
452 		key->hw_key_idx = wcid->idx;
453 		wcid->hw_key_idx = idx;
454 		if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
455 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
456 			wcid->sw_iv = true;
457 		}
458 	} else {
459 		if (idx == wcid->hw_key_idx) {
460 			wcid->hw_key_idx = -1;
461 			wcid->sw_iv = false;
462 		}
463 
464 		key = NULL;
465 	}
466 	mt76_wcid_key_setup(&dev->mt76, wcid, key);
467 
468 	if (!msta) {
469 		if (key || wcid->hw_key_idx == idx) {
470 			ret = mt76x02_mac_wcid_set_key(dev, wcid->idx, key);
471 			if (ret)
472 				return ret;
473 		}
474 
475 		return mt76x02_mac_shared_key_setup(dev, mvif->idx, idx, key);
476 	}
477 
478 	return mt76x02_mac_wcid_set_key(dev, msta->wcid.idx, key);
479 }
480 EXPORT_SYMBOL_GPL(mt76x02_set_key);
481 
482 int mt76x02_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
483 		    u16 queue, const struct ieee80211_tx_queue_params *params)
484 {
485 	struct mt76x02_dev *dev = hw->priv;
486 	u8 cw_min = 5, cw_max = 10, qid;
487 	u32 val;
488 
489 	qid = dev->mt76.q_tx[queue].q->hw_idx;
490 
491 	if (params->cw_min)
492 		cw_min = fls(params->cw_min);
493 	if (params->cw_max)
494 		cw_max = fls(params->cw_max);
495 
496 	val = FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop) |
497 	      FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
498 	      FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
499 	      FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
500 	mt76_wr(dev, MT_EDCA_CFG_AC(qid), val);
501 
502 	val = mt76_rr(dev, MT_WMM_TXOP(qid));
503 	val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(qid));
504 	val |= params->txop << MT_WMM_TXOP_SHIFT(qid);
505 	mt76_wr(dev, MT_WMM_TXOP(qid), val);
506 
507 	val = mt76_rr(dev, MT_WMM_AIFSN);
508 	val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(qid));
509 	val |= params->aifs << MT_WMM_AIFSN_SHIFT(qid);
510 	mt76_wr(dev, MT_WMM_AIFSN, val);
511 
512 	val = mt76_rr(dev, MT_WMM_CWMIN);
513 	val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(qid));
514 	val |= cw_min << MT_WMM_CWMIN_SHIFT(qid);
515 	mt76_wr(dev, MT_WMM_CWMIN, val);
516 
517 	val = mt76_rr(dev, MT_WMM_CWMAX);
518 	val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(qid));
519 	val |= cw_max << MT_WMM_CWMAX_SHIFT(qid);
520 	mt76_wr(dev, MT_WMM_CWMAX, val);
521 
522 	return 0;
523 }
524 EXPORT_SYMBOL_GPL(mt76x02_conf_tx);
525 
526 void mt76x02_set_tx_ackto(struct mt76x02_dev *dev)
527 {
528 	u8 ackto, sifs, slottime = dev->slottime;
529 
530 	/* As defined by IEEE 802.11-2007 17.3.8.6 */
531 	slottime += 3 * dev->coverage_class;
532 	mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
533 		       MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
534 
535 	sifs = mt76_get_field(dev, MT_XIFS_TIME_CFG,
536 			      MT_XIFS_TIME_CFG_OFDM_SIFS);
537 
538 	ackto = slottime + sifs;
539 	mt76_rmw_field(dev, MT_TX_TIMEOUT_CFG,
540 		       MT_TX_TIMEOUT_CFG_ACKTO, ackto);
541 }
542 EXPORT_SYMBOL_GPL(mt76x02_set_tx_ackto);
543 
544 void mt76x02_set_coverage_class(struct ieee80211_hw *hw,
545 				s16 coverage_class)
546 {
547 	struct mt76x02_dev *dev = hw->priv;
548 
549 	mutex_lock(&dev->mt76.mutex);
550 	dev->coverage_class = max_t(s16, coverage_class, 0);
551 	mt76x02_set_tx_ackto(dev);
552 	mutex_unlock(&dev->mt76.mutex);
553 }
554 EXPORT_SYMBOL_GPL(mt76x02_set_coverage_class);
555 
556 int mt76x02_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
557 {
558 	struct mt76x02_dev *dev = hw->priv;
559 
560 	if (val != ~0 && val > 0xffff)
561 		return -EINVAL;
562 
563 	mutex_lock(&dev->mt76.mutex);
564 	mt76x02_mac_set_rts_thresh(dev, val);
565 	mutex_unlock(&dev->mt76.mutex);
566 
567 	return 0;
568 }
569 EXPORT_SYMBOL_GPL(mt76x02_set_rts_threshold);
570 
571 void mt76x02_sta_rate_tbl_update(struct ieee80211_hw *hw,
572 				 struct ieee80211_vif *vif,
573 				 struct ieee80211_sta *sta)
574 {
575 	struct mt76x02_dev *dev = hw->priv;
576 	struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
577 	struct ieee80211_sta_rates *rates = rcu_dereference(sta->rates);
578 	struct ieee80211_tx_rate rate = {};
579 
580 	if (!rates)
581 		return;
582 
583 	rate.idx = rates->rate[0].idx;
584 	rate.flags = rates->rate[0].flags;
585 	mt76x02_mac_wcid_set_rate(dev, &msta->wcid, &rate);
586 }
587 EXPORT_SYMBOL_GPL(mt76x02_sta_rate_tbl_update);
588 
589 void mt76x02_remove_hdr_pad(struct sk_buff *skb, int len)
590 {
591 	int hdrlen;
592 
593 	if (!len)
594 		return;
595 
596 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
597 	memmove(skb->data + len, skb->data, hdrlen);
598 	skb_pull(skb, len);
599 }
600 EXPORT_SYMBOL_GPL(mt76x02_remove_hdr_pad);
601 
602 void mt76x02_sw_scan_complete(struct ieee80211_hw *hw,
603 			      struct ieee80211_vif *vif)
604 {
605 	struct mt76x02_dev *dev = hw->priv;
606 
607 	clear_bit(MT76_SCANNING, &dev->mphy.state);
608 	if (dev->cal.gain_init_done) {
609 		/* Restore AGC gain and resume calibration after scanning. */
610 		dev->cal.low_gain = -1;
611 		ieee80211_queue_delayed_work(hw, &dev->cal_work, 0);
612 	}
613 }
614 EXPORT_SYMBOL_GPL(mt76x02_sw_scan_complete);
615 
616 void mt76x02_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta,
617 		    bool ps)
618 {
619 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
620 	struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
621 	int idx = msta->wcid.idx;
622 
623 	mt76_stop_tx_queues(&dev->mt76, sta, true);
624 	if (mt76_is_mmio(mdev))
625 		mt76x02_mac_wcid_set_drop(dev, idx, ps);
626 }
627 EXPORT_SYMBOL_GPL(mt76x02_sta_ps);
628 
629 void mt76x02_bss_info_changed(struct ieee80211_hw *hw,
630 			      struct ieee80211_vif *vif,
631 			      struct ieee80211_bss_conf *info,
632 			      u32 changed)
633 {
634 	struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
635 	struct mt76x02_dev *dev = hw->priv;
636 
637 	mutex_lock(&dev->mt76.mutex);
638 
639 	if (changed & BSS_CHANGED_BSSID)
640 		mt76x02_mac_set_bssid(dev, mvif->idx, info->bssid);
641 
642 	if (changed & BSS_CHANGED_HT || changed & BSS_CHANGED_ERP_CTS_PROT)
643 		mt76x02_mac_set_tx_protection(dev, info->use_cts_prot,
644 					      info->ht_operation_mode);
645 
646 	if (changed & BSS_CHANGED_BEACON_INT) {
647 		mt76_rmw_field(dev, MT_BEACON_TIME_CFG,
648 			       MT_BEACON_TIME_CFG_INTVAL,
649 			       info->beacon_int << 4);
650 		dev->mt76.beacon_int = info->beacon_int;
651 	}
652 
653 	if (changed & BSS_CHANGED_BEACON_ENABLED)
654 		mt76x02_mac_set_beacon_enable(dev, vif, info->enable_beacon);
655 
656 	if (changed & BSS_CHANGED_ERP_PREAMBLE)
657 		mt76x02_mac_set_short_preamble(dev, info->use_short_preamble);
658 
659 	if (changed & BSS_CHANGED_ERP_SLOT) {
660 		int slottime = info->use_short_slot ? 9 : 20;
661 
662 		dev->slottime = slottime;
663 		mt76x02_set_tx_ackto(dev);
664 	}
665 
666 	mutex_unlock(&dev->mt76.mutex);
667 }
668 EXPORT_SYMBOL_GPL(mt76x02_bss_info_changed);
669 
670 void mt76x02_config_mac_addr_list(struct mt76x02_dev *dev)
671 {
672 	struct ieee80211_hw *hw = mt76_hw(dev);
673 	struct wiphy *wiphy = hw->wiphy;
674 	int i;
675 
676 	for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
677 		u8 *addr = dev->macaddr_list[i].addr;
678 
679 		memcpy(addr, dev->mt76.macaddr, ETH_ALEN);
680 
681 		if (!i)
682 			continue;
683 
684 		addr[0] |= BIT(1);
685 		addr[0] ^= ((i - 1) << 2);
686 	}
687 	wiphy->addresses = dev->macaddr_list;
688 	wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
689 }
690 EXPORT_SYMBOL_GPL(mt76x02_config_mac_addr_list);
691 
692 MODULE_LICENSE("Dual BSD/GPL");
693