1aa43c215SJeff Kirsher /*
2aa43c215SJeff Kirsher  * QLogic qlcnic NIC Driver
3577ae39dSJitendra Kalsaria  * Copyright (c) 2009-2013 QLogic Corporation
4aa43c215SJeff Kirsher  *
5aa43c215SJeff Kirsher  * See LICENSE.qlcnic for copyright and licensing details.
6aa43c215SJeff Kirsher  */
7aa43c215SJeff Kirsher 
8aa43c215SJeff Kirsher #include <linux/vmalloc.h>
9aa43c215SJeff Kirsher #include <linux/interrupt.h>
10aa43c215SJeff Kirsher 
11aa43c215SJeff Kirsher #include "qlcnic.h"
12a15ebd37SHimanshu Madhani #include "qlcnic_hw.h"
13aa43c215SJeff Kirsher 
14aa43c215SJeff Kirsher #include <linux/swab.h>
15aa43c215SJeff Kirsher #include <linux/dma-mapping.h>
1613159183SSony Chacko #include <linux/if_vlan.h>
17aa43c215SJeff Kirsher #include <net/ip.h>
18aa43c215SJeff Kirsher #include <linux/ipv6.h>
19aa43c215SJeff Kirsher #include <linux/inetdevice.h>
20aa43c215SJeff Kirsher #include <linux/aer.h>
21aa43c215SJeff Kirsher #include <linux/log2.h>
2247caf255SStephen Rothwell #include <linux/pci.h>
23aa43c215SJeff Kirsher 
24aa43c215SJeff Kirsher MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
25aa43c215SJeff Kirsher MODULE_LICENSE("GPL");
26aa43c215SJeff Kirsher MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
27aa43c215SJeff Kirsher MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
28aa43c215SJeff Kirsher 
29aa43c215SJeff Kirsher char qlcnic_driver_name[] = "qlcnic";
30aa43c215SJeff Kirsher static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
31aa43c215SJeff Kirsher 	"Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
32aa43c215SJeff Kirsher 
33aa43c215SJeff Kirsher static int qlcnic_mac_learn;
34aa43c215SJeff Kirsher module_param(qlcnic_mac_learn, int, 0444);
35fe1adc6bSJitendra Kalsaria MODULE_PARM_DESC(qlcnic_mac_learn,
36fe1adc6bSJitendra Kalsaria 		 "Mac Filter (0=learning is disabled, 1=Driver learning is enabled, 2=FDB learning is enabled)");
37aa43c215SJeff Kirsher 
38629263acSSony Chacko int qlcnic_use_msi = 1;
39aa43c215SJeff Kirsher MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
40099f7aa7SSony Chacko module_param_named(use_msi, qlcnic_use_msi, int, 0444);
41aa43c215SJeff Kirsher 
42629263acSSony Chacko int qlcnic_use_msi_x = 1;
43aa43c215SJeff Kirsher MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
44099f7aa7SSony Chacko module_param_named(use_msi_x, qlcnic_use_msi_x, int, 0444);
45aa43c215SJeff Kirsher 
46629263acSSony Chacko int qlcnic_auto_fw_reset = 1;
47aa43c215SJeff Kirsher MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
48099f7aa7SSony Chacko module_param_named(auto_fw_reset, qlcnic_auto_fw_reset, int, 0644);
49aa43c215SJeff Kirsher 
50629263acSSony Chacko int qlcnic_load_fw_file;
51aa43c215SJeff Kirsher MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
52099f7aa7SSony Chacko module_param_named(load_fw_file, qlcnic_load_fw_file, int, 0444);
53aa43c215SJeff Kirsher 
54629263acSSony Chacko int qlcnic_config_npars;
55aa43c215SJeff Kirsher module_param(qlcnic_config_npars, int, 0444);
56aa43c215SJeff Kirsher MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
57aa43c215SJeff Kirsher 
581dd06ae8SGreg Kroah-Hartman static int qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
596bb58bb0SBill Pemberton static void qlcnic_remove(struct pci_dev *pdev);
60aa43c215SJeff Kirsher static int qlcnic_open(struct net_device *netdev);
61aa43c215SJeff Kirsher static int qlcnic_close(struct net_device *netdev);
62aa43c215SJeff Kirsher static void qlcnic_tx_timeout(struct net_device *netdev);
63aa43c215SJeff Kirsher static void qlcnic_attach_work(struct work_struct *work);
64aa43c215SJeff Kirsher static void qlcnic_fwinit_work(struct work_struct *work);
65aa43c215SJeff Kirsher static void qlcnic_fw_poll_work(struct work_struct *work);
66aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
67aa43c215SJeff Kirsher static void qlcnic_poll_controller(struct net_device *netdev);
68aa43c215SJeff Kirsher #endif
69aa43c215SJeff Kirsher 
70aa43c215SJeff Kirsher static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
71aa43c215SJeff Kirsher static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
72aa43c215SJeff Kirsher static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
73aa43c215SJeff Kirsher 
74aa43c215SJeff Kirsher static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
75aa43c215SJeff Kirsher static irqreturn_t qlcnic_intr(int irq, void *data);
76aa43c215SJeff Kirsher static irqreturn_t qlcnic_msi_intr(int irq, void *data);
77aa43c215SJeff Kirsher static irqreturn_t qlcnic_msix_intr(int irq, void *data);
7813159183SSony Chacko static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data);
79aa43c215SJeff Kirsher 
80aa43c215SJeff Kirsher static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
81aa43c215SJeff Kirsher static int qlcnic_start_firmware(struct qlcnic_adapter *);
82aa43c215SJeff Kirsher 
83aa43c215SJeff Kirsher static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
84aa43c215SJeff Kirsher static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
85aa43c215SJeff Kirsher static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
86aa43c215SJeff Kirsher static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
87aa43c215SJeff Kirsher 				struct qlcnic_esw_func_cfg *);
888e586137SJiri Pirko static int qlcnic_vlan_rx_add(struct net_device *, u16);
898e586137SJiri Pirko static int qlcnic_vlan_rx_del(struct net_device *, u16);
90aa43c215SJeff Kirsher 
91bff57d8eSSony Chacko #define QLCNIC_IS_TSO_CAPABLE(adapter)	\
92bff57d8eSSony Chacko 	((adapter)->ahw->capabilities & QLCNIC_FW_CAPABILITY_TSO)
93bff57d8eSSony Chacko 
9413159183SSony Chacko static u32 qlcnic_vlan_tx_check(struct qlcnic_adapter *adapter)
9513159183SSony Chacko {
9613159183SSony Chacko 	struct qlcnic_hardware_context *ahw = adapter->ahw;
9713159183SSony Chacko 
9813159183SSony Chacko 	if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X)
9913159183SSony Chacko 		return ahw->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX;
10013159183SSony Chacko 	else
10113159183SSony Chacko 		return 1;
10213159183SSony Chacko }
10313159183SSony Chacko 
104aa43c215SJeff Kirsher /*  PCI Device ID Table  */
105aa43c215SJeff Kirsher #define ENTRY(device) \
106aa43c215SJeff Kirsher 	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
107aa43c215SJeff Kirsher 	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
108aa43c215SJeff Kirsher 
109aa43c215SJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
110aa43c215SJeff Kirsher 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
11113159183SSony Chacko 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE834X),
112aa43c215SJeff Kirsher 	{0,}
113aa43c215SJeff Kirsher };
114aa43c215SJeff Kirsher 
115aa43c215SJeff Kirsher MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116aa43c215SJeff Kirsher 
117aa43c215SJeff Kirsher 
1185ad6ff9dSSony Chacko inline void qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring *tx_ring)
119aa43c215SJeff Kirsher {
120aa43c215SJeff Kirsher 	writel(tx_ring->producer, tx_ring->crb_cmd_producer);
121aa43c215SJeff Kirsher }
122aa43c215SJeff Kirsher 
123aa43c215SJeff Kirsher static const u32 msi_tgt_status[8] = {
124aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
125aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
126aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
127aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
128aa43c215SJeff Kirsher };
129aa43c215SJeff Kirsher 
1307e2cf4feSSony Chacko static const u32 qlcnic_reg_tbl[] = {
1317e2cf4feSSony Chacko 	0x1B20A8,	/* PEG_HALT_STAT1 */
1327e2cf4feSSony Chacko 	0x1B20AC,	/* PEG_HALT_STAT2 */
1337e2cf4feSSony Chacko 	0x1B20B0,	/* FW_HEARTBEAT */
1347e2cf4feSSony Chacko 	0x1B2100,	/* LOCK ID */
1357e2cf4feSSony Chacko 	0x1B2128,	/* FW_CAPABILITIES */
1367e2cf4feSSony Chacko 	0x1B2138,	/* drv active */
1377e2cf4feSSony Chacko 	0x1B2140,	/* dev state */
1387e2cf4feSSony Chacko 	0x1B2144,	/* drv state */
1397e2cf4feSSony Chacko 	0x1B2148,	/* drv scratch */
1407e2cf4feSSony Chacko 	0x1B214C,	/* dev partition info */
1417e2cf4feSSony Chacko 	0x1B2174,	/* drv idc ver */
1427e2cf4feSSony Chacko 	0x1B2150,	/* fw version major */
1437e2cf4feSSony Chacko 	0x1B2154,	/* fw version minor */
1447e2cf4feSSony Chacko 	0x1B2158,	/* fw version sub */
1457e2cf4feSSony Chacko 	0x1B219C,	/* npar state */
1467e2cf4feSSony Chacko 	0x1B21FC,	/* FW_IMG_VALID */
1477e2cf4feSSony Chacko 	0x1B2250,	/* CMD_PEG_STATE */
1487e2cf4feSSony Chacko 	0x1B233C,	/* RCV_PEG_STATE */
1497e2cf4feSSony Chacko 	0x1B23B4,	/* ASIC TEMP */
1507e2cf4feSSony Chacko 	0x1B216C,	/* FW api */
1517e2cf4feSSony Chacko 	0x1B2170,	/* drv op mode */
1527e2cf4feSSony Chacko 	0x13C010,	/* flash lock */
1537e2cf4feSSony Chacko 	0x13C014,	/* flash unlock */
1547e2cf4feSSony Chacko };
1557e2cf4feSSony Chacko 
15622999798SSony Chacko static const struct qlcnic_board_info qlcnic_boards[] = {
15722999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x203,
15822999798SSony Chacko 	 "8200 Series Single Port 10GbE Converged Network Adapter"
15922999798SSony Chacko 	 "(TCP/IP Networking)"},
16022999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x207,
16122999798SSony Chacko 	 "8200 Series Dual Port 10GbE Converged Network Adapter"
16222999798SSony Chacko 	 "(TCP/IP Networking)"},
16322999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x20b,
16422999798SSony Chacko 	 "3200 Series Dual Port 10Gb Intelligent Ethernet Adapter"},
16522999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x20c,
16622999798SSony Chacko 	 "3200 Series Quad Port 1Gb Intelligent Ethernet Adapter"},
16722999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x20f,
16822999798SSony Chacko 	 "3200 Series Single Port 10Gb Intelligent Ethernet Adapter"},
16922999798SSony Chacko 	{0x1077, 0x8020, 0x103c, 0x3733,
17022999798SSony Chacko 	 "NC523SFP 10Gb 2-port Server Adapter"},
17122999798SSony Chacko 	{0x1077, 0x8020, 0x103c, 0x3346,
17222999798SSony Chacko 	 "CN1000Q Dual Port Converged Network Adapter"},
17322999798SSony Chacko 	{0x1077, 0x8020, 0x1077, 0x210,
17422999798SSony Chacko 	 "QME8242-k 10GbE Dual Port Mezzanine Card"},
17522999798SSony Chacko 	{0x1077, 0x8020, 0x0, 0x0, "cLOM8214 1/10GbE Controller"},
17622999798SSony Chacko };
17722999798SSony Chacko 
17822999798SSony Chacko #define NUM_SUPPORTED_BOARDS ARRAY_SIZE(qlcnic_boards)
179319ecf12SSony Chacko #define QLC_MAX_SDS_RINGS	8
18022999798SSony Chacko 
181aa43c215SJeff Kirsher static const
182aa43c215SJeff Kirsher struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
183aa43c215SJeff Kirsher 
184c70001a9SSony Chacko int qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
185aa43c215SJeff Kirsher {
186aa43c215SJeff Kirsher 	int size = sizeof(struct qlcnic_host_sds_ring) * count;
187aa43c215SJeff Kirsher 
188aa43c215SJeff Kirsher 	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
189aa43c215SJeff Kirsher 
190aa43c215SJeff Kirsher 	return recv_ctx->sds_rings == NULL;
191aa43c215SJeff Kirsher }
192aa43c215SJeff Kirsher 
193c70001a9SSony Chacko void qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
194aa43c215SJeff Kirsher {
195aa43c215SJeff Kirsher 	if (recv_ctx->sds_rings != NULL)
196aa43c215SJeff Kirsher 		kfree(recv_ctx->sds_rings);
197aa43c215SJeff Kirsher 
198aa43c215SJeff Kirsher 	recv_ctx->sds_rings = NULL;
199aa43c215SJeff Kirsher }
200aa43c215SJeff Kirsher 
201aa43c215SJeff Kirsher static int
202aa43c215SJeff Kirsher qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
203aa43c215SJeff Kirsher {
204aa43c215SJeff Kirsher 	u8 mac_addr[ETH_ALEN];
205aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
206aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
207aa43c215SJeff Kirsher 
208aa43c215SJeff Kirsher 	if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
209aa43c215SJeff Kirsher 		return -EIO;
210aa43c215SJeff Kirsher 
211aa43c215SJeff Kirsher 	memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
212aa43c215SJeff Kirsher 	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
213aa43c215SJeff Kirsher 
214aa43c215SJeff Kirsher 	/* set station address */
215aa43c215SJeff Kirsher 
216aaeb6cdfSJiri Pirko 	if (!is_valid_ether_addr(netdev->dev_addr))
217aa43c215SJeff Kirsher 		dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
218aa43c215SJeff Kirsher 					netdev->dev_addr);
219aa43c215SJeff Kirsher 
220aa43c215SJeff Kirsher 	return 0;
221aa43c215SJeff Kirsher }
222aa43c215SJeff Kirsher 
223aa43c215SJeff Kirsher static int qlcnic_set_mac(struct net_device *netdev, void *p)
224aa43c215SJeff Kirsher {
225aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
226aa43c215SJeff Kirsher 	struct sockaddr *addr = p;
227aa43c215SJeff Kirsher 
228aa43c215SJeff Kirsher 	if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
229aa43c215SJeff Kirsher 		return -EOPNOTSUPP;
230aa43c215SJeff Kirsher 
231aa43c215SJeff Kirsher 	if (!is_valid_ether_addr(addr->sa_data))
23213159183SSony Chacko 		return -EINVAL;
233aa43c215SJeff Kirsher 
234aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
235aa43c215SJeff Kirsher 		netif_device_detach(netdev);
236aa43c215SJeff Kirsher 		qlcnic_napi_disable(adapter);
237aa43c215SJeff Kirsher 	}
238aa43c215SJeff Kirsher 
239aa43c215SJeff Kirsher 	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
240aa43c215SJeff Kirsher 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
241aa43c215SJeff Kirsher 	qlcnic_set_multi(adapter->netdev);
242aa43c215SJeff Kirsher 
243aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
244aa43c215SJeff Kirsher 		netif_device_attach(netdev);
245aa43c215SJeff Kirsher 		qlcnic_napi_enable(adapter);
246aa43c215SJeff Kirsher 	}
247aa43c215SJeff Kirsher 	return 0;
248aa43c215SJeff Kirsher }
249aa43c215SJeff Kirsher 
2501690be63SVlad Yasevich static int qlcnic_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
2511690be63SVlad Yasevich 			struct net_device *netdev, const unsigned char *addr)
252fe1adc6bSJitendra Kalsaria {
253fe1adc6bSJitendra Kalsaria 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
254fe1adc6bSJitendra Kalsaria 	int err = -EOPNOTSUPP;
255fe1adc6bSJitendra Kalsaria 
2563e5c112fSVlad Yasevich 	if (!adapter->fdb_mac_learn)
2573e5c112fSVlad Yasevich 		return ndo_dflt_fdb_del(ndm, tb, netdev, addr);
258fe1adc6bSJitendra Kalsaria 
259fe1adc6bSJitendra Kalsaria 	if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
260fe1adc6bSJitendra Kalsaria 		if (is_unicast_ether_addr(addr))
261fe1adc6bSJitendra Kalsaria 			err = qlcnic_nic_del_mac(adapter, addr);
262fe1adc6bSJitendra Kalsaria 		else if (is_multicast_ether_addr(addr))
263fe1adc6bSJitendra Kalsaria 			err = dev_mc_del(netdev, addr);
264fe1adc6bSJitendra Kalsaria 		else
265fe1adc6bSJitendra Kalsaria 			err =  -EINVAL;
266fe1adc6bSJitendra Kalsaria 	}
267fe1adc6bSJitendra Kalsaria 	return err;
268fe1adc6bSJitendra Kalsaria }
269fe1adc6bSJitendra Kalsaria 
270fe1adc6bSJitendra Kalsaria static int qlcnic_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
271fe1adc6bSJitendra Kalsaria 			struct net_device *netdev,
272fe1adc6bSJitendra Kalsaria 			const unsigned char *addr, u16 flags)
273fe1adc6bSJitendra Kalsaria {
274fe1adc6bSJitendra Kalsaria 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
275fe1adc6bSJitendra Kalsaria 	int err = 0;
276fe1adc6bSJitendra Kalsaria 
2773e5c112fSVlad Yasevich 	if (!adapter->fdb_mac_learn)
2783e5c112fSVlad Yasevich 		return ndo_dflt_fdb_add(ndm, tb, netdev, addr, flags);
279fe1adc6bSJitendra Kalsaria 
280fe1adc6bSJitendra Kalsaria 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
281fe1adc6bSJitendra Kalsaria 		pr_info("%s: FDB e-switch is not enabled\n", __func__);
282fe1adc6bSJitendra Kalsaria 		return -EOPNOTSUPP;
283fe1adc6bSJitendra Kalsaria 	}
284fe1adc6bSJitendra Kalsaria 
285fe1adc6bSJitendra Kalsaria 	if (ether_addr_equal(addr, adapter->mac_addr))
286fe1adc6bSJitendra Kalsaria 		return err;
287fe1adc6bSJitendra Kalsaria 
288fe1adc6bSJitendra Kalsaria 	if (is_unicast_ether_addr(addr))
289fe1adc6bSJitendra Kalsaria 		err = qlcnic_nic_add_mac(adapter, addr);
290fe1adc6bSJitendra Kalsaria 	else if (is_multicast_ether_addr(addr))
291fe1adc6bSJitendra Kalsaria 		err = dev_mc_add_excl(netdev, addr);
292fe1adc6bSJitendra Kalsaria 	else
293fe1adc6bSJitendra Kalsaria 		err = -EINVAL;
294fe1adc6bSJitendra Kalsaria 
295fe1adc6bSJitendra Kalsaria 	return err;
296fe1adc6bSJitendra Kalsaria }
297fe1adc6bSJitendra Kalsaria 
298fe1adc6bSJitendra Kalsaria static int qlcnic_fdb_dump(struct sk_buff *skb, struct netlink_callback *ncb,
299fe1adc6bSJitendra Kalsaria 			struct net_device *netdev, int idx)
300fe1adc6bSJitendra Kalsaria {
301fe1adc6bSJitendra Kalsaria 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
302fe1adc6bSJitendra Kalsaria 
3033e5c112fSVlad Yasevich 	if (!adapter->fdb_mac_learn)
3043e5c112fSVlad Yasevich 		return ndo_dflt_fdb_dump(skb, ncb, netdev, idx);
305fe1adc6bSJitendra Kalsaria 
306fe1adc6bSJitendra Kalsaria 	if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
307fe1adc6bSJitendra Kalsaria 		idx = ndo_dflt_fdb_dump(skb, ncb, netdev, idx);
308fe1adc6bSJitendra Kalsaria 
309fe1adc6bSJitendra Kalsaria 	return idx;
310fe1adc6bSJitendra Kalsaria }
311fe1adc6bSJitendra Kalsaria 
3127e2cf4feSSony Chacko static void qlcnic_82xx_cancel_idc_work(struct qlcnic_adapter *adapter)
3137e2cf4feSSony Chacko {
3147e2cf4feSSony Chacko 	while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3157e2cf4feSSony Chacko 		usleep_range(10000, 11000);
3167e2cf4feSSony Chacko 
3177e2cf4feSSony Chacko 	cancel_delayed_work_sync(&adapter->fw_work);
3187e2cf4feSSony Chacko }
3197e2cf4feSSony Chacko 
320aa43c215SJeff Kirsher static const struct net_device_ops qlcnic_netdev_ops = {
321aa43c215SJeff Kirsher 	.ndo_open	   = qlcnic_open,
322aa43c215SJeff Kirsher 	.ndo_stop	   = qlcnic_close,
323aa43c215SJeff Kirsher 	.ndo_start_xmit    = qlcnic_xmit_frame,
324aa43c215SJeff Kirsher 	.ndo_get_stats	   = qlcnic_get_stats,
325aa43c215SJeff Kirsher 	.ndo_validate_addr = eth_validate_addr,
326afc4b13dSJiri Pirko 	.ndo_set_rx_mode   = qlcnic_set_multi,
327aa43c215SJeff Kirsher 	.ndo_set_mac_address    = qlcnic_set_mac,
328aa43c215SJeff Kirsher 	.ndo_change_mtu	   = qlcnic_change_mtu,
329aa43c215SJeff Kirsher 	.ndo_fix_features  = qlcnic_fix_features,
330aa43c215SJeff Kirsher 	.ndo_set_features  = qlcnic_set_features,
331aa43c215SJeff Kirsher 	.ndo_tx_timeout	   = qlcnic_tx_timeout,
332aa43c215SJeff Kirsher 	.ndo_vlan_rx_add_vid	= qlcnic_vlan_rx_add,
333aa43c215SJeff Kirsher 	.ndo_vlan_rx_kill_vid	= qlcnic_vlan_rx_del,
334fe1adc6bSJitendra Kalsaria 	.ndo_fdb_add		= qlcnic_fdb_add,
335fe1adc6bSJitendra Kalsaria 	.ndo_fdb_del		= qlcnic_fdb_del,
336fe1adc6bSJitendra Kalsaria 	.ndo_fdb_dump		= qlcnic_fdb_dump,
337aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
338aa43c215SJeff Kirsher 	.ndo_poll_controller = qlcnic_poll_controller,
339aa43c215SJeff Kirsher #endif
340aa43c215SJeff Kirsher };
341aa43c215SJeff Kirsher 
342b43e5ee7SSucheta Chakraborty static const struct net_device_ops qlcnic_netdev_failed_ops = {
343b43e5ee7SSucheta Chakraborty 	.ndo_open	   = qlcnic_open,
344b43e5ee7SSucheta Chakraborty };
345b43e5ee7SSucheta Chakraborty 
346aa43c215SJeff Kirsher static struct qlcnic_nic_template qlcnic_ops = {
347aa43c215SJeff Kirsher 	.config_bridged_mode	= qlcnic_config_bridged_mode,
3487e2cf4feSSony Chacko 	.config_led		= qlcnic_82xx_config_led,
3497e2cf4feSSony Chacko 	.start_firmware		= qlcnic_82xx_start_firmware,
3507e2cf4feSSony Chacko 	.request_reset		= qlcnic_82xx_dev_request_reset,
3517e2cf4feSSony Chacko 	.cancel_idc_work	= qlcnic_82xx_cancel_idc_work,
3527e2cf4feSSony Chacko 	.napi_add		= qlcnic_82xx_napi_add,
3534be41e92SSony Chacko 	.napi_del		= qlcnic_82xx_napi_del,
3547e2cf4feSSony Chacko 	.config_ipaddr		= qlcnic_82xx_config_ipaddr,
3557e2cf4feSSony Chacko 	.clear_legacy_intr	= qlcnic_82xx_clear_legacy_intr,
356aa43c215SJeff Kirsher };
357aa43c215SJeff Kirsher 
3587e2cf4feSSony Chacko struct qlcnic_nic_template qlcnic_vf_ops = {
359aa43c215SJeff Kirsher 	.config_bridged_mode	= qlcnicvf_config_bridged_mode,
360aa43c215SJeff Kirsher 	.config_led		= qlcnicvf_config_led,
361aa43c215SJeff Kirsher 	.start_firmware		= qlcnicvf_start_firmware
362aa43c215SJeff Kirsher };
363aa43c215SJeff Kirsher 
3647e2cf4feSSony Chacko static struct qlcnic_hardware_ops qlcnic_hw_ops = {
3657e2cf4feSSony Chacko 	.read_crb			= qlcnic_82xx_read_crb,
3667e2cf4feSSony Chacko 	.write_crb			= qlcnic_82xx_write_crb,
3677e2cf4feSSony Chacko 	.read_reg			= qlcnic_82xx_hw_read_wx_2M,
3687e2cf4feSSony Chacko 	.write_reg			= qlcnic_82xx_hw_write_wx_2M,
3697e2cf4feSSony Chacko 	.get_mac_address		= qlcnic_82xx_get_mac_address,
3707e2cf4feSSony Chacko 	.setup_intr			= qlcnic_82xx_setup_intr,
3717e2cf4feSSony Chacko 	.alloc_mbx_args			= qlcnic_82xx_alloc_mbx_args,
3727e2cf4feSSony Chacko 	.mbx_cmd			= qlcnic_82xx_issue_cmd,
3737e2cf4feSSony Chacko 	.get_func_no			= qlcnic_82xx_get_func_no,
3747e2cf4feSSony Chacko 	.api_lock			= qlcnic_82xx_api_lock,
3757e2cf4feSSony Chacko 	.api_unlock			= qlcnic_82xx_api_unlock,
3767e2cf4feSSony Chacko 	.add_sysfs			= qlcnic_82xx_add_sysfs,
3777e2cf4feSSony Chacko 	.remove_sysfs			= qlcnic_82xx_remove_sysfs,
3787e2cf4feSSony Chacko 	.process_lb_rcv_ring_diag	= qlcnic_82xx_process_rcv_ring_diag,
3797e2cf4feSSony Chacko 	.create_rx_ctx			= qlcnic_82xx_fw_cmd_create_rx_ctx,
3807e2cf4feSSony Chacko 	.create_tx_ctx			= qlcnic_82xx_fw_cmd_create_tx_ctx,
3817e2cf4feSSony Chacko 	.setup_link_event		= qlcnic_82xx_linkevent_request,
3827e2cf4feSSony Chacko 	.get_nic_info			= qlcnic_82xx_get_nic_info,
3837e2cf4feSSony Chacko 	.get_pci_info			= qlcnic_82xx_get_pci_info,
3847e2cf4feSSony Chacko 	.set_nic_info			= qlcnic_82xx_set_nic_info,
3857e2cf4feSSony Chacko 	.change_macvlan			= qlcnic_82xx_sre_macaddr_change,
3867e2cf4feSSony Chacko 	.napi_enable			= qlcnic_82xx_napi_enable,
3877e2cf4feSSony Chacko 	.napi_disable			= qlcnic_82xx_napi_disable,
3887e2cf4feSSony Chacko 	.config_intr_coal		= qlcnic_82xx_config_intr_coalesce,
3897e2cf4feSSony Chacko 	.config_rss			= qlcnic_82xx_config_rss,
3907e2cf4feSSony Chacko 	.config_hw_lro			= qlcnic_82xx_config_hw_lro,
3917e2cf4feSSony Chacko 	.config_loopback		= qlcnic_82xx_set_lb_mode,
3927e2cf4feSSony Chacko 	.clear_loopback			= qlcnic_82xx_clear_lb_mode,
3937e2cf4feSSony Chacko 	.config_promisc_mode		= qlcnic_82xx_nic_set_promisc,
3947e2cf4feSSony Chacko 	.change_l2_filter		= qlcnic_82xx_change_filter,
3957e2cf4feSSony Chacko 	.get_board_info			= qlcnic_82xx_get_board_info,
3967e2cf4feSSony Chacko };
3977e2cf4feSSony Chacko 
3987f966452SSony Chacko int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
399aa43c215SJeff Kirsher {
400aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
40113159183SSony Chacko 	int err = -1, i;
40213159183SSony Chacko 	int max_tx_rings;
40313159183SSony Chacko 
40413159183SSony Chacko 	if (!adapter->msix_entries) {
40513159183SSony Chacko 		adapter->msix_entries = kcalloc(num_msix,
40613159183SSony Chacko 						sizeof(struct msix_entry),
40713159183SSony Chacko 						GFP_KERNEL);
408b2adaca9SJoe Perches 		if (!adapter->msix_entries)
40913159183SSony Chacko 			return -ENOMEM;
41013159183SSony Chacko 	}
411aa43c215SJeff Kirsher 
412aa43c215SJeff Kirsher 	adapter->max_sds_rings = 1;
413aa43c215SJeff Kirsher 	adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
414aa43c215SJeff Kirsher 
41579788450SSony Chacko 	if (adapter->ahw->msix_supported) {
416aa43c215SJeff Kirsher  enable_msix:
41713159183SSony Chacko 		for (i = 0; i < num_msix; i++)
41813159183SSony Chacko 			adapter->msix_entries[i].entry = i;
419aa43c215SJeff Kirsher 		err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
420aa43c215SJeff Kirsher 		if (err == 0) {
421aa43c215SJeff Kirsher 			adapter->flags |= QLCNIC_MSIX_ENABLED;
42213159183SSony Chacko 			if (qlcnic_83xx_check(adapter)) {
42313159183SSony Chacko 				adapter->ahw->num_msix = num_msix;
42413159183SSony Chacko 				/* subtract mail box and tx ring vectors */
42513159183SSony Chacko 				max_tx_rings = adapter->max_drv_tx_rings;
42613159183SSony Chacko 				adapter->max_sds_rings = num_msix -
42713159183SSony Chacko 							 max_tx_rings - 1;
42813159183SSony Chacko 			} else {
429aa43c215SJeff Kirsher 				adapter->max_sds_rings = num_msix;
43013159183SSony Chacko 			}
431aa43c215SJeff Kirsher 			dev_info(&pdev->dev, "using msi-x interrupts\n");
432aa43c215SJeff Kirsher 			return err;
43313159183SSony Chacko 		} else if (err > 0) {
43413159183SSony Chacko 			dev_info(&pdev->dev,
43513159183SSony Chacko 				 "Unable to allocate %d MSI-X interrupt vectors\n",
43613159183SSony Chacko 				 num_msix);
43713159183SSony Chacko 			if (qlcnic_83xx_check(adapter)) {
43813159183SSony Chacko 				if (err < QLC_83XX_MINIMUM_VECTOR)
43913159183SSony Chacko 					return err;
44013159183SSony Chacko 				err -= (adapter->max_drv_tx_rings + 1);
441aa43c215SJeff Kirsher 				num_msix = rounddown_pow_of_two(err);
44213159183SSony Chacko 				num_msix += (adapter->max_drv_tx_rings + 1);
44313159183SSony Chacko 			} else {
44413159183SSony Chacko 				num_msix = rounddown_pow_of_two(err);
44513159183SSony Chacko 			}
44613159183SSony Chacko 
44713159183SSony Chacko 			if (num_msix) {
44813159183SSony Chacko 				dev_info(&pdev->dev,
449dce056cbSManish Chopra 					 "Trying to allocate %d MSI-X interrupt vectors\n",
45013159183SSony Chacko 					 num_msix);
451aa43c215SJeff Kirsher 				goto enable_msix;
452aa43c215SJeff Kirsher 			}
45313159183SSony Chacko 		} else {
454dce056cbSManish Chopra 			dev_info(&pdev->dev,
455dce056cbSManish Chopra 				 "Unable to allocate %d MSI-X interrupt vectors\n",
45613159183SSony Chacko 				 num_msix);
457aa43c215SJeff Kirsher 		}
45813159183SSony Chacko 	}
45913159183SSony Chacko 
460aa43c215SJeff Kirsher 	return err;
461aa43c215SJeff Kirsher }
462aa43c215SJeff Kirsher 
4639a97e705SManish chopra static int qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
464aa43c215SJeff Kirsher {
4659a97e705SManish chopra 	int err = 0;
46615087c2bSSony Chacko 	u32 offset, mask_reg;
467aa43c215SJeff Kirsher 	const struct qlcnic_legacy_intr_set *legacy_intrp;
46815087c2bSSony Chacko 	struct qlcnic_hardware_context *ahw = adapter->ahw;
469aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
470aa43c215SJeff Kirsher 
471099f7aa7SSony Chacko 	if (qlcnic_use_msi && !pci_enable_msi(pdev)) {
472aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MSI_ENABLED;
47315087c2bSSony Chacko 		offset = msi_tgt_status[adapter->ahw->pci_func];
47415087c2bSSony Chacko 		adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter->ahw,
47515087c2bSSony Chacko 							    offset);
476aa43c215SJeff Kirsher 		dev_info(&pdev->dev, "using msi interrupts\n");
477aa43c215SJeff Kirsher 		adapter->msix_entries[0].vector = pdev->irq;
4789a97e705SManish chopra 		return err;
479aa43c215SJeff Kirsher 	}
4809a97e705SManish chopra 	if (qlcnic_use_msi || qlcnic_use_msi_x)
4819a97e705SManish chopra 		return -EOPNOTSUPP;
482aa43c215SJeff Kirsher 
483aa43c215SJeff Kirsher 	legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
48479788450SSony Chacko 	adapter->ahw->int_vec_bit = legacy_intrp->int_vec_bit;
48515087c2bSSony Chacko 	offset = legacy_intrp->tgt_status_reg;
48615087c2bSSony Chacko 	adapter->tgt_status_reg = qlcnic_get_ioaddr(ahw, offset);
48715087c2bSSony Chacko 	mask_reg = legacy_intrp->tgt_mask_reg;
48815087c2bSSony Chacko 	adapter->tgt_mask_reg = qlcnic_get_ioaddr(ahw, mask_reg);
48915087c2bSSony Chacko 	adapter->isr_int_vec = qlcnic_get_ioaddr(ahw, ISR_INT_VECTOR);
49015087c2bSSony Chacko 	adapter->crb_int_state_reg = qlcnic_get_ioaddr(ahw, ISR_INT_STATE_REG);
491aa43c215SJeff Kirsher 	dev_info(&pdev->dev, "using legacy interrupts\n");
492aa43c215SJeff Kirsher 	adapter->msix_entries[0].vector = pdev->irq;
4939a97e705SManish chopra 	return err;
494aa43c215SJeff Kirsher }
495aa43c215SJeff Kirsher 
4967e2cf4feSSony Chacko int qlcnic_82xx_setup_intr(struct qlcnic_adapter *adapter, u8 num_intr)
497aa43c215SJeff Kirsher {
4989a97e705SManish chopra 	int num_msix, err = 0;
499aa43c215SJeff Kirsher 
5007e2cf4feSSony Chacko 	if (!num_intr)
5017e2cf4feSSony Chacko 		num_intr = QLCNIC_DEF_NUM_STS_DESC_RINGS;
5027e2cf4feSSony Chacko 
5037e2cf4feSSony Chacko 	if (adapter->ahw->msix_supported)
504aa43c215SJeff Kirsher 		num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
5057e2cf4feSSony Chacko 						num_intr));
5067e2cf4feSSony Chacko 	else
507aa43c215SJeff Kirsher 		num_msix = 1;
508aa43c215SJeff Kirsher 
5097e2cf4feSSony Chacko 	err = qlcnic_enable_msix(adapter, num_msix);
5107e2cf4feSSony Chacko 	if (err == -ENOMEM || !err)
5117e2cf4feSSony Chacko 		return err;
512aa43c215SJeff Kirsher 
5139a97e705SManish chopra 	err = qlcnic_enable_msi_legacy(adapter);
5149a97e705SManish chopra 	if (!err)
5159a97e705SManish chopra 		return err;
5169a97e705SManish chopra 
5179a97e705SManish chopra 	return -EIO;
518aa43c215SJeff Kirsher }
51913159183SSony Chacko 
520319ecf12SSony Chacko void qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
521aa43c215SJeff Kirsher {
522aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
523aa43c215SJeff Kirsher 		pci_disable_msix(adapter->pdev);
524aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSI_ENABLED)
525aa43c215SJeff Kirsher 		pci_disable_msi(adapter->pdev);
52613159183SSony Chacko 
52713159183SSony Chacko 	kfree(adapter->msix_entries);
52813159183SSony Chacko 	adapter->msix_entries = NULL;
52913159183SSony Chacko 
53013159183SSony Chacko 	if (adapter->ahw->intr_tbl) {
53113159183SSony Chacko 		vfree(adapter->ahw->intr_tbl);
53213159183SSony Chacko 		adapter->ahw->intr_tbl = NULL;
53313159183SSony Chacko 	}
534aa43c215SJeff Kirsher }
535aa43c215SJeff Kirsher 
536aa43c215SJeff Kirsher static void
537aa43c215SJeff Kirsher qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
538aa43c215SJeff Kirsher {
539aa43c215SJeff Kirsher 	if (adapter->ahw->pci_base0 != NULL)
540aa43c215SJeff Kirsher 		iounmap(adapter->ahw->pci_base0);
541aa43c215SJeff Kirsher }
542aa43c215SJeff Kirsher 
5437e2cf4feSSony Chacko static int qlcnic_get_act_pci_func(struct qlcnic_adapter *adapter)
5447e2cf4feSSony Chacko {
5457e2cf4feSSony Chacko 	struct qlcnic_pci_info *pci_info;
5467e2cf4feSSony Chacko 	int ret;
5477e2cf4feSSony Chacko 
5487e2cf4feSSony Chacko 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
5497e2cf4feSSony Chacko 		switch (adapter->ahw->port_type) {
5507e2cf4feSSony Chacko 		case QLCNIC_GBE:
5517e2cf4feSSony Chacko 			adapter->ahw->act_pci_func = QLCNIC_NIU_MAX_GBE_PORTS;
5527e2cf4feSSony Chacko 			break;
5537e2cf4feSSony Chacko 		case QLCNIC_XGBE:
5547e2cf4feSSony Chacko 			adapter->ahw->act_pci_func = QLCNIC_NIU_MAX_XG_PORTS;
5557e2cf4feSSony Chacko 			break;
5567e2cf4feSSony Chacko 		}
5577e2cf4feSSony Chacko 		return 0;
5587e2cf4feSSony Chacko 	}
5597e2cf4feSSony Chacko 
5607e2cf4feSSony Chacko 	if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
5617e2cf4feSSony Chacko 		return 0;
5627e2cf4feSSony Chacko 
5637e2cf4feSSony Chacko 	pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
5647e2cf4feSSony Chacko 	if (!pci_info)
5657e2cf4feSSony Chacko 		return -ENOMEM;
5667e2cf4feSSony Chacko 
5677e2cf4feSSony Chacko 	ret = qlcnic_get_pci_info(adapter, pci_info);
5687e2cf4feSSony Chacko 	kfree(pci_info);
5697e2cf4feSSony Chacko 	return ret;
5707e2cf4feSSony Chacko }
571d71170fbSSony Chacko 
572d71170fbSSony Chacko int qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
573aa43c215SJeff Kirsher {
574aa43c215SJeff Kirsher 	struct qlcnic_pci_info *pci_info;
575bff57d8eSSony Chacko 	int i, ret = 0, j = 0;
576bff57d8eSSony Chacko 	u16 act_pci_func;
577aa43c215SJeff Kirsher 	u8 pfn;
578aa43c215SJeff Kirsher 
579aa43c215SJeff Kirsher 	pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
580aa43c215SJeff Kirsher 	if (!pci_info)
581aa43c215SJeff Kirsher 		return -ENOMEM;
582aa43c215SJeff Kirsher 
583bff57d8eSSony Chacko 	ret = qlcnic_get_pci_info(adapter, pci_info);
584bff57d8eSSony Chacko 	if (ret)
585bff57d8eSSony Chacko 		goto err_pci_info;
586bff57d8eSSony Chacko 
587bff57d8eSSony Chacko 	act_pci_func = adapter->ahw->act_pci_func;
588bff57d8eSSony Chacko 
589aa43c215SJeff Kirsher 	adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
590bff57d8eSSony Chacko 				 act_pci_func, GFP_KERNEL);
591aa43c215SJeff Kirsher 	if (!adapter->npars) {
592aa43c215SJeff Kirsher 		ret = -ENOMEM;
593aa43c215SJeff Kirsher 		goto err_pci_info;
594aa43c215SJeff Kirsher 	}
595aa43c215SJeff Kirsher 
596aa43c215SJeff Kirsher 	adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
597aa43c215SJeff Kirsher 				QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
598aa43c215SJeff Kirsher 	if (!adapter->eswitch) {
599aa43c215SJeff Kirsher 		ret = -ENOMEM;
600aa43c215SJeff Kirsher 		goto err_npars;
601aa43c215SJeff Kirsher 	}
602aa43c215SJeff Kirsher 
603aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
604aa43c215SJeff Kirsher 		pfn = pci_info[i].id;
605bff57d8eSSony Chacko 
6060f6efff9SDan Carpenter 		if (pfn >= QLCNIC_MAX_PCI_FUNC) {
607aa43c215SJeff Kirsher 			ret = QL_STATUS_INVALID_PARAM;
608aa43c215SJeff Kirsher 			goto err_eswitch;
609aa43c215SJeff Kirsher 		}
610bff57d8eSSony Chacko 
611bff57d8eSSony Chacko 		if (!pci_info[i].active ||
612bff57d8eSSony Chacko 		    (pci_info[i].type != QLCNIC_TYPE_NIC))
613bff57d8eSSony Chacko 			continue;
614bff57d8eSSony Chacko 
615bff57d8eSSony Chacko 		adapter->npars[j].pci_func = pfn;
616bff57d8eSSony Chacko 		adapter->npars[j].active = (u8)pci_info[i].active;
617bff57d8eSSony Chacko 		adapter->npars[j].type = (u8)pci_info[i].type;
618bff57d8eSSony Chacko 		adapter->npars[j].phy_port = (u8)pci_info[i].default_port;
619bff57d8eSSony Chacko 		adapter->npars[j].min_bw = pci_info[i].tx_min_bw;
620bff57d8eSSony Chacko 		adapter->npars[j].max_bw = pci_info[i].tx_max_bw;
621bff57d8eSSony Chacko 		j++;
622aa43c215SJeff Kirsher 	}
623aa43c215SJeff Kirsher 
62413159183SSony Chacko 	for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++) {
625aa43c215SJeff Kirsher 		adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
62613159183SSony Chacko 		if (qlcnic_83xx_check(adapter))
62713159183SSony Chacko 			qlcnic_enable_eswitch(adapter, i, 1);
62813159183SSony Chacko 	}
629aa43c215SJeff Kirsher 
630aa43c215SJeff Kirsher 	kfree(pci_info);
631aa43c215SJeff Kirsher 	return 0;
632aa43c215SJeff Kirsher 
633aa43c215SJeff Kirsher err_eswitch:
634aa43c215SJeff Kirsher 	kfree(adapter->eswitch);
635aa43c215SJeff Kirsher 	adapter->eswitch = NULL;
636aa43c215SJeff Kirsher err_npars:
637aa43c215SJeff Kirsher 	kfree(adapter->npars);
638aa43c215SJeff Kirsher 	adapter->npars = NULL;
639aa43c215SJeff Kirsher err_pci_info:
640aa43c215SJeff Kirsher 	kfree(pci_info);
641aa43c215SJeff Kirsher 
642aa43c215SJeff Kirsher 	return ret;
643aa43c215SJeff Kirsher }
644aa43c215SJeff Kirsher 
645aa43c215SJeff Kirsher static int
646aa43c215SJeff Kirsher qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
647aa43c215SJeff Kirsher {
648aa43c215SJeff Kirsher 	u8 id;
649aa43c215SJeff Kirsher 	int i, ret = 1;
650aa43c215SJeff Kirsher 	u32 data = QLCNIC_MGMT_FUNC;
651bff57d8eSSony Chacko 	struct qlcnic_hardware_context *ahw = adapter->ahw;
652aa43c215SJeff Kirsher 
653aa43c215SJeff Kirsher 	ret = qlcnic_api_lock(adapter);
654aa43c215SJeff Kirsher 	if (ret)
655aa43c215SJeff Kirsher 		goto err_lock;
656aa43c215SJeff Kirsher 
657aa43c215SJeff Kirsher 	if (qlcnic_config_npars) {
658bff57d8eSSony Chacko 		for (i = 0; i < ahw->act_pci_func; i++) {
659bff57d8eSSony Chacko 			id = adapter->npars[i].pci_func;
660bff57d8eSSony Chacko 			if (id == ahw->pci_func)
661aa43c215SJeff Kirsher 				continue;
662aa43c215SJeff Kirsher 			data |= (qlcnic_config_npars &
663aa43c215SJeff Kirsher 					QLC_DEV_SET_DRV(0xf, id));
664aa43c215SJeff Kirsher 		}
665aa43c215SJeff Kirsher 	} else {
666a15ebd37SHimanshu Madhani 		data = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
667bff57d8eSSony Chacko 		data = (data & ~QLC_DEV_SET_DRV(0xf, ahw->pci_func)) |
668aa43c215SJeff Kirsher 			(QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
669bff57d8eSSony Chacko 					 ahw->pci_func));
670aa43c215SJeff Kirsher 	}
671a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_DRV_OP_MODE, data);
672aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
673aa43c215SJeff Kirsher err_lock:
674aa43c215SJeff Kirsher 	return ret;
675aa43c215SJeff Kirsher }
676aa43c215SJeff Kirsher 
67713159183SSony Chacko static void qlcnic_check_vf(struct qlcnic_adapter *adapter,
67813159183SSony Chacko 			    const struct pci_device_id *ent)
679aa43c215SJeff Kirsher {
680aa43c215SJeff Kirsher 	u32 op_mode, priv_level;
681aa43c215SJeff Kirsher 
682aa43c215SJeff Kirsher 	/* Determine FW API version */
68313159183SSony Chacko 	adapter->ahw->fw_hal_version = QLC_SHARED_REG_RD32(adapter,
684aa43c215SJeff Kirsher 							   QLCNIC_FW_API);
685aa43c215SJeff Kirsher 
686aa43c215SJeff Kirsher 	/* Find PCI function number */
68713159183SSony Chacko 	qlcnic_get_func_no(adapter);
688aa43c215SJeff Kirsher 
689aa43c215SJeff Kirsher 	/* Determine function privilege level */
69013159183SSony Chacko 	op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
691aa43c215SJeff Kirsher 	if (op_mode == QLC_DEV_DRV_DEFAULT)
692aa43c215SJeff Kirsher 		priv_level = QLCNIC_MGMT_FUNC;
693aa43c215SJeff Kirsher 	else
694aa43c215SJeff Kirsher 		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
695aa43c215SJeff Kirsher 
696aa43c215SJeff Kirsher 	if (priv_level == QLCNIC_NON_PRIV_FUNC) {
69779788450SSony Chacko 		adapter->ahw->op_mode = QLCNIC_NON_PRIV_FUNC;
698aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev,
699aa43c215SJeff Kirsher 			"HAL Version: %d Non Privileged function\n",
70079788450SSony Chacko 			 adapter->ahw->fw_hal_version);
701aa43c215SJeff Kirsher 		adapter->nic_ops = &qlcnic_vf_ops;
702aa43c215SJeff Kirsher 	} else
703aa43c215SJeff Kirsher 		adapter->nic_ops = &qlcnic_ops;
704aa43c215SJeff Kirsher }
705aa43c215SJeff Kirsher 
70615087c2bSSony Chacko #define QLCNIC_82XX_BAR0_LENGTH 0x00200000UL
70713159183SSony Chacko #define QLCNIC_83XX_BAR0_LENGTH 0x4000
70815087c2bSSony Chacko static void qlcnic_get_bar_length(u32 dev_id, ulong *bar)
709aa43c215SJeff Kirsher {
71015087c2bSSony Chacko 	switch (dev_id) {
71115087c2bSSony Chacko 	case PCI_DEVICE_ID_QLOGIC_QLE824X:
71215087c2bSSony Chacko 		*bar = QLCNIC_82XX_BAR0_LENGTH;
71315087c2bSSony Chacko 		break;
71413159183SSony Chacko 	case PCI_DEVICE_ID_QLOGIC_QLE834X:
71513159183SSony Chacko 		*bar = QLCNIC_83XX_BAR0_LENGTH;
71613159183SSony Chacko 		break;
71715087c2bSSony Chacko 	default:
71815087c2bSSony Chacko 		*bar = 0;
71915087c2bSSony Chacko 	}
72015087c2bSSony Chacko }
72115087c2bSSony Chacko 
72215087c2bSSony Chacko static int qlcnic_setup_pci_map(struct pci_dev *pdev,
72315087c2bSSony Chacko 				struct qlcnic_hardware_context *ahw)
72415087c2bSSony Chacko {
72515087c2bSSony Chacko 	u32 offset;
726aa43c215SJeff Kirsher 	void __iomem *mem_ptr0 = NULL;
72715087c2bSSony Chacko 	unsigned long mem_len, pci_len0 = 0, bar0_len;
728aa43c215SJeff Kirsher 
729aa43c215SJeff Kirsher 	/* remap phys address */
730aa43c215SJeff Kirsher 	mem_len = pci_resource_len(pdev, 0);
731aa43c215SJeff Kirsher 
73215087c2bSSony Chacko 	qlcnic_get_bar_length(pdev->device, &bar0_len);
73315087c2bSSony Chacko 	if (mem_len >= bar0_len) {
734aa43c215SJeff Kirsher 
735aa43c215SJeff Kirsher 		mem_ptr0 = pci_ioremap_bar(pdev, 0);
736aa43c215SJeff Kirsher 		if (mem_ptr0 == NULL) {
737aa43c215SJeff Kirsher 			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
738aa43c215SJeff Kirsher 			return -EIO;
739aa43c215SJeff Kirsher 		}
740aa43c215SJeff Kirsher 		pci_len0 = mem_len;
741aa43c215SJeff Kirsher 	} else {
742aa43c215SJeff Kirsher 		return -EIO;
743aa43c215SJeff Kirsher 	}
744aa43c215SJeff Kirsher 
745aa43c215SJeff Kirsher 	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
7467e2cf4feSSony Chacko 
74715087c2bSSony Chacko 	ahw->pci_base0 = mem_ptr0;
74815087c2bSSony Chacko 	ahw->pci_len0 = pci_len0;
74915087c2bSSony Chacko 	offset = QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(ahw->pci_func));
75015087c2bSSony Chacko 	qlcnic_get_ioaddr(ahw, offset);
751aa43c215SJeff Kirsher 
752aa43c215SJeff Kirsher 	return 0;
753aa43c215SJeff Kirsher }
754aa43c215SJeff Kirsher 
75522999798SSony Chacko static void qlcnic_get_board_name(struct qlcnic_adapter *adapter, char *name)
756aa43c215SJeff Kirsher {
757aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
758aa43c215SJeff Kirsher 	int i, found = 0;
759aa43c215SJeff Kirsher 
760aa43c215SJeff Kirsher 	for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
761aa43c215SJeff Kirsher 		if (qlcnic_boards[i].vendor == pdev->vendor &&
762aa43c215SJeff Kirsher 			qlcnic_boards[i].device == pdev->device &&
763aa43c215SJeff Kirsher 			qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
764aa43c215SJeff Kirsher 			qlcnic_boards[i].sub_device == pdev->subsystem_device) {
765aa43c215SJeff Kirsher 				sprintf(name, "%pM: %s" ,
766aa43c215SJeff Kirsher 					adapter->mac_addr,
767aa43c215SJeff Kirsher 					qlcnic_boards[i].short_name);
768aa43c215SJeff Kirsher 				found = 1;
769aa43c215SJeff Kirsher 				break;
770aa43c215SJeff Kirsher 		}
771aa43c215SJeff Kirsher 
772aa43c215SJeff Kirsher 	}
773aa43c215SJeff Kirsher 
774aa43c215SJeff Kirsher 	if (!found)
775aa43c215SJeff Kirsher 		sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
776aa43c215SJeff Kirsher }
777aa43c215SJeff Kirsher 
778aa43c215SJeff Kirsher static void
779aa43c215SJeff Kirsher qlcnic_check_options(struct qlcnic_adapter *adapter)
780aa43c215SJeff Kirsher {
781a15ebd37SHimanshu Madhani 	int err;
782aa43c215SJeff Kirsher 	u32 fw_major, fw_minor, fw_build, prev_fw_version;
783aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
784a15ebd37SHimanshu Madhani 	struct qlcnic_hardware_context *ahw = adapter->ahw;
785a15ebd37SHimanshu Madhani 	struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
786aa43c215SJeff Kirsher 
787aa43c215SJeff Kirsher 	prev_fw_version = adapter->fw_version;
788aa43c215SJeff Kirsher 
789a15ebd37SHimanshu Madhani 	fw_major = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MAJOR);
790a15ebd37SHimanshu Madhani 	fw_minor = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MINOR);
791a15ebd37SHimanshu Madhani 	fw_build = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_SUB);
792aa43c215SJeff Kirsher 
793aa43c215SJeff Kirsher 	adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
794aa43c215SJeff Kirsher 
795a15ebd37SHimanshu Madhani 	err = qlcnic_get_board_info(adapter);
796a15ebd37SHimanshu Madhani 	if (err) {
797a15ebd37SHimanshu Madhani 		dev_err(&pdev->dev, "Error getting board config info.\n");
798a15ebd37SHimanshu Madhani 		return;
799a15ebd37SHimanshu Madhani 	}
800a15ebd37SHimanshu Madhani 	if (ahw->op_mode != QLCNIC_NON_PRIV_FUNC) {
801aa43c215SJeff Kirsher 		if (fw_dump->tmpl_hdr == NULL ||
802aa43c215SJeff Kirsher 				adapter->fw_version > prev_fw_version) {
803aa43c215SJeff Kirsher 			if (fw_dump->tmpl_hdr)
804aa43c215SJeff Kirsher 				vfree(fw_dump->tmpl_hdr);
805aa43c215SJeff Kirsher 			if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
806aa43c215SJeff Kirsher 				dev_info(&pdev->dev,
807aa43c215SJeff Kirsher 					"Supports FW dump capability\n");
808aa43c215SJeff Kirsher 		}
809aa43c215SJeff Kirsher 	}
810aa43c215SJeff Kirsher 
81113159183SSony Chacko 	dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d\n",
81213159183SSony Chacko 		 QLCNIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build);
81313159183SSony Chacko 
814aa43c215SJeff Kirsher 	if (adapter->ahw->port_type == QLCNIC_XGBE) {
815aa43c215SJeff Kirsher 		if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
816aa43c215SJeff Kirsher 			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
817aa43c215SJeff Kirsher 			adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
818aa43c215SJeff Kirsher 		} else {
819aa43c215SJeff Kirsher 			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
820aa43c215SJeff Kirsher 			adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
821aa43c215SJeff Kirsher 		}
822aa43c215SJeff Kirsher 
823aa43c215SJeff Kirsher 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
824aa43c215SJeff Kirsher 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
825aa43c215SJeff Kirsher 
826aa43c215SJeff Kirsher 	} else if (adapter->ahw->port_type == QLCNIC_GBE) {
827aa43c215SJeff Kirsher 		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
828aa43c215SJeff Kirsher 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
829aa43c215SJeff Kirsher 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
830aa43c215SJeff Kirsher 		adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
831aa43c215SJeff Kirsher 	}
832aa43c215SJeff Kirsher 
833099f7aa7SSony Chacko 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
834aa43c215SJeff Kirsher 
835aa43c215SJeff Kirsher 	adapter->num_txd = MAX_CMD_DESCRIPTORS;
836aa43c215SJeff Kirsher 
837aa43c215SJeff Kirsher 	adapter->max_rds_rings = MAX_RDS_RINGS;
838aa43c215SJeff Kirsher }
839aa43c215SJeff Kirsher 
840aa43c215SJeff Kirsher static int
841aa43c215SJeff Kirsher qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
842aa43c215SJeff Kirsher {
843aa43c215SJeff Kirsher 	int err;
844aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
845aa43c215SJeff Kirsher 
846bff57d8eSSony Chacko 	memset(&nic_info, 0, sizeof(struct qlcnic_info));
847aa43c215SJeff Kirsher 	err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
848aa43c215SJeff Kirsher 	if (err)
849aa43c215SJeff Kirsher 		return err;
850aa43c215SJeff Kirsher 
85179788450SSony Chacko 	adapter->ahw->physical_port = (u8)nic_info.phys_port;
85279788450SSony Chacko 	adapter->ahw->switch_mode = nic_info.switch_mode;
85379788450SSony Chacko 	adapter->ahw->max_tx_ques = nic_info.max_tx_ques;
85479788450SSony Chacko 	adapter->ahw->max_rx_ques = nic_info.max_rx_ques;
85579788450SSony Chacko 	adapter->ahw->capabilities = nic_info.capabilities;
856776e7bdeSShahed Shaikh 
857776e7bdeSShahed Shaikh 	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
858776e7bdeSShahed Shaikh 		u32 temp;
859776e7bdeSShahed Shaikh 		temp = QLCRD32(adapter, CRB_FW_CAPABILITIES_2);
860776e7bdeSShahed Shaikh 		adapter->ahw->capabilities2 = temp;
861776e7bdeSShahed Shaikh 	}
86279788450SSony Chacko 	adapter->ahw->max_mac_filters = nic_info.max_mac_filters;
86379788450SSony Chacko 	adapter->ahw->max_mtu = nic_info.max_mtu;
864aa43c215SJeff Kirsher 
86513159183SSony Chacko 	/* Disable NPAR for 83XX */
86613159183SSony Chacko 	if (qlcnic_83xx_check(adapter))
86713159183SSony Chacko 		return err;
86813159183SSony Chacko 
86979788450SSony Chacko 	if (adapter->ahw->capabilities & BIT_6)
870aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_ESWITCH_ENABLED;
871aa43c215SJeff Kirsher 	else
872aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
873aa43c215SJeff Kirsher 
874aa43c215SJeff Kirsher 	return err;
875aa43c215SJeff Kirsher }
876aa43c215SJeff Kirsher 
877ec079a07SSony Chacko void qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
878aa43c215SJeff Kirsher 			    struct qlcnic_esw_func_cfg *esw_cfg)
879aa43c215SJeff Kirsher {
880aa43c215SJeff Kirsher 	if (esw_cfg->discard_tagged)
881aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
882aa43c215SJeff Kirsher 	else
883aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_TAGGING_ENABLED;
884aa43c215SJeff Kirsher 
885aa43c215SJeff Kirsher 	if (esw_cfg->vlan_id)
886aa43c215SJeff Kirsher 		adapter->pvid = esw_cfg->vlan_id;
887aa43c215SJeff Kirsher 	else
888aa43c215SJeff Kirsher 		adapter->pvid = 0;
889aa43c215SJeff Kirsher }
890aa43c215SJeff Kirsher 
8918e586137SJiri Pirko static int
892aa43c215SJeff Kirsher qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
893aa43c215SJeff Kirsher {
894aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
895aa43c215SJeff Kirsher 	set_bit(vid, adapter->vlans);
8968e586137SJiri Pirko 	return 0;
897aa43c215SJeff Kirsher }
898aa43c215SJeff Kirsher 
8998e586137SJiri Pirko static int
900aa43c215SJeff Kirsher qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
901aa43c215SJeff Kirsher {
902aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
903aa43c215SJeff Kirsher 
904aa43c215SJeff Kirsher 	qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
905aa43c215SJeff Kirsher 	clear_bit(vid, adapter->vlans);
9068e586137SJiri Pirko 	return 0;
907aa43c215SJeff Kirsher }
908aa43c215SJeff Kirsher 
909ec079a07SSony Chacko void qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
910aa43c215SJeff Kirsher 				      struct qlcnic_esw_func_cfg *esw_cfg)
911aa43c215SJeff Kirsher {
912aa43c215SJeff Kirsher 	adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
913aa43c215SJeff Kirsher 				QLCNIC_PROMISC_DISABLED);
914aa43c215SJeff Kirsher 
915aa43c215SJeff Kirsher 	if (esw_cfg->mac_anti_spoof)
916aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MACSPOOF;
917aa43c215SJeff Kirsher 
918aa43c215SJeff Kirsher 	if (!esw_cfg->mac_override)
919aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
920aa43c215SJeff Kirsher 
921aa43c215SJeff Kirsher 	if (!esw_cfg->promisc_mode)
922aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_PROMISC_DISABLED;
923aa43c215SJeff Kirsher 
924aa43c215SJeff Kirsher 	qlcnic_set_netdev_features(adapter, esw_cfg);
925aa43c215SJeff Kirsher }
926aa43c215SJeff Kirsher 
927d71170fbSSony Chacko int qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
928aa43c215SJeff Kirsher {
929aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
930aa43c215SJeff Kirsher 
931aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
932aa43c215SJeff Kirsher 		return 0;
933aa43c215SJeff Kirsher 
934aa43c215SJeff Kirsher 	esw_cfg.pci_func = adapter->ahw->pci_func;
935aa43c215SJeff Kirsher 	if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
936aa43c215SJeff Kirsher 			return -EIO;
937aa43c215SJeff Kirsher 	qlcnic_set_vlan_config(adapter, &esw_cfg);
938aa43c215SJeff Kirsher 	qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
939aa43c215SJeff Kirsher 
940aa43c215SJeff Kirsher 	return 0;
941aa43c215SJeff Kirsher }
942aa43c215SJeff Kirsher 
943aa43c215SJeff Kirsher static void
944aa43c215SJeff Kirsher qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
945aa43c215SJeff Kirsher 		struct qlcnic_esw_func_cfg *esw_cfg)
946aa43c215SJeff Kirsher {
947aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
94813159183SSony Chacko 	unsigned long features, vlan_features;
949aa43c215SJeff Kirsher 
95013159183SSony Chacko 	if (qlcnic_83xx_check(adapter))
95113159183SSony Chacko 		return;
95213159183SSony Chacko 
9537e38d04bSSony Chacko 	features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
954aa43c215SJeff Kirsher 		    NETIF_F_IPV6_CSUM | NETIF_F_GRO);
955aa43c215SJeff Kirsher 	vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
95613159183SSony Chacko 			NETIF_F_IPV6_CSUM);
957aa43c215SJeff Kirsher 
95813159183SSony Chacko 	if (QLCNIC_IS_TSO_CAPABLE(adapter)) {
959aa43c215SJeff Kirsher 		features |= (NETIF_F_TSO | NETIF_F_TSO6);
960aa43c215SJeff Kirsher 		vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
961aa43c215SJeff Kirsher 	}
962aa43c215SJeff Kirsher 
963aa43c215SJeff Kirsher 	if (netdev->features & NETIF_F_LRO)
964aa43c215SJeff Kirsher 		features |= NETIF_F_LRO;
965aa43c215SJeff Kirsher 
966aa43c215SJeff Kirsher 	if (esw_cfg->offload_flags & BIT_0) {
967aa43c215SJeff Kirsher 		netdev->features |= features;
96813159183SSony Chacko 		adapter->rx_csum = 1;
9697e38d04bSSony Chacko 		if (!(esw_cfg->offload_flags & BIT_1)) {
970aa43c215SJeff Kirsher 			netdev->features &= ~NETIF_F_TSO;
9717e38d04bSSony Chacko 			features &= ~NETIF_F_TSO;
9727e38d04bSSony Chacko 		}
9737e38d04bSSony Chacko 		if (!(esw_cfg->offload_flags & BIT_2)) {
974aa43c215SJeff Kirsher 			netdev->features &= ~NETIF_F_TSO6;
9757e38d04bSSony Chacko 			features &= ~NETIF_F_TSO6;
9767e38d04bSSony Chacko 		}
977aa43c215SJeff Kirsher 	} else {
978aa43c215SJeff Kirsher 		netdev->features &= ~features;
9797e38d04bSSony Chacko 		features &= ~features;
98013159183SSony Chacko 		adapter->rx_csum = 0;
981aa43c215SJeff Kirsher 	}
982aa43c215SJeff Kirsher 
983aa43c215SJeff Kirsher 	netdev->vlan_features = (features & vlan_features);
984aa43c215SJeff Kirsher }
985aa43c215SJeff Kirsher 
986aa43c215SJeff Kirsher static int
987aa43c215SJeff Kirsher qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
988aa43c215SJeff Kirsher {
989aa43c215SJeff Kirsher 	u32 op_mode, priv_level;
990aa43c215SJeff Kirsher 	int err = 0;
991aa43c215SJeff Kirsher 
992aa43c215SJeff Kirsher 	err = qlcnic_initialize_nic(adapter);
993aa43c215SJeff Kirsher 	if (err)
994aa43c215SJeff Kirsher 		return err;
995aa43c215SJeff Kirsher 
996aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
997aa43c215SJeff Kirsher 		return 0;
998aa43c215SJeff Kirsher 
99913159183SSony Chacko 	op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1000aa43c215SJeff Kirsher 	priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1001aa43c215SJeff Kirsher 
1002aa43c215SJeff Kirsher 	if (op_mode == QLC_DEV_DRV_DEFAULT)
1003aa43c215SJeff Kirsher 		priv_level = QLCNIC_MGMT_FUNC;
1004aa43c215SJeff Kirsher 	else
1005aa43c215SJeff Kirsher 		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1006aa43c215SJeff Kirsher 
1007aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
1008aa43c215SJeff Kirsher 		if (priv_level == QLCNIC_MGMT_FUNC) {
100979788450SSony Chacko 			adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
1010aa43c215SJeff Kirsher 			err = qlcnic_init_pci_info(adapter);
1011aa43c215SJeff Kirsher 			if (err)
1012aa43c215SJeff Kirsher 				return err;
1013aa43c215SJeff Kirsher 			/* Set privilege level for other functions */
1014aa43c215SJeff Kirsher 			qlcnic_set_function_modes(adapter);
1015aa43c215SJeff Kirsher 			dev_info(&adapter->pdev->dev,
1016aa43c215SJeff Kirsher 				"HAL Version: %d, Management function\n",
101779788450SSony Chacko 				 adapter->ahw->fw_hal_version);
1018aa43c215SJeff Kirsher 		} else if (priv_level == QLCNIC_PRIV_FUNC) {
101979788450SSony Chacko 			adapter->ahw->op_mode = QLCNIC_PRIV_FUNC;
1020aa43c215SJeff Kirsher 			dev_info(&adapter->pdev->dev,
1021aa43c215SJeff Kirsher 				"HAL Version: %d, Privileged function\n",
102279788450SSony Chacko 				 adapter->ahw->fw_hal_version);
1023aa43c215SJeff Kirsher 		}
1024aa43c215SJeff Kirsher 	}
1025aa43c215SJeff Kirsher 
1026aa43c215SJeff Kirsher 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
1027aa43c215SJeff Kirsher 
1028aa43c215SJeff Kirsher 	return err;
1029aa43c215SJeff Kirsher }
1030aa43c215SJeff Kirsher 
1031d71170fbSSony Chacko int qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
1032aa43c215SJeff Kirsher {
1033aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
1034aa43c215SJeff Kirsher 	struct qlcnic_npar_info *npar;
1035aa43c215SJeff Kirsher 	u8 i;
1036aa43c215SJeff Kirsher 
1037aa43c215SJeff Kirsher 	if (adapter->need_fw_reset)
1038aa43c215SJeff Kirsher 		return 0;
1039aa43c215SJeff Kirsher 
1040bff57d8eSSony Chacko 	for (i = 0; i < adapter->ahw->act_pci_func; i++) {
1041aa43c215SJeff Kirsher 		memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
1042bff57d8eSSony Chacko 		esw_cfg.pci_func = adapter->npars[i].pci_func;
1043aa43c215SJeff Kirsher 		esw_cfg.mac_override = BIT_0;
1044aa43c215SJeff Kirsher 		esw_cfg.promisc_mode = BIT_0;
1045bff57d8eSSony Chacko 		if (qlcnic_82xx_check(adapter)) {
1046bff57d8eSSony Chacko 			esw_cfg.offload_flags = BIT_0;
1047bff57d8eSSony Chacko 			if (QLCNIC_IS_TSO_CAPABLE(adapter))
1048aa43c215SJeff Kirsher 				esw_cfg.offload_flags |= (BIT_1 | BIT_2);
1049bff57d8eSSony Chacko 		}
1050aa43c215SJeff Kirsher 		if (qlcnic_config_switch_port(adapter, &esw_cfg))
1051aa43c215SJeff Kirsher 			return -EIO;
1052aa43c215SJeff Kirsher 		npar = &adapter->npars[i];
1053aa43c215SJeff Kirsher 		npar->pvid = esw_cfg.vlan_id;
1054aa43c215SJeff Kirsher 		npar->mac_override = esw_cfg.mac_override;
1055aa43c215SJeff Kirsher 		npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
1056aa43c215SJeff Kirsher 		npar->discard_tagged = esw_cfg.discard_tagged;
1057aa43c215SJeff Kirsher 		npar->promisc_mode = esw_cfg.promisc_mode;
1058aa43c215SJeff Kirsher 		npar->offload_flags = esw_cfg.offload_flags;
1059aa43c215SJeff Kirsher 	}
1060aa43c215SJeff Kirsher 
1061aa43c215SJeff Kirsher 	return 0;
1062aa43c215SJeff Kirsher }
1063aa43c215SJeff Kirsher 
106413159183SSony Chacko 
1065aa43c215SJeff Kirsher static int
1066aa43c215SJeff Kirsher qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
1067aa43c215SJeff Kirsher 			struct qlcnic_npar_info *npar, int pci_func)
1068aa43c215SJeff Kirsher {
1069aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
1070aa43c215SJeff Kirsher 	esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
1071aa43c215SJeff Kirsher 	esw_cfg.pci_func = pci_func;
1072aa43c215SJeff Kirsher 	esw_cfg.vlan_id = npar->pvid;
1073aa43c215SJeff Kirsher 	esw_cfg.mac_override = npar->mac_override;
1074aa43c215SJeff Kirsher 	esw_cfg.discard_tagged = npar->discard_tagged;
1075aa43c215SJeff Kirsher 	esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
1076aa43c215SJeff Kirsher 	esw_cfg.offload_flags = npar->offload_flags;
1077aa43c215SJeff Kirsher 	esw_cfg.promisc_mode = npar->promisc_mode;
1078aa43c215SJeff Kirsher 	if (qlcnic_config_switch_port(adapter, &esw_cfg))
1079aa43c215SJeff Kirsher 		return -EIO;
1080aa43c215SJeff Kirsher 
1081aa43c215SJeff Kirsher 	esw_cfg.op_mode = QLCNIC_ADD_VLAN;
1082aa43c215SJeff Kirsher 	if (qlcnic_config_switch_port(adapter, &esw_cfg))
1083aa43c215SJeff Kirsher 		return -EIO;
1084aa43c215SJeff Kirsher 
1085aa43c215SJeff Kirsher 	return 0;
1086aa43c215SJeff Kirsher }
1087aa43c215SJeff Kirsher 
1088d71170fbSSony Chacko int qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
1089aa43c215SJeff Kirsher {
1090aa43c215SJeff Kirsher 	int i, err;
1091aa43c215SJeff Kirsher 	struct qlcnic_npar_info *npar;
1092aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
1093bff57d8eSSony Chacko 	u8 pci_func;
1094aa43c215SJeff Kirsher 
1095bff57d8eSSony Chacko 	if (qlcnic_82xx_check(adapter))
1096aa43c215SJeff Kirsher 		if (!adapter->need_fw_reset)
1097aa43c215SJeff Kirsher 			return 0;
1098aa43c215SJeff Kirsher 
1099aa43c215SJeff Kirsher 	/* Set the NPAR config data after FW reset */
1100bff57d8eSSony Chacko 	for (i = 0; i < adapter->ahw->act_pci_func; i++) {
1101aa43c215SJeff Kirsher 		npar = &adapter->npars[i];
1102bff57d8eSSony Chacko 		pci_func = npar->pci_func;
1103bff57d8eSSony Chacko 		memset(&nic_info, 0, sizeof(struct qlcnic_info));
110413159183SSony Chacko 		err = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
1105aa43c215SJeff Kirsher 		if (err)
1106aa43c215SJeff Kirsher 			return err;
1107aa43c215SJeff Kirsher 		nic_info.min_tx_bw = npar->min_bw;
1108aa43c215SJeff Kirsher 		nic_info.max_tx_bw = npar->max_bw;
1109aa43c215SJeff Kirsher 		err = qlcnic_set_nic_info(adapter, &nic_info);
1110aa43c215SJeff Kirsher 		if (err)
1111aa43c215SJeff Kirsher 			return err;
1112aa43c215SJeff Kirsher 
1113aa43c215SJeff Kirsher 		if (npar->enable_pm) {
1114aa43c215SJeff Kirsher 			err = qlcnic_config_port_mirroring(adapter,
1115bff57d8eSSony Chacko 							   npar->dest_npar, 1,
1116bff57d8eSSony Chacko 							   pci_func);
1117aa43c215SJeff Kirsher 			if (err)
1118aa43c215SJeff Kirsher 				return err;
1119aa43c215SJeff Kirsher 		}
1120bff57d8eSSony Chacko 		err = qlcnic_reset_eswitch_config(adapter, npar, pci_func);
1121aa43c215SJeff Kirsher 		if (err)
1122aa43c215SJeff Kirsher 			return err;
1123aa43c215SJeff Kirsher 	}
1124aa43c215SJeff Kirsher 	return 0;
1125aa43c215SJeff Kirsher }
1126aa43c215SJeff Kirsher 
1127aa43c215SJeff Kirsher static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
1128aa43c215SJeff Kirsher {
1129aa43c215SJeff Kirsher 	u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
1130aa43c215SJeff Kirsher 	u32 npar_state;
1131aa43c215SJeff Kirsher 
113279788450SSony Chacko 	if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
1133aa43c215SJeff Kirsher 		return 0;
1134aa43c215SJeff Kirsher 
1135a15ebd37SHimanshu Madhani 	npar_state = QLC_SHARED_REG_RD32(adapter,
1136a15ebd37SHimanshu Madhani 					 QLCNIC_CRB_DEV_NPAR_STATE);
1137aa43c215SJeff Kirsher 	while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
1138aa43c215SJeff Kirsher 		msleep(1000);
1139a15ebd37SHimanshu Madhani 		npar_state = QLC_SHARED_REG_RD32(adapter,
1140a15ebd37SHimanshu Madhani 						 QLCNIC_CRB_DEV_NPAR_STATE);
1141aa43c215SJeff Kirsher 	}
1142aa43c215SJeff Kirsher 	if (!npar_opt_timeo) {
1143aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
11448a168ca7SMasanari Iida 			"Waiting for NPAR state to operational timeout\n");
1145aa43c215SJeff Kirsher 		return -EIO;
1146aa43c215SJeff Kirsher 	}
1147aa43c215SJeff Kirsher 	return 0;
1148aa43c215SJeff Kirsher }
1149aa43c215SJeff Kirsher 
1150aa43c215SJeff Kirsher static int
1151aa43c215SJeff Kirsher qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
1152aa43c215SJeff Kirsher {
1153aa43c215SJeff Kirsher 	int err;
1154aa43c215SJeff Kirsher 
1155aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
115679788450SSony Chacko 	    adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1157aa43c215SJeff Kirsher 		return 0;
1158aa43c215SJeff Kirsher 
1159aa43c215SJeff Kirsher 	err = qlcnic_set_default_offload_settings(adapter);
1160aa43c215SJeff Kirsher 	if (err)
1161aa43c215SJeff Kirsher 		return err;
1162aa43c215SJeff Kirsher 
1163aa43c215SJeff Kirsher 	err = qlcnic_reset_npar_config(adapter);
1164aa43c215SJeff Kirsher 	if (err)
1165aa43c215SJeff Kirsher 		return err;
1166aa43c215SJeff Kirsher 
1167aa43c215SJeff Kirsher 	qlcnic_dev_set_npar_ready(adapter);
1168aa43c215SJeff Kirsher 
1169aa43c215SJeff Kirsher 	return err;
1170aa43c215SJeff Kirsher }
1171aa43c215SJeff Kirsher 
11727e2cf4feSSony Chacko int qlcnic_82xx_start_firmware(struct qlcnic_adapter *adapter)
1173aa43c215SJeff Kirsher {
1174aa43c215SJeff Kirsher 	int err;
1175aa43c215SJeff Kirsher 
1176aa43c215SJeff Kirsher 	err = qlcnic_can_start_firmware(adapter);
1177aa43c215SJeff Kirsher 	if (err < 0)
1178aa43c215SJeff Kirsher 		return err;
1179aa43c215SJeff Kirsher 	else if (!err)
1180aa43c215SJeff Kirsher 		goto check_fw_status;
1181aa43c215SJeff Kirsher 
1182099f7aa7SSony Chacko 	if (qlcnic_load_fw_file)
1183aa43c215SJeff Kirsher 		qlcnic_request_firmware(adapter);
1184aa43c215SJeff Kirsher 	else {
1185aa43c215SJeff Kirsher 		err = qlcnic_check_flash_fw_ver(adapter);
1186aa43c215SJeff Kirsher 		if (err)
1187aa43c215SJeff Kirsher 			goto err_out;
1188aa43c215SJeff Kirsher 
118979788450SSony Chacko 		adapter->ahw->fw_type = QLCNIC_FLASH_ROMIMAGE;
1190aa43c215SJeff Kirsher 	}
1191aa43c215SJeff Kirsher 
1192aa43c215SJeff Kirsher 	err = qlcnic_need_fw_reset(adapter);
1193aa43c215SJeff Kirsher 	if (err == 0)
1194aa43c215SJeff Kirsher 		goto check_fw_status;
1195aa43c215SJeff Kirsher 
1196aa43c215SJeff Kirsher 	err = qlcnic_pinit_from_rom(adapter);
1197aa43c215SJeff Kirsher 	if (err)
1198aa43c215SJeff Kirsher 		goto err_out;
1199aa43c215SJeff Kirsher 
1200aa43c215SJeff Kirsher 	err = qlcnic_load_firmware(adapter);
1201aa43c215SJeff Kirsher 	if (err)
1202aa43c215SJeff Kirsher 		goto err_out;
1203aa43c215SJeff Kirsher 
1204aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1205aa43c215SJeff Kirsher 	QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1206aa43c215SJeff Kirsher 
1207aa43c215SJeff Kirsher check_fw_status:
1208aa43c215SJeff Kirsher 	err = qlcnic_check_fw_status(adapter);
1209aa43c215SJeff Kirsher 	if (err)
1210aa43c215SJeff Kirsher 		goto err_out;
1211aa43c215SJeff Kirsher 
1212a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1213aa43c215SJeff Kirsher 	qlcnic_idc_debug_info(adapter, 1);
1214aa43c215SJeff Kirsher 	err = qlcnic_check_eswitch_mode(adapter);
1215aa43c215SJeff Kirsher 	if (err) {
1216aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
1217aa43c215SJeff Kirsher 			"Memory allocation failed for eswitch\n");
1218aa43c215SJeff Kirsher 		goto err_out;
1219aa43c215SJeff Kirsher 	}
1220aa43c215SJeff Kirsher 	err = qlcnic_set_mgmt_operations(adapter);
1221aa43c215SJeff Kirsher 	if (err)
1222aa43c215SJeff Kirsher 		goto err_out;
1223aa43c215SJeff Kirsher 
1224aa43c215SJeff Kirsher 	qlcnic_check_options(adapter);
1225aa43c215SJeff Kirsher 	adapter->need_fw_reset = 0;
1226aa43c215SJeff Kirsher 
1227aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1228aa43c215SJeff Kirsher 	return 0;
1229aa43c215SJeff Kirsher 
1230aa43c215SJeff Kirsher err_out:
1231a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1232aa43c215SJeff Kirsher 	dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1233aa43c215SJeff Kirsher 
1234aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1235aa43c215SJeff Kirsher 	return err;
1236aa43c215SJeff Kirsher }
1237aa43c215SJeff Kirsher 
1238aa43c215SJeff Kirsher static int
1239aa43c215SJeff Kirsher qlcnic_request_irq(struct qlcnic_adapter *adapter)
1240aa43c215SJeff Kirsher {
1241aa43c215SJeff Kirsher 	irq_handler_t handler;
1242aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
124313159183SSony Chacko 	struct qlcnic_host_tx_ring *tx_ring;
1244aa43c215SJeff Kirsher 	int err, ring;
1245aa43c215SJeff Kirsher 
1246aa43c215SJeff Kirsher 	unsigned long flags = 0;
1247aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1248aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1249aa43c215SJeff Kirsher 
125079788450SSony Chacko 	if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
125113159183SSony Chacko 		if (qlcnic_82xx_check(adapter))
1252aa43c215SJeff Kirsher 			handler = qlcnic_tmp_intr;
1253aa43c215SJeff Kirsher 		if (!QLCNIC_IS_MSI_FAMILY(adapter))
1254aa43c215SJeff Kirsher 			flags |= IRQF_SHARED;
1255aa43c215SJeff Kirsher 
1256aa43c215SJeff Kirsher 	} else {
1257aa43c215SJeff Kirsher 		if (adapter->flags & QLCNIC_MSIX_ENABLED)
1258aa43c215SJeff Kirsher 			handler = qlcnic_msix_intr;
1259aa43c215SJeff Kirsher 		else if (adapter->flags & QLCNIC_MSI_ENABLED)
1260aa43c215SJeff Kirsher 			handler = qlcnic_msi_intr;
1261aa43c215SJeff Kirsher 		else {
1262aa43c215SJeff Kirsher 			flags |= IRQF_SHARED;
1263ac166700SHimanshu Madhani 			if (qlcnic_82xx_check(adapter))
1264aa43c215SJeff Kirsher 				handler = qlcnic_intr;
1265ac166700SHimanshu Madhani 			else
1266ac166700SHimanshu Madhani 				handler = qlcnic_83xx_intr;
1267aa43c215SJeff Kirsher 		}
1268aa43c215SJeff Kirsher 	}
1269aa43c215SJeff Kirsher 	adapter->irq = netdev->irq;
1270aa43c215SJeff Kirsher 
127113159183SSony Chacko 	if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1272ac166700SHimanshu Madhani 		if (qlcnic_82xx_check(adapter) ||
1273ac166700SHimanshu Madhani 		    (qlcnic_83xx_check(adapter) &&
1274ac166700SHimanshu Madhani 		     (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1275aa43c215SJeff Kirsher 			for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1276aa43c215SJeff Kirsher 				sds_ring = &recv_ctx->sds_rings[ring];
127713159183SSony Chacko 				snprintf(sds_ring->name, sizeof(int) + IFNAMSIZ,
127813159183SSony Chacko 					 "%s[%d]", netdev->name, ring);
127913159183SSony Chacko 				err = request_irq(sds_ring->irq, handler, flags,
128013159183SSony Chacko 						  sds_ring->name, sds_ring);
1281aa43c215SJeff Kirsher 				if (err)
1282aa43c215SJeff Kirsher 					return err;
1283aa43c215SJeff Kirsher 			}
1284ac166700SHimanshu Madhani 		}
128513159183SSony Chacko 		if (qlcnic_83xx_check(adapter) &&
128613159183SSony Chacko 		    (adapter->flags & QLCNIC_MSIX_ENABLED)) {
128713159183SSony Chacko 			handler = qlcnic_msix_tx_intr;
128813159183SSony Chacko 			for (ring = 0; ring < adapter->max_drv_tx_rings;
128913159183SSony Chacko 			     ring++) {
129013159183SSony Chacko 				tx_ring = &adapter->tx_ring[ring];
129113159183SSony Chacko 				snprintf(tx_ring->name, sizeof(int) + IFNAMSIZ,
129213159183SSony Chacko 					 "%s[%d]", netdev->name,
129313159183SSony Chacko 					 adapter->max_sds_rings + ring);
129413159183SSony Chacko 				err = request_irq(tx_ring->irq, handler, flags,
129513159183SSony Chacko 						  tx_ring->name, tx_ring);
129613159183SSony Chacko 				if (err)
129713159183SSony Chacko 					return err;
129813159183SSony Chacko 			}
129913159183SSony Chacko 		}
130013159183SSony Chacko 	}
1301aa43c215SJeff Kirsher 	return 0;
1302aa43c215SJeff Kirsher }
1303aa43c215SJeff Kirsher 
1304aa43c215SJeff Kirsher static void
1305aa43c215SJeff Kirsher qlcnic_free_irq(struct qlcnic_adapter *adapter)
1306aa43c215SJeff Kirsher {
1307aa43c215SJeff Kirsher 	int ring;
1308aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
130913159183SSony Chacko 	struct qlcnic_host_tx_ring *tx_ring;
1310aa43c215SJeff Kirsher 
1311aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1312aa43c215SJeff Kirsher 
131313159183SSony Chacko 	if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1314ac166700SHimanshu Madhani 		if (qlcnic_82xx_check(adapter) ||
1315ac166700SHimanshu Madhani 		    (qlcnic_83xx_check(adapter) &&
1316ac166700SHimanshu Madhani 		     (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1317aa43c215SJeff Kirsher 			for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1318aa43c215SJeff Kirsher 				sds_ring = &recv_ctx->sds_rings[ring];
1319aa43c215SJeff Kirsher 				free_irq(sds_ring->irq, sds_ring);
1320aa43c215SJeff Kirsher 			}
1321ac166700SHimanshu Madhani 		}
132213159183SSony Chacko 		if (qlcnic_83xx_check(adapter)) {
132313159183SSony Chacko 			for (ring = 0; ring < adapter->max_drv_tx_rings;
132413159183SSony Chacko 			     ring++) {
132513159183SSony Chacko 				tx_ring = &adapter->tx_ring[ring];
132613159183SSony Chacko 				if (tx_ring->irq)
132713159183SSony Chacko 					free_irq(tx_ring->irq, tx_ring);
132813159183SSony Chacko 			}
132913159183SSony Chacko 		}
133013159183SSony Chacko 	}
1331aa43c215SJeff Kirsher }
1332aa43c215SJeff Kirsher 
133399e85879SShahed Shaikh static void qlcnic_get_lro_mss_capability(struct qlcnic_adapter *adapter)
133499e85879SShahed Shaikh {
133599e85879SShahed Shaikh 	u32 capab = 0;
133699e85879SShahed Shaikh 
133799e85879SShahed Shaikh 	if (qlcnic_82xx_check(adapter)) {
133899e85879SShahed Shaikh 		if (adapter->ahw->capabilities2 &
133999e85879SShahed Shaikh 		    QLCNIC_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
134099e85879SShahed Shaikh 			adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
134199e85879SShahed Shaikh 	} else {
134299e85879SShahed Shaikh 		capab = adapter->ahw->capabilities;
134399e85879SShahed Shaikh 		if (QLC_83XX_GET_FW_LRO_MSS_CAPABILITY(capab))
134499e85879SShahed Shaikh 			adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
134599e85879SShahed Shaikh 	}
134699e85879SShahed Shaikh }
134799e85879SShahed Shaikh 
1348319ecf12SSony Chacko int __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1349aa43c215SJeff Kirsher {
1350aa43c215SJeff Kirsher 	int ring;
1351aa43c215SJeff Kirsher 	struct qlcnic_host_rds_ring *rds_ring;
1352aa43c215SJeff Kirsher 
1353aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1354aa43c215SJeff Kirsher 		return -EIO;
1355aa43c215SJeff Kirsher 
1356aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1357aa43c215SJeff Kirsher 		return 0;
1358aa43c215SJeff Kirsher 	if (qlcnic_set_eswitch_port_config(adapter))
1359aa43c215SJeff Kirsher 		return -EIO;
136099e85879SShahed Shaikh 	qlcnic_get_lro_mss_capability(adapter);
1361cae82d49SRajesh Borundia 
1362aa43c215SJeff Kirsher 	if (qlcnic_fw_create_ctx(adapter))
1363aa43c215SJeff Kirsher 		return -EIO;
1364aa43c215SJeff Kirsher 
1365aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1366aa43c215SJeff Kirsher 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
13674be41e92SSony Chacko 		qlcnic_post_rx_buffers(adapter, rds_ring, ring);
1368aa43c215SJeff Kirsher 	}
1369aa43c215SJeff Kirsher 
1370aa43c215SJeff Kirsher 	qlcnic_set_multi(netdev);
1371aa43c215SJeff Kirsher 	qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1372aa43c215SJeff Kirsher 
1373aa43c215SJeff Kirsher 	adapter->ahw->linkup = 0;
1374aa43c215SJeff Kirsher 
1375aa43c215SJeff Kirsher 	if (adapter->max_sds_rings > 1)
1376aa43c215SJeff Kirsher 		qlcnic_config_rss(adapter, 1);
1377aa43c215SJeff Kirsher 
1378aa43c215SJeff Kirsher 	qlcnic_config_intr_coalesce(adapter);
1379aa43c215SJeff Kirsher 
1380aa43c215SJeff Kirsher 	if (netdev->features & NETIF_F_LRO)
1381aa43c215SJeff Kirsher 		qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1382aa43c215SJeff Kirsher 
1383aa43c215SJeff Kirsher 	qlcnic_napi_enable(adapter);
1384aa43c215SJeff Kirsher 
1385aa43c215SJeff Kirsher 	qlcnic_linkevent_request(adapter, 1);
1386aa43c215SJeff Kirsher 
138779788450SSony Chacko 	adapter->ahw->reset_context = 0;
1388aa43c215SJeff Kirsher 	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1389aa43c215SJeff Kirsher 	return 0;
1390aa43c215SJeff Kirsher }
1391aa43c215SJeff Kirsher 
1392629263acSSony Chacko int qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1393aa43c215SJeff Kirsher {
1394aa43c215SJeff Kirsher 	int err = 0;
1395aa43c215SJeff Kirsher 
1396aa43c215SJeff Kirsher 	rtnl_lock();
1397aa43c215SJeff Kirsher 	if (netif_running(netdev))
1398aa43c215SJeff Kirsher 		err = __qlcnic_up(adapter, netdev);
1399aa43c215SJeff Kirsher 	rtnl_unlock();
1400aa43c215SJeff Kirsher 
1401aa43c215SJeff Kirsher 	return err;
1402aa43c215SJeff Kirsher }
1403aa43c215SJeff Kirsher 
1404319ecf12SSony Chacko void __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1405aa43c215SJeff Kirsher {
1406aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1407aa43c215SJeff Kirsher 		return;
1408aa43c215SJeff Kirsher 
1409aa43c215SJeff Kirsher 	if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1410aa43c215SJeff Kirsher 		return;
1411aa43c215SJeff Kirsher 
1412aa43c215SJeff Kirsher 	smp_mb();
1413aa43c215SJeff Kirsher 	spin_lock(&adapter->tx_clean_lock);
1414aa43c215SJeff Kirsher 	netif_carrier_off(netdev);
1415aa43c215SJeff Kirsher 	netif_tx_disable(netdev);
1416aa43c215SJeff Kirsher 
1417aa43c215SJeff Kirsher 	qlcnic_free_mac_list(adapter);
1418aa43c215SJeff Kirsher 
1419aa43c215SJeff Kirsher 	if (adapter->fhash.fnum)
1420aa43c215SJeff Kirsher 		qlcnic_delete_lb_filters(adapter);
1421aa43c215SJeff Kirsher 
1422aa43c215SJeff Kirsher 	qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1423aa43c215SJeff Kirsher 
1424aa43c215SJeff Kirsher 	qlcnic_napi_disable(adapter);
1425aa43c215SJeff Kirsher 
1426aa43c215SJeff Kirsher 	qlcnic_fw_destroy_ctx(adapter);
1427cae82d49SRajesh Borundia 	adapter->flags &= ~QLCNIC_FW_LRO_MSS_CAP;
1428aa43c215SJeff Kirsher 
1429aa43c215SJeff Kirsher 	qlcnic_reset_rx_buffers_list(adapter);
1430aa43c215SJeff Kirsher 	qlcnic_release_tx_buffers(adapter);
1431aa43c215SJeff Kirsher 	spin_unlock(&adapter->tx_clean_lock);
1432aa43c215SJeff Kirsher }
1433aa43c215SJeff Kirsher 
1434aa43c215SJeff Kirsher /* Usage: During suspend and firmware recovery module */
1435aa43c215SJeff Kirsher 
1436629263acSSony Chacko void qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1437aa43c215SJeff Kirsher {
1438aa43c215SJeff Kirsher 	rtnl_lock();
1439aa43c215SJeff Kirsher 	if (netif_running(netdev))
1440aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
1441aa43c215SJeff Kirsher 	rtnl_unlock();
1442aa43c215SJeff Kirsher 
1443aa43c215SJeff Kirsher }
1444aa43c215SJeff Kirsher 
1445319ecf12SSony Chacko int
1446aa43c215SJeff Kirsher qlcnic_attach(struct qlcnic_adapter *adapter)
1447aa43c215SJeff Kirsher {
1448aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1449aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1450aa43c215SJeff Kirsher 	int err;
1451aa43c215SJeff Kirsher 
1452aa43c215SJeff Kirsher 	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1453aa43c215SJeff Kirsher 		return 0;
1454aa43c215SJeff Kirsher 
1455aa43c215SJeff Kirsher 	err = qlcnic_napi_add(adapter, netdev);
1456aa43c215SJeff Kirsher 	if (err)
1457aa43c215SJeff Kirsher 		return err;
1458aa43c215SJeff Kirsher 
1459aa43c215SJeff Kirsher 	err = qlcnic_alloc_sw_resources(adapter);
1460aa43c215SJeff Kirsher 	if (err) {
1461aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Error in setting sw resources\n");
1462aa43c215SJeff Kirsher 		goto err_out_napi_del;
1463aa43c215SJeff Kirsher 	}
1464aa43c215SJeff Kirsher 
1465aa43c215SJeff Kirsher 	err = qlcnic_alloc_hw_resources(adapter);
1466aa43c215SJeff Kirsher 	if (err) {
1467aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Error in setting hw resources\n");
1468aa43c215SJeff Kirsher 		goto err_out_free_sw;
1469aa43c215SJeff Kirsher 	}
1470aa43c215SJeff Kirsher 
1471aa43c215SJeff Kirsher 	err = qlcnic_request_irq(adapter);
1472aa43c215SJeff Kirsher 	if (err) {
1473aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to setup interrupt\n");
1474aa43c215SJeff Kirsher 		goto err_out_free_hw;
1475aa43c215SJeff Kirsher 	}
1476aa43c215SJeff Kirsher 
1477aa43c215SJeff Kirsher 	qlcnic_create_sysfs_entries(adapter);
1478aa43c215SJeff Kirsher 
1479aa43c215SJeff Kirsher 	adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1480aa43c215SJeff Kirsher 	return 0;
1481aa43c215SJeff Kirsher 
1482aa43c215SJeff Kirsher err_out_free_hw:
1483aa43c215SJeff Kirsher 	qlcnic_free_hw_resources(adapter);
1484aa43c215SJeff Kirsher err_out_free_sw:
1485aa43c215SJeff Kirsher 	qlcnic_free_sw_resources(adapter);
1486aa43c215SJeff Kirsher err_out_napi_del:
1487aa43c215SJeff Kirsher 	qlcnic_napi_del(adapter);
1488aa43c215SJeff Kirsher 	return err;
1489aa43c215SJeff Kirsher }
1490aa43c215SJeff Kirsher 
1491319ecf12SSony Chacko void qlcnic_detach(struct qlcnic_adapter *adapter)
1492aa43c215SJeff Kirsher {
1493aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1494aa43c215SJeff Kirsher 		return;
1495aa43c215SJeff Kirsher 
1496aa43c215SJeff Kirsher 	qlcnic_remove_sysfs_entries(adapter);
1497aa43c215SJeff Kirsher 
1498aa43c215SJeff Kirsher 	qlcnic_free_hw_resources(adapter);
1499aa43c215SJeff Kirsher 	qlcnic_release_rx_buffers(adapter);
1500aa43c215SJeff Kirsher 	qlcnic_free_irq(adapter);
1501aa43c215SJeff Kirsher 	qlcnic_napi_del(adapter);
1502aa43c215SJeff Kirsher 	qlcnic_free_sw_resources(adapter);
1503aa43c215SJeff Kirsher 
1504aa43c215SJeff Kirsher 	adapter->is_up = 0;
1505aa43c215SJeff Kirsher }
1506aa43c215SJeff Kirsher 
1507aa43c215SJeff Kirsher void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1508aa43c215SJeff Kirsher {
1509aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1510aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1511aa43c215SJeff Kirsher 	int ring;
1512aa43c215SJeff Kirsher 
1513aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_DEV_UP, &adapter->state);
151479788450SSony Chacko 	if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
1515aa43c215SJeff Kirsher 		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1516aa43c215SJeff Kirsher 			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1517aa43c215SJeff Kirsher 			qlcnic_disable_int(sds_ring);
1518aa43c215SJeff Kirsher 		}
1519aa43c215SJeff Kirsher 	}
1520aa43c215SJeff Kirsher 
1521aa43c215SJeff Kirsher 	qlcnic_fw_destroy_ctx(adapter);
1522aa43c215SJeff Kirsher 
1523aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1524aa43c215SJeff Kirsher 
152579788450SSony Chacko 	adapter->ahw->diag_test = 0;
1526aa43c215SJeff Kirsher 	adapter->max_sds_rings = max_sds_rings;
1527aa43c215SJeff Kirsher 
1528aa43c215SJeff Kirsher 	if (qlcnic_attach(adapter))
1529aa43c215SJeff Kirsher 		goto out;
1530aa43c215SJeff Kirsher 
1531aa43c215SJeff Kirsher 	if (netif_running(netdev))
1532aa43c215SJeff Kirsher 		__qlcnic_up(adapter, netdev);
1533aa43c215SJeff Kirsher out:
1534aa43c215SJeff Kirsher 	netif_device_attach(netdev);
1535aa43c215SJeff Kirsher }
1536aa43c215SJeff Kirsher 
1537aa43c215SJeff Kirsher static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1538aa43c215SJeff Kirsher {
1539aa43c215SJeff Kirsher 	int err = 0;
1540aa43c215SJeff Kirsher 	adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1541aa43c215SJeff Kirsher 				GFP_KERNEL);
1542aa43c215SJeff Kirsher 	if (!adapter->recv_ctx) {
1543aa43c215SJeff Kirsher 		err = -ENOMEM;
1544aa43c215SJeff Kirsher 		goto err_out;
1545aa43c215SJeff Kirsher 	}
1546aa43c215SJeff Kirsher 	/* Initialize interrupt coalesce parameters */
1547aa43c215SJeff Kirsher 	adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1548aa43c215SJeff Kirsher 	adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1549aa43c215SJeff Kirsher 	adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
15504be41e92SSony Chacko 	/* clear stats */
15514be41e92SSony Chacko 	memset(&adapter->stats, 0, sizeof(adapter->stats));
1552aa43c215SJeff Kirsher err_out:
1553aa43c215SJeff Kirsher 	return err;
1554aa43c215SJeff Kirsher }
1555aa43c215SJeff Kirsher 
1556aa43c215SJeff Kirsher static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1557aa43c215SJeff Kirsher {
1558aa43c215SJeff Kirsher 	kfree(adapter->recv_ctx);
1559aa43c215SJeff Kirsher 	adapter->recv_ctx = NULL;
1560aa43c215SJeff Kirsher 
1561aa43c215SJeff Kirsher 	if (adapter->ahw->fw_dump.tmpl_hdr) {
1562aa43c215SJeff Kirsher 		vfree(adapter->ahw->fw_dump.tmpl_hdr);
1563aa43c215SJeff Kirsher 		adapter->ahw->fw_dump.tmpl_hdr = NULL;
1564aa43c215SJeff Kirsher 	}
15654be41e92SSony Chacko 
156681d0aeb0SSony Chacko 	kfree(adapter->ahw->reset.buff);
15674be41e92SSony Chacko 	adapter->ahw->fw_dump.tmpl_hdr = NULL;
1568aa43c215SJeff Kirsher }
1569aa43c215SJeff Kirsher 
1570aa43c215SJeff Kirsher int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1571aa43c215SJeff Kirsher {
1572aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1573aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1574aa43c215SJeff Kirsher 	struct qlcnic_host_rds_ring *rds_ring;
1575aa43c215SJeff Kirsher 	int ring;
1576aa43c215SJeff Kirsher 	int ret;
1577aa43c215SJeff Kirsher 
1578aa43c215SJeff Kirsher 	netif_device_detach(netdev);
1579aa43c215SJeff Kirsher 
1580aa43c215SJeff Kirsher 	if (netif_running(netdev))
1581aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
1582aa43c215SJeff Kirsher 
1583aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1584aa43c215SJeff Kirsher 
1585aa43c215SJeff Kirsher 	adapter->max_sds_rings = 1;
158679788450SSony Chacko 	adapter->ahw->diag_test = test;
15874be41e92SSony Chacko 	adapter->ahw->linkup = 0;
1588aa43c215SJeff Kirsher 
1589aa43c215SJeff Kirsher 	ret = qlcnic_attach(adapter);
1590aa43c215SJeff Kirsher 	if (ret) {
1591aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1592aa43c215SJeff Kirsher 		return ret;
1593aa43c215SJeff Kirsher 	}
1594aa43c215SJeff Kirsher 
1595aa43c215SJeff Kirsher 	ret = qlcnic_fw_create_ctx(adapter);
1596aa43c215SJeff Kirsher 	if (ret) {
1597aa43c215SJeff Kirsher 		qlcnic_detach(adapter);
1598aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1599aa43c215SJeff Kirsher 		return ret;
1600aa43c215SJeff Kirsher 	}
1601aa43c215SJeff Kirsher 
1602aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1603aa43c215SJeff Kirsher 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
16044be41e92SSony Chacko 		qlcnic_post_rx_buffers(adapter, rds_ring, ring);
1605aa43c215SJeff Kirsher 	}
1606aa43c215SJeff Kirsher 
160779788450SSony Chacko 	if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
1608aa43c215SJeff Kirsher 		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1609aa43c215SJeff Kirsher 			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1610aa43c215SJeff Kirsher 			qlcnic_enable_int(sds_ring);
1611aa43c215SJeff Kirsher 		}
1612aa43c215SJeff Kirsher 	}
1613aa43c215SJeff Kirsher 
161479788450SSony Chacko 	if (adapter->ahw->diag_test == QLCNIC_LOOPBACK_TEST) {
1615aa43c215SJeff Kirsher 		adapter->ahw->loopback_state = 0;
1616aa43c215SJeff Kirsher 		qlcnic_linkevent_request(adapter, 1);
1617aa43c215SJeff Kirsher 	}
1618aa43c215SJeff Kirsher 
1619aa43c215SJeff Kirsher 	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1620aa43c215SJeff Kirsher 
1621aa43c215SJeff Kirsher 	return 0;
1622aa43c215SJeff Kirsher }
1623aa43c215SJeff Kirsher 
1624aa43c215SJeff Kirsher /* Reset context in hardware only */
1625aa43c215SJeff Kirsher static int
1626aa43c215SJeff Kirsher qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1627aa43c215SJeff Kirsher {
1628aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1629aa43c215SJeff Kirsher 
1630aa43c215SJeff Kirsher 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1631aa43c215SJeff Kirsher 		return -EBUSY;
1632aa43c215SJeff Kirsher 
1633aa43c215SJeff Kirsher 	netif_device_detach(netdev);
1634aa43c215SJeff Kirsher 
1635aa43c215SJeff Kirsher 	qlcnic_down(adapter, netdev);
1636aa43c215SJeff Kirsher 
1637aa43c215SJeff Kirsher 	qlcnic_up(adapter, netdev);
1638aa43c215SJeff Kirsher 
1639aa43c215SJeff Kirsher 	netif_device_attach(netdev);
1640aa43c215SJeff Kirsher 
1641aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
164213159183SSony Chacko 	dev_err(&adapter->pdev->dev, "%s:\n", __func__);
1643aa43c215SJeff Kirsher 	return 0;
1644aa43c215SJeff Kirsher }
1645aa43c215SJeff Kirsher 
1646aa43c215SJeff Kirsher int
1647aa43c215SJeff Kirsher qlcnic_reset_context(struct qlcnic_adapter *adapter)
1648aa43c215SJeff Kirsher {
1649aa43c215SJeff Kirsher 	int err = 0;
1650aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1651aa43c215SJeff Kirsher 
1652aa43c215SJeff Kirsher 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1653aa43c215SJeff Kirsher 		return -EBUSY;
1654aa43c215SJeff Kirsher 
1655aa43c215SJeff Kirsher 	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1656aa43c215SJeff Kirsher 
1657aa43c215SJeff Kirsher 		netif_device_detach(netdev);
1658aa43c215SJeff Kirsher 
1659aa43c215SJeff Kirsher 		if (netif_running(netdev))
1660aa43c215SJeff Kirsher 			__qlcnic_down(adapter, netdev);
1661aa43c215SJeff Kirsher 
1662aa43c215SJeff Kirsher 		qlcnic_detach(adapter);
1663aa43c215SJeff Kirsher 
1664aa43c215SJeff Kirsher 		if (netif_running(netdev)) {
1665aa43c215SJeff Kirsher 			err = qlcnic_attach(adapter);
16661d5c88e3SAnirban Chakraborty 			if (!err) {
1667aa43c215SJeff Kirsher 				__qlcnic_up(adapter, netdev);
16681d5c88e3SAnirban Chakraborty 				qlcnic_restore_indev_addr(netdev, NETDEV_UP);
16691d5c88e3SAnirban Chakraborty 			}
1670aa43c215SJeff Kirsher 		}
1671aa43c215SJeff Kirsher 
1672aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1673aa43c215SJeff Kirsher 	}
1674aa43c215SJeff Kirsher 
1675aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1676aa43c215SJeff Kirsher 	return err;
1677aa43c215SJeff Kirsher }
1678aa43c215SJeff Kirsher 
1679aa43c215SJeff Kirsher static int
16805ad6ff9dSSony Chacko qlcnic_setup_netdev(struct qlcnic_adapter *adapter, struct net_device *netdev,
16815ad6ff9dSSony Chacko 		    int pci_using_dac)
1682aa43c215SJeff Kirsher {
1683aa43c215SJeff Kirsher 	int err;
1684aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1685aa43c215SJeff Kirsher 
168613159183SSony Chacko 	adapter->rx_csum = 1;
168779788450SSony Chacko 	adapter->ahw->mc_enabled = 0;
168813159183SSony Chacko 	adapter->ahw->max_mc_count = QLCNIC_MAX_MC_COUNT;
1689aa43c215SJeff Kirsher 
1690aa43c215SJeff Kirsher 	netdev->netdev_ops	   = &qlcnic_netdev_ops;
169113159183SSony Chacko 	netdev->watchdog_timeo     = QLCNIC_WATCHDOG_TIMEOUTVALUE * HZ;
1692aa43c215SJeff Kirsher 
1693aa43c215SJeff Kirsher 	qlcnic_change_mtu(netdev, netdev->mtu);
1694aa43c215SJeff Kirsher 
1695aa43c215SJeff Kirsher 	SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1696aa43c215SJeff Kirsher 
16977e38d04bSSony Chacko 	netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
169813159183SSony Chacko 			     NETIF_F_IPV6_CSUM | NETIF_F_GRO |
169913159183SSony Chacko 			     NETIF_F_HW_VLAN_RX);
170013159183SSony Chacko 	netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
170113159183SSony Chacko 				  NETIF_F_IPV6_CSUM);
1702aa43c215SJeff Kirsher 
170313159183SSony Chacko 	if (QLCNIC_IS_TSO_CAPABLE(adapter)) {
170413159183SSony Chacko 		netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
170513159183SSony Chacko 		netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
170613159183SSony Chacko 	}
1707aa43c215SJeff Kirsher 
170813159183SSony Chacko 	if (pci_using_dac) {
170913159183SSony Chacko 		netdev->features |= NETIF_F_HIGHDMA;
171013159183SSony Chacko 		netdev->vlan_features |= NETIF_F_HIGHDMA;
171113159183SSony Chacko 	}
1712aa43c215SJeff Kirsher 
171313159183SSony Chacko 	if (qlcnic_vlan_tx_check(adapter))
171413159183SSony Chacko 		netdev->features |= (NETIF_F_HW_VLAN_TX);
171513159183SSony Chacko 
171679788450SSony Chacko 	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
171713159183SSony Chacko 		netdev->features |= NETIF_F_LRO;
1718aa43c215SJeff Kirsher 
17197e38d04bSSony Chacko 	netdev->hw_features = netdev->features;
1720aa43c215SJeff Kirsher 	netdev->irq = adapter->msix_entries[0].vector;
1721aa43c215SJeff Kirsher 
1722aa43c215SJeff Kirsher 	err = register_netdev(netdev);
1723aa43c215SJeff Kirsher 	if (err) {
1724aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to register net device\n");
1725aa43c215SJeff Kirsher 		return err;
1726aa43c215SJeff Kirsher 	}
1727aa43c215SJeff Kirsher 
1728aa43c215SJeff Kirsher 	return 0;
1729aa43c215SJeff Kirsher }
1730aa43c215SJeff Kirsher 
17315ad6ff9dSSony Chacko static int qlcnic_set_dma_mask(struct pci_dev *pdev, int *pci_using_dac)
1732aa43c215SJeff Kirsher {
1733aa43c215SJeff Kirsher 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1734aa43c215SJeff Kirsher 			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1735aa43c215SJeff Kirsher 		*pci_using_dac = 1;
1736aa43c215SJeff Kirsher 	else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1737aa43c215SJeff Kirsher 			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1738aa43c215SJeff Kirsher 		*pci_using_dac = 0;
1739aa43c215SJeff Kirsher 	else {
1740aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1741aa43c215SJeff Kirsher 		return -EIO;
1742aa43c215SJeff Kirsher 	}
1743aa43c215SJeff Kirsher 
1744aa43c215SJeff Kirsher 	return 0;
1745aa43c215SJeff Kirsher }
1746aa43c215SJeff Kirsher 
17474be41e92SSony Chacko void qlcnic_free_tx_rings(struct qlcnic_adapter *adapter)
17484be41e92SSony Chacko {
17494be41e92SSony Chacko 	int ring;
17504be41e92SSony Chacko 	struct qlcnic_host_tx_ring *tx_ring;
17514be41e92SSony Chacko 
17524be41e92SSony Chacko 	for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
17534be41e92SSony Chacko 		tx_ring = &adapter->tx_ring[ring];
17544be41e92SSony Chacko 		if (tx_ring && tx_ring->cmd_buf_arr != NULL) {
17554be41e92SSony Chacko 			vfree(tx_ring->cmd_buf_arr);
17564be41e92SSony Chacko 			tx_ring->cmd_buf_arr = NULL;
17574be41e92SSony Chacko 		}
17584be41e92SSony Chacko 	}
17594be41e92SSony Chacko 	if (adapter->tx_ring != NULL)
17604be41e92SSony Chacko 		kfree(adapter->tx_ring);
17614be41e92SSony Chacko }
17624be41e92SSony Chacko 
17634be41e92SSony Chacko int qlcnic_alloc_tx_rings(struct qlcnic_adapter *adapter,
17644be41e92SSony Chacko 			  struct net_device *netdev)
17654be41e92SSony Chacko {
1766b2adaca9SJoe Perches 	int ring, vector, index;
17674be41e92SSony Chacko 	struct qlcnic_host_tx_ring *tx_ring;
17684be41e92SSony Chacko 	struct qlcnic_cmd_buffer *cmd_buf_arr;
17694be41e92SSony Chacko 
1770b2adaca9SJoe Perches 	tx_ring = kcalloc(adapter->max_drv_tx_rings,
1771b2adaca9SJoe Perches 			  sizeof(struct qlcnic_host_tx_ring), GFP_KERNEL);
1772b2adaca9SJoe Perches 	if (tx_ring == NULL)
17734be41e92SSony Chacko 		return -ENOMEM;
1774b2adaca9SJoe Perches 
17754be41e92SSony Chacko 	adapter->tx_ring = tx_ring;
17764be41e92SSony Chacko 
17774be41e92SSony Chacko 	for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
17784be41e92SSony Chacko 		tx_ring = &adapter->tx_ring[ring];
17794be41e92SSony Chacko 		tx_ring->num_desc = adapter->num_txd;
17804be41e92SSony Chacko 		tx_ring->txq = netdev_get_tx_queue(netdev, ring);
17814be41e92SSony Chacko 		cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
17824be41e92SSony Chacko 		if (cmd_buf_arr == NULL) {
17834be41e92SSony Chacko 			qlcnic_free_tx_rings(adapter);
17844be41e92SSony Chacko 			return -ENOMEM;
17854be41e92SSony Chacko 		}
17864be41e92SSony Chacko 		memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
17874be41e92SSony Chacko 		tx_ring->cmd_buf_arr = cmd_buf_arr;
17884be41e92SSony Chacko 	}
17894be41e92SSony Chacko 
17904be41e92SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
17914be41e92SSony Chacko 		for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
17924be41e92SSony Chacko 			tx_ring = &adapter->tx_ring[ring];
17934be41e92SSony Chacko 			tx_ring->adapter = adapter;
17944be41e92SSony Chacko 			if (adapter->flags & QLCNIC_MSIX_ENABLED) {
17954be41e92SSony Chacko 				index = adapter->max_sds_rings + ring;
17964be41e92SSony Chacko 				vector = adapter->msix_entries[index].vector;
17974be41e92SSony Chacko 				tx_ring->irq = vector;
17984be41e92SSony Chacko 			}
17994be41e92SSony Chacko 		}
18004be41e92SSony Chacko 	}
18014be41e92SSony Chacko 	return 0;
18024be41e92SSony Chacko }
18034be41e92SSony Chacko 
18046bb58bb0SBill Pemberton static int
1805aa43c215SJeff Kirsher qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1806aa43c215SJeff Kirsher {
1807aa43c215SJeff Kirsher 	struct net_device *netdev = NULL;
1808aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = NULL;
18097e2cf4feSSony Chacko 	struct qlcnic_hardware_context *ahw;
18105ad6ff9dSSony Chacko 	int err, pci_using_dac = -1;
1811c84e340aSSritej Velaga 	u32 capab2;
18127bc48646SDan Carpenter 	char board_name[QLCNIC_MAX_BOARD_NAME_LEN + 19]; /* MAC + ": " + name */
1813aa43c215SJeff Kirsher 
1814aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
1815aa43c215SJeff Kirsher 	if (err)
1816aa43c215SJeff Kirsher 		return err;
1817aa43c215SJeff Kirsher 
1818aa43c215SJeff Kirsher 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1819aa43c215SJeff Kirsher 		err = -ENODEV;
1820aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1821aa43c215SJeff Kirsher 	}
1822aa43c215SJeff Kirsher 
1823aa43c215SJeff Kirsher 	err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1824aa43c215SJeff Kirsher 	if (err)
1825aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1826aa43c215SJeff Kirsher 
1827aa43c215SJeff Kirsher 	err = pci_request_regions(pdev, qlcnic_driver_name);
1828aa43c215SJeff Kirsher 	if (err)
1829aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1830aa43c215SJeff Kirsher 
1831aa43c215SJeff Kirsher 	pci_set_master(pdev);
1832aa43c215SJeff Kirsher 	pci_enable_pcie_error_reporting(pdev);
1833aa43c215SJeff Kirsher 
18347e2cf4feSSony Chacko 	ahw = kzalloc(sizeof(struct qlcnic_hardware_context), GFP_KERNEL);
18357e2cf4feSSony Chacko 	if (!ahw)
18367e2cf4feSSony Chacko 		goto err_out_free_res;
18377e2cf4feSSony Chacko 
18387e2cf4feSSony Chacko 	if (ent->device == PCI_DEVICE_ID_QLOGIC_QLE824X) {
18397e2cf4feSSony Chacko 		ahw->hw_ops = &qlcnic_hw_ops;
18407e2cf4feSSony Chacko 		ahw->reg_tbl = (u32 *)qlcnic_reg_tbl;
184113159183SSony Chacko 	} else if (ent->device == PCI_DEVICE_ID_QLOGIC_QLE834X) {
184213159183SSony Chacko 		qlcnic_83xx_register_map(ahw);
184313159183SSony Chacko 	} else {
184413159183SSony Chacko 		goto err_out_free_hw_res;
18457e2cf4feSSony Chacko 	}
18467e2cf4feSSony Chacko 
18477e2cf4feSSony Chacko 	err = qlcnic_setup_pci_map(pdev, ahw);
18487e2cf4feSSony Chacko 	if (err)
18497e2cf4feSSony Chacko 		goto err_out_free_hw_res;
18507e2cf4feSSony Chacko 
1851aa43c215SJeff Kirsher 	netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1852aa43c215SJeff Kirsher 	if (!netdev) {
1853aa43c215SJeff Kirsher 		err = -ENOMEM;
18547e2cf4feSSony Chacko 		goto err_out_iounmap;
1855aa43c215SJeff Kirsher 	}
1856aa43c215SJeff Kirsher 
1857aa43c215SJeff Kirsher 	SET_NETDEV_DEV(netdev, &pdev->dev);
1858aa43c215SJeff Kirsher 
1859aa43c215SJeff Kirsher 	adapter = netdev_priv(netdev);
1860aa43c215SJeff Kirsher 	adapter->netdev  = netdev;
1861aa43c215SJeff Kirsher 	adapter->pdev    = pdev;
186213159183SSony Chacko 	adapter->ahw = ahw;
186313159183SSony Chacko 
186413159183SSony Chacko 	adapter->qlcnic_wq = create_singlethread_workqueue("qlcnic");
186513159183SSony Chacko 	if (adapter->qlcnic_wq == NULL) {
186613159183SSony Chacko 		dev_err(&pdev->dev, "Failed to create workqueue\n");
186713159183SSony Chacko 		goto err_out_free_netdev;
186813159183SSony Chacko 	}
1869aa43c215SJeff Kirsher 
18702dfc9671SPeter Senna Tschudin 	err = qlcnic_alloc_adapter_resources(adapter);
18712dfc9671SPeter Senna Tschudin 	if (err)
1872aa43c215SJeff Kirsher 		goto err_out_free_netdev;
1873aa43c215SJeff Kirsher 
1874aa43c215SJeff Kirsher 	adapter->dev_rst_time = jiffies;
18757e2cf4feSSony Chacko 	adapter->ahw->revision_id = pdev->revision;
1876fe1adc6bSJitendra Kalsaria 	if (qlcnic_mac_learn == FDB_MAC_LEARN)
1877fe1adc6bSJitendra Kalsaria 		adapter->fdb_mac_learn = true;
1878fe1adc6bSJitendra Kalsaria 	else if (qlcnic_mac_learn == DRV_MAC_LEARN)
1879fe1adc6bSJitendra Kalsaria 		adapter->drv_mac_learn = true;
18807e2cf4feSSony Chacko 	adapter->max_drv_tx_rings = 1;
1881aa43c215SJeff Kirsher 
1882aa43c215SJeff Kirsher 	rwlock_init(&adapter->ahw->crb_lock);
1883aa43c215SJeff Kirsher 	mutex_init(&adapter->ahw->mem_lock);
1884aa43c215SJeff Kirsher 
1885aa43c215SJeff Kirsher 	spin_lock_init(&adapter->tx_clean_lock);
1886aa43c215SJeff Kirsher 	INIT_LIST_HEAD(&adapter->mac_list);
1887aa43c215SJeff Kirsher 
18887e2cf4feSSony Chacko 	if (qlcnic_82xx_check(adapter)) {
188913159183SSony Chacko 		qlcnic_check_vf(adapter, ent);
1890aa43c215SJeff Kirsher 		adapter->portnum = adapter->ahw->pci_func;
18917e2cf4feSSony Chacko 		err = qlcnic_start_firmware(adapter);
1892aa43c215SJeff Kirsher 		if (err) {
18937e2cf4feSSony Chacko 			dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
18947e2cf4feSSony Chacko 			goto err_out_free_hw;
1895aa43c215SJeff Kirsher 		}
1896aa43c215SJeff Kirsher 
1897aa43c215SJeff Kirsher 		err = qlcnic_setup_idc_param(adapter);
1898aa43c215SJeff Kirsher 		if (err)
18997e2cf4feSSony Chacko 			goto err_out_free_hw;
1900aa43c215SJeff Kirsher 
1901aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_NEED_FLR;
190213159183SSony Chacko 	} else if (qlcnic_83xx_check(adapter)) {
190313159183SSony Chacko 		qlcnic_83xx_check_vf(adapter, ent);
190413159183SSony Chacko 		adapter->portnum = adapter->ahw->pci_func;
1905629263acSSony Chacko 		err = qlcnic_83xx_init(adapter);
1906629263acSSony Chacko 		if (err) {
1907629263acSSony Chacko 			dev_err(&pdev->dev, "%s: failed\n", __func__);
1908629263acSSony Chacko 			goto err_out_free_hw;
1909629263acSSony Chacko 		}
191013159183SSony Chacko 	} else {
191113159183SSony Chacko 		dev_err(&pdev->dev,
191213159183SSony Chacko 			"%s: failed. Please Reboot\n", __func__);
191313159183SSony Chacko 		goto err_out_free_hw;
1914aa43c215SJeff Kirsher 	}
1915aa43c215SJeff Kirsher 
1916aa43c215SJeff Kirsher 	if (qlcnic_read_mac_addr(adapter))
1917aa43c215SJeff Kirsher 		dev_warn(&pdev->dev, "failed to read mac addr\n");
1918aa43c215SJeff Kirsher 
1919aa43c215SJeff Kirsher 	if (adapter->portnum == 0) {
192022999798SSony Chacko 		qlcnic_get_board_name(adapter, board_name);
192113159183SSony Chacko 
1922aa43c215SJeff Kirsher 		pr_info("%s: %s Board Chip rev 0x%x\n",
1923aa43c215SJeff Kirsher 			module_name(THIS_MODULE),
192422999798SSony Chacko 			board_name, adapter->ahw->revision_id);
1925aa43c215SJeff Kirsher 	}
1926460374f7SHimanshu Madhani 
1927460374f7SHimanshu Madhani 	if (qlcnic_83xx_check(adapter) && !qlcnic_use_msi_x &&
1928460374f7SHimanshu Madhani 	    !!qlcnic_use_msi)
1929460374f7SHimanshu Madhani 		dev_warn(&pdev->dev,
1930460374f7SHimanshu Madhani 			 "83xx adapter do not support MSI interrupts\n");
1931460374f7SHimanshu Madhani 
19327e2cf4feSSony Chacko 	err = qlcnic_setup_intr(adapter, 0);
19339a97e705SManish chopra 	if (err) {
19349a97e705SManish chopra 		dev_err(&pdev->dev, "Failed to setup interrupt\n");
19357e2cf4feSSony Chacko 		goto err_out_disable_msi;
19369a97e705SManish chopra 	}
19377e2cf4feSSony Chacko 
193813159183SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
193913159183SSony Chacko 		err = qlcnic_83xx_setup_mbx_intr(adapter);
194013159183SSony Chacko 		if (err)
194113159183SSony Chacko 			goto err_out_disable_msi;
194213159183SSony Chacko 	}
1943aa43c215SJeff Kirsher 
1944aa43c215SJeff Kirsher 	err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1945aa43c215SJeff Kirsher 	if (err)
19467e2cf4feSSony Chacko 		goto err_out_disable_mbx_intr;
1947aa43c215SJeff Kirsher 
1948c84e340aSSritej Velaga 	if (qlcnic_82xx_check(adapter)) {
1949c84e340aSSritej Velaga 		if (ahw->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
1950c84e340aSSritej Velaga 			capab2 = QLCRD32(adapter, CRB_FW_CAPABILITIES_2);
1951c84e340aSSritej Velaga 			if (capab2 & QLCNIC_FW_CAPABILITY_2_OCBB)
1952c84e340aSSritej Velaga 				qlcnic_fw_cmd_set_drv_version(adapter);
1953c84e340aSSritej Velaga 		}
1954c84e340aSSritej Velaga 	}
1955c84e340aSSritej Velaga 
1956aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, adapter);
1957aa43c215SJeff Kirsher 
195897ee45ebSSony Chacko 	if (qlcnic_82xx_check(adapter))
195997ee45ebSSony Chacko 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
196097ee45ebSSony Chacko 				     FW_POLL_DELAY);
1961aa43c215SJeff Kirsher 
1962aa43c215SJeff Kirsher 	switch (adapter->ahw->port_type) {
1963aa43c215SJeff Kirsher 	case QLCNIC_GBE:
1964aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1965aa43c215SJeff Kirsher 				adapter->netdev->name);
1966aa43c215SJeff Kirsher 		break;
1967aa43c215SJeff Kirsher 	case QLCNIC_XGBE:
1968aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1969aa43c215SJeff Kirsher 				adapter->netdev->name);
1970aa43c215SJeff Kirsher 		break;
1971aa43c215SJeff Kirsher 	}
1972aa43c215SJeff Kirsher 
19737e2cf4feSSony Chacko 	if (qlcnic_get_act_pci_func(adapter))
19747e2cf4feSSony Chacko 		goto err_out_disable_mbx_intr;
19757e2cf4feSSony Chacko 
1976fe1adc6bSJitendra Kalsaria 	if (adapter->drv_mac_learn)
1977aa43c215SJeff Kirsher 		qlcnic_alloc_lb_filters_mem(adapter);
1978aa43c215SJeff Kirsher 
19797e2cf4feSSony Chacko 	qlcnic_add_sysfs(adapter);
1980aa43c215SJeff Kirsher 
1981aa43c215SJeff Kirsher 	return 0;
1982aa43c215SJeff Kirsher 
19837e2cf4feSSony Chacko err_out_disable_mbx_intr:
19847dd90cf1SSucheta Chakraborty 	if (qlcnic_83xx_check(adapter))
198513159183SSony Chacko 		qlcnic_83xx_free_mbx_intr(adapter);
19867e2cf4feSSony Chacko 
1987aa43c215SJeff Kirsher err_out_disable_msi:
1988aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
19897e2cf4feSSony Chacko 	qlcnic_cancel_idc_work(adapter);
1990aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
1991aa43c215SJeff Kirsher 
1992aa43c215SJeff Kirsher err_out_free_hw:
1993aa43c215SJeff Kirsher 	qlcnic_free_adapter_resources(adapter);
1994aa43c215SJeff Kirsher 
1995aa43c215SJeff Kirsher err_out_free_netdev:
1996aa43c215SJeff Kirsher 	free_netdev(netdev);
1997aa43c215SJeff Kirsher 
19987e2cf4feSSony Chacko err_out_iounmap:
19997e2cf4feSSony Chacko 	qlcnic_cleanup_pci_map(adapter);
20007e2cf4feSSony Chacko 
20017e2cf4feSSony Chacko err_out_free_hw_res:
20027e2cf4feSSony Chacko 	kfree(ahw);
20037e2cf4feSSony Chacko 
2004aa43c215SJeff Kirsher err_out_free_res:
2005aa43c215SJeff Kirsher 	pci_release_regions(pdev);
2006aa43c215SJeff Kirsher 
2007aa43c215SJeff Kirsher err_out_disable_pdev:
2008aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, NULL);
2009aa43c215SJeff Kirsher 	pci_disable_device(pdev);
2010aa43c215SJeff Kirsher 	return err;
2011aa43c215SJeff Kirsher }
2012aa43c215SJeff Kirsher 
20136bb58bb0SBill Pemberton static void qlcnic_remove(struct pci_dev *pdev)
2014aa43c215SJeff Kirsher {
2015aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
2016aa43c215SJeff Kirsher 	struct net_device *netdev;
201713159183SSony Chacko 	struct qlcnic_hardware_context *ahw;
2018aa43c215SJeff Kirsher 
2019aa43c215SJeff Kirsher 	adapter = pci_get_drvdata(pdev);
2020aa43c215SJeff Kirsher 	if (adapter == NULL)
2021aa43c215SJeff Kirsher 		return;
2022aa43c215SJeff Kirsher 
2023aa43c215SJeff Kirsher 	netdev = adapter->netdev;
2024aa43c215SJeff Kirsher 
202513159183SSony Chacko 	qlcnic_cancel_idc_work(adapter);
202613159183SSony Chacko 	ahw = adapter->ahw;
2027aa43c215SJeff Kirsher 
2028aa43c215SJeff Kirsher 	unregister_netdev(netdev);
2029aa43c215SJeff Kirsher 
203013159183SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
203113159183SSony Chacko 		qlcnic_83xx_free_mbx_intr(adapter);
20323d73b5fdSJitendra Kalsaria 		qlcnic_83xx_register_nic_idc_func(adapter, 0);
20333d73b5fdSJitendra Kalsaria 		cancel_delayed_work_sync(&adapter->idc_aen_work);
203413159183SSony Chacko 	}
203513159183SSony Chacko 
2036aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
2037aa43c215SJeff Kirsher 
2038aa43c215SJeff Kirsher 	if (adapter->npars != NULL)
2039aa43c215SJeff Kirsher 		kfree(adapter->npars);
2040aa43c215SJeff Kirsher 	if (adapter->eswitch != NULL)
2041aa43c215SJeff Kirsher 		kfree(adapter->eswitch);
2042aa43c215SJeff Kirsher 
204397ee45ebSSony Chacko 	if (qlcnic_82xx_check(adapter))
2044aa43c215SJeff Kirsher 		qlcnic_clr_all_drv_state(adapter, 0);
2045aa43c215SJeff Kirsher 
2046aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2047aa43c215SJeff Kirsher 
2048aa43c215SJeff Kirsher 	qlcnic_free_lb_filters_mem(adapter);
2049aa43c215SJeff Kirsher 
2050aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
2051aa43c215SJeff Kirsher 
205213159183SSony Chacko 	qlcnic_remove_sysfs(adapter);
2053aa43c215SJeff Kirsher 
2054aa43c215SJeff Kirsher 	qlcnic_cleanup_pci_map(adapter);
2055aa43c215SJeff Kirsher 
2056aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
2057aa43c215SJeff Kirsher 
2058aa43c215SJeff Kirsher 	pci_disable_pcie_error_reporting(pdev);
2059aa43c215SJeff Kirsher 	pci_release_regions(pdev);
2060aa43c215SJeff Kirsher 	pci_disable_device(pdev);
2061aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, NULL);
2062aa43c215SJeff Kirsher 
206313159183SSony Chacko 	if (adapter->qlcnic_wq) {
206413159183SSony Chacko 		destroy_workqueue(adapter->qlcnic_wq);
206513159183SSony Chacko 		adapter->qlcnic_wq = NULL;
206613159183SSony Chacko 	}
2067aa43c215SJeff Kirsher 	qlcnic_free_adapter_resources(adapter);
206813159183SSony Chacko 	kfree(ahw);
2069aa43c215SJeff Kirsher 	free_netdev(netdev);
2070aa43c215SJeff Kirsher }
2071aa43c215SJeff Kirsher static int __qlcnic_shutdown(struct pci_dev *pdev)
2072aa43c215SJeff Kirsher {
2073aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2074aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2075aa43c215SJeff Kirsher 	int retval;
2076aa43c215SJeff Kirsher 
2077aa43c215SJeff Kirsher 	netif_device_detach(netdev);
2078aa43c215SJeff Kirsher 
207913159183SSony Chacko 	qlcnic_cancel_idc_work(adapter);
2080aa43c215SJeff Kirsher 
2081aa43c215SJeff Kirsher 	if (netif_running(netdev))
2082aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
2083aa43c215SJeff Kirsher 
208497ee45ebSSony Chacko 	if (qlcnic_82xx_check(adapter))
2085aa43c215SJeff Kirsher 		qlcnic_clr_all_drv_state(adapter, 0);
2086aa43c215SJeff Kirsher 
2087aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2088aa43c215SJeff Kirsher 
2089aa43c215SJeff Kirsher 	retval = pci_save_state(pdev);
2090aa43c215SJeff Kirsher 	if (retval)
2091aa43c215SJeff Kirsher 		return retval;
209297ee45ebSSony Chacko 	if (qlcnic_82xx_check(adapter)) {
2093aa43c215SJeff Kirsher 		if (qlcnic_wol_supported(adapter)) {
2094aa43c215SJeff Kirsher 			pci_enable_wake(pdev, PCI_D3cold, 1);
2095aa43c215SJeff Kirsher 			pci_enable_wake(pdev, PCI_D3hot, 1);
2096aa43c215SJeff Kirsher 		}
209797ee45ebSSony Chacko 	}
2098aa43c215SJeff Kirsher 
2099aa43c215SJeff Kirsher 	return 0;
2100aa43c215SJeff Kirsher }
2101aa43c215SJeff Kirsher 
2102aa43c215SJeff Kirsher static void qlcnic_shutdown(struct pci_dev *pdev)
2103aa43c215SJeff Kirsher {
2104aa43c215SJeff Kirsher 	if (__qlcnic_shutdown(pdev))
2105aa43c215SJeff Kirsher 		return;
2106aa43c215SJeff Kirsher 
2107aa43c215SJeff Kirsher 	pci_disable_device(pdev);
2108aa43c215SJeff Kirsher }
2109aa43c215SJeff Kirsher 
2110aa43c215SJeff Kirsher #ifdef CONFIG_PM
2111aa43c215SJeff Kirsher static int
2112aa43c215SJeff Kirsher qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
2113aa43c215SJeff Kirsher {
2114aa43c215SJeff Kirsher 	int retval;
2115aa43c215SJeff Kirsher 
2116aa43c215SJeff Kirsher 	retval = __qlcnic_shutdown(pdev);
2117aa43c215SJeff Kirsher 	if (retval)
2118aa43c215SJeff Kirsher 		return retval;
2119aa43c215SJeff Kirsher 
2120aa43c215SJeff Kirsher 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
2121aa43c215SJeff Kirsher 	return 0;
2122aa43c215SJeff Kirsher }
2123aa43c215SJeff Kirsher 
2124aa43c215SJeff Kirsher static int
2125aa43c215SJeff Kirsher qlcnic_resume(struct pci_dev *pdev)
2126aa43c215SJeff Kirsher {
2127aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2128aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2129aa43c215SJeff Kirsher 	int err;
2130aa43c215SJeff Kirsher 
2131aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
2132aa43c215SJeff Kirsher 	if (err)
2133aa43c215SJeff Kirsher 		return err;
2134aa43c215SJeff Kirsher 
2135aa43c215SJeff Kirsher 	pci_set_power_state(pdev, PCI_D0);
2136aa43c215SJeff Kirsher 	pci_set_master(pdev);
2137aa43c215SJeff Kirsher 	pci_restore_state(pdev);
2138aa43c215SJeff Kirsher 
213913159183SSony Chacko 	err = qlcnic_start_firmware(adapter);
2140aa43c215SJeff Kirsher 	if (err) {
2141aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to start firmware\n");
2142aa43c215SJeff Kirsher 		return err;
2143aa43c215SJeff Kirsher 	}
2144aa43c215SJeff Kirsher 
2145aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
2146aa43c215SJeff Kirsher 		err = qlcnic_up(adapter, netdev);
2147aa43c215SJeff Kirsher 		if (err)
2148aa43c215SJeff Kirsher 			goto done;
2149aa43c215SJeff Kirsher 
2150aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
2151aa43c215SJeff Kirsher 	}
2152aa43c215SJeff Kirsher done:
2153aa43c215SJeff Kirsher 	netif_device_attach(netdev);
2154aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2155aa43c215SJeff Kirsher 	return 0;
2156aa43c215SJeff Kirsher }
2157aa43c215SJeff Kirsher #endif
2158aa43c215SJeff Kirsher 
2159aa43c215SJeff Kirsher static int qlcnic_open(struct net_device *netdev)
2160aa43c215SJeff Kirsher {
2161aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2162aa43c215SJeff Kirsher 	int err;
2163aa43c215SJeff Kirsher 
2164aa43c215SJeff Kirsher 	netif_carrier_off(netdev);
2165aa43c215SJeff Kirsher 
2166aa43c215SJeff Kirsher 	err = qlcnic_attach(adapter);
2167aa43c215SJeff Kirsher 	if (err)
2168aa43c215SJeff Kirsher 		return err;
2169aa43c215SJeff Kirsher 
2170aa43c215SJeff Kirsher 	err = __qlcnic_up(adapter, netdev);
2171aa43c215SJeff Kirsher 	if (err)
2172aa43c215SJeff Kirsher 		goto err_out;
2173aa43c215SJeff Kirsher 
2174aa43c215SJeff Kirsher 	netif_start_queue(netdev);
2175aa43c215SJeff Kirsher 
2176aa43c215SJeff Kirsher 	return 0;
2177aa43c215SJeff Kirsher 
2178aa43c215SJeff Kirsher err_out:
2179aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
2180aa43c215SJeff Kirsher 	return err;
2181aa43c215SJeff Kirsher }
2182aa43c215SJeff Kirsher 
2183aa43c215SJeff Kirsher /*
2184aa43c215SJeff Kirsher  * qlcnic_close - Disables a network interface entry point
2185aa43c215SJeff Kirsher  */
2186aa43c215SJeff Kirsher static int qlcnic_close(struct net_device *netdev)
2187aa43c215SJeff Kirsher {
2188aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2189aa43c215SJeff Kirsher 
2190aa43c215SJeff Kirsher 	__qlcnic_down(adapter, netdev);
219113159183SSony Chacko 
2192aa43c215SJeff Kirsher 	return 0;
2193aa43c215SJeff Kirsher }
2194aa43c215SJeff Kirsher 
2195aa43c215SJeff Kirsher void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
2196aa43c215SJeff Kirsher {
2197aa43c215SJeff Kirsher 	void *head;
2198aa43c215SJeff Kirsher 	int i;
219913159183SSony Chacko 	struct net_device *netdev = adapter->netdev;
220013159183SSony Chacko 	u32 filter_size = 0;
220113159183SSony Chacko 	u16 act_pci_func = 0;
2202aa43c215SJeff Kirsher 
2203aa43c215SJeff Kirsher 	if (adapter->fhash.fmax && adapter->fhash.fhead)
2204aa43c215SJeff Kirsher 		return;
2205aa43c215SJeff Kirsher 
220613159183SSony Chacko 	act_pci_func = adapter->ahw->act_pci_func;
2207aa43c215SJeff Kirsher 	spin_lock_init(&adapter->mac_learn_lock);
220853643a75SShahed Shaikh 	spin_lock_init(&adapter->rx_mac_learn_lock);
2209aa43c215SJeff Kirsher 
221013159183SSony Chacko 	if (qlcnic_82xx_check(adapter)) {
221113159183SSony Chacko 		filter_size = QLCNIC_LB_MAX_FILTERS;
221213159183SSony Chacko 		adapter->fhash.fbucket_size = QLCNIC_LB_BUCKET_SIZE;
221313159183SSony Chacko 	} else {
221413159183SSony Chacko 		filter_size = QLC_83XX_LB_MAX_FILTERS;
221513159183SSony Chacko 		adapter->fhash.fbucket_size = QLC_83XX_LB_BUCKET_SIZE;
221613159183SSony Chacko 	}
221713159183SSony Chacko 
221813159183SSony Chacko 	head = kcalloc(adapter->fhash.fbucket_size,
22191d9219ddSJitendra Kalsaria 		       sizeof(struct hlist_head), GFP_ATOMIC);
222013159183SSony Chacko 
2221aa43c215SJeff Kirsher 	if (!head)
2222aa43c215SJeff Kirsher 		return;
2223aa43c215SJeff Kirsher 
222413159183SSony Chacko 	adapter->fhash.fmax = (filter_size / act_pci_func);
2225aa43c215SJeff Kirsher 	adapter->fhash.fhead = head;
2226aa43c215SJeff Kirsher 
222713159183SSony Chacko 	netdev_info(netdev, "active nic func = %d, mac filter size=%d\n",
222813159183SSony Chacko 		    act_pci_func, adapter->fhash.fmax);
222913159183SSony Chacko 
223013159183SSony Chacko 	for (i = 0; i < adapter->fhash.fbucket_size; i++)
2231aa43c215SJeff Kirsher 		INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
223253643a75SShahed Shaikh 
223353643a75SShahed Shaikh 	adapter->rx_fhash.fbucket_size = adapter->fhash.fbucket_size;
223453643a75SShahed Shaikh 
223553643a75SShahed Shaikh 	head = kcalloc(adapter->rx_fhash.fbucket_size,
223653643a75SShahed Shaikh 		       sizeof(struct hlist_head), GFP_ATOMIC);
223753643a75SShahed Shaikh 
223853643a75SShahed Shaikh 	if (!head)
223953643a75SShahed Shaikh 		return;
224053643a75SShahed Shaikh 
224153643a75SShahed Shaikh 	adapter->rx_fhash.fmax = (filter_size / act_pci_func);
224253643a75SShahed Shaikh 	adapter->rx_fhash.fhead = head;
224353643a75SShahed Shaikh 
224453643a75SShahed Shaikh 	for (i = 0; i < adapter->rx_fhash.fbucket_size; i++)
224553643a75SShahed Shaikh 		INIT_HLIST_HEAD(&adapter->rx_fhash.fhead[i]);
2246aa43c215SJeff Kirsher }
2247aa43c215SJeff Kirsher 
2248aa43c215SJeff Kirsher static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
2249aa43c215SJeff Kirsher {
2250aa43c215SJeff Kirsher 	if (adapter->fhash.fmax && adapter->fhash.fhead)
2251aa43c215SJeff Kirsher 		kfree(adapter->fhash.fhead);
2252aa43c215SJeff Kirsher 
2253aa43c215SJeff Kirsher 	adapter->fhash.fhead = NULL;
2254aa43c215SJeff Kirsher 	adapter->fhash.fmax = 0;
225553643a75SShahed Shaikh 
225653643a75SShahed Shaikh 	if (adapter->rx_fhash.fmax && adapter->rx_fhash.fhead)
225753643a75SShahed Shaikh 		kfree(adapter->rx_fhash.fhead);
225853643a75SShahed Shaikh 
225953643a75SShahed Shaikh 	adapter->rx_fhash.fmax = 0;
226053643a75SShahed Shaikh 	adapter->rx_fhash.fhead = NULL;
2261aa43c215SJeff Kirsher }
2262aa43c215SJeff Kirsher 
2263629263acSSony Chacko int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2264aa43c215SJeff Kirsher {
2265aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
226697ee45ebSSony Chacko 	u32 temp_state, temp_val, temp = 0;
2267aa43c215SJeff Kirsher 	int rv = 0;
2268aa43c215SJeff Kirsher 
226913159183SSony Chacko 	if (qlcnic_83xx_check(adapter))
227013159183SSony Chacko 		temp = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
227113159183SSony Chacko 
227297ee45ebSSony Chacko 	if (qlcnic_82xx_check(adapter))
2273a15ebd37SHimanshu Madhani 		temp = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
2274aa43c215SJeff Kirsher 
2275aa43c215SJeff Kirsher 	temp_state = qlcnic_get_temp_state(temp);
2276aa43c215SJeff Kirsher 	temp_val = qlcnic_get_temp_val(temp);
2277aa43c215SJeff Kirsher 
2278aa43c215SJeff Kirsher 	if (temp_state == QLCNIC_TEMP_PANIC) {
2279aa43c215SJeff Kirsher 		dev_err(&netdev->dev,
2280aa43c215SJeff Kirsher 		       "Device temperature %d degrees C exceeds"
2281aa43c215SJeff Kirsher 		       " maximum allowed. Hardware has been shut down.\n",
2282aa43c215SJeff Kirsher 		       temp_val);
2283aa43c215SJeff Kirsher 		rv = 1;
2284aa43c215SJeff Kirsher 	} else if (temp_state == QLCNIC_TEMP_WARN) {
228579788450SSony Chacko 		if (adapter->ahw->temp == QLCNIC_TEMP_NORMAL) {
2286aa43c215SJeff Kirsher 			dev_err(&netdev->dev,
2287aa43c215SJeff Kirsher 			       "Device temperature %d degrees C "
2288aa43c215SJeff Kirsher 			       "exceeds operating range."
2289aa43c215SJeff Kirsher 			       " Immediate action needed.\n",
2290aa43c215SJeff Kirsher 			       temp_val);
2291aa43c215SJeff Kirsher 		}
2292aa43c215SJeff Kirsher 	} else {
229379788450SSony Chacko 		if (adapter->ahw->temp == QLCNIC_TEMP_WARN) {
2294aa43c215SJeff Kirsher 			dev_info(&netdev->dev,
2295aa43c215SJeff Kirsher 			       "Device temperature is now %d degrees C"
2296aa43c215SJeff Kirsher 			       " in normal range.\n", temp_val);
2297aa43c215SJeff Kirsher 		}
2298aa43c215SJeff Kirsher 	}
229979788450SSony Chacko 	adapter->ahw->temp = temp_state;
2300aa43c215SJeff Kirsher 	return rv;
2301aa43c215SJeff Kirsher }
2302aa43c215SJeff Kirsher 
2303aa43c215SJeff Kirsher static void qlcnic_tx_timeout(struct net_device *netdev)
2304aa43c215SJeff Kirsher {
2305aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2306aa43c215SJeff Kirsher 
2307aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2308aa43c215SJeff Kirsher 		return;
2309aa43c215SJeff Kirsher 
2310aa43c215SJeff Kirsher 	dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2311aa43c215SJeff Kirsher 
2312aa43c215SJeff Kirsher 	if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2313aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
2314aa43c215SJeff Kirsher 	else
231579788450SSony Chacko 		adapter->ahw->reset_context = 1;
2316aa43c215SJeff Kirsher }
2317aa43c215SJeff Kirsher 
2318aa43c215SJeff Kirsher static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2319aa43c215SJeff Kirsher {
2320aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2321aa43c215SJeff Kirsher 	struct net_device_stats *stats = &netdev->stats;
2322aa43c215SJeff Kirsher 
2323aa43c215SJeff Kirsher 	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2324aa43c215SJeff Kirsher 	stats->tx_packets = adapter->stats.xmitfinished;
2325aa43c215SJeff Kirsher 	stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2326aa43c215SJeff Kirsher 	stats->tx_bytes = adapter->stats.txbytes;
2327aa43c215SJeff Kirsher 	stats->rx_dropped = adapter->stats.rxdropped;
2328aa43c215SJeff Kirsher 	stats->tx_dropped = adapter->stats.txdropped;
2329aa43c215SJeff Kirsher 
2330aa43c215SJeff Kirsher 	return stats;
2331aa43c215SJeff Kirsher }
2332aa43c215SJeff Kirsher 
23337e2cf4feSSony Chacko irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *adapter)
2334aa43c215SJeff Kirsher {
2335aa43c215SJeff Kirsher 	u32 status;
2336aa43c215SJeff Kirsher 
2337aa43c215SJeff Kirsher 	status = readl(adapter->isr_int_vec);
2338aa43c215SJeff Kirsher 
233979788450SSony Chacko 	if (!(status & adapter->ahw->int_vec_bit))
2340aa43c215SJeff Kirsher 		return IRQ_NONE;
2341aa43c215SJeff Kirsher 
2342aa43c215SJeff Kirsher 	/* check interrupt state machine, to be sure */
2343aa43c215SJeff Kirsher 	status = readl(adapter->crb_int_state_reg);
2344aa43c215SJeff Kirsher 	if (!ISR_LEGACY_INT_TRIGGERED(status))
2345aa43c215SJeff Kirsher 		return IRQ_NONE;
2346aa43c215SJeff Kirsher 
2347aa43c215SJeff Kirsher 	writel(0xffffffff, adapter->tgt_status_reg);
2348aa43c215SJeff Kirsher 	/* read twice to ensure write is flushed */
2349aa43c215SJeff Kirsher 	readl(adapter->isr_int_vec);
2350aa43c215SJeff Kirsher 	readl(adapter->isr_int_vec);
2351aa43c215SJeff Kirsher 
2352aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2353aa43c215SJeff Kirsher }
2354aa43c215SJeff Kirsher 
2355aa43c215SJeff Kirsher static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2356aa43c215SJeff Kirsher {
2357aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2358aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2359aa43c215SJeff Kirsher 
2360aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
2361aa43c215SJeff Kirsher 		goto done;
2362aa43c215SJeff Kirsher 	else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2363aa43c215SJeff Kirsher 		writel(0xffffffff, adapter->tgt_status_reg);
2364aa43c215SJeff Kirsher 		goto done;
2365aa43c215SJeff Kirsher 	}
2366aa43c215SJeff Kirsher 
2367aa43c215SJeff Kirsher 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2368aa43c215SJeff Kirsher 		return IRQ_NONE;
2369aa43c215SJeff Kirsher 
2370aa43c215SJeff Kirsher done:
237179788450SSony Chacko 	adapter->ahw->diag_cnt++;
2372aa43c215SJeff Kirsher 	qlcnic_enable_int(sds_ring);
2373aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2374aa43c215SJeff Kirsher }
2375aa43c215SJeff Kirsher 
2376aa43c215SJeff Kirsher static irqreturn_t qlcnic_intr(int irq, void *data)
2377aa43c215SJeff Kirsher {
2378aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2379aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2380aa43c215SJeff Kirsher 
2381aa43c215SJeff Kirsher 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2382aa43c215SJeff Kirsher 		return IRQ_NONE;
2383aa43c215SJeff Kirsher 
2384aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2385aa43c215SJeff Kirsher 
2386aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2387aa43c215SJeff Kirsher }
2388aa43c215SJeff Kirsher 
2389aa43c215SJeff Kirsher static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2390aa43c215SJeff Kirsher {
2391aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2392aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2393aa43c215SJeff Kirsher 
2394aa43c215SJeff Kirsher 	/* clear interrupt */
2395aa43c215SJeff Kirsher 	writel(0xffffffff, adapter->tgt_status_reg);
2396aa43c215SJeff Kirsher 
2397aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2398aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2399aa43c215SJeff Kirsher }
2400aa43c215SJeff Kirsher 
2401aa43c215SJeff Kirsher static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2402aa43c215SJeff Kirsher {
2403aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2404aa43c215SJeff Kirsher 
2405aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2406aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2407aa43c215SJeff Kirsher }
2408aa43c215SJeff Kirsher 
240913159183SSony Chacko static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data)
241013159183SSony Chacko {
241113159183SSony Chacko 	struct qlcnic_host_tx_ring *tx_ring = data;
241213159183SSony Chacko 
241313159183SSony Chacko 	napi_schedule(&tx_ring->napi);
241413159183SSony Chacko 	return IRQ_HANDLED;
241513159183SSony Chacko }
241613159183SSony Chacko 
2417aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2418aa43c215SJeff Kirsher static void qlcnic_poll_controller(struct net_device *netdev)
2419aa43c215SJeff Kirsher {
2420aa43c215SJeff Kirsher 	int ring;
2421aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
2422aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2423aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2424aa43c215SJeff Kirsher 
2425aa43c215SJeff Kirsher 	disable_irq(adapter->irq);
2426aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2427aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
2428aa43c215SJeff Kirsher 		qlcnic_intr(adapter->irq, sds_ring);
2429aa43c215SJeff Kirsher 	}
2430aa43c215SJeff Kirsher 	enable_irq(adapter->irq);
2431aa43c215SJeff Kirsher }
2432aa43c215SJeff Kirsher #endif
2433aa43c215SJeff Kirsher 
2434aa43c215SJeff Kirsher static void
2435aa43c215SJeff Kirsher qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2436aa43c215SJeff Kirsher {
2437aa43c215SJeff Kirsher 	u32 val;
2438aa43c215SJeff Kirsher 
2439aa43c215SJeff Kirsher 	val = adapter->portnum & 0xf;
2440aa43c215SJeff Kirsher 	val |= encoding << 7;
2441aa43c215SJeff Kirsher 	val |= (jiffies - adapter->dev_rst_time) << 8;
2442aa43c215SJeff Kirsher 
2443a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2444aa43c215SJeff Kirsher 	adapter->dev_rst_time = jiffies;
2445aa43c215SJeff Kirsher }
2446aa43c215SJeff Kirsher 
2447aa43c215SJeff Kirsher static int
2448aa43c215SJeff Kirsher qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2449aa43c215SJeff Kirsher {
2450aa43c215SJeff Kirsher 	u32  val;
2451aa43c215SJeff Kirsher 
2452aa43c215SJeff Kirsher 	WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2453aa43c215SJeff Kirsher 			state != QLCNIC_DEV_NEED_QUISCENT);
2454aa43c215SJeff Kirsher 
2455aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2456aa43c215SJeff Kirsher 		return -EIO;
2457aa43c215SJeff Kirsher 
2458a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2459aa43c215SJeff Kirsher 
2460aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NEED_RESET)
2461aa43c215SJeff Kirsher 		QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2462aa43c215SJeff Kirsher 	else if (state == QLCNIC_DEV_NEED_QUISCENT)
2463aa43c215SJeff Kirsher 		QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2464aa43c215SJeff Kirsher 
2465a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2466aa43c215SJeff Kirsher 
2467aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2468aa43c215SJeff Kirsher 
2469aa43c215SJeff Kirsher 	return 0;
2470aa43c215SJeff Kirsher }
2471aa43c215SJeff Kirsher 
2472aa43c215SJeff Kirsher static int
2473aa43c215SJeff Kirsher qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2474aa43c215SJeff Kirsher {
2475aa43c215SJeff Kirsher 	u32  val;
2476aa43c215SJeff Kirsher 
2477aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2478aa43c215SJeff Kirsher 		return -EBUSY;
2479aa43c215SJeff Kirsher 
2480a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2481aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2482a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2483aa43c215SJeff Kirsher 
2484aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2485aa43c215SJeff Kirsher 
2486aa43c215SJeff Kirsher 	return 0;
2487aa43c215SJeff Kirsher }
2488aa43c215SJeff Kirsher 
2489aa43c215SJeff Kirsher static void
2490aa43c215SJeff Kirsher qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2491aa43c215SJeff Kirsher {
2492aa43c215SJeff Kirsher 	u32  val;
2493aa43c215SJeff Kirsher 
2494aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2495aa43c215SJeff Kirsher 		goto err;
2496aa43c215SJeff Kirsher 
2497a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2498aa43c215SJeff Kirsher 	QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2499a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2500aa43c215SJeff Kirsher 
2501aa43c215SJeff Kirsher 	if (failed) {
2502a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
2503a15ebd37SHimanshu Madhani 				    QLCNIC_DEV_FAILED);
2504aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev,
2505aa43c215SJeff Kirsher 				"Device state set to Failed. Please Reboot\n");
2506aa43c215SJeff Kirsher 	} else if (!(val & 0x11111111))
2507a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
2508a15ebd37SHimanshu Madhani 				    QLCNIC_DEV_COLD);
2509aa43c215SJeff Kirsher 
2510a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2511aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2512a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2513aa43c215SJeff Kirsher 
2514aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2515aa43c215SJeff Kirsher err:
2516aa43c215SJeff Kirsher 	adapter->fw_fail_cnt = 0;
2517aa43c215SJeff Kirsher 	adapter->flags &= ~QLCNIC_FW_HANG;
2518aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_START_FW, &adapter->state);
2519aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2520aa43c215SJeff Kirsher }
2521aa43c215SJeff Kirsher 
2522aa43c215SJeff Kirsher /* Grab api lock, before checking state */
2523aa43c215SJeff Kirsher static int
2524aa43c215SJeff Kirsher qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2525aa43c215SJeff Kirsher {
2526aa43c215SJeff Kirsher 	int act, state, active_mask;
2527a15ebd37SHimanshu Madhani 	struct qlcnic_hardware_context *ahw = adapter->ahw;
2528aa43c215SJeff Kirsher 
2529a15ebd37SHimanshu Madhani 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2530a15ebd37SHimanshu Madhani 	act = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2531aa43c215SJeff Kirsher 
2532aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2533a15ebd37SHimanshu Madhani 		active_mask = (~(1 << (ahw->pci_func * 4)));
2534aa43c215SJeff Kirsher 		act = act & active_mask;
2535aa43c215SJeff Kirsher 	}
2536aa43c215SJeff Kirsher 
2537aa43c215SJeff Kirsher 	if (((state & 0x11111111) == (act & 0x11111111)) ||
2538aa43c215SJeff Kirsher 			((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2539aa43c215SJeff Kirsher 		return 0;
2540aa43c215SJeff Kirsher 	else
2541aa43c215SJeff Kirsher 		return 1;
2542aa43c215SJeff Kirsher }
2543aa43c215SJeff Kirsher 
2544aa43c215SJeff Kirsher static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2545aa43c215SJeff Kirsher {
2546a15ebd37SHimanshu Madhani 	u32 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2547aa43c215SJeff Kirsher 
2548aa43c215SJeff Kirsher 	if (val != QLCNIC_DRV_IDC_VER) {
2549aa43c215SJeff Kirsher 		dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2550aa43c215SJeff Kirsher 			" idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2551aa43c215SJeff Kirsher 	}
2552aa43c215SJeff Kirsher 
2553aa43c215SJeff Kirsher 	return 0;
2554aa43c215SJeff Kirsher }
2555aa43c215SJeff Kirsher 
2556aa43c215SJeff Kirsher static int
2557aa43c215SJeff Kirsher qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2558aa43c215SJeff Kirsher {
2559aa43c215SJeff Kirsher 	u32 val, prev_state;
2560aa43c215SJeff Kirsher 	u8 dev_init_timeo = adapter->dev_init_timeo;
2561aa43c215SJeff Kirsher 	u8 portnum = adapter->portnum;
2562aa43c215SJeff Kirsher 	u8 ret;
2563aa43c215SJeff Kirsher 
2564aa43c215SJeff Kirsher 	if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2565aa43c215SJeff Kirsher 		return 1;
2566aa43c215SJeff Kirsher 
2567aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2568aa43c215SJeff Kirsher 		return -1;
2569aa43c215SJeff Kirsher 
2570a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2571aa43c215SJeff Kirsher 	if (!(val & (1 << (portnum * 4)))) {
2572aa43c215SJeff Kirsher 		QLC_DEV_SET_REF_CNT(val, portnum);
2573a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2574aa43c215SJeff Kirsher 	}
2575aa43c215SJeff Kirsher 
2576a15ebd37SHimanshu Madhani 	prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2577aa43c215SJeff Kirsher 	QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2578aa43c215SJeff Kirsher 
2579aa43c215SJeff Kirsher 	switch (prev_state) {
2580aa43c215SJeff Kirsher 	case QLCNIC_DEV_COLD:
2581a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
2582a15ebd37SHimanshu Madhani 				    QLCNIC_DEV_INITIALIZING);
2583a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_IDC_VER,
2584a15ebd37SHimanshu Madhani 				    QLCNIC_DRV_IDC_VER);
2585aa43c215SJeff Kirsher 		qlcnic_idc_debug_info(adapter, 0);
2586aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2587aa43c215SJeff Kirsher 		return 1;
2588aa43c215SJeff Kirsher 
2589aa43c215SJeff Kirsher 	case QLCNIC_DEV_READY:
2590aa43c215SJeff Kirsher 		ret = qlcnic_check_idc_ver(adapter);
2591aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2592aa43c215SJeff Kirsher 		return ret;
2593aa43c215SJeff Kirsher 
2594aa43c215SJeff Kirsher 	case QLCNIC_DEV_NEED_RESET:
2595a15ebd37SHimanshu Madhani 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2596aa43c215SJeff Kirsher 		QLC_DEV_SET_RST_RDY(val, portnum);
2597a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2598aa43c215SJeff Kirsher 		break;
2599aa43c215SJeff Kirsher 
2600aa43c215SJeff Kirsher 	case QLCNIC_DEV_NEED_QUISCENT:
2601a15ebd37SHimanshu Madhani 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2602aa43c215SJeff Kirsher 		QLC_DEV_SET_QSCNT_RDY(val, portnum);
2603a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2604aa43c215SJeff Kirsher 		break;
2605aa43c215SJeff Kirsher 
2606aa43c215SJeff Kirsher 	case QLCNIC_DEV_FAILED:
2607aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2608aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2609aa43c215SJeff Kirsher 		return -1;
2610aa43c215SJeff Kirsher 
2611aa43c215SJeff Kirsher 	case QLCNIC_DEV_INITIALIZING:
2612aa43c215SJeff Kirsher 	case QLCNIC_DEV_QUISCENT:
2613aa43c215SJeff Kirsher 		break;
2614aa43c215SJeff Kirsher 	}
2615aa43c215SJeff Kirsher 
2616aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2617aa43c215SJeff Kirsher 
2618aa43c215SJeff Kirsher 	do {
2619aa43c215SJeff Kirsher 		msleep(1000);
2620a15ebd37SHimanshu Madhani 		prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2621aa43c215SJeff Kirsher 
2622aa43c215SJeff Kirsher 		if (prev_state == QLCNIC_DEV_QUISCENT)
2623aa43c215SJeff Kirsher 			continue;
2624aa43c215SJeff Kirsher 	} while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2625aa43c215SJeff Kirsher 
2626aa43c215SJeff Kirsher 	if (!dev_init_timeo) {
2627aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
2628aa43c215SJeff Kirsher 			"Waiting for device to initialize timeout\n");
2629aa43c215SJeff Kirsher 		return -1;
2630aa43c215SJeff Kirsher 	}
2631aa43c215SJeff Kirsher 
2632aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2633aa43c215SJeff Kirsher 		return -1;
2634aa43c215SJeff Kirsher 
2635a15ebd37SHimanshu Madhani 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
2636aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, portnum);
2637a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2638aa43c215SJeff Kirsher 
2639aa43c215SJeff Kirsher 	ret = qlcnic_check_idc_ver(adapter);
2640aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2641aa43c215SJeff Kirsher 
2642aa43c215SJeff Kirsher 	return ret;
2643aa43c215SJeff Kirsher }
2644aa43c215SJeff Kirsher 
2645aa43c215SJeff Kirsher static void
2646aa43c215SJeff Kirsher qlcnic_fwinit_work(struct work_struct *work)
2647aa43c215SJeff Kirsher {
2648aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
2649aa43c215SJeff Kirsher 			struct qlcnic_adapter, fw_work.work);
2650aa43c215SJeff Kirsher 	u32 dev_state = 0xf;
2651aa43c215SJeff Kirsher 	u32 val;
2652aa43c215SJeff Kirsher 
2653aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2654aa43c215SJeff Kirsher 		goto err_ret;
2655aa43c215SJeff Kirsher 
2656a15ebd37SHimanshu Madhani 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2657aa43c215SJeff Kirsher 	if (dev_state == QLCNIC_DEV_QUISCENT ||
2658aa43c215SJeff Kirsher 	    dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2659aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2660aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2661aa43c215SJeff Kirsher 						FW_POLL_DELAY * 2);
2662aa43c215SJeff Kirsher 		return;
2663aa43c215SJeff Kirsher 	}
2664aa43c215SJeff Kirsher 
266579788450SSony Chacko 	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
2666aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2667aa43c215SJeff Kirsher 		goto wait_npar;
2668aa43c215SJeff Kirsher 	}
2669aa43c215SJeff Kirsher 
267016e3cf73SSritej Velaga 	if (dev_state == QLCNIC_DEV_INITIALIZING ||
267116e3cf73SSritej Velaga 	    dev_state == QLCNIC_DEV_READY) {
267216e3cf73SSritej Velaga 		dev_info(&adapter->pdev->dev, "Detected state change from "
267316e3cf73SSritej Velaga 				"DEV_NEED_RESET, skipping ack check\n");
267416e3cf73SSritej Velaga 		goto skip_ack_check;
267516e3cf73SSritej Velaga 	}
267616e3cf73SSritej Velaga 
2677aa43c215SJeff Kirsher 	if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
267816e3cf73SSritej Velaga 		dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2679aa43c215SJeff Kirsher 					adapter->reset_ack_timeo);
2680aa43c215SJeff Kirsher 		goto skip_ack_check;
2681aa43c215SJeff Kirsher 	}
2682aa43c215SJeff Kirsher 
2683aa43c215SJeff Kirsher 	if (!qlcnic_check_drv_state(adapter)) {
2684aa43c215SJeff Kirsher skip_ack_check:
2685a15ebd37SHimanshu Madhani 		dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2686aa43c215SJeff Kirsher 
2687aa43c215SJeff Kirsher 		if (dev_state == QLCNIC_DEV_NEED_RESET) {
2688a15ebd37SHimanshu Madhani 			QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
2689aa43c215SJeff Kirsher 					    QLCNIC_DEV_INITIALIZING);
2690aa43c215SJeff Kirsher 			set_bit(__QLCNIC_START_FW, &adapter->state);
2691aa43c215SJeff Kirsher 			QLCDB(adapter, DRV, "Restarting fw\n");
2692aa43c215SJeff Kirsher 			qlcnic_idc_debug_info(adapter, 0);
2693a15ebd37SHimanshu Madhani 			val = QLC_SHARED_REG_RD32(adapter,
2694a15ebd37SHimanshu Madhani 						  QLCNIC_CRB_DRV_STATE);
2695aa43c215SJeff Kirsher 			QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2696a15ebd37SHimanshu Madhani 			QLC_SHARED_REG_WR32(adapter,
2697a15ebd37SHimanshu Madhani 					    QLCNIC_CRB_DRV_STATE, val);
2698aa43c215SJeff Kirsher 		}
2699aa43c215SJeff Kirsher 
2700aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2701aa43c215SJeff Kirsher 
2702aa43c215SJeff Kirsher 		rtnl_lock();
2703aa43c215SJeff Kirsher 		if (adapter->ahw->fw_dump.enable &&
2704aa43c215SJeff Kirsher 		    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2705aa43c215SJeff Kirsher 			QLCDB(adapter, DRV, "Take FW dump\n");
2706aa43c215SJeff Kirsher 			qlcnic_dump_fw(adapter);
2707aa43c215SJeff Kirsher 			adapter->flags |= QLCNIC_FW_HANG;
2708aa43c215SJeff Kirsher 		}
2709aa43c215SJeff Kirsher 		rtnl_unlock();
2710aa43c215SJeff Kirsher 
2711aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2712aa43c215SJeff Kirsher 		if (!adapter->nic_ops->start_firmware(adapter)) {
2713aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2714aa43c215SJeff Kirsher 			adapter->fw_wait_cnt = 0;
2715aa43c215SJeff Kirsher 			return;
2716aa43c215SJeff Kirsher 		}
2717aa43c215SJeff Kirsher 		goto err_ret;
2718aa43c215SJeff Kirsher 	}
2719aa43c215SJeff Kirsher 
2720aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2721aa43c215SJeff Kirsher 
2722aa43c215SJeff Kirsher wait_npar:
2723a15ebd37SHimanshu Madhani 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2724aa43c215SJeff Kirsher 	QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2725aa43c215SJeff Kirsher 
2726aa43c215SJeff Kirsher 	switch (dev_state) {
2727aa43c215SJeff Kirsher 	case QLCNIC_DEV_READY:
27287e2cf4feSSony Chacko 		if (!qlcnic_start_firmware(adapter)) {
2729aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2730aa43c215SJeff Kirsher 			adapter->fw_wait_cnt = 0;
2731aa43c215SJeff Kirsher 			return;
2732aa43c215SJeff Kirsher 		}
2733aa43c215SJeff Kirsher 	case QLCNIC_DEV_FAILED:
2734aa43c215SJeff Kirsher 		break;
2735aa43c215SJeff Kirsher 	default:
2736aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter,
2737aa43c215SJeff Kirsher 			qlcnic_fwinit_work, FW_POLL_DELAY);
2738aa43c215SJeff Kirsher 		return;
2739aa43c215SJeff Kirsher 	}
2740aa43c215SJeff Kirsher 
2741aa43c215SJeff Kirsher err_ret:
2742aa43c215SJeff Kirsher 	dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2743aa43c215SJeff Kirsher 		"fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2744aa43c215SJeff Kirsher 	netif_device_attach(adapter->netdev);
2745aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
2746aa43c215SJeff Kirsher }
2747aa43c215SJeff Kirsher 
2748aa43c215SJeff Kirsher static void
2749aa43c215SJeff Kirsher qlcnic_detach_work(struct work_struct *work)
2750aa43c215SJeff Kirsher {
2751aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
2752aa43c215SJeff Kirsher 			struct qlcnic_adapter, fw_work.work);
2753aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2754aa43c215SJeff Kirsher 	u32 status;
2755aa43c215SJeff Kirsher 
2756aa43c215SJeff Kirsher 	netif_device_detach(netdev);
2757aa43c215SJeff Kirsher 
2758aa43c215SJeff Kirsher 	/* Dont grab rtnl lock during Quiscent mode */
2759aa43c215SJeff Kirsher 	if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2760aa43c215SJeff Kirsher 		if (netif_running(netdev))
2761aa43c215SJeff Kirsher 			__qlcnic_down(adapter, netdev);
2762aa43c215SJeff Kirsher 	} else
2763aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
2764aa43c215SJeff Kirsher 
2765a15ebd37SHimanshu Madhani 	status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2766aa43c215SJeff Kirsher 
276744f65b29SSony Chacko 	if (status & QLCNIC_RCODE_FATAL_ERROR) {
276844f65b29SSony Chacko 		dev_err(&adapter->pdev->dev,
276944f65b29SSony Chacko 			"Detaching the device: peg halt status1=0x%x\n",
277044f65b29SSony Chacko 					status);
2771aa43c215SJeff Kirsher 
277244f65b29SSony Chacko 		if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
277344f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
277444f65b29SSony Chacko 			"On board active cooling fan failed. "
277544f65b29SSony Chacko 				"Device has been halted.\n");
277644f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
277744f65b29SSony Chacko 				"Replace the adapter.\n");
277844f65b29SSony Chacko 		}
277944f65b29SSony Chacko 
2780aa43c215SJeff Kirsher 		goto err_ret;
278144f65b29SSony Chacko 	}
278244f65b29SSony Chacko 
278379788450SSony Chacko 	if (adapter->ahw->temp == QLCNIC_TEMP_PANIC) {
278444f65b29SSony Chacko 		dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
278579788450SSony Chacko 			adapter->ahw->temp);
278644f65b29SSony Chacko 		goto err_ret;
278744f65b29SSony Chacko 	}
278844f65b29SSony Chacko 
2789aa43c215SJeff Kirsher 	/* Dont ack if this instance is the reset owner */
2790aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
279144f65b29SSony Chacko 		if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
279244f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
279344f65b29SSony Chacko 				"Failed to set driver state,"
279444f65b29SSony Chacko 					"detaching the device.\n");
2795aa43c215SJeff Kirsher 			goto err_ret;
2796aa43c215SJeff Kirsher 		}
279744f65b29SSony Chacko 	}
2798aa43c215SJeff Kirsher 
2799aa43c215SJeff Kirsher 	adapter->fw_wait_cnt = 0;
2800aa43c215SJeff Kirsher 
2801aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2802aa43c215SJeff Kirsher 
2803aa43c215SJeff Kirsher 	return;
2804aa43c215SJeff Kirsher 
2805aa43c215SJeff Kirsher err_ret:
2806aa43c215SJeff Kirsher 	netif_device_attach(netdev);
2807aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 1);
2808aa43c215SJeff Kirsher }
2809aa43c215SJeff Kirsher 
2810aa43c215SJeff Kirsher /*Transit NPAR state to NON Operational */
2811aa43c215SJeff Kirsher static void
2812aa43c215SJeff Kirsher qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2813aa43c215SJeff Kirsher {
2814aa43c215SJeff Kirsher 	u32 state;
2815aa43c215SJeff Kirsher 
2816a15ebd37SHimanshu Madhani 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2817aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NPAR_NON_OPER)
2818aa43c215SJeff Kirsher 		return;
2819aa43c215SJeff Kirsher 
2820aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2821aa43c215SJeff Kirsher 		return;
2822a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
2823a15ebd37SHimanshu Madhani 			    QLCNIC_DEV_NPAR_NON_OPER);
2824aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2825aa43c215SJeff Kirsher }
2826aa43c215SJeff Kirsher 
28277e2cf4feSSony Chacko void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *adapter, u32 key)
2828aa43c215SJeff Kirsher {
2829646779f1SSritej Velaga 	u32 state, xg_val = 0, gb_val = 0;
2830aa43c215SJeff Kirsher 
2831646779f1SSritej Velaga 	qlcnic_xg_set_xg0_mask(xg_val);
2832646779f1SSritej Velaga 	qlcnic_xg_set_xg1_mask(xg_val);
2833646779f1SSritej Velaga 	QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
2834646779f1SSritej Velaga 	qlcnic_gb_set_gb0_mask(gb_val);
2835646779f1SSritej Velaga 	qlcnic_gb_set_gb1_mask(gb_val);
2836646779f1SSritej Velaga 	qlcnic_gb_set_gb2_mask(gb_val);
2837646779f1SSritej Velaga 	qlcnic_gb_set_gb3_mask(gb_val);
2838646779f1SSritej Velaga 	QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
2839646779f1SSritej Velaga 	dev_info(&adapter->pdev->dev, "Pause control frames disabled"
2840646779f1SSritej Velaga 				" on all ports\n");
2841aa43c215SJeff Kirsher 	adapter->need_fw_reset = 1;
284213159183SSony Chacko 
2843aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2844aa43c215SJeff Kirsher 		return;
2845aa43c215SJeff Kirsher 
284613159183SSony Chacko 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2847aa43c215SJeff Kirsher 
2848aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_READY) {
284913159183SSony Chacko 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
285013159183SSony Chacko 				    QLCNIC_DEV_NEED_RESET);
2851aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_FW_RESET_OWNER;
2852aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "NEED_RESET state set\n");
2853aa43c215SJeff Kirsher 		qlcnic_idc_debug_info(adapter, 0);
2854aa43c215SJeff Kirsher 	}
2855aa43c215SJeff Kirsher 
285613159183SSony Chacko 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
285713159183SSony Chacko 			    QLCNIC_DEV_NPAR_NON_OPER);
2858aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2859aa43c215SJeff Kirsher }
2860aa43c215SJeff Kirsher 
2861aa43c215SJeff Kirsher /* Transit to NPAR READY state from NPAR NOT READY state */
2862aa43c215SJeff Kirsher static void
2863aa43c215SJeff Kirsher qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2864aa43c215SJeff Kirsher {
2865aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2866aa43c215SJeff Kirsher 		return;
2867aa43c215SJeff Kirsher 
2868a15ebd37SHimanshu Madhani 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
2869a15ebd37SHimanshu Madhani 			    QLCNIC_DEV_NPAR_OPER);
2870aa43c215SJeff Kirsher 	QLCDB(adapter, DRV, "NPAR operational state set\n");
2871aa43c215SJeff Kirsher 
2872aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2873aa43c215SJeff Kirsher }
2874aa43c215SJeff Kirsher 
287513159183SSony Chacko void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2876aa43c215SJeff Kirsher 			  work_func_t func, int delay)
2877aa43c215SJeff Kirsher {
2878aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_AER, &adapter->state))
2879aa43c215SJeff Kirsher 		return;
2880aa43c215SJeff Kirsher 
2881aa43c215SJeff Kirsher 	INIT_DELAYED_WORK(&adapter->fw_work, func);
288213159183SSony Chacko 	queue_delayed_work(adapter->qlcnic_wq, &adapter->fw_work,
2883aa43c215SJeff Kirsher 			   round_jiffies_relative(delay));
2884aa43c215SJeff Kirsher }
2885aa43c215SJeff Kirsher 
2886aa43c215SJeff Kirsher static void
2887aa43c215SJeff Kirsher qlcnic_attach_work(struct work_struct *work)
2888aa43c215SJeff Kirsher {
2889aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
2890aa43c215SJeff Kirsher 				struct qlcnic_adapter, fw_work.work);
2891aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2892aa43c215SJeff Kirsher 	u32 npar_state;
2893aa43c215SJeff Kirsher 
289479788450SSony Chacko 	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC) {
2895a15ebd37SHimanshu Madhani 		npar_state = QLC_SHARED_REG_RD32(adapter,
2896a15ebd37SHimanshu Madhani 						 QLCNIC_CRB_DEV_NPAR_STATE);
2897aa43c215SJeff Kirsher 		if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
2898aa43c215SJeff Kirsher 			qlcnic_clr_all_drv_state(adapter, 0);
2899aa43c215SJeff Kirsher 		else if (npar_state != QLCNIC_DEV_NPAR_OPER)
2900aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work,
2901aa43c215SJeff Kirsher 							FW_POLL_DELAY);
2902aa43c215SJeff Kirsher 		else
2903aa43c215SJeff Kirsher 			goto attach;
2904aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
2905aa43c215SJeff Kirsher 		return;
2906aa43c215SJeff Kirsher 	}
2907aa43c215SJeff Kirsher attach:
2908aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
2909aa43c215SJeff Kirsher 		if (qlcnic_up(adapter, netdev))
2910aa43c215SJeff Kirsher 			goto done;
2911aa43c215SJeff Kirsher 
2912aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
2913aa43c215SJeff Kirsher 	}
2914aa43c215SJeff Kirsher 
2915aa43c215SJeff Kirsher done:
2916aa43c215SJeff Kirsher 	netif_device_attach(netdev);
2917aa43c215SJeff Kirsher 	adapter->fw_fail_cnt = 0;
2918aa43c215SJeff Kirsher 	adapter->flags &= ~QLCNIC_FW_HANG;
2919aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2920aa43c215SJeff Kirsher 
2921aa43c215SJeff Kirsher 	if (!qlcnic_clr_drv_state(adapter))
2922aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2923aa43c215SJeff Kirsher 							FW_POLL_DELAY);
2924aa43c215SJeff Kirsher }
2925aa43c215SJeff Kirsher 
2926aa43c215SJeff Kirsher static int
2927aa43c215SJeff Kirsher qlcnic_check_health(struct qlcnic_adapter *adapter)
2928aa43c215SJeff Kirsher {
2929aa43c215SJeff Kirsher 	u32 state = 0, heartbeat;
2930853d4bcaSAmeen Rahman 	u32 peg_status;
2931aa43c215SJeff Kirsher 
2932aa43c215SJeff Kirsher 	if (qlcnic_check_temp(adapter))
2933aa43c215SJeff Kirsher 		goto detach;
2934aa43c215SJeff Kirsher 
2935aa43c215SJeff Kirsher 	if (adapter->need_fw_reset)
29367e2cf4feSSony Chacko 		qlcnic_dev_request_reset(adapter, 0);
2937aa43c215SJeff Kirsher 
2938a15ebd37SHimanshu Madhani 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
2939aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NEED_RESET) {
2940aa43c215SJeff Kirsher 		qlcnic_set_npar_non_operational(adapter);
2941aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
2942aa43c215SJeff Kirsher 	} else if (state == QLCNIC_DEV_NEED_QUISCENT)
2943aa43c215SJeff Kirsher 		goto detach;
2944aa43c215SJeff Kirsher 
2945a15ebd37SHimanshu Madhani 	heartbeat = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2946aa43c215SJeff Kirsher 	if (heartbeat != adapter->heartbeat) {
2947aa43c215SJeff Kirsher 		adapter->heartbeat = heartbeat;
2948aa43c215SJeff Kirsher 		adapter->fw_fail_cnt = 0;
2949aa43c215SJeff Kirsher 		if (adapter->need_fw_reset)
2950aa43c215SJeff Kirsher 			goto detach;
2951aa43c215SJeff Kirsher 
2952099f7aa7SSony Chacko 		if (adapter->ahw->reset_context && qlcnic_auto_fw_reset) {
2953aa43c215SJeff Kirsher 			qlcnic_reset_hw_context(adapter);
2954aa43c215SJeff Kirsher 			adapter->netdev->trans_start = jiffies;
2955aa43c215SJeff Kirsher 		}
2956aa43c215SJeff Kirsher 
2957aa43c215SJeff Kirsher 		return 0;
2958aa43c215SJeff Kirsher 	}
2959aa43c215SJeff Kirsher 
2960aa43c215SJeff Kirsher 	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2961aa43c215SJeff Kirsher 		return 0;
2962aa43c215SJeff Kirsher 
2963aa43c215SJeff Kirsher 	adapter->flags |= QLCNIC_FW_HANG;
2964aa43c215SJeff Kirsher 
29657e2cf4feSSony Chacko 	qlcnic_dev_request_reset(adapter, 0);
2966aa43c215SJeff Kirsher 
2967099f7aa7SSony Chacko 	if (qlcnic_auto_fw_reset)
2968aa43c215SJeff Kirsher 		clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
2969aa43c215SJeff Kirsher 
2970853d4bcaSAmeen Rahman 	dev_err(&adapter->pdev->dev, "firmware hang detected\n");
2971a15ebd37SHimanshu Madhani 	peg_status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2972853d4bcaSAmeen Rahman 	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
2973aa43c215SJeff Kirsher 			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
2974aa43c215SJeff Kirsher 			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
2975aa43c215SJeff Kirsher 			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
2976aa43c215SJeff Kirsher 			"PEG_NET_4_PC: 0x%x\n",
2977a15ebd37SHimanshu Madhani 			peg_status,
2978a15ebd37SHimanshu Madhani 			QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS2),
2979aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
2980aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
2981aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
2982aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
2983aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
298497048a1fSSritej Velaga 	if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
2985853d4bcaSAmeen Rahman 		dev_err(&adapter->pdev->dev,
2986853d4bcaSAmeen Rahman 			"Firmware aborted with error code 0x00006700. "
2987853d4bcaSAmeen Rahman 				"Device is being reset.\n");
2988aa43c215SJeff Kirsher detach:
2989aa43c215SJeff Kirsher 	adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2990aa43c215SJeff Kirsher 		QLCNIC_DEV_NEED_RESET;
2991aa43c215SJeff Kirsher 
2992099f7aa7SSony Chacko 	if (qlcnic_auto_fw_reset && !test_and_set_bit(__QLCNIC_RESETTING,
2993099f7aa7SSony Chacko 						      &adapter->state)) {
2994aa43c215SJeff Kirsher 
2995aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
2996aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2997aa43c215SJeff Kirsher 	}
2998aa43c215SJeff Kirsher 
2999aa43c215SJeff Kirsher 	return 1;
3000aa43c215SJeff Kirsher }
3001aa43c215SJeff Kirsher 
3002aa43c215SJeff Kirsher static void
3003aa43c215SJeff Kirsher qlcnic_fw_poll_work(struct work_struct *work)
3004aa43c215SJeff Kirsher {
3005aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
3006aa43c215SJeff Kirsher 				struct qlcnic_adapter, fw_work.work);
3007aa43c215SJeff Kirsher 
3008aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3009aa43c215SJeff Kirsher 		goto reschedule;
3010aa43c215SJeff Kirsher 
3011aa43c215SJeff Kirsher 
3012aa43c215SJeff Kirsher 	if (qlcnic_check_health(adapter))
3013aa43c215SJeff Kirsher 		return;
3014aa43c215SJeff Kirsher 
3015aa43c215SJeff Kirsher 	if (adapter->fhash.fnum)
3016aa43c215SJeff Kirsher 		qlcnic_prune_lb_filters(adapter);
3017aa43c215SJeff Kirsher 
3018aa43c215SJeff Kirsher reschedule:
3019aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3020aa43c215SJeff Kirsher }
3021aa43c215SJeff Kirsher 
3022aa43c215SJeff Kirsher static int qlcnic_is_first_func(struct pci_dev *pdev)
3023aa43c215SJeff Kirsher {
3024aa43c215SJeff Kirsher 	struct pci_dev *oth_pdev;
3025aa43c215SJeff Kirsher 	int val = pdev->devfn;
3026aa43c215SJeff Kirsher 
3027aa43c215SJeff Kirsher 	while (val-- > 0) {
3028aa43c215SJeff Kirsher 		oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3029aa43c215SJeff Kirsher 			(pdev->bus), pdev->bus->number,
3030aa43c215SJeff Kirsher 			PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3031aa43c215SJeff Kirsher 		if (!oth_pdev)
3032aa43c215SJeff Kirsher 			continue;
3033aa43c215SJeff Kirsher 
3034aa43c215SJeff Kirsher 		if (oth_pdev->current_state != PCI_D3cold) {
3035aa43c215SJeff Kirsher 			pci_dev_put(oth_pdev);
3036aa43c215SJeff Kirsher 			return 0;
3037aa43c215SJeff Kirsher 		}
3038aa43c215SJeff Kirsher 		pci_dev_put(oth_pdev);
3039aa43c215SJeff Kirsher 	}
3040aa43c215SJeff Kirsher 	return 1;
3041aa43c215SJeff Kirsher }
3042aa43c215SJeff Kirsher 
3043aa43c215SJeff Kirsher static int qlcnic_attach_func(struct pci_dev *pdev)
3044aa43c215SJeff Kirsher {
3045aa43c215SJeff Kirsher 	int err, first_func;
3046aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3047aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3048aa43c215SJeff Kirsher 
3049aa43c215SJeff Kirsher 	pdev->error_state = pci_channel_io_normal;
3050aa43c215SJeff Kirsher 
3051aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
3052aa43c215SJeff Kirsher 	if (err)
3053aa43c215SJeff Kirsher 		return err;
3054aa43c215SJeff Kirsher 
3055aa43c215SJeff Kirsher 	pci_set_power_state(pdev, PCI_D0);
3056aa43c215SJeff Kirsher 	pci_set_master(pdev);
3057aa43c215SJeff Kirsher 	pci_restore_state(pdev);
3058aa43c215SJeff Kirsher 
3059aa43c215SJeff Kirsher 	first_func = qlcnic_is_first_func(pdev);
3060aa43c215SJeff Kirsher 
3061aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
3062aa43c215SJeff Kirsher 		return -EINVAL;
3063aa43c215SJeff Kirsher 
306479788450SSony Chacko 	if (adapter->ahw->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3065aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
3066aa43c215SJeff Kirsher 		set_bit(__QLCNIC_START_FW, &adapter->state);
3067a15ebd37SHimanshu Madhani 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3068a15ebd37SHimanshu Madhani 				    QLCNIC_DEV_INITIALIZING);
3069aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "Restarting fw\n");
3070aa43c215SJeff Kirsher 	}
3071aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
3072aa43c215SJeff Kirsher 
307313159183SSony Chacko 	err = qlcnic_start_firmware(adapter);
3074aa43c215SJeff Kirsher 	if (err)
3075aa43c215SJeff Kirsher 		return err;
3076aa43c215SJeff Kirsher 
3077aa43c215SJeff Kirsher 	qlcnic_clr_drv_state(adapter);
307813159183SSony Chacko 	kfree(adapter->msix_entries);
307913159183SSony Chacko 	adapter->msix_entries = NULL;
308013159183SSony Chacko 	err = qlcnic_setup_intr(adapter, 0);
308113159183SSony Chacko 
30829a97e705SManish chopra 	if (err) {
30839a97e705SManish chopra 		kfree(adapter->msix_entries);
30849a97e705SManish chopra 		netdev_err(netdev, "failed to setup interrupt\n");
30859a97e705SManish chopra 		return err;
30869a97e705SManish chopra 	}
30879a97e705SManish chopra 
308813159183SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
3089d5fcff04SHimanshu Madhani 		/* register for NIC IDC AEN Events */
3090d5fcff04SHimanshu Madhani 		qlcnic_83xx_register_nic_idc_func(adapter, 1);
309113159183SSony Chacko 		err = qlcnic_83xx_setup_mbx_intr(adapter);
309213159183SSony Chacko 		if (err) {
309313159183SSony Chacko 			dev_err(&adapter->pdev->dev,
309413159183SSony Chacko 				"failed to setup mbx interrupt\n");
309513159183SSony Chacko 			qlcnic_clr_all_drv_state(adapter, 1);
309613159183SSony Chacko 			clear_bit(__QLCNIC_AER, &adapter->state);
309713159183SSony Chacko 			goto done;
309813159183SSony Chacko 		}
309913159183SSony Chacko 	}
3100aa43c215SJeff Kirsher 
3101aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
3102aa43c215SJeff Kirsher 		err = qlcnic_attach(adapter);
3103aa43c215SJeff Kirsher 		if (err) {
3104aa43c215SJeff Kirsher 			qlcnic_clr_all_drv_state(adapter, 1);
3105aa43c215SJeff Kirsher 			clear_bit(__QLCNIC_AER, &adapter->state);
3106aa43c215SJeff Kirsher 			netif_device_attach(netdev);
3107aa43c215SJeff Kirsher 			return err;
3108aa43c215SJeff Kirsher 		}
3109aa43c215SJeff Kirsher 
3110aa43c215SJeff Kirsher 		err = qlcnic_up(adapter, netdev);
3111aa43c215SJeff Kirsher 		if (err)
3112aa43c215SJeff Kirsher 			goto done;
3113aa43c215SJeff Kirsher 
3114aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3115aa43c215SJeff Kirsher 	}
3116aa43c215SJeff Kirsher  done:
3117aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3118aa43c215SJeff Kirsher 	return err;
3119aa43c215SJeff Kirsher }
3120aa43c215SJeff Kirsher 
3121aa43c215SJeff Kirsher static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3122aa43c215SJeff Kirsher 						pci_channel_state_t state)
3123aa43c215SJeff Kirsher {
3124aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3125aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3126aa43c215SJeff Kirsher 
3127aa43c215SJeff Kirsher 	if (state == pci_channel_io_perm_failure)
3128aa43c215SJeff Kirsher 		return PCI_ERS_RESULT_DISCONNECT;
3129aa43c215SJeff Kirsher 
3130aa43c215SJeff Kirsher 	if (state == pci_channel_io_normal)
3131aa43c215SJeff Kirsher 		return PCI_ERS_RESULT_RECOVERED;
3132aa43c215SJeff Kirsher 
3133aa43c215SJeff Kirsher 	set_bit(__QLCNIC_AER, &adapter->state);
3134aa43c215SJeff Kirsher 	netif_device_detach(netdev);
3135aa43c215SJeff Kirsher 
3136aa43c215SJeff Kirsher 	cancel_delayed_work_sync(&adapter->fw_work);
3137aa43c215SJeff Kirsher 
3138aa43c215SJeff Kirsher 	if (netif_running(netdev))
3139aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
3140aa43c215SJeff Kirsher 
314113159183SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
314213159183SSony Chacko 		qlcnic_83xx_free_mbx_intr(adapter);
31433d73b5fdSJitendra Kalsaria 		qlcnic_83xx_register_nic_idc_func(adapter, 0);
31443d73b5fdSJitendra Kalsaria 		cancel_delayed_work_sync(&adapter->idc_aen_work);
314513159183SSony Chacko 	}
314613159183SSony Chacko 
3147aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
3148aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
3149aa43c215SJeff Kirsher 
3150aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3151aa43c215SJeff Kirsher 
3152aa43c215SJeff Kirsher 	pci_save_state(pdev);
3153aa43c215SJeff Kirsher 	pci_disable_device(pdev);
3154aa43c215SJeff Kirsher 
3155aa43c215SJeff Kirsher 	return PCI_ERS_RESULT_NEED_RESET;
3156aa43c215SJeff Kirsher }
3157aa43c215SJeff Kirsher 
3158aa43c215SJeff Kirsher static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3159aa43c215SJeff Kirsher {
3160aa43c215SJeff Kirsher 	return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3161aa43c215SJeff Kirsher 				PCI_ERS_RESULT_RECOVERED;
3162aa43c215SJeff Kirsher }
3163aa43c215SJeff Kirsher 
3164aa43c215SJeff Kirsher static void qlcnic_io_resume(struct pci_dev *pdev)
3165aa43c215SJeff Kirsher {
3166a15ebd37SHimanshu Madhani 	u32 state;
3167aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3168aa43c215SJeff Kirsher 
3169aa43c215SJeff Kirsher 	pci_cleanup_aer_uncorrect_error_status(pdev);
3170a15ebd37SHimanshu Madhani 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3171a15ebd37SHimanshu Madhani 	if (state == QLCNIC_DEV_READY && test_and_clear_bit(__QLCNIC_AER,
3172a15ebd37SHimanshu Madhani 							    &adapter->state))
3173aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3174aa43c215SJeff Kirsher 						FW_POLL_DELAY);
3175aa43c215SJeff Kirsher }
3176aa43c215SJeff Kirsher 
3177aa43c215SJeff Kirsher static int
3178aa43c215SJeff Kirsher qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3179aa43c215SJeff Kirsher {
3180aa43c215SJeff Kirsher 	int err;
3181aa43c215SJeff Kirsher 
3182aa43c215SJeff Kirsher 	err = qlcnic_can_start_firmware(adapter);
3183aa43c215SJeff Kirsher 	if (err)
3184aa43c215SJeff Kirsher 		return err;
3185aa43c215SJeff Kirsher 
3186aa43c215SJeff Kirsher 	err = qlcnic_check_npar_opertional(adapter);
3187aa43c215SJeff Kirsher 	if (err)
3188aa43c215SJeff Kirsher 		return err;
3189aa43c215SJeff Kirsher 
3190aa43c215SJeff Kirsher 	err = qlcnic_initialize_nic(adapter);
3191aa43c215SJeff Kirsher 	if (err)
3192aa43c215SJeff Kirsher 		return err;
3193aa43c215SJeff Kirsher 
3194aa43c215SJeff Kirsher 	qlcnic_check_options(adapter);
3195aa43c215SJeff Kirsher 
3196aa43c215SJeff Kirsher 	err = qlcnic_set_eswitch_port_config(adapter);
3197aa43c215SJeff Kirsher 	if (err)
3198aa43c215SJeff Kirsher 		return err;
3199aa43c215SJeff Kirsher 
3200aa43c215SJeff Kirsher 	adapter->need_fw_reset = 0;
3201aa43c215SJeff Kirsher 
3202aa43c215SJeff Kirsher 	return err;
3203aa43c215SJeff Kirsher }
3204aa43c215SJeff Kirsher 
3205319ecf12SSony Chacko int qlcnic_validate_max_rss(u8 max_hw, u8 val)
3206aa43c215SJeff Kirsher {
3207319ecf12SSony Chacko 	u32 max_allowed;
3208319ecf12SSony Chacko 
3209319ecf12SSony Chacko 	if (max_hw > QLC_MAX_SDS_RINGS) {
3210319ecf12SSony Chacko 		max_hw = QLC_MAX_SDS_RINGS;
3211319ecf12SSony Chacko 		pr_info("max rss reset to %d\n", QLC_MAX_SDS_RINGS);
3212aa43c215SJeff Kirsher 	}
3213aa43c215SJeff Kirsher 
3214319ecf12SSony Chacko 	max_allowed = rounddown_pow_of_two(min_t(int, max_hw,
3215319ecf12SSony Chacko 						 num_online_cpus()));
3216319ecf12SSony Chacko 	if ((val > max_allowed) || (val < 2) || !is_power_of_2(val)) {
3217319ecf12SSony Chacko 		pr_info("rss_ring valid range [2 - %x] in powers of 2\n",
3218319ecf12SSony Chacko 			max_allowed);
3219aa43c215SJeff Kirsher 		return -EINVAL;
3220aa43c215SJeff Kirsher 	}
3221aa43c215SJeff Kirsher 	return 0;
3222aa43c215SJeff Kirsher }
3223aa43c215SJeff Kirsher 
3224319ecf12SSony Chacko int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data, size_t len)
3225aa43c215SJeff Kirsher {
322613159183SSony Chacko 	int err;
3227aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3228aa43c215SJeff Kirsher 
3229319ecf12SSony Chacko 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3230319ecf12SSony Chacko 		return -EBUSY;
3231319ecf12SSony Chacko 
3232aa43c215SJeff Kirsher 	netif_device_detach(netdev);
3233aa43c215SJeff Kirsher 	if (netif_running(netdev))
3234aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
323513159183SSony Chacko 
3236aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
32377dd90cf1SSucheta Chakraborty 
32387dd90cf1SSucheta Chakraborty 	if (qlcnic_83xx_check(adapter))
32397dd90cf1SSucheta Chakraborty 		qlcnic_83xx_free_mbx_intr(adapter);
32407dd90cf1SSucheta Chakraborty 
3241aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
3242319ecf12SSony Chacko 	err = qlcnic_setup_intr(adapter, data);
32439a97e705SManish chopra 	if (err) {
32449a97e705SManish chopra 		kfree(adapter->msix_entries);
32459a97e705SManish chopra 		netdev_err(netdev, "failed to setup interrupt\n");
32469a97e705SManish chopra 		return err;
32479a97e705SManish chopra 	}
3248aa43c215SJeff Kirsher 
324913159183SSony Chacko 	if (qlcnic_83xx_check(adapter)) {
3250d5fcff04SHimanshu Madhani 		/* register for NIC IDC AEN Events */
3251d5fcff04SHimanshu Madhani 		qlcnic_83xx_register_nic_idc_func(adapter, 1);
325213159183SSony Chacko 		err = qlcnic_83xx_setup_mbx_intr(adapter);
325313159183SSony Chacko 		if (err) {
325413159183SSony Chacko 			dev_err(&adapter->pdev->dev,
325513159183SSony Chacko 				"failed to setup mbx interrupt\n");
325613159183SSony Chacko 			goto done;
325713159183SSony Chacko 		}
3258aa43c215SJeff Kirsher 	}
3259aa43c215SJeff Kirsher 
3260aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
3261aa43c215SJeff Kirsher 		err = qlcnic_attach(adapter);
3262aa43c215SJeff Kirsher 		if (err)
3263aa43c215SJeff Kirsher 			goto done;
3264aa43c215SJeff Kirsher 		err = __qlcnic_up(adapter, netdev);
3265aa43c215SJeff Kirsher 		if (err)
3266aa43c215SJeff Kirsher 			goto done;
3267aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3268aa43c215SJeff Kirsher 	}
3269319ecf12SSony Chacko 	err = len;
3270aa43c215SJeff Kirsher  done:
3271aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3272aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3273aa43c215SJeff Kirsher 	return err;
3274aa43c215SJeff Kirsher }
3275aa43c215SJeff Kirsher 
3276aa43c215SJeff Kirsher #ifdef CONFIG_INET
3277aa43c215SJeff Kirsher 
3278aa43c215SJeff Kirsher #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
3279aa43c215SJeff Kirsher 
3280aa43c215SJeff Kirsher static void
3281aa43c215SJeff Kirsher qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
3282aa43c215SJeff Kirsher 			struct net_device *dev, unsigned long event)
3283aa43c215SJeff Kirsher {
3284aa43c215SJeff Kirsher 	struct in_device *indev;
3285aa43c215SJeff Kirsher 
3286aa43c215SJeff Kirsher 	indev = in_dev_get(dev);
3287aa43c215SJeff Kirsher 	if (!indev)
3288aa43c215SJeff Kirsher 		return;
3289aa43c215SJeff Kirsher 
3290aa43c215SJeff Kirsher 	for_ifa(indev) {
3291aa43c215SJeff Kirsher 		switch (event) {
3292aa43c215SJeff Kirsher 		case NETDEV_UP:
3293aa43c215SJeff Kirsher 			qlcnic_config_ipaddr(adapter,
3294aa43c215SJeff Kirsher 					ifa->ifa_address, QLCNIC_IP_UP);
3295aa43c215SJeff Kirsher 			break;
3296aa43c215SJeff Kirsher 		case NETDEV_DOWN:
3297aa43c215SJeff Kirsher 			qlcnic_config_ipaddr(adapter,
3298aa43c215SJeff Kirsher 					ifa->ifa_address, QLCNIC_IP_DOWN);
3299aa43c215SJeff Kirsher 			break;
3300aa43c215SJeff Kirsher 		default:
3301aa43c215SJeff Kirsher 			break;
3302aa43c215SJeff Kirsher 		}
3303aa43c215SJeff Kirsher 	} endfor_ifa(indev);
3304aa43c215SJeff Kirsher 
3305aa43c215SJeff Kirsher 	in_dev_put(indev);
3306aa43c215SJeff Kirsher }
3307aa43c215SJeff Kirsher 
3308319ecf12SSony Chacko void qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
3309aa43c215SJeff Kirsher {
3310aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
3311aa43c215SJeff Kirsher 	struct net_device *dev;
3312aa43c215SJeff Kirsher 	u16 vid;
3313aa43c215SJeff Kirsher 
3314aa43c215SJeff Kirsher 	qlcnic_config_indev_addr(adapter, netdev, event);
3315aa43c215SJeff Kirsher 
331643c00a75SJiri Pirko 	rcu_read_lock();
3317aa43c215SJeff Kirsher 	for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
3318aa43c215SJeff Kirsher 		dev = __vlan_find_dev_deep(netdev, vid);
3319aa43c215SJeff Kirsher 		if (!dev)
3320aa43c215SJeff Kirsher 			continue;
3321aa43c215SJeff Kirsher 		qlcnic_config_indev_addr(adapter, dev, event);
3322aa43c215SJeff Kirsher 	}
332343c00a75SJiri Pirko 	rcu_read_unlock();
3324aa43c215SJeff Kirsher }
3325aa43c215SJeff Kirsher 
3326aa43c215SJeff Kirsher static int qlcnic_netdev_event(struct notifier_block *this,
3327aa43c215SJeff Kirsher 				 unsigned long event, void *ptr)
3328aa43c215SJeff Kirsher {
3329aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
3330aa43c215SJeff Kirsher 	struct net_device *dev = (struct net_device *)ptr;
3331aa43c215SJeff Kirsher 
3332aa43c215SJeff Kirsher recheck:
3333aa43c215SJeff Kirsher 	if (dev == NULL)
3334aa43c215SJeff Kirsher 		goto done;
3335aa43c215SJeff Kirsher 
3336aa43c215SJeff Kirsher 	if (dev->priv_flags & IFF_802_1Q_VLAN) {
3337aa43c215SJeff Kirsher 		dev = vlan_dev_real_dev(dev);
3338aa43c215SJeff Kirsher 		goto recheck;
3339aa43c215SJeff Kirsher 	}
3340aa43c215SJeff Kirsher 
3341aa43c215SJeff Kirsher 	if (!is_qlcnic_netdev(dev))
3342aa43c215SJeff Kirsher 		goto done;
3343aa43c215SJeff Kirsher 
3344aa43c215SJeff Kirsher 	adapter = netdev_priv(dev);
3345aa43c215SJeff Kirsher 
3346aa43c215SJeff Kirsher 	if (!adapter)
3347aa43c215SJeff Kirsher 		goto done;
3348aa43c215SJeff Kirsher 
3349aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3350aa43c215SJeff Kirsher 		goto done;
3351aa43c215SJeff Kirsher 
3352aa43c215SJeff Kirsher 	qlcnic_config_indev_addr(adapter, dev, event);
3353aa43c215SJeff Kirsher done:
3354aa43c215SJeff Kirsher 	return NOTIFY_DONE;
3355aa43c215SJeff Kirsher }
3356aa43c215SJeff Kirsher 
3357aa43c215SJeff Kirsher static int
3358aa43c215SJeff Kirsher qlcnic_inetaddr_event(struct notifier_block *this,
3359aa43c215SJeff Kirsher 		unsigned long event, void *ptr)
3360aa43c215SJeff Kirsher {
3361aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
3362aa43c215SJeff Kirsher 	struct net_device *dev;
3363aa43c215SJeff Kirsher 
3364aa43c215SJeff Kirsher 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3365aa43c215SJeff Kirsher 
3366aa43c215SJeff Kirsher 	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3367aa43c215SJeff Kirsher 
3368aa43c215SJeff Kirsher recheck:
3369aa43c215SJeff Kirsher 	if (dev == NULL)
3370aa43c215SJeff Kirsher 		goto done;
3371aa43c215SJeff Kirsher 
3372aa43c215SJeff Kirsher 	if (dev->priv_flags & IFF_802_1Q_VLAN) {
3373aa43c215SJeff Kirsher 		dev = vlan_dev_real_dev(dev);
3374aa43c215SJeff Kirsher 		goto recheck;
3375aa43c215SJeff Kirsher 	}
3376aa43c215SJeff Kirsher 
3377aa43c215SJeff Kirsher 	if (!is_qlcnic_netdev(dev))
3378aa43c215SJeff Kirsher 		goto done;
3379aa43c215SJeff Kirsher 
3380aa43c215SJeff Kirsher 	adapter = netdev_priv(dev);
3381aa43c215SJeff Kirsher 
3382aa43c215SJeff Kirsher 	if (!adapter)
3383aa43c215SJeff Kirsher 		goto done;
3384aa43c215SJeff Kirsher 
3385aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3386aa43c215SJeff Kirsher 		goto done;
3387aa43c215SJeff Kirsher 
3388aa43c215SJeff Kirsher 	switch (event) {
3389aa43c215SJeff Kirsher 	case NETDEV_UP:
3390aa43c215SJeff Kirsher 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
339113159183SSony Chacko 
3392aa43c215SJeff Kirsher 		break;
3393aa43c215SJeff Kirsher 	case NETDEV_DOWN:
3394aa43c215SJeff Kirsher 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
339513159183SSony Chacko 
3396aa43c215SJeff Kirsher 		break;
3397aa43c215SJeff Kirsher 	default:
3398aa43c215SJeff Kirsher 		break;
3399aa43c215SJeff Kirsher 	}
3400aa43c215SJeff Kirsher 
3401aa43c215SJeff Kirsher done:
3402aa43c215SJeff Kirsher 	return NOTIFY_DONE;
3403aa43c215SJeff Kirsher }
3404aa43c215SJeff Kirsher 
3405aa43c215SJeff Kirsher static struct notifier_block	qlcnic_netdev_cb = {
3406aa43c215SJeff Kirsher 	.notifier_call = qlcnic_netdev_event,
3407aa43c215SJeff Kirsher };
3408aa43c215SJeff Kirsher 
3409aa43c215SJeff Kirsher static struct notifier_block qlcnic_inetaddr_cb = {
3410aa43c215SJeff Kirsher 	.notifier_call = qlcnic_inetaddr_event,
3411aa43c215SJeff Kirsher };
3412aa43c215SJeff Kirsher #else
3413f8ca2b6fSSony Chacko void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
3414aa43c215SJeff Kirsher { }
3415aa43c215SJeff Kirsher #endif
3416fec9dd15SStephen Hemminger static const struct pci_error_handlers qlcnic_err_handler = {
3417aa43c215SJeff Kirsher 	.error_detected = qlcnic_io_error_detected,
3418aa43c215SJeff Kirsher 	.slot_reset = qlcnic_io_slot_reset,
3419aa43c215SJeff Kirsher 	.resume = qlcnic_io_resume,
3420aa43c215SJeff Kirsher };
3421aa43c215SJeff Kirsher 
3422aa43c215SJeff Kirsher static struct pci_driver qlcnic_driver = {
3423aa43c215SJeff Kirsher 	.name = qlcnic_driver_name,
3424aa43c215SJeff Kirsher 	.id_table = qlcnic_pci_tbl,
3425aa43c215SJeff Kirsher 	.probe = qlcnic_probe,
34266bb58bb0SBill Pemberton 	.remove = qlcnic_remove,
3427aa43c215SJeff Kirsher #ifdef CONFIG_PM
3428aa43c215SJeff Kirsher 	.suspend = qlcnic_suspend,
3429aa43c215SJeff Kirsher 	.resume = qlcnic_resume,
3430aa43c215SJeff Kirsher #endif
3431aa43c215SJeff Kirsher 	.shutdown = qlcnic_shutdown,
3432aa43c215SJeff Kirsher 	.err_handler = &qlcnic_err_handler
3433aa43c215SJeff Kirsher 
3434aa43c215SJeff Kirsher };
3435aa43c215SJeff Kirsher 
3436aa43c215SJeff Kirsher static int __init qlcnic_init_module(void)
3437aa43c215SJeff Kirsher {
3438aa43c215SJeff Kirsher 	int ret;
3439aa43c215SJeff Kirsher 
3440aa43c215SJeff Kirsher 	printk(KERN_INFO "%s\n", qlcnic_driver_string);
3441aa43c215SJeff Kirsher 
3442aa43c215SJeff Kirsher #ifdef CONFIG_INET
3443aa43c215SJeff Kirsher 	register_netdevice_notifier(&qlcnic_netdev_cb);
3444aa43c215SJeff Kirsher 	register_inetaddr_notifier(&qlcnic_inetaddr_cb);
3445aa43c215SJeff Kirsher #endif
3446aa43c215SJeff Kirsher 
3447aa43c215SJeff Kirsher 	ret = pci_register_driver(&qlcnic_driver);
3448aa43c215SJeff Kirsher 	if (ret) {
3449aa43c215SJeff Kirsher #ifdef CONFIG_INET
3450aa43c215SJeff Kirsher 		unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3451aa43c215SJeff Kirsher 		unregister_netdevice_notifier(&qlcnic_netdev_cb);
3452aa43c215SJeff Kirsher #endif
3453aa43c215SJeff Kirsher 	}
3454aa43c215SJeff Kirsher 
3455aa43c215SJeff Kirsher 	return ret;
3456aa43c215SJeff Kirsher }
3457aa43c215SJeff Kirsher 
3458aa43c215SJeff Kirsher module_init(qlcnic_init_module);
3459aa43c215SJeff Kirsher 
3460aa43c215SJeff Kirsher static void __exit qlcnic_exit_module(void)
3461aa43c215SJeff Kirsher {
3462aa43c215SJeff Kirsher 	pci_unregister_driver(&qlcnic_driver);
3463aa43c215SJeff Kirsher 
3464aa43c215SJeff Kirsher #ifdef CONFIG_INET
3465aa43c215SJeff Kirsher 	unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3466aa43c215SJeff Kirsher 	unregister_netdevice_notifier(&qlcnic_netdev_cb);
3467aa43c215SJeff Kirsher #endif
3468aa43c215SJeff Kirsher }
3469aa43c215SJeff Kirsher 
3470aa43c215SJeff Kirsher module_exit(qlcnic_exit_module);
3471