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