]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/mediatek/mt76/mt7615/main.c
mt76: mt7615: move mt7615_mcu_set_rates to mac.c
[linux.git] / drivers / net / wireless / mediatek / mt76 / mt7615 / main.c
1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2019 MediaTek Inc.
3  *
4  * Author: Roy Luo <royluo@google.com>
5  *         Ryder Lee <ryder.lee@mediatek.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  */
8
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"
14
15 static int mt7615_start(struct ieee80211_hw *hw)
16 {
17         struct mt7615_dev *dev = hw->priv;
18
19         set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
20         ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
21                                      MT7615_WATCHDOG_TIME);
22
23         return 0;
24 }
25
26 static void mt7615_stop(struct ieee80211_hw *hw)
27 {
28         struct mt7615_dev *dev = hw->priv;
29
30         clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
31         cancel_delayed_work_sync(&dev->mt76.mac_work);
32 }
33
34 static int get_omac_idx(enum nl80211_iftype type, u32 mask)
35 {
36         int i;
37
38         switch (type) {
39         case NL80211_IFTYPE_AP:
40         case NL80211_IFTYPE_MESH_POINT:
41                 /* ap use hw bssid 0 and ext bssid */
42                 if (~mask & BIT(HW_BSSID_0))
43                         return HW_BSSID_0;
44
45                 for (i = EXT_BSSID_1; i < EXT_BSSID_END; i++)
46                         if (~mask & BIT(i))
47                                 return i;
48
49                 break;
50         case NL80211_IFTYPE_STATION:
51                 /* sta use hw bssid other than 0 */
52                 for (i = HW_BSSID_1; i < HW_BSSID_MAX; i++)
53                         if (~mask & BIT(i))
54                                 return i;
55
56                 break;
57         default:
58                 WARN_ON(1);
59                 break;
60         };
61
62         return -1;
63 }
64
65 static int mt7615_add_interface(struct ieee80211_hw *hw,
66                                 struct ieee80211_vif *vif)
67 {
68         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
69         struct mt7615_dev *dev = hw->priv;
70         struct mt76_txq *mtxq;
71         int idx, ret = 0;
72
73         mutex_lock(&dev->mt76.mutex);
74
75         mvif->idx = ffs(~dev->vif_mask) - 1;
76         if (mvif->idx >= MT7615_MAX_INTERFACES) {
77                 ret = -ENOSPC;
78                 goto out;
79         }
80
81         idx = get_omac_idx(vif->type, dev->omac_mask);
82         if (idx < 0) {
83                 ret = -ENOSPC;
84                 goto out;
85         }
86         mvif->omac_idx = idx;
87
88         /* TODO: DBDC support. Use band 0 and wmm 0 for now */
89         mvif->band_idx = 0;
90         mvif->wmm_idx = 0;
91
92         ret = mt7615_mcu_set_dev_info(dev, vif, 1);
93         if (ret)
94                 goto out;
95
96         dev->vif_mask |= BIT(mvif->idx);
97         dev->omac_mask |= BIT(mvif->omac_idx);
98         idx = MT7615_WTBL_RESERVED - mvif->idx;
99         mvif->sta.wcid.idx = idx;
100         mvif->sta.wcid.hw_key_idx = -1;
101
102         rcu_assign_pointer(dev->mt76.wcid[idx], &mvif->sta.wcid);
103         mtxq = (struct mt76_txq *)vif->txq->drv_priv;
104         mtxq->wcid = &mvif->sta.wcid;
105         mt76_txq_init(&dev->mt76, vif->txq);
106
107 out:
108         mutex_unlock(&dev->mt76.mutex);
109
110         return ret;
111 }
112
113 static void mt7615_remove_interface(struct ieee80211_hw *hw,
114                                     struct ieee80211_vif *vif)
115 {
116         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
117         struct mt7615_dev *dev = hw->priv;
118         int idx = mvif->sta.wcid.idx;
119
120         /* TODO: disable beacon for the bss */
121
122         mt7615_mcu_set_dev_info(dev, vif, 0);
123
124         rcu_assign_pointer(dev->mt76.wcid[idx], NULL);
125         mt76_txq_remove(&dev->mt76, vif->txq);
126
127         mutex_lock(&dev->mt76.mutex);
128         dev->vif_mask &= ~BIT(mvif->idx);
129         dev->omac_mask &= ~BIT(mvif->omac_idx);
130         mutex_unlock(&dev->mt76.mutex);
131 }
132
133 static int mt7615_set_channel(struct mt7615_dev *dev)
134 {
135         int ret;
136
137         cancel_delayed_work_sync(&dev->mt76.mac_work);
138         set_bit(MT76_RESET, &dev->mt76.state);
139
140         mt7615_dfs_check_channel(dev);
141
142         mt76_set_channel(&dev->mt76);
143
144         ret = mt7615_mcu_set_channel(dev);
145         if (ret)
146                 return ret;
147
148         ret = mt7615_dfs_init_radar_detector(dev);
149         if (ret < 0)
150                 return ret;
151
152         clear_bit(MT76_RESET, &dev->mt76.state);
153
154         mt76_txq_schedule_all(&dev->mt76);
155         ieee80211_queue_delayed_work(mt76_hw(dev), &dev->mt76.mac_work,
156                                      MT7615_WATCHDOG_TIME);
157         return 0;
158 }
159
160 static int mt7615_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
161                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
162                           struct ieee80211_key_conf *key)
163 {
164         struct mt7615_dev *dev = hw->priv;
165         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
166         struct mt7615_sta *msta = sta ? (struct mt7615_sta *)sta->drv_priv :
167                                   &mvif->sta;
168         struct mt76_wcid *wcid = &msta->wcid;
169         int idx = key->keyidx;
170
171         /* The hardware does not support per-STA RX GTK, fallback
172          * to software mode for these.
173          */
174         if ((vif->type == NL80211_IFTYPE_ADHOC ||
175              vif->type == NL80211_IFTYPE_MESH_POINT) &&
176             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
177              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
178             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
179                 return -EOPNOTSUPP;
180
181         /* fall back to sw encryption for unsupported ciphers */
182         switch (key->cipher) {
183         case WLAN_CIPHER_SUITE_WEP40:
184         case WLAN_CIPHER_SUITE_WEP104:
185         case WLAN_CIPHER_SUITE_TKIP:
186         case WLAN_CIPHER_SUITE_CCMP:
187         case WLAN_CIPHER_SUITE_CCMP_256:
188         case WLAN_CIPHER_SUITE_GCMP:
189         case WLAN_CIPHER_SUITE_GCMP_256:
190         case WLAN_CIPHER_SUITE_SMS4:
191                 break;
192         default:
193                 return -EOPNOTSUPP;
194         }
195
196         if (cmd == SET_KEY) {
197                 key->hw_key_idx = wcid->idx;
198                 wcid->hw_key_idx = idx;
199         } else {
200                 if (idx == wcid->hw_key_idx)
201                         wcid->hw_key_idx = -1;
202
203                 key = NULL;
204         }
205         mt76_wcid_key_setup(&dev->mt76, wcid, key);
206
207         return mt7615_mcu_set_wtbl_key(dev, wcid->idx, key, cmd);
208 }
209
210 static int mt7615_config(struct ieee80211_hw *hw, u32 changed)
211 {
212         struct mt7615_dev *dev = hw->priv;
213         int ret = 0;
214
215         mutex_lock(&dev->mt76.mutex);
216
217         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
218                 ieee80211_stop_queues(hw);
219                 ret = mt7615_set_channel(dev);
220                 ieee80211_wake_queues(hw);
221         }
222
223         if (changed & IEEE80211_CONF_CHANGE_POWER)
224                 ret = mt7615_mcu_set_tx_power(dev);
225
226         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
227                 if (!(hw->conf.flags & IEEE80211_CONF_MONITOR))
228                         dev->mt76.rxfilter |= MT_WF_RFCR_DROP_OTHER_UC;
229                 else
230                         dev->mt76.rxfilter &= ~MT_WF_RFCR_DROP_OTHER_UC;
231
232                 mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
233         }
234
235         mutex_unlock(&dev->mt76.mutex);
236
237         return ret;
238 }
239
240 static int
241 mt7615_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
242                const struct ieee80211_tx_queue_params *params)
243 {
244         struct mt7615_dev *dev = hw->priv;
245         static const u8 wmm_queue_map[] = {
246                 [IEEE80211_AC_BK] = 0,
247                 [IEEE80211_AC_BE] = 1,
248                 [IEEE80211_AC_VI] = 2,
249                 [IEEE80211_AC_VO] = 3,
250         };
251
252         /* TODO: hw wmm_set 1~3 */
253         return mt7615_mcu_set_wmm(dev, wmm_queue_map[queue], params);
254 }
255
256 static void mt7615_configure_filter(struct ieee80211_hw *hw,
257                                     unsigned int changed_flags,
258                                     unsigned int *total_flags,
259                                     u64 multicast)
260 {
261         struct mt7615_dev *dev = hw->priv;
262         u32 flags = 0;
263
264 #define MT76_FILTER(_flag, _hw) do { \
265                 flags |= *total_flags & FIF_##_flag;                    \
266                 dev->mt76.rxfilter &= ~(_hw);                           \
267                 dev->mt76.rxfilter |= !(flags & FIF_##_flag) * (_hw);   \
268         } while (0)
269
270         dev->mt76.rxfilter &= ~(MT_WF_RFCR_DROP_OTHER_BSS |
271                                 MT_WF_RFCR_DROP_OTHER_BEACON |
272                                 MT_WF_RFCR_DROP_FRAME_REPORT |
273                                 MT_WF_RFCR_DROP_PROBEREQ |
274                                 MT_WF_RFCR_DROP_MCAST_FILTERED |
275                                 MT_WF_RFCR_DROP_MCAST |
276                                 MT_WF_RFCR_DROP_BCAST |
277                                 MT_WF_RFCR_DROP_DUPLICATE |
278                                 MT_WF_RFCR_DROP_A2_BSSID |
279                                 MT_WF_RFCR_DROP_UNWANTED_CTL |
280                                 MT_WF_RFCR_DROP_STBC_MULTI);
281
282         MT76_FILTER(OTHER_BSS, MT_WF_RFCR_DROP_OTHER_TIM |
283                                MT_WF_RFCR_DROP_A3_MAC |
284                                MT_WF_RFCR_DROP_A3_BSSID);
285
286         MT76_FILTER(FCSFAIL, MT_WF_RFCR_DROP_FCSFAIL);
287
288         MT76_FILTER(CONTROL, MT_WF_RFCR_DROP_CTS |
289                              MT_WF_RFCR_DROP_RTS |
290                              MT_WF_RFCR_DROP_CTL_RSV |
291                              MT_WF_RFCR_DROP_NDPA);
292
293         *total_flags = flags;
294         mt76_wr(dev, MT_WF_RFCR, dev->mt76.rxfilter);
295 }
296
297 static void mt7615_bss_info_changed(struct ieee80211_hw *hw,
298                                     struct ieee80211_vif *vif,
299                                     struct ieee80211_bss_conf *info,
300                                     u32 changed)
301 {
302         struct mt7615_dev *dev = hw->priv;
303
304         mutex_lock(&dev->mt76.mutex);
305
306         if (changed & BSS_CHANGED_ASSOC)
307                 mt7615_mcu_set_bss_info(dev, vif, info->assoc);
308
309         /* TODO: update beacon content
310          * BSS_CHANGED_BEACON
311          */
312
313         if (changed & BSS_CHANGED_BEACON_ENABLED) {
314                 mt7615_mcu_set_bss_info(dev, vif, info->enable_beacon);
315                 mt7615_mcu_wtbl_bmc(dev, vif, info->enable_beacon);
316                 mt7615_mcu_set_sta_rec_bmc(dev, vif, info->enable_beacon);
317                 mt7615_mcu_set_bcn(dev, vif, info->enable_beacon);
318         }
319
320         mutex_unlock(&dev->mt76.mutex);
321 }
322
323 static void
324 mt7615_channel_switch_beacon(struct ieee80211_hw *hw,
325                              struct ieee80211_vif *vif,
326                              struct cfg80211_chan_def *chandef)
327 {
328         struct mt7615_dev *dev = hw->priv;
329
330         mutex_lock(&dev->mt76.mutex);
331         mt7615_mcu_set_bcn(dev, vif, true);
332         mutex_unlock(&dev->mt76.mutex);
333 }
334
335 int mt7615_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
336                    struct ieee80211_sta *sta)
337 {
338         struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
339         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
340         struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
341         int idx;
342
343         idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
344         if (idx < 0)
345                 return -ENOSPC;
346
347         msta->vif = mvif;
348         msta->wcid.sta = 1;
349         msta->wcid.idx = idx;
350
351         mt7615_mcu_add_wtbl(dev, vif, sta);
352         mt7615_mcu_set_sta_rec(dev, vif, sta, 1);
353
354         return 0;
355 }
356
357 void mt7615_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
358                       struct ieee80211_sta *sta)
359 {
360         struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
361
362         if (sta->ht_cap.ht_supported)
363                 mt7615_mcu_set_ht_cap(dev, vif, sta);
364 }
365
366 void mt7615_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
367                        struct ieee80211_sta *sta)
368 {
369         struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
370
371         mt7615_mcu_set_sta_rec(dev, vif, sta, 0);
372         mt7615_mcu_del_wtbl(dev, sta);
373 }
374
375 static void mt7615_sta_rate_tbl_update(struct ieee80211_hw *hw,
376                                        struct ieee80211_vif *vif,
377                                        struct ieee80211_sta *sta)
378 {
379         struct mt7615_dev *dev = hw->priv;
380         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
381         struct ieee80211_sta_rates *sta_rates = rcu_dereference(sta->rates);
382         int i;
383
384         spin_lock_bh(&dev->mt76.lock);
385         for (i = 0; i < ARRAY_SIZE(msta->rates); i++) {
386                 msta->rates[i].idx = sta_rates->rate[i].idx;
387                 msta->rates[i].count = sta_rates->rate[i].count;
388                 msta->rates[i].flags = sta_rates->rate[i].flags;
389
390                 if (msta->rates[i].idx < 0 || !msta->rates[i].count)
391                         break;
392         }
393         msta->n_rates = i;
394         mt7615_mac_set_rates(dev, msta, NULL, msta->rates);
395         msta->rate_probe = false;
396         spin_unlock_bh(&dev->mt76.lock);
397 }
398
399 static void mt7615_tx(struct ieee80211_hw *hw,
400                       struct ieee80211_tx_control *control,
401                       struct sk_buff *skb)
402 {
403         struct mt7615_dev *dev = hw->priv;
404         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
405         struct ieee80211_vif *vif = info->control.vif;
406         struct mt76_wcid *wcid = &dev->mt76.global_wcid;
407
408         if (control->sta) {
409                 struct mt7615_sta *sta;
410
411                 sta = (struct mt7615_sta *)control->sta->drv_priv;
412                 wcid = &sta->wcid;
413         }
414
415         if (vif && !control->sta) {
416                 struct mt7615_vif *mvif;
417
418                 mvif = (struct mt7615_vif *)vif->drv_priv;
419                 wcid = &mvif->sta.wcid;
420         }
421
422         mt76_tx(&dev->mt76, control->sta, wcid, skb);
423 }
424
425 static int mt7615_set_rts_threshold(struct ieee80211_hw *hw, u32 val)
426 {
427         struct mt7615_dev *dev = hw->priv;
428
429         mutex_lock(&dev->mt76.mutex);
430         mt7615_mcu_set_rts_thresh(dev, val);
431         mutex_unlock(&dev->mt76.mutex);
432
433         return 0;
434 }
435
436 static int
437 mt7615_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
438                     struct ieee80211_ampdu_params *params)
439 {
440         enum ieee80211_ampdu_mlme_action action = params->action;
441         struct mt7615_dev *dev = hw->priv;
442         struct ieee80211_sta *sta = params->sta;
443         struct ieee80211_txq *txq = sta->txq[params->tid];
444         struct mt7615_sta *msta = (struct mt7615_sta *)sta->drv_priv;
445         u16 tid = params->tid;
446         u16 *ssn = &params->ssn;
447         struct mt76_txq *mtxq;
448
449         if (!txq)
450                 return -EINVAL;
451
452         mtxq = (struct mt76_txq *)txq->drv_priv;
453
454         switch (action) {
455         case IEEE80211_AMPDU_RX_START:
456                 mt76_rx_aggr_start(&dev->mt76, &msta->wcid, tid, *ssn,
457                                    params->buf_size);
458                 mt7615_mcu_set_rx_ba(dev, params, 1);
459                 break;
460         case IEEE80211_AMPDU_RX_STOP:
461                 mt76_rx_aggr_stop(&dev->mt76, &msta->wcid, tid);
462                 mt7615_mcu_set_rx_ba(dev, params, 0);
463                 break;
464         case IEEE80211_AMPDU_TX_OPERATIONAL:
465                 mtxq->aggr = true;
466                 mtxq->send_bar = false;
467                 mt7615_mcu_set_tx_ba(dev, params, 1);
468                 break;
469         case IEEE80211_AMPDU_TX_STOP_FLUSH:
470         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
471                 mtxq->aggr = false;
472                 ieee80211_send_bar(vif, sta->addr, tid, mtxq->agg_ssn);
473                 mt7615_mcu_set_tx_ba(dev, params, 0);
474                 break;
475         case IEEE80211_AMPDU_TX_START:
476                 mtxq->agg_ssn = IEEE80211_SN_TO_SEQ(*ssn);
477                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
478                 break;
479         case IEEE80211_AMPDU_TX_STOP_CONT:
480                 mtxq->aggr = false;
481                 mt7615_mcu_set_tx_ba(dev, params, 0);
482                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
483                 break;
484         }
485
486         return 0;
487 }
488
489 static void
490 mt7615_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
491                const u8 *mac)
492 {
493         struct mt7615_dev *dev = hw->priv;
494
495         set_bit(MT76_SCANNING, &dev->mt76.state);
496 }
497
498 static void
499 mt7615_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
500 {
501         struct mt7615_dev *dev = hw->priv;
502
503         clear_bit(MT76_SCANNING, &dev->mt76.state);
504 }
505
506 const struct ieee80211_ops mt7615_ops = {
507         .tx = mt7615_tx,
508         .start = mt7615_start,
509         .stop = mt7615_stop,
510         .add_interface = mt7615_add_interface,
511         .remove_interface = mt7615_remove_interface,
512         .config = mt7615_config,
513         .conf_tx = mt7615_conf_tx,
514         .configure_filter = mt7615_configure_filter,
515         .bss_info_changed = mt7615_bss_info_changed,
516         .sta_state = mt76_sta_state,
517         .set_key = mt7615_set_key,
518         .ampdu_action = mt7615_ampdu_action,
519         .set_rts_threshold = mt7615_set_rts_threshold,
520         .wake_tx_queue = mt76_wake_tx_queue,
521         .sta_rate_tbl_update = mt7615_sta_rate_tbl_update,
522         .sw_scan_start = mt7615_sw_scan,
523         .sw_scan_complete = mt7615_sw_scan_complete,
524         .release_buffered_frames = mt76_release_buffered_frames,
525         .get_txpower = mt76_get_txpower,
526         .channel_switch_beacon = mt7615_channel_switch_beacon,
527 };