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