xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/mt7921.h (revision fe0195f7563314a88cf08a1e7e528d3c0266fbbe)
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 #define MT7921_MCU_INIT_RETRY_COUNT	10
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 to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
51 #define to_rcpi(rssi)			(2 * (rssi) + 220)
52 
53 struct mt7921_vif;
54 struct mt7921_sta;
55 
56 enum mt7921_txq_id {
57 	MT7921_TXQ_BAND0,
58 	MT7921_TXQ_BAND1,
59 	MT7921_TXQ_FWDL = 16,
60 	MT7921_TXQ_MCU_WM,
61 };
62 
63 enum mt7921_rxq_id {
64 	MT7921_RXQ_BAND0 = 0,
65 	MT7921_RXQ_BAND1,
66 	MT7921_RXQ_MCU_WM = 0,
67 };
68 
69 struct mt7921_sta_key_conf {
70 	s8 keyidx;
71 	u8 key[16];
72 };
73 
74 struct mt7921_sta {
75 	struct mt76_wcid wcid; /* must be first */
76 
77 	struct mt7921_vif *vif;
78 
79 	struct list_head poll_list;
80 	u32 airtime_ac[8];
81 
82 	unsigned long last_txs;
83 	unsigned long ampdu_state;
84 
85 	struct mt7921_sta_key_conf bip;
86 };
87 
88 DECLARE_EWMA(rssi, 10, 8);
89 
90 struct mt7921_vif {
91 	struct mt76_vif mt76; /* must be first */
92 
93 	struct mt7921_sta sta;
94 	struct mt7921_sta *wep_sta;
95 
96 	struct mt7921_phy *phy;
97 
98 	struct ewma_rssi rssi;
99 
100 	struct ieee80211_tx_queue_params queue_params[IEEE80211_NUM_ACS];
101 };
102 
103 struct mib_stats {
104 	u32 ack_fail_cnt;
105 	u32 fcs_err_cnt;
106 	u32 rts_cnt;
107 	u32 rts_retries_cnt;
108 	u32 ba_miss_cnt;
109 };
110 
111 struct mt7921_phy {
112 	struct mt76_phy *mt76;
113 	struct mt7921_dev *dev;
114 
115 	struct ieee80211_sband_iftype_data iftype[NUM_NL80211_BANDS][NUM_NL80211_IFTYPES];
116 
117 	u32 rxfilter;
118 	u64 omac_mask;
119 
120 	u16 noise;
121 
122 	s16 coverage_class;
123 	u8 slottime;
124 
125 	u32 rx_ampdu_ts;
126 	u32 ampdu_ref;
127 
128 	struct mib_stats mib;
129 
130 	u8 sta_work_count;
131 
132 	struct sk_buff_head scan_event_list;
133 	struct delayed_work scan_work;
134 };
135 
136 #define mt7921_init_reset(dev)		((dev)->hif_ops->init_reset(dev))
137 #define mt7921_dev_reset(dev)		((dev)->hif_ops->reset(dev))
138 #define mt7921_mcu_init(dev)		((dev)->hif_ops->mcu_init(dev))
139 #define __mt7921_mcu_drv_pmctrl(dev)	((dev)->hif_ops->drv_own(dev))
140 #define	__mt7921_mcu_fw_pmctrl(dev)	((dev)->hif_ops->fw_own(dev))
141 struct mt7921_hif_ops {
142 	int (*init_reset)(struct mt7921_dev *dev);
143 	int (*reset)(struct mt7921_dev *dev);
144 	int (*mcu_init)(struct mt7921_dev *dev);
145 	int (*drv_own)(struct mt7921_dev *dev);
146 	int (*fw_own)(struct mt7921_dev *dev);
147 };
148 
149 struct mt7921_dev {
150 	union { /* must be first */
151 		struct mt76_dev mt76;
152 		struct mt76_phy mphy;
153 	};
154 
155 	const struct mt76_bus_ops *bus_ops;
156 	struct mt7921_phy phy;
157 	struct tasklet_struct irq_tasklet;
158 
159 	struct work_struct reset_work;
160 	bool hw_full_reset:1;
161 	bool hw_init_done:1;
162 
163 	struct list_head sta_poll_list;
164 	spinlock_t sta_poll_lock;
165 
166 	u8 fw_debug;
167 
168 	struct mt76_connac_pm pm;
169 	struct mt76_connac_coredump coredump;
170 	const struct mt7921_hif_ops *hif_ops;
171 };
172 
173 enum {
174 	TXPWR_USER,
175 	TXPWR_EEPROM,
176 	TXPWR_MAC,
177 	TXPWR_MAX_NUM,
178 };
179 
180 struct mt7921_txpwr {
181 	u8 ch;
182 	u8 rsv[3];
183 	struct {
184 		u8 ch;
185 		u8 cck[4];
186 		u8 ofdm[8];
187 		u8 ht20[8];
188 		u8 ht40[9];
189 		u8 vht20[12];
190 		u8 vht40[12];
191 		u8 vht80[12];
192 		u8 vht160[12];
193 		u8 he26[12];
194 		u8 he52[12];
195 		u8 he106[12];
196 		u8 he242[12];
197 		u8 he484[12];
198 		u8 he996[12];
199 		u8 he996x2[12];
200 	} data[TXPWR_MAX_NUM];
201 };
202 
203 enum {
204 	MT_LMAC_AC00,
205 	MT_LMAC_AC01,
206 	MT_LMAC_AC02,
207 	MT_LMAC_AC03,
208 	MT_LMAC_ALTX0 = 0x10,
209 	MT_LMAC_BMC0,
210 	MT_LMAC_BCN0,
211 };
212 
213 static inline struct mt7921_phy *
214 mt7921_hw_phy(struct ieee80211_hw *hw)
215 {
216 	struct mt76_phy *phy = hw->priv;
217 
218 	return phy->priv;
219 }
220 
221 static inline struct mt7921_dev *
222 mt7921_hw_dev(struct ieee80211_hw *hw)
223 {
224 	struct mt76_phy *phy = hw->priv;
225 
226 	return container_of(phy->dev, struct mt7921_dev, mt76);
227 }
228 
229 #define mt7921_mutex_acquire(dev)	\
230 	mt76_connac_mutex_acquire(&(dev)->mt76, &(dev)->pm)
231 #define mt7921_mutex_release(dev)	\
232 	mt76_connac_mutex_release(&(dev)->mt76, &(dev)->pm)
233 
234 static inline u8 mt7921_lmac_mapping(struct mt7921_dev *dev, u8 ac)
235 {
236 	/* LMAC uses the reverse order of mac80211 AC indexes */
237 	return 3 - ac;
238 }
239 
240 extern const struct ieee80211_ops mt7921_ops;
241 extern struct pci_driver mt7921_pci_driver;
242 
243 u32 mt7921_reg_map(struct mt7921_dev *dev, u32 addr);
244 
245 int __mt7921_start(struct mt7921_phy *phy);
246 int mt7921_register_device(struct mt7921_dev *dev);
247 void mt7921_unregister_device(struct mt7921_dev *dev);
248 int mt7921_dma_init(struct mt7921_dev *dev);
249 int mt7921_wpdma_reset(struct mt7921_dev *dev, bool force);
250 int mt7921_wpdma_reinit_cond(struct mt7921_dev *dev);
251 void mt7921_dma_cleanup(struct mt7921_dev *dev);
252 int mt7921_run_firmware(struct mt7921_dev *dev);
253 int mt7921_mcu_add_key(struct mt7921_dev *dev, struct ieee80211_vif *vif,
254 		       struct mt7921_sta *msta, struct ieee80211_key_conf *key,
255 		       enum set_key_cmd cmd);
256 int mt7921_mcu_sta_update(struct mt7921_dev *dev, struct ieee80211_sta *sta,
257 			  struct ieee80211_vif *vif, bool enable,
258 			  enum mt76_sta_info_state state);
259 int mt7921_mcu_set_chan_info(struct mt7921_phy *phy, int cmd);
260 int mt7921_mcu_set_tx(struct mt7921_dev *dev, struct ieee80211_vif *vif);
261 int mt7921_mcu_set_eeprom(struct mt7921_dev *dev);
262 int mt7921_mcu_get_eeprom(struct mt7921_dev *dev, u32 offset);
263 int mt7921_mcu_get_rx_rate(struct mt7921_phy *phy, struct ieee80211_vif *vif,
264 			   struct ieee80211_sta *sta, struct rate_info *rate);
265 int mt7921_mcu_fw_log_2_host(struct mt7921_dev *dev, u8 ctrl);
266 void mt7921_mcu_rx_event(struct mt7921_dev *dev, struct sk_buff *skb);
267 void mt7921_mcu_exit(struct mt7921_dev *dev);
268 
269 static inline void mt7921_irq_enable(struct mt7921_dev *dev, u32 mask)
270 {
271 	mt76_set_irq_mask(&dev->mt76, 0, 0, mask);
272 
273 	tasklet_schedule(&dev->irq_tasklet);
274 }
275 
276 static inline u32
277 mt7921_reg_map_l1(struct mt7921_dev *dev, u32 addr)
278 {
279 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
280 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
281 
282 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
283 	/* use read to push write */
284 	mt76_rr(dev, MT_HIF_REMAP_L1);
285 
286 	return MT_HIF_REMAP_BASE_L1 + offset;
287 }
288 
289 static inline u32
290 mt7921_l1_rr(struct mt7921_dev *dev, u32 addr)
291 {
292 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
293 }
294 
295 static inline void
296 mt7921_l1_wr(struct mt7921_dev *dev, u32 addr, u32 val)
297 {
298 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
299 }
300 
301 static inline u32
302 mt7921_l1_rmw(struct mt7921_dev *dev, u32 addr, u32 mask, u32 val)
303 {
304 	val |= mt7921_l1_rr(dev, addr) & ~mask;
305 	mt7921_l1_wr(dev, addr, val);
306 
307 	return val;
308 }
309 
310 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
311 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
312 
313 static inline bool mt7921_dma_need_reinit(struct mt7921_dev *dev)
314 {
315 	return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT);
316 }
317 
318 static inline void mt7921_mcu_tx_cleanup(struct mt7921_dev *dev)
319 {
320 	mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_WM], false);
321 	mt76_queue_tx_cleanup(dev, dev->mt76.q_mcu[MT_MCUQ_WA], false);
322 }
323 
324 int mt7921_mac_init(struct mt7921_dev *dev);
325 bool mt7921_mac_wtbl_update(struct mt7921_dev *dev, int idx, u32 mask);
326 void mt7921_mac_reset_counters(struct mt7921_phy *phy);
327 void mt7921_mac_set_timing(struct mt7921_phy *phy);
328 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
329 		       struct ieee80211_sta *sta);
330 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
331 			  struct ieee80211_sta *sta);
332 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
333 			   struct ieee80211_sta *sta);
334 void mt7921_mac_work(struct work_struct *work);
335 void mt7921_mac_reset_work(struct work_struct *work);
336 void mt7921_mac_update_mib_stats(struct mt7921_phy *phy);
337 void mt7921_reset(struct mt76_dev *mdev);
338 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
339 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
340 			   struct ieee80211_sta *sta,
341 			   struct mt76_tx_info *tx_info);
342 
343 void mt7921_tx_worker(struct mt76_worker *w);
344 void mt7921e_tx_complete_skb(struct mt76_dev *mdev, struct mt76_queue_entry *e);
345 int mt7921_init_tx_queues(struct mt7921_phy *phy, int idx, int n_desc);
346 void mt7921_tx_token_put(struct mt7921_dev *dev);
347 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
348 			 struct sk_buff *skb);
349 void mt7921_sta_ps(struct mt76_dev *mdev, struct ieee80211_sta *sta, bool ps);
350 void mt7921_stats_work(struct work_struct *work);
351 void mt7921_set_stream_he_caps(struct mt7921_phy *phy);
352 void mt7921_update_channel(struct mt76_phy *mphy);
353 int mt7921_init_debugfs(struct mt7921_dev *dev);
354 
355 int mt7921_mcu_set_beacon_filter(struct mt7921_dev *dev,
356 				 struct ieee80211_vif *vif,
357 				 bool enable);
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 int mt7921_mcu_uni_bss_ps(struct mt7921_dev *dev, struct ieee80211_vif *vif);
366 int mt7921_mcu_drv_pmctrl(struct mt7921_dev *dev);
367 int mt7921_mcu_fw_pmctrl(struct mt7921_dev *dev);
368 void mt7921_pm_wake_work(struct work_struct *work);
369 void mt7921_pm_power_save_work(struct work_struct *work);
370 bool mt7921_wait_for_mcu_init(struct mt7921_dev *dev);
371 void mt7921_coredump_work(struct work_struct *work);
372 int mt7921_wfsys_reset(struct mt7921_dev *dev);
373 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr);
374 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
375 			void *data, int len);
376 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
377 			 struct netlink_callback *cb, void *data, int len);
378 void mt7921_mac_write_txwi(struct mt7921_dev *dev, __le32 *txwi,
379 			   struct sk_buff *skb, struct mt76_wcid *wcid,
380 			   struct ieee80211_key_conf *key, int pid,
381 			   bool beacon);
382 void mt7921_tx_check_aggr(struct ieee80211_sta *sta, __le32 *txwi);
383 void mt7921_mac_sta_poll(struct mt7921_dev *dev);
384 int mt7921_mcu_fill_message(struct mt76_dev *mdev, struct sk_buff *skb,
385 			    int cmd, int *wait_seq);
386 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
387 			      struct sk_buff *skb, int seq);
388 int mt7921_mcu_restart(struct mt76_dev *dev);
389 
390 void mt7921e_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
391 			  struct sk_buff *skb);
392 int mt7921e_mac_reset(struct mt7921_dev *dev);
393 int mt7921e_mcu_init(struct mt7921_dev *dev);
394 int mt7921e_mcu_drv_pmctrl(struct mt7921_dev *dev);
395 int mt7921e_mcu_fw_pmctrl(struct mt7921_dev *dev);
396 
397 #endif
398