1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Handling of a conduit 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> 7 */ 8 9 #include <linux/ethtool.h> 10 #include <linux/netdevice.h> 11 #include <linux/netlink.h> 12 #include <net/dsa.h> 13 #include <net/netdev_lock.h> 14 15 #include "conduit.h" 16 #include "dsa.h" 17 #include "port.h" 18 #include "tag.h" 19 20 static int dsa_conduit_get_regs_len(struct net_device *dev) 21 { 22 struct dsa_port *cpu_dp = dev->dsa_ptr; 23 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 24 struct dsa_switch *ds = cpu_dp->ds; 25 int port = cpu_dp->index; 26 int ret = 0; 27 int len; 28 29 if (ops->get_regs_len) { 30 netdev_lock_ops(dev); 31 len = ops->get_regs_len(dev); 32 netdev_unlock_ops(dev); 33 if (len < 0) 34 return len; 35 ret += len; 36 } 37 38 ret += sizeof(struct ethtool_drvinfo); 39 ret += sizeof(struct ethtool_regs); 40 41 if (ds->ops->get_regs_len) { 42 len = ds->ops->get_regs_len(ds, port); 43 if (len < 0) 44 return len; 45 ret += len; 46 } 47 48 return ret; 49 } 50 51 static void dsa_conduit_get_regs(struct net_device *dev, 52 struct ethtool_regs *regs, void *data) 53 { 54 struct dsa_port *cpu_dp = dev->dsa_ptr; 55 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 56 struct dsa_switch *ds = cpu_dp->ds; 57 struct ethtool_drvinfo *cpu_info; 58 struct ethtool_regs *cpu_regs; 59 int port = cpu_dp->index; 60 int len; 61 62 if (ops->get_regs_len && ops->get_regs) { 63 netdev_lock_ops(dev); 64 len = ops->get_regs_len(dev); 65 if (len < 0) { 66 netdev_unlock_ops(dev); 67 return; 68 } 69 regs->len = len; 70 ops->get_regs(dev, regs, data); 71 netdev_unlock_ops(dev); 72 data += regs->len; 73 } 74 75 cpu_info = (struct ethtool_drvinfo *)data; 76 strscpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver)); 77 data += sizeof(*cpu_info); 78 cpu_regs = (struct ethtool_regs *)data; 79 data += sizeof(*cpu_regs); 80 81 if (ds->ops->get_regs_len && ds->ops->get_regs) { 82 len = ds->ops->get_regs_len(ds, port); 83 if (len < 0) 84 return; 85 cpu_regs->len = len; 86 ds->ops->get_regs(ds, port, cpu_regs, data); 87 } 88 } 89 90 static void dsa_conduit_get_ethtool_stats(struct net_device *dev, 91 struct ethtool_stats *stats, 92 uint64_t *data) 93 { 94 struct dsa_port *cpu_dp = dev->dsa_ptr; 95 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 96 struct dsa_switch *ds = cpu_dp->ds; 97 int port = cpu_dp->index; 98 int count = 0; 99 100 if (ops->get_sset_count && ops->get_ethtool_stats) { 101 netdev_lock_ops(dev); 102 count = ops->get_sset_count(dev, ETH_SS_STATS); 103 ops->get_ethtool_stats(dev, stats, data); 104 netdev_unlock_ops(dev); 105 } 106 107 if (ds->ops->get_ethtool_stats) 108 ds->ops->get_ethtool_stats(ds, port, data + count); 109 } 110 111 static void dsa_conduit_get_ethtool_phy_stats(struct net_device *dev, 112 struct ethtool_stats *stats, 113 uint64_t *data) 114 { 115 struct dsa_port *cpu_dp = dev->dsa_ptr; 116 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 117 struct dsa_switch *ds = cpu_dp->ds; 118 int port = cpu_dp->index; 119 int count = 0; 120 121 if (dev->phydev && !ops->get_ethtool_phy_stats) { 122 count = phy_ethtool_get_sset_count(dev->phydev); 123 if (count >= 0) 124 phy_ethtool_get_stats(dev->phydev, stats, data); 125 } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) { 126 netdev_lock_ops(dev); 127 count = ops->get_sset_count(dev, ETH_SS_PHY_STATS); 128 ops->get_ethtool_phy_stats(dev, stats, data); 129 netdev_unlock_ops(dev); 130 } 131 132 if (count < 0) 133 count = 0; 134 135 if (ds->ops->get_ethtool_phy_stats) 136 ds->ops->get_ethtool_phy_stats(ds, port, data + count); 137 } 138 139 static int dsa_conduit_get_sset_count(struct net_device *dev, int sset) 140 { 141 struct dsa_port *cpu_dp = dev->dsa_ptr; 142 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 143 struct dsa_switch *ds = cpu_dp->ds; 144 int count = 0; 145 146 netdev_lock_ops(dev); 147 if (sset == ETH_SS_PHY_STATS && dev->phydev && 148 !ops->get_ethtool_phy_stats) 149 count = phy_ethtool_get_sset_count(dev->phydev); 150 else if (ops->get_sset_count) 151 count = ops->get_sset_count(dev, sset); 152 netdev_unlock_ops(dev); 153 154 if (count < 0) 155 count = 0; 156 157 if (ds->ops->get_sset_count) 158 count += ds->ops->get_sset_count(ds, cpu_dp->index, sset); 159 160 return count; 161 } 162 163 static void dsa_conduit_get_strings(struct net_device *dev, uint32_t stringset, 164 uint8_t *data) 165 { 166 struct dsa_port *cpu_dp = dev->dsa_ptr; 167 const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops; 168 struct dsa_switch *ds = cpu_dp->ds; 169 int port = cpu_dp->index; 170 int len = ETH_GSTRING_LEN; 171 int mcount = 0, count, i; 172 uint8_t pfx[4]; 173 uint8_t *ndata; 174 175 snprintf(pfx, sizeof(pfx), "p%.2d", port); 176 /* We do not want to be NULL-terminated, since this is a prefix */ 177 pfx[sizeof(pfx) - 1] = '_'; 178 179 netdev_lock_ops(dev); 180 if (stringset == ETH_SS_PHY_STATS && dev->phydev && 181 !ops->get_ethtool_phy_stats) { 182 mcount = phy_ethtool_get_sset_count(dev->phydev); 183 if (mcount < 0) 184 mcount = 0; 185 else 186 phy_ethtool_get_strings(dev->phydev, data); 187 } else if (ops->get_sset_count && ops->get_strings) { 188 mcount = ops->get_sset_count(dev, stringset); 189 if (mcount < 0) 190 mcount = 0; 191 ops->get_strings(dev, stringset, data); 192 } 193 netdev_unlock_ops(dev); 194 195 if (ds->ops->get_strings) { 196 ndata = data + mcount * len; 197 /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle 198 * the output after to prepend our CPU port prefix we 199 * constructed earlier 200 */ 201 ds->ops->get_strings(ds, port, stringset, ndata); 202 count = ds->ops->get_sset_count(ds, port, stringset); 203 if (count < 0) 204 return; 205 for (i = 0; i < count; i++) { 206 memmove(ndata + (i * len + sizeof(pfx)), 207 ndata + i * len, len - sizeof(pfx)); 208 memcpy(ndata + i * len, pfx, sizeof(pfx)); 209 } 210 } 211 } 212 213 /* Deny PTP operations on conduit if there is at least one switch in the tree 214 * that is PTP capable. 215 */ 216 int __dsa_conduit_hwtstamp_validate(struct net_device *dev, 217 const struct kernel_hwtstamp_config *config, 218 struct netlink_ext_ack *extack) 219 { 220 struct dsa_port *cpu_dp = dev->dsa_ptr; 221 struct dsa_switch *ds = cpu_dp->ds; 222 struct dsa_switch_tree *dst; 223 struct dsa_port *dp; 224 225 dst = ds->dst; 226 227 list_for_each_entry(dp, &dst->ports, list) { 228 if (dsa_port_supports_hwtstamp(dp)) { 229 NL_SET_ERR_MSG(extack, 230 "HW timestamping not allowed on DSA conduit when switch supports the operation"); 231 return -EBUSY; 232 } 233 } 234 235 return 0; 236 } 237 238 static int dsa_conduit_ethtool_setup(struct net_device *dev) 239 { 240 struct dsa_port *cpu_dp = dev->dsa_ptr; 241 struct dsa_switch *ds = cpu_dp->ds; 242 struct ethtool_ops *ops; 243 244 if (netif_is_lag_master(dev)) 245 return 0; 246 247 ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL); 248 if (!ops) 249 return -ENOMEM; 250 251 cpu_dp->orig_ethtool_ops = dev->ethtool_ops; 252 if (cpu_dp->orig_ethtool_ops) 253 memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops)); 254 255 ops->get_regs_len = dsa_conduit_get_regs_len; 256 ops->get_regs = dsa_conduit_get_regs; 257 ops->get_sset_count = dsa_conduit_get_sset_count; 258 ops->get_ethtool_stats = dsa_conduit_get_ethtool_stats; 259 ops->get_strings = dsa_conduit_get_strings; 260 ops->get_ethtool_phy_stats = dsa_conduit_get_ethtool_phy_stats; 261 262 dev->ethtool_ops = ops; 263 264 return 0; 265 } 266 267 static void dsa_conduit_ethtool_teardown(struct net_device *dev) 268 { 269 struct dsa_port *cpu_dp = dev->dsa_ptr; 270 271 if (netif_is_lag_master(dev)) 272 return; 273 274 dev->ethtool_ops = cpu_dp->orig_ethtool_ops; 275 cpu_dp->orig_ethtool_ops = NULL; 276 } 277 278 /* Keep the conduit always promiscuous if the tagging protocol requires that 279 * (garbles MAC DA) or if it doesn't support unicast filtering, case in which 280 * it would revert to promiscuous mode as soon as we call dev_uc_add() on it 281 * anyway. 282 */ 283 static void dsa_conduit_set_promiscuity(struct net_device *dev, int inc) 284 { 285 const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops; 286 287 if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_conduit) 288 return; 289 290 ASSERT_RTNL(); 291 292 dev_set_promiscuity(dev, inc); 293 } 294 295 static ssize_t tagging_show(struct device *d, struct device_attribute *attr, 296 char *buf) 297 { 298 struct net_device *dev = to_net_dev(d); 299 struct dsa_port *cpu_dp = dev->dsa_ptr; 300 301 return sysfs_emit(buf, "%s\n", 302 dsa_tag_protocol_to_str(cpu_dp->tag_ops)); 303 } 304 305 static ssize_t tagging_store(struct device *d, struct device_attribute *attr, 306 const char *buf, size_t count) 307 { 308 const struct dsa_device_ops *new_tag_ops, *old_tag_ops; 309 const char *end = strchrnul(buf, '\n'), *name; 310 struct net_device *dev = to_net_dev(d); 311 struct dsa_port *cpu_dp = dev->dsa_ptr; 312 size_t len = end - buf; 313 int err; 314 315 /* Empty string passed */ 316 if (!len) 317 return -ENOPROTOOPT; 318 319 name = kstrndup(buf, len, GFP_KERNEL); 320 if (!name) 321 return -ENOMEM; 322 323 old_tag_ops = cpu_dp->tag_ops; 324 new_tag_ops = dsa_tag_driver_get_by_name(name); 325 kfree(name); 326 /* Bad tagger name? */ 327 if (IS_ERR(new_tag_ops)) 328 return PTR_ERR(new_tag_ops); 329 330 if (new_tag_ops == old_tag_ops) 331 /* Drop the temporarily held duplicate reference, since 332 * the DSA switch tree uses this tagger. 333 */ 334 goto out; 335 336 err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops, 337 old_tag_ops); 338 if (err) { 339 /* On failure the old tagger is restored, so we don't need the 340 * driver for the new one. 341 */ 342 dsa_tag_driver_put(new_tag_ops); 343 return err; 344 } 345 346 /* On success we no longer need the module for the old tagging protocol 347 */ 348 out: 349 dsa_tag_driver_put(old_tag_ops); 350 return count; 351 } 352 static DEVICE_ATTR_RW(tagging); 353 354 static struct attribute *dsa_user_attrs[] = { 355 &dev_attr_tagging.attr, 356 NULL 357 }; 358 359 static const struct attribute_group dsa_group = { 360 .name = "dsa", 361 .attrs = dsa_user_attrs, 362 }; 363 364 static void dsa_conduit_reset_mtu(struct net_device *dev) 365 { 366 int err; 367 368 err = dev_set_mtu(dev, ETH_DATA_LEN); 369 if (err) 370 netdev_dbg(dev, 371 "Unable to reset MTU to exclude DSA overheads\n"); 372 } 373 374 int dsa_conduit_setup(struct net_device *dev, struct dsa_port *cpu_dp) 375 { 376 const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops; 377 struct dsa_switch *ds = cpu_dp->ds; 378 struct device_link *consumer_link; 379 int mtu, ret; 380 381 mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops); 382 383 /* The DSA conduit must use SET_NETDEV_DEV for this to work. */ 384 if (!netif_is_lag_master(dev)) { 385 consumer_link = device_link_add(ds->dev, dev->dev.parent, 386 DL_FLAG_AUTOREMOVE_CONSUMER); 387 if (!consumer_link) 388 netdev_err(dev, 389 "Failed to create a device link to DSA switch %s\n", 390 dev_name(ds->dev)); 391 } 392 393 /* The switch driver may not implement ->port_change_mtu(), case in 394 * which dsa_user_change_mtu() will not update the conduit MTU either, 395 * so we need to do that here. 396 */ 397 ret = dev_set_mtu(dev, mtu); 398 if (ret) 399 netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n", 400 ret, mtu); 401 402 /* If we use a tagging format that doesn't have an ethertype 403 * field, make sure that all packets from this point on get 404 * sent to the tag format's receive function. 405 */ 406 wmb(); 407 408 dev->dsa_ptr = cpu_dp; 409 410 dsa_conduit_set_promiscuity(dev, 1); 411 412 ret = dsa_conduit_ethtool_setup(dev); 413 if (ret) 414 goto out_err_reset_promisc; 415 416 ret = sysfs_create_group(&dev->dev.kobj, &dsa_group); 417 if (ret) 418 goto out_err_ethtool_teardown; 419 420 return ret; 421 422 out_err_ethtool_teardown: 423 dsa_conduit_ethtool_teardown(dev); 424 out_err_reset_promisc: 425 dsa_conduit_set_promiscuity(dev, -1); 426 return ret; 427 } 428 429 void dsa_conduit_teardown(struct net_device *dev) 430 { 431 sysfs_remove_group(&dev->dev.kobj, &dsa_group); 432 dsa_conduit_ethtool_teardown(dev); 433 dsa_conduit_reset_mtu(dev); 434 dsa_conduit_set_promiscuity(dev, -1); 435 436 dev->dsa_ptr = NULL; 437 438 /* If we used a tagging format that doesn't have an ethertype 439 * field, make sure that all packets from this point get sent 440 * without the tag and go through the regular receive path. 441 */ 442 wmb(); 443 } 444 445 int dsa_conduit_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp, 446 struct netdev_lag_upper_info *uinfo, 447 struct netlink_ext_ack *extack) 448 { 449 bool conduit_setup = false; 450 int err; 451 452 if (!netdev_uses_dsa(lag_dev)) { 453 err = dsa_conduit_setup(lag_dev, cpu_dp); 454 if (err) 455 return err; 456 457 conduit_setup = true; 458 } 459 460 err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack); 461 if (err) { 462 NL_SET_ERR_MSG_WEAK_MOD(extack, "CPU port failed to join LAG"); 463 goto out_conduit_teardown; 464 } 465 466 return 0; 467 468 out_conduit_teardown: 469 if (conduit_setup) 470 dsa_conduit_teardown(lag_dev); 471 return err; 472 } 473 474 /* Tear down a conduit if there isn't any other user port on it, 475 * optionally also destroying LAG information. 476 */ 477 void dsa_conduit_lag_teardown(struct net_device *lag_dev, 478 struct dsa_port *cpu_dp) 479 { 480 struct net_device *upper; 481 struct list_head *iter; 482 483 dsa_port_lag_leave(cpu_dp, lag_dev); 484 485 netdev_for_each_upper_dev_rcu(lag_dev, upper, iter) 486 if (dsa_user_dev_check(upper)) 487 return; 488 489 dsa_conduit_teardown(lag_dev); 490 } 491