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