mt76: mt7615: measure channel noise and report it via survey
[linux-2.6-microblaze.git] / drivers / net / wireless / mediatek / mt76 / mt7615 / mt7615.h
1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2019 MediaTek Inc. */
3
4 #ifndef __MT7615_H
5 #define __MT7615_H
6
7 #include <linux/interrupt.h>
8 #include <linux/ktime.h>
9 #include "../mt76.h"
10 #include "regs.h"
11
12 #define MT7615_MAX_INTERFACES           4
13 #define MT7615_MAX_WMM_SETS             4
14 #define MT7615_WTBL_SIZE                128
15 #define MT7615_WTBL_RESERVED            (MT7615_WTBL_SIZE - 1)
16 #define MT7615_WTBL_STA                 (MT7615_WTBL_RESERVED - \
17                                          MT7615_MAX_INTERFACES)
18
19 #define MT7615_WATCHDOG_TIME            (HZ / 10)
20 #define MT7615_RATE_RETRY               2
21
22 #define MT7615_TX_RING_SIZE             1024
23 #define MT7615_TX_MCU_RING_SIZE         128
24 #define MT7615_TX_FWDL_RING_SIZE        128
25
26 #define MT7615_RX_RING_SIZE             1024
27 #define MT7615_RX_MCU_RING_SIZE         512
28
29 #define MT7615_FIRMWARE_CR4             "mediatek/mt7615_cr4.bin"
30 #define MT7615_FIRMWARE_N9              "mediatek/mt7615_n9.bin"
31 #define MT7615_ROM_PATCH                "mediatek/mt7615_rom_patch.bin"
32
33 #define MT7615_EEPROM_SIZE              1024
34 #define MT7615_TOKEN_SIZE               4096
35
36 #define MT_FRAC_SCALE           12
37 #define MT_FRAC(val, div)       (((val) << MT_FRAC_SCALE) / (div))
38
39 #define MT_CHFREQ_VALID         BIT(7)
40 #define MT_CHFREQ_DBDC_IDX      BIT(6)
41 #define MT_CHFREQ_SEQ           GENMASK(5, 0)
42
43 #define MT7615_BAR_RATE_DEFAULT         0x4b /* OFDM 6M */
44 #define MT7615_CFEND_RATE_DEFAULT       0x49 /* OFDM 24M */
45 #define MT7615_CFEND_RATE_11B           0x03 /* 11B LP, 11M */
46
47 struct mt7615_vif;
48 struct mt7615_sta;
49
50 enum mt7615_hw_txq_id {
51         MT7615_TXQ_MAIN,
52         MT7615_TXQ_EXT,
53         MT7615_TXQ_MCU,
54         MT7615_TXQ_FWDL,
55 };
56
57 struct mt7615_rate_set {
58         struct ieee80211_tx_rate probe_rate;
59         struct ieee80211_tx_rate rates[4];
60 };
61
62 struct mt7615_sta {
63         struct mt76_wcid wcid; /* must be first */
64
65         struct mt7615_vif *vif;
66
67         struct list_head poll_list;
68         u32 airtime_ac[8];
69
70         struct ieee80211_tx_rate rates[4];
71
72         struct mt7615_rate_set rateset[2];
73         u32 rate_set_tsf;
74
75         u8 rate_count;
76         u8 n_rates;
77
78         u8 rate_probe;
79 };
80
81 struct mt7615_vif {
82         u8 idx;
83         u8 omac_idx;
84         u8 band_idx;
85         u8 wmm_idx;
86
87         struct mt7615_sta sta;
88 };
89
90 struct mt7615_phy {
91         struct mt76_phy *mt76;
92         struct mt7615_dev *dev;
93
94         u32 rxfilter;
95         u32 omac_mask;
96
97         u16 noise;
98
99         unsigned long last_cca_adj;
100         int false_cca_ofdm, false_cca_cck;
101         s8 ofdm_sensitivity;
102         s8 cck_sensitivity;
103
104         u16 chainmask;
105
106         s16 coverage_class;
107         u8 slottime;
108
109         u8 chfreq_seq;
110         u8 rdd_state;
111         int dfs_state;
112
113         __le32 rx_ampdu_ts;
114         u32 ampdu_ref;
115 };
116
117 struct mt7615_dev {
118         union { /* must be first */
119                 struct mt76_dev mt76;
120                 struct mt76_phy mphy;
121         };
122
123         struct mt7615_phy phy;
124         u32 vif_mask;
125         u32 omac_mask;
126
127         u16 chainmask;
128
129         struct work_struct mcu_work;
130
131         struct list_head sta_poll_list;
132         spinlock_t sta_poll_lock;
133
134         struct {
135                 u8 n_pulses;
136                 u32 period;
137                 u16 width;
138                 s16 power;
139         } radar_pattern;
140         u32 hw_pattern;
141
142         u8 mac_work_count;
143         bool scs_en;
144
145         spinlock_t token_lock;
146         struct idr token;
147 };
148
149 enum {
150         HW_BSSID_0 = 0x0,
151         HW_BSSID_1,
152         HW_BSSID_2,
153         HW_BSSID_3,
154         HW_BSSID_MAX,
155         EXT_BSSID_START = 0x10,
156         EXT_BSSID_1,
157         EXT_BSSID_2,
158         EXT_BSSID_3,
159         EXT_BSSID_4,
160         EXT_BSSID_5,
161         EXT_BSSID_6,
162         EXT_BSSID_7,
163         EXT_BSSID_8,
164         EXT_BSSID_9,
165         EXT_BSSID_10,
166         EXT_BSSID_11,
167         EXT_BSSID_12,
168         EXT_BSSID_13,
169         EXT_BSSID_14,
170         EXT_BSSID_15,
171         EXT_BSSID_END
172 };
173
174 enum {
175         MT_RX_SEL0,
176         MT_RX_SEL1,
177 };
178
179 enum mt7615_rdd_cmd {
180         RDD_STOP,
181         RDD_START,
182         RDD_DET_MODE,
183         RDD_DET_STOP,
184         RDD_CAC_START,
185         RDD_CAC_END,
186         RDD_NORMAL_START,
187         RDD_DISABLE_DFS_CAL,
188         RDD_PULSE_DBG,
189         RDD_READ_PULSE,
190         RDD_RESUME_BF,
191 };
192
193 static inline struct mt7615_phy *
194 mt7615_hw_phy(struct ieee80211_hw *hw)
195 {
196         struct mt76_phy *phy = hw->priv;
197
198         return phy->priv;
199 }
200
201 static inline struct mt7615_dev *
202 mt7615_hw_dev(struct ieee80211_hw *hw)
203 {
204         struct mt76_phy *phy = hw->priv;
205
206         return container_of(phy->dev, struct mt7615_dev, mt76);
207 }
208
209 static inline struct mt7615_phy *
210 mt7615_ext_phy(struct mt7615_dev *dev)
211 {
212         struct mt76_phy *phy = dev->mt76.phy2;
213
214         if (!phy)
215                 return NULL;
216
217         return phy->priv;
218 }
219
220 extern const struct ieee80211_ops mt7615_ops;
221 extern struct pci_driver mt7615_pci_driver;
222
223 u32 mt7615_reg_map(struct mt7615_dev *dev, u32 addr);
224
225 int mt7615_register_device(struct mt7615_dev *dev);
226 void mt7615_unregister_device(struct mt7615_dev *dev);
227 int mt7615_register_ext_phy(struct mt7615_dev *dev);
228 void mt7615_unregister_ext_phy(struct mt7615_dev *dev);
229 int mt7615_eeprom_init(struct mt7615_dev *dev);
230 int mt7615_eeprom_get_power_index(struct mt7615_dev *dev,
231                                   struct ieee80211_channel *chan,
232                                   u8 chain_idx);
233 int mt7615_dma_init(struct mt7615_dev *dev);
234 void mt7615_dma_cleanup(struct mt7615_dev *dev);
235 int mt7615_mcu_init(struct mt7615_dev *dev);
236 bool mt7615_wait_for_mcu_init(struct mt7615_dev *dev);
237 int mt7615_mcu_set_dev_info(struct mt7615_dev *dev,
238                             struct ieee80211_vif *vif, bool enable);
239 int mt7615_mcu_set_bss_info(struct mt7615_dev *dev, struct ieee80211_vif *vif,
240                             int en);
241 void mt7615_mac_set_rates(struct mt7615_phy *phy, struct mt7615_sta *sta,
242                           struct ieee80211_tx_rate *probe_rate,
243                           struct ieee80211_tx_rate *rates);
244 int mt7615_mcu_wtbl_bmc(struct mt7615_dev *dev, struct ieee80211_vif *vif,
245                         bool enable);
246 int mt7615_mcu_add_wtbl(struct mt7615_dev *dev, struct ieee80211_vif *vif,
247                         struct ieee80211_sta *sta);
248 int mt7615_mcu_del_wtbl(struct mt7615_dev *dev, struct ieee80211_sta *sta);
249 int mt7615_mcu_del_wtbl_all(struct mt7615_dev *dev);
250 int mt7615_mcu_set_sta_rec_bmc(struct mt7615_dev *dev,
251                                struct ieee80211_vif *vif, bool en);
252 int mt7615_mcu_set_sta_rec(struct mt7615_dev *dev, struct ieee80211_vif *vif,
253                            struct ieee80211_sta *sta, bool en);
254 int mt7615_mcu_set_bcn(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
255                        int en);
256 int mt7615_mcu_set_channel(struct mt7615_phy *phy);
257 int mt7615_mcu_set_wmm(struct mt7615_dev *dev, u8 queue,
258                        const struct ieee80211_tx_queue_params *params);
259 int mt7615_mcu_set_tx_ba(struct mt7615_dev *dev,
260                          struct ieee80211_ampdu_params *params,
261                          bool add);
262 int mt7615_mcu_set_rx_ba(struct mt7615_dev *dev,
263                          struct ieee80211_ampdu_params *params,
264                          bool add);
265 int mt7615_mcu_set_ht_cap(struct mt7615_dev *dev, struct ieee80211_vif *vif,
266                           struct ieee80211_sta *sta);
267 void mt7615_mcu_rx_event(struct mt7615_dev *dev, struct sk_buff *skb);
268 int mt7615_mcu_rdd_cmd(struct mt7615_dev *dev,
269                        enum mt7615_rdd_cmd cmd, u8 index,
270                        u8 rx_sel, u8 val);
271 int mt7615_mcu_rdd_send_pattern(struct mt7615_dev *dev);
272
273 static inline bool is_mt7622(struct mt76_dev *dev)
274 {
275         return mt76_chip(dev) == 0x7622;
276 }
277
278 static inline void mt7615_irq_enable(struct mt7615_dev *dev, u32 mask)
279 {
280         mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, 0, mask);
281 }
282
283 static inline void mt7615_irq_disable(struct mt7615_dev *dev, u32 mask)
284 {
285         mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, mask, 0);
286 }
287
288 void mt7615_update_channel(struct mt76_dev *mdev);
289 bool mt7615_mac_wtbl_update(struct mt7615_dev *dev, int idx, u32 mask);
290 void mt7615_mac_reset_counters(struct mt7615_dev *dev);
291 void mt7615_mac_cca_stats_reset(struct mt7615_phy *phy);
292 void mt7615_mac_set_scs(struct mt7615_dev *dev, bool enable);
293 void mt7615_mac_enable_nf(struct mt7615_dev *dev, bool ext_phy);
294 void mt7615_mac_sta_poll(struct mt7615_dev *dev);
295 int mt7615_mac_write_txwi(struct mt7615_dev *dev, __le32 *txwi,
296                           struct sk_buff *skb, struct mt76_wcid *wcid,
297                           struct ieee80211_sta *sta, int pid,
298                           struct ieee80211_key_conf *key);
299 void mt7615_mac_set_timing(struct mt7615_phy *phy);
300 int mt7615_mac_fill_rx(struct mt7615_dev *dev, struct sk_buff *skb);
301 void mt7615_mac_add_txs(struct mt7615_dev *dev, void *data);
302 void mt7615_mac_tx_free(struct mt7615_dev *dev, struct sk_buff *skb);
303 int mt7615_mac_wtbl_set_key(struct mt7615_dev *dev, struct mt76_wcid *wcid,
304                             struct ieee80211_key_conf *key,
305                             enum set_key_cmd cmd);
306
307 int mt7615_mcu_set_dbdc(struct mt7615_dev *dev);
308 int mt7615_mcu_set_eeprom(struct mt7615_dev *dev);
309 int mt7615_mcu_set_mac_enable(struct mt7615_dev *dev, int band, bool enable);
310 int mt7615_mcu_set_rts_thresh(struct mt7615_phy *phy, u32 val);
311 int mt7615_mcu_ctrl_pm_state(struct mt7615_dev *dev, int band, int enter);
312 int mt7615_mcu_get_temperature(struct mt7615_dev *dev, int index);
313 int mt7615_mcu_set_tx_power(struct mt7615_phy *phy);
314 void mt7615_mcu_exit(struct mt7615_dev *dev);
315
316 int mt7615_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
317                           enum mt76_txq_id qid, struct mt76_wcid *wcid,
318                           struct ieee80211_sta *sta,
319                           struct mt76_tx_info *tx_info);
320
321 void mt7615_tx_complete_skb(struct mt76_dev *mdev, enum mt76_txq_id qid,
322                             struct mt76_queue_entry *e);
323
324 void mt7615_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
325                          struct sk_buff *skb);
326 void mt7615_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
327 int mt7615_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
328                        struct ieee80211_sta *sta);
329 void mt7615_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
330                            struct ieee80211_sta *sta);
331 void mt7615_mac_work(struct work_struct *work);
332 void mt7615_txp_skb_unmap(struct mt76_dev *dev,
333                           struct mt76_txwi_cache *txwi);
334 int mt76_dfs_start_rdd(struct mt7615_dev *dev, bool force);
335 int mt7615_dfs_init_radar_detector(struct mt7615_phy *phy);
336
337 int mt7615_init_debugfs(struct mt7615_dev *dev);
338
339 #endif