xref: /linux/drivers/net/ethernet/sfc/rx_common.c (revision bdf1b5c3)
11751cc36SAlex Maftei (amaftei) // SPDX-License-Identifier: GPL-2.0-only
21751cc36SAlex Maftei (amaftei) /****************************************************************************
31751cc36SAlex Maftei (amaftei)  * Driver for Solarflare network controllers and boards
41751cc36SAlex Maftei (amaftei)  * Copyright 2018 Solarflare Communications Inc.
51751cc36SAlex Maftei (amaftei)  *
61751cc36SAlex Maftei (amaftei)  * This program is free software; you can redistribute it and/or modify it
71751cc36SAlex Maftei (amaftei)  * under the terms of the GNU General Public License version 2 as published
81751cc36SAlex Maftei (amaftei)  * by the Free Software Foundation, incorporated herein by reference.
91751cc36SAlex Maftei (amaftei)  */
101751cc36SAlex Maftei (amaftei) 
111751cc36SAlex Maftei (amaftei) #include "net_driver.h"
121751cc36SAlex Maftei (amaftei) #include <linux/module.h>
133d95b884SAlex Maftei (amaftei) #include <linux/iommu.h>
141751cc36SAlex Maftei (amaftei) #include "efx.h"
151751cc36SAlex Maftei (amaftei) #include "nic.h"
161751cc36SAlex Maftei (amaftei) #include "rx_common.h"
171751cc36SAlex Maftei (amaftei) 
181751cc36SAlex Maftei (amaftei) /* This is the percentage fill level below which new RX descriptors
191751cc36SAlex Maftei (amaftei)  * will be added to the RX descriptor ring.
201751cc36SAlex Maftei (amaftei)  */
211751cc36SAlex Maftei (amaftei) static unsigned int rx_refill_threshold;
221751cc36SAlex Maftei (amaftei) module_param(rx_refill_threshold, uint, 0444);
231751cc36SAlex Maftei (amaftei) MODULE_PARM_DESC(rx_refill_threshold,
241751cc36SAlex Maftei (amaftei) 		 "RX descriptor ring refill threshold (%)");
251751cc36SAlex Maftei (amaftei) 
263d95b884SAlex Maftei (amaftei) /* Number of RX buffers to recycle pages for.  When creating the RX page recycle
273d95b884SAlex Maftei (amaftei)  * ring, this number is divided by the number of buffers per page to calculate
283d95b884SAlex Maftei (amaftei)  * the number of pages to store in the RX page recycle ring.
293d95b884SAlex Maftei (amaftei)  */
303d95b884SAlex Maftei (amaftei) #define EFX_RECYCLE_RING_SIZE_IOMMU 4096
313d95b884SAlex Maftei (amaftei) #define EFX_RECYCLE_RING_SIZE_NOIOMMU (2 * EFX_RX_PREFERRED_BATCH)
323d95b884SAlex Maftei (amaftei) 
331751cc36SAlex Maftei (amaftei) /* RX maximum head room required.
341751cc36SAlex Maftei (amaftei)  *
351751cc36SAlex Maftei (amaftei)  * This must be at least 1 to prevent overflow, plus one packet-worth
361751cc36SAlex Maftei (amaftei)  * to allow pipelined receives.
371751cc36SAlex Maftei (amaftei)  */
381751cc36SAlex Maftei (amaftei) #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
391751cc36SAlex Maftei (amaftei) 
403d95b884SAlex Maftei (amaftei) /* Check the RX page recycle ring for a page that can be reused. */
413d95b884SAlex Maftei (amaftei) static struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
423d95b884SAlex Maftei (amaftei) {
433d95b884SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
443d95b884SAlex Maftei (amaftei) 	struct efx_rx_page_state *state;
453d95b884SAlex Maftei (amaftei) 	unsigned int index;
463d95b884SAlex Maftei (amaftei) 	struct page *page;
473d95b884SAlex Maftei (amaftei) 
483d95b884SAlex Maftei (amaftei) 	index = rx_queue->page_remove & rx_queue->page_ptr_mask;
493d95b884SAlex Maftei (amaftei) 	page = rx_queue->page_ring[index];
503d95b884SAlex Maftei (amaftei) 	if (page == NULL)
513d95b884SAlex Maftei (amaftei) 		return NULL;
523d95b884SAlex Maftei (amaftei) 
533d95b884SAlex Maftei (amaftei) 	rx_queue->page_ring[index] = NULL;
543d95b884SAlex Maftei (amaftei) 	/* page_remove cannot exceed page_add. */
553d95b884SAlex Maftei (amaftei) 	if (rx_queue->page_remove != rx_queue->page_add)
563d95b884SAlex Maftei (amaftei) 		++rx_queue->page_remove;
573d95b884SAlex Maftei (amaftei) 
583d95b884SAlex Maftei (amaftei) 	/* If page_count is 1 then we hold the only reference to this page. */
593d95b884SAlex Maftei (amaftei) 	if (page_count(page) == 1) {
603d95b884SAlex Maftei (amaftei) 		++rx_queue->page_recycle_count;
613d95b884SAlex Maftei (amaftei) 		return page;
623d95b884SAlex Maftei (amaftei) 	} else {
633d95b884SAlex Maftei (amaftei) 		state = page_address(page);
643d95b884SAlex Maftei (amaftei) 		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
653d95b884SAlex Maftei (amaftei) 			       PAGE_SIZE << efx->rx_buffer_order,
663d95b884SAlex Maftei (amaftei) 			       DMA_FROM_DEVICE);
673d95b884SAlex Maftei (amaftei) 		put_page(page);
683d95b884SAlex Maftei (amaftei) 		++rx_queue->page_recycle_failed;
693d95b884SAlex Maftei (amaftei) 	}
703d95b884SAlex Maftei (amaftei) 
713d95b884SAlex Maftei (amaftei) 	return NULL;
723d95b884SAlex Maftei (amaftei) }
733d95b884SAlex Maftei (amaftei) 
743d95b884SAlex Maftei (amaftei) /* Attempt to recycle the page if there is an RX recycle ring; the page can
753d95b884SAlex Maftei (amaftei)  * only be added if this is the final RX buffer, to prevent pages being used in
763d95b884SAlex Maftei (amaftei)  * the descriptor ring and appearing in the recycle ring simultaneously.
773d95b884SAlex Maftei (amaftei)  */
783d95b884SAlex Maftei (amaftei) static void efx_recycle_rx_page(struct efx_channel *channel,
793d95b884SAlex Maftei (amaftei) 				struct efx_rx_buffer *rx_buf)
803d95b884SAlex Maftei (amaftei) {
813d95b884SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
823d95b884SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
833d95b884SAlex Maftei (amaftei) 	struct page *page = rx_buf->page;
843d95b884SAlex Maftei (amaftei) 	unsigned int index;
853d95b884SAlex Maftei (amaftei) 
863d95b884SAlex Maftei (amaftei) 	/* Only recycle the page after processing the final buffer. */
873d95b884SAlex Maftei (amaftei) 	if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
883d95b884SAlex Maftei (amaftei) 		return;
893d95b884SAlex Maftei (amaftei) 
903d95b884SAlex Maftei (amaftei) 	index = rx_queue->page_add & rx_queue->page_ptr_mask;
913d95b884SAlex Maftei (amaftei) 	if (rx_queue->page_ring[index] == NULL) {
923d95b884SAlex Maftei (amaftei) 		unsigned int read_index = rx_queue->page_remove &
933d95b884SAlex Maftei (amaftei) 			rx_queue->page_ptr_mask;
943d95b884SAlex Maftei (amaftei) 
953d95b884SAlex Maftei (amaftei) 		/* The next slot in the recycle ring is available, but
963d95b884SAlex Maftei (amaftei) 		 * increment page_remove if the read pointer currently
973d95b884SAlex Maftei (amaftei) 		 * points here.
983d95b884SAlex Maftei (amaftei) 		 */
993d95b884SAlex Maftei (amaftei) 		if (read_index == index)
1003d95b884SAlex Maftei (amaftei) 			++rx_queue->page_remove;
1013d95b884SAlex Maftei (amaftei) 		rx_queue->page_ring[index] = page;
1023d95b884SAlex Maftei (amaftei) 		++rx_queue->page_add;
1033d95b884SAlex Maftei (amaftei) 		return;
1043d95b884SAlex Maftei (amaftei) 	}
1053d95b884SAlex Maftei (amaftei) 	++rx_queue->page_recycle_full;
1063d95b884SAlex Maftei (amaftei) 	efx_unmap_rx_buffer(efx, rx_buf);
1073d95b884SAlex Maftei (amaftei) 	put_page(rx_buf->page);
1083d95b884SAlex Maftei (amaftei) }
1093d95b884SAlex Maftei (amaftei) 
1103d95b884SAlex Maftei (amaftei) /* Recycle the pages that are used by buffers that have just been received. */
1113d95b884SAlex Maftei (amaftei) void efx_recycle_rx_pages(struct efx_channel *channel,
1123d95b884SAlex Maftei (amaftei) 			  struct efx_rx_buffer *rx_buf,
1133d95b884SAlex Maftei (amaftei) 			  unsigned int n_frags)
1143d95b884SAlex Maftei (amaftei) {
1153d95b884SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
1163d95b884SAlex Maftei (amaftei) 
1173d95b884SAlex Maftei (amaftei) 	do {
1183d95b884SAlex Maftei (amaftei) 		efx_recycle_rx_page(channel, rx_buf);
1193d95b884SAlex Maftei (amaftei) 		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
1203d95b884SAlex Maftei (amaftei) 	} while (--n_frags);
1213d95b884SAlex Maftei (amaftei) }
1223d95b884SAlex Maftei (amaftei) 
1233d95b884SAlex Maftei (amaftei) void efx_discard_rx_packet(struct efx_channel *channel,
1243d95b884SAlex Maftei (amaftei) 			   struct efx_rx_buffer *rx_buf,
1253d95b884SAlex Maftei (amaftei) 			   unsigned int n_frags)
1263d95b884SAlex Maftei (amaftei) {
1273d95b884SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
1283d95b884SAlex Maftei (amaftei) 
1293d95b884SAlex Maftei (amaftei) 	efx_recycle_rx_pages(channel, rx_buf, n_frags);
1303d95b884SAlex Maftei (amaftei) 
1313d95b884SAlex Maftei (amaftei) 	efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
1323d95b884SAlex Maftei (amaftei) }
1333d95b884SAlex Maftei (amaftei) 
1343d95b884SAlex Maftei (amaftei) static void efx_init_rx_recycle_ring(struct efx_rx_queue *rx_queue)
1353d95b884SAlex Maftei (amaftei) {
1363d95b884SAlex Maftei (amaftei) 	unsigned int bufs_in_recycle_ring, page_ring_size;
1373d95b884SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
1383d95b884SAlex Maftei (amaftei) 
1393d95b884SAlex Maftei (amaftei) 	/* Set the RX recycle ring size */
1403d95b884SAlex Maftei (amaftei) #ifdef CONFIG_PPC64
1413d95b884SAlex Maftei (amaftei) 	bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
1423d95b884SAlex Maftei (amaftei) #else
1433d95b884SAlex Maftei (amaftei) 	if (iommu_present(&pci_bus_type))
1443d95b884SAlex Maftei (amaftei) 		bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
1453d95b884SAlex Maftei (amaftei) 	else
1463d95b884SAlex Maftei (amaftei) 		bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_NOIOMMU;
1473d95b884SAlex Maftei (amaftei) #endif /* CONFIG_PPC64 */
1483d95b884SAlex Maftei (amaftei) 
1493d95b884SAlex Maftei (amaftei) 	page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
1503d95b884SAlex Maftei (amaftei) 					    efx->rx_bufs_per_page);
1513d95b884SAlex Maftei (amaftei) 	rx_queue->page_ring = kcalloc(page_ring_size,
1523d95b884SAlex Maftei (amaftei) 				      sizeof(*rx_queue->page_ring), GFP_KERNEL);
153*bdf1b5c3SJiasheng Jiang 	if (!rx_queue->page_ring)
154*bdf1b5c3SJiasheng Jiang 		rx_queue->page_ptr_mask = 0;
155*bdf1b5c3SJiasheng Jiang 	else
1563d95b884SAlex Maftei (amaftei) 		rx_queue->page_ptr_mask = page_ring_size - 1;
1573d95b884SAlex Maftei (amaftei) }
1583d95b884SAlex Maftei (amaftei) 
1593d95b884SAlex Maftei (amaftei) static void efx_fini_rx_recycle_ring(struct efx_rx_queue *rx_queue)
1603d95b884SAlex Maftei (amaftei) {
1613d95b884SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
1623d95b884SAlex Maftei (amaftei) 	int i;
1633d95b884SAlex Maftei (amaftei) 
1643d95b884SAlex Maftei (amaftei) 	/* Unmap and release the pages in the recycle ring. Remove the ring. */
1653d95b884SAlex Maftei (amaftei) 	for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
1663d95b884SAlex Maftei (amaftei) 		struct page *page = rx_queue->page_ring[i];
1673d95b884SAlex Maftei (amaftei) 		struct efx_rx_page_state *state;
1683d95b884SAlex Maftei (amaftei) 
1693d95b884SAlex Maftei (amaftei) 		if (page == NULL)
1703d95b884SAlex Maftei (amaftei) 			continue;
1713d95b884SAlex Maftei (amaftei) 
1723d95b884SAlex Maftei (amaftei) 		state = page_address(page);
1733d95b884SAlex Maftei (amaftei) 		dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
1743d95b884SAlex Maftei (amaftei) 			       PAGE_SIZE << efx->rx_buffer_order,
1753d95b884SAlex Maftei (amaftei) 			       DMA_FROM_DEVICE);
1763d95b884SAlex Maftei (amaftei) 		put_page(page);
1773d95b884SAlex Maftei (amaftei) 	}
1783d95b884SAlex Maftei (amaftei) 	kfree(rx_queue->page_ring);
1793d95b884SAlex Maftei (amaftei) 	rx_queue->page_ring = NULL;
1803d95b884SAlex Maftei (amaftei) }
1813d95b884SAlex Maftei (amaftei) 
1821751cc36SAlex Maftei (amaftei) static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
1831751cc36SAlex Maftei (amaftei) 			       struct efx_rx_buffer *rx_buf)
1841751cc36SAlex Maftei (amaftei) {
1851751cc36SAlex Maftei (amaftei) 	/* Release the page reference we hold for the buffer. */
1861751cc36SAlex Maftei (amaftei) 	if (rx_buf->page)
1871751cc36SAlex Maftei (amaftei) 		put_page(rx_buf->page);
1881751cc36SAlex Maftei (amaftei) 
1891751cc36SAlex Maftei (amaftei) 	/* If this is the last buffer in a page, unmap and free it. */
1901751cc36SAlex Maftei (amaftei) 	if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
1911751cc36SAlex Maftei (amaftei) 		efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
1921751cc36SAlex Maftei (amaftei) 		efx_free_rx_buffers(rx_queue, rx_buf, 1);
1931751cc36SAlex Maftei (amaftei) 	}
1941751cc36SAlex Maftei (amaftei) 	rx_buf->page = NULL;
1951751cc36SAlex Maftei (amaftei) }
1961751cc36SAlex Maftei (amaftei) 
1971751cc36SAlex Maftei (amaftei) int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
1981751cc36SAlex Maftei (amaftei) {
1991751cc36SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
2001751cc36SAlex Maftei (amaftei) 	unsigned int entries;
2011751cc36SAlex Maftei (amaftei) 	int rc;
2021751cc36SAlex Maftei (amaftei) 
2031751cc36SAlex Maftei (amaftei) 	/* Create the smallest power-of-two aligned ring */
2041751cc36SAlex Maftei (amaftei) 	entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
2051751cc36SAlex Maftei (amaftei) 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
2061751cc36SAlex Maftei (amaftei) 	rx_queue->ptr_mask = entries - 1;
2071751cc36SAlex Maftei (amaftei) 
2081751cc36SAlex Maftei (amaftei) 	netif_dbg(efx, probe, efx->net_dev,
2091751cc36SAlex Maftei (amaftei) 		  "creating RX queue %d size %#x mask %#x\n",
2101751cc36SAlex Maftei (amaftei) 		  efx_rx_queue_index(rx_queue), efx->rxq_entries,
2111751cc36SAlex Maftei (amaftei) 		  rx_queue->ptr_mask);
2121751cc36SAlex Maftei (amaftei) 
2131751cc36SAlex Maftei (amaftei) 	/* Allocate RX buffers */
2141751cc36SAlex Maftei (amaftei) 	rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
2151751cc36SAlex Maftei (amaftei) 				   GFP_KERNEL);
2161751cc36SAlex Maftei (amaftei) 	if (!rx_queue->buffer)
2171751cc36SAlex Maftei (amaftei) 		return -ENOMEM;
2181751cc36SAlex Maftei (amaftei) 
2191751cc36SAlex Maftei (amaftei) 	rc = efx_nic_probe_rx(rx_queue);
2201751cc36SAlex Maftei (amaftei) 	if (rc) {
2211751cc36SAlex Maftei (amaftei) 		kfree(rx_queue->buffer);
2221751cc36SAlex Maftei (amaftei) 		rx_queue->buffer = NULL;
2231751cc36SAlex Maftei (amaftei) 	}
2241751cc36SAlex Maftei (amaftei) 
2251751cc36SAlex Maftei (amaftei) 	return rc;
2261751cc36SAlex Maftei (amaftei) }
2271751cc36SAlex Maftei (amaftei) 
2281751cc36SAlex Maftei (amaftei) void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
2291751cc36SAlex Maftei (amaftei) {
2301751cc36SAlex Maftei (amaftei) 	unsigned int max_fill, trigger, max_trigger;
2311751cc36SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
2321751cc36SAlex Maftei (amaftei) 	int rc = 0;
2331751cc36SAlex Maftei (amaftei) 
2341751cc36SAlex Maftei (amaftei) 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
2351751cc36SAlex Maftei (amaftei) 		  "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
2361751cc36SAlex Maftei (amaftei) 
2371751cc36SAlex Maftei (amaftei) 	/* Initialise ptr fields */
2381751cc36SAlex Maftei (amaftei) 	rx_queue->added_count = 0;
2391751cc36SAlex Maftei (amaftei) 	rx_queue->notified_count = 0;
2401751cc36SAlex Maftei (amaftei) 	rx_queue->removed_count = 0;
2411751cc36SAlex Maftei (amaftei) 	rx_queue->min_fill = -1U;
2421751cc36SAlex Maftei (amaftei) 	efx_init_rx_recycle_ring(rx_queue);
2431751cc36SAlex Maftei (amaftei) 
2441751cc36SAlex Maftei (amaftei) 	rx_queue->page_remove = 0;
2451751cc36SAlex Maftei (amaftei) 	rx_queue->page_add = rx_queue->page_ptr_mask + 1;
2461751cc36SAlex Maftei (amaftei) 	rx_queue->page_recycle_count = 0;
2471751cc36SAlex Maftei (amaftei) 	rx_queue->page_recycle_failed = 0;
2481751cc36SAlex Maftei (amaftei) 	rx_queue->page_recycle_full = 0;
2491751cc36SAlex Maftei (amaftei) 
2501751cc36SAlex Maftei (amaftei) 	/* Initialise limit fields */
2511751cc36SAlex Maftei (amaftei) 	max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
2521751cc36SAlex Maftei (amaftei) 	max_trigger =
2531751cc36SAlex Maftei (amaftei) 		max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
2541751cc36SAlex Maftei (amaftei) 	if (rx_refill_threshold != 0) {
2551751cc36SAlex Maftei (amaftei) 		trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
2561751cc36SAlex Maftei (amaftei) 		if (trigger > max_trigger)
2571751cc36SAlex Maftei (amaftei) 			trigger = max_trigger;
2581751cc36SAlex Maftei (amaftei) 	} else {
2591751cc36SAlex Maftei (amaftei) 		trigger = max_trigger;
2601751cc36SAlex Maftei (amaftei) 	}
2611751cc36SAlex Maftei (amaftei) 
2621751cc36SAlex Maftei (amaftei) 	rx_queue->max_fill = max_fill;
2631751cc36SAlex Maftei (amaftei) 	rx_queue->fast_fill_trigger = trigger;
2641751cc36SAlex Maftei (amaftei) 	rx_queue->refill_enabled = true;
2651751cc36SAlex Maftei (amaftei) 
2661751cc36SAlex Maftei (amaftei) 	/* Initialise XDP queue information */
2671751cc36SAlex Maftei (amaftei) 	rc = xdp_rxq_info_reg(&rx_queue->xdp_rxq_info, efx->net_dev,
268b02e5a0eSBjörn Töpel 			      rx_queue->core_index, 0);
2691751cc36SAlex Maftei (amaftei) 
2701751cc36SAlex Maftei (amaftei) 	if (rc) {
2711751cc36SAlex Maftei (amaftei) 		netif_err(efx, rx_err, efx->net_dev,
2721751cc36SAlex Maftei (amaftei) 			  "Failure to initialise XDP queue information rc=%d\n",
2731751cc36SAlex Maftei (amaftei) 			  rc);
2741751cc36SAlex Maftei (amaftei) 		efx->xdp_rxq_info_failed = true;
2751751cc36SAlex Maftei (amaftei) 	} else {
2761751cc36SAlex Maftei (amaftei) 		rx_queue->xdp_rxq_info_valid = true;
2771751cc36SAlex Maftei (amaftei) 	}
2781751cc36SAlex Maftei (amaftei) 
2791751cc36SAlex Maftei (amaftei) 	/* Set up RX descriptor ring */
2801751cc36SAlex Maftei (amaftei) 	efx_nic_init_rx(rx_queue);
2811751cc36SAlex Maftei (amaftei) }
2821751cc36SAlex Maftei (amaftei) 
2831751cc36SAlex Maftei (amaftei) void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
2841751cc36SAlex Maftei (amaftei) {
2851751cc36SAlex Maftei (amaftei) 	struct efx_rx_buffer *rx_buf;
2861751cc36SAlex Maftei (amaftei) 	int i;
2871751cc36SAlex Maftei (amaftei) 
2881751cc36SAlex Maftei (amaftei) 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
2891751cc36SAlex Maftei (amaftei) 		  "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
2901751cc36SAlex Maftei (amaftei) 
2911751cc36SAlex Maftei (amaftei) 	del_timer_sync(&rx_queue->slow_fill);
2921751cc36SAlex Maftei (amaftei) 
2931751cc36SAlex Maftei (amaftei) 	/* Release RX buffers from the current read ptr to the write ptr */
2941751cc36SAlex Maftei (amaftei) 	if (rx_queue->buffer) {
2951751cc36SAlex Maftei (amaftei) 		for (i = rx_queue->removed_count; i < rx_queue->added_count;
2961751cc36SAlex Maftei (amaftei) 		     i++) {
2971751cc36SAlex Maftei (amaftei) 			unsigned int index = i & rx_queue->ptr_mask;
2981751cc36SAlex Maftei (amaftei) 
2991751cc36SAlex Maftei (amaftei) 			rx_buf = efx_rx_buffer(rx_queue, index);
3001751cc36SAlex Maftei (amaftei) 			efx_fini_rx_buffer(rx_queue, rx_buf);
3011751cc36SAlex Maftei (amaftei) 		}
3021751cc36SAlex Maftei (amaftei) 	}
3031751cc36SAlex Maftei (amaftei) 
3043d95b884SAlex Maftei (amaftei) 	efx_fini_rx_recycle_ring(rx_queue);
3051751cc36SAlex Maftei (amaftei) 
3061751cc36SAlex Maftei (amaftei) 	if (rx_queue->xdp_rxq_info_valid)
3071751cc36SAlex Maftei (amaftei) 		xdp_rxq_info_unreg(&rx_queue->xdp_rxq_info);
3081751cc36SAlex Maftei (amaftei) 
3091751cc36SAlex Maftei (amaftei) 	rx_queue->xdp_rxq_info_valid = false;
3101751cc36SAlex Maftei (amaftei) }
3111751cc36SAlex Maftei (amaftei) 
3121751cc36SAlex Maftei (amaftei) void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
3131751cc36SAlex Maftei (amaftei) {
3141751cc36SAlex Maftei (amaftei) 	netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
3151751cc36SAlex Maftei (amaftei) 		  "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
3161751cc36SAlex Maftei (amaftei) 
3171751cc36SAlex Maftei (amaftei) 	efx_nic_remove_rx(rx_queue);
3181751cc36SAlex Maftei (amaftei) 
3191751cc36SAlex Maftei (amaftei) 	kfree(rx_queue->buffer);
3201751cc36SAlex Maftei (amaftei) 	rx_queue->buffer = NULL;
3211751cc36SAlex Maftei (amaftei) }
3221751cc36SAlex Maftei (amaftei) 
3231751cc36SAlex Maftei (amaftei) /* Unmap a DMA-mapped page.  This function is only called for the final RX
3241751cc36SAlex Maftei (amaftei)  * buffer in a page.
3251751cc36SAlex Maftei (amaftei)  */
3261751cc36SAlex Maftei (amaftei) void efx_unmap_rx_buffer(struct efx_nic *efx,
3271751cc36SAlex Maftei (amaftei) 			 struct efx_rx_buffer *rx_buf)
3281751cc36SAlex Maftei (amaftei) {
3291751cc36SAlex Maftei (amaftei) 	struct page *page = rx_buf->page;
3301751cc36SAlex Maftei (amaftei) 
3311751cc36SAlex Maftei (amaftei) 	if (page) {
3321751cc36SAlex Maftei (amaftei) 		struct efx_rx_page_state *state = page_address(page);
3331751cc36SAlex Maftei (amaftei) 
3341751cc36SAlex Maftei (amaftei) 		dma_unmap_page(&efx->pci_dev->dev,
3351751cc36SAlex Maftei (amaftei) 			       state->dma_addr,
3361751cc36SAlex Maftei (amaftei) 			       PAGE_SIZE << efx->rx_buffer_order,
3371751cc36SAlex Maftei (amaftei) 			       DMA_FROM_DEVICE);
3381751cc36SAlex Maftei (amaftei) 	}
3391751cc36SAlex Maftei (amaftei) }
3401751cc36SAlex Maftei (amaftei) 
3411751cc36SAlex Maftei (amaftei) void efx_free_rx_buffers(struct efx_rx_queue *rx_queue,
3421751cc36SAlex Maftei (amaftei) 			 struct efx_rx_buffer *rx_buf,
3431751cc36SAlex Maftei (amaftei) 			 unsigned int num_bufs)
3441751cc36SAlex Maftei (amaftei) {
3451751cc36SAlex Maftei (amaftei) 	do {
3461751cc36SAlex Maftei (amaftei) 		if (rx_buf->page) {
3471751cc36SAlex Maftei (amaftei) 			put_page(rx_buf->page);
3481751cc36SAlex Maftei (amaftei) 			rx_buf->page = NULL;
3491751cc36SAlex Maftei (amaftei) 		}
3501751cc36SAlex Maftei (amaftei) 		rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
3511751cc36SAlex Maftei (amaftei) 	} while (--num_bufs);
3521751cc36SAlex Maftei (amaftei) }
3531751cc36SAlex Maftei (amaftei) 
3541751cc36SAlex Maftei (amaftei) void efx_rx_slow_fill(struct timer_list *t)
3551751cc36SAlex Maftei (amaftei) {
3561751cc36SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
3571751cc36SAlex Maftei (amaftei) 
3581751cc36SAlex Maftei (amaftei) 	/* Post an event to cause NAPI to run and refill the queue */
3591751cc36SAlex Maftei (amaftei) 	efx_nic_generate_fill_event(rx_queue);
3601751cc36SAlex Maftei (amaftei) 	++rx_queue->slow_fill_count;
3611751cc36SAlex Maftei (amaftei) }
3621751cc36SAlex Maftei (amaftei) 
3631751cc36SAlex Maftei (amaftei) void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
3641751cc36SAlex Maftei (amaftei) {
3651751cc36SAlex Maftei (amaftei) 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
3661751cc36SAlex Maftei (amaftei) }
3671751cc36SAlex Maftei (amaftei) 
3681751cc36SAlex Maftei (amaftei) /* efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
3691751cc36SAlex Maftei (amaftei)  *
3701751cc36SAlex Maftei (amaftei)  * @rx_queue:		Efx RX queue
3711751cc36SAlex Maftei (amaftei)  *
3721751cc36SAlex Maftei (amaftei)  * This allocates a batch of pages, maps them for DMA, and populates
3731751cc36SAlex Maftei (amaftei)  * struct efx_rx_buffers for each one. Return a negative error code or
3741751cc36SAlex Maftei (amaftei)  * 0 on success. If a single page can be used for multiple buffers,
3751751cc36SAlex Maftei (amaftei)  * then the page will either be inserted fully, or not at all.
3761751cc36SAlex Maftei (amaftei)  */
3771751cc36SAlex Maftei (amaftei) static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
3781751cc36SAlex Maftei (amaftei) {
3791751cc36SAlex Maftei (amaftei) 	unsigned int page_offset, index, count;
3801751cc36SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
3811751cc36SAlex Maftei (amaftei) 	struct efx_rx_page_state *state;
3821751cc36SAlex Maftei (amaftei) 	struct efx_rx_buffer *rx_buf;
3831751cc36SAlex Maftei (amaftei) 	dma_addr_t dma_addr;
3841751cc36SAlex Maftei (amaftei) 	struct page *page;
3851751cc36SAlex Maftei (amaftei) 
3861751cc36SAlex Maftei (amaftei) 	count = 0;
3871751cc36SAlex Maftei (amaftei) 	do {
3881751cc36SAlex Maftei (amaftei) 		page = efx_reuse_page(rx_queue);
3891751cc36SAlex Maftei (amaftei) 		if (page == NULL) {
3901751cc36SAlex Maftei (amaftei) 			page = alloc_pages(__GFP_COMP |
3911751cc36SAlex Maftei (amaftei) 					   (atomic ? GFP_ATOMIC : GFP_KERNEL),
3921751cc36SAlex Maftei (amaftei) 					   efx->rx_buffer_order);
3931751cc36SAlex Maftei (amaftei) 			if (unlikely(page == NULL))
3941751cc36SAlex Maftei (amaftei) 				return -ENOMEM;
3951751cc36SAlex Maftei (amaftei) 			dma_addr =
3961751cc36SAlex Maftei (amaftei) 				dma_map_page(&efx->pci_dev->dev, page, 0,
3971751cc36SAlex Maftei (amaftei) 					     PAGE_SIZE << efx->rx_buffer_order,
3981751cc36SAlex Maftei (amaftei) 					     DMA_FROM_DEVICE);
3991751cc36SAlex Maftei (amaftei) 			if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
4001751cc36SAlex Maftei (amaftei) 						       dma_addr))) {
4011751cc36SAlex Maftei (amaftei) 				__free_pages(page, efx->rx_buffer_order);
4021751cc36SAlex Maftei (amaftei) 				return -EIO;
4031751cc36SAlex Maftei (amaftei) 			}
4041751cc36SAlex Maftei (amaftei) 			state = page_address(page);
4051751cc36SAlex Maftei (amaftei) 			state->dma_addr = dma_addr;
4061751cc36SAlex Maftei (amaftei) 		} else {
4071751cc36SAlex Maftei (amaftei) 			state = page_address(page);
4081751cc36SAlex Maftei (amaftei) 			dma_addr = state->dma_addr;
4091751cc36SAlex Maftei (amaftei) 		}
4101751cc36SAlex Maftei (amaftei) 
4111751cc36SAlex Maftei (amaftei) 		dma_addr += sizeof(struct efx_rx_page_state);
4121751cc36SAlex Maftei (amaftei) 		page_offset = sizeof(struct efx_rx_page_state);
4131751cc36SAlex Maftei (amaftei) 
4141751cc36SAlex Maftei (amaftei) 		do {
4151751cc36SAlex Maftei (amaftei) 			index = rx_queue->added_count & rx_queue->ptr_mask;
4161751cc36SAlex Maftei (amaftei) 			rx_buf = efx_rx_buffer(rx_queue, index);
4171751cc36SAlex Maftei (amaftei) 			rx_buf->dma_addr = dma_addr + efx->rx_ip_align +
41886e85bf6SJesper Dangaard Brouer 					   EFX_XDP_HEADROOM;
4191751cc36SAlex Maftei (amaftei) 			rx_buf->page = page;
4201751cc36SAlex Maftei (amaftei) 			rx_buf->page_offset = page_offset + efx->rx_ip_align +
42186e85bf6SJesper Dangaard Brouer 					      EFX_XDP_HEADROOM;
4221751cc36SAlex Maftei (amaftei) 			rx_buf->len = efx->rx_dma_len;
4231751cc36SAlex Maftei (amaftei) 			rx_buf->flags = 0;
4241751cc36SAlex Maftei (amaftei) 			++rx_queue->added_count;
4251751cc36SAlex Maftei (amaftei) 			get_page(page);
4261751cc36SAlex Maftei (amaftei) 			dma_addr += efx->rx_page_buf_step;
4271751cc36SAlex Maftei (amaftei) 			page_offset += efx->rx_page_buf_step;
4281751cc36SAlex Maftei (amaftei) 		} while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
4291751cc36SAlex Maftei (amaftei) 
4301751cc36SAlex Maftei (amaftei) 		rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
4311751cc36SAlex Maftei (amaftei) 	} while (++count < efx->rx_pages_per_batch);
4321751cc36SAlex Maftei (amaftei) 
4331751cc36SAlex Maftei (amaftei) 	return 0;
4341751cc36SAlex Maftei (amaftei) }
4351751cc36SAlex Maftei (amaftei) 
4361751cc36SAlex Maftei (amaftei) void efx_rx_config_page_split(struct efx_nic *efx)
4371751cc36SAlex Maftei (amaftei) {
4381751cc36SAlex Maftei (amaftei) 	efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align +
43986e85bf6SJesper Dangaard Brouer 				      EFX_XDP_HEADROOM + EFX_XDP_TAILROOM,
4401751cc36SAlex Maftei (amaftei) 				      EFX_RX_BUF_ALIGNMENT);
4411751cc36SAlex Maftei (amaftei) 	efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
4421751cc36SAlex Maftei (amaftei) 		((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
4431751cc36SAlex Maftei (amaftei) 		efx->rx_page_buf_step);
4441751cc36SAlex Maftei (amaftei) 	efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
4451751cc36SAlex Maftei (amaftei) 		efx->rx_bufs_per_page;
4461751cc36SAlex Maftei (amaftei) 	efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
4471751cc36SAlex Maftei (amaftei) 					       efx->rx_bufs_per_page);
4481751cc36SAlex Maftei (amaftei) }
4491751cc36SAlex Maftei (amaftei) 
4501751cc36SAlex Maftei (amaftei) /* efx_fast_push_rx_descriptors - push new RX descriptors quickly
4511751cc36SAlex Maftei (amaftei)  * @rx_queue:		RX descriptor queue
4521751cc36SAlex Maftei (amaftei)  *
4531751cc36SAlex Maftei (amaftei)  * This will aim to fill the RX descriptor queue up to
4541751cc36SAlex Maftei (amaftei)  * @rx_queue->@max_fill. If there is insufficient atomic
4551751cc36SAlex Maftei (amaftei)  * memory to do so, a slow fill will be scheduled.
4561751cc36SAlex Maftei (amaftei)  *
4571751cc36SAlex Maftei (amaftei)  * The caller must provide serialisation (none is used here). In practise,
4581751cc36SAlex Maftei (amaftei)  * this means this function must run from the NAPI handler, or be called
4591751cc36SAlex Maftei (amaftei)  * when NAPI is disabled.
4601751cc36SAlex Maftei (amaftei)  */
4611751cc36SAlex Maftei (amaftei) void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
4621751cc36SAlex Maftei (amaftei) {
4631751cc36SAlex Maftei (amaftei) 	struct efx_nic *efx = rx_queue->efx;
4641751cc36SAlex Maftei (amaftei) 	unsigned int fill_level, batch_size;
4651751cc36SAlex Maftei (amaftei) 	int space, rc = 0;
4661751cc36SAlex Maftei (amaftei) 
4671751cc36SAlex Maftei (amaftei) 	if (!rx_queue->refill_enabled)
4681751cc36SAlex Maftei (amaftei) 		return;
4691751cc36SAlex Maftei (amaftei) 
4701751cc36SAlex Maftei (amaftei) 	/* Calculate current fill level, and exit if we don't need to fill */
4711751cc36SAlex Maftei (amaftei) 	fill_level = (rx_queue->added_count - rx_queue->removed_count);
4721751cc36SAlex Maftei (amaftei) 	EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
4731751cc36SAlex Maftei (amaftei) 	if (fill_level >= rx_queue->fast_fill_trigger)
4741751cc36SAlex Maftei (amaftei) 		goto out;
4751751cc36SAlex Maftei (amaftei) 
4761751cc36SAlex Maftei (amaftei) 	/* Record minimum fill level */
4771751cc36SAlex Maftei (amaftei) 	if (unlikely(fill_level < rx_queue->min_fill)) {
4781751cc36SAlex Maftei (amaftei) 		if (fill_level)
4791751cc36SAlex Maftei (amaftei) 			rx_queue->min_fill = fill_level;
4801751cc36SAlex Maftei (amaftei) 	}
4811751cc36SAlex Maftei (amaftei) 
4821751cc36SAlex Maftei (amaftei) 	batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
4831751cc36SAlex Maftei (amaftei) 	space = rx_queue->max_fill - fill_level;
4841751cc36SAlex Maftei (amaftei) 	EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
4851751cc36SAlex Maftei (amaftei) 
4861751cc36SAlex Maftei (amaftei) 	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
4871751cc36SAlex Maftei (amaftei) 		   "RX queue %d fast-filling descriptor ring from"
4881751cc36SAlex Maftei (amaftei) 		   " level %d to level %d\n",
4891751cc36SAlex Maftei (amaftei) 		   efx_rx_queue_index(rx_queue), fill_level,
4901751cc36SAlex Maftei (amaftei) 		   rx_queue->max_fill);
4911751cc36SAlex Maftei (amaftei) 
4921751cc36SAlex Maftei (amaftei) 	do {
4931751cc36SAlex Maftei (amaftei) 		rc = efx_init_rx_buffers(rx_queue, atomic);
4941751cc36SAlex Maftei (amaftei) 		if (unlikely(rc)) {
4951751cc36SAlex Maftei (amaftei) 			/* Ensure that we don't leave the rx queue empty */
4961751cc36SAlex Maftei (amaftei) 			efx_schedule_slow_fill(rx_queue);
4971751cc36SAlex Maftei (amaftei) 			goto out;
4981751cc36SAlex Maftei (amaftei) 		}
4991751cc36SAlex Maftei (amaftei) 	} while ((space -= batch_size) >= batch_size);
5001751cc36SAlex Maftei (amaftei) 
5011751cc36SAlex Maftei (amaftei) 	netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
5021751cc36SAlex Maftei (amaftei) 		   "RX queue %d fast-filled descriptor ring "
5031751cc36SAlex Maftei (amaftei) 		   "to level %d\n", efx_rx_queue_index(rx_queue),
5041751cc36SAlex Maftei (amaftei) 		   rx_queue->added_count - rx_queue->removed_count);
5051751cc36SAlex Maftei (amaftei) 
5061751cc36SAlex Maftei (amaftei)  out:
5071751cc36SAlex Maftei (amaftei) 	if (rx_queue->notified_count != rx_queue->added_count)
5081751cc36SAlex Maftei (amaftei) 		efx_nic_notify_rx_desc(rx_queue);
5091751cc36SAlex Maftei (amaftei) }
5103d95b884SAlex Maftei (amaftei) 
5113d95b884SAlex Maftei (amaftei) /* Pass a received packet up through GRO.  GRO can handle pages
5123d95b884SAlex Maftei (amaftei)  * regardless of checksum state and skbs with a good checksum.
5133d95b884SAlex Maftei (amaftei)  */
5143d95b884SAlex Maftei (amaftei) void
5153d95b884SAlex Maftei (amaftei) efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
5164d9c0a2dSEdward Cree 		  unsigned int n_frags, u8 *eh, __wsum csum)
5173d95b884SAlex Maftei (amaftei) {
5183d95b884SAlex Maftei (amaftei) 	struct napi_struct *napi = &channel->napi_str;
5193d95b884SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
5203d95b884SAlex Maftei (amaftei) 	struct sk_buff *skb;
5213d95b884SAlex Maftei (amaftei) 
5223d95b884SAlex Maftei (amaftei) 	skb = napi_get_frags(napi);
5233d95b884SAlex Maftei (amaftei) 	if (unlikely(!skb)) {
5243d95b884SAlex Maftei (amaftei) 		struct efx_rx_queue *rx_queue;
5253d95b884SAlex Maftei (amaftei) 
5263d95b884SAlex Maftei (amaftei) 		rx_queue = efx_channel_get_rx_queue(channel);
5273d95b884SAlex Maftei (amaftei) 		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
5283d95b884SAlex Maftei (amaftei) 		return;
5293d95b884SAlex Maftei (amaftei) 	}
5303d95b884SAlex Maftei (amaftei) 
53106888543SEdward Cree 	if (efx->net_dev->features & NETIF_F_RXHASH &&
53206888543SEdward Cree 	    efx_rx_buf_hash_valid(efx, eh))
5333d95b884SAlex Maftei (amaftei) 		skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
5343d95b884SAlex Maftei (amaftei) 			     PKT_HASH_TYPE_L3);
5354d9c0a2dSEdward Cree 	if (csum) {
5364d9c0a2dSEdward Cree 		skb->csum = csum;
5374d9c0a2dSEdward Cree 		skb->ip_summed = CHECKSUM_COMPLETE;
5384d9c0a2dSEdward Cree 	} else {
5393d95b884SAlex Maftei (amaftei) 		skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
5403d95b884SAlex Maftei (amaftei) 				  CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
5414d9c0a2dSEdward Cree 	}
5423d95b884SAlex Maftei (amaftei) 	skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
5433d95b884SAlex Maftei (amaftei) 
5443d95b884SAlex Maftei (amaftei) 	for (;;) {
5453d95b884SAlex Maftei (amaftei) 		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
5463d95b884SAlex Maftei (amaftei) 				   rx_buf->page, rx_buf->page_offset,
5473d95b884SAlex Maftei (amaftei) 				   rx_buf->len);
5483d95b884SAlex Maftei (amaftei) 		rx_buf->page = NULL;
5493d95b884SAlex Maftei (amaftei) 		skb->len += rx_buf->len;
5503d95b884SAlex Maftei (amaftei) 		if (skb_shinfo(skb)->nr_frags == n_frags)
5513d95b884SAlex Maftei (amaftei) 			break;
5523d95b884SAlex Maftei (amaftei) 
5533d95b884SAlex Maftei (amaftei) 		rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
5543d95b884SAlex Maftei (amaftei) 	}
5553d95b884SAlex Maftei (amaftei) 
5563d95b884SAlex Maftei (amaftei) 	skb->data_len = skb->len;
5573d95b884SAlex Maftei (amaftei) 	skb->truesize += n_frags * efx->rx_buffer_truesize;
5583d95b884SAlex Maftei (amaftei) 
5593d95b884SAlex Maftei (amaftei) 	skb_record_rx_queue(skb, channel->rx_queue.core_index);
5603d95b884SAlex Maftei (amaftei) 
5613d95b884SAlex Maftei (amaftei) 	napi_gro_frags(napi);
5623d95b884SAlex Maftei (amaftei) }
563960f1627SAlex Maftei (amaftei) 
564960f1627SAlex Maftei (amaftei) /* RSS contexts.  We're using linked lists and crappy O(n) algorithms, because
565960f1627SAlex Maftei (amaftei)  * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
566960f1627SAlex Maftei (amaftei)  */
567960f1627SAlex Maftei (amaftei) struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
568960f1627SAlex Maftei (amaftei) {
569960f1627SAlex Maftei (amaftei) 	struct list_head *head = &efx->rss_context.list;
570960f1627SAlex Maftei (amaftei) 	struct efx_rss_context *ctx, *new;
571960f1627SAlex Maftei (amaftei) 	u32 id = 1; /* Don't use zero, that refers to the master RSS context */
572960f1627SAlex Maftei (amaftei) 
573960f1627SAlex Maftei (amaftei) 	WARN_ON(!mutex_is_locked(&efx->rss_lock));
574960f1627SAlex Maftei (amaftei) 
575960f1627SAlex Maftei (amaftei) 	/* Search for first gap in the numbering */
576960f1627SAlex Maftei (amaftei) 	list_for_each_entry(ctx, head, list) {
577960f1627SAlex Maftei (amaftei) 		if (ctx->user_id != id)
578960f1627SAlex Maftei (amaftei) 			break;
579960f1627SAlex Maftei (amaftei) 		id++;
580960f1627SAlex Maftei (amaftei) 		/* Check for wrap.  If this happens, we have nearly 2^32
581960f1627SAlex Maftei (amaftei) 		 * allocated RSS contexts, which seems unlikely.
582960f1627SAlex Maftei (amaftei) 		 */
583960f1627SAlex Maftei (amaftei) 		if (WARN_ON_ONCE(!id))
584960f1627SAlex Maftei (amaftei) 			return NULL;
585960f1627SAlex Maftei (amaftei) 	}
586960f1627SAlex Maftei (amaftei) 
587960f1627SAlex Maftei (amaftei) 	/* Create the new entry */
588960f1627SAlex Maftei (amaftei) 	new = kmalloc(sizeof(*new), GFP_KERNEL);
589960f1627SAlex Maftei (amaftei) 	if (!new)
590960f1627SAlex Maftei (amaftei) 		return NULL;
591f7226e0fSAlex Maftei (amaftei) 	new->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
592960f1627SAlex Maftei (amaftei) 	new->rx_hash_udp_4tuple = false;
593960f1627SAlex Maftei (amaftei) 
594960f1627SAlex Maftei (amaftei) 	/* Insert the new entry into the gap */
595960f1627SAlex Maftei (amaftei) 	new->user_id = id;
596960f1627SAlex Maftei (amaftei) 	list_add_tail(&new->list, &ctx->list);
597960f1627SAlex Maftei (amaftei) 	return new;
598960f1627SAlex Maftei (amaftei) }
599960f1627SAlex Maftei (amaftei) 
600960f1627SAlex Maftei (amaftei) struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
601960f1627SAlex Maftei (amaftei) {
602960f1627SAlex Maftei (amaftei) 	struct list_head *head = &efx->rss_context.list;
603960f1627SAlex Maftei (amaftei) 	struct efx_rss_context *ctx;
604960f1627SAlex Maftei (amaftei) 
605960f1627SAlex Maftei (amaftei) 	WARN_ON(!mutex_is_locked(&efx->rss_lock));
606960f1627SAlex Maftei (amaftei) 
607960f1627SAlex Maftei (amaftei) 	list_for_each_entry(ctx, head, list)
608960f1627SAlex Maftei (amaftei) 		if (ctx->user_id == id)
609960f1627SAlex Maftei (amaftei) 			return ctx;
610960f1627SAlex Maftei (amaftei) 	return NULL;
611960f1627SAlex Maftei (amaftei) }
612960f1627SAlex Maftei (amaftei) 
613960f1627SAlex Maftei (amaftei) void efx_free_rss_context_entry(struct efx_rss_context *ctx)
614960f1627SAlex Maftei (amaftei) {
615960f1627SAlex Maftei (amaftei) 	list_del(&ctx->list);
616960f1627SAlex Maftei (amaftei) 	kfree(ctx);
617960f1627SAlex Maftei (amaftei) }
618960f1627SAlex Maftei (amaftei) 
619960f1627SAlex Maftei (amaftei) void efx_set_default_rx_indir_table(struct efx_nic *efx,
620960f1627SAlex Maftei (amaftei) 				    struct efx_rss_context *ctx)
621960f1627SAlex Maftei (amaftei) {
622960f1627SAlex Maftei (amaftei) 	size_t i;
623960f1627SAlex Maftei (amaftei) 
624960f1627SAlex Maftei (amaftei) 	for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
625960f1627SAlex Maftei (amaftei) 		ctx->rx_indir_table[i] =
626960f1627SAlex Maftei (amaftei) 			ethtool_rxfh_indir_default(i, efx->rss_spread);
627960f1627SAlex Maftei (amaftei) }
628f7226e0fSAlex Maftei (amaftei) 
629f7226e0fSAlex Maftei (amaftei) /**
630f7226e0fSAlex Maftei (amaftei)  * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
631f7226e0fSAlex Maftei (amaftei)  * @spec: Specification to test
632f7226e0fSAlex Maftei (amaftei)  *
633f7226e0fSAlex Maftei (amaftei)  * Return: %true if the specification is a non-drop RX filter that
634f7226e0fSAlex Maftei (amaftei)  * matches a local MAC address I/G bit value of 1 or matches a local
635f7226e0fSAlex Maftei (amaftei)  * IPv4 or IPv6 address value in the respective multicast address
636f7226e0fSAlex Maftei (amaftei)  * range.  Otherwise %false.
637f7226e0fSAlex Maftei (amaftei)  */
638f7226e0fSAlex Maftei (amaftei) bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
639f7226e0fSAlex Maftei (amaftei) {
640f7226e0fSAlex Maftei (amaftei) 	if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
641f7226e0fSAlex Maftei (amaftei) 	    spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
642f7226e0fSAlex Maftei (amaftei) 		return false;
643f7226e0fSAlex Maftei (amaftei) 
644f7226e0fSAlex Maftei (amaftei) 	if (spec->match_flags &
645f7226e0fSAlex Maftei (amaftei) 	    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
646f7226e0fSAlex Maftei (amaftei) 	    is_multicast_ether_addr(spec->loc_mac))
647f7226e0fSAlex Maftei (amaftei) 		return true;
648f7226e0fSAlex Maftei (amaftei) 
649f7226e0fSAlex Maftei (amaftei) 	if ((spec->match_flags &
650f7226e0fSAlex Maftei (amaftei) 	     (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
651f7226e0fSAlex Maftei (amaftei) 	    (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
652f7226e0fSAlex Maftei (amaftei) 		if (spec->ether_type == htons(ETH_P_IP) &&
653f7226e0fSAlex Maftei (amaftei) 		    ipv4_is_multicast(spec->loc_host[0]))
654f7226e0fSAlex Maftei (amaftei) 			return true;
655f7226e0fSAlex Maftei (amaftei) 		if (spec->ether_type == htons(ETH_P_IPV6) &&
656f7226e0fSAlex Maftei (amaftei) 		    ((const u8 *)spec->loc_host)[0] == 0xff)
657f7226e0fSAlex Maftei (amaftei) 			return true;
658f7226e0fSAlex Maftei (amaftei) 	}
659f7226e0fSAlex Maftei (amaftei) 
660f7226e0fSAlex Maftei (amaftei) 	return false;
661f7226e0fSAlex Maftei (amaftei) }
662f7226e0fSAlex Maftei (amaftei) 
663f7226e0fSAlex Maftei (amaftei) bool efx_filter_spec_equal(const struct efx_filter_spec *left,
664f7226e0fSAlex Maftei (amaftei) 			   const struct efx_filter_spec *right)
665f7226e0fSAlex Maftei (amaftei) {
666f7226e0fSAlex Maftei (amaftei) 	if ((left->match_flags ^ right->match_flags) |
667f7226e0fSAlex Maftei (amaftei) 	    ((left->flags ^ right->flags) &
668f7226e0fSAlex Maftei (amaftei) 	     (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
669f7226e0fSAlex Maftei (amaftei) 		return false;
670f7226e0fSAlex Maftei (amaftei) 
671f7226e0fSAlex Maftei (amaftei) 	return memcmp(&left->outer_vid, &right->outer_vid,
672f7226e0fSAlex Maftei (amaftei) 		      sizeof(struct efx_filter_spec) -
673f7226e0fSAlex Maftei (amaftei) 		      offsetof(struct efx_filter_spec, outer_vid)) == 0;
674f7226e0fSAlex Maftei (amaftei) }
675f7226e0fSAlex Maftei (amaftei) 
676f7226e0fSAlex Maftei (amaftei) u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
677f7226e0fSAlex Maftei (amaftei) {
678f7226e0fSAlex Maftei (amaftei) 	BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
679f7226e0fSAlex Maftei (amaftei) 	return jhash2((const u32 *)&spec->outer_vid,
680f7226e0fSAlex Maftei (amaftei) 		      (sizeof(struct efx_filter_spec) -
681f7226e0fSAlex Maftei (amaftei) 		       offsetof(struct efx_filter_spec, outer_vid)) / 4,
682f7226e0fSAlex Maftei (amaftei) 		      0);
683f7226e0fSAlex Maftei (amaftei) }
684f7226e0fSAlex Maftei (amaftei) 
685f7226e0fSAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
686f7226e0fSAlex Maftei (amaftei) bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
687f7226e0fSAlex Maftei (amaftei) 			bool *force)
688f7226e0fSAlex Maftei (amaftei) {
689f7226e0fSAlex Maftei (amaftei) 	if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
690f7226e0fSAlex Maftei (amaftei) 		/* ARFS is currently updating this entry, leave it */
691f7226e0fSAlex Maftei (amaftei) 		return false;
692f7226e0fSAlex Maftei (amaftei) 	}
693f7226e0fSAlex Maftei (amaftei) 	if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
694f7226e0fSAlex Maftei (amaftei) 		/* ARFS tried and failed to update this, so it's probably out
695f7226e0fSAlex Maftei (amaftei) 		 * of date.  Remove the filter and the ARFS rule entry.
696f7226e0fSAlex Maftei (amaftei) 		 */
697f7226e0fSAlex Maftei (amaftei) 		rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
698f7226e0fSAlex Maftei (amaftei) 		*force = true;
699f7226e0fSAlex Maftei (amaftei) 		return true;
700f7226e0fSAlex Maftei (amaftei) 	} else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
701f7226e0fSAlex Maftei (amaftei) 		/* ARFS has moved on, so old filter is not needed.  Since we did
702f7226e0fSAlex Maftei (amaftei) 		 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
703f7226e0fSAlex Maftei (amaftei) 		 * not be removed by efx_rps_hash_del() subsequently.
704f7226e0fSAlex Maftei (amaftei) 		 */
705f7226e0fSAlex Maftei (amaftei) 		*force = true;
706f7226e0fSAlex Maftei (amaftei) 		return true;
707f7226e0fSAlex Maftei (amaftei) 	}
708f7226e0fSAlex Maftei (amaftei) 	/* Remove it iff ARFS wants to. */
709f7226e0fSAlex Maftei (amaftei) 	return true;
710f7226e0fSAlex Maftei (amaftei) }
711f7226e0fSAlex Maftei (amaftei) 
712f7226e0fSAlex Maftei (amaftei) static
713f7226e0fSAlex Maftei (amaftei) struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
714f7226e0fSAlex Maftei (amaftei) 				       const struct efx_filter_spec *spec)
715f7226e0fSAlex Maftei (amaftei) {
716f7226e0fSAlex Maftei (amaftei) 	u32 hash = efx_filter_spec_hash(spec);
717f7226e0fSAlex Maftei (amaftei) 
718f7226e0fSAlex Maftei (amaftei) 	lockdep_assert_held(&efx->rps_hash_lock);
719f7226e0fSAlex Maftei (amaftei) 	if (!efx->rps_hash_table)
720f7226e0fSAlex Maftei (amaftei) 		return NULL;
721f7226e0fSAlex Maftei (amaftei) 	return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
722f7226e0fSAlex Maftei (amaftei) }
723f7226e0fSAlex Maftei (amaftei) 
724f7226e0fSAlex Maftei (amaftei) struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
725f7226e0fSAlex Maftei (amaftei) 					const struct efx_filter_spec *spec)
726f7226e0fSAlex Maftei (amaftei) {
727f7226e0fSAlex Maftei (amaftei) 	struct efx_arfs_rule *rule;
728f7226e0fSAlex Maftei (amaftei) 	struct hlist_head *head;
729f7226e0fSAlex Maftei (amaftei) 	struct hlist_node *node;
730f7226e0fSAlex Maftei (amaftei) 
731f7226e0fSAlex Maftei (amaftei) 	head = efx_rps_hash_bucket(efx, spec);
732f7226e0fSAlex Maftei (amaftei) 	if (!head)
733f7226e0fSAlex Maftei (amaftei) 		return NULL;
734f7226e0fSAlex Maftei (amaftei) 	hlist_for_each(node, head) {
735f7226e0fSAlex Maftei (amaftei) 		rule = container_of(node, struct efx_arfs_rule, node);
736f7226e0fSAlex Maftei (amaftei) 		if (efx_filter_spec_equal(spec, &rule->spec))
737f7226e0fSAlex Maftei (amaftei) 			return rule;
738f7226e0fSAlex Maftei (amaftei) 	}
739f7226e0fSAlex Maftei (amaftei) 	return NULL;
740f7226e0fSAlex Maftei (amaftei) }
741f7226e0fSAlex Maftei (amaftei) 
742f7226e0fSAlex Maftei (amaftei) struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
743f7226e0fSAlex Maftei (amaftei) 				       const struct efx_filter_spec *spec,
744f7226e0fSAlex Maftei (amaftei) 				       bool *new)
745f7226e0fSAlex Maftei (amaftei) {
746f7226e0fSAlex Maftei (amaftei) 	struct efx_arfs_rule *rule;
747f7226e0fSAlex Maftei (amaftei) 	struct hlist_head *head;
748f7226e0fSAlex Maftei (amaftei) 	struct hlist_node *node;
749f7226e0fSAlex Maftei (amaftei) 
750f7226e0fSAlex Maftei (amaftei) 	head = efx_rps_hash_bucket(efx, spec);
751f7226e0fSAlex Maftei (amaftei) 	if (!head)
752f7226e0fSAlex Maftei (amaftei) 		return NULL;
753f7226e0fSAlex Maftei (amaftei) 	hlist_for_each(node, head) {
754f7226e0fSAlex Maftei (amaftei) 		rule = container_of(node, struct efx_arfs_rule, node);
755f7226e0fSAlex Maftei (amaftei) 		if (efx_filter_spec_equal(spec, &rule->spec)) {
756f7226e0fSAlex Maftei (amaftei) 			*new = false;
757f7226e0fSAlex Maftei (amaftei) 			return rule;
758f7226e0fSAlex Maftei (amaftei) 		}
759f7226e0fSAlex Maftei (amaftei) 	}
760f7226e0fSAlex Maftei (amaftei) 	rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
761f7226e0fSAlex Maftei (amaftei) 	*new = true;
762f7226e0fSAlex Maftei (amaftei) 	if (rule) {
763f7226e0fSAlex Maftei (amaftei) 		memcpy(&rule->spec, spec, sizeof(rule->spec));
764f7226e0fSAlex Maftei (amaftei) 		hlist_add_head(&rule->node, head);
765f7226e0fSAlex Maftei (amaftei) 	}
766f7226e0fSAlex Maftei (amaftei) 	return rule;
767f7226e0fSAlex Maftei (amaftei) }
768f7226e0fSAlex Maftei (amaftei) 
769f7226e0fSAlex Maftei (amaftei) void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
770f7226e0fSAlex Maftei (amaftei) {
771f7226e0fSAlex Maftei (amaftei) 	struct efx_arfs_rule *rule;
772f7226e0fSAlex Maftei (amaftei) 	struct hlist_head *head;
773f7226e0fSAlex Maftei (amaftei) 	struct hlist_node *node;
774f7226e0fSAlex Maftei (amaftei) 
775f7226e0fSAlex Maftei (amaftei) 	head = efx_rps_hash_bucket(efx, spec);
776f7226e0fSAlex Maftei (amaftei) 	if (WARN_ON(!head))
777f7226e0fSAlex Maftei (amaftei) 		return;
778f7226e0fSAlex Maftei (amaftei) 	hlist_for_each(node, head) {
779f7226e0fSAlex Maftei (amaftei) 		rule = container_of(node, struct efx_arfs_rule, node);
780f7226e0fSAlex Maftei (amaftei) 		if (efx_filter_spec_equal(spec, &rule->spec)) {
781f7226e0fSAlex Maftei (amaftei) 			/* Someone already reused the entry.  We know that if
782f7226e0fSAlex Maftei (amaftei) 			 * this check doesn't fire (i.e. filter_id == REMOVING)
783f7226e0fSAlex Maftei (amaftei) 			 * then the REMOVING mark was put there by our caller,
784f7226e0fSAlex Maftei (amaftei) 			 * because caller is holding a lock on filter table and
785f7226e0fSAlex Maftei (amaftei) 			 * only holders of that lock set REMOVING.
786f7226e0fSAlex Maftei (amaftei) 			 */
787f7226e0fSAlex Maftei (amaftei) 			if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
788f7226e0fSAlex Maftei (amaftei) 				return;
789f7226e0fSAlex Maftei (amaftei) 			hlist_del(node);
790f7226e0fSAlex Maftei (amaftei) 			kfree(rule);
791f7226e0fSAlex Maftei (amaftei) 			return;
792f7226e0fSAlex Maftei (amaftei) 		}
793f7226e0fSAlex Maftei (amaftei) 	}
794f7226e0fSAlex Maftei (amaftei) 	/* We didn't find it. */
795f7226e0fSAlex Maftei (amaftei) 	WARN_ON(1);
796f7226e0fSAlex Maftei (amaftei) }
797f7226e0fSAlex Maftei (amaftei) #endif
798f7226e0fSAlex Maftei (amaftei) 
799f7226e0fSAlex Maftei (amaftei) int efx_probe_filters(struct efx_nic *efx)
800f7226e0fSAlex Maftei (amaftei) {
801f7226e0fSAlex Maftei (amaftei) 	int rc;
802f7226e0fSAlex Maftei (amaftei) 
803f7226e0fSAlex Maftei (amaftei) 	mutex_lock(&efx->mac_lock);
804f7226e0fSAlex Maftei (amaftei) 	down_write(&efx->filter_sem);
805f7226e0fSAlex Maftei (amaftei) 	rc = efx->type->filter_table_probe(efx);
806f7226e0fSAlex Maftei (amaftei) 	if (rc)
807f7226e0fSAlex Maftei (amaftei) 		goto out_unlock;
808f7226e0fSAlex Maftei (amaftei) 
809f7226e0fSAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
810f7226e0fSAlex Maftei (amaftei) 	if (efx->type->offload_features & NETIF_F_NTUPLE) {
811f7226e0fSAlex Maftei (amaftei) 		struct efx_channel *channel;
812f7226e0fSAlex Maftei (amaftei) 		int i, success = 1;
813f7226e0fSAlex Maftei (amaftei) 
814f7226e0fSAlex Maftei (amaftei) 		efx_for_each_channel(channel, efx) {
815f7226e0fSAlex Maftei (amaftei) 			channel->rps_flow_id =
816f7226e0fSAlex Maftei (amaftei) 				kcalloc(efx->type->max_rx_ip_filters,
817f7226e0fSAlex Maftei (amaftei) 					sizeof(*channel->rps_flow_id),
818f7226e0fSAlex Maftei (amaftei) 					GFP_KERNEL);
819f7226e0fSAlex Maftei (amaftei) 			if (!channel->rps_flow_id)
820f7226e0fSAlex Maftei (amaftei) 				success = 0;
821f7226e0fSAlex Maftei (amaftei) 			else
822f7226e0fSAlex Maftei (amaftei) 				for (i = 0;
823f7226e0fSAlex Maftei (amaftei) 				     i < efx->type->max_rx_ip_filters;
824f7226e0fSAlex Maftei (amaftei) 				     ++i)
825f7226e0fSAlex Maftei (amaftei) 					channel->rps_flow_id[i] =
826f7226e0fSAlex Maftei (amaftei) 						RPS_FLOW_ID_INVALID;
827f7226e0fSAlex Maftei (amaftei) 			channel->rfs_expire_index = 0;
828f7226e0fSAlex Maftei (amaftei) 			channel->rfs_filter_count = 0;
829f7226e0fSAlex Maftei (amaftei) 		}
830f7226e0fSAlex Maftei (amaftei) 
831f7226e0fSAlex Maftei (amaftei) 		if (!success) {
832f7226e0fSAlex Maftei (amaftei) 			efx_for_each_channel(channel, efx)
833f7226e0fSAlex Maftei (amaftei) 				kfree(channel->rps_flow_id);
834f7226e0fSAlex Maftei (amaftei) 			efx->type->filter_table_remove(efx);
835f7226e0fSAlex Maftei (amaftei) 			rc = -ENOMEM;
836f7226e0fSAlex Maftei (amaftei) 			goto out_unlock;
837f7226e0fSAlex Maftei (amaftei) 		}
838f7226e0fSAlex Maftei (amaftei) 	}
839f7226e0fSAlex Maftei (amaftei) #endif
840f7226e0fSAlex Maftei (amaftei) out_unlock:
841f7226e0fSAlex Maftei (amaftei) 	up_write(&efx->filter_sem);
842f7226e0fSAlex Maftei (amaftei) 	mutex_unlock(&efx->mac_lock);
843f7226e0fSAlex Maftei (amaftei) 	return rc;
844f7226e0fSAlex Maftei (amaftei) }
845f7226e0fSAlex Maftei (amaftei) 
846f7226e0fSAlex Maftei (amaftei) void efx_remove_filters(struct efx_nic *efx)
847f7226e0fSAlex Maftei (amaftei) {
848f7226e0fSAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
849f7226e0fSAlex Maftei (amaftei) 	struct efx_channel *channel;
850f7226e0fSAlex Maftei (amaftei) 
851f7226e0fSAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
852f7226e0fSAlex Maftei (amaftei) 		cancel_delayed_work_sync(&channel->filter_work);
853f7226e0fSAlex Maftei (amaftei) 		kfree(channel->rps_flow_id);
854788f920aSEdward Cree 		channel->rps_flow_id = NULL;
855f7226e0fSAlex Maftei (amaftei) 	}
856f7226e0fSAlex Maftei (amaftei) #endif
857f7226e0fSAlex Maftei (amaftei) 	down_write(&efx->filter_sem);
858f7226e0fSAlex Maftei (amaftei) 	efx->type->filter_table_remove(efx);
859f7226e0fSAlex Maftei (amaftei) 	up_write(&efx->filter_sem);
860f7226e0fSAlex Maftei (amaftei) }
86128abe825SEdward Cree 
86228abe825SEdward Cree #ifdef CONFIG_RFS_ACCEL
86328abe825SEdward Cree 
86428abe825SEdward Cree static void efx_filter_rfs_work(struct work_struct *data)
86528abe825SEdward Cree {
86628abe825SEdward Cree 	struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
86728abe825SEdward Cree 							      work);
86828abe825SEdward Cree 	struct efx_nic *efx = netdev_priv(req->net_dev);
86928abe825SEdward Cree 	struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
87028abe825SEdward Cree 	int slot_idx = req - efx->rps_slot;
87128abe825SEdward Cree 	struct efx_arfs_rule *rule;
87228abe825SEdward Cree 	u16 arfs_id = 0;
87328abe825SEdward Cree 	int rc;
87428abe825SEdward Cree 
87528abe825SEdward Cree 	rc = efx->type->filter_insert(efx, &req->spec, true);
87628abe825SEdward Cree 	if (rc >= 0)
87728abe825SEdward Cree 		/* Discard 'priority' part of EF10+ filter ID (mcdi_filters) */
87828abe825SEdward Cree 		rc %= efx->type->max_rx_ip_filters;
87928abe825SEdward Cree 	if (efx->rps_hash_table) {
88028abe825SEdward Cree 		spin_lock_bh(&efx->rps_hash_lock);
88128abe825SEdward Cree 		rule = efx_rps_hash_find(efx, &req->spec);
88228abe825SEdward Cree 		/* The rule might have already gone, if someone else's request
88328abe825SEdward Cree 		 * for the same spec was already worked and then expired before
88428abe825SEdward Cree 		 * we got around to our work.  In that case we have nothing
88528abe825SEdward Cree 		 * tying us to an arfs_id, meaning that as soon as the filter
88628abe825SEdward Cree 		 * is considered for expiry it will be removed.
88728abe825SEdward Cree 		 */
88828abe825SEdward Cree 		if (rule) {
88928abe825SEdward Cree 			if (rc < 0)
89028abe825SEdward Cree 				rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
89128abe825SEdward Cree 			else
89228abe825SEdward Cree 				rule->filter_id = rc;
89328abe825SEdward Cree 			arfs_id = rule->arfs_id;
89428abe825SEdward Cree 		}
89528abe825SEdward Cree 		spin_unlock_bh(&efx->rps_hash_lock);
89628abe825SEdward Cree 	}
89728abe825SEdward Cree 	if (rc >= 0) {
89828abe825SEdward Cree 		/* Remember this so we can check whether to expire the filter
89928abe825SEdward Cree 		 * later.
90028abe825SEdward Cree 		 */
90128abe825SEdward Cree 		mutex_lock(&efx->rps_mutex);
90228abe825SEdward Cree 		if (channel->rps_flow_id[rc] == RPS_FLOW_ID_INVALID)
90328abe825SEdward Cree 			channel->rfs_filter_count++;
90428abe825SEdward Cree 		channel->rps_flow_id[rc] = req->flow_id;
90528abe825SEdward Cree 		mutex_unlock(&efx->rps_mutex);
90628abe825SEdward Cree 
90728abe825SEdward Cree 		if (req->spec.ether_type == htons(ETH_P_IP))
90828abe825SEdward Cree 			netif_info(efx, rx_status, efx->net_dev,
90928abe825SEdward Cree 				   "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
91028abe825SEdward Cree 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
91128abe825SEdward Cree 				   req->spec.rem_host, ntohs(req->spec.rem_port),
91228abe825SEdward Cree 				   req->spec.loc_host, ntohs(req->spec.loc_port),
91328abe825SEdward Cree 				   req->rxq_index, req->flow_id, rc, arfs_id);
91428abe825SEdward Cree 		else
91528abe825SEdward Cree 			netif_info(efx, rx_status, efx->net_dev,
91628abe825SEdward Cree 				   "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
91728abe825SEdward Cree 				   (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
91828abe825SEdward Cree 				   req->spec.rem_host, ntohs(req->spec.rem_port),
91928abe825SEdward Cree 				   req->spec.loc_host, ntohs(req->spec.loc_port),
92028abe825SEdward Cree 				   req->rxq_index, req->flow_id, rc, arfs_id);
92128abe825SEdward Cree 		channel->n_rfs_succeeded++;
92228abe825SEdward Cree 	} else {
92328abe825SEdward Cree 		if (req->spec.ether_type == htons(ETH_P_IP))
92428abe825SEdward Cree 			netif_dbg(efx, rx_status, efx->net_dev,
92528abe825SEdward Cree 				  "failed to steer %s %pI4:%u:%pI4:%u to queue %u [flow %u rc %d id %u]\n",
92628abe825SEdward Cree 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
92728abe825SEdward Cree 				  req->spec.rem_host, ntohs(req->spec.rem_port),
92828abe825SEdward Cree 				  req->spec.loc_host, ntohs(req->spec.loc_port),
92928abe825SEdward Cree 				  req->rxq_index, req->flow_id, rc, arfs_id);
93028abe825SEdward Cree 		else
93128abe825SEdward Cree 			netif_dbg(efx, rx_status, efx->net_dev,
93228abe825SEdward Cree 				  "failed to steer %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u rc %d id %u]\n",
93328abe825SEdward Cree 				  (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
93428abe825SEdward Cree 				  req->spec.rem_host, ntohs(req->spec.rem_port),
93528abe825SEdward Cree 				  req->spec.loc_host, ntohs(req->spec.loc_port),
93628abe825SEdward Cree 				  req->rxq_index, req->flow_id, rc, arfs_id);
93728abe825SEdward Cree 		channel->n_rfs_failed++;
93828abe825SEdward Cree 		/* We're overloading the NIC's filter tables, so let's do a
93928abe825SEdward Cree 		 * chunk of extra expiry work.
94028abe825SEdward Cree 		 */
94128abe825SEdward Cree 		__efx_filter_rfs_expire(channel, min(channel->rfs_filter_count,
94228abe825SEdward Cree 						     100u));
94328abe825SEdward Cree 	}
94428abe825SEdward Cree 
94528abe825SEdward Cree 	/* Release references */
94628abe825SEdward Cree 	clear_bit(slot_idx, &efx->rps_slot_map);
94728abe825SEdward Cree 	dev_put(req->net_dev);
94828abe825SEdward Cree }
94928abe825SEdward Cree 
95028abe825SEdward Cree int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
95128abe825SEdward Cree 		   u16 rxq_index, u32 flow_id)
95228abe825SEdward Cree {
95328abe825SEdward Cree 	struct efx_nic *efx = netdev_priv(net_dev);
95428abe825SEdward Cree 	struct efx_async_filter_insertion *req;
95528abe825SEdward Cree 	struct efx_arfs_rule *rule;
95628abe825SEdward Cree 	struct flow_keys fk;
95728abe825SEdward Cree 	int slot_idx;
95828abe825SEdward Cree 	bool new;
95928abe825SEdward Cree 	int rc;
96028abe825SEdward Cree 
96128abe825SEdward Cree 	/* find a free slot */
96228abe825SEdward Cree 	for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
96328abe825SEdward Cree 		if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
96428abe825SEdward Cree 			break;
96528abe825SEdward Cree 	if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
96628abe825SEdward Cree 		return -EBUSY;
96728abe825SEdward Cree 
96828abe825SEdward Cree 	if (flow_id == RPS_FLOW_ID_INVALID) {
96928abe825SEdward Cree 		rc = -EINVAL;
97028abe825SEdward Cree 		goto out_clear;
97128abe825SEdward Cree 	}
97228abe825SEdward Cree 
97328abe825SEdward Cree 	if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
97428abe825SEdward Cree 		rc = -EPROTONOSUPPORT;
97528abe825SEdward Cree 		goto out_clear;
97628abe825SEdward Cree 	}
97728abe825SEdward Cree 
97828abe825SEdward Cree 	if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
97928abe825SEdward Cree 		rc = -EPROTONOSUPPORT;
98028abe825SEdward Cree 		goto out_clear;
98128abe825SEdward Cree 	}
98228abe825SEdward Cree 	if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
98328abe825SEdward Cree 		rc = -EPROTONOSUPPORT;
98428abe825SEdward Cree 		goto out_clear;
98528abe825SEdward Cree 	}
98628abe825SEdward Cree 
98728abe825SEdward Cree 	req = efx->rps_slot + slot_idx;
98828abe825SEdward Cree 	efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
98928abe825SEdward Cree 			   efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
99028abe825SEdward Cree 			   rxq_index);
99128abe825SEdward Cree 	req->spec.match_flags =
99228abe825SEdward Cree 		EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
99328abe825SEdward Cree 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
99428abe825SEdward Cree 		EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
99528abe825SEdward Cree 	req->spec.ether_type = fk.basic.n_proto;
99628abe825SEdward Cree 	req->spec.ip_proto = fk.basic.ip_proto;
99728abe825SEdward Cree 
99828abe825SEdward Cree 	if (fk.basic.n_proto == htons(ETH_P_IP)) {
99928abe825SEdward Cree 		req->spec.rem_host[0] = fk.addrs.v4addrs.src;
100028abe825SEdward Cree 		req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
100128abe825SEdward Cree 	} else {
100228abe825SEdward Cree 		memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
100328abe825SEdward Cree 		       sizeof(struct in6_addr));
100428abe825SEdward Cree 		memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
100528abe825SEdward Cree 		       sizeof(struct in6_addr));
100628abe825SEdward Cree 	}
100728abe825SEdward Cree 
100828abe825SEdward Cree 	req->spec.rem_port = fk.ports.src;
100928abe825SEdward Cree 	req->spec.loc_port = fk.ports.dst;
101028abe825SEdward Cree 
101128abe825SEdward Cree 	if (efx->rps_hash_table) {
101228abe825SEdward Cree 		/* Add it to ARFS hash table */
101328abe825SEdward Cree 		spin_lock(&efx->rps_hash_lock);
101428abe825SEdward Cree 		rule = efx_rps_hash_add(efx, &req->spec, &new);
101528abe825SEdward Cree 		if (!rule) {
101628abe825SEdward Cree 			rc = -ENOMEM;
101728abe825SEdward Cree 			goto out_unlock;
101828abe825SEdward Cree 		}
101928abe825SEdward Cree 		if (new)
102028abe825SEdward Cree 			rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
102128abe825SEdward Cree 		rc = rule->arfs_id;
102228abe825SEdward Cree 		/* Skip if existing or pending filter already does the right thing */
102328abe825SEdward Cree 		if (!new && rule->rxq_index == rxq_index &&
102428abe825SEdward Cree 		    rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
102528abe825SEdward Cree 			goto out_unlock;
102628abe825SEdward Cree 		rule->rxq_index = rxq_index;
102728abe825SEdward Cree 		rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
102828abe825SEdward Cree 		spin_unlock(&efx->rps_hash_lock);
102928abe825SEdward Cree 	} else {
103028abe825SEdward Cree 		/* Without an ARFS hash table, we just use arfs_id 0 for all
103128abe825SEdward Cree 		 * filters.  This means if multiple flows hash to the same
103228abe825SEdward Cree 		 * flow_id, all but the most recently touched will be eligible
103328abe825SEdward Cree 		 * for expiry.
103428abe825SEdward Cree 		 */
103528abe825SEdward Cree 		rc = 0;
103628abe825SEdward Cree 	}
103728abe825SEdward Cree 
103828abe825SEdward Cree 	/* Queue the request */
103928abe825SEdward Cree 	dev_hold(req->net_dev = net_dev);
104028abe825SEdward Cree 	INIT_WORK(&req->work, efx_filter_rfs_work);
104128abe825SEdward Cree 	req->rxq_index = rxq_index;
104228abe825SEdward Cree 	req->flow_id = flow_id;
104328abe825SEdward Cree 	schedule_work(&req->work);
104428abe825SEdward Cree 	return rc;
104528abe825SEdward Cree out_unlock:
104628abe825SEdward Cree 	spin_unlock(&efx->rps_hash_lock);
104728abe825SEdward Cree out_clear:
104828abe825SEdward Cree 	clear_bit(slot_idx, &efx->rps_slot_map);
104928abe825SEdward Cree 	return rc;
105028abe825SEdward Cree }
105128abe825SEdward Cree 
105228abe825SEdward Cree bool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota)
105328abe825SEdward Cree {
105428abe825SEdward Cree 	bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
105528abe825SEdward Cree 	struct efx_nic *efx = channel->efx;
105628abe825SEdward Cree 	unsigned int index, size, start;
105728abe825SEdward Cree 	u32 flow_id;
105828abe825SEdward Cree 
105928abe825SEdward Cree 	if (!mutex_trylock(&efx->rps_mutex))
106028abe825SEdward Cree 		return false;
106128abe825SEdward Cree 	expire_one = efx->type->filter_rfs_expire_one;
106228abe825SEdward Cree 	index = channel->rfs_expire_index;
106328abe825SEdward Cree 	start = index;
106428abe825SEdward Cree 	size = efx->type->max_rx_ip_filters;
106528abe825SEdward Cree 	while (quota) {
106628abe825SEdward Cree 		flow_id = channel->rps_flow_id[index];
106728abe825SEdward Cree 
106828abe825SEdward Cree 		if (flow_id != RPS_FLOW_ID_INVALID) {
106928abe825SEdward Cree 			quota--;
107028abe825SEdward Cree 			if (expire_one(efx, flow_id, index)) {
107128abe825SEdward Cree 				netif_info(efx, rx_status, efx->net_dev,
107228abe825SEdward Cree 					   "expired filter %d [channel %u flow %u]\n",
107328abe825SEdward Cree 					   index, channel->channel, flow_id);
107428abe825SEdward Cree 				channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
107528abe825SEdward Cree 				channel->rfs_filter_count--;
107628abe825SEdward Cree 			}
107728abe825SEdward Cree 		}
107828abe825SEdward Cree 		if (++index == size)
107928abe825SEdward Cree 			index = 0;
108028abe825SEdward Cree 		/* If we were called with a quota that exceeds the total number
108128abe825SEdward Cree 		 * of filters in the table (which shouldn't happen, but could
108228abe825SEdward Cree 		 * if two callers race), ensure that we don't loop forever -
108328abe825SEdward Cree 		 * stop when we've examined every row of the table.
108428abe825SEdward Cree 		 */
108528abe825SEdward Cree 		if (index == start)
108628abe825SEdward Cree 			break;
108728abe825SEdward Cree 	}
108828abe825SEdward Cree 
108928abe825SEdward Cree 	channel->rfs_expire_index = index;
109028abe825SEdward Cree 	mutex_unlock(&efx->rps_mutex);
109128abe825SEdward Cree 	return true;
109228abe825SEdward Cree }
109328abe825SEdward Cree 
109428abe825SEdward Cree #endif /* CONFIG_RFS_ACCEL */
1095