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