xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x02_util.c (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
1 /*
2  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  *
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.
8  *
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.
16  */
17 
18 #include <linux/module.h>
19 #include "mt76x02.h"
20 
21 #define CCK_RATE(_idx, _rate) {					\
22 	.bitrate = _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),	\
26 }
27 
28 #define OFDM_RATE(_idx, _rate) {				\
29 	.bitrate = _rate,					\
30 	.hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx,		\
31 	.hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx,	\
32 }
33 
34 struct ieee80211_rate mt76x02_rates[] = {
35 	CCK_RATE(0, 10),
36 	CCK_RATE(1, 20),
37 	CCK_RATE(2, 55),
38 	CCK_RATE(3, 110),
39 	OFDM_RATE(0, 60),
40 	OFDM_RATE(1, 90),
41 	OFDM_RATE(2, 120),
42 	OFDM_RATE(3, 180),
43 	OFDM_RATE(4, 240),
44 	OFDM_RATE(5, 360),
45 	OFDM_RATE(6, 480),
46 	OFDM_RATE(7, 540),
47 };
48 EXPORT_SYMBOL_GPL(mt76x02_rates);
49 
50 void mt76x02_configure_filter(struct ieee80211_hw *hw,
51 			     unsigned int changed_flags,
52 			     unsigned int *total_flags, u64 multicast)
53 {
54 	struct mt76_dev *dev = hw->priv;
55 	u32 flags = 0;
56 
57 #define MT76_FILTER(_flag, _hw) do { \
58 		flags |= *total_flags & FIF_##_flag;			\
59 		dev->rxfilter &= ~(_hw);				\
60 		dev->rxfilter |= !(flags & FIF_##_flag) * (_hw);	\
61 	} while (0)
62 
63 	mutex_lock(&dev->mutex);
64 
65 	dev->rxfilter &= ~MT_RX_FILTR_CFG_OTHER_BSS;
66 
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 |
70 			     MT_RX_FILTR_CFG_CTS |
71 			     MT_RX_FILTR_CFG_CFEND |
72 			     MT_RX_FILTR_CFG_CFACK |
73 			     MT_RX_FILTR_CFG_BA |
74 			     MT_RX_FILTR_CFG_CTRL_RSV);
75 	MT76_FILTER(PSPOLL, MT_RX_FILTR_CFG_PSPOLL);
76 
77 	*total_flags = flags;
78 	dev->bus->wr(dev, MT_RX_FILTR_CFG, dev->rxfilter);
79 
80 	mutex_unlock(&dev->mutex);
81 }
82 EXPORT_SYMBOL_GPL(mt76x02_configure_filter);
83 
84 int mt76x02_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
85 		   struct ieee80211_sta *sta)
86 {
87 	struct mt76_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;
90 	int ret = 0;
91 	int idx = 0;
92 	int i;
93 
94 	mutex_lock(&dev->mutex);
95 
96 	idx = mt76_wcid_alloc(dev->wcid_mask, ARRAY_SIZE(dev->wcid));
97 	if (idx < 0) {
98 		ret = -ENOSPC;
99 		goto out;
100 	}
101 
102 	msta->vif = mvif;
103 	msta->wcid.sta = 1;
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]);
110 
111 	if (vif->type == NL80211_IFTYPE_AP)
112 		set_bit(MT_WCID_FLAG_CHECK_PS, &msta->wcid.flags);
113 
114 	ewma_signal_init(&msta->rssi);
115 
116 	rcu_assign_pointer(dev->wcid[idx], &msta->wcid);
117 
118 out:
119 	mutex_unlock(&dev->mutex);
120 
121 	return ret;
122 }
123 EXPORT_SYMBOL_GPL(mt76x02_sta_add);
124 
125 int mt76x02_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
126 		      struct ieee80211_sta *sta)
127 {
128 	struct mt76_dev *dev = hw->priv;
129 	struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
130 	int idx = msta->wcid.idx;
131 	int i;
132 
133 	mutex_lock(&dev->mutex);
134 	rcu_assign_pointer(dev->wcid[idx], NULL);
135 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
136 		mt76_txq_remove(dev, sta->txq[i]);
137 	mt76x02_mac_wcid_set_drop(dev, idx, true);
138 	mt76_wcid_free(dev->wcid_mask, idx);
139 	mt76x02_mac_wcid_setup(dev, idx, 0, NULL);
140 	mutex_unlock(&dev->mutex);
141 
142 	return 0;
143 }
144 EXPORT_SYMBOL_GPL(mt76x02_sta_remove);
145 
146 void mt76x02_vif_init(struct mt76_dev *dev, struct ieee80211_vif *vif,
147 		     unsigned int idx)
148 {
149 	struct mt76x02_vif *mvif = (struct mt76x02_vif *) vif->drv_priv;
150 
151 	mvif->idx = idx;
152 	mvif->group_wcid.idx = MT_VIF_WCID(idx);
153 	mvif->group_wcid.hw_key_idx = -1;
154 	mt76x02_txq_init(dev, vif->txq);
155 }
156 EXPORT_SYMBOL_GPL(mt76x02_vif_init);
157 
158 int
159 mt76x02_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
160 {
161 	struct mt76_dev *dev = hw->priv;
162 	unsigned int idx = 0;
163 
164 	if (vif->addr[0] & BIT(1))
165 		idx = 1 + (((dev->macaddr[0] ^ vif->addr[0]) >> 2) & 7);
166 
167 	/*
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
173 	 * remote BSSID.
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.
179 	 */
180 	if (vif->type == NL80211_IFTYPE_STATION)
181 		idx += 8;
182 
183 	mt76x02_vif_init(dev, vif, idx);
184 	return 0;
185 }
186 EXPORT_SYMBOL_GPL(mt76x02_add_interface);
187 
188 void mt76x02_remove_interface(struct ieee80211_hw *hw,
189 			     struct ieee80211_vif *vif)
190 {
191 	struct mt76_dev *dev = hw->priv;
192 
193 	mt76_txq_remove(dev, vif->txq);
194 }
195 EXPORT_SYMBOL_GPL(mt76x02_remove_interface);
196 
197 int mt76x02_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
198 			struct ieee80211_ampdu_params *params)
199 {
200 	enum ieee80211_ampdu_mlme_action action = params->action;
201 	struct ieee80211_sta *sta = params->sta;
202 	struct mt76_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 = &params->ssn;
207 	struct mt76_txq *mtxq;
208 
209 	if (!txq)
210 		return -EINVAL;
211 
212 	mtxq = (struct mt76_txq *)txq->drv_priv;
213 
214 	switch (action) {
215 	case IEEE80211_AMPDU_RX_START:
216 		mt76_rx_aggr_start(dev, &msta->wcid, tid, *ssn, params->buf_size);
217 		__mt76_set(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
218 		break;
219 	case IEEE80211_AMPDU_RX_STOP:
220 		mt76_rx_aggr_stop(dev, &msta->wcid, tid);
221 		__mt76_clear(dev, MT_WCID_ADDR(msta->wcid.idx) + 4, BIT(16 + tid));
222 		break;
223 	case IEEE80211_AMPDU_TX_OPERATIONAL:
224 		mtxq->aggr = true;
225 		mtxq->send_bar = false;
226 		ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
227 		break;
228 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
229 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
230 		mtxq->aggr = false;
231 		ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
232 		break;
233 	case IEEE80211_AMPDU_TX_START:
234 		mtxq->agg_ssn = *ssn << 4;
235 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
236 		break;
237 	case IEEE80211_AMPDU_TX_STOP_CONT:
238 		mtxq->aggr = false;
239 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
240 		break;
241 	}
242 
243 	return 0;
244 }
245 EXPORT_SYMBOL_GPL(mt76x02_ampdu_action);
246 
247 int mt76x02_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
248 		   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
249 		   struct ieee80211_key_conf *key)
250 {
251 	struct mt76_dev *dev = hw->priv;
252 	struct mt76x02_vif *mvif = (struct mt76x02_vif *) vif->drv_priv;
253 	struct mt76x02_sta *msta;
254 	struct mt76_wcid *wcid;
255 	int idx = key->keyidx;
256 	int ret;
257 
258 	/* fall back to sw encryption for unsupported ciphers */
259 	switch (key->cipher) {
260 	case WLAN_CIPHER_SUITE_WEP40:
261 	case WLAN_CIPHER_SUITE_WEP104:
262 	case WLAN_CIPHER_SUITE_TKIP:
263 	case WLAN_CIPHER_SUITE_CCMP:
264 		break;
265 	default:
266 		return -EOPNOTSUPP;
267 	}
268 
269 	/*
270 	 * The hardware does not support per-STA RX GTK, fall back
271 	 * to software mode for these.
272 	 */
273 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
274 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
275 	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
276 	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
277 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
278 		return -EOPNOTSUPP;
279 
280 	msta = sta ? (struct mt76x02_sta *) sta->drv_priv : NULL;
281 	wcid = msta ? &msta->wcid : &mvif->group_wcid;
282 
283 	if (cmd == SET_KEY) {
284 		key->hw_key_idx = wcid->idx;
285 		wcid->hw_key_idx = idx;
286 		if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
287 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
288 			wcid->sw_iv = true;
289 		}
290 	} else {
291 		if (idx == wcid->hw_key_idx) {
292 			wcid->hw_key_idx = -1;
293 			wcid->sw_iv = true;
294 		}
295 
296 		key = NULL;
297 	}
298 	mt76_wcid_key_setup(dev, wcid, key);
299 
300 	if (!msta) {
301 		if (key || wcid->hw_key_idx == idx) {
302 			ret = mt76x02_mac_wcid_set_key(dev, wcid->idx, key);
303 			if (ret)
304 				return ret;
305 		}
306 
307 		return mt76x02_mac_shared_key_setup(dev, mvif->idx, idx, key);
308 	}
309 
310 	return mt76x02_mac_wcid_set_key(dev, msta->wcid.idx, key);
311 }
312 EXPORT_SYMBOL_GPL(mt76x02_set_key);
313 
314 int mt76x02_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
315 		   u16 queue, const struct ieee80211_tx_queue_params *params)
316 {
317 	struct mt76_dev *dev = hw->priv;
318 	u8 cw_min = 5, cw_max = 10, qid;
319 	u32 val;
320 
321 	qid = dev->q_tx[queue].hw_idx;
322 
323 	if (params->cw_min)
324 		cw_min = fls(params->cw_min);
325 	if (params->cw_max)
326 		cw_max = fls(params->cw_max);
327 
328 	val = FIELD_PREP(MT_EDCA_CFG_TXOP, params->txop) |
329 	      FIELD_PREP(MT_EDCA_CFG_AIFSN, params->aifs) |
330 	      FIELD_PREP(MT_EDCA_CFG_CWMIN, cw_min) |
331 	      FIELD_PREP(MT_EDCA_CFG_CWMAX, cw_max);
332 	__mt76_wr(dev, MT_EDCA_CFG_AC(qid), val);
333 
334 	val = __mt76_rr(dev, MT_WMM_TXOP(qid));
335 	val &= ~(MT_WMM_TXOP_MASK << MT_WMM_TXOP_SHIFT(qid));
336 	val |= params->txop << MT_WMM_TXOP_SHIFT(qid);
337 	__mt76_wr(dev, MT_WMM_TXOP(qid), val);
338 
339 	val = __mt76_rr(dev, MT_WMM_AIFSN);
340 	val &= ~(MT_WMM_AIFSN_MASK << MT_WMM_AIFSN_SHIFT(qid));
341 	val |= params->aifs << MT_WMM_AIFSN_SHIFT(qid);
342 	__mt76_wr(dev, MT_WMM_AIFSN, val);
343 
344 	val = __mt76_rr(dev, MT_WMM_CWMIN);
345 	val &= ~(MT_WMM_CWMIN_MASK << MT_WMM_CWMIN_SHIFT(qid));
346 	val |= cw_min << MT_WMM_CWMIN_SHIFT(qid);
347 	__mt76_wr(dev, MT_WMM_CWMIN, val);
348 
349 	val = __mt76_rr(dev, MT_WMM_CWMAX);
350 	val &= ~(MT_WMM_CWMAX_MASK << MT_WMM_CWMAX_SHIFT(qid));
351 	val |= cw_max << MT_WMM_CWMAX_SHIFT(qid);
352 	__mt76_wr(dev, MT_WMM_CWMAX, val);
353 
354 	return 0;
355 }
356 EXPORT_SYMBOL_GPL(mt76x02_conf_tx);
357 
358 void mt76x02_sta_rate_tbl_update(struct ieee80211_hw *hw,
359 				struct ieee80211_vif *vif,
360 				struct ieee80211_sta *sta)
361 {
362 	struct mt76_dev *dev = hw->priv;
363 	struct mt76x02_sta *msta = (struct mt76x02_sta *) sta->drv_priv;
364 	struct ieee80211_sta_rates *rates = rcu_dereference(sta->rates);
365 	struct ieee80211_tx_rate rate = {};
366 
367 	if (!rates)
368 		return;
369 
370 	rate.idx = rates->rate[0].idx;
371 	rate.flags = rates->rate[0].flags;
372 	mt76x02_mac_wcid_set_rate(dev, &msta->wcid, &rate);
373 	msta->wcid.max_txpwr_adj = mt76x02_tx_get_max_txpwr_adj(dev, &rate);
374 }
375 EXPORT_SYMBOL_GPL(mt76x02_sta_rate_tbl_update);
376 
377 int mt76x02_insert_hdr_pad(struct sk_buff *skb)
378 {
379 	int len = ieee80211_get_hdrlen_from_skb(skb);
380 
381 	if (len % 4 == 0)
382 		return 0;
383 
384 	skb_push(skb, 2);
385 	memmove(skb->data, skb->data + 2, len);
386 
387 	skb->data[len] = 0;
388 	skb->data[len + 1] = 0;
389 	return 2;
390 }
391 EXPORT_SYMBOL_GPL(mt76x02_insert_hdr_pad);
392 
393 void mt76x02_remove_hdr_pad(struct sk_buff *skb, int len)
394 {
395 	int hdrlen;
396 
397 	if (!len)
398 		return;
399 
400 	hdrlen = ieee80211_get_hdrlen_from_skb(skb);
401 	memmove(skb->data + len, skb->data, hdrlen);
402 	skb_pull(skb, len);
403 }
404 EXPORT_SYMBOL_GPL(mt76x02_remove_hdr_pad);
405 
406 const u16 mt76x02_beacon_offsets[16] = {
407 	/* 1024 byte per beacon */
408 	0xc000,
409 	0xc400,
410 	0xc800,
411 	0xcc00,
412 	0xd000,
413 	0xd400,
414 	0xd800,
415 	0xdc00,
416 	/* BSS idx 8-15 not used for beacons */
417 	0xc000,
418 	0xc000,
419 	0xc000,
420 	0xc000,
421 	0xc000,
422 	0xc000,
423 	0xc000,
424 	0xc000,
425 };
426 EXPORT_SYMBOL_GPL(mt76x02_beacon_offsets);
427 
428 void mt76x02_set_beacon_offsets(struct mt76_dev *dev)
429 {
430 	u16 val, base = MT_BEACON_BASE;
431 	u32 regs[4] = {};
432 	int i;
433 
434 	for (i = 0; i < 16; i++) {
435 		val = mt76x02_beacon_offsets[i] - base;
436 		regs[i / 4] |= (val / 64) << (8 * (i % 4));
437 	}
438 
439 	for (i = 0; i < 4; i++)
440 		__mt76_wr(dev, MT_BCN_OFFSET(i), regs[i]);
441 }
442 EXPORT_SYMBOL_GPL(mt76x02_set_beacon_offsets);
443 
444 MODULE_LICENSE("Dual BSD/GPL");
445