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