xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7921/mt7921.h (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #ifndef __MT7921_H
5 #define __MT7921_H
6 
7 #include <linux/interrupt.h>
8 #include <linux/ktime.h>
9 #include "../mt76_connac_mcu.h"
10 #include "regs.h"
11 #if !defined(__FreeBSD__) || defined(CONFIG_ACPI)
12 #include "acpi_sar.h"
13 #endif
14 
15 #define MT7921_MAX_INTERFACES		4
16 #define MT7921_WTBL_SIZE		20
17 #define MT7921_WTBL_RESERVED		(MT7921_WTBL_SIZE - 1)
18 #define MT7921_WTBL_STA			(MT7921_WTBL_RESERVED - \
19 					 MT7921_MAX_INTERFACES)
20 
21 #define MT7921_PM_TIMEOUT		(HZ / 12)
22 #define MT7921_HW_SCAN_TIMEOUT		(HZ / 10)
23 #define MT7921_WATCHDOG_TIME		(HZ / 4)
24 #define MT7921_RESET_TIMEOUT		(30 * HZ)
25 
26 #define MT7921_TX_RING_SIZE		2048
27 #define MT7921_TX_MCU_RING_SIZE		256
28 #define MT7921_TX_FWDL_RING_SIZE	128
29 
30 #define MT7921_RX_RING_SIZE		1536
31 #define MT7921_RX_MCU_RING_SIZE		512
32 
33 #define MT7921_DRV_OWN_RETRY_COUNT	10
34 #define MT7921_MCU_INIT_RETRY_COUNT	10
35 #define MT7921_WFSYS_INIT_RETRY_COUNT	2
36 
37 #define MT7921_FIRMWARE_WM		"mediatek/WIFI_RAM_CODE_MT7961_1.bin"
38 #define MT7921_ROM_PATCH		"mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin"
39 
40 #define MT7922_FIRMWARE_WM		"mediatek/WIFI_RAM_CODE_MT7922_1.bin"
41 #define MT7922_ROM_PATCH		"mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin"
42 
43 #define MT7921_EEPROM_SIZE		3584
44 #define MT7921_TOKEN_SIZE		8192
45 
46 #define MT7921_EEPROM_BLOCK_SIZE	16
47 
48 #define MT7921_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
49 #define MT7921_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
50 
51 #define MT7921_SKU_RATE_NUM		161
52 #define MT7921_SKU_MAX_DELTA_IDX	MT7921_SKU_RATE_NUM
53 #define MT7921_SKU_TABLE_SIZE		(MT7921_SKU_RATE_NUM + 1)
54 
55 #define MT7921_SDIO_HDR_TX_BYTES	GENMASK(15, 0)
56 #define MT7921_SDIO_HDR_PKT_TYPE	GENMASK(17, 16)
57 
58 enum mt7921_sdio_pkt_type {
59 	MT7921_SDIO_TXD,
60 	MT7921_SDIO_DATA,
61 	MT7921_SDIO_CMD,
62 	MT7921_SDIO_FWDL,
63 };
64 
65 struct mt7921_sdio_intr {
66 	u32 isr;
67 	struct {
68 		u32 wtqcr[16];
69 	} tx;
70 	struct {
71 		u16 num[2];
72 		u16 len0[16];
73 		u16 len1[128];
74 	} rx;
75 	u32 rec_mb[2];
76 } __packed;
77 
78 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
79 #define to_rcpi(rssi)			(2 * (rssi) + 220)
80 
81 struct mt7921_vif;
82 struct mt7921_sta;
83 
84 enum mt7921_txq_id {
85 	MT7921_TXQ_BAND0,
86 	MT7921_TXQ_BAND1,
87 	MT7921_TXQ_FWDL = 16,
88 	MT7921_TXQ_MCU_WM,
89 };
90 
91 enum mt7921_rxq_id {
92 	MT7921_RXQ_BAND0 = 0,
93 	MT7921_RXQ_BAND1,
94 	MT7921_RXQ_MCU_WM = 0,
95 };
96 
97 struct mt7921_sta {
98 	struct mt76_wcid wcid; /* must be first */
99 
100 	struct mt7921_vif *vif;
101 
102 	struct list_head poll_list;
103 	u32 airtime_ac[8];
104 
105 	unsigned long last_txs;
106 	unsigned long ampdu_state;
107 
108 	struct mt76_connac_sta_key_conf bip;
109 };
110 
111 DECLARE_EWMA(rssi, 10, 8);
112 
113 struct mt7921_vif {
114 	struct mt76_vif mt76; /* must be first */
115 
116 	struct mt7921_sta sta;
117 	struct mt7921_sta *wep_sta;
118 
119 	struct mt7921_phy *phy;
120 
121 	struct ewma_rssi rssi;
122 
123 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
124 };
125 
126 struct mib_stats {
127 	u32 ack_fail_cnt;
128 	u32 fcs_err_cnt;
129 	u32 rts_cnt;
130 	u32 rts_retries_cnt;
131 	u32 ba_miss_cnt;
132 
133 	u32 tx_bf_ibf_ppdu_cnt;
134 	u32 tx_bf_ebf_ppdu_cnt;
135 	u32 tx_bf_rx_fb_all_cnt;
136 	u32 tx_bf_rx_fb_he_cnt;
137 	u32 tx_bf_rx_fb_vht_cnt;
138 	u32 tx_bf_rx_fb_ht_cnt;
139 
140 	u32 tx_ampdu_cnt;
141 	u32 tx_mpdu_attempts_cnt;
142 	u32 tx_mpdu_success_cnt;
143 	u32 tx_pkt_ebf_cnt;
144 	u32 tx_pkt_ibf_cnt;
145 
146 	u32 rx_mpdu_cnt;
147 	u32 rx_ampdu_cnt;
148 	u32 rx_ampdu_bytes_cnt;
149 	u32 rx_ba_cnt;
150 
151 	u32 tx_amsdu[8];
152 	u32 tx_amsdu_cnt;
153 };
154 
155 enum {
156 	MT7921_CLC_POWER,
157 	MT7921_CLC_CHAN,
158 	MT7921_CLC_MAX_NUM,
159 };
160 
161 struct mt7921_clc_rule {
162 	u8 alpha2[2];
163 	u8 type[2];
164 	__le16 len;
165 	u8 data[];
166 } __packed;
167 
168 struct mt7921_clc {
169 	__le32 len;
170 	u8 idx;
171 	u8 ver;
172 	u8 nr_country;
173 	u8 type;
174 	u8 rsv[8];
175 	u8 data[];
176 };
177 
178 struct mt7921_phy {
179 	struct mt76_phy *mt76;
180 	struct mt7921_dev *dev;
181 
182 	struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES];
183 
184 	u64 omac_mask;
185 
186 	u16 noise;
187 
188 	s16 coverage_class;
189 	u8 slottime;
190 
191 	u32 rx_ampdu_ts;
192 	u32 ampdu_ref;
193 
194 	struct mib_stats mib;
195 
196 	u8 sta_work_count;
197 
198 	struct sk_buff_head scan_event_list;
199 	struct delayed_work scan_work;
200 #ifdef CONFIG_ACPI
201 	struct mt7921_acpi_sar *acpisar;
202 #endif
203 
204 	struct mt7921_clc *clc[MT7921_CLC_MAX_NUM];
205 };
206 
207 #define mt7921_init_reset(dev)		((dev)->hif_ops->init_reset(dev))
208 #define mt7921_dev_reset(dev)		((dev)->hif_ops->reset(dev))
209 #define mt7921_mcu_init(dev)		((dev)->hif_ops->mcu_init(dev))
210 #define __mt7921_mcu_drv_pmctrl(dev)	((dev)->hif_ops->drv_own(dev))
211 #define	__mt7921_mcu_fw_pmctrl(dev)	((dev)->hif_ops->fw_own(dev))
212 struct mt7921_hif_ops {
213 	int (*init_reset)(struct mt7921_dev *dev);
214 	int (*reset)(struct mt7921_dev *dev);
215 	int (*mcu_init)(struct mt7921_dev *dev);
216 	int (*drv_own)(struct mt7921_dev *dev);
217 	int (*fw_own)(struct mt7921_dev *dev);
218 };
219 
220 struct mt7921_dev {
221 	union { /* must be first */
222 		struct mt76_dev mt76;
223 		struct mt76_phy mphy;
224 	};
225 
226 	const struct mt76_bus_ops *bus_ops;
227 	struct mt7921_phy phy;
228 	struct tasklet_struct irq_tasklet;
229 
230 	struct work_struct reset_work;
231 	bool hw_full_reset:1;
232 	bool hw_init_done:1;
233 	bool fw_assert:1;
234 
235 	struct list_head sta_poll_list;
236 	spinlock_t sta_poll_lock;
237 
238 	struct work_struct init_work;
239 
240 	u8 fw_debug;
241 
242 	struct mt76_connac_pm pm;
243 	struct mt76_connac_coredump coredump;
244 	const struct mt7921_hif_ops *hif_ops;
245 
246 	struct work_struct ipv6_ns_work;
247 	/* IPv6 addresses for WoWLAN */
248 	struct sk_buff_head ipv6_ns_list;
249 };
250 
251 enum {
252 	TXPWR_USER,
253 	TXPWR_EEPROM,
254 	TXPWR_MAC,
255 	TXPWR_MAX_NUM,
256 };
257 
258 struct mt7921_txpwr {
259 	u8 ch;
260 	u8 rsv[3];
261 	struct {
262 		u8 ch;
263 		u8 cck[4];
264 		u8 ofdm[8];
265 		u8 ht20[8];
266 		u8 ht40[9];
267 		u8 vht20[12];
268 		u8 vht40[12];
269 		u8 vht80[12];
270 		u8 vht160[12];
271 		u8 he26[12];
272 		u8 he52[12];
273 		u8 he106[12];
274 		u8 he242[12];
275 		u8 he484[12];
276 		u8 he996[12];
277 		u8 he996x2[12];
278 	} data[TXPWR_MAX_NUM];
279 };
280 
281 static inline struct mt7921_phy *
282 mt7921_hw_phy(struct ieee80211_hw *hw)
283 {
284 	struct mt76_phy *phy = hw->priv;
285 
286 	return phy->priv;
287 }
288 
289 static inline struct mt7921_dev *
290 mt7921_hw_dev(struct ieee80211_hw *hw)
291 {
292 	struct mt76_phy *phy = hw->priv;
293 
294 	return container_of(phy->dev, struct mt7921_dev, mt76);
295 }
296 
297 #define mt7921_mutex_acquire(dev)	\
298 	mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
299 #define mt7921_mutex_release(dev)	\
300 	mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
301 
302 extern const struct ieee80211_ops mt7921_ops;
303 
304 u32 mt7921_reg_map(struct mt7921_dev *dev, u32 addr);
305 
306 int __mt7921_start(struct mt7921_phy *phy);
307 int mt7921_register_device(struct mt7921_dev *dev);
308 void mt7921_unregister_device(struct mt7921_dev *dev);
309 int mt7921_dma_init(struct mt7921_dev *dev);
310 int mt7921_wpdma_reset(struct mt7921_dev *dev, bool force);
311 int mt7921_wpdma_reinit_cond(struct mt7921_dev *dev);
312 void mt7921_dma_cleanup(struct mt7921_dev *dev);
313 int mt7921_run_firmware(struct mt7921_dev *dev);
314 int mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
315 			  bool enable);
316 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
317 			  struct ieee80211_vif *vif, bool enable,
318 			  enum mt76_sta_info_state state);
319 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd);
320 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif);
321 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev);
322 int mt7921_mcu_get_rx_rate(struct mt7921_phy *phy, struct ieee80211_vif *vif,
323 			   struct ieee80211_sta *sta, struct rate_info *rate);
324 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl);
325 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb);
326 
327 static inline void mt7921_irq_enable(struct mt7921_dev *dev, u32 mask)
328 {
329 	mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
330 
331 	tasklet_schedule(&dev->irq_tasklet);
332 }
333 
334 static inline u32
335 mt7921_reg_map_l1(struct mt7921_dev *dev, u32 addr)
336 {
337 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
338 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
339 
340 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
341 	/* use read to push write */
342 	mt76_rr(dev, MT_HIF_REMAP_L1);
343 
344 	return MT_HIF_REMAP_BASE_L1 + offset;
345 }
346 
347 static inline u32
348 mt7921_l1_rr(struct mt7921_dev *dev, u32 addr)
349 {
350 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
351 }
352 
353 static inline void
354 mt7921_l1_wr(struct mt7921_dev *dev, u32 addr, u32 val)
355 {
356 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
357 }
358 
359 static inline u32
360 mt7921_l1_rmw(struct mt7921_dev *dev, u32 addr, u32 mask, u32 val)
361 {
362 	val |= mt7921_l1_rr(dev, addr) & ~mask;
363 	mt7921_l1_wr(dev, addr, val);
364 
365 	return val;
366 }
367 
368 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
369 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
370 
371 static inline bool mt7921_dma_need_reinit(struct mt7921_dev *dev)
372 {
373 	return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT);
374 }
375 
376 static inline void
377 mt7921_skb_add_usb_sdio_hdr(struct mt7921_dev *dev, struct sk_buff *skb,
378 			    int type)
379 {
380 	u32 hdr, len;
381 
382 	len = mt76_is_usb(&dev->mt76) ? skb->len : skb->len + sizeof(hdr);
383 	hdr = FIELD_PREP(MT7921_SDIO_HDR_TX_BYTES, len) |
384 	      FIELD_PREP(MT7921_SDIO_HDR_PKT_TYPE, type);
385 
386 	put_unaligned_le32(hdr, skb_push(skb, sizeof(hdr)));
387 }
388 
389 void mt7921_stop(struct ieee80211_hw *hw);
390 int mt7921_mac_init(struct mt7921_dev *dev);
391 bool mt7921_mac_wtbl_update(struct mt7921_dev *dev, int idx, u32 mask);
392 void mt7921_mac_reset_counters(struct mt7921_phy *phy);
393 void mt7921_mac_set_timing(struct mt7921_phy *phy);
394 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
395 		       struct ieee80211_sta *sta);
396 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
397 			  struct ieee80211_sta *sta);
398 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
399 			   struct ieee80211_sta *sta);
400 void mt7921_mac_work(struct work_struct *work);
401 void mt7921_mac_reset_work(struct work_struct *work);
402 void mt7921_mac_update_mib_stats(struct mt7921_phy *phy);
403 void mt7921_reset(struct mt76_dev *mdev);
404 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
405 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
406 			   struct ieee80211_sta *sta,
407 			   struct mt76_tx_info *tx_info);
408 
409 void mt7921_tx_worker(struct mt76_worker *w);
410 void mt7921_tx_token_put(struct mt7921_dev *dev);
411 bool mt7921_rx_check(struct mt76_dev *mdev, void *data, int len);
412 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
413 			 struct sk_buff *skb);
414 void mt7921_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
415 void mt7921_stats_work(struct work_struct *work);
416 void mt7921_set_stream_he_caps(struct mt7921_phy *phy);
417 void mt7921_update_channel(struct mt76_phy *mphy);
418 int mt7921_init_debugfs(struct mt7921_dev *dev);
419 
420 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
421 				 struct ieee80211_vif *vif,
422 				 bool enable);
423 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev,
424 			 struct ieee80211_ampdu_params *params,
425 			 bool enable);
426 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev,
427 			 struct ieee80211_ampdu_params *params,
428 			 bool enable);
429 void mt7921_scan_work(struct work_struct *work);
430 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif);
431 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev);
432 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev);
433 void mt7921_pm_wake_work(struct work_struct *work);
434 void mt7921_pm_power_save_work(struct work_struct *work);
435 void mt7921_coredump_work(struct work_struct *work);
436 int mt7921_wfsys_reset(struct mt7921_dev *dev);
437 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr);
438 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
439 			void *data, int len);
440 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
441 			 struct netlink_callback *cb, void *data, int len);
442 void mt7921_txwi_free(struct mt7921_dev *dev, struct mt76_txwi_cache *t,
443 		      struct ieee80211_sta *sta, bool clear_status,
444 		      struct list_head *free_list);
445 void mt7921_mac_sta_poll(struct mt7921_dev *dev);
446 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
447 			      struct sk_buff *skb, int seq);
448 
449 int mt7921e_driver_own(struct mt7921_dev *dev);
450 int mt7921e_mac_reset(struct mt7921_dev *dev);
451 int mt7921e_mcu_init(struct mt7921_dev *dev);
452 int mt7921s_wfsys_reset(struct mt7921_dev *dev);
453 int mt7921s_mac_reset(struct mt7921_dev *dev);
454 int mt7921s_init_reset(struct mt7921_dev *dev);
455 int __mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev);
456 int mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev);
457 int mt7921e_mcu_fw_pmctrl(struct mt7921_dev *dev);
458 
459 int mt7921s_mcu_init(struct mt7921_dev *dev);
460 int mt7921s_mcu_drv_pmctrl(struct mt7921_dev *dev);
461 int mt7921s_mcu_fw_pmctrl(struct mt7921_dev *dev);
462 void mt7921_mac_add_txs(struct mt7921_dev *dev, void *data);
463 void mt7921_set_runtime_pm(struct mt7921_dev *dev);
464 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac,
465 				 struct ieee80211_vif *vif);
466 void mt7921_set_ipv6_ns_work(struct work_struct *work);
467 
468 int mt7921_mcu_set_sniffer(struct mt7921_dev *dev, struct ieee80211_vif *vif,
469 			   bool enable);
470 
471 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
472 				   enum mt76_txq_id qid, struct mt76_wcid *wcid,
473 				   struct ieee80211_sta *sta,
474 				   struct mt76_tx_info *tx_info);
475 void mt7921_usb_sdio_tx_complete_skb(struct mt76_dev *mdev,
476 				     struct mt76_queue_entry *e);
477 bool mt7921_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update);
478 
479 /* usb */
480 #define MT_USB_TYPE_VENDOR	(USB_TYPE_VENDOR | 0x1f)
481 #define MT_USB_TYPE_UHW_VENDOR	(USB_TYPE_VENDOR | 0x1e)
482 
483 int mt7921u_mcu_power_on(struct mt7921_dev *dev);
484 int mt7921u_wfsys_reset(struct mt7921_dev *dev);
485 int mt7921u_dma_init(struct mt7921_dev *dev, bool resume);
486 int mt7921u_init_reset(struct mt7921_dev *dev);
487 int mt7921u_mac_reset(struct mt7921_dev *dev);
488 int mt7921_mcu_uni_add_beacon_offload(struct mt7921_dev *dev,
489 				      struct ieee80211_hw *hw,
490 				      struct ieee80211_vif *vif,
491 				      bool enable);
492 #ifdef CONFIG_ACPI
493 int mt7921_init_acpi_sar(struct mt7921_dev *dev);
494 int mt7921_init_acpi_sar_power(struct mt7921_phy *phy, bool set_default);
495 #else
496 static inline int
497 mt7921_init_acpi_sar(struct mt7921_dev *dev)
498 {
499 	return 0;
500 }
501 
502 static inline int
503 mt7921_init_acpi_sar_power(struct mt7921_phy *phy, bool set_default)
504 {
505 	return 0;
506 }
507 #endif
508 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
509 			  const struct cfg80211_sar_specs *sar);
510 
511 int mt7921_mcu_set_clc(struct mt7921_dev *dev, u8 *alpha2,
512 		       enum environment_cap env_cap);
513 #endif
514