fm10k: Add support for netdev offloads
[cascardo/linux.git] / drivers / net / ethernet / intel / fm10k / fm10k_netdev.c
1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2014 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_VXLAN)
24 #include <net/vxlan.h>
25 #endif /* CONFIG_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
248 /**
249  * fm10k_free_all_tx_resources - Free Tx Resources for All Queues
250  * @interface: board private structure
251  *
252  * Free all transmit software resources
253  **/
254 static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface)
255 {
256         int i = interface->num_tx_queues;
257
258         while (i--)
259                 fm10k_free_tx_resources(interface->tx_ring[i]);
260 }
261
262 /**
263  * fm10k_clean_rx_ring - Free Rx Buffers per Queue
264  * @rx_ring: ring to free buffers from
265  **/
266 static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring)
267 {
268         unsigned long size;
269         u16 i;
270
271         if (!rx_ring->rx_buffer)
272                 return;
273
274         if (rx_ring->skb)
275                 dev_kfree_skb(rx_ring->skb);
276         rx_ring->skb = NULL;
277
278         /* Free all the Rx ring sk_buffs */
279         for (i = 0; i < rx_ring->count; i++) {
280                 struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
281                 /* clean-up will only set page pointer to NULL */
282                 if (!buffer->page)
283                         continue;
284
285                 dma_unmap_page(rx_ring->dev, buffer->dma,
286                                PAGE_SIZE, DMA_FROM_DEVICE);
287                 __free_page(buffer->page);
288
289                 buffer->page = NULL;
290         }
291
292         size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
293         memset(rx_ring->rx_buffer, 0, size);
294
295         /* Zero out the descriptor ring */
296         memset(rx_ring->desc, 0, rx_ring->size);
297
298         rx_ring->next_to_alloc = 0;
299         rx_ring->next_to_clean = 0;
300         rx_ring->next_to_use = 0;
301 }
302
303 /**
304  * fm10k_free_rx_resources - Free Rx Resources
305  * @rx_ring: ring to clean the resources from
306  *
307  * Free all receive software resources
308  **/
309 void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
310 {
311         fm10k_clean_rx_ring(rx_ring);
312
313         vfree(rx_ring->rx_buffer);
314         rx_ring->rx_buffer = NULL;
315
316         /* if not set, then don't free */
317         if (!rx_ring->desc)
318                 return;
319
320         dma_free_coherent(rx_ring->dev, rx_ring->size,
321                           rx_ring->desc, rx_ring->dma);
322
323         rx_ring->desc = NULL;
324 }
325
326 /**
327  * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
328  * @interface: board private structure
329  **/
330 void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
331 {
332         int i;
333
334         for (i = 0; i < interface->num_rx_queues; i++)
335                 fm10k_clean_rx_ring(interface->rx_ring[i]);
336 }
337
338 /**
339  * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
340  * @interface: board private structure
341  *
342  * Free all receive software resources
343  **/
344 static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
345 {
346         int i = interface->num_rx_queues;
347
348         while (i--)
349                 fm10k_free_rx_resources(interface->rx_ring[i]);
350 }
351
352 /**
353  * fm10k_request_glort_range - Request GLORTs for use in configuring rules
354  * @interface: board private structure
355  *
356  * This function allocates a range of glorts for this inteface to use.
357  **/
358 static void fm10k_request_glort_range(struct fm10k_intfc *interface)
359 {
360         struct fm10k_hw *hw = &interface->hw;
361         u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
362
363         /* establish GLORT base */
364         interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
365         interface->glort_count = 0;
366
367         /* nothing we can do until mask is allocated */
368         if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
369                 return;
370
371         interface->glort_count = mask + 1;
372 }
373
374 /**
375  * fm10k_del_vxlan_port_all
376  * @interface: board private structure
377  *
378  * This function frees the entire vxlan_port list
379  **/
380 static void fm10k_del_vxlan_port_all(struct fm10k_intfc *interface)
381 {
382         struct fm10k_vxlan_port *vxlan_port;
383
384         /* flush all entries from list */
385         vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
386                                               struct fm10k_vxlan_port, list);
387         while (vxlan_port) {
388                 list_del(&vxlan_port->list);
389                 kfree(vxlan_port);
390                 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
391                                                       struct fm10k_vxlan_port,
392                                                       list);
393         }
394 }
395
396 /**
397  * fm10k_restore_vxlan_port
398  * @interface: board private structure
399  *
400  * This function restores the value in the tunnel_cfg register after reset
401  **/
402 static void fm10k_restore_vxlan_port(struct fm10k_intfc *interface)
403 {
404         struct fm10k_hw *hw = &interface->hw;
405         struct fm10k_vxlan_port *vxlan_port;
406
407         /* only the PF supports configuring tunnels */
408         if (hw->mac.type != fm10k_mac_pf)
409                 return;
410
411         vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
412                                               struct fm10k_vxlan_port, list);
413
414         /* restore tunnel configuration register */
415         fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
416                         (vxlan_port ? ntohs(vxlan_port->port) : 0) |
417                         (ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
418 }
419
420 /**
421  * fm10k_add_vxlan_port
422  * @netdev: network interface device structure
423  * @sa_family: Address family of new port
424  * @port: port number used for VXLAN
425  *
426  * This funciton is called when a new VXLAN interface has added a new port
427  * number to the range that is currently in use for VXLAN.  The new port
428  * number is always added to the tail so that the port number list should
429  * match the order in which the ports were allocated.  The head of the list
430  * is always used as the VXLAN port number for offloads.
431  **/
432 static void fm10k_add_vxlan_port(struct net_device *dev,
433                                  sa_family_t sa_family, __be16 port) {
434         struct fm10k_intfc *interface = netdev_priv(dev);
435         struct fm10k_vxlan_port *vxlan_port;
436
437         /* only the PF supports configuring tunnels */
438         if (interface->hw.mac.type != fm10k_mac_pf)
439                 return;
440
441         /* existing ports are pulled out so our new entry is always last */
442         fm10k_vxlan_port_for_each(vxlan_port, interface) {
443                 if ((vxlan_port->port == port) &&
444                     (vxlan_port->sa_family == sa_family)) {
445                         list_del(&vxlan_port->list);
446                         goto insert_tail;
447                 }
448         }
449
450         /* allocate memory to track ports */
451         vxlan_port = kmalloc(sizeof(*vxlan_port), GFP_ATOMIC);
452         if (!vxlan_port)
453                 return;
454         vxlan_port->port = port;
455         vxlan_port->sa_family = sa_family;
456
457 insert_tail:
458         /* add new port value to list */
459         list_add_tail(&vxlan_port->list, &interface->vxlan_port);
460
461         fm10k_restore_vxlan_port(interface);
462 }
463
464 /**
465  * fm10k_del_vxlan_port
466  * @netdev: network interface device structure
467  * @sa_family: Address family of freed port
468  * @port: port number used for VXLAN
469  *
470  * This funciton is called when a new VXLAN interface has freed a port
471  * number from the range that is currently in use for VXLAN.  The freed
472  * port is removed from the list and the new head is used to determine
473  * the port number for offloads.
474  **/
475 static void fm10k_del_vxlan_port(struct net_device *dev,
476                                  sa_family_t sa_family, __be16 port) {
477         struct fm10k_intfc *interface = netdev_priv(dev);
478         struct fm10k_vxlan_port *vxlan_port;
479
480         if (interface->hw.mac.type != fm10k_mac_pf)
481                 return;
482
483         /* find the port in the list and free it */
484         fm10k_vxlan_port_for_each(vxlan_port, interface) {
485                 if ((vxlan_port->port == port) &&
486                     (vxlan_port->sa_family == sa_family)) {
487                         list_del(&vxlan_port->list);
488                         kfree(vxlan_port);
489                         break;
490                 }
491         }
492
493         fm10k_restore_vxlan_port(interface);
494 }
495
496 /**
497  * fm10k_open - Called when a network interface is made active
498  * @netdev: network interface device structure
499  *
500  * Returns 0 on success, negative value on failure
501  *
502  * The open entry point is called when a network interface is made
503  * active by the system (IFF_UP).  At this point all resources needed
504  * for transmit and receive operations are allocated, the interrupt
505  * handler is registered with the OS, the watchdog timer is started,
506  * and the stack is notified that the interface is ready.
507  **/
508 int fm10k_open(struct net_device *netdev)
509 {
510         struct fm10k_intfc *interface = netdev_priv(netdev);
511         int err;
512
513         /* allocate transmit descriptors */
514         err = fm10k_setup_all_tx_resources(interface);
515         if (err)
516                 goto err_setup_tx;
517
518         /* allocate receive descriptors */
519         err = fm10k_setup_all_rx_resources(interface);
520         if (err)
521                 goto err_setup_rx;
522
523         /* allocate interrupt resources */
524         err = fm10k_qv_request_irq(interface);
525         if (err)
526                 goto err_req_irq;
527
528         /* setup GLORT assignment for this port */
529         fm10k_request_glort_range(interface);
530
531         /* Notify the stack of the actual queue counts */
532
533         err = netif_set_real_num_rx_queues(netdev,
534                                            interface->num_rx_queues);
535         if (err)
536                 goto err_set_queues;
537
538 #if IS_ENABLED(CONFIG_VXLAN)
539         /* update VXLAN port configuration */
540         vxlan_get_rx_port(netdev);
541
542 #endif
543         fm10k_up(interface);
544
545         return 0;
546
547 err_set_queues:
548         fm10k_qv_free_irq(interface);
549 err_req_irq:
550         fm10k_free_all_rx_resources(interface);
551 err_setup_rx:
552         fm10k_free_all_tx_resources(interface);
553 err_setup_tx:
554         return err;
555 }
556
557 /**
558  * fm10k_close - Disables a network interface
559  * @netdev: network interface device structure
560  *
561  * Returns 0, this is not allowed to fail
562  *
563  * The close entry point is called when an interface is de-activated
564  * by the OS.  The hardware is still under the drivers control, but
565  * needs to be disabled.  A global MAC reset is issued to stop the
566  * hardware, and all transmit and receive resources are freed.
567  **/
568 int fm10k_close(struct net_device *netdev)
569 {
570         struct fm10k_intfc *interface = netdev_priv(netdev);
571
572         fm10k_down(interface);
573
574         fm10k_qv_free_irq(interface);
575
576         fm10k_del_vxlan_port_all(interface);
577
578         fm10k_free_all_tx_resources(interface);
579         fm10k_free_all_rx_resources(interface);
580
581         return 0;
582 }
583
584 static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
585 {
586         struct fm10k_intfc *interface = netdev_priv(dev);
587         unsigned int r_idx = 0;
588         int err;
589
590         if ((skb->protocol ==  htons(ETH_P_8021Q)) &&
591             !vlan_tx_tag_present(skb)) {
592                 /* FM10K only supports hardware tagging, any tags in frame
593                  * are considered 2nd level or "outer" tags
594                  */
595                 struct vlan_hdr *vhdr;
596                 __be16 proto;
597
598                 /* make sure skb is not shared */
599                 skb = skb_share_check(skb, GFP_ATOMIC);
600                 if (!skb)
601                         return NETDEV_TX_OK;
602
603                 /* make sure there is enough room to move the ethernet header */
604                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
605                         return NETDEV_TX_OK;
606
607                 /* verify the skb head is not shared */
608                 err = skb_cow_head(skb, 0);
609                 if (err)
610                         return NETDEV_TX_OK;
611
612                 /* locate vlan header */
613                 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
614
615                 /* pull the 2 key pieces of data out of it */
616                 __vlan_hwaccel_put_tag(skb,
617                                        htons(ETH_P_8021Q),
618                                        ntohs(vhdr->h_vlan_TCI));
619                 proto = vhdr->h_vlan_encapsulated_proto;
620                 skb->protocol = (ntohs(proto) >= 1536) ? proto :
621                                                          htons(ETH_P_802_2);
622
623                 /* squash it by moving the ethernet addresses up 4 bytes */
624                 memmove(skb->data + VLAN_HLEN, skb->data, 12);
625                 __skb_pull(skb, VLAN_HLEN);
626                 skb_reset_mac_header(skb);
627         }
628
629         /* The minimum packet size for a single buffer is 17B so pad the skb
630          * in order to meet this minimum size requirement.
631          */
632         if (unlikely(skb->len < 17)) {
633                 int pad_len = 17 - skb->len;
634
635                 if (skb_pad(skb, pad_len))
636                         return NETDEV_TX_OK;
637                 __skb_put(skb, pad_len);
638         }
639
640         if (r_idx >= interface->num_tx_queues)
641                 r_idx %= interface->num_tx_queues;
642
643         err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
644
645         return err;
646 }
647
648 static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
649 {
650         if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
651                 return -EINVAL;
652
653         dev->mtu = new_mtu;
654
655         return 0;
656 }
657
658 /**
659  * fm10k_tx_timeout - Respond to a Tx Hang
660  * @netdev: network interface device structure
661  **/
662 static void fm10k_tx_timeout(struct net_device *netdev)
663 {
664         struct fm10k_intfc *interface = netdev_priv(netdev);
665         bool real_tx_hang = false;
666         int i;
667
668 #define TX_TIMEO_LIMIT 16000
669         for (i = 0; i < interface->num_tx_queues; i++) {
670                 struct fm10k_ring *tx_ring = interface->tx_ring[i];
671
672                 if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
673                         real_tx_hang = true;
674         }
675
676         if (real_tx_hang) {
677                 fm10k_tx_timeout_reset(interface);
678         } else {
679                 netif_info(interface, drv, netdev,
680                            "Fake Tx hang detected with timeout of %d seconds\n",
681                            netdev->watchdog_timeo/HZ);
682
683                 /* fake Tx hang - increase the kernel timeout */
684                 if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
685                         netdev->watchdog_timeo *= 2;
686         }
687 }
688
689 static int fm10k_uc_vlan_unsync(struct net_device *netdev,
690                                 const unsigned char *uc_addr)
691 {
692         struct fm10k_intfc *interface = netdev_priv(netdev);
693         struct fm10k_hw *hw = &interface->hw;
694         u16 glort = interface->glort;
695         u16 vid = interface->vid;
696         bool set = !!(vid / VLAN_N_VID);
697         int err;
698
699         /* drop any leading bits on the VLAN ID */
700         vid &= VLAN_N_VID - 1;
701
702         err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
703         if (err)
704                 return err;
705
706         /* return non-zero value as we are only doing a partial sync/unsync */
707         return 1;
708 }
709
710 static int fm10k_mc_vlan_unsync(struct net_device *netdev,
711                                 const unsigned char *mc_addr)
712 {
713         struct fm10k_intfc *interface = netdev_priv(netdev);
714         struct fm10k_hw *hw = &interface->hw;
715         u16 glort = interface->glort;
716         u16 vid = interface->vid;
717         bool set = !!(vid / VLAN_N_VID);
718         int err;
719
720         /* drop any leading bits on the VLAN ID */
721         vid &= VLAN_N_VID - 1;
722
723         err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
724         if (err)
725                 return err;
726
727         /* return non-zero value as we are only doing a partial sync/unsync */
728         return 1;
729 }
730
731 static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
732 {
733         struct fm10k_intfc *interface = netdev_priv(netdev);
734         struct fm10k_hw *hw = &interface->hw;
735         s32 err;
736
737         /* updates do not apply to VLAN 0 */
738         if (!vid)
739                 return 0;
740
741         if (vid >= VLAN_N_VID)
742                 return -EINVAL;
743
744         /* Verify we have permission to add VLANs */
745         if (hw->mac.vlan_override)
746                 return -EACCES;
747
748         /* if default VLAN is already present do nothing */
749         if (vid == hw->mac.default_vid)
750                 return -EBUSY;
751
752         /* update active_vlans bitmask */
753         set_bit(vid, interface->active_vlans);
754         if (!set)
755                 clear_bit(vid, interface->active_vlans);
756
757         fm10k_mbx_lock(interface);
758
759         /* only need to update the VLAN if not in promiscous mode */
760         if (!(netdev->flags & IFF_PROMISC)) {
761                 err = hw->mac.ops.update_vlan(hw, vid, 0, set);
762                 if (err)
763                         return err;
764         }
765
766         /* update our base MAC address */
767         err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
768                                          vid, set, 0);
769         if (err)
770                 return err;
771
772         /* set vid prior to syncing/unsyncing the VLAN */
773         interface->vid = vid + (set ? VLAN_N_VID : 0);
774
775         /* Update the unicast and multicast address list to add/drop VLAN */
776         __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
777         __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
778
779         fm10k_mbx_unlock(interface);
780
781         return 0;
782 }
783
784 static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
785                                  __always_unused __be16 proto, u16 vid)
786 {
787         /* update VLAN and address table based on changes */
788         return fm10k_update_vid(netdev, vid, true);
789 }
790
791 static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
792                                   __always_unused __be16 proto, u16 vid)
793 {
794         /* update VLAN and address table based on changes */
795         return fm10k_update_vid(netdev, vid, false);
796 }
797
798 static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
799 {
800         struct fm10k_hw *hw = &interface->hw;
801         u16 default_vid = hw->mac.default_vid;
802         u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
803
804         vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
805
806         return vid;
807 }
808
809 static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
810 {
811         struct fm10k_hw *hw = &interface->hw;
812         u32 vid, prev_vid;
813
814         /* loop through and find any gaps in the table */
815         for (vid = 0, prev_vid = 0;
816              prev_vid < VLAN_N_VID;
817              prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
818                 if (prev_vid == vid)
819                         continue;
820
821                 /* send request to clear multiple bits at a time */
822                 prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
823                 hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
824         }
825 }
826
827 static int __fm10k_uc_sync(struct net_device *dev,
828                            const unsigned char *addr, bool sync)
829 {
830         struct fm10k_intfc *interface = netdev_priv(dev);
831         struct fm10k_hw *hw = &interface->hw;
832         u16 vid, glort = interface->glort;
833         s32 err;
834
835         if (!is_valid_ether_addr(addr))
836                 return -EADDRNOTAVAIL;
837
838         /* update table with current entries */
839         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
840              vid < VLAN_N_VID;
841              vid = fm10k_find_next_vlan(interface, vid)) {
842                 err = hw->mac.ops.update_uc_addr(hw, glort, addr,
843                                                   vid, sync, 0);
844                 if (err)
845                         return err;
846         }
847
848         return 0;
849 }
850
851 static int fm10k_uc_sync(struct net_device *dev,
852                          const unsigned char *addr)
853 {
854         return __fm10k_uc_sync(dev, addr, true);
855 }
856
857 static int fm10k_uc_unsync(struct net_device *dev,
858                            const unsigned char *addr)
859 {
860         return __fm10k_uc_sync(dev, addr, false);
861 }
862
863 static int fm10k_set_mac(struct net_device *dev, void *p)
864 {
865         struct fm10k_intfc *interface = netdev_priv(dev);
866         struct fm10k_hw *hw = &interface->hw;
867         struct sockaddr *addr = p;
868         s32 err = 0;
869
870         if (!is_valid_ether_addr(addr->sa_data))
871                 return -EADDRNOTAVAIL;
872
873         if (dev->flags & IFF_UP) {
874                 /* setting MAC address requires mailbox */
875                 fm10k_mbx_lock(interface);
876
877                 err = fm10k_uc_sync(dev, addr->sa_data);
878                 if (!err)
879                         fm10k_uc_unsync(dev, hw->mac.addr);
880
881                 fm10k_mbx_unlock(interface);
882         }
883
884         if (!err) {
885                 ether_addr_copy(dev->dev_addr, addr->sa_data);
886                 ether_addr_copy(hw->mac.addr, addr->sa_data);
887                 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
888         }
889
890         /* if we had a mailbox error suggest trying again */
891         return err ? -EAGAIN : 0;
892 }
893
894 static int __fm10k_mc_sync(struct net_device *dev,
895                            const unsigned char *addr, bool sync)
896 {
897         struct fm10k_intfc *interface = netdev_priv(dev);
898         struct fm10k_hw *hw = &interface->hw;
899         u16 vid, glort = interface->glort;
900         s32 err;
901
902         if (!is_multicast_ether_addr(addr))
903                 return -EADDRNOTAVAIL;
904
905         /* update table with current entries */
906         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
907              vid < VLAN_N_VID;
908              vid = fm10k_find_next_vlan(interface, vid)) {
909                 err = hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
910                 if (err)
911                         return err;
912         }
913
914         return 0;
915 }
916
917 static int fm10k_mc_sync(struct net_device *dev,
918                          const unsigned char *addr)
919 {
920         return __fm10k_mc_sync(dev, addr, true);
921 }
922
923 static int fm10k_mc_unsync(struct net_device *dev,
924                            const unsigned char *addr)
925 {
926         return __fm10k_mc_sync(dev, addr, false);
927 }
928
929 static void fm10k_set_rx_mode(struct net_device *dev)
930 {
931         struct fm10k_intfc *interface = netdev_priv(dev);
932         struct fm10k_hw *hw = &interface->hw;
933         int xcast_mode;
934
935         /* no need to update the harwdare if we are not running */
936         if (!(dev->flags & IFF_UP))
937                 return;
938
939         /* determine new mode based on flags */
940         xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
941                      (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
942                      (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
943                      FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
944
945         fm10k_mbx_lock(interface);
946
947         /* syncronize all of the addresses */
948         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
949                 __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
950                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
951                         __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
952         }
953
954         /* if we aren't changing modes there is nothing to do */
955         if (interface->xcast_mode != xcast_mode) {
956                 /* update VLAN table */
957                 if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
958                         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
959                 if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
960                         fm10k_clear_unused_vlans(interface);
961
962                 /* update xcast mode */
963                 hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
964
965                 /* record updated xcast mode state */
966                 interface->xcast_mode = xcast_mode;
967         }
968
969         fm10k_mbx_unlock(interface);
970 }
971
972 void fm10k_restore_rx_state(struct fm10k_intfc *interface)
973 {
974         struct net_device *netdev = interface->netdev;
975         struct fm10k_hw *hw = &interface->hw;
976         int xcast_mode;
977         u16 vid, glort;
978
979         /* record glort for this interface */
980         glort = interface->glort;
981
982         /* convert interface flags to xcast mode */
983         if (netdev->flags & IFF_PROMISC)
984                 xcast_mode = FM10K_XCAST_MODE_PROMISC;
985         else if (netdev->flags & IFF_ALLMULTI)
986                 xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
987         else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
988                 xcast_mode = FM10K_XCAST_MODE_MULTI;
989         else
990                 xcast_mode = FM10K_XCAST_MODE_NONE;
991
992         fm10k_mbx_lock(interface);
993
994         /* Enable logical port */
995         hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
996
997         /* update VLAN table */
998         hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
999                                 xcast_mode == FM10K_XCAST_MODE_PROMISC);
1000
1001         /* Add filter for VLAN 0 */
1002         hw->mac.ops.update_vlan(hw, 0, 0, true);
1003
1004         /* update table with current entries */
1005         for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0;
1006              vid < VLAN_N_VID;
1007              vid = fm10k_find_next_vlan(interface, vid)) {
1008                 hw->mac.ops.update_vlan(hw, vid, 0, true);
1009                 hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
1010                                            vid, true, 0);
1011         }
1012
1013         /* syncronize all of the addresses */
1014         if (xcast_mode != FM10K_XCAST_MODE_PROMISC) {
1015                 __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
1016                 if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI)
1017                         __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
1018         }
1019
1020         /* update xcast mode */
1021         hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
1022
1023         fm10k_mbx_unlock(interface);
1024
1025         /* record updated xcast mode state */
1026         interface->xcast_mode = xcast_mode;
1027
1028         /* Restore tunnel configuration */
1029         fm10k_restore_vxlan_port(interface);
1030 }
1031
1032 void fm10k_reset_rx_state(struct fm10k_intfc *interface)
1033 {
1034         struct net_device *netdev = interface->netdev;
1035         struct fm10k_hw *hw = &interface->hw;
1036
1037         fm10k_mbx_lock(interface);
1038
1039         /* clear the logical port state on lower device */
1040         hw->mac.ops.update_lport_state(hw, interface->glort,
1041                                        interface->glort_count, false);
1042
1043         fm10k_mbx_unlock(interface);
1044
1045         /* reset flags to default state */
1046         interface->xcast_mode = FM10K_XCAST_MODE_NONE;
1047
1048         /* clear the sync flag since the lport has been dropped */
1049         __dev_uc_unsync(netdev, NULL);
1050         __dev_mc_unsync(netdev, NULL);
1051 }
1052
1053 /**
1054  * fm10k_get_stats64 - Get System Network Statistics
1055  * @netdev: network interface device structure
1056  * @stats: storage space for 64bit statistics
1057  *
1058  * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
1059  * function replaces fm10k_get_stats for kernels which support it.
1060  */
1061 static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
1062                                                    struct rtnl_link_stats64 *stats)
1063 {
1064         struct fm10k_intfc *interface = netdev_priv(netdev);
1065         struct fm10k_ring *ring;
1066         unsigned int start, i;
1067         u64 bytes, packets;
1068
1069         rcu_read_lock();
1070
1071         for (i = 0; i < interface->num_rx_queues; i++) {
1072                 ring = ACCESS_ONCE(interface->rx_ring[i]);
1073
1074                 if (!ring)
1075                         continue;
1076
1077                 do {
1078                         start = u64_stats_fetch_begin_irq(&ring->syncp);
1079                         packets = ring->stats.packets;
1080                         bytes   = ring->stats.bytes;
1081                 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1082
1083                 stats->rx_packets += packets;
1084                 stats->rx_bytes   += bytes;
1085         }
1086
1087         for (i = 0; i < interface->num_tx_queues; i++) {
1088                 ring = ACCESS_ONCE(interface->rx_ring[i]);
1089
1090                 if (!ring)
1091                         continue;
1092
1093                 do {
1094                         start = u64_stats_fetch_begin_irq(&ring->syncp);
1095                         packets = ring->stats.packets;
1096                         bytes   = ring->stats.bytes;
1097                 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1098
1099                 stats->tx_packets += packets;
1100                 stats->tx_bytes   += bytes;
1101         }
1102
1103         rcu_read_unlock();
1104
1105         /* following stats updated by fm10k_service_task() */
1106         stats->rx_missed_errors = netdev->stats.rx_missed_errors;
1107
1108         return stats;
1109 }
1110
1111 int fm10k_setup_tc(struct net_device *dev, u8 tc)
1112 {
1113         struct fm10k_intfc *interface = netdev_priv(dev);
1114
1115         /* Currently only the PF supports priority classes */
1116         if (tc && (interface->hw.mac.type != fm10k_mac_pf))
1117                 return -EINVAL;
1118
1119         /* Hardware supports up to 8 traffic classes */
1120         if (tc > 8)
1121                 return -EINVAL;
1122
1123         /* Hardware has to reinitialize queues to match packet
1124          * buffer alignment. Unfortunately, the hardware is not
1125          * flexible enough to do this dynamically.
1126          */
1127         if (netif_running(dev))
1128                 fm10k_close(dev);
1129
1130         fm10k_mbx_free_irq(interface);
1131
1132         fm10k_clear_queueing_scheme(interface);
1133
1134         /* we expect the prio_tc map to be repopulated later */
1135         netdev_reset_tc(dev);
1136         netdev_set_num_tc(dev, tc);
1137
1138         fm10k_init_queueing_scheme(interface);
1139
1140         fm10k_mbx_request_irq(interface);
1141
1142         if (netif_running(dev))
1143                 fm10k_open(dev);
1144
1145         /* flag to indicate SWPRI has yet to be updated */
1146         interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
1147
1148         return 0;
1149 }
1150
1151 static const struct net_device_ops fm10k_netdev_ops = {
1152         .ndo_open               = fm10k_open,
1153         .ndo_stop               = fm10k_close,
1154         .ndo_validate_addr      = eth_validate_addr,
1155         .ndo_start_xmit         = fm10k_xmit_frame,
1156         .ndo_set_mac_address    = fm10k_set_mac,
1157         .ndo_change_mtu         = fm10k_change_mtu,
1158         .ndo_tx_timeout         = fm10k_tx_timeout,
1159         .ndo_vlan_rx_add_vid    = fm10k_vlan_rx_add_vid,
1160         .ndo_vlan_rx_kill_vid   = fm10k_vlan_rx_kill_vid,
1161         .ndo_set_rx_mode        = fm10k_set_rx_mode,
1162         .ndo_get_stats64        = fm10k_get_stats64,
1163         .ndo_setup_tc           = fm10k_setup_tc,
1164         .ndo_add_vxlan_port     = fm10k_add_vxlan_port,
1165         .ndo_del_vxlan_port     = fm10k_del_vxlan_port,
1166 };
1167
1168 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
1169
1170 struct net_device *fm10k_alloc_netdev(void)
1171 {
1172         struct fm10k_intfc *interface;
1173         struct net_device *dev;
1174
1175         dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
1176         if (!dev)
1177                 return NULL;
1178
1179         /* set net device and ethtool ops */
1180         dev->netdev_ops = &fm10k_netdev_ops;
1181         fm10k_set_ethtool_ops(dev);
1182
1183         /* configure default debug level */
1184         interface = netdev_priv(dev);
1185         interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
1186
1187         /* configure default features */
1188         dev->features |= NETIF_F_IP_CSUM |
1189                          NETIF_F_IPV6_CSUM |
1190                          NETIF_F_SG |
1191                          NETIF_F_TSO |
1192                          NETIF_F_TSO6 |
1193                          NETIF_F_TSO_ECN |
1194                          NETIF_F_GSO_UDP_TUNNEL |
1195                          NETIF_F_RXHASH |
1196                          NETIF_F_RXCSUM;
1197
1198         /* all features defined to this point should be changeable */
1199         dev->hw_features |= dev->features;
1200
1201         /* configure VLAN features */
1202         dev->vlan_features |= dev->features;
1203
1204         /* configure tunnel offloads */
1205         dev->hw_enc_features = NETIF_F_IP_CSUM |
1206                                NETIF_F_TSO |
1207                                NETIF_F_TSO6 |
1208                                NETIF_F_TSO_ECN |
1209                                NETIF_F_GSO_UDP_TUNNEL |
1210                                NETIF_F_IPV6_CSUM |
1211                                NETIF_F_SG;
1212
1213         /* we want to leave these both on as we cannot disable VLAN tag
1214          * insertion or stripping on the hardware since it is contained
1215          * in the FTAG and not in the frame itself.
1216          */
1217         dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1218                          NETIF_F_HW_VLAN_CTAG_RX |
1219                          NETIF_F_HW_VLAN_CTAG_FILTER;
1220
1221         dev->priv_flags |= IFF_UNICAST_FLT;
1222
1223         return dev;
1224 }