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