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