1a6a5580cSJeff Kirsher /*
2a6a5580cSJeff Kirsher  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3a6a5580cSJeff Kirsher  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4a6a5580cSJeff Kirsher  *
5a6a5580cSJeff Kirsher  * This program is free software; you may redistribute it and/or modify
6a6a5580cSJeff Kirsher  * it under the terms of the GNU General Public License as published by
7a6a5580cSJeff Kirsher  * the Free Software Foundation; version 2 of the License.
8a6a5580cSJeff Kirsher  *
9a6a5580cSJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10a6a5580cSJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11a6a5580cSJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12a6a5580cSJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13a6a5580cSJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14a6a5580cSJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15a6a5580cSJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16a6a5580cSJeff Kirsher  * SOFTWARE.
17a6a5580cSJeff Kirsher  *
18a6a5580cSJeff Kirsher  */
19a6a5580cSJeff Kirsher 
20a6a5580cSJeff Kirsher #include <linux/module.h>
21a6a5580cSJeff Kirsher #include <linux/kernel.h>
22a6a5580cSJeff Kirsher #include <linux/string.h>
23a6a5580cSJeff Kirsher #include <linux/errno.h>
24a6a5580cSJeff Kirsher #include <linux/types.h>
25a6a5580cSJeff Kirsher #include <linux/init.h>
26a6a5580cSJeff Kirsher #include <linux/interrupt.h>
27a6a5580cSJeff Kirsher #include <linux/workqueue.h>
28a6a5580cSJeff Kirsher #include <linux/pci.h>
29a6a5580cSJeff Kirsher #include <linux/netdevice.h>
30a6a5580cSJeff Kirsher #include <linux/etherdevice.h>
3101789349SJiri Pirko #include <linux/if.h>
32a6a5580cSJeff Kirsher #include <linux/if_ether.h>
33a6a5580cSJeff Kirsher #include <linux/if_vlan.h>
34a6a5580cSJeff Kirsher #include <linux/in.h>
35a6a5580cSJeff Kirsher #include <linux/ip.h>
36a6a5580cSJeff Kirsher #include <linux/ipv6.h>
37a6a5580cSJeff Kirsher #include <linux/tcp.h>
38a6a5580cSJeff Kirsher #include <linux/rtnetlink.h>
39a6a5580cSJeff Kirsher #include <linux/prefetch.h>
40a6a5580cSJeff Kirsher #include <net/ip6_checksum.h>
417c2ce6e6SSujith Sankar #include <linux/ktime.h>
42b6e97c13SGovindarajulu Varadarajan #ifdef CONFIG_RFS_ACCEL
43b6e97c13SGovindarajulu Varadarajan #include <linux/cpu_rmap.h>
44b6e97c13SGovindarajulu Varadarajan #endif
4514747cd9SGovindarajulu Varadarajan #ifdef CONFIG_NET_RX_BUSY_POLL
4614747cd9SGovindarajulu Varadarajan #include <net/busy_poll.h>
4714747cd9SGovindarajulu Varadarajan #endif
48a6a5580cSJeff Kirsher 
49a6a5580cSJeff Kirsher #include "cq_enet_desc.h"
50a6a5580cSJeff Kirsher #include "vnic_dev.h"
51a6a5580cSJeff Kirsher #include "vnic_intr.h"
52a6a5580cSJeff Kirsher #include "vnic_stats.h"
53a6a5580cSJeff Kirsher #include "vnic_vic.h"
54a6a5580cSJeff Kirsher #include "enic_res.h"
55a6a5580cSJeff Kirsher #include "enic.h"
56a6a5580cSJeff Kirsher #include "enic_dev.h"
57a6a5580cSJeff Kirsher #include "enic_pp.h"
58a145df23SGovindarajulu Varadarajan #include "enic_clsf.h"
59a6a5580cSJeff Kirsher 
60a6a5580cSJeff Kirsher #define ENIC_NOTIFY_TIMER_PERIOD	(2 * HZ)
61a6a5580cSJeff Kirsher #define WQ_ENET_MAX_DESC_LEN		(1 << WQ_ENET_LEN_BITS)
62a6a5580cSJeff Kirsher #define MAX_TSO				(1 << 16)
63a6a5580cSJeff Kirsher #define ENIC_DESC_MAX_SPLITS		(MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
64a6a5580cSJeff Kirsher 
65a6a5580cSJeff Kirsher #define PCI_DEVICE_ID_CISCO_VIC_ENET         0x0043  /* ethernet vnic */
66a6a5580cSJeff Kirsher #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN     0x0044  /* enet dynamic vnic */
673a4adef5SRoopa Prabhu #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF      0x0071  /* enet SRIOV VF */
68a6a5580cSJeff Kirsher 
69a03bb56eSGovindarajulu Varadarajan #define RX_COPYBREAK_DEFAULT		256
70a03bb56eSGovindarajulu Varadarajan 
71a6a5580cSJeff Kirsher /* Supported devices */
729baa3c34SBenoit Taine static const struct pci_device_id enic_id_table[] = {
73a6a5580cSJeff Kirsher 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
74a6a5580cSJeff Kirsher 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
753a4adef5SRoopa Prabhu 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
76a6a5580cSJeff Kirsher 	{ 0, }	/* end of table */
77a6a5580cSJeff Kirsher };
78a6a5580cSJeff Kirsher 
79a6a5580cSJeff Kirsher MODULE_DESCRIPTION(DRV_DESCRIPTION);
80a6a5580cSJeff Kirsher MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
81a6a5580cSJeff Kirsher MODULE_LICENSE("GPL");
82a6a5580cSJeff Kirsher MODULE_VERSION(DRV_VERSION);
83a6a5580cSJeff Kirsher MODULE_DEVICE_TABLE(pci, enic_id_table);
84a6a5580cSJeff Kirsher 
857c2ce6e6SSujith Sankar #define ENIC_LARGE_PKT_THRESHOLD		1000
867c2ce6e6SSujith Sankar #define ENIC_MAX_COALESCE_TIMERS		10
877c2ce6e6SSujith Sankar /*  Interrupt moderation table, which will be used to decide the
887c2ce6e6SSujith Sankar  *  coalescing timer values
897c2ce6e6SSujith Sankar  *  {rx_rate in Mbps, mapping percentage of the range}
907c2ce6e6SSujith Sankar  */
917c2ce6e6SSujith Sankar struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
927c2ce6e6SSujith Sankar 	{4000,  0},
937c2ce6e6SSujith Sankar 	{4400, 10},
947c2ce6e6SSujith Sankar 	{5060, 20},
957c2ce6e6SSujith Sankar 	{5230, 30},
967c2ce6e6SSujith Sankar 	{5540, 40},
977c2ce6e6SSujith Sankar 	{5820, 50},
987c2ce6e6SSujith Sankar 	{6120, 60},
997c2ce6e6SSujith Sankar 	{6435, 70},
1007c2ce6e6SSujith Sankar 	{6745, 80},
1017c2ce6e6SSujith Sankar 	{7000, 90},
1027c2ce6e6SSujith Sankar 	{0xFFFFFFFF, 100}
1037c2ce6e6SSujith Sankar };
1047c2ce6e6SSujith Sankar 
1057c2ce6e6SSujith Sankar /* This table helps the driver to pick different ranges for rx coalescing
1067c2ce6e6SSujith Sankar  * timer depending on the link speed.
1077c2ce6e6SSujith Sankar  */
1087c2ce6e6SSujith Sankar struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
1097c2ce6e6SSujith Sankar 	{0,  0}, /* 0  - 4  Gbps */
1107c2ce6e6SSujith Sankar 	{0,  3}, /* 4  - 10 Gbps */
1117c2ce6e6SSujith Sankar 	{3,  6}, /* 10 - 40 Gbps */
1127c2ce6e6SSujith Sankar };
1137c2ce6e6SSujith Sankar 
1143f192795SRoopa Prabhu int enic_is_dynamic(struct enic *enic)
115a6a5580cSJeff Kirsher {
116a6a5580cSJeff Kirsher 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
117a6a5580cSJeff Kirsher }
118a6a5580cSJeff Kirsher 
1198749b427SRoopa Prabhu int enic_sriov_enabled(struct enic *enic)
1208749b427SRoopa Prabhu {
1218749b427SRoopa Prabhu 	return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
1228749b427SRoopa Prabhu }
1238749b427SRoopa Prabhu 
1243a4adef5SRoopa Prabhu static int enic_is_sriov_vf(struct enic *enic)
1253a4adef5SRoopa Prabhu {
1263a4adef5SRoopa Prabhu 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
1273a4adef5SRoopa Prabhu }
1283a4adef5SRoopa Prabhu 
129889d13f5SRoopa Prabhu int enic_is_valid_vf(struct enic *enic, int vf)
130889d13f5SRoopa Prabhu {
131889d13f5SRoopa Prabhu #ifdef CONFIG_PCI_IOV
132889d13f5SRoopa Prabhu 	return vf >= 0 && vf < enic->num_vfs;
133889d13f5SRoopa Prabhu #else
134889d13f5SRoopa Prabhu 	return 0;
135889d13f5SRoopa Prabhu #endif
136889d13f5SRoopa Prabhu }
137889d13f5SRoopa Prabhu 
138a6a5580cSJeff Kirsher static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
139a6a5580cSJeff Kirsher {
140a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(wq->vdev);
141a6a5580cSJeff Kirsher 
142a6a5580cSJeff Kirsher 	if (buf->sop)
143a6a5580cSJeff Kirsher 		pci_unmap_single(enic->pdev, buf->dma_addr,
144a6a5580cSJeff Kirsher 			buf->len, PCI_DMA_TODEVICE);
145a6a5580cSJeff Kirsher 	else
146a6a5580cSJeff Kirsher 		pci_unmap_page(enic->pdev, buf->dma_addr,
147a6a5580cSJeff Kirsher 			buf->len, PCI_DMA_TODEVICE);
148a6a5580cSJeff Kirsher 
149a6a5580cSJeff Kirsher 	if (buf->os_buf)
150a6a5580cSJeff Kirsher 		dev_kfree_skb_any(buf->os_buf);
151a6a5580cSJeff Kirsher }
152a6a5580cSJeff Kirsher 
153a6a5580cSJeff Kirsher static void enic_wq_free_buf(struct vnic_wq *wq,
154a6a5580cSJeff Kirsher 	struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
155a6a5580cSJeff Kirsher {
156a6a5580cSJeff Kirsher 	enic_free_wq_buf(wq, buf);
157a6a5580cSJeff Kirsher }
158a6a5580cSJeff Kirsher 
159a6a5580cSJeff Kirsher static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
160a6a5580cSJeff Kirsher 	u8 type, u16 q_number, u16 completed_index, void *opaque)
161a6a5580cSJeff Kirsher {
162a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(vdev);
163a6a5580cSJeff Kirsher 
164a6a5580cSJeff Kirsher 	spin_lock(&enic->wq_lock[q_number]);
165a6a5580cSJeff Kirsher 
166a6a5580cSJeff Kirsher 	vnic_wq_service(&enic->wq[q_number], cq_desc,
167a6a5580cSJeff Kirsher 		completed_index, enic_wq_free_buf,
168a6a5580cSJeff Kirsher 		opaque);
169a6a5580cSJeff Kirsher 
170822473b6Sgovindarajulu.v 	if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) &&
171a6a5580cSJeff Kirsher 	    vnic_wq_desc_avail(&enic->wq[q_number]) >=
172a6a5580cSJeff Kirsher 	    (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
173822473b6Sgovindarajulu.v 		netif_wake_subqueue(enic->netdev, q_number);
174a6a5580cSJeff Kirsher 
175a6a5580cSJeff Kirsher 	spin_unlock(&enic->wq_lock[q_number]);
176a6a5580cSJeff Kirsher 
177a6a5580cSJeff Kirsher 	return 0;
178a6a5580cSJeff Kirsher }
179a6a5580cSJeff Kirsher 
180a6a5580cSJeff Kirsher static void enic_log_q_error(struct enic *enic)
181a6a5580cSJeff Kirsher {
182a6a5580cSJeff Kirsher 	unsigned int i;
183a6a5580cSJeff Kirsher 	u32 error_status;
184a6a5580cSJeff Kirsher 
185a6a5580cSJeff Kirsher 	for (i = 0; i < enic->wq_count; i++) {
186a6a5580cSJeff Kirsher 		error_status = vnic_wq_error_status(&enic->wq[i]);
187a6a5580cSJeff Kirsher 		if (error_status)
188a6a5580cSJeff Kirsher 			netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
189a6a5580cSJeff Kirsher 				i, error_status);
190a6a5580cSJeff Kirsher 	}
191a6a5580cSJeff Kirsher 
192a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++) {
193a6a5580cSJeff Kirsher 		error_status = vnic_rq_error_status(&enic->rq[i]);
194a6a5580cSJeff Kirsher 		if (error_status)
195a6a5580cSJeff Kirsher 			netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
196a6a5580cSJeff Kirsher 				i, error_status);
197a6a5580cSJeff Kirsher 	}
198a6a5580cSJeff Kirsher }
199a6a5580cSJeff Kirsher 
200a6a5580cSJeff Kirsher static void enic_msglvl_check(struct enic *enic)
201a6a5580cSJeff Kirsher {
202a6a5580cSJeff Kirsher 	u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
203a6a5580cSJeff Kirsher 
204a6a5580cSJeff Kirsher 	if (msg_enable != enic->msg_enable) {
205a6a5580cSJeff Kirsher 		netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
206a6a5580cSJeff Kirsher 			enic->msg_enable, msg_enable);
207a6a5580cSJeff Kirsher 		enic->msg_enable = msg_enable;
208a6a5580cSJeff Kirsher 	}
209a6a5580cSJeff Kirsher }
210a6a5580cSJeff Kirsher 
211a6a5580cSJeff Kirsher static void enic_mtu_check(struct enic *enic)
212a6a5580cSJeff Kirsher {
213a6a5580cSJeff Kirsher 	u32 mtu = vnic_dev_mtu(enic->vdev);
214a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
215a6a5580cSJeff Kirsher 
216a6a5580cSJeff Kirsher 	if (mtu && mtu != enic->port_mtu) {
217a6a5580cSJeff Kirsher 		enic->port_mtu = mtu;
2187335903cSRoopa Prabhu 		if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
219a6a5580cSJeff Kirsher 			mtu = max_t(int, ENIC_MIN_MTU,
220a6a5580cSJeff Kirsher 				min_t(int, ENIC_MAX_MTU, mtu));
221a6a5580cSJeff Kirsher 			if (mtu != netdev->mtu)
222a6a5580cSJeff Kirsher 				schedule_work(&enic->change_mtu_work);
223a6a5580cSJeff Kirsher 		} else {
224a6a5580cSJeff Kirsher 			if (mtu < netdev->mtu)
225a6a5580cSJeff Kirsher 				netdev_warn(netdev,
226a6a5580cSJeff Kirsher 					"interface MTU (%d) set higher "
227a6a5580cSJeff Kirsher 					"than switch port MTU (%d)\n",
228a6a5580cSJeff Kirsher 					netdev->mtu, mtu);
229a6a5580cSJeff Kirsher 		}
230a6a5580cSJeff Kirsher 	}
231a6a5580cSJeff Kirsher }
232a6a5580cSJeff Kirsher 
233a6a5580cSJeff Kirsher static void enic_link_check(struct enic *enic)
234a6a5580cSJeff Kirsher {
235a6a5580cSJeff Kirsher 	int link_status = vnic_dev_link_status(enic->vdev);
236a6a5580cSJeff Kirsher 	int carrier_ok = netif_carrier_ok(enic->netdev);
237a6a5580cSJeff Kirsher 
238a6a5580cSJeff Kirsher 	if (link_status && !carrier_ok) {
239a6a5580cSJeff Kirsher 		netdev_info(enic->netdev, "Link UP\n");
240a6a5580cSJeff Kirsher 		netif_carrier_on(enic->netdev);
241a6a5580cSJeff Kirsher 	} else if (!link_status && carrier_ok) {
242a6a5580cSJeff Kirsher 		netdev_info(enic->netdev, "Link DOWN\n");
243a6a5580cSJeff Kirsher 		netif_carrier_off(enic->netdev);
244a6a5580cSJeff Kirsher 	}
245a6a5580cSJeff Kirsher }
246a6a5580cSJeff Kirsher 
247a6a5580cSJeff Kirsher static void enic_notify_check(struct enic *enic)
248a6a5580cSJeff Kirsher {
249a6a5580cSJeff Kirsher 	enic_msglvl_check(enic);
250a6a5580cSJeff Kirsher 	enic_mtu_check(enic);
251a6a5580cSJeff Kirsher 	enic_link_check(enic);
252a6a5580cSJeff Kirsher }
253a6a5580cSJeff Kirsher 
254a6a5580cSJeff Kirsher #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
255a6a5580cSJeff Kirsher 
256a6a5580cSJeff Kirsher static irqreturn_t enic_isr_legacy(int irq, void *data)
257a6a5580cSJeff Kirsher {
258a6a5580cSJeff Kirsher 	struct net_device *netdev = data;
259a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
260a6a5580cSJeff Kirsher 	unsigned int io_intr = enic_legacy_io_intr();
261a6a5580cSJeff Kirsher 	unsigned int err_intr = enic_legacy_err_intr();
262a6a5580cSJeff Kirsher 	unsigned int notify_intr = enic_legacy_notify_intr();
263a6a5580cSJeff Kirsher 	u32 pba;
264a6a5580cSJeff Kirsher 
265a6a5580cSJeff Kirsher 	vnic_intr_mask(&enic->intr[io_intr]);
266a6a5580cSJeff Kirsher 
267a6a5580cSJeff Kirsher 	pba = vnic_intr_legacy_pba(enic->legacy_pba);
268a6a5580cSJeff Kirsher 	if (!pba) {
269a6a5580cSJeff Kirsher 		vnic_intr_unmask(&enic->intr[io_intr]);
270a6a5580cSJeff Kirsher 		return IRQ_NONE;	/* not our interrupt */
271a6a5580cSJeff Kirsher 	}
272a6a5580cSJeff Kirsher 
273a6a5580cSJeff Kirsher 	if (ENIC_TEST_INTR(pba, notify_intr)) {
274a6a5580cSJeff Kirsher 		vnic_intr_return_all_credits(&enic->intr[notify_intr]);
275a6a5580cSJeff Kirsher 		enic_notify_check(enic);
276a6a5580cSJeff Kirsher 	}
277a6a5580cSJeff Kirsher 
278a6a5580cSJeff Kirsher 	if (ENIC_TEST_INTR(pba, err_intr)) {
279a6a5580cSJeff Kirsher 		vnic_intr_return_all_credits(&enic->intr[err_intr]);
280a6a5580cSJeff Kirsher 		enic_log_q_error(enic);
281a6a5580cSJeff Kirsher 		/* schedule recovery from WQ/RQ error */
282a6a5580cSJeff Kirsher 		schedule_work(&enic->reset);
283a6a5580cSJeff Kirsher 		return IRQ_HANDLED;
284a6a5580cSJeff Kirsher 	}
285a6a5580cSJeff Kirsher 
286db40b3f5SGovindarajulu Varadarajan 	if (ENIC_TEST_INTR(pba, io_intr))
287db40b3f5SGovindarajulu Varadarajan 		napi_schedule_irqoff(&enic->napi[0]);
288db40b3f5SGovindarajulu Varadarajan 	else
289a6a5580cSJeff Kirsher 		vnic_intr_unmask(&enic->intr[io_intr]);
290a6a5580cSJeff Kirsher 
291a6a5580cSJeff Kirsher 	return IRQ_HANDLED;
292a6a5580cSJeff Kirsher }
293a6a5580cSJeff Kirsher 
294a6a5580cSJeff Kirsher static irqreturn_t enic_isr_msi(int irq, void *data)
295a6a5580cSJeff Kirsher {
296a6a5580cSJeff Kirsher 	struct enic *enic = data;
297a6a5580cSJeff Kirsher 
298a6a5580cSJeff Kirsher 	/* With MSI, there is no sharing of interrupts, so this is
299a6a5580cSJeff Kirsher 	 * our interrupt and there is no need to ack it.  The device
300a6a5580cSJeff Kirsher 	 * is not providing per-vector masking, so the OS will not
301a6a5580cSJeff Kirsher 	 * write to PCI config space to mask/unmask the interrupt.
302a6a5580cSJeff Kirsher 	 * We're using mask_on_assertion for MSI, so the device
303a6a5580cSJeff Kirsher 	 * automatically masks the interrupt when the interrupt is
304a6a5580cSJeff Kirsher 	 * generated.  Later, when exiting polling, the interrupt
305a6a5580cSJeff Kirsher 	 * will be unmasked (see enic_poll).
306a6a5580cSJeff Kirsher 	 *
307a6a5580cSJeff Kirsher 	 * Also, the device uses the same PCIe Traffic Class (TC)
308a6a5580cSJeff Kirsher 	 * for Memory Write data and MSI, so there are no ordering
309a6a5580cSJeff Kirsher 	 * issues; the MSI will always arrive at the Root Complex
310a6a5580cSJeff Kirsher 	 * _after_ corresponding Memory Writes (i.e. descriptor
311a6a5580cSJeff Kirsher 	 * writes).
312a6a5580cSJeff Kirsher 	 */
313a6a5580cSJeff Kirsher 
314db40b3f5SGovindarajulu Varadarajan 	napi_schedule_irqoff(&enic->napi[0]);
315a6a5580cSJeff Kirsher 
316a6a5580cSJeff Kirsher 	return IRQ_HANDLED;
317a6a5580cSJeff Kirsher }
318a6a5580cSJeff Kirsher 
3194cfe8785SGovindarajulu Varadarajan static irqreturn_t enic_isr_msix(int irq, void *data)
320a6a5580cSJeff Kirsher {
321a6a5580cSJeff Kirsher 	struct napi_struct *napi = data;
322a6a5580cSJeff Kirsher 
323db40b3f5SGovindarajulu Varadarajan 	napi_schedule_irqoff(napi);
324a6a5580cSJeff Kirsher 
325a6a5580cSJeff Kirsher 	return IRQ_HANDLED;
326a6a5580cSJeff Kirsher }
327a6a5580cSJeff Kirsher 
328a6a5580cSJeff Kirsher static irqreturn_t enic_isr_msix_err(int irq, void *data)
329a6a5580cSJeff Kirsher {
330a6a5580cSJeff Kirsher 	struct enic *enic = data;
331a6a5580cSJeff Kirsher 	unsigned int intr = enic_msix_err_intr(enic);
332a6a5580cSJeff Kirsher 
333a6a5580cSJeff Kirsher 	vnic_intr_return_all_credits(&enic->intr[intr]);
334a6a5580cSJeff Kirsher 
335a6a5580cSJeff Kirsher 	enic_log_q_error(enic);
336a6a5580cSJeff Kirsher 
337a6a5580cSJeff Kirsher 	/* schedule recovery from WQ/RQ error */
338a6a5580cSJeff Kirsher 	schedule_work(&enic->reset);
339a6a5580cSJeff Kirsher 
340a6a5580cSJeff Kirsher 	return IRQ_HANDLED;
341a6a5580cSJeff Kirsher }
342a6a5580cSJeff Kirsher 
343a6a5580cSJeff Kirsher static irqreturn_t enic_isr_msix_notify(int irq, void *data)
344a6a5580cSJeff Kirsher {
345a6a5580cSJeff Kirsher 	struct enic *enic = data;
346a6a5580cSJeff Kirsher 	unsigned int intr = enic_msix_notify_intr(enic);
347a6a5580cSJeff Kirsher 
348a6a5580cSJeff Kirsher 	vnic_intr_return_all_credits(&enic->intr[intr]);
349a6a5580cSJeff Kirsher 	enic_notify_check(enic);
350a6a5580cSJeff Kirsher 
351a6a5580cSJeff Kirsher 	return IRQ_HANDLED;
352a6a5580cSJeff Kirsher }
353a6a5580cSJeff Kirsher 
354a6a5580cSJeff Kirsher static inline void enic_queue_wq_skb_cont(struct enic *enic,
355a6a5580cSJeff Kirsher 	struct vnic_wq *wq, struct sk_buff *skb,
356a6a5580cSJeff Kirsher 	unsigned int len_left, int loopback)
357a6a5580cSJeff Kirsher {
3589e903e08SEric Dumazet 	const skb_frag_t *frag;
359a6a5580cSJeff Kirsher 
360a6a5580cSJeff Kirsher 	/* Queue additional data fragments */
361a6a5580cSJeff Kirsher 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
3629e903e08SEric Dumazet 		len_left -= skb_frag_size(frag);
363a6a5580cSJeff Kirsher 		enic_queue_wq_desc_cont(wq, skb,
3644bf5adbfSIan Campbell 			skb_frag_dma_map(&enic->pdev->dev,
3659e903e08SEric Dumazet 					 frag, 0, skb_frag_size(frag),
3665d6bcdfeSIan Campbell 					 DMA_TO_DEVICE),
3679e903e08SEric Dumazet 			skb_frag_size(frag),
368a6a5580cSJeff Kirsher 			(len_left == 0),	/* EOP? */
369a6a5580cSJeff Kirsher 			loopback);
370a6a5580cSJeff Kirsher 	}
371a6a5580cSJeff Kirsher }
372a6a5580cSJeff Kirsher 
373a6a5580cSJeff Kirsher static inline void enic_queue_wq_skb_vlan(struct enic *enic,
374a6a5580cSJeff Kirsher 	struct vnic_wq *wq, struct sk_buff *skb,
375a6a5580cSJeff Kirsher 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
376a6a5580cSJeff Kirsher {
377a6a5580cSJeff Kirsher 	unsigned int head_len = skb_headlen(skb);
378a6a5580cSJeff Kirsher 	unsigned int len_left = skb->len - head_len;
379a6a5580cSJeff Kirsher 	int eop = (len_left == 0);
380a6a5580cSJeff Kirsher 
381a6a5580cSJeff Kirsher 	/* Queue the main skb fragment. The fragments are no larger
382a6a5580cSJeff Kirsher 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
383a6a5580cSJeff Kirsher 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
384a6a5580cSJeff Kirsher 	 * per fragment is queued.
385a6a5580cSJeff Kirsher 	 */
386a6a5580cSJeff Kirsher 	enic_queue_wq_desc(wq, skb,
387a6a5580cSJeff Kirsher 		pci_map_single(enic->pdev, skb->data,
388a6a5580cSJeff Kirsher 			head_len, PCI_DMA_TODEVICE),
389a6a5580cSJeff Kirsher 		head_len,
390a6a5580cSJeff Kirsher 		vlan_tag_insert, vlan_tag,
391a6a5580cSJeff Kirsher 		eop, loopback);
392a6a5580cSJeff Kirsher 
393a6a5580cSJeff Kirsher 	if (!eop)
394a6a5580cSJeff Kirsher 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
395a6a5580cSJeff Kirsher }
396a6a5580cSJeff Kirsher 
397a6a5580cSJeff Kirsher static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
398a6a5580cSJeff Kirsher 	struct vnic_wq *wq, struct sk_buff *skb,
399a6a5580cSJeff Kirsher 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
400a6a5580cSJeff Kirsher {
401a6a5580cSJeff Kirsher 	unsigned int head_len = skb_headlen(skb);
402a6a5580cSJeff Kirsher 	unsigned int len_left = skb->len - head_len;
403a6a5580cSJeff Kirsher 	unsigned int hdr_len = skb_checksum_start_offset(skb);
404a6a5580cSJeff Kirsher 	unsigned int csum_offset = hdr_len + skb->csum_offset;
405a6a5580cSJeff Kirsher 	int eop = (len_left == 0);
406a6a5580cSJeff Kirsher 
407a6a5580cSJeff Kirsher 	/* Queue the main skb fragment. The fragments are no larger
408a6a5580cSJeff Kirsher 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
409a6a5580cSJeff Kirsher 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
410a6a5580cSJeff Kirsher 	 * per fragment is queued.
411a6a5580cSJeff Kirsher 	 */
412a6a5580cSJeff Kirsher 	enic_queue_wq_desc_csum_l4(wq, skb,
413a6a5580cSJeff Kirsher 		pci_map_single(enic->pdev, skb->data,
414a6a5580cSJeff Kirsher 			head_len, PCI_DMA_TODEVICE),
415a6a5580cSJeff Kirsher 		head_len,
416a6a5580cSJeff Kirsher 		csum_offset,
417a6a5580cSJeff Kirsher 		hdr_len,
418a6a5580cSJeff Kirsher 		vlan_tag_insert, vlan_tag,
419a6a5580cSJeff Kirsher 		eop, loopback);
420a6a5580cSJeff Kirsher 
421a6a5580cSJeff Kirsher 	if (!eop)
422a6a5580cSJeff Kirsher 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
423a6a5580cSJeff Kirsher }
424a6a5580cSJeff Kirsher 
425a6a5580cSJeff Kirsher static inline void enic_queue_wq_skb_tso(struct enic *enic,
426a6a5580cSJeff Kirsher 	struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
427a6a5580cSJeff Kirsher 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
428a6a5580cSJeff Kirsher {
429a6a5580cSJeff Kirsher 	unsigned int frag_len_left = skb_headlen(skb);
430a6a5580cSJeff Kirsher 	unsigned int len_left = skb->len - frag_len_left;
431a6a5580cSJeff Kirsher 	unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
432a6a5580cSJeff Kirsher 	int eop = (len_left == 0);
433a6a5580cSJeff Kirsher 	unsigned int len;
434a6a5580cSJeff Kirsher 	dma_addr_t dma_addr;
435a6a5580cSJeff Kirsher 	unsigned int offset = 0;
436a6a5580cSJeff Kirsher 	skb_frag_t *frag;
437a6a5580cSJeff Kirsher 
438a6a5580cSJeff Kirsher 	/* Preload TCP csum field with IP pseudo hdr calculated
439a6a5580cSJeff Kirsher 	 * with IP length set to zero.  HW will later add in length
440a6a5580cSJeff Kirsher 	 * to each TCP segment resulting from the TSO.
441a6a5580cSJeff Kirsher 	 */
442a6a5580cSJeff Kirsher 
443a6a5580cSJeff Kirsher 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
444a6a5580cSJeff Kirsher 		ip_hdr(skb)->check = 0;
445a6a5580cSJeff Kirsher 		tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
446a6a5580cSJeff Kirsher 			ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
447a6a5580cSJeff Kirsher 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
448a6a5580cSJeff Kirsher 		tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
449a6a5580cSJeff Kirsher 			&ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
450a6a5580cSJeff Kirsher 	}
451a6a5580cSJeff Kirsher 
452a6a5580cSJeff Kirsher 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
453a6a5580cSJeff Kirsher 	 * for the main skb fragment
454a6a5580cSJeff Kirsher 	 */
455a6a5580cSJeff Kirsher 	while (frag_len_left) {
456a6a5580cSJeff Kirsher 		len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
457a6a5580cSJeff Kirsher 		dma_addr = pci_map_single(enic->pdev, skb->data + offset,
458a6a5580cSJeff Kirsher 				len, PCI_DMA_TODEVICE);
459a6a5580cSJeff Kirsher 		enic_queue_wq_desc_tso(wq, skb,
460a6a5580cSJeff Kirsher 			dma_addr,
461a6a5580cSJeff Kirsher 			len,
462a6a5580cSJeff Kirsher 			mss, hdr_len,
463a6a5580cSJeff Kirsher 			vlan_tag_insert, vlan_tag,
464a6a5580cSJeff Kirsher 			eop && (len == frag_len_left), loopback);
465a6a5580cSJeff Kirsher 		frag_len_left -= len;
466a6a5580cSJeff Kirsher 		offset += len;
467a6a5580cSJeff Kirsher 	}
468a6a5580cSJeff Kirsher 
469a6a5580cSJeff Kirsher 	if (eop)
470a6a5580cSJeff Kirsher 		return;
471a6a5580cSJeff Kirsher 
472a6a5580cSJeff Kirsher 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
473a6a5580cSJeff Kirsher 	 * for additional data fragments
474a6a5580cSJeff Kirsher 	 */
475a6a5580cSJeff Kirsher 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
4769e903e08SEric Dumazet 		len_left -= skb_frag_size(frag);
4779e903e08SEric Dumazet 		frag_len_left = skb_frag_size(frag);
4784bf5adbfSIan Campbell 		offset = 0;
479a6a5580cSJeff Kirsher 
480a6a5580cSJeff Kirsher 		while (frag_len_left) {
481a6a5580cSJeff Kirsher 			len = min(frag_len_left,
482a6a5580cSJeff Kirsher 				(unsigned int)WQ_ENET_MAX_DESC_LEN);
4834bf5adbfSIan Campbell 			dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
484a6a5580cSJeff Kirsher 						    offset, len,
4855d6bcdfeSIan Campbell 						    DMA_TO_DEVICE);
486a6a5580cSJeff Kirsher 			enic_queue_wq_desc_cont(wq, skb,
487a6a5580cSJeff Kirsher 				dma_addr,
488a6a5580cSJeff Kirsher 				len,
489a6a5580cSJeff Kirsher 				(len_left == 0) &&
490a6a5580cSJeff Kirsher 				(len == frag_len_left),		/* EOP? */
491a6a5580cSJeff Kirsher 				loopback);
492a6a5580cSJeff Kirsher 			frag_len_left -= len;
493a6a5580cSJeff Kirsher 			offset += len;
494a6a5580cSJeff Kirsher 		}
495a6a5580cSJeff Kirsher 	}
496a6a5580cSJeff Kirsher }
497a6a5580cSJeff Kirsher 
498a6a5580cSJeff Kirsher static inline void enic_queue_wq_skb(struct enic *enic,
499a6a5580cSJeff Kirsher 	struct vnic_wq *wq, struct sk_buff *skb)
500a6a5580cSJeff Kirsher {
501a6a5580cSJeff Kirsher 	unsigned int mss = skb_shinfo(skb)->gso_size;
502a6a5580cSJeff Kirsher 	unsigned int vlan_tag = 0;
503a6a5580cSJeff Kirsher 	int vlan_tag_insert = 0;
504a6a5580cSJeff Kirsher 	int loopback = 0;
505a6a5580cSJeff Kirsher 
506a6a5580cSJeff Kirsher 	if (vlan_tx_tag_present(skb)) {
507a6a5580cSJeff Kirsher 		/* VLAN tag from trunking driver */
508a6a5580cSJeff Kirsher 		vlan_tag_insert = 1;
509a6a5580cSJeff Kirsher 		vlan_tag = vlan_tx_tag_get(skb);
510a6a5580cSJeff Kirsher 	} else if (enic->loop_enable) {
511a6a5580cSJeff Kirsher 		vlan_tag = enic->loop_tag;
512a6a5580cSJeff Kirsher 		loopback = 1;
513a6a5580cSJeff Kirsher 	}
514a6a5580cSJeff Kirsher 
515a6a5580cSJeff Kirsher 	if (mss)
516a6a5580cSJeff Kirsher 		enic_queue_wq_skb_tso(enic, wq, skb, mss,
517a6a5580cSJeff Kirsher 			vlan_tag_insert, vlan_tag, loopback);
518a6a5580cSJeff Kirsher 	else if	(skb->ip_summed == CHECKSUM_PARTIAL)
519a6a5580cSJeff Kirsher 		enic_queue_wq_skb_csum_l4(enic, wq, skb,
520a6a5580cSJeff Kirsher 			vlan_tag_insert, vlan_tag, loopback);
521a6a5580cSJeff Kirsher 	else
522a6a5580cSJeff Kirsher 		enic_queue_wq_skb_vlan(enic, wq, skb,
523a6a5580cSJeff Kirsher 			vlan_tag_insert, vlan_tag, loopback);
524a6a5580cSJeff Kirsher }
525a6a5580cSJeff Kirsher 
526a6a5580cSJeff Kirsher /* netif_tx_lock held, process context with BHs disabled, or BH */
527a6a5580cSJeff Kirsher static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
528a6a5580cSJeff Kirsher 	struct net_device *netdev)
529a6a5580cSJeff Kirsher {
530a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
531822473b6Sgovindarajulu.v 	struct vnic_wq *wq;
532822473b6Sgovindarajulu.v 	unsigned int txq_map;
533f8e34d24SGovindarajulu Varadarajan 	struct netdev_queue *txq;
534a6a5580cSJeff Kirsher 
535a6a5580cSJeff Kirsher 	if (skb->len <= 0) {
53698d8a65dSEric W. Biederman 		dev_kfree_skb_any(skb);
537a6a5580cSJeff Kirsher 		return NETDEV_TX_OK;
538a6a5580cSJeff Kirsher 	}
539a6a5580cSJeff Kirsher 
540822473b6Sgovindarajulu.v 	txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
541822473b6Sgovindarajulu.v 	wq = &enic->wq[txq_map];
542f8e34d24SGovindarajulu Varadarajan 	txq = netdev_get_tx_queue(netdev, txq_map);
543822473b6Sgovindarajulu.v 
544a6a5580cSJeff Kirsher 	/* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
545a6a5580cSJeff Kirsher 	 * which is very likely.  In the off chance it's going to take
546a6a5580cSJeff Kirsher 	 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
547a6a5580cSJeff Kirsher 	 */
548a6a5580cSJeff Kirsher 
549a6a5580cSJeff Kirsher 	if (skb_shinfo(skb)->gso_size == 0 &&
550a6a5580cSJeff Kirsher 	    skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
551a6a5580cSJeff Kirsher 	    skb_linearize(skb)) {
55298d8a65dSEric W. Biederman 		dev_kfree_skb_any(skb);
553a6a5580cSJeff Kirsher 		return NETDEV_TX_OK;
554a6a5580cSJeff Kirsher 	}
555a6a5580cSJeff Kirsher 
55678e2045dSGovindarajulu Varadarajan 	spin_lock(&enic->wq_lock[txq_map]);
557a6a5580cSJeff Kirsher 
558a6a5580cSJeff Kirsher 	if (vnic_wq_desc_avail(wq) <
559a6a5580cSJeff Kirsher 	    skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
560f8e34d24SGovindarajulu Varadarajan 		netif_tx_stop_queue(txq);
561a6a5580cSJeff Kirsher 		/* This is a hard error, log it */
562a6a5580cSJeff Kirsher 		netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
56378e2045dSGovindarajulu Varadarajan 		spin_unlock(&enic->wq_lock[txq_map]);
564a6a5580cSJeff Kirsher 		return NETDEV_TX_BUSY;
565a6a5580cSJeff Kirsher 	}
566a6a5580cSJeff Kirsher 
567a6a5580cSJeff Kirsher 	enic_queue_wq_skb(enic, wq, skb);
568a6a5580cSJeff Kirsher 
569a6a5580cSJeff Kirsher 	if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
570f8e34d24SGovindarajulu Varadarajan 		netif_tx_stop_queue(txq);
571f8e34d24SGovindarajulu Varadarajan 	if (!skb->xmit_more || netif_xmit_stopped(txq))
572f8e34d24SGovindarajulu Varadarajan 		vnic_wq_doorbell(wq);
573a6a5580cSJeff Kirsher 
57478e2045dSGovindarajulu Varadarajan 	spin_unlock(&enic->wq_lock[txq_map]);
575a6a5580cSJeff Kirsher 
576a6a5580cSJeff Kirsher 	return NETDEV_TX_OK;
577a6a5580cSJeff Kirsher }
578a6a5580cSJeff Kirsher 
579a6a5580cSJeff Kirsher /* dev_base_lock rwlock held, nominally process context */
580a6a5580cSJeff Kirsher static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
581a6a5580cSJeff Kirsher 						struct rtnl_link_stats64 *net_stats)
582a6a5580cSJeff Kirsher {
583a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
584a6a5580cSJeff Kirsher 	struct vnic_stats *stats;
585a6a5580cSJeff Kirsher 
586a6a5580cSJeff Kirsher 	enic_dev_stats_dump(enic, &stats);
587a6a5580cSJeff Kirsher 
588a6a5580cSJeff Kirsher 	net_stats->tx_packets = stats->tx.tx_frames_ok;
589a6a5580cSJeff Kirsher 	net_stats->tx_bytes = stats->tx.tx_bytes_ok;
590a6a5580cSJeff Kirsher 	net_stats->tx_errors = stats->tx.tx_errors;
591a6a5580cSJeff Kirsher 	net_stats->tx_dropped = stats->tx.tx_drops;
592a6a5580cSJeff Kirsher 
593a6a5580cSJeff Kirsher 	net_stats->rx_packets = stats->rx.rx_frames_ok;
594a6a5580cSJeff Kirsher 	net_stats->rx_bytes = stats->rx.rx_bytes_ok;
595a6a5580cSJeff Kirsher 	net_stats->rx_errors = stats->rx.rx_errors;
596a6a5580cSJeff Kirsher 	net_stats->multicast = stats->rx.rx_multicast_frames_ok;
597a6a5580cSJeff Kirsher 	net_stats->rx_over_errors = enic->rq_truncated_pkts;
598a6a5580cSJeff Kirsher 	net_stats->rx_crc_errors = enic->rq_bad_fcs;
599a6a5580cSJeff Kirsher 	net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
600a6a5580cSJeff Kirsher 
601a6a5580cSJeff Kirsher 	return net_stats;
602a6a5580cSJeff Kirsher }
603a6a5580cSJeff Kirsher 
604f009618aSAlexander Duyck static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
605f009618aSAlexander Duyck {
606f009618aSAlexander Duyck 	struct enic *enic = netdev_priv(netdev);
607f009618aSAlexander Duyck 
608f009618aSAlexander Duyck 	if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
609f009618aSAlexander Duyck 		unsigned int mc_count = netdev_mc_count(netdev);
610f009618aSAlexander Duyck 
611f009618aSAlexander Duyck 		netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
612f009618aSAlexander Duyck 			    ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
613f009618aSAlexander Duyck 
614f009618aSAlexander Duyck 		return -ENOSPC;
615f009618aSAlexander Duyck 	}
616f009618aSAlexander Duyck 
617f009618aSAlexander Duyck 	enic_dev_add_addr(enic, mc_addr);
618f009618aSAlexander Duyck 	enic->mc_count++;
619f009618aSAlexander Duyck 
620f009618aSAlexander Duyck 	return 0;
621f009618aSAlexander Duyck }
622f009618aSAlexander Duyck 
623f009618aSAlexander Duyck static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
624f009618aSAlexander Duyck {
625f009618aSAlexander Duyck 	struct enic *enic = netdev_priv(netdev);
626f009618aSAlexander Duyck 
627f009618aSAlexander Duyck 	enic_dev_del_addr(enic, mc_addr);
628f009618aSAlexander Duyck 	enic->mc_count--;
629f009618aSAlexander Duyck 
630f009618aSAlexander Duyck 	return 0;
631f009618aSAlexander Duyck }
632f009618aSAlexander Duyck 
633f009618aSAlexander Duyck static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
634f009618aSAlexander Duyck {
635f009618aSAlexander Duyck 	struct enic *enic = netdev_priv(netdev);
636f009618aSAlexander Duyck 
637f009618aSAlexander Duyck 	if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
638f009618aSAlexander Duyck 		unsigned int uc_count = netdev_uc_count(netdev);
639f009618aSAlexander Duyck 
640f009618aSAlexander Duyck 		netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
641f009618aSAlexander Duyck 			    ENIC_UNICAST_PERFECT_FILTERS, uc_count);
642f009618aSAlexander Duyck 
643f009618aSAlexander Duyck 		return -ENOSPC;
644f009618aSAlexander Duyck 	}
645f009618aSAlexander Duyck 
646f009618aSAlexander Duyck 	enic_dev_add_addr(enic, uc_addr);
647f009618aSAlexander Duyck 	enic->uc_count++;
648f009618aSAlexander Duyck 
649f009618aSAlexander Duyck 	return 0;
650f009618aSAlexander Duyck }
651f009618aSAlexander Duyck 
652f009618aSAlexander Duyck static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
653f009618aSAlexander Duyck {
654f009618aSAlexander Duyck 	struct enic *enic = netdev_priv(netdev);
655f009618aSAlexander Duyck 
656f009618aSAlexander Duyck 	enic_dev_del_addr(enic, uc_addr);
657f009618aSAlexander Duyck 	enic->uc_count--;
658f009618aSAlexander Duyck 
659f009618aSAlexander Duyck 	return 0;
660f009618aSAlexander Duyck }
661f009618aSAlexander Duyck 
662a6a5580cSJeff Kirsher void enic_reset_addr_lists(struct enic *enic)
663a6a5580cSJeff Kirsher {
664f009618aSAlexander Duyck 	struct net_device *netdev = enic->netdev;
665f009618aSAlexander Duyck 
666f009618aSAlexander Duyck 	__dev_uc_unsync(netdev, NULL);
667f009618aSAlexander Duyck 	__dev_mc_unsync(netdev, NULL);
668f009618aSAlexander Duyck 
669a6a5580cSJeff Kirsher 	enic->mc_count = 0;
670a6a5580cSJeff Kirsher 	enic->uc_count = 0;
671a6a5580cSJeff Kirsher 	enic->flags = 0;
672a6a5580cSJeff Kirsher }
673a6a5580cSJeff Kirsher 
674a6a5580cSJeff Kirsher static int enic_set_mac_addr(struct net_device *netdev, char *addr)
675a6a5580cSJeff Kirsher {
676a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
677a6a5580cSJeff Kirsher 
6787335903cSRoopa Prabhu 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
679a6a5580cSJeff Kirsher 		if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
680a6a5580cSJeff Kirsher 			return -EADDRNOTAVAIL;
681a6a5580cSJeff Kirsher 	} else {
682a6a5580cSJeff Kirsher 		if (!is_valid_ether_addr(addr))
683a6a5580cSJeff Kirsher 			return -EADDRNOTAVAIL;
684a6a5580cSJeff Kirsher 	}
685a6a5580cSJeff Kirsher 
686a6a5580cSJeff Kirsher 	memcpy(netdev->dev_addr, addr, netdev->addr_len);
687a6a5580cSJeff Kirsher 
688a6a5580cSJeff Kirsher 	return 0;
689a6a5580cSJeff Kirsher }
690a6a5580cSJeff Kirsher 
691a6a5580cSJeff Kirsher static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
692a6a5580cSJeff Kirsher {
693a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
694a6a5580cSJeff Kirsher 	struct sockaddr *saddr = p;
695a6a5580cSJeff Kirsher 	char *addr = saddr->sa_data;
696a6a5580cSJeff Kirsher 	int err;
697a6a5580cSJeff Kirsher 
698a6a5580cSJeff Kirsher 	if (netif_running(enic->netdev)) {
699a6a5580cSJeff Kirsher 		err = enic_dev_del_station_addr(enic);
700a6a5580cSJeff Kirsher 		if (err)
701a6a5580cSJeff Kirsher 			return err;
702a6a5580cSJeff Kirsher 	}
703a6a5580cSJeff Kirsher 
704a6a5580cSJeff Kirsher 	err = enic_set_mac_addr(netdev, addr);
705a6a5580cSJeff Kirsher 	if (err)
706a6a5580cSJeff Kirsher 		return err;
707a6a5580cSJeff Kirsher 
708a6a5580cSJeff Kirsher 	if (netif_running(enic->netdev)) {
709a6a5580cSJeff Kirsher 		err = enic_dev_add_station_addr(enic);
710a6a5580cSJeff Kirsher 		if (err)
711a6a5580cSJeff Kirsher 			return err;
712a6a5580cSJeff Kirsher 	}
713a6a5580cSJeff Kirsher 
714a6a5580cSJeff Kirsher 	return err;
715a6a5580cSJeff Kirsher }
716a6a5580cSJeff Kirsher 
717a6a5580cSJeff Kirsher static int enic_set_mac_address(struct net_device *netdev, void *p)
718a6a5580cSJeff Kirsher {
719a6a5580cSJeff Kirsher 	struct sockaddr *saddr = p;
720a6a5580cSJeff Kirsher 	char *addr = saddr->sa_data;
721a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
722a6a5580cSJeff Kirsher 	int err;
723a6a5580cSJeff Kirsher 
724a6a5580cSJeff Kirsher 	err = enic_dev_del_station_addr(enic);
725a6a5580cSJeff Kirsher 	if (err)
726a6a5580cSJeff Kirsher 		return err;
727a6a5580cSJeff Kirsher 
728a6a5580cSJeff Kirsher 	err = enic_set_mac_addr(netdev, addr);
729a6a5580cSJeff Kirsher 	if (err)
730a6a5580cSJeff Kirsher 		return err;
731a6a5580cSJeff Kirsher 
732a6a5580cSJeff Kirsher 	return enic_dev_add_station_addr(enic);
733a6a5580cSJeff Kirsher }
734a6a5580cSJeff Kirsher 
735a6a5580cSJeff Kirsher /* netif_tx_lock held, BHs disabled */
736a6a5580cSJeff Kirsher static void enic_set_rx_mode(struct net_device *netdev)
737a6a5580cSJeff Kirsher {
738a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
739a6a5580cSJeff Kirsher 	int directed = 1;
740a6a5580cSJeff Kirsher 	int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
741a6a5580cSJeff Kirsher 	int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
742a6a5580cSJeff Kirsher 	int promisc = (netdev->flags & IFF_PROMISC) ||
743a6a5580cSJeff Kirsher 		netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
744a6a5580cSJeff Kirsher 	int allmulti = (netdev->flags & IFF_ALLMULTI) ||
745a6a5580cSJeff Kirsher 		netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
746a6a5580cSJeff Kirsher 	unsigned int flags = netdev->flags |
747a6a5580cSJeff Kirsher 		(allmulti ? IFF_ALLMULTI : 0) |
748a6a5580cSJeff Kirsher 		(promisc ? IFF_PROMISC : 0);
749a6a5580cSJeff Kirsher 
750a6a5580cSJeff Kirsher 	if (enic->flags != flags) {
751a6a5580cSJeff Kirsher 		enic->flags = flags;
752a6a5580cSJeff Kirsher 		enic_dev_packet_filter(enic, directed,
753a6a5580cSJeff Kirsher 			multicast, broadcast, promisc, allmulti);
754a6a5580cSJeff Kirsher 	}
755a6a5580cSJeff Kirsher 
756a6a5580cSJeff Kirsher 	if (!promisc) {
757f009618aSAlexander Duyck 		__dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
758a6a5580cSJeff Kirsher 		if (!allmulti)
759f009618aSAlexander Duyck 			__dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
760a6a5580cSJeff Kirsher 	}
761a6a5580cSJeff Kirsher }
762a6a5580cSJeff Kirsher 
763a6a5580cSJeff Kirsher /* netif_tx_lock held, BHs disabled */
764a6a5580cSJeff Kirsher static void enic_tx_timeout(struct net_device *netdev)
765a6a5580cSJeff Kirsher {
766a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
767a6a5580cSJeff Kirsher 	schedule_work(&enic->reset);
768a6a5580cSJeff Kirsher }
769a6a5580cSJeff Kirsher 
770a6a5580cSJeff Kirsher static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
771a6a5580cSJeff Kirsher {
772a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
7733f192795SRoopa Prabhu 	struct enic_port_profile *pp;
7743f192795SRoopa Prabhu 	int err;
775a6a5580cSJeff Kirsher 
7763f192795SRoopa Prabhu 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
7773f192795SRoopa Prabhu 	if (err)
7783f192795SRoopa Prabhu 		return err;
779a6a5580cSJeff Kirsher 
780b8622cbdSRoopa Prabhu 	if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
781b4765833SRoopa Prabhu 		if (vf == PORT_SELF_VF) {
7823f192795SRoopa Prabhu 			memcpy(pp->vf_mac, mac, ETH_ALEN);
783a6a5580cSJeff Kirsher 			return 0;
784b4765833SRoopa Prabhu 		} else {
785b4765833SRoopa Prabhu 			/*
786b4765833SRoopa Prabhu 			 * For sriov vf's set the mac in hw
787b4765833SRoopa Prabhu 			 */
788b4765833SRoopa Prabhu 			ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
789b4765833SRoopa Prabhu 				vnic_dev_set_mac_addr, mac);
790b4765833SRoopa Prabhu 			return enic_dev_status_to_errno(err);
791b4765833SRoopa Prabhu 		}
792a6a5580cSJeff Kirsher 	} else
793a6a5580cSJeff Kirsher 		return -EINVAL;
794a6a5580cSJeff Kirsher }
795a6a5580cSJeff Kirsher 
796a6a5580cSJeff Kirsher static int enic_set_vf_port(struct net_device *netdev, int vf,
797a6a5580cSJeff Kirsher 	struct nlattr *port[])
798a6a5580cSJeff Kirsher {
799a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
800a6a5580cSJeff Kirsher 	struct enic_port_profile prev_pp;
8013f192795SRoopa Prabhu 	struct enic_port_profile *pp;
802a6a5580cSJeff Kirsher 	int err = 0, restore_pp = 1;
803a6a5580cSJeff Kirsher 
8043f192795SRoopa Prabhu 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
8053f192795SRoopa Prabhu 	if (err)
8063f192795SRoopa Prabhu 		return err;
807a6a5580cSJeff Kirsher 
808a6a5580cSJeff Kirsher 	if (!port[IFLA_PORT_REQUEST])
809a6a5580cSJeff Kirsher 		return -EOPNOTSUPP;
810a6a5580cSJeff Kirsher 
8113f192795SRoopa Prabhu 	memcpy(&prev_pp, pp, sizeof(*enic->pp));
8123f192795SRoopa Prabhu 	memset(pp, 0, sizeof(*enic->pp));
813a6a5580cSJeff Kirsher 
8143f192795SRoopa Prabhu 	pp->set |= ENIC_SET_REQUEST;
8153f192795SRoopa Prabhu 	pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
816a6a5580cSJeff Kirsher 
817a6a5580cSJeff Kirsher 	if (port[IFLA_PORT_PROFILE]) {
8183f192795SRoopa Prabhu 		pp->set |= ENIC_SET_NAME;
8193f192795SRoopa Prabhu 		memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
820a6a5580cSJeff Kirsher 			PORT_PROFILE_MAX);
821a6a5580cSJeff Kirsher 	}
822a6a5580cSJeff Kirsher 
823a6a5580cSJeff Kirsher 	if (port[IFLA_PORT_INSTANCE_UUID]) {
8243f192795SRoopa Prabhu 		pp->set |= ENIC_SET_INSTANCE;
8253f192795SRoopa Prabhu 		memcpy(pp->instance_uuid,
826a6a5580cSJeff Kirsher 			nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
827a6a5580cSJeff Kirsher 	}
828a6a5580cSJeff Kirsher 
829a6a5580cSJeff Kirsher 	if (port[IFLA_PORT_HOST_UUID]) {
8303f192795SRoopa Prabhu 		pp->set |= ENIC_SET_HOST;
8313f192795SRoopa Prabhu 		memcpy(pp->host_uuid,
832a6a5580cSJeff Kirsher 			nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
833a6a5580cSJeff Kirsher 	}
834a6a5580cSJeff Kirsher 
835b4765833SRoopa Prabhu 	if (vf == PORT_SELF_VF) {
836a6a5580cSJeff Kirsher 		/* Special case handling: mac came from IFLA_VF_MAC */
837a6a5580cSJeff Kirsher 		if (!is_zero_ether_addr(prev_pp.vf_mac))
8383f192795SRoopa Prabhu 			memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
839a6a5580cSJeff Kirsher 
840b4765833SRoopa Prabhu 		if (is_zero_ether_addr(netdev->dev_addr))
841da194316SDanny Kukawka 			eth_hw_addr_random(netdev);
842b4765833SRoopa Prabhu 	} else {
843b4765833SRoopa Prabhu 		/* SR-IOV VF: get mac from adapter */
844b4765833SRoopa Prabhu 		ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
845b4765833SRoopa Prabhu 			vnic_dev_get_mac_addr, pp->mac_addr);
846b4765833SRoopa Prabhu 		if (err) {
847b4765833SRoopa Prabhu 			netdev_err(netdev, "Error getting mac for vf %d\n", vf);
848b4765833SRoopa Prabhu 			memcpy(pp, &prev_pp, sizeof(*pp));
849b4765833SRoopa Prabhu 			return enic_dev_status_to_errno(err);
850b4765833SRoopa Prabhu 		}
851b4765833SRoopa Prabhu 	}
852a6a5580cSJeff Kirsher 
8533f192795SRoopa Prabhu 	err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
854a6a5580cSJeff Kirsher 	if (err) {
855a6a5580cSJeff Kirsher 		if (restore_pp) {
856a6a5580cSJeff Kirsher 			/* Things are still the way they were: Implicit
857a6a5580cSJeff Kirsher 			 * DISASSOCIATE failed
858a6a5580cSJeff Kirsher 			 */
8593f192795SRoopa Prabhu 			memcpy(pp, &prev_pp, sizeof(*pp));
860a6a5580cSJeff Kirsher 		} else {
8613f192795SRoopa Prabhu 			memset(pp, 0, sizeof(*pp));
8623f192795SRoopa Prabhu 			if (vf == PORT_SELF_VF)
863a6a5580cSJeff Kirsher 				memset(netdev->dev_addr, 0, ETH_ALEN);
864a6a5580cSJeff Kirsher 		}
865a6a5580cSJeff Kirsher 	} else {
866a6a5580cSJeff Kirsher 		/* Set flag to indicate that the port assoc/disassoc
867a6a5580cSJeff Kirsher 		 * request has been sent out to fw
868a6a5580cSJeff Kirsher 		 */
8693f192795SRoopa Prabhu 		pp->set |= ENIC_PORT_REQUEST_APPLIED;
870a6a5580cSJeff Kirsher 
871a6a5580cSJeff Kirsher 		/* If DISASSOCIATE, clean up all assigned/saved macaddresses */
8723f192795SRoopa Prabhu 		if (pp->request == PORT_REQUEST_DISASSOCIATE) {
8733f192795SRoopa Prabhu 			memset(pp->mac_addr, 0, ETH_ALEN);
8743f192795SRoopa Prabhu 			if (vf == PORT_SELF_VF)
875a6a5580cSJeff Kirsher 				memset(netdev->dev_addr, 0, ETH_ALEN);
876a6a5580cSJeff Kirsher 		}
877a6a5580cSJeff Kirsher 	}
878a6a5580cSJeff Kirsher 
879b4765833SRoopa Prabhu 	if (vf == PORT_SELF_VF)
8803f192795SRoopa Prabhu 		memset(pp->vf_mac, 0, ETH_ALEN);
881a6a5580cSJeff Kirsher 
882a6a5580cSJeff Kirsher 	return err;
883a6a5580cSJeff Kirsher }
884a6a5580cSJeff Kirsher 
885a6a5580cSJeff Kirsher static int enic_get_vf_port(struct net_device *netdev, int vf,
886a6a5580cSJeff Kirsher 	struct sk_buff *skb)
887a6a5580cSJeff Kirsher {
888a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
889a6a5580cSJeff Kirsher 	u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
8903f192795SRoopa Prabhu 	struct enic_port_profile *pp;
891a6a5580cSJeff Kirsher 	int err;
892a6a5580cSJeff Kirsher 
8933f192795SRoopa Prabhu 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
894a6a5580cSJeff Kirsher 	if (err)
895a6a5580cSJeff Kirsher 		return err;
896a6a5580cSJeff Kirsher 
8973f192795SRoopa Prabhu 	if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
8983f192795SRoopa Prabhu 		return -ENODATA;
8993f192795SRoopa Prabhu 
9003f192795SRoopa Prabhu 	err = enic_process_get_pp_request(enic, vf, pp->request, &response);
9013f192795SRoopa Prabhu 	if (err)
9023f192795SRoopa Prabhu 		return err;
9033f192795SRoopa Prabhu 
9041a106de6SDavid S. Miller 	if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
9051a106de6SDavid S. Miller 	    nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
9061a106de6SDavid S. Miller 	    ((pp->set & ENIC_SET_NAME) &&
9071a106de6SDavid S. Miller 	     nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
9081a106de6SDavid S. Miller 	    ((pp->set & ENIC_SET_INSTANCE) &&
9091a106de6SDavid S. Miller 	     nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
9101a106de6SDavid S. Miller 		     pp->instance_uuid)) ||
9111a106de6SDavid S. Miller 	    ((pp->set & ENIC_SET_HOST) &&
9121a106de6SDavid S. Miller 	     nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
9131a106de6SDavid S. Miller 		goto nla_put_failure;
914a6a5580cSJeff Kirsher 	return 0;
915a6a5580cSJeff Kirsher 
916a6a5580cSJeff Kirsher nla_put_failure:
917a6a5580cSJeff Kirsher 	return -EMSGSIZE;
918a6a5580cSJeff Kirsher }
919a6a5580cSJeff Kirsher 
920a6a5580cSJeff Kirsher static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
921a6a5580cSJeff Kirsher {
922a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(rq->vdev);
923a6a5580cSJeff Kirsher 
924a6a5580cSJeff Kirsher 	if (!buf->os_buf)
925a6a5580cSJeff Kirsher 		return;
926a6a5580cSJeff Kirsher 
927a6a5580cSJeff Kirsher 	pci_unmap_single(enic->pdev, buf->dma_addr,
928a6a5580cSJeff Kirsher 		buf->len, PCI_DMA_FROMDEVICE);
929a6a5580cSJeff Kirsher 	dev_kfree_skb_any(buf->os_buf);
930a03bb56eSGovindarajulu Varadarajan 	buf->os_buf = NULL;
931a6a5580cSJeff Kirsher }
932a6a5580cSJeff Kirsher 
933a6a5580cSJeff Kirsher static int enic_rq_alloc_buf(struct vnic_rq *rq)
934a6a5580cSJeff Kirsher {
935a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(rq->vdev);
936a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
937a6a5580cSJeff Kirsher 	struct sk_buff *skb;
938a6a5580cSJeff Kirsher 	unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
939a6a5580cSJeff Kirsher 	unsigned int os_buf_index = 0;
940a6a5580cSJeff Kirsher 	dma_addr_t dma_addr;
941a03bb56eSGovindarajulu Varadarajan 	struct vnic_rq_buf *buf = rq->to_use;
942a6a5580cSJeff Kirsher 
943a03bb56eSGovindarajulu Varadarajan 	if (buf->os_buf) {
944f6b7734bSGovindarajulu Varadarajan 		enic_queue_rq_desc(rq, buf->os_buf, os_buf_index, buf->dma_addr,
945f6b7734bSGovindarajulu Varadarajan 				   buf->len);
946a03bb56eSGovindarajulu Varadarajan 
947a03bb56eSGovindarajulu Varadarajan 		return 0;
948a03bb56eSGovindarajulu Varadarajan 	}
949a6a5580cSJeff Kirsher 	skb = netdev_alloc_skb_ip_align(netdev, len);
950a6a5580cSJeff Kirsher 	if (!skb)
951a6a5580cSJeff Kirsher 		return -ENOMEM;
952a6a5580cSJeff Kirsher 
953a6a5580cSJeff Kirsher 	dma_addr = pci_map_single(enic->pdev, skb->data,
954a6a5580cSJeff Kirsher 		len, PCI_DMA_FROMDEVICE);
955a6a5580cSJeff Kirsher 
956a6a5580cSJeff Kirsher 	enic_queue_rq_desc(rq, skb, os_buf_index,
957a6a5580cSJeff Kirsher 		dma_addr, len);
958a6a5580cSJeff Kirsher 
959a6a5580cSJeff Kirsher 	return 0;
960a6a5580cSJeff Kirsher }
961a6a5580cSJeff Kirsher 
9627c2ce6e6SSujith Sankar static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
9637c2ce6e6SSujith Sankar 				      u32 pkt_len)
9647c2ce6e6SSujith Sankar {
9657c2ce6e6SSujith Sankar 	if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
9667c2ce6e6SSujith Sankar 		pkt_size->large_pkt_bytes_cnt += pkt_len;
9677c2ce6e6SSujith Sankar 	else
9687c2ce6e6SSujith Sankar 		pkt_size->small_pkt_bytes_cnt += pkt_len;
9697c2ce6e6SSujith Sankar }
9707c2ce6e6SSujith Sankar 
971a03bb56eSGovindarajulu Varadarajan static bool enic_rxcopybreak(struct net_device *netdev, struct sk_buff **skb,
972a03bb56eSGovindarajulu Varadarajan 			     struct vnic_rq_buf *buf, u16 len)
973a03bb56eSGovindarajulu Varadarajan {
974a03bb56eSGovindarajulu Varadarajan 	struct enic *enic = netdev_priv(netdev);
975a03bb56eSGovindarajulu Varadarajan 	struct sk_buff *new_skb;
976a03bb56eSGovindarajulu Varadarajan 
977a03bb56eSGovindarajulu Varadarajan 	if (len > enic->rx_copybreak)
978a03bb56eSGovindarajulu Varadarajan 		return false;
979a03bb56eSGovindarajulu Varadarajan 	new_skb = netdev_alloc_skb_ip_align(netdev, len);
980a03bb56eSGovindarajulu Varadarajan 	if (!new_skb)
981a03bb56eSGovindarajulu Varadarajan 		return false;
982a03bb56eSGovindarajulu Varadarajan 	pci_dma_sync_single_for_cpu(enic->pdev, buf->dma_addr, len,
983a03bb56eSGovindarajulu Varadarajan 				    DMA_FROM_DEVICE);
984a03bb56eSGovindarajulu Varadarajan 	memcpy(new_skb->data, (*skb)->data, len);
985a03bb56eSGovindarajulu Varadarajan 	*skb = new_skb;
986a03bb56eSGovindarajulu Varadarajan 
987a03bb56eSGovindarajulu Varadarajan 	return true;
988a03bb56eSGovindarajulu Varadarajan }
989a03bb56eSGovindarajulu Varadarajan 
990a6a5580cSJeff Kirsher static void enic_rq_indicate_buf(struct vnic_rq *rq,
991a6a5580cSJeff Kirsher 	struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
992a6a5580cSJeff Kirsher 	int skipped, void *opaque)
993a6a5580cSJeff Kirsher {
994a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(rq->vdev);
995a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
996a6a5580cSJeff Kirsher 	struct sk_buff *skb;
9977c2ce6e6SSujith Sankar 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
998a6a5580cSJeff Kirsher 
999a6a5580cSJeff Kirsher 	u8 type, color, eop, sop, ingress_port, vlan_stripped;
1000a6a5580cSJeff Kirsher 	u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1001a6a5580cSJeff Kirsher 	u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1002a6a5580cSJeff Kirsher 	u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1003a6a5580cSJeff Kirsher 	u8 packet_error;
1004a6a5580cSJeff Kirsher 	u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1005a6a5580cSJeff Kirsher 	u32 rss_hash;
1006a6a5580cSJeff Kirsher 
1007a6a5580cSJeff Kirsher 	if (skipped)
1008a6a5580cSJeff Kirsher 		return;
1009a6a5580cSJeff Kirsher 
1010a6a5580cSJeff Kirsher 	skb = buf->os_buf;
1011a6a5580cSJeff Kirsher 
1012a6a5580cSJeff Kirsher 	cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1013a6a5580cSJeff Kirsher 		&type, &color, &q_number, &completed_index,
1014a6a5580cSJeff Kirsher 		&ingress_port, &fcoe, &eop, &sop, &rss_type,
1015a6a5580cSJeff Kirsher 		&csum_not_calc, &rss_hash, &bytes_written,
1016a6a5580cSJeff Kirsher 		&packet_error, &vlan_stripped, &vlan_tci, &checksum,
1017a6a5580cSJeff Kirsher 		&fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1018a6a5580cSJeff Kirsher 		&fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1019a6a5580cSJeff Kirsher 		&ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1020a6a5580cSJeff Kirsher 		&fcs_ok);
1021a6a5580cSJeff Kirsher 
1022a6a5580cSJeff Kirsher 	if (packet_error) {
1023a6a5580cSJeff Kirsher 
1024a6a5580cSJeff Kirsher 		if (!fcs_ok) {
1025a6a5580cSJeff Kirsher 			if (bytes_written > 0)
1026a6a5580cSJeff Kirsher 				enic->rq_bad_fcs++;
1027a6a5580cSJeff Kirsher 			else if (bytes_written == 0)
1028a6a5580cSJeff Kirsher 				enic->rq_truncated_pkts++;
1029a6a5580cSJeff Kirsher 		}
1030a6a5580cSJeff Kirsher 
103144aa91abSGovindarajulu Varadarajan 		pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
103244aa91abSGovindarajulu Varadarajan 				 PCI_DMA_FROMDEVICE);
1033a6a5580cSJeff Kirsher 		dev_kfree_skb_any(skb);
103444aa91abSGovindarajulu Varadarajan 		buf->os_buf = NULL;
1035a6a5580cSJeff Kirsher 
1036a6a5580cSJeff Kirsher 		return;
1037a6a5580cSJeff Kirsher 	}
1038a6a5580cSJeff Kirsher 
1039a6a5580cSJeff Kirsher 	if (eop && bytes_written > 0) {
1040a6a5580cSJeff Kirsher 
1041a6a5580cSJeff Kirsher 		/* Good receive
1042a6a5580cSJeff Kirsher 		 */
1043a6a5580cSJeff Kirsher 
1044a03bb56eSGovindarajulu Varadarajan 		if (!enic_rxcopybreak(netdev, &skb, buf, bytes_written)) {
1045a03bb56eSGovindarajulu Varadarajan 			buf->os_buf = NULL;
1046a03bb56eSGovindarajulu Varadarajan 			pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1047a03bb56eSGovindarajulu Varadarajan 					 PCI_DMA_FROMDEVICE);
1048a03bb56eSGovindarajulu Varadarajan 		}
1049a03bb56eSGovindarajulu Varadarajan 		prefetch(skb->data - NET_IP_ALIGN);
1050a03bb56eSGovindarajulu Varadarajan 
1051a6a5580cSJeff Kirsher 		skb_put(skb, bytes_written);
1052a6a5580cSJeff Kirsher 		skb->protocol = eth_type_trans(skb, netdev);
1053bf751ba8Sgovindarajulu.v 		skb_record_rx_queue(skb, q_number);
1054bf751ba8Sgovindarajulu.v 		if (netdev->features & NETIF_F_RXHASH) {
10553739acddSTom Herbert 			skb_set_hash(skb, rss_hash,
10563739acddSTom Herbert 				     (rss_type &
10573739acddSTom Herbert 				      (NIC_CFG_RSS_HASH_TYPE_TCP_IPV6_EX |
1058bf751ba8Sgovindarajulu.v 				       NIC_CFG_RSS_HASH_TYPE_TCP_IPV6 |
10593739acddSTom Herbert 				       NIC_CFG_RSS_HASH_TYPE_TCP_IPV4)) ?
10603739acddSTom Herbert 				     PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
1061bf751ba8Sgovindarajulu.v 		}
1062a6a5580cSJeff Kirsher 
1063a6a5580cSJeff Kirsher 		if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
1064a6a5580cSJeff Kirsher 			skb->csum = htons(checksum);
1065a6a5580cSJeff Kirsher 			skb->ip_summed = CHECKSUM_COMPLETE;
1066a6a5580cSJeff Kirsher 		}
1067a6a5580cSJeff Kirsher 
1068a6a5580cSJeff Kirsher 		if (vlan_stripped)
106986a9bad3SPatrick McHardy 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1070a6a5580cSJeff Kirsher 
107114747cd9SGovindarajulu Varadarajan 		skb_mark_napi_id(skb, &enic->napi[rq->index]);
107214747cd9SGovindarajulu Varadarajan 		if (enic_poll_busy_polling(rq) ||
107314747cd9SGovindarajulu Varadarajan 		    !(netdev->features & NETIF_F_GRO))
1074a6a5580cSJeff Kirsher 			netif_receive_skb(skb);
107514747cd9SGovindarajulu Varadarajan 		else
107614747cd9SGovindarajulu Varadarajan 			napi_gro_receive(&enic->napi[q_number], skb);
10777c2ce6e6SSujith Sankar 		if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
10787c2ce6e6SSujith Sankar 			enic_intr_update_pkt_size(&cq->pkt_size_counter,
10797c2ce6e6SSujith Sankar 						  bytes_written);
1080a6a5580cSJeff Kirsher 	} else {
1081a6a5580cSJeff Kirsher 
1082a6a5580cSJeff Kirsher 		/* Buffer overflow
1083a6a5580cSJeff Kirsher 		 */
1084a6a5580cSJeff Kirsher 
108544aa91abSGovindarajulu Varadarajan 		pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
108644aa91abSGovindarajulu Varadarajan 				 PCI_DMA_FROMDEVICE);
1087a6a5580cSJeff Kirsher 		dev_kfree_skb_any(skb);
108844aa91abSGovindarajulu Varadarajan 		buf->os_buf = NULL;
1089a6a5580cSJeff Kirsher 	}
1090a6a5580cSJeff Kirsher }
1091a6a5580cSJeff Kirsher 
1092a6a5580cSJeff Kirsher static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1093a6a5580cSJeff Kirsher 	u8 type, u16 q_number, u16 completed_index, void *opaque)
1094a6a5580cSJeff Kirsher {
1095a6a5580cSJeff Kirsher 	struct enic *enic = vnic_dev_priv(vdev);
1096a6a5580cSJeff Kirsher 
1097a6a5580cSJeff Kirsher 	vnic_rq_service(&enic->rq[q_number], cq_desc,
1098a6a5580cSJeff Kirsher 		completed_index, VNIC_RQ_RETURN_DESC,
1099a6a5580cSJeff Kirsher 		enic_rq_indicate_buf, opaque);
1100a6a5580cSJeff Kirsher 
1101a6a5580cSJeff Kirsher 	return 0;
1102a6a5580cSJeff Kirsher }
1103a6a5580cSJeff Kirsher 
1104a6a5580cSJeff Kirsher static int enic_poll(struct napi_struct *napi, int budget)
1105a6a5580cSJeff Kirsher {
1106a6a5580cSJeff Kirsher 	struct net_device *netdev = napi->dev;
1107a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1108a6a5580cSJeff Kirsher 	unsigned int cq_rq = enic_cq_rq(enic, 0);
1109a6a5580cSJeff Kirsher 	unsigned int cq_wq = enic_cq_wq(enic, 0);
1110a6a5580cSJeff Kirsher 	unsigned int intr = enic_legacy_io_intr();
1111a6a5580cSJeff Kirsher 	unsigned int rq_work_to_do = budget;
1112a6a5580cSJeff Kirsher 	unsigned int wq_work_to_do = -1; /* no limit */
11134c502549SEric W. Biederman 	unsigned int  work_done, rq_work_done = 0, wq_work_done;
1114a6a5580cSJeff Kirsher 	int err;
1115a6a5580cSJeff Kirsher 
111614747cd9SGovindarajulu Varadarajan 	wq_work_done = vnic_cq_service(&enic->cq[cq_wq], wq_work_to_do,
111714747cd9SGovindarajulu Varadarajan 				       enic_wq_service, NULL);
111814747cd9SGovindarajulu Varadarajan 
111914747cd9SGovindarajulu Varadarajan 	if (!enic_poll_lock_napi(&enic->rq[cq_rq])) {
112014747cd9SGovindarajulu Varadarajan 		if (wq_work_done > 0)
112114747cd9SGovindarajulu Varadarajan 			vnic_intr_return_credits(&enic->intr[intr],
112214747cd9SGovindarajulu Varadarajan 						 wq_work_done,
112314747cd9SGovindarajulu Varadarajan 						 0 /* dont unmask intr */,
112414747cd9SGovindarajulu Varadarajan 						 0 /* dont reset intr timer */);
112514747cd9SGovindarajulu Varadarajan 		return rq_work_done;
112614747cd9SGovindarajulu Varadarajan 	}
1127a6a5580cSJeff Kirsher 
11284c502549SEric W. Biederman 	if (budget > 0)
1129a6a5580cSJeff Kirsher 		rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1130a6a5580cSJeff Kirsher 			rq_work_to_do, enic_rq_service, NULL);
1131a6a5580cSJeff Kirsher 
1132a6a5580cSJeff Kirsher 	/* Accumulate intr event credits for this polling
1133a6a5580cSJeff Kirsher 	 * cycle.  An intr event is the completion of a
1134a6a5580cSJeff Kirsher 	 * a WQ or RQ packet.
1135a6a5580cSJeff Kirsher 	 */
1136a6a5580cSJeff Kirsher 
1137a6a5580cSJeff Kirsher 	work_done = rq_work_done + wq_work_done;
1138a6a5580cSJeff Kirsher 
1139a6a5580cSJeff Kirsher 	if (work_done > 0)
1140a6a5580cSJeff Kirsher 		vnic_intr_return_credits(&enic->intr[intr],
1141a6a5580cSJeff Kirsher 			work_done,
1142a6a5580cSJeff Kirsher 			0 /* don't unmask intr */,
1143a6a5580cSJeff Kirsher 			0 /* don't reset intr timer */);
1144a6a5580cSJeff Kirsher 
1145a6a5580cSJeff Kirsher 	err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1146a6a5580cSJeff Kirsher 
1147a6a5580cSJeff Kirsher 	/* Buffer allocation failed. Stay in polling
1148a6a5580cSJeff Kirsher 	 * mode so we can try to fill the ring again.
1149a6a5580cSJeff Kirsher 	 */
1150a6a5580cSJeff Kirsher 
1151a6a5580cSJeff Kirsher 	if (err)
1152a6a5580cSJeff Kirsher 		rq_work_done = rq_work_to_do;
1153a6a5580cSJeff Kirsher 
1154a6a5580cSJeff Kirsher 	if (rq_work_done < rq_work_to_do) {
1155a6a5580cSJeff Kirsher 
1156a6a5580cSJeff Kirsher 		/* Some work done, but not enough to stay in polling,
1157a6a5580cSJeff Kirsher 		 * exit polling
1158a6a5580cSJeff Kirsher 		 */
1159a6a5580cSJeff Kirsher 
1160a6a5580cSJeff Kirsher 		napi_complete(napi);
1161a6a5580cSJeff Kirsher 		vnic_intr_unmask(&enic->intr[intr]);
1162a6a5580cSJeff Kirsher 	}
116314747cd9SGovindarajulu Varadarajan 	enic_poll_unlock_napi(&enic->rq[cq_rq]);
1164a6a5580cSJeff Kirsher 
1165a6a5580cSJeff Kirsher 	return rq_work_done;
1166a6a5580cSJeff Kirsher }
1167a6a5580cSJeff Kirsher 
11687c2ce6e6SSujith Sankar static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
11697c2ce6e6SSujith Sankar {
11707c2ce6e6SSujith Sankar 	unsigned int intr = enic_msix_rq_intr(enic, rq->index);
11717c2ce6e6SSujith Sankar 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
11727c2ce6e6SSujith Sankar 	u32 timer = cq->tobe_rx_coal_timeval;
11737c2ce6e6SSujith Sankar 
11747c2ce6e6SSujith Sankar 	if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
11757c2ce6e6SSujith Sankar 		vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
11767c2ce6e6SSujith Sankar 		cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
11777c2ce6e6SSujith Sankar 	}
11787c2ce6e6SSujith Sankar }
11797c2ce6e6SSujith Sankar 
11807c2ce6e6SSujith Sankar static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
11817c2ce6e6SSujith Sankar {
11827c2ce6e6SSujith Sankar 	struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
11837c2ce6e6SSujith Sankar 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
11847c2ce6e6SSujith Sankar 	struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
11857c2ce6e6SSujith Sankar 	int index;
11867c2ce6e6SSujith Sankar 	u32 timer;
11877c2ce6e6SSujith Sankar 	u32 range_start;
11887c2ce6e6SSujith Sankar 	u32 traffic;
11897c2ce6e6SSujith Sankar 	u64 delta;
11907c2ce6e6SSujith Sankar 	ktime_t now = ktime_get();
11917c2ce6e6SSujith Sankar 
11927c2ce6e6SSujith Sankar 	delta = ktime_us_delta(now, cq->prev_ts);
11937c2ce6e6SSujith Sankar 	if (delta < ENIC_AIC_TS_BREAK)
11947c2ce6e6SSujith Sankar 		return;
11957c2ce6e6SSujith Sankar 	cq->prev_ts = now;
11967c2ce6e6SSujith Sankar 
11977c2ce6e6SSujith Sankar 	traffic = pkt_size_counter->large_pkt_bytes_cnt +
11987c2ce6e6SSujith Sankar 		  pkt_size_counter->small_pkt_bytes_cnt;
11997c2ce6e6SSujith Sankar 	/* The table takes Mbps
12007c2ce6e6SSujith Sankar 	 * traffic *= 8    => bits
12017c2ce6e6SSujith Sankar 	 * traffic *= (10^6 / delta)    => bps
12027c2ce6e6SSujith Sankar 	 * traffic /= 10^6     => Mbps
12037c2ce6e6SSujith Sankar 	 *
12047c2ce6e6SSujith Sankar 	 * Combining, traffic *= (8 / delta)
12057c2ce6e6SSujith Sankar 	 */
12067c2ce6e6SSujith Sankar 
12077c2ce6e6SSujith Sankar 	traffic <<= 3;
1208958c492cSGovindarajulu Varadarajan 	traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
12097c2ce6e6SSujith Sankar 
12107c2ce6e6SSujith Sankar 	for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
12117c2ce6e6SSujith Sankar 		if (traffic < mod_table[index].rx_rate)
12127c2ce6e6SSujith Sankar 			break;
12137c2ce6e6SSujith Sankar 	range_start = (pkt_size_counter->small_pkt_bytes_cnt >
12147c2ce6e6SSujith Sankar 		       pkt_size_counter->large_pkt_bytes_cnt << 1) ?
12157c2ce6e6SSujith Sankar 		      rx_coal->small_pkt_range_start :
12167c2ce6e6SSujith Sankar 		      rx_coal->large_pkt_range_start;
12177c2ce6e6SSujith Sankar 	timer = range_start + ((rx_coal->range_end - range_start) *
12187c2ce6e6SSujith Sankar 			       mod_table[index].range_percent / 100);
12197c2ce6e6SSujith Sankar 	/* Damping */
12207c2ce6e6SSujith Sankar 	cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
12217c2ce6e6SSujith Sankar 
12227c2ce6e6SSujith Sankar 	pkt_size_counter->large_pkt_bytes_cnt = 0;
12237c2ce6e6SSujith Sankar 	pkt_size_counter->small_pkt_bytes_cnt = 0;
12247c2ce6e6SSujith Sankar }
12257c2ce6e6SSujith Sankar 
1226b6e97c13SGovindarajulu Varadarajan #ifdef CONFIG_RFS_ACCEL
1227b6e97c13SGovindarajulu Varadarajan static void enic_free_rx_cpu_rmap(struct enic *enic)
1228b6e97c13SGovindarajulu Varadarajan {
1229b6e97c13SGovindarajulu Varadarajan 	free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
1230b6e97c13SGovindarajulu Varadarajan 	enic->netdev->rx_cpu_rmap = NULL;
1231b6e97c13SGovindarajulu Varadarajan }
1232b6e97c13SGovindarajulu Varadarajan 
1233b6e97c13SGovindarajulu Varadarajan static void enic_set_rx_cpu_rmap(struct enic *enic)
1234b6e97c13SGovindarajulu Varadarajan {
1235b6e97c13SGovindarajulu Varadarajan 	int i, res;
1236b6e97c13SGovindarajulu Varadarajan 
1237b6e97c13SGovindarajulu Varadarajan 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
1238b6e97c13SGovindarajulu Varadarajan 		enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
1239b6e97c13SGovindarajulu Varadarajan 		if (unlikely(!enic->netdev->rx_cpu_rmap))
1240b6e97c13SGovindarajulu Varadarajan 			return;
1241b6e97c13SGovindarajulu Varadarajan 		for (i = 0; i < enic->rq_count; i++) {
1242b6e97c13SGovindarajulu Varadarajan 			res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
1243b6e97c13SGovindarajulu Varadarajan 					       enic->msix_entry[i].vector);
1244b6e97c13SGovindarajulu Varadarajan 			if (unlikely(res)) {
1245b6e97c13SGovindarajulu Varadarajan 				enic_free_rx_cpu_rmap(enic);
1246b6e97c13SGovindarajulu Varadarajan 				return;
1247b6e97c13SGovindarajulu Varadarajan 			}
1248b6e97c13SGovindarajulu Varadarajan 		}
1249b6e97c13SGovindarajulu Varadarajan 	}
1250b6e97c13SGovindarajulu Varadarajan }
1251b6e97c13SGovindarajulu Varadarajan 
1252b6e97c13SGovindarajulu Varadarajan #else
1253b6e97c13SGovindarajulu Varadarajan 
1254b6e97c13SGovindarajulu Varadarajan static void enic_free_rx_cpu_rmap(struct enic *enic)
1255b6e97c13SGovindarajulu Varadarajan {
1256b6e97c13SGovindarajulu Varadarajan }
1257b6e97c13SGovindarajulu Varadarajan 
1258b6e97c13SGovindarajulu Varadarajan static void enic_set_rx_cpu_rmap(struct enic *enic)
1259b6e97c13SGovindarajulu Varadarajan {
1260b6e97c13SGovindarajulu Varadarajan }
1261b6e97c13SGovindarajulu Varadarajan 
1262b6e97c13SGovindarajulu Varadarajan #endif /* CONFIG_RFS_ACCEL */
1263b6e97c13SGovindarajulu Varadarajan 
126414747cd9SGovindarajulu Varadarajan #ifdef CONFIG_NET_RX_BUSY_POLL
126514747cd9SGovindarajulu Varadarajan int enic_busy_poll(struct napi_struct *napi)
126614747cd9SGovindarajulu Varadarajan {
126714747cd9SGovindarajulu Varadarajan 	struct net_device *netdev = napi->dev;
126814747cd9SGovindarajulu Varadarajan 	struct enic *enic = netdev_priv(netdev);
126914747cd9SGovindarajulu Varadarajan 	unsigned int rq = (napi - &enic->napi[0]);
127014747cd9SGovindarajulu Varadarajan 	unsigned int cq = enic_cq_rq(enic, rq);
127114747cd9SGovindarajulu Varadarajan 	unsigned int intr = enic_msix_rq_intr(enic, rq);
127214747cd9SGovindarajulu Varadarajan 	unsigned int work_to_do = -1; /* clean all pkts possible */
127314747cd9SGovindarajulu Varadarajan 	unsigned int work_done;
127414747cd9SGovindarajulu Varadarajan 
127514747cd9SGovindarajulu Varadarajan 	if (!enic_poll_lock_poll(&enic->rq[rq]))
127614747cd9SGovindarajulu Varadarajan 		return LL_FLUSH_BUSY;
127714747cd9SGovindarajulu Varadarajan 	work_done = vnic_cq_service(&enic->cq[cq], work_to_do,
127814747cd9SGovindarajulu Varadarajan 				    enic_rq_service, NULL);
127914747cd9SGovindarajulu Varadarajan 
128014747cd9SGovindarajulu Varadarajan 	if (work_done > 0)
128114747cd9SGovindarajulu Varadarajan 		vnic_intr_return_credits(&enic->intr[intr],
128214747cd9SGovindarajulu Varadarajan 					 work_done, 0, 0);
128314747cd9SGovindarajulu Varadarajan 	vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
128414747cd9SGovindarajulu Varadarajan 	if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
128514747cd9SGovindarajulu Varadarajan 		enic_calc_int_moderation(enic, &enic->rq[rq]);
128614747cd9SGovindarajulu Varadarajan 	enic_poll_unlock_poll(&enic->rq[rq]);
128714747cd9SGovindarajulu Varadarajan 
128814747cd9SGovindarajulu Varadarajan 	return work_done;
128914747cd9SGovindarajulu Varadarajan }
129014747cd9SGovindarajulu Varadarajan #endif /* CONFIG_NET_RX_BUSY_POLL */
129114747cd9SGovindarajulu Varadarajan 
12924cfe8785SGovindarajulu Varadarajan static int enic_poll_msix_wq(struct napi_struct *napi, int budget)
12934cfe8785SGovindarajulu Varadarajan {
12944cfe8785SGovindarajulu Varadarajan 	struct net_device *netdev = napi->dev;
12954cfe8785SGovindarajulu Varadarajan 	struct enic *enic = netdev_priv(netdev);
12964cfe8785SGovindarajulu Varadarajan 	unsigned int wq_index = (napi - &enic->napi[0]) - enic->rq_count;
12974cfe8785SGovindarajulu Varadarajan 	struct vnic_wq *wq = &enic->wq[wq_index];
12984cfe8785SGovindarajulu Varadarajan 	unsigned int cq;
12994cfe8785SGovindarajulu Varadarajan 	unsigned int intr;
13004cfe8785SGovindarajulu Varadarajan 	unsigned int wq_work_to_do = -1; /* clean all desc possible */
13014cfe8785SGovindarajulu Varadarajan 	unsigned int wq_work_done;
13024cfe8785SGovindarajulu Varadarajan 	unsigned int wq_irq;
13034cfe8785SGovindarajulu Varadarajan 
13044cfe8785SGovindarajulu Varadarajan 	wq_irq = wq->index;
13054cfe8785SGovindarajulu Varadarajan 	cq = enic_cq_wq(enic, wq_irq);
13064cfe8785SGovindarajulu Varadarajan 	intr = enic_msix_wq_intr(enic, wq_irq);
13074cfe8785SGovindarajulu Varadarajan 	wq_work_done = vnic_cq_service(&enic->cq[cq], wq_work_to_do,
13084cfe8785SGovindarajulu Varadarajan 				       enic_wq_service, NULL);
13094cfe8785SGovindarajulu Varadarajan 
13104cfe8785SGovindarajulu Varadarajan 	vnic_intr_return_credits(&enic->intr[intr], wq_work_done,
13114cfe8785SGovindarajulu Varadarajan 				 0 /* don't unmask intr */,
13124cfe8785SGovindarajulu Varadarajan 				 1 /* reset intr timer */);
13134cfe8785SGovindarajulu Varadarajan 	if (!wq_work_done) {
13144cfe8785SGovindarajulu Varadarajan 		napi_complete(napi);
13154cfe8785SGovindarajulu Varadarajan 		vnic_intr_unmask(&enic->intr[intr]);
1316f41281d0SGovindarajulu Varadarajan 		return 0;
13174cfe8785SGovindarajulu Varadarajan 	}
13184cfe8785SGovindarajulu Varadarajan 
1319f41281d0SGovindarajulu Varadarajan 	return budget;
13204cfe8785SGovindarajulu Varadarajan }
13214cfe8785SGovindarajulu Varadarajan 
13224cfe8785SGovindarajulu Varadarajan static int enic_poll_msix_rq(struct napi_struct *napi, int budget)
1323a6a5580cSJeff Kirsher {
1324a6a5580cSJeff Kirsher 	struct net_device *netdev = napi->dev;
1325a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1326a6a5580cSJeff Kirsher 	unsigned int rq = (napi - &enic->napi[0]);
1327a6a5580cSJeff Kirsher 	unsigned int cq = enic_cq_rq(enic, rq);
1328a6a5580cSJeff Kirsher 	unsigned int intr = enic_msix_rq_intr(enic, rq);
1329a6a5580cSJeff Kirsher 	unsigned int work_to_do = budget;
13304c502549SEric W. Biederman 	unsigned int work_done = 0;
1331a6a5580cSJeff Kirsher 	int err;
1332a6a5580cSJeff Kirsher 
133314747cd9SGovindarajulu Varadarajan 	if (!enic_poll_lock_napi(&enic->rq[rq]))
133414747cd9SGovindarajulu Varadarajan 		return work_done;
1335a6a5580cSJeff Kirsher 	/* Service RQ
1336a6a5580cSJeff Kirsher 	 */
1337a6a5580cSJeff Kirsher 
13384c502549SEric W. Biederman 	if (budget > 0)
1339a6a5580cSJeff Kirsher 		work_done = vnic_cq_service(&enic->cq[cq],
1340a6a5580cSJeff Kirsher 			work_to_do, enic_rq_service, NULL);
1341a6a5580cSJeff Kirsher 
1342a6a5580cSJeff Kirsher 	/* Return intr event credits for this polling
1343a6a5580cSJeff Kirsher 	 * cycle.  An intr event is the completion of a
1344a6a5580cSJeff Kirsher 	 * RQ packet.
1345a6a5580cSJeff Kirsher 	 */
1346a6a5580cSJeff Kirsher 
1347a6a5580cSJeff Kirsher 	if (work_done > 0)
1348a6a5580cSJeff Kirsher 		vnic_intr_return_credits(&enic->intr[intr],
1349a6a5580cSJeff Kirsher 			work_done,
1350a6a5580cSJeff Kirsher 			0 /* don't unmask intr */,
1351a6a5580cSJeff Kirsher 			0 /* don't reset intr timer */);
1352a6a5580cSJeff Kirsher 
1353a6a5580cSJeff Kirsher 	err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1354a6a5580cSJeff Kirsher 
1355a6a5580cSJeff Kirsher 	/* Buffer allocation failed. Stay in polling mode
1356a6a5580cSJeff Kirsher 	 * so we can try to fill the ring again.
1357a6a5580cSJeff Kirsher 	 */
1358a6a5580cSJeff Kirsher 
1359a6a5580cSJeff Kirsher 	if (err)
1360a6a5580cSJeff Kirsher 		work_done = work_to_do;
13617c2ce6e6SSujith Sankar 	if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
13627c2ce6e6SSujith Sankar 		/* Call the function which refreshes
13637c2ce6e6SSujith Sankar 		 * the intr coalescing timer value based on
13647c2ce6e6SSujith Sankar 		 * the traffic.  This is supported only in
13657c2ce6e6SSujith Sankar 		 * the case of MSI-x mode
13667c2ce6e6SSujith Sankar 		 */
13677c2ce6e6SSujith Sankar 		enic_calc_int_moderation(enic, &enic->rq[rq]);
1368a6a5580cSJeff Kirsher 
1369a6a5580cSJeff Kirsher 	if (work_done < work_to_do) {
1370a6a5580cSJeff Kirsher 
1371a6a5580cSJeff Kirsher 		/* Some work done, but not enough to stay in polling,
1372a6a5580cSJeff Kirsher 		 * exit polling
1373a6a5580cSJeff Kirsher 		 */
1374a6a5580cSJeff Kirsher 
1375a6a5580cSJeff Kirsher 		napi_complete(napi);
13767c2ce6e6SSujith Sankar 		if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
13777c2ce6e6SSujith Sankar 			enic_set_int_moderation(enic, &enic->rq[rq]);
1378a6a5580cSJeff Kirsher 		vnic_intr_unmask(&enic->intr[intr]);
1379a6a5580cSJeff Kirsher 	}
138014747cd9SGovindarajulu Varadarajan 	enic_poll_unlock_napi(&enic->rq[rq]);
1381a6a5580cSJeff Kirsher 
1382a6a5580cSJeff Kirsher 	return work_done;
1383a6a5580cSJeff Kirsher }
1384a6a5580cSJeff Kirsher 
1385a6a5580cSJeff Kirsher static void enic_notify_timer(unsigned long data)
1386a6a5580cSJeff Kirsher {
1387a6a5580cSJeff Kirsher 	struct enic *enic = (struct enic *)data;
1388a6a5580cSJeff Kirsher 
1389a6a5580cSJeff Kirsher 	enic_notify_check(enic);
1390a6a5580cSJeff Kirsher 
1391a6a5580cSJeff Kirsher 	mod_timer(&enic->notify_timer,
1392a6a5580cSJeff Kirsher 		round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1393a6a5580cSJeff Kirsher }
1394a6a5580cSJeff Kirsher 
1395a6a5580cSJeff Kirsher static void enic_free_intr(struct enic *enic)
1396a6a5580cSJeff Kirsher {
1397a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
1398a6a5580cSJeff Kirsher 	unsigned int i;
1399a6a5580cSJeff Kirsher 
1400b6e97c13SGovindarajulu Varadarajan 	enic_free_rx_cpu_rmap(enic);
1401a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1402a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_INTX:
1403a6a5580cSJeff Kirsher 		free_irq(enic->pdev->irq, netdev);
1404a6a5580cSJeff Kirsher 		break;
1405a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
1406a6a5580cSJeff Kirsher 		free_irq(enic->pdev->irq, enic);
1407a6a5580cSJeff Kirsher 		break;
1408a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
1409a6a5580cSJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1410a6a5580cSJeff Kirsher 			if (enic->msix[i].requested)
1411a6a5580cSJeff Kirsher 				free_irq(enic->msix_entry[i].vector,
1412a6a5580cSJeff Kirsher 					enic->msix[i].devid);
1413a6a5580cSJeff Kirsher 		break;
1414a6a5580cSJeff Kirsher 	default:
1415a6a5580cSJeff Kirsher 		break;
1416a6a5580cSJeff Kirsher 	}
1417a6a5580cSJeff Kirsher }
1418a6a5580cSJeff Kirsher 
1419a6a5580cSJeff Kirsher static int enic_request_intr(struct enic *enic)
1420a6a5580cSJeff Kirsher {
1421a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
1422a6a5580cSJeff Kirsher 	unsigned int i, intr;
1423a6a5580cSJeff Kirsher 	int err = 0;
1424a6a5580cSJeff Kirsher 
1425b6e97c13SGovindarajulu Varadarajan 	enic_set_rx_cpu_rmap(enic);
1426a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1427a6a5580cSJeff Kirsher 
1428a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_INTX:
1429a6a5580cSJeff Kirsher 
1430a6a5580cSJeff Kirsher 		err = request_irq(enic->pdev->irq, enic_isr_legacy,
1431a6a5580cSJeff Kirsher 			IRQF_SHARED, netdev->name, netdev);
1432a6a5580cSJeff Kirsher 		break;
1433a6a5580cSJeff Kirsher 
1434a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
1435a6a5580cSJeff Kirsher 
1436a6a5580cSJeff Kirsher 		err = request_irq(enic->pdev->irq, enic_isr_msi,
1437a6a5580cSJeff Kirsher 			0, netdev->name, enic);
1438a6a5580cSJeff Kirsher 		break;
1439a6a5580cSJeff Kirsher 
1440a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
1441a6a5580cSJeff Kirsher 
1442a6a5580cSJeff Kirsher 		for (i = 0; i < enic->rq_count; i++) {
1443a6a5580cSJeff Kirsher 			intr = enic_msix_rq_intr(enic, i);
14444505f40aSDan Carpenter 			snprintf(enic->msix[intr].devname,
14454505f40aSDan Carpenter 				sizeof(enic->msix[intr].devname),
1446a6a5580cSJeff Kirsher 				"%.11s-rx-%d", netdev->name, i);
14474cfe8785SGovindarajulu Varadarajan 			enic->msix[intr].isr = enic_isr_msix;
1448a6a5580cSJeff Kirsher 			enic->msix[intr].devid = &enic->napi[i];
1449a6a5580cSJeff Kirsher 		}
1450a6a5580cSJeff Kirsher 
1451a6a5580cSJeff Kirsher 		for (i = 0; i < enic->wq_count; i++) {
14524cfe8785SGovindarajulu Varadarajan 			int wq = enic_cq_wq(enic, i);
14534cfe8785SGovindarajulu Varadarajan 
1454a6a5580cSJeff Kirsher 			intr = enic_msix_wq_intr(enic, i);
14554505f40aSDan Carpenter 			snprintf(enic->msix[intr].devname,
14564505f40aSDan Carpenter 				sizeof(enic->msix[intr].devname),
1457a6a5580cSJeff Kirsher 				"%.11s-tx-%d", netdev->name, i);
14584cfe8785SGovindarajulu Varadarajan 			enic->msix[intr].isr = enic_isr_msix;
14594cfe8785SGovindarajulu Varadarajan 			enic->msix[intr].devid = &enic->napi[wq];
1460a6a5580cSJeff Kirsher 		}
1461a6a5580cSJeff Kirsher 
1462a6a5580cSJeff Kirsher 		intr = enic_msix_err_intr(enic);
14634505f40aSDan Carpenter 		snprintf(enic->msix[intr].devname,
14644505f40aSDan Carpenter 			sizeof(enic->msix[intr].devname),
1465a6a5580cSJeff Kirsher 			"%.11s-err", netdev->name);
1466a6a5580cSJeff Kirsher 		enic->msix[intr].isr = enic_isr_msix_err;
1467a6a5580cSJeff Kirsher 		enic->msix[intr].devid = enic;
1468a6a5580cSJeff Kirsher 
1469a6a5580cSJeff Kirsher 		intr = enic_msix_notify_intr(enic);
14704505f40aSDan Carpenter 		snprintf(enic->msix[intr].devname,
14714505f40aSDan Carpenter 			sizeof(enic->msix[intr].devname),
1472a6a5580cSJeff Kirsher 			"%.11s-notify", netdev->name);
1473a6a5580cSJeff Kirsher 		enic->msix[intr].isr = enic_isr_msix_notify;
1474a6a5580cSJeff Kirsher 		enic->msix[intr].devid = enic;
1475a6a5580cSJeff Kirsher 
1476a6a5580cSJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1477a6a5580cSJeff Kirsher 			enic->msix[i].requested = 0;
1478a6a5580cSJeff Kirsher 
1479a6a5580cSJeff Kirsher 		for (i = 0; i < enic->intr_count; i++) {
1480a6a5580cSJeff Kirsher 			err = request_irq(enic->msix_entry[i].vector,
1481a6a5580cSJeff Kirsher 				enic->msix[i].isr, 0,
1482a6a5580cSJeff Kirsher 				enic->msix[i].devname,
1483a6a5580cSJeff Kirsher 				enic->msix[i].devid);
1484a6a5580cSJeff Kirsher 			if (err) {
1485a6a5580cSJeff Kirsher 				enic_free_intr(enic);
1486a6a5580cSJeff Kirsher 				break;
1487a6a5580cSJeff Kirsher 			}
1488a6a5580cSJeff Kirsher 			enic->msix[i].requested = 1;
1489a6a5580cSJeff Kirsher 		}
1490a6a5580cSJeff Kirsher 
1491a6a5580cSJeff Kirsher 		break;
1492a6a5580cSJeff Kirsher 
1493a6a5580cSJeff Kirsher 	default:
1494a6a5580cSJeff Kirsher 		break;
1495a6a5580cSJeff Kirsher 	}
1496a6a5580cSJeff Kirsher 
1497a6a5580cSJeff Kirsher 	return err;
1498a6a5580cSJeff Kirsher }
1499a6a5580cSJeff Kirsher 
1500a6a5580cSJeff Kirsher static void enic_synchronize_irqs(struct enic *enic)
1501a6a5580cSJeff Kirsher {
1502a6a5580cSJeff Kirsher 	unsigned int i;
1503a6a5580cSJeff Kirsher 
1504a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1505a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_INTX:
1506a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
1507a6a5580cSJeff Kirsher 		synchronize_irq(enic->pdev->irq);
1508a6a5580cSJeff Kirsher 		break;
1509a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
1510a6a5580cSJeff Kirsher 		for (i = 0; i < enic->intr_count; i++)
1511a6a5580cSJeff Kirsher 			synchronize_irq(enic->msix_entry[i].vector);
1512a6a5580cSJeff Kirsher 		break;
1513a6a5580cSJeff Kirsher 	default:
1514a6a5580cSJeff Kirsher 		break;
1515a6a5580cSJeff Kirsher 	}
1516a6a5580cSJeff Kirsher }
1517a6a5580cSJeff Kirsher 
15187c2ce6e6SSujith Sankar static void enic_set_rx_coal_setting(struct enic *enic)
15197c2ce6e6SSujith Sankar {
15207c2ce6e6SSujith Sankar 	unsigned int speed;
15217c2ce6e6SSujith Sankar 	int index = -1;
15227c2ce6e6SSujith Sankar 	struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
15237c2ce6e6SSujith Sankar 
15247c2ce6e6SSujith Sankar 	/* If intr mode is not MSIX, do not do adaptive coalescing */
15257c2ce6e6SSujith Sankar 	if (VNIC_DEV_INTR_MODE_MSIX != vnic_dev_get_intr_mode(enic->vdev)) {
15267c2ce6e6SSujith Sankar 		netdev_info(enic->netdev, "INTR mode is not MSIX, Not initializing adaptive coalescing");
15277c2ce6e6SSujith Sankar 		return;
15287c2ce6e6SSujith Sankar 	}
15297c2ce6e6SSujith Sankar 
15307c2ce6e6SSujith Sankar 	/* 1. Read the link speed from fw
15317c2ce6e6SSujith Sankar 	 * 2. Pick the default range for the speed
15327c2ce6e6SSujith Sankar 	 * 3. Update it in enic->rx_coalesce_setting
15337c2ce6e6SSujith Sankar 	 */
15347c2ce6e6SSujith Sankar 	speed = vnic_dev_port_speed(enic->vdev);
15357c2ce6e6SSujith Sankar 	if (ENIC_LINK_SPEED_10G < speed)
15367c2ce6e6SSujith Sankar 		index = ENIC_LINK_40G_INDEX;
15377c2ce6e6SSujith Sankar 	else if (ENIC_LINK_SPEED_4G < speed)
15387c2ce6e6SSujith Sankar 		index = ENIC_LINK_10G_INDEX;
15397c2ce6e6SSujith Sankar 	else
15407c2ce6e6SSujith Sankar 		index = ENIC_LINK_4G_INDEX;
15417c2ce6e6SSujith Sankar 
15427c2ce6e6SSujith Sankar 	rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
15437c2ce6e6SSujith Sankar 	rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
15447c2ce6e6SSujith Sankar 	rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
15457c2ce6e6SSujith Sankar 
15467c2ce6e6SSujith Sankar 	/* Start with the value provided by UCSM */
15477c2ce6e6SSujith Sankar 	for (index = 0; index < enic->rq_count; index++)
15487c2ce6e6SSujith Sankar 		enic->cq[index].cur_rx_coal_timeval =
15497c2ce6e6SSujith Sankar 				enic->config.intr_timer_usec;
15507c2ce6e6SSujith Sankar 
15517c2ce6e6SSujith Sankar 	rx_coal->use_adaptive_rx_coalesce = 1;
15527c2ce6e6SSujith Sankar }
15537c2ce6e6SSujith Sankar 
1554a6a5580cSJeff Kirsher static int enic_dev_notify_set(struct enic *enic)
1555a6a5580cSJeff Kirsher {
1556a6a5580cSJeff Kirsher 	int err;
1557a6a5580cSJeff Kirsher 
15588e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1559a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1560a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_INTX:
1561a6a5580cSJeff Kirsher 		err = vnic_dev_notify_set(enic->vdev,
1562a6a5580cSJeff Kirsher 			enic_legacy_notify_intr());
1563a6a5580cSJeff Kirsher 		break;
1564a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
1565a6a5580cSJeff Kirsher 		err = vnic_dev_notify_set(enic->vdev,
1566a6a5580cSJeff Kirsher 			enic_msix_notify_intr(enic));
1567a6a5580cSJeff Kirsher 		break;
1568a6a5580cSJeff Kirsher 	default:
1569a6a5580cSJeff Kirsher 		err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1570a6a5580cSJeff Kirsher 		break;
1571a6a5580cSJeff Kirsher 	}
15728e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1573a6a5580cSJeff Kirsher 
1574a6a5580cSJeff Kirsher 	return err;
1575a6a5580cSJeff Kirsher }
1576a6a5580cSJeff Kirsher 
1577a6a5580cSJeff Kirsher static void enic_notify_timer_start(struct enic *enic)
1578a6a5580cSJeff Kirsher {
1579a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1580a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
1581a6a5580cSJeff Kirsher 		mod_timer(&enic->notify_timer, jiffies);
1582a6a5580cSJeff Kirsher 		break;
1583a6a5580cSJeff Kirsher 	default:
1584a6a5580cSJeff Kirsher 		/* Using intr for notification for INTx/MSI-X */
1585a6a5580cSJeff Kirsher 		break;
1586a6a5580cSJeff Kirsher 	}
1587a6a5580cSJeff Kirsher }
1588a6a5580cSJeff Kirsher 
1589a6a5580cSJeff Kirsher /* rtnl lock is held, process context */
1590a6a5580cSJeff Kirsher static int enic_open(struct net_device *netdev)
1591a6a5580cSJeff Kirsher {
1592a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1593a6a5580cSJeff Kirsher 	unsigned int i;
1594a6a5580cSJeff Kirsher 	int err;
1595a6a5580cSJeff Kirsher 
1596a6a5580cSJeff Kirsher 	err = enic_request_intr(enic);
1597a6a5580cSJeff Kirsher 	if (err) {
1598a6a5580cSJeff Kirsher 		netdev_err(netdev, "Unable to request irq.\n");
1599a6a5580cSJeff Kirsher 		return err;
1600a6a5580cSJeff Kirsher 	}
1601a6a5580cSJeff Kirsher 
1602a6a5580cSJeff Kirsher 	err = enic_dev_notify_set(enic);
1603a6a5580cSJeff Kirsher 	if (err) {
1604a6a5580cSJeff Kirsher 		netdev_err(netdev,
1605a6a5580cSJeff Kirsher 			"Failed to alloc notify buffer, aborting.\n");
1606a6a5580cSJeff Kirsher 		goto err_out_free_intr;
1607a6a5580cSJeff Kirsher 	}
1608a6a5580cSJeff Kirsher 
1609a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++) {
1610a6a5580cSJeff Kirsher 		vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1611a6a5580cSJeff Kirsher 		/* Need at least one buffer on ring to get going */
1612a6a5580cSJeff Kirsher 		if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1613a6a5580cSJeff Kirsher 			netdev_err(netdev, "Unable to alloc receive buffers\n");
1614a6a5580cSJeff Kirsher 			err = -ENOMEM;
1615a6a5580cSJeff Kirsher 			goto err_out_notify_unset;
1616a6a5580cSJeff Kirsher 		}
1617a6a5580cSJeff Kirsher 	}
1618a6a5580cSJeff Kirsher 
1619a6a5580cSJeff Kirsher 	for (i = 0; i < enic->wq_count; i++)
1620a6a5580cSJeff Kirsher 		vnic_wq_enable(&enic->wq[i]);
1621a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++)
1622a6a5580cSJeff Kirsher 		vnic_rq_enable(&enic->rq[i]);
1623a6a5580cSJeff Kirsher 
16247335903cSRoopa Prabhu 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1625a6a5580cSJeff Kirsher 		enic_dev_add_station_addr(enic);
16263f192795SRoopa Prabhu 
1627a6a5580cSJeff Kirsher 	enic_set_rx_mode(netdev);
1628a6a5580cSJeff Kirsher 
1629822473b6Sgovindarajulu.v 	netif_tx_wake_all_queues(netdev);
1630a6a5580cSJeff Kirsher 
163114747cd9SGovindarajulu Varadarajan 	for (i = 0; i < enic->rq_count; i++) {
163214747cd9SGovindarajulu Varadarajan 		enic_busy_poll_init_lock(&enic->rq[i]);
1633a6a5580cSJeff Kirsher 		napi_enable(&enic->napi[i]);
163414747cd9SGovindarajulu Varadarajan 	}
16354cfe8785SGovindarajulu Varadarajan 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
16364cfe8785SGovindarajulu Varadarajan 		for (i = 0; i < enic->wq_count; i++)
16374cfe8785SGovindarajulu Varadarajan 			napi_enable(&enic->napi[enic_cq_wq(enic, i)]);
1638a6a5580cSJeff Kirsher 	enic_dev_enable(enic);
1639a6a5580cSJeff Kirsher 
1640a6a5580cSJeff Kirsher 	for (i = 0; i < enic->intr_count; i++)
1641a6a5580cSJeff Kirsher 		vnic_intr_unmask(&enic->intr[i]);
1642a6a5580cSJeff Kirsher 
1643a6a5580cSJeff Kirsher 	enic_notify_timer_start(enic);
1644a145df23SGovindarajulu Varadarajan 	enic_rfs_flw_tbl_init(enic);
1645a6a5580cSJeff Kirsher 
1646a6a5580cSJeff Kirsher 	return 0;
1647a6a5580cSJeff Kirsher 
1648a6a5580cSJeff Kirsher err_out_notify_unset:
1649a6a5580cSJeff Kirsher 	enic_dev_notify_unset(enic);
1650a6a5580cSJeff Kirsher err_out_free_intr:
1651a6a5580cSJeff Kirsher 	enic_free_intr(enic);
1652a6a5580cSJeff Kirsher 
1653a6a5580cSJeff Kirsher 	return err;
1654a6a5580cSJeff Kirsher }
1655a6a5580cSJeff Kirsher 
1656a6a5580cSJeff Kirsher /* rtnl lock is held, process context */
1657a6a5580cSJeff Kirsher static int enic_stop(struct net_device *netdev)
1658a6a5580cSJeff Kirsher {
1659a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1660a6a5580cSJeff Kirsher 	unsigned int i;
1661a6a5580cSJeff Kirsher 	int err;
1662a6a5580cSJeff Kirsher 
1663a6a5580cSJeff Kirsher 	for (i = 0; i < enic->intr_count; i++) {
1664a6a5580cSJeff Kirsher 		vnic_intr_mask(&enic->intr[i]);
1665a6a5580cSJeff Kirsher 		(void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1666a6a5580cSJeff Kirsher 	}
1667a6a5580cSJeff Kirsher 
1668a6a5580cSJeff Kirsher 	enic_synchronize_irqs(enic);
1669a6a5580cSJeff Kirsher 
1670a6a5580cSJeff Kirsher 	del_timer_sync(&enic->notify_timer);
1671a145df23SGovindarajulu Varadarajan 	enic_rfs_flw_tbl_free(enic);
1672a6a5580cSJeff Kirsher 
1673a6a5580cSJeff Kirsher 	enic_dev_disable(enic);
1674a6a5580cSJeff Kirsher 
167514747cd9SGovindarajulu Varadarajan 	for (i = 0; i < enic->rq_count; i++) {
1676a6a5580cSJeff Kirsher 		napi_disable(&enic->napi[i]);
167739dc90c1SGovindarajulu Varadarajan 		local_bh_disable();
167814747cd9SGovindarajulu Varadarajan 		while (!enic_poll_lock_napi(&enic->rq[i]))
167914747cd9SGovindarajulu Varadarajan 			mdelay(1);
168014747cd9SGovindarajulu Varadarajan 		local_bh_enable();
168139dc90c1SGovindarajulu Varadarajan 	}
1682a6a5580cSJeff Kirsher 
1683a6a5580cSJeff Kirsher 	netif_carrier_off(netdev);
1684a6a5580cSJeff Kirsher 	netif_tx_disable(netdev);
16854cfe8785SGovindarajulu Varadarajan 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
16864cfe8785SGovindarajulu Varadarajan 		for (i = 0; i < enic->wq_count; i++)
16874cfe8785SGovindarajulu Varadarajan 			napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
16883f192795SRoopa Prabhu 
16897335903cSRoopa Prabhu 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1690a6a5580cSJeff Kirsher 		enic_dev_del_station_addr(enic);
1691a6a5580cSJeff Kirsher 
1692a6a5580cSJeff Kirsher 	for (i = 0; i < enic->wq_count; i++) {
1693a6a5580cSJeff Kirsher 		err = vnic_wq_disable(&enic->wq[i]);
1694a6a5580cSJeff Kirsher 		if (err)
1695a6a5580cSJeff Kirsher 			return err;
1696a6a5580cSJeff Kirsher 	}
1697a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++) {
1698a6a5580cSJeff Kirsher 		err = vnic_rq_disable(&enic->rq[i]);
1699a6a5580cSJeff Kirsher 		if (err)
1700a6a5580cSJeff Kirsher 			return err;
1701a6a5580cSJeff Kirsher 	}
1702a6a5580cSJeff Kirsher 
1703a6a5580cSJeff Kirsher 	enic_dev_notify_unset(enic);
1704a6a5580cSJeff Kirsher 	enic_free_intr(enic);
1705a6a5580cSJeff Kirsher 
1706a6a5580cSJeff Kirsher 	for (i = 0; i < enic->wq_count; i++)
1707a6a5580cSJeff Kirsher 		vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1708a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++)
1709a6a5580cSJeff Kirsher 		vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1710a6a5580cSJeff Kirsher 	for (i = 0; i < enic->cq_count; i++)
1711a6a5580cSJeff Kirsher 		vnic_cq_clean(&enic->cq[i]);
1712a6a5580cSJeff Kirsher 	for (i = 0; i < enic->intr_count; i++)
1713a6a5580cSJeff Kirsher 		vnic_intr_clean(&enic->intr[i]);
1714a6a5580cSJeff Kirsher 
1715a6a5580cSJeff Kirsher 	return 0;
1716a6a5580cSJeff Kirsher }
1717a6a5580cSJeff Kirsher 
1718a6a5580cSJeff Kirsher static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1719a6a5580cSJeff Kirsher {
1720a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1721a6a5580cSJeff Kirsher 	int running = netif_running(netdev);
1722a6a5580cSJeff Kirsher 
1723a6a5580cSJeff Kirsher 	if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1724a6a5580cSJeff Kirsher 		return -EINVAL;
1725a6a5580cSJeff Kirsher 
17267335903cSRoopa Prabhu 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
1727a6a5580cSJeff Kirsher 		return -EOPNOTSUPP;
1728a6a5580cSJeff Kirsher 
1729a6a5580cSJeff Kirsher 	if (running)
1730a6a5580cSJeff Kirsher 		enic_stop(netdev);
1731a6a5580cSJeff Kirsher 
1732a6a5580cSJeff Kirsher 	netdev->mtu = new_mtu;
1733a6a5580cSJeff Kirsher 
1734a6a5580cSJeff Kirsher 	if (netdev->mtu > enic->port_mtu)
1735a6a5580cSJeff Kirsher 		netdev_warn(netdev,
1736a6a5580cSJeff Kirsher 			"interface MTU (%d) set higher than port MTU (%d)\n",
1737a6a5580cSJeff Kirsher 			netdev->mtu, enic->port_mtu);
1738a6a5580cSJeff Kirsher 
1739a6a5580cSJeff Kirsher 	if (running)
1740a6a5580cSJeff Kirsher 		enic_open(netdev);
1741a6a5580cSJeff Kirsher 
1742a6a5580cSJeff Kirsher 	return 0;
1743a6a5580cSJeff Kirsher }
1744a6a5580cSJeff Kirsher 
1745a6a5580cSJeff Kirsher static void enic_change_mtu_work(struct work_struct *work)
1746a6a5580cSJeff Kirsher {
1747a6a5580cSJeff Kirsher 	struct enic *enic = container_of(work, struct enic, change_mtu_work);
1748a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
1749a6a5580cSJeff Kirsher 	int new_mtu = vnic_dev_mtu(enic->vdev);
1750a6a5580cSJeff Kirsher 	int err;
1751a6a5580cSJeff Kirsher 	unsigned int i;
1752a6a5580cSJeff Kirsher 
1753a6a5580cSJeff Kirsher 	new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1754a6a5580cSJeff Kirsher 
1755a6a5580cSJeff Kirsher 	rtnl_lock();
1756a6a5580cSJeff Kirsher 
1757a6a5580cSJeff Kirsher 	/* Stop RQ */
1758a6a5580cSJeff Kirsher 	del_timer_sync(&enic->notify_timer);
1759a6a5580cSJeff Kirsher 
1760a6a5580cSJeff Kirsher 	for (i = 0; i < enic->rq_count; i++)
1761a6a5580cSJeff Kirsher 		napi_disable(&enic->napi[i]);
1762a6a5580cSJeff Kirsher 
1763a6a5580cSJeff Kirsher 	vnic_intr_mask(&enic->intr[0]);
1764a6a5580cSJeff Kirsher 	enic_synchronize_irqs(enic);
1765a6a5580cSJeff Kirsher 	err = vnic_rq_disable(&enic->rq[0]);
1766a6a5580cSJeff Kirsher 	if (err) {
1767e057590bSKonstantin Khlebnikov 		rtnl_unlock();
1768a6a5580cSJeff Kirsher 		netdev_err(netdev, "Unable to disable RQ.\n");
1769a6a5580cSJeff Kirsher 		return;
1770a6a5580cSJeff Kirsher 	}
1771a6a5580cSJeff Kirsher 	vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1772a6a5580cSJeff Kirsher 	vnic_cq_clean(&enic->cq[0]);
1773a6a5580cSJeff Kirsher 	vnic_intr_clean(&enic->intr[0]);
1774a6a5580cSJeff Kirsher 
1775a6a5580cSJeff Kirsher 	/* Fill RQ with new_mtu-sized buffers */
1776a6a5580cSJeff Kirsher 	netdev->mtu = new_mtu;
1777a6a5580cSJeff Kirsher 	vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1778a6a5580cSJeff Kirsher 	/* Need at least one buffer on ring to get going */
1779a6a5580cSJeff Kirsher 	if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1780e057590bSKonstantin Khlebnikov 		rtnl_unlock();
1781a6a5580cSJeff Kirsher 		netdev_err(netdev, "Unable to alloc receive buffers.\n");
1782a6a5580cSJeff Kirsher 		return;
1783a6a5580cSJeff Kirsher 	}
1784a6a5580cSJeff Kirsher 
1785a6a5580cSJeff Kirsher 	/* Start RQ */
1786a6a5580cSJeff Kirsher 	vnic_rq_enable(&enic->rq[0]);
1787a6a5580cSJeff Kirsher 	napi_enable(&enic->napi[0]);
1788a6a5580cSJeff Kirsher 	vnic_intr_unmask(&enic->intr[0]);
1789a6a5580cSJeff Kirsher 	enic_notify_timer_start(enic);
1790a6a5580cSJeff Kirsher 
1791a6a5580cSJeff Kirsher 	rtnl_unlock();
1792a6a5580cSJeff Kirsher 
1793a6a5580cSJeff Kirsher 	netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1794a6a5580cSJeff Kirsher }
1795a6a5580cSJeff Kirsher 
1796a6a5580cSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1797a6a5580cSJeff Kirsher static void enic_poll_controller(struct net_device *netdev)
1798a6a5580cSJeff Kirsher {
1799a6a5580cSJeff Kirsher 	struct enic *enic = netdev_priv(netdev);
1800a6a5580cSJeff Kirsher 	struct vnic_dev *vdev = enic->vdev;
1801a6a5580cSJeff Kirsher 	unsigned int i, intr;
1802a6a5580cSJeff Kirsher 
1803a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(vdev)) {
1804a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
1805a6a5580cSJeff Kirsher 		for (i = 0; i < enic->rq_count; i++) {
1806a6a5580cSJeff Kirsher 			intr = enic_msix_rq_intr(enic, i);
18074cfe8785SGovindarajulu Varadarajan 			enic_isr_msix(enic->msix_entry[intr].vector,
1808a6a5580cSJeff Kirsher 				      &enic->napi[i]);
1809a6a5580cSJeff Kirsher 		}
1810a6a5580cSJeff Kirsher 
1811a6a5580cSJeff Kirsher 		for (i = 0; i < enic->wq_count; i++) {
1812a6a5580cSJeff Kirsher 			intr = enic_msix_wq_intr(enic, i);
18134cfe8785SGovindarajulu Varadarajan 			enic_isr_msix(enic->msix_entry[intr].vector,
18144cfe8785SGovindarajulu Varadarajan 				      &enic->napi[enic_cq_wq(enic, i)]);
1815a6a5580cSJeff Kirsher 		}
1816a6a5580cSJeff Kirsher 
1817a6a5580cSJeff Kirsher 		break;
1818a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
1819a6a5580cSJeff Kirsher 		enic_isr_msi(enic->pdev->irq, enic);
1820a6a5580cSJeff Kirsher 		break;
1821a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_INTX:
1822a6a5580cSJeff Kirsher 		enic_isr_legacy(enic->pdev->irq, netdev);
1823a6a5580cSJeff Kirsher 		break;
1824a6a5580cSJeff Kirsher 	default:
1825a6a5580cSJeff Kirsher 		break;
1826a6a5580cSJeff Kirsher 	}
1827a6a5580cSJeff Kirsher }
1828a6a5580cSJeff Kirsher #endif
1829a6a5580cSJeff Kirsher 
1830a6a5580cSJeff Kirsher static int enic_dev_wait(struct vnic_dev *vdev,
1831a6a5580cSJeff Kirsher 	int (*start)(struct vnic_dev *, int),
1832a6a5580cSJeff Kirsher 	int (*finished)(struct vnic_dev *, int *),
1833a6a5580cSJeff Kirsher 	int arg)
1834a6a5580cSJeff Kirsher {
1835a6a5580cSJeff Kirsher 	unsigned long time;
1836a6a5580cSJeff Kirsher 	int done;
1837a6a5580cSJeff Kirsher 	int err;
1838a6a5580cSJeff Kirsher 
1839a6a5580cSJeff Kirsher 	BUG_ON(in_interrupt());
1840a6a5580cSJeff Kirsher 
1841a6a5580cSJeff Kirsher 	err = start(vdev, arg);
1842a6a5580cSJeff Kirsher 	if (err)
1843a6a5580cSJeff Kirsher 		return err;
1844a6a5580cSJeff Kirsher 
1845a6a5580cSJeff Kirsher 	/* Wait for func to complete...2 seconds max
1846a6a5580cSJeff Kirsher 	 */
1847a6a5580cSJeff Kirsher 
1848a6a5580cSJeff Kirsher 	time = jiffies + (HZ * 2);
1849a6a5580cSJeff Kirsher 	do {
1850a6a5580cSJeff Kirsher 
1851a6a5580cSJeff Kirsher 		err = finished(vdev, &done);
1852a6a5580cSJeff Kirsher 		if (err)
1853a6a5580cSJeff Kirsher 			return err;
1854a6a5580cSJeff Kirsher 
1855a6a5580cSJeff Kirsher 		if (done)
1856a6a5580cSJeff Kirsher 			return 0;
1857a6a5580cSJeff Kirsher 
1858a6a5580cSJeff Kirsher 		schedule_timeout_uninterruptible(HZ / 10);
1859a6a5580cSJeff Kirsher 
1860a6a5580cSJeff Kirsher 	} while (time_after(time, jiffies));
1861a6a5580cSJeff Kirsher 
1862a6a5580cSJeff Kirsher 	return -ETIMEDOUT;
1863a6a5580cSJeff Kirsher }
1864a6a5580cSJeff Kirsher 
1865a6a5580cSJeff Kirsher static int enic_dev_open(struct enic *enic)
1866a6a5580cSJeff Kirsher {
1867a6a5580cSJeff Kirsher 	int err;
1868a6a5580cSJeff Kirsher 
1869a6a5580cSJeff Kirsher 	err = enic_dev_wait(enic->vdev, vnic_dev_open,
1870a6a5580cSJeff Kirsher 		vnic_dev_open_done, 0);
1871a6a5580cSJeff Kirsher 	if (err)
1872a6a5580cSJeff Kirsher 		dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1873a6a5580cSJeff Kirsher 			err);
1874a6a5580cSJeff Kirsher 
1875a6a5580cSJeff Kirsher 	return err;
1876a6a5580cSJeff Kirsher }
1877a6a5580cSJeff Kirsher 
1878a6a5580cSJeff Kirsher static int enic_dev_hang_reset(struct enic *enic)
1879a6a5580cSJeff Kirsher {
1880a6a5580cSJeff Kirsher 	int err;
1881a6a5580cSJeff Kirsher 
1882a6a5580cSJeff Kirsher 	err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1883a6a5580cSJeff Kirsher 		vnic_dev_hang_reset_done, 0);
1884a6a5580cSJeff Kirsher 	if (err)
1885a6a5580cSJeff Kirsher 		netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1886a6a5580cSJeff Kirsher 			err);
1887a6a5580cSJeff Kirsher 
1888a6a5580cSJeff Kirsher 	return err;
1889a6a5580cSJeff Kirsher }
1890a6a5580cSJeff Kirsher 
18914f675eb2SGovindarajulu Varadarajan int __enic_set_rsskey(struct enic *enic)
1892a6a5580cSJeff Kirsher {
1893c33d23c2SEric Dumazet 	union vnic_rss_key *rss_key_buf_va;
1894a6a5580cSJeff Kirsher 	dma_addr_t rss_key_buf_pa;
1895c33d23c2SEric Dumazet 	int i, kidx, bidx, err;
1896a6a5580cSJeff Kirsher 
1897c33d23c2SEric Dumazet 	rss_key_buf_va = pci_zalloc_consistent(enic->pdev,
1898c33d23c2SEric Dumazet 					       sizeof(union vnic_rss_key),
1899c33d23c2SEric Dumazet 					       &rss_key_buf_pa);
1900a6a5580cSJeff Kirsher 	if (!rss_key_buf_va)
1901a6a5580cSJeff Kirsher 		return -ENOMEM;
1902a6a5580cSJeff Kirsher 
1903c33d23c2SEric Dumazet 	for (i = 0; i < ENIC_RSS_LEN; i++) {
1904c33d23c2SEric Dumazet 		kidx = i / ENIC_RSS_BYTES_PER_KEY;
1905c33d23c2SEric Dumazet 		bidx = i % ENIC_RSS_BYTES_PER_KEY;
19064f675eb2SGovindarajulu Varadarajan 		rss_key_buf_va->key[kidx].b[bidx] = enic->rss_key[i];
1907c33d23c2SEric Dumazet 	}
19088e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1909a6a5580cSJeff Kirsher 	err = enic_set_rss_key(enic,
1910a6a5580cSJeff Kirsher 		rss_key_buf_pa,
1911a6a5580cSJeff Kirsher 		sizeof(union vnic_rss_key));
19128e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1913a6a5580cSJeff Kirsher 
1914a6a5580cSJeff Kirsher 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1915a6a5580cSJeff Kirsher 		rss_key_buf_va, rss_key_buf_pa);
1916a6a5580cSJeff Kirsher 
1917a6a5580cSJeff Kirsher 	return err;
1918a6a5580cSJeff Kirsher }
1919a6a5580cSJeff Kirsher 
19204f675eb2SGovindarajulu Varadarajan static int enic_set_rsskey(struct enic *enic)
19214f675eb2SGovindarajulu Varadarajan {
19224f675eb2SGovindarajulu Varadarajan 	netdev_rss_key_fill(enic->rss_key, ENIC_RSS_LEN);
19234f675eb2SGovindarajulu Varadarajan 
19244f675eb2SGovindarajulu Varadarajan 	return __enic_set_rsskey(enic);
19254f675eb2SGovindarajulu Varadarajan }
19264f675eb2SGovindarajulu Varadarajan 
1927a6a5580cSJeff Kirsher static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1928a6a5580cSJeff Kirsher {
1929a6a5580cSJeff Kirsher 	dma_addr_t rss_cpu_buf_pa;
1930a6a5580cSJeff Kirsher 	union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1931a6a5580cSJeff Kirsher 	unsigned int i;
1932a6a5580cSJeff Kirsher 	int err;
1933a6a5580cSJeff Kirsher 
1934a6a5580cSJeff Kirsher 	rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1935a6a5580cSJeff Kirsher 		sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1936a6a5580cSJeff Kirsher 	if (!rss_cpu_buf_va)
1937a6a5580cSJeff Kirsher 		return -ENOMEM;
1938a6a5580cSJeff Kirsher 
1939a6a5580cSJeff Kirsher 	for (i = 0; i < (1 << rss_hash_bits); i++)
1940a6a5580cSJeff Kirsher 		(*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1941a6a5580cSJeff Kirsher 
19428e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1943a6a5580cSJeff Kirsher 	err = enic_set_rss_cpu(enic,
1944a6a5580cSJeff Kirsher 		rss_cpu_buf_pa,
1945a6a5580cSJeff Kirsher 		sizeof(union vnic_rss_cpu));
19468e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1947a6a5580cSJeff Kirsher 
1948a6a5580cSJeff Kirsher 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1949a6a5580cSJeff Kirsher 		rss_cpu_buf_va, rss_cpu_buf_pa);
1950a6a5580cSJeff Kirsher 
1951a6a5580cSJeff Kirsher 	return err;
1952a6a5580cSJeff Kirsher }
1953a6a5580cSJeff Kirsher 
1954a6a5580cSJeff Kirsher static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1955a6a5580cSJeff Kirsher 	u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1956a6a5580cSJeff Kirsher {
1957a6a5580cSJeff Kirsher 	const u8 tso_ipid_split_en = 0;
1958a6a5580cSJeff Kirsher 	const u8 ig_vlan_strip_en = 1;
1959a6a5580cSJeff Kirsher 	int err;
1960a6a5580cSJeff Kirsher 
1961a6a5580cSJeff Kirsher 	/* Enable VLAN tag stripping.
1962a6a5580cSJeff Kirsher 	*/
1963a6a5580cSJeff Kirsher 
19648e091340STony Camuso 	spin_lock_bh(&enic->devcmd_lock);
1965a6a5580cSJeff Kirsher 	err = enic_set_nic_cfg(enic,
1966a6a5580cSJeff Kirsher 		rss_default_cpu, rss_hash_type,
1967a6a5580cSJeff Kirsher 		rss_hash_bits, rss_base_cpu,
1968a6a5580cSJeff Kirsher 		rss_enable, tso_ipid_split_en,
1969a6a5580cSJeff Kirsher 		ig_vlan_strip_en);
19708e091340STony Camuso 	spin_unlock_bh(&enic->devcmd_lock);
1971a6a5580cSJeff Kirsher 
1972a6a5580cSJeff Kirsher 	return err;
1973a6a5580cSJeff Kirsher }
1974a6a5580cSJeff Kirsher 
1975a6a5580cSJeff Kirsher static int enic_set_rss_nic_cfg(struct enic *enic)
1976a6a5580cSJeff Kirsher {
1977a6a5580cSJeff Kirsher 	struct device *dev = enic_get_dev(enic);
1978a6a5580cSJeff Kirsher 	const u8 rss_default_cpu = 0;
1979a6a5580cSJeff Kirsher 	const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1980a6a5580cSJeff Kirsher 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1981a6a5580cSJeff Kirsher 		NIC_CFG_RSS_HASH_TYPE_IPV6 |
1982a6a5580cSJeff Kirsher 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1983a6a5580cSJeff Kirsher 	const u8 rss_hash_bits = 7;
1984a6a5580cSJeff Kirsher 	const u8 rss_base_cpu = 0;
1985a6a5580cSJeff Kirsher 	u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1986a6a5580cSJeff Kirsher 
1987a6a5580cSJeff Kirsher 	if (rss_enable) {
1988a6a5580cSJeff Kirsher 		if (!enic_set_rsskey(enic)) {
1989a6a5580cSJeff Kirsher 			if (enic_set_rsscpu(enic, rss_hash_bits)) {
1990a6a5580cSJeff Kirsher 				rss_enable = 0;
1991a6a5580cSJeff Kirsher 				dev_warn(dev, "RSS disabled, "
1992a6a5580cSJeff Kirsher 					"Failed to set RSS cpu indirection table.");
1993a6a5580cSJeff Kirsher 			}
1994a6a5580cSJeff Kirsher 		} else {
1995a6a5580cSJeff Kirsher 			rss_enable = 0;
1996a6a5580cSJeff Kirsher 			dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1997a6a5580cSJeff Kirsher 		}
1998a6a5580cSJeff Kirsher 	}
1999a6a5580cSJeff Kirsher 
2000a6a5580cSJeff Kirsher 	return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
2001a6a5580cSJeff Kirsher 		rss_hash_bits, rss_base_cpu, rss_enable);
2002a6a5580cSJeff Kirsher }
2003a6a5580cSJeff Kirsher 
2004a6a5580cSJeff Kirsher static void enic_reset(struct work_struct *work)
2005a6a5580cSJeff Kirsher {
2006a6a5580cSJeff Kirsher 	struct enic *enic = container_of(work, struct enic, reset);
2007a6a5580cSJeff Kirsher 
2008a6a5580cSJeff Kirsher 	if (!netif_running(enic->netdev))
2009a6a5580cSJeff Kirsher 		return;
2010a6a5580cSJeff Kirsher 
2011a6a5580cSJeff Kirsher 	rtnl_lock();
2012a6a5580cSJeff Kirsher 
20130b038566SNeel Patel 	spin_lock(&enic->enic_api_lock);
2014a6a5580cSJeff Kirsher 	enic_dev_hang_notify(enic);
2015a6a5580cSJeff Kirsher 	enic_stop(enic->netdev);
2016a6a5580cSJeff Kirsher 	enic_dev_hang_reset(enic);
2017a6a5580cSJeff Kirsher 	enic_reset_addr_lists(enic);
2018a6a5580cSJeff Kirsher 	enic_init_vnic_resources(enic);
2019a6a5580cSJeff Kirsher 	enic_set_rss_nic_cfg(enic);
2020a6a5580cSJeff Kirsher 	enic_dev_set_ig_vlan_rewrite_mode(enic);
2021a6a5580cSJeff Kirsher 	enic_open(enic->netdev);
20220b038566SNeel Patel 	spin_unlock(&enic->enic_api_lock);
2023d765bb41SNeel Patel 	call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2024a6a5580cSJeff Kirsher 
2025a6a5580cSJeff Kirsher 	rtnl_unlock();
2026a6a5580cSJeff Kirsher }
2027a6a5580cSJeff Kirsher 
2028a6a5580cSJeff Kirsher static int enic_set_intr_mode(struct enic *enic)
2029a6a5580cSJeff Kirsher {
2030a6a5580cSJeff Kirsher 	unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
2031a6a5580cSJeff Kirsher 	unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
2032a6a5580cSJeff Kirsher 	unsigned int i;
2033a6a5580cSJeff Kirsher 
2034a6a5580cSJeff Kirsher 	/* Set interrupt mode (INTx, MSI, MSI-X) depending
2035a6a5580cSJeff Kirsher 	 * on system capabilities.
2036a6a5580cSJeff Kirsher 	 *
2037a6a5580cSJeff Kirsher 	 * Try MSI-X first
2038a6a5580cSJeff Kirsher 	 *
2039a6a5580cSJeff Kirsher 	 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2040a6a5580cSJeff Kirsher 	 * (the second to last INTR is used for WQ/RQ errors)
2041a6a5580cSJeff Kirsher 	 * (the last INTR is used for notifications)
2042a6a5580cSJeff Kirsher 	 */
2043a6a5580cSJeff Kirsher 
2044a6a5580cSJeff Kirsher 	BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2045a6a5580cSJeff Kirsher 	for (i = 0; i < n + m + 2; i++)
2046a6a5580cSJeff Kirsher 		enic->msix_entry[i].entry = i;
2047a6a5580cSJeff Kirsher 
2048a6a5580cSJeff Kirsher 	/* Use multiple RQs if RSS is enabled
2049a6a5580cSJeff Kirsher 	 */
2050a6a5580cSJeff Kirsher 
2051a6a5580cSJeff Kirsher 	if (ENIC_SETTING(enic, RSS) &&
2052a6a5580cSJeff Kirsher 	    enic->config.intr_mode < 1 &&
2053a6a5580cSJeff Kirsher 	    enic->rq_count >= n &&
2054a6a5580cSJeff Kirsher 	    enic->wq_count >= m &&
2055a6a5580cSJeff Kirsher 	    enic->cq_count >= n + m &&
2056a6a5580cSJeff Kirsher 	    enic->intr_count >= n + m + 2) {
2057a6a5580cSJeff Kirsher 
2058abbb6a37SAlexander Gordeev 		if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2059abbb6a37SAlexander Gordeev 					  n + m + 2, n + m + 2) > 0) {
2060a6a5580cSJeff Kirsher 
2061a6a5580cSJeff Kirsher 			enic->rq_count = n;
2062a6a5580cSJeff Kirsher 			enic->wq_count = m;
2063a6a5580cSJeff Kirsher 			enic->cq_count = n + m;
2064a6a5580cSJeff Kirsher 			enic->intr_count = n + m + 2;
2065a6a5580cSJeff Kirsher 
2066a6a5580cSJeff Kirsher 			vnic_dev_set_intr_mode(enic->vdev,
2067a6a5580cSJeff Kirsher 				VNIC_DEV_INTR_MODE_MSIX);
2068a6a5580cSJeff Kirsher 
2069a6a5580cSJeff Kirsher 			return 0;
2070a6a5580cSJeff Kirsher 		}
2071a6a5580cSJeff Kirsher 	}
2072a6a5580cSJeff Kirsher 
2073a6a5580cSJeff Kirsher 	if (enic->config.intr_mode < 1 &&
2074a6a5580cSJeff Kirsher 	    enic->rq_count >= 1 &&
2075a6a5580cSJeff Kirsher 	    enic->wq_count >= m &&
2076a6a5580cSJeff Kirsher 	    enic->cq_count >= 1 + m &&
2077a6a5580cSJeff Kirsher 	    enic->intr_count >= 1 + m + 2) {
2078abbb6a37SAlexander Gordeev 		if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2079abbb6a37SAlexander Gordeev 					  1 + m + 2, 1 + m + 2) > 0) {
2080a6a5580cSJeff Kirsher 
2081a6a5580cSJeff Kirsher 			enic->rq_count = 1;
2082a6a5580cSJeff Kirsher 			enic->wq_count = m;
2083a6a5580cSJeff Kirsher 			enic->cq_count = 1 + m;
2084a6a5580cSJeff Kirsher 			enic->intr_count = 1 + m + 2;
2085a6a5580cSJeff Kirsher 
2086a6a5580cSJeff Kirsher 			vnic_dev_set_intr_mode(enic->vdev,
2087a6a5580cSJeff Kirsher 				VNIC_DEV_INTR_MODE_MSIX);
2088a6a5580cSJeff Kirsher 
2089a6a5580cSJeff Kirsher 			return 0;
2090a6a5580cSJeff Kirsher 		}
2091a6a5580cSJeff Kirsher 	}
2092a6a5580cSJeff Kirsher 
2093a6a5580cSJeff Kirsher 	/* Next try MSI
2094a6a5580cSJeff Kirsher 	 *
2095a6a5580cSJeff Kirsher 	 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2096a6a5580cSJeff Kirsher 	 */
2097a6a5580cSJeff Kirsher 
2098a6a5580cSJeff Kirsher 	if (enic->config.intr_mode < 2 &&
2099a6a5580cSJeff Kirsher 	    enic->rq_count >= 1 &&
2100a6a5580cSJeff Kirsher 	    enic->wq_count >= 1 &&
2101a6a5580cSJeff Kirsher 	    enic->cq_count >= 2 &&
2102a6a5580cSJeff Kirsher 	    enic->intr_count >= 1 &&
2103a6a5580cSJeff Kirsher 	    !pci_enable_msi(enic->pdev)) {
2104a6a5580cSJeff Kirsher 
2105a6a5580cSJeff Kirsher 		enic->rq_count = 1;
2106a6a5580cSJeff Kirsher 		enic->wq_count = 1;
2107a6a5580cSJeff Kirsher 		enic->cq_count = 2;
2108a6a5580cSJeff Kirsher 		enic->intr_count = 1;
2109a6a5580cSJeff Kirsher 
2110a6a5580cSJeff Kirsher 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2111a6a5580cSJeff Kirsher 
2112a6a5580cSJeff Kirsher 		return 0;
2113a6a5580cSJeff Kirsher 	}
2114a6a5580cSJeff Kirsher 
2115a6a5580cSJeff Kirsher 	/* Next try INTx
2116a6a5580cSJeff Kirsher 	 *
2117a6a5580cSJeff Kirsher 	 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2118a6a5580cSJeff Kirsher 	 * (the first INTR is used for WQ/RQ)
2119a6a5580cSJeff Kirsher 	 * (the second INTR is used for WQ/RQ errors)
2120a6a5580cSJeff Kirsher 	 * (the last INTR is used for notifications)
2121a6a5580cSJeff Kirsher 	 */
2122a6a5580cSJeff Kirsher 
2123a6a5580cSJeff Kirsher 	if (enic->config.intr_mode < 3 &&
2124a6a5580cSJeff Kirsher 	    enic->rq_count >= 1 &&
2125a6a5580cSJeff Kirsher 	    enic->wq_count >= 1 &&
2126a6a5580cSJeff Kirsher 	    enic->cq_count >= 2 &&
2127a6a5580cSJeff Kirsher 	    enic->intr_count >= 3) {
2128a6a5580cSJeff Kirsher 
2129a6a5580cSJeff Kirsher 		enic->rq_count = 1;
2130a6a5580cSJeff Kirsher 		enic->wq_count = 1;
2131a6a5580cSJeff Kirsher 		enic->cq_count = 2;
2132a6a5580cSJeff Kirsher 		enic->intr_count = 3;
2133a6a5580cSJeff Kirsher 
2134a6a5580cSJeff Kirsher 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2135a6a5580cSJeff Kirsher 
2136a6a5580cSJeff Kirsher 		return 0;
2137a6a5580cSJeff Kirsher 	}
2138a6a5580cSJeff Kirsher 
2139a6a5580cSJeff Kirsher 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2140a6a5580cSJeff Kirsher 
2141a6a5580cSJeff Kirsher 	return -EINVAL;
2142a6a5580cSJeff Kirsher }
2143a6a5580cSJeff Kirsher 
2144a6a5580cSJeff Kirsher static void enic_clear_intr_mode(struct enic *enic)
2145a6a5580cSJeff Kirsher {
2146a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2147a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
2148a6a5580cSJeff Kirsher 		pci_disable_msix(enic->pdev);
2149a6a5580cSJeff Kirsher 		break;
2150a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSI:
2151a6a5580cSJeff Kirsher 		pci_disable_msi(enic->pdev);
2152a6a5580cSJeff Kirsher 		break;
2153a6a5580cSJeff Kirsher 	default:
2154a6a5580cSJeff Kirsher 		break;
2155a6a5580cSJeff Kirsher 	}
2156a6a5580cSJeff Kirsher 
2157a6a5580cSJeff Kirsher 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2158a6a5580cSJeff Kirsher }
2159a6a5580cSJeff Kirsher 
2160a6a5580cSJeff Kirsher static const struct net_device_ops enic_netdev_dynamic_ops = {
2161a6a5580cSJeff Kirsher 	.ndo_open		= enic_open,
2162a6a5580cSJeff Kirsher 	.ndo_stop		= enic_stop,
2163a6a5580cSJeff Kirsher 	.ndo_start_xmit		= enic_hard_start_xmit,
2164a6a5580cSJeff Kirsher 	.ndo_get_stats64	= enic_get_stats,
2165a6a5580cSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2166a6a5580cSJeff Kirsher 	.ndo_set_rx_mode	= enic_set_rx_mode,
2167a6a5580cSJeff Kirsher 	.ndo_set_mac_address	= enic_set_mac_address_dynamic,
2168a6a5580cSJeff Kirsher 	.ndo_change_mtu		= enic_change_mtu,
2169a6a5580cSJeff Kirsher 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2170a6a5580cSJeff Kirsher 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2171a6a5580cSJeff Kirsher 	.ndo_tx_timeout		= enic_tx_timeout,
2172a6a5580cSJeff Kirsher 	.ndo_set_vf_port	= enic_set_vf_port,
2173a6a5580cSJeff Kirsher 	.ndo_get_vf_port	= enic_get_vf_port,
2174a6a5580cSJeff Kirsher 	.ndo_set_vf_mac		= enic_set_vf_mac,
2175a6a5580cSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2176a6a5580cSJeff Kirsher 	.ndo_poll_controller	= enic_poll_controller,
2177a6a5580cSJeff Kirsher #endif
2178a145df23SGovindarajulu Varadarajan #ifdef CONFIG_RFS_ACCEL
2179a145df23SGovindarajulu Varadarajan 	.ndo_rx_flow_steer	= enic_rx_flow_steer,
2180a145df23SGovindarajulu Varadarajan #endif
218114747cd9SGovindarajulu Varadarajan #ifdef CONFIG_NET_RX_BUSY_POLL
218214747cd9SGovindarajulu Varadarajan 	.ndo_busy_poll		= enic_busy_poll,
218314747cd9SGovindarajulu Varadarajan #endif
2184a6a5580cSJeff Kirsher };
2185a6a5580cSJeff Kirsher 
2186a6a5580cSJeff Kirsher static const struct net_device_ops enic_netdev_ops = {
2187a6a5580cSJeff Kirsher 	.ndo_open		= enic_open,
2188a6a5580cSJeff Kirsher 	.ndo_stop		= enic_stop,
2189a6a5580cSJeff Kirsher 	.ndo_start_xmit		= enic_hard_start_xmit,
2190a6a5580cSJeff Kirsher 	.ndo_get_stats64	= enic_get_stats,
2191a6a5580cSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2192a6a5580cSJeff Kirsher 	.ndo_set_mac_address	= enic_set_mac_address,
2193a6a5580cSJeff Kirsher 	.ndo_set_rx_mode	= enic_set_rx_mode,
2194a6a5580cSJeff Kirsher 	.ndo_change_mtu		= enic_change_mtu,
2195a6a5580cSJeff Kirsher 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2196a6a5580cSJeff Kirsher 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2197a6a5580cSJeff Kirsher 	.ndo_tx_timeout		= enic_tx_timeout,
21983f192795SRoopa Prabhu 	.ndo_set_vf_port	= enic_set_vf_port,
21993f192795SRoopa Prabhu 	.ndo_get_vf_port	= enic_get_vf_port,
22003f192795SRoopa Prabhu 	.ndo_set_vf_mac		= enic_set_vf_mac,
2201a6a5580cSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2202a6a5580cSJeff Kirsher 	.ndo_poll_controller	= enic_poll_controller,
2203a6a5580cSJeff Kirsher #endif
2204a145df23SGovindarajulu Varadarajan #ifdef CONFIG_RFS_ACCEL
2205a145df23SGovindarajulu Varadarajan 	.ndo_rx_flow_steer	= enic_rx_flow_steer,
2206a145df23SGovindarajulu Varadarajan #endif
220714747cd9SGovindarajulu Varadarajan #ifdef CONFIG_NET_RX_BUSY_POLL
220814747cd9SGovindarajulu Varadarajan 	.ndo_busy_poll		= enic_busy_poll,
220914747cd9SGovindarajulu Varadarajan #endif
2210a6a5580cSJeff Kirsher };
2211a6a5580cSJeff Kirsher 
2212a6a5580cSJeff Kirsher static void enic_dev_deinit(struct enic *enic)
2213a6a5580cSJeff Kirsher {
2214a6a5580cSJeff Kirsher 	unsigned int i;
2215a6a5580cSJeff Kirsher 
221614747cd9SGovindarajulu Varadarajan 	for (i = 0; i < enic->rq_count; i++) {
221714747cd9SGovindarajulu Varadarajan 		napi_hash_del(&enic->napi[i]);
2218a6a5580cSJeff Kirsher 		netif_napi_del(&enic->napi[i]);
221914747cd9SGovindarajulu Varadarajan 	}
22204cfe8785SGovindarajulu Varadarajan 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
22214cfe8785SGovindarajulu Varadarajan 		for (i = 0; i < enic->wq_count; i++)
22224cfe8785SGovindarajulu Varadarajan 			netif_napi_del(&enic->napi[enic_cq_wq(enic, i)]);
2223a6a5580cSJeff Kirsher 
2224a6a5580cSJeff Kirsher 	enic_free_vnic_resources(enic);
2225a6a5580cSJeff Kirsher 	enic_clear_intr_mode(enic);
2226a6a5580cSJeff Kirsher }
2227a6a5580cSJeff Kirsher 
2228a6a5580cSJeff Kirsher static int enic_dev_init(struct enic *enic)
2229a6a5580cSJeff Kirsher {
2230a6a5580cSJeff Kirsher 	struct device *dev = enic_get_dev(enic);
2231a6a5580cSJeff Kirsher 	struct net_device *netdev = enic->netdev;
2232a6a5580cSJeff Kirsher 	unsigned int i;
2233a6a5580cSJeff Kirsher 	int err;
2234a6a5580cSJeff Kirsher 
2235a6a5580cSJeff Kirsher 	/* Get interrupt coalesce timer info */
2236a6a5580cSJeff Kirsher 	err = enic_dev_intr_coal_timer_info(enic);
2237a6a5580cSJeff Kirsher 	if (err) {
2238a6a5580cSJeff Kirsher 		dev_warn(dev, "Using default conversion factor for "
2239a6a5580cSJeff Kirsher 			"interrupt coalesce timer\n");
2240a6a5580cSJeff Kirsher 		vnic_dev_intr_coal_timer_info_default(enic->vdev);
2241a6a5580cSJeff Kirsher 	}
2242a6a5580cSJeff Kirsher 
2243a6a5580cSJeff Kirsher 	/* Get vNIC configuration
2244a6a5580cSJeff Kirsher 	 */
2245a6a5580cSJeff Kirsher 
2246a6a5580cSJeff Kirsher 	err = enic_get_vnic_config(enic);
2247a6a5580cSJeff Kirsher 	if (err) {
2248a6a5580cSJeff Kirsher 		dev_err(dev, "Get vNIC configuration failed, aborting\n");
2249a6a5580cSJeff Kirsher 		return err;
2250a6a5580cSJeff Kirsher 	}
2251a6a5580cSJeff Kirsher 
2252a6a5580cSJeff Kirsher 	/* Get available resource counts
2253a6a5580cSJeff Kirsher 	 */
2254a6a5580cSJeff Kirsher 
2255a6a5580cSJeff Kirsher 	enic_get_res_counts(enic);
2256a6a5580cSJeff Kirsher 
2257a6a5580cSJeff Kirsher 	/* Set interrupt mode based on resource counts and system
2258a6a5580cSJeff Kirsher 	 * capabilities
2259a6a5580cSJeff Kirsher 	 */
2260a6a5580cSJeff Kirsher 
2261a6a5580cSJeff Kirsher 	err = enic_set_intr_mode(enic);
2262a6a5580cSJeff Kirsher 	if (err) {
2263a6a5580cSJeff Kirsher 		dev_err(dev, "Failed to set intr mode based on resource "
2264a6a5580cSJeff Kirsher 			"counts and system capabilities, aborting\n");
2265a6a5580cSJeff Kirsher 		return err;
2266a6a5580cSJeff Kirsher 	}
2267a6a5580cSJeff Kirsher 
2268a6a5580cSJeff Kirsher 	/* Allocate and configure vNIC resources
2269a6a5580cSJeff Kirsher 	 */
2270a6a5580cSJeff Kirsher 
2271a6a5580cSJeff Kirsher 	err = enic_alloc_vnic_resources(enic);
2272a6a5580cSJeff Kirsher 	if (err) {
2273a6a5580cSJeff Kirsher 		dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2274a6a5580cSJeff Kirsher 		goto err_out_free_vnic_resources;
2275a6a5580cSJeff Kirsher 	}
2276a6a5580cSJeff Kirsher 
2277a6a5580cSJeff Kirsher 	enic_init_vnic_resources(enic);
2278a6a5580cSJeff Kirsher 
2279a6a5580cSJeff Kirsher 	err = enic_set_rss_nic_cfg(enic);
2280a6a5580cSJeff Kirsher 	if (err) {
2281a6a5580cSJeff Kirsher 		dev_err(dev, "Failed to config nic, aborting\n");
2282a6a5580cSJeff Kirsher 		goto err_out_free_vnic_resources;
2283a6a5580cSJeff Kirsher 	}
2284a6a5580cSJeff Kirsher 
2285a6a5580cSJeff Kirsher 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2286a6a5580cSJeff Kirsher 	default:
2287a6a5580cSJeff Kirsher 		netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
228814747cd9SGovindarajulu Varadarajan 		napi_hash_add(&enic->napi[0]);
2289a6a5580cSJeff Kirsher 		break;
2290a6a5580cSJeff Kirsher 	case VNIC_DEV_INTR_MODE_MSIX:
229114747cd9SGovindarajulu Varadarajan 		for (i = 0; i < enic->rq_count; i++) {
2292a6a5580cSJeff Kirsher 			netif_napi_add(netdev, &enic->napi[i],
22934cfe8785SGovindarajulu Varadarajan 				enic_poll_msix_rq, NAPI_POLL_WEIGHT);
229414747cd9SGovindarajulu Varadarajan 			napi_hash_add(&enic->napi[i]);
229514747cd9SGovindarajulu Varadarajan 		}
22964cfe8785SGovindarajulu Varadarajan 		for (i = 0; i < enic->wq_count; i++)
22974cfe8785SGovindarajulu Varadarajan 			netif_napi_add(netdev, &enic->napi[enic_cq_wq(enic, i)],
22984cfe8785SGovindarajulu Varadarajan 				       enic_poll_msix_wq, NAPI_POLL_WEIGHT);
2299a6a5580cSJeff Kirsher 		break;
2300a6a5580cSJeff Kirsher 	}
2301a6a5580cSJeff Kirsher 
2302a6a5580cSJeff Kirsher 	return 0;
2303a6a5580cSJeff Kirsher 
2304a6a5580cSJeff Kirsher err_out_free_vnic_resources:
2305a6a5580cSJeff Kirsher 	enic_clear_intr_mode(enic);
2306a6a5580cSJeff Kirsher 	enic_free_vnic_resources(enic);
2307a6a5580cSJeff Kirsher 
2308a6a5580cSJeff Kirsher 	return err;
2309a6a5580cSJeff Kirsher }
2310a6a5580cSJeff Kirsher 
2311a6a5580cSJeff Kirsher static void enic_iounmap(struct enic *enic)
2312a6a5580cSJeff Kirsher {
2313a6a5580cSJeff Kirsher 	unsigned int i;
2314a6a5580cSJeff Kirsher 
2315a6a5580cSJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2316a6a5580cSJeff Kirsher 		if (enic->bar[i].vaddr)
2317a6a5580cSJeff Kirsher 			iounmap(enic->bar[i].vaddr);
2318a6a5580cSJeff Kirsher }
2319a6a5580cSJeff Kirsher 
23201dd06ae8SGreg Kroah-Hartman static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2321a6a5580cSJeff Kirsher {
2322a6a5580cSJeff Kirsher 	struct device *dev = &pdev->dev;
2323a6a5580cSJeff Kirsher 	struct net_device *netdev;
2324a6a5580cSJeff Kirsher 	struct enic *enic;
2325a6a5580cSJeff Kirsher 	int using_dac = 0;
2326a6a5580cSJeff Kirsher 	unsigned int i;
2327a6a5580cSJeff Kirsher 	int err;
23288749b427SRoopa Prabhu #ifdef CONFIG_PCI_IOV
23298749b427SRoopa Prabhu 	int pos = 0;
23308749b427SRoopa Prabhu #endif
2331b67f231dSRoopa Prabhu 	int num_pps = 1;
2332a6a5580cSJeff Kirsher 
2333a6a5580cSJeff Kirsher 	/* Allocate net device structure and initialize.  Private
2334a6a5580cSJeff Kirsher 	 * instance data is initialized to zero.
2335a6a5580cSJeff Kirsher 	 */
2336a6a5580cSJeff Kirsher 
2337822473b6Sgovindarajulu.v 	netdev = alloc_etherdev_mqs(sizeof(struct enic),
2338822473b6Sgovindarajulu.v 				    ENIC_RQ_MAX, ENIC_WQ_MAX);
233941de8d4cSJoe Perches 	if (!netdev)
2340a6a5580cSJeff Kirsher 		return -ENOMEM;
2341a6a5580cSJeff Kirsher 
2342a6a5580cSJeff Kirsher 	pci_set_drvdata(pdev, netdev);
2343a6a5580cSJeff Kirsher 
2344a6a5580cSJeff Kirsher 	SET_NETDEV_DEV(netdev, &pdev->dev);
2345a6a5580cSJeff Kirsher 
2346a6a5580cSJeff Kirsher 	enic = netdev_priv(netdev);
2347a6a5580cSJeff Kirsher 	enic->netdev = netdev;
2348a6a5580cSJeff Kirsher 	enic->pdev = pdev;
2349a6a5580cSJeff Kirsher 
2350a6a5580cSJeff Kirsher 	/* Setup PCI resources
2351a6a5580cSJeff Kirsher 	 */
2352a6a5580cSJeff Kirsher 
2353a6a5580cSJeff Kirsher 	err = pci_enable_device_mem(pdev);
2354a6a5580cSJeff Kirsher 	if (err) {
2355a6a5580cSJeff Kirsher 		dev_err(dev, "Cannot enable PCI device, aborting\n");
2356a6a5580cSJeff Kirsher 		goto err_out_free_netdev;
2357a6a5580cSJeff Kirsher 	}
2358a6a5580cSJeff Kirsher 
2359a6a5580cSJeff Kirsher 	err = pci_request_regions(pdev, DRV_NAME);
2360a6a5580cSJeff Kirsher 	if (err) {
2361a6a5580cSJeff Kirsher 		dev_err(dev, "Cannot request PCI regions, aborting\n");
2362a6a5580cSJeff Kirsher 		goto err_out_disable_device;
2363a6a5580cSJeff Kirsher 	}
2364a6a5580cSJeff Kirsher 
2365a6a5580cSJeff Kirsher 	pci_set_master(pdev);
2366a6a5580cSJeff Kirsher 
2367a6a5580cSJeff Kirsher 	/* Query PCI controller on system for DMA addressing
2368624dbf55Sgovindarajulu.v 	 * limitation for the device.  Try 64-bit first, and
2369a6a5580cSJeff Kirsher 	 * fail to 32-bit.
2370a6a5580cSJeff Kirsher 	 */
2371a6a5580cSJeff Kirsher 
2372624dbf55Sgovindarajulu.v 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2373a6a5580cSJeff Kirsher 	if (err) {
2374a6a5580cSJeff Kirsher 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2375a6a5580cSJeff Kirsher 		if (err) {
2376a6a5580cSJeff Kirsher 			dev_err(dev, "No usable DMA configuration, aborting\n");
2377a6a5580cSJeff Kirsher 			goto err_out_release_regions;
2378a6a5580cSJeff Kirsher 		}
2379a6a5580cSJeff Kirsher 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2380a6a5580cSJeff Kirsher 		if (err) {
2381a6a5580cSJeff Kirsher 			dev_err(dev, "Unable to obtain %u-bit DMA "
2382a6a5580cSJeff Kirsher 				"for consistent allocations, aborting\n", 32);
2383a6a5580cSJeff Kirsher 			goto err_out_release_regions;
2384a6a5580cSJeff Kirsher 		}
2385a6a5580cSJeff Kirsher 	} else {
2386624dbf55Sgovindarajulu.v 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2387a6a5580cSJeff Kirsher 		if (err) {
2388a6a5580cSJeff Kirsher 			dev_err(dev, "Unable to obtain %u-bit DMA "
2389624dbf55Sgovindarajulu.v 				"for consistent allocations, aborting\n", 64);
2390a6a5580cSJeff Kirsher 			goto err_out_release_regions;
2391a6a5580cSJeff Kirsher 		}
2392a6a5580cSJeff Kirsher 		using_dac = 1;
2393a6a5580cSJeff Kirsher 	}
2394a6a5580cSJeff Kirsher 
2395a6a5580cSJeff Kirsher 	/* Map vNIC resources from BAR0-5
2396a6a5580cSJeff Kirsher 	 */
2397a6a5580cSJeff Kirsher 
2398a6a5580cSJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2399a6a5580cSJeff Kirsher 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2400a6a5580cSJeff Kirsher 			continue;
2401a6a5580cSJeff Kirsher 		enic->bar[i].len = pci_resource_len(pdev, i);
2402a6a5580cSJeff Kirsher 		enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2403a6a5580cSJeff Kirsher 		if (!enic->bar[i].vaddr) {
2404a6a5580cSJeff Kirsher 			dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2405a6a5580cSJeff Kirsher 			err = -ENODEV;
2406a6a5580cSJeff Kirsher 			goto err_out_iounmap;
2407a6a5580cSJeff Kirsher 		}
2408a6a5580cSJeff Kirsher 		enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2409a6a5580cSJeff Kirsher 	}
2410a6a5580cSJeff Kirsher 
2411a6a5580cSJeff Kirsher 	/* Register vNIC device
2412a6a5580cSJeff Kirsher 	 */
2413a6a5580cSJeff Kirsher 
2414a6a5580cSJeff Kirsher 	enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2415a6a5580cSJeff Kirsher 		ARRAY_SIZE(enic->bar));
2416a6a5580cSJeff Kirsher 	if (!enic->vdev) {
2417a6a5580cSJeff Kirsher 		dev_err(dev, "vNIC registration failed, aborting\n");
2418a6a5580cSJeff Kirsher 		err = -ENODEV;
2419a6a5580cSJeff Kirsher 		goto err_out_iounmap;
2420a6a5580cSJeff Kirsher 	}
2421a6a5580cSJeff Kirsher 
24228749b427SRoopa Prabhu #ifdef CONFIG_PCI_IOV
24238749b427SRoopa Prabhu 	/* Get number of subvnics */
24248749b427SRoopa Prabhu 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
24258749b427SRoopa Prabhu 	if (pos) {
24268749b427SRoopa Prabhu 		pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
2427413708bbSDan Carpenter 			&enic->num_vfs);
24288749b427SRoopa Prabhu 		if (enic->num_vfs) {
24298749b427SRoopa Prabhu 			err = pci_enable_sriov(pdev, enic->num_vfs);
24308749b427SRoopa Prabhu 			if (err) {
24318749b427SRoopa Prabhu 				dev_err(dev, "SRIOV enable failed, aborting."
24328749b427SRoopa Prabhu 					" pci_enable_sriov() returned %d\n",
24338749b427SRoopa Prabhu 					err);
24348749b427SRoopa Prabhu 				goto err_out_vnic_unregister;
24358749b427SRoopa Prabhu 			}
24368749b427SRoopa Prabhu 			enic->priv_flags |= ENIC_SRIOV_ENABLED;
2437b67f231dSRoopa Prabhu 			num_pps = enic->num_vfs;
24388749b427SRoopa Prabhu 		}
24398749b427SRoopa Prabhu 	}
24408749b427SRoopa Prabhu #endif
2441ca2b721dSRoopa Prabhu 
24423f192795SRoopa Prabhu 	/* Allocate structure for port profiles */
2443a1de2219SThomas Meyer 	enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
24443f192795SRoopa Prabhu 	if (!enic->pp) {
24453f192795SRoopa Prabhu 		err = -ENOMEM;
2446ca2b721dSRoopa Prabhu 		goto err_out_disable_sriov_pp;
24473f192795SRoopa Prabhu 	}
24483f192795SRoopa Prabhu 
2449a6a5580cSJeff Kirsher 	/* Issue device open to get device in known state
2450a6a5580cSJeff Kirsher 	 */
2451a6a5580cSJeff Kirsher 
2452a6a5580cSJeff Kirsher 	err = enic_dev_open(enic);
2453a6a5580cSJeff Kirsher 	if (err) {
2454a6a5580cSJeff Kirsher 		dev_err(dev, "vNIC dev open failed, aborting\n");
2455ca2b721dSRoopa Prabhu 		goto err_out_disable_sriov;
2456a6a5580cSJeff Kirsher 	}
2457a6a5580cSJeff Kirsher 
2458a6a5580cSJeff Kirsher 	/* Setup devcmd lock
2459a6a5580cSJeff Kirsher 	 */
2460a6a5580cSJeff Kirsher 
2461a6a5580cSJeff Kirsher 	spin_lock_init(&enic->devcmd_lock);
24620b038566SNeel Patel 	spin_lock_init(&enic->enic_api_lock);
2463a6a5580cSJeff Kirsher 
2464a6a5580cSJeff Kirsher 	/*
2465a6a5580cSJeff Kirsher 	 * Set ingress vlan rewrite mode before vnic initialization
2466a6a5580cSJeff Kirsher 	 */
2467a6a5580cSJeff Kirsher 
2468a6a5580cSJeff Kirsher 	err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2469a6a5580cSJeff Kirsher 	if (err) {
2470a6a5580cSJeff Kirsher 		dev_err(dev,
2471a6a5580cSJeff Kirsher 			"Failed to set ingress vlan rewrite mode, aborting.\n");
2472a6a5580cSJeff Kirsher 		goto err_out_dev_close;
2473a6a5580cSJeff Kirsher 	}
2474a6a5580cSJeff Kirsher 
2475a6a5580cSJeff Kirsher 	/* Issue device init to initialize the vnic-to-switch link.
2476a6a5580cSJeff Kirsher 	 * We'll start with carrier off and wait for link UP
2477a6a5580cSJeff Kirsher 	 * notification later to turn on carrier.  We don't need
2478a6a5580cSJeff Kirsher 	 * to wait here for the vnic-to-switch link initialization
2479a6a5580cSJeff Kirsher 	 * to complete; link UP notification is the indication that
2480a6a5580cSJeff Kirsher 	 * the process is complete.
2481a6a5580cSJeff Kirsher 	 */
2482a6a5580cSJeff Kirsher 
2483a6a5580cSJeff Kirsher 	netif_carrier_off(netdev);
2484a6a5580cSJeff Kirsher 
2485a6a5580cSJeff Kirsher 	/* Do not call dev_init for a dynamic vnic.
2486a6a5580cSJeff Kirsher 	 * For a dynamic vnic, init_prov_info will be
2487a6a5580cSJeff Kirsher 	 * called later by an upper layer.
2488a6a5580cSJeff Kirsher 	 */
2489a6a5580cSJeff Kirsher 
24902b68c181SRoopa Prabhu 	if (!enic_is_dynamic(enic)) {
2491a6a5580cSJeff Kirsher 		err = vnic_dev_init(enic->vdev, 0);
2492a6a5580cSJeff Kirsher 		if (err) {
2493a6a5580cSJeff Kirsher 			dev_err(dev, "vNIC dev init failed, aborting\n");
2494a6a5580cSJeff Kirsher 			goto err_out_dev_close;
2495a6a5580cSJeff Kirsher 		}
2496a6a5580cSJeff Kirsher 	}
2497a6a5580cSJeff Kirsher 
2498a6a5580cSJeff Kirsher 	err = enic_dev_init(enic);
2499a6a5580cSJeff Kirsher 	if (err) {
2500a6a5580cSJeff Kirsher 		dev_err(dev, "Device initialization failed, aborting\n");
2501a6a5580cSJeff Kirsher 		goto err_out_dev_close;
2502a6a5580cSJeff Kirsher 	}
2503a6a5580cSJeff Kirsher 
2504822473b6Sgovindarajulu.v 	netif_set_real_num_tx_queues(netdev, enic->wq_count);
2505bf751ba8Sgovindarajulu.v 	netif_set_real_num_rx_queues(netdev, enic->rq_count);
2506822473b6Sgovindarajulu.v 
2507a6a5580cSJeff Kirsher 	/* Setup notification timer, HW reset task, and wq locks
2508a6a5580cSJeff Kirsher 	 */
2509a6a5580cSJeff Kirsher 
2510a6a5580cSJeff Kirsher 	init_timer(&enic->notify_timer);
2511a6a5580cSJeff Kirsher 	enic->notify_timer.function = enic_notify_timer;
2512a6a5580cSJeff Kirsher 	enic->notify_timer.data = (unsigned long)enic;
2513a6a5580cSJeff Kirsher 
25147c2ce6e6SSujith Sankar 	enic_set_rx_coal_setting(enic);
2515a6a5580cSJeff Kirsher 	INIT_WORK(&enic->reset, enic_reset);
2516a6a5580cSJeff Kirsher 	INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
2517a6a5580cSJeff Kirsher 
2518a6a5580cSJeff Kirsher 	for (i = 0; i < enic->wq_count; i++)
2519a6a5580cSJeff Kirsher 		spin_lock_init(&enic->wq_lock[i]);
2520a6a5580cSJeff Kirsher 
2521a6a5580cSJeff Kirsher 	/* Register net device
2522a6a5580cSJeff Kirsher 	 */
2523a6a5580cSJeff Kirsher 
2524a6a5580cSJeff Kirsher 	enic->port_mtu = enic->config.mtu;
2525a6a5580cSJeff Kirsher 	(void)enic_change_mtu(netdev, enic->port_mtu);
2526a6a5580cSJeff Kirsher 
2527a6a5580cSJeff Kirsher 	err = enic_set_mac_addr(netdev, enic->mac_addr);
2528a6a5580cSJeff Kirsher 	if (err) {
2529a6a5580cSJeff Kirsher 		dev_err(dev, "Invalid MAC address, aborting\n");
2530a6a5580cSJeff Kirsher 		goto err_out_dev_deinit;
2531a6a5580cSJeff Kirsher 	}
2532a6a5580cSJeff Kirsher 
2533a6a5580cSJeff Kirsher 	enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
25347c2ce6e6SSujith Sankar 	/* rx coalesce time already got initialized. This gets used
25357c2ce6e6SSujith Sankar 	 * if adaptive coal is turned off
25367c2ce6e6SSujith Sankar 	 */
2537a6a5580cSJeff Kirsher 	enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2538a6a5580cSJeff Kirsher 
25397335903cSRoopa Prabhu 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
2540a6a5580cSJeff Kirsher 		netdev->netdev_ops = &enic_netdev_dynamic_ops;
2541a6a5580cSJeff Kirsher 	else
2542a6a5580cSJeff Kirsher 		netdev->netdev_ops = &enic_netdev_ops;
2543a6a5580cSJeff Kirsher 
2544a6a5580cSJeff Kirsher 	netdev->watchdog_timeo = 2 * HZ;
2545f13bbc2fSNeel Patel 	enic_set_ethtool_ops(netdev);
2546a6a5580cSJeff Kirsher 
2547f646968fSPatrick McHardy 	netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2548a6a5580cSJeff Kirsher 	if (ENIC_SETTING(enic, LOOP)) {
2549f646968fSPatrick McHardy 		netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2550a6a5580cSJeff Kirsher 		enic->loop_enable = 1;
2551a6a5580cSJeff Kirsher 		enic->loop_tag = enic->config.loop_tag;
2552a6a5580cSJeff Kirsher 		dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2553a6a5580cSJeff Kirsher 	}
2554a6a5580cSJeff Kirsher 	if (ENIC_SETTING(enic, TXCSUM))
2555a6a5580cSJeff Kirsher 		netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2556a6a5580cSJeff Kirsher 	if (ENIC_SETTING(enic, TSO))
2557a6a5580cSJeff Kirsher 		netdev->hw_features |= NETIF_F_TSO |
2558a6a5580cSJeff Kirsher 			NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2559bf751ba8Sgovindarajulu.v 	if (ENIC_SETTING(enic, RSS))
2560bf751ba8Sgovindarajulu.v 		netdev->hw_features |= NETIF_F_RXHASH;
2561a6a5580cSJeff Kirsher 	if (ENIC_SETTING(enic, RXCSUM))
2562a6a5580cSJeff Kirsher 		netdev->hw_features |= NETIF_F_RXCSUM;
2563a6a5580cSJeff Kirsher 
2564a6a5580cSJeff Kirsher 	netdev->features |= netdev->hw_features;
2565a6a5580cSJeff Kirsher 
2566a145df23SGovindarajulu Varadarajan #ifdef CONFIG_RFS_ACCEL
2567a145df23SGovindarajulu Varadarajan 	netdev->hw_features |= NETIF_F_NTUPLE;
2568a145df23SGovindarajulu Varadarajan #endif
2569a145df23SGovindarajulu Varadarajan 
2570a6a5580cSJeff Kirsher 	if (using_dac)
2571a6a5580cSJeff Kirsher 		netdev->features |= NETIF_F_HIGHDMA;
2572a6a5580cSJeff Kirsher 
257301789349SJiri Pirko 	netdev->priv_flags |= IFF_UNICAST_FLT;
257401789349SJiri Pirko 
2575a6a5580cSJeff Kirsher 	err = register_netdev(netdev);
2576a6a5580cSJeff Kirsher 	if (err) {
2577a6a5580cSJeff Kirsher 		dev_err(dev, "Cannot register net device, aborting\n");
2578a6a5580cSJeff Kirsher 		goto err_out_dev_deinit;
2579a6a5580cSJeff Kirsher 	}
2580a03bb56eSGovindarajulu Varadarajan 	enic->rx_copybreak = RX_COPYBREAK_DEFAULT;
2581a6a5580cSJeff Kirsher 
2582a6a5580cSJeff Kirsher 	return 0;
2583a6a5580cSJeff Kirsher 
2584a6a5580cSJeff Kirsher err_out_dev_deinit:
2585a6a5580cSJeff Kirsher 	enic_dev_deinit(enic);
2586a6a5580cSJeff Kirsher err_out_dev_close:
2587a6a5580cSJeff Kirsher 	vnic_dev_close(enic->vdev);
25888749b427SRoopa Prabhu err_out_disable_sriov:
2589ca2b721dSRoopa Prabhu 	kfree(enic->pp);
2590ca2b721dSRoopa Prabhu err_out_disable_sriov_pp:
25918749b427SRoopa Prabhu #ifdef CONFIG_PCI_IOV
25928749b427SRoopa Prabhu 	if (enic_sriov_enabled(enic)) {
25938749b427SRoopa Prabhu 		pci_disable_sriov(pdev);
25948749b427SRoopa Prabhu 		enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
25958749b427SRoopa Prabhu 	}
2596a6a5580cSJeff Kirsher err_out_vnic_unregister:
25978749b427SRoopa Prabhu #endif
259835d87e33SRoopa Prabhu 	vnic_dev_unregister(enic->vdev);
2599a6a5580cSJeff Kirsher err_out_iounmap:
2600a6a5580cSJeff Kirsher 	enic_iounmap(enic);
2601a6a5580cSJeff Kirsher err_out_release_regions:
2602a6a5580cSJeff Kirsher 	pci_release_regions(pdev);
2603a6a5580cSJeff Kirsher err_out_disable_device:
2604a6a5580cSJeff Kirsher 	pci_disable_device(pdev);
2605a6a5580cSJeff Kirsher err_out_free_netdev:
2606a6a5580cSJeff Kirsher 	free_netdev(netdev);
2607a6a5580cSJeff Kirsher 
2608a6a5580cSJeff Kirsher 	return err;
2609a6a5580cSJeff Kirsher }
2610a6a5580cSJeff Kirsher 
2611854de92fSBill Pemberton static void enic_remove(struct pci_dev *pdev)
2612a6a5580cSJeff Kirsher {
2613a6a5580cSJeff Kirsher 	struct net_device *netdev = pci_get_drvdata(pdev);
2614a6a5580cSJeff Kirsher 
2615a6a5580cSJeff Kirsher 	if (netdev) {
2616a6a5580cSJeff Kirsher 		struct enic *enic = netdev_priv(netdev);
2617a6a5580cSJeff Kirsher 
2618a6a5580cSJeff Kirsher 		cancel_work_sync(&enic->reset);
2619a6a5580cSJeff Kirsher 		cancel_work_sync(&enic->change_mtu_work);
2620a6a5580cSJeff Kirsher 		unregister_netdev(netdev);
2621a6a5580cSJeff Kirsher 		enic_dev_deinit(enic);
2622a6a5580cSJeff Kirsher 		vnic_dev_close(enic->vdev);
26238749b427SRoopa Prabhu #ifdef CONFIG_PCI_IOV
26248749b427SRoopa Prabhu 		if (enic_sriov_enabled(enic)) {
26258749b427SRoopa Prabhu 			pci_disable_sriov(pdev);
26268749b427SRoopa Prabhu 			enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
26278749b427SRoopa Prabhu 		}
26288749b427SRoopa Prabhu #endif
26293f192795SRoopa Prabhu 		kfree(enic->pp);
2630a6a5580cSJeff Kirsher 		vnic_dev_unregister(enic->vdev);
2631a6a5580cSJeff Kirsher 		enic_iounmap(enic);
2632a6a5580cSJeff Kirsher 		pci_release_regions(pdev);
2633a6a5580cSJeff Kirsher 		pci_disable_device(pdev);
2634a6a5580cSJeff Kirsher 		free_netdev(netdev);
2635a6a5580cSJeff Kirsher 	}
2636a6a5580cSJeff Kirsher }
2637a6a5580cSJeff Kirsher 
2638a6a5580cSJeff Kirsher static struct pci_driver enic_driver = {
2639a6a5580cSJeff Kirsher 	.name = DRV_NAME,
2640a6a5580cSJeff Kirsher 	.id_table = enic_id_table,
2641a6a5580cSJeff Kirsher 	.probe = enic_probe,
2642854de92fSBill Pemberton 	.remove = enic_remove,
2643a6a5580cSJeff Kirsher };
2644a6a5580cSJeff Kirsher 
2645a6a5580cSJeff Kirsher static int __init enic_init_module(void)
2646a6a5580cSJeff Kirsher {
2647a6a5580cSJeff Kirsher 	pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2648a6a5580cSJeff Kirsher 
2649a6a5580cSJeff Kirsher 	return pci_register_driver(&enic_driver);
2650a6a5580cSJeff Kirsher }
2651a6a5580cSJeff Kirsher 
2652a6a5580cSJeff Kirsher static void __exit enic_cleanup_module(void)
2653a6a5580cSJeff Kirsher {
2654a6a5580cSJeff Kirsher 	pci_unregister_driver(&enic_driver);
2655a6a5580cSJeff Kirsher }
2656a6a5580cSJeff Kirsher 
2657a6a5580cSJeff Kirsher module_init(enic_init_module);
2658a6a5580cSJeff Kirsher module_exit(enic_cleanup_module);
2659