cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

qlcnic_main.c (110571B)


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