xref: /linux/drivers/net/ethernet/intel/fm10k/fm10k_netdev.c (revision f412eed9dfdeeb6becd7de2ffe8b5d0a8b3f81ca)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Intel(R) Ethernet Switch Host Interface Driver
3  * Copyright(c) 2013 - 2018 Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in
15  * the file called "COPYING".
16  *
17  * Contact Information:
18  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
19  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
20  */
21 
22 #include "fm10k.h"
23 #include <linux/vmalloc.h>
24 #include <net/udp_tunnel.h>
25 #include <linux/if_macvlan.h>
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 interface 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 	/* we support 3 possible GLORT configurations.
372 	 * 1: VFs consume all but the last 1
373 	 * 2: VFs and PF split glorts with possible gap between
374 	 * 3: VFs allocated first 64, all others belong to PF
375 	 */
376 	if (mask <= hw->iov.total_vfs) {
377 		interface->glort_count = 1;
378 		interface->glort += mask;
379 	} else if (mask < 64) {
380 		interface->glort_count = (mask + 1) / 2;
381 		interface->glort += interface->glort_count;
382 	} else {
383 		interface->glort_count = mask - 63;
384 		interface->glort += 64;
385 	}
386 }
387 
388 /**
389  * fm10k_free_udp_port_info
390  * @interface: board private structure
391  *
392  * This function frees both geneve_port and vxlan_port structures
393  **/
394 static void fm10k_free_udp_port_info(struct fm10k_intfc *interface)
395 {
396 	struct fm10k_udp_port *port;
397 
398 	/* flush all entries from vxlan list */
399 	port = list_first_entry_or_null(&interface->vxlan_port,
400 					struct fm10k_udp_port, list);
401 	while (port) {
402 		list_del(&port->list);
403 		kfree(port);
404 		port = list_first_entry_or_null(&interface->vxlan_port,
405 						struct fm10k_udp_port,
406 						list);
407 	}
408 
409 	/* flush all entries from geneve list */
410 	port = list_first_entry_or_null(&interface->geneve_port,
411 					struct fm10k_udp_port, list);
412 	while (port) {
413 		list_del(&port->list);
414 		kfree(port);
415 		port = list_first_entry_or_null(&interface->vxlan_port,
416 						struct fm10k_udp_port,
417 						list);
418 	}
419 }
420 
421 /**
422  * fm10k_restore_udp_port_info
423  * @interface: board private structure
424  *
425  * This function restores the value in the tunnel_cfg register(s) after reset
426  **/
427 static void fm10k_restore_udp_port_info(struct fm10k_intfc *interface)
428 {
429 	struct fm10k_hw *hw = &interface->hw;
430 	struct fm10k_udp_port *port;
431 
432 	/* only the PF supports configuring tunnels */
433 	if (hw->mac.type != fm10k_mac_pf)
434 		return;
435 
436 	port = list_first_entry_or_null(&interface->vxlan_port,
437 					struct fm10k_udp_port, list);
438 
439 	/* restore tunnel configuration register */
440 	fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
441 			(port ? ntohs(port->port) : 0) |
442 			(ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
443 
444 	port = list_first_entry_or_null(&interface->geneve_port,
445 					struct fm10k_udp_port, list);
446 
447 	/* restore Geneve tunnel configuration register */
448 	fm10k_write_reg(hw, FM10K_TUNNEL_CFG_GENEVE,
449 			(port ? ntohs(port->port) : 0));
450 }
451 
452 static struct fm10k_udp_port *
453 fm10k_remove_tunnel_port(struct list_head *ports,
454 			 struct udp_tunnel_info *ti)
455 {
456 	struct fm10k_udp_port *port;
457 
458 	list_for_each_entry(port, ports, list) {
459 		if ((port->port == ti->port) &&
460 		    (port->sa_family == ti->sa_family)) {
461 			list_del(&port->list);
462 			return port;
463 		}
464 	}
465 
466 	return NULL;
467 }
468 
469 static void fm10k_insert_tunnel_port(struct list_head *ports,
470 				     struct udp_tunnel_info *ti)
471 {
472 	struct fm10k_udp_port *port;
473 
474 	/* remove existing port entry from the list so that the newest items
475 	 * are always at the tail of the list.
476 	 */
477 	port = fm10k_remove_tunnel_port(ports, ti);
478 	if (!port) {
479 		port = kmalloc(sizeof(*port), GFP_ATOMIC);
480 		if  (!port)
481 			return;
482 		port->port = ti->port;
483 		port->sa_family = ti->sa_family;
484 	}
485 
486 	list_add_tail(&port->list, ports);
487 }
488 
489 /**
490  * fm10k_udp_tunnel_add
491  * @dev: network interface device structure
492  * @ti: Tunnel endpoint information
493  *
494  * This function is called when a new UDP tunnel port has been added.
495  * Due to hardware restrictions, only one port per type can be offloaded at
496  * once.
497  **/
498 static void fm10k_udp_tunnel_add(struct net_device *dev,
499 				 struct udp_tunnel_info *ti)
500 {
501 	struct fm10k_intfc *interface = netdev_priv(dev);
502 
503 	/* only the PF supports configuring tunnels */
504 	if (interface->hw.mac.type != fm10k_mac_pf)
505 		return;
506 
507 	switch (ti->type) {
508 	case UDP_TUNNEL_TYPE_VXLAN:
509 		fm10k_insert_tunnel_port(&interface->vxlan_port, ti);
510 		break;
511 	case UDP_TUNNEL_TYPE_GENEVE:
512 		fm10k_insert_tunnel_port(&interface->geneve_port, ti);
513 		break;
514 	default:
515 		return;
516 	}
517 
518 	fm10k_restore_udp_port_info(interface);
519 }
520 
521 /**
522  * fm10k_udp_tunnel_del
523  * @dev: network interface device structure
524  * @ti: Tunnel end point information
525  *
526  * This function is called when a new UDP tunnel port is deleted. The freed
527  * port will be removed from the list, then we reprogram the offloaded port
528  * based on the head of the list.
529  **/
530 static void fm10k_udp_tunnel_del(struct net_device *dev,
531 				 struct udp_tunnel_info *ti)
532 {
533 	struct fm10k_intfc *interface = netdev_priv(dev);
534 	struct fm10k_udp_port *port = NULL;
535 
536 	if (interface->hw.mac.type != fm10k_mac_pf)
537 		return;
538 
539 	switch (ti->type) {
540 	case UDP_TUNNEL_TYPE_VXLAN:
541 		port = fm10k_remove_tunnel_port(&interface->vxlan_port, ti);
542 		break;
543 	case UDP_TUNNEL_TYPE_GENEVE:
544 		port = fm10k_remove_tunnel_port(&interface->geneve_port, ti);
545 		break;
546 	default:
547 		return;
548 	}
549 
550 	/* if we did remove a port we need to free its memory */
551 	kfree(port);
552 
553 	fm10k_restore_udp_port_info(interface);
554 }
555 
556 /**
557  * fm10k_open - Called when a network interface is made active
558  * @netdev: network interface device structure
559  *
560  * Returns 0 on success, negative value on failure
561  *
562  * The open entry point is called when a network interface is made
563  * active by the system (IFF_UP).  At this point all resources needed
564  * for transmit and receive operations are allocated, the interrupt
565  * handler is registered with the OS, the watchdog timer is started,
566  * and the stack is notified that the interface is ready.
567  **/
568 int fm10k_open(struct net_device *netdev)
569 {
570 	struct fm10k_intfc *interface = netdev_priv(netdev);
571 	int err;
572 
573 	/* allocate transmit descriptors */
574 	err = fm10k_setup_all_tx_resources(interface);
575 	if (err)
576 		goto err_setup_tx;
577 
578 	/* allocate receive descriptors */
579 	err = fm10k_setup_all_rx_resources(interface);
580 	if (err)
581 		goto err_setup_rx;
582 
583 	/* allocate interrupt resources */
584 	err = fm10k_qv_request_irq(interface);
585 	if (err)
586 		goto err_req_irq;
587 
588 	/* setup GLORT assignment for this port */
589 	fm10k_request_glort_range(interface);
590 
591 	/* Notify the stack of the actual queue counts */
592 	err = netif_set_real_num_tx_queues(netdev,
593 					   interface->num_tx_queues);
594 	if (err)
595 		goto err_set_queues;
596 
597 	err = netif_set_real_num_rx_queues(netdev,
598 					   interface->num_rx_queues);
599 	if (err)
600 		goto err_set_queues;
601 
602 	udp_tunnel_get_rx_info(netdev);
603 
604 	fm10k_up(interface);
605 
606 	return 0;
607 
608 err_set_queues:
609 	fm10k_qv_free_irq(interface);
610 err_req_irq:
611 	fm10k_free_all_rx_resources(interface);
612 err_setup_rx:
613 	fm10k_free_all_tx_resources(interface);
614 err_setup_tx:
615 	return err;
616 }
617 
618 /**
619  * fm10k_close - Disables a network interface
620  * @netdev: network interface device structure
621  *
622  * Returns 0, this is not allowed to fail
623  *
624  * The close entry point is called when an interface is de-activated
625  * by the OS.  The hardware is still under the drivers control, but
626  * needs to be disabled.  A global MAC reset is issued to stop the
627  * hardware, and all transmit and receive resources are freed.
628  **/
629 int fm10k_close(struct net_device *netdev)
630 {
631 	struct fm10k_intfc *interface = netdev_priv(netdev);
632 
633 	fm10k_down(interface);
634 
635 	fm10k_qv_free_irq(interface);
636 
637 	fm10k_free_udp_port_info(interface);
638 
639 	fm10k_free_all_tx_resources(interface);
640 	fm10k_free_all_rx_resources(interface);
641 
642 	return 0;
643 }
644 
645 static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
646 {
647 	struct fm10k_intfc *interface = netdev_priv(dev);
648 	int num_tx_queues = READ_ONCE(interface->num_tx_queues);
649 	unsigned int r_idx = skb->queue_mapping;
650 	int err;
651 
652 	if (!num_tx_queues)
653 		return NETDEV_TX_BUSY;
654 
655 	if ((skb->protocol == htons(ETH_P_8021Q)) &&
656 	    !skb_vlan_tag_present(skb)) {
657 		/* FM10K only supports hardware tagging, any tags in frame
658 		 * are considered 2nd level or "outer" tags
659 		 */
660 		struct vlan_hdr *vhdr;
661 		__be16 proto;
662 
663 		/* make sure skb is not shared */
664 		skb = skb_share_check(skb, GFP_ATOMIC);
665 		if (!skb)
666 			return NETDEV_TX_OK;
667 
668 		/* make sure there is enough room to move the ethernet header */
669 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
670 			return NETDEV_TX_OK;
671 
672 		/* verify the skb head is not shared */
673 		err = skb_cow_head(skb, 0);
674 		if (err) {
675 			dev_kfree_skb(skb);
676 			return NETDEV_TX_OK;
677 		}
678 
679 		/* locate VLAN header */
680 		vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
681 
682 		/* pull the 2 key pieces of data out of it */
683 		__vlan_hwaccel_put_tag(skb,
684 				       htons(ETH_P_8021Q),
685 				       ntohs(vhdr->h_vlan_TCI));
686 		proto = vhdr->h_vlan_encapsulated_proto;
687 		skb->protocol = (ntohs(proto) >= 1536) ? proto :
688 							 htons(ETH_P_802_2);
689 
690 		/* squash it by moving the ethernet addresses up 4 bytes */
691 		memmove(skb->data + VLAN_HLEN, skb->data, 12);
692 		__skb_pull(skb, VLAN_HLEN);
693 		skb_reset_mac_header(skb);
694 	}
695 
696 	/* The minimum packet size for a single buffer is 17B so pad the skb
697 	 * in order to meet this minimum size requirement.
698 	 */
699 	if (unlikely(skb->len < 17)) {
700 		int pad_len = 17 - skb->len;
701 
702 		if (skb_pad(skb, pad_len))
703 			return NETDEV_TX_OK;
704 		__skb_put(skb, pad_len);
705 	}
706 
707 	if (r_idx >= num_tx_queues)
708 		r_idx %= num_tx_queues;
709 
710 	err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
711 
712 	return err;
713 }
714 
715 /**
716  * fm10k_tx_timeout - Respond to a Tx Hang
717  * @netdev: network interface device structure
718  **/
719 static void fm10k_tx_timeout(struct net_device *netdev)
720 {
721 	struct fm10k_intfc *interface = netdev_priv(netdev);
722 	bool real_tx_hang = false;
723 	int i;
724 
725 #define TX_TIMEO_LIMIT 16000
726 	for (i = 0; i < interface->num_tx_queues; i++) {
727 		struct fm10k_ring *tx_ring = interface->tx_ring[i];
728 
729 		if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
730 			real_tx_hang = true;
731 	}
732 
733 	if (real_tx_hang) {
734 		fm10k_tx_timeout_reset(interface);
735 	} else {
736 		netif_info(interface, drv, netdev,
737 			   "Fake Tx hang detected with timeout of %d seconds\n",
738 			   netdev->watchdog_timeo / HZ);
739 
740 		/* fake Tx hang - increase the kernel timeout */
741 		if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
742 			netdev->watchdog_timeo *= 2;
743 	}
744 }
745 
746 /**
747  * fm10k_host_mbx_ready - Check PF interface's mailbox readiness
748  * @interface: board private structure
749  *
750  * This function checks if the PF interface's mailbox is ready before queueing
751  * mailbox messages for transmission. This will prevent filling the TX mailbox
752  * queue when the receiver is not ready. VF interfaces are exempt from this
753  * check since it will block all PF-VF mailbox messages from being sent from
754  * the VF to the PF at initialization.
755  **/
756 static bool fm10k_host_mbx_ready(struct fm10k_intfc *interface)
757 {
758 	struct fm10k_hw *hw = &interface->hw;
759 
760 	return (hw->mac.type == fm10k_mac_vf || interface->host_ready);
761 }
762 
763 /**
764  * fm10k_queue_vlan_request - Queue a VLAN update request
765  * @interface: the fm10k interface structure
766  * @vid: the VLAN vid
767  * @vsi: VSI index number
768  * @set: whether to set or clear
769  *
770  * This function queues up a VLAN update. For VFs, this must be sent to the
771  * managing PF over the mailbox. For PFs, we'll use the same handling so that
772  * it's similar to the VF. This avoids storming the PF<->VF mailbox with too
773  * many VLAN updates during reset.
774  */
775 int fm10k_queue_vlan_request(struct fm10k_intfc *interface,
776 			     u32 vid, u8 vsi, bool set)
777 {
778 	struct fm10k_macvlan_request *request;
779 	unsigned long flags;
780 
781 	/* This must be atomic since we may be called while the netdev
782 	 * addr_list_lock is held
783 	 */
784 	request = kzalloc(sizeof(*request), GFP_ATOMIC);
785 	if (!request)
786 		return -ENOMEM;
787 
788 	request->type = FM10K_VLAN_REQUEST;
789 	request->vlan.vid = vid;
790 	request->vlan.vsi = vsi;
791 	request->set = set;
792 
793 	spin_lock_irqsave(&interface->macvlan_lock, flags);
794 	list_add_tail(&request->list, &interface->macvlan_requests);
795 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
796 
797 	fm10k_macvlan_schedule(interface);
798 
799 	return 0;
800 }
801 
802 /**
803  * fm10k_queue_mac_request - Queue a MAC update request
804  * @interface: the fm10k interface structure
805  * @glort: the target glort for this update
806  * @addr: the address to update
807  * @vid: the vid to update
808  * @set: whether to add or remove
809  *
810  * This function queues up a MAC request for sending to the switch manager.
811  * A separate thread monitors the queue and sends updates to the switch
812  * manager. Return 0 on success, and negative error code on failure.
813  **/
814 int fm10k_queue_mac_request(struct fm10k_intfc *interface, u16 glort,
815 			    const unsigned char *addr, u16 vid, bool set)
816 {
817 	struct fm10k_macvlan_request *request;
818 	unsigned long flags;
819 
820 	/* This must be atomic since we may be called while the netdev
821 	 * addr_list_lock is held
822 	 */
823 	request = kzalloc(sizeof(*request), GFP_ATOMIC);
824 	if (!request)
825 		return -ENOMEM;
826 
827 	if (is_multicast_ether_addr(addr))
828 		request->type = FM10K_MC_MAC_REQUEST;
829 	else
830 		request->type = FM10K_UC_MAC_REQUEST;
831 
832 	ether_addr_copy(request->mac.addr, addr);
833 	request->mac.glort = glort;
834 	request->mac.vid = vid;
835 	request->set = set;
836 
837 	spin_lock_irqsave(&interface->macvlan_lock, flags);
838 	list_add_tail(&request->list, &interface->macvlan_requests);
839 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
840 
841 	fm10k_macvlan_schedule(interface);
842 
843 	return 0;
844 }
845 
846 /**
847  * fm10k_clear_macvlan_queue - Cancel pending updates for a given glort
848  * @interface: the fm10k interface structure
849  * @glort: the target glort to clear
850  * @vlans: true to clear VLAN messages, false to ignore them
851  *
852  * Cancel any outstanding MAC/VLAN requests for a given glort. This is
853  * expected to be called when a logical port goes down.
854  **/
855 void fm10k_clear_macvlan_queue(struct fm10k_intfc *interface,
856 			       u16 glort, bool vlans)
857 
858 {
859 	struct fm10k_macvlan_request *r, *tmp;
860 	unsigned long flags;
861 
862 	spin_lock_irqsave(&interface->macvlan_lock, flags);
863 
864 	/* Free any outstanding MAC/VLAN requests for this interface */
865 	list_for_each_entry_safe(r, tmp, &interface->macvlan_requests, list) {
866 		switch (r->type) {
867 		case FM10K_MC_MAC_REQUEST:
868 		case FM10K_UC_MAC_REQUEST:
869 			/* Don't free requests for other interfaces */
870 			if (r->mac.glort != glort)
871 				break;
872 			/* fall through */
873 		case FM10K_VLAN_REQUEST:
874 			if (vlans) {
875 				list_del(&r->list);
876 				kfree(r);
877 			}
878 			break;
879 		}
880 	}
881 
882 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
883 }
884 
885 static int fm10k_uc_vlan_unsync(struct net_device *netdev,
886 				const unsigned char *uc_addr)
887 {
888 	struct fm10k_intfc *interface = netdev_priv(netdev);
889 	u16 glort = interface->glort;
890 	u16 vid = interface->vid;
891 	bool set = !!(vid / VLAN_N_VID);
892 	int err = -EHOSTDOWN;
893 
894 	/* drop any leading bits on the VLAN ID */
895 	vid &= VLAN_N_VID - 1;
896 
897 	err = fm10k_queue_mac_request(interface, glort, uc_addr, vid, set);
898 	if (err)
899 		return err;
900 
901 	/* return non-zero value as we are only doing a partial sync/unsync */
902 	return 1;
903 }
904 
905 static int fm10k_mc_vlan_unsync(struct net_device *netdev,
906 				const unsigned char *mc_addr)
907 {
908 	struct fm10k_intfc *interface = netdev_priv(netdev);
909 	u16 glort = interface->glort;
910 	u16 vid = interface->vid;
911 	bool set = !!(vid / VLAN_N_VID);
912 	int err = -EHOSTDOWN;
913 
914 	/* drop any leading bits on the VLAN ID */
915 	vid &= VLAN_N_VID - 1;
916 
917 	err = fm10k_queue_mac_request(interface, glort, mc_addr, vid, set);
918 	if (err)
919 		return err;
920 
921 	/* return non-zero value as we are only doing a partial sync/unsync */
922 	return 1;
923 }
924 
925 static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
926 {
927 	struct fm10k_intfc *interface = netdev_priv(netdev);
928 	struct fm10k_hw *hw = &interface->hw;
929 	s32 err;
930 	int i;
931 
932 	/* updates do not apply to VLAN 0 */
933 	if (!vid)
934 		return 0;
935 
936 	if (vid >= VLAN_N_VID)
937 		return -EINVAL;
938 
939 	/* Verify that we have permission to add VLANs. If this is a request
940 	 * to remove a VLAN, we still want to allow the user to remove the
941 	 * VLAN device. In that case, we need to clear the bit in the
942 	 * active_vlans bitmask.
943 	 */
944 	if (set && hw->mac.vlan_override)
945 		return -EACCES;
946 
947 	/* update active_vlans bitmask */
948 	set_bit(vid, interface->active_vlans);
949 	if (!set)
950 		clear_bit(vid, interface->active_vlans);
951 
952 	/* disable the default VLAN ID on ring if we have an active VLAN */
953 	for (i = 0; i < interface->num_rx_queues; i++) {
954 		struct fm10k_ring *rx_ring = interface->rx_ring[i];
955 		u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
956 
957 		if (test_bit(rx_vid, interface->active_vlans))
958 			rx_ring->vid |= FM10K_VLAN_CLEAR;
959 		else
960 			rx_ring->vid &= ~FM10K_VLAN_CLEAR;
961 	}
962 
963 	/* If our VLAN has been overridden, there is no reason to send VLAN
964 	 * removal requests as they will be silently ignored.
965 	 */
966 	if (hw->mac.vlan_override)
967 		return 0;
968 
969 	/* Do not remove default VLAN ID related entries from VLAN and MAC
970 	 * tables
971 	 */
972 	if (!set && vid == hw->mac.default_vid)
973 		return 0;
974 
975 	/* Do not throw an error if the interface is down. We will sync once
976 	 * we come up
977 	 */
978 	if (test_bit(__FM10K_DOWN, interface->state))
979 		return 0;
980 
981 	fm10k_mbx_lock(interface);
982 
983 	/* only need to update the VLAN if not in promiscuous mode */
984 	if (!(netdev->flags & IFF_PROMISC)) {
985 		err = fm10k_queue_vlan_request(interface, vid, 0, set);
986 		if (err)
987 			goto err_out;
988 	}
989 
990 	/* Update our base MAC address */
991 	err = fm10k_queue_mac_request(interface, interface->glort,
992 				      hw->mac.addr, vid, set);
993 	if (err)
994 		goto err_out;
995 
996 	/* set VLAN ID prior to syncing/unsyncing the VLAN */
997 	interface->vid = vid + (set ? VLAN_N_VID : 0);
998 
999 	/* Update the unicast and multicast address list to add/drop VLAN */
1000 	__dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
1001 	__dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
1002 
1003 err_out:
1004 	fm10k_mbx_unlock(interface);
1005 
1006 	return err;
1007 }
1008 
1009 static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
1010 				 __always_unused __be16 proto, u16 vid)
1011 {
1012 	/* update VLAN and address table based on changes */
1013 	return fm10k_update_vid(netdev, vid, true);
1014 }
1015 
1016 static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
1017 				  __always_unused __be16 proto, u16 vid)
1018 {
1019 	/* update VLAN and address table based on changes */
1020 	return fm10k_update_vid(netdev, vid, false);
1021 }
1022 
1023 static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
1024 {
1025 	struct fm10k_hw *hw = &interface->hw;
1026 	u16 default_vid = hw->mac.default_vid;
1027 	u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
1028 
1029 	vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
1030 
1031 	return vid;
1032 }
1033 
1034 static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
1035 {
1036 	u32 vid, prev_vid;
1037 
1038 	/* loop through and find any gaps in the table */
1039 	for (vid = 0, prev_vid = 0;
1040 	     prev_vid < VLAN_N_VID;
1041 	     prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
1042 		if (prev_vid == vid)
1043 			continue;
1044 
1045 		/* send request to clear multiple bits at a time */
1046 		prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
1047 		fm10k_queue_vlan_request(interface, prev_vid, 0, false);
1048 	}
1049 }
1050 
1051 static int __fm10k_uc_sync(struct net_device *dev,
1052 			   const unsigned char *addr, bool sync)
1053 {
1054 	struct fm10k_intfc *interface = netdev_priv(dev);
1055 	u16 vid, glort = interface->glort;
1056 	s32 err;
1057 
1058 	if (!is_valid_ether_addr(addr))
1059 		return -EADDRNOTAVAIL;
1060 
1061 	for (vid = fm10k_find_next_vlan(interface, 0);
1062 	     vid < VLAN_N_VID;
1063 	     vid = fm10k_find_next_vlan(interface, vid)) {
1064 		err = fm10k_queue_mac_request(interface, glort,
1065 					      addr, vid, sync);
1066 		if (err)
1067 			return err;
1068 	}
1069 
1070 	return 0;
1071 }
1072 
1073 static int fm10k_uc_sync(struct net_device *dev,
1074 			 const unsigned char *addr)
1075 {
1076 	return __fm10k_uc_sync(dev, addr, true);
1077 }
1078 
1079 static int fm10k_uc_unsync(struct net_device *dev,
1080 			   const unsigned char *addr)
1081 {
1082 	return __fm10k_uc_sync(dev, addr, false);
1083 }
1084 
1085 static int fm10k_set_mac(struct net_device *dev, void *p)
1086 {
1087 	struct fm10k_intfc *interface = netdev_priv(dev);
1088 	struct fm10k_hw *hw = &interface->hw;
1089 	struct sockaddr *addr = p;
1090 	s32 err = 0;
1091 
1092 	if (!is_valid_ether_addr(addr->sa_data))
1093 		return -EADDRNOTAVAIL;
1094 
1095 	if (dev->flags & IFF_UP) {
1096 		/* setting MAC address requires mailbox */
1097 		fm10k_mbx_lock(interface);
1098 
1099 		err = fm10k_uc_sync(dev, addr->sa_data);
1100 		if (!err)
1101 			fm10k_uc_unsync(dev, hw->mac.addr);
1102 
1103 		fm10k_mbx_unlock(interface);
1104 	}
1105 
1106 	if (!err) {
1107 		ether_addr_copy(dev->dev_addr, addr->sa_data);
1108 		ether_addr_copy(hw->mac.addr, addr->sa_data);
1109 		dev->addr_assign_type &= ~NET_ADDR_RANDOM;
1110 	}
1111 
1112 	/* if we had a mailbox error suggest trying again */
1113 	return err ? -EAGAIN : 0;
1114 }
1115 
1116 static int __fm10k_mc_sync(struct net_device *dev,
1117 			   const unsigned char *addr, bool sync)
1118 {
1119 	struct fm10k_intfc *interface = netdev_priv(dev);
1120 	u16 vid, glort = interface->glort;
1121 	s32 err;
1122 
1123 	if (!is_multicast_ether_addr(addr))
1124 		return -EADDRNOTAVAIL;
1125 
1126 	for (vid = fm10k_find_next_vlan(interface, 0);
1127 	     vid < VLAN_N_VID;
1128 	     vid = fm10k_find_next_vlan(interface, vid)) {
1129 		err = fm10k_queue_mac_request(interface, glort,
1130 					      addr, vid, sync);
1131 		if (err)
1132 			return err;
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 static int fm10k_mc_sync(struct net_device *dev,
1139 			 const unsigned char *addr)
1140 {
1141 	return __fm10k_mc_sync(dev, addr, true);
1142 }
1143 
1144 static int fm10k_mc_unsync(struct net_device *dev,
1145 			   const unsigned char *addr)
1146 {
1147 	return __fm10k_mc_sync(dev, addr, false);
1148 }
1149 
1150 static void fm10k_set_rx_mode(struct net_device *dev)
1151 {
1152 	struct fm10k_intfc *interface = netdev_priv(dev);
1153 	struct fm10k_hw *hw = &interface->hw;
1154 	int xcast_mode;
1155 
1156 	/* no need to update the harwdare if we are not running */
1157 	if (!(dev->flags & IFF_UP))
1158 		return;
1159 
1160 	/* determine new mode based on flags */
1161 	xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
1162 		     (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
1163 		     (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
1164 		     FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
1165 
1166 	fm10k_mbx_lock(interface);
1167 
1168 	/* update xcast mode first, but only if it changed */
1169 	if (interface->xcast_mode != xcast_mode) {
1170 		/* update VLAN table when entering promiscuous mode */
1171 		if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
1172 			fm10k_queue_vlan_request(interface, FM10K_VLAN_ALL,
1173 						 0, true);
1174 
1175 		/* clear VLAN table when exiting promiscuous mode */
1176 		if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
1177 			fm10k_clear_unused_vlans(interface);
1178 
1179 		/* update xcast mode if host's mailbox is ready */
1180 		if (fm10k_host_mbx_ready(interface))
1181 			hw->mac.ops.update_xcast_mode(hw, interface->glort,
1182 						      xcast_mode);
1183 
1184 		/* record updated xcast mode state */
1185 		interface->xcast_mode = xcast_mode;
1186 	}
1187 
1188 	/* synchronize all of the addresses */
1189 	__dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
1190 	__dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
1191 
1192 	fm10k_mbx_unlock(interface);
1193 }
1194 
1195 void fm10k_restore_rx_state(struct fm10k_intfc *interface)
1196 {
1197 	struct fm10k_l2_accel *l2_accel = interface->l2_accel;
1198 	struct net_device *netdev = interface->netdev;
1199 	struct fm10k_hw *hw = &interface->hw;
1200 	int xcast_mode, i;
1201 	u16 vid, glort;
1202 
1203 	/* record glort for this interface */
1204 	glort = interface->glort;
1205 
1206 	/* convert interface flags to xcast mode */
1207 	if (netdev->flags & IFF_PROMISC)
1208 		xcast_mode = FM10K_XCAST_MODE_PROMISC;
1209 	else if (netdev->flags & IFF_ALLMULTI)
1210 		xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
1211 	else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
1212 		xcast_mode = FM10K_XCAST_MODE_MULTI;
1213 	else
1214 		xcast_mode = FM10K_XCAST_MODE_NONE;
1215 
1216 	fm10k_mbx_lock(interface);
1217 
1218 	/* Enable logical port if host's mailbox is ready */
1219 	if (fm10k_host_mbx_ready(interface))
1220 		hw->mac.ops.update_lport_state(hw, glort,
1221 					       interface->glort_count, true);
1222 
1223 	/* update VLAN table */
1224 	fm10k_queue_vlan_request(interface, FM10K_VLAN_ALL, 0,
1225 				 xcast_mode == FM10K_XCAST_MODE_PROMISC);
1226 
1227 	/* update table with current entries */
1228 	for (vid = fm10k_find_next_vlan(interface, 0);
1229 	     vid < VLAN_N_VID;
1230 	     vid = fm10k_find_next_vlan(interface, vid)) {
1231 		fm10k_queue_vlan_request(interface, vid, 0, true);
1232 
1233 		fm10k_queue_mac_request(interface, glort,
1234 					hw->mac.addr, vid, true);
1235 	}
1236 
1237 	/* update xcast mode before synchronizing addresses if host's mailbox
1238 	 * is ready
1239 	 */
1240 	if (fm10k_host_mbx_ready(interface))
1241 		hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
1242 
1243 	/* synchronize all of the addresses */
1244 	__dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
1245 	__dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
1246 
1247 	/* synchronize macvlan addresses */
1248 	if (l2_accel) {
1249 		for (i = 0; i < l2_accel->size; i++) {
1250 			struct net_device *sdev = l2_accel->macvlan[i];
1251 
1252 			if (!sdev)
1253 				continue;
1254 
1255 			glort = l2_accel->dglort + 1 + i;
1256 
1257 			hw->mac.ops.update_xcast_mode(hw, glort,
1258 						      FM10K_XCAST_MODE_NONE);
1259 			fm10k_queue_mac_request(interface, glort,
1260 						sdev->dev_addr,
1261 						hw->mac.default_vid, true);
1262 		}
1263 	}
1264 
1265 	fm10k_mbx_unlock(interface);
1266 
1267 	/* record updated xcast mode state */
1268 	interface->xcast_mode = xcast_mode;
1269 
1270 	/* Restore tunnel configuration */
1271 	fm10k_restore_udp_port_info(interface);
1272 }
1273 
1274 void fm10k_reset_rx_state(struct fm10k_intfc *interface)
1275 {
1276 	struct net_device *netdev = interface->netdev;
1277 	struct fm10k_hw *hw = &interface->hw;
1278 
1279 	/* Wait for MAC/VLAN work to finish */
1280 	while (test_bit(__FM10K_MACVLAN_SCHED, interface->state))
1281 		usleep_range(1000, 2000);
1282 
1283 	/* Cancel pending MAC/VLAN requests */
1284 	fm10k_clear_macvlan_queue(interface, interface->glort, true);
1285 
1286 	fm10k_mbx_lock(interface);
1287 
1288 	/* clear the logical port state on lower device if host's mailbox is
1289 	 * ready
1290 	 */
1291 	if (fm10k_host_mbx_ready(interface))
1292 		hw->mac.ops.update_lport_state(hw, interface->glort,
1293 					       interface->glort_count, false);
1294 
1295 	fm10k_mbx_unlock(interface);
1296 
1297 	/* reset flags to default state */
1298 	interface->xcast_mode = FM10K_XCAST_MODE_NONE;
1299 
1300 	/* clear the sync flag since the lport has been dropped */
1301 	__dev_uc_unsync(netdev, NULL);
1302 	__dev_mc_unsync(netdev, NULL);
1303 }
1304 
1305 /**
1306  * fm10k_get_stats64 - Get System Network Statistics
1307  * @netdev: network interface device structure
1308  * @stats: storage space for 64bit statistics
1309  *
1310  * Obtain 64bit statistics in a way that is safe for both 32bit and 64bit
1311  * architectures.
1312  */
1313 static void fm10k_get_stats64(struct net_device *netdev,
1314 			      struct rtnl_link_stats64 *stats)
1315 {
1316 	struct fm10k_intfc *interface = netdev_priv(netdev);
1317 	struct fm10k_ring *ring;
1318 	unsigned int start, i;
1319 	u64 bytes, packets;
1320 
1321 	rcu_read_lock();
1322 
1323 	for (i = 0; i < interface->num_rx_queues; i++) {
1324 		ring = READ_ONCE(interface->rx_ring[i]);
1325 
1326 		if (!ring)
1327 			continue;
1328 
1329 		do {
1330 			start = u64_stats_fetch_begin_irq(&ring->syncp);
1331 			packets = ring->stats.packets;
1332 			bytes   = ring->stats.bytes;
1333 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1334 
1335 		stats->rx_packets += packets;
1336 		stats->rx_bytes   += bytes;
1337 	}
1338 
1339 	for (i = 0; i < interface->num_tx_queues; i++) {
1340 		ring = READ_ONCE(interface->tx_ring[i]);
1341 
1342 		if (!ring)
1343 			continue;
1344 
1345 		do {
1346 			start = u64_stats_fetch_begin_irq(&ring->syncp);
1347 			packets = ring->stats.packets;
1348 			bytes   = ring->stats.bytes;
1349 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1350 
1351 		stats->tx_packets += packets;
1352 		stats->tx_bytes   += bytes;
1353 	}
1354 
1355 	rcu_read_unlock();
1356 
1357 	/* following stats updated by fm10k_service_task() */
1358 	stats->rx_missed_errors	= netdev->stats.rx_missed_errors;
1359 }
1360 
1361 int fm10k_setup_tc(struct net_device *dev, u8 tc)
1362 {
1363 	struct fm10k_intfc *interface = netdev_priv(dev);
1364 	int err;
1365 
1366 	/* Currently only the PF supports priority classes */
1367 	if (tc && (interface->hw.mac.type != fm10k_mac_pf))
1368 		return -EINVAL;
1369 
1370 	/* Hardware supports up to 8 traffic classes */
1371 	if (tc > 8)
1372 		return -EINVAL;
1373 
1374 	/* Hardware has to reinitialize queues to match packet
1375 	 * buffer alignment. Unfortunately, the hardware is not
1376 	 * flexible enough to do this dynamically.
1377 	 */
1378 	if (netif_running(dev))
1379 		fm10k_close(dev);
1380 
1381 	fm10k_mbx_free_irq(interface);
1382 
1383 	fm10k_clear_queueing_scheme(interface);
1384 
1385 	/* we expect the prio_tc map to be repopulated later */
1386 	netdev_reset_tc(dev);
1387 	netdev_set_num_tc(dev, tc);
1388 
1389 	err = fm10k_init_queueing_scheme(interface);
1390 	if (err)
1391 		goto err_queueing_scheme;
1392 
1393 	err = fm10k_mbx_request_irq(interface);
1394 	if (err)
1395 		goto err_mbx_irq;
1396 
1397 	err = netif_running(dev) ? fm10k_open(dev) : 0;
1398 	if (err)
1399 		goto err_open;
1400 
1401 	/* flag to indicate SWPRI has yet to be updated */
1402 	set_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
1403 
1404 	return 0;
1405 err_open:
1406 	fm10k_mbx_free_irq(interface);
1407 err_mbx_irq:
1408 	fm10k_clear_queueing_scheme(interface);
1409 err_queueing_scheme:
1410 	netif_device_detach(dev);
1411 
1412 	return err;
1413 }
1414 
1415 static int __fm10k_setup_tc(struct net_device *dev, enum tc_setup_type type,
1416 			    void *type_data)
1417 {
1418 	struct tc_mqprio_qopt *mqprio = type_data;
1419 
1420 	if (type != TC_SETUP_QDISC_MQPRIO)
1421 		return -EOPNOTSUPP;
1422 
1423 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1424 
1425 	return fm10k_setup_tc(dev, mqprio->num_tc);
1426 }
1427 
1428 static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
1429 				  struct fm10k_l2_accel *l2_accel)
1430 {
1431 	struct fm10k_ring *ring;
1432 	int i;
1433 
1434 	for (i = 0; i < interface->num_rx_queues; i++) {
1435 		ring = interface->rx_ring[i];
1436 		rcu_assign_pointer(ring->l2_accel, l2_accel);
1437 	}
1438 
1439 	interface->l2_accel = l2_accel;
1440 }
1441 
1442 static void *fm10k_dfwd_add_station(struct net_device *dev,
1443 				    struct net_device *sdev)
1444 {
1445 	struct fm10k_intfc *interface = netdev_priv(dev);
1446 	struct fm10k_l2_accel *l2_accel = interface->l2_accel;
1447 	struct fm10k_l2_accel *old_l2_accel = NULL;
1448 	struct fm10k_dglort_cfg dglort = { 0 };
1449 	struct fm10k_hw *hw = &interface->hw;
1450 	int size = 0, i;
1451 	u16 glort;
1452 
1453 	/* The hardware supported by fm10k only filters on the destination MAC
1454 	 * address. In order to avoid issues we only support offloading modes
1455 	 * where the hardware can actually provide the functionality.
1456 	 */
1457 	if (!macvlan_supports_dest_filter(sdev))
1458 		return ERR_PTR(-EMEDIUMTYPE);
1459 
1460 	/* allocate l2 accel structure if it is not available */
1461 	if (!l2_accel) {
1462 		/* verify there is enough free GLORTs to support l2_accel */
1463 		if (interface->glort_count < 7)
1464 			return ERR_PTR(-EBUSY);
1465 
1466 		size = offsetof(struct fm10k_l2_accel, macvlan[7]);
1467 		l2_accel = kzalloc(size, GFP_KERNEL);
1468 		if (!l2_accel)
1469 			return ERR_PTR(-ENOMEM);
1470 
1471 		l2_accel->size = 7;
1472 		l2_accel->dglort = interface->glort;
1473 
1474 		/* update pointers */
1475 		fm10k_assign_l2_accel(interface, l2_accel);
1476 	/* do not expand if we are at our limit */
1477 	} else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
1478 		   (l2_accel->count == (interface->glort_count - 1))) {
1479 		return ERR_PTR(-EBUSY);
1480 	/* expand if we have hit the size limit */
1481 	} else if (l2_accel->count == l2_accel->size) {
1482 		old_l2_accel = l2_accel;
1483 		size = offsetof(struct fm10k_l2_accel,
1484 				macvlan[(l2_accel->size * 2) + 1]);
1485 		l2_accel = kzalloc(size, GFP_KERNEL);
1486 		if (!l2_accel)
1487 			return ERR_PTR(-ENOMEM);
1488 
1489 		memcpy(l2_accel, old_l2_accel,
1490 		       offsetof(struct fm10k_l2_accel,
1491 				macvlan[old_l2_accel->size]));
1492 
1493 		l2_accel->size = (old_l2_accel->size * 2) + 1;
1494 
1495 		/* update pointers */
1496 		fm10k_assign_l2_accel(interface, l2_accel);
1497 		kfree_rcu(old_l2_accel, rcu);
1498 	}
1499 
1500 	/* add macvlan to accel table, and record GLORT for position */
1501 	for (i = 0; i < l2_accel->size; i++) {
1502 		if (!l2_accel->macvlan[i])
1503 			break;
1504 	}
1505 
1506 	/* record station */
1507 	l2_accel->macvlan[i] = sdev;
1508 	l2_accel->count++;
1509 
1510 	/* configure default DGLORT mapping for RSS/DCB */
1511 	dglort.idx = fm10k_dglort_pf_rss;
1512 	dglort.inner_rss = 1;
1513 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1514 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1515 	dglort.glort = interface->glort;
1516 	dglort.shared_l = fls(l2_accel->size);
1517 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1518 
1519 	/* Add rules for this specific dglort to the switch */
1520 	fm10k_mbx_lock(interface);
1521 
1522 	glort = l2_accel->dglort + 1 + i;
1523 
1524 	if (fm10k_host_mbx_ready(interface)) {
1525 		hw->mac.ops.update_xcast_mode(hw, glort,
1526 					      FM10K_XCAST_MODE_NONE);
1527 		fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1528 					hw->mac.default_vid, true);
1529 	}
1530 
1531 	fm10k_mbx_unlock(interface);
1532 
1533 	return sdev;
1534 }
1535 
1536 static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
1537 {
1538 	struct fm10k_intfc *interface = netdev_priv(dev);
1539 	struct fm10k_l2_accel *l2_accel = READ_ONCE(interface->l2_accel);
1540 	struct fm10k_dglort_cfg dglort = { 0 };
1541 	struct fm10k_hw *hw = &interface->hw;
1542 	struct net_device *sdev = priv;
1543 	int i;
1544 	u16 glort;
1545 
1546 	if (!l2_accel)
1547 		return;
1548 
1549 	/* search table for matching interface */
1550 	for (i = 0; i < l2_accel->size; i++) {
1551 		if (l2_accel->macvlan[i] == sdev)
1552 			break;
1553 	}
1554 
1555 	/* exit if macvlan not found */
1556 	if (i == l2_accel->size)
1557 		return;
1558 
1559 	/* Remove any rules specific to this dglort */
1560 	fm10k_mbx_lock(interface);
1561 
1562 	glort = l2_accel->dglort + 1 + i;
1563 
1564 	if (fm10k_host_mbx_ready(interface)) {
1565 		hw->mac.ops.update_xcast_mode(hw, glort,
1566 					      FM10K_XCAST_MODE_NONE);
1567 		fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1568 					hw->mac.default_vid, false);
1569 	}
1570 
1571 	fm10k_mbx_unlock(interface);
1572 
1573 	/* record removal */
1574 	l2_accel->macvlan[i] = NULL;
1575 	l2_accel->count--;
1576 
1577 	/* configure default DGLORT mapping for RSS/DCB */
1578 	dglort.idx = fm10k_dglort_pf_rss;
1579 	dglort.inner_rss = 1;
1580 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1581 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1582 	dglort.glort = interface->glort;
1583 	dglort.shared_l = fls(l2_accel->size);
1584 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1585 
1586 	/* If table is empty remove it */
1587 	if (l2_accel->count == 0) {
1588 		fm10k_assign_l2_accel(interface, NULL);
1589 		kfree_rcu(l2_accel, rcu);
1590 	}
1591 }
1592 
1593 static netdev_features_t fm10k_features_check(struct sk_buff *skb,
1594 					      struct net_device *dev,
1595 					      netdev_features_t features)
1596 {
1597 	if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
1598 		return features;
1599 
1600 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1601 }
1602 
1603 static const struct net_device_ops fm10k_netdev_ops = {
1604 	.ndo_open		= fm10k_open,
1605 	.ndo_stop		= fm10k_close,
1606 	.ndo_validate_addr	= eth_validate_addr,
1607 	.ndo_start_xmit		= fm10k_xmit_frame,
1608 	.ndo_set_mac_address	= fm10k_set_mac,
1609 	.ndo_tx_timeout		= fm10k_tx_timeout,
1610 	.ndo_vlan_rx_add_vid	= fm10k_vlan_rx_add_vid,
1611 	.ndo_vlan_rx_kill_vid	= fm10k_vlan_rx_kill_vid,
1612 	.ndo_set_rx_mode	= fm10k_set_rx_mode,
1613 	.ndo_get_stats64	= fm10k_get_stats64,
1614 	.ndo_setup_tc		= __fm10k_setup_tc,
1615 	.ndo_set_vf_mac		= fm10k_ndo_set_vf_mac,
1616 	.ndo_set_vf_vlan	= fm10k_ndo_set_vf_vlan,
1617 	.ndo_set_vf_rate	= fm10k_ndo_set_vf_bw,
1618 	.ndo_get_vf_config	= fm10k_ndo_get_vf_config,
1619 	.ndo_udp_tunnel_add	= fm10k_udp_tunnel_add,
1620 	.ndo_udp_tunnel_del	= fm10k_udp_tunnel_del,
1621 	.ndo_dfwd_add_station	= fm10k_dfwd_add_station,
1622 	.ndo_dfwd_del_station	= fm10k_dfwd_del_station,
1623 #ifdef CONFIG_NET_POLL_CONTROLLER
1624 	.ndo_poll_controller	= fm10k_netpoll,
1625 #endif
1626 	.ndo_features_check	= fm10k_features_check,
1627 };
1628 
1629 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
1630 
1631 struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
1632 {
1633 	netdev_features_t hw_features;
1634 	struct fm10k_intfc *interface;
1635 	struct net_device *dev;
1636 
1637 	dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
1638 	if (!dev)
1639 		return NULL;
1640 
1641 	/* set net device and ethtool ops */
1642 	dev->netdev_ops = &fm10k_netdev_ops;
1643 	fm10k_set_ethtool_ops(dev);
1644 
1645 	/* configure default debug level */
1646 	interface = netdev_priv(dev);
1647 	interface->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
1648 
1649 	/* configure default features */
1650 	dev->features |= NETIF_F_IP_CSUM |
1651 			 NETIF_F_IPV6_CSUM |
1652 			 NETIF_F_SG |
1653 			 NETIF_F_TSO |
1654 			 NETIF_F_TSO6 |
1655 			 NETIF_F_TSO_ECN |
1656 			 NETIF_F_RXHASH |
1657 			 NETIF_F_RXCSUM;
1658 
1659 	/* Only the PF can support VXLAN and NVGRE tunnel offloads */
1660 	if (info->mac == fm10k_mac_pf) {
1661 		dev->hw_enc_features = NETIF_F_IP_CSUM |
1662 				       NETIF_F_TSO |
1663 				       NETIF_F_TSO6 |
1664 				       NETIF_F_TSO_ECN |
1665 				       NETIF_F_GSO_UDP_TUNNEL |
1666 				       NETIF_F_IPV6_CSUM |
1667 				       NETIF_F_SG;
1668 
1669 		dev->features |= NETIF_F_GSO_UDP_TUNNEL;
1670 	}
1671 
1672 	/* all features defined to this point should be changeable */
1673 	hw_features = dev->features;
1674 
1675 	/* allow user to enable L2 forwarding acceleration */
1676 	hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
1677 
1678 	/* configure VLAN features */
1679 	dev->vlan_features |= dev->features;
1680 
1681 	/* we want to leave these both on as we cannot disable VLAN tag
1682 	 * insertion or stripping on the hardware since it is contained
1683 	 * in the FTAG and not in the frame itself.
1684 	 */
1685 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1686 			 NETIF_F_HW_VLAN_CTAG_RX |
1687 			 NETIF_F_HW_VLAN_CTAG_FILTER;
1688 
1689 	dev->priv_flags |= IFF_UNICAST_FLT;
1690 
1691 	dev->hw_features |= hw_features;
1692 
1693 	/* MTU range: 68 - 15342 */
1694 	dev->min_mtu = ETH_MIN_MTU;
1695 	dev->max_mtu = FM10K_MAX_JUMBO_FRAME_SIZE;
1696 
1697 	return dev;
1698 }
1699