1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 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 = 0;
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 = 0;
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 	struct fm10k_tx_buffer *tx_buffer;
173 	unsigned long size;
174 	u16 i;
175 
176 	/* ring already cleared, nothing to do */
177 	if (!tx_ring->tx_buffer)
178 		return;
179 
180 	/* Free all the Tx ring sk_buffs */
181 	for (i = 0; i < tx_ring->count; i++) {
182 		tx_buffer = &tx_ring->tx_buffer[i];
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 	if (rx_ring->skb)
257 		dev_kfree_skb(rx_ring->skb);
258 	rx_ring->skb = NULL;
259 
260 	/* Free all the Rx ring sk_buffs */
261 	for (i = 0; i < rx_ring->count; i++) {
262 		struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
263 		/* clean-up will only set page pointer to NULL */
264 		if (!buffer->page)
265 			continue;
266 
267 		dma_unmap_page(rx_ring->dev, buffer->dma,
268 			       PAGE_SIZE, DMA_FROM_DEVICE);
269 		__free_page(buffer->page);
270 
271 		buffer->page = NULL;
272 	}
273 
274 	size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
275 	memset(rx_ring->rx_buffer, 0, size);
276 
277 	/* Zero out the descriptor ring */
278 	memset(rx_ring->desc, 0, rx_ring->size);
279 
280 	rx_ring->next_to_alloc = 0;
281 	rx_ring->next_to_clean = 0;
282 	rx_ring->next_to_use = 0;
283 }
284 
285 /**
286  * fm10k_free_rx_resources - Free Rx Resources
287  * @rx_ring: ring to clean the resources from
288  *
289  * Free all receive software resources
290  **/
291 void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
292 {
293 	fm10k_clean_rx_ring(rx_ring);
294 
295 	vfree(rx_ring->rx_buffer);
296 	rx_ring->rx_buffer = NULL;
297 
298 	/* if not set, then don't free */
299 	if (!rx_ring->desc)
300 		return;
301 
302 	dma_free_coherent(rx_ring->dev, rx_ring->size,
303 			  rx_ring->desc, rx_ring->dma);
304 
305 	rx_ring->desc = NULL;
306 }
307 
308 /**
309  * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
310  * @interface: board private structure
311  **/
312 void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
313 {
314 	int i;
315 
316 	for (i = 0; i < interface->num_rx_queues; i++)
317 		fm10k_clean_rx_ring(interface->rx_ring[i]);
318 }
319 
320 /**
321  * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
322  * @interface: board private structure
323  *
324  * Free all receive software resources
325  **/
326 static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
327 {
328 	int i = interface->num_rx_queues;
329 
330 	while (i--)
331 		fm10k_free_rx_resources(interface->rx_ring[i]);
332 }
333 
334 /**
335  * fm10k_request_glort_range - Request GLORTs for use in configuring rules
336  * @interface: board private structure
337  *
338  * This function allocates a range of glorts for this interface to use.
339  **/
340 static void fm10k_request_glort_range(struct fm10k_intfc *interface)
341 {
342 	struct fm10k_hw *hw = &interface->hw;
343 	u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
344 
345 	/* establish GLORT base */
346 	interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
347 	interface->glort_count = 0;
348 
349 	/* nothing we can do until mask is allocated */
350 	if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
351 		return;
352 
353 	/* we support 3 possible GLORT configurations.
354 	 * 1: VFs consume all but the last 1
355 	 * 2: VFs and PF split glorts with possible gap between
356 	 * 3: VFs allocated first 64, all others belong to PF
357 	 */
358 	if (mask <= hw->iov.total_vfs) {
359 		interface->glort_count = 1;
360 		interface->glort += mask;
361 	} else if (mask < 64) {
362 		interface->glort_count = (mask + 1) / 2;
363 		interface->glort += interface->glort_count;
364 	} else {
365 		interface->glort_count = mask - 63;
366 		interface->glort += 64;
367 	}
368 }
369 
370 /**
371  * fm10k_free_udp_port_info
372  * @interface: board private structure
373  *
374  * This function frees both geneve_port and vxlan_port structures
375  **/
376 static void fm10k_free_udp_port_info(struct fm10k_intfc *interface)
377 {
378 	struct fm10k_udp_port *port;
379 
380 	/* flush all entries from vxlan list */
381 	port = list_first_entry_or_null(&interface->vxlan_port,
382 					struct fm10k_udp_port, list);
383 	while (port) {
384 		list_del(&port->list);
385 		kfree(port);
386 		port = list_first_entry_or_null(&interface->vxlan_port,
387 						struct fm10k_udp_port,
388 						list);
389 	}
390 
391 	/* flush all entries from geneve list */
392 	port = list_first_entry_or_null(&interface->geneve_port,
393 					struct fm10k_udp_port, list);
394 	while (port) {
395 		list_del(&port->list);
396 		kfree(port);
397 		port = list_first_entry_or_null(&interface->vxlan_port,
398 						struct fm10k_udp_port,
399 						list);
400 	}
401 }
402 
403 /**
404  * fm10k_restore_udp_port_info
405  * @interface: board private structure
406  *
407  * This function restores the value in the tunnel_cfg register(s) after reset
408  **/
409 static void fm10k_restore_udp_port_info(struct fm10k_intfc *interface)
410 {
411 	struct fm10k_hw *hw = &interface->hw;
412 	struct fm10k_udp_port *port;
413 
414 	/* only the PF supports configuring tunnels */
415 	if (hw->mac.type != fm10k_mac_pf)
416 		return;
417 
418 	port = list_first_entry_or_null(&interface->vxlan_port,
419 					struct fm10k_udp_port, list);
420 
421 	/* restore tunnel configuration register */
422 	fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
423 			(port ? ntohs(port->port) : 0) |
424 			(ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
425 
426 	port = list_first_entry_or_null(&interface->geneve_port,
427 					struct fm10k_udp_port, list);
428 
429 	/* restore Geneve tunnel configuration register */
430 	fm10k_write_reg(hw, FM10K_TUNNEL_CFG_GENEVE,
431 			(port ? ntohs(port->port) : 0));
432 }
433 
434 static struct fm10k_udp_port *
435 fm10k_remove_tunnel_port(struct list_head *ports,
436 			 struct udp_tunnel_info *ti)
437 {
438 	struct fm10k_udp_port *port;
439 
440 	list_for_each_entry(port, ports, list) {
441 		if ((port->port == ti->port) &&
442 		    (port->sa_family == ti->sa_family)) {
443 			list_del(&port->list);
444 			return port;
445 		}
446 	}
447 
448 	return NULL;
449 }
450 
451 static void fm10k_insert_tunnel_port(struct list_head *ports,
452 				     struct udp_tunnel_info *ti)
453 {
454 	struct fm10k_udp_port *port;
455 
456 	/* remove existing port entry from the list so that the newest items
457 	 * are always at the tail of the list.
458 	 */
459 	port = fm10k_remove_tunnel_port(ports, ti);
460 	if (!port) {
461 		port = kmalloc(sizeof(*port), GFP_ATOMIC);
462 		if  (!port)
463 			return;
464 		port->port = ti->port;
465 		port->sa_family = ti->sa_family;
466 	}
467 
468 	list_add_tail(&port->list, ports);
469 }
470 
471 /**
472  * fm10k_udp_tunnel_add
473  * @dev: network interface device structure
474  * @ti: Tunnel endpoint information
475  *
476  * This function is called when a new UDP tunnel port has been added.
477  * Due to hardware restrictions, only one port per type can be offloaded at
478  * once.
479  **/
480 static void fm10k_udp_tunnel_add(struct net_device *dev,
481 				 struct udp_tunnel_info *ti)
482 {
483 	struct fm10k_intfc *interface = netdev_priv(dev);
484 
485 	/* only the PF supports configuring tunnels */
486 	if (interface->hw.mac.type != fm10k_mac_pf)
487 		return;
488 
489 	switch (ti->type) {
490 	case UDP_TUNNEL_TYPE_VXLAN:
491 		fm10k_insert_tunnel_port(&interface->vxlan_port, ti);
492 		break;
493 	case UDP_TUNNEL_TYPE_GENEVE:
494 		fm10k_insert_tunnel_port(&interface->geneve_port, ti);
495 		break;
496 	default:
497 		return;
498 	}
499 
500 	fm10k_restore_udp_port_info(interface);
501 }
502 
503 /**
504  * fm10k_udp_tunnel_del
505  * @dev: network interface device structure
506  * @ti: Tunnel end point information
507  *
508  * This function is called when a new UDP tunnel port is deleted. The freed
509  * port will be removed from the list, then we reprogram the offloaded port
510  * based on the head of the list.
511  **/
512 static void fm10k_udp_tunnel_del(struct net_device *dev,
513 				 struct udp_tunnel_info *ti)
514 {
515 	struct fm10k_intfc *interface = netdev_priv(dev);
516 	struct fm10k_udp_port *port = NULL;
517 
518 	if (interface->hw.mac.type != fm10k_mac_pf)
519 		return;
520 
521 	switch (ti->type) {
522 	case UDP_TUNNEL_TYPE_VXLAN:
523 		port = fm10k_remove_tunnel_port(&interface->vxlan_port, ti);
524 		break;
525 	case UDP_TUNNEL_TYPE_GENEVE:
526 		port = fm10k_remove_tunnel_port(&interface->geneve_port, ti);
527 		break;
528 	default:
529 		return;
530 	}
531 
532 	/* if we did remove a port we need to free its memory */
533 	kfree(port);
534 
535 	fm10k_restore_udp_port_info(interface);
536 }
537 
538 /**
539  * fm10k_open - Called when a network interface is made active
540  * @netdev: network interface device structure
541  *
542  * Returns 0 on success, negative value on failure
543  *
544  * The open entry point is called when a network interface is made
545  * active by the system (IFF_UP).  At this point all resources needed
546  * for transmit and receive operations are allocated, the interrupt
547  * handler is registered with the OS, the watchdog timer is started,
548  * and the stack is notified that the interface is ready.
549  **/
550 int fm10k_open(struct net_device *netdev)
551 {
552 	struct fm10k_intfc *interface = netdev_priv(netdev);
553 	int err;
554 
555 	/* allocate transmit descriptors */
556 	err = fm10k_setup_all_tx_resources(interface);
557 	if (err)
558 		goto err_setup_tx;
559 
560 	/* allocate receive descriptors */
561 	err = fm10k_setup_all_rx_resources(interface);
562 	if (err)
563 		goto err_setup_rx;
564 
565 	/* allocate interrupt resources */
566 	err = fm10k_qv_request_irq(interface);
567 	if (err)
568 		goto err_req_irq;
569 
570 	/* setup GLORT assignment for this port */
571 	fm10k_request_glort_range(interface);
572 
573 	/* Notify the stack of the actual queue counts */
574 	err = netif_set_real_num_tx_queues(netdev,
575 					   interface->num_tx_queues);
576 	if (err)
577 		goto err_set_queues;
578 
579 	err = netif_set_real_num_rx_queues(netdev,
580 					   interface->num_rx_queues);
581 	if (err)
582 		goto err_set_queues;
583 
584 	udp_tunnel_get_rx_info(netdev);
585 
586 	fm10k_up(interface);
587 
588 	return 0;
589 
590 err_set_queues:
591 	fm10k_qv_free_irq(interface);
592 err_req_irq:
593 	fm10k_free_all_rx_resources(interface);
594 err_setup_rx:
595 	fm10k_free_all_tx_resources(interface);
596 err_setup_tx:
597 	return err;
598 }
599 
600 /**
601  * fm10k_close - Disables a network interface
602  * @netdev: network interface device structure
603  *
604  * Returns 0, this is not allowed to fail
605  *
606  * The close entry point is called when an interface is de-activated
607  * by the OS.  The hardware is still under the drivers control, but
608  * needs to be disabled.  A global MAC reset is issued to stop the
609  * hardware, and all transmit and receive resources are freed.
610  **/
611 int fm10k_close(struct net_device *netdev)
612 {
613 	struct fm10k_intfc *interface = netdev_priv(netdev);
614 
615 	fm10k_down(interface);
616 
617 	fm10k_qv_free_irq(interface);
618 
619 	fm10k_free_udp_port_info(interface);
620 
621 	fm10k_free_all_tx_resources(interface);
622 	fm10k_free_all_rx_resources(interface);
623 
624 	return 0;
625 }
626 
627 static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
628 {
629 	struct fm10k_intfc *interface = netdev_priv(dev);
630 	int num_tx_queues = READ_ONCE(interface->num_tx_queues);
631 	unsigned int r_idx = skb->queue_mapping;
632 	int err;
633 
634 	if (!num_tx_queues)
635 		return NETDEV_TX_BUSY;
636 
637 	if ((skb->protocol == htons(ETH_P_8021Q)) &&
638 	    !skb_vlan_tag_present(skb)) {
639 		/* FM10K only supports hardware tagging, any tags in frame
640 		 * are considered 2nd level or "outer" tags
641 		 */
642 		struct vlan_hdr *vhdr;
643 		__be16 proto;
644 
645 		/* make sure skb is not shared */
646 		skb = skb_share_check(skb, GFP_ATOMIC);
647 		if (!skb)
648 			return NETDEV_TX_OK;
649 
650 		/* make sure there is enough room to move the ethernet header */
651 		if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
652 			return NETDEV_TX_OK;
653 
654 		/* verify the skb head is not shared */
655 		err = skb_cow_head(skb, 0);
656 		if (err) {
657 			dev_kfree_skb(skb);
658 			return NETDEV_TX_OK;
659 		}
660 
661 		/* locate VLAN header */
662 		vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
663 
664 		/* pull the 2 key pieces of data out of it */
665 		__vlan_hwaccel_put_tag(skb,
666 				       htons(ETH_P_8021Q),
667 				       ntohs(vhdr->h_vlan_TCI));
668 		proto = vhdr->h_vlan_encapsulated_proto;
669 		skb->protocol = (ntohs(proto) >= 1536) ? proto :
670 							 htons(ETH_P_802_2);
671 
672 		/* squash it by moving the ethernet addresses up 4 bytes */
673 		memmove(skb->data + VLAN_HLEN, skb->data, 12);
674 		__skb_pull(skb, VLAN_HLEN);
675 		skb_reset_mac_header(skb);
676 	}
677 
678 	/* The minimum packet size for a single buffer is 17B so pad the skb
679 	 * in order to meet this minimum size requirement.
680 	 */
681 	if (unlikely(skb->len < 17)) {
682 		int pad_len = 17 - skb->len;
683 
684 		if (skb_pad(skb, pad_len))
685 			return NETDEV_TX_OK;
686 		__skb_put(skb, pad_len);
687 	}
688 
689 	if (r_idx >= num_tx_queues)
690 		r_idx %= num_tx_queues;
691 
692 	err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
693 
694 	return err;
695 }
696 
697 /**
698  * fm10k_tx_timeout - Respond to a Tx Hang
699  * @netdev: network interface device structure
700  **/
701 static void fm10k_tx_timeout(struct net_device *netdev)
702 {
703 	struct fm10k_intfc *interface = netdev_priv(netdev);
704 	bool real_tx_hang = false;
705 	int i;
706 
707 #define TX_TIMEO_LIMIT 16000
708 	for (i = 0; i < interface->num_tx_queues; i++) {
709 		struct fm10k_ring *tx_ring = interface->tx_ring[i];
710 
711 		if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
712 			real_tx_hang = true;
713 	}
714 
715 	if (real_tx_hang) {
716 		fm10k_tx_timeout_reset(interface);
717 	} else {
718 		netif_info(interface, drv, netdev,
719 			   "Fake Tx hang detected with timeout of %d seconds\n",
720 			   netdev->watchdog_timeo / HZ);
721 
722 		/* fake Tx hang - increase the kernel timeout */
723 		if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
724 			netdev->watchdog_timeo *= 2;
725 	}
726 }
727 
728 /**
729  * fm10k_host_mbx_ready - Check PF interface's mailbox readiness
730  * @interface: board private structure
731  *
732  * This function checks if the PF interface's mailbox is ready before queueing
733  * mailbox messages for transmission. This will prevent filling the TX mailbox
734  * queue when the receiver is not ready. VF interfaces are exempt from this
735  * check since it will block all PF-VF mailbox messages from being sent from
736  * the VF to the PF at initialization.
737  **/
738 static bool fm10k_host_mbx_ready(struct fm10k_intfc *interface)
739 {
740 	struct fm10k_hw *hw = &interface->hw;
741 
742 	return (hw->mac.type == fm10k_mac_vf || interface->host_ready);
743 }
744 
745 /**
746  * fm10k_queue_vlan_request - Queue a VLAN update request
747  * @interface: the fm10k interface structure
748  * @vid: the VLAN vid
749  * @vsi: VSI index number
750  * @set: whether to set or clear
751  *
752  * This function queues up a VLAN update. For VFs, this must be sent to the
753  * managing PF over the mailbox. For PFs, we'll use the same handling so that
754  * it's similar to the VF. This avoids storming the PF<->VF mailbox with too
755  * many VLAN updates during reset.
756  */
757 int fm10k_queue_vlan_request(struct fm10k_intfc *interface,
758 			     u32 vid, u8 vsi, bool set)
759 {
760 	struct fm10k_macvlan_request *request;
761 	unsigned long flags;
762 
763 	/* This must be atomic since we may be called while the netdev
764 	 * addr_list_lock is held
765 	 */
766 	request = kzalloc(sizeof(*request), GFP_ATOMIC);
767 	if (!request)
768 		return -ENOMEM;
769 
770 	request->type = FM10K_VLAN_REQUEST;
771 	request->vlan.vid = vid;
772 	request->vlan.vsi = vsi;
773 	request->set = set;
774 
775 	spin_lock_irqsave(&interface->macvlan_lock, flags);
776 	list_add_tail(&request->list, &interface->macvlan_requests);
777 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
778 
779 	fm10k_macvlan_schedule(interface);
780 
781 	return 0;
782 }
783 
784 /**
785  * fm10k_queue_mac_request - Queue a MAC update request
786  * @interface: the fm10k interface structure
787  * @glort: the target glort for this update
788  * @addr: the address to update
789  * @vid: the vid to update
790  * @set: whether to add or remove
791  *
792  * This function queues up a MAC request for sending to the switch manager.
793  * A separate thread monitors the queue and sends updates to the switch
794  * manager. Return 0 on success, and negative error code on failure.
795  **/
796 int fm10k_queue_mac_request(struct fm10k_intfc *interface, u16 glort,
797 			    const unsigned char *addr, u16 vid, bool set)
798 {
799 	struct fm10k_macvlan_request *request;
800 	unsigned long flags;
801 
802 	/* This must be atomic since we may be called while the netdev
803 	 * addr_list_lock is held
804 	 */
805 	request = kzalloc(sizeof(*request), GFP_ATOMIC);
806 	if (!request)
807 		return -ENOMEM;
808 
809 	if (is_multicast_ether_addr(addr))
810 		request->type = FM10K_MC_MAC_REQUEST;
811 	else
812 		request->type = FM10K_UC_MAC_REQUEST;
813 
814 	ether_addr_copy(request->mac.addr, addr);
815 	request->mac.glort = glort;
816 	request->mac.vid = vid;
817 	request->set = set;
818 
819 	spin_lock_irqsave(&interface->macvlan_lock, flags);
820 	list_add_tail(&request->list, &interface->macvlan_requests);
821 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
822 
823 	fm10k_macvlan_schedule(interface);
824 
825 	return 0;
826 }
827 
828 /**
829  * fm10k_clear_macvlan_queue - Cancel pending updates for a given glort
830  * @interface: the fm10k interface structure
831  * @glort: the target glort to clear
832  * @vlans: true to clear VLAN messages, false to ignore them
833  *
834  * Cancel any outstanding MAC/VLAN requests for a given glort. This is
835  * expected to be called when a logical port goes down.
836  **/
837 void fm10k_clear_macvlan_queue(struct fm10k_intfc *interface,
838 			       u16 glort, bool vlans)
839 
840 {
841 	struct fm10k_macvlan_request *r, *tmp;
842 	unsigned long flags;
843 
844 	spin_lock_irqsave(&interface->macvlan_lock, flags);
845 
846 	/* Free any outstanding MAC/VLAN requests for this interface */
847 	list_for_each_entry_safe(r, tmp, &interface->macvlan_requests, list) {
848 		switch (r->type) {
849 		case FM10K_MC_MAC_REQUEST:
850 		case FM10K_UC_MAC_REQUEST:
851 			/* Don't free requests for other interfaces */
852 			if (r->mac.glort != glort)
853 				break;
854 			/* fall through */
855 		case FM10K_VLAN_REQUEST:
856 			if (vlans) {
857 				list_del(&r->list);
858 				kfree(r);
859 			}
860 			break;
861 		}
862 	}
863 
864 	spin_unlock_irqrestore(&interface->macvlan_lock, flags);
865 }
866 
867 static int fm10k_uc_vlan_unsync(struct net_device *netdev,
868 				const unsigned char *uc_addr)
869 {
870 	struct fm10k_intfc *interface = netdev_priv(netdev);
871 	u16 glort = interface->glort;
872 	u16 vid = interface->vid;
873 	bool set = !!(vid / VLAN_N_VID);
874 	int err = -EHOSTDOWN;
875 
876 	/* drop any leading bits on the VLAN ID */
877 	vid &= VLAN_N_VID - 1;
878 
879 	err = fm10k_queue_mac_request(interface, glort, uc_addr, vid, set);
880 	if (err)
881 		return err;
882 
883 	/* return non-zero value as we are only doing a partial sync/unsync */
884 	return 1;
885 }
886 
887 static int fm10k_mc_vlan_unsync(struct net_device *netdev,
888 				const unsigned char *mc_addr)
889 {
890 	struct fm10k_intfc *interface = netdev_priv(netdev);
891 	u16 glort = interface->glort;
892 	u16 vid = interface->vid;
893 	bool set = !!(vid / VLAN_N_VID);
894 	int err = -EHOSTDOWN;
895 
896 	/* drop any leading bits on the VLAN ID */
897 	vid &= VLAN_N_VID - 1;
898 
899 	err = fm10k_queue_mac_request(interface, glort, mc_addr, vid, set);
900 	if (err)
901 		return err;
902 
903 	/* return non-zero value as we are only doing a partial sync/unsync */
904 	return 1;
905 }
906 
907 static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
908 {
909 	struct fm10k_intfc *interface = netdev_priv(netdev);
910 	struct fm10k_l2_accel *l2_accel = interface->l2_accel;
911 	struct fm10k_hw *hw = &interface->hw;
912 	u16 glort;
913 	s32 err;
914 	int i;
915 
916 	/* updates do not apply to VLAN 0 */
917 	if (!vid)
918 		return 0;
919 
920 	if (vid >= VLAN_N_VID)
921 		return -EINVAL;
922 
923 	/* Verify that we have permission to add VLANs. If this is a request
924 	 * to remove a VLAN, we still want to allow the user to remove the
925 	 * VLAN device. In that case, we need to clear the bit in the
926 	 * active_vlans bitmask.
927 	 */
928 	if (set && hw->mac.vlan_override)
929 		return -EACCES;
930 
931 	/* update active_vlans bitmask */
932 	set_bit(vid, interface->active_vlans);
933 	if (!set)
934 		clear_bit(vid, interface->active_vlans);
935 
936 	/* disable the default VLAN ID on ring if we have an active VLAN */
937 	for (i = 0; i < interface->num_rx_queues; i++) {
938 		struct fm10k_ring *rx_ring = interface->rx_ring[i];
939 		u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
940 
941 		if (test_bit(rx_vid, interface->active_vlans))
942 			rx_ring->vid |= FM10K_VLAN_CLEAR;
943 		else
944 			rx_ring->vid &= ~FM10K_VLAN_CLEAR;
945 	}
946 
947 	/* If our VLAN has been overridden, there is no reason to send VLAN
948 	 * removal requests as they will be silently ignored.
949 	 */
950 	if (hw->mac.vlan_override)
951 		return 0;
952 
953 	/* Do not remove default VLAN ID related entries from VLAN and MAC
954 	 * tables
955 	 */
956 	if (!set && vid == hw->mac.default_vid)
957 		return 0;
958 
959 	/* Do not throw an error if the interface is down. We will sync once
960 	 * we come up
961 	 */
962 	if (test_bit(__FM10K_DOWN, interface->state))
963 		return 0;
964 
965 	fm10k_mbx_lock(interface);
966 
967 	/* only need to update the VLAN if not in promiscuous mode */
968 	if (!(netdev->flags & IFF_PROMISC)) {
969 		err = fm10k_queue_vlan_request(interface, vid, 0, set);
970 		if (err)
971 			goto err_out;
972 	}
973 
974 	/* Update our base MAC address */
975 	err = fm10k_queue_mac_request(interface, interface->glort,
976 				      hw->mac.addr, vid, set);
977 	if (err)
978 		goto err_out;
979 
980 	/* Update L2 accelerated macvlan addresses */
981 	if (l2_accel) {
982 		for (i = 0; i < l2_accel->size; i++) {
983 			struct net_device *sdev = l2_accel->macvlan[i];
984 
985 			if (!sdev)
986 				continue;
987 
988 			glort = l2_accel->dglort + 1 + i;
989 
990 			fm10k_queue_mac_request(interface, glort,
991 						sdev->dev_addr,
992 						vid, set);
993 		}
994 	}
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 		/* synchronize macvlan addresses */
1237 		if (l2_accel) {
1238 			for (i = 0; i < l2_accel->size; i++) {
1239 				struct net_device *sdev = l2_accel->macvlan[i];
1240 
1241 				if (!sdev)
1242 					continue;
1243 
1244 				glort = l2_accel->dglort + 1 + i;
1245 
1246 				fm10k_queue_mac_request(interface, glort,
1247 							sdev->dev_addr,
1248 							vid, true);
1249 			}
1250 		}
1251 	}
1252 
1253 	/* update xcast mode before synchronizing addresses if host's mailbox
1254 	 * is ready
1255 	 */
1256 	if (fm10k_host_mbx_ready(interface))
1257 		hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
1258 
1259 	/* synchronize all of the addresses */
1260 	__dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
1261 	__dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
1262 
1263 	/* synchronize macvlan addresses */
1264 	if (l2_accel) {
1265 		for (i = 0; i < l2_accel->size; i++) {
1266 			struct net_device *sdev = l2_accel->macvlan[i];
1267 
1268 			if (!sdev)
1269 				continue;
1270 
1271 			glort = l2_accel->dglort + 1 + i;
1272 
1273 			hw->mac.ops.update_xcast_mode(hw, glort,
1274 						      FM10K_XCAST_MODE_NONE);
1275 			fm10k_queue_mac_request(interface, glort,
1276 						sdev->dev_addr,
1277 						hw->mac.default_vid, true);
1278 		}
1279 	}
1280 
1281 	fm10k_mbx_unlock(interface);
1282 
1283 	/* record updated xcast mode state */
1284 	interface->xcast_mode = xcast_mode;
1285 
1286 	/* Restore tunnel configuration */
1287 	fm10k_restore_udp_port_info(interface);
1288 }
1289 
1290 void fm10k_reset_rx_state(struct fm10k_intfc *interface)
1291 {
1292 	struct net_device *netdev = interface->netdev;
1293 	struct fm10k_hw *hw = &interface->hw;
1294 
1295 	/* Wait for MAC/VLAN work to finish */
1296 	while (test_bit(__FM10K_MACVLAN_SCHED, interface->state))
1297 		usleep_range(1000, 2000);
1298 
1299 	/* Cancel pending MAC/VLAN requests */
1300 	fm10k_clear_macvlan_queue(interface, interface->glort, true);
1301 
1302 	fm10k_mbx_lock(interface);
1303 
1304 	/* clear the logical port state on lower device if host's mailbox is
1305 	 * ready
1306 	 */
1307 	if (fm10k_host_mbx_ready(interface))
1308 		hw->mac.ops.update_lport_state(hw, interface->glort,
1309 					       interface->glort_count, false);
1310 
1311 	fm10k_mbx_unlock(interface);
1312 
1313 	/* reset flags to default state */
1314 	interface->xcast_mode = FM10K_XCAST_MODE_NONE;
1315 
1316 	/* clear the sync flag since the lport has been dropped */
1317 	__dev_uc_unsync(netdev, NULL);
1318 	__dev_mc_unsync(netdev, NULL);
1319 }
1320 
1321 /**
1322  * fm10k_get_stats64 - Get System Network Statistics
1323  * @netdev: network interface device structure
1324  * @stats: storage space for 64bit statistics
1325  *
1326  * Obtain 64bit statistics in a way that is safe for both 32bit and 64bit
1327  * architectures.
1328  */
1329 static void fm10k_get_stats64(struct net_device *netdev,
1330 			      struct rtnl_link_stats64 *stats)
1331 {
1332 	struct fm10k_intfc *interface = netdev_priv(netdev);
1333 	struct fm10k_ring *ring;
1334 	unsigned int start, i;
1335 	u64 bytes, packets;
1336 
1337 	rcu_read_lock();
1338 
1339 	for (i = 0; i < interface->num_rx_queues; i++) {
1340 		ring = READ_ONCE(interface->rx_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->rx_packets += packets;
1352 		stats->rx_bytes   += bytes;
1353 	}
1354 
1355 	for (i = 0; i < interface->num_tx_queues; i++) {
1356 		ring = READ_ONCE(interface->tx_ring[i]);
1357 
1358 		if (!ring)
1359 			continue;
1360 
1361 		do {
1362 			start = u64_stats_fetch_begin_irq(&ring->syncp);
1363 			packets = ring->stats.packets;
1364 			bytes   = ring->stats.bytes;
1365 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1366 
1367 		stats->tx_packets += packets;
1368 		stats->tx_bytes   += bytes;
1369 	}
1370 
1371 	rcu_read_unlock();
1372 
1373 	/* following stats updated by fm10k_service_task() */
1374 	stats->rx_missed_errors	= netdev->stats.rx_missed_errors;
1375 }
1376 
1377 int fm10k_setup_tc(struct net_device *dev, u8 tc)
1378 {
1379 	struct fm10k_intfc *interface = netdev_priv(dev);
1380 	int err;
1381 
1382 	/* Currently only the PF supports priority classes */
1383 	if (tc && (interface->hw.mac.type != fm10k_mac_pf))
1384 		return -EINVAL;
1385 
1386 	/* Hardware supports up to 8 traffic classes */
1387 	if (tc > 8)
1388 		return -EINVAL;
1389 
1390 	/* Hardware has to reinitialize queues to match packet
1391 	 * buffer alignment. Unfortunately, the hardware is not
1392 	 * flexible enough to do this dynamically.
1393 	 */
1394 	if (netif_running(dev))
1395 		fm10k_close(dev);
1396 
1397 	fm10k_mbx_free_irq(interface);
1398 
1399 	fm10k_clear_queueing_scheme(interface);
1400 
1401 	/* we expect the prio_tc map to be repopulated later */
1402 	netdev_reset_tc(dev);
1403 	netdev_set_num_tc(dev, tc);
1404 
1405 	err = fm10k_init_queueing_scheme(interface);
1406 	if (err)
1407 		goto err_queueing_scheme;
1408 
1409 	err = fm10k_mbx_request_irq(interface);
1410 	if (err)
1411 		goto err_mbx_irq;
1412 
1413 	err = netif_running(dev) ? fm10k_open(dev) : 0;
1414 	if (err)
1415 		goto err_open;
1416 
1417 	/* flag to indicate SWPRI has yet to be updated */
1418 	set_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
1419 
1420 	return 0;
1421 err_open:
1422 	fm10k_mbx_free_irq(interface);
1423 err_mbx_irq:
1424 	fm10k_clear_queueing_scheme(interface);
1425 err_queueing_scheme:
1426 	netif_device_detach(dev);
1427 
1428 	return err;
1429 }
1430 
1431 static int __fm10k_setup_tc(struct net_device *dev, enum tc_setup_type type,
1432 			    void *type_data)
1433 {
1434 	struct tc_mqprio_qopt *mqprio = type_data;
1435 
1436 	if (type != TC_SETUP_QDISC_MQPRIO)
1437 		return -EOPNOTSUPP;
1438 
1439 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1440 
1441 	return fm10k_setup_tc(dev, mqprio->num_tc);
1442 }
1443 
1444 static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
1445 				  struct fm10k_l2_accel *l2_accel)
1446 {
1447 	struct fm10k_ring *ring;
1448 	int i;
1449 
1450 	for (i = 0; i < interface->num_rx_queues; i++) {
1451 		ring = interface->rx_ring[i];
1452 		rcu_assign_pointer(ring->l2_accel, l2_accel);
1453 	}
1454 
1455 	interface->l2_accel = l2_accel;
1456 }
1457 
1458 static void *fm10k_dfwd_add_station(struct net_device *dev,
1459 				    struct net_device *sdev)
1460 {
1461 	struct fm10k_intfc *interface = netdev_priv(dev);
1462 	struct fm10k_l2_accel *l2_accel = interface->l2_accel;
1463 	struct fm10k_l2_accel *old_l2_accel = NULL;
1464 	struct fm10k_dglort_cfg dglort = { 0 };
1465 	struct fm10k_hw *hw = &interface->hw;
1466 	int size = 0, i;
1467 	u16 vid, glort;
1468 
1469 	/* The hardware supported by fm10k only filters on the destination MAC
1470 	 * address. In order to avoid issues we only support offloading modes
1471 	 * where the hardware can actually provide the functionality.
1472 	 */
1473 	if (!macvlan_supports_dest_filter(sdev))
1474 		return ERR_PTR(-EMEDIUMTYPE);
1475 
1476 	/* allocate l2 accel structure if it is not available */
1477 	if (!l2_accel) {
1478 		/* verify there is enough free GLORTs to support l2_accel */
1479 		if (interface->glort_count < 7)
1480 			return ERR_PTR(-EBUSY);
1481 
1482 		size = offsetof(struct fm10k_l2_accel, macvlan[7]);
1483 		l2_accel = kzalloc(size, GFP_KERNEL);
1484 		if (!l2_accel)
1485 			return ERR_PTR(-ENOMEM);
1486 
1487 		l2_accel->size = 7;
1488 		l2_accel->dglort = interface->glort;
1489 
1490 		/* update pointers */
1491 		fm10k_assign_l2_accel(interface, l2_accel);
1492 	/* do not expand if we are at our limit */
1493 	} else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
1494 		   (l2_accel->count == (interface->glort_count - 1))) {
1495 		return ERR_PTR(-EBUSY);
1496 	/* expand if we have hit the size limit */
1497 	} else if (l2_accel->count == l2_accel->size) {
1498 		old_l2_accel = l2_accel;
1499 		size = offsetof(struct fm10k_l2_accel,
1500 				macvlan[(l2_accel->size * 2) + 1]);
1501 		l2_accel = kzalloc(size, GFP_KERNEL);
1502 		if (!l2_accel)
1503 			return ERR_PTR(-ENOMEM);
1504 
1505 		memcpy(l2_accel, old_l2_accel,
1506 		       offsetof(struct fm10k_l2_accel,
1507 				macvlan[old_l2_accel->size]));
1508 
1509 		l2_accel->size = (old_l2_accel->size * 2) + 1;
1510 
1511 		/* update pointers */
1512 		fm10k_assign_l2_accel(interface, l2_accel);
1513 		kfree_rcu(old_l2_accel, rcu);
1514 	}
1515 
1516 	/* add macvlan to accel table, and record GLORT for position */
1517 	for (i = 0; i < l2_accel->size; i++) {
1518 		if (!l2_accel->macvlan[i])
1519 			break;
1520 	}
1521 
1522 	/* record station */
1523 	l2_accel->macvlan[i] = sdev;
1524 	l2_accel->count++;
1525 
1526 	/* configure default DGLORT mapping for RSS/DCB */
1527 	dglort.idx = fm10k_dglort_pf_rss;
1528 	dglort.inner_rss = 1;
1529 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1530 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1531 	dglort.glort = interface->glort;
1532 	dglort.shared_l = fls(l2_accel->size);
1533 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1534 
1535 	/* Add rules for this specific dglort to the switch */
1536 	fm10k_mbx_lock(interface);
1537 
1538 	glort = l2_accel->dglort + 1 + i;
1539 
1540 	if (fm10k_host_mbx_ready(interface))
1541 		hw->mac.ops.update_xcast_mode(hw, glort,
1542 					      FM10K_XCAST_MODE_NONE);
1543 
1544 	fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1545 				hw->mac.default_vid, true);
1546 
1547 	for (vid = fm10k_find_next_vlan(interface, 0);
1548 	     vid < VLAN_N_VID;
1549 	     vid = fm10k_find_next_vlan(interface, vid))
1550 		fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1551 					vid, true);
1552 
1553 	fm10k_mbx_unlock(interface);
1554 
1555 	return sdev;
1556 }
1557 
1558 static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
1559 {
1560 	struct fm10k_intfc *interface = netdev_priv(dev);
1561 	struct fm10k_l2_accel *l2_accel = READ_ONCE(interface->l2_accel);
1562 	struct fm10k_dglort_cfg dglort = { 0 };
1563 	struct fm10k_hw *hw = &interface->hw;
1564 	struct net_device *sdev = priv;
1565 	u16 vid, glort;
1566 	int i;
1567 
1568 	if (!l2_accel)
1569 		return;
1570 
1571 	/* search table for matching interface */
1572 	for (i = 0; i < l2_accel->size; i++) {
1573 		if (l2_accel->macvlan[i] == sdev)
1574 			break;
1575 	}
1576 
1577 	/* exit if macvlan not found */
1578 	if (i == l2_accel->size)
1579 		return;
1580 
1581 	/* Remove any rules specific to this dglort */
1582 	fm10k_mbx_lock(interface);
1583 
1584 	glort = l2_accel->dglort + 1 + i;
1585 
1586 	if (fm10k_host_mbx_ready(interface))
1587 		hw->mac.ops.update_xcast_mode(hw, glort,
1588 					      FM10K_XCAST_MODE_NONE);
1589 
1590 	fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1591 				hw->mac.default_vid, false);
1592 
1593 	for (vid = fm10k_find_next_vlan(interface, 0);
1594 	     vid < VLAN_N_VID;
1595 	     vid = fm10k_find_next_vlan(interface, vid))
1596 		fm10k_queue_mac_request(interface, glort, sdev->dev_addr,
1597 					vid, false);
1598 
1599 	fm10k_mbx_unlock(interface);
1600 
1601 	/* record removal */
1602 	l2_accel->macvlan[i] = NULL;
1603 	l2_accel->count--;
1604 
1605 	/* configure default DGLORT mapping for RSS/DCB */
1606 	dglort.idx = fm10k_dglort_pf_rss;
1607 	dglort.inner_rss = 1;
1608 	dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1609 	dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1610 	dglort.glort = interface->glort;
1611 	dglort.shared_l = fls(l2_accel->size);
1612 	hw->mac.ops.configure_dglort_map(hw, &dglort);
1613 
1614 	/* If table is empty remove it */
1615 	if (l2_accel->count == 0) {
1616 		fm10k_assign_l2_accel(interface, NULL);
1617 		kfree_rcu(l2_accel, rcu);
1618 	}
1619 }
1620 
1621 static netdev_features_t fm10k_features_check(struct sk_buff *skb,
1622 					      struct net_device *dev,
1623 					      netdev_features_t features)
1624 {
1625 	if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
1626 		return features;
1627 
1628 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1629 }
1630 
1631 static const struct net_device_ops fm10k_netdev_ops = {
1632 	.ndo_open		= fm10k_open,
1633 	.ndo_stop		= fm10k_close,
1634 	.ndo_validate_addr	= eth_validate_addr,
1635 	.ndo_start_xmit		= fm10k_xmit_frame,
1636 	.ndo_set_mac_address	= fm10k_set_mac,
1637 	.ndo_tx_timeout		= fm10k_tx_timeout,
1638 	.ndo_vlan_rx_add_vid	= fm10k_vlan_rx_add_vid,
1639 	.ndo_vlan_rx_kill_vid	= fm10k_vlan_rx_kill_vid,
1640 	.ndo_set_rx_mode	= fm10k_set_rx_mode,
1641 	.ndo_get_stats64	= fm10k_get_stats64,
1642 	.ndo_setup_tc		= __fm10k_setup_tc,
1643 	.ndo_set_vf_mac		= fm10k_ndo_set_vf_mac,
1644 	.ndo_set_vf_vlan	= fm10k_ndo_set_vf_vlan,
1645 	.ndo_set_vf_rate	= fm10k_ndo_set_vf_bw,
1646 	.ndo_get_vf_config	= fm10k_ndo_get_vf_config,
1647 	.ndo_udp_tunnel_add	= fm10k_udp_tunnel_add,
1648 	.ndo_udp_tunnel_del	= fm10k_udp_tunnel_del,
1649 	.ndo_dfwd_add_station	= fm10k_dfwd_add_station,
1650 	.ndo_dfwd_del_station	= fm10k_dfwd_del_station,
1651 	.ndo_features_check	= fm10k_features_check,
1652 };
1653 
1654 #define DEFAULT_DEBUG_LEVEL_SHIFT 3
1655 
1656 struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
1657 {
1658 	netdev_features_t hw_features;
1659 	struct fm10k_intfc *interface;
1660 	struct net_device *dev;
1661 
1662 	dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
1663 	if (!dev)
1664 		return NULL;
1665 
1666 	/* set net device and ethtool ops */
1667 	dev->netdev_ops = &fm10k_netdev_ops;
1668 	fm10k_set_ethtool_ops(dev);
1669 
1670 	/* configure default debug level */
1671 	interface = netdev_priv(dev);
1672 	interface->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
1673 
1674 	/* configure default features */
1675 	dev->features |= NETIF_F_IP_CSUM |
1676 			 NETIF_F_IPV6_CSUM |
1677 			 NETIF_F_SG |
1678 			 NETIF_F_TSO |
1679 			 NETIF_F_TSO6 |
1680 			 NETIF_F_TSO_ECN |
1681 			 NETIF_F_RXHASH |
1682 			 NETIF_F_RXCSUM;
1683 
1684 	/* Only the PF can support VXLAN and NVGRE tunnel offloads */
1685 	if (info->mac == fm10k_mac_pf) {
1686 		dev->hw_enc_features = NETIF_F_IP_CSUM |
1687 				       NETIF_F_TSO |
1688 				       NETIF_F_TSO6 |
1689 				       NETIF_F_TSO_ECN |
1690 				       NETIF_F_GSO_UDP_TUNNEL |
1691 				       NETIF_F_IPV6_CSUM |
1692 				       NETIF_F_SG;
1693 
1694 		dev->features |= NETIF_F_GSO_UDP_TUNNEL;
1695 	}
1696 
1697 	/* all features defined to this point should be changeable */
1698 	hw_features = dev->features;
1699 
1700 	/* allow user to enable L2 forwarding acceleration */
1701 	hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
1702 
1703 	/* configure VLAN features */
1704 	dev->vlan_features |= dev->features;
1705 
1706 	/* we want to leave these both on as we cannot disable VLAN tag
1707 	 * insertion or stripping on the hardware since it is contained
1708 	 * in the FTAG and not in the frame itself.
1709 	 */
1710 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1711 			 NETIF_F_HW_VLAN_CTAG_RX |
1712 			 NETIF_F_HW_VLAN_CTAG_FILTER;
1713 
1714 	dev->priv_flags |= IFF_UNICAST_FLT;
1715 
1716 	dev->hw_features |= hw_features;
1717 
1718 	/* MTU range: 68 - 15342 */
1719 	dev->min_mtu = ETH_MIN_MTU;
1720 	dev->max_mtu = FM10K_MAX_JUMBO_FRAME_SIZE;
1721 
1722 	return dev;
1723 }
1724