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