mt76: mt7921: introduce testmode support
authorLorenzo Bianconi <lorenzo@kernel.org>
Mon, 21 Jun 2021 09:23:26 +0000 (11:23 +0200)
committerFelix Fietkau <nbd@nbd.name>
Wed, 20 Oct 2021 08:36:28 +0000 (10:36 +0200)
We add the testmode support to access the testmode feature provided
by the MT7921 firmware.

Tested-by: Sean Wang <sean.wang@mediatek.com>
Co-developed-by: Sean Wang <sean.wang@mediatek.com>
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h
drivers/net/wireless/mediatek/mt76/mt7921/Makefile
drivers/net/wireless/mediatek/mt76/mt7921/init.c
drivers/net/wireless/mediatek/mt76/mt7921/main.c
drivers/net/wireless/mediatek/mt76/mt7921/mcu.h
drivers/net/wireless/mediatek/mt76/mt7921/mt7921.h
drivers/net/wireless/mediatek/mt76/mt7921/testmode.c [new file with mode: 0644]
drivers/net/wireless/mediatek/mt76/testmode.c
drivers/net/wireless/mediatek/mt76/testmode.h

index 4bcd728..ab77289 100644 (file)
@@ -548,6 +548,7 @@ enum {
 
 /* offload mcu commands */
 enum {
+       MCU_CMD_TEST_CTRL = MCU_CE_PREFIX | 0x01,
        MCU_CMD_START_HW_SCAN = MCU_CE_PREFIX | 0x03,
        MCU_CMD_SET_PS_PROFILE = MCU_CE_PREFIX | 0x05,
        MCU_CMD_SET_CHAN_DOMAIN = MCU_CE_PREFIX | 0x0f,
index 0ebb599..3471d82 100644 (file)
@@ -5,3 +5,4 @@ obj-$(CONFIG_MT7921E) += mt7921e.o
 CFLAGS_trace.o := -I$(src)
 
 mt7921e-y := pci.o mac.o mcu.o dma.o eeprom.o main.o init.o debugfs.o trace.o
+mt7921e-$(CONFIG_NL80211_TESTMODE) += testmode.o
index fd119b2..49725ca 100644 (file)
@@ -141,6 +141,8 @@ int mt7921_mac_init(struct mt7921_dev *dev)
        for (i = 0; i < 2; i++)
                mt7921_mac_init_band(dev, i);
 
+       dev->mt76.rxfilter = mt76_rr(dev, MT_WF_RFCR(0));
+
        return mt76_connac_mcu_set_rts_thresh(&dev->mt76, 0x92b, 0);
 }
 
index 0ad9ffb..7c4f97d 100644 (file)
@@ -1240,6 +1240,8 @@ const struct ieee80211_ops mt7921_ops = {
        .sta_statistics = mt7921_sta_statistics,
        .sched_scan_start = mt7921_start_sched_scan,
        .sched_scan_stop = mt7921_stop_sched_scan,
+       CFG80211_TESTMODE_CMD(mt7921_testmode_cmd)
+       CFG80211_TESTMODE_DUMP(mt7921_testmode_dump)
 #ifdef CONFIG_PM
        .suspend = mt7921_suspend,
        .resume = mt7921_resume,
index 42e7271..c4fc749 100644 (file)
@@ -320,4 +320,30 @@ struct mt7921_mcu_tx_done_event {
 
        u8 rsv1[32];
 } __packed;
+
+enum {
+       TM_SWITCH_MODE,
+       TM_SET_AT_CMD,
+       TM_QUERY_AT_CMD,
+};
+
+enum {
+       MT7921_TM_NORMAL,
+       MT7921_TM_TESTMODE,
+       MT7921_TM_ICAP,
+       MT7921_TM_ICAP_OVERLAP,
+       MT7921_TM_WIFISPECTRUM,
+};
+
+struct mt7921_rftest_cmd {
+       u8 action;
+       u8 rsv[3];
+       __le32 param0;
+       __le32 param1;
+} __packed;
+
+struct mt7921_rftest_evt {
+       __le32 param0;
+       __le32 param1;
+} __packed;
 #endif
index 2d8bd6b..ac0b414 100644 (file)
@@ -388,4 +388,8 @@ void mt7921_pm_interface_iter(void *priv, u8 *mac, struct ieee80211_vif *vif);
 void mt7921_coredump_work(struct work_struct *work);
 int mt7921_wfsys_reset(struct mt7921_dev *dev);
 int mt7921_get_txpwr_info(struct mt7921_dev *dev, struct mt7921_txpwr *txpwr);
+int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
+                       void *data, int len);
+int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
+                        struct netlink_callback *cb, void *data, int len);
 #endif
