linux/drivers/net/ethernet/huawei/hinic/hinic_main.c
<<
>>
Prefs
   1/*
   2 * Huawei HiNIC PCI Express Linux driver
   3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * for more details.
  13 *
  14 */
  15
  16#include <linux/kernel.h>
  17#include <linux/module.h>
  18#include <linux/moduleparam.h>
  19#include <linux/pci.h>
  20#include <linux/device.h>
  21#include <linux/errno.h>
  22#include <linux/types.h>
  23#include <linux/etherdevice.h>
  24#include <linux/netdevice.h>
  25#include <linux/slab.h>
  26#include <linux/if_vlan.h>
  27#include <linux/semaphore.h>
  28#include <linux/workqueue.h>
  29#include <net/ip.h>
  30#include <linux/bitops.h>
  31#include <linux/bitmap.h>
  32#include <linux/delay.h>
  33#include <linux/err.h>
  34
  35#include "hinic_hw_qp.h"
  36#include "hinic_hw_dev.h"
  37#include "hinic_port.h"
  38#include "hinic_tx.h"
  39#include "hinic_rx.h"
  40#include "hinic_dev.h"
  41
  42MODULE_AUTHOR("Huawei Technologies CO., Ltd");
  43MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
  44MODULE_LICENSE("GPL");
  45
  46static unsigned int tx_weight = 64;
  47module_param(tx_weight, uint, 0644);
  48MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
  49
  50static unsigned int rx_weight = 64;
  51module_param(rx_weight, uint, 0644);
  52MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
  53
  54#define HINIC_DEV_ID_QUAD_PORT_25GE         0x1822
  55#define HINIC_DEV_ID_DUAL_PORT_100GE        0x0200
  56#define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ   0x0205
  57#define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ    0x0210
  58
  59#define HINIC_WQ_NAME                   "hinic_dev"
  60
  61#define MSG_ENABLE_DEFAULT              (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  62                                         NETIF_MSG_IFUP |                  \
  63                                         NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
  64
  65#define VLAN_BITMAP_SIZE(nic_dev)       (ALIGN(VLAN_N_VID, 8) / 8)
  66
  67#define work_to_rx_mode_work(work)      \
  68                container_of(work, struct hinic_rx_mode_work, work)
  69
  70#define rx_mode_work_to_nic_dev(rx_mode_work) \
  71                container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
  72
  73static int change_mac_addr(struct net_device *netdev, const u8 *addr);
  74
  75static void set_link_speed(struct ethtool_link_ksettings *link_ksettings,
  76                           enum hinic_speed speed)
  77{
  78        switch (speed) {
  79        case HINIC_SPEED_10MB_LINK:
  80                link_ksettings->base.speed = SPEED_10;
  81                break;
  82
  83        case HINIC_SPEED_100MB_LINK:
  84                link_ksettings->base.speed = SPEED_100;
  85                break;
  86
  87        case HINIC_SPEED_1000MB_LINK:
  88                link_ksettings->base.speed = SPEED_1000;
  89                break;
  90
  91        case HINIC_SPEED_10GB_LINK:
  92                link_ksettings->base.speed = SPEED_10000;
  93                break;
  94
  95        case HINIC_SPEED_25GB_LINK:
  96                link_ksettings->base.speed = SPEED_25000;
  97                break;
  98
  99        case HINIC_SPEED_40GB_LINK:
 100                link_ksettings->base.speed = SPEED_40000;
 101                break;
 102
 103        case HINIC_SPEED_100GB_LINK:
 104                link_ksettings->base.speed = SPEED_100000;
 105                break;
 106
 107        default:
 108                link_ksettings->base.speed = SPEED_UNKNOWN;
 109                break;
 110        }
 111}
 112
 113static int hinic_get_link_ksettings(struct net_device *netdev,
 114                                    struct ethtool_link_ksettings
 115                                    *link_ksettings)
 116{
 117        struct hinic_dev *nic_dev = netdev_priv(netdev);
 118        enum hinic_port_link_state link_state;
 119        struct hinic_port_cap port_cap;
 120        int err;
 121
 122        ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
 123        ethtool_link_ksettings_add_link_mode(link_ksettings, supported,
 124                                             Autoneg);
 125
 126        link_ksettings->base.speed   = SPEED_UNKNOWN;
 127        link_ksettings->base.autoneg = AUTONEG_DISABLE;
 128        link_ksettings->base.duplex  = DUPLEX_UNKNOWN;
 129
 130        err = hinic_port_get_cap(nic_dev, &port_cap);
 131        if (err) {
 132                netif_err(nic_dev, drv, netdev,
 133                          "Failed to get port capabilities\n");
 134                return err;
 135        }
 136
 137        err = hinic_port_link_state(nic_dev, &link_state);
 138        if (err) {
 139                netif_err(nic_dev, drv, netdev,
 140                          "Failed to get port link state\n");
 141                return err;
 142        }
 143
 144        if (link_state != HINIC_LINK_STATE_UP) {
 145                netif_info(nic_dev, drv, netdev, "No link\n");
 146                return err;
 147        }
 148
 149        set_link_speed(link_ksettings, port_cap.speed);
 150
 151        if (!!(port_cap.autoneg_cap & HINIC_AUTONEG_SUPPORTED))
 152                ethtool_link_ksettings_add_link_mode(link_ksettings,
 153                                                     advertising, Autoneg);
 154
 155        if (port_cap.autoneg_state == HINIC_AUTONEG_ACTIVE)
 156                link_ksettings->base.autoneg = AUTONEG_ENABLE;
 157
 158        link_ksettings->base.duplex = (port_cap.duplex == HINIC_DUPLEX_FULL) ?
 159                                       DUPLEX_FULL : DUPLEX_HALF;
 160        return 0;
 161}
 162
 163static void hinic_get_drvinfo(struct net_device *netdev,
 164                              struct ethtool_drvinfo *info)
 165{
 166        struct hinic_dev *nic_dev = netdev_priv(netdev);
 167        struct hinic_hwdev *hwdev = nic_dev->hwdev;
 168        struct hinic_hwif *hwif = hwdev->hwif;
 169
 170        strlcpy(info->driver, HINIC_DRV_NAME, sizeof(info->driver));
 171        strlcpy(info->bus_info, pci_name(hwif->pdev), sizeof(info->bus_info));
 172}
 173
 174static void hinic_get_ringparam(struct net_device *netdev,
 175                                struct ethtool_ringparam *ring)
 176{
 177        ring->rx_max_pending = HINIC_RQ_DEPTH;
 178        ring->tx_max_pending = HINIC_SQ_DEPTH;
 179        ring->rx_pending = HINIC_RQ_DEPTH;
 180        ring->tx_pending = HINIC_SQ_DEPTH;
 181}
 182
 183static void hinic_get_channels(struct net_device *netdev,
 184                               struct ethtool_channels *channels)
 185{
 186        struct hinic_dev *nic_dev = netdev_priv(netdev);
 187        struct hinic_hwdev *hwdev = nic_dev->hwdev;
 188
 189        channels->max_rx = hwdev->nic_cap.max_qps;
 190        channels->max_tx = hwdev->nic_cap.max_qps;
 191        channels->max_other    = 0;
 192        channels->max_combined = 0;
 193        channels->rx_count = hinic_hwdev_num_qps(hwdev);
 194        channels->tx_count = hinic_hwdev_num_qps(hwdev);
 195        channels->other_count    = 0;
 196        channels->combined_count = 0;
 197}
 198
 199static const struct ethtool_ops hinic_ethtool_ops = {
 200        .get_link_ksettings = hinic_get_link_ksettings,
 201        .get_drvinfo = hinic_get_drvinfo,
 202        .get_link = ethtool_op_get_link,
 203        .get_ringparam = hinic_get_ringparam,
 204        .get_channels = hinic_get_channels,
 205};
 206
 207static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
 208{
 209        struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
 210        struct hinic_rxq_stats rx_stats;
 211
 212        u64_stats_init(&rx_stats.syncp);
 213
 214        hinic_rxq_get_stats(rxq, &rx_stats);
 215
 216        u64_stats_update_begin(&nic_rx_stats->syncp);
 217        nic_rx_stats->bytes += rx_stats.bytes;
 218        nic_rx_stats->pkts  += rx_stats.pkts;
 219        u64_stats_update_end(&nic_rx_stats->syncp);
 220
 221        hinic_rxq_clean_stats(rxq);
 222}
 223
 224static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
 225{
 226        struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
 227        struct hinic_txq_stats tx_stats;
 228
 229        u64_stats_init(&tx_stats.syncp);
 230
 231        hinic_txq_get_stats(txq, &tx_stats);
 232
 233        u64_stats_update_begin(&nic_tx_stats->syncp);
 234        nic_tx_stats->bytes += tx_stats.bytes;
 235        nic_tx_stats->pkts += tx_stats.pkts;
 236        nic_tx_stats->tx_busy += tx_stats.tx_busy;
 237        nic_tx_stats->tx_wake += tx_stats.tx_wake;
 238        nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
 239        u64_stats_update_end(&nic_tx_stats->syncp);
 240
 241        hinic_txq_clean_stats(txq);
 242}
 243
 244static void update_nic_stats(struct hinic_dev *nic_dev)
 245{
 246        int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
 247
 248        for (i = 0; i < num_qps; i++)
 249                update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
 250
 251        for (i = 0; i < num_qps; i++)
 252                update_tx_stats(nic_dev, &nic_dev->txqs[i]);
 253}
 254
 255/**
 256 * create_txqs - Create the Logical Tx Queues of specific NIC device
 257 * @nic_dev: the specific NIC device
 258 *
 259 * Return 0 - Success, negative - Failure
 260 **/
 261static int create_txqs(struct hinic_dev *nic_dev)
 262{
 263        int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 264        struct net_device *netdev = nic_dev->netdev;
 265        size_t txq_size;
 266
 267        if (nic_dev->txqs)
 268                return -EINVAL;
 269
 270        txq_size = num_txqs * sizeof(*nic_dev->txqs);
 271        nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
 272        if (!nic_dev->txqs)
 273                return -ENOMEM;
 274
 275        for (i = 0; i < num_txqs; i++) {
 276                struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
 277
 278                err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
 279                if (err) {
 280                        netif_err(nic_dev, drv, netdev,
 281                                  "Failed to init Txq\n");
 282                        goto err_init_txq;
 283                }
 284        }
 285
 286        return 0;
 287
 288err_init_txq:
 289        for (j = 0; j < i; j++)
 290                hinic_clean_txq(&nic_dev->txqs[j]);
 291
 292        devm_kfree(&netdev->dev, nic_dev->txqs);
 293        return err;
 294}
 295
 296/**
 297 * free_txqs - Free the Logical Tx Queues of specific NIC device
 298 * @nic_dev: the specific NIC device
 299 **/
 300static void free_txqs(struct hinic_dev *nic_dev)
 301{
 302        int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 303        struct net_device *netdev = nic_dev->netdev;
 304
 305        if (!nic_dev->txqs)
 306                return;
 307
 308        for (i = 0; i < num_txqs; i++)
 309                hinic_clean_txq(&nic_dev->txqs[i]);
 310
 311        devm_kfree(&netdev->dev, nic_dev->txqs);
 312        nic_dev->txqs = NULL;
 313}
 314
 315/**
 316 * create_txqs - Create the Logical Rx Queues of specific NIC device
 317 * @nic_dev: the specific NIC device
 318 *
 319 * Return 0 - Success, negative - Failure
 320 **/
 321static int create_rxqs(struct hinic_dev *nic_dev)
 322{
 323        int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 324        struct net_device *netdev = nic_dev->netdev;
 325        size_t rxq_size;
 326
 327        if (nic_dev->rxqs)
 328                return -EINVAL;
 329
 330        rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
 331        nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
 332        if (!nic_dev->rxqs)
 333                return -ENOMEM;
 334
 335        for (i = 0; i < num_rxqs; i++) {
 336                struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
 337
 338                err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
 339                if (err) {
 340                        netif_err(nic_dev, drv, netdev,
 341                                  "Failed to init rxq\n");
 342                        goto err_init_rxq;
 343                }
 344        }
 345
 346        return 0;
 347
 348err_init_rxq:
 349        for (j = 0; j < i; j++)
 350                hinic_clean_rxq(&nic_dev->rxqs[j]);
 351
 352        devm_kfree(&netdev->dev, nic_dev->rxqs);
 353        return err;
 354}
 355
 356/**
 357 * free_txqs - Free the Logical Rx Queues of specific NIC device
 358 * @nic_dev: the specific NIC device
 359 **/
 360static void free_rxqs(struct hinic_dev *nic_dev)
 361{
 362        int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
 363        struct net_device *netdev = nic_dev->netdev;
 364
 365        if (!nic_dev->rxqs)
 366                return;
 367
 368        for (i = 0; i < num_rxqs; i++)
 369                hinic_clean_rxq(&nic_dev->rxqs[i]);
 370
 371        devm_kfree(&netdev->dev, nic_dev->rxqs);
 372        nic_dev->rxqs = NULL;
 373}
 374
 375static int hinic_open(struct net_device *netdev)
 376{
 377        struct hinic_dev *nic_dev = netdev_priv(netdev);
 378        enum hinic_port_link_state link_state;
 379        int err, ret, num_qps;
 380
 381        if (!(nic_dev->flags & HINIC_INTF_UP)) {
 382                err = hinic_hwdev_ifup(nic_dev->hwdev);
 383                if (err) {
 384                        netif_err(nic_dev, drv, netdev,
 385                                  "Failed - HW interface up\n");
 386                        return err;
 387                }
 388        }
 389
 390        err = create_txqs(nic_dev);
 391        if (err) {
 392                netif_err(nic_dev, drv, netdev,
 393                          "Failed to create Tx queues\n");
 394                goto err_create_txqs;
 395        }
 396
 397        err = create_rxqs(nic_dev);
 398        if (err) {
 399                netif_err(nic_dev, drv, netdev,
 400                          "Failed to create Rx queues\n");
 401                goto err_create_rxqs;
 402        }
 403
 404        num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
 405        netif_set_real_num_tx_queues(netdev, num_qps);
 406        netif_set_real_num_rx_queues(netdev, num_qps);
 407
 408        err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
 409        if (err) {
 410                netif_err(nic_dev, drv, netdev,
 411                          "Failed to set port state\n");
 412                goto err_port_state;
 413        }
 414
 415        err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
 416        if (err) {
 417                netif_err(nic_dev, drv, netdev,
 418                          "Failed to set func port state\n");
 419                goto err_func_port_state;
 420        }
 421
 422        /* Wait up to 3 sec between port enable to link state */
 423        msleep(3000);
 424
 425        down(&nic_dev->mgmt_lock);
 426
 427        err = hinic_port_link_state(nic_dev, &link_state);
 428        if (err) {
 429                netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
 430                goto err_port_link;
 431        }
 432
 433        if (link_state == HINIC_LINK_STATE_UP)
 434                nic_dev->flags |= HINIC_LINK_UP;
 435
 436        nic_dev->flags |= HINIC_INTF_UP;
 437
 438        if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
 439            (HINIC_LINK_UP | HINIC_INTF_UP)) {
 440                netif_info(nic_dev, drv, netdev, "link + intf UP\n");
 441                netif_carrier_on(netdev);
 442                netif_tx_wake_all_queues(netdev);
 443        }
 444
 445        up(&nic_dev->mgmt_lock);
 446
 447        netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
 448        return 0;
 449
 450err_port_link:
 451        up(&nic_dev->mgmt_lock);
 452        ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
 453        if (ret)
 454                netif_warn(nic_dev, drv, netdev,
 455                           "Failed to revert func port state\n");
 456
 457err_func_port_state:
 458        ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
 459        if (ret)
 460                netif_warn(nic_dev, drv, netdev,
 461                           "Failed to revert port state\n");
 462
 463err_port_state:
 464        free_rxqs(nic_dev);
 465
 466err_create_rxqs:
 467        free_txqs(nic_dev);
 468
 469err_create_txqs:
 470        if (!(nic_dev->flags & HINIC_INTF_UP))
 471                hinic_hwdev_ifdown(nic_dev->hwdev);
 472        return err;
 473}
 474
 475static int hinic_close(struct net_device *netdev)
 476{
 477        struct hinic_dev *nic_dev = netdev_priv(netdev);
 478        unsigned int flags;
 479        int err;
 480
 481        down(&nic_dev->mgmt_lock);
 482
 483        flags = nic_dev->flags;
 484        nic_dev->flags &= ~HINIC_INTF_UP;
 485
 486        netif_carrier_off(netdev);
 487        netif_tx_disable(netdev);
 488
 489        update_nic_stats(nic_dev);
 490
 491        up(&nic_dev->mgmt_lock);
 492
 493        err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
 494        if (err) {
 495                netif_err(nic_dev, drv, netdev,
 496                          "Failed to set func port state\n");
 497                nic_dev->flags |= (flags & HINIC_INTF_UP);
 498                return err;
 499        }
 500
 501        err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
 502        if (err) {
 503                netif_err(nic_dev, drv, netdev, "Failed to set port state\n");
 504                nic_dev->flags |= (flags & HINIC_INTF_UP);
 505                return err;
 506        }
 507
 508        free_rxqs(nic_dev);
 509        free_txqs(nic_dev);
 510
 511        if (flags & HINIC_INTF_UP)
 512                hinic_hwdev_ifdown(nic_dev->hwdev);
 513
 514        netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
 515        return 0;
 516}
 517
 518static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
 519{
 520        struct hinic_dev *nic_dev = netdev_priv(netdev);
 521        int err;
 522
 523        netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
 524
 525        err = hinic_port_set_mtu(nic_dev, new_mtu);
 526        if (err)
 527                netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
 528        else
 529                netdev->mtu = new_mtu;
 530
 531        return err;
 532}
 533
 534/**
 535 * change_mac_addr - change the main mac address of network device
 536 * @netdev: network device
 537 * @addr: mac address to set
 538 *
 539 * Return 0 - Success, negative - Failure
 540 **/
 541static int change_mac_addr(struct net_device *netdev, const u8 *addr)
 542{
 543        struct hinic_dev *nic_dev = netdev_priv(netdev);
 544        u16 vid = 0;
 545        int err;
 546
 547        if (!is_valid_ether_addr(addr))
 548                return -EADDRNOTAVAIL;
 549
 550        netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
 551                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 552
 553        down(&nic_dev->mgmt_lock);
 554
 555        do {
 556                err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
 557                if (err) {
 558                        netif_err(nic_dev, drv, netdev,
 559                                  "Failed to delete mac\n");
 560                        break;
 561                }
 562
 563                err = hinic_port_add_mac(nic_dev, addr, vid);
 564                if (err) {
 565                        netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
 566                        break;
 567                }
 568
 569                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 570        } while (vid != VLAN_N_VID);
 571
 572        up(&nic_dev->mgmt_lock);
 573        return err;
 574}
 575
 576static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
 577{
 578        unsigned char new_mac[ETH_ALEN];
 579        struct sockaddr *saddr = addr;
 580        int err;
 581
 582        memcpy(new_mac, saddr->sa_data, ETH_ALEN);
 583
 584        err = change_mac_addr(netdev, new_mac);
 585        if (!err)
 586                memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
 587
 588        return err;
 589}
 590
 591/**
 592 * add_mac_addr - add mac address to network device
 593 * @netdev: network device
 594 * @addr: mac address to add
 595 *
 596 * Return 0 - Success, negative - Failure
 597 **/
 598static int add_mac_addr(struct net_device *netdev, const u8 *addr)
 599{
 600        struct hinic_dev *nic_dev = netdev_priv(netdev);
 601        u16 vid = 0;
 602        int err;
 603
 604        netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
 605                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 606
 607        down(&nic_dev->mgmt_lock);
 608
 609        do {
 610                err = hinic_port_add_mac(nic_dev, addr, vid);
 611                if (err) {
 612                        netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
 613                        break;
 614                }
 615
 616                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 617        } while (vid != VLAN_N_VID);
 618
 619        up(&nic_dev->mgmt_lock);
 620        return err;
 621}
 622
 623/**
 624 * remove_mac_addr - remove mac address from network device
 625 * @netdev: network device
 626 * @addr: mac address to remove
 627 *
 628 * Return 0 - Success, negative - Failure
 629 **/
 630static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
 631{
 632        struct hinic_dev *nic_dev = netdev_priv(netdev);
 633        u16 vid = 0;
 634        int err;
 635
 636        if (!is_valid_ether_addr(addr))
 637                return -EADDRNOTAVAIL;
 638
 639        netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
 640                   addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
 641
 642        down(&nic_dev->mgmt_lock);
 643
 644        do {
 645                err = hinic_port_del_mac(nic_dev, addr, vid);
 646                if (err) {
 647                        netif_err(nic_dev, drv, netdev,
 648                                  "Failed to delete mac\n");
 649                        break;
 650                }
 651
 652                vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
 653        } while (vid != VLAN_N_VID);
 654
 655        up(&nic_dev->mgmt_lock);
 656        return err;
 657}
 658
 659static int hinic_vlan_rx_add_vid(struct net_device *netdev,
 660                                 __always_unused __be16 proto, u16 vid)
 661{
 662        struct hinic_dev *nic_dev = netdev_priv(netdev);
 663        int ret, err;
 664
 665        netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
 666
 667        down(&nic_dev->mgmt_lock);
 668
 669        err = hinic_port_add_vlan(nic_dev, vid);
 670        if (err) {
 671                netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
 672                goto err_vlan_add;
 673        }
 674
 675        err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
 676        if (err) {
 677                netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
 678                goto err_add_mac;
 679        }
 680
 681        bitmap_set(nic_dev->vlan_bitmap, vid, 1);
 682
 683        up(&nic_dev->mgmt_lock);
 684        return 0;
 685
 686err_add_mac:
 687        ret = hinic_port_del_vlan(nic_dev, vid);
 688        if (ret)
 689                netif_err(nic_dev, drv, netdev,
 690                          "Failed to revert by removing vlan\n");
 691
 692err_vlan_add:
 693        up(&nic_dev->mgmt_lock);
 694        return err;
 695}
 696
 697static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
 698                                  __always_unused __be16 proto, u16 vid)
 699{
 700        struct hinic_dev *nic_dev = netdev_priv(netdev);
 701        int err;
 702
 703        netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
 704
 705        down(&nic_dev->mgmt_lock);
 706
 707        err = hinic_port_del_vlan(nic_dev, vid);
 708        if (err) {
 709                netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
 710                goto err_del_vlan;
 711        }
 712
 713        bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
 714
 715        up(&nic_dev->mgmt_lock);
 716        return 0;
 717
 718err_del_vlan:
 719        up(&nic_dev->mgmt_lock);
 720        return err;
 721}
 722
 723static void set_rx_mode(struct work_struct *work)
 724{
 725        struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
 726        struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
 727        struct netdev_hw_addr *ha;
 728
 729        netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
 730
 731        hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
 732
 733        __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
 734        __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
 735
 736        netdev_for_each_mc_addr(ha, nic_dev->netdev)
 737                add_mac_addr(nic_dev->netdev, ha->addr);
 738}
 739
 740static void hinic_set_rx_mode(struct net_device *netdev)
 741{
 742        struct hinic_dev *nic_dev = netdev_priv(netdev);
 743        struct hinic_rx_mode_work *rx_mode_work;
 744        u32 rx_mode;
 745
 746        rx_mode_work = &nic_dev->rx_mode_work;
 747
 748        rx_mode = HINIC_RX_MODE_UC |
 749                  HINIC_RX_MODE_MC |
 750                  HINIC_RX_MODE_BC;
 751
 752        if (netdev->flags & IFF_PROMISC)
 753                rx_mode |= HINIC_RX_MODE_PROMISC;
 754        else if (netdev->flags & IFF_ALLMULTI)
 755                rx_mode |= HINIC_RX_MODE_MC_ALL;
 756
 757        rx_mode_work->rx_mode = rx_mode;
 758
 759        queue_work(nic_dev->workq, &rx_mode_work->work);
 760}
 761
 762static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
 763{
 764        struct hinic_dev *nic_dev = netdev_priv(netdev);
 765
 766        netif_err(nic_dev, drv, netdev, "Tx timeout\n");
 767}
 768
 769static void hinic_get_stats64(struct net_device *netdev,
 770                              struct rtnl_link_stats64 *stats)
 771{
 772        struct hinic_dev *nic_dev = netdev_priv(netdev);
 773        struct hinic_rxq_stats *nic_rx_stats;
 774        struct hinic_txq_stats *nic_tx_stats;
 775
 776        nic_rx_stats = &nic_dev->rx_stats;
 777        nic_tx_stats = &nic_dev->tx_stats;
 778
 779        down(&nic_dev->mgmt_lock);
 780
 781        if (nic_dev->flags & HINIC_INTF_UP)
 782                update_nic_stats(nic_dev);
 783
 784        up(&nic_dev->mgmt_lock);
 785
 786        stats->rx_bytes   = nic_rx_stats->bytes;
 787        stats->rx_packets = nic_rx_stats->pkts;
 788
 789        stats->tx_bytes   = nic_tx_stats->bytes;
 790        stats->tx_packets = nic_tx_stats->pkts;
 791        stats->tx_errors  = nic_tx_stats->tx_dropped;
 792}
 793
 794#ifdef CONFIG_NET_POLL_CONTROLLER
 795static void hinic_netpoll(struct net_device *netdev)
 796{
 797        struct hinic_dev *nic_dev = netdev_priv(netdev);
 798        int i, num_qps;
 799
 800        num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
 801        for (i = 0; i < num_qps; i++) {
 802                struct hinic_txq *txq = &nic_dev->txqs[i];
 803                struct hinic_rxq *rxq = &nic_dev->rxqs[i];
 804
 805                napi_schedule(&txq->napi);
 806                napi_schedule(&rxq->napi);
 807        }
 808}
 809#endif
 810
 811static const struct net_device_ops hinic_netdev_ops = {
 812        .ndo_open = hinic_open,
 813        .ndo_stop = hinic_close,
 814        .ndo_change_mtu = hinic_change_mtu,
 815        .ndo_set_mac_address = hinic_set_mac_addr,
 816        .ndo_validate_addr = eth_validate_addr,
 817        .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
 818        .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
 819        .ndo_set_rx_mode = hinic_set_rx_mode,
 820        .ndo_start_xmit = hinic_xmit_frame,
 821        .ndo_tx_timeout = hinic_tx_timeout,
 822        .ndo_get_stats64 = hinic_get_stats64,
 823#ifdef CONFIG_NET_POLL_CONTROLLER
 824        .ndo_poll_controller = hinic_netpoll,
 825#endif
 826};
 827
 828static void netdev_features_init(struct net_device *netdev)
 829{
 830        netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
 831                              NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
 832                              NETIF_F_RXCSUM;
 833
 834        netdev->vlan_features = netdev->hw_features;
 835
 836        netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
 837}
 838
 839/**
 840 * link_status_event_handler - link event handler
 841 * @handle: nic device for the handler
 842 * @buf_in: input buffer
 843 * @in_size: input size
 844 * @buf_in: output buffer
 845 * @out_size: returned output size
 846 *
 847 * Return 0 - Success, negative - Failure
 848 **/
 849static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
 850                                      void *buf_out, u16 *out_size)
 851{
 852        struct hinic_port_link_status *link_status, *ret_link_status;
 853        struct hinic_dev *nic_dev = handle;
 854
 855        link_status = buf_in;
 856
 857        if (link_status->link == HINIC_LINK_STATE_UP) {
 858                down(&nic_dev->mgmt_lock);
 859
 860                nic_dev->flags |= HINIC_LINK_UP;
 861
 862                if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
 863                    (HINIC_LINK_UP | HINIC_INTF_UP)) {
 864                        netif_carrier_on(nic_dev->netdev);
 865                        netif_tx_wake_all_queues(nic_dev->netdev);
 866                }
 867
 868                up(&nic_dev->mgmt_lock);
 869
 870                netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
 871        } else {
 872                down(&nic_dev->mgmt_lock);
 873
 874                nic_dev->flags &= ~HINIC_LINK_UP;
 875
 876                netif_carrier_off(nic_dev->netdev);
 877                netif_tx_disable(nic_dev->netdev);
 878
 879                up(&nic_dev->mgmt_lock);
 880
 881                netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
 882        }
 883
 884        ret_link_status = buf_out;
 885        ret_link_status->status = 0;
 886
 887        *out_size = sizeof(*ret_link_status);
 888}
 889
 890static int set_features(struct hinic_dev *nic_dev,
 891                        netdev_features_t pre_features,
 892                        netdev_features_t features, bool force_change)
 893{
 894        netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
 895        u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
 896        int err = 0;
 897
 898        if (changed & NETIF_F_TSO)
 899                err = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
 900                                         HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
 901
 902        if (changed & NETIF_F_RXCSUM)
 903                err = hinic_set_rx_csum_offload(nic_dev, csum_en);
 904
 905        return err;
 906}
 907
 908/**
 909 * nic_dev_init - Initialize the NIC device
 910 * @pdev: the NIC pci device
 911 *
 912 * Return 0 - Success, negative - Failure
 913 **/
 914static int nic_dev_init(struct pci_dev *pdev)
 915{
 916        struct hinic_rx_mode_work *rx_mode_work;
 917        struct hinic_txq_stats *tx_stats;
 918        struct hinic_rxq_stats *rx_stats;
 919        struct hinic_dev *nic_dev;
 920        struct net_device *netdev;
 921        struct hinic_hwdev *hwdev;
 922        int err, num_qps;
 923
 924        hwdev = hinic_init_hwdev(pdev);
 925        if (IS_ERR(hwdev)) {
 926                dev_err(&pdev->dev, "Failed to initialize HW device\n");
 927                return PTR_ERR(hwdev);
 928        }
 929
 930        num_qps = hinic_hwdev_num_qps(hwdev);
 931        if (num_qps <= 0) {
 932                dev_err(&pdev->dev, "Invalid number of QPS\n");
 933                err = -EINVAL;
 934                goto err_num_qps;
 935        }
 936
 937        netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
 938        if (!netdev) {
 939                dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
 940                err = -ENOMEM;
 941                goto err_alloc_etherdev;
 942        }
 943
 944        netdev->netdev_ops = &hinic_netdev_ops;
 945        netdev->ethtool_ops = &hinic_ethtool_ops;
 946        netdev->max_mtu = ETH_MAX_MTU;
 947
 948        nic_dev = netdev_priv(netdev);
 949        nic_dev->netdev = netdev;
 950        nic_dev->hwdev  = hwdev;
 951        nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
 952        nic_dev->flags = 0;
 953        nic_dev->txqs = NULL;
 954        nic_dev->rxqs = NULL;
 955        nic_dev->tx_weight = tx_weight;
 956        nic_dev->rx_weight = rx_weight;
 957
 958        sema_init(&nic_dev->mgmt_lock, 1);
 959
 960        tx_stats = &nic_dev->tx_stats;
 961        rx_stats = &nic_dev->rx_stats;
 962
 963        u64_stats_init(&tx_stats->syncp);
 964        u64_stats_init(&rx_stats->syncp);
 965
 966        nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
 967                                            VLAN_BITMAP_SIZE(nic_dev),
 968                                            GFP_KERNEL);
 969        if (!nic_dev->vlan_bitmap) {
 970                err = -ENOMEM;
 971                goto err_vlan_bitmap;
 972        }
 973
 974        nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
 975        if (!nic_dev->workq) {
 976                err = -ENOMEM;
 977                goto err_workq;
 978        }
 979
 980        pci_set_drvdata(pdev, netdev);
 981
 982        err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
 983        if (err)
 984                dev_warn(&pdev->dev, "Failed to get mac address\n");
 985
 986        err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
 987        if (err) {
 988                dev_err(&pdev->dev, "Failed to add mac\n");
 989                goto err_add_mac;
 990        }
 991
 992        err = hinic_port_set_mtu(nic_dev, netdev->mtu);
 993        if (err) {
 994                dev_err(&pdev->dev, "Failed to set mtu\n");
 995                goto err_set_mtu;
 996        }
 997
 998        rx_mode_work = &nic_dev->rx_mode_work;
 999        INIT_WORK(&rx_mode_work->work, set_rx_mode);
