rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * net/dsa/slave.c - Slave device handling |
| 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/etherdevice.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/phy.h> |
| 15 | #include <linux/phy_fixed.h> |
| 16 | #include <linux/of_net.h> |
| 17 | #include <linux/of_mdio.h> |
| 18 | #include <linux/mdio.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <net/rtnetlink.h> |
| 21 | #include <net/pkt_cls.h> |
| 22 | #include <net/tc_act/tc_mirred.h> |
| 23 | #include <linux/if_bridge.h> |
| 24 | #include <linux/netpoll.h> |
| 25 | |
| 26 | #include "dsa_priv.h" |
| 27 | |
| 28 | static bool dsa_slave_dev_check(struct net_device *dev); |
| 29 | |
| 30 | /* slave mii_bus handling ***************************************************/ |
| 31 | static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg) |
| 32 | { |
| 33 | struct dsa_switch *ds = bus->priv; |
| 34 | |
| 35 | if (ds->phys_mii_mask & (1 << addr)) |
| 36 | return ds->ops->phy_read(ds, addr, reg); |
| 37 | |
| 38 | return 0xffff; |
| 39 | } |
| 40 | |
| 41 | static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val) |
| 42 | { |
| 43 | struct dsa_switch *ds = bus->priv; |
| 44 | |
| 45 | if (ds->phys_mii_mask & (1 << addr)) |
| 46 | return ds->ops->phy_write(ds, addr, reg, val); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | void dsa_slave_mii_bus_init(struct dsa_switch *ds) |
| 52 | { |
| 53 | ds->slave_mii_bus->priv = (void *)ds; |
| 54 | ds->slave_mii_bus->name = "dsa slave smi"; |
| 55 | ds->slave_mii_bus->read = dsa_slave_phy_read; |
| 56 | ds->slave_mii_bus->write = dsa_slave_phy_write; |
| 57 | snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d", |
| 58 | ds->dst->tree, ds->index); |
| 59 | ds->slave_mii_bus->parent = ds->dev; |
| 60 | ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* slave device handling ****************************************************/ |
| 65 | static int dsa_slave_get_iflink(const struct net_device *dev) |
| 66 | { |
| 67 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 68 | |
| 69 | return dsa_master_netdev(p)->ifindex; |
| 70 | } |
| 71 | |
| 72 | static int dsa_slave_open(struct net_device *dev) |
| 73 | { |
| 74 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 75 | struct dsa_port *dp = p->dp; |
| 76 | struct dsa_switch *ds = dp->ds; |
| 77 | struct net_device *master = dsa_master_netdev(p); |
| 78 | u8 stp_state = dp->bridge_dev ? BR_STATE_BLOCKING : BR_STATE_FORWARDING; |
| 79 | int err; |
| 80 | |
| 81 | if (!(master->flags & IFF_UP)) |
| 82 | return -ENETDOWN; |
| 83 | |
| 84 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) { |
| 85 | err = dev_uc_add(master, dev->dev_addr); |
| 86 | if (err < 0) |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | if (dev->flags & IFF_ALLMULTI) { |
| 91 | err = dev_set_allmulti(master, 1); |
| 92 | if (err < 0) |
| 93 | goto del_unicast; |
| 94 | } |
| 95 | if (dev->flags & IFF_PROMISC) { |
| 96 | err = dev_set_promiscuity(master, 1); |
| 97 | if (err < 0) |
| 98 | goto clear_allmulti; |
| 99 | } |
| 100 | |
| 101 | if (ds->ops->port_enable) { |
| 102 | err = ds->ops->port_enable(ds, p->dp->index, p->phy); |
| 103 | if (err) |
| 104 | goto clear_promisc; |
| 105 | } |
| 106 | |
| 107 | dsa_port_set_state_now(p->dp, stp_state); |
| 108 | |
| 109 | if (p->phy) |
| 110 | phy_start(p->phy); |
| 111 | |
| 112 | return 0; |
| 113 | |
| 114 | clear_promisc: |
| 115 | if (dev->flags & IFF_PROMISC) |
| 116 | dev_set_promiscuity(master, -1); |
| 117 | clear_allmulti: |
| 118 | if (dev->flags & IFF_ALLMULTI) |
| 119 | dev_set_allmulti(master, -1); |
| 120 | del_unicast: |
| 121 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
| 122 | dev_uc_del(master, dev->dev_addr); |
| 123 | out: |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | static int dsa_slave_close(struct net_device *dev) |
| 128 | { |
| 129 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 130 | struct net_device *master = dsa_master_netdev(p); |
| 131 | struct dsa_switch *ds = p->dp->ds; |
| 132 | |
| 133 | if (p->phy) |
| 134 | phy_stop(p->phy); |
| 135 | |
| 136 | dev_mc_unsync(master, dev); |
| 137 | dev_uc_unsync(master, dev); |
| 138 | if (dev->flags & IFF_ALLMULTI) |
| 139 | dev_set_allmulti(master, -1); |
| 140 | if (dev->flags & IFF_PROMISC) |
| 141 | dev_set_promiscuity(master, -1); |
| 142 | |
| 143 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
| 144 | dev_uc_del(master, dev->dev_addr); |
| 145 | |
| 146 | if (ds->ops->port_disable) |
| 147 | ds->ops->port_disable(ds, p->dp->index, p->phy); |
| 148 | |
| 149 | dsa_port_set_state_now(p->dp, BR_STATE_DISABLED); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static void dsa_slave_change_rx_flags(struct net_device *dev, int change) |
| 155 | { |
| 156 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 157 | struct net_device *master = dsa_master_netdev(p); |
| 158 | |
| 159 | if (dev->flags & IFF_UP) { |
| 160 | if (change & IFF_ALLMULTI) |
| 161 | dev_set_allmulti(master, |
| 162 | dev->flags & IFF_ALLMULTI ? 1 : -1); |
| 163 | if (change & IFF_PROMISC) |
| 164 | dev_set_promiscuity(master, |
| 165 | dev->flags & IFF_PROMISC ? 1 : -1); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void dsa_slave_set_rx_mode(struct net_device *dev) |
| 170 | { |
| 171 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 172 | struct net_device *master = dsa_master_netdev(p); |
| 173 | |
| 174 | dev_mc_sync(master, dev); |
| 175 | dev_uc_sync(master, dev); |
| 176 | } |
| 177 | |
| 178 | static int dsa_slave_set_mac_address(struct net_device *dev, void *a) |
| 179 | { |
| 180 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 181 | struct net_device *master = dsa_master_netdev(p); |
| 182 | struct sockaddr *addr = a; |
| 183 | int err; |
| 184 | |
| 185 | if (!is_valid_ether_addr(addr->sa_data)) |
| 186 | return -EADDRNOTAVAIL; |
| 187 | |
| 188 | if (!(dev->flags & IFF_UP)) |
| 189 | goto out; |
| 190 | |
| 191 | if (!ether_addr_equal(addr->sa_data, master->dev_addr)) { |
| 192 | err = dev_uc_add(master, addr->sa_data); |
| 193 | if (err < 0) |
| 194 | return err; |
| 195 | } |
| 196 | |
| 197 | if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) |
| 198 | dev_uc_del(master, dev->dev_addr); |
| 199 | |
| 200 | out: |
| 201 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | struct dsa_slave_dump_ctx { |
| 207 | struct net_device *dev; |
| 208 | struct sk_buff *skb; |
| 209 | struct netlink_callback *cb; |
| 210 | int idx; |
| 211 | }; |
| 212 | |
| 213 | static int |
| 214 | dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 215 | bool is_static, void *data) |
| 216 | { |
| 217 | struct dsa_slave_dump_ctx *dump = data; |
| 218 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 219 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 220 | struct nlmsghdr *nlh; |
| 221 | struct ndmsg *ndm; |
| 222 | |
| 223 | if (dump->idx < dump->cb->args[2]) |
| 224 | goto skip; |
| 225 | |
| 226 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 227 | sizeof(*ndm), NLM_F_MULTI); |
| 228 | if (!nlh) |
| 229 | return -EMSGSIZE; |
| 230 | |
| 231 | ndm = nlmsg_data(nlh); |
| 232 | ndm->ndm_family = AF_BRIDGE; |
| 233 | ndm->ndm_pad1 = 0; |
| 234 | ndm->ndm_pad2 = 0; |
| 235 | ndm->ndm_flags = NTF_SELF; |
| 236 | ndm->ndm_type = 0; |
| 237 | ndm->ndm_ifindex = dump->dev->ifindex; |
| 238 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
| 239 | |
| 240 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 241 | goto nla_put_failure; |
| 242 | |
| 243 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
| 244 | goto nla_put_failure; |
| 245 | |
| 246 | nlmsg_end(dump->skb, nlh); |
| 247 | |
| 248 | skip: |
| 249 | dump->idx++; |
| 250 | return 0; |
| 251 | |
| 252 | nla_put_failure: |
| 253 | nlmsg_cancel(dump->skb, nlh); |
| 254 | return -EMSGSIZE; |
| 255 | } |
| 256 | |
| 257 | static int |
| 258 | dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 259 | struct net_device *dev, struct net_device *filter_dev, |
| 260 | int *idx) |
| 261 | { |
| 262 | struct dsa_slave_dump_ctx dump = { |
| 263 | .dev = dev, |
| 264 | .skb = skb, |
| 265 | .cb = cb, |
| 266 | .idx = *idx, |
| 267 | }; |
| 268 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 269 | struct dsa_port *dp = p->dp; |
| 270 | struct dsa_switch *ds = dp->ds; |
| 271 | int err; |
| 272 | |
| 273 | if (!ds->ops->port_fdb_dump) |
| 274 | return -EOPNOTSUPP; |
| 275 | |
| 276 | err = ds->ops->port_fdb_dump(ds, dp->index, |
| 277 | dsa_slave_port_fdb_do_dump, |
| 278 | &dump); |
| 279 | *idx = dump.idx; |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 284 | { |
| 285 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 286 | |
| 287 | if (p->phy != NULL) |
| 288 | return phy_mii_ioctl(p->phy, ifr, cmd); |
| 289 | |
| 290 | return -EOPNOTSUPP; |
| 291 | } |
| 292 | |
| 293 | static int dsa_slave_port_attr_set(struct net_device *dev, |
| 294 | const struct switchdev_attr *attr, |
| 295 | struct switchdev_trans *trans) |
| 296 | { |
| 297 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 298 | struct dsa_port *dp = p->dp; |
| 299 | int ret; |
| 300 | |
| 301 | switch (attr->id) { |
| 302 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
| 303 | ret = dsa_port_set_state(dp, attr->u.stp_state, trans); |
| 304 | break; |
| 305 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
| 306 | ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering, |
| 307 | trans); |
| 308 | break; |
| 309 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
| 310 | ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans); |
| 311 | break; |
| 312 | default: |
| 313 | ret = -EOPNOTSUPP; |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int dsa_slave_port_obj_add(struct net_device *dev, |
| 321 | const struct switchdev_obj *obj, |
| 322 | struct switchdev_trans *trans) |
| 323 | { |
| 324 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 325 | struct dsa_port *dp = p->dp; |
| 326 | int err; |
| 327 | |
| 328 | /* For the prepare phase, ensure the full set of changes is feasable in |
| 329 | * one go in order to signal a failure properly. If an operation is not |
| 330 | * supported, return -EOPNOTSUPP. |
| 331 | */ |
| 332 | |
| 333 | switch (obj->id) { |
| 334 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 335 | err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans); |
| 336 | break; |
| 337 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 338 | err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj), |
| 339 | trans); |
| 340 | break; |
| 341 | default: |
| 342 | err = -EOPNOTSUPP; |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | static int dsa_slave_port_obj_del(struct net_device *dev, |
| 350 | const struct switchdev_obj *obj) |
| 351 | { |
| 352 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 353 | struct dsa_port *dp = p->dp; |
| 354 | int err; |
| 355 | |
| 356 | switch (obj->id) { |
| 357 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 358 | err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj)); |
| 359 | break; |
| 360 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 361 | err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj)); |
| 362 | break; |
| 363 | default: |
| 364 | err = -EOPNOTSUPP; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | static int dsa_slave_port_attr_get(struct net_device *dev, |
| 372 | struct switchdev_attr *attr) |
| 373 | { |
| 374 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 375 | struct dsa_switch *ds = p->dp->ds; |
| 376 | |
| 377 | switch (attr->id) { |
| 378 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
| 379 | attr->u.ppid.id_len = sizeof(ds->index); |
| 380 | memcpy(&attr->u.ppid.id, &ds->index, attr->u.ppid.id_len); |
| 381 | break; |
| 382 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT: |
| 383 | attr->u.brport_flags_support = 0; |
| 384 | break; |
| 385 | default: |
| 386 | return -EOPNOTSUPP; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static inline netdev_tx_t dsa_netpoll_send_skb(struct dsa_slave_priv *p, |
| 393 | struct sk_buff *skb) |
| 394 | { |
| 395 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 396 | if (p->netpoll) |
| 397 | netpoll_send_skb(p->netpoll, skb); |
| 398 | #else |
| 399 | BUG(); |
| 400 | #endif |
| 401 | return NETDEV_TX_OK; |
| 402 | } |
| 403 | |
| 404 | static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
| 405 | { |
| 406 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 407 | struct pcpu_sw_netstats *s; |
| 408 | struct sk_buff *nskb; |
| 409 | |
| 410 | s = this_cpu_ptr(p->stats64); |
| 411 | u64_stats_update_begin(&s->syncp); |
| 412 | s->tx_packets++; |
| 413 | s->tx_bytes += skb->len; |
| 414 | u64_stats_update_end(&s->syncp); |
| 415 | |
| 416 | /* Transmit function may have to reallocate the original SKB, |
| 417 | * in which case it must have freed it. Only free it here on error. |
| 418 | */ |
| 419 | nskb = p->xmit(skb, dev); |
| 420 | if (!nskb) { |
| 421 | kfree_skb(skb); |
| 422 | return NETDEV_TX_OK; |
| 423 | } |
| 424 | |
| 425 | /* SKB for netpoll still need to be mangled with the protocol-specific |
| 426 | * tag to be successfully transmitted |
| 427 | */ |
| 428 | if (unlikely(netpoll_tx_running(dev))) |
| 429 | return dsa_netpoll_send_skb(p, nskb); |
| 430 | |
| 431 | /* Queue the SKB for transmission on the parent interface, but |
| 432 | * do not modify its EtherType |
| 433 | */ |
| 434 | nskb->dev = dsa_master_netdev(p); |
| 435 | dev_queue_xmit(nskb); |
| 436 | |
| 437 | return NETDEV_TX_OK; |
| 438 | } |
| 439 | |
| 440 | /* ethtool operations *******************************************************/ |
| 441 | static int |
| 442 | dsa_slave_get_link_ksettings(struct net_device *dev, |
| 443 | struct ethtool_link_ksettings *cmd) |
| 444 | { |
| 445 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 446 | |
| 447 | if (!p->phy) |
| 448 | return -EOPNOTSUPP; |
| 449 | |
| 450 | phy_ethtool_ksettings_get(p->phy, cmd); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int |
| 456 | dsa_slave_set_link_ksettings(struct net_device *dev, |
| 457 | const struct ethtool_link_ksettings *cmd) |
| 458 | { |
| 459 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 460 | |
| 461 | if (p->phy != NULL) |
| 462 | return phy_ethtool_ksettings_set(p->phy, cmd); |
| 463 | |
| 464 | return -EOPNOTSUPP; |
| 465 | } |
| 466 | |
| 467 | static void dsa_slave_get_drvinfo(struct net_device *dev, |
| 468 | struct ethtool_drvinfo *drvinfo) |
| 469 | { |
| 470 | strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver)); |
| 471 | strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); |
| 472 | strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); |
| 473 | } |
| 474 | |
| 475 | static int dsa_slave_get_regs_len(struct net_device *dev) |
| 476 | { |
| 477 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 478 | struct dsa_switch *ds = p->dp->ds; |
| 479 | |
| 480 | if (ds->ops->get_regs_len) |
| 481 | return ds->ops->get_regs_len(ds, p->dp->index); |
| 482 | |
| 483 | return -EOPNOTSUPP; |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p) |
| 488 | { |
| 489 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 490 | struct dsa_switch *ds = p->dp->ds; |
| 491 | |
| 492 | if (ds->ops->get_regs) |
| 493 | ds->ops->get_regs(ds, p->dp->index, regs, _p); |
| 494 | } |
| 495 | |
| 496 | static int dsa_slave_nway_reset(struct net_device *dev) |
| 497 | { |
| 498 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 499 | |
| 500 | if (p->phy != NULL) |
| 501 | return genphy_restart_aneg(p->phy); |
| 502 | |
| 503 | return -EOPNOTSUPP; |
| 504 | } |
| 505 | |
| 506 | static u32 dsa_slave_get_link(struct net_device *dev) |
| 507 | { |
| 508 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 509 | |
| 510 | if (p->phy != NULL) { |
| 511 | genphy_update_link(p->phy); |
| 512 | return p->phy->link; |
| 513 | } |
| 514 | |
| 515 | return -EOPNOTSUPP; |
| 516 | } |
| 517 | |
| 518 | static int dsa_slave_get_eeprom_len(struct net_device *dev) |
| 519 | { |
| 520 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 521 | struct dsa_switch *ds = p->dp->ds; |
| 522 | |
| 523 | if (ds->cd && ds->cd->eeprom_len) |
| 524 | return ds->cd->eeprom_len; |
| 525 | |
| 526 | if (ds->ops->get_eeprom_len) |
| 527 | return ds->ops->get_eeprom_len(ds); |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int dsa_slave_get_eeprom(struct net_device *dev, |
| 533 | struct ethtool_eeprom *eeprom, u8 *data) |
| 534 | { |
| 535 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 536 | struct dsa_switch *ds = p->dp->ds; |
| 537 | |
| 538 | if (ds->ops->get_eeprom) |
| 539 | return ds->ops->get_eeprom(ds, eeprom, data); |
| 540 | |
| 541 | return -EOPNOTSUPP; |
| 542 | } |
| 543 | |
| 544 | static int dsa_slave_set_eeprom(struct net_device *dev, |
| 545 | struct ethtool_eeprom *eeprom, u8 *data) |
| 546 | { |
| 547 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 548 | struct dsa_switch *ds = p->dp->ds; |
| 549 | |
| 550 | if (ds->ops->set_eeprom) |
| 551 | return ds->ops->set_eeprom(ds, eeprom, data); |
| 552 | |
| 553 | return -EOPNOTSUPP; |
| 554 | } |
| 555 | |
| 556 | static void dsa_slave_get_strings(struct net_device *dev, |
| 557 | uint32_t stringset, uint8_t *data) |
| 558 | { |
| 559 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 560 | struct dsa_switch *ds = p->dp->ds; |
| 561 | |
| 562 | if (stringset == ETH_SS_STATS) { |
| 563 | int len = ETH_GSTRING_LEN; |
| 564 | |
| 565 | strncpy(data, "tx_packets", len); |
| 566 | strncpy(data + len, "tx_bytes", len); |
| 567 | strncpy(data + 2 * len, "rx_packets", len); |
| 568 | strncpy(data + 3 * len, "rx_bytes", len); |
| 569 | if (ds->ops->get_strings) |
| 570 | ds->ops->get_strings(ds, p->dp->index, data + 4 * len); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void dsa_cpu_port_get_ethtool_stats(struct net_device *dev, |
| 575 | struct ethtool_stats *stats, |
| 576 | uint64_t *data) |
| 577 | { |
| 578 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 579 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
| 580 | struct dsa_switch *ds = cpu_dp->ds; |
| 581 | s8 cpu_port = cpu_dp->index; |
| 582 | int count = 0; |
| 583 | |
| 584 | if (cpu_dp->ethtool_ops.get_sset_count) { |
| 585 | count = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS); |
| 586 | cpu_dp->ethtool_ops.get_ethtool_stats(dev, stats, data); |
| 587 | } |
| 588 | |
| 589 | if (ds->ops->get_ethtool_stats) |
| 590 | ds->ops->get_ethtool_stats(ds, cpu_port, data + count); |
| 591 | } |
| 592 | |
| 593 | static int dsa_cpu_port_get_sset_count(struct net_device *dev, int sset) |
| 594 | { |
| 595 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 596 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
| 597 | struct dsa_switch *ds = cpu_dp->ds; |
| 598 | int count = 0; |
| 599 | |
| 600 | if (cpu_dp->ethtool_ops.get_sset_count) |
| 601 | count += cpu_dp->ethtool_ops.get_sset_count(dev, sset); |
| 602 | |
| 603 | if (sset == ETH_SS_STATS && ds->ops->get_sset_count) |
| 604 | count += ds->ops->get_sset_count(ds); |
| 605 | |
| 606 | return count; |
| 607 | } |
| 608 | |
| 609 | static void dsa_cpu_port_get_strings(struct net_device *dev, |
| 610 | uint32_t stringset, uint8_t *data) |
| 611 | { |
| 612 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 613 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
| 614 | struct dsa_switch *ds = cpu_dp->ds; |
| 615 | s8 cpu_port = cpu_dp->index; |
| 616 | int len = ETH_GSTRING_LEN; |
| 617 | int mcount = 0, count; |
| 618 | unsigned int i; |
| 619 | uint8_t pfx[4]; |
| 620 | uint8_t *ndata; |
| 621 | |
| 622 | snprintf(pfx, sizeof(pfx), "p%.2d", cpu_port); |
| 623 | /* We do not want to be NULL-terminated, since this is a prefix */ |
| 624 | pfx[sizeof(pfx) - 1] = '_'; |
| 625 | |
| 626 | if (cpu_dp->ethtool_ops.get_sset_count) { |
| 627 | mcount = cpu_dp->ethtool_ops.get_sset_count(dev, ETH_SS_STATS); |
| 628 | cpu_dp->ethtool_ops.get_strings(dev, stringset, data); |
| 629 | } |
| 630 | |
| 631 | if (stringset == ETH_SS_STATS && ds->ops->get_strings) { |
| 632 | ndata = data + mcount * len; |
| 633 | /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle |
| 634 | * the output after to prepend our CPU port prefix we |
| 635 | * constructed earlier |
| 636 | */ |
| 637 | ds->ops->get_strings(ds, cpu_port, ndata); |
| 638 | count = ds->ops->get_sset_count(ds); |
| 639 | for (i = 0; i < count; i++) { |
| 640 | memmove(ndata + (i * len + sizeof(pfx)), |
| 641 | ndata + i * len, len - sizeof(pfx)); |
| 642 | memcpy(ndata + i * len, pfx, sizeof(pfx)); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | static void dsa_slave_get_ethtool_stats(struct net_device *dev, |
| 648 | struct ethtool_stats *stats, |
| 649 | uint64_t *data) |
| 650 | { |
| 651 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 652 | struct dsa_switch *ds = p->dp->ds; |
| 653 | struct pcpu_sw_netstats *s; |
| 654 | unsigned int start; |
| 655 | int i; |
| 656 | |
| 657 | for_each_possible_cpu(i) { |
| 658 | u64 tx_packets, tx_bytes, rx_packets, rx_bytes; |
| 659 | |
| 660 | s = per_cpu_ptr(p->stats64, i); |
| 661 | do { |
| 662 | start = u64_stats_fetch_begin_irq(&s->syncp); |
| 663 | tx_packets = s->tx_packets; |
| 664 | tx_bytes = s->tx_bytes; |
| 665 | rx_packets = s->rx_packets; |
| 666 | rx_bytes = s->rx_bytes; |
| 667 | } while (u64_stats_fetch_retry_irq(&s->syncp, start)); |
| 668 | data[0] += tx_packets; |
| 669 | data[1] += tx_bytes; |
| 670 | data[2] += rx_packets; |
| 671 | data[3] += rx_bytes; |
| 672 | } |
| 673 | if (ds->ops->get_ethtool_stats) |
| 674 | ds->ops->get_ethtool_stats(ds, p->dp->index, data + 4); |
| 675 | } |
| 676 | |
| 677 | static int dsa_slave_get_sset_count(struct net_device *dev, int sset) |
| 678 | { |
| 679 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 680 | struct dsa_switch *ds = p->dp->ds; |
| 681 | |
| 682 | if (sset == ETH_SS_STATS) { |
| 683 | int count; |
| 684 | |
| 685 | count = 4; |
| 686 | if (ds->ops->get_sset_count) |
| 687 | count += ds->ops->get_sset_count(ds); |
| 688 | |
| 689 | return count; |
| 690 | } |
| 691 | |
| 692 | return -EOPNOTSUPP; |
| 693 | } |
| 694 | |
| 695 | static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 696 | { |
| 697 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 698 | struct dsa_switch *ds = p->dp->ds; |
| 699 | |
| 700 | if (ds->ops->get_wol) |
| 701 | ds->ops->get_wol(ds, p->dp->index, w); |
| 702 | } |
| 703 | |
| 704 | static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) |
| 705 | { |
| 706 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 707 | struct dsa_switch *ds = p->dp->ds; |
| 708 | int ret = -EOPNOTSUPP; |
| 709 | |
| 710 | if (ds->ops->set_wol) |
| 711 | ret = ds->ops->set_wol(ds, p->dp->index, w); |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e) |
| 717 | { |
| 718 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 719 | struct dsa_switch *ds = p->dp->ds; |
| 720 | int ret; |
| 721 | |
| 722 | /* Port's PHY and MAC both need to be EEE capable */ |
| 723 | if (!p->phy) |
| 724 | return -ENODEV; |
| 725 | |
| 726 | if (!ds->ops->set_mac_eee) |
| 727 | return -EOPNOTSUPP; |
| 728 | |
| 729 | ret = ds->ops->set_mac_eee(ds, p->dp->index, e); |
| 730 | if (ret) |
| 731 | return ret; |
| 732 | |
| 733 | if (e->eee_enabled) { |
| 734 | ret = phy_init_eee(p->phy, 0); |
| 735 | if (ret) |
| 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | return phy_ethtool_set_eee(p->phy, e); |
| 740 | } |
| 741 | |
| 742 | static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) |
| 743 | { |
| 744 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 745 | struct dsa_switch *ds = p->dp->ds; |
| 746 | int ret; |
| 747 | |
| 748 | /* Port's PHY and MAC both need to be EEE capable */ |
| 749 | if (!p->phy) |
| 750 | return -ENODEV; |
| 751 | |
| 752 | if (!ds->ops->get_mac_eee) |
| 753 | return -EOPNOTSUPP; |
| 754 | |
| 755 | ret = ds->ops->get_mac_eee(ds, p->dp->index, e); |
| 756 | if (ret) |
| 757 | return ret; |
| 758 | |
| 759 | return phy_ethtool_get_eee(p->phy, e); |
| 760 | } |
| 761 | |
| 762 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 763 | static int dsa_slave_netpoll_setup(struct net_device *dev, |
| 764 | struct netpoll_info *ni) |
| 765 | { |
| 766 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 767 | struct net_device *master = dsa_master_netdev(p); |
| 768 | struct netpoll *netpoll; |
| 769 | int err = 0; |
| 770 | |
| 771 | netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL); |
| 772 | if (!netpoll) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | err = __netpoll_setup(netpoll, master); |
| 776 | if (err) { |
| 777 | kfree(netpoll); |
| 778 | goto out; |
| 779 | } |
| 780 | |
| 781 | p->netpoll = netpoll; |
| 782 | out: |
| 783 | return err; |
| 784 | } |
| 785 | |
| 786 | static void dsa_slave_netpoll_cleanup(struct net_device *dev) |
| 787 | { |
| 788 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 789 | struct netpoll *netpoll = p->netpoll; |
| 790 | |
| 791 | if (!netpoll) |
| 792 | return; |
| 793 | |
| 794 | p->netpoll = NULL; |
| 795 | |
| 796 | __netpoll_free_async(netpoll); |
| 797 | } |
| 798 | |
| 799 | static void dsa_slave_poll_controller(struct net_device *dev) |
| 800 | { |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | static int dsa_slave_get_phys_port_name(struct net_device *dev, |
| 805 | char *name, size_t len) |
| 806 | { |
| 807 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 808 | |
| 809 | if (snprintf(name, len, "p%d", p->dp->index) >= len) |
| 810 | return -EINVAL; |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static struct dsa_mall_tc_entry * |
| 816 | dsa_slave_mall_tc_entry_find(struct dsa_slave_priv *p, |
| 817 | unsigned long cookie) |
| 818 | { |
| 819 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 820 | |
| 821 | list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) |
| 822 | if (mall_tc_entry->cookie == cookie) |
| 823 | return mall_tc_entry; |
| 824 | |
| 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | static int dsa_slave_add_cls_matchall(struct net_device *dev, |
| 829 | struct tc_cls_matchall_offload *cls, |
| 830 | bool ingress) |
| 831 | { |
| 832 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 833 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 834 | __be16 protocol = cls->common.protocol; |
| 835 | struct dsa_switch *ds = p->dp->ds; |
| 836 | struct net *net = dev_net(dev); |
| 837 | struct dsa_slave_priv *to_p; |
| 838 | struct net_device *to_dev; |
| 839 | const struct tc_action *a; |
| 840 | int err = -EOPNOTSUPP; |
| 841 | LIST_HEAD(actions); |
| 842 | int ifindex; |
| 843 | |
| 844 | if (!ds->ops->port_mirror_add) |
| 845 | return err; |
| 846 | |
| 847 | if (!tcf_exts_has_one_action(cls->exts)) |
| 848 | return err; |
| 849 | |
| 850 | tcf_exts_to_list(cls->exts, &actions); |
| 851 | a = list_first_entry(&actions, struct tc_action, list); |
| 852 | |
| 853 | if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) { |
| 854 | struct dsa_mall_mirror_tc_entry *mirror; |
| 855 | |
| 856 | ifindex = tcf_mirred_ifindex(a); |
| 857 | to_dev = __dev_get_by_index(net, ifindex); |
| 858 | if (!to_dev) |
| 859 | return -EINVAL; |
| 860 | |
| 861 | if (!dsa_slave_dev_check(to_dev)) |
| 862 | return -EOPNOTSUPP; |
| 863 | |
| 864 | mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); |
| 865 | if (!mall_tc_entry) |
| 866 | return -ENOMEM; |
| 867 | |
| 868 | mall_tc_entry->cookie = cls->cookie; |
| 869 | mall_tc_entry->type = DSA_PORT_MALL_MIRROR; |
| 870 | mirror = &mall_tc_entry->mirror; |
| 871 | |
| 872 | to_p = netdev_priv(to_dev); |
| 873 | |
| 874 | mirror->to_local_port = to_p->dp->index; |
| 875 | mirror->ingress = ingress; |
| 876 | |
| 877 | err = ds->ops->port_mirror_add(ds, p->dp->index, mirror, |
| 878 | ingress); |
| 879 | if (err) { |
| 880 | kfree(mall_tc_entry); |
| 881 | return err; |
| 882 | } |
| 883 | |
| 884 | list_add_tail(&mall_tc_entry->list, &p->mall_tc_list); |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static void dsa_slave_del_cls_matchall(struct net_device *dev, |
| 891 | struct tc_cls_matchall_offload *cls) |
| 892 | { |
| 893 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 894 | struct dsa_mall_tc_entry *mall_tc_entry; |
| 895 | struct dsa_switch *ds = p->dp->ds; |
| 896 | |
| 897 | if (!ds->ops->port_mirror_del) |
| 898 | return; |
| 899 | |
| 900 | mall_tc_entry = dsa_slave_mall_tc_entry_find(p, cls->cookie); |
| 901 | if (!mall_tc_entry) |
| 902 | return; |
| 903 | |
| 904 | list_del(&mall_tc_entry->list); |
| 905 | |
| 906 | switch (mall_tc_entry->type) { |
| 907 | case DSA_PORT_MALL_MIRROR: |
| 908 | ds->ops->port_mirror_del(ds, p->dp->index, |
| 909 | &mall_tc_entry->mirror); |
| 910 | break; |
| 911 | default: |
| 912 | WARN_ON(1); |
| 913 | } |
| 914 | |
| 915 | kfree(mall_tc_entry); |
| 916 | } |
| 917 | |
| 918 | static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev, |
| 919 | struct tc_cls_matchall_offload *cls) |
| 920 | { |
| 921 | bool ingress; |
| 922 | |
| 923 | if (is_classid_clsact_ingress(cls->common.classid)) |
| 924 | ingress = true; |
| 925 | else if (is_classid_clsact_egress(cls->common.classid)) |
| 926 | ingress = false; |
| 927 | else |
| 928 | return -EOPNOTSUPP; |
| 929 | |
| 930 | if (cls->common.chain_index) |
| 931 | return -EOPNOTSUPP; |
| 932 | |
| 933 | switch (cls->command) { |
| 934 | case TC_CLSMATCHALL_REPLACE: |
| 935 | return dsa_slave_add_cls_matchall(dev, cls, ingress); |
| 936 | case TC_CLSMATCHALL_DESTROY: |
| 937 | dsa_slave_del_cls_matchall(dev, cls); |
| 938 | return 0; |
| 939 | default: |
| 940 | return -EOPNOTSUPP; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type, |
| 945 | void *type_data) |
| 946 | { |
| 947 | switch (type) { |
| 948 | case TC_SETUP_CLSMATCHALL: |
| 949 | return dsa_slave_setup_tc_cls_matchall(dev, type_data); |
| 950 | default: |
| 951 | return -EOPNOTSUPP; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | static void dsa_slave_get_stats64(struct net_device *dev, |
| 956 | struct rtnl_link_stats64 *stats) |
| 957 | { |
| 958 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 959 | struct pcpu_sw_netstats *s; |
| 960 | unsigned int start; |
| 961 | int i; |
| 962 | |
| 963 | netdev_stats_to_stats64(stats, &dev->stats); |
| 964 | for_each_possible_cpu(i) { |
| 965 | u64 tx_packets, tx_bytes, rx_packets, rx_bytes; |
| 966 | |
| 967 | s = per_cpu_ptr(p->stats64, i); |
| 968 | do { |
| 969 | start = u64_stats_fetch_begin_irq(&s->syncp); |
| 970 | tx_packets = s->tx_packets; |
| 971 | tx_bytes = s->tx_bytes; |
| 972 | rx_packets = s->rx_packets; |
| 973 | rx_bytes = s->rx_bytes; |
| 974 | } while (u64_stats_fetch_retry_irq(&s->syncp, start)); |
| 975 | |
| 976 | stats->tx_packets += tx_packets; |
| 977 | stats->tx_bytes += tx_bytes; |
| 978 | stats->rx_packets += rx_packets; |
| 979 | stats->rx_bytes += rx_bytes; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | void dsa_cpu_port_ethtool_init(struct ethtool_ops *ops) |
| 984 | { |
| 985 | ops->get_sset_count = dsa_cpu_port_get_sset_count; |
| 986 | ops->get_ethtool_stats = dsa_cpu_port_get_ethtool_stats; |
| 987 | ops->get_strings = dsa_cpu_port_get_strings; |
| 988 | } |
| 989 | |
| 990 | static int dsa_slave_get_rxnfc(struct net_device *dev, |
| 991 | struct ethtool_rxnfc *nfc, u32 *rule_locs) |
| 992 | { |
| 993 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 994 | struct dsa_switch *ds = p->dp->ds; |
| 995 | |
| 996 | if (!ds->ops->get_rxnfc) |
| 997 | return -EOPNOTSUPP; |
| 998 | |
| 999 | return ds->ops->get_rxnfc(ds, p->dp->index, nfc, rule_locs); |
| 1000 | } |
| 1001 | |
| 1002 | static int dsa_slave_set_rxnfc(struct net_device *dev, |
| 1003 | struct ethtool_rxnfc *nfc) |
| 1004 | { |
| 1005 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1006 | struct dsa_switch *ds = p->dp->ds; |
| 1007 | |
| 1008 | if (!ds->ops->set_rxnfc) |
| 1009 | return -EOPNOTSUPP; |
| 1010 | |
| 1011 | return ds->ops->set_rxnfc(ds, p->dp->index, nfc); |
| 1012 | } |
| 1013 | |
| 1014 | static const struct ethtool_ops dsa_slave_ethtool_ops = { |
| 1015 | .get_drvinfo = dsa_slave_get_drvinfo, |
| 1016 | .get_regs_len = dsa_slave_get_regs_len, |
| 1017 | .get_regs = dsa_slave_get_regs, |
| 1018 | .nway_reset = dsa_slave_nway_reset, |
| 1019 | .get_link = dsa_slave_get_link, |
| 1020 | .get_eeprom_len = dsa_slave_get_eeprom_len, |
| 1021 | .get_eeprom = dsa_slave_get_eeprom, |
| 1022 | .set_eeprom = dsa_slave_set_eeprom, |
| 1023 | .get_strings = dsa_slave_get_strings, |
| 1024 | .get_ethtool_stats = dsa_slave_get_ethtool_stats, |
| 1025 | .get_sset_count = dsa_slave_get_sset_count, |
| 1026 | .set_wol = dsa_slave_set_wol, |
| 1027 | .get_wol = dsa_slave_get_wol, |
| 1028 | .set_eee = dsa_slave_set_eee, |
| 1029 | .get_eee = dsa_slave_get_eee, |
| 1030 | .get_link_ksettings = dsa_slave_get_link_ksettings, |
| 1031 | .set_link_ksettings = dsa_slave_set_link_ksettings, |
| 1032 | .get_rxnfc = dsa_slave_get_rxnfc, |
| 1033 | .set_rxnfc = dsa_slave_set_rxnfc, |
| 1034 | }; |
| 1035 | |
| 1036 | static const struct net_device_ops dsa_slave_netdev_ops = { |
| 1037 | .ndo_open = dsa_slave_open, |
| 1038 | .ndo_stop = dsa_slave_close, |
| 1039 | .ndo_start_xmit = dsa_slave_xmit, |
| 1040 | .ndo_change_rx_flags = dsa_slave_change_rx_flags, |
| 1041 | .ndo_set_rx_mode = dsa_slave_set_rx_mode, |
| 1042 | .ndo_set_mac_address = dsa_slave_set_mac_address, |
| 1043 | .ndo_fdb_add = dsa_legacy_fdb_add, |
| 1044 | .ndo_fdb_del = dsa_legacy_fdb_del, |
| 1045 | .ndo_fdb_dump = dsa_slave_fdb_dump, |
| 1046 | .ndo_do_ioctl = dsa_slave_ioctl, |
| 1047 | .ndo_get_iflink = dsa_slave_get_iflink, |
| 1048 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1049 | .ndo_netpoll_setup = dsa_slave_netpoll_setup, |
| 1050 | .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup, |
| 1051 | .ndo_poll_controller = dsa_slave_poll_controller, |
| 1052 | #endif |
| 1053 | .ndo_get_phys_port_name = dsa_slave_get_phys_port_name, |
| 1054 | .ndo_setup_tc = dsa_slave_setup_tc, |
| 1055 | .ndo_get_stats64 = dsa_slave_get_stats64, |
| 1056 | }; |
| 1057 | |
| 1058 | static const struct switchdev_ops dsa_slave_switchdev_ops = { |
| 1059 | .switchdev_port_attr_get = dsa_slave_port_attr_get, |
| 1060 | .switchdev_port_attr_set = dsa_slave_port_attr_set, |
| 1061 | .switchdev_port_obj_add = dsa_slave_port_obj_add, |
| 1062 | .switchdev_port_obj_del = dsa_slave_port_obj_del, |
| 1063 | }; |
| 1064 | |
| 1065 | static struct device_type dsa_type = { |
| 1066 | .name = "dsa", |
| 1067 | }; |
| 1068 | |
| 1069 | static void dsa_slave_adjust_link(struct net_device *dev) |
| 1070 | { |
| 1071 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1072 | struct dsa_switch *ds = p->dp->ds; |
| 1073 | unsigned int status_changed = 0; |
| 1074 | |
| 1075 | if (p->old_link != p->phy->link) { |
| 1076 | status_changed = 1; |
| 1077 | p->old_link = p->phy->link; |
| 1078 | } |
| 1079 | |
| 1080 | if (p->old_duplex != p->phy->duplex) { |
| 1081 | status_changed = 1; |
| 1082 | p->old_duplex = p->phy->duplex; |
| 1083 | } |
| 1084 | |
| 1085 | if (p->old_pause != p->phy->pause) { |
| 1086 | status_changed = 1; |
| 1087 | p->old_pause = p->phy->pause; |
| 1088 | } |
| 1089 | |
| 1090 | if (ds->ops->adjust_link && status_changed) |
| 1091 | ds->ops->adjust_link(ds, p->dp->index, p->phy); |
| 1092 | |
| 1093 | if (status_changed) |
| 1094 | phy_print_status(p->phy); |
| 1095 | } |
| 1096 | |
| 1097 | static int dsa_slave_fixed_link_update(struct net_device *dev, |
| 1098 | struct fixed_phy_status *status) |
| 1099 | { |
| 1100 | struct dsa_slave_priv *p; |
| 1101 | struct dsa_switch *ds; |
| 1102 | |
| 1103 | if (dev) { |
| 1104 | p = netdev_priv(dev); |
| 1105 | ds = p->dp->ds; |
| 1106 | if (ds->ops->fixed_link_update) |
| 1107 | ds->ops->fixed_link_update(ds, p->dp->index, status); |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | /* slave device setup *******************************************************/ |
| 1114 | static int dsa_slave_phy_connect(struct dsa_slave_priv *p, |
| 1115 | struct net_device *slave_dev, |
| 1116 | int addr) |
| 1117 | { |
| 1118 | struct dsa_switch *ds = p->dp->ds; |
| 1119 | |
| 1120 | p->phy = mdiobus_get_phy(ds->slave_mii_bus, addr); |
| 1121 | if (!p->phy) { |
| 1122 | netdev_err(slave_dev, "no phy at %d\n", addr); |
| 1123 | return -ENODEV; |
| 1124 | } |
| 1125 | |
| 1126 | /* Use already configured phy mode */ |
| 1127 | if (p->phy_interface == PHY_INTERFACE_MODE_NA) |
| 1128 | p->phy_interface = p->phy->interface; |
| 1129 | return phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link, |
| 1130 | p->phy_interface); |
| 1131 | } |
| 1132 | |
| 1133 | static int dsa_slave_phy_setup(struct dsa_slave_priv *p, |
| 1134 | struct net_device *slave_dev) |
| 1135 | { |
| 1136 | struct dsa_switch *ds = p->dp->ds; |
| 1137 | struct device_node *phy_dn, *port_dn; |
| 1138 | bool phy_is_fixed = false; |
| 1139 | u32 phy_flags = 0; |
| 1140 | int mode, ret; |
| 1141 | |
| 1142 | port_dn = p->dp->dn; |
| 1143 | mode = of_get_phy_mode(port_dn); |
| 1144 | if (mode < 0) |
| 1145 | mode = PHY_INTERFACE_MODE_NA; |
| 1146 | p->phy_interface = mode; |
| 1147 | |
| 1148 | phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); |
| 1149 | if (!phy_dn && of_phy_is_fixed_link(port_dn)) { |
| 1150 | /* In the case of a fixed PHY, the DT node associated |
| 1151 | * to the fixed PHY is the Port DT node |
| 1152 | */ |
| 1153 | ret = of_phy_register_fixed_link(port_dn); |
| 1154 | if (ret) { |
| 1155 | netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | phy_is_fixed = true; |
| 1159 | phy_dn = of_node_get(port_dn); |
| 1160 | } |
| 1161 | |
| 1162 | if (ds->ops->get_phy_flags) |
| 1163 | phy_flags = ds->ops->get_phy_flags(ds, p->dp->index); |
| 1164 | |
| 1165 | if (phy_dn) { |
| 1166 | int phy_id = of_mdio_parse_addr(&slave_dev->dev, phy_dn); |
| 1167 | |
| 1168 | /* If this PHY address is part of phys_mii_mask, which means |
| 1169 | * that we need to divert reads and writes to/from it, then we |
| 1170 | * want to bind this device using the slave MII bus created by |
| 1171 | * DSA to make that happen. |
| 1172 | */ |
| 1173 | if (!phy_is_fixed && phy_id >= 0 && |
| 1174 | (ds->phys_mii_mask & (1 << phy_id))) { |
| 1175 | ret = dsa_slave_phy_connect(p, slave_dev, phy_id); |
| 1176 | if (ret) { |
| 1177 | netdev_err(slave_dev, "failed to connect to phy%d: %d\n", phy_id, ret); |
| 1178 | of_node_put(phy_dn); |
| 1179 | return ret; |
| 1180 | } |
| 1181 | } else { |
| 1182 | p->phy = of_phy_connect(slave_dev, phy_dn, |
| 1183 | dsa_slave_adjust_link, |
| 1184 | phy_flags, |
| 1185 | p->phy_interface); |
| 1186 | } |
| 1187 | |
| 1188 | of_node_put(phy_dn); |
| 1189 | } |
| 1190 | |
| 1191 | if (p->phy && phy_is_fixed) |
| 1192 | fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); |
| 1193 | |
| 1194 | /* We could not connect to a designated PHY, so use the switch internal |
| 1195 | * MDIO bus instead |
| 1196 | */ |
| 1197 | if (!p->phy) { |
| 1198 | ret = dsa_slave_phy_connect(p, slave_dev, p->dp->index); |
| 1199 | if (ret) { |
| 1200 | netdev_err(slave_dev, "failed to connect to port %d: %d\n", |
| 1201 | p->dp->index, ret); |
| 1202 | if (phy_is_fixed) |
| 1203 | of_phy_deregister_fixed_link(port_dn); |
| 1204 | return ret; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | phy_attached_info(p->phy); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static struct lock_class_key dsa_slave_netdev_xmit_lock_key; |
| 1214 | static void dsa_slave_set_lockdep_class_one(struct net_device *dev, |
| 1215 | struct netdev_queue *txq, |
| 1216 | void *_unused) |
| 1217 | { |
| 1218 | lockdep_set_class(&txq->_xmit_lock, |
| 1219 | &dsa_slave_netdev_xmit_lock_key); |
| 1220 | } |
| 1221 | |
| 1222 | int dsa_slave_suspend(struct net_device *slave_dev) |
| 1223 | { |
| 1224 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1225 | |
| 1226 | if (!netif_running(slave_dev)) |
| 1227 | return 0; |
| 1228 | |
| 1229 | netif_device_detach(slave_dev); |
| 1230 | |
| 1231 | if (p->phy) { |
| 1232 | phy_stop(p->phy); |
| 1233 | p->old_pause = -1; |
| 1234 | p->old_link = -1; |
| 1235 | p->old_duplex = -1; |
| 1236 | phy_suspend(p->phy); |
| 1237 | } |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | int dsa_slave_resume(struct net_device *slave_dev) |
| 1243 | { |
| 1244 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1245 | |
| 1246 | if (!netif_running(slave_dev)) |
| 1247 | return 0; |
| 1248 | |
| 1249 | netif_device_attach(slave_dev); |
| 1250 | |
| 1251 | if (p->phy) { |
| 1252 | phy_resume(p->phy); |
| 1253 | phy_start(p->phy); |
| 1254 | } |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | int dsa_slave_create(struct dsa_port *port, const char *name) |
| 1260 | { |
| 1261 | struct dsa_switch *ds = port->ds; |
| 1262 | struct dsa_switch_tree *dst = ds->dst; |
| 1263 | struct net_device *master; |
| 1264 | struct net_device *slave_dev; |
| 1265 | struct dsa_slave_priv *p; |
| 1266 | struct dsa_port *cpu_dp; |
| 1267 | int ret; |
| 1268 | |
| 1269 | cpu_dp = ds->dst->cpu_dp; |
| 1270 | master = cpu_dp->netdev; |
| 1271 | |
| 1272 | if (!ds->num_tx_queues) |
| 1273 | ds->num_tx_queues = 1; |
| 1274 | |
| 1275 | slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name, |
| 1276 | NET_NAME_UNKNOWN, ether_setup, |
| 1277 | ds->num_tx_queues, 1); |
| 1278 | if (slave_dev == NULL) |
| 1279 | return -ENOMEM; |
| 1280 | |
| 1281 | slave_dev->features = master->vlan_features | NETIF_F_HW_TC; |
| 1282 | slave_dev->hw_features |= NETIF_F_HW_TC; |
| 1283 | slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; |
| 1284 | eth_hw_addr_inherit(slave_dev, master); |
| 1285 | slave_dev->priv_flags |= IFF_NO_QUEUE; |
| 1286 | slave_dev->netdev_ops = &dsa_slave_netdev_ops; |
| 1287 | slave_dev->switchdev_ops = &dsa_slave_switchdev_ops; |
| 1288 | slave_dev->min_mtu = 0; |
| 1289 | slave_dev->max_mtu = ETH_MAX_MTU; |
| 1290 | SET_NETDEV_DEVTYPE(slave_dev, &dsa_type); |
| 1291 | |
| 1292 | netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one, |
| 1293 | NULL); |
| 1294 | |
| 1295 | SET_NETDEV_DEV(slave_dev, port->ds->dev); |
| 1296 | slave_dev->dev.of_node = port->dn; |
| 1297 | slave_dev->vlan_features = master->vlan_features; |
| 1298 | |
| 1299 | p = netdev_priv(slave_dev); |
| 1300 | p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
| 1301 | if (!p->stats64) { |
| 1302 | free_netdev(slave_dev); |
| 1303 | return -ENOMEM; |
| 1304 | } |
| 1305 | p->dp = port; |
| 1306 | INIT_LIST_HEAD(&p->mall_tc_list); |
| 1307 | p->xmit = dst->tag_ops->xmit; |
| 1308 | |
| 1309 | p->old_pause = -1; |
| 1310 | p->old_link = -1; |
| 1311 | p->old_duplex = -1; |
| 1312 | |
| 1313 | port->netdev = slave_dev; |
| 1314 | |
| 1315 | netif_carrier_off(slave_dev); |
| 1316 | |
| 1317 | ret = dsa_slave_phy_setup(p, slave_dev); |
| 1318 | if (ret) { |
| 1319 | netdev_err(master, "error %d setting up slave phy\n", ret); |
| 1320 | goto out_free; |
| 1321 | } |
| 1322 | |
| 1323 | ret = register_netdev(slave_dev); |
| 1324 | if (ret) { |
| 1325 | netdev_err(master, "error %d registering interface %s\n", |
| 1326 | ret, slave_dev->name); |
| 1327 | goto out_phy; |
| 1328 | } |
| 1329 | |
| 1330 | return 0; |
| 1331 | |
| 1332 | out_phy: |
| 1333 | phy_disconnect(p->phy); |
| 1334 | if (of_phy_is_fixed_link(p->dp->dn)) |
| 1335 | of_phy_deregister_fixed_link(p->dp->dn); |
| 1336 | out_free: |
| 1337 | free_percpu(p->stats64); |
| 1338 | free_netdev(slave_dev); |
| 1339 | port->netdev = NULL; |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | void dsa_slave_destroy(struct net_device *slave_dev) |
| 1344 | { |
| 1345 | struct dsa_slave_priv *p = netdev_priv(slave_dev); |
| 1346 | struct device_node *port_dn; |
| 1347 | |
| 1348 | port_dn = p->dp->dn; |
| 1349 | |
| 1350 | netif_carrier_off(slave_dev); |
| 1351 | if (p->phy) { |
| 1352 | phy_disconnect(p->phy); |
| 1353 | |
| 1354 | if (of_phy_is_fixed_link(port_dn)) |
| 1355 | of_phy_deregister_fixed_link(port_dn); |
| 1356 | } |
| 1357 | unregister_netdev(slave_dev); |
| 1358 | free_percpu(p->stats64); |
| 1359 | free_netdev(slave_dev); |
| 1360 | } |
| 1361 | |
| 1362 | static bool dsa_slave_dev_check(struct net_device *dev) |
| 1363 | { |
| 1364 | return dev->netdev_ops == &dsa_slave_netdev_ops; |
| 1365 | } |
| 1366 | |
| 1367 | static int dsa_slave_changeupper(struct net_device *dev, |
| 1368 | struct netdev_notifier_changeupper_info *info) |
| 1369 | { |
| 1370 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1371 | struct dsa_port *dp = p->dp; |
| 1372 | int err = NOTIFY_DONE; |
| 1373 | |
| 1374 | if (netif_is_bridge_master(info->upper_dev)) { |
| 1375 | if (info->linking) { |
| 1376 | err = dsa_port_bridge_join(dp, info->upper_dev); |
| 1377 | err = notifier_from_errno(err); |
| 1378 | } else { |
| 1379 | dsa_port_bridge_leave(dp, info->upper_dev); |
| 1380 | err = NOTIFY_OK; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | return err; |
| 1385 | } |
| 1386 | |
| 1387 | static int dsa_slave_netdevice_event(struct notifier_block *nb, |
| 1388 | unsigned long event, void *ptr) |
| 1389 | { |
| 1390 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1391 | |
| 1392 | if (dev->netdev_ops != &dsa_slave_netdev_ops) |
| 1393 | return NOTIFY_DONE; |
| 1394 | |
| 1395 | if (event == NETDEV_CHANGEUPPER) |
| 1396 | return dsa_slave_changeupper(dev, ptr); |
| 1397 | |
| 1398 | return NOTIFY_DONE; |
| 1399 | } |
| 1400 | |
| 1401 | struct dsa_switchdev_event_work { |
| 1402 | struct work_struct work; |
| 1403 | struct switchdev_notifier_fdb_info fdb_info; |
| 1404 | struct net_device *dev; |
| 1405 | unsigned long event; |
| 1406 | }; |
| 1407 | |
| 1408 | static void dsa_slave_switchdev_event_work(struct work_struct *work) |
| 1409 | { |
| 1410 | struct dsa_switchdev_event_work *switchdev_work = |
| 1411 | container_of(work, struct dsa_switchdev_event_work, work); |
| 1412 | struct net_device *dev = switchdev_work->dev; |
| 1413 | struct switchdev_notifier_fdb_info *fdb_info; |
| 1414 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 1415 | int err; |
| 1416 | |
| 1417 | rtnl_lock(); |
| 1418 | switch (switchdev_work->event) { |
| 1419 | case SWITCHDEV_FDB_ADD_TO_DEVICE: |
| 1420 | fdb_info = &switchdev_work->fdb_info; |
| 1421 | err = dsa_port_fdb_add(p->dp, fdb_info->addr, fdb_info->vid); |
| 1422 | if (err) { |
| 1423 | netdev_dbg(dev, "fdb add failed err=%d\n", err); |
| 1424 | break; |
| 1425 | } |
| 1426 | call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, |
| 1427 | &fdb_info->info); |
| 1428 | break; |
| 1429 | |
| 1430 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1431 | fdb_info = &switchdev_work->fdb_info; |
| 1432 | err = dsa_port_fdb_del(p->dp, fdb_info->addr, fdb_info->vid); |
| 1433 | if (err) { |
| 1434 | netdev_dbg(dev, "fdb del failed err=%d\n", err); |
| 1435 | dev_close(dev); |
| 1436 | } |
| 1437 | break; |
| 1438 | } |
| 1439 | rtnl_unlock(); |
| 1440 | |
| 1441 | kfree(switchdev_work->fdb_info.addr); |
| 1442 | kfree(switchdev_work); |
| 1443 | dev_put(dev); |
| 1444 | } |
| 1445 | |
| 1446 | static int |
| 1447 | dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work * |
| 1448 | switchdev_work, |
| 1449 | const struct switchdev_notifier_fdb_info * |
| 1450 | fdb_info) |
| 1451 | { |
| 1452 | memcpy(&switchdev_work->fdb_info, fdb_info, |
| 1453 | sizeof(switchdev_work->fdb_info)); |
| 1454 | switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); |
| 1455 | if (!switchdev_work->fdb_info.addr) |
| 1456 | return -ENOMEM; |
| 1457 | ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, |
| 1458 | fdb_info->addr); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* Called under rcu_read_lock() */ |
| 1463 | static int dsa_slave_switchdev_event(struct notifier_block *unused, |
| 1464 | unsigned long event, void *ptr) |
| 1465 | { |
| 1466 | struct net_device *dev = switchdev_notifier_info_to_dev(ptr); |
| 1467 | struct dsa_switchdev_event_work *switchdev_work; |
| 1468 | |
| 1469 | if (!dsa_slave_dev_check(dev)) |
| 1470 | return NOTIFY_DONE; |
| 1471 | |
| 1472 | switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); |
| 1473 | if (!switchdev_work) |
| 1474 | return NOTIFY_BAD; |
| 1475 | |
| 1476 | INIT_WORK(&switchdev_work->work, |
| 1477 | dsa_slave_switchdev_event_work); |
| 1478 | switchdev_work->dev = dev; |
| 1479 | switchdev_work->event = event; |
| 1480 | |
| 1481 | switch (event) { |
| 1482 | case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */ |
| 1483 | case SWITCHDEV_FDB_DEL_TO_DEVICE: |
| 1484 | if (dsa_slave_switchdev_fdb_work_init(switchdev_work, |
| 1485 | ptr)) |
| 1486 | goto err_fdb_work_init; |
| 1487 | dev_hold(dev); |
| 1488 | break; |
| 1489 | default: |
| 1490 | kfree(switchdev_work); |
| 1491 | return NOTIFY_DONE; |
| 1492 | } |
| 1493 | |
| 1494 | dsa_schedule_work(&switchdev_work->work); |
| 1495 | return NOTIFY_OK; |
| 1496 | |
| 1497 | err_fdb_work_init: |
| 1498 | kfree(switchdev_work); |
| 1499 | return NOTIFY_BAD; |
| 1500 | } |
| 1501 | |
| 1502 | static struct notifier_block dsa_slave_nb __read_mostly = { |
| 1503 | .notifier_call = dsa_slave_netdevice_event, |
| 1504 | }; |
| 1505 | |
| 1506 | static struct notifier_block dsa_slave_switchdev_notifier = { |
| 1507 | .notifier_call = dsa_slave_switchdev_event, |
| 1508 | }; |
| 1509 | |
| 1510 | int dsa_slave_register_notifier(void) |
| 1511 | { |
| 1512 | int err; |
| 1513 | |
| 1514 | err = register_netdevice_notifier(&dsa_slave_nb); |
| 1515 | if (err) |
| 1516 | return err; |
| 1517 | |
| 1518 | err = register_switchdev_notifier(&dsa_slave_switchdev_notifier); |
| 1519 | if (err) |
| 1520 | goto err_switchdev_nb; |
| 1521 | |
| 1522 | return 0; |
| 1523 | |
| 1524 | err_switchdev_nb: |
| 1525 | unregister_netdevice_notifier(&dsa_slave_nb); |
| 1526 | return err; |
| 1527 | } |
| 1528 | |
| 1529 | void dsa_slave_unregister_notifier(void) |
| 1530 | { |
| 1531 | int err; |
| 1532 | |
| 1533 | err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier); |
| 1534 | if (err) |
| 1535 | pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err); |
| 1536 | |
| 1537 | err = unregister_netdevice_notifier(&dsa_slave_nb); |
| 1538 | if (err) |
| 1539 | pr_err("DSA: failed to unregister slave notifier (%d)\n", err); |
| 1540 | } |