1 /*******************************************************************************
2 
3   Intel 82599 Virtual Function driver
4   Copyright(c) 1999 - 2015 Intel Corporation.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9 
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14 
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, see <http://www.gnu.org/licenses/>.
17 
18   The full GNU General Public License is included in this distribution in
19   the file called "COPYING".
20 
21   Contact Information:
22   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 
25 *******************************************************************************/
26 
27 /******************************************************************************
28  Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
29 ******************************************************************************/
30 
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 
33 #include <linux/types.h>
34 #include <linux/bitops.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/netdevice.h>
38 #include <linux/vmalloc.h>
39 #include <linux/string.h>
40 #include <linux/in.h>
41 #include <linux/ip.h>
42 #include <linux/tcp.h>
43 #include <linux/sctp.h>
44 #include <linux/ipv6.h>
45 #include <linux/slab.h>
46 #include <net/checksum.h>
47 #include <net/ip6_checksum.h>
48 #include <linux/ethtool.h>
49 #include <linux/if.h>
50 #include <linux/if_vlan.h>
51 #include <linux/prefetch.h>
52 #include <net/mpls.h>
53 
54 #include "ixgbevf.h"
55 
56 const char ixgbevf_driver_name[] = "ixgbevf";
57 static const char ixgbevf_driver_string[] =
58 	"Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
59 
60 #define DRV_VERSION "4.1.0-k"
61 const char ixgbevf_driver_version[] = DRV_VERSION;
62 static char ixgbevf_copyright[] =
63 	"Copyright (c) 2009 - 2015 Intel Corporation.";
64 
65 static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
66 	[board_82599_vf]	= &ixgbevf_82599_vf_info,
67 	[board_82599_vf_hv]	= &ixgbevf_82599_vf_hv_info,
68 	[board_X540_vf]		= &ixgbevf_X540_vf_info,
69 	[board_X540_vf_hv]	= &ixgbevf_X540_vf_hv_info,
70 	[board_X550_vf]		= &ixgbevf_X550_vf_info,
71 	[board_X550_vf_hv]	= &ixgbevf_X550_vf_hv_info,
72 	[board_X550EM_x_vf]	= &ixgbevf_X550EM_x_vf_info,
73 	[board_X550EM_x_vf_hv]	= &ixgbevf_X550EM_x_vf_hv_info,
74 	[board_x550em_a_vf]	= &ixgbevf_x550em_a_vf_info,
75 };
76 
77 /* ixgbevf_pci_tbl - PCI Device ID Table
78  *
79  * Wildcard entries (PCI_ANY_ID) should come last
80  * Last entry must be all 0s
81  *
82  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
83  *   Class, Class Mask, private data (not used) }
84  */
85 static const struct pci_device_id ixgbevf_pci_tbl[] = {
86 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF), board_82599_vf },
87 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF_HV), board_82599_vf_hv },
88 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF), board_X540_vf },
89 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF_HV), board_X540_vf_hv },
90 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF), board_X550_vf },
91 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550_VF_HV), board_X550_vf_hv },
92 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF), board_X550EM_x_vf },
93 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_X_VF_HV), board_X550EM_x_vf_hv},
94 	{PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X550EM_A_VF), board_x550em_a_vf },
95 	/* required last entry */
96 	{0, }
97 };
98 MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
99 
100 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
101 MODULE_DESCRIPTION("Intel(R) 10 Gigabit Virtual Function Network Driver");
102 MODULE_LICENSE("GPL");
103 MODULE_VERSION(DRV_VERSION);
104 
105 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
106 static int debug = -1;
107 module_param(debug, int, 0);
108 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
109 
110 static struct workqueue_struct *ixgbevf_wq;
111 
112 static void ixgbevf_service_event_schedule(struct ixgbevf_adapter *adapter)
113 {
114 	if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
115 	    !test_bit(__IXGBEVF_REMOVING, &adapter->state) &&
116 	    !test_and_set_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state))
117 		queue_work(ixgbevf_wq, &adapter->service_task);
118 }
119 
120 static void ixgbevf_service_event_complete(struct ixgbevf_adapter *adapter)
121 {
122 	BUG_ON(!test_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state));
123 
124 	/* flush memory to make sure state is correct before next watchdog */
125 	smp_mb__before_atomic();
126 	clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
127 }
128 
129 /* forward decls */
130 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter);
131 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
132 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter);
133 
134 static void ixgbevf_remove_adapter(struct ixgbe_hw *hw)
135 {
136 	struct ixgbevf_adapter *adapter = hw->back;
137 
138 	if (!hw->hw_addr)
139 		return;
140 	hw->hw_addr = NULL;
141 	dev_err(&adapter->pdev->dev, "Adapter removed\n");
142 	if (test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
143 		ixgbevf_service_event_schedule(adapter);
144 }
145 
146 static void ixgbevf_check_remove(struct ixgbe_hw *hw, u32 reg)
147 {
148 	u32 value;
149 
150 	/* The following check not only optimizes a bit by not
151 	 * performing a read on the status register when the
152 	 * register just read was a status register read that
153 	 * returned IXGBE_FAILED_READ_REG. It also blocks any
154 	 * potential recursion.
155 	 */
156 	if (reg == IXGBE_VFSTATUS) {
157 		ixgbevf_remove_adapter(hw);
158 		return;
159 	}
160 	value = ixgbevf_read_reg(hw, IXGBE_VFSTATUS);
161 	if (value == IXGBE_FAILED_READ_REG)
162 		ixgbevf_remove_adapter(hw);
163 }
164 
165 u32 ixgbevf_read_reg(struct ixgbe_hw *hw, u32 reg)
166 {
167 	u8 __iomem *reg_addr = READ_ONCE(hw->hw_addr);
168 	u32 value;
169 
170 	if (IXGBE_REMOVED(reg_addr))
171 		return IXGBE_FAILED_READ_REG;
172 	value = readl(reg_addr + reg);
173 	if (unlikely(value == IXGBE_FAILED_READ_REG))
174 		ixgbevf_check_remove(hw, reg);
175 	return value;
176 }
177 
178 /**
179  * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
180  * @adapter: pointer to adapter struct
181  * @direction: 0 for Rx, 1 for Tx, -1 for other causes
182  * @queue: queue to map the corresponding interrupt to
183  * @msix_vector: the vector to map to the corresponding queue
184  **/
185 static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
186 			     u8 queue, u8 msix_vector)
187 {
188 	u32 ivar, index;
189 	struct ixgbe_hw *hw = &adapter->hw;
190 
191 	if (direction == -1) {
192 		/* other causes */
193 		msix_vector |= IXGBE_IVAR_ALLOC_VAL;
194 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
195 		ivar &= ~0xFF;
196 		ivar |= msix_vector;
197 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
198 	} else {
199 		/* Tx or Rx causes */
200 		msix_vector |= IXGBE_IVAR_ALLOC_VAL;
201 		index = ((16 * (queue & 1)) + (8 * direction));
202 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
203 		ivar &= ~(0xFF << index);
204 		ivar |= (msix_vector << index);
205 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
206 	}
207 }
208 
209 static u64 ixgbevf_get_tx_completed(struct ixgbevf_ring *ring)
210 {
211 	return ring->stats.packets;
212 }
213 
214 static u32 ixgbevf_get_tx_pending(struct ixgbevf_ring *ring)
215 {
216 	struct ixgbevf_adapter *adapter = netdev_priv(ring->netdev);
217 	struct ixgbe_hw *hw = &adapter->hw;
218 
219 	u32 head = IXGBE_READ_REG(hw, IXGBE_VFTDH(ring->reg_idx));
220 	u32 tail = IXGBE_READ_REG(hw, IXGBE_VFTDT(ring->reg_idx));
221 
222 	if (head != tail)
223 		return (head < tail) ?
224 			tail - head : (tail + ring->count - head);
225 
226 	return 0;
227 }
228 
229 static inline bool ixgbevf_check_tx_hang(struct ixgbevf_ring *tx_ring)
230 {
231 	u32 tx_done = ixgbevf_get_tx_completed(tx_ring);
232 	u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
233 	u32 tx_pending = ixgbevf_get_tx_pending(tx_ring);
234 
235 	clear_check_for_tx_hang(tx_ring);
236 
237 	/* Check for a hung queue, but be thorough. This verifies
238 	 * that a transmit has been completed since the previous
239 	 * check AND there is at least one packet pending. The
240 	 * ARMED bit is set to indicate a potential hang.
241 	 */
242 	if ((tx_done_old == tx_done) && tx_pending) {
243 		/* make sure it is true for two checks in a row */
244 		return test_and_set_bit(__IXGBEVF_HANG_CHECK_ARMED,
245 					&tx_ring->state);
246 	}
247 	/* reset the countdown */
248 	clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &tx_ring->state);
249 
250 	/* update completed stats and continue */
251 	tx_ring->tx_stats.tx_done_old = tx_done;
252 
253 	return false;
254 }
255 
256 static void ixgbevf_tx_timeout_reset(struct ixgbevf_adapter *adapter)
257 {
258 	/* Do the reset outside of interrupt context */
259 	if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
260 		set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
261 		ixgbevf_service_event_schedule(adapter);
262 	}
263 }
264 
265 /**
266  * ixgbevf_tx_timeout - Respond to a Tx Hang
267  * @netdev: network interface device structure
268  **/
269 static void ixgbevf_tx_timeout(struct net_device *netdev)
270 {
271 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
272 
273 	ixgbevf_tx_timeout_reset(adapter);
274 }
275 
276 /**
277  * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
278  * @q_vector: board private structure
279  * @tx_ring: tx ring to clean
280  * @napi_budget: Used to determine if we are in netpoll
281  **/
282 static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
283 				 struct ixgbevf_ring *tx_ring, int napi_budget)
284 {
285 	struct ixgbevf_adapter *adapter = q_vector->adapter;
286 	struct ixgbevf_tx_buffer *tx_buffer;
287 	union ixgbe_adv_tx_desc *tx_desc;
288 	unsigned int total_bytes = 0, total_packets = 0;
289 	unsigned int budget = tx_ring->count / 2;
290 	unsigned int i = tx_ring->next_to_clean;
291 
292 	if (test_bit(__IXGBEVF_DOWN, &adapter->state))
293 		return true;
294 
295 	tx_buffer = &tx_ring->tx_buffer_info[i];
296 	tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
297 	i -= tx_ring->count;
298 
299 	do {
300 		union ixgbe_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
301 
302 		/* if next_to_watch is not set then there is no work pending */
303 		if (!eop_desc)
304 			break;
305 
306 		/* prevent any other reads prior to eop_desc */
307 		smp_rmb();
308 
309 		/* if DD is not set pending work has not been completed */
310 		if (!(eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)))
311 			break;
312 
313 		/* clear next_to_watch to prevent false hangs */
314 		tx_buffer->next_to_watch = NULL;
315 
316 		/* update the statistics for this packet */
317 		total_bytes += tx_buffer->bytecount;
318 		total_packets += tx_buffer->gso_segs;
319 
320 		/* free the skb */
321 		napi_consume_skb(tx_buffer->skb, napi_budget);
322 
323 		/* unmap skb header data */
324 		dma_unmap_single(tx_ring->dev,
325 				 dma_unmap_addr(tx_buffer, dma),
326 				 dma_unmap_len(tx_buffer, len),
327 				 DMA_TO_DEVICE);
328 
329 		/* clear tx_buffer data */
330 		dma_unmap_len_set(tx_buffer, len, 0);
331 
332 		/* unmap remaining buffers */
333 		while (tx_desc != eop_desc) {
334 			tx_buffer++;
335 			tx_desc++;
336 			i++;
337 			if (unlikely(!i)) {
338 				i -= tx_ring->count;
339 				tx_buffer = tx_ring->tx_buffer_info;
340 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
341 			}
342 
343 			/* unmap any remaining paged data */
344 			if (dma_unmap_len(tx_buffer, len)) {
345 				dma_unmap_page(tx_ring->dev,
346 					       dma_unmap_addr(tx_buffer, dma),
347 					       dma_unmap_len(tx_buffer, len),
348 					       DMA_TO_DEVICE);
349 				dma_unmap_len_set(tx_buffer, len, 0);
350 			}
351 		}
352 
353 		/* move us one more past the eop_desc for start of next pkt */
354 		tx_buffer++;
355 		tx_desc++;
356 		i++;
357 		if (unlikely(!i)) {
358 			i -= tx_ring->count;
359 			tx_buffer = tx_ring->tx_buffer_info;
360 			tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
361 		}
362 
363 		/* issue prefetch for next Tx descriptor */
364 		prefetch(tx_desc);
365 
366 		/* update budget accounting */
367 		budget--;
368 	} while (likely(budget));
369 
370 	i += tx_ring->count;
371 	tx_ring->next_to_clean = i;
372 	u64_stats_update_begin(&tx_ring->syncp);
373 	tx_ring->stats.bytes += total_bytes;
374 	tx_ring->stats.packets += total_packets;
375 	u64_stats_update_end(&tx_ring->syncp);
376 	q_vector->tx.total_bytes += total_bytes;
377 	q_vector->tx.total_packets += total_packets;
378 
379 	if (check_for_tx_hang(tx_ring) && ixgbevf_check_tx_hang(tx_ring)) {
380 		struct ixgbe_hw *hw = &adapter->hw;
381 		union ixgbe_adv_tx_desc *eop_desc;
382 
383 		eop_desc = tx_ring->tx_buffer_info[i].next_to_watch;
384 
385 		pr_err("Detected Tx Unit Hang\n"
386 		       "  Tx Queue             <%d>\n"
387 		       "  TDH, TDT             <%x>, <%x>\n"
388 		       "  next_to_use          <%x>\n"
389 		       "  next_to_clean        <%x>\n"
390 		       "tx_buffer_info[next_to_clean]\n"
391 		       "  next_to_watch        <%p>\n"
392 		       "  eop_desc->wb.status  <%x>\n"
393 		       "  time_stamp           <%lx>\n"
394 		       "  jiffies              <%lx>\n",
395 		       tx_ring->queue_index,
396 		       IXGBE_READ_REG(hw, IXGBE_VFTDH(tx_ring->reg_idx)),
397 		       IXGBE_READ_REG(hw, IXGBE_VFTDT(tx_ring->reg_idx)),
398 		       tx_ring->next_to_use, i,
399 		       eop_desc, (eop_desc ? eop_desc->wb.status : 0),
400 		       tx_ring->tx_buffer_info[i].time_stamp, jiffies);
401 
402 		netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
403 
404 		/* schedule immediate reset if we believe we hung */
405 		ixgbevf_tx_timeout_reset(adapter);
406 
407 		return true;
408 	}
409 
410 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
411 	if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
412 		     (ixgbevf_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
413 		/* Make sure that anybody stopping the queue after this
414 		 * sees the new next_to_clean.
415 		 */
416 		smp_mb();
417 
418 		if (__netif_subqueue_stopped(tx_ring->netdev,
419 					     tx_ring->queue_index) &&
420 		    !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
421 			netif_wake_subqueue(tx_ring->netdev,
422 					    tx_ring->queue_index);
423 			++tx_ring->tx_stats.restart_queue;
424 		}
425 	}
426 
427 	return !!budget;
428 }
429 
430 /**
431  * ixgbevf_rx_skb - Helper function to determine proper Rx method
432  * @q_vector: structure containing interrupt and ring information
433  * @skb: packet to send up
434  **/
435 static void ixgbevf_rx_skb(struct ixgbevf_q_vector *q_vector,
436 			   struct sk_buff *skb)
437 {
438 	napi_gro_receive(&q_vector->napi, skb);
439 }
440 
441 #define IXGBE_RSS_L4_TYPES_MASK \
442 	((1ul << IXGBE_RXDADV_RSSTYPE_IPV4_TCP) | \
443 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV4_UDP) | \
444 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_TCP) | \
445 	 (1ul << IXGBE_RXDADV_RSSTYPE_IPV6_UDP))
446 
447 static inline void ixgbevf_rx_hash(struct ixgbevf_ring *ring,
448 				   union ixgbe_adv_rx_desc *rx_desc,
449 				   struct sk_buff *skb)
450 {
451 	u16 rss_type;
452 
453 	if (!(ring->netdev->features & NETIF_F_RXHASH))
454 		return;
455 
456 	rss_type = le16_to_cpu(rx_desc->wb.lower.lo_dword.hs_rss.pkt_info) &
457 		   IXGBE_RXDADV_RSSTYPE_MASK;
458 
459 	if (!rss_type)
460 		return;
461 
462 	skb_set_hash(skb, le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
463 		     (IXGBE_RSS_L4_TYPES_MASK & (1ul << rss_type)) ?
464 		     PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
465 }
466 
467 /**
468  * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
469  * @ring: structure containig ring specific data
470  * @rx_desc: current Rx descriptor being processed
471  * @skb: skb currently being received and modified
472  **/
473 static inline void ixgbevf_rx_checksum(struct ixgbevf_ring *ring,
474 				       union ixgbe_adv_rx_desc *rx_desc,
475 				       struct sk_buff *skb)
476 {
477 	skb_checksum_none_assert(skb);
478 
479 	/* Rx csum disabled */
480 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
481 		return;
482 
483 	/* if IP and error */
484 	if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_IPCS) &&
485 	    ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_IPE)) {
486 		ring->rx_stats.csum_err++;
487 		return;
488 	}
489 
490 	if (!ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_L4CS))
491 		return;
492 
493 	if (ixgbevf_test_staterr(rx_desc, IXGBE_RXDADV_ERR_TCPE)) {
494 		ring->rx_stats.csum_err++;
495 		return;
496 	}
497 
498 	/* It must be a TCP or UDP packet with a valid checksum */
499 	skb->ip_summed = CHECKSUM_UNNECESSARY;
500 }
501 
502 /**
503  * ixgbevf_process_skb_fields - Populate skb header fields from Rx descriptor
504  * @rx_ring: rx descriptor ring packet is being transacted on
505  * @rx_desc: pointer to the EOP Rx descriptor
506  * @skb: pointer to current skb being populated
507  *
508  * This function checks the ring, descriptor, and packet information in
509  * order to populate the checksum, VLAN, protocol, and other fields within
510  * the skb.
511  **/
512 static void ixgbevf_process_skb_fields(struct ixgbevf_ring *rx_ring,
513 				       union ixgbe_adv_rx_desc *rx_desc,
514 				       struct sk_buff *skb)
515 {
516 	ixgbevf_rx_hash(rx_ring, rx_desc, skb);
517 	ixgbevf_rx_checksum(rx_ring, rx_desc, skb);
518 
519 	if (ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_VP)) {
520 		u16 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
521 		unsigned long *active_vlans = netdev_priv(rx_ring->netdev);
522 
523 		if (test_bit(vid & VLAN_VID_MASK, active_vlans))
524 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
525 	}
526 
527 	skb->protocol = eth_type_trans(skb, rx_ring->netdev);
528 }
529 
530 /**
531  * ixgbevf_is_non_eop - process handling of non-EOP buffers
532  * @rx_ring: Rx ring being processed
533  * @rx_desc: Rx descriptor for current buffer
534  *
535  * This function updates next to clean.  If the buffer is an EOP buffer
536  * this function exits returning false, otherwise it will place the
537  * sk_buff in the next buffer to be chained and return true indicating
538  * that this is in fact a non-EOP buffer.
539  **/
540 static bool ixgbevf_is_non_eop(struct ixgbevf_ring *rx_ring,
541 			       union ixgbe_adv_rx_desc *rx_desc)
542 {
543 	u32 ntc = rx_ring->next_to_clean + 1;
544 
545 	/* fetch, update, and store next to clean */
546 	ntc = (ntc < rx_ring->count) ? ntc : 0;
547 	rx_ring->next_to_clean = ntc;
548 
549 	prefetch(IXGBEVF_RX_DESC(rx_ring, ntc));
550 
551 	if (likely(ixgbevf_test_staterr(rx_desc, IXGBE_RXD_STAT_EOP)))
552 		return false;
553 
554 	return true;
555 }
556 
557 static bool ixgbevf_alloc_mapped_page(struct ixgbevf_ring *rx_ring,
558 				      struct ixgbevf_rx_buffer *bi)
559 {
560 	struct page *page = bi->page;
561 	dma_addr_t dma = bi->dma;
562 
563 	/* since we are recycling buffers we should seldom need to alloc */
564 	if (likely(page))
565 		return true;
566 
567 	/* alloc new page for storage */
568 	page = dev_alloc_page();
569 	if (unlikely(!page)) {
570 		rx_ring->rx_stats.alloc_rx_page_failed++;
571 		return false;
572 	}
573 
574 	/* map page for use */
575 	dma = dma_map_page_attrs(rx_ring->dev, page, 0, PAGE_SIZE,
576 				 DMA_FROM_DEVICE, IXGBEVF_RX_DMA_ATTR);
577 
578 	/* if mapping failed free memory back to system since
579 	 * there isn't much point in holding memory we can't use
580 	 */
581 	if (dma_mapping_error(rx_ring->dev, dma)) {
582 		__free_page(page);
583 
584 		rx_ring->rx_stats.alloc_rx_page_failed++;
585 		return false;
586 	}
587 
588 	bi->dma = dma;
589 	bi->page = page;
590 	bi->page_offset = 0;
591 	bi->pagecnt_bias = 1;
592 	rx_ring->rx_stats.alloc_rx_page++;
593 
594 	return true;
595 }
596 
597 /**
598  * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
599  * @rx_ring: rx descriptor ring (for a specific queue) to setup buffers on
600  * @cleaned_count: number of buffers to replace
601  **/
602 static void ixgbevf_alloc_rx_buffers(struct ixgbevf_ring *rx_ring,
603 				     u16 cleaned_count)
604 {
605 	union ixgbe_adv_rx_desc *rx_desc;
606 	struct ixgbevf_rx_buffer *bi;
607 	unsigned int i = rx_ring->next_to_use;
608 
609 	/* nothing to do or no valid netdev defined */
610 	if (!cleaned_count || !rx_ring->netdev)
611 		return;
612 
613 	rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
614 	bi = &rx_ring->rx_buffer_info[i];
615 	i -= rx_ring->count;
616 
617 	do {
618 		if (!ixgbevf_alloc_mapped_page(rx_ring, bi))
619 			break;
620 
621 		/* sync the buffer for use by the device */
622 		dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
623 						 bi->page_offset,
624 						 IXGBEVF_RX_BUFSZ,
625 						 DMA_FROM_DEVICE);
626 
627 		/* Refresh the desc even if pkt_addr didn't change
628 		 * because each write-back erases this info.
629 		 */
630 		rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
631 
632 		rx_desc++;
633 		bi++;
634 		i++;
635 		if (unlikely(!i)) {
636 			rx_desc = IXGBEVF_RX_DESC(rx_ring, 0);
637 			bi = rx_ring->rx_buffer_info;
638 			i -= rx_ring->count;
639 		}
640 
641 		/* clear the length for the next_to_use descriptor */
642 		rx_desc->wb.upper.length = 0;
643 
644 		cleaned_count--;
645 	} while (cleaned_count);
646 
647 	i += rx_ring->count;
648 
649 	if (rx_ring->next_to_use != i) {
650 		/* record the next descriptor to use */
651 		rx_ring->next_to_use = i;
652 
653 		/* update next to alloc since we have filled the ring */
654 		rx_ring->next_to_alloc = i;
655 
656 		/* Force memory writes to complete before letting h/w
657 		 * know there are new descriptors to fetch.  (Only
658 		 * applicable for weak-ordered memory model archs,
659 		 * such as IA-64).
660 		 */
661 		wmb();
662 		ixgbevf_write_tail(rx_ring, i);
663 	}
664 }
665 
666 /**
667  * ixgbevf_cleanup_headers - Correct corrupted or empty headers
668  * @rx_ring: rx descriptor ring packet is being transacted on
669  * @rx_desc: pointer to the EOP Rx descriptor
670  * @skb: pointer to current skb being fixed
671  *
672  * Check for corrupted packet headers caused by senders on the local L2
673  * embedded NIC switch not setting up their Tx Descriptors right.  These
674  * should be very rare.
675  *
676  * Also address the case where we are pulling data in on pages only
677  * and as such no data is present in the skb header.
678  *
679  * In addition if skb is not at least 60 bytes we need to pad it so that
680  * it is large enough to qualify as a valid Ethernet frame.
681  *
682  * Returns true if an error was encountered and skb was freed.
683  **/
684 static bool ixgbevf_cleanup_headers(struct ixgbevf_ring *rx_ring,
685 				    union ixgbe_adv_rx_desc *rx_desc,
686 				    struct sk_buff *skb)
687 {
688 	/* verify that the packet does not have any known errors */
689 	if (unlikely(ixgbevf_test_staterr(rx_desc,
690 					  IXGBE_RXDADV_ERR_FRAME_ERR_MASK))) {
691 		struct net_device *netdev = rx_ring->netdev;
692 
693 		if (!(netdev->features & NETIF_F_RXALL)) {
694 			dev_kfree_skb_any(skb);
695 			return true;
696 		}
697 	}
698 
699 	/* if eth_skb_pad returns an error the skb was freed */
700 	if (eth_skb_pad(skb))
701 		return true;
702 
703 	return false;
704 }
705 
706 /**
707  * ixgbevf_reuse_rx_page - page flip buffer and store it back on the ring
708  * @rx_ring: rx descriptor ring to store buffers on
709  * @old_buff: donor buffer to have page reused
710  *
711  * Synchronizes page for reuse by the adapter
712  **/
713 static void ixgbevf_reuse_rx_page(struct ixgbevf_ring *rx_ring,
714 				  struct ixgbevf_rx_buffer *old_buff)
715 {
716 	struct ixgbevf_rx_buffer *new_buff;
717 	u16 nta = rx_ring->next_to_alloc;
718 
719 	new_buff = &rx_ring->rx_buffer_info[nta];
720 
721 	/* update, and store next to alloc */
722 	nta++;
723 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
724 
725 	/* transfer page from old buffer to new buffer */
726 	new_buff->page = old_buff->page;
727 	new_buff->dma = old_buff->dma;
728 	new_buff->page_offset = old_buff->page_offset;
729 	new_buff->pagecnt_bias = old_buff->pagecnt_bias;
730 }
731 
732 static inline bool ixgbevf_page_is_reserved(struct page *page)
733 {
734 	return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
735 }
736 
737 static bool ixgbevf_can_reuse_rx_page(struct ixgbevf_rx_buffer *rx_buffer,
738 				      struct page *page,
739 				      const unsigned int truesize)
740 {
741 	unsigned int pagecnt_bias = rx_buffer->pagecnt_bias--;
742 
743 	/* avoid re-using remote pages */
744 	if (unlikely(ixgbevf_page_is_reserved(page)))
745 		return false;
746 
747 #if (PAGE_SIZE < 8192)
748 	/* if we are only owner of page we can reuse it */
749 	if (unlikely(page_ref_count(page) != pagecnt_bias))
750 		return false;
751 
752 	/* flip page offset to other buffer */
753 	rx_buffer->page_offset ^= IXGBEVF_RX_BUFSZ;
754 
755 #else
756 	/* move offset up to the next cache line */
757 	rx_buffer->page_offset += truesize;
758 
759 	if (rx_buffer->page_offset > (PAGE_SIZE - IXGBEVF_RX_BUFSZ))
760 		return false;
761 
762 #endif
763 
764 	/* If we have drained the page fragment pool we need to update
765 	 * the pagecnt_bias and page count so that we fully restock the
766 	 * number of references the driver holds.
767 	 */
768 	if (unlikely(pagecnt_bias == 1)) {
769 		page_ref_add(page, USHRT_MAX);
770 		rx_buffer->pagecnt_bias = USHRT_MAX;
771 	}
772 
773 	return true;
774 }
775 
776 /**
777  * ixgbevf_add_rx_frag - Add contents of Rx buffer to sk_buff
778  * @rx_ring: rx descriptor ring to transact packets on
779  * @rx_buffer: buffer containing page to add
780  * @rx_desc: descriptor containing length of buffer written by hardware
781  * @skb: sk_buff to place the data into
782  *
783  * This function will add the data contained in rx_buffer->page to the skb.
784  * This is done either through a direct copy if the data in the buffer is
785  * less than the skb header size, otherwise it will just attach the page as
786  * a frag to the skb.
787  *
788  * The function will then update the page offset if necessary and return
789  * true if the buffer can be reused by the adapter.
790  **/
791 static bool ixgbevf_add_rx_frag(struct ixgbevf_ring *rx_ring,
792 				struct ixgbevf_rx_buffer *rx_buffer,
793 				u16 size,
794 				union ixgbe_adv_rx_desc *rx_desc,
795 				struct sk_buff *skb)
796 {
797 	struct page *page = rx_buffer->page;
798 	unsigned char *va = page_address(page) + rx_buffer->page_offset;
799 #if (PAGE_SIZE < 8192)
800 	unsigned int truesize = IXGBEVF_RX_BUFSZ;
801 #else
802 	unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
803 #endif
804 	unsigned int pull_len;
805 
806 	if (unlikely(skb_is_nonlinear(skb)))
807 		goto add_tail_frag;
808 
809 	if (likely(size <= IXGBEVF_RX_HDR_SIZE)) {
810 		memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
811 
812 		/* page is not reserved, we can reuse buffer as is */
813 		if (likely(!ixgbevf_page_is_reserved(page)))
814 			return true;
815 
816 		/* this page cannot be reused so discard it */
817 		return false;
818 	}
819 
820 	/* we need the header to contain the greater of either ETH_HLEN or
821 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
822 	 */
823 	pull_len = eth_get_headlen(va, IXGBEVF_RX_HDR_SIZE);
824 
825 	/* align pull length to size of long to optimize memcpy performance */
826 	memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
827 
828 	/* update all of the pointers */
829 	va += pull_len;
830 	size -= pull_len;
831 
832 add_tail_frag:
833 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
834 			(unsigned long)va & ~PAGE_MASK, size, truesize);
835 
836 	return ixgbevf_can_reuse_rx_page(rx_buffer, page, truesize);
837 }
838 
839 static struct sk_buff *ixgbevf_fetch_rx_buffer(struct ixgbevf_ring *rx_ring,
840 					       union ixgbe_adv_rx_desc *rx_desc,
841 					       struct sk_buff *skb)
842 {
843 	struct ixgbevf_rx_buffer *rx_buffer;
844 	struct page *page;
845 	u16 size = le16_to_cpu(rx_desc->wb.upper.length);
846 
847 	rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
848 	page = rx_buffer->page;
849 	prefetchw(page);
850 
851 	/* we are reusing so sync this buffer for CPU use */
852 	dma_sync_single_range_for_cpu(rx_ring->dev,
853 				      rx_buffer->dma,
854 				      rx_buffer->page_offset,
855 				      size,
856 				      DMA_FROM_DEVICE);
857 
858 	if (likely(!skb)) {
859 		void *page_addr = page_address(page) +
860 				  rx_buffer->page_offset;
861 
862 		/* prefetch first cache line of first page */
863 		prefetch(page_addr);
864 #if L1_CACHE_BYTES < 128
865 		prefetch(page_addr + L1_CACHE_BYTES);
866 #endif
867 
868 		/* allocate a skb to store the frags */
869 		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
870 						IXGBEVF_RX_HDR_SIZE);
871 		if (unlikely(!skb)) {
872 			rx_ring->rx_stats.alloc_rx_buff_failed++;
873 			return NULL;
874 		}
875 
876 		/* we will be copying header into skb->data in
877 		 * pskb_may_pull so it is in our interest to prefetch
878 		 * it now to avoid a possible cache miss
879 		 */
880 		prefetchw(skb->data);
881 	}
882 
883 	/* pull page into skb */
884 	if (ixgbevf_add_rx_frag(rx_ring, rx_buffer, size, rx_desc, skb)) {
885 		/* hand second half of page back to the ring */
886 		ixgbevf_reuse_rx_page(rx_ring, rx_buffer);
887 	} else {
888 		/* We are not reusing the buffer so unmap it and free
889 		 * any references we are holding to it
890 		 */
891 		dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
892 				     PAGE_SIZE, DMA_FROM_DEVICE,
893 				     IXGBEVF_RX_DMA_ATTR);
894 		__page_frag_cache_drain(page, rx_buffer->pagecnt_bias);
895 	}
896 
897 	/* clear contents of buffer_info */
898 	rx_buffer->dma = 0;
899 	rx_buffer->page = NULL;
900 
901 	return skb;
902 }
903 
904 static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
905 					     u32 qmask)
906 {
907 	struct ixgbe_hw *hw = &adapter->hw;
908 
909 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
910 }
911 
912 static int ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
913 				struct ixgbevf_ring *rx_ring,
914 				int budget)
915 {
916 	unsigned int total_rx_bytes = 0, total_rx_packets = 0;
917 	u16 cleaned_count = ixgbevf_desc_unused(rx_ring);
918 	struct sk_buff *skb = rx_ring->skb;
919 
920 	while (likely(total_rx_packets < budget)) {
921 		union ixgbe_adv_rx_desc *rx_desc;
922 
923 		/* return some buffers to hardware, one at a time is too slow */
924 		if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
925 			ixgbevf_alloc_rx_buffers(rx_ring, cleaned_count);
926 			cleaned_count = 0;
927 		}
928 
929 		rx_desc = IXGBEVF_RX_DESC(rx_ring, rx_ring->next_to_clean);
930 
931 		if (!rx_desc->wb.upper.length)
932 			break;
933 
934 		/* This memory barrier is needed to keep us from reading
935 		 * any other fields out of the rx_desc until we know the
936 		 * RXD_STAT_DD bit is set
937 		 */
938 		rmb();
939 
940 		/* retrieve a buffer from the ring */
941 		skb = ixgbevf_fetch_rx_buffer(rx_ring, rx_desc, skb);
942 
943 		/* exit if we failed to retrieve a buffer */
944 		if (!skb) {
945 			rx_ring->rx_stats.alloc_rx_buff_failed++;
946 			break;
947 		}
948 
949 		cleaned_count++;
950 
951 		/* fetch next buffer in frame if non-eop */
952 		if (ixgbevf_is_non_eop(rx_ring, rx_desc))
953 			continue;
954 
955 		/* verify the packet layout is correct */
956 		if (ixgbevf_cleanup_headers(rx_ring, rx_desc, skb)) {
957 			skb = NULL;
958 			continue;
959 		}
960 
961 		/* probably a little skewed due to removing CRC */
962 		total_rx_bytes += skb->len;
963 
964 		/* Workaround hardware that can't do proper VEPA multicast
965 		 * source pruning.
966 		 */
967 		if ((skb->pkt_type == PACKET_BROADCAST ||
968 		     skb->pkt_type == PACKET_MULTICAST) &&
969 		    ether_addr_equal(rx_ring->netdev->dev_addr,
970 				     eth_hdr(skb)->h_source)) {
971 			dev_kfree_skb_irq(skb);
972 			continue;
973 		}
974 
975 		/* populate checksum, VLAN, and protocol */
976 		ixgbevf_process_skb_fields(rx_ring, rx_desc, skb);
977 
978 		ixgbevf_rx_skb(q_vector, skb);
979 
980 		/* reset skb pointer */
981 		skb = NULL;
982 
983 		/* update budget accounting */
984 		total_rx_packets++;
985 	}
986 
987 	/* place incomplete frames back on ring for completion */
988 	rx_ring->skb = skb;
989 
990 	u64_stats_update_begin(&rx_ring->syncp);
991 	rx_ring->stats.packets += total_rx_packets;
992 	rx_ring->stats.bytes += total_rx_bytes;
993 	u64_stats_update_end(&rx_ring->syncp);
994 	q_vector->rx.total_packets += total_rx_packets;
995 	q_vector->rx.total_bytes += total_rx_bytes;
996 
997 	return total_rx_packets;
998 }
999 
1000 /**
1001  * ixgbevf_poll - NAPI polling calback
1002  * @napi: napi struct with our devices info in it
1003  * @budget: amount of work driver is allowed to do this pass, in packets
1004  *
1005  * This function will clean more than one or more rings associated with a
1006  * q_vector.
1007  **/
1008 static int ixgbevf_poll(struct napi_struct *napi, int budget)
1009 {
1010 	struct ixgbevf_q_vector *q_vector =
1011 		container_of(napi, struct ixgbevf_q_vector, napi);
1012 	struct ixgbevf_adapter *adapter = q_vector->adapter;
1013 	struct ixgbevf_ring *ring;
1014 	int per_ring_budget, work_done = 0;
1015 	bool clean_complete = true;
1016 
1017 	ixgbevf_for_each_ring(ring, q_vector->tx) {
1018 		if (!ixgbevf_clean_tx_irq(q_vector, ring, budget))
1019 			clean_complete = false;
1020 	}
1021 
1022 	if (budget <= 0)
1023 		return budget;
1024 
1025 	/* attempt to distribute budget to each queue fairly, but don't allow
1026 	 * the budget to go below 1 because we'll exit polling
1027 	 */
1028 	if (q_vector->rx.count > 1)
1029 		per_ring_budget = max(budget/q_vector->rx.count, 1);
1030 	else
1031 		per_ring_budget = budget;
1032 
1033 	ixgbevf_for_each_ring(ring, q_vector->rx) {
1034 		int cleaned = ixgbevf_clean_rx_irq(q_vector, ring,
1035 						   per_ring_budget);
1036 		work_done += cleaned;
1037 		if (cleaned >= per_ring_budget)
1038 			clean_complete = false;
1039 	}
1040 
1041 	/* If all work not completed, return budget and keep polling */
1042 	if (!clean_complete)
1043 		return budget;
1044 	/* all work done, exit the polling mode */
1045 	napi_complete_done(napi, work_done);
1046 	if (adapter->rx_itr_setting == 1)
1047 		ixgbevf_set_itr(q_vector);
1048 	if (!test_bit(__IXGBEVF_DOWN, &adapter->state) &&
1049 	    !test_bit(__IXGBEVF_REMOVING, &adapter->state))
1050 		ixgbevf_irq_enable_queues(adapter,
1051 					  BIT(q_vector->v_idx));
1052 
1053 	return 0;
1054 }
1055 
1056 /**
1057  * ixgbevf_write_eitr - write VTEITR register in hardware specific way
1058  * @q_vector: structure containing interrupt and ring information
1059  **/
1060 void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
1061 {
1062 	struct ixgbevf_adapter *adapter = q_vector->adapter;
1063 	struct ixgbe_hw *hw = &adapter->hw;
1064 	int v_idx = q_vector->v_idx;
1065 	u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
1066 
1067 	/* set the WDIS bit to not clear the timer bits and cause an
1068 	 * immediate assertion of the interrupt
1069 	 */
1070 	itr_reg |= IXGBE_EITR_CNT_WDIS;
1071 
1072 	IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
1073 }
1074 
1075 /**
1076  * ixgbevf_configure_msix - Configure MSI-X hardware
1077  * @adapter: board private structure
1078  *
1079  * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
1080  * interrupts.
1081  **/
1082 static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
1083 {
1084 	struct ixgbevf_q_vector *q_vector;
1085 	int q_vectors, v_idx;
1086 
1087 	q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1088 	adapter->eims_enable_mask = 0;
1089 
1090 	/* Populate the IVAR table and set the ITR values to the
1091 	 * corresponding register.
1092 	 */
1093 	for (v_idx = 0; v_idx < q_vectors; v_idx++) {
1094 		struct ixgbevf_ring *ring;
1095 
1096 		q_vector = adapter->q_vector[v_idx];
1097 
1098 		ixgbevf_for_each_ring(ring, q_vector->rx)
1099 			ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
1100 
1101 		ixgbevf_for_each_ring(ring, q_vector->tx)
1102 			ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
1103 
1104 		if (q_vector->tx.ring && !q_vector->rx.ring) {
1105 			/* Tx only vector */
1106 			if (adapter->tx_itr_setting == 1)
1107 				q_vector->itr = IXGBE_12K_ITR;
1108 			else
1109 				q_vector->itr = adapter->tx_itr_setting;
1110 		} else {
1111 			/* Rx or Rx/Tx vector */
1112 			if (adapter->rx_itr_setting == 1)
1113 				q_vector->itr = IXGBE_20K_ITR;
1114 			else
1115 				q_vector->itr = adapter->rx_itr_setting;
1116 		}
1117 
1118 		/* add q_vector eims value to global eims_enable_mask */
1119 		adapter->eims_enable_mask |= BIT(v_idx);
1120 
1121 		ixgbevf_write_eitr(q_vector);
1122 	}
1123 
1124 	ixgbevf_set_ivar(adapter, -1, 1, v_idx);
1125 	/* setup eims_other and add value to global eims_enable_mask */
1126 	adapter->eims_other = BIT(v_idx);
1127 	adapter->eims_enable_mask |= adapter->eims_other;
1128 }
1129 
1130 enum latency_range {
1131 	lowest_latency = 0,
1132 	low_latency = 1,
1133 	bulk_latency = 2,
1134 	latency_invalid = 255
1135 };
1136 
1137 /**
1138  * ixgbevf_update_itr - update the dynamic ITR value based on statistics
1139  * @q_vector: structure containing interrupt and ring information
1140  * @ring_container: structure containing ring performance data
1141  *
1142  * Stores a new ITR value based on packets and byte
1143  * counts during the last interrupt.  The advantage of per interrupt
1144  * computation is faster updates and more accurate ITR for the current
1145  * traffic pattern.  Constants in this function were computed
1146  * based on theoretical maximum wire speed and thresholds were set based
1147  * on testing data as well as attempting to minimize response time
1148  * while increasing bulk throughput.
1149  **/
1150 static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
1151 			       struct ixgbevf_ring_container *ring_container)
1152 {
1153 	int bytes = ring_container->total_bytes;
1154 	int packets = ring_container->total_packets;
1155 	u32 timepassed_us;
1156 	u64 bytes_perint;
1157 	u8 itr_setting = ring_container->itr;
1158 
1159 	if (packets == 0)
1160 		return;
1161 
1162 	/* simple throttle rate management
1163 	 *    0-20MB/s lowest (100000 ints/s)
1164 	 *   20-100MB/s low   (20000 ints/s)
1165 	 *  100-1249MB/s bulk (12000 ints/s)
1166 	 */
1167 	/* what was last interrupt timeslice? */
1168 	timepassed_us = q_vector->itr >> 2;
1169 	bytes_perint = bytes / timepassed_us; /* bytes/usec */
1170 
1171 	switch (itr_setting) {
1172 	case lowest_latency:
1173 		if (bytes_perint > 10)
1174 			itr_setting = low_latency;
1175 		break;
1176 	case low_latency:
1177 		if (bytes_perint > 20)
1178 			itr_setting = bulk_latency;
1179 		else if (bytes_perint <= 10)
1180 			itr_setting = lowest_latency;
1181 		break;
1182 	case bulk_latency:
1183 		if (bytes_perint <= 20)
1184 			itr_setting = low_latency;
1185 		break;
1186 	}
1187 
1188 	/* clear work counters since we have the values we need */
1189 	ring_container->total_bytes = 0;
1190 	ring_container->total_packets = 0;
1191 
1192 	/* write updated itr to ring container */
1193 	ring_container->itr = itr_setting;
1194 }
1195 
1196 static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
1197 {
1198 	u32 new_itr = q_vector->itr;
1199 	u8 current_itr;
1200 
1201 	ixgbevf_update_itr(q_vector, &q_vector->tx);
1202 	ixgbevf_update_itr(q_vector, &q_vector->rx);
1203 
1204 	current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
1205 
1206 	switch (current_itr) {
1207 	/* counts and packets in update_itr are dependent on these numbers */
1208 	case lowest_latency:
1209 		new_itr = IXGBE_100K_ITR;
1210 		break;
1211 	case low_latency:
1212 		new_itr = IXGBE_20K_ITR;
1213 		break;
1214 	case bulk_latency:
1215 		new_itr = IXGBE_12K_ITR;
1216 		break;
1217 	default:
1218 		break;
1219 	}
1220 
1221 	if (new_itr != q_vector->itr) {
1222 		/* do an exponential smoothing */
1223 		new_itr = (10 * new_itr * q_vector->itr) /
1224 			  ((9 * new_itr) + q_vector->itr);
1225 
1226 		/* save the algorithm value here */
1227 		q_vector->itr = new_itr;
1228 
1229 		ixgbevf_write_eitr(q_vector);
1230 	}
1231 }
1232 
1233 static irqreturn_t ixgbevf_msix_other(int irq, void *data)
1234 {
1235 	struct ixgbevf_adapter *adapter = data;
1236 	struct ixgbe_hw *hw = &adapter->hw;
1237 
1238 	hw->mac.get_link_status = 1;
1239 
1240 	ixgbevf_service_event_schedule(adapter);
1241 
1242 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
1243 
1244 	return IRQ_HANDLED;
1245 }
1246 
1247 /**
1248  * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
1249  * @irq: unused
1250  * @data: pointer to our q_vector struct for this interrupt vector
1251  **/
1252 static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
1253 {
1254 	struct ixgbevf_q_vector *q_vector = data;
1255 
1256 	/* EIAM disabled interrupts (on this vector) for us */
1257 	if (q_vector->rx.ring || q_vector->tx.ring)
1258 		napi_schedule_irqoff(&q_vector->napi);
1259 
1260 	return IRQ_HANDLED;
1261 }
1262 
1263 static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
1264 				     int r_idx)
1265 {
1266 	struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1267 
1268 	a->rx_ring[r_idx]->next = q_vector->rx.ring;
1269 	q_vector->rx.ring = a->rx_ring[r_idx];
1270 	q_vector->rx.count++;
1271 }
1272 
1273 static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
1274 				     int t_idx)
1275 {
1276 	struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
1277 
1278 	a->tx_ring[t_idx]->next = q_vector->tx.ring;
1279 	q_vector->tx.ring = a->tx_ring[t_idx];
1280 	q_vector->tx.count++;
1281 }
1282 
1283 /**
1284  * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
1285  * @adapter: board private structure to initialize
1286  *
1287  * This function maps descriptor rings to the queue-specific vectors
1288  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
1289  * one vector per ring/queue, but on a constrained vector budget, we
1290  * group the rings as "efficiently" as possible.  You would add new
1291  * mapping configurations in here.
1292  **/
1293 static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
1294 {
1295 	int q_vectors;
1296 	int v_start = 0;
1297 	int rxr_idx = 0, txr_idx = 0;
1298 	int rxr_remaining = adapter->num_rx_queues;
1299 	int txr_remaining = adapter->num_tx_queues;
1300 	int i, j;
1301 	int rqpv, tqpv;
1302 
1303 	q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1304 
1305 	/* The ideal configuration...
1306 	 * We have enough vectors to map one per queue.
1307 	 */
1308 	if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
1309 		for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
1310 			map_vector_to_rxq(adapter, v_start, rxr_idx);
1311 
1312 		for (; txr_idx < txr_remaining; v_start++, txr_idx++)
1313 			map_vector_to_txq(adapter, v_start, txr_idx);
1314 		return 0;
1315 	}
1316 
1317 	/* If we don't have enough vectors for a 1-to-1
1318 	 * mapping, we'll have to group them so there are
1319 	 * multiple queues per vector.
1320 	 */
1321 	/* Re-adjusting *qpv takes care of the remainder. */
1322 	for (i = v_start; i < q_vectors; i++) {
1323 		rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
1324 		for (j = 0; j < rqpv; j++) {
1325 			map_vector_to_rxq(adapter, i, rxr_idx);
1326 			rxr_idx++;
1327 			rxr_remaining--;
1328 		}
1329 	}
1330 	for (i = v_start; i < q_vectors; i++) {
1331 		tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
1332 		for (j = 0; j < tqpv; j++) {
1333 			map_vector_to_txq(adapter, i, txr_idx);
1334 			txr_idx++;
1335 			txr_remaining--;
1336 		}
1337 	}
1338 
1339 	return 0;
1340 }
1341 
1342 /**
1343  * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
1344  * @adapter: board private structure
1345  *
1346  * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
1347  * interrupts from the kernel.
1348  **/
1349 static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
1350 {
1351 	struct net_device *netdev = adapter->netdev;
1352 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1353 	unsigned int ri = 0, ti = 0;
1354 	int vector, err;
1355 
1356 	for (vector = 0; vector < q_vectors; vector++) {
1357 		struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
1358 		struct msix_entry *entry = &adapter->msix_entries[vector];
1359 
1360 		if (q_vector->tx.ring && q_vector->rx.ring) {
1361 			snprintf(q_vector->name, sizeof(q_vector->name),
1362 				 "%s-TxRx-%u", netdev->name, ri++);
1363 			ti++;
1364 		} else if (q_vector->rx.ring) {
1365 			snprintf(q_vector->name, sizeof(q_vector->name),
1366 				 "%s-rx-%u", netdev->name, ri++);
1367 		} else if (q_vector->tx.ring) {
1368 			snprintf(q_vector->name, sizeof(q_vector->name),
1369 				 "%s-tx-%u", netdev->name, ti++);
1370 		} else {
1371 			/* skip this unused q_vector */
1372 			continue;
1373 		}
1374 		err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
1375 				  q_vector->name, q_vector);
1376 		if (err) {
1377 			hw_dbg(&adapter->hw,
1378 			       "request_irq failed for MSIX interrupt Error: %d\n",
1379 			       err);
1380 			goto free_queue_irqs;
1381 		}
1382 	}
1383 
1384 	err = request_irq(adapter->msix_entries[vector].vector,
1385 			  &ixgbevf_msix_other, 0, netdev->name, adapter);
1386 	if (err) {
1387 		hw_dbg(&adapter->hw, "request_irq for msix_other failed: %d\n",
1388 		       err);
1389 		goto free_queue_irqs;
1390 	}
1391 
1392 	return 0;
1393 
1394 free_queue_irqs:
1395 	while (vector) {
1396 		vector--;
1397 		free_irq(adapter->msix_entries[vector].vector,
1398 			 adapter->q_vector[vector]);
1399 	}
1400 	/* This failure is non-recoverable - it indicates the system is
1401 	 * out of MSIX vector resources and the VF driver cannot run
1402 	 * without them.  Set the number of msix vectors to zero
1403 	 * indicating that not enough can be allocated.  The error
1404 	 * will be returned to the user indicating device open failed.
1405 	 * Any further attempts to force the driver to open will also
1406 	 * fail.  The only way to recover is to unload the driver and
1407 	 * reload it again.  If the system has recovered some MSIX
1408 	 * vectors then it may succeed.
1409 	 */
1410 	adapter->num_msix_vectors = 0;
1411 	return err;
1412 }
1413 
1414 static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
1415 {
1416 	int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1417 
1418 	for (i = 0; i < q_vectors; i++) {
1419 		struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
1420 
1421 		q_vector->rx.ring = NULL;
1422 		q_vector->tx.ring = NULL;
1423 		q_vector->rx.count = 0;
1424 		q_vector->tx.count = 0;
1425 	}
1426 }
1427 
1428 /**
1429  * ixgbevf_request_irq - initialize interrupts
1430  * @adapter: board private structure
1431  *
1432  * Attempts to configure interrupts using the best available
1433  * capabilities of the hardware and kernel.
1434  **/
1435 static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
1436 {
1437 	int err = ixgbevf_request_msix_irqs(adapter);
1438 
1439 	if (err)
1440 		hw_dbg(&adapter->hw, "request_irq failed, Error %d\n", err);
1441 
1442 	return err;
1443 }
1444 
1445 static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
1446 {
1447 	int i, q_vectors;
1448 
1449 	if (!adapter->msix_entries)
1450 		return;
1451 
1452 	q_vectors = adapter->num_msix_vectors;
1453 	i = q_vectors - 1;
1454 
1455 	free_irq(adapter->msix_entries[i].vector, adapter);
1456 	i--;
1457 
1458 	for (; i >= 0; i--) {
1459 		/* free only the irqs that were actually requested */
1460 		if (!adapter->q_vector[i]->rx.ring &&
1461 		    !adapter->q_vector[i]->tx.ring)
1462 			continue;
1463 
1464 		free_irq(adapter->msix_entries[i].vector,
1465 			 adapter->q_vector[i]);
1466 	}
1467 
1468 	ixgbevf_reset_q_vectors(adapter);
1469 }
1470 
1471 /**
1472  * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
1473  * @adapter: board private structure
1474  **/
1475 static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
1476 {
1477 	struct ixgbe_hw *hw = &adapter->hw;
1478 	int i;
1479 
1480 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
1481 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
1482 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
1483 
1484 	IXGBE_WRITE_FLUSH(hw);
1485 
1486 	for (i = 0; i < adapter->num_msix_vectors; i++)
1487 		synchronize_irq(adapter->msix_entries[i].vector);
1488 }
1489 
1490 /**
1491  * ixgbevf_irq_enable - Enable default interrupt generation settings
1492  * @adapter: board private structure
1493  **/
1494 static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
1495 {
1496 	struct ixgbe_hw *hw = &adapter->hw;
1497 
1498 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
1499 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
1500 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
1501 }
1502 
1503 /**
1504  * ixgbevf_configure_tx_ring - Configure 82599 VF Tx ring after Reset
1505  * @adapter: board private structure
1506  * @ring: structure containing ring specific data
1507  *
1508  * Configure the Tx descriptor ring after a reset.
1509  **/
1510 static void ixgbevf_configure_tx_ring(struct ixgbevf_adapter *adapter,
1511 				      struct ixgbevf_ring *ring)
1512 {
1513 	struct ixgbe_hw *hw = &adapter->hw;
1514 	u64 tdba = ring->dma;
1515 	int wait_loop = 10;
1516 	u32 txdctl = IXGBE_TXDCTL_ENABLE;
1517 	u8 reg_idx = ring->reg_idx;
1518 
1519 	/* disable queue to avoid issues while updating state */
1520 	IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), IXGBE_TXDCTL_SWFLSH);
1521 	IXGBE_WRITE_FLUSH(hw);
1522 
1523 	IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
1524 	IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(reg_idx), tdba >> 32);
1525 	IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(reg_idx),
1526 			ring->count * sizeof(union ixgbe_adv_tx_desc));
1527 
1528 	/* disable head writeback */
1529 	IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAH(reg_idx), 0);
1530 	IXGBE_WRITE_REG(hw, IXGBE_VFTDWBAL(reg_idx), 0);
1531 
1532 	/* enable relaxed ordering */
1533 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(reg_idx),
1534 			(IXGBE_DCA_TXCTRL_DESC_RRO_EN |
1535 			 IXGBE_DCA_TXCTRL_DATA_RRO_EN));
1536 
1537 	/* reset head and tail pointers */
1538 	IXGBE_WRITE_REG(hw, IXGBE_VFTDH(reg_idx), 0);
1539 	IXGBE_WRITE_REG(hw, IXGBE_VFTDT(reg_idx), 0);
1540 	ring->tail = adapter->io_addr + IXGBE_VFTDT(reg_idx);
1541 
1542 	/* reset ntu and ntc to place SW in sync with hardwdare */
1543 	ring->next_to_clean = 0;
1544 	ring->next_to_use = 0;
1545 
1546 	/* In order to avoid issues WTHRESH + PTHRESH should always be equal
1547 	 * to or less than the number of on chip descriptors, which is
1548 	 * currently 40.
1549 	 */
1550 	txdctl |= (8 << 16);    /* WTHRESH = 8 */
1551 
1552 	/* Setting PTHRESH to 32 both improves performance */
1553 	txdctl |= (1u << 8) |    /* HTHRESH = 1 */
1554 		   32;           /* PTHRESH = 32 */
1555 
1556 	/* reinitialize tx_buffer_info */
1557 	memset(ring->tx_buffer_info, 0,
1558 	       sizeof(struct ixgbevf_tx_buffer) * ring->count);
1559 
1560 	clear_bit(__IXGBEVF_HANG_CHECK_ARMED, &ring->state);
1561 
1562 	IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx), txdctl);
1563 
1564 	/* poll to verify queue is enabled */
1565 	do {
1566 		usleep_range(1000, 2000);
1567 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(reg_idx));
1568 	}  while (--wait_loop && !(txdctl & IXGBE_TXDCTL_ENABLE));
1569 	if (!wait_loop)
1570 		hw_dbg(hw, "Could not enable Tx Queue %d\n", reg_idx);
1571 }
1572 
1573 /**
1574  * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
1575  * @adapter: board private structure
1576  *
1577  * Configure the Tx unit of the MAC after a reset.
1578  **/
1579 static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
1580 {
1581 	u32 i;
1582 
1583 	/* Setup the HW Tx Head and Tail descriptor pointers */
1584 	for (i = 0; i < adapter->num_tx_queues; i++)
1585 		ixgbevf_configure_tx_ring(adapter, adapter->tx_ring[i]);
1586 }
1587 
1588 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT	2
1589 
1590 static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
1591 {
1592 	struct ixgbe_hw *hw = &adapter->hw;
1593 	u32 srrctl;
1594 
1595 	srrctl = IXGBE_SRRCTL_DROP_EN;
1596 
1597 	srrctl |= IXGBEVF_RX_HDR_SIZE << IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT;
1598 	srrctl |= IXGBEVF_RX_BUFSZ >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1599 	srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1600 
1601 	IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
1602 }
1603 
1604 static void ixgbevf_setup_psrtype(struct ixgbevf_adapter *adapter)
1605 {
1606 	struct ixgbe_hw *hw = &adapter->hw;
1607 
1608 	/* PSRTYPE must be initialized in 82599 */
1609 	u32 psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1610 		      IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1611 		      IXGBE_PSRTYPE_L2HDR;
1612 
1613 	if (adapter->num_rx_queues > 1)
1614 		psrtype |= BIT(29);
1615 
1616 	IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1617 }
1618 
1619 #define IXGBEVF_MAX_RX_DESC_POLL 10
1620 static void ixgbevf_disable_rx_queue(struct ixgbevf_adapter *adapter,
1621 				     struct ixgbevf_ring *ring)
1622 {
1623 	struct ixgbe_hw *hw = &adapter->hw;
1624 	int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1625 	u32 rxdctl;
1626 	u8 reg_idx = ring->reg_idx;
1627 
1628 	if (IXGBE_REMOVED(hw->hw_addr))
1629 		return;
1630 	rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1631 	rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1632 
1633 	/* write value back with RXDCTL.ENABLE bit cleared */
1634 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1635 
1636 	/* the hardware may take up to 100us to really disable the Rx queue */
1637 	do {
1638 		udelay(10);
1639 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1640 	} while (--wait_loop && (rxdctl & IXGBE_RXDCTL_ENABLE));
1641 
1642 	if (!wait_loop)
1643 		pr_err("RXDCTL.ENABLE queue %d not cleared while polling\n",
1644 		       reg_idx);
1645 }
1646 
1647 static void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
1648 					 struct ixgbevf_ring *ring)
1649 {
1650 	struct ixgbe_hw *hw = &adapter->hw;
1651 	int wait_loop = IXGBEVF_MAX_RX_DESC_POLL;
1652 	u32 rxdctl;
1653 	u8 reg_idx = ring->reg_idx;
1654 
1655 	if (IXGBE_REMOVED(hw->hw_addr))
1656 		return;
1657 	do {
1658 		usleep_range(1000, 2000);
1659 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1660 	} while (--wait_loop && !(rxdctl & IXGBE_RXDCTL_ENABLE));
1661 
1662 	if (!wait_loop)
1663 		pr_err("RXDCTL.ENABLE queue %d not set while polling\n",
1664 		       reg_idx);
1665 }
1666 
1667 /**
1668  * ixgbevf_init_rss_key - Initialize adapter RSS key
1669  * @adapter: device handle
1670  *
1671  * Allocates and initializes the RSS key if it is not allocated.
1672  **/
1673 static inline int ixgbevf_init_rss_key(struct ixgbevf_adapter *adapter)
1674 {
1675 	u32 *rss_key;
1676 
1677 	if (!adapter->rss_key) {
1678 		rss_key = kzalloc(IXGBEVF_RSS_HASH_KEY_SIZE, GFP_KERNEL);
1679 		if (unlikely(!rss_key))
1680 			return -ENOMEM;
1681 
1682 		netdev_rss_key_fill(rss_key, IXGBEVF_RSS_HASH_KEY_SIZE);
1683 		adapter->rss_key = rss_key;
1684 	}
1685 
1686 	return 0;
1687 }
1688 
1689 static void ixgbevf_setup_vfmrqc(struct ixgbevf_adapter *adapter)
1690 {
1691 	struct ixgbe_hw *hw = &adapter->hw;
1692 	u32 vfmrqc = 0, vfreta = 0;
1693 	u16 rss_i = adapter->num_rx_queues;
1694 	u8 i, j;
1695 
1696 	/* Fill out hash function seeds */
1697 	for (i = 0; i < IXGBEVF_VFRSSRK_REGS; i++)
1698 		IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), *(adapter->rss_key + i));
1699 
1700 	for (i = 0, j = 0; i < IXGBEVF_X550_VFRETA_SIZE; i++, j++) {
1701 		if (j == rss_i)
1702 			j = 0;
1703 
1704 		adapter->rss_indir_tbl[i] = j;
1705 
1706 		vfreta |= j << (i & 0x3) * 8;
1707 		if ((i & 3) == 3) {
1708 			IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), vfreta);
1709 			vfreta = 0;
1710 		}
1711 	}
1712 
1713 	/* Perform hash on these packet types */
1714 	vfmrqc |= IXGBE_VFMRQC_RSS_FIELD_IPV4 |
1715 		IXGBE_VFMRQC_RSS_FIELD_IPV4_TCP |
1716 		IXGBE_VFMRQC_RSS_FIELD_IPV6 |
1717 		IXGBE_VFMRQC_RSS_FIELD_IPV6_TCP;
1718 
1719 	vfmrqc |= IXGBE_VFMRQC_RSSEN;
1720 
1721 	IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, vfmrqc);
1722 }
1723 
1724 static void ixgbevf_configure_rx_ring(struct ixgbevf_adapter *adapter,
1725 				      struct ixgbevf_ring *ring)
1726 {
1727 	struct ixgbe_hw *hw = &adapter->hw;
1728 	union ixgbe_adv_rx_desc *rx_desc;
1729 	u64 rdba = ring->dma;
1730 	u32 rxdctl;
1731 	u8 reg_idx = ring->reg_idx;
1732 
1733 	/* disable queue to avoid issues while updating state */
1734 	rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(reg_idx));
1735 	ixgbevf_disable_rx_queue(adapter, ring);
1736 
1737 	IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1738 	IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(reg_idx), rdba >> 32);
1739 	IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(reg_idx),
1740 			ring->count * sizeof(union ixgbe_adv_rx_desc));
1741 
1742 #ifndef CONFIG_SPARC
1743 	/* enable relaxed ordering */
1744 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1745 			IXGBE_DCA_RXCTRL_DESC_RRO_EN);
1746 #else
1747 	IXGBE_WRITE_REG(hw, IXGBE_VFDCA_RXCTRL(reg_idx),
1748 			IXGBE_DCA_RXCTRL_DESC_RRO_EN |
1749 			IXGBE_DCA_RXCTRL_DATA_WRO_EN);
1750 #endif
1751 
1752 	/* reset head and tail pointers */
1753 	IXGBE_WRITE_REG(hw, IXGBE_VFRDH(reg_idx), 0);
1754 	IXGBE_WRITE_REG(hw, IXGBE_VFRDT(reg_idx), 0);
1755 	ring->tail = adapter->io_addr + IXGBE_VFRDT(reg_idx);
1756 
1757 	/* initialize rx_buffer_info */
1758 	memset(ring->rx_buffer_info, 0,
1759 	       sizeof(struct ixgbevf_rx_buffer) * ring->count);
1760 
1761 	/* initialize Rx descriptor 0 */
1762 	rx_desc = IXGBEVF_RX_DESC(ring, 0);
1763 	rx_desc->wb.upper.length = 0;
1764 
1765 	/* reset ntu and ntc to place SW in sync with hardwdare */
1766 	ring->next_to_clean = 0;
1767 	ring->next_to_use = 0;
1768 	ring->next_to_alloc = 0;
1769 
1770 	ixgbevf_configure_srrctl(adapter, reg_idx);
1771 
1772 	/* allow any size packet since we can handle overflow */
1773 	rxdctl &= ~IXGBE_RXDCTL_RLPML_EN;
1774 
1775 	rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1776 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(reg_idx), rxdctl);
1777 
1778 	ixgbevf_rx_desc_queue_enable(adapter, ring);
1779 	ixgbevf_alloc_rx_buffers(ring, ixgbevf_desc_unused(ring));
1780 }
1781 
1782 /**
1783  * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
1784  * @adapter: board private structure
1785  *
1786  * Configure the Rx unit of the MAC after a reset.
1787  **/
1788 static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
1789 {
1790 	struct ixgbe_hw *hw = &adapter->hw;
1791 	struct net_device *netdev = adapter->netdev;
1792 	int i, ret;
1793 
1794 	ixgbevf_setup_psrtype(adapter);
1795 	if (hw->mac.type >= ixgbe_mac_X550_vf)
1796 		ixgbevf_setup_vfmrqc(adapter);
1797 
1798 	spin_lock_bh(&adapter->mbx_lock);
1799 	/* notify the PF of our intent to use this size of frame */
1800 	ret = hw->mac.ops.set_rlpml(hw, netdev->mtu + ETH_HLEN + ETH_FCS_LEN);
1801 	spin_unlock_bh(&adapter->mbx_lock);
1802 	if (ret)
1803 		dev_err(&adapter->pdev->dev,
1804 			"Failed to set MTU at %d\n", netdev->mtu);
1805 
1806 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
1807 	 * the Base and Length of the Rx Descriptor Ring
1808 	 */
1809 	for (i = 0; i < adapter->num_rx_queues; i++)
1810 		ixgbevf_configure_rx_ring(adapter, adapter->rx_ring[i]);
1811 }
1812 
1813 static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev,
1814 				   __be16 proto, u16 vid)
1815 {
1816 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1817 	struct ixgbe_hw *hw = &adapter->hw;
1818 	int err;
1819 
1820 	spin_lock_bh(&adapter->mbx_lock);
1821 
1822 	/* add VID to filter table */
1823 	err = hw->mac.ops.set_vfta(hw, vid, 0, true);
1824 
1825 	spin_unlock_bh(&adapter->mbx_lock);
1826 
1827 	/* translate error return types so error makes sense */
1828 	if (err == IXGBE_ERR_MBX)
1829 		return -EIO;
1830 
1831 	if (err == IXGBE_ERR_INVALID_ARGUMENT)
1832 		return -EACCES;
1833 
1834 	set_bit(vid, adapter->active_vlans);
1835 
1836 	return err;
1837 }
1838 
1839 static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev,
1840 				    __be16 proto, u16 vid)
1841 {
1842 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1843 	struct ixgbe_hw *hw = &adapter->hw;
1844 	int err;
1845 
1846 	spin_lock_bh(&adapter->mbx_lock);
1847 
1848 	/* remove VID from filter table */
1849 	err = hw->mac.ops.set_vfta(hw, vid, 0, false);
1850 
1851 	spin_unlock_bh(&adapter->mbx_lock);
1852 
1853 	clear_bit(vid, adapter->active_vlans);
1854 
1855 	return err;
1856 }
1857 
1858 static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
1859 {
1860 	u16 vid;
1861 
1862 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
1863 		ixgbevf_vlan_rx_add_vid(adapter->netdev,
1864 					htons(ETH_P_8021Q), vid);
1865 }
1866 
1867 static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
1868 {
1869 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1870 	struct ixgbe_hw *hw = &adapter->hw;
1871 	int count = 0;
1872 
1873 	if ((netdev_uc_count(netdev)) > 10) {
1874 		pr_err("Too many unicast filters - No Space\n");
1875 		return -ENOSPC;
1876 	}
1877 
1878 	if (!netdev_uc_empty(netdev)) {
1879 		struct netdev_hw_addr *ha;
1880 
1881 		netdev_for_each_uc_addr(ha, netdev) {
1882 			hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
1883 			udelay(200);
1884 		}
1885 	} else {
1886 		/* If the list is empty then send message to PF driver to
1887 		 * clear all MAC VLANs on this VF.
1888 		 */
1889 		hw->mac.ops.set_uc_addr(hw, 0, NULL);
1890 	}
1891 
1892 	return count;
1893 }
1894 
1895 /**
1896  * ixgbevf_set_rx_mode - Multicast and unicast set
1897  * @netdev: network interface device structure
1898  *
1899  * The set_rx_method entry point is called whenever the multicast address
1900  * list, unicast address list or the network interface flags are updated.
1901  * This routine is responsible for configuring the hardware for proper
1902  * multicast mode and configuring requested unicast filters.
1903  **/
1904 static void ixgbevf_set_rx_mode(struct net_device *netdev)
1905 {
1906 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
1907 	struct ixgbe_hw *hw = &adapter->hw;
1908 	unsigned int flags = netdev->flags;
1909 	int xcast_mode;
1910 
1911 	/* request the most inclusive mode we need */
1912 	if (flags & IFF_PROMISC)
1913 		xcast_mode = IXGBEVF_XCAST_MODE_PROMISC;
1914 	else if (flags & IFF_ALLMULTI)
1915 		xcast_mode = IXGBEVF_XCAST_MODE_ALLMULTI;
1916 	else if (flags & (IFF_BROADCAST | IFF_MULTICAST))
1917 		xcast_mode = IXGBEVF_XCAST_MODE_MULTI;
1918 	else
1919 		xcast_mode = IXGBEVF_XCAST_MODE_NONE;
1920 
1921 	spin_lock_bh(&adapter->mbx_lock);
1922 
1923 	hw->mac.ops.update_xcast_mode(hw, xcast_mode);
1924 
1925 	/* reprogram multicast list */
1926 	hw->mac.ops.update_mc_addr_list(hw, netdev);
1927 
1928 	ixgbevf_write_uc_addr_list(netdev);
1929 
1930 	spin_unlock_bh(&adapter->mbx_lock);
1931 }
1932 
1933 static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
1934 {
1935 	int q_idx;
1936 	struct ixgbevf_q_vector *q_vector;
1937 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1938 
1939 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1940 		q_vector = adapter->q_vector[q_idx];
1941 		napi_enable(&q_vector->napi);
1942 	}
1943 }
1944 
1945 static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
1946 {
1947 	int q_idx;
1948 	struct ixgbevf_q_vector *q_vector;
1949 	int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
1950 
1951 	for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1952 		q_vector = adapter->q_vector[q_idx];
1953 		napi_disable(&q_vector->napi);
1954 	}
1955 }
1956 
1957 static int ixgbevf_configure_dcb(struct ixgbevf_adapter *adapter)
1958 {
1959 	struct ixgbe_hw *hw = &adapter->hw;
1960 	unsigned int def_q = 0;
1961 	unsigned int num_tcs = 0;
1962 	unsigned int num_rx_queues = adapter->num_rx_queues;
1963 	unsigned int num_tx_queues = adapter->num_tx_queues;
1964 	int err;
1965 
1966 	spin_lock_bh(&adapter->mbx_lock);
1967 
1968 	/* fetch queue configuration from the PF */
1969 	err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
1970 
1971 	spin_unlock_bh(&adapter->mbx_lock);
1972 
1973 	if (err)
1974 		return err;
1975 
1976 	if (num_tcs > 1) {
1977 		/* we need only one Tx queue */
1978 		num_tx_queues = 1;
1979 
1980 		/* update default Tx ring register index */
1981 		adapter->tx_ring[0]->reg_idx = def_q;
1982 
1983 		/* we need as many queues as traffic classes */
1984 		num_rx_queues = num_tcs;
1985 	}
1986 
1987 	/* if we have a bad config abort request queue reset */
1988 	if ((adapter->num_rx_queues != num_rx_queues) ||
1989 	    (adapter->num_tx_queues != num_tx_queues)) {
1990 		/* force mailbox timeout to prevent further messages */
1991 		hw->mbx.timeout = 0;
1992 
1993 		/* wait for watchdog to come around and bail us out */
1994 		set_bit(__IXGBEVF_QUEUE_RESET_REQUESTED, &adapter->state);
1995 	}
1996 
1997 	return 0;
1998 }
1999 
2000 static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
2001 {
2002 	ixgbevf_configure_dcb(adapter);
2003 
2004 	ixgbevf_set_rx_mode(adapter->netdev);
2005 
2006 	ixgbevf_restore_vlan(adapter);
2007 
2008 	ixgbevf_configure_tx(adapter);
2009 	ixgbevf_configure_rx(adapter);
2010 }
2011 
2012 static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
2013 {
2014 	/* Only save pre-reset stats if there are some */
2015 	if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
2016 		adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
2017 			adapter->stats.base_vfgprc;
2018 		adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
2019 			adapter->stats.base_vfgptc;
2020 		adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
2021 			adapter->stats.base_vfgorc;
2022 		adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
2023 			adapter->stats.base_vfgotc;
2024 		adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
2025 			adapter->stats.base_vfmprc;
2026 	}
2027 }
2028 
2029 static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
2030 {
2031 	struct ixgbe_hw *hw = &adapter->hw;
2032 
2033 	adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2034 	adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2035 	adapter->stats.last_vfgorc |=
2036 		(((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2037 	adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2038 	adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2039 	adapter->stats.last_vfgotc |=
2040 		(((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2041 	adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2042 
2043 	adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
2044 	adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
2045 	adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
2046 	adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
2047 	adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
2048 }
2049 
2050 static void ixgbevf_negotiate_api(struct ixgbevf_adapter *adapter)
2051 {
2052 	struct ixgbe_hw *hw = &adapter->hw;
2053 	int api[] = { ixgbe_mbox_api_13,
2054 		      ixgbe_mbox_api_12,
2055 		      ixgbe_mbox_api_11,
2056 		      ixgbe_mbox_api_10,
2057 		      ixgbe_mbox_api_unknown };
2058 	int err, idx = 0;
2059 
2060 	spin_lock_bh(&adapter->mbx_lock);
2061 
2062 	while (api[idx] != ixgbe_mbox_api_unknown) {
2063 		err = hw->mac.ops.negotiate_api_version(hw, api[idx]);
2064 		if (!err)
2065 			break;
2066 		idx++;
2067 	}
2068 
2069 	spin_unlock_bh(&adapter->mbx_lock);
2070 }
2071 
2072 static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
2073 {
2074 	struct net_device *netdev = adapter->netdev;
2075 	struct ixgbe_hw *hw = &adapter->hw;
2076 
2077 	ixgbevf_configure_msix(adapter);
2078 
2079 	spin_lock_bh(&adapter->mbx_lock);
2080 
2081 	if (is_valid_ether_addr(hw->mac.addr))
2082 		hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
2083 	else
2084 		hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
2085 
2086 	spin_unlock_bh(&adapter->mbx_lock);
2087 
2088 	smp_mb__before_atomic();
2089 	clear_bit(__IXGBEVF_DOWN, &adapter->state);
2090 	ixgbevf_napi_enable_all(adapter);
2091 
2092 	/* clear any pending interrupts, may auto mask */
2093 	IXGBE_READ_REG(hw, IXGBE_VTEICR);
2094 	ixgbevf_irq_enable(adapter);
2095 
2096 	/* enable transmits */
2097 	netif_tx_start_all_queues(netdev);
2098 
2099 	ixgbevf_save_reset_stats(adapter);
2100 	ixgbevf_init_last_counter_stats(adapter);
2101 
2102 	hw->mac.get_link_status = 1;
2103 	mod_timer(&adapter->service_timer, jiffies);
2104 }
2105 
2106 void ixgbevf_up(struct ixgbevf_adapter *adapter)
2107 {
2108 	ixgbevf_configure(adapter);
2109 
2110 	ixgbevf_up_complete(adapter);
2111 }
2112 
2113 /**
2114  * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
2115  * @rx_ring: ring to free buffers from
2116  **/
2117 static void ixgbevf_clean_rx_ring(struct ixgbevf_ring *rx_ring)
2118 {
2119 	u16 i = rx_ring->next_to_clean;
2120 
2121 	/* Free Rx ring sk_buff */
2122 	if (rx_ring->skb) {
2123 		dev_kfree_skb(rx_ring->skb);
2124 		rx_ring->skb = NULL;
2125 	}
2126 
2127 	/* Free all the Rx ring pages */
2128 	while (i != rx_ring->next_to_alloc) {
2129 		struct ixgbevf_rx_buffer *rx_buffer;
2130 
2131 		rx_buffer = &rx_ring->rx_buffer_info[i];
2132 
2133 		/* Invalidate cache lines that may have been written to by
2134 		 * device so that we avoid corrupting memory.
2135 		 */
2136 		dma_sync_single_range_for_cpu(rx_ring->dev,
2137 					      rx_buffer->dma,
2138 					      rx_buffer->page_offset,
2139 					      IXGBEVF_RX_BUFSZ,
2140 					      DMA_FROM_DEVICE);
2141 
2142 		/* free resources associated with mapping */
2143 		dma_unmap_page_attrs(rx_ring->dev,
2144 				     rx_buffer->dma,
2145 				     PAGE_SIZE,
2146 				     DMA_FROM_DEVICE,
2147 				     IXGBEVF_RX_DMA_ATTR);
2148 
2149 		__page_frag_cache_drain(rx_buffer->page,
2150 					rx_buffer->pagecnt_bias);
2151 
2152 		i++;
2153 		if (i == rx_ring->count)
2154 			i = 0;
2155 	}
2156 
2157 	rx_ring->next_to_alloc = 0;
2158 	rx_ring->next_to_clean = 0;
2159 	rx_ring->next_to_use = 0;
2160 }
2161 
2162 /**
2163  * ixgbevf_clean_tx_ring - Free Tx Buffers
2164  * @tx_ring: ring to be cleaned
2165  **/
2166 static void ixgbevf_clean_tx_ring(struct ixgbevf_ring *tx_ring)
2167 {
2168 	u16 i = tx_ring->next_to_clean;
2169 	struct ixgbevf_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
2170 
2171 	while (i != tx_ring->next_to_use) {
2172 		union ixgbe_adv_tx_desc *eop_desc, *tx_desc;
2173 
2174 		/* Free all the Tx ring sk_buffs */
2175 		dev_kfree_skb_any(tx_buffer->skb);
2176 
2177 		/* unmap skb header data */
2178 		dma_unmap_single(tx_ring->dev,
2179 				 dma_unmap_addr(tx_buffer, dma),
2180 				 dma_unmap_len(tx_buffer, len),
2181 				 DMA_TO_DEVICE);
2182 
2183 		/* check for eop_desc to determine the end of the packet */
2184 		eop_desc = tx_buffer->next_to_watch;
2185 		tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
2186 
2187 		/* unmap remaining buffers */
2188 		while (tx_desc != eop_desc) {
2189 			tx_buffer++;
2190 			tx_desc++;
2191 			i++;
2192 			if (unlikely(i == tx_ring->count)) {
2193 				i = 0;
2194 				tx_buffer = tx_ring->tx_buffer_info;
2195 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
2196 			}
2197 
2198 			/* unmap any remaining paged data */
2199 			if (dma_unmap_len(tx_buffer, len))
2200 				dma_unmap_page(tx_ring->dev,
2201 					       dma_unmap_addr(tx_buffer, dma),
2202 					       dma_unmap_len(tx_buffer, len),
2203 					       DMA_TO_DEVICE);
2204 		}
2205 
2206 		/* move us one more past the eop_desc for start of next pkt */
2207 		tx_buffer++;
2208 		i++;
2209 		if (unlikely(i == tx_ring->count)) {
2210 			i = 0;
2211 			tx_buffer = tx_ring->tx_buffer_info;
2212 		}
2213 	}
2214 
2215 	/* reset next_to_use and next_to_clean */
2216 	tx_ring->next_to_use = 0;
2217 	tx_ring->next_to_clean = 0;
2218 
2219 }
2220 
2221 /**
2222  * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
2223  * @adapter: board private structure
2224  **/
2225 static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
2226 {
2227 	int i;
2228 
2229 	for (i = 0; i < adapter->num_rx_queues; i++)
2230 		ixgbevf_clean_rx_ring(adapter->rx_ring[i]);
2231 }
2232 
2233 /**
2234  * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
2235  * @adapter: board private structure
2236  **/
2237 static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
2238 {
2239 	int i;
2240 
2241 	for (i = 0; i < adapter->num_tx_queues; i++)
2242 		ixgbevf_clean_tx_ring(adapter->tx_ring[i]);
2243 }
2244 
2245 void ixgbevf_down(struct ixgbevf_adapter *adapter)
2246 {
2247 	struct net_device *netdev = adapter->netdev;
2248 	struct ixgbe_hw *hw = &adapter->hw;
2249 	int i;
2250 
2251 	/* signal that we are down to the interrupt handler */
2252 	if (test_and_set_bit(__IXGBEVF_DOWN, &adapter->state))
2253 		return; /* do nothing if already down */
2254 
2255 	/* disable all enabled Rx queues */
2256 	for (i = 0; i < adapter->num_rx_queues; i++)
2257 		ixgbevf_disable_rx_queue(adapter, adapter->rx_ring[i]);
2258 
2259 	usleep_range(10000, 20000);
2260 
2261 	netif_tx_stop_all_queues(netdev);
2262 
2263 	/* call carrier off first to avoid false dev_watchdog timeouts */
2264 	netif_carrier_off(netdev);
2265 	netif_tx_disable(netdev);
2266 
2267 	ixgbevf_irq_disable(adapter);
2268 
2269 	ixgbevf_napi_disable_all(adapter);
2270 
2271 	del_timer_sync(&adapter->service_timer);
2272 
2273 	/* disable transmits in the hardware now that interrupts are off */
2274 	for (i = 0; i < adapter->num_tx_queues; i++) {
2275 		u8 reg_idx = adapter->tx_ring[i]->reg_idx;
2276 
2277 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(reg_idx),
2278 				IXGBE_TXDCTL_SWFLSH);
2279 	}
2280 
2281 	if (!pci_channel_offline(adapter->pdev))
2282 		ixgbevf_reset(adapter);
2283 
2284 	ixgbevf_clean_all_tx_rings(adapter);
2285 	ixgbevf_clean_all_rx_rings(adapter);
2286 }
2287 
2288 void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
2289 {
2290 	WARN_ON(in_interrupt());
2291 
2292 	while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
2293 		msleep(1);
2294 
2295 	ixgbevf_down(adapter);
2296 	ixgbevf_up(adapter);
2297 
2298 	clear_bit(__IXGBEVF_RESETTING, &adapter->state);
2299 }
2300 
2301 void ixgbevf_reset(struct ixgbevf_adapter *adapter)
2302 {
2303 	struct ixgbe_hw *hw = &adapter->hw;
2304 	struct net_device *netdev = adapter->netdev;
2305 
2306 	if (hw->mac.ops.reset_hw(hw)) {
2307 		hw_dbg(hw, "PF still resetting\n");
2308 	} else {
2309 		hw->mac.ops.init_hw(hw);
2310 		ixgbevf_negotiate_api(adapter);
2311 	}
2312 
2313 	if (is_valid_ether_addr(adapter->hw.mac.addr)) {
2314 		ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
2315 		ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2316 	}
2317 
2318 	adapter->last_reset = jiffies;
2319 }
2320 
2321 static int ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
2322 					int vectors)
2323 {
2324 	int vector_threshold;
2325 
2326 	/* We'll want at least 2 (vector_threshold):
2327 	 * 1) TxQ[0] + RxQ[0] handler
2328 	 * 2) Other (Link Status Change, etc.)
2329 	 */
2330 	vector_threshold = MIN_MSIX_COUNT;
2331 
2332 	/* The more we get, the more we will assign to Tx/Rx Cleanup
2333 	 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
2334 	 * Right now, we simply care about how many we'll get; we'll
2335 	 * set them up later while requesting irq's.
2336 	 */
2337 	vectors = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
2338 					vector_threshold, vectors);
2339 
2340 	if (vectors < 0) {
2341 		dev_err(&adapter->pdev->dev,
2342 			"Unable to allocate MSI-X interrupts\n");
2343 		kfree(adapter->msix_entries);
2344 		adapter->msix_entries = NULL;
2345 		return vectors;
2346 	}
2347 
2348 	/* Adjust for only the vectors we'll use, which is minimum
2349 	 * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
2350 	 * vectors we were allocated.
2351 	 */
2352 	adapter->num_msix_vectors = vectors;
2353 
2354 	return 0;
2355 }
2356 
2357 /**
2358  * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
2359  * @adapter: board private structure to initialize
2360  *
2361  * This is the top level queue allocation routine.  The order here is very
2362  * important, starting with the "most" number of features turned on at once,
2363  * and ending with the smallest set of features.  This way large combinations
2364  * can be allocated if they're turned on, and smaller combinations are the
2365  * fallthrough conditions.
2366  *
2367  **/
2368 static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
2369 {
2370 	struct ixgbe_hw *hw = &adapter->hw;
2371 	unsigned int def_q = 0;
2372 	unsigned int num_tcs = 0;
2373 	int err;
2374 
2375 	/* Start with base case */
2376 	adapter->num_rx_queues = 1;
2377 	adapter->num_tx_queues = 1;
2378 
2379 	spin_lock_bh(&adapter->mbx_lock);
2380 
2381 	/* fetch queue configuration from the PF */
2382 	err = ixgbevf_get_queues(hw, &num_tcs, &def_q);
2383 
2384 	spin_unlock_bh(&adapter->mbx_lock);
2385 
2386 	if (err)
2387 		return;
2388 
2389 	/* we need as many queues as traffic classes */
2390 	if (num_tcs > 1) {
2391 		adapter->num_rx_queues = num_tcs;
2392 	} else {
2393 		u16 rss = min_t(u16, num_online_cpus(), IXGBEVF_MAX_RSS_QUEUES);
2394 
2395 		switch (hw->api_version) {
2396 		case ixgbe_mbox_api_11:
2397 		case ixgbe_mbox_api_12:
2398 		case ixgbe_mbox_api_13:
2399 			adapter->num_rx_queues = rss;
2400 			adapter->num_tx_queues = rss;
2401 		default:
2402 			break;
2403 		}
2404 	}
2405 }
2406 
2407 /**
2408  * ixgbevf_alloc_queues - Allocate memory for all rings
2409  * @adapter: board private structure to initialize
2410  *
2411  * We allocate one ring per queue at run-time since we don't know the
2412  * number of queues at compile-time.  The polling_netdev array is
2413  * intended for Multiqueue, but should work fine with a single queue.
2414  **/
2415 static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
2416 {
2417 	struct ixgbevf_ring *ring;
2418 	int rx = 0, tx = 0;
2419 
2420 	for (; tx < adapter->num_tx_queues; tx++) {
2421 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2422 		if (!ring)
2423 			goto err_allocation;
2424 
2425 		ring->dev = &adapter->pdev->dev;
2426 		ring->netdev = adapter->netdev;
2427 		ring->count = adapter->tx_ring_count;
2428 		ring->queue_index = tx;
2429 		ring->reg_idx = tx;
2430 
2431 		adapter->tx_ring[tx] = ring;
2432 	}
2433 
2434 	for (; rx < adapter->num_rx_queues; rx++) {
2435 		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
2436 		if (!ring)
2437 			goto err_allocation;
2438 
2439 		ring->dev = &adapter->pdev->dev;
2440 		ring->netdev = adapter->netdev;
2441 
2442 		ring->count = adapter->rx_ring_count;
2443 		ring->queue_index = rx;
2444 		ring->reg_idx = rx;
2445 
2446 		adapter->rx_ring[rx] = ring;
2447 	}
2448 
2449 	return 0;
2450 
2451 err_allocation:
2452 	while (tx) {
2453 		kfree(adapter->tx_ring[--tx]);
2454 		adapter->tx_ring[tx] = NULL;
2455 	}
2456 
2457 	while (rx) {
2458 		kfree(adapter->rx_ring[--rx]);
2459 		adapter->rx_ring[rx] = NULL;
2460 	}
2461 	return -ENOMEM;
2462 }
2463 
2464 /**
2465  * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
2466  * @adapter: board private structure to initialize
2467  *
2468  * Attempt to configure the interrupts using the best available
2469  * capabilities of the hardware and the kernel.
2470  **/
2471 static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
2472 {
2473 	struct net_device *netdev = adapter->netdev;
2474 	int err;
2475 	int vector, v_budget;
2476 
2477 	/* It's easy to be greedy for MSI-X vectors, but it really
2478 	 * doesn't do us much good if we have a lot more vectors
2479 	 * than CPU's.  So let's be conservative and only ask for
2480 	 * (roughly) the same number of vectors as there are CPU's.
2481 	 * The default is to use pairs of vectors.
2482 	 */
2483 	v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
2484 	v_budget = min_t(int, v_budget, num_online_cpus());
2485 	v_budget += NON_Q_VECTORS;
2486 
2487 	/* A failure in MSI-X entry allocation isn't fatal, but it does
2488 	 * mean we disable MSI-X capabilities of the adapter.
2489 	 */
2490 	adapter->msix_entries = kcalloc(v_budget,
2491 					sizeof(struct msix_entry), GFP_KERNEL);
2492 	if (!adapter->msix_entries)
2493 		return -ENOMEM;
2494 
2495 	for (vector = 0; vector < v_budget; vector++)
2496 		adapter->msix_entries[vector].entry = vector;
2497 
2498 	err = ixgbevf_acquire_msix_vectors(adapter, v_budget);
2499 	if (err)
2500 		return err;
2501 
2502 	err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
2503 	if (err)
2504 		return err;
2505 
2506 	return netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
2507 }
2508 
2509 /**
2510  * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
2511  * @adapter: board private structure to initialize
2512  *
2513  * We allocate one q_vector per queue interrupt.  If allocation fails we
2514  * return -ENOMEM.
2515  **/
2516 static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
2517 {
2518 	int q_idx, num_q_vectors;
2519 	struct ixgbevf_q_vector *q_vector;
2520 
2521 	num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2522 
2523 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2524 		q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
2525 		if (!q_vector)
2526 			goto err_out;
2527 		q_vector->adapter = adapter;
2528 		q_vector->v_idx = q_idx;
2529 		netif_napi_add(adapter->netdev, &q_vector->napi,
2530 			       ixgbevf_poll, 64);
2531 		adapter->q_vector[q_idx] = q_vector;
2532 	}
2533 
2534 	return 0;
2535 
2536 err_out:
2537 	while (q_idx) {
2538 		q_idx--;
2539 		q_vector = adapter->q_vector[q_idx];
2540 #ifdef CONFIG_NET_RX_BUSY_POLL
2541 		napi_hash_del(&q_vector->napi);
2542 #endif
2543 		netif_napi_del(&q_vector->napi);
2544 		kfree(q_vector);
2545 		adapter->q_vector[q_idx] = NULL;
2546 	}
2547 	return -ENOMEM;
2548 }
2549 
2550 /**
2551  * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
2552  * @adapter: board private structure to initialize
2553  *
2554  * This function frees the memory allocated to the q_vectors.  In addition if
2555  * NAPI is enabled it will delete any references to the NAPI struct prior
2556  * to freeing the q_vector.
2557  **/
2558 static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
2559 {
2560 	int q_idx, num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
2561 
2562 	for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
2563 		struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
2564 
2565 		adapter->q_vector[q_idx] = NULL;
2566 #ifdef CONFIG_NET_RX_BUSY_POLL
2567 		napi_hash_del(&q_vector->napi);
2568 #endif
2569 		netif_napi_del(&q_vector->napi);
2570 		kfree(q_vector);
2571 	}
2572 }
2573 
2574 /**
2575  * ixgbevf_reset_interrupt_capability - Reset MSIX setup
2576  * @adapter: board private structure
2577  *
2578  **/
2579 static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
2580 {
2581 	if (!adapter->msix_entries)
2582 		return;
2583 
2584 	pci_disable_msix(adapter->pdev);
2585 	kfree(adapter->msix_entries);
2586 	adapter->msix_entries = NULL;
2587 }
2588 
2589 /**
2590  * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
2591  * @adapter: board private structure to initialize
2592  *
2593  **/
2594 static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
2595 {
2596 	int err;
2597 
2598 	/* Number of supported queues */
2599 	ixgbevf_set_num_queues(adapter);
2600 
2601 	err = ixgbevf_set_interrupt_capability(adapter);
2602 	if (err) {
2603 		hw_dbg(&adapter->hw,
2604 		       "Unable to setup interrupt capabilities\n");
2605 		goto err_set_interrupt;
2606 	}
2607 
2608 	err = ixgbevf_alloc_q_vectors(adapter);
2609 	if (err) {
2610 		hw_dbg(&adapter->hw, "Unable to allocate memory for queue vectors\n");
2611 		goto err_alloc_q_vectors;
2612 	}
2613 
2614 	err = ixgbevf_alloc_queues(adapter);
2615 	if (err) {
2616 		pr_err("Unable to allocate memory for queues\n");
2617 		goto err_alloc_queues;
2618 	}
2619 
2620 	hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, Tx Queue count = %u\n",
2621 	       (adapter->num_rx_queues > 1) ? "Enabled" :
2622 	       "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
2623 
2624 	set_bit(__IXGBEVF_DOWN, &adapter->state);
2625 
2626 	return 0;
2627 err_alloc_queues:
2628 	ixgbevf_free_q_vectors(adapter);
2629 err_alloc_q_vectors:
2630 	ixgbevf_reset_interrupt_capability(adapter);
2631 err_set_interrupt:
2632 	return err;
2633 }
2634 
2635 /**
2636  * ixgbevf_clear_interrupt_scheme - Clear the current interrupt scheme settings
2637  * @adapter: board private structure to clear interrupt scheme on
2638  *
2639  * We go through and clear interrupt specific resources and reset the structure
2640  * to pre-load conditions
2641  **/
2642 static void ixgbevf_clear_interrupt_scheme(struct ixgbevf_adapter *adapter)
2643 {
2644 	int i;
2645 
2646 	for (i = 0; i < adapter->num_tx_queues; i++) {
2647 		kfree(adapter->tx_ring[i]);
2648 		adapter->tx_ring[i] = NULL;
2649 	}
2650 	for (i = 0; i < adapter->num_rx_queues; i++) {
2651 		kfree(adapter->rx_ring[i]);
2652 		adapter->rx_ring[i] = NULL;
2653 	}
2654 
2655 	adapter->num_tx_queues = 0;
2656 	adapter->num_rx_queues = 0;
2657 
2658 	ixgbevf_free_q_vectors(adapter);
2659 	ixgbevf_reset_interrupt_capability(adapter);
2660 }
2661 
2662 /**
2663  * ixgbevf_sw_init - Initialize general software structures
2664  * @adapter: board private structure to initialize
2665  *
2666  * ixgbevf_sw_init initializes the Adapter private data structure.
2667  * Fields are initialized based on PCI device information and
2668  * OS network device settings (MTU size).
2669  **/
2670 static int ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
2671 {
2672 	struct ixgbe_hw *hw = &adapter->hw;
2673 	struct pci_dev *pdev = adapter->pdev;
2674 	struct net_device *netdev = adapter->netdev;
2675 	int err;
2676 
2677 	/* PCI config space info */
2678 	hw->vendor_id = pdev->vendor;
2679 	hw->device_id = pdev->device;
2680 	hw->revision_id = pdev->revision;
2681 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
2682 	hw->subsystem_device_id = pdev->subsystem_device;
2683 
2684 	hw->mbx.ops.init_params(hw);
2685 
2686 	if (hw->mac.type >= ixgbe_mac_X550_vf) {
2687 		err = ixgbevf_init_rss_key(adapter);
2688 		if (err)
2689 			goto out;
2690 	}
2691 
2692 	/* assume legacy case in which PF would only give VF 2 queues */
2693 	hw->mac.max_tx_queues = 2;
2694 	hw->mac.max_rx_queues = 2;
2695 
2696 	/* lock to protect mailbox accesses */
2697 	spin_lock_init(&adapter->mbx_lock);
2698 
2699 	err = hw->mac.ops.reset_hw(hw);
2700 	if (err) {
2701 		dev_info(&pdev->dev,
2702 			 "PF still in reset state.  Is the PF interface up?\n");
2703 	} else {
2704 		err = hw->mac.ops.init_hw(hw);
2705 		if (err) {
2706 			pr_err("init_shared_code failed: %d\n", err);
2707 			goto out;
2708 		}
2709 		ixgbevf_negotiate_api(adapter);
2710 		err = hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
2711 		if (err)
2712 			dev_info(&pdev->dev, "Error reading MAC address\n");
2713 		else if (is_zero_ether_addr(adapter->hw.mac.addr))
2714 			dev_info(&pdev->dev,
2715 				 "MAC address not assigned by administrator.\n");
2716 		ether_addr_copy(netdev->dev_addr, hw->mac.addr);
2717 	}
2718 
2719 	if (!is_valid_ether_addr(netdev->dev_addr)) {
2720 		dev_info(&pdev->dev, "Assigning random MAC address\n");
2721 		eth_hw_addr_random(netdev);
2722 		ether_addr_copy(hw->mac.addr, netdev->dev_addr);
2723 		ether_addr_copy(hw->mac.perm_addr, netdev->dev_addr);
2724 	}
2725 
2726 	/* Enable dynamic interrupt throttling rates */
2727 	adapter->rx_itr_setting = 1;
2728 	adapter->tx_itr_setting = 1;
2729 
2730 	/* set default ring sizes */
2731 	adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
2732 	adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
2733 
2734 	set_bit(__IXGBEVF_DOWN, &adapter->state);
2735 	return 0;
2736 
2737 out:
2738 	return err;
2739 }
2740 
2741 #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter)	\
2742 	{							\
2743 		u32 current_counter = IXGBE_READ_REG(hw, reg);	\
2744 		if (current_counter < last_counter)		\
2745 			counter += 0x100000000LL;		\
2746 		last_counter = current_counter;			\
2747 		counter &= 0xFFFFFFFF00000000LL;		\
2748 		counter |= current_counter;			\
2749 	}
2750 
2751 #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
2752 	{								 \
2753 		u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb);	 \
2754 		u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb);	 \
2755 		u64 current_counter = (current_counter_msb << 32) |	 \
2756 			current_counter_lsb;				 \
2757 		if (current_counter < last_counter)			 \
2758 			counter += 0x1000000000LL;			 \
2759 		last_counter = current_counter;				 \
2760 		counter &= 0xFFFFFFF000000000LL;			 \
2761 		counter |= current_counter;				 \
2762 	}
2763 /**
2764  * ixgbevf_update_stats - Update the board statistics counters.
2765  * @adapter: board private structure
2766  **/
2767 void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
2768 {
2769 	struct ixgbe_hw *hw = &adapter->hw;
2770 	u64 alloc_rx_page_failed = 0, alloc_rx_buff_failed = 0;
2771 	u64 alloc_rx_page = 0, hw_csum_rx_error = 0;
2772 	int i;
2773 
2774 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2775 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
2776 		return;
2777 
2778 	UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
2779 				adapter->stats.vfgprc);
2780 	UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
2781 				adapter->stats.vfgptc);
2782 	UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2783 				adapter->stats.last_vfgorc,
2784 				adapter->stats.vfgorc);
2785 	UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2786 				adapter->stats.last_vfgotc,
2787 				adapter->stats.vfgotc);
2788 	UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
2789 				adapter->stats.vfmprc);
2790 
2791 	for (i = 0;  i  < adapter->num_rx_queues;  i++) {
2792 		struct ixgbevf_ring *rx_ring = adapter->rx_ring[i];
2793 
2794 		hw_csum_rx_error += rx_ring->rx_stats.csum_err;
2795 		alloc_rx_page_failed += rx_ring->rx_stats.alloc_rx_page_failed;
2796 		alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
2797 		alloc_rx_page += rx_ring->rx_stats.alloc_rx_page;
2798 	}
2799 
2800 	adapter->hw_csum_rx_error = hw_csum_rx_error;
2801 	adapter->alloc_rx_page_failed = alloc_rx_page_failed;
2802 	adapter->alloc_rx_buff_failed = alloc_rx_buff_failed;
2803 	adapter->alloc_rx_page = alloc_rx_page;
2804 }
2805 
2806 /**
2807  * ixgbevf_service_timer - Timer Call-back
2808  * @t: pointer to timer_list struct
2809  **/
2810 static void ixgbevf_service_timer(struct timer_list *t)
2811 {
2812 	struct ixgbevf_adapter *adapter = from_timer(adapter, t,
2813 						     service_timer);
2814 
2815 	/* Reset the timer */
2816 	mod_timer(&adapter->service_timer, (HZ * 2) + jiffies);
2817 
2818 	ixgbevf_service_event_schedule(adapter);
2819 }
2820 
2821 static void ixgbevf_reset_subtask(struct ixgbevf_adapter *adapter)
2822 {
2823 	if (!test_and_clear_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state))
2824 		return;
2825 
2826 	/* If we're already down or resetting, just bail */
2827 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2828 	    test_bit(__IXGBEVF_REMOVING, &adapter->state) ||
2829 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
2830 		return;
2831 
2832 	adapter->tx_timeout_count++;
2833 
2834 	rtnl_lock();
2835 	ixgbevf_reinit_locked(adapter);
2836 	rtnl_unlock();
2837 }
2838 
2839 /**
2840  * ixgbevf_check_hang_subtask - check for hung queues and dropped interrupts
2841  * @adapter: pointer to the device adapter structure
2842  *
2843  * This function serves two purposes.  First it strobes the interrupt lines
2844  * in order to make certain interrupts are occurring.  Secondly it sets the
2845  * bits needed to check for TX hangs.  As a result we should immediately
2846  * determine if a hang has occurred.
2847  **/
2848 static void ixgbevf_check_hang_subtask(struct ixgbevf_adapter *adapter)
2849 {
2850 	struct ixgbe_hw *hw = &adapter->hw;
2851 	u32 eics = 0;
2852 	int i;
2853 
2854 	/* If we're down or resetting, just bail */
2855 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2856 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
2857 		return;
2858 
2859 	/* Force detection of hung controller */
2860 	if (netif_carrier_ok(adapter->netdev)) {
2861 		for (i = 0; i < adapter->num_tx_queues; i++)
2862 			set_check_for_tx_hang(adapter->tx_ring[i]);
2863 	}
2864 
2865 	/* get one bit for every active Tx/Rx interrupt vector */
2866 	for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
2867 		struct ixgbevf_q_vector *qv = adapter->q_vector[i];
2868 
2869 		if (qv->rx.ring || qv->tx.ring)
2870 			eics |= BIT(i);
2871 	}
2872 
2873 	/* Cause software interrupt to ensure rings are cleaned */
2874 	IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
2875 }
2876 
2877 /**
2878  * ixgbevf_watchdog_update_link - update the link status
2879  * @adapter: pointer to the device adapter structure
2880  **/
2881 static void ixgbevf_watchdog_update_link(struct ixgbevf_adapter *adapter)
2882 {
2883 	struct ixgbe_hw *hw = &adapter->hw;
2884 	u32 link_speed = adapter->link_speed;
2885 	bool link_up = adapter->link_up;
2886 	s32 err;
2887 
2888 	spin_lock_bh(&adapter->mbx_lock);
2889 
2890 	err = hw->mac.ops.check_link(hw, &link_speed, &link_up, false);
2891 
2892 	spin_unlock_bh(&adapter->mbx_lock);
2893 
2894 	/* if check for link returns error we will need to reset */
2895 	if (err && time_after(jiffies, adapter->last_reset + (10 * HZ))) {
2896 		set_bit(__IXGBEVF_RESET_REQUESTED, &adapter->state);
2897 		link_up = false;
2898 	}
2899 
2900 	adapter->link_up = link_up;
2901 	adapter->link_speed = link_speed;
2902 }
2903 
2904 /**
2905  * ixgbevf_watchdog_link_is_up - update netif_carrier status and
2906  *				 print link up message
2907  * @adapter: pointer to the device adapter structure
2908  **/
2909 static void ixgbevf_watchdog_link_is_up(struct ixgbevf_adapter *adapter)
2910 {
2911 	struct net_device *netdev = adapter->netdev;
2912 
2913 	/* only continue if link was previously down */
2914 	if (netif_carrier_ok(netdev))
2915 		return;
2916 
2917 	dev_info(&adapter->pdev->dev, "NIC Link is Up %s\n",
2918 		 (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
2919 		 "10 Gbps" :
2920 		 (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL) ?
2921 		 "1 Gbps" :
2922 		 (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL) ?
2923 		 "100 Mbps" :
2924 		 "unknown speed");
2925 
2926 	netif_carrier_on(netdev);
2927 }
2928 
2929 /**
2930  * ixgbevf_watchdog_link_is_down - update netif_carrier status and
2931  *				   print link down message
2932  * @adapter: pointer to the adapter structure
2933  **/
2934 static void ixgbevf_watchdog_link_is_down(struct ixgbevf_adapter *adapter)
2935 {
2936 	struct net_device *netdev = adapter->netdev;
2937 
2938 	adapter->link_speed = 0;
2939 
2940 	/* only continue if link was up previously */
2941 	if (!netif_carrier_ok(netdev))
2942 		return;
2943 
2944 	dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
2945 
2946 	netif_carrier_off(netdev);
2947 }
2948 
2949 /**
2950  * ixgbevf_watchdog_subtask - worker thread to bring link up
2951  * @adapter: board private structure
2952  **/
2953 static void ixgbevf_watchdog_subtask(struct ixgbevf_adapter *adapter)
2954 {
2955 	/* if interface is down do nothing */
2956 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
2957 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
2958 		return;
2959 
2960 	ixgbevf_watchdog_update_link(adapter);
2961 
2962 	if (adapter->link_up)
2963 		ixgbevf_watchdog_link_is_up(adapter);
2964 	else
2965 		ixgbevf_watchdog_link_is_down(adapter);
2966 
2967 	ixgbevf_update_stats(adapter);
2968 }
2969 
2970 /**
2971  * ixgbevf_service_task - manages and runs subtasks
2972  * @work: pointer to work_struct containing our data
2973  **/
2974 static void ixgbevf_service_task(struct work_struct *work)
2975 {
2976 	struct ixgbevf_adapter *adapter = container_of(work,
2977 						       struct ixgbevf_adapter,
2978 						       service_task);
2979 	struct ixgbe_hw *hw = &adapter->hw;
2980 
2981 	if (IXGBE_REMOVED(hw->hw_addr)) {
2982 		if (!test_bit(__IXGBEVF_DOWN, &adapter->state)) {
2983 			rtnl_lock();
2984 			ixgbevf_down(adapter);
2985 			rtnl_unlock();
2986 		}
2987 		return;
2988 	}
2989 
2990 	ixgbevf_queue_reset_subtask(adapter);
2991 	ixgbevf_reset_subtask(adapter);
2992 	ixgbevf_watchdog_subtask(adapter);
2993 	ixgbevf_check_hang_subtask(adapter);
2994 
2995 	ixgbevf_service_event_complete(adapter);
2996 }
2997 
2998 /**
2999  * ixgbevf_free_tx_resources - Free Tx Resources per Queue
3000  * @tx_ring: Tx descriptor ring for a specific queue
3001  *
3002  * Free all transmit software resources
3003  **/
3004 void ixgbevf_free_tx_resources(struct ixgbevf_ring *tx_ring)
3005 {
3006 	ixgbevf_clean_tx_ring(tx_ring);
3007 
3008 	vfree(tx_ring->tx_buffer_info);
3009 	tx_ring->tx_buffer_info = NULL;
3010 
3011 	/* if not set, then don't free */
3012 	if (!tx_ring->desc)
3013 		return;
3014 
3015 	dma_free_coherent(tx_ring->dev, tx_ring->size, tx_ring->desc,
3016 			  tx_ring->dma);
3017 
3018 	tx_ring->desc = NULL;
3019 }
3020 
3021 /**
3022  * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
3023  * @adapter: board private structure
3024  *
3025  * Free all transmit software resources
3026  **/
3027 static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
3028 {
3029 	int i;
3030 
3031 	for (i = 0; i < adapter->num_tx_queues; i++)
3032 		if (adapter->tx_ring[i]->desc)
3033 			ixgbevf_free_tx_resources(adapter->tx_ring[i]);
3034 }
3035 
3036 /**
3037  * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
3038  * @tx_ring: Tx descriptor ring (for a specific queue) to setup
3039  *
3040  * Return 0 on success, negative on failure
3041  **/
3042 int ixgbevf_setup_tx_resources(struct ixgbevf_ring *tx_ring)
3043 {
3044 	struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
3045 	int size;
3046 
3047 	size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
3048 	tx_ring->tx_buffer_info = vmalloc(size);
3049 	if (!tx_ring->tx_buffer_info)
3050 		goto err;
3051 
3052 	u64_stats_init(&tx_ring->syncp);
3053 
3054 	/* round up to nearest 4K */
3055 	tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
3056 	tx_ring->size = ALIGN(tx_ring->size, 4096);
3057 
3058 	tx_ring->desc = dma_alloc_coherent(tx_ring->dev, tx_ring->size,
3059 					   &tx_ring->dma, GFP_KERNEL);
3060 	if (!tx_ring->desc)
3061 		goto err;
3062 
3063 	return 0;
3064 
3065 err:
3066 	vfree(tx_ring->tx_buffer_info);
3067 	tx_ring->tx_buffer_info = NULL;
3068 	hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit descriptor ring\n");
3069 	return -ENOMEM;
3070 }
3071 
3072 /**
3073  * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
3074  * @adapter: board private structure
3075  *
3076  * If this function returns with an error, then it's possible one or
3077  * more of the rings is populated (while the rest are not).  It is the
3078  * callers duty to clean those orphaned rings.
3079  *
3080  * Return 0 on success, negative on failure
3081  **/
3082 static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
3083 {
3084 	int i, err = 0;
3085 
3086 	for (i = 0; i < adapter->num_tx_queues; i++) {
3087 		err = ixgbevf_setup_tx_resources(adapter->tx_ring[i]);
3088 		if (!err)
3089 			continue;
3090 		hw_dbg(&adapter->hw, "Allocation for Tx Queue %u failed\n", i);
3091 		break;
3092 	}
3093 
3094 	return err;
3095 }
3096 
3097 /**
3098  * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
3099  * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3100  *
3101  * Returns 0 on success, negative on failure
3102  **/
3103 int ixgbevf_setup_rx_resources(struct ixgbevf_ring *rx_ring)
3104 {
3105 	int size;
3106 
3107 	size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
3108 	rx_ring->rx_buffer_info = vmalloc(size);
3109 	if (!rx_ring->rx_buffer_info)
3110 		goto err;
3111 
3112 	u64_stats_init(&rx_ring->syncp);
3113 
3114 	/* Round up to nearest 4K */
3115 	rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
3116 	rx_ring->size = ALIGN(rx_ring->size, 4096);
3117 
3118 	rx_ring->desc = dma_alloc_coherent(rx_ring->dev, rx_ring->size,
3119 					   &rx_ring->dma, GFP_KERNEL);
3120 
3121 	if (!rx_ring->desc)
3122 		goto err;
3123 
3124 	return 0;
3125 err:
3126 	vfree(rx_ring->rx_buffer_info);
3127 	rx_ring->rx_buffer_info = NULL;
3128 	dev_err(rx_ring->dev, "Unable to allocate memory for the Rx descriptor ring\n");
3129 	return -ENOMEM;
3130 }
3131 
3132 /**
3133  * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
3134  * @adapter: board private structure
3135  *
3136  * If this function returns with an error, then it's possible one or
3137  * more of the rings is populated (while the rest are not).  It is the
3138  * callers duty to clean those orphaned rings.
3139  *
3140  * Return 0 on success, negative on failure
3141  **/
3142 static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
3143 {
3144 	int i, err = 0;
3145 
3146 	for (i = 0; i < adapter->num_rx_queues; i++) {
3147 		err = ixgbevf_setup_rx_resources(adapter->rx_ring[i]);
3148 		if (!err)
3149 			continue;
3150 		hw_dbg(&adapter->hw, "Allocation for Rx Queue %u failed\n", i);
3151 		break;
3152 	}
3153 	return err;
3154 }
3155 
3156 /**
3157  * ixgbevf_free_rx_resources - Free Rx Resources
3158  * @rx_ring: ring to clean the resources from
3159  *
3160  * Free all receive software resources
3161  **/
3162 void ixgbevf_free_rx_resources(struct ixgbevf_ring *rx_ring)
3163 {
3164 	ixgbevf_clean_rx_ring(rx_ring);
3165 
3166 	vfree(rx_ring->rx_buffer_info);
3167 	rx_ring->rx_buffer_info = NULL;
3168 
3169 	dma_free_coherent(rx_ring->dev, rx_ring->size, rx_ring->desc,
3170 			  rx_ring->dma);
3171 
3172 	rx_ring->desc = NULL;
3173 }
3174 
3175 /**
3176  * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
3177  * @adapter: board private structure
3178  *
3179  * Free all receive software resources
3180  **/
3181 static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
3182 {
3183 	int i;
3184 
3185 	for (i = 0; i < adapter->num_rx_queues; i++)
3186 		if (adapter->rx_ring[i]->desc)
3187 			ixgbevf_free_rx_resources(adapter->rx_ring[i]);
3188 }
3189 
3190 /**
3191  * ixgbevf_open - Called when a network interface is made active
3192  * @netdev: network interface device structure
3193  *
3194  * Returns 0 on success, negative value on failure
3195  *
3196  * The open entry point is called when a network interface is made
3197  * active by the system (IFF_UP).  At this point all resources needed
3198  * for transmit and receive operations are allocated, the interrupt
3199  * handler is registered with the OS, the watchdog timer is started,
3200  * and the stack is notified that the interface is ready.
3201  **/
3202 int ixgbevf_open(struct net_device *netdev)
3203 {
3204 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3205 	struct ixgbe_hw *hw = &adapter->hw;
3206 	int err;
3207 
3208 	/* A previous failure to open the device because of a lack of
3209 	 * available MSIX vector resources may have reset the number
3210 	 * of msix vectors variable to zero.  The only way to recover
3211 	 * is to unload/reload the driver and hope that the system has
3212 	 * been able to recover some MSIX vector resources.
3213 	 */
3214 	if (!adapter->num_msix_vectors)
3215 		return -ENOMEM;
3216 
3217 	if (hw->adapter_stopped) {
3218 		ixgbevf_reset(adapter);
3219 		/* if adapter is still stopped then PF isn't up and
3220 		 * the VF can't start.
3221 		 */
3222 		if (hw->adapter_stopped) {
3223 			err = IXGBE_ERR_MBX;
3224 			pr_err("Unable to start - perhaps the PF Driver isn't up yet\n");
3225 			goto err_setup_reset;
3226 		}
3227 	}
3228 
3229 	/* disallow open during test */
3230 	if (test_bit(__IXGBEVF_TESTING, &adapter->state))
3231 		return -EBUSY;
3232 
3233 	netif_carrier_off(netdev);
3234 
3235 	/* allocate transmit descriptors */
3236 	err = ixgbevf_setup_all_tx_resources(adapter);
3237 	if (err)
3238 		goto err_setup_tx;
3239 
3240 	/* allocate receive descriptors */
3241 	err = ixgbevf_setup_all_rx_resources(adapter);
3242 	if (err)
3243 		goto err_setup_rx;
3244 
3245 	ixgbevf_configure(adapter);
3246 
3247 	/* Map the Tx/Rx rings to the vectors we were allotted.
3248 	 * if request_irq will be called in this function map_rings
3249 	 * must be called *before* up_complete
3250 	 */
3251 	ixgbevf_map_rings_to_vectors(adapter);
3252 
3253 	err = ixgbevf_request_irq(adapter);
3254 	if (err)
3255 		goto err_req_irq;
3256 
3257 	ixgbevf_up_complete(adapter);
3258 
3259 	return 0;
3260 
3261 err_req_irq:
3262 	ixgbevf_down(adapter);
3263 err_setup_rx:
3264 	ixgbevf_free_all_rx_resources(adapter);
3265 err_setup_tx:
3266 	ixgbevf_free_all_tx_resources(adapter);
3267 	ixgbevf_reset(adapter);
3268 
3269 err_setup_reset:
3270 
3271 	return err;
3272 }
3273 
3274 /**
3275  * ixgbevf_close_suspend - actions necessary to both suspend and close flows
3276  * @adapter: the private adapter struct
3277  *
3278  * This function should contain the necessary work common to both suspending
3279  * and closing of the device.
3280  */
3281 static void ixgbevf_close_suspend(struct ixgbevf_adapter *adapter)
3282 {
3283 	ixgbevf_down(adapter);
3284 	ixgbevf_free_irq(adapter);
3285 	ixgbevf_free_all_tx_resources(adapter);
3286 	ixgbevf_free_all_rx_resources(adapter);
3287 }
3288 
3289 /**
3290  * ixgbevf_close - Disables a network interface
3291  * @netdev: network interface device structure
3292  *
3293  * Returns 0, this is not allowed to fail
3294  *
3295  * The close entry point is called when an interface is de-activated
3296  * by the OS.  The hardware is still under the drivers control, but
3297  * needs to be disabled.  A global MAC reset is issued to stop the
3298  * hardware, and all transmit and receive resources are freed.
3299  **/
3300 int ixgbevf_close(struct net_device *netdev)
3301 {
3302 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3303 
3304 	if (netif_device_present(netdev))
3305 		ixgbevf_close_suspend(adapter);
3306 
3307 	return 0;
3308 }
3309 
3310 static void ixgbevf_queue_reset_subtask(struct ixgbevf_adapter *adapter)
3311 {
3312 	struct net_device *dev = adapter->netdev;
3313 
3314 	if (!test_and_clear_bit(__IXGBEVF_QUEUE_RESET_REQUESTED,
3315 				&adapter->state))
3316 		return;
3317 
3318 	/* if interface is down do nothing */
3319 	if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
3320 	    test_bit(__IXGBEVF_RESETTING, &adapter->state))
3321 		return;
3322 
3323 	/* Hardware has to reinitialize queues and interrupts to
3324 	 * match packet buffer alignment. Unfortunately, the
3325 	 * hardware is not flexible enough to do this dynamically.
3326 	 */
3327 	rtnl_lock();
3328 
3329 	if (netif_running(dev))
3330 		ixgbevf_close(dev);
3331 
3332 	ixgbevf_clear_interrupt_scheme(adapter);
3333 	ixgbevf_init_interrupt_scheme(adapter);
3334 
3335 	if (netif_running(dev))
3336 		ixgbevf_open(dev);
3337 
3338 	rtnl_unlock();
3339 }
3340 
3341 static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
3342 				u32 vlan_macip_lens, u32 type_tucmd,
3343 				u32 mss_l4len_idx)
3344 {
3345 	struct ixgbe_adv_tx_context_desc *context_desc;
3346 	u16 i = tx_ring->next_to_use;
3347 
3348 	context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
3349 
3350 	i++;
3351 	tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3352 
3353 	/* set bits to identify this as an advanced context descriptor */
3354 	type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
3355 
3356 	context_desc->vlan_macip_lens	= cpu_to_le32(vlan_macip_lens);
3357 	context_desc->seqnum_seed	= 0;
3358 	context_desc->type_tucmd_mlhl	= cpu_to_le32(type_tucmd);
3359 	context_desc->mss_l4len_idx	= cpu_to_le32(mss_l4len_idx);
3360 }
3361 
3362 static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
3363 		       struct ixgbevf_tx_buffer *first,
3364 		       u8 *hdr_len)
3365 {
3366 	u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
3367 	struct sk_buff *skb = first->skb;
3368 	union {
3369 		struct iphdr *v4;
3370 		struct ipv6hdr *v6;
3371 		unsigned char *hdr;
3372 	} ip;
3373 	union {
3374 		struct tcphdr *tcp;
3375 		unsigned char *hdr;
3376 	} l4;
3377 	u32 paylen, l4_offset;
3378 	int err;
3379 
3380 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3381 		return 0;
3382 
3383 	if (!skb_is_gso(skb))
3384 		return 0;
3385 
3386 	err = skb_cow_head(skb, 0);
3387 	if (err < 0)
3388 		return err;
3389 
3390 	if (eth_p_mpls(first->protocol))
3391 		ip.hdr = skb_inner_network_header(skb);
3392 	else
3393 		ip.hdr = skb_network_header(skb);
3394 	l4.hdr = skb_checksum_start(skb);
3395 
3396 	/* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
3397 	type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3398 
3399 	/* initialize outer IP header fields */
3400 	if (ip.v4->version == 4) {
3401 		unsigned char *csum_start = skb_checksum_start(skb);
3402 		unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
3403 
3404 		/* IP header will have to cancel out any data that
3405 		 * is not a part of the outer IP header
3406 		 */
3407 		ip.v4->check = csum_fold(csum_partial(trans_start,
3408 						      csum_start - trans_start,
3409 						      0));
3410 		type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
3411 
3412 		ip.v4->tot_len = 0;
3413 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3414 				   IXGBE_TX_FLAGS_CSUM |
3415 				   IXGBE_TX_FLAGS_IPV4;
3416 	} else {
3417 		ip.v6->payload_len = 0;
3418 		first->tx_flags |= IXGBE_TX_FLAGS_TSO |
3419 				   IXGBE_TX_FLAGS_CSUM;
3420 	}
3421 
3422 	/* determine offset of inner transport header */
3423 	l4_offset = l4.hdr - skb->data;
3424 
3425 	/* compute length of segmentation header */
3426 	*hdr_len = (l4.tcp->doff * 4) + l4_offset;
3427 
3428 	/* remove payload length from inner checksum */
3429 	paylen = skb->len - l4_offset;
3430 	csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
3431 
3432 	/* update gso size and bytecount with header size */
3433 	first->gso_segs = skb_shinfo(skb)->gso_segs;
3434 	first->bytecount += (first->gso_segs - 1) * *hdr_len;
3435 
3436 	/* mss_l4len_id: use 1 as index for TSO */
3437 	mss_l4len_idx = (*hdr_len - l4_offset) << IXGBE_ADVTXD_L4LEN_SHIFT;
3438 	mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
3439 	mss_l4len_idx |= (1u << IXGBE_ADVTXD_IDX_SHIFT);
3440 
3441 	/* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
3442 	vlan_macip_lens = l4.hdr - ip.hdr;
3443 	vlan_macip_lens |= (ip.hdr - skb->data) << IXGBE_ADVTXD_MACLEN_SHIFT;
3444 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3445 
3446 	ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
3447 			    type_tucmd, mss_l4len_idx);
3448 
3449 	return 1;
3450 }
3451 
3452 static inline bool ixgbevf_ipv6_csum_is_sctp(struct sk_buff *skb)
3453 {
3454 	unsigned int offset = 0;
3455 
3456 	ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
3457 
3458 	return offset == skb_checksum_start_offset(skb);
3459 }
3460 
3461 static void ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
3462 			    struct ixgbevf_tx_buffer *first)
3463 {
3464 	struct sk_buff *skb = first->skb;
3465 	u32 vlan_macip_lens = 0;
3466 	u32 type_tucmd = 0;
3467 
3468 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3469 		goto no_csum;
3470 
3471 	switch (skb->csum_offset) {
3472 	case offsetof(struct tcphdr, check):
3473 		type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
3474 		/* fall through */
3475 	case offsetof(struct udphdr, check):
3476 		break;
3477 	case offsetof(struct sctphdr, checksum):
3478 		/* validate that this is actually an SCTP request */
3479 		if (((first->protocol == htons(ETH_P_IP)) &&
3480 		     (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
3481 		    ((first->protocol == htons(ETH_P_IPV6)) &&
3482 		     ixgbevf_ipv6_csum_is_sctp(skb))) {
3483 			type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_SCTP;
3484 			break;
3485 		}
3486 		/* fall through */
3487 	default:
3488 		skb_checksum_help(skb);
3489 		goto no_csum;
3490 	}
3491 	/* update TX checksum flag */
3492 	first->tx_flags |= IXGBE_TX_FLAGS_CSUM;
3493 	vlan_macip_lens = skb_checksum_start_offset(skb) -
3494 			  skb_network_offset(skb);
3495 no_csum:
3496 	/* vlan_macip_lens: MACLEN, VLAN tag */
3497 	vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
3498 	vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
3499 
3500 	ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
3501 }
3502 
3503 static __le32 ixgbevf_tx_cmd_type(u32 tx_flags)
3504 {
3505 	/* set type for advanced descriptor with frame checksum insertion */
3506 	__le32 cmd_type = cpu_to_le32(IXGBE_ADVTXD_DTYP_DATA |
3507 				      IXGBE_ADVTXD_DCMD_IFCS |
3508 				      IXGBE_ADVTXD_DCMD_DEXT);
3509 
3510 	/* set HW VLAN bit if VLAN is present */
3511 	if (tx_flags & IXGBE_TX_FLAGS_VLAN)
3512 		cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_VLE);
3513 
3514 	/* set segmentation enable bits for TSO/FSO */
3515 	if (tx_flags & IXGBE_TX_FLAGS_TSO)
3516 		cmd_type |= cpu_to_le32(IXGBE_ADVTXD_DCMD_TSE);
3517 
3518 	return cmd_type;
3519 }
3520 
3521 static void ixgbevf_tx_olinfo_status(union ixgbe_adv_tx_desc *tx_desc,
3522 				     u32 tx_flags, unsigned int paylen)
3523 {
3524 	__le32 olinfo_status = cpu_to_le32(paylen << IXGBE_ADVTXD_PAYLEN_SHIFT);
3525 
3526 	/* enable L4 checksum for TSO and TX checksum offload */
3527 	if (tx_flags & IXGBE_TX_FLAGS_CSUM)
3528 		olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_TXSM);
3529 
3530 	/* enble IPv4 checksum for TSO */
3531 	if (tx_flags & IXGBE_TX_FLAGS_IPV4)
3532 		olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_POPTS_IXSM);
3533 
3534 	/* use index 1 context for TSO/FSO/FCOE */
3535 	if (tx_flags & IXGBE_TX_FLAGS_TSO)
3536 		olinfo_status |= cpu_to_le32(1u << IXGBE_ADVTXD_IDX_SHIFT);
3537 
3538 	/* Check Context must be set if Tx switch is enabled, which it
3539 	 * always is for case where virtual functions are running
3540 	 */
3541 	olinfo_status |= cpu_to_le32(IXGBE_ADVTXD_CC);
3542 
3543 	tx_desc->read.olinfo_status = olinfo_status;
3544 }
3545 
3546 static void ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
3547 			   struct ixgbevf_tx_buffer *first,
3548 			   const u8 hdr_len)
3549 {
3550 	struct sk_buff *skb = first->skb;
3551 	struct ixgbevf_tx_buffer *tx_buffer;
3552 	union ixgbe_adv_tx_desc *tx_desc;
3553 	struct skb_frag_struct *frag;
3554 	dma_addr_t dma;
3555 	unsigned int data_len, size;
3556 	u32 tx_flags = first->tx_flags;
3557 	__le32 cmd_type = ixgbevf_tx_cmd_type(tx_flags);
3558 	u16 i = tx_ring->next_to_use;
3559 
3560 	tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
3561 
3562 	ixgbevf_tx_olinfo_status(tx_desc, tx_flags, skb->len - hdr_len);
3563 
3564 	size = skb_headlen(skb);
3565 	data_len = skb->data_len;
3566 
3567 	dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
3568 
3569 	tx_buffer = first;
3570 
3571 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
3572 		if (dma_mapping_error(tx_ring->dev, dma))
3573 			goto dma_error;
3574 
3575 		/* record length, and DMA address */
3576 		dma_unmap_len_set(tx_buffer, len, size);
3577 		dma_unmap_addr_set(tx_buffer, dma, dma);
3578 
3579 		tx_desc->read.buffer_addr = cpu_to_le64(dma);
3580 
3581 		while (unlikely(size > IXGBE_MAX_DATA_PER_TXD)) {
3582 			tx_desc->read.cmd_type_len =
3583 				cmd_type | cpu_to_le32(IXGBE_MAX_DATA_PER_TXD);
3584 
3585 			i++;
3586 			tx_desc++;
3587 			if (i == tx_ring->count) {
3588 				tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3589 				i = 0;
3590 			}
3591 			tx_desc->read.olinfo_status = 0;
3592 
3593 			dma += IXGBE_MAX_DATA_PER_TXD;
3594 			size -= IXGBE_MAX_DATA_PER_TXD;
3595 
3596 			tx_desc->read.buffer_addr = cpu_to_le64(dma);
3597 		}
3598 
3599 		if (likely(!data_len))
3600 			break;
3601 
3602 		tx_desc->read.cmd_type_len = cmd_type | cpu_to_le32(size);
3603 
3604 		i++;
3605 		tx_desc++;
3606 		if (i == tx_ring->count) {
3607 			tx_desc = IXGBEVF_TX_DESC(tx_ring, 0);
3608 			i = 0;
3609 		}
3610 		tx_desc->read.olinfo_status = 0;
3611 
3612 		size = skb_frag_size(frag);
3613 		data_len -= size;
3614 
3615 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
3616 				       DMA_TO_DEVICE);
3617 
3618 		tx_buffer = &tx_ring->tx_buffer_info[i];
3619 	}
3620 
3621 	/* write last descriptor with RS and EOP bits */
3622 	cmd_type |= cpu_to_le32(size) | cpu_to_le32(IXGBE_TXD_CMD);
3623 	tx_desc->read.cmd_type_len = cmd_type;
3624 
3625 	/* set the timestamp */
3626 	first->time_stamp = jiffies;
3627 
3628 	/* Force memory writes to complete before letting h/w know there
3629 	 * are new descriptors to fetch.  (Only applicable for weak-ordered
3630 	 * memory model archs, such as IA-64).
3631 	 *
3632 	 * We also need this memory barrier (wmb) to make certain all of the
3633 	 * status bits have been updated before next_to_watch is written.
3634 	 */
3635 	wmb();
3636 
3637 	/* set next_to_watch value indicating a packet is present */
3638 	first->next_to_watch = tx_desc;
3639 
3640 	i++;
3641 	if (i == tx_ring->count)
3642 		i = 0;
3643 
3644 	tx_ring->next_to_use = i;
3645 
3646 	/* notify HW of packet */
3647 	ixgbevf_write_tail(tx_ring, i);
3648 
3649 	return;
3650 dma_error:
3651 	dev_err(tx_ring->dev, "TX DMA map failed\n");
3652 	tx_buffer = &tx_ring->tx_buffer_info[i];
3653 
3654 	/* clear dma mappings for failed tx_buffer_info map */
3655 	while (tx_buffer != first) {
3656 		if (dma_unmap_len(tx_buffer, len))
3657 			dma_unmap_page(tx_ring->dev,
3658 				       dma_unmap_addr(tx_buffer, dma),
3659 				       dma_unmap_len(tx_buffer, len),
3660 				       DMA_TO_DEVICE);
3661 		dma_unmap_len_set(tx_buffer, len, 0);
3662 
3663 		if (i-- == 0)
3664 			i += tx_ring->count;
3665 		tx_buffer = &tx_ring->tx_buffer_info[i];
3666 	}
3667 
3668 	if (dma_unmap_len(tx_buffer, len))
3669 		dma_unmap_single(tx_ring->dev,
3670 				 dma_unmap_addr(tx_buffer, dma),
3671 				 dma_unmap_len(tx_buffer, len),
3672 				 DMA_TO_DEVICE);
3673 	dma_unmap_len_set(tx_buffer, len, 0);
3674 
3675 	dev_kfree_skb_any(tx_buffer->skb);
3676 	tx_buffer->skb = NULL;
3677 
3678 	tx_ring->next_to_use = i;
3679 }
3680 
3681 static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3682 {
3683 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
3684 	/* Herbert's original patch had:
3685 	 *  smp_mb__after_netif_stop_queue();
3686 	 * but since that doesn't exist yet, just open code it.
3687 	 */
3688 	smp_mb();
3689 
3690 	/* We need to check again in a case another CPU has just
3691 	 * made room available.
3692 	 */
3693 	if (likely(ixgbevf_desc_unused(tx_ring) < size))
3694 		return -EBUSY;
3695 
3696 	/* A reprieve! - use start_queue because it doesn't call schedule */
3697 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
3698 	++tx_ring->tx_stats.restart_queue;
3699 
3700 	return 0;
3701 }
3702 
3703 static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
3704 {
3705 	if (likely(ixgbevf_desc_unused(tx_ring) >= size))
3706 		return 0;
3707 	return __ixgbevf_maybe_stop_tx(tx_ring, size);
3708 }
3709 
3710 static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
3711 {
3712 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3713 	struct ixgbevf_tx_buffer *first;
3714 	struct ixgbevf_ring *tx_ring;
3715 	int tso;
3716 	u32 tx_flags = 0;
3717 	u16 count = TXD_USE_COUNT(skb_headlen(skb));
3718 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3719 	unsigned short f;
3720 #endif
3721 	u8 hdr_len = 0;
3722 	u8 *dst_mac = skb_header_pointer(skb, 0, 0, NULL);
3723 
3724 	if (!dst_mac || is_link_local_ether_addr(dst_mac)) {
3725 		dev_kfree_skb_any(skb);
3726 		return NETDEV_TX_OK;
3727 	}
3728 
3729 	tx_ring = adapter->tx_ring[skb->queue_mapping];
3730 
3731 	/* need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
3732 	 *       + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
3733 	 *       + 2 desc gap to keep tail from touching head,
3734 	 *       + 1 desc for context descriptor,
3735 	 * otherwise try next time
3736 	 */
3737 #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
3738 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
3739 		count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
3740 #else
3741 	count += skb_shinfo(skb)->nr_frags;
3742 #endif
3743 	if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
3744 		tx_ring->tx_stats.tx_busy++;
3745 		return NETDEV_TX_BUSY;
3746 	}
3747 
3748 	/* record the location of the first descriptor for this packet */
3749 	first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
3750 	first->skb = skb;
3751 	first->bytecount = skb->len;
3752 	first->gso_segs = 1;
3753 
3754 	if (skb_vlan_tag_present(skb)) {
3755 		tx_flags |= skb_vlan_tag_get(skb);
3756 		tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
3757 		tx_flags |= IXGBE_TX_FLAGS_VLAN;
3758 	}
3759 
3760 	/* record initial flags and protocol */
3761 	first->tx_flags = tx_flags;
3762 	first->protocol = vlan_get_protocol(skb);
3763 
3764 	tso = ixgbevf_tso(tx_ring, first, &hdr_len);
3765 	if (tso < 0)
3766 		goto out_drop;
3767 	else if (!tso)
3768 		ixgbevf_tx_csum(tx_ring, first);
3769 
3770 	ixgbevf_tx_map(tx_ring, first, hdr_len);
3771 
3772 	ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
3773 
3774 	return NETDEV_TX_OK;
3775 
3776 out_drop:
3777 	dev_kfree_skb_any(first->skb);
3778 	first->skb = NULL;
3779 
3780 	return NETDEV_TX_OK;
3781 }
3782 
3783 /**
3784  * ixgbevf_set_mac - Change the Ethernet Address of the NIC
3785  * @netdev: network interface device structure
3786  * @p: pointer to an address structure
3787  *
3788  * Returns 0 on success, negative on failure
3789  **/
3790 static int ixgbevf_set_mac(struct net_device *netdev, void *p)
3791 {
3792 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3793 	struct ixgbe_hw *hw = &adapter->hw;
3794 	struct sockaddr *addr = p;
3795 	int err;
3796 
3797 	if (!is_valid_ether_addr(addr->sa_data))
3798 		return -EADDRNOTAVAIL;
3799 
3800 	spin_lock_bh(&adapter->mbx_lock);
3801 
3802 	err = hw->mac.ops.set_rar(hw, 0, addr->sa_data, 0);
3803 
3804 	spin_unlock_bh(&adapter->mbx_lock);
3805 
3806 	if (err)
3807 		return -EPERM;
3808 
3809 	ether_addr_copy(hw->mac.addr, addr->sa_data);
3810 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
3811 
3812 	return 0;
3813 }
3814 
3815 /**
3816  * ixgbevf_change_mtu - Change the Maximum Transfer Unit
3817  * @netdev: network interface device structure
3818  * @new_mtu: new value for maximum frame size
3819  *
3820  * Returns 0 on success, negative on failure
3821  **/
3822 static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
3823 {
3824 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3825 	struct ixgbe_hw *hw = &adapter->hw;
3826 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
3827 	int ret;
3828 
3829 	spin_lock_bh(&adapter->mbx_lock);
3830 	/* notify the PF of our intent to use this size of frame */
3831 	ret = hw->mac.ops.set_rlpml(hw, max_frame);
3832 	spin_unlock_bh(&adapter->mbx_lock);
3833 	if (ret)
3834 		return -EINVAL;
3835 
3836 	hw_dbg(hw, "changing MTU from %d to %d\n",
3837 	       netdev->mtu, new_mtu);
3838 
3839 	/* must set new MTU before calling down or up */
3840 	netdev->mtu = new_mtu;
3841 
3842 	return 0;
3843 }
3844 
3845 #ifdef CONFIG_NET_POLL_CONTROLLER
3846 /* Polling 'interrupt' - used by things like netconsole to send skbs
3847  * without having to re-enable interrupts. It's not called while
3848  * the interrupt routine is executing.
3849  */
3850 static void ixgbevf_netpoll(struct net_device *netdev)
3851 {
3852 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3853 	int i;
3854 
3855 	/* if interface is down do nothing */
3856 	if (test_bit(__IXGBEVF_DOWN, &adapter->state))
3857 		return;
3858 	for (i = 0; i < adapter->num_rx_queues; i++)
3859 		ixgbevf_msix_clean_rings(0, adapter->q_vector[i]);
3860 }
3861 #endif /* CONFIG_NET_POLL_CONTROLLER */
3862 
3863 static int ixgbevf_suspend(struct pci_dev *pdev, pm_message_t state)
3864 {
3865 	struct net_device *netdev = pci_get_drvdata(pdev);
3866 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3867 #ifdef CONFIG_PM
3868 	int retval = 0;
3869 #endif
3870 
3871 	rtnl_lock();
3872 	netif_device_detach(netdev);
3873 
3874 	if (netif_running(netdev))
3875 		ixgbevf_close_suspend(adapter);
3876 
3877 	ixgbevf_clear_interrupt_scheme(adapter);
3878 	rtnl_unlock();
3879 
3880 #ifdef CONFIG_PM
3881 	retval = pci_save_state(pdev);
3882 	if (retval)
3883 		return retval;
3884 
3885 #endif
3886 	if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
3887 		pci_disable_device(pdev);
3888 
3889 	return 0;
3890 }
3891 
3892 #ifdef CONFIG_PM
3893 static int ixgbevf_resume(struct pci_dev *pdev)
3894 {
3895 	struct net_device *netdev = pci_get_drvdata(pdev);
3896 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3897 	u32 err;
3898 
3899 	pci_restore_state(pdev);
3900 	/* pci_restore_state clears dev->state_saved so call
3901 	 * pci_save_state to restore it.
3902 	 */
3903 	pci_save_state(pdev);
3904 
3905 	err = pci_enable_device_mem(pdev);
3906 	if (err) {
3907 		dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
3908 		return err;
3909 	}
3910 
3911 	adapter->hw.hw_addr = adapter->io_addr;
3912 	smp_mb__before_atomic();
3913 	clear_bit(__IXGBEVF_DISABLED, &adapter->state);
3914 	pci_set_master(pdev);
3915 
3916 	ixgbevf_reset(adapter);
3917 
3918 	rtnl_lock();
3919 	err = ixgbevf_init_interrupt_scheme(adapter);
3920 	rtnl_unlock();
3921 	if (err) {
3922 		dev_err(&pdev->dev, "Cannot initialize interrupts\n");
3923 		return err;
3924 	}
3925 
3926 	if (netif_running(netdev)) {
3927 		err = ixgbevf_open(netdev);
3928 		if (err)
3929 			return err;
3930 	}
3931 
3932 	netif_device_attach(netdev);
3933 
3934 	return err;
3935 }
3936 
3937 #endif /* CONFIG_PM */
3938 static void ixgbevf_shutdown(struct pci_dev *pdev)
3939 {
3940 	ixgbevf_suspend(pdev, PMSG_SUSPEND);
3941 }
3942 
3943 static void ixgbevf_get_stats(struct net_device *netdev,
3944 			      struct rtnl_link_stats64 *stats)
3945 {
3946 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
3947 	unsigned int start;
3948 	u64 bytes, packets;
3949 	const struct ixgbevf_ring *ring;
3950 	int i;
3951 
3952 	ixgbevf_update_stats(adapter);
3953 
3954 	stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
3955 
3956 	for (i = 0; i < adapter->num_rx_queues; i++) {
3957 		ring = adapter->rx_ring[i];
3958 		do {
3959 			start = u64_stats_fetch_begin_irq(&ring->syncp);
3960 			bytes = ring->stats.bytes;
3961 			packets = ring->stats.packets;
3962 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3963 		stats->rx_bytes += bytes;
3964 		stats->rx_packets += packets;
3965 	}
3966 
3967 	for (i = 0; i < adapter->num_tx_queues; i++) {
3968 		ring = adapter->tx_ring[i];
3969 		do {
3970 			start = u64_stats_fetch_begin_irq(&ring->syncp);
3971 			bytes = ring->stats.bytes;
3972 			packets = ring->stats.packets;
3973 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
3974 		stats->tx_bytes += bytes;
3975 		stats->tx_packets += packets;
3976 	}
3977 }
3978 
3979 #define IXGBEVF_MAX_MAC_HDR_LEN		127
3980 #define IXGBEVF_MAX_NETWORK_HDR_LEN	511
3981 
3982 static netdev_features_t
3983 ixgbevf_features_check(struct sk_buff *skb, struct net_device *dev,
3984 		       netdev_features_t features)
3985 {
3986 	unsigned int network_hdr_len, mac_hdr_len;
3987 
3988 	/* Make certain the headers can be described by a context descriptor */
3989 	mac_hdr_len = skb_network_header(skb) - skb->data;
3990 	if (unlikely(mac_hdr_len > IXGBEVF_MAX_MAC_HDR_LEN))
3991 		return features & ~(NETIF_F_HW_CSUM |
3992 				    NETIF_F_SCTP_CRC |
3993 				    NETIF_F_HW_VLAN_CTAG_TX |
3994 				    NETIF_F_TSO |
3995 				    NETIF_F_TSO6);
3996 
3997 	network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
3998 	if (unlikely(network_hdr_len >  IXGBEVF_MAX_NETWORK_HDR_LEN))
3999 		return features & ~(NETIF_F_HW_CSUM |
4000 				    NETIF_F_SCTP_CRC |
4001 				    NETIF_F_TSO |
4002 				    NETIF_F_TSO6);
4003 
4004 	/* We can only support IPV4 TSO in tunnels if we can mangle the
4005 	 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4006 	 */
4007 	if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4008 		features &= ~NETIF_F_TSO;
4009 
4010 	return features;
4011 }
4012 
4013 static const struct net_device_ops ixgbevf_netdev_ops = {
4014 	.ndo_open		= ixgbevf_open,
4015 	.ndo_stop		= ixgbevf_close,
4016 	.ndo_start_xmit		= ixgbevf_xmit_frame,
4017 	.ndo_set_rx_mode	= ixgbevf_set_rx_mode,
4018 	.ndo_get_stats64	= ixgbevf_get_stats,
4019 	.ndo_validate_addr	= eth_validate_addr,
4020 	.ndo_set_mac_address	= ixgbevf_set_mac,
4021 	.ndo_change_mtu		= ixgbevf_change_mtu,
4022 	.ndo_tx_timeout		= ixgbevf_tx_timeout,
4023 	.ndo_vlan_rx_add_vid	= ixgbevf_vlan_rx_add_vid,
4024 	.ndo_vlan_rx_kill_vid	= ixgbevf_vlan_rx_kill_vid,
4025 #ifdef CONFIG_NET_POLL_CONTROLLER
4026 	.ndo_poll_controller	= ixgbevf_netpoll,
4027 #endif
4028 	.ndo_features_check	= ixgbevf_features_check,
4029 };
4030 
4031 static void ixgbevf_assign_netdev_ops(struct net_device *dev)
4032 {
4033 	dev->netdev_ops = &ixgbevf_netdev_ops;
4034 	ixgbevf_set_ethtool_ops(dev);
4035 	dev->watchdog_timeo = 5 * HZ;
4036 }
4037 
4038 /**
4039  * ixgbevf_probe - Device Initialization Routine
4040  * @pdev: PCI device information struct
4041  * @ent: entry in ixgbevf_pci_tbl
4042  *
4043  * Returns 0 on success, negative on failure
4044  *
4045  * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
4046  * The OS initialization, configuring of the adapter private structure,
4047  * and a hardware reset occur.
4048  **/
4049 static int ixgbevf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4050 {
4051 	struct net_device *netdev;
4052 	struct ixgbevf_adapter *adapter = NULL;
4053 	struct ixgbe_hw *hw = NULL;
4054 	const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
4055 	int err, pci_using_dac;
4056 	bool disable_dev = false;
4057 
4058 	err = pci_enable_device(pdev);
4059 	if (err)
4060 		return err;
4061 
4062 	if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
4063 		pci_using_dac = 1;
4064 	} else {
4065 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4066 		if (err) {
4067 			dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
4068 			goto err_dma;
4069 		}
4070 		pci_using_dac = 0;
4071 	}
4072 
4073 	err = pci_request_regions(pdev, ixgbevf_driver_name);
4074 	if (err) {
4075 		dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
4076 		goto err_pci_reg;
4077 	}
4078 
4079 	pci_set_master(pdev);
4080 
4081 	netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
4082 				   MAX_TX_QUEUES);
4083 	if (!netdev) {
4084 		err = -ENOMEM;
4085 		goto err_alloc_etherdev;
4086 	}
4087 
4088 	SET_NETDEV_DEV(netdev, &pdev->dev);
4089 
4090 	adapter = netdev_priv(netdev);
4091 
4092 	adapter->netdev = netdev;
4093 	adapter->pdev = pdev;
4094 	hw = &adapter->hw;
4095 	hw->back = adapter;
4096 	adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4097 
4098 	/* call save state here in standalone driver because it relies on
4099 	 * adapter struct to exist, and needs to call netdev_priv
4100 	 */
4101 	pci_save_state(pdev);
4102 
4103 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
4104 			      pci_resource_len(pdev, 0));
4105 	adapter->io_addr = hw->hw_addr;
4106 	if (!hw->hw_addr) {
4107 		err = -EIO;
4108 		goto err_ioremap;
4109 	}
4110 
4111 	ixgbevf_assign_netdev_ops(netdev);
4112 
4113 	/* Setup HW API */
4114 	memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
4115 	hw->mac.type  = ii->mac;
4116 
4117 	memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
4118 	       sizeof(struct ixgbe_mbx_operations));
4119 
4120 	/* setup the private structure */
4121 	err = ixgbevf_sw_init(adapter);
4122 	if (err)
4123 		goto err_sw_init;
4124 
4125 	/* The HW MAC address was set and/or determined in sw_init */
4126 	if (!is_valid_ether_addr(netdev->dev_addr)) {
4127 		pr_err("invalid MAC address\n");
4128 		err = -EIO;
4129 		goto err_sw_init;
4130 	}
4131 
4132 	netdev->hw_features = NETIF_F_SG |
4133 			      NETIF_F_TSO |
4134 			      NETIF_F_TSO6 |
4135 			      NETIF_F_RXCSUM |
4136 			      NETIF_F_HW_CSUM |
4137 			      NETIF_F_SCTP_CRC;
4138 
4139 #define IXGBEVF_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
4140 				      NETIF_F_GSO_GRE_CSUM | \
4141 				      NETIF_F_GSO_IPXIP4 | \
4142 				      NETIF_F_GSO_IPXIP6 | \
4143 				      NETIF_F_GSO_UDP_TUNNEL | \
4144 				      NETIF_F_GSO_UDP_TUNNEL_CSUM)
4145 
4146 	netdev->gso_partial_features = IXGBEVF_GSO_PARTIAL_FEATURES;
4147 	netdev->hw_features |= NETIF_F_GSO_PARTIAL |
4148 			       IXGBEVF_GSO_PARTIAL_FEATURES;
4149 
4150 	netdev->features = netdev->hw_features;
4151 
4152 	if (pci_using_dac)
4153 		netdev->features |= NETIF_F_HIGHDMA;
4154 
4155 	netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
4156 	netdev->mpls_features |= NETIF_F_SG |
4157 				 NETIF_F_TSO |
4158 				 NETIF_F_TSO6 |
4159 				 NETIF_F_HW_CSUM;
4160 	netdev->mpls_features |= IXGBEVF_GSO_PARTIAL_FEATURES;
4161 	netdev->hw_enc_features |= netdev->vlan_features;
4162 
4163 	/* set this bit last since it cannot be part of vlan_features */
4164 	netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4165 			    NETIF_F_HW_VLAN_CTAG_RX |
4166 			    NETIF_F_HW_VLAN_CTAG_TX;
4167 
4168 	netdev->priv_flags |= IFF_UNICAST_FLT;
4169 
4170 	/* MTU range: 68 - 1504 or 9710 */
4171 	netdev->min_mtu = ETH_MIN_MTU;
4172 	switch (adapter->hw.api_version) {
4173 	case ixgbe_mbox_api_11:
4174 	case ixgbe_mbox_api_12:
4175 	case ixgbe_mbox_api_13:
4176 		netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4177 				  (ETH_HLEN + ETH_FCS_LEN);
4178 		break;
4179 	default:
4180 		if (adapter->hw.mac.type != ixgbe_mac_82599_vf)
4181 			netdev->max_mtu = IXGBE_MAX_JUMBO_FRAME_SIZE -
4182 					  (ETH_HLEN + ETH_FCS_LEN);
4183 		else
4184 			netdev->max_mtu = ETH_DATA_LEN + ETH_FCS_LEN;
4185 		break;
4186 	}
4187 
4188 	if (IXGBE_REMOVED(hw->hw_addr)) {
4189 		err = -EIO;
4190 		goto err_sw_init;
4191 	}
4192 
4193 	timer_setup(&adapter->service_timer, ixgbevf_service_timer, 0);
4194 
4195 	INIT_WORK(&adapter->service_task, ixgbevf_service_task);
4196 	set_bit(__IXGBEVF_SERVICE_INITED, &adapter->state);
4197 	clear_bit(__IXGBEVF_SERVICE_SCHED, &adapter->state);
4198 
4199 	err = ixgbevf_init_interrupt_scheme(adapter);
4200 	if (err)
4201 		goto err_sw_init;
4202 
4203 	strcpy(netdev->name, "eth%d");
4204 
4205 	err = register_netdev(netdev);
4206 	if (err)
4207 		goto err_register;
4208 
4209 	pci_set_drvdata(pdev, netdev);
4210 	netif_carrier_off(netdev);
4211 
4212 	ixgbevf_init_last_counter_stats(adapter);
4213 
4214 	/* print the VF info */
4215 	dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
4216 	dev_info(&pdev->dev, "MAC: %d\n", hw->mac.type);
4217 
4218 	switch (hw->mac.type) {
4219 	case ixgbe_mac_X550_vf:
4220 		dev_info(&pdev->dev, "Intel(R) X550 Virtual Function\n");
4221 		break;
4222 	case ixgbe_mac_X540_vf:
4223 		dev_info(&pdev->dev, "Intel(R) X540 Virtual Function\n");
4224 		break;
4225 	case ixgbe_mac_82599_vf:
4226 	default:
4227 		dev_info(&pdev->dev, "Intel(R) 82599 Virtual Function\n");
4228 		break;
4229 	}
4230 
4231 	return 0;
4232 
4233 err_register:
4234 	ixgbevf_clear_interrupt_scheme(adapter);
4235 err_sw_init:
4236 	ixgbevf_reset_interrupt_capability(adapter);
4237 	iounmap(adapter->io_addr);
4238 	kfree(adapter->rss_key);
4239 err_ioremap:
4240 	disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4241 	free_netdev(netdev);
4242 err_alloc_etherdev:
4243 	pci_release_regions(pdev);
4244 err_pci_reg:
4245 err_dma:
4246 	if (!adapter || disable_dev)
4247 		pci_disable_device(pdev);
4248 	return err;
4249 }
4250 
4251 /**
4252  * ixgbevf_remove - Device Removal Routine
4253  * @pdev: PCI device information struct
4254  *
4255  * ixgbevf_remove is called by the PCI subsystem to alert the driver
4256  * that it should release a PCI device.  The could be caused by a
4257  * Hot-Plug event, or because the driver is going to be removed from
4258  * memory.
4259  **/
4260 static void ixgbevf_remove(struct pci_dev *pdev)
4261 {
4262 	struct net_device *netdev = pci_get_drvdata(pdev);
4263 	struct ixgbevf_adapter *adapter;
4264 	bool disable_dev;
4265 
4266 	if (!netdev)
4267 		return;
4268 
4269 	adapter = netdev_priv(netdev);
4270 
4271 	set_bit(__IXGBEVF_REMOVING, &adapter->state);
4272 	cancel_work_sync(&adapter->service_task);
4273 
4274 	if (netdev->reg_state == NETREG_REGISTERED)
4275 		unregister_netdev(netdev);
4276 
4277 	ixgbevf_clear_interrupt_scheme(adapter);
4278 	ixgbevf_reset_interrupt_capability(adapter);
4279 
4280 	iounmap(adapter->io_addr);
4281 	pci_release_regions(pdev);
4282 
4283 	hw_dbg(&adapter->hw, "Remove complete\n");
4284 
4285 	kfree(adapter->rss_key);
4286 	disable_dev = !test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state);
4287 	free_netdev(netdev);
4288 
4289 	if (disable_dev)
4290 		pci_disable_device(pdev);
4291 }
4292 
4293 /**
4294  * ixgbevf_io_error_detected - called when PCI error is detected
4295  * @pdev: Pointer to PCI device
4296  * @state: The current pci connection state
4297  *
4298  * This function is called after a PCI bus error affecting
4299  * this device has been detected.
4300  **/
4301 static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
4302 						  pci_channel_state_t state)
4303 {
4304 	struct net_device *netdev = pci_get_drvdata(pdev);
4305 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4306 
4307 	if (!test_bit(__IXGBEVF_SERVICE_INITED, &adapter->state))
4308 		return PCI_ERS_RESULT_DISCONNECT;
4309 
4310 	rtnl_lock();
4311 	netif_device_detach(netdev);
4312 
4313 	if (state == pci_channel_io_perm_failure) {
4314 		rtnl_unlock();
4315 		return PCI_ERS_RESULT_DISCONNECT;
4316 	}
4317 
4318 	if (netif_running(netdev))
4319 		ixgbevf_close_suspend(adapter);
4320 
4321 	if (!test_and_set_bit(__IXGBEVF_DISABLED, &adapter->state))
4322 		pci_disable_device(pdev);
4323 	rtnl_unlock();
4324 
4325 	/* Request a slot slot reset. */
4326 	return PCI_ERS_RESULT_NEED_RESET;
4327 }
4328 
4329 /**
4330  * ixgbevf_io_slot_reset - called after the pci bus has been reset.
4331  * @pdev: Pointer to PCI device
4332  *
4333  * Restart the card from scratch, as if from a cold-boot. Implementation
4334  * resembles the first-half of the ixgbevf_resume routine.
4335  **/
4336 static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
4337 {
4338 	struct net_device *netdev = pci_get_drvdata(pdev);
4339 	struct ixgbevf_adapter *adapter = netdev_priv(netdev);
4340 
4341 	if (pci_enable_device_mem(pdev)) {
4342 		dev_err(&pdev->dev,
4343 			"Cannot re-enable PCI device after reset.\n");
4344 		return PCI_ERS_RESULT_DISCONNECT;
4345 	}
4346 
4347 	adapter->hw.hw_addr = adapter->io_addr;
4348 	smp_mb__before_atomic();
4349 	clear_bit(__IXGBEVF_DISABLED, &adapter->state);
4350 	pci_set_master(pdev);
4351 
4352 	ixgbevf_reset(adapter);
4353 
4354 	return PCI_ERS_RESULT_RECOVERED;
4355 }
4356 
4357 /**
4358  * ixgbevf_io_resume - called when traffic can start flowing again.
4359  * @pdev: Pointer to PCI device
4360  *
4361  * This callback is called when the error recovery driver tells us that
4362  * its OK to resume normal operation. Implementation resembles the
4363  * second-half of the ixgbevf_resume routine.
4364  **/
4365 static void ixgbevf_io_resume(struct pci_dev *pdev)
4366 {
4367 	struct net_device *netdev = pci_get_drvdata(pdev);
4368 
4369 	rtnl_lock();
4370 	if (netif_running(netdev))
4371 		ixgbevf_open(netdev);
4372 
4373 	netif_device_attach(netdev);
4374 	rtnl_unlock();
4375 }
4376 
4377 /* PCI Error Recovery (ERS) */
4378 static const struct pci_error_handlers ixgbevf_err_handler = {
4379 	.error_detected = ixgbevf_io_error_detected,
4380 	.slot_reset = ixgbevf_io_slot_reset,
4381 	.resume = ixgbevf_io_resume,
4382 };
4383 
4384 static struct pci_driver ixgbevf_driver = {
4385 	.name		= ixgbevf_driver_name,
4386 	.id_table	= ixgbevf_pci_tbl,
4387 	.probe		= ixgbevf_probe,
4388 	.remove		= ixgbevf_remove,
4389 #ifdef CONFIG_PM
4390 	/* Power Management Hooks */
4391 	.suspend	= ixgbevf_suspend,
4392 	.resume		= ixgbevf_resume,
4393 #endif
4394 	.shutdown	= ixgbevf_shutdown,
4395 	.err_handler	= &ixgbevf_err_handler
4396 };
4397 
4398 /**
4399  * ixgbevf_init_module - Driver Registration Routine
4400  *
4401  * ixgbevf_init_module is the first routine called when the driver is
4402  * loaded. All it does is register with the PCI subsystem.
4403  **/
4404 static int __init ixgbevf_init_module(void)
4405 {
4406 	pr_info("%s - version %s\n", ixgbevf_driver_string,
4407 		ixgbevf_driver_version);
4408 
4409 	pr_info("%s\n", ixgbevf_copyright);
4410 	ixgbevf_wq = create_singlethread_workqueue(ixgbevf_driver_name);
4411 	if (!ixgbevf_wq) {
4412 		pr_err("%s: Failed to create workqueue\n", ixgbevf_driver_name);
4413 		return -ENOMEM;
4414 	}
4415 
4416 	return pci_register_driver(&ixgbevf_driver);
4417 }
4418 
4419 module_init(ixgbevf_init_module);
4420 
4421 /**
4422  * ixgbevf_exit_module - Driver Exit Cleanup Routine
4423  *
4424  * ixgbevf_exit_module is called just before the driver is removed
4425  * from memory.
4426  **/
4427 static void __exit ixgbevf_exit_module(void)
4428 {
4429 	pci_unregister_driver(&ixgbevf_driver);
4430 	if (ixgbevf_wq) {
4431 		destroy_workqueue(ixgbevf_wq);
4432 		ixgbevf_wq = NULL;
4433 	}
4434 }
4435 
4436 #ifdef DEBUG
4437 /**
4438  * ixgbevf_get_hw_dev_name - return device name string
4439  * used by hardware layer to print debugging information
4440  * @hw: pointer to private hardware struct
4441  **/
4442 char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
4443 {
4444 	struct ixgbevf_adapter *adapter = hw->back;
4445 
4446 	return adapter->netdev->name;
4447 }
4448 
4449 #endif
4450 module_exit(ixgbevf_exit_module);
4451 
4452 /* ixgbevf_main.c */
4453