treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt76 / mt7615 / main.c
blob070b0340389436e76f76308920e36741ec0c3d4a
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
4 * Author: Roy Luo <royluo@google.com>
5 * Ryder Lee <ryder.lee@mediatek.com>
6 * Felix Fietkau <nbd@nbd.name>
7 */
9 #include <linux/etherdevice.h>
10 #include <linux/platform_device.h>
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include "mt7615.h"
15 static int mt7615_start(struct ieee80211_hw *hw)
17 struct mt7615_dev *dev = hw->priv;
19 mt7615_mac_reset_counters(dev);
21 dev->mt76.survey_time = ktime_get_boottime();
22 set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
23 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
24 MT7615_WATCHDOG_TIME);
26 return 0;
29 static void mt7615_stop(struct ieee80211_hw *hw)
31 struct mt7615_dev *dev = hw->priv;
33 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
34 cancel_delayed_work_sync(&dev->mt76.mac_work);
37 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
39 int i;
41 switch (type) {
42 case NL80211_IFTYPE_AP:
43 case NL80211_IFTYPE_MESH_POINT:
44 case NL80211_IFTYPE_ADHOC:
45 /* ap use hw bssid 0 and ext bssid */
46 if (~mask & BIT(HW_BSSID_0))
47 return HW_BSSID_0;
49 for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
50 if (~mask & BIT(i))
51 return i;
53 break;
54 case NL80211_IFTYPE_STATION:
55 /* sta use hw bssid other than 0 */
56 for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
57 if (~mask & BIT(i))
58 return i;
60 break;
61 default:
62 WARN_ON(1);
63 break;
66 return -1;
69 static int mt7615_add_interface(struct ieee80211_hw *hw,
70 struct ieee80211_vif *vif)
72 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
73 struct mt7615_dev *dev = hw->priv;
74 struct mt76_txq *mtxq;
75 int idx, ret = 0;
77 mutex_lock(&dev->mt76.mutex);
79 mvif->idx = ffs(~dev->vif_mask) - 1;
80 if (mvif->idx >= MT7615_MAX_INTERFACES) {
81 ret = -ENOSPC;
82 goto out;
85 idx = get_omac_idx(vif->type, dev->omac_mask);
86 if (idx < 0) {
87 ret = -ENOSPC;
88 goto out;
90 mvif->omac_idx = idx;
92 /* TODO: DBDC support. Use band 0 for now */
93 mvif->band_idx = 0;
94 mvif->wmm_idx = mvif->idx % MT7615_MAX_WMM_SETS;
96 ret = mt7615_mcu_set_dev_info(dev, vif, 1);
97 if (ret)
98 goto out;
100 dev->vif_mask |= BIT(mvif->idx);
101 dev->omac_mask |= BIT(mvif->omac_idx);
102 idx = MT7615_WTBL_RESERVED - mvif->idx;
104 INIT_LIST_HEAD(&mvif->sta.poll_list);
105 mvif->sta.wcid.idx = idx;
106 mvif->sta.wcid.hw_key_idx = -1;
107 mt7615_mac_wtbl_update(dev, idx,
108 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
110 rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
111 mtxq = (struct mt76_txq *)vif->txq->drv_priv;
112 mtxq->wcid = &mvif->sta.wcid;
113 mt76_txq_init(&dev->mt76, vif->txq);
115 out:
116 mutex_unlock(&dev->mt76.mutex);
118 return ret;
121 static void mt7615_remove_interface(struct ieee80211_hw *hw,
122 struct ieee80211_vif *vif)
124 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
125 struct mt7615_sta *msta = &mvif->sta;
126 struct mt7615_dev *dev = hw->priv;
127 int idx = msta->wcid.idx;
129 /* TODO: disable beacon for the bss */
131 mt7615_mcu_set_dev_info(dev, vif, 0);
133 rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
134 mt76_txq_remove(&dev->mt76, vif->txq);
136 mutex_lock(&dev->mt76.mutex);
137 dev->vif_mask &= ~BIT(mvif->idx);
138 dev->omac_mask &= ~BIT(mvif->omac_idx);
139 mutex_unlock(&dev->mt76.mutex);
141 spin_lock_bh(&dev->sta_poll_lock);
142 if (!list_empty(&msta->poll_list))
143 list_del_init(&msta->poll_list);
144 spin_unlock_bh(&dev->sta_poll_lock);
147 static int mt7615_set_channel(struct mt7615_dev *dev)
149 int ret;
151 cancel_delayed_work_sync(&dev->mt76.mac_work);
153 mutex_lock(&dev->mt76.mutex);
154 set_bit(MT76_RESET, &dev->mt76.state);
156 mt7615_dfs_check_channel(dev);
158 mt76_set_channel(&dev->mt76);
160 ret = mt7615_mcu_set_channel(dev);
161 if (ret)
162 goto out;
164 ret = mt7615_dfs_init_radar_detector(dev);
165 mt7615_mac_cca_stats_reset(dev);
166 dev->mt76.survey_time = ktime_get_boottime();
168 mt7615_mac_reset_counters(dev);
170 out:
171 clear_bit(MT76_RESET, &dev->mt76.state);
172 mutex_unlock(&dev->mt76.mutex);
174 mt76_txq_schedule_all(&dev->mt76);
175 ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
176 MT7615_WATCHDOG_TIME);
177 return ret;
180 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
181 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
182 struct ieee80211_key_conf *key)
184 struct mt7615_dev *dev = hw->priv;
185 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
186 struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
187 &mvif->sta;
188 struct mt76_wcid *wcid = &msta->wcid;
189 int idx = key->keyidx;
191 /* The hardware does not support per-STA RX GTK, fallback
192 * to software mode for these.
194 if ((vif->type == NL80211_IFTYPE_ADHOC ||
195 vif->type == NL80211_IFTYPE_MESH_POINT) &&
196 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
197 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
198 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
199 return -EOPNOTSUPP;
201 /* fall back to sw encryption for unsupported ciphers */
202 switch (key->cipher) {
203 case WLAN_CIPHER_SUITE_AES_CMAC:
204 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIE;
205 break;
206 case WLAN_CIPHER_SUITE_WEP40:
207 case WLAN_CIPHER_SUITE_WEP104:
208 case WLAN_CIPHER_SUITE_TKIP:
209 case WLAN_CIPHER_SUITE_CCMP:
210 case WLAN_CIPHER_SUITE_CCMP_256:
211 case WLAN_CIPHER_SUITE_GCMP:
212 case WLAN_CIPHER_SUITE_GCMP_256:
213 case WLAN_CIPHER_SUITE_SMS4:
214 break;
215 default:
216 return -EOPNOTSUPP;
219 if (cmd == SET_KEY) {
220 key->hw_key_idx = wcid->idx;
221 wcid->hw_key_idx = idx;
222 } else if (idx == wcid->hw_key_idx) {
223 wcid->hw_key_idx = -1;
225 mt76_wcid_key_setup(&dev->mt76, wcid,
226 cmd == SET_KEY ? key : NULL);
228 return mt7615_mac_wtbl_set_key(dev, wcid, key, cmd);
231 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
233 struct mt7615_dev *dev = hw->priv;
234 int ret = 0;
236 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
237 ieee80211_stop_queues(hw);
238 ret = mt7615_set_channel(dev);
239 ieee80211_wake_queues(hw);
242 mutex_lock(&dev->mt76.mutex);
244 if (changed & IEEE80211_CONF_CHANGE_POWER)
245 ret = mt7615_mcu_set_tx_power(dev);
247 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
248 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
249 dev->mt76.rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
250 else
251 dev->mt76.rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
253 mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
256 mutex_unlock(&dev->mt76.mutex);
258 return ret;
261 static int
262 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
263 const struct ieee80211_tx_queue_params *params)
265 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
266 struct mt7615_dev *dev = hw->priv;
268 queue += mvif->wmm_idx * MT7615_MAX_WMM_SETS;
270 return mt7615_mcu_set_wmm(dev, queue, params);
273 static void mt7615_configure_filter(struct ieee80211_hw *hw,
274 unsigned int changed_flags,
275 unsigned int *total_flags,
276 u64 multicast)
278 struct mt7615_dev *dev = hw->priv;
279 u32 ctl_flags = MT_WF_RFCR1_DROP_ACK |
280 MT_WF_RFCR1_DROP_BF_POLL |
281 MT_WF_RFCR1_DROP_BA |
282 MT_WF_RFCR1_DROP_CFEND |
283 MT_WF_RFCR1_DROP_CFACK;
284 u32 flags = 0;
286 #define MT76_FILTER(_flag, _hw) do { \
287 flags |= *total_flags & FIF_##_flag; \
288 dev->mt76.rxfilter &= ~(_hw); \
289 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw); \
290 } while (0)
292 dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
293 MT_WF_RFCR_DROP_OTHER_BEACON |
294 MT_WF_RFCR_DROP_FRAME_REPORT |
295 MT_WF_RFCR_DROP_PROBEREQ |
296 MT_WF_RFCR_DROP_MCAST_FILTERED |
297 MT_WF_RFCR_DROP_MCAST |
298 MT_WF_RFCR_DROP_BCAST |
299 MT_WF_RFCR_DROP_DUPLICATE |
300 MT_WF_RFCR_DROP_A2_BSSID |
301 MT_WF_RFCR_DROP_UNWANTED_CTL |
302 MT_WF_RFCR_DROP_STBC_MULTI);
304 MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
305 MT_WF_RFCR_DROP_A3_MAC |
306 MT_WF_RFCR_DROP_A3_BSSID);
308 MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
310 MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
311 MT_WF_RFCR_DROP_RTS |
312 MT_WF_RFCR_DROP_CTL_RSV |
313 MT_WF_RFCR_DROP_NDPA);
315 *total_flags = flags;
316 mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
318 if (*total_flags & FIF_CONTROL)
319 mt76_clear(dev, MT_WF_RFCR1, ctl_flags);
320 else
321 mt76_set(dev, MT_WF_RFCR1, ctl_flags);
324 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
325 struct ieee80211_vif *vif,
326 struct ieee80211_bss_conf *info,
327 u32 changed)
329 struct mt7615_dev *dev = hw->priv;
331 mutex_lock(&dev->mt76.mutex);
333 if (changed & BSS_CHANGED_ASSOC)
334 mt7615_mcu_set_bss_info(dev, vif, info->assoc);
336 /* TODO: update beacon content
337 * BSS_CHANGED_BEACON
340 if (changed & BSS_CHANGED_BEACON_ENABLED) {
341 mt7615_mcu_set_bss_info(dev, vif, info->enable_beacon);
342 mt7615_mcu_wtbl_bmc(dev, vif, info->enable_beacon);
343 mt7615_mcu_set_sta_rec_bmc(dev, vif, info->enable_beacon);
344 mt7615_mcu_set_bcn(dev, vif, info->enable_beacon);
347 mutex_unlock(&dev->mt76.mutex);
350 static void
351 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
352 struct ieee80211_vif *vif,
353 struct cfg80211_chan_def *chandef)
355 struct mt7615_dev *dev = hw->priv;
357 mutex_lock(&dev->mt76.mutex);
358 mt7615_mcu_set_bcn(dev, vif, true);
359 mutex_unlock(&dev->mt76.mutex);
362 int mt7615_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
363 struct ieee80211_sta *sta)
365 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
366 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
367 struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
368 int idx;
370 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
371 if (idx < 0)
372 return -ENOSPC;
374 INIT_LIST_HEAD(&msta->poll_list);
375 msta->vif = mvif;
376 msta->wcid.sta = 1;
377 msta->wcid.idx = idx;
378 mt7615_mac_wtbl_update(dev, idx,
379 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
381 mt7615_mcu_add_wtbl(dev, vif, sta);
382 mt7615_mcu_set_sta_rec(dev, vif, sta, 1);
384 return 0;
387 void mt7615_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
388 struct ieee80211_sta *sta)
390 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
392 if (sta->ht_cap.ht_supported)
393 mt7615_mcu_set_ht_cap(dev, vif, sta);
396 void mt7615_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
397 struct ieee80211_sta *sta)
399 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
400 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
402 mt7615_mcu_set_sta_rec(dev, vif, sta, 0);
403 mt7615_mcu_del_wtbl(dev, sta);
405 mt7615_mac_wtbl_update(dev, msta->wcid.idx,
406 MT_WTBL_UPDATE_ADM_COUNT_CLEAR);
408 spin_lock_bh(&dev->sta_poll_lock);
409 if (!list_empty(&msta->poll_list))
410 list_del_init(&msta->poll_list);
411 spin_unlock_bh(&dev->sta_poll_lock);
414 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
415 struct ieee80211_vif *vif,
416 struct ieee80211_sta *sta)
418 struct mt7615_dev *dev = hw->priv;
419 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
420 struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
421 int i;
423 spin_lock_bh(&dev->mt76.lock);
424 for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
425 msta->rates[i].idx = sta_rates->rate[i].idx;
426 msta->rates[i].count = sta_rates->rate[i].count;
427 msta->rates[i].flags = sta_rates->rate[i].flags;
429 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
430 break;
432 msta->n_rates = i;
433 mt7615_mac_set_rates(dev, msta, NULL, msta->rates);
434 msta->rate_probe = false;
435 spin_unlock_bh(&dev->mt76.lock);
438 static void mt7615_tx(struct ieee80211_hw *hw,
439 struct ieee80211_tx_control *control,
440 struct sk_buff *skb)
442 struct mt7615_dev *dev = hw->priv;
443 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
444 struct ieee80211_vif *vif = info->control.vif;
445 struct mt76_wcid *wcid = &dev->mt76.global_wcid;
447 if (control->sta) {
448 struct mt7615_sta *sta;
450 sta = (struct mt7615_sta *)control->sta->drv_priv;
451 wcid = &sta->wcid;
454 if (vif && !control->sta) {
455 struct mt7615_vif *mvif;
457 mvif = (struct mt7615_vif *)vif->drv_priv;
458 wcid = &mvif->sta.wcid;
461 mt76_tx(&dev->mt76, control->sta, wcid, skb);
464 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
466 struct mt7615_dev *dev = hw->priv;
468 mutex_lock(&dev->mt76.mutex);
469 mt7615_mcu_set_rts_thresh(dev, val);
470 mutex_unlock(&dev->mt76.mutex);
472 return 0;
475 static int
476 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
477 struct ieee80211_ampdu_params *params)
479 enum ieee80211_ampdu_mlme_action action = params->action;
480 struct mt7615_dev *dev = hw->priv;
481 struct ieee80211_sta *sta = params->sta;
482 struct ieee80211_txq *txq = sta->txq[params->tid];
483 struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
484 u16 tid = params->tid;
485 u16 ssn = params->ssn;
486 struct mt76_txq *mtxq;
487 int ret = 0;
489 if (!txq)
490 return -EINVAL;
492 mtxq = (struct mt76_txq *)txq->drv_priv;
494 mutex_lock(&dev->mt76.mutex);
495 switch (action) {
496 case IEEE80211_AMPDU_RX_START:
497 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, ssn,
498 params->buf_size);
499 mt7615_mcu_set_rx_ba(dev, params, 1);
500 break;
501 case IEEE80211_AMPDU_RX_STOP:
502 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
503 mt7615_mcu_set_rx_ba(dev, params, 0);
504 break;
505 case IEEE80211_AMPDU_TX_OPERATIONAL:
506 mtxq->aggr = true;
507 mtxq->send_bar = false;
508 mt7615_mcu_set_tx_ba(dev, params, 1);
509 break;
510 case IEEE80211_AMPDU_TX_STOP_FLUSH:
511 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
512 mtxq->aggr = false;
513 mt7615_mcu_set_tx_ba(dev, params, 0);
514 break;
515 case IEEE80211_AMPDU_TX_START:
516 mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(ssn);
517 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
518 break;
519 case IEEE80211_AMPDU_TX_STOP_CONT:
520 mtxq->aggr = false;
521 mt7615_mcu_set_tx_ba(dev, params, 0);
522 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
523 break;
525 mutex_unlock(&dev->mt76.mutex);
527 return ret;
530 const struct ieee80211_ops mt7615_ops = {
531 .tx = mt7615_tx,
532 .start = mt7615_start,
533 .stop = mt7615_stop,
534 .add_interface = mt7615_add_interface,
535 .remove_interface = mt7615_remove_interface,
536 .config = mt7615_config,
537 .conf_tx = mt7615_conf_tx,
538 .configure_filter = mt7615_configure_filter,
539 .bss_info_changed = mt7615_bss_info_changed,
540 .sta_state = mt76_sta_state,
541 .set_key = mt7615_set_key,
542 .ampdu_action = mt7615_ampdu_action,
543 .set_rts_threshold = mt7615_set_rts_threshold,
544 .wake_tx_queue = mt76_wake_tx_queue,
545 .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
546 .sw_scan_start = mt76_sw_scan,
547 .sw_scan_complete = mt76_sw_scan_complete,
548 .release_buffered_frames = mt76_release_buffered_frames,
549 .get_txpower = mt76_get_txpower,
550 .channel_switch_beacon = mt7615_channel_switch_beacon,
551 .get_survey = mt76_get_survey,
552 .get_antenna = mt76_get_antenna,