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