xref: /linux/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h (revision 011f129fee4bd064a3db30ca1a0139548a619482)
1 /* SPDX-License-Identifier: ISC */
2 /*
3  * Copyright (C) 2022 MediaTek Inc.
4  */
5 
6 #ifndef __MT7996_H
7 #define __MT7996_H
8 
9 #include <linux/interrupt.h>
10 #include <linux/ktime.h>
11 #include "../mt76_connac.h"
12 #include "regs.h"
13 
14 #define MT7996_MAX_INTERFACES		19	/* per-band */
15 #define MT7996_MAX_WMM_SETS		4
16 #define MT7996_WTBL_RESERVED		(mt7996_wtbl_size(dev) - 1)
17 #define MT7996_WTBL_STA			(MT7996_WTBL_RESERVED - \
18 					 mt7996_max_interface_num(dev))
19 
20 #define MT7996_WATCHDOG_TIME		(HZ / 10)
21 #define MT7996_RESET_TIMEOUT		(30 * HZ)
22 
23 #define MT7996_TX_RING_SIZE		2048
24 #define MT7996_TX_MCU_RING_SIZE		256
25 #define MT7996_TX_FWDL_RING_SIZE	128
26 
27 #define MT7996_RX_RING_SIZE		1536
28 #define MT7996_RX_MCU_RING_SIZE		512
29 #define MT7996_RX_MCU_RING_SIZE_WA	1024
30 
31 #define MT7996_FIRMWARE_WA		"mediatek/mt7996/mt7996_wa.bin"
32 #define MT7996_FIRMWARE_WM		"mediatek/mt7996/mt7996_wm.bin"
33 #define MT7996_FIRMWARE_DSP		"mediatek/mt7996/mt7996_dsp.bin"
34 #define MT7996_ROM_PATCH		"mediatek/mt7996/mt7996_rom_patch.bin"
35 
36 #define MT7996_EEPROM_DEFAULT		"mediatek/mt7996/mt7996_eeprom.bin"
37 #define MT7996_EEPROM_SIZE		7680
38 #define MT7996_EEPROM_BLOCK_SIZE	16
39 #define MT7996_TOKEN_SIZE		16384
40 
41 #define MT7996_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
42 #define MT7996_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
43 
44 #define MT7996_MAX_TWT_AGRT		16
45 #define MT7996_MAX_STA_TWT_AGRT		8
46 #define MT7996_MAX_QUEUE		(__MT_RXQ_MAX +	__MT_MCUQ_MAX + 3)
47 
48 /* NOTE: used to map mt76_rates. idx may change if firmware expands table */
49 #define MT7996_BASIC_RATES_TBL		11
50 #define MT7996_BEACON_RATES_TBL		25
51 
52 struct mt7996_vif;
53 struct mt7996_sta;
54 struct mt7996_dfs_pulse;
55 struct mt7996_dfs_pattern;
56 
57 enum mt7996_ram_type {
58 	MT7996_RAM_TYPE_WM,
59 	MT7996_RAM_TYPE_WA,
60 	MT7996_RAM_TYPE_DSP,
61 };
62 
63 enum mt7996_txq_id {
64 	MT7996_TXQ_FWDL = 16,
65 	MT7996_TXQ_MCU_WM,
66 	MT7996_TXQ_BAND0,
67 	MT7996_TXQ_BAND1,
68 	MT7996_TXQ_MCU_WA,
69 	MT7996_TXQ_BAND2,
70 };
71 
72 enum mt7996_rxq_id {
73 	MT7996_RXQ_MCU_WM = 0,
74 	MT7996_RXQ_MCU_WA,
75 	MT7996_RXQ_MCU_WA_MAIN = 2,
76 	MT7996_RXQ_MCU_WA_EXT = 2,/* unused */
77 	MT7996_RXQ_MCU_WA_TRI = 3,
78 	MT7996_RXQ_BAND0 = 4,
79 	MT7996_RXQ_BAND1 = 4,/* unused */
80 	MT7996_RXQ_BAND2 = 5,
81 };
82 
83 struct mt7996_twt_flow {
84 	struct list_head list;
85 	u64 start_tsf;
86 	u64 tsf;
87 	u32 duration;
88 	u16 wcid;
89 	__le16 mantissa;
90 	u8 exp;
91 	u8 table_id;
92 	u8 id;
93 	u8 protection:1;
94 	u8 flowtype:1;
95 	u8 trigger:1;
96 	u8 sched:1;
97 };
98 
99 DECLARE_EWMA(avg_signal, 10, 8)
100 
101 struct mt7996_sta {
102 	struct mt76_wcid wcid; /* must be first */
103 
104 	struct mt7996_vif *vif;
105 
106 	struct list_head rc_list;
107 	u32 airtime_ac[8];
108 
109 	int ack_signal;
110 	struct ewma_avg_signal avg_ack_signal;
111 
112 	unsigned long changed;
113 
114 	struct mt76_connac_sta_key_conf bip;
115 
116 	struct {
117 		u8 flowid_mask;
118 		struct mt7996_twt_flow flow[MT7996_MAX_STA_TWT_AGRT];
119 	} twt;
120 };
121 
122 struct mt7996_vif {
123 	struct mt76_vif mt76; /* must be first */
124 
125 	struct mt7996_sta sta;
126 	struct mt7996_phy *phy;
127 
128 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
129 	struct cfg80211_bitrate_mask bitrate_mask;
130 };
131 
132 /* crash-dump */
133 struct mt7996_crash_data {
134 	guid_t guid;
135 	struct timespec64 timestamp;
136 
137 	u8 *memdump_buf;
138 	size_t memdump_buf_len;
139 };
140 
141 struct mt7996_hif {
142 	struct list_head list;
143 
144 	struct device *dev;
145 	void __iomem *regs;
146 	int irq;
147 };
148 
149 struct mt7996_phy {
150 	struct mt76_phy *mt76;
151 	struct mt7996_dev *dev;
152 
153 	struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES];
154 
155 	struct ieee80211_vif *monitor_vif;
156 
157 	u32 rxfilter;
158 	u64 omac_mask;
159 
160 	u16 noise;
161 
162 	s16 coverage_class;
163 	u8 slottime;
164 
165 	u8 rdd_state;
166 
167 	u32 rx_ampdu_ts;
168 	u32 ampdu_ref;
169 
170 	struct mt76_mib_stats mib;
171 	struct mt76_channel_state state_ts;
172 };
173 
174 struct mt7996_dev {
175 	union { /* must be first */
176 		struct mt76_dev mt76;
177 		struct mt76_phy mphy;
178 	};
179 
180 	struct mt7996_hif *hif2;
181 	struct mt7996_reg_desc reg;
182 	u8 q_id[MT7996_MAX_QUEUE];
183 	u32 q_int_mask[MT7996_MAX_QUEUE];
184 	u32 q_wfdma_mask;
185 
186 	const struct mt76_bus_ops *bus_ops;
187 	struct mt7996_phy phy;
188 
189 	/* monitor rx chain configured channel */
190 	struct cfg80211_chan_def rdd2_chandef;
191 	struct mt7996_phy *rdd2_phy;
192 
193 	u16 chainmask;
194 	u8 chainshift[__MT_MAX_BAND];
195 	u32 hif_idx;
196 
197 	struct work_struct init_work;
198 	struct work_struct rc_work;
199 	struct work_struct dump_work;
200 	struct work_struct reset_work;
201 	wait_queue_head_t reset_wait;
202 	struct {
203 		u32 state;
204 		u32 wa_reset_count;
205 		u32 wm_reset_count;
206 		bool hw_full_reset:1;
207 		bool hw_init_done:1;
208 		bool restart:1;
209 	} recovery;
210 
211 	/* protects coredump data */
212 	struct mutex dump_mutex;
213 #ifdef CONFIG_DEV_COREDUMP
214 	struct {
215 		struct mt7996_crash_data *crash_data;
216 	} coredump;
217 #endif
218 
219 	struct list_head sta_rc_list;
220 	struct list_head twt_list;
221 
222 	u32 hw_pattern;
223 
224 	bool dbdc_support:1;
225 	bool tbtc_support:1;
226 	bool flash_mode:1;
227 	bool has_eht:1;
228 
229 	bool ibf;
230 	u8 fw_debug_wm;
231 	u8 fw_debug_wa;
232 	u8 fw_debug_bin;
233 	u16 fw_debug_seq;
234 
235 	struct dentry *debugfs_dir;
236 	struct rchan *relay_fwlog;
237 
238 	struct {
239 		u8 table_mask;
240 		u8 n_agrt;
241 	} twt;
242 
243 	u32 reg_l1_backup;
244 	u32 reg_l2_backup;
245 
246 	u8 wtbl_size_group;
247 };
248 
249 enum {
250 	WFDMA0 = 0x0,
251 	WFDMA1,
252 	WFDMA_EXT,
253 	__MT_WFDMA_MAX,
254 };
255 
256 enum {
257 	MT_RX_SEL0,
258 	MT_RX_SEL1,
259 	MT_RX_SEL2, /* monitor chain */
260 };
261 
262 enum mt7996_rdd_cmd {
263 	RDD_STOP,
264 	RDD_START,
265 	RDD_DET_MODE,
266 	RDD_RADAR_EMULATE,
267 	RDD_START_TXQ = 20,
268 	RDD_CAC_START = 50,
269 	RDD_CAC_END,
270 	RDD_NORMAL_START,
271 	RDD_DISABLE_DFS_CAL,
272 	RDD_PULSE_DBG,
273 	RDD_READ_PULSE,
274 	RDD_RESUME_BF,
275 	RDD_IRQ_OFF,
276 };
277 
278 static inline struct mt7996_phy *
279 mt7996_hw_phy(struct ieee80211_hw *hw)
280 {
281 	struct mt76_phy *phy = hw->priv;
282 
283 	return phy->priv;
284 }
285 
286 static inline struct mt7996_dev *
287 mt7996_hw_dev(struct ieee80211_hw *hw)
288 {
289 	struct mt76_phy *phy = hw->priv;
290 
291 	return container_of(phy->dev, struct mt7996_dev, mt76);
292 }
293 
294 static inline struct mt7996_phy *
295 __mt7996_phy(struct mt7996_dev *dev, enum mt76_band_id band)
296 {
297 	struct mt76_phy *phy = dev->mt76.phys[band];
298 
299 	if (!phy)
300 		return NULL;
301 
302 	return phy->priv;
303 }
304 
305 static inline struct mt7996_phy *
306 mt7996_phy2(struct mt7996_dev *dev)
307 {
308 	return __mt7996_phy(dev, MT_BAND1);
309 }
310 
311 static inline struct mt7996_phy *
312 mt7996_phy3(struct mt7996_dev *dev)
313 {
314 	return __mt7996_phy(dev, MT_BAND2);
315 }
316 
317 extern const struct ieee80211_ops mt7996_ops;
318 extern struct pci_driver mt7996_pci_driver;
319 extern struct pci_driver mt7996_hif_driver;
320 
321 struct mt7996_dev *mt7996_mmio_probe(struct device *pdev,
322 				     void __iomem *mem_base, u32 device_id);
323 void mt7996_wfsys_reset(struct mt7996_dev *dev);
324 irqreturn_t mt7996_irq_handler(int irq, void *dev_instance);
325 u64 __mt7996_get_tsf(struct ieee80211_hw *hw, struct mt7996_vif *mvif);
326 int mt7996_register_device(struct mt7996_dev *dev);
327 void mt7996_unregister_device(struct mt7996_dev *dev);
328 int mt7996_eeprom_init(struct mt7996_dev *dev);
329 int mt7996_eeprom_parse_hw_cap(struct mt7996_dev *dev, struct mt7996_phy *phy);
330 int mt7996_eeprom_get_target_power(struct mt7996_dev *dev,
331 				   struct ieee80211_channel *chan);
332 s8 mt7996_eeprom_get_power_delta(struct mt7996_dev *dev, int band);
333 int mt7996_dma_init(struct mt7996_dev *dev);
334 void mt7996_dma_reset(struct mt7996_dev *dev, bool force);
335 void mt7996_dma_prefetch(struct mt7996_dev *dev);
336 void mt7996_dma_cleanup(struct mt7996_dev *dev);
337 void mt7996_dma_start(struct mt7996_dev *dev, bool reset);
338 void mt7996_init_txpower(struct mt7996_dev *dev,
339 			 struct ieee80211_supported_band *sband);
340 int mt7996_txbf_init(struct mt7996_dev *dev);
341 void mt7996_reset(struct mt7996_dev *dev);
342 int mt7996_run(struct ieee80211_hw *hw);
343 int mt7996_mcu_init(struct mt7996_dev *dev);
344 int mt7996_mcu_init_firmware(struct mt7996_dev *dev);
345 int mt7996_mcu_twt_agrt_update(struct mt7996_dev *dev,
346 			       struct mt7996_vif *mvif,
347 			       struct mt7996_twt_flow *flow,
348 			       int cmd);
349 int mt7996_mcu_add_dev_info(struct mt7996_phy *phy,
350 			    struct ieee80211_vif *vif, bool enable);
351 int mt7996_mcu_add_bss_info(struct mt7996_phy *phy,
352 			    struct ieee80211_vif *vif, int enable);
353 int mt7996_mcu_add_sta(struct mt7996_dev *dev, struct ieee80211_vif *vif,
354 		       struct ieee80211_sta *sta, bool enable);
355 int mt7996_mcu_add_tx_ba(struct mt7996_dev *dev,
356 			 struct ieee80211_ampdu_params *params,
357 			 bool add);
358 int mt7996_mcu_add_rx_ba(struct mt7996_dev *dev,
359 			 struct ieee80211_ampdu_params *params,
360 			 bool add);
361 int mt7996_mcu_update_bss_color(struct mt7996_dev *dev, struct ieee80211_vif *vif,
362 				struct cfg80211_he_bss_color *he_bss_color);
363 int mt7996_mcu_add_beacon(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
364 			  int enable);
365 int mt7996_mcu_beacon_inband_discov(struct mt7996_dev *dev,
366 				    struct ieee80211_vif *vif, u32 changed);
367 int mt7996_mcu_add_obss_spr(struct mt7996_phy *phy, struct ieee80211_vif *vif,
368 			    struct ieee80211_he_obss_pd *he_obss_pd);
369 int mt7996_mcu_add_rate_ctrl(struct mt7996_dev *dev, struct ieee80211_vif *vif,
370 			     struct ieee80211_sta *sta, bool changed);
371 int mt7996_set_channel(struct mt7996_phy *phy);
372 int mt7996_mcu_set_chan_info(struct mt7996_phy *phy, u16 tag);
373 int mt7996_mcu_set_tx(struct mt7996_dev *dev, struct ieee80211_vif *vif);
374 int mt7996_mcu_set_fixed_rate_ctrl(struct mt7996_dev *dev,
375 				   void *data, u16 version);
376 int mt7996_mcu_set_eeprom(struct mt7996_dev *dev);
377 int mt7996_mcu_get_eeprom(struct mt7996_dev *dev, u32 offset);
378 int mt7996_mcu_get_eeprom_free_block(struct mt7996_dev *dev, u8 *block_num);
379 int mt7996_mcu_get_chip_config(struct mt7996_dev *dev, u32 *cap);
380 int mt7996_mcu_set_ser(struct mt7996_dev *dev, u8 action, u8 set, u8 band);
381 int mt7996_mcu_set_txbf(struct mt7996_dev *dev, u8 action);
382 int mt7996_mcu_set_fcc5_lpn(struct mt7996_dev *dev, int val);
383 int mt7996_mcu_set_pulse_th(struct mt7996_dev *dev,
384 			    const struct mt7996_dfs_pulse *pulse);
385 int mt7996_mcu_set_radar_th(struct mt7996_dev *dev, int index,
386 			    const struct mt7996_dfs_pattern *pattern);
387 int mt7996_mcu_set_radio_en(struct mt7996_phy *phy, bool enable);
388 int mt7996_mcu_set_rts_thresh(struct mt7996_phy *phy, u32 val);
389 int mt7996_mcu_set_timing(struct mt7996_phy *phy, struct ieee80211_vif *vif);
390 int mt7996_mcu_get_chan_mib_info(struct mt7996_phy *phy, bool chan_switch);
391 int mt7996_mcu_rdd_cmd(struct mt7996_dev *dev, int cmd, u8 index,
392 		       u8 rx_sel, u8 val);
393 int mt7996_mcu_rdd_background_enable(struct mt7996_phy *phy,
394 				     struct cfg80211_chan_def *chandef);
395 int mt7996_mcu_rf_regval(struct mt7996_dev *dev, u32 regidx, u32 *val, bool set);
396 int mt7996_mcu_set_hdr_trans(struct mt7996_dev *dev, bool hdr_trans);
397 int mt7996_mcu_set_rro(struct mt7996_dev *dev, u16 tag, u8 val);
398 int mt7996_mcu_wa_cmd(struct mt7996_dev *dev, int cmd, u32 a1, u32 a2, u32 a3);
399 int mt7996_mcu_fw_log_2_host(struct mt7996_dev *dev, u8 type, u8 ctrl);
400 int mt7996_mcu_fw_dbg_ctrl(struct mt7996_dev *dev, u32 module, u8 level);
401 int mt7996_mcu_trigger_assert(struct mt7996_dev *dev);
402 void mt7996_mcu_rx_event(struct mt7996_dev *dev, struct sk_buff *skb);
403 void mt7996_mcu_exit(struct mt7996_dev *dev);
404 int mt7996_mcu_get_all_sta_info(struct mt7996_phy *phy, u16 tag);
405 
406 static inline u8 mt7996_max_interface_num(struct mt7996_dev *dev)
407 {
408 	return MT7996_MAX_INTERFACES * (1 + dev->dbdc_support + dev->tbtc_support);
409 }
410 
411 static inline u16 mt7996_wtbl_size(struct mt7996_dev *dev)
412 {
413 	return (dev->wtbl_size_group << 8) + 64;
414 }
415 
416 void mt7996_dual_hif_set_irq_mask(struct mt7996_dev *dev, bool write_reg,
417 				  u32 clear, u32 set);
418 
419 static inline void mt7996_irq_enable(struct mt7996_dev *dev, u32 mask)
420 {
421 	if (dev->hif2)
422 		mt7996_dual_hif_set_irq_mask(dev, false, 0, mask);
423 	else
424 		mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
425 
426 	tasklet_schedule(&dev->mt76.irq_tasklet);
427 }
428 
429 static inline void mt7996_irq_disable(struct mt7996_dev *dev, u32 mask)
430 {
431 	if (dev->hif2)
432 		mt7996_dual_hif_set_irq_mask(dev, true, mask, 0);
433 	else
434 		mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, mask, 0);
435 }
436 
437 void mt7996_memcpy_fromio(struct mt7996_dev *dev, void *buf, u32 offset,
438 			  size_t len);
439 
440 void mt7996_mac_init(struct mt7996_dev *dev);
441 u32 mt7996_mac_wtbl_lmac_addr(struct mt7996_dev *dev, u16 wcid, u8 dw);
442 bool mt7996_mac_wtbl_update(struct mt7996_dev *dev, int idx, u32 mask);
443 void mt7996_mac_reset_counters(struct mt7996_phy *phy);
444 void mt7996_mac_cca_stats_reset(struct mt7996_phy *phy);
445 void mt7996_mac_enable_nf(struct mt7996_dev *dev, u8 band);
446 void mt7996_mac_enable_rtscts(struct mt7996_dev *dev,
447 			      struct ieee80211_vif *vif, bool enable);
448 void mt7996_mac_set_fixed_rate_table(struct mt7996_dev *dev,
449 				     u8 tbl_idx, u16 rate_idx);
450 void mt7996_mac_write_txwi(struct mt7996_dev *dev, __le32 *txwi,
451 			   struct sk_buff *skb, struct mt76_wcid *wcid,
452 			   struct ieee80211_key_conf *key, int pid,
453 			   enum mt76_txq_id qid, u32 changed);
454 void mt7996_mac_set_coverage_class(struct mt7996_phy *phy);
455 int mt7996_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
456 		       struct ieee80211_sta *sta);
457 void mt7996_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
458 			   struct ieee80211_sta *sta);
459 void mt7996_mac_work(struct work_struct *work);
460 void mt7996_mac_reset_work(struct work_struct *work);
461 void mt7996_mac_dump_work(struct work_struct *work);
462 void mt7996_mac_sta_rc_work(struct work_struct *work);
463 void mt7996_mac_update_stats(struct mt7996_phy *phy);
464 void mt7996_mac_twt_teardown_flow(struct mt7996_dev *dev,
465 				  struct mt7996_sta *msta,
466 				  u8 flowid);
467 void mt7996_mac_add_twt_setup(struct ieee80211_hw *hw,
468 			      struct ieee80211_sta *sta,
469 			      struct ieee80211_twt_setup *twt);
470 int mt7996_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
471 			  enum mt76_txq_id qid, struct mt76_wcid *wcid,
472 			  struct ieee80211_sta *sta,
473 			  struct mt76_tx_info *tx_info);
474 void mt7996_tx_token_put(struct mt7996_dev *dev);
475 void mt7996_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
476 			 struct sk_buff *skb, u32 *info);
477 bool mt7996_rx_check(struct mt76_dev *mdev, void *data, int len);
478 void mt7996_stats_work(struct work_struct *work);
479 int mt76_dfs_start_rdd(struct mt7996_dev *dev, bool force);
480 int mt7996_dfs_init_radar_detector(struct mt7996_phy *phy);
481 void mt7996_set_stream_he_eht_caps(struct mt7996_phy *phy);
482 void mt7996_set_stream_vht_txbf_caps(struct mt7996_phy *phy);
483 void mt7996_update_channel(struct mt76_phy *mphy);
484 int mt7996_init_debugfs(struct mt7996_phy *phy);
485 void mt7996_debugfs_rx_fw_monitor(struct mt7996_dev *dev, const void *data, int len);
486 bool mt7996_debugfs_rx_log(struct mt7996_dev *dev, const void *data, int len);
487 int mt7996_mcu_add_key(struct mt76_dev *dev, struct ieee80211_vif *vif,
488 		       struct mt76_connac_sta_key_conf *sta_key_conf,
489 		       struct ieee80211_key_conf *key, int mcu_cmd,
490 		       struct mt76_wcid *wcid, enum set_key_cmd cmd);
491 int mt7996_mcu_wtbl_update_hdr_trans(struct mt7996_dev *dev,
492 				     struct ieee80211_vif *vif,
493 				     struct ieee80211_sta *sta);
494 #ifdef CONFIG_MAC80211_DEBUGFS
495 void mt7996_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
496 			    struct ieee80211_sta *sta, struct dentry *dir);
497 #endif
498 
499 #endif
500