162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/****************************************************************************
362306a36Sopenharmony_ci * Driver for Solarflare network controllers and boards
462306a36Sopenharmony_ci * Copyright 2018 Solarflare Communications Inc.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
762306a36Sopenharmony_ci * under the terms of the GNU General Public License version 2 as published
862306a36Sopenharmony_ci * by the Free Software Foundation, incorporated herein by reference.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "net_driver.h"
1262306a36Sopenharmony_ci#include <linux/module.h>
1362306a36Sopenharmony_ci#include <linux/iommu.h>
1462306a36Sopenharmony_ci#include "efx.h"
1562306a36Sopenharmony_ci#include "nic.h"
1662306a36Sopenharmony_ci#include "rx_common.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/* This is the percentage fill level below which new RX descriptors
1962306a36Sopenharmony_ci * will be added to the RX descriptor ring.
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_cistatic unsigned int rx_refill_threshold;
2262306a36Sopenharmony_cimodule_param(rx_refill_threshold, uint, 0444);
2362306a36Sopenharmony_ciMODULE_PARM_DESC(rx_refill_threshold,
2462306a36Sopenharmony_ci		 "RX descriptor ring refill threshold (%)");
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* RX maximum head room required.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * This must be at least 1 to prevent overflow, plus one packet-worth
2962306a36Sopenharmony_ci * to allow pipelined receives.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic void efx_unmap_rx_buffer(struct efx_nic *efx,
3462306a36Sopenharmony_ci				struct efx_rx_buffer *rx_buf);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* Check the RX page recycle ring for a page that can be reused. */
3762306a36Sopenharmony_cistatic struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
4062306a36Sopenharmony_ci	struct efx_rx_page_state *state;
4162306a36Sopenharmony_ci	unsigned int index;
4262306a36Sopenharmony_ci	struct page *page;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
4562306a36Sopenharmony_ci		return NULL;
4662306a36Sopenharmony_ci	index = rx_queue->page_remove & rx_queue->page_ptr_mask;
4762306a36Sopenharmony_ci	page = rx_queue->page_ring[index];
4862306a36Sopenharmony_ci	if (page == NULL)
4962306a36Sopenharmony_ci		return NULL;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	rx_queue->page_ring[index] = NULL;
5262306a36Sopenharmony_ci	/* page_remove cannot exceed page_add. */
5362306a36Sopenharmony_ci	if (rx_queue->page_remove != rx_queue->page_add)
5462306a36Sopenharmony_ci		++rx_queue->page_remove;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	/* If page_count is 1 then we hold the only reference to this page. */
5762306a36Sopenharmony_ci	if (page_count(page) == 1) {
5862306a36Sopenharmony_ci		++rx_queue->page_recycle_count;
5962306a36Sopenharmony_ci		return page;
6062306a36Sopenharmony_ci	} else {
6162306a36Sopenharmony_ci		state = page_address(page);
6262306a36Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
6362306a36Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
6462306a36Sopenharmony_ci			       DMA_FROM_DEVICE);
6562306a36Sopenharmony_ci		put_page(page);
6662306a36Sopenharmony_ci		++rx_queue->page_recycle_failed;
6762306a36Sopenharmony_ci	}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	return NULL;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* Attempt to recycle the page if there is an RX recycle ring; the page can
7362306a36Sopenharmony_ci * only be added if this is the final RX buffer, to prevent pages being used in
7462306a36Sopenharmony_ci * the descriptor ring and appearing in the recycle ring simultaneously.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cistatic void efx_recycle_rx_page(struct efx_channel *channel,
7762306a36Sopenharmony_ci				struct efx_rx_buffer *rx_buf)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
8062306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
8162306a36Sopenharmony_ci	struct page *page = rx_buf->page;
8262306a36Sopenharmony_ci	unsigned int index;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	/* Only recycle the page after processing the final buffer. */
8562306a36Sopenharmony_ci	if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
8662306a36Sopenharmony_ci		return;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	index = rx_queue->page_add & rx_queue->page_ptr_mask;
8962306a36Sopenharmony_ci	if (rx_queue->page_ring[index] == NULL) {
9062306a36Sopenharmony_ci		unsigned int read_index = rx_queue->page_remove &
9162306a36Sopenharmony_ci			rx_queue->page_ptr_mask;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci		/* The next slot in the recycle ring is available, but
9462306a36Sopenharmony_ci		 * increment page_remove if the read pointer currently
9562306a36Sopenharmony_ci		 * points here.
9662306a36Sopenharmony_ci		 */
9762306a36Sopenharmony_ci		if (read_index == index)
9862306a36Sopenharmony_ci			++rx_queue->page_remove;
9962306a36Sopenharmony_ci		rx_queue->page_ring[index] = page;
10062306a36Sopenharmony_ci		++rx_queue->page_add;
10162306a36Sopenharmony_ci		return;
10262306a36Sopenharmony_ci	}
10362306a36Sopenharmony_ci	++rx_queue->page_recycle_full;
10462306a36Sopenharmony_ci	efx_unmap_rx_buffer(efx, rx_buf);
10562306a36Sopenharmony_ci	put_page(rx_buf->page);
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/* Recycle the pages that are used by buffers that have just been received. */
10962306a36Sopenharmony_civoid efx_siena_recycle_rx_pages(struct efx_channel *channel,
11062306a36Sopenharmony_ci				struct efx_rx_buffer *rx_buf,
11162306a36Sopenharmony_ci				unsigned int n_frags)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
11662306a36Sopenharmony_ci		return;
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	do {
11962306a36Sopenharmony_ci		efx_recycle_rx_page(channel, rx_buf);
12062306a36Sopenharmony_ci		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
12162306a36Sopenharmony_ci	} while (--n_frags);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_civoid efx_siena_discard_rx_packet(struct efx_channel *channel,
12562306a36Sopenharmony_ci				 struct efx_rx_buffer *rx_buf,
12662306a36Sopenharmony_ci				 unsigned int n_frags)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	efx_siena_recycle_rx_pages(channel, rx_buf, n_frags);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	efx_siena_free_rx_buffers(rx_queue, rx_buf, n_frags);
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	unsigned int bufs_in_recycle_ring, page_ring_size;
13862306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	bufs_in_recycle_ring = efx_rx_recycle_ring_size(efx);
14162306a36Sopenharmony_ci	page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
14262306a36Sopenharmony_ci					    efx->rx_bufs_per_page);
14362306a36Sopenharmony_ci	rx_queue->page_ring = kcalloc(page_ring_size,
14462306a36Sopenharmony_ci				      sizeof(*rx_queue->page_ring), GFP_KERNEL);
14562306a36Sopenharmony_ci	if (!rx_queue->page_ring)
14662306a36Sopenharmony_ci		rx_queue->page_ptr_mask = 0;
14762306a36Sopenharmony_ci	else
14862306a36Sopenharmony_ci		rx_queue->page_ptr_mask = page_ring_size - 1;
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
15462306a36Sopenharmony_ci	int i;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
15762306a36Sopenharmony_ci		return;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	/* Unmap and release the pages in the recycle ring. Remove the ring. */
16062306a36Sopenharmony_ci	for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
16162306a36Sopenharmony_ci		struct page *page = rx_queue->page_ring[i];
16262306a36Sopenharmony_ci		struct efx_rx_page_state *state;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci		if (page == NULL)
16562306a36Sopenharmony_ci			continue;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci		state = page_address(page);
16862306a36Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
16962306a36Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
17062306a36Sopenharmony_ci			       DMA_FROM_DEVICE);
17162306a36Sopenharmony_ci		put_page(page);
17262306a36Sopenharmony_ci	}
17362306a36Sopenharmony_ci	kfree(rx_queue->page_ring);
17462306a36Sopenharmony_ci	rx_queue->page_ring = NULL;
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
17862306a36Sopenharmony_ci			       struct efx_rx_buffer *rx_buf)
17962306a36Sopenharmony_ci{
18062306a36Sopenharmony_ci	/* Release the page reference we hold for the buffer. */
18162306a36Sopenharmony_ci	if (rx_buf->page)
18262306a36Sopenharmony_ci		put_page(rx_buf->page);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	/* If this is the last buffer in a page, unmap and free it. */
18562306a36Sopenharmony_ci	if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
18662306a36Sopenharmony_ci		efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
18762306a36Sopenharmony_ci		efx_siena_free_rx_buffers(rx_queue, rx_buf, 1);
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci	rx_buf->page = NULL;
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciint efx_siena_probe_rx_queue(struct efx_rx_queue *rx_queue)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
19562306a36Sopenharmony_ci	unsigned int entries;
19662306a36Sopenharmony_ci	int rc;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	/* Create the smallest power-of-two aligned ring */
19962306a36Sopenharmony_ci	entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
20062306a36Sopenharmony_ci	EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
20162306a36Sopenharmony_ci	rx_queue->ptr_mask = entries - 1;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	netif_dbg(efx, probe, efx->net_dev,
20462306a36Sopenharmony_ci		  "creating RX queue %d size %#x mask %#x\n",
20562306a36Sopenharmony_ci		  efx_rx_queue_index(rx_queue), efx->rxq_entries,
20662306a36Sopenharmony_ci		  rx_queue->ptr_mask);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	/* Allocate RX buffers */
20962306a36Sopenharmony_ci	rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
21062306a36Sopenharmony_ci				   GFP_KERNEL);
21162306a36Sopenharmony_ci	if (!rx_queue->buffer)
21262306a36Sopenharmony_ci		return -ENOMEM;
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	rc = efx_nic_probe_rx(rx_queue);
21562306a36Sopenharmony_ci	if (rc) {
21662306a36Sopenharmony_ci		kfree(rx_queue->buffer);
21762306a36Sopenharmony_ci		rx_queue->buffer = NULL;
21862306a36Sopenharmony_ci	}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	return rc;
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_civoid efx_siena_init_rx_queue(struct efx_rx_queue *rx_queue)
22462306a36Sopenharmony_ci{
22562306a36Sopenharmony_ci	unsigned int max_fill, trigger, max_trigger;
22662306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
22762306a36Sopenharmony_ci	int rc = 0;
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
23062306a36Sopenharmony_ci		  "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	/* Initialise ptr fields */
23362306a36Sopenharmony_ci	rx_queue->added_count = 0;
23462306a36Sopenharmony_ci	rx_queue->notified_count = 0;
23562306a36Sopenharmony_ci	rx_queue->removed_count = 0;
23662306a36Sopenharmony_ci	rx_queue->min_fill = -1U;
23762306a36Sopenharmony_ci	efx_init_rx_recycle_ring(rx_queue);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	rx_queue->page_remove = 0;
24062306a36Sopenharmony_ci	rx_queue->page_add = rx_queue->page_ptr_mask + 1;
24162306a36Sopenharmony_ci	rx_queue->page_recycle_count = 0;
24262306a36Sopenharmony_ci	rx_queue->page_recycle_failed = 0;
24362306a36Sopenharmony_ci	rx_queue->page_recycle_full = 0;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* Initialise limit fields */
24662306a36Sopenharmony_ci	max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
24762306a36Sopenharmony_ci	max_trigger =
24862306a36Sopenharmony_ci		max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
24962306a36Sopenharmony_ci	if (rx_refill_threshold != 0) {
25062306a36Sopenharmony_ci		trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
25162306a36Sopenharmony_ci		if (trigger > max_trigger)
25262306a36Sopenharmony_ci			trigger = max_trigger;
25362306a36Sopenharmony_ci	} else {
25462306a36Sopenharmony_ci		trigger = max_trigger;
25562306a36Sopenharmony_ci	}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	rx_queue->max_fill = max_fill;
25862306a36Sopenharmony_ci	rx_queue->fast_fill_trigger = trigger;
25962306a36Sopenharmony_ci	rx_queue->refill_enabled = true;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	/* Initialise XDP queue information */
26262306a36Sopenharmony_ci	rc = xdp_rxq_info_reg(&rx_queue->xdp_rxq_info, efx->net_dev,
26362306a36Sopenharmony_ci			      rx_queue->core_index, 0);
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	if (rc) {
26662306a36Sopenharmony_ci		netif_err(efx, rx_err, efx->net_dev,
26762306a36Sopenharmony_ci			  "Failure to initialise XDP queue information rc=%d\n",
26862306a36Sopenharmony_ci			  rc);
26962306a36Sopenharmony_ci		efx->xdp_rxq_info_failed = true;
27062306a36Sopenharmony_ci	} else {
27162306a36Sopenharmony_ci		rx_queue->xdp_rxq_info_valid = true;
27262306a36Sopenharmony_ci	}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	/* Set up RX descriptor ring */
27562306a36Sopenharmony_ci	efx_nic_init_rx(rx_queue);
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_civoid efx_siena_fini_rx_queue(struct efx_rx_queue *rx_queue)
27962306a36Sopenharmony_ci{
28062306a36Sopenharmony_ci	struct efx_rx_buffer *rx_buf;
28162306a36Sopenharmony_ci	int i;
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
28462306a36Sopenharmony_ci		  "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci	del_timer_sync(&rx_queue->slow_fill);
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	/* Release RX buffers from the current read ptr to the write ptr */
28962306a36Sopenharmony_ci	if (rx_queue->buffer) {
29062306a36Sopenharmony_ci		for (i = rx_queue->removed_count; i < rx_queue->added_count;
29162306a36Sopenharmony_ci		     i++) {
29262306a36Sopenharmony_ci			unsigned int index = i & rx_queue->ptr_mask;
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci			rx_buf = efx_rx_buffer(rx_queue, index);
29562306a36Sopenharmony_ci			efx_fini_rx_buffer(rx_queue, rx_buf);
29662306a36Sopenharmony_ci		}
29762306a36Sopenharmony_ci	}
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	efx_fini_rx_recycle_ring(rx_queue);
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	if (rx_queue->xdp_rxq_info_valid)
30262306a36Sopenharmony_ci		xdp_rxq_info_unreg(&rx_queue->xdp_rxq_info);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	rx_queue->xdp_rxq_info_valid = false;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_civoid efx_siena_remove_rx_queue(struct efx_rx_queue *rx_queue)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
31062306a36Sopenharmony_ci		  "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	efx_nic_remove_rx(rx_queue);
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	kfree(rx_queue->buffer);
31562306a36Sopenharmony_ci	rx_queue->buffer = NULL;
31662306a36Sopenharmony_ci}
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci/* Unmap a DMA-mapped page.  This function is only called for the final RX
31962306a36Sopenharmony_ci * buffer in a page.
32062306a36Sopenharmony_ci */
32162306a36Sopenharmony_cistatic void efx_unmap_rx_buffer(struct efx_nic *efx,
32262306a36Sopenharmony_ci				struct efx_rx_buffer *rx_buf)
32362306a36Sopenharmony_ci{
32462306a36Sopenharmony_ci	struct page *page = rx_buf->page;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	if (page) {
32762306a36Sopenharmony_ci		struct efx_rx_page_state *state = page_address(page);
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev,
33062306a36Sopenharmony_ci			       state->dma_addr,
33162306a36Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
33262306a36Sopenharmony_ci			       DMA_FROM_DEVICE);
33362306a36Sopenharmony_ci	}
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_civoid efx_siena_free_rx_buffers(struct efx_rx_queue *rx_queue,
33762306a36Sopenharmony_ci			       struct efx_rx_buffer *rx_buf,
33862306a36Sopenharmony_ci			       unsigned int num_bufs)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	do {
34162306a36Sopenharmony_ci		if (rx_buf->page) {
34262306a36Sopenharmony_ci			put_page(rx_buf->page);
34362306a36Sopenharmony_ci			rx_buf->page = NULL;
34462306a36Sopenharmony_ci		}
34562306a36Sopenharmony_ci		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
34662306a36Sopenharmony_ci	} while (--num_bufs);
34762306a36Sopenharmony_ci}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_civoid efx_siena_rx_slow_fill(struct timer_list *t)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	/* Post an event to cause NAPI to run and refill the queue */
35462306a36Sopenharmony_ci	efx_nic_generate_fill_event(rx_queue);
35562306a36Sopenharmony_ci	++rx_queue->slow_fill_count;
35662306a36Sopenharmony_ci}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_cistatic void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
35962306a36Sopenharmony_ci{
36062306a36Sopenharmony_ci	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
36162306a36Sopenharmony_ci}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci/* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
36462306a36Sopenharmony_ci *
36562306a36Sopenharmony_ci * @rx_queue:		Efx RX queue
36662306a36Sopenharmony_ci *
36762306a36Sopenharmony_ci * This allocates a batch of pages, maps them for DMA, and populates
36862306a36Sopenharmony_ci * struct efx_rx_buffers for each one. Return a negative error code or
36962306a36Sopenharmony_ci * 0 on success. If a single page can be used for multiple buffers,
37062306a36Sopenharmony_ci * then the page will either be inserted fully, or not at all.
37162306a36Sopenharmony_ci */
37262306a36Sopenharmony_cistatic int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	unsigned int page_offset, index, count;
37562306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
37662306a36Sopenharmony_ci	struct efx_rx_page_state *state;
37762306a36Sopenharmony_ci	struct efx_rx_buffer *rx_buf;
37862306a36Sopenharmony_ci	dma_addr_t dma_addr;
37962306a36Sopenharmony_ci	struct page *page;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	count = 0;
38262306a36Sopenharmony_ci	do {
38362306a36Sopenharmony_ci		page = efx_reuse_page(rx_queue);
38462306a36Sopenharmony_ci		if (page == NULL) {
38562306a36Sopenharmony_ci			page = alloc_pages(__GFP_COMP |
38662306a36Sopenharmony_ci					   (atomic ? GFP_ATOMIC : GFP_KERNEL),
38762306a36Sopenharmony_ci					   efx->rx_buffer_order);
38862306a36Sopenharmony_ci			if (unlikely(page == NULL))
38962306a36Sopenharmony_ci				return -ENOMEM;
39062306a36Sopenharmony_ci			dma_addr =
39162306a36Sopenharmony_ci				dma_map_page(&efx->pci_dev->dev, page, 0,
39262306a36Sopenharmony_ci					     PAGE_SIZE << efx->rx_buffer_order,
39362306a36Sopenharmony_ci					     DMA_FROM_DEVICE);
39462306a36Sopenharmony_ci			if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
39562306a36Sopenharmony_ci						       dma_addr))) {
39662306a36Sopenharmony_ci				__free_pages(page, efx->rx_buffer_order);
39762306a36Sopenharmony_ci				return -EIO;
39862306a36Sopenharmony_ci			}
39962306a36Sopenharmony_ci			state = page_address(page);
40062306a36Sopenharmony_ci			state->dma_addr = dma_addr;
40162306a36Sopenharmony_ci		} else {
40262306a36Sopenharmony_ci			state = page_address(page);
40362306a36Sopenharmony_ci			dma_addr = state->dma_addr;
40462306a36Sopenharmony_ci		}
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci		dma_addr += sizeof(struct efx_rx_page_state);
40762306a36Sopenharmony_ci		page_offset = sizeof(struct efx_rx_page_state);
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci		do {
41062306a36Sopenharmony_ci			index = rx_queue->added_count & rx_queue->ptr_mask;
41162306a36Sopenharmony_ci			rx_buf = efx_rx_buffer(rx_queue, index);
41262306a36Sopenharmony_ci			rx_buf->dma_addr = dma_addr + efx->rx_ip_align +
41362306a36Sopenharmony_ci					   EFX_XDP_HEADROOM;
41462306a36Sopenharmony_ci			rx_buf->page = page;
41562306a36Sopenharmony_ci			rx_buf->page_offset = page_offset + efx->rx_ip_align +
41662306a36Sopenharmony_ci					      EFX_XDP_HEADROOM;
41762306a36Sopenharmony_ci			rx_buf->len = efx->rx_dma_len;
41862306a36Sopenharmony_ci			rx_buf->flags = 0;
41962306a36Sopenharmony_ci			++rx_queue->added_count;
42062306a36Sopenharmony_ci			get_page(page);
42162306a36Sopenharmony_ci			dma_addr += efx->rx_page_buf_step;
42262306a36Sopenharmony_ci			page_offset += efx->rx_page_buf_step;
42362306a36Sopenharmony_ci		} while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci		rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
42662306a36Sopenharmony_ci	} while (++count < efx->rx_pages_per_batch);
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	return 0;
42962306a36Sopenharmony_ci}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_civoid efx_siena_rx_config_page_split(struct efx_nic *efx)
43262306a36Sopenharmony_ci{
43362306a36Sopenharmony_ci	efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align +
43462306a36Sopenharmony_ci				      EFX_XDP_HEADROOM + EFX_XDP_TAILROOM,
43562306a36Sopenharmony_ci				      EFX_RX_BUF_ALIGNMENT);
43662306a36Sopenharmony_ci	efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
43762306a36Sopenharmony_ci		((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
43862306a36Sopenharmony_ci		efx->rx_page_buf_step);
43962306a36Sopenharmony_ci	efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
44062306a36Sopenharmony_ci		efx->rx_bufs_per_page;
44162306a36Sopenharmony_ci	efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
44262306a36Sopenharmony_ci					       efx->rx_bufs_per_page);
44362306a36Sopenharmony_ci}
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci/* efx_siena_fast_push_rx_descriptors - push new RX descriptors quickly
44662306a36Sopenharmony_ci * @rx_queue:		RX descriptor queue
44762306a36Sopenharmony_ci *
44862306a36Sopenharmony_ci * This will aim to fill the RX descriptor queue up to
44962306a36Sopenharmony_ci * @rx_queue->@max_fill. If there is insufficient atomic
45062306a36Sopenharmony_ci * memory to do so, a slow fill will be scheduled.
45162306a36Sopenharmony_ci *
45262306a36Sopenharmony_ci * The caller must provide serialisation (none is used here). In practise,
45362306a36Sopenharmony_ci * this means this function must run from the NAPI handler, or be called
45462306a36Sopenharmony_ci * when NAPI is disabled.
45562306a36Sopenharmony_ci */
45662306a36Sopenharmony_civoid efx_siena_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue,
45762306a36Sopenharmony_ci					bool atomic)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
46062306a36Sopenharmony_ci	unsigned int fill_level, batch_size;
46162306a36Sopenharmony_ci	int space, rc = 0;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	if (!rx_queue->refill_enabled)
46462306a36Sopenharmony_ci		return;
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	/* Calculate current fill level, and exit if we don't need to fill */
46762306a36Sopenharmony_ci	fill_level = (rx_queue->added_count - rx_queue->removed_count);
46862306a36Sopenharmony_ci	EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
46962306a36Sopenharmony_ci	if (fill_level >= rx_queue->fast_fill_trigger)
47062306a36Sopenharmony_ci		goto out;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci	/* Record minimum fill level */
47362306a36Sopenharmony_ci	if (unlikely(fill_level < rx_queue->min_fill)) {
47462306a36Sopenharmony_ci		if (fill_level)
47562306a36Sopenharmony_ci			rx_queue->min_fill = fill_level;
47662306a36Sopenharmony_ci	}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
47962306a36Sopenharmony_ci	space = rx_queue->max_fill - fill_level;
48062306a36Sopenharmony_ci	EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
48362306a36Sopenharmony_ci		   "RX queue %d fast-filling descriptor ring from"
48462306a36Sopenharmony_ci		   " level %d to level %d\n",
48562306a36Sopenharmony_ci		   efx_rx_queue_index(rx_queue), fill_level,
48662306a36Sopenharmony_ci		   rx_queue->max_fill);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	do {
48962306a36Sopenharmony_ci		rc = efx_init_rx_buffers(rx_queue, atomic);
49062306a36Sopenharmony_ci		if (unlikely(rc)) {
49162306a36Sopenharmony_ci			/* Ensure that we don't leave the rx queue empty */
49262306a36Sopenharmony_ci			efx_schedule_slow_fill(rx_queue);
49362306a36Sopenharmony_ci			goto out;
49462306a36Sopenharmony_ci		}
49562306a36Sopenharmony_ci	} while ((space -= batch_size) >= batch_size);
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
49862306a36Sopenharmony_ci		   "RX queue %d fast-filled descriptor ring "
49962306a36Sopenharmony_ci		   "to level %d\n", efx_rx_queue_index(rx_queue),
50062306a36Sopenharmony_ci		   rx_queue->added_count - rx_queue->removed_count);
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci out:
50362306a36Sopenharmony_ci	if (rx_queue->notified_count != rx_queue->added_count)
50462306a36Sopenharmony_ci		efx_nic_notify_rx_desc(rx_queue);
50562306a36Sopenharmony_ci}
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci/* Pass a received packet up through GRO.  GRO can handle pages
50862306a36Sopenharmony_ci * regardless of checksum state and skbs with a good checksum.
50962306a36Sopenharmony_ci */
51062306a36Sopenharmony_civoid
51162306a36Sopenharmony_ciefx_siena_rx_packet_gro(struct efx_channel *channel,
51262306a36Sopenharmony_ci			struct efx_rx_buffer *rx_buf,
51362306a36Sopenharmony_ci			unsigned int n_frags, u8 *eh, __wsum csum)
51462306a36Sopenharmony_ci{
51562306a36Sopenharmony_ci	struct napi_struct *napi = &channel->napi_str;
51662306a36Sopenharmony_ci	struct efx_nic *efx = channel->efx;
51762306a36Sopenharmony_ci	struct sk_buff *skb;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	skb = napi_get_frags(napi);
52062306a36Sopenharmony_ci	if (unlikely(!skb)) {
52162306a36Sopenharmony_ci		struct efx_rx_queue *rx_queue;
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci		rx_queue = efx_channel_get_rx_queue(channel);
52462306a36Sopenharmony_ci		efx_siena_free_rx_buffers(rx_queue, rx_buf, n_frags);
52562306a36Sopenharmony_ci		return;
52662306a36Sopenharmony_ci	}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci	if (efx->net_dev->features & NETIF_F_RXHASH)
52962306a36Sopenharmony_ci		skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
53062306a36Sopenharmony_ci			     PKT_HASH_TYPE_L3);
53162306a36Sopenharmony_ci	if (csum) {
53262306a36Sopenharmony_ci		skb->csum = csum;
53362306a36Sopenharmony_ci		skb->ip_summed = CHECKSUM_COMPLETE;
53462306a36Sopenharmony_ci	} else {
53562306a36Sopenharmony_ci		skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
53662306a36Sopenharmony_ci				  CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
53762306a36Sopenharmony_ci	}
53862306a36Sopenharmony_ci	skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci	for (;;) {
54162306a36Sopenharmony_ci		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
54262306a36Sopenharmony_ci				   rx_buf->page, rx_buf->page_offset,
54362306a36Sopenharmony_ci				   rx_buf->len);
54462306a36Sopenharmony_ci		rx_buf->page = NULL;
54562306a36Sopenharmony_ci		skb->len += rx_buf->len;
54662306a36Sopenharmony_ci		if (skb_shinfo(skb)->nr_frags == n_frags)
54762306a36Sopenharmony_ci			break;
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci		rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
55062306a36Sopenharmony_ci	}
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci	skb->data_len = skb->len;
55362306a36Sopenharmony_ci	skb->truesize += n_frags * efx->rx_buffer_truesize;
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci	skb_record_rx_queue(skb, channel->rx_queue.core_index);
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci	napi_gro_frags(napi);
55862306a36Sopenharmony_ci}
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci/* RSS contexts.  We're using linked lists and crappy O(n) algorithms, because
56162306a36Sopenharmony_ci * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
56262306a36Sopenharmony_ci */
56362306a36Sopenharmony_cistruct efx_rss_context *efx_siena_alloc_rss_context_entry(struct efx_nic *efx)
56462306a36Sopenharmony_ci{
56562306a36Sopenharmony_ci	struct list_head *head = &efx->rss_context.list;
56662306a36Sopenharmony_ci	struct efx_rss_context *ctx, *new;
56762306a36Sopenharmony_ci	u32 id = 1; /* Don't use zero, that refers to the master RSS context */
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	WARN_ON(!mutex_is_locked(&efx->rss_lock));
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	/* Search for first gap in the numbering */
57262306a36Sopenharmony_ci	list_for_each_entry(ctx, head, list) {
57362306a36Sopenharmony_ci		if (ctx->user_id != id)
57462306a36Sopenharmony_ci			break;
57562306a36Sopenharmony_ci		id++;
57662306a36Sopenharmony_ci		/* Check for wrap.  If this happens, we have nearly 2^32
57762306a36Sopenharmony_ci		 * allocated RSS contexts, which seems unlikely.
57862306a36Sopenharmony_ci		 */
57962306a36Sopenharmony_ci		if (WARN_ON_ONCE(!id))
58062306a36Sopenharmony_ci			return NULL;
58162306a36Sopenharmony_ci	}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	/* Create the new entry */
58462306a36Sopenharmony_ci	new = kmalloc(sizeof(*new), GFP_KERNEL);
58562306a36Sopenharmony_ci	if (!new)
58662306a36Sopenharmony_ci		return NULL;
58762306a36Sopenharmony_ci	new->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
58862306a36Sopenharmony_ci	new->rx_hash_udp_4tuple = false;
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci	/* Insert the new entry into the gap */
59162306a36Sopenharmony_ci	new->user_id = id;
59262306a36Sopenharmony_ci	list_add_tail(&new->list, &ctx->list);
59362306a36Sopenharmony_ci	return new;
59462306a36Sopenharmony_ci}
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_cistruct efx_rss_context *efx_siena_find_rss_context_entry(struct efx_nic *efx,
59762306a36Sopenharmony_ci							 u32 id)
59862306a36Sopenharmony_ci{
59962306a36Sopenharmony_ci	struct list_head *head = &efx->rss_context.list;
60062306a36Sopenharmony_ci	struct efx_rss_context *ctx;
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	WARN_ON(!mutex_is_locked(&efx->rss_lock));
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci	list_for_each_entry(ctx, head, list)
60562306a36Sopenharmony_ci		if (ctx->user_id == id)
60662306a36Sopenharmony_ci			return ctx;
60762306a36Sopenharmony_ci	return NULL;
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_civoid efx_siena_free_rss_context_entry(struct efx_rss_context *ctx)
61162306a36Sopenharmony_ci{
61262306a36Sopenharmony_ci	list_del(&ctx->list);
61362306a36Sopenharmony_ci	kfree(ctx);
61462306a36Sopenharmony_ci}
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_civoid efx_siena_set_default_rx_indir_table(struct efx_nic *efx,
61762306a36Sopenharmony_ci					  struct efx_rss_context *ctx)
61862306a36Sopenharmony_ci{
61962306a36Sopenharmony_ci	size_t i;
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
62262306a36Sopenharmony_ci		ctx->rx_indir_table[i] =
62362306a36Sopenharmony_ci			ethtool_rxfh_indir_default(i, efx->rss_spread);
62462306a36Sopenharmony_ci}
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci/**
62762306a36Sopenharmony_ci * efx_siena_filter_is_mc_recipient - test whether spec is a multicast recipient
62862306a36Sopenharmony_ci * @spec: Specification to test
62962306a36Sopenharmony_ci *
63062306a36Sopenharmony_ci * Return: %true if the specification is a non-drop RX filter that
63162306a36Sopenharmony_ci * matches a local MAC address I/G bit value of 1 or matches a local
63262306a36Sopenharmony_ci * IPv4 or IPv6 address value in the respective multicast address
63362306a36Sopenharmony_ci * range.  Otherwise %false.
63462306a36Sopenharmony_ci */
63562306a36Sopenharmony_cibool efx_siena_filter_is_mc_recipient(const struct efx_filter_spec *spec)
63662306a36Sopenharmony_ci{
63762306a36Sopenharmony_ci	if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
63862306a36Sopenharmony_ci	    spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
63962306a36Sopenharmony_ci		return false;
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	if (spec->match_flags &
64262306a36Sopenharmony_ci	    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
64362306a36Sopenharmony_ci	    is_multicast_ether_addr(spec->loc_mac))
64462306a36Sopenharmony_ci		return true;
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	if ((spec->match_flags &
64762306a36Sopenharmony_ci	     (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
64862306a36Sopenharmony_ci	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
64962306a36Sopenharmony_ci		if (spec->ether_type == htons(ETH_P_IP) &&
65062306a36Sopenharmony_ci		    ipv4_is_multicast(spec->loc_host[0]))
65162306a36Sopenharmony_ci			return true;
65262306a36Sopenharmony_ci		if (spec->ether_type == htons(ETH_P_IPV6) &&
65362306a36Sopenharmony_ci		    ((const u8 *)spec->loc_host)[0] == 0xff)
65462306a36Sopenharmony_ci			return true;
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	return false;
65862306a36Sopenharmony_ci}
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_cibool efx_siena_filter_spec_equal(const struct efx_filter_spec *left,
66162306a36Sopenharmony_ci				 const struct efx_filter_spec *right)
66262306a36Sopenharmony_ci{
66362306a36Sopenharmony_ci	if ((left->match_flags ^ right->match_flags) |
66462306a36Sopenharmony_ci	    ((left->flags ^ right->flags) &
66562306a36Sopenharmony_ci	     (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
66662306a36Sopenharmony_ci		return false;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	return memcmp(&left->outer_vid, &right->outer_vid,
66962306a36Sopenharmony_ci		      sizeof(struct efx_filter_spec) -
67062306a36Sopenharmony_ci		      offsetof(struct efx_filter_spec, outer_vid)) == 0;
67162306a36Sopenharmony_ci}
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ciu32 efx_siena_filter_spec_hash(const struct efx_filter_spec *spec)
67462306a36Sopenharmony_ci{
67562306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
67662306a36Sopenharmony_ci	return jhash2((const u32 *)&spec->outer_vid,
67762306a36Sopenharmony_ci		      (sizeof(struct efx_filter_spec) -
67862306a36Sopenharmony_ci		       offsetof(struct efx_filter_spec, outer_vid)) / 4,
67962306a36Sopenharmony_ci		      0);
68062306a36Sopenharmony_ci}
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
68362306a36Sopenharmony_cibool efx_siena_rps_check_rule(struct efx_arfs_rule *rule,
68462306a36Sopenharmony_ci			      unsigned int filter_idx, bool *force)
68562306a36Sopenharmony_ci{
68662306a36Sopenharmony_ci	if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
68762306a36Sopenharmony_ci		/* ARFS is currently updating this entry, leave it */
68862306a36Sopenharmony_ci		return false;
68962306a36Sopenharmony_ci	}
69062306a36Sopenharmony_ci	if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
69162306a36Sopenharmony_ci		/* ARFS tried and failed to update this, so it's probably out
69262306a36Sopenharmony_ci		 * of date.  Remove the filter and the ARFS rule entry.
69362306a36Sopenharmony_ci		 */
69462306a36Sopenharmony_ci		rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
69562306a36Sopenharmony_ci		*force = true;
69662306a36Sopenharmony_ci		return true;
69762306a36Sopenharmony_ci	} else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
69862306a36Sopenharmony_ci		/* ARFS has moved on, so old filter is not needed.  Since we did
69962306a36Sopenharmony_ci		 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
70062306a36Sopenharmony_ci		 * not be removed by efx_siena_rps_hash_del() subsequently.
70162306a36Sopenharmony_ci		 */
70262306a36Sopenharmony_ci		*force = true;
70362306a36Sopenharmony_ci		return true;
70462306a36Sopenharmony_ci	}
70562306a36Sopenharmony_ci	/* Remove it iff ARFS wants to. */
70662306a36Sopenharmony_ci	return true;
70762306a36Sopenharmony_ci}
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_cistatic
71062306a36Sopenharmony_cistruct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
71162306a36Sopenharmony_ci				       const struct efx_filter_spec *spec)
71262306a36Sopenharmony_ci{
71362306a36Sopenharmony_ci	u32 hash = efx_siena_filter_spec_hash(spec);
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci	lockdep_assert_held(&efx->rps_hash_lock);
71662306a36Sopenharmony_ci	if (!efx->rps_hash_table)
71762306a36Sopenharmony_ci		return NULL;
71862306a36Sopenharmony_ci	return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
71962306a36Sopenharmony_ci}
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_cistruct efx_arfs_rule *efx_siena_rps_hash_find(struct efx_nic *efx,
72262306a36Sopenharmony_ci					const struct efx_filter_spec *spec)
72362306a36Sopenharmony_ci{
72462306a36Sopenharmony_ci	struct efx_arfs_rule *rule;
72562306a36Sopenharmony_ci	struct hlist_head *head;
72662306a36Sopenharmony_ci	struct hlist_node *node;
72762306a36Sopenharmony_ci
72862306a36Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
72962306a36Sopenharmony_ci	if (!head)
73062306a36Sopenharmony_ci		return NULL;
73162306a36Sopenharmony_ci	hlist_for_each(node, head) {
73262306a36Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
73362306a36Sopenharmony_ci		if (efx_siena_filter_spec_equal(spec, &rule->spec))
73462306a36Sopenharmony_ci			return rule;
73562306a36Sopenharmony_ci	}
73662306a36Sopenharmony_ci	return NULL;
73762306a36Sopenharmony_ci}
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_cistatic struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
74062306a36Sopenharmony_ci					const struct efx_filter_spec *spec,
74162306a36Sopenharmony_ci					bool *new)
74262306a36Sopenharmony_ci{
74362306a36Sopenharmony_ci	struct efx_arfs_rule *rule;
74462306a36Sopenharmony_ci	struct hlist_head *head;
74562306a36Sopenharmony_ci	struct hlist_node *node;
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
74862306a36Sopenharmony_ci	if (!head)
74962306a36Sopenharmony_ci		return NULL;
75062306a36Sopenharmony_ci	hlist_for_each(node, head) {
75162306a36Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
75262306a36Sopenharmony_ci		if (efx_siena_filter_spec_equal(spec, &rule->spec)) {
75362306a36Sopenharmony_ci			*new = false;
75462306a36Sopenharmony_ci			return rule;
75562306a36Sopenharmony_ci		}
75662306a36Sopenharmony_ci	}
75762306a36Sopenharmony_ci	rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
75862306a36Sopenharmony_ci	*new = true;
75962306a36Sopenharmony_ci	if (rule) {
76062306a36Sopenharmony_ci		memcpy(&rule->spec, spec, sizeof(rule->spec));
76162306a36Sopenharmony_ci		hlist_add_head(&rule->node, head);
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci	return rule;
76462306a36Sopenharmony_ci}
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_civoid efx_siena_rps_hash_del(struct efx_nic *efx,
76762306a36Sopenharmony_ci			    const struct efx_filter_spec *spec)
76862306a36Sopenharmony_ci{
76962306a36Sopenharmony_ci	struct efx_arfs_rule *rule;
77062306a36Sopenharmony_ci	struct hlist_head *head;
77162306a36Sopenharmony_ci	struct hlist_node *node;
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
77462306a36Sopenharmony_ci	if (WARN_ON(!head))
77562306a36Sopenharmony_ci		return;
77662306a36Sopenharmony_ci	hlist_for_each(node, head) {
77762306a36Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
77862306a36Sopenharmony_ci		if (efx_siena_filter_spec_equal(spec, &rule->spec)) {
77962306a36Sopenharmony_ci			/* Someone already reused the entry.  We know that if
78062306a36Sopenharmony_ci			 * this check doesn't fire (i.e. filter_id == REMOVING)
78162306a36Sopenharmony_ci			 * then the REMOVING mark was put there by our caller,
78262306a36Sopenharmony_ci			 * because caller is holding a lock on filter table and
78362306a36Sopenharmony_ci			 * only holders of that lock set REMOVING.
78462306a36Sopenharmony_ci			 */
78562306a36Sopenharmony_ci			if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
78662306a36Sopenharmony_ci				return;
78762306a36Sopenharmony_ci			hlist_del(node);
78862306a36Sopenharmony_ci			kfree(rule);
78962306a36Sopenharmony_ci			return;
79062306a36Sopenharmony_ci		}
79162306a36Sopenharmony_ci	}
79262306a36Sopenharmony_ci	/* We didn't find it. */
79362306a36Sopenharmony_ci	WARN_ON(1);
79462306a36Sopenharmony_ci}
79562306a36Sopenharmony_ci#endif
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ciint efx_siena_probe_filters(struct efx_nic *efx)
79862306a36Sopenharmony_ci{
79962306a36Sopenharmony_ci	int rc;
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	mutex_lock(&efx->mac_lock);
80262306a36Sopenharmony_ci	down_write(&efx->filter_sem);
80362306a36Sopenharmony_ci	rc = efx->type->filter_table_probe(efx);
80462306a36Sopenharmony_ci	if (rc)
80562306a36Sopenharmony_ci		goto out_unlock;
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
80862306a36Sopenharmony_ci	if (efx->type->offload_features & NETIF_F_NTUPLE) {
80962306a36Sopenharmony_ci		struct efx_channel *channel;
81062306a36Sopenharmony_ci		int i, success = 1;
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci		efx_for_each_channel(channel, efx) {
81362306a36Sopenharmony_ci			channel->rps_flow_id =
81462306a36Sopenharmony_ci				kcalloc(efx->type->max_rx_ip_filters,
81562306a36Sopenharmony_ci					sizeof(*channel->rps_flow_id),
81662306a36Sopenharmony_ci					GFP_KERNEL);
81762306a36Sopenharmony_ci			if (!channel->rps_flow_id)
81862306a36Sopenharmony_ci				success = 0;
81962306a36Sopenharmony_ci			else
82062306a36Sopenharmony_ci				for (i = 0;
82162306a36Sopenharmony_ci				     i < efx->type->max_rx_ip_filters;
82262306a36Sopenharmony_ci				     ++i)
82362306a36Sopenharmony_ci					channel->rps_flow_id[i] =
82462306a36Sopenharmony_ci						RPS_FLOW_ID_INVALID;
82562306a36Sopenharmony_ci			channel->rfs_expire_index = 0;
82662306a36Sopenharmony_ci			channel->rfs_filter_count = 0;
82762306a36Sopenharmony_ci		}
82862306a36Sopenharmony_ci
82962306a36Sopenharmony_ci		if (!success) {
83062306a36Sopenharmony_ci			efx_for_each_channel(channel, efx)
83162306a36Sopenharmony_ci				kfree(channel->rps_flow_id);
83262306a36Sopenharmony_ci			efx->type->filter_table_remove(efx);
83362306a36Sopenharmony_ci			rc = -ENOMEM;
83462306a36Sopenharmony_ci			goto out_unlock;
83562306a36Sopenharmony_ci		}
83662306a36Sopenharmony_ci	}
83762306a36Sopenharmony_ci#endif
83862306a36Sopenharmony_ciout_unlock:
83962306a36Sopenharmony_ci	up_write(&efx->filter_sem);
84062306a36Sopenharmony_ci	mutex_unlock(&efx->mac_lock);
84162306a36Sopenharmony_ci	return rc;
84262306a36Sopenharmony_ci}
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_civoid efx_siena_remove_filters(struct efx_nic *efx)
84562306a36Sopenharmony_ci{
84662306a36Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
84762306a36Sopenharmony_ci	struct efx_channel *channel;
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci	efx_for_each_channel(channel, efx) {
85062306a36Sopenharmony_ci		cancel_delayed_work_sync(&channel->filter_work);
85162306a36Sopenharmony_ci		kfree(channel->rps_flow_id);
85262306a36Sopenharmony_ci		channel->rps_flow_id = NULL;
85362306a36Sopenharmony_ci	}
85462306a36Sopenharmony_ci#endif
85562306a36Sopenharmony_ci	down_write(&efx->filter_sem);
85662306a36Sopenharmony_ci	efx->type->filter_table_remove(efx);
85762306a36Sopenharmony_ci	up_write(&efx->filter_sem);
85862306a36Sopenharmony_ci}
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_cistatic void efx_filter_rfs_work(struct work_struct *data)
86362306a36Sopenharmony_ci{
86462306a36Sopenharmony_ci	struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
86562306a36Sopenharmony_ci							      work);
86662306a36Sopenharmony_ci	struct efx_nic *efx = netdev_priv(req->net_dev);
86762306a36Sopenharmony_ci	struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
86862306a36Sopenharmony_ci	int slot_idx = req - efx->rps_slot;
86962306a36Sopenharmony_ci	struct efx_arfs_rule *rule;
87062306a36Sopenharmony_ci	u16 arfs_id = 0;
87162306a36Sopenharmony_ci	int rc;
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci	rc = efx->type->filter_insert(efx, &req->spec, true);
87462306a36Sopenharmony_ci	if (rc >= 0)
87562306a36Sopenharmony_ci		/* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
87662306a36Sopenharmony_ci		rc %= efx->type->max_rx_ip_filters;
87762306a36Sopenharmony_ci	if (efx->rps_hash_table) {
87862306a36Sopenharmony_ci		spin_lock_bh(&efx->rps_hash_lock);
87962306a36Sopenharmony_ci		rule = efx_siena_rps_hash_find(efx, &req->spec);
88062306a36Sopenharmony_ci		/* The rule might have already gone, if someone else's request
88162306a36Sopenharmony_ci		 * for the same spec was already worked and then expired before
88262306a36Sopenharmony_ci		 * we got around to our work.  In that case we have nothing
88362306a36Sopenharmony_ci		 * tying us to an arfs_id, meaning that as soon as the filter
88462306a36Sopenharmony_ci		 * is considered for expiry it will be removed.
88562306a36Sopenharmony_ci		 */
88662306a36Sopenharmony_ci		if (rule) {
88762306a36Sopenharmony_ci			if (rc < 0)
88862306a36Sopenharmony_ci				rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
88962306a36Sopenharmony_ci			else
89062306a36Sopenharmony_ci				rule->filter_id = rc;
89162306a36Sopenharmony_ci			arfs_id = rule->arfs_id;
89262306a36Sopenharmony_ci		}
89362306a36Sopenharmony_ci		spin_unlock_bh(&efx->rps_hash_lock);
89462306a36Sopenharmony_ci	}
89562306a36Sopenharmony_ci	if (rc >= 0) {
89662306a36Sopenharmony_ci		/* Remember this so we can check whether to expire the filter
89762306a36Sopenharmony_ci		 * later.
89862306a36Sopenharmony_ci		 */
89962306a36Sopenharmony_ci		mutex_lock(&efx->rps_mutex);
90062306a36Sopenharmony_ci		if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
90162306a36Sopenharmony_ci			channel->rfs_filter_count++;
90262306a36Sopenharmony_ci		channel->rps_flow_id[rc] = req->flow_id;
90362306a36Sopenharmony_ci		mutex_unlock(&efx->rps_mutex);
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci		if (req->spec.ether_type == htons(ETH_P_IP))
90662306a36Sopenharmony_ci			netif_info(efx, rx_status, efx->net_dev,
90762306a36Sopenharmony_ci				   "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
90862306a36Sopenharmony_ci				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
90962306a36Sopenharmony_ci				   req->spec.rem_host, ntohs(req->spec.rem_port),
91062306a36Sopenharmony_ci				   req->spec.loc_host, ntohs(req->spec.loc_port),
91162306a36Sopenharmony_ci				   req->rxq_index, req->flow_id, rc, arfs_id);
91262306a36Sopenharmony_ci		else
91362306a36Sopenharmony_ci			netif_info(efx, rx_status, efx->net_dev,
91462306a36Sopenharmony_ci				   "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
91562306a36Sopenharmony_ci				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
91662306a36Sopenharmony_ci				   req->spec.rem_host, ntohs(req->spec.rem_port),
91762306a36Sopenharmony_ci				   req->spec.loc_host, ntohs(req->spec.loc_port),
91862306a36Sopenharmony_ci				   req->rxq_index, req->flow_id, rc, arfs_id);
91962306a36Sopenharmony_ci		channel->n_rfs_succeeded++;
92062306a36Sopenharmony_ci	} else {
92162306a36Sopenharmony_ci		if (req->spec.ether_type == htons(ETH_P_IP))
92262306a36Sopenharmony_ci			netif_dbg(efx, rx_status, efx->net_dev,
92362306a36Sopenharmony_ci				  "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
92462306a36Sopenharmony_ci				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
92562306a36Sopenharmony_ci				  req->spec.rem_host, ntohs(req->spec.rem_port),
92662306a36Sopenharmony_ci				  req->spec.loc_host, ntohs(req->spec.loc_port),
92762306a36Sopenharmony_ci				  req->rxq_index, req->flow_id, rc, arfs_id);
92862306a36Sopenharmony_ci		else
92962306a36Sopenharmony_ci			netif_dbg(efx, rx_status, efx->net_dev,
93062306a36Sopenharmony_ci				  "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
93162306a36Sopenharmony_ci				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
93262306a36Sopenharmony_ci				  req->spec.rem_host, ntohs(req->spec.rem_port),
93362306a36Sopenharmony_ci				  req->spec.loc_host, ntohs(req->spec.loc_port),
93462306a36Sopenharmony_ci				  req->rxq_index, req->flow_id, rc, arfs_id);
93562306a36Sopenharmony_ci		channel->n_rfs_failed++;
93662306a36Sopenharmony_ci		/* We're overloading the NIC's filter tables, so let's do a
93762306a36Sopenharmony_ci		 * chunk of extra expiry work.
93862306a36Sopenharmony_ci		 */
93962306a36Sopenharmony_ci		__efx_siena_filter_rfs_expire(channel,
94062306a36Sopenharmony_ci					      min(channel->rfs_filter_count,
94162306a36Sopenharmony_ci						  100u));
94262306a36Sopenharmony_ci	}
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_ci	/* Release references */
94562306a36Sopenharmony_ci	clear_bit(slot_idx, &efx->rps_slot_map);
94662306a36Sopenharmony_ci	dev_put(req->net_dev);
94762306a36Sopenharmony_ci}
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_ciint efx_siena_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
95062306a36Sopenharmony_ci			 u16 rxq_index, u32 flow_id)
95162306a36Sopenharmony_ci{
95262306a36Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
95362306a36Sopenharmony_ci	struct efx_async_filter_insertion *req;
95462306a36Sopenharmony_ci	struct efx_arfs_rule *rule;
95562306a36Sopenharmony_ci	struct flow_keys fk;
95662306a36Sopenharmony_ci	int slot_idx;
95762306a36Sopenharmony_ci	bool new;
95862306a36Sopenharmony_ci	int rc;
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_ci	/* find a free slot */
96162306a36Sopenharmony_ci	for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
96262306a36Sopenharmony_ci		if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
96362306a36Sopenharmony_ci			break;
96462306a36Sopenharmony_ci	if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
96562306a36Sopenharmony_ci		return -EBUSY;
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ci	if (flow_id == RPS_FLOW_ID_INVALID) {
96862306a36Sopenharmony_ci		rc = -EINVAL;
96962306a36Sopenharmony_ci		goto out_clear;
97062306a36Sopenharmony_ci	}
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci	if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
97362306a36Sopenharmony_ci		rc = -EPROTONOSUPPORT;
97462306a36Sopenharmony_ci		goto out_clear;
97562306a36Sopenharmony_ci	}
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
97862306a36Sopenharmony_ci		rc = -EPROTONOSUPPORT;
97962306a36Sopenharmony_ci		goto out_clear;
98062306a36Sopenharmony_ci	}
98162306a36Sopenharmony_ci	if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
98262306a36Sopenharmony_ci		rc = -EPROTONOSUPPORT;
98362306a36Sopenharmony_ci		goto out_clear;
98462306a36Sopenharmony_ci	}
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	req = efx->rps_slot + slot_idx;
98762306a36Sopenharmony_ci	efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
98862306a36Sopenharmony_ci			   efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
98962306a36Sopenharmony_ci			   rxq_index);
99062306a36Sopenharmony_ci	req->spec.match_flags =
99162306a36Sopenharmony_ci		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
99262306a36Sopenharmony_ci		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
99362306a36Sopenharmony_ci		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
99462306a36Sopenharmony_ci	req->spec.ether_type = fk.basic.n_proto;
99562306a36Sopenharmony_ci	req->spec.ip_proto = fk.basic.ip_proto;
99662306a36Sopenharmony_ci
99762306a36Sopenharmony_ci	if (fk.basic.n_proto == htons(ETH_P_IP)) {
99862306a36Sopenharmony_ci		req->spec.rem_host[0] = fk.addrs.v4addrs.src;
99962306a36Sopenharmony_ci		req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
100062306a36Sopenharmony_ci	} else {
100162306a36Sopenharmony_ci		memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
100262306a36Sopenharmony_ci		       sizeof(struct in6_addr));
100362306a36Sopenharmony_ci		memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
100462306a36Sopenharmony_ci		       sizeof(struct in6_addr));
100562306a36Sopenharmony_ci	}
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci	req->spec.rem_port = fk.ports.src;
100862306a36Sopenharmony_ci	req->spec.loc_port = fk.ports.dst;
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci	if (efx->rps_hash_table) {
101162306a36Sopenharmony_ci		/* Add it to ARFS hash table */
101262306a36Sopenharmony_ci		spin_lock(&efx->rps_hash_lock);
101362306a36Sopenharmony_ci		rule = efx_rps_hash_add(efx, &req->spec, &new);
101462306a36Sopenharmony_ci		if (!rule) {
101562306a36Sopenharmony_ci			rc = -ENOMEM;
101662306a36Sopenharmony_ci			goto out_unlock;
101762306a36Sopenharmony_ci		}
101862306a36Sopenharmony_ci		if (new)
101962306a36Sopenharmony_ci			rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
102062306a36Sopenharmony_ci		rc = rule->arfs_id;
102162306a36Sopenharmony_ci		/* Skip if existing or pending filter already does the right thing */
102262306a36Sopenharmony_ci		if (!new && rule->rxq_index == rxq_index &&
102362306a36Sopenharmony_ci		    rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
102462306a36Sopenharmony_ci			goto out_unlock;
102562306a36Sopenharmony_ci		rule->rxq_index = rxq_index;
102662306a36Sopenharmony_ci		rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
102762306a36Sopenharmony_ci		spin_unlock(&efx->rps_hash_lock);
102862306a36Sopenharmony_ci	} else {
102962306a36Sopenharmony_ci		/* Without an ARFS hash table, we just use arfs_id 0 for all
103062306a36Sopenharmony_ci		 * filters.  This means if multiple flows hash to the same
103162306a36Sopenharmony_ci		 * flow_id, all but the most recently touched will be eligible
103262306a36Sopenharmony_ci		 * for expiry.
103362306a36Sopenharmony_ci		 */
103462306a36Sopenharmony_ci		rc = 0;
103562306a36Sopenharmony_ci	}
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	/* Queue the request */
103862306a36Sopenharmony_ci	dev_hold(req->net_dev = net_dev);
103962306a36Sopenharmony_ci	INIT_WORK(&req->work, efx_filter_rfs_work);
104062306a36Sopenharmony_ci	req->rxq_index = rxq_index;
104162306a36Sopenharmony_ci	req->flow_id = flow_id;
104262306a36Sopenharmony_ci	schedule_work(&req->work);
104362306a36Sopenharmony_ci	return rc;
104462306a36Sopenharmony_ciout_unlock:
104562306a36Sopenharmony_ci	spin_unlock(&efx->rps_hash_lock);
104662306a36Sopenharmony_ciout_clear:
104762306a36Sopenharmony_ci	clear_bit(slot_idx, &efx->rps_slot_map);
104862306a36Sopenharmony_ci	return rc;
104962306a36Sopenharmony_ci}
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_cibool __efx_siena_filter_rfs_expire(struct efx_channel *channel,
105262306a36Sopenharmony_ci				   unsigned int quota)
105362306a36Sopenharmony_ci{
105462306a36Sopenharmony_ci	bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
105562306a36Sopenharmony_ci	struct efx_nic *efx = channel->efx;
105662306a36Sopenharmony_ci	unsigned int index, size, start;
105762306a36Sopenharmony_ci	u32 flow_id;
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci	if (!mutex_trylock(&efx->rps_mutex))
106062306a36Sopenharmony_ci		return false;
106162306a36Sopenharmony_ci	expire_one = efx->type->filter_rfs_expire_one;
106262306a36Sopenharmony_ci	index = channel->rfs_expire_index;
106362306a36Sopenharmony_ci	start = index;
106462306a36Sopenharmony_ci	size = efx->type->max_rx_ip_filters;
106562306a36Sopenharmony_ci	while (quota) {
106662306a36Sopenharmony_ci		flow_id = channel->rps_flow_id[index];
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci		if (flow_id != RPS_FLOW_ID_INVALID) {
106962306a36Sopenharmony_ci			quota--;
107062306a36Sopenharmony_ci			if (expire_one(efx, flow_id, index)) {
107162306a36Sopenharmony_ci				netif_info(efx, rx_status, efx->net_dev,
107262306a36Sopenharmony_ci					   "expired filter %d [channel %u flow %u]\n",
107362306a36Sopenharmony_ci					   index, channel->channel, flow_id);
107462306a36Sopenharmony_ci				channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
107562306a36Sopenharmony_ci				channel->rfs_filter_count--;
107662306a36Sopenharmony_ci			}
107762306a36Sopenharmony_ci		}
107862306a36Sopenharmony_ci		if (++index == size)
107962306a36Sopenharmony_ci			index = 0;
108062306a36Sopenharmony_ci		/* If we were called with a quota that exceeds the total number
108162306a36Sopenharmony_ci		 * of filters in the table (which shouldn't happen, but could
108262306a36Sopenharmony_ci		 * if two callers race), ensure that we don't loop forever -
108362306a36Sopenharmony_ci		 * stop when we've examined every row of the table.
108462306a36Sopenharmony_ci		 */
108562306a36Sopenharmony_ci		if (index == start)
108662306a36Sopenharmony_ci			break;
108762306a36Sopenharmony_ci	}
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	channel->rfs_expire_index = index;
109062306a36Sopenharmony_ci	mutex_unlock(&efx->rps_mutex);
109162306a36Sopenharmony_ci	return true;
109262306a36Sopenharmony_ci}
109362306a36Sopenharmony_ci
109462306a36Sopenharmony_ci#endif /* CONFIG_RFS_ACCEL */
1095