David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 <vivien.didelot@savoirfairelinux.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "dsa_priv.h" |
| 10 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 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 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | static void dsa_master_get_ethtool_stats(struct net_device *dev, |
| 76 | struct ethtool_stats *stats, |
| 77 | uint64_t *data) |
| 78 | { |
| 79 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 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; |
| 83 | int count = 0; |
| 84 | |
| 85 | if (ops->get_sset_count && ops->get_ethtool_stats) { |
| 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) |
| 91 | ds->ops->get_ethtool_stats(ds, port, data + count); |
| 92 | } |
| 93 | |
| 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 | |
| 120 | static int dsa_master_get_sset_count(struct net_device *dev, int sset) |
| 121 | { |
| 122 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 123 | const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; |
| 124 | struct dsa_switch *ds = cpu_dp->ds; |
| 125 | int count = 0; |
| 126 | |
| 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) |
| 131 | count = ops->get_sset_count(dev, sset); |
| 132 | |
| 133 | if (count < 0) |
| 134 | count = 0; |
| 135 | |
| 136 | if (ds->ops->get_sset_count) |
| 137 | count += ds->ops->get_sset_count(ds, cpu_dp->index, sset); |
| 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 | { |
| 145 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 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; |
| 149 | int len = ETH_GSTRING_LEN; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 150 | int mcount = 0, count, i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | uint8_t pfx[4]; |
| 152 | uint8_t *ndata; |
| 153 | |
| 154 | snprintf(pfx, sizeof(pfx), "p%.2d", port); |
| 155 | /* We do not want to be NULL-terminated, since this is a prefix */ |
| 156 | pfx[sizeof(pfx) - 1] = '_'; |
| 157 | |
| 158 | if (stringset == ETH_SS_PHY_STATS && dev->phydev && |
| 159 | !ops->get_ethtool_phy_stats) { |
| 160 | mcount = phy_ethtool_get_sset_count(dev->phydev); |
| 161 | if (mcount < 0) |
| 162 | mcount = 0; |
| 163 | else |
| 164 | phy_ethtool_get_strings(dev->phydev, data); |
| 165 | } else if (ops->get_sset_count && ops->get_strings) { |
| 166 | mcount = ops->get_sset_count(dev, stringset); |
| 167 | if (mcount < 0) |
| 168 | mcount = 0; |
| 169 | ops->get_strings(dev, stringset, data); |
| 170 | } |
| 171 | |
| 172 | if (ds->ops->get_strings) { |
| 173 | ndata = data + mcount * len; |
| 174 | /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle |
| 175 | * the output after to prepend our CPU port prefix we |
| 176 | * constructed earlier |
| 177 | */ |
| 178 | ds->ops->get_strings(ds, port, stringset, ndata); |
| 179 | count = ds->ops->get_sset_count(ds, port, stringset); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 180 | if (count < 0) |
| 181 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 182 | for (i = 0; i < count; i++) { |
| 183 | memmove(ndata + (i * len + sizeof(pfx)), |
| 184 | ndata + i * len, len - sizeof(pfx)); |
| 185 | memcpy(ndata + i * len, pfx, sizeof(pfx)); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 190 | static int dsa_master_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 191 | { |
| 192 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 193 | struct dsa_switch *ds = cpu_dp->ds; |
| 194 | struct dsa_switch_tree *dst; |
| 195 | int err = -EOPNOTSUPP; |
| 196 | struct dsa_port *dp; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 197 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 198 | dst = ds->dst; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 199 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 200 | switch (cmd) { |
| 201 | case SIOCGHWTSTAMP: |
| 202 | case SIOCSHWTSTAMP: |
| 203 | /* Deny PTP operations on master if there is at least one |
| 204 | * switch in the tree that is PTP capable. |
| 205 | */ |
| 206 | list_for_each_entry(dp, &dst->ports, list) |
| 207 | if (dp->ds->ops->port_hwtstamp_get || |
| 208 | dp->ds->ops->port_hwtstamp_set) |
| 209 | return -EBUSY; |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | if (dev->netdev_ops->ndo_do_ioctl) |
| 214 | err = dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd); |
| 215 | |
| 216 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 219 | static const struct dsa_netdevice_ops dsa_netdev_ops = { |
| 220 | .ndo_do_ioctl = dsa_master_ioctl, |
| 221 | }; |
| 222 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 223 | static int dsa_master_ethtool_setup(struct net_device *dev) |
| 224 | { |
| 225 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 226 | struct dsa_switch *ds = cpu_dp->ds; |
| 227 | struct ethtool_ops *ops; |
| 228 | |
| 229 | ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); |
| 230 | if (!ops) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | cpu_dp->orig_ethtool_ops = dev->ethtool_ops; |
| 234 | if (cpu_dp->orig_ethtool_ops) |
| 235 | memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); |
| 236 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 237 | ops->get_regs_len = dsa_master_get_regs_len; |
| 238 | ops->get_regs = dsa_master_get_regs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 239 | ops->get_sset_count = dsa_master_get_sset_count; |
| 240 | ops->get_ethtool_stats = dsa_master_get_ethtool_stats; |
| 241 | ops->get_strings = dsa_master_get_strings; |
| 242 | ops->get_ethtool_phy_stats = dsa_master_get_ethtool_phy_stats; |
| 243 | |
| 244 | dev->ethtool_ops = ops; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static void dsa_master_ethtool_teardown(struct net_device *dev) |
| 250 | { |
| 251 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 252 | |
| 253 | dev->ethtool_ops = cpu_dp->orig_ethtool_ops; |
| 254 | cpu_dp->orig_ethtool_ops = NULL; |
| 255 | } |
| 256 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 257 | static void dsa_netdev_ops_set(struct net_device *dev, |
| 258 | const struct dsa_netdevice_ops *ops) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 259 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 260 | dev->dsa_ptr->netdev_ops = ops; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 263 | static void dsa_master_set_promiscuity(struct net_device *dev, int inc) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 264 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 265 | const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 266 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 267 | if (!ops->promisc_on_master) |
| 268 | return; |
| 269 | |
| 270 | rtnl_lock(); |
| 271 | dev_set_promiscuity(dev, inc); |
| 272 | rtnl_unlock(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static ssize_t tagging_show(struct device *d, struct device_attribute *attr, |
| 276 | char *buf) |
| 277 | { |
| 278 | struct net_device *dev = to_net_dev(d); |
| 279 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
| 280 | |
| 281 | return sprintf(buf, "%s\n", |
| 282 | dsa_tag_protocol_to_str(cpu_dp->tag_ops)); |
| 283 | } |
| 284 | static DEVICE_ATTR_RO(tagging); |
| 285 | |
| 286 | static struct attribute *dsa_slave_attrs[] = { |
| 287 | &dev_attr_tagging.attr, |
| 288 | NULL |
| 289 | }; |
| 290 | |
| 291 | static const struct attribute_group dsa_group = { |
| 292 | .name = "dsa", |
| 293 | .attrs = dsa_slave_attrs, |
| 294 | }; |
| 295 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 296 | static void dsa_master_reset_mtu(struct net_device *dev) |
| 297 | { |
| 298 | int err; |
| 299 | |
| 300 | rtnl_lock(); |
| 301 | err = dev_set_mtu(dev, ETH_DATA_LEN); |
| 302 | if (err) |
| 303 | netdev_dbg(dev, |
| 304 | "Unable to reset MTU to exclude DSA overheads\n"); |
| 305 | rtnl_unlock(); |
| 306 | } |
| 307 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 308 | static struct lock_class_key dsa_master_addr_list_lock_key; |
| 309 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 310 | int dsa_master_setup(struct net_device *dev, struct dsa_port *cpu_dp) |
| 311 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 312 | struct dsa_switch *ds = cpu_dp->ds; |
| 313 | struct device_link *consumer_link; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 314 | int ret; |
| 315 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 316 | /* The DSA master must use SET_NETDEV_DEV for this to work. */ |
| 317 | consumer_link = device_link_add(ds->dev, dev->dev.parent, |
| 318 | DL_FLAG_AUTOREMOVE_CONSUMER); |
| 319 | if (!consumer_link) |
| 320 | netdev_err(dev, |
| 321 | "Failed to create a device link to DSA switch %s\n", |
| 322 | dev_name(ds->dev)); |
| 323 | |
| 324 | rtnl_lock(); |
| 325 | ret = dev_set_mtu(dev, ETH_DATA_LEN + cpu_dp->tag_ops->overhead); |
| 326 | rtnl_unlock(); |
| 327 | if (ret) |
| 328 | netdev_warn(dev, "error %d setting MTU to include DSA overhead\n", |
| 329 | ret); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 330 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 331 | /* If we use a tagging format that doesn't have an ethertype |
| 332 | * field, make sure that all packets from this point on get |
| 333 | * sent to the tag format's receive function. |
| 334 | */ |
| 335 | wmb(); |
| 336 | |
| 337 | dev->dsa_ptr = cpu_dp; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 338 | lockdep_set_class(&dev->addr_list_lock, |
| 339 | &dsa_master_addr_list_lock_key); |
| 340 | |
| 341 | dsa_master_set_promiscuity(dev, 1); |
| 342 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 343 | ret = dsa_master_ethtool_setup(dev); |
| 344 | if (ret) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 345 | goto out_err_reset_promisc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 346 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 347 | dsa_netdev_ops_set(dev, &dsa_netdev_ops); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 348 | |
| 349 | ret = sysfs_create_group(&dev->dev.kobj, &dsa_group); |
| 350 | if (ret) |
| 351 | goto out_err_ndo_teardown; |
| 352 | |
| 353 | return ret; |
| 354 | |
| 355 | out_err_ndo_teardown: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 356 | dsa_netdev_ops_set(dev, NULL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 357 | dsa_master_ethtool_teardown(dev); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 358 | out_err_reset_promisc: |
| 359 | dsa_master_set_promiscuity(dev, -1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 360 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void dsa_master_teardown(struct net_device *dev) |
| 364 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 365 | sysfs_remove_group(&dev->dev.kobj, &dsa_group); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 366 | dsa_netdev_ops_set(dev, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 367 | dsa_master_ethtool_teardown(dev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 368 | dsa_master_reset_mtu(dev); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 369 | dsa_master_set_promiscuity(dev, -1); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 370 | |
| 371 | dev->dsa_ptr = NULL; |
| 372 | |
| 373 | /* If we used a tagging format that doesn't have an ethertype |
| 374 | * field, make sure that all packets from this point get sent |
| 375 | * without the tag and go through the regular receive path. |
| 376 | */ |
| 377 | wmb(); |
| 378 | } |