Linux Audio

Check our new training course

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