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