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