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