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