18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd.
58c2ecf20Sopenharmony_ci * Copyright 2006-2013 Solarflare Communications Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
148c2ecf20Sopenharmony_ci#include <linux/crc32.h>
158c2ecf20Sopenharmony_ci#include "net_driver.h"
168c2ecf20Sopenharmony_ci#include "bitfield.h"
178c2ecf20Sopenharmony_ci#include "efx.h"
188c2ecf20Sopenharmony_ci#include "nic.h"
198c2ecf20Sopenharmony_ci#include "farch_regs.h"
208c2ecf20Sopenharmony_ci#include "io.h"
218c2ecf20Sopenharmony_ci#include "workarounds.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Falcon-architecture (SFC4000) support */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/**************************************************************************
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Configurable values
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci **************************************************************************
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* This is set to 16 for a good reason.  In summary, if larger than
338c2ecf20Sopenharmony_ci * 16, the descriptor cache holds more than a default socket
348c2ecf20Sopenharmony_ci * buffer's worth of packets (for UDP we can only have at most one
358c2ecf20Sopenharmony_ci * socket buffer's worth outstanding).  This combined with the fact
368c2ecf20Sopenharmony_ci * that we only get 1 TX event per descriptor cache means the NIC
378c2ecf20Sopenharmony_ci * goes idle.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci#define TX_DC_ENTRIES 16
408c2ecf20Sopenharmony_ci#define TX_DC_ENTRIES_ORDER 1
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define RX_DC_ENTRIES 64
438c2ecf20Sopenharmony_ci#define RX_DC_ENTRIES_ORDER 3
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* If EF4_MAX_INT_ERRORS internal errors occur within
468c2ecf20Sopenharmony_ci * EF4_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
478c2ecf20Sopenharmony_ci * disable it.
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_ci#define EF4_INT_ERROR_EXPIRE 3600
508c2ecf20Sopenharmony_ci#define EF4_MAX_INT_ERRORS 5
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Depth of RX flush request fifo */
538c2ecf20Sopenharmony_ci#define EF4_RX_FLUSH_COUNT 4
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Driver generated events */
568c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC_TEST		0x000101
578c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC_FILL		0x000102
588c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC_RX_DRAIN	0x000103
598c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC_TX_DRAIN	0x000104
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC(_code, _data)	((_code) << 8 | (_data))
628c2ecf20Sopenharmony_ci#define _EF4_CHANNEL_MAGIC_CODE(_magic)		((_magic) >> 8)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define EF4_CHANNEL_MAGIC_TEST(_channel)				\
658c2ecf20Sopenharmony_ci	_EF4_CHANNEL_MAGIC(_EF4_CHANNEL_MAGIC_TEST, (_channel)->channel)
668c2ecf20Sopenharmony_ci#define EF4_CHANNEL_MAGIC_FILL(_rx_queue)				\
678c2ecf20Sopenharmony_ci	_EF4_CHANNEL_MAGIC(_EF4_CHANNEL_MAGIC_FILL,			\
688c2ecf20Sopenharmony_ci			   ef4_rx_queue_index(_rx_queue))
698c2ecf20Sopenharmony_ci#define EF4_CHANNEL_MAGIC_RX_DRAIN(_rx_queue)				\
708c2ecf20Sopenharmony_ci	_EF4_CHANNEL_MAGIC(_EF4_CHANNEL_MAGIC_RX_DRAIN,			\
718c2ecf20Sopenharmony_ci			   ef4_rx_queue_index(_rx_queue))
728c2ecf20Sopenharmony_ci#define EF4_CHANNEL_MAGIC_TX_DRAIN(_tx_queue)				\
738c2ecf20Sopenharmony_ci	_EF4_CHANNEL_MAGIC(_EF4_CHANNEL_MAGIC_TX_DRAIN,			\
748c2ecf20Sopenharmony_ci			   (_tx_queue)->queue)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void ef4_farch_magic_event(struct ef4_channel *channel, u32 magic);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/**************************************************************************
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * Hardware access
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci **************************************************************************/
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic inline void ef4_write_buf_tbl(struct ef4_nic *efx, ef4_qword_t *value,
858c2ecf20Sopenharmony_ci				     unsigned int index)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	ef4_sram_writeq(efx, efx->membase + efx->type->buf_tbl_base,
888c2ecf20Sopenharmony_ci			value, index);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic bool ef4_masked_compare_oword(const ef4_oword_t *a, const ef4_oword_t *b,
928c2ecf20Sopenharmony_ci				     const ef4_oword_t *mask)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
958c2ecf20Sopenharmony_ci		((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciint ef4_farch_test_registers(struct ef4_nic *efx,
998c2ecf20Sopenharmony_ci			     const struct ef4_farch_register_test *regs,
1008c2ecf20Sopenharmony_ci			     size_t n_regs)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	unsigned address = 0;
1038c2ecf20Sopenharmony_ci	int i, j;
1048c2ecf20Sopenharmony_ci	ef4_oword_t mask, imask, original, reg, buf;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	for (i = 0; i < n_regs; ++i) {
1078c2ecf20Sopenharmony_ci		address = regs[i].address;
1088c2ecf20Sopenharmony_ci		mask = imask = regs[i].mask;
1098c2ecf20Sopenharmony_ci		EF4_INVERT_OWORD(imask);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		ef4_reado(efx, &original, address);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci		/* bit sweep on and off */
1148c2ecf20Sopenharmony_ci		for (j = 0; j < 128; j++) {
1158c2ecf20Sopenharmony_ci			if (!EF4_EXTRACT_OWORD32(mask, j, j))
1168c2ecf20Sopenharmony_ci				continue;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci			/* Test this testable bit can be set in isolation */
1198c2ecf20Sopenharmony_ci			EF4_AND_OWORD(reg, original, mask);
1208c2ecf20Sopenharmony_ci			EF4_SET_OWORD32(reg, j, j, 1);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci			ef4_writeo(efx, &reg, address);
1238c2ecf20Sopenharmony_ci			ef4_reado(efx, &buf, address);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			if (ef4_masked_compare_oword(&reg, &buf, &mask))
1268c2ecf20Sopenharmony_ci				goto fail;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci			/* Test this testable bit can be cleared in isolation */
1298c2ecf20Sopenharmony_ci			EF4_OR_OWORD(reg, original, mask);
1308c2ecf20Sopenharmony_ci			EF4_SET_OWORD32(reg, j, j, 0);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci			ef4_writeo(efx, &reg, address);
1338c2ecf20Sopenharmony_ci			ef4_reado(efx, &buf, address);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci			if (ef4_masked_compare_oword(&reg, &buf, &mask))
1368c2ecf20Sopenharmony_ci				goto fail;
1378c2ecf20Sopenharmony_ci		}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci		ef4_writeo(efx, &original, address);
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return 0;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cifail:
1458c2ecf20Sopenharmony_ci	netif_err(efx, hw, efx->net_dev,
1468c2ecf20Sopenharmony_ci		  "wrote "EF4_OWORD_FMT" read "EF4_OWORD_FMT
1478c2ecf20Sopenharmony_ci		  " at address 0x%x mask "EF4_OWORD_FMT"\n", EF4_OWORD_VAL(reg),
1488c2ecf20Sopenharmony_ci		  EF4_OWORD_VAL(buf), address, EF4_OWORD_VAL(mask));
1498c2ecf20Sopenharmony_ci	return -EIO;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/**************************************************************************
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci * Special buffer handling
1558c2ecf20Sopenharmony_ci * Special buffers are used for event queues and the TX and RX
1568c2ecf20Sopenharmony_ci * descriptor rings.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci *************************************************************************/
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/*
1618c2ecf20Sopenharmony_ci * Initialise a special buffer
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * This will define a buffer (previously allocated via
1648c2ecf20Sopenharmony_ci * ef4_alloc_special_buffer()) in the buffer table, allowing
1658c2ecf20Sopenharmony_ci * it to be used for event queues, descriptor rings etc.
1668c2ecf20Sopenharmony_ci */
1678c2ecf20Sopenharmony_cistatic void
1688c2ecf20Sopenharmony_cief4_init_special_buffer(struct ef4_nic *efx, struct ef4_special_buffer *buffer)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	ef4_qword_t buf_desc;
1718c2ecf20Sopenharmony_ci	unsigned int index;
1728c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
1738c2ecf20Sopenharmony_ci	int i;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	EF4_BUG_ON_PARANOID(!buffer->buf.addr);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* Write buffer descriptors to NIC */
1788c2ecf20Sopenharmony_ci	for (i = 0; i < buffer->entries; i++) {
1798c2ecf20Sopenharmony_ci		index = buffer->index + i;
1808c2ecf20Sopenharmony_ci		dma_addr = buffer->buf.dma_addr + (i * EF4_BUF_SIZE);
1818c2ecf20Sopenharmony_ci		netif_dbg(efx, probe, efx->net_dev,
1828c2ecf20Sopenharmony_ci			  "mapping special buffer %d at %llx\n",
1838c2ecf20Sopenharmony_ci			  index, (unsigned long long)dma_addr);
1848c2ecf20Sopenharmony_ci		EF4_POPULATE_QWORD_3(buf_desc,
1858c2ecf20Sopenharmony_ci				     FRF_AZ_BUF_ADR_REGION, 0,
1868c2ecf20Sopenharmony_ci				     FRF_AZ_BUF_ADR_FBUF, dma_addr >> 12,
1878c2ecf20Sopenharmony_ci				     FRF_AZ_BUF_OWNER_ID_FBUF, 0);
1888c2ecf20Sopenharmony_ci		ef4_write_buf_tbl(efx, &buf_desc, index);
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* Unmaps a buffer and clears the buffer table entries */
1938c2ecf20Sopenharmony_cistatic void
1948c2ecf20Sopenharmony_cief4_fini_special_buffer(struct ef4_nic *efx, struct ef4_special_buffer *buffer)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	ef4_oword_t buf_tbl_upd;
1978c2ecf20Sopenharmony_ci	unsigned int start = buffer->index;
1988c2ecf20Sopenharmony_ci	unsigned int end = (buffer->index + buffer->entries - 1);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (!buffer->entries)
2018c2ecf20Sopenharmony_ci		return;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev, "unmapping special buffers %d-%d\n",
2048c2ecf20Sopenharmony_ci		  buffer->index, buffer->index + buffer->entries - 1);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_4(buf_tbl_upd,
2078c2ecf20Sopenharmony_ci			     FRF_AZ_BUF_UPD_CMD, 0,
2088c2ecf20Sopenharmony_ci			     FRF_AZ_BUF_CLR_CMD, 1,
2098c2ecf20Sopenharmony_ci			     FRF_AZ_BUF_CLR_END_ID, end,
2108c2ecf20Sopenharmony_ci			     FRF_AZ_BUF_CLR_START_ID, start);
2118c2ecf20Sopenharmony_ci	ef4_writeo(efx, &buf_tbl_upd, FR_AZ_BUF_TBL_UPD);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/*
2158c2ecf20Sopenharmony_ci * Allocate a new special buffer
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * This allocates memory for a new buffer, clears it and allocates a
2188c2ecf20Sopenharmony_ci * new buffer ID range.  It does not write into the buffer table.
2198c2ecf20Sopenharmony_ci *
2208c2ecf20Sopenharmony_ci * This call will allocate 4KB buffers, since 8KB buffers can't be
2218c2ecf20Sopenharmony_ci * used for event queues and descriptor rings.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_cistatic int ef4_alloc_special_buffer(struct ef4_nic *efx,
2248c2ecf20Sopenharmony_ci				    struct ef4_special_buffer *buffer,
2258c2ecf20Sopenharmony_ci				    unsigned int len)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	len = ALIGN(len, EF4_BUF_SIZE);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (ef4_nic_alloc_buffer(efx, &buffer->buf, len, GFP_KERNEL))
2308c2ecf20Sopenharmony_ci		return -ENOMEM;
2318c2ecf20Sopenharmony_ci	buffer->entries = len / EF4_BUF_SIZE;
2328c2ecf20Sopenharmony_ci	BUG_ON(buffer->buf.dma_addr & (EF4_BUF_SIZE - 1));
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	/* Select new buffer ID */
2358c2ecf20Sopenharmony_ci	buffer->index = efx->next_buffer_table;
2368c2ecf20Sopenharmony_ci	efx->next_buffer_table += buffer->entries;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	netif_dbg(efx, probe, efx->net_dev,
2398c2ecf20Sopenharmony_ci		  "allocating special buffers %d-%d at %llx+%x "
2408c2ecf20Sopenharmony_ci		  "(virt %p phys %llx)\n", buffer->index,
2418c2ecf20Sopenharmony_ci		  buffer->index + buffer->entries - 1,
2428c2ecf20Sopenharmony_ci		  (u64)buffer->buf.dma_addr, len,
2438c2ecf20Sopenharmony_ci		  buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return 0;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic void
2498c2ecf20Sopenharmony_cief4_free_special_buffer(struct ef4_nic *efx, struct ef4_special_buffer *buffer)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	if (!buffer->buf.addr)
2528c2ecf20Sopenharmony_ci		return;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev,
2558c2ecf20Sopenharmony_ci		  "deallocating special buffers %d-%d at %llx+%x "
2568c2ecf20Sopenharmony_ci		  "(virt %p phys %llx)\n", buffer->index,
2578c2ecf20Sopenharmony_ci		  buffer->index + buffer->entries - 1,
2588c2ecf20Sopenharmony_ci		  (u64)buffer->buf.dma_addr, buffer->buf.len,
2598c2ecf20Sopenharmony_ci		  buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	ef4_nic_free_buffer(efx, &buffer->buf);
2628c2ecf20Sopenharmony_ci	buffer->entries = 0;
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci/**************************************************************************
2668c2ecf20Sopenharmony_ci *
2678c2ecf20Sopenharmony_ci * TX path
2688c2ecf20Sopenharmony_ci *
2698c2ecf20Sopenharmony_ci **************************************************************************/
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci/* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
2728c2ecf20Sopenharmony_cistatic inline void ef4_farch_notify_tx_desc(struct ef4_tx_queue *tx_queue)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	unsigned write_ptr;
2758c2ecf20Sopenharmony_ci	ef4_dword_t reg;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
2788c2ecf20Sopenharmony_ci	EF4_POPULATE_DWORD_1(reg, FRF_AZ_TX_DESC_WPTR_DWORD, write_ptr);
2798c2ecf20Sopenharmony_ci	ef4_writed_page(tx_queue->efx, &reg,
2808c2ecf20Sopenharmony_ci			FR_AZ_TX_DESC_UPD_DWORD_P0, tx_queue->queue);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/* Write pointer and first descriptor for TX descriptor ring */
2848c2ecf20Sopenharmony_cistatic inline void ef4_farch_push_tx_desc(struct ef4_tx_queue *tx_queue,
2858c2ecf20Sopenharmony_ci					  const ef4_qword_t *txd)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	unsigned write_ptr;
2888c2ecf20Sopenharmony_ci	ef4_oword_t reg;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	BUILD_BUG_ON(FRF_AZ_TX_DESC_LBN != 0);
2918c2ecf20Sopenharmony_ci	BUILD_BUG_ON(FR_AA_TX_DESC_UPD_KER != FR_BZ_TX_DESC_UPD_P0);
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
2948c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_2(reg, FRF_AZ_TX_DESC_PUSH_CMD, true,
2958c2ecf20Sopenharmony_ci			     FRF_AZ_TX_DESC_WPTR, write_ptr);
2968c2ecf20Sopenharmony_ci	reg.qword[0] = *txd;
2978c2ecf20Sopenharmony_ci	ef4_writeo_page(tx_queue->efx, &reg,
2988c2ecf20Sopenharmony_ci			FR_BZ_TX_DESC_UPD_P0, tx_queue->queue);
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci/* For each entry inserted into the software descriptor ring, create a
3038c2ecf20Sopenharmony_ci * descriptor in the hardware TX descriptor ring (in host memory), and
3048c2ecf20Sopenharmony_ci * write a doorbell.
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_civoid ef4_farch_tx_write(struct ef4_tx_queue *tx_queue)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct ef4_tx_buffer *buffer;
3098c2ecf20Sopenharmony_ci	ef4_qword_t *txd;
3108c2ecf20Sopenharmony_ci	unsigned write_ptr;
3118c2ecf20Sopenharmony_ci	unsigned old_write_count = tx_queue->write_count;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	tx_queue->xmit_more_available = false;
3148c2ecf20Sopenharmony_ci	if (unlikely(tx_queue->write_count == tx_queue->insert_count))
3158c2ecf20Sopenharmony_ci		return;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	do {
3188c2ecf20Sopenharmony_ci		write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
3198c2ecf20Sopenharmony_ci		buffer = &tx_queue->buffer[write_ptr];
3208c2ecf20Sopenharmony_ci		txd = ef4_tx_desc(tx_queue, write_ptr);
3218c2ecf20Sopenharmony_ci		++tx_queue->write_count;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		EF4_BUG_ON_PARANOID(buffer->flags & EF4_TX_BUF_OPTION);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci		/* Create TX descriptor ring entry */
3268c2ecf20Sopenharmony_ci		BUILD_BUG_ON(EF4_TX_BUF_CONT != 1);
3278c2ecf20Sopenharmony_ci		EF4_POPULATE_QWORD_4(*txd,
3288c2ecf20Sopenharmony_ci				     FSF_AZ_TX_KER_CONT,
3298c2ecf20Sopenharmony_ci				     buffer->flags & EF4_TX_BUF_CONT,
3308c2ecf20Sopenharmony_ci				     FSF_AZ_TX_KER_BYTE_COUNT, buffer->len,
3318c2ecf20Sopenharmony_ci				     FSF_AZ_TX_KER_BUF_REGION, 0,
3328c2ecf20Sopenharmony_ci				     FSF_AZ_TX_KER_BUF_ADDR, buffer->dma_addr);
3338c2ecf20Sopenharmony_ci	} while (tx_queue->write_count != tx_queue->insert_count);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	wmb(); /* Ensure descriptors are written before they are fetched */
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (ef4_nic_may_push_tx_desc(tx_queue, old_write_count)) {
3388c2ecf20Sopenharmony_ci		txd = ef4_tx_desc(tx_queue,
3398c2ecf20Sopenharmony_ci				  old_write_count & tx_queue->ptr_mask);
3408c2ecf20Sopenharmony_ci		ef4_farch_push_tx_desc(tx_queue, txd);
3418c2ecf20Sopenharmony_ci		++tx_queue->pushes;
3428c2ecf20Sopenharmony_ci	} else {
3438c2ecf20Sopenharmony_ci		ef4_farch_notify_tx_desc(tx_queue);
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ciunsigned int ef4_farch_tx_limit_len(struct ef4_tx_queue *tx_queue,
3488c2ecf20Sopenharmony_ci				    dma_addr_t dma_addr, unsigned int len)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	/* Don't cross 4K boundaries with descriptors. */
3518c2ecf20Sopenharmony_ci	unsigned int limit = (~dma_addr & (EF4_PAGE_SIZE - 1)) + 1;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	len = min(limit, len);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (EF4_WORKAROUND_5391(tx_queue->efx) && (dma_addr & 0xf))
3568c2ecf20Sopenharmony_ci		len = min_t(unsigned int, len, 512 - (dma_addr & 0xf));
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	return len;
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci/* Allocate hardware resources for a TX queue */
3638c2ecf20Sopenharmony_ciint ef4_farch_tx_probe(struct ef4_tx_queue *tx_queue)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct ef4_nic *efx = tx_queue->efx;
3668c2ecf20Sopenharmony_ci	unsigned entries;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	entries = tx_queue->ptr_mask + 1;
3698c2ecf20Sopenharmony_ci	return ef4_alloc_special_buffer(efx, &tx_queue->txd,
3708c2ecf20Sopenharmony_ci					entries * sizeof(ef4_qword_t));
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_civoid ef4_farch_tx_init(struct ef4_tx_queue *tx_queue)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct ef4_nic *efx = tx_queue->efx;
3768c2ecf20Sopenharmony_ci	ef4_oword_t reg;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* Pin TX descriptor ring */
3798c2ecf20Sopenharmony_ci	ef4_init_special_buffer(efx, &tx_queue->txd);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/* Push TX descriptor ring to card */
3828c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_10(reg,
3838c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_EN, 1,
3848c2ecf20Sopenharmony_ci			      FRF_AZ_TX_ISCSI_DDIG_EN, 0,
3858c2ecf20Sopenharmony_ci			      FRF_AZ_TX_ISCSI_HDIG_EN, 0,
3868c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
3878c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_EVQ_ID,
3888c2ecf20Sopenharmony_ci			      tx_queue->channel->channel,
3898c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_OWNER_ID, 0,
3908c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_LABEL, tx_queue->queue,
3918c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_SIZE,
3928c2ecf20Sopenharmony_ci			      __ffs(tx_queue->txd.entries),
3938c2ecf20Sopenharmony_ci			      FRF_AZ_TX_DESCQ_TYPE, 0,
3948c2ecf20Sopenharmony_ci			      FRF_BZ_TX_NON_IP_DROP_DIS, 1);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) {
3978c2ecf20Sopenharmony_ci		int csum = tx_queue->queue & EF4_TXQ_TYPE_OFFLOAD;
3988c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(reg, FRF_BZ_TX_IP_CHKSM_DIS, !csum);
3998c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(reg, FRF_BZ_TX_TCP_CHKSM_DIS,
4008c2ecf20Sopenharmony_ci				    !csum);
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &reg, efx->type->txd_ptr_tbl_base,
4048c2ecf20Sopenharmony_ci			 tx_queue->queue);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) < EF4_REV_FALCON_B0) {
4078c2ecf20Sopenharmony_ci		/* Only 128 bits in this register */
4088c2ecf20Sopenharmony_ci		BUILD_BUG_ON(EF4_MAX_TX_QUEUES > 128);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci		ef4_reado(efx, &reg, FR_AA_TX_CHKSM_CFG);
4118c2ecf20Sopenharmony_ci		if (tx_queue->queue & EF4_TXQ_TYPE_OFFLOAD)
4128c2ecf20Sopenharmony_ci			__clear_bit_le(tx_queue->queue, &reg);
4138c2ecf20Sopenharmony_ci		else
4148c2ecf20Sopenharmony_ci			__set_bit_le(tx_queue->queue, &reg);
4158c2ecf20Sopenharmony_ci		ef4_writeo(efx, &reg, FR_AA_TX_CHKSM_CFG);
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) {
4198c2ecf20Sopenharmony_ci		EF4_POPULATE_OWORD_1(reg,
4208c2ecf20Sopenharmony_ci				     FRF_BZ_TX_PACE,
4218c2ecf20Sopenharmony_ci				     (tx_queue->queue & EF4_TXQ_TYPE_HIGHPRI) ?
4228c2ecf20Sopenharmony_ci				     FFE_BZ_TX_PACE_OFF :
4238c2ecf20Sopenharmony_ci				     FFE_BZ_TX_PACE_RESERVED);
4248c2ecf20Sopenharmony_ci		ef4_writeo_table(efx, &reg, FR_BZ_TX_PACE_TBL,
4258c2ecf20Sopenharmony_ci				 tx_queue->queue);
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic void ef4_farch_flush_tx_queue(struct ef4_tx_queue *tx_queue)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct ef4_nic *efx = tx_queue->efx;
4328c2ecf20Sopenharmony_ci	ef4_oword_t tx_flush_descq;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	WARN_ON(atomic_read(&tx_queue->flush_outstanding));
4358c2ecf20Sopenharmony_ci	atomic_set(&tx_queue->flush_outstanding, 1);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_2(tx_flush_descq,
4388c2ecf20Sopenharmony_ci			     FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
4398c2ecf20Sopenharmony_ci			     FRF_AZ_TX_FLUSH_DESCQ, tx_queue->queue);
4408c2ecf20Sopenharmony_ci	ef4_writeo(efx, &tx_flush_descq, FR_AZ_TX_FLUSH_DESCQ);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_civoid ef4_farch_tx_fini(struct ef4_tx_queue *tx_queue)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct ef4_nic *efx = tx_queue->efx;
4468c2ecf20Sopenharmony_ci	ef4_oword_t tx_desc_ptr;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	/* Remove TX descriptor ring from card */
4498c2ecf20Sopenharmony_ci	EF4_ZERO_OWORD(tx_desc_ptr);
4508c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
4518c2ecf20Sopenharmony_ci			 tx_queue->queue);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/* Unpin TX descriptor ring */
4548c2ecf20Sopenharmony_ci	ef4_fini_special_buffer(efx, &tx_queue->txd);
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci/* Free buffers backing TX queue */
4588c2ecf20Sopenharmony_civoid ef4_farch_tx_remove(struct ef4_tx_queue *tx_queue)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	ef4_free_special_buffer(tx_queue->efx, &tx_queue->txd);
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci/**************************************************************************
4648c2ecf20Sopenharmony_ci *
4658c2ecf20Sopenharmony_ci * RX path
4668c2ecf20Sopenharmony_ci *
4678c2ecf20Sopenharmony_ci **************************************************************************/
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/* This creates an entry in the RX descriptor queue */
4708c2ecf20Sopenharmony_cistatic inline void
4718c2ecf20Sopenharmony_cief4_farch_build_rx_desc(struct ef4_rx_queue *rx_queue, unsigned index)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	struct ef4_rx_buffer *rx_buf;
4748c2ecf20Sopenharmony_ci	ef4_qword_t *rxd;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	rxd = ef4_rx_desc(rx_queue, index);
4778c2ecf20Sopenharmony_ci	rx_buf = ef4_rx_buffer(rx_queue, index);
4788c2ecf20Sopenharmony_ci	EF4_POPULATE_QWORD_3(*rxd,
4798c2ecf20Sopenharmony_ci			     FSF_AZ_RX_KER_BUF_SIZE,
4808c2ecf20Sopenharmony_ci			     rx_buf->len -
4818c2ecf20Sopenharmony_ci			     rx_queue->efx->type->rx_buffer_padding,
4828c2ecf20Sopenharmony_ci			     FSF_AZ_RX_KER_BUF_REGION, 0,
4838c2ecf20Sopenharmony_ci			     FSF_AZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* This writes to the RX_DESC_WPTR register for the specified receive
4878c2ecf20Sopenharmony_ci * descriptor ring.
4888c2ecf20Sopenharmony_ci */
4898c2ecf20Sopenharmony_civoid ef4_farch_rx_write(struct ef4_rx_queue *rx_queue)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
4928c2ecf20Sopenharmony_ci	ef4_dword_t reg;
4938c2ecf20Sopenharmony_ci	unsigned write_ptr;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	while (rx_queue->notified_count != rx_queue->added_count) {
4968c2ecf20Sopenharmony_ci		ef4_farch_build_rx_desc(
4978c2ecf20Sopenharmony_ci			rx_queue,
4988c2ecf20Sopenharmony_ci			rx_queue->notified_count & rx_queue->ptr_mask);
4998c2ecf20Sopenharmony_ci		++rx_queue->notified_count;
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	wmb();
5038c2ecf20Sopenharmony_ci	write_ptr = rx_queue->added_count & rx_queue->ptr_mask;
5048c2ecf20Sopenharmony_ci	EF4_POPULATE_DWORD_1(reg, FRF_AZ_RX_DESC_WPTR_DWORD, write_ptr);
5058c2ecf20Sopenharmony_ci	ef4_writed_page(efx, &reg, FR_AZ_RX_DESC_UPD_DWORD_P0,
5068c2ecf20Sopenharmony_ci			ef4_rx_queue_index(rx_queue));
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ciint ef4_farch_rx_probe(struct ef4_rx_queue *rx_queue)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
5128c2ecf20Sopenharmony_ci	unsigned entries;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	entries = rx_queue->ptr_mask + 1;
5158c2ecf20Sopenharmony_ci	return ef4_alloc_special_buffer(efx, &rx_queue->rxd,
5168c2ecf20Sopenharmony_ci					entries * sizeof(ef4_qword_t));
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_civoid ef4_farch_rx_init(struct ef4_rx_queue *rx_queue)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	ef4_oword_t rx_desc_ptr;
5228c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
5238c2ecf20Sopenharmony_ci	bool is_b0 = ef4_nic_rev(efx) >= EF4_REV_FALCON_B0;
5248c2ecf20Sopenharmony_ci	bool iscsi_digest_en = is_b0;
5258c2ecf20Sopenharmony_ci	bool jumbo_en;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* For kernel-mode queues in Falcon A1, the JUMBO flag enables
5288c2ecf20Sopenharmony_ci	 * DMA to continue after a PCIe page boundary (and scattering
5298c2ecf20Sopenharmony_ci	 * is not possible).  In Falcon B0 and Siena, it enables
5308c2ecf20Sopenharmony_ci	 * scatter.
5318c2ecf20Sopenharmony_ci	 */
5328c2ecf20Sopenharmony_ci	jumbo_en = !is_b0 || efx->rx_scatter;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev,
5358c2ecf20Sopenharmony_ci		  "RX queue %d ring in special buffers %d-%d\n",
5368c2ecf20Sopenharmony_ci		  ef4_rx_queue_index(rx_queue), rx_queue->rxd.index,
5378c2ecf20Sopenharmony_ci		  rx_queue->rxd.index + rx_queue->rxd.entries - 1);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	rx_queue->scatter_n = 0;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* Pin RX descriptor ring */
5428c2ecf20Sopenharmony_ci	ef4_init_special_buffer(efx, &rx_queue->rxd);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/* Push RX descriptor ring to card */
5458c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_10(rx_desc_ptr,
5468c2ecf20Sopenharmony_ci			      FRF_AZ_RX_ISCSI_DDIG_EN, iscsi_digest_en,
5478c2ecf20Sopenharmony_ci			      FRF_AZ_RX_ISCSI_HDIG_EN, iscsi_digest_en,
5488c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
5498c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_EVQ_ID,
5508c2ecf20Sopenharmony_ci			      ef4_rx_queue_channel(rx_queue)->channel,
5518c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_OWNER_ID, 0,
5528c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_LABEL,
5538c2ecf20Sopenharmony_ci			      ef4_rx_queue_index(rx_queue),
5548c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_SIZE,
5558c2ecf20Sopenharmony_ci			      __ffs(rx_queue->rxd.entries),
5568c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_TYPE, 0 /* kernel queue */ ,
5578c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_JUMBO, jumbo_en,
5588c2ecf20Sopenharmony_ci			      FRF_AZ_RX_DESCQ_EN, 1);
5598c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
5608c2ecf20Sopenharmony_ci			 ef4_rx_queue_index(rx_queue));
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_cistatic void ef4_farch_flush_rx_queue(struct ef4_rx_queue *rx_queue)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
5668c2ecf20Sopenharmony_ci	ef4_oword_t rx_flush_descq;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_2(rx_flush_descq,
5698c2ecf20Sopenharmony_ci			     FRF_AZ_RX_FLUSH_DESCQ_CMD, 1,
5708c2ecf20Sopenharmony_ci			     FRF_AZ_RX_FLUSH_DESCQ,
5718c2ecf20Sopenharmony_ci			     ef4_rx_queue_index(rx_queue));
5728c2ecf20Sopenharmony_ci	ef4_writeo(efx, &rx_flush_descq, FR_AZ_RX_FLUSH_DESCQ);
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_civoid ef4_farch_rx_fini(struct ef4_rx_queue *rx_queue)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	ef4_oword_t rx_desc_ptr;
5788c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* Remove RX descriptor ring from card */
5818c2ecf20Sopenharmony_ci	EF4_ZERO_OWORD(rx_desc_ptr);
5828c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
5838c2ecf20Sopenharmony_ci			 ef4_rx_queue_index(rx_queue));
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* Unpin RX descriptor ring */
5868c2ecf20Sopenharmony_ci	ef4_fini_special_buffer(efx, &rx_queue->rxd);
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci/* Free buffers backing RX queue */
5908c2ecf20Sopenharmony_civoid ef4_farch_rx_remove(struct ef4_rx_queue *rx_queue)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	ef4_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
5938c2ecf20Sopenharmony_ci}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci/**************************************************************************
5968c2ecf20Sopenharmony_ci *
5978c2ecf20Sopenharmony_ci * Flush handling
5988c2ecf20Sopenharmony_ci *
5998c2ecf20Sopenharmony_ci **************************************************************************/
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci/* ef4_farch_flush_queues() must be woken up when all flushes are completed,
6028c2ecf20Sopenharmony_ci * or more RX flushes can be kicked off.
6038c2ecf20Sopenharmony_ci */
6048c2ecf20Sopenharmony_cistatic bool ef4_farch_flush_wake(struct ef4_nic *efx)
6058c2ecf20Sopenharmony_ci{
6068c2ecf20Sopenharmony_ci	/* Ensure that all updates are visible to ef4_farch_flush_queues() */
6078c2ecf20Sopenharmony_ci	smp_mb();
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	return (atomic_read(&efx->active_queues) == 0 ||
6108c2ecf20Sopenharmony_ci		(atomic_read(&efx->rxq_flush_outstanding) < EF4_RX_FLUSH_COUNT
6118c2ecf20Sopenharmony_ci		 && atomic_read(&efx->rxq_flush_pending) > 0));
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic bool ef4_check_tx_flush_complete(struct ef4_nic *efx)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	bool i = true;
6178c2ecf20Sopenharmony_ci	ef4_oword_t txd_ptr_tbl;
6188c2ecf20Sopenharmony_ci	struct ef4_channel *channel;
6198c2ecf20Sopenharmony_ci	struct ef4_tx_queue *tx_queue;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	ef4_for_each_channel(channel, efx) {
6228c2ecf20Sopenharmony_ci		ef4_for_each_channel_tx_queue(tx_queue, channel) {
6238c2ecf20Sopenharmony_ci			ef4_reado_table(efx, &txd_ptr_tbl,
6248c2ecf20Sopenharmony_ci					FR_BZ_TX_DESC_PTR_TBL, tx_queue->queue);
6258c2ecf20Sopenharmony_ci			if (EF4_OWORD_FIELD(txd_ptr_tbl,
6268c2ecf20Sopenharmony_ci					    FRF_AZ_TX_DESCQ_FLUSH) ||
6278c2ecf20Sopenharmony_ci			    EF4_OWORD_FIELD(txd_ptr_tbl,
6288c2ecf20Sopenharmony_ci					    FRF_AZ_TX_DESCQ_EN)) {
6298c2ecf20Sopenharmony_ci				netif_dbg(efx, hw, efx->net_dev,
6308c2ecf20Sopenharmony_ci					  "flush did not complete on TXQ %d\n",
6318c2ecf20Sopenharmony_ci					  tx_queue->queue);
6328c2ecf20Sopenharmony_ci				i = false;
6338c2ecf20Sopenharmony_ci			} else if (atomic_cmpxchg(&tx_queue->flush_outstanding,
6348c2ecf20Sopenharmony_ci						  1, 0)) {
6358c2ecf20Sopenharmony_ci				/* The flush is complete, but we didn't
6368c2ecf20Sopenharmony_ci				 * receive a flush completion event
6378c2ecf20Sopenharmony_ci				 */
6388c2ecf20Sopenharmony_ci				netif_dbg(efx, hw, efx->net_dev,
6398c2ecf20Sopenharmony_ci					  "flush complete on TXQ %d, so drain "
6408c2ecf20Sopenharmony_ci					  "the queue\n", tx_queue->queue);
6418c2ecf20Sopenharmony_ci				/* Don't need to increment active_queues as it
6428c2ecf20Sopenharmony_ci				 * has already been incremented for the queues
6438c2ecf20Sopenharmony_ci				 * which did not drain
6448c2ecf20Sopenharmony_ci				 */
6458c2ecf20Sopenharmony_ci				ef4_farch_magic_event(channel,
6468c2ecf20Sopenharmony_ci						      EF4_CHANNEL_MAGIC_TX_DRAIN(
6478c2ecf20Sopenharmony_ci							      tx_queue));
6488c2ecf20Sopenharmony_ci			}
6498c2ecf20Sopenharmony_ci		}
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	return i;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci/* Flush all the transmit queues, and continue flushing receive queues until
6568c2ecf20Sopenharmony_ci * they're all flushed. Wait for the DRAIN events to be received so that there
6578c2ecf20Sopenharmony_ci * are no more RX and TX events left on any channel. */
6588c2ecf20Sopenharmony_cistatic int ef4_farch_do_flush(struct ef4_nic *efx)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	unsigned timeout = msecs_to_jiffies(5000); /* 5s for all flushes and drains */
6618c2ecf20Sopenharmony_ci	struct ef4_channel *channel;
6628c2ecf20Sopenharmony_ci	struct ef4_rx_queue *rx_queue;
6638c2ecf20Sopenharmony_ci	struct ef4_tx_queue *tx_queue;
6648c2ecf20Sopenharmony_ci	int rc = 0;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	ef4_for_each_channel(channel, efx) {
6678c2ecf20Sopenharmony_ci		ef4_for_each_channel_tx_queue(tx_queue, channel) {
6688c2ecf20Sopenharmony_ci			ef4_farch_flush_tx_queue(tx_queue);
6698c2ecf20Sopenharmony_ci		}
6708c2ecf20Sopenharmony_ci		ef4_for_each_channel_rx_queue(rx_queue, channel) {
6718c2ecf20Sopenharmony_ci			rx_queue->flush_pending = true;
6728c2ecf20Sopenharmony_ci			atomic_inc(&efx->rxq_flush_pending);
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	while (timeout && atomic_read(&efx->active_queues) > 0) {
6778c2ecf20Sopenharmony_ci		/* The hardware supports four concurrent rx flushes, each of
6788c2ecf20Sopenharmony_ci		 * which may need to be retried if there is an outstanding
6798c2ecf20Sopenharmony_ci		 * descriptor fetch
6808c2ecf20Sopenharmony_ci		 */
6818c2ecf20Sopenharmony_ci		ef4_for_each_channel(channel, efx) {
6828c2ecf20Sopenharmony_ci			ef4_for_each_channel_rx_queue(rx_queue, channel) {
6838c2ecf20Sopenharmony_ci				if (atomic_read(&efx->rxq_flush_outstanding) >=
6848c2ecf20Sopenharmony_ci				    EF4_RX_FLUSH_COUNT)
6858c2ecf20Sopenharmony_ci					break;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci				if (rx_queue->flush_pending) {
6888c2ecf20Sopenharmony_ci					rx_queue->flush_pending = false;
6898c2ecf20Sopenharmony_ci					atomic_dec(&efx->rxq_flush_pending);
6908c2ecf20Sopenharmony_ci					atomic_inc(&efx->rxq_flush_outstanding);
6918c2ecf20Sopenharmony_ci					ef4_farch_flush_rx_queue(rx_queue);
6928c2ecf20Sopenharmony_ci				}
6938c2ecf20Sopenharmony_ci			}
6948c2ecf20Sopenharmony_ci		}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci		timeout = wait_event_timeout(efx->flush_wq,
6978c2ecf20Sopenharmony_ci					     ef4_farch_flush_wake(efx),
6988c2ecf20Sopenharmony_ci					     timeout);
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	if (atomic_read(&efx->active_queues) &&
7028c2ecf20Sopenharmony_ci	    !ef4_check_tx_flush_complete(efx)) {
7038c2ecf20Sopenharmony_ci		netif_err(efx, hw, efx->net_dev, "failed to flush %d queues "
7048c2ecf20Sopenharmony_ci			  "(rx %d+%d)\n", atomic_read(&efx->active_queues),
7058c2ecf20Sopenharmony_ci			  atomic_read(&efx->rxq_flush_outstanding),
7068c2ecf20Sopenharmony_ci			  atomic_read(&efx->rxq_flush_pending));
7078c2ecf20Sopenharmony_ci		rc = -ETIMEDOUT;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		atomic_set(&efx->active_queues, 0);
7108c2ecf20Sopenharmony_ci		atomic_set(&efx->rxq_flush_pending, 0);
7118c2ecf20Sopenharmony_ci		atomic_set(&efx->rxq_flush_outstanding, 0);
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return rc;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ciint ef4_farch_fini_dmaq(struct ef4_nic *efx)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct ef4_channel *channel;
7208c2ecf20Sopenharmony_ci	struct ef4_tx_queue *tx_queue;
7218c2ecf20Sopenharmony_ci	struct ef4_rx_queue *rx_queue;
7228c2ecf20Sopenharmony_ci	int rc = 0;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* Do not attempt to write to the NIC during EEH recovery */
7258c2ecf20Sopenharmony_ci	if (efx->state != STATE_RECOVERY) {
7268c2ecf20Sopenharmony_ci		/* Only perform flush if DMA is enabled */
7278c2ecf20Sopenharmony_ci		if (efx->pci_dev->is_busmaster) {
7288c2ecf20Sopenharmony_ci			efx->type->prepare_flush(efx);
7298c2ecf20Sopenharmony_ci			rc = ef4_farch_do_flush(efx);
7308c2ecf20Sopenharmony_ci			efx->type->finish_flush(efx);
7318c2ecf20Sopenharmony_ci		}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		ef4_for_each_channel(channel, efx) {
7348c2ecf20Sopenharmony_ci			ef4_for_each_channel_rx_queue(rx_queue, channel)
7358c2ecf20Sopenharmony_ci				ef4_farch_rx_fini(rx_queue);
7368c2ecf20Sopenharmony_ci			ef4_for_each_channel_tx_queue(tx_queue, channel)
7378c2ecf20Sopenharmony_ci				ef4_farch_tx_fini(tx_queue);
7388c2ecf20Sopenharmony_ci		}
7398c2ecf20Sopenharmony_ci	}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	return rc;
7428c2ecf20Sopenharmony_ci}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci/* Reset queue and flush accounting after FLR
7458c2ecf20Sopenharmony_ci *
7468c2ecf20Sopenharmony_ci * One possible cause of FLR recovery is that DMA may be failing (eg. if bus
7478c2ecf20Sopenharmony_ci * mastering was disabled), in which case we don't receive (RXQ) flush
7488c2ecf20Sopenharmony_ci * completion events.  This means that efx->rxq_flush_outstanding remained at 4
7498c2ecf20Sopenharmony_ci * after the FLR; also, efx->active_queues was non-zero (as no flush completion
7508c2ecf20Sopenharmony_ci * events were received, and we didn't go through ef4_check_tx_flush_complete())
7518c2ecf20Sopenharmony_ci * If we don't fix this up, on the next call to ef4_realloc_channels() we won't
7528c2ecf20Sopenharmony_ci * flush any RX queues because efx->rxq_flush_outstanding is at the limit of 4
7538c2ecf20Sopenharmony_ci * for batched flush requests; and the efx->active_queues gets messed up because
7548c2ecf20Sopenharmony_ci * we keep incrementing for the newly initialised queues, but it never went to
7558c2ecf20Sopenharmony_ci * zero previously.  Then we get a timeout every time we try to restart the
7568c2ecf20Sopenharmony_ci * queues, as it doesn't go back to zero when we should be flushing the queues.
7578c2ecf20Sopenharmony_ci */
7588c2ecf20Sopenharmony_civoid ef4_farch_finish_flr(struct ef4_nic *efx)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	atomic_set(&efx->rxq_flush_pending, 0);
7618c2ecf20Sopenharmony_ci	atomic_set(&efx->rxq_flush_outstanding, 0);
7628c2ecf20Sopenharmony_ci	atomic_set(&efx->active_queues, 0);
7638c2ecf20Sopenharmony_ci}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci/**************************************************************************
7678c2ecf20Sopenharmony_ci *
7688c2ecf20Sopenharmony_ci * Event queue processing
7698c2ecf20Sopenharmony_ci * Event queues are processed by per-channel tasklets.
7708c2ecf20Sopenharmony_ci *
7718c2ecf20Sopenharmony_ci **************************************************************************/
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci/* Update a channel's event queue's read pointer (RPTR) register
7748c2ecf20Sopenharmony_ci *
7758c2ecf20Sopenharmony_ci * This writes the EVQ_RPTR_REG register for the specified channel's
7768c2ecf20Sopenharmony_ci * event queue.
7778c2ecf20Sopenharmony_ci */
7788c2ecf20Sopenharmony_civoid ef4_farch_ev_read_ack(struct ef4_channel *channel)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	ef4_dword_t reg;
7818c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	EF4_POPULATE_DWORD_1(reg, FRF_AZ_EVQ_RPTR,
7848c2ecf20Sopenharmony_ci			     channel->eventq_read_ptr & channel->eventq_mask);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/* For Falcon A1, EVQ_RPTR_KER is documented as having a step size
7878c2ecf20Sopenharmony_ci	 * of 4 bytes, but it is really 16 bytes just like later revisions.
7888c2ecf20Sopenharmony_ci	 */
7898c2ecf20Sopenharmony_ci	ef4_writed(efx, &reg,
7908c2ecf20Sopenharmony_ci		   efx->type->evq_rptr_tbl_base +
7918c2ecf20Sopenharmony_ci		   FR_BZ_EVQ_RPTR_STEP * channel->channel);
7928c2ecf20Sopenharmony_ci}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci/* Use HW to insert a SW defined event */
7958c2ecf20Sopenharmony_civoid ef4_farch_generate_event(struct ef4_nic *efx, unsigned int evq,
7968c2ecf20Sopenharmony_ci			      ef4_qword_t *event)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	ef4_oword_t drv_ev_reg;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	BUILD_BUG_ON(FRF_AZ_DRV_EV_DATA_LBN != 0 ||
8018c2ecf20Sopenharmony_ci		     FRF_AZ_DRV_EV_DATA_WIDTH != 64);
8028c2ecf20Sopenharmony_ci	drv_ev_reg.u32[0] = event->u32[0];
8038c2ecf20Sopenharmony_ci	drv_ev_reg.u32[1] = event->u32[1];
8048c2ecf20Sopenharmony_ci	drv_ev_reg.u32[2] = 0;
8058c2ecf20Sopenharmony_ci	drv_ev_reg.u32[3] = 0;
8068c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(drv_ev_reg, FRF_AZ_DRV_EV_QID, evq);
8078c2ecf20Sopenharmony_ci	ef4_writeo(efx, &drv_ev_reg, FR_AZ_DRV_EV);
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic void ef4_farch_magic_event(struct ef4_channel *channel, u32 magic)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	ef4_qword_t event;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	EF4_POPULATE_QWORD_2(event, FSF_AZ_EV_CODE,
8158c2ecf20Sopenharmony_ci			     FSE_AZ_EV_CODE_DRV_GEN_EV,
8168c2ecf20Sopenharmony_ci			     FSF_AZ_DRV_GEN_EV_MAGIC, magic);
8178c2ecf20Sopenharmony_ci	ef4_farch_generate_event(channel->efx, channel->channel, &event);
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci/* Handle a transmit completion event
8218c2ecf20Sopenharmony_ci *
8228c2ecf20Sopenharmony_ci * The NIC batches TX completion events; the message we receive is of
8238c2ecf20Sopenharmony_ci * the form "complete all TX events up to this index".
8248c2ecf20Sopenharmony_ci */
8258c2ecf20Sopenharmony_cistatic int
8268c2ecf20Sopenharmony_cief4_farch_handle_tx_event(struct ef4_channel *channel, ef4_qword_t *event)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	unsigned int tx_ev_desc_ptr;
8298c2ecf20Sopenharmony_ci	unsigned int tx_ev_q_label;
8308c2ecf20Sopenharmony_ci	struct ef4_tx_queue *tx_queue;
8318c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
8328c2ecf20Sopenharmony_ci	int tx_packets = 0;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	if (unlikely(READ_ONCE(efx->reset_pending)))
8358c2ecf20Sopenharmony_ci		return 0;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	if (likely(EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
8388c2ecf20Sopenharmony_ci		/* Transmit completion */
8398c2ecf20Sopenharmony_ci		tx_ev_desc_ptr = EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
8408c2ecf20Sopenharmony_ci		tx_ev_q_label = EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
8418c2ecf20Sopenharmony_ci		tx_queue = ef4_channel_get_tx_queue(
8428c2ecf20Sopenharmony_ci			channel, tx_ev_q_label % EF4_TXQ_TYPES);
8438c2ecf20Sopenharmony_ci		tx_packets = ((tx_ev_desc_ptr - tx_queue->read_count) &
8448c2ecf20Sopenharmony_ci			      tx_queue->ptr_mask);
8458c2ecf20Sopenharmony_ci		ef4_xmit_done(tx_queue, tx_ev_desc_ptr);
8468c2ecf20Sopenharmony_ci	} else if (EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
8478c2ecf20Sopenharmony_ci		/* Rewrite the FIFO write pointer */
8488c2ecf20Sopenharmony_ci		tx_ev_q_label = EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
8498c2ecf20Sopenharmony_ci		tx_queue = ef4_channel_get_tx_queue(
8508c2ecf20Sopenharmony_ci			channel, tx_ev_q_label % EF4_TXQ_TYPES);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		netif_tx_lock(efx->net_dev);
8538c2ecf20Sopenharmony_ci		ef4_farch_notify_tx_desc(tx_queue);
8548c2ecf20Sopenharmony_ci		netif_tx_unlock(efx->net_dev);
8558c2ecf20Sopenharmony_ci	} else if (EF4_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR)) {
8568c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
8578c2ecf20Sopenharmony_ci	} else {
8588c2ecf20Sopenharmony_ci		netif_err(efx, tx_err, efx->net_dev,
8598c2ecf20Sopenharmony_ci			  "channel %d unexpected TX event "
8608c2ecf20Sopenharmony_ci			  EF4_QWORD_FMT"\n", channel->channel,
8618c2ecf20Sopenharmony_ci			  EF4_QWORD_VAL(*event));
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	return tx_packets;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci/* Detect errors included in the rx_evt_pkt_ok bit. */
8688c2ecf20Sopenharmony_cistatic u16 ef4_farch_handle_rx_not_ok(struct ef4_rx_queue *rx_queue,
8698c2ecf20Sopenharmony_ci				      const ef4_qword_t *event)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	struct ef4_channel *channel = ef4_rx_queue_channel(rx_queue);
8728c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
8738c2ecf20Sopenharmony_ci	bool __maybe_unused rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
8748c2ecf20Sopenharmony_ci	bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
8758c2ecf20Sopenharmony_ci	bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
8768c2ecf20Sopenharmony_ci	bool rx_ev_pause_frm;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	rx_ev_tobe_disc = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
8798c2ecf20Sopenharmony_ci	rx_ev_buf_owner_id_err = EF4_QWORD_FIELD(*event,
8808c2ecf20Sopenharmony_ci						 FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
8818c2ecf20Sopenharmony_ci	rx_ev_ip_hdr_chksum_err = EF4_QWORD_FIELD(*event,
8828c2ecf20Sopenharmony_ci						  FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
8838c2ecf20Sopenharmony_ci	rx_ev_tcp_udp_chksum_err = EF4_QWORD_FIELD(*event,
8848c2ecf20Sopenharmony_ci						   FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
8858c2ecf20Sopenharmony_ci	rx_ev_eth_crc_err = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
8868c2ecf20Sopenharmony_ci	rx_ev_frm_trunc = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
8878c2ecf20Sopenharmony_ci	rx_ev_drib_nib = ((ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) ?
8888c2ecf20Sopenharmony_ci			  0 : EF4_QWORD_FIELD(*event, FSF_AA_RX_EV_DRIB_NIB));
8898c2ecf20Sopenharmony_ci	rx_ev_pause_frm = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* Count errors that are not in MAC stats.  Ignore expected
8938c2ecf20Sopenharmony_ci	 * checksum errors during self-test. */
8948c2ecf20Sopenharmony_ci	if (rx_ev_frm_trunc)
8958c2ecf20Sopenharmony_ci		++channel->n_rx_frm_trunc;
8968c2ecf20Sopenharmony_ci	else if (rx_ev_tobe_disc)
8978c2ecf20Sopenharmony_ci		++channel->n_rx_tobe_disc;
8988c2ecf20Sopenharmony_ci	else if (!efx->loopback_selftest) {
8998c2ecf20Sopenharmony_ci		if (rx_ev_ip_hdr_chksum_err)
9008c2ecf20Sopenharmony_ci			++channel->n_rx_ip_hdr_chksum_err;
9018c2ecf20Sopenharmony_ci		else if (rx_ev_tcp_udp_chksum_err)
9028c2ecf20Sopenharmony_ci			++channel->n_rx_tcp_udp_chksum_err;
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	/* TOBE_DISC is expected on unicast mismatches; don't print out an
9068c2ecf20Sopenharmony_ci	 * error message.  FRM_TRUNC indicates RXDP dropped the packet due
9078c2ecf20Sopenharmony_ci	 * to a FIFO overflow.
9088c2ecf20Sopenharmony_ci	 */
9098c2ecf20Sopenharmony_ci#ifdef DEBUG
9108c2ecf20Sopenharmony_ci	{
9118c2ecf20Sopenharmony_ci	/* Every error apart from tobe_disc and pause_frm */
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	bool rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
9148c2ecf20Sopenharmony_ci				rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
9158c2ecf20Sopenharmony_ci				rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	if (rx_ev_other_err && net_ratelimit()) {
9188c2ecf20Sopenharmony_ci		netif_dbg(efx, rx_err, efx->net_dev,
9198c2ecf20Sopenharmony_ci			  " RX queue %d unexpected RX event "
9208c2ecf20Sopenharmony_ci			  EF4_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
9218c2ecf20Sopenharmony_ci			  ef4_rx_queue_index(rx_queue), EF4_QWORD_VAL(*event),
9228c2ecf20Sopenharmony_ci			  rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
9238c2ecf20Sopenharmony_ci			  rx_ev_ip_hdr_chksum_err ?
9248c2ecf20Sopenharmony_ci			  " [IP_HDR_CHKSUM_ERR]" : "",
9258c2ecf20Sopenharmony_ci			  rx_ev_tcp_udp_chksum_err ?
9268c2ecf20Sopenharmony_ci			  " [TCP_UDP_CHKSUM_ERR]" : "",
9278c2ecf20Sopenharmony_ci			  rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
9288c2ecf20Sopenharmony_ci			  rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
9298c2ecf20Sopenharmony_ci			  rx_ev_drib_nib ? " [DRIB_NIB]" : "",
9308c2ecf20Sopenharmony_ci			  rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
9318c2ecf20Sopenharmony_ci			  rx_ev_pause_frm ? " [PAUSE]" : "");
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci	}
9348c2ecf20Sopenharmony_ci#endif
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	/* The frame must be discarded if any of these are true. */
9378c2ecf20Sopenharmony_ci	return (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
9388c2ecf20Sopenharmony_ci		rx_ev_tobe_disc | rx_ev_pause_frm) ?
9398c2ecf20Sopenharmony_ci		EF4_RX_PKT_DISCARD : 0;
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci/* Handle receive events that are not in-order. Return true if this
9438c2ecf20Sopenharmony_ci * can be handled as a partial packet discard, false if it's more
9448c2ecf20Sopenharmony_ci * serious.
9458c2ecf20Sopenharmony_ci */
9468c2ecf20Sopenharmony_cistatic bool
9478c2ecf20Sopenharmony_cief4_farch_handle_rx_bad_index(struct ef4_rx_queue *rx_queue, unsigned index)
9488c2ecf20Sopenharmony_ci{
9498c2ecf20Sopenharmony_ci	struct ef4_channel *channel = ef4_rx_queue_channel(rx_queue);
9508c2ecf20Sopenharmony_ci	struct ef4_nic *efx = rx_queue->efx;
9518c2ecf20Sopenharmony_ci	unsigned expected, dropped;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	if (rx_queue->scatter_n &&
9548c2ecf20Sopenharmony_ci	    index == ((rx_queue->removed_count + rx_queue->scatter_n - 1) &
9558c2ecf20Sopenharmony_ci		      rx_queue->ptr_mask)) {
9568c2ecf20Sopenharmony_ci		++channel->n_rx_nodesc_trunc;
9578c2ecf20Sopenharmony_ci		return true;
9588c2ecf20Sopenharmony_ci	}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	expected = rx_queue->removed_count & rx_queue->ptr_mask;
9618c2ecf20Sopenharmony_ci	dropped = (index - expected) & rx_queue->ptr_mask;
9628c2ecf20Sopenharmony_ci	netif_info(efx, rx_err, efx->net_dev,
9638c2ecf20Sopenharmony_ci		   "dropped %d events (index=%d expected=%d)\n",
9648c2ecf20Sopenharmony_ci		   dropped, index, expected);
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	ef4_schedule_reset(efx, EF4_WORKAROUND_5676(efx) ?
9678c2ecf20Sopenharmony_ci			   RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
9688c2ecf20Sopenharmony_ci	return false;
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci/* Handle a packet received event
9728c2ecf20Sopenharmony_ci *
9738c2ecf20Sopenharmony_ci * The NIC gives a "discard" flag if it's a unicast packet with the
9748c2ecf20Sopenharmony_ci * wrong destination address
9758c2ecf20Sopenharmony_ci * Also "is multicast" and "matches multicast filter" flags can be used to
9768c2ecf20Sopenharmony_ci * discard non-matching multicast packets.
9778c2ecf20Sopenharmony_ci */
9788c2ecf20Sopenharmony_cistatic void
9798c2ecf20Sopenharmony_cief4_farch_handle_rx_event(struct ef4_channel *channel, const ef4_qword_t *event)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
9828c2ecf20Sopenharmony_ci	unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
9838c2ecf20Sopenharmony_ci	unsigned expected_ptr;
9848c2ecf20Sopenharmony_ci	bool rx_ev_pkt_ok, rx_ev_sop, rx_ev_cont;
9858c2ecf20Sopenharmony_ci	u16 flags;
9868c2ecf20Sopenharmony_ci	struct ef4_rx_queue *rx_queue;
9878c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	if (unlikely(READ_ONCE(efx->reset_pending)))
9908c2ecf20Sopenharmony_ci		return;
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	rx_ev_cont = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT);
9938c2ecf20Sopenharmony_ci	rx_ev_sop = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP);
9948c2ecf20Sopenharmony_ci	WARN_ON(EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
9958c2ecf20Sopenharmony_ci		channel->channel);
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	rx_queue = ef4_channel_get_rx_queue(channel);
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	rx_ev_desc_ptr = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
10008c2ecf20Sopenharmony_ci	expected_ptr = ((rx_queue->removed_count + rx_queue->scatter_n) &
10018c2ecf20Sopenharmony_ci			rx_queue->ptr_mask);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	/* Check for partial drops and other errors */
10048c2ecf20Sopenharmony_ci	if (unlikely(rx_ev_desc_ptr != expected_ptr) ||
10058c2ecf20Sopenharmony_ci	    unlikely(rx_ev_sop != (rx_queue->scatter_n == 0))) {
10068c2ecf20Sopenharmony_ci		if (rx_ev_desc_ptr != expected_ptr &&
10078c2ecf20Sopenharmony_ci		    !ef4_farch_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr))
10088c2ecf20Sopenharmony_ci			return;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci		/* Discard all pending fragments */
10118c2ecf20Sopenharmony_ci		if (rx_queue->scatter_n) {
10128c2ecf20Sopenharmony_ci			ef4_rx_packet(
10138c2ecf20Sopenharmony_ci				rx_queue,
10148c2ecf20Sopenharmony_ci				rx_queue->removed_count & rx_queue->ptr_mask,
10158c2ecf20Sopenharmony_ci				rx_queue->scatter_n, 0, EF4_RX_PKT_DISCARD);
10168c2ecf20Sopenharmony_ci			rx_queue->removed_count += rx_queue->scatter_n;
10178c2ecf20Sopenharmony_ci			rx_queue->scatter_n = 0;
10188c2ecf20Sopenharmony_ci		}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci		/* Return if there is no new fragment */
10218c2ecf20Sopenharmony_ci		if (rx_ev_desc_ptr != expected_ptr)
10228c2ecf20Sopenharmony_ci			return;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci		/* Discard new fragment if not SOP */
10258c2ecf20Sopenharmony_ci		if (!rx_ev_sop) {
10268c2ecf20Sopenharmony_ci			ef4_rx_packet(
10278c2ecf20Sopenharmony_ci				rx_queue,
10288c2ecf20Sopenharmony_ci				rx_queue->removed_count & rx_queue->ptr_mask,
10298c2ecf20Sopenharmony_ci				1, 0, EF4_RX_PKT_DISCARD);
10308c2ecf20Sopenharmony_ci			++rx_queue->removed_count;
10318c2ecf20Sopenharmony_ci			return;
10328c2ecf20Sopenharmony_ci		}
10338c2ecf20Sopenharmony_ci	}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	++rx_queue->scatter_n;
10368c2ecf20Sopenharmony_ci	if (rx_ev_cont)
10378c2ecf20Sopenharmony_ci		return;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	rx_ev_byte_cnt = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
10408c2ecf20Sopenharmony_ci	rx_ev_pkt_ok = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
10418c2ecf20Sopenharmony_ci	rx_ev_hdr_type = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	if (likely(rx_ev_pkt_ok)) {
10448c2ecf20Sopenharmony_ci		/* If packet is marked as OK then we can rely on the
10458c2ecf20Sopenharmony_ci		 * hardware checksum and classification.
10468c2ecf20Sopenharmony_ci		 */
10478c2ecf20Sopenharmony_ci		flags = 0;
10488c2ecf20Sopenharmony_ci		switch (rx_ev_hdr_type) {
10498c2ecf20Sopenharmony_ci		case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
10508c2ecf20Sopenharmony_ci			flags |= EF4_RX_PKT_TCP;
10518c2ecf20Sopenharmony_ci			fallthrough;
10528c2ecf20Sopenharmony_ci		case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
10538c2ecf20Sopenharmony_ci			flags |= EF4_RX_PKT_CSUMMED;
10548c2ecf20Sopenharmony_ci			fallthrough;
10558c2ecf20Sopenharmony_ci		case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
10568c2ecf20Sopenharmony_ci		case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
10578c2ecf20Sopenharmony_ci			break;
10588c2ecf20Sopenharmony_ci		}
10598c2ecf20Sopenharmony_ci	} else {
10608c2ecf20Sopenharmony_ci		flags = ef4_farch_handle_rx_not_ok(rx_queue, event);
10618c2ecf20Sopenharmony_ci	}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	/* Detect multicast packets that didn't match the filter */
10648c2ecf20Sopenharmony_ci	rx_ev_mcast_pkt = EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
10658c2ecf20Sopenharmony_ci	if (rx_ev_mcast_pkt) {
10668c2ecf20Sopenharmony_ci		unsigned int rx_ev_mcast_hash_match =
10678c2ecf20Sopenharmony_ci			EF4_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci		if (unlikely(!rx_ev_mcast_hash_match)) {
10708c2ecf20Sopenharmony_ci			++channel->n_rx_mcast_mismatch;
10718c2ecf20Sopenharmony_ci			flags |= EF4_RX_PKT_DISCARD;
10728c2ecf20Sopenharmony_ci		}
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	channel->irq_mod_score += 2;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	/* Handle received packet */
10788c2ecf20Sopenharmony_ci	ef4_rx_packet(rx_queue,
10798c2ecf20Sopenharmony_ci		      rx_queue->removed_count & rx_queue->ptr_mask,
10808c2ecf20Sopenharmony_ci		      rx_queue->scatter_n, rx_ev_byte_cnt, flags);
10818c2ecf20Sopenharmony_ci	rx_queue->removed_count += rx_queue->scatter_n;
10828c2ecf20Sopenharmony_ci	rx_queue->scatter_n = 0;
10838c2ecf20Sopenharmony_ci}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci/* If this flush done event corresponds to a &struct ef4_tx_queue, then
10868c2ecf20Sopenharmony_ci * send an %EF4_CHANNEL_MAGIC_TX_DRAIN event to drain the event queue
10878c2ecf20Sopenharmony_ci * of all transmit completions.
10888c2ecf20Sopenharmony_ci */
10898c2ecf20Sopenharmony_cistatic void
10908c2ecf20Sopenharmony_cief4_farch_handle_tx_flush_done(struct ef4_nic *efx, ef4_qword_t *event)
10918c2ecf20Sopenharmony_ci{
10928c2ecf20Sopenharmony_ci	struct ef4_tx_queue *tx_queue;
10938c2ecf20Sopenharmony_ci	int qid;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	qid = EF4_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
10968c2ecf20Sopenharmony_ci	if (qid < EF4_TXQ_TYPES * efx->n_tx_channels) {
10978c2ecf20Sopenharmony_ci		tx_queue = ef4_get_tx_queue(efx, qid / EF4_TXQ_TYPES,
10988c2ecf20Sopenharmony_ci					    qid % EF4_TXQ_TYPES);
10998c2ecf20Sopenharmony_ci		if (atomic_cmpxchg(&tx_queue->flush_outstanding, 1, 0)) {
11008c2ecf20Sopenharmony_ci			ef4_farch_magic_event(tx_queue->channel,
11018c2ecf20Sopenharmony_ci					      EF4_CHANNEL_MAGIC_TX_DRAIN(tx_queue));
11028c2ecf20Sopenharmony_ci		}
11038c2ecf20Sopenharmony_ci	}
11048c2ecf20Sopenharmony_ci}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci/* If this flush done event corresponds to a &struct ef4_rx_queue: If the flush
11078c2ecf20Sopenharmony_ci * was successful then send an %EF4_CHANNEL_MAGIC_RX_DRAIN, otherwise add
11088c2ecf20Sopenharmony_ci * the RX queue back to the mask of RX queues in need of flushing.
11098c2ecf20Sopenharmony_ci */
11108c2ecf20Sopenharmony_cistatic void
11118c2ecf20Sopenharmony_cief4_farch_handle_rx_flush_done(struct ef4_nic *efx, ef4_qword_t *event)
11128c2ecf20Sopenharmony_ci{
11138c2ecf20Sopenharmony_ci	struct ef4_channel *channel;
11148c2ecf20Sopenharmony_ci	struct ef4_rx_queue *rx_queue;
11158c2ecf20Sopenharmony_ci	int qid;
11168c2ecf20Sopenharmony_ci	bool failed;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	qid = EF4_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
11198c2ecf20Sopenharmony_ci	failed = EF4_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
11208c2ecf20Sopenharmony_ci	if (qid >= efx->n_channels)
11218c2ecf20Sopenharmony_ci		return;
11228c2ecf20Sopenharmony_ci	channel = ef4_get_channel(efx, qid);
11238c2ecf20Sopenharmony_ci	if (!ef4_channel_has_rx_queue(channel))
11248c2ecf20Sopenharmony_ci		return;
11258c2ecf20Sopenharmony_ci	rx_queue = ef4_channel_get_rx_queue(channel);
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	if (failed) {
11288c2ecf20Sopenharmony_ci		netif_info(efx, hw, efx->net_dev,
11298c2ecf20Sopenharmony_ci			   "RXQ %d flush retry\n", qid);
11308c2ecf20Sopenharmony_ci		rx_queue->flush_pending = true;
11318c2ecf20Sopenharmony_ci		atomic_inc(&efx->rxq_flush_pending);
11328c2ecf20Sopenharmony_ci	} else {
11338c2ecf20Sopenharmony_ci		ef4_farch_magic_event(ef4_rx_queue_channel(rx_queue),
11348c2ecf20Sopenharmony_ci				      EF4_CHANNEL_MAGIC_RX_DRAIN(rx_queue));
11358c2ecf20Sopenharmony_ci	}
11368c2ecf20Sopenharmony_ci	atomic_dec(&efx->rxq_flush_outstanding);
11378c2ecf20Sopenharmony_ci	if (ef4_farch_flush_wake(efx))
11388c2ecf20Sopenharmony_ci		wake_up(&efx->flush_wq);
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_cistatic void
11428c2ecf20Sopenharmony_cief4_farch_handle_drain_event(struct ef4_channel *channel)
11438c2ecf20Sopenharmony_ci{
11448c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	WARN_ON(atomic_read(&efx->active_queues) == 0);
11478c2ecf20Sopenharmony_ci	atomic_dec(&efx->active_queues);
11488c2ecf20Sopenharmony_ci	if (ef4_farch_flush_wake(efx))
11498c2ecf20Sopenharmony_ci		wake_up(&efx->flush_wq);
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_cistatic void ef4_farch_handle_generated_event(struct ef4_channel *channel,
11538c2ecf20Sopenharmony_ci					     ef4_qword_t *event)
11548c2ecf20Sopenharmony_ci{
11558c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
11568c2ecf20Sopenharmony_ci	struct ef4_rx_queue *rx_queue =
11578c2ecf20Sopenharmony_ci		ef4_channel_has_rx_queue(channel) ?
11588c2ecf20Sopenharmony_ci		ef4_channel_get_rx_queue(channel) : NULL;
11598c2ecf20Sopenharmony_ci	unsigned magic, code;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	magic = EF4_QWORD_FIELD(*event, FSF_AZ_DRV_GEN_EV_MAGIC);
11628c2ecf20Sopenharmony_ci	code = _EF4_CHANNEL_MAGIC_CODE(magic);
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	if (magic == EF4_CHANNEL_MAGIC_TEST(channel)) {
11658c2ecf20Sopenharmony_ci		channel->event_test_cpu = raw_smp_processor_id();
11668c2ecf20Sopenharmony_ci	} else if (rx_queue && magic == EF4_CHANNEL_MAGIC_FILL(rx_queue)) {
11678c2ecf20Sopenharmony_ci		/* The queue must be empty, so we won't receive any rx
11688c2ecf20Sopenharmony_ci		 * events, so ef4_process_channel() won't refill the
11698c2ecf20Sopenharmony_ci		 * queue. Refill it here */
11708c2ecf20Sopenharmony_ci		ef4_fast_push_rx_descriptors(rx_queue, true);
11718c2ecf20Sopenharmony_ci	} else if (rx_queue && magic == EF4_CHANNEL_MAGIC_RX_DRAIN(rx_queue)) {
11728c2ecf20Sopenharmony_ci		ef4_farch_handle_drain_event(channel);
11738c2ecf20Sopenharmony_ci	} else if (code == _EF4_CHANNEL_MAGIC_TX_DRAIN) {
11748c2ecf20Sopenharmony_ci		ef4_farch_handle_drain_event(channel);
11758c2ecf20Sopenharmony_ci	} else {
11768c2ecf20Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev, "channel %d received "
11778c2ecf20Sopenharmony_ci			  "generated event "EF4_QWORD_FMT"\n",
11788c2ecf20Sopenharmony_ci			  channel->channel, EF4_QWORD_VAL(*event));
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic void
11838c2ecf20Sopenharmony_cief4_farch_handle_driver_event(struct ef4_channel *channel, ef4_qword_t *event)
11848c2ecf20Sopenharmony_ci{
11858c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
11868c2ecf20Sopenharmony_ci	unsigned int ev_sub_code;
11878c2ecf20Sopenharmony_ci	unsigned int ev_sub_data;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	ev_sub_code = EF4_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
11908c2ecf20Sopenharmony_ci	ev_sub_data = EF4_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	switch (ev_sub_code) {
11938c2ecf20Sopenharmony_ci	case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
11948c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev, "channel %d TXQ %d flushed\n",
11958c2ecf20Sopenharmony_ci			   channel->channel, ev_sub_data);
11968c2ecf20Sopenharmony_ci		ef4_farch_handle_tx_flush_done(efx, event);
11978c2ecf20Sopenharmony_ci		break;
11988c2ecf20Sopenharmony_ci	case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
11998c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev, "channel %d RXQ %d flushed\n",
12008c2ecf20Sopenharmony_ci			   channel->channel, ev_sub_data);
12018c2ecf20Sopenharmony_ci		ef4_farch_handle_rx_flush_done(efx, event);
12028c2ecf20Sopenharmony_ci		break;
12038c2ecf20Sopenharmony_ci	case FSE_AZ_EVQ_INIT_DONE_EV:
12048c2ecf20Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev,
12058c2ecf20Sopenharmony_ci			  "channel %d EVQ %d initialised\n",
12068c2ecf20Sopenharmony_ci			  channel->channel, ev_sub_data);
12078c2ecf20Sopenharmony_ci		break;
12088c2ecf20Sopenharmony_ci	case FSE_AZ_SRM_UPD_DONE_EV:
12098c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev,
12108c2ecf20Sopenharmony_ci			   "channel %d SRAM update done\n", channel->channel);
12118c2ecf20Sopenharmony_ci		break;
12128c2ecf20Sopenharmony_ci	case FSE_AZ_WAKE_UP_EV:
12138c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev,
12148c2ecf20Sopenharmony_ci			   "channel %d RXQ %d wakeup event\n",
12158c2ecf20Sopenharmony_ci			   channel->channel, ev_sub_data);
12168c2ecf20Sopenharmony_ci		break;
12178c2ecf20Sopenharmony_ci	case FSE_AZ_TIMER_EV:
12188c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev,
12198c2ecf20Sopenharmony_ci			   "channel %d RX queue %d timer expired\n",
12208c2ecf20Sopenharmony_ci			   channel->channel, ev_sub_data);
12218c2ecf20Sopenharmony_ci		break;
12228c2ecf20Sopenharmony_ci	case FSE_AA_RX_RECOVER_EV:
12238c2ecf20Sopenharmony_ci		netif_err(efx, rx_err, efx->net_dev,
12248c2ecf20Sopenharmony_ci			  "channel %d seen DRIVER RX_RESET event. "
12258c2ecf20Sopenharmony_ci			"Resetting.\n", channel->channel);
12268c2ecf20Sopenharmony_ci		atomic_inc(&efx->rx_reset);
12278c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx,
12288c2ecf20Sopenharmony_ci				   EF4_WORKAROUND_6555(efx) ?
12298c2ecf20Sopenharmony_ci				   RESET_TYPE_RX_RECOVERY :
12308c2ecf20Sopenharmony_ci				   RESET_TYPE_DISABLE);
12318c2ecf20Sopenharmony_ci		break;
12328c2ecf20Sopenharmony_ci	case FSE_BZ_RX_DSC_ERROR_EV:
12338c2ecf20Sopenharmony_ci		netif_err(efx, rx_err, efx->net_dev,
12348c2ecf20Sopenharmony_ci			  "RX DMA Q %d reports descriptor fetch error."
12358c2ecf20Sopenharmony_ci			  " RX Q %d is disabled.\n", ev_sub_data,
12368c2ecf20Sopenharmony_ci			  ev_sub_data);
12378c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
12388c2ecf20Sopenharmony_ci		break;
12398c2ecf20Sopenharmony_ci	case FSE_BZ_TX_DSC_ERROR_EV:
12408c2ecf20Sopenharmony_ci		netif_err(efx, tx_err, efx->net_dev,
12418c2ecf20Sopenharmony_ci			  "TX DMA Q %d reports descriptor fetch error."
12428c2ecf20Sopenharmony_ci			  " TX Q %d is disabled.\n", ev_sub_data,
12438c2ecf20Sopenharmony_ci			  ev_sub_data);
12448c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
12458c2ecf20Sopenharmony_ci		break;
12468c2ecf20Sopenharmony_ci	default:
12478c2ecf20Sopenharmony_ci		netif_vdbg(efx, hw, efx->net_dev,
12488c2ecf20Sopenharmony_ci			   "channel %d unknown driver event code %d "
12498c2ecf20Sopenharmony_ci			   "data %04x\n", channel->channel, ev_sub_code,
12508c2ecf20Sopenharmony_ci			   ev_sub_data);
12518c2ecf20Sopenharmony_ci		break;
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ciint ef4_farch_ev_process(struct ef4_channel *channel, int budget)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
12588c2ecf20Sopenharmony_ci	unsigned int read_ptr;
12598c2ecf20Sopenharmony_ci	ef4_qword_t event, *p_event;
12608c2ecf20Sopenharmony_ci	int ev_code;
12618c2ecf20Sopenharmony_ci	int tx_packets = 0;
12628c2ecf20Sopenharmony_ci	int spent = 0;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	if (budget <= 0)
12658c2ecf20Sopenharmony_ci		return spent;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	read_ptr = channel->eventq_read_ptr;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	for (;;) {
12708c2ecf20Sopenharmony_ci		p_event = ef4_event(channel, read_ptr);
12718c2ecf20Sopenharmony_ci		event = *p_event;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci		if (!ef4_event_present(&event))
12748c2ecf20Sopenharmony_ci			/* End of events */
12758c2ecf20Sopenharmony_ci			break;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci		netif_vdbg(channel->efx, intr, channel->efx->net_dev,
12788c2ecf20Sopenharmony_ci			   "channel %d event is "EF4_QWORD_FMT"\n",
12798c2ecf20Sopenharmony_ci			   channel->channel, EF4_QWORD_VAL(event));
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci		/* Clear this event by marking it all ones */
12828c2ecf20Sopenharmony_ci		EF4_SET_QWORD(*p_event);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci		++read_ptr;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci		ev_code = EF4_QWORD_FIELD(event, FSF_AZ_EV_CODE);
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci		switch (ev_code) {
12898c2ecf20Sopenharmony_ci		case FSE_AZ_EV_CODE_RX_EV:
12908c2ecf20Sopenharmony_ci			ef4_farch_handle_rx_event(channel, &event);
12918c2ecf20Sopenharmony_ci			if (++spent == budget)
12928c2ecf20Sopenharmony_ci				goto out;
12938c2ecf20Sopenharmony_ci			break;
12948c2ecf20Sopenharmony_ci		case FSE_AZ_EV_CODE_TX_EV:
12958c2ecf20Sopenharmony_ci			tx_packets += ef4_farch_handle_tx_event(channel,
12968c2ecf20Sopenharmony_ci								&event);
12978c2ecf20Sopenharmony_ci			if (tx_packets > efx->txq_entries) {
12988c2ecf20Sopenharmony_ci				spent = budget;
12998c2ecf20Sopenharmony_ci				goto out;
13008c2ecf20Sopenharmony_ci			}
13018c2ecf20Sopenharmony_ci			break;
13028c2ecf20Sopenharmony_ci		case FSE_AZ_EV_CODE_DRV_GEN_EV:
13038c2ecf20Sopenharmony_ci			ef4_farch_handle_generated_event(channel, &event);
13048c2ecf20Sopenharmony_ci			break;
13058c2ecf20Sopenharmony_ci		case FSE_AZ_EV_CODE_DRIVER_EV:
13068c2ecf20Sopenharmony_ci			ef4_farch_handle_driver_event(channel, &event);
13078c2ecf20Sopenharmony_ci			break;
13088c2ecf20Sopenharmony_ci		case FSE_AZ_EV_CODE_GLOBAL_EV:
13098c2ecf20Sopenharmony_ci			if (efx->type->handle_global_event &&
13108c2ecf20Sopenharmony_ci			    efx->type->handle_global_event(channel, &event))
13118c2ecf20Sopenharmony_ci				break;
13128c2ecf20Sopenharmony_ci			fallthrough;
13138c2ecf20Sopenharmony_ci		default:
13148c2ecf20Sopenharmony_ci			netif_err(channel->efx, hw, channel->efx->net_dev,
13158c2ecf20Sopenharmony_ci				  "channel %d unknown event type %d (data "
13168c2ecf20Sopenharmony_ci				  EF4_QWORD_FMT ")\n", channel->channel,
13178c2ecf20Sopenharmony_ci				  ev_code, EF4_QWORD_VAL(event));
13188c2ecf20Sopenharmony_ci		}
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ciout:
13228c2ecf20Sopenharmony_ci	channel->eventq_read_ptr = read_ptr;
13238c2ecf20Sopenharmony_ci	return spent;
13248c2ecf20Sopenharmony_ci}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci/* Allocate buffer table entries for event queue */
13278c2ecf20Sopenharmony_ciint ef4_farch_ev_probe(struct ef4_channel *channel)
13288c2ecf20Sopenharmony_ci{
13298c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
13308c2ecf20Sopenharmony_ci	unsigned entries;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	entries = channel->eventq_mask + 1;
13338c2ecf20Sopenharmony_ci	return ef4_alloc_special_buffer(efx, &channel->eventq,
13348c2ecf20Sopenharmony_ci					entries * sizeof(ef4_qword_t));
13358c2ecf20Sopenharmony_ci}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ciint ef4_farch_ev_init(struct ef4_channel *channel)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	ef4_oword_t reg;
13408c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev,
13438c2ecf20Sopenharmony_ci		  "channel %d event queue in special buffers %d-%d\n",
13448c2ecf20Sopenharmony_ci		  channel->channel, channel->eventq.index,
13458c2ecf20Sopenharmony_ci		  channel->eventq.index + channel->eventq.entries - 1);
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	/* Pin event queue buffer */
13488c2ecf20Sopenharmony_ci	ef4_init_special_buffer(efx, &channel->eventq);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	/* Fill event queue with all ones (i.e. empty events) */
13518c2ecf20Sopenharmony_ci	memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	/* Push event queue to card */
13548c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_3(reg,
13558c2ecf20Sopenharmony_ci			     FRF_AZ_EVQ_EN, 1,
13568c2ecf20Sopenharmony_ci			     FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
13578c2ecf20Sopenharmony_ci			     FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
13588c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
13598c2ecf20Sopenharmony_ci			 channel->channel);
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	return 0;
13628c2ecf20Sopenharmony_ci}
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_civoid ef4_farch_ev_fini(struct ef4_channel *channel)
13658c2ecf20Sopenharmony_ci{
13668c2ecf20Sopenharmony_ci	ef4_oword_t reg;
13678c2ecf20Sopenharmony_ci	struct ef4_nic *efx = channel->efx;
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	/* Remove event queue from card */
13708c2ecf20Sopenharmony_ci	EF4_ZERO_OWORD(reg);
13718c2ecf20Sopenharmony_ci	ef4_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
13728c2ecf20Sopenharmony_ci			 channel->channel);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	/* Unpin event queue */
13758c2ecf20Sopenharmony_ci	ef4_fini_special_buffer(efx, &channel->eventq);
13768c2ecf20Sopenharmony_ci}
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci/* Free buffers backing event queue */
13798c2ecf20Sopenharmony_civoid ef4_farch_ev_remove(struct ef4_channel *channel)
13808c2ecf20Sopenharmony_ci{
13818c2ecf20Sopenharmony_ci	ef4_free_special_buffer(channel->efx, &channel->eventq);
13828c2ecf20Sopenharmony_ci}
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_civoid ef4_farch_ev_test_generate(struct ef4_channel *channel)
13868c2ecf20Sopenharmony_ci{
13878c2ecf20Sopenharmony_ci	ef4_farch_magic_event(channel, EF4_CHANNEL_MAGIC_TEST(channel));
13888c2ecf20Sopenharmony_ci}
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_civoid ef4_farch_rx_defer_refill(struct ef4_rx_queue *rx_queue)
13918c2ecf20Sopenharmony_ci{
13928c2ecf20Sopenharmony_ci	ef4_farch_magic_event(ef4_rx_queue_channel(rx_queue),
13938c2ecf20Sopenharmony_ci			      EF4_CHANNEL_MAGIC_FILL(rx_queue));
13948c2ecf20Sopenharmony_ci}
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci/**************************************************************************
13978c2ecf20Sopenharmony_ci *
13988c2ecf20Sopenharmony_ci * Hardware interrupts
13998c2ecf20Sopenharmony_ci * The hardware interrupt handler does very little work; all the event
14008c2ecf20Sopenharmony_ci * queue processing is carried out by per-channel tasklets.
14018c2ecf20Sopenharmony_ci *
14028c2ecf20Sopenharmony_ci **************************************************************************/
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci/* Enable/disable/generate interrupts */
14058c2ecf20Sopenharmony_cistatic inline void ef4_farch_interrupts(struct ef4_nic *efx,
14068c2ecf20Sopenharmony_ci				      bool enabled, bool force)
14078c2ecf20Sopenharmony_ci{
14088c2ecf20Sopenharmony_ci	ef4_oword_t int_en_reg_ker;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_3(int_en_reg_ker,
14118c2ecf20Sopenharmony_ci			     FRF_AZ_KER_INT_LEVE_SEL, efx->irq_level,
14128c2ecf20Sopenharmony_ci			     FRF_AZ_KER_INT_KER, force,
14138c2ecf20Sopenharmony_ci			     FRF_AZ_DRV_INT_EN_KER, enabled);
14148c2ecf20Sopenharmony_ci	ef4_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
14158c2ecf20Sopenharmony_ci}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_civoid ef4_farch_irq_enable_master(struct ef4_nic *efx)
14188c2ecf20Sopenharmony_ci{
14198c2ecf20Sopenharmony_ci	EF4_ZERO_OWORD(*((ef4_oword_t *) efx->irq_status.addr));
14208c2ecf20Sopenharmony_ci	wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	ef4_farch_interrupts(efx, true, false);
14238c2ecf20Sopenharmony_ci}
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_civoid ef4_farch_irq_disable_master(struct ef4_nic *efx)
14268c2ecf20Sopenharmony_ci{
14278c2ecf20Sopenharmony_ci	/* Disable interrupts */
14288c2ecf20Sopenharmony_ci	ef4_farch_interrupts(efx, false, false);
14298c2ecf20Sopenharmony_ci}
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci/* Generate a test interrupt
14328c2ecf20Sopenharmony_ci * Interrupt must already have been enabled, otherwise nasty things
14338c2ecf20Sopenharmony_ci * may happen.
14348c2ecf20Sopenharmony_ci */
14358c2ecf20Sopenharmony_ciint ef4_farch_irq_test_generate(struct ef4_nic *efx)
14368c2ecf20Sopenharmony_ci{
14378c2ecf20Sopenharmony_ci	ef4_farch_interrupts(efx, true, true);
14388c2ecf20Sopenharmony_ci	return 0;
14398c2ecf20Sopenharmony_ci}
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci/* Process a fatal interrupt
14428c2ecf20Sopenharmony_ci * Disable bus mastering ASAP and schedule a reset
14438c2ecf20Sopenharmony_ci */
14448c2ecf20Sopenharmony_ciirqreturn_t ef4_farch_fatal_interrupt(struct ef4_nic *efx)
14458c2ecf20Sopenharmony_ci{
14468c2ecf20Sopenharmony_ci	struct falcon_nic_data *nic_data = efx->nic_data;
14478c2ecf20Sopenharmony_ci	ef4_oword_t *int_ker = efx->irq_status.addr;
14488c2ecf20Sopenharmony_ci	ef4_oword_t fatal_intr;
14498c2ecf20Sopenharmony_ci	int error, mem_perr;
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	ef4_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
14528c2ecf20Sopenharmony_ci	error = EF4_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	netif_err(efx, hw, efx->net_dev, "SYSTEM ERROR "EF4_OWORD_FMT" status "
14558c2ecf20Sopenharmony_ci		  EF4_OWORD_FMT ": %s\n", EF4_OWORD_VAL(*int_ker),
14568c2ecf20Sopenharmony_ci		  EF4_OWORD_VAL(fatal_intr),
14578c2ecf20Sopenharmony_ci		  error ? "disabling bus mastering" : "no recognised error");
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	/* If this is a memory parity error dump which blocks are offending */
14608c2ecf20Sopenharmony_ci	mem_perr = (EF4_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER) ||
14618c2ecf20Sopenharmony_ci		    EF4_OWORD_FIELD(fatal_intr, FRF_AZ_SRM_PERR_INT_KER));
14628c2ecf20Sopenharmony_ci	if (mem_perr) {
14638c2ecf20Sopenharmony_ci		ef4_oword_t reg;
14648c2ecf20Sopenharmony_ci		ef4_reado(efx, &reg, FR_AZ_MEM_STAT);
14658c2ecf20Sopenharmony_ci		netif_err(efx, hw, efx->net_dev,
14668c2ecf20Sopenharmony_ci			  "SYSTEM ERROR: memory parity error "EF4_OWORD_FMT"\n",
14678c2ecf20Sopenharmony_ci			  EF4_OWORD_VAL(reg));
14688c2ecf20Sopenharmony_ci	}
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	/* Disable both devices */
14718c2ecf20Sopenharmony_ci	pci_clear_master(efx->pci_dev);
14728c2ecf20Sopenharmony_ci	if (ef4_nic_is_dual_func(efx))
14738c2ecf20Sopenharmony_ci		pci_clear_master(nic_data->pci_dev2);
14748c2ecf20Sopenharmony_ci	ef4_farch_irq_disable_master(efx);
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	/* Count errors and reset or disable the NIC accordingly */
14778c2ecf20Sopenharmony_ci	if (efx->int_error_count == 0 ||
14788c2ecf20Sopenharmony_ci	    time_after(jiffies, efx->int_error_expire)) {
14798c2ecf20Sopenharmony_ci		efx->int_error_count = 0;
14808c2ecf20Sopenharmony_ci		efx->int_error_expire =
14818c2ecf20Sopenharmony_ci			jiffies + EF4_INT_ERROR_EXPIRE * HZ;
14828c2ecf20Sopenharmony_ci	}
14838c2ecf20Sopenharmony_ci	if (++efx->int_error_count < EF4_MAX_INT_ERRORS) {
14848c2ecf20Sopenharmony_ci		netif_err(efx, hw, efx->net_dev,
14858c2ecf20Sopenharmony_ci			  "SYSTEM ERROR - reset scheduled\n");
14868c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx, RESET_TYPE_INT_ERROR);
14878c2ecf20Sopenharmony_ci	} else {
14888c2ecf20Sopenharmony_ci		netif_err(efx, hw, efx->net_dev,
14898c2ecf20Sopenharmony_ci			  "SYSTEM ERROR - max number of errors seen."
14908c2ecf20Sopenharmony_ci			  "NIC will be disabled\n");
14918c2ecf20Sopenharmony_ci		ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
14928c2ecf20Sopenharmony_ci	}
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
14958c2ecf20Sopenharmony_ci}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci/* Handle a legacy interrupt
14988c2ecf20Sopenharmony_ci * Acknowledges the interrupt and schedule event queue processing.
14998c2ecf20Sopenharmony_ci */
15008c2ecf20Sopenharmony_ciirqreturn_t ef4_farch_legacy_interrupt(int irq, void *dev_id)
15018c2ecf20Sopenharmony_ci{
15028c2ecf20Sopenharmony_ci	struct ef4_nic *efx = dev_id;
15038c2ecf20Sopenharmony_ci	bool soft_enabled = READ_ONCE(efx->irq_soft_enabled);
15048c2ecf20Sopenharmony_ci	ef4_oword_t *int_ker = efx->irq_status.addr;
15058c2ecf20Sopenharmony_ci	irqreturn_t result = IRQ_NONE;
15068c2ecf20Sopenharmony_ci	struct ef4_channel *channel;
15078c2ecf20Sopenharmony_ci	ef4_dword_t reg;
15088c2ecf20Sopenharmony_ci	u32 queues;
15098c2ecf20Sopenharmony_ci	int syserr;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	/* Read the ISR which also ACKs the interrupts */
15128c2ecf20Sopenharmony_ci	ef4_readd(efx, &reg, FR_BZ_INT_ISR0);
15138c2ecf20Sopenharmony_ci	queues = EF4_EXTRACT_DWORD(reg, 0, 31);
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	/* Legacy interrupts are disabled too late by the EEH kernel
15168c2ecf20Sopenharmony_ci	 * code. Disable them earlier.
15178c2ecf20Sopenharmony_ci	 * If an EEH error occurred, the read will have returned all ones.
15188c2ecf20Sopenharmony_ci	 */
15198c2ecf20Sopenharmony_ci	if (EF4_DWORD_IS_ALL_ONES(reg) && ef4_try_recovery(efx) &&
15208c2ecf20Sopenharmony_ci	    !efx->eeh_disabled_legacy_irq) {
15218c2ecf20Sopenharmony_ci		disable_irq_nosync(efx->legacy_irq);
15228c2ecf20Sopenharmony_ci		efx->eeh_disabled_legacy_irq = true;
15238c2ecf20Sopenharmony_ci	}
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	/* Handle non-event-queue sources */
15268c2ecf20Sopenharmony_ci	if (queues & (1U << efx->irq_level) && soft_enabled) {
15278c2ecf20Sopenharmony_ci		syserr = EF4_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
15288c2ecf20Sopenharmony_ci		if (unlikely(syserr))
15298c2ecf20Sopenharmony_ci			return ef4_farch_fatal_interrupt(efx);
15308c2ecf20Sopenharmony_ci		efx->last_irq_cpu = raw_smp_processor_id();
15318c2ecf20Sopenharmony_ci	}
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	if (queues != 0) {
15348c2ecf20Sopenharmony_ci		efx->irq_zero_count = 0;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci		/* Schedule processing of any interrupting queues */
15378c2ecf20Sopenharmony_ci		if (likely(soft_enabled)) {
15388c2ecf20Sopenharmony_ci			ef4_for_each_channel(channel, efx) {
15398c2ecf20Sopenharmony_ci				if (queues & 1)
15408c2ecf20Sopenharmony_ci					ef4_schedule_channel_irq(channel);
15418c2ecf20Sopenharmony_ci				queues >>= 1;
15428c2ecf20Sopenharmony_ci			}
15438c2ecf20Sopenharmony_ci		}
15448c2ecf20Sopenharmony_ci		result = IRQ_HANDLED;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	} else {
15478c2ecf20Sopenharmony_ci		ef4_qword_t *event;
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci		/* Legacy ISR read can return zero once (SF bug 15783) */
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci		/* We can't return IRQ_HANDLED more than once on seeing ISR=0
15528c2ecf20Sopenharmony_ci		 * because this might be a shared interrupt. */
15538c2ecf20Sopenharmony_ci		if (efx->irq_zero_count++ == 0)
15548c2ecf20Sopenharmony_ci			result = IRQ_HANDLED;
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci		/* Ensure we schedule or rearm all event queues */
15578c2ecf20Sopenharmony_ci		if (likely(soft_enabled)) {
15588c2ecf20Sopenharmony_ci			ef4_for_each_channel(channel, efx) {
15598c2ecf20Sopenharmony_ci				event = ef4_event(channel,
15608c2ecf20Sopenharmony_ci						  channel->eventq_read_ptr);
15618c2ecf20Sopenharmony_ci				if (ef4_event_present(event))
15628c2ecf20Sopenharmony_ci					ef4_schedule_channel_irq(channel);
15638c2ecf20Sopenharmony_ci				else
15648c2ecf20Sopenharmony_ci					ef4_farch_ev_read_ack(channel);
15658c2ecf20Sopenharmony_ci			}
15668c2ecf20Sopenharmony_ci		}
15678c2ecf20Sopenharmony_ci	}
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	if (result == IRQ_HANDLED)
15708c2ecf20Sopenharmony_ci		netif_vdbg(efx, intr, efx->net_dev,
15718c2ecf20Sopenharmony_ci			   "IRQ %d on CPU %d status " EF4_DWORD_FMT "\n",
15728c2ecf20Sopenharmony_ci			   irq, raw_smp_processor_id(), EF4_DWORD_VAL(reg));
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	return result;
15758c2ecf20Sopenharmony_ci}
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci/* Handle an MSI interrupt
15788c2ecf20Sopenharmony_ci *
15798c2ecf20Sopenharmony_ci * Handle an MSI hardware interrupt.  This routine schedules event
15808c2ecf20Sopenharmony_ci * queue processing.  No interrupt acknowledgement cycle is necessary.
15818c2ecf20Sopenharmony_ci * Also, we never need to check that the interrupt is for us, since
15828c2ecf20Sopenharmony_ci * MSI interrupts cannot be shared.
15838c2ecf20Sopenharmony_ci */
15848c2ecf20Sopenharmony_ciirqreturn_t ef4_farch_msi_interrupt(int irq, void *dev_id)
15858c2ecf20Sopenharmony_ci{
15868c2ecf20Sopenharmony_ci	struct ef4_msi_context *context = dev_id;
15878c2ecf20Sopenharmony_ci	struct ef4_nic *efx = context->efx;
15888c2ecf20Sopenharmony_ci	ef4_oword_t *int_ker = efx->irq_status.addr;
15898c2ecf20Sopenharmony_ci	int syserr;
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	netif_vdbg(efx, intr, efx->net_dev,
15928c2ecf20Sopenharmony_ci		   "IRQ %d on CPU %d status " EF4_OWORD_FMT "\n",
15938c2ecf20Sopenharmony_ci		   irq, raw_smp_processor_id(), EF4_OWORD_VAL(*int_ker));
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	if (!likely(READ_ONCE(efx->irq_soft_enabled)))
15968c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	/* Handle non-event-queue sources */
15998c2ecf20Sopenharmony_ci	if (context->index == efx->irq_level) {
16008c2ecf20Sopenharmony_ci		syserr = EF4_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
16018c2ecf20Sopenharmony_ci		if (unlikely(syserr))
16028c2ecf20Sopenharmony_ci			return ef4_farch_fatal_interrupt(efx);
16038c2ecf20Sopenharmony_ci		efx->last_irq_cpu = raw_smp_processor_id();
16048c2ecf20Sopenharmony_ci	}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	/* Schedule processing of the channel */
16078c2ecf20Sopenharmony_ci	ef4_schedule_channel_irq(efx->channel[context->index]);
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
16108c2ecf20Sopenharmony_ci}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci/* Setup RSS indirection table.
16138c2ecf20Sopenharmony_ci * This maps from the hash value of the packet to RXQ
16148c2ecf20Sopenharmony_ci */
16158c2ecf20Sopenharmony_civoid ef4_farch_rx_push_indir_table(struct ef4_nic *efx)
16168c2ecf20Sopenharmony_ci{
16178c2ecf20Sopenharmony_ci	size_t i = 0;
16188c2ecf20Sopenharmony_ci	ef4_dword_t dword;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	BUG_ON(ef4_nic_rev(efx) < EF4_REV_FALCON_B0);
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
16238c2ecf20Sopenharmony_ci		     FR_BZ_RX_INDIRECTION_TBL_ROWS);
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
16268c2ecf20Sopenharmony_ci		EF4_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
16278c2ecf20Sopenharmony_ci				     efx->rx_indir_table[i]);
16288c2ecf20Sopenharmony_ci		ef4_writed(efx, &dword,
16298c2ecf20Sopenharmony_ci			   FR_BZ_RX_INDIRECTION_TBL +
16308c2ecf20Sopenharmony_ci			   FR_BZ_RX_INDIRECTION_TBL_STEP * i);
16318c2ecf20Sopenharmony_ci	}
16328c2ecf20Sopenharmony_ci}
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci/* Looks at available SRAM resources and works out how many queues we
16358c2ecf20Sopenharmony_ci * can support, and where things like descriptor caches should live.
16368c2ecf20Sopenharmony_ci *
16378c2ecf20Sopenharmony_ci * SRAM is split up as follows:
16388c2ecf20Sopenharmony_ci * 0                          buftbl entries for channels
16398c2ecf20Sopenharmony_ci * efx->vf_buftbl_base        buftbl entries for SR-IOV
16408c2ecf20Sopenharmony_ci * efx->rx_dc_base            RX descriptor caches
16418c2ecf20Sopenharmony_ci * efx->tx_dc_base            TX descriptor caches
16428c2ecf20Sopenharmony_ci */
16438c2ecf20Sopenharmony_civoid ef4_farch_dimension_resources(struct ef4_nic *efx, unsigned sram_lim_qw)
16448c2ecf20Sopenharmony_ci{
16458c2ecf20Sopenharmony_ci	unsigned vi_count;
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	/* Account for the buffer table entries backing the datapath channels
16488c2ecf20Sopenharmony_ci	 * and the descriptor caches for those channels.
16498c2ecf20Sopenharmony_ci	 */
16508c2ecf20Sopenharmony_ci	vi_count = max(efx->n_channels, efx->n_tx_channels * EF4_TXQ_TYPES);
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	efx->tx_dc_base = sram_lim_qw - vi_count * TX_DC_ENTRIES;
16538c2ecf20Sopenharmony_ci	efx->rx_dc_base = efx->tx_dc_base - vi_count * RX_DC_ENTRIES;
16548c2ecf20Sopenharmony_ci}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ciu32 ef4_farch_fpga_ver(struct ef4_nic *efx)
16578c2ecf20Sopenharmony_ci{
16588c2ecf20Sopenharmony_ci	ef4_oword_t altera_build;
16598c2ecf20Sopenharmony_ci	ef4_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
16608c2ecf20Sopenharmony_ci	return EF4_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
16618c2ecf20Sopenharmony_ci}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_civoid ef4_farch_init_common(struct ef4_nic *efx)
16648c2ecf20Sopenharmony_ci{
16658c2ecf20Sopenharmony_ci	ef4_oword_t temp;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	/* Set positions of descriptor caches in SRAM. */
16688c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR, efx->tx_dc_base);
16698c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
16708c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR, efx->rx_dc_base);
16718c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci	/* Set TX descriptor cache size. */
16748c2ecf20Sopenharmony_ci	BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
16758c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
16768c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	/* Set RX descriptor cache size.  Set low watermark to size-8, as
16798c2ecf20Sopenharmony_ci	 * this allows most efficient prefetching.
16808c2ecf20Sopenharmony_ci	 */
16818c2ecf20Sopenharmony_ci	BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
16828c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
16838c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
16848c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
16858c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci	/* Program INT_KER address */
16888c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_2(temp,
16898c2ecf20Sopenharmony_ci			     FRF_AZ_NORM_INT_VEC_DIS_KER,
16908c2ecf20Sopenharmony_ci			     EF4_INT_MODE_USE_MSI(efx),
16918c2ecf20Sopenharmony_ci			     FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
16928c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	/* Use a valid MSI-X vector */
16958c2ecf20Sopenharmony_ci	efx->irq_level = 0;
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci	/* Enable all the genuinely fatal interrupts.  (They are still
16988c2ecf20Sopenharmony_ci	 * masked by the overall interrupt mask, controlled by
16998c2ecf20Sopenharmony_ci	 * falcon_interrupts()).
17008c2ecf20Sopenharmony_ci	 *
17018c2ecf20Sopenharmony_ci	 * Note: All other fatal interrupts are enabled
17028c2ecf20Sopenharmony_ci	 */
17038c2ecf20Sopenharmony_ci	EF4_POPULATE_OWORD_3(temp,
17048c2ecf20Sopenharmony_ci			     FRF_AZ_ILL_ADR_INT_KER_EN, 1,
17058c2ecf20Sopenharmony_ci			     FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
17068c2ecf20Sopenharmony_ci			     FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
17078c2ecf20Sopenharmony_ci	EF4_INVERT_OWORD(temp);
17088c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	/* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
17118c2ecf20Sopenharmony_ci	 * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
17128c2ecf20Sopenharmony_ci	 */
17138c2ecf20Sopenharmony_ci	ef4_reado(efx, &temp, FR_AZ_TX_RESERVED);
17148c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
17158c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
17168c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
17178c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 1);
17188c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
17198c2ecf20Sopenharmony_ci	/* Enable SW_EV to inherit in char driver - assume harmless here */
17208c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
17218c2ecf20Sopenharmony_ci	/* Prefetch threshold 2 => fetch when descriptor cache half empty */
17228c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
17238c2ecf20Sopenharmony_ci	/* Disable hardware watchdog which can misfire */
17248c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
17258c2ecf20Sopenharmony_ci	/* Squash TX of packets of 16 bytes or less */
17268c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0)
17278c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
17288c2ecf20Sopenharmony_ci	ef4_writeo(efx, &temp, FR_AZ_TX_RESERVED);
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) {
17318c2ecf20Sopenharmony_ci		EF4_POPULATE_OWORD_4(temp,
17328c2ecf20Sopenharmony_ci				     /* Default values */
17338c2ecf20Sopenharmony_ci				     FRF_BZ_TX_PACE_SB_NOT_AF, 0x15,
17348c2ecf20Sopenharmony_ci				     FRF_BZ_TX_PACE_SB_AF, 0xb,
17358c2ecf20Sopenharmony_ci				     FRF_BZ_TX_PACE_FB_BASE, 0,
17368c2ecf20Sopenharmony_ci				     /* Allow large pace values in the
17378c2ecf20Sopenharmony_ci				      * fast bin. */
17388c2ecf20Sopenharmony_ci				     FRF_BZ_TX_PACE_BIN_TH,
17398c2ecf20Sopenharmony_ci				     FFE_BZ_TX_PACE_RESERVED);
17408c2ecf20Sopenharmony_ci		ef4_writeo(efx, &temp, FR_BZ_TX_PACE);
17418c2ecf20Sopenharmony_ci	}
17428c2ecf20Sopenharmony_ci}
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci/**************************************************************************
17458c2ecf20Sopenharmony_ci *
17468c2ecf20Sopenharmony_ci * Filter tables
17478c2ecf20Sopenharmony_ci *
17488c2ecf20Sopenharmony_ci **************************************************************************
17498c2ecf20Sopenharmony_ci */
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci/* "Fudge factors" - difference between programmed value and actual depth.
17528c2ecf20Sopenharmony_ci * Due to pipelined implementation we need to program H/W with a value that
17538c2ecf20Sopenharmony_ci * is larger than the hop limit we want.
17548c2ecf20Sopenharmony_ci */
17558c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_CTL_SRCH_FUDGE_WILD 3
17568c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_CTL_SRCH_FUDGE_FULL 1
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci/* Hard maximum search limit.  Hardware will time-out beyond 200-something.
17598c2ecf20Sopenharmony_ci * We also need to avoid infinite loops in ef4_farch_filter_search() when the
17608c2ecf20Sopenharmony_ci * table is full.
17618c2ecf20Sopenharmony_ci */
17628c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_CTL_SRCH_MAX 200
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci/* Don't try very hard to find space for performance hints, as this is
17658c2ecf20Sopenharmony_ci * counter-productive. */
17668c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_CTL_SRCH_HINT_MAX 5
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_cienum ef4_farch_filter_type {
17698c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TCP_FULL = 0,
17708c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TCP_WILD,
17718c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_UDP_FULL,
17728c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_UDP_WILD,
17738c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_MAC_FULL = 4,
17748c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_MAC_WILD,
17758c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_UC_DEF = 8,
17768c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_MC_DEF,
17778c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TYPE_COUNT,		/* number of specific types */
17788c2ecf20Sopenharmony_ci};
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_cienum ef4_farch_filter_table_id {
17818c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_IP = 0,
17828c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_MAC,
17838c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_DEF,
17848c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_TX_MAC,
17858c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_COUNT,
17868c2ecf20Sopenharmony_ci};
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_cienum ef4_farch_filter_index {
17898c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_INDEX_UC_DEF,
17908c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_INDEX_MC_DEF,
17918c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_SIZE_RX_DEF,
17928c2ecf20Sopenharmony_ci};
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_cistruct ef4_farch_filter_spec {
17958c2ecf20Sopenharmony_ci	u8	type:4;
17968c2ecf20Sopenharmony_ci	u8	priority:4;
17978c2ecf20Sopenharmony_ci	u8	flags;
17988c2ecf20Sopenharmony_ci	u16	dmaq_id;
17998c2ecf20Sopenharmony_ci	u32	data[3];
18008c2ecf20Sopenharmony_ci};
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_cistruct ef4_farch_filter_table {
18038c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id id;
18048c2ecf20Sopenharmony_ci	u32		offset;		/* address of table relative to BAR */
18058c2ecf20Sopenharmony_ci	unsigned	size;		/* number of entries */
18068c2ecf20Sopenharmony_ci	unsigned	step;		/* step between entries */
18078c2ecf20Sopenharmony_ci	unsigned	used;		/* number currently used */
18088c2ecf20Sopenharmony_ci	unsigned long	*used_bitmap;
18098c2ecf20Sopenharmony_ci	struct ef4_farch_filter_spec *spec;
18108c2ecf20Sopenharmony_ci	unsigned	search_limit[EF4_FARCH_FILTER_TYPE_COUNT];
18118c2ecf20Sopenharmony_ci};
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_cistruct ef4_farch_filter_state {
18148c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table table[EF4_FARCH_FILTER_TABLE_COUNT];
18158c2ecf20Sopenharmony_ci};
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_cistatic void
18188c2ecf20Sopenharmony_cief4_farch_filter_table_clear_entry(struct ef4_nic *efx,
18198c2ecf20Sopenharmony_ci				   struct ef4_farch_filter_table *table,
18208c2ecf20Sopenharmony_ci				   unsigned int filter_idx);
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci/* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
18238c2ecf20Sopenharmony_ci * key derived from the n-tuple.  The initial LFSR state is 0xffff. */
18248c2ecf20Sopenharmony_cistatic u16 ef4_farch_filter_hash(u32 key)
18258c2ecf20Sopenharmony_ci{
18268c2ecf20Sopenharmony_ci	u16 tmp;
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci	/* First 16 rounds */
18298c2ecf20Sopenharmony_ci	tmp = 0x1fff ^ key >> 16;
18308c2ecf20Sopenharmony_ci	tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
18318c2ecf20Sopenharmony_ci	tmp = tmp ^ tmp >> 9;
18328c2ecf20Sopenharmony_ci	/* Last 16 rounds */
18338c2ecf20Sopenharmony_ci	tmp = tmp ^ tmp << 13 ^ key;
18348c2ecf20Sopenharmony_ci	tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
18358c2ecf20Sopenharmony_ci	return tmp ^ tmp >> 9;
18368c2ecf20Sopenharmony_ci}
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci/* To allow for hash collisions, filter search continues at these
18398c2ecf20Sopenharmony_ci * increments from the first possible entry selected by the hash. */
18408c2ecf20Sopenharmony_cistatic u16 ef4_farch_filter_increment(u32 key)
18418c2ecf20Sopenharmony_ci{
18428c2ecf20Sopenharmony_ci	return key * 2 - 1;
18438c2ecf20Sopenharmony_ci}
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_cistatic enum ef4_farch_filter_table_id
18468c2ecf20Sopenharmony_cief4_farch_filter_spec_table_id(const struct ef4_farch_filter_spec *spec)
18478c2ecf20Sopenharmony_ci{
18488c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_IP !=
18498c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_TCP_FULL >> 2));
18508c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_IP !=
18518c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_TCP_WILD >> 2));
18528c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_IP !=
18538c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_UDP_FULL >> 2));
18548c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_IP !=
18558c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_UDP_WILD >> 2));
18568c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_MAC !=
18578c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_MAC_FULL >> 2));
18588c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_RX_MAC !=
18598c2ecf20Sopenharmony_ci		     (EF4_FARCH_FILTER_MAC_WILD >> 2));
18608c2ecf20Sopenharmony_ci	BUILD_BUG_ON(EF4_FARCH_FILTER_TABLE_TX_MAC !=
18618c2ecf20Sopenharmony_ci		     EF4_FARCH_FILTER_TABLE_RX_MAC + 2);
18628c2ecf20Sopenharmony_ci	return (spec->type >> 2) + ((spec->flags & EF4_FILTER_FLAG_TX) ? 2 : 0);
18638c2ecf20Sopenharmony_ci}
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_cistatic void ef4_farch_filter_push_rx_config(struct ef4_nic *efx)
18668c2ecf20Sopenharmony_ci{
18678c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
18688c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
18698c2ecf20Sopenharmony_ci	ef4_oword_t filter_ctl;
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci	ef4_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	table = &state->table[EF4_FARCH_FILTER_TABLE_RX_IP];
18748c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
18758c2ecf20Sopenharmony_ci			    table->search_limit[EF4_FARCH_FILTER_TCP_FULL] +
18768c2ecf20Sopenharmony_ci			    EF4_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
18778c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
18788c2ecf20Sopenharmony_ci			    table->search_limit[EF4_FARCH_FILTER_TCP_WILD] +
18798c2ecf20Sopenharmony_ci			    EF4_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
18808c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
18818c2ecf20Sopenharmony_ci			    table->search_limit[EF4_FARCH_FILTER_UDP_FULL] +
18828c2ecf20Sopenharmony_ci			    EF4_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
18838c2ecf20Sopenharmony_ci	EF4_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
18848c2ecf20Sopenharmony_ci			    table->search_limit[EF4_FARCH_FILTER_UDP_WILD] +
18858c2ecf20Sopenharmony_ci			    EF4_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci	table = &state->table[EF4_FARCH_FILTER_TABLE_RX_MAC];
18888c2ecf20Sopenharmony_ci	if (table->size) {
18898c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
18908c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
18918c2ecf20Sopenharmony_ci			table->search_limit[EF4_FARCH_FILTER_MAC_FULL] +
18928c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
18938c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
18948c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
18958c2ecf20Sopenharmony_ci			table->search_limit[EF4_FARCH_FILTER_MAC_WILD] +
18968c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
18978c2ecf20Sopenharmony_ci	}
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci	table = &state->table[EF4_FARCH_FILTER_TABLE_RX_DEF];
19008c2ecf20Sopenharmony_ci	if (table->size) {
19018c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19028c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_UNICAST_NOMATCH_Q_ID,
19038c2ecf20Sopenharmony_ci			table->spec[EF4_FARCH_FILTER_INDEX_UC_DEF].dmaq_id);
19048c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19058c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_UNICAST_NOMATCH_RSS_ENABLED,
19068c2ecf20Sopenharmony_ci			!!(table->spec[EF4_FARCH_FILTER_INDEX_UC_DEF].flags &
19078c2ecf20Sopenharmony_ci			   EF4_FILTER_FLAG_RX_RSS));
19088c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19098c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_MULTICAST_NOMATCH_Q_ID,
19108c2ecf20Sopenharmony_ci			table->spec[EF4_FARCH_FILTER_INDEX_MC_DEF].dmaq_id);
19118c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19128c2ecf20Sopenharmony_ci			filter_ctl, FRF_CZ_MULTICAST_NOMATCH_RSS_ENABLED,
19138c2ecf20Sopenharmony_ci			!!(table->spec[EF4_FARCH_FILTER_INDEX_MC_DEF].flags &
19148c2ecf20Sopenharmony_ci			   EF4_FILTER_FLAG_RX_RSS));
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci		/* There is a single bit to enable RX scatter for all
19178c2ecf20Sopenharmony_ci		 * unmatched packets.  Only set it if scatter is
19188c2ecf20Sopenharmony_ci		 * enabled in both filter specs.
19198c2ecf20Sopenharmony_ci		 */
19208c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19218c2ecf20Sopenharmony_ci			filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
19228c2ecf20Sopenharmony_ci			!!(table->spec[EF4_FARCH_FILTER_INDEX_UC_DEF].flags &
19238c2ecf20Sopenharmony_ci			   table->spec[EF4_FARCH_FILTER_INDEX_MC_DEF].flags &
19248c2ecf20Sopenharmony_ci			   EF4_FILTER_FLAG_RX_SCATTER));
19258c2ecf20Sopenharmony_ci	} else if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) {
19268c2ecf20Sopenharmony_ci		/* We don't expose 'default' filters because unmatched
19278c2ecf20Sopenharmony_ci		 * packets always go to the queue number found in the
19288c2ecf20Sopenharmony_ci		 * RSS table.  But we still need to set the RX scatter
19298c2ecf20Sopenharmony_ci		 * bit here.
19308c2ecf20Sopenharmony_ci		 */
19318c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19328c2ecf20Sopenharmony_ci			filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
19338c2ecf20Sopenharmony_ci			efx->rx_scatter);
19348c2ecf20Sopenharmony_ci	}
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci	ef4_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_cistatic void ef4_farch_filter_push_tx_limits(struct ef4_nic *efx)
19408c2ecf20Sopenharmony_ci{
19418c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
19428c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
19438c2ecf20Sopenharmony_ci	ef4_oword_t tx_cfg;
19448c2ecf20Sopenharmony_ci
19458c2ecf20Sopenharmony_ci	ef4_reado(efx, &tx_cfg, FR_AZ_TX_CFG);
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	table = &state->table[EF4_FARCH_FILTER_TABLE_TX_MAC];
19488c2ecf20Sopenharmony_ci	if (table->size) {
19498c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19508c2ecf20Sopenharmony_ci			tx_cfg, FRF_CZ_TX_ETH_FILTER_FULL_SEARCH_RANGE,
19518c2ecf20Sopenharmony_ci			table->search_limit[EF4_FARCH_FILTER_MAC_FULL] +
19528c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
19538c2ecf20Sopenharmony_ci		EF4_SET_OWORD_FIELD(
19548c2ecf20Sopenharmony_ci			tx_cfg, FRF_CZ_TX_ETH_FILTER_WILD_SEARCH_RANGE,
19558c2ecf20Sopenharmony_ci			table->search_limit[EF4_FARCH_FILTER_MAC_WILD] +
19568c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
19578c2ecf20Sopenharmony_ci	}
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_ci	ef4_writeo(efx, &tx_cfg, FR_AZ_TX_CFG);
19608c2ecf20Sopenharmony_ci}
19618c2ecf20Sopenharmony_ci
19628c2ecf20Sopenharmony_cistatic int
19638c2ecf20Sopenharmony_cief4_farch_filter_from_gen_spec(struct ef4_farch_filter_spec *spec,
19648c2ecf20Sopenharmony_ci			       const struct ef4_filter_spec *gen_spec)
19658c2ecf20Sopenharmony_ci{
19668c2ecf20Sopenharmony_ci	bool is_full = false;
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci	if ((gen_spec->flags & EF4_FILTER_FLAG_RX_RSS) &&
19698c2ecf20Sopenharmony_ci	    gen_spec->rss_context != EF4_FILTER_RSS_CONTEXT_DEFAULT)
19708c2ecf20Sopenharmony_ci		return -EINVAL;
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	spec->priority = gen_spec->priority;
19738c2ecf20Sopenharmony_ci	spec->flags = gen_spec->flags;
19748c2ecf20Sopenharmony_ci	spec->dmaq_id = gen_spec->dmaq_id;
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci	switch (gen_spec->match_flags) {
19778c2ecf20Sopenharmony_ci	case (EF4_FILTER_MATCH_ETHER_TYPE | EF4_FILTER_MATCH_IP_PROTO |
19788c2ecf20Sopenharmony_ci	      EF4_FILTER_MATCH_LOC_HOST | EF4_FILTER_MATCH_LOC_PORT |
19798c2ecf20Sopenharmony_ci	      EF4_FILTER_MATCH_REM_HOST | EF4_FILTER_MATCH_REM_PORT):
19808c2ecf20Sopenharmony_ci		is_full = true;
19818c2ecf20Sopenharmony_ci		fallthrough;
19828c2ecf20Sopenharmony_ci	case (EF4_FILTER_MATCH_ETHER_TYPE | EF4_FILTER_MATCH_IP_PROTO |
19838c2ecf20Sopenharmony_ci	      EF4_FILTER_MATCH_LOC_HOST | EF4_FILTER_MATCH_LOC_PORT): {
19848c2ecf20Sopenharmony_ci		__be32 rhost, host1, host2;
19858c2ecf20Sopenharmony_ci		__be16 rport, port1, port2;
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_ci		EF4_BUG_ON_PARANOID(!(gen_spec->flags & EF4_FILTER_FLAG_RX));
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci		if (gen_spec->ether_type != htons(ETH_P_IP))
19908c2ecf20Sopenharmony_ci			return -EPROTONOSUPPORT;
19918c2ecf20Sopenharmony_ci		if (gen_spec->loc_port == 0 ||
19928c2ecf20Sopenharmony_ci		    (is_full && gen_spec->rem_port == 0))
19938c2ecf20Sopenharmony_ci			return -EADDRNOTAVAIL;
19948c2ecf20Sopenharmony_ci		switch (gen_spec->ip_proto) {
19958c2ecf20Sopenharmony_ci		case IPPROTO_TCP:
19968c2ecf20Sopenharmony_ci			spec->type = (is_full ? EF4_FARCH_FILTER_TCP_FULL :
19978c2ecf20Sopenharmony_ci				      EF4_FARCH_FILTER_TCP_WILD);
19988c2ecf20Sopenharmony_ci			break;
19998c2ecf20Sopenharmony_ci		case IPPROTO_UDP:
20008c2ecf20Sopenharmony_ci			spec->type = (is_full ? EF4_FARCH_FILTER_UDP_FULL :
20018c2ecf20Sopenharmony_ci				      EF4_FARCH_FILTER_UDP_WILD);
20028c2ecf20Sopenharmony_ci			break;
20038c2ecf20Sopenharmony_ci		default:
20048c2ecf20Sopenharmony_ci			return -EPROTONOSUPPORT;
20058c2ecf20Sopenharmony_ci		}
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci		/* Filter is constructed in terms of source and destination,
20088c2ecf20Sopenharmony_ci		 * with the odd wrinkle that the ports are swapped in a UDP
20098c2ecf20Sopenharmony_ci		 * wildcard filter.  We need to convert from local and remote
20108c2ecf20Sopenharmony_ci		 * (= zero for wildcard) addresses.
20118c2ecf20Sopenharmony_ci		 */
20128c2ecf20Sopenharmony_ci		rhost = is_full ? gen_spec->rem_host[0] : 0;
20138c2ecf20Sopenharmony_ci		rport = is_full ? gen_spec->rem_port : 0;
20148c2ecf20Sopenharmony_ci		host1 = rhost;
20158c2ecf20Sopenharmony_ci		host2 = gen_spec->loc_host[0];
20168c2ecf20Sopenharmony_ci		if (!is_full && gen_spec->ip_proto == IPPROTO_UDP) {
20178c2ecf20Sopenharmony_ci			port1 = gen_spec->loc_port;
20188c2ecf20Sopenharmony_ci			port2 = rport;
20198c2ecf20Sopenharmony_ci		} else {
20208c2ecf20Sopenharmony_ci			port1 = rport;
20218c2ecf20Sopenharmony_ci			port2 = gen_spec->loc_port;
20228c2ecf20Sopenharmony_ci		}
20238c2ecf20Sopenharmony_ci		spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
20248c2ecf20Sopenharmony_ci		spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
20258c2ecf20Sopenharmony_ci		spec->data[2] = ntohl(host2);
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci		break;
20288c2ecf20Sopenharmony_ci	}
20298c2ecf20Sopenharmony_ci
20308c2ecf20Sopenharmony_ci	case EF4_FILTER_MATCH_LOC_MAC | EF4_FILTER_MATCH_OUTER_VID:
20318c2ecf20Sopenharmony_ci		is_full = true;
20328c2ecf20Sopenharmony_ci		fallthrough;
20338c2ecf20Sopenharmony_ci	case EF4_FILTER_MATCH_LOC_MAC:
20348c2ecf20Sopenharmony_ci		spec->type = (is_full ? EF4_FARCH_FILTER_MAC_FULL :
20358c2ecf20Sopenharmony_ci			      EF4_FARCH_FILTER_MAC_WILD);
20368c2ecf20Sopenharmony_ci		spec->data[0] = is_full ? ntohs(gen_spec->outer_vid) : 0;
20378c2ecf20Sopenharmony_ci		spec->data[1] = (gen_spec->loc_mac[2] << 24 |
20388c2ecf20Sopenharmony_ci				 gen_spec->loc_mac[3] << 16 |
20398c2ecf20Sopenharmony_ci				 gen_spec->loc_mac[4] << 8 |
20408c2ecf20Sopenharmony_ci				 gen_spec->loc_mac[5]);
20418c2ecf20Sopenharmony_ci		spec->data[2] = (gen_spec->loc_mac[0] << 8 |
20428c2ecf20Sopenharmony_ci				 gen_spec->loc_mac[1]);
20438c2ecf20Sopenharmony_ci		break;
20448c2ecf20Sopenharmony_ci
20458c2ecf20Sopenharmony_ci	case EF4_FILTER_MATCH_LOC_MAC_IG:
20468c2ecf20Sopenharmony_ci		spec->type = (is_multicast_ether_addr(gen_spec->loc_mac) ?
20478c2ecf20Sopenharmony_ci			      EF4_FARCH_FILTER_MC_DEF :
20488c2ecf20Sopenharmony_ci			      EF4_FARCH_FILTER_UC_DEF);
20498c2ecf20Sopenharmony_ci		memset(spec->data, 0, sizeof(spec->data)); /* ensure equality */
20508c2ecf20Sopenharmony_ci		break;
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	default:
20538c2ecf20Sopenharmony_ci		return -EPROTONOSUPPORT;
20548c2ecf20Sopenharmony_ci	}
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	return 0;
20578c2ecf20Sopenharmony_ci}
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_cistatic void
20608c2ecf20Sopenharmony_cief4_farch_filter_to_gen_spec(struct ef4_filter_spec *gen_spec,
20618c2ecf20Sopenharmony_ci			     const struct ef4_farch_filter_spec *spec)
20628c2ecf20Sopenharmony_ci{
20638c2ecf20Sopenharmony_ci	bool is_full = false;
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	/* *gen_spec should be completely initialised, to be consistent
20668c2ecf20Sopenharmony_ci	 * with ef4_filter_init_{rx,tx}() and in case we want to copy
20678c2ecf20Sopenharmony_ci	 * it back to userland.
20688c2ecf20Sopenharmony_ci	 */
20698c2ecf20Sopenharmony_ci	memset(gen_spec, 0, sizeof(*gen_spec));
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci	gen_spec->priority = spec->priority;
20728c2ecf20Sopenharmony_ci	gen_spec->flags = spec->flags;
20738c2ecf20Sopenharmony_ci	gen_spec->dmaq_id = spec->dmaq_id;
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_ci	switch (spec->type) {
20768c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_TCP_FULL:
20778c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_UDP_FULL:
20788c2ecf20Sopenharmony_ci		is_full = true;
20798c2ecf20Sopenharmony_ci		fallthrough;
20808c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_TCP_WILD:
20818c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_UDP_WILD: {
20828c2ecf20Sopenharmony_ci		__be32 host1, host2;
20838c2ecf20Sopenharmony_ci		__be16 port1, port2;
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci		gen_spec->match_flags =
20868c2ecf20Sopenharmony_ci			EF4_FILTER_MATCH_ETHER_TYPE |
20878c2ecf20Sopenharmony_ci			EF4_FILTER_MATCH_IP_PROTO |
20888c2ecf20Sopenharmony_ci			EF4_FILTER_MATCH_LOC_HOST | EF4_FILTER_MATCH_LOC_PORT;
20898c2ecf20Sopenharmony_ci		if (is_full)
20908c2ecf20Sopenharmony_ci			gen_spec->match_flags |= (EF4_FILTER_MATCH_REM_HOST |
20918c2ecf20Sopenharmony_ci						  EF4_FILTER_MATCH_REM_PORT);
20928c2ecf20Sopenharmony_ci		gen_spec->ether_type = htons(ETH_P_IP);
20938c2ecf20Sopenharmony_ci		gen_spec->ip_proto =
20948c2ecf20Sopenharmony_ci			(spec->type == EF4_FARCH_FILTER_TCP_FULL ||
20958c2ecf20Sopenharmony_ci			 spec->type == EF4_FARCH_FILTER_TCP_WILD) ?
20968c2ecf20Sopenharmony_ci			IPPROTO_TCP : IPPROTO_UDP;
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_ci		host1 = htonl(spec->data[0] >> 16 | spec->data[1] << 16);
20998c2ecf20Sopenharmony_ci		port1 = htons(spec->data[0]);
21008c2ecf20Sopenharmony_ci		host2 = htonl(spec->data[2]);
21018c2ecf20Sopenharmony_ci		port2 = htons(spec->data[1] >> 16);
21028c2ecf20Sopenharmony_ci		if (spec->flags & EF4_FILTER_FLAG_TX) {
21038c2ecf20Sopenharmony_ci			gen_spec->loc_host[0] = host1;
21048c2ecf20Sopenharmony_ci			gen_spec->rem_host[0] = host2;
21058c2ecf20Sopenharmony_ci		} else {
21068c2ecf20Sopenharmony_ci			gen_spec->loc_host[0] = host2;
21078c2ecf20Sopenharmony_ci			gen_spec->rem_host[0] = host1;
21088c2ecf20Sopenharmony_ci		}
21098c2ecf20Sopenharmony_ci		if (!!(gen_spec->flags & EF4_FILTER_FLAG_TX) ^
21108c2ecf20Sopenharmony_ci		    (!is_full && gen_spec->ip_proto == IPPROTO_UDP)) {
21118c2ecf20Sopenharmony_ci			gen_spec->loc_port = port1;
21128c2ecf20Sopenharmony_ci			gen_spec->rem_port = port2;
21138c2ecf20Sopenharmony_ci		} else {
21148c2ecf20Sopenharmony_ci			gen_spec->loc_port = port2;
21158c2ecf20Sopenharmony_ci			gen_spec->rem_port = port1;
21168c2ecf20Sopenharmony_ci		}
21178c2ecf20Sopenharmony_ci
21188c2ecf20Sopenharmony_ci		break;
21198c2ecf20Sopenharmony_ci	}
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_MAC_FULL:
21228c2ecf20Sopenharmony_ci		is_full = true;
21238c2ecf20Sopenharmony_ci		fallthrough;
21248c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_MAC_WILD:
21258c2ecf20Sopenharmony_ci		gen_spec->match_flags = EF4_FILTER_MATCH_LOC_MAC;
21268c2ecf20Sopenharmony_ci		if (is_full)
21278c2ecf20Sopenharmony_ci			gen_spec->match_flags |= EF4_FILTER_MATCH_OUTER_VID;
21288c2ecf20Sopenharmony_ci		gen_spec->loc_mac[0] = spec->data[2] >> 8;
21298c2ecf20Sopenharmony_ci		gen_spec->loc_mac[1] = spec->data[2];
21308c2ecf20Sopenharmony_ci		gen_spec->loc_mac[2] = spec->data[1] >> 24;
21318c2ecf20Sopenharmony_ci		gen_spec->loc_mac[3] = spec->data[1] >> 16;
21328c2ecf20Sopenharmony_ci		gen_spec->loc_mac[4] = spec->data[1] >> 8;
21338c2ecf20Sopenharmony_ci		gen_spec->loc_mac[5] = spec->data[1];
21348c2ecf20Sopenharmony_ci		gen_spec->outer_vid = htons(spec->data[0]);
21358c2ecf20Sopenharmony_ci		break;
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_UC_DEF:
21388c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_MC_DEF:
21398c2ecf20Sopenharmony_ci		gen_spec->match_flags = EF4_FILTER_MATCH_LOC_MAC_IG;
21408c2ecf20Sopenharmony_ci		gen_spec->loc_mac[0] = spec->type == EF4_FARCH_FILTER_MC_DEF;
21418c2ecf20Sopenharmony_ci		break;
21428c2ecf20Sopenharmony_ci
21438c2ecf20Sopenharmony_ci	default:
21448c2ecf20Sopenharmony_ci		WARN_ON(1);
21458c2ecf20Sopenharmony_ci		break;
21468c2ecf20Sopenharmony_ci	}
21478c2ecf20Sopenharmony_ci}
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_cistatic void
21508c2ecf20Sopenharmony_cief4_farch_filter_init_rx_auto(struct ef4_nic *efx,
21518c2ecf20Sopenharmony_ci			      struct ef4_farch_filter_spec *spec)
21528c2ecf20Sopenharmony_ci{
21538c2ecf20Sopenharmony_ci	/* If there's only one channel then disable RSS for non VF
21548c2ecf20Sopenharmony_ci	 * traffic, thereby allowing VFs to use RSS when the PF can't.
21558c2ecf20Sopenharmony_ci	 */
21568c2ecf20Sopenharmony_ci	spec->priority = EF4_FILTER_PRI_AUTO;
21578c2ecf20Sopenharmony_ci	spec->flags = (EF4_FILTER_FLAG_RX |
21588c2ecf20Sopenharmony_ci		       (ef4_rss_enabled(efx) ? EF4_FILTER_FLAG_RX_RSS : 0) |
21598c2ecf20Sopenharmony_ci		       (efx->rx_scatter ? EF4_FILTER_FLAG_RX_SCATTER : 0));
21608c2ecf20Sopenharmony_ci	spec->dmaq_id = 0;
21618c2ecf20Sopenharmony_ci}
21628c2ecf20Sopenharmony_ci
21638c2ecf20Sopenharmony_ci/* Build a filter entry and return its n-tuple key. */
21648c2ecf20Sopenharmony_cistatic u32 ef4_farch_filter_build(ef4_oword_t *filter,
21658c2ecf20Sopenharmony_ci				  struct ef4_farch_filter_spec *spec)
21668c2ecf20Sopenharmony_ci{
21678c2ecf20Sopenharmony_ci	u32 data3;
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci	switch (ef4_farch_filter_spec_table_id(spec)) {
21708c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_TABLE_RX_IP: {
21718c2ecf20Sopenharmony_ci		bool is_udp = (spec->type == EF4_FARCH_FILTER_UDP_FULL ||
21728c2ecf20Sopenharmony_ci			       spec->type == EF4_FARCH_FILTER_UDP_WILD);
21738c2ecf20Sopenharmony_ci		EF4_POPULATE_OWORD_7(
21748c2ecf20Sopenharmony_ci			*filter,
21758c2ecf20Sopenharmony_ci			FRF_BZ_RSS_EN,
21768c2ecf20Sopenharmony_ci			!!(spec->flags & EF4_FILTER_FLAG_RX_RSS),
21778c2ecf20Sopenharmony_ci			FRF_BZ_SCATTER_EN,
21788c2ecf20Sopenharmony_ci			!!(spec->flags & EF4_FILTER_FLAG_RX_SCATTER),
21798c2ecf20Sopenharmony_ci			FRF_BZ_TCP_UDP, is_udp,
21808c2ecf20Sopenharmony_ci			FRF_BZ_RXQ_ID, spec->dmaq_id,
21818c2ecf20Sopenharmony_ci			EF4_DWORD_2, spec->data[2],
21828c2ecf20Sopenharmony_ci			EF4_DWORD_1, spec->data[1],
21838c2ecf20Sopenharmony_ci			EF4_DWORD_0, spec->data[0]);
21848c2ecf20Sopenharmony_ci		data3 = is_udp;
21858c2ecf20Sopenharmony_ci		break;
21868c2ecf20Sopenharmony_ci	}
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_TABLE_RX_MAC: {
21898c2ecf20Sopenharmony_ci		bool is_wild = spec->type == EF4_FARCH_FILTER_MAC_WILD;
21908c2ecf20Sopenharmony_ci		EF4_POPULATE_OWORD_7(
21918c2ecf20Sopenharmony_ci			*filter,
21928c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_RSS_EN,
21938c2ecf20Sopenharmony_ci			!!(spec->flags & EF4_FILTER_FLAG_RX_RSS),
21948c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_SCATTER_EN,
21958c2ecf20Sopenharmony_ci			!!(spec->flags & EF4_FILTER_FLAG_RX_SCATTER),
21968c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
21978c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
21988c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
21998c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
22008c2ecf20Sopenharmony_ci			FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
22018c2ecf20Sopenharmony_ci		data3 = is_wild;
22028c2ecf20Sopenharmony_ci		break;
22038c2ecf20Sopenharmony_ci	}
22048c2ecf20Sopenharmony_ci
22058c2ecf20Sopenharmony_ci	case EF4_FARCH_FILTER_TABLE_TX_MAC: {
22068c2ecf20Sopenharmony_ci		bool is_wild = spec->type == EF4_FARCH_FILTER_MAC_WILD;
22078c2ecf20Sopenharmony_ci		EF4_POPULATE_OWORD_5(*filter,
22088c2ecf20Sopenharmony_ci				     FRF_CZ_TMFT_TXQ_ID, spec->dmaq_id,
22098c2ecf20Sopenharmony_ci				     FRF_CZ_TMFT_WILDCARD_MATCH, is_wild,
22108c2ecf20Sopenharmony_ci				     FRF_CZ_TMFT_SRC_MAC_HI, spec->data[2],
22118c2ecf20Sopenharmony_ci				     FRF_CZ_TMFT_SRC_MAC_LO, spec->data[1],
22128c2ecf20Sopenharmony_ci				     FRF_CZ_TMFT_VLAN_ID, spec->data[0]);
22138c2ecf20Sopenharmony_ci		data3 = is_wild | spec->dmaq_id << 1;
22148c2ecf20Sopenharmony_ci		break;
22158c2ecf20Sopenharmony_ci	}
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	default:
22188c2ecf20Sopenharmony_ci		BUG();
22198c2ecf20Sopenharmony_ci	}
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ci	return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
22228c2ecf20Sopenharmony_ci}
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_cistatic bool ef4_farch_filter_equal(const struct ef4_farch_filter_spec *left,
22258c2ecf20Sopenharmony_ci				   const struct ef4_farch_filter_spec *right)
22268c2ecf20Sopenharmony_ci{
22278c2ecf20Sopenharmony_ci	if (left->type != right->type ||
22288c2ecf20Sopenharmony_ci	    memcmp(left->data, right->data, sizeof(left->data)))
22298c2ecf20Sopenharmony_ci		return false;
22308c2ecf20Sopenharmony_ci
22318c2ecf20Sopenharmony_ci	if (left->flags & EF4_FILTER_FLAG_TX &&
22328c2ecf20Sopenharmony_ci	    left->dmaq_id != right->dmaq_id)
22338c2ecf20Sopenharmony_ci		return false;
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_ci	return true;
22368c2ecf20Sopenharmony_ci}
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci/*
22398c2ecf20Sopenharmony_ci * Construct/deconstruct external filter IDs.  At least the RX filter
22408c2ecf20Sopenharmony_ci * IDs must be ordered by matching priority, for RX NFC semantics.
22418c2ecf20Sopenharmony_ci *
22428c2ecf20Sopenharmony_ci * Deconstruction needs to be robust against invalid IDs so that
22438c2ecf20Sopenharmony_ci * ef4_filter_remove_id_safe() and ef4_filter_get_filter_safe() can
22448c2ecf20Sopenharmony_ci * accept user-provided IDs.
22458c2ecf20Sopenharmony_ci */
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_MATCH_PRI_COUNT	5
22488c2ecf20Sopenharmony_ci
22498c2ecf20Sopenharmony_cistatic const u8 ef4_farch_filter_type_match_pri[EF4_FARCH_FILTER_TYPE_COUNT] = {
22508c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_TCP_FULL]	= 0,
22518c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_UDP_FULL]	= 0,
22528c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_TCP_WILD]	= 1,
22538c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_UDP_WILD]	= 1,
22548c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_MAC_FULL]	= 2,
22558c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_MAC_WILD]	= 3,
22568c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_UC_DEF]	= 4,
22578c2ecf20Sopenharmony_ci	[EF4_FARCH_FILTER_MC_DEF]	= 4,
22588c2ecf20Sopenharmony_ci};
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_cistatic const enum ef4_farch_filter_table_id ef4_farch_filter_range_table[] = {
22618c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_IP,	/* RX match pri 0 */
22628c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_IP,
22638c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_MAC,
22648c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_MAC,
22658c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_RX_DEF,	/* RX match pri 4 */
22668c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_TX_MAC,	/* TX match pri 0 */
22678c2ecf20Sopenharmony_ci	EF4_FARCH_FILTER_TABLE_TX_MAC,	/* TX match pri 1 */
22688c2ecf20Sopenharmony_ci};
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_INDEX_WIDTH 13
22718c2ecf20Sopenharmony_ci#define EF4_FARCH_FILTER_INDEX_MASK ((1 << EF4_FARCH_FILTER_INDEX_WIDTH) - 1)
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_cistatic inline u32
22748c2ecf20Sopenharmony_cief4_farch_filter_make_id(const struct ef4_farch_filter_spec *spec,
22758c2ecf20Sopenharmony_ci			 unsigned int index)
22768c2ecf20Sopenharmony_ci{
22778c2ecf20Sopenharmony_ci	unsigned int range;
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci	range = ef4_farch_filter_type_match_pri[spec->type];
22808c2ecf20Sopenharmony_ci	if (!(spec->flags & EF4_FILTER_FLAG_RX))
22818c2ecf20Sopenharmony_ci		range += EF4_FARCH_FILTER_MATCH_PRI_COUNT;
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	return range << EF4_FARCH_FILTER_INDEX_WIDTH | index;
22848c2ecf20Sopenharmony_ci}
22858c2ecf20Sopenharmony_ci
22868c2ecf20Sopenharmony_cistatic inline enum ef4_farch_filter_table_id
22878c2ecf20Sopenharmony_cief4_farch_filter_id_table_id(u32 id)
22888c2ecf20Sopenharmony_ci{
22898c2ecf20Sopenharmony_ci	unsigned int range = id >> EF4_FARCH_FILTER_INDEX_WIDTH;
22908c2ecf20Sopenharmony_ci
22918c2ecf20Sopenharmony_ci	if (range < ARRAY_SIZE(ef4_farch_filter_range_table))
22928c2ecf20Sopenharmony_ci		return ef4_farch_filter_range_table[range];
22938c2ecf20Sopenharmony_ci	else
22948c2ecf20Sopenharmony_ci		return EF4_FARCH_FILTER_TABLE_COUNT; /* invalid */
22958c2ecf20Sopenharmony_ci}
22968c2ecf20Sopenharmony_ci
22978c2ecf20Sopenharmony_cistatic inline unsigned int ef4_farch_filter_id_index(u32 id)
22988c2ecf20Sopenharmony_ci{
22998c2ecf20Sopenharmony_ci	return id & EF4_FARCH_FILTER_INDEX_MASK;
23008c2ecf20Sopenharmony_ci}
23018c2ecf20Sopenharmony_ci
23028c2ecf20Sopenharmony_ciu32 ef4_farch_filter_get_rx_id_limit(struct ef4_nic *efx)
23038c2ecf20Sopenharmony_ci{
23048c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
23058c2ecf20Sopenharmony_ci	unsigned int range = EF4_FARCH_FILTER_MATCH_PRI_COUNT - 1;
23068c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
23078c2ecf20Sopenharmony_ci
23088c2ecf20Sopenharmony_ci	do {
23098c2ecf20Sopenharmony_ci		table_id = ef4_farch_filter_range_table[range];
23108c2ecf20Sopenharmony_ci		if (state->table[table_id].size != 0)
23118c2ecf20Sopenharmony_ci			return range << EF4_FARCH_FILTER_INDEX_WIDTH |
23128c2ecf20Sopenharmony_ci				state->table[table_id].size;
23138c2ecf20Sopenharmony_ci	} while (range--);
23148c2ecf20Sopenharmony_ci
23158c2ecf20Sopenharmony_ci	return 0;
23168c2ecf20Sopenharmony_ci}
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_cis32 ef4_farch_filter_insert(struct ef4_nic *efx,
23198c2ecf20Sopenharmony_ci			    struct ef4_filter_spec *gen_spec,
23208c2ecf20Sopenharmony_ci			    bool replace_equal)
23218c2ecf20Sopenharmony_ci{
23228c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
23238c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
23248c2ecf20Sopenharmony_ci	struct ef4_farch_filter_spec spec;
23258c2ecf20Sopenharmony_ci	ef4_oword_t filter;
23268c2ecf20Sopenharmony_ci	int rep_index, ins_index;
23278c2ecf20Sopenharmony_ci	unsigned int depth = 0;
23288c2ecf20Sopenharmony_ci	int rc;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	rc = ef4_farch_filter_from_gen_spec(&spec, gen_spec);
23318c2ecf20Sopenharmony_ci	if (rc)
23328c2ecf20Sopenharmony_ci		return rc;
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	table = &state->table[ef4_farch_filter_spec_table_id(&spec)];
23358c2ecf20Sopenharmony_ci	if (table->size == 0)
23368c2ecf20Sopenharmony_ci		return -EINVAL;
23378c2ecf20Sopenharmony_ci
23388c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
23398c2ecf20Sopenharmony_ci		   "%s: type %d search_limit=%d", __func__, spec.type,
23408c2ecf20Sopenharmony_ci		   table->search_limit[spec.type]);
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci	if (table->id == EF4_FARCH_FILTER_TABLE_RX_DEF) {
23438c2ecf20Sopenharmony_ci		/* One filter spec per type */
23448c2ecf20Sopenharmony_ci		BUILD_BUG_ON(EF4_FARCH_FILTER_INDEX_UC_DEF != 0);
23458c2ecf20Sopenharmony_ci		BUILD_BUG_ON(EF4_FARCH_FILTER_INDEX_MC_DEF !=
23468c2ecf20Sopenharmony_ci			     EF4_FARCH_FILTER_MC_DEF - EF4_FARCH_FILTER_UC_DEF);
23478c2ecf20Sopenharmony_ci		rep_index = spec.type - EF4_FARCH_FILTER_UC_DEF;
23488c2ecf20Sopenharmony_ci		ins_index = rep_index;
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci		spin_lock_bh(&efx->filter_lock);
23518c2ecf20Sopenharmony_ci	} else {
23528c2ecf20Sopenharmony_ci		/* Search concurrently for
23538c2ecf20Sopenharmony_ci		 * (1) a filter to be replaced (rep_index): any filter
23548c2ecf20Sopenharmony_ci		 *     with the same match values, up to the current
23558c2ecf20Sopenharmony_ci		 *     search depth for this type, and
23568c2ecf20Sopenharmony_ci		 * (2) the insertion point (ins_index): (1) or any
23578c2ecf20Sopenharmony_ci		 *     free slot before it or up to the maximum search
23588c2ecf20Sopenharmony_ci		 *     depth for this priority
23598c2ecf20Sopenharmony_ci		 * We fail if we cannot find (2).
23608c2ecf20Sopenharmony_ci		 *
23618c2ecf20Sopenharmony_ci		 * We can stop once either
23628c2ecf20Sopenharmony_ci		 * (a) we find (1), in which case we have definitely
23638c2ecf20Sopenharmony_ci		 *     found (2) as well; or
23648c2ecf20Sopenharmony_ci		 * (b) we have searched exhaustively for (1), and have
23658c2ecf20Sopenharmony_ci		 *     either found (2) or searched exhaustively for it
23668c2ecf20Sopenharmony_ci		 */
23678c2ecf20Sopenharmony_ci		u32 key = ef4_farch_filter_build(&filter, &spec);
23688c2ecf20Sopenharmony_ci		unsigned int hash = ef4_farch_filter_hash(key);
23698c2ecf20Sopenharmony_ci		unsigned int incr = ef4_farch_filter_increment(key);
23708c2ecf20Sopenharmony_ci		unsigned int max_rep_depth = table->search_limit[spec.type];
23718c2ecf20Sopenharmony_ci		unsigned int max_ins_depth =
23728c2ecf20Sopenharmony_ci			spec.priority <= EF4_FILTER_PRI_HINT ?
23738c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_HINT_MAX :
23748c2ecf20Sopenharmony_ci			EF4_FARCH_FILTER_CTL_SRCH_MAX;
23758c2ecf20Sopenharmony_ci		unsigned int i = hash & (table->size - 1);
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci		ins_index = -1;
23788c2ecf20Sopenharmony_ci		depth = 1;
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci		spin_lock_bh(&efx->filter_lock);
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci		for (;;) {
23838c2ecf20Sopenharmony_ci			if (!test_bit(i, table->used_bitmap)) {
23848c2ecf20Sopenharmony_ci				if (ins_index < 0)
23858c2ecf20Sopenharmony_ci					ins_index = i;
23868c2ecf20Sopenharmony_ci			} else if (ef4_farch_filter_equal(&spec,
23878c2ecf20Sopenharmony_ci							  &table->spec[i])) {
23888c2ecf20Sopenharmony_ci				/* Case (a) */
23898c2ecf20Sopenharmony_ci				if (ins_index < 0)
23908c2ecf20Sopenharmony_ci					ins_index = i;
23918c2ecf20Sopenharmony_ci				rep_index = i;
23928c2ecf20Sopenharmony_ci				break;
23938c2ecf20Sopenharmony_ci			}
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci			if (depth >= max_rep_depth &&
23968c2ecf20Sopenharmony_ci			    (ins_index >= 0 || depth >= max_ins_depth)) {
23978c2ecf20Sopenharmony_ci				/* Case (b) */
23988c2ecf20Sopenharmony_ci				if (ins_index < 0) {
23998c2ecf20Sopenharmony_ci					rc = -EBUSY;
24008c2ecf20Sopenharmony_ci					goto out;
24018c2ecf20Sopenharmony_ci				}
24028c2ecf20Sopenharmony_ci				rep_index = -1;
24038c2ecf20Sopenharmony_ci				break;
24048c2ecf20Sopenharmony_ci			}
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci			i = (i + incr) & (table->size - 1);
24078c2ecf20Sopenharmony_ci			++depth;
24088c2ecf20Sopenharmony_ci		}
24098c2ecf20Sopenharmony_ci	}
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci	/* If we found a filter to be replaced, check whether we
24128c2ecf20Sopenharmony_ci	 * should do so
24138c2ecf20Sopenharmony_ci	 */
24148c2ecf20Sopenharmony_ci	if (rep_index >= 0) {
24158c2ecf20Sopenharmony_ci		struct ef4_farch_filter_spec *saved_spec =
24168c2ecf20Sopenharmony_ci			&table->spec[rep_index];
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci		if (spec.priority == saved_spec->priority && !replace_equal) {
24198c2ecf20Sopenharmony_ci			rc = -EEXIST;
24208c2ecf20Sopenharmony_ci			goto out;
24218c2ecf20Sopenharmony_ci		}
24228c2ecf20Sopenharmony_ci		if (spec.priority < saved_spec->priority) {
24238c2ecf20Sopenharmony_ci			rc = -EPERM;
24248c2ecf20Sopenharmony_ci			goto out;
24258c2ecf20Sopenharmony_ci		}
24268c2ecf20Sopenharmony_ci		if (saved_spec->priority == EF4_FILTER_PRI_AUTO ||
24278c2ecf20Sopenharmony_ci		    saved_spec->flags & EF4_FILTER_FLAG_RX_OVER_AUTO)
24288c2ecf20Sopenharmony_ci			spec.flags |= EF4_FILTER_FLAG_RX_OVER_AUTO;
24298c2ecf20Sopenharmony_ci	}
24308c2ecf20Sopenharmony_ci
24318c2ecf20Sopenharmony_ci	/* Insert the filter */
24328c2ecf20Sopenharmony_ci	if (ins_index != rep_index) {
24338c2ecf20Sopenharmony_ci		__set_bit(ins_index, table->used_bitmap);
24348c2ecf20Sopenharmony_ci		++table->used;
24358c2ecf20Sopenharmony_ci	}
24368c2ecf20Sopenharmony_ci	table->spec[ins_index] = spec;
24378c2ecf20Sopenharmony_ci
24388c2ecf20Sopenharmony_ci	if (table->id == EF4_FARCH_FILTER_TABLE_RX_DEF) {
24398c2ecf20Sopenharmony_ci		ef4_farch_filter_push_rx_config(efx);
24408c2ecf20Sopenharmony_ci	} else {
24418c2ecf20Sopenharmony_ci		if (table->search_limit[spec.type] < depth) {
24428c2ecf20Sopenharmony_ci			table->search_limit[spec.type] = depth;
24438c2ecf20Sopenharmony_ci			if (spec.flags & EF4_FILTER_FLAG_TX)
24448c2ecf20Sopenharmony_ci				ef4_farch_filter_push_tx_limits(efx);
24458c2ecf20Sopenharmony_ci			else
24468c2ecf20Sopenharmony_ci				ef4_farch_filter_push_rx_config(efx);
24478c2ecf20Sopenharmony_ci		}
24488c2ecf20Sopenharmony_ci
24498c2ecf20Sopenharmony_ci		ef4_writeo(efx, &filter,
24508c2ecf20Sopenharmony_ci			   table->offset + table->step * ins_index);
24518c2ecf20Sopenharmony_ci
24528c2ecf20Sopenharmony_ci		/* If we were able to replace a filter by inserting
24538c2ecf20Sopenharmony_ci		 * at a lower depth, clear the replaced filter
24548c2ecf20Sopenharmony_ci		 */
24558c2ecf20Sopenharmony_ci		if (ins_index != rep_index && rep_index >= 0)
24568c2ecf20Sopenharmony_ci			ef4_farch_filter_table_clear_entry(efx, table,
24578c2ecf20Sopenharmony_ci							   rep_index);
24588c2ecf20Sopenharmony_ci	}
24598c2ecf20Sopenharmony_ci
24608c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
24618c2ecf20Sopenharmony_ci		   "%s: filter type %d index %d rxq %u set",
24628c2ecf20Sopenharmony_ci		   __func__, spec.type, ins_index, spec.dmaq_id);
24638c2ecf20Sopenharmony_ci	rc = ef4_farch_filter_make_id(&spec, ins_index);
24648c2ecf20Sopenharmony_ci
24658c2ecf20Sopenharmony_ciout:
24668c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
24678c2ecf20Sopenharmony_ci	return rc;
24688c2ecf20Sopenharmony_ci}
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_cistatic void
24718c2ecf20Sopenharmony_cief4_farch_filter_table_clear_entry(struct ef4_nic *efx,
24728c2ecf20Sopenharmony_ci				   struct ef4_farch_filter_table *table,
24738c2ecf20Sopenharmony_ci				   unsigned int filter_idx)
24748c2ecf20Sopenharmony_ci{
24758c2ecf20Sopenharmony_ci	static ef4_oword_t filter;
24768c2ecf20Sopenharmony_ci
24778c2ecf20Sopenharmony_ci	EF4_WARN_ON_PARANOID(!test_bit(filter_idx, table->used_bitmap));
24788c2ecf20Sopenharmony_ci	BUG_ON(table->offset == 0); /* can't clear MAC default filters */
24798c2ecf20Sopenharmony_ci
24808c2ecf20Sopenharmony_ci	__clear_bit(filter_idx, table->used_bitmap);
24818c2ecf20Sopenharmony_ci	--table->used;
24828c2ecf20Sopenharmony_ci	memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci	ef4_writeo(efx, &filter, table->offset + table->step * filter_idx);
24858c2ecf20Sopenharmony_ci
24868c2ecf20Sopenharmony_ci	/* If this filter required a greater search depth than
24878c2ecf20Sopenharmony_ci	 * any other, the search limit for its type can now be
24888c2ecf20Sopenharmony_ci	 * decreased.  However, it is hard to determine that
24898c2ecf20Sopenharmony_ci	 * unless the table has become completely empty - in
24908c2ecf20Sopenharmony_ci	 * which case, all its search limits can be set to 0.
24918c2ecf20Sopenharmony_ci	 */
24928c2ecf20Sopenharmony_ci	if (unlikely(table->used == 0)) {
24938c2ecf20Sopenharmony_ci		memset(table->search_limit, 0, sizeof(table->search_limit));
24948c2ecf20Sopenharmony_ci		if (table->id == EF4_FARCH_FILTER_TABLE_TX_MAC)
24958c2ecf20Sopenharmony_ci			ef4_farch_filter_push_tx_limits(efx);
24968c2ecf20Sopenharmony_ci		else
24978c2ecf20Sopenharmony_ci			ef4_farch_filter_push_rx_config(efx);
24988c2ecf20Sopenharmony_ci	}
24998c2ecf20Sopenharmony_ci}
25008c2ecf20Sopenharmony_ci
25018c2ecf20Sopenharmony_cistatic int ef4_farch_filter_remove(struct ef4_nic *efx,
25028c2ecf20Sopenharmony_ci				   struct ef4_farch_filter_table *table,
25038c2ecf20Sopenharmony_ci				   unsigned int filter_idx,
25048c2ecf20Sopenharmony_ci				   enum ef4_filter_priority priority)
25058c2ecf20Sopenharmony_ci{
25068c2ecf20Sopenharmony_ci	struct ef4_farch_filter_spec *spec = &table->spec[filter_idx];
25078c2ecf20Sopenharmony_ci
25088c2ecf20Sopenharmony_ci	if (!test_bit(filter_idx, table->used_bitmap) ||
25098c2ecf20Sopenharmony_ci	    spec->priority != priority)
25108c2ecf20Sopenharmony_ci		return -ENOENT;
25118c2ecf20Sopenharmony_ci
25128c2ecf20Sopenharmony_ci	if (spec->flags & EF4_FILTER_FLAG_RX_OVER_AUTO) {
25138c2ecf20Sopenharmony_ci		ef4_farch_filter_init_rx_auto(efx, spec);
25148c2ecf20Sopenharmony_ci		ef4_farch_filter_push_rx_config(efx);
25158c2ecf20Sopenharmony_ci	} else {
25168c2ecf20Sopenharmony_ci		ef4_farch_filter_table_clear_entry(efx, table, filter_idx);
25178c2ecf20Sopenharmony_ci	}
25188c2ecf20Sopenharmony_ci
25198c2ecf20Sopenharmony_ci	return 0;
25208c2ecf20Sopenharmony_ci}
25218c2ecf20Sopenharmony_ci
25228c2ecf20Sopenharmony_ciint ef4_farch_filter_remove_safe(struct ef4_nic *efx,
25238c2ecf20Sopenharmony_ci				 enum ef4_filter_priority priority,
25248c2ecf20Sopenharmony_ci				 u32 filter_id)
25258c2ecf20Sopenharmony_ci{
25268c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
25278c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
25288c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
25298c2ecf20Sopenharmony_ci	unsigned int filter_idx;
25308c2ecf20Sopenharmony_ci	int rc;
25318c2ecf20Sopenharmony_ci
25328c2ecf20Sopenharmony_ci	table_id = ef4_farch_filter_id_table_id(filter_id);
25338c2ecf20Sopenharmony_ci	if ((unsigned int)table_id >= EF4_FARCH_FILTER_TABLE_COUNT)
25348c2ecf20Sopenharmony_ci		return -ENOENT;
25358c2ecf20Sopenharmony_ci	table = &state->table[table_id];
25368c2ecf20Sopenharmony_ci
25378c2ecf20Sopenharmony_ci	filter_idx = ef4_farch_filter_id_index(filter_id);
25388c2ecf20Sopenharmony_ci	if (filter_idx >= table->size)
25398c2ecf20Sopenharmony_ci		return -ENOENT;
25408c2ecf20Sopenharmony_ci
25418c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
25428c2ecf20Sopenharmony_ci	rc = ef4_farch_filter_remove(efx, table, filter_idx, priority);
25438c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_ci	return rc;
25468c2ecf20Sopenharmony_ci}
25478c2ecf20Sopenharmony_ci
25488c2ecf20Sopenharmony_ciint ef4_farch_filter_get_safe(struct ef4_nic *efx,
25498c2ecf20Sopenharmony_ci			      enum ef4_filter_priority priority,
25508c2ecf20Sopenharmony_ci			      u32 filter_id, struct ef4_filter_spec *spec_buf)
25518c2ecf20Sopenharmony_ci{
25528c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
25538c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
25548c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
25558c2ecf20Sopenharmony_ci	struct ef4_farch_filter_spec *spec;
25568c2ecf20Sopenharmony_ci	unsigned int filter_idx;
25578c2ecf20Sopenharmony_ci	int rc;
25588c2ecf20Sopenharmony_ci
25598c2ecf20Sopenharmony_ci	table_id = ef4_farch_filter_id_table_id(filter_id);
25608c2ecf20Sopenharmony_ci	if ((unsigned int)table_id >= EF4_FARCH_FILTER_TABLE_COUNT)
25618c2ecf20Sopenharmony_ci		return -ENOENT;
25628c2ecf20Sopenharmony_ci	table = &state->table[table_id];
25638c2ecf20Sopenharmony_ci
25648c2ecf20Sopenharmony_ci	filter_idx = ef4_farch_filter_id_index(filter_id);
25658c2ecf20Sopenharmony_ci	if (filter_idx >= table->size)
25668c2ecf20Sopenharmony_ci		return -ENOENT;
25678c2ecf20Sopenharmony_ci	spec = &table->spec[filter_idx];
25688c2ecf20Sopenharmony_ci
25698c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
25708c2ecf20Sopenharmony_ci
25718c2ecf20Sopenharmony_ci	if (test_bit(filter_idx, table->used_bitmap) &&
25728c2ecf20Sopenharmony_ci	    spec->priority == priority) {
25738c2ecf20Sopenharmony_ci		ef4_farch_filter_to_gen_spec(spec_buf, spec);
25748c2ecf20Sopenharmony_ci		rc = 0;
25758c2ecf20Sopenharmony_ci	} else {
25768c2ecf20Sopenharmony_ci		rc = -ENOENT;
25778c2ecf20Sopenharmony_ci	}
25788c2ecf20Sopenharmony_ci
25798c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
25808c2ecf20Sopenharmony_ci
25818c2ecf20Sopenharmony_ci	return rc;
25828c2ecf20Sopenharmony_ci}
25838c2ecf20Sopenharmony_ci
25848c2ecf20Sopenharmony_cistatic void
25858c2ecf20Sopenharmony_cief4_farch_filter_table_clear(struct ef4_nic *efx,
25868c2ecf20Sopenharmony_ci			     enum ef4_farch_filter_table_id table_id,
25878c2ecf20Sopenharmony_ci			     enum ef4_filter_priority priority)
25888c2ecf20Sopenharmony_ci{
25898c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
25908c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table = &state->table[table_id];
25918c2ecf20Sopenharmony_ci	unsigned int filter_idx;
25928c2ecf20Sopenharmony_ci
25938c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
25948c2ecf20Sopenharmony_ci	for (filter_idx = 0; filter_idx < table->size; ++filter_idx) {
25958c2ecf20Sopenharmony_ci		if (table->spec[filter_idx].priority != EF4_FILTER_PRI_AUTO)
25968c2ecf20Sopenharmony_ci			ef4_farch_filter_remove(efx, table,
25978c2ecf20Sopenharmony_ci						filter_idx, priority);
25988c2ecf20Sopenharmony_ci	}
25998c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
26008c2ecf20Sopenharmony_ci}
26018c2ecf20Sopenharmony_ci
26028c2ecf20Sopenharmony_ciint ef4_farch_filter_clear_rx(struct ef4_nic *efx,
26038c2ecf20Sopenharmony_ci			       enum ef4_filter_priority priority)
26048c2ecf20Sopenharmony_ci{
26058c2ecf20Sopenharmony_ci	ef4_farch_filter_table_clear(efx, EF4_FARCH_FILTER_TABLE_RX_IP,
26068c2ecf20Sopenharmony_ci				     priority);
26078c2ecf20Sopenharmony_ci	ef4_farch_filter_table_clear(efx, EF4_FARCH_FILTER_TABLE_RX_MAC,
26088c2ecf20Sopenharmony_ci				     priority);
26098c2ecf20Sopenharmony_ci	ef4_farch_filter_table_clear(efx, EF4_FARCH_FILTER_TABLE_RX_DEF,
26108c2ecf20Sopenharmony_ci				     priority);
26118c2ecf20Sopenharmony_ci	return 0;
26128c2ecf20Sopenharmony_ci}
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ciu32 ef4_farch_filter_count_rx_used(struct ef4_nic *efx,
26158c2ecf20Sopenharmony_ci				   enum ef4_filter_priority priority)
26168c2ecf20Sopenharmony_ci{
26178c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
26188c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
26198c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
26208c2ecf20Sopenharmony_ci	unsigned int filter_idx;
26218c2ecf20Sopenharmony_ci	u32 count = 0;
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_ci	for (table_id = EF4_FARCH_FILTER_TABLE_RX_IP;
26268c2ecf20Sopenharmony_ci	     table_id <= EF4_FARCH_FILTER_TABLE_RX_DEF;
26278c2ecf20Sopenharmony_ci	     table_id++) {
26288c2ecf20Sopenharmony_ci		table = &state->table[table_id];
26298c2ecf20Sopenharmony_ci		for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
26308c2ecf20Sopenharmony_ci			if (test_bit(filter_idx, table->used_bitmap) &&
26318c2ecf20Sopenharmony_ci			    table->spec[filter_idx].priority == priority)
26328c2ecf20Sopenharmony_ci				++count;
26338c2ecf20Sopenharmony_ci		}
26348c2ecf20Sopenharmony_ci	}
26358c2ecf20Sopenharmony_ci
26368c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
26378c2ecf20Sopenharmony_ci
26388c2ecf20Sopenharmony_ci	return count;
26398c2ecf20Sopenharmony_ci}
26408c2ecf20Sopenharmony_ci
26418c2ecf20Sopenharmony_cis32 ef4_farch_filter_get_rx_ids(struct ef4_nic *efx,
26428c2ecf20Sopenharmony_ci				enum ef4_filter_priority priority,
26438c2ecf20Sopenharmony_ci				u32 *buf, u32 size)
26448c2ecf20Sopenharmony_ci{
26458c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
26468c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
26478c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
26488c2ecf20Sopenharmony_ci	unsigned int filter_idx;
26498c2ecf20Sopenharmony_ci	s32 count = 0;
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
26528c2ecf20Sopenharmony_ci
26538c2ecf20Sopenharmony_ci	for (table_id = EF4_FARCH_FILTER_TABLE_RX_IP;
26548c2ecf20Sopenharmony_ci	     table_id <= EF4_FARCH_FILTER_TABLE_RX_DEF;
26558c2ecf20Sopenharmony_ci	     table_id++) {
26568c2ecf20Sopenharmony_ci		table = &state->table[table_id];
26578c2ecf20Sopenharmony_ci		for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
26588c2ecf20Sopenharmony_ci			if (test_bit(filter_idx, table->used_bitmap) &&
26598c2ecf20Sopenharmony_ci			    table->spec[filter_idx].priority == priority) {
26608c2ecf20Sopenharmony_ci				if (count == size) {
26618c2ecf20Sopenharmony_ci					count = -EMSGSIZE;
26628c2ecf20Sopenharmony_ci					goto out;
26638c2ecf20Sopenharmony_ci				}
26648c2ecf20Sopenharmony_ci				buf[count++] = ef4_farch_filter_make_id(
26658c2ecf20Sopenharmony_ci					&table->spec[filter_idx], filter_idx);
26668c2ecf20Sopenharmony_ci			}
26678c2ecf20Sopenharmony_ci		}
26688c2ecf20Sopenharmony_ci	}
26698c2ecf20Sopenharmony_ciout:
26708c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	return count;
26738c2ecf20Sopenharmony_ci}
26748c2ecf20Sopenharmony_ci
26758c2ecf20Sopenharmony_ci/* Restore filter stater after reset */
26768c2ecf20Sopenharmony_civoid ef4_farch_filter_table_restore(struct ef4_nic *efx)
26778c2ecf20Sopenharmony_ci{
26788c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
26798c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
26808c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
26818c2ecf20Sopenharmony_ci	ef4_oword_t filter;
26828c2ecf20Sopenharmony_ci	unsigned int filter_idx;
26838c2ecf20Sopenharmony_ci
26848c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
26858c2ecf20Sopenharmony_ci
26868c2ecf20Sopenharmony_ci	for (table_id = 0; table_id < EF4_FARCH_FILTER_TABLE_COUNT; table_id++) {
26878c2ecf20Sopenharmony_ci		table = &state->table[table_id];
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci		/* Check whether this is a regular register table */
26908c2ecf20Sopenharmony_ci		if (table->step == 0)
26918c2ecf20Sopenharmony_ci			continue;
26928c2ecf20Sopenharmony_ci
26938c2ecf20Sopenharmony_ci		for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
26948c2ecf20Sopenharmony_ci			if (!test_bit(filter_idx, table->used_bitmap))
26958c2ecf20Sopenharmony_ci				continue;
26968c2ecf20Sopenharmony_ci			ef4_farch_filter_build(&filter, &table->spec[filter_idx]);
26978c2ecf20Sopenharmony_ci			ef4_writeo(efx, &filter,
26988c2ecf20Sopenharmony_ci				   table->offset + table->step * filter_idx);
26998c2ecf20Sopenharmony_ci		}
27008c2ecf20Sopenharmony_ci	}
27018c2ecf20Sopenharmony_ci
27028c2ecf20Sopenharmony_ci	ef4_farch_filter_push_rx_config(efx);
27038c2ecf20Sopenharmony_ci	ef4_farch_filter_push_tx_limits(efx);
27048c2ecf20Sopenharmony_ci
27058c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
27068c2ecf20Sopenharmony_ci}
27078c2ecf20Sopenharmony_ci
27088c2ecf20Sopenharmony_civoid ef4_farch_filter_table_remove(struct ef4_nic *efx)
27098c2ecf20Sopenharmony_ci{
27108c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
27118c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_ci	for (table_id = 0; table_id < EF4_FARCH_FILTER_TABLE_COUNT; table_id++) {
27148c2ecf20Sopenharmony_ci		kfree(state->table[table_id].used_bitmap);
27158c2ecf20Sopenharmony_ci		vfree(state->table[table_id].spec);
27168c2ecf20Sopenharmony_ci	}
27178c2ecf20Sopenharmony_ci	kfree(state);
27188c2ecf20Sopenharmony_ci}
27198c2ecf20Sopenharmony_ci
27208c2ecf20Sopenharmony_ciint ef4_farch_filter_table_probe(struct ef4_nic *efx)
27218c2ecf20Sopenharmony_ci{
27228c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state;
27238c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
27248c2ecf20Sopenharmony_ci	unsigned table_id;
27258c2ecf20Sopenharmony_ci
27268c2ecf20Sopenharmony_ci	state = kzalloc(sizeof(struct ef4_farch_filter_state), GFP_KERNEL);
27278c2ecf20Sopenharmony_ci	if (!state)
27288c2ecf20Sopenharmony_ci		return -ENOMEM;
27298c2ecf20Sopenharmony_ci	efx->filter_state = state;
27308c2ecf20Sopenharmony_ci
27318c2ecf20Sopenharmony_ci	if (ef4_nic_rev(efx) >= EF4_REV_FALCON_B0) {
27328c2ecf20Sopenharmony_ci		table = &state->table[EF4_FARCH_FILTER_TABLE_RX_IP];
27338c2ecf20Sopenharmony_ci		table->id = EF4_FARCH_FILTER_TABLE_RX_IP;
27348c2ecf20Sopenharmony_ci		table->offset = FR_BZ_RX_FILTER_TBL0;
27358c2ecf20Sopenharmony_ci		table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
27368c2ecf20Sopenharmony_ci		table->step = FR_BZ_RX_FILTER_TBL0_STEP;
27378c2ecf20Sopenharmony_ci	}
27388c2ecf20Sopenharmony_ci
27398c2ecf20Sopenharmony_ci	for (table_id = 0; table_id < EF4_FARCH_FILTER_TABLE_COUNT; table_id++) {
27408c2ecf20Sopenharmony_ci		table = &state->table[table_id];
27418c2ecf20Sopenharmony_ci		if (table->size == 0)
27428c2ecf20Sopenharmony_ci			continue;
27438c2ecf20Sopenharmony_ci		table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
27448c2ecf20Sopenharmony_ci					     sizeof(unsigned long),
27458c2ecf20Sopenharmony_ci					     GFP_KERNEL);
27468c2ecf20Sopenharmony_ci		if (!table->used_bitmap)
27478c2ecf20Sopenharmony_ci			goto fail;
27488c2ecf20Sopenharmony_ci		table->spec = vzalloc(array_size(sizeof(*table->spec),
27498c2ecf20Sopenharmony_ci						 table->size));
27508c2ecf20Sopenharmony_ci		if (!table->spec)
27518c2ecf20Sopenharmony_ci			goto fail;
27528c2ecf20Sopenharmony_ci	}
27538c2ecf20Sopenharmony_ci
27548c2ecf20Sopenharmony_ci	table = &state->table[EF4_FARCH_FILTER_TABLE_RX_DEF];
27558c2ecf20Sopenharmony_ci	if (table->size) {
27568c2ecf20Sopenharmony_ci		/* RX default filters must always exist */
27578c2ecf20Sopenharmony_ci		struct ef4_farch_filter_spec *spec;
27588c2ecf20Sopenharmony_ci		unsigned i;
27598c2ecf20Sopenharmony_ci
27608c2ecf20Sopenharmony_ci		for (i = 0; i < EF4_FARCH_FILTER_SIZE_RX_DEF; i++) {
27618c2ecf20Sopenharmony_ci			spec = &table->spec[i];
27628c2ecf20Sopenharmony_ci			spec->type = EF4_FARCH_FILTER_UC_DEF + i;
27638c2ecf20Sopenharmony_ci			ef4_farch_filter_init_rx_auto(efx, spec);
27648c2ecf20Sopenharmony_ci			__set_bit(i, table->used_bitmap);
27658c2ecf20Sopenharmony_ci		}
27668c2ecf20Sopenharmony_ci	}
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci	ef4_farch_filter_push_rx_config(efx);
27698c2ecf20Sopenharmony_ci
27708c2ecf20Sopenharmony_ci	return 0;
27718c2ecf20Sopenharmony_ci
27728c2ecf20Sopenharmony_cifail:
27738c2ecf20Sopenharmony_ci	ef4_farch_filter_table_remove(efx);
27748c2ecf20Sopenharmony_ci	return -ENOMEM;
27758c2ecf20Sopenharmony_ci}
27768c2ecf20Sopenharmony_ci
27778c2ecf20Sopenharmony_ci/* Update scatter enable flags for filters pointing to our own RX queues */
27788c2ecf20Sopenharmony_civoid ef4_farch_filter_update_rx_scatter(struct ef4_nic *efx)
27798c2ecf20Sopenharmony_ci{
27808c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
27818c2ecf20Sopenharmony_ci	enum ef4_farch_filter_table_id table_id;
27828c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table;
27838c2ecf20Sopenharmony_ci	ef4_oword_t filter;
27848c2ecf20Sopenharmony_ci	unsigned int filter_idx;
27858c2ecf20Sopenharmony_ci
27868c2ecf20Sopenharmony_ci	spin_lock_bh(&efx->filter_lock);
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci	for (table_id = EF4_FARCH_FILTER_TABLE_RX_IP;
27898c2ecf20Sopenharmony_ci	     table_id <= EF4_FARCH_FILTER_TABLE_RX_DEF;
27908c2ecf20Sopenharmony_ci	     table_id++) {
27918c2ecf20Sopenharmony_ci		table = &state->table[table_id];
27928c2ecf20Sopenharmony_ci
27938c2ecf20Sopenharmony_ci		for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
27948c2ecf20Sopenharmony_ci			if (!test_bit(filter_idx, table->used_bitmap) ||
27958c2ecf20Sopenharmony_ci			    table->spec[filter_idx].dmaq_id >=
27968c2ecf20Sopenharmony_ci			    efx->n_rx_channels)
27978c2ecf20Sopenharmony_ci				continue;
27988c2ecf20Sopenharmony_ci
27998c2ecf20Sopenharmony_ci			if (efx->rx_scatter)
28008c2ecf20Sopenharmony_ci				table->spec[filter_idx].flags |=
28018c2ecf20Sopenharmony_ci					EF4_FILTER_FLAG_RX_SCATTER;
28028c2ecf20Sopenharmony_ci			else
28038c2ecf20Sopenharmony_ci				table->spec[filter_idx].flags &=
28048c2ecf20Sopenharmony_ci					~EF4_FILTER_FLAG_RX_SCATTER;
28058c2ecf20Sopenharmony_ci
28068c2ecf20Sopenharmony_ci			if (table_id == EF4_FARCH_FILTER_TABLE_RX_DEF)
28078c2ecf20Sopenharmony_ci				/* Pushed by ef4_farch_filter_push_rx_config() */
28088c2ecf20Sopenharmony_ci				continue;
28098c2ecf20Sopenharmony_ci
28108c2ecf20Sopenharmony_ci			ef4_farch_filter_build(&filter, &table->spec[filter_idx]);
28118c2ecf20Sopenharmony_ci			ef4_writeo(efx, &filter,
28128c2ecf20Sopenharmony_ci				   table->offset + table->step * filter_idx);
28138c2ecf20Sopenharmony_ci		}
28148c2ecf20Sopenharmony_ci	}
28158c2ecf20Sopenharmony_ci
28168c2ecf20Sopenharmony_ci	ef4_farch_filter_push_rx_config(efx);
28178c2ecf20Sopenharmony_ci
28188c2ecf20Sopenharmony_ci	spin_unlock_bh(&efx->filter_lock);
28198c2ecf20Sopenharmony_ci}
28208c2ecf20Sopenharmony_ci
28218c2ecf20Sopenharmony_ci#ifdef CONFIG_RFS_ACCEL
28228c2ecf20Sopenharmony_ci
28238c2ecf20Sopenharmony_cis32 ef4_farch_filter_rfs_insert(struct ef4_nic *efx,
28248c2ecf20Sopenharmony_ci				struct ef4_filter_spec *gen_spec)
28258c2ecf20Sopenharmony_ci{
28268c2ecf20Sopenharmony_ci	return ef4_farch_filter_insert(efx, gen_spec, true);
28278c2ecf20Sopenharmony_ci}
28288c2ecf20Sopenharmony_ci
28298c2ecf20Sopenharmony_cibool ef4_farch_filter_rfs_expire_one(struct ef4_nic *efx, u32 flow_id,
28308c2ecf20Sopenharmony_ci				     unsigned int index)
28318c2ecf20Sopenharmony_ci{
28328c2ecf20Sopenharmony_ci	struct ef4_farch_filter_state *state = efx->filter_state;
28338c2ecf20Sopenharmony_ci	struct ef4_farch_filter_table *table =
28348c2ecf20Sopenharmony_ci		&state->table[EF4_FARCH_FILTER_TABLE_RX_IP];
28358c2ecf20Sopenharmony_ci
28368c2ecf20Sopenharmony_ci	if (test_bit(index, table->used_bitmap) &&
28378c2ecf20Sopenharmony_ci	    table->spec[index].priority == EF4_FILTER_PRI_HINT &&
28388c2ecf20Sopenharmony_ci	    rps_may_expire_flow(efx->net_dev, table->spec[index].dmaq_id,
28398c2ecf20Sopenharmony_ci				flow_id, index)) {
28408c2ecf20Sopenharmony_ci		ef4_farch_filter_table_clear_entry(efx, table, index);
28418c2ecf20Sopenharmony_ci		return true;
28428c2ecf20Sopenharmony_ci	}
28438c2ecf20Sopenharmony_ci
28448c2ecf20Sopenharmony_ci	return false;
28458c2ecf20Sopenharmony_ci}
28468c2ecf20Sopenharmony_ci
28478c2ecf20Sopenharmony_ci#endif /* CONFIG_RFS_ACCEL */
28488c2ecf20Sopenharmony_ci
28498c2ecf20Sopenharmony_civoid ef4_farch_filter_sync_rx_mode(struct ef4_nic *efx)
28508c2ecf20Sopenharmony_ci{
28518c2ecf20Sopenharmony_ci	struct net_device *net_dev = efx->net_dev;
28528c2ecf20Sopenharmony_ci	struct netdev_hw_addr *ha;
28538c2ecf20Sopenharmony_ci	union ef4_multicast_hash *mc_hash = &efx->multicast_hash;
28548c2ecf20Sopenharmony_ci	u32 crc;
28558c2ecf20Sopenharmony_ci	int bit;
28568c2ecf20Sopenharmony_ci
28578c2ecf20Sopenharmony_ci	if (!ef4_dev_registered(efx))
28588c2ecf20Sopenharmony_ci		return;
28598c2ecf20Sopenharmony_ci
28608c2ecf20Sopenharmony_ci	netif_addr_lock_bh(net_dev);
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci	efx->unicast_filter = !(net_dev->flags & IFF_PROMISC);
28638c2ecf20Sopenharmony_ci
28648c2ecf20Sopenharmony_ci	/* Build multicast hash table */
28658c2ecf20Sopenharmony_ci	if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
28668c2ecf20Sopenharmony_ci		memset(mc_hash, 0xff, sizeof(*mc_hash));
28678c2ecf20Sopenharmony_ci	} else {
28688c2ecf20Sopenharmony_ci		memset(mc_hash, 0x00, sizeof(*mc_hash));
28698c2ecf20Sopenharmony_ci		netdev_for_each_mc_addr(ha, net_dev) {
28708c2ecf20Sopenharmony_ci			crc = ether_crc_le(ETH_ALEN, ha->addr);
28718c2ecf20Sopenharmony_ci			bit = crc & (EF4_MCAST_HASH_ENTRIES - 1);
28728c2ecf20Sopenharmony_ci			__set_bit_le(bit, mc_hash);
28738c2ecf20Sopenharmony_ci		}
28748c2ecf20Sopenharmony_ci
28758c2ecf20Sopenharmony_ci		/* Broadcast packets go through the multicast hash filter.
28768c2ecf20Sopenharmony_ci		 * ether_crc_le() of the broadcast address is 0xbe2612ff
28778c2ecf20Sopenharmony_ci		 * so we always add bit 0xff to the mask.
28788c2ecf20Sopenharmony_ci		 */
28798c2ecf20Sopenharmony_ci		__set_bit_le(0xff, mc_hash);
28808c2ecf20Sopenharmony_ci	}
28818c2ecf20Sopenharmony_ci
28828c2ecf20Sopenharmony_ci	netif_addr_unlock_bh(net_dev);
28838c2ecf20Sopenharmony_ci}
2884