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