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