xref: /linux/drivers/net/ethernet/intel/fm10k/fm10k_main.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /* Intel(R) Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2017 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20 
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <net/ipv6.h>
24 #include <net/ip.h>
25 #include <net/tcp.h>
26 #include <linux/if_macvlan.h>
27 #include <linux/prefetch.h>
28 
29 #include "fm10k.h"
30 
31 #define DRV_VERSION	"0.21.7-k"
32 #define DRV_SUMMARY	"Intel(R) Ethernet Switch Host Interface Driver"
33 const char fm10k_driver_version[] = DRV_VERSION;
34 char fm10k_driver_name[] = "fm10k";
35 static const char fm10k_driver_string[] = DRV_SUMMARY;
36 static const char fm10k_copyright[] =
37 	"Copyright(c) 2013 - 2017 Intel Corporation.";
38 
39 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
40 MODULE_DESCRIPTION(DRV_SUMMARY);
41 MODULE_LICENSE("GPL");
42 MODULE_VERSION(DRV_VERSION);
43 
44 /* single workqueue for entire fm10k driver */
45 struct workqueue_struct *fm10k_workqueue;
46 
47 /**
48  * fm10k_init_module - Driver Registration Routine
49  *
50  * fm10k_init_module is the first routine called when the driver is
51  * loaded.  All it does is register with the PCI subsystem.
52  **/
53 static int __init fm10k_init_module(void)
54 {
55 	pr_info("%s - version %s\n", fm10k_driver_string, fm10k_driver_version);
56 	pr_info("%s\n", fm10k_copyright);
57 
58 	/* create driver workqueue */
59 	fm10k_workqueue = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0,
60 					  fm10k_driver_name);
61 
62 	fm10k_dbg_init();
63 
64 	return fm10k_register_pci_driver();
65 }
66 module_init(fm10k_init_module);
67 
68 /**
69  * fm10k_exit_module - Driver Exit Cleanup Routine
70  *
71  * fm10k_exit_module is called just before the driver is removed
72  * from memory.
73  **/
74 static void __exit fm10k_exit_module(void)
75 {
76 	fm10k_unregister_pci_driver();
77 
78 	fm10k_dbg_exit();
79 
80 	/* destroy driver workqueue */
81 	destroy_workqueue(fm10k_workqueue);
82 }
83 module_exit(fm10k_exit_module);
84 
85 static bool fm10k_alloc_mapped_page(struct fm10k_ring *rx_ring,
86 				    struct fm10k_rx_buffer *bi)
87 {
88 	struct page *page = bi->page;
89 	dma_addr_t dma;
90 
91 	/* Only page will be NULL if buffer was consumed */
92 	if (likely(page))
93 		return true;
94 
95 	/* alloc new page for storage */
96 	page = dev_alloc_page();
97 	if (unlikely(!page)) {
98 		rx_ring->rx_stats.alloc_failed++;
99 		return false;
100 	}
101 
102 	/* map page for use */
103 	dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
104 
105 	/* if mapping failed free memory back to system since
106 	 * there isn't much point in holding memory we can't use
107 	 */
108 	if (dma_mapping_error(rx_ring->dev, dma)) {
109 		__free_page(page);
110 
111 		rx_ring->rx_stats.alloc_failed++;
112 		return false;
113 	}
114 
115 	bi->dma = dma;
116 	bi->page = page;
117 	bi->page_offset = 0;
118 
119 	return true;
120 }
121 
122 /**
123  * fm10k_alloc_rx_buffers - Replace used receive buffers
124  * @rx_ring: ring to place buffers on
125  * @cleaned_count: number of buffers to replace
126  **/
127 void fm10k_alloc_rx_buffers(struct fm10k_ring *rx_ring, u16 cleaned_count)
128 {
129 	union fm10k_rx_desc *rx_desc;
130 	struct fm10k_rx_buffer *bi;
131 	u16 i = rx_ring->next_to_use;
132 
133 	/* nothing to do */
134 	if (!cleaned_count)
135 		return;
136 
137 	rx_desc = FM10K_RX_DESC(rx_ring, i);
138 	bi = &rx_ring->rx_buffer[i];
139 	i -= rx_ring->count;
140 
141 	do {
142 		if (!fm10k_alloc_mapped_page(rx_ring, bi))
143 			break;
144 
145 		/* Refresh the desc even if buffer_addrs didn't change
146 		 * because each write-back erases this info.
147 		 */
148 		rx_desc->q.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
149 
150 		rx_desc++;
151 		bi++;
152 		i++;
153 		if (unlikely(!i)) {
154 			rx_desc = FM10K_RX_DESC(rx_ring, 0);
155 			bi = rx_ring->rx_buffer;
156 			i -= rx_ring->count;
157 		}
158 
159 		/* clear the status bits for the next_to_use descriptor */
160 		rx_desc->d.staterr = 0;
161 
162 		cleaned_count--;
163 	} while (cleaned_count);
164 
165 	i += rx_ring->count;
166 
167 	if (rx_ring->next_to_use != i) {
168 		/* record the next descriptor to use */
169 		rx_ring->next_to_use = i;
170 
171 		/* update next to alloc since we have filled the ring */
172 		rx_ring->next_to_alloc = i;
173 
174 		/* Force memory writes to complete before letting h/w
175 		 * know there are new descriptors to fetch.  (Only
176 		 * applicable for weak-ordered memory model archs,
177 		 * such as IA-64).
178 		 */
179 		wmb();
180 
181 		/* notify hardware of new descriptors */
182 		writel(i, rx_ring->tail);
183 	}
184 }
185 
186 /**
187  * fm10k_reuse_rx_page - page flip buffer and store it back on the ring
188  * @rx_ring: rx descriptor ring to store buffers on
189  * @old_buff: donor buffer to have page reused
190  *
191  * Synchronizes page for reuse by the interface
192  **/
193 static void fm10k_reuse_rx_page(struct fm10k_ring *rx_ring,
194 				struct fm10k_rx_buffer *old_buff)
195 {
196 	struct fm10k_rx_buffer *new_buff;
197 	u16 nta = rx_ring->next_to_alloc;
198 
199 	new_buff = &rx_ring->rx_buffer[nta];
200 
201 	/* update, and store next to alloc */
202 	nta++;
203 	rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
204 
205 	/* transfer page from old buffer to new buffer */
206 	*new_buff = *old_buff;
207 
208 	/* sync the buffer for use by the device */
209 	dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
210 					 old_buff->page_offset,
211 					 FM10K_RX_BUFSZ,
212 					 DMA_FROM_DEVICE);
213 }
214 
215 static inline bool fm10k_page_is_reserved(struct page *page)
216 {
217 	return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
218 }
219 
220 static bool fm10k_can_reuse_rx_page(struct fm10k_rx_buffer *rx_buffer,
221 				    struct page *page,
222 				    unsigned int __maybe_unused truesize)
223 {
224 	/* avoid re-using remote pages */
225 	if (unlikely(fm10k_page_is_reserved(page)))
226 		return false;
227 
228 #if (PAGE_SIZE < 8192)
229 	/* if we are only owner of page we can reuse it */
230 	if (unlikely(page_count(page) != 1))
231 		return false;
232 
233 	/* flip page offset to other buffer */
234 	rx_buffer->page_offset ^= FM10K_RX_BUFSZ;
235 #else
236 	/* move offset up to the next cache line */
237 	rx_buffer->page_offset += truesize;
238 
239 	if (rx_buffer->page_offset > (PAGE_SIZE - FM10K_RX_BUFSZ))
240 		return false;
241 #endif
242 
243 	/* Even if we own the page, we are not allowed to use atomic_set()
244 	 * This would break get_page_unless_zero() users.
245 	 */
246 	page_ref_inc(page);
247 
248 	return true;
249 }
250 
251 /**
252  * fm10k_add_rx_frag - Add contents of Rx buffer to sk_buff
253  * @rx_buffer: buffer containing page to add
254  * @size: packet size from rx_desc
255  * @rx_desc: descriptor containing length of buffer written by hardware
256  * @skb: sk_buff to place the data into
257  *
258  * This function will add the data contained in rx_buffer->page to the skb.
259  * This is done either through a direct copy if the data in the buffer is
260  * less than the skb header size, otherwise it will just attach the page as
261  * a frag to the skb.
262  *
263  * The function will then update the page offset if necessary and return
264  * true if the buffer can be reused by the interface.
265  **/
266 static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
267 			      unsigned int size,
268 			      union fm10k_rx_desc *rx_desc,
269 			      struct sk_buff *skb)
270 {
271 	struct page *page = rx_buffer->page;
272 	unsigned char *va = page_address(page) + rx_buffer->page_offset;
273 #if (PAGE_SIZE < 8192)
274 	unsigned int truesize = FM10K_RX_BUFSZ;
275 #else
276 	unsigned int truesize = ALIGN(size, 512);
277 #endif
278 	unsigned int pull_len;
279 
280 	if (unlikely(skb_is_nonlinear(skb)))
281 		goto add_tail_frag;
282 
283 	if (likely(size <= FM10K_RX_HDR_LEN)) {
284 		memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
285 
286 		/* page is not reserved, we can reuse buffer as-is */
287 		if (likely(!fm10k_page_is_reserved(page)))
288 			return true;
289 
290 		/* this page cannot be reused so discard it */
291 		__free_page(page);
292 		return false;
293 	}
294 
295 	/* we need the header to contain the greater of either ETH_HLEN or
296 	 * 60 bytes if the skb->len is less than 60 for skb_pad.
297 	 */
298 	pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
299 
300 	/* align pull length to size of long to optimize memcpy performance */
301 	memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
302 
303 	/* update all of the pointers */
304 	va += pull_len;
305 	size -= pull_len;
306 
307 add_tail_frag:
308 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
309 			(unsigned long)va & ~PAGE_MASK, size, truesize);
310 
311 	return fm10k_can_reuse_rx_page(rx_buffer, page, truesize);
312 }
313 
314 static struct sk_buff *fm10k_fetch_rx_buffer(struct fm10k_ring *rx_ring,
315 					     union fm10k_rx_desc *rx_desc,
316 					     struct sk_buff *skb)
317 {
318 	unsigned int size = le16_to_cpu(rx_desc->w.length);
319 	struct fm10k_rx_buffer *rx_buffer;
320 	struct page *page;
321 
322 	rx_buffer = &rx_ring->rx_buffer[rx_ring->next_to_clean];
323 	page = rx_buffer->page;
324 	prefetchw(page);
325 
326 	if (likely(!skb)) {
327 		void *page_addr = page_address(page) +
328 				  rx_buffer->page_offset;
329 
330 		/* prefetch first cache line of first page */
331 		prefetch(page_addr);
332 #if L1_CACHE_BYTES < 128
333 		prefetch(page_addr + L1_CACHE_BYTES);
334 #endif
335 
336 		/* allocate a skb to store the frags */
337 		skb = napi_alloc_skb(&rx_ring->q_vector->napi,
338 				     FM10K_RX_HDR_LEN);
339 		if (unlikely(!skb)) {
340 			rx_ring->rx_stats.alloc_failed++;
341 			return NULL;
342 		}
343 
344 		/* we will be copying header into skb->data in
345 		 * pskb_may_pull so it is in our interest to prefetch
346 		 * it now to avoid a possible cache miss
347 		 */
348 		prefetchw(skb->data);
349 	}
350 
351 	/* we are reusing so sync this buffer for CPU use */
352 	dma_sync_single_range_for_cpu(rx_ring->dev,
353 				      rx_buffer->dma,
354 				      rx_buffer->page_offset,
355 				      size,
356 				      DMA_FROM_DEVICE);
357 
358 	/* pull page into skb */
359 	if (fm10k_add_rx_frag(rx_buffer, size, rx_desc, skb)) {
360 		/* hand second half of page back to the ring */
361 		fm10k_reuse_rx_page(rx_ring, rx_buffer);
362 	} else {
363 		/* we are not reusing the buffer so unmap it */
364 		dma_unmap_page(rx_ring->dev, rx_buffer->dma,
365 			       PAGE_SIZE, DMA_FROM_DEVICE);
366 	}
367 
368 	/* clear contents of rx_buffer */
369 	rx_buffer->page = NULL;
370 
371 	return skb;
372 }
373 
374 static inline void fm10k_rx_checksum(struct fm10k_ring *ring,
375 				     union fm10k_rx_desc *rx_desc,
376 				     struct sk_buff *skb)
377 {
378 	skb_checksum_none_assert(skb);
379 
380 	/* Rx checksum disabled via ethtool */
381 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
382 		return;
383 
384 	/* TCP/UDP checksum error bit is set */
385 	if (fm10k_test_staterr(rx_desc,
386 			       FM10K_RXD_STATUS_L4E |
387 			       FM10K_RXD_STATUS_L4E2 |
388 			       FM10K_RXD_STATUS_IPE |
389 			       FM10K_RXD_STATUS_IPE2)) {
390 		ring->rx_stats.csum_err++;
391 		return;
392 	}
393 
394 	/* It must be a TCP or UDP packet with a valid checksum */
395 	if (fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS2))
396 		skb->encapsulation = true;
397 	else if (!fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS))
398 		return;
399 
400 	skb->ip_summed = CHECKSUM_UNNECESSARY;
401 
402 	ring->rx_stats.csum_good++;
403 }
404 
405 #define FM10K_RSS_L4_TYPES_MASK \
406 	(BIT(FM10K_RSSTYPE_IPV4_TCP) | \
407 	 BIT(FM10K_RSSTYPE_IPV4_UDP) | \
408 	 BIT(FM10K_RSSTYPE_IPV6_TCP) | \
409 	 BIT(FM10K_RSSTYPE_IPV6_UDP))
410 
411 static inline void fm10k_rx_hash(struct fm10k_ring *ring,
412 				 union fm10k_rx_desc *rx_desc,
413 				 struct sk_buff *skb)
414 {
415 	u16 rss_type;
416 
417 	if (!(ring->netdev->features & NETIF_F_RXHASH))
418 		return;
419 
420 	rss_type = le16_to_cpu(rx_desc->w.pkt_info) & FM10K_RXD_RSSTYPE_MASK;
421 	if (!rss_type)
422 		return;
423 
424 	skb_set_hash(skb, le32_to_cpu(rx_desc->d.rss),
425 		     (BIT(rss_type) & FM10K_RSS_L4_TYPES_MASK) ?
426 		     PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
427 }
428 
429 static void fm10k_type_trans(struct fm10k_ring *rx_ring,
430 			     union fm10k_rx_desc __maybe_unused *rx_desc,
431 			     struct sk_buff *skb)
432 {
433 	struct net_device *dev = rx_ring->netdev;
434 	struct fm10k_l2_accel *l2_accel = rcu_dereference_bh(rx_ring->l2_accel);
435 
436 	/* check to see if DGLORT belongs to a MACVLAN */
437 	if (l2_accel) {
438 		u16 idx = le16_to_cpu(FM10K_CB(skb)->fi.w.dglort) - 1;
439 
440 		idx -= l2_accel->dglort;
441 		if (idx < l2_accel->size && l2_accel->macvlan[idx])
442 			dev = l2_accel->macvlan[idx];
443 		else
444 			l2_accel = NULL;
445 	}
446 
447 	skb->protocol = eth_type_trans(skb, dev);
448 
449 	if (!l2_accel)
450 		return;
451 
452 	/* update MACVLAN statistics */
453 	macvlan_count_rx(netdev_priv(dev), skb->len + ETH_HLEN, 1,
454 			 !!(rx_desc->w.hdr_info &
455 			    cpu_to_le16(FM10K_RXD_HDR_INFO_XC_MASK)));
456 }
457 
458 /**
459  * fm10k_process_skb_fields - Populate skb header fields from Rx descriptor
460  * @rx_ring: rx descriptor ring packet is being transacted on
461  * @rx_desc: pointer to the EOP Rx descriptor
462  * @skb: pointer to current skb being populated
463  *
464  * This function checks the ring, descriptor, and packet information in
465  * order to populate the hash, checksum, VLAN, timestamp, protocol, and
466  * other fields within the skb.
467  **/
468 static unsigned int fm10k_process_skb_fields(struct fm10k_ring *rx_ring,
469 					     union fm10k_rx_desc *rx_desc,
470 					     struct sk_buff *skb)
471 {
472 	unsigned int len = skb->len;
473 
474 	fm10k_rx_hash(rx_ring, rx_desc, skb);
475 
476 	fm10k_rx_checksum(rx_ring, rx_desc, skb);
477 
478 	FM10K_CB(skb)->tstamp = rx_desc->q.timestamp;
479 
480 	FM10K_CB(skb)->fi.w.vlan = rx_desc->w.vlan;
481 
482 	skb_record_rx_queue(skb, rx_ring->queue_index);
483 
484 	FM10K_CB(skb)->fi.d.glort = rx_desc->d.glort;
485 
486 	if (rx_desc->w.vlan) {
487 		u16 vid = le16_to_cpu(rx_desc->w.vlan);
488 
489 		if ((vid & VLAN_VID_MASK) != rx_ring->vid)
490 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
491 		else if (vid & VLAN_PRIO_MASK)
492 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
493 					       vid & VLAN_PRIO_MASK);
494 	}
495 
496 	fm10k_type_trans(rx_ring, rx_desc, skb);
497 
498 	return len;
499 }
500 
501 /**
502  * fm10k_is_non_eop - process handling of non-EOP buffers
503  * @rx_ring: Rx ring being processed
504  * @rx_desc: Rx descriptor for current buffer
505  *
506  * This function updates next to clean.  If the buffer is an EOP buffer
507  * this function exits returning false, otherwise it will place the
508  * sk_buff in the next buffer to be chained and return true indicating
509  * that this is in fact a non-EOP buffer.
510  **/
511 static bool fm10k_is_non_eop(struct fm10k_ring *rx_ring,
512 			     union fm10k_rx_desc *rx_desc)
513 {
514 	u32 ntc = rx_ring->next_to_clean + 1;
515 
516 	/* fetch, update, and store next to clean */
517 	ntc = (ntc < rx_ring->count) ? ntc : 0;
518 	rx_ring->next_to_clean = ntc;
519 
520 	prefetch(FM10K_RX_DESC(rx_ring, ntc));
521 
522 	if (likely(fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_EOP)))
523 		return false;
524 
525 	return true;
526 }
527 
528 /**
529  * fm10k_cleanup_headers - Correct corrupted or empty headers
530  * @rx_ring: rx descriptor ring packet is being transacted on
531  * @rx_desc: pointer to the EOP Rx descriptor
532  * @skb: pointer to current skb being fixed
533  *
534  * Address the case where we are pulling data in on pages only
535  * and as such no data is present in the skb header.
536  *
537  * In addition if skb is not at least 60 bytes we need to pad it so that
538  * it is large enough to qualify as a valid Ethernet frame.
539  *
540  * Returns true if an error was encountered and skb was freed.
541  **/
542 static bool fm10k_cleanup_headers(struct fm10k_ring *rx_ring,
543 				  union fm10k_rx_desc *rx_desc,
544 				  struct sk_buff *skb)
545 {
546 	if (unlikely((fm10k_test_staterr(rx_desc,
547 					 FM10K_RXD_STATUS_RXE)))) {
548 #define FM10K_TEST_RXD_BIT(rxd, bit) \
549 	((rxd)->w.csum_err & cpu_to_le16(bit))
550 		if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_ERROR))
551 			rx_ring->rx_stats.switch_errors++;
552 		if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_NO_DESCRIPTOR))
553 			rx_ring->rx_stats.drops++;
554 		if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_PP_ERROR))
555 			rx_ring->rx_stats.pp_errors++;
556 		if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_READY))
557 			rx_ring->rx_stats.link_errors++;
558 		if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_TOO_BIG))
559 			rx_ring->rx_stats.length_errors++;
560 		dev_kfree_skb_any(skb);
561 		rx_ring->rx_stats.errors++;
562 		return true;
563 	}
564 
565 	/* if eth_skb_pad returns an error the skb was freed */
566 	if (eth_skb_pad(skb))
567 		return true;
568 
569 	return false;
570 }
571 
572 /**
573  * fm10k_receive_skb - helper function to handle rx indications
574  * @q_vector: structure containing interrupt and ring information
575  * @skb: packet to send up
576  **/
577 static void fm10k_receive_skb(struct fm10k_q_vector *q_vector,
578 			      struct sk_buff *skb)
579 {
580 	napi_gro_receive(&q_vector->napi, skb);
581 }
582 
583 static int fm10k_clean_rx_irq(struct fm10k_q_vector *q_vector,
584 			      struct fm10k_ring *rx_ring,
585 			      int budget)
586 {
587 	struct sk_buff *skb = rx_ring->skb;
588 	unsigned int total_bytes = 0, total_packets = 0;
589 	u16 cleaned_count = fm10k_desc_unused(rx_ring);
590 
591 	while (likely(total_packets < budget)) {
592 		union fm10k_rx_desc *rx_desc;
593 
594 		/* return some buffers to hardware, one at a time is too slow */
595 		if (cleaned_count >= FM10K_RX_BUFFER_WRITE) {
596 			fm10k_alloc_rx_buffers(rx_ring, cleaned_count);
597 			cleaned_count = 0;
598 		}
599 
600 		rx_desc = FM10K_RX_DESC(rx_ring, rx_ring->next_to_clean);
601 
602 		if (!rx_desc->d.staterr)
603 			break;
604 
605 		/* This memory barrier is needed to keep us from reading
606 		 * any other fields out of the rx_desc until we know the
607 		 * descriptor has been written back
608 		 */
609 		dma_rmb();
610 
611 		/* retrieve a buffer from the ring */
612 		skb = fm10k_fetch_rx_buffer(rx_ring, rx_desc, skb);
613 
614 		/* exit if we failed to retrieve a buffer */
615 		if (!skb)
616 			break;
617 
618 		cleaned_count++;
619 
620 		/* fetch next buffer in frame if non-eop */
621 		if (fm10k_is_non_eop(rx_ring, rx_desc))
622 			continue;
623 
624 		/* verify the packet layout is correct */
625 		if (fm10k_cleanup_headers(rx_ring, rx_desc, skb)) {
626 			skb = NULL;
627 			continue;
628 		}
629 
630 		/* populate checksum, timestamp, VLAN, and protocol */
631 		total_bytes += fm10k_process_skb_fields(rx_ring, rx_desc, skb);
632 
633 		fm10k_receive_skb(q_vector, skb);
634 
635 		/* reset skb pointer */
636 		skb = NULL;
637 
638 		/* update budget accounting */
639 		total_packets++;
640 	}
641 
642 	/* place incomplete frames back on ring for completion */
643 	rx_ring->skb = skb;
644 
645 	u64_stats_update_begin(&rx_ring->syncp);
646 	rx_ring->stats.packets += total_packets;
647 	rx_ring->stats.bytes += total_bytes;
648 	u64_stats_update_end(&rx_ring->syncp);
649 	q_vector->rx.total_packets += total_packets;
650 	q_vector->rx.total_bytes += total_bytes;
651 
652 	return total_packets;
653 }
654 
655 #define VXLAN_HLEN (sizeof(struct udphdr) + 8)
656 static struct ethhdr *fm10k_port_is_vxlan(struct sk_buff *skb)
657 {
658 	struct fm10k_intfc *interface = netdev_priv(skb->dev);
659 	struct fm10k_udp_port *vxlan_port;
660 
661 	/* we can only offload a vxlan if we recognize it as such */
662 	vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
663 					      struct fm10k_udp_port, list);
664 
665 	if (!vxlan_port)
666 		return NULL;
667 	if (vxlan_port->port != udp_hdr(skb)->dest)
668 		return NULL;
669 
670 	/* return offset of udp_hdr plus 8 bytes for VXLAN header */
671 	return (struct ethhdr *)(skb_transport_header(skb) + VXLAN_HLEN);
672 }
673 
674 #define FM10K_NVGRE_RESERVED0_FLAGS htons(0x9FFF)
675 #define NVGRE_TNI htons(0x2000)
676 struct fm10k_nvgre_hdr {
677 	__be16 flags;
678 	__be16 proto;
679 	__be32 tni;
680 };
681 
682 static struct ethhdr *fm10k_gre_is_nvgre(struct sk_buff *skb)
683 {
684 	struct fm10k_nvgre_hdr *nvgre_hdr;
685 	int hlen = ip_hdrlen(skb);
686 
687 	/* currently only IPv4 is supported due to hlen above */
688 	if (vlan_get_protocol(skb) != htons(ETH_P_IP))
689 		return NULL;
690 
691 	/* our transport header should be NVGRE */
692 	nvgre_hdr = (struct fm10k_nvgre_hdr *)(skb_network_header(skb) + hlen);
693 
694 	/* verify all reserved flags are 0 */
695 	if (nvgre_hdr->flags & FM10K_NVGRE_RESERVED0_FLAGS)
696 		return NULL;
697 
698 	/* report start of ethernet header */
699 	if (nvgre_hdr->flags & NVGRE_TNI)
700 		return (struct ethhdr *)(nvgre_hdr + 1);
701 
702 	return (struct ethhdr *)(&nvgre_hdr->tni);
703 }
704 
705 __be16 fm10k_tx_encap_offload(struct sk_buff *skb)
706 {
707 	u8 l4_hdr = 0, inner_l4_hdr = 0, inner_l4_hlen;
708 	struct ethhdr *eth_hdr;
709 
710 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
711 	    skb->inner_protocol != htons(ETH_P_TEB))
712 		return 0;
713 
714 	switch (vlan_get_protocol(skb)) {
715 	case htons(ETH_P_IP):
716 		l4_hdr = ip_hdr(skb)->protocol;
717 		break;
718 	case htons(ETH_P_IPV6):
719 		l4_hdr = ipv6_hdr(skb)->nexthdr;
720 		break;
721 	default:
722 		return 0;
723 	}
724 
725 	switch (l4_hdr) {
726 	case IPPROTO_UDP:
727 		eth_hdr = fm10k_port_is_vxlan(skb);
728 		break;
729 	case IPPROTO_GRE:
730 		eth_hdr = fm10k_gre_is_nvgre(skb);
731 		break;
732 	default:
733 		return 0;
734 	}
735 
736 	if (!eth_hdr)
737 		return 0;
738 
739 	switch (eth_hdr->h_proto) {
740 	case htons(ETH_P_IP):
741 		inner_l4_hdr = inner_ip_hdr(skb)->protocol;
742 		break;
743 	case htons(ETH_P_IPV6):
744 		inner_l4_hdr = inner_ipv6_hdr(skb)->nexthdr;
745 		break;
746 	default:
747 		return 0;
748 	}
749 
750 	switch (inner_l4_hdr) {
751 	case IPPROTO_TCP:
752 		inner_l4_hlen = inner_tcp_hdrlen(skb);
753 		break;
754 	case IPPROTO_UDP:
755 		inner_l4_hlen = 8;
756 		break;
757 	default:
758 		return 0;
759 	}
760 
761 	/* The hardware allows tunnel offloads only if the combined inner and
762 	 * outer header is 184 bytes or less
763 	 */
764 	if (skb_inner_transport_header(skb) + inner_l4_hlen -
765 	    skb_mac_header(skb) > FM10K_TUNNEL_HEADER_LENGTH)
766 		return 0;
767 
768 	return eth_hdr->h_proto;
769 }
770 
771 static int fm10k_tso(struct fm10k_ring *tx_ring,
772 		     struct fm10k_tx_buffer *first)
773 {
774 	struct sk_buff *skb = first->skb;
775 	struct fm10k_tx_desc *tx_desc;
776 	unsigned char *th;
777 	u8 hdrlen;
778 
779 	if (skb->ip_summed != CHECKSUM_PARTIAL)
780 		return 0;
781 
782 	if (!skb_is_gso(skb))
783 		return 0;
784 
785 	/* compute header lengths */
786 	if (skb->encapsulation) {
787 		if (!fm10k_tx_encap_offload(skb))
788 			goto err_vxlan;
789 		th = skb_inner_transport_header(skb);
790 	} else {
791 		th = skb_transport_header(skb);
792 	}
793 
794 	/* compute offset from SOF to transport header and add header len */
795 	hdrlen = (th - skb->data) + (((struct tcphdr *)th)->doff << 2);
796 
797 	first->tx_flags |= FM10K_TX_FLAGS_CSUM;
798 
799 	/* update gso size and bytecount with header size */
800 	first->gso_segs = skb_shinfo(skb)->gso_segs;
801 	first->bytecount += (first->gso_segs - 1) * hdrlen;
802 
803 	/* populate Tx descriptor header size and mss */
804 	tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
805 	tx_desc->hdrlen = hdrlen;
806 	tx_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
807 
808 	return 1;
809 err_vxlan:
810 	tx_ring->netdev->features &= ~NETIF_F_GSO_UDP_TUNNEL;
811 	if (!net_ratelimit())
812 		netdev_err(tx_ring->netdev,
813 			   "TSO requested for unsupported tunnel, disabling offload\n");
814 	return -1;
815 }
816 
817 static void fm10k_tx_csum(struct fm10k_ring *tx_ring,
818 			  struct fm10k_tx_buffer *first)
819 {
820 	struct sk_buff *skb = first->skb;
821 	struct fm10k_tx_desc *tx_desc;
822 	union {
823 		struct iphdr *ipv4;
824 		struct ipv6hdr *ipv6;
825 		u8 *raw;
826 	} network_hdr;
827 	u8 *transport_hdr;
828 	__be16 frag_off;
829 	__be16 protocol;
830 	u8 l4_hdr = 0;
831 
832 	if (skb->ip_summed != CHECKSUM_PARTIAL)
833 		goto no_csum;
834 
835 	if (skb->encapsulation) {
836 		protocol = fm10k_tx_encap_offload(skb);
837 		if (!protocol) {
838 			if (skb_checksum_help(skb)) {
839 				dev_warn(tx_ring->dev,
840 					 "failed to offload encap csum!\n");
841 				tx_ring->tx_stats.csum_err++;
842 			}
843 			goto no_csum;
844 		}
845 		network_hdr.raw = skb_inner_network_header(skb);
846 		transport_hdr = skb_inner_transport_header(skb);
847 	} else {
848 		protocol = vlan_get_protocol(skb);
849 		network_hdr.raw = skb_network_header(skb);
850 		transport_hdr = skb_transport_header(skb);
851 	}
852 
853 	switch (protocol) {
854 	case htons(ETH_P_IP):
855 		l4_hdr = network_hdr.ipv4->protocol;
856 		break;
857 	case htons(ETH_P_IPV6):
858 		l4_hdr = network_hdr.ipv6->nexthdr;
859 		if (likely((transport_hdr - network_hdr.raw) ==
860 			   sizeof(struct ipv6hdr)))
861 			break;
862 		ipv6_skip_exthdr(skb, network_hdr.raw - skb->data +
863 				      sizeof(struct ipv6hdr),
864 				 &l4_hdr, &frag_off);
865 		if (unlikely(frag_off))
866 			l4_hdr = NEXTHDR_FRAGMENT;
867 		break;
868 	default:
869 		break;
870 	}
871 
872 	switch (l4_hdr) {
873 	case IPPROTO_TCP:
874 	case IPPROTO_UDP:
875 		break;
876 	case IPPROTO_GRE:
877 		if (skb->encapsulation)
878 			break;
879 	default:
880 		if (unlikely(net_ratelimit())) {
881 			dev_warn(tx_ring->dev,
882 				 "partial checksum, version=%d l4 proto=%x\n",
883 				 protocol, l4_hdr);
884 		}
885 		skb_checksum_help(skb);
886 		tx_ring->tx_stats.csum_err++;
887 		goto no_csum;
888 	}
889 
890 	/* update TX checksum flag */
891 	first->tx_flags |= FM10K_TX_FLAGS_CSUM;
892 	tx_ring->tx_stats.csum_good++;
893 
894 no_csum:
895 	/* populate Tx descriptor header size and mss */
896 	tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
897 	tx_desc->hdrlen = 0;
898 	tx_desc->mss = 0;
899 }
900 
901 #define FM10K_SET_FLAG(_input, _flag, _result) \
902 	((_flag <= _result) ? \
903 	 ((u32)(_input & _flag) * (_result / _flag)) : \
904 	 ((u32)(_input & _flag) / (_flag / _result)))
905 
906 static u8 fm10k_tx_desc_flags(struct sk_buff *skb, u32 tx_flags)
907 {
908 	/* set type for advanced descriptor with frame checksum insertion */
909 	u32 desc_flags = 0;
910 
911 	/* set checksum offload bits */
912 	desc_flags |= FM10K_SET_FLAG(tx_flags, FM10K_TX_FLAGS_CSUM,
913 				     FM10K_TXD_FLAG_CSUM);
914 
915 	return desc_flags;
916 }
917 
918 static bool fm10k_tx_desc_push(struct fm10k_ring *tx_ring,
919 			       struct fm10k_tx_desc *tx_desc, u16 i,
920 			       dma_addr_t dma, unsigned int size, u8 desc_flags)
921 {
922 	/* set RS and INT for last frame in a cache line */
923 	if ((++i & (FM10K_TXD_WB_FIFO_SIZE - 1)) == 0)
924 		desc_flags |= FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_INT;
925 
926 	/* record values to descriptor */
927 	tx_desc->buffer_addr = cpu_to_le64(dma);
928 	tx_desc->flags = desc_flags;
929 	tx_desc->buflen = cpu_to_le16(size);
930 
931 	/* return true if we just wrapped the ring */
932 	return i == tx_ring->count;
933 }
934 
935 static int __fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
936 {
937 	netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
938 
939 	/* Memory barrier before checking head and tail */
940 	smp_mb();
941 
942 	/* Check again in a case another CPU has just made room available */
943 	if (likely(fm10k_desc_unused(tx_ring) < size))
944 		return -EBUSY;
945 
946 	/* A reprieve! - use start_queue because it doesn't call schedule */
947 	netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
948 	++tx_ring->tx_stats.restart_queue;
949 	return 0;
950 }
951 
952 static inline int fm10k_maybe_stop_tx(struct fm10k_ring *tx_ring, u16 size)
953 {
954 	if (likely(fm10k_desc_unused(tx_ring) >= size))
955 		return 0;
956 	return __fm10k_maybe_stop_tx(tx_ring, size);
957 }
958 
959 static void fm10k_tx_map(struct fm10k_ring *tx_ring,
960 			 struct fm10k_tx_buffer *first)
961 {
962 	struct sk_buff *skb = first->skb;
963 	struct fm10k_tx_buffer *tx_buffer;
964 	struct fm10k_tx_desc *tx_desc;
965 	struct skb_frag_struct *frag;
966 	unsigned char *data;
967 	dma_addr_t dma;
968 	unsigned int data_len, size;
969 	u32 tx_flags = first->tx_flags;
970 	u16 i = tx_ring->next_to_use;
971 	u8 flags = fm10k_tx_desc_flags(skb, tx_flags);
972 
973 	tx_desc = FM10K_TX_DESC(tx_ring, i);
974 
975 	/* add HW VLAN tag */
976 	if (skb_vlan_tag_present(skb))
977 		tx_desc->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
978 	else
979 		tx_desc->vlan = 0;
980 
981 	size = skb_headlen(skb);
982 	data = skb->data;
983 
984 	dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
985 
986 	data_len = skb->data_len;
987 	tx_buffer = first;
988 
989 	for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
990 		if (dma_mapping_error(tx_ring->dev, dma))
991 			goto dma_error;
992 
993 		/* record length, and DMA address */
994 		dma_unmap_len_set(tx_buffer, len, size);
995 		dma_unmap_addr_set(tx_buffer, dma, dma);
996 
997 		while (unlikely(size > FM10K_MAX_DATA_PER_TXD)) {
998 			if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++, dma,
999 					       FM10K_MAX_DATA_PER_TXD, flags)) {
1000 				tx_desc = FM10K_TX_DESC(tx_ring, 0);
1001 				i = 0;
1002 			}
1003 
1004 			dma += FM10K_MAX_DATA_PER_TXD;
1005 			size -= FM10K_MAX_DATA_PER_TXD;
1006 		}
1007 
1008 		if (likely(!data_len))
1009 			break;
1010 
1011 		if (fm10k_tx_desc_push(tx_ring, tx_desc++, i++,
1012 				       dma, size, flags)) {
1013 			tx_desc = FM10K_TX_DESC(tx_ring, 0);
1014 			i = 0;
1015 		}
1016 
1017 		size = skb_frag_size(frag);
1018 		data_len -= size;
1019 
1020 		dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
1021 				       DMA_TO_DEVICE);
1022 
1023 		tx_buffer = &tx_ring->tx_buffer[i];
1024 	}
1025 
1026 	/* write last descriptor with LAST bit set */
1027 	flags |= FM10K_TXD_FLAG_LAST;
1028 
1029 	if (fm10k_tx_desc_push(tx_ring, tx_desc, i++, dma, size, flags))
1030 		i = 0;
1031 
1032 	/* record bytecount for BQL */
1033 	netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1034 
1035 	/* record SW timestamp if HW timestamp is not available */
1036 	skb_tx_timestamp(first->skb);
1037 
1038 	/* Force memory writes to complete before letting h/w know there
1039 	 * are new descriptors to fetch.  (Only applicable for weak-ordered
1040 	 * memory model archs, such as IA-64).
1041 	 *
1042 	 * We also need this memory barrier to make certain all of the
1043 	 * status bits have been updated before next_to_watch is written.
1044 	 */
1045 	wmb();
1046 
1047 	/* set next_to_watch value indicating a packet is present */
1048 	first->next_to_watch = tx_desc;
1049 
1050 	tx_ring->next_to_use = i;
1051 
1052 	/* Make sure there is space in the ring for the next send. */
1053 	fm10k_maybe_stop_tx(tx_ring, DESC_NEEDED);
1054 
1055 	/* notify HW of packet */
1056 	if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
1057 		writel(i, tx_ring->tail);
1058 
1059 		/* we need this if more than one processor can write to our tail
1060 		 * at a time, it synchronizes IO on IA64/Altix systems
1061 		 */
1062 		mmiowb();
1063 	}
1064 
1065 	return;
1066 dma_error:
1067 	dev_err(tx_ring->dev, "TX DMA map failed\n");
1068 
1069 	/* clear dma mappings for failed tx_buffer map */
1070 	for (;;) {
1071 		tx_buffer = &tx_ring->tx_buffer[i];
1072 		fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
1073 		if (tx_buffer == first)
1074 			break;
1075 		if (i == 0)
1076 			i = tx_ring->count;
1077 		i--;
1078 	}
1079 
1080 	tx_ring->next_to_use = i;
1081 }
1082 
1083 netdev_tx_t fm10k_xmit_frame_ring(struct sk_buff *skb,
1084 				  struct fm10k_ring *tx_ring)
1085 {
1086 	u16 count = TXD_USE_COUNT(skb_headlen(skb));
1087 	struct fm10k_tx_buffer *first;
1088 	unsigned short f;
1089 	u32 tx_flags = 0;
1090 	int tso;
1091 
1092 	/* need: 1 descriptor per page * PAGE_SIZE/FM10K_MAX_DATA_PER_TXD,
1093 	 *       + 1 desc for skb_headlen/FM10K_MAX_DATA_PER_TXD,
1094 	 *       + 2 desc gap to keep tail from touching head
1095 	 * otherwise try next time
1096 	 */
1097 	for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1098 		count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1099 
1100 	if (fm10k_maybe_stop_tx(tx_ring, count + 3)) {
1101 		tx_ring->tx_stats.tx_busy++;
1102 		return NETDEV_TX_BUSY;
1103 	}
1104 
1105 	/* record the location of the first descriptor for this packet */
1106 	first = &tx_ring->tx_buffer[tx_ring->next_to_use];
1107 	first->skb = skb;
1108 	first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN);
1109 	first->gso_segs = 1;
1110 
1111 	/* record initial flags and protocol */
1112 	first->tx_flags = tx_flags;
1113 
1114 	tso = fm10k_tso(tx_ring, first);
1115 	if (tso < 0)
1116 		goto out_drop;
1117 	else if (!tso)
1118 		fm10k_tx_csum(tx_ring, first);
1119 
1120 	fm10k_tx_map(tx_ring, first);
1121 
1122 	return NETDEV_TX_OK;
1123 
1124 out_drop:
1125 	dev_kfree_skb_any(first->skb);
1126 	first->skb = NULL;
1127 
1128 	return NETDEV_TX_OK;
1129 }
1130 
1131 static u64 fm10k_get_tx_completed(struct fm10k_ring *ring)
1132 {
1133 	return ring->stats.packets;
1134 }
1135 
1136 /**
1137  * fm10k_get_tx_pending - how many Tx descriptors not processed
1138  * @ring: the ring structure
1139  * @in_sw: is tx_pending being checked in SW or in HW?
1140  */
1141 u64 fm10k_get_tx_pending(struct fm10k_ring *ring, bool in_sw)
1142 {
1143 	struct fm10k_intfc *interface = ring->q_vector->interface;
1144 	struct fm10k_hw *hw = &interface->hw;
1145 	u32 head, tail;
1146 
1147 	if (likely(in_sw)) {
1148 		head = ring->next_to_clean;
1149 		tail = ring->next_to_use;
1150 	} else {
1151 		head = fm10k_read_reg(hw, FM10K_TDH(ring->reg_idx));
1152 		tail = fm10k_read_reg(hw, FM10K_TDT(ring->reg_idx));
1153 	}
1154 
1155 	return ((head <= tail) ? tail : tail + ring->count) - head;
1156 }
1157 
1158 bool fm10k_check_tx_hang(struct fm10k_ring *tx_ring)
1159 {
1160 	u32 tx_done = fm10k_get_tx_completed(tx_ring);
1161 	u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
1162 	u32 tx_pending = fm10k_get_tx_pending(tx_ring, true);
1163 
1164 	clear_check_for_tx_hang(tx_ring);
1165 
1166 	/* Check for a hung queue, but be thorough. This verifies
1167 	 * that a transmit has been completed since the previous
1168 	 * check AND there is at least one packet pending. By
1169 	 * requiring this to fail twice we avoid races with
1170 	 * clearing the ARMED bit and conditions where we
1171 	 * run the check_tx_hang logic with a transmit completion
1172 	 * pending but without time to complete it yet.
1173 	 */
1174 	if (!tx_pending || (tx_done_old != tx_done)) {
1175 		/* update completed stats and continue */
1176 		tx_ring->tx_stats.tx_done_old = tx_done;
1177 		/* reset the countdown */
1178 		clear_bit(__FM10K_HANG_CHECK_ARMED, tx_ring->state);
1179 
1180 		return false;
1181 	}
1182 
1183 	/* make sure it is true for two checks in a row */
1184 	return test_and_set_bit(__FM10K_HANG_CHECK_ARMED, tx_ring->state);
1185 }
1186 
1187 /**
1188  * fm10k_tx_timeout_reset - initiate reset due to Tx timeout
1189  * @interface: driver private struct
1190  **/
1191 void fm10k_tx_timeout_reset(struct fm10k_intfc *interface)
1192 {
1193 	/* Do the reset outside of interrupt context */
1194 	if (!test_bit(__FM10K_DOWN, interface->state)) {
1195 		interface->tx_timeout_count++;
1196 		set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1197 		fm10k_service_event_schedule(interface);
1198 	}
1199 }
1200 
1201 /**
1202  * fm10k_clean_tx_irq - Reclaim resources after transmit completes
1203  * @q_vector: structure containing interrupt and ring information
1204  * @tx_ring: tx ring to clean
1205  * @napi_budget: Used to determine if we are in netpoll
1206  **/
1207 static bool fm10k_clean_tx_irq(struct fm10k_q_vector *q_vector,
1208 			       struct fm10k_ring *tx_ring, int napi_budget)
1209 {
1210 	struct fm10k_intfc *interface = q_vector->interface;
1211 	struct fm10k_tx_buffer *tx_buffer;
1212 	struct fm10k_tx_desc *tx_desc;
1213 	unsigned int total_bytes = 0, total_packets = 0;
1214 	unsigned int budget = q_vector->tx.work_limit;
1215 	unsigned int i = tx_ring->next_to_clean;
1216 
1217 	if (test_bit(__FM10K_DOWN, interface->state))
1218 		return true;
1219 
1220 	tx_buffer = &tx_ring->tx_buffer[i];
1221 	tx_desc = FM10K_TX_DESC(tx_ring, i);
1222 	i -= tx_ring->count;
1223 
1224 	do {
1225 		struct fm10k_tx_desc *eop_desc = tx_buffer->next_to_watch;
1226 
1227 		/* if next_to_watch is not set then there is no work pending */
1228 		if (!eop_desc)
1229 			break;
1230 
1231 		/* prevent any other reads prior to eop_desc */
1232 		read_barrier_depends();
1233 
1234 		/* if DD is not set pending work has not been completed */
1235 		if (!(eop_desc->flags & FM10K_TXD_FLAG_DONE))
1236 			break;
1237 
1238 		/* clear next_to_watch to prevent false hangs */
1239 		tx_buffer->next_to_watch = NULL;
1240 
1241 		/* update the statistics for this packet */
1242 		total_bytes += tx_buffer->bytecount;
1243 		total_packets += tx_buffer->gso_segs;
1244 
1245 		/* free the skb */
1246 		napi_consume_skb(tx_buffer->skb, napi_budget);
1247 
1248 		/* unmap skb header data */
1249 		dma_unmap_single(tx_ring->dev,
1250 				 dma_unmap_addr(tx_buffer, dma),
1251 				 dma_unmap_len(tx_buffer, len),
1252 				 DMA_TO_DEVICE);
1253 
1254 		/* clear tx_buffer data */
1255 		tx_buffer->skb = NULL;
1256 		dma_unmap_len_set(tx_buffer, len, 0);
1257 
1258 		/* unmap remaining buffers */
1259 		while (tx_desc != eop_desc) {
1260 			tx_buffer++;
1261 			tx_desc++;
1262 			i++;
1263 			if (unlikely(!i)) {
1264 				i -= tx_ring->count;
1265 				tx_buffer = tx_ring->tx_buffer;
1266 				tx_desc = FM10K_TX_DESC(tx_ring, 0);
1267 			}
1268 
1269 			/* unmap any remaining paged data */
1270 			if (dma_unmap_len(tx_buffer, len)) {
1271 				dma_unmap_page(tx_ring->dev,
1272 					       dma_unmap_addr(tx_buffer, dma),
1273 					       dma_unmap_len(tx_buffer, len),
1274 					       DMA_TO_DEVICE);
1275 				dma_unmap_len_set(tx_buffer, len, 0);
1276 			}
1277 		}
1278 
1279 		/* move us one more past the eop_desc for start of next pkt */
1280 		tx_buffer++;
1281 		tx_desc++;
1282 		i++;
1283 		if (unlikely(!i)) {
1284 			i -= tx_ring->count;
1285 			tx_buffer = tx_ring->tx_buffer;
1286 			tx_desc = FM10K_TX_DESC(tx_ring, 0);
1287 		}
1288 
1289 		/* issue prefetch for next Tx descriptor */
1290 		prefetch(tx_desc);
1291 
1292 		/* update budget accounting */
1293 		budget--;
1294 	} while (likely(budget));
1295 
1296 	i += tx_ring->count;
1297 	tx_ring->next_to_clean = i;
1298 	u64_stats_update_begin(&tx_ring->syncp);
1299 	tx_ring->stats.bytes += total_bytes;
1300 	tx_ring->stats.packets += total_packets;
1301 	u64_stats_update_end(&tx_ring->syncp);
1302 	q_vector->tx.total_bytes += total_bytes;
1303 	q_vector->tx.total_packets += total_packets;
1304 
1305 	if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring)) {
1306 		/* schedule immediate reset if we believe we hung */
1307 		struct fm10k_hw *hw = &interface->hw;
1308 
1309 		netif_err(interface, drv, tx_ring->netdev,
1310 			  "Detected Tx Unit Hang\n"
1311 			  "  Tx Queue             <%d>\n"
1312 			  "  TDH, TDT             <%x>, <%x>\n"
1313 			  "  next_to_use          <%x>\n"
1314 			  "  next_to_clean        <%x>\n",
1315 			  tx_ring->queue_index,
1316 			  fm10k_read_reg(hw, FM10K_TDH(tx_ring->reg_idx)),
1317 			  fm10k_read_reg(hw, FM10K_TDT(tx_ring->reg_idx)),
1318 			  tx_ring->next_to_use, i);
1319 
1320 		netif_stop_subqueue(tx_ring->netdev,
1321 				    tx_ring->queue_index);
1322 
1323 		netif_info(interface, probe, tx_ring->netdev,
1324 			   "tx hang %d detected on queue %d, resetting interface\n",
1325 			   interface->tx_timeout_count + 1,
1326 			   tx_ring->queue_index);
1327 
1328 		fm10k_tx_timeout_reset(interface);
1329 
1330 		/* the netdev is about to reset, no point in enabling stuff */
1331 		return true;
1332 	}
1333 
1334 	/* notify netdev of completed buffers */
1335 	netdev_tx_completed_queue(txring_txq(tx_ring),
1336 				  total_packets, total_bytes);
1337 
1338 #define TX_WAKE_THRESHOLD min_t(u16, FM10K_MIN_TXD - 1, DESC_NEEDED * 2)
1339 	if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
1340 		     (fm10k_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD))) {
1341 		/* Make sure that anybody stopping the queue after this
1342 		 * sees the new next_to_clean.
1343 		 */
1344 		smp_mb();
1345 		if (__netif_subqueue_stopped(tx_ring->netdev,
1346 					     tx_ring->queue_index) &&
1347 		    !test_bit(__FM10K_DOWN, interface->state)) {
1348 			netif_wake_subqueue(tx_ring->netdev,
1349 					    tx_ring->queue_index);
1350 			++tx_ring->tx_stats.restart_queue;
1351 		}
1352 	}
1353 
1354 	return !!budget;
1355 }
1356 
1357 /**
1358  * fm10k_update_itr - update the dynamic ITR value based on packet size
1359  *
1360  *      Stores a new ITR value based on strictly on packet size.  The
1361  *      divisors and thresholds used by this function were determined based
1362  *      on theoretical maximum wire speed and testing data, in order to
1363  *      minimize response time while increasing bulk throughput.
1364  *
1365  * @ring_container: Container for rings to have ITR updated
1366  **/
1367 static void fm10k_update_itr(struct fm10k_ring_container *ring_container)
1368 {
1369 	unsigned int avg_wire_size, packets, itr_round;
1370 
1371 	/* Only update ITR if we are using adaptive setting */
1372 	if (!ITR_IS_ADAPTIVE(ring_container->itr))
1373 		goto clear_counts;
1374 
1375 	packets = ring_container->total_packets;
1376 	if (!packets)
1377 		goto clear_counts;
1378 
1379 	avg_wire_size = ring_container->total_bytes / packets;
1380 
1381 	/* The following is a crude approximation of:
1382 	 *  wmem_default / (size + overhead) = desired_pkts_per_int
1383 	 *  rate / bits_per_byte / (size + ethernet overhead) = pkt_rate
1384 	 *  (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value
1385 	 *
1386 	 * Assuming wmem_default is 212992 and overhead is 640 bytes per
1387 	 * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the
1388 	 * formula down to
1389 	 *
1390 	 *  (34 * (size + 24)) / (size + 640) = ITR
1391 	 *
1392 	 * We first do some math on the packet size and then finally bitshift
1393 	 * by 8 after rounding up. We also have to account for PCIe link speed
1394 	 * difference as ITR scales based on this.
1395 	 */
1396 	if (avg_wire_size <= 360) {
1397 		/* Start at 250K ints/sec and gradually drop to 77K ints/sec */
1398 		avg_wire_size *= 8;
1399 		avg_wire_size += 376;
1400 	} else if (avg_wire_size <= 1152) {
1401 		/* 77K ints/sec to 45K ints/sec */
1402 		avg_wire_size *= 3;
1403 		avg_wire_size += 2176;
1404 	} else if (avg_wire_size <= 1920) {
1405 		/* 45K ints/sec to 38K ints/sec */
1406 		avg_wire_size += 4480;
1407 	} else {
1408 		/* plateau at a limit of 38K ints/sec */
1409 		avg_wire_size = 6656;
1410 	}
1411 
1412 	/* Perform final bitshift for division after rounding up to ensure
1413 	 * that the calculation will never get below a 1. The bit shift
1414 	 * accounts for changes in the ITR due to PCIe link speed.
1415 	 */
1416 	itr_round = READ_ONCE(ring_container->itr_scale) + 8;
1417 	avg_wire_size += BIT(itr_round) - 1;
1418 	avg_wire_size >>= itr_round;
1419 
1420 	/* write back value and retain adaptive flag */
1421 	ring_container->itr = avg_wire_size | FM10K_ITR_ADAPTIVE;
1422 
1423 clear_counts:
1424 	ring_container->total_bytes = 0;
1425 	ring_container->total_packets = 0;
1426 }
1427 
1428 static void fm10k_qv_enable(struct fm10k_q_vector *q_vector)
1429 {
1430 	/* Enable auto-mask and clear the current mask */
1431 	u32 itr = FM10K_ITR_ENABLE;
1432 
1433 	/* Update Tx ITR */
1434 	fm10k_update_itr(&q_vector->tx);
1435 
1436 	/* Update Rx ITR */
1437 	fm10k_update_itr(&q_vector->rx);
1438 
1439 	/* Store Tx itr in timer slot 0 */
1440 	itr |= (q_vector->tx.itr & FM10K_ITR_MAX);
1441 
1442 	/* Shift Rx itr to timer slot 1 */
1443 	itr |= (q_vector->rx.itr & FM10K_ITR_MAX) << FM10K_ITR_INTERVAL1_SHIFT;
1444 
1445 	/* Write the final value to the ITR register */
1446 	writel(itr, q_vector->itr);
1447 }
1448 
1449 static int fm10k_poll(struct napi_struct *napi, int budget)
1450 {
1451 	struct fm10k_q_vector *q_vector =
1452 			       container_of(napi, struct fm10k_q_vector, napi);
1453 	struct fm10k_ring *ring;
1454 	int per_ring_budget, work_done = 0;
1455 	bool clean_complete = true;
1456 
1457 	fm10k_for_each_ring(ring, q_vector->tx) {
1458 		if (!fm10k_clean_tx_irq(q_vector, ring, budget))
1459 			clean_complete = false;
1460 	}
1461 
1462 	/* Handle case where we are called by netpoll with a budget of 0 */
1463 	if (budget <= 0)
1464 		return budget;
1465 
1466 	/* attempt to distribute budget to each queue fairly, but don't
1467 	 * allow the budget to go below 1 because we'll exit polling
1468 	 */
1469 	if (q_vector->rx.count > 1)
1470 		per_ring_budget = max(budget / q_vector->rx.count, 1);
1471 	else
1472 		per_ring_budget = budget;
1473 
1474 	fm10k_for_each_ring(ring, q_vector->rx) {
1475 		int work = fm10k_clean_rx_irq(q_vector, ring, per_ring_budget);
1476 
1477 		work_done += work;
1478 		if (work >= per_ring_budget)
1479 			clean_complete = false;
1480 	}
1481 
1482 	/* If all work not completed, return budget and keep polling */
1483 	if (!clean_complete)
1484 		return budget;
1485 
1486 	/* all work done, exit the polling mode */
1487 	napi_complete_done(napi, work_done);
1488 
1489 	/* re-enable the q_vector */
1490 	fm10k_qv_enable(q_vector);
1491 
1492 	return min(work_done, budget - 1);
1493 }
1494 
1495 /**
1496  * fm10k_set_qos_queues: Allocate queues for a QOS-enabled device
1497  * @interface: board private structure to initialize
1498  *
1499  * When QoS (Quality of Service) is enabled, allocate queues for
1500  * each traffic class.  If multiqueue isn't available,then abort QoS
1501  * initialization.
1502  *
1503  * This function handles all combinations of Qos and RSS.
1504  *
1505  **/
1506 static bool fm10k_set_qos_queues(struct fm10k_intfc *interface)
1507 {
1508 	struct net_device *dev = interface->netdev;
1509 	struct fm10k_ring_feature *f;
1510 	int rss_i, i;
1511 	int pcs;
1512 
1513 	/* Map queue offset and counts onto allocated tx queues */
1514 	pcs = netdev_get_num_tc(dev);
1515 
1516 	if (pcs <= 1)
1517 		return false;
1518 
1519 	/* set QoS mask and indices */
1520 	f = &interface->ring_feature[RING_F_QOS];
1521 	f->indices = pcs;
1522 	f->mask = BIT(fls(pcs - 1)) - 1;
1523 
1524 	/* determine the upper limit for our current DCB mode */
1525 	rss_i = interface->hw.mac.max_queues / pcs;
1526 	rss_i = BIT(fls(rss_i) - 1);
1527 
1528 	/* set RSS mask and indices */
1529 	f = &interface->ring_feature[RING_F_RSS];
1530 	rss_i = min_t(u16, rss_i, f->limit);
1531 	f->indices = rss_i;
1532 	f->mask = BIT(fls(rss_i - 1)) - 1;
1533 
1534 	/* configure pause class to queue mapping */
1535 	for (i = 0; i < pcs; i++)
1536 		netdev_set_tc_queue(dev, i, rss_i, rss_i * i);
1537 
1538 	interface->num_rx_queues = rss_i * pcs;
1539 	interface->num_tx_queues = rss_i * pcs;
1540 
1541 	return true;
1542 }
1543 
1544 /**
1545  * fm10k_set_rss_queues: Allocate queues for RSS
1546  * @interface: board private structure to initialize
1547  *
1548  * This is our "base" multiqueue mode.  RSS (Receive Side Scaling) will try
1549  * to allocate one Rx queue per CPU, and if available, one Tx queue per CPU.
1550  *
1551  **/
1552 static bool fm10k_set_rss_queues(struct fm10k_intfc *interface)
1553 {
1554 	struct fm10k_ring_feature *f;
1555 	u16 rss_i;
1556 
1557 	f = &interface->ring_feature[RING_F_RSS];
1558 	rss_i = min_t(u16, interface->hw.mac.max_queues, f->limit);
1559 
1560 	/* record indices and power of 2 mask for RSS */
1561 	f->indices = rss_i;
1562 	f->mask = BIT(fls(rss_i - 1)) - 1;
1563 
1564 	interface->num_rx_queues = rss_i;
1565 	interface->num_tx_queues = rss_i;
1566 
1567 	return true;
1568 }
1569 
1570 /**
1571  * fm10k_set_num_queues: Allocate queues for device, feature dependent
1572  * @interface: board private structure to initialize
1573  *
1574  * This is the top level queue allocation routine.  The order here is very
1575  * important, starting with the "most" number of features turned on at once,
1576  * and ending with the smallest set of features.  This way large combinations
1577  * can be allocated if they're turned on, and smaller combinations are the
1578  * fallthrough conditions.
1579  *
1580  **/
1581 static void fm10k_set_num_queues(struct fm10k_intfc *interface)
1582 {
1583 	/* Attempt to setup QoS and RSS first */
1584 	if (fm10k_set_qos_queues(interface))
1585 		return;
1586 
1587 	/* If we don't have QoS, just fallback to only RSS. */
1588 	fm10k_set_rss_queues(interface);
1589 }
1590 
1591 /**
1592  * fm10k_reset_num_queues - Reset the number of queues to zero
1593  * @interface: board private structure
1594  *
1595  * This function should be called whenever we need to reset the number of
1596  * queues after an error condition.
1597  */
1598 static void fm10k_reset_num_queues(struct fm10k_intfc *interface)
1599 {
1600 	interface->num_tx_queues = 0;
1601 	interface->num_rx_queues = 0;
1602 	interface->num_q_vectors = 0;
1603 }
1604 
1605 /**
1606  * fm10k_alloc_q_vector - Allocate memory for a single interrupt vector
1607  * @interface: board private structure to initialize
1608  * @v_count: q_vectors allocated on interface, used for ring interleaving
1609  * @v_idx: index of vector in interface struct
1610  * @txr_count: total number of Tx rings to allocate
1611  * @txr_idx: index of first Tx ring to allocate
1612  * @rxr_count: total number of Rx rings to allocate
1613  * @rxr_idx: index of first Rx ring to allocate
1614  *
1615  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
1616  **/
1617 static int fm10k_alloc_q_vector(struct fm10k_intfc *interface,
1618 				unsigned int v_count, unsigned int v_idx,
1619 				unsigned int txr_count, unsigned int txr_idx,
1620 				unsigned int rxr_count, unsigned int rxr_idx)
1621 {
1622 	struct fm10k_q_vector *q_vector;
1623 	struct fm10k_ring *ring;
1624 	int ring_count, size;
1625 
1626 	ring_count = txr_count + rxr_count;
1627 	size = sizeof(struct fm10k_q_vector) +
1628 	       (sizeof(struct fm10k_ring) * ring_count);
1629 
1630 	/* allocate q_vector and rings */
1631 	q_vector = kzalloc(size, GFP_KERNEL);
1632 	if (!q_vector)
1633 		return -ENOMEM;
1634 
1635 	/* initialize NAPI */
1636 	netif_napi_add(interface->netdev, &q_vector->napi,
1637 		       fm10k_poll, NAPI_POLL_WEIGHT);
1638 
1639 	/* tie q_vector and interface together */
1640 	interface->q_vector[v_idx] = q_vector;
1641 	q_vector->interface = interface;
1642 	q_vector->v_idx = v_idx;
1643 
1644 	/* initialize pointer to rings */
1645 	ring = q_vector->ring;
1646 
1647 	/* save Tx ring container info */
1648 	q_vector->tx.ring = ring;
1649 	q_vector->tx.work_limit = FM10K_DEFAULT_TX_WORK;
1650 	q_vector->tx.itr = interface->tx_itr;
1651 	q_vector->tx.itr_scale = interface->hw.mac.itr_scale;
1652 	q_vector->tx.count = txr_count;
1653 
1654 	while (txr_count) {
1655 		/* assign generic ring traits */
1656 		ring->dev = &interface->pdev->dev;
1657 		ring->netdev = interface->netdev;
1658 
1659 		/* configure backlink on ring */
1660 		ring->q_vector = q_vector;
1661 
1662 		/* apply Tx specific ring traits */
1663 		ring->count = interface->tx_ring_count;
1664 		ring->queue_index = txr_idx;
1665 
1666 		/* assign ring to interface */
1667 		interface->tx_ring[txr_idx] = ring;
1668 
1669 		/* update count and index */
1670 		txr_count--;
1671 		txr_idx += v_count;
1672 
1673 		/* push pointer to next ring */
1674 		ring++;
1675 	}
1676 
1677 	/* save Rx ring container info */
1678 	q_vector->rx.ring = ring;
1679 	q_vector->rx.itr = interface->rx_itr;
1680 	q_vector->rx.itr_scale = interface->hw.mac.itr_scale;
1681 	q_vector->rx.count = rxr_count;
1682 
1683 	while (rxr_count) {
1684 		/* assign generic ring traits */
1685 		ring->dev = &interface->pdev->dev;
1686 		ring->netdev = interface->netdev;
1687 		rcu_assign_pointer(ring->l2_accel, interface->l2_accel);
1688 
1689 		/* configure backlink on ring */
1690 		ring->q_vector = q_vector;
1691 
1692 		/* apply Rx specific ring traits */
1693 		ring->count = interface->rx_ring_count;
1694 		ring->queue_index = rxr_idx;
1695 
1696 		/* assign ring to interface */
1697 		interface->rx_ring[rxr_idx] = ring;
1698 
1699 		/* update count and index */
1700 		rxr_count--;
1701 		rxr_idx += v_count;
1702 
1703 		/* push pointer to next ring */
1704 		ring++;
1705 	}
1706 
1707 	fm10k_dbg_q_vector_init(q_vector);
1708 
1709 	return 0;
1710 }
1711 
1712 /**
1713  * fm10k_free_q_vector - Free memory allocated for specific interrupt vector
1714  * @interface: board private structure to initialize
1715  * @v_idx: Index of vector to be freed
1716  *
1717  * This function frees the memory allocated to the q_vector.  In addition if
1718  * NAPI is enabled it will delete any references to the NAPI struct prior
1719  * to freeing the q_vector.
1720  **/
1721 static void fm10k_free_q_vector(struct fm10k_intfc *interface, int v_idx)
1722 {
1723 	struct fm10k_q_vector *q_vector = interface->q_vector[v_idx];
1724 	struct fm10k_ring *ring;
1725 
1726 	fm10k_dbg_q_vector_exit(q_vector);
1727 
1728 	fm10k_for_each_ring(ring, q_vector->tx)
1729 		interface->tx_ring[ring->queue_index] = NULL;
1730 
1731 	fm10k_for_each_ring(ring, q_vector->rx)
1732 		interface->rx_ring[ring->queue_index] = NULL;
1733 
1734 	interface->q_vector[v_idx] = NULL;
1735 	netif_napi_del(&q_vector->napi);
1736 	kfree_rcu(q_vector, rcu);
1737 }
1738 
1739 /**
1740  * fm10k_alloc_q_vectors - Allocate memory for interrupt vectors
1741  * @interface: board private structure to initialize
1742  *
1743  * We allocate one q_vector per queue interrupt.  If allocation fails we
1744  * return -ENOMEM.
1745  **/
1746 static int fm10k_alloc_q_vectors(struct fm10k_intfc *interface)
1747 {
1748 	unsigned int q_vectors = interface->num_q_vectors;
1749 	unsigned int rxr_remaining = interface->num_rx_queues;
1750 	unsigned int txr_remaining = interface->num_tx_queues;
1751 	unsigned int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1752 	int err;
1753 
1754 	if (q_vectors >= (rxr_remaining + txr_remaining)) {
1755 		for (; rxr_remaining; v_idx++) {
1756 			err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
1757 						   0, 0, 1, rxr_idx);
1758 			if (err)
1759 				goto err_out;
1760 
1761 			/* update counts and index */
1762 			rxr_remaining--;
1763 			rxr_idx++;
1764 		}
1765 	}
1766 
1767 	for (; v_idx < q_vectors; v_idx++) {
1768 		int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1769 		int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1770 
1771 		err = fm10k_alloc_q_vector(interface, q_vectors, v_idx,
1772 					   tqpv, txr_idx,
1773 					   rqpv, rxr_idx);
1774 
1775 		if (err)
1776 			goto err_out;
1777 
1778 		/* update counts and index */
1779 		rxr_remaining -= rqpv;
1780 		txr_remaining -= tqpv;
1781 		rxr_idx++;
1782 		txr_idx++;
1783 	}
1784 
1785 	return 0;
1786 
1787 err_out:
1788 	fm10k_reset_num_queues(interface);
1789 
1790 	while (v_idx--)
1791 		fm10k_free_q_vector(interface, v_idx);
1792 
1793 	return -ENOMEM;
1794 }
1795 
1796 /**
1797  * fm10k_free_q_vectors - Free memory allocated for interrupt vectors
1798  * @interface: board private structure to initialize
1799  *
1800  * This function frees the memory allocated to the q_vectors.  In addition if
1801  * NAPI is enabled it will delete any references to the NAPI struct prior
1802  * to freeing the q_vector.
1803  **/
1804 static void fm10k_free_q_vectors(struct fm10k_intfc *interface)
1805 {
1806 	int v_idx = interface->num_q_vectors;
1807 
1808 	fm10k_reset_num_queues(interface);
1809 
1810 	while (v_idx--)
1811 		fm10k_free_q_vector(interface, v_idx);
1812 }
1813 
1814 /**
1815  * f10k_reset_msix_capability - reset MSI-X capability
1816  * @interface: board private structure to initialize
1817  *
1818  * Reset the MSI-X capability back to its starting state
1819  **/
1820 static void fm10k_reset_msix_capability(struct fm10k_intfc *interface)
1821 {
1822 	pci_disable_msix(interface->pdev);
1823 	kfree(interface->msix_entries);
1824 	interface->msix_entries = NULL;
1825 }
1826 
1827 /**
1828  * f10k_init_msix_capability - configure MSI-X capability
1829  * @interface: board private structure to initialize
1830  *
1831  * Attempt to configure the interrupts using the best available
1832  * capabilities of the hardware and the kernel.
1833  **/
1834 static int fm10k_init_msix_capability(struct fm10k_intfc *interface)
1835 {
1836 	struct fm10k_hw *hw = &interface->hw;
1837 	int v_budget, vector;
1838 
1839 	/* It's easy to be greedy for MSI-X vectors, but it really
1840 	 * doesn't do us much good if we have a lot more vectors
1841 	 * than CPU's.  So let's be conservative and only ask for
1842 	 * (roughly) the same number of vectors as there are CPU's.
1843 	 * the default is to use pairs of vectors
1844 	 */
1845 	v_budget = max(interface->num_rx_queues, interface->num_tx_queues);
1846 	v_budget = min_t(u16, v_budget, num_online_cpus());
1847 
1848 	/* account for vectors not related to queues */
1849 	v_budget += NON_Q_VECTORS(hw);
1850 
1851 	/* At the same time, hardware can only support a maximum of
1852 	 * hw.mac->max_msix_vectors vectors.  With features
1853 	 * such as RSS and VMDq, we can easily surpass the number of Rx and Tx
1854 	 * descriptor queues supported by our device.  Thus, we cap it off in
1855 	 * those rare cases where the cpu count also exceeds our vector limit.
1856 	 */
1857 	v_budget = min_t(int, v_budget, hw->mac.max_msix_vectors);
1858 
1859 	/* A failure in MSI-X entry allocation is fatal. */
1860 	interface->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
1861 					  GFP_KERNEL);
1862 	if (!interface->msix_entries)
1863 		return -ENOMEM;
1864 
1865 	/* populate entry values */
1866 	for (vector = 0; vector < v_budget; vector++)
1867 		interface->msix_entries[vector].entry = vector;
1868 
1869 	/* Attempt to enable MSI-X with requested value */
1870 	v_budget = pci_enable_msix_range(interface->pdev,
1871 					 interface->msix_entries,
1872 					 MIN_MSIX_COUNT(hw),
1873 					 v_budget);
1874 	if (v_budget < 0) {
1875 		kfree(interface->msix_entries);
1876 		interface->msix_entries = NULL;
1877 		return v_budget;
1878 	}
1879 
1880 	/* record the number of queues available for q_vectors */
1881 	interface->num_q_vectors = v_budget - NON_Q_VECTORS(hw);
1882 
1883 	return 0;
1884 }
1885 
1886 /**
1887  * fm10k_cache_ring_qos - Descriptor ring to register mapping for QoS
1888  * @interface: Interface structure continaining rings and devices
1889  *
1890  * Cache the descriptor ring offsets for Qos
1891  **/
1892 static bool fm10k_cache_ring_qos(struct fm10k_intfc *interface)
1893 {
1894 	struct net_device *dev = interface->netdev;
1895 	int pc, offset, rss_i, i, q_idx;
1896 	u16 pc_stride = interface->ring_feature[RING_F_QOS].mask + 1;
1897 	u8 num_pcs = netdev_get_num_tc(dev);
1898 
1899 	if (num_pcs <= 1)
1900 		return false;
1901 
1902 	rss_i = interface->ring_feature[RING_F_RSS].indices;
1903 
1904 	for (pc = 0, offset = 0; pc < num_pcs; pc++, offset += rss_i) {
1905 		q_idx = pc;
1906 		for (i = 0; i < rss_i; i++) {
1907 			interface->tx_ring[offset + i]->reg_idx = q_idx;
1908 			interface->tx_ring[offset + i]->qos_pc = pc;
1909 			interface->rx_ring[offset + i]->reg_idx = q_idx;
1910 			interface->rx_ring[offset + i]->qos_pc = pc;
1911 			q_idx += pc_stride;
1912 		}
1913 	}
1914 
1915 	return true;
1916 }
1917 
1918 /**
1919  * fm10k_cache_ring_rss - Descriptor ring to register mapping for RSS
1920  * @interface: Interface structure continaining rings and devices
1921  *
1922  * Cache the descriptor ring offsets for RSS
1923  **/
1924 static void fm10k_cache_ring_rss(struct fm10k_intfc *interface)
1925 {
1926 	int i;
1927 
1928 	for (i = 0; i < interface->num_rx_queues; i++)
1929 		interface->rx_ring[i]->reg_idx = i;
1930 
1931 	for (i = 0; i < interface->num_tx_queues; i++)
1932 		interface->tx_ring[i]->reg_idx = i;
1933 }
1934 
1935 /**
1936  * fm10k_assign_rings - Map rings to network devices
1937  * @interface: Interface structure containing rings and devices
1938  *
1939  * This function is meant to go though and configure both the network
1940  * devices so that they contain rings, and configure the rings so that
1941  * they function with their network devices.
1942  **/
1943 static void fm10k_assign_rings(struct fm10k_intfc *interface)
1944 {
1945 	if (fm10k_cache_ring_qos(interface))
1946 		return;
1947 
1948 	fm10k_cache_ring_rss(interface);
1949 }
1950 
1951 static void fm10k_init_reta(struct fm10k_intfc *interface)
1952 {
1953 	u16 i, rss_i = interface->ring_feature[RING_F_RSS].indices;
1954 	u32 reta;
1955 
1956 	/* If the Rx flow indirection table has been configured manually, we
1957 	 * need to maintain it when possible.
1958 	 */
1959 	if (netif_is_rxfh_configured(interface->netdev)) {
1960 		for (i = FM10K_RETA_SIZE; i--;) {
1961 			reta = interface->reta[i];
1962 			if ((((reta << 24) >> 24) < rss_i) &&
1963 			    (((reta << 16) >> 24) < rss_i) &&
1964 			    (((reta <<  8) >> 24) < rss_i) &&
1965 			    (((reta)       >> 24) < rss_i))
1966 				continue;
1967 
1968 			/* this should never happen */
1969 			dev_err(&interface->pdev->dev,
1970 				"RSS indirection table assigned flows out of queue bounds. Reconfiguring.\n");
1971 			goto repopulate_reta;
1972 		}
1973 
1974 		/* do nothing if all of the elements are in bounds */
1975 		return;
1976 	}
1977 
1978 repopulate_reta:
1979 	fm10k_write_reta(interface, NULL);
1980 }
1981 
1982 /**
1983  * fm10k_init_queueing_scheme - Determine proper queueing scheme
1984  * @interface: board private structure to initialize
1985  *
1986  * We determine which queueing scheme to use based on...
1987  * - Hardware queue count (num_*_queues)
1988  *   - defined by miscellaneous hardware support/features (RSS, etc.)
1989  **/
1990 int fm10k_init_queueing_scheme(struct fm10k_intfc *interface)
1991 {
1992 	int err;
1993 
1994 	/* Number of supported queues */
1995 	fm10k_set_num_queues(interface);
1996 
1997 	/* Configure MSI-X capability */
1998 	err = fm10k_init_msix_capability(interface);
1999 	if (err) {
2000 		dev_err(&interface->pdev->dev,
2001 			"Unable to initialize MSI-X capability\n");
2002 		goto err_init_msix;
2003 	}
2004 
2005 	/* Allocate memory for queues */
2006 	err = fm10k_alloc_q_vectors(interface);
2007 	if (err) {
2008 		dev_err(&interface->pdev->dev,
2009 			"Unable to allocate queue vectors\n");
2010 		goto err_alloc_q_vectors;
2011 	}
2012 
2013 	/* Map rings to devices, and map devices to physical queues */
2014 	fm10k_assign_rings(interface);
2015 
2016 	/* Initialize RSS redirection table */
2017 	fm10k_init_reta(interface);
2018 
2019 	return 0;
2020 
2021 err_alloc_q_vectors:
2022 	fm10k_reset_msix_capability(interface);
2023 err_init_msix:
2024 	fm10k_reset_num_queues(interface);
2025 	return err;
2026 }
2027 
2028 /**
2029  * fm10k_clear_queueing_scheme - Clear the current queueing scheme settings
2030  * @interface: board private structure to clear queueing scheme on
2031  *
2032  * We go through and clear queueing specific resources and reset the structure
2033  * to pre-load conditions
2034  **/
2035 void fm10k_clear_queueing_scheme(struct fm10k_intfc *interface)
2036 {
2037 	fm10k_free_q_vectors(interface);
2038 	fm10k_reset_msix_capability(interface);
2039 }
2040