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