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