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		19
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_DEFAULT		"mediatek/mt7915_eeprom.bin"
34 #define MT7915_EEPROM_DEFAULT_DBDC	"mediatek/mt7915_eeprom_dbdc.bin"
35 
36 #define MT7915_EEPROM_SIZE		3584
37 #define MT7915_EEPROM_BLOCK_SIZE	16
38 #define MT7915_TOKEN_SIZE		8192
39 
40 #define MT7915_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
41 #define MT7915_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
42 
43 #define MT7915_THERMAL_THROTTLE_MAX	100
44 
45 #define MT7915_SKU_RATE_NUM		161
46 
47 #define MT7915_MAX_TWT_AGRT		16
48 #define MT7915_MAX_STA_TWT_AGRT		8
49 
50 struct mt7915_vif;
51 struct mt7915_sta;
52 struct mt7915_dfs_pulse;
53 struct mt7915_dfs_pattern;
54 
55 enum mt7915_txq_id {
56 	MT7915_TXQ_FWDL = 16,
57 	MT7915_TXQ_MCU_WM,
58 	MT7915_TXQ_BAND0,
59 	MT7915_TXQ_BAND1,
60 	MT7915_TXQ_MCU_WA,
61 };
62 
63 enum mt7915_rxq_id {
64 	MT7915_RXQ_BAND0 = 0,
65 	MT7915_RXQ_BAND1,
66 	MT7915_RXQ_MCU_WM = 0,
67 	MT7915_RXQ_MCU_WA,
68 	MT7915_RXQ_MCU_WA_EXT,
69 };
70 
71 struct mt7915_sta_key_conf {
72 	s8 keyidx;
73 	u8 key[16];
74 };
75 
76 struct mt7915_twt_flow {
77 	struct list_head list;
78 	u64 start_tsf;
79 	u64 tsf;
80 	u32 duration;
81 	u16 wcid;
82 	__le16 mantissa;
83 	u8 exp;
84 	u8 table_id;
85 	u8 id;
86 	u8 protection:1;
87 	u8 flowtype:1;
88 	u8 trigger:1;
89 	u8 sched:1;
90 };
91 
92 struct mt7915_sta {
93 	struct mt76_wcid wcid; /* must be first */
94 
95 	struct mt7915_vif *vif;
96 
97 	struct list_head poll_list;
98 	struct list_head rc_list;
99 	u32 airtime_ac[8];
100 
101 	unsigned long changed;
102 	unsigned long jiffies;
103 	unsigned long ampdu_state;
104 
105 	struct mt76_sta_stats stats;
106 
107 	struct mt7915_sta_key_conf bip;
108 
109 	struct {
110 		u8 flowid_mask;
111 		struct mt7915_twt_flow flow[MT7915_MAX_STA_TWT_AGRT];
112 	} twt;
113 };
114 
115 struct mt7915_vif_cap {
116 	bool ldpc:1;
117 	bool vht_su_ebfer:1;
118 	bool vht_su_ebfee:1;
119 	bool vht_mu_ebfer:1;
120 	bool vht_mu_ebfee:1;
121 	bool he_su_ebfer:1;
122 	bool he_su_ebfee:1;
123 	bool he_mu_ebfer:1;
124 };
125 
126 struct mt7915_vif {
127 	struct mt76_vif mt76; /* must be first */
128 
129 	struct mt7915_vif_cap cap;
130 	struct mt7915_sta sta;
131 	struct mt7915_phy *phy;
132 
133 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
134 	struct cfg80211_bitrate_mask bitrate_mask;
135 };
136 
137 /* per-phy stats.  */
138 struct mib_stats {
139 	u32 ack_fail_cnt;
140 	u32 fcs_err_cnt;
141 	u32 rts_cnt;
142 	u32 rts_retries_cnt;
143 	u32 ba_miss_cnt;
144 	u32 tx_bf_cnt;
145 	u32 tx_mu_mpdu_cnt;
146 	u32 tx_mu_acked_mpdu_cnt;
147 	u32 tx_su_acked_mpdu_cnt;
148 	u32 tx_bf_ibf_ppdu_cnt;
149 	u32 tx_bf_ebf_ppdu_cnt;
150 
151 	u32 tx_bf_rx_fb_all_cnt;
152 	u32 tx_bf_rx_fb_he_cnt;
153 	u32 tx_bf_rx_fb_vht_cnt;
154 	u32 tx_bf_rx_fb_ht_cnt;
155 
156 	u32 tx_bf_rx_fb_bw; /* value of last sample, not cumulative */
157 	u32 tx_bf_rx_fb_nc_cnt;
158 	u32 tx_bf_rx_fb_nr_cnt;
159 	u32 tx_bf_fb_cpl_cnt;
160 	u32 tx_bf_fb_trig_cnt;
161 
162 	u32 tx_ampdu_cnt;
163 	u32 tx_stop_q_empty_cnt;
164 	u32 tx_mpdu_attempts_cnt;
165 	u32 tx_mpdu_success_cnt;
166 	u32 tx_pkt_ebf_cnt;
167 	u32 tx_pkt_ibf_cnt;
168 
169 	u32 tx_rwp_fail_cnt;
170 	u32 tx_rwp_need_cnt;
171 
172 	/* rx stats */
173 	u32 rx_fifo_full_cnt;
174 	u32 channel_idle_cnt;
175 	u32 rx_vector_mismatch_cnt;
176 	u32 rx_delimiter_fail_cnt;
177 	u32 rx_len_mismatch_cnt;
178 	u32 rx_mpdu_cnt;
179 	u32 rx_ampdu_cnt;
180 	u32 rx_ampdu_bytes_cnt;
181 	u32 rx_ampdu_valid_subframe_cnt;
182 	u32 rx_ampdu_valid_subframe_bytes_cnt;
183 	u32 rx_pfdrop_cnt;
184 	u32 rx_vec_queue_overflow_drop_cnt;
185 	u32 rx_ba_cnt;
186 
187 	u32 tx_amsdu[8];
188 	u32 tx_amsdu_cnt;
189 };
190 
191 struct mt7915_hif {
192 	struct list_head list;
193 
194 	struct device *dev;
195 	void __iomem *regs;
196 	int irq;
197 };
198 
199 struct mt7915_phy {
200 	struct mt76_phy *mt76;
201 	struct mt7915_dev *dev;
202 
203 	struct ieee80211_sband_iftype_data iftype[2][NUM_NL80211_IFTYPES];
204 
205 	struct ieee80211_vif *monitor_vif;
206 
207 	struct thermal_cooling_device *cdev;
208 	u8 throttle_state;
209 	u32 throttle_temp[2]; /* 0: critical high, 1: maximum */
210 
211 	u32 rxfilter;
212 	u64 omac_mask;
213 
214 	u16 noise;
215 
216 	s16 coverage_class;
217 	u8 slottime;
218 
219 	u8 rdd_state;
220 	int dfs_state;
221 
222 	u32 rx_ampdu_ts;
223 	u32 ampdu_ref;
224 
225 	struct mib_stats mib;
226 	struct mt76_channel_state state_ts;
227 
228 #ifdef CONFIG_NL80211_TESTMODE
229 	struct {
230 		u32 *reg_backup;
231 
232 		s32 last_freq_offset;
233 		u8 last_rcpi[4];
234 		s8 last_ib_rssi[4];
235 		s8 last_wb_rssi[4];
236 		u8 last_snr;
237 
238 		u8 spe_idx;
239 	} test;
240 #endif
241 };
242 
243 struct mt7915_dev {
244 	union { /* must be first */
245 		struct mt76_dev mt76;
246 		struct mt76_phy mphy;
247 	};
248 
249 	struct mt7915_hif *hif2;
250 
251 	const struct mt76_bus_ops *bus_ops;
252 	struct tasklet_struct irq_tasklet;
253 	struct mt7915_phy phy;
254 
255 	u16 chainmask;
256 	u32 hif_idx;
257 
258 	struct work_struct init_work;
259 	struct work_struct rc_work;
260 	struct work_struct reset_work;
261 	wait_queue_head_t reset_wait;
262 	u32 reset_state;
263 
264 	struct list_head sta_rc_list;
265 	struct list_head sta_poll_list;
266 	struct list_head twt_list;
267 	spinlock_t sta_poll_lock;
268 
269 	u32 hw_pattern;
270 
271 	bool dbdc_support;
272 	bool flash_mode;
273 	bool muru_debug;
274 	bool ibf;
275 	u8 fw_debug_wm;
276 	u8 fw_debug_wa;
277 
278 	void *cal;
279 
280 	struct {
281 		u8 table_mask;
282 		u8 n_agrt;
283 	} twt;
284 };
285 
286 enum {
287 	MT_CTX0,
288 	MT_HIF0 = 0x0,
289 
290 	MT_LMAC_AC00 = 0x0,
291 	MT_LMAC_AC01,
292 	MT_LMAC_AC02,
293 	MT_LMAC_AC03,
294 	MT_LMAC_ALTX0 = 0x10,
295 	MT_LMAC_BMC0,
296 	MT_LMAC_BCN0,
297 	MT_LMAC_PSMP0,
298 };
299 
300 enum {
301 	MT_RX_SEL0,
302 	MT_RX_SEL1,
303 };
304 
305 enum mt7915_rdd_cmd {
306 	RDD_STOP,
307 	RDD_START,
308 	RDD_DET_MODE,
309 	RDD_RADAR_EMULATE,
310 	RDD_START_TXQ = 20,
311 	RDD_CAC_START = 50,
312 	RDD_CAC_END,
313 	RDD_NORMAL_START,
314 	RDD_DISABLE_DFS_CAL,
315 	RDD_PULSE_DBG,
316 	RDD_READ_PULSE,
317 	RDD_RESUME_BF,
318 	RDD_IRQ_OFF,
319 };
320 
321 static inline struct mt7915_phy *
322 mt7915_hw_phy(struct ieee80211_hw *hw)
323 {
324 	struct mt76_phy *phy = hw->priv;
325 
326 	return phy->priv;
327 }
328 
329 static inline struct mt7915_dev *
330 mt7915_hw_dev(struct ieee80211_hw *hw)
331 {
332 	struct mt76_phy *phy = hw->priv;
333 
334 	return container_of(phy->dev, struct mt7915_dev, mt76);
335 }
336 
337 static inline struct mt7915_phy *
338 mt7915_ext_phy(struct mt7915_dev *dev)
339 {
340 	struct mt76_phy *phy = dev->mt76.phy2;
341 
342 	if (!phy)
343 		return NULL;
344 
345 	return phy->priv;
346 }
347 
348 static inline u8 mt7915_lmac_mapping(struct mt7915_dev *dev, u8 ac)
349 {
350 	/* LMAC uses the reverse order of mac80211 AC indexes */
351 	return 3 - ac;
352 }
353 
354 extern const struct ieee80211_ops mt7915_ops;
355 extern const struct mt76_testmode_ops mt7915_testmode_ops;
356 
357 u32 mt7915_reg_map(struct mt7915_dev *dev, u32 addr);
358 u64 __mt7915_get_tsf(struct ieee80211_hw *hw, struct mt7915_vif *mvif);
359 int mt7915_register_device(struct mt7915_dev *dev);
360 void mt7915_unregister_device(struct mt7915_dev *dev);
361 int mt7915_eeprom_init(struct mt7915_dev *dev);
362 void mt7915_eeprom_parse_band_config(struct mt7915_phy *phy);
363 int mt7915_eeprom_get_target_power(struct mt7915_dev *dev,
364 				   struct ieee80211_channel *chan,
365 				   u8 chain_idx);
366 s8 mt7915_eeprom_get_power_delta(struct mt7915_dev *dev, int band);
367 int mt7915_dma_init(struct mt7915_dev *dev);
368 void mt7915_dma_prefetch(struct mt7915_dev *dev);
369 void mt7915_dma_cleanup(struct mt7915_dev *dev);
370 int mt7915_mcu_init(struct mt7915_dev *dev);
371 int mt7915_mcu_twt_agrt_update(struct mt7915_dev *dev,
372 			       struct mt7915_vif *mvif,
373 			       struct mt7915_twt_flow *flow,
374 			       int cmd);
375 int mt7915_mcu_add_dev_info(struct mt7915_phy *phy,
376 			    struct ieee80211_vif *vif, bool enable);
377 int mt7915_mcu_add_bss_info(struct mt7915_phy *phy,
378 			    struct ieee80211_vif *vif, int enable);
379 int mt7915_mcu_add_sta(struct mt7915_dev *dev, struct ieee80211_vif *vif,
380 		       struct ieee80211_sta *sta, bool enable);
381 int mt7915_mcu_sta_update_hdr_trans(struct mt7915_dev *dev,
382 				    struct ieee80211_vif *vif,
383 				    struct ieee80211_sta *sta);
384 int mt7915_mcu_add_tx_ba(struct mt7915_dev *dev,
385 			 struct ieee80211_ampdu_params *params,
386 			 bool add);
387 int mt7915_mcu_add_rx_ba(struct mt7915_dev *dev,
388 			 struct ieee80211_ampdu_params *params,
389 			 bool add);
390 int mt7915_mcu_add_key(struct mt7915_dev *dev, struct ieee80211_vif *vif,
391 		       struct mt7915_sta *msta, struct ieee80211_key_conf *key,
392 		       enum set_key_cmd cmd);
393 int mt7915_mcu_update_bss_color(struct mt7915_dev *dev, struct ieee80211_vif *vif,
394 				struct cfg80211_he_bss_color *he_bss_color);
395 int mt7915_mcu_add_beacon(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
396 			  int enable);
397 int mt7915_mcu_add_obss_spr(struct mt7915_dev *dev, struct ieee80211_vif *vif,
398                             bool enable);
399 int mt7915_mcu_add_rate_ctrl(struct mt7915_dev *dev, struct ieee80211_vif *vif,
400 			     struct ieee80211_sta *sta, bool changed);
401 int mt7915_mcu_add_smps(struct mt7915_dev *dev, struct ieee80211_vif *vif,
402 			struct ieee80211_sta *sta);
403 int mt7915_set_channel(struct mt7915_phy *phy);
404 int mt7915_mcu_set_chan_info(struct mt7915_phy *phy, int cmd);
405 int mt7915_mcu_set_tx(struct mt7915_dev *dev, struct ieee80211_vif *vif);
406 int mt7915_mcu_update_edca(struct mt7915_dev *dev, void *req);
407 int mt7915_mcu_set_fixed_rate_ctrl(struct mt7915_dev *dev,
408 				   struct ieee80211_vif *vif,
409 				   struct ieee80211_sta *sta,
410 				   void *data, u32 field);
411 int mt7915_mcu_set_eeprom(struct mt7915_dev *dev);
412 int mt7915_mcu_get_eeprom(struct mt7915_dev *dev, u32 offset);
413 int mt7915_mcu_get_eeprom_free_block(struct mt7915_dev *dev, u8 *block_num);
414 int mt7915_mcu_set_mac(struct mt7915_dev *dev, int band, bool enable,
415 		       bool hdr_trans);
416 int mt7915_mcu_set_test_param(struct mt7915_dev *dev, u8 param, bool test_mode,
417 			      u8 en);
418 int mt7915_mcu_set_scs(struct mt7915_dev *dev, u8 band, bool enable);
419 int mt7915_mcu_set_ser(struct mt7915_dev *dev, u8 action, u8 set, u8 band);
420 int mt7915_mcu_set_rts_thresh(struct mt7915_phy *phy, u32 val);
421 int mt7915_mcu_set_pm(struct mt7915_dev *dev, int band, int enter);
422 int mt7915_mcu_set_sku_en(struct mt7915_phy *phy, bool enable);
423 int mt7915_mcu_set_txpower_sku(struct mt7915_phy *phy);
424 int mt7915_mcu_get_txpower_sku(struct mt7915_phy *phy, s8 *txpower, int len);
425 int mt7915_mcu_set_txbf(struct mt7915_dev *dev, u8 action);
426 int mt7915_mcu_set_fcc5_lpn(struct mt7915_dev *dev, int val);
427 int mt7915_mcu_set_pulse_th(struct mt7915_dev *dev,
428 			    const struct mt7915_dfs_pulse *pulse);
429 int mt7915_mcu_set_radar_th(struct mt7915_dev *dev, int index,
430 			    const struct mt7915_dfs_pattern *pattern);
431 int mt7915_mcu_set_muru_ctrl(struct mt7915_dev *dev, u32 cmd, u32 val);
432 int mt7915_mcu_apply_group_cal(struct mt7915_dev *dev);
433 int mt7915_mcu_apply_tx_dpd(struct mt7915_phy *phy);
434 int mt7915_mcu_get_chan_mib_info(struct mt7915_phy *phy, bool chan_switch);
435 int mt7915_mcu_get_temperature(struct mt7915_phy *phy);
436 int mt7915_mcu_set_thermal_throttling(struct mt7915_phy *phy, u8 state);
437 int mt7915_mcu_get_rx_rate(struct mt7915_phy *phy, struct ieee80211_vif *vif,
438 			   struct ieee80211_sta *sta, struct rate_info *rate);
439 int mt7915_mcu_rdd_cmd(struct mt7915_dev *dev, enum mt7915_rdd_cmd cmd,
440 		       u8 index, u8 rx_sel, u8 val);
441 int mt7915_mcu_wa_cmd(struct mt7915_dev *dev, int cmd, u32 a1, u32 a2, u32 a3);
442 int mt7915_mcu_fw_log_2_host(struct mt7915_dev *dev, u8 type, u8 ctrl);
443 int mt7915_mcu_fw_dbg_ctrl(struct mt7915_dev *dev, u32 module, u8 level);
444 void mt7915_mcu_rx_event(struct mt7915_dev *dev, struct sk_buff *skb);
445 void mt7915_mcu_exit(struct mt7915_dev *dev);
446 
447 static inline bool is_mt7915(struct mt76_dev *dev)
448 {
449 	return mt76_chip(dev) == 0x7915;
450 }
451 
452 void mt7915_dual_hif_set_irq_mask(struct mt7915_dev *dev, bool write_reg,
453 				  u32 clear, u32 set);
454 
455 static inline void mt7915_irq_enable(struct mt7915_dev *dev, u32 mask)
456 {
457 	if (dev->hif2)
458 		mt7915_dual_hif_set_irq_mask(dev, false, 0, mask);
459 	else
460 		mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
461 
462 	tasklet_schedule(&dev->irq_tasklet);
463 }
464 
465 static inline void mt7915_irq_disable(struct mt7915_dev *dev, u32 mask)
466 {
467 	if (dev->hif2)
468 		mt7915_dual_hif_set_irq_mask(dev, true, mask, 0);
469 	else
470 		mt76_set_irq_mask(&dev->mt76, MT_INT_MASK_CSR, mask, 0);
471 }
472 
473 u32 mt7915_mac_wtbl_lmac_addr(struct mt7915_dev *dev, u16 wcid, u8 dw);
474 bool mt7915_mac_wtbl_update(struct mt7915_dev *dev, int idx, u32 mask);
475 void mt7915_mac_reset_counters(struct mt7915_phy *phy);
476 void mt7915_mac_cca_stats_reset(struct mt7915_phy *phy);
477 void mt7915_mac_enable_nf(struct mt7915_dev *dev, bool ext_phy);
478 void mt7915_mac_write_txwi(struct mt7915_dev *dev, __le32 *txwi,
479 			   struct sk_buff *skb, struct mt76_wcid *wcid, int pid,
480 			   struct ieee80211_key_conf *key, bool beacon);
481 void mt7915_mac_set_timing(struct mt7915_phy *phy);
482 int mt7915_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
483 		       struct ieee80211_sta *sta);
484 void mt7915_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
485 			   struct ieee80211_sta *sta);
486 void mt7915_mac_work(struct work_struct *work);
487 void mt7915_mac_reset_work(struct work_struct *work);
488 void mt7915_mac_sta_rc_work(struct work_struct *work);
489 void mt7915_mac_update_stats(struct mt7915_phy *phy);
490 int mt7915_mmio_init(struct mt76_dev *mdev, void __iomem *mem_base, int irq);
491 void mt7915_mac_twt_teardown_flow(struct mt7915_dev *dev,
492 				  struct mt7915_sta *msta,
493 				  u8 flowid);
494 void mt7915_mac_add_twt_setup(struct ieee80211_hw *hw,
495 			      struct ieee80211_sta *sta,
496 			      struct ieee80211_twt_setup *twt);
497 int mt7915_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
498 			  enum mt76_txq_id qid, struct mt76_wcid *wcid,
499 			  struct ieee80211_sta *sta,
500 			  struct mt76_tx_info *tx_info);
501 void mt7915_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e);
502 void mt7915_tx_token_put(struct mt7915_dev *dev);
503 int mt7915_init_tx_queues(struct mt7915_phy *phy, int idx, int n_desc);
504 void mt7915_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
505 			 struct sk_buff *skb);
506 bool mt7915_rx_check(struct mt76_dev *mdev, void *data, int len);
507 void mt7915_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
508 void mt7915_stats_work(struct work_struct *work);
509 int mt76_dfs_start_rdd(struct mt7915_dev *dev, bool force);
510 int mt7915_dfs_init_radar_detector(struct mt7915_phy *phy);
511 void mt7915_set_stream_he_caps(struct mt7915_phy *phy);
512 void mt7915_set_stream_vht_txbf_caps(struct mt7915_phy *phy);
513 void mt7915_update_channel(struct mt76_phy *mphy);
514 int mt7915_mcu_muru_debug_set(struct mt7915_dev *dev, bool enable);
515 int mt7915_mcu_muru_debug_get(struct mt7915_phy *phy, void *ms);
516 int mt7915_init_debugfs(struct mt7915_phy *phy);
517 #ifdef CONFIG_MAC80211_DEBUGFS
518 void mt7915_sta_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
519 			    struct ieee80211_sta *sta, struct dentry *dir);
520 #endif
521 
522 #endif
523