2938119e891e751d0622b5cb933b6bdbb2e0a977
[openwrt/.git] / target / linux / ramips / files / drivers / net / ethernet / ralink / mtk_debugfs.c
1 /*   This program is free software; you can redistribute it and/or modify
2  *   it under the terms of the GNU General Public License as published by
3  *   the Free Software Foundation; version 2 of the License
4  *
5  *   This program is distributed in the hope that it will be useful,
6  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
7  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8  *   GNU General Public License for more details.
9  *
10  *   Copyright (C) 2014-2016 Sean Wang <sean.wang@mediatek.com>
11  *   Copyright (C) 2016-2017 John Crispin <blogic@openwrt.org>
12  */
13
14 #include "mtk_offload.h"
15
16 static const char *mtk_foe_entry_state_str[] = {
17         "INVALID",
18         "UNBIND",
19         "BIND",
20         "FIN"
21 };
22
23 static const char *mtk_foe_packet_type_str[] = {
24         "IPV4_HNAPT",
25         "IPV4_HNAT",
26         "IPV6_1T_ROUTE",
27         "IPV4_DSLITE",
28         "IPV6_3T_ROUTE",
29         "IPV6_5T_ROUTE",
30         "IPV6_6RD",
31 };
32
33 #define IPV4_HNAPT                      0
34 #define IPV4_HNAT                       1
35 #define IS_IPV4_HNAPT(x)        (((x)->bfib1.pkt_type == IPV4_HNAPT) ? 1: 0)
36 struct mtk_eth *_eth;
37 #define es(entry)               (mtk_foe_entry_state_str[entry->bfib1.state])
38 //#define ei(entry, end)                (MTK_PPE_TBL_SZ - (int)(end - entry))
39 #define ei(entry, end)          (MTK_PPE_ENTRY_CNT - (int)(end - entry))
40 #define pt(entry)               (mtk_foe_packet_type_str[entry->ipv4_hnapt.bfib1.pkt_type])
41
42 static int mtk_ppe_debugfs_foe_show(struct seq_file *m, void *private)
43 {
44         struct mtk_eth *eth = _eth;
45         struct mtk_foe_entry *entry, *end;
46         int i = 0;
47
48         entry = eth->foe_table;
49         end = eth->foe_table + MTK_PPE_ENTRY_CNT;
50
51         while (entry < end) {
52                 if (IS_IPV4_HNAPT(entry)) {
53                         __be32 saddr = htonl(entry->ipv4_hnapt.sip);
54                         __be32 daddr = htonl(entry->ipv4_hnapt.dip);
55                         __be32 nsaddr = htonl(entry->ipv4_hnapt.new_sip);
56                         __be32 ndaddr = htonl(entry->ipv4_hnapt.new_dip);
57                         unsigned char h_dest[ETH_ALEN];
58                         unsigned char h_source[ETH_ALEN];
59
60                         *((u32*) h_source) = swab32(entry->ipv4_hnapt.smac_hi);
61                         *((u16*) &h_source[4]) = swab16(entry->ipv4_hnapt.smac_lo);
62                         *((u32*) h_dest) = swab32(entry->ipv4_hnapt.dmac_hi);
63                         *((u16*) &h_dest[4]) = swab16(entry->ipv4_hnapt.dmac_lo);
64                         seq_printf(m,
65                                    "(%x)0x%05x|state=%s|type=%s|"
66                                    "%pI4:%d->%pI4:%d=>%pI4:%d->%pI4:%d|%pM=>%pM|"
67                                    "etype=0x%04x|info1=0x%x|info2=0x%x|"
68                                    "vlan1=%d|vlan2=%d\n",
69                                    i,
70                                    ei(entry, end), es(entry), pt(entry),
71                                    &saddr, entry->ipv4_hnapt.sport,
72                                    &daddr, entry->ipv4_hnapt.dport,
73                                    &nsaddr, entry->ipv4_hnapt.new_sport,
74                                    &ndaddr, entry->ipv4_hnapt.new_dport, h_source,
75                                    h_dest, ntohs(entry->ipv4_hnapt.etype),
76                                    entry->ipv4_hnapt.info_blk1,
77                                    entry->ipv4_hnapt.info_blk2,
78                                    entry->ipv4_hnapt.vlan1,
79                                    entry->ipv4_hnapt.vlan2);
80                 } else
81                         seq_printf(m, "0x%05x state=%s\n",
82                                    ei(entry, end), es(entry));
83                 entry++;
84                 i++;
85         }
86
87         return 0;
88 }
89
90 static int mtk_ppe_debugfs_foe_open(struct inode *inode, struct file *file)
91 {
92         return single_open(file, mtk_ppe_debugfs_foe_show, file->private_data);
93 }
94
95 static const struct file_operations mtk_ppe_debugfs_foe_fops = {
96         .open = mtk_ppe_debugfs_foe_open,
97         .read = seq_read,
98         .llseek = seq_lseek,
99         .release = single_release,
100 };
101
102 int mtk_ppe_debugfs_init(struct mtk_eth *eth)
103 {
104         struct dentry *root;
105
106         _eth = eth;
107
108         root = debugfs_create_dir("mtk_ppe", NULL);
109         if (!root)
110                 return -ENOMEM;
111
112         debugfs_create_file("all_entry", S_IRUGO, root, eth, &mtk_ppe_debugfs_foe_fops);
113
114         return 0;
115 }