xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt792x.h (revision cbb3ec25)
1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2023 MediaTek Inc. */
3 
4 #ifndef __MT792X_H
5 #define __MT792X_H
6 
7 #include <linux/interrupt.h>
8 #include <linux/ktime.h>
9 
10 #include "mt76_connac_mcu.h"
11 #include "mt792x_regs.h"
12 #include "mt792x_acpi_sar.h"
13 
14 #define MT792x_PM_TIMEOUT	(HZ / 12)
15 #define MT792x_HW_SCAN_TIMEOUT	(HZ / 10)
16 
17 #define MT792x_MAX_INTERFACES	4
18 #define MT792x_WTBL_SIZE	20
19 #define MT792x_WTBL_RESERVED	(MT792x_WTBL_SIZE - 1)
20 #define MT792x_WTBL_STA		(MT792x_WTBL_RESERVED - MT792x_MAX_INTERFACES)
21 
22 #define MT792x_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
23 #define MT792x_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
24 
25 #define MT792x_FW_TAG_FEATURE	4
26 #define MT792x_FW_CAP_CNM	BIT(7)
27 
28 /* NOTE: used to map mt76_rates. idx may change if firmware expands table */
29 #define MT792x_BASIC_RATES_TBL	11
30 
31 #define MT792x_WATCHDOG_TIME	(HZ / 4)
32 
33 #define MT792x_DRV_OWN_RETRY_COUNT	10
34 #define MT792x_MCU_INIT_RETRY_COUNT	10
35 #define MT792x_WFSYS_INIT_RETRY_COUNT	2
36 
37 #define MT7921_FIRMWARE_WM	"mediatek/WIFI_RAM_CODE_MT7961_1.bin"
38 #define MT7922_FIRMWARE_WM	"mediatek/WIFI_RAM_CODE_MT7922_1.bin"
39 
40 #define MT7921_ROM_PATCH	"mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin"
41 #define MT7922_ROM_PATCH	"mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin"
42 
43 struct mt792x_vif;
44 struct mt792x_sta;
45 
46 struct mt792x_realease_info {
47 	__le16 len;
48 	u8 pad_len;
49 	u8 tag;
50 } __packed;
51 
52 struct mt792x_fw_features {
53 	u8 segment;
54 	u8 data;
55 	u8 rsv[14];
56 } __packed;
57 
58 enum {
59 	MT792x_CLC_POWER,
60 	MT792x_CLC_CHAN,
61 	MT792x_CLC_MAX_NUM,
62 };
63 
64 DECLARE_EWMA(avg_signal, 10, 8)
65 
66 struct mt792x_sta {
67 	struct mt76_wcid wcid; /* must be first */
68 
69 	struct mt792x_vif *vif;
70 
71 	u32 airtime_ac[8];
72 
73 	int ack_signal;
74 	struct ewma_avg_signal avg_ack_signal;
75 
76 	unsigned long last_txs;
77 
78 	struct mt76_connac_sta_key_conf bip;
79 };
80 
81 DECLARE_EWMA(rssi, 10, 8);
82 
83 struct mt792x_vif {
84 	struct mt76_vif mt76; /* must be first */
85 
86 	struct mt792x_sta sta;
87 	struct mt792x_sta *wep_sta;
88 
89 	struct mt792x_phy *phy;
90 
91 	struct ewma_rssi rssi;
92 
93 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
94 	struct ieee80211_chanctx_conf *ctx;
95 };
96 
97 struct mt792x_phy {
98 	struct mt76_phy *mt76;
99 	struct mt792x_dev *dev;
100 
101 	struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES];
102 
103 	u64 omac_mask;
104 
105 	u16 noise;
106 
107 	s16 coverage_class;
108 	u8 slottime;
109 
110 	u32 rx_ampdu_ts;
111 	u32 ampdu_ref;
112 
113 	struct mt76_mib_stats mib;
114 
115 	u8 sta_work_count;
116 
117 	struct sk_buff_head scan_event_list;
118 	struct delayed_work scan_work;
119 #ifdef CONFIG_ACPI
120 	void *acpisar;
121 #endif
122 	void *clc[MT792x_CLC_MAX_NUM];
123 
124 	struct work_struct roc_work;
125 	struct timer_list roc_timer;
126 	wait_queue_head_t roc_wait;
127 	u8 roc_token_id;
128 	bool roc_grant;
129 };
130 
131 struct mt792x_irq_map {
132 	u32 host_irq_enable;
133 	struct {
134 		u32 all_complete_mask;
135 		u32 mcu_complete_mask;
136 	} tx;
137 	struct {
138 		u32 data_complete_mask;
139 		u32 wm_complete_mask;
140 		u32 wm2_complete_mask;
141 	} rx;
142 };
143 
144 #define mt792x_init_reset(dev)		((dev)->hif_ops->init_reset(dev))
145 #define mt792x_dev_reset(dev)		((dev)->hif_ops->reset(dev))
146 #define mt792x_mcu_init(dev)		((dev)->hif_ops->mcu_init(dev))
147 #define __mt792x_mcu_drv_pmctrl(dev)	((dev)->hif_ops->drv_own(dev))
148 #define	__mt792x_mcu_fw_pmctrl(dev)	((dev)->hif_ops->fw_own(dev))
149 
150 struct mt792x_hif_ops {
151 	int (*init_reset)(struct mt792x_dev *dev);
152 	int (*reset)(struct mt792x_dev *dev);
153 	int (*mcu_init)(struct mt792x_dev *dev);
154 	int (*drv_own)(struct mt792x_dev *dev);
155 	int (*fw_own)(struct mt792x_dev *dev);
156 };
157 
158 struct mt792x_dev {
159 	union { /* must be first */
160 		struct mt76_dev mt76;
161 		struct mt76_phy mphy;
162 	};
163 
164 	const struct mt76_bus_ops *bus_ops;
165 	struct mt792x_phy phy;
166 
167 	struct work_struct reset_work;
168 	bool hw_full_reset:1;
169 	bool hw_init_done:1;
170 	bool fw_assert:1;
171 	bool has_eht:1;
172 
173 	struct work_struct init_work;
174 
175 	u8 fw_debug;
176 	u8 fw_features;
177 
178 	struct mt76_connac_pm pm;
179 	struct mt76_connac_coredump coredump;
180 	const struct mt792x_hif_ops *hif_ops;
181 	const struct mt792x_irq_map *irq_map;
182 
183 	struct work_struct ipv6_ns_work;
184 	/* IPv6 addresses for WoWLAN */
185 	struct sk_buff_head ipv6_ns_list;
186 
187 	enum environment_cap country_ie_env;
188 	u32 backup_l1;
189 	u32 backup_l2;
190 };
191 
192 static inline struct mt792x_dev *
mt792x_hw_dev(struct ieee80211_hw * hw)193 mt792x_hw_dev(struct ieee80211_hw *hw)
194 {
195 	struct mt76_phy *phy = hw->priv;
196 
197 	return container_of(phy->dev, struct mt792x_dev, mt76);
198 }
199 
200 static inline struct mt792x_phy *
mt792x_hw_phy(struct ieee80211_hw * hw)201 mt792x_hw_phy(struct ieee80211_hw *hw)
202 {
203 	struct mt76_phy *phy = hw->priv;
204 
205 	return phy->priv;
206 }
207 
208 static inline void
mt792x_get_status_freq_info(struct mt76_rx_status * status,u8 chfreq)209 mt792x_get_status_freq_info(struct mt76_rx_status *status, u8 chfreq)
210 {
211 	if (chfreq > 180) {
212 		status->band = NL80211_BAND_6GHZ;
213 		chfreq = (chfreq - 181) * 4 + 1;
214 	} else if (chfreq > 14) {
215 		status->band = NL80211_BAND_5GHZ;
216 	} else {
217 		status->band = NL80211_BAND_2GHZ;
218 	}
219 	status->freq = ieee80211_channel_to_frequency(chfreq, status->band);
220 }
221 
mt792x_dma_need_reinit(struct mt792x_dev * dev)222 static inline bool mt792x_dma_need_reinit(struct mt792x_dev *dev)
223 {
224 	return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT);
225 }
226 
227 #define mt792x_mutex_acquire(dev)	\
228 	mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
229 #define mt792x_mutex_release(dev)	\
230 	mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
231 
232 void mt792x_pm_wake_work(struct work_struct *work);
233 void mt792x_pm_power_save_work(struct work_struct *work);
234 void mt792x_reset(struct mt76_dev *mdev);
235 void mt792x_update_channel(struct mt76_phy *mphy);
236 void mt792x_mac_reset_counters(struct mt792x_phy *phy);
237 void mt792x_mac_init_band(struct mt792x_dev *dev, u8 band);
238 void mt792x_mac_assoc_rssi(struct mt792x_dev *dev, struct sk_buff *skb);
239 struct mt76_wcid *mt792x_rx_get_wcid(struct mt792x_dev *dev, u16 idx,
240 				     bool unicast);
241 void mt792x_mac_update_mib_stats(struct mt792x_phy *phy);
242 void mt792x_mac_set_timeing(struct mt792x_phy *phy);
243 void mt792x_mac_work(struct work_struct *work);
244 void mt792x_remove_interface(struct ieee80211_hw *hw,
245 			     struct ieee80211_vif *vif);
246 void mt792x_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
247 	       struct sk_buff *skb);
248 int mt792x_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
249 		   unsigned int link_id, u16 queue,
250 		   const struct ieee80211_tx_queue_params *params);
251 int mt792x_get_stats(struct ieee80211_hw *hw,
252 		     struct ieee80211_low_level_stats *stats);
253 u64 mt792x_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif);
254 void mt792x_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
255 		    u64 timestamp);
256 void mt792x_tx_worker(struct mt76_worker *w);
257 void mt792x_roc_timer(struct timer_list *timer);
258 void mt792x_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
259 		  u32 queues, bool drop);
260 int mt792x_assign_vif_chanctx(struct ieee80211_hw *hw,
261 			      struct ieee80211_vif *vif,
262 			      struct ieee80211_bss_conf *link_conf,
263 			      struct ieee80211_chanctx_conf *ctx);
264 void mt792x_unassign_vif_chanctx(struct ieee80211_hw *hw,
265 				 struct ieee80211_vif *vif,
266 				 struct ieee80211_bss_conf *link_conf,
267 				 struct ieee80211_chanctx_conf *ctx);
268 void mt792x_set_wakeup(struct ieee80211_hw *hw, bool enabled);
269 void mt792x_get_et_strings(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
270 			   u32 sset, u8 *data);
271 int mt792x_get_et_sset_count(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
272 			     int sset);
273 void mt792x_get_et_stats(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
274 			 struct ethtool_stats *stats, u64 *data);
275 void mt792x_sta_statistics(struct ieee80211_hw *hw,
276 			   struct ieee80211_vif *vif,
277 			   struct ieee80211_sta *sta,
278 			   struct station_info *sinfo);
279 void mt792x_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class);
280 void mt792x_dma_cleanup(struct mt792x_dev *dev);
281 int mt792x_dma_enable(struct mt792x_dev *dev);
282 int mt792x_wpdma_reset(struct mt792x_dev *dev, bool force);
283 int mt792x_wpdma_reinit_cond(struct mt792x_dev *dev);
284 int mt792x_dma_disable(struct mt792x_dev *dev, bool force);
285 irqreturn_t mt792x_irq_handler(int irq, void *dev_instance);
286 void mt792x_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q);
287 int mt792x_poll_tx(struct napi_struct *napi, int budget);
288 int mt792x_poll_rx(struct napi_struct *napi, int budget);
289 void mt792x_irq_tasklet(unsigned long data);
290 int mt792x_wfsys_reset(struct mt792x_dev *dev);
291 int mt792x_tx_stats_show(struct seq_file *file, void *data);
292 int mt792x_queues_acq(struct seq_file *s, void *data);
293 int mt792x_queues_read(struct seq_file *s, void *data);
294 int mt792x_pm_stats(struct seq_file *s, void *data);
295 int mt792x_pm_idle_timeout_set(void *data, u64 val);
296 int mt792x_pm_idle_timeout_get(void *data, u64 *val);
297 int mt792x_init_wiphy(struct ieee80211_hw *hw);
298 struct ieee80211_ops *
299 mt792x_get_mac80211_ops(struct device *dev,
300 			const struct ieee80211_ops *mac80211_ops,
301 			void *drv_data, u8 *fw_features);
302 int mt792x_init_wcid(struct mt792x_dev *dev);
303 int mt792x_mcu_drv_pmctrl(struct mt792x_dev *dev);
304 int mt792x_mcu_fw_pmctrl(struct mt792x_dev *dev);
305 
mt792x_ram_name(struct mt792x_dev * dev)306 static inline char *mt792x_ram_name(struct mt792x_dev *dev)
307 {
308 	switch (mt76_chip(&dev->mt76)) {
309 	case 0x7922:
310 		return MT7922_FIRMWARE_WM;
311 	default:
312 		return MT7921_FIRMWARE_WM;
313 	}
314 }
315 
mt792x_patch_name(struct mt792x_dev * dev)316 static inline char *mt792x_patch_name(struct mt792x_dev *dev)
317 {
318 	switch (mt76_chip(&dev->mt76)) {
319 	case 0x7922:
320 		return MT7922_ROM_PATCH;
321 	default:
322 		return MT7921_ROM_PATCH;
323 	}
324 }
325 
326 int mt792x_load_firmware(struct mt792x_dev *dev);
327 
328 /* usb */
329 #define MT_USB_TYPE_VENDOR	(USB_TYPE_VENDOR | 0x1f)
330 #define MT_USB_TYPE_UHW_VENDOR	(USB_TYPE_VENDOR | 0x1e)
331 int mt792xu_dma_init(struct mt792x_dev *dev, bool resume);
332 int mt792xu_mcu_power_on(struct mt792x_dev *dev);
333 int mt792xu_wfsys_reset(struct mt792x_dev *dev);
334 int mt792xu_init_reset(struct mt792x_dev *dev);
335 u32 mt792xu_rr(struct mt76_dev *dev, u32 addr);
336 void mt792xu_wr(struct mt76_dev *dev, u32 addr, u32 val);
337 u32 mt792xu_rmw(struct mt76_dev *dev, u32 addr, u32 mask, u32 val);
338 void mt792xu_copy(struct mt76_dev *dev, u32 offset, const void *data, int len);
339 void mt792xu_disconnect(struct usb_interface *usb_intf);
340 
341 int __mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev);
342 int mt792xe_mcu_drv_pmctrl(struct mt792x_dev *dev);
343 int mt792xe_mcu_fw_pmctrl(struct mt792x_dev *dev);
344 
345 #ifdef CONFIG_ACPI
346 int mt792x_init_acpi_sar(struct mt792x_dev *dev);
347 int mt792x_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default);
348 u8 mt792x_acpi_get_flags(struct mt792x_phy *phy);
349 #else
mt792x_init_acpi_sar(struct mt792x_dev * dev)350 static inline int mt792x_init_acpi_sar(struct mt792x_dev *dev)
351 {
352 	return 0;
353 }
354 
mt792x_init_acpi_sar_power(struct mt792x_phy * phy,bool set_default)355 static inline int mt792x_init_acpi_sar_power(struct mt792x_phy *phy,
356 					     bool set_default)
357 {
358 	return 0;
359 }
360 
mt792x_acpi_get_flags(struct mt792x_phy * phy)361 static inline u8 mt792x_acpi_get_flags(struct mt792x_phy *phy)
362 {
363 	return 0;
364 }
365 #endif
366 
367 #endif /* __MT7925_H */
368