summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
blob: 316fe2e70fead930808b4acc23f7954d9ffce5af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */

#include <linux/kernel.h>
#include <linux/debugfs.h>
#include "mtk_eth_soc.h"

struct mtk_flow_addr_info
{
	void *src, *dest;
	u16 *src_port, *dest_port;
	bool ipv6;
};

static const char *mtk_foe_entry_state_str(int state)
{
	static const char * const state_str[] = {
		[MTK_FOE_STATE_INVALID] = "INV",
		[MTK_FOE_STATE_UNBIND] = "UNB",
		[MTK_FOE_STATE_BIND] = "BND",
		[MTK_FOE_STATE_FIN] = "FIN",
	};

	if (state >= ARRAY_SIZE(state_str) || !state_str[state])
		return "UNK";

	return state_str[state];
}

static const char *mtk_foe_pkt_type_str(int type)
{
	static const char * const type_str[] = {
		[MTK_PPE_PKT_TYPE_IPV4_HNAPT] = "IPv4 5T",
		[MTK_PPE_PKT_TYPE_IPV4_ROUTE] = "IPv4 3T",
		[MTK_PPE_PKT_TYPE_IPV4_DSLITE] = "DS-LITE",
		[MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T] = "IPv6 3T",
		[MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T] = "IPv6 5T",
		[MTK_PPE_PKT_TYPE_IPV6_6RD] = "6RD",
	};

	if (type >= ARRAY_SIZE(type_str) || !type_str[type])
		return "UNKNOWN";

	return type_str[type];
}

static void
mtk_print_addr(struct seq_file *m, u32 *addr, bool ipv6)
{
	__be32 n_addr[4];
	int i;

	if (!ipv6) {
		seq_printf(m, "%pI4h", addr);
		return;
	}

	for (i = 0; i < ARRAY_SIZE(n_addr); i++)
		n_addr[i] = htonl(addr[i]);
	seq_printf(m, "%pI6", n_addr);
}

static void
mtk_print_addr_info(struct seq_file *m, struct mtk_flow_addr_info *ai)
{
	mtk_print_addr(m, ai->src, ai->ipv6);
	if (ai->src_port)
		seq_printf(m, ":%d", *ai->src_port);
	seq_printf(m, "->");
	mtk_print_addr(m, ai->dest, ai->ipv6);
	if (ai->dest_port)
		seq_printf(m, ":%d", *ai->dest_port);
}

static int
mtk_ppe_debugfs_foe_show(struct seq_file *m, void *private, bool bind)
{
	struct mtk_ppe *ppe = m->private;
	int i;

	for (i = 0; i < MTK_PPE_ENTRIES; i++) {
		struct mtk_foe_entry *entry = mtk_foe_get_entry(ppe, i);
		struct mtk_foe_mac_info *l2;
		struct mtk_flow_addr_info ai = {};
		struct mtk_foe_accounting *acct;
		unsigned char h_source[ETH_ALEN];
		unsigned char h_dest[ETH_ALEN];
		int type, state;
		u32 ib2;


		state = FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1);
		if (!state)
			continue;

		if (bind && state != MTK_FOE_STATE_BIND)
			continue;

		acct = mtk_foe_entry_get_mib(ppe, i, NULL);

		type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
		seq_printf(m, "%05x %s %7s", i,
			   mtk_foe_entry_state_str(state),
			   mtk_foe_pkt_type_str(type));

		switch (type) {
		case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
		case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
			ai.src_port = &entry->ipv4.orig.src_port;
			ai.dest_port = &entry->ipv4.orig.dest_port;
			fallthrough;
		case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
			ai.src = &entry->ipv4.orig.src_ip;
			ai.dest = &entry->ipv4.orig.dest_ip;
			break;
		case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
			ai.src_port = &entry->ipv6.src_port;
			ai.dest_port = &entry->ipv6.dest_port;
			fallthrough;
		case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
		case MTK_PPE_PKT_TYPE_IPV6_6RD:
			ai.src = &entry->ipv6.src_ip;
			ai.dest = &entry->ipv6.dest_ip;
			ai.ipv6 = true;
			break;
		}

		seq_printf(m, " orig=");
		mtk_print_addr_info(m, &ai);

		switch (type) {
		case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
		case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
			ai.src_port = &entry->ipv4.new.src_port;
			ai.dest_port = &entry->ipv4.new.dest_port;
			fallthrough;
		case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
			ai.src = &entry->ipv4.new.src_ip;
			ai.dest = &entry->ipv4.new.dest_ip;
			seq_printf(m, " new=");
			mtk_print_addr_info(m, &ai);
			break;
		}

		if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
			l2 = &entry->ipv6.l2;
			ib2 = entry->ipv6.ib2;
		} else {
			l2 = &entry->ipv4.l2;
			ib2 = entry->ipv4.ib2;
		}

		*((__be32 *)h_source) = htonl(l2->src_mac_hi);
		*((__be16 *)&h_source[4]) = htons(l2->src_mac_lo);
		*((__be32 *)h_dest) = htonl(l2->dest_mac_hi);
		*((__be16 *)&h_dest[4]) = htons(l2->dest_mac_lo);

		seq_printf(m, " eth=%pM->%pM etype=%04x"
			      " vlan=%d,%d ib1=%08x ib2=%08x"
			      " packets=%llu bytes=%llu\n",
			   h_source, h_dest, ntohs(l2->etype),
			   l2->vlan1, l2->vlan2, entry->ib1, ib2,
			   acct ? acct->packets : 0, acct ? acct->bytes : 0);
	}

	return 0;
}

static int
mtk_ppe_debugfs_foe_all_show(struct seq_file *m, void *private)
{
	return mtk_ppe_debugfs_foe_show(m, private, false);
}
DEFINE_SHOW_ATTRIBUTE(mtk_ppe_debugfs_foe_all);

static int
mtk_ppe_debugfs_foe_bind_show(struct seq_file *m, void *private)
{
	return mtk_ppe_debugfs_foe_show(m, private, true);
}
DEFINE_SHOW_ATTRIBUTE(mtk_ppe_debugfs_foe_bind);

int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index)
{
	struct dentry *root;

	snprintf(ppe->dirname, sizeof(ppe->dirname), "ppe%d", index);

	root = debugfs_create_dir(ppe->dirname, NULL);
	debugfs_create_file("entries", S_IRUGO, root, ppe, &mtk_ppe_debugfs_foe_all_fops);
	debugfs_create_file("bind", S_IRUGO, root, ppe, &mtk_ppe_debugfs_foe_bind_fops);

	return 0;
}