18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2018 Solarflare Communications Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it
78c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License version 2 as published
88c2ecf20Sopenharmony_ci * by the Free Software Foundation, incorporated herein by reference.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "net_driver.h"
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/iommu.h>
148c2ecf20Sopenharmony_ci#include "efx.h"
158c2ecf20Sopenharmony_ci#include "nic.h"
168c2ecf20Sopenharmony_ci#include "rx_common.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* This is the percentage fill level below which new RX descriptors
198c2ecf20Sopenharmony_ci * will be added to the RX descriptor ring.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistatic unsigned int rx_refill_threshold;
228c2ecf20Sopenharmony_cimodule_param(rx_refill_threshold, uint, 0444);
238c2ecf20Sopenharmony_ciMODULE_PARM_DESC(rx_refill_threshold,
248c2ecf20Sopenharmony_ci		 "RX descriptor ring refill threshold (%)");
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Number of RX buffers to recycle pages for.  When creating the RX page recycle
278c2ecf20Sopenharmony_ci * ring, this number is divided by the number of buffers per page to calculate
288c2ecf20Sopenharmony_ci * the number of pages to store in the RX page recycle ring.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#define EFX_RECYCLE_RING_SIZE_IOMMU 4096
318c2ecf20Sopenharmony_ci#define EFX_RECYCLE_RING_SIZE_NOIOMMU (2 * EFX_RX_PREFERRED_BATCH)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* RX maximum head room required.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * This must be at least 1 to prevent overflow, plus one packet-worth
368c2ecf20Sopenharmony_ci * to allow pipelined receives.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci#define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Check the RX page recycle ring for a page that can be reused. */
418c2ecf20Sopenharmony_cistatic struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
448c2ecf20Sopenharmony_ci	struct efx_rx_page_state *state;
458c2ecf20Sopenharmony_ci	unsigned int index;
468c2ecf20Sopenharmony_ci	struct page *page;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
498c2ecf20Sopenharmony_ci		return NULL;
508c2ecf20Sopenharmony_ci	index = rx_queue->page_remove & rx_queue->page_ptr_mask;
518c2ecf20Sopenharmony_ci	page = rx_queue->page_ring[index];
528c2ecf20Sopenharmony_ci	if (page == NULL)
538c2ecf20Sopenharmony_ci		return NULL;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	rx_queue->page_ring[index] = NULL;
568c2ecf20Sopenharmony_ci	/* page_remove cannot exceed page_add. */
578c2ecf20Sopenharmony_ci	if (rx_queue->page_remove != rx_queue->page_add)
588c2ecf20Sopenharmony_ci		++rx_queue->page_remove;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* If page_count is 1 then we hold the only reference to this page. */
618c2ecf20Sopenharmony_ci	if (page_count(page) == 1) {
628c2ecf20Sopenharmony_ci		++rx_queue->page_recycle_count;
638c2ecf20Sopenharmony_ci		return page;
648c2ecf20Sopenharmony_ci	} else {
658c2ecf20Sopenharmony_ci		state = page_address(page);
668c2ecf20Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
678c2ecf20Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
688c2ecf20Sopenharmony_ci			       DMA_FROM_DEVICE);
698c2ecf20Sopenharmony_ci		put_page(page);
708c2ecf20Sopenharmony_ci		++rx_queue->page_recycle_failed;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return NULL;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* Attempt to recycle the page if there is an RX recycle ring; the page can
778c2ecf20Sopenharmony_ci * only be added if this is the final RX buffer, to prevent pages being used in
788c2ecf20Sopenharmony_ci * the descriptor ring and appearing in the recycle ring simultaneously.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_cistatic void efx_recycle_rx_page(struct efx_channel *channel,
818c2ecf20Sopenharmony_ci				struct efx_rx_buffer *rx_buf)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
848c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
858c2ecf20Sopenharmony_ci	struct page *page = rx_buf->page;
868c2ecf20Sopenharmony_ci	unsigned int index;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* Only recycle the page after processing the final buffer. */
898c2ecf20Sopenharmony_ci	if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
908c2ecf20Sopenharmony_ci		return;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	index = rx_queue->page_add & rx_queue->page_ptr_mask;
938c2ecf20Sopenharmony_ci	if (rx_queue->page_ring[index] == NULL) {
948c2ecf20Sopenharmony_ci		unsigned int read_index = rx_queue->page_remove &
958c2ecf20Sopenharmony_ci			rx_queue->page_ptr_mask;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		/* The next slot in the recycle ring is available, but
988c2ecf20Sopenharmony_ci		 * increment page_remove if the read pointer currently
998c2ecf20Sopenharmony_ci		 * points here.
1008c2ecf20Sopenharmony_ci		 */
1018c2ecf20Sopenharmony_ci		if (read_index == index)
1028c2ecf20Sopenharmony_ci			++rx_queue->page_remove;
1038c2ecf20Sopenharmony_ci		rx_queue->page_ring[index] = page;
1048c2ecf20Sopenharmony_ci		++rx_queue->page_add;
1058c2ecf20Sopenharmony_ci		return;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci	++rx_queue->page_recycle_full;
1088c2ecf20Sopenharmony_ci	efx_unmap_rx_buffer(efx, rx_buf);
1098c2ecf20Sopenharmony_ci	put_page(rx_buf->page);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* Recycle the pages that are used by buffers that have just been received. */
1138c2ecf20Sopenharmony_civoid efx_recycle_rx_pages(struct efx_channel *channel,
1148c2ecf20Sopenharmony_ci			  struct efx_rx_buffer *rx_buf,
1158c2ecf20Sopenharmony_ci			  unsigned int n_frags)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
1208c2ecf20Sopenharmony_ci		return;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	do {
1238c2ecf20Sopenharmony_ci		efx_recycle_rx_page(channel, rx_buf);
1248c2ecf20Sopenharmony_ci		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
1258c2ecf20Sopenharmony_ci	} while (--n_frags);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_civoid efx_discard_rx_packet(struct efx_channel *channel,
1298c2ecf20Sopenharmony_ci			   struct efx_rx_buffer *rx_buf,
1308c2ecf20Sopenharmony_ci			   unsigned int n_frags)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	efx_recycle_rx_pages(channel, rx_buf, n_frags);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	unsigned int bufs_in_recycle_ring, page_ring_size;
1428c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Set the RX recycle ring size */
1458c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
1468c2ecf20Sopenharmony_ci	bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
1478c2ecf20Sopenharmony_ci#else
1488c2ecf20Sopenharmony_ci	if (iommu_present(&pci_bus_type))
1498c2ecf20Sopenharmony_ci		bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
1508c2ecf20Sopenharmony_ci	else
1518c2ecf20Sopenharmony_ci		bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_NOIOMMU;
1528c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC64 */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
1558c2ecf20Sopenharmony_ci					    efx->rx_bufs_per_page);
1568c2ecf20Sopenharmony_ci	rx_queue->page_ring = kcalloc(page_ring_size,
1578c2ecf20Sopenharmony_ci				      sizeof(*rx_queue->page_ring), GFP_KERNEL);
1588c2ecf20Sopenharmony_ci	if (!rx_queue->page_ring)
1598c2ecf20Sopenharmony_ci		rx_queue->page_ptr_mask = 0;
1608c2ecf20Sopenharmony_ci	else
1618c2ecf20Sopenharmony_ci		rx_queue->page_ptr_mask = page_ring_size - 1;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
1678c2ecf20Sopenharmony_ci	int i;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (unlikely(!rx_queue->page_ring))
1708c2ecf20Sopenharmony_ci		return;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* Unmap and release the pages in the recycle ring. Remove the ring. */
1738c2ecf20Sopenharmony_ci	for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
1748c2ecf20Sopenharmony_ci		struct page *page = rx_queue->page_ring[i];
1758c2ecf20Sopenharmony_ci		struct efx_rx_page_state *state;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		if (page == NULL)
1788c2ecf20Sopenharmony_ci			continue;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		state = page_address(page);
1818c2ecf20Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
1828c2ecf20Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
1838c2ecf20Sopenharmony_ci			       DMA_FROM_DEVICE);
1848c2ecf20Sopenharmony_ci		put_page(page);
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci	kfree(rx_queue->page_ring);
1878c2ecf20Sopenharmony_ci	rx_queue->page_ring = NULL;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
1918c2ecf20Sopenharmony_ci			       struct efx_rx_buffer *rx_buf)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	/* Release the page reference we hold for the buffer. */
1948c2ecf20Sopenharmony_ci	if (rx_buf->page)
1958c2ecf20Sopenharmony_ci		put_page(rx_buf->page);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* If this is the last buffer in a page, unmap and free it. */
1988c2ecf20Sopenharmony_ci	if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
1998c2ecf20Sopenharmony_ci		efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
2008c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf, 1);
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci	rx_buf->page = NULL;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ciint efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
2088c2ecf20Sopenharmony_ci	unsigned int entries;
2098c2ecf20Sopenharmony_ci	int rc;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* Create the smallest power-of-two aligned ring */
2128c2ecf20Sopenharmony_ci	entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
2138c2ecf20Sopenharmony_ci	EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
2148c2ecf20Sopenharmony_ci	rx_queue->ptr_mask = entries - 1;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	netif_dbg(efx, probe, efx->net_dev,
2178c2ecf20Sopenharmony_ci		  "creating RX queue %d size %#x mask %#x\n",
2188c2ecf20Sopenharmony_ci		  efx_rx_queue_index(rx_queue), efx->rxq_entries,
2198c2ecf20Sopenharmony_ci		  rx_queue->ptr_mask);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* Allocate RX buffers */
2228c2ecf20Sopenharmony_ci	rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
2238c2ecf20Sopenharmony_ci				   GFP_KERNEL);
2248c2ecf20Sopenharmony_ci	if (!rx_queue->buffer)
2258c2ecf20Sopenharmony_ci		return -ENOMEM;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	rc = efx_nic_probe_rx(rx_queue);
2288c2ecf20Sopenharmony_ci	if (rc) {
2298c2ecf20Sopenharmony_ci		kfree(rx_queue->buffer);
2308c2ecf20Sopenharmony_ci		rx_queue->buffer = NULL;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return rc;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_civoid efx_init_rx_queue(struct efx_rx_queue *rx_queue)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	unsigned int max_fill, trigger, max_trigger;
2398c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
2408c2ecf20Sopenharmony_ci	int rc = 0;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
2438c2ecf20Sopenharmony_ci		  "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/* Initialise ptr fields */
2468c2ecf20Sopenharmony_ci	rx_queue->added_count = 0;
2478c2ecf20Sopenharmony_ci	rx_queue->notified_count = 0;
2488c2ecf20Sopenharmony_ci	rx_queue->removed_count = 0;
2498c2ecf20Sopenharmony_ci	rx_queue->min_fill = -1U;
2508c2ecf20Sopenharmony_ci	efx_init_rx_recycle_ring(rx_queue);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	rx_queue->page_remove = 0;
2538c2ecf20Sopenharmony_ci	rx_queue->page_add = rx_queue->page_ptr_mask + 1;
2548c2ecf20Sopenharmony_ci	rx_queue->page_recycle_count = 0;
2558c2ecf20Sopenharmony_ci	rx_queue->page_recycle_failed = 0;
2568c2ecf20Sopenharmony_ci	rx_queue->page_recycle_full = 0;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* Initialise limit fields */
2598c2ecf20Sopenharmony_ci	max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
2608c2ecf20Sopenharmony_ci	max_trigger =
2618c2ecf20Sopenharmony_ci		max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
2628c2ecf20Sopenharmony_ci	if (rx_refill_threshold != 0) {
2638c2ecf20Sopenharmony_ci		trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
2648c2ecf20Sopenharmony_ci		if (trigger > max_trigger)
2658c2ecf20Sopenharmony_ci			trigger = max_trigger;
2668c2ecf20Sopenharmony_ci	} else {
2678c2ecf20Sopenharmony_ci		trigger = max_trigger;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	rx_queue->max_fill = max_fill;
2718c2ecf20Sopenharmony_ci	rx_queue->fast_fill_trigger = trigger;
2728c2ecf20Sopenharmony_ci	rx_queue->refill_enabled = true;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Initialise XDP queue information */
2758c2ecf20Sopenharmony_ci	rc = xdp_rxq_info_reg(&rx_queue->xdp_rxq_info, efx->net_dev,
2768c2ecf20Sopenharmony_ci			      rx_queue->core_index);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (rc) {
2798c2ecf20Sopenharmony_ci		netif_err(efx, rx_err, efx->net_dev,
2808c2ecf20Sopenharmony_ci			  "Failure to initialise XDP queue information rc=%d\n",
2818c2ecf20Sopenharmony_ci			  rc);
2828c2ecf20Sopenharmony_ci		efx->xdp_rxq_info_failed = true;
2838c2ecf20Sopenharmony_ci	} else {
2848c2ecf20Sopenharmony_ci		rx_queue->xdp_rxq_info_valid = true;
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Set up RX descriptor ring */
2888c2ecf20Sopenharmony_ci	efx_nic_init_rx(rx_queue);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_civoid efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	struct efx_rx_buffer *rx_buf;
2948c2ecf20Sopenharmony_ci	int i;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
2978c2ecf20Sopenharmony_ci		  "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	del_timer_sync(&rx_queue->slow_fill);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/* Release RX buffers from the current read ptr to the write ptr */
3028c2ecf20Sopenharmony_ci	if (rx_queue->buffer) {
3038c2ecf20Sopenharmony_ci		for (i = rx_queue->removed_count; i < rx_queue->added_count;
3048c2ecf20Sopenharmony_ci		     i++) {
3058c2ecf20Sopenharmony_ci			unsigned int index = i & rx_queue->ptr_mask;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci			rx_buf = efx_rx_buffer(rx_queue, index);
3088c2ecf20Sopenharmony_ci			efx_fini_rx_buffer(rx_queue, rx_buf);
3098c2ecf20Sopenharmony_ci		}
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	efx_fini_rx_recycle_ring(rx_queue);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (rx_queue->xdp_rxq_info_valid)
3158c2ecf20Sopenharmony_ci		xdp_rxq_info_unreg(&rx_queue->xdp_rxq_info);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	rx_queue->xdp_rxq_info_valid = false;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_civoid efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
3238c2ecf20Sopenharmony_ci		  "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	efx_nic_remove_rx(rx_queue);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	kfree(rx_queue->buffer);
3288c2ecf20Sopenharmony_ci	rx_queue->buffer = NULL;
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/* Unmap a DMA-mapped page.  This function is only called for the final RX
3328c2ecf20Sopenharmony_ci * buffer in a page.
3338c2ecf20Sopenharmony_ci */
3348c2ecf20Sopenharmony_civoid efx_unmap_rx_buffer(struct efx_nic *efx,
3358c2ecf20Sopenharmony_ci			 struct efx_rx_buffer *rx_buf)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	struct page *page = rx_buf->page;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	if (page) {
3408c2ecf20Sopenharmony_ci		struct efx_rx_page_state *state = page_address(page);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		dma_unmap_page(&efx->pci_dev->dev,
3438c2ecf20Sopenharmony_ci			       state->dma_addr,
3448c2ecf20Sopenharmony_ci			       PAGE_SIZE << efx->rx_buffer_order,
3458c2ecf20Sopenharmony_ci			       DMA_FROM_DEVICE);
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_civoid efx_free_rx_buffers(struct efx_rx_queue *rx_queue,
3508c2ecf20Sopenharmony_ci			 struct efx_rx_buffer *rx_buf,
3518c2ecf20Sopenharmony_ci			 unsigned int num_bufs)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	do {
3548c2ecf20Sopenharmony_ci		if (rx_buf->page) {
3558c2ecf20Sopenharmony_ci			put_page(rx_buf->page);
3568c2ecf20Sopenharmony_ci			rx_buf->page = NULL;
3578c2ecf20Sopenharmony_ci		}
3588c2ecf20Sopenharmony_ci		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
3598c2ecf20Sopenharmony_ci	} while (--num_bufs);
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_civoid efx_rx_slow_fill(struct timer_list *t)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Post an event to cause NAPI to run and refill the queue */
3678c2ecf20Sopenharmony_ci	efx_nic_generate_fill_event(rx_queue);
3688c2ecf20Sopenharmony_ci	++rx_queue->slow_fill_count;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_civoid efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
3778c2ecf20Sopenharmony_ci *
3788c2ecf20Sopenharmony_ci * @rx_queue:		Efx RX queue
3798c2ecf20Sopenharmony_ci *
3808c2ecf20Sopenharmony_ci * This allocates a batch of pages, maps them for DMA, and populates
3818c2ecf20Sopenharmony_ci * struct efx_rx_buffers for each one. Return a negative error code or
3828c2ecf20Sopenharmony_ci * 0 on success. If a single page can be used for multiple buffers,
3838c2ecf20Sopenharmony_ci * then the page will either be inserted fully, or not at all.
3848c2ecf20Sopenharmony_ci */
3858c2ecf20Sopenharmony_cistatic int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	unsigned int page_offset, index, count;
3888c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
3898c2ecf20Sopenharmony_ci	struct efx_rx_page_state *state;
3908c2ecf20Sopenharmony_ci	struct efx_rx_buffer *rx_buf;
3918c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
3928c2ecf20Sopenharmony_ci	struct page *page;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	count = 0;
3958c2ecf20Sopenharmony_ci	do {
3968c2ecf20Sopenharmony_ci		page = efx_reuse_page(rx_queue);
3978c2ecf20Sopenharmony_ci		if (page == NULL) {
3988c2ecf20Sopenharmony_ci			page = alloc_pages(__GFP_COMP |
3998c2ecf20Sopenharmony_ci					   (atomic ? GFP_ATOMIC : GFP_KERNEL),
4008c2ecf20Sopenharmony_ci					   efx->rx_buffer_order);
4018c2ecf20Sopenharmony_ci			if (unlikely(page == NULL))
4028c2ecf20Sopenharmony_ci				return -ENOMEM;
4038c2ecf20Sopenharmony_ci			dma_addr =
4048c2ecf20Sopenharmony_ci				dma_map_page(&efx->pci_dev->dev, page, 0,
4058c2ecf20Sopenharmony_ci					     PAGE_SIZE << efx->rx_buffer_order,
4068c2ecf20Sopenharmony_ci					     DMA_FROM_DEVICE);
4078c2ecf20Sopenharmony_ci			if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
4088c2ecf20Sopenharmony_ci						       dma_addr))) {
4098c2ecf20Sopenharmony_ci				__free_pages(page, efx->rx_buffer_order);
4108c2ecf20Sopenharmony_ci				return -EIO;
4118c2ecf20Sopenharmony_ci			}
4128c2ecf20Sopenharmony_ci			state = page_address(page);
4138c2ecf20Sopenharmony_ci			state->dma_addr = dma_addr;
4148c2ecf20Sopenharmony_ci		} else {
4158c2ecf20Sopenharmony_ci			state = page_address(page);
4168c2ecf20Sopenharmony_ci			dma_addr = state->dma_addr;
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		dma_addr += sizeof(struct efx_rx_page_state);
4208c2ecf20Sopenharmony_ci		page_offset = sizeof(struct efx_rx_page_state);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		do {
4238c2ecf20Sopenharmony_ci			index = rx_queue->added_count & rx_queue->ptr_mask;
4248c2ecf20Sopenharmony_ci			rx_buf = efx_rx_buffer(rx_queue, index);
4258c2ecf20Sopenharmony_ci			rx_buf->dma_addr = dma_addr + efx->rx_ip_align +
4268c2ecf20Sopenharmony_ci					   EFX_XDP_HEADROOM;
4278c2ecf20Sopenharmony_ci			rx_buf->page = page;
4288c2ecf20Sopenharmony_ci			rx_buf->page_offset = page_offset + efx->rx_ip_align +
4298c2ecf20Sopenharmony_ci					      EFX_XDP_HEADROOM;
4308c2ecf20Sopenharmony_ci			rx_buf->len = efx->rx_dma_len;
4318c2ecf20Sopenharmony_ci			rx_buf->flags = 0;
4328c2ecf20Sopenharmony_ci			++rx_queue->added_count;
4338c2ecf20Sopenharmony_ci			get_page(page);
4348c2ecf20Sopenharmony_ci			dma_addr += efx->rx_page_buf_step;
4358c2ecf20Sopenharmony_ci			page_offset += efx->rx_page_buf_step;
4368c2ecf20Sopenharmony_ci		} while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
4398c2ecf20Sopenharmony_ci	} while (++count < efx->rx_pages_per_batch);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return 0;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_civoid efx_rx_config_page_split(struct efx_nic *efx)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align +
4478c2ecf20Sopenharmony_ci				      EFX_XDP_HEADROOM + EFX_XDP_TAILROOM,
4488c2ecf20Sopenharmony_ci				      EFX_RX_BUF_ALIGNMENT);
4498c2ecf20Sopenharmony_ci	efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
4508c2ecf20Sopenharmony_ci		((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
4518c2ecf20Sopenharmony_ci		efx->rx_page_buf_step);
4528c2ecf20Sopenharmony_ci	efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
4538c2ecf20Sopenharmony_ci		efx->rx_bufs_per_page;
4548c2ecf20Sopenharmony_ci	efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
4558c2ecf20Sopenharmony_ci					       efx->rx_bufs_per_page);
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/* efx_fast_push_rx_descriptors - push new RX descriptors quickly
4598c2ecf20Sopenharmony_ci * @rx_queue:		RX descriptor queue
4608c2ecf20Sopenharmony_ci *
4618c2ecf20Sopenharmony_ci * This will aim to fill the RX descriptor queue up to
4628c2ecf20Sopenharmony_ci * @rx_queue->@max_fill. If there is insufficient atomic
4638c2ecf20Sopenharmony_ci * memory to do so, a slow fill will be scheduled.
4648c2ecf20Sopenharmony_ci *
4658c2ecf20Sopenharmony_ci * The caller must provide serialisation (none is used here). In practise,
4668c2ecf20Sopenharmony_ci * this means this function must run from the NAPI handler, or be called
4678c2ecf20Sopenharmony_ci * when NAPI is disabled.
4688c2ecf20Sopenharmony_ci */
4698c2ecf20Sopenharmony_civoid efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
4728c2ecf20Sopenharmony_ci	unsigned int fill_level, batch_size;
4738c2ecf20Sopenharmony_ci	int space, rc = 0;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (!rx_queue->refill_enabled)
4768c2ecf20Sopenharmony_ci		return;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* Calculate current fill level, and exit if we don't need to fill */
4798c2ecf20Sopenharmony_ci	fill_level = (rx_queue->added_count - rx_queue->removed_count);
4808c2ecf20Sopenharmony_ci	EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
4818c2ecf20Sopenharmony_ci	if (fill_level >= rx_queue->fast_fill_trigger)
4828c2ecf20Sopenharmony_ci		goto out;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* Record minimum fill level */
4858c2ecf20Sopenharmony_ci	if (unlikely(fill_level < rx_queue->min_fill)) {
4868c2ecf20Sopenharmony_ci		if (fill_level)
4878c2ecf20Sopenharmony_ci			rx_queue->min_fill = fill_level;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
4918c2ecf20Sopenharmony_ci	space = rx_queue->max_fill - fill_level;
4928c2ecf20Sopenharmony_ci	EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
4958c2ecf20Sopenharmony_ci		   "RX queue %d fast-filling descriptor ring from"
4968c2ecf20Sopenharmony_ci		   " level %d to level %d\n",
4978c2ecf20Sopenharmony_ci		   efx_rx_queue_index(rx_queue), fill_level,
4988c2ecf20Sopenharmony_ci		   rx_queue->max_fill);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	do {
5018c2ecf20Sopenharmony_ci		rc = efx_init_rx_buffers(rx_queue, atomic);
5028c2ecf20Sopenharmony_ci		if (unlikely(rc)) {
5038c2ecf20Sopenharmony_ci			/* Ensure that we don't leave the rx queue empty */
5048c2ecf20Sopenharmony_ci			efx_schedule_slow_fill(rx_queue);
5058c2ecf20Sopenharmony_ci			goto out;
5068c2ecf20Sopenharmony_ci		}
5078c2ecf20Sopenharmony_ci	} while ((space -= batch_size) >= batch_size);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
5108c2ecf20Sopenharmony_ci		   "RX queue %d fast-filled descriptor ring "
5118c2ecf20Sopenharmony_ci		   "to level %d\n", efx_rx_queue_index(rx_queue),
5128c2ecf20Sopenharmony_ci		   rx_queue->added_count - rx_queue->removed_count);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci out:
5158c2ecf20Sopenharmony_ci	if (rx_queue->notified_count != rx_queue->added_count)
5168c2ecf20Sopenharmony_ci		efx_nic_notify_rx_desc(rx_queue);
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci/* Pass a received packet up through GRO.  GRO can handle pages
5208c2ecf20Sopenharmony_ci * regardless of checksum state and skbs with a good checksum.
5218c2ecf20Sopenharmony_ci */
5228c2ecf20Sopenharmony_civoid
5238c2ecf20Sopenharmony_ciefx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
5248c2ecf20Sopenharmony_ci		  unsigned int n_frags, u8 *eh, __wsum csum)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct napi_struct *napi = &channel->napi_str;
5278c2ecf20Sopenharmony_ci	struct efx_nic *efx = channel->efx;
5288c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	skb = napi_get_frags(napi);
5318c2ecf20Sopenharmony_ci	if (unlikely(!skb)) {
5328c2ecf20Sopenharmony_ci		struct efx_rx_queue *rx_queue;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		rx_queue = efx_channel_get_rx_queue(channel);
5358c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
5368c2ecf20Sopenharmony_ci		return;
5378c2ecf20Sopenharmony_ci	}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	if (efx->net_dev->features & NETIF_F_RXHASH &&
5408c2ecf20Sopenharmony_ci	    efx_rx_buf_hash_valid(efx, eh))
5418c2ecf20Sopenharmony_ci		skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
5428c2ecf20Sopenharmony_ci			     PKT_HASH_TYPE_L3);
5438c2ecf20Sopenharmony_ci	if (csum) {
5448c2ecf20Sopenharmony_ci		skb->csum = csum;
5458c2ecf20Sopenharmony_ci		skb->ip_summed = CHECKSUM_COMPLETE;
5468c2ecf20Sopenharmony_ci	} else {
5478c2ecf20Sopenharmony_ci		skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
5488c2ecf20Sopenharmony_ci				  CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci	skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	for (;;) {
5538c2ecf20Sopenharmony_ci		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
5548c2ecf20Sopenharmony_ci				   rx_buf->page, rx_buf->page_offset,
5558c2ecf20Sopenharmony_ci				   rx_buf->len);
5568c2ecf20Sopenharmony_ci		rx_buf->page = NULL;
5578c2ecf20Sopenharmony_ci		skb->len += rx_buf->len;
5588c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->nr_frags == n_frags)
5598c2ecf20Sopenharmony_ci			break;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci		rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	skb->data_len = skb->len;
5658c2ecf20Sopenharmony_ci	skb->truesize += n_frags * efx->rx_buffer_truesize;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	skb_record_rx_queue(skb, channel->rx_queue.core_index);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	napi_gro_frags(napi);
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci/* RSS contexts.  We're using linked lists and crappy O(n) algorithms, because
5738c2ecf20Sopenharmony_ci * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
5748c2ecf20Sopenharmony_ci */
5758c2ecf20Sopenharmony_cistruct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	struct list_head *head = &efx->rss_context.list;
5788c2ecf20Sopenharmony_ci	struct efx_rss_context *ctx, *new;
5798c2ecf20Sopenharmony_ci	u32 id = 1; /* Don't use zero, that refers to the master RSS context */
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	WARN_ON(!mutex_is_locked(&efx->rss_lock));
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	/* Search for first gap in the numbering */
5848c2ecf20Sopenharmony_ci	list_for_each_entry(ctx, head, list) {
5858c2ecf20Sopenharmony_ci		if (ctx->user_id != id)
5868c2ecf20Sopenharmony_ci			break;
5878c2ecf20Sopenharmony_ci		id++;
5888c2ecf20Sopenharmony_ci		/* Check for wrap.  If this happens, we have nearly 2^32
5898c2ecf20Sopenharmony_ci		 * allocated RSS contexts, which seems unlikely.
5908c2ecf20Sopenharmony_ci		 */
5918c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(!id))
5928c2ecf20Sopenharmony_ci			return NULL;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/* Create the new entry */
5968c2ecf20Sopenharmony_ci	new = kmalloc(sizeof(*new), GFP_KERNEL);
5978c2ecf20Sopenharmony_ci	if (!new)
5988c2ecf20Sopenharmony_ci		return NULL;
5998c2ecf20Sopenharmony_ci	new->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
6008c2ecf20Sopenharmony_ci	new->rx_hash_udp_4tuple = false;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* Insert the new entry into the gap */
6038c2ecf20Sopenharmony_ci	new->user_id = id;
6048c2ecf20Sopenharmony_ci	list_add_tail(&new->list, &ctx->list);
6058c2ecf20Sopenharmony_ci	return new;
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistruct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	struct list_head *head = &efx->rss_context.list;
6118c2ecf20Sopenharmony_ci	struct efx_rss_context *ctx;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	WARN_ON(!mutex_is_locked(&efx->rss_lock));
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	list_for_each_entry(ctx, head, list)
6168c2ecf20Sopenharmony_ci		if (ctx->user_id == id)
6178c2ecf20Sopenharmony_ci			return ctx;
6188c2ecf20Sopenharmony_ci	return NULL;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_civoid efx_free_rss_context_entry(struct efx_rss_context *ctx)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	list_del(&ctx->list);
6248c2ecf20Sopenharmony_ci	kfree(ctx);
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_civoid efx_set_default_rx_indir_table(struct efx_nic *efx,
6288c2ecf20Sopenharmony_ci				    struct efx_rss_context *ctx)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	size_t i;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
6338c2ecf20Sopenharmony_ci		ctx->rx_indir_table[i] =
6348c2ecf20Sopenharmony_ci			ethtool_rxfh_indir_default(i, efx->rss_spread);
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci/**
6388c2ecf20Sopenharmony_ci * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
6398c2ecf20Sopenharmony_ci * @spec: Specification to test
6408c2ecf20Sopenharmony_ci *
6418c2ecf20Sopenharmony_ci * Return: %true if the specification is a non-drop RX filter that
6428c2ecf20Sopenharmony_ci * matches a local MAC address I/G bit value of 1 or matches a local
6438c2ecf20Sopenharmony_ci * IPv4 or IPv6 address value in the respective multicast address
6448c2ecf20Sopenharmony_ci * range.  Otherwise %false.
6458c2ecf20Sopenharmony_ci */
6468c2ecf20Sopenharmony_cibool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
6498c2ecf20Sopenharmony_ci	    spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
6508c2ecf20Sopenharmony_ci		return false;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (spec->match_flags &
6538c2ecf20Sopenharmony_ci	    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
6548c2ecf20Sopenharmony_ci	    is_multicast_ether_addr(spec->loc_mac))
6558c2ecf20Sopenharmony_ci		return true;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	if ((spec->match_flags &
6588c2ecf20Sopenharmony_ci	     (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
6598c2ecf20Sopenharmony_ci	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
6608c2ecf20Sopenharmony_ci		if (spec->ether_type == htons(ETH_P_IP) &&
6618c2ecf20Sopenharmony_ci		    ipv4_is_multicast(spec->loc_host[0]))
6628c2ecf20Sopenharmony_ci			return true;
6638c2ecf20Sopenharmony_ci		if (spec->ether_type == htons(ETH_P_IPV6) &&
6648c2ecf20Sopenharmony_ci		    ((const u8 *)spec->loc_host)[0] == 0xff)
6658c2ecf20Sopenharmony_ci			return true;
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	return false;
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cibool efx_filter_spec_equal(const struct efx_filter_spec *left,
6728c2ecf20Sopenharmony_ci			   const struct efx_filter_spec *right)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	if ((left->match_flags ^ right->match_flags) |
6758c2ecf20Sopenharmony_ci	    ((left->flags ^ right->flags) &
6768c2ecf20Sopenharmony_ci	     (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
6778c2ecf20Sopenharmony_ci		return false;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	return memcmp(&left->vport_id, &right->vport_id,
6808c2ecf20Sopenharmony_ci		      sizeof(struct efx_filter_spec) -
6818c2ecf20Sopenharmony_ci		      offsetof(struct efx_filter_spec, vport_id)) == 0;
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ciu32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct efx_filter_spec, vport_id) & 3);
6878c2ecf20Sopenharmony_ci	return jhash2((const u32 *)&spec->vport_id,
6888c2ecf20Sopenharmony_ci		      (sizeof(struct efx_filter_spec) -
6898c2ecf20Sopenharmony_ci		       offsetof(struct efx_filter_spec, vport_id)) / 4,
6908c2ecf20Sopenharmony_ci		      0);
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
6948c2ecf20Sopenharmony_cibool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
6958c2ecf20Sopenharmony_ci			bool *force)
6968c2ecf20Sopenharmony_ci{
6978c2ecf20Sopenharmony_ci	if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
6988c2ecf20Sopenharmony_ci		/* ARFS is currently updating this entry, leave it */
6998c2ecf20Sopenharmony_ci		return false;
7008c2ecf20Sopenharmony_ci	}
7018c2ecf20Sopenharmony_ci	if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
7028c2ecf20Sopenharmony_ci		/* ARFS tried and failed to update this, so it's probably out
7038c2ecf20Sopenharmony_ci		 * of date.  Remove the filter and the ARFS rule entry.
7048c2ecf20Sopenharmony_ci		 */
7058c2ecf20Sopenharmony_ci		rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
7068c2ecf20Sopenharmony_ci		*force = true;
7078c2ecf20Sopenharmony_ci		return true;
7088c2ecf20Sopenharmony_ci	} else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
7098c2ecf20Sopenharmony_ci		/* ARFS has moved on, so old filter is not needed.  Since we did
7108c2ecf20Sopenharmony_ci		 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
7118c2ecf20Sopenharmony_ci		 * not be removed by efx_rps_hash_del() subsequently.
7128c2ecf20Sopenharmony_ci		 */
7138c2ecf20Sopenharmony_ci		*force = true;
7148c2ecf20Sopenharmony_ci		return true;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci	/* Remove it iff ARFS wants to. */
7178c2ecf20Sopenharmony_ci	return true;
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic
7218c2ecf20Sopenharmony_cistruct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
7228c2ecf20Sopenharmony_ci				       const struct efx_filter_spec *spec)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	u32 hash = efx_filter_spec_hash(spec);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	lockdep_assert_held(&efx->rps_hash_lock);
7278c2ecf20Sopenharmony_ci	if (!efx->rps_hash_table)
7288c2ecf20Sopenharmony_ci		return NULL;
7298c2ecf20Sopenharmony_ci	return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistruct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
7338c2ecf20Sopenharmony_ci					const struct efx_filter_spec *spec)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	struct efx_arfs_rule *rule;
7368c2ecf20Sopenharmony_ci	struct hlist_head *head;
7378c2ecf20Sopenharmony_ci	struct hlist_node *node;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
7408c2ecf20Sopenharmony_ci	if (!head)
7418c2ecf20Sopenharmony_ci		return NULL;
7428c2ecf20Sopenharmony_ci	hlist_for_each(node, head) {
7438c2ecf20Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
7448c2ecf20Sopenharmony_ci		if (efx_filter_spec_equal(spec, &rule->spec))
7458c2ecf20Sopenharmony_ci			return rule;
7468c2ecf20Sopenharmony_ci	}
7478c2ecf20Sopenharmony_ci	return NULL;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistruct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
7518c2ecf20Sopenharmony_ci				       const struct efx_filter_spec *spec,
7528c2ecf20Sopenharmony_ci				       bool *new)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	struct efx_arfs_rule *rule;
7558c2ecf20Sopenharmony_ci	struct hlist_head *head;
7568c2ecf20Sopenharmony_ci	struct hlist_node *node;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
7598c2ecf20Sopenharmony_ci	if (!head)
7608c2ecf20Sopenharmony_ci		return NULL;
7618c2ecf20Sopenharmony_ci	hlist_for_each(node, head) {
7628c2ecf20Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
7638c2ecf20Sopenharmony_ci		if (efx_filter_spec_equal(spec, &rule->spec)) {
7648c2ecf20Sopenharmony_ci			*new = false;
7658c2ecf20Sopenharmony_ci			return rule;
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci	rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
7698c2ecf20Sopenharmony_ci	*new = true;
7708c2ecf20Sopenharmony_ci	if (rule) {
7718c2ecf20Sopenharmony_ci		memcpy(&rule->spec, spec, sizeof(rule->spec));
7728c2ecf20Sopenharmony_ci		hlist_add_head(&rule->node, head);
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci	return rule;
7758c2ecf20Sopenharmony_ci}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_civoid efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	struct efx_arfs_rule *rule;
7808c2ecf20Sopenharmony_ci	struct hlist_head *head;
7818c2ecf20Sopenharmony_ci	struct hlist_node *node;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	head = efx_rps_hash_bucket(efx, spec);
7848c2ecf20Sopenharmony_ci	if (WARN_ON(!head))
7858c2ecf20Sopenharmony_ci		return;
7868c2ecf20Sopenharmony_ci	hlist_for_each(node, head) {
7878c2ecf20Sopenharmony_ci		rule = container_of(node, struct efx_arfs_rule, node);
7888c2ecf20Sopenharmony_ci		if (efx_filter_spec_equal(spec, &rule->spec)) {
7898c2ecf20Sopenharmony_ci			/* Someone already reused the entry.  We know that if
7908c2ecf20Sopenharmony_ci			 * this check doesn't fire (i.e. filter_id == REMOVING)
7918c2ecf20Sopenharmony_ci			 * then the REMOVING mark was put there by our caller,
7928c2ecf20Sopenharmony_ci			 * because caller is holding a lock on filter table and
7938c2ecf20Sopenharmony_ci			 * only holders of that lock set REMOVING.
7948c2ecf20Sopenharmony_ci			 */
7958c2ecf20Sopenharmony_ci			if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
7968c2ecf20Sopenharmony_ci				return;
7978c2ecf20Sopenharmony_ci			hlist_del(node);
7988c2ecf20Sopenharmony_ci			kfree(rule);
7998c2ecf20Sopenharmony_ci			return;
8008c2ecf20Sopenharmony_ci		}
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci	/* We didn't find it. */
8038c2ecf20Sopenharmony_ci	WARN_ON(1);
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci#endif
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ciint efx_probe_filters(struct efx_nic *efx)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	int rc;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	mutex_lock(&efx->mac_lock);
8128c2ecf20Sopenharmony_ci	down_write(&efx->filter_sem);
8138c2ecf20Sopenharmony_ci	rc = efx->type->filter_table_probe(efx);
8148c2ecf20Sopenharmony_ci	if (rc)
8158c2ecf20Sopenharmony_ci		goto out_unlock;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
8188c2ecf20Sopenharmony_ci	if (efx->type->offload_features & NETIF_F_NTUPLE) {
8198c2ecf20Sopenharmony_ci		struct efx_channel *channel;
8208c2ecf20Sopenharmony_ci		int i, success = 1;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci		efx_for_each_channel(channel, efx) {
8238c2ecf20Sopenharmony_ci			channel->rps_flow_id =
8248c2ecf20Sopenharmony_ci				kcalloc(efx->type->max_rx_ip_filters,
8258c2ecf20Sopenharmony_ci					sizeof(*channel->rps_flow_id),
8268c2ecf20Sopenharmony_ci					GFP_KERNEL);
8278c2ecf20Sopenharmony_ci			if (!channel->rps_flow_id)
8288c2ecf20Sopenharmony_ci				success = 0;
8298c2ecf20Sopenharmony_ci			else
8308c2ecf20Sopenharmony_ci				for (i = 0;
8318c2ecf20Sopenharmony_ci				     i < efx->type->max_rx_ip_filters;
8328c2ecf20Sopenharmony_ci				     ++i)
8338c2ecf20Sopenharmony_ci					channel->rps_flow_id[i] =
8348c2ecf20Sopenharmony_ci						RPS_FLOW_ID_INVALID;
8358c2ecf20Sopenharmony_ci			channel->rfs_expire_index = 0;
8368c2ecf20Sopenharmony_ci			channel->rfs_filter_count = 0;
8378c2ecf20Sopenharmony_ci		}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci		if (!success) {
8408c2ecf20Sopenharmony_ci			efx_for_each_channel(channel, efx) {
8418c2ecf20Sopenharmony_ci				kfree(channel->rps_flow_id);
8428c2ecf20Sopenharmony_ci				channel->rps_flow_id = NULL;
8438c2ecf20Sopenharmony_ci			}
8448c2ecf20Sopenharmony_ci			efx->type->filter_table_remove(efx);
8458c2ecf20Sopenharmony_ci			rc = -ENOMEM;
8468c2ecf20Sopenharmony_ci			goto out_unlock;
8478c2ecf20Sopenharmony_ci		}
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci#endif
8508c2ecf20Sopenharmony_ciout_unlock:
8518c2ecf20Sopenharmony_ci	up_write(&efx->filter_sem);
8528c2ecf20Sopenharmony_ci	mutex_unlock(&efx->mac_lock);
8538c2ecf20Sopenharmony_ci	return rc;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_civoid efx_remove_filters(struct efx_nic *efx)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
8598c2ecf20Sopenharmony_ci	struct efx_channel *channel;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	efx_for_each_channel(channel, efx) {
8628c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&channel->filter_work);
8638c2ecf20Sopenharmony_ci		kfree(channel->rps_flow_id);
8648c2ecf20Sopenharmony_ci		channel->rps_flow_id = NULL;
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci#endif
8678c2ecf20Sopenharmony_ci	down_write(&efx->filter_sem);
8688c2ecf20Sopenharmony_ci	efx->type->filter_table_remove(efx);
8698c2ecf20Sopenharmony_ci	up_write(&efx->filter_sem);
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_cistatic void efx_filter_rfs_work(struct work_struct *data)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
8778c2ecf20Sopenharmony_ci							      work);
8788c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(req->net_dev);
8798c2ecf20Sopenharmony_ci	struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
8808c2ecf20Sopenharmony_ci	int slot_idx = req - efx->rps_slot;
8818c2ecf20Sopenharmony_ci	struct efx_arfs_rule *rule;
8828c2ecf20Sopenharmony_ci	u16 arfs_id = 0;
8838c2ecf20Sopenharmony_ci	int rc;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	rc = efx->type->filter_insert(efx, &req->spec, true);
8868c2ecf20Sopenharmony_ci	if (rc >= 0)
8878c2ecf20Sopenharmony_ci		/* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
8888c2ecf20Sopenharmony_ci		rc %= efx->type->max_rx_ip_filters;
8898c2ecf20Sopenharmony_ci	if (efx->rps_hash_table) {
8908c2ecf20Sopenharmony_ci		spin_lock_bh(&efx->rps_hash_lock);
8918c2ecf20Sopenharmony_ci		rule = efx_rps_hash_find(efx, &req->spec);
8928c2ecf20Sopenharmony_ci		/* The rule might have already gone, if someone else's request
8938c2ecf20Sopenharmony_ci		 * for the same spec was already worked and then expired before
8948c2ecf20Sopenharmony_ci		 * we got around to our work.  In that case we have nothing
8958c2ecf20Sopenharmony_ci		 * tying us to an arfs_id, meaning that as soon as the filter
8968c2ecf20Sopenharmony_ci		 * is considered for expiry it will be removed.
8978c2ecf20Sopenharmony_ci		 */
8988c2ecf20Sopenharmony_ci		if (rule) {
8998c2ecf20Sopenharmony_ci			if (rc < 0)
9008c2ecf20Sopenharmony_ci				rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
9018c2ecf20Sopenharmony_ci			else
9028c2ecf20Sopenharmony_ci				rule->filter_id = rc;
9038c2ecf20Sopenharmony_ci			arfs_id = rule->arfs_id;
9048c2ecf20Sopenharmony_ci		}
9058c2ecf20Sopenharmony_ci		spin_unlock_bh(&efx->rps_hash_lock);
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci	if (rc >= 0) {
9088c2ecf20Sopenharmony_ci		/* Remember this so we can check whether to expire the filter
9098c2ecf20Sopenharmony_ci		 * later.
9108c2ecf20Sopenharmony_ci		 */
9118c2ecf20Sopenharmony_ci		mutex_lock(&efx->rps_mutex);
9128c2ecf20Sopenharmony_ci		if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
9138c2ecf20Sopenharmony_ci			channel->rfs_filter_count++;
9148c2ecf20Sopenharmony_ci		channel->rps_flow_id[rc] = req->flow_id;
9158c2ecf20Sopenharmony_ci		mutex_unlock(&efx->rps_mutex);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci		if (req->spec.ether_type == htons(ETH_P_IP))
9188c2ecf20Sopenharmony_ci			netif_info(efx, rx_status, efx->net_dev,
9198c2ecf20Sopenharmony_ci				   "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
9208c2ecf20Sopenharmony_ci				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
9218c2ecf20Sopenharmony_ci				   req->spec.rem_host, ntohs(req->spec.rem_port),
9228c2ecf20Sopenharmony_ci				   req->spec.loc_host, ntohs(req->spec.loc_port),
9238c2ecf20Sopenharmony_ci				   req->rxq_index, req->flow_id, rc, arfs_id);
9248c2ecf20Sopenharmony_ci		else
9258c2ecf20Sopenharmony_ci			netif_info(efx, rx_status, efx->net_dev,
9268c2ecf20Sopenharmony_ci				   "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
9278c2ecf20Sopenharmony_ci				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
9288c2ecf20Sopenharmony_ci				   req->spec.rem_host, ntohs(req->spec.rem_port),
9298c2ecf20Sopenharmony_ci				   req->spec.loc_host, ntohs(req->spec.loc_port),
9308c2ecf20Sopenharmony_ci				   req->rxq_index, req->flow_id, rc, arfs_id);
9318c2ecf20Sopenharmony_ci		channel->n_rfs_succeeded++;
9328c2ecf20Sopenharmony_ci	} else {
9338c2ecf20Sopenharmony_ci		if (req->spec.ether_type == htons(ETH_P_IP))
9348c2ecf20Sopenharmony_ci			netif_dbg(efx, rx_status, efx->net_dev,
9358c2ecf20Sopenharmony_ci				  "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
9368c2ecf20Sopenharmony_ci				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
9378c2ecf20Sopenharmony_ci				  req->spec.rem_host, ntohs(req->spec.rem_port),
9388c2ecf20Sopenharmony_ci				  req->spec.loc_host, ntohs(req->spec.loc_port),
9398c2ecf20Sopenharmony_ci				  req->rxq_index, req->flow_id, rc, arfs_id);
9408c2ecf20Sopenharmony_ci		else
9418c2ecf20Sopenharmony_ci			netif_dbg(efx, rx_status, efx->net_dev,
9428c2ecf20Sopenharmony_ci				  "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
9438c2ecf20Sopenharmony_ci				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
9448c2ecf20Sopenharmony_ci				  req->spec.rem_host, ntohs(req->spec.rem_port),
9458c2ecf20Sopenharmony_ci				  req->spec.loc_host, ntohs(req->spec.loc_port),
9468c2ecf20Sopenharmony_ci				  req->rxq_index, req->flow_id, rc, arfs_id);
9478c2ecf20Sopenharmony_ci		channel->n_rfs_failed++;
9488c2ecf20Sopenharmony_ci		/* We're overloading the NIC's filter tables, so let's do a
9498c2ecf20Sopenharmony_ci		 * chunk of extra expiry work.
9508c2ecf20Sopenharmony_ci		 */
9518c2ecf20Sopenharmony_ci		__efx_filter_rfs_expire(channel, min(channel->rfs_filter_count,
9528c2ecf20Sopenharmony_ci						     100u));
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	/* Release references */
9568c2ecf20Sopenharmony_ci	clear_bit(slot_idx, &efx->rps_slot_map);
9578c2ecf20Sopenharmony_ci	dev_put(req->net_dev);
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ciint efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
9618c2ecf20Sopenharmony_ci		   u16 rxq_index, u32 flow_id)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	struct efx_nic *efx = netdev_priv(net_dev);
9648c2ecf20Sopenharmony_ci	struct efx_async_filter_insertion *req;
9658c2ecf20Sopenharmony_ci	struct efx_arfs_rule *rule;
9668c2ecf20Sopenharmony_ci	struct flow_keys fk;
9678c2ecf20Sopenharmony_ci	int slot_idx;
9688c2ecf20Sopenharmony_ci	bool new;
9698c2ecf20Sopenharmony_ci	int rc;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	/* find a free slot */
9728c2ecf20Sopenharmony_ci	for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
9738c2ecf20Sopenharmony_ci		if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
9748c2ecf20Sopenharmony_ci			break;
9758c2ecf20Sopenharmony_ci	if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
9768c2ecf20Sopenharmony_ci		return -EBUSY;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	if (flow_id == RPS_FLOW_ID_INVALID) {
9798c2ecf20Sopenharmony_ci		rc = -EINVAL;
9808c2ecf20Sopenharmony_ci		goto out_clear;
9818c2ecf20Sopenharmony_ci	}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
9848c2ecf20Sopenharmony_ci		rc = -EPROTONOSUPPORT;
9858c2ecf20Sopenharmony_ci		goto out_clear;
9868c2ecf20Sopenharmony_ci	}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
9898c2ecf20Sopenharmony_ci		rc = -EPROTONOSUPPORT;
9908c2ecf20Sopenharmony_ci		goto out_clear;
9918c2ecf20Sopenharmony_ci	}
9928c2ecf20Sopenharmony_ci	if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
9938c2ecf20Sopenharmony_ci		rc = -EPROTONOSUPPORT;
9948c2ecf20Sopenharmony_ci		goto out_clear;
9958c2ecf20Sopenharmony_ci	}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	req = efx->rps_slot + slot_idx;
9988c2ecf20Sopenharmony_ci	efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
9998c2ecf20Sopenharmony_ci			   efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
10008c2ecf20Sopenharmony_ci			   rxq_index);
10018c2ecf20Sopenharmony_ci	req->spec.match_flags =
10028c2ecf20Sopenharmony_ci		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
10038c2ecf20Sopenharmony_ci		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
10048c2ecf20Sopenharmony_ci		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
10058c2ecf20Sopenharmony_ci	req->spec.ether_type = fk.basic.n_proto;
10068c2ecf20Sopenharmony_ci	req->spec.ip_proto = fk.basic.ip_proto;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	if (fk.basic.n_proto == htons(ETH_P_IP)) {
10098c2ecf20Sopenharmony_ci		req->spec.rem_host[0] = fk.addrs.v4addrs.src;
10108c2ecf20Sopenharmony_ci		req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
10118c2ecf20Sopenharmony_ci	} else {
10128c2ecf20Sopenharmony_ci		memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
10138c2ecf20Sopenharmony_ci		       sizeof(struct in6_addr));
10148c2ecf20Sopenharmony_ci		memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
10158c2ecf20Sopenharmony_ci		       sizeof(struct in6_addr));
10168c2ecf20Sopenharmony_ci	}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	req->spec.rem_port = fk.ports.src;
10198c2ecf20Sopenharmony_ci	req->spec.loc_port = fk.ports.dst;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (efx->rps_hash_table) {
10228c2ecf20Sopenharmony_ci		/* Add it to ARFS hash table */
10238c2ecf20Sopenharmony_ci		spin_lock(&efx->rps_hash_lock);
10248c2ecf20Sopenharmony_ci		rule = efx_rps_hash_add(efx, &req->spec, &new);
10258c2ecf20Sopenharmony_ci		if (!rule) {
10268c2ecf20Sopenharmony_ci			rc = -ENOMEM;
10278c2ecf20Sopenharmony_ci			goto out_unlock;
10288c2ecf20Sopenharmony_ci		}
10298c2ecf20Sopenharmony_ci		if (new)
10308c2ecf20Sopenharmony_ci			rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
10318c2ecf20Sopenharmony_ci		rc = rule->arfs_id;
10328c2ecf20Sopenharmony_ci		/* Skip if existing or pending filter already does the right thing */
10338c2ecf20Sopenharmony_ci		if (!new && rule->rxq_index == rxq_index &&
10348c2ecf20Sopenharmony_ci		    rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
10358c2ecf20Sopenharmony_ci			goto out_unlock;
10368c2ecf20Sopenharmony_ci		rule->rxq_index = rxq_index;
10378c2ecf20Sopenharmony_ci		rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
10388c2ecf20Sopenharmony_ci		spin_unlock(&efx->rps_hash_lock);
10398c2ecf20Sopenharmony_ci	} else {
10408c2ecf20Sopenharmony_ci		/* Without an ARFS hash table, we just use arfs_id 0 for all
10418c2ecf20Sopenharmony_ci		 * filters.  This means if multiple flows hash to the same
10428c2ecf20Sopenharmony_ci		 * flow_id, all but the most recently touched will be eligible
10438c2ecf20Sopenharmony_ci		 * for expiry.
10448c2ecf20Sopenharmony_ci		 */
10458c2ecf20Sopenharmony_ci		rc = 0;
10468c2ecf20Sopenharmony_ci	}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	/* Queue the request */
10498c2ecf20Sopenharmony_ci	dev_hold(req->net_dev = net_dev);
10508c2ecf20Sopenharmony_ci	INIT_WORK(&req->work, efx_filter_rfs_work);
10518c2ecf20Sopenharmony_ci	req->rxq_index = rxq_index;
10528c2ecf20Sopenharmony_ci	req->flow_id = flow_id;
10538c2ecf20Sopenharmony_ci	schedule_work(&req->work);
10548c2ecf20Sopenharmony_ci	return rc;
10558c2ecf20Sopenharmony_ciout_unlock:
10568c2ecf20Sopenharmony_ci	spin_unlock(&efx->rps_hash_lock);
10578c2ecf20Sopenharmony_ciout_clear:
10588c2ecf20Sopenharmony_ci	clear_bit(slot_idx, &efx->rps_slot_map);
10598c2ecf20Sopenharmony_ci	return rc;
10608c2ecf20Sopenharmony_ci}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_cibool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota)
10638c2ecf20Sopenharmony_ci{
10648c2ecf20Sopenharmony_ci	bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
10658c2ecf20Sopenharmony_ci	struct efx_nic *efx = channel->efx;
10668c2ecf20Sopenharmony_ci	unsigned int index, size, start;
10678c2ecf20Sopenharmony_ci	u32 flow_id;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	if (!mutex_trylock(&efx->rps_mutex))
10708c2ecf20Sopenharmony_ci		return false;
10718c2ecf20Sopenharmony_ci	expire_one = efx->type->filter_rfs_expire_one;
10728c2ecf20Sopenharmony_ci	index = channel->rfs_expire_index;
10738c2ecf20Sopenharmony_ci	start = index;
10748c2ecf20Sopenharmony_ci	size = efx->type->max_rx_ip_filters;
10758c2ecf20Sopenharmony_ci	while (quota) {
10768c2ecf20Sopenharmony_ci		flow_id = channel->rps_flow_id[index];
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci		if (flow_id != RPS_FLOW_ID_INVALID) {
10798c2ecf20Sopenharmony_ci			quota--;
10808c2ecf20Sopenharmony_ci			if (expire_one(efx, flow_id, index)) {
10818c2ecf20Sopenharmony_ci				netif_info(efx, rx_status, efx->net_dev,
10828c2ecf20Sopenharmony_ci					   "expired filter %d [channel %u flow %u]\n",
10838c2ecf20Sopenharmony_ci					   index, channel->channel, flow_id);
10848c2ecf20Sopenharmony_ci				channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
10858c2ecf20Sopenharmony_ci				channel->rfs_filter_count--;
10868c2ecf20Sopenharmony_ci			}
10878c2ecf20Sopenharmony_ci		}
10888c2ecf20Sopenharmony_ci		if (++index == size)
10898c2ecf20Sopenharmony_ci			index = 0;
10908c2ecf20Sopenharmony_ci		/* If we were called with a quota that exceeds the total number
10918c2ecf20Sopenharmony_ci		 * of filters in the table (which shouldn't happen, but could
10928c2ecf20Sopenharmony_ci		 * if two callers race), ensure that we don't loop forever -
10938c2ecf20Sopenharmony_ci		 * stop when we've examined every row of the table.
10948c2ecf20Sopenharmony_ci		 */
10958c2ecf20Sopenharmony_ci		if (index == start)
10968c2ecf20Sopenharmony_ci			break;
10978c2ecf20Sopenharmony_ci	}
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	channel->rfs_expire_index = index;
11008c2ecf20Sopenharmony_ci	mutex_unlock(&efx->rps_mutex);
11018c2ecf20Sopenharmony_ci	return true;
11028c2ecf20Sopenharmony_ci}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci#endif /* CONFIG_RFS_ACCEL */
1105