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 
10 #define MT7921_TX_RING_SIZE		2048
11 #define MT7921_TX_MCU_RING_SIZE		256
12 #define MT7921_TX_FWDL_RING_SIZE	128
13 
14 #define MT7921_RX_RING_SIZE		1536
15 #define MT7921_RX_MCU_RING_SIZE		512
16 
17 #define MT7921_EEPROM_SIZE		3584
18 #define MT7921_TOKEN_SIZE		8192
19 
20 #define MT7921_EEPROM_BLOCK_SIZE	16
21 
22 #define MT7921_SKU_RATE_NUM		161
23 #define MT7921_SKU_MAX_DELTA_IDX	MT7921_SKU_RATE_NUM
24 #define MT7921_SKU_TABLE_SIZE		(MT7921_SKU_RATE_NUM + 1)
25 
26 #define MT7921_SDIO_HDR_TX_BYTES	GENMASK(15, 0)
27 #define MT7921_SDIO_HDR_PKT_TYPE	GENMASK(17, 16)
28 
29 #define MCU_UNI_EVENT_ROC  0x27
30 
31 enum {
32 	UNI_ROC_ACQUIRE,
33 	UNI_ROC_ABORT,
34 	UNI_ROC_NUM
35 };
36 
37 enum mt7921_roc_req {
38 	MT7921_ROC_REQ_JOIN,
39 	MT7921_ROC_REQ_ROC,
40 	MT7921_ROC_REQ_NUM
41 };
42 
43 enum {
44 	UNI_EVENT_ROC_GRANT = 0,
45 	UNI_EVENT_ROC_TAG_NUM
46 };
47 
48 struct mt7921_realease_info {
49 	__le16 len;
50 	u8 pad_len;
51 	u8 tag;
52 } __packed;
53 
54 struct mt7921_fw_features {
55 	u8 segment;
56 	u8 data;
57 	u8 rsv[14];
58 } __packed;
59 
60 struct mt7921_roc_grant_tlv {
61 	__le16 tag;
62 	__le16 len;
63 	u8 bss_idx;
64 	u8 tokenid;
65 	u8 status;
66 	u8 primarychannel;
67 	u8 rfsco;
68 	u8 rfband;
69 	u8 channelwidth;
70 	u8 centerfreqseg1;
71 	u8 centerfreqseg2;
72 	u8 reqtype;
73 	u8 dbdcband;
74 	u8 rsv[1];
75 	__le32 max_interval;
76 } __packed;
77 
78 enum mt7921_sdio_pkt_type {
79 	MT7921_SDIO_TXD,
80 	MT7921_SDIO_DATA,
81 	MT7921_SDIO_CMD,
82 	MT7921_SDIO_FWDL,
83 };
84 
85 struct mt7921_sdio_intr {
86 	u32 isr;
87 	struct {
88 		u32 wtqcr[16];
89 	} tx;
90 	struct {
91 		u16 num[2];
92 		u16 len0[16];
93 		u16 len1[128];
94 	} rx;
95 	u32 rec_mb[2];
96 } __packed;
97 
98 #define to_rssi(field, rxv)		((FIELD_GET(field, rxv) - 220) / 2)
99 #define to_rcpi(rssi)			(2 * (rssi) + 220)
100 
101 enum mt7921_txq_id {
102 	MT7921_TXQ_BAND0,
103 	MT7921_TXQ_BAND1,
104 	MT7921_TXQ_FWDL = 16,
105 	MT7921_TXQ_MCU_WM,
106 };
107 
108 enum mt7921_rxq_id {
109 	MT7921_RXQ_BAND0 = 0,
110 	MT7921_RXQ_BAND1,
111 	MT7921_RXQ_MCU_WM = 0,
112 };
113 
114 enum {
115 	MT7921_CLC_POWER,
116 	MT7921_CLC_CHAN,
117 	MT7921_CLC_MAX_NUM,
118 };
119 
120 struct mt7921_clc_rule {
121 	u8 alpha2[2];
122 	u8 type[2];
123 	__le16 len;
124 	u8 data[];
125 } __packed;
126 
127 struct mt7921_clc {
128 	__le32 len;
129 	u8 idx;
130 	u8 ver;
131 	u8 nr_country;
132 	u8 type;
133 	u8 rsv[8];
134 	u8 data[];
135 } __packed;
136 
137 enum mt7921_eeprom_field {
138 	MT_EE_CHIP_ID =		0x000,
139 	MT_EE_VERSION =		0x002,
140 	MT_EE_MAC_ADDR =	0x004,
141 	MT_EE_WIFI_CONF =	0x07c,
142 	MT_EE_HW_TYPE =		0x55b,
143 	__MT_EE_MAX =		0x9ff
144 };
145 
146 #define MT_EE_HW_TYPE_ENCAP			BIT(0)
147 
148 enum {
149 	TXPWR_USER,
150 	TXPWR_EEPROM,
151 	TXPWR_MAC,
152 	TXPWR_MAX_NUM,
153 };
154 
155 struct mt7921_txpwr {
156 	u8 ch;
157 	u8 rsv[3];
158 	struct {
159 		u8 ch;
160 		u8 cck[4];
161 		u8 ofdm[8];
162 		u8 ht20[8];
163 		u8 ht40[9];
164 		u8 vht20[12];
165 		u8 vht40[12];
166 		u8 vht80[12];
167 		u8 vht160[12];
168 		u8 he26[12];
169 		u8 he52[12];
170 		u8 he106[12];
171 		u8 he242[12];
172 		u8 he484[12];
173 		u8 he996[12];
174 		u8 he996x2[12];
175 	} data[TXPWR_MAX_NUM];
176 };
177 
178 extern const struct ieee80211_ops mt7921_ops;
179 
180 u32 mt7921_reg_map(struct mt792x_dev *dev, u32 addr);
181 
182 int __mt7921_start(struct mt792x_phy *phy);
183 int mt7921_register_device(struct mt792x_dev *dev);
184 void mt7921_unregister_device(struct mt792x_dev *dev);
185 int mt7921_run_firmware(struct mt792x_dev *dev);
186 int mt7921_mcu_set_bss_pm(struct mt792x_dev *dev, struct ieee80211_vif *vif,
187 			  bool enable);
188 int mt7921_mcu_sta_update(struct mt792x_dev *dev, struct ieee80211_sta *sta,
189 			  struct ieee80211_vif *vif, bool enable,
190 			  enum mt76_sta_info_state state);
191 int mt7921_mcu_set_chan_info(struct mt792x_phy *phy, int cmd);
192 int mt7921_mcu_set_tx(struct mt792x_dev *dev, struct ieee80211_vif *vif);
193 int mt7921_mcu_set_eeprom(struct mt792x_dev *dev);
194 int mt7921_mcu_get_rx_rate(struct mt792x_phy *phy, struct ieee80211_vif *vif,
195 			   struct ieee80211_sta *sta, struct rate_info *rate);
196 int mt7921_mcu_fw_log_2_host(struct mt792x_dev *dev, u8 ctrl);
197 void mt7921_mcu_rx_event(struct mt792x_dev *dev, struct sk_buff *skb);
198 int mt7921_mcu_set_rxfilter(struct mt792x_dev *dev, u32 fif,
199 			    u8 bit_op, u32 bit_map);
200 
201 static inline u32
202 mt7921_reg_map_l1(struct mt792x_dev *dev, u32 addr)
203 {
204 	u32 offset = FIELD_GET(MT_HIF_REMAP_L1_OFFSET, addr);
205 	u32 base = FIELD_GET(MT_HIF_REMAP_L1_BASE, addr);
206 
207 	mt76_rmw_field(dev, MT_HIF_REMAP_L1, MT_HIF_REMAP_L1_MASK, base);
208 	/* use read to push write */
209 	mt76_rr(dev, MT_HIF_REMAP_L1);
210 
211 	return MT_HIF_REMAP_BASE_L1 + offset;
212 }
213 
214 static inline u32
215 mt7921_l1_rr(struct mt792x_dev *dev, u32 addr)
216 {
217 	return mt76_rr(dev, mt7921_reg_map_l1(dev, addr));
218 }
219 
220 static inline void
221 mt7921_l1_wr(struct mt792x_dev *dev, u32 addr, u32 val)
222 {
223 	mt76_wr(dev, mt7921_reg_map_l1(dev, addr), val);
224 }
225 
226 static inline u32
227 mt7921_l1_rmw(struct mt792x_dev *dev, u32 addr, u32 mask, u32 val)
228 {
229 	val |= mt7921_l1_rr(dev, addr) & ~mask;
230 	mt7921_l1_wr(dev, addr, val);
231 
232 	return val;
233 }
234 
235 #define mt7921_l1_set(dev, addr, val)	mt7921_l1_rmw(dev, addr, 0, val)
236 #define mt7921_l1_clear(dev, addr, val)	mt7921_l1_rmw(dev, addr, val, 0)
237 
238 static inline void
239 mt7921_skb_add_usb_sdio_hdr(struct mt792x_dev *dev, struct sk_buff *skb,
240 			    int type)
241 {
242 	u32 hdr, len;
243 
244 	len = mt76_is_usb(&dev->mt76) ? skb->len : skb->len + sizeof(hdr);
245 	hdr = FIELD_PREP(MT7921_SDIO_HDR_TX_BYTES, len) |
246 	      FIELD_PREP(MT7921_SDIO_HDR_PKT_TYPE, type);
247 
248 	put_unaligned_le32(hdr, skb_push(skb, sizeof(hdr)));
249 }
250 
251 void mt7921_stop(struct ieee80211_hw *hw);
252 int mt7921_mac_init(struct mt792x_dev *dev);
253 bool mt7921_mac_wtbl_update(struct mt792x_dev *dev, int idx, u32 mask);
254 int mt7921_mac_sta_add(struct mt76_dev *mdev, struct ieee80211_vif *vif,
255 		       struct ieee80211_sta *sta);
256 void mt7921_mac_sta_assoc(struct mt76_dev *mdev, struct ieee80211_vif *vif,
257 			  struct ieee80211_sta *sta);
258 void mt7921_mac_sta_remove(struct mt76_dev *mdev, struct ieee80211_vif *vif,
259 			   struct ieee80211_sta *sta);
260 void mt7921_mac_reset_work(struct work_struct *work);
261 int mt7921e_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
262 			   enum mt76_txq_id qid, struct mt76_wcid *wcid,
263 			   struct ieee80211_sta *sta,
264 			   struct mt76_tx_info *tx_info);
265 
266 bool mt7921_rx_check(struct mt76_dev *mdev, void *data, int len);
267 void mt7921_queue_rx_skb(struct mt76_dev *mdev, enum mt76_rxq_id q,
268 			 struct sk_buff *skb, u32 *info);
269 void mt7921_stats_work(struct work_struct *work);
270 void mt7921_set_stream_he_caps(struct mt792x_phy *phy);
271 int mt7921_init_debugfs(struct mt792x_dev *dev);
272 
273 int mt7921_mcu_set_beacon_filter(struct mt792x_dev *dev,
274 				 struct ieee80211_vif *vif,
275 				 bool enable);
276 int mt7921_mcu_uni_tx_ba(struct mt792x_dev *dev,
277 			 struct ieee80211_ampdu_params *params,
278 			 bool enable);
279 int mt7921_mcu_uni_rx_ba(struct mt792x_dev *dev,
280 			 struct ieee80211_ampdu_params *params,
281 			 bool enable);
282 void mt7921_scan_work(struct work_struct *work);
283 void mt7921_roc_work(struct work_struct *work);
284 int mt7921_mcu_uni_bss_ps(struct mt792x_dev *dev, struct ieee80211_vif *vif);
285 void mt7921_coredump_work(struct work_struct *work);
286 int mt7921_get_txpwr_info(struct mt792x_dev *dev, struct mt7921_txpwr *txpwr);
287 int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
288 			void *data, int len);
289 int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
290 			 struct netlink_callback *cb, void *data, int len);
291 int mt7921_mcu_parse_response(struct mt76_dev *mdev, int cmd,
292 			      struct sk_buff *skb, int seq);
293 
294 int mt7921e_driver_own(struct mt792x_dev *dev);
295 int mt7921e_mac_reset(struct mt792x_dev *dev);
296 int mt7921e_mcu_init(struct mt792x_dev *dev);
297 int mt7921s_wfsys_reset(struct mt792x_dev *dev);
298 int mt7921s_mac_reset(struct mt792x_dev *dev);
299 int mt7921s_init_reset(struct mt792x_dev *dev);
300 
301 int mt7921s_mcu_init(struct mt792x_dev *dev);
302 int mt7921s_mcu_drv_pmctrl(struct mt792x_dev *dev);
303 int mt7921s_mcu_fw_pmctrl(struct mt792x_dev *dev);
304 void mt7921_mac_add_txs(struct mt792x_dev *dev, void *data);
305 void mt7921_set_runtime_pm(struct mt792x_dev *dev);
306 void mt7921_mcu_set_suspend_iter(void *priv, u8 *mac,
307 				 struct ieee80211_vif *vif);
308 void mt7921_set_ipv6_ns_work(struct work_struct *work);
309 
310 int mt7921_mcu_set_sniffer(struct mt792x_dev *dev, struct ieee80211_vif *vif,
311 			   bool enable);
312 int mt7921_mcu_config_sniffer(struct mt792x_vif *vif,
313 			      struct ieee80211_chanctx_conf *ctx);
314 int mt7921_mcu_get_temperature(struct mt792x_phy *phy);
315 
316 int mt7921_usb_sdio_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 void mt7921_usb_sdio_tx_complete_skb(struct mt76_dev *mdev,
321 				     struct mt76_queue_entry *e);
322 bool mt7921_usb_sdio_tx_status_data(struct mt76_dev *mdev, u8 *update);
323 
324 /* usb */
325 int mt7921_mcu_uni_add_beacon_offload(struct mt792x_dev *dev,
326 				      struct ieee80211_hw *hw,
327 				      struct ieee80211_vif *vif,
328 				      bool enable);
329 int mt7921_set_tx_sar_pwr(struct ieee80211_hw *hw,
330 			  const struct cfg80211_sar_specs *sar);
331 
332 int mt7921_mcu_set_clc(struct mt792x_dev *dev, u8 *alpha2,
333 		       enum environment_cap env_cap);
334 int mt7921_mcu_set_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
335 		       struct ieee80211_channel *chan, int duration,
336 		       enum mt7921_roc_req type, u8 token_id);
337 int mt7921_mcu_abort_roc(struct mt792x_phy *phy, struct mt792x_vif *vif,
338 			 u8 token_id);
339 #endif
340