2 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <linux/etherdevice.h>
20 static int mt76x0_start(struct ieee80211_hw *hw)
22 struct mt76x0_dev *dev = hw->priv;
25 mutex_lock(&dev->mutex);
27 ret = mt76x0_mac_start(dev);
31 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mac_work,
32 MT_CALIBRATE_INTERVAL);
33 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
34 MT_CALIBRATE_INTERVAL);
36 mutex_unlock(&dev->mutex);
40 static void mt76x0_stop(struct ieee80211_hw *hw)
42 struct mt76x0_dev *dev = hw->priv;
44 mutex_lock(&dev->mutex);
46 cancel_delayed_work_sync(&dev->cal_work);
47 cancel_delayed_work_sync(&dev->mac_work);
50 mutex_unlock(&dev->mutex);
54 static int mt76x0_add_interface(struct ieee80211_hw *hw,
55 struct ieee80211_vif *vif)
57 struct mt76x0_dev *dev = hw->priv;
58 struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
61 idx = ffs(~dev->vif_mask);
66 dev->vif_mask |= BIT(idx);
69 mvif->group_wcid.idx = GROUP_WCID(idx);
70 mvif->group_wcid.hw_key_idx = -1;
75 static void mt76x0_remove_interface(struct ieee80211_hw *hw,
76 struct ieee80211_vif *vif)
78 struct mt76x0_dev *dev = hw->priv;
79 struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
81 dev->vif_mask &= ~BIT(mvif->idx);
84 static int mt76x0_config(struct ieee80211_hw *hw, u32 changed)
86 struct mt76x0_dev *dev = hw->priv;
89 mutex_lock(&dev->mutex);
91 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
92 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
93 dev->rxfilter |= MT_RX_FILTR_CFG_PROMISC;
95 dev->rxfilter &= ~MT_RX_FILTR_CFG_PROMISC;
97 mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
100 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
101 ieee80211_stop_queues(hw);
102 ret = mt76x0_phy_set_channel(dev, &hw->conf.chandef);
103 ieee80211_wake_queues(hw);
106 mutex_unlock(&dev->mutex);
112 mt76_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
113 unsigned int *total_flags, u64 multicast)
115 struct mt76x0_dev *dev = hw->priv;
118 #define MT76_FILTER(_flag, _hw) do { \
119 flags |= *total_flags & FIF_##_flag; \
120 dev->rxfilter &= ~(_hw); \
121 dev->rxfilter |= !(flags & FIF_##_flag) * (_hw); \
124 mutex_lock(&dev->mutex);
126 dev->rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
128 MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
129 MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
130 MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
131 MT_RX_FILTR_CFG_CTS |
132 MT_RX_FILTR_CFG_CFEND |
133 MT_RX_FILTR_CFG_CFACK |
135 MT_RX_FILTR_CFG_CTRL_RSV);
136 MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
138 *total_flags = flags;
139 mt76_wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
141 mutex_unlock(&dev->mutex);
145 mt76x0_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
146 struct ieee80211_bss_conf *info, u32 changed)
148 struct mt76x0_dev *dev = hw->priv;
150 mutex_lock(&dev->mutex);
152 if (changed & BSS_CHANGED_ASSOC)
153 mt76x0_phy_con_cal_onoff(dev, info);
155 if (changed & BSS_CHANGED_BSSID) {
156 mt76x0_addr_wr(dev, MT_MAC_BSSID_DW0, info->bssid);
158 /* Note: this is a hack because beacon_int is not changed
159 * on leave nor is any more appropriate event generated.
160 * rt2x00 doesn't seem to be bothered though.
162 if (is_zero_ether_addr(info->bssid))
163 mt76x0_mac_config_tsf(dev, false, 0);
166 if (changed & BSS_CHANGED_BASIC_RATES) {
167 mt76_wr(dev, MT_LEGACY_BASIC_RATE, info->basic_rates);
168 mt76_wr(dev, MT_HT_FBK_CFG0, 0x65432100);
169 mt76_wr(dev, MT_HT_FBK_CFG1, 0xedcba980);
170 mt76_wr(dev, MT_LG_FBK_CFG0, 0xedcba988);
171 mt76_wr(dev, MT_LG_FBK_CFG1, 0x00002100);
174 if (changed & BSS_CHANGED_BEACON_INT)
175 mt76x0_mac_config_tsf(dev, true, info->beacon_int);
177 if (changed & BSS_CHANGED_HT || changed & BSS_CHANGED_ERP_CTS_PROT)
178 mt76x0_mac_set_protection(dev, info->use_cts_prot,
179 info->ht_operation_mode);
181 if (changed & BSS_CHANGED_ERP_PREAMBLE)
182 mt76x0_mac_set_short_preamble(dev, info->use_short_preamble);
184 if (changed & BSS_CHANGED_ERP_SLOT) {
185 int slottime = info->use_short_slot ? 9 : 20;
187 mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
188 MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
191 if (changed & BSS_CHANGED_ASSOC)
192 mt76x0_phy_recalibrate_after_assoc(dev);
194 mutex_unlock(&dev->mutex);
198 mt76x0_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
199 struct ieee80211_sta *sta)
201 struct mt76x0_dev *dev = hw->priv;
202 struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
203 struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
207 mutex_lock(&dev->mutex);
209 idx = mt76_wcid_alloc(dev->wcid_mask, ARRAY_SIZE(dev->wcid));
215 msta->wcid.idx = idx;
216 msta->wcid.hw_key_idx = -1;
217 mt76x0_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
218 mt76_clear(dev, MT_WCID_DROP(idx), MT_WCID_DROP_MASK(idx));
219 rcu_assign_pointer(dev->wcid[idx], &msta->wcid);
220 mt76x0_mac_set_ampdu_factor(dev);
223 mutex_unlock(&dev->mutex);
229 mt76x0_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
230 struct ieee80211_sta *sta)
232 struct mt76x0_dev *dev = hw->priv;
233 struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
234 int idx = msta->wcid.idx;
236 mutex_lock(&dev->mutex);
237 rcu_assign_pointer(dev->wcid[idx], NULL);
238 mt76_set(dev, MT_WCID_DROP(idx), MT_WCID_DROP_MASK(idx));
239 dev->wcid_mask[idx / BITS_PER_LONG] &= ~BIT(idx % BITS_PER_LONG);
240 mt76x0_mac_wcid_setup(dev, idx, 0, NULL);
241 mt76x0_mac_set_ampdu_factor(dev);
242 mutex_unlock(&dev->mutex);
248 mt76x0_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
249 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
254 mt76x0_sw_scan(struct ieee80211_hw *hw,
255 struct ieee80211_vif *vif,
258 struct mt76x0_dev *dev = hw->priv;
260 cancel_delayed_work_sync(&dev->cal_work);
261 mt76x0_agc_save(dev);
262 set_bit(MT76_SCANNING, &dev->mt76.state);
266 mt76x0_sw_scan_complete(struct ieee80211_hw *hw,
267 struct ieee80211_vif *vif)
269 struct mt76x0_dev *dev = hw->priv;
271 mt76x0_agc_restore(dev);
272 clear_bit(MT76_SCANNING, &dev->mt76.state);
274 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
275 MT_CALIBRATE_INTERVAL);
279 mt76x0_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
280 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
281 struct ieee80211_key_conf *key)
283 struct mt76x0_dev *dev = hw->priv;
284 struct mt76_vif *mvif = (struct mt76_vif *) vif->drv_priv;
285 struct mt76_sta *msta = sta ? (struct mt76_sta *) sta->drv_priv : NULL;
286 struct mt76_wcid *wcid = msta ? &msta->wcid : &mvif->group_wcid;
287 int idx = key->keyidx;
290 if (cmd == SET_KEY) {
291 key->hw_key_idx = wcid->idx;
292 wcid->hw_key_idx = idx;
294 if (idx == wcid->hw_key_idx)
295 wcid->hw_key_idx = -1;
301 if (key || wcid->hw_key_idx == idx) {
302 ret = mt76x0_mac_wcid_set_key(dev, wcid->idx, key);
307 return mt76x0_mac_shared_key_setup(dev, mvif->idx, idx, key);
310 return mt76x0_mac_wcid_set_key(dev, msta->wcid.idx, key);
313 static int mt76x0_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
315 struct mt76x0_dev *dev = hw->priv;
317 mt76_rmw_field(dev, MT_TX_RTS_CFG, MT_TX_RTS_CFG_THRESH, value);
323 mt76_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
324 struct ieee80211_ampdu_params *params)
326 struct mt76x0_dev *dev = hw->priv;
327 struct ieee80211_sta *sta = params->sta;
328 enum ieee80211_ampdu_mlme_action action = params->action;
329 u16 tid = params->tid;
330 u16 *ssn = ¶ms->ssn;
331 struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
333 WARN_ON(msta->wcid.idx > N_WCIDS);
336 case IEEE80211_AMPDU_RX_START:
337 mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
339 case IEEE80211_AMPDU_RX_STOP:
340 mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
342 case IEEE80211_AMPDU_TX_OPERATIONAL:
343 ieee80211_send_bar(vif, sta->addr, tid, msta->agg_ssn[tid]);
345 case IEEE80211_AMPDU_TX_STOP_FLUSH:
346 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
348 case IEEE80211_AMPDU_TX_START:
349 msta->agg_ssn[tid] = *ssn << 4;
350 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
352 case IEEE80211_AMPDU_TX_STOP_CONT:
353 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
361 mt76_sta_rate_tbl_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
362 struct ieee80211_sta *sta)
364 struct mt76x0_dev *dev = hw->priv;
365 struct mt76_sta *msta = (struct mt76_sta *) sta->drv_priv;
366 struct ieee80211_sta_rates *rates;
367 struct ieee80211_tx_rate rate = {};
370 rates = rcu_dereference(sta->rates);
375 rate.idx = rates->rate[0].idx;
376 rate.flags = rates->rate[0].flags;
377 mt76x0_mac_wcid_set_rate(dev, &msta->wcid, &rate);
383 const struct ieee80211_ops mt76x0_ops = {
385 .start = mt76x0_start,
387 .add_interface = mt76x0_add_interface,
388 .remove_interface = mt76x0_remove_interface,
389 .config = mt76x0_config,
390 .configure_filter = mt76_configure_filter,
391 .bss_info_changed = mt76x0_bss_info_changed,
392 .sta_add = mt76x0_sta_add,
393 .sta_remove = mt76x0_sta_remove,
394 .sta_notify = mt76x0_sta_notify,
395 .set_key = mt76x0_set_key,
396 .conf_tx = mt76x0_conf_tx,
397 .sw_scan_start = mt76x0_sw_scan,
398 .sw_scan_complete = mt76x0_sw_scan_complete,
399 .ampdu_action = mt76_ampdu_action,
400 .sta_rate_tbl_update = mt76_sta_rate_tbl_update,
401 .set_rts_threshold = mt76x0_set_rts_threshold,