xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt7615/testmode.c (revision 8ba4d145d351db26e07695b8e90697398c5dfec2)
16c92544dSBjoern A. Zeeb // SPDX-License-Identifier: ISC
26c92544dSBjoern A. Zeeb /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
36c92544dSBjoern A. Zeeb 
46c92544dSBjoern A. Zeeb #include "mt7615.h"
56c92544dSBjoern A. Zeeb #include "eeprom.h"
66c92544dSBjoern A. Zeeb #include "mcu.h"
76c92544dSBjoern A. Zeeb 
86c92544dSBjoern A. Zeeb enum {
96c92544dSBjoern A. Zeeb 	TM_CHANGED_TXPOWER_CTRL,
106c92544dSBjoern A. Zeeb 	TM_CHANGED_TXPOWER,
116c92544dSBjoern A. Zeeb 	TM_CHANGED_FREQ_OFFSET,
126c92544dSBjoern A. Zeeb 
136c92544dSBjoern A. Zeeb 	/* must be last */
146c92544dSBjoern A. Zeeb 	NUM_TM_CHANGED
156c92544dSBjoern A. Zeeb };
166c92544dSBjoern A. Zeeb 
176c92544dSBjoern A. Zeeb 
186c92544dSBjoern A. Zeeb static const u8 tm_change_map[] = {
196c92544dSBjoern A. Zeeb 	[TM_CHANGED_TXPOWER_CTRL] = MT76_TM_ATTR_TX_POWER_CONTROL,
206c92544dSBjoern A. Zeeb 	[TM_CHANGED_TXPOWER] = MT76_TM_ATTR_TX_POWER,
216c92544dSBjoern A. Zeeb 	[TM_CHANGED_FREQ_OFFSET] = MT76_TM_ATTR_FREQ_OFFSET,
226c92544dSBjoern A. Zeeb };
236c92544dSBjoern A. Zeeb 
246c92544dSBjoern A. Zeeb static const u32 reg_backup_list[] = {
256c92544dSBjoern A. Zeeb 	MT_WF_PHY_RFINTF3_0(0),
266c92544dSBjoern A. Zeeb 	MT_WF_PHY_RFINTF3_0(1),
276c92544dSBjoern A. Zeeb 	MT_WF_PHY_RFINTF3_0(2),
286c92544dSBjoern A. Zeeb 	MT_WF_PHY_RFINTF3_0(3),
296c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(2),
306c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(3),
316c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(4),
326c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(6),
336c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(7),
346c92544dSBjoern A. Zeeb 	MT_ANT_SWITCH_CON(8),
356c92544dSBjoern A. Zeeb };
366c92544dSBjoern A. Zeeb 
376c92544dSBjoern A. Zeeb static const struct {
386c92544dSBjoern A. Zeeb 	u16 wf;
396c92544dSBjoern A. Zeeb 	u16 reg;
406c92544dSBjoern A. Zeeb } rf_backup_list[] = {
416c92544dSBjoern A. Zeeb 	{ 0, 0x48 },
426c92544dSBjoern A. Zeeb 	{ 1, 0x48 },
436c92544dSBjoern A. Zeeb 	{ 2, 0x48 },
446c92544dSBjoern A. Zeeb 	{ 3, 0x48 },
456c92544dSBjoern A. Zeeb };
466c92544dSBjoern A. Zeeb 
476c92544dSBjoern A. Zeeb static int
mt7615_tm_set_tx_power(struct mt7615_phy * phy)486c92544dSBjoern A. Zeeb mt7615_tm_set_tx_power(struct mt7615_phy *phy)
496c92544dSBjoern A. Zeeb {
506c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
516c92544dSBjoern A. Zeeb 	struct mt76_phy *mphy = phy->mt76;
526c92544dSBjoern A. Zeeb 	int i, ret, n_chains = hweight8(mphy->antenna_mask);
536c92544dSBjoern A. Zeeb 	struct cfg80211_chan_def *chandef = &mphy->chandef;
546c92544dSBjoern A. Zeeb 	int freq = chandef->center_freq1, len, target_chains;
556c92544dSBjoern A. Zeeb 	u8 *data, *eep = (u8 *)dev->mt76.eeprom.data;
566c92544dSBjoern A. Zeeb 	enum nl80211_band band = chandef->chan->band;
576c92544dSBjoern A. Zeeb 	struct sk_buff *skb;
586c92544dSBjoern A. Zeeb 	struct {
596c92544dSBjoern A. Zeeb 		u8 center_chan;
606c92544dSBjoern A. Zeeb 		u8 dbdc_idx;
616c92544dSBjoern A. Zeeb 		u8 band;
626c92544dSBjoern A. Zeeb 		u8 rsv;
636c92544dSBjoern A. Zeeb 	} __packed req_hdr = {
646c92544dSBjoern A. Zeeb 		.center_chan = ieee80211_frequency_to_channel(freq),
656c92544dSBjoern A. Zeeb 		.band = band,
666c92544dSBjoern A. Zeeb 		.dbdc_idx = phy != &dev->phy,
676c92544dSBjoern A. Zeeb 	};
686c92544dSBjoern A. Zeeb 	u8 *tx_power = NULL;
696c92544dSBjoern A. Zeeb 
706c92544dSBjoern A. Zeeb 	if (mphy->test.state != MT76_TM_STATE_OFF)
716c92544dSBjoern A. Zeeb 		tx_power = mphy->test.tx_power;
726c92544dSBjoern A. Zeeb 
736c92544dSBjoern A. Zeeb 	len = MT7615_EE_MAX - MT_EE_NIC_CONF_0;
746c92544dSBjoern A. Zeeb 	skb = mt76_mcu_msg_alloc(&dev->mt76, NULL, sizeof(req_hdr) + len);
756c92544dSBjoern A. Zeeb 	if (!skb)
766c92544dSBjoern A. Zeeb 		return -ENOMEM;
776c92544dSBjoern A. Zeeb 
786c92544dSBjoern A. Zeeb 	skb_put_data(skb, &req_hdr, sizeof(req_hdr));
796c92544dSBjoern A. Zeeb 	data = skb_put_data(skb, eep + MT_EE_NIC_CONF_0, len);
806c92544dSBjoern A. Zeeb 
816c92544dSBjoern A. Zeeb 	target_chains = mt7615_ext_pa_enabled(dev, band) ? 1 : n_chains;
826c92544dSBjoern A. Zeeb 	for (i = 0; i < target_chains; i++) {
836c92544dSBjoern A. Zeeb 		ret = mt7615_eeprom_get_target_power_index(dev, chandef->chan, i);
846c92544dSBjoern A. Zeeb 		if (ret < 0) {
856c92544dSBjoern A. Zeeb 			dev_kfree_skb(skb);
866c92544dSBjoern A. Zeeb 			return -EINVAL;
876c92544dSBjoern A. Zeeb 		}
886c92544dSBjoern A. Zeeb 
896c92544dSBjoern A. Zeeb 		if (tx_power && tx_power[i])
906c92544dSBjoern A. Zeeb 			data[ret - MT_EE_NIC_CONF_0] = tx_power[i];
916c92544dSBjoern A. Zeeb 	}
926c92544dSBjoern A. Zeeb 
936c92544dSBjoern A. Zeeb 	return mt76_mcu_skb_send_msg(&dev->mt76, skb,
946c92544dSBjoern A. Zeeb 				     MCU_EXT_CMD(SET_TX_POWER_CTRL), false);
956c92544dSBjoern A. Zeeb }
966c92544dSBjoern A. Zeeb 
976c92544dSBjoern A. Zeeb static void
mt7615_tm_reg_backup_restore(struct mt7615_phy * phy)986c92544dSBjoern A. Zeeb mt7615_tm_reg_backup_restore(struct mt7615_phy *phy)
996c92544dSBjoern A. Zeeb {
1006c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
1016c92544dSBjoern A. Zeeb 	u32 *b = phy->test.reg_backup;
1026c92544dSBjoern A. Zeeb 	int n_regs = ARRAY_SIZE(reg_backup_list);
1036c92544dSBjoern A. Zeeb 	int n_rf_regs = ARRAY_SIZE(rf_backup_list);
1046c92544dSBjoern A. Zeeb 	int i;
1056c92544dSBjoern A. Zeeb 
1066c92544dSBjoern A. Zeeb 	if (phy->mt76->test.state == MT76_TM_STATE_OFF) {
1076c92544dSBjoern A. Zeeb 		for (i = 0; i < n_regs; i++)
1086c92544dSBjoern A. Zeeb 			mt76_wr(dev, reg_backup_list[i], b[i]);
1096c92544dSBjoern A. Zeeb 
1106c92544dSBjoern A. Zeeb 		for (i = 0; i < n_rf_regs; i++)
1116c92544dSBjoern A. Zeeb 			mt7615_rf_wr(dev, rf_backup_list[i].wf,
1126c92544dSBjoern A. Zeeb 				     rf_backup_list[i].reg, b[n_regs + i]);
1136c92544dSBjoern A. Zeeb 		return;
1146c92544dSBjoern A. Zeeb 	}
1156c92544dSBjoern A. Zeeb 
1166c92544dSBjoern A. Zeeb 	if (b)
1176c92544dSBjoern A. Zeeb 		return;
1186c92544dSBjoern A. Zeeb 
1196c92544dSBjoern A. Zeeb 	b = devm_kzalloc(dev->mt76.dev, 4 * (n_regs + n_rf_regs),
1206c92544dSBjoern A. Zeeb 			 GFP_KERNEL);
1216c92544dSBjoern A. Zeeb 	if (!b)
1226c92544dSBjoern A. Zeeb 		return;
1236c92544dSBjoern A. Zeeb 
1246c92544dSBjoern A. Zeeb 	phy->test.reg_backup = b;
1256c92544dSBjoern A. Zeeb 	for (i = 0; i < n_regs; i++)
1266c92544dSBjoern A. Zeeb 		b[i] = mt76_rr(dev, reg_backup_list[i]);
1276c92544dSBjoern A. Zeeb 	for (i = 0; i < n_rf_regs; i++)
1286c92544dSBjoern A. Zeeb 		b[n_regs + i] = mt7615_rf_rr(dev, rf_backup_list[i].wf,
1296c92544dSBjoern A. Zeeb 					     rf_backup_list[i].reg);
1306c92544dSBjoern A. Zeeb }
1316c92544dSBjoern A. Zeeb 
1326c92544dSBjoern A. Zeeb static void
mt7615_tm_init(struct mt7615_phy * phy)1336c92544dSBjoern A. Zeeb mt7615_tm_init(struct mt7615_phy *phy)
1346c92544dSBjoern A. Zeeb {
1356c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
1366c92544dSBjoern A. Zeeb 	unsigned int total_flags = ~0;
1376c92544dSBjoern A. Zeeb 
1386c92544dSBjoern A. Zeeb 	if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
1396c92544dSBjoern A. Zeeb 		return;
1406c92544dSBjoern A. Zeeb 
1416c92544dSBjoern A. Zeeb 	mt7615_mcu_set_sku_en(phy, phy->mt76->test.state == MT76_TM_STATE_OFF);
1426c92544dSBjoern A. Zeeb 
1436c92544dSBjoern A. Zeeb 	mutex_unlock(&dev->mt76.mutex);
144*8ba4d145SBjoern A. Zeeb 	mt76_update_channel(phy->mt76);
1456c92544dSBjoern A. Zeeb 	mt7615_ops.configure_filter(phy->mt76->hw, 0, &total_flags, 0);
1466c92544dSBjoern A. Zeeb 	mutex_lock(&dev->mt76.mutex);
1476c92544dSBjoern A. Zeeb 
1486c92544dSBjoern A. Zeeb 	mt7615_tm_reg_backup_restore(phy);
1496c92544dSBjoern A. Zeeb }
1506c92544dSBjoern A. Zeeb 
1516c92544dSBjoern A. Zeeb static void
mt7615_tm_set_rx_enable(struct mt7615_dev * dev,bool en)1526c92544dSBjoern A. Zeeb mt7615_tm_set_rx_enable(struct mt7615_dev *dev, bool en)
1536c92544dSBjoern A. Zeeb {
1546c92544dSBjoern A. Zeeb 	u32 rqcr_mask = (MT_ARB_RQCR_RX_START |
1556c92544dSBjoern A. Zeeb 			 MT_ARB_RQCR_RXV_START |
1566c92544dSBjoern A. Zeeb 			 MT_ARB_RQCR_RXV_R_EN |
1576c92544dSBjoern A. Zeeb 			 MT_ARB_RQCR_RXV_T_EN) *
1586c92544dSBjoern A. Zeeb 			(BIT(0) | BIT(MT_ARB_RQCR_BAND_SHIFT));
1596c92544dSBjoern A. Zeeb 
1606c92544dSBjoern A. Zeeb 	if (en) {
1616c92544dSBjoern A. Zeeb 		mt76_clear(dev, MT_ARB_SCR,
1626c92544dSBjoern A. Zeeb 			   MT_ARB_SCR_RX0_DISABLE | MT_ARB_SCR_RX1_DISABLE);
1636c92544dSBjoern A. Zeeb 		mt76_set(dev, MT_ARB_RQCR, rqcr_mask);
1646c92544dSBjoern A. Zeeb 	} else {
1656c92544dSBjoern A. Zeeb 		mt76_set(dev, MT_ARB_SCR,
1666c92544dSBjoern A. Zeeb 			 MT_ARB_SCR_RX0_DISABLE | MT_ARB_SCR_RX1_DISABLE);
1676c92544dSBjoern A. Zeeb 		mt76_clear(dev, MT_ARB_RQCR, rqcr_mask);
1686c92544dSBjoern A. Zeeb 	}
1696c92544dSBjoern A. Zeeb }
1706c92544dSBjoern A. Zeeb 
1716c92544dSBjoern A. Zeeb static void
mt7615_tm_set_tx_antenna(struct mt7615_phy * phy,bool en)1726c92544dSBjoern A. Zeeb mt7615_tm_set_tx_antenna(struct mt7615_phy *phy, bool en)
1736c92544dSBjoern A. Zeeb {
1746c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
1756c92544dSBjoern A. Zeeb 	struct mt76_testmode_data *td = &phy->mt76->test;
1766c92544dSBjoern A. Zeeb 	u8 mask = td->tx_antenna_mask;
1776c92544dSBjoern A. Zeeb 	int i;
1786c92544dSBjoern A. Zeeb 
1796c92544dSBjoern A. Zeeb 	if (!mask)
1806c92544dSBjoern A. Zeeb 		return;
1816c92544dSBjoern A. Zeeb 
1826c92544dSBjoern A. Zeeb 	if (!en)
1836c92544dSBjoern A. Zeeb 		mask = phy->mt76->chainmask;
1846c92544dSBjoern A. Zeeb 
1856c92544dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
1866c92544dSBjoern A. Zeeb 		mt76_rmw_field(dev, MT_WF_PHY_RFINTF3_0(i),
1876c92544dSBjoern A. Zeeb 			       MT_WF_PHY_RFINTF3_0_ANT,
1886c92544dSBjoern A. Zeeb 			       (mask & BIT(i)) ? 0 : 0xa);
1896c92544dSBjoern A. Zeeb 	}
1906c92544dSBjoern A. Zeeb 
1916c92544dSBjoern A. Zeeb 	/* 2.4 GHz band */
1926c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(3), MT_ANT_SWITCH_CON_MODE(0),
1936c92544dSBjoern A. Zeeb 		       (mask & BIT(0)) ? 0x8 : 0x1b);
1946c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(4), MT_ANT_SWITCH_CON_MODE(2),
1956c92544dSBjoern A. Zeeb 		       (mask & BIT(1)) ? 0xe : 0x1b);
1966c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(6), MT_ANT_SWITCH_CON_MODE1(0),
1976c92544dSBjoern A. Zeeb 		       (mask & BIT(2)) ? 0x0 : 0xf);
1986c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(7), MT_ANT_SWITCH_CON_MODE1(2),
1996c92544dSBjoern A. Zeeb 		       (mask & BIT(3)) ? 0x6 : 0xf);
2006c92544dSBjoern A. Zeeb 
2016c92544dSBjoern A. Zeeb 	/* 5 GHz band */
2026c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(4), MT_ANT_SWITCH_CON_MODE(1),
2036c92544dSBjoern A. Zeeb 		       (mask & BIT(0)) ? 0xd : 0x1b);
2046c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(2), MT_ANT_SWITCH_CON_MODE(3),
2056c92544dSBjoern A. Zeeb 		       (mask & BIT(1)) ? 0x13 : 0x1b);
2066c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(7), MT_ANT_SWITCH_CON_MODE1(1),
2076c92544dSBjoern A. Zeeb 		       (mask & BIT(2)) ? 0x5 : 0xf);
2086c92544dSBjoern A. Zeeb 	mt76_rmw_field(dev, MT_ANT_SWITCH_CON(8), MT_ANT_SWITCH_CON_MODE1(3),
2096c92544dSBjoern A. Zeeb 		       (mask & BIT(3)) ? 0xb : 0xf);
2106c92544dSBjoern A. Zeeb 
2116c92544dSBjoern A. Zeeb 	for (i = 0; i < 4; i++) {
2126c92544dSBjoern A. Zeeb 		u32 val;
2136c92544dSBjoern A. Zeeb 
2146c92544dSBjoern A. Zeeb 		val = mt7615_rf_rr(dev, i, 0x48);
2156c92544dSBjoern A. Zeeb 		val &= ~(0x3ff << 20);
2166c92544dSBjoern A. Zeeb 		if (mask & BIT(i))
2176c92544dSBjoern A. Zeeb 			val |= 3 << 20;
2186c92544dSBjoern A. Zeeb 		else
2196c92544dSBjoern A. Zeeb 			val |= (2 << 28) | (2 << 26) | (8 << 20);
2206c92544dSBjoern A. Zeeb 		mt7615_rf_wr(dev, i, 0x48, val);
2216c92544dSBjoern A. Zeeb 	}
2226c92544dSBjoern A. Zeeb }
2236c92544dSBjoern A. Zeeb 
2246c92544dSBjoern A. Zeeb static void
mt7615_tm_set_tx_frames(struct mt7615_phy * phy,bool en)2256c92544dSBjoern A. Zeeb mt7615_tm_set_tx_frames(struct mt7615_phy *phy, bool en)
2266c92544dSBjoern A. Zeeb {
2276c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
2286c92544dSBjoern A. Zeeb 	struct ieee80211_tx_info *info;
2296c92544dSBjoern A. Zeeb 	struct sk_buff *skb = phy->mt76->test.tx_skb;
2306c92544dSBjoern A. Zeeb 
2316c92544dSBjoern A. Zeeb 	mt7615_mcu_set_chan_info(phy, MCU_EXT_CMD(SET_RX_PATH));
2326c92544dSBjoern A. Zeeb 	mt7615_tm_set_tx_antenna(phy, en);
2336c92544dSBjoern A. Zeeb 	mt7615_tm_set_rx_enable(dev, !en);
2346c92544dSBjoern A. Zeeb 	if (!en || !skb)
2356c92544dSBjoern A. Zeeb 		return;
2366c92544dSBjoern A. Zeeb 
2376c92544dSBjoern A. Zeeb 	info = IEEE80211_SKB_CB(skb);
2386c92544dSBjoern A. Zeeb 	info->control.vif = phy->monitor_vif;
2396c92544dSBjoern A. Zeeb }
2406c92544dSBjoern A. Zeeb 
2416c92544dSBjoern A. Zeeb static void
mt7615_tm_update_params(struct mt7615_phy * phy,u32 changed)2426c92544dSBjoern A. Zeeb mt7615_tm_update_params(struct mt7615_phy *phy, u32 changed)
2436c92544dSBjoern A. Zeeb {
2446c92544dSBjoern A. Zeeb 	struct mt7615_dev *dev = phy->dev;
2456c92544dSBjoern A. Zeeb 	struct mt76_testmode_data *td = &phy->mt76->test;
2466c92544dSBjoern A. Zeeb 	bool en = phy->mt76->test.state != MT76_TM_STATE_OFF;
2476c92544dSBjoern A. Zeeb 
2486c92544dSBjoern A. Zeeb 	if (changed & BIT(TM_CHANGED_TXPOWER_CTRL))
2496c92544dSBjoern A. Zeeb 		mt7615_mcu_set_test_param(dev, MCU_ATE_SET_TX_POWER_CONTROL,
2506c92544dSBjoern A. Zeeb 					  en, en && td->tx_power_control);
2516c92544dSBjoern A. Zeeb 	if (changed & BIT(TM_CHANGED_FREQ_OFFSET))
2526c92544dSBjoern A. Zeeb 		mt7615_mcu_set_test_param(dev, MCU_ATE_SET_FREQ_OFFSET,
2536c92544dSBjoern A. Zeeb 					  en, en ? td->freq_offset : 0);
2546c92544dSBjoern A. Zeeb 	if (changed & BIT(TM_CHANGED_TXPOWER))
2556c92544dSBjoern A. Zeeb 		mt7615_tm_set_tx_power(phy);
2566c92544dSBjoern A. Zeeb }
2576c92544dSBjoern A. Zeeb 
2586c92544dSBjoern A. Zeeb static int
mt7615_tm_set_state(struct mt76_phy * mphy,enum mt76_testmode_state state)2596c92544dSBjoern A. Zeeb mt7615_tm_set_state(struct mt76_phy *mphy, enum mt76_testmode_state state)
2606c92544dSBjoern A. Zeeb {
2616c92544dSBjoern A. Zeeb 	struct mt7615_phy *phy = mphy->priv;
2626c92544dSBjoern A. Zeeb 	struct mt76_testmode_data *td = &mphy->test;
2636c92544dSBjoern A. Zeeb 	enum mt76_testmode_state prev_state = td->state;
2646c92544dSBjoern A. Zeeb 
2656c92544dSBjoern A. Zeeb 	mphy->test.state = state;
2666c92544dSBjoern A. Zeeb 
2676c92544dSBjoern A. Zeeb 	if (prev_state == MT76_TM_STATE_TX_FRAMES)
2686c92544dSBjoern A. Zeeb 		mt7615_tm_set_tx_frames(phy, false);
2696c92544dSBjoern A. Zeeb 	else if (state == MT76_TM_STATE_TX_FRAMES)
2706c92544dSBjoern A. Zeeb 		mt7615_tm_set_tx_frames(phy, true);
2716c92544dSBjoern A. Zeeb 
2726c92544dSBjoern A. Zeeb 	if (state <= MT76_TM_STATE_IDLE)
2736c92544dSBjoern A. Zeeb 		mt7615_tm_init(phy);
2746c92544dSBjoern A. Zeeb 
2756c92544dSBjoern A. Zeeb 	if ((state == MT76_TM_STATE_IDLE &&
2766c92544dSBjoern A. Zeeb 	     prev_state == MT76_TM_STATE_OFF) ||
2776c92544dSBjoern A. Zeeb 	    (state == MT76_TM_STATE_OFF &&
2786c92544dSBjoern A. Zeeb 	     prev_state == MT76_TM_STATE_IDLE)) {
2796c92544dSBjoern A. Zeeb 		u32 changed = 0;
2806c92544dSBjoern A. Zeeb 		int i;
2816c92544dSBjoern A. Zeeb 
2826c92544dSBjoern A. Zeeb 		for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
2836c92544dSBjoern A. Zeeb 			u16 cur = tm_change_map[i];
2846c92544dSBjoern A. Zeeb 
2856c92544dSBjoern A. Zeeb 			if (td->param_set[cur / 32] & BIT(cur % 32))
2866c92544dSBjoern A. Zeeb 				changed |= BIT(i);
2876c92544dSBjoern A. Zeeb 		}
2886c92544dSBjoern A. Zeeb 
2896c92544dSBjoern A. Zeeb 		mt7615_tm_update_params(phy, changed);
2906c92544dSBjoern A. Zeeb 	}
2916c92544dSBjoern A. Zeeb 
2926c92544dSBjoern A. Zeeb 	return 0;
2936c92544dSBjoern A. Zeeb }
2946c92544dSBjoern A. Zeeb 
2956c92544dSBjoern A. Zeeb static int
mt7615_tm_set_params(struct mt76_phy * mphy,struct nlattr ** tb,enum mt76_testmode_state new_state)2966c92544dSBjoern A. Zeeb mt7615_tm_set_params(struct mt76_phy *mphy, struct nlattr **tb,
2976c92544dSBjoern A. Zeeb 		     enum mt76_testmode_state new_state)
2986c92544dSBjoern A. Zeeb {
2996c92544dSBjoern A. Zeeb 	struct mt76_testmode_data *td = &mphy->test;
3006c92544dSBjoern A. Zeeb 	struct mt7615_phy *phy = mphy->priv;
3016c92544dSBjoern A. Zeeb 	u32 changed = 0;
3026c92544dSBjoern A. Zeeb 	int i;
3036c92544dSBjoern A. Zeeb 
3046c92544dSBjoern A. Zeeb 	BUILD_BUG_ON(NUM_TM_CHANGED >= 32);
3056c92544dSBjoern A. Zeeb 
3066c92544dSBjoern A. Zeeb 	if (new_state == MT76_TM_STATE_OFF ||
3076c92544dSBjoern A. Zeeb 	    td->state == MT76_TM_STATE_OFF)
3086c92544dSBjoern A. Zeeb 		return 0;
3096c92544dSBjoern A. Zeeb 
3106c92544dSBjoern A. Zeeb 	if (td->tx_antenna_mask & ~mphy->chainmask)
3116c92544dSBjoern A. Zeeb 		return -EINVAL;
3126c92544dSBjoern A. Zeeb 
3136c92544dSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(tm_change_map); i++) {
3146c92544dSBjoern A. Zeeb 		if (tb[tm_change_map[i]])
3156c92544dSBjoern A. Zeeb 			changed |= BIT(i);
3166c92544dSBjoern A. Zeeb 	}
3176c92544dSBjoern A. Zeeb 
3186c92544dSBjoern A. Zeeb 	mt7615_tm_update_params(phy, changed);
3196c92544dSBjoern A. Zeeb 
3206c92544dSBjoern A. Zeeb 	return 0;
3216c92544dSBjoern A. Zeeb }
3226c92544dSBjoern A. Zeeb 
3236c92544dSBjoern A. Zeeb static int
mt7615_tm_dump_stats(struct mt76_phy * mphy,struct sk_buff * msg)3246c92544dSBjoern A. Zeeb mt7615_tm_dump_stats(struct mt76_phy *mphy, struct sk_buff *msg)
3256c92544dSBjoern A. Zeeb {
3266c92544dSBjoern A. Zeeb 	struct mt7615_phy *phy = mphy->priv;
3276c92544dSBjoern A. Zeeb 	void *rx, *rssi;
3286c92544dSBjoern A. Zeeb 	int i;
3296c92544dSBjoern A. Zeeb 
3306c92544dSBjoern A. Zeeb 	rx = nla_nest_start(msg, MT76_TM_STATS_ATTR_LAST_RX);
3316c92544dSBjoern A. Zeeb 	if (!rx)
3326c92544dSBjoern A. Zeeb 		return -ENOMEM;
3336c92544dSBjoern A. Zeeb 
3346c92544dSBjoern A. Zeeb 	if (nla_put_s32(msg, MT76_TM_RX_ATTR_FREQ_OFFSET, phy->test.last_freq_offset))
3356c92544dSBjoern A. Zeeb 		return -ENOMEM;
3366c92544dSBjoern A. Zeeb 
3376c92544dSBjoern A. Zeeb 	rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_RCPI);
3386c92544dSBjoern A. Zeeb 	if (!rssi)
3396c92544dSBjoern A. Zeeb 		return -ENOMEM;
3406c92544dSBjoern A. Zeeb 
3416c92544dSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(phy->test.last_rcpi); i++)
3426c92544dSBjoern A. Zeeb 		if (nla_put_u8(msg, i, phy->test.last_rcpi[i]))
3436c92544dSBjoern A. Zeeb 			return -ENOMEM;
3446c92544dSBjoern A. Zeeb 
3456c92544dSBjoern A. Zeeb 	nla_nest_end(msg, rssi);
3466c92544dSBjoern A. Zeeb 
3476c92544dSBjoern A. Zeeb 	rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_IB_RSSI);
3486c92544dSBjoern A. Zeeb 	if (!rssi)
3496c92544dSBjoern A. Zeeb 		return -ENOMEM;
3506c92544dSBjoern A. Zeeb 
3516c92544dSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(phy->test.last_ib_rssi); i++)
3526c92544dSBjoern A. Zeeb 		if (nla_put_s8(msg, i, phy->test.last_ib_rssi[i]))
3536c92544dSBjoern A. Zeeb 			return -ENOMEM;
3546c92544dSBjoern A. Zeeb 
3556c92544dSBjoern A. Zeeb 	nla_nest_end(msg, rssi);
3566c92544dSBjoern A. Zeeb 
3576c92544dSBjoern A. Zeeb 	rssi = nla_nest_start(msg, MT76_TM_RX_ATTR_WB_RSSI);
3586c92544dSBjoern A. Zeeb 	if (!rssi)
3596c92544dSBjoern A. Zeeb 		return -ENOMEM;
3606c92544dSBjoern A. Zeeb 
3616c92544dSBjoern A. Zeeb 	for (i = 0; i < ARRAY_SIZE(phy->test.last_wb_rssi); i++)
3626c92544dSBjoern A. Zeeb 		if (nla_put_s8(msg, i, phy->test.last_wb_rssi[i]))
3636c92544dSBjoern A. Zeeb 			return -ENOMEM;
3646c92544dSBjoern A. Zeeb 
3656c92544dSBjoern A. Zeeb 	nla_nest_end(msg, rssi);
3666c92544dSBjoern A. Zeeb 
3676c92544dSBjoern A. Zeeb 	nla_nest_end(msg, rx);
3686c92544dSBjoern A. Zeeb 
3696c92544dSBjoern A. Zeeb 	return 0;
3706c92544dSBjoern A. Zeeb }
3716c92544dSBjoern A. Zeeb 
3726c92544dSBjoern A. Zeeb const struct mt76_testmode_ops mt7615_testmode_ops = {
3736c92544dSBjoern A. Zeeb 	.set_state = mt7615_tm_set_state,
3746c92544dSBjoern A. Zeeb 	.set_params = mt7615_tm_set_params,
3756c92544dSBjoern A. Zeeb 	.dump_stats = mt7615_tm_dump_stats,
3766c92544dSBjoern A. Zeeb };
377