xref: /linux/drivers/net/wireless/mediatek/mt76/mt7921/mt7921.h (revision 20249e1a853c412f452aa6ee0beb752360e69f17)
1 /* SPDX-License-Identifier: ISC */
2 /* Copyright (C) 2020 MediaTek Inc. */
3 
4 #ifndef __MT7921_H
5 #define __MT7921_H
6 
7 #include "../mt792x.h"
8 #include "regs.h"
9 #include "acpi_sar.h"
10 
11 #define MT7921_MAX_INTERFACES		4
12 #define MT7921_WTBL_SIZE		20
13 #define MT7921_WTBL_RESERVED		(MT7921_WTBL_SIZE - 1)
14 #define MT7921_WTBL_STA			(MT7921_WTBL_RESERVED - \
15 					 MT7921_MAX_INTERFACES)
16 
17 #define MT7921_PM_TIMEOUT		(HZ / 12)
18 #define MT7921_HW_SCAN_TIMEOUT		(HZ / 10)
19 #define MT7921_WATCHDOG_TIME		(HZ / 4)
20 
21 #define MT7921_TX_RING_SIZE		2048
22 #define MT7921_TX_MCU_RING_SIZE		256
23 #define MT7921_TX_FWDL_RING_SIZE	128
24 
25 #define MT7921_RX_RING_SIZE		1536
26 #define MT7921_RX_MCU_RING_SIZE		512
27 
28 #define MT7921_DRV_OWN_RETRY_COUNT	10
29 #define MT7921_MCU_INIT_RETRY_COUNT	10
30 #define MT7921_WFSYS_INIT_RETRY_COUNT	2
31 
32 #define MT7921_FW_TAG_FEATURE		4
33 #define MT7921_FW_CAP_CNM		BIT(7)
34 
35 #define MT7921_FIRMWARE_WM		"mediatek/WIFI_RAM_CODE_MT7961_1.bin"
36 #define MT7921_ROM_PATCH		"mediatek/WIFI_MT7961_patch_mcu_1_2_hdr.bin"
37 
38 #define MT7922_FIRMWARE_WM		"mediatek/WIFI_RAM_CODE_MT7922_1.bin"
39 #define MT7922_ROM_PATCH		"mediatek/WIFI_MT7922_patch_mcu_1_1_hdr.bin"
40 
41 #define MT7921_EEPROM_SIZE		3584
42 #define MT7921_TOKEN_SIZE		8192
43 
44 #define MT7921_EEPROM_BLOCK_SIZE	16
45 
46 #define MT7921_CFEND_RATE_DEFAULT	0x49	/* OFDM 24M */
47 #define MT7921_CFEND_RATE_11B		0x03	/* 11B LP, 11M */
48 
49 #define MT7921_SKU_RATE_NUM		161
50 #define MT7921_SKU_MAX_DELTA_IDX	MT7921_SKU_RATE_NUM
51 #define MT7921_SKU_TABLE_SIZE		(MT7921_SKU_RATE_NUM + 1)
52 
53 #define MT7921_SDIO_HDR_TX_BYTES	GENMASK(15, 0)
54 #define MT7921_SDIO_HDR_PKT_TYPE	GENMASK(17, 16)
55 
56 #define MCU_UNI_EVENT_ROC  0x27
57 
58 enum {
59 	UNI_ROC_ACQUIRE,
60 	UNI_ROC_ABORT,
61 	UNI_ROC_NUM
62 };
63 
64 enum mt7921_roc_req {
65 	MT7921_ROC_REQ_JOIN,
66 	MT7921_ROC_REQ_ROC,
67 	MT7921_ROC_REQ_NUM
68 };
69 
70 enum {
71 	UNI_EVENT_ROC_GRANT = 0,
72 	UNI_EVENT_ROC_TAG_NUM
73 };
74 
75 struct mt7921_realease_info {
76 	__le16 len;
77 	u8 pad_len;
78 	u8 tag;
79 } __packed;
80 
81 struct mt7921_fw_features {
82 	u8 segment;
83 	u8 data;
84 	u8 rsv[14];
85 } __packed;
86 
87 struct mt7921_roc_grant_tlv {
88 	__le16 tag;
89 	__le16 len;
90 	u8 bss_idx;
91 	u8 tokenid;
92 	u8 status;
93 	u8 primarychannel;
94 	u8 rfsco;
95 	u8 rfband;
96 	u8 channelwidth;
97 	u8 centerfreqseg1;
98 	u8 centerfreqseg2;
99 	u8 reqtype;
100 	u8 dbdcband;
101 	u8 rsv[1];
102 	__le32 max_interval;
103 } __packed;
104 
105 enum mt7921_sdio_pkt_type {
106 	MT7921_SDIO_TXD,
107 	MT7921_SDIO_DATA,
108 	MT7921_SDIO_CMD,
109 	MT7921_SDIO_FWDL,
110 };
111 
112 struct mt7921_sdio_intr {
113 	u32 isr;
114 	struct {
115 		u32 wtqcr[16];
116 	} tx;
117 	struct {
118 		u16 num[2];
119 		u16 len0[16];
120 		u16 len1[128];
121 	} rx;
122 	u32 rec_mb[2];
123 } __packed;
124 
125 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
126 #define to_rcpi(rssi)			(2 * (rssi) + 220)
127 
128 enum mt7921_txq_id {
129 	MT7921_TXQ_BAND0,
130 	MT7921_TXQ_BAND1,
131 	MT7921_TXQ_FWDL = 16,
132 	MT7921_TXQ_MCU_WM,
133 };
134 
135 enum mt7921_rxq_id {
136 	MT7921_RXQ_BAND0 = 0,
137 	MT7921_RXQ_BAND1,
138 	MT7921_RXQ_MCU_WM = 0,
139 };
140 
141 enum {
142 	MT7921_CLC_POWER,
143 	MT7921_CLC_CHAN,
144 	MT7921_CLC_MAX_NUM,
145 };
146 
147 struct mt7921_clc_rule {
148 	u8 alpha2[2];
149 	u8 type[2];
150 	__le16 len;
151 	u8 data[];
152 } __packed;
153 
154 struct mt7921_clc {
155 	__le32 len;
156 	u8 idx;
157 	u8 ver;
158 	u8 nr_country;
159 	u8 type;
160 	u8 rsv[8];
161 	u8 data[];
162 } __packed;
163 
164 enum mt7921_eeprom_field {
165 	MT_EE_CHIP_ID =		0x000,
166 	MT_EE_VERSION =		0x002,
167 	MT_EE_MAC_ADDR =	0x004,
168 	MT_EE_WIFI_CONF =	0x07c,
169 	MT_EE_HW_TYPE =		0x55b,
170 	__MT_EE_MAX =		0x9ff
171 };
172 
173 #define MT_EE_HW_TYPE_ENCAP			BIT(0)
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 
181 enum {
182 	TXPWR_USER,
183 	TXPWR_EEPROM,
184 	TXPWR_MAC,
185 	TXPWR_MAX_NUM,
186 };
187 
188 struct mt7921_txpwr {
189 	u8 ch;
190 	u8 rsv[3];
191 	struct {
192 		u8 ch;
193 		u8 cck[4];
194 		u8 ofdm[8];
195 		u8 ht20[8];
196 		u8 ht40[9];
197 		u8 vht20[12];
198 		u8 vht40[12];
199 		u8 vht80[12];
200 		u8 vht160[12];
201 		u8 he26[12];
202 		u8 he52[12];
203 		u8 he106[12];
204 		u8 he242[12];
205 		u8 he484[12];
206 		u8 he996[12];
207 		u8 he996x2[12];
208 	} data[TXPWR_MAX_NUM];
209 };
210 
211 static inline struct mt792x_phy *
212 mt7921_hw_phy(struct ieee80211_hw *hw)
213 {
214 	struct mt76_phy *phy = hw->priv;
215 
216 	return phy->priv;
217 }
218 
219 extern const struct ieee80211_ops mt7921_ops;
220 
221 u32 mt7921_reg_map(struct mt792x_dev *dev, u32 addr);
222 
223 int __mt7921_start(struct mt792x_phy *phy);
224 int mt7921_register_device(struct mt792x_dev *dev);
225 void mt7921_unregister_device(struct mt792x_dev *dev);
226 int mt7921_dma_init(struct mt792x_dev *dev);
227 int mt7921_wpdma_reset(struct mt792x_dev *dev, bool force);
228 int mt7921_wpdma_reinit_cond(struct mt792x_dev *dev);
229 void mt7921_dma_cleanup(struct mt792x_dev *dev);
230 int mt7921_run_firmware(struct mt792x_dev *dev);
231 int mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif,
232 			  bool enable);
233 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta,
234 			  struct ieee80211_vif *vif, bool enable,
235 			  enum mt76_sta_info_state state);
236 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd);
237 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif);
238 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev);
239 int mt7921_mcu_get_rx_rate(struct mt792x_phy *phy, struct ieee80211_vif *vif,
240 			   struct ieee80211_sta *sta, struct rate_info *rate);
241 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl);
242 void mt7921_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb);
243 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
244 			    u8 bit_op, u32 bit_map);
245 
246 static inline u32
247 mt7921_reg_map_l1(struct mt792x_dev *dev, u32 addr)
248 {
249 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
250 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
251 
252 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
253 	/* use read to push write */
254 	mt76_rr(dev, MT_HIF_REMAP_L1);
255 
256 	return MT_HIF_REMAP_BASE_L1 + offset;
257 }
258 
259 static inline u32
260 mt7921_l1_rr(struct mt792x_dev *dev, u32 addr)
261 {
262 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
263 }
264 
265 static inline void
266 mt7921_l1_wr(struct mt792x_dev *dev, u32 addr, u32 val)
267 {
268 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
269 }
270 
271 static inline u32
272 mt7921_l1_rmw(struct mt792x_dev *dev, u32 addr, u32 mask, u32 val)
273 {
274 	val |= mt7921_l1_rr(dev, addr) & ~mask;
275 	mt7921_l1_wr(dev, addr, val);
276 
277 	return val;
278 }
279 
280 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
281 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
282 
283 static inline bool mt7921_dma_need_reinit(struct mt792x_dev *dev)
284 {
285 	return !mt76_get_field(dev, MT_WFDMA_DUMMY_CR, MT_WFDMA_NEED_REINIT);
286 }
287 
288 static inline void
289 mt7921_skb_add_usb_sdio_hdr(struct mt792x_dev *dev, struct sk_buff *skb,
290 			    int type)
291 {
292 	u32 hdr, len;
293 
294 	len = mt76_is_usb(&dev->mt76) ? skb->len : skb->len + sizeof(hdr);
295 	hdr = FIELD_PREP(MT7921_SDIO_HDR_TX_BYTES, len) |
296 	      FIELD_PREP(MT7921_SDIO_HDR_PKT_TYPE, type);
297 
298 	put_unaligned_le32(hdr, skb_push(skb, sizeof(hdr)));
299 }
300 
301 void mt7921_stop(struct ieee80211_hw *hw);
302 int mt7921_mac_init(struct mt792x_dev *dev);
303 bool mt7921_mac_wtbl_update(struct mt792x_dev *dev, int idx, u32 mask);
304 void mt7921_mac_reset_counters(struct mt792x_phy *phy);
305 void mt7921_mac_set_timing(struct mt792x_phy *phy);
306 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
307 		       struct ieee80211_sta *sta);
308 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
309 			  struct ieee80211_sta *sta);
310 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
311 			   struct ieee80211_sta *sta);
312 void mt7921_mac_work(struct work_struct *work);
313 void mt7921_mac_reset_work(struct work_struct *work);
314 void mt7921_mac_update_mib_stats(struct mt792x_phy *phy);
315 void mt7921_reset(struct mt76_dev *mdev);
316 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
317 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
318 			   struct ieee80211_sta *sta,
319 			   struct mt76_tx_info *tx_info);
320 
321 void mt7921_tx_worker(struct mt76_worker *w);
322 bool mt7921_rx_check(struct mt76_dev *mdev, void *data, int len);
323 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
324 			 struct sk_buff *skb, u32 *info);
325 void mt7921_stats_work(struct work_struct *work);
326 void mt7921_set_stream_he_caps(struct mt792x_phy *phy);
327 void mt7921_update_channel(struct mt76_phy *mphy);
328 int mt7921_init_debugfs(struct mt792x_dev *dev);
329 
330 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev,
331 				 struct ieee80211_vif *vif,
332 				 bool enable);
333 int mt7921_mcu_uni_tx_ba(struct mt792x_dev *dev,
334 			 struct ieee80211_ampdu_params *params,
335 			 bool enable);
336 int mt7921_mcu_uni_rx_ba(struct mt792x_dev *dev,
337 			 struct ieee80211_ampdu_params *params,
338 			 bool enable);
339 void mt7921_scan_work(struct work_struct *work);
340 void mt7921_roc_work(struct work_struct *work);
341 void mt7921_roc_timer(struct timer_list *timer);
342 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif);
343 int mt7921_mcu_drv_pmctrl(struct mt792x_dev *dev);
344 int mt7921_mcu_fw_pmctrl(struct mt792x_dev *dev);
345 void mt7921_pm_wake_work(struct work_struct *work);
346 void mt7921_pm_power_save_work(struct work_struct *work);
347 void mt7921_coredump_work(struct work_struct *work);
348 int mt7921_wfsys_reset(struct mt792x_dev *dev);
349 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr);
350 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
351 			void *data, int len);
352 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
353 			 struct netlink_callback *cb, void *data, int len);
354 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
355 			      struct sk_buff *skb, int seq);
356 
357 int mt7921e_driver_own(struct mt792x_dev *dev);
358 int mt7921e_mac_reset(struct mt792x_dev *dev);
359 int mt7921e_mcu_init(struct mt792x_dev *dev);
360 int mt7921s_wfsys_reset(struct mt792x_dev *dev);
361 int mt7921s_mac_reset(struct mt792x_dev *dev);
362 int mt7921s_init_reset(struct mt792x_dev *dev);
363 int __mt7921e_mcu_drv_pmctrl(struct mt792x_dev *dev);
364 int mt7921e_mcu_drv_pmctrl(struct mt792x_dev *dev);
365 int mt7921e_mcu_fw_pmctrl(struct mt792x_dev *dev);
366 
367 int mt7921s_mcu_init(struct mt792x_dev *dev);
368 int mt7921s_mcu_drv_pmctrl(struct mt792x_dev *dev);
369 int mt7921s_mcu_fw_pmctrl(struct mt792x_dev *dev);
370 void mt7921_mac_add_txs(struct mt792x_dev *dev, void *data);
371 void mt7921_set_runtime_pm(struct mt792x_dev *dev);
372 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac,
373 				 struct ieee80211_vif *vif);
374 void mt7921_set_ipv6_ns_work(struct work_struct *work);
375 
376 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
377 			   bool enable);
378 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif,
379 			      struct ieee80211_chanctx_conf *ctx);
380 int mt7921_mcu_get_temperature(struct mt792x_phy *phy);
381 
382 int mt7921_usb_sdio_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
383 				   enum mt76_txq_id qid, struct mt76_wcid *wcid,
384 				   struct ieee80211_sta *sta,
385 				   struct mt76_tx_info *tx_info);
386 void mt7921_usb_sdio_tx_complete_skb(struct mt76_dev *mdev,
387 				     struct mt76_queue_entry *e);
388 bool mt7921_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update);
389 
390 /* usb */
391 #define MT_USB_TYPE_VENDOR	(USB_TYPE_VENDOR | 0x1f)
392 #define MT_USB_TYPE_UHW_VENDOR	(USB_TYPE_VENDOR | 0x1e)
393 
394 int mt7921u_mcu_power_on(struct mt792x_dev *dev);
395 int mt7921u_wfsys_reset(struct mt792x_dev *dev);
396 int mt7921u_dma_init(struct mt792x_dev *dev, bool resume);
397 int mt7921u_init_reset(struct mt792x_dev *dev);
398 int mt7921u_mac_reset(struct mt792x_dev *dev);
399 int mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
400 				      struct ieee80211_hw *hw,
401 				      struct ieee80211_vif *vif,
402 				      bool enable);
403 #ifdef CONFIG_ACPI
404 int mt7921_init_acpi_sar(struct mt792x_dev *dev);
405 int mt7921_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default);
406 u8 mt7921_acpi_get_flags(struct mt792x_phy *phy);
407 #else
408 static inline int
409 mt7921_init_acpi_sar(struct mt792x_dev *dev)
410 {
411 	return 0;
412 }
413 
414 static inline int
415 mt7921_init_acpi_sar_power(struct mt792x_phy *phy, bool set_default)
416 {
417 	return 0;
418 }
419 
420 static inline u8
421 mt7921_acpi_get_flags(struct mt792x_phy *phy)
422 {
423 	return 0;
424 }
425 #endif
426 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
427 			  const struct cfg80211_sar_specs *sar);
428 
429 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
430 		       enum environment_cap env_cap);
431 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
432 		       struct ieee80211_channel *chan, int duration,
433 		       enum mt7921_roc_req type, u8 token_id);
434 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
435 			 u8 token_id);
436 struct ieee80211_ops *mt7921_get_mac80211_ops(struct device *dev,
437 					      void *drv_data, u8 *fw_features);
438 #endif
439