2 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <linux/module.h>
21 #define CCK_RATE(_idx, _rate) { \
23 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
24 .hw_value = (MT_PHY_TYPE_CCK << 8) | _idx, \
25 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx), \
28 #define OFDM_RATE(_idx, _rate) { \
30 .hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx, \
31 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx, \
34 struct ieee80211_rate mt76x02_rates[] = {
48 EXPORT_SYMBOL_GPL(mt76x02_rates);
50 void mt76x02_configure_filter(struct ieee80211_hw *hw,
51 unsigned int changed_flags,
52 unsigned int *total_flags, u64 multicast)
54 struct mt76x02_dev *dev = hw->priv;
57 #define MT76_FILTER(_flag, _hw) do { \
58 flags |= *total_flags & FIF_##_flag; \
59 dev->mt76.rxfilter &= ~(_hw); \
60 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
63 mutex_lock(&dev->mt76.mutex);
65 dev->mt76.rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
67 MT76_FILTER(FCSFAIL, MT_RX_FILTR_CFG_CRC_ERR);
68 MT76_FILTER(PLCPFAIL, MT_RX_FILTR_CFG_PHY_ERR);
69 MT76_FILTER(CONTROL, MT_RX_FILTR_CFG_ACK |
71 MT_RX_FILTR_CFG_CFEND |
72 MT_RX_FILTR_CFG_CFACK |
74 MT_RX_FILTR_CFG_CTRL_RSV);
75 MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
78 mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
80 mutex_unlock(&dev->mt76.mutex);
82 EXPORT_SYMBOL_GPL(mt76x02_configure_filter);
84 int mt76x02_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
85 struct ieee80211_sta *sta)
87 struct mt76x02_dev *dev = hw->priv;
88 struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
89 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
94 mutex_lock(&dev->mt76.mutex);
96 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, ARRAY_SIZE(dev->mt76.wcid));
104 msta->wcid.idx = idx;
105 msta->wcid.hw_key_idx = -1;
106 mt76x02_mac_wcid_setup(dev, idx, mvif->idx, sta->addr);
107 mt76x02_mac_wcid_set_drop(dev, idx, false);
108 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
109 mt76x02_txq_init(dev, sta->txq[i]);
111 if (vif->type == NL80211_IFTYPE_AP)
112 set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
114 ewma_signal_init(&msta->rssi);
116 rcu_assign_pointer(dev->mt76.wcid[idx], &msta->wcid);
119 mutex_unlock(&dev->mt76.mutex);
123 EXPORT_SYMBOL_GPL(mt76x02_sta_add);
125 int mt76x02_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
126 struct ieee80211_sta *sta)
128 struct mt76x02_dev *dev = hw->priv;
129 struct mt76x02_sta *msta = (struct mt76x02_sta *)sta->drv_priv;
130 int idx = msta->wcid.idx;
133 mutex_lock(&dev->mt76.mutex);
134 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
135 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
136 mt76_txq_remove(&dev->mt76, sta->txq[i]);
137 mt76x02_mac_wcid_set_drop(dev, idx, true);
138 mt76_wcid_free(dev->mt76.wcid_mask, idx);
139 mt76x02_mac_wcid_setup(dev, idx, 0, NULL);
140 mutex_unlock(&dev->mt76.mutex);
144 EXPORT_SYMBOL_GPL(mt76x02_sta_remove);
146 void mt76x02_vif_init(struct mt76x02_dev *dev, struct ieee80211_vif *vif,
149 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
152 mvif->group_wcid.idx = MT_VIF_WCID(idx);
153 mvif->group_wcid.hw_key_idx = -1;
154 mt76x02_txq_init(dev, vif->txq);
156 EXPORT_SYMBOL_GPL(mt76x02_vif_init);
159 mt76x02_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
161 struct mt76x02_dev *dev = hw->priv;
162 unsigned int idx = 0;
164 if (vif->addr[0] & BIT(1))
165 idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7);
168 * Client mode typically only has one configurable BSSID register,
169 * which is used for bssidx=0. This is linked to the MAC address.
170 * Since mac80211 allows changing interface types, and we cannot
171 * force the use of the primary MAC address for a station mode
172 * interface, we need some other way of configuring a per-interface
174 * The hardware provides an AP-Client feature, where bssidx 0-7 are
175 * used for AP mode and bssidx 8-15 for client mode.
176 * We shift the station interface bss index by 8 to force the
177 * hardware to recognize the BSSID.
178 * The resulting bssidx mismatch for unicast frames is ignored by hw.
180 if (vif->type == NL80211_IFTYPE_STATION)
183 mt76x02_vif_init(dev, vif, idx);
186 EXPORT_SYMBOL_GPL(mt76x02_add_interface);
188 void mt76x02_remove_interface(struct ieee80211_hw *hw,
189 struct ieee80211_vif *vif)
191 struct mt76x02_dev *dev = hw->priv;
193 mt76_txq_remove(&dev->mt76, vif->txq);
195 EXPORT_SYMBOL_GPL(mt76x02_remove_interface);
197 int mt76x02_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
198 struct ieee80211_ampdu_params *params)
200 enum ieee80211_ampdu_mlme_action action = params->action;
201 struct ieee80211_sta *sta = params->sta;
202 struct mt76x02_dev *dev = hw->priv;
203 struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
204 struct ieee80211_txq *txq = sta->txq[params->tid];
205 u16 tid = params->tid;
206 u16 *ssn = ¶ms->ssn;
207 struct mt76_txq *mtxq;
212 mtxq = (struct mt76_txq *)txq->drv_priv;
215 case IEEE80211_AMPDU_RX_START:
216 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid,
217 *ssn, params->buf_size);
218 mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
220 case IEEE80211_AMPDU_RX_STOP:
221 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
222 mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4,
225 case IEEE80211_AMPDU_TX_OPERATIONAL:
227 mtxq->send_bar = false;
228 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
230 case IEEE80211_AMPDU_TX_STOP_FLUSH:
231 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
233 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
235 case IEEE80211_AMPDU_TX_START:
236 mtxq->agg_ssn = *ssn << 4;
237 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
239 case IEEE80211_AMPDU_TX_STOP_CONT:
241 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
247 EXPORT_SYMBOL_GPL(mt76x02_ampdu_action);
249 int mt76x02_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
250 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
251 struct ieee80211_key_conf *key)
253 struct mt76x02_dev *dev = hw->priv;
254 struct mt76x02_vif *mvif = (struct mt76x02_vif *)vif->drv_priv;
255 struct mt76x02_sta *msta;
256 struct mt76_wcid *wcid;
257 int idx = key->keyidx;
260 /* fall back to sw encryption for unsupported ciphers */
261 switch (key->cipher) {
262 case WLAN_CIPHER_SUITE_WEP40:
263 case WLAN_CIPHER_SUITE_WEP104:
264 case WLAN_CIPHER_SUITE_TKIP:
265 case WLAN_CIPHER_SUITE_CCMP:
272 * The hardware does not support per-STA RX GTK, fall back
273 * to software mode for these.
275 if ((vif->type == NL80211_IFTYPE_ADHOC ||
276 vif->type == NL80211_IFTYPE_MESH_POINT) &&
277 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
278 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
279 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
282 msta = sta ? (struct mt76x02_sta *) sta->drv_priv : NULL;
283 wcid = msta ? &msta->wcid : &mvif->group_wcid;
285 if (cmd == SET_KEY) {
286 key->hw_key_idx = wcid->idx;
287 wcid->hw_key_idx = idx;
288 if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
289 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
293 if (idx == wcid->hw_key_idx) {
294 wcid->hw_key_idx = -1;
300 mt76_wcid_key_setup(&dev->mt76, wcid, key);
303 if (key || wcid->hw_key_idx == idx) {
304 ret = mt76x02_mac_wcid_set_key(dev, wcid->idx, key);
309 return mt76x02_mac_shared_key_setup(dev, mvif->idx, idx, key);
312 return mt76x02_mac_wcid_set_key(dev, msta->wcid.idx, key);
314 EXPORT_SYMBOL_GPL(mt76x02_set_key);
316 int mt76x02_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
317 u16 queue, const struct ieee80211_tx_queue_params *params)
319 struct mt76x02_dev *dev = hw->priv;
320 u8 cw_min = 5, cw_max = 10, qid;
323 qid = dev->mt76.q_tx[queue].hw_idx;
326 cw_min = fls(params->cw_min);
328 cw_max = fls(params->cw_max);
330 val = FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop) |
331 FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
332 FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
333 FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
334 mt76_wr(dev, MT_EDCA_CFG_AC(qid), val);
336 val = mt76_rr(dev, MT_WMM_TXOP(qid));
337 val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(qid));
338 val |= params->txop << MT_WMM_TXOP_SHIFT(qid);
339 mt76_wr(dev, MT_WMM_TXOP(qid), val);
341 val = mt76_rr(dev, MT_WMM_AIFSN);
342 val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(qid));
343 val |= params->aifs << MT_WMM_AIFSN_SHIFT(qid);
344 mt76_wr(dev, MT_WMM_AIFSN, val);
346 val = mt76_rr(dev, MT_WMM_CWMIN);
347 val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(qid));
348 val |= cw_min << MT_WMM_CWMIN_SHIFT(qid);
349 mt76_wr(dev, MT_WMM_CWMIN, val);
351 val = mt76_rr(dev, MT_WMM_CWMAX);
352 val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(qid));
353 val |= cw_max << MT_WMM_CWMAX_SHIFT(qid);
354 mt76_wr(dev, MT_WMM_CWMAX, val);
358 EXPORT_SYMBOL_GPL(mt76x02_conf_tx);
360 void mt76x02_sta_rate_tbl_update(struct ieee80211_hw *hw,
361 struct ieee80211_vif *vif,
362 struct ieee80211_sta *sta)
364 struct mt76x02_dev *dev = hw->priv;
365 struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
366 struct ieee80211_sta_rates *rates = rcu_dereference(sta->rates);
367 struct ieee80211_tx_rate rate = {};
372 rate.idx = rates->rate[0].idx;
373 rate.flags = rates->rate[0].flags;
374 mt76x02_mac_wcid_set_rate(dev, &msta->wcid, &rate);
375 msta->wcid.max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, &rate);
377 EXPORT_SYMBOL_GPL(mt76x02_sta_rate_tbl_update);
379 int mt76x02_insert_hdr_pad(struct sk_buff *skb)
381 int len = ieee80211_get_hdrlen_from_skb(skb);
387 memmove(skb->data, skb->data + 2, len);
390 skb->data[len + 1] = 0;
393 EXPORT_SYMBOL_GPL(mt76x02_insert_hdr_pad);
395 void mt76x02_remove_hdr_pad(struct sk_buff *skb, int len)
402 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
403 memmove(skb->data + len, skb->data, hdrlen);
406 EXPORT_SYMBOL_GPL(mt76x02_remove_hdr_pad);
408 const u16 mt76x02_beacon_offsets[16] = {
409 /* 1024 byte per beacon */
418 /* BSS idx 8-15 not used for beacons */
428 EXPORT_SYMBOL_GPL(mt76x02_beacon_offsets);
430 void mt76x02_set_beacon_offsets(struct mt76x02_dev *dev)
432 u16 val, base = MT_BEACON_BASE;
436 for (i = 0; i < 16; i++) {
437 val = mt76x02_beacon_offsets[i] - base;
438 regs[i / 4] |= (val / 64) << (8 * (i % 4));
441 for (i = 0; i < 4; i++)
442 mt76_wr(dev, MT_BCN_OFFSET(i), regs[i]);
444 EXPORT_SYMBOL_GPL(mt76x02_set_beacon_offsets);
446 MODULE_LICENSE("Dual BSD/GPL");