linux/drivers/staging/fsl-dpaa2/ethsw/ethsw.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * DPAA2 Ethernet Switch driver
   4 *
   5 * Copyright 2014-2016 Freescale Semiconductor Inc.
   6 * Copyright 2017-2018 NXP
   7 *
   8 */
   9
  10#include <linux/module.h>
  11
  12#include <linux/interrupt.h>
  13#include <linux/msi.h>
  14#include <linux/kthread.h>
  15#include <linux/workqueue.h>
  16
  17#include <linux/fsl/mc.h>
  18
  19#include "ethsw.h"
  20
  21static struct workqueue_struct *ethsw_owq;
  22
  23/* Minimal supported DPSW version */
  24#define DPSW_MIN_VER_MAJOR              8
  25#define DPSW_MIN_VER_MINOR              1
  26
  27#define DEFAULT_VLAN_ID                 1
  28
  29static int ethsw_add_vlan(struct ethsw_core *ethsw, u16 vid)
  30{
  31        int err;
  32
  33        struct dpsw_vlan_cfg    vcfg = {
  34                .fdb_id = 0,
  35        };
  36
  37        err = dpsw_vlan_add(ethsw->mc_io, 0,
  38                            ethsw->dpsw_handle, vid, &vcfg);
  39        if (err) {
  40                dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
  41                return err;
  42        }
  43        ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
  44
  45        return 0;
  46}
  47
  48static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
  49{
  50        struct ethsw_core *ethsw = port_priv->ethsw_data;
  51        struct net_device *netdev = port_priv->netdev;
  52        struct dpsw_tci_cfg tci_cfg = { 0 };
  53        bool is_oper;
  54        int err, ret;
  55
  56        err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
  57                              port_priv->idx, &tci_cfg);
  58        if (err) {
  59                netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
  60                return err;
  61        }
  62
  63        tci_cfg.vlan_id = pvid;
  64
  65        /* Interface needs to be down to change PVID */
  66        is_oper = netif_oper_up(netdev);
  67        if (is_oper) {
  68                err = dpsw_if_disable(ethsw->mc_io, 0,
  69                                      ethsw->dpsw_handle,
  70                                      port_priv->idx);
  71                if (err) {
  72                        netdev_err(netdev, "dpsw_if_disable err %d\n", err);
  73                        return err;
  74                }
  75        }
  76
  77        err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
  78                              port_priv->idx, &tci_cfg);
  79        if (err) {
  80                netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
  81                goto set_tci_error;
  82        }
  83
  84        /* Delete previous PVID info and mark the new one */
  85        port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
  86        port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
  87        port_priv->pvid = pvid;
  88
  89set_tci_error:
  90        if (is_oper) {
  91                ret = dpsw_if_enable(ethsw->mc_io, 0,
  92                                     ethsw->dpsw_handle,
  93                                     port_priv->idx);
  94                if (ret) {
  95                        netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
  96                        return ret;
  97                }
  98        }
  99
 100        return err;
 101}
 102
 103static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
 104                               u16 vid, u16 flags)
 105{
 106        struct ethsw_core *ethsw = port_priv->ethsw_data;
 107        struct net_device *netdev = port_priv->netdev;
 108        struct dpsw_vlan_if_cfg vcfg;
 109        int err;
 110
 111        if (port_priv->vlans[vid]) {
 112                netdev_warn(netdev, "VLAN %d already configured\n", vid);
 113                return -EEXIST;
 114        }
 115
 116        vcfg.num_ifs = 1;
 117        vcfg.if_id[0] = port_priv->idx;
 118        err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
 119        if (err) {
 120                netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
 121                return err;
 122        }
 123
 124        port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
 125
 126        if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
 127                err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
 128                                                ethsw->dpsw_handle,
 129                                                vid, &vcfg);
 130                if (err) {
 131                        netdev_err(netdev,
 132                                   "dpsw_vlan_add_if_untagged err %d\n", err);
 133                        return err;
 134                }
 135                port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
 136        }
 137
 138        if (flags & BRIDGE_VLAN_INFO_PVID) {
 139                err = ethsw_port_set_pvid(port_priv, vid);
 140                if (err)
 141                        return err;
 142        }
 143
 144        return 0;
 145}
 146
 147static int ethsw_set_learning(struct ethsw_core *ethsw, bool enable)
 148{
 149        enum dpsw_fdb_learning_mode learn_mode;
 150        int err;
 151
 152        if (enable)
 153                learn_mode = DPSW_FDB_LEARNING_MODE_HW;
 154        else
 155                learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
 156
 157        err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
 158                                         learn_mode);
 159        if (err) {
 160                dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
 161                return err;
 162        }
 163        ethsw->learning = enable;
 164
 165        return 0;
 166}
 167
 168static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, bool enable)
 169{
 170        int err;
 171
 172        err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
 173                                   port_priv->ethsw_data->dpsw_handle,
 174                                   port_priv->idx, enable);
 175        if (err) {
 176                netdev_err(port_priv->netdev,
 177                           "dpsw_if_set_flooding err %d\n", err);
 178                return err;
 179        }
 180        port_priv->flood = enable;
 181
 182        return 0;
 183}
 184
 185static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
 186{
 187        struct dpsw_stp_cfg stp_cfg = {
 188                .vlan_id = DEFAULT_VLAN_ID,
 189                .state = state,
 190        };
 191        int err;
 192
 193        if (!netif_oper_up(port_priv->netdev) || state == port_priv->stp_state)
 194                return 0;       /* Nothing to do */
 195
 196        err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
 197                              port_priv->ethsw_data->dpsw_handle,
 198                              port_priv->idx, &stp_cfg);
 199        if (err) {
 200                netdev_err(port_priv->netdev,
 201                           "dpsw_if_set_stp err %d\n", err);
 202                return err;
 203        }
 204
 205        port_priv->stp_state = state;
 206
 207        return 0;
 208}
 209
 210static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
 211{
 212        struct ethsw_port_priv *ppriv_local = NULL;
 213        int i, err;
 214
 215        if (!ethsw->vlans[vid])
 216                return -ENOENT;
 217
 218        err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
 219        if (err) {
 220                dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
 221                return err;
 222        }
 223        ethsw->vlans[vid] = 0;
 224
 225        for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
 226                ppriv_local = ethsw->ports[i];
 227                ppriv_local->vlans[vid] = 0;
 228        }
 229
 230        return 0;
 231}
 232
 233static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
 234                                 const unsigned char *addr)
 235{
 236        struct dpsw_fdb_unicast_cfg entry = {0};
 237        int err;
 238
 239        entry.if_egress = port_priv->idx;
 240        entry.type = DPSW_FDB_ENTRY_STATIC;
 241        ether_addr_copy(entry.mac_addr, addr);
 242
 243        err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
 244                                   port_priv->ethsw_data->dpsw_handle,
 245                                   0, &entry);
 246        if (err)
 247                netdev_err(port_priv->netdev,
 248                           "dpsw_fdb_add_unicast err %d\n", err);
 249        return err;
 250}
 251
 252static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
 253                                 const unsigned char *addr)
 254{
 255        struct dpsw_fdb_unicast_cfg entry = {0};
 256        int err;
 257
 258        entry.if_egress = port_priv->idx;
 259        entry.type = DPSW_FDB_ENTRY_STATIC;
 260        ether_addr_copy(entry.mac_addr, addr);
 261
 262        err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
 263                                      port_priv->ethsw_data->dpsw_handle,
 264                                      0, &entry);
 265        /* Silently discard error for calling multiple times the del command */
 266        if (err && err != -ENXIO)
 267                netdev_err(port_priv->netdev,
 268                           "dpsw_fdb_remove_unicast err %d\n", err);
 269        return err;
 270}
 271
 272static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
 273                                 const unsigned char *addr)
 274{
 275        struct dpsw_fdb_multicast_cfg entry = {0};
 276        int err;
 277
 278        ether_addr_copy(entry.mac_addr, addr);
 279        entry.type = DPSW_FDB_ENTRY_STATIC;
 280        entry.num_ifs = 1;
 281        entry.if_id[0] = port_priv->idx;
 282
 283        err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
 284                                     port_priv->ethsw_data->dpsw_handle,
 285                                     0, &entry);
 286        /* Silently discard error for calling multiple times the add command */
 287        if (err && err != -ENXIO)
 288                netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
 289                           err);
 290        return err;
 291}
 292
 293static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
 294                                 const unsigned char *addr)
 295{
 296        struct dpsw_fdb_multicast_cfg entry = {0};
 297        int err;
 298
 299        ether_addr_copy(entry.mac_addr, addr);
 300        entry.type = DPSW_FDB_ENTRY_STATIC;
 301        entry.num_ifs = 1;
 302        entry.if_id[0] = port_priv->idx;
 303
 304        err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
 305                                        port_priv->ethsw_data->dpsw_handle,
 306                                        0, &entry);
 307        /* Silently discard error for calling multiple times the del command */
 308        if (err && err != -ENAVAIL)
 309                netdev_err(port_priv->netdev,
 310                           "dpsw_fdb_remove_multicast err %d\n", err);
 311        return err;
 312}
 313
 314static int port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 315                        struct net_device *dev, const unsigned char *addr,
 316                        u16 vid, u16 flags,
 317                        struct netlink_ext_ack *extack)
 318{
 319        if (is_unicast_ether_addr(addr))
 320                return ethsw_port_fdb_add_uc(netdev_priv(dev),
 321                                             addr);
 322        else
 323                return ethsw_port_fdb_add_mc(netdev_priv(dev),
 324                                             addr);
 325}
 326
 327static int port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
 328                        struct net_device *dev,
 329                        const unsigned char *addr, u16 vid)
 330{
 331        if (is_unicast_ether_addr(addr))
 332                return ethsw_port_fdb_del_uc(netdev_priv(dev),
 333                                             addr);
 334        else
 335                return ethsw_port_fdb_del_mc(netdev_priv(dev),
 336                                             addr);
 337}
 338
 339static void port_get_stats(struct net_device *netdev,
 340                           struct rtnl_link_stats64 *stats)
 341{
 342        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 343        u64 tmp;
 344        int err;
 345
 346        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 347                                  port_priv->ethsw_data->dpsw_handle,
 348                                  port_priv->idx,
 349                                  DPSW_CNT_ING_FRAME, &stats->rx_packets);
 350        if (err)
 351                goto error;
 352
 353        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 354                                  port_priv->ethsw_data->dpsw_handle,
 355                                  port_priv->idx,
 356                                  DPSW_CNT_EGR_FRAME, &stats->tx_packets);
 357        if (err)
 358                goto error;
 359
 360        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 361                                  port_priv->ethsw_data->dpsw_handle,
 362                                  port_priv->idx,
 363                                  DPSW_CNT_ING_BYTE, &stats->rx_bytes);
 364        if (err)
 365                goto error;
 366
 367        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 368                                  port_priv->ethsw_data->dpsw_handle,
 369                                  port_priv->idx,
 370                                  DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
 371        if (err)
 372                goto error;
 373
 374        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 375                                  port_priv->ethsw_data->dpsw_handle,
 376                                  port_priv->idx,
 377                                  DPSW_CNT_ING_FRAME_DISCARD,
 378                                  &stats->rx_dropped);
 379        if (err)
 380                goto error;
 381
 382        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 383                                  port_priv->ethsw_data->dpsw_handle,
 384                                  port_priv->idx,
 385                                  DPSW_CNT_ING_FLTR_FRAME,
 386                                  &tmp);
 387        if (err)
 388                goto error;
 389        stats->rx_dropped += tmp;
 390
 391        err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
 392                                  port_priv->ethsw_data->dpsw_handle,
 393                                  port_priv->idx,
 394                                  DPSW_CNT_EGR_FRAME_DISCARD,
 395                                  &stats->tx_dropped);
 396        if (err)
 397                goto error;
 398
 399        return;
 400
 401error:
 402        netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
 403}
 404
 405static bool port_has_offload_stats(const struct net_device *netdev,
 406                                   int attr_id)
 407{
 408        return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
 409}
 410
 411static int port_get_offload_stats(int attr_id,
 412                                  const struct net_device *netdev,
 413                                  void *sp)
 414{
 415        switch (attr_id) {
 416        case IFLA_OFFLOAD_XSTATS_CPU_HIT:
 417                port_get_stats((struct net_device *)netdev, sp);
 418                return 0;
 419        }
 420
 421        return -EINVAL;
 422}
 423
 424static int port_change_mtu(struct net_device *netdev, int mtu)
 425{
 426        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 427        int err;
 428
 429        err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
 430                                           0,
 431                                           port_priv->ethsw_data->dpsw_handle,
 432                                           port_priv->idx,
 433                                           (u16)ETHSW_L2_MAX_FRM(mtu));
 434        if (err) {
 435                netdev_err(netdev,
 436                           "dpsw_if_set_max_frame_length() err %d\n", err);
 437                return err;
 438        }
 439
 440        netdev->mtu = mtu;
 441        return 0;
 442}
 443
 444static int port_carrier_state_sync(struct net_device *netdev)
 445{
 446        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 447        struct dpsw_link_state state;
 448        int err;
 449
 450        err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
 451                                     port_priv->ethsw_data->dpsw_handle,
 452                                     port_priv->idx, &state);
 453        if (err) {
 454                netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
 455                return err;
 456        }
 457
 458        WARN_ONCE(state.up > 1, "Garbage read into link_state");
 459
 460        if (state.up != port_priv->link_state) {
 461                if (state.up)
 462                        netif_carrier_on(netdev);
 463                else
 464                        netif_carrier_off(netdev);
 465                port_priv->link_state = state.up;
 466        }
 467        return 0;
 468}
 469
 470static int port_open(struct net_device *netdev)
 471{
 472        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 473        int err;
 474
 475        /* No need to allow Tx as control interface is disabled */
 476        netif_tx_stop_all_queues(netdev);
 477
 478        err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
 479                             port_priv->ethsw_data->dpsw_handle,
 480                             port_priv->idx);
 481        if (err) {
 482                netdev_err(netdev, "dpsw_if_enable err %d\n", err);
 483                return err;
 484        }
 485
 486        /* sync carrier state */
 487        err = port_carrier_state_sync(netdev);
 488        if (err) {
 489                netdev_err(netdev,
 490                           "port_carrier_state_sync err %d\n", err);
 491                goto err_carrier_sync;
 492        }
 493
 494        return 0;
 495
 496err_carrier_sync:
 497        dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
 498                        port_priv->ethsw_data->dpsw_handle,
 499                        port_priv->idx);
 500        return err;
 501}
 502
 503static int port_stop(struct net_device *netdev)
 504{
 505        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 506        int err;
 507
 508        err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
 509                              port_priv->ethsw_data->dpsw_handle,
 510                              port_priv->idx);
 511        if (err) {
 512                netdev_err(netdev, "dpsw_if_disable err %d\n", err);
 513                return err;
 514        }
 515
 516        return 0;
 517}
 518
 519static netdev_tx_t port_dropframe(struct sk_buff *skb,
 520                                  struct net_device *netdev)
 521{
 522        /* we don't support I/O for now, drop the frame */
 523        dev_kfree_skb_any(skb);
 524
 525        return NETDEV_TX_OK;
 526}
 527
 528static int swdev_get_port_parent_id(struct net_device *dev,
 529                                    struct netdev_phys_item_id *ppid)
 530{
 531        struct ethsw_port_priv *port_priv = netdev_priv(dev);
 532
 533        ppid->id_len = 1;
 534        ppid->id[0] = port_priv->ethsw_data->dev_id;
 535
 536        return 0;
 537}
 538
 539static int port_get_phys_name(struct net_device *netdev, char *name,
 540                              size_t len)
 541{
 542        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 543        int err;
 544
 545        err = snprintf(name, len, "p%d", port_priv->idx);
 546        if (err >= len)
 547                return -EINVAL;
 548
 549        return 0;
 550}
 551
 552struct ethsw_dump_ctx {
 553        struct net_device *dev;
 554        struct sk_buff *skb;
 555        struct netlink_callback *cb;
 556        int idx;
 557};
 558
 559static int ethsw_fdb_do_dump(struct fdb_dump_entry *entry,
 560                             struct ethsw_dump_ctx *dump)
 561{
 562        int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
 563        u32 portid = NETLINK_CB(dump->cb->skb).portid;
 564        u32 seq = dump->cb->nlh->nlmsg_seq;
 565        struct nlmsghdr *nlh;
 566        struct ndmsg *ndm;
 567
 568        if (dump->idx < dump->cb->args[2])
 569                goto skip;
 570
 571        nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
 572                        sizeof(*ndm), NLM_F_MULTI);
 573        if (!nlh)
 574                return -EMSGSIZE;
 575
 576        ndm = nlmsg_data(nlh);
 577        ndm->ndm_family  = AF_BRIDGE;
 578        ndm->ndm_pad1    = 0;
 579        ndm->ndm_pad2    = 0;
 580        ndm->ndm_flags   = NTF_SELF;
 581        ndm->ndm_type    = 0;
 582        ndm->ndm_ifindex = dump->dev->ifindex;
 583        ndm->ndm_state   = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
 584
 585        if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
 586                goto nla_put_failure;
 587
 588        nlmsg_end(dump->skb, nlh);
 589
 590skip:
 591        dump->idx++;
 592        return 0;
 593
 594nla_put_failure:
 595        nlmsg_cancel(dump->skb, nlh);
 596        return -EMSGSIZE;
 597}
 598
 599static int port_fdb_valid_entry(struct fdb_dump_entry *entry,
 600                                struct ethsw_port_priv *port_priv)
 601{
 602        int idx = port_priv->idx;
 603        int valid;
 604
 605        if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
 606                valid = entry->if_info == port_priv->idx;
 607        else
 608                valid = entry->if_mask[idx / 8] & BIT(idx % 8);
 609
 610        return valid;
 611}
 612
 613static int port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
 614                         struct net_device *net_dev,
 615                         struct net_device *filter_dev, int *idx)
 616{
 617        struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
 618        struct ethsw_core *ethsw = port_priv->ethsw_data;
 619        struct device *dev = net_dev->dev.parent;
 620        struct fdb_dump_entry *fdb_entries;
 621        struct fdb_dump_entry fdb_entry;
 622        struct ethsw_dump_ctx dump = {
 623                .dev = net_dev,
 624                .skb = skb,
 625                .cb = cb,
 626                .idx = *idx,
 627        };
 628        dma_addr_t fdb_dump_iova;
 629        u16 num_fdb_entries;
 630        u32 fdb_dump_size;
 631        int err = 0, i;
 632        u8 *dma_mem;
 633
 634        fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
 635        dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
 636        if (!dma_mem)
 637                return -ENOMEM;
 638
 639        fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
 640                                       DMA_FROM_DEVICE);
 641        if (dma_mapping_error(dev, fdb_dump_iova)) {
 642                netdev_err(net_dev, "dma_map_single() failed\n");
 643                err = -ENOMEM;
 644                goto err_map;
 645        }
 646
 647        err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
 648                            fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
 649        if (err) {
 650                netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
 651                goto err_dump;
 652        }
 653
 654        dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
 655
 656        fdb_entries = (struct fdb_dump_entry *)dma_mem;
 657        for (i = 0; i < num_fdb_entries; i++) {
 658                fdb_entry = fdb_entries[i];
 659
 660                if (!port_fdb_valid_entry(&fdb_entry, port_priv))
 661                        continue;
 662
 663                err = ethsw_fdb_do_dump(&fdb_entry, &dump);
 664                if (err)
 665                        goto end;
 666        }
 667
 668end:
 669        *idx = dump.idx;
 670
 671        kfree(dma_mem);
 672
 673        return 0;
 674
 675err_dump:
 676        dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
 677err_map:
 678        kfree(dma_mem);
 679        return err;
 680}
 681
 682static const struct net_device_ops ethsw_port_ops = {
 683        .ndo_open               = port_open,
 684        .ndo_stop               = port_stop,
 685
 686        .ndo_set_mac_address    = eth_mac_addr,
 687        .ndo_get_stats64        = port_get_stats,
 688        .ndo_change_mtu         = port_change_mtu,
 689        .ndo_has_offload_stats  = port_has_offload_stats,
 690        .ndo_get_offload_stats  = port_get_offload_stats,
 691        .ndo_fdb_add            = port_fdb_add,
 692        .ndo_fdb_del            = port_fdb_del,
 693        .ndo_fdb_dump           = port_fdb_dump,
 694
 695        .ndo_start_xmit         = port_dropframe,
 696        .ndo_get_port_parent_id = swdev_get_port_parent_id,
 697        .ndo_get_phys_port_name = port_get_phys_name,
 698};
 699
 700static void ethsw_links_state_update(struct ethsw_core *ethsw)
 701{
 702        int i;
 703
 704        for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
 705                port_carrier_state_sync(ethsw->ports[i]->netdev);
 706}
 707
 708static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
 709{
 710        struct device *dev = (struct device *)arg;
 711        struct ethsw_core *ethsw = dev_get_drvdata(dev);
 712
 713        /* Mask the events and the if_id reserved bits to be cleared on read */
 714        u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
 715        int err;
 716
 717        err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
 718                                  DPSW_IRQ_INDEX_IF, &status);
 719        if (err) {
 720                dev_err(dev, "Can't get irq status (err %d)\n", err);
 721
 722                err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
 723                                            DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
 724                if (err)
 725                        dev_err(dev, "Can't clear irq status (err %d)\n", err);
 726                goto out;
 727        }
 728
 729        if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
 730                ethsw_links_state_update(ethsw);
 731
 732out:
 733        return IRQ_HANDLED;
 734}
 735
 736static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
 737{
 738        struct device *dev = &sw_dev->dev;
 739        struct ethsw_core *ethsw = dev_get_drvdata(dev);
 740        u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
 741        struct fsl_mc_device_irq *irq;
 742        int err;
 743
 744        err = fsl_mc_allocate_irqs(sw_dev);
 745        if (err) {
 746                dev_err(dev, "MC irqs allocation failed\n");
 747                return err;
 748        }
 749
 750        if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
 751                err = -EINVAL;
 752                goto free_irq;
 753        }
 754
 755        err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
 756                                  DPSW_IRQ_INDEX_IF, 0);
 757        if (err) {
 758                dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
 759                goto free_irq;
 760        }
 761
 762        irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
 763
 764        err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
 765                                        NULL,
 766                                        ethsw_irq0_handler_thread,
 767                                        IRQF_NO_SUSPEND | IRQF_ONESHOT,
 768                                        dev_name(dev), dev);
 769        if (err) {
 770                dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
 771                goto free_irq;
 772        }
 773
 774        err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
 775                                DPSW_IRQ_INDEX_IF, mask);
 776        if (err) {
 777                dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
 778                goto free_devm_irq;
 779        }
 780
 781        err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
 782                                  DPSW_IRQ_INDEX_IF, 1);
 783        if (err) {
 784                dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
 785                goto free_devm_irq;
 786        }
 787
 788        return 0;
 789
 790free_devm_irq:
 791        devm_free_irq(dev, irq->msi_desc->irq, dev);
 792free_irq:
 793        fsl_mc_free_irqs(sw_dev);
 794        return err;
 795}
 796
 797static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
 798{
 799        struct device *dev = &sw_dev->dev;
 800        struct ethsw_core *ethsw = dev_get_drvdata(dev);
 801        int err;
 802
 803        err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
 804                                  DPSW_IRQ_INDEX_IF, 0);
 805        if (err)
 806                dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
 807
 808        fsl_mc_free_irqs(sw_dev);
 809}
 810
 811static int port_attr_stp_state_set(struct net_device *netdev,
 812                                   struct switchdev_trans *trans,
 813                                   u8 state)
 814{
 815        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 816
 817        if (switchdev_trans_ph_prepare(trans))
 818                return 0;
 819
 820        return ethsw_port_set_stp_state(port_priv, state);
 821}
 822
 823static int port_attr_br_flags_pre_set(struct net_device *netdev,
 824                                      struct switchdev_trans *trans,
 825                                      unsigned long flags)
 826{
 827        if (flags & ~(BR_LEARNING | BR_FLOOD))
 828                return -EINVAL;
 829
 830        return 0;
 831}
 832
 833static int port_attr_br_flags_set(struct net_device *netdev,
 834                                  struct switchdev_trans *trans,
 835                                  unsigned long flags)
 836{
 837        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 838        int err = 0;
 839
 840        if (switchdev_trans_ph_prepare(trans))
 841                return 0;
 842
 843        /* Learning is enabled per switch */
 844        err = ethsw_set_learning(port_priv->ethsw_data,
 845                                 !!(flags & BR_LEARNING));
 846        if (err)
 847                goto exit;
 848
 849        err = ethsw_port_set_flood(port_priv, !!(flags & BR_FLOOD));
 850
 851exit:
 852        return err;
 853}
 854
 855static int swdev_port_attr_set(struct net_device *netdev,
 856                               const struct switchdev_attr *attr,
 857                               struct switchdev_trans *trans)
 858{
 859        int err = 0;
 860
 861        switch (attr->id) {
 862        case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
 863                err = port_attr_stp_state_set(netdev, trans,
 864                                              attr->u.stp_state);
 865                break;
 866        case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
 867                err = port_attr_br_flags_pre_set(netdev, trans,
 868                                                 attr->u.brport_flags);
 869                break;
 870        case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
 871                err = port_attr_br_flags_set(netdev, trans,
 872                                             attr->u.brport_flags);
 873                break;
 874        case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
 875                /* VLANs are supported by default  */
 876                break;
 877        default:
 878                err = -EOPNOTSUPP;
 879                break;
 880        }
 881
 882        return err;
 883}
 884
 885static int port_vlans_add(struct net_device *netdev,
 886                          const struct switchdev_obj_port_vlan *vlan,
 887                          struct switchdev_trans *trans)
 888{
 889        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 890        int vid, err = 0;
 891
 892        if (switchdev_trans_ph_prepare(trans))
 893                return 0;
 894
 895        for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
 896                if (!port_priv->ethsw_data->vlans[vid]) {
 897                        /* this is a new VLAN */
 898                        err = ethsw_add_vlan(port_priv->ethsw_data, vid);
 899                        if (err)
 900                                return err;
 901
 902                        port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
 903                }
 904                err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
 905                if (err)
 906                        break;
 907        }
 908
 909        return err;
 910}
 911
 912static int port_lookup_address(struct net_device *netdev, int is_uc,
 913                               const unsigned char *addr)
 914{
 915        struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
 916        struct netdev_hw_addr *ha;
 917
 918        netif_addr_lock_bh(netdev);
 919        list_for_each_entry(ha, &list->list, list) {
 920                if (ether_addr_equal(ha->addr, addr)) {
 921                        netif_addr_unlock_bh(netdev);
 922                        return 1;
 923                }
 924        }
 925        netif_addr_unlock_bh(netdev);
 926        return 0;
 927}
 928
 929static int port_mdb_add(struct net_device *netdev,
 930                        const struct switchdev_obj_port_mdb *mdb,
 931                        struct switchdev_trans *trans)
 932{
 933        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
 934        int err;
 935
 936        if (switchdev_trans_ph_prepare(trans))
 937                return 0;
 938
 939        /* Check if address is already set on this port */
 940        if (port_lookup_address(netdev, 0, mdb->addr))
 941                return -EEXIST;
 942
 943        err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
 944        if (err)
 945                return err;
 946
 947        err = dev_mc_add(netdev, mdb->addr);
 948        if (err) {
 949                netdev_err(netdev, "dev_mc_add err %d\n", err);
 950                ethsw_port_fdb_del_mc(port_priv, mdb->addr);
 951        }
 952
 953        return err;
 954}
 955
 956static int swdev_port_obj_add(struct net_device *netdev,
 957                              const struct switchdev_obj *obj,
 958                              struct switchdev_trans *trans)
 959{
 960        int err;
 961
 962        switch (obj->id) {
 963        case SWITCHDEV_OBJ_ID_PORT_VLAN:
 964                err = port_vlans_add(netdev,
 965                                     SWITCHDEV_OBJ_PORT_VLAN(obj),
 966                                     trans);
 967                break;
 968        case SWITCHDEV_OBJ_ID_PORT_MDB:
 969                err = port_mdb_add(netdev,
 970                                   SWITCHDEV_OBJ_PORT_MDB(obj),
 971                                   trans);
 972                break;
 973        default:
 974                err = -EOPNOTSUPP;
 975                break;
 976        }
 977
 978        return err;
 979}
 980
 981static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
 982{
 983        struct ethsw_core *ethsw = port_priv->ethsw_data;
 984        struct net_device *netdev = port_priv->netdev;
 985        struct dpsw_vlan_if_cfg vcfg;
 986        int i, err;
 987
 988        if (!port_priv->vlans[vid])
 989                return -ENOENT;
 990
 991        if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
 992                err = ethsw_port_set_pvid(port_priv, 0);
 993                if (err)
 994                        return err;
 995        }
 996
 997        vcfg.num_ifs = 1;
 998        vcfg.if_id[0] = port_priv->idx;
 999        if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1000                err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1001                                                   ethsw->dpsw_handle,
