1aa43c215SJeff Kirsher /*
2aa43c215SJeff Kirsher  * QLogic qlcnic NIC Driver
3aa43c215SJeff Kirsher  * Copyright (c)  2009-2010 QLogic Corporation
4aa43c215SJeff Kirsher  *
5aa43c215SJeff Kirsher  * See LICENSE.qlcnic for copyright and licensing details.
6aa43c215SJeff Kirsher  */
7aa43c215SJeff Kirsher 
8aa43c215SJeff Kirsher #include <linux/slab.h>
9aa43c215SJeff Kirsher #include <linux/vmalloc.h>
10aa43c215SJeff Kirsher #include <linux/interrupt.h>
11aa43c215SJeff Kirsher 
12aa43c215SJeff Kirsher #include "qlcnic.h"
13aa43c215SJeff Kirsher 
14aa43c215SJeff Kirsher #include <linux/swab.h>
15aa43c215SJeff Kirsher #include <linux/dma-mapping.h>
16aa43c215SJeff Kirsher #include <net/ip.h>
17aa43c215SJeff Kirsher #include <linux/ipv6.h>
18aa43c215SJeff Kirsher #include <linux/inetdevice.h>
19aa43c215SJeff Kirsher #include <linux/sysfs.h>
20aa43c215SJeff Kirsher #include <linux/aer.h>
21aa43c215SJeff Kirsher #include <linux/log2.h>
22aa43c215SJeff Kirsher 
23aa43c215SJeff Kirsher MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
24aa43c215SJeff Kirsher MODULE_LICENSE("GPL");
25aa43c215SJeff Kirsher MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
26aa43c215SJeff Kirsher MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
27aa43c215SJeff Kirsher 
28aa43c215SJeff Kirsher char qlcnic_driver_name[] = "qlcnic";
29aa43c215SJeff Kirsher static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
30aa43c215SJeff Kirsher 	"Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
31aa43c215SJeff Kirsher 
32aa43c215SJeff Kirsher static struct workqueue_struct *qlcnic_wq;
33aa43c215SJeff Kirsher static int qlcnic_mac_learn;
34aa43c215SJeff Kirsher module_param(qlcnic_mac_learn, int, 0444);
35aa43c215SJeff Kirsher MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
36aa43c215SJeff Kirsher 
37aa43c215SJeff Kirsher static int use_msi = 1;
38aa43c215SJeff Kirsher module_param(use_msi, int, 0444);
39aa43c215SJeff Kirsher MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
40aa43c215SJeff Kirsher 
41aa43c215SJeff Kirsher static int use_msi_x = 1;
42aa43c215SJeff Kirsher module_param(use_msi_x, int, 0444);
43aa43c215SJeff Kirsher MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
44aa43c215SJeff Kirsher 
45aa43c215SJeff Kirsher static int auto_fw_reset = 1;
46aa43c215SJeff Kirsher module_param(auto_fw_reset, int, 0644);
47aa43c215SJeff Kirsher MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
48aa43c215SJeff Kirsher 
49aa43c215SJeff Kirsher static int load_fw_file;
50aa43c215SJeff Kirsher module_param(load_fw_file, int, 0444);
51aa43c215SJeff Kirsher MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
52aa43c215SJeff Kirsher 
53aa43c215SJeff Kirsher static int qlcnic_config_npars;
54aa43c215SJeff Kirsher module_param(qlcnic_config_npars, int, 0444);
55aa43c215SJeff Kirsher MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
56aa43c215SJeff Kirsher 
57aa43c215SJeff Kirsher static int __devinit qlcnic_probe(struct pci_dev *pdev,
58aa43c215SJeff Kirsher 		const struct pci_device_id *ent);
59aa43c215SJeff Kirsher static void __devexit qlcnic_remove(struct pci_dev *pdev);
60aa43c215SJeff Kirsher static int qlcnic_open(struct net_device *netdev);
61aa43c215SJeff Kirsher static int qlcnic_close(struct net_device *netdev);
62aa43c215SJeff Kirsher static void qlcnic_tx_timeout(struct net_device *netdev);
63aa43c215SJeff Kirsher static void qlcnic_attach_work(struct work_struct *work);
64aa43c215SJeff Kirsher static void qlcnic_fwinit_work(struct work_struct *work);
65aa43c215SJeff Kirsher static void qlcnic_fw_poll_work(struct work_struct *work);
66aa43c215SJeff Kirsher static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
67aa43c215SJeff Kirsher 		work_func_t func, int delay);
68aa43c215SJeff Kirsher static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
69aa43c215SJeff Kirsher static int qlcnic_poll(struct napi_struct *napi, int budget);
70aa43c215SJeff Kirsher static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
71aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
72aa43c215SJeff Kirsher static void qlcnic_poll_controller(struct net_device *netdev);
73aa43c215SJeff Kirsher #endif
74aa43c215SJeff Kirsher 
75aa43c215SJeff Kirsher static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
76aa43c215SJeff Kirsher static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
77aa43c215SJeff Kirsher static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
78aa43c215SJeff Kirsher static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
79aa43c215SJeff Kirsher 
80aa43c215SJeff Kirsher static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
81aa43c215SJeff Kirsher static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
82aa43c215SJeff Kirsher static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
83aa43c215SJeff Kirsher 
84aa43c215SJeff Kirsher static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
85aa43c215SJeff Kirsher static irqreturn_t qlcnic_intr(int irq, void *data);
86aa43c215SJeff Kirsher static irqreturn_t qlcnic_msi_intr(int irq, void *data);
87aa43c215SJeff Kirsher static irqreturn_t qlcnic_msix_intr(int irq, void *data);
88aa43c215SJeff Kirsher 
89aa43c215SJeff Kirsher static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
90aa43c215SJeff Kirsher static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
91aa43c215SJeff Kirsher static int qlcnic_start_firmware(struct qlcnic_adapter *);
92aa43c215SJeff Kirsher 
93aa43c215SJeff Kirsher static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
94aa43c215SJeff Kirsher static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
95aa43c215SJeff Kirsher static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
96aa43c215SJeff Kirsher static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
97aa43c215SJeff Kirsher static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
98aa43c215SJeff Kirsher static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
99aa43c215SJeff Kirsher 				struct qlcnic_esw_func_cfg *);
1008e586137SJiri Pirko static int qlcnic_vlan_rx_add(struct net_device *, u16);
1018e586137SJiri Pirko static int qlcnic_vlan_rx_del(struct net_device *, u16);
102aa43c215SJeff Kirsher 
103aa43c215SJeff Kirsher /*  PCI Device ID Table  */
104aa43c215SJeff Kirsher #define ENTRY(device) \
105aa43c215SJeff Kirsher 	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
106aa43c215SJeff Kirsher 	.class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
107aa43c215SJeff Kirsher 
108aa43c215SJeff Kirsher #define PCI_DEVICE_ID_QLOGIC_QLE824X  0x8020
109aa43c215SJeff Kirsher 
110aa43c215SJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
111aa43c215SJeff Kirsher 	ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
112aa43c215SJeff Kirsher 	{0,}
113aa43c215SJeff Kirsher };
114aa43c215SJeff Kirsher 
115aa43c215SJeff Kirsher MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116aa43c215SJeff Kirsher 
117aa43c215SJeff Kirsher 
1185ad6ff9dSSony Chacko inline void qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring *tx_ring)
119aa43c215SJeff Kirsher {
120aa43c215SJeff Kirsher 	writel(tx_ring->producer, tx_ring->crb_cmd_producer);
121aa43c215SJeff Kirsher }
122aa43c215SJeff Kirsher 
123aa43c215SJeff Kirsher static const u32 msi_tgt_status[8] = {
124aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
125aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
126aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
127aa43c215SJeff Kirsher 	ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
128aa43c215SJeff Kirsher };
129aa43c215SJeff Kirsher 
130aa43c215SJeff Kirsher static const
131aa43c215SJeff Kirsher struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
132aa43c215SJeff Kirsher 
133aa43c215SJeff Kirsher static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
134aa43c215SJeff Kirsher {
135aa43c215SJeff Kirsher 	writel(0, sds_ring->crb_intr_mask);
136aa43c215SJeff Kirsher }
137aa43c215SJeff Kirsher 
138aa43c215SJeff Kirsher static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
139aa43c215SJeff Kirsher {
140aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
141aa43c215SJeff Kirsher 
142aa43c215SJeff Kirsher 	writel(0x1, sds_ring->crb_intr_mask);
143aa43c215SJeff Kirsher 
144aa43c215SJeff Kirsher 	if (!QLCNIC_IS_MSI_FAMILY(adapter))
145aa43c215SJeff Kirsher 		writel(0xfbff, adapter->tgt_mask_reg);
146aa43c215SJeff Kirsher }
147aa43c215SJeff Kirsher 
148aa43c215SJeff Kirsher static int
149aa43c215SJeff Kirsher qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
150aa43c215SJeff Kirsher {
151aa43c215SJeff Kirsher 	int size = sizeof(struct qlcnic_host_sds_ring) * count;
152aa43c215SJeff Kirsher 
153aa43c215SJeff Kirsher 	recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
154aa43c215SJeff Kirsher 
155aa43c215SJeff Kirsher 	return recv_ctx->sds_rings == NULL;
156aa43c215SJeff Kirsher }
157aa43c215SJeff Kirsher 
158aa43c215SJeff Kirsher static void
159aa43c215SJeff Kirsher qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
160aa43c215SJeff Kirsher {
161aa43c215SJeff Kirsher 	if (recv_ctx->sds_rings != NULL)
162aa43c215SJeff Kirsher 		kfree(recv_ctx->sds_rings);
163aa43c215SJeff Kirsher 
164aa43c215SJeff Kirsher 	recv_ctx->sds_rings = NULL;
165aa43c215SJeff Kirsher }
166aa43c215SJeff Kirsher 
167aa43c215SJeff Kirsher static int
168aa43c215SJeff Kirsher qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
169aa43c215SJeff Kirsher {
170aa43c215SJeff Kirsher 	int ring;
171aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
172aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
173aa43c215SJeff Kirsher 
174aa43c215SJeff Kirsher 	if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
175aa43c215SJeff Kirsher 		return -ENOMEM;
176aa43c215SJeff Kirsher 
177aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
178aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
179aa43c215SJeff Kirsher 
180aa43c215SJeff Kirsher 		if (ring == adapter->max_sds_rings - 1)
181aa43c215SJeff Kirsher 			netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
182aa43c215SJeff Kirsher 				QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
183aa43c215SJeff Kirsher 		else
184aa43c215SJeff Kirsher 			netif_napi_add(netdev, &sds_ring->napi,
185aa43c215SJeff Kirsher 				qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
186aa43c215SJeff Kirsher 	}
187aa43c215SJeff Kirsher 
188aa43c215SJeff Kirsher 	return 0;
189aa43c215SJeff Kirsher }
190aa43c215SJeff Kirsher 
191aa43c215SJeff Kirsher static void
192aa43c215SJeff Kirsher qlcnic_napi_del(struct qlcnic_adapter *adapter)
193aa43c215SJeff Kirsher {
194aa43c215SJeff Kirsher 	int ring;
195aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
196aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
197aa43c215SJeff Kirsher 
198aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
199aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
200aa43c215SJeff Kirsher 		netif_napi_del(&sds_ring->napi);
201aa43c215SJeff Kirsher 	}
202aa43c215SJeff Kirsher 
203aa43c215SJeff Kirsher 	qlcnic_free_sds_rings(adapter->recv_ctx);
204aa43c215SJeff Kirsher }
205aa43c215SJeff Kirsher 
206aa43c215SJeff Kirsher static void
207aa43c215SJeff Kirsher qlcnic_napi_enable(struct qlcnic_adapter *adapter)
208aa43c215SJeff Kirsher {
209aa43c215SJeff Kirsher 	int ring;
210aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
211aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
212aa43c215SJeff Kirsher 
213aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
214aa43c215SJeff Kirsher 		return;
215aa43c215SJeff Kirsher 
216aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
217aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
218aa43c215SJeff Kirsher 		napi_enable(&sds_ring->napi);
219aa43c215SJeff Kirsher 		qlcnic_enable_int(sds_ring);
220aa43c215SJeff Kirsher 	}
221aa43c215SJeff Kirsher }
222aa43c215SJeff Kirsher 
223aa43c215SJeff Kirsher static void
224aa43c215SJeff Kirsher qlcnic_napi_disable(struct qlcnic_adapter *adapter)
225aa43c215SJeff Kirsher {
226aa43c215SJeff Kirsher 	int ring;
227aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
228aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
229aa43c215SJeff Kirsher 
230aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
231aa43c215SJeff Kirsher 		return;
232aa43c215SJeff Kirsher 
233aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
234aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
235aa43c215SJeff Kirsher 		qlcnic_disable_int(sds_ring);
236aa43c215SJeff Kirsher 		napi_synchronize(&sds_ring->napi);
237aa43c215SJeff Kirsher 		napi_disable(&sds_ring->napi);
238aa43c215SJeff Kirsher 	}
239aa43c215SJeff Kirsher }
240aa43c215SJeff Kirsher 
241aa43c215SJeff Kirsher static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
242aa43c215SJeff Kirsher {
243aa43c215SJeff Kirsher 	memset(&adapter->stats, 0, sizeof(adapter->stats));
244aa43c215SJeff Kirsher }
245aa43c215SJeff Kirsher 
246aa43c215SJeff Kirsher static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
247aa43c215SJeff Kirsher {
248aa43c215SJeff Kirsher 	u32 control;
249aa43c215SJeff Kirsher 	int pos;
250aa43c215SJeff Kirsher 
251aa43c215SJeff Kirsher 	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
252aa43c215SJeff Kirsher 	if (pos) {
253aa43c215SJeff Kirsher 		pci_read_config_dword(pdev, pos, &control);
254aa43c215SJeff Kirsher 		if (enable)
255aa43c215SJeff Kirsher 			control |= PCI_MSIX_FLAGS_ENABLE;
256aa43c215SJeff Kirsher 		else
257aa43c215SJeff Kirsher 			control = 0;
258aa43c215SJeff Kirsher 		pci_write_config_dword(pdev, pos, control);
259aa43c215SJeff Kirsher 	}
260aa43c215SJeff Kirsher }
261aa43c215SJeff Kirsher 
262aa43c215SJeff Kirsher static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
263aa43c215SJeff Kirsher {
264aa43c215SJeff Kirsher 	int i;
265aa43c215SJeff Kirsher 
266aa43c215SJeff Kirsher 	for (i = 0; i < count; i++)
267aa43c215SJeff Kirsher 		adapter->msix_entries[i].entry = i;
268aa43c215SJeff Kirsher }
269aa43c215SJeff Kirsher 
270aa43c215SJeff Kirsher static int
271aa43c215SJeff Kirsher qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
272aa43c215SJeff Kirsher {
273aa43c215SJeff Kirsher 	u8 mac_addr[ETH_ALEN];
274aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
275aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
276aa43c215SJeff Kirsher 
277aa43c215SJeff Kirsher 	if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
278aa43c215SJeff Kirsher 		return -EIO;
279aa43c215SJeff Kirsher 
280aa43c215SJeff Kirsher 	memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
281aa43c215SJeff Kirsher 	memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
282aa43c215SJeff Kirsher 	memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
283aa43c215SJeff Kirsher 
284aa43c215SJeff Kirsher 	/* set station address */
285aa43c215SJeff Kirsher 
286aa43c215SJeff Kirsher 	if (!is_valid_ether_addr(netdev->perm_addr))
287aa43c215SJeff Kirsher 		dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
288aa43c215SJeff Kirsher 					netdev->dev_addr);
289aa43c215SJeff Kirsher 
290aa43c215SJeff Kirsher 	return 0;
291aa43c215SJeff Kirsher }
292aa43c215SJeff Kirsher 
293aa43c215SJeff Kirsher static int qlcnic_set_mac(struct net_device *netdev, void *p)
294aa43c215SJeff Kirsher {
295aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
296aa43c215SJeff Kirsher 	struct sockaddr *addr = p;
297aa43c215SJeff Kirsher 
298aa43c215SJeff Kirsher 	if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
299aa43c215SJeff Kirsher 		return -EOPNOTSUPP;
300aa43c215SJeff Kirsher 
301aa43c215SJeff Kirsher 	if (!is_valid_ether_addr(addr->sa_data))
302504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
303aa43c215SJeff Kirsher 
304aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
305aa43c215SJeff Kirsher 		netif_device_detach(netdev);
306aa43c215SJeff Kirsher 		qlcnic_napi_disable(adapter);
307aa43c215SJeff Kirsher 	}
308aa43c215SJeff Kirsher 
309aa43c215SJeff Kirsher 	memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
310aa43c215SJeff Kirsher 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
311aa43c215SJeff Kirsher 	qlcnic_set_multi(adapter->netdev);
312aa43c215SJeff Kirsher 
313aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
314aa43c215SJeff Kirsher 		netif_device_attach(netdev);
315aa43c215SJeff Kirsher 		qlcnic_napi_enable(adapter);
316aa43c215SJeff Kirsher 	}
317aa43c215SJeff Kirsher 	return 0;
318aa43c215SJeff Kirsher }
319aa43c215SJeff Kirsher 
320aa43c215SJeff Kirsher static const struct net_device_ops qlcnic_netdev_ops = {
321aa43c215SJeff Kirsher 	.ndo_open	   = qlcnic_open,
322aa43c215SJeff Kirsher 	.ndo_stop	   = qlcnic_close,
323aa43c215SJeff Kirsher 	.ndo_start_xmit    = qlcnic_xmit_frame,
324aa43c215SJeff Kirsher 	.ndo_get_stats	   = qlcnic_get_stats,
325aa43c215SJeff Kirsher 	.ndo_validate_addr = eth_validate_addr,
326afc4b13dSJiri Pirko 	.ndo_set_rx_mode   = qlcnic_set_multi,
327aa43c215SJeff Kirsher 	.ndo_set_mac_address    = qlcnic_set_mac,
328aa43c215SJeff Kirsher 	.ndo_change_mtu	   = qlcnic_change_mtu,
329aa43c215SJeff Kirsher 	.ndo_fix_features  = qlcnic_fix_features,
330aa43c215SJeff Kirsher 	.ndo_set_features  = qlcnic_set_features,
331aa43c215SJeff Kirsher 	.ndo_tx_timeout	   = qlcnic_tx_timeout,
332aa43c215SJeff Kirsher 	.ndo_vlan_rx_add_vid	= qlcnic_vlan_rx_add,
333aa43c215SJeff Kirsher 	.ndo_vlan_rx_kill_vid	= qlcnic_vlan_rx_del,
334aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
335aa43c215SJeff Kirsher 	.ndo_poll_controller = qlcnic_poll_controller,
336aa43c215SJeff Kirsher #endif
337aa43c215SJeff Kirsher };
338aa43c215SJeff Kirsher 
339b43e5ee7SSucheta Chakraborty static const struct net_device_ops qlcnic_netdev_failed_ops = {
340b43e5ee7SSucheta Chakraborty 	.ndo_open	   = qlcnic_open,
341b43e5ee7SSucheta Chakraborty };
342b43e5ee7SSucheta Chakraborty 
343aa43c215SJeff Kirsher static struct qlcnic_nic_template qlcnic_ops = {
344aa43c215SJeff Kirsher 	.config_bridged_mode = qlcnic_config_bridged_mode,
345aa43c215SJeff Kirsher 	.config_led = qlcnic_config_led,
346aa43c215SJeff Kirsher 	.start_firmware = qlcnic_start_firmware
347aa43c215SJeff Kirsher };
348aa43c215SJeff Kirsher 
349aa43c215SJeff Kirsher static struct qlcnic_nic_template qlcnic_vf_ops = {
350aa43c215SJeff Kirsher 	.config_bridged_mode = qlcnicvf_config_bridged_mode,
351aa43c215SJeff Kirsher 	.config_led = qlcnicvf_config_led,
352aa43c215SJeff Kirsher 	.start_firmware = qlcnicvf_start_firmware
353aa43c215SJeff Kirsher };
354aa43c215SJeff Kirsher 
355aa43c215SJeff Kirsher static int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
356aa43c215SJeff Kirsher {
357aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
358aa43c215SJeff Kirsher 	int err = -1;
359aa43c215SJeff Kirsher 
360aa43c215SJeff Kirsher 	adapter->max_sds_rings = 1;
361aa43c215SJeff Kirsher 	adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
362aa43c215SJeff Kirsher 	qlcnic_set_msix_bit(pdev, 0);
363aa43c215SJeff Kirsher 
364aa43c215SJeff Kirsher 	if (adapter->msix_supported) {
365aa43c215SJeff Kirsher  enable_msix:
366aa43c215SJeff Kirsher 		qlcnic_init_msix_entries(adapter, num_msix);
367aa43c215SJeff Kirsher 		err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
368aa43c215SJeff Kirsher 		if (err == 0) {
369aa43c215SJeff Kirsher 			adapter->flags |= QLCNIC_MSIX_ENABLED;
370aa43c215SJeff Kirsher 			qlcnic_set_msix_bit(pdev, 1);
371aa43c215SJeff Kirsher 
372aa43c215SJeff Kirsher 			adapter->max_sds_rings = num_msix;
373aa43c215SJeff Kirsher 
374aa43c215SJeff Kirsher 			dev_info(&pdev->dev, "using msi-x interrupts\n");
375aa43c215SJeff Kirsher 			return err;
376aa43c215SJeff Kirsher 		}
377aa43c215SJeff Kirsher 		if (err > 0) {
378aa43c215SJeff Kirsher 			num_msix = rounddown_pow_of_two(err);
379aa43c215SJeff Kirsher 			if (num_msix)
380aa43c215SJeff Kirsher 				goto enable_msix;
381aa43c215SJeff Kirsher 		}
382aa43c215SJeff Kirsher 	}
383aa43c215SJeff Kirsher 	return err;
384aa43c215SJeff Kirsher }
385aa43c215SJeff Kirsher 
386aa43c215SJeff Kirsher 
387aa43c215SJeff Kirsher static void qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
388aa43c215SJeff Kirsher {
389aa43c215SJeff Kirsher 	const struct qlcnic_legacy_intr_set *legacy_intrp;
390aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
391aa43c215SJeff Kirsher 
392aa43c215SJeff Kirsher 	if (use_msi && !pci_enable_msi(pdev)) {
393aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MSI_ENABLED;
394aa43c215SJeff Kirsher 		adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
395aa43c215SJeff Kirsher 				msi_tgt_status[adapter->ahw->pci_func]);
396aa43c215SJeff Kirsher 		dev_info(&pdev->dev, "using msi interrupts\n");
397aa43c215SJeff Kirsher 		adapter->msix_entries[0].vector = pdev->irq;
398aa43c215SJeff Kirsher 		return;
399aa43c215SJeff Kirsher 	}
400aa43c215SJeff Kirsher 
401aa43c215SJeff Kirsher 	legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
402aa43c215SJeff Kirsher 
403aa43c215SJeff Kirsher 	adapter->int_vec_bit = legacy_intrp->int_vec_bit;
404aa43c215SJeff Kirsher 	adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
405aa43c215SJeff Kirsher 			legacy_intrp->tgt_status_reg);
406aa43c215SJeff Kirsher 	adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
407aa43c215SJeff Kirsher 			legacy_intrp->tgt_mask_reg);
408aa43c215SJeff Kirsher 	adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
409aa43c215SJeff Kirsher 
410aa43c215SJeff Kirsher 	adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
411aa43c215SJeff Kirsher 			ISR_INT_STATE_REG);
412aa43c215SJeff Kirsher 	dev_info(&pdev->dev, "using legacy interrupts\n");
413aa43c215SJeff Kirsher 	adapter->msix_entries[0].vector = pdev->irq;
414aa43c215SJeff Kirsher }
415aa43c215SJeff Kirsher 
416aa43c215SJeff Kirsher static void
417aa43c215SJeff Kirsher qlcnic_setup_intr(struct qlcnic_adapter *adapter)
418aa43c215SJeff Kirsher {
419aa43c215SJeff Kirsher 	int num_msix;
420aa43c215SJeff Kirsher 
421aa43c215SJeff Kirsher 	if (adapter->msix_supported) {
422aa43c215SJeff Kirsher 		num_msix = rounddown_pow_of_two(min_t(int, num_online_cpus(),
423aa43c215SJeff Kirsher 				QLCNIC_DEF_NUM_STS_DESC_RINGS));
424aa43c215SJeff Kirsher 	} else
425aa43c215SJeff Kirsher 		num_msix = 1;
426aa43c215SJeff Kirsher 
427aa43c215SJeff Kirsher 	if (!qlcnic_enable_msix(adapter, num_msix))
428aa43c215SJeff Kirsher 		return;
429aa43c215SJeff Kirsher 
430aa43c215SJeff Kirsher 	qlcnic_enable_msi_legacy(adapter);
431aa43c215SJeff Kirsher }
432aa43c215SJeff Kirsher 
433aa43c215SJeff Kirsher static void
434aa43c215SJeff Kirsher qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
435aa43c215SJeff Kirsher {
436aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
437aa43c215SJeff Kirsher 		pci_disable_msix(adapter->pdev);
438aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSI_ENABLED)
439aa43c215SJeff Kirsher 		pci_disable_msi(adapter->pdev);
440aa43c215SJeff Kirsher }
441aa43c215SJeff Kirsher 
442aa43c215SJeff Kirsher static void
443aa43c215SJeff Kirsher qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
444aa43c215SJeff Kirsher {
445aa43c215SJeff Kirsher 	if (adapter->ahw->pci_base0 != NULL)
446aa43c215SJeff Kirsher 		iounmap(adapter->ahw->pci_base0);
447aa43c215SJeff Kirsher }
448aa43c215SJeff Kirsher 
449aa43c215SJeff Kirsher static int
450aa43c215SJeff Kirsher qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
451aa43c215SJeff Kirsher {
452aa43c215SJeff Kirsher 	struct qlcnic_pci_info *pci_info;
453aa43c215SJeff Kirsher 	int i, ret = 0;
454aa43c215SJeff Kirsher 	u8 pfn;
455aa43c215SJeff Kirsher 
456aa43c215SJeff Kirsher 	pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
457aa43c215SJeff Kirsher 	if (!pci_info)
458aa43c215SJeff Kirsher 		return -ENOMEM;
459aa43c215SJeff Kirsher 
460aa43c215SJeff Kirsher 	adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
461aa43c215SJeff Kirsher 				QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
462aa43c215SJeff Kirsher 	if (!adapter->npars) {
463aa43c215SJeff Kirsher 		ret = -ENOMEM;
464aa43c215SJeff Kirsher 		goto err_pci_info;
465aa43c215SJeff Kirsher 	}
466aa43c215SJeff Kirsher 
467aa43c215SJeff Kirsher 	adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
468aa43c215SJeff Kirsher 				QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
469aa43c215SJeff Kirsher 	if (!adapter->eswitch) {
470aa43c215SJeff Kirsher 		ret = -ENOMEM;
471aa43c215SJeff Kirsher 		goto err_npars;
472aa43c215SJeff Kirsher 	}
473aa43c215SJeff Kirsher 
474aa43c215SJeff Kirsher 	ret = qlcnic_get_pci_info(adapter, pci_info);
475aa43c215SJeff Kirsher 	if (ret)
476aa43c215SJeff Kirsher 		goto err_eswitch;
477aa43c215SJeff Kirsher 
478aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
479aa43c215SJeff Kirsher 		pfn = pci_info[i].id;
4800f6efff9SDan Carpenter 		if (pfn >= QLCNIC_MAX_PCI_FUNC) {
481aa43c215SJeff Kirsher 			ret = QL_STATUS_INVALID_PARAM;
482aa43c215SJeff Kirsher 			goto err_eswitch;
483aa43c215SJeff Kirsher 		}
484aa43c215SJeff Kirsher 		adapter->npars[pfn].active = (u8)pci_info[i].active;
485aa43c215SJeff Kirsher 		adapter->npars[pfn].type = (u8)pci_info[i].type;
486aa43c215SJeff Kirsher 		adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
487aa43c215SJeff Kirsher 		adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
488aa43c215SJeff Kirsher 		adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
489aa43c215SJeff Kirsher 	}
490aa43c215SJeff Kirsher 
491aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
492aa43c215SJeff Kirsher 		adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
493aa43c215SJeff Kirsher 
494aa43c215SJeff Kirsher 	kfree(pci_info);
495aa43c215SJeff Kirsher 	return 0;
496aa43c215SJeff Kirsher 
497aa43c215SJeff Kirsher err_eswitch:
498aa43c215SJeff Kirsher 	kfree(adapter->eswitch);
499aa43c215SJeff Kirsher 	adapter->eswitch = NULL;
500aa43c215SJeff Kirsher err_npars:
501aa43c215SJeff Kirsher 	kfree(adapter->npars);
502aa43c215SJeff Kirsher 	adapter->npars = NULL;
503aa43c215SJeff Kirsher err_pci_info:
504aa43c215SJeff Kirsher 	kfree(pci_info);
505aa43c215SJeff Kirsher 
506aa43c215SJeff Kirsher 	return ret;
507aa43c215SJeff Kirsher }
508aa43c215SJeff Kirsher 
509aa43c215SJeff Kirsher static int
510aa43c215SJeff Kirsher qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
511aa43c215SJeff Kirsher {
512aa43c215SJeff Kirsher 	u8 id;
513aa43c215SJeff Kirsher 	u32 ref_count;
514aa43c215SJeff Kirsher 	int i, ret = 1;
515aa43c215SJeff Kirsher 	u32 data = QLCNIC_MGMT_FUNC;
516aa43c215SJeff Kirsher 	void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
517aa43c215SJeff Kirsher 
518aa43c215SJeff Kirsher 	/* If other drivers are not in use set their privilege level */
519aa43c215SJeff Kirsher 	ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
520aa43c215SJeff Kirsher 	ret = qlcnic_api_lock(adapter);
521aa43c215SJeff Kirsher 	if (ret)
522aa43c215SJeff Kirsher 		goto err_lock;
523aa43c215SJeff Kirsher 
524aa43c215SJeff Kirsher 	if (qlcnic_config_npars) {
525aa43c215SJeff Kirsher 		for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
526aa43c215SJeff Kirsher 			id = i;
527aa43c215SJeff Kirsher 			if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
528aa43c215SJeff Kirsher 				id == adapter->ahw->pci_func)
529aa43c215SJeff Kirsher 				continue;
530aa43c215SJeff Kirsher 			data |= (qlcnic_config_npars &
531aa43c215SJeff Kirsher 					QLC_DEV_SET_DRV(0xf, id));
532aa43c215SJeff Kirsher 		}
533aa43c215SJeff Kirsher 	} else {
534aa43c215SJeff Kirsher 		data = readl(priv_op);
535aa43c215SJeff Kirsher 		data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
536aa43c215SJeff Kirsher 			(QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
537aa43c215SJeff Kirsher 			adapter->ahw->pci_func));
538aa43c215SJeff Kirsher 	}
539aa43c215SJeff Kirsher 	writel(data, priv_op);
540aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
541aa43c215SJeff Kirsher err_lock:
542aa43c215SJeff Kirsher 	return ret;
543aa43c215SJeff Kirsher }
544aa43c215SJeff Kirsher 
545aa43c215SJeff Kirsher static void
546aa43c215SJeff Kirsher qlcnic_check_vf(struct qlcnic_adapter *adapter)
547aa43c215SJeff Kirsher {
548aa43c215SJeff Kirsher 	void __iomem *msix_base_addr;
549aa43c215SJeff Kirsher 	void __iomem *priv_op;
550aa43c215SJeff Kirsher 	u32 func;
551aa43c215SJeff Kirsher 	u32 msix_base;
552aa43c215SJeff Kirsher 	u32 op_mode, priv_level;
553aa43c215SJeff Kirsher 
554aa43c215SJeff Kirsher 	/* Determine FW API version */
555aa43c215SJeff Kirsher 	adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
556aa43c215SJeff Kirsher 					QLCNIC_FW_API);
557aa43c215SJeff Kirsher 
558aa43c215SJeff Kirsher 	/* Find PCI function number */
559aa43c215SJeff Kirsher 	pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
560aa43c215SJeff Kirsher 	msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
561aa43c215SJeff Kirsher 	msix_base = readl(msix_base_addr);
562aa43c215SJeff Kirsher 	func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
563aa43c215SJeff Kirsher 	adapter->ahw->pci_func = func;
564aa43c215SJeff Kirsher 
565aa43c215SJeff Kirsher 	/* Determine function privilege level */
566aa43c215SJeff Kirsher 	priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
567aa43c215SJeff Kirsher 	op_mode = readl(priv_op);
568aa43c215SJeff Kirsher 	if (op_mode == QLC_DEV_DRV_DEFAULT)
569aa43c215SJeff Kirsher 		priv_level = QLCNIC_MGMT_FUNC;
570aa43c215SJeff Kirsher 	else
571aa43c215SJeff Kirsher 		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
572aa43c215SJeff Kirsher 
573aa43c215SJeff Kirsher 	if (priv_level == QLCNIC_NON_PRIV_FUNC) {
574aa43c215SJeff Kirsher 		adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
575aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev,
576aa43c215SJeff Kirsher 			"HAL Version: %d Non Privileged function\n",
577aa43c215SJeff Kirsher 			adapter->fw_hal_version);
578aa43c215SJeff Kirsher 		adapter->nic_ops = &qlcnic_vf_ops;
579aa43c215SJeff Kirsher 	} else
580aa43c215SJeff Kirsher 		adapter->nic_ops = &qlcnic_ops;
581aa43c215SJeff Kirsher }
582aa43c215SJeff Kirsher 
583aa43c215SJeff Kirsher static int
584aa43c215SJeff Kirsher qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
585aa43c215SJeff Kirsher {
586aa43c215SJeff Kirsher 	void __iomem *mem_ptr0 = NULL;
587aa43c215SJeff Kirsher 	resource_size_t mem_base;
588aa43c215SJeff Kirsher 	unsigned long mem_len, pci_len0 = 0;
589aa43c215SJeff Kirsher 
590aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
591aa43c215SJeff Kirsher 
592aa43c215SJeff Kirsher 	/* remap phys address */
593aa43c215SJeff Kirsher 	mem_base = pci_resource_start(pdev, 0);	/* 0 is for BAR 0 */
594aa43c215SJeff Kirsher 	mem_len = pci_resource_len(pdev, 0);
595aa43c215SJeff Kirsher 
596aa43c215SJeff Kirsher 	if (mem_len == QLCNIC_PCI_2MB_SIZE) {
597aa43c215SJeff Kirsher 
598aa43c215SJeff Kirsher 		mem_ptr0 = pci_ioremap_bar(pdev, 0);
599aa43c215SJeff Kirsher 		if (mem_ptr0 == NULL) {
600aa43c215SJeff Kirsher 			dev_err(&pdev->dev, "failed to map PCI bar 0\n");
601aa43c215SJeff Kirsher 			return -EIO;
602aa43c215SJeff Kirsher 		}
603aa43c215SJeff Kirsher 		pci_len0 = mem_len;
604aa43c215SJeff Kirsher 	} else {
605aa43c215SJeff Kirsher 		return -EIO;
606aa43c215SJeff Kirsher 	}
607aa43c215SJeff Kirsher 
608aa43c215SJeff Kirsher 	dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
609aa43c215SJeff Kirsher 
610aa43c215SJeff Kirsher 	adapter->ahw->pci_base0 = mem_ptr0;
611aa43c215SJeff Kirsher 	adapter->ahw->pci_len0 = pci_len0;
612aa43c215SJeff Kirsher 
613aa43c215SJeff Kirsher 	qlcnic_check_vf(adapter);
614aa43c215SJeff Kirsher 
615aa43c215SJeff Kirsher 	adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
616aa43c215SJeff Kirsher 		QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
617aa43c215SJeff Kirsher 			adapter->ahw->pci_func)));
618aa43c215SJeff Kirsher 
619aa43c215SJeff Kirsher 	return 0;
620aa43c215SJeff Kirsher }
621aa43c215SJeff Kirsher 
622aa43c215SJeff Kirsher static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
623aa43c215SJeff Kirsher {
624aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
625aa43c215SJeff Kirsher 	int i, found = 0;
626aa43c215SJeff Kirsher 
627aa43c215SJeff Kirsher 	for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
628aa43c215SJeff Kirsher 		if (qlcnic_boards[i].vendor == pdev->vendor &&
629aa43c215SJeff Kirsher 			qlcnic_boards[i].device == pdev->device &&
630aa43c215SJeff Kirsher 			qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
631aa43c215SJeff Kirsher 			qlcnic_boards[i].sub_device == pdev->subsystem_device) {
632aa43c215SJeff Kirsher 				sprintf(name, "%pM: %s" ,
633aa43c215SJeff Kirsher 					adapter->mac_addr,
634aa43c215SJeff Kirsher 					qlcnic_boards[i].short_name);
635aa43c215SJeff Kirsher 				found = 1;
636aa43c215SJeff Kirsher 				break;
637aa43c215SJeff Kirsher 		}
638aa43c215SJeff Kirsher 
639aa43c215SJeff Kirsher 	}
640aa43c215SJeff Kirsher 
641aa43c215SJeff Kirsher 	if (!found)
642aa43c215SJeff Kirsher 		sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
643aa43c215SJeff Kirsher }
644aa43c215SJeff Kirsher 
645aa43c215SJeff Kirsher static void
646aa43c215SJeff Kirsher qlcnic_check_options(struct qlcnic_adapter *adapter)
647aa43c215SJeff Kirsher {
648aa43c215SJeff Kirsher 	u32 fw_major, fw_minor, fw_build, prev_fw_version;
649aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
650aa43c215SJeff Kirsher 	struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
651aa43c215SJeff Kirsher 
652aa43c215SJeff Kirsher 	prev_fw_version = adapter->fw_version;
653aa43c215SJeff Kirsher 
654aa43c215SJeff Kirsher 	fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
655aa43c215SJeff Kirsher 	fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
656aa43c215SJeff Kirsher 	fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
657aa43c215SJeff Kirsher 
658aa43c215SJeff Kirsher 	adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
659aa43c215SJeff Kirsher 
660aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC) {
661aa43c215SJeff Kirsher 		if (fw_dump->tmpl_hdr == NULL ||
662aa43c215SJeff Kirsher 				adapter->fw_version > prev_fw_version) {
663aa43c215SJeff Kirsher 			if (fw_dump->tmpl_hdr)
664aa43c215SJeff Kirsher 				vfree(fw_dump->tmpl_hdr);
665aa43c215SJeff Kirsher 			if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
666aa43c215SJeff Kirsher 				dev_info(&pdev->dev,
667aa43c215SJeff Kirsher 					"Supports FW dump capability\n");
668aa43c215SJeff Kirsher 		}
669aa43c215SJeff Kirsher 	}
670aa43c215SJeff Kirsher 
671aa43c215SJeff Kirsher 	dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
672aa43c215SJeff Kirsher 			fw_major, fw_minor, fw_build);
673aa43c215SJeff Kirsher 	if (adapter->ahw->port_type == QLCNIC_XGBE) {
674aa43c215SJeff Kirsher 		if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
675aa43c215SJeff Kirsher 			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
676aa43c215SJeff Kirsher 			adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
677aa43c215SJeff Kirsher 		} else {
678aa43c215SJeff Kirsher 			adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
679aa43c215SJeff Kirsher 			adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
680aa43c215SJeff Kirsher 		}
681aa43c215SJeff Kirsher 
682aa43c215SJeff Kirsher 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
683aa43c215SJeff Kirsher 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
684aa43c215SJeff Kirsher 
685aa43c215SJeff Kirsher 	} else if (adapter->ahw->port_type == QLCNIC_GBE) {
686aa43c215SJeff Kirsher 		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
687aa43c215SJeff Kirsher 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
688aa43c215SJeff Kirsher 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
689aa43c215SJeff Kirsher 		adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
690aa43c215SJeff Kirsher 	}
691aa43c215SJeff Kirsher 
692aa43c215SJeff Kirsher 	adapter->msix_supported = !!use_msi_x;
693aa43c215SJeff Kirsher 
694aa43c215SJeff Kirsher 	adapter->num_txd = MAX_CMD_DESCRIPTORS;
695aa43c215SJeff Kirsher 
696aa43c215SJeff Kirsher 	adapter->max_rds_rings = MAX_RDS_RINGS;
697aa43c215SJeff Kirsher }
698aa43c215SJeff Kirsher 
699aa43c215SJeff Kirsher static int
700aa43c215SJeff Kirsher qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
701aa43c215SJeff Kirsher {
702aa43c215SJeff Kirsher 	int err;
703aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
704aa43c215SJeff Kirsher 
705aa43c215SJeff Kirsher 	err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
706aa43c215SJeff Kirsher 	if (err)
707aa43c215SJeff Kirsher 		return err;
708aa43c215SJeff Kirsher 
709aa43c215SJeff Kirsher 	adapter->physical_port = (u8)nic_info.phys_port;
710aa43c215SJeff Kirsher 	adapter->switch_mode = nic_info.switch_mode;
711aa43c215SJeff Kirsher 	adapter->max_tx_ques = nic_info.max_tx_ques;
712aa43c215SJeff Kirsher 	adapter->max_rx_ques = nic_info.max_rx_ques;
713aa43c215SJeff Kirsher 	adapter->capabilities = nic_info.capabilities;
714aa43c215SJeff Kirsher 	adapter->max_mac_filters = nic_info.max_mac_filters;
715aa43c215SJeff Kirsher 	adapter->max_mtu = nic_info.max_mtu;
716aa43c215SJeff Kirsher 
717aa43c215SJeff Kirsher 	if (adapter->capabilities & BIT_6)
718aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_ESWITCH_ENABLED;
719aa43c215SJeff Kirsher 	else
720aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
721aa43c215SJeff Kirsher 
722aa43c215SJeff Kirsher 	return err;
723aa43c215SJeff Kirsher }
724aa43c215SJeff Kirsher 
725aa43c215SJeff Kirsher static void
726aa43c215SJeff Kirsher qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
727aa43c215SJeff Kirsher 		struct qlcnic_esw_func_cfg *esw_cfg)
728aa43c215SJeff Kirsher {
729aa43c215SJeff Kirsher 	if (esw_cfg->discard_tagged)
730aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
731aa43c215SJeff Kirsher 	else
732aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_TAGGING_ENABLED;
733aa43c215SJeff Kirsher 
734aa43c215SJeff Kirsher 	if (esw_cfg->vlan_id)
735aa43c215SJeff Kirsher 		adapter->pvid = esw_cfg->vlan_id;
736aa43c215SJeff Kirsher 	else
737aa43c215SJeff Kirsher 		adapter->pvid = 0;
738aa43c215SJeff Kirsher }
739aa43c215SJeff Kirsher 
7408e586137SJiri Pirko static int
741aa43c215SJeff Kirsher qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
742aa43c215SJeff Kirsher {
743aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
744aa43c215SJeff Kirsher 	set_bit(vid, adapter->vlans);
7458e586137SJiri Pirko 	return 0;
746aa43c215SJeff Kirsher }
747aa43c215SJeff Kirsher 
7488e586137SJiri Pirko static int
749aa43c215SJeff Kirsher qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
750aa43c215SJeff Kirsher {
751aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
752aa43c215SJeff Kirsher 
753aa43c215SJeff Kirsher 	qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
754aa43c215SJeff Kirsher 	clear_bit(vid, adapter->vlans);
7558e586137SJiri Pirko 	return 0;
756aa43c215SJeff Kirsher }
757aa43c215SJeff Kirsher 
758aa43c215SJeff Kirsher static void
759aa43c215SJeff Kirsher qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
760aa43c215SJeff Kirsher 		struct qlcnic_esw_func_cfg *esw_cfg)
761aa43c215SJeff Kirsher {
762aa43c215SJeff Kirsher 	adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
763aa43c215SJeff Kirsher 				QLCNIC_PROMISC_DISABLED);
764aa43c215SJeff Kirsher 
765aa43c215SJeff Kirsher 	if (esw_cfg->mac_anti_spoof)
766aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MACSPOOF;
767aa43c215SJeff Kirsher 
768aa43c215SJeff Kirsher 	if (!esw_cfg->mac_override)
769aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
770aa43c215SJeff Kirsher 
771aa43c215SJeff Kirsher 	if (!esw_cfg->promisc_mode)
772aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_PROMISC_DISABLED;
773aa43c215SJeff Kirsher 
774aa43c215SJeff Kirsher 	qlcnic_set_netdev_features(adapter, esw_cfg);
775aa43c215SJeff Kirsher }
776aa43c215SJeff Kirsher 
777aa43c215SJeff Kirsher static int
778aa43c215SJeff Kirsher qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
779aa43c215SJeff Kirsher {
780aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
781aa43c215SJeff Kirsher 
782aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
783aa43c215SJeff Kirsher 		return 0;
784aa43c215SJeff Kirsher 
785aa43c215SJeff Kirsher 	esw_cfg.pci_func = adapter->ahw->pci_func;
786aa43c215SJeff Kirsher 	if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
787aa43c215SJeff Kirsher 			return -EIO;
788aa43c215SJeff Kirsher 	qlcnic_set_vlan_config(adapter, &esw_cfg);
789aa43c215SJeff Kirsher 	qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
790aa43c215SJeff Kirsher 
791aa43c215SJeff Kirsher 	return 0;
792aa43c215SJeff Kirsher }
793aa43c215SJeff Kirsher 
794aa43c215SJeff Kirsher static void
795aa43c215SJeff Kirsher qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
796aa43c215SJeff Kirsher 		struct qlcnic_esw_func_cfg *esw_cfg)
797aa43c215SJeff Kirsher {
798aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
799c8f44affSMichał Mirosław 	netdev_features_t features, vlan_features;
800aa43c215SJeff Kirsher 
801aa43c215SJeff Kirsher 	features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
802aa43c215SJeff Kirsher 			NETIF_F_IPV6_CSUM | NETIF_F_GRO);
803aa43c215SJeff Kirsher 	vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
804aa43c215SJeff Kirsher 			NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
805aa43c215SJeff Kirsher 
806aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
807aa43c215SJeff Kirsher 		features |= (NETIF_F_TSO | NETIF_F_TSO6);
808aa43c215SJeff Kirsher 		vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
809aa43c215SJeff Kirsher 	}
810aa43c215SJeff Kirsher 
811aa43c215SJeff Kirsher 	if (netdev->features & NETIF_F_LRO)
812aa43c215SJeff Kirsher 		features |= NETIF_F_LRO;
813aa43c215SJeff Kirsher 
814aa43c215SJeff Kirsher 	if (esw_cfg->offload_flags & BIT_0) {
815aa43c215SJeff Kirsher 		netdev->features |= features;
816aa43c215SJeff Kirsher 		if (!(esw_cfg->offload_flags & BIT_1))
817aa43c215SJeff Kirsher 			netdev->features &= ~NETIF_F_TSO;
818aa43c215SJeff Kirsher 		if (!(esw_cfg->offload_flags & BIT_2))
819aa43c215SJeff Kirsher 			netdev->features &= ~NETIF_F_TSO6;
820aa43c215SJeff Kirsher 	} else {
821aa43c215SJeff Kirsher 		netdev->features &= ~features;
822aa43c215SJeff Kirsher 	}
823aa43c215SJeff Kirsher 
824aa43c215SJeff Kirsher 	netdev->vlan_features = (features & vlan_features);
825aa43c215SJeff Kirsher }
826aa43c215SJeff Kirsher 
827aa43c215SJeff Kirsher static int
828aa43c215SJeff Kirsher qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
829aa43c215SJeff Kirsher {
830aa43c215SJeff Kirsher 	void __iomem *priv_op;
831aa43c215SJeff Kirsher 	u32 op_mode, priv_level;
832aa43c215SJeff Kirsher 	int err = 0;
833aa43c215SJeff Kirsher 
834aa43c215SJeff Kirsher 	err = qlcnic_initialize_nic(adapter);
835aa43c215SJeff Kirsher 	if (err)
836aa43c215SJeff Kirsher 		return err;
837aa43c215SJeff Kirsher 
838aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
839aa43c215SJeff Kirsher 		return 0;
840aa43c215SJeff Kirsher 
841aa43c215SJeff Kirsher 	priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
842aa43c215SJeff Kirsher 	op_mode = readl(priv_op);
843aa43c215SJeff Kirsher 	priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
844aa43c215SJeff Kirsher 
845aa43c215SJeff Kirsher 	if (op_mode == QLC_DEV_DRV_DEFAULT)
846aa43c215SJeff Kirsher 		priv_level = QLCNIC_MGMT_FUNC;
847aa43c215SJeff Kirsher 	else
848aa43c215SJeff Kirsher 		priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
849aa43c215SJeff Kirsher 
850aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
851aa43c215SJeff Kirsher 		if (priv_level == QLCNIC_MGMT_FUNC) {
852aa43c215SJeff Kirsher 			adapter->op_mode = QLCNIC_MGMT_FUNC;
853aa43c215SJeff Kirsher 			err = qlcnic_init_pci_info(adapter);
854aa43c215SJeff Kirsher 			if (err)
855aa43c215SJeff Kirsher 				return err;
856aa43c215SJeff Kirsher 			/* Set privilege level for other functions */
857aa43c215SJeff Kirsher 			qlcnic_set_function_modes(adapter);
858aa43c215SJeff Kirsher 			dev_info(&adapter->pdev->dev,
859aa43c215SJeff Kirsher 				"HAL Version: %d, Management function\n",
860aa43c215SJeff Kirsher 				adapter->fw_hal_version);
861aa43c215SJeff Kirsher 		} else if (priv_level == QLCNIC_PRIV_FUNC) {
862aa43c215SJeff Kirsher 			adapter->op_mode = QLCNIC_PRIV_FUNC;
863aa43c215SJeff Kirsher 			dev_info(&adapter->pdev->dev,
864aa43c215SJeff Kirsher 				"HAL Version: %d, Privileged function\n",
865aa43c215SJeff Kirsher 				adapter->fw_hal_version);
866aa43c215SJeff Kirsher 		}
867aa43c215SJeff Kirsher 	}
868aa43c215SJeff Kirsher 
869aa43c215SJeff Kirsher 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
870aa43c215SJeff Kirsher 
871aa43c215SJeff Kirsher 	return err;
872aa43c215SJeff Kirsher }
873aa43c215SJeff Kirsher 
874aa43c215SJeff Kirsher static int
875aa43c215SJeff Kirsher qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
876aa43c215SJeff Kirsher {
877aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
878aa43c215SJeff Kirsher 	struct qlcnic_npar_info *npar;
879aa43c215SJeff Kirsher 	u8 i;
880aa43c215SJeff Kirsher 
881aa43c215SJeff Kirsher 	if (adapter->need_fw_reset)
882aa43c215SJeff Kirsher 		return 0;
883aa43c215SJeff Kirsher 
884aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
885aa43c215SJeff Kirsher 		if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
886aa43c215SJeff Kirsher 			continue;
887aa43c215SJeff Kirsher 		memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
888aa43c215SJeff Kirsher 		esw_cfg.pci_func = i;
889aa43c215SJeff Kirsher 		esw_cfg.offload_flags = BIT_0;
890aa43c215SJeff Kirsher 		esw_cfg.mac_override = BIT_0;
891aa43c215SJeff Kirsher 		esw_cfg.promisc_mode = BIT_0;
892aa43c215SJeff Kirsher 		if (adapter->capabilities  & QLCNIC_FW_CAPABILITY_TSO)
893aa43c215SJeff Kirsher 			esw_cfg.offload_flags |= (BIT_1 | BIT_2);
894aa43c215SJeff Kirsher 		if (qlcnic_config_switch_port(adapter, &esw_cfg))
895aa43c215SJeff Kirsher 			return -EIO;
896aa43c215SJeff Kirsher 		npar = &adapter->npars[i];
897aa43c215SJeff Kirsher 		npar->pvid = esw_cfg.vlan_id;
898aa43c215SJeff Kirsher 		npar->mac_override = esw_cfg.mac_override;
899aa43c215SJeff Kirsher 		npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
900aa43c215SJeff Kirsher 		npar->discard_tagged = esw_cfg.discard_tagged;
901aa43c215SJeff Kirsher 		npar->promisc_mode = esw_cfg.promisc_mode;
902aa43c215SJeff Kirsher 		npar->offload_flags = esw_cfg.offload_flags;
903aa43c215SJeff Kirsher 	}
904aa43c215SJeff Kirsher 
905aa43c215SJeff Kirsher 	return 0;
906aa43c215SJeff Kirsher }
907aa43c215SJeff Kirsher 
908aa43c215SJeff Kirsher static int
909aa43c215SJeff Kirsher qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
910aa43c215SJeff Kirsher 			struct qlcnic_npar_info *npar, int pci_func)
911aa43c215SJeff Kirsher {
912aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg;
913aa43c215SJeff Kirsher 	esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
914aa43c215SJeff Kirsher 	esw_cfg.pci_func = pci_func;
915aa43c215SJeff Kirsher 	esw_cfg.vlan_id = npar->pvid;
916aa43c215SJeff Kirsher 	esw_cfg.mac_override = npar->mac_override;
917aa43c215SJeff Kirsher 	esw_cfg.discard_tagged = npar->discard_tagged;
918aa43c215SJeff Kirsher 	esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
919aa43c215SJeff Kirsher 	esw_cfg.offload_flags = npar->offload_flags;
920aa43c215SJeff Kirsher 	esw_cfg.promisc_mode = npar->promisc_mode;
921aa43c215SJeff Kirsher 	if (qlcnic_config_switch_port(adapter, &esw_cfg))
922aa43c215SJeff Kirsher 		return -EIO;
923aa43c215SJeff Kirsher 
924aa43c215SJeff Kirsher 	esw_cfg.op_mode = QLCNIC_ADD_VLAN;
925aa43c215SJeff Kirsher 	if (qlcnic_config_switch_port(adapter, &esw_cfg))
926aa43c215SJeff Kirsher 		return -EIO;
927aa43c215SJeff Kirsher 
928aa43c215SJeff Kirsher 	return 0;
929aa43c215SJeff Kirsher }
930aa43c215SJeff Kirsher 
931aa43c215SJeff Kirsher static int
932aa43c215SJeff Kirsher qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
933aa43c215SJeff Kirsher {
934aa43c215SJeff Kirsher 	int i, err;
935aa43c215SJeff Kirsher 	struct qlcnic_npar_info *npar;
936aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
937aa43c215SJeff Kirsher 
938aa43c215SJeff Kirsher 	if (!adapter->need_fw_reset)
939aa43c215SJeff Kirsher 		return 0;
940aa43c215SJeff Kirsher 
941aa43c215SJeff Kirsher 	/* Set the NPAR config data after FW reset */
942aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
943aa43c215SJeff Kirsher 		npar = &adapter->npars[i];
944aa43c215SJeff Kirsher 		if (npar->type != QLCNIC_TYPE_NIC)
945aa43c215SJeff Kirsher 			continue;
946aa43c215SJeff Kirsher 		err = qlcnic_get_nic_info(adapter, &nic_info, i);
947aa43c215SJeff Kirsher 		if (err)
948aa43c215SJeff Kirsher 			return err;
949aa43c215SJeff Kirsher 		nic_info.min_tx_bw = npar->min_bw;
950aa43c215SJeff Kirsher 		nic_info.max_tx_bw = npar->max_bw;
951aa43c215SJeff Kirsher 		err = qlcnic_set_nic_info(adapter, &nic_info);
952aa43c215SJeff Kirsher 		if (err)
953aa43c215SJeff Kirsher 			return err;
954aa43c215SJeff Kirsher 
955aa43c215SJeff Kirsher 		if (npar->enable_pm) {
956aa43c215SJeff Kirsher 			err = qlcnic_config_port_mirroring(adapter,
957aa43c215SJeff Kirsher 							npar->dest_npar, 1, i);
958aa43c215SJeff Kirsher 			if (err)
959aa43c215SJeff Kirsher 				return err;
960aa43c215SJeff Kirsher 		}
961aa43c215SJeff Kirsher 		err = qlcnic_reset_eswitch_config(adapter, npar, i);
962aa43c215SJeff Kirsher 		if (err)
963aa43c215SJeff Kirsher 			return err;
964aa43c215SJeff Kirsher 	}
965aa43c215SJeff Kirsher 	return 0;
966aa43c215SJeff Kirsher }
967aa43c215SJeff Kirsher 
968aa43c215SJeff Kirsher static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
969aa43c215SJeff Kirsher {
970aa43c215SJeff Kirsher 	u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
971aa43c215SJeff Kirsher 	u32 npar_state;
972aa43c215SJeff Kirsher 
973aa43c215SJeff Kirsher 	if (adapter->op_mode == QLCNIC_MGMT_FUNC)
974aa43c215SJeff Kirsher 		return 0;
975aa43c215SJeff Kirsher 
976aa43c215SJeff Kirsher 	npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
977aa43c215SJeff Kirsher 	while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
978aa43c215SJeff Kirsher 		msleep(1000);
979aa43c215SJeff Kirsher 		npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
980aa43c215SJeff Kirsher 	}
981aa43c215SJeff Kirsher 	if (!npar_opt_timeo) {
982aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
983aa43c215SJeff Kirsher 			"Waiting for NPAR state to opertional timeout\n");
984aa43c215SJeff Kirsher 		return -EIO;
985aa43c215SJeff Kirsher 	}
986aa43c215SJeff Kirsher 	return 0;
987aa43c215SJeff Kirsher }
988aa43c215SJeff Kirsher 
989aa43c215SJeff Kirsher static int
990aa43c215SJeff Kirsher qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
991aa43c215SJeff Kirsher {
992aa43c215SJeff Kirsher 	int err;
993aa43c215SJeff Kirsher 
994aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
995aa43c215SJeff Kirsher 		    adapter->op_mode != QLCNIC_MGMT_FUNC)
996aa43c215SJeff Kirsher 		return 0;
997aa43c215SJeff Kirsher 
998aa43c215SJeff Kirsher 	err = qlcnic_set_default_offload_settings(adapter);
999aa43c215SJeff Kirsher 	if (err)
1000aa43c215SJeff Kirsher 		return err;
1001aa43c215SJeff Kirsher 
1002aa43c215SJeff Kirsher 	err = qlcnic_reset_npar_config(adapter);
1003aa43c215SJeff Kirsher 	if (err)
1004aa43c215SJeff Kirsher 		return err;
1005aa43c215SJeff Kirsher 
1006aa43c215SJeff Kirsher 	qlcnic_dev_set_npar_ready(adapter);
1007aa43c215SJeff Kirsher 
1008aa43c215SJeff Kirsher 	return err;
1009aa43c215SJeff Kirsher }
1010aa43c215SJeff Kirsher 
1011aa43c215SJeff Kirsher static int
1012aa43c215SJeff Kirsher qlcnic_start_firmware(struct qlcnic_adapter *adapter)
1013aa43c215SJeff Kirsher {
1014aa43c215SJeff Kirsher 	int err;
1015aa43c215SJeff Kirsher 
1016aa43c215SJeff Kirsher 	err = qlcnic_can_start_firmware(adapter);
1017aa43c215SJeff Kirsher 	if (err < 0)
1018aa43c215SJeff Kirsher 		return err;
1019aa43c215SJeff Kirsher 	else if (!err)
1020aa43c215SJeff Kirsher 		goto check_fw_status;
1021aa43c215SJeff Kirsher 
1022aa43c215SJeff Kirsher 	if (load_fw_file)
1023aa43c215SJeff Kirsher 		qlcnic_request_firmware(adapter);
1024aa43c215SJeff Kirsher 	else {
1025aa43c215SJeff Kirsher 		err = qlcnic_check_flash_fw_ver(adapter);
1026aa43c215SJeff Kirsher 		if (err)
1027aa43c215SJeff Kirsher 			goto err_out;
1028aa43c215SJeff Kirsher 
1029aa43c215SJeff Kirsher 		adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
1030aa43c215SJeff Kirsher 	}
1031aa43c215SJeff Kirsher 
1032aa43c215SJeff Kirsher 	err = qlcnic_need_fw_reset(adapter);
1033aa43c215SJeff Kirsher 	if (err == 0)
1034aa43c215SJeff Kirsher 		goto check_fw_status;
1035aa43c215SJeff Kirsher 
1036aa43c215SJeff Kirsher 	err = qlcnic_pinit_from_rom(adapter);
1037aa43c215SJeff Kirsher 	if (err)
1038aa43c215SJeff Kirsher 		goto err_out;
1039aa43c215SJeff Kirsher 
1040aa43c215SJeff Kirsher 	err = qlcnic_load_firmware(adapter);
1041aa43c215SJeff Kirsher 	if (err)
1042aa43c215SJeff Kirsher 		goto err_out;
1043aa43c215SJeff Kirsher 
1044aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1045aa43c215SJeff Kirsher 	QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1046aa43c215SJeff Kirsher 
1047aa43c215SJeff Kirsher check_fw_status:
1048aa43c215SJeff Kirsher 	err = qlcnic_check_fw_status(adapter);
1049aa43c215SJeff Kirsher 	if (err)
1050aa43c215SJeff Kirsher 		goto err_out;
1051aa43c215SJeff Kirsher 
1052aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1053aa43c215SJeff Kirsher 	qlcnic_idc_debug_info(adapter, 1);
1054aa43c215SJeff Kirsher 
1055aa43c215SJeff Kirsher 	err = qlcnic_check_eswitch_mode(adapter);
1056aa43c215SJeff Kirsher 	if (err) {
1057aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
1058aa43c215SJeff Kirsher 			"Memory allocation failed for eswitch\n");
1059aa43c215SJeff Kirsher 		goto err_out;
1060aa43c215SJeff Kirsher 	}
1061aa43c215SJeff Kirsher 	err = qlcnic_set_mgmt_operations(adapter);
1062aa43c215SJeff Kirsher 	if (err)
1063aa43c215SJeff Kirsher 		goto err_out;
1064aa43c215SJeff Kirsher 
1065aa43c215SJeff Kirsher 	qlcnic_check_options(adapter);
1066aa43c215SJeff Kirsher 	adapter->need_fw_reset = 0;
1067aa43c215SJeff Kirsher 
1068aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1069aa43c215SJeff Kirsher 	return 0;
1070aa43c215SJeff Kirsher 
1071aa43c215SJeff Kirsher err_out:
1072aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1073aa43c215SJeff Kirsher 	dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1074aa43c215SJeff Kirsher 
1075aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1076aa43c215SJeff Kirsher 	return err;
1077aa43c215SJeff Kirsher }
1078aa43c215SJeff Kirsher 
1079aa43c215SJeff Kirsher static int
1080aa43c215SJeff Kirsher qlcnic_request_irq(struct qlcnic_adapter *adapter)
1081aa43c215SJeff Kirsher {
1082aa43c215SJeff Kirsher 	irq_handler_t handler;
1083aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1084aa43c215SJeff Kirsher 	int err, ring;
1085aa43c215SJeff Kirsher 
1086aa43c215SJeff Kirsher 	unsigned long flags = 0;
1087aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1088aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1089aa43c215SJeff Kirsher 
1090aa43c215SJeff Kirsher 	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1091aa43c215SJeff Kirsher 		handler = qlcnic_tmp_intr;
1092aa43c215SJeff Kirsher 		if (!QLCNIC_IS_MSI_FAMILY(adapter))
1093aa43c215SJeff Kirsher 			flags |= IRQF_SHARED;
1094aa43c215SJeff Kirsher 
1095aa43c215SJeff Kirsher 	} else {
1096aa43c215SJeff Kirsher 		if (adapter->flags & QLCNIC_MSIX_ENABLED)
1097aa43c215SJeff Kirsher 			handler = qlcnic_msix_intr;
1098aa43c215SJeff Kirsher 		else if (adapter->flags & QLCNIC_MSI_ENABLED)
1099aa43c215SJeff Kirsher 			handler = qlcnic_msi_intr;
1100aa43c215SJeff Kirsher 		else {
1101aa43c215SJeff Kirsher 			flags |= IRQF_SHARED;
1102aa43c215SJeff Kirsher 			handler = qlcnic_intr;
1103aa43c215SJeff Kirsher 		}
1104aa43c215SJeff Kirsher 	}
1105aa43c215SJeff Kirsher 	adapter->irq = netdev->irq;
1106aa43c215SJeff Kirsher 
1107aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1108aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
1109aa43c215SJeff Kirsher 		sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1110aa43c215SJeff Kirsher 		err = request_irq(sds_ring->irq, handler,
1111aa43c215SJeff Kirsher 				  flags, sds_ring->name, sds_ring);
1112aa43c215SJeff Kirsher 		if (err)
1113aa43c215SJeff Kirsher 			return err;
1114aa43c215SJeff Kirsher 	}
1115aa43c215SJeff Kirsher 
1116aa43c215SJeff Kirsher 	return 0;
1117aa43c215SJeff Kirsher }
1118aa43c215SJeff Kirsher 
1119aa43c215SJeff Kirsher static void
1120aa43c215SJeff Kirsher qlcnic_free_irq(struct qlcnic_adapter *adapter)
1121aa43c215SJeff Kirsher {
1122aa43c215SJeff Kirsher 	int ring;
1123aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1124aa43c215SJeff Kirsher 
1125aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1126aa43c215SJeff Kirsher 
1127aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1128aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
1129aa43c215SJeff Kirsher 		free_irq(sds_ring->irq, sds_ring);
1130aa43c215SJeff Kirsher 	}
1131aa43c215SJeff Kirsher }
1132aa43c215SJeff Kirsher 
1133aa43c215SJeff Kirsher static int
1134aa43c215SJeff Kirsher __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1135aa43c215SJeff Kirsher {
1136aa43c215SJeff Kirsher 	int ring;
1137cae82d49SRajesh Borundia 	u32 capab2;
1138cae82d49SRajesh Borundia 
1139aa43c215SJeff Kirsher 	struct qlcnic_host_rds_ring *rds_ring;
1140aa43c215SJeff Kirsher 
1141aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1142aa43c215SJeff Kirsher 		return -EIO;
1143aa43c215SJeff Kirsher 
1144aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1145aa43c215SJeff Kirsher 		return 0;
1146aa43c215SJeff Kirsher 	if (qlcnic_set_eswitch_port_config(adapter))
1147aa43c215SJeff Kirsher 		return -EIO;
1148aa43c215SJeff Kirsher 
1149cae82d49SRajesh Borundia 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
1150cae82d49SRajesh Borundia 		capab2 = QLCRD32(adapter, CRB_FW_CAPABILITIES_2);
1151cae82d49SRajesh Borundia 		if (capab2 & QLCNIC_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1152cae82d49SRajesh Borundia 			adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1153cae82d49SRajesh Borundia 	}
1154cae82d49SRajesh Borundia 
1155aa43c215SJeff Kirsher 	if (qlcnic_fw_create_ctx(adapter))
1156aa43c215SJeff Kirsher 		return -EIO;
1157aa43c215SJeff Kirsher 
1158aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1159aa43c215SJeff Kirsher 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1160aa43c215SJeff Kirsher 		qlcnic_post_rx_buffers(adapter, rds_ring);
1161aa43c215SJeff Kirsher 	}
1162aa43c215SJeff Kirsher 
1163aa43c215SJeff Kirsher 	qlcnic_set_multi(netdev);
1164aa43c215SJeff Kirsher 	qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1165aa43c215SJeff Kirsher 
1166aa43c215SJeff Kirsher 	adapter->ahw->linkup = 0;
1167aa43c215SJeff Kirsher 
1168aa43c215SJeff Kirsher 	if (adapter->max_sds_rings > 1)
1169aa43c215SJeff Kirsher 		qlcnic_config_rss(adapter, 1);
1170aa43c215SJeff Kirsher 
1171aa43c215SJeff Kirsher 	qlcnic_config_intr_coalesce(adapter);
1172aa43c215SJeff Kirsher 
1173aa43c215SJeff Kirsher 	if (netdev->features & NETIF_F_LRO)
1174aa43c215SJeff Kirsher 		qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1175aa43c215SJeff Kirsher 
1176aa43c215SJeff Kirsher 	qlcnic_napi_enable(adapter);
1177aa43c215SJeff Kirsher 
1178aa43c215SJeff Kirsher 	qlcnic_linkevent_request(adapter, 1);
1179aa43c215SJeff Kirsher 
1180aa43c215SJeff Kirsher 	adapter->reset_context = 0;
1181aa43c215SJeff Kirsher 	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1182aa43c215SJeff Kirsher 	return 0;
1183aa43c215SJeff Kirsher }
1184aa43c215SJeff Kirsher 
1185aa43c215SJeff Kirsher /* Usage: During resume and firmware recovery module.*/
1186aa43c215SJeff Kirsher 
1187aa43c215SJeff Kirsher static int
1188aa43c215SJeff Kirsher qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1189aa43c215SJeff Kirsher {
1190aa43c215SJeff Kirsher 	int err = 0;
1191aa43c215SJeff Kirsher 
1192aa43c215SJeff Kirsher 	rtnl_lock();
1193aa43c215SJeff Kirsher 	if (netif_running(netdev))
1194aa43c215SJeff Kirsher 		err = __qlcnic_up(adapter, netdev);
1195aa43c215SJeff Kirsher 	rtnl_unlock();
1196aa43c215SJeff Kirsher 
1197aa43c215SJeff Kirsher 	return err;
1198aa43c215SJeff Kirsher }
1199aa43c215SJeff Kirsher 
1200aa43c215SJeff Kirsher static void
1201aa43c215SJeff Kirsher __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1202aa43c215SJeff Kirsher {
1203aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1204aa43c215SJeff Kirsher 		return;
1205aa43c215SJeff Kirsher 
1206aa43c215SJeff Kirsher 	if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1207aa43c215SJeff Kirsher 		return;
1208aa43c215SJeff Kirsher 
1209aa43c215SJeff Kirsher 	smp_mb();
1210aa43c215SJeff Kirsher 	spin_lock(&adapter->tx_clean_lock);
1211aa43c215SJeff Kirsher 	netif_carrier_off(netdev);
1212aa43c215SJeff Kirsher 	netif_tx_disable(netdev);
1213aa43c215SJeff Kirsher 
1214aa43c215SJeff Kirsher 	qlcnic_free_mac_list(adapter);
1215aa43c215SJeff Kirsher 
1216aa43c215SJeff Kirsher 	if (adapter->fhash.fnum)
1217aa43c215SJeff Kirsher 		qlcnic_delete_lb_filters(adapter);
1218aa43c215SJeff Kirsher 
1219aa43c215SJeff Kirsher 	qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1220aa43c215SJeff Kirsher 
1221aa43c215SJeff Kirsher 	qlcnic_napi_disable(adapter);
1222aa43c215SJeff Kirsher 
1223aa43c215SJeff Kirsher 	qlcnic_fw_destroy_ctx(adapter);
1224cae82d49SRajesh Borundia 	adapter->flags &= ~QLCNIC_FW_LRO_MSS_CAP;
1225aa43c215SJeff Kirsher 
1226aa43c215SJeff Kirsher 	qlcnic_reset_rx_buffers_list(adapter);
1227aa43c215SJeff Kirsher 	qlcnic_release_tx_buffers(adapter);
1228aa43c215SJeff Kirsher 	spin_unlock(&adapter->tx_clean_lock);
1229aa43c215SJeff Kirsher }
1230aa43c215SJeff Kirsher 
1231aa43c215SJeff Kirsher /* Usage: During suspend and firmware recovery module */
1232aa43c215SJeff Kirsher 
1233aa43c215SJeff Kirsher static void
1234aa43c215SJeff Kirsher qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1235aa43c215SJeff Kirsher {
1236aa43c215SJeff Kirsher 	rtnl_lock();
1237aa43c215SJeff Kirsher 	if (netif_running(netdev))
1238aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
1239aa43c215SJeff Kirsher 	rtnl_unlock();
1240aa43c215SJeff Kirsher 
1241aa43c215SJeff Kirsher }
1242aa43c215SJeff Kirsher 
1243aa43c215SJeff Kirsher static int
1244aa43c215SJeff Kirsher qlcnic_attach(struct qlcnic_adapter *adapter)
1245aa43c215SJeff Kirsher {
1246aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1247aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1248aa43c215SJeff Kirsher 	int err;
1249aa43c215SJeff Kirsher 
1250aa43c215SJeff Kirsher 	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1251aa43c215SJeff Kirsher 		return 0;
1252aa43c215SJeff Kirsher 
1253aa43c215SJeff Kirsher 	err = qlcnic_napi_add(adapter, netdev);
1254aa43c215SJeff Kirsher 	if (err)
1255aa43c215SJeff Kirsher 		return err;
1256aa43c215SJeff Kirsher 
1257aa43c215SJeff Kirsher 	err = qlcnic_alloc_sw_resources(adapter);
1258aa43c215SJeff Kirsher 	if (err) {
1259aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Error in setting sw resources\n");
1260aa43c215SJeff Kirsher 		goto err_out_napi_del;
1261aa43c215SJeff Kirsher 	}
1262aa43c215SJeff Kirsher 
1263aa43c215SJeff Kirsher 	err = qlcnic_alloc_hw_resources(adapter);
1264aa43c215SJeff Kirsher 	if (err) {
1265aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Error in setting hw resources\n");
1266aa43c215SJeff Kirsher 		goto err_out_free_sw;
1267aa43c215SJeff Kirsher 	}
1268aa43c215SJeff Kirsher 
1269aa43c215SJeff Kirsher 	err = qlcnic_request_irq(adapter);
1270aa43c215SJeff Kirsher 	if (err) {
1271aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to setup interrupt\n");
1272aa43c215SJeff Kirsher 		goto err_out_free_hw;
1273aa43c215SJeff Kirsher 	}
1274aa43c215SJeff Kirsher 
1275aa43c215SJeff Kirsher 	qlcnic_create_sysfs_entries(adapter);
1276aa43c215SJeff Kirsher 
1277aa43c215SJeff Kirsher 	adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1278aa43c215SJeff Kirsher 	return 0;
1279aa43c215SJeff Kirsher 
1280aa43c215SJeff Kirsher err_out_free_hw:
1281aa43c215SJeff Kirsher 	qlcnic_free_hw_resources(adapter);
1282aa43c215SJeff Kirsher err_out_free_sw:
1283aa43c215SJeff Kirsher 	qlcnic_free_sw_resources(adapter);
1284aa43c215SJeff Kirsher err_out_napi_del:
1285aa43c215SJeff Kirsher 	qlcnic_napi_del(adapter);
1286aa43c215SJeff Kirsher 	return err;
1287aa43c215SJeff Kirsher }
1288aa43c215SJeff Kirsher 
1289aa43c215SJeff Kirsher static void
1290aa43c215SJeff Kirsher qlcnic_detach(struct qlcnic_adapter *adapter)
1291aa43c215SJeff Kirsher {
1292aa43c215SJeff Kirsher 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1293aa43c215SJeff Kirsher 		return;
1294aa43c215SJeff Kirsher 
1295aa43c215SJeff Kirsher 	qlcnic_remove_sysfs_entries(adapter);
1296aa43c215SJeff Kirsher 
1297aa43c215SJeff Kirsher 	qlcnic_free_hw_resources(adapter);
1298aa43c215SJeff Kirsher 	qlcnic_release_rx_buffers(adapter);
1299aa43c215SJeff Kirsher 	qlcnic_free_irq(adapter);
1300aa43c215SJeff Kirsher 	qlcnic_napi_del(adapter);
1301aa43c215SJeff Kirsher 	qlcnic_free_sw_resources(adapter);
1302aa43c215SJeff Kirsher 
1303aa43c215SJeff Kirsher 	adapter->is_up = 0;
1304aa43c215SJeff Kirsher }
1305aa43c215SJeff Kirsher 
1306aa43c215SJeff Kirsher void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1307aa43c215SJeff Kirsher {
1308aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1309aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1310aa43c215SJeff Kirsher 	int ring;
1311aa43c215SJeff Kirsher 
1312aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_DEV_UP, &adapter->state);
1313aa43c215SJeff Kirsher 	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1314aa43c215SJeff Kirsher 		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1315aa43c215SJeff Kirsher 			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1316aa43c215SJeff Kirsher 			qlcnic_disable_int(sds_ring);
1317aa43c215SJeff Kirsher 		}
1318aa43c215SJeff Kirsher 	}
1319aa43c215SJeff Kirsher 
1320aa43c215SJeff Kirsher 	qlcnic_fw_destroy_ctx(adapter);
1321aa43c215SJeff Kirsher 
1322aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1323aa43c215SJeff Kirsher 
1324aa43c215SJeff Kirsher 	adapter->diag_test = 0;
1325aa43c215SJeff Kirsher 	adapter->max_sds_rings = max_sds_rings;
1326aa43c215SJeff Kirsher 
1327aa43c215SJeff Kirsher 	if (qlcnic_attach(adapter))
1328aa43c215SJeff Kirsher 		goto out;
1329aa43c215SJeff Kirsher 
1330aa43c215SJeff Kirsher 	if (netif_running(netdev))
1331aa43c215SJeff Kirsher 		__qlcnic_up(adapter, netdev);
1332aa43c215SJeff Kirsher out:
1333aa43c215SJeff Kirsher 	netif_device_attach(netdev);
1334aa43c215SJeff Kirsher }
1335aa43c215SJeff Kirsher 
1336aa43c215SJeff Kirsher static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1337aa43c215SJeff Kirsher {
1338aa43c215SJeff Kirsher 	int err = 0;
1339aa43c215SJeff Kirsher 	adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1340aa43c215SJeff Kirsher 				GFP_KERNEL);
1341aa43c215SJeff Kirsher 	if (!adapter->ahw) {
1342aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
1343aa43c215SJeff Kirsher 			"Failed to allocate recv ctx resources for adapter\n");
1344aa43c215SJeff Kirsher 		err = -ENOMEM;
1345aa43c215SJeff Kirsher 		goto err_out;
1346aa43c215SJeff Kirsher 	}
1347aa43c215SJeff Kirsher 	adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1348aa43c215SJeff Kirsher 				GFP_KERNEL);
1349aa43c215SJeff Kirsher 	if (!adapter->recv_ctx) {
1350aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
1351aa43c215SJeff Kirsher 			"Failed to allocate recv ctx resources for adapter\n");
1352aa43c215SJeff Kirsher 		kfree(adapter->ahw);
1353aa43c215SJeff Kirsher 		adapter->ahw = NULL;
1354aa43c215SJeff Kirsher 		err = -ENOMEM;
1355aa43c215SJeff Kirsher 		goto err_out;
1356aa43c215SJeff Kirsher 	}
1357aa43c215SJeff Kirsher 	/* Initialize interrupt coalesce parameters */
1358aa43c215SJeff Kirsher 	adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1359aa43c215SJeff Kirsher 	adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1360aa43c215SJeff Kirsher 	adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
1361aa43c215SJeff Kirsher err_out:
1362aa43c215SJeff Kirsher 	return err;
1363aa43c215SJeff Kirsher }
1364aa43c215SJeff Kirsher 
1365aa43c215SJeff Kirsher static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1366aa43c215SJeff Kirsher {
1367aa43c215SJeff Kirsher 	kfree(adapter->recv_ctx);
1368aa43c215SJeff Kirsher 	adapter->recv_ctx = NULL;
1369aa43c215SJeff Kirsher 
1370aa43c215SJeff Kirsher 	if (adapter->ahw->fw_dump.tmpl_hdr) {
1371aa43c215SJeff Kirsher 		vfree(adapter->ahw->fw_dump.tmpl_hdr);
1372aa43c215SJeff Kirsher 		adapter->ahw->fw_dump.tmpl_hdr = NULL;
1373aa43c215SJeff Kirsher 	}
1374aa43c215SJeff Kirsher 	kfree(adapter->ahw);
1375aa43c215SJeff Kirsher 	adapter->ahw = NULL;
1376aa43c215SJeff Kirsher }
1377aa43c215SJeff Kirsher 
1378aa43c215SJeff Kirsher int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1379aa43c215SJeff Kirsher {
1380aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1381aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
1382aa43c215SJeff Kirsher 	struct qlcnic_host_rds_ring *rds_ring;
1383aa43c215SJeff Kirsher 	int ring;
1384aa43c215SJeff Kirsher 	int ret;
1385aa43c215SJeff Kirsher 
1386aa43c215SJeff Kirsher 	netif_device_detach(netdev);
1387aa43c215SJeff Kirsher 
1388aa43c215SJeff Kirsher 	if (netif_running(netdev))
1389aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
1390aa43c215SJeff Kirsher 
1391aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1392aa43c215SJeff Kirsher 
1393aa43c215SJeff Kirsher 	adapter->max_sds_rings = 1;
1394aa43c215SJeff Kirsher 	adapter->diag_test = test;
1395aa43c215SJeff Kirsher 
1396aa43c215SJeff Kirsher 	ret = qlcnic_attach(adapter);
1397aa43c215SJeff Kirsher 	if (ret) {
1398aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1399aa43c215SJeff Kirsher 		return ret;
1400aa43c215SJeff Kirsher 	}
1401aa43c215SJeff Kirsher 
1402aa43c215SJeff Kirsher 	ret = qlcnic_fw_create_ctx(adapter);
1403aa43c215SJeff Kirsher 	if (ret) {
1404aa43c215SJeff Kirsher 		qlcnic_detach(adapter);
1405aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1406aa43c215SJeff Kirsher 		return ret;
1407aa43c215SJeff Kirsher 	}
1408aa43c215SJeff Kirsher 
1409aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1410aa43c215SJeff Kirsher 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1411aa43c215SJeff Kirsher 		qlcnic_post_rx_buffers(adapter, rds_ring);
1412aa43c215SJeff Kirsher 	}
1413aa43c215SJeff Kirsher 
1414aa43c215SJeff Kirsher 	if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1415aa43c215SJeff Kirsher 		for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1416aa43c215SJeff Kirsher 			sds_ring = &adapter->recv_ctx->sds_rings[ring];
1417aa43c215SJeff Kirsher 			qlcnic_enable_int(sds_ring);
1418aa43c215SJeff Kirsher 		}
1419aa43c215SJeff Kirsher 	}
1420aa43c215SJeff Kirsher 
1421aa43c215SJeff Kirsher 	if (adapter->diag_test == QLCNIC_LOOPBACK_TEST) {
1422aa43c215SJeff Kirsher 		adapter->ahw->loopback_state = 0;
1423aa43c215SJeff Kirsher 		qlcnic_linkevent_request(adapter, 1);
1424aa43c215SJeff Kirsher 	}
1425aa43c215SJeff Kirsher 
1426aa43c215SJeff Kirsher 	set_bit(__QLCNIC_DEV_UP, &adapter->state);
1427aa43c215SJeff Kirsher 
1428aa43c215SJeff Kirsher 	return 0;
1429aa43c215SJeff Kirsher }
1430aa43c215SJeff Kirsher 
1431aa43c215SJeff Kirsher /* Reset context in hardware only */
1432aa43c215SJeff Kirsher static int
1433aa43c215SJeff Kirsher qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1434aa43c215SJeff Kirsher {
1435aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1436aa43c215SJeff Kirsher 
1437aa43c215SJeff Kirsher 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1438aa43c215SJeff Kirsher 		return -EBUSY;
1439aa43c215SJeff Kirsher 
1440aa43c215SJeff Kirsher 	netif_device_detach(netdev);
1441aa43c215SJeff Kirsher 
1442aa43c215SJeff Kirsher 	qlcnic_down(adapter, netdev);
1443aa43c215SJeff Kirsher 
1444aa43c215SJeff Kirsher 	qlcnic_up(adapter, netdev);
1445aa43c215SJeff Kirsher 
1446aa43c215SJeff Kirsher 	netif_device_attach(netdev);
1447aa43c215SJeff Kirsher 
1448aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1449aa43c215SJeff Kirsher 	return 0;
1450aa43c215SJeff Kirsher }
1451aa43c215SJeff Kirsher 
1452aa43c215SJeff Kirsher int
1453aa43c215SJeff Kirsher qlcnic_reset_context(struct qlcnic_adapter *adapter)
1454aa43c215SJeff Kirsher {
1455aa43c215SJeff Kirsher 	int err = 0;
1456aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1457aa43c215SJeff Kirsher 
1458aa43c215SJeff Kirsher 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1459aa43c215SJeff Kirsher 		return -EBUSY;
1460aa43c215SJeff Kirsher 
1461aa43c215SJeff Kirsher 	if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1462aa43c215SJeff Kirsher 
1463aa43c215SJeff Kirsher 		netif_device_detach(netdev);
1464aa43c215SJeff Kirsher 
1465aa43c215SJeff Kirsher 		if (netif_running(netdev))
1466aa43c215SJeff Kirsher 			__qlcnic_down(adapter, netdev);
1467aa43c215SJeff Kirsher 
1468aa43c215SJeff Kirsher 		qlcnic_detach(adapter);
1469aa43c215SJeff Kirsher 
1470aa43c215SJeff Kirsher 		if (netif_running(netdev)) {
1471aa43c215SJeff Kirsher 			err = qlcnic_attach(adapter);
14721d5c88e3SAnirban Chakraborty 			if (!err) {
1473aa43c215SJeff Kirsher 				__qlcnic_up(adapter, netdev);
14741d5c88e3SAnirban Chakraborty 				qlcnic_restore_indev_addr(netdev, NETDEV_UP);
14751d5c88e3SAnirban Chakraborty 			}
1476aa43c215SJeff Kirsher 		}
1477aa43c215SJeff Kirsher 
1478aa43c215SJeff Kirsher 		netif_device_attach(netdev);
1479aa43c215SJeff Kirsher 	}
1480aa43c215SJeff Kirsher 
1481aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1482aa43c215SJeff Kirsher 	return err;
1483aa43c215SJeff Kirsher }
1484aa43c215SJeff Kirsher 
1485aa43c215SJeff Kirsher static int
14865ad6ff9dSSony Chacko qlcnic_setup_netdev(struct qlcnic_adapter *adapter, struct net_device *netdev,
14875ad6ff9dSSony Chacko 		    int pci_using_dac)
1488aa43c215SJeff Kirsher {
1489aa43c215SJeff Kirsher 	int err;
1490aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
1491aa43c215SJeff Kirsher 
1492aa43c215SJeff Kirsher 	adapter->mc_enabled = 0;
1493aa43c215SJeff Kirsher 	adapter->max_mc_count = 38;
1494aa43c215SJeff Kirsher 
1495aa43c215SJeff Kirsher 	netdev->netdev_ops	   = &qlcnic_netdev_ops;
1496aa43c215SJeff Kirsher 	netdev->watchdog_timeo     = 5*HZ;
1497aa43c215SJeff Kirsher 
1498aa43c215SJeff Kirsher 	qlcnic_change_mtu(netdev, netdev->mtu);
1499aa43c215SJeff Kirsher 
1500aa43c215SJeff Kirsher 	SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1501aa43c215SJeff Kirsher 
1502aa43c215SJeff Kirsher 	netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1503aa43c215SJeff Kirsher 		NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
1504aa43c215SJeff Kirsher 
1505aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1506aa43c215SJeff Kirsher 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
15075ad6ff9dSSony Chacko 	if (pci_using_dac == 1)
1508aa43c215SJeff Kirsher 		netdev->hw_features |= NETIF_F_HIGHDMA;
1509aa43c215SJeff Kirsher 
1510aa43c215SJeff Kirsher 	netdev->vlan_features = netdev->hw_features;
1511aa43c215SJeff Kirsher 
1512aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1513aa43c215SJeff Kirsher 		netdev->hw_features |= NETIF_F_HW_VLAN_TX;
1514aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1515aa43c215SJeff Kirsher 		netdev->hw_features |= NETIF_F_LRO;
1516aa43c215SJeff Kirsher 
1517aa43c215SJeff Kirsher 	netdev->features |= netdev->hw_features |
1518aa43c215SJeff Kirsher 		NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1519aa43c215SJeff Kirsher 
1520aa43c215SJeff Kirsher 	netdev->irq = adapter->msix_entries[0].vector;
1521aa43c215SJeff Kirsher 
1522aa43c215SJeff Kirsher 	err = register_netdev(netdev);
1523aa43c215SJeff Kirsher 	if (err) {
1524aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to register net device\n");
1525aa43c215SJeff Kirsher 		return err;
1526aa43c215SJeff Kirsher 	}
1527aa43c215SJeff Kirsher 
1528aa43c215SJeff Kirsher 	return 0;
1529aa43c215SJeff Kirsher }
1530aa43c215SJeff Kirsher 
15315ad6ff9dSSony Chacko static int qlcnic_set_dma_mask(struct pci_dev *pdev, int *pci_using_dac)
1532aa43c215SJeff Kirsher {
1533aa43c215SJeff Kirsher 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1534aa43c215SJeff Kirsher 			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1535aa43c215SJeff Kirsher 		*pci_using_dac = 1;
1536aa43c215SJeff Kirsher 	else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1537aa43c215SJeff Kirsher 			!pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1538aa43c215SJeff Kirsher 		*pci_using_dac = 0;
1539aa43c215SJeff Kirsher 	else {
1540aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1541aa43c215SJeff Kirsher 		return -EIO;
1542aa43c215SJeff Kirsher 	}
1543aa43c215SJeff Kirsher 
1544aa43c215SJeff Kirsher 	return 0;
1545aa43c215SJeff Kirsher }
1546aa43c215SJeff Kirsher 
1547aa43c215SJeff Kirsher static int
1548aa43c215SJeff Kirsher qlcnic_alloc_msix_entries(struct qlcnic_adapter *adapter, u16 count)
1549aa43c215SJeff Kirsher {
1550aa43c215SJeff Kirsher 	adapter->msix_entries = kcalloc(count, sizeof(struct msix_entry),
1551aa43c215SJeff Kirsher 					GFP_KERNEL);
1552aa43c215SJeff Kirsher 
1553aa43c215SJeff Kirsher 	if (adapter->msix_entries)
1554aa43c215SJeff Kirsher 		return 0;
1555aa43c215SJeff Kirsher 
1556aa43c215SJeff Kirsher 	dev_err(&adapter->pdev->dev, "failed allocating msix_entries\n");
1557aa43c215SJeff Kirsher 	return -ENOMEM;
1558aa43c215SJeff Kirsher }
1559aa43c215SJeff Kirsher 
1560aa43c215SJeff Kirsher static int __devinit
1561aa43c215SJeff Kirsher qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1562aa43c215SJeff Kirsher {
1563aa43c215SJeff Kirsher 	struct net_device *netdev = NULL;
1564aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = NULL;
15655ad6ff9dSSony Chacko 	int err, pci_using_dac = -1;
1566aa43c215SJeff Kirsher 	uint8_t revision_id;
1567aa43c215SJeff Kirsher 	char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
1568aa43c215SJeff Kirsher 
1569aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
1570aa43c215SJeff Kirsher 	if (err)
1571aa43c215SJeff Kirsher 		return err;
1572aa43c215SJeff Kirsher 
1573aa43c215SJeff Kirsher 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1574aa43c215SJeff Kirsher 		err = -ENODEV;
1575aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1576aa43c215SJeff Kirsher 	}
1577aa43c215SJeff Kirsher 
1578aa43c215SJeff Kirsher 	err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1579aa43c215SJeff Kirsher 	if (err)
1580aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1581aa43c215SJeff Kirsher 
1582aa43c215SJeff Kirsher 	err = pci_request_regions(pdev, qlcnic_driver_name);
1583aa43c215SJeff Kirsher 	if (err)
1584aa43c215SJeff Kirsher 		goto err_out_disable_pdev;
1585aa43c215SJeff Kirsher 
1586aa43c215SJeff Kirsher 	pci_set_master(pdev);
1587aa43c215SJeff Kirsher 	pci_enable_pcie_error_reporting(pdev);
1588aa43c215SJeff Kirsher 
1589aa43c215SJeff Kirsher 	netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1590aa43c215SJeff Kirsher 	if (!netdev) {
1591aa43c215SJeff Kirsher 		err = -ENOMEM;
1592aa43c215SJeff Kirsher 		goto err_out_free_res;
1593aa43c215SJeff Kirsher 	}
1594aa43c215SJeff Kirsher 
1595aa43c215SJeff Kirsher 	SET_NETDEV_DEV(netdev, &pdev->dev);
1596aa43c215SJeff Kirsher 
1597aa43c215SJeff Kirsher 	adapter = netdev_priv(netdev);
1598aa43c215SJeff Kirsher 	adapter->netdev  = netdev;
1599aa43c215SJeff Kirsher 	adapter->pdev    = pdev;
1600aa43c215SJeff Kirsher 
16012dfc9671SPeter Senna Tschudin 	err = qlcnic_alloc_adapter_resources(adapter);
16022dfc9671SPeter Senna Tschudin 	if (err)
1603aa43c215SJeff Kirsher 		goto err_out_free_netdev;
1604aa43c215SJeff Kirsher 
1605aa43c215SJeff Kirsher 	adapter->dev_rst_time = jiffies;
1606aa43c215SJeff Kirsher 	revision_id = pdev->revision;
1607aa43c215SJeff Kirsher 	adapter->ahw->revision_id = revision_id;
1608aa43c215SJeff Kirsher 	adapter->mac_learn = qlcnic_mac_learn;
1609aa43c215SJeff Kirsher 
1610aa43c215SJeff Kirsher 	rwlock_init(&adapter->ahw->crb_lock);
1611aa43c215SJeff Kirsher 	mutex_init(&adapter->ahw->mem_lock);
1612aa43c215SJeff Kirsher 
1613aa43c215SJeff Kirsher 	spin_lock_init(&adapter->tx_clean_lock);
1614aa43c215SJeff Kirsher 	INIT_LIST_HEAD(&adapter->mac_list);
1615aa43c215SJeff Kirsher 
1616aa43c215SJeff Kirsher 	err = qlcnic_setup_pci_map(adapter);
1617aa43c215SJeff Kirsher 	if (err)
1618aa43c215SJeff Kirsher 		goto err_out_free_hw;
1619aa43c215SJeff Kirsher 
1620aa43c215SJeff Kirsher 	/* This will be reset for mezz cards  */
1621aa43c215SJeff Kirsher 	adapter->portnum = adapter->ahw->pci_func;
1622aa43c215SJeff Kirsher 
1623aa43c215SJeff Kirsher 	err = qlcnic_get_board_info(adapter);
1624aa43c215SJeff Kirsher 	if (err) {
1625aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "Error getting board config info.\n");
1626aa43c215SJeff Kirsher 		goto err_out_iounmap;
1627aa43c215SJeff Kirsher 	}
1628aa43c215SJeff Kirsher 
1629aa43c215SJeff Kirsher 	err = qlcnic_setup_idc_param(adapter);
1630aa43c215SJeff Kirsher 	if (err)
1631aa43c215SJeff Kirsher 		goto err_out_iounmap;
1632aa43c215SJeff Kirsher 
1633aa43c215SJeff Kirsher 	adapter->flags |= QLCNIC_NEED_FLR;
1634aa43c215SJeff Kirsher 
1635aa43c215SJeff Kirsher 	err = adapter->nic_ops->start_firmware(adapter);
1636aa43c215SJeff Kirsher 	if (err) {
1637b43e5ee7SSucheta Chakraborty 		dev_err(&pdev->dev, "Loading fw failed. Please Reboot\n"
1638b43e5ee7SSucheta Chakraborty 			"\t\tIf reboot doesn't help, try flashing the card\n");
1639b43e5ee7SSucheta Chakraborty 		goto err_out_maintenance_mode;
1640aa43c215SJeff Kirsher 	}
1641aa43c215SJeff Kirsher 
1642aa43c215SJeff Kirsher 	if (qlcnic_read_mac_addr(adapter))
1643aa43c215SJeff Kirsher 		dev_warn(&pdev->dev, "failed to read mac addr\n");
1644aa43c215SJeff Kirsher 
1645aa43c215SJeff Kirsher 	if (adapter->portnum == 0) {
1646aa43c215SJeff Kirsher 		get_brd_name(adapter, brd_name);
1647aa43c215SJeff Kirsher 
1648aa43c215SJeff Kirsher 		pr_info("%s: %s Board Chip rev 0x%x\n",
1649aa43c215SJeff Kirsher 				module_name(THIS_MODULE),
1650aa43c215SJeff Kirsher 				brd_name, adapter->ahw->revision_id);
1651aa43c215SJeff Kirsher 	}
1652aa43c215SJeff Kirsher 
1653aa43c215SJeff Kirsher 	qlcnic_clear_stats(adapter);
1654aa43c215SJeff Kirsher 
1655aa43c215SJeff Kirsher 	err = qlcnic_alloc_msix_entries(adapter, adapter->max_rx_ques);
1656aa43c215SJeff Kirsher 	if (err)
1657aa43c215SJeff Kirsher 		goto err_out_decr_ref;
1658aa43c215SJeff Kirsher 
1659aa43c215SJeff Kirsher 	qlcnic_setup_intr(adapter);
1660aa43c215SJeff Kirsher 
1661aa43c215SJeff Kirsher 	err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
1662aa43c215SJeff Kirsher 	if (err)
1663aa43c215SJeff Kirsher 		goto err_out_disable_msi;
1664aa43c215SJeff Kirsher 
1665aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, adapter);
1666aa43c215SJeff Kirsher 
1667aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1668aa43c215SJeff Kirsher 
1669aa43c215SJeff Kirsher 	switch (adapter->ahw->port_type) {
1670aa43c215SJeff Kirsher 	case QLCNIC_GBE:
1671aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1672aa43c215SJeff Kirsher 				adapter->netdev->name);
1673aa43c215SJeff Kirsher 		break;
1674aa43c215SJeff Kirsher 	case QLCNIC_XGBE:
1675aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1676aa43c215SJeff Kirsher 				adapter->netdev->name);
1677aa43c215SJeff Kirsher 		break;
1678aa43c215SJeff Kirsher 	}
1679aa43c215SJeff Kirsher 
1680aa43c215SJeff Kirsher 	if (adapter->mac_learn)
1681aa43c215SJeff Kirsher 		qlcnic_alloc_lb_filters_mem(adapter);
1682aa43c215SJeff Kirsher 
1683aa43c215SJeff Kirsher 	qlcnic_create_diag_entries(adapter);
1684aa43c215SJeff Kirsher 
1685aa43c215SJeff Kirsher 	return 0;
1686aa43c215SJeff Kirsher 
1687aa43c215SJeff Kirsher err_out_disable_msi:
1688aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
1689aa43c215SJeff Kirsher 	kfree(adapter->msix_entries);
1690aa43c215SJeff Kirsher 
1691aa43c215SJeff Kirsher err_out_decr_ref:
1692aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
1693aa43c215SJeff Kirsher 
1694aa43c215SJeff Kirsher err_out_iounmap:
1695aa43c215SJeff Kirsher 	qlcnic_cleanup_pci_map(adapter);
1696aa43c215SJeff Kirsher 
1697aa43c215SJeff Kirsher err_out_free_hw:
1698aa43c215SJeff Kirsher 	qlcnic_free_adapter_resources(adapter);
1699aa43c215SJeff Kirsher 
1700aa43c215SJeff Kirsher err_out_free_netdev:
1701aa43c215SJeff Kirsher 	free_netdev(netdev);
1702aa43c215SJeff Kirsher 
1703aa43c215SJeff Kirsher err_out_free_res:
1704aa43c215SJeff Kirsher 	pci_release_regions(pdev);
1705aa43c215SJeff Kirsher 
1706aa43c215SJeff Kirsher err_out_disable_pdev:
1707aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, NULL);
1708aa43c215SJeff Kirsher 	pci_disable_device(pdev);
1709aa43c215SJeff Kirsher 	return err;
1710b43e5ee7SSucheta Chakraborty 
1711b43e5ee7SSucheta Chakraborty err_out_maintenance_mode:
1712b43e5ee7SSucheta Chakraborty 	netdev->netdev_ops = &qlcnic_netdev_failed_ops;
1713b43e5ee7SSucheta Chakraborty 	SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_failed_ops);
1714b43e5ee7SSucheta Chakraborty 	err = register_netdev(netdev);
1715b43e5ee7SSucheta Chakraborty 	if (err) {
1716b43e5ee7SSucheta Chakraborty 		dev_err(&pdev->dev, "failed to register net device\n");
1717b43e5ee7SSucheta Chakraborty 		goto err_out_decr_ref;
1718b43e5ee7SSucheta Chakraborty 	}
1719b43e5ee7SSucheta Chakraborty 	pci_set_drvdata(pdev, adapter);
1720b43e5ee7SSucheta Chakraborty 	qlcnic_create_diag_entries(adapter);
1721b43e5ee7SSucheta Chakraborty 	return 0;
1722aa43c215SJeff Kirsher }
1723aa43c215SJeff Kirsher 
1724aa43c215SJeff Kirsher static void __devexit qlcnic_remove(struct pci_dev *pdev)
1725aa43c215SJeff Kirsher {
1726aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
1727aa43c215SJeff Kirsher 	struct net_device *netdev;
1728aa43c215SJeff Kirsher 
1729aa43c215SJeff Kirsher 	adapter = pci_get_drvdata(pdev);
1730aa43c215SJeff Kirsher 	if (adapter == NULL)
1731aa43c215SJeff Kirsher 		return;
1732aa43c215SJeff Kirsher 
1733aa43c215SJeff Kirsher 	netdev = adapter->netdev;
1734aa43c215SJeff Kirsher 
1735aa43c215SJeff Kirsher 	qlcnic_cancel_fw_work(adapter);
1736aa43c215SJeff Kirsher 
1737aa43c215SJeff Kirsher 	unregister_netdev(netdev);
1738aa43c215SJeff Kirsher 
1739aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1740aa43c215SJeff Kirsher 
1741aa43c215SJeff Kirsher 	if (adapter->npars != NULL)
1742aa43c215SJeff Kirsher 		kfree(adapter->npars);
1743aa43c215SJeff Kirsher 	if (adapter->eswitch != NULL)
1744aa43c215SJeff Kirsher 		kfree(adapter->eswitch);
1745aa43c215SJeff Kirsher 
1746aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
1747aa43c215SJeff Kirsher 
1748aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1749aa43c215SJeff Kirsher 
1750aa43c215SJeff Kirsher 	qlcnic_free_lb_filters_mem(adapter);
1751aa43c215SJeff Kirsher 
1752aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
1753aa43c215SJeff Kirsher 	kfree(adapter->msix_entries);
1754aa43c215SJeff Kirsher 
1755aa43c215SJeff Kirsher 	qlcnic_remove_diag_entries(adapter);
1756aa43c215SJeff Kirsher 
1757aa43c215SJeff Kirsher 	qlcnic_cleanup_pci_map(adapter);
1758aa43c215SJeff Kirsher 
1759aa43c215SJeff Kirsher 	qlcnic_release_firmware(adapter);
1760aa43c215SJeff Kirsher 
1761aa43c215SJeff Kirsher 	pci_disable_pcie_error_reporting(pdev);
1762aa43c215SJeff Kirsher 	pci_release_regions(pdev);
1763aa43c215SJeff Kirsher 	pci_disable_device(pdev);
1764aa43c215SJeff Kirsher 	pci_set_drvdata(pdev, NULL);
1765aa43c215SJeff Kirsher 
1766aa43c215SJeff Kirsher 	qlcnic_free_adapter_resources(adapter);
1767aa43c215SJeff Kirsher 	free_netdev(netdev);
1768aa43c215SJeff Kirsher }
1769aa43c215SJeff Kirsher static int __qlcnic_shutdown(struct pci_dev *pdev)
1770aa43c215SJeff Kirsher {
1771aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1772aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1773aa43c215SJeff Kirsher 	int retval;
1774aa43c215SJeff Kirsher 
1775aa43c215SJeff Kirsher 	netif_device_detach(netdev);
1776aa43c215SJeff Kirsher 
1777aa43c215SJeff Kirsher 	qlcnic_cancel_fw_work(adapter);
1778aa43c215SJeff Kirsher 
1779aa43c215SJeff Kirsher 	if (netif_running(netdev))
1780aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
1781aa43c215SJeff Kirsher 
1782aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
1783aa43c215SJeff Kirsher 
1784aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
1785aa43c215SJeff Kirsher 
1786aa43c215SJeff Kirsher 	retval = pci_save_state(pdev);
1787aa43c215SJeff Kirsher 	if (retval)
1788aa43c215SJeff Kirsher 		return retval;
1789aa43c215SJeff Kirsher 
1790aa43c215SJeff Kirsher 	if (qlcnic_wol_supported(adapter)) {
1791aa43c215SJeff Kirsher 		pci_enable_wake(pdev, PCI_D3cold, 1);
1792aa43c215SJeff Kirsher 		pci_enable_wake(pdev, PCI_D3hot, 1);
1793aa43c215SJeff Kirsher 	}
1794aa43c215SJeff Kirsher 
1795aa43c215SJeff Kirsher 	return 0;
1796aa43c215SJeff Kirsher }
1797aa43c215SJeff Kirsher 
1798aa43c215SJeff Kirsher static void qlcnic_shutdown(struct pci_dev *pdev)
1799aa43c215SJeff Kirsher {
1800aa43c215SJeff Kirsher 	if (__qlcnic_shutdown(pdev))
1801aa43c215SJeff Kirsher 		return;
1802aa43c215SJeff Kirsher 
1803aa43c215SJeff Kirsher 	pci_disable_device(pdev);
1804aa43c215SJeff Kirsher }
1805aa43c215SJeff Kirsher 
1806aa43c215SJeff Kirsher #ifdef CONFIG_PM
1807aa43c215SJeff Kirsher static int
1808aa43c215SJeff Kirsher qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1809aa43c215SJeff Kirsher {
1810aa43c215SJeff Kirsher 	int retval;
1811aa43c215SJeff Kirsher 
1812aa43c215SJeff Kirsher 	retval = __qlcnic_shutdown(pdev);
1813aa43c215SJeff Kirsher 	if (retval)
1814aa43c215SJeff Kirsher 		return retval;
1815aa43c215SJeff Kirsher 
1816aa43c215SJeff Kirsher 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1817aa43c215SJeff Kirsher 	return 0;
1818aa43c215SJeff Kirsher }
1819aa43c215SJeff Kirsher 
1820aa43c215SJeff Kirsher static int
1821aa43c215SJeff Kirsher qlcnic_resume(struct pci_dev *pdev)
1822aa43c215SJeff Kirsher {
1823aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1824aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
1825aa43c215SJeff Kirsher 	int err;
1826aa43c215SJeff Kirsher 
1827aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
1828aa43c215SJeff Kirsher 	if (err)
1829aa43c215SJeff Kirsher 		return err;
1830aa43c215SJeff Kirsher 
1831aa43c215SJeff Kirsher 	pci_set_power_state(pdev, PCI_D0);
1832aa43c215SJeff Kirsher 	pci_set_master(pdev);
1833aa43c215SJeff Kirsher 	pci_restore_state(pdev);
1834aa43c215SJeff Kirsher 
1835aa43c215SJeff Kirsher 	err = adapter->nic_ops->start_firmware(adapter);
1836aa43c215SJeff Kirsher 	if (err) {
1837aa43c215SJeff Kirsher 		dev_err(&pdev->dev, "failed to start firmware\n");
1838aa43c215SJeff Kirsher 		return err;
1839aa43c215SJeff Kirsher 	}
1840aa43c215SJeff Kirsher 
1841aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
1842aa43c215SJeff Kirsher 		err = qlcnic_up(adapter, netdev);
1843aa43c215SJeff Kirsher 		if (err)
1844aa43c215SJeff Kirsher 			goto done;
1845aa43c215SJeff Kirsher 
1846aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1847aa43c215SJeff Kirsher 	}
1848aa43c215SJeff Kirsher done:
1849aa43c215SJeff Kirsher 	netif_device_attach(netdev);
1850aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1851aa43c215SJeff Kirsher 	return 0;
1852aa43c215SJeff Kirsher }
1853aa43c215SJeff Kirsher #endif
1854aa43c215SJeff Kirsher 
1855aa43c215SJeff Kirsher static int qlcnic_open(struct net_device *netdev)
1856aa43c215SJeff Kirsher {
1857aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1858b43e5ee7SSucheta Chakraborty 	u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
1859aa43c215SJeff Kirsher 	int err;
1860aa43c215SJeff Kirsher 
1861b43e5ee7SSucheta Chakraborty 	if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
1862b43e5ee7SSucheta Chakraborty 		netdev_err(netdev, "Device in FAILED state\n");
1863b43e5ee7SSucheta Chakraborty 		return -EIO;
1864b43e5ee7SSucheta Chakraborty 	}
1865b43e5ee7SSucheta Chakraborty 
1866aa43c215SJeff Kirsher 	netif_carrier_off(netdev);
1867aa43c215SJeff Kirsher 
1868aa43c215SJeff Kirsher 	err = qlcnic_attach(adapter);
1869aa43c215SJeff Kirsher 	if (err)
1870aa43c215SJeff Kirsher 		return err;
1871aa43c215SJeff Kirsher 
1872aa43c215SJeff Kirsher 	err = __qlcnic_up(adapter, netdev);
1873aa43c215SJeff Kirsher 	if (err)
1874aa43c215SJeff Kirsher 		goto err_out;
1875aa43c215SJeff Kirsher 
1876aa43c215SJeff Kirsher 	netif_start_queue(netdev);
1877aa43c215SJeff Kirsher 
1878aa43c215SJeff Kirsher 	return 0;
1879aa43c215SJeff Kirsher 
1880aa43c215SJeff Kirsher err_out:
1881aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
1882aa43c215SJeff Kirsher 	return err;
1883aa43c215SJeff Kirsher }
1884aa43c215SJeff Kirsher 
1885aa43c215SJeff Kirsher /*
1886aa43c215SJeff Kirsher  * qlcnic_close - Disables a network interface entry point
1887aa43c215SJeff Kirsher  */
1888aa43c215SJeff Kirsher static int qlcnic_close(struct net_device *netdev)
1889aa43c215SJeff Kirsher {
1890aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1891aa43c215SJeff Kirsher 
1892aa43c215SJeff Kirsher 	__qlcnic_down(adapter, netdev);
1893aa43c215SJeff Kirsher 	return 0;
1894aa43c215SJeff Kirsher }
1895aa43c215SJeff Kirsher 
1896aa43c215SJeff Kirsher void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1897aa43c215SJeff Kirsher {
1898aa43c215SJeff Kirsher 	void *head;
1899aa43c215SJeff Kirsher 	int i;
1900aa43c215SJeff Kirsher 
1901aa43c215SJeff Kirsher 	if (adapter->fhash.fmax && adapter->fhash.fhead)
1902aa43c215SJeff Kirsher 		return;
1903aa43c215SJeff Kirsher 
1904aa43c215SJeff Kirsher 	spin_lock_init(&adapter->mac_learn_lock);
1905aa43c215SJeff Kirsher 
1906aa43c215SJeff Kirsher 	head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1907aa43c215SJeff Kirsher 								GFP_KERNEL);
1908aa43c215SJeff Kirsher 	if (!head)
1909aa43c215SJeff Kirsher 		return;
1910aa43c215SJeff Kirsher 
1911aa43c215SJeff Kirsher 	adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1912aa43c215SJeff Kirsher 	adapter->fhash.fhead = head;
1913aa43c215SJeff Kirsher 
1914aa43c215SJeff Kirsher 	for (i = 0; i < adapter->fhash.fmax; i++)
1915aa43c215SJeff Kirsher 		INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1916aa43c215SJeff Kirsher }
1917aa43c215SJeff Kirsher 
1918aa43c215SJeff Kirsher static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1919aa43c215SJeff Kirsher {
1920aa43c215SJeff Kirsher 	if (adapter->fhash.fmax && adapter->fhash.fhead)
1921aa43c215SJeff Kirsher 		kfree(adapter->fhash.fhead);
1922aa43c215SJeff Kirsher 
1923aa43c215SJeff Kirsher 	adapter->fhash.fhead = NULL;
1924aa43c215SJeff Kirsher 	adapter->fhash.fmax = 0;
1925aa43c215SJeff Kirsher }
1926aa43c215SJeff Kirsher 
1927aa43c215SJeff Kirsher static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
1928aa43c215SJeff Kirsher 		u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
1929aa43c215SJeff Kirsher {
1930aa43c215SJeff Kirsher 	struct cmd_desc_type0 *hwdesc;
1931aa43c215SJeff Kirsher 	struct qlcnic_nic_req *req;
1932aa43c215SJeff Kirsher 	struct qlcnic_mac_req *mac_req;
1933aa43c215SJeff Kirsher 	struct qlcnic_vlan_req *vlan_req;
1934aa43c215SJeff Kirsher 	u32 producer;
1935aa43c215SJeff Kirsher 	u64 word;
1936aa43c215SJeff Kirsher 
1937aa43c215SJeff Kirsher 	producer = tx_ring->producer;
1938aa43c215SJeff Kirsher 	hwdesc = &tx_ring->desc_head[tx_ring->producer];
1939aa43c215SJeff Kirsher 
1940aa43c215SJeff Kirsher 	req = (struct qlcnic_nic_req *)hwdesc;
1941aa43c215SJeff Kirsher 	memset(req, 0, sizeof(struct qlcnic_nic_req));
1942aa43c215SJeff Kirsher 	req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1943aa43c215SJeff Kirsher 
1944aa43c215SJeff Kirsher 	word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1945aa43c215SJeff Kirsher 	req->req_hdr = cpu_to_le64(word);
1946aa43c215SJeff Kirsher 
1947aa43c215SJeff Kirsher 	mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
1948aa43c215SJeff Kirsher 	mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
1949aa43c215SJeff Kirsher 	memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1950aa43c215SJeff Kirsher 
1951aa43c215SJeff Kirsher 	vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1952aa43c215SJeff Kirsher 	vlan_req->vlan_id = vlan_id;
1953aa43c215SJeff Kirsher 
1954aa43c215SJeff Kirsher 	tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
1955aa43c215SJeff Kirsher 	smp_mb();
1956aa43c215SJeff Kirsher }
1957aa43c215SJeff Kirsher 
1958aa43c215SJeff Kirsher #define QLCNIC_MAC_HASH(MAC)\
1959aa43c215SJeff Kirsher 	((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1960aa43c215SJeff Kirsher 
1961aa43c215SJeff Kirsher static void
1962aa43c215SJeff Kirsher qlcnic_send_filter(struct qlcnic_adapter *adapter,
1963aa43c215SJeff Kirsher 		struct qlcnic_host_tx_ring *tx_ring,
1964aa43c215SJeff Kirsher 		struct cmd_desc_type0 *first_desc,
1965aa43c215SJeff Kirsher 		struct sk_buff *skb)
1966aa43c215SJeff Kirsher {
1967aa43c215SJeff Kirsher 	struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1968aa43c215SJeff Kirsher 	struct qlcnic_filter *fil, *tmp_fil;
1969aa43c215SJeff Kirsher 	struct hlist_node *tmp_hnode, *n;
1970aa43c215SJeff Kirsher 	struct hlist_head *head;
1971aa43c215SJeff Kirsher 	u64 src_addr = 0;
1972aa43c215SJeff Kirsher 	__le16 vlan_id = 0;
1973aa43c215SJeff Kirsher 	u8 hindex;
1974aa43c215SJeff Kirsher 
19752e42e474SJoe Perches 	if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
1976aa43c215SJeff Kirsher 		return;
1977aa43c215SJeff Kirsher 
1978aa43c215SJeff Kirsher 	if (adapter->fhash.fnum >= adapter->fhash.fmax)
1979aa43c215SJeff Kirsher 		return;
1980aa43c215SJeff Kirsher 
1981aa43c215SJeff Kirsher 	/* Only NPAR capable devices support vlan based learning*/
1982aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1983aa43c215SJeff Kirsher 		vlan_id = first_desc->vlan_TCI;
1984aa43c215SJeff Kirsher 	memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1985aa43c215SJeff Kirsher 	hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1986aa43c215SJeff Kirsher 	head = &(adapter->fhash.fhead[hindex]);
1987aa43c215SJeff Kirsher 
1988aa43c215SJeff Kirsher 	hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
1989aa43c215SJeff Kirsher 		if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1990aa43c215SJeff Kirsher 			    tmp_fil->vlan_id == vlan_id) {
1991aa43c215SJeff Kirsher 
1992aa43c215SJeff Kirsher 			if (jiffies >
1993aa43c215SJeff Kirsher 			    (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1994aa43c215SJeff Kirsher 				qlcnic_change_filter(adapter, src_addr, vlan_id,
1995aa43c215SJeff Kirsher 								tx_ring);
1996aa43c215SJeff Kirsher 			tmp_fil->ftime = jiffies;
1997aa43c215SJeff Kirsher 			return;
1998aa43c215SJeff Kirsher 		}
1999aa43c215SJeff Kirsher 	}
2000aa43c215SJeff Kirsher 
2001aa43c215SJeff Kirsher 	fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
2002aa43c215SJeff Kirsher 	if (!fil)
2003aa43c215SJeff Kirsher 		return;
2004aa43c215SJeff Kirsher 
2005aa43c215SJeff Kirsher 	qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
2006aa43c215SJeff Kirsher 
2007aa43c215SJeff Kirsher 	fil->ftime = jiffies;
2008aa43c215SJeff Kirsher 	fil->vlan_id = vlan_id;
2009aa43c215SJeff Kirsher 	memcpy(fil->faddr, &src_addr, ETH_ALEN);
2010aa43c215SJeff Kirsher 	spin_lock(&adapter->mac_learn_lock);
2011aa43c215SJeff Kirsher 	hlist_add_head(&(fil->fnode), head);
2012aa43c215SJeff Kirsher 	adapter->fhash.fnum++;
2013aa43c215SJeff Kirsher 	spin_unlock(&adapter->mac_learn_lock);
2014aa43c215SJeff Kirsher }
2015aa43c215SJeff Kirsher 
2016aa43c215SJeff Kirsher static int
2017aa43c215SJeff Kirsher qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
2018aa43c215SJeff Kirsher 		struct cmd_desc_type0 *first_desc,
2019aa43c215SJeff Kirsher 		struct sk_buff *skb)
2020aa43c215SJeff Kirsher {
2021aa43c215SJeff Kirsher 	u8 opcode = 0, hdr_len = 0;
2022aa43c215SJeff Kirsher 	u16 flags = 0, vlan_tci = 0;
2023aa43c215SJeff Kirsher 	int copied, offset, copy_len;
2024aa43c215SJeff Kirsher 	struct cmd_desc_type0 *hwdesc;
2025aa43c215SJeff Kirsher 	struct vlan_ethhdr *vh;
2026aa43c215SJeff Kirsher 	struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2027aa43c215SJeff Kirsher 	u16 protocol = ntohs(skb->protocol);
2028aa43c215SJeff Kirsher 	u32 producer = tx_ring->producer;
2029aa43c215SJeff Kirsher 
2030aa43c215SJeff Kirsher 	if (protocol == ETH_P_8021Q) {
2031aa43c215SJeff Kirsher 		vh = (struct vlan_ethhdr *)skb->data;
2032aa43c215SJeff Kirsher 		flags = FLAGS_VLAN_TAGGED;
203363507592SShahed Shaikh 		vlan_tci = ntohs(vh->h_vlan_TCI);
20340bb79565SRajesh Borundia 		protocol = ntohs(vh->h_vlan_encapsulated_proto);
2035aa43c215SJeff Kirsher 	} else if (vlan_tx_tag_present(skb)) {
2036aa43c215SJeff Kirsher 		flags = FLAGS_VLAN_OOB;
2037aa43c215SJeff Kirsher 		vlan_tci = vlan_tx_tag_get(skb);
2038aa43c215SJeff Kirsher 	}
2039aa43c215SJeff Kirsher 	if (unlikely(adapter->pvid)) {
2040aa43c215SJeff Kirsher 		if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
2041aa43c215SJeff Kirsher 			return -EIO;
2042aa43c215SJeff Kirsher 		if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
2043aa43c215SJeff Kirsher 			goto set_flags;
2044aa43c215SJeff Kirsher 
2045aa43c215SJeff Kirsher 		flags = FLAGS_VLAN_OOB;
2046aa43c215SJeff Kirsher 		vlan_tci = adapter->pvid;
2047aa43c215SJeff Kirsher 	}
2048aa43c215SJeff Kirsher set_flags:
2049aa43c215SJeff Kirsher 	qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
2050aa43c215SJeff Kirsher 	qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2051aa43c215SJeff Kirsher 
2052aa43c215SJeff Kirsher 	if (*(skb->data) & BIT_0) {
2053aa43c215SJeff Kirsher 		flags |= BIT_0;
2054aa43c215SJeff Kirsher 		memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
2055aa43c215SJeff Kirsher 	}
2056aa43c215SJeff Kirsher 	opcode = TX_ETHER_PKT;
2057aa43c215SJeff Kirsher 	if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
2058aa43c215SJeff Kirsher 			skb_shinfo(skb)->gso_size > 0) {
2059aa43c215SJeff Kirsher 
2060aa43c215SJeff Kirsher 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2061aa43c215SJeff Kirsher 
2062aa43c215SJeff Kirsher 		first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2063aa43c215SJeff Kirsher 		first_desc->total_hdr_length = hdr_len;
2064aa43c215SJeff Kirsher 
2065aa43c215SJeff Kirsher 		opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
2066aa43c215SJeff Kirsher 
2067aa43c215SJeff Kirsher 		/* For LSO, we need to copy the MAC/IP/TCP headers into
2068aa43c215SJeff Kirsher 		* the descriptor ring */
2069aa43c215SJeff Kirsher 		copied = 0;
2070aa43c215SJeff Kirsher 		offset = 2;
2071aa43c215SJeff Kirsher 
2072aa43c215SJeff Kirsher 		if (flags & FLAGS_VLAN_OOB) {
2073aa43c215SJeff Kirsher 			first_desc->total_hdr_length += VLAN_HLEN;
2074aa43c215SJeff Kirsher 			first_desc->tcp_hdr_offset = VLAN_HLEN;
2075aa43c215SJeff Kirsher 			first_desc->ip_hdr_offset = VLAN_HLEN;
2076aa43c215SJeff Kirsher 			/* Only in case of TSO on vlan device */
2077aa43c215SJeff Kirsher 			flags |= FLAGS_VLAN_TAGGED;
2078aa43c215SJeff Kirsher 
2079aa43c215SJeff Kirsher 			/* Create a TSO vlan header template for firmware */
2080aa43c215SJeff Kirsher 
2081aa43c215SJeff Kirsher 			hwdesc = &tx_ring->desc_head[producer];
2082aa43c215SJeff Kirsher 			tx_ring->cmd_buf_arr[producer].skb = NULL;
2083aa43c215SJeff Kirsher 
2084aa43c215SJeff Kirsher 			copy_len = min((int)sizeof(struct cmd_desc_type0) -
2085aa43c215SJeff Kirsher 				offset, hdr_len + VLAN_HLEN);
2086aa43c215SJeff Kirsher 
2087aa43c215SJeff Kirsher 			vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
2088aa43c215SJeff Kirsher 			skb_copy_from_linear_data(skb, vh, 12);
2089aa43c215SJeff Kirsher 			vh->h_vlan_proto = htons(ETH_P_8021Q);
2090aa43c215SJeff Kirsher 			vh->h_vlan_TCI = htons(vlan_tci);
2091aa43c215SJeff Kirsher 
2092aa43c215SJeff Kirsher 			skb_copy_from_linear_data_offset(skb, 12,
2093aa43c215SJeff Kirsher 				(char *)vh + 16, copy_len - 16);
2094aa43c215SJeff Kirsher 
2095aa43c215SJeff Kirsher 			copied = copy_len - VLAN_HLEN;
2096aa43c215SJeff Kirsher 			offset = 0;
2097aa43c215SJeff Kirsher 
2098aa43c215SJeff Kirsher 			producer = get_next_index(producer, tx_ring->num_desc);
2099aa43c215SJeff Kirsher 		}
2100aa43c215SJeff Kirsher 
2101aa43c215SJeff Kirsher 		while (copied < hdr_len) {
2102aa43c215SJeff Kirsher 
2103aa43c215SJeff Kirsher 			copy_len = min((int)sizeof(struct cmd_desc_type0) -
2104aa43c215SJeff Kirsher 				offset, (hdr_len - copied));
2105aa43c215SJeff Kirsher 
2106aa43c215SJeff Kirsher 			hwdesc = &tx_ring->desc_head[producer];
2107aa43c215SJeff Kirsher 			tx_ring->cmd_buf_arr[producer].skb = NULL;
2108aa43c215SJeff Kirsher 
2109aa43c215SJeff Kirsher 			skb_copy_from_linear_data_offset(skb, copied,
2110aa43c215SJeff Kirsher 				 (char *) hwdesc + offset, copy_len);
2111aa43c215SJeff Kirsher 
2112aa43c215SJeff Kirsher 			copied += copy_len;
2113aa43c215SJeff Kirsher 			offset = 0;
2114aa43c215SJeff Kirsher 
2115aa43c215SJeff Kirsher 			producer = get_next_index(producer, tx_ring->num_desc);
2116aa43c215SJeff Kirsher 		}
2117aa43c215SJeff Kirsher 
2118aa43c215SJeff Kirsher 		tx_ring->producer = producer;
2119aa43c215SJeff Kirsher 		smp_mb();
2120aa43c215SJeff Kirsher 		adapter->stats.lso_frames++;
2121aa43c215SJeff Kirsher 
2122aa43c215SJeff Kirsher 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2123aa43c215SJeff Kirsher 		u8 l4proto;
2124aa43c215SJeff Kirsher 
2125aa43c215SJeff Kirsher 		if (protocol == ETH_P_IP) {
2126aa43c215SJeff Kirsher 			l4proto = ip_hdr(skb)->protocol;
2127aa43c215SJeff Kirsher 
2128aa43c215SJeff Kirsher 			if (l4proto == IPPROTO_TCP)
2129aa43c215SJeff Kirsher 				opcode = TX_TCP_PKT;
2130aa43c215SJeff Kirsher 			else if (l4proto == IPPROTO_UDP)
2131aa43c215SJeff Kirsher 				opcode = TX_UDP_PKT;
2132aa43c215SJeff Kirsher 		} else if (protocol == ETH_P_IPV6) {
2133aa43c215SJeff Kirsher 			l4proto = ipv6_hdr(skb)->nexthdr;
2134aa43c215SJeff Kirsher 
2135aa43c215SJeff Kirsher 			if (l4proto == IPPROTO_TCP)
2136aa43c215SJeff Kirsher 				opcode = TX_TCPV6_PKT;
2137aa43c215SJeff Kirsher 			else if (l4proto == IPPROTO_UDP)
2138aa43c215SJeff Kirsher 				opcode = TX_UDPV6_PKT;
2139aa43c215SJeff Kirsher 		}
2140aa43c215SJeff Kirsher 	}
2141aa43c215SJeff Kirsher 	first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2142aa43c215SJeff Kirsher 	first_desc->ip_hdr_offset += skb_network_offset(skb);
2143aa43c215SJeff Kirsher 	qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2144aa43c215SJeff Kirsher 
2145aa43c215SJeff Kirsher 	return 0;
2146aa43c215SJeff Kirsher }
2147aa43c215SJeff Kirsher 
2148aa43c215SJeff Kirsher static int
2149aa43c215SJeff Kirsher qlcnic_map_tx_skb(struct pci_dev *pdev,
2150aa43c215SJeff Kirsher 		struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2151aa43c215SJeff Kirsher {
2152aa43c215SJeff Kirsher 	struct qlcnic_skb_frag *nf;
2153aa43c215SJeff Kirsher 	struct skb_frag_struct *frag;
2154aa43c215SJeff Kirsher 	int i, nr_frags;
2155aa43c215SJeff Kirsher 	dma_addr_t map;
2156aa43c215SJeff Kirsher 
2157aa43c215SJeff Kirsher 	nr_frags = skb_shinfo(skb)->nr_frags;
2158aa43c215SJeff Kirsher 	nf = &pbuf->frag_array[0];
2159aa43c215SJeff Kirsher 
2160aa43c215SJeff Kirsher 	map = pci_map_single(pdev, skb->data,
2161aa43c215SJeff Kirsher 			skb_headlen(skb), PCI_DMA_TODEVICE);
2162aa43c215SJeff Kirsher 	if (pci_dma_mapping_error(pdev, map))
2163aa43c215SJeff Kirsher 		goto out_err;
2164aa43c215SJeff Kirsher 
2165aa43c215SJeff Kirsher 	nf->dma = map;
2166aa43c215SJeff Kirsher 	nf->length = skb_headlen(skb);
2167aa43c215SJeff Kirsher 
2168aa43c215SJeff Kirsher 	for (i = 0; i < nr_frags; i++) {
2169aa43c215SJeff Kirsher 		frag = &skb_shinfo(skb)->frags[i];
2170aa43c215SJeff Kirsher 		nf = &pbuf->frag_array[i+1];
2171aa43c215SJeff Kirsher 
21729e903e08SEric Dumazet 		map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
21735d6bcdfeSIan Campbell 				       DMA_TO_DEVICE);
21745d6bcdfeSIan Campbell 		if (dma_mapping_error(&pdev->dev, map))
2175aa43c215SJeff Kirsher 			goto unwind;
2176aa43c215SJeff Kirsher 
2177aa43c215SJeff Kirsher 		nf->dma = map;
21789e903e08SEric Dumazet 		nf->length = skb_frag_size(frag);
2179aa43c215SJeff Kirsher 	}
2180aa43c215SJeff Kirsher 
2181aa43c215SJeff Kirsher 	return 0;
2182aa43c215SJeff Kirsher 
2183aa43c215SJeff Kirsher unwind:
2184aa43c215SJeff Kirsher 	while (--i >= 0) {
2185aa43c215SJeff Kirsher 		nf = &pbuf->frag_array[i+1];
2186aa43c215SJeff Kirsher 		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2187aa43c215SJeff Kirsher 	}
2188aa43c215SJeff Kirsher 
2189aa43c215SJeff Kirsher 	nf = &pbuf->frag_array[0];
2190aa43c215SJeff Kirsher 	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2191aa43c215SJeff Kirsher 
2192aa43c215SJeff Kirsher out_err:
2193aa43c215SJeff Kirsher 	return -ENOMEM;
2194aa43c215SJeff Kirsher }
2195aa43c215SJeff Kirsher 
2196aa43c215SJeff Kirsher static void
2197aa43c215SJeff Kirsher qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2198aa43c215SJeff Kirsher 			struct qlcnic_cmd_buffer *pbuf)
2199aa43c215SJeff Kirsher {
2200aa43c215SJeff Kirsher 	struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2201aa43c215SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
2202aa43c215SJeff Kirsher 	int i;
2203aa43c215SJeff Kirsher 
2204aa43c215SJeff Kirsher 	for (i = 0; i < nr_frags; i++) {
2205aa43c215SJeff Kirsher 		nf = &pbuf->frag_array[i+1];
2206aa43c215SJeff Kirsher 		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2207aa43c215SJeff Kirsher 	}
2208aa43c215SJeff Kirsher 
2209aa43c215SJeff Kirsher 	nf = &pbuf->frag_array[0];
2210aa43c215SJeff Kirsher 	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2211aa43c215SJeff Kirsher 	pbuf->skb = NULL;
2212aa43c215SJeff Kirsher }
2213aa43c215SJeff Kirsher 
2214aa43c215SJeff Kirsher static inline void
2215aa43c215SJeff Kirsher qlcnic_clear_cmddesc(u64 *desc)
2216aa43c215SJeff Kirsher {
2217aa43c215SJeff Kirsher 	desc[0] = 0ULL;
2218aa43c215SJeff Kirsher 	desc[2] = 0ULL;
2219aa43c215SJeff Kirsher 	desc[7] = 0ULL;
2220aa43c215SJeff Kirsher }
2221aa43c215SJeff Kirsher 
2222aa43c215SJeff Kirsher netdev_tx_t
2223aa43c215SJeff Kirsher qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2224aa43c215SJeff Kirsher {
2225aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2226aa43c215SJeff Kirsher 	struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2227aa43c215SJeff Kirsher 	struct qlcnic_cmd_buffer *pbuf;
2228aa43c215SJeff Kirsher 	struct qlcnic_skb_frag *buffrag;
2229aa43c215SJeff Kirsher 	struct cmd_desc_type0 *hwdesc, *first_desc;
2230aa43c215SJeff Kirsher 	struct pci_dev *pdev;
2231aa43c215SJeff Kirsher 	struct ethhdr *phdr;
2232aa43c215SJeff Kirsher 	int delta = 0;
2233aa43c215SJeff Kirsher 	int i, k;
2234aa43c215SJeff Kirsher 
2235aa43c215SJeff Kirsher 	u32 producer;
2236aa43c215SJeff Kirsher 	int frag_count;
2237aa43c215SJeff Kirsher 	u32 num_txd = tx_ring->num_desc;
2238aa43c215SJeff Kirsher 
2239aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2240aa43c215SJeff Kirsher 		netif_stop_queue(netdev);
2241aa43c215SJeff Kirsher 		return NETDEV_TX_BUSY;
2242aa43c215SJeff Kirsher 	}
2243aa43c215SJeff Kirsher 
2244aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MACSPOOF) {
2245aa43c215SJeff Kirsher 		phdr = (struct ethhdr *)skb->data;
22462e42e474SJoe Perches 		if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
2247aa43c215SJeff Kirsher 			goto drop_packet;
2248aa43c215SJeff Kirsher 	}
2249aa43c215SJeff Kirsher 
2250aa43c215SJeff Kirsher 	frag_count = skb_shinfo(skb)->nr_frags + 1;
2251aa43c215SJeff Kirsher 	/* 14 frags supported for normal packet and
2252aa43c215SJeff Kirsher 	 * 32 frags supported for TSO packet
2253aa43c215SJeff Kirsher 	 */
2254aa43c215SJeff Kirsher 	if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2255aa43c215SJeff Kirsher 
2256aa43c215SJeff Kirsher 		for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
22579e903e08SEric Dumazet 			delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
2258aa43c215SJeff Kirsher 
2259aa43c215SJeff Kirsher 		if (!__pskb_pull_tail(skb, delta))
2260aa43c215SJeff Kirsher 			goto drop_packet;
2261aa43c215SJeff Kirsher 
2262aa43c215SJeff Kirsher 		frag_count = 1 + skb_shinfo(skb)->nr_frags;
2263aa43c215SJeff Kirsher 	}
2264aa43c215SJeff Kirsher 
2265aa43c215SJeff Kirsher 	if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
2266aa43c215SJeff Kirsher 		netif_stop_queue(netdev);
2267aa43c215SJeff Kirsher 		if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2268aa43c215SJeff Kirsher 			netif_start_queue(netdev);
2269aa43c215SJeff Kirsher 		else {
2270aa43c215SJeff Kirsher 			adapter->stats.xmit_off++;
2271aa43c215SJeff Kirsher 			return NETDEV_TX_BUSY;
2272aa43c215SJeff Kirsher 		}
2273aa43c215SJeff Kirsher 	}
2274aa43c215SJeff Kirsher 
2275aa43c215SJeff Kirsher 	producer = tx_ring->producer;
2276aa43c215SJeff Kirsher 	pbuf = &tx_ring->cmd_buf_arr[producer];
2277aa43c215SJeff Kirsher 
2278aa43c215SJeff Kirsher 	pdev = adapter->pdev;
2279aa43c215SJeff Kirsher 
2280aa43c215SJeff Kirsher 	first_desc = hwdesc = &tx_ring->desc_head[producer];
2281aa43c215SJeff Kirsher 	qlcnic_clear_cmddesc((u64 *)hwdesc);
2282aa43c215SJeff Kirsher 
2283aa43c215SJeff Kirsher 	if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2284aa43c215SJeff Kirsher 		adapter->stats.tx_dma_map_error++;
2285aa43c215SJeff Kirsher 		goto drop_packet;
2286aa43c215SJeff Kirsher 	}
2287aa43c215SJeff Kirsher 
2288aa43c215SJeff Kirsher 	pbuf->skb = skb;
2289aa43c215SJeff Kirsher 	pbuf->frag_count = frag_count;
2290aa43c215SJeff Kirsher 
2291aa43c215SJeff Kirsher 	qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2292aa43c215SJeff Kirsher 	qlcnic_set_tx_port(first_desc, adapter->portnum);
2293aa43c215SJeff Kirsher 
2294aa43c215SJeff Kirsher 	for (i = 0; i < frag_count; i++) {
2295aa43c215SJeff Kirsher 
2296aa43c215SJeff Kirsher 		k = i % 4;
2297aa43c215SJeff Kirsher 
2298aa43c215SJeff Kirsher 		if ((k == 0) && (i > 0)) {
2299aa43c215SJeff Kirsher 			/* move to next desc.*/
2300aa43c215SJeff Kirsher 			producer = get_next_index(producer, num_txd);
2301aa43c215SJeff Kirsher 			hwdesc = &tx_ring->desc_head[producer];
2302aa43c215SJeff Kirsher 			qlcnic_clear_cmddesc((u64 *)hwdesc);
2303aa43c215SJeff Kirsher 			tx_ring->cmd_buf_arr[producer].skb = NULL;
2304aa43c215SJeff Kirsher 		}
2305aa43c215SJeff Kirsher 
2306aa43c215SJeff Kirsher 		buffrag = &pbuf->frag_array[i];
2307aa43c215SJeff Kirsher 
2308aa43c215SJeff Kirsher 		hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2309aa43c215SJeff Kirsher 		switch (k) {
2310aa43c215SJeff Kirsher 		case 0:
2311aa43c215SJeff Kirsher 			hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2312aa43c215SJeff Kirsher 			break;
2313aa43c215SJeff Kirsher 		case 1:
2314aa43c215SJeff Kirsher 			hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2315aa43c215SJeff Kirsher 			break;
2316aa43c215SJeff Kirsher 		case 2:
2317aa43c215SJeff Kirsher 			hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2318aa43c215SJeff Kirsher 			break;
2319aa43c215SJeff Kirsher 		case 3:
2320aa43c215SJeff Kirsher 			hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2321aa43c215SJeff Kirsher 			break;
2322aa43c215SJeff Kirsher 		}
2323aa43c215SJeff Kirsher 	}
2324aa43c215SJeff Kirsher 
2325aa43c215SJeff Kirsher 	tx_ring->producer = get_next_index(producer, num_txd);
2326aa43c215SJeff Kirsher 	smp_mb();
2327aa43c215SJeff Kirsher 
2328aa43c215SJeff Kirsher 	if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2329aa43c215SJeff Kirsher 		goto unwind_buff;
2330aa43c215SJeff Kirsher 
2331aa43c215SJeff Kirsher 	if (adapter->mac_learn)
2332aa43c215SJeff Kirsher 		qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2333aa43c215SJeff Kirsher 
2334aa43c215SJeff Kirsher 	adapter->stats.txbytes += skb->len;
2335aa43c215SJeff Kirsher 	adapter->stats.xmitcalled++;
2336aa43c215SJeff Kirsher 
23375ad6ff9dSSony Chacko 	qlcnic_update_cmd_producer(tx_ring);
2338aa43c215SJeff Kirsher 
2339aa43c215SJeff Kirsher 	return NETDEV_TX_OK;
2340aa43c215SJeff Kirsher 
2341aa43c215SJeff Kirsher unwind_buff:
2342aa43c215SJeff Kirsher 	qlcnic_unmap_buffers(pdev, skb, pbuf);
2343aa43c215SJeff Kirsher drop_packet:
2344aa43c215SJeff Kirsher 	adapter->stats.txdropped++;
2345aa43c215SJeff Kirsher 	dev_kfree_skb_any(skb);
2346aa43c215SJeff Kirsher 	return NETDEV_TX_OK;
2347aa43c215SJeff Kirsher }
2348aa43c215SJeff Kirsher 
2349aa43c215SJeff Kirsher static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2350aa43c215SJeff Kirsher {
2351aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2352aa43c215SJeff Kirsher 	u32 temp, temp_state, temp_val;
2353aa43c215SJeff Kirsher 	int rv = 0;
2354aa43c215SJeff Kirsher 
2355aa43c215SJeff Kirsher 	temp = QLCRD32(adapter, CRB_TEMP_STATE);
2356aa43c215SJeff Kirsher 
2357aa43c215SJeff Kirsher 	temp_state = qlcnic_get_temp_state(temp);
2358aa43c215SJeff Kirsher 	temp_val = qlcnic_get_temp_val(temp);
2359aa43c215SJeff Kirsher 
2360aa43c215SJeff Kirsher 	if (temp_state == QLCNIC_TEMP_PANIC) {
2361aa43c215SJeff Kirsher 		dev_err(&netdev->dev,
2362aa43c215SJeff Kirsher 		       "Device temperature %d degrees C exceeds"
2363aa43c215SJeff Kirsher 		       " maximum allowed. Hardware has been shut down.\n",
2364aa43c215SJeff Kirsher 		       temp_val);
2365aa43c215SJeff Kirsher 		rv = 1;
2366aa43c215SJeff Kirsher 	} else if (temp_state == QLCNIC_TEMP_WARN) {
2367aa43c215SJeff Kirsher 		if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2368aa43c215SJeff Kirsher 			dev_err(&netdev->dev,
2369aa43c215SJeff Kirsher 			       "Device temperature %d degrees C "
2370aa43c215SJeff Kirsher 			       "exceeds operating range."
2371aa43c215SJeff Kirsher 			       " Immediate action needed.\n",
2372aa43c215SJeff Kirsher 			       temp_val);
2373aa43c215SJeff Kirsher 		}
2374aa43c215SJeff Kirsher 	} else {
2375aa43c215SJeff Kirsher 		if (adapter->temp == QLCNIC_TEMP_WARN) {
2376aa43c215SJeff Kirsher 			dev_info(&netdev->dev,
2377aa43c215SJeff Kirsher 			       "Device temperature is now %d degrees C"
2378aa43c215SJeff Kirsher 			       " in normal range.\n", temp_val);
2379aa43c215SJeff Kirsher 		}
2380aa43c215SJeff Kirsher 	}
2381aa43c215SJeff Kirsher 	adapter->temp = temp_state;
2382aa43c215SJeff Kirsher 	return rv;
2383aa43c215SJeff Kirsher }
2384aa43c215SJeff Kirsher 
2385aa43c215SJeff Kirsher void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2386aa43c215SJeff Kirsher {
2387aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2388aa43c215SJeff Kirsher 
2389aa43c215SJeff Kirsher 	if (adapter->ahw->linkup && !linkup) {
2390aa43c215SJeff Kirsher 		netdev_info(netdev, "NIC Link is down\n");
2391aa43c215SJeff Kirsher 		adapter->ahw->linkup = 0;
2392aa43c215SJeff Kirsher 		if (netif_running(netdev)) {
2393aa43c215SJeff Kirsher 			netif_carrier_off(netdev);
2394aa43c215SJeff Kirsher 			netif_stop_queue(netdev);
2395aa43c215SJeff Kirsher 		}
2396aa43c215SJeff Kirsher 	} else if (!adapter->ahw->linkup && linkup) {
2397aa43c215SJeff Kirsher 		netdev_info(netdev, "NIC Link is up\n");
2398aa43c215SJeff Kirsher 		adapter->ahw->linkup = 1;
2399aa43c215SJeff Kirsher 		if (netif_running(netdev)) {
2400aa43c215SJeff Kirsher 			netif_carrier_on(netdev);
2401aa43c215SJeff Kirsher 			netif_wake_queue(netdev);
2402aa43c215SJeff Kirsher 		}
2403aa43c215SJeff Kirsher 	}
2404aa43c215SJeff Kirsher }
2405aa43c215SJeff Kirsher 
2406aa43c215SJeff Kirsher static void qlcnic_tx_timeout(struct net_device *netdev)
2407aa43c215SJeff Kirsher {
2408aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2409aa43c215SJeff Kirsher 
2410aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2411aa43c215SJeff Kirsher 		return;
2412aa43c215SJeff Kirsher 
2413aa43c215SJeff Kirsher 	dev_err(&netdev->dev, "transmit timeout, resetting.\n");
2414aa43c215SJeff Kirsher 
2415aa43c215SJeff Kirsher 	if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
2416aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
2417aa43c215SJeff Kirsher 	else
2418aa43c215SJeff Kirsher 		adapter->reset_context = 1;
2419aa43c215SJeff Kirsher }
2420aa43c215SJeff Kirsher 
2421aa43c215SJeff Kirsher static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2422aa43c215SJeff Kirsher {
2423aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2424aa43c215SJeff Kirsher 	struct net_device_stats *stats = &netdev->stats;
2425aa43c215SJeff Kirsher 
2426aa43c215SJeff Kirsher 	stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2427aa43c215SJeff Kirsher 	stats->tx_packets = adapter->stats.xmitfinished;
2428aa43c215SJeff Kirsher 	stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
2429aa43c215SJeff Kirsher 	stats->tx_bytes = adapter->stats.txbytes;
2430aa43c215SJeff Kirsher 	stats->rx_dropped = adapter->stats.rxdropped;
2431aa43c215SJeff Kirsher 	stats->tx_dropped = adapter->stats.txdropped;
2432aa43c215SJeff Kirsher 
2433aa43c215SJeff Kirsher 	return stats;
2434aa43c215SJeff Kirsher }
2435aa43c215SJeff Kirsher 
2436aa43c215SJeff Kirsher static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
2437aa43c215SJeff Kirsher {
2438aa43c215SJeff Kirsher 	u32 status;
2439aa43c215SJeff Kirsher 
2440aa43c215SJeff Kirsher 	status = readl(adapter->isr_int_vec);
2441aa43c215SJeff Kirsher 
2442aa43c215SJeff Kirsher 	if (!(status & adapter->int_vec_bit))
2443aa43c215SJeff Kirsher 		return IRQ_NONE;
2444aa43c215SJeff Kirsher 
2445aa43c215SJeff Kirsher 	/* check interrupt state machine, to be sure */
2446aa43c215SJeff Kirsher 	status = readl(adapter->crb_int_state_reg);
2447aa43c215SJeff Kirsher 	if (!ISR_LEGACY_INT_TRIGGERED(status))
2448aa43c215SJeff Kirsher 		return IRQ_NONE;
2449aa43c215SJeff Kirsher 
2450aa43c215SJeff Kirsher 	writel(0xffffffff, adapter->tgt_status_reg);
2451aa43c215SJeff Kirsher 	/* read twice to ensure write is flushed */
2452aa43c215SJeff Kirsher 	readl(adapter->isr_int_vec);
2453aa43c215SJeff Kirsher 	readl(adapter->isr_int_vec);
2454aa43c215SJeff Kirsher 
2455aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2456aa43c215SJeff Kirsher }
2457aa43c215SJeff Kirsher 
2458aa43c215SJeff Kirsher static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2459aa43c215SJeff Kirsher {
2460aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2461aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2462aa43c215SJeff Kirsher 
2463aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_MSIX_ENABLED)
2464aa43c215SJeff Kirsher 		goto done;
2465aa43c215SJeff Kirsher 	else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2466aa43c215SJeff Kirsher 		writel(0xffffffff, adapter->tgt_status_reg);
2467aa43c215SJeff Kirsher 		goto done;
2468aa43c215SJeff Kirsher 	}
2469aa43c215SJeff Kirsher 
2470aa43c215SJeff Kirsher 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2471aa43c215SJeff Kirsher 		return IRQ_NONE;
2472aa43c215SJeff Kirsher 
2473aa43c215SJeff Kirsher done:
2474aa43c215SJeff Kirsher 	adapter->diag_cnt++;
2475aa43c215SJeff Kirsher 	qlcnic_enable_int(sds_ring);
2476aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2477aa43c215SJeff Kirsher }
2478aa43c215SJeff Kirsher 
2479aa43c215SJeff Kirsher static irqreturn_t qlcnic_intr(int irq, void *data)
2480aa43c215SJeff Kirsher {
2481aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2482aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2483aa43c215SJeff Kirsher 
2484aa43c215SJeff Kirsher 	if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2485aa43c215SJeff Kirsher 		return IRQ_NONE;
2486aa43c215SJeff Kirsher 
2487aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2488aa43c215SJeff Kirsher 
2489aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2490aa43c215SJeff Kirsher }
2491aa43c215SJeff Kirsher 
2492aa43c215SJeff Kirsher static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2493aa43c215SJeff Kirsher {
2494aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2495aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2496aa43c215SJeff Kirsher 
2497aa43c215SJeff Kirsher 	/* clear interrupt */
2498aa43c215SJeff Kirsher 	writel(0xffffffff, adapter->tgt_status_reg);
2499aa43c215SJeff Kirsher 
2500aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2501aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2502aa43c215SJeff Kirsher }
2503aa43c215SJeff Kirsher 
2504aa43c215SJeff Kirsher static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2505aa43c215SJeff Kirsher {
2506aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring = data;
2507aa43c215SJeff Kirsher 
2508aa43c215SJeff Kirsher 	napi_schedule(&sds_ring->napi);
2509aa43c215SJeff Kirsher 	return IRQ_HANDLED;
2510aa43c215SJeff Kirsher }
2511aa43c215SJeff Kirsher 
2512aa43c215SJeff Kirsher static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2513aa43c215SJeff Kirsher {
2514aa43c215SJeff Kirsher 	u32 sw_consumer, hw_consumer;
2515aa43c215SJeff Kirsher 	int count = 0, i;
2516aa43c215SJeff Kirsher 	struct qlcnic_cmd_buffer *buffer;
2517aa43c215SJeff Kirsher 	struct pci_dev *pdev = adapter->pdev;
2518aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2519aa43c215SJeff Kirsher 	struct qlcnic_skb_frag *frag;
2520aa43c215SJeff Kirsher 	int done;
2521aa43c215SJeff Kirsher 	struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2522aa43c215SJeff Kirsher 
2523aa43c215SJeff Kirsher 	if (!spin_trylock(&adapter->tx_clean_lock))
2524aa43c215SJeff Kirsher 		return 1;
2525aa43c215SJeff Kirsher 
2526aa43c215SJeff Kirsher 	sw_consumer = tx_ring->sw_consumer;
2527aa43c215SJeff Kirsher 	hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2528aa43c215SJeff Kirsher 
2529aa43c215SJeff Kirsher 	while (sw_consumer != hw_consumer) {
2530aa43c215SJeff Kirsher 		buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2531aa43c215SJeff Kirsher 		if (buffer->skb) {
2532aa43c215SJeff Kirsher 			frag = &buffer->frag_array[0];
2533aa43c215SJeff Kirsher 			pci_unmap_single(pdev, frag->dma, frag->length,
2534aa43c215SJeff Kirsher 					 PCI_DMA_TODEVICE);
2535aa43c215SJeff Kirsher 			frag->dma = 0ULL;
2536aa43c215SJeff Kirsher 			for (i = 1; i < buffer->frag_count; i++) {
2537aa43c215SJeff Kirsher 				frag++;
2538aa43c215SJeff Kirsher 				pci_unmap_page(pdev, frag->dma, frag->length,
2539aa43c215SJeff Kirsher 					       PCI_DMA_TODEVICE);
2540aa43c215SJeff Kirsher 				frag->dma = 0ULL;
2541aa43c215SJeff Kirsher 			}
2542aa43c215SJeff Kirsher 
2543aa43c215SJeff Kirsher 			adapter->stats.xmitfinished++;
2544aa43c215SJeff Kirsher 			dev_kfree_skb_any(buffer->skb);
2545aa43c215SJeff Kirsher 			buffer->skb = NULL;
2546aa43c215SJeff Kirsher 		}
2547aa43c215SJeff Kirsher 
2548aa43c215SJeff Kirsher 		sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2549aa43c215SJeff Kirsher 		if (++count >= MAX_STATUS_HANDLE)
2550aa43c215SJeff Kirsher 			break;
2551aa43c215SJeff Kirsher 	}
2552aa43c215SJeff Kirsher 
2553aa43c215SJeff Kirsher 	if (count && netif_running(netdev)) {
2554aa43c215SJeff Kirsher 		tx_ring->sw_consumer = sw_consumer;
2555aa43c215SJeff Kirsher 
2556aa43c215SJeff Kirsher 		smp_mb();
2557aa43c215SJeff Kirsher 
2558aa43c215SJeff Kirsher 		if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2559aa43c215SJeff Kirsher 			if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2560aa43c215SJeff Kirsher 				netif_wake_queue(netdev);
2561aa43c215SJeff Kirsher 				adapter->stats.xmit_on++;
2562aa43c215SJeff Kirsher 			}
2563aa43c215SJeff Kirsher 		}
2564aa43c215SJeff Kirsher 		adapter->tx_timeo_cnt = 0;
2565aa43c215SJeff Kirsher 	}
2566aa43c215SJeff Kirsher 	/*
2567aa43c215SJeff Kirsher 	 * If everything is freed up to consumer then check if the ring is full
2568aa43c215SJeff Kirsher 	 * If the ring is full then check if more needs to be freed and
2569aa43c215SJeff Kirsher 	 * schedule the call back again.
2570aa43c215SJeff Kirsher 	 *
2571aa43c215SJeff Kirsher 	 * This happens when there are 2 CPUs. One could be freeing and the
2572aa43c215SJeff Kirsher 	 * other filling it. If the ring is full when we get out of here and
2573aa43c215SJeff Kirsher 	 * the card has already interrupted the host then the host can miss the
2574aa43c215SJeff Kirsher 	 * interrupt.
2575aa43c215SJeff Kirsher 	 *
2576aa43c215SJeff Kirsher 	 * There is still a possible race condition and the host could miss an
2577aa43c215SJeff Kirsher 	 * interrupt. The card has to take care of this.
2578aa43c215SJeff Kirsher 	 */
2579aa43c215SJeff Kirsher 	hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2580aa43c215SJeff Kirsher 	done = (sw_consumer == hw_consumer);
2581aa43c215SJeff Kirsher 	spin_unlock(&adapter->tx_clean_lock);
2582aa43c215SJeff Kirsher 
2583aa43c215SJeff Kirsher 	return done;
2584aa43c215SJeff Kirsher }
2585aa43c215SJeff Kirsher 
2586aa43c215SJeff Kirsher static int qlcnic_poll(struct napi_struct *napi, int budget)
2587aa43c215SJeff Kirsher {
2588aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring =
2589aa43c215SJeff Kirsher 		container_of(napi, struct qlcnic_host_sds_ring, napi);
2590aa43c215SJeff Kirsher 
2591aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2592aa43c215SJeff Kirsher 
2593aa43c215SJeff Kirsher 	int tx_complete;
2594aa43c215SJeff Kirsher 	int work_done;
2595aa43c215SJeff Kirsher 
2596aa43c215SJeff Kirsher 	tx_complete = qlcnic_process_cmd_ring(adapter);
2597aa43c215SJeff Kirsher 
2598aa43c215SJeff Kirsher 	work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2599aa43c215SJeff Kirsher 
2600aa43c215SJeff Kirsher 	if ((work_done < budget) && tx_complete) {
2601aa43c215SJeff Kirsher 		napi_complete(&sds_ring->napi);
2602aa43c215SJeff Kirsher 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2603aa43c215SJeff Kirsher 			qlcnic_enable_int(sds_ring);
2604aa43c215SJeff Kirsher 	}
2605aa43c215SJeff Kirsher 
2606aa43c215SJeff Kirsher 	return work_done;
2607aa43c215SJeff Kirsher }
2608aa43c215SJeff Kirsher 
2609aa43c215SJeff Kirsher static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2610aa43c215SJeff Kirsher {
2611aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring =
2612aa43c215SJeff Kirsher 		container_of(napi, struct qlcnic_host_sds_ring, napi);
2613aa43c215SJeff Kirsher 
2614aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2615aa43c215SJeff Kirsher 	int work_done;
2616aa43c215SJeff Kirsher 
2617aa43c215SJeff Kirsher 	work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2618aa43c215SJeff Kirsher 
2619aa43c215SJeff Kirsher 	if (work_done < budget) {
2620aa43c215SJeff Kirsher 		napi_complete(&sds_ring->napi);
2621aa43c215SJeff Kirsher 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2622aa43c215SJeff Kirsher 			qlcnic_enable_int(sds_ring);
2623aa43c215SJeff Kirsher 	}
2624aa43c215SJeff Kirsher 
2625aa43c215SJeff Kirsher 	return work_done;
2626aa43c215SJeff Kirsher }
2627aa43c215SJeff Kirsher 
2628aa43c215SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2629aa43c215SJeff Kirsher static void qlcnic_poll_controller(struct net_device *netdev)
2630aa43c215SJeff Kirsher {
2631aa43c215SJeff Kirsher 	int ring;
2632aa43c215SJeff Kirsher 	struct qlcnic_host_sds_ring *sds_ring;
2633aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2634aa43c215SJeff Kirsher 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2635aa43c215SJeff Kirsher 
2636aa43c215SJeff Kirsher 	disable_irq(adapter->irq);
2637aa43c215SJeff Kirsher 	for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2638aa43c215SJeff Kirsher 		sds_ring = &recv_ctx->sds_rings[ring];
2639aa43c215SJeff Kirsher 		qlcnic_intr(adapter->irq, sds_ring);
2640aa43c215SJeff Kirsher 	}
2641aa43c215SJeff Kirsher 	enable_irq(adapter->irq);
2642aa43c215SJeff Kirsher }
2643aa43c215SJeff Kirsher #endif
2644aa43c215SJeff Kirsher 
2645aa43c215SJeff Kirsher static void
2646aa43c215SJeff Kirsher qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2647aa43c215SJeff Kirsher {
2648aa43c215SJeff Kirsher 	u32 val;
2649aa43c215SJeff Kirsher 
2650aa43c215SJeff Kirsher 	val = adapter->portnum & 0xf;
2651aa43c215SJeff Kirsher 	val |= encoding << 7;
2652aa43c215SJeff Kirsher 	val |= (jiffies - adapter->dev_rst_time) << 8;
2653aa43c215SJeff Kirsher 
2654aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2655aa43c215SJeff Kirsher 	adapter->dev_rst_time = jiffies;
2656aa43c215SJeff Kirsher }
2657aa43c215SJeff Kirsher 
2658aa43c215SJeff Kirsher static int
2659aa43c215SJeff Kirsher qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
2660aa43c215SJeff Kirsher {
2661aa43c215SJeff Kirsher 	u32  val;
2662aa43c215SJeff Kirsher 
2663aa43c215SJeff Kirsher 	WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2664aa43c215SJeff Kirsher 			state != QLCNIC_DEV_NEED_QUISCENT);
2665aa43c215SJeff Kirsher 
2666aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2667aa43c215SJeff Kirsher 		return -EIO;
2668aa43c215SJeff Kirsher 
2669aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2670aa43c215SJeff Kirsher 
2671aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NEED_RESET)
2672aa43c215SJeff Kirsher 		QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2673aa43c215SJeff Kirsher 	else if (state == QLCNIC_DEV_NEED_QUISCENT)
2674aa43c215SJeff Kirsher 		QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
2675aa43c215SJeff Kirsher 
2676aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2677aa43c215SJeff Kirsher 
2678aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2679aa43c215SJeff Kirsher 
2680aa43c215SJeff Kirsher 	return 0;
2681aa43c215SJeff Kirsher }
2682aa43c215SJeff Kirsher 
2683aa43c215SJeff Kirsher static int
2684aa43c215SJeff Kirsher qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2685aa43c215SJeff Kirsher {
2686aa43c215SJeff Kirsher 	u32  val;
2687aa43c215SJeff Kirsher 
2688aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2689aa43c215SJeff Kirsher 		return -EBUSY;
2690aa43c215SJeff Kirsher 
2691aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2692aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2693aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2694aa43c215SJeff Kirsher 
2695aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2696aa43c215SJeff Kirsher 
2697aa43c215SJeff Kirsher 	return 0;
2698aa43c215SJeff Kirsher }
2699aa43c215SJeff Kirsher 
2700aa43c215SJeff Kirsher static void
2701aa43c215SJeff Kirsher qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
2702aa43c215SJeff Kirsher {
2703aa43c215SJeff Kirsher 	u32  val;
2704aa43c215SJeff Kirsher 
2705aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2706aa43c215SJeff Kirsher 		goto err;
2707aa43c215SJeff Kirsher 
2708aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2709aa43c215SJeff Kirsher 	QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
2710aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2711aa43c215SJeff Kirsher 
2712aa43c215SJeff Kirsher 	if (failed) {
2713aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2714aa43c215SJeff Kirsher 		dev_info(&adapter->pdev->dev,
2715aa43c215SJeff Kirsher 				"Device state set to Failed. Please Reboot\n");
2716aa43c215SJeff Kirsher 	} else if (!(val & 0x11111111))
2717aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2718aa43c215SJeff Kirsher 
2719aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2720aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
2721aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2722aa43c215SJeff Kirsher 
2723aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2724aa43c215SJeff Kirsher err:
2725aa43c215SJeff Kirsher 	adapter->fw_fail_cnt = 0;
2726aa43c215SJeff Kirsher 	adapter->flags &= ~QLCNIC_FW_HANG;
2727aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_START_FW, &adapter->state);
2728aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
2729aa43c215SJeff Kirsher }
2730aa43c215SJeff Kirsher 
2731aa43c215SJeff Kirsher /* Grab api lock, before checking state */
2732aa43c215SJeff Kirsher static int
2733aa43c215SJeff Kirsher qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2734aa43c215SJeff Kirsher {
2735aa43c215SJeff Kirsher 	int act, state, active_mask;
2736aa43c215SJeff Kirsher 
2737aa43c215SJeff Kirsher 	state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2738aa43c215SJeff Kirsher 	act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2739aa43c215SJeff Kirsher 
2740aa43c215SJeff Kirsher 	if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
2741aa43c215SJeff Kirsher 		active_mask = (~(1 << (adapter->ahw->pci_func * 4)));
2742aa43c215SJeff Kirsher 		act = act & active_mask;
2743aa43c215SJeff Kirsher 	}
2744aa43c215SJeff Kirsher 
2745aa43c215SJeff Kirsher 	if (((state & 0x11111111) == (act & 0x11111111)) ||
2746aa43c215SJeff Kirsher 			((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2747aa43c215SJeff Kirsher 		return 0;
2748aa43c215SJeff Kirsher 	else
2749aa43c215SJeff Kirsher 		return 1;
2750aa43c215SJeff Kirsher }
2751aa43c215SJeff Kirsher 
2752aa43c215SJeff Kirsher static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2753aa43c215SJeff Kirsher {
2754aa43c215SJeff Kirsher 	u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2755aa43c215SJeff Kirsher 
2756aa43c215SJeff Kirsher 	if (val != QLCNIC_DRV_IDC_VER) {
2757aa43c215SJeff Kirsher 		dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2758aa43c215SJeff Kirsher 			" idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2759aa43c215SJeff Kirsher 	}
2760aa43c215SJeff Kirsher 
2761aa43c215SJeff Kirsher 	return 0;
2762aa43c215SJeff Kirsher }
2763aa43c215SJeff Kirsher 
2764aa43c215SJeff Kirsher static int
2765aa43c215SJeff Kirsher qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2766aa43c215SJeff Kirsher {
2767aa43c215SJeff Kirsher 	u32 val, prev_state;
2768aa43c215SJeff Kirsher 	u8 dev_init_timeo = adapter->dev_init_timeo;
2769aa43c215SJeff Kirsher 	u8 portnum = adapter->portnum;
2770aa43c215SJeff Kirsher 	u8 ret;
2771aa43c215SJeff Kirsher 
2772aa43c215SJeff Kirsher 	if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2773aa43c215SJeff Kirsher 		return 1;
2774aa43c215SJeff Kirsher 
2775aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2776aa43c215SJeff Kirsher 		return -1;
2777aa43c215SJeff Kirsher 
2778aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
2779aa43c215SJeff Kirsher 	if (!(val & (1 << (portnum * 4)))) {
2780aa43c215SJeff Kirsher 		QLC_DEV_SET_REF_CNT(val, portnum);
2781aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
2782aa43c215SJeff Kirsher 	}
2783aa43c215SJeff Kirsher 
2784aa43c215SJeff Kirsher 	prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2785aa43c215SJeff Kirsher 	QLCDB(adapter, HW, "Device state = %u\n", prev_state);
2786aa43c215SJeff Kirsher 
2787aa43c215SJeff Kirsher 	switch (prev_state) {
2788aa43c215SJeff Kirsher 	case QLCNIC_DEV_COLD:
2789aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2790aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
2791aa43c215SJeff Kirsher 		qlcnic_idc_debug_info(adapter, 0);
2792aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2793aa43c215SJeff Kirsher 		return 1;
2794aa43c215SJeff Kirsher 
2795aa43c215SJeff Kirsher 	case QLCNIC_DEV_READY:
2796aa43c215SJeff Kirsher 		ret = qlcnic_check_idc_ver(adapter);
2797aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2798aa43c215SJeff Kirsher 		return ret;
2799aa43c215SJeff Kirsher 
2800aa43c215SJeff Kirsher 	case QLCNIC_DEV_NEED_RESET:
2801aa43c215SJeff Kirsher 		val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2802aa43c215SJeff Kirsher 		QLC_DEV_SET_RST_RDY(val, portnum);
2803aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2804aa43c215SJeff Kirsher 		break;
2805aa43c215SJeff Kirsher 
2806aa43c215SJeff Kirsher 	case QLCNIC_DEV_NEED_QUISCENT:
2807aa43c215SJeff Kirsher 		val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2808aa43c215SJeff Kirsher 		QLC_DEV_SET_QSCNT_RDY(val, portnum);
2809aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2810aa43c215SJeff Kirsher 		break;
2811aa43c215SJeff Kirsher 
2812aa43c215SJeff Kirsher 	case QLCNIC_DEV_FAILED:
2813aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev, "Device in failed state.\n");
2814aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2815aa43c215SJeff Kirsher 		return -1;
2816aa43c215SJeff Kirsher 
2817aa43c215SJeff Kirsher 	case QLCNIC_DEV_INITIALIZING:
2818aa43c215SJeff Kirsher 	case QLCNIC_DEV_QUISCENT:
2819aa43c215SJeff Kirsher 		break;
2820aa43c215SJeff Kirsher 	}
2821aa43c215SJeff Kirsher 
2822aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2823aa43c215SJeff Kirsher 
2824aa43c215SJeff Kirsher 	do {
2825aa43c215SJeff Kirsher 		msleep(1000);
2826aa43c215SJeff Kirsher 		prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2827aa43c215SJeff Kirsher 
2828aa43c215SJeff Kirsher 		if (prev_state == QLCNIC_DEV_QUISCENT)
2829aa43c215SJeff Kirsher 			continue;
2830aa43c215SJeff Kirsher 	} while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
2831aa43c215SJeff Kirsher 
2832aa43c215SJeff Kirsher 	if (!dev_init_timeo) {
2833aa43c215SJeff Kirsher 		dev_err(&adapter->pdev->dev,
2834aa43c215SJeff Kirsher 			"Waiting for device to initialize timeout\n");
2835aa43c215SJeff Kirsher 		return -1;
2836aa43c215SJeff Kirsher 	}
2837aa43c215SJeff Kirsher 
2838aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2839aa43c215SJeff Kirsher 		return -1;
2840aa43c215SJeff Kirsher 
2841aa43c215SJeff Kirsher 	val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2842aa43c215SJeff Kirsher 	QLC_DEV_CLR_RST_QSCNT(val, portnum);
2843aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2844aa43c215SJeff Kirsher 
2845aa43c215SJeff Kirsher 	ret = qlcnic_check_idc_ver(adapter);
2846aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2847aa43c215SJeff Kirsher 
2848aa43c215SJeff Kirsher 	return ret;
2849aa43c215SJeff Kirsher }
2850aa43c215SJeff Kirsher 
2851aa43c215SJeff Kirsher static void
2852aa43c215SJeff Kirsher qlcnic_fwinit_work(struct work_struct *work)
2853aa43c215SJeff Kirsher {
2854aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
2855aa43c215SJeff Kirsher 			struct qlcnic_adapter, fw_work.work);
2856aa43c215SJeff Kirsher 	u32 dev_state = 0xf;
2857aa43c215SJeff Kirsher 	u32 val;
2858aa43c215SJeff Kirsher 
2859aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
2860aa43c215SJeff Kirsher 		goto err_ret;
2861aa43c215SJeff Kirsher 
2862aa43c215SJeff Kirsher 	dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2863aa43c215SJeff Kirsher 	if (dev_state == QLCNIC_DEV_QUISCENT ||
2864aa43c215SJeff Kirsher 	    dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2865aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2866aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2867aa43c215SJeff Kirsher 						FW_POLL_DELAY * 2);
2868aa43c215SJeff Kirsher 		return;
2869aa43c215SJeff Kirsher 	}
2870aa43c215SJeff Kirsher 
2871aa43c215SJeff Kirsher 	if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2872aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2873aa43c215SJeff Kirsher 		goto wait_npar;
2874aa43c215SJeff Kirsher 	}
2875aa43c215SJeff Kirsher 
287616e3cf73SSritej Velaga 	if (dev_state == QLCNIC_DEV_INITIALIZING ||
287716e3cf73SSritej Velaga 	    dev_state == QLCNIC_DEV_READY) {
287816e3cf73SSritej Velaga 		dev_info(&adapter->pdev->dev, "Detected state change from "
287916e3cf73SSritej Velaga 				"DEV_NEED_RESET, skipping ack check\n");
288016e3cf73SSritej Velaga 		goto skip_ack_check;
288116e3cf73SSritej Velaga 	}
288216e3cf73SSritej Velaga 
2883aa43c215SJeff Kirsher 	if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
288416e3cf73SSritej Velaga 		dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2885aa43c215SJeff Kirsher 					adapter->reset_ack_timeo);
2886aa43c215SJeff Kirsher 		goto skip_ack_check;
2887aa43c215SJeff Kirsher 	}
2888aa43c215SJeff Kirsher 
2889aa43c215SJeff Kirsher 	if (!qlcnic_check_drv_state(adapter)) {
2890aa43c215SJeff Kirsher skip_ack_check:
2891aa43c215SJeff Kirsher 		dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2892aa43c215SJeff Kirsher 
2893aa43c215SJeff Kirsher 		if (dev_state == QLCNIC_DEV_NEED_RESET) {
2894aa43c215SJeff Kirsher 			QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2895aa43c215SJeff Kirsher 						QLCNIC_DEV_INITIALIZING);
2896aa43c215SJeff Kirsher 			set_bit(__QLCNIC_START_FW, &adapter->state);
2897aa43c215SJeff Kirsher 			QLCDB(adapter, DRV, "Restarting fw\n");
2898aa43c215SJeff Kirsher 			qlcnic_idc_debug_info(adapter, 0);
2899aa43c215SJeff Kirsher 			val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2900aa43c215SJeff Kirsher 			QLC_DEV_SET_RST_RDY(val, adapter->portnum);
2901aa43c215SJeff Kirsher 			QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2902aa43c215SJeff Kirsher 		}
2903aa43c215SJeff Kirsher 
2904aa43c215SJeff Kirsher 		qlcnic_api_unlock(adapter);
2905aa43c215SJeff Kirsher 
2906aa43c215SJeff Kirsher 		rtnl_lock();
2907aa43c215SJeff Kirsher 		if (adapter->ahw->fw_dump.enable &&
2908aa43c215SJeff Kirsher 		    (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
2909aa43c215SJeff Kirsher 			QLCDB(adapter, DRV, "Take FW dump\n");
2910aa43c215SJeff Kirsher 			qlcnic_dump_fw(adapter);
2911aa43c215SJeff Kirsher 			adapter->flags |= QLCNIC_FW_HANG;
2912aa43c215SJeff Kirsher 		}
2913aa43c215SJeff Kirsher 		rtnl_unlock();
2914aa43c215SJeff Kirsher 
2915aa43c215SJeff Kirsher 		adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
2916aa43c215SJeff Kirsher 		if (!adapter->nic_ops->start_firmware(adapter)) {
2917aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2918aa43c215SJeff Kirsher 			adapter->fw_wait_cnt = 0;
2919aa43c215SJeff Kirsher 			return;
2920aa43c215SJeff Kirsher 		}
2921aa43c215SJeff Kirsher 		goto err_ret;
2922aa43c215SJeff Kirsher 	}
2923aa43c215SJeff Kirsher 
2924aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
2925aa43c215SJeff Kirsher 
2926aa43c215SJeff Kirsher wait_npar:
2927aa43c215SJeff Kirsher 	dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2928aa43c215SJeff Kirsher 	QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
2929aa43c215SJeff Kirsher 
2930aa43c215SJeff Kirsher 	switch (dev_state) {
2931aa43c215SJeff Kirsher 	case QLCNIC_DEV_READY:
2932aa43c215SJeff Kirsher 		if (!adapter->nic_ops->start_firmware(adapter)) {
2933aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2934aa43c215SJeff Kirsher 			adapter->fw_wait_cnt = 0;
2935aa43c215SJeff Kirsher 			return;
2936aa43c215SJeff Kirsher 		}
2937aa43c215SJeff Kirsher 	case QLCNIC_DEV_FAILED:
2938aa43c215SJeff Kirsher 		break;
2939aa43c215SJeff Kirsher 	default:
2940aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter,
2941aa43c215SJeff Kirsher 			qlcnic_fwinit_work, FW_POLL_DELAY);
2942aa43c215SJeff Kirsher 		return;
2943aa43c215SJeff Kirsher 	}
2944aa43c215SJeff Kirsher 
2945aa43c215SJeff Kirsher err_ret:
2946aa43c215SJeff Kirsher 	dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2947aa43c215SJeff Kirsher 		"fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
2948aa43c215SJeff Kirsher 	netif_device_attach(adapter->netdev);
2949aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 0);
2950aa43c215SJeff Kirsher }
2951aa43c215SJeff Kirsher 
2952aa43c215SJeff Kirsher static void
2953aa43c215SJeff Kirsher qlcnic_detach_work(struct work_struct *work)
2954aa43c215SJeff Kirsher {
2955aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
2956aa43c215SJeff Kirsher 			struct qlcnic_adapter, fw_work.work);
2957aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
2958aa43c215SJeff Kirsher 	u32 status;
2959aa43c215SJeff Kirsher 
2960aa43c215SJeff Kirsher 	netif_device_detach(netdev);
2961aa43c215SJeff Kirsher 
2962aa43c215SJeff Kirsher 	/* Dont grab rtnl lock during Quiscent mode */
2963aa43c215SJeff Kirsher 	if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2964aa43c215SJeff Kirsher 		if (netif_running(netdev))
2965aa43c215SJeff Kirsher 			__qlcnic_down(adapter, netdev);
2966aa43c215SJeff Kirsher 	} else
2967aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
2968aa43c215SJeff Kirsher 
2969aa43c215SJeff Kirsher 	status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2970aa43c215SJeff Kirsher 
297144f65b29SSony Chacko 	if (status & QLCNIC_RCODE_FATAL_ERROR) {
297244f65b29SSony Chacko 		dev_err(&adapter->pdev->dev,
297344f65b29SSony Chacko 			"Detaching the device: peg halt status1=0x%x\n",
297444f65b29SSony Chacko 					status);
2975aa43c215SJeff Kirsher 
297644f65b29SSony Chacko 		if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
297744f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
297844f65b29SSony Chacko 			"On board active cooling fan failed. "
297944f65b29SSony Chacko 				"Device has been halted.\n");
298044f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
298144f65b29SSony Chacko 				"Replace the adapter.\n");
298244f65b29SSony Chacko 		}
298344f65b29SSony Chacko 
2984aa43c215SJeff Kirsher 		goto err_ret;
298544f65b29SSony Chacko 	}
298644f65b29SSony Chacko 
298744f65b29SSony Chacko 	if (adapter->temp == QLCNIC_TEMP_PANIC) {
298844f65b29SSony Chacko 		dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
298944f65b29SSony Chacko 			adapter->temp);
299044f65b29SSony Chacko 		goto err_ret;
299144f65b29SSony Chacko 	}
299244f65b29SSony Chacko 
2993aa43c215SJeff Kirsher 	/* Dont ack if this instance is the reset owner */
2994aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
299544f65b29SSony Chacko 		if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
299644f65b29SSony Chacko 			dev_err(&adapter->pdev->dev,
299744f65b29SSony Chacko 				"Failed to set driver state,"
299844f65b29SSony Chacko 					"detaching the device.\n");
2999aa43c215SJeff Kirsher 			goto err_ret;
3000aa43c215SJeff Kirsher 		}
300144f65b29SSony Chacko 	}
3002aa43c215SJeff Kirsher 
3003aa43c215SJeff Kirsher 	adapter->fw_wait_cnt = 0;
3004aa43c215SJeff Kirsher 
3005aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3006aa43c215SJeff Kirsher 
3007aa43c215SJeff Kirsher 	return;
3008aa43c215SJeff Kirsher 
3009aa43c215SJeff Kirsher err_ret:
3010aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3011aa43c215SJeff Kirsher 	qlcnic_clr_all_drv_state(adapter, 1);
3012aa43c215SJeff Kirsher }
3013aa43c215SJeff Kirsher 
3014aa43c215SJeff Kirsher /*Transit NPAR state to NON Operational */
3015aa43c215SJeff Kirsher static void
3016aa43c215SJeff Kirsher qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3017aa43c215SJeff Kirsher {
3018aa43c215SJeff Kirsher 	u32 state;
3019aa43c215SJeff Kirsher 
3020aa43c215SJeff Kirsher 	state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3021aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NPAR_NON_OPER)
3022aa43c215SJeff Kirsher 		return;
3023aa43c215SJeff Kirsher 
3024aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
3025aa43c215SJeff Kirsher 		return;
3026aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3027aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
3028aa43c215SJeff Kirsher }
3029aa43c215SJeff Kirsher 
3030aa43c215SJeff Kirsher /*Transit to RESET state from READY state only */
3031aa43c215SJeff Kirsher void
3032aa43c215SJeff Kirsher qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
3033aa43c215SJeff Kirsher {
3034646779f1SSritej Velaga 	u32 state, xg_val = 0, gb_val = 0;
3035aa43c215SJeff Kirsher 
3036646779f1SSritej Velaga 	qlcnic_xg_set_xg0_mask(xg_val);
3037646779f1SSritej Velaga 	qlcnic_xg_set_xg1_mask(xg_val);
3038646779f1SSritej Velaga 	QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3039646779f1SSritej Velaga 	qlcnic_gb_set_gb0_mask(gb_val);
3040646779f1SSritej Velaga 	qlcnic_gb_set_gb1_mask(gb_val);
3041646779f1SSritej Velaga 	qlcnic_gb_set_gb2_mask(gb_val);
3042646779f1SSritej Velaga 	qlcnic_gb_set_gb3_mask(gb_val);
3043646779f1SSritej Velaga 	QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3044646779f1SSritej Velaga 	dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3045646779f1SSritej Velaga 				" on all ports\n");
3046aa43c215SJeff Kirsher 	adapter->need_fw_reset = 1;
3047aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
3048aa43c215SJeff Kirsher 		return;
3049aa43c215SJeff Kirsher 
3050aa43c215SJeff Kirsher 	state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3051b43e5ee7SSucheta Chakraborty 	if (state  == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD)) {
3052b43e5ee7SSucheta Chakraborty 		netdev_err(adapter->netdev,
3053b43e5ee7SSucheta Chakraborty 				"Device is in FAILED state, Please Reboot\n");
3054b43e5ee7SSucheta Chakraborty 		qlcnic_api_unlock(adapter);
3055b43e5ee7SSucheta Chakraborty 		return;
3056b43e5ee7SSucheta Chakraborty 	}
3057aa43c215SJeff Kirsher 
3058aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_READY) {
3059aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
3060aa43c215SJeff Kirsher 		adapter->flags |= QLCNIC_FW_RESET_OWNER;
3061aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "NEED_RESET state set\n");
3062aa43c215SJeff Kirsher 		qlcnic_idc_debug_info(adapter, 0);
3063aa43c215SJeff Kirsher 	}
3064aa43c215SJeff Kirsher 
3065aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
3066aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
3067aa43c215SJeff Kirsher }
3068aa43c215SJeff Kirsher 
3069aa43c215SJeff Kirsher /* Transit to NPAR READY state from NPAR NOT READY state */
3070aa43c215SJeff Kirsher static void
3071aa43c215SJeff Kirsher qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3072aa43c215SJeff Kirsher {
3073aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
3074aa43c215SJeff Kirsher 		return;
3075aa43c215SJeff Kirsher 
3076aa43c215SJeff Kirsher 	QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
3077aa43c215SJeff Kirsher 	QLCDB(adapter, DRV, "NPAR operational state set\n");
3078aa43c215SJeff Kirsher 
3079aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
3080aa43c215SJeff Kirsher }
3081aa43c215SJeff Kirsher 
3082aa43c215SJeff Kirsher static void
3083aa43c215SJeff Kirsher qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3084aa43c215SJeff Kirsher 		work_func_t func, int delay)
3085aa43c215SJeff Kirsher {
3086aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_AER, &adapter->state))
3087aa43c215SJeff Kirsher 		return;
3088aa43c215SJeff Kirsher 
3089aa43c215SJeff Kirsher 	INIT_DELAYED_WORK(&adapter->fw_work, func);
3090aa43c215SJeff Kirsher 	queue_delayed_work(qlcnic_wq, &adapter->fw_work,
3091aa43c215SJeff Kirsher 					round_jiffies_relative(delay));
3092aa43c215SJeff Kirsher }
3093aa43c215SJeff Kirsher 
3094aa43c215SJeff Kirsher static void
3095aa43c215SJeff Kirsher qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
3096aa43c215SJeff Kirsher {
3097aa43c215SJeff Kirsher 	while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3098aa43c215SJeff Kirsher 		msleep(10);
3099aa43c215SJeff Kirsher 
3100b43e5ee7SSucheta Chakraborty 	if (!adapter->fw_work.work.func)
3101b43e5ee7SSucheta Chakraborty 		return;
3102b43e5ee7SSucheta Chakraborty 
3103aa43c215SJeff Kirsher 	cancel_delayed_work_sync(&adapter->fw_work);
3104aa43c215SJeff Kirsher }
3105aa43c215SJeff Kirsher 
3106aa43c215SJeff Kirsher static void
3107aa43c215SJeff Kirsher qlcnic_attach_work(struct work_struct *work)
3108aa43c215SJeff Kirsher {
3109aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
3110aa43c215SJeff Kirsher 				struct qlcnic_adapter, fw_work.work);
3111aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3112aa43c215SJeff Kirsher 	u32 npar_state;
3113aa43c215SJeff Kirsher 
3114aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
3115aa43c215SJeff Kirsher 		npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3116aa43c215SJeff Kirsher 		if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3117aa43c215SJeff Kirsher 			qlcnic_clr_all_drv_state(adapter, 0);
3118aa43c215SJeff Kirsher 		else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3119aa43c215SJeff Kirsher 			qlcnic_schedule_work(adapter, qlcnic_attach_work,
3120aa43c215SJeff Kirsher 							FW_POLL_DELAY);
3121aa43c215SJeff Kirsher 		else
3122aa43c215SJeff Kirsher 			goto attach;
3123aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3124aa43c215SJeff Kirsher 		return;
3125aa43c215SJeff Kirsher 	}
3126aa43c215SJeff Kirsher attach:
3127aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
3128aa43c215SJeff Kirsher 		if (qlcnic_up(adapter, netdev))
3129aa43c215SJeff Kirsher 			goto done;
3130aa43c215SJeff Kirsher 
3131aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3132aa43c215SJeff Kirsher 	}
3133aa43c215SJeff Kirsher 
3134aa43c215SJeff Kirsher done:
3135aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3136aa43c215SJeff Kirsher 	adapter->fw_fail_cnt = 0;
3137aa43c215SJeff Kirsher 	adapter->flags &= ~QLCNIC_FW_HANG;
3138aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3139aa43c215SJeff Kirsher 
3140aa43c215SJeff Kirsher 	if (!qlcnic_clr_drv_state(adapter))
3141aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3142aa43c215SJeff Kirsher 							FW_POLL_DELAY);
3143aa43c215SJeff Kirsher }
3144aa43c215SJeff Kirsher 
3145aa43c215SJeff Kirsher static int
3146aa43c215SJeff Kirsher qlcnic_check_health(struct qlcnic_adapter *adapter)
3147aa43c215SJeff Kirsher {
3148aa43c215SJeff Kirsher 	u32 state = 0, heartbeat;
3149853d4bcaSAmeen Rahman 	u32 peg_status;
3150aa43c215SJeff Kirsher 
3151aa43c215SJeff Kirsher 	if (qlcnic_check_temp(adapter))
3152aa43c215SJeff Kirsher 		goto detach;
3153aa43c215SJeff Kirsher 
3154aa43c215SJeff Kirsher 	if (adapter->need_fw_reset)
3155aa43c215SJeff Kirsher 		qlcnic_dev_request_reset(adapter);
3156aa43c215SJeff Kirsher 
3157aa43c215SJeff Kirsher 	state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
3158aa43c215SJeff Kirsher 	if (state == QLCNIC_DEV_NEED_RESET) {
3159aa43c215SJeff Kirsher 		qlcnic_set_npar_non_operational(adapter);
3160aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
3161aa43c215SJeff Kirsher 	} else if (state == QLCNIC_DEV_NEED_QUISCENT)
3162aa43c215SJeff Kirsher 		goto detach;
3163aa43c215SJeff Kirsher 
3164aa43c215SJeff Kirsher 	heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3165aa43c215SJeff Kirsher 	if (heartbeat != adapter->heartbeat) {
3166aa43c215SJeff Kirsher 		adapter->heartbeat = heartbeat;
3167aa43c215SJeff Kirsher 		adapter->fw_fail_cnt = 0;
3168aa43c215SJeff Kirsher 		if (adapter->need_fw_reset)
3169aa43c215SJeff Kirsher 			goto detach;
3170aa43c215SJeff Kirsher 
3171aa43c215SJeff Kirsher 		if (adapter->reset_context && auto_fw_reset) {
3172aa43c215SJeff Kirsher 			qlcnic_reset_hw_context(adapter);
3173aa43c215SJeff Kirsher 			adapter->netdev->trans_start = jiffies;
3174aa43c215SJeff Kirsher 		}
3175aa43c215SJeff Kirsher 
3176aa43c215SJeff Kirsher 		return 0;
3177aa43c215SJeff Kirsher 	}
3178aa43c215SJeff Kirsher 
3179aa43c215SJeff Kirsher 	if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3180aa43c215SJeff Kirsher 		return 0;
3181aa43c215SJeff Kirsher 
3182aa43c215SJeff Kirsher 	adapter->flags |= QLCNIC_FW_HANG;
3183aa43c215SJeff Kirsher 
3184aa43c215SJeff Kirsher 	qlcnic_dev_request_reset(adapter);
3185aa43c215SJeff Kirsher 
3186aa43c215SJeff Kirsher 	if (auto_fw_reset)
3187aa43c215SJeff Kirsher 		clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3188aa43c215SJeff Kirsher 
3189853d4bcaSAmeen Rahman 	dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3190853d4bcaSAmeen Rahman 	dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3191aa43c215SJeff Kirsher 			"PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3192aa43c215SJeff Kirsher 			"PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3193aa43c215SJeff Kirsher 			"PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3194aa43c215SJeff Kirsher 			"PEG_NET_4_PC: 0x%x\n",
3195aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1),
3196aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3197aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c),
3198aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c),
3199aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c),
3200aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c),
3201aa43c215SJeff Kirsher 			QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c));
3202853d4bcaSAmeen Rahman 	peg_status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
320397048a1fSSritej Velaga 	if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3204853d4bcaSAmeen Rahman 		dev_err(&adapter->pdev->dev,
3205853d4bcaSAmeen Rahman 			"Firmware aborted with error code 0x00006700. "
3206853d4bcaSAmeen Rahman 				"Device is being reset.\n");
3207aa43c215SJeff Kirsher detach:
3208aa43c215SJeff Kirsher 	adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3209aa43c215SJeff Kirsher 		QLCNIC_DEV_NEED_RESET;
3210aa43c215SJeff Kirsher 
3211aa43c215SJeff Kirsher 	if (auto_fw_reset &&
3212aa43c215SJeff Kirsher 		!test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3213aa43c215SJeff Kirsher 
3214aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3215aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3216aa43c215SJeff Kirsher 	}
3217aa43c215SJeff Kirsher 
3218aa43c215SJeff Kirsher 	return 1;
3219aa43c215SJeff Kirsher }
3220aa43c215SJeff Kirsher 
3221aa43c215SJeff Kirsher static void
3222aa43c215SJeff Kirsher qlcnic_fw_poll_work(struct work_struct *work)
3223aa43c215SJeff Kirsher {
3224aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = container_of(work,
3225aa43c215SJeff Kirsher 				struct qlcnic_adapter, fw_work.work);
3226aa43c215SJeff Kirsher 
3227aa43c215SJeff Kirsher 	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3228aa43c215SJeff Kirsher 		goto reschedule;
3229aa43c215SJeff Kirsher 
3230aa43c215SJeff Kirsher 
3231aa43c215SJeff Kirsher 	if (qlcnic_check_health(adapter))
3232aa43c215SJeff Kirsher 		return;
3233aa43c215SJeff Kirsher 
3234aa43c215SJeff Kirsher 	if (adapter->fhash.fnum)
3235aa43c215SJeff Kirsher 		qlcnic_prune_lb_filters(adapter);
3236aa43c215SJeff Kirsher 
3237aa43c215SJeff Kirsher reschedule:
3238aa43c215SJeff Kirsher 	qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3239aa43c215SJeff Kirsher }
3240aa43c215SJeff Kirsher 
3241aa43c215SJeff Kirsher static int qlcnic_is_first_func(struct pci_dev *pdev)
3242aa43c215SJeff Kirsher {
3243aa43c215SJeff Kirsher 	struct pci_dev *oth_pdev;
3244aa43c215SJeff Kirsher 	int val = pdev->devfn;
3245aa43c215SJeff Kirsher 
3246aa43c215SJeff Kirsher 	while (val-- > 0) {
3247aa43c215SJeff Kirsher 		oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3248aa43c215SJeff Kirsher 			(pdev->bus), pdev->bus->number,
3249aa43c215SJeff Kirsher 			PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3250aa43c215SJeff Kirsher 		if (!oth_pdev)
3251aa43c215SJeff Kirsher 			continue;
3252aa43c215SJeff Kirsher 
3253aa43c215SJeff Kirsher 		if (oth_pdev->current_state != PCI_D3cold) {
3254aa43c215SJeff Kirsher 			pci_dev_put(oth_pdev);
3255aa43c215SJeff Kirsher 			return 0;
3256aa43c215SJeff Kirsher 		}
3257aa43c215SJeff Kirsher 		pci_dev_put(oth_pdev);
3258aa43c215SJeff Kirsher 	}
3259aa43c215SJeff Kirsher 	return 1;
3260aa43c215SJeff Kirsher }
3261aa43c215SJeff Kirsher 
3262aa43c215SJeff Kirsher static int qlcnic_attach_func(struct pci_dev *pdev)
3263aa43c215SJeff Kirsher {
3264aa43c215SJeff Kirsher 	int err, first_func;
3265aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3266aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3267aa43c215SJeff Kirsher 
3268aa43c215SJeff Kirsher 	pdev->error_state = pci_channel_io_normal;
3269aa43c215SJeff Kirsher 
3270aa43c215SJeff Kirsher 	err = pci_enable_device(pdev);
3271aa43c215SJeff Kirsher 	if (err)
3272aa43c215SJeff Kirsher 		return err;
3273aa43c215SJeff Kirsher 
3274aa43c215SJeff Kirsher 	pci_set_power_state(pdev, PCI_D0);
3275aa43c215SJeff Kirsher 	pci_set_master(pdev);
3276aa43c215SJeff Kirsher 	pci_restore_state(pdev);
3277aa43c215SJeff Kirsher 
3278aa43c215SJeff Kirsher 	first_func = qlcnic_is_first_func(pdev);
3279aa43c215SJeff Kirsher 
3280aa43c215SJeff Kirsher 	if (qlcnic_api_lock(adapter))
3281aa43c215SJeff Kirsher 		return -EINVAL;
3282aa43c215SJeff Kirsher 
3283aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3284aa43c215SJeff Kirsher 		adapter->need_fw_reset = 1;
3285aa43c215SJeff Kirsher 		set_bit(__QLCNIC_START_FW, &adapter->state);
3286aa43c215SJeff Kirsher 		QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3287aa43c215SJeff Kirsher 		QLCDB(adapter, DRV, "Restarting fw\n");
3288aa43c215SJeff Kirsher 	}
3289aa43c215SJeff Kirsher 	qlcnic_api_unlock(adapter);
3290aa43c215SJeff Kirsher 
3291aa43c215SJeff Kirsher 	err = adapter->nic_ops->start_firmware(adapter);
3292aa43c215SJeff Kirsher 	if (err)
3293aa43c215SJeff Kirsher 		return err;
3294aa43c215SJeff Kirsher 
3295aa43c215SJeff Kirsher 	qlcnic_clr_drv_state(adapter);
3296aa43c215SJeff Kirsher 	qlcnic_setup_intr(adapter);
3297aa43c215SJeff Kirsher 
3298aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
3299aa43c215SJeff Kirsher 		err = qlcnic_attach(adapter);
3300aa43c215SJeff Kirsher 		if (err) {
3301aa43c215SJeff Kirsher 			qlcnic_clr_all_drv_state(adapter, 1);
3302aa43c215SJeff Kirsher 			clear_bit(__QLCNIC_AER, &adapter->state);
3303aa43c215SJeff Kirsher 			netif_device_attach(netdev);
3304aa43c215SJeff Kirsher 			return err;
3305aa43c215SJeff Kirsher 		}
3306aa43c215SJeff Kirsher 
3307aa43c215SJeff Kirsher 		err = qlcnic_up(adapter, netdev);
3308aa43c215SJeff Kirsher 		if (err)
3309aa43c215SJeff Kirsher 			goto done;
3310aa43c215SJeff Kirsher 
3311aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3312aa43c215SJeff Kirsher 	}
3313aa43c215SJeff Kirsher  done:
3314aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3315aa43c215SJeff Kirsher 	return err;
3316aa43c215SJeff Kirsher }
3317aa43c215SJeff Kirsher 
3318aa43c215SJeff Kirsher static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3319aa43c215SJeff Kirsher 						pci_channel_state_t state)
3320aa43c215SJeff Kirsher {
3321aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3322aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3323aa43c215SJeff Kirsher 
3324aa43c215SJeff Kirsher 	if (state == pci_channel_io_perm_failure)
3325aa43c215SJeff Kirsher 		return PCI_ERS_RESULT_DISCONNECT;
3326aa43c215SJeff Kirsher 
3327aa43c215SJeff Kirsher 	if (state == pci_channel_io_normal)
3328aa43c215SJeff Kirsher 		return PCI_ERS_RESULT_RECOVERED;
3329aa43c215SJeff Kirsher 
3330aa43c215SJeff Kirsher 	set_bit(__QLCNIC_AER, &adapter->state);
3331aa43c215SJeff Kirsher 	netif_device_detach(netdev);
3332aa43c215SJeff Kirsher 
3333aa43c215SJeff Kirsher 	cancel_delayed_work_sync(&adapter->fw_work);
3334aa43c215SJeff Kirsher 
3335aa43c215SJeff Kirsher 	if (netif_running(netdev))
3336aa43c215SJeff Kirsher 		qlcnic_down(adapter, netdev);
3337aa43c215SJeff Kirsher 
3338aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
3339aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
3340aa43c215SJeff Kirsher 
3341aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3342aa43c215SJeff Kirsher 
3343aa43c215SJeff Kirsher 	pci_save_state(pdev);
3344aa43c215SJeff Kirsher 	pci_disable_device(pdev);
3345aa43c215SJeff Kirsher 
3346aa43c215SJeff Kirsher 	return PCI_ERS_RESULT_NEED_RESET;
3347aa43c215SJeff Kirsher }
3348aa43c215SJeff Kirsher 
3349aa43c215SJeff Kirsher static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3350aa43c215SJeff Kirsher {
3351aa43c215SJeff Kirsher 	return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3352aa43c215SJeff Kirsher 				PCI_ERS_RESULT_RECOVERED;
3353aa43c215SJeff Kirsher }
3354aa43c215SJeff Kirsher 
3355aa43c215SJeff Kirsher static void qlcnic_io_resume(struct pci_dev *pdev)
3356aa43c215SJeff Kirsher {
3357aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3358aa43c215SJeff Kirsher 
3359aa43c215SJeff Kirsher 	pci_cleanup_aer_uncorrect_error_status(pdev);
3360aa43c215SJeff Kirsher 
3361aa43c215SJeff Kirsher 	if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3362aa43c215SJeff Kirsher 	    test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3363aa43c215SJeff Kirsher 		qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3364aa43c215SJeff Kirsher 						FW_POLL_DELAY);
3365aa43c215SJeff Kirsher }
3366aa43c215SJeff Kirsher 
3367aa43c215SJeff Kirsher static int
3368aa43c215SJeff Kirsher qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3369aa43c215SJeff Kirsher {
3370aa43c215SJeff Kirsher 	int err;
3371aa43c215SJeff Kirsher 
3372aa43c215SJeff Kirsher 	err = qlcnic_can_start_firmware(adapter);
3373aa43c215SJeff Kirsher 	if (err)
3374aa43c215SJeff Kirsher 		return err;
3375aa43c215SJeff Kirsher 
3376aa43c215SJeff Kirsher 	err = qlcnic_check_npar_opertional(adapter);
3377aa43c215SJeff Kirsher 	if (err)
3378aa43c215SJeff Kirsher 		return err;
3379aa43c215SJeff Kirsher 
3380aa43c215SJeff Kirsher 	err = qlcnic_initialize_nic(adapter);
3381aa43c215SJeff Kirsher 	if (err)
3382aa43c215SJeff Kirsher 		return err;
3383aa43c215SJeff Kirsher 
3384aa43c215SJeff Kirsher 	qlcnic_check_options(adapter);
3385aa43c215SJeff Kirsher 
3386aa43c215SJeff Kirsher 	err = qlcnic_set_eswitch_port_config(adapter);
3387aa43c215SJeff Kirsher 	if (err)
3388aa43c215SJeff Kirsher 		return err;
3389aa43c215SJeff Kirsher 
3390aa43c215SJeff Kirsher 	adapter->need_fw_reset = 0;
3391aa43c215SJeff Kirsher 
3392aa43c215SJeff Kirsher 	return err;
3393aa43c215SJeff Kirsher }
3394aa43c215SJeff Kirsher 
3395aa43c215SJeff Kirsher static int
3396aa43c215SJeff Kirsher qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3397aa43c215SJeff Kirsher {
3398aa43c215SJeff Kirsher 	return -EOPNOTSUPP;
3399aa43c215SJeff Kirsher }
3400aa43c215SJeff Kirsher 
3401aa43c215SJeff Kirsher static int
3402aa43c215SJeff Kirsher qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3403aa43c215SJeff Kirsher {
3404aa43c215SJeff Kirsher 	return -EOPNOTSUPP;
3405aa43c215SJeff Kirsher }
3406aa43c215SJeff Kirsher 
3407aa43c215SJeff Kirsher static ssize_t
3408aa43c215SJeff Kirsher qlcnic_store_bridged_mode(struct device *dev,
3409aa43c215SJeff Kirsher 		struct device_attribute *attr, const char *buf, size_t len)
3410aa43c215SJeff Kirsher {
3411aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3412aa43c215SJeff Kirsher 	unsigned long new;
3413aa43c215SJeff Kirsher 	int ret = -EINVAL;
3414aa43c215SJeff Kirsher 
3415aa43c215SJeff Kirsher 	if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3416aa43c215SJeff Kirsher 		goto err_out;
3417aa43c215SJeff Kirsher 
3418aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
3419aa43c215SJeff Kirsher 		goto err_out;
3420aa43c215SJeff Kirsher 
3421aa43c215SJeff Kirsher 	if (strict_strtoul(buf, 2, &new))
3422aa43c215SJeff Kirsher 		goto err_out;
3423aa43c215SJeff Kirsher 
3424aa43c215SJeff Kirsher 	if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
3425aa43c215SJeff Kirsher 		ret = len;
3426aa43c215SJeff Kirsher 
3427aa43c215SJeff Kirsher err_out:
3428aa43c215SJeff Kirsher 	return ret;
3429aa43c215SJeff Kirsher }
3430aa43c215SJeff Kirsher 
3431aa43c215SJeff Kirsher static ssize_t
3432aa43c215SJeff Kirsher qlcnic_show_bridged_mode(struct device *dev,
3433aa43c215SJeff Kirsher 		struct device_attribute *attr, char *buf)
3434aa43c215SJeff Kirsher {
3435aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3436aa43c215SJeff Kirsher 	int bridged_mode = 0;
3437aa43c215SJeff Kirsher 
3438aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3439aa43c215SJeff Kirsher 		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3440aa43c215SJeff Kirsher 
3441aa43c215SJeff Kirsher 	return sprintf(buf, "%d\n", bridged_mode);
3442aa43c215SJeff Kirsher }
3443aa43c215SJeff Kirsher 
3444aa43c215SJeff Kirsher static struct device_attribute dev_attr_bridged_mode = {
3445aa43c215SJeff Kirsher        .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3446aa43c215SJeff Kirsher        .show = qlcnic_show_bridged_mode,
3447aa43c215SJeff Kirsher        .store = qlcnic_store_bridged_mode,
3448aa43c215SJeff Kirsher };
3449aa43c215SJeff Kirsher 
3450aa43c215SJeff Kirsher static ssize_t
3451aa43c215SJeff Kirsher qlcnic_store_diag_mode(struct device *dev,
3452aa43c215SJeff Kirsher 		struct device_attribute *attr, const char *buf, size_t len)
3453aa43c215SJeff Kirsher {
3454aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3455aa43c215SJeff Kirsher 	unsigned long new;
3456aa43c215SJeff Kirsher 
3457aa43c215SJeff Kirsher 	if (strict_strtoul(buf, 2, &new))
3458aa43c215SJeff Kirsher 		return -EINVAL;
3459aa43c215SJeff Kirsher 
3460aa43c215SJeff Kirsher 	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3461aa43c215SJeff Kirsher 		adapter->flags ^= QLCNIC_DIAG_ENABLED;
3462aa43c215SJeff Kirsher 
3463aa43c215SJeff Kirsher 	return len;
3464aa43c215SJeff Kirsher }
3465aa43c215SJeff Kirsher 
3466aa43c215SJeff Kirsher static ssize_t
3467aa43c215SJeff Kirsher qlcnic_show_diag_mode(struct device *dev,
3468aa43c215SJeff Kirsher 		struct device_attribute *attr, char *buf)
3469aa43c215SJeff Kirsher {
3470aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3471aa43c215SJeff Kirsher 
3472aa43c215SJeff Kirsher 	return sprintf(buf, "%d\n",
3473aa43c215SJeff Kirsher 			!!(adapter->flags & QLCNIC_DIAG_ENABLED));
3474aa43c215SJeff Kirsher }
3475aa43c215SJeff Kirsher 
3476aa43c215SJeff Kirsher static struct device_attribute dev_attr_diag_mode = {
3477aa43c215SJeff Kirsher 	.attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3478aa43c215SJeff Kirsher 	.show = qlcnic_show_diag_mode,
3479aa43c215SJeff Kirsher 	.store = qlcnic_store_diag_mode,
3480aa43c215SJeff Kirsher };
3481aa43c215SJeff Kirsher 
3482aa43c215SJeff Kirsher int qlcnic_validate_max_rss(struct net_device *netdev, u8 max_hw, u8 val)
3483aa43c215SJeff Kirsher {
3484aa43c215SJeff Kirsher 	if (!use_msi_x && !use_msi) {
3485aa43c215SJeff Kirsher 		netdev_info(netdev, "no msix or msi support, hence no rss\n");
3486aa43c215SJeff Kirsher 		return -EINVAL;
3487aa43c215SJeff Kirsher 	}
3488aa43c215SJeff Kirsher 
3489aa43c215SJeff Kirsher 	if ((val > max_hw) || (val <  2) || !is_power_of_2(val)) {
3490aa43c215SJeff Kirsher 		netdev_info(netdev, "rss_ring valid range [2 - %x] in "
3491aa43c215SJeff Kirsher 			" powers of 2\n", max_hw);
3492aa43c215SJeff Kirsher 		return -EINVAL;
3493aa43c215SJeff Kirsher 	}
3494aa43c215SJeff Kirsher 	return 0;
3495aa43c215SJeff Kirsher 
3496aa43c215SJeff Kirsher }
3497aa43c215SJeff Kirsher 
3498aa43c215SJeff Kirsher int qlcnic_set_max_rss(struct qlcnic_adapter *adapter, u8 data)
3499aa43c215SJeff Kirsher {
3500aa43c215SJeff Kirsher 	struct net_device *netdev = adapter->netdev;
3501aa43c215SJeff Kirsher 	int err = 0;
3502aa43c215SJeff Kirsher 
3503aa43c215SJeff Kirsher 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
3504aa43c215SJeff Kirsher 		return -EBUSY;
3505aa43c215SJeff Kirsher 
3506aa43c215SJeff Kirsher 	netif_device_detach(netdev);
3507aa43c215SJeff Kirsher 	if (netif_running(netdev))
3508aa43c215SJeff Kirsher 		__qlcnic_down(adapter, netdev);
3509aa43c215SJeff Kirsher 	qlcnic_detach(adapter);
3510aa43c215SJeff Kirsher 	qlcnic_teardown_intr(adapter);
3511aa43c215SJeff Kirsher 
3512aa43c215SJeff Kirsher 	if (qlcnic_enable_msix(adapter, data)) {
3513aa43c215SJeff Kirsher 		netdev_info(netdev, "failed setting max_rss; rss disabled\n");
3514aa43c215SJeff Kirsher 		qlcnic_enable_msi_legacy(adapter);
3515aa43c215SJeff Kirsher 	}
3516aa43c215SJeff Kirsher 
3517aa43c215SJeff Kirsher 	if (netif_running(netdev)) {
3518aa43c215SJeff Kirsher 		err = qlcnic_attach(adapter);
3519aa43c215SJeff Kirsher 		if (err)
3520aa43c215SJeff Kirsher 			goto done;
3521aa43c215SJeff Kirsher 		err = __qlcnic_up(adapter, netdev);
3522aa43c215SJeff Kirsher 		if (err)
3523aa43c215SJeff Kirsher 			goto done;
3524aa43c215SJeff Kirsher 		qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3525aa43c215SJeff Kirsher 	}
3526aa43c215SJeff Kirsher  done:
3527aa43c215SJeff Kirsher 	netif_device_attach(netdev);
3528aa43c215SJeff Kirsher 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
3529aa43c215SJeff Kirsher 	return err;
3530aa43c215SJeff Kirsher }
3531aa43c215SJeff Kirsher 
3532aa43c215SJeff Kirsher static int
3533728a98b8SSucheta Chakraborty qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon, u8 *state,
3534728a98b8SSucheta Chakraborty 			u8 *rate)
3535728a98b8SSucheta Chakraborty {
3536728a98b8SSucheta Chakraborty 	*rate = LSB(beacon);
3537728a98b8SSucheta Chakraborty 	*state = MSB(beacon);
3538728a98b8SSucheta Chakraborty 
3539728a98b8SSucheta Chakraborty 	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
3540728a98b8SSucheta Chakraborty 
3541728a98b8SSucheta Chakraborty 	if (!*state) {
3542728a98b8SSucheta Chakraborty 		*rate = __QLCNIC_MAX_LED_RATE;
3543728a98b8SSucheta Chakraborty 		return 0;
3544728a98b8SSucheta Chakraborty 	} else if (*state > __QLCNIC_MAX_LED_STATE)
3545728a98b8SSucheta Chakraborty 		return -EINVAL;
3546728a98b8SSucheta Chakraborty 
3547728a98b8SSucheta Chakraborty 	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
3548728a98b8SSucheta Chakraborty 		return -EINVAL;
3549728a98b8SSucheta Chakraborty 
3550728a98b8SSucheta Chakraborty 	return 0;
3551728a98b8SSucheta Chakraborty }
3552728a98b8SSucheta Chakraborty 
3553728a98b8SSucheta Chakraborty static ssize_t
3554728a98b8SSucheta Chakraborty qlcnic_store_beacon(struct device *dev,
3555728a98b8SSucheta Chakraborty 		struct device_attribute *attr, const char *buf, size_t len)
3556728a98b8SSucheta Chakraborty {
3557728a98b8SSucheta Chakraborty 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3558728a98b8SSucheta Chakraborty 	int max_sds_rings = adapter->max_sds_rings;
3559728a98b8SSucheta Chakraborty 	u16 beacon;
3560728a98b8SSucheta Chakraborty 	u8 b_state, b_rate;
3561728a98b8SSucheta Chakraborty 	int err;
3562728a98b8SSucheta Chakraborty 
356310ee0faeSSucheta Chakraborty 	if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
356410ee0faeSSucheta Chakraborty 		dev_warn(dev, "LED test not supported for non "
356510ee0faeSSucheta Chakraborty 				"privilege function\n");
356610ee0faeSSucheta Chakraborty 		return -EOPNOTSUPP;
356710ee0faeSSucheta Chakraborty 	}
356810ee0faeSSucheta Chakraborty 
3569728a98b8SSucheta Chakraborty 	if (len != sizeof(u16))
3570728a98b8SSucheta Chakraborty 		return QL_STATUS_INVALID_PARAM;
3571728a98b8SSucheta Chakraborty 
3572728a98b8SSucheta Chakraborty 	memcpy(&beacon, buf, sizeof(u16));
3573728a98b8SSucheta Chakraborty 	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
3574728a98b8SSucheta Chakraborty 	if (err)
3575728a98b8SSucheta Chakraborty 		return err;
3576728a98b8SSucheta Chakraborty 
3577728a98b8SSucheta Chakraborty 	if (adapter->ahw->beacon_state == b_state)
3578728a98b8SSucheta Chakraborty 		return len;
3579728a98b8SSucheta Chakraborty 
358010ee0faeSSucheta Chakraborty 	rtnl_lock();
358110ee0faeSSucheta Chakraborty 
3582728a98b8SSucheta Chakraborty 	if (!adapter->ahw->beacon_state)
358310ee0faeSSucheta Chakraborty 		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
358410ee0faeSSucheta Chakraborty 			rtnl_unlock();
3585728a98b8SSucheta Chakraborty 			return -EBUSY;
358610ee0faeSSucheta Chakraborty 		}
358710ee0faeSSucheta Chakraborty 
358810ee0faeSSucheta Chakraborty 	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
358910ee0faeSSucheta Chakraborty 		err = -EIO;
359010ee0faeSSucheta Chakraborty 		goto out;
359110ee0faeSSucheta Chakraborty 	}
3592728a98b8SSucheta Chakraborty 
3593728a98b8SSucheta Chakraborty 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
3594728a98b8SSucheta Chakraborty 		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
359510ee0faeSSucheta Chakraborty 		if (err)
359610ee0faeSSucheta Chakraborty 			goto out;
359710ee0faeSSucheta Chakraborty 		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
3598728a98b8SSucheta Chakraborty 	}
3599728a98b8SSucheta Chakraborty 
3600728a98b8SSucheta Chakraborty 	err = qlcnic_config_led(adapter, b_state, b_rate);
3601728a98b8SSucheta Chakraborty 
3602728a98b8SSucheta Chakraborty 	if (!err) {
3603728a98b8SSucheta Chakraborty 		err = len;
360410ee0faeSSucheta Chakraborty 		adapter->ahw->beacon_state = b_state;
3605728a98b8SSucheta Chakraborty 	}
3606728a98b8SSucheta Chakraborty 
360710ee0faeSSucheta Chakraborty 	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
3608728a98b8SSucheta Chakraborty 		qlcnic_diag_free_res(adapter->netdev, max_sds_rings);
3609728a98b8SSucheta Chakraborty 
361010ee0faeSSucheta Chakraborty  out:
361110ee0faeSSucheta Chakraborty 	if (!adapter->ahw->beacon_state)
3612728a98b8SSucheta Chakraborty 		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
361310ee0faeSSucheta Chakraborty 	rtnl_unlock();
3614728a98b8SSucheta Chakraborty 
3615728a98b8SSucheta Chakraborty 	return err;
3616728a98b8SSucheta Chakraborty }
3617728a98b8SSucheta Chakraborty 
3618728a98b8SSucheta Chakraborty static ssize_t
3619728a98b8SSucheta Chakraborty qlcnic_show_beacon(struct device *dev,
3620728a98b8SSucheta Chakraborty 		struct device_attribute *attr, char *buf)
3621728a98b8SSucheta Chakraborty {
3622728a98b8SSucheta Chakraborty 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3623728a98b8SSucheta Chakraborty 
3624728a98b8SSucheta Chakraborty 	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
3625728a98b8SSucheta Chakraborty }
3626728a98b8SSucheta Chakraborty 
3627728a98b8SSucheta Chakraborty static struct device_attribute dev_attr_beacon = {
3628728a98b8SSucheta Chakraborty 	.attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
3629728a98b8SSucheta Chakraborty 	.show = qlcnic_show_beacon,
3630728a98b8SSucheta Chakraborty 	.store = qlcnic_store_beacon,
3631728a98b8SSucheta Chakraborty };
3632728a98b8SSucheta Chakraborty 
3633728a98b8SSucheta Chakraborty static int
3634aa43c215SJeff Kirsher qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3635aa43c215SJeff Kirsher 		loff_t offset, size_t size)
3636aa43c215SJeff Kirsher {
3637aa43c215SJeff Kirsher 	size_t crb_size = 4;
3638aa43c215SJeff Kirsher 
3639aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3640aa43c215SJeff Kirsher 		return -EIO;
3641aa43c215SJeff Kirsher 
3642aa43c215SJeff Kirsher 	if (offset < QLCNIC_PCI_CRBSPACE) {
3643aa43c215SJeff Kirsher 		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3644aa43c215SJeff Kirsher 					QLCNIC_PCI_CAMQM_END))
3645aa43c215SJeff Kirsher 			crb_size = 8;
3646aa43c215SJeff Kirsher 		else
3647aa43c215SJeff Kirsher 			return -EINVAL;
3648aa43c215SJeff Kirsher 	}
3649aa43c215SJeff Kirsher 
3650aa43c215SJeff Kirsher 	if ((size != crb_size) || (offset & (crb_size-1)))
3651aa43c215SJeff Kirsher 		return  -EINVAL;
3652aa43c215SJeff Kirsher 
3653aa43c215SJeff Kirsher 	return 0;
3654aa43c215SJeff Kirsher }
3655aa43c215SJeff Kirsher 
3656aa43c215SJeff Kirsher static ssize_t
3657aa43c215SJeff Kirsher qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3658aa43c215SJeff Kirsher 		struct bin_attribute *attr,
3659aa43c215SJeff Kirsher 		char *buf, loff_t offset, size_t size)
3660aa43c215SJeff Kirsher {
3661aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3662aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3663aa43c215SJeff Kirsher 	u32 data;
3664aa43c215SJeff Kirsher 	u64 qmdata;
3665aa43c215SJeff Kirsher 	int ret;
3666aa43c215SJeff Kirsher 
3667aa43c215SJeff Kirsher 	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3668aa43c215SJeff Kirsher 	if (ret != 0)
3669aa43c215SJeff Kirsher 		return ret;
3670aa43c215SJeff Kirsher 
3671aa43c215SJeff Kirsher 	if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3672aa43c215SJeff Kirsher 		qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3673aa43c215SJeff Kirsher 		memcpy(buf, &qmdata, size);
3674aa43c215SJeff Kirsher 	} else {
3675aa43c215SJeff Kirsher 		data = QLCRD32(adapter, offset);
3676aa43c215SJeff Kirsher 		memcpy(buf, &data, size);
3677aa43c215SJeff Kirsher 	}
3678aa43c215SJeff Kirsher 	return size;
3679aa43c215SJeff Kirsher }
3680aa43c215SJeff Kirsher 
3681aa43c215SJeff Kirsher static ssize_t
3682aa43c215SJeff Kirsher qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3683aa43c215SJeff Kirsher 		struct bin_attribute *attr,
3684aa43c215SJeff Kirsher 		char *buf, loff_t offset, size_t size)
3685aa43c215SJeff Kirsher {
3686aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3687aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3688aa43c215SJeff Kirsher 	u32 data;
3689aa43c215SJeff Kirsher 	u64 qmdata;
3690aa43c215SJeff Kirsher 	int ret;
3691aa43c215SJeff Kirsher 
3692aa43c215SJeff Kirsher 	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3693aa43c215SJeff Kirsher 	if (ret != 0)
3694aa43c215SJeff Kirsher 		return ret;
3695aa43c215SJeff Kirsher 
3696aa43c215SJeff Kirsher 	if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3697aa43c215SJeff Kirsher 		memcpy(&qmdata, buf, size);
3698aa43c215SJeff Kirsher 		qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3699aa43c215SJeff Kirsher 	} else {
3700aa43c215SJeff Kirsher 		memcpy(&data, buf, size);
3701aa43c215SJeff Kirsher 		QLCWR32(adapter, offset, data);
3702aa43c215SJeff Kirsher 	}
3703aa43c215SJeff Kirsher 	return size;
3704aa43c215SJeff Kirsher }
3705aa43c215SJeff Kirsher 
3706aa43c215SJeff Kirsher static int
3707aa43c215SJeff Kirsher qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3708aa43c215SJeff Kirsher 		loff_t offset, size_t size)
3709aa43c215SJeff Kirsher {
3710aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3711aa43c215SJeff Kirsher 		return -EIO;
3712aa43c215SJeff Kirsher 
3713aa43c215SJeff Kirsher 	if ((size != 8) || (offset & 0x7))
3714aa43c215SJeff Kirsher 		return  -EIO;
3715aa43c215SJeff Kirsher 
3716aa43c215SJeff Kirsher 	return 0;
3717aa43c215SJeff Kirsher }
3718aa43c215SJeff Kirsher 
3719aa43c215SJeff Kirsher static ssize_t
3720aa43c215SJeff Kirsher qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3721aa43c215SJeff Kirsher 		struct bin_attribute *attr,
3722aa43c215SJeff Kirsher 		char *buf, loff_t offset, size_t size)
3723aa43c215SJeff Kirsher {
3724aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3725aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3726aa43c215SJeff Kirsher 	u64 data;
3727aa43c215SJeff Kirsher 	int ret;
3728aa43c215SJeff Kirsher 
3729aa43c215SJeff Kirsher 	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3730aa43c215SJeff Kirsher 	if (ret != 0)
3731aa43c215SJeff Kirsher 		return ret;
3732aa43c215SJeff Kirsher 
3733aa43c215SJeff Kirsher 	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3734aa43c215SJeff Kirsher 		return -EIO;
3735aa43c215SJeff Kirsher 
3736aa43c215SJeff Kirsher 	memcpy(buf, &data, size);
3737aa43c215SJeff Kirsher 
3738aa43c215SJeff Kirsher 	return size;
3739aa43c215SJeff Kirsher }
3740aa43c215SJeff Kirsher 
3741aa43c215SJeff Kirsher static ssize_t
3742aa43c215SJeff Kirsher qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3743aa43c215SJeff Kirsher 		struct bin_attribute *attr,
3744aa43c215SJeff Kirsher 		char *buf, loff_t offset, size_t size)
3745aa43c215SJeff Kirsher {
3746aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3747aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3748aa43c215SJeff Kirsher 	u64 data;
3749aa43c215SJeff Kirsher 	int ret;
3750aa43c215SJeff Kirsher 
3751aa43c215SJeff Kirsher 	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3752aa43c215SJeff Kirsher 	if (ret != 0)
3753aa43c215SJeff Kirsher 		return ret;
3754aa43c215SJeff Kirsher 
3755aa43c215SJeff Kirsher 	memcpy(&data, buf, size);
3756aa43c215SJeff Kirsher 
3757aa43c215SJeff Kirsher 	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3758aa43c215SJeff Kirsher 		return -EIO;
3759aa43c215SJeff Kirsher 
3760aa43c215SJeff Kirsher 	return size;
3761aa43c215SJeff Kirsher }
3762aa43c215SJeff Kirsher 
3763aa43c215SJeff Kirsher static struct bin_attribute bin_attr_crb = {
3764aa43c215SJeff Kirsher 	.attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3765aa43c215SJeff Kirsher 	.size = 0,
3766aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_crb,
3767aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_write_crb,
3768aa43c215SJeff Kirsher };
3769aa43c215SJeff Kirsher 
3770aa43c215SJeff Kirsher static struct bin_attribute bin_attr_mem = {
3771aa43c215SJeff Kirsher 	.attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3772aa43c215SJeff Kirsher 	.size = 0,
3773aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_mem,
3774aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_write_mem,
3775aa43c215SJeff Kirsher };
3776aa43c215SJeff Kirsher 
3777aa43c215SJeff Kirsher static int
3778aa43c215SJeff Kirsher validate_pm_config(struct qlcnic_adapter *adapter,
3779aa43c215SJeff Kirsher 			struct qlcnic_pm_func_cfg *pm_cfg, int count)
3780aa43c215SJeff Kirsher {
3781aa43c215SJeff Kirsher 
3782aa43c215SJeff Kirsher 	u8 src_pci_func, s_esw_id, d_esw_id;
3783aa43c215SJeff Kirsher 	u8 dest_pci_func;
3784aa43c215SJeff Kirsher 	int i;
3785aa43c215SJeff Kirsher 
3786aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3787aa43c215SJeff Kirsher 		src_pci_func = pm_cfg[i].pci_func;
3788aa43c215SJeff Kirsher 		dest_pci_func = pm_cfg[i].dest_npar;
3789aa43c215SJeff Kirsher 		if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3790aa43c215SJeff Kirsher 				|| dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3791aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3792aa43c215SJeff Kirsher 
3793aa43c215SJeff Kirsher 		if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3794aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3795aa43c215SJeff Kirsher 
3796aa43c215SJeff Kirsher 		if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3797aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3798aa43c215SJeff Kirsher 
3799aa43c215SJeff Kirsher 		s_esw_id = adapter->npars[src_pci_func].phy_port;
3800aa43c215SJeff Kirsher 		d_esw_id = adapter->npars[dest_pci_func].phy_port;
3801aa43c215SJeff Kirsher 
3802aa43c215SJeff Kirsher 		if (s_esw_id != d_esw_id)
3803aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3804aa43c215SJeff Kirsher 
3805aa43c215SJeff Kirsher 	}
3806aa43c215SJeff Kirsher 	return 0;
3807aa43c215SJeff Kirsher 
3808aa43c215SJeff Kirsher }
3809aa43c215SJeff Kirsher 
3810aa43c215SJeff Kirsher static ssize_t
3811aa43c215SJeff Kirsher qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3812aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3813aa43c215SJeff Kirsher {
3814aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3815aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3816aa43c215SJeff Kirsher 	struct qlcnic_pm_func_cfg *pm_cfg;
3817aa43c215SJeff Kirsher 	u32 id, action, pci_func;
3818aa43c215SJeff Kirsher 	int count, rem, i, ret;
3819aa43c215SJeff Kirsher 
3820aa43c215SJeff Kirsher 	count	= size / sizeof(struct qlcnic_pm_func_cfg);
3821aa43c215SJeff Kirsher 	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
3822aa43c215SJeff Kirsher 	if (rem)
3823aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
3824aa43c215SJeff Kirsher 
3825aa43c215SJeff Kirsher 	pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3826aa43c215SJeff Kirsher 
3827aa43c215SJeff Kirsher 	ret = validate_pm_config(adapter, pm_cfg, count);
3828aa43c215SJeff Kirsher 	if (ret)
3829aa43c215SJeff Kirsher 		return ret;
3830aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3831aa43c215SJeff Kirsher 		pci_func = pm_cfg[i].pci_func;
3832aa43c215SJeff Kirsher 		action = !!pm_cfg[i].action;
3833aa43c215SJeff Kirsher 		id = adapter->npars[pci_func].phy_port;
3834aa43c215SJeff Kirsher 		ret = qlcnic_config_port_mirroring(adapter, id,
3835aa43c215SJeff Kirsher 						action, pci_func);
3836aa43c215SJeff Kirsher 		if (ret)
3837aa43c215SJeff Kirsher 			return ret;
3838aa43c215SJeff Kirsher 	}
3839aa43c215SJeff Kirsher 
3840aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3841aa43c215SJeff Kirsher 		pci_func = pm_cfg[i].pci_func;
3842aa43c215SJeff Kirsher 		id = adapter->npars[pci_func].phy_port;
3843aa43c215SJeff Kirsher 		adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
3844aa43c215SJeff Kirsher 		adapter->npars[pci_func].dest_npar = id;
3845aa43c215SJeff Kirsher 	}
3846aa43c215SJeff Kirsher 	return size;
3847aa43c215SJeff Kirsher }
3848aa43c215SJeff Kirsher 
3849aa43c215SJeff Kirsher static ssize_t
3850aa43c215SJeff Kirsher qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3851aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3852aa43c215SJeff Kirsher {
3853aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3854aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3855aa43c215SJeff Kirsher 	struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3856aa43c215SJeff Kirsher 	int i;
3857aa43c215SJeff Kirsher 
3858aa43c215SJeff Kirsher 	if (size != sizeof(pm_cfg))
3859aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
3860aa43c215SJeff Kirsher 
3861aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3862aa43c215SJeff Kirsher 		if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3863aa43c215SJeff Kirsher 			continue;
3864aa43c215SJeff Kirsher 		pm_cfg[i].action = adapter->npars[i].enable_pm;
3865aa43c215SJeff Kirsher 		pm_cfg[i].dest_npar = 0;
3866aa43c215SJeff Kirsher 		pm_cfg[i].pci_func = i;
3867aa43c215SJeff Kirsher 	}
3868aa43c215SJeff Kirsher 	memcpy(buf, &pm_cfg, size);
3869aa43c215SJeff Kirsher 
3870aa43c215SJeff Kirsher 	return size;
3871aa43c215SJeff Kirsher }
3872aa43c215SJeff Kirsher 
3873aa43c215SJeff Kirsher static int
3874aa43c215SJeff Kirsher validate_esw_config(struct qlcnic_adapter *adapter,
3875aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg *esw_cfg, int count)
3876aa43c215SJeff Kirsher {
3877aa43c215SJeff Kirsher 	u32 op_mode;
3878aa43c215SJeff Kirsher 	u8 pci_func;
3879aa43c215SJeff Kirsher 	int i;
3880aa43c215SJeff Kirsher 
3881aa43c215SJeff Kirsher 	op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
3882aa43c215SJeff Kirsher 
3883aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3884aa43c215SJeff Kirsher 		pci_func = esw_cfg[i].pci_func;
3885aa43c215SJeff Kirsher 		if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3886aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3887aa43c215SJeff Kirsher 
3888aa43c215SJeff Kirsher 		if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3889aa43c215SJeff Kirsher 			if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3890aa43c215SJeff Kirsher 				return QL_STATUS_INVALID_PARAM;
3891aa43c215SJeff Kirsher 
3892aa43c215SJeff Kirsher 		switch (esw_cfg[i].op_mode) {
3893aa43c215SJeff Kirsher 		case QLCNIC_PORT_DEFAULTS:
3894aa43c215SJeff Kirsher 			if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
3895aa43c215SJeff Kirsher 						QLCNIC_NON_PRIV_FUNC) {
3896aa43c215SJeff Kirsher 				if (esw_cfg[i].mac_anti_spoof != 0)
3897aa43c215SJeff Kirsher 					return QL_STATUS_INVALID_PARAM;
3898aa43c215SJeff Kirsher 				if (esw_cfg[i].mac_override != 1)
3899aa43c215SJeff Kirsher 					return QL_STATUS_INVALID_PARAM;
3900aa43c215SJeff Kirsher 				if (esw_cfg[i].promisc_mode != 1)
3901aa43c215SJeff Kirsher 					return QL_STATUS_INVALID_PARAM;
3902aa43c215SJeff Kirsher 			}
3903aa43c215SJeff Kirsher 			break;
3904aa43c215SJeff Kirsher 		case QLCNIC_ADD_VLAN:
3905aa43c215SJeff Kirsher 			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3906aa43c215SJeff Kirsher 				return QL_STATUS_INVALID_PARAM;
3907aa43c215SJeff Kirsher 			if (!esw_cfg[i].op_type)
3908aa43c215SJeff Kirsher 				return QL_STATUS_INVALID_PARAM;
3909aa43c215SJeff Kirsher 			break;
3910aa43c215SJeff Kirsher 		case QLCNIC_DEL_VLAN:
3911aa43c215SJeff Kirsher 			if (!esw_cfg[i].op_type)
3912aa43c215SJeff Kirsher 				return QL_STATUS_INVALID_PARAM;
3913aa43c215SJeff Kirsher 			break;
3914aa43c215SJeff Kirsher 		default:
3915aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
3916aa43c215SJeff Kirsher 		}
3917aa43c215SJeff Kirsher 	}
3918aa43c215SJeff Kirsher 	return 0;
3919aa43c215SJeff Kirsher }
3920aa43c215SJeff Kirsher 
3921aa43c215SJeff Kirsher static ssize_t
3922aa43c215SJeff Kirsher qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3923aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3924aa43c215SJeff Kirsher {
3925aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
3926aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3927aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg *esw_cfg;
3928aa43c215SJeff Kirsher 	struct qlcnic_npar_info *npar;
3929aa43c215SJeff Kirsher 	int count, rem, i, ret;
3930aa43c215SJeff Kirsher 	u8 pci_func, op_mode = 0;
3931aa43c215SJeff Kirsher 
3932aa43c215SJeff Kirsher 	count	= size / sizeof(struct qlcnic_esw_func_cfg);
3933aa43c215SJeff Kirsher 	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
3934aa43c215SJeff Kirsher 	if (rem)
3935aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
3936aa43c215SJeff Kirsher 
3937aa43c215SJeff Kirsher 	esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3938aa43c215SJeff Kirsher 	ret = validate_esw_config(adapter, esw_cfg, count);
3939aa43c215SJeff Kirsher 	if (ret)
3940aa43c215SJeff Kirsher 		return ret;
3941aa43c215SJeff Kirsher 
3942aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3943aa43c215SJeff Kirsher 		if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3944aa43c215SJeff Kirsher 			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3945aa43c215SJeff Kirsher 				return QL_STATUS_INVALID_PARAM;
3946aa43c215SJeff Kirsher 
3947aa43c215SJeff Kirsher 		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
3948aa43c215SJeff Kirsher 			continue;
3949aa43c215SJeff Kirsher 
3950aa43c215SJeff Kirsher 		op_mode = esw_cfg[i].op_mode;
3951aa43c215SJeff Kirsher 		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3952aa43c215SJeff Kirsher 		esw_cfg[i].op_mode = op_mode;
3953aa43c215SJeff Kirsher 		esw_cfg[i].pci_func = adapter->ahw->pci_func;
3954aa43c215SJeff Kirsher 
3955aa43c215SJeff Kirsher 		switch (esw_cfg[i].op_mode) {
3956aa43c215SJeff Kirsher 		case QLCNIC_PORT_DEFAULTS:
3957aa43c215SJeff Kirsher 			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3958aa43c215SJeff Kirsher 			break;
3959aa43c215SJeff Kirsher 		case QLCNIC_ADD_VLAN:
3960aa43c215SJeff Kirsher 			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3961aa43c215SJeff Kirsher 			break;
3962aa43c215SJeff Kirsher 		case QLCNIC_DEL_VLAN:
3963aa43c215SJeff Kirsher 			esw_cfg[i].vlan_id = 0;
3964aa43c215SJeff Kirsher 			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3965aa43c215SJeff Kirsher 			break;
3966aa43c215SJeff Kirsher 		}
3967aa43c215SJeff Kirsher 	}
3968aa43c215SJeff Kirsher 
3969aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3970aa43c215SJeff Kirsher 		goto out;
3971aa43c215SJeff Kirsher 
3972aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
3973aa43c215SJeff Kirsher 		pci_func = esw_cfg[i].pci_func;
3974aa43c215SJeff Kirsher 		npar = &adapter->npars[pci_func];
3975aa43c215SJeff Kirsher 		switch (esw_cfg[i].op_mode) {
3976aa43c215SJeff Kirsher 		case QLCNIC_PORT_DEFAULTS:
3977aa43c215SJeff Kirsher 			npar->promisc_mode = esw_cfg[i].promisc_mode;
3978aa43c215SJeff Kirsher 			npar->mac_override = esw_cfg[i].mac_override;
3979aa43c215SJeff Kirsher 			npar->offload_flags = esw_cfg[i].offload_flags;
3980aa43c215SJeff Kirsher 			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3981aa43c215SJeff Kirsher 			npar->discard_tagged = esw_cfg[i].discard_tagged;
3982aa43c215SJeff Kirsher 			break;
3983aa43c215SJeff Kirsher 		case QLCNIC_ADD_VLAN:
3984aa43c215SJeff Kirsher 			npar->pvid = esw_cfg[i].vlan_id;
3985aa43c215SJeff Kirsher 			break;
3986aa43c215SJeff Kirsher 		case QLCNIC_DEL_VLAN:
3987aa43c215SJeff Kirsher 			npar->pvid = 0;
3988aa43c215SJeff Kirsher 			break;
3989aa43c215SJeff Kirsher 		}
3990aa43c215SJeff Kirsher 	}
3991aa43c215SJeff Kirsher out:
3992aa43c215SJeff Kirsher 	return size;
3993aa43c215SJeff Kirsher }
3994aa43c215SJeff Kirsher 
3995aa43c215SJeff Kirsher static ssize_t
3996aa43c215SJeff Kirsher qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3997aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3998aa43c215SJeff Kirsher {
3999aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4000aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4001aa43c215SJeff Kirsher 	struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
4002aa43c215SJeff Kirsher 	u8 i;
4003aa43c215SJeff Kirsher 
4004aa43c215SJeff Kirsher 	if (size != sizeof(esw_cfg))
4005aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4006aa43c215SJeff Kirsher 
4007aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
4008aa43c215SJeff Kirsher 		if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4009aa43c215SJeff Kirsher 			continue;
4010aa43c215SJeff Kirsher 		esw_cfg[i].pci_func = i;
4011aa43c215SJeff Kirsher 		if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
4012aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
4013aa43c215SJeff Kirsher 	}
4014aa43c215SJeff Kirsher 	memcpy(buf, &esw_cfg, size);
4015aa43c215SJeff Kirsher 
4016aa43c215SJeff Kirsher 	return size;
4017aa43c215SJeff Kirsher }
4018aa43c215SJeff Kirsher 
4019aa43c215SJeff Kirsher static int
4020aa43c215SJeff Kirsher validate_npar_config(struct qlcnic_adapter *adapter,
4021aa43c215SJeff Kirsher 				struct qlcnic_npar_func_cfg *np_cfg, int count)
4022aa43c215SJeff Kirsher {
4023aa43c215SJeff Kirsher 	u8 pci_func, i;
4024aa43c215SJeff Kirsher 
4025aa43c215SJeff Kirsher 	for (i = 0; i < count; i++) {
4026aa43c215SJeff Kirsher 		pci_func = np_cfg[i].pci_func;
4027aa43c215SJeff Kirsher 		if (pci_func >= QLCNIC_MAX_PCI_FUNC)
4028aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
4029aa43c215SJeff Kirsher 
4030aa43c215SJeff Kirsher 		if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
4031aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
4032aa43c215SJeff Kirsher 
4033aa43c215SJeff Kirsher 		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
4034aa43c215SJeff Kirsher 		    !IS_VALID_BW(np_cfg[i].max_bw))
4035aa43c215SJeff Kirsher 			return QL_STATUS_INVALID_PARAM;
4036aa43c215SJeff Kirsher 	}
4037aa43c215SJeff Kirsher 	return 0;
4038aa43c215SJeff Kirsher }
4039aa43c215SJeff Kirsher 
4040aa43c215SJeff Kirsher static ssize_t
4041aa43c215SJeff Kirsher qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
4042aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4043aa43c215SJeff Kirsher {
4044aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4045aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4046aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
4047aa43c215SJeff Kirsher 	struct qlcnic_npar_func_cfg *np_cfg;
4048aa43c215SJeff Kirsher 	int i, count, rem, ret;
4049aa43c215SJeff Kirsher 	u8 pci_func;
4050aa43c215SJeff Kirsher 
4051aa43c215SJeff Kirsher 	count	= size / sizeof(struct qlcnic_npar_func_cfg);
4052aa43c215SJeff Kirsher 	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
4053aa43c215SJeff Kirsher 	if (rem)
4054aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4055aa43c215SJeff Kirsher 
4056aa43c215SJeff Kirsher 	np_cfg = (struct qlcnic_npar_func_cfg *) buf;
4057aa43c215SJeff Kirsher 	ret = validate_npar_config(adapter, np_cfg, count);
4058aa43c215SJeff Kirsher 	if (ret)
4059aa43c215SJeff Kirsher 		return ret;
4060aa43c215SJeff Kirsher 
4061aa43c215SJeff Kirsher 	for (i = 0; i < count ; i++) {
4062aa43c215SJeff Kirsher 		pci_func = np_cfg[i].pci_func;
4063aa43c215SJeff Kirsher 		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
4064aa43c215SJeff Kirsher 		if (ret)
4065aa43c215SJeff Kirsher 			return ret;
4066aa43c215SJeff Kirsher 		nic_info.pci_func = pci_func;
4067aa43c215SJeff Kirsher 		nic_info.min_tx_bw = np_cfg[i].min_bw;
4068aa43c215SJeff Kirsher 		nic_info.max_tx_bw = np_cfg[i].max_bw;
4069aa43c215SJeff Kirsher 		ret = qlcnic_set_nic_info(adapter, &nic_info);
4070aa43c215SJeff Kirsher 		if (ret)
4071aa43c215SJeff Kirsher 			return ret;
4072aa43c215SJeff Kirsher 		adapter->npars[i].min_bw = nic_info.min_tx_bw;
4073aa43c215SJeff Kirsher 		adapter->npars[i].max_bw = nic_info.max_tx_bw;
4074aa43c215SJeff Kirsher 	}
4075aa43c215SJeff Kirsher 
4076aa43c215SJeff Kirsher 	return size;
4077aa43c215SJeff Kirsher 
4078aa43c215SJeff Kirsher }
4079aa43c215SJeff Kirsher static ssize_t
4080aa43c215SJeff Kirsher qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
4081aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4082aa43c215SJeff Kirsher {
4083aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4084aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4085aa43c215SJeff Kirsher 	struct qlcnic_info nic_info;
4086aa43c215SJeff Kirsher 	struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
4087aa43c215SJeff Kirsher 	int i, ret;
4088aa43c215SJeff Kirsher 
4089aa43c215SJeff Kirsher 	if (size != sizeof(np_cfg))
4090aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4091aa43c215SJeff Kirsher 
4092aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4093aa43c215SJeff Kirsher 		if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
4094aa43c215SJeff Kirsher 			continue;
4095aa43c215SJeff Kirsher 		ret = qlcnic_get_nic_info(adapter, &nic_info, i);
4096aa43c215SJeff Kirsher 		if (ret)
4097aa43c215SJeff Kirsher 			return ret;
4098aa43c215SJeff Kirsher 
4099aa43c215SJeff Kirsher 		np_cfg[i].pci_func = i;
4100aa43c215SJeff Kirsher 		np_cfg[i].op_mode = (u8)nic_info.op_mode;
4101aa43c215SJeff Kirsher 		np_cfg[i].port_num = nic_info.phys_port;
4102aa43c215SJeff Kirsher 		np_cfg[i].fw_capab = nic_info.capabilities;
4103aa43c215SJeff Kirsher 		np_cfg[i].min_bw = nic_info.min_tx_bw ;
4104aa43c215SJeff Kirsher 		np_cfg[i].max_bw = nic_info.max_tx_bw;
4105aa43c215SJeff Kirsher 		np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
4106aa43c215SJeff Kirsher 		np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
4107aa43c215SJeff Kirsher 	}
4108aa43c215SJeff Kirsher 	memcpy(buf, &np_cfg, size);
4109aa43c215SJeff Kirsher 	return size;
4110aa43c215SJeff Kirsher }
4111aa43c215SJeff Kirsher 
4112aa43c215SJeff Kirsher static ssize_t
4113aa43c215SJeff Kirsher qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
4114aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4115aa43c215SJeff Kirsher {
4116aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4117aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4118aa43c215SJeff Kirsher 	struct qlcnic_esw_statistics port_stats;
4119aa43c215SJeff Kirsher 	int ret;
4120aa43c215SJeff Kirsher 
4121aa43c215SJeff Kirsher 	if (size != sizeof(struct qlcnic_esw_statistics))
4122aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4123aa43c215SJeff Kirsher 
4124aa43c215SJeff Kirsher 	if (offset >= QLCNIC_MAX_PCI_FUNC)
4125aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4126aa43c215SJeff Kirsher 
4127aa43c215SJeff Kirsher 	memset(&port_stats, 0, size);
4128aa43c215SJeff Kirsher 	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4129aa43c215SJeff Kirsher 								&port_stats.rx);
4130aa43c215SJeff Kirsher 	if (ret)
4131aa43c215SJeff Kirsher 		return ret;
4132aa43c215SJeff Kirsher 
4133aa43c215SJeff Kirsher 	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4134aa43c215SJeff Kirsher 								&port_stats.tx);
4135aa43c215SJeff Kirsher 	if (ret)
4136aa43c215SJeff Kirsher 		return ret;
4137aa43c215SJeff Kirsher 
4138aa43c215SJeff Kirsher 	memcpy(buf, &port_stats, size);
4139aa43c215SJeff Kirsher 	return size;
4140aa43c215SJeff Kirsher }
4141aa43c215SJeff Kirsher 
4142aa43c215SJeff Kirsher static ssize_t
4143aa43c215SJeff Kirsher qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
4144aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4145aa43c215SJeff Kirsher {
4146aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4147aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4148aa43c215SJeff Kirsher 	struct qlcnic_esw_statistics esw_stats;
4149aa43c215SJeff Kirsher 	int ret;
4150aa43c215SJeff Kirsher 
4151aa43c215SJeff Kirsher 	if (size != sizeof(struct qlcnic_esw_statistics))
4152aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4153aa43c215SJeff Kirsher 
4154aa43c215SJeff Kirsher 	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4155aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4156aa43c215SJeff Kirsher 
4157aa43c215SJeff Kirsher 	memset(&esw_stats, 0, size);
4158aa43c215SJeff Kirsher 	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
4159aa43c215SJeff Kirsher 								&esw_stats.rx);
4160aa43c215SJeff Kirsher 	if (ret)
4161aa43c215SJeff Kirsher 		return ret;
4162aa43c215SJeff Kirsher 
4163aa43c215SJeff Kirsher 	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
4164aa43c215SJeff Kirsher 								&esw_stats.tx);
4165aa43c215SJeff Kirsher 	if (ret)
4166aa43c215SJeff Kirsher 		return ret;
4167aa43c215SJeff Kirsher 
4168aa43c215SJeff Kirsher 	memcpy(buf, &esw_stats, size);
4169aa43c215SJeff Kirsher 	return size;
4170aa43c215SJeff Kirsher }
4171aa43c215SJeff Kirsher 
4172aa43c215SJeff Kirsher static ssize_t
4173aa43c215SJeff Kirsher qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
4174aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4175aa43c215SJeff Kirsher {
4176aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4177aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4178aa43c215SJeff Kirsher 	int ret;
4179aa43c215SJeff Kirsher 
4180aa43c215SJeff Kirsher 	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
4181aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4182aa43c215SJeff Kirsher 
4183aa43c215SJeff Kirsher 	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4184aa43c215SJeff Kirsher 						QLCNIC_QUERY_RX_COUNTER);
4185aa43c215SJeff Kirsher 	if (ret)
4186aa43c215SJeff Kirsher 		return ret;
4187aa43c215SJeff Kirsher 
4188aa43c215SJeff Kirsher 	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
4189aa43c215SJeff Kirsher 						QLCNIC_QUERY_TX_COUNTER);
4190aa43c215SJeff Kirsher 	if (ret)
4191aa43c215SJeff Kirsher 		return ret;
4192aa43c215SJeff Kirsher 
4193aa43c215SJeff Kirsher 	return size;
4194aa43c215SJeff Kirsher }
4195aa43c215SJeff Kirsher 
4196aa43c215SJeff Kirsher static ssize_t
4197aa43c215SJeff Kirsher qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
4198aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4199aa43c215SJeff Kirsher {
4200aa43c215SJeff Kirsher 
4201aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4202aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4203aa43c215SJeff Kirsher 	int ret;
4204aa43c215SJeff Kirsher 
4205aa43c215SJeff Kirsher 	if (offset >= QLCNIC_MAX_PCI_FUNC)
4206aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4207aa43c215SJeff Kirsher 
4208aa43c215SJeff Kirsher 	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4209aa43c215SJeff Kirsher 						QLCNIC_QUERY_RX_COUNTER);
4210aa43c215SJeff Kirsher 	if (ret)
4211aa43c215SJeff Kirsher 		return ret;
4212aa43c215SJeff Kirsher 
4213aa43c215SJeff Kirsher 	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
4214aa43c215SJeff Kirsher 						QLCNIC_QUERY_TX_COUNTER);
4215aa43c215SJeff Kirsher 	if (ret)
4216aa43c215SJeff Kirsher 		return ret;
4217aa43c215SJeff Kirsher 
4218aa43c215SJeff Kirsher 	return size;
4219aa43c215SJeff Kirsher }
4220aa43c215SJeff Kirsher 
4221aa43c215SJeff Kirsher static ssize_t
4222aa43c215SJeff Kirsher qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
4223aa43c215SJeff Kirsher 	struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
4224aa43c215SJeff Kirsher {
4225aa43c215SJeff Kirsher 	struct device *dev = container_of(kobj, struct device, kobj);
4226aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
4227aa43c215SJeff Kirsher 	struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
4228aa43c215SJeff Kirsher 	struct qlcnic_pci_info *pci_info;
4229aa43c215SJeff Kirsher 	int i, ret;
4230aa43c215SJeff Kirsher 
4231aa43c215SJeff Kirsher 	if (size != sizeof(pci_cfg))
4232aa43c215SJeff Kirsher 		return QL_STATUS_INVALID_PARAM;
4233aa43c215SJeff Kirsher 
4234aa43c215SJeff Kirsher 	pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
4235aa43c215SJeff Kirsher 	if (!pci_info)
4236aa43c215SJeff Kirsher 		return -ENOMEM;
4237aa43c215SJeff Kirsher 
4238aa43c215SJeff Kirsher 	ret = qlcnic_get_pci_info(adapter, pci_info);
4239aa43c215SJeff Kirsher 	if (ret) {
4240aa43c215SJeff Kirsher 		kfree(pci_info);
4241aa43c215SJeff Kirsher 		return ret;
4242aa43c215SJeff Kirsher 	}
4243aa43c215SJeff Kirsher 
4244aa43c215SJeff Kirsher 	for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
4245aa43c215SJeff Kirsher 		pci_cfg[i].pci_func = pci_info[i].id;
4246aa43c215SJeff Kirsher 		pci_cfg[i].func_type = pci_info[i].type;
4247aa43c215SJeff Kirsher 		pci_cfg[i].port_num = pci_info[i].default_port;
4248aa43c215SJeff Kirsher 		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
4249aa43c215SJeff Kirsher 		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
4250aa43c215SJeff Kirsher 		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
4251aa43c215SJeff Kirsher 	}
4252aa43c215SJeff Kirsher 	memcpy(buf, &pci_cfg, size);
4253aa43c215SJeff Kirsher 	kfree(pci_info);
4254aa43c215SJeff Kirsher 	return size;
4255aa43c215SJeff Kirsher }
4256aa43c215SJeff Kirsher static struct bin_attribute bin_attr_npar_config = {
4257aa43c215SJeff Kirsher 	.attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
4258aa43c215SJeff Kirsher 	.size = 0,
4259aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_npar_config,
4260aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_write_npar_config,
4261aa43c215SJeff Kirsher };
4262aa43c215SJeff Kirsher 
4263aa43c215SJeff Kirsher static struct bin_attribute bin_attr_pci_config = {
4264aa43c215SJeff Kirsher 	.attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
4265aa43c215SJeff Kirsher 	.size = 0,
4266aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_pci_config,
4267aa43c215SJeff Kirsher 	.write = NULL,
4268aa43c215SJeff Kirsher };
4269aa43c215SJeff Kirsher 
4270aa43c215SJeff Kirsher static struct bin_attribute bin_attr_port_stats = {
4271aa43c215SJeff Kirsher 	.attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
4272aa43c215SJeff Kirsher 	.size = 0,
4273aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_get_port_stats,
4274aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_clear_port_stats,
4275aa43c215SJeff Kirsher };
4276aa43c215SJeff Kirsher 
4277aa43c215SJeff Kirsher static struct bin_attribute bin_attr_esw_stats = {
4278aa43c215SJeff Kirsher 	.attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
4279aa43c215SJeff Kirsher 	.size = 0,
4280aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_get_esw_stats,
4281aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_clear_esw_stats,
4282aa43c215SJeff Kirsher };
4283aa43c215SJeff Kirsher 
4284aa43c215SJeff Kirsher static struct bin_attribute bin_attr_esw_config = {
4285aa43c215SJeff Kirsher 	.attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
4286aa43c215SJeff Kirsher 	.size = 0,
4287aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_esw_config,
4288aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_write_esw_config,
4289aa43c215SJeff Kirsher };
4290aa43c215SJeff Kirsher 
4291aa43c215SJeff Kirsher static struct bin_attribute bin_attr_pm_config = {
4292aa43c215SJeff Kirsher 	.attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
4293aa43c215SJeff Kirsher 	.size = 0,
4294aa43c215SJeff Kirsher 	.read = qlcnic_sysfs_read_pm_config,
4295aa43c215SJeff Kirsher 	.write = qlcnic_sysfs_write_pm_config,
4296aa43c215SJeff Kirsher };
4297aa43c215SJeff Kirsher 
4298aa43c215SJeff Kirsher static void
4299aa43c215SJeff Kirsher qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
4300aa43c215SJeff Kirsher {
4301aa43c215SJeff Kirsher 	struct device *dev = &adapter->pdev->dev;
4302aa43c215SJeff Kirsher 
4303aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4304aa43c215SJeff Kirsher 		if (device_create_file(dev, &dev_attr_bridged_mode))
4305aa43c215SJeff Kirsher 			dev_warn(dev,
4306aa43c215SJeff Kirsher 				"failed to create bridged_mode sysfs entry\n");
4307aa43c215SJeff Kirsher }
4308aa43c215SJeff Kirsher 
4309aa43c215SJeff Kirsher static void
4310aa43c215SJeff Kirsher qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
4311aa43c215SJeff Kirsher {
4312aa43c215SJeff Kirsher 	struct device *dev = &adapter->pdev->dev;
4313aa43c215SJeff Kirsher 
4314aa43c215SJeff Kirsher 	if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
4315aa43c215SJeff Kirsher 		device_remove_file(dev, &dev_attr_bridged_mode);
4316aa43c215SJeff Kirsher }
4317aa43c215SJeff Kirsher 
4318aa43c215SJeff Kirsher static void
4319aa43c215SJeff Kirsher qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
4320aa43c215SJeff Kirsher {
4321aa43c215SJeff Kirsher 	struct device *dev = &adapter->pdev->dev;
4322b43e5ee7SSucheta Chakraborty 	u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
4323aa43c215SJeff Kirsher 
4324aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_port_stats))
4325aa43c215SJeff Kirsher 		dev_info(dev, "failed to create port stats sysfs entry");
4326aa43c215SJeff Kirsher 
4327aa43c215SJeff Kirsher 	if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4328aa43c215SJeff Kirsher 		return;
4329aa43c215SJeff Kirsher 	if (device_create_file(dev, &dev_attr_diag_mode))
4330aa43c215SJeff Kirsher 		dev_info(dev, "failed to create diag_mode sysfs entry\n");
4331aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_crb))
4332aa43c215SJeff Kirsher 		dev_info(dev, "failed to create crb sysfs entry\n");
4333aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_mem))
4334aa43c215SJeff Kirsher 		dev_info(dev, "failed to create mem sysfs entry\n");
4335b43e5ee7SSucheta Chakraborty 
4336b43e5ee7SSucheta Chakraborty 	if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4337b43e5ee7SSucheta Chakraborty 		return;
4338b43e5ee7SSucheta Chakraborty 
4339aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_pci_config))
4340aa43c215SJeff Kirsher 		dev_info(dev, "failed to create pci config sysfs entry");
4341b43e5ee7SSucheta Chakraborty 	if (device_create_file(dev, &dev_attr_beacon))
4342b43e5ee7SSucheta Chakraborty 		dev_info(dev, "failed to create beacon sysfs entry");
4343b43e5ee7SSucheta Chakraborty 
4344aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4345aa43c215SJeff Kirsher 		return;
4346aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_esw_config))
4347aa43c215SJeff Kirsher 		dev_info(dev, "failed to create esw config sysfs entry");
4348aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4349aa43c215SJeff Kirsher 		return;
4350aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_npar_config))
4351aa43c215SJeff Kirsher 		dev_info(dev, "failed to create npar config sysfs entry");
4352aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_pm_config))
4353aa43c215SJeff Kirsher 		dev_info(dev, "failed to create pm config sysfs entry");
4354aa43c215SJeff Kirsher 	if (device_create_bin_file(dev, &bin_attr_esw_stats))
4355aa43c215SJeff Kirsher 		dev_info(dev, "failed to create eswitch stats sysfs entry");
4356aa43c215SJeff Kirsher }
4357aa43c215SJeff Kirsher 
4358aa43c215SJeff Kirsher static void
4359aa43c215SJeff Kirsher qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4360aa43c215SJeff Kirsher {
4361aa43c215SJeff Kirsher 	struct device *dev = &adapter->pdev->dev;
4362b43e5ee7SSucheta Chakraborty 	u32 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
4363aa43c215SJeff Kirsher 
4364aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_port_stats);
4365aa43c215SJeff Kirsher 
4366aa43c215SJeff Kirsher 	if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4367aa43c215SJeff Kirsher 		return;
4368aa43c215SJeff Kirsher 	device_remove_file(dev, &dev_attr_diag_mode);
4369aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_crb);
4370aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_mem);
4371b43e5ee7SSucheta Chakraborty 	if (state == QLCNIC_DEV_FAILED || (state == QLCNIC_DEV_BADBAD))
4372b43e5ee7SSucheta Chakraborty 		return;
4373aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_pci_config);
4374b43e5ee7SSucheta Chakraborty 	device_remove_file(dev, &dev_attr_beacon);
4375aa43c215SJeff Kirsher 	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4376aa43c215SJeff Kirsher 		return;
4377aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_esw_config);
4378aa43c215SJeff Kirsher 	if (adapter->op_mode != QLCNIC_MGMT_FUNC)
4379aa43c215SJeff Kirsher 		return;
4380aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_npar_config);
4381aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_pm_config);
4382aa43c215SJeff Kirsher 	device_remove_bin_file(dev, &bin_attr_esw_stats);
4383aa43c215SJeff Kirsher }
4384aa43c215SJeff Kirsher 
4385aa43c215SJeff Kirsher #ifdef CONFIG_INET
4386aa43c215SJeff Kirsher 
4387aa43c215SJeff Kirsher #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4388aa43c215SJeff Kirsher 
4389aa43c215SJeff Kirsher static void
4390aa43c215SJeff Kirsher qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4391aa43c215SJeff Kirsher 			struct net_device *dev, unsigned long event)
4392aa43c215SJeff Kirsher {
4393aa43c215SJeff Kirsher 	struct in_device *indev;
4394aa43c215SJeff Kirsher 
4395aa43c215SJeff Kirsher 	indev = in_dev_get(dev);
4396aa43c215SJeff Kirsher 	if (!indev)
4397aa43c215SJeff Kirsher 		return;
4398aa43c215SJeff Kirsher 
4399aa43c215SJeff Kirsher 	for_ifa(indev) {
4400aa43c215SJeff Kirsher 		switch (event) {
4401aa43c215SJeff Kirsher 		case NETDEV_UP:
4402aa43c215SJeff Kirsher 			qlcnic_config_ipaddr(adapter,
4403aa43c215SJeff Kirsher 					ifa->ifa_address, QLCNIC_IP_UP);
4404aa43c215SJeff Kirsher 			break;
4405aa43c215SJeff Kirsher 		case NETDEV_DOWN:
4406aa43c215SJeff Kirsher 			qlcnic_config_ipaddr(adapter,
4407aa43c215SJeff Kirsher 					ifa->ifa_address, QLCNIC_IP_DOWN);
4408aa43c215SJeff Kirsher 			break;
4409aa43c215SJeff Kirsher 		default:
4410aa43c215SJeff Kirsher 			break;
4411aa43c215SJeff Kirsher 		}
4412aa43c215SJeff Kirsher 	} endfor_ifa(indev);
4413aa43c215SJeff Kirsher 
4414aa43c215SJeff Kirsher 	in_dev_put(indev);
4415aa43c215SJeff Kirsher }
4416aa43c215SJeff Kirsher 
4417aa43c215SJeff Kirsher static void
4418aa43c215SJeff Kirsher qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4419aa43c215SJeff Kirsher {
4420aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
4421aa43c215SJeff Kirsher 	struct net_device *dev;
4422aa43c215SJeff Kirsher 	u16 vid;
4423aa43c215SJeff Kirsher 
4424aa43c215SJeff Kirsher 	qlcnic_config_indev_addr(adapter, netdev, event);
4425aa43c215SJeff Kirsher 
4426aa43c215SJeff Kirsher 	for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4427aa43c215SJeff Kirsher 		dev = __vlan_find_dev_deep(netdev, vid);
4428aa43c215SJeff Kirsher 		if (!dev)
4429aa43c215SJeff Kirsher 			continue;
4430aa43c215SJeff Kirsher 		qlcnic_config_indev_addr(adapter, dev, event);
4431aa43c215SJeff Kirsher 	}
4432aa43c215SJeff Kirsher }
4433aa43c215SJeff Kirsher 
4434aa43c215SJeff Kirsher static int qlcnic_netdev_event(struct notifier_block *this,
4435aa43c215SJeff Kirsher 				 unsigned long event, void *ptr)
4436aa43c215SJeff Kirsher {
4437aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
4438aa43c215SJeff Kirsher 	struct net_device *dev = (struct net_device *)ptr;
4439aa43c215SJeff Kirsher 
4440aa43c215SJeff Kirsher recheck:
4441aa43c215SJeff Kirsher 	if (dev == NULL)
4442aa43c215SJeff Kirsher 		goto done;
4443aa43c215SJeff Kirsher 
4444aa43c215SJeff Kirsher 	if (dev->priv_flags & IFF_802_1Q_VLAN) {
4445aa43c215SJeff Kirsher 		dev = vlan_dev_real_dev(dev);
4446aa43c215SJeff Kirsher 		goto recheck;
4447aa43c215SJeff Kirsher 	}
4448aa43c215SJeff Kirsher 
4449aa43c215SJeff Kirsher 	if (!is_qlcnic_netdev(dev))
4450aa43c215SJeff Kirsher 		goto done;
4451aa43c215SJeff Kirsher 
4452aa43c215SJeff Kirsher 	adapter = netdev_priv(dev);
4453aa43c215SJeff Kirsher 
4454aa43c215SJeff Kirsher 	if (!adapter)
4455aa43c215SJeff Kirsher 		goto done;
4456aa43c215SJeff Kirsher 
4457aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4458aa43c215SJeff Kirsher 		goto done;
4459aa43c215SJeff Kirsher 
4460aa43c215SJeff Kirsher 	qlcnic_config_indev_addr(adapter, dev, event);
4461aa43c215SJeff Kirsher done:
4462aa43c215SJeff Kirsher 	return NOTIFY_DONE;
4463aa43c215SJeff Kirsher }
4464aa43c215SJeff Kirsher 
4465aa43c215SJeff Kirsher static int
4466aa43c215SJeff Kirsher qlcnic_inetaddr_event(struct notifier_block *this,
4467aa43c215SJeff Kirsher 		unsigned long event, void *ptr)
4468aa43c215SJeff Kirsher {
4469aa43c215SJeff Kirsher 	struct qlcnic_adapter *adapter;
4470aa43c215SJeff Kirsher 	struct net_device *dev;
4471aa43c215SJeff Kirsher 
4472aa43c215SJeff Kirsher 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4473aa43c215SJeff Kirsher 
4474aa43c215SJeff Kirsher 	dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4475aa43c215SJeff Kirsher 
4476aa43c215SJeff Kirsher recheck:
4477aa43c215SJeff Kirsher 	if (dev == NULL)
4478aa43c215SJeff Kirsher 		goto done;
4479aa43c215SJeff Kirsher 
4480aa43c215SJeff Kirsher 	if (dev->priv_flags & IFF_802_1Q_VLAN) {
4481aa43c215SJeff Kirsher 		dev = vlan_dev_real_dev(dev);
4482aa43c215SJeff Kirsher 		goto recheck;
4483aa43c215SJeff Kirsher 	}
4484aa43c215SJeff Kirsher 
4485aa43c215SJeff Kirsher 	if (!is_qlcnic_netdev(dev))
4486aa43c215SJeff Kirsher 		goto done;
4487aa43c215SJeff Kirsher 
4488aa43c215SJeff Kirsher 	adapter = netdev_priv(dev);
4489aa43c215SJeff Kirsher 
4490aa43c215SJeff Kirsher 	if (!adapter)
4491aa43c215SJeff Kirsher 		goto done;
4492aa43c215SJeff Kirsher 
4493aa43c215SJeff Kirsher 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4494aa43c215SJeff Kirsher 		goto done;
4495aa43c215SJeff Kirsher 
4496aa43c215SJeff Kirsher 	switch (event) {
4497aa43c215SJeff Kirsher 	case NETDEV_UP:
4498aa43c215SJeff Kirsher 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4499aa43c215SJeff Kirsher 		break;
4500aa43c215SJeff Kirsher 	case NETDEV_DOWN:
4501aa43c215SJeff Kirsher 		qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4502aa43c215SJeff Kirsher 		break;
4503aa43c215SJeff Kirsher 	default:
4504aa43c215SJeff Kirsher 		break;
4505aa43c215SJeff Kirsher 	}
4506aa43c215SJeff Kirsher 
4507aa43c215SJeff Kirsher done:
4508aa43c215SJeff Kirsher 	return NOTIFY_DONE;
4509aa43c215SJeff Kirsher }
4510aa43c215SJeff Kirsher 
4511aa43c215SJeff Kirsher static struct notifier_block	qlcnic_netdev_cb = {
4512aa43c215SJeff Kirsher 	.notifier_call = qlcnic_netdev_event,
4513aa43c215SJeff Kirsher };
4514aa43c215SJeff Kirsher 
4515aa43c215SJeff Kirsher static struct notifier_block qlcnic_inetaddr_cb = {
4516aa43c215SJeff Kirsher 	.notifier_call = qlcnic_inetaddr_event,
4517aa43c215SJeff Kirsher };
4518aa43c215SJeff Kirsher #else
4519aa43c215SJeff Kirsher static void
4520aa43c215SJeff Kirsher qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4521aa43c215SJeff Kirsher { }
4522aa43c215SJeff Kirsher #endif
452363507592SShahed Shaikh static struct pci_error_handlers qlcnic_err_handler = {
4524aa43c215SJeff Kirsher 	.error_detected = qlcnic_io_error_detected,
4525aa43c215SJeff Kirsher 	.slot_reset = qlcnic_io_slot_reset,
4526aa43c215SJeff Kirsher 	.resume = qlcnic_io_resume,
4527aa43c215SJeff Kirsher };
4528aa43c215SJeff Kirsher 
4529aa43c215SJeff Kirsher static struct pci_driver qlcnic_driver = {
4530aa43c215SJeff Kirsher 	.name = qlcnic_driver_name,
4531aa43c215SJeff Kirsher 	.id_table = qlcnic_pci_tbl,
4532aa43c215SJeff Kirsher 	.probe = qlcnic_probe,
4533aa43c215SJeff Kirsher 	.remove = __devexit_p(qlcnic_remove),
4534aa43c215SJeff Kirsher #ifdef CONFIG_PM
4535aa43c215SJeff Kirsher 	.suspend = qlcnic_suspend,
4536aa43c215SJeff Kirsher 	.resume = qlcnic_resume,
4537aa43c215SJeff Kirsher #endif
4538aa43c215SJeff Kirsher 	.shutdown = qlcnic_shutdown,
4539aa43c215SJeff Kirsher 	.err_handler = &qlcnic_err_handler
4540aa43c215SJeff Kirsher 
4541aa43c215SJeff Kirsher };
4542aa43c215SJeff Kirsher 
4543aa43c215SJeff Kirsher static int __init qlcnic_init_module(void)
4544aa43c215SJeff Kirsher {
4545aa43c215SJeff Kirsher 	int ret;
4546aa43c215SJeff Kirsher 
4547aa43c215SJeff Kirsher 	printk(KERN_INFO "%s\n", qlcnic_driver_string);
4548aa43c215SJeff Kirsher 
4549aa43c215SJeff Kirsher 	qlcnic_wq = create_singlethread_workqueue("qlcnic");
4550aa43c215SJeff Kirsher 	if (qlcnic_wq == NULL) {
4551aa43c215SJeff Kirsher 		printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4552aa43c215SJeff Kirsher 		return -ENOMEM;
4553aa43c215SJeff Kirsher 	}
4554aa43c215SJeff Kirsher 
4555aa43c215SJeff Kirsher #ifdef CONFIG_INET
4556aa43c215SJeff Kirsher 	register_netdevice_notifier(&qlcnic_netdev_cb);
4557aa43c215SJeff Kirsher 	register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4558aa43c215SJeff Kirsher #endif
4559aa43c215SJeff Kirsher 
4560aa43c215SJeff Kirsher 	ret = pci_register_driver(&qlcnic_driver);
4561aa43c215SJeff Kirsher 	if (ret) {
4562aa43c215SJeff Kirsher #ifdef CONFIG_INET
4563aa43c215SJeff Kirsher 		unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4564aa43c215SJeff Kirsher 		unregister_netdevice_notifier(&qlcnic_netdev_cb);
4565aa43c215SJeff Kirsher #endif
4566aa43c215SJeff Kirsher 		destroy_workqueue(qlcnic_wq);
4567aa43c215SJeff Kirsher 	}
4568aa43c215SJeff Kirsher 
4569aa43c215SJeff Kirsher 	return ret;
4570aa43c215SJeff Kirsher }
4571aa43c215SJeff Kirsher 
4572aa43c215SJeff Kirsher module_init(qlcnic_init_module);
4573aa43c215SJeff Kirsher 
4574aa43c215SJeff Kirsher static void __exit qlcnic_exit_module(void)
4575aa43c215SJeff Kirsher {
4576aa43c215SJeff Kirsher 
4577aa43c215SJeff Kirsher 	pci_unregister_driver(&qlcnic_driver);
4578aa43c215SJeff Kirsher 
4579aa43c215SJeff Kirsher #ifdef CONFIG_INET
4580aa43c215SJeff Kirsher 	unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4581aa43c215SJeff Kirsher 	unregister_netdevice_notifier(&qlcnic_netdev_cb);
4582aa43c215SJeff Kirsher #endif
4583aa43c215SJeff Kirsher 	destroy_workqueue(qlcnic_wq);
4584aa43c215SJeff Kirsher }
4585aa43c215SJeff Kirsher 
4586aa43c215SJeff Kirsher module_exit(qlcnic_exit_module);
4587