OSDN Git Service

ef15d35f1574bfd9bd72da4b5fb7191949752adf
[uclinux-h8/linux.git] / net / dsa / master.c
1 /*
2  * Handling of a master device, switching frames via its switch fabric CPU port
3  *
4  * Copyright (c) 2017 Savoir-faire Linux Inc.
5  *      Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include "dsa_priv.h"
14
15 static void dsa_master_get_ethtool_stats(struct net_device *dev,
16                                          struct ethtool_stats *stats,
17                                          uint64_t *data)
18 {
19         struct dsa_switch_tree *dst = dev->dsa_ptr;
20         struct dsa_port *cpu_dp = dst->cpu_dp;
21         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
22         struct dsa_switch *ds = cpu_dp->ds;
23         int port = cpu_dp->index;
24         int count = 0;
25
26         if (ops && ops->get_sset_count && ops->get_ethtool_stats) {
27                 count = ops->get_sset_count(dev, ETH_SS_STATS);
28                 ops->get_ethtool_stats(dev, stats, data);
29         }
30
31         if (ds->ops->get_ethtool_stats)
32                 ds->ops->get_ethtool_stats(ds, port, data + count);
33 }
34
35 static int dsa_master_get_sset_count(struct net_device *dev, int sset)
36 {
37         struct dsa_switch_tree *dst = dev->dsa_ptr;
38         struct dsa_port *cpu_dp = dst->cpu_dp;
39         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
40         struct dsa_switch *ds = cpu_dp->ds;
41         int count = 0;
42
43         if (ops && ops->get_sset_count)
44                 count += ops->get_sset_count(dev, sset);
45
46         if (sset == ETH_SS_STATS && ds->ops->get_sset_count)
47                 count += ds->ops->get_sset_count(ds);
48
49         return count;
50 }
51
52 static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset,
53                                    uint8_t *data)
54 {
55         struct dsa_switch_tree *dst = dev->dsa_ptr;
56         struct dsa_port *cpu_dp = dst->cpu_dp;
57         const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
58         struct dsa_switch *ds = cpu_dp->ds;
59         int port = cpu_dp->index;
60         int len = ETH_GSTRING_LEN;
61         int mcount = 0, count;
62         unsigned int i;
63         uint8_t pfx[4];
64         uint8_t *ndata;
65
66         snprintf(pfx, sizeof(pfx), "p%.2d", port);
67         /* We do not want to be NULL-terminated, since this is a prefix */
68         pfx[sizeof(pfx) - 1] = '_';
69
70         if (ops && ops->get_sset_count && ops->get_strings) {
71                 mcount = ops->get_sset_count(dev, ETH_SS_STATS);
72                 ops->get_strings(dev, stringset, data);
73         }
74
75         if (stringset == ETH_SS_STATS && ds->ops->get_strings) {
76                 ndata = data + mcount * len;
77                 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
78                  * the output after to prepend our CPU port prefix we
79                  * constructed earlier
80                  */
81                 ds->ops->get_strings(ds, port, ndata);
82                 count = ds->ops->get_sset_count(ds);
83                 for (i = 0; i < count; i++) {
84                         memmove(ndata + (i * len + sizeof(pfx)),
85                                 ndata + i * len, len - sizeof(pfx));
86                         memcpy(ndata + i * len, pfx, sizeof(pfx));
87                 }
88         }
89 }
90
91 int dsa_master_ethtool_setup(struct net_device *dev)
92 {
93         struct dsa_switch_tree *dst = dev->dsa_ptr;
94         struct dsa_port *cpu_dp = dst->cpu_dp;
95         struct dsa_switch *ds = cpu_dp->ds;
96         struct ethtool_ops *ops;
97
98         ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
99         if (!ops)
100                 return -ENOMEM;
101
102         cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
103         if (cpu_dp->orig_ethtool_ops)
104                 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
105
106         ops->get_sset_count = dsa_master_get_sset_count;
107         ops->get_ethtool_stats = dsa_master_get_ethtool_stats;
108         ops->get_strings = dsa_master_get_strings;
109
110         dev->ethtool_ops = ops;
111
112         return 0;
113 }
114
115 void dsa_master_ethtool_restore(struct net_device *dev)
116 {
117         struct dsa_switch_tree *dst = dev->dsa_ptr;
118         struct dsa_port *cpu_dp = dst->cpu_dp;
119
120         dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
121         cpu_dp->orig_ethtool_ops = NULL;
122 }