16c92544dSBjoern A. Zeeb // SPDX-License-Identifier: ISC
26c92544dSBjoern A. Zeeb /* Copyright (C) 2020 MediaTek Inc. */
36c92544dSBjoern A. Zeeb
46c92544dSBjoern A. Zeeb #include "mt7921.h"
56c92544dSBjoern A. Zeeb
66c92544dSBjoern A. Zeeb static int
mt7921_reg_set(void * data,u64 val)76c92544dSBjoern A. Zeeb mt7921_reg_set(void *data, u64 val)
86c92544dSBjoern A. Zeeb {
9*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
106c92544dSBjoern A. Zeeb
11*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
126c92544dSBjoern A. Zeeb mt76_wr(dev, dev->mt76.debugfs_reg, val);
13*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
146c92544dSBjoern A. Zeeb
156c92544dSBjoern A. Zeeb return 0;
166c92544dSBjoern A. Zeeb }
176c92544dSBjoern A. Zeeb
186c92544dSBjoern A. Zeeb static int
mt7921_reg_get(void * data,u64 * val)196c92544dSBjoern A. Zeeb mt7921_reg_get(void *data, u64 *val)
206c92544dSBjoern A. Zeeb {
21*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
226c92544dSBjoern A. Zeeb
23*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
246c92544dSBjoern A. Zeeb *val = mt76_rr(dev, dev->mt76.debugfs_reg);
25*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
266c92544dSBjoern A. Zeeb
276c92544dSBjoern A. Zeeb return 0;
286c92544dSBjoern A. Zeeb }
296c92544dSBjoern A. Zeeb
306c92544dSBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_regval, mt7921_reg_get, mt7921_reg_set,
316c92544dSBjoern A. Zeeb "0x%08llx\n");
326c92544dSBjoern A. Zeeb static int
mt7921_fw_debug_set(void * data,u64 val)336c92544dSBjoern A. Zeeb mt7921_fw_debug_set(void *data, u64 val)
346c92544dSBjoern A. Zeeb {
35*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
366c92544dSBjoern A. Zeeb
37*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
386c92544dSBjoern A. Zeeb
396c92544dSBjoern A. Zeeb dev->fw_debug = (u8)val;
406c92544dSBjoern A. Zeeb mt7921_mcu_fw_log_2_host(dev, dev->fw_debug);
416c92544dSBjoern A. Zeeb
42*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
436c92544dSBjoern A. Zeeb
446c92544dSBjoern A. Zeeb return 0;
456c92544dSBjoern A. Zeeb }
466c92544dSBjoern A. Zeeb
476c92544dSBjoern A. Zeeb static int
mt7921_fw_debug_get(void * data,u64 * val)486c92544dSBjoern A. Zeeb mt7921_fw_debug_get(void *data, u64 *val)
496c92544dSBjoern A. Zeeb {
50*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
516c92544dSBjoern A. Zeeb
526c92544dSBjoern A. Zeeb *val = dev->fw_debug;
536c92544dSBjoern A. Zeeb
546c92544dSBjoern A. Zeeb return 0;
556c92544dSBjoern A. Zeeb }
566c92544dSBjoern A. Zeeb
576c92544dSBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_fw_debug, mt7921_fw_debug_get,
586c92544dSBjoern A. Zeeb mt7921_fw_debug_set, "%lld\n");
596c92544dSBjoern A. Zeeb
60*cbb3ec25SBjoern A. Zeeb DEFINE_SHOW_ATTRIBUTE(mt792x_tx_stats);
616c92544dSBjoern A. Zeeb
626c92544dSBjoern A. Zeeb static void
mt7921_seq_puts_array(struct seq_file * file,const char * str,s8 * val,int len)636c92544dSBjoern A. Zeeb mt7921_seq_puts_array(struct seq_file *file, const char *str,
646c92544dSBjoern A. Zeeb s8 *val, int len)
656c92544dSBjoern A. Zeeb {
666c92544dSBjoern A. Zeeb int i;
676c92544dSBjoern A. Zeeb
686c92544dSBjoern A. Zeeb seq_printf(file, "%-16s:", str);
696c92544dSBjoern A. Zeeb for (i = 0; i < len; i++)
706c92544dSBjoern A. Zeeb if (val[i] == 127)
716c92544dSBjoern A. Zeeb seq_printf(file, " %6s", "N.A");
726c92544dSBjoern A. Zeeb else
736c92544dSBjoern A. Zeeb seq_printf(file, " %6d", val[i]);
746c92544dSBjoern A. Zeeb seq_puts(file, "\n");
756c92544dSBjoern A. Zeeb }
766c92544dSBjoern A. Zeeb
776c92544dSBjoern A. Zeeb #define mt7921_print_txpwr_entry(prefix, rate) \
786c92544dSBjoern A. Zeeb ({ \
796c92544dSBjoern A. Zeeb mt7921_seq_puts_array(s, #prefix " (user)", \
806c92544dSBjoern A. Zeeb txpwr.data[TXPWR_USER].rate, \
816c92544dSBjoern A. Zeeb ARRAY_SIZE(txpwr.data[TXPWR_USER].rate)); \
826c92544dSBjoern A. Zeeb mt7921_seq_puts_array(s, #prefix " (eeprom)", \
836c92544dSBjoern A. Zeeb txpwr.data[TXPWR_EEPROM].rate, \
846c92544dSBjoern A. Zeeb ARRAY_SIZE(txpwr.data[TXPWR_EEPROM].rate)); \
856c92544dSBjoern A. Zeeb mt7921_seq_puts_array(s, #prefix " (tmac)", \
866c92544dSBjoern A. Zeeb txpwr.data[TXPWR_MAC].rate, \
876c92544dSBjoern A. Zeeb ARRAY_SIZE(txpwr.data[TXPWR_MAC].rate)); \
886c92544dSBjoern A. Zeeb })
896c92544dSBjoern A. Zeeb
906c92544dSBjoern A. Zeeb static int
mt7921_txpwr(struct seq_file * s,void * data)916c92544dSBjoern A. Zeeb mt7921_txpwr(struct seq_file *s, void *data)
926c92544dSBjoern A. Zeeb {
93*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = dev_get_drvdata(s->private);
946c92544dSBjoern A. Zeeb struct mt7921_txpwr txpwr;
956c92544dSBjoern A. Zeeb int ret;
966c92544dSBjoern A. Zeeb
97*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
986c92544dSBjoern A. Zeeb ret = mt7921_get_txpwr_info(dev, &txpwr);
99*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
1006c92544dSBjoern A. Zeeb
1016c92544dSBjoern A. Zeeb if (ret)
1026c92544dSBjoern A. Zeeb return ret;
1036c92544dSBjoern A. Zeeb
1046c92544dSBjoern A. Zeeb seq_printf(s, "Tx power table (channel %d)\n", txpwr.ch);
1056c92544dSBjoern A. Zeeb seq_printf(s, "%-16s %6s %6s %6s %6s\n",
1066c92544dSBjoern A. Zeeb " ", "1m", "2m", "5m", "11m");
1076c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(CCK, cck);
1086c92544dSBjoern A. Zeeb
1096c92544dSBjoern A. Zeeb seq_printf(s, "%-16s %6s %6s %6s %6s %6s %6s %6s %6s\n",
1106c92544dSBjoern A. Zeeb " ", "6m", "9m", "12m", "18m", "24m", "36m",
1116c92544dSBjoern A. Zeeb "48m", "54m");
1126c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(OFDM, ofdm);
1136c92544dSBjoern A. Zeeb
1146c92544dSBjoern A. Zeeb seq_printf(s, "%-16s %6s %6s %6s %6s %6s %6s %6s %6s\n",
1156c92544dSBjoern A. Zeeb " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
1166c92544dSBjoern A. Zeeb "mcs6", "mcs7");
1176c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HT20, ht20);
1186c92544dSBjoern A. Zeeb
1196c92544dSBjoern A. Zeeb seq_printf(s, "%-16s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
1206c92544dSBjoern A. Zeeb " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
1216c92544dSBjoern A. Zeeb "mcs6", "mcs7", "mcs32");
1226c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HT40, ht40);
1236c92544dSBjoern A. Zeeb
1246c92544dSBjoern A. Zeeb seq_printf(s, "%-16s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s %6s\n",
1256c92544dSBjoern A. Zeeb " ", "mcs0", "mcs1", "mcs2", "mcs3", "mcs4", "mcs5",
1266c92544dSBjoern A. Zeeb "mcs6", "mcs7", "mcs8", "mcs9", "mcs10", "mcs11");
1276c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(VHT20, vht20);
1286c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(VHT40, vht40);
1296c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(VHT80, vht80);
1306c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(VHT160, vht160);
1316c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE26, he26);
1326c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE52, he52);
1336c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE106, he106);
1346c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE242, he242);
1356c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE484, he484);
1366c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE996, he996);
1376c92544dSBjoern A. Zeeb mt7921_print_txpwr_entry(HE996x2, he996x2);
1386c92544dSBjoern A. Zeeb
1396c92544dSBjoern A. Zeeb return 0;
1406c92544dSBjoern A. Zeeb }
1416c92544dSBjoern A. Zeeb
1426c92544dSBjoern A. Zeeb static int
mt7921_pm_set(void * data,u64 val)1436c92544dSBjoern A. Zeeb mt7921_pm_set(void *data, u64 val)
1446c92544dSBjoern A. Zeeb {
145*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
1466c92544dSBjoern A. Zeeb struct mt76_connac_pm *pm = &dev->pm;
1476c92544dSBjoern A. Zeeb
1486c92544dSBjoern A. Zeeb if (mt76_is_usb(&dev->mt76))
1496c92544dSBjoern A. Zeeb return -EOPNOTSUPP;
1506c92544dSBjoern A. Zeeb
1516c92544dSBjoern A. Zeeb mutex_lock(&dev->mt76.mutex);
1526c92544dSBjoern A. Zeeb
1536c92544dSBjoern A. Zeeb if (val == pm->enable_user)
1546c92544dSBjoern A. Zeeb goto out;
1556c92544dSBjoern A. Zeeb
1566c92544dSBjoern A. Zeeb if (!pm->enable_user) {
1576c92544dSBjoern A. Zeeb pm->stats.last_wake_event = jiffies;
1586c92544dSBjoern A. Zeeb pm->stats.last_doze_event = jiffies;
1596c92544dSBjoern A. Zeeb }
1606c92544dSBjoern A. Zeeb /* make sure the chip is awake here and ps_work is scheduled
1616c92544dSBjoern A. Zeeb * just at end of the this routine.
1626c92544dSBjoern A. Zeeb */
1636c92544dSBjoern A. Zeeb pm->enable = false;
1646c92544dSBjoern A. Zeeb mt76_connac_pm_wake(&dev->mphy, pm);
1656c92544dSBjoern A. Zeeb
1666c92544dSBjoern A. Zeeb pm->enable_user = val;
1676c92544dSBjoern A. Zeeb mt7921_set_runtime_pm(dev);
1686c92544dSBjoern A. Zeeb mt76_connac_power_save_sched(&dev->mphy, pm);
1696c92544dSBjoern A. Zeeb out:
1706c92544dSBjoern A. Zeeb mutex_unlock(&dev->mt76.mutex);
1716c92544dSBjoern A. Zeeb
1726c92544dSBjoern A. Zeeb return 0;
1736c92544dSBjoern A. Zeeb }
1746c92544dSBjoern A. Zeeb
1756c92544dSBjoern A. Zeeb static int
mt7921_pm_get(void * data,u64 * val)1766c92544dSBjoern A. Zeeb mt7921_pm_get(void *data, u64 *val)
1776c92544dSBjoern A. Zeeb {
178*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
1796c92544dSBjoern A. Zeeb
1806c92544dSBjoern A. Zeeb *val = dev->pm.enable_user;
1816c92544dSBjoern A. Zeeb
1826c92544dSBjoern A. Zeeb return 0;
1836c92544dSBjoern A. Zeeb }
1846c92544dSBjoern A. Zeeb
1856c92544dSBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_pm, mt7921_pm_get, mt7921_pm_set, "%lld\n");
1866c92544dSBjoern A. Zeeb
1876c92544dSBjoern A. Zeeb static int
mt7921_deep_sleep_set(void * data,u64 val)1886c92544dSBjoern A. Zeeb mt7921_deep_sleep_set(void *data, u64 val)
1896c92544dSBjoern A. Zeeb {
190*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
1916c92544dSBjoern A. Zeeb struct mt76_connac_pm *pm = &dev->pm;
1926c92544dSBjoern A. Zeeb bool monitor = !!(dev->mphy.hw->conf.flags & IEEE80211_CONF_MONITOR);
1936c92544dSBjoern A. Zeeb bool enable = !!val;
1946c92544dSBjoern A. Zeeb
1956c92544dSBjoern A. Zeeb if (mt76_is_usb(&dev->mt76))
1966c92544dSBjoern A. Zeeb return -EOPNOTSUPP;
1976c92544dSBjoern A. Zeeb
198*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
1996c92544dSBjoern A. Zeeb if (pm->ds_enable_user == enable)
2006c92544dSBjoern A. Zeeb goto out;
2016c92544dSBjoern A. Zeeb
2026c92544dSBjoern A. Zeeb pm->ds_enable_user = enable;
2036c92544dSBjoern A. Zeeb pm->ds_enable = enable && !monitor;
2046c92544dSBjoern A. Zeeb mt76_connac_mcu_set_deep_sleep(&dev->mt76, pm->ds_enable);
2056c92544dSBjoern A. Zeeb out:
206*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
2076c92544dSBjoern A. Zeeb
2086c92544dSBjoern A. Zeeb return 0;
2096c92544dSBjoern A. Zeeb }
2106c92544dSBjoern A. Zeeb
2116c92544dSBjoern A. Zeeb static int
mt7921_deep_sleep_get(void * data,u64 * val)2126c92544dSBjoern A. Zeeb mt7921_deep_sleep_get(void *data, u64 *val)
2136c92544dSBjoern A. Zeeb {
214*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
2156c92544dSBjoern A. Zeeb
2166c92544dSBjoern A. Zeeb *val = dev->pm.ds_enable_user;
2176c92544dSBjoern A. Zeeb
2186c92544dSBjoern A. Zeeb return 0;
2196c92544dSBjoern A. Zeeb }
2206c92544dSBjoern A. Zeeb
2216c92544dSBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_ds, mt7921_deep_sleep_get,
2226c92544dSBjoern A. Zeeb mt7921_deep_sleep_set, "%lld\n");
2236c92544dSBjoern A. Zeeb
224*cbb3ec25SBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_pm_idle_timeout, mt792x_pm_idle_timeout_get,
225*cbb3ec25SBjoern A. Zeeb mt792x_pm_idle_timeout_set, "%lld\n");
2266c92544dSBjoern A. Zeeb
mt7921_chip_reset(void * data,u64 val)2276c92544dSBjoern A. Zeeb static int mt7921_chip_reset(void *data, u64 val)
2286c92544dSBjoern A. Zeeb {
229*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = data;
2306c92544dSBjoern A. Zeeb int ret = 0;
2316c92544dSBjoern A. Zeeb
2326c92544dSBjoern A. Zeeb switch (val) {
2336c92544dSBjoern A. Zeeb case 1:
2346c92544dSBjoern A. Zeeb /* Reset wifisys directly. */
235*cbb3ec25SBjoern A. Zeeb mt792x_reset(&dev->mt76);
2366c92544dSBjoern A. Zeeb break;
2376c92544dSBjoern A. Zeeb default:
2386c92544dSBjoern A. Zeeb /* Collect the core dump before reset wifisys. */
239*cbb3ec25SBjoern A. Zeeb mt792x_mutex_acquire(dev);
2406c92544dSBjoern A. Zeeb ret = mt76_connac_mcu_chip_config(&dev->mt76);
241*cbb3ec25SBjoern A. Zeeb mt792x_mutex_release(dev);
2426c92544dSBjoern A. Zeeb break;
2436c92544dSBjoern A. Zeeb }
2446c92544dSBjoern A. Zeeb
2456c92544dSBjoern A. Zeeb return ret;
2466c92544dSBjoern A. Zeeb }
2476c92544dSBjoern A. Zeeb
2486c92544dSBjoern A. Zeeb DEFINE_DEBUGFS_ATTRIBUTE(fops_reset, NULL, mt7921_chip_reset, "%lld\n");
2496c92544dSBjoern A. Zeeb
2506c92544dSBjoern A. Zeeb static int
mt7921s_sched_quota_read(struct seq_file * s,void * data)2516c92544dSBjoern A. Zeeb mt7921s_sched_quota_read(struct seq_file *s, void *data)
2526c92544dSBjoern A. Zeeb {
253*cbb3ec25SBjoern A. Zeeb struct mt792x_dev *dev = dev_get_drvdata(s->private);
2546c92544dSBjoern A. Zeeb struct mt76_sdio *sdio = &dev->mt76.sdio;
2556c92544dSBjoern A. Zeeb
2566c92544dSBjoern A. Zeeb seq_printf(s, "pse_data_quota\t%d\n", sdio->sched.pse_data_quota);
2576c92544dSBjoern A. Zeeb seq_printf(s, "ple_data_quota\t%d\n", sdio->sched.ple_data_quota);
2586c92544dSBjoern A. Zeeb seq_printf(s, "pse_mcu_quota\t%d\n", sdio->sched.pse_mcu_quota);
2596c92544dSBjoern A. Zeeb seq_printf(s, "sched_deficit\t%d\n", sdio->sched.deficit);
2606c92544dSBjoern A. Zeeb
2616c92544dSBjoern A. Zeeb return 0;
2626c92544dSBjoern A. Zeeb }
2636c92544dSBjoern A. Zeeb
mt7921_init_debugfs(struct mt792x_dev * dev)264*cbb3ec25SBjoern A. Zeeb int mt7921_init_debugfs(struct mt792x_dev *dev)
2656c92544dSBjoern A. Zeeb {
2666c92544dSBjoern A. Zeeb struct dentry *dir;
2676c92544dSBjoern A. Zeeb
2686c92544dSBjoern A. Zeeb dir = mt76_register_debugfs_fops(&dev->mphy, &fops_regval);
2696c92544dSBjoern A. Zeeb if (!dir)
2706c92544dSBjoern A. Zeeb return -ENOMEM;
2716c92544dSBjoern A. Zeeb
2726c92544dSBjoern A. Zeeb if (mt76_is_mmio(&dev->mt76))
2736c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues",
274*cbb3ec25SBjoern A. Zeeb dir, mt792x_queues_read);
2756c92544dSBjoern A. Zeeb else
2766c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "xmit-queues",
2776c92544dSBjoern A. Zeeb dir, mt76_queues_read);
2786c92544dSBjoern A. Zeeb
2796c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "acq", dir,
280*cbb3ec25SBjoern A. Zeeb mt792x_queues_acq);
2816c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "txpower_sku", dir,
2826c92544dSBjoern A. Zeeb mt7921_txpwr);
283*cbb3ec25SBjoern A. Zeeb debugfs_create_file("tx_stats", 0400, dir, dev, &mt792x_tx_stats_fops);
2846c92544dSBjoern A. Zeeb debugfs_create_file("fw_debug", 0600, dir, dev, &fops_fw_debug);
2856c92544dSBjoern A. Zeeb debugfs_create_file("runtime-pm", 0600, dir, dev, &fops_pm);
2866c92544dSBjoern A. Zeeb debugfs_create_file("idle-timeout", 0600, dir, dev,
2876c92544dSBjoern A. Zeeb &fops_pm_idle_timeout);
2886c92544dSBjoern A. Zeeb debugfs_create_file("chip_reset", 0600, dir, dev, &fops_reset);
2896c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "runtime_pm_stats", dir,
290*cbb3ec25SBjoern A. Zeeb mt792x_pm_stats);
2916c92544dSBjoern A. Zeeb debugfs_create_file("deep-sleep", 0600, dir, dev, &fops_ds);
2926c92544dSBjoern A. Zeeb if (mt76_is_sdio(&dev->mt76))
2936c92544dSBjoern A. Zeeb debugfs_create_devm_seqfile(dev->mt76.dev, "sched-quota", dir,
2946c92544dSBjoern A. Zeeb mt7921s_sched_quota_read);
2956c92544dSBjoern A. Zeeb return 0;
2966c92544dSBjoern A. Zeeb }
297