xref: /linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_main.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
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/log2.h>
16 #include <linux/pci.h>
17 #include <net/vxlan.h>
18 
19 #include "qlcnic.h"
20 #include "qlcnic_sriov.h"
21 #include "qlcnic_hw.h"
22 
23 MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
24 MODULE_LICENSE("GPL");
25 MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
26 MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
27 
28 char qlcnic_driver_name[] = "qlcnic";
29 static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
30 	"Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
31 
32 static int qlcnic_mac_learn;
33 module_param(qlcnic_mac_learn, int, 0444);
34 MODULE_PARM_DESC(qlcnic_mac_learn,
35 		 "Mac Filter (0=learning is disabled, 1=Driver learning is enabled, 2=FDB learning is enabled)");
36 
37 int qlcnic_use_msi = 1;
38 MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled)");
39 module_param_named(use_msi, qlcnic_use_msi, int, 0444);
40 
41 int qlcnic_use_msi_x = 1;
42 MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled)");
43 module_param_named(use_msi_x, qlcnic_use_msi_x, int, 0444);
44 
45 int qlcnic_auto_fw_reset = 1;
46 MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled)");
47 module_param_named(auto_fw_reset, qlcnic_auto_fw_reset, int, 0644);
48 
49 int qlcnic_load_fw_file;
50 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)");
51 module_param_named(load_fw_file, qlcnic_load_fw_file, int, 0444);
52 
53 static int qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
54 static void qlcnic_remove(struct pci_dev *pdev);
55 static int qlcnic_open(struct net_device *netdev);
56 static int qlcnic_close(struct net_device *netdev);
57 static void qlcnic_tx_timeout(struct net_device *netdev, unsigned int txqueue);
58 static void qlcnic_attach_work(struct work_struct *work);
59 static void qlcnic_fwinit_work(struct work_struct *work);
60 
61 static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
62 static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
63 
64 static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
65 static irqreturn_t qlcnic_intr(int irq, void *data);
66 static irqreturn_t qlcnic_msi_intr(int irq, void *data);
67 static irqreturn_t qlcnic_msix_intr(int irq, void *data);
68 static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data);
69 
70 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
71 static int qlcnic_start_firmware(struct qlcnic_adapter *);
72 
73 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
74 static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
75 static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
76 static int qlcnic_vlan_rx_add(struct net_device *, __be16, u16);
77 static int qlcnic_vlan_rx_del(struct net_device *, __be16, u16);
78 
79 static int qlcnic_82xx_setup_intr(struct qlcnic_adapter *);
80 static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *, u32);
81 static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *);
82 static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *);
83 static int qlcnic_82xx_start_firmware(struct qlcnic_adapter *);
84 static void qlcnic_82xx_io_resume(struct pci_dev *);
85 static void qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter *);
86 static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *,
87 						      pci_channel_state_t);
88 static u32 qlcnic_vlan_tx_check(struct qlcnic_adapter *adapter)
89 {
90 	struct qlcnic_hardware_context *ahw = adapter->ahw;
91 
92 	if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X)
93 		return ahw->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX;
94 	else
95 		return 1;
96 }
97 
98 /*  PCI Device ID Table  */
99 #define ENTRY(device) \
100 	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
101 	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
102 
103 static const struct pci_device_id qlcnic_pci_tbl[] = {
104 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
105 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE834X),
106 	ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE834X),
107 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE8830),
108 	ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE8C30),
109 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE844X),
110 	ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE844X),
111 	{0,}
112 };
113 
114 MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
115 
116 
117 inline void qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring *tx_ring)
118 {
119 	writel(tx_ring->producer, tx_ring->crb_cmd_producer);
120 }
121 
122 static const u32 msi_tgt_status[8] = {
123 	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
124 	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
125 	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
126 	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
127 };
128 
129 static const u32 qlcnic_reg_tbl[] = {
130 	0x1B20A8,	/* PEG_HALT_STAT1 */
131 	0x1B20AC,	/* PEG_HALT_STAT2 */
132 	0x1B20B0,	/* FW_HEARTBEAT */
133 	0x1B2100,	/* LOCK ID */
134 	0x1B2128,	/* FW_CAPABILITIES */
135 	0x1B2138,	/* drv active */
136 	0x1B2140,	/* dev state */
137 	0x1B2144,	/* drv state */
138 	0x1B2148,	/* drv scratch */
139 	0x1B214C,	/* dev partition info */
140 	0x1B2174,	/* drv idc ver */
141 	0x1B2150,	/* fw version major */
142 	0x1B2154,	/* fw version minor */
143 	0x1B2158,	/* fw version sub */
144 	0x1B219C,	/* npar state */
145 	0x1B21FC,	/* FW_IMG_VALID */
146 	0x1B2250,	/* CMD_PEG_STATE */
147 	0x1B233C,	/* RCV_PEG_STATE */
148 	0x1B23B4,	/* ASIC TEMP */
149 	0x1B216C,	/* FW api */
150 	0x1B2170,	/* drv op mode */
151 	0x13C010,	/* flash lock */
152 	0x13C014,	/* flash unlock */
153 };
154 
155 static const struct qlcnic_board_info qlcnic_boards[] = {
156 	{ PCI_VENDOR_ID_QLOGIC,
157 	  PCI_DEVICE_ID_QLOGIC_QLE844X,
158 	  0x0,
159 	  0x0,
160 	  "8400 series 10GbE Converged Network Adapter (TCP/IP Networking)" },
161 	{ PCI_VENDOR_ID_QLOGIC,
162 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
163 	  PCI_VENDOR_ID_QLOGIC,
164 	  0x24e,
165 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
166 	  "(TCP/IP Networking)" },
167 	{ PCI_VENDOR_ID_QLOGIC,
168 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
169 	  PCI_VENDOR_ID_QLOGIC,
170 	  0x243,
171 	  "8300 Series Single Port 10GbE Converged Network Adapter "
172 	  "(TCP/IP Networking)" },
173 	{ PCI_VENDOR_ID_QLOGIC,
174 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
175 	  PCI_VENDOR_ID_QLOGIC,
176 	  0x24a,
177 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
178 	  "(TCP/IP Networking)" },
179 	{ PCI_VENDOR_ID_QLOGIC,
180 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
181 	  PCI_VENDOR_ID_QLOGIC,
182 	  0x246,
183 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
184 	  "(TCP/IP Networking)" },
185 	{ PCI_VENDOR_ID_QLOGIC,
186 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
187 	  PCI_VENDOR_ID_QLOGIC,
188 	  0x252,
189 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
190 	  "(TCP/IP Networking)" },
191 	{ PCI_VENDOR_ID_QLOGIC,
192 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
193 	  PCI_VENDOR_ID_QLOGIC,
194 	  0x26e,
195 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
196 	  "(TCP/IP Networking)" },
197 	{ PCI_VENDOR_ID_QLOGIC,
198 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
199 	  PCI_VENDOR_ID_QLOGIC,
200 	  0x260,
201 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
202 	  "(TCP/IP Networking)" },
203 	{ PCI_VENDOR_ID_QLOGIC,
204 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
205 	  PCI_VENDOR_ID_QLOGIC,
206 	  0x266,
207 	  "8300 Series Single Port 10GbE Converged Network Adapter "
208 	  "(TCP/IP Networking)" },
209 	{ PCI_VENDOR_ID_QLOGIC,
210 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
211 	  PCI_VENDOR_ID_QLOGIC,
212 	  0x269,
213 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
214 	  "(TCP/IP Networking)" },
215 	{ PCI_VENDOR_ID_QLOGIC,
216 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
217 	  PCI_VENDOR_ID_QLOGIC,
218 	  0x271,
219 	  "8300 Series Dual Port 10GbE Converged Network Adapter "
220 	  "(TCP/IP Networking)" },
221 	{ PCI_VENDOR_ID_QLOGIC,
222 	  PCI_DEVICE_ID_QLOGIC_QLE834X,
223 	  0x0, 0x0, "8300 Series 1/10GbE Controller" },
224 	{ PCI_VENDOR_ID_QLOGIC,
225 	  PCI_DEVICE_ID_QLOGIC_QLE8830,
226 	  0x0,
227 	  0x0,
228 	  "8830 Series 1/10GbE Controller" },
229 	{ PCI_VENDOR_ID_QLOGIC,
230 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
231 	  PCI_VENDOR_ID_QLOGIC,
232 	  0x203,
233 	  "8200 Series Single Port 10GbE Converged Network Adapter"
234 	  "(TCP/IP Networking)" },
235 	{ PCI_VENDOR_ID_QLOGIC,
236 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
237 	  PCI_VENDOR_ID_QLOGIC,
238 	  0x207,
239 	  "8200 Series Dual Port 10GbE Converged Network Adapter"
240 	  "(TCP/IP Networking)" },
241 	{ PCI_VENDOR_ID_QLOGIC,
242 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
243 	  PCI_VENDOR_ID_QLOGIC,
244 	  0x20b,
245 	  "3200 Series Dual Port 10Gb Intelligent Ethernet Adapter" },
246 	{ PCI_VENDOR_ID_QLOGIC,
247 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
248 	  PCI_VENDOR_ID_QLOGIC,
249 	  0x20c,
250 	  "3200 Series Quad Port 1Gb Intelligent Ethernet Adapter" },
251 	{ PCI_VENDOR_ID_QLOGIC,
252 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
253 	  PCI_VENDOR_ID_QLOGIC,
254 	  0x20f,
255 	  "3200 Series Single Port 10Gb Intelligent Ethernet Adapter" },
256 	{ PCI_VENDOR_ID_QLOGIC,
257 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
258 	  0x103c, 0x3733,
259 	  "NC523SFP 10Gb 2-port Server Adapter" },
260 	{ PCI_VENDOR_ID_QLOGIC,
261 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
262 	  0x103c, 0x3346,
263 	  "CN1000Q Dual Port Converged Network Adapter" },
264 	{ PCI_VENDOR_ID_QLOGIC,
265 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
266 	  PCI_VENDOR_ID_QLOGIC,
267 	  0x210,
268 	  "QME8242-k 10GbE Dual Port Mezzanine Card" },
269 	{ PCI_VENDOR_ID_QLOGIC,
270 	  PCI_DEVICE_ID_QLOGIC_QLE824X,
271 	  0x0, 0x0, "cLOM8214 1/10GbE Controller" },
272 };
273 
274 #define NUM_SUPPORTED_BOARDS ARRAY_SIZE(qlcnic_boards)
275 
276 static const
277 struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
278 
279 int qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
280 {
281 	int size = sizeof(struct qlcnic_host_sds_ring) * count;
282 
283 	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
284 
285 	return recv_ctx->sds_rings == NULL;
286 }
287 
288 void qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
289 {
290 	kfree(recv_ctx->sds_rings);
291 	recv_ctx->sds_rings = NULL;
292 }
293 
294 int qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
295 {
296 	struct net_device *netdev = adapter->netdev;
297 	struct pci_dev *pdev = adapter->pdev;
298 	u8 mac_addr[ETH_ALEN];
299 	int ret;
300 
301 	ret = qlcnic_get_mac_address(adapter, mac_addr,
302 				     adapter->ahw->pci_func);
303 	if (ret)
304 		return ret;
305 
306 	eth_hw_addr_set(netdev, mac_addr);
307 	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
308 
309 	/* set station address */
310 
311 	if (!is_valid_ether_addr(netdev->dev_addr))
312 		dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
313 					netdev->dev_addr);
314 
315 	return 0;
316 }
317 
318 static void qlcnic_delete_adapter_mac(struct qlcnic_adapter *adapter)
319 {
320 	struct qlcnic_mac_vlan_list *cur;
321 
322 	list_for_each_entry(cur, &adapter->mac_list, list) {
323 		if (ether_addr_equal_unaligned(adapter->mac_addr, cur->mac_addr)) {
324 			qlcnic_sre_macaddr_change(adapter, cur->mac_addr,
325 						  0, QLCNIC_MAC_DEL);
326 			list_del(&cur->list);
327 			kfree(cur);
328 			return;
329 		}
330 	}
331 }
332 
333 static int qlcnic_set_mac(struct net_device *netdev, void *p)
334 {
335 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
336 	struct sockaddr *addr = p;
337 
338 	if (qlcnic_sriov_vf_check(adapter))
339 		return -EINVAL;
340 
341 	if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
342 		return -EOPNOTSUPP;
343 
344 	if (!is_valid_ether_addr(addr->sa_data))
345 		return -EINVAL;
346 
347 	if (ether_addr_equal_unaligned(adapter->mac_addr, addr->sa_data) &&
348 	    ether_addr_equal_unaligned(netdev->dev_addr, addr->sa_data))
349 		return 0;
350 
351 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
352 		netif_device_detach(netdev);
353 		qlcnic_napi_disable(adapter);
354 	}
355 
356 	qlcnic_delete_adapter_mac(adapter);
357 	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
358 	eth_hw_addr_set(netdev, addr->sa_data);
359 	qlcnic_set_multi(adapter->netdev);
360 
361 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
362 		netif_device_attach(netdev);
363 		qlcnic_napi_enable(adapter);
364 	}
365 	return 0;
366 }
367 
368 static int qlcnic_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
369 			struct net_device *netdev,
370 			const unsigned char *addr, u16 vid,
371 			struct netlink_ext_ack *extack)
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 
2448 	ahw = kzalloc(sizeof(struct qlcnic_hardware_context), GFP_KERNEL);
2449 	if (!ahw) {
2450 		err = -ENOMEM;
2451 		goto err_out_free_res;
2452 	}
2453 
2454 	switch (ent->device) {
2455 	case PCI_DEVICE_ID_QLOGIC_QLE824X:
2456 		ahw->hw_ops = &qlcnic_hw_ops;
2457 		ahw->reg_tbl = (u32 *) qlcnic_reg_tbl;
2458 		break;
2459 	case PCI_DEVICE_ID_QLOGIC_QLE834X:
2460 	case PCI_DEVICE_ID_QLOGIC_QLE8830:
2461 	case PCI_DEVICE_ID_QLOGIC_QLE844X:
2462 		qlcnic_83xx_register_map(ahw);
2463 		break;
2464 	case PCI_DEVICE_ID_QLOGIC_VF_QLE834X:
2465 	case PCI_DEVICE_ID_QLOGIC_VF_QLE8C30:
2466 	case PCI_DEVICE_ID_QLOGIC_VF_QLE844X:
2467 		qlcnic_sriov_vf_register_map(ahw);
2468 		break;
2469 	default:
2470 		err = -EINVAL;
2471 		goto err_out_free_hw_res;
2472 	}
2473 
2474 	err = qlcnic_setup_pci_map(pdev, ahw);
2475 	if (err)
2476 		goto err_out_free_hw_res;
2477 
2478 	netdev = alloc_etherdev_mq(sizeof(struct qlcnic_adapter),
2479 				   QLCNIC_MAX_TX_RINGS);
2480 	if (!netdev) {
2481 		err = -ENOMEM;
2482 		goto err_out_iounmap;
2483 	}
2484 
2485 	SET_NETDEV_DEV(netdev, &pdev->dev);
2486 
2487 	adapter = netdev_priv(netdev);
2488 	adapter->netdev  = netdev;
2489 	adapter->pdev    = pdev;
2490 	adapter->ahw = ahw;
2491 
2492 	adapter->qlcnic_wq = create_singlethread_workqueue("qlcnic");
2493 	if (adapter->qlcnic_wq == NULL) {
2494 		err = -ENOMEM;
2495 		dev_err(&pdev->dev, "Failed to create workqueue\n");
2496 		goto err_out_free_netdev;
2497 	}
2498 
2499 	err = qlcnic_alloc_adapter_resources(adapter);
2500 	if (err)
2501 		goto err_out_free_wq;
2502 
2503 	adapter->dev_rst_time = jiffies;
2504 	ahw->revision_id = pdev->revision;
2505 	ahw->max_vnic_func = qlcnic_get_vnic_func_count(adapter);
2506 	if (qlcnic_mac_learn == FDB_MAC_LEARN)
2507 		adapter->fdb_mac_learn = true;
2508 	else if (qlcnic_mac_learn == DRV_MAC_LEARN)
2509 		adapter->drv_mac_learn = true;
2510 
2511 	rwlock_init(&adapter->ahw->crb_lock);
2512 	mutex_init(&adapter->ahw->mem_lock);
2513 
2514 	INIT_LIST_HEAD(&adapter->mac_list);
2515 
2516 	qlcnic_register_dcb(adapter);
2517 
2518 	if (qlcnic_82xx_check(adapter)) {
2519 		qlcnic_check_vf(adapter, ent);
2520 		adapter->portnum = adapter->ahw->pci_func;
2521 		qlcnic_reset_api_lock(adapter);
2522 		err = qlcnic_start_firmware(adapter);
2523 		if (err) {
2524 			dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n"
2525 				"\t\tIf reboot doesn't help, try flashing the card\n");
2526 			goto err_out_maintenance_mode;
2527 		}
2528 
2529 		/* compute and set default and max tx/sds rings */
2530 		if (adapter->ahw->msix_supported) {
2531 			if (qlcnic_check_multi_tx_capability(adapter) == 1)
2532 				qlcnic_set_tx_ring_count(adapter,
2533 							 QLCNIC_SINGLE_RING);
2534 			else
2535 				qlcnic_set_tx_ring_count(adapter,
2536 							 QLCNIC_DEF_TX_RINGS);
2537 			qlcnic_set_sds_ring_count(adapter,
2538 						  QLCNIC_DEF_SDS_RINGS);
2539 		} else {
2540 			qlcnic_set_tx_ring_count(adapter, QLCNIC_SINGLE_RING);
2541 			qlcnic_set_sds_ring_count(adapter, QLCNIC_SINGLE_RING);
2542 		}
2543 
2544 		err = qlcnic_setup_idc_param(adapter);
2545 		if (err)
2546 			goto err_out_free_hw;
2547 
2548 		adapter->flags |= QLCNIC_NEED_FLR;
2549 
2550 	} else if (qlcnic_83xx_check(adapter)) {
2551 		qlcnic_83xx_check_vf(adapter, ent);
2552 		adapter->portnum = adapter->ahw->pci_func;
2553 		err = qlcnic_83xx_init(adapter);
2554 		if (err) {
2555 			switch (err) {
2556 			case -ENOTRECOVERABLE:
2557 				dev_err(&pdev->dev, "Adapter initialization failed due to a faulty hardware\n");
2558 				dev_err(&pdev->dev, "Please replace the adapter with new one and return the faulty adapter for repair\n");
2559 				goto err_out_free_hw;
2560 			case -ENOMEM:
2561 				dev_err(&pdev->dev, "Adapter initialization failed. Please reboot\n");
2562 				goto err_out_free_hw;
2563 			case -EOPNOTSUPP:
2564 				dev_err(&pdev->dev, "Adapter initialization failed\n");
2565 				goto err_out_free_hw;
2566 			default:
2567 				dev_err(&pdev->dev, "Adapter initialization failed. Driver will load in maintenance mode to recover the adapter using the application\n");
2568 				goto err_out_maintenance_mode;
2569 			}
2570 		}
2571 
2572 		if (qlcnic_sriov_vf_check(adapter))
2573 			return 0;
2574 	} else {
2575 		dev_err(&pdev->dev,
2576 			"%s: failed. Please Reboot\n", __func__);
2577 		err = -ENODEV;
2578 		goto err_out_free_hw;
2579 	}
2580 
2581 	if (qlcnic_read_mac_addr(adapter))
2582 		dev_warn(&pdev->dev, "failed to read mac addr\n");
2583 
2584 	qlcnic_read_phys_port_id(adapter);
2585 
2586 	if (adapter->portnum == 0) {
2587 		qlcnic_get_board_name(adapter, board_name);
2588 
2589 		pr_info("%s: %s Board Chip rev 0x%x\n",
2590 			module_name(THIS_MODULE),
2591 			board_name, adapter->ahw->revision_id);
2592 	}
2593 
2594 	if (qlcnic_83xx_check(adapter) && !qlcnic_use_msi_x &&
2595 	    !!qlcnic_use_msi)
2596 		dev_warn(&pdev->dev,
2597 			 "Device does not support MSI interrupts\n");
2598 
2599 	if (qlcnic_82xx_check(adapter)) {
2600 		err = qlcnic_dcb_enable(adapter->dcb);
2601 		if (err) {
2602 			qlcnic_dcb_free(adapter->dcb);
2603 			dev_err(&pdev->dev, "Failed to enable DCB\n");
2604 			goto err_out_free_hw;
2605 		}
2606 
2607 		qlcnic_dcb_get_info(adapter->dcb);
2608 		err = qlcnic_setup_intr(adapter);
2609 
2610 		if (err) {
2611 			dev_err(&pdev->dev, "Failed to setup interrupt\n");
2612 			goto err_out_disable_msi;
2613 		}
2614 	}
2615 
2616 	err = qlcnic_get_act_pci_func(adapter);
2617 	if (err)
2618 		goto err_out_disable_mbx_intr;
2619 
2620 	if (adapter->portnum == 0)
2621 		qlcnic_set_drv_version(adapter);
2622 
2623 	err = qlcnic_setup_netdev(adapter, netdev);
2624 	if (err)
2625 		goto err_out_disable_mbx_intr;
2626 
2627 	pci_set_drvdata(pdev, adapter);
2628 
2629 	if (qlcnic_82xx_check(adapter))
2630 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2631 				     FW_POLL_DELAY);
2632 
2633 	switch (adapter->ahw->port_type) {
2634 	case QLCNIC_GBE:
2635 		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
2636 				adapter->netdev->name);
2637 		break;
2638 	case QLCNIC_XGBE:
2639 		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
2640 				adapter->netdev->name);
2641 		break;
2642 	}
2643 
2644 	if (adapter->drv_mac_learn)
2645 		qlcnic_alloc_lb_filters_mem(adapter);
2646 
2647 	qlcnic_add_sysfs(adapter);
2648 	qlcnic_register_hwmon_dev(adapter);
2649 	return 0;
2650 
2651 err_out_disable_mbx_intr:
2652 	if (qlcnic_83xx_check(adapter))
2653 		qlcnic_83xx_free_mbx_intr(adapter);
2654 
2655 err_out_disable_msi:
2656 	qlcnic_teardown_intr(adapter);
2657 	qlcnic_cancel_idc_work(adapter);
2658 	qlcnic_clr_all_drv_state(adapter, 0);
2659 
2660 err_out_free_hw:
2661 	qlcnic_free_adapter_resources(adapter);
2662 
2663 err_out_free_wq:
2664 	destroy_workqueue(adapter->qlcnic_wq);
2665 
2666 err_out_free_netdev:
2667 	free_netdev(netdev);
2668 
2669 err_out_iounmap:
2670 	qlcnic_cleanup_pci_map(ahw);
2671 
2672 err_out_free_hw_res:
2673 	kfree(ahw);
2674 
2675 err_out_free_res:
2676 	pci_release_regions(pdev);
2677 
2678 err_out_disable_pdev:
2679 	pci_disable_device(pdev);
2680 	return err;
2681 
2682 err_out_maintenance_mode:
2683 	set_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state);
2684 	netdev->netdev_ops = &qlcnic_netdev_failed_ops;
2685 	netdev->ethtool_ops = &qlcnic_ethtool_failed_ops;
2686 	ahw->port_type = QLCNIC_XGBE;
2687 
2688 	if (qlcnic_83xx_check(adapter))
2689 		adapter->tgt_status_reg = NULL;
2690 	else
2691 		ahw->board_type = QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS;
2692 
2693 	err = register_netdev(netdev);
2694 
2695 	if (err) {
2696 		dev_err(&pdev->dev, "Failed to register net device\n");
2697 		qlcnic_clr_all_drv_state(adapter, 0);
2698 		goto err_out_free_hw;
2699 	}
2700 
2701 	pci_set_drvdata(pdev, adapter);
2702 	qlcnic_add_sysfs(adapter);
2703 
2704 	return 0;
2705 }
2706 
2707 static void qlcnic_remove(struct pci_dev *pdev)
2708 {
2709 	struct qlcnic_adapter *adapter;
2710 	struct net_device *netdev;
2711 	struct qlcnic_hardware_context *ahw;
2712 
2713 	adapter = pci_get_drvdata(pdev);
2714 	if (adapter == NULL)
2715 		return;
2716 
2717 	netdev = adapter->netdev;
2718 
2719 	qlcnic_cancel_idc_work(adapter);
2720 	qlcnic_sriov_pf_disable(adapter);
2721 	ahw = adapter->ahw;
2722 
2723 	unregister_netdev(netdev);
2724 	qlcnic_sriov_cleanup(adapter);
2725 
2726 	if (qlcnic_83xx_check(adapter)) {
2727 		qlcnic_83xx_initialize_nic(adapter, 0);
2728 		cancel_delayed_work_sync(&adapter->idc_aen_work);
2729 		qlcnic_83xx_free_mbx_intr(adapter);
2730 		qlcnic_83xx_detach_mailbox_work(adapter);
2731 		qlcnic_83xx_free_mailbox(ahw->mailbox);
2732 		kfree(ahw->fw_info);
2733 	}
2734 
2735 	qlcnic_dcb_free(adapter->dcb);
2736 	qlcnic_detach(adapter);
2737 	kfree(adapter->npars);
2738 	kfree(adapter->eswitch);
2739 
2740 	if (qlcnic_82xx_check(adapter))
2741 		qlcnic_clr_all_drv_state(adapter, 0);
2742 
2743 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2744 
2745 	qlcnic_free_lb_filters_mem(adapter);
2746 
2747 	qlcnic_teardown_intr(adapter);
2748 
2749 	qlcnic_remove_sysfs(adapter);
2750 
2751 	qlcnic_unregister_hwmon_dev(adapter);
2752 
2753 	qlcnic_cleanup_pci_map(adapter->ahw);
2754 
2755 	qlcnic_release_firmware(adapter);
2756 
2757 	pci_release_regions(pdev);
2758 	pci_disable_device(pdev);
2759 
2760 	if (adapter->qlcnic_wq) {
2761 		destroy_workqueue(adapter->qlcnic_wq);
2762 		adapter->qlcnic_wq = NULL;
2763 	}
2764 
2765 	qlcnic_free_adapter_resources(adapter);
2766 	kfree(ahw);
2767 	free_netdev(netdev);
2768 }
2769 
2770 static void qlcnic_shutdown(struct pci_dev *pdev)
2771 {
2772 	if (__qlcnic_shutdown(pdev))
2773 		return;
2774 
2775 	pci_disable_device(pdev);
2776 }
2777 
2778 static int __maybe_unused qlcnic_suspend(struct device *dev_d)
2779 {
2780 	return __qlcnic_shutdown(to_pci_dev(dev_d));
2781 }
2782 
2783 static int __maybe_unused qlcnic_resume(struct device *dev_d)
2784 {
2785 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev_d);
2786 
2787 	return  __qlcnic_resume(adapter);
2788 }
2789 
2790 static int qlcnic_open(struct net_device *netdev)
2791 {
2792 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2793 	int err;
2794 
2795 	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
2796 		netdev_err(netdev, "%s: Device is in non-operational state\n",
2797 			   __func__);
2798 
2799 		return -EIO;
2800 	}
2801 
2802 	netif_carrier_off(netdev);
2803 
2804 	err = qlcnic_attach(adapter);
2805 	if (err)
2806 		return err;
2807 
2808 	err = __qlcnic_up(adapter, netdev);
2809 	if (err)
2810 		qlcnic_detach(adapter);
2811 
2812 	return err;
2813 }
2814 
2815 /*
2816  * qlcnic_close - Disables a network interface entry point
2817  */
2818 static int qlcnic_close(struct net_device *netdev)
2819 {
2820 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2821 
2822 	__qlcnic_down(adapter, netdev);
2823 
2824 	return 0;
2825 }
2826 
2827 #define QLCNIC_VF_LB_BUCKET_SIZE 1
2828 
2829 void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
2830 {
2831 	void *head;
2832 	int i;
2833 	struct net_device *netdev = adapter->netdev;
2834 	u32 filter_size = 0;
2835 	u16 act_pci_func = 0;
2836 
2837 	if (adapter->fhash.fmax && adapter->fhash.fhead)
2838 		return;
2839 
2840 	act_pci_func = adapter->ahw->total_nic_func;
2841 	spin_lock_init(&adapter->mac_learn_lock);
2842 	spin_lock_init(&adapter->rx_mac_learn_lock);
2843 
2844 	if (qlcnic_sriov_vf_check(adapter)) {
2845 		filter_size = QLCNIC_83XX_SRIOV_VF_MAX_MAC - 1;
2846 		adapter->fhash.fbucket_size = QLCNIC_VF_LB_BUCKET_SIZE;
2847 	} else if (qlcnic_82xx_check(adapter)) {
2848 		filter_size = QLCNIC_LB_MAX_FILTERS;
2849 		adapter->fhash.fbucket_size = QLCNIC_LB_BUCKET_SIZE;
2850 	} else {
2851 		filter_size = QLC_83XX_LB_MAX_FILTERS;
2852 		adapter->fhash.fbucket_size = QLC_83XX_LB_BUCKET_SIZE;
2853 	}
2854 
2855 	head = kcalloc(adapter->fhash.fbucket_size,
2856 		       sizeof(struct hlist_head), GFP_ATOMIC);
2857 
2858 	if (!head)
2859 		return;
2860 
2861 	adapter->fhash.fmax = (filter_size / act_pci_func);
2862 	adapter->fhash.fhead = head;
2863 
2864 	netdev_info(netdev, "active nic func = %d, mac filter size=%d\n",
2865 		    act_pci_func, adapter->fhash.fmax);
2866 
2867 	for (i = 0; i < adapter->fhash.fbucket_size; i++)
2868 		INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
2869 
2870 	adapter->rx_fhash.fbucket_size = adapter->fhash.fbucket_size;
2871 
2872 	head = kcalloc(adapter->rx_fhash.fbucket_size,
2873 		       sizeof(struct hlist_head), GFP_ATOMIC);
2874 
2875 	if (!head)
2876 		return;
2877 
2878 	adapter->rx_fhash.fmax = (filter_size / act_pci_func);
2879 	adapter->rx_fhash.fhead = head;
2880 
2881 	for (i = 0; i < adapter->rx_fhash.fbucket_size; i++)
2882 		INIT_HLIST_HEAD(&adapter->rx_fhash.fhead[i]);
2883 }
2884 
2885 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
2886 {
2887 	if (adapter->fhash.fmax)
2888 		kfree(adapter->fhash.fhead);
2889 
2890 	adapter->fhash.fhead = NULL;
2891 	adapter->fhash.fmax = 0;
2892 
2893 	if (adapter->rx_fhash.fmax)
2894 		kfree(adapter->rx_fhash.fhead);
2895 
2896 	adapter->rx_fhash.fmax = 0;
2897 	adapter->rx_fhash.fhead = NULL;
2898 }
2899 
2900 int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2901 {
2902 	struct net_device *netdev = adapter->netdev;
2903 	u32 temp_state, temp_val, temp = 0;
2904 	int rv = 0;
2905 
2906 	if (qlcnic_83xx_check(adapter))
2907 		temp = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
2908 
2909 	if (qlcnic_82xx_check(adapter))
2910 		temp = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
2911 
2912 	temp_state = qlcnic_get_temp_state(temp);
2913 	temp_val = qlcnic_get_temp_val(temp);
2914 
2915 	if (temp_state == QLCNIC_TEMP_PANIC) {
2916 		dev_err(&netdev->dev,
2917 		       "Device temperature %d degrees C exceeds"
2918 		       " maximum allowed. Hardware has been shut down.\n",
2919 		       temp_val);
2920 		rv = 1;
2921 	} else if (temp_state == QLCNIC_TEMP_WARN) {
2922 		if (adapter->ahw->temp == QLCNIC_TEMP_NORMAL) {
2923 			dev_err(&netdev->dev,
2924 			       "Device temperature %d degrees C "
2925 			       "exceeds operating range."
2926 			       " Immediate action needed.\n",
2927 			       temp_val);
2928 		}
2929 	} else {
2930 		if (adapter->ahw->temp == QLCNIC_TEMP_WARN) {
2931 			dev_info(&netdev->dev,
2932 			       "Device temperature is now %d degrees C"
2933 			       " in normal range.\n", temp_val);
2934 		}
2935 	}
2936 	adapter->ahw->temp = temp_state;
2937 	return rv;
2938 }
2939 
2940 static inline void dump_tx_ring_desc(struct qlcnic_host_tx_ring *tx_ring)
2941 {
2942 	int i;
2943 
2944 	for (i = 0; i < tx_ring->num_desc; i++) {
2945 		pr_info("TX Desc: %d\n", i);
2946 		print_hex_dump(KERN_INFO, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
2947 			       &tx_ring->desc_head[i],
2948 			       sizeof(struct cmd_desc_type0), true);
2949 	}
2950 }
2951 
2952 static void qlcnic_dump_rings(struct qlcnic_adapter *adapter)
2953 {
2954 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2955 	struct net_device *netdev = adapter->netdev;
2956 	struct qlcnic_host_rds_ring *rds_ring;
2957 	struct qlcnic_host_sds_ring *sds_ring;
2958 	struct qlcnic_host_tx_ring *tx_ring;
2959 	int ring;
2960 
2961 	if (!netdev || !netif_running(netdev))
2962 		return;
2963 
2964 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
2965 		rds_ring = &recv_ctx->rds_rings[ring];
2966 		if (!rds_ring)
2967 			continue;
2968 		netdev_info(netdev,
2969 			    "rds_ring=%d crb_rcv_producer=%d producer=%u num_desc=%u\n",
2970 			     ring, readl(rds_ring->crb_rcv_producer),
2971 			     rds_ring->producer, rds_ring->num_desc);
2972 	}
2973 
2974 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2975 		sds_ring = &(recv_ctx->sds_rings[ring]);
2976 		if (!sds_ring)
2977 			continue;
2978 		netdev_info(netdev,
2979 			    "sds_ring=%d crb_sts_consumer=%d consumer=%u crb_intr_mask=%d num_desc=%u\n",
2980 			    ring, readl(sds_ring->crb_sts_consumer),
2981 			    sds_ring->consumer, readl(sds_ring->crb_intr_mask),
2982 			    sds_ring->num_desc);
2983 	}
2984 
2985 	for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2986 		tx_ring = &adapter->tx_ring[ring];
2987 		if (!tx_ring)
2988 			continue;
2989 		netdev_info(netdev, "Tx ring=%d Context Id=0x%x\n",
2990 			    ring, tx_ring->ctx_id);
2991 		netdev_info(netdev,
2992 			    "xmit_finished=%llu, xmit_called=%llu, xmit_on=%llu, xmit_off=%llu\n",
2993 			    tx_ring->tx_stats.xmit_finished,
2994 			    tx_ring->tx_stats.xmit_called,
2995 			    tx_ring->tx_stats.xmit_on,
2996 			    tx_ring->tx_stats.xmit_off);
2997 
2998 		if (tx_ring->crb_intr_mask)
2999 			netdev_info(netdev, "crb_intr_mask=%d\n",
3000 				    readl(tx_ring->crb_intr_mask));
3001 
3002 		netdev_info(netdev,
3003 			    "hw_producer=%d, sw_producer=%d sw_consumer=%d, hw_consumer=%d\n",
3004 			    readl(tx_ring->crb_cmd_producer),
3005 			    tx_ring->producer, tx_ring->sw_consumer,
3006 			    le32_to_cpu(*(tx_ring->hw_consumer)));
3007 
3008 		netdev_info(netdev, "Total desc=%d, Available desc=%d\n",
3009 			    tx_ring->num_desc, qlcnic_tx_avail(tx_ring));
3010 
3011 		if (netif_msg_tx_err(adapter->ahw))
3012 			dump_tx_ring_desc(tx_ring);
3013 	}
3014 
3015 }
3016 
3017 static void qlcnic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
3018 {
3019 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
3020 
3021 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3022 		return;
3023 
3024 	qlcnic_dump_rings(adapter);
3025 
3026 	if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS ||
3027 	    netif_msg_tx_err(adapter->ahw)) {
3028 		netdev_err(netdev, "Tx timeout, reset the adapter.\n");
3029 		if (qlcnic_82xx_check(adapter))
3030 			adapter->need_fw_reset = 1;
3031 		else if (qlcnic_83xx_check(adapter))
3032 			qlcnic_83xx_idc_request_reset(adapter,
3033 						      QLCNIC_FORCE_FW_DUMP_KEY);
3034 	} else {
3035 		netdev_err(netdev, "Tx timeout, reset adapter context.\n");
3036 		adapter->ahw->reset_context = 1;
3037 	}
3038 }
3039 
3040 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
3041 {
3042 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
3043 	struct net_device_stats *stats = &netdev->stats;
3044 
3045 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
3046 		qlcnic_update_stats(adapter);
3047 
3048 	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
3049 	stats->tx_packets = adapter->stats.xmitfinished;
3050 	stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
3051 	stats->tx_bytes = adapter->stats.txbytes;
3052 	stats->rx_dropped = adapter->stats.rxdropped;
3053 	stats->tx_dropped = adapter->stats.txdropped;
3054 
3055 	return stats;
3056 }
3057 
3058 static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *adapter)
3059 {
3060 	u32 status;
3061 
3062 	status = readl(adapter->isr_int_vec);
3063 
3064 	if (!(status & adapter->ahw->int_vec_bit))
3065 		return IRQ_NONE;
3066 
3067 	/* check interrupt state machine, to be sure */
3068 	status = readl(adapter->crb_int_state_reg);
3069 	if (!ISR_LEGACY_INT_TRIGGERED(status))
3070 		return IRQ_NONE;
3071 
3072 	writel(0xffffffff, adapter->tgt_status_reg);
3073 	/* read twice to ensure write is flushed */
3074 	readl(adapter->isr_int_vec);
3075 	readl(adapter->isr_int_vec);
3076 
3077 	return IRQ_HANDLED;
3078 }
3079 
3080 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
3081 {
3082 	struct qlcnic_host_sds_ring *sds_ring = data;
3083 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3084 
3085 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
3086 		goto done;
3087 	else if (adapter->flags & QLCNIC_MSI_ENABLED) {
3088 		writel(0xffffffff, adapter->tgt_status_reg);
3089 		goto done;
3090 	}
3091 
3092 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3093 		return IRQ_NONE;
3094 
3095 done:
3096 	adapter->ahw->diag_cnt++;
3097 	qlcnic_enable_sds_intr(adapter, sds_ring);
3098 	return IRQ_HANDLED;
3099 }
3100 
3101 static irqreturn_t qlcnic_intr(int irq, void *data)
3102 {
3103 	struct qlcnic_host_sds_ring *sds_ring = data;
3104 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3105 
3106 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3107 		return IRQ_NONE;
3108 
3109 	napi_schedule(&sds_ring->napi);
3110 
3111 	return IRQ_HANDLED;
3112 }
3113 
3114 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
3115 {
3116 	struct qlcnic_host_sds_ring *sds_ring = data;
3117 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3118 
3119 	/* clear interrupt */
3120 	writel(0xffffffff, adapter->tgt_status_reg);
3121 
3122 	napi_schedule(&sds_ring->napi);
3123 	return IRQ_HANDLED;
3124 }
3125 
3126 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
3127 {
3128 	struct qlcnic_host_sds_ring *sds_ring = data;
3129 
3130 	napi_schedule(&sds_ring->napi);
3131 	return IRQ_HANDLED;
3132 }
3133 
3134 static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data)
3135 {
3136 	struct qlcnic_host_tx_ring *tx_ring = data;
3137 
3138 	napi_schedule(&tx_ring->napi);
3139 	return IRQ_HANDLED;
3140 }
3141 
3142 static void
3143 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
3144 {
3145 	u32 val;
3146 
3147 	val = adapter->portnum & 0xf;
3148 	val |= encoding << 7;
3149 	val |= (jiffies - adapter->dev_rst_time) << 8;
3150 
3151 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
3152 	adapter->dev_rst_time = jiffies;
3153 }
3154 
3155 static int
3156 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
3157 {
3158 	u32  val;
3159 
3160 	WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
3161 			state != QLCNIC_DEV_NEED_QUISCENT);
3162 
3163 	if (qlcnic_api_lock(adapter))
3164 		return -EIO;
3165 
3166 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3167 
3168 	if (state == QLCNIC_DEV_NEED_RESET)
3169 		QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3170 	else if (state == QLCNIC_DEV_NEED_QUISCENT)
3171 		QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
3172 
3173 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3174 
3175 	qlcnic_api_unlock(adapter);
3176 
3177 	return 0;
3178 }
3179 
3180 static int
3181 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
3182 {
3183 	u32  val;
3184 
3185 	if (qlcnic_api_lock(adapter))
3186 		return -EBUSY;
3187 
3188 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3189 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3190 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3191 
3192 	qlcnic_api_unlock(adapter);
3193 
3194 	return 0;
3195 }
3196 
3197 void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
3198 {
3199 	u32  val;
3200 
3201 	if (qlcnic_api_lock(adapter))
3202 		goto err;
3203 
3204 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3205 	QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
3206 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3207 
3208 	if (failed) {
3209 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3210 				    QLCNIC_DEV_FAILED);
3211 		dev_info(&adapter->pdev->dev,
3212 				"Device state set to Failed. Please Reboot\n");
3213 	} else if (!(val & 0x11111111))
3214 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3215 				    QLCNIC_DEV_COLD);
3216 
3217 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3218 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3219 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3220 
3221 	qlcnic_api_unlock(adapter);
3222 err:
3223 	adapter->fw_fail_cnt = 0;
3224 	adapter->flags &= ~QLCNIC_FW_HANG;
3225 	clear_bit(__QLCNIC_START_FW, &adapter->state);
3226 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3227 }
3228 
3229 /* Grab api lock, before checking state */
3230 static int
3231 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
3232 {
3233 	int act, state, active_mask;
3234 	struct qlcnic_hardware_context *ahw = adapter->ahw;
3235 
3236 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3237 	act = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3238 
3239 	if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
3240 		active_mask = (~(1 << (ahw->pci_func * 4)));
3241 		act = act & active_mask;
3242 	}
3243 
3244 	if (((state & 0x11111111) == (act & 0x11111111)) ||
3245 			((act & 0x11111111) == ((state >> 1) & 0x11111111)))
3246 		return 0;
3247 	else
3248 		return 1;
3249 }
3250 
3251 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
3252 {
3253 	u32 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
3254 
3255 	if (val != QLCNIC_DRV_IDC_VER) {
3256 		dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
3257 			" idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
3258 	}
3259 
3260 	return 0;
3261 }
3262 
3263 static int
3264 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
3265 {
3266 	u32 val, prev_state;
3267 	u8 dev_init_timeo = adapter->dev_init_timeo;
3268 	u8 portnum = adapter->portnum;
3269 	u8 ret;
3270 
3271 	if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
3272 		return 1;
3273 
3274 	if (qlcnic_api_lock(adapter))
3275 		return -1;
3276 
3277 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3278 	if (!(val & (1 << (portnum * 4)))) {
3279 		QLC_DEV_SET_REF_CNT(val, portnum);
3280 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3281 	}
3282 
3283 	prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3284 	QLCDB(adapter, HW, "Device state = %u\n", prev_state);
3285 
3286 	switch (prev_state) {
3287 	case QLCNIC_DEV_COLD:
3288 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3289 				    QLCNIC_DEV_INITIALIZING);
3290 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_IDC_VER,
3291 				    QLCNIC_DRV_IDC_VER);
3292 		qlcnic_idc_debug_info(adapter, 0);
3293 		qlcnic_api_unlock(adapter);
3294 		return 1;
3295 
3296 	case QLCNIC_DEV_READY:
3297 		ret = qlcnic_check_idc_ver(adapter);
3298 		qlcnic_api_unlock(adapter);
3299 		return ret;
3300 
3301 	case QLCNIC_DEV_NEED_RESET:
3302 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3303 		QLC_DEV_SET_RST_RDY(val, portnum);
3304 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3305 		break;
3306 
3307 	case QLCNIC_DEV_NEED_QUISCENT:
3308 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3309 		QLC_DEV_SET_QSCNT_RDY(val, portnum);
3310 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3311 		break;
3312 
3313 	case QLCNIC_DEV_FAILED:
3314 		dev_err(&adapter->pdev->dev, "Device in failed state.\n");
3315 		qlcnic_api_unlock(adapter);
3316 		return -1;
3317 
3318 	case QLCNIC_DEV_INITIALIZING:
3319 	case QLCNIC_DEV_QUISCENT:
3320 		break;
3321 	}
3322 
3323 	qlcnic_api_unlock(adapter);
3324 
3325 	do {
3326 		msleep(1000);
3327 		prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3328 	} while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
3329 
3330 	if (!dev_init_timeo) {
3331 		dev_err(&adapter->pdev->dev,
3332 			"Waiting for device to initialize timeout\n");
3333 		return -1;
3334 	}
3335 
3336 	if (qlcnic_api_lock(adapter))
3337 		return -1;
3338 
3339 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3340 	QLC_DEV_CLR_RST_QSCNT(val, portnum);
3341 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3342 
3343 	ret = qlcnic_check_idc_ver(adapter);
3344 	qlcnic_api_unlock(adapter);
3345 
3346 	return ret;
3347 }
3348 
3349 static void
3350 qlcnic_fwinit_work(struct work_struct *work)
3351 {
3352 	struct qlcnic_adapter *adapter = container_of(work,
3353 			struct qlcnic_adapter, fw_work.work);
3354 	u32 dev_state = 0xf;
3355 	u32 val;
3356 
3357 	if (qlcnic_api_lock(adapter))
3358 		goto err_ret;
3359 
3360 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3361 	if (dev_state == QLCNIC_DEV_QUISCENT ||
3362 	    dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3363 		qlcnic_api_unlock(adapter);
3364 		qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
3365 						FW_POLL_DELAY * 2);
3366 		return;
3367 	}
3368 
3369 	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
3370 		qlcnic_api_unlock(adapter);
3371 		goto wait_npar;
3372 	}
3373 
3374 	if (dev_state == QLCNIC_DEV_INITIALIZING ||
3375 	    dev_state == QLCNIC_DEV_READY) {
3376 		dev_info(&adapter->pdev->dev, "Detected state change from "
3377 				"DEV_NEED_RESET, skipping ack check\n");
3378 		goto skip_ack_check;
3379 	}
3380 
3381 	if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
3382 		dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
3383 					adapter->reset_ack_timeo);
3384 		goto skip_ack_check;
3385 	}
3386 
3387 	if (!qlcnic_check_drv_state(adapter)) {
3388 skip_ack_check:
3389 		dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3390 
3391 		if (dev_state == QLCNIC_DEV_NEED_RESET) {
3392 			QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3393 					    QLCNIC_DEV_INITIALIZING);
3394 			set_bit(__QLCNIC_START_FW, &adapter->state);
3395 			QLCDB(adapter, DRV, "Restarting fw\n");
3396 			qlcnic_idc_debug_info(adapter, 0);
3397 			val = QLC_SHARED_REG_RD32(adapter,
3398 						  QLCNIC_CRB_DRV_STATE);
3399 			QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3400 			QLC_SHARED_REG_WR32(adapter,
3401 					    QLCNIC_CRB_DRV_STATE, val);
3402 		}
3403 
3404 		qlcnic_api_unlock(adapter);
3405 
3406 		rtnl_lock();
3407 		if (qlcnic_check_fw_dump_state(adapter) &&
3408 		    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3409 			QLCDB(adapter, DRV, "Take FW dump\n");
3410 			qlcnic_dump_fw(adapter);
3411 			adapter->flags |= QLCNIC_FW_HANG;
3412 		}
3413 		rtnl_unlock();
3414 
3415 		adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
3416 		if (!adapter->nic_ops->start_firmware(adapter)) {
3417 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3418 			adapter->fw_wait_cnt = 0;
3419 			return;
3420 		}
3421 		goto err_ret;
3422 	}
3423 
3424 	qlcnic_api_unlock(adapter);
3425 
3426 wait_npar:
3427 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3428 	QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
3429 
3430 	switch (dev_state) {
3431 	case QLCNIC_DEV_READY:
3432 		if (!qlcnic_start_firmware(adapter)) {
3433 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3434 			adapter->fw_wait_cnt = 0;
3435 			return;
3436 		}
3437 		break;
3438 	case QLCNIC_DEV_FAILED:
3439 		break;
3440 	default:
3441 		qlcnic_schedule_work(adapter,
3442 			qlcnic_fwinit_work, FW_POLL_DELAY);
3443 		return;
3444 	}
3445 
3446 err_ret:
3447 	dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
3448 		"fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
3449 	netif_device_attach(adapter->netdev);
3450 	qlcnic_clr_all_drv_state(adapter, 0);
3451 }
3452 
3453 static void
3454 qlcnic_detach_work(struct work_struct *work)
3455 {
3456 	struct qlcnic_adapter *adapter = container_of(work,
3457 			struct qlcnic_adapter, fw_work.work);
3458 	struct net_device *netdev = adapter->netdev;
3459 	u32 status;
3460 
3461 	netif_device_detach(netdev);
3462 
3463 	/* Dont grab rtnl lock during Quiscent mode */
3464 	if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3465 		if (netif_running(netdev))
3466 			__qlcnic_down(adapter, netdev);
3467 	} else
3468 		qlcnic_down(adapter, netdev);
3469 
3470 	status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3471 
3472 	if (status & QLCNIC_RCODE_FATAL_ERROR) {
3473 		dev_err(&adapter->pdev->dev,
3474 			"Detaching the device: peg halt status1=0x%x\n",
3475 					status);
3476 
3477 		if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
3478 			dev_err(&adapter->pdev->dev,
3479 			"On board active cooling fan failed. "
3480 				"Device has been halted.\n");
3481 			dev_err(&adapter->pdev->dev,
3482 				"Replace the adapter.\n");
3483 		}
3484 
3485 		goto err_ret;
3486 	}
3487 
3488 	if (adapter->ahw->temp == QLCNIC_TEMP_PANIC) {
3489 		dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
3490 			adapter->ahw->temp);
3491 		goto err_ret;
3492 	}
3493 
3494 	/* Dont ack if this instance is the reset owner */
3495 	if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3496 		if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
3497 			dev_err(&adapter->pdev->dev,
3498 				"Failed to set driver state,"
3499 					"detaching the device.\n");
3500 			goto err_ret;
3501 		}
3502 	}
3503 
3504 	adapter->fw_wait_cnt = 0;
3505 
3506 	qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3507 
3508 	return;
3509 
3510 err_ret:
3511 	netif_device_attach(netdev);
3512 	qlcnic_clr_all_drv_state(adapter, 1);
3513 }
3514 
3515 /*Transit NPAR state to NON Operational */
3516 static void
3517 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3518 {
3519 	u32 state;
3520 
3521 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3522 	if (state == QLCNIC_DEV_NPAR_NON_OPER)
3523 		return;
3524 
3525 	if (qlcnic_api_lock(adapter))
3526 		return;
3527 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3528 			    QLCNIC_DEV_NPAR_NON_OPER);
3529 	qlcnic_api_unlock(adapter);
3530 }
3531 
3532 static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *adapter,
3533 					  u32 key)
3534 {
3535 	u32 state, xg_val = 0, gb_val = 0;
3536 
3537 	qlcnic_xg_set_xg0_mask(xg_val);
3538 	qlcnic_xg_set_xg1_mask(xg_val);
3539 	QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3540 	qlcnic_gb_set_gb0_mask(gb_val);
3541 	qlcnic_gb_set_gb1_mask(gb_val);
3542 	qlcnic_gb_set_gb2_mask(gb_val);
3543 	qlcnic_gb_set_gb3_mask(gb_val);
3544 	QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3545 	dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3546 				" on all ports\n");
3547 	adapter->need_fw_reset = 1;
3548 
3549 	if (qlcnic_api_lock(adapter))
3550 		return;
3551 
3552 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3553 
3554 	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
3555 		netdev_err(adapter->netdev, "%s: Device is in non-operational state\n",
3556 			   __func__);
3557 		qlcnic_api_unlock(adapter);
3558 
3559 		return;
3560 	}
3561 
3562 	if (state == QLCNIC_DEV_READY) {
3563 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3564 				    QLCNIC_DEV_NEED_RESET);
3565 		adapter->flags |= QLCNIC_FW_RESET_OWNER;
3566 		QLCDB(adapter, DRV, "NEED_RESET state set\n");
3567 		qlcnic_idc_debug_info(adapter, 0);
3568 	}
3569 
3570 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3571 			    QLCNIC_DEV_NPAR_NON_OPER);
3572 	qlcnic_api_unlock(adapter);
3573 }
3574 
3575 /* Transit to NPAR READY state from NPAR NOT READY state */
3576 static void
3577 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3578 {
3579 	if (qlcnic_api_lock(adapter))
3580 		return;
3581 
3582 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3583 			    QLCNIC_DEV_NPAR_OPER);
3584 	QLCDB(adapter, DRV, "NPAR operational state set\n");
3585 
3586 	qlcnic_api_unlock(adapter);
3587 }
3588 
3589 void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3590 			  work_func_t func, int delay)
3591 {
3592 	if (test_bit(__QLCNIC_AER, &adapter->state))
3593 		return;
3594 
3595 	INIT_DELAYED_WORK(&adapter->fw_work, func);
3596 	queue_delayed_work(adapter->qlcnic_wq, &adapter->fw_work,
3597 			   round_jiffies_relative(delay));
3598 }
3599 
3600 static void
3601 qlcnic_attach_work(struct work_struct *work)
3602 {
3603 	struct qlcnic_adapter *adapter = container_of(work,
3604 				struct qlcnic_adapter, fw_work.work);
3605 	struct net_device *netdev = adapter->netdev;
3606 	u32 npar_state;
3607 
3608 	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC) {
3609 		npar_state = QLC_SHARED_REG_RD32(adapter,
3610 						 QLCNIC_CRB_DEV_NPAR_STATE);
3611 		if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3612 			qlcnic_clr_all_drv_state(adapter, 0);
3613 		else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3614 			qlcnic_schedule_work(adapter, qlcnic_attach_work,
3615 							FW_POLL_DELAY);
3616 		else
3617 			goto attach;
3618 		QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3619 		return;
3620 	}
3621 attach:
3622 	qlcnic_dcb_get_info(adapter->dcb);
3623 
3624 	if (netif_running(netdev)) {
3625 		if (qlcnic_up(adapter, netdev))
3626 			goto done;
3627 
3628 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3629 	}
3630 
3631 done:
3632 	netif_device_attach(netdev);
3633 	adapter->fw_fail_cnt = 0;
3634 	adapter->flags &= ~QLCNIC_FW_HANG;
3635 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3636 	if (adapter->portnum == 0)
3637 		qlcnic_set_drv_version(adapter);
3638 
3639 	if (!qlcnic_clr_drv_state(adapter))
3640 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3641 							FW_POLL_DELAY);
3642 }
3643 
3644 static int
3645 qlcnic_check_health(struct qlcnic_adapter *adapter)
3646 {
3647 	struct qlcnic_hardware_context *ahw = adapter->ahw;
3648 	struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
3649 	u32 state = 0, heartbeat;
3650 	u32 peg_status;
3651 	int err = 0;
3652 
3653 	if (qlcnic_check_temp(adapter))
3654 		goto detach;
3655 
3656 	if (adapter->need_fw_reset)
3657 		qlcnic_dev_request_reset(adapter, 0);
3658 
3659 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3660 	if (state == QLCNIC_DEV_NEED_RESET) {
3661 		qlcnic_set_npar_non_operational(adapter);
3662 		adapter->need_fw_reset = 1;
3663 	} else if (state == QLCNIC_DEV_NEED_QUISCENT)
3664 		goto detach;
3665 
3666 	heartbeat = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3667 	if (heartbeat != adapter->heartbeat) {
3668 		adapter->heartbeat = heartbeat;
3669 		adapter->fw_fail_cnt = 0;
3670 		if (adapter->need_fw_reset)
3671 			goto detach;
3672 
3673 		if (ahw->reset_context && qlcnic_auto_fw_reset)
3674 			qlcnic_reset_hw_context(adapter);
3675 
3676 		return 0;
3677 	}
3678 
3679 	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3680 		return 0;
3681 
3682 	adapter->flags |= QLCNIC_FW_HANG;
3683 
3684 	qlcnic_dev_request_reset(adapter, 0);
3685 
3686 	if (qlcnic_auto_fw_reset)
3687 		clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3688 
3689 	dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3690 	peg_status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3691 	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3692 			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3693 			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3694 			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3695 			"PEG_NET_4_PC: 0x%x\n",
3696 			peg_status,
3697 			QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3698 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, &err),
3699 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, &err),
3700 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, &err),
3701 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, &err),
3702 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, &err));
3703 	if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3704 		dev_err(&adapter->pdev->dev,
3705 			"Firmware aborted with error code 0x00006700. "
3706 				"Device is being reset.\n");
3707 detach:
3708 	adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3709 		QLCNIC_DEV_NEED_RESET;
3710 
3711 	if (qlcnic_auto_fw_reset && !test_and_set_bit(__QLCNIC_RESETTING,
3712 						      &adapter->state)) {
3713 
3714 		qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3715 		QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3716 	} else if (!qlcnic_auto_fw_reset && fw_dump->enable &&
3717 		   adapter->flags & QLCNIC_FW_RESET_OWNER) {
3718 		qlcnic_dump_fw(adapter);
3719 	}
3720 
3721 	return 1;
3722 }
3723 
3724 void qlcnic_fw_poll_work(struct work_struct *work)
3725 {
3726 	struct qlcnic_adapter *adapter = container_of(work,
3727 				struct qlcnic_adapter, fw_work.work);
3728 
3729 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3730 		goto reschedule;
3731 
3732 
3733 	if (qlcnic_check_health(adapter))
3734 		return;
3735 
3736 	if (adapter->fhash.fnum)
3737 		qlcnic_prune_lb_filters(adapter);
3738 
3739 reschedule:
3740 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3741 }
3742 
3743 static int qlcnic_is_first_func(struct pci_dev *pdev)
3744 {
3745 	struct pci_dev *oth_pdev;
3746 	int val = pdev->devfn;
3747 
3748 	while (val-- > 0) {
3749 		oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3750 			(pdev->bus), pdev->bus->number,
3751 			PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3752 		if (!oth_pdev)
3753 			continue;
3754 
3755 		if (oth_pdev->current_state != PCI_D3cold) {
3756 			pci_dev_put(oth_pdev);
3757 			return 0;
3758 		}
3759 		pci_dev_put(oth_pdev);
3760 	}
3761 	return 1;
3762 }
3763 
3764 static int qlcnic_attach_func(struct pci_dev *pdev)
3765 {
3766 	int err, first_func;
3767 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3768 	struct net_device *netdev = adapter->netdev;
3769 
3770 	pdev->error_state = pci_channel_io_normal;
3771 
3772 	err = pci_enable_device(pdev);
3773 	if (err)
3774 		return err;
3775 
3776 	pci_set_master(pdev);
3777 	pci_restore_state(pdev);
3778 
3779 	first_func = qlcnic_is_first_func(pdev);
3780 
3781 	if (qlcnic_api_lock(adapter))
3782 		return -EINVAL;
3783 
3784 	if (adapter->ahw->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3785 		adapter->need_fw_reset = 1;
3786 		set_bit(__QLCNIC_START_FW, &adapter->state);
3787 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3788 				    QLCNIC_DEV_INITIALIZING);
3789 		QLCDB(adapter, DRV, "Restarting fw\n");
3790 	}
3791 	qlcnic_api_unlock(adapter);
3792 
3793 	err = qlcnic_start_firmware(adapter);
3794 	if (err)
3795 		return err;
3796 
3797 	qlcnic_clr_drv_state(adapter);
3798 	kfree(adapter->msix_entries);
3799 	adapter->msix_entries = NULL;
3800 	err = qlcnic_setup_intr(adapter);
3801 
3802 	if (err) {
3803 		kfree(adapter->msix_entries);
3804 		netdev_err(netdev, "failed to setup interrupt\n");
3805 		return err;
3806 	}
3807 
3808 	if (netif_running(netdev)) {
3809 		err = qlcnic_attach(adapter);
3810 		if (err) {
3811 			qlcnic_clr_all_drv_state(adapter, 1);
3812 			clear_bit(__QLCNIC_AER, &adapter->state);
3813 			netif_device_attach(netdev);
3814 			return err;
3815 		}
3816 
3817 		err = qlcnic_up(adapter, netdev);
3818 		if (err)
3819 			goto done;
3820 
3821 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3822 	}
3823  done:
3824 	netif_device_attach(netdev);
3825 	return err;
3826 }
3827 
3828 static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *pdev,
3829 						      pci_channel_state_t state)
3830 {
3831 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3832 	struct net_device *netdev = adapter->netdev;
3833 
3834 	if (state == pci_channel_io_perm_failure)
3835 		return PCI_ERS_RESULT_DISCONNECT;
3836 
3837 	if (state == pci_channel_io_normal)
3838 		return PCI_ERS_RESULT_RECOVERED;
3839 
3840 	set_bit(__QLCNIC_AER, &adapter->state);
3841 	netif_device_detach(netdev);
3842 
3843 	cancel_delayed_work_sync(&adapter->fw_work);
3844 
3845 	if (netif_running(netdev))
3846 		qlcnic_down(adapter, netdev);
3847 
3848 	qlcnic_detach(adapter);
3849 	qlcnic_teardown_intr(adapter);
3850 
3851 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3852 
3853 	pci_save_state(pdev);
3854 	pci_disable_device(pdev);
3855 
3856 	return PCI_ERS_RESULT_NEED_RESET;
3857 }
3858 
3859 static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *pdev)
3860 {
3861 	pci_ers_result_t res;
3862 
3863 	rtnl_lock();
3864 	res = qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3865 					 PCI_ERS_RESULT_RECOVERED;
3866 	rtnl_unlock();
3867 
3868 	return res;
3869 }
3870 
3871 static void qlcnic_82xx_io_resume(struct pci_dev *pdev)
3872 {
3873 	u32 state;
3874 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3875 
3876 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3877 	if (state == QLCNIC_DEV_READY && test_and_clear_bit(__QLCNIC_AER,
3878 							    &adapter->state))
3879 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3880 				     FW_POLL_DELAY);
3881 }
3882 
3883 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3884 						 pci_channel_state_t state)
3885 {
3886 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3887 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3888 
3889 	if (hw_ops->io_error_detected) {
3890 		return hw_ops->io_error_detected(pdev, state);
3891 	} else {
3892 		dev_err(&pdev->dev, "AER error_detected handler not registered.\n");
3893 		return PCI_ERS_RESULT_DISCONNECT;
3894 	}
3895 }
3896 
3897 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3898 {
3899 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3900 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3901 
3902 	if (hw_ops->io_slot_reset) {
3903 		return hw_ops->io_slot_reset(pdev);
3904 	} else {
3905 		dev_err(&pdev->dev, "AER slot_reset handler not registered.\n");
3906 		return PCI_ERS_RESULT_DISCONNECT;
3907 	}
3908 }
3909 
3910 static void qlcnic_io_resume(struct pci_dev *pdev)
3911 {
3912 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3913 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3914 
3915 	if (hw_ops->io_resume)
3916 		hw_ops->io_resume(pdev);
3917 	else
3918 		dev_err(&pdev->dev, "AER resume handler not registered.\n");
3919 }
3920 
3921 
3922 static int
3923 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3924 {
3925 	int err;
3926 
3927 	err = qlcnic_can_start_firmware(adapter);
3928 	if (err)
3929 		return err;
3930 
3931 	err = qlcnic_check_npar_opertional(adapter);
3932 	if (err)
3933 		return err;
3934 
3935 	err = qlcnic_initialize_nic(adapter);
3936 	if (err)
3937 		return err;
3938 
3939 	qlcnic_check_options(adapter);
3940 
3941 	err = qlcnic_set_eswitch_port_config(adapter);
3942 	if (err)
3943 		return err;
3944 
3945 	adapter->need_fw_reset = 0;
3946 
3947 	return err;
3948 }
3949 
3950 int qlcnic_validate_rings(struct qlcnic_adapter *adapter, __u32 ring_cnt,
3951 			  int queue_type)
3952 {
3953 	struct net_device *netdev = adapter->netdev;
3954 	char buf[8];
3955 
3956 	if (queue_type == QLCNIC_RX_QUEUE)
3957 		strcpy(buf, "SDS");
3958 	else
3959 		strcpy(buf, "Tx");
3960 
3961 	if (!is_power_of_2(ring_cnt)) {
3962 		netdev_err(netdev, "%s rings value should be a power of 2\n",
3963 			   buf);
3964 		return -EINVAL;
3965 	}
3966 
3967 	if (qlcnic_82xx_check(adapter) && (queue_type == QLCNIC_TX_QUEUE) &&
3968 	    !qlcnic_check_multi_tx(adapter)) {
3969 			netdev_err(netdev, "No Multi Tx queue support\n");
3970 			return -EINVAL;
3971 	}
3972 
3973 	if (ring_cnt > num_online_cpus()) {
3974 		netdev_err(netdev,
3975 			   "%s value[%u] should not be higher than, number of online CPUs\n",
3976 			   buf, num_online_cpus());
3977 		return -EINVAL;
3978 	}
3979 
3980 	return 0;
3981 }
3982 
3983 int qlcnic_setup_rings(struct qlcnic_adapter *adapter)
3984 {
3985 	struct net_device *netdev = adapter->netdev;
3986 	u8 tx_rings, rx_rings;
3987 	int err;
3988 
3989 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3990 		return -EBUSY;
3991 
3992 	tx_rings = adapter->drv_tss_rings;
3993 	rx_rings = adapter->drv_rss_rings;
3994 
3995 	netif_device_detach(netdev);
3996 
3997 	err = qlcnic_set_real_num_queues(adapter, tx_rings, rx_rings);
3998 	if (err)
3999 		goto done;
4000 
4001 	if (netif_running(netdev))
4002 		__qlcnic_down(adapter, netdev);
4003 
4004 	qlcnic_detach(adapter);
4005 
4006 	if (qlcnic_83xx_check(adapter)) {
4007 		qlcnic_83xx_free_mbx_intr(adapter);
4008 		qlcnic_83xx_enable_mbx_poll(adapter);
4009 	}
4010 
4011 	qlcnic_teardown_intr(adapter);
4012 
4013 	err = qlcnic_setup_intr(adapter);
4014 	if (err) {
4015 		kfree(adapter->msix_entries);
4016 		netdev_err(netdev, "failed to setup interrupt\n");
4017 		return err;
4018 	}
4019 
4020 	/* Check if we need to update real_num_{tx|rx}_queues because
4021 	 * qlcnic_setup_intr() may change Tx/Rx rings size
4022 	 */
4023 	if ((tx_rings != adapter->drv_tx_rings) ||
4024 	    (rx_rings != adapter->drv_sds_rings)) {
4025 		err = qlcnic_set_real_num_queues(adapter,
4026 						 adapter->drv_tx_rings,
4027 						 adapter->drv_sds_rings);
4028 		if (err)
4029 			goto done;
4030 	}
4031 
4032 	if (qlcnic_83xx_check(adapter)) {
4033 		qlcnic_83xx_initialize_nic(adapter, 1);
4034 		err = qlcnic_83xx_setup_mbx_intr(adapter);
4035 		qlcnic_83xx_disable_mbx_poll(adapter);
4036 		if (err) {
4037 			dev_err(&adapter->pdev->dev,
4038 				"failed to setup mbx interrupt\n");
4039 			goto done;
4040 		}
4041 	}
4042 
4043 	if (netif_running(netdev)) {
4044 		err = qlcnic_attach(adapter);
4045 		if (err)
4046 			goto done;
4047 		err = __qlcnic_up(adapter, netdev);
4048 		if (err)
4049 			goto done;
4050 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
4051 	}
4052 done:
4053 	netif_device_attach(netdev);
4054 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
4055 	return err;
4056 }
4057 
4058 #ifdef CONFIG_INET
4059 
4060 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4061 
4062 static void
4063 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4064 			struct net_device *dev, unsigned long event)
4065 {
4066 	const struct in_ifaddr *ifa;
4067 	struct in_device *indev;
4068 
4069 	indev = in_dev_get(dev);
4070 	if (!indev)
4071 		return;
4072 
4073 	in_dev_for_each_ifa_rtnl(ifa, indev) {
4074 		switch (event) {
4075 		case NETDEV_UP:
4076 			qlcnic_config_ipaddr(adapter,
4077 					ifa->ifa_address, QLCNIC_IP_UP);
4078 			break;
4079 		case NETDEV_DOWN:
4080 			qlcnic_config_ipaddr(adapter,
4081 					ifa->ifa_address, QLCNIC_IP_DOWN);
4082 			break;
4083 		default:
4084 			break;
4085 		}
4086 	}
4087 
4088 	in_dev_put(indev);
4089 }
4090 
4091 void qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4092 {
4093 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
4094 	struct net_device *dev;
4095 	u16 vid;
4096 
4097 	qlcnic_config_indev_addr(adapter, netdev, event);
4098 
4099 	rcu_read_lock();
4100 	for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4101 		dev = __vlan_find_dev_deep_rcu(netdev, htons(ETH_P_8021Q), vid);
4102 		if (!dev)
4103 			continue;
4104 		qlcnic_config_indev_addr(adapter, dev, event);
4105 	}
4106 	rcu_read_unlock();
4107 }
4108 
4109 static int qlcnic_netdev_event(struct notifier_block *this,
4110 				 unsigned long event, void *ptr)
4111 {
4112 	struct qlcnic_adapter *adapter;
4113 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4114 
4115 recheck:
4116 	if (dev == NULL)
4117 		goto done;
4118 
4119 	if (is_vlan_dev(dev)) {
4120 		dev = vlan_dev_real_dev(dev);
4121 		goto recheck;
4122 	}
4123 
4124 	if (!is_qlcnic_netdev(dev))
4125 		goto done;
4126 
4127 	adapter = netdev_priv(dev);
4128 
4129 	if (!adapter)
4130 		goto done;
4131 
4132 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4133 		goto done;
4134 
4135 	qlcnic_config_indev_addr(adapter, dev, event);
4136 done:
4137 	return NOTIFY_DONE;
4138 }
4139 
4140 static int
4141 qlcnic_inetaddr_event(struct notifier_block *this,
4142 		unsigned long event, void *ptr)
4143 {
4144 	struct qlcnic_adapter *adapter;
4145 	struct net_device *dev;
4146 
4147 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4148 
4149 	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4150 
4151 recheck:
4152 	if (dev == NULL)
4153 		goto done;
4154 
4155 	if (is_vlan_dev(dev)) {
4156 		dev = vlan_dev_real_dev(dev);
4157 		goto recheck;
4158 	}
4159 
4160 	if (!is_qlcnic_netdev(dev))
4161 		goto done;
4162 
4163 	adapter = netdev_priv(dev);
4164 
4165 	if (!adapter)
4166 		goto done;
4167 
4168 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4169 		goto done;
4170 
4171 	switch (event) {
4172 	case NETDEV_UP:
4173 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4174 
4175 		break;
4176 	case NETDEV_DOWN:
4177 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4178 
4179 		break;
4180 	default:
4181 		break;
4182 	}
4183 
4184 done:
4185 	return NOTIFY_DONE;
4186 }
4187 
4188 static struct notifier_block	qlcnic_netdev_cb = {
4189 	.notifier_call = qlcnic_netdev_event,
4190 };
4191 
4192 static struct notifier_block qlcnic_inetaddr_cb = {
4193 	.notifier_call = qlcnic_inetaddr_event,
4194 };
4195 #else
4196 void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4197 { }
4198 #endif
4199 static const struct pci_error_handlers qlcnic_err_handler = {
4200 	.error_detected = qlcnic_io_error_detected,
4201 	.slot_reset = qlcnic_io_slot_reset,
4202 	.resume = qlcnic_io_resume,
4203 };
4204 
4205 static SIMPLE_DEV_PM_OPS(qlcnic_pm_ops, qlcnic_suspend, qlcnic_resume);
4206 
4207 static struct pci_driver qlcnic_driver = {
4208 	.name = qlcnic_driver_name,
4209 	.id_table = qlcnic_pci_tbl,
4210 	.probe = qlcnic_probe,
4211 	.remove = qlcnic_remove,
4212 	.driver.pm = &qlcnic_pm_ops,
4213 	.shutdown = qlcnic_shutdown,
4214 	.err_handler = &qlcnic_err_handler,
4215 #ifdef CONFIG_QLCNIC_SRIOV
4216 	.sriov_configure = qlcnic_pci_sriov_configure,
4217 #endif
4218 
4219 };
4220 
4221 static int __init qlcnic_init_module(void)
4222 {
4223 	int ret;
4224 
4225 	printk(KERN_INFO "%s\n", qlcnic_driver_string);
4226 
4227 #ifdef CONFIG_INET
4228 	register_netdevice_notifier(&qlcnic_netdev_cb);
4229 	register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4230 #endif
4231 
4232 	ret = pci_register_driver(&qlcnic_driver);
4233 	if (ret) {
4234 #ifdef CONFIG_INET
4235 		unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4236 		unregister_netdevice_notifier(&qlcnic_netdev_cb);
4237 #endif
4238 	}
4239 
4240 	return ret;
4241 }
4242 
4243 module_init(qlcnic_init_module);
4244 
4245 static void __exit qlcnic_exit_module(void)
4246 {
4247 	pci_unregister_driver(&qlcnic_driver);
4248 
4249 #ifdef CONFIG_INET
4250 	unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4251 	unregister_netdevice_notifier(&qlcnic_netdev_cb);
4252 #endif
4253 }
4254 
4255 module_exit(qlcnic_exit_module);
4256