]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/fm10k/fm10k_netdev.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[karo-tx-linux.git] / drivers / net / ethernet / intel / fm10k / fm10k_netdev.c
1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2015 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 "fm10k.h"
22 #include <linux/vmalloc.h>
23 #if IS_ENABLED(CONFIG_FM10K_VXLAN)
24 #include <net/vxlan.h>
25 #endif /* CONFIG_FM10K_VXLAN */
26
27 /**
28  * fm10k_setup_tx_resources - allocate Tx resources (Descriptors)
29  * @tx_ring:    tx descriptor ring (for a specific queue) to setup
30  *
31  * Return 0 on success, negative on failure
32  **/
33 int fm10k_setup_tx_resources(struct fm10k_ring *tx_ring)
34 {
35         struct device *dev = tx_ring->dev;
36         int size;
37
38         size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
39
40         tx_ring->tx_buffer = vzalloc(size);
41         if (!tx_ring->tx_buffer)
42                 goto err;
43
44         u64_stats_init(&tx_ring->syncp);
45
46         /* round up to nearest 4K */
47         tx_ring->size = tx_ring->count * sizeof(struct fm10k_tx_desc);
48         tx_ring->size = ALIGN(tx_ring->size, 4096);
49
50         tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
51                                            &tx_ring->dma, GFP_KERNEL);
52         if (!tx_ring->desc)
53                 goto err;
54
55         return 0;
56
57 err:
58         vfree(tx_ring->tx_buffer);
59         tx_ring->tx_buffer = NULL;
60         return -ENOMEM;
61 }
62
63 /**
64  * fm10k_setup_all_tx_resources - allocate all queues Tx resources
65  * @interface: board private structure
66  *
67  * If this function returns with an error, then it's possible one or
68  * more of the rings is populated (while the rest are not).  It is the
69  * callers duty to clean those orphaned rings.
70  *
71  * Return 0 on success, negative on failure
72  **/
73 static int fm10k_setup_all_tx_resources(struct fm10k_intfc *interface)
74 {
75         int i, err = 0;
76
77         for (i = 0; i < interface->num_tx_queues; i++) {
78                 err = fm10k_setup_tx_resources(interface->tx_ring[i]);
79                 if (!err)
80                         continue;
81
82                 netif_err(interface, probe, interface->netdev,
83                           "Allocation for Tx Queue %u failed\n", i);
84                 goto err_setup_tx;
85         }
86
87         return 0;
88 err_setup_tx:
89         /* rewind the index freeing the rings as we go */
90         while (i--)
91                 fm10k_free_tx_resources(interface->tx_ring[i]);
92         return err;
93 }
94
95 /**
96  * fm10k_setup_rx_resources - allocate Rx resources (Descriptors)
97  * @rx_ring:    rx descriptor ring (for a specific queue) to setup
98  *
99  * Returns 0 on success, negative on failure
100  **/
101 int fm10k_setup_rx_resources(struct fm10k_ring *rx_ring)
102 {
103         struct device *dev = rx_ring->dev;
104         int size;
105
106         size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
107
108         rx_ring->rx_buffer = vzalloc(size);
109         if (!rx_ring->rx_buffer)
110                 goto err;
111
112         u64_stats_init(&rx_ring->syncp);
113
114         /* Round up to nearest 4K */
115         rx_ring->size = rx_ring->count * sizeof(union fm10k_rx_desc);
116         rx_ring->size = ALIGN(rx_ring->size, 4096);
117
118         rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
119                                            &rx_ring->dma, GFP_KERNEL);
120         if (!rx_ring->desc)
121                 goto err;
122
123         return 0;
124 err:
125         vfree(rx_ring->rx_buffer);
126         rx_ring->rx_buffer = NULL;
127         return -ENOMEM;
128 }
129
130 /**
131  * fm10k_setup_all_rx_resources - allocate all queues Rx resources
132  * @interface: board private structure
133  *
134  * If this function returns with an error, then it's possible one or
135  * more of the rings is populated (while the rest are not).  It is the
136  * callers duty to clean those orphaned rings.
137  *
138  * Return 0 on success, negative on failure
139  **/
140 static int fm10k_setup_all_rx_resources(struct fm10k_intfc *interface)
141 {
142         int i, err = 0;
143
144         for (i = 0; i < interface->num_rx_queues; i++) {
145                 err = fm10k_setup_rx_resources(interface->rx_ring[i]);
146                 if (!err)
147                         continue;
148
149                 netif_err(interface, probe, interface->netdev,
150                           "Allocation for Rx Queue %u failed\n", i);
151                 goto err_setup_rx;
152         }
153
154         return 0;
155 err_setup_rx:
156         /* rewind the index freeing the rings as we go */
157         while (i--)
158                 fm10k_free_rx_resources(interface->rx_ring[i]);
159         return err;
160 }
161
162 void fm10k_unmap_and_free_tx_resource(struct fm10k_ring *ring,
163                                       struct fm10k_tx_buffer *tx_buffer)
164 {
165         if (tx_buffer->skb) {
166                 dev_kfree_skb_any(tx_buffer->skb);
167                 if (dma_unmap_len(tx_buffer, len))
168                         dma_unmap_single(ring->dev,
169                                          dma_unmap_addr(tx_buffer, dma),
170                                          dma_unmap_len(tx_buffer, len),
171                                          DMA_TO_DEVICE);
172         } else if (dma_unmap_len(tx_buffer, len)) {
173                 dma_unmap_page(ring->dev,
174                                dma_unmap_addr(tx_buffer, dma),
175                                dma_unmap_len(tx_buffer, len),
176                                DMA_TO_DEVICE);
177         }
178         tx_buffer->next_to_watch = NULL;
179         tx_buffer->skb = NULL;
180         dma_unmap_len_set(tx_buffer, len, 0);
181         /* tx_buffer must be completely set up in the transmit path */
182 }
183
184 /**
185  * fm10k_clean_tx_ring - Free Tx Buffers
186  * @tx_ring: ring to be cleaned
187  **/
188 static void fm10k_clean_tx_ring(struct fm10k_ring *tx_ring)
189 {
190         struct fm10k_tx_buffer *tx_buffer;
191         unsigned long size;
192         u16 i;
193
194         /* ring already cleared, nothing to do */
195         if (!tx_ring->tx_buffer)
196                 return;
197
198         /* Free all the Tx ring sk_buffs */
199         for (i = 0; i < tx_ring->count; i++) {
200                 tx_buffer = &tx_ring->tx_buffer[i];
201                 fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
202         }
203
204         /* reset BQL values */
205         netdev_tx_reset_queue(txring_txq(tx_ring));
206
207         size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
208         memset(tx_ring->tx_buffer, 0, size);
209
210         /* Zero out the descriptor ring */
211         memset(tx_ring->desc, 0, tx_ring->size);
212 }
213
214 /**
215  * fm10k_free_tx_resources - Free Tx Resources per Queue
216  * @tx_ring: Tx descriptor ring for a specific queue
217  *
218  * Free all transmit software resources
219  **/
220 void fm10k_free_tx_resources(struct fm10k_ring *tx_ring)
221 {
222         fm10k_clean_tx_ring(tx_ring);
223
224         vfree(tx_ring->tx_buffer);
225         tx_ring->tx_buffer = NULL;
226
227         /* if not set, then don't free */
228         if (!tx_ring->desc)
229                 return;
230
231         dma_free_coherent(tx_ring->dev, tx_ring->size,
232                           tx_ring->desc, tx_ring->dma);
233         tx_ring->desc = NULL;
234 }
235
236 /**
237  * fm10k_clean_all_tx_rings - Free Tx Buffers for all queues
238  * @interface: board private structure
239  **/
240 void fm10k_clean_all_tx_rings(struct fm10k_intfc *interface)
241 {
242         int i;
243
244         for (i = 0; i < interface->num_tx_queues; i++)
245                 fm10k_clean_tx_ring(interface->tx_ring[i]);
246
247         /* remove any stale timestamp buffers and free them */
248         skb_queue_purge(&interface->ts_tx_skb_queue);
249 }
250
251 /**
252  * fm10k_free_all_tx_resources - Free Tx Resources for All Queues
253  * @interface: board private structure
254  *
255  * Free all transmit software resources
256  **/
257 static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface)
258 {
259         int i = interface->num_tx_queues;
260
261         while (i--)
262                 fm10k_free_tx_resources(interface->tx_ring[i]);
263 }
264
265 /**
266  * fm10k_clean_rx_ring - Free Rx Buffers per Queue
267  * @rx_ring: ring to free buffers from
268  **/
269 static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring)
270 {
271         unsigned long size;
272         u16 i;
273
274         if (!rx_ring->rx_buffer)
275                 return;
276
277         if (rx_ring->skb)
278                 dev_kfree_skb(rx_ring->skb);
279         rx_ring->skb = NULL;
280
281         /* Free all the Rx ring sk_buffs */
282         for (i = 0; i < rx_ring->count; i++) {
283                 struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
284                 /* clean-up will only set page pointer to NULL */
285                 if (!buffer->page)
286                         continue;
287
288                 dma_unmap_page(rx_ring->dev, buffer->dma,
289                                PAGE_SIZE, DMA_FROM_DEVICE);
290                 __free_page(buffer->page);
291
292                 buffer->page = NULL;
293         }
294
295         size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
296         memset(rx_ring->rx_buffer, 0, size);
297
298         /* Zero out the descriptor ring */
299         memset(rx_ring->desc, 0, rx_ring->size);
300
301         rx_ring->next_to_alloc = 0;
302         rx_ring->next_to_clean = 0;
303         rx_ring->next_to_use = 0;
304 }
305
306 /**
307  * fm10k_free_rx_resources - Free Rx Resources
308  * @rx_ring: ring to clean the resources from
309  *
310  * Free all receive software resources
311  **/
312 void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
313 {
314         fm10k_clean_rx_ring(rx_ring);
315
316         vfree(rx_ring->rx_buffer);
317         rx_ring->rx_buffer = NULL;
318
319         /* if not set, then don't free */
320         if (!rx_ring->desc)
321                 return;
322
323         dma_free_coherent(rx_ring->dev, rx_ring->size,
324                           rx_ring->desc, rx_ring->dma);
325
326         rx_ring->desc = NULL;
327 }
328
329 /**
330  * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
331  * @interface: board private structure
332  **/
333 void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
334 {
335         int i;
336
337         for (i = 0; i < interface->num_rx_queues; i++)
338                 fm10k_clean_rx_ring(interface->rx_ring[i]);
339 }
340
341 /**
342  * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
343  * @interface: board private structure
344  *
345  * Free all receive software resources
346  **/
347 static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
348 {
349         int i = interface->num_rx_queues;
350
351         while (i--)
352                 fm10k_free_rx_resources(interface->rx_ring[i]);
353 }
354
355 /**
356  * fm10k_request_glort_range - Request GLORTs for use in configuring rules
357  * @interface: board private structure
358  *
359  * This function allocates a range of glorts for this interface to use.
360  **/
361 static void fm10k_request_glort_range(struct fm10k_intfc *interface)
362 {
363         struct fm10k_hw *hw = &interface->hw;
364         u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
365
366         /* establish GLORT base */
367         interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
368         interface->glort_count = 0;
369
370         /* nothing we can do until mask is allocated */
371         if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
372                 return;
373
374         /* we support 3 possible GLORT configurations.
375          * 1: VFs consume all but the last 1
376          * 2: VFs and PF split glorts with possible gap between
377          * 3: VFs allocated first 64, all others belong to PF
378          */
379         if (mask <= hw->iov.total_vfs) {
380                 interface->glort_count = 1;
381                 interface->glort += mask;
382         } else if (mask < 64) {
383                 interface->glort_count = (mask + 1) / 2;
384                 interface->glort += interface->glort_count;
385         } else {
386                 interface->glort_count = mask - 63;
387                 interface->glort += 64;
388         }
389 }
390
391 /**
392  * fm10k_del_vxlan_port_all
393  * @interface: board private structure
394  *
395  * This function frees the entire vxlan_port list
396  **/
397 static void fm10k_del_vxlan_port_all(struct fm10k_intfc *interface)
398 {
399         struct fm10k_vxlan_port *vxlan_port;
400
401         /* flush all entries from list */
402         vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
403                                               struct fm10k_vxlan_port, list);
404         while (vxlan_port) {
405                 list_del(&vxlan_port->list);
406                 kfree(vxlan_port);
407                 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
408                                                       struct fm10k_vxlan_port,
409                                                       list);
410         }
411 }
412
413 /**
414  * fm10k_restore_vxlan_port
415  * @interface: board private structure
416  *
417  * This function restores the value in the tunnel_cfg register after reset
418  **/
419 static void fm10k_restore_vxlan_port(struct fm10k_intfc *interface)
420 {
421         struct fm10k_hw *hw = &interface->hw;
422         struct fm10k_vxlan_port *vxlan_port;
423
424         /* only the PF supports configuring tunnels */
425         if (hw->mac.type != fm10k_mac_pf)
426                 return;
427
428         vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
429                                               struct fm10k_vxlan_port, list);
430
431         /* restore tunnel configuration register */
432         fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
433                         (vxlan_port ? ntohs(vxlan_port->port) : 0) |
434                         (ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
435 }
436
437 /**
438  * fm10k_add_vxlan_port
439  * @netdev: network interface device structure
440  * @sa_family: Address family of new port
441  * @port: port number used for VXLAN
442  *
443  * This funciton is called when a new VXLAN interface has added a new port
444  * number to the range that is currently in use for VXLAN.  The new port
445  * number is always added to the tail so that the port number list should
446  * match the order in which the ports were allocated.  The head of the list
447  * is always used as the VXLAN port number for offloads.
448  **/
449 static void fm10k_add_vxlan_port(struct net_device *dev,
450                                  sa_family_t sa_family, __be16 port) {
451         struct fm10k_intfc *interface = netdev_priv(dev);
452         struct fm10k_vxlan_port *vxlan_port;
453
454         /* only the PF supports configuring tunnels */
455         if (interface->hw.mac.type != fm10k_mac_pf)
456                 return;
457
458         /* existing ports are pulled out so our new entry is always last */
459         fm10k_vxlan_port_for_each(vxlan_port, interface) {
460                 if ((vxlan_port->port == port) &&
461                     (vxlan_port->sa_family == sa_family)) {
462                         list_del(&vxlan_port->list);
463                         goto insert_tail;
464                 }
465         }
466
467         /* allocate memory to track ports */
468         vxlan_port = kmalloc(sizeof(*vxlan_port), GFP_ATOMIC);
469         if (!vxlan_port)
470                 return;
471         vxlan_port->port = port;
472         vxlan_port->sa_family = sa_family;
473
474 insert_tail:
475         /* add new port value to list */
476         list_add_tail(&vxlan_port->list, &interface->vxlan_port);
477
478         fm10k_restore_vxlan_port(interface);
479 }
480
481 /**
482  * fm10k_del_vxlan_port
483  * @netdev: network interface device structure
484  * @sa_family: Address family of freed port
485  * @port: port number used for VXLAN
486  *
487  * This funciton is called when a new VXLAN interface has freed a port
488  * number from the range that is currently in use for VXLAN.  The freed
489  * port is removed from the list and the new head is used to determine
490  * the port number for offloads.
491  **/
492 static void fm10k_del_vxlan_port(struct net_device *dev,
493                                  sa_family_t sa_family, __be16 port) {
494         struct fm10k_intfc *interface = netdev_priv(dev);
495         struct fm10k_vxlan_port *vxlan_port;
496
497         if (interface->hw.mac.type != fm10k_mac_pf)
498                 return;
499
500         /* find the port in the list and free it */
501         fm10k_vxlan_port_for_each(vxlan_port, interface) {
502                 if ((vxlan_port->port == port) &&
503                     (vxlan_port->sa_family == sa_family)) {
504                         list_del(&vxlan_port->list);
505                         kfree(vxlan_port);
506                         break;
507                 }
508         }
509
510         fm10k_restore_vxlan_port(interface);
511 }
512
513 /**
514  * fm10k_open - Called when a network interface is made active
515  * @netdev: network interface device structure
516  *
517  * Returns 0 on success, negative value on failure
518  *
519  * The open entry point is called when a network interface is made
520  * active by the system (IFF_UP).  At this point all resources needed
521  * for transmit and receive operations are allocated, the interrupt
522  * handler is registered with the OS, the watchdog timer is started,
523  * and the stack is notified that the interface is ready.
524  **/
525 int fm10k_open(struct net_device *netdev)
526 {
527         struct fm10k_intfc *interface = netdev_priv(netdev);
528         int err;
529
530         /* allocate transmit descriptors */
531         err = fm10k_setup_all_tx_resources(interface);
532         if (err)
533                 goto err_setup_tx;
534
535         /* allocate receive descriptors */
536         err = fm10k_setup_all_rx_resources(interface);
537         if (err)
538                 goto err_setup_rx;
539
540         /* allocate interrupt resources */
541         err = fm10k_qv_request_irq(interface);
542         if (err)
543                 goto err_req_irq;
544
545         /* setup GLORT assignment for this port */
546         fm10k_request_glort_range(interface);
547
548         /* Notify the stack of the actual queue counts */
549         err = netif_set_real_num_tx_queues(netdev,
550                                            interface->num_tx_queues);
551         if (err)
552                 goto err_set_queues;
553
554         err = netif_set_real_num_rx_queues(netdev,
555                                            interface->num_rx_queues);
556         if (err)
557                 goto err_set_queues;
558
559 #if IS_ENABLED(CONFIG_FM10K_VXLAN)
560         /* update VXLAN port configuration */
561         vxlan_get_rx_port(netdev);
562
563 #endif
564         fm10k_up(interface);
565
566         return 0;
567
568 err_set_queues:
569         fm10k_qv_free_irq(interface);
570 err_req_irq:
571         fm10k_free_all_rx_resources(interface);
572 err_setup_rx:
573         fm10k_free_all_tx_resources(interface);
574 err_setup_tx:
575         return err;
576 }
577
578 /**
579  * fm10k_close - Disables a network interface
580  * @netdev: network interface device structure
581  *
582  * Returns 0, this is not allowed to fail
583  *
584  * The close entry point is called when an interface is de-activated
585  * by the OS.  The hardware is still under the drivers control, but
586  * needs to be disabled.  A global MAC reset is issued to stop the
587  * hardware, and all transmit and receive resources are freed.
588  **/
589 int fm10k_close(struct net_device *netdev)
590 {
591         struct fm10k_intfc *interface = netdev_priv(netdev);
592
593         fm10k_down(interface);
594
595         fm10k_qv_free_irq(interface);
596
597         fm10k_del_vxlan_port_all(interface);
598
599         fm10k_free_all_tx_resources(interface);
600         fm10k_free_all_rx_resources(interface);
601
602         return 0;
603 }
604
605 static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
606 {
607         struct fm10k_intfc *interface = netdev_priv(dev);
608         unsigned int r_idx = skb->queue_mapping;
609         int err;
610
611         if ((skb->protocol ==  htons(ETH_P_8021Q)) &&
612             !skb_vlan_tag_present(skb)) {
613                 /* FM10K only supports hardware tagging, any tags in frame
614                  * are considered 2nd level or "outer" tags
615                  */
616                 struct vlan_hdr *vhdr;
617                 __be16 proto;
618
619                 /* make sure skb is not shared */
620                 skb = skb_share_check(skb, GFP_ATOMIC);
621                 if (!skb)
622                         return NETDEV_TX_OK;
623
624                 /* make sure there is enough room to move the ethernet header */
625                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
626                         return NETDEV_TX_OK;
627
628                 /* verify the skb head is not shared */
629                 err = skb_cow_head(skb, 0);
630                 if (err)
631                         return NETDEV_TX_OK;
632
633                 /* locate vlan header */
634                 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
635
636                 /* pull the 2 key pieces of data out of it */
637                 __vlan_hwaccel_put_tag(skb,
638                                        htons(ETH_P_8021Q),
639                                        ntohs(vhdr->h_vlan_TCI));
640                 proto = vhdr->h_vlan_encapsulated_proto;
641                 skb->protocol = (ntohs(proto) >= 1536) ? proto :
642                                                          htons(ETH_P_802_2);
643
644                 /* squash it by moving the ethernet addresses up 4 bytes */
645                 memmove(skb->data + VLAN_HLEN, skb->data, 12);
646                 __skb_pull(skb, VLAN_HLEN);
647                 skb_reset_mac_header(skb);
648         }
649
650         /* The minimum packet size for a single buffer is 17B so pad the skb
651          * in order to meet this minimum size requirement.
652          */
653         if (unlikely(skb->len < 17)) {
654                 int pad_len = 17 - skb->len;
655
656                 if (skb_pad(skb, pad_len))
657                         return NETDEV_TX_OK;
658                 __skb_put(skb, pad_len);
659         }
660
661         /* prepare packet for hardware time stamping */
662         if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
663                 fm10k_ts_tx_enqueue(interface, skb);
664
665         if (r_idx >= interface->num_tx_queues)
666                 r_idx %= interface->num_tx_queues;
667
668         err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
669
670         return err;
671 }
672
673 static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
674 {
675         if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
676                 return -EINVAL;
677
678         dev->mtu = new_mtu;
679
680         return 0;
681 }
682
683 /**
684  * fm10k_tx_timeout - Respond to a Tx Hang
685  * @netdev: network interface device structure
686  **/
687 static void fm10k_tx_timeout(struct net_device *netdev)
688 {
689         struct fm10k_intfc *interface = netdev_priv(netdev);
690         bool real_tx_hang = false;
691         int i;
692
693 #define TX_TIMEO_LIMIT 16000
694         for (i = 0; i < interface->num_tx_queues; i++) {
695                 struct fm10k_ring *tx_ring = interface->tx_ring[i];
696
697                 if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
698                         real_tx_hang = true;
699         }
700
701         if (real_tx_hang) {
702                 fm10k_tx_timeout_reset(interface);
703         } else {
704                 netif_info(interface, drv, netdev,
705                            "Fake Tx hang detected with timeout of %d seconds\n",
706                            netdev->watchdog_timeo/HZ);
707
708                 /* fake Tx hang - increase the kernel timeout */
709                 if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
710                         netdev->watchdog_timeo *= 2;
711         }
712 }
713
714 static int fm10k_uc_vlan_unsync(struct net_device *netdev,
715                                 const unsigned char *uc_addr)
716 {
717         struct fm10k_intfc *interface = netdev_priv(netdev);
718         struct fm10k_hw *hw = &interface->hw;
719         u16 glort = interface->glort;
720         u16 vid = interface->vid;
721         bool set = !!(vid / VLAN_N_VID);
722         int err;
723
724         /* drop any leading bits on the VLAN ID */
725         vid &= VLAN_N_VID - 1;
726
727         err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
728         if (err)
729                 return err;
730
731         /* return non-zero value as we are only doing a partial sync/unsync */
732         return 1;
733 }
734
735 static int fm10k_mc_vlan_unsync(struct net_device *netdev,
736                                 const unsigned char *mc_addr)
737 {
738         struct fm10k_intfc *interface = netdev_priv(netdev);
739         struct fm10k_hw *hw = &interface->hw;
740         u16 glort = interface->glort;
741         u16 vid = interface->vid;
742         bool set = !!(vid / VLAN_N_VID);
743         int err;
744
745         /* drop any leading bits on the VLAN ID */
746         vid &= VLAN_N_VID - 1;
747
748         err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
749         if (err)
750                 return err;
751
752         /* return non-zero value as we are only doing a partial sync/unsync */
753         return 1;
754 }
755
756 static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
757 {
758         struct fm10k_intfc *interface = netdev_priv(netdev);
759         struct fm10k_hw *hw = &interface->hw;
760         s32 err;
761         int i;
762
763         /* updates do not apply to VLAN 0 */
764         if (!vid)
765                 return 0;
766
767         if (vid >= VLAN_N_VID)
768                 return -EINVAL;
769
770         /* Verify we have permission to add VLANs */
771         if (hw->mac.vlan_override)
772                 return -EACCES;
773
774         /* update active_vlans bitmask */
775         set_bit(vid, interface->active_vlans);
776         if (!set)
777                 clear_bit(vid, interface->active_vlans);
778
779         /* disable the default VID on ring if we have an active VLAN */
780         for (i = 0; i < interface->num_rx_queues; i++) {
781                 struct fm10k_ring *rx_ring = interface->rx_ring[i];
782                 u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
783
784                 if (test_bit(rx_vid, interface->active_vlans))
785                         rx_ring->vid |= FM10K_VLAN_CLEAR;
786                 else
787                         rx_ring->vid &= ~FM10K_VLAN_CLEAR;
788         }
789
790         /* Do not remove default VID related entries from VLAN and MAC tables */
791         if (!set && vid == hw->mac.default_vid)
792                 return 0;
793
794         /* Do not throw an error if the interface is down. We will sync once
795          * we come up
796          */
797         if (test_bit(__FM10K_DOWN, &interface->state))
798                 return 0;
799
800         fm10k_mbx_lock(interface);
801
802         /* only need to update the VLAN if not in promiscuous mode */
803         if (!(netdev->flags & IFF_PROMISC)) {
804                 err = hw->mac.ops.update_vlan(hw, vid, 0, set);
805                 if (err)
806                         goto err_out;
807         }
808
809         /* update our base MAC address */
810         err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
811                                          vid, set, 0);
812         if (err)
813                 goto err_out;
814
815         /* set vid prior to syncing/unsyncing the VLAN */
816         interface->vid = vid + (set ? VLAN_N_VID : 0);
817
818         /* Update the unicast and multicast address list to add/drop VLAN */
819         __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
820         __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
821
822 err_out:
823         fm10k_mbx_unlock(interface);
824
825         return err;
826 }
827
828 static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
829                                  __always_unused __be16 proto, u16 vid)
830 {
831         /* update VLAN and address table based on changes */
832         return fm10k_update_vid(netdev, vid, true);
833 }
834
835 static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
836                                   __always_unused __be16 proto, u16 vid)
837 {
838         /* update VLAN and address table based on changes */
839         return fm10k_update_vid(netdev, vid, false);
840 }
841
842 static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
843 {
844         struct fm10k_hw *hw = &interface->hw;
845         u16 default_vid = hw->mac.default_vid;
846         u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
847
848         vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
849
850         return vid;
851 }
852
853 static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
854 {
855         struct fm10k_hw *hw = &interface->hw;
856         u32 vid, prev_vid;
857
858         /* loop through and find any gaps in the table */
859         for (vid = 0, prev_vid = 0;
860              prev_vid < VLAN_N_VID;
861              prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
862                 if (prev_vid == vid)
863                         continue;
864
865                 /* send request to clear multiple bits at a time */
866                 prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
867                 hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
868         }
869 }
870
871 static int __fm10k_uc_sync(struct net_device *dev,
872                            const unsigned char *addr, bool sync)
873 {
874         struct fm10k_intfc *interface = netdev_priv(dev);
875         struct fm10k_hw *hw = &interface->hw;
876         u16 vid, glort = interface->glort;
877         s32 err;
878
879         if (!is_valid_ether_addr(addr))
880                 return -EADDRNOTAVAIL;
881
882         /* update table with current entries */
883         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
884              vid < VLAN_N_VID;
885              vid = fm10k_find_next_vlan(interface, vid)) {
886                 err = hw->mac.ops.update_uc_addr(hw, glort, addr,
887                                                   vid, sync, 0);
888                 if (err)
889                         return err;
890         }
891
892         return 0;
893 }
894
895 static int fm10k_uc_sync(struct net_device *dev,
896                          const unsigned char *addr)
897 {
898         return __fm10k_uc_sync(dev, addr, true);
899 }
900
901 static int fm10k_uc_unsync(struct net_device *dev,
902                            const unsigned char *addr)
903 {
904         return __fm10k_uc_sync(dev, addr, false);
905 }
906
907 static int fm10k_set_mac(struct net_device *dev, void *p)
908 {
909         struct fm10k_intfc *interface = netdev_priv(dev);
910         struct fm10k_hw *hw = &interface->hw;
911         struct sockaddr *addr = p;
912         s32 err = 0;
913
914         if (!is_valid_ether_addr(addr->sa_data))
915                 return -EADDRNOTAVAIL;
916
917         if (dev->flags & IFF_UP) {
918                 /* setting MAC address requires mailbox */
919                 fm10k_mbx_lock(interface);
920
921                 err = fm10k_uc_sync(dev, addr->sa_data);
922                 if (!err)
923                         fm10k_uc_unsync(dev, hw->mac.addr);
924
925                 fm10k_mbx_unlock(interface);
926         }
927
928         if (!err) {
929                 ether_addr_copy(dev->dev_addr, addr->sa_data);
930                 ether_addr_copy(hw->mac.addr, addr->sa_data);
931                 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
932         }
933
934         /* if we had a mailbox error suggest trying again */
935         return err ? -EAGAIN : 0;
936 }
937
938 static int __fm10k_mc_sync(struct net_device *dev,
939                            const unsigned char *addr, bool sync)
940 {
941         struct fm10k_intfc *interface = netdev_priv(dev);
942         struct fm10k_hw *hw = &interface->hw;
943         u16 vid, glort = interface->glort;
944
945         /* update table with current entries */
946         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
947              vid < VLAN_N_VID;
948              vid = fm10k_find_next_vlan(interface, vid)) {
949                 hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
950         }
951
952         return 0;
953 }
954
955 static int fm10k_mc_sync(struct net_device *dev,
956                          const unsigned char *addr)
957 {
958         return __fm10k_mc_sync(dev, addr, true);
959 }
960
961 static int fm10k_mc_unsync(struct net_device *dev,
962                            const unsigned char *addr)
963 {
964         return __fm10k_mc_sync(dev, addr, false);
965 }
966
967 static void fm10k_set_rx_mode(struct net_device *dev)
968 {
969         struct fm10k_intfc *interface = netdev_priv(dev);
970         struct fm10k_hw *hw = &interface->hw;
971         int xcast_mode;
972
973         /* no need to update the harwdare if we are not running */
974         if (!(dev->flags & IFF_UP))
975                 return;
976
977         /* determine new mode based on flags */
978         xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
979                      (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
980                      (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
981                      FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
982
983         fm10k_mbx_lock(interface);
984
985         /* update xcast mode first, but only if it changed */
986         if (interface->xcast_mode != xcast_mode) {
987                 /* update VLAN table */
988                 if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
989                         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
990                 if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
991                         fm10k_clear_unused_vlans(interface);
992
993                 /* update xcast mode */
994                 hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
995
996                 /* record updated xcast mode state */
997                 interface->xcast_mode = xcast_mode;
998         }
999
1000         /* synchronize all of the addresses */
1001         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
1002                 __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
1003                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
1004                         __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
1005         }
1006
1007         fm10k_mbx_unlock(interface);
1008 }
1009
1010 void fm10k_restore_rx_state(struct fm10k_intfc *interface)
1011 {
1012         struct net_device *netdev = interface->netdev;
1013         struct fm10k_hw *hw = &interface->hw;
1014         int xcast_mode;
1015         u16 vid, glort;
1016
1017         /* record glort for this interface */
1018         glort = interface->glort;
1019
1020         /* convert interface flags to xcast mode */
1021         if (netdev->flags & IFF_PROMISC)
1022                 xcast_mode = FM10K_XCAST_MODE_PROMISC;
1023         else if (netdev->flags & IFF_ALLMULTI)
1024                 xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
1025         else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
1026                 xcast_mode = FM10K_XCAST_MODE_MULTI;
1027         else
1028                 xcast_mode = FM10K_XCAST_MODE_NONE;
1029
1030         fm10k_mbx_lock(interface);
1031
1032         /* Enable logical port */
1033         hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
1034
1035         /* update VLAN table */
1036         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
1037                                 xcast_mode == FM10K_XCAST_MODE_PROMISC);
1038
1039         /* Add filter for VLAN 0 */
1040         hw->mac.ops.update_vlan(hw, 0, 0, true);
1041
1042         /* update table with current entries */
1043         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
1044              vid < VLAN_N_VID;
1045              vid = fm10k_find_next_vlan(interface, vid)) {
1046                 hw->mac.ops.update_vlan(hw, vid, 0, true);
1047                 hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
1048                                            vid, true, 0);
1049         }
1050
1051         /* update xcast mode before synchronizing addresses */
1052         hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
1053
1054         /* synchronize all of the addresses */
1055         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
1056                 __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
1057                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
1058                         __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
1059         }
1060
1061         fm10k_mbx_unlock(interface);
1062
1063         /* record updated xcast mode state */
1064         interface->xcast_mode = xcast_mode;
1065
1066         /* Restore tunnel configuration */
1067         fm10k_restore_vxlan_port(interface);
1068 }
1069
1070 void fm10k_reset_rx_state(struct fm10k_intfc *interface)
1071 {
1072         struct net_device *netdev = interface->netdev;
1073         struct fm10k_hw *hw = &interface->hw;
1074
1075         fm10k_mbx_lock(interface);
1076
1077         /* clear the logical port state on lower device */
1078         hw->mac.ops.update_lport_state(hw, interface->glort,
1079                                        interface->glort_count, false);
1080
1081         fm10k_mbx_unlock(interface);
1082
1083         /* reset flags to default state */
1084         interface->xcast_mode = FM10K_XCAST_MODE_NONE;
1085
1086         /* clear the sync flag since the lport has been dropped */
1087         __dev_uc_unsync(netdev, NULL);
1088         __dev_mc_unsync(netdev, NULL);
1089 }
1090
1091 /**
1092  * fm10k_get_stats64 - Get System Network Statistics
1093  * @netdev: network interface device structure
1094  * @stats: storage space for 64bit statistics
1095  *
1096  * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
1097  * function replaces fm10k_get_stats for kernels which support it.
1098  */
1099 static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
1100                                                    struct rtnl_link_stats64 *stats)
1101 {
1102         struct fm10k_intfc *interface = netdev_priv(netdev);
1103         struct fm10k_ring *ring;
1104         unsigned int start, i;
1105         u64 bytes, packets;
1106
1107         rcu_read_lock();
1108
1109         for (i = 0; i < interface->num_rx_queues; i++) {
1110                 ring = ACCESS_ONCE(interface->rx_ring[i]);
1111
1112                 if (!ring)
1113                         continue;
1114
1115                 do {
1116                         start = u64_stats_fetch_begin_irq(&ring->syncp);
1117                         packets = ring->stats.packets;
1118                         bytes   = ring->stats.bytes;
1119                 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1120
1121                 stats->rx_packets += packets;
1122                 stats->rx_bytes   += bytes;
1123         }
1124
1125         for (i = 0; i < interface->num_tx_queues; i++) {
1126                 ring = ACCESS_ONCE(interface->tx_ring[i]);
1127
1128                 if (!ring)
1129                         continue;
1130
1131                 do {
1132                         start = u64_stats_fetch_begin_irq(&ring->syncp);
1133                         packets = ring->stats.packets;
1134                         bytes   = ring->stats.bytes;
1135                 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1136
1137                 stats->tx_packets += packets;
1138                 stats->tx_bytes   += bytes;
1139         }
1140
1141         rcu_read_unlock();
1142
1143         /* following stats updated by fm10k_service_task() */
1144         stats->rx_missed_errors = netdev->stats.rx_missed_errors;
1145
1146         return stats;
1147 }
1148
1149 int fm10k_setup_tc(struct net_device *dev, u8 tc)
1150 {
1151         struct fm10k_intfc *interface = netdev_priv(dev);
1152
1153         /* Currently only the PF supports priority classes */
1154         if (tc && (interface->hw.mac.type != fm10k_mac_pf))
1155                 return -EINVAL;
1156
1157         /* Hardware supports up to 8 traffic classes */
1158         if (tc > 8)
1159                 return -EINVAL;
1160
1161         /* Hardware has to reinitialize queues to match packet
1162          * buffer alignment. Unfortunately, the hardware is not
1163          * flexible enough to do this dynamically.
1164          */
1165         if (netif_running(dev))
1166                 fm10k_close(dev);
1167
1168         fm10k_mbx_free_irq(interface);
1169
1170         fm10k_clear_queueing_scheme(interface);
1171
1172         /* we expect the prio_tc map to be repopulated later */
1173         netdev_reset_tc(dev);
1174         netdev_set_num_tc(dev, tc);
1175
1176         fm10k_init_queueing_scheme(interface);
1177
1178         fm10k_mbx_request_irq(interface);
1179
1180         if (netif_running(dev))
1181                 fm10k_open(dev);
1182
1183         /* flag to indicate SWPRI has yet to be updated */
1184         interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
1185
1186         return 0;
1187 }
1188
1189 static int fm10k_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1190 {
1191         switch (cmd) {
1192         case SIOCGHWTSTAMP:
1193                 return fm10k_get_ts_config(netdev, ifr);
1194         case SIOCSHWTSTAMP:
1195                 return fm10k_set_ts_config(netdev, ifr);
1196         default:
1197                 return -EOPNOTSUPP;
1198         }
1199 }
1200
1201 static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
1202                                   struct fm10k_l2_accel *l2_accel)
1203 {
1204         struct fm10k_ring *ring;
1205         int i;
1206
1207         for (i = 0; i < interface->num_rx_queues; i++) {
1208                 ring = interface->rx_ring[i];
1209                 rcu_assign_pointer(ring->l2_accel, l2_accel);
1210         }
1211
1212         interface->l2_accel = l2_accel;
1213 }
1214
1215 static void *fm10k_dfwd_add_station(struct net_device *dev,
1216                                     struct net_device *sdev)
1217 {
1218         struct fm10k_intfc *interface = netdev_priv(dev);
1219         struct fm10k_l2_accel *l2_accel = interface->l2_accel;
1220         struct fm10k_l2_accel *old_l2_accel = NULL;
1221         struct fm10k_dglort_cfg dglort = { 0 };
1222         struct fm10k_hw *hw = &interface->hw;
1223         int size = 0, i;
1224         u16 glort;
1225
1226         /* allocate l2 accel structure if it is not available */
1227         if (!l2_accel) {
1228                 /* verify there is enough free GLORTs to support l2_accel */
1229                 if (interface->glort_count < 7)
1230                         return ERR_PTR(-EBUSY);
1231
1232                 size = offsetof(struct fm10k_l2_accel, macvlan[7]);
1233                 l2_accel = kzalloc(size, GFP_KERNEL);
1234                 if (!l2_accel)
1235                         return ERR_PTR(-ENOMEM);
1236
1237                 l2_accel->size = 7;
1238                 l2_accel->dglort = interface->glort;
1239
1240                 /* update pointers */
1241                 fm10k_assign_l2_accel(interface, l2_accel);
1242         /* do not expand if we are at our limit */
1243         } else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
1244                    (l2_accel->count == (interface->glort_count - 1))) {
1245                 return ERR_PTR(-EBUSY);
1246         /* expand if we have hit the size limit */
1247         } else if (l2_accel->count == l2_accel->size) {
1248                 old_l2_accel = l2_accel;
1249                 size = offsetof(struct fm10k_l2_accel,
1250                                 macvlan[(l2_accel->size * 2) + 1]);
1251                 l2_accel = kzalloc(size, GFP_KERNEL);
1252                 if (!l2_accel)
1253                         return ERR_PTR(-ENOMEM);
1254
1255                 memcpy(l2_accel, old_l2_accel,
1256                        offsetof(struct fm10k_l2_accel,
1257                                 macvlan[old_l2_accel->size]));
1258
1259                 l2_accel->size = (old_l2_accel->size * 2) + 1;
1260
1261                 /* update pointers */
1262                 fm10k_assign_l2_accel(interface, l2_accel);
1263                 kfree_rcu(old_l2_accel, rcu);
1264         }
1265
1266         /* add macvlan to accel table, and record GLORT for position */
1267         for (i = 0; i < l2_accel->size; i++) {
1268                 if (!l2_accel->macvlan[i])
1269                         break;
1270         }
1271
1272         /* record station */
1273         l2_accel->macvlan[i] = sdev;
1274         l2_accel->count++;
1275
1276         /* configure default DGLORT mapping for RSS/DCB */
1277         dglort.idx = fm10k_dglort_pf_rss;
1278         dglort.inner_rss = 1;
1279         dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1280         dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1281         dglort.glort = interface->glort;
1282         dglort.shared_l = fls(l2_accel->size);
1283         hw->mac.ops.configure_dglort_map(hw, &dglort);
1284
1285         /* Add rules for this specific dglort to the switch */
1286         fm10k_mbx_lock(interface);
1287
1288         glort = l2_accel->dglort + 1 + i;
1289         hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_MULTI);
1290         hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, true, 0);
1291
1292         fm10k_mbx_unlock(interface);
1293
1294         return sdev;
1295 }
1296
1297 static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
1298 {
1299         struct fm10k_intfc *interface = netdev_priv(dev);
1300         struct fm10k_l2_accel *l2_accel = ACCESS_ONCE(interface->l2_accel);
1301         struct fm10k_dglort_cfg dglort = { 0 };
1302         struct fm10k_hw *hw = &interface->hw;
1303         struct net_device *sdev = priv;
1304         int i;
1305         u16 glort;
1306
1307         if (!l2_accel)
1308                 return;
1309
1310         /* search table for matching interface */
1311         for (i = 0; i < l2_accel->size; i++) {
1312                 if (l2_accel->macvlan[i] == sdev)
1313                         break;
1314         }
1315
1316         /* exit if macvlan not found */
1317         if (i == l2_accel->size)
1318                 return;
1319
1320         /* Remove any rules specific to this dglort */
1321         fm10k_mbx_lock(interface);
1322
1323         glort = l2_accel->dglort + 1 + i;
1324         hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_NONE);
1325         hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, false, 0);
1326
1327         fm10k_mbx_unlock(interface);
1328
1329         /* record removal */
1330         l2_accel->macvlan[i] = NULL;
1331         l2_accel->count--;
1332
1333         /* configure default DGLORT mapping for RSS/DCB */
1334         dglort.idx = fm10k_dglort_pf_rss;
1335         dglort.inner_rss = 1;
1336         dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1337         dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1338         dglort.glort = interface->glort;
1339         dglort.shared_l = fls(l2_accel->size);
1340         hw->mac.ops.configure_dglort_map(hw, &dglort);
1341
1342         /* If table is empty remove it */
1343         if (l2_accel->count == 0) {
1344                 fm10k_assign_l2_accel(interface, NULL);
1345                 kfree_rcu(l2_accel, rcu);
1346         }
1347 }
1348
1349 static netdev_features_t fm10k_features_check(struct sk_buff *skb,
1350                                               struct net_device *dev,
1351                                               netdev_features_t features)
1352 {
1353         if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
1354                 return features;
1355
1356         return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
1357 }
1358
1359 static const struct net_device_ops fm10k_netdev_ops = {
1360         .ndo_open               = fm10k_open,
1361         .ndo_stop               = fm10k_close,
1362         .ndo_validate_addr      = eth_validate_addr,
1363         .ndo_start_xmit         = fm10k_xmit_frame,
1364         .ndo_set_mac_address    = fm10k_set_mac,
1365         .ndo_change_mtu         = fm10k_change_mtu,
1366         .ndo_tx_timeout         = fm10k_tx_timeout,
1367         .ndo_vlan_rx_add_vid    = fm10k_vlan_rx_add_vid,
1368         .ndo_vlan_rx_kill_vid   = fm10k_vlan_rx_kill_vid,
1369         .ndo_set_rx_mode        = fm10k_set_rx_mode,
1370         .ndo_get_stats64        = fm10k_get_stats64,
1371         .ndo_setup_tc           = fm10k_setup_tc,
1372         .ndo_set_vf_mac         = fm10k_ndo_set_vf_mac,
1373         .ndo_set_vf_vlan        = fm10k_ndo_set_vf_vlan,
1374         .ndo_set_vf_rate        = fm10k_ndo_set_vf_bw,
1375         .ndo_get_vf_config      = fm10k_ndo_get_vf_config,
1376         .ndo_add_vxlan_port     = fm10k_add_vxlan_port,
1377         .ndo_del_vxlan_port     = fm10k_del_vxlan_port,
1378         .ndo_do_ioctl           = fm10k_ioctl,
1379         .ndo_dfwd_add_station   = fm10k_dfwd_add_station,
1380         .ndo_dfwd_del_station   = fm10k_dfwd_del_station,
1381 #ifdef CONFIG_NET_POLL_CONTROLLER
1382         .ndo_poll_controller    = fm10k_netpoll,
1383 #endif
1384         .ndo_features_check     = fm10k_features_check,
1385 };
1386
1387 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
1388
1389 struct net_device *fm10k_alloc_netdev(void)
1390 {
1391         struct fm10k_intfc *interface;
1392         struct net_device *dev;
1393
1394         dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
1395         if (!dev)
1396                 return NULL;
1397
1398         /* set net device and ethtool ops */
1399         dev->netdev_ops = &fm10k_netdev_ops;
1400         fm10k_set_ethtool_ops(dev);
1401
1402         /* configure default debug level */
1403         interface = netdev_priv(dev);
1404         interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
1405
1406         /* configure default features */
1407         dev->features |= NETIF_F_IP_CSUM |
1408                          NETIF_F_IPV6_CSUM |
1409                          NETIF_F_SG |
1410                          NETIF_F_TSO |
1411                          NETIF_F_TSO6 |
1412                          NETIF_F_TSO_ECN |
1413                          NETIF_F_GSO_UDP_TUNNEL |
1414                          NETIF_F_RXHASH |
1415                          NETIF_F_RXCSUM;
1416
1417         /* all features defined to this point should be changeable */
1418         dev->hw_features |= dev->features;
1419
1420         /* allow user to enable L2 forwarding acceleration */
1421         dev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
1422
1423         /* configure VLAN features */
1424         dev->vlan_features |= dev->features;
1425
1426         /* configure tunnel offloads */
1427         dev->hw_enc_features |= NETIF_F_IP_CSUM |
1428                                 NETIF_F_TSO |
1429                                 NETIF_F_TSO6 |
1430                                 NETIF_F_TSO_ECN |
1431                                 NETIF_F_GSO_UDP_TUNNEL |
1432                                 NETIF_F_IPV6_CSUM;
1433
1434         /* we want to leave these both on as we cannot disable VLAN tag
1435          * insertion or stripping on the hardware since it is contained
1436          * in the FTAG and not in the frame itself.
1437          */
1438         dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1439                          NETIF_F_HW_VLAN_CTAG_RX |
1440                          NETIF_F_HW_VLAN_CTAG_FILTER;
1441
1442         dev->priv_flags |= IFF_UNICAST_FLT;
1443
1444         return dev;
1445 }