1000
1001        netdev_features_init(netdev);
1002
1003        netif_carrier_off(netdev);
1004
1005        hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1006                                nic_dev, link_status_event_handler);
1007
1008        err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1009        if (err)
1010                goto err_set_features;
1011
1012        SET_NETDEV_DEV(netdev, &pdev->dev);
1013
1014        err = register_netdev(netdev);
1015        if (err) {
1016                dev_err(&pdev->dev, "Failed to register netdev\n");
1017                goto err_reg_netdev;
1018        }
1019
1020        return 0;
1021
1022err_reg_netdev:
1023err_set_features:
1024        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1025                                  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1026        cancel_work_sync(&rx_mode_work->work);
1027
1028err_set_mtu:
1029err_add_mac:
1030        pci_set_drvdata(pdev, NULL);
1031        destroy_workqueue(nic_dev->workq);
1032
1033err_workq:
1034err_vlan_bitmap:
1035        free_netdev(netdev);
1036
1037err_alloc_etherdev:
1038err_num_qps:
1039        hinic_free_hwdev(hwdev);
1040        return err;
1041}
1042
1043static int hinic_probe(struct pci_dev *pdev,
1044                       const struct pci_device_id *id)
1045{
1046        int err = pci_enable_device(pdev);
1047
1048        if (err) {
1049                dev_err(&pdev->dev, "Failed to enable PCI device\n");
1050                return err;
1051        }
1052
1053        err = pci_request_regions(pdev, HINIC_DRV_NAME);
1054        if (err) {
1055                dev_err(&pdev->dev, "Failed to request PCI regions\n");
1056                goto err_pci_regions;
1057        }
1058
1059        pci_set_master(pdev);
1060
1061        err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1062        if (err) {
1063                dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1064                err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1065                if (err) {
1066                        dev_err(&pdev->dev, "Failed to set DMA mask\n");
1067                        goto err_dma_mask;
1068                }
1069        }
1070
1071        err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1072        if (err) {
1073                dev_warn(&pdev->dev,
1074                         "Couldn't set 64-bit consistent DMA mask\n");
1075                err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1076                if (err) {
1077                        dev_err(&pdev->dev,
1078                                "Failed to set consistent DMA mask\n");
1079                        goto err_dma_consistent_mask;
1080                }
1081        }
1082
1083        err = nic_dev_init(pdev);
1084        if (err) {
1085                dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1086                goto err_nic_dev_init;
1087        }
1088
1089        dev_info(&pdev->dev, "HiNIC driver - probed\n");
1090        return 0;
1091
1092err_nic_dev_init:
1093err_dma_consistent_mask:
1094err_dma_mask:
1095        pci_release_regions(pdev);
1096
1097err_pci_regions:
1098        pci_disable_device(pdev);
1099        return err;
1100}
1101
1102static void hinic_remove(struct pci_dev *pdev)
1103{
1104        struct net_device *netdev = pci_get_drvdata(pdev);
1105        struct hinic_dev *nic_dev = netdev_priv(netdev);
1106        struct hinic_rx_mode_work *rx_mode_work;
1107
1108        unregister_netdev(netdev);
1109
1110        hinic_hwdev_cb_unregister(nic_dev->hwdev,
1111                                  HINIC_MGMT_MSG_CMD_LINK_STATUS);
1112
1113        rx_mode_work = &nic_dev->rx_mode_work;
1114        cancel_work_sync(&rx_mode_work->work);
1115
1116        pci_set_drvdata(pdev, NULL);
1117
1118        destroy_workqueue(nic_dev->workq);
1119
1120        hinic_free_hwdev(nic_dev->hwdev);
1121
1122        free_netdev(netdev);
1123
1124        pci_release_regions(pdev);
1125        pci_disable_device(pdev);
1126
1127        dev_info(&pdev->dev, "HiNIC driver - removed\n");
1128}
1129
1130static void hinic_shutdown(struct pci_dev *pdev)
1131{
1132        pci_disable_device(pdev);
1133}
1134
1135static const struct pci_device_id hinic_pci_table[] = {
1136        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1137        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1138        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1139        { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1140        { 0, 0}
1141};
1142MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1143
1144static struct pci_driver hinic_driver = {
1145        .name           = HINIC_DRV_NAME,
1146        .id_table       = hinic_pci_table,
1147        .probe          = hinic_probe,
1148        .remove         = hinic_remove,
1149        .shutdown       = hinic_shutdown,
1150};
1151
1152module_pci_driver(hinic_driver);
1153