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