blob: c51d61f5f715779a7ac40911632ca111d1c979b3 [file] [log] [blame]
Jeff Kirsherae06c702018-03-22 10:08:48 -07001// SPDX-License-Identifier: GPL-2.0
Jacob Keller86641092016-04-07 08:21:21 -07002/* Intel(R) Ethernet Switch Host Interface Driver
Jacob Keller3ee7b3a2017-01-12 15:59:38 -08003 * Copyright(c) 2013 - 2017 Intel Corporation.
Alexander Duyckb3890e32014-09-20 19:46:05 -04004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * The full GNU General Public License is included in this distribution in
15 * the file called "COPYING".
16 *
17 * Contact Information:
18 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
19 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
20 */
21
22#include <linux/types.h>
23#include <linux/module.h>
24#include <net/ipv6.h>
25#include <net/ip.h>
26#include <net/tcp.h>
27#include <linux/if_macvlan.h>
Alexander Duyckb101c962014-09-20 19:50:03 -040028#include <linux/prefetch.h>
Alexander Duyckb3890e32014-09-20 19:46:05 -040029
30#include "fm10k.h"
31
Jacob Kellere9d328d2018-01-18 09:18:57 -080032#define DRV_VERSION "0.23.4-k"
Jacob Keller2d0f76b2016-03-09 16:36:08 -080033#define DRV_SUMMARY "Intel(R) Ethernet Switch Host Interface Driver"
Alexander Duyckb3890e32014-09-20 19:46:05 -040034const char fm10k_driver_version[] = DRV_VERSION;
35char fm10k_driver_name[] = "fm10k";
Jacob Keller2d0f76b2016-03-09 16:36:08 -080036static const char fm10k_driver_string[] = DRV_SUMMARY;
Alexander Duyckb3890e32014-09-20 19:46:05 -040037static const char fm10k_copyright[] =
Jacob Kellere9d328d2018-01-18 09:18:57 -080038 "Copyright(c) 2013 - 2018 Intel Corporation.";
Alexander Duyckb3890e32014-09-20 19:46:05 -040039
40MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
Jacob Keller2d0f76b2016-03-09 16:36:08 -080041MODULE_DESCRIPTION(DRV_SUMMARY);
Alexander Duyckb3890e32014-09-20 19:46:05 -040042MODULE_LICENSE("GPL");
43MODULE_VERSION(DRV_VERSION);
44
Jeff Kirsherb382bb12015-04-03 13:27:05 -070045/* single workqueue for entire fm10k driver */
Bruce Allan07146e22015-11-03 11:35:02 -080046struct workqueue_struct *fm10k_workqueue;
Jeff Kirsherb382bb12015-04-03 13:27:05 -070047
Alexander Duyck6d2ce902014-09-20 19:46:20 -040048/**
49 * fm10k_init_module - Driver Registration Routine
Alexander Duyckb3890e32014-09-20 19:46:05 -040050 *
51 * fm10k_init_module is the first routine called when the driver is
52 * loaded. All it does is register with the PCI subsystem.
53 **/
54static int __init fm10k_init_module(void)
55{
56 pr_info("%s - version %s\n", fm10k_driver_string, fm10k_driver_version);
57 pr_info("%s\n", fm10k_copyright);
58
Jeff Kirsherb382bb12015-04-03 13:27:05 -070059 /* create driver workqueue */
Jacob Keller5e3d0332016-08-25 14:15:39 -070060 fm10k_workqueue = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0,
61 fm10k_driver_name);
Jeff Kirsherb382bb12015-04-03 13:27:05 -070062
Alexander Duyck7461fd92014-09-20 19:53:23 -040063 fm10k_dbg_init();
64
Alexander Duyckb3890e32014-09-20 19:46:05 -040065 return fm10k_register_pci_driver();
66}
67module_init(fm10k_init_module);
68
69/**
70 * fm10k_exit_module - Driver Exit Cleanup Routine
71 *
72 * fm10k_exit_module is called just before the driver is removed
73 * from memory.
74 **/
75static void __exit fm10k_exit_module(void)
76{
77 fm10k_unregister_pci_driver();
Alexander Duyck7461fd92014-09-20 19:53:23 -040078
79 fm10k_dbg_exit();
Jeff Kirsherb382bb12015-04-03 13:27:05 -070080
81 /* destroy driver workqueue */
Jeff Kirsherb382bb12015-04-03 13:27:05 -070082 destroy_workqueue(fm10k_workqueue);
Alexander Duyckb3890e32014-09-20 19:46:05 -040083}
84module_exit(fm10k_exit_module);
Alexander Duyck18283ca2014-09-20 19:48:51 -040085
Alexander Duyckb101c962014-09-20 19:50:03 -040086static bool fm10k_alloc_mapped_page(struct fm10k_ring *rx_ring,
87 struct fm10k_rx_buffer *bi)
88{
89 struct page *page = bi->page;
90 dma_addr_t dma;
91
92 /* Only page will be NULL if buffer was consumed */
93 if (likely(page))
94 return true;
95
96 /* alloc new page for storage */
Alexander Duyck42b17f02014-11-11 09:26:57 -080097 page = dev_alloc_page();
Alexander Duyckb101c962014-09-20 19:50:03 -040098 if (unlikely(!page)) {
99 rx_ring->rx_stats.alloc_failed++;
100 return false;
101 }
102
103 /* map page for use */
104 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
105
106 /* if mapping failed free memory back to system since
107 * there isn't much point in holding memory we can't use
108 */
109 if (dma_mapping_error(rx_ring->dev, dma)) {
110 __free_page(page);
Alexander Duyckb101c962014-09-20 19:50:03 -0400111
112 rx_ring->rx_stats.alloc_failed++;
113 return false;
114 }
115
116 bi->dma = dma;
117 bi->page = page;
118 bi->page_offset = 0;
119
120 return true;
121}
122
123/**
124 * fm10k_alloc_rx_buffers - Replace used receive buffers
125 * @rx_ring: ring to place buffers on
126 * @cleaned_count: number of buffers to replace
127 **/
128void fm10k_alloc_rx_buffers(struct fm10k_ring *rx_ring, u16 cleaned_count)
129{
130 union fm10k_rx_desc *rx_desc;
131 struct fm10k_rx_buffer *bi;
132 u16 i = rx_ring->next_to_use;
133
134 /* nothing to do */
135 if (!cleaned_count)
136 return;
137
138 rx_desc = FM10K_RX_DESC(rx_ring, i);
139 bi = &rx_ring->rx_buffer[i];
140 i -= rx_ring->count;
141
142 do {
143 if (!fm10k_alloc_mapped_page(rx_ring, bi))
144 break;
145
146 /* Refresh the desc even if buffer_addrs didn't change
147 * because each write-back erases this info.
148 */
149 rx_desc->q.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
150
151 rx_desc++;
152 bi++;
153 i++;
154 if (unlikely(!i)) {
155 rx_desc = FM10K_RX_DESC(rx_ring, 0);
156 bi = rx_ring->rx_buffer;
157 i -= rx_ring->count;
158 }
159
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000160 /* clear the status bits for the next_to_use descriptor */
161 rx_desc->d.staterr = 0;
Alexander Duyckb101c962014-09-20 19:50:03 -0400162
163 cleaned_count--;
164 } while (cleaned_count);
165
166 i += rx_ring->count;
167
168 if (rx_ring->next_to_use != i) {
169 /* record the next descriptor to use */
170 rx_ring->next_to_use = i;
171
172 /* update next to alloc since we have filled the ring */
173 rx_ring->next_to_alloc = i;
174
175 /* Force memory writes to complete before letting h/w
176 * know there are new descriptors to fetch. (Only
177 * applicable for weak-ordered memory model archs,
178 * such as IA-64).
179 */
180 wmb();
181
182 /* notify hardware of new descriptors */
183 writel(i, rx_ring->tail);
184 }
185}
186
187/**
188 * fm10k_reuse_rx_page - page flip buffer and store it back on the ring
189 * @rx_ring: rx descriptor ring to store buffers on
190 * @old_buff: donor buffer to have page reused
191 *
192 * Synchronizes page for reuse by the interface
193 **/
194static void fm10k_reuse_rx_page(struct fm10k_ring *rx_ring,
195 struct fm10k_rx_buffer *old_buff)
196{
197 struct fm10k_rx_buffer *new_buff;
198 u16 nta = rx_ring->next_to_alloc;
199
200 new_buff = &rx_ring->rx_buffer[nta];
201
202 /* update, and store next to alloc */
203 nta++;
204 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
205
206 /* transfer page from old buffer to new buffer */
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000207 *new_buff = *old_buff;
Alexander Duyckb101c962014-09-20 19:50:03 -0400208
209 /* sync the buffer for use by the device */
210 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
211 old_buff->page_offset,
212 FM10K_RX_BUFSZ,
213 DMA_FROM_DEVICE);
214}
215
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000216static inline bool fm10k_page_is_reserved(struct page *page)
217{
Michal Hocko2f064f32015-08-21 14:11:51 -0700218 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000219}
220
Alexander Duyckb101c962014-09-20 19:50:03 -0400221static bool fm10k_can_reuse_rx_page(struct fm10k_rx_buffer *rx_buffer,
222 struct page *page,
Jeff Kirsherde445192015-04-03 13:26:56 -0700223 unsigned int __maybe_unused truesize)
Alexander Duyckb101c962014-09-20 19:50:03 -0400224{
225 /* avoid re-using remote pages */
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000226 if (unlikely(fm10k_page_is_reserved(page)))
Alexander Duyckb101c962014-09-20 19:50:03 -0400227 return false;
228
229#if (PAGE_SIZE < 8192)
230 /* if we are only owner of page we can reuse it */
231 if (unlikely(page_count(page) != 1))
232 return false;
233
234 /* flip page offset to other buffer */
235 rx_buffer->page_offset ^= FM10K_RX_BUFSZ;
Alexander Duyckb101c962014-09-20 19:50:03 -0400236#else
237 /* move offset up to the next cache line */
238 rx_buffer->page_offset += truesize;
239
240 if (rx_buffer->page_offset > (PAGE_SIZE - FM10K_RX_BUFSZ))
241 return false;
Alexander Duyckb101c962014-09-20 19:50:03 -0400242#endif
243
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000244 /* Even if we own the page, we are not allowed to use atomic_set()
245 * This would break get_page_unless_zero() users.
246 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700247 page_ref_inc(page);
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000248
Alexander Duyckb101c962014-09-20 19:50:03 -0400249 return true;
250}
251
252/**
253 * fm10k_add_rx_frag - Add contents of Rx buffer to sk_buff
Alexander Duyckb101c962014-09-20 19:50:03 -0400254 * @rx_buffer: buffer containing page to add
Scott Peterson881571c2016-11-03 16:09:41 -0700255 * @size: packet size from rx_desc
Alexander Duyckb101c962014-09-20 19:50:03 -0400256 * @rx_desc: descriptor containing length of buffer written by hardware
257 * @skb: sk_buff to place the data into
258 *
259 * This function will add the data contained in rx_buffer->page to the skb.
260 * This is done either through a direct copy if the data in the buffer is
261 * less than the skb header size, otherwise it will just attach the page as
262 * a frag to the skb.
263 *
264 * The function will then update the page offset if necessary and return
265 * true if the buffer can be reused by the interface.
266 **/
Jeff Kirsherde445192015-04-03 13:26:56 -0700267static bool fm10k_add_rx_frag(struct fm10k_rx_buffer *rx_buffer,
Scott Peterson881571c2016-11-03 16:09:41 -0700268 unsigned int size,
Alexander Duyckb101c962014-09-20 19:50:03 -0400269 union fm10k_rx_desc *rx_desc,
270 struct sk_buff *skb)
271{
272 struct page *page = rx_buffer->page;
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700273 unsigned char *va = page_address(page) + rx_buffer->page_offset;
Alexander Duyckb101c962014-09-20 19:50:03 -0400274#if (PAGE_SIZE < 8192)
275 unsigned int truesize = FM10K_RX_BUFSZ;
276#else
Alexander Duyckfb5677a2016-04-15 13:00:46 -0400277 unsigned int truesize = ALIGN(size, 512);
Alexander Duyckb101c962014-09-20 19:50:03 -0400278#endif
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700279 unsigned int pull_len;
Alexander Duyckb101c962014-09-20 19:50:03 -0400280
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700281 if (unlikely(skb_is_nonlinear(skb)))
282 goto add_tail_frag;
Alexander Duyckb101c962014-09-20 19:50:03 -0400283
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700284 if (likely(size <= FM10K_RX_HDR_LEN)) {
Alexander Duyckb101c962014-09-20 19:50:03 -0400285 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
286
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000287 /* page is not reserved, we can reuse buffer as-is */
288 if (likely(!fm10k_page_is_reserved(page)))
Alexander Duyckb101c962014-09-20 19:50:03 -0400289 return true;
290
291 /* this page cannot be reused so discard it */
Alexander Duyckba5b8dcd2014-11-14 00:56:24 +0000292 __free_page(page);
Alexander Duyckb101c962014-09-20 19:50:03 -0400293 return false;
294 }
295
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700296 /* we need the header to contain the greater of either ETH_HLEN or
297 * 60 bytes if the skb->len is less than 60 for skb_pad.
298 */
299 pull_len = eth_get_headlen(va, FM10K_RX_HDR_LEN);
300
301 /* align pull length to size of long to optimize memcpy performance */
302 memcpy(__skb_put(skb, pull_len), va, ALIGN(pull_len, sizeof(long)));
303
304 /* update all of the pointers */
305 va += pull_len;
306 size -= pull_len;
307
308add_tail_frag:
Alexander Duyckb101c962014-09-20 19:50:03 -0400309 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyck1a8782e2015-04-22 21:49:25 -0700310 (unsigned long)va & ~PAGE_MASK, size, truesize);
Alexander Duyckb101c962014-09-20 19:50:03 -0400311
312 return fm10k_can_reuse_rx_page(rx_buffer, page, truesize);
313}
314
315static struct sk_buff *fm10k_fetch_rx_buffer(struct fm10k_ring *rx_ring,
316 union fm10k_rx_desc *rx_desc,
317 struct sk_buff *skb)
318{
Scott Peterson881571c2016-11-03 16:09:41 -0700319 unsigned int size = le16_to_cpu(rx_desc->w.length);
Alexander Duyckb101c962014-09-20 19:50:03 -0400320 struct fm10k_rx_buffer *rx_buffer;
321 struct page *page;
322
323 rx_buffer = &rx_ring->rx_buffer[rx_ring->next_to_clean];
Alexander Duyckb101c962014-09-20 19:50:03 -0400324 page = rx_buffer->page;
325 prefetchw(page);
326
327 if (likely(!skb)) {
328 void *page_addr = page_address(page) +
329 rx_buffer->page_offset;
330
331 /* prefetch first cache line of first page */
332 prefetch(page_addr);
333#if L1_CACHE_BYTES < 128
334 prefetch(page_addr + L1_CACHE_BYTES);
335#endif
336
337 /* allocate a skb to store the frags */
Alexander Duyck67fd8932014-12-09 19:40:56 -0800338 skb = napi_alloc_skb(&rx_ring->q_vector->napi,
339 FM10K_RX_HDR_LEN);
Alexander Duyckb101c962014-09-20 19:50:03 -0400340 if (unlikely(!skb)) {
341 rx_ring->rx_stats.alloc_failed++;
342 return NULL;
343 }
344
345 /* we will be copying header into skb->data in
346 * pskb_may_pull so it is in our interest to prefetch
347 * it now to avoid a possible cache miss
348 */
349 prefetchw(skb->data);
350 }
351
352 /* we are reusing so sync this buffer for CPU use */
353 dma_sync_single_range_for_cpu(rx_ring->dev,
354 rx_buffer->dma,
355 rx_buffer->page_offset,
Scott Peterson881571c2016-11-03 16:09:41 -0700356 size,
Alexander Duyckb101c962014-09-20 19:50:03 -0400357 DMA_FROM_DEVICE);
358
359 /* pull page into skb */
Scott Peterson881571c2016-11-03 16:09:41 -0700360 if (fm10k_add_rx_frag(rx_buffer, size, rx_desc, skb)) {
Alexander Duyckb101c962014-09-20 19:50:03 -0400361 /* hand second half of page back to the ring */
362 fm10k_reuse_rx_page(rx_ring, rx_buffer);
363 } else {
364 /* we are not reusing the buffer so unmap it */
365 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
366 PAGE_SIZE, DMA_FROM_DEVICE);
367 }
368
369 /* clear contents of rx_buffer */
370 rx_buffer->page = NULL;
371
372 return skb;
373}
374
Alexander Duyck76a540d2014-09-20 19:51:02 -0400375static inline void fm10k_rx_checksum(struct fm10k_ring *ring,
376 union fm10k_rx_desc *rx_desc,
377 struct sk_buff *skb)
378{
379 skb_checksum_none_assert(skb);
380
381 /* Rx checksum disabled via ethtool */
382 if (!(ring->netdev->features & NETIF_F_RXCSUM))
383 return;
384
385 /* TCP/UDP checksum error bit is set */
386 if (fm10k_test_staterr(rx_desc,
387 FM10K_RXD_STATUS_L4E |
388 FM10K_RXD_STATUS_L4E2 |
389 FM10K_RXD_STATUS_IPE |
390 FM10K_RXD_STATUS_IPE2)) {
391 ring->rx_stats.csum_err++;
392 return;
393 }
394
395 /* It must be a TCP or UDP packet with a valid checksum */
396 if (fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS2))
397 skb->encapsulation = true;
398 else if (!fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_L4CS))
399 return;
400
401 skb->ip_summed = CHECKSUM_UNNECESSARY;
Jacob Keller80043f32015-07-01 17:38:36 -0700402
403 ring->rx_stats.csum_good++;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400404}
405
406#define FM10K_RSS_L4_TYPES_MASK \
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800407 (BIT(FM10K_RSSTYPE_IPV4_TCP) | \
408 BIT(FM10K_RSSTYPE_IPV4_UDP) | \
409 BIT(FM10K_RSSTYPE_IPV6_TCP) | \
410 BIT(FM10K_RSSTYPE_IPV6_UDP))
Alexander Duyck76a540d2014-09-20 19:51:02 -0400411
412static inline void fm10k_rx_hash(struct fm10k_ring *ring,
413 union fm10k_rx_desc *rx_desc,
414 struct sk_buff *skb)
415{
416 u16 rss_type;
417
418 if (!(ring->netdev->features & NETIF_F_RXHASH))
419 return;
420
421 rss_type = le16_to_cpu(rx_desc->w.pkt_info) & FM10K_RXD_RSSTYPE_MASK;
422 if (!rss_type)
423 return;
424
425 skb_set_hash(skb, le32_to_cpu(rx_desc->d.rss),
Bruce Allanfcdb0a92015-12-22 13:43:49 -0800426 (BIT(rss_type) & FM10K_RSS_L4_TYPES_MASK) ?
Alexander Duyck76a540d2014-09-20 19:51:02 -0400427 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
428}
429
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -0400430static void fm10k_type_trans(struct fm10k_ring *rx_ring,
Jeff Kirsherde445192015-04-03 13:26:56 -0700431 union fm10k_rx_desc __maybe_unused *rx_desc,
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -0400432 struct sk_buff *skb)
433{
434 struct net_device *dev = rx_ring->netdev;
435 struct fm10k_l2_accel *l2_accel = rcu_dereference_bh(rx_ring->l2_accel);
436
437 /* check to see if DGLORT belongs to a MACVLAN */
438 if (l2_accel) {
439 u16 idx = le16_to_cpu(FM10K_CB(skb)->fi.w.dglort) - 1;
440
441 idx -= l2_accel->dglort;
442 if (idx < l2_accel->size && l2_accel->macvlan[idx])
443 dev = l2_accel->macvlan[idx];
444 else
445 l2_accel = NULL;
446 }
447
Alexander Duyck58918df2017-11-22 10:57:17 -0800448 /* Record Rx queue, or update macvlan statistics */
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -0400449 if (!l2_accel)
Alexander Duyck58918df2017-11-22 10:57:17 -0800450 skb_record_rx_queue(skb, rx_ring->queue_index);
451 else
452 macvlan_count_rx(netdev_priv(dev), skb->len + ETH_HLEN, true,
Alexander Duyck8d80ac42018-04-03 17:16:14 -0400453 false);
454
455 skb->protocol = eth_type_trans(skb, dev);
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -0400456}
457
Alexander Duyckb101c962014-09-20 19:50:03 -0400458/**
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 **/
468static 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
Alexander Duyck76a540d2014-09-20 19:51:02 -0400474 fm10k_rx_hash(rx_ring, rx_desc, skb);
475
476 fm10k_rx_checksum(rx_ring, rx_desc, skb);
477
Jacob Kellerb5db29f02016-11-18 09:45:38 -0800478 FM10K_CB(skb)->tstamp = rx_desc->q.timestamp;
479
Alexander Duyckb101c962014-09-20 19:50:03 -0400480 FM10K_CB(skb)->fi.w.vlan = rx_desc->w.vlan;
481
Alexander Duyckb101c962014-09-20 19:50:03 -0400482 FM10K_CB(skb)->fi.d.glort = rx_desc->d.glort;
483
484 if (rx_desc->w.vlan) {
485 u16 vid = le16_to_cpu(rx_desc->w.vlan);
486
Jacob Kellere71c9312015-06-24 13:34:46 -0700487 if ((vid & VLAN_VID_MASK) != rx_ring->vid)
Alexander Duyckb101c962014-09-20 19:50:03 -0400488 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Jacob Kellere71c9312015-06-24 13:34:46 -0700489 else if (vid & VLAN_PRIO_MASK)
490 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
491 vid & VLAN_PRIO_MASK);
Alexander Duyckb101c962014-09-20 19:50:03 -0400492 }
493
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -0400494 fm10k_type_trans(rx_ring, rx_desc, skb);
Alexander Duyckb101c962014-09-20 19:50:03 -0400495
496 return len;
497}
498
499/**
500 * fm10k_is_non_eop - process handling of non-EOP buffers
501 * @rx_ring: Rx ring being processed
502 * @rx_desc: Rx descriptor for current buffer
503 *
504 * This function updates next to clean. If the buffer is an EOP buffer
505 * this function exits returning false, otherwise it will place the
506 * sk_buff in the next buffer to be chained and return true indicating
507 * that this is in fact a non-EOP buffer.
508 **/
509static bool fm10k_is_non_eop(struct fm10k_ring *rx_ring,
510 union fm10k_rx_desc *rx_desc)
511{
512 u32 ntc = rx_ring->next_to_clean + 1;
513
514 /* fetch, update, and store next to clean */
515 ntc = (ntc < rx_ring->count) ? ntc : 0;
516 rx_ring->next_to_clean = ntc;
517
518 prefetch(FM10K_RX_DESC(rx_ring, ntc));
519
520 if (likely(fm10k_test_staterr(rx_desc, FM10K_RXD_STATUS_EOP)))
521 return false;
522
523 return true;
524}
525
526/**
Alexander Duyckb101c962014-09-20 19:50:03 -0400527 * fm10k_cleanup_headers - Correct corrupted or empty headers
528 * @rx_ring: rx descriptor ring packet is being transacted on
529 * @rx_desc: pointer to the EOP Rx descriptor
530 * @skb: pointer to current skb being fixed
531 *
532 * Address the case where we are pulling data in on pages only
533 * and as such no data is present in the skb header.
534 *
535 * In addition if skb is not at least 60 bytes we need to pad it so that
536 * it is large enough to qualify as a valid Ethernet frame.
537 *
538 * Returns true if an error was encountered and skb was freed.
539 **/
540static bool fm10k_cleanup_headers(struct fm10k_ring *rx_ring,
541 union fm10k_rx_desc *rx_desc,
542 struct sk_buff *skb)
543{
544 if (unlikely((fm10k_test_staterr(rx_desc,
545 FM10K_RXD_STATUS_RXE)))) {
Jacob Keller80043f32015-07-01 17:38:36 -0700546#define FM10K_TEST_RXD_BIT(rxd, bit) \
547 ((rxd)->w.csum_err & cpu_to_le16(bit))
548 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_ERROR))
549 rx_ring->rx_stats.switch_errors++;
550 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_NO_DESCRIPTOR))
551 rx_ring->rx_stats.drops++;
552 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_PP_ERROR))
553 rx_ring->rx_stats.pp_errors++;
554 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_SWITCH_READY))
555 rx_ring->rx_stats.link_errors++;
556 if (FM10K_TEST_RXD_BIT(rx_desc, FM10K_RXD_ERR_TOO_BIG))
557 rx_ring->rx_stats.length_errors++;
Alexander Duyckb101c962014-09-20 19:50:03 -0400558 dev_kfree_skb_any(skb);
559 rx_ring->rx_stats.errors++;
560 return true;
561 }
562
Alexander Duycka94d9e22014-12-03 08:17:39 -0800563 /* if eth_skb_pad returns an error the skb was freed */
564 if (eth_skb_pad(skb))
565 return true;
Alexander Duyckb101c962014-09-20 19:50:03 -0400566
567 return false;
568}
569
570/**
571 * fm10k_receive_skb - helper function to handle rx indications
572 * @q_vector: structure containing interrupt and ring information
573 * @skb: packet to send up
574 **/
575static void fm10k_receive_skb(struct fm10k_q_vector *q_vector,
576 struct sk_buff *skb)
577{
578 napi_gro_receive(&q_vector->napi, skb);
579}
580
Jesse Brandeburg32b3e082015-09-24 16:35:47 -0700581static int fm10k_clean_rx_irq(struct fm10k_q_vector *q_vector,
582 struct fm10k_ring *rx_ring,
583 int budget)
Alexander Duyckb101c962014-09-20 19:50:03 -0400584{
585 struct sk_buff *skb = rx_ring->skb;
586 unsigned int total_bytes = 0, total_packets = 0;
587 u16 cleaned_count = fm10k_desc_unused(rx_ring);
588
Alexander Duyck59486322015-05-01 10:34:38 -0700589 while (likely(total_packets < budget)) {
Alexander Duyckb101c962014-09-20 19:50:03 -0400590 union fm10k_rx_desc *rx_desc;
591
592 /* return some buffers to hardware, one at a time is too slow */
593 if (cleaned_count >= FM10K_RX_BUFFER_WRITE) {
594 fm10k_alloc_rx_buffers(rx_ring, cleaned_count);
595 cleaned_count = 0;
596 }
597
598 rx_desc = FM10K_RX_DESC(rx_ring, rx_ring->next_to_clean);
599
Alexander Duyck124b74c2014-12-11 15:02:28 -0800600 if (!rx_desc->d.staterr)
Alexander Duyckb101c962014-09-20 19:50:03 -0400601 break;
602
603 /* This memory barrier is needed to keep us from reading
604 * any other fields out of the rx_desc until we know the
Alexander Duyck124b74c2014-12-11 15:02:28 -0800605 * descriptor has been written back
Alexander Duyckb101c962014-09-20 19:50:03 -0400606 */
Alexander Duyck124b74c2014-12-11 15:02:28 -0800607 dma_rmb();
Alexander Duyckb101c962014-09-20 19:50:03 -0400608
609 /* retrieve a buffer from the ring */
610 skb = fm10k_fetch_rx_buffer(rx_ring, rx_desc, skb);
611
612 /* exit if we failed to retrieve a buffer */
613 if (!skb)
614 break;
615
616 cleaned_count++;
617
618 /* fetch next buffer in frame if non-eop */
619 if (fm10k_is_non_eop(rx_ring, rx_desc))
620 continue;
621
622 /* verify the packet layout is correct */
623 if (fm10k_cleanup_headers(rx_ring, rx_desc, skb)) {
624 skb = NULL;
625 continue;
626 }
627
628 /* populate checksum, timestamp, VLAN, and protocol */
629 total_bytes += fm10k_process_skb_fields(rx_ring, rx_desc, skb);
630
631 fm10k_receive_skb(q_vector, skb);
632
633 /* reset skb pointer */
634 skb = NULL;
635
636 /* update budget accounting */
637 total_packets++;
Alexander Duyck59486322015-05-01 10:34:38 -0700638 }
Alexander Duyckb101c962014-09-20 19:50:03 -0400639
640 /* place incomplete frames back on ring for completion */
641 rx_ring->skb = skb;
642
643 u64_stats_update_begin(&rx_ring->syncp);
644 rx_ring->stats.packets += total_packets;
645 rx_ring->stats.bytes += total_bytes;
646 u64_stats_update_end(&rx_ring->syncp);
647 q_vector->rx.total_packets += total_packets;
648 q_vector->rx.total_bytes += total_bytes;
649
Jesse Brandeburg32b3e082015-09-24 16:35:47 -0700650 return total_packets;
Alexander Duyckb101c962014-09-20 19:50:03 -0400651}
652
Alexander Duyck76a540d2014-09-20 19:51:02 -0400653#define VXLAN_HLEN (sizeof(struct udphdr) + 8)
654static struct ethhdr *fm10k_port_is_vxlan(struct sk_buff *skb)
655{
656 struct fm10k_intfc *interface = netdev_priv(skb->dev);
Jacob Kellerf92e0e42016-08-26 00:14:34 -0700657 struct fm10k_udp_port *vxlan_port;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400658
659 /* we can only offload a vxlan if we recognize it as such */
660 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
Jacob Kellerf92e0e42016-08-26 00:14:34 -0700661 struct fm10k_udp_port, list);
Alexander Duyck76a540d2014-09-20 19:51:02 -0400662
663 if (!vxlan_port)
664 return NULL;
665 if (vxlan_port->port != udp_hdr(skb)->dest)
666 return NULL;
667
668 /* return offset of udp_hdr plus 8 bytes for VXLAN header */
669 return (struct ethhdr *)(skb_transport_header(skb) + VXLAN_HLEN);
670}
671
672#define FM10K_NVGRE_RESERVED0_FLAGS htons(0x9FFF)
673#define NVGRE_TNI htons(0x2000)
674struct fm10k_nvgre_hdr {
675 __be16 flags;
676 __be16 proto;
677 __be32 tni;
678};
679
680static struct ethhdr *fm10k_gre_is_nvgre(struct sk_buff *skb)
681{
682 struct fm10k_nvgre_hdr *nvgre_hdr;
683 int hlen = ip_hdrlen(skb);
684
685 /* currently only IPv4 is supported due to hlen above */
686 if (vlan_get_protocol(skb) != htons(ETH_P_IP))
687 return NULL;
688
689 /* our transport header should be NVGRE */
690 nvgre_hdr = (struct fm10k_nvgre_hdr *)(skb_network_header(skb) + hlen);
691
692 /* verify all reserved flags are 0 */
693 if (nvgre_hdr->flags & FM10K_NVGRE_RESERVED0_FLAGS)
694 return NULL;
695
Alexander Duyck76a540d2014-09-20 19:51:02 -0400696 /* report start of ethernet header */
697 if (nvgre_hdr->flags & NVGRE_TNI)
698 return (struct ethhdr *)(nvgre_hdr + 1);
699
700 return (struct ethhdr *)(&nvgre_hdr->tni);
701}
702
Matthew Vick5bf33dc2015-01-29 07:17:27 +0000703__be16 fm10k_tx_encap_offload(struct sk_buff *skb)
Alexander Duyck76a540d2014-09-20 19:51:02 -0400704{
Matthew Vick8c1a90a2015-01-29 07:17:22 +0000705 u8 l4_hdr = 0, inner_l4_hdr = 0, inner_l4_hlen;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400706 struct ethhdr *eth_hdr;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400707
Matthew Vick8c1a90a2015-01-29 07:17:22 +0000708 if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
709 skb->inner_protocol != htons(ETH_P_TEB))
Joe Stringerb66b6d92014-11-14 07:47:40 +0000710 return 0;
711
Alexander Duyck76a540d2014-09-20 19:51:02 -0400712 switch (vlan_get_protocol(skb)) {
713 case htons(ETH_P_IP):
714 l4_hdr = ip_hdr(skb)->protocol;
715 break;
716 case htons(ETH_P_IPV6):
717 l4_hdr = ipv6_hdr(skb)->nexthdr;
718 break;
719 default:
720 return 0;
721 }
722
723 switch (l4_hdr) {
724 case IPPROTO_UDP:
725 eth_hdr = fm10k_port_is_vxlan(skb);
726 break;
727 case IPPROTO_GRE:
728 eth_hdr = fm10k_gre_is_nvgre(skb);
729 break;
730 default:
731 return 0;
732 }
733
734 if (!eth_hdr)
735 return 0;
736
737 switch (eth_hdr->h_proto) {
738 case htons(ETH_P_IP):
Matthew Vick8c1a90a2015-01-29 07:17:22 +0000739 inner_l4_hdr = inner_ip_hdr(skb)->protocol;
740 break;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400741 case htons(ETH_P_IPV6):
Matthew Vick8c1a90a2015-01-29 07:17:22 +0000742 inner_l4_hdr = inner_ipv6_hdr(skb)->nexthdr;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400743 break;
744 default:
745 return 0;
746 }
747
Matthew Vick8c1a90a2015-01-29 07:17:22 +0000748 switch (inner_l4_hdr) {
749 case IPPROTO_TCP:
750 inner_l4_hlen = inner_tcp_hdrlen(skb);
751 break;
752 case IPPROTO_UDP:
753 inner_l4_hlen = 8;
754 break;
755 default:
756 return 0;
757 }
758
759 /* The hardware allows tunnel offloads only if the combined inner and
760 * outer header is 184 bytes or less
761 */
762 if (skb_inner_transport_header(skb) + inner_l4_hlen -
763 skb_mac_header(skb) > FM10K_TUNNEL_HEADER_LENGTH)
764 return 0;
765
Alexander Duyck76a540d2014-09-20 19:51:02 -0400766 return eth_hdr->h_proto;
767}
768
769static int fm10k_tso(struct fm10k_ring *tx_ring,
770 struct fm10k_tx_buffer *first)
771{
772 struct sk_buff *skb = first->skb;
773 struct fm10k_tx_desc *tx_desc;
774 unsigned char *th;
775 u8 hdrlen;
776
777 if (skb->ip_summed != CHECKSUM_PARTIAL)
778 return 0;
779
780 if (!skb_is_gso(skb))
781 return 0;
782
783 /* compute header lengths */
784 if (skb->encapsulation) {
785 if (!fm10k_tx_encap_offload(skb))
786 goto err_vxlan;
787 th = skb_inner_transport_header(skb);
788 } else {
789 th = skb_transport_header(skb);
790 }
791
792 /* compute offset from SOF to transport header and add header len */
793 hdrlen = (th - skb->data) + (((struct tcphdr *)th)->doff << 2);
794
795 first->tx_flags |= FM10K_TX_FLAGS_CSUM;
796
797 /* update gso size and bytecount with header size */
798 first->gso_segs = skb_shinfo(skb)->gso_segs;
799 first->bytecount += (first->gso_segs - 1) * hdrlen;
800
801 /* populate Tx descriptor header size and mss */
802 tx_desc = FM10K_TX_DESC(tx_ring, tx_ring->next_to_use);
803 tx_desc->hdrlen = hdrlen;
804 tx_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
805
806 return 1;
Joe Perchesc0ad8ef2017-08-11 09:17:15 -0700807
Alexander Duyck76a540d2014-09-20 19:51:02 -0400808err_vxlan:
809 tx_ring->netdev->features &= ~NETIF_F_GSO_UDP_TUNNEL;
Joe Perchesc0ad8ef2017-08-11 09:17:15 -0700810 if (net_ratelimit())
Alexander Duyck76a540d2014-09-20 19:51:02 -0400811 netdev_err(tx_ring->netdev,
812 "TSO requested for unsupported tunnel, disabling offload\n");
813 return -1;
814}
815
816static void fm10k_tx_csum(struct fm10k_ring *tx_ring,
817 struct fm10k_tx_buffer *first)
818{
819 struct sk_buff *skb = first->skb;
820 struct fm10k_tx_desc *tx_desc;
821 union {
822 struct iphdr *ipv4;
823 struct ipv6hdr *ipv6;
824 u8 *raw;
825 } network_hdr;
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700826 u8 *transport_hdr;
827 __be16 frag_off;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400828 __be16 protocol;
829 u8 l4_hdr = 0;
830
831 if (skb->ip_summed != CHECKSUM_PARTIAL)
832 goto no_csum;
833
834 if (skb->encapsulation) {
835 protocol = fm10k_tx_encap_offload(skb);
836 if (!protocol) {
837 if (skb_checksum_help(skb)) {
838 dev_warn(tx_ring->dev,
839 "failed to offload encap csum!\n");
840 tx_ring->tx_stats.csum_err++;
841 }
842 goto no_csum;
843 }
844 network_hdr.raw = skb_inner_network_header(skb);
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700845 transport_hdr = skb_inner_transport_header(skb);
Alexander Duyck76a540d2014-09-20 19:51:02 -0400846 } else {
847 protocol = vlan_get_protocol(skb);
848 network_hdr.raw = skb_network_header(skb);
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700849 transport_hdr = skb_transport_header(skb);
Alexander Duyck76a540d2014-09-20 19:51:02 -0400850 }
851
852 switch (protocol) {
853 case htons(ETH_P_IP):
854 l4_hdr = network_hdr.ipv4->protocol;
855 break;
856 case htons(ETH_P_IPV6):
857 l4_hdr = network_hdr.ipv6->nexthdr;
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700858 if (likely((transport_hdr - network_hdr.raw) ==
859 sizeof(struct ipv6hdr)))
860 break;
861 ipv6_skip_exthdr(skb, network_hdr.raw - skb->data +
862 sizeof(struct ipv6hdr),
863 &l4_hdr, &frag_off);
864 if (unlikely(frag_off))
865 l4_hdr = NEXTHDR_FRAGMENT;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400866 break;
867 default:
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700868 break;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400869 }
870
871 switch (l4_hdr) {
872 case IPPROTO_TCP:
873 case IPPROTO_UDP:
874 break;
875 case IPPROTO_GRE:
876 if (skb->encapsulation)
877 break;
Jacob Keller523a0b552017-07-10 13:23:07 -0700878 /* fall through */
Alexander Duyck76a540d2014-09-20 19:51:02 -0400879 default:
880 if (unlikely(net_ratelimit())) {
881 dev_warn(tx_ring->dev,
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700882 "partial checksum, version=%d l4 proto=%x\n",
883 protocol, l4_hdr);
Alexander Duyck76a540d2014-09-20 19:51:02 -0400884 }
Jacob Kellerdc1b4c22016-04-07 08:52:53 -0700885 skb_checksum_help(skb);
Alexander Duyck76a540d2014-09-20 19:51:02 -0400886 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;
Jacob Keller80043f32015-07-01 17:38:36 -0700892 tx_ring->tx_stats.csum_good++;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400893
894no_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
906static 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
Alexander Duyckb101c962014-09-20 19:50:03 -0400918static 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
Alexander Duyck2c2b2f02014-10-10 14:30:52 -0700935static 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
Matthew Vickeca32042015-01-31 02:23:05 +0000939 /* Memory barrier before checking head and tail */
Alexander Duyck2c2b2f02014-10-10 14:30:52 -0700940 smp_mb();
941
Matthew Vickeca32042015-01-31 02:23:05 +0000942 /* Check again in a case another CPU has just made room available */
Alexander Duyck2c2b2f02014-10-10 14:30:52 -0700943 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
952static 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
Alexander Duyckb101c962014-09-20 19:50:03 -0400959static 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;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400969 u32 tx_flags = first->tx_flags;
Alexander Duyckb101c962014-09-20 19:50:03 -0400970 u16 i = tx_ring->next_to_use;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400971 u8 flags = fm10k_tx_desc_flags(skb, tx_flags);
Alexander Duyckb101c962014-09-20 19:50:03 -0400972
973 tx_desc = FM10K_TX_DESC(tx_ring, i);
974
975 /* add HW VLAN tag */
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100976 if (skb_vlan_tag_present(skb))
977 tx_desc->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
Alexander Duyckb101c962014-09-20 19:50:03 -0400978 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
Alexander Duyck2c2b2f02014-10-10 14:30:52 -07001052 /* Make sure there is space in the ring for the next send. */
1053 fm10k_maybe_stop_tx(tx_ring, DESC_NEEDED);
Alexander Duyckb101c962014-09-20 19:50:03 -04001054
Alexander Duyck2c2b2f02014-10-10 14:30:52 -07001055 /* 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 }
Alexander Duyckb101c962014-09-20 19:50:03 -04001064
1065 return;
1066dma_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
Alexander Duyckb101c962014-09-20 19:50:03 -04001083netdev_tx_t fm10k_xmit_frame_ring(struct sk_buff *skb,
1084 struct fm10k_ring *tx_ring)
1085{
Alexander Duyckb101c962014-09-20 19:50:03 -04001086 u16 count = TXD_USE_COUNT(skb_headlen(skb));
Jacob Keller03d13a52015-10-16 10:57:11 -07001087 struct fm10k_tx_buffer *first;
1088 unsigned short f;
1089 u32 tx_flags = 0;
1090 int tso;
Alexander Duyckb101c962014-09-20 19:50:03 -04001091
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 */
Alexander Duyckb101c962014-09-20 19:50:03 -04001097 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1098 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Alexander Duyckaae072e2015-06-16 11:47:12 -07001099
Alexander Duyckb101c962014-09-20 19:50:03 -04001100 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
Alexander Duyck76a540d2014-09-20 19:51:02 -04001114 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
Alexander Duyckb101c962014-09-20 19:50:03 -04001120 fm10k_tx_map(tx_ring, first);
1121
Alexander Duyckb101c962014-09-20 19:50:03 -04001122 return NETDEV_TX_OK;
Alexander Duyck76a540d2014-09-20 19:51:02 -04001123
1124out_drop:
1125 dev_kfree_skb_any(first->skb);
1126 first->skb = NULL;
1127
1128 return NETDEV_TX_OK;
Alexander Duyckb101c962014-09-20 19:50:03 -04001129}
1130
1131static u64 fm10k_get_tx_completed(struct fm10k_ring *ring)
1132{
1133 return ring->stats.packets;
1134}
1135
Jacob Keller5b9e4432016-06-09 14:56:05 -07001136/**
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 */
1141u64 fm10k_get_tx_pending(struct fm10k_ring *ring, bool in_sw)
Alexander Duyckb101c962014-09-20 19:50:03 -04001142{
Jacob Keller34bad712016-06-07 16:08:49 -07001143 struct fm10k_intfc *interface = ring->q_vector->interface;
1144 struct fm10k_hw *hw = &interface->hw;
Jacob Keller5b9e4432016-06-09 14:56:05 -07001145 u32 head, tail;
Jacob Keller34bad712016-06-07 16:08:49 -07001146
Jacob Keller5b9e4432016-06-09 14:56:05 -07001147 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 }
Alexander Duyckb101c962014-09-20 19:50:03 -04001154
1155 return ((head <= tail) ? tail : tail + ring->count) - head;
1156}
1157
1158bool 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;
Jacob Keller5b9e4432016-06-09 14:56:05 -07001162 u32 tx_pending = fm10k_get_tx_pending(tx_ring, true);
Alexander Duyckb101c962014-09-20 19:50:03 -04001163
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 */
Jacob Keller46929552017-01-12 15:59:39 -08001178 clear_bit(__FM10K_HANG_CHECK_ARMED, tx_ring->state);
Alexander Duyckb101c962014-09-20 19:50:03 -04001179
1180 return false;
1181 }
1182
1183 /* make sure it is true for two checks in a row */
Jacob Keller46929552017-01-12 15:59:39 -08001184 return test_and_set_bit(__FM10K_HANG_CHECK_ARMED, tx_ring->state);
Alexander Duyckb101c962014-09-20 19:50:03 -04001185}
1186
1187/**
1188 * fm10k_tx_timeout_reset - initiate reset due to Tx timeout
1189 * @interface: driver private struct
1190 **/
1191void fm10k_tx_timeout_reset(struct fm10k_intfc *interface)
1192{
1193 /* Do the reset outside of interrupt context */
Jacob Keller46929552017-01-12 15:59:39 -08001194 if (!test_bit(__FM10K_DOWN, interface->state)) {
Alexander Duyckb101c962014-09-20 19:50:03 -04001195 interface->tx_timeout_count++;
Jacob Keller3ee7b3a2017-01-12 15:59:38 -08001196 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
Alexander Duyckb101c962014-09-20 19:50:03 -04001197 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
Alexander Duyck144d8302016-03-07 09:30:15 -08001205 * @napi_budget: Used to determine if we are in netpoll
Alexander Duyckb101c962014-09-20 19:50:03 -04001206 **/
1207static bool fm10k_clean_tx_irq(struct fm10k_q_vector *q_vector,
Alexander Duyck144d8302016-03-07 09:30:15 -08001208 struct fm10k_ring *tx_ring, int napi_budget)
Alexander Duyckb101c962014-09-20 19:50:03 -04001209{
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
Jacob Keller46929552017-01-12 15:59:39 -08001217 if (test_bit(__FM10K_DOWN, interface->state))
Alexander Duyckb101c962014-09-20 19:50:03 -04001218 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 */
Brian King7b8edcc2017-11-17 11:05:48 -06001232 smp_rmb();
Alexander Duyckb101c962014-09-20 19:50:03 -04001233
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 */
Alexander Duyck144d8302016-03-07 09:30:15 -08001246 napi_consume_skb(tx_buffer->skb, napi_budget);
Alexander Duyckb101c962014-09-20 19:50:03 -04001247
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) &&
Jacob Keller46929552017-01-12 15:59:39 -08001347 !test_bit(__FM10K_DOWN, interface->state)) {
Alexander Duyckb101c962014-09-20 19:50:03 -04001348 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
Alexander Duyck18283ca2014-09-20 19:48:51 -04001357/**
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 **/
1367static void fm10k_update_itr(struct fm10k_ring_container *ring_container)
1368{
Jacob Keller242722d2015-10-16 10:57:07 -07001369 unsigned int avg_wire_size, packets, itr_round;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001370
1371 /* Only update ITR if we are using adaptive setting */
Jacob Keller584373f2015-10-16 10:57:06 -07001372 if (!ITR_IS_ADAPTIVE(ring_container->itr))
Alexander Duyck18283ca2014-09-20 19:48:51 -04001373 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
Jacob Keller242722d2015-10-16 10:57:07 -07001381 /* 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 }
Alexander Duyck18283ca2014-09-20 19:48:51 -04001411
Jacob Keller242722d2015-10-16 10:57:07 -07001412 /* 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 */
Jacob Kellerce4dad22016-06-17 16:21:11 -07001416 itr_round = READ_ONCE(ring_container->itr_scale) + 8;
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001417 avg_wire_size += BIT(itr_round) - 1;
Jacob Keller242722d2015-10-16 10:57:07 -07001418 avg_wire_size >>= itr_round;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001419
1420 /* write back value and retain adaptive flag */
1421 ring_container->itr = avg_wire_size | FM10K_ITR_ADAPTIVE;
1422
1423clear_counts:
1424 ring_container->total_bytes = 0;
1425 ring_container->total_packets = 0;
1426}
1427
1428static 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
1449static 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);
Alexander Duyckb101c962014-09-20 19:50:03 -04001453 struct fm10k_ring *ring;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001454 int per_ring_budget, work_done = 0;
Alexander Duyckb101c962014-09-20 19:50:03 -04001455 bool clean_complete = true;
1456
Alexander Duyck144d8302016-03-07 09:30:15 -08001457 fm10k_for_each_ring(ring, q_vector->tx) {
1458 if (!fm10k_clean_tx_irq(q_vector, ring, budget))
1459 clean_complete = false;
1460 }
Alexander Duyckb101c962014-09-20 19:50:03 -04001461
Alexander Duyck9f872982015-09-22 14:35:35 -07001462 /* Handle case where we are called by netpoll with a budget of 0 */
1463 if (budget <= 0)
1464 return budget;
1465
Alexander Duyckb101c962014-09-20 19:50:03 -04001466 /* 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)
Bruce Allana4fcad62015-10-28 17:19:40 -07001470 per_ring_budget = max(budget / q_vector->rx.count, 1);
Alexander Duyckb101c962014-09-20 19:50:03 -04001471 else
1472 per_ring_budget = budget;
1473
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001474 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;
Alexander Duyck144d8302016-03-07 09:30:15 -08001478 if (work >= per_ring_budget)
1479 clean_complete = false;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001480 }
Alexander Duyckb101c962014-09-20 19:50:03 -04001481
1482 /* If all work not completed, return budget and keep polling */
1483 if (!clean_complete)
1484 return budget;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001485
1486 /* all work done, exit the polling mode */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07001487 napi_complete_done(napi, work_done);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001488
1489 /* re-enable the q_vector */
1490 fm10k_qv_enable(q_vector);
1491
Jacob Kellere5fbfb72016-06-20 10:39:32 -07001492 return min(work_done, budget - 1);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001493}
1494
1495/**
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001496 * 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 **/
1506static 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;
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001522 f->mask = BIT(fls(pcs - 1)) - 1;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001523
1524 /* determine the upper limit for our current DCB mode */
1525 rss_i = interface->hw.mac.max_queues / pcs;
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001526 rss_i = BIT(fls(rss_i) - 1);
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001527
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;
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001532 f->mask = BIT(fls(rss_i - 1)) - 1;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001533
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 **/
1552static 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;
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001562 f->mask = BIT(fls(rss_i - 1)) - 1;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001563
1564 interface->num_rx_queues = rss_i;
1565 interface->num_tx_queues = rss_i;
1566
1567 return true;
1568}
1569
1570/**
Alexander Duyck18283ca2014-09-20 19:48:51 -04001571 * 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 **/
1581static void fm10k_set_num_queues(struct fm10k_intfc *interface)
1582{
Jacob Kellerb3525692016-02-04 10:47:56 -08001583 /* Attempt to setup QoS and RSS first */
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001584 if (fm10k_set_qos_queues(interface))
1585 return;
1586
Jacob Kellerb3525692016-02-04 10:47:56 -08001587 /* If we don't have QoS, just fallback to only RSS. */
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001588 fm10k_set_rss_queues(interface);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001589}
1590
1591/**
Jacob Keller4be37c42016-02-10 14:45:50 -08001592 * 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 */
1598static 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/**
Alexander Duyck18283ca2014-09-20 19:48:51 -04001606 * 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 **/
1617static 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;
Alexander Duycke27ef592014-09-20 19:49:03 -04001623 struct fm10k_ring *ring;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001624 int ring_count, size;
1625
1626 ring_count = txr_count + rxr_count;
Alexander Duycke27ef592014-09-20 19:49:03 -04001627 size = sizeof(struct fm10k_q_vector) +
1628 (sizeof(struct fm10k_ring) * ring_count);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001629
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
Alexander Duycke27ef592014-09-20 19:49:03 -04001644 /* initialize pointer to rings */
1645 ring = q_vector->ring;
1646
Alexander Duyck18283ca2014-09-20 19:48:51 -04001647 /* save Tx ring container info */
Alexander Duycke27ef592014-09-20 19:49:03 -04001648 q_vector->tx.ring = ring;
1649 q_vector->tx.work_limit = FM10K_DEFAULT_TX_WORK;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001650 q_vector->tx.itr = interface->tx_itr;
Jacob Keller242722d2015-10-16 10:57:07 -07001651 q_vector->tx.itr_scale = interface->hw.mac.itr_scale;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001652 q_vector->tx.count = txr_count;
1653
Alexander Duycke27ef592014-09-20 19:49:03 -04001654 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
Alexander Duyck18283ca2014-09-20 19:48:51 -04001677 /* save Rx ring container info */
Alexander Duycke27ef592014-09-20 19:49:03 -04001678 q_vector->rx.ring = ring;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001679 q_vector->rx.itr = interface->rx_itr;
Jacob Keller242722d2015-10-16 10:57:07 -07001680 q_vector->rx.itr_scale = interface->hw.mac.itr_scale;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001681 q_vector->rx.count = rxr_count;
1682
Alexander Duycke27ef592014-09-20 19:49:03 -04001683 while (rxr_count) {
1684 /* assign generic ring traits */
1685 ring->dev = &interface->pdev->dev;
1686 ring->netdev = interface->netdev;
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001687 rcu_assign_pointer(ring->l2_accel, interface->l2_accel);
Alexander Duycke27ef592014-09-20 19:49:03 -04001688
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
Alexander Duyck7461fd92014-09-20 19:53:23 -04001707 fm10k_dbg_q_vector_init(q_vector);
1708
Alexander Duyck18283ca2014-09-20 19:48:51 -04001709 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 **/
1721static 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];
Alexander Duycke27ef592014-09-20 19:49:03 -04001724 struct fm10k_ring *ring;
1725
Alexander Duyck7461fd92014-09-20 19:53:23 -04001726 fm10k_dbg_q_vector_exit(q_vector);
1727
Alexander Duycke27ef592014-09-20 19:49:03 -04001728 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;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001733
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 **/
1746static 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
1787err_out:
Jacob Keller4be37c42016-02-10 14:45:50 -08001788 fm10k_reset_num_queues(interface);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001789
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 **/
1804static void fm10k_free_q_vectors(struct fm10k_intfc *interface)
1805{
1806 int v_idx = interface->num_q_vectors;
1807
Jacob Keller4be37c42016-02-10 14:45:50 -08001808 fm10k_reset_num_queues(interface);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001809
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 **/
1820static 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 **/
1834static 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;
Jacob Keller30e23b72016-06-07 16:09:01 -07001877 return v_budget;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001878 }
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
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001886/**
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 **/
1892static 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 **/
1924static 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 **/
1943static 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
Alexander Duyck18283ca2014-09-20 19:48:51 -04001951static void fm10k_init_reta(struct fm10k_intfc *interface)
1952{
1953 u16 i, rss_i = interface->ring_feature[RING_F_RSS].indices;
Jacob Keller540a5d82016-04-07 08:21:20 -07001954 u32 reta;
Alexander Duyck18283ca2014-09-20 19:48:51 -04001955
Keller, Jacob E10120142016-02-08 16:05:05 -08001956 /* 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)) {
Alexander Duyck18283ca2014-09-20 19:48:51 -04001960 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;
Keller, Jacob E10120142016-02-08 16:05:05 -08001967
1968 /* this should never happen */
1969 dev_err(&interface->pdev->dev,
1970 "RSS indirection table assigned flows out of queue bounds. Reconfiguring.\n");
Alexander Duyck18283ca2014-09-20 19:48:51 -04001971 goto repopulate_reta;
1972 }
1973
1974 /* do nothing if all of the elements are in bounds */
1975 return;
1976 }
1977
1978repopulate_reta:
Jacob Keller540a5d82016-04-07 08:21:20 -07001979 fm10k_write_reta(interface, NULL);
Alexander Duyck18283ca2014-09-20 19:48:51 -04001980}
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 **/
1990int 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");
Jacob Keller4be37c42016-02-10 14:45:50 -08002002 goto err_init_msix;
Alexander Duyck18283ca2014-09-20 19:48:51 -04002003 }
2004
2005 /* Allocate memory for queues */
2006 err = fm10k_alloc_q_vectors(interface);
Alexander Duyck587731e2015-10-27 16:59:12 -07002007 if (err) {
Jacob Keller4be37c42016-02-10 14:45:50 -08002008 dev_err(&interface->pdev->dev,
2009 "Unable to allocate queue vectors\n");
2010 goto err_alloc_q_vectors;
Alexander Duyck587731e2015-10-27 16:59:12 -07002011 }
Alexander Duyck18283ca2014-09-20 19:48:51 -04002012
Alexander Duyckaa3ac822014-09-20 19:50:42 -04002013 /* Map rings to devices, and map devices to physical queues */
2014 fm10k_assign_rings(interface);
2015
Alexander Duyck18283ca2014-09-20 19:48:51 -04002016 /* Initialize RSS redirection table */
2017 fm10k_init_reta(interface);
2018
2019 return 0;
Jacob Keller4be37c42016-02-10 14:45:50 -08002020
2021err_alloc_q_vectors:
2022 fm10k_reset_msix_capability(interface);
2023err_init_msix:
2024 fm10k_reset_num_queues(interface);
2025 return err;
Alexander Duyck18283ca2014-09-20 19:48:51 -04002026}
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 **/
2035void fm10k_clear_queueing_scheme(struct fm10k_intfc *interface)
2036{
2037 fm10k_free_q_vectors(interface);
2038 fm10k_reset_msix_capability(interface);
2039}