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