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_MAX_WMM_SETS		4
14 #define MT7921_WTBL_SIZE		20
15 #define MT7921_WTBL_RESERVED		(MT7921_WTBL_SIZE - 1)
16 #define MT7921_WTBL_STA			(MT7921_WTBL_RESERVED - \
17 					 MT7921_MAX_INTERFACES)
18 
19 #define MT7921_PM_TIMEOUT		(HZ / 12)
20 #define MT7921_HW_SCAN_TIMEOUT		(HZ / 10)
21 #define MT7921_WATCHDOG_TIME		(HZ / 4)
22 #define MT7921_RESET_TIMEOUT		(30 * HZ)
23 
24 #define MT7921_TX_RING_SIZE		2048
25 #define MT7921_TX_MCU_RING_SIZE		256
26 #define MT7921_TX_FWDL_RING_SIZE	128
27 
28 #define MT7921_RX_RING_SIZE		1536
29 #define MT7921_RX_MCU_RING_SIZE		512
30 
31 #define MT7921_DRV_OWN_RETRY_COUNT	10
32 
33 #define MT7921_FIRMWARE_WM		"mediatek/WIFI_RAM_CODE_MT7961_1.bin"
34 #define MT7921_ROM_PATCH		"mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin"
35 
36 #define MT7921_EEPROM_SIZE		3584
37 #define MT7921_TOKEN_SIZE		8192
38 
39 #define MT7921_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
40 #define MT7921_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
41 #define MT7921_5G_RATE_DEFAULT		0x4b	/* OFDM 6M */
42 #define MT7921_2G_RATE_DEFAULT		0x0	/* CCK 1M */
43 
44 #define MT7921_SKU_RATE_NUM		161
45 #define MT7921_SKU_MAX_DELTA_IDX	MT7921_SKU_RATE_NUM
46 #define MT7921_SKU_TABLE_SIZE		(MT7921_SKU_RATE_NUM + 1)
47 
48 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
49 #define to_rcpi(rssi)			(2 * (rssi) + 220)
50 
51 struct mt7921_vif;
52 struct mt7921_sta;
53 
54 enum mt7921_txq_id {
55 	MT7921_TXQ_BAND0,
56 	MT7921_TXQ_BAND1,
57 	MT7921_TXQ_FWDL = 16,
58 	MT7921_TXQ_MCU_WM,
59 };
60 
61 enum mt7921_rxq_id {
62 	MT7921_RXQ_BAND0 = 0,
63 	MT7921_RXQ_BAND1,
64 	MT7921_RXQ_MCU_WM = 0,
65 };
66 
67 struct mt7921_sta_stats {
68 	struct rate_info prob_rate;
69 	struct rate_info tx_rate;
70 
71 	unsigned long per;
72 	unsigned long changed;
73 	unsigned long jiffies;
74 };
75 
76 struct mt7921_sta_key_conf {
77 	s8 keyidx;
78 	u8 key[16];
79 };
80 
81 struct mt7921_sta {
82 	struct mt76_wcid wcid; /* must be first */
83 
84 	struct mt7921_vif *vif;
85 
86 	struct list_head stats_list;
87 	struct list_head poll_list;
88 	u32 airtime_ac[8];
89 
90 	struct mt7921_sta_stats stats;
91 
92 	unsigned long ampdu_state;
93 
94 	struct mt7921_sta_key_conf bip;
95 };
96 
97 DECLARE_EWMA(rssi, 10, 8);
98 
99 struct mt7921_vif {
100 	struct mt76_vif mt76; /* must be first */
101 
102 	struct mt7921_sta sta;
103 	struct mt7921_phy *phy;
104 
105 	struct ewma_rssi rssi;
106 
107 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
108 };
109 
110 struct mib_stats {
111 	u32 ack_fail_cnt;
112 	u32 fcs_err_cnt;
113 	u32 rts_cnt;
114 	u32 rts_retries_cnt;
115 	u32 ba_miss_cnt;
116 };
117 
118 struct mt7921_phy {
119 	struct mt76_phy *mt76;
120 	struct mt7921_dev *dev;
121 
122 	struct ieee80211_sband_iftype_data iftype[2][NUM_NL80211_IFTYPES];
123 
124 	struct ieee80211_vif *monitor_vif;
125 
126 	u32 rxfilter;
127 	u64 omac_mask;
128 
129 	u16 noise;
130 
131 	s16 coverage_class;
132 	u8 slottime;
133 
134 	u32 rx_ampdu_ts;
135 	u32 ampdu_ref;
136 
137 	struct mib_stats mib;
138 	struct list_head stats_list;
139 
140 	u8 sta_work_count;
141 
142 	struct sk_buff_head scan_event_list;
143 	struct delayed_work scan_work;
144 };
145 
146 struct mt7921_dev {
147 	union { /* must be first */
148 		struct mt76_dev mt76;
149 		struct mt76_phy mphy;
150 	};
151 
152 	const struct mt76_bus_ops *bus_ops;
153 	struct mt7921_phy phy;
154 	struct tasklet_struct irq_tasklet;
155 
156 	u16 chainmask;
157 
158 	struct work_struct reset_work;
159 
160 	struct list_head sta_poll_list;
161 	spinlock_t sta_poll_lock;
162 
163 	u8 fw_debug;
164 
165 	struct mt76_connac_pm pm;
166 	struct mt76_connac_coredump coredump;
167 };
168 
169 enum {
170 	TXPWR_USER,
171 	TXPWR_EEPROM,
172 	TXPWR_MAC,
173 	TXPWR_MAX_NUM,
174 };
175 
176 struct mt7921_txpwr {
177 	u8 ch;
178 	u8 rsv[3];
179 	struct {
180 		u8 ch;
181 		u8 cck[4];
182 		u8 ofdm[8];
183 		u8 ht20[8];
184 		u8 ht40[9];
185 		u8 vht20[12];
186 		u8 vht40[12];
187 		u8 vht80[12];
188 		u8 vht160[12];
189 		u8 he26[12];
190 		u8 he52[12];
191 		u8 he106[12];
192 		u8 he242[12];
193 		u8 he484[12];
194 		u8 he996[12];
195 		u8 he996x2[12];
196 	} data[TXPWR_MAX_NUM];
197 };
198 
199 enum {
200 	MT_LMAC_AC00,
201 	MT_LMAC_AC01,
202 	MT_LMAC_AC02,
203 	MT_LMAC_AC03,
204 	MT_LMAC_ALTX0 = 0x10,
205 	MT_LMAC_BMC0,
206 	MT_LMAC_BCN0,
207 };
208 
209 static inline struct mt7921_phy *
210 mt7921_hw_phy(struct ieee80211_hw *hw)
211 {
212 	struct mt76_phy *phy = hw->priv;
213 
214 	return phy->priv;
215 }
216 
217 static inline struct mt7921_dev *
218 mt7921_hw_dev(struct ieee80211_hw *hw)
219 {
220 	struct mt76_phy *phy = hw->priv;
221 
222 	return container_of(phy->dev, struct mt7921_dev, mt76);
223 }
224 
225 #define mt7921_mutex_acquire(dev)	\
226 	mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
227 #define mt7921_mutex_release(dev)	\
228 	mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
229 
230 static inline u8 mt7921_lmac_mapping(struct mt7921_dev *dev, u8 ac)
231 {
232 	/* LMAC uses the reverse order of mac80211 AC indexes */
233 	return 3 - ac;
234 }
235 
236 extern const struct ieee80211_ops mt7921_ops;
237 extern struct pci_driver mt7921_pci_driver;
238 
239 u32 mt7921_reg_map(struct mt7921_dev *dev, u32 addr);
240 
241 int __mt7921_start(struct mt7921_phy *phy);
242 int mt7921_register_device(struct mt7921_dev *dev);
243 void mt7921_unregister_device(struct mt7921_dev *dev);
244 int mt7921_eeprom_init(struct mt7921_dev *dev);
245 void mt7921_eeprom_parse_band_config(struct mt7921_phy *phy);
246 int mt7921_eeprom_get_target_power(struct mt7921_dev *dev,
247 				   struct ieee80211_channel *chan,
248 				   u8 chain_idx);
249 void mt7921_eeprom_init_sku(struct mt7921_dev *dev);
250 int mt7921_dma_init(struct mt7921_dev *dev);
251 int mt7921_wpdma_reset(struct mt7921_dev *dev, bool force);
252 int mt7921_wpdma_reinit_cond(struct mt7921_dev *dev);
253 void mt7921_dma_cleanup(struct mt7921_dev *dev);
254 int mt7921_run_firmware(struct mt7921_dev *dev);
255 int mt7921_mcu_init(struct mt7921_dev *dev);
256 int mt7921_mcu_add_key(struct mt7921_dev *dev, struct ieee80211_vif *vif,
257 		       struct mt7921_sta *msta, struct ieee80211_key_conf *key,
258 		       enum set_key_cmd cmd);
259 int mt7921_set_channel(struct mt7921_phy *phy);
260 int mt7921_mcu_sta_add(struct mt7921_dev *dev, struct ieee80211_sta *sta,
261 		       struct ieee80211_vif *vif, bool enable);
262 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd);
263 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif);
264 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev);
265 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset);
266 int mt7921_mcu_get_rx_rate(struct mt7921_phy *phy, struct ieee80211_vif *vif,
267 			   struct ieee80211_sta *sta, struct rate_info *rate);
268 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl);
269 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb);
270 void mt7921_mcu_exit(struct mt7921_dev *dev);
271 
272 static inline void mt7921_irq_enable(struct mt7921_dev *dev, u32 mask)
273 {
274 	mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
275 
276 	tasklet_schedule(&dev->irq_tasklet);
277 }
278 
279 static inline u32
280 mt7921_reg_map_l1(struct mt7921_dev *dev, u32 addr)
281 {
282 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
283 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
284 
285 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
286 	/* use read to push write */
287 	mt76_rr(dev, MT_HIF_REMAP_L1);
288 
289 	return MT_HIF_REMAP_BASE_L1 + offset;
290 }
291 
292 static inline u32
293 mt7921_l1_rr(struct mt7921_dev *dev, u32 addr)
294 {
295 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
296 }
297 
298 static inline void
299 mt7921_l1_wr(struct mt7921_dev *dev, u32 addr, u32 val)
300 {
301 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
302 }
303 
304 static inline u32
305 mt7921_l1_rmw(struct mt7921_dev *dev, u32 addr, u32 mask, u32 val)
306 {
307 	val |= mt7921_l1_rr(dev, addr) & ~mask;
308 	mt7921_l1_wr(dev, addr, val);
309 
310 	return val;
311 }
312 
313 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
314 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
315 
316 static inline bool mt7921_dma_need_reinit(struct mt7921_dev *dev)
317 {
318 	return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT);
319 }
320 
321 void mt7921_mac_init(struct mt7921_dev *dev);
322 bool mt7921_mac_wtbl_update(struct mt7921_dev *dev, int idx, u32 mask);
323 void mt7921_mac_reset_counters(struct mt7921_phy *phy);
324 void mt7921_mac_write_txwi(struct mt7921_dev *dev, __le32 *txwi,
325 			   struct sk_buff *skb, struct mt76_wcid *wcid,
326 			   struct ieee80211_key_conf *key, bool beacon);
327 void mt7921_mac_set_timing(struct mt7921_phy *phy);
328 int mt7921_mac_fill_rx(struct mt7921_dev *dev, struct sk_buff *skb);
329 void mt7921_mac_fill_rx_vector(struct mt7921_dev *dev, struct sk_buff *skb);
330 void mt7921_mac_tx_free(struct mt7921_dev *dev, struct sk_buff *skb);
331 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
332 		       struct ieee80211_sta *sta);
333 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
334 			   struct ieee80211_sta *sta);
335 void mt7921_mac_work(struct work_struct *work);
336 void mt7921_mac_reset_work(struct work_struct *work);
337 void mt7921_reset(struct mt76_dev *mdev);
338 void mt7921_tx_cleanup(struct mt7921_dev *dev);
339 int mt7921_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
340 			  enum mt76_txq_id qid, struct mt76_wcid *wcid,
341 			  struct ieee80211_sta *sta,
342 			  struct mt76_tx_info *tx_info);
343 
344 void mt7921_tx_worker(struct mt76_worker *w);
345 void mt7921_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e);
346 int mt7921_init_tx_queues(struct mt7921_phy *phy, int idx, int n_desc);
347 void mt7921_tx_token_put(struct mt7921_dev *dev);
348 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
349 			 struct sk_buff *skb);
350 void mt7921_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
351 void mt7921_stats_work(struct work_struct *work);
352 void mt7921_txp_skb_unmap(struct mt76_dev *dev,
353 			  struct mt76_txwi_cache *txwi);
354 void mt7921_set_stream_he_caps(struct mt7921_phy *phy);
355 void mt7921_update_channel(struct mt76_dev *mdev);
356 int mt7921_init_debugfs(struct mt7921_dev *dev);
357 
358 int mt7921_mcu_uni_tx_ba(struct mt7921_dev *dev,
359 			 struct ieee80211_ampdu_params *params,
360 			 bool enable);
361 int mt7921_mcu_uni_rx_ba(struct mt7921_dev *dev,
362 			 struct ieee80211_ampdu_params *params,
363 			 bool enable);
364 void mt7921_scan_work(struct work_struct *work);
365 u32 mt7921_get_wtbl_info(struct mt7921_dev *dev, u32 wlan_idx);
366 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif);
367 int mt7921_mcu_uni_bss_bcnft(struct mt7921_dev *dev, struct ieee80211_vif *vif,
368 			     bool enable);
369 int mt7921_mcu_set_bss_pm(struct mt7921_dev *dev, struct ieee80211_vif *vif,
370 			  bool enable);
371 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev);
372 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev);
373 void mt7921_pm_wake_work(struct work_struct *work);
374 void mt7921_pm_power_save_work(struct work_struct *work);
375 bool mt7921_wait_for_mcu_init(struct mt7921_dev *dev);
376 int mt7921_mac_set_beacon_filter(struct mt7921_phy *phy,
377 				 struct ieee80211_vif *vif,
378 				 bool enable);
379 void mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif);
380 void mt7921_coredump_work(struct work_struct *work);
381 int mt7921_wfsys_reset(struct mt7921_dev *dev);
382 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr);
383 #endif
384