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