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