2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 mt76x2_start(struct ieee80211_hw
*hw
)
22 struct mt76x2_dev
*dev
= hw
->priv
;
25 mutex_lock(&dev
->mutex
);
27 ret
= mt76x2_mac_start(dev
);
31 ret
= mt76x2_phy_start(dev
);
35 ieee80211_queue_delayed_work(mt76_hw(dev
), &dev
->mac_work
,
36 MT_CALIBRATE_INTERVAL
);
38 set_bit(MT76_STATE_RUNNING
, &dev
->mt76
.state
);
41 mutex_unlock(&dev
->mutex
);
46 mt76x2_stop(struct ieee80211_hw
*hw
)
48 struct mt76x2_dev
*dev
= hw
->priv
;
50 mutex_lock(&dev
->mutex
);
51 clear_bit(MT76_STATE_RUNNING
, &dev
->mt76
.state
);
52 mt76x2_stop_hardware(dev
);
53 mutex_unlock(&dev
->mutex
);
57 mt76x2_txq_init(struct mt76x2_dev
*dev
, struct ieee80211_txq
*txq
)
59 struct mt76_txq
*mtxq
;
64 mtxq
= (struct mt76_txq
*) txq
->drv_priv
;
66 struct mt76x2_sta
*sta
;
68 sta
= (struct mt76x2_sta
*) txq
->sta
->drv_priv
;
69 mtxq
->wcid
= &sta
->wcid
;
71 struct mt76x2_vif
*mvif
;
73 mvif
= (struct mt76x2_vif
*) txq
->vif
->drv_priv
;
74 mtxq
->wcid
= &mvif
->group_wcid
;
77 mt76_txq_init(&dev
->mt76
, txq
);
81 mt76x2_add_interface(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
83 struct mt76x2_dev
*dev
= hw
->priv
;
84 struct mt76x2_vif
*mvif
= (struct mt76x2_vif
*) vif
->drv_priv
;
87 if (vif
->addr
[0] & BIT(1))
88 idx
= 1 + (((dev
->mt76
.macaddr
[0] ^ vif
->addr
[0]) >> 2) & 7);
91 * Client mode typically only has one configurable BSSID register,
92 * which is used for bssidx=0. This is linked to the MAC address.
93 * Since mac80211 allows changing interface types, and we cannot
94 * force the use of the primary MAC address for a station mode
95 * interface, we need some other way of configuring a per-interface
97 * The hardware provides an AP-Client feature, where bssidx 0-7 are
98 * used for AP mode and bssidx 8-15 for client mode.
99 * We shift the station interface bss index by 8 to force the
100 * hardware to recognize the BSSID.
101 * The resulting bssidx mismatch for unicast frames is ignored by hw.
103 if (vif
->type
== NL80211_IFTYPE_STATION
)
107 mvif
->group_wcid
.idx
= 254 - idx
;
108 mvif
->group_wcid
.hw_key_idx
= -1;
109 mt76x2_txq_init(dev
, vif
->txq
);
115 mt76x2_remove_interface(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
117 struct mt76x2_dev
*dev
= hw
->priv
;
119 mt76_txq_remove(&dev
->mt76
, vif
->txq
);
123 mt76x2_set_channel(struct mt76x2_dev
*dev
, struct cfg80211_chan_def
*chandef
)
127 mt76_set_channel(&dev
->mt76
);
129 tasklet_disable(&dev
->pre_tbtt_tasklet
);
130 tasklet_disable(&dev
->dfs_pd
.dfs_tasklet
);
131 cancel_delayed_work_sync(&dev
->cal_work
);
133 mt76x2_mac_stop(dev
, true);
134 ret
= mt76x2_phy_set_channel(dev
, chandef
);
136 /* channel cycle counters read-and-clear */
137 mt76_rr(dev
, MT_CH_IDLE
);
138 mt76_rr(dev
, MT_CH_BUSY
);
140 mt76x2_dfs_init_params(dev
);
142 mt76x2_mac_resume(dev
);
143 tasklet_enable(&dev
->dfs_pd
.dfs_tasklet
);
144 tasklet_enable(&dev
->pre_tbtt_tasklet
);
150 mt76x2_config(struct ieee80211_hw
*hw
, u32 changed
)
152 struct mt76x2_dev
*dev
= hw
->priv
;
155 mutex_lock(&dev
->mutex
);
157 if (changed
& IEEE80211_CONF_CHANGE_MONITOR
) {
158 if (!(hw
->conf
.flags
& IEEE80211_CONF_MONITOR
))
159 dev
->rxfilter
|= MT_RX_FILTR_CFG_PROMISC
;
161 dev
->rxfilter
&= ~MT_RX_FILTR_CFG_PROMISC
;
163 mt76_wr(dev
, MT_RX_FILTR_CFG
, dev
->rxfilter
);
166 if (changed
& IEEE80211_CONF_CHANGE_POWER
) {
167 dev
->txpower_conf
= hw
->conf
.power_level
* 2;
169 /* convert to per-chain power for 2x2 devices */
170 dev
->txpower_conf
-= 6;
172 if (test_bit(MT76_STATE_RUNNING
, &dev
->mt76
.state
)) {
173 mt76x2_phy_set_txpower(dev
);
174 mt76x2_tx_set_txpwr_auto(dev
, dev
->txpower_conf
);
178 if (changed
& IEEE80211_CONF_CHANGE_CHANNEL
) {
179 ieee80211_stop_queues(hw
);
180 ret
= mt76x2_set_channel(dev
, &hw
->conf
.chandef
);
181 ieee80211_wake_queues(hw
);
184 mutex_unlock(&dev
->mutex
);
190 mt76x2_configure_filter(struct ieee80211_hw
*hw
, unsigned int changed_flags
,
191 unsigned int *total_flags
, u64 multicast
)
193 struct mt76x2_dev
*dev
= hw
->priv
;
196 #define MT76_FILTER(_flag, _hw) do { \
197 flags |= *total_flags & FIF_##_flag; \
198 dev->rxfilter &= ~(_hw); \
199 dev->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
202 mutex_lock(&dev
->mutex
);
204 dev
->rxfilter
&= ~MT_RX_FILTR_CFG_OTHER_BSS
;
206 MT76_FILTER(FCSFAIL
, MT_RX_FILTR_CFG_CRC_ERR
);
207 MT76_FILTER(PLCPFAIL
, MT_RX_FILTR_CFG_PHY_ERR
);
208 MT76_FILTER(CONTROL
, MT_RX_FILTR_CFG_ACK
|
209 MT_RX_FILTR_CFG_CTS
|
210 MT_RX_FILTR_CFG_CFEND
|
211 MT_RX_FILTR_CFG_CFACK
|
213 MT_RX_FILTR_CFG_CTRL_RSV
);
214 MT76_FILTER(PSPOLL
, MT_RX_FILTR_CFG_PSPOLL
);
216 *total_flags
= flags
;
217 mt76_wr(dev
, MT_RX_FILTR_CFG
, dev
->rxfilter
);
219 mutex_unlock(&dev
->mutex
);
223 mt76x2_bss_info_changed(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
224 struct ieee80211_bss_conf
*info
, u32 changed
)
226 struct mt76x2_dev
*dev
= hw
->priv
;
227 struct mt76x2_vif
*mvif
= (struct mt76x2_vif
*) vif
->drv_priv
;
229 mutex_lock(&dev
->mutex
);
231 if (changed
& BSS_CHANGED_BSSID
)
232 mt76x2_mac_set_bssid(dev
, mvif
->idx
, info
->bssid
);
234 if (changed
& BSS_CHANGED_BEACON_INT
)
235 mt76_rmw_field(dev
, MT_BEACON_TIME_CFG
,
236 MT_BEACON_TIME_CFG_INTVAL
,
237 info
->beacon_int
<< 4);
239 if (changed
& BSS_CHANGED_BEACON_ENABLED
) {
240 tasklet_disable(&dev
->pre_tbtt_tasklet
);
241 mt76x2_mac_set_beacon_enable(dev
, mvif
->idx
,
242 info
->enable_beacon
);
243 tasklet_enable(&dev
->pre_tbtt_tasklet
);
246 if (changed
& BSS_CHANGED_ERP_SLOT
) {
247 int slottime
= info
->use_short_slot
? 9 : 20;
249 dev
->slottime
= slottime
;
250 mt76_rmw_field(dev
, MT_BKOFF_SLOT_CFG
,
251 MT_BKOFF_SLOT_CFG_SLOTTIME
, slottime
);
254 mutex_unlock(&dev
->mutex
);
258 mt76x2_sta_add(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
259 struct ieee80211_sta
*sta
)
261 struct mt76x2_dev
*dev
= hw
->priv
;
262 struct mt76x2_sta
*msta
= (struct mt76x2_sta
*) sta
->drv_priv
;
263 struct mt76x2_vif
*mvif
= (struct mt76x2_vif
*) vif
->drv_priv
;
268 mutex_lock(&dev
->mutex
);
270 idx
= mt76_wcid_alloc(dev
->wcid_mask
, ARRAY_SIZE(dev
->wcid
));
278 msta
->wcid
.idx
= idx
;
279 msta
->wcid
.hw_key_idx
= -1;
280 mt76x2_mac_wcid_setup(dev
, idx
, mvif
->idx
, sta
->addr
);
281 mt76x2_mac_wcid_set_drop(dev
, idx
, false);
282 for (i
= 0; i
< ARRAY_SIZE(sta
->txq
); i
++)
283 mt76x2_txq_init(dev
, sta
->txq
[i
]);
285 if (vif
->type
== NL80211_IFTYPE_AP
)
286 set_bit(MT_WCID_FLAG_CHECK_PS
, &msta
->wcid
.flags
);
288 rcu_assign_pointer(dev
->wcid
[idx
], &msta
->wcid
);
291 mutex_unlock(&dev
->mutex
);
297 mt76x2_sta_remove(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
298 struct ieee80211_sta
*sta
)
300 struct mt76x2_dev
*dev
= hw
->priv
;
301 struct mt76x2_sta
*msta
= (struct mt76x2_sta
*) sta
->drv_priv
;
302 int idx
= msta
->wcid
.idx
;
305 mutex_lock(&dev
->mutex
);
306 rcu_assign_pointer(dev
->wcid
[idx
], NULL
);
307 for (i
= 0; i
< ARRAY_SIZE(sta
->txq
); i
++)
308 mt76_txq_remove(&dev
->mt76
, sta
->txq
[i
]);
309 mt76x2_mac_wcid_set_drop(dev
, idx
, true);
310 mt76_wcid_free(dev
->wcid_mask
, idx
);
311 mt76x2_mac_wcid_setup(dev
, idx
, 0, NULL
);
312 mutex_unlock(&dev
->mutex
);
318 mt76x2_sta_ps(struct mt76_dev
*mdev
, struct ieee80211_sta
*sta
, bool ps
)
320 struct mt76x2_sta
*msta
= (struct mt76x2_sta
*) sta
->drv_priv
;
321 struct mt76x2_dev
*dev
= container_of(mdev
, struct mt76x2_dev
, mt76
);
322 int idx
= msta
->wcid
.idx
;
324 mt76x2_mac_wcid_set_drop(dev
, idx
, ps
);
328 mt76x2_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
329 struct ieee80211_vif
*vif
, struct ieee80211_sta
*sta
,
330 struct ieee80211_key_conf
*key
)
332 struct mt76x2_dev
*dev
= hw
->priv
;
333 struct mt76x2_vif
*mvif
= (struct mt76x2_vif
*) vif
->drv_priv
;
334 struct mt76x2_sta
*msta
;
335 struct mt76_wcid
*wcid
;
336 int idx
= key
->keyidx
;
340 * The hardware does not support per-STA RX GTK, fall back
341 * to software mode for these.
343 if ((vif
->type
== NL80211_IFTYPE_ADHOC
||
344 vif
->type
== NL80211_IFTYPE_MESH_POINT
) &&
345 (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
||
346 key
->cipher
== WLAN_CIPHER_SUITE_CCMP
) &&
347 !(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
350 msta
= sta
? (struct mt76x2_sta
*) sta
->drv_priv
: NULL
;
351 wcid
= msta
? &msta
->wcid
: &mvif
->group_wcid
;
353 if (cmd
== SET_KEY
) {
354 key
->hw_key_idx
= wcid
->idx
;
355 wcid
->hw_key_idx
= idx
;
356 if (key
->flags
& IEEE80211_KEY_FLAG_RX_MGMT
) {
357 key
->flags
|= IEEE80211_KEY_FLAG_SW_MGMT_TX
;
361 if (idx
== wcid
->hw_key_idx
) {
362 wcid
->hw_key_idx
= -1;
368 mt76_wcid_key_setup(&dev
->mt76
, wcid
, key
);
371 if (key
|| wcid
->hw_key_idx
== idx
) {
372 ret
= mt76x2_mac_wcid_set_key(dev
, wcid
->idx
, key
);
377 return mt76x2_mac_shared_key_setup(dev
, mvif
->idx
, idx
, key
);
380 return mt76x2_mac_wcid_set_key(dev
, msta
->wcid
.idx
, key
);
384 mt76x2_conf_tx(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
, u16 queue
,
385 const struct ieee80211_tx_queue_params
*params
)
387 struct mt76x2_dev
*dev
= hw
->priv
;
388 u8 cw_min
= 5, cw_max
= 10, qid
;
391 qid
= dev
->mt76
.q_tx
[queue
].hw_idx
;
394 cw_min
= fls(params
->cw_min
);
396 cw_max
= fls(params
->cw_max
);
398 val
= FIELD_PREP(MT_EDCA_CFG_TXOP
, params
->txop
) |
399 FIELD_PREP(MT_EDCA_CFG_AIFSN
, params
->aifs
) |
400 FIELD_PREP(MT_EDCA_CFG_CWMIN
, cw_min
) |
401 FIELD_PREP(MT_EDCA_CFG_CWMAX
, cw_max
);
402 mt76_wr(dev
, MT_EDCA_CFG_AC(qid
), val
);
404 val
= mt76_rr(dev
, MT_WMM_TXOP(qid
));
405 val
&= ~(MT_WMM_TXOP_MASK
<< MT_WMM_TXOP_SHIFT(qid
));
406 val
|= params
->txop
<< MT_WMM_TXOP_SHIFT(qid
);
407 mt76_wr(dev
, MT_WMM_TXOP(qid
), val
);
409 val
= mt76_rr(dev
, MT_WMM_AIFSN
);
410 val
&= ~(MT_WMM_AIFSN_MASK
<< MT_WMM_AIFSN_SHIFT(qid
));
411 val
|= params
->aifs
<< MT_WMM_AIFSN_SHIFT(qid
);
412 mt76_wr(dev
, MT_WMM_AIFSN
, val
);
414 val
= mt76_rr(dev
, MT_WMM_CWMIN
);
415 val
&= ~(MT_WMM_CWMIN_MASK
<< MT_WMM_CWMIN_SHIFT(qid
));
416 val
|= cw_min
<< MT_WMM_CWMIN_SHIFT(qid
);
417 mt76_wr(dev
, MT_WMM_CWMIN
, val
);
419 val
= mt76_rr(dev
, MT_WMM_CWMAX
);
420 val
&= ~(MT_WMM_CWMAX_MASK
<< MT_WMM_CWMAX_SHIFT(qid
));
421 val
|= cw_max
<< MT_WMM_CWMAX_SHIFT(qid
);
422 mt76_wr(dev
, MT_WMM_CWMAX
, val
);
428 mt76x2_sw_scan(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
431 struct mt76x2_dev
*dev
= hw
->priv
;
433 tasklet_disable(&dev
->pre_tbtt_tasklet
);
434 set_bit(MT76_SCANNING
, &dev
->mt76
.state
);
438 mt76x2_sw_scan_complete(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
)
440 struct mt76x2_dev
*dev
= hw
->priv
;
442 clear_bit(MT76_SCANNING
, &dev
->mt76
.state
);
443 tasklet_enable(&dev
->pre_tbtt_tasklet
);
444 mt76_txq_schedule_all(&dev
->mt76
);
448 mt76x2_flush(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
449 u32 queues
, bool drop
)
454 mt76x2_get_txpower(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
, int *dbm
)
456 struct mt76x2_dev
*dev
= hw
->priv
;
458 *dbm
= dev
->txpower_cur
/ 2;
460 /* convert from per-chain power to combined output on 2x2 devices */
467 mt76x2_ampdu_action(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
468 struct ieee80211_ampdu_params
*params
)
470 enum ieee80211_ampdu_mlme_action action
= params
->action
;
471 struct ieee80211_sta
*sta
= params
->sta
;
472 struct mt76x2_dev
*dev
= hw
->priv
;
473 struct mt76x2_sta
*msta
= (struct mt76x2_sta
*) sta
->drv_priv
;
474 struct ieee80211_txq
*txq
= sta
->txq
[params
->tid
];
475 u16 tid
= params
->tid
;
476 u16
*ssn
= ¶ms
->ssn
;
477 struct mt76_txq
*mtxq
;
482 mtxq
= (struct mt76_txq
*)txq
->drv_priv
;
485 case IEEE80211_AMPDU_RX_START
:
486 mt76_rx_aggr_start(&dev
->mt76
, &msta
->wcid
, tid
, *ssn
, params
->buf_size
);
487 mt76_set(dev
, MT_WCID_ADDR(msta
->wcid
.idx
) + 4, BIT(16 + tid
));
489 case IEEE80211_AMPDU_RX_STOP
:
490 mt76_rx_aggr_stop(&dev
->mt76
, &msta
->wcid
, tid
);
491 mt76_clear(dev
, MT_WCID_ADDR(msta
->wcid
.idx
) + 4,
494 case IEEE80211_AMPDU_TX_OPERATIONAL
:
496 mtxq
->send_bar
= false;
497 ieee80211_send_bar(vif
, sta
->addr
, tid
, mtxq
->agg_ssn
);
499 case IEEE80211_AMPDU_TX_STOP_FLUSH
:
500 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
:
502 ieee80211_send_bar(vif
, sta
->addr
, tid
, mtxq
->agg_ssn
);
504 case IEEE80211_AMPDU_TX_START
:
505 mtxq
->agg_ssn
= *ssn
<< 4;
506 ieee80211_start_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
508 case IEEE80211_AMPDU_TX_STOP_CONT
:
510 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
518 mt76x2_sta_rate_tbl_update(struct ieee80211_hw
*hw
, struct ieee80211_vif
*vif
,
519 struct ieee80211_sta
*sta
)
521 struct mt76x2_dev
*dev
= hw
->priv
;
522 struct mt76x2_sta
*msta
= (struct mt76x2_sta
*) sta
->drv_priv
;
523 struct ieee80211_sta_rates
*rates
= rcu_dereference(sta
->rates
);
524 struct ieee80211_tx_rate rate
= {};
529 rate
.idx
= rates
->rate
[0].idx
;
530 rate
.flags
= rates
->rate
[0].flags
;
531 mt76x2_mac_wcid_set_rate(dev
, &msta
->wcid
, &rate
);
532 msta
->wcid
.max_txpwr_adj
= mt76x2_tx_get_max_txpwr_adj(dev
, &rate
);
535 static void mt76x2_set_coverage_class(struct ieee80211_hw
*hw
,
538 struct mt76x2_dev
*dev
= hw
->priv
;
540 mutex_lock(&dev
->mutex
);
541 dev
->coverage_class
= coverage_class
;
542 mt76x2_set_tx_ackto(dev
);
543 mutex_unlock(&dev
->mutex
);
547 mt76x2_set_tim(struct ieee80211_hw
*hw
, struct ieee80211_sta
*sta
, bool set
)
552 const struct ieee80211_ops mt76x2_ops
= {
554 .start
= mt76x2_start
,
556 .add_interface
= mt76x2_add_interface
,
557 .remove_interface
= mt76x2_remove_interface
,
558 .config
= mt76x2_config
,
559 .configure_filter
= mt76x2_configure_filter
,
560 .bss_info_changed
= mt76x2_bss_info_changed
,
561 .sta_add
= mt76x2_sta_add
,
562 .sta_remove
= mt76x2_sta_remove
,
563 .set_key
= mt76x2_set_key
,
564 .conf_tx
= mt76x2_conf_tx
,
565 .sw_scan_start
= mt76x2_sw_scan
,
566 .sw_scan_complete
= mt76x2_sw_scan_complete
,
567 .flush
= mt76x2_flush
,
568 .ampdu_action
= mt76x2_ampdu_action
,
569 .get_txpower
= mt76x2_get_txpower
,
570 .wake_tx_queue
= mt76_wake_tx_queue
,
571 .sta_rate_tbl_update
= mt76x2_sta_rate_tbl_update
,
572 .release_buffered_frames
= mt76_release_buffered_frames
,
573 .set_coverage_class
= mt76x2_set_coverage_class
,
574 .get_survey
= mt76_get_survey
,
575 .set_tim
= mt76x2_set_tim
,