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