Skip to content

Commit 5e69ea7

Browse files
Yufeng Modavem330
authored andcommitted
net: hns3: refactor the debugfs process
Currently, each debugfs command needs to create a file to get the information. To better support more debugfs commands, the debugfs process is reconstructed, including the process of creating dentries and files, and obtaining information. Signed-off-by: Yufeng Mo <[email protected]> Signed-off-by: Huazhong Tan <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1ddc028 commit 5e69ea7

File tree

8 files changed

+236
-47
lines changed

8 files changed

+236
-47
lines changed

drivers/net/ethernet/hisilicon/hns3/hnae3.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ enum hnae3_port_base_vlan_state {
250250
HNAE3_PORT_BASE_VLAN_NOCHANGE,
251251
};
252252

253+
enum hnae3_dbg_cmd {
254+
HNAE3_DBG_CMD_TM_NODES,
255+
HNAE3_DBG_CMD_TM_PRI,
256+
HNAE3_DBG_CMD_TM_QSET,
257+
HNAE3_DBG_CMD_UNKNOWN,
258+
};
259+
253260
struct hnae3_vector_info {
254261
u8 __iomem *io_addr;
255262
int vector;
@@ -627,7 +634,7 @@ struct hnae3_ae_ops {
627634
int (*add_arfs_entry)(struct hnae3_handle *handle, u16 queue_id,
628635
u16 flow_id, struct flow_keys *fkeys);
629636
int (*dbg_run_cmd)(struct hnae3_handle *handle, const char *cmd_buf);
630-
int (*dbg_read_cmd)(struct hnae3_handle *handle, const char *cmd_buf,
637+
int (*dbg_read_cmd)(struct hnae3_handle *handle, enum hnae3_dbg_cmd cmd,
631638
char *buf, int len);
632639
pci_ers_result_t (*handle_hw_ras_error)(struct hnae3_ae_dev *ae_dev);
633640
bool (*get_hw_reset_stat)(struct hnae3_handle *handle);
@@ -790,10 +797,6 @@ struct hnae3_handle {
790797
#define hnae3_get_bit(origin, shift) \
791798
hnae3_get_field(origin, 0x1 << (shift), shift)
792799

793-
#define HNAE3_DBG_TM_NODES "tm_nodes"
794-
#define HNAE3_DBG_TM_PRI "tm_priority"
795-
#define HNAE3_DBG_TM_QSET "tm_qset"
796-
797800
int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev);
798801
void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev);
799802

drivers/net/ethernet/hisilicon/hns3/hns3_debugfs.c

Lines changed: 164 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,48 @@
55
#include <linux/device.h>
66

77
#include "hnae3.h"
8+
#include "hns3_debugfs.h"
89
#include "hns3_enet.h"
910

10-
#define HNS3_DBG_READ_LEN 65536
11-
#define HNS3_DBG_WRITE_LEN 1024
12-
1311
static struct dentry *hns3_dbgfs_root;
1412

13+
static struct hns3_dbg_dentry_info hns3_dbg_dentry[] = {
14+
{
15+
.name = "tm"
16+
},
17+
/* keep common at the bottom and add new directory above */
18+
{
19+
.name = "common"
20+
},
21+
};
22+
23+
static int hns3_dbg_common_file_init(struct hnae3_handle *handle,
24+
unsigned int cmd);
25+
26+
static struct hns3_dbg_cmd_info hns3_dbg_cmd[] = {
27+
{
28+
.name = "tm_nodes",
29+
.cmd = HNAE3_DBG_CMD_TM_NODES,
30+
.dentry = HNS3_DBG_DENTRY_TM,
31+
.buf_len = HNS3_DBG_READ_LEN,
32+
.init = hns3_dbg_common_file_init,
33+
},
34+
{
35+
.name = "tm_priority",
36+
.cmd = HNAE3_DBG_CMD_TM_PRI,
37+
.dentry = HNS3_DBG_DENTRY_TM,
38+
.buf_len = HNS3_DBG_READ_LEN,
39+
.init = hns3_dbg_common_file_init,
40+
},
41+
{
42+
.name = "tm_qset",
43+
.cmd = HNAE3_DBG_CMD_TM_QSET,
44+
.dentry = HNS3_DBG_DENTRY_TM,
45+
.buf_len = HNS3_DBG_READ_LEN,
46+
.init = hns3_dbg_common_file_init,
47+
},
48+
};
49+
1550
static int hns3_dbg_queue_info(struct hnae3_handle *h,
1651
const char *cmd_buf)
1752
{
@@ -493,37 +528,90 @@ static ssize_t hns3_dbg_cmd_write(struct file *filp, const char __user *buffer,
493528
return count;
494529
}
495530

531+
static int hns3_dbg_get_cmd_index(struct hnae3_handle *handle,
532+
const unsigned char *name, u32 *index)
533+
{
534+
u32 i;
535+
536+
for (i = 0; i < ARRAY_SIZE(hns3_dbg_cmd); i++) {
537+
if (!strncmp(name, hns3_dbg_cmd[i].name,
538+
strlen(hns3_dbg_cmd[i].name))) {
539+
*index = i;
540+
return 0;
541+
}
542+
}
543+
544+
dev_err(&handle->pdev->dev, "unknown command(%s)\n", name);
545+
return -EINVAL;
546+
}
547+
548+
static int hns3_dbg_read_cmd(struct hnae3_handle *handle,
549+
enum hnae3_dbg_cmd cmd, char *buf, int len)
550+
{
551+
const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
552+
553+
if (!ops->dbg_read_cmd)
554+
return -EOPNOTSUPP;
555+
556+
return ops->dbg_read_cmd(handle, cmd, buf, len);
557+
}
558+
496559
static ssize_t hns3_dbg_read(struct file *filp, char __user *buffer,
497560
size_t count, loff_t *ppos)
498561
{
499562
struct hnae3_handle *handle = filp->private_data;
500-
const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
501563
struct hns3_nic_priv *priv = handle->priv;
502-
char *cmd_buf, *read_buf;
503564
ssize_t size = 0;
504-
int ret = 0;
505-
506-
read_buf = kzalloc(HNS3_DBG_READ_LEN, GFP_KERNEL);
507-
if (!read_buf)
508-
return -ENOMEM;
565+
char **save_buf;
566+
char *read_buf;
567+
u32 index;
568+
int ret;
509569

510-
cmd_buf = filp->f_path.dentry->d_iname;
570+
ret = hns3_dbg_get_cmd_index(handle, filp->f_path.dentry->d_iname,
571+
&index);
572+
if (ret)
573+
return ret;
511574

512-
if (ops->dbg_read_cmd)
513-
ret = ops->dbg_read_cmd(handle, cmd_buf, read_buf,
514-
HNS3_DBG_READ_LEN);
575+
save_buf = &hns3_dbg_cmd[index].buf;
515576

516-
if (ret) {
517-
dev_info(priv->dev, "unknown command\n");
577+
if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state) ||
578+
test_bit(HNS3_NIC_STATE_RESETTING, &priv->state)) {
579+
ret = -EBUSY;
518580
goto out;
519581
}
520582

583+
if (*save_buf) {
584+
read_buf = *save_buf;
585+
} else {
586+
read_buf = kvzalloc(hns3_dbg_cmd[index].buf_len, GFP_KERNEL);
587+
if (!read_buf)
588+
return -ENOMEM;
589+
590+
/* save the buffer addr until the last read operation */
591+
*save_buf = read_buf;
592+
}
593+
594+
/* get data ready for the first time to read */
595+
if (!*ppos) {
596+
ret = hns3_dbg_read_cmd(handle, hns3_dbg_cmd[index].cmd,
597+
read_buf, hns3_dbg_cmd[index].buf_len);
598+
if (ret)
599+
goto out;
600+
}
601+
521602
size = simple_read_from_buffer(buffer, count, ppos, read_buf,
522603
strlen(read_buf));
604+
if (size > 0)
605+
return size;
523606

524607
out:
525-
kfree(read_buf);
526-
return size;
608+
/* free the buffer for the last read operation */
609+
if (*save_buf) {
610+
kvfree(*save_buf);
611+
*save_buf = NULL;
612+
}
613+
614+
return ret;
527615
}
528616

529617
static const struct file_operations hns3_dbg_cmd_fops = {
@@ -539,29 +627,76 @@ static const struct file_operations hns3_dbg_fops = {
539627
.read = hns3_dbg_read,
540628
};
541629

542-
void hns3_dbg_init(struct hnae3_handle *handle)
630+
static int
631+
hns3_dbg_common_file_init(struct hnae3_handle *handle, u32 cmd)
632+
{
633+
struct dentry *entry_dir;
634+
635+
entry_dir = hns3_dbg_dentry[hns3_dbg_cmd[cmd].dentry].dentry;
636+
debugfs_create_file(hns3_dbg_cmd[cmd].name, 0400, entry_dir,
637+
handle, &hns3_dbg_fops);
638+
639+
return 0;
640+
}
641+
642+
int hns3_dbg_init(struct hnae3_handle *handle)
543643
{
544644
struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
545645
const char *name = pci_name(handle->pdev);
546-
struct dentry *entry_dir;
646+
int ret;
647+
u32 i;
547648

548-
handle->hnae3_dbgfs = debugfs_create_dir(name, hns3_dbgfs_root);
649+
hns3_dbg_dentry[HNS3_DBG_DENTRY_COMMON].dentry =
650+
debugfs_create_dir(name, hns3_dbgfs_root);
651+
handle->hnae3_dbgfs = hns3_dbg_dentry[HNS3_DBG_DENTRY_COMMON].dentry;
549652

550653
debugfs_create_file("cmd", 0600, handle->hnae3_dbgfs, handle,
551654
&hns3_dbg_cmd_fops);
552655

553-
entry_dir = debugfs_create_dir("tm", handle->hnae3_dbgfs);
554-
if (ae_dev->dev_version > HNAE3_DEVICE_VERSION_V2)
555-
debugfs_create_file(HNAE3_DBG_TM_NODES, 0600, entry_dir, handle,
556-
&hns3_dbg_fops);
557-
debugfs_create_file(HNAE3_DBG_TM_PRI, 0600, entry_dir, handle,
558-
&hns3_dbg_fops);
559-
debugfs_create_file(HNAE3_DBG_TM_QSET, 0600, entry_dir, handle,
560-
&hns3_dbg_fops);
656+
for (i = 0; i < HNS3_DBG_DENTRY_COMMON; i++)
657+
hns3_dbg_dentry[i].dentry =
658+
debugfs_create_dir(hns3_dbg_dentry[i].name,
659+
handle->hnae3_dbgfs);
660+
661+
for (i = 0; i < ARRAY_SIZE(hns3_dbg_cmd); i++) {
662+
if (hns3_dbg_cmd[i].cmd == HNAE3_DBG_CMD_TM_NODES &&
663+
ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2)
664+
continue;
665+
666+
if (!hns3_dbg_cmd[i].init) {
667+
dev_err(&handle->pdev->dev,
668+
"cmd %s lack of init func\n",
669+
hns3_dbg_cmd[i].name);
670+
ret = -EINVAL;
671+
goto out;
672+
}
673+
674+
ret = hns3_dbg_cmd[i].init(handle, i);
675+
if (ret) {
676+
dev_err(&handle->pdev->dev, "failed to init cmd %s\n",
677+
hns3_dbg_cmd[i].name);
678+
goto out;
679+
}
680+
}
681+
682+
return 0;
683+
684+
out:
685+
debugfs_remove_recursive(handle->hnae3_dbgfs);
686+
handle->hnae3_dbgfs = NULL;
687+
return ret;
561688
}
562689

563690
void hns3_dbg_uninit(struct hnae3_handle *handle)
564691
{
692+
u32 i;
693+
694+
for (i = 0; i < ARRAY_SIZE(hns3_dbg_cmd); i++)
695+
if (hns3_dbg_cmd[i].buf) {
696+
kvfree(hns3_dbg_cmd[i].buf);
697+
hns3_dbg_cmd[i].buf = NULL;
698+
}
699+
565700
debugfs_remove_recursive(handle->hnae3_dbgfs);
566701
handle->hnae3_dbgfs = NULL;
567702
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
/* Copyright (c) 2021 Hisilicon Limited. */
3+
4+
#ifndef __HNS3_DEBUGFS_H
5+
#define __HNS3_DEBUGFS_H
6+
7+
#define HNS3_DBG_READ_LEN 65536
8+
#define HNS3_DBG_WRITE_LEN 1024
9+
10+
enum hns3_dbg_dentry_type {
11+
HNS3_DBG_DENTRY_TM,
12+
HNS3_DBG_DENTRY_COMMON,
13+
};
14+
15+
struct hns3_dbg_dentry_info {
16+
const char *name;
17+
struct dentry *dentry;
18+
};
19+
20+
struct hns3_dbg_cmd_info {
21+
const char *name;
22+
enum hnae3_dbg_cmd cmd;
23+
enum hns3_dbg_dentry_type dentry;
24+
u32 buf_len;
25+
char *buf;
26+
int (*init)(struct hnae3_handle *handle, unsigned int cmd);
27+
};
28+
29+
#endif

drivers/net/ethernet/hisilicon/hns3/hns3_enet.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4648,7 +4648,12 @@ static int hns3_client_init(struct hnae3_handle *handle)
46484648

46494649
hns3_dcbnl_setup(handle);
46504650

4651-
hns3_dbg_init(handle);
4651+
ret = hns3_dbg_init(handle);
4652+
if (ret) {
4653+
dev_err(priv->dev, "failed to init debugfs, ret = %d\n",
4654+
ret);
4655+
goto out_client_start;
4656+
}
46524657

46534658
netdev->max_mtu = HNS3_MAX_MTU(ae_dev->dev_specs.max_frm_size);
46544659

drivers/net/ethernet/hisilicon/hns3/hns3_enet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ void hns3_dcbnl_setup(struct hnae3_handle *handle);
650650
static inline void hns3_dcbnl_setup(struct hnae3_handle *handle) {}
651651
#endif
652652

653-
void hns3_dbg_init(struct hnae3_handle *handle);
653+
int hns3_dbg_init(struct hnae3_handle *handle);
654654
void hns3_dbg_uninit(struct hnae3_handle *handle);
655655
void hns3_dbg_register_debugfs(const char *debugfs_dir_name);
656656
void hns3_dbg_unregister_debugfs(void);

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,21 +1800,33 @@ int hclge_dbg_run_cmd(struct hnae3_handle *handle, const char *cmd_buf)
18001800
return 0;
18011801
}
18021802

1803-
int hclge_dbg_read_cmd(struct hnae3_handle *handle, const char *cmd_buf,
1803+
static const struct hclge_dbg_func hclge_dbg_cmd_func[] = {
1804+
{
1805+
.cmd = HNAE3_DBG_CMD_TM_NODES,
1806+
.dbg_dump = hclge_dbg_dump_tm_nodes,
1807+
},
1808+
{
1809+
.cmd = HNAE3_DBG_CMD_TM_PRI,
1810+
.dbg_dump = hclge_dbg_dump_tm_pri,
1811+
},
1812+
{
1813+
.cmd = HNAE3_DBG_CMD_TM_QSET,
1814+
.dbg_dump = hclge_dbg_dump_tm_qset,
1815+
},
1816+
};
1817+
1818+
int hclge_dbg_read_cmd(struct hnae3_handle *handle, enum hnae3_dbg_cmd cmd,
18041819
char *buf, int len)
18051820
{
18061821
struct hclge_vport *vport = hclge_get_vport(handle);
18071822
struct hclge_dev *hdev = vport->back;
1823+
u32 i;
18081824

1809-
if (strncmp(cmd_buf, HNAE3_DBG_TM_NODES,
1810-
strlen(HNAE3_DBG_TM_NODES)) == 0)
1811-
return hclge_dbg_dump_tm_nodes(hdev, buf, len);
1812-
else if (strncmp(cmd_buf, HNAE3_DBG_TM_PRI,
1813-
strlen(HNAE3_DBG_TM_PRI)) == 0)
1814-
return hclge_dbg_dump_tm_pri(hdev, buf, len);
1815-
else if (strncmp(cmd_buf, HNAE3_DBG_TM_QSET,
1816-
strlen(HNAE3_DBG_TM_QSET)) == 0)
1817-
return hclge_dbg_dump_tm_qset(hdev, buf, len);
1825+
for (i = 0; i < ARRAY_SIZE(hclge_dbg_cmd_func); i++) {
1826+
if (cmd == hclge_dbg_cmd_func[i].cmd)
1827+
return hclge_dbg_cmd_func[i].dbg_dump(hdev, buf, len);
1828+
}
18181829

1830+
dev_err(&hdev->pdev->dev, "invalid command(%d)\n", cmd);
18191831
return -EINVAL;
18201832
}

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ struct hclge_dbg_reg_type_info {
8383
struct hclge_dbg_reg_common_msg reg_msg;
8484
};
8585

86+
struct hclge_dbg_func {
87+
enum hnae3_dbg_cmd cmd;
88+
int (*dbg_dump)(struct hclge_dev *hdev, char *buf, int len);
89+
};
90+
8691
static const struct hclge_dbg_dfx_message hclge_dbg_bios_common_reg[] = {
8792
{false, "Reserved"},
8893
{true, "BP_CPU_STATE"},

drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ int hclge_vport_start(struct hclge_vport *vport);
10611061
void hclge_vport_stop(struct hclge_vport *vport);
10621062
int hclge_set_vport_mtu(struct hclge_vport *vport, int new_mtu);
10631063
int hclge_dbg_run_cmd(struct hnae3_handle *handle, const char *cmd_buf);
1064-
int hclge_dbg_read_cmd(struct hnae3_handle *handle, const char *cmd_buf,
1064+
int hclge_dbg_read_cmd(struct hnae3_handle *handle, enum hnae3_dbg_cmd cmd,
10651065
char *buf, int len);
10661066
u16 hclge_covert_handle_qid_global(struct hnae3_handle *handle, u16 queue_id);
10671067
int hclge_notify_client(struct hclge_dev *hdev,

0 commit comments

Comments
 (0)