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