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