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 	struct cmd_desc_type0 *tx_desc_info;
2997 
2998 	for (i = 0; i < tx_ring->num_desc; i++) {
2999 		tx_desc_info = &tx_ring->desc_head[i];
3000 		pr_info("TX Desc: %d\n", i);
3001 		print_hex_dump(KERN_INFO, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
3002 			       &tx_ring->desc_head[i],
3003 			       sizeof(struct cmd_desc_type0), true);
3004 	}
3005 }
3006 
3007 static void qlcnic_dump_rings(struct qlcnic_adapter *adapter)
3008 {
3009 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
3010 	struct net_device *netdev = adapter->netdev;
3011 	struct qlcnic_host_rds_ring *rds_ring;
3012 	struct qlcnic_host_sds_ring *sds_ring;
3013 	struct qlcnic_host_tx_ring *tx_ring;
3014 	int ring;
3015 
3016 	if (!netdev || !netif_running(netdev))
3017 		return;
3018 
3019 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
3020 		rds_ring = &recv_ctx->rds_rings[ring];
3021 		if (!rds_ring)
3022 			continue;
3023 		netdev_info(netdev,
3024 			    "rds_ring=%d crb_rcv_producer=%d producer=%u num_desc=%u\n",
3025 			     ring, readl(rds_ring->crb_rcv_producer),
3026 			     rds_ring->producer, rds_ring->num_desc);
3027 	}
3028 
3029 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
3030 		sds_ring = &(recv_ctx->sds_rings[ring]);
3031 		if (!sds_ring)
3032 			continue;
3033 		netdev_info(netdev,
3034 			    "sds_ring=%d crb_sts_consumer=%d consumer=%u crb_intr_mask=%d num_desc=%u\n",
3035 			    ring, readl(sds_ring->crb_sts_consumer),
3036 			    sds_ring->consumer, readl(sds_ring->crb_intr_mask),
3037 			    sds_ring->num_desc);
3038 	}
3039 
3040 	for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
3041 		tx_ring = &adapter->tx_ring[ring];
3042 		if (!tx_ring)
3043 			continue;
3044 		netdev_info(netdev, "Tx ring=%d Context Id=0x%x\n",
3045 			    ring, tx_ring->ctx_id);
3046 		netdev_info(netdev,
3047 			    "xmit_finished=%llu, xmit_called=%llu, xmit_on=%llu, xmit_off=%llu\n",
3048 			    tx_ring->tx_stats.xmit_finished,
3049 			    tx_ring->tx_stats.xmit_called,
3050 			    tx_ring->tx_stats.xmit_on,
3051 			    tx_ring->tx_stats.xmit_off);
3052 
3053 		if (tx_ring->crb_intr_mask)
3054 			netdev_info(netdev, "crb_intr_mask=%d\n",
3055 				    readl(tx_ring->crb_intr_mask));
3056 
3057 		netdev_info(netdev,
3058 			    "hw_producer=%d, sw_producer=%d sw_consumer=%d, hw_consumer=%d\n",
3059 			    readl(tx_ring->crb_cmd_producer),
3060 			    tx_ring->producer, tx_ring->sw_consumer,
3061 			    le32_to_cpu(*(tx_ring->hw_consumer)));
3062 
3063 		netdev_info(netdev, "Total desc=%d, Available desc=%d\n",
3064 			    tx_ring->num_desc, qlcnic_tx_avail(tx_ring));
3065 
3066 		if (netif_msg_tx_err(adapter->ahw))
3067 			dump_tx_ring_desc(tx_ring);
3068 	}
3069 
3070 }
3071 
3072 static void qlcnic_tx_timeout(struct net_device *netdev)
3073 {
3074 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
3075 
3076 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3077 		return;
3078 
3079 	qlcnic_dump_rings(adapter);
3080 
3081 	if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS ||
3082 	    netif_msg_tx_err(adapter->ahw)) {
3083 		netdev_err(netdev, "Tx timeout, reset the adapter.\n");
3084 		if (qlcnic_82xx_check(adapter))
3085 			adapter->need_fw_reset = 1;
3086 		else if (qlcnic_83xx_check(adapter))
3087 			qlcnic_83xx_idc_request_reset(adapter,
3088 						      QLCNIC_FORCE_FW_DUMP_KEY);
3089 	} else {
3090 		netdev_err(netdev, "Tx timeout, reset adapter context.\n");
3091 		adapter->ahw->reset_context = 1;
3092 	}
3093 }
3094 
3095 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
3096 {
3097 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
3098 	struct net_device_stats *stats = &netdev->stats;
3099 
3100 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
3101 		qlcnic_update_stats(adapter);
3102 
3103 	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
3104 	stats->tx_packets = adapter->stats.xmitfinished;
3105 	stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
3106 	stats->tx_bytes = adapter->stats.txbytes;
3107 	stats->rx_dropped = adapter->stats.rxdropped;
3108 	stats->tx_dropped = adapter->stats.txdropped;
3109 
3110 	return stats;
3111 }
3112 
3113 static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *adapter)
3114 {
3115 	u32 status;
3116 
3117 	status = readl(adapter->isr_int_vec);
3118 
3119 	if (!(status & adapter->ahw->int_vec_bit))
3120 		return IRQ_NONE;
3121 
3122 	/* check interrupt state machine, to be sure */
3123 	status = readl(adapter->crb_int_state_reg);
3124 	if (!ISR_LEGACY_INT_TRIGGERED(status))
3125 		return IRQ_NONE;
3126 
3127 	writel(0xffffffff, adapter->tgt_status_reg);
3128 	/* read twice to ensure write is flushed */
3129 	readl(adapter->isr_int_vec);
3130 	readl(adapter->isr_int_vec);
3131 
3132 	return IRQ_HANDLED;
3133 }
3134 
3135 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
3136 {
3137 	struct qlcnic_host_sds_ring *sds_ring = data;
3138 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3139 
3140 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
3141 		goto done;
3142 	else if (adapter->flags & QLCNIC_MSI_ENABLED) {
3143 		writel(0xffffffff, adapter->tgt_status_reg);
3144 		goto done;
3145 	}
3146 
3147 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3148 		return IRQ_NONE;
3149 
3150 done:
3151 	adapter->ahw->diag_cnt++;
3152 	qlcnic_enable_sds_intr(adapter, sds_ring);
3153 	return IRQ_HANDLED;
3154 }
3155 
3156 static irqreturn_t qlcnic_intr(int irq, void *data)
3157 {
3158 	struct qlcnic_host_sds_ring *sds_ring = data;
3159 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3160 
3161 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3162 		return IRQ_NONE;
3163 
3164 	napi_schedule(&sds_ring->napi);
3165 
3166 	return IRQ_HANDLED;
3167 }
3168 
3169 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
3170 {
3171 	struct qlcnic_host_sds_ring *sds_ring = data;
3172 	struct qlcnic_adapter *adapter = sds_ring->adapter;
3173 
3174 	/* clear interrupt */
3175 	writel(0xffffffff, adapter->tgt_status_reg);
3176 
3177 	napi_schedule(&sds_ring->napi);
3178 	return IRQ_HANDLED;
3179 }
3180 
3181 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
3182 {
3183 	struct qlcnic_host_sds_ring *sds_ring = data;
3184 
3185 	napi_schedule(&sds_ring->napi);
3186 	return IRQ_HANDLED;
3187 }
3188 
3189 static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data)
3190 {
3191 	struct qlcnic_host_tx_ring *tx_ring = data;
3192 
3193 	napi_schedule(&tx_ring->napi);
3194 	return IRQ_HANDLED;
3195 }
3196 
3197 static void
3198 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
3199 {
3200 	u32 val;
3201 
3202 	val = adapter->portnum & 0xf;
3203 	val |= encoding << 7;
3204 	val |= (jiffies - adapter->dev_rst_time) << 8;
3205 
3206 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
3207 	adapter->dev_rst_time = jiffies;
3208 }
3209 
3210 static int
3211 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
3212 {
3213 	u32  val;
3214 
3215 	WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
3216 			state != QLCNIC_DEV_NEED_QUISCENT);
3217 
3218 	if (qlcnic_api_lock(adapter))
3219 		return -EIO;
3220 
3221 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3222 
3223 	if (state == QLCNIC_DEV_NEED_RESET)
3224 		QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3225 	else if (state == QLCNIC_DEV_NEED_QUISCENT)
3226 		QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
3227 
3228 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3229 
3230 	qlcnic_api_unlock(adapter);
3231 
3232 	return 0;
3233 }
3234 
3235 static int
3236 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
3237 {
3238 	u32  val;
3239 
3240 	if (qlcnic_api_lock(adapter))
3241 		return -EBUSY;
3242 
3243 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3244 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3245 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3246 
3247 	qlcnic_api_unlock(adapter);
3248 
3249 	return 0;
3250 }
3251 
3252 void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
3253 {
3254 	u32  val;
3255 
3256 	if (qlcnic_api_lock(adapter))
3257 		goto err;
3258 
3259 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3260 	QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
3261 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3262 
3263 	if (failed) {
3264 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3265 				    QLCNIC_DEV_FAILED);
3266 		dev_info(&adapter->pdev->dev,
3267 				"Device state set to Failed. Please Reboot\n");
3268 	} else if (!(val & 0x11111111))
3269 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3270 				    QLCNIC_DEV_COLD);
3271 
3272 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3273 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3274 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3275 
3276 	qlcnic_api_unlock(adapter);
3277 err:
3278 	adapter->fw_fail_cnt = 0;
3279 	adapter->flags &= ~QLCNIC_FW_HANG;
3280 	clear_bit(__QLCNIC_START_FW, &adapter->state);
3281 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3282 }
3283 
3284 /* Grab api lock, before checking state */
3285 static int
3286 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
3287 {
3288 	int act, state, active_mask;
3289 	struct qlcnic_hardware_context *ahw = adapter->ahw;
3290 
3291 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3292 	act = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3293 
3294 	if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
3295 		active_mask = (~(1 << (ahw->pci_func * 4)));
3296 		act = act & active_mask;
3297 	}
3298 
3299 	if (((state & 0x11111111) == (act & 0x11111111)) ||
3300 			((act & 0x11111111) == ((state >> 1) & 0x11111111)))
3301 		return 0;
3302 	else
3303 		return 1;
3304 }
3305 
3306 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
3307 {
3308 	u32 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
3309 
3310 	if (val != QLCNIC_DRV_IDC_VER) {
3311 		dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
3312 			" idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
3313 	}
3314 
3315 	return 0;
3316 }
3317 
3318 static int
3319 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
3320 {
3321 	u32 val, prev_state;
3322 	u8 dev_init_timeo = adapter->dev_init_timeo;
3323 	u8 portnum = adapter->portnum;
3324 	u8 ret;
3325 
3326 	if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
3327 		return 1;
3328 
3329 	if (qlcnic_api_lock(adapter))
3330 		return -1;
3331 
3332 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3333 	if (!(val & (1 << (portnum * 4)))) {
3334 		QLC_DEV_SET_REF_CNT(val, portnum);
3335 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3336 	}
3337 
3338 	prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3339 	QLCDB(adapter, HW, "Device state = %u\n", prev_state);
3340 
3341 	switch (prev_state) {
3342 	case QLCNIC_DEV_COLD:
3343 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3344 				    QLCNIC_DEV_INITIALIZING);
3345 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_IDC_VER,
3346 				    QLCNIC_DRV_IDC_VER);
3347 		qlcnic_idc_debug_info(adapter, 0);
3348 		qlcnic_api_unlock(adapter);
3349 		return 1;
3350 
3351 	case QLCNIC_DEV_READY:
3352 		ret = qlcnic_check_idc_ver(adapter);
3353 		qlcnic_api_unlock(adapter);
3354 		return ret;
3355 
3356 	case QLCNIC_DEV_NEED_RESET:
3357 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3358 		QLC_DEV_SET_RST_RDY(val, portnum);
3359 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3360 		break;
3361 
3362 	case QLCNIC_DEV_NEED_QUISCENT:
3363 		val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3364 		QLC_DEV_SET_QSCNT_RDY(val, portnum);
3365 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3366 		break;
3367 
3368 	case QLCNIC_DEV_FAILED:
3369 		dev_err(&adapter->pdev->dev, "Device in failed state.\n");
3370 		qlcnic_api_unlock(adapter);
3371 		return -1;
3372 
3373 	case QLCNIC_DEV_INITIALIZING:
3374 	case QLCNIC_DEV_QUISCENT:
3375 		break;
3376 	}
3377 
3378 	qlcnic_api_unlock(adapter);
3379 
3380 	do {
3381 		msleep(1000);
3382 		prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3383 
3384 		if (prev_state == QLCNIC_DEV_QUISCENT)
3385 			continue;
3386 	} while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
3387 
3388 	if (!dev_init_timeo) {
3389 		dev_err(&adapter->pdev->dev,
3390 			"Waiting for device to initialize timeout\n");
3391 		return -1;
3392 	}
3393 
3394 	if (qlcnic_api_lock(adapter))
3395 		return -1;
3396 
3397 	val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3398 	QLC_DEV_CLR_RST_QSCNT(val, portnum);
3399 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3400 
3401 	ret = qlcnic_check_idc_ver(adapter);
3402 	qlcnic_api_unlock(adapter);
3403 
3404 	return ret;
3405 }
3406 
3407 static void
3408 qlcnic_fwinit_work(struct work_struct *work)
3409 {
3410 	struct qlcnic_adapter *adapter = container_of(work,
3411 			struct qlcnic_adapter, fw_work.work);
3412 	u32 dev_state = 0xf;
3413 	u32 val;
3414 
3415 	if (qlcnic_api_lock(adapter))
3416 		goto err_ret;
3417 
3418 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3419 	if (dev_state == QLCNIC_DEV_QUISCENT ||
3420 	    dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3421 		qlcnic_api_unlock(adapter);
3422 		qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
3423 						FW_POLL_DELAY * 2);
3424 		return;
3425 	}
3426 
3427 	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
3428 		qlcnic_api_unlock(adapter);
3429 		goto wait_npar;
3430 	}
3431 
3432 	if (dev_state == QLCNIC_DEV_INITIALIZING ||
3433 	    dev_state == QLCNIC_DEV_READY) {
3434 		dev_info(&adapter->pdev->dev, "Detected state change from "
3435 				"DEV_NEED_RESET, skipping ack check\n");
3436 		goto skip_ack_check;
3437 	}
3438 
3439 	if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
3440 		dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
3441 					adapter->reset_ack_timeo);
3442 		goto skip_ack_check;
3443 	}
3444 
3445 	if (!qlcnic_check_drv_state(adapter)) {
3446 skip_ack_check:
3447 		dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3448 
3449 		if (dev_state == QLCNIC_DEV_NEED_RESET) {
3450 			QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3451 					    QLCNIC_DEV_INITIALIZING);
3452 			set_bit(__QLCNIC_START_FW, &adapter->state);
3453 			QLCDB(adapter, DRV, "Restarting fw\n");
3454 			qlcnic_idc_debug_info(adapter, 0);
3455 			val = QLC_SHARED_REG_RD32(adapter,
3456 						  QLCNIC_CRB_DRV_STATE);
3457 			QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3458 			QLC_SHARED_REG_WR32(adapter,
3459 					    QLCNIC_CRB_DRV_STATE, val);
3460 		}
3461 
3462 		qlcnic_api_unlock(adapter);
3463 
3464 		rtnl_lock();
3465 		if (qlcnic_check_fw_dump_state(adapter) &&
3466 		    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3467 			QLCDB(adapter, DRV, "Take FW dump\n");
3468 			qlcnic_dump_fw(adapter);
3469 			adapter->flags |= QLCNIC_FW_HANG;
3470 		}
3471 		rtnl_unlock();
3472 
3473 		adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
3474 		if (!adapter->nic_ops->start_firmware(adapter)) {
3475 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3476 			adapter->fw_wait_cnt = 0;
3477 			return;
3478 		}
3479 		goto err_ret;
3480 	}
3481 
3482 	qlcnic_api_unlock(adapter);
3483 
3484 wait_npar:
3485 	dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3486 	QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
3487 
3488 	switch (dev_state) {
3489 	case QLCNIC_DEV_READY:
3490 		if (!qlcnic_start_firmware(adapter)) {
3491 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3492 			adapter->fw_wait_cnt = 0;
3493 			return;
3494 		}
3495 	case QLCNIC_DEV_FAILED:
3496 		break;
3497 	default:
3498 		qlcnic_schedule_work(adapter,
3499 			qlcnic_fwinit_work, FW_POLL_DELAY);
3500 		return;
3501 	}
3502 
3503 err_ret:
3504 	dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
3505 		"fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
3506 	netif_device_attach(adapter->netdev);
3507 	qlcnic_clr_all_drv_state(adapter, 0);
3508 }
3509 
3510 static void
3511 qlcnic_detach_work(struct work_struct *work)
3512 {
3513 	struct qlcnic_adapter *adapter = container_of(work,
3514 			struct qlcnic_adapter, fw_work.work);
3515 	struct net_device *netdev = adapter->netdev;
3516 	u32 status;
3517 
3518 	netif_device_detach(netdev);
3519 
3520 	/* Dont grab rtnl lock during Quiscent mode */
3521 	if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3522 		if (netif_running(netdev))
3523 			__qlcnic_down(adapter, netdev);
3524 	} else
3525 		qlcnic_down(adapter, netdev);
3526 
3527 	status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3528 
3529 	if (status & QLCNIC_RCODE_FATAL_ERROR) {
3530 		dev_err(&adapter->pdev->dev,
3531 			"Detaching the device: peg halt status1=0x%x\n",
3532 					status);
3533 
3534 		if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
3535 			dev_err(&adapter->pdev->dev,
3536 			"On board active cooling fan failed. "
3537 				"Device has been halted.\n");
3538 			dev_err(&adapter->pdev->dev,
3539 				"Replace the adapter.\n");
3540 		}
3541 
3542 		goto err_ret;
3543 	}
3544 
3545 	if (adapter->ahw->temp == QLCNIC_TEMP_PANIC) {
3546 		dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
3547 			adapter->ahw->temp);
3548 		goto err_ret;
3549 	}
3550 
3551 	/* Dont ack if this instance is the reset owner */
3552 	if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3553 		if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
3554 			dev_err(&adapter->pdev->dev,
3555 				"Failed to set driver state,"
3556 					"detaching the device.\n");
3557 			goto err_ret;
3558 		}
3559 	}
3560 
3561 	adapter->fw_wait_cnt = 0;
3562 
3563 	qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3564 
3565 	return;
3566 
3567 err_ret:
3568 	netif_device_attach(netdev);
3569 	qlcnic_clr_all_drv_state(adapter, 1);
3570 }
3571 
3572 /*Transit NPAR state to NON Operational */
3573 static void
3574 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3575 {
3576 	u32 state;
3577 
3578 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3579 	if (state == QLCNIC_DEV_NPAR_NON_OPER)
3580 		return;
3581 
3582 	if (qlcnic_api_lock(adapter))
3583 		return;
3584 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3585 			    QLCNIC_DEV_NPAR_NON_OPER);
3586 	qlcnic_api_unlock(adapter);
3587 }
3588 
3589 static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *adapter,
3590 					  u32 key)
3591 {
3592 	u32 state, xg_val = 0, gb_val = 0;
3593 
3594 	qlcnic_xg_set_xg0_mask(xg_val);
3595 	qlcnic_xg_set_xg1_mask(xg_val);
3596 	QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3597 	qlcnic_gb_set_gb0_mask(gb_val);
3598 	qlcnic_gb_set_gb1_mask(gb_val);
3599 	qlcnic_gb_set_gb2_mask(gb_val);
3600 	qlcnic_gb_set_gb3_mask(gb_val);
3601 	QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3602 	dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3603 				" on all ports\n");
3604 	adapter->need_fw_reset = 1;
3605 
3606 	if (qlcnic_api_lock(adapter))
3607 		return;
3608 
3609 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3610 
3611 	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
3612 		netdev_err(adapter->netdev, "%s: Device is in non-operational state\n",
3613 			   __func__);
3614 		qlcnic_api_unlock(adapter);
3615 
3616 		return;
3617 	}
3618 
3619 	if (state == QLCNIC_DEV_READY) {
3620 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3621 				    QLCNIC_DEV_NEED_RESET);
3622 		adapter->flags |= QLCNIC_FW_RESET_OWNER;
3623 		QLCDB(adapter, DRV, "NEED_RESET state set\n");
3624 		qlcnic_idc_debug_info(adapter, 0);
3625 	}
3626 
3627 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3628 			    QLCNIC_DEV_NPAR_NON_OPER);
3629 	qlcnic_api_unlock(adapter);
3630 }
3631 
3632 /* Transit to NPAR READY state from NPAR NOT READY state */
3633 static void
3634 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3635 {
3636 	if (qlcnic_api_lock(adapter))
3637 		return;
3638 
3639 	QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3640 			    QLCNIC_DEV_NPAR_OPER);
3641 	QLCDB(adapter, DRV, "NPAR operational state set\n");
3642 
3643 	qlcnic_api_unlock(adapter);
3644 }
3645 
3646 void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3647 			  work_func_t func, int delay)
3648 {
3649 	if (test_bit(__QLCNIC_AER, &adapter->state))
3650 		return;
3651 
3652 	INIT_DELAYED_WORK(&adapter->fw_work, func);
3653 	queue_delayed_work(adapter->qlcnic_wq, &adapter->fw_work,
3654 			   round_jiffies_relative(delay));
3655 }
3656 
3657 static void
3658 qlcnic_attach_work(struct work_struct *work)
3659 {
3660 	struct qlcnic_adapter *adapter = container_of(work,
3661 				struct qlcnic_adapter, fw_work.work);
3662 	struct net_device *netdev = adapter->netdev;
3663 	u32 npar_state;
3664 
3665 	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC) {
3666 		npar_state = QLC_SHARED_REG_RD32(adapter,
3667 						 QLCNIC_CRB_DEV_NPAR_STATE);
3668 		if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3669 			qlcnic_clr_all_drv_state(adapter, 0);
3670 		else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3671 			qlcnic_schedule_work(adapter, qlcnic_attach_work,
3672 							FW_POLL_DELAY);
3673 		else
3674 			goto attach;
3675 		QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3676 		return;
3677 	}
3678 attach:
3679 	qlcnic_dcb_get_info(adapter->dcb);
3680 
3681 	if (netif_running(netdev)) {
3682 		if (qlcnic_up(adapter, netdev))
3683 			goto done;
3684 
3685 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3686 	}
3687 
3688 done:
3689 	netif_device_attach(netdev);
3690 	adapter->fw_fail_cnt = 0;
3691 	adapter->flags &= ~QLCNIC_FW_HANG;
3692 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3693 	if (adapter->portnum == 0)
3694 		qlcnic_set_drv_version(adapter);
3695 
3696 	if (!qlcnic_clr_drv_state(adapter))
3697 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3698 							FW_POLL_DELAY);
3699 }
3700 
3701 static int
3702 qlcnic_check_health(struct qlcnic_adapter *adapter)
3703 {
3704 	struct qlcnic_hardware_context *ahw = adapter->ahw;
3705 	struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
3706 	u32 state = 0, heartbeat;
3707 	u32 peg_status;
3708 	int err = 0;
3709 
3710 	if (qlcnic_check_temp(adapter))
3711 		goto detach;
3712 
3713 	if (adapter->need_fw_reset)
3714 		qlcnic_dev_request_reset(adapter, 0);
3715 
3716 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3717 	if (state == QLCNIC_DEV_NEED_RESET) {
3718 		qlcnic_set_npar_non_operational(adapter);
3719 		adapter->need_fw_reset = 1;
3720 	} else if (state == QLCNIC_DEV_NEED_QUISCENT)
3721 		goto detach;
3722 
3723 	heartbeat = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3724 	if (heartbeat != adapter->heartbeat) {
3725 		adapter->heartbeat = heartbeat;
3726 		adapter->fw_fail_cnt = 0;
3727 		if (adapter->need_fw_reset)
3728 			goto detach;
3729 
3730 		if (ahw->reset_context && qlcnic_auto_fw_reset)
3731 			qlcnic_reset_hw_context(adapter);
3732 
3733 		return 0;
3734 	}
3735 
3736 	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3737 		return 0;
3738 
3739 	adapter->flags |= QLCNIC_FW_HANG;
3740 
3741 	qlcnic_dev_request_reset(adapter, 0);
3742 
3743 	if (qlcnic_auto_fw_reset)
3744 		clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3745 
3746 	dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3747 	peg_status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3748 	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3749 			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3750 			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3751 			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3752 			"PEG_NET_4_PC: 0x%x\n",
3753 			peg_status,
3754 			QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3755 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, &err),
3756 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, &err),
3757 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, &err),
3758 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, &err),
3759 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, &err));
3760 	if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3761 		dev_err(&adapter->pdev->dev,
3762 			"Firmware aborted with error code 0x00006700. "
3763 				"Device is being reset.\n");
3764 detach:
3765 	adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3766 		QLCNIC_DEV_NEED_RESET;
3767 
3768 	if (qlcnic_auto_fw_reset && !test_and_set_bit(__QLCNIC_RESETTING,
3769 						      &adapter->state)) {
3770 
3771 		qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3772 		QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3773 	} else if (!qlcnic_auto_fw_reset && fw_dump->enable &&
3774 		   adapter->flags & QLCNIC_FW_RESET_OWNER) {
3775 		qlcnic_dump_fw(adapter);
3776 	}
3777 
3778 	return 1;
3779 }
3780 
3781 void qlcnic_fw_poll_work(struct work_struct *work)
3782 {
3783 	struct qlcnic_adapter *adapter = container_of(work,
3784 				struct qlcnic_adapter, fw_work.work);
3785 
3786 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3787 		goto reschedule;
3788 
3789 
3790 	if (qlcnic_check_health(adapter))
3791 		return;
3792 
3793 	if (adapter->fhash.fnum)
3794 		qlcnic_prune_lb_filters(adapter);
3795 
3796 reschedule:
3797 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3798 }
3799 
3800 static int qlcnic_is_first_func(struct pci_dev *pdev)
3801 {
3802 	struct pci_dev *oth_pdev;
3803 	int val = pdev->devfn;
3804 
3805 	while (val-- > 0) {
3806 		oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3807 			(pdev->bus), pdev->bus->number,
3808 			PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3809 		if (!oth_pdev)
3810 			continue;
3811 
3812 		if (oth_pdev->current_state != PCI_D3cold) {
3813 			pci_dev_put(oth_pdev);
3814 			return 0;
3815 		}
3816 		pci_dev_put(oth_pdev);
3817 	}
3818 	return 1;
3819 }
3820 
3821 static int qlcnic_attach_func(struct pci_dev *pdev)
3822 {
3823 	int err, first_func;
3824 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3825 	struct net_device *netdev = adapter->netdev;
3826 
3827 	pdev->error_state = pci_channel_io_normal;
3828 
3829 	err = pci_enable_device(pdev);
3830 	if (err)
3831 		return err;
3832 
3833 	pci_set_master(pdev);
3834 	pci_restore_state(pdev);
3835 
3836 	first_func = qlcnic_is_first_func(pdev);
3837 
3838 	if (qlcnic_api_lock(adapter))
3839 		return -EINVAL;
3840 
3841 	if (adapter->ahw->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3842 		adapter->need_fw_reset = 1;
3843 		set_bit(__QLCNIC_START_FW, &adapter->state);
3844 		QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3845 				    QLCNIC_DEV_INITIALIZING);
3846 		QLCDB(adapter, DRV, "Restarting fw\n");
3847 	}
3848 	qlcnic_api_unlock(adapter);
3849 
3850 	err = qlcnic_start_firmware(adapter);
3851 	if (err)
3852 		return err;
3853 
3854 	qlcnic_clr_drv_state(adapter);
3855 	kfree(adapter->msix_entries);
3856 	adapter->msix_entries = NULL;
3857 	err = qlcnic_setup_intr(adapter);
3858 
3859 	if (err) {
3860 		kfree(adapter->msix_entries);
3861 		netdev_err(netdev, "failed to setup interrupt\n");
3862 		return err;
3863 	}
3864 
3865 	if (netif_running(netdev)) {
3866 		err = qlcnic_attach(adapter);
3867 		if (err) {
3868 			qlcnic_clr_all_drv_state(adapter, 1);
3869 			clear_bit(__QLCNIC_AER, &adapter->state);
3870 			netif_device_attach(netdev);
3871 			return err;
3872 		}
3873 
3874 		err = qlcnic_up(adapter, netdev);
3875 		if (err)
3876 			goto done;
3877 
3878 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3879 	}
3880  done:
3881 	netif_device_attach(netdev);
3882 	return err;
3883 }
3884 
3885 static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *pdev,
3886 						      pci_channel_state_t state)
3887 {
3888 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3889 	struct net_device *netdev = adapter->netdev;
3890 
3891 	if (state == pci_channel_io_perm_failure)
3892 		return PCI_ERS_RESULT_DISCONNECT;
3893 
3894 	if (state == pci_channel_io_normal)
3895 		return PCI_ERS_RESULT_RECOVERED;
3896 
3897 	set_bit(__QLCNIC_AER, &adapter->state);
3898 	netif_device_detach(netdev);
3899 
3900 	cancel_delayed_work_sync(&adapter->fw_work);
3901 
3902 	if (netif_running(netdev))
3903 		qlcnic_down(adapter, netdev);
3904 
3905 	qlcnic_detach(adapter);
3906 	qlcnic_teardown_intr(adapter);
3907 
3908 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3909 
3910 	pci_save_state(pdev);
3911 	pci_disable_device(pdev);
3912 
3913 	return PCI_ERS_RESULT_NEED_RESET;
3914 }
3915 
3916 static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *pdev)
3917 {
3918 	pci_ers_result_t res;
3919 
3920 	rtnl_lock();
3921 	res = qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3922 					 PCI_ERS_RESULT_RECOVERED;
3923 	rtnl_unlock();
3924 
3925 	return res;
3926 }
3927 
3928 static void qlcnic_82xx_io_resume(struct pci_dev *pdev)
3929 {
3930 	u32 state;
3931 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3932 
3933 	state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3934 	if (state == QLCNIC_DEV_READY && test_and_clear_bit(__QLCNIC_AER,
3935 							    &adapter->state))
3936 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3937 				     FW_POLL_DELAY);
3938 }
3939 
3940 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3941 						 pci_channel_state_t state)
3942 {
3943 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3944 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3945 
3946 	if (hw_ops->io_error_detected) {
3947 		return hw_ops->io_error_detected(pdev, state);
3948 	} else {
3949 		dev_err(&pdev->dev, "AER error_detected handler not registered.\n");
3950 		return PCI_ERS_RESULT_DISCONNECT;
3951 	}
3952 }
3953 
3954 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3955 {
3956 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3957 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3958 
3959 	if (hw_ops->io_slot_reset) {
3960 		return hw_ops->io_slot_reset(pdev);
3961 	} else {
3962 		dev_err(&pdev->dev, "AER slot_reset handler not registered.\n");
3963 		return PCI_ERS_RESULT_DISCONNECT;
3964 	}
3965 }
3966 
3967 static void qlcnic_io_resume(struct pci_dev *pdev)
3968 {
3969 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3970 	struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3971 
3972 	if (hw_ops->io_resume)
3973 		hw_ops->io_resume(pdev);
3974 	else
3975 		dev_err(&pdev->dev, "AER resume handler not registered.\n");
3976 }
3977 
3978 
3979 static int
3980 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3981 {
3982 	int err;
3983 
3984 	err = qlcnic_can_start_firmware(adapter);
3985 	if (err)
3986 		return err;
3987 
3988 	err = qlcnic_check_npar_opertional(adapter);
3989 	if (err)
3990 		return err;
3991 
3992 	err = qlcnic_initialize_nic(adapter);
3993 	if (err)
3994 		return err;
3995 
3996 	qlcnic_check_options(adapter);
3997 
3998 	err = qlcnic_set_eswitch_port_config(adapter);
3999 	if (err)
4000 		return err;
4001 
4002 	adapter->need_fw_reset = 0;
4003 
4004 	return err;
4005 }
4006 
4007 int qlcnic_validate_rings(struct qlcnic_adapter *adapter, __u32 ring_cnt,
4008 			  int queue_type)
4009 {
4010 	struct net_device *netdev = adapter->netdev;
4011 	u8 max_hw_rings = 0;
4012 	char buf[8];
4013 	int cur_rings;
4014 
4015 	if (queue_type == QLCNIC_RX_QUEUE) {
4016 		max_hw_rings = adapter->max_sds_rings;
4017 		cur_rings = adapter->drv_sds_rings;
4018 		strcpy(buf, "SDS");
4019 	} else if (queue_type == QLCNIC_TX_QUEUE) {
4020 		max_hw_rings = adapter->max_tx_rings;
4021 		cur_rings = adapter->drv_tx_rings;
4022 		strcpy(buf, "Tx");
4023 	}
4024 
4025 	if (!is_power_of_2(ring_cnt)) {
4026 		netdev_err(netdev, "%s rings value should be a power of 2\n",
4027 			   buf);
4028 		return -EINVAL;
4029 	}
4030 
4031 	if (qlcnic_82xx_check(adapter) && (queue_type == QLCNIC_TX_QUEUE) &&
4032 	    !qlcnic_check_multi_tx(adapter)) {
4033 			netdev_err(netdev, "No Multi Tx queue support\n");
4034 			return -EINVAL;
4035 	}
4036 
4037 	if (ring_cnt > num_online_cpus()) {
4038 		netdev_err(netdev,
4039 			   "%s value[%u] should not be higher than, number of online CPUs\n",
4040 			   buf, num_online_cpus());
4041 		return -EINVAL;
4042 	}
4043 
4044 	return 0;
4045 }
4046 
4047 int qlcnic_setup_rings(struct qlcnic_adapter *adapter)
4048 {
4049 	struct net_device *netdev = adapter->netdev;
4050 	u8 tx_rings, rx_rings;
4051 	int err;
4052 
4053 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
4054 		return -EBUSY;
4055 
4056 	tx_rings = adapter->drv_tss_rings;
4057 	rx_rings = adapter->drv_rss_rings;
4058 
4059 	netif_device_detach(netdev);
4060 
4061 	err = qlcnic_set_real_num_queues(adapter, tx_rings, rx_rings);
4062 	if (err)
4063 		goto done;
4064 
4065 	if (netif_running(netdev))
4066 		__qlcnic_down(adapter, netdev);
4067 
4068 	qlcnic_detach(adapter);
4069 
4070 	if (qlcnic_83xx_check(adapter)) {
4071 		qlcnic_83xx_free_mbx_intr(adapter);
4072 		qlcnic_83xx_enable_mbx_poll(adapter);
4073 	}
4074 
4075 	qlcnic_teardown_intr(adapter);
4076 
4077 	err = qlcnic_setup_intr(adapter);
4078 	if (err) {
4079 		kfree(adapter->msix_entries);
4080 		netdev_err(netdev, "failed to setup interrupt\n");
4081 		return err;
4082 	}
4083 
4084 	/* Check if we need to update real_num_{tx|rx}_queues because
4085 	 * qlcnic_setup_intr() may change Tx/Rx rings size
4086 	 */
4087 	if ((tx_rings != adapter->drv_tx_rings) ||
4088 	    (rx_rings != adapter->drv_sds_rings)) {
4089 		err = qlcnic_set_real_num_queues(adapter,
4090 						 adapter->drv_tx_rings,
4091 						 adapter->drv_sds_rings);
4092 		if (err)
4093 			goto done;
4094 	}
4095 
4096 	if (qlcnic_83xx_check(adapter)) {
4097 		qlcnic_83xx_initialize_nic(adapter, 1);
4098 		err = qlcnic_83xx_setup_mbx_intr(adapter);
4099 		qlcnic_83xx_disable_mbx_poll(adapter);
4100 		if (err) {
4101 			dev_err(&adapter->pdev->dev,
4102 				"failed to setup mbx interrupt\n");
4103 			goto done;
4104 		}
4105 	}
4106 
4107 	if (netif_running(netdev)) {
4108 		err = qlcnic_attach(adapter);
4109 		if (err)
4110 			goto done;
4111 		err = __qlcnic_up(adapter, netdev);
4112 		if (err)
4113 			goto done;
4114 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
4115 	}
4116 done:
4117 	netif_device_attach(netdev);
4118 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
4119 	return err;
4120 }
4121 
4122 #ifdef CONFIG_INET
4123 
4124 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4125 
4126 static void
4127 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4128 			struct net_device *dev, unsigned long event)
4129 {
4130 	struct in_device *indev;
4131 
4132 	indev = in_dev_get(dev);
4133 	if (!indev)
4134 		return;
4135 
4136 	for_ifa(indev) {
4137 		switch (event) {
4138 		case NETDEV_UP:
4139 			qlcnic_config_ipaddr(adapter,
4140 					ifa->ifa_address, QLCNIC_IP_UP);
4141 			break;
4142 		case NETDEV_DOWN:
4143 			qlcnic_config_ipaddr(adapter,
4144 					ifa->ifa_address, QLCNIC_IP_DOWN);
4145 			break;
4146 		default:
4147 			break;
4148 		}
4149 	} endfor_ifa(indev);
4150 
4151 	in_dev_put(indev);
4152 }
4153 
4154 void qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4155 {
4156 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
4157 	struct net_device *dev;
4158 	u16 vid;
4159 
4160 	qlcnic_config_indev_addr(adapter, netdev, event);
4161 
4162 	rcu_read_lock();
4163 	for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4164 		dev = __vlan_find_dev_deep_rcu(netdev, htons(ETH_P_8021Q), vid);
4165 		if (!dev)
4166 			continue;
4167 		qlcnic_config_indev_addr(adapter, dev, event);
4168 	}
4169 	rcu_read_unlock();
4170 }
4171 
4172 static int qlcnic_netdev_event(struct notifier_block *this,
4173 				 unsigned long event, void *ptr)
4174 {
4175 	struct qlcnic_adapter *adapter;
4176 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4177 
4178 recheck:
4179 	if (dev == NULL)
4180 		goto done;
4181 
4182 	if (is_vlan_dev(dev)) {
4183 		dev = vlan_dev_real_dev(dev);
4184 		goto recheck;
4185 	}
4186 
4187 	if (!is_qlcnic_netdev(dev))
4188 		goto done;
4189 
4190 	adapter = netdev_priv(dev);
4191 
4192 	if (!adapter)
4193 		goto done;
4194 
4195 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4196 		goto done;
4197 
4198 	qlcnic_config_indev_addr(adapter, dev, event);
4199 done:
4200 	return NOTIFY_DONE;
4201 }
4202 
4203 static int
4204 qlcnic_inetaddr_event(struct notifier_block *this,
4205 		unsigned long event, void *ptr)
4206 {
4207 	struct qlcnic_adapter *adapter;
4208 	struct net_device *dev;
4209 
4210 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4211 
4212 	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4213 
4214 recheck:
4215 	if (dev == NULL)
4216 		goto done;
4217 
4218 	if (is_vlan_dev(dev)) {
4219 		dev = vlan_dev_real_dev(dev);
4220 		goto recheck;
4221 	}
4222 
4223 	if (!is_qlcnic_netdev(dev))
4224 		goto done;
4225 
4226 	adapter = netdev_priv(dev);
4227 
4228 	if (!adapter)
4229 		goto done;
4230 
4231 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4232 		goto done;
4233 
4234 	switch (event) {
4235 	case NETDEV_UP:
4236 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4237 
4238 		break;
4239 	case NETDEV_DOWN:
4240 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4241 
4242 		break;
4243 	default:
4244 		break;
4245 	}
4246 
4247 done:
4248 	return NOTIFY_DONE;
4249 }
4250 
4251 static struct notifier_block	qlcnic_netdev_cb = {
4252 	.notifier_call = qlcnic_netdev_event,
4253 };
4254 
4255 static struct notifier_block qlcnic_inetaddr_cb = {
4256 	.notifier_call = qlcnic_inetaddr_event,
4257 };
4258 #else
4259 void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4260 { }
4261 #endif
4262 static const struct pci_error_handlers qlcnic_err_handler = {
4263 	.error_detected = qlcnic_io_error_detected,
4264 	.slot_reset = qlcnic_io_slot_reset,
4265 	.resume = qlcnic_io_resume,
4266 };
4267 
4268 static struct pci_driver qlcnic_driver = {
4269 	.name = qlcnic_driver_name,
4270 	.id_table = qlcnic_pci_tbl,
4271 	.probe = qlcnic_probe,
4272 	.remove = qlcnic_remove,
4273 #ifdef CONFIG_PM
4274 	.suspend = qlcnic_suspend,
4275 	.resume = qlcnic_resume,
4276 #endif
4277 	.shutdown = qlcnic_shutdown,
4278 	.err_handler = &qlcnic_err_handler,
4279 #ifdef CONFIG_QLCNIC_SRIOV
4280 	.sriov_configure = qlcnic_pci_sriov_configure,
4281 #endif
4282 
4283 };
4284 
4285 static int __init qlcnic_init_module(void)
4286 {
4287 	int ret;
4288 
4289 	printk(KERN_INFO "%s\n", qlcnic_driver_string);
4290 
4291 #ifdef CONFIG_INET
4292 	register_netdevice_notifier(&qlcnic_netdev_cb);
4293 	register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4294 #endif
4295 
4296 	ret = pci_register_driver(&qlcnic_driver);
4297 	if (ret) {
4298 #ifdef CONFIG_INET
4299 		unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4300 		unregister_netdevice_notifier(&qlcnic_netdev_cb);
4301 #endif
4302 	}
4303 
4304 	return ret;
4305 }
4306 
4307 module_init(qlcnic_init_module);
4308 
4309 static void __exit qlcnic_exit_module(void)
4310 {
4311 	pci_unregister_driver(&qlcnic_driver);
4312 
4313 #ifdef CONFIG_INET
4314 	unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4315 	unregister_netdevice_notifier(&qlcnic_netdev_cb);
4316 #endif
4317 }
4318 
4319 module_exit(qlcnic_exit_module);
4320