linux/drivers/net/ethernet/rocker/rocker_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
   4 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
   5 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
   6 */
   7
   8#include <linux/kernel.h>
   9#include <linux/module.h>
  10#include <linux/pci.h>
  11#include <linux/interrupt.h>
  12#include <linux/sched.h>
  13#include <linux/wait.h>
  14#include <linux/spinlock.h>
  15#include <linux/sort.h>
  16#include <linux/random.h>
  17#include <linux/netdevice.h>
  18#include <linux/skbuff.h>
  19#include <linux/socket.h>
  20#include <linux/etherdevice.h>
  21#include <linux/ethtool.h>
  22#include <linux/if_ether.h>
  23#include <linux/if_vlan.h>
  24#include <linux/if_bridge.h>
  25#include <linux/bitops.h>
  26#include <linux/ctype.h>
  27#include <linux/workqueue.h>
  28#include <net/switchdev.h>
  29#include <net/rtnetlink.h>
  30#include <net/netevent.h>
  31#include <net/arp.h>
  32#include <net/fib_rules.h>
  33#include <net/fib_notifier.h>
  34#include <linux/io-64-nonatomic-lo-hi.h>
  35#include <generated/utsrelease.h>
  36
  37#include "rocker_hw.h"
  38#include "rocker.h"
  39#include "rocker_tlv.h"
  40
  41static const char rocker_driver_name[] = "rocker";
  42
  43static const struct pci_device_id rocker_pci_id_table[] = {
  44        {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
  45        {0, }
  46};
  47
  48struct rocker_wait {
  49        wait_queue_head_t wait;
  50        bool done;
  51        bool nowait;
  52};
  53
  54static void rocker_wait_reset(struct rocker_wait *wait)
  55{
  56        wait->done = false;
  57        wait->nowait = false;
  58}
  59
  60static void rocker_wait_init(struct rocker_wait *wait)
  61{
  62        init_waitqueue_head(&wait->wait);
  63        rocker_wait_reset(wait);
  64}
  65
  66static struct rocker_wait *rocker_wait_create(void)
  67{
  68        struct rocker_wait *wait;
  69
  70        wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  71        if (!wait)
  72                return NULL;
  73        return wait;
  74}
  75
  76static void rocker_wait_destroy(struct rocker_wait *wait)
  77{
  78        kfree(wait);
  79}
  80
  81static bool rocker_wait_event_timeout(struct rocker_wait *wait,
  82                                      unsigned long timeout)
  83{
  84        wait_event_timeout(wait->wait, wait->done, HZ / 10);
  85        if (!wait->done)
  86                return false;
  87        return true;
  88}
  89
  90static void rocker_wait_wake_up(struct rocker_wait *wait)
  91{
  92        wait->done = true;
  93        wake_up(&wait->wait);
  94}
  95
  96static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
  97{
  98        return rocker->msix_entries[vector].vector;
  99}
 100
 101static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
 102{
 103        return rocker_msix_vector(rocker_port->rocker,
 104                                  ROCKER_MSIX_VEC_TX(rocker_port->port_number));
 105}
 106
 107static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
 108{
 109        return rocker_msix_vector(rocker_port->rocker,
 110                                  ROCKER_MSIX_VEC_RX(rocker_port->port_number));
 111}
 112
 113#define rocker_write32(rocker, reg, val)        \
 114        writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
 115#define rocker_read32(rocker, reg)      \
 116        readl((rocker)->hw_addr + (ROCKER_ ## reg))
 117#define rocker_write64(rocker, reg, val)        \
 118        writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
 119#define rocker_read64(rocker, reg)      \
 120        readq((rocker)->hw_addr + (ROCKER_ ## reg))
 121
 122/*****************************
 123 * HW basic testing functions
 124 *****************************/
 125
 126static int rocker_reg_test(const struct rocker *rocker)
 127{
 128        const struct pci_dev *pdev = rocker->pdev;
 129        u64 test_reg;
 130        u64 rnd;
 131
 132        rnd = prandom_u32();
 133        rnd >>= 1;
 134        rocker_write32(rocker, TEST_REG, rnd);
 135        test_reg = rocker_read32(rocker, TEST_REG);
 136        if (test_reg != rnd * 2) {
 137                dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
 138                        test_reg, rnd * 2);
 139                return -EIO;
 140        }
 141
 142        rnd = prandom_u32();
 143        rnd <<= 31;
 144        rnd |= prandom_u32();
 145        rocker_write64(rocker, TEST_REG64, rnd);
 146        test_reg = rocker_read64(rocker, TEST_REG64);
 147        if (test_reg != rnd * 2) {
 148                dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
 149                        test_reg, rnd * 2);
 150                return -EIO;
 151        }
 152
 153        return 0;
 154}
 155
 156static int rocker_dma_test_one(const struct rocker *rocker,
 157                               struct rocker_wait *wait, u32 test_type,
 158                               dma_addr_t dma_handle, const unsigned char *buf,
 159                               const unsigned char *expect, size_t size)
 160{
 161        const struct pci_dev *pdev = rocker->pdev;
 162        int i;
 163
 164        rocker_wait_reset(wait);
 165        rocker_write32(rocker, TEST_DMA_CTRL, test_type);
 166
 167        if (!rocker_wait_event_timeout(wait, HZ / 10)) {
 168                dev_err(&pdev->dev, "no interrupt received within a timeout\n");
 169                return -EIO;
 170        }
 171
 172        for (i = 0; i < size; i++) {
 173                if (buf[i] != expect[i]) {
 174                        dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
 175                                buf[i], i, expect[i]);
 176                        return -EIO;
 177                }
 178        }
 179        return 0;
 180}
 181
 182#define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
 183#define ROCKER_TEST_DMA_FILL_PATTERN 0x96
 184
 185static int rocker_dma_test_offset(const struct rocker *rocker,
 186                                  struct rocker_wait *wait, int offset)
 187{
 188        struct pci_dev *pdev = rocker->pdev;
 189        unsigned char *alloc;
 190        unsigned char *buf;
 191        unsigned char *expect;
 192        dma_addr_t dma_handle;
 193        int i;
 194        int err;
 195
 196        alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
 197                        GFP_KERNEL | GFP_DMA);
 198        if (!alloc)
 199                return -ENOMEM;
 200        buf = alloc + offset;
 201        expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
 202
 203        dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
 204                                    PCI_DMA_BIDIRECTIONAL);
 205        if (pci_dma_mapping_error(pdev, dma_handle)) {
 206                err = -EIO;
 207                goto free_alloc;
 208        }
 209
 210        rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
 211        rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
 212
 213        memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
 214        err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
 215                                  dma_handle, buf, expect,
 216                                  ROCKER_TEST_DMA_BUF_SIZE);
 217        if (err)
 218                goto unmap;
 219
 220        memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
 221        err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
 222                                  dma_handle, buf, expect,
 223                                  ROCKER_TEST_DMA_BUF_SIZE);
 224        if (err)
 225                goto unmap;
 226
 227        prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
 228        for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
 229                expect[i] = ~buf[i];
 230        err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
 231                                  dma_handle, buf, expect,
 232                                  ROCKER_TEST_DMA_BUF_SIZE);
 233        if (err)
 234                goto unmap;
 235
 236unmap:
 237        pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
 238                         PCI_DMA_BIDIRECTIONAL);
 239free_alloc:
 240        kfree(alloc);
 241
 242        return err;
 243}
 244
 245static int rocker_dma_test(const struct rocker *rocker,
 246                           struct rocker_wait *wait)
 247{
 248        int i;
 249        int err;
 250
 251        for (i = 0; i < 8; i++) {
 252                err = rocker_dma_test_offset(rocker, wait, i);
 253                if (err)
 254                        return err;
 255        }
 256        return 0;
 257}
 258
 259static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
 260{
 261        struct rocker_wait *wait = dev_id;
 262
 263        rocker_wait_wake_up(wait);
 264
 265        return IRQ_HANDLED;
 266}
 267
 268static int rocker_basic_hw_test(const struct rocker *rocker)
 269{
 270        const struct pci_dev *pdev = rocker->pdev;
 271        struct rocker_wait wait;
 272        int err;
 273
 274        err = rocker_reg_test(rocker);
 275        if (err) {
 276                dev_err(&pdev->dev, "reg test failed\n");
 277                return err;
 278        }
 279
 280        err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
 281                          rocker_test_irq_handler, 0,
 282                          rocker_driver_name, &wait);
 283        if (err) {
 284                dev_err(&pdev->dev, "cannot assign test irq\n");
 285                return err;
 286        }
 287
 288        rocker_wait_init(&wait);
 289        rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
 290
 291        if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
 292                dev_err(&pdev->dev, "no interrupt received within a timeout\n");
 293                err = -EIO;
 294                goto free_irq;
 295        }
 296
 297        err = rocker_dma_test(rocker, &wait);
 298        if (err)
 299                dev_err(&pdev->dev, "dma test failed\n");
 300
 301free_irq:
 302        free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
 303        return err;
 304}
 305
 306/******************************************
 307 * DMA rings and descriptors manipulations
 308 ******************************************/
 309
 310static u32 __pos_inc(u32 pos, size_t limit)
 311{
 312        return ++pos == limit ? 0 : pos;
 313}
 314
 315static int rocker_desc_err(const struct rocker_desc_info *desc_info)
 316{
 317        int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
 318
 319        switch (err) {
 320        case ROCKER_OK:
 321                return 0;
 322        case -ROCKER_ENOENT:
 323                return -ENOENT;
 324        case -ROCKER_ENXIO:
 325                return -ENXIO;
 326        case -ROCKER_ENOMEM:
 327                return -ENOMEM;
 328        case -ROCKER_EEXIST:
 329                return -EEXIST;
 330        case -ROCKER_EINVAL:
 331                return -EINVAL;
 332        case -ROCKER_EMSGSIZE:
 333                return -EMSGSIZE;
 334        case -ROCKER_ENOTSUP:
 335                return -EOPNOTSUPP;
 336        case -ROCKER_ENOBUFS:
 337                return -ENOBUFS;
 338        }
 339
 340        return -EINVAL;
 341}
 342
 343static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
 344{
 345        desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
 346}
 347
 348static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
 349{
 350        u32 comp_err = desc_info->desc->comp_err;
 351
 352        return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
 353}
 354
 355static void *
 356rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
 357{
 358        return (void *)(uintptr_t)desc_info->desc->cookie;
 359}
 360
 361static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
 362                                       void *ptr)
 363{
 364        desc_info->desc->cookie = (uintptr_t) ptr;
 365}
 366
 367static struct rocker_desc_info *
 368rocker_desc_head_get(const struct rocker_dma_ring_info *info)
 369{
 370        struct rocker_desc_info *desc_info;
 371        u32 head = __pos_inc(info->head, info->size);
 372
 373        desc_info = &info->desc_info[info->head];
 374        if (head == info->tail)
 375                return NULL; /* ring full */
 376        desc_info->tlv_size = 0;
 377        return desc_info;
 378}
 379
 380static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
 381{
 382        desc_info->desc->buf_size = desc_info->data_size;
 383        desc_info->desc->tlv_size = desc_info->tlv_size;
 384}
 385
 386static void rocker_desc_head_set(const struct rocker *rocker,
 387                                 struct rocker_dma_ring_info *info,
 388                                 const struct rocker_desc_info *desc_info)
 389{
 390        u32 head = __pos_inc(info->head, info->size);
 391
 392        BUG_ON(head == info->tail);
 393        rocker_desc_commit(desc_info);
 394        info->head = head;
 395        rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
 396}
 397
 398static struct rocker_desc_info *
 399rocker_desc_tail_get(struct rocker_dma_ring_info *info)
 400{
 401        struct rocker_desc_info *desc_info;
 402
 403        if (info->tail == info->head)
 404                return NULL; /* nothing to be done between head and tail */
 405        desc_info = &info->desc_info[info->tail];
 406        if (!rocker_desc_gen(desc_info))
 407                return NULL; /* gen bit not set, desc is not ready yet */
 408        info->tail = __pos_inc(info->tail, info->size);
 409        desc_info->tlv_size = desc_info->desc->tlv_size;
 410        return desc_info;
 411}
 412
 413static void rocker_dma_ring_credits_set(const struct rocker *rocker,
 414                                        const struct rocker_dma_ring_info *info,
 415                                        u32 credits)
 416{
 417        if (credits)
 418                rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
 419}
 420
 421static unsigned long rocker_dma_ring_size_fix(size_t size)
 422{
 423        return max(ROCKER_DMA_SIZE_MIN,
 424                   min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
 425}
 426
 427static int rocker_dma_ring_create(const struct rocker *rocker,
 428                                  unsigned int type,
 429                                  size_t size,
 430                                  struct rocker_dma_ring_info *info)
 431{
 432        int i;
 433
 434        BUG_ON(size != rocker_dma_ring_size_fix(size));
 435        info->size = size;
 436        info->type = type;
 437        info->head = 0;
 438        info->tail = 0;
 439        info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
 440                                  GFP_KERNEL);
 441        if (!info->desc_info)
 442                return -ENOMEM;
 443
 444        info->desc = pci_alloc_consistent(rocker->pdev,
 445                                          info->size * sizeof(*info->desc),
 446                                          &info->mapaddr);
 447        if (!info->desc) {
 448                kfree(info->desc_info);
 449                return -ENOMEM;
 450        }
 451
 452        for (i = 0; i < info->size; i++)
 453                info->desc_info[i].desc = &info->desc[i];
 454
 455        rocker_write32(rocker, DMA_DESC_CTRL(info->type),
 456                       ROCKER_DMA_DESC_CTRL_RESET);
 457        rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
 458        rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
 459
 460        return 0;
 461}
 462
 463static void rocker_dma_ring_destroy(const struct rocker *rocker,
 464                                    const struct rocker_dma_ring_info *info)
 465{
 466        rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
 467
 468        pci_free_consistent(rocker->pdev,
 469                            info->size * sizeof(struct rocker_desc),
 470                            info->desc, info->mapaddr);
 471        kfree(info->desc_info);
 472}
 473
 474static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
 475                                             struct rocker_dma_ring_info *info)
 476{
 477        int i;
 478
 479        BUG_ON(info->head || info->tail);
 480
 481        /* When ring is consumer, we need to advance head for each desc.
 482         * That tells hw that the desc is ready to be used by it.
 483         */
 484        for (i = 0; i < info->size - 1; i++)
 485                rocker_desc_head_set(rocker, info, &info->desc_info[i]);
 486        rocker_desc_commit(&info->desc_info[i]);
 487}
 488
 489static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
 490                                      const struct rocker_dma_ring_info *info,
 491                                      int direction, size_t buf_size)
 492{
 493        struct pci_dev *pdev = rocker->pdev;
 494        int i;
 495        int err;
 496
 497        for (i = 0; i < info->size; i++) {
 498                struct rocker_desc_info *desc_info = &info->desc_info[i];
 499                struct rocker_desc *desc = &info->desc[i];
 500                dma_addr_t dma_handle;
 501                char *buf;
 502
 503                buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
 504                if (!buf) {
 505                        err = -ENOMEM;
 506                        goto rollback;
 507                }
 508
 509                dma_handle = pci_map_single(pdev, buf, buf_size, direction);
 510                if (pci_dma_mapping_error(pdev, dma_handle)) {
 511                        kfree(buf);
 512                        err = -EIO;
 513                        goto rollback;
 514                }
 515
 516                desc_info->data = buf;
 517                desc_info->data_size = buf_size;
 518                dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
 519
 520                desc->buf_addr = dma_handle;
 521                desc->buf_size = buf_size;
 522        }
 523        return 0;
 524
 525rollback:
 526        for (i--; i >= 0; i--) {
 527                const struct rocker_desc_info *desc_info = &info->desc_info[i];
 528
 529                pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
 530                                 desc_info->data_size, direction);
 531                kfree(desc_info->data);
 532        }
 533        return err;
 534}
 535
 536static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
 537                                      const struct rocker_dma_ring_info *info,
 538                                      int direction)
 539{
 540        struct pci_dev *pdev = rocker->pdev;
 541        int i;
 542
 543        for (i = 0; i < info->size; i++) {
 544                const struct rocker_desc_info *desc_info = &info->desc_info[i];
 545                struct rocker_desc *desc = &info->desc[i];
 546
 547                desc->buf_addr = 0;
 548                desc->buf_size = 0;
 549                pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
 550                                 desc_info->data_size, direction);
 551                kfree(desc_info->data);
 552        }
 553}
 554
 555static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
 556{
 557        struct rocker_wait *wait;
 558
 559        wait = rocker_wait_create();
 560        if (!wait)
 561                return -ENOMEM;
 562        rocker_desc_cookie_ptr_set(desc_info, wait);
 563        return 0;
 564}
 565
 566static void
 567rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
 568{
 569        struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);
 570
 571        rocker_wait_destroy(wait);
 572}
 573
 574static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
 575{
 576        const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
 577        int i;
 578        int err;
 579
 580        for (i = 0; i < cmd_ring->size; i++) {
 581                err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
 582                if (err)
 583                        goto rollback;
 584        }
 585        return 0;
 586
 587rollback:
 588        for (i--; i >= 0; i--)
 589                rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
 590        return err;
 591}
 592
 593static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
 594{
 595        const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
 596        int i;
 597
 598        for (i = 0; i < cmd_ring->size; i++)
 599                rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
 600}
 601
 602static int rocker_dma_rings_init(struct rocker *rocker)
 603{
 604        const struct pci_dev *pdev = rocker->pdev;
 605        int err;
 606
 607        err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
 608                                     ROCKER_DMA_CMD_DEFAULT_SIZE,
 609                                     &rocker->cmd_ring);
 610        if (err) {
 611                dev_err(&pdev->dev, "failed to create command dma ring\n");
 612                return err;
 613        }
 614
 615        spin_lock_init(&rocker->cmd_ring_lock);
 616
 617        err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
 618                                         PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
 619        if (err) {
 620                dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
 621                goto err_dma_cmd_ring_bufs_alloc;
 622        }
 623
 624        err = rocker_dma_cmd_ring_waits_alloc(rocker);
 625        if (err) {
 626                dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
 627                goto err_dma_cmd_ring_waits_alloc;
 628        }
 629
 630        err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
 631                                     ROCKER_DMA_EVENT_DEFAULT_SIZE,
 632                                     &rocker->event_ring);
 633        if (err) {
 634                dev_err(&pdev->dev, "failed to create event dma ring\n");
 635                goto err_dma_event_ring_create;
 636        }
 637
 638        err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
 639                                         PCI_DMA_FROMDEVICE, PAGE_SIZE);
 640        if (err) {
 641                dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
 642                goto err_dma_event_ring_bufs_alloc;
 643        }
 644        rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
 645        return 0;
 646
 647err_dma_event_ring_bufs_alloc:
 648        rocker_dma_ring_destroy(rocker, &rocker->event_ring);
 649err_dma_event_ring_create:
 650        rocker_dma_cmd_ring_waits_free(rocker);
 651err_dma_cmd_ring_waits_alloc:
 652        rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
 653                                  PCI_DMA_BIDIRECTIONAL);
 654err_dma_cmd_ring_bufs_alloc:
 655        rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
 656        return err;
 657}
 658
 659static void rocker_dma_rings_fini(struct rocker *rocker)
 660{
 661        rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
 662                                  PCI_DMA_BIDIRECTIONAL);
 663        rocker_dma_ring_destroy(rocker, &rocker->event_ring);
 664        rocker_dma_cmd_ring_waits_free(rocker);
 665        rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
 666                                  PCI_DMA_BIDIRECTIONAL);
 667        rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
 668}
 669
 670static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
 671                                      struct rocker_desc_info *desc_info,
 672                                      struct sk_buff *skb, size_t buf_len)
 673{
 674        const struct rocker *rocker = rocker_port->rocker;
 675        struct pci_dev *pdev = rocker->pdev;
 676        dma_addr_t dma_handle;
 677
 678        dma_handle = pci_map_single(pdev, skb->data, buf_len,
 679                                    PCI_DMA_FROMDEVICE);
 680        if (pci_dma_mapping_error(pdev, dma_handle))
 681                return -EIO;
 682        if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
 683                goto tlv_put_failure;
 684        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
 685                goto tlv_put_failure;
 686        return 0;
 687
 688tlv_put_failure:
 689        pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
 690        desc_info->tlv_size = 0;
 691        return -EMSGSIZE;
 692}
 693
 694static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
 695{
 696        return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
 697}
 698
 699static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
 700                                        struct rocker_desc_info *desc_info)
 701{
 702        struct net_device *dev = rocker_port->dev;
 703        struct sk_buff *skb;
 704        size_t buf_len = rocker_port_rx_buf_len(rocker_port);
 705        int err;
 706
 707        /* Ensure that hw will see tlv_size zero in case of an error.
 708         * That tells hw to use another descriptor.
 709         */
 710        rocker_desc_cookie_ptr_set(desc_info, NULL);
 711        desc_info->tlv_size = 0;
 712
 713        skb = netdev_alloc_skb_ip_align(dev, buf_len);
 714        if (!skb)
 715                return -ENOMEM;
 716        err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
 717        if (err) {
 718                dev_kfree_skb_any(skb);
 719                return err;
 720        }
 721        rocker_desc_cookie_ptr_set(desc_info, skb);
 722        return 0;
 723}
 724
 725static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
 726                                         const struct rocker_tlv **attrs)
 727{
 728        struct pci_dev *pdev = rocker->pdev;
 729        dma_addr_t dma_handle;
 730        size_t len;
 731
 732        if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
 733            !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
 734                return;
 735        dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
 736        len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
 737        pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
 738}
 739
 740static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
 741                                        const struct rocker_desc_info *desc_info)
 742{
 743        const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
 744        struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
 745
 746        if (!skb)
 747                return;
 748        rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
 749        rocker_dma_rx_ring_skb_unmap(rocker, attrs);
 750        dev_kfree_skb_any(skb);
 751}
 752
 753static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
 754{
 755        const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
 756        const struct rocker *rocker = rocker_port->rocker;
 757        int i;
 758        int err;
 759
 760        for (i = 0; i < rx_ring->size; i++) {
 761                err = rocker_dma_rx_ring_skb_alloc(rocker_port,
 762                                                   &rx_ring->desc_info[i]);
 763                if (err)
 764                        goto rollback;
 765        }
 766        return 0;
 767
 768rollback:
 769        for (i--; i >= 0; i--)
 770                rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
 771        return err;
 772}
 773
 774static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
 775{
 776        const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
 777        const struct rocker *rocker = rocker_port->rocker;
 778        int i;
 779
 780        for (i = 0; i < rx_ring->size; i++)
 781                rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
 782}
 783
 784static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
 785{
 786        struct rocker *rocker = rocker_port->rocker;
 787        int err;
 788
 789        err = rocker_dma_ring_create(rocker,
 790                                     ROCKER_DMA_TX(rocker_port->port_number),
 791                                     ROCKER_DMA_TX_DEFAULT_SIZE,
 792                                     &rocker_port->tx_ring);
 793        if (err) {
 794                netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
 795                return err;
 796        }
 797
 798        err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
 799                                         PCI_DMA_TODEVICE,
 800                                         ROCKER_DMA_TX_DESC_SIZE);
 801        if (err) {
 802                netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
 803                goto err_dma_tx_ring_bufs_alloc;
 804        }
 805
 806        err = rocker_dma_ring_create(rocker,
 807                                     ROCKER_DMA_RX(rocker_port->port_number),
 808                                     ROCKER_DMA_RX_DEFAULT_SIZE,
 809                                     &rocker_port->rx_ring);
 810        if (err) {
 811                netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
 812                goto err_dma_rx_ring_create;
 813        }
 814
 815        err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
 816                                         PCI_DMA_BIDIRECTIONAL,
 817                                         ROCKER_DMA_RX_DESC_SIZE);
 818        if (err) {
 819                netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
 820                goto err_dma_rx_ring_bufs_alloc;
 821        }
 822
 823        err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
 824        if (err) {
 825                netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
 826                goto err_dma_rx_ring_skbs_alloc;
 827        }
 828        rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
 829
 830        return 0;
 831
 832err_dma_rx_ring_skbs_alloc:
 833        rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
 834                                  PCI_DMA_BIDIRECTIONAL);
 835err_dma_rx_ring_bufs_alloc:
 836        rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
 837err_dma_rx_ring_create:
 838        rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
 839                                  PCI_DMA_TODEVICE);
 840err_dma_tx_ring_bufs_alloc:
 841        rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
 842        return err;
 843}
 844
 845static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
 846{
 847        struct rocker *rocker = rocker_port->rocker;
 848
 849        rocker_dma_rx_ring_skbs_free(rocker_port);
 850        rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
 851                                  PCI_DMA_BIDIRECTIONAL);
 852        rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
 853        rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
 854                                  PCI_DMA_TODEVICE);
 855        rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
 856}
 857
 858static void rocker_port_set_enable(const struct rocker_port *rocker_port,
 859                                   bool enable)
 860{
 861        u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
 862
 863        if (enable)
 864                val |= 1ULL << rocker_port->pport;
 865        else
 866                val &= ~(1ULL << rocker_port->pport);
 867        rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
 868}
 869
 870/********************************
 871 * Interrupt handler and helpers
 872 ********************************/
 873
 874static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
 875{
 876        struct rocker *rocker = dev_id;
 877        const struct rocker_desc_info *desc_info;
 878        struct rocker_wait *wait;
 879        u32 credits = 0;
 880
 881        spin_lock(&rocker->cmd_ring_lock);
 882        while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
 883                wait = rocker_desc_cookie_ptr_get(desc_info);
 884                if (wait->nowait) {
 885                        rocker_desc_gen_clear(desc_info);
 886                } else {
 887                        rocker_wait_wake_up(wait);
 888                }
 889                credits++;
 890        }
 891        spin_unlock(&rocker->cmd_ring_lock);
 892        rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
 893
 894        return IRQ_HANDLED;
 895}
 896
 897static void rocker_port_link_up(const struct rocker_port *rocker_port)
 898{
 899        netif_carrier_on(rocker_port->dev);
 900        netdev_info(rocker_port->dev, "Link is up\n");
 901}
 902
 903static void rocker_port_link_down(const struct rocker_port *rocker_port)
 904{
 905        netif_carrier_off(rocker_port->dev);
 906        netdev_info(rocker_port->dev, "Link is down\n");
 907}
 908
 909static int rocker_event_link_change(const struct rocker *rocker,
 910                                    const struct rocker_tlv *info)
 911{
 912        const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
 913        unsigned int port_number;
 914        bool link_up;
 915        struct rocker_port *rocker_port;
 916
 917        rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
 918        if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
 919            !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
 920                return -EIO;
 921        port_number =
 922                rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
 923        link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
 924
 925        if (port_number >= rocker->port_count)
 926                return -EINVAL;
 927
 928        rocker_port = rocker->ports[port_number];
 929        if (netif_carrier_ok(rocker_port->dev) != link_up) {
 930                if (link_up)
 931                        rocker_port_link_up(rocker_port);
 932                else
 933                        rocker_port_link_down(rocker_port);
 934        }
 935
 936        return 0;
 937}
 938
 939static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
 940                                              const unsigned char *addr,
 941                                              __be16 vlan_id);
 942
 943static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
 944                                      const struct rocker_tlv *info)
 945{
 946        const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
 947        unsigned int port_number;
 948        struct rocker_port *rocker_port;
 949        const unsigned char *addr;
 950        __be16 vlan_id;
 951
 952        rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
 953        if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
 954            !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
 955            !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
 956                return -EIO;
 957        port_number =
 958                rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
 959        addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
 960        vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
 961
 962        if (port_number >= rocker->port_count)
 963                return -EINVAL;
 964
 965        rocker_port = rocker->ports[port_number];
 966        return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
 967}
 968
 969static int rocker_event_process(const struct rocker *rocker,
 970                                const struct rocker_desc_info *desc_info)
 971{
 972        const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
 973        const struct rocker_tlv *info;
 974        u16 type;
 975
 976        rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
 977        if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
 978            !attrs[ROCKER_TLV_EVENT_INFO])
 979                return -EIO;
 980
 981        type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
 982        info = attrs[ROCKER_TLV_EVENT_INFO];
 983
 984        switch (type) {
 985        case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
 986                return rocker_event_link_change(rocker, info);
 987        case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
 988                return rocker_event_mac_vlan_seen(rocker, info);
 989        }
 990
 991        return -EOPNOTSUPP;
 992}
 993
 994static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
 995{
 996        struct rocker *rocker = dev_id;
 997        const struct pci_dev *pdev = rocker->pdev;
 998        const struct rocker_desc_info *desc_info;
 999        u32 credits = 0;
1000        int err;
1001
1002        while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1003                err = rocker_desc_err(desc_info);
1004                if (err) {
1005                        dev_err(&pdev->dev, "event desc received with err %d\n",
1006                                err);
1007                } else {
1008                        err = rocker_event_process(rocker, desc_info);
1009                        if (err)
1010                                dev_err(&pdev->dev, "event processing failed with err %d\n",
1011                                        err);
1012                }
1013                rocker_desc_gen_clear(desc_info);
1014                rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1015                credits++;
1016        }
1017        rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1018
1019        return IRQ_HANDLED;
1020}
1021
1022static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1023{
1024        struct rocker_port *rocker_port = dev_id;
1025
1026        napi_schedule(&rocker_port->napi_tx);
1027        return IRQ_HANDLED;
1028}
1029
1030static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1031{
1032        struct rocker_port *rocker_port = dev_id;
1033
1034        napi_schedule(&rocker_port->napi_rx);
1035        return IRQ_HANDLED;
1036}
1037
1038/********************
1039 * Command interface
1040 ********************/
1041
1042int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
1043                    rocker_cmd_prep_cb_t prepare, void *prepare_priv,
1044                    rocker_cmd_proc_cb_t process, void *process_priv)
1045{
1046        struct rocker *rocker = rocker_port->rocker;
1047        struct rocker_desc_info *desc_info;
1048        struct rocker_wait *wait;
1049        unsigned long lock_flags;
1050        int err;
1051
1052        spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
1053
1054        desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1055        if (!desc_info) {
1056                spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1057                return -EAGAIN;
1058        }
1059
1060        wait = rocker_desc_cookie_ptr_get(desc_info);
1061        rocker_wait_init(wait);
1062        wait->nowait = nowait;
1063
1064        err = prepare(rocker_port, desc_info, prepare_priv);
1065        if (err) {
1066                spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1067                return err;
1068        }
1069
1070        rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1071
1072        spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1073
1074        if (nowait)
1075                return 0;
1076
1077        if (!rocker_wait_event_timeout(wait, HZ / 10))
1078                return -EIO;
1079
1080        err = rocker_desc_err(desc_info);
1081        if (err)
1082                return err;
1083
1084        if (process)
1085                err = process(rocker_port, desc_info, process_priv);
1086
1087        rocker_desc_gen_clear(desc_info);
1088        return err;
1089}
1090
1091static int
1092rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
1093                                  struct rocker_desc_info *desc_info,
1094                                  void *priv)
1095{
1096        struct rocker_tlv *cmd_info;
1097
1098        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1099                               ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1100                return -EMSGSIZE;
1101        cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1102        if (!cmd_info)
1103                return -EMSGSIZE;
1104        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1105                               rocker_port->pport))
1106                return -EMSGSIZE;
1107        rocker_tlv_nest_end(desc_info, cmd_info);
1108        return 0;
1109}
1110
1111static int
1112rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
1113                                          const struct rocker_desc_info *desc_info,
1114                                          void *priv)
1115{
1116        struct ethtool_link_ksettings *ecmd = priv;
1117        const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1118        const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1119        u32 speed;
1120        u8 duplex;
1121        u8 autoneg;
1122
1123        rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1124        if (!attrs[ROCKER_TLV_CMD_INFO])
1125                return -EIO;
1126
1127        rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1128                                attrs[ROCKER_TLV_CMD_INFO]);
1129        if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1130            !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1131            !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1132                return -EIO;
1133
1134        speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1135        duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1136        autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1137
1138        ethtool_link_ksettings_zero_link_mode(ecmd, supported);
1139        ethtool_link_ksettings_add_link_mode(ecmd, supported, TP);
1140
1141        ecmd->base.phy_address = 0xff;
1142        ecmd->base.port = PORT_TP;
1143        ecmd->base.speed = speed;
1144        ecmd->base.duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1145        ecmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1146
1147        return 0;
1148}
1149
1150static int
1151rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
1152                                          const struct rocker_desc_info *desc_info,
1153                                          void *priv)
1154{
1155        unsigned char *macaddr = priv;
1156        const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1157        const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1158        const struct rocker_tlv *attr;
1159
1160        rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1161        if (!attrs[ROCKER_TLV_CMD_INFO])
1162                return -EIO;
1163
1164        rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1165                                attrs[ROCKER_TLV_CMD_INFO]);
1166        attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1167        if (!attr)
1168                return -EIO;
1169
1170        if (rocker_tlv_len(attr) != ETH_ALEN)
1171                return -EINVAL;
1172
1173        ether_addr_copy(macaddr, rocker_tlv_data(attr));
1174        return 0;
1175}
1176
1177static int
1178rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port,
1179                                       const struct rocker_desc_info *desc_info,
1180                                       void *priv)
1181{
1182        u8 *p_mode = priv;
1183        const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1184        const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1185        const struct rocker_tlv *attr;
1186
1187        rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1188        if (!attrs[ROCKER_TLV_CMD_INFO])
1189                return -EIO;
1190
1191        rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1192                                attrs[ROCKER_TLV_CMD_INFO]);
1193        attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE];
1194        if (!attr)
1195                return -EIO;
1196
1197        *p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
1198        return 0;
1199}
1200
1201struct port_name {
1202        char *buf;
1203        size_t len;
1204};
1205
1206static int
1207rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
1208                                            const struct rocker_desc_info *desc_info,
1209                                            void *priv)
1210{
1211        const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1212        const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1213        struct port_name *name = priv;
1214        const struct rocker_tlv *attr;
1215        size_t i, j, len;
1216        const char *str;
1217
1218        rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1219        if (!attrs[ROCKER_TLV_CMD_INFO])
1220                return -EIO;
1221
1222        rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1223                                attrs[ROCKER_TLV_CMD_INFO]);
1224        attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
1225        if (!attr)
1226                return -EIO;
1227
1228        len = min_t(size_t, rocker_tlv_len(attr), name->len);
1229        str = rocker_tlv_data(attr);
1230
1231        /* make sure name only contains alphanumeric characters */
1232        for (i = j = 0; i < len; ++i) {
1233                if (isalnum(str[i])) {
1234                        name->buf[j] = str[i];
1235                        j++;
1236                }
1237        }
1238
1239        if (j == 0)
1240                return -EIO;
1241
1242        name->buf[j] = '\0';
1243
1244        return 0;
1245}
1246
1247static int
1248rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
1249                                          struct rocker_desc_info *desc_info,
1250                                          void *priv)
1251{
1252        struct ethtool_link_ksettings *ecmd = priv;
1253        struct rocker_tlv *cmd_info;
1254
1255        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1256                               ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1257                return -EMSGSIZE;
1258        cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1259        if (!cmd_info)
1260                return -EMSGSIZE;
1261        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1262                               rocker_port->pport))
1263                return -EMSGSIZE;
1264        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1265                               ecmd->base.speed))
1266                return -EMSGSIZE;
1267        if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1268                              ecmd->base.duplex))
1269                return -EMSGSIZE;
1270        if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1271                              ecmd->base.autoneg))
1272                return -EMSGSIZE;
1273        rocker_tlv_nest_end(desc_info, cmd_info);
1274        return 0;
1275}
1276
1277static int
1278rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
1279                                          struct rocker_desc_info *desc_info,
1280                                          void *priv)
1281{
1282        const unsigned char *macaddr = priv;
1283        struct rocker_tlv *cmd_info;
1284
1285        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1286                               ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1287                return -EMSGSIZE;
1288        cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1289        if (!cmd_info)
1290                return -EMSGSIZE;
1291        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1292                               rocker_port->pport))
1293                return -EMSGSIZE;
1294        if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1295                           ETH_ALEN, macaddr))
1296                return -EMSGSIZE;
1297        rocker_tlv_nest_end(desc_info, cmd_info);
1298        return 0;
1299}
1300
1301static int
1302rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port,
1303                                      struct rocker_desc_info *desc_info,
1304                                      void *priv)
1305{
1306        int mtu = *(int *)priv;
1307        struct rocker_tlv *cmd_info;
1308
1309        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1310                               ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1311                return -EMSGSIZE;
1312        cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1313        if (!cmd_info)
1314                return -EMSGSIZE;
1315        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1316                               rocker_port->pport))
1317                return -EMSGSIZE;
1318        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU,
1319                               mtu))
1320                return -EMSGSIZE;
1321        rocker_tlv_nest_end(desc_info, cmd_info);
1322        return 0;
1323}
1324
1325static int
1326rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
1327                                  struct rocker_desc_info *desc_info,
1328                                  void *priv)
1329{
1330        bool learning = *(bool *)priv;
1331        struct rocker_tlv *cmd_info;
1332
1333        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1334                               ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1335                return -EMSGSIZE;
1336        cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1337        if (!cmd_info)
1338                return -EMSGSIZE;
1339        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1340                               rocker_port->pport))
1341                return -EMSGSIZE;
1342        if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1343                              learning))
1344                return -EMSGSIZE;
1345        rocker_tlv_nest_end(desc_info, cmd_info);
1346        return 0;
1347}
1348
1349static int
1350rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1351                                     struct ethtool_link_ksettings *ecmd)
1352{
1353        return rocker_cmd_exec(rocker_port, false,
1354                               rocker_cmd_get_port_settings_prep, NULL,
1355                               rocker_cmd_get_port_settings_ethtool_proc,
1356                               ecmd);
1357}
1358
1359static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1360                                                unsigned char *macaddr)
1361{
1362        return rocker_cmd_exec(rocker_port, false,
1363                               rocker_cmd_get_port_settings_prep, NULL,
1364                               rocker_cmd_get_port_settings_macaddr_proc,
1365                               macaddr);
1366}
1367
1368static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
1369                                             u8 *p_mode)
1370{
1371        return rocker_cmd_exec(rocker_port, false,
1372                               rocker_cmd_get_port_settings_prep, NULL,
1373                               rocker_cmd_get_port_settings_mode_proc, p_mode);
1374}
1375
1376static int
1377rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1378                                     const struct ethtool_link_ksettings *ecmd)
1379{
1380        struct ethtool_link_ksettings copy_ecmd;
1381
1382        memcpy(&copy_ecmd, ecmd, sizeof(copy_ecmd));
1383
1384        return rocker_cmd_exec(rocker_port, false,
1385                               rocker_cmd_set_port_settings_ethtool_prep,
1386                               &copy_ecmd, NULL, NULL);
1387}
1388
1389static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1390                                                unsigned char *macaddr)
1391{
1392        return rocker_cmd_exec(rocker_port, false,
1393                               rocker_cmd_set_port_settings_macaddr_prep,
1394                               macaddr, NULL, NULL);
1395}
1396
1397static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
1398                                            int mtu)
1399{
1400        return rocker_cmd_exec(rocker_port, false,
1401                               rocker_cmd_set_port_settings_mtu_prep,
1402                               &mtu, NULL, NULL);
1403}
1404
1405int rocker_port_set_learning(struct rocker_port *rocker_port,
1406                             bool learning)
1407{
1408        return rocker_cmd_exec(rocker_port, false,
1409                               rocker_cmd_set_port_learning_prep,
1410                               &learning, NULL, NULL);
1411}
1412
1413/**********************
1414 * Worlds manipulation
1415 **********************/
1416
1417static struct rocker_world_ops *rocker_world_ops[] = {
1418        &rocker_ofdpa_ops,
1419};
1420
1421#define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1422
1423static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
1424{
1425        int i;
1426
1427        for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
1428                if (rocker_world_ops[i]->mode == mode)
1429                        return rocker_world_ops[i];
1430        return NULL;
1431}
1432
1433static int rocker_world_init(struct rocker *rocker, u8 mode)
1434{
1435        struct rocker_world_ops *wops;
1436        int err;
1437
1438        wops = rocker_world_ops_find(mode);
1439        if (!wops) {
1440                dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
1441                        mode);
1442                return -EINVAL;
1443        }
1444        rocker->wops = wops;
1445        rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
1446        if (!rocker->wpriv)
1447                return -ENOMEM;
1448        if (!wops->init)
1449                return 0;
1450        err = wops->init(rocker);
1451        if (err)
1452                kfree(rocker->wpriv);
1453        return err;
1454}
1455
1456static void rocker_world_fini(struct rocker *rocker)
1457{
1458        struct rocker_world_ops *wops = rocker->wops;
1459
1460        if (!wops || !wops->fini)
1461                return;
1462        wops->fini(rocker);
1463        kfree(rocker->wpriv);
1464}
1465
1466static int rocker_world_check_init(struct rocker_port *rocker_port)
1467{
1468        struct rocker *rocker = rocker_port->rocker;
1469        u8 mode;
1470        int err;
1471
1472        err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
1473        if (err) {
1474                dev_err(&rocker->pdev->dev, "failed to get port mode\n");
1475                return err;
1476        }
1477        if (rocker->wops) {
1478                if (rocker->wops->mode != mode) {
1479                        dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
1480                        return -EINVAL;
1481                }
1482                return 0;
1483        }
1484        return rocker_world_init(rocker, mode);
1485}
1486
1487static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
1488{
1489        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1490        int err;
1491
1492        rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
1493        if (!rocker_port->wpriv)
1494                return -ENOMEM;
1495        if (!wops->port_pre_init)
1496                return 0;
1497        err = wops->port_pre_init(rocker_port);
1498        if (err)
1499                kfree(rocker_port->wpriv);
1500        return 0;
1501}
1502
1503static int rocker_world_port_init(struct rocker_port *rocker_port)
1504{
1505        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1506
1507        if (!wops->port_init)
1508                return 0;
1509        return wops->port_init(rocker_port);
1510}
1511
1512static void rocker_world_port_fini(struct rocker_port *rocker_port)
1513{
1514        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1515
1516        if (!wops->port_fini)
1517                return;
1518        wops->port_fini(rocker_port);
1519}
1520
1521static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
1522{
1523        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1524
1525        if (!wops->port_post_fini)
1526                return;
1527        wops->port_post_fini(rocker_port);
1528        kfree(rocker_port->wpriv);
1529}
1530
1531static int rocker_world_port_open(struct rocker_port *rocker_port)
1532{
1533        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1534
1535        if (!wops->port_open)
1536                return 0;
1537        return wops->port_open(rocker_port);
1538}
1539
1540static void rocker_world_port_stop(struct rocker_port *rocker_port)
1541{
1542        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1543
1544        if (!wops->port_stop)
1545                return;
1546        wops->port_stop(rocker_port);
1547}
1548
1549static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
1550                                                u8 state,
1551                                                struct switchdev_trans *trans)
1552{
1553        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1554
1555        if (!wops->port_attr_stp_state_set)
1556                return -EOPNOTSUPP;
1557
1558        if (switchdev_trans_ph_prepare(trans))
1559                return 0;
1560
1561        return wops->port_attr_stp_state_set(rocker_port, state);
1562}
1563
1564static int
1565rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port *
1566                                                rocker_port,
1567                                                unsigned long *
1568                                                p_brport_flags_support)
1569{
1570        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1571
1572        if (!wops->port_attr_bridge_flags_support_get)
1573                return -EOPNOTSUPP;
1574        return wops->port_attr_bridge_flags_support_get(rocker_port,
1575                                                        p_brport_flags_support);
1576}
1577
1578static int
1579rocker_world_port_attr_pre_bridge_flags_set(struct rocker_port *rocker_port,
1580                                            unsigned long brport_flags,
1581                                            struct switchdev_trans *trans)
1582{
1583        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1584        unsigned long brport_flags_s;
1585        int err;
1586
1587        if (!wops->port_attr_bridge_flags_set)
1588                return -EOPNOTSUPP;
1589
1590        err = rocker_world_port_attr_bridge_flags_support_get(rocker_port,
1591                                                              &brport_flags_s);
1592        if (err)
1593                return err;
1594
1595        if (brport_flags & ~brport_flags_s)
1596                return -EINVAL;
1597
1598        return 0;
1599}
1600
1601static int
1602rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
1603                                        unsigned long brport_flags,
1604                                        struct switchdev_trans *trans)
1605{
1606        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1607
1608        if (!wops->port_attr_bridge_flags_set)
1609                return -EOPNOTSUPP;
1610
1611        if (switchdev_trans_ph_prepare(trans))
1612                return 0;
1613
1614        return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
1615                                                trans);
1616}
1617
1618static int
1619rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
1620                                              u32 ageing_time,
1621                                              struct switchdev_trans *trans)
1622
1623{
1624        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1625
1626        if (!wops->port_attr_bridge_ageing_time_set)
1627                return -EOPNOTSUPP;
1628
1629        if (switchdev_trans_ph_prepare(trans))
1630                return 0;
1631
1632        return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
1633                                                      trans);
1634}
1635
1636static int
1637rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
1638                               const struct switchdev_obj_port_vlan *vlan,
1639                               struct switchdev_trans *trans)
1640{
1641        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1642
1643        if (!wops->port_obj_vlan_add)
1644                return -EOPNOTSUPP;
1645
1646        if (switchdev_trans_ph_prepare(trans))
1647                return 0;
1648
1649        return wops->port_obj_vlan_add(rocker_port, vlan);
1650}
1651
1652static int
1653rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
1654                               const struct switchdev_obj_port_vlan *vlan)
1655{
1656        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1657
1658        if (netif_is_bridge_master(vlan->obj.orig_dev))
1659                return -EOPNOTSUPP;
1660
1661        if (!wops->port_obj_vlan_del)
1662                return -EOPNOTSUPP;
1663        return wops->port_obj_vlan_del(rocker_port, vlan);
1664}
1665
1666static int
1667rocker_world_port_fdb_add(struct rocker_port *rocker_port,
1668                          struct switchdev_notifier_fdb_info *info)
1669{
1670        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1671
1672        if (!wops->port_obj_fdb_add)
1673                return -EOPNOTSUPP;
1674
1675        return wops->port_obj_fdb_add(rocker_port, info->vid, info->addr);
1676}
1677
1678static int
1679rocker_world_port_fdb_del(struct rocker_port *rocker_port,
1680                          struct switchdev_notifier_fdb_info *info)
1681{
1682        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1683
1684        if (!wops->port_obj_fdb_del)
1685                return -EOPNOTSUPP;
1686        return wops->port_obj_fdb_del(rocker_port, info->vid, info->addr);
1687}
1688
1689static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
1690                                           struct net_device *master)
1691{
1692        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1693
1694        if (!wops->port_master_linked)
1695                return -EOPNOTSUPP;
1696        return wops->port_master_linked(rocker_port, master);
1697}
1698
1699static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port,
1700                                             struct net_device *master)
1701{
1702        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1703
1704        if (!wops->port_master_unlinked)
1705                return -EOPNOTSUPP;
1706        return wops->port_master_unlinked(rocker_port, master);
1707}
1708
1709static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
1710                                          struct neighbour *n)
1711{
1712        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1713
1714        if (!wops->port_neigh_update)
1715                return -EOPNOTSUPP;
1716        return wops->port_neigh_update(rocker_port, n);
1717}
1718
1719static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port,
1720                                           struct neighbour *n)
1721{
1722        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1723
1724        if (!wops->port_neigh_destroy)
1725                return -EOPNOTSUPP;
1726        return wops->port_neigh_destroy(rocker_port, n);
1727}
1728
1729static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
1730                                              const unsigned char *addr,
1731                                              __be16 vlan_id)
1732{
1733        struct rocker_world_ops *wops = rocker_port->rocker->wops;
1734
1735        if (!wops->port_ev_mac_vlan_seen)
1736                return -EOPNOTSUPP;
1737        return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
1738}
1739
1740static int rocker_world_fib4_add(struct rocker *rocker,
1741                                 const struct fib_entry_notifier_info *fen_info)
1742{
1743        struct rocker_world_ops *wops = rocker->wops;
1744
1745        if (!wops->fib4_add)
1746                return 0;
1747        return wops->fib4_add(rocker, fen_info);
1748}
1749
1750static int rocker_world_fib4_del(struct rocker *rocker,
1751                                 const struct fib_entry_notifier_info *fen_info)
1752{
1753        struct rocker_world_ops *wops = rocker->wops;
1754
1755        if (!wops->fib4_del)
1756                return 0;
1757        return wops->fib4_del(rocker, fen_info);
1758}
1759
1760static void rocker_world_fib4_abort(struct rocker *rocker)
1761{
1762        struct rocker_world_ops *wops = rocker->wops;
1763
1764        if (wops->fib4_abort)
1765                wops->fib4_abort(rocker);
1766}
1767
1768/*****************
1769 * Net device ops
1770 *****************/
1771
1772static int rocker_port_open(struct net_device *dev)
1773{
1774        struct rocker_port *rocker_port = netdev_priv(dev);
1775        int err;
1776
1777        err = rocker_port_dma_rings_init(rocker_port);
1778        if (err)
1779                return err;
1780
1781        err = request_irq(rocker_msix_tx_vector(rocker_port),
1782                          rocker_tx_irq_handler, 0,
1783                          rocker_driver_name, rocker_port);
1784        if (err) {
1785                netdev_err(rocker_port->dev, "cannot assign tx irq\n");
1786                goto err_request_tx_irq;
1787        }
1788
1789        err = request_irq(rocker_msix_rx_vector(rocker_port),
1790                          rocker_rx_irq_handler, 0,
1791                          rocker_driver_name, rocker_port);
1792        if (err) {
1793                netdev_err(rocker_port->dev, "cannot assign rx irq\n");
1794                goto err_request_rx_irq;
1795        }
1796
1797        err = rocker_world_port_open(rocker_port);
1798        if (err) {
1799                netdev_err(rocker_port->dev, "cannot open port in world\n");
1800                goto err_world_port_open;
1801        }
1802
1803        napi_enable(&rocker_port->napi_tx);
1804        napi_enable(&rocker_port->napi_rx);
1805        if (!dev->proto_down)
1806                rocker_port_set_enable(rocker_port, true);
1807        netif_start_queue(dev);
1808        return 0;
1809
1810err_world_port_open:
1811        free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1812err_request_rx_irq:
1813        free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1814err_request_tx_irq:
1815        rocker_port_dma_rings_fini(rocker_port);
1816        return err;
1817}
1818
1819static int rocker_port_stop(struct net_device *dev)
1820{
1821        struct rocker_port *rocker_port = netdev_priv(dev);
1822
1823        netif_stop_queue(dev);
1824        rocker_port_set_enable(rocker_port, false);
1825        napi_disable(&rocker_port->napi_rx);
1826        napi_disable(&rocker_port->napi_tx);
1827        rocker_world_port_stop(rocker_port);
1828        free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1829        free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1830        rocker_port_dma_rings_fini(rocker_port);
1831
1832        return 0;
1833}
1834
1835static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
1836                                       const struct rocker_desc_info *desc_info)
1837{
1838        const struct rocker *rocker = rocker_port->rocker;
1839        struct pci_dev *pdev = rocker->pdev;
1840        const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
1841        struct rocker_tlv *attr;
1842        int rem;
1843
1844        rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
1845        if (!attrs[ROCKER_TLV_TX_FRAGS])
1846                return;
1847        rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
1848                const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
1849                dma_addr_t dma_handle;
1850                size_t len;
1851
1852                if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
1853                        continue;
1854                rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
1855                                        attr);
1856                if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
1857                    !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
1858                        continue;
1859                dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
1860                len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
1861                pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
1862        }
1863}
1864
1865static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
1866                                       struct rocker_desc_info *desc_info,
1867                                       char *buf, size_t buf_len)
1868{
1869        const struct rocker *rocker = rocker_port->rocker;
1870        struct pci_dev *pdev = rocker->pdev;
1871        dma_addr_t dma_handle;
1872        struct rocker_tlv *frag;
1873
1874        dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
1875        if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
1876                if (net_ratelimit())
1877                        netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
1878                return -EIO;
1879        }
1880        frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
1881        if (!frag)
1882                goto unmap_frag;
1883        if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
1884                               dma_handle))
1885                goto nest_cancel;
1886        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
1887                               buf_len))
1888                goto nest_cancel;
1889        rocker_tlv_nest_end(desc_info, frag);
1890        return 0;
1891
1892nest_cancel:
1893        rocker_tlv_nest_cancel(desc_info, frag);
1894unmap_frag:
1895        pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
1896        return -EMSGSIZE;
1897}
1898
1899static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
1900{
1901        struct rocker_port *rocker_port = netdev_priv(dev);
1902        struct rocker *rocker = rocker_port->rocker;
1903        struct rocker_desc_info *desc_info;
1904        struct rocker_tlv *frags;
1905        int i;
1906        int err;
1907
1908        desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1909        if (unlikely(!desc_info)) {
1910                if (net_ratelimit())
1911                        netdev_err(dev, "tx ring full when queue awake\n");
1912                return NETDEV_TX_BUSY;
1913        }
1914
1915        rocker_desc_cookie_ptr_set(desc_info, skb);
1916
1917        frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
1918        if (!frags)
1919                goto out;
1920        err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1921                                          skb->data, skb_headlen(skb));
1922        if (err)
1923                goto nest_cancel;
1924        if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
1925                err = skb_linearize(skb);
1926                if (err)
1927                        goto unmap_frags;
1928        }
1929
1930        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1931                const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1932
1933                err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1934                                                  skb_frag_address(frag),
1935                                                  skb_frag_size(frag));
1936                if (err)
1937                        goto unmap_frags;
1938        }
1939        rocker_tlv_nest_end(desc_info, frags);
1940
1941        rocker_desc_gen_clear(desc_info);
1942        rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
1943
1944        desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1945        if (!desc_info)
1946                netif_stop_queue(dev);
1947
1948        return NETDEV_TX_OK;
1949
1950unmap_frags:
1951        rocker_tx_desc_frags_unmap(rocker_port, desc_info);
1952nest_cancel:
1953        rocker_tlv_nest_cancel(desc_info, frags);
1954out:
1955        dev_kfree_skb(skb);
1956        dev->stats.tx_dropped++;
1957
1958        return NETDEV_TX_OK;
1959}
1960
1961static int rocker_port_set_mac_address(struct net_device *dev, void *p)
1962{
1963        struct sockaddr *addr = p;
1964        struct rocker_port *rocker_port = netdev_priv(dev);
1965        int err;
1966
1967        if (!is_valid_ether_addr(addr->sa_data))
1968                return -EADDRNOTAVAIL;
1969
1970        err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
1971        if (err)
1972                return err;
1973        memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1974        return 0;
1975}
1976
1977static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
1978{
1979        struct rocker_port *rocker_port = netdev_priv(dev);
1980        int running = netif_running(dev);
1981        int err;
1982
1983        if (running)
1984                rocker_port_stop(dev);
1985
1986        netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
1987        dev->mtu = new_mtu;
1988
1989        err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
1990        if (err)
1991                return err;
1992
1993        if (running)
1994                err = rocker_port_open(dev);
1995
1996        return err;
1997}
1998
1999static int rocker_port_get_phys_port_name(struct net_device *dev,
2000                                          char *buf, size_t len)
2001{
2002        struct rocker_port *rocker_port = netdev_priv(dev);
2003        struct port_name name = { .buf = buf, .len = len };
2004        int err;
2005
2006        err = rocker_cmd_exec(rocker_port, false,
2007                              rocker_cmd_get_port_settings_prep, NULL,
2008                              rocker_cmd_get_port_settings_phys_name_proc,
2009                              &name);
2010
2011        return err ? -EOPNOTSUPP : 0;
2012}
2013
2014static int rocker_port_change_proto_down(struct net_device *dev,
2015                                         bool proto_down)
2016{
2017        struct rocker_port *rocker_port = netdev_priv(dev);
2018
2019        if (rocker_port->dev->flags & IFF_UP)
2020                rocker_port_set_enable(rocker_port, !proto_down);
2021        rocker_port->dev->proto_down = proto_down;
2022        return 0;
2023}
2024
2025static void rocker_port_neigh_destroy(struct net_device *dev,
2026                                      struct neighbour *n)
2027{
2028        struct rocker_port *rocker_port = netdev_priv(n->dev);
2029        int err;
2030
2031        err = rocker_world_port_neigh_destroy(rocker_port, n);
2032        if (err)
2033                netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
2034                            err);
2035}
2036
2037static int rocker_port_get_port_parent_id(struct net_device *dev,
2038                                          struct netdev_phys_item_id *ppid)
2039{
2040        const struct rocker_port *rocker_port = netdev_priv(dev);
2041        const struct rocker *rocker = rocker_port->rocker;
2042
2043        ppid->id_len = sizeof(rocker->hw.id);
2044        memcpy(&ppid->id, &rocker->hw.id, ppid->id_len);
2045
2046        return 0;
2047}
2048
2049static const struct net_device_ops rocker_port_netdev_ops = {
2050        .ndo_open                       = rocker_port_open,
2051        .ndo_stop                       = rocker_port_stop,
2052        .ndo_start_xmit                 = rocker_port_xmit,
2053        .ndo_set_mac_address            = rocker_port_set_mac_address,
2054        .ndo_change_mtu                 = rocker_port_change_mtu,
2055        .ndo_get_phys_port_name         = rocker_port_get_phys_port_name,
2056        .ndo_change_proto_down          = rocker_port_change_proto_down,
2057        .ndo_neigh_destroy              = rocker_port_neigh_destroy,
2058        .ndo_get_port_parent_id         = rocker_port_get_port_parent_id,
2059};
2060
2061/********************
2062 * swdev interface
2063 ********************/
2064
2065static int rocker_port_attr_set(struct net_device *dev,
2066                                const struct switchdev_attr *attr,
2067                                struct switchdev_trans *trans)
2068{
2069        struct rocker_port *rocker_port = netdev_priv(dev);
2070        int err = 0;
2071
2072        switch (attr->id) {
2073        case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
2074                err = rocker_world_port_attr_stp_state_set(rocker_port,
2075                                                           attr->u.stp_state,
2076                                                           trans);
2077                break;
2078        case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
2079                err = rocker_world_port_attr_pre_bridge_flags_set(rocker_port,
2080                                                              attr->u.brport_flags,
2081                                                              trans);
2082                break;
2083        case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
2084                err = rocker_world_port_attr_bridge_flags_set(rocker_port,
2085                                                              attr->u.brport_flags,
2086                                                              trans);
2087                break;
2088        case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
2089                err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
2090                                                                    attr->u.ageing_time,
2091                                                                    trans);
2092                break;
2093        default:
2094                err = -EOPNOTSUPP;
2095                break;
2096        }
2097
2098        return err;
2099}
2100
2101static int rocker_port_obj_add(struct net_device *dev,
2102                               const struct switchdev_obj *obj,
2103                               struct switchdev_trans *trans)
2104{
2105        struct rocker_port *rocker_port = netdev_priv(dev);
2106        int err = 0;
2107
2108        switch (obj->id) {
2109        case SWITCHDEV_OBJ_ID_PORT_VLAN:
2110                err = rocker_world_port_obj_vlan_add(rocker_port,
2111                                                     SWITCHDEV_OBJ_PORT_VLAN(obj),
2112                                                     trans);
2113                break;
2114        default:
2115                err = -EOPNOTSUPP;
2116                break;
2117        }
2118
2119        return err;
2120}
2121
2122static int rocker_port_obj_del(struct net_device *dev,
2123                               const struct switchdev_obj *obj)
2124{
2125        struct rocker_port *rocker_port = netdev_priv(dev);
2126        int err = 0;
2127
2128        switch (obj->id) {
2129        case SWITCHDEV_OBJ_ID_PORT_VLAN:
2130                err = rocker_world_port_obj_vlan_del(rocker_port,
2131                                                     SWITCHDEV_OBJ_PORT_VLAN(obj));
2132                break;
2133        default:
2134                err = -EOPNOTSUPP;
2135                break;
2136        }
2137
2138        return err;
2139}
2140
2141struct rocker_fib_event_work {
2142        struct work_struct work;
2143        union {
2144                struct fib_entry_notifier_info fen_info;
2145                struct fib_rule_notifier_info fr_info;
2146        };
2147        struct rocker *rocker;
2148        unsigned long event;
2149};
2150
2151static void rocker_router_fib_event_work(struct work_struct *work)
2152{
2153        struct rocker_fib_event_work *fib_work =
2154                container_of(work, struct rocker_fib_event_work, work);
2155        struct rocker *rocker = fib_work->rocker;
2156        struct fib_rule *rule;
2157        int err;
2158
2159        /* Protect internal structures from changes */
2160        rtnl_lock();
2161        switch (fib_work->event) {
2162        case FIB_EVENT_ENTRY_REPLACE:
2163                err = rocker_world_fib4_add(rocker, &fib_work->fen_info);
2164                if (err)
2165                        rocker_world_fib4_abort(rocker);
2166                fib_info_put(fib_work->fen_info.fi);
2167                break;
2168        case FIB_EVENT_ENTRY_DEL:
2169                rocker_world_fib4_del(rocker, &fib_work->fen_info);
2170                fib_info_put(fib_work->fen_info.fi);
2171                break;
2172        case FIB_EVENT_RULE_ADD: /* fall through */
2173        case FIB_EVENT_RULE_DEL:
2174                rule = fib_work->fr_info.rule;
2175                if (!fib4_rule_default(rule))
2176                        rocker_world_fib4_abort(rocker);
2177                fib_rule_put(rule);
2178                break;
2179        }
2180        rtnl_unlock();
2181        kfree(fib_work);
2182}
2183
2184/* Called with rcu_read_lock() */
2185static int rocker_router_fib_event(struct notifier_block *nb,
2186                                   unsigned long event, void *ptr)
2187{
2188        struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
2189        struct rocker_fib_event_work *fib_work;
2190        struct fib_notifier_info *info = ptr;
2191
2192        if (info->family != AF_INET)
2193                return NOTIFY_DONE;
2194
2195        fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2196        if (WARN_ON(!fib_work))
2197                return NOTIFY_BAD;
2198
2199        INIT_WORK(&fib_work->work, rocker_router_fib_event_work);
2200        fib_work->rocker = rocker;
2201        fib_work->event = event;
2202
2203        switch (event) {
2204        case FIB_EVENT_ENTRY_REPLACE: /* fall through */
2205        case FIB_EVENT_ENTRY_DEL:
2206                if (info->family == AF_INET) {
2207                        struct fib_entry_notifier_info *fen_info = ptr;
2208
2209                        if (fen_info->fi->fib_nh_is_v6) {
2210                                NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
2211                                kfree(fib_work);
2212                                return notifier_from_errno(-EINVAL);
2213                        }
2214                        if (fen_info->fi->nh) {
2215                                NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
2216                                kfree(fib_work);
2217                                return notifier_from_errno(-EINVAL);
2218                        }
2219                }
2220
2221                memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2222                /* Take referece on fib_info to prevent it from being
2223                 * freed while work is queued. Release it afterwards.
2224                 */
2225                fib_info_hold(fib_work->fen_info.fi);
2226                break;
2227        case FIB_EVENT_RULE_ADD: /* fall through */
2228        case FIB_EVENT_RULE_DEL:
2229                memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2230                fib_rule_get(fib_work->fr_info.rule);
2231                break;
2232        }
2233
2234        queue_work(rocker->rocker_owq, &fib_work->work);
2235
2236        return NOTIFY_DONE;
2237}
2238
2239/********************
2240 * ethtool interface
2241 ********************/
2242
2243static int
2244rocker_port_get_link_ksettings(struct net_device *dev,
2245                               struct ethtool_link_ksettings *ecmd)
2246{
2247        struct rocker_port *rocker_port = netdev_priv(dev);
2248
2249        return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
2250}
2251
2252static int
2253rocker_port_set_link_ksettings(struct net_device *dev,
2254                               const struct ethtool_link_ksettings *ecmd)
2255{
2256        struct rocker_port *rocker_port = netdev_priv(dev);
2257
2258        return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
2259}
2260
2261static void rocker_port_get_drvinfo(struct net_device *dev,
2262                                    struct ethtool_drvinfo *drvinfo)
2263{
2264        strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
2265        strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2266}
2267
2268static struct rocker_port_stats {
2269        char str[ETH_GSTRING_LEN];
2270        int type;
2271} rocker_port_stats[] = {
2272        { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS,    },
2273        { "rx_bytes",   ROCKER_TLV_CMD_PORT_STATS_RX_BYTES,   },
2274        { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
2275        { "rx_errors",  ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS,  },
2276
2277        { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS,    },
2278        { "tx_bytes",   ROCKER_TLV_CMD_PORT_STATS_TX_BYTES,   },
2279        { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
2280        { "tx_errors",  ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS,  },
2281};
2282
2283#define ROCKER_PORT_STATS_LEN  ARRAY_SIZE(rocker_port_stats)
2284
2285static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
2286                                    u8 *data)
2287{
2288        u8 *p = data;
2289        int i;
2290
2291        switch (stringset) {
2292        case ETH_SS_STATS:
2293                for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2294                        memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
2295                        p += ETH_GSTRING_LEN;
2296                }
2297                break;
2298        }
2299}
2300
2301static int
2302rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
2303                               struct rocker_desc_info *desc_info,
2304                               void *priv)
2305{
2306        struct rocker_tlv *cmd_stats;
2307
2308        if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
2309                               ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
2310                return -EMSGSIZE;
2311
2312        cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2313        if (!cmd_stats)
2314                return -EMSGSIZE;
2315
2316        if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
2317                               rocker_port->pport))
2318                return -EMSGSIZE;
2319
2320        rocker_tlv_nest_end(desc_info, cmd_stats);
2321
2322        return 0;
2323}
2324
2325static int
2326rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
2327                                       const struct rocker_desc_info *desc_info,
2328                                       void *priv)
2329{
2330        const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
2331        const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
2332        const struct rocker_tlv *pattr;
2333        u32 pport;
2334        u64 *data = priv;
2335        int i;
2336
2337        rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
2338
2339        if (!attrs[ROCKER_TLV_CMD_INFO])
2340                return -EIO;
2341
2342        rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
2343                                attrs[ROCKER_TLV_CMD_INFO]);
2344
2345        if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
2346                return -EIO;
2347
2348        pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
2349        if (pport != rocker_port->pport)
2350                return -EIO;
2351
2352        for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2353                pattr = stats_attrs[rocker_port_stats[i].type];
2354                if (!pattr)
2355                        continue;
2356
2357                data[i] = rocker_tlv_get_u64(pattr);
2358        }
2359
2360        return 0;
2361}
2362
2363static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
2364                                             void *priv)
2365{
2366        return rocker_cmd_exec(rocker_port, false,
2367                               rocker_cmd_get_port_stats_prep, NULL,
2368                               rocker_cmd_get_port_stats_ethtool_proc,
2369                               priv);
2370}
2371
2372static void rocker_port_get_stats(struct net_device *dev,
2373                                  struct ethtool_stats *stats, u64 *data)
2374{
2375        struct rocker_port *rocker_port = netdev_priv(dev);
2376
2377        if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
2378                int i;
2379
2380                for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
2381                        data[i] = 0;
2382        }
2383}
2384
2385static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
2386{
2387        switch (sset) {
2388        case ETH_SS_STATS:
2389                return ROCKER_PORT_STATS_LEN;
2390        default:
2391                return -EOPNOTSUPP;
2392        }
2393}
2394
2395static const struct ethtool_ops rocker_port_ethtool_ops = {
2396        .get_drvinfo            = rocker_port_get_drvinfo,
2397        .get_link               = ethtool_op_get_link,
2398        .get_strings            = rocker_port_get_strings,
2399        .get_ethtool_stats      = rocker_port_get_stats,
2400        .get_sset_count         = rocker_port_get_sset_count,
2401        .get_link_ksettings     = rocker_port_get_link_ksettings,
2402        .set_link_ksettings     = rocker_port_set_link_ksettings,
2403};
2404
2405/*****************
2406 * NAPI interface
2407 *****************/
2408
2409static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
2410{
2411        return container_of(napi, struct rocker_port, napi_tx);
2412}
2413
2414static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
2415{
2416        struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
2417        const struct rocker *rocker = rocker_port->rocker;
2418        const struct rocker_desc_info *desc_info;
2419        u32 credits = 0;
2420        int err;
2421
2422        /* Cleanup tx descriptors */
2423        while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
2424                struct sk_buff *skb;
2425
2426                err = rocker_desc_err(desc_info);
2427                if (err && net_ratelimit())
2428                        netdev_err(rocker_port->dev, "tx desc received with err %d\n",
2429                                   err);
2430                rocker_tx_desc_frags_unmap(rocker_port, desc_info);
2431
2432                skb = rocker_desc_cookie_ptr_get(desc_info);
2433                if (err == 0) {
2434                        rocker_port->dev->stats.tx_packets++;
2435                        rocker_port->dev->stats.tx_bytes += skb->len;
2436                } else {
2437                        rocker_port->dev->stats.tx_errors++;
2438                }
2439
2440                dev_kfree_skb_any(skb);
2441                credits++;
2442        }
2443
2444        if (credits && netif_queue_stopped(rocker_port->dev))
2445                netif_wake_queue(rocker_port->dev);
2446
2447        napi_complete(napi);
2448        rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
2449
2450        return 0;
2451}
2452
2453static int rocker_port_rx_proc(const struct rocker *rocker,
2454                               const struct rocker_port *rocker_port,
2455                               struct rocker_desc_info *desc_info)
2456{
2457        const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
2458        struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
2459        size_t rx_len;
2460        u16 rx_flags = 0;
2461
2462        if (!skb)
2463                return -ENOENT;
2464
2465        rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
2466        if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
2467                return -EINVAL;
2468        if (attrs[ROCKER_TLV_RX_FLAGS])
2469                rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
2470
2471        rocker_dma_rx_ring_skb_unmap(rocker, attrs);
2472
2473        rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
2474        skb_put(skb, rx_len);
2475        skb->protocol = eth_type_trans(skb, rocker_port->dev);
2476
2477        if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
2478                skb->offload_fwd_mark = 1;
2479
2480        rocker_port->dev->stats.rx_packets++;
2481        rocker_port->dev->stats.rx_bytes += skb->len;
2482
2483        netif_receive_skb(skb);
2484
2485        return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
2486}
2487
2488static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
2489{
2490        return container_of(napi, struct rocker_port, napi_rx);
2491}
2492
2493static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
2494{
2495        struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
2496        const struct rocker *rocker = rocker_port->rocker;
2497        struct rocker_desc_info *desc_info;
2498        u32 credits = 0;
2499        int err;
2500
2501        /* Process rx descriptors */
2502        while (credits < budget &&
2503               (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
2504                err = rocker_desc_err(desc_info);
2505                if (err) {
2506                        if (net_ratelimit())
2507                                netdev_err(rocker_port->dev, "rx desc received with err %d\n",
2508                                           err);
2509                } else {
2510                        err = rocker_port_rx_proc(rocker, rocker_port,
2511                                                  desc_info);
2512                        if (err && net_ratelimit())
2513                                netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
2514                                           err);
2515                }
2516                if (err)
2517                        rocker_port->dev->stats.rx_errors++;
2518
2519                rocker_desc_gen_clear(desc_info);
2520                rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
2521                credits++;
2522        }
2523
2524        if (credits < budget)
2525                napi_complete_done(napi, credits);
2526
2527        rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
2528
2529        return credits;
2530}
2531
2532/*****************
2533 * PCI driver ops
2534 *****************/
2535
2536static void rocker_carrier_init(const struct rocker_port *rocker_port)
2537{
2538        const struct rocker *rocker = rocker_port->rocker;
2539        u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
2540        bool link_up;
2541
2542        link_up = link_status & (1 << rocker_port->pport);
2543        if (link_up)
2544                netif_carrier_on(rocker_port->dev);
2545        else
2546                netif_carrier_off(rocker_port->dev);
2547}
2548
2549static void rocker_remove_ports(struct rocker *rocker)
2550{
2551        struct rocker_port *rocker_port;
2552        int i;
2553
2554        for (i = 0; i < rocker->port_count; i++) {
2555                rocker_port = rocker->ports[i];
2556                if (!rocker_port)
2557                        continue;
2558                rocker_world_port_fini(rocker_port);
2559                unregister_netdev(rocker_port->dev);
2560                rocker_world_port_post_fini(rocker_port);
2561                free_netdev(rocker_port->dev);
2562        }
2563        rocker_world_fini(rocker);
2564        kfree(rocker->ports);
2565}
2566
2567static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
2568{
2569        const struct rocker *rocker = rocker_port->rocker;
2570        const struct pci_dev *pdev = rocker->pdev;
2571        int err;
2572
2573        err = rocker_cmd_get_port_settings_macaddr(rocker_port,
2574                                                   rocker_port->dev->dev_addr);
2575        if (err) {
2576                dev_warn(&pdev->dev, "failed to get mac address, using random\n");
2577                eth_hw_addr_random(rocker_port->dev);
2578        }
2579}
2580
2581#define ROCKER_PORT_MIN_MTU     ETH_MIN_MTU
2582#define ROCKER_PORT_MAX_MTU     9000
2583static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
2584{
2585        struct pci_dev *pdev = rocker->pdev;
2586        struct rocker_port *rocker_port;
2587        struct net_device *dev;
2588        int err;
2589
2590        dev = alloc_etherdev(sizeof(struct rocker_port));
2591        if (!dev)
2592                return -ENOMEM;
2593        SET_NETDEV_DEV(dev, &pdev->dev);
2594        rocker_port = netdev_priv(dev);
2595        rocker_port->dev = dev;
2596        rocker_port->rocker = rocker;
2597        rocker_port->port_number = port_number;
2598        rocker_port->pport = port_number + 1;
2599
2600        err = rocker_world_check_init(rocker_port);
2601        if (err) {
2602                dev_err(&pdev->dev, "world init failed\n");
2603                goto err_world_check_init;
2604        }
2605
2606        rocker_port_dev_addr_init(rocker_port);
2607        dev->netdev_ops = &rocker_port_netdev_ops;
2608        dev->ethtool_ops = &rocker_port_ethtool_ops;
2609        netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
2610                          NAPI_POLL_WEIGHT);
2611        netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
2612                       NAPI_POLL_WEIGHT);
2613        rocker_carrier_init(rocker_port);
2614
2615        dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
2616
2617        /* MTU range: 68 - 9000 */
2618        dev->min_mtu = ROCKER_PORT_MIN_MTU;
2619        dev->max_mtu = ROCKER_PORT_MAX_MTU;
2620
2621        err = rocker_world_port_pre_init(rocker_port);
2622        if (err) {
2623                dev_err(&pdev->dev, "port world pre-init failed\n");
2624                goto err_world_port_pre_init;
2625        }
2626        err = register_netdev(dev);
2627        if (err) {
2628                dev_err(&pdev->dev, "register_netdev failed\n");
2629                goto err_register_netdev;
2630        }
2631        rocker->ports[port_number] = rocker_port;
2632
2633        err = rocker_world_port_init(rocker_port);
2634        if (err) {
2635                dev_err(&pdev->dev, "port world init failed\n");
2636                goto err_world_port_init;
2637        }
2638
2639        return 0;
2640
2641err_world_port_init:
2642        rocker->ports[port_number] = NULL;
2643        unregister_netdev(dev);
2644err_register_netdev:
2645        rocker_world_port_post_fini(rocker_port);
2646err_world_port_pre_init:
2647err_world_check_init:
2648        free_netdev(dev);
2649        return err;
2650}
2651
2652static int rocker_probe_ports(struct rocker *rocker)
2653{
2654        int i;
2655        size_t alloc_size;
2656        int err;
2657
2658        alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
2659        rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
2660        if (!rocker->ports)
2661                return -ENOMEM;
2662        for (i = 0; i < rocker->port_count; i++) {
2663                err = rocker_probe_port(rocker, i);
2664                if (err)
2665                        goto remove_ports;
2666        }
2667        return 0;
2668
2669remove_ports:
2670        rocker_remove_ports(rocker);
2671        return err;
2672}
2673
2674static int rocker_msix_init(struct rocker *rocker)
2675{
2676        struct pci_dev *pdev = rocker->pdev;
2677        int msix_entries;
2678        int i;
2679        int err;
2680
2681        msix_entries = pci_msix_vec_count(pdev);
2682        if (msix_entries < 0)
2683                return msix_entries;
2684
2685        if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
2686                return -EINVAL;
2687
2688        rocker->msix_entries = kmalloc_array(msix_entries,
2689                                             sizeof(struct msix_entry),
2690                                             GFP_KERNEL);
2691        if (!rocker->msix_entries)
2692                return -ENOMEM;
2693
2694        for (i = 0; i < msix_entries; i++)
2695                rocker->msix_entries[i].entry = i;
2696
2697        err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
2698        if (err < 0)
2699                goto err_enable_msix;
2700
2701        return 0;
2702
2703err_enable_msix:
2704        kfree(rocker->msix_entries);
2705        return err;
2706}
2707
2708static void rocker_msix_fini(const struct rocker *rocker)
2709{
2710        pci_disable_msix(rocker->pdev);
2711        kfree(rocker->msix_entries);
2712}
2713
2714static bool rocker_port_dev_check(const struct net_device *dev)
2715{
2716        return dev->netdev_ops == &rocker_port_netdev_ops;
2717}
2718
2719static int
2720rocker_switchdev_port_attr_set_event(struct net_device *netdev,
2721                struct switchdev_notifier_port_attr_info *port_attr_info)
2722{
2723        int err;
2724
2725        err = rocker_port_attr_set(netdev, port_attr_info->attr,
2726                                   port_attr_info->trans);
2727
2728        port_attr_info->handled = true;
2729        return notifier_from_errno(err);
2730}
2731
2732struct rocker_switchdev_event_work {
2733        struct work_struct work;
2734        struct switchdev_notifier_fdb_info fdb_info;
2735        struct rocker_port *rocker_port;
2736        unsigned long event;
2737};
2738
2739static void
2740rocker_fdb_offload_notify(struct rocker_port *rocker_port,
2741                          struct switchdev_notifier_fdb_info *recv_info)
2742{
2743        struct switchdev_notifier_fdb_info info;
2744
2745        info.addr = recv_info->addr;
2746        info.vid = recv_info->vid;
2747        info.offloaded = true;
2748        call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2749                                 rocker_port->dev, &info.info, NULL);
2750}
2751
2752static void rocker_switchdev_event_work(struct work_struct *work)
2753{
2754        struct rocker_switchdev_event_work *switchdev_work =
2755                container_of(work, struct rocker_switchdev_event_work, work);
2756        struct rocker_port *rocker_port = switchdev_work->rocker_port;
2757        struct switchdev_notifier_fdb_info *fdb_info;
2758        int err;
2759
2760        rtnl_lock();
2761        switch (switchdev_work->event) {
2762        case SWITCHDEV_FDB_ADD_TO_DEVICE:
2763                fdb_info = &switchdev_work->fdb_info;
2764                if (!fdb_info->added_by_user)
2765                        break;
2766                err = rocker_world_port_fdb_add(rocker_port, fdb_info);
2767                if (err) {
2768                        netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2769                        break;
2770                }
2771                rocker_fdb_offload_notify(rocker_port, fdb_info);
2772                break;
2773        case SWITCHDEV_FDB_DEL_TO_DEVICE:
2774                fdb_info = &switchdev_work->fdb_info;
2775                if (!fdb_info->added_by_user)
2776                        break;
2777                err = rocker_world_port_fdb_del(rocker_port, fdb_info);
2778                if (err)
2779                        netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2780                break;
2781        }
2782        rtnl_unlock();
2783
2784        kfree(switchdev_work->fdb_info.addr);
2785        kfree(switchdev_work);
2786        dev_put(rocker_port->dev);
2787}
2788
2789/* called under rcu_read_lock() */
2790static int rocker_switchdev_event(struct notifier_block *unused,
2791                                  unsigned long event, void *ptr)
2792{
2793        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2794        struct rocker_switchdev_event_work *switchdev_work;
2795        struct switchdev_notifier_fdb_info *fdb_info = ptr;
2796        struct rocker_port *rocker_port;
2797
2798        if (!rocker_port_dev_check(dev))
2799                return NOTIFY_DONE;
2800
2801        if (event == SWITCHDEV_PORT_ATTR_SET)
2802                return rocker_switchdev_port_attr_set_event(dev, ptr);
2803
2804        rocker_port = netdev_priv(dev);
2805        switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2806        if (WARN_ON(!switchdev_work))
2807                return NOTIFY_BAD;
2808
2809        INIT_WORK(&switchdev_work->work, rocker_switchdev_event_work);
2810        switchdev_work->rocker_port = rocker_port;
2811        switchdev_work->event = event;
2812
2813        switch (event) {
2814        case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2815        case SWITCHDEV_FDB_DEL_TO_DEVICE:
2816                memcpy(&switchdev_work->fdb_info, ptr,
2817                       sizeof(switchdev_work->fdb_info));
2818                switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2819                if (unlikely(!switchdev_work->fdb_info.addr)) {
2820                        kfree(switchdev_work);
2821                        return NOTIFY_BAD;
2822                }
2823
2824                ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2825                                fdb_info->addr);
2826                /* Take a reference on the rocker device */
2827                dev_hold(dev);
2828                break;
2829        default:
2830                kfree(switchdev_work);
2831                return NOTIFY_DONE;
2832        }
2833
2834        queue_work(rocker_port->rocker->rocker_owq,
2835                   &switchdev_work->work);
2836        return NOTIFY_DONE;
2837}
2838
2839static int
2840rocker_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
2841                        struct switchdev_notifier_port_obj_info *port_obj_info)
2842{
2843        int err = -EOPNOTSUPP;
2844
2845        switch (event) {
2846        case SWITCHDEV_PORT_OBJ_ADD:
2847                err = rocker_port_obj_add(netdev, port_obj_info->obj,
2848                                          port_obj_info->trans);
2849                break;
2850        case SWITCHDEV_PORT_OBJ_DEL:
2851                err = rocker_port_obj_del(netdev, port_obj_info->obj);
2852                break;
2853        }
2854
2855        port_obj_info->handled = true;
2856        return notifier_from_errno(err);
2857}
2858
2859static int rocker_switchdev_blocking_event(struct notifier_block *unused,
2860                                           unsigned long event, void *ptr)
2861{
2862        struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2863
2864        if (!rocker_port_dev_check(dev))
2865                return NOTIFY_DONE;
2866
2867        switch (event) {
2868        case SWITCHDEV_PORT_OBJ_ADD:
2869        case SWITCHDEV_PORT_OBJ_DEL:
2870                return rocker_switchdev_port_obj_event(event, dev, ptr);
2871        case SWITCHDEV_PORT_ATTR_SET:
2872                return rocker_switchdev_port_attr_set_event(dev, ptr);
2873        }
2874
2875        return NOTIFY_DONE;
2876}
2877
2878static struct notifier_block rocker_switchdev_notifier = {
2879        .notifier_call = rocker_switchdev_event,
2880};
2881
2882static struct notifier_block rocker_switchdev_blocking_notifier = {
2883        .notifier_call = rocker_switchdev_blocking_event,
2884};
2885
2886static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2887{
2888        struct notifier_block *nb;
2889        struct rocker *rocker;
2890        int err;
2891
2892        rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
2893        if (!rocker)
2894                return -ENOMEM;
2895
2896        err = pci_enable_device(pdev);
2897        if (err) {
2898                dev_err(&pdev->dev, "pci_enable_device failed\n");
2899                goto err_pci_enable_device;
2900        }
2901
2902        err = pci_request_regions(pdev, rocker_driver_name);
2903        if (err) {
2904                dev_err(&pdev->dev, "pci_request_regions failed\n");
2905                goto err_pci_request_regions;
2906        }
2907
2908        err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2909        if (!err) {
2910                err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2911                if (err) {
2912                        dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
2913                        goto err_pci_set_dma_mask;
2914                }
2915        } else {
2916                err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2917                if (err) {
2918                        dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
2919                        goto err_pci_set_dma_mask;
2920                }
2921        }
2922
2923        if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
2924                dev_err(&pdev->dev, "invalid PCI region size\n");
2925                err = -EINVAL;
2926                goto err_pci_resource_len_check;
2927        }
2928
2929        rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
2930                                  pci_resource_len(pdev, 0));
2931        if (!rocker->hw_addr) {
2932                dev_err(&pdev->dev, "ioremap failed\n");
2933                err = -EIO;
2934                goto err_ioremap;
2935        }
2936        pci_set_master(pdev);
2937
2938        rocker->pdev = pdev;
2939        pci_set_drvdata(pdev, rocker);
2940
2941        rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
2942
2943        err = rocker_msix_init(rocker);
2944        if (err) {
2945                dev_err(&pdev->dev, "MSI-X init failed\n");
2946                goto err_msix_init;
2947        }
2948
2949        err = rocker_basic_hw_test(rocker);
2950        if (err) {
2951                dev_err(&pdev->dev, "basic hw test failed\n");
2952                goto err_basic_hw_test;
2953        }
2954
2955        rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2956
2957        err = rocker_dma_rings_init(rocker);
2958        if (err)
2959                goto err_dma_rings_init;
2960
2961        err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
2962                          rocker_cmd_irq_handler, 0,
2963                          rocker_driver_name, rocker);
2964        if (err) {
2965                dev_err(&pdev->dev, "cannot assign cmd irq\n");
2966                goto err_request_cmd_irq;
2967        }
2968
2969        err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
2970                          rocker_event_irq_handler, 0,
2971                          rocker_driver_name, rocker);
2972        if (err) {
2973                dev_err(&pdev->dev, "cannot assign event irq\n");
2974                goto err_request_event_irq;
2975        }
2976
2977        rocker->rocker_owq = alloc_ordered_workqueue(rocker_driver_name,
2978                                                     WQ_MEM_RECLAIM);
2979        if (!rocker->rocker_owq) {
2980                err = -ENOMEM;
2981                goto err_alloc_ordered_workqueue;
2982        }
2983
2984        err = rocker_probe_ports(rocker);
2985        if (err) {
2986                dev_err(&pdev->dev, "failed to probe ports\n");
2987                goto err_probe_ports;
2988        }
2989
2990        /* Only FIBs pointing to our own netdevs are programmed into
2991         * the device, so no need to pass a callback.
2992         */
2993        rocker->fib_nb.notifier_call = rocker_router_fib_event;
2994        err = register_fib_notifier(&init_net, &rocker->fib_nb, NULL, NULL);
2995        if (err)
2996                goto err_register_fib_notifier;
2997
2998        err = register_switchdev_notifier(&rocker_switchdev_notifier);
2999        if (err) {
3000                dev_err(&pdev->dev, "Failed to register switchdev notifier\n");
3001                goto err_register_switchdev_notifier;
3002        }
3003
3004        nb = &rocker_switchdev_blocking_notifier;
3005        err = register_switchdev_blocking_notifier(nb);
3006        if (err) {
3007                dev_err(&pdev->dev, "Failed to register switchdev blocking notifier\n");
3008                goto err_register_switchdev_blocking_notifier;
3009        }
3010
3011        rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
3012
3013        dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
3014                 (int)sizeof(rocker->hw.id), &rocker->hw.id);
3015
3016        return 0;
3017
3018err_register_switchdev_blocking_notifier:
3019        unregister_switchdev_notifier(&rocker_switchdev_notifier);
3020err_register_switchdev_notifier:
3021        unregister_fib_notifier(&init_net, &rocker->fib_nb);
3022err_register_fib_notifier:
3023        rocker_remove_ports(rocker);
3024err_probe_ports:
3025        destroy_workqueue(rocker->rocker_owq);
3026err_alloc_ordered_workqueue:
3027        free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3028err_request_event_irq:
3029        free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3030err_request_cmd_irq:
3031        rocker_dma_rings_fini(rocker);
3032err_dma_rings_init:
3033err_basic_hw_test:
3034        rocker_msix_fini(rocker);
3035err_msix_init:
3036        iounmap(rocker->hw_addr);
3037err_ioremap:
3038err_pci_resource_len_check:
3039err_pci_set_dma_mask:
3040        pci_release_regions(pdev);
3041err_pci_request_regions:
3042        pci_disable_device(pdev);
3043err_pci_enable_device:
3044        kfree(rocker);
3045        return err;
3046}
3047
3048static void rocker_remove(struct pci_dev *pdev)
3049{
3050        struct rocker *rocker = pci_get_drvdata(pdev);
3051        struct notifier_block *nb;
3052
3053        nb = &rocker_switchdev_blocking_notifier;
3054        unregister_switchdev_blocking_notifier(nb);
3055
3056        unregister_switchdev_notifier(&rocker_switchdev_notifier);
3057        unregister_fib_notifier(&init_net, &rocker->fib_nb);
3058        rocker_remove_ports(rocker);
3059        rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
3060        destroy_workqueue(rocker->rocker_owq);
3061        free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3062        free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3063        rocker_dma_rings_fini(rocker);
3064        rocker_msix_fini(rocker);
3065        iounmap(rocker->hw_addr);
3066        pci_release_regions(rocker->pdev);
3067        pci_disable_device(rocker->pdev);
3068        kfree(rocker);
3069}
3070
3071static struct pci_driver rocker_pci_driver = {
3072        .name           = rocker_driver_name,
3073        .id_table       = rocker_pci_id_table,
3074        .probe          = rocker_probe,
3075        .remove         = rocker_remove,
3076};
3077
3078/************************************
3079 * Net device notifier event handler
3080 ************************************/
3081
3082static bool rocker_port_dev_check_under(const struct net_device *dev,
3083                                        struct rocker *rocker)
3084{
3085        struct rocker_port *rocker_port;
3086
3087        if (!rocker_port_dev_check(dev))
3088                return false;
3089
3090        rocker_port = netdev_priv(dev);
3091        if (rocker_port->rocker != rocker)
3092                return false;
3093
3094        return true;
3095}
3096
3097struct rocker_walk_data {
3098        struct rocker *rocker;
3099        struct rocker_port *port;
3100};
3101
3102static int rocker_lower_dev_walk(struct net_device *lower_dev, void *_data)
3103{
3104        struct rocker_walk_data *data = _data;
3105        int ret = 0;
3106
3107        if (rocker_port_dev_check_under(lower_dev, data->rocker)) {
3108                data->port = netdev_priv(lower_dev);
3109                ret = 1;
3110        }
3111
3112        return ret;
3113}
3114
3115struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
3116                                               struct rocker *rocker)
3117{
3118        struct rocker_walk_data data;
3119
3120        if (rocker_port_dev_check_under(dev, rocker))
3121                return netdev_priv(dev);
3122
3123        data.rocker = rocker;
3124        data.port = NULL;
3125        netdev_walk_all_lower_dev(dev, rocker_lower_dev_walk, &data);
3126
3127        return data.port;
3128}
3129
3130static int rocker_netdevice_event(struct notifier_block *unused,
3131                                  unsigned long event, void *ptr)
3132{
3133        struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3134        struct netdev_notifier_changeupper_info *info;
3135        struct rocker_port *rocker_port;
3136        int err;
3137
3138        if (!rocker_port_dev_check(dev))
3139                return NOTIFY_DONE;
3140
3141        switch (event) {
3142        case NETDEV_CHANGEUPPER:
3143                info = ptr;
3144                if (!info->master)
3145                        goto out;
3146                rocker_port = netdev_priv(dev);
3147                if (info->linking) {
3148                        err = rocker_world_port_master_linked(rocker_port,
3149                                                              info->upper_dev);
3150                        if (err)
3151                                netdev_warn(dev, "failed to reflect master linked (err %d)\n",
3152                                            err);
3153                } else {
3154                        err = rocker_world_port_master_unlinked(rocker_port,
3155                                                                info->upper_dev);
3156                        if (err)
3157                                netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
3158                                            err);
3159                }
3160        }
3161out:
3162        return NOTIFY_DONE;
3163}
3164
3165static struct notifier_block rocker_netdevice_nb __read_mostly = {
3166        .notifier_call = rocker_netdevice_event,
3167};
3168
3169/************************************
3170 * Net event notifier event handler
3171 ************************************/
3172
3173static int rocker_netevent_event(struct notifier_block *unused,
3174                                 unsigned long event, void *ptr)
3175{
3176        struct rocker_port *rocker_port;
3177        struct net_device *dev;
3178        struct neighbour *n = ptr;
3179        int err;
3180
3181        switch (event) {
3182        case NETEVENT_NEIGH_UPDATE:
3183                if (n->tbl != &arp_tbl)
3184                        return NOTIFY_DONE;
3185                dev = n->dev;
3186                if (!rocker_port_dev_check(dev))
3187                        return NOTIFY_DONE;
3188                rocker_port = netdev_priv(dev);
3189                err = rocker_world_port_neigh_update(rocker_port, n);
3190                if (err)
3191                        netdev_warn(dev, "failed to handle neigh update (err %d)\n",
3192                                    err);
3193                break;
3194        }
3195
3196        return NOTIFY_DONE;
3197}
3198
3199static struct notifier_block rocker_netevent_nb __read_mostly = {
3200        .notifier_call = rocker_netevent_event,
3201};
3202
3203/***********************
3204 * Module init and exit
3205 ***********************/
3206
3207static int __init rocker_module_init(void)
3208{
3209        int err;
3210
3211        register_netdevice_notifier(&rocker_netdevice_nb);
3212        register_netevent_notifier(&rocker_netevent_nb);
3213        err = pci_register_driver(&rocker_pci_driver);
3214        if (err)
3215                goto err_pci_register_driver;
3216        return 0;
3217
3218err_pci_register_driver:
3219        unregister_netevent_notifier(&rocker_netevent_nb);
3220        unregister_netdevice_notifier(&rocker_netdevice_nb);
3221        return err;
3222}
3223
3224static void __exit rocker_module_exit(void)
3225{
3226        unregister_netevent_notifier(&rocker_netevent_nb);
3227        unregister_netdevice_notifier(&rocker_netdevice_nb);
3228        pci_unregister_driver(&rocker_pci_driver);
3229}
3230
3231module_init(rocker_module_init);
3232module_exit(rocker_module_exit);
3233
3234MODULE_LICENSE("GPL v2");
3235MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3236MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
3237MODULE_DESCRIPTION("Rocker switch device driver");
3238MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);
3239