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