linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c
<<
>>
Prefs
   1/*
   2 * QLogic qlcnic NIC Driver
   3 * Copyright (c) 2009-2013 QLogic Corporation
   4 *
   5 * See LICENSE.qlcnic for copyright and licensing details.
   6 */
   7
   8#include <linux/vmalloc.h>
   9#include <linux/interrupt.h>
  10
  11#include "qlcnic.h"
  12#include "qlcnic_sriov.h"
  13#include "qlcnic_hw.h"
  14
  15#include <linux/swab.h>
  16#include <linux/dma-mapping.h>
  17#include <linux/if_vlan.h>
  18#include <net/ip.h>
  19#include <linux/ipv6.h>
  20#include <linux/inetdevice.h>
  21#include <linux/aer.h>
  22#include <linux/log2.h>
  23#include <linux/pci.h>
  24#ifdef CONFIG_QLCNIC_VXLAN
  25#include <net/vxlan.h>
  26#endif
  27
  28MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
  29MODULE_LICENSE("GPL");
  30MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
  31MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
  32
  33char qlcnic_driver_name[] = "qlcnic";
  34static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
  35        "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
  36
  37static int qlcnic_mac_learn;
  38module_param(qlcnic_mac_learn, int, 0444);
  39MODULE_PARM_DESC(qlcnic_mac_learn,
  40                 "Mac Filter (0=learning is disabled, 1=Driver learning is enabled, 2=FDB learning is enabled)");
  41
  42int qlcnic_use_msi = 1;
  43MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled)");
  44module_param_named(use_msi, qlcnic_use_msi, int, 0444);
  45
  46int qlcnic_use_msi_x = 1;
  47MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled)");
  48module_param_named(use_msi_x, qlcnic_use_msi_x, int, 0444);
  49
  50int qlcnic_auto_fw_reset = 1;
  51MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled)");
  52module_param_named(auto_fw_reset, qlcnic_auto_fw_reset, int, 0644);
  53
  54int qlcnic_load_fw_file;
  55MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file, 2=POST in fast mode, 3= POST in medium mode, 4=POST in slow mode)");
  56module_param_named(load_fw_file, qlcnic_load_fw_file, int, 0444);
  57
  58static int qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  59static void qlcnic_remove(struct pci_dev *pdev);
  60static int qlcnic_open(struct net_device *netdev);
  61static int qlcnic_close(struct net_device *netdev);
  62static void qlcnic_tx_timeout(struct net_device *netdev);
  63static void qlcnic_attach_work(struct work_struct *work);
  64static void qlcnic_fwinit_work(struct work_struct *work);
  65#ifdef CONFIG_NET_POLL_CONTROLLER
  66static void qlcnic_poll_controller(struct net_device *netdev);
  67#endif
  68
  69static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
  70static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
  71
  72static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
  73static irqreturn_t qlcnic_intr(int irq, void *data);
  74static irqreturn_t qlcnic_msi_intr(int irq, void *data);
  75static irqreturn_t qlcnic_msix_intr(int irq, void *data);
  76static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data);
  77
  78static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
  79static int qlcnic_start_firmware(struct qlcnic_adapter *);
  80
  81static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
  82static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
  83static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
  84static int qlcnic_vlan_rx_add(struct net_device *, __be16, u16);
  85static int qlcnic_vlan_rx_del(struct net_device *, __be16, u16);
  86
  87static int qlcnic_82xx_setup_intr(struct qlcnic_adapter *);
  88static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *, u32);
  89static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *);
  90static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *);
  91static int qlcnic_82xx_start_firmware(struct qlcnic_adapter *);
  92static void qlcnic_82xx_io_resume(struct pci_dev *);
  93static void qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter *);
  94static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *,
  95                                                      pci_channel_state_t);
  96static u32 qlcnic_vlan_tx_check(struct qlcnic_adapter *adapter)
  97{
  98        struct qlcnic_hardware_context *ahw = adapter->ahw;
  99
 100        if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X)
 101                return ahw->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX;
 102        else
 103                return 1;
 104}
 105
 106/*  PCI Device ID Table  */
 107#define ENTRY(device) \
 108        {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
 109        .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
 110
 111static const struct pci_device_id qlcnic_pci_tbl[] = {
 112        ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
 113        ENTRY(PCI_DEVICE_ID_QLOGIC_QLE834X),
 114        ENTRY(PCI_DEVICE_ID_QLOGIC_QLE8830),
 115        ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE834X),
 116        ENTRY(PCI_DEVICE_ID_QLOGIC_QLE844X),
 117        ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE844X),
 118        {0,}
 119};
 120
 121MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
 122
 123
 124inline void qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring *tx_ring)
 125{
 126        writel(tx_ring->producer, tx_ring->crb_cmd_producer);
 127}
 128
 129static const u32 msi_tgt_status[8] = {
 130        ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
 131        ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
 132        ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
 133        ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
 134};
 135
 136static const u32 qlcnic_reg_tbl[] = {
 137        0x1B20A8,       /* PEG_HALT_STAT1 */
 138        0x1B20AC,       /* PEG_HALT_STAT2 */
 139        0x1B20B0,       /* FW_HEARTBEAT */
 140        0x1B2100,       /* LOCK ID */
 141        0x1B2128,       /* FW_CAPABILITIES */
 142        0x1B2138,       /* drv active */
 143        0x1B2140,       /* dev state */
 144        0x1B2144,       /* drv state */
 145        0x1B2148,       /* drv scratch */
 146        0x1B214C,       /* dev partition info */
 147        0x1B2174,       /* drv idc ver */
 148        0x1B2150,       /* fw version major */
 149        0x1B2154,       /* fw version minor */
 150        0x1B2158,       /* fw version sub */
 151        0x1B219C,       /* npar state */
 152        0x1B21FC,       /* FW_IMG_VALID */
 153        0x1B2250,       /* CMD_PEG_STATE */
 154        0x1B233C,       /* RCV_PEG_STATE */
 155        0x1B23B4,       /* ASIC TEMP */
 156        0x1B216C,       /* FW api */
 157        0x1B2170,       /* drv op mode */
 158        0x13C010,       /* flash lock */
 159        0x13C014,       /* flash unlock */
 160};
 161
 162static const struct qlcnic_board_info qlcnic_boards[] = {
 163        { PCI_VENDOR_ID_QLOGIC,
 164          PCI_DEVICE_ID_QLOGIC_QLE844X,
 165          0x0,
 166          0x0,
 167          "8400 series 10GbE Converged Network Adapter (TCP/IP Networking)" },
 168        { PCI_VENDOR_ID_QLOGIC,
 169          PCI_DEVICE_ID_QLOGIC_QLE834X,
 170          PCI_VENDOR_ID_QLOGIC,
 171          0x24e,
 172          "8300 Series Dual Port 10GbE Converged Network Adapter "
 173          "(TCP/IP Networking)" },
 174        { PCI_VENDOR_ID_QLOGIC,
 175          PCI_DEVICE_ID_QLOGIC_QLE834X,
 176          PCI_VENDOR_ID_QLOGIC,
 177          0x243,
 178          "8300 Series Single Port 10GbE Converged Network Adapter "
 179          "(TCP/IP Networking)" },
 180        { PCI_VENDOR_ID_QLOGIC,
 181          PCI_DEVICE_ID_QLOGIC_QLE834X,
 182          PCI_VENDOR_ID_QLOGIC,
 183          0x24a,
 184          "8300 Series Dual Port 10GbE Converged Network Adapter "
 185          "(TCP/IP Networking)" },
 186        { PCI_VENDOR_ID_QLOGIC,
 187          PCI_DEVICE_ID_QLOGIC_QLE834X,
 188          PCI_VENDOR_ID_QLOGIC,
 189          0x246,
 190          "8300 Series Dual Port 10GbE Converged Network Adapter "
 191          "(TCP/IP Networking)" },
 192        { PCI_VENDOR_ID_QLOGIC,
 193          PCI_DEVICE_ID_QLOGIC_QLE834X,
 194          PCI_VENDOR_ID_QLOGIC,
 195          0x252,
 196          "8300 Series Dual Port 10GbE Converged Network Adapter "
 197          "(TCP/IP Networking)" },
 198        { PCI_VENDOR_ID_QLOGIC,
 199          PCI_DEVICE_ID_QLOGIC_QLE834X,
 200          PCI_VENDOR_ID_QLOGIC,
 201          0x26e,
 202          "8300 Series Dual Port 10GbE Converged Network Adapter "
 203          "(TCP/IP Networking)" },
 204        { PCI_VENDOR_ID_QLOGIC,
 205          PCI_DEVICE_ID_QLOGIC_QLE834X,
 206          PCI_VENDOR_ID_QLOGIC,
 207          0x260,
 208          "8300 Series Dual Port 10GbE Converged Network Adapter "
 209          "(TCP/IP Networking)" },
 210        { PCI_VENDOR_ID_QLOGIC,
 211          PCI_DEVICE_ID_QLOGIC_QLE834X,
 212          PCI_VENDOR_ID_QLOGIC,
 213          0x266,
 214          "8300 Series Single Port 10GbE Converged Network Adapter "
 215          "(TCP/IP Networking)" },
 216        { PCI_VENDOR_ID_QLOGIC,
 217          PCI_DEVICE_ID_QLOGIC_QLE834X,
 218          PCI_VENDOR_ID_QLOGIC,
 219          0x269,
 220          "8300 Series Dual Port 10GbE Converged Network Adapter "
 221          "(TCP/IP Networking)" },
 222        { PCI_VENDOR_ID_QLOGIC,
 223          PCI_DEVICE_ID_QLOGIC_QLE834X,
 224          PCI_VENDOR_ID_QLOGIC,
 225          0x271,
 226          "8300 Series Dual Port 10GbE Converged Network Adapter "
 227          "(TCP/IP Networking)" },
 228        { PCI_VENDOR_ID_QLOGIC,
 229          PCI_DEVICE_ID_QLOGIC_QLE834X,
 230          0x0, 0x0, "8300 Series 1/10GbE Controller" },
 231        { PCI_VENDOR_ID_QLOGIC,
 232          PCI_DEVICE_ID_QLOGIC_QLE8830,
 233          0x0,
 234          0x0,
 235          "8830 Series 1/10GbE Controller" },
 236        { PCI_VENDOR_ID_QLOGIC,
 237          PCI_DEVICE_ID_QLOGIC_QLE824X,
 238          PCI_VENDOR_ID_QLOGIC,
 239          0x203,
 240          "8200 Series Single Port 10GbE Converged Network Adapter"
 241          "(TCP/IP Networking)" },
 242        { PCI_VENDOR_ID_QLOGIC,
 243          PCI_DEVICE_ID_QLOGIC_QLE824X,
 244          PCI_VENDOR_ID_QLOGIC,
 245          0x207,
 246          "8200 Series Dual Port 10GbE Converged Network Adapter"
 247          "(TCP/IP Networking)" },
 248        { PCI_VENDOR_ID_QLOGIC,
 249          PCI_DEVICE_ID_QLOGIC_QLE824X,
 250          PCI_VENDOR_ID_QLOGIC,
 251          0x20b,
 252          "3200 Series Dual Port 10Gb Intelligent Ethernet Adapter" },
 253        { PCI_VENDOR_ID_QLOGIC,
 254          PCI_DEVICE_ID_QLOGIC_QLE824X,
 255          PCI_VENDOR_ID_QLOGIC,
 256          0x20c,
 257          "3200 Series Quad Port 1Gb Intelligent Ethernet Adapter" },
 258        { PCI_VENDOR_ID_QLOGIC,
 259          PCI_DEVICE_ID_QLOGIC_QLE824X,
 260          PCI_VENDOR_ID_QLOGIC,
 261          0x20f,
 262          "3200 Series Single Port 10Gb Intelligent Ethernet Adapter" },
 263        { PCI_VENDOR_ID_QLOGIC,
 264          PCI_DEVICE_ID_QLOGIC_QLE824X,
 265          0x103c, 0x3733,
 266          "NC523SFP 10Gb 2-port Server Adapter" },
 267        { PCI_VENDOR_ID_QLOGIC,
 268          PCI_DEVICE_ID_QLOGIC_QLE824X,
 269          0x103c, 0x3346,
 270          "CN1000Q Dual Port Converged Network Adapter" },
 271        { PCI_VENDOR_ID_QLOGIC,
 272          PCI_DEVICE_ID_QLOGIC_QLE824X,
 273          PCI_VENDOR_ID_QLOGIC,
 274          0x210,
 275          "QME8242-k 10GbE Dual Port Mezzanine Card" },
 276        { PCI_VENDOR_ID_QLOGIC,
 277          PCI_DEVICE_ID_QLOGIC_QLE824X,
 278          0x0, 0x0, "cLOM8214 1/10GbE Controller" },
 279};
 280
 281#define NUM_SUPPORTED_BOARDS ARRAY_SIZE(qlcnic_boards)
 282
 283static const
 284struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
 285
 286int qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
 287{
 288        int size = sizeof(struct qlcnic_host_sds_ring) * count;
 289
 290        recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
 291
 292        return recv_ctx->sds_rings == NULL;
 293}
 294
 295void qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
 296{
 297        kfree(recv_ctx->sds_rings);
 298        recv_ctx->sds_rings = NULL;
 299}
 300
 301int qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
 302{
 303        struct net_device *netdev = adapter->netdev;
 304        struct pci_dev *pdev = adapter->pdev;
 305        u8 mac_addr[ETH_ALEN];
 306        int ret;
 307
 308        ret = qlcnic_get_mac_address(adapter, mac_addr,
 309                                     adapter->ahw->pci_func);
 310        if (ret)
 311                return ret;
 312
 313        memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
 314        memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
 315
 316        /* set station address */
 317
 318        if (!is_valid_ether_addr(netdev->dev_addr))
 319                dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
 320                                        netdev->dev_addr);
 321
 322        return 0;
 323}
 324
 325static void qlcnic_delete_adapter_mac(struct qlcnic_adapter *adapter)
 326{
 327        struct qlcnic_mac_vlan_list *cur;
 328        struct list_head *head;
 329
 330        list_for_each(head, &adapter->mac_list) {
 331                cur = list_entry(head, struct qlcnic_mac_vlan_list, list);
 332                if (ether_addr_equal_unaligned(adapter->mac_addr, cur->mac_addr)) {
 333                        qlcnic_sre_macaddr_change(adapter, cur->mac_addr,
 334                                                  0, QLCNIC_MAC_DEL);
 335                        list_del(&cur->list);
 336                        kfree(cur);
 337                        return;
 338                }
 339        }
 340}
 341
 342static int qlcnic_set_mac(struct net_device *netdev, void *p)
 343{
 344        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 345        struct sockaddr *addr = p;
 346
 347        if (qlcnic_sriov_vf_check(adapter))
 348                return -EINVAL;
 349
 350        if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
 351                return -EOPNOTSUPP;
 352
 353        if (!is_valid_ether_addr(addr->sa_data))
 354                return -EINVAL;
 355
 356        if (ether_addr_equal_unaligned(adapter->mac_addr, addr->sa_data))
 357                return 0;
 358
 359        if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 360                netif_device_detach(netdev);
 361                qlcnic_napi_disable(adapter);
 362        }
 363
 364        qlcnic_delete_adapter_mac(adapter);
 365        memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
 366        memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
 367        qlcnic_set_multi(adapter->netdev);
 368
 369        if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 370                netif_device_attach(netdev);
 371                qlcnic_napi_enable(adapter);
 372        }
 373        return 0;
 374}
 375
 376static int qlcnic_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
 377                        struct net_device *netdev,
 378                        const unsigned char *addr, u16 vid)
 379{
 380        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 381        int err = -EOPNOTSUPP;
 382
 383        if (!adapter->fdb_mac_learn)
 384                return ndo_dflt_fdb_del(ndm, tb, netdev, addr, vid);
 385
 386        if ((adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
 387            qlcnic_sriov_check(adapter)) {
 388                if (is_unicast_ether_addr(addr)) {
 389                        err = dev_uc_del(netdev, addr);
 390                        if (!err)
 391                                err = qlcnic_nic_del_mac(adapter, addr);
 392                } else if (is_multicast_ether_addr(addr)) {
 393                        err = dev_mc_del(netdev, addr);
 394                } else {
 395                        err =  -EINVAL;
 396                }
 397        }
 398        return err;
 399}
 400
 401static int qlcnic_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 402                        struct net_device *netdev,
 403                        const unsigned char *addr, u16 vid, u16 flags)
 404{
 405        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 406        int err = 0;
 407
 408        if (!adapter->fdb_mac_learn)
 409                return ndo_dflt_fdb_add(ndm, tb, netdev, addr, vid, flags);
 410
 411        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) &&
 412            !qlcnic_sriov_check(adapter)) {
 413                pr_info("%s: FDB e-switch is not enabled\n", __func__);
 414                return -EOPNOTSUPP;
 415        }
 416
 417        if (ether_addr_equal(addr, adapter->mac_addr))
 418                return err;
 419
 420        if (is_unicast_ether_addr(addr)) {
 421                if (netdev_uc_count(netdev) < adapter->ahw->max_uc_count)
 422                        err = dev_uc_add_excl(netdev, addr);
 423                else
 424                        err = -ENOMEM;
 425        } else if (is_multicast_ether_addr(addr)) {
 426                err = dev_mc_add_excl(netdev, addr);
 427        } else {
 428                err = -EINVAL;
 429        }
 430
 431        return err;
 432}
 433
 434static int qlcnic_fdb_dump(struct sk_buff *skb, struct netlink_callback *ncb,
 435                        struct net_device *netdev,
 436                        struct net_device *filter_dev, int idx)
 437{
 438        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 439
 440        if (!adapter->fdb_mac_learn)
 441                return ndo_dflt_fdb_dump(skb, ncb, netdev, filter_dev, idx);
 442
 443        if ((adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
 444            qlcnic_sriov_check(adapter))
 445                idx = ndo_dflt_fdb_dump(skb, ncb, netdev, filter_dev, idx);
 446
 447        return idx;
 448}
 449
 450static void qlcnic_82xx_cancel_idc_work(struct qlcnic_adapter *adapter)
 451{
 452        while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
 453                usleep_range(10000, 11000);
 454
 455        if (!adapter->fw_work.work.func)
 456                return;
 457
 458        cancel_delayed_work_sync(&adapter->fw_work);
 459}
 460
 461static int qlcnic_get_phys_port_id(struct net_device *netdev,
 462                                   struct netdev_phys_item_id *ppid)
 463{
 464        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 465        struct qlcnic_hardware_context *ahw = adapter->ahw;
 466
 467        if (!(adapter->flags & QLCNIC_HAS_PHYS_PORT_ID))
 468                return -EOPNOTSUPP;
 469
 470        ppid->id_len = sizeof(ahw->phys_port_id);
 471        memcpy(ppid->id, ahw->phys_port_id, ppid->id_len);
 472
 473        return 0;
 474}
 475
 476#ifdef CONFIG_QLCNIC_VXLAN
 477static void qlcnic_add_vxlan_port(struct net_device *netdev,
 478                                  sa_family_t sa_family, __be16 port)
 479{
 480        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 481        struct qlcnic_hardware_context *ahw = adapter->ahw;
 482
 483        /* Adapter supports only one VXLAN port. Use very first port
 484         * for enabling offload
 485         */
 486        if (!qlcnic_encap_rx_offload(adapter) || ahw->vxlan_port)
 487                return;
 488
 489        ahw->vxlan_port = ntohs(port);
 490        adapter->flags |= QLCNIC_ADD_VXLAN_PORT;
 491}
 492
 493static void qlcnic_del_vxlan_port(struct net_device *netdev,
 494                                  sa_family_t sa_family, __be16 port)
 495{
 496        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 497        struct qlcnic_hardware_context *ahw = adapter->ahw;
 498
 499        if (!qlcnic_encap_rx_offload(adapter) || !ahw->vxlan_port ||
 500            (ahw->vxlan_port != ntohs(port)))
 501                return;
 502
 503        adapter->flags |= QLCNIC_DEL_VXLAN_PORT;
 504}
 505
 506static netdev_features_t qlcnic_features_check(struct sk_buff *skb,
 507                                               struct net_device *dev,
 508                                               netdev_features_t features)
 509{
 510        features = vlan_features_check(skb, features);
 511        return vxlan_features_check(skb, features);
 512}
 513#endif
 514
 515static const struct net_device_ops qlcnic_netdev_ops = {
 516        .ndo_open          = qlcnic_open,
 517        .ndo_stop          = qlcnic_close,
 518        .ndo_start_xmit    = qlcnic_xmit_frame,
 519        .ndo_get_stats     = qlcnic_get_stats,
 520        .ndo_validate_addr = eth_validate_addr,
 521        .ndo_set_rx_mode   = qlcnic_set_multi,
 522        .ndo_set_mac_address    = qlcnic_set_mac,
 523        .ndo_change_mtu    = qlcnic_change_mtu,
 524        .ndo_fix_features  = qlcnic_fix_features,
 525        .ndo_set_features  = qlcnic_set_features,
 526        .ndo_tx_timeout    = qlcnic_tx_timeout,
 527        .ndo_vlan_rx_add_vid    = qlcnic_vlan_rx_add,
 528        .ndo_vlan_rx_kill_vid   = qlcnic_vlan_rx_del,
 529        .ndo_fdb_add            = qlcnic_fdb_add,
 530        .ndo_fdb_del            = qlcnic_fdb_del,
 531        .ndo_fdb_dump           = qlcnic_fdb_dump,
 532        .ndo_get_phys_port_id   = qlcnic_get_phys_port_id,
 533#ifdef CONFIG_QLCNIC_VXLAN
 534        .ndo_add_vxlan_port     = qlcnic_add_vxlan_port,
 535        .ndo_del_vxlan_port     = qlcnic_del_vxlan_port,
 536        .ndo_features_check     = qlcnic_features_check,
 537#endif
 538#ifdef CONFIG_NET_POLL_CONTROLLER
 539        .ndo_poll_controller = qlcnic_poll_controller,
 540#endif
 541#ifdef CONFIG_QLCNIC_SRIOV
 542        .ndo_set_vf_mac         = qlcnic_sriov_set_vf_mac,
 543        .ndo_set_vf_rate        = qlcnic_sriov_set_vf_tx_rate,
 544        .ndo_get_vf_config      = qlcnic_sriov_get_vf_config,
 545        .ndo_set_vf_vlan        = qlcnic_sriov_set_vf_vlan,
 546        .ndo_set_vf_spoofchk    = qlcnic_sriov_set_vf_spoofchk,
 547#endif
 548};
 549
 550static const struct net_device_ops qlcnic_netdev_failed_ops = {
 551        .ndo_open          = qlcnic_open,
 552};
 553
 554static struct qlcnic_nic_template qlcnic_ops = {
 555        .config_bridged_mode    = qlcnic_config_bridged_mode,
 556        .config_led             = qlcnic_82xx_config_led,
 557        .start_firmware         = qlcnic_82xx_start_firmware,
 558        .request_reset          = qlcnic_82xx_dev_request_reset,
 559        .cancel_idc_work        = qlcnic_82xx_cancel_idc_work,
 560        .napi_add               = qlcnic_82xx_napi_add,
 561        .napi_del               = qlcnic_82xx_napi_del,
 562        .config_ipaddr          = qlcnic_82xx_config_ipaddr,
 563        .shutdown               = qlcnic_82xx_shutdown,
 564        .resume                 = qlcnic_82xx_resume,
 565        .clear_legacy_intr      = qlcnic_82xx_clear_legacy_intr,
 566};
 567
 568struct qlcnic_nic_template qlcnic_vf_ops = {
 569        .config_bridged_mode    = qlcnicvf_config_bridged_mode,
 570        .config_led             = qlcnicvf_config_led,
 571        .start_firmware         = qlcnicvf_start_firmware
 572};
 573
 574static struct qlcnic_hardware_ops qlcnic_hw_ops = {
 575        .read_crb                       = qlcnic_82xx_read_crb,
 576        .write_crb                      = qlcnic_82xx_write_crb,
 577        .read_reg                       = qlcnic_82xx_hw_read_wx_2M,
 578        .write_reg                      = qlcnic_82xx_hw_write_wx_2M,
 579        .get_mac_address                = qlcnic_82xx_get_mac_address,
 580        .setup_intr                     = qlcnic_82xx_setup_intr,
 581        .alloc_mbx_args                 = qlcnic_82xx_alloc_mbx_args,
 582        .mbx_cmd                        = qlcnic_82xx_issue_cmd,
 583        .get_func_no                    = qlcnic_82xx_get_func_no,
 584        .api_lock                       = qlcnic_82xx_api_lock,
 585        .api_unlock                     = qlcnic_82xx_api_unlock,
 586        .add_sysfs                      = qlcnic_82xx_add_sysfs,
 587        .remove_sysfs                   = qlcnic_82xx_remove_sysfs,
 588        .process_lb_rcv_ring_diag       = qlcnic_82xx_process_rcv_ring_diag,
 589        .create_rx_ctx                  = qlcnic_82xx_fw_cmd_create_rx_ctx,
 590        .create_tx_ctx                  = qlcnic_82xx_fw_cmd_create_tx_ctx,
 591        .del_rx_ctx                     = qlcnic_82xx_fw_cmd_del_rx_ctx,
 592        .del_tx_ctx                     = qlcnic_82xx_fw_cmd_del_tx_ctx,
 593        .setup_link_event               = qlcnic_82xx_linkevent_request,
 594        .get_nic_info                   = qlcnic_82xx_get_nic_info,
 595        .get_pci_info                   = qlcnic_82xx_get_pci_info,
 596        .set_nic_info                   = qlcnic_82xx_set_nic_info,
 597        .change_macvlan                 = qlcnic_82xx_sre_macaddr_change,
 598        .napi_enable                    = qlcnic_82xx_napi_enable,
 599        .napi_disable                   = qlcnic_82xx_napi_disable,
 600        .config_intr_coal               = qlcnic_82xx_config_intr_coalesce,
 601        .config_rss                     = qlcnic_82xx_config_rss,
 602        .config_hw_lro                  = qlcnic_82xx_config_hw_lro,
 603        .config_loopback                = qlcnic_82xx_set_lb_mode,
 604        .clear_loopback                 = qlcnic_82xx_clear_lb_mode,
 605        .config_promisc_mode            = qlcnic_82xx_nic_set_promisc,
 606        .change_l2_filter               = qlcnic_82xx_change_filter,
 607        .get_board_info                 = qlcnic_82xx_get_board_info,
 608        .set_mac_filter_count           = qlcnic_82xx_set_mac_filter_count,
 609        .free_mac_list                  = qlcnic_82xx_free_mac_list,
 610        .read_phys_port_id              = qlcnic_82xx_read_phys_port_id,
 611        .io_error_detected              = qlcnic_82xx_io_error_detected,
 612        .io_slot_reset                  = qlcnic_82xx_io_slot_reset,
 613        .io_resume                      = qlcnic_82xx_io_resume,
 614        .get_beacon_state               = qlcnic_82xx_get_beacon_state,
 615        .enable_sds_intr                = qlcnic_82xx_enable_sds_intr,
 616        .disable_sds_intr               = qlcnic_82xx_disable_sds_intr,
 617        .enable_tx_intr                 = qlcnic_82xx_enable_tx_intr,
 618        .disable_tx_intr                = qlcnic_82xx_disable_tx_intr,
 619        .get_saved_state                = qlcnic_82xx_get_saved_state,
 620        .set_saved_state                = qlcnic_82xx_set_saved_state,
 621        .cache_tmpl_hdr_values          = qlcnic_82xx_cache_tmpl_hdr_values,
 622        .get_cap_size                   = qlcnic_82xx_get_cap_size,
 623        .set_sys_info                   = qlcnic_82xx_set_sys_info,
 624        .store_cap_mask                 = qlcnic_82xx_store_cap_mask,
 625};
 626
 627static int qlcnic_check_multi_tx_capability(struct qlcnic_adapter *adapter)
 628{
 629        struct qlcnic_hardware_context *ahw = adapter->ahw;
 630
 631        if (qlcnic_82xx_check(adapter) &&
 632            (ahw->extra_capability[0] & QLCNIC_FW_CAPABILITY_2_MULTI_TX)) {
 633                test_and_set_bit(__QLCNIC_MULTI_TX_UNIQUE, &adapter->state);
 634                return 0;
 635        } else {
 636                return 1;
 637        }
 638}
 639
 640static int qlcnic_max_rings(struct qlcnic_adapter *adapter, u8 ring_cnt,
 641                            int queue_type)
 642{
 643        int num_rings, max_rings = QLCNIC_MAX_SDS_RINGS;
 644
 645        if (queue_type == QLCNIC_RX_QUEUE)
 646                max_rings = adapter->max_sds_rings;
 647        else if (queue_type == QLCNIC_TX_QUEUE)
 648                max_rings = adapter->max_tx_rings;
 649
 650        num_rings = rounddown_pow_of_two(min_t(int, num_online_cpus(),
 651                                              max_rings));
 652
 653        if (ring_cnt > num_rings)
 654                return num_rings;
 655        else
 656                return ring_cnt;
 657}
 658
 659void qlcnic_set_tx_ring_count(struct qlcnic_adapter *adapter, u8 tx_cnt)
 660{
 661        /* 83xx adapter does not have max_tx_rings intialized in probe */
 662        if (adapter->max_tx_rings)
 663                adapter->drv_tx_rings = qlcnic_max_rings(adapter, tx_cnt,
 664                                                         QLCNIC_TX_QUEUE);
 665        else
 666                adapter->drv_tx_rings = tx_cnt;
 667}
 668
 669void qlcnic_set_sds_ring_count(struct qlcnic_adapter *adapter, u8 rx_cnt)
 670{
 671        /* 83xx adapter does not have max_sds_rings intialized in probe */
 672        if (adapter->max_sds_rings)
 673                adapter->drv_sds_rings = qlcnic_max_rings(adapter, rx_cnt,
 674                                                          QLCNIC_RX_QUEUE);
 675        else
 676                adapter->drv_sds_rings = rx_cnt;
 677}
 678
 679int qlcnic_setup_tss_rss_intr(struct qlcnic_adapter *adapter)
 680{
 681        struct pci_dev *pdev = adapter->pdev;
 682        int num_msix = 0, err = 0, vector;
 683
 684        adapter->flags &= ~QLCNIC_TSS_RSS;
 685
 686        if (adapter->drv_tss_rings > 0)
 687                num_msix += adapter->drv_tss_rings;
 688        else
 689                num_msix += adapter->drv_tx_rings;
 690
 691        if (adapter->drv_rss_rings > 0)
 692                num_msix += adapter->drv_rss_rings;
 693        else
 694                num_msix += adapter->drv_sds_rings;
 695
 696        if (qlcnic_83xx_check(adapter))
 697                num_msix += 1;
 698
 699        if (!adapter->msix_entries) {
 700                adapter->msix_entries = kcalloc(num_msix,
 701                                                sizeof(struct msix_entry),
 702                                                GFP_KERNEL);
 703                if (!adapter->msix_entries)
 704                        return -ENOMEM;
 705        }
 706
 707        for (vector = 0; vector < num_msix; vector++)
 708                adapter->msix_entries[vector].entry = vector;
 709
 710restore:
 711        err = pci_enable_msix_exact(pdev, adapter->msix_entries, num_msix);
 712        if (err == -ENOSPC) {
 713                if (!adapter->drv_tss_rings && !adapter->drv_rss_rings)
 714                        return err;
 715
 716                netdev_info(adapter->netdev,
 717                            "Unable to allocate %d MSI-X vectors, Available vectors %d\n",
 718                            num_msix, err);
 719
 720                num_msix = adapter->drv_tx_rings + adapter->drv_sds_rings;
 721
 722                /* Set rings to 0 so we can restore original TSS/RSS count */
 723                adapter->drv_tss_rings = 0;
 724                adapter->drv_rss_rings = 0;
 725
 726                if (qlcnic_83xx_check(adapter))
 727                        num_msix += 1;
 728
 729                netdev_info(adapter->netdev,
 730                            "Restoring %d Tx, %d SDS rings for total %d vectors.\n",
 731                            adapter->drv_tx_rings, adapter->drv_sds_rings,
 732                            num_msix);
 733
 734                goto restore;
 735        } else if (err < 0) {
 736                return err;
 737        }
 738
 739        adapter->ahw->num_msix = num_msix;
 740        if (adapter->drv_tss_rings > 0)
 741                adapter->drv_tx_rings = adapter->drv_tss_rings;
 742
 743        if (adapter->drv_rss_rings > 0)
 744                adapter->drv_sds_rings = adapter->drv_rss_rings;
 745
 746        return 0;
 747}
 748
 749int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
 750{
 751        struct pci_dev *pdev = adapter->pdev;
 752        int err, vector;
 753
 754        if (!adapter->msix_entries) {
 755                adapter->msix_entries = kcalloc(num_msix,
 756                                                sizeof(struct msix_entry),
 757                                                GFP_KERNEL);
 758                if (!adapter->msix_entries)
 759                        return -ENOMEM;
 760        }
 761
 762        adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
 763
 764        if (adapter->ahw->msix_supported) {
 765enable_msix:
 766                for (vector = 0; vector < num_msix; vector++)
 767                        adapter->msix_entries[vector].entry = vector;
 768
 769                err = pci_enable_msix_range(pdev,
 770                                            adapter->msix_entries, 1, num_msix);
 771
 772                if (err == num_msix) {
 773                        adapter->flags |= QLCNIC_MSIX_ENABLED;
 774                        adapter->ahw->num_msix = num_msix;
 775                        dev_info(&pdev->dev, "using msi-x interrupts\n");
 776                        return 0;
 777                } else if (err > 0) {
 778                        pci_disable_msix(pdev);
 779
 780                        dev_info(&pdev->dev,
 781                                 "Unable to allocate %d MSI-X vectors, Available vectors %d\n",
 782                                 num_msix, err);
 783
 784                        if (qlcnic_82xx_check(adapter)) {
 785                                num_msix = rounddown_pow_of_two(err);
 786                                if (err < QLCNIC_82XX_MINIMUM_VECTOR)
 787                                        return -ENOSPC;
 788                        } else {
 789                                num_msix = rounddown_pow_of_two(err - 1);
 790                                num_msix += 1;
 791                                if (err < QLCNIC_83XX_MINIMUM_VECTOR)
 792                                        return -ENOSPC;
 793                        }
 794
 795                        if (qlcnic_82xx_check(adapter) &&
 796                            !qlcnic_check_multi_tx(adapter)) {
 797                                adapter->drv_sds_rings = num_msix;
 798                                adapter->drv_tx_rings = QLCNIC_SINGLE_RING;
 799                        } else {
 800                                /* Distribute vectors equally */
 801                                adapter->drv_tx_rings = num_msix / 2;
 802                                adapter->drv_sds_rings = adapter->drv_tx_rings;
 803                        }
 804
 805                        if (num_msix) {
 806                                dev_info(&pdev->dev,
 807                                         "Trying to allocate %d MSI-X interrupt vectors\n",
 808                                         num_msix);
 809                                goto enable_msix;
 810                        }
 811                } else {
 812                        dev_info(&pdev->dev,
 813                                 "Unable to allocate %d MSI-X vectors, err=%d\n",
 814                                 num_msix, err);
 815                        return err;
 816                }
 817        }
 818
 819        return -EIO;
 820}
 821
 822static int qlcnic_82xx_calculate_msix_vector(struct qlcnic_adapter *adapter)
 823{
 824        int num_msix;
 825
 826        num_msix = adapter->drv_sds_rings;
 827
 828        if (qlcnic_check_multi_tx(adapter))
 829                num_msix += adapter->drv_tx_rings;
 830        else
 831                num_msix += QLCNIC_SINGLE_RING;
 832
 833        return num_msix;
 834}
 835
 836static int qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
 837{
 838        int err = 0;
 839        u32 offset, mask_reg;
 840        const struct qlcnic_legacy_intr_set *legacy_intrp;
 841        struct qlcnic_hardware_context *ahw = adapter->ahw;
 842        struct pci_dev *pdev = adapter->pdev;
 843
 844        if (qlcnic_use_msi && !pci_enable_msi(pdev)) {
 845                adapter->flags |= QLCNIC_MSI_ENABLED;
 846                offset = msi_tgt_status[adapter->ahw->pci_func];
 847                adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter->ahw,
 848                                                            offset);
 849                dev_info(&pdev->dev, "using msi interrupts\n");
 850                adapter->msix_entries[0].vector = pdev->irq;
 851                return err;
 852        }
 853
 854        if (qlcnic_use_msi || qlcnic_use_msi_x)
 855                return -EOPNOTSUPP;
 856
 857        legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
 858        adapter->ahw->int_vec_bit = legacy_intrp->int_vec_bit;
 859        offset = legacy_intrp->tgt_status_reg;
 860        adapter->tgt_status_reg = qlcnic_get_ioaddr(ahw, offset);
 861        mask_reg = legacy_intrp->tgt_mask_reg;
 862        adapter->tgt_mask_reg = qlcnic_get_ioaddr(ahw, mask_reg);
 863        adapter->isr_int_vec = qlcnic_get_ioaddr(ahw, ISR_INT_VECTOR);
 864        adapter->crb_int_state_reg = qlcnic_get_ioaddr(ahw, ISR_INT_STATE_REG);
 865        dev_info(&pdev->dev, "using legacy interrupts\n");
 866        adapter->msix_entries[0].vector = pdev->irq;
 867        return err;
 868}
 869
 870static int qlcnic_82xx_setup_intr(struct qlcnic_adapter *adapter)
 871{
 872        int num_msix, err = 0;
 873
 874        if (adapter->flags & QLCNIC_TSS_RSS) {
 875                err = qlcnic_setup_tss_rss_intr(adapter);
 876                if (err < 0)
 877                        return err;
 878                num_msix = adapter->ahw->num_msix;
 879        } else {
 880                num_msix = qlcnic_82xx_calculate_msix_vector(adapter);
 881
 882                err = qlcnic_enable_msix(adapter, num_msix);
 883                if (err == -ENOMEM)
 884                        return err;
 885
 886                if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
 887                        qlcnic_disable_multi_tx(adapter);
 888                        adapter->drv_sds_rings = QLCNIC_SINGLE_RING;
 889
 890                        err = qlcnic_enable_msi_legacy(adapter);
 891                        if (err)
 892                                return err;
 893                }
 894        }
 895
 896        return 0;
 897}
 898
 899int qlcnic_82xx_mq_intrpt(struct qlcnic_adapter *adapter, int op_type)
 900{
 901        struct qlcnic_hardware_context *ahw = adapter->ahw;
 902        int err, i;
 903
 904        if (qlcnic_check_multi_tx(adapter) &&
 905            !ahw->diag_test &&
 906            (adapter->flags & QLCNIC_MSIX_ENABLED)) {
 907                ahw->intr_tbl = vzalloc(ahw->num_msix *
 908                                        sizeof(struct qlcnic_intrpt_config));
 909                if (!ahw->intr_tbl)
 910                        return -ENOMEM;
 911
 912                for (i = 0; i < ahw->num_msix; i++) {
 913                        ahw->intr_tbl[i].type = QLCNIC_INTRPT_MSIX;
 914                        ahw->intr_tbl[i].id = i;
 915                        ahw->intr_tbl[i].src = 0;
 916                }
 917
 918                err = qlcnic_82xx_config_intrpt(adapter, 1);
 919                if (err)
 920                        dev_err(&adapter->pdev->dev,
 921                                "Failed to configure Interrupt for %d vector\n",
 922                                ahw->num_msix);
 923                return err;
 924        }
 925
 926        return 0;
 927}
 928
 929void qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
 930{
 931        if (adapter->flags & QLCNIC_MSIX_ENABLED)
 932                pci_disable_msix(adapter->pdev);
 933        if (adapter->flags & QLCNIC_MSI_ENABLED)
 934                pci_disable_msi(adapter->pdev);
 935
 936        kfree(adapter->msix_entries);
 937        adapter->msix_entries = NULL;
 938
 939        if (adapter->ahw->intr_tbl) {
 940                vfree(adapter->ahw->intr_tbl);
 941                adapter->ahw->intr_tbl = NULL;
 942        }
 943}
 944
 945static void qlcnic_cleanup_pci_map(struct qlcnic_hardware_context *ahw)
 946{
 947        if (ahw->pci_base0 != NULL)
 948                iounmap(ahw->pci_base0);
 949}
 950
 951static int qlcnic_get_act_pci_func(struct qlcnic_adapter *adapter)
 952{
 953        struct qlcnic_hardware_context *ahw = adapter->ahw;
 954        struct qlcnic_pci_info *pci_info;
 955        int ret;
 956
 957        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
 958                switch (ahw->port_type) {
 959                case QLCNIC_GBE:
 960                        ahw->total_nic_func = QLCNIC_NIU_MAX_GBE_PORTS;
 961                        break;
 962                case QLCNIC_XGBE:
 963                        ahw->total_nic_func = QLCNIC_NIU_MAX_XG_PORTS;
 964                        break;
 965                }
 966                return 0;
 967        }
 968
 969        if (ahw->op_mode == QLCNIC_MGMT_FUNC)
 970                return 0;
 971
 972        pci_info = kcalloc(ahw->max_vnic_func, sizeof(*pci_info), GFP_KERNEL);
 973        if (!pci_info)
 974                return -ENOMEM;
 975
 976        ret = qlcnic_get_pci_info(adapter, pci_info);
 977        kfree(pci_info);
 978        return ret;
 979}
 980
 981static bool qlcnic_port_eswitch_cfg_capability(struct qlcnic_adapter *adapter)
 982{
 983        bool ret = false;
 984
 985        if (qlcnic_84xx_check(adapter)) {
 986                ret = true;
 987        } else if (qlcnic_83xx_check(adapter)) {
 988                if (adapter->ahw->extra_capability[0] &
 989                    QLCNIC_FW_CAPABILITY_2_PER_PORT_ESWITCH_CFG)
 990                        ret = true;
 991                else
 992                        ret = false;
 993        }
 994
 995        return ret;
 996}
 997
 998int qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
 999{
1000        struct qlcnic_hardware_context *ahw = adapter->ahw;
1001        struct qlcnic_pci_info *pci_info;
1002        int i, id = 0, ret = 0, j = 0;
1003        u16 act_pci_func;
1004        u8 pfn;
1005
1006        pci_info = kcalloc(ahw->max_vnic_func, sizeof(*pci_info), GFP_KERNEL);
1007        if (!pci_info)
1008                return -ENOMEM;
1009
1010        ret = qlcnic_get_pci_info(adapter, pci_info);
1011        if (ret)
1012                goto err_pci_info;
1013
1014        act_pci_func = ahw->total_nic_func;
1015
1016        adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
1017                                 act_pci_func, GFP_KERNEL);
1018        if (!adapter->npars) {
1019                ret = -ENOMEM;
1020                goto err_pci_info;
1021        }
1022
1023        adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
1024                                QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
1025        if (!adapter->eswitch) {
1026                ret = -ENOMEM;
1027                goto err_npars;
1028        }
1029
1030        for (i = 0; i < ahw->max_vnic_func; i++) {
1031                pfn = pci_info[i].id;
1032
1033                if (pfn >= ahw->max_vnic_func) {
1034                        ret = QL_STATUS_INVALID_PARAM;
1035                        dev_err(&adapter->pdev->dev, "%s: Invalid function 0x%x, max 0x%x\n",
1036                                __func__, pfn, ahw->max_vnic_func);
1037                        goto err_eswitch;
1038                }
1039
1040                if (!pci_info[i].active ||
1041                    (pci_info[i].type != QLCNIC_TYPE_NIC))
1042                        continue;
1043
1044                if (qlcnic_port_eswitch_cfg_capability(adapter)) {
1045                        if (!qlcnic_83xx_set_port_eswitch_status(adapter, pfn,
1046                                                                 &id))
1047                                adapter->npars[j].eswitch_status = true;
1048                        else
1049                                continue;
1050                } else {
1051                        adapter->npars[j].eswitch_status = true;
1052                }
1053
1054                adapter->npars[j].pci_func = pfn;
1055                adapter->npars[j].active = (u8)pci_info[i].active;
1056                adapter->npars[j].type = (u8)pci_info[i].type;
1057                adapter->npars[j].phy_port = (u8)pci_info[i].default_port;
1058                adapter->npars[j].min_bw = pci_info[i].tx_min_bw;
1059                adapter->npars[j].max_bw = pci_info[i].tx_max_bw;
1060
1061                memcpy(&adapter->npars[j].mac, &pci_info[i].mac, ETH_ALEN);
1062                j++;
1063        }
1064
1065        /* Update eSwitch status for adapters without per port eSwitch
1066         * configuration capability
1067         */
1068        if (!qlcnic_port_eswitch_cfg_capability(adapter)) {
1069                for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
1070                        adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
1071        }
1072
1073        kfree(pci_info);
1074        return 0;
1075
1076err_eswitch:
1077        kfree(adapter->eswitch);
1078        adapter->eswitch = NULL;
1079err_npars:
1080        kfree(adapter->npars);
1081        adapter->npars = NULL;
1082err_pci_info:
1083        kfree(pci_info);
1084
1085        return ret;
1086}
1087
1088static int
1089qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
1090{
1091        u8 id;
1092        int ret;
1093        u32 data = QLCNIC_MGMT_FUNC;
1094        struct qlcnic_hardware_context *ahw = adapter->ahw;
1095
1096        ret = qlcnic_api_lock(adapter);
1097        if (ret)
1098                goto err_lock;
1099
1100        id = ahw->pci_func;
1101        data = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1102        data = (data & ~QLC_DEV_SET_DRV(0xf, id)) |
1103               QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC, id);
1104        QLC_SHARED_REG_WR32(adapter, QLCNIC_DRV_OP_MODE, data);
1105        qlcnic_api_unlock(adapter);
1106err_lock:
1107        return ret;
1108}
1109
1110static void qlcnic_check_vf(struct qlcnic_adapter *adapter,
1111                            const struct pci_device_id *ent)
1112{
1113        u32 op_mode, priv_level;
1114
1115        /* Determine FW API version */
1116        adapter->ahw->fw_hal_version = QLC_SHARED_REG_RD32(adapter,
1117                                                           QLCNIC_FW_API);
1118
1119        /* Find PCI function number */
1120        qlcnic_get_func_no(adapter);
1121
1122        /* Determine function privilege level */
1123        op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1124        if (op_mode == QLC_DEV_DRV_DEFAULT)
1125                priv_level = QLCNIC_MGMT_FUNC;
1126        else
1127                priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1128
1129        if (priv_level == QLCNIC_NON_PRIV_FUNC) {
1130                adapter->ahw->op_mode = QLCNIC_NON_PRIV_FUNC;
1131                dev_info(&adapter->pdev->dev,
1132                        "HAL Version: %d Non Privileged function\n",
1133                         adapter->ahw->fw_hal_version);
1134                adapter->nic_ops = &qlcnic_vf_ops;
1135        } else
1136                adapter->nic_ops = &qlcnic_ops;
1137}
1138
1139#define QLCNIC_82XX_BAR0_LENGTH 0x00200000UL
1140#define QLCNIC_83XX_BAR0_LENGTH 0x4000
1141static void qlcnic_get_bar_length(u32 dev_id, ulong *bar)
1142{
1143        switch (dev_id) {
1144        case PCI_DEVICE_ID_QLOGIC_QLE824X:
1145                *bar = QLCNIC_82XX_BAR0_LENGTH;
1146                break;
1147        case PCI_DEVICE_ID_QLOGIC_QLE834X:
1148        case PCI_DEVICE_ID_QLOGIC_QLE8830:
1149        case PCI_DEVICE_ID_QLOGIC_QLE844X:
1150        case PCI_DEVICE_ID_QLOGIC_VF_QLE834X:
1151        case PCI_DEVICE_ID_QLOGIC_VF_QLE844X:
1152                *bar = QLCNIC_83XX_BAR0_LENGTH;
1153                break;
1154        default:
1155                *bar = 0;
1156        }
1157}
1158
1159static int qlcnic_setup_pci_map(struct pci_dev *pdev,
1160                                struct qlcnic_hardware_context *ahw)
1161{
1162        u32 offset;
1163        void __iomem *mem_ptr0 = NULL;
1164        unsigned long mem_len, pci_len0 = 0, bar0_len;
1165
1166        /* remap phys address */
1167        mem_len = pci_resource_len(pdev, 0);
1168
1169        qlcnic_get_bar_length(pdev->device, &bar0_len);
1170        if (mem_len >= bar0_len) {
1171
1172                mem_ptr0 = pci_ioremap_bar(pdev, 0);
1173                if (mem_ptr0 == NULL) {
1174                        dev_err(&pdev->dev, "failed to map PCI bar 0\n");
1175                        return -EIO;
1176                }
1177                pci_len0 = mem_len;
1178        } else {
1179                return -EIO;
1180        }
1181
1182        dev_info(&pdev->dev, "%dKB memory map\n", (int)(mem_len >> 10));
1183
1184        ahw->pci_base0 = mem_ptr0;
1185        ahw->pci_len0 = pci_len0;
1186        offset = QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(ahw->pci_func));
1187        qlcnic_get_ioaddr(ahw, offset);
1188
1189        return 0;
1190}
1191
1192static bool qlcnic_validate_subsystem_id(struct qlcnic_adapter *adapter,
1193                                         int index)
1194{
1195        struct pci_dev *pdev = adapter->pdev;
1196        unsigned short subsystem_vendor;
1197        bool ret = true;
1198
1199        subsystem_vendor = pdev->subsystem_vendor;
1200
1201        if (pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X ||
1202            pdev->device == PCI_DEVICE_ID_QLOGIC_QLE834X) {
1203                if (qlcnic_boards[index].sub_vendor == subsystem_vendor &&
1204                    qlcnic_boards[index].sub_device == pdev->subsystem_device)
1205                        ret = true;
1206                else
1207                        ret = false;
1208        }
1209
1210        return ret;
1211}
1212
1213static void qlcnic_get_board_name(struct qlcnic_adapter *adapter, char *name)
1214{
1215        struct pci_dev *pdev = adapter->pdev;
1216        int i, found = 0;
1217
1218        for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
1219                if (qlcnic_boards[i].vendor == pdev->vendor &&
1220                    qlcnic_boards[i].device == pdev->device &&
1221                    qlcnic_validate_subsystem_id(adapter, i)) {
1222                        found = 1;
1223                        break;
1224                }
1225        }
1226
1227        if (!found)
1228                sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
1229        else
1230                sprintf(name, "%pM: %s" , adapter->mac_addr,
1231                        qlcnic_boards[i].short_name);
1232}
1233
1234static void
1235qlcnic_check_options(struct qlcnic_adapter *adapter)
1236{
1237        int err;
1238        u32 fw_major, fw_minor, fw_build, prev_fw_version;
1239        struct pci_dev *pdev = adapter->pdev;
1240        struct qlcnic_hardware_context *ahw = adapter->ahw;
1241        struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
1242
1243        prev_fw_version = adapter->fw_version;
1244
1245        fw_major = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MAJOR);
1246        fw_minor = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MINOR);
1247        fw_build = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_SUB);
1248
1249        adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
1250
1251        err = qlcnic_get_board_info(adapter);
1252        if (err) {
1253                dev_err(&pdev->dev, "Error getting board config info.\n");
1254                return;
1255        }
1256        if (ahw->op_mode != QLCNIC_NON_PRIV_FUNC) {
1257                if (fw_dump->tmpl_hdr == NULL ||
1258                                adapter->fw_version > prev_fw_version) {
1259                        vfree(fw_dump->tmpl_hdr);
1260                        if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
1261                                dev_info(&pdev->dev,
1262                                        "Supports FW dump capability\n");
1263                }
1264        }
1265
1266        dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d\n",
1267                 QLCNIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build);
1268
1269        if (adapter->ahw->port_type == QLCNIC_XGBE) {
1270                if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
1271                        adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
1272                        adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
1273                } else {
1274                        adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
1275                        adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
1276                }
1277
1278                adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
1279                adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
1280
1281        } else if (adapter->ahw->port_type == QLCNIC_GBE) {
1282                adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
1283                adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
1284                adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
1285                adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
1286        }
1287
1288        adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
1289
1290        adapter->num_txd = MAX_CMD_DESCRIPTORS;
1291
1292        adapter->max_rds_rings = MAX_RDS_RINGS;
1293}
1294
1295static int
1296qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
1297{
1298        struct qlcnic_info nic_info;
1299        int err = 0;
1300
1301        memset(&nic_info, 0, sizeof(struct qlcnic_info));
1302        err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
1303        if (err)
1304                return err;
1305
1306        adapter->ahw->physical_port = (u8)nic_info.phys_port;
1307        adapter->ahw->switch_mode = nic_info.switch_mode;
1308        adapter->ahw->max_tx_ques = nic_info.max_tx_ques;
1309        adapter->ahw->max_rx_ques = nic_info.max_rx_ques;
1310        adapter->ahw->capabilities = nic_info.capabilities;
1311
1312        if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
1313                u32 temp;
1314                temp = QLCRD32(adapter, CRB_FW_CAPABILITIES_2, &err);
1315                if (err == -EIO)
1316                        return err;
1317                adapter->ahw->extra_capability[0] = temp;
1318        } else {
1319                adapter->ahw->extra_capability[0] = 0;
1320        }
1321
1322        adapter->ahw->max_mac_filters = nic_info.max_mac_filters;
1323        adapter->ahw->max_mtu = nic_info.max_mtu;
1324
1325        if (adapter->ahw->capabilities & BIT_6) {
1326                adapter->flags |= QLCNIC_ESWITCH_ENABLED;
1327                adapter->ahw->nic_mode = QLCNIC_VNIC_MODE;
1328                adapter->max_tx_rings = QLCNIC_MAX_HW_VNIC_TX_RINGS;
1329                adapter->max_sds_rings = QLCNIC_MAX_VNIC_SDS_RINGS;
1330
1331                dev_info(&adapter->pdev->dev, "vNIC mode enabled.\n");
1332        } else {
1333                adapter->ahw->nic_mode = QLCNIC_DEFAULT_MODE;
1334                adapter->max_tx_rings = QLCNIC_MAX_HW_TX_RINGS;
1335                adapter->max_sds_rings = QLCNIC_MAX_SDS_RINGS;
1336                adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
1337        }
1338
1339        return err;
1340}
1341
1342void qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
1343                            struct qlcnic_esw_func_cfg *esw_cfg)
1344{
1345        if (esw_cfg->discard_tagged)
1346                adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
1347        else
1348                adapter->flags |= QLCNIC_TAGGING_ENABLED;
1349
1350        if (esw_cfg->vlan_id) {
1351                adapter->rx_pvid = esw_cfg->vlan_id;
1352                adapter->tx_pvid = esw_cfg->vlan_id;
1353        } else {
1354                adapter->rx_pvid = 0;
1355                adapter->tx_pvid = 0;
1356        }
1357}
1358
1359static int
1360qlcnic_vlan_rx_add(struct net_device *netdev, __be16 proto, u16 vid)
1361{
1362        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1363        int err;
1364
1365        if (qlcnic_sriov_vf_check(adapter)) {
1366                err = qlcnic_sriov_cfg_vf_guest_vlan(adapter, vid, 1);
1367                if (err) {
1368                        netdev_err(netdev,
1369                                   "Cannot add VLAN filter for VLAN id %d, err=%d",
1370                                   vid, err);
1371                        return err;
1372                }
1373        }
1374
1375        set_bit(vid, adapter->vlans);
1376        return 0;
1377}
1378
1379static int
1380qlcnic_vlan_rx_del(struct net_device *netdev, __be16 proto, u16 vid)
1381{
1382        struct qlcnic_adapter *adapter = netdev_priv(netdev);
1383        int err;
1384
1385        if (qlcnic_sriov_vf_check(adapter)) {
1386                err = qlcnic_sriov_cfg_vf_guest_vlan(adapter, vid, 0);
1387                if (err) {
1388                        netdev_err(netdev,
1389                                   "Cannot delete VLAN filter for VLAN id %d, err=%d",
1390                                   vid, err);
1391                        return err;
1392                }
1393        }
1394
1395        qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
1396        clear_bit(vid, adapter->vlans);
1397        return 0;
1398}
1399
1400void qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
1401                                      struct qlcnic_esw_func_cfg *esw_cfg)
1402{
1403        adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
1404                                QLCNIC_PROMISC_DISABLED);
1405
1406        if (esw_cfg->mac_anti_spoof)
1407                adapter->flags |= QLCNIC_MACSPOOF;
1408
1409        if (!esw_cfg->mac_override)
1410                adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
1411
1412        if (!esw_cfg->promisc_mode)
1413                adapter->flags |= QLCNIC_PROMISC_DISABLED;
1414}
1415
1416int qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
1417{
1418        struct qlcnic_esw_func_cfg esw_cfg;
1419
1420        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1421                return 0;
1422
1423        esw_cfg.pci_func = adapter->ahw->pci_func;
1424        if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
1425                        return -EIO;
1426        qlcnic_set_vlan_config(adapter, &esw_cfg);
1427        qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
1428        qlcnic_set_netdev_features(adapter, &esw_cfg);
1429
1430        return 0;
1431}
1432
1433void qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
1434                                struct qlcnic_esw_func_cfg *esw_cfg)
1435{
1436        struct net_device *netdev = adapter->netdev;
1437
1438        if (qlcnic_83xx_check(adapter))
1439                return;
1440
1441        adapter->offload_flags = esw_cfg->offload_flags;
1442        adapter->flags |= QLCNIC_APP_CHANGED_FLAGS;
1443        netdev_update_features(netdev);
1444        adapter->flags &= ~QLCNIC_APP_CHANGED_FLAGS;
1445}
1446
1447static int
1448qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
1449{
1450        u32 op_mode, priv_level;
1451        int err = 0;
1452
1453        err = qlcnic_initialize_nic(adapter);
1454        if (err)
1455                return err;
1456
1457        if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
1458                return 0;
1459
1460        op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1461        priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1462
1463        if (op_mode == QLC_DEV_DRV_DEFAULT)
1464                priv_level = QLCNIC_MGMT_FUNC;
1465        else
1466                priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1467
1468        if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
1469                if (priv_level == QLCNIC_MGMT_FUNC) {
1470                        adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
1471                        err = qlcnic_init_pci_info(adapter);
1472                        if (err)
1473                                return err;
1474                        /* Set privilege level for other functions */
1475                        qlcnic_set_function_modes(adapter);
1476                        dev_info(&adapter->pdev->dev,
1477                                "HAL Version: %d, Management function\n",
1478                                 adapter->ahw->fw_hal_version);
1479                } else if (priv_level == QLCNIC_PRIV_FUNC) {
1480                        adapter->ahw->op_mode = QLCNIC_PRIV_FUNC;
1481                        dev_info(&adapter->pdev->dev,
1482                                "HAL Version: %d, Privileged function\n",
1483                                 adapter->ahw->fw_hal_version);
1484                }
1485        } else {
1486                adapter->ahw->nic_mode = QLCNIC_DEFAULT_MODE;
1487        }
1488
1489        adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
1490
1491        return err;
1492}
1493
1494int qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
1495{
1496        struct qlcnic_esw_func_cfg esw_cfg;
1497        struct qlcnic_npar_info *npar;
1498        u8 i;
1499
1500        if (adapter->need_fw_reset)
1501                return 0;
1502
1503        for (i = 0; i < adapter->ahw->total_nic_func; i++) {
1504                if (!adapter->npars[i].eswitch_status)
1505                        continue;
1506
1507                memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
1508                esw_cfg.pci_func = adapter->npars[i].pci_func;
1509                esw_cfg.mac_override = BIT_0;
1510                esw_cfg.promisc_mode = BIT_0;
1511                if (qlcnic_82xx_check(adapter)) {
1512                        esw_cfg.offload_flags = BIT_0;
1513                        if (QLCNIC_IS_TSO_CAPABLE(adapter))
1514                                esw_cfg.offload_flags |= (BIT_1 | BIT_2);
1515                }
1516                if (qlcnic_config_switch_port(adapter, &esw_cfg))
1517                        return -EIO;
1518                npar = &adapter->npars[i];
1519                npar->pvid = esw_cfg.vlan_id;
1520                npar->mac_override = esw_cfg.mac_override;
1521                npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
1522                npar->discard_tagged = esw_cfg.discard_tagged;
1523                npar->promisc_mode = esw_cfg.promisc_mode;
1524                npar->offload_flags = esw_cfg.offload_flags;
1525        }
1526
1527        return 0;
1528}
1529
1530
1531static int
1532qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
1533                        struct qlcnic_npar_info *npar, int pci_func)
1534{
1535        struct qlcnic_esw_func_cfg esw_cfg;
1536        esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
1537        esw_cfg.pci_func = pci_func;
1538        esw_cfg.vlan_id = npar->pvid;
1539        esw_cfg.mac_override = npar->mac_override;
1540        esw_cfg.discard_tagged = npar->discard_tagged;
1541        esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
1542        esw_cfg.offload_flags = npar->offload_flags;
1543        esw_cfg.promisc_mode = npar->promisc_mode;
1544        if (qlcnic_config_switch_port(adapter, &esw_cfg))
1545                return -EIO;
1546
1547        esw_cfg.op_mode = QLCNIC_ADD_VLAN;
1548        if (qlcnic_config_switch_port(adapter, &esw_cfg))
1549                return -EIO;
1550
1551        return 0;
1552}
1553
1554int qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
1555{
1556        int i, err;
1557        struct qlcnic_npar_info *npar;
1558        struct qlcnic_info nic_info;
1559        u8 pci_func;
1560
1561        if (qlcnic_82xx_check(adapter))
1562                if (!adapter->need_fw_reset)
1563                        return 0;
1564
1565        /* Set the NPAR config data after FW reset */
1566        for (i = 0; i < adapter->ahw->total_nic_func; i++) {
1567                npar = &adapter->npars[i];
1568                pci_func = npar->pci_func;
1569                if (!adapter->npars[i].eswitch_status)
1570                        continue;
1571
1572                memset(&nic_info, 0, sizeof(struct qlcnic_info));
1573                err = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
1574                if (err)
1575                        return err;
1576                nic_info.min_tx_bw = npar->min_bw;
1577                nic_info.max_tx_bw = npar->max_bw;
1578                err = qlcnic_set_nic_info(adapter, &nic_info);
1579                if (err)
1580                        return err;
1581
1582                if (npar->enable_pm) {
1583                        err = qlcnic_config_port_mirroring(adapter,
1584                                                           npar->dest_npar, 1,
1585                                                           pci_func);
1586                        if (err)
1587                                return err;
1588                }
1589                err = qlcnic_reset_eswitch_config(adapter, npar, pci_func);
1590                if (err)
1591                        return err;
1592        }
1593        return 0;
1594}
1595
1596static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
1597{
1598        u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
1599        u32 npar_state;
1600
1601        if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
1602                return 0;
1603
1604        npar_state = QLC_SHARED_REG_RD32(adapter,
1605                                         QLCNIC_CRB_DEV_NPAR_STATE);
1606        while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
1607                msleep(1000);
1608                npar_state = QLC_SHARED_REG_RD32(adapter,
1609                                                 QLCNIC_CRB_DEV_NPAR_STATE);
1610        }
1611        if (!npar_opt_timeo) {
1612                dev_err(&adapter->pdev->dev,
1613                        "Waiting for NPAR state to operational timeout\n");
1614                return -EIO;
1615        }
1616        return 0;
1617}
1618
1619static int
1620qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
1621{
1622        int err;
1623
1624        if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
1625            adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1626                return 0;
1627
1628        err = qlcnic_set_default_offload_settings(adapter);
1629        if (err)
1630                return err;
1631
1632        err = qlcnic_reset_npar_config(adapter);
1633        if (err)
1634                return err;
1635
1636        qlcnic_dev_set_npar_ready(adapter);
1637
1638        return err;
1639}
1640
1641static int qlcnic_82xx_start_firmware(struct qlcnic_adapter *adapter)
1642{
1643        int err;
1644
1645        err = qlcnic_can_start_firmware(adapter);
1646        if (err < 0)
1647                return err;
1648        else if (!err)
1649                goto check_fw_status;
1650
1651        if (qlcnic_load_fw_file)
1652                qlcnic_request_firmware(adapter);
1653        else {
1654                err = qlcnic_check_flash_fw_ver(adapter);
1655                if (err)
1656                        goto err_out;
1657
1658                adapter->ahw->fw_type = QLCNIC_FLASH_ROMIMAGE;
1659        }
1660
1661        err = qlcnic_need_fw_reset(adapter);
1662        if (err == 0)
1663                goto check_fw_status;
1664
1665        err = qlcnic_pinit_from_rom(adapter);
1666        if (err)
1667                goto err_out;
1668
1669        err = qlcnic_load_firmware(adapter);
1670        if (err)
1671                goto err_out;
1672
1673        qlcnic_release_firmware(adapter);
1674        QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1675
1676check_fw_status:
1677        err = qlcnic_check_fw_status(adapter);
1678        if (err)
1679                goto err_out;
1680
1681        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1682        qlcnic_idc_debug_info(adapter, 1);
1683        err = qlcnic_check_eswitch_mode(adapter);
1684        if (err) {
1685                dev_err(&adapter->pdev->dev,
1686                        "Memory allocation failed for eswitch\n");
1687                goto err_out;
1688        }
1689        err = qlcnic_set_mgmt_operations(adapter);
1690        if (err)
1691                goto err_out;
1692
1693        qlcnic_check_options(adapter);
1694        adapter->need_fw_reset = 0;
1695
1696        qlcnic_release_firmware(adapter);
1697        return 0;
1698
1699err_out:
1700        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1701        dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1702
1703        qlcnic_release_firmware(adapter);
1704        return err;
1705}
1706
1707static int
1708qlcnic_request_irq(struct qlcnic_adapter *adapter)
1709{
1710        irq_handler_t handler;
1711        struct qlcnic_host_sds_ring *sds_ring;
1712        struct qlcnic_host_tx_ring *tx_ring;
1713        int err, ring, num_sds_rings;
1714
1715        unsigned long flags = 0;
1716        struct net_device *netdev = adapter->netdev;
1717        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1718
1719        if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
1720                if (qlcnic_82xx_check(adapter))
1721                        handler = qlcnic_tmp_intr;
1722                else
1723                        handler = qlcnic_83xx_tmp_intr;
1724                if (!QLCNIC_IS_MSI_FAMILY(adapter))
1725                        flags |= IRQF_SHARED;
1726
1727        } else {
1728                if (adapter->flags & QLCNIC_MSIX_ENABLED)
1729                        handler = qlcnic_msix_intr;
1730                else if (adapter->flags & QLCNIC_MSI_ENABLED)
1731                        handler = qlcnic_msi_intr;
1732                else {
1733                        flags |= IRQF_SHARED;
1734                        if (qlcnic_82xx_check(adapter))
1735                                handler = qlcnic_intr;
1736                        else
1737                                handler = qlcnic_83xx_intr;
1738                }
1739        }
1740        adapter->irq = netdev->irq;
1741
1742        if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1743                if (qlcnic_82xx_check(adapter) ||
1744                    (qlcnic_83xx_check(adapter) &&
1745                     (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1746                        num_sds_rings = adapter->drv_sds_rings;
1747                        for (ring = 0; ring < num_sds_rings; ring++) {
1748                                sds_ring = &recv_ctx->sds_rings[ring];
1749                                if (qlcnic_82xx_check(adapter) &&
1750                                    !qlcnic_check_multi_tx(adapter) &&
1751                                    (ring == (num_sds_rings - 1))) {
1752                                        if (!(adapter->flags &
1753                                              QLCNIC_MSIX_ENABLED))
1754                                                snprintf(sds_ring->name,
1755                                                         sizeof(sds_ring->name),
1756                                                         "qlcnic");
1757                                        else
1758                                                snprintf(sds_ring->name,
1759                                                         sizeof(sds_ring->name),
1760                                                         "%s-tx-0-rx-%d",
1761                                                         netdev->name, ring);
1762                                } else {
1763                                        snprintf(sds_ring->name,
1764                                                 sizeof(sds_ring->name),
1765                                                 "%s-rx-%d",
1766                                                 netdev->name, ring);
1767                                }
1768                                err = request_irq(sds_ring->irq, handler, flags,
1769                                                  sds_ring->name, sds_ring);
1770                                if (err)
1771                                        return err;
1772                        }
1773                }
1774                if ((qlcnic_82xx_check(adapter) &&
1775                     qlcnic_check_multi_tx(adapter)) ||
1776                    (qlcnic_83xx_check(adapter) &&
1777                     (adapter->flags & QLCNIC_MSIX_ENABLED) &&
1778                     !(adapter->flags & QLCNIC_TX_INTR_SHARED))) {
1779                        handler = qlcnic_msix_tx_intr;
1780                        for (ring = 0; ring < adapter->drv_tx_rings;
1781                             ring++) {
1782                                tx_ring = &adapter->tx_ring[ring];
1783                                snprintf(tx_ring->name, sizeof(tx_ring->name),
1784                                         "%s-tx-%d", netdev->name, ring);
1785                                err = request_irq(tx_ring->irq, handler, flags,
1786                                                  tx_ring->name, tx_ring);
1787                                if (err)
1788                                        return err;
1789                        }
1790                }
1791        }
1792        return 0;
1793}
1794
1795static void
1796qlcnic_free_irq(struct qlcnic_adapter *adapter)
1797{
1798        int ring;
1799        struct qlcnic_host_sds_ring *sds_ring;
1800        struct qlcnic_host_tx_ring *tx_ring;
1801
1802        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1803
1804        if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1805                if (qlcnic_82xx_check(adapter) ||
1806                    (qlcnic_83xx_check(adapter) &&
1807                     (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1808                        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1809                                sds_ring = &recv_ctx->sds_rings[ring];
1810                                free_irq(sds_ring->irq, sds_ring);
1811                        }
1812                }
1813                if ((qlcnic_83xx_check(adapter) &&
1814                     !(adapter->flags & QLCNIC_TX_INTR_SHARED)) ||
1815                    (qlcnic_82xx_check(adapter) &&
1816                     qlcnic_check_multi_tx(adapter))) {
1817                        for (ring = 0; ring < adapter->drv_tx_rings;
1818                             ring++) {
1819                                tx_ring = &adapter->tx_ring[ring];
1820                                if (tx_ring->irq)
1821                                        free_irq(tx_ring->irq, tx_ring);
1822                        }
1823                }
1824        }
1825}
1826
1827static void qlcnic_get_lro_mss_capability(struct qlcnic_adapter *adapter)
1828{
1829        u32 capab = 0;
1830
1831        if (qlcnic_82xx_check(adapter)) {
1832                if (adapter->ahw->extra_capability[0] &
1833                    QLCNIC_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1834                        adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1835        } else {
1836                capab = adapter->ahw->capabilities;
1837                if (QLC_83XX_GET_FW_LRO_MSS_CAPABILITY(capab))
1838                        adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1839        }
1840}
1841
1842static int qlcnic_config_def_intr_coalesce(struct qlcnic_adapter *adapter)
1843{
1844        struct qlcnic_hardware_context *ahw = adapter->ahw;
1845        int err;
1846
1847        /* Initialize interrupt coalesce parameters */
1848        ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1849
1850        if (qlcnic_83xx_check(adapter)) {
1851                ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX_TX;
1852                ahw->coal.tx_time_us = QLCNIC_DEF_INTR_COALESCE_TX_TIME_US;
1853                ahw->coal.tx_packets = QLCNIC_DEF_INTR_COALESCE_TX_PACKETS;
1854                ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
1855                ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
1856
1857                err = qlcnic_83xx_set_rx_tx_intr_coal(adapter);
1858        } else {
1859                ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX;
1860                ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
1861                ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
1862
1863                err = qlcnic_82xx_set_rx_coalesce(adapter);
1864        }
1865
1866        return err;
1867}
1868
1869int __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1870{
1871        int ring;
1872        struct qlcnic_host_rds_ring *rds_ring;
1873
1874        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1875                return -EIO;
1876
1877        if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1878                return 0;
1879
1880        if (qlcnic_set_eswitch_port_config(adapter))
1881                return -EIO;
1882
1883        qlcnic_get_lro_mss_capability(adapter);
1884
1885        if (qlcnic_fw_create_ctx(adapter))
1886                return -EIO;
1887
1888        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1889                rds_ring = &adapter->recv_ctx->rds_rings[ring];
1890                qlcnic_post_rx_buffers(adapter, rds_ring, ring);
1891        }
1892
1893        qlcnic_set_multi(netdev);
1894        qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1895
1896        adapter->ahw->linkup = 0;
1897
1898        if (adapter->drv_sds_rings > 1)
1899                qlcnic_config_rss(adapter, 1);
1900
1901        qlcnic_config_def_intr_coalesce(adapter);
1902
1903        if (netdev->features & NETIF_F_LRO)
1904                qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1905
1906        set_bit(__QLCNIC_DEV_UP, &adapter->state);
1907        qlcnic_napi_enable(adapter);
1908
1909        qlcnic_linkevent_request(adapter, 1);
1910
1911        adapter->ahw->reset_context = 0;
1912        netif_tx_start_all_queues(netdev);
1913        return 0;
1914}
1915
1916int qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1917{
1918        int err = 0;
1919
1920        rtnl_lock();
1921        if (netif_running(netdev))
1922                err = __qlcnic_up(adapter, netdev);
1923        rtnl_unlock();
1924
1925        return err;
1926}
1927
1928void __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1929{
1930        int ring;
1931
1932        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1933                return;
1934
1935        if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1936                return;
1937
1938        smp_mb();
1939        netif_carrier_off(netdev);
1940        adapter->ahw->linkup = 0;
1941        netif_tx_disable(netdev);
1942
1943        qlcnic_free_mac_list(adapter);
1944
1945        if (adapter->fhash.fnum)
1946                qlcnic_delete_lb_filters(adapter);
1947
1948        qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1949        if (qlcnic_sriov_vf_check(adapter))
1950                qlcnic_sriov_cleanup_async_list(&adapter->ahw->sriov->bc);
1951
1952        qlcnic_napi_disable(adapter);
1953
1954        qlcnic_fw_destroy_ctx(adapter);
1955        adapter->flags &= ~QLCNIC_FW_LRO_MSS_CAP;
1956
1957        qlcnic_reset_rx_buffers_list(adapter);
1958
1959        for (ring = 0; ring < adapter->drv_tx_rings; ring++)
1960                qlcnic_release_tx_buffers(adapter, &adapter->tx_ring[ring]);
1961}
1962
1963/* Usage: During suspend and firmware recovery module */
1964
1965void qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1966{
1967        rtnl_lock();
1968        if (netif_running(netdev))
1969                __qlcnic_down(adapter, netdev);
1970        rtnl_unlock();
1971
1972}
1973
1974int
1975qlcnic_attach(struct qlcnic_adapter *adapter)
1976{
1977        struct net_device *netdev = adapter->netdev;
1978        struct pci_dev *pdev = adapter->pdev;
1979        int err;
1980
1981        if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1982                return 0;
1983
1984        err = qlcnic_napi_add(adapter, netdev);
1985        if (err)
1986                return err;
1987
1988        err = qlcnic_alloc_sw_resources(adapter);
1989        if (err) {
1990                dev_err(&pdev->dev, "Error in setting sw resources\n");
1991                goto err_out_napi_del;
1992        }
1993
1994        err = qlcnic_alloc_hw_resources(adapter);
1995        if (err) {
1996                dev_err(&pdev->dev, "Error in setting hw resources\n");
1997                goto err_out_free_sw;
1998        }
1999
2000        err = qlcnic_request_irq(adapter);
2001        if (err) {
2002                dev_err(&pdev->dev, "failed to setup interrupt\n");
2003                goto err_out_free_hw;
2004        }
2005
2006        qlcnic_create_sysfs_entries(adapter);
2007
2008#ifdef CONFIG_QLCNIC_VXLAN
2009        if (qlcnic_encap_rx_offload(adapter))
2010                vxlan_get_rx_port(netdev);
2011#endif
2012
2013        adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
2014        return 0;
2015
2016err_out_free_hw:
2017        qlcnic_free_hw_resources(adapter);
2018err_out_free_sw:
2019        qlcnic_free_sw_resources(adapter);
2020err_out_napi_del:
2021        qlcnic_napi_del(adapter);
2022        return err;
2023}
2024
2025void qlcnic_detach(struct qlcnic_adapter *adapter)
2026{
2027        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2028                return;
2029
2030        qlcnic_remove_sysfs_entries(adapter);
2031
2032        qlcnic_free_hw_resources(adapter);
2033        qlcnic_release_rx_buffers(adapter);
2034        qlcnic_free_irq(adapter);
2035        qlcnic_napi_del(adapter);
2036        qlcnic_free_sw_resources(adapter);
2037
2038        adapter->is_up = 0;
2039}
2040
2041void qlcnic_diag_free_res(struct net_device *netdev, int drv_sds_rings)
2042{
2043        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2044        struct qlcnic_host_sds_ring *sds_ring;
2045        int drv_tx_rings = adapter->drv_tx_rings;
2046        int ring;
2047
2048        clear_bit(__QLCNIC_DEV_UP, &adapter->state);
2049        if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
2050                for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2051                        sds_ring = &adapter->recv_ctx->sds_rings[ring];
2052                        qlcnic_disable_sds_intr(adapter, sds_ring);
2053                }
2054        }
2055
2056        qlcnic_fw_destroy_ctx(adapter);
2057
2058        qlcnic_detach(adapter);
2059
2060        adapter->ahw->diag_test = 0;
2061        adapter->drv_sds_rings = drv_sds_rings;
2062        adapter->drv_tx_rings = drv_tx_rings;
2063
2064        if (qlcnic_attach(adapter))
2065                goto out;
2066
2067        if (netif_running(netdev))
2068                __qlcnic_up(adapter, netdev);
2069out:
2070        netif_device_attach(netdev);
2071}
2072
2073static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
2074{
2075        struct qlcnic_hardware_context *ahw = adapter->ahw;
2076        int err = 0;
2077
2078        adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
2079                                GFP_KERNEL);
2080        if (!adapter->recv_ctx) {
2081                err = -ENOMEM;
2082                goto err_out;
2083        }
2084
2085        if (qlcnic_83xx_check(adapter)) {
2086                ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX_TX;
2087                ahw->coal.tx_time_us = QLCNIC_DEF_INTR_COALESCE_TX_TIME_US;
2088                ahw->coal.tx_packets = QLCNIC_DEF_INTR_COALESCE_TX_PACKETS;
2089                ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
2090                ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
2091        } else {
2092                ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX;
2093                ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
2094                ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
2095        }
2096
2097        /* clear stats */
2098        memset(&adapter->stats, 0, sizeof(adapter->stats));
2099err_out:
2100        return err;
2101}
2102
2103static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
2104{
2105        struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
2106
2107        kfree(adapter->recv_ctx);
2108        adapter->recv_ctx = NULL;
2109
2110        if (fw_dump->tmpl_hdr) {
2111                vfree(fw_dump->tmpl_hdr);
2112                fw_dump->tmpl_hdr = NULL;
2113        }
2114
2115        if (fw_dump->dma_buffer) {
2116                dma_free_coherent(&adapter->pdev->dev, QLC_PEX_DMA_READ_SIZE,
2117                                  fw_dump->dma_buffer, fw_dump->phys_addr);
2118                fw_dump->dma_buffer = NULL;
2119        }
2120
2121        kfree(adapter->ahw->reset.buff);
2122        adapter->ahw->fw_dump.tmpl_hdr = NULL;
2123}
2124
2125int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
2126{
2127        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2128        struct qlcnic_host_sds_ring *sds_ring;
2129        struct qlcnic_host_rds_ring *rds_ring;
2130        int ring;
2131        int ret;
2132
2133        netif_device_detach(netdev);
2134
2135        if (netif_running(netdev))
2136                __qlcnic_down(adapter, netdev);
2137
2138        qlcnic_detach(adapter);
2139
2140        adapter->drv_sds_rings = QLCNIC_SINGLE_RING;
2141        adapter->ahw->diag_test = test;
2142        adapter->ahw->linkup = 0;
2143
2144        ret = qlcnic_attach(adapter);
2145        if (ret) {
2146                netif_device_attach(netdev);
2147                return ret;
2148        }
2149
2150        ret = qlcnic_fw_create_ctx(adapter);
2151        if (ret) {
2152                qlcnic_detach(adapter);
2153                netif_device_attach(netdev);
2154                return ret;
2155        }
2156
2157        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
2158                rds_ring = &adapter->recv_ctx->rds_rings[ring];
2159                qlcnic_post_rx_buffers(adapter, rds_ring, ring);
2160        }
2161
2162        if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
2163                for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2164                        sds_ring = &adapter->recv_ctx->sds_rings[ring];
2165                        qlcnic_enable_sds_intr(adapter, sds_ring);
2166                }
2167        }
2168
2169        if (adapter->ahw->diag_test == QLCNIC_LOOPBACK_TEST) {
2170                adapter->ahw->loopback_state = 0;
2171                qlcnic_linkevent_request(adapter, 1);
2172        }
2173
2174        set_bit(__QLCNIC_DEV_UP, &adapter->state);
2175
2176        return 0;
2177}
2178
2179/* Reset context in hardware only */
2180static int
2181qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
2182{
2183        struct net_device *netdev = adapter->netdev;
2184
2185        if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2186                return -EBUSY;
2187
2188        netif_device_detach(netdev);
2189
2190        qlcnic_down(adapter, netdev);
2191
2192        qlcnic_up(adapter, netdev);
2193
2194        netif_device_attach(netdev);
2195
2196        clear_bit(__QLCNIC_RESETTING, &adapter->state);
2197        netdev_info(adapter->netdev, "%s: soft reset complete\n", __func__);
2198        return 0;
2199}
2200
2201int
2202qlcnic_reset_context(struct qlcnic_adapter *adapter)
2203{
2204        int err = 0;
2205        struct net_device *netdev = adapter->netdev;
2206
2207        if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2208                return -EBUSY;
2209
2210        if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
2211
2212                netif_device_detach(netdev);
2213
2214                if (netif_running(netdev))
2215                        __qlcnic_down(adapter, netdev);
2216
2217                qlcnic_detach(adapter);
2218
2219                if (netif_running(netdev)) {
2220                        err = qlcnic_attach(adapter);
2221                        if (!err) {
2222                                __qlcnic_up(adapter, netdev);
2223                                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
2224                        }
2225                }
2226
2227                netif_device_attach(netdev);
2228        }
2229
2230        clear_bit(__QLCNIC_RESETTING, &adapter->state);
2231        return err;
2232}
2233
2234static void qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter *adapter)
2235{
2236        struct qlcnic_hardware_context *ahw = adapter->ahw;
2237        u16 act_pci_fn = ahw->total_nic_func;
2238        u16 count;
2239
2240        ahw->max_mc_count = QLCNIC_MAX_MC_COUNT;
2241        if (act_pci_fn <= 2)
2242                count = (QLCNIC_MAX_UC_COUNT - QLCNIC_MAX_MC_COUNT) /
2243                         act_pci_fn;
2244        else
2245                count = (QLCNIC_LB_MAX_FILTERS - QLCNIC_MAX_MC_COUNT) /
2246                         act_pci_fn;
2247        ahw->max_uc_count = count;
2248}
2249
2250static int qlcnic_set_real_num_queues(struct qlcnic_adapter *adapter,
2251                                      u8 tx_queues, u8 rx_queues)
2252{
2253        struct net_device *netdev = adapter->netdev;
2254        int err = 0;
2255
2256        if (tx_queues) {
2257                err = netif_set_real_num_tx_queues(netdev, tx_queues);
2258                if (err) {
2259                        netdev_err(netdev, "failed to set %d Tx queues\n",
2260                                   tx_queues);
2261                        return err;
2262                }
2263        }
2264
2265        if (rx_queues) {
2266                err = netif_set_real_num_rx_queues(netdev, rx_queues);
2267                if (err)
2268                        netdev_err(netdev, "failed to set %d Rx queues\n",
2269                                   rx_queues);
2270        }
2271
2272        return err;
2273}
2274
2275int
2276qlcnic_setup_netdev(struct qlcnic_adapter *adapter, struct net_device *netdev,
2277                    int pci_using_dac)
2278{
2279        int err;
2280        struct pci_dev *pdev = adapter->pdev;
2281
2282        adapter->rx_csum = 1;
2283        adapter->ahw->mc_enabled = 0;
2284        qlcnic_set_mac_filter_count(adapter);
2285
2286        netdev->netdev_ops         = &qlcnic_netdev_ops;
2287        netdev->watchdog_timeo     = QLCNIC_WATCHDOG_TIMEOUTVALUE * HZ;
2288
2289        qlcnic_change_mtu(netdev, netdev->mtu);
2290
2291        netdev->ethtool_ops = (qlcnic_sriov_vf_check(adapter)) ?
2292                &qlcnic_sriov_vf_ethtool_ops : &qlcnic_ethtool_ops;
2293
2294        netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2295                             NETIF_F_IPV6_CSUM | NETIF_F_GRO |
2296                             NETIF_F_HW_VLAN_CTAG_RX);
2297        netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
2298                                  NETIF_F_IPV6_CSUM);
2299
2300        if (QLCNIC_IS_TSO_CAPABLE(adapter)) {
2301                netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
2302                netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
2303        }
2304
2305        if (pci_using_dac) {
2306                netdev->features |= NETIF_F_HIGHDMA;
2307                netdev->vlan_features |= NETIF_F_HIGHDMA;
2308        }
2309
2310        if (qlcnic_vlan_tx_check(adapter))
2311                netdev->features |= (NETIF_F_HW_VLAN_CTAG_TX);
2312
2313        if (qlcnic_sriov_vf_check(adapter))
2314                netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2315
2316        if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
2317                netdev->features |= NETIF_F_LRO;
2318
2319        if (qlcnic_encap_tx_offload(adapter)) {
2320                netdev->features |= NETIF_F_GSO_UDP_TUNNEL;
2321
2322                /* encapsulation Tx offload supported by Adapter */
2323                netdev->hw_enc_features = NETIF_F_IP_CSUM        |
2324                                          NETIF_F_GSO_UDP_TUNNEL |
2325                                          NETIF_F_TSO            |
2326                                          NETIF_F_TSO6;
2327        }
2328
2329        if (qlcnic_encap_rx_offload(adapter))
2330                netdev->hw_enc_features |= NETIF_F_RXCSUM;
2331
2332        netdev->hw_features = netdev->features;
2333        netdev->priv_flags |= IFF_UNICAST_FLT;
2334        netdev->irq = adapter->msix_entries[0].vector;
2335
2336        err = qlcnic_set_real_num_queues(adapter, adapter->drv_tx_rings,
2337                                         adapter->drv_sds_rings);
2338        if (err)
2339                return err;
2340
2341        qlcnic_dcb_init_dcbnl_ops(adapter->dcb);
2342
2343        err = register_netdev(netdev);
2344        if (err) {
2345                dev_err(&pdev->dev, "failed to register net device\n");
2346                return err;
2347        }
2348
2349        return 0;
2350}
2351
2352static int qlcnic_set_dma_mask(struct pci_dev *pdev, int *pci_using_dac)
2353{
2354        if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
2355                        !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2356                *pci_using_dac = 1;
2357        else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
2358                        !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2359                *pci_using_dac = 0;
2360        else {
2361                dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
2362                return -EIO;
2363        }
2364
2365        return 0;
2366}
2367
2368void qlcnic_free_tx_rings(struct qlcnic_adapter *adapter)
2369{
2370        int ring;
2371        struct qlcnic_host_tx_ring *tx_ring;
2372
2373        for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2374                tx_ring = &adapter->tx_ring[ring];
2375                if (tx_ring) {
2376                        vfree(tx_ring->cmd_buf_arr);
2377                        tx_ring->cmd_buf_arr = NULL;
2378                }
2379        }
2380        kfree(adapter->tx_ring);
2381}
2382
2383int qlcnic_alloc_tx_rings(struct qlcnic_adapter *adapter,
2384                          struct net_device *netdev)
2385{
2386        int ring, vector, index;
2387        struct qlcnic_host_tx_ring *tx_ring;
2388        struct qlcnic_cmd_buffer *cmd_buf_arr;
2389
2390        tx_ring = kcalloc(adapter->drv_tx_rings,
2391                          sizeof(struct qlcnic_host_tx_ring), GFP_KERNEL);
2392        if (tx_ring == NULL)
2393                return -ENOMEM;
2394
2395        adapter->tx_ring = tx_ring;
2396
2397        for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2398                tx_ring = &adapter->tx_ring[ring];
2399                tx_ring->num_desc = adapter->num_txd;
2400                tx_ring->txq = netdev_get_tx_queue(netdev, ring);
2401                cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
2402                if (cmd_buf_arr == NULL) {
2403                        qlcnic_free_tx_rings(adapter);
2404                        return -ENOMEM;
2405                }
2406                memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
2407                tx_ring->cmd_buf_arr = cmd_buf_arr;
2408                spin_lock_init(&tx_ring->tx_clean_lock);
2409        }
2410
2411        if (qlcnic_83xx_check(adapter) ||
2412            (qlcnic_82xx_check(adapter) && qlcnic_check_multi_tx(adapter))) {
2413                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2414                        tx_ring = &adapter->tx_ring[ring];
2415                        tx_ring->adapter = adapter;
2416                        if (adapter->flags & QLCNIC_MSIX_ENABLED) {
2417                                index = adapter->drv_sds_rings + ring;
2418                                vector = adapter->msix_entries[index].vector;
2419                                tx_ring->irq = vector;
2420                        }
2421                }
2422        }
2423
2424        return 0;
2425}
2426
2427void qlcnic_set_drv_version(struct qlcnic_adapter *adapter)
2428{
2429        struct qlcnic_hardware_context *ahw = adapter->ahw;
2430        u32 fw_cmd = 0;
2431
2432        if (qlcnic_82xx_check(adapter))
2433                fw_cmd = QLCNIC_CMD_82XX_SET_DRV_VER;
2434        else if (qlcnic_83xx_check(adapter))
2435                fw_cmd = QLCNIC_CMD_83XX_SET_DRV_VER;
2436
2437        if (ahw->extra_capability[0] & QLCNIC_FW_CAPABILITY_SET_DRV_VER)
2438                qlcnic_fw_cmd_set_drv_version(adapter, fw_cmd);
2439}
2440
2441/* Reset firmware API lock */
2442static void qlcnic_reset_api_lock(struct qlcnic_adapter *adapter)
2443{
2444        qlcnic_api_lock(adapter);
2445        qlcnic_api_unlock(adapter);
2446}
2447
2448
2449static int
2450qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2451{
2452        struct net_device *netdev = NULL;
2453        struct qlcnic_adapter *adapter = NULL;
2454        struct qlcnic_hardware_context *ahw;
2455        int err, pci_using_dac = -1;
2456        char board_name[QLCNIC_MAX_BOARD_NAME_LEN + 19]; /* MAC + ": " + name */
2457
2458        err = pci_enable_device(pdev);
2459        if (err)
2460                return err;
2461
2462        if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2463                err = -ENODEV;
2464                goto err_out_disable_pdev;
2465        }
2466
2467        err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
2468        if (err)
2469                goto err_out_disable_pdev;
2470
2471        err = pci_request_regions(pdev, qlcnic_driver_name);
2472        if (err)
2473                goto err_out_disable_pdev;
2474
2475        pci_set_master(pdev);
2476        pci_enable_pcie_error_reporting(pdev);
2477
2478        ahw = kzalloc(sizeof(struct qlcnic_hardware_context), GFP_KERNEL);
2479        if (!ahw) {
2480                err = -ENOMEM;
2481                goto err_out_free_res;
2482        }
2483
2484        switch (ent->device) {
2485        case PCI_DEVICE_ID_QLOGIC_QLE824X:
2486                ahw->hw_ops = &qlcnic_hw_ops;
2487                ahw->reg_tbl = (u32 *) qlcnic_reg_tbl;
2488                break;
2489        case PCI_DEVICE_ID_QLOGIC_QLE834X:
2490        case PCI_DEVICE_ID_QLOGIC_QLE8830:
2491        case PCI_DEVICE_ID_QLOGIC_QLE844X:
2492                qlcnic_83xx_register_map(ahw);
2493                break;
2494        case PCI_DEVICE_ID_QLOGIC_VF_QLE834X:
2495        case PCI_DEVICE_ID_QLOGIC_VF_QLE844X:
2496                qlcnic_sriov_vf_register_map(ahw);
2497                break;
2498        default:
2499                goto err_out_free_hw_res;
2500        }
2501
2502        err = qlcnic_setup_pci_map(pdev, ahw);
2503        if (err)
2504                goto err_out_free_hw_res;
2505
2506        netdev = alloc_etherdev_mq(sizeof(struct qlcnic_adapter),
2507                                   QLCNIC_MAX_TX_RINGS);
2508        if (!netdev) {
2509                err = -ENOMEM;
2510                goto err_out_iounmap;
2511        }
2512
2513        SET_NETDEV_DEV(netdev, &pdev->dev);
2514
2515        adapter = netdev_priv(netdev);
2516        adapter->netdev  = netdev;
2517        adapter->pdev    = pdev;
2518        adapter->ahw = ahw;
2519
2520        adapter->qlcnic_wq = create_singlethread_workqueue("qlcnic");
2521        if (adapter->qlcnic_wq == NULL) {
2522                err = -ENOMEM;
2523                dev_err(&pdev->dev, "Failed to create workqueue\n");
2524                goto err_out_free_netdev;
2525        }
2526
2527        err = qlcnic_alloc_adapter_resources(adapter);
2528        if (err)
2529                goto err_out_free_wq;
2530
2531        adapter->dev_rst_time = jiffies;
2532        ahw->revision_id = pdev->revision;
2533        ahw->max_vnic_func = qlcnic_get_vnic_func_count(adapter);
2534        if (qlcnic_mac_learn == FDB_MAC_LEARN)
2535                adapter->fdb_mac_learn = true;
2536        else if (qlcnic_mac_learn == DRV_MAC_LEARN)
2537                adapter->drv_mac_learn = true;
2538
2539        rwlock_init(&adapter->ahw->crb_lock);
2540        mutex_init(&adapter->ahw->mem_lock);
2541
2542        INIT_LIST_HEAD(&adapter->mac_list);
2543
2544        qlcnic_register_dcb(adapter);
2545
2546        if (qlcnic_82xx_check(adapter)) {
2547                qlcnic_check_vf(adapter, ent);
2548                adapter->portnum = adapter->ahw->pci_func;
2549                qlcnic_reset_api_lock(adapter);
2550                err = qlcnic_start_firmware(adapter);
2551                if (err) {
2552                        dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n"
2553                                "\t\tIf reboot doesn't help, try flashing the card\n");
2554                        goto err_out_maintenance_mode;
2555                }
2556
2557                /* compute and set default and max tx/sds rings */
2558                if (adapter->ahw->msix_supported) {
2559                        if (qlcnic_check_multi_tx_capability(adapter) == 1)
2560                                qlcnic_set_tx_ring_count(adapter,
2561                                                         QLCNIC_SINGLE_RING);
2562                        else
2563                                qlcnic_set_tx_ring_count(adapter,
2564                                                         QLCNIC_DEF_TX_RINGS);
2565                        qlcnic_set_sds_ring_count(adapter,
2566                                                  QLCNIC_DEF_SDS_RINGS);
2567                } else {
2568                        qlcnic_set_tx_ring_count(adapter, QLCNIC_SINGLE_RING);
2569                        qlcnic_set_sds_ring_count(adapter, QLCNIC_SINGLE_RING);
2570                }
2571
2572                err = qlcnic_setup_idc_param(adapter);
2573                if (err)
2574                        goto err_out_free_hw;
2575
2576                adapter->flags |= QLCNIC_NEED_FLR;
2577
2578        } else if (qlcnic_83xx_check(adapter)) {
2579                qlcnic_83xx_check_vf(adapter, ent);
2580                adapter->portnum = adapter->ahw->pci_func;
2581                err = qlcnic_83xx_init(adapter, pci_using_dac);
2582                if (err) {
2583                        switch (err) {
2584                        case -ENOTRECOVERABLE:
2585                                dev_err(&pdev->dev, "Adapter initialization failed due to a faulty hardware\n");
2586                                dev_err(&pdev->dev, "Please replace the adapter with new one and return the faulty adapter for repair\n");
2587                                goto err_out_free_hw;
2588                        case -ENOMEM:
2589                                dev_err(&pdev->dev, "Adapter initialization failed. Please reboot\n");
2590                                goto err_out_free_hw;
2591                        case -EOPNOTSUPP:
2592                                dev_err(&pdev->dev, "Adapter initialization failed\n");
2593                                goto err_out_free_hw;
2594                        default:
2595                                dev_err(&pdev->dev, "Adapter initialization failed. Driver will load in maintenance mode to recover the adapter using the application\n");
2596                                goto err_out_maintenance_mode;
2597                        }
2598                }
2599
2600                if (qlcnic_sriov_vf_check(adapter))
2601                        return 0;
2602        } else {
2603                dev_err(&pdev->dev,
2604                        "%s: failed. Please Reboot\n", __func__);
2605                err = -ENODEV;
2606                goto err_out_free_hw;
2607        }
2608
2609        if (qlcnic_read_mac_addr(adapter))
2610                dev_warn(&pdev->dev, "failed to read mac addr\n");
2611
2612        qlcnic_read_phys_port_id(adapter);
2613
2614        if (adapter->portnum == 0) {
2615                qlcnic_get_board_name(adapter, board_name);
2616
2617                pr_info("%s: %s Board Chip rev 0x%x\n",
2618                        module_name(THIS_MODULE),
2619                        board_name, adapter->ahw->revision_id);
2620        }
2621
2622        if (qlcnic_83xx_check(adapter) && !qlcnic_use_msi_x &&
2623            !!qlcnic_use_msi)
2624                dev_warn(&pdev->dev,
2625                         "Device does not support MSI interrupts\n");
2626
2627        if (qlcnic_82xx_check(adapter)) {
2628                qlcnic_dcb_enable(adapter->dcb);
2629                qlcnic_dcb_get_info(adapter->dcb);
2630                err = qlcnic_setup_intr(adapter);
2631
2632                if (err) {
2633                        dev_err(&pdev->dev, "Failed to setup interrupt\n");
2634                        goto err_out_disable_msi;
2635                }
2636        }
2637
2638        err = qlcnic_get_act_pci_func(adapter);
2639        if (err)
2640                goto err_out_disable_mbx_intr;
2641
2642        if (adapter->portnum == 0)
2643                qlcnic_set_drv_version(adapter);
2644
2645        err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
2646        if (err)
2647                goto err_out_disable_mbx_intr;
2648
2649        pci_set_drvdata(pdev, adapter);
2650
2651        if (qlcnic_82xx_check(adapter))
2652                qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2653                                     FW_POLL_DELAY);
2654
2655        switch (adapter->ahw->port_type) {
2656        case QLCNIC_GBE:
2657                dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
2658                                adapter->netdev->name);
2659                break;
2660        case QLCNIC_XGBE:
2661                dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
2662                                adapter->netdev->name);
2663                break;
2664        }
2665
2666        if (adapter->drv_mac_learn)
2667                qlcnic_alloc_lb_filters_mem(adapter);
2668
2669        qlcnic_add_sysfs(adapter);
2670        qlcnic_register_hwmon_dev(adapter);
2671        return 0;
2672
2673err_out_disable_mbx_intr:
2674        if (qlcnic_83xx_check(adapter))
2675                qlcnic_83xx_free_mbx_intr(adapter);
2676
2677err_out_disable_msi:
2678        qlcnic_teardown_intr(adapter);
2679        qlcnic_cancel_idc_work(adapter);
2680        qlcnic_clr_all_drv_state(adapter, 0);
2681
2682err_out_free_hw:
2683        qlcnic_free_adapter_resources(adapter);
2684
2685err_out_free_wq:
2686        destroy_workqueue(adapter->qlcnic_wq);
2687
2688err_out_free_netdev:
2689        free_netdev(netdev);
2690
2691err_out_iounmap:
2692        qlcnic_cleanup_pci_map(ahw);
2693
2694err_out_free_hw_res:
2695        kfree(ahw);
2696
2697err_out_free_res:
2698        pci_release_regions(pdev);
2699
2700err_out_disable_pdev:
2701        pci_disable_device(pdev);
2702        return err;
2703
2704err_out_maintenance_mode:
2705        set_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state);
2706        netdev->netdev_ops = &qlcnic_netdev_failed_ops;
2707        netdev->ethtool_ops = &qlcnic_ethtool_failed_ops;
2708        ahw->port_type = QLCNIC_XGBE;
2709
2710        if (qlcnic_83xx_check(adapter))
2711                adapter->tgt_status_reg = NULL;
2712        else
2713                ahw->board_type = QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS;
2714
2715        err = register_netdev(netdev);
2716
2717        if (err) {
2718                dev_err(&pdev->dev, "Failed to register net device\n");
2719                qlcnic_clr_all_drv_state(adapter, 0);
2720                goto err_out_free_hw;
2721        }
2722
2723        pci_set_drvdata(pdev, adapter);
2724        qlcnic_add_sysfs(adapter);
2725
2726        return 0;
2727}
2728
2729static void qlcnic_remove(struct pci_dev *pdev)
2730{
2731        struct qlcnic_adapter *adapter;
2732        struct net_device *netdev;
2733        struct qlcnic_hardware_context *ahw;
2734
2735        adapter = pci_get_drvdata(pdev);
2736        if (adapter == NULL)
2737                return;
2738
2739        netdev = adapter->netdev;
2740
2741        qlcnic_cancel_idc_work(adapter);
2742        qlcnic_sriov_pf_disable(adapter);
2743        ahw = adapter->ahw;
2744
2745        unregister_netdev(netdev);
2746        qlcnic_sriov_cleanup(adapter);
2747
2748        if (qlcnic_83xx_check(adapter)) {
2749                qlcnic_83xx_initialize_nic(adapter, 0);
2750                cancel_delayed_work_sync(&adapter->idc_aen_work);
2751                qlcnic_83xx_free_mbx_intr(adapter);
2752                qlcnic_83xx_detach_mailbox_work(adapter);
2753                qlcnic_83xx_free_mailbox(ahw->mailbox);
2754                kfree(ahw->fw_info);
2755        }
2756
2757        qlcnic_dcb_free(adapter->dcb);
2758        qlcnic_detach(adapter);
2759        kfree(adapter->npars);
2760        kfree(adapter->eswitch);
2761
2762        if (qlcnic_82xx_check(adapter))
2763                qlcnic_clr_all_drv_state(adapter, 0);
2764
2765        clear_bit(__QLCNIC_RESETTING, &adapter->state);
2766
2767        qlcnic_free_lb_filters_mem(adapter);
2768
2769        qlcnic_teardown_intr(adapter);
2770
2771        qlcnic_remove_sysfs(adapter);
2772
2773        qlcnic_unregister_hwmon_dev(adapter);
2774
2775        qlcnic_cleanup_pci_map(adapter->ahw);
2776
2777        qlcnic_release_firmware(adapter);
2778
2779        pci_disable_pcie_error_reporting(pdev);
2780        pci_release_regions(pdev);
2781        pci_disable_device(pdev);
2782
2783        if (adapter->qlcnic_wq) {
2784                destroy_workqueue(adapter->qlcnic_wq);
2785                adapter->qlcnic_wq = NULL;
2786        }
2787
2788        qlcnic_free_adapter_resources(adapter);
2789        kfree(ahw);
2790        free_netdev(netdev);
2791}
2792
2793static void qlcnic_shutdown(struct pci_dev *pdev)
2794{
2795        if (__qlcnic_shutdown(pdev))
2796                return;
2797
2798        pci_disable_device(pdev);
2799}
2800
2801#ifdef CONFIG_PM
2802static int qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
2803{
2804        int retval;
2805
2806        retval = __qlcnic_shutdown(pdev);
2807        if (retval)
2808                return retval;
2809
2810        pci_set_power_state(pdev, pci_choose_state(pdev, state));
2811        return 0;
2812}
2813
2814static int qlcnic_resume(struct pci_dev *pdev)
2815{
2816        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2817        int err;
2818
2819        err = pci_enable_device(pdev);
2820        if (err)
2821                return err;
2822
2823        pci_set_power_state(pdev, PCI_D0);
2824        pci_set_master(pdev);
2825        pci_restore_state(pdev);
2826
2827        return  __qlcnic_resume(adapter);
2828}
2829#endif
2830
2831static int qlcnic_open(struct net_device *netdev)
2832{
2833        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2834        int err;
2835
2836        if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
2837                netdev_err(netdev, "%s: Device is in non-operational state\n",
2838                           __func__);
2839
2840                return -EIO;
2841        }
2842
2843        netif_carrier_off(netdev);
2844
2845        err = qlcnic_attach(adapter);
2846        if (err)
2847                return err;
2848
2849        err = __qlcnic_up(adapter, netdev);
2850        if (err)
2851                qlcnic_detach(adapter);
2852
2853        return err;
2854}
2855
2856/*
2857 * qlcnic_close - Disables a network interface entry point
2858 */
2859static int qlcnic_close(struct net_device *netdev)
2860{
2861        struct qlcnic_adapter *adapter = netdev_priv(netdev);
2862
2863        __qlcnic_down(adapter, netdev);
2864
2865        return 0;
2866}
2867
2868#define QLCNIC_VF_LB_BUCKET_SIZE 1
2869
2870void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
2871{
2872        void *head;
2873        int i;
2874        struct net_device *netdev = adapter->netdev;
2875        u32 filter_size = 0;
2876        u16 act_pci_func = 0;
2877
2878        if (adapter->fhash.fmax && adapter->fhash.fhead)
2879                return;
2880
2881        act_pci_func = adapter->ahw->total_nic_func;
2882        spin_lock_init(&adapter->mac_learn_lock);
2883        spin_lock_init(&adapter->rx_mac_learn_lock);
2884
2885        if (qlcnic_sriov_vf_check(adapter)) {
2886                filter_size = QLCNIC_83XX_SRIOV_VF_MAX_MAC - 1;
2887                adapter->fhash.fbucket_size = QLCNIC_VF_LB_BUCKET_SIZE;
2888        } else if (qlcnic_82xx_check(adapter)) {
2889                filter_size = QLCNIC_LB_MAX_FILTERS;
2890                adapter->fhash.fbucket_size = QLCNIC_LB_BUCKET_SIZE;
2891        } else {
2892                filter_size = QLC_83XX_LB_MAX_FILTERS;
2893                adapter->fhash.fbucket_size = QLC_83XX_LB_BUCKET_SIZE;
2894        }
2895
2896        head = kcalloc(adapter->fhash.fbucket_size,
2897                       sizeof(struct hlist_head), GFP_ATOMIC);
2898
2899        if (!head)
2900                return;
2901
2902        adapter->fhash.fmax = (filter_size / act_pci_func);
2903        adapter->fhash.fhead = head;
2904
2905        netdev_info(netdev, "active nic func = %d, mac filter size=%d\n",
2906                    act_pci_func, adapter->fhash.fmax);
2907
2908        for (i = 0; i < adapter->fhash.fbucket_size; i++)
2909                INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
2910
2911        adapter->rx_fhash.fbucket_size = adapter->fhash.fbucket_size;
2912
2913        head = kcalloc(adapter->rx_fhash.fbucket_size,
2914                       sizeof(struct hlist_head), GFP_ATOMIC);
2915
2916        if (!head)
2917                return;
2918
2919        adapter->rx_fhash.fmax = (filter_size / act_pci_func);
2920        adapter->rx_fhash.fhead = head;
2921
2922        for (i = 0; i < adapter->rx_fhash.fbucket_size; i++)
2923                INIT_HLIST_HEAD(&adapter->rx_fhash.fhead[i]);
2924}
2925
2926static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
2927{
2928        if (adapter->fhash.fmax)
2929                kfree(adapter->fhash.fhead);
2930
2931        adapter->fhash.fhead = NULL;
2932        adapter->fhash.fmax = 0;
2933
2934        if (adapter->rx_fhash.fmax)
2935                kfree(adapter->rx_fhash.fhead);
2936
2937        adapter->rx_fhash.fmax = 0;
2938        adapter->rx_fhash.fhead = NULL;
2939}
2940
2941int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2942{
2943        struct net_device *netdev = adapter->netdev;
2944        u32 temp_state, temp_val, temp = 0;
2945        int rv = 0;
2946
2947        if (qlcnic_83xx_check(adapter))
2948                temp = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
2949
2950        if (qlcnic_82xx_check(adapter))
2951                temp = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
2952
2953        temp_state = qlcnic_get_temp_state(temp);
2954        temp_val = qlcnic_get_temp_val(temp);
2955
2956        if (temp_state == QLCNIC_TEMP_PANIC) {
2957                dev_err(&netdev->dev,
2958                       "Device temperature %d degrees C exceeds"
2959                       " maximum allowed. Hardware has been shut down.\n",
2960                       temp_val);
2961                rv = 1;
2962        } else if (temp_state == QLCNIC_TEMP_WARN) {
2963                if (adapter->ahw->temp == QLCNIC_TEMP_NORMAL) {
2964                        dev_err(&netdev->dev,
2965                               "Device temperature %d degrees C "
2966                               "exceeds operating range."
2967                               " Immediate action needed.\n",
2968                               temp_val);
2969                }
2970        } else {
2971                if (adapter->ahw->temp == QLCNIC_TEMP_WARN) {
2972                        dev_info(&netdev->dev,
2973                               "Device temperature is now %d degrees C"
2974                               " in normal range.\n", temp_val);
2975                }
2976        }
2977        adapter->ahw->temp = temp_state;
2978        return rv;
2979}
2980
2981static inline void dump_tx_ring_desc(struct qlcnic_host_tx_ring *tx_ring)
2982{
2983        int i;
2984        struct cmd_desc_type0 *tx_desc_info;
2985
2986        for (i = 0; i < tx_ring->num_desc; i++) {
2987                tx_desc_info = &tx_ring->desc_head[i];
2988                pr_info("TX Desc: %d\n", i);
2989                print_hex_dump(KERN_INFO, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
2990                               &tx_ring->desc_head[i],
2991                               sizeof(struct cmd_desc_type0), true);
2992        }
2993}
2994
2995static void qlcnic_dump_rings(struct qlcnic_adapter *adapter)
2996{
2997        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2998        struct net_device *netdev = adapter->netdev;
2999        struct qlcnic_host_rds_ring *rds_ring;
3000        struct qlcnic_host_sds_ring *sds_ring;
3001        struct qlcnic_host_tx_ring *tx_ring;
3002        int ring;
3003
3004        if (!netdev || !netif_running(netdev))
3005                return;
3006
3007        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
3008                rds_ring = &recv_ctx->rds_rings[ring];
3009                if (!rds_ring)
3010                        continue;
3011                netdev_info(netdev,
3012                            "rds_ring=%d crb_rcv_producer=%d producer=%u num_desc=%u\n",
3013                             ring, readl(rds_ring->crb_rcv_producer),
3014                             rds_ring->producer, rds_ring->num_desc);
3015        }
3016
3017        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
3018                sds_ring = &(recv_ctx->sds_rings[ring]);
3019                if (!sds_ring)
3020                        continue;
3021                netdev_info(netdev,
3022                            "sds_ring=%d crb_sts_consumer=%d consumer=%u crb_intr_mask=%d num_desc=%u\n",
3023                            ring, readl(sds_ring->crb_sts_consumer),
3024                            sds_ring->consumer, readl(sds_ring->crb_intr_mask),
3025                            sds_ring->num_desc);
3026        }
3027
3028        for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
3029                tx_ring = &adapter->tx_ring[ring];
3030                if (!tx_ring)
3031                        continue;
3032                netdev_info(netdev, "Tx ring=%d Context Id=0x%x\n",
3033                            ring, tx_ring->ctx_id);
3034                netdev_info(netdev,
3035                            "xmit_finished=%llu, xmit_called=%llu, xmit_on=%llu, xmit_off=%llu\n",
3036                            tx_ring->tx_stats.xmit_finished,
3037                            tx_ring->tx_stats.xmit_called,
3038                            tx_ring->tx_stats.xmit_on,
3039                            tx_ring->tx_stats.xmit_off);
3040
3041                if (tx_ring->crb_intr_mask)
3042                        netdev_info(netdev, "crb_intr_mask=%d\n",
3043                                    readl(tx_ring->crb_intr_mask));
3044
3045                netdev_info(netdev,
3046                            "hw_producer=%d, sw_producer=%d sw_consumer=%d, hw_consumer=%d\n",
3047                            readl(tx_ring->crb_cmd_producer),
3048                            tx_ring->producer, tx_ring->sw_consumer,
3049                            le32_to_cpu(*(tx_ring->hw_consumer)));
3050
3051                netdev_info(netdev, "Total desc=%d, Available desc=%d\n",
3052                            tx_ring->num_desc, qlcnic_tx_avail(tx_ring));
3053
3054                if (netif_msg_tx_err(adapter->ahw))
3055                        dump_tx_ring_desc(tx_ring);
3056        }
3057
3058}
3059
3060static void qlcnic_tx_timeout(struct net_device *netdev)
3061{
3062        struct qlcnic_adapter *adapter = netdev_priv(netdev);
3063
3064        if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3065                return;
3066
3067        qlcnic_dump_rings(adapter);
3068
3069        if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS ||
3070            netif_msg_tx_err(adapter->ahw)) {
3071                netdev_err(netdev, "Tx timeout, reset the adapter.\n");
3072                if (qlcnic_82xx_check(adapter))
3073                        adapter->need_fw_reset = 1;
3074                else if (qlcnic_83xx_check(adapter))
3075                        qlcnic_83xx_idc_request_reset(adapter,
3076                                                      QLCNIC_FORCE_FW_DUMP_KEY);
3077        } else {
3078                netdev_err(netdev, "Tx timeout, reset adapter context.\n");
3079                adapter->ahw->reset_context = 1;
3080        }
3081}
3082
3083static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
3084{
3085        struct qlcnic_adapter *adapter = netdev_priv(netdev);
3086        struct net_device_stats *stats = &netdev->stats;
3087
3088        if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
3089                qlcnic_update_stats(adapter);
3090
3091        stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
3092        stats->tx_packets = adapter->stats.xmitfinished;
3093        stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
3094        stats->tx_bytes = adapter->stats.txbytes;
3095        stats->rx_dropped = adapter->stats.rxdropped;
3096        stats->tx_dropped = adapter->stats.txdropped;
3097
3098        return stats;
3099}
3100
3101static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *adapter)
3102{
3103        u32 status;
3104
3105        status = readl(adapter->isr_int_vec);
3106
3107        if (!(status & adapter->ahw->int_vec_bit))
3108                return IRQ_NONE;
3109
3110        /* check interrupt state machine, to be sure */
3111        status = readl(adapter->crb_int_state_reg);
3112        if (!ISR_LEGACY_INT_TRIGGERED(status))
3113                return IRQ_NONE;
3114
3115        writel(0xffffffff, adapter->tgt_status_reg);
3116        /* read twice to ensure write is flushed */
3117        readl(adapter->isr_int_vec);
3118        readl(adapter->isr_int_vec);
3119
3120        return IRQ_HANDLED;
3121}
3122
3123static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
3124{
3125        struct qlcnic_host_sds_ring *sds_ring = data;
3126        struct qlcnic_adapter *adapter = sds_ring->adapter;
3127
3128        if (adapter->flags & QLCNIC_MSIX_ENABLED)
3129                goto done;
3130        else if (adapter->flags & QLCNIC_MSI_ENABLED) {
3131                writel(0xffffffff, adapter->tgt_status_reg);
3132                goto done;
3133        }
3134
3135        if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3136                return IRQ_NONE;
3137
3138done:
3139        adapter->ahw->diag_cnt++;
3140        qlcnic_enable_sds_intr(adapter, sds_ring);
3141        return IRQ_HANDLED;
3142}
3143
3144static irqreturn_t qlcnic_intr(int irq, void *data)
3145{
3146        struct qlcnic_host_sds_ring *sds_ring = data;
3147        struct qlcnic_adapter *adapter = sds_ring->adapter;
3148
3149        if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3150                return IRQ_NONE;
3151
3152        napi_schedule(&sds_ring->napi);
3153
3154        return IRQ_HANDLED;
3155}
3156
3157static irqreturn_t qlcnic_msi_intr(int irq, void *data)
3158{
3159        struct qlcnic_host_sds_ring *sds_ring = data;
3160        struct qlcnic_adapter *adapter = sds_ring->adapter;
3161
3162        /* clear interrupt */
3163        writel(0xffffffff, adapter->tgt_status_reg);
3164
3165        napi_schedule(&sds_ring->napi);
3166        return IRQ_HANDLED;
3167}
3168
3169static irqreturn_t qlcnic_msix_intr(int irq, void *data)
3170{
3171        struct qlcnic_host_sds_ring *sds_ring = data;
3172
3173        napi_schedule(&sds_ring->napi);
3174        return IRQ_HANDLED;
3175}
3176
3177static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data)
3178{
3179        struct qlcnic_host_tx_ring *tx_ring = data;
3180
3181        napi_schedule(&tx_ring->napi);
3182        return IRQ_HANDLED;
3183}
3184
3185#ifdef CONFIG_NET_POLL_CONTROLLER
3186static void qlcnic_poll_controller(struct net_device *netdev)
3187{
3188        struct qlcnic_adapter *adapter = netdev_priv(netdev);
3189        struct qlcnic_host_sds_ring *sds_ring;
3190        struct qlcnic_recv_context *recv_ctx;
3191        struct qlcnic_host_tx_ring *tx_ring;
3192        int ring;
3193
3194        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3195                return;
3196
3197        recv_ctx = adapter->recv_ctx;
3198
3199        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
3200                sds_ring = &recv_ctx->sds_rings[ring];
3201                qlcnic_disable_sds_intr(adapter, sds_ring);
3202                napi_schedule(&sds_ring->napi);
3203        }
3204
3205        if (adapter->flags & QLCNIC_MSIX_ENABLED) {
3206                /* Only Multi-Tx queue capable devices need to
3207                 * schedule NAPI for TX rings
3208                 */
3209                if ((qlcnic_83xx_check(adapter) &&
3210                     (adapter->flags & QLCNIC_TX_INTR_SHARED)) ||
3211                    (qlcnic_82xx_check(adapter) &&
3212                     !qlcnic_check_multi_tx(adapter)))
3213                        return;
3214
3215                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
3216                        tx_ring = &adapter->tx_ring[ring];
3217                        qlcnic_disable_tx_intr(adapter, tx_ring);
3218                        napi_schedule(&tx_ring->napi);
3219                }
3220        }
3221}
3222#endif
3223
3224static void
3225qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
3226{
3227        u32 val;
3228
3229        val = adapter->portnum & 0xf;
3230        val |= encoding << 7;
3231        val |= (jiffies - adapter->dev_rst_time) << 8;
3232
3233        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
3234        adapter->dev_rst_time = jiffies;
3235}
3236
3237static int
3238qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
3239{
3240        u32  val;
3241
3242        WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
3243                        state != QLCNIC_DEV_NEED_QUISCENT);
3244
3245        if (qlcnic_api_lock(adapter))
3246                return -EIO;
3247
3248        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3249
3250        if (state == QLCNIC_DEV_NEED_RESET)
3251                QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3252        else if (state == QLCNIC_DEV_NEED_QUISCENT)
3253                QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
3254
3255        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3256
3257        qlcnic_api_unlock(adapter);
3258
3259        return 0;
3260}
3261
3262static int
3263qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
3264{
3265        u32  val;
3266
3267        if (qlcnic_api_lock(adapter))
3268                return -EBUSY;
3269
3270        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3271        QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3272        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3273
3274        qlcnic_api_unlock(adapter);
3275
3276        return 0;
3277}
3278
3279void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
3280{
3281        u32  val;
3282
3283        if (qlcnic_api_lock(adapter))
3284                goto err;
3285
3286        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3287        QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
3288        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3289
3290        if (failed) {
3291                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3292                                    QLCNIC_DEV_FAILED);
3293                dev_info(&adapter->pdev->dev,
3294                                "Device state set to Failed. Please Reboot\n");
3295        } else if (!(val & 0x11111111))
3296                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3297                                    QLCNIC_DEV_COLD);
3298
3299        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3300        QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3301        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3302
3303        qlcnic_api_unlock(adapter);
3304err:
3305        adapter->fw_fail_cnt = 0;
3306        adapter->flags &= ~QLCNIC_FW_HANG;
3307        clear_bit(__QLCNIC_START_FW, &adapter->state);
3308        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3309}
3310
3311/* Grab api lock, before checking state */
3312static int
3313qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
3314{
3315        int act, state, active_mask;
3316        struct qlcnic_hardware_context *ahw = adapter->ahw;
3317
3318        state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3319        act = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3320
3321        if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
3322                active_mask = (~(1 << (ahw->pci_func * 4)));
3323                act = act & active_mask;
3324        }
3325
3326        if (((state & 0x11111111) == (act & 0x11111111)) ||
3327                        ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
3328                return 0;
3329        else
3330                return 1;
3331}
3332
3333static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
3334{
3335        u32 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
3336
3337        if (val != QLCNIC_DRV_IDC_VER) {
3338                dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
3339                        " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
3340        }
3341
3342        return 0;
3343}
3344
3345static int
3346qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
3347{
3348        u32 val, prev_state;
3349        u8 dev_init_timeo = adapter->dev_init_timeo;
3350        u8 portnum = adapter->portnum;
3351        u8 ret;
3352
3353        if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
3354                return 1;
3355
3356        if (qlcnic_api_lock(adapter))
3357                return -1;
3358
3359        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3360        if (!(val & (1 << (portnum * 4)))) {
3361                QLC_DEV_SET_REF_CNT(val, portnum);
3362                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3363        }
3364
3365        prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3366        QLCDB(adapter, HW, "Device state = %u\n", prev_state);
3367
3368        switch (prev_state) {
3369        case QLCNIC_DEV_COLD:
3370                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3371                                    QLCNIC_DEV_INITIALIZING);
3372                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_IDC_VER,
3373                                    QLCNIC_DRV_IDC_VER);
3374                qlcnic_idc_debug_info(adapter, 0);
3375                qlcnic_api_unlock(adapter);
3376                return 1;
3377
3378        case QLCNIC_DEV_READY:
3379                ret = qlcnic_check_idc_ver(adapter);
3380                qlcnic_api_unlock(adapter);
3381                return ret;
3382
3383        case QLCNIC_DEV_NEED_RESET:
3384                val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3385                QLC_DEV_SET_RST_RDY(val, portnum);
3386                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3387                break;
3388
3389        case QLCNIC_DEV_NEED_QUISCENT:
3390                val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3391                QLC_DEV_SET_QSCNT_RDY(val, portnum);
3392                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3393                break;
3394
3395        case QLCNIC_DEV_FAILED:
3396                dev_err(&adapter->pdev->dev, "Device in failed state.\n");
3397                qlcnic_api_unlock(adapter);
3398                return -1;
3399
3400        case QLCNIC_DEV_INITIALIZING:
3401        case QLCNIC_DEV_QUISCENT:
3402                break;
3403        }
3404
3405        qlcnic_api_unlock(adapter);
3406
3407        do {
3408                msleep(1000);
3409                prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3410
3411                if (prev_state == QLCNIC_DEV_QUISCENT)
3412                        continue;
3413        } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
3414
3415        if (!dev_init_timeo) {
3416                dev_err(&adapter->pdev->dev,
3417                        "Waiting for device to initialize timeout\n");
3418                return -1;
3419        }
3420
3421        if (qlcnic_api_lock(adapter))
3422                return -1;
3423
3424        val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3425        QLC_DEV_CLR_RST_QSCNT(val, portnum);
3426        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3427
3428        ret = qlcnic_check_idc_ver(adapter);
3429        qlcnic_api_unlock(adapter);
3430
3431        return ret;
3432}
3433
3434static void
3435qlcnic_fwinit_work(struct work_struct *work)
3436{
3437        struct qlcnic_adapter *adapter = container_of(work,
3438                        struct qlcnic_adapter, fw_work.work);
3439        u32 dev_state = 0xf;
3440        u32 val;
3441
3442        if (qlcnic_api_lock(adapter))
3443                goto err_ret;
3444
3445        dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3446        if (dev_state == QLCNIC_DEV_QUISCENT ||
3447            dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3448                qlcnic_api_unlock(adapter);
3449                qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
3450                                                FW_POLL_DELAY * 2);
3451                return;
3452        }
3453
3454        if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
3455                qlcnic_api_unlock(adapter);
3456                goto wait_npar;
3457        }
3458
3459        if (dev_state == QLCNIC_DEV_INITIALIZING ||
3460            dev_state == QLCNIC_DEV_READY) {
3461                dev_info(&adapter->pdev->dev, "Detected state change from "
3462                                "DEV_NEED_RESET, skipping ack check\n");
3463                goto skip_ack_check;
3464        }
3465
3466        if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
3467                dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
3468                                        adapter->reset_ack_timeo);
3469                goto skip_ack_check;
3470        }
3471
3472        if (!qlcnic_check_drv_state(adapter)) {
3473skip_ack_check:
3474                dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3475
3476                if (dev_state == QLCNIC_DEV_NEED_RESET) {
3477                        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3478                                            QLCNIC_DEV_INITIALIZING);
3479                        set_bit(__QLCNIC_START_FW, &adapter->state);
3480                        QLCDB(adapter, DRV, "Restarting fw\n");
3481                        qlcnic_idc_debug_info(adapter, 0);
3482                        val = QLC_SHARED_REG_RD32(adapter,
3483                                                  QLCNIC_CRB_DRV_STATE);
3484                        QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3485                        QLC_SHARED_REG_WR32(adapter,
3486                                            QLCNIC_CRB_DRV_STATE, val);
3487                }
3488
3489                qlcnic_api_unlock(adapter);
3490
3491                rtnl_lock();
3492                if (qlcnic_check_fw_dump_state(adapter) &&
3493                    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3494                        QLCDB(adapter, DRV, "Take FW dump\n");
3495                        qlcnic_dump_fw(adapter);
3496                        adapter->flags |= QLCNIC_FW_HANG;
3497                }
3498                rtnl_unlock();
3499
3500                adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
3501                if (!adapter->nic_ops->start_firmware(adapter)) {
3502                        qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3503                        adapter->fw_wait_cnt = 0;
3504                        return;
3505                }
3506                goto err_ret;
3507        }
3508
3509        qlcnic_api_unlock(adapter);
3510
3511wait_npar:
3512        dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3513        QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
3514
3515        switch (dev_state) {
3516        case QLCNIC_DEV_READY:
3517                if (!qlcnic_start_firmware(adapter)) {
3518                        qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3519                        adapter->fw_wait_cnt = 0;
3520                        return;
3521                }
3522        case QLCNIC_DEV_FAILED:
3523                break;
3524        default:
3525                qlcnic_schedule_work(adapter,
3526                        qlcnic_fwinit_work, FW_POLL_DELAY);
3527                return;
3528        }
3529
3530err_ret:
3531        dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
3532                "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
3533        netif_device_attach(adapter->netdev);
3534        qlcnic_clr_all_drv_state(adapter, 0);
3535}
3536
3537static void
3538qlcnic_detach_work(struct work_struct *work)
3539{
3540        struct qlcnic_adapter *adapter = container_of(work,
3541                        struct qlcnic_adapter, fw_work.work);
3542        struct net_device *netdev = adapter->netdev;
3543        u32 status;
3544
3545        netif_device_detach(netdev);
3546
3547        /* Dont grab rtnl lock during Quiscent mode */
3548        if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3549                if (netif_running(netdev))
3550                        __qlcnic_down(adapter, netdev);
3551        } else
3552                qlcnic_down(adapter, netdev);
3553
3554        status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3555
3556        if (status & QLCNIC_RCODE_FATAL_ERROR) {
3557                dev_err(&adapter->pdev->dev,
3558                        "Detaching the device: peg halt status1=0x%x\n",
3559                                        status);
3560
3561                if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
3562                        dev_err(&adapter->pdev->dev,
3563                        "On board active cooling fan failed. "
3564                                "Device has been halted.\n");
3565                        dev_err(&adapter->pdev->dev,
3566                                "Replace the adapter.\n");
3567                }
3568
3569                goto err_ret;
3570        }
3571
3572        if (adapter->ahw->temp == QLCNIC_TEMP_PANIC) {
3573                dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
3574                        adapter->ahw->temp);
3575                goto err_ret;
3576        }
3577
3578        /* Dont ack if this instance is the reset owner */
3579        if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3580                if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
3581                        dev_err(&adapter->pdev->dev,
3582                                "Failed to set driver state,"
3583                                        "detaching the device.\n");
3584                        goto err_ret;
3585                }
3586        }
3587
3588        adapter->fw_wait_cnt = 0;
3589
3590        qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3591
3592        return;
3593
3594err_ret:
3595        netif_device_attach(netdev);
3596        qlcnic_clr_all_drv_state(adapter, 1);
3597}
3598
3599/*Transit NPAR state to NON Operational */
3600static void
3601qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3602{
3603        u32 state;
3604
3605        state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3606        if (state == QLCNIC_DEV_NPAR_NON_OPER)
3607                return;
3608
3609        if (qlcnic_api_lock(adapter))
3610                return;
3611        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3612                            QLCNIC_DEV_NPAR_NON_OPER);
3613        qlcnic_api_unlock(adapter);
3614}
3615
3616static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *adapter,
3617                                          u32 key)
3618{
3619        u32 state, xg_val = 0, gb_val = 0;
3620
3621        qlcnic_xg_set_xg0_mask(xg_val);
3622        qlcnic_xg_set_xg1_mask(xg_val);
3623        QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3624        qlcnic_gb_set_gb0_mask(gb_val);
3625        qlcnic_gb_set_gb1_mask(gb_val);
3626        qlcnic_gb_set_gb2_mask(gb_val);
3627        qlcnic_gb_set_gb3_mask(gb_val);
3628        QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3629        dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3630                                " on all ports\n");
3631        adapter->need_fw_reset = 1;
3632
3633        if (qlcnic_api_lock(adapter))
3634                return;
3635
3636        state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3637
3638        if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
3639                netdev_err(adapter->netdev, "%s: Device is in non-operational state\n",
3640                           __func__);
3641                qlcnic_api_unlock(adapter);
3642
3643                return;
3644        }
3645
3646        if (state == QLCNIC_DEV_READY) {
3647                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3648                                    QLCNIC_DEV_NEED_RESET);
3649                adapter->flags |= QLCNIC_FW_RESET_OWNER;
3650                QLCDB(adapter, DRV, "NEED_RESET state set\n");
3651                qlcnic_idc_debug_info(adapter, 0);
3652        }
3653
3654        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3655                            QLCNIC_DEV_NPAR_NON_OPER);
3656        qlcnic_api_unlock(adapter);
3657}
3658
3659/* Transit to NPAR READY state from NPAR NOT READY state */
3660static void
3661qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3662{
3663        if (qlcnic_api_lock(adapter))
3664                return;
3665
3666        QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3667                            QLCNIC_DEV_NPAR_OPER);
3668        QLCDB(adapter, DRV, "NPAR operational state set\n");
3669
3670        qlcnic_api_unlock(adapter);
3671}
3672
3673void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3674                          work_func_t func, int delay)
3675{
3676        if (test_bit(__QLCNIC_AER, &adapter->state))
3677                return;
3678
3679        INIT_DELAYED_WORK(&adapter->fw_work, func);
3680        queue_delayed_work(adapter->qlcnic_wq, &adapter->fw_work,
3681                           round_jiffies_relative(delay));
3682}
3683
3684static void
3685qlcnic_attach_work(struct work_struct *work)
3686{
3687        struct qlcnic_adapter *adapter = container_of(work,
3688                                struct qlcnic_adapter, fw_work.work);
3689        struct net_device *netdev = adapter->netdev;
3690        u32 npar_state;
3691
3692        if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC) {
3693                npar_state = QLC_SHARED_REG_RD32(adapter,
3694                                                 QLCNIC_CRB_DEV_NPAR_STATE);
3695                if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3696                        qlcnic_clr_all_drv_state(adapter, 0);
3697                else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3698                        qlcnic_schedule_work(adapter, qlcnic_attach_work,
3699                                                        FW_POLL_DELAY);
3700                else
3701                        goto attach;
3702                QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3703                return;
3704        }
3705attach:
3706        qlcnic_dcb_get_info(adapter->dcb);
3707
3708        if (netif_running(netdev)) {
3709                if (qlcnic_up(adapter, netdev))
3710                        goto done;
3711
3712                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3713        }
3714
3715done:
3716        netif_device_attach(netdev);
3717        adapter->fw_fail_cnt = 0;
3718        adapter->flags &= ~QLCNIC_FW_HANG;
3719        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3720        if (adapter->portnum == 0)
3721                qlcnic_set_drv_version(adapter);
3722
3723        if (!qlcnic_clr_drv_state(adapter))
3724                qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3725                                                        FW_POLL_DELAY);
3726}
3727
3728static int
3729qlcnic_check_health(struct qlcnic_adapter *adapter)
3730{
3731        struct qlcnic_hardware_context *ahw = adapter->ahw;
3732        struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
3733        u32 state = 0, heartbeat;
3734        u32 peg_status;
3735        int err = 0;
3736
3737        if (qlcnic_check_temp(adapter))
3738                goto detach;
3739
3740        if (adapter->need_fw_reset)
3741                qlcnic_dev_request_reset(adapter, 0);
3742
3743        state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3744        if (state == QLCNIC_DEV_NEED_RESET) {
3745                qlcnic_set_npar_non_operational(adapter);
3746                adapter->need_fw_reset = 1;
3747        } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3748                goto detach;
3749
3750        heartbeat = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3751        if (heartbeat != adapter->heartbeat) {
3752                adapter->heartbeat = heartbeat;
3753                adapter->fw_fail_cnt = 0;
3754                if (adapter->need_fw_reset)
3755                        goto detach;
3756
3757                if (ahw->reset_context && qlcnic_auto_fw_reset)
3758                        qlcnic_reset_hw_context(adapter);
3759
3760                return 0;
3761        }
3762
3763        if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3764                return 0;
3765
3766        adapter->flags |= QLCNIC_FW_HANG;
3767
3768        qlcnic_dev_request_reset(adapter, 0);
3769
3770        if (qlcnic_auto_fw_reset)
3771                clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3772
3773        dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3774        peg_status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3775        dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3776                        "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3777                        "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3778                        "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3779                        "PEG_NET_4_PC: 0x%x\n",
3780                        peg_status,
3781                        QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3782                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, &err),
3783                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, &err),
3784                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, &err),
3785                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, &err),
3786                        QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, &err));
3787        if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3788                dev_err(&adapter->pdev->dev,
3789                        "Firmware aborted with error code 0x00006700. "
3790                                "Device is being reset.\n");
3791detach:
3792        adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3793                QLCNIC_DEV_NEED_RESET;
3794
3795        if (qlcnic_auto_fw_reset && !test_and_set_bit(__QLCNIC_RESETTING,
3796                                                      &adapter->state)) {
3797
3798                qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3799                QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3800        } else if (!qlcnic_auto_fw_reset && fw_dump->enable &&
3801                   adapter->flags & QLCNIC_FW_RESET_OWNER) {
3802                qlcnic_dump_fw(adapter);
3803        }
3804
3805        return 1;
3806}
3807
3808void qlcnic_fw_poll_work(struct work_struct *work)
3809{
3810        struct qlcnic_adapter *adapter = container_of(work,
3811                                struct qlcnic_adapter, fw_work.work);
3812
3813        if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3814                goto reschedule;
3815
3816
3817        if (qlcnic_check_health(adapter))
3818                return;
3819
3820        if (adapter->fhash.fnum)
3821                qlcnic_prune_lb_filters(adapter);
3822
3823reschedule:
3824        qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3825}
3826
3827static int qlcnic_is_first_func(struct pci_dev *pdev)
3828{
3829        struct pci_dev *oth_pdev;
3830        int val = pdev->devfn;
3831
3832        while (val-- > 0) {
3833                oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3834                        (pdev->bus), pdev->bus->number,
3835                        PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3836                if (!oth_pdev)
3837                        continue;
3838
3839                if (oth_pdev->current_state != PCI_D3cold) {
3840                        pci_dev_put(oth_pdev);
3841                        return 0;
3842                }
3843                pci_dev_put(oth_pdev);
3844        }
3845        return 1;
3846}
3847
3848static int qlcnic_attach_func(struct pci_dev *pdev)
3849{
3850        int err, first_func;
3851        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3852        struct net_device *netdev = adapter->netdev;
3853
3854        pdev->error_state = pci_channel_io_normal;
3855
3856        err = pci_enable_device(pdev);
3857        if (err)
3858                return err;
3859
3860        pci_set_master(pdev);
3861        pci_restore_state(pdev);
3862
3863        first_func = qlcnic_is_first_func(pdev);
3864
3865        if (qlcnic_api_lock(adapter))
3866                return -EINVAL;
3867
3868        if (adapter->ahw->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3869                adapter->need_fw_reset = 1;
3870                set_bit(__QLCNIC_START_FW, &adapter->state);
3871                QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3872                                    QLCNIC_DEV_INITIALIZING);
3873                QLCDB(adapter, DRV, "Restarting fw\n");
3874        }
3875        qlcnic_api_unlock(adapter);
3876
3877        err = qlcnic_start_firmware(adapter);
3878        if (err)
3879                return err;
3880
3881        qlcnic_clr_drv_state(adapter);
3882        kfree(adapter->msix_entries);
3883        adapter->msix_entries = NULL;
3884        err = qlcnic_setup_intr(adapter);
3885
3886        if (err) {
3887                kfree(adapter->msix_entries);
3888                netdev_err(netdev, "failed to setup interrupt\n");
3889                return err;
3890        }
3891
3892        if (netif_running(netdev)) {
3893                err = qlcnic_attach(adapter);
3894                if (err) {
3895                        qlcnic_clr_all_drv_state(adapter, 1);
3896                        clear_bit(__QLCNIC_AER, &adapter->state);
3897                        netif_device_attach(netdev);
3898                        return err;
3899                }
3900
3901                err = qlcnic_up(adapter, netdev);
3902                if (err)
3903                        goto done;
3904
3905                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3906        }
3907 done:
3908        netif_device_attach(netdev);
3909        return err;
3910}
3911
3912static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *pdev,
3913                                                      pci_channel_state_t state)
3914{
3915        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3916        struct net_device *netdev = adapter->netdev;
3917
3918        if (state == pci_channel_io_perm_failure)
3919                return PCI_ERS_RESULT_DISCONNECT;
3920
3921        if (state == pci_channel_io_normal)
3922                return PCI_ERS_RESULT_RECOVERED;
3923
3924        set_bit(__QLCNIC_AER, &adapter->state);
3925        netif_device_detach(netdev);
3926
3927        cancel_delayed_work_sync(&adapter->fw_work);
3928
3929        if (netif_running(netdev))
3930                qlcnic_down(adapter, netdev);
3931
3932        qlcnic_detach(adapter);
3933        qlcnic_teardown_intr(adapter);
3934
3935        clear_bit(__QLCNIC_RESETTING, &adapter->state);
3936
3937        pci_save_state(pdev);
3938        pci_disable_device(pdev);
3939
3940        return PCI_ERS_RESULT_NEED_RESET;
3941}
3942
3943static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *pdev)
3944{
3945        return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3946                                PCI_ERS_RESULT_RECOVERED;
3947}
3948
3949static void qlcnic_82xx_io_resume(struct pci_dev *pdev)
3950{
3951        u32 state;
3952        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3953
3954        pci_cleanup_aer_uncorrect_error_status(pdev);
3955        state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3956        if (state == QLCNIC_DEV_READY && test_and_clear_bit(__QLCNIC_AER,
3957                                                            &adapter->state))
3958                qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3959                                     FW_POLL_DELAY);
3960}
3961
3962static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3963                                                 pci_channel_state_t state)
3964{
3965        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3966        struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3967
3968        if (hw_ops->io_error_detected) {
3969                return hw_ops->io_error_detected(pdev, state);
3970        } else {
3971                dev_err(&pdev->dev, "AER error_detected handler not registered.\n");
3972                return PCI_ERS_RESULT_DISCONNECT;
3973        }
3974}
3975
3976static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3977{
3978        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3979        struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3980
3981        if (hw_ops->io_slot_reset) {
3982                return hw_ops->io_slot_reset(pdev);
3983        } else {
3984                dev_err(&pdev->dev, "AER slot_reset handler not registered.\n");
3985                return PCI_ERS_RESULT_DISCONNECT;
3986        }
3987}
3988
3989static void qlcnic_io_resume(struct pci_dev *pdev)
3990{
3991        struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3992        struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3993
3994        if (hw_ops->io_resume)
3995                hw_ops->io_resume(pdev);
3996        else
3997                dev_err(&pdev->dev, "AER resume handler not registered.\n");
3998}
3999
4000
4001static int
4002qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
4003{
4004        int err;
4005
4006        err = qlcnic_can_start_firmware(adapter);
4007        if (err)
4008                return err;
4009
4010        err = qlcnic_check_npar_opertional(adapter);
4011        if (err)
4012                return err;
4013
4014        err = qlcnic_initialize_nic(adapter);
4015        if (err)
4016                return err;
4017
4018        qlcnic_check_options(adapter);
4019
4020        err = qlcnic_set_eswitch_port_config(adapter);
4021        if (err)
4022                return err;
4023
4024        adapter->need_fw_reset = 0;
4025
4026        return err;
4027}
4028
4029int qlcnic_validate_rings(struct qlcnic_adapter *adapter, __u32 ring_cnt,
4030                          int queue_type)
4031{
4032        struct net_device *netdev = adapter->netdev;
4033        u8 max_hw_rings = 0;
4034        char buf[8];
4035        int cur_rings;
4036
4037        if (queue_type == QLCNIC_RX_QUEUE) {
4038                max_hw_rings = adapter->max_sds_rings;
4039                cur_rings = adapter->drv_sds_rings;
4040                strcpy(buf, "SDS");
4041        } else if (queue_type == QLCNIC_TX_QUEUE) {
4042                max_hw_rings = adapter->max_tx_rings;
4043                cur_rings = adapter->drv_tx_rings;
4044                strcpy(buf, "Tx");
4045        }
4046
4047        if (!is_power_of_2(ring_cnt)) {
4048                netdev_err(netdev, "%s rings value should be a power of 2\n",
4049                           buf);
4050                return -EINVAL;
4051        }
4052
4053        if (qlcnic_82xx_check(adapter) && (queue_type == QLCNIC_TX_QUEUE) &&
4054            !qlcnic_check_multi_tx(adapter)) {
4055                        netdev_err(netdev, "No Multi Tx queue support\n");
4056                        return -EINVAL;
4057        }
4058
4059        if (ring_cnt > num_online_cpus()) {
4060                netdev_err(netdev,
4061                           "%s value[%u] should not be higher than, number of online CPUs\n",
4062                           buf, num_online_cpus());
4063                return -EINVAL;
4064        }
4065
4066        return 0;
4067}
4068
4069int qlcnic_setup_rings(struct qlcnic_adapter *adapter)
4070{
4071        struct net_device *netdev = adapter->netdev;
4072        u8 tx_rings, rx_rings;
4073        int err;
4074
4075        if (test_bit(__QLCNIC_RESETTING, &adapter->state))
4076                return -EBUSY;
4077
4078        tx_rings = adapter->drv_tss_rings;
4079        rx_rings = adapter->drv_rss_rings;
4080
4081        netif_device_detach(netdev);
4082
4083        err = qlcnic_set_real_num_queues(adapter, tx_rings, rx_rings);
4084        if (err)
4085                goto done;
4086
4087        if (netif_running(netdev))
4088                __qlcnic_down(adapter, netdev);
4089
4090        qlcnic_detach(adapter);
4091
4092        if (qlcnic_83xx_check(adapter)) {
4093                qlcnic_83xx_free_mbx_intr(adapter);
4094                qlcnic_83xx_enable_mbx_poll(adapter);
4095        }
4096
4097        qlcnic_teardown_intr(adapter);
4098
4099        err = qlcnic_setup_intr(adapter);
4100        if (err) {
4101                kfree(adapter->msix_entries);
4102                netdev_err(netdev, "failed to setup interrupt\n");
4103                return err;
4104        }
4105
4106        /* Check if we need to update real_num_{tx|rx}_queues because
4107         * qlcnic_setup_intr() may change Tx/Rx rings size
4108         */
4109        if ((tx_rings != adapter->drv_tx_rings) ||
4110            (rx_rings != adapter->drv_sds_rings)) {
4111                err = qlcnic_set_real_num_queues(adapter,
4112                                                 adapter->drv_tx_rings,
4113                                                 adapter->drv_sds_rings);
4114                if (err)
4115                        goto done;
4116        }
4117
4118        if (qlcnic_83xx_check(adapter)) {
4119                qlcnic_83xx_initialize_nic(adapter, 1);
4120                err = qlcnic_83xx_setup_mbx_intr(adapter);
4121                qlcnic_83xx_disable_mbx_poll(adapter);
4122                if (err) {
4123                        dev_err(&adapter->pdev->dev,
4124                                "failed to setup mbx interrupt\n");
4125                        goto done;
4126                }
4127        }
4128
4129        if (netif_running(netdev)) {
4130                err = qlcnic_attach(adapter);
4131                if (err)
4132                        goto done;
4133                err = __qlcnic_up(adapter, netdev);
4134                if (err)
4135                        goto done;
4136                qlcnic_restore_indev_addr(netdev, NETDEV_UP);
4137        }
4138done:
4139        netif_device_attach(netdev);
4140        clear_bit(__QLCNIC_RESETTING, &adapter->state);
4141        return err;
4142}
4143
4144#ifdef CONFIG_INET
4145
4146#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4147
4148static void
4149qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4150                        struct net_device *dev, unsigned long event)
4151{
4152        struct in_device *indev;
4153
4154        indev = in_dev_get(dev);
4155        if (!indev)
4156                return;
4157
4158        for_ifa(indev) {
4159                switch (event) {
4160                case NETDEV_UP:
4161                        qlcnic_config_ipaddr(adapter,
4162                                        ifa->ifa_address, QLCNIC_IP_UP);
4163                        break;
4164                case NETDEV_DOWN:
4165                        qlcnic_config_ipaddr(adapter,
4166                                        ifa->ifa_address, QLCNIC_IP_DOWN);
4167                        break;
4168                default:
4169                        break;
4170                }
4171        } endfor_ifa(indev);
4172
4173        in_dev_put(indev);
4174}
4175
4176void qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4177{
4178        struct qlcnic_adapter *adapter = netdev_priv(netdev);
4179        struct net_device *dev;
4180        u16 vid;
4181
4182        qlcnic_config_indev_addr(adapter, netdev, event);
4183
4184        rcu_read_lock();
4185        for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4186                dev = __vlan_find_dev_deep_rcu(netdev, htons(ETH_P_8021Q), vid);
4187                if (!dev)
4188                        continue;
4189                qlcnic_config_indev_addr(adapter, dev, event);
4190        }
4191        rcu_read_unlock();
4192}
4193
4194static int qlcnic_netdev_event(struct notifier_block *this,
4195                                 unsigned long event, void *ptr)
4196{
4197        struct qlcnic_adapter *adapter;
4198        struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4199
4200recheck:
4201        if (dev == NULL)
4202                goto done;
4203
4204        if (dev->priv_flags & IFF_802_1Q_VLAN) {
4205                dev = vlan_dev_real_dev(dev);
4206                goto recheck;
4207        }
4208
4209        if (!is_qlcnic_netdev(dev))
4210                goto done;
4211
4212        adapter = netdev_priv(dev);
4213
4214        if (!adapter)
4215                goto done;
4216
4217        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4218                goto done;
4219
4220        qlcnic_config_indev_addr(adapter, dev, event);
4221done:
4222        return NOTIFY_DONE;
4223}
4224
4225static int
4226qlcnic_inetaddr_event(struct notifier_block *this,
4227                unsigned long event, void *ptr)
4228{
4229        struct qlcnic_adapter *adapter;
4230        struct net_device *dev;
4231
4232        struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4233
4234        dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4235
4236recheck:
4237        if (dev == NULL)
4238                goto done;
4239
4240        if (dev->priv_flags & IFF_802_1Q_VLAN) {
4241                dev = vlan_dev_real_dev(dev);
4242                goto recheck;
4243        }
4244
4245        if (!is_qlcnic_netdev(dev))
4246                goto done;
4247
4248        adapter = netdev_priv(dev);
4249
4250        if (!adapter)
4251                goto done;
4252
4253        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4254                goto done;
4255
4256        switch (event) {
4257        case NETDEV_UP:
4258                qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4259
4260                break;
4261        case NETDEV_DOWN:
4262                qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4263
4264                break;
4265        default:
4266                break;
4267        }
4268
4269done:
4270        return NOTIFY_DONE;
4271}
4272
4273static struct notifier_block    qlcnic_netdev_cb = {
4274        .notifier_call = qlcnic_netdev_event,
4275};
4276
4277static struct notifier_block qlcnic_inetaddr_cb = {
4278        .notifier_call = qlcnic_inetaddr_event,
4279};
4280#else
4281void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4282{ }
4283#endif
4284static const struct pci_error_handlers qlcnic_err_handler = {
4285        .error_detected = qlcnic_io_error_detected,
4286        .slot_reset = qlcnic_io_slot_reset,
4287        .resume = qlcnic_io_resume,
4288};
4289
4290static struct pci_driver qlcnic_driver = {
4291        .name = qlcnic_driver_name,
4292        .id_table = qlcnic_pci_tbl,
4293        .probe = qlcnic_probe,
4294        .remove = qlcnic_remove,
4295#ifdef CONFIG_PM
4296        .suspend = qlcnic_suspend,
4297        .resume = qlcnic_resume,
4298#endif
4299        .shutdown = qlcnic_shutdown,
4300        .err_handler = &qlcnic_err_handler,
4301#ifdef CONFIG_QLCNIC_SRIOV
4302        .sriov_configure = qlcnic_pci_sriov_configure,
4303#endif
4304
4305};
4306
4307static int __init qlcnic_init_module(void)
4308{
4309        int ret;
4310
4311        printk(KERN_INFO "%s\n", qlcnic_driver_string);
4312
4313#ifdef CONFIG_INET
4314        register_netdevice_notifier(&qlcnic_netdev_cb);
4315        register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4316#endif
4317
4318        ret = pci_register_driver(&qlcnic_driver);
4319        if (ret) {
4320#ifdef CONFIG_INET
4321                unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4322                unregister_netdevice_notifier(&qlcnic_netdev_cb);
4323#endif
4324        }
4325
4326        return ret;
4327}
4328
4329module_init(qlcnic_init_module);
4330
4331static void __exit qlcnic_exit_module(void)
4332{
4333        pci_unregister_driver(&qlcnic_driver);
4334
4335#ifdef CONFIG_INET
4336        unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4337        unregister_netdevice_notifier(&qlcnic_netdev_cb);
4338#endif
4339}
4340
4341module_exit(qlcnic_exit_module);
4342