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