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