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