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