mac80211: Switch to a virtual time-based airtime scheduler
[linux-2.6-microblaze.git] / drivers / net / wireless / mediatek / mt76 / mt7915 / mt7915.h
1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2020 MediaTek Inc. */
3
4 #ifndef __MT7915_H
5 #define __MT7915_H
6
7 #include <linux/interrupt.h>
8 #include <linux/ktime.h>
9 #include "../mt76.h"
10 #include "regs.h"
11
12 #define MT7915_MAX_INTERFACES           32
13 #define MT7915_MAX_WMM_SETS             4
14 #define MT7915_WTBL_SIZE                288
15 #define MT7915_WTBL_RESERVED            (MT7915_WTBL_SIZE - 1)
16 #define MT7915_WTBL_STA                 (MT7915_WTBL_RESERVED - \
17                                          MT7915_MAX_INTERFACES)
18
19 #define MT7915_WATCHDOG_TIME            (HZ / 10)
20 #define MT7915_RESET_TIMEOUT            (30 * HZ)
21
22 #define MT7915_TX_RING_SIZE             2048
23 #define MT7915_TX_MCU_RING_SIZE         256
24 #define MT7915_TX_FWDL_RING_SIZE        128
25
26 #define MT7915_RX_RING_SIZE             1536
27 #define MT7915_RX_MCU_RING_SIZE         512
28
29 #define MT7915_FIRMWARE_WA              "mediatek/mt7915_wa.bin"
30 #define MT7915_FIRMWARE_WM              "mediatek/mt7915_wm.bin"
31 #define MT7915_ROM_PATCH                "mediatek/mt7915_rom_patch.bin"
32
33 #define MT7915_EEPROM_SIZE              3584
34 #define MT7915_TOKEN_SIZE               8192
35
36 #define MT7915_CFEND_RATE_DEFAULT       0x49    /* OFDM 24M */
37 #define MT7915_CFEND_RATE_11B           0x03    /* 11B LP, 11M */
38 #define MT7915_5G_RATE_DEFAULT          0x4b    /* OFDM 6M */
39 #define MT7915_2G_RATE_DEFAULT          0x0     /* CCK 1M */
40
41 struct mt7915_vif;
42 struct mt7915_sta;
43 struct mt7915_dfs_pulse;
44 struct mt7915_dfs_pattern;
45
46 enum mt7915_txq_id {
47         MT7915_TXQ_FWDL = 16,
48         MT7915_TXQ_MCU_WM,
49         MT7915_TXQ_BAND0,
50         MT7915_TXQ_BAND1,
51         MT7915_TXQ_MCU_WA,
52 };
53
54 enum mt7915_rxq_id {
55         MT7915_RXQ_BAND0 = 0,
56         MT7915_RXQ_BAND1,
57         MT7915_RXQ_MCU_WM = 0,
58         MT7915_RXQ_MCU_WA,
59         MT7915_RXQ_MCU_WA_EXT,
60 };
61
62 struct mt7915_sta_stats {
63         struct rate_info prob_rate;
64         struct rate_info tx_rate;
65
66         unsigned long per;
67         unsigned long changed;
68         unsigned long jiffies;
69 };
70
71 struct mt7915_sta_key_conf {
72         s8 keyidx;
73         u8 key[16];
74 };
75
76 struct mt7915_sta {
77         struct mt76_wcid wcid; /* must be first */
78
79         struct mt7915_vif *vif;
80
81         struct list_head stats_list;
82         struct list_head poll_list;
83         struct list_head rc_list;
84         u32 airtime_ac[8];
85
86         struct mt7915_sta_stats stats;
87
88         unsigned long ampdu_state;
89
90         struct mt7915_sta_key_conf bip;
91 };
92
93 struct mt7915_vif {
94         u16 idx;
95         u8 omac_idx;
96         u8 band_idx;
97         u8 wmm_idx;
98
99         struct mt7915_sta sta;
100         struct mt7915_phy *phy;
101
102         struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
103 };
104
105 struct mib_stats {
106         u32 ack_fail_cnt;
107         u32 fcs_err_cnt;
108         u32 rts_cnt;
109         u32 rts_retries_cnt;
110         u32 ba_miss_cnt;
111 };
112
113 struct mt7915_hif {
114         struct list_head list;
115
116         struct device *dev;
117         void __iomem *regs;
118         int irq;
119 };
120
121 struct mt7915_phy {
122         struct mt76_phy *mt76;
123         struct mt7915_dev *dev;
124
125         struct ieee80211_sband_iftype_data iftype[2][NUM_NL80211_IFTYPES];
126
127         struct ieee80211_vif *monitor_vif;
128
129         u32 rxfilter;
130         u64 omac_mask;
131
132         u16 noise;
133
134         s16 coverage_class;
135         u8 slottime;
136
137         u8 rdd_state;
138         int dfs_state;
139
140         u32 rx_ampdu_ts;
141         u32 ampdu_ref;
142
143         struct mib_stats mib;
144         struct list_head stats_list;
145
146         u8 sta_work_count;
147
148 #ifdef CONFIG_NL80211_TESTMODE
149         struct {
150                 u32 *reg_backup;
151
152                 s32 last_freq_offset;
153                 u8 last_rcpi[4];
154                 s8 last_ib_rssi[4];
155                 s8 last_wb_rssi[4];
156                 u8 last_snr;
157
158                 u8 spe_idx;
159         } test;
160 #endif
161 };
162
163 struct mt7915_dev {
164         union { /* must be first */
165                 struct mt76_dev mt76;
166                 struct mt76_phy mphy;
167         };
168
169         struct mt7915_hif *hif2;
170
171         const struct mt76_bus_ops *bus_ops;
172         struct mt7915_phy phy;
173
174         u16 chainmask;
175         u32 hif_idx;
176
177         struct work_struct init_work;
178         struct work_struct rc_work;
179         struct work_struct reset_work;
180         wait_queue_head_t reset_wait;
181         u32 reset_state;
182
183         struct list_head sta_rc_list;
184         struct list_head sta_poll_list;
185         spinlock_t sta_poll_lock;
186
187         u32 hw_pattern;
188
189         bool dbdc_support;
190         bool flash_mode;
191         bool fw_debug;
192         bool ibf;
193
194         void *cal;
195 };
196
197 enum {
198         HW_BSSID_0 = 0x0,
199         HW_BSSID_1,
200         HW_BSSID_2,
201         HW_BSSID_3,
202         HW_BSSID_MAX = HW_BSSID_3,
203         EXT_BSSID_START = 0x10,
204         EXT_BSSID_1,
205         EXT_BSSID_15 = 0x1f,
206         EXT_BSSID_MAX = EXT_BSSID_15,
207         REPEATER_BSSID_START = 0x20,
208         REPEATER_BSSID_MAX = 0x3f,
209 };
210
211 enum {
212         MT_LMAC_AC00,
213         MT_LMAC_AC01,
214         MT_LMAC_AC02,
215         MT_LMAC_AC03,
216         MT_LMAC_ALTX0 = 0x10,
217         MT_LMAC_BMC0,
218         MT_LMAC_BCN0,
219 };
220
221 enum {
222         MT_RX_SEL0,
223         MT_RX_SEL1,
224 };
225
226 enum mt7915_rdd_cmd {
227         RDD_STOP,
228         RDD_START,
229         RDD_DET_MODE,
230         RDD_RADAR_EMULATE,
231         RDD_START_TXQ = 20,
232         RDD_CAC_START = 50,
233         RDD_CAC_END,
234         RDD_NORMAL_START,
235         RDD_DISABLE_DFS_CAL,
236         RDD_PULSE_DBG,
237         RDD_READ_PULSE,
238         RDD_RESUME_BF,
239         RDD_IRQ_OFF,
240 };
241
242 enum {
243         RATE_CTRL_RU_INFO,
244         RATE_CTRL_FIXED_RATE_INFO,
245         RATE_CTRL_DUMP_INFO,
246         RATE_CTRL_MU_INFO,
247 };
248
249 static inline struct mt7915_phy *
250 mt7915_hw_phy(struct ieee80211_hw *hw)
251 {
252         struct mt76_phy *phy = hw->priv;
253
254         return phy->priv;
255 }
256
257 static inline struct mt7915_dev *
258 mt7915_hw_dev(struct ieee80211_hw *hw)
259 {
260         struct mt76_phy *phy = hw->priv;
261
262         return container_of(phy->dev, struct mt7915_dev, mt76);
263 }
264
265 static inline struct mt7915_phy *
266 mt7915_ext_phy(struct mt7915_dev *dev)
267 {
268         struct mt76_phy *phy = dev->mt76.phy2;
269
270         if (!phy)
271                 return NULL;
272
273         return phy->priv;
274 }
275
276 static inline u8 mt7915_lmac_mapping(struct mt7915_dev *dev, u8 ac)
277 {
278         /* LMAC uses the reverse order of mac80211 AC indexes */
279         return 3 - ac;
280 }
281
282 extern const struct ieee80211_ops mt7915_ops;
283 extern const struct mt76_testmode_ops mt7915_testmode_ops;
284
285 u32 mt7915_reg_map(struct mt7915_dev *dev, u32 addr);
286
287 int mt7915_register_device(struct mt7915_dev *dev);
288 void mt7915_unregister_device(struct mt7915_dev *dev);
289 int mt7915_eeprom_init(struct mt7915_dev *dev);
290 void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy);
291 int mt7915_eeprom_get_target_power(struct mt7915_dev *dev,
292                                    struct ieee80211_channel *chan,
293                                    u8 chain_idx);
294 s8 mt7915_eeprom_get_power_delta(struct mt7915_dev *dev, int band);
295 int mt7915_dma_init(struct mt7915_dev *dev);
296 void mt7915_dma_prefetch(struct mt7915_dev *dev);
297 void mt7915_dma_cleanup(struct mt7915_dev *dev);
298 int mt7915_mcu_init(struct mt7915_dev *dev);
299 int mt7915_mcu_add_dev_info(struct mt7915_phy *phy,
300                             struct ieee80211_vif *vif, bool enable);
301 int mt7915_mcu_add_bss_info(struct mt7915_phy *phy,
302                             struct ieee80211_vif *vif, int enable);
303 int mt7915_mcu_add_sta(struct mt7915_dev *dev, struct ieee80211_vif *vif,
304                        struct ieee80211_sta *sta, bool enable);
305 int mt7915_mcu_add_sta_adv(struct mt7915_dev *dev, struct ieee80211_vif *vif,
306                            struct ieee80211_sta *sta, bool enable);
307 int mt7915_mcu_sta_update_hdr_trans(struct mt7915_dev *dev,
308                                     struct ieee80211_vif *vif,
309                                     struct ieee80211_sta *sta);
310 int mt7915_mcu_add_tx_ba(struct mt7915_dev *dev,
311                          struct ieee80211_ampdu_params *params,
312                          bool add);
313 int mt7915_mcu_add_rx_ba(struct mt7915_dev *dev,
314                          struct ieee80211_ampdu_params *params,
315                          bool add);
316 int mt7915_mcu_add_key(struct mt7915_dev *dev, struct ieee80211_vif *vif,
317                        struct mt7915_sta *msta, struct ieee80211_key_conf *key,
318                        enum set_key_cmd cmd);
319 int mt7915_mcu_add_beacon(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
320                           int enable);
321 int mt7915_mcu_add_obss_spr(struct mt7915_dev *dev, struct ieee80211_vif *vif,
322                             bool enable);
323 int mt7915_mcu_add_rate_ctrl(struct mt7915_dev *dev, struct ieee80211_vif *vif,
324                              struct ieee80211_sta *sta);
325 int mt7915_mcu_add_smps(struct mt7915_dev *dev, struct ieee80211_vif *vif,
326                         struct ieee80211_sta *sta);
327 int mt7915_set_channel(struct mt7915_phy *phy);
328 int mt7915_mcu_set_chan_info(struct mt7915_phy *phy, int cmd);
329 int mt7915_mcu_set_tx(struct mt7915_dev *dev, struct ieee80211_vif *vif);
330 int mt7915_mcu_update_edca(struct mt7915_dev *dev, void *req);
331 int mt7915_mcu_set_fixed_rate(struct mt7915_dev *dev,
332                               struct ieee80211_sta *sta, u32 rate);
333 int mt7915_mcu_set_eeprom(struct mt7915_dev *dev);
334 int mt7915_mcu_get_eeprom(struct mt7915_dev *dev, u32 offset);
335 int mt7915_mcu_set_mac(struct mt7915_dev *dev, int band, bool enable,
336                        bool hdr_trans);
337 int mt7915_mcu_set_test_param(struct mt7915_dev *dev, u8 param, bool test_mode,
338                               u8 en);
339 int mt7915_mcu_set_scs(struct mt7915_dev *dev, u8 band, bool enable);
340 int mt7915_mcu_set_ser(struct mt7915_dev *dev, u8 action, u8 set, u8 band);
341 int mt7915_mcu_set_rts_thresh(struct mt7915_phy *phy, u32 val);
342 int mt7915_mcu_set_pm(struct mt7915_dev *dev, int band, int enter);
343 int mt7915_mcu_set_sku_en(struct mt7915_phy *phy, bool enable);
344 int mt7915_mcu_set_txpower_sku(struct mt7915_phy *phy);
345 int mt7915_mcu_set_txbf_type(struct mt7915_dev *dev);
346 int mt7915_mcu_set_txbf_module(struct mt7915_dev *dev);
347 int mt7915_mcu_set_txbf_sounding(struct mt7915_dev *dev);
348 int mt7915_mcu_set_fcc5_lpn(struct mt7915_dev *dev, int val);
349 int mt7915_mcu_set_pulse_th(struct mt7915_dev *dev,
350                             const struct mt7915_dfs_pulse *pulse);
351 int mt7915_mcu_set_radar_th(struct mt7915_dev *dev, int index,
352                             const struct mt7915_dfs_pattern *pattern);
353 int mt7915_mcu_apply_group_cal(struct mt7915_dev *dev);
354 int mt7915_mcu_apply_tx_dpd(struct mt7915_phy *phy);
355 int mt7915_mcu_get_temperature(struct mt7915_dev *dev, int index);
356 int mt7915_mcu_get_tx_rate(struct mt7915_dev *dev, u32 cmd, u16 wlan_idx);
357 int mt7915_mcu_get_rx_rate(struct mt7915_phy *phy, struct ieee80211_vif *vif,
358                            struct ieee80211_sta *sta, struct rate_info *rate);
359 int mt7915_mcu_rdd_cmd(struct mt7915_dev *dev, enum mt7915_rdd_cmd cmd,
360                        u8 index, u8 rx_sel, u8 val);
361 int mt7915_mcu_fw_log_2_host(struct mt7915_dev *dev, u8 ctrl);
362 int mt7915_mcu_fw_dbg_ctrl(struct mt7915_dev *dev, u32 module, u8 level);
363 void mt7915_mcu_rx_event(struct mt7915_dev *dev, struct sk_buff *skb);
364 void mt7915_mcu_exit(struct mt7915_dev *dev);
365
366 static inline bool is_mt7915(struct mt76_dev *dev)
367 {
368         return mt76_chip(dev) == 0x7915;
369 }
370
371 void mt7915_dual_hif_set_irq_mask(struct mt7915_dev *dev, bool write_reg,
372                                   u32 clear, u32 set);
373
374 static inline void mt7915_irq_enable(struct mt7915_dev *dev, u32 mask)
375 {
376         if (dev->hif2)
377                 mt7915_dual_hif_set_irq_mask(dev, true, 0, mask);
378         else
379                 mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, 0, mask);
380 }
381
382 static inline void mt7915_irq_disable(struct mt7915_dev *dev, u32 mask)
383 {
384         if (dev->hif2)
385                 mt7915_dual_hif_set_irq_mask(dev, true, mask, 0);
386         else
387                 mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, mask, 0);
388 }
389
390 bool mt7915_mac_wtbl_update(struct mt7915_dev *dev, int idx, u32 mask);
391 void mt7915_mac_reset_counters(struct mt7915_phy *phy);
392 void mt7915_mac_cca_stats_reset(struct mt7915_phy *phy);
393 void mt7915_mac_enable_nf(struct mt7915_dev *dev, bool ext_phy);
394 void mt7915_mac_write_txwi(struct mt7915_dev *dev, __le32 *txwi,
395                            struct sk_buff *skb, struct mt76_wcid *wcid,
396                            struct ieee80211_key_conf *key, bool beacon);
397 void mt7915_mac_set_timing(struct mt7915_phy *phy);
398 int mt7915_mac_fill_rx(struct mt7915_dev *dev, struct sk_buff *skb);
399 void mt7915_mac_fill_rx_vector(struct mt7915_dev *dev, struct sk_buff *skb);
400 void mt7915_mac_tx_free(struct mt7915_dev *dev, struct sk_buff *skb);
401 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
402                        struct ieee80211_sta *sta);
403 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
404                            struct ieee80211_sta *sta);
405 void mt7915_mac_work(struct work_struct *work);
406 void mt7915_mac_reset_work(struct work_struct *work);
407 void mt7915_mac_sta_rc_work(struct work_struct *work);
408 int mt7915_mmio_init(struct mt76_dev *mdev, void __iomem *mem_base, int irq);
409 int mt7915_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
410                           enum mt76_txq_id qid, struct mt76_wcid *wcid,
411                           struct ieee80211_sta *sta,
412                           struct mt76_tx_info *tx_info);
413 void mt7915_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e);
414 void mt7915_tx_token_put(struct mt7915_dev *dev);
415 int mt7915_init_tx_queues(struct mt7915_phy *phy, int idx, int n_desc);
416 void mt7915_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
417                          struct sk_buff *skb);
418 void mt7915_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
419 void mt7915_stats_work(struct work_struct *work);
420 void mt7915_txp_skb_unmap(struct mt76_dev *dev,
421                           struct mt76_txwi_cache *txwi);
422 int mt76_dfs_start_rdd(struct mt7915_dev *dev, bool force);
423 int mt7915_dfs_init_radar_detector(struct mt7915_phy *phy);
424 void mt7915_set_stream_he_caps(struct mt7915_phy *phy);
425 void mt7915_set_stream_vht_txbf_caps(struct mt7915_phy *phy);
426 void mt7915_update_channel(struct mt76_dev *mdev);
427 int mt7915_init_debugfs(struct mt7915_dev *dev);
428 #ifdef CONFIG_MAC80211_DEBUGFS
429 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
430                             struct ieee80211_sta *sta, struct dentry *dir);
431 #endif
432
433 #endif