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