diff --git a/drivers/net/wireless/mediatek/mt76/mt7921/testmode.c b/drivers/net/wireless/mediatek/mt76/mt7921/testmode.c
new file mode 100644 (file)
index 0000000..8bd4387
--- /dev/null
@@ -0,0 +1,197 @@
+// SPDX-License-Identifier: ISC
+
+#include "mt7921.h"
+#include "mcu.h"
+
+enum mt7921_testmode_attr {
+       MT7921_TM_ATTR_UNSPEC,
+       MT7921_TM_ATTR_SET,
+       MT7921_TM_ATTR_QUERY,
+       MT7921_TM_ATTR_RSP,
+
+       /* keep last */
+       NUM_MT7921_TM_ATTRS,
+       MT7921_TM_ATTR_MAX = NUM_MT7921_TM_ATTRS - 1,
+};
+
+struct mt7921_tm_cmd {
+       u8 action;
+       u32 param0;
+       u32 param1;
+};
+
+struct mt7921_tm_evt {
+       u32 param0;
+       u32 param1;
+};
+
+static const struct nla_policy mt7921_tm_policy[NUM_MT7921_TM_ATTRS] = {
+       [MT7921_TM_ATTR_SET] = NLA_POLICY_EXACT_LEN(sizeof(struct mt7921_tm_cmd)),
+       [MT7921_TM_ATTR_QUERY] = NLA_POLICY_EXACT_LEN(sizeof(struct mt7921_tm_cmd)),
+};
+
+static int
+mt7921_tm_set(struct mt7921_dev *dev, struct mt7921_tm_cmd *req)
+{
+       struct mt7921_rftest_cmd cmd = {
+               .action = req->action,
+               .param0 = cpu_to_le32(req->param0),
+               .param1 = cpu_to_le32(req->param1),
+       };
+       bool testmode = false, normal = false;
+       struct mt76_connac_pm *pm = &dev->pm;
+       struct mt76_phy *phy = &dev->mphy;
+       int ret = -ENOTCONN;
+
+       mutex_lock(&dev->mt76.mutex);
+
+       if (req->action == TM_SWITCH_MODE) {
+               if (req->param0 == MT7921_TM_NORMAL)
+                       normal = true;
+               else
+                       testmode = true;
+       }
+
+       if (testmode) {
+               /* Make sure testmode running on full power mode */
+               pm->enable = false;
+               cancel_delayed_work_sync(&pm->ps_work);
+               cancel_work_sync(&pm->wake_work);
+               __mt7921_mcu_drv_pmctrl(dev);
+
+               mt76_wr(dev, MT_WF_RFCR(0), dev->mt76.rxfilter);
+               phy->test.state = MT76_TM_STATE_ON;
+       }
+
+       if (!mt76_testmode_enabled(phy))
+               goto out;
+
+       ret = mt76_mcu_send_msg(&dev->mt76, MCU_CMD_TEST_CTRL, &cmd,
+                               sizeof(cmd), false);
+       if (ret)
+               goto out;
+
+       if (normal) {
+               /* Switch back to the normal world */
+               phy->test.state = MT76_TM_STATE_OFF;
+               pm->enable = true;
+       }
+out:
+       mutex_unlock(&dev->mt76.mutex);
+
+       return ret;
+}
+
+static int
+mt7921_tm_query(struct mt7921_dev *dev, struct mt7921_tm_cmd *req,
+               struct mt7921_tm_evt *evt_resp)
+{
+       struct mt7921_rftest_cmd cmd = {
+               .action = req->action,
+               .param0 = cpu_to_le32(req->param0),
+               .param1 = cpu_to_le32(req->param1),
+       };
+       struct mt7921_rftest_evt *evt;
+       struct sk_buff *skb;
+       int ret;
+
+       ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_CMD_TEST_CTRL,
+                                       &cmd, sizeof(cmd), true, &skb);
+       if (ret)
+               goto out;
+
+       evt = (struct mt7921_rftest_evt *)skb->data;
+       evt_resp->param0 = le32_to_cpu(evt->param0);
+       evt_resp->param1 = le32_to_cpu(evt->param1);
+out:
+       dev_kfree_skb(skb);
+
+       return ret;
+}
+
+int mt7921_testmode_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
+                       void *data, int len)
+{
+       struct nlattr *tb[NUM_MT76_TM_ATTRS];
+       struct mt76_phy *mphy = hw->priv;
+       struct mt7921_phy *phy = mphy->priv;
+       int err;
+
+       if (!test_bit(MT76_STATE_RUNNING, &mphy->state) ||
+           !(hw->conf.flags & IEEE80211_CONF_MONITOR))
+               return -ENOTCONN;
+
+       err = nla_parse_deprecated(tb, MT76_TM_ATTR_MAX, data, len,
+                                  mt76_tm_policy, NULL);
+       if (err)
+               return err;
+
+       if (tb[MT76_TM_ATTR_DRV_DATA]) {
+               struct nlattr *drv_tb[NUM_MT7921_TM_ATTRS], *data;
+               int ret;
+
+               data = tb[MT76_TM_ATTR_DRV_DATA];
+               ret = nla_parse_nested_deprecated(drv_tb,
+                                                 MT7921_TM_ATTR_MAX,
+                                                 data, mt7921_tm_policy,
+                                                 NULL);
+               if (ret)
+                       return ret;
+
+               data = drv_tb[MT7921_TM_ATTR_SET];
+               if (data)
+                       return mt7921_tm_set(phy->dev, nla_data(data));
+       }
+
+       return -EINVAL;
+}
+
+int mt7921_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *msg,
+                        struct netlink_callback *cb, void *data, int len)
+{
+       struct nlattr *tb[NUM_MT76_TM_ATTRS];
+       struct mt76_phy *mphy = hw->priv;
+       struct mt7921_phy *phy = mphy->priv;
+       int err;
+
+       if (!test_bit(MT76_STATE_RUNNING, &mphy->state) ||
+           !(hw->conf.flags & IEEE80211_CONF_MONITOR) ||
+           !mt76_testmode_enabled(mphy))
+               return -ENOTCONN;
+
+       if (cb->args[2]++ > 0)
+               return -ENOENT;
+
+       err = nla_parse_deprecated(tb, MT76_TM_ATTR_MAX, data, len,
+                                  mt76_tm_policy, NULL);
+       if (err)
+               return err;
+
+       if (tb[MT76_TM_ATTR_DRV_DATA]) {
+               struct nlattr *drv_tb[NUM_MT7921_TM_ATTRS], *data;
+               int ret;
+
+               data = tb[MT76_TM_ATTR_DRV_DATA];
+               ret = nla_parse_nested_deprecated(drv_tb,
+                                                 MT7921_TM_ATTR_MAX,
+                                                 data, mt7921_tm_policy,
+                                                 NULL);
+               if (ret)
+                       return ret;
+
+               data = drv_tb[MT7921_TM_ATTR_QUERY];
+               if (data) {
+                       struct mt7921_tm_evt evt_resp;
+
+                       err = mt7921_tm_query(phy->dev, nla_data(data),
+                                             &evt_resp);
+                       if (err)
+                               return err;
+
+                       return nla_put(msg, MT7921_TM_ATTR_RSP,
+                                      sizeof(evt_resp), &evt_resp);
+               }
+       }
+
+       return -EINVAL;
+}
index f73ffbd..66afc2b 100644 (file)
@@ -2,7 +2,7 @@
 /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
 #include "mt76.h"
 
