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