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