-static const struct nla_policy mt76_tm_policy[NUM_MT76_TM_ATTRS] = {
+const struct nla_policy mt76_tm_policy[NUM_MT76_TM_ATTRS] = {
        [MT76_TM_ATTR_RESET] = { .type = NLA_FLAG },
        [MT76_TM_ATTR_STATE] = { .type = NLA_U8 },
        [MT76_TM_ATTR_TX_COUNT] = { .type = NLA_U32 },
@@ -21,7 +21,9 @@ static const struct nla_policy mt76_tm_policy[NUM_MT76_TM_ATTRS] = {
        [MT76_TM_ATTR_TX_IPG] = { .type = NLA_U32 },
        [MT76_TM_ATTR_TX_TIME] = { .type = NLA_U32 },
        [MT76_TM_ATTR_FREQ_OFFSET] = { .type = NLA_U32 },
+       [MT76_TM_ATTR_DRV_DATA] = { .type = NLA_NESTED },
 };
+EXPORT_SYMBOL_GPL(mt76_tm_policy);
 
 void mt76_testmode_tx_pending(struct mt76_phy *phy)
 {
index d32a765..d1f9c03 100644 (file)
@@ -44,6 +44,7 @@
  * @MT76_TM_ATTR_TX_IPG: tx inter-packet gap, in unit of us (u32)
  * @MT76_TM_ATTR_TX_TIME: packet transmission time, in unit of us (u32)
  *
+ * @MT76_TM_ATTR_DRV_DATA: driver specific netlink attrs (nested)
  */
 enum mt76_testmode_attr {
        MT76_TM_ATTR_UNSPEC,
@@ -78,6 +79,8 @@ enum mt76_testmode_attr {
        MT76_TM_ATTR_TX_IPG,
        MT76_TM_ATTR_TX_TIME,
 
+       MT76_TM_ATTR_DRV_DATA,
+
        /* keep last */
        NUM_MT76_TM_ATTRS,
        MT76_TM_ATTR_MAX = NUM_MT76_TM_ATTRS - 1,
@@ -144,6 +147,7 @@ enum mt76_testmode_rx_attr {
  * @MT76_TM_STATE_TX_FRAMES: send a fixed number of test frames
  * @MT76_TM_STATE_RX_FRAMES: receive packets and keep statistics
  * @MT76_TM_STATE_TX_CONT: waveform tx without time gap
+ * @MT76_TM_STATE_ON: test mode enabled used in offload firmware
  */
 enum mt76_testmode_state {
        MT76_TM_STATE_OFF,
@@ -151,6 +155,7 @@ enum mt76_testmode_state {
        MT76_TM_STATE_TX_FRAMES,
        MT76_TM_STATE_RX_FRAMES,
        MT76_TM_STATE_TX_CONT,
+       MT76_TM_STATE_ON,
 
        /* keep last */
        NUM_MT76_TM_STATES,
@@ -184,4 +189,6 @@ enum mt76_testmode_tx_mode {
        MT76_TM_TX_MODE_MAX = NUM_MT76_TM_TX_MODES - 1,
 };
 
+extern const struct nla_policy mt76_tm_policy[NUM_MT76_TM_ATTRS];
+
 #endif