1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  *
18  */
19 
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_vlan.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/prefetch.h>
40 #include <net/ip6_checksum.h>
41 #include <linux/ktime.h>
42 #ifdef CONFIG_RFS_ACCEL
43 #include <linux/cpu_rmap.h>
44 #endif
45 #ifdef CONFIG_NET_RX_BUSY_POLL
46 #include <net/busy_poll.h>
47 #endif
48 
49 #include "cq_enet_desc.h"
50 #include "vnic_dev.h"
51 #include "vnic_intr.h"
52 #include "vnic_stats.h"
53 #include "vnic_vic.h"
54 #include "enic_res.h"
55 #include "enic.h"
56 #include "enic_dev.h"
57 #include "enic_pp.h"
58 #include "enic_clsf.h"
59 
60 #define ENIC_NOTIFY_TIMER_PERIOD	(2 * HZ)
61 #define WQ_ENET_MAX_DESC_LEN		(1 << WQ_ENET_LEN_BITS)
62 #define MAX_TSO				(1 << 16)
63 #define ENIC_DESC_MAX_SPLITS		(MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
64 
65 #define PCI_DEVICE_ID_CISCO_VIC_ENET         0x0043  /* ethernet vnic */
66 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN     0x0044  /* enet dynamic vnic */
67 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF      0x0071  /* enet SRIOV VF */
68 
69 #define RX_COPYBREAK_DEFAULT		256
70 
71 /* Supported devices */
72 static const struct pci_device_id enic_id_table[] = {
73 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
74 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
75 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
76 	{ 0, }	/* end of table */
77 };
78 
79 MODULE_DESCRIPTION(DRV_DESCRIPTION);
80 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(DRV_VERSION);
83 MODULE_DEVICE_TABLE(pci, enic_id_table);
84 
85 #define ENIC_LARGE_PKT_THRESHOLD		1000
86 #define ENIC_MAX_COALESCE_TIMERS		10
87 /*  Interrupt moderation table, which will be used to decide the
88  *  coalescing timer values
89  *  {rx_rate in Mbps, mapping percentage of the range}
90  */
91 struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
92 	{4000,  0},
93 	{4400, 10},
94 	{5060, 20},
95 	{5230, 30},
96 	{5540, 40},
97 	{5820, 50},
98 	{6120, 60},
99 	{6435, 70},
100 	{6745, 80},
101 	{7000, 90},
102 	{0xFFFFFFFF, 100}
103 };
104 
105 /* This table helps the driver to pick different ranges for rx coalescing
106  * timer depending on the link speed.
107  */
108 struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
109 	{0,  0}, /* 0  - 4  Gbps */
110 	{0,  3}, /* 4  - 10 Gbps */
111 	{3,  6}, /* 10 - 40 Gbps */
112 };
113 
114 int enic_is_dynamic(struct enic *enic)
115 {
116 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
117 }
118 
119 int enic_sriov_enabled(struct enic *enic)
120 {
121 	return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
122 }
123 
124 static int enic_is_sriov_vf(struct enic *enic)
125 {
126 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
127 }
128 
129 int enic_is_valid_vf(struct enic *enic, int vf)
130 {
131 #ifdef CONFIG_PCI_IOV
132 	return vf >= 0 && vf < enic->num_vfs;
133 #else
134 	return 0;
135 #endif
136 }
137 
138 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
139 {
140 	struct enic *enic = vnic_dev_priv(wq->vdev);
141 
142 	if (buf->sop)
143 		pci_unmap_single(enic->pdev, buf->dma_addr,
144 			buf->len, PCI_DMA_TODEVICE);
145 	else
146 		pci_unmap_page(enic->pdev, buf->dma_addr,
147 			buf->len, PCI_DMA_TODEVICE);
148 
149 	if (buf->os_buf)
150 		dev_kfree_skb_any(buf->os_buf);
151 }
152 
153 static void enic_wq_free_buf(struct vnic_wq *wq,
154 	struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
155 {
156 	enic_free_wq_buf(wq, buf);
157 }
158 
159 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
160 	u8 type, u16 q_number, u16 completed_index, void *opaque)
161 {
162 	struct enic *enic = vnic_dev_priv(vdev);
163 
164 	spin_lock(&enic->wq_lock[q_number]);
165 
166 	vnic_wq_service(&enic->wq[q_number], cq_desc,
167 		completed_index, enic_wq_free_buf,
168 		opaque);
169 
170 	if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) &&
171 	    vnic_wq_desc_avail(&enic->wq[q_number]) >=
172 	    (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
173 		netif_wake_subqueue(enic->netdev, q_number);
174 
175 	spin_unlock(&enic->wq_lock[q_number]);
176 
177 	return 0;
178 }
179 
180 static void enic_log_q_error(struct enic *enic)
181 {
182 	unsigned int i;
183 	u32 error_status;
184 
185 	for (i = 0; i < enic->wq_count; i++) {
186 		error_status = vnic_wq_error_status(&enic->wq[i]);
187 		if (error_status)
188 			netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
189 				i, error_status);
190 	}
191 
192 	for (i = 0; i < enic->rq_count; i++) {
193 		error_status = vnic_rq_error_status(&enic->rq[i]);
194 		if (error_status)
195 			netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
196 				i, error_status);
197 	}
198 }
199 
200 static void enic_msglvl_check(struct enic *enic)
201 {
202 	u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
203 
204 	if (msg_enable != enic->msg_enable) {
205 		netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
206 			enic->msg_enable, msg_enable);
207 		enic->msg_enable = msg_enable;
208 	}
209 }
210 
211 static void enic_mtu_check(struct enic *enic)
212 {
213 	u32 mtu = vnic_dev_mtu(enic->vdev);
214 	struct net_device *netdev = enic->netdev;
215 
216 	if (mtu && mtu != enic->port_mtu) {
217 		enic->port_mtu = mtu;
218 		if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
219 			mtu = max_t(int, ENIC_MIN_MTU,
220 				min_t(int, ENIC_MAX_MTU, mtu));
221 			if (mtu != netdev->mtu)
222 				schedule_work(&enic->change_mtu_work);
223 		} else {
224 			if (mtu < netdev->mtu)
225 				netdev_warn(netdev,
226 					"interface MTU (%d) set higher "
227 					"than switch port MTU (%d)\n",
228 					netdev->mtu, mtu);
229 		}
230 	}
231 }
232 
233 static void enic_link_check(struct enic *enic)
234 {
235 	int link_status = vnic_dev_link_status(enic->vdev);
236 	int carrier_ok = netif_carrier_ok(enic->netdev);
237 
238 	if (link_status && !carrier_ok) {
239 		netdev_info(enic->netdev, "Link UP\n");
240 		netif_carrier_on(enic->netdev);
241 	} else if (!link_status && carrier_ok) {
242 		netdev_info(enic->netdev, "Link DOWN\n");
243 		netif_carrier_off(enic->netdev);
244 	}
245 }
246 
247 static void enic_notify_check(struct enic *enic)
248 {
249 	enic_msglvl_check(enic);
250 	enic_mtu_check(enic);
251 	enic_link_check(enic);
252 }
253 
254 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
255 
256 static irqreturn_t enic_isr_legacy(int irq, void *data)
257 {
258 	struct net_device *netdev = data;
259 	struct enic *enic = netdev_priv(netdev);
260 	unsigned int io_intr = enic_legacy_io_intr();
261 	unsigned int err_intr = enic_legacy_err_intr();
262 	unsigned int notify_intr = enic_legacy_notify_intr();
263 	u32 pba;
264 
265 	vnic_intr_mask(&enic->intr[io_intr]);
266 
267 	pba = vnic_intr_legacy_pba(enic->legacy_pba);
268 	if (!pba) {
269 		vnic_intr_unmask(&enic->intr[io_intr]);
270 		return IRQ_NONE;	/* not our interrupt */
271 	}
272 
273 	if (ENIC_TEST_INTR(pba, notify_intr)) {
274 		vnic_intr_return_all_credits(&enic->intr[notify_intr]);
275 		enic_notify_check(enic);
276 	}
277 
278 	if (ENIC_TEST_INTR(pba, err_intr)) {
279 		vnic_intr_return_all_credits(&enic->intr[err_intr]);
280 		enic_log_q_error(enic);
281 		/* schedule recovery from WQ/RQ error */
282 		schedule_work(&enic->reset);
283 		return IRQ_HANDLED;
284 	}
285 
286 	if (ENIC_TEST_INTR(pba, io_intr))
287 		napi_schedule_irqoff(&enic->napi[0]);
288 	else
289 		vnic_intr_unmask(&enic->intr[io_intr]);
290 
291 	return IRQ_HANDLED;
292 }
293 
294 static irqreturn_t enic_isr_msi(int irq, void *data)
295 {
296 	struct enic *enic = data;
297 
298 	/* With MSI, there is no sharing of interrupts, so this is
299 	 * our interrupt and there is no need to ack it.  The device
300 	 * is not providing per-vector masking, so the OS will not
301 	 * write to PCI config space to mask/unmask the interrupt.
302 	 * We're using mask_on_assertion for MSI, so the device
303 	 * automatically masks the interrupt when the interrupt is
304 	 * generated.  Later, when exiting polling, the interrupt
305 	 * will be unmasked (see enic_poll).
306 	 *
307 	 * Also, the device uses the same PCIe Traffic Class (TC)
308 	 * for Memory Write data and MSI, so there are no ordering
309 	 * issues; the MSI will always arrive at the Root Complex
310 	 * _after_ corresponding Memory Writes (i.e. descriptor
311 	 * writes).
312 	 */
313 
314 	napi_schedule_irqoff(&enic->napi[0]);
315 
316 	return IRQ_HANDLED;
317 }
318 
319 static irqreturn_t enic_isr_msix(int irq, void *data)
320 {
321 	struct napi_struct *napi = data;
322 
323 	napi_schedule_irqoff(napi);
324 
325 	return IRQ_HANDLED;
326 }
327 
328 static irqreturn_t enic_isr_msix_err(int irq, void *data)
329 {
330 	struct enic *enic = data;
331 	unsigned int intr = enic_msix_err_intr(enic);
332 
333 	vnic_intr_return_all_credits(&enic->intr[intr]);
334 
335 	enic_log_q_error(enic);
336 
337 	/* schedule recovery from WQ/RQ error */
338 	schedule_work(&enic->reset);
339 
340 	return IRQ_HANDLED;
341 }
342 
343 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
344 {
345 	struct enic *enic = data;
346 	unsigned int intr = enic_msix_notify_intr(enic);
347 
348 	vnic_intr_return_all_credits(&enic->intr[intr]);
349 	enic_notify_check(enic);
350 
351 	return IRQ_HANDLED;
352 }
353 
354 static inline void enic_queue_wq_skb_cont(struct enic *enic,
355 	struct vnic_wq *wq, struct sk_buff *skb,
356 	unsigned int len_left, int loopback)
357 {
358 	const skb_frag_t *frag;
359 
360 	/* Queue additional data fragments */
361 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
362 		len_left -= skb_frag_size(frag);
363 		enic_queue_wq_desc_cont(wq, skb,
364 			skb_frag_dma_map(&enic->pdev->dev,
365 					 frag, 0, skb_frag_size(frag),
366 					 DMA_TO_DEVICE),
367 			skb_frag_size(frag),
368 			(len_left == 0),	/* EOP? */
369 			loopback);
370 	}
371 }
372 
373 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
374 	struct vnic_wq *wq, struct sk_buff *skb,
375 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
376 {
377 	unsigned int head_len = skb_headlen(skb);
378 	unsigned int len_left = skb->len - head_len;
379 	int eop = (len_left == 0);
380 
381 	/* Queue the main skb fragment. The fragments are no larger
382 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
383 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
384 	 * per fragment is queued.
385 	 */
386 	enic_queue_wq_desc(wq, skb,
387 		pci_map_single(enic->pdev, skb->data,
388 			head_len, PCI_DMA_TODEVICE),
389 		head_len,
390 		vlan_tag_insert, vlan_tag,
391 		eop, loopback);
392 
393 	if (!eop)
394 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
395 }
396 
397 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
398 	struct vnic_wq *wq, struct sk_buff *skb,
399 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
400 {
401 	unsigned int head_len = skb_headlen(skb);
402 	unsigned int len_left = skb->len - head_len;
403 	unsigned int hdr_len = skb_checksum_start_offset(skb);
404 	unsigned int csum_offset = hdr_len + skb->csum_offset;
405 	int eop = (len_left == 0);
406 
407 	/* Queue the main skb fragment. The fragments are no larger
408 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
409 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
410 	 * per fragment is queued.
411 	 */
412 	enic_queue_wq_desc_csum_l4(wq, skb,
413 		pci_map_single(enic->pdev, skb->data,
414 			head_len, PCI_DMA_TODEVICE),
415 		head_len,
416 		csum_offset,
417 		hdr_len,
418 		vlan_tag_insert, vlan_tag,
419 		eop, loopback);
420 
421 	if (!eop)
422 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
423 }
424 
425 static inline void enic_queue_wq_skb_tso(struct enic *enic,
426 	struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
427 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
428 {
429 	unsigned int frag_len_left = skb_headlen(skb);
430 	unsigned int len_left = skb->len - frag_len_left;
431 	unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
432 	int eop = (len_left == 0);
433 	unsigned int len;
434 	dma_addr_t dma_addr;
435 	unsigned int offset = 0;
436 	skb_frag_t *frag;
437 
438 	/* Preload TCP csum field with IP pseudo hdr calculated
439 	 * with IP length set to zero.  HW will later add in length
440 	 * to each TCP segment resulting from the TSO.
441 	 */
442 
443 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
444 		ip_hdr(skb)->check = 0;
445 		tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
446 			ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
447 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
448 		tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
449 			&ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
450 	}
451 
452 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
453 	 * for the main skb fragment
454 	 */
455 	while (frag_len_left) {
456 		len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
457 		dma_addr = pci_map_single(enic->pdev, skb->data + offset,
458 				len, PCI_DMA_TODEVICE);
459 		enic_queue_wq_desc_tso(wq, skb,
460 			dma_addr,
461 			len,
462 			mss, hdr_len,
463 			vlan_tag_insert, vlan_tag,
464 			eop && (len == frag_len_left), loopback);
465 		frag_len_left -= len;
466 		offset += len;
467 	}
468 
469 	if (eop)
470 		return;
471 
472 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
473 	 * for additional data fragments
474 	 */
475 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
476 		len_left -= skb_frag_size(frag);
477 		frag_len_left = skb_frag_size(frag);
478 		offset = 0;
479 
480 		while (frag_len_left) {
481 			len = min(frag_len_left,
482 				(unsigned int)WQ_ENET_MAX_DESC_LEN);
483 			dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
484 						    offset, len,
485 						    DMA_TO_DEVICE);
486 			enic_queue_wq_desc_cont(wq, skb,
487 				dma_addr,
488 				len,
489 				(len_left == 0) &&
490 				(len == frag_len_left),		/* EOP? */
491 				loopback);
492 			frag_len_left -= len;
493 			offset += len;
494 		}
495 	}
496 }
497 
498 static inline void enic_queue_wq_skb(struct enic *enic,
499 	struct vnic_wq *wq, struct sk_buff *skb)
500 {
501 	unsigned int mss = skb_shinfo(skb)->gso_size;
502 	unsigned int vlan_tag = 0;
503 	int vlan_tag_insert = 0;
504 	int loopback = 0;
505 
506 	if (vlan_tx_tag_present(skb)) {
507 		/* VLAN tag from trunking driver */
508 		vlan_tag_insert = 1;
509 		vlan_tag = vlan_tx_tag_get(skb);
510 	} else if (enic->loop_enable) {
511 		vlan_tag = enic->loop_tag;
512 		loopback = 1;
513 	}
514 
515 	if (mss)
516 		enic_queue_wq_skb_tso(enic, wq, skb, mss,
517 			vlan_tag_insert, vlan_tag, loopback);
518 	else if	(skb->ip_summed == CHECKSUM_PARTIAL)
519 		enic_queue_wq_skb_csum_l4(enic, wq, skb,
520 			vlan_tag_insert, vlan_tag, loopback);
521 	else
522 		enic_queue_wq_skb_vlan(enic, wq, skb,
523 			vlan_tag_insert, vlan_tag, loopback);
524 }
525 
526 /* netif_tx_lock held, process context with BHs disabled, or BH */
527 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
528 	struct net_device *netdev)
529 {
530 	struct enic *enic = netdev_priv(netdev);
531 	struct vnic_wq *wq;
532 	unsigned int txq_map;
533 	struct netdev_queue *txq;
534 
535 	if (skb->len <= 0) {
536 		dev_kfree_skb_any(skb);
537 		return NETDEV_TX_OK;
538 	}
539 
540 	txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
541 	wq = &enic->wq[txq_map];
542 	txq = netdev_get_tx_queue(netdev, txq_map);
543 
544 	/* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
545 	 * which is very likely.  In the off chance it's going to take
546 	 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
547 	 */
548 
549 	if (skb_shinfo(skb)->gso_size == 0 &&
550 	    skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
551 	    skb_linearize(skb)) {
552 		dev_kfree_skb_any(skb);
553 		return NETDEV_TX_OK;
554 	}
555 
556 	spin_lock(&enic->wq_lock[txq_map]);
557 
558 	if (vnic_wq_desc_avail(wq) <
559 	    skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
560 		netif_tx_stop_queue(txq);
561 		/* This is a hard error, log it */
562 		netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
563 		spin_unlock(&enic->wq_lock[txq_map]);
564 		return NETDEV_TX_BUSY;
565 	}
566 
567 	enic_queue_wq_skb(enic, wq, skb);
568 
569 	if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
570 		netif_tx_stop_queue(txq);
571 	if (!skb->xmit_more || netif_xmit_stopped(txq))
572 		vnic_wq_doorbell(wq);
573 
574 	spin_unlock(&enic->wq_lock[txq_map]);
575 
576 	return NETDEV_TX_OK;
577 }
578 
579 /* dev_base_lock rwlock held, nominally process context */
580 static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
581 						struct rtnl_link_stats64 *net_stats)
582 {
583 	struct enic *enic = netdev_priv(netdev);
584 	struct vnic_stats *stats;
585 
586 	enic_dev_stats_dump(enic, &stats);
587 
588 	net_stats->tx_packets = stats->tx.tx_frames_ok;
589 	net_stats->tx_bytes = stats->tx.tx_bytes_ok;
590 	net_stats->tx_errors = stats->tx.tx_errors;
591 	net_stats->tx_dropped = stats->tx.tx_drops;
592 
593 	net_stats->rx_packets = stats->rx.rx_frames_ok;
594 	net_stats->rx_bytes = stats->rx.rx_bytes_ok;
595 	net_stats->rx_errors = stats->rx.rx_errors;
596 	net_stats->multicast = stats->rx.rx_multicast_frames_ok;
597 	net_stats->rx_over_errors = enic->rq_truncated_pkts;
598 	net_stats->rx_crc_errors = enic->rq_bad_fcs;
599 	net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
600 
601 	return net_stats;
602 }
603 
604 static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
605 {
606 	struct enic *enic = netdev_priv(netdev);
607 
608 	if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
609 		unsigned int mc_count = netdev_mc_count(netdev);
610 
611 		netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
612 			    ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
613 
614 		return -ENOSPC;
615 	}
616 
617 	enic_dev_add_addr(enic, mc_addr);
618 	enic->mc_count++;
619 
620 	return 0;
621 }
622 
623 static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
624 {
625 	struct enic *enic = netdev_priv(netdev);
626 
627 	enic_dev_del_addr(enic, mc_addr);
628 	enic->mc_count--;
629 
630 	return 0;
631 }
632 
633 static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
634 {
635 	struct enic *enic = netdev_priv(netdev);
636 
637 	if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
638 		unsigned int uc_count = netdev_uc_count(netdev);
639 
640 		netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
641 			    ENIC_UNICAST_PERFECT_FILTERS, uc_count);
642 
643 		return -ENOSPC;
644 	}
645 
646 	enic_dev_add_addr(enic, uc_addr);
647 	enic->uc_count++;
648 
649 	return 0;
650 }
651 
652 static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
653 {
654 	struct enic *enic = netdev_priv(netdev);
655 
656 	enic_dev_del_addr(enic, uc_addr);
657 	enic->uc_count--;
658 
659 	return 0;
660 }
661 
662 void enic_reset_addr_lists(struct enic *enic)
663 {
664 	struct net_device *netdev = enic->netdev;
665 
666 	__dev_uc_unsync(netdev, NULL);
667 	__dev_mc_unsync(netdev, NULL);
668 
669 	enic->mc_count = 0;
670 	enic->uc_count = 0;
671 	enic->flags = 0;
672 }
673 
674 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
675 {
676 	struct enic *enic = netdev_priv(netdev);
677 
678 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
679 		if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
680 			return -EADDRNOTAVAIL;
681 	} else {
682 		if (!is_valid_ether_addr(addr))
683 			return -EADDRNOTAVAIL;
684 	}
685 
686 	memcpy(netdev->dev_addr, addr, netdev->addr_len);
687 
688 	return 0;
689 }
690 
691 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
692 {
693 	struct enic *enic = netdev_priv(netdev);
694 	struct sockaddr *saddr = p;
695 	char *addr = saddr->sa_data;
696 	int err;
697 
698 	if (netif_running(enic->netdev)) {
699 		err = enic_dev_del_station_addr(enic);
700 		if (err)
701 			return err;
702 	}
703 
704 	err = enic_set_mac_addr(netdev, addr);
705 	if (err)
706 		return err;
707 
708 	if (netif_running(enic->netdev)) {
709 		err = enic_dev_add_station_addr(enic);
710 		if (err)
711 			return err;
712 	}
713 
714 	return err;
715 }
716 
717 static int enic_set_mac_address(struct net_device *netdev, void *p)
718 {
719 	struct sockaddr *saddr = p;
720 	char *addr = saddr->sa_data;
721 	struct enic *enic = netdev_priv(netdev);
722 	int err;
723 
724 	err = enic_dev_del_station_addr(enic);
725 	if (err)
726 		return err;
727 
728 	err = enic_set_mac_addr(netdev, addr);
729 	if (err)
730 		return err;
731 
732 	return enic_dev_add_station_addr(enic);
733 }
734 
735 /* netif_tx_lock held, BHs disabled */
736 static void enic_set_rx_mode(struct net_device *netdev)
737 {
738 	struct enic *enic = netdev_priv(netdev);
739 	int directed = 1;
740 	int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
741 	int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
742 	int promisc = (netdev->flags & IFF_PROMISC) ||
743 		netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
744 	int allmulti = (netdev->flags & IFF_ALLMULTI) ||
745 		netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
746 	unsigned int flags = netdev->flags |
747 		(allmulti ? IFF_ALLMULTI : 0) |
748 		(promisc ? IFF_PROMISC : 0);
749 
750 	if (enic->flags != flags) {
751 		enic->flags = flags;
752 		enic_dev_packet_filter(enic, directed,
753 			multicast, broadcast, promisc, allmulti);
754 	}
755 
756 	if (!promisc) {
757 		__dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
758 		if (!allmulti)
759 			__dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
760 	}
761 }
762 
763 /* netif_tx_lock held, BHs disabled */
764 static void enic_tx_timeout(struct net_device *netdev)
765 {
766 	struct enic *enic = netdev_priv(netdev);
767 	schedule_work(&enic->reset);
768 }
769 
770 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
771 {
772 	struct enic *enic = netdev_priv(netdev);
773 	struct enic_port_profile *pp;
774 	int err;
775 
776 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
777 	if (err)
778 		return err;
779 
780 	if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
781 		if (vf == PORT_SELF_VF) {
782 			memcpy(pp->vf_mac, mac, ETH_ALEN);
783 			return 0;
784 		} else {
785 			/*
786 			 * For sriov vf's set the mac in hw
787 			 */
788 			ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
789 				vnic_dev_set_mac_addr, mac);
790 			return enic_dev_status_to_errno(err);
791 		}
792 	} else
793 		return -EINVAL;
794 }
795 
796 static int enic_set_vf_port(struct net_device *netdev, int vf,
797 	struct nlattr *port[])
798 {
799 	struct enic *enic = netdev_priv(netdev);
800 	struct enic_port_profile prev_pp;
801 	struct enic_port_profile *pp;
802 	int err = 0, restore_pp = 1;
803 
804 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
805 	if (err)
806 		return err;
807 
808 	if (!port[IFLA_PORT_REQUEST])
809 		return -EOPNOTSUPP;
810 
811 	memcpy(&prev_pp, pp, sizeof(*enic->pp));
812 	memset(pp, 0, sizeof(*enic->pp));
813 
814 	pp->set |= ENIC_SET_REQUEST;
815 	pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
816 
817 	if (port[IFLA_PORT_PROFILE]) {
818 		pp->set |= ENIC_SET_NAME;
819 		memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
820 			PORT_PROFILE_MAX);
821 	}
822 
823 	if (port[IFLA_PORT_INSTANCE_UUID]) {
824 		pp->set |= ENIC_SET_INSTANCE;
825 		memcpy(pp->instance_uuid,
826 			nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
827 	}
828 
829 	if (port[IFLA_PORT_HOST_UUID]) {
830 		pp->set |= ENIC_SET_HOST;
831 		memcpy(pp->host_uuid,
832 			nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
833 	}
834 
835 	if (vf == PORT_SELF_VF) {
836 		/* Special case handling: mac came from IFLA_VF_MAC */
837 		if (!is_zero_ether_addr(prev_pp.vf_mac))
838 			memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
839 
840 		if (is_zero_ether_addr(netdev->dev_addr))
841 			eth_hw_addr_random(netdev);
842 	} else {
843 		/* SR-IOV VF: get mac from adapter */
844 		ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
845 			vnic_dev_get_mac_addr, pp->mac_addr);
846 		if (err) {
847 			netdev_err(netdev, "Error getting mac for vf %d\n", vf);
848 			memcpy(pp, &prev_pp, sizeof(*pp));
849 			return enic_dev_status_to_errno(err);
850 		}
851 	}
852 
853 	err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
854 	if (err) {
855 		if (restore_pp) {
856 			/* Things are still the way they were: Implicit
857 			 * DISASSOCIATE failed
858 			 */
859 			memcpy(pp, &prev_pp, sizeof(*pp));
860 		} else {
861 			memset(pp, 0, sizeof(*pp));
862 			if (vf == PORT_SELF_VF)
863 				memset(netdev->dev_addr, 0, ETH_ALEN);
864 		}
865 	} else {
866 		/* Set flag to indicate that the port assoc/disassoc
867 		 * request has been sent out to fw
868 		 */
869 		pp->set |= ENIC_PORT_REQUEST_APPLIED;
870 
871 		/* If DISASSOCIATE, clean up all assigned/saved macaddresses */
872 		if (pp->request == PORT_REQUEST_DISASSOCIATE) {
873 			memset(pp->mac_addr, 0, ETH_ALEN);
874 			if (vf == PORT_SELF_VF)
875 				memset(netdev->dev_addr, 0, ETH_ALEN);
876 		}
877 	}
878 
879 	if (vf == PORT_SELF_VF)
880 		memset(pp->vf_mac, 0, ETH_ALEN);
881 
882 	return err;
883 }
884 
885 static int enic_get_vf_port(struct net_device *netdev, int vf,
886 	struct sk_buff *skb)
887 {
888 	struct enic *enic = netdev_priv(netdev);
889 	u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
890 	struct enic_port_profile *pp;
891 	int err;
892 
893 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
894 	if (err)
895 		return err;
896 
897 	if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
898 		return -ENODATA;
899 
900 	err = enic_process_get_pp_request(enic, vf, pp->request, &response);
901 	if (err)
902 		return err;
903 
904 	if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
905 	    nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
906 	    ((pp->set & ENIC_SET_NAME) &&
907 	     nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
908 	    ((pp->set & ENIC_SET_INSTANCE) &&
909 	     nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
910 		     pp->instance_uuid)) ||
911 	    ((pp->set & ENIC_SET_HOST) &&
912 	     nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
913 		goto nla_put_failure;
914 	return 0;
915 
916 nla_put_failure:
917 	return -EMSGSIZE;
918 }
919 
920 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
921 {
922 	struct enic *enic = vnic_dev_priv(rq->vdev);
923 
924 	if (!buf->os_buf)
925 		return;
926 
927 	pci_unmap_single(enic->pdev, buf->dma_addr,
928 		buf->len, PCI_DMA_FROMDEVICE);
929 	dev_kfree_skb_any(buf->os_buf);
930 	buf->os_buf = NULL;
931 }
932 
933 static int enic_rq_alloc_buf(struct vnic_rq *rq)
934 {
935 	struct enic *enic = vnic_dev_priv(rq->vdev);
936 	struct net_device *netdev = enic->netdev;
937 	struct sk_buff *skb;
938 	unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
939 	unsigned int os_buf_index = 0;
940 	dma_addr_t dma_addr;
941 	struct vnic_rq_buf *buf = rq->to_use;
942 
943 	if (buf->os_buf) {
944 		enic_queue_rq_desc(rq, buf->os_buf, os_buf_index, buf->dma_addr,
945 				   buf->len);
946 
947 		return 0;
948 	}
949 	skb = netdev_alloc_skb_ip_align(netdev, len);
950 	if (!skb)
951 		return -ENOMEM;
952 
953 	dma_addr = pci_map_single(enic->pdev, skb->data,
954 		len, PCI_DMA_FROMDEVICE);
955 
956 	enic_queue_rq_desc(rq, skb, os_buf_index,
957 		dma_addr, len);
958 
959 	return 0;
960 }
961 
962 static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
963 				      u32 pkt_len)
964 {
965 	if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
966 		pkt_size->large_pkt_bytes_cnt += pkt_len;
967 	else
968 		pkt_size->small_pkt_bytes_cnt += pkt_len;
969 }
970 
971 static bool enic_rxcopybreak(struct net_device *netdev, struct sk_buff **skb,
972 			     struct vnic_rq_buf *buf, u16 len)
973 {
974 	struct enic *enic = netdev_priv(netdev);
975 	struct sk_buff *new_skb;
976 
977 	if (len > enic->rx_copybreak)
978 		return false;
979 	new_skb = netdev_alloc_skb_ip_align(netdev, len);
980 	if (!new_skb)
981 		return false;
982 	pci_dma_sync_single_for_cpu(enic->pdev, buf->dma_addr, len,
983 				    DMA_FROM_DEVICE);
984 	memcpy(new_skb->data, (*skb)->data, len);
985 	*skb = new_skb;
986 
987 	return true;
988 }
989 
990 static void enic_rq_indicate_buf(struct vnic_rq *rq,
991 	struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
992 	int skipped, void *opaque)
993 {
994 	struct enic *enic = vnic_dev_priv(rq->vdev);
995 	struct net_device *netdev = enic->netdev;
996 	struct sk_buff *skb;
997 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
998 
999 	u8 type, color, eop, sop, ingress_port, vlan_stripped;
1000 	u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1001 	u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1002 	u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1003 	u8 packet_error;
1004 	u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1005 	u32 rss_hash;
1006 
1007 	if (skipped)
1008 		return;
1009 
1010 	skb = buf->os_buf;
1011 
1012 	cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1013 		&type, &color, &q_number, &completed_index,
1014 		&ingress_port, &fcoe, &eop, &sop, &rss_type,
1015 		&csum_not_calc, &rss_hash, &bytes_written,
1016 		&packet_error, &vlan_stripped, &vlan_tci, &checksum,
1017 		&fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1018 		&fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1019 		&ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1020 		&fcs_ok);
1021 
1022 	if (packet_error) {
1023 
1024 		if (!fcs_ok) {
1025 			if (bytes_written > 0)
1026 				enic->rq_bad_fcs++;
1027 			else if (bytes_written == 0)
1028 				enic->rq_truncated_pkts++;
1029 		}
1030 
1031 		pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1032 				 PCI_DMA_FROMDEVICE);
1033 		dev_kfree_skb_any(skb);
1034 		buf->os_buf = NULL;
1035 
1036 		return;
1037 	}
1038 
1039 	if (eop && bytes_written > 0) {
1040 
1041 		/* Good receive
1042 		 */
1043 
1044 		if (!enic_rxcopybreak(netdev, &skb, buf, bytes_written)) {
1045 			buf->os_buf = NULL;
1046 			pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1047 					 PCI_DMA_FROMDEVICE);
1048 		}
1049 		prefetch(skb->data - NET_IP_ALIGN);
1050 
1051 		skb_put(skb, bytes_written);
1052 		skb->protocol = eth_type_trans(skb, netdev);
1053 		skb_record_rx_queue(skb, q_number);
1054 		if (netdev->features & NETIF_F_RXHASH) {
1055 			skb_set_hash(skb, rss_hash,
1056 				     (rss_type &
1057 				      (NIC_CFG_RSS_HASH_TYPE_TCP_IPV6_EX |
1058 				       NIC_CFG_RSS_HASH_TYPE_TCP_IPV6 |
1059 				       NIC_CFG_RSS_HASH_TYPE_TCP_IPV4)) ?
1060 				     PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
1061 		}
1062 
1063 		/* Hardware does not provide whole packet checksum. It only
1064 		 * provides pseudo checksum. Since hw validates the packet
1065 		 * checksum but not provide us the checksum value. use
1066 		 * CHECSUM_UNNECESSARY.
1067 		 */
1068 		if ((netdev->features & NETIF_F_RXCSUM) && tcp_udp_csum_ok &&
1069 		    ipv4_csum_ok)
1070 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1071 
1072 		if (vlan_stripped)
1073 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1074 
1075 		skb_mark_napi_id(skb, &enic->napi[rq->index]);
1076 		if (enic_poll_busy_polling(rq) ||
1077 		    !(netdev->features & NETIF_F_GRO))
1078 			netif_receive_skb(skb);
1079 		else
1080 			napi_gro_receive(&enic->napi[q_number], skb);
1081 		if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1082 			enic_intr_update_pkt_size(&cq->pkt_size_counter,
1083 						  bytes_written);
1084 	} else {
1085 
1086 		/* Buffer overflow
1087 		 */
1088 
1089 		pci_unmap_single(enic->pdev, buf->dma_addr, buf->len,
1090 				 PCI_DMA_FROMDEVICE);
1091 		dev_kfree_skb_any(skb);
1092 		buf->os_buf = NULL;
1093 	}
1094 }
1095 
1096 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1097 	u8 type, u16 q_number, u16 completed_index, void *opaque)
1098 {
1099 	struct enic *enic = vnic_dev_priv(vdev);
1100 
1101 	vnic_rq_service(&enic->rq[q_number], cq_desc,
1102 		completed_index, VNIC_RQ_RETURN_DESC,
1103 		enic_rq_indicate_buf, opaque);
1104 
1105 	return 0;
1106 }
1107 
1108 static int enic_poll(struct napi_struct *napi, int budget)
1109 {
1110 	struct net_device *netdev = napi->dev;
1111 	struct enic *enic = netdev_priv(netdev);
1112 	unsigned int cq_rq = enic_cq_rq(enic, 0);
1113 	unsigned int cq_wq = enic_cq_wq(enic, 0);
1114 	unsigned int intr = enic_legacy_io_intr();
1115 	unsigned int rq_work_to_do = budget;
1116 	unsigned int wq_work_to_do = -1; /* no limit */
1117 	unsigned int  work_done, rq_work_done = 0, wq_work_done;
1118 	int err;
1119 
1120 	wq_work_done = vnic_cq_service(&enic->cq[cq_wq], wq_work_to_do,
1121 				       enic_wq_service, NULL);
1122 
1123 	if (!enic_poll_lock_napi(&enic->rq[cq_rq])) {
1124 		if (wq_work_done > 0)
1125 			vnic_intr_return_credits(&enic->intr[intr],
1126 						 wq_work_done,
1127 						 0 /* dont unmask intr */,
1128 						 0 /* dont reset intr timer */);
1129 		return rq_work_done;
1130 	}
1131 
1132 	if (budget > 0)
1133 		rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1134 			rq_work_to_do, enic_rq_service, NULL);
1135 
1136 	/* Accumulate intr event credits for this polling
1137 	 * cycle.  An intr event is the completion of a
1138 	 * a WQ or RQ packet.
1139 	 */
1140 
1141 	work_done = rq_work_done + wq_work_done;
1142 
1143 	if (work_done > 0)
1144 		vnic_intr_return_credits(&enic->intr[intr],
1145 			work_done,
1146 			0 /* don't unmask intr */,
1147 			0 /* don't reset intr timer */);
1148 
1149 	err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1150 
1151 	/* Buffer allocation failed. Stay in polling
1152 	 * mode so we can try to fill the ring again.
1153 	 */
1154 
1155 	if (err)
1156 		rq_work_done = rq_work_to_do;
1157 
1158 	if (rq_work_done < rq_work_to_do) {
1159 
1160 		/* Some work done, but not enough to stay in polling,
1161 		 * exit polling
1162 		 */
1163 
1164 		napi_complete(napi);
1165 		vnic_intr_unmask(&enic->intr[intr]);
1166 	}
1167 	enic_poll_unlock_napi(&enic->rq[cq_rq]);
1168 
1169 	return rq_work_done;
1170 }
1171 
1172 static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
1173 {
1174 	unsigned int intr = enic_msix_rq_intr(enic, rq->index);
1175 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1176 	u32 timer = cq->tobe_rx_coal_timeval;
1177 
1178 	if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
1179 		vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
1180 		cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
1181 	}
1182 }
1183 
1184 static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
1185 {
1186 	struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1187 	struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1188 	struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
1189 	int index;
1190 	u32 timer;
1191 	u32 range_start;
1192 	u32 traffic;
1193 	u64 delta;
1194 	ktime_t now = ktime_get();
1195 
1196 	delta = ktime_us_delta(now, cq->prev_ts);
1197 	if (delta < ENIC_AIC_TS_BREAK)
1198 		return;
1199 	cq->prev_ts = now;
1200 
1201 	traffic = pkt_size_counter->large_pkt_bytes_cnt +
1202 		  pkt_size_counter->small_pkt_bytes_cnt;
1203 	/* The table takes Mbps
1204 	 * traffic *= 8    => bits
1205 	 * traffic *= (10^6 / delta)    => bps
1206 	 * traffic /= 10^6     => Mbps
1207 	 *
1208 	 * Combining, traffic *= (8 / delta)
1209 	 */
1210 
1211 	traffic <<= 3;
1212 	traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
1213 
1214 	for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
1215 		if (traffic < mod_table[index].rx_rate)
1216 			break;
1217 	range_start = (pkt_size_counter->small_pkt_bytes_cnt >
1218 		       pkt_size_counter->large_pkt_bytes_cnt << 1) ?
1219 		      rx_coal->small_pkt_range_start :
1220 		      rx_coal->large_pkt_range_start;
1221 	timer = range_start + ((rx_coal->range_end - range_start) *
1222 			       mod_table[index].range_percent / 100);
1223 	/* Damping */
1224 	cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
1225 
1226 	pkt_size_counter->large_pkt_bytes_cnt = 0;
1227 	pkt_size_counter->small_pkt_bytes_cnt = 0;
1228 }
1229 
1230 #ifdef CONFIG_RFS_ACCEL
1231 static void enic_free_rx_cpu_rmap(struct enic *enic)
1232 {
1233 	free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
1234 	enic->netdev->rx_cpu_rmap = NULL;
1235 }
1236 
1237 static void enic_set_rx_cpu_rmap(struct enic *enic)
1238 {
1239 	int i, res;
1240 
1241 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
1242 		enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
1243 		if (unlikely(!enic->netdev->rx_cpu_rmap))
1244 			return;
1245 		for (i = 0; i < enic->rq_count; i++) {
1246 			res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
1247 					       enic->msix_entry[i].vector);
1248 			if (unlikely(res)) {
1249 				enic_free_rx_cpu_rmap(enic);
1250 				return;
1251 			}
1252 		}
1253 	}
1254 }
1255 
1256 #else
1257 
1258 static void enic_free_rx_cpu_rmap(struct enic *enic)
1259 {
1260 }
1261 
1262 static void enic_set_rx_cpu_rmap(struct enic *enic)
1263 {
1264 }
1265 
1266 #endif /* CONFIG_RFS_ACCEL */
1267 
1268 #ifdef CONFIG_NET_RX_BUSY_POLL
1269 int enic_busy_poll(struct napi_struct *napi)
1270 {
1271 	struct net_device *netdev = napi->dev;
1272 	struct enic *enic = netdev_priv(netdev);
1273 	unsigned int rq = (napi - &enic->napi[0]);
1274 	unsigned int cq = enic_cq_rq(enic, rq);
1275 	unsigned int intr = enic_msix_rq_intr(enic, rq);
1276 	unsigned int work_to_do = -1; /* clean all pkts possible */
1277 	unsigned int work_done;
1278 
1279 	if (!enic_poll_lock_poll(&enic->rq[rq]))
1280 		return LL_FLUSH_BUSY;
1281 	work_done = vnic_cq_service(&enic->cq[cq], work_to_do,
1282 				    enic_rq_service, NULL);
1283 
1284 	if (work_done > 0)
1285 		vnic_intr_return_credits(&enic->intr[intr],
1286 					 work_done, 0, 0);
1287 	vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1288 	if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1289 		enic_calc_int_moderation(enic, &enic->rq[rq]);
1290 	enic_poll_unlock_poll(&enic->rq[rq]);
1291 
1292 	return work_done;
1293 }
1294 #endif /* CONFIG_NET_RX_BUSY_POLL */
1295 
1296 static int enic_poll_msix_wq(struct napi_struct *napi, int budget)
1297 {
1298 	struct net_device *netdev = napi->dev;
1299 	struct enic *enic = netdev_priv(netdev);
1300 	unsigned int wq_index = (napi - &enic->napi[0]) - enic->rq_count;
1301 	struct vnic_wq *wq = &enic->wq[wq_index];
1302 	unsigned int cq;
1303 	unsigned int intr;
1304 	unsigned int wq_work_to_do = -1; /* clean all desc possible */
1305 	unsigned int wq_work_done;
1306 	unsigned int wq_irq;
1307 
1308 	wq_irq = wq->index;
1309 	cq = enic_cq_wq(enic, wq_irq);
1310 	intr = enic_msix_wq_intr(enic, wq_irq);
1311 	wq_work_done = vnic_cq_service(&enic->cq[cq], wq_work_to_do,
1312 				       enic_wq_service, NULL);
1313 
1314 	vnic_intr_return_credits(&enic->intr[intr], wq_work_done,
1315 				 0 /* don't unmask intr */,
1316 				 1 /* reset intr timer */);
1317 	if (!wq_work_done) {
1318 		napi_complete(napi);
1319 		vnic_intr_unmask(&enic->intr[intr]);
1320 		return 0;
1321 	}
1322 
1323 	return budget;
1324 }
1325 
1326 static int enic_poll_msix_rq(struct napi_struct *napi, int budget)
1327 {
1328 	struct net_device *netdev = napi->dev;
1329 	struct enic *enic = netdev_priv(netdev);
1330 	unsigned int rq = (napi - &enic->napi[0]);
1331 	unsigned int cq = enic_cq_rq(enic, rq);
1332 	unsigned int intr = enic_msix_rq_intr(enic, rq);
1333 	unsigned int work_to_do = budget;
1334 	unsigned int work_done = 0;
1335 	int err;
1336 
1337 	if (!enic_poll_lock_napi(&enic->rq[rq]))
1338 		return budget;
1339 	/* Service RQ
1340 	 */
1341 
1342 	if (budget > 0)
1343 		work_done = vnic_cq_service(&enic->cq[cq],
1344 			work_to_do, enic_rq_service, NULL);
1345 
1346 	/* Return intr event credits for this polling
1347 	 * cycle.  An intr event is the completion of a
1348 	 * RQ packet.
1349 	 */
1350 
1351 	if (work_done > 0)
1352 		vnic_intr_return_credits(&enic->intr[intr],
1353 			work_done,
1354 			0 /* don't unmask intr */,
1355 			0 /* don't reset intr timer */);
1356 
1357 	err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1358 
1359 	/* Buffer allocation failed. Stay in polling mode
1360 	 * so we can try to fill the ring again.
1361 	 */
1362 
1363 	if (err)
1364 		work_done = work_to_do;
1365 	if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1366 		/* Call the function which refreshes
1367 		 * the intr coalescing timer value based on
1368 		 * the traffic.  This is supported only in
1369 		 * the case of MSI-x mode
1370 		 */
1371 		enic_calc_int_moderation(enic, &enic->rq[rq]);
1372 
1373 	if (work_done < work_to_do) {
1374 
1375 		/* Some work done, but not enough to stay in polling,
1376 		 * exit polling
1377 		 */
1378 
1379 		napi_complete(napi);
1380 		if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1381 			enic_set_int_moderation(enic, &enic->rq[rq]);
1382 		vnic_intr_unmask(&enic->intr[intr]);
1383 	}
1384 	enic_poll_unlock_napi(&enic->rq[rq]);
1385 
1386 	return work_done;
1387 }
1388 
1389 static void enic_notify_timer(unsigned long data)
1390 {
1391 	struct enic *enic = (struct enic *)data;
1392 
1393 	enic_notify_check(enic);
1394 
1395 	mod_timer(&enic->notify_timer,
1396 		round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1397 }
1398 
1399 static void enic_free_intr(struct enic *enic)
1400 {
1401 	struct net_device *netdev = enic->netdev;
1402 	unsigned int i;
1403 
1404 	enic_free_rx_cpu_rmap(enic);
1405 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1406 	case VNIC_DEV_INTR_MODE_INTX:
1407 		free_irq(enic->pdev->irq, netdev);
1408 		break;
1409 	case VNIC_DEV_INTR_MODE_MSI:
1410 		free_irq(enic->pdev->irq, enic);
1411 		break;
1412 	case VNIC_DEV_INTR_MODE_MSIX:
1413 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1414 			if (enic->msix[i].requested)
1415 				free_irq(enic->msix_entry[i].vector,
1416 					enic->msix[i].devid);
1417 		break;
1418 	default:
1419 		break;
1420 	}
1421 }
1422 
1423 static int enic_request_intr(struct enic *enic)
1424 {
1425 	struct net_device *netdev = enic->netdev;
1426 	unsigned int i, intr;
1427 	int err = 0;
1428 
1429 	enic_set_rx_cpu_rmap(enic);
1430 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1431 
1432 	case VNIC_DEV_INTR_MODE_INTX:
1433 
1434 		err = request_irq(enic->pdev->irq, enic_isr_legacy,
1435 			IRQF_SHARED, netdev->name, netdev);
1436 		break;
1437 
1438 	case VNIC_DEV_INTR_MODE_MSI:
1439 
1440 		err = request_irq(enic->pdev->irq, enic_isr_msi,
1441 			0, netdev->name, enic);
1442 		break;
1443 
1444 	case VNIC_DEV_INTR_MODE_MSIX:
1445 
1446 		for (i = 0; i < enic->rq_count; i++) {
1447 			intr = enic_msix_rq_intr(enic, i);
1448 			snprintf(enic->msix[intr].devname,
1449 				sizeof(enic->msix[intr].devname),
1450 				"%.11s-rx-%d", netdev->name, i);
1451 			enic->msix[intr].isr = enic_isr_msix;
1452 			enic->msix[intr].devid = &enic->napi[i];
1453 		}
1454 
1455 		for (i = 0; i < enic->wq_count; i++) {
1456 			int wq = enic_cq_wq(enic, i);
1457 
1458 			intr = enic_msix_wq_intr(enic, i);
1459 			snprintf(enic->msix[intr].devname,
1460 				sizeof(enic->msix[intr].devname),
1461 				"%.11s-tx-%d", netdev->name, i);
1462 			enic->msix[intr].isr = enic_isr_msix;
1463 			enic->msix[intr].devid = &enic->napi[wq];
1464 		}
1465 
1466 		intr = enic_msix_err_intr(enic);
1467 		snprintf(enic->msix[intr].devname,
1468 			sizeof(enic->msix[intr].devname),
1469 			"%.11s-err", netdev->name);
1470 		enic->msix[intr].isr = enic_isr_msix_err;
1471 		enic->msix[intr].devid = enic;
1472 
1473 		intr = enic_msix_notify_intr(enic);
1474 		snprintf(enic->msix[intr].devname,
1475 			sizeof(enic->msix[intr].devname),
1476 			"%.11s-notify", netdev->name);
1477 		enic->msix[intr].isr = enic_isr_msix_notify;
1478 		enic->msix[intr].devid = enic;
1479 
1480 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1481 			enic->msix[i].requested = 0;
1482 
1483 		for (i = 0; i < enic->intr_count; i++) {
1484 			err = request_irq(enic->msix_entry[i].vector,
1485 				enic->msix[i].isr, 0,
1486 				enic->msix[i].devname,
1487 				enic->msix[i].devid);
1488 			if (err) {
1489 				enic_free_intr(enic);
1490 				break;
1491 			}
1492 			enic->msix[i].requested = 1;
1493 		}
1494 
1495 		break;
1496 
1497 	default:
1498 		break;
1499 	}
1500 
1501 	return err;
1502 }
1503 
1504 static void enic_synchronize_irqs(struct enic *enic)
1505 {
1506 	unsigned int i;
1507 
1508 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1509 	case VNIC_DEV_INTR_MODE_INTX:
1510 	case VNIC_DEV_INTR_MODE_MSI:
1511 		synchronize_irq(enic->pdev->irq);
1512 		break;
1513 	case VNIC_DEV_INTR_MODE_MSIX:
1514 		for (i = 0; i < enic->intr_count; i++)
1515 			synchronize_irq(enic->msix_entry[i].vector);
1516 		break;
1517 	default:
1518 		break;
1519 	}
1520 }
1521 
1522 static void enic_set_rx_coal_setting(struct enic *enic)
1523 {
1524 	unsigned int speed;
1525 	int index = -1;
1526 	struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1527 
1528 	/* If intr mode is not MSIX, do not do adaptive coalescing */
1529 	if (VNIC_DEV_INTR_MODE_MSIX != vnic_dev_get_intr_mode(enic->vdev)) {
1530 		netdev_info(enic->netdev, "INTR mode is not MSIX, Not initializing adaptive coalescing");
1531 		return;
1532 	}
1533 
1534 	/* 1. Read the link speed from fw
1535 	 * 2. Pick the default range for the speed
1536 	 * 3. Update it in enic->rx_coalesce_setting
1537 	 */
1538 	speed = vnic_dev_port_speed(enic->vdev);
1539 	if (ENIC_LINK_SPEED_10G < speed)
1540 		index = ENIC_LINK_40G_INDEX;
1541 	else if (ENIC_LINK_SPEED_4G < speed)
1542 		index = ENIC_LINK_10G_INDEX;
1543 	else
1544 		index = ENIC_LINK_4G_INDEX;
1545 
1546 	rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
1547 	rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
1548 	rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
1549 
1550 	/* Start with the value provided by UCSM */
1551 	for (index = 0; index < enic->rq_count; index++)
1552 		enic->cq[index].cur_rx_coal_timeval =
1553 				enic->config.intr_timer_usec;
1554 
1555 	rx_coal->use_adaptive_rx_coalesce = 1;
1556 }
1557 
1558 static int enic_dev_notify_set(struct enic *enic)
1559 {
1560 	int err;
1561 
1562 	spin_lock_bh(&enic->devcmd_lock);
1563 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1564 	case VNIC_DEV_INTR_MODE_INTX:
1565 		err = vnic_dev_notify_set(enic->vdev,
1566 			enic_legacy_notify_intr());
1567 		break;
1568 	case VNIC_DEV_INTR_MODE_MSIX:
1569 		err = vnic_dev_notify_set(enic->vdev,
1570 			enic_msix_notify_intr(enic));
1571 		break;
1572 	default:
1573 		err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1574 		break;
1575 	}
1576 	spin_unlock_bh(&enic->devcmd_lock);
1577 
1578 	return err;
1579 }
1580 
1581 static void enic_notify_timer_start(struct enic *enic)
1582 {
1583 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1584 	case VNIC_DEV_INTR_MODE_MSI:
1585 		mod_timer(&enic->notify_timer, jiffies);
1586 		break;
1587 	default:
1588 		/* Using intr for notification for INTx/MSI-X */
1589 		break;
1590 	}
1591 }
1592 
1593 /* rtnl lock is held, process context */
1594 static int enic_open(struct net_device *netdev)
1595 {
1596 	struct enic *enic = netdev_priv(netdev);
1597 	unsigned int i;
1598 	int err;
1599 
1600 	err = enic_request_intr(enic);
1601 	if (err) {
1602 		netdev_err(netdev, "Unable to request irq.\n");
1603 		return err;
1604 	}
1605 
1606 	err = enic_dev_notify_set(enic);
1607 	if (err) {
1608 		netdev_err(netdev,
1609 			"Failed to alloc notify buffer, aborting.\n");
1610 		goto err_out_free_intr;
1611 	}
1612 
1613 	for (i = 0; i < enic->rq_count; i++) {
1614 		vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1615 		/* Need at least one buffer on ring to get going */
1616 		if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1617 			netdev_err(netdev, "Unable to alloc receive buffers\n");
1618 			err = -ENOMEM;
1619 			goto err_out_free_rq;
1620 		}
1621 	}
1622 
1623 	for (i = 0; i < enic->wq_count; i++)
1624 		vnic_wq_enable(&enic->wq[i]);
1625 	for (i = 0; i < enic->rq_count; i++)
1626 		vnic_rq_enable(&enic->rq[i]);
1627 
1628 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1629 		enic_dev_add_station_addr(enic);
1630 
1631 	enic_set_rx_mode(netdev);
1632 
1633 	netif_tx_wake_all_queues(netdev);
1634 
1635 	for (i = 0; i < enic->rq_count; i++) {
1636 		enic_busy_poll_init_lock(&enic->rq[i]);
1637 		napi_enable(&enic->napi[i]);
1638 	}
1639 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1640 		for (i = 0; i < enic->wq_count; i++)
1641 			napi_enable(&enic->napi[enic_cq_wq(enic, i)]);
1642 	enic_dev_enable(enic);
1643 
1644 	for (i = 0; i < enic->intr_count; i++)
1645 		vnic_intr_unmask(&enic->intr[i]);
1646 
1647 	enic_notify_timer_start(enic);
1648 	enic_rfs_flw_tbl_init(enic);
1649 
1650 	return 0;
1651 
1652 err_out_free_rq:
1653 	for (i = 0; i < enic->rq_count; i++)
1654 		vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1655 	enic_dev_notify_unset(enic);
1656 err_out_free_intr:
1657 	enic_free_intr(enic);
1658 
1659 	return err;
1660 }
1661 
1662 /* rtnl lock is held, process context */
1663 static int enic_stop(struct net_device *netdev)
1664 {
1665 	struct enic *enic = netdev_priv(netdev);
1666 	unsigned int i;
1667 	int err;
1668 
1669 	for (i = 0; i < enic->intr_count; i++) {
1670 		vnic_intr_mask(&enic->intr[i]);
1671 		(void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1672 	}
1673 
1674 	enic_synchronize_irqs(enic);
1675 
1676 	del_timer_sync(&enic->notify_timer);
1677 	enic_rfs_flw_tbl_free(enic);
1678 
1679 	enic_dev_disable(enic);
1680 
1681 	for (i = 0; i < enic->rq_count; i++) {
1682 		napi_disable(&enic->napi[i]);
1683 		local_bh_disable();
1684 		while (!enic_poll_lock_napi(&enic->rq[i]))
1685 			mdelay(1);
1686 		local_bh_enable();
1687 	}
1688 
1689 	netif_carrier_off(netdev);
1690 	netif_tx_disable(netdev);
1691 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
1692 		for (i = 0; i < enic->wq_count; i++)
1693 			napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
1694 
1695 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1696 		enic_dev_del_station_addr(enic);
1697 
1698 	for (i = 0; i < enic->wq_count; i++) {
1699 		err = vnic_wq_disable(&enic->wq[i]);
1700 		if (err)
1701 			return err;
1702 	}
1703 	for (i = 0; i < enic->rq_count; i++) {
1704 		err = vnic_rq_disable(&enic->rq[i]);
1705 		if (err)
1706 			return err;
1707 	}
1708 
1709 	enic_dev_notify_unset(enic);
1710 	enic_free_intr(enic);
1711 
1712 	for (i = 0; i < enic->wq_count; i++)
1713 		vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1714 	for (i = 0; i < enic->rq_count; i++)
1715 		vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1716 	for (i = 0; i < enic->cq_count; i++)
1717 		vnic_cq_clean(&enic->cq[i]);
1718 	for (i = 0; i < enic->intr_count; i++)
1719 		vnic_intr_clean(&enic->intr[i]);
1720 
1721 	return 0;
1722 }
1723 
1724 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1725 {
1726 	struct enic *enic = netdev_priv(netdev);
1727 	int running = netif_running(netdev);
1728 
1729 	if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1730 		return -EINVAL;
1731 
1732 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
1733 		return -EOPNOTSUPP;
1734 
1735 	if (running)
1736 		enic_stop(netdev);
1737 
1738 	netdev->mtu = new_mtu;
1739 
1740 	if (netdev->mtu > enic->port_mtu)
1741 		netdev_warn(netdev,
1742 			"interface MTU (%d) set higher than port MTU (%d)\n",
1743 			netdev->mtu, enic->port_mtu);
1744 
1745 	if (running)
1746 		enic_open(netdev);
1747 
1748 	return 0;
1749 }
1750 
1751 static void enic_change_mtu_work(struct work_struct *work)
1752 {
1753 	struct enic *enic = container_of(work, struct enic, change_mtu_work);
1754 	struct net_device *netdev = enic->netdev;
1755 	int new_mtu = vnic_dev_mtu(enic->vdev);
1756 	int err;
1757 	unsigned int i;
1758 
1759 	new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1760 
1761 	rtnl_lock();
1762 
1763 	/* Stop RQ */
1764 	del_timer_sync(&enic->notify_timer);
1765 
1766 	for (i = 0; i < enic->rq_count; i++)
1767 		napi_disable(&enic->napi[i]);
1768 
1769 	vnic_intr_mask(&enic->intr[0]);
1770 	enic_synchronize_irqs(enic);
1771 	err = vnic_rq_disable(&enic->rq[0]);
1772 	if (err) {
1773 		rtnl_unlock();
1774 		netdev_err(netdev, "Unable to disable RQ.\n");
1775 		return;
1776 	}
1777 	vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1778 	vnic_cq_clean(&enic->cq[0]);
1779 	vnic_intr_clean(&enic->intr[0]);
1780 
1781 	/* Fill RQ with new_mtu-sized buffers */
1782 	netdev->mtu = new_mtu;
1783 	vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1784 	/* Need at least one buffer on ring to get going */
1785 	if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1786 		rtnl_unlock();
1787 		netdev_err(netdev, "Unable to alloc receive buffers.\n");
1788 		return;
1789 	}
1790 
1791 	/* Start RQ */
1792 	vnic_rq_enable(&enic->rq[0]);
1793 	napi_enable(&enic->napi[0]);
1794 	vnic_intr_unmask(&enic->intr[0]);
1795 	enic_notify_timer_start(enic);
1796 
1797 	rtnl_unlock();
1798 
1799 	netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1800 }
1801 
1802 #ifdef CONFIG_NET_POLL_CONTROLLER
1803 static void enic_poll_controller(struct net_device *netdev)
1804 {
1805 	struct enic *enic = netdev_priv(netdev);
1806 	struct vnic_dev *vdev = enic->vdev;
1807 	unsigned int i, intr;
1808 
1809 	switch (vnic_dev_get_intr_mode(vdev)) {
1810 	case VNIC_DEV_INTR_MODE_MSIX:
1811 		for (i = 0; i < enic->rq_count; i++) {
1812 			intr = enic_msix_rq_intr(enic, i);
1813 			enic_isr_msix(enic->msix_entry[intr].vector,
1814 				      &enic->napi[i]);
1815 		}
1816 
1817 		for (i = 0; i < enic->wq_count; i++) {
1818 			intr = enic_msix_wq_intr(enic, i);
1819 			enic_isr_msix(enic->msix_entry[intr].vector,
1820 				      &enic->napi[enic_cq_wq(enic, i)]);
1821 		}
1822 
1823 		break;
1824 	case VNIC_DEV_INTR_MODE_MSI:
1825 		enic_isr_msi(enic->pdev->irq, enic);
1826 		break;
1827 	case VNIC_DEV_INTR_MODE_INTX:
1828 		enic_isr_legacy(enic->pdev->irq, netdev);
1829 		break;
1830 	default:
1831 		break;
1832 	}
1833 }
1834 #endif
1835 
1836 static int enic_dev_wait(struct vnic_dev *vdev,
1837 	int (*start)(struct vnic_dev *, int),
1838 	int (*finished)(struct vnic_dev *, int *),
1839 	int arg)
1840 {
1841 	unsigned long time;
1842 	int done;
1843 	int err;
1844 
1845 	BUG_ON(in_interrupt());
1846 
1847 	err = start(vdev, arg);
1848 	if (err)
1849 		return err;
1850 
1851 	/* Wait for func to complete...2 seconds max
1852 	 */
1853 
1854 	time = jiffies + (HZ * 2);
1855 	do {
1856 
1857 		err = finished(vdev, &done);
1858 		if (err)
1859 			return err;
1860 
1861 		if (done)
1862 			return 0;
1863 
1864 		schedule_timeout_uninterruptible(HZ / 10);
1865 
1866 	} while (time_after(time, jiffies));
1867 
1868 	return -ETIMEDOUT;
1869 }
1870 
1871 static int enic_dev_open(struct enic *enic)
1872 {
1873 	int err;
1874 
1875 	err = enic_dev_wait(enic->vdev, vnic_dev_open,
1876 		vnic_dev_open_done, 0);
1877 	if (err)
1878 		dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1879 			err);
1880 
1881 	return err;
1882 }
1883 
1884 static int enic_dev_hang_reset(struct enic *enic)
1885 {
1886 	int err;
1887 
1888 	err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1889 		vnic_dev_hang_reset_done, 0);
1890 	if (err)
1891 		netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1892 			err);
1893 
1894 	return err;
1895 }
1896 
1897 int __enic_set_rsskey(struct enic *enic)
1898 {
1899 	union vnic_rss_key *rss_key_buf_va;
1900 	dma_addr_t rss_key_buf_pa;
1901 	int i, kidx, bidx, err;
1902 
1903 	rss_key_buf_va = pci_zalloc_consistent(enic->pdev,
1904 					       sizeof(union vnic_rss_key),
1905 					       &rss_key_buf_pa);
1906 	if (!rss_key_buf_va)
1907 		return -ENOMEM;
1908 
1909 	for (i = 0; i < ENIC_RSS_LEN; i++) {
1910 		kidx = i / ENIC_RSS_BYTES_PER_KEY;
1911 		bidx = i % ENIC_RSS_BYTES_PER_KEY;
1912 		rss_key_buf_va->key[kidx].b[bidx] = enic->rss_key[i];
1913 	}
1914 	spin_lock_bh(&enic->devcmd_lock);
1915 	err = enic_set_rss_key(enic,
1916 		rss_key_buf_pa,
1917 		sizeof(union vnic_rss_key));
1918 	spin_unlock_bh(&enic->devcmd_lock);
1919 
1920 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1921 		rss_key_buf_va, rss_key_buf_pa);
1922 
1923 	return err;
1924 }
1925 
1926 static int enic_set_rsskey(struct enic *enic)
1927 {
1928 	netdev_rss_key_fill(enic->rss_key, ENIC_RSS_LEN);
1929 
1930 	return __enic_set_rsskey(enic);
1931 }
1932 
1933 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1934 {
1935 	dma_addr_t rss_cpu_buf_pa;
1936 	union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1937 	unsigned int i;
1938 	int err;
1939 
1940 	rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1941 		sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1942 	if (!rss_cpu_buf_va)
1943 		return -ENOMEM;
1944 
1945 	for (i = 0; i < (1 << rss_hash_bits); i++)
1946 		(*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1947 
1948 	spin_lock_bh(&enic->devcmd_lock);
1949 	err = enic_set_rss_cpu(enic,
1950 		rss_cpu_buf_pa,
1951 		sizeof(union vnic_rss_cpu));
1952 	spin_unlock_bh(&enic->devcmd_lock);
1953 
1954 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1955 		rss_cpu_buf_va, rss_cpu_buf_pa);
1956 
1957 	return err;
1958 }
1959 
1960 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1961 	u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1962 {
1963 	const u8 tso_ipid_split_en = 0;
1964 	const u8 ig_vlan_strip_en = 1;
1965 	int err;
1966 
1967 	/* Enable VLAN tag stripping.
1968 	*/
1969 
1970 	spin_lock_bh(&enic->devcmd_lock);
1971 	err = enic_set_nic_cfg(enic,
1972 		rss_default_cpu, rss_hash_type,
1973 		rss_hash_bits, rss_base_cpu,
1974 		rss_enable, tso_ipid_split_en,
1975 		ig_vlan_strip_en);
1976 	spin_unlock_bh(&enic->devcmd_lock);
1977 
1978 	return err;
1979 }
1980 
1981 static int enic_set_rss_nic_cfg(struct enic *enic)
1982 {
1983 	struct device *dev = enic_get_dev(enic);
1984 	const u8 rss_default_cpu = 0;
1985 	const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1986 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1987 		NIC_CFG_RSS_HASH_TYPE_IPV6 |
1988 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1989 	const u8 rss_hash_bits = 7;
1990 	const u8 rss_base_cpu = 0;
1991 	u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1992 
1993 	if (rss_enable) {
1994 		if (!enic_set_rsskey(enic)) {
1995 			if (enic_set_rsscpu(enic, rss_hash_bits)) {
1996 				rss_enable = 0;
1997 				dev_warn(dev, "RSS disabled, "
1998 					"Failed to set RSS cpu indirection table.");
1999 			}
2000 		} else {
2001 			rss_enable = 0;
2002 			dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
2003 		}
2004 	}
2005 
2006 	return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
2007 		rss_hash_bits, rss_base_cpu, rss_enable);
2008 }
2009 
2010 static void enic_reset(struct work_struct *work)
2011 {
2012 	struct enic *enic = container_of(work, struct enic, reset);
2013 
2014 	if (!netif_running(enic->netdev))
2015 		return;
2016 
2017 	rtnl_lock();
2018 
2019 	spin_lock(&enic->enic_api_lock);
2020 	enic_dev_hang_notify(enic);
2021 	enic_stop(enic->netdev);
2022 	enic_dev_hang_reset(enic);
2023 	enic_reset_addr_lists(enic);
2024 	enic_init_vnic_resources(enic);
2025 	enic_set_rss_nic_cfg(enic);
2026 	enic_dev_set_ig_vlan_rewrite_mode(enic);
2027 	enic_open(enic->netdev);
2028 	spin_unlock(&enic->enic_api_lock);
2029 	call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
2030 
2031 	rtnl_unlock();
2032 }
2033 
2034 static int enic_set_intr_mode(struct enic *enic)
2035 {
2036 	unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
2037 	unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
2038 	unsigned int i;
2039 
2040 	/* Set interrupt mode (INTx, MSI, MSI-X) depending
2041 	 * on system capabilities.
2042 	 *
2043 	 * Try MSI-X first
2044 	 *
2045 	 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2046 	 * (the second to last INTR is used for WQ/RQ errors)
2047 	 * (the last INTR is used for notifications)
2048 	 */
2049 
2050 	BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2051 	for (i = 0; i < n + m + 2; i++)
2052 		enic->msix_entry[i].entry = i;
2053 
2054 	/* Use multiple RQs if RSS is enabled
2055 	 */
2056 
2057 	if (ENIC_SETTING(enic, RSS) &&
2058 	    enic->config.intr_mode < 1 &&
2059 	    enic->rq_count >= n &&
2060 	    enic->wq_count >= m &&
2061 	    enic->cq_count >= n + m &&
2062 	    enic->intr_count >= n + m + 2) {
2063 
2064 		if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2065 					  n + m + 2, n + m + 2) > 0) {
2066 
2067 			enic->rq_count = n;
2068 			enic->wq_count = m;
2069 			enic->cq_count = n + m;
2070 			enic->intr_count = n + m + 2;
2071 
2072 			vnic_dev_set_intr_mode(enic->vdev,
2073 				VNIC_DEV_INTR_MODE_MSIX);
2074 
2075 			return 0;
2076 		}
2077 	}
2078 
2079 	if (enic->config.intr_mode < 1 &&
2080 	    enic->rq_count >= 1 &&
2081 	    enic->wq_count >= m &&
2082 	    enic->cq_count >= 1 + m &&
2083 	    enic->intr_count >= 1 + m + 2) {
2084 		if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
2085 					  1 + m + 2, 1 + m + 2) > 0) {
2086 
2087 			enic->rq_count = 1;
2088 			enic->wq_count = m;
2089 			enic->cq_count = 1 + m;
2090 			enic->intr_count = 1 + m + 2;
2091 
2092 			vnic_dev_set_intr_mode(enic->vdev,
2093 				VNIC_DEV_INTR_MODE_MSIX);
2094 
2095 			return 0;
2096 		}
2097 	}
2098 
2099 	/* Next try MSI
2100 	 *
2101 	 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2102 	 */
2103 
2104 	if (enic->config.intr_mode < 2 &&
2105 	    enic->rq_count >= 1 &&
2106 	    enic->wq_count >= 1 &&
2107 	    enic->cq_count >= 2 &&
2108 	    enic->intr_count >= 1 &&
2109 	    !pci_enable_msi(enic->pdev)) {
2110 
2111 		enic->rq_count = 1;
2112 		enic->wq_count = 1;
2113 		enic->cq_count = 2;
2114 		enic->intr_count = 1;
2115 
2116 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2117 
2118 		return 0;
2119 	}
2120 
2121 	/* Next try INTx
2122 	 *
2123 	 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2124 	 * (the first INTR is used for WQ/RQ)
2125 	 * (the second INTR is used for WQ/RQ errors)
2126 	 * (the last INTR is used for notifications)
2127 	 */
2128 
2129 	if (enic->config.intr_mode < 3 &&
2130 	    enic->rq_count >= 1 &&
2131 	    enic->wq_count >= 1 &&
2132 	    enic->cq_count >= 2 &&
2133 	    enic->intr_count >= 3) {
2134 
2135 		enic->rq_count = 1;
2136 		enic->wq_count = 1;
2137 		enic->cq_count = 2;
2138 		enic->intr_count = 3;
2139 
2140 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2141 
2142 		return 0;
2143 	}
2144 
2145 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2146 
2147 	return -EINVAL;
2148 }
2149 
2150 static void enic_clear_intr_mode(struct enic *enic)
2151 {
2152 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2153 	case VNIC_DEV_INTR_MODE_MSIX:
2154 		pci_disable_msix(enic->pdev);
2155 		break;
2156 	case VNIC_DEV_INTR_MODE_MSI:
2157 		pci_disable_msi(enic->pdev);
2158 		break;
2159 	default:
2160 		break;
2161 	}
2162 
2163 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2164 }
2165 
2166 static const struct net_device_ops enic_netdev_dynamic_ops = {
2167 	.ndo_open		= enic_open,
2168 	.ndo_stop		= enic_stop,
2169 	.ndo_start_xmit		= enic_hard_start_xmit,
2170 	.ndo_get_stats64	= enic_get_stats,
2171 	.ndo_validate_addr	= eth_validate_addr,
2172 	.ndo_set_rx_mode	= enic_set_rx_mode,
2173 	.ndo_set_mac_address	= enic_set_mac_address_dynamic,
2174 	.ndo_change_mtu		= enic_change_mtu,
2175 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2176 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2177 	.ndo_tx_timeout		= enic_tx_timeout,
2178 	.ndo_set_vf_port	= enic_set_vf_port,
2179 	.ndo_get_vf_port	= enic_get_vf_port,
2180 	.ndo_set_vf_mac		= enic_set_vf_mac,
2181 #ifdef CONFIG_NET_POLL_CONTROLLER
2182 	.ndo_poll_controller	= enic_poll_controller,
2183 #endif
2184 #ifdef CONFIG_RFS_ACCEL
2185 	.ndo_rx_flow_steer	= enic_rx_flow_steer,
2186 #endif
2187 #ifdef CONFIG_NET_RX_BUSY_POLL
2188 	.ndo_busy_poll		= enic_busy_poll,
2189 #endif
2190 };
2191 
2192 static const struct net_device_ops enic_netdev_ops = {
2193 	.ndo_open		= enic_open,
2194 	.ndo_stop		= enic_stop,
2195 	.ndo_start_xmit		= enic_hard_start_xmit,
2196 	.ndo_get_stats64	= enic_get_stats,
2197 	.ndo_validate_addr	= eth_validate_addr,
2198 	.ndo_set_mac_address	= enic_set_mac_address,
2199 	.ndo_set_rx_mode	= enic_set_rx_mode,
2200 	.ndo_change_mtu		= enic_change_mtu,
2201 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2202 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2203 	.ndo_tx_timeout		= enic_tx_timeout,
2204 	.ndo_set_vf_port	= enic_set_vf_port,
2205 	.ndo_get_vf_port	= enic_get_vf_port,
2206 	.ndo_set_vf_mac		= enic_set_vf_mac,
2207 #ifdef CONFIG_NET_POLL_CONTROLLER
2208 	.ndo_poll_controller	= enic_poll_controller,
2209 #endif
2210 #ifdef CONFIG_RFS_ACCEL
2211 	.ndo_rx_flow_steer	= enic_rx_flow_steer,
2212 #endif
2213 #ifdef CONFIG_NET_RX_BUSY_POLL
2214 	.ndo_busy_poll		= enic_busy_poll,
2215 #endif
2216 };
2217 
2218 static void enic_dev_deinit(struct enic *enic)
2219 {
2220 	unsigned int i;
2221 
2222 	for (i = 0; i < enic->rq_count; i++) {
2223 		napi_hash_del(&enic->napi[i]);
2224 		netif_napi_del(&enic->napi[i]);
2225 	}
2226 	if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
2227 		for (i = 0; i < enic->wq_count; i++)
2228 			netif_napi_del(&enic->napi[enic_cq_wq(enic, i)]);
2229 
2230 	enic_free_vnic_resources(enic);
2231 	enic_clear_intr_mode(enic);
2232 }
2233 
2234 static int enic_dev_init(struct enic *enic)
2235 {
2236 	struct device *dev = enic_get_dev(enic);
2237 	struct net_device *netdev = enic->netdev;
2238 	unsigned int i;
2239 	int err;
2240 
2241 	/* Get interrupt coalesce timer info */
2242 	err = enic_dev_intr_coal_timer_info(enic);
2243 	if (err) {
2244 		dev_warn(dev, "Using default conversion factor for "
2245 			"interrupt coalesce timer\n");
2246 		vnic_dev_intr_coal_timer_info_default(enic->vdev);
2247 	}
2248 
2249 	/* Get vNIC configuration
2250 	 */
2251 
2252 	err = enic_get_vnic_config(enic);
2253 	if (err) {
2254 		dev_err(dev, "Get vNIC configuration failed, aborting\n");
2255 		return err;
2256 	}
2257 
2258 	/* Get available resource counts
2259 	 */
2260 
2261 	enic_get_res_counts(enic);
2262 
2263 	/* Set interrupt mode based on resource counts and system
2264 	 * capabilities
2265 	 */
2266 
2267 	err = enic_set_intr_mode(enic);
2268 	if (err) {
2269 		dev_err(dev, "Failed to set intr mode based on resource "
2270 			"counts and system capabilities, aborting\n");
2271 		return err;
2272 	}
2273 
2274 	/* Allocate and configure vNIC resources
2275 	 */
2276 
2277 	err = enic_alloc_vnic_resources(enic);
2278 	if (err) {
2279 		dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2280 		goto err_out_free_vnic_resources;
2281 	}
2282 
2283 	enic_init_vnic_resources(enic);
2284 
2285 	err = enic_set_rss_nic_cfg(enic);
2286 	if (err) {
2287 		dev_err(dev, "Failed to config nic, aborting\n");
2288 		goto err_out_free_vnic_resources;
2289 	}
2290 
2291 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2292 	default:
2293 		netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2294 		napi_hash_add(&enic->napi[0]);
2295 		break;
2296 	case VNIC_DEV_INTR_MODE_MSIX:
2297 		for (i = 0; i < enic->rq_count; i++) {
2298 			netif_napi_add(netdev, &enic->napi[i],
2299 				enic_poll_msix_rq, NAPI_POLL_WEIGHT);
2300 			napi_hash_add(&enic->napi[i]);
2301 		}
2302 		for (i = 0; i < enic->wq_count; i++)
2303 			netif_napi_add(netdev, &enic->napi[enic_cq_wq(enic, i)],
2304 				       enic_poll_msix_wq, NAPI_POLL_WEIGHT);
2305 		break;
2306 	}
2307 
2308 	return 0;
2309 
2310 err_out_free_vnic_resources:
2311 	enic_clear_intr_mode(enic);
2312 	enic_free_vnic_resources(enic);
2313 
2314 	return err;
2315 }
2316 
2317 static void enic_iounmap(struct enic *enic)
2318 {
2319 	unsigned int i;
2320 
2321 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2322 		if (enic->bar[i].vaddr)
2323 			iounmap(enic->bar[i].vaddr);
2324 }
2325 
2326 static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2327 {
2328 	struct device *dev = &pdev->dev;
2329 	struct net_device *netdev;
2330 	struct enic *enic;
2331 	int using_dac = 0;
2332 	unsigned int i;
2333 	int err;
2334 #ifdef CONFIG_PCI_IOV
2335 	int pos = 0;
2336 #endif
2337 	int num_pps = 1;
2338 
2339 	/* Allocate net device structure and initialize.  Private
2340 	 * instance data is initialized to zero.
2341 	 */
2342 
2343 	netdev = alloc_etherdev_mqs(sizeof(struct enic),
2344 				    ENIC_RQ_MAX, ENIC_WQ_MAX);
2345 	if (!netdev)
2346 		return -ENOMEM;
2347 
2348 	pci_set_drvdata(pdev, netdev);
2349 
2350 	SET_NETDEV_DEV(netdev, &pdev->dev);
2351 
2352 	enic = netdev_priv(netdev);
2353 	enic->netdev = netdev;
2354 	enic->pdev = pdev;
2355 
2356 	/* Setup PCI resources
2357 	 */
2358 
2359 	err = pci_enable_device_mem(pdev);
2360 	if (err) {
2361 		dev_err(dev, "Cannot enable PCI device, aborting\n");
2362 		goto err_out_free_netdev;
2363 	}
2364 
2365 	err = pci_request_regions(pdev, DRV_NAME);
2366 	if (err) {
2367 		dev_err(dev, "Cannot request PCI regions, aborting\n");
2368 		goto err_out_disable_device;
2369 	}
2370 
2371 	pci_set_master(pdev);
2372 
2373 	/* Query PCI controller on system for DMA addressing
2374 	 * limitation for the device.  Try 64-bit first, and
2375 	 * fail to 32-bit.
2376 	 */
2377 
2378 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2379 	if (err) {
2380 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2381 		if (err) {
2382 			dev_err(dev, "No usable DMA configuration, aborting\n");
2383 			goto err_out_release_regions;
2384 		}
2385 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2386 		if (err) {
2387 			dev_err(dev, "Unable to obtain %u-bit DMA "
2388 				"for consistent allocations, aborting\n", 32);
2389 			goto err_out_release_regions;
2390 		}
2391 	} else {
2392 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2393 		if (err) {
2394 			dev_err(dev, "Unable to obtain %u-bit DMA "
2395 				"for consistent allocations, aborting\n", 64);
2396 			goto err_out_release_regions;
2397 		}
2398 		using_dac = 1;
2399 	}
2400 
2401 	/* Map vNIC resources from BAR0-5
2402 	 */
2403 
2404 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2405 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2406 			continue;
2407 		enic->bar[i].len = pci_resource_len(pdev, i);
2408 		enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2409 		if (!enic->bar[i].vaddr) {
2410 			dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2411 			err = -ENODEV;
2412 			goto err_out_iounmap;
2413 		}
2414 		enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2415 	}
2416 
2417 	/* Register vNIC device
2418 	 */
2419 
2420 	enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2421 		ARRAY_SIZE(enic->bar));
2422 	if (!enic->vdev) {
2423 		dev_err(dev, "vNIC registration failed, aborting\n");
2424 		err = -ENODEV;
2425 		goto err_out_iounmap;
2426 	}
2427 
2428 #ifdef CONFIG_PCI_IOV
2429 	/* Get number of subvnics */
2430 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2431 	if (pos) {
2432 		pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
2433 			&enic->num_vfs);
2434 		if (enic->num_vfs) {
2435 			err = pci_enable_sriov(pdev, enic->num_vfs);
2436 			if (err) {
2437 				dev_err(dev, "SRIOV enable failed, aborting."
2438 					" pci_enable_sriov() returned %d\n",
2439 					err);
2440 				goto err_out_vnic_unregister;
2441 			}
2442 			enic->priv_flags |= ENIC_SRIOV_ENABLED;
2443 			num_pps = enic->num_vfs;
2444 		}
2445 	}
2446 #endif
2447 
2448 	/* Allocate structure for port profiles */
2449 	enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
2450 	if (!enic->pp) {
2451 		err = -ENOMEM;
2452 		goto err_out_disable_sriov_pp;
2453 	}
2454 
2455 	/* Issue device open to get device in known state
2456 	 */
2457 
2458 	err = enic_dev_open(enic);
2459 	if (err) {
2460 		dev_err(dev, "vNIC dev open failed, aborting\n");
2461 		goto err_out_disable_sriov;
2462 	}
2463 
2464 	/* Setup devcmd lock
2465 	 */
2466 
2467 	spin_lock_init(&enic->devcmd_lock);
2468 	spin_lock_init(&enic->enic_api_lock);
2469 
2470 	/*
2471 	 * Set ingress vlan rewrite mode before vnic initialization
2472 	 */
2473 
2474 	err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2475 	if (err) {
2476 		dev_err(dev,
2477 			"Failed to set ingress vlan rewrite mode, aborting.\n");
2478 		goto err_out_dev_close;
2479 	}
2480 
2481 	/* Issue device init to initialize the vnic-to-switch link.
2482 	 * We'll start with carrier off and wait for link UP
2483 	 * notification later to turn on carrier.  We don't need
2484 	 * to wait here for the vnic-to-switch link initialization
2485 	 * to complete; link UP notification is the indication that
2486 	 * the process is complete.
2487 	 */
2488 
2489 	netif_carrier_off(netdev);
2490 
2491 	/* Do not call dev_init for a dynamic vnic.
2492 	 * For a dynamic vnic, init_prov_info will be
2493 	 * called later by an upper layer.
2494 	 */
2495 
2496 	if (!enic_is_dynamic(enic)) {
2497 		err = vnic_dev_init(enic->vdev, 0);
2498 		if (err) {
2499 			dev_err(dev, "vNIC dev init failed, aborting\n");
2500 			goto err_out_dev_close;
2501 		}
2502 	}
2503 
2504 	err = enic_dev_init(enic);
2505 	if (err) {
2506 		dev_err(dev, "Device initialization failed, aborting\n");
2507 		goto err_out_dev_close;
2508 	}
2509 
2510 	netif_set_real_num_tx_queues(netdev, enic->wq_count);
2511 	netif_set_real_num_rx_queues(netdev, enic->rq_count);
2512 
2513 	/* Setup notification timer, HW reset task, and wq locks
2514 	 */
2515 
2516 	init_timer(&enic->notify_timer);
2517 	enic->notify_timer.function = enic_notify_timer;
2518 	enic->notify_timer.data = (unsigned long)enic;
2519 
2520 	enic_set_rx_coal_setting(enic);
2521 	INIT_WORK(&enic->reset, enic_reset);
2522 	INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
2523 
2524 	for (i = 0; i < enic->wq_count; i++)
2525 		spin_lock_init(&enic->wq_lock[i]);
2526 
2527 	/* Register net device
2528 	 */
2529 
2530 	enic->port_mtu = enic->config.mtu;
2531 	(void)enic_change_mtu(netdev, enic->port_mtu);
2532 
2533 	err = enic_set_mac_addr(netdev, enic->mac_addr);
2534 	if (err) {
2535 		dev_err(dev, "Invalid MAC address, aborting\n");
2536 		goto err_out_dev_deinit;
2537 	}
2538 
2539 	enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2540 	/* rx coalesce time already got initialized. This gets used
2541 	 * if adaptive coal is turned off
2542 	 */
2543 	enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2544 
2545 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
2546 		netdev->netdev_ops = &enic_netdev_dynamic_ops;
2547 	else
2548 		netdev->netdev_ops = &enic_netdev_ops;
2549 
2550 	netdev->watchdog_timeo = 2 * HZ;
2551 	enic_set_ethtool_ops(netdev);
2552 
2553 	netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2554 	if (ENIC_SETTING(enic, LOOP)) {
2555 		netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2556 		enic->loop_enable = 1;
2557 		enic->loop_tag = enic->config.loop_tag;
2558 		dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2559 	}
2560 	if (ENIC_SETTING(enic, TXCSUM))
2561 		netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2562 	if (ENIC_SETTING(enic, TSO))
2563 		netdev->hw_features |= NETIF_F_TSO |
2564 			NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2565 	if (ENIC_SETTING(enic, RSS))
2566 		netdev->hw_features |= NETIF_F_RXHASH;
2567 	if (ENIC_SETTING(enic, RXCSUM))
2568 		netdev->hw_features |= NETIF_F_RXCSUM;
2569 
2570 	netdev->features |= netdev->hw_features;
2571 
2572 #ifdef CONFIG_RFS_ACCEL
2573 	netdev->hw_features |= NETIF_F_NTUPLE;
2574 #endif
2575 
2576 	if (using_dac)
2577 		netdev->features |= NETIF_F_HIGHDMA;
2578 
2579 	netdev->priv_flags |= IFF_UNICAST_FLT;
2580 
2581 	err = register_netdev(netdev);
2582 	if (err) {
2583 		dev_err(dev, "Cannot register net device, aborting\n");
2584 		goto err_out_dev_deinit;
2585 	}
2586 	enic->rx_copybreak = RX_COPYBREAK_DEFAULT;
2587 
2588 	return 0;
2589 
2590 err_out_dev_deinit:
2591 	enic_dev_deinit(enic);
2592 err_out_dev_close:
2593 	vnic_dev_close(enic->vdev);
2594 err_out_disable_sriov:
2595 	kfree(enic->pp);
2596 err_out_disable_sriov_pp:
2597 #ifdef CONFIG_PCI_IOV
2598 	if (enic_sriov_enabled(enic)) {
2599 		pci_disable_sriov(pdev);
2600 		enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2601 	}
2602 err_out_vnic_unregister:
2603 #endif
2604 	vnic_dev_unregister(enic->vdev);
2605 err_out_iounmap:
2606 	enic_iounmap(enic);
2607 err_out_release_regions:
2608 	pci_release_regions(pdev);
2609 err_out_disable_device:
2610 	pci_disable_device(pdev);
2611 err_out_free_netdev:
2612 	free_netdev(netdev);
2613 
2614 	return err;
2615 }
2616 
2617 static void enic_remove(struct pci_dev *pdev)
2618 {
2619 	struct net_device *netdev = pci_get_drvdata(pdev);
2620 
2621 	if (netdev) {
2622 		struct enic *enic = netdev_priv(netdev);
2623 
2624 		cancel_work_sync(&enic->reset);
2625 		cancel_work_sync(&enic->change_mtu_work);
2626 		unregister_netdev(netdev);
2627 		enic_dev_deinit(enic);
2628 		vnic_dev_close(enic->vdev);
2629 #ifdef CONFIG_PCI_IOV
2630 		if (enic_sriov_enabled(enic)) {
2631 			pci_disable_sriov(pdev);
2632 			enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2633 		}
2634 #endif
2635 		kfree(enic->pp);
2636 		vnic_dev_unregister(enic->vdev);
2637 		enic_iounmap(enic);
2638 		pci_release_regions(pdev);
2639 		pci_disable_device(pdev);
2640 		free_netdev(netdev);
2641 	}
2642 }
2643 
2644 static struct pci_driver enic_driver = {
2645 	.name = DRV_NAME,
2646 	.id_table = enic_id_table,
2647 	.probe = enic_probe,
2648 	.remove = enic_remove,
2649 };
2650 
2651 static int __init enic_init_module(void)
2652 {
2653 	pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2654 
2655 	return pci_register_driver(&enic_driver);
2656 }
2657 
2658 static void __exit enic_cleanup_module(void)
2659 {
2660 	pci_unregister_driver(&enic_driver);
2661 }
2662 
2663 module_init(enic_init_module);
2664 module_exit(enic_cleanup_module);
2665