1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
3 
4 #include "mt7615.h"
5 #include "eeprom.h"
6 #include "mcu.h"
7 
8 enum {
9 	TM_CHANGED_TXPOWER_CTRL,
10 	TM_CHANGED_TXPOWER,
11 	TM_CHANGED_FREQ_OFFSET,
12 
13 	/* must be last */
14 	NUM_TM_CHANGED
15 };
16 
17 
18 static const u8 tm_change_map[] = {
19 	[TM_CHANGED_TXPOWER_CTRL] = MT76_TM_ATTR_TX_POWER_CONTROL,
20 	[TM_CHANGED_TXPOWER] = MT76_TM_ATTR_TX_POWER,
21 	[TM_CHANGED_FREQ_OFFSET] = MT76_TM_ATTR_FREQ_OFFSET,
22 };
23 
24 static const u32 reg_backup_list[] = {
25 	MT_WF_PHY_RFINTF3_0(0),
26 	MT_WF_PHY_RFINTF3_0(1),
27 	MT_WF_PHY_RFINTF3_0(2),
28 	MT_WF_PHY_RFINTF3_0(3),
29 	MT_ANT_SWITCH_CON(2),
30 	MT_ANT_SWITCH_CON(3),
31 	MT_ANT_SWITCH_CON(4),
32 	MT_ANT_SWITCH_CON(6),
33 	MT_ANT_SWITCH_CON(7),
34 	MT_ANT_SWITCH_CON(8),
35 };
36 
37 static const struct {
38 	u16 wf;
39 	u16 reg;
40 } rf_backup_list[] = {
41 	{ 0, 0x48 },
42 	{ 1, 0x48 },
43 	{ 2, 0x48 },
44 	{ 3, 0x48 },
45 };
46 
47 static int
48 mt7615_tm_set_tx_power(struct mt7615_phy *phy)
49 {
50 	struct mt7615_dev *dev = phy->dev;
51 	struct mt76_phy *mphy = phy->mt76;
52 	int i, ret, n_chains = hweight8(mphy->antenna_mask);
53 	struct cfg80211_chan_def *chandef = &mphy->chandef;
54 	int freq = chandef->center_freq1, len, target_chains;
55 	u8 *data, *eep = (u8 *)dev->mt76.eeprom.data;
56 	enum nl80211_band band = chandef->chan->band;
57 	struct sk_buff *skb;
58 	struct {
59 		u8 center_chan;
60 		u8 dbdc_idx;
61 		u8 band;
62 		u8 rsv;
63 	} __packed req_hdr = {
64 		.center_chan = ieee80211_frequency_to_channel(freq),
65 		.band = band,
66 		.dbdc_idx = phy != &dev->phy,
67 	};
68 	u8 *tx_power = NULL;
69 
70 	if (dev->mt76.test.state != MT76_TM_STATE_OFF)
71 		tx_power = dev->mt76.test.tx_power;
72 
73 	len = sizeof(req_hdr) + MT7615_EE_MAX - MT_EE_NIC_CONF_0;
74 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr) + len);
75 	if (!skb)
76 		return -ENOMEM;
77 
78 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
79 	data = skb_put_data(skb, eep + MT_EE_NIC_CONF_0, len);
80 
81 	target_chains = mt7615_ext_pa_enabled(dev, band) ? 1 : n_chains;
82 	for (i = 0; i < target_chains; i++) {
83 		int index;
84 
85 		ret = mt7615_eeprom_get_target_power_index(dev, chandef->chan, i);
86 		if (ret < 0)
87 			return -EINVAL;
88 
89 		index = ret - MT_EE_NIC_CONF_0;
90 		if (tx_power && tx_power[i])
91 			data[ret - MT_EE_NIC_CONF_0] = tx_power[i];
92 	}
93 
94 	return __mt76_mcu_skb_send_msg(&dev->mt76, skb,
95 				       MCU_EXT_CMD_SET_TX_POWER_CTRL, false);
96 }
97 
98 static void
99 mt7615_tm_reg_backup_restore(struct mt7615_dev *dev)
100 {
101 	u32 *b = dev->test.reg_backup;
102 	int n_regs = ARRAY_SIZE(reg_backup_list);
103 	int n_rf_regs = ARRAY_SIZE(rf_backup_list);
104 	int i;
105 
106 	if (dev->mt76.test.state == MT76_TM_STATE_OFF) {
107 		for (i = 0; i < n_regs; i++)
108 			mt76_wr(dev, reg_backup_list[i], b[i]);
109 
110 		for (i = 0; i < n_rf_regs; i++)
111 			mt7615_rf_wr(dev, rf_backup_list[i].wf,
112 				     rf_backup_list[i].reg, b[n_regs + i]);
113 		return;
114 	}
115 
116 	if (b)
117 		return;
118 
119 	b = devm_kzalloc(dev->mt76.dev, 4 * (n_regs + n_rf_regs),
120 			 GFP_KERNEL);
121 	if (!b)
122 		return;
123 
124 	dev->test.reg_backup = b;
125 	for (i = 0; i < n_regs; i++)
126 		b[i] = mt76_rr(dev, reg_backup_list[i]);
127 	for (i = 0; i < n_rf_regs; i++)
128 		b[n_regs + i] = mt7615_rf_rr(dev, rf_backup_list[i].wf,
129 					     rf_backup_list[i].reg);
130 }
131 
132 
133 static void
134 mt7615_tm_init_phy(struct mt7615_dev *dev, struct mt7615_phy *phy)
135 {
136 	unsigned int total_flags = ~0;
137 
138 	if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
139 		return;
140 
141 	mutex_unlock(&dev->mt76.mutex);
142 	mt7615_set_channel(phy);
143 	mt7615_ops.configure_filter(phy->mt76->hw, 0, &total_flags, 0);
144 	mutex_lock(&dev->mt76.mutex);
145 
146 	mt7615_tm_reg_backup_restore(dev);
147 }
148 
149 static void
150 mt7615_tm_init(struct mt7615_dev *dev)
151 {
152 	mt7615_tm_init_phy(dev, &dev->phy);
153 
154 	if (dev->mt76.phy2)
155 		mt7615_tm_init_phy(dev, dev->mt76.phy2->priv);
156 }
157 
158 static void
159 mt7615_tm_set_rx_enable(struct mt7615_dev *dev, bool en)
160 {
161 	u32 rqcr_mask = (MT_ARB_RQCR_RX_START |
162 			 MT_ARB_RQCR_RXV_START |
163 			 MT_ARB_RQCR_RXV_R_EN |
164 			 MT_ARB_RQCR_RXV_T_EN) *
165 			(BIT(0) | BIT(MT_ARB_RQCR_BAND_SHIFT));
166 
167 	if (en) {
168 		mt76_clear(dev, MT_ARB_SCR,
169 			   MT_ARB_SCR_RX0_DISABLE | MT_ARB_SCR_RX1_DISABLE);
170 		mt76_set(dev, MT_ARB_RQCR, rqcr_mask);
171 	} else {
172 		mt76_set(dev, MT_ARB_SCR,
173 			 MT_ARB_SCR_RX0_DISABLE | MT_ARB_SCR_RX1_DISABLE);
174 		mt76_clear(dev, MT_ARB_RQCR, rqcr_mask);
175 	}
176 }
177 
178 static void
179 mt7615_tm_set_tx_antenna(struct mt7615_dev *dev, bool en)
180 {
181 	struct mt76_testmode_data *td = &dev->mt76.test;
182 	u8 mask = td->tx_antenna_mask;
183 	int i;
184 
185 	if (!mask)
186 		return;
187 
188 	if (!en)
189 		mask = dev->phy.chainmask;
190 
191 	for (i = 0; i < 4; i++) {
192 		mt76_rmw_field(dev, MT_WF_PHY_RFINTF3_0(i),
193 			       MT_WF_PHY_RFINTF3_0_ANT,
194 			       td->tx_antenna_mask & BIT(i) ? 0 : 0xa);
195 
196 	}
197 
198 	/* 2.4 GHz band */
199 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(3), MT_ANT_SWITCH_CON_MODE(0),
200 		       (td->tx_antenna_mask & BIT(0)) ? 0x8 : 0x1b);
201 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(4), MT_ANT_SWITCH_CON_MODE(2),
202 		       (td->tx_antenna_mask & BIT(1)) ? 0xe : 0x1b);
203 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(6), MT_ANT_SWITCH_CON_MODE1(0),
204 		       (td->tx_antenna_mask & BIT(2)) ? 0x0 : 0xf);
205 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(7), MT_ANT_SWITCH_CON_MODE1(2),
206 		       (td->tx_antenna_mask & BIT(3)) ? 0x6 : 0xf);
207 
208 	/* 5 GHz band */
209 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(4), MT_ANT_SWITCH_CON_MODE(1),
210 		       (td->tx_antenna_mask & BIT(0)) ? 0xd : 0x1b);
211 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(2), MT_ANT_SWITCH_CON_MODE(3),
212 		       (td->tx_antenna_mask & BIT(1)) ? 0x13 : 0x1b);
213 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(7), MT_ANT_SWITCH_CON_MODE1(1),
214 		       (td->tx_antenna_mask & BIT(2)) ? 0x5 : 0xf);
215 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(8), MT_ANT_SWITCH_CON_MODE1(3),
216 		       (td->tx_antenna_mask & BIT(3)) ? 0xb : 0xf);
217 
218 	for (i = 0; i < 4; i++) {
219 		u32 val;
220 
221 		val = mt7615_rf_rr(dev, i, 0x48);
222 		val &= ~(0x3ff << 20);
223 		if (td->tx_antenna_mask & BIT(i))
224 			val |= 3 << 20;
225 		else
226 			val |= (2 << 28) | (2 << 26) | (8 << 20);
227 		mt7615_rf_wr(dev, i, 0x48, val);
228 	}
229 }
230 
231 static void
232 mt7615_tm_set_tx_frames(struct mt7615_dev *dev, bool en)
233 {
234 	struct ieee80211_tx_info *info;
235 	struct sk_buff *skb = dev->mt76.test.tx_skb;
236 
237 	mt7615_mcu_set_chan_info(&dev->phy, MCU_EXT_CMD_SET_RX_PATH);
238 	mt7615_tm_set_tx_antenna(dev, en);
239 	mt7615_tm_set_rx_enable(dev, !en);
240 	if (!en || !skb)
241 		return;
242 
243 	info = IEEE80211_SKB_CB(skb);
244 	info->control.vif = dev->phy.monitor_vif;
245 }
246 
247 static void
248 mt7615_tm_update_params(struct mt7615_dev *dev, u32 changed)
249 {
250 	struct mt76_testmode_data *td = &dev->mt76.test;
251 	bool en = dev->mt76.test.state != MT76_TM_STATE_OFF;
252 
253 	if (changed & BIT(TM_CHANGED_TXPOWER_CTRL))
254 		mt7615_mcu_set_test_param(dev, MCU_ATE_SET_TX_POWER_CONTROL,
255 					  en, en && td->tx_power_control);
256 	if (changed & BIT(TM_CHANGED_FREQ_OFFSET))
257 		mt7615_mcu_set_test_param(dev, MCU_ATE_SET_FREQ_OFFSET,
258 					  en, en ? td->freq_offset : 0);
259 	if (changed & BIT(TM_CHANGED_TXPOWER))
260 		mt7615_tm_set_tx_power(&dev->phy);
261 }
262 
263 static int
264 mt7615_tm_set_state(struct mt76_dev *mdev, enum mt76_testmode_state state)
265 {
266 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
267 	struct mt76_testmode_data *td = &mdev->test;
268 	enum mt76_testmode_state prev_state = td->state;
269 
270 	mdev->test.state = state;
271 
272 	if (prev_state == MT76_TM_STATE_TX_FRAMES)
273 		mt7615_tm_set_tx_frames(dev, false);
274 	else if (state == MT76_TM_STATE_TX_FRAMES)
275 		mt7615_tm_set_tx_frames(dev, true);
276 
277 	if (state <= MT76_TM_STATE_IDLE)
278 		mt7615_tm_init(dev);
279 
280 	if ((state == MT76_TM_STATE_IDLE &&
281 	     prev_state == MT76_TM_STATE_OFF) ||
282 	    (state == MT76_TM_STATE_OFF &&
283 	     prev_state == MT76_TM_STATE_IDLE)) {
284 		u32 changed = 0;
285 		int i;
286 
287 		for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
288 			u16 cur = tm_change_map[i];
289 
290 			if (td->param_set[cur / 32] & BIT(cur % 32))
291 				changed |= BIT(i);
292 		}
293 
294 		mt7615_tm_update_params(dev, changed);
295 	}
296 
297 	return 0;
298 }
299 
300 static int
301 mt7615_tm_set_params(struct mt76_dev *mdev, struct nlattr **tb,
302 		     enum mt76_testmode_state new_state)
303 {
304 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
305 	struct mt76_testmode_data *td = &dev->mt76.test;
306 	u32 changed = 0;
307 	int i;
308 
309 	BUILD_BUG_ON(NUM_TM_CHANGED >= 32);
310 
311 	if (new_state == MT76_TM_STATE_OFF ||
312 	    td->state == MT76_TM_STATE_OFF)
313 		return 0;
314 
315 	if (td->tx_antenna_mask & ~dev->phy.chainmask)
316 		return -EINVAL;
317 
318 	for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
319 		if (tb[tm_change_map[i]])
320 			changed |= BIT(i);
321 	}
322 
323 	mt7615_tm_update_params(dev, changed);
324 
325 	return 0;
326 }
327 
328 static int
329 mt7615_tm_dump_stats(struct mt76_dev *mdev, struct sk_buff *msg)
330 {
331 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
332 	void *rx, *rssi;
333 	int i;
334 
335 	rx = nla_nest_start(msg, MT76_TM_STATS_ATTR_LAST_RX);
336 	if (!rx)
337 		return -ENOMEM;
338 
339 	if (nla_put_s32(msg, MT76_TM_RX_ATTR_FREQ_OFFSET, dev->test.last_freq_offset) ||
340 	    nla_put_s32(msg, MT76_TM_RX_ATTR_IB_RSSI, dev->test.last_ib_rssi) ||
341 	    nla_put_s32(msg, MT76_TM_RX_ATTR_WB_RSSI, dev->test.last_wb_rssi))
342 		return -ENOMEM;
343 
344 	rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_RCPI);
345 	if (!rssi)
346 		return -ENOMEM;
347 
348 	for (i = 0; i < ARRAY_SIZE(dev->test.last_rcpi); i++)
349 		if (nla_put_u8(msg, i, dev->test.last_rcpi[i]))
350 			return -ENOMEM;
351 
352 	nla_nest_end(msg, rssi);
353 
354 	nla_nest_end(msg, rx);
355 
356 	return 0;
357 }
358 
359 const struct mt76_testmode_ops mt7615_testmode_ops = {
360 	.set_state = mt7615_tm_set_state,
361 	.set_params = mt7615_tm_set_params,
362 	.dump_stats = mt7615_tm_dump_stats,
363 };
364