1002                                                   vid, &vcfg);
1003                if (err) {
1004                        netdev_err(netdev,
1005                                   "dpsw_vlan_remove_if_untagged err %d\n",
1006                                   err);
1007                }
1008                port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1009        }
1010
1011        if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1012                err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1013                                          vid, &vcfg);
1014                if (err) {
1015                        netdev_err(netdev,
1016                                   "dpsw_vlan_remove_if err %d\n", err);
1017                        return err;
1018                }
1019                port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1020
1021                /* Delete VLAN from switch if it is no longer configured on
1022                 * any port
1023                 */
1024                for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1025                        if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1026                                return 0; /* Found a port member in VID */
1027
1028                ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1029
1030                err = ethsw_dellink_switch(ethsw, vid);
1031                if (err)
1032                        return err;
1033        }
1034
1035        return 0;
1036}
1037
1038static int port_vlans_del(struct net_device *netdev,
1039                          const struct switchdev_obj_port_vlan *vlan)
1040{
1041        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1042        int vid, err = 0;
1043
1044        if (netif_is_bridge_master(vlan->obj.orig_dev))
1045                return -EOPNOTSUPP;
1046
1047        for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1048                err = ethsw_port_del_vlan(port_priv, vid);
1049                if (err)
1050                        break;
1051        }
1052
1053        return err;
1054}
1055
1056static int port_mdb_del(struct net_device *netdev,
1057                        const struct switchdev_obj_port_mdb *mdb)
1058{
1059        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1060        int err;
1061
1062        if (!port_lookup_address(netdev, 0, mdb->addr))
1063                return -ENOENT;
1064
1065        err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1066        if (err)
1067                return err;
1068
1069        err = dev_mc_del(netdev, mdb->addr);
1070        if (err) {
1071                netdev_err(netdev, "dev_mc_del err %d\n", err);
1072                return err;
1073        }
1074
1075        return err;
1076}
1077
1078static int swdev_port_obj_del(struct net_device *netdev,
1079                              const struct switchdev_obj *obj)
1080{
1081        int err;
1082
1083        switch (obj->id) {
1084        case SWITCHDEV_OBJ_ID_PORT_VLAN:
1085                err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1086                break;
1087        case SWITCHDEV_OBJ_ID_PORT_MDB:
1088                err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1089                break;
1090        default:
1091                err = -EOPNOTSUPP;
1092                break;
1093        }
1094        return err;
1095}
1096
1097static int
1098ethsw_switchdev_port_attr_set_event(struct net_device *netdev,
1099                struct switchdev_notifier_port_attr_info *port_attr_info)
1100{
1101        int err;
1102
1103        err = swdev_port_attr_set(netdev, port_attr_info->attr,
1104                                  port_attr_info->trans);
1105
1106        port_attr_info->handled = true;
1107        return notifier_from_errno(err);
1108}
1109
1110/* For the moment, only flood setting needs to be updated */
1111static int port_bridge_join(struct net_device *netdev,
1112                            struct net_device *upper_dev)
1113{
1114        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1115        struct ethsw_core *ethsw = port_priv->ethsw_data;
1116        int i, err;
1117
1118        for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1119                if (ethsw->ports[i]->bridge_dev &&
1120                    (ethsw->ports[i]->bridge_dev != upper_dev)) {
1121                        netdev_err(netdev,
1122                                   "Only one bridge supported per DPSW object!\n");
1123                        return -EINVAL;
1124                }
1125
1126        /* Enable flooding */
1127        err = ethsw_port_set_flood(port_priv, 1);
1128        if (!err)
1129                port_priv->bridge_dev = upper_dev;
1130
1131        return err;
1132}
1133
1134static int port_bridge_leave(struct net_device *netdev)
1135{
1136        struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1137        int err;
1138
1139        /* Disable flooding */
1140        err = ethsw_port_set_flood(port_priv, 0);
1141        if (!err)
1142                port_priv->bridge_dev = NULL;
1143
1144        return err;
1145}
1146
1147static bool ethsw_port_dev_check(const struct net_device *netdev)
1148{
1149        return netdev->netdev_ops == &ethsw_port_ops;
1150}
1151
1152static int port_netdevice_event(struct notifier_block *unused,
1153                                unsigned long event, void *ptr)
1154{
1155        struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1156        struct netdev_notifier_changeupper_info *info = ptr;
1157        struct net_device *upper_dev;
1158        int err = 0;
1159
1160        if (!ethsw_port_dev_check(netdev))
1161                return NOTIFY_DONE;
1162
1163        /* Handle just upper dev link/unlink for the moment */
1164        if (event == NETDEV_CHANGEUPPER) {
1165                upper_dev = info->upper_dev;
1166                if (netif_is_bridge_master(upper_dev)) {
1167                        if (info->linking)
1168                                err = port_bridge_join(netdev, upper_dev);
1169                        else
1170                                err = port_bridge_leave(netdev);
1171                }
1172        }
1173
1174        return notifier_from_errno(err);
1175}
1176
1177static struct notifier_block port_nb __read_mostly = {
1178        .notifier_call = port_netdevice_event,
1179};
1180
1181struct ethsw_switchdev_event_work {
1182        struct work_struct work;
1183        struct switchdev_notifier_fdb_info fdb_info;
1184        struct net_device *dev;
1185        unsigned long event;
1186};
1187
1188static void ethsw_switchdev_event_work(struct work_struct *work)
1189{
1190        struct ethsw_switchdev_event_work *switchdev_work =
1191                container_of(work, struct ethsw_switchdev_event_work, work);
1192        struct net_device *dev = switchdev_work->dev;
1193        struct switchdev_notifier_fdb_info *fdb_info;
1194        int err;
1195
1196        rtnl_lock();
1197        fdb_info = &switchdev_work->fdb_info;
1198
1199        switch (switchdev_work->event) {
1200        case SWITCHDEV_FDB_ADD_TO_DEVICE:
1201                if (!fdb_info->added_by_user)
1202                        break;
1203                if (is_unicast_ether_addr(fdb_info->addr))
1204                        err = ethsw_port_fdb_add_uc(netdev_priv(dev),
1205                                                    fdb_info->addr);
1206                else
1207                        err = ethsw_port_fdb_add_mc(netdev_priv(dev),
1208                                                    fdb_info->addr);
1209                if (err)
1210                        break;
1211                fdb_info->offloaded = true;
1212                call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1213                                         &fdb_info->info, NULL);
1214                break;
1215        case SWITCHDEV_FDB_DEL_TO_DEVICE:
1216                if (!fdb_info->added_by_user)
1217                        break;
1218                if (is_unicast_ether_addr(fdb_info->addr))
1219                        ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1220                else
1221                        ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1222                break;
1223        }
1224
1225        rtnl_unlock();
1226        kfree(switchdev_work->fdb_info.addr);
1227        kfree(switchdev_work);
1228        dev_put(dev);
1229}
1230
1231/* Called under rcu_read_lock() */
1232static int port_switchdev_event(struct notifier_block *unused,
1233                                unsigned long event, void *ptr)
1234{
1235        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1236        struct ethsw_switchdev_event_work *switchdev_work;
1237        struct switchdev_notifier_fdb_info *fdb_info = ptr;
1238
1239        if (!ethsw_port_dev_check(dev))
1240                return NOTIFY_DONE;
1241
1242        if (event == SWITCHDEV_PORT_ATTR_SET)
1243                return ethsw_switchdev_port_attr_set_event(dev, ptr);
1244
1245        switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1246        if (!switchdev_work)
1247                return NOTIFY_BAD;
1248
1249        INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1250        switchdev_work->dev = dev;
1251        switchdev_work->event = event;
1252
1253        switch (event) {
1254        case SWITCHDEV_FDB_ADD_TO_DEVICE:
1255        case SWITCHDEV_FDB_DEL_TO_DEVICE:
1256                memcpy(&switchdev_work->fdb_info, ptr,
1257                       sizeof(switchdev_work->fdb_info));
1258                switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1259                if (!switchdev_work->fdb_info.addr)
1260                        goto err_addr_alloc;
1261
1262                ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1263                                fdb_info->addr);
1264
1265                /* Take a reference on the device to avoid being freed. */
1266                dev_hold(dev);
1267                break;
1268        default:
1269                kfree(switchdev_work);
1270                return NOTIFY_DONE;
1271        }
1272
1273        queue_work(ethsw_owq, &switchdev_work->work);
1274
1275        return NOTIFY_DONE;
1276
1277err_addr_alloc:
1278        kfree(switchdev_work);
1279        return NOTIFY_BAD;
1280}
1281
1282static int
1283ethsw_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
1284                        struct switchdev_notifier_port_obj_info *port_obj_info)
1285{
1286        int err = -EOPNOTSUPP;
1287
1288        switch (event) {
1289        case SWITCHDEV_PORT_OBJ_ADD:
1290                err = swdev_port_obj_add(netdev, port_obj_info->obj,
1291                                         port_obj_info->trans);
1292                break;
1293        case SWITCHDEV_PORT_OBJ_DEL:
1294                err = swdev_port_obj_del(netdev, port_obj_info->obj);
1295                break;
1296        }
1297
1298        port_obj_info->handled = true;
1299        return notifier_from_errno(err);
1300}
1301
1302static int port_switchdev_blocking_event(struct notifier_block *unused,
1303                                         unsigned long event, void *ptr)
1304{
1305        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1306
1307        if (!ethsw_port_dev_check(dev))
1308                return NOTIFY_DONE;
1309
1310        switch (event) {
1311        case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1312        case SWITCHDEV_PORT_OBJ_DEL:
1313                return ethsw_switchdev_port_obj_event(event, dev, ptr);
1314        case SWITCHDEV_PORT_ATTR_SET:
1315                return ethsw_switchdev_port_attr_set_event(dev, ptr);
1316        }
1317
1318        return NOTIFY_DONE;
1319}
1320
1321static struct notifier_block port_switchdev_nb = {
1322        .notifier_call = port_switchdev_event,
1323};
1324
1325static struct notifier_block port_switchdev_blocking_nb = {
1326        .notifier_call = port_switchdev_blocking_event,
1327};
1328
1329static int ethsw_register_notifier(struct device *dev)
1330{
1331        int err;
1332
1333        err = register_netdevice_notifier(&port_nb);
1334        if (err) {
1335                dev_err(dev, "Failed to register netdev notifier\n");
1336                return err;
1337        }
1338
1339        err = register_switchdev_notifier(&port_switchdev_nb);
1340        if (err) {
1341                dev_err(dev, "Failed to register switchdev notifier\n");
1342                goto err_switchdev_nb;
1343        }
1344
1345        err = register_switchdev_blocking_notifier(&port_switchdev_blocking_nb);
1346        if (err) {
1347                dev_err(dev, "Failed to register switchdev blocking notifier\n");
1348                goto err_switchdev_blocking_nb;
1349        }
1350
1351        return 0;
1352
1353err_switchdev_blocking_nb:
1354        unregister_switchdev_notifier(&port_switchdev_nb);
1355err_switchdev_nb:
1356        unregister_netdevice_notifier(&port_nb);
1357        return err;
1358}
1359
1360static int ethsw_init(struct fsl_mc_device *sw_dev)
1361{
1362        struct device *dev = &sw_dev->dev;
1363        struct ethsw_core *ethsw = dev_get_drvdata(dev);
1364        u16 version_major, version_minor, i;
1365        struct dpsw_stp_cfg stp_cfg;
1366        int err;
1367
1368        ethsw->dev_id = sw_dev->obj_desc.id;
1369
1370        err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, &ethsw->dpsw_handle);
1371        if (err) {
1372                dev_err(dev, "dpsw_open err %d\n", err);
1373                return err;
1374        }
1375
1376        err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1377                                  &ethsw->sw_attr);
1378        if (err) {
1379                dev_err(dev, "dpsw_get_attributes err %d\n", err);
1380                goto err_close;
1381        }
1382
1383        err = dpsw_get_api_version(ethsw->mc_io, 0,
1384                                   &version_major,
1385                                   &version_minor);
1386        if (err) {
1387                dev_err(dev, "dpsw_get_api_version err %d\n", err);
1388                goto err_close;
1389        }
1390
1391        /* Minimum supported DPSW version check */
1392        if (version_major < DPSW_MIN_VER_MAJOR ||
1393            (version_major == DPSW_MIN_VER_MAJOR &&
1394             version_minor < DPSW_MIN_VER_MINOR)) {
1395                dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1396                        version_major,
1397                        version_minor,
1398                        DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1399                err = -ENOTSUPP;
1400                goto err_close;
1401        }
1402
1403        err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1404        if (err) {
1405                dev_err(dev, "dpsw_reset err %d\n", err);
1406                goto err_close;
1407        }
1408
1409        err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1410                                         DPSW_FDB_LEARNING_MODE_HW);
1411        if (err) {
1412                dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1413                goto err_close;
1414        }
1415
1416        stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1417        stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1418
1419        for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1420                err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1421                                      &stp_cfg);
1422                if (err) {
1423                        dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1424                                err, i);
1425                        goto err_close;
1426                }
1427
1428                err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1429                                            ethsw->dpsw_handle, i, 1);
1430                if (err) {
1431                        dev_err(dev,
1432                                "dpsw_if_set_broadcast err %d for port %d\n",
1433                                err, i);
1434                        goto err_close;
1435                }
1436        }
1437
1438        ethsw_owq = alloc_ordered_workqueue("%s_ordered", WQ_MEM_RECLAIM,
1439                                            "ethsw");
1440        if (!ethsw_owq) {
1441                err = -ENOMEM;
1442                goto err_close;
1443        }
1444
1445        err = ethsw_register_notifier(dev);
1446        if (err)
1447                goto err_destroy_ordered_workqueue;
1448
1449        return 0;
1450
1451err_destroy_ordered_workqueue:
1452        destroy_workqueue(ethsw_owq);
1453
1454err_close:
1455        dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1456        return err;
1457}
1458
1459static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1460{
1461        struct net_device *netdev = port_priv->netdev;
1462        struct ethsw_core *ethsw = port_priv->ethsw_data;
1463        struct dpsw_vlan_if_cfg vcfg;
1464        int err;
1465
1466        /* Switch starts with all ports configured to VLAN 1. Need to
1467         * remove this setting to allow configuration at bridge join
1468         */
1469        vcfg.num_ifs = 1;
1470        vcfg.if_id[0] = port_priv->idx;
1471
1472        err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1473                                           DEFAULT_VLAN_ID, &vcfg);
1474        if (err) {
1475                netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1476                           err);
1477                return err;
1478        }
1479
1480        err = ethsw_port_set_pvid(port_priv, 0);
1481        if (err)
1482                return err;
1483
1484        err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1485                                  DEFAULT_VLAN_ID, &vcfg);
1486        if (err)
1487                netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1488
1489        return err;
1490}
1491
1492static void ethsw_unregister_notifier(struct device *dev)
1493{
1494        struct notifier_block *nb;
1495        int err;
1496
1497        nb = &port_switchdev_blocking_nb;
1498        err = unregister_switchdev_blocking_notifier(nb);
1499        if (err)
1500                dev_err(dev,
1501                        "Failed to unregister switchdev blocking notifier (%d)\n", err);
1502
1503        err = unregister_switchdev_notifier(&port_switchdev_nb);
1504        if (err)
1505                dev_err(dev,
1506                        "Failed to unregister switchdev notifier (%d)\n", err);
1507
1508        err = unregister_netdevice_notifier(&port_nb);
1509        if (err)
1510                dev_err(dev,
1511                        "Failed to unregister netdev notifier (%d)\n", err);
1512}
1513
1514static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1515{
1516        struct device *dev = &sw_dev->dev;
1517        struct ethsw_core *ethsw = dev_get_drvdata(dev);
1518        int err;
1519
1520        ethsw_unregister_notifier(dev);
1521
1522        err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1523        if (err)
1524                dev_warn(dev, "dpsw_close err %d\n", err);
1525}
1526
1527static int ethsw_remove(struct fsl_mc_device *sw_dev)
1528{
1529        struct ethsw_port_priv *port_priv;
1530        struct ethsw_core *ethsw;
1531        struct device *dev;
1532        int i;
1533
1534        dev = &sw_dev->dev;
1535        ethsw = dev_get_drvdata(dev);
1536
1537        ethsw_teardown_irqs(sw_dev);
1538
1539        destroy_workqueue(ethsw_owq);
1540
1541        dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1542
1543        for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1544                port_priv = ethsw->ports[i];
1545                unregister_netdev(port_priv->netdev);
1546                free_netdev(port_priv->netdev);
1547        }
1548        kfree(ethsw->ports);
1549
1550        ethsw_takedown(sw_dev);
1551        fsl_mc_portal_free(ethsw->mc_io);
1552
1553        kfree(ethsw);
1554
1555        dev_set_drvdata(dev, NULL);
1556
1557        return 0;
1558}
1559
1560static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1561{
1562        struct ethsw_port_priv *port_priv;
1563        struct device *dev = ethsw->dev;
1564        struct net_device *port_netdev;
1565        int err;
1566
1567        port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1568        if (!port_netdev) {
1569                dev_err(dev, "alloc_etherdev error\n");
1570                return -ENOMEM;
1571        }
1572
1573        port_priv = netdev_priv(port_netdev);
1574        port_priv->netdev = port_netdev;
1575        port_priv->ethsw_data = ethsw;
1576
1577        port_priv->idx = port_idx;
1578        port_priv->stp_state = BR_STATE_FORWARDING;
1579
1580        /* Flooding is implicitly enabled */
1581        port_priv->flood = true;
1582
1583        SET_NETDEV_DEV(port_netdev, dev);
1584        port_netdev->netdev_ops = &ethsw_port_ops;
1585        port_netdev->ethtool_ops = &ethsw_port_ethtool_ops;
1586
1587        /* Set MTU limits */
1588        port_netdev->min_mtu = ETH_MIN_MTU;
1589        port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1590
1591        err = ethsw_port_init(port_priv, port_idx);
1592        if (err)
1593                goto err_port_probe;
1594
1595        err = register_netdev(port_netdev);
1596        if (err < 0) {
1597                dev_err(dev, "register_netdev error %d\n", err);
1598                goto err_port_probe;
1599        }
1600
1601        ethsw->ports[port_idx] = port_priv;
1602
1603        return 0;
1604
1605err_port_probe:
1606        free_netdev(port_netdev);
1607
1608        return err;
1609}
1610
1611static int ethsw_probe(struct fsl_mc_device *sw_dev)
1612{
1613        struct device *dev = &sw_dev->dev;
1614        struct ethsw_core *ethsw;
1615        int i, err;
1616
1617        /* Allocate switch core*/
1618        ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1619
1620        if (!ethsw)
1621                return -ENOMEM;
1622
1623        ethsw->dev = dev;
1624        dev_set_drvdata(dev, ethsw);
1625
1626        err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
1627                                     &ethsw->mc_io);
1628        if (err) {
1629                if (err == -ENXIO)
1630                        err = -EPROBE_DEFER;
1631                else
1632                        dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1633                goto err_free_drvdata;
1634        }
1635
1636        err = ethsw_init(sw_dev);
1637        if (err)
1638                goto err_free_cmdport;
1639
1640        /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1641        ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1642
1643        /* Learning is implicitly enabled */
1644        ethsw->learning = true;
1645
1646        ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1647                               GFP_KERNEL);
1648        if (!(ethsw->ports)) {
1649                err = -ENOMEM;
1650                goto err_takedown;
1651        }
1652
1653        for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1654                err = ethsw_probe_port(ethsw, i);
1655                if (err)
1656                        goto err_free_ports;
1657        }
1658
1659        err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1660        if (err) {
1661                dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1662                goto err_free_ports;
1663        }
1664
1665        /* Setup IRQs */
1666        err = ethsw_setup_irqs(sw_dev);
1667        if (err)
1668                goto err_stop;
1669
1670        dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1671        return 0;
1672
1673err_stop:
1674        dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1675
1676err_free_ports:
1677        /* Cleanup registered ports only */
1678        for (i--; i >= 0; i--) {
1679                unregister_netdev(ethsw->ports[i]->netdev);
1680                free_netdev(ethsw->ports[i]->netdev);
1681        }
1682        kfree(ethsw->ports);
1683
1684err_takedown:
1685        ethsw_takedown(sw_dev);
1686
1687err_free_cmdport:
1688        fsl_mc_portal_free(ethsw->mc_io);
1689
1690err_free_drvdata:
1691        kfree(ethsw);
1692        dev_set_drvdata(dev, NULL);
1693
1694        return err;
1695}
1696
1697static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1698        {
1699                .vendor = FSL_MC_VENDOR_FREESCALE,
1700                .obj_type = "dpsw",
1701        },
1702        { .vendor = 0x0 }
1703};
1704MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1705
1706static struct fsl_mc_driver eth_sw_drv = {
1707        .driver = {
1708                .name = KBUILD_MODNAME,
1709                .owner = THIS_MODULE,
1710        },
1711        .probe = ethsw_probe,
1712        .remove = ethsw_remove,
1713        .match_id_table = ethsw_match_id_table
1714};
1715
1716module_fsl_mc_driver(eth_sw_drv);
1717
1718MODULE_LICENSE("GPL v2");
1719MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");
1720