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