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