]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
f2f23566 VD |
2 | /* |
3 | * Handling of a master device, switching frames via its switch fabric CPU port | |
4 | * | |
5 | * Copyright (c) 2017 Savoir-faire Linux Inc. | |
6 | * Vivien Didelot <[email protected]> | |
f2f23566 VD |
7 | */ |
8 | ||
9 | #include "dsa_priv.h" | |
10 | ||
48e23311 VD |
11 | static int dsa_master_get_regs_len(struct net_device *dev) |
12 | { | |
13 | struct dsa_port *cpu_dp = dev->dsa_ptr; | |
14 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; | |
15 | struct dsa_switch *ds = cpu_dp->ds; | |
16 | int port = cpu_dp->index; | |
17 | int ret = 0; | |
18 | int len; | |
19 | ||
20 | if (ops->get_regs_len) { | |
21 | len = ops->get_regs_len(dev); | |
22 | if (len < 0) | |
23 | return len; | |
24 | ret += len; | |
25 | } | |
26 | ||
27 | ret += sizeof(struct ethtool_drvinfo); | |
28 | ret += sizeof(struct ethtool_regs); | |
29 | ||
30 | if (ds->ops->get_regs_len) { | |
31 | len = ds->ops->get_regs_len(ds, port); | |
32 | if (len < 0) | |
33 | return len; | |
34 | ret += len; | |
35 | } | |
36 | ||
37 | return ret; | |
38 | } | |
39 | ||
40 | static void dsa_master_get_regs(struct net_device *dev, | |
41 | struct ethtool_regs *regs, void *data) | |
42 | { | |
43 | struct dsa_port *cpu_dp = dev->dsa_ptr; | |
44 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; | |
45 | struct dsa_switch *ds = cpu_dp->ds; | |
46 | struct ethtool_drvinfo *cpu_info; | |
47 | struct ethtool_regs *cpu_regs; | |
48 | int port = cpu_dp->index; | |
49 | int len; | |
50 | ||
51 | if (ops->get_regs_len && ops->get_regs) { | |
52 | len = ops->get_regs_len(dev); | |
53 | if (len < 0) | |
54 | return; | |
55 | regs->len = len; | |
56 | ops->get_regs(dev, regs, data); | |
57 | data += regs->len; | |
58 | } | |
59 | ||
60 | cpu_info = (struct ethtool_drvinfo *)data; | |
61 | strlcpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver)); | |
62 | data += sizeof(*cpu_info); | |
63 | cpu_regs = (struct ethtool_regs *)data; | |
64 | data += sizeof(*cpu_regs); | |
65 | ||
66 | if (ds->ops->get_regs_len && ds->ops->get_regs) { | |
67 | len = ds->ops->get_regs_len(ds, port); | |
68 | if (len < 0) | |
69 | return; | |
70 | cpu_regs->len = len; | |
71 | ds->ops->get_regs(ds, port, cpu_regs, data); | |
72 | } | |
73 | } | |
74 | ||
f2f23566 VD |
75 | static void dsa_master_get_ethtool_stats(struct net_device *dev, |
76 | struct ethtool_stats *stats, | |
77 | uint64_t *data) | |
78 | { | |
2f657a60 | 79 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
7ec764ee VD |
80 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; |
81 | struct dsa_switch *ds = cpu_dp->ds; | |
82 | int port = cpu_dp->index; | |
f2f23566 VD |
83 | int count = 0; |
84 | ||
1d1e79f1 | 85 | if (ops->get_sset_count && ops->get_ethtool_stats) { |
f2f23566 VD |
86 | count = ops->get_sset_count(dev, ETH_SS_STATS); |
87 | ops->get_ethtool_stats(dev, stats, data); | |
88 | } | |
89 | ||
90 | if (ds->ops->get_ethtool_stats) | |
7ec764ee | 91 | ds->ops->get_ethtool_stats(ds, port, data + count); |
f2f23566 VD |
92 | } |
93 | ||
cf963573 FF |
94 | static void dsa_master_get_ethtool_phy_stats(struct net_device *dev, |
95 | struct ethtool_stats *stats, | |
96 | uint64_t *data) | |
97 | { | |
98 | struct dsa_port *cpu_dp = dev->dsa_ptr; | |
99 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; | |
100 | struct dsa_switch *ds = cpu_dp->ds; | |
101 | int port = cpu_dp->index; | |
102 | int count = 0; | |
103 | ||
104 | if (dev->phydev && !ops->get_ethtool_phy_stats) { | |
105 | count = phy_ethtool_get_sset_count(dev->phydev); | |
106 | if (count >= 0) | |
107 | phy_ethtool_get_stats(dev->phydev, stats, data); | |
108 | } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) { | |
109 | count = ops->get_sset_count(dev, ETH_SS_PHY_STATS); | |
110 | ops->get_ethtool_phy_stats(dev, stats, data); | |
111 | } | |
112 | ||
113 | if (count < 0) | |
114 | count = 0; | |
115 | ||
116 | if (ds->ops->get_ethtool_phy_stats) | |
117 | ds->ops->get_ethtool_phy_stats(ds, port, data + count); | |
118 | } | |
119 | ||
f2f23566 VD |
120 | static int dsa_master_get_sset_count(struct net_device *dev, int sset) |
121 | { | |
2f657a60 | 122 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
7ec764ee VD |
123 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; |
124 | struct dsa_switch *ds = cpu_dp->ds; | |
f2f23566 VD |
125 | int count = 0; |
126 | ||
cf963573 FF |
127 | if (sset == ETH_SS_PHY_STATS && dev->phydev && |
128 | !ops->get_ethtool_phy_stats) | |
129 | count = phy_ethtool_get_sset_count(dev->phydev); | |
130 | else if (ops->get_sset_count) | |
89f09048 | 131 | count = ops->get_sset_count(dev, sset); |
cf963573 FF |
132 | |
133 | if (count < 0) | |
134 | count = 0; | |
f2f23566 | 135 | |
89f09048 FF |
136 | if (ds->ops->get_sset_count) |
137 | count += ds->ops->get_sset_count(ds, cpu_dp->index, sset); | |
f2f23566 VD |
138 | |
139 | return count; | |
140 | } | |
141 | ||
142 | static void dsa_master_get_strings(struct net_device *dev, uint32_t stringset, | |
143 | uint8_t *data) | |
144 | { | |
2f657a60 | 145 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
7ec764ee VD |
146 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; |
147 | struct dsa_switch *ds = cpu_dp->ds; | |
148 | int port = cpu_dp->index; | |
f2f23566 VD |
149 | int len = ETH_GSTRING_LEN; |
150 | int mcount = 0, count; | |
151 | unsigned int i; | |
152 | uint8_t pfx[4]; | |
153 | uint8_t *ndata; | |
154 | ||
7ec764ee | 155 | snprintf(pfx, sizeof(pfx), "p%.2d", port); |
f2f23566 VD |
156 | /* We do not want to be NULL-terminated, since this is a prefix */ |
157 | pfx[sizeof(pfx) - 1] = '_'; | |
158 | ||
cf963573 FF |
159 | if (stringset == ETH_SS_PHY_STATS && dev->phydev && |
160 | !ops->get_ethtool_phy_stats) { | |
161 | mcount = phy_ethtool_get_sset_count(dev->phydev); | |
162 | if (mcount < 0) | |
163 | mcount = 0; | |
164 | else | |
165 | phy_ethtool_get_strings(dev->phydev, data); | |
166 | } else if (ops->get_sset_count && ops->get_strings) { | |
89f09048 FF |
167 | mcount = ops->get_sset_count(dev, stringset); |
168 | if (mcount < 0) | |
169 | mcount = 0; | |
f2f23566 VD |
170 | ops->get_strings(dev, stringset, data); |
171 | } | |
172 | ||
89f09048 | 173 | if (ds->ops->get_strings) { |
f2f23566 VD |
174 | ndata = data + mcount * len; |
175 | /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle | |
176 | * the output after to prepend our CPU port prefix we | |
177 | * constructed earlier | |
178 | */ | |
89f09048 FF |
179 | ds->ops->get_strings(ds, port, stringset, ndata); |
180 | count = ds->ops->get_sset_count(ds, port, stringset); | |
f2f23566 VD |
181 | for (i = 0; i < count; i++) { |
182 | memmove(ndata + (i * len + sizeof(pfx)), | |
183 | ndata + i * len, len - sizeof(pfx)); | |
184 | memcpy(ndata + i * len, pfx, sizeof(pfx)); | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
f685e609 VO |
189 | static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
190 | { | |
191 | struct dsa_port *cpu_dp = dev->dsa_ptr; | |
192 | struct dsa_switch *ds = cpu_dp->ds; | |
193 | struct dsa_switch_tree *dst; | |
194 | int err = -EOPNOTSUPP; | |
195 | struct dsa_port *dp; | |
196 | ||
197 | dst = ds->dst; | |
198 | ||
199 | switch (cmd) { | |
200 | case SIOCGHWTSTAMP: | |
201 | case SIOCSHWTSTAMP: | |
202 | /* Deny PTP operations on master if there is at least one | |
203 | * switch in the tree that is PTP capable. | |
204 | */ | |
205 | list_for_each_entry(dp, &dst->ports, list) | |
206 | if (dp->ds->ops->port_hwtstamp_get || | |
207 | dp->ds->ops->port_hwtstamp_set) | |
208 | return -EBUSY; | |
209 | break; | |
210 | } | |
211 | ||
9c0c7014 FF |
212 | if (dev->netdev_ops->ndo_do_ioctl) |
213 | err = dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd); | |
f685e609 VO |
214 | |
215 | return err; | |
216 | } | |
217 | ||
9c0c7014 FF |
218 | static const struct dsa_netdevice_ops dsa_netdev_ops = { |
219 | .ndo_do_ioctl = dsa_master_ioctl, | |
9c0c7014 FF |
220 | }; |
221 | ||
17a22fcf | 222 | static int dsa_master_ethtool_setup(struct net_device *dev) |
f2f23566 | 223 | { |
2f657a60 | 224 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
7ec764ee | 225 | struct dsa_switch *ds = cpu_dp->ds; |
f2f23566 VD |
226 | struct ethtool_ops *ops; |
227 | ||
228 | ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); | |
229 | if (!ops) | |
230 | return -ENOMEM; | |
231 | ||
7ec764ee VD |
232 | cpu_dp->orig_ethtool_ops = dev->ethtool_ops; |
233 | if (cpu_dp->orig_ethtool_ops) | |
234 | memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); | |
f2f23566 | 235 | |
48e23311 VD |
236 | ops->get_regs_len = dsa_master_get_regs_len; |
237 | ops->get_regs = dsa_master_get_regs; | |
f2f23566 VD |
238 | ops->get_sset_count = dsa_master_get_sset_count; |
239 | ops->get_ethtool_stats = dsa_master_get_ethtool_stats; | |
240 | ops->get_strings = dsa_master_get_strings; | |
cf963573 | 241 | ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats; |
f2f23566 VD |
242 | |
243 | dev->ethtool_ops = ops; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
17a22fcf | 248 | static void dsa_master_ethtool_teardown(struct net_device *dev) |
f2f23566 | 249 | { |
2f657a60 | 250 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
f2f23566 | 251 | |
7ec764ee VD |
252 | dev->ethtool_ops = cpu_dp->orig_ethtool_ops; |
253 | cpu_dp->orig_ethtool_ops = NULL; | |
f2f23566 | 254 | } |
17a22fcf | 255 | |
9c0c7014 FF |
256 | static void dsa_netdev_ops_set(struct net_device *dev, |
257 | const struct dsa_netdevice_ops *ops) | |
da7b9e9b | 258 | { |
9c0c7014 | 259 | dev->dsa_ptr->netdev_ops = ops; |
da7b9e9b FF |
260 | } |
261 | ||
a3d7e01d FF |
262 | static ssize_t tagging_show(struct device *d, struct device_attribute *attr, |
263 | char *buf) | |
264 | { | |
265 | struct net_device *dev = to_net_dev(d); | |
266 | struct dsa_port *cpu_dp = dev->dsa_ptr; | |
267 | ||
268 | return sprintf(buf, "%s\n", | |
269 | dsa_tag_protocol_to_str(cpu_dp->tag_ops)); | |
270 | } | |
271 | static DEVICE_ATTR_RO(tagging); | |
272 | ||
273 | static struct attribute *dsa_slave_attrs[] = { | |
274 | &dev_attr_tagging.attr, | |
275 | NULL | |
276 | }; | |
277 | ||
278 | static const struct attribute_group dsa_group = { | |
279 | .name = "dsa", | |
280 | .attrs = dsa_slave_attrs, | |
281 | }; | |
282 | ||
91ba4795 AL |
283 | static void dsa_master_reset_mtu(struct net_device *dev) |
284 | { | |
285 | int err; | |
286 | ||
287 | rtnl_lock(); | |
288 | err = dev_set_mtu(dev, ETH_DATA_LEN); | |
289 | if (err) | |
290 | netdev_dbg(dev, | |
291 | "Unable to reset MTU to exclude DSA overheads\n"); | |
292 | rtnl_unlock(); | |
293 | } | |
294 | ||
845e0ebb CW |
295 | static struct lock_class_key dsa_master_addr_list_lock_key; |
296 | ||
17a22fcf VD |
297 | int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp) |
298 | { | |
a3d7e01d FF |
299 | int ret; |
300 | ||
bfcb8132 VO |
301 | rtnl_lock(); |
302 | ret = dev_set_mtu(dev, ETH_DATA_LEN + cpu_dp->tag_ops->overhead); | |
303 | rtnl_unlock(); | |
304 | if (ret) | |
305 | netdev_warn(dev, "error %d setting MTU to include DSA overhead\n", | |
306 | ret); | |
dc0fe7d4 | 307 | |
17a22fcf VD |
308 | /* If we use a tagging format that doesn't have an ethertype |
309 | * field, make sure that all packets from this point on get | |
310 | * sent to the tag format's receive function. | |
311 | */ | |
312 | wmb(); | |
313 | ||
314 | dev->dsa_ptr = cpu_dp; | |
845e0ebb CW |
315 | lockdep_set_class(&dev->addr_list_lock, |
316 | &dsa_master_addr_list_lock_key); | |
a3d7e01d FF |
317 | ret = dsa_master_ethtool_setup(dev); |
318 | if (ret) | |
319 | return ret; | |
320 | ||
9c0c7014 | 321 | dsa_netdev_ops_set(dev, &dsa_netdev_ops); |
da7b9e9b | 322 | |
a3d7e01d FF |
323 | ret = sysfs_create_group(&dev->dev.kobj, &dsa_group); |
324 | if (ret) | |
da7b9e9b FF |
325 | goto out_err_ndo_teardown; |
326 | ||
327 | return ret; | |
a3d7e01d | 328 | |
da7b9e9b | 329 | out_err_ndo_teardown: |
9c0c7014 | 330 | dsa_netdev_ops_set(dev, NULL); |
da7b9e9b | 331 | dsa_master_ethtool_teardown(dev); |
a3d7e01d | 332 | return ret; |
17a22fcf VD |
333 | } |
334 | ||
335 | void dsa_master_teardown(struct net_device *dev) | |
336 | { | |
a3d7e01d | 337 | sysfs_remove_group(&dev->dev.kobj, &dsa_group); |
9c0c7014 | 338 | dsa_netdev_ops_set(dev, NULL); |
17a22fcf | 339 | dsa_master_ethtool_teardown(dev); |
91ba4795 | 340 | dsa_master_reset_mtu(dev); |
17a22fcf VD |
341 | |
342 | dev->dsa_ptr = NULL; | |
343 | ||
344 | /* If we used a tagging format that doesn't have an ethertype | |
345 | * field, make sure that all packets from this point get sent | |
346 | * without the tag and go through the regular receive path. | |
347 | */ | |
348 | wmb(); | |
349 | } |