162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/****************************************************************************
362306a36Sopenharmony_ci * Driver for Solarflare network controllers and boards
462306a36Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd.
562306a36Sopenharmony_ci * Copyright 2006-2013 Solarflare Communications Inc.
662306a36Sopenharmony_ci * Copyright 2019-2020 Xilinx Inc.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef EFX_NIC_COMMON_H
1062306a36Sopenharmony_ci#define EFX_NIC_COMMON_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include "net_driver.h"
1362306a36Sopenharmony_ci#include "efx_common.h"
1462306a36Sopenharmony_ci#include "mcdi.h"
1562306a36Sopenharmony_ci#include "ptp.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cienum {
1862306a36Sopenharmony_ci	/* Revisions 0-2 were Falcon A0, A1 and B0 respectively.
1962306a36Sopenharmony_ci	 * They are not supported by this driver but these revision numbers
2062306a36Sopenharmony_ci	 * form part of the ethtool API for register dumping.
2162306a36Sopenharmony_ci	 */
2262306a36Sopenharmony_ci	EFX_REV_SIENA_A0 = 3,
2362306a36Sopenharmony_ci	EFX_REV_HUNT_A0 = 4,
2462306a36Sopenharmony_ci	EFX_REV_EF100 = 5,
2562306a36Sopenharmony_ci};
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic inline int efx_nic_rev(struct efx_nic *efx)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	return efx->type->revision;
3062306a36Sopenharmony_ci}
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* Read the current event from the event queue */
3362306a36Sopenharmony_cistatic inline efx_qword_t *efx_event(struct efx_channel *channel,
3462306a36Sopenharmony_ci				     unsigned int index)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	return ((efx_qword_t *) (channel->eventq.buf.addr)) +
3762306a36Sopenharmony_ci		(index & channel->eventq_mask);
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* See if an event is present
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * We check both the high and low dword of the event for all ones.  We
4362306a36Sopenharmony_ci * wrote all ones when we cleared the event, and no valid event can
4462306a36Sopenharmony_ci * have all ones in either its high or low dwords.  This approach is
4562306a36Sopenharmony_ci * robust against reordering.
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * Note that using a single 64-bit comparison is incorrect; even
4862306a36Sopenharmony_ci * though the CPU read will be atomic, the DMA write may not be.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_cistatic inline int efx_event_present(efx_qword_t *event)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
5362306a36Sopenharmony_ci		  EFX_DWORD_IS_ALL_ONES(event->dword[1]));
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/* Returns a pointer to the specified transmit descriptor in the TX
5762306a36Sopenharmony_ci * descriptor queue belonging to the specified channel.
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_cistatic inline efx_qword_t *
6062306a36Sopenharmony_ciefx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	return ((efx_qword_t *) (tx_queue->txd.buf.addr)) + index;
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/* Report whether this TX queue would be empty for the given write_count.
6662306a36Sopenharmony_ci * May return false negative.
6762306a36Sopenharmony_ci */
6862306a36Sopenharmony_cistatic inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue, unsigned int write_count)
6962306a36Sopenharmony_ci{
7062306a36Sopenharmony_ci	unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	if (empty_read_count == 0)
7362306a36Sopenharmony_ci		return false;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/* Decide whether to push a TX descriptor to the NIC vs merely writing
7962306a36Sopenharmony_ci * the doorbell.  This can reduce latency when we are adding a single
8062306a36Sopenharmony_ci * descriptor to an empty queue, but is otherwise pointless.  Further,
8162306a36Sopenharmony_ci * Falcon and Siena have hardware bugs (SF bug 33851) that may be
8262306a36Sopenharmony_ci * triggered if we don't check this.
8362306a36Sopenharmony_ci * We use the write_count used for the last doorbell push, to get the
8462306a36Sopenharmony_ci * NIC's view of the tx queue.
8562306a36Sopenharmony_ci */
8662306a36Sopenharmony_cistatic inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
8762306a36Sopenharmony_ci					    unsigned int write_count)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	bool was_empty = efx_nic_tx_is_empty(tx_queue, write_count);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	tx_queue->empty_read_count = 0;
9262306a36Sopenharmony_ci	return was_empty && tx_queue->write_count - write_count == 1;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/* Returns a pointer to the specified descriptor in the RX descriptor queue */
9662306a36Sopenharmony_cistatic inline efx_qword_t *
9762306a36Sopenharmony_ciefx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	return ((efx_qword_t *) (rx_queue->rxd.buf.addr)) + index;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci/* Alignment of PCIe DMA boundaries (4KB) */
10362306a36Sopenharmony_ci#define EFX_PAGE_SIZE	4096
10462306a36Sopenharmony_ci/* Size and alignment of buffer table entries (same) */
10562306a36Sopenharmony_ci#define EFX_BUF_SIZE	EFX_PAGE_SIZE
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/* NIC-generic software stats */
10862306a36Sopenharmony_cienum {
10962306a36Sopenharmony_ci	GENERIC_STAT_rx_noskb_drops,
11062306a36Sopenharmony_ci	GENERIC_STAT_rx_nodesc_trunc,
11162306a36Sopenharmony_ci	GENERIC_STAT_COUNT
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci#define EFX_GENERIC_SW_STAT(ext_name)				\
11562306a36Sopenharmony_ci	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/* TX data path */
11862306a36Sopenharmony_cistatic inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return tx_queue->efx->type->tx_probe(tx_queue);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_cistatic inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
12362306a36Sopenharmony_ci{
12462306a36Sopenharmony_ci	tx_queue->efx->type->tx_init(tx_queue);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_cistatic inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	if (tx_queue->efx->type->tx_remove)
12962306a36Sopenharmony_ci		tx_queue->efx->type->tx_remove(tx_queue);
13062306a36Sopenharmony_ci}
13162306a36Sopenharmony_cistatic inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	tx_queue->efx->type->tx_write(tx_queue);
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci/* RX data path */
13762306a36Sopenharmony_cistatic inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
13862306a36Sopenharmony_ci{
13962306a36Sopenharmony_ci	return rx_queue->efx->type->rx_probe(rx_queue);
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_cistatic inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	rx_queue->efx->type->rx_init(rx_queue);
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_cistatic inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	rx_queue->efx->type->rx_remove(rx_queue);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_cistatic inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	rx_queue->efx->type->rx_write(rx_queue);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_cistatic inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	rx_queue->efx->type->rx_defer_refill(rx_queue);
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/* Event data path */
15962306a36Sopenharmony_cistatic inline int efx_nic_probe_eventq(struct efx_channel *channel)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	return channel->efx->type->ev_probe(channel);
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_cistatic inline int efx_nic_init_eventq(struct efx_channel *channel)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	return channel->efx->type->ev_init(channel);
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_cistatic inline void efx_nic_fini_eventq(struct efx_channel *channel)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	channel->efx->type->ev_fini(channel);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_cistatic inline void efx_nic_remove_eventq(struct efx_channel *channel)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	channel->efx->type->ev_remove(channel);
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_cistatic inline int
17662306a36Sopenharmony_ciefx_nic_process_eventq(struct efx_channel *channel, int quota)
17762306a36Sopenharmony_ci{
17862306a36Sopenharmony_ci	return channel->efx->type->ev_process(channel, quota);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_cistatic inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	channel->efx->type->ev_read_ack(channel);
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_civoid efx_siena_event_test_start(struct efx_channel *channel);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cibool efx_siena_event_present(struct efx_channel *channel);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cistatic inline void efx_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	if (efx->type->sensor_event)
19262306a36Sopenharmony_ci		efx->type->sensor_event(efx, ev);
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic inline unsigned int efx_rx_recycle_ring_size(const struct efx_nic *efx)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return efx->type->rx_recycle_ring_size(efx);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/* Some statistics are computed as A - B where A and B each increase
20162306a36Sopenharmony_ci * linearly with some hardware counter(s) and the counters are read
20262306a36Sopenharmony_ci * asynchronously.  If the counters contributing to B are always read
20362306a36Sopenharmony_ci * after those contributing to A, the computed value may be lower than
20462306a36Sopenharmony_ci * the true value by some variable amount, and may decrease between
20562306a36Sopenharmony_ci * subsequent computations.
20662306a36Sopenharmony_ci *
20762306a36Sopenharmony_ci * We should never allow statistics to decrease or to exceed the true
20862306a36Sopenharmony_ci * value.  Since the computed value will never be greater than the
20962306a36Sopenharmony_ci * true value, we can achieve this by only storing the computed value
21062306a36Sopenharmony_ci * when it increases.
21162306a36Sopenharmony_ci */
21262306a36Sopenharmony_cistatic inline void efx_update_diff_stat(u64 *stat, u64 diff)
21362306a36Sopenharmony_ci{
21462306a36Sopenharmony_ci	if ((s64)(diff - *stat) > 0)
21562306a36Sopenharmony_ci		*stat = diff;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/* Interrupts */
21962306a36Sopenharmony_ciint efx_siena_init_interrupt(struct efx_nic *efx);
22062306a36Sopenharmony_ciint efx_siena_irq_test_start(struct efx_nic *efx);
22162306a36Sopenharmony_civoid efx_siena_fini_interrupt(struct efx_nic *efx);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
22462306a36Sopenharmony_ci{
22562306a36Sopenharmony_ci	return READ_ONCE(channel->event_test_cpu);
22662306a36Sopenharmony_ci}
22762306a36Sopenharmony_cistatic inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	return READ_ONCE(efx->last_irq_cpu);
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci/* Global Resources */
23362306a36Sopenharmony_ciint efx_siena_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
23462306a36Sopenharmony_ci			   unsigned int len, gfp_t gfp_flags);
23562306a36Sopenharmony_civoid efx_siena_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_cisize_t efx_siena_get_regs_len(struct efx_nic *efx);
23862306a36Sopenharmony_civoid efx_siena_get_regs(struct efx_nic *efx, void *buf);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci#define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cisize_t efx_siena_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
24362306a36Sopenharmony_ci				const unsigned long *mask, u8 *names);
24462306a36Sopenharmony_civoid efx_siena_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
24562306a36Sopenharmony_ci			    const unsigned long *mask, u64 *stats,
24662306a36Sopenharmony_ci			    const void *dma_buf, bool accumulate);
24762306a36Sopenharmony_civoid efx_siena_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci#define EFX_MAX_FLUSH_TIME 5000
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci#endif /* EFX_NIC_COMMON_H */
252