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 * Copyright 2019-2020 Xilinx Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef EFX_NIC_COMMON_H
108c2ecf20Sopenharmony_ci#define EFX_NIC_COMMON_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "net_driver.h"
138c2ecf20Sopenharmony_ci#include "efx_common.h"
148c2ecf20Sopenharmony_ci#include "mcdi.h"
158c2ecf20Sopenharmony_ci#include "ptp.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cienum {
188c2ecf20Sopenharmony_ci	/* Revisions 0-2 were Falcon A0, A1 and B0 respectively.
198c2ecf20Sopenharmony_ci	 * They are not supported by this driver but these revision numbers
208c2ecf20Sopenharmony_ci	 * form part of the ethtool API for register dumping.
218c2ecf20Sopenharmony_ci	 */
228c2ecf20Sopenharmony_ci	EFX_REV_SIENA_A0 = 3,
238c2ecf20Sopenharmony_ci	EFX_REV_HUNT_A0 = 4,
248c2ecf20Sopenharmony_ci	EFX_REV_EF100 = 5,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic inline int efx_nic_rev(struct efx_nic *efx)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	return efx->type->revision;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Read the current event from the event queue */
338c2ecf20Sopenharmony_cistatic inline efx_qword_t *efx_event(struct efx_channel *channel,
348c2ecf20Sopenharmony_ci				     unsigned int index)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	return ((efx_qword_t *) (channel->eventq.buf.addr)) +
378c2ecf20Sopenharmony_ci		(index & channel->eventq_mask);
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* See if an event is present
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * We check both the high and low dword of the event for all ones.  We
438c2ecf20Sopenharmony_ci * wrote all ones when we cleared the event, and no valid event can
448c2ecf20Sopenharmony_ci * have all ones in either its high or low dwords.  This approach is
458c2ecf20Sopenharmony_ci * robust against reordering.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * Note that using a single 64-bit comparison is incorrect; even
488c2ecf20Sopenharmony_ci * though the CPU read will be atomic, the DMA write may not be.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_cistatic inline int efx_event_present(efx_qword_t *event)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
538c2ecf20Sopenharmony_ci		  EFX_DWORD_IS_ALL_ONES(event->dword[1]));
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Returns a pointer to the specified transmit descriptor in the TX
578c2ecf20Sopenharmony_ci * descriptor queue belonging to the specified channel.
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_cistatic inline efx_qword_t *
608c2ecf20Sopenharmony_ciefx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return ((efx_qword_t *) (tx_queue->txd.buf.addr)) + index;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* Report whether this TX queue would be empty for the given write_count.
668c2ecf20Sopenharmony_ci * May return false negative.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue, unsigned int write_count)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (empty_read_count == 0)
738c2ecf20Sopenharmony_ci		return false;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciint efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
798c2ecf20Sopenharmony_ci			bool *data_mapped);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* Decide whether to push a TX descriptor to the NIC vs merely writing
828c2ecf20Sopenharmony_ci * the doorbell.  This can reduce latency when we are adding a single
838c2ecf20Sopenharmony_ci * descriptor to an empty queue, but is otherwise pointless.  Further,
848c2ecf20Sopenharmony_ci * Falcon and Siena have hardware bugs (SF bug 33851) that may be
858c2ecf20Sopenharmony_ci * triggered if we don't check this.
868c2ecf20Sopenharmony_ci * We use the write_count used for the last doorbell push, to get the
878c2ecf20Sopenharmony_ci * NIC's view of the tx queue.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_cistatic inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
908c2ecf20Sopenharmony_ci					    unsigned int write_count)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	bool was_empty = efx_nic_tx_is_empty(tx_queue, write_count);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	tx_queue->empty_read_count = 0;
958c2ecf20Sopenharmony_ci	return was_empty && tx_queue->write_count - write_count == 1;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* Returns a pointer to the specified descriptor in the RX descriptor queue */
998c2ecf20Sopenharmony_cistatic inline efx_qword_t *
1008c2ecf20Sopenharmony_ciefx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return ((efx_qword_t *) (rx_queue->rxd.buf.addr)) + index;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/* Alignment of PCIe DMA boundaries (4KB) */
1068c2ecf20Sopenharmony_ci#define EFX_PAGE_SIZE	4096
1078c2ecf20Sopenharmony_ci/* Size and alignment of buffer table entries (same) */
1088c2ecf20Sopenharmony_ci#define EFX_BUF_SIZE	EFX_PAGE_SIZE
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* NIC-generic software stats */
1118c2ecf20Sopenharmony_cienum {
1128c2ecf20Sopenharmony_ci	GENERIC_STAT_rx_noskb_drops,
1138c2ecf20Sopenharmony_ci	GENERIC_STAT_rx_nodesc_trunc,
1148c2ecf20Sopenharmony_ci	GENERIC_STAT_COUNT
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define EFX_GENERIC_SW_STAT(ext_name)				\
1188c2ecf20Sopenharmony_ci	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* TX data path */
1218c2ecf20Sopenharmony_cistatic inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return tx_queue->efx->type->tx_probe(tx_queue);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_cistatic inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	tx_queue->efx->type->tx_init(tx_queue);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_cistatic inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	if (tx_queue->efx->type->tx_remove)
1328c2ecf20Sopenharmony_ci		tx_queue->efx->type->tx_remove(tx_queue);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_cistatic inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	tx_queue->efx->type->tx_write(tx_queue);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* RX data path */
1408c2ecf20Sopenharmony_cistatic inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return rx_queue->efx->type->rx_probe(rx_queue);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_cistatic inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	rx_queue->efx->type->rx_init(rx_queue);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_cistatic inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	rx_queue->efx->type->rx_remove(rx_queue);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_cistatic inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	rx_queue->efx->type->rx_write(rx_queue);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_cistatic inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	rx_queue->efx->type->rx_defer_refill(rx_queue);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* Event data path */
1628c2ecf20Sopenharmony_cistatic inline int efx_nic_probe_eventq(struct efx_channel *channel)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	return channel->efx->type->ev_probe(channel);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_cistatic inline int efx_nic_init_eventq(struct efx_channel *channel)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	return channel->efx->type->ev_init(channel);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_cistatic inline void efx_nic_fini_eventq(struct efx_channel *channel)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	channel->efx->type->ev_fini(channel);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_cistatic inline void efx_nic_remove_eventq(struct efx_channel *channel)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	channel->efx->type->ev_remove(channel);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_cistatic inline int
1798c2ecf20Sopenharmony_ciefx_nic_process_eventq(struct efx_channel *channel, int quota)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	return channel->efx->type->ev_process(channel, quota);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_cistatic inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	channel->efx->type->ev_read_ack(channel);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_civoid efx_nic_event_test_start(struct efx_channel *channel);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cibool efx_nic_event_present(struct efx_channel *channel);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline void efx_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	if (efx->type->sensor_event)
1958c2ecf20Sopenharmony_ci		efx->type->sensor_event(efx, ev);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* Some statistics are computed as A - B where A and B each increase
1998c2ecf20Sopenharmony_ci * linearly with some hardware counter(s) and the counters are read
2008c2ecf20Sopenharmony_ci * asynchronously.  If the counters contributing to B are always read
2018c2ecf20Sopenharmony_ci * after those contributing to A, the computed value may be lower than
2028c2ecf20Sopenharmony_ci * the true value by some variable amount, and may decrease between
2038c2ecf20Sopenharmony_ci * subsequent computations.
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * We should never allow statistics to decrease or to exceed the true
2068c2ecf20Sopenharmony_ci * value.  Since the computed value will never be greater than the
2078c2ecf20Sopenharmony_ci * true value, we can achieve this by only storing the computed value
2088c2ecf20Sopenharmony_ci * when it increases.
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_cistatic inline void efx_update_diff_stat(u64 *stat, u64 diff)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	if ((s64)(diff - *stat) > 0)
2138c2ecf20Sopenharmony_ci		*stat = diff;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/* Interrupts */
2178c2ecf20Sopenharmony_ciint efx_nic_init_interrupt(struct efx_nic *efx);
2188c2ecf20Sopenharmony_ciint efx_nic_irq_test_start(struct efx_nic *efx);
2198c2ecf20Sopenharmony_civoid efx_nic_fini_interrupt(struct efx_nic *efx);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	return READ_ONCE(channel->event_test_cpu);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_cistatic inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	return READ_ONCE(efx->last_irq_cpu);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/* Global Resources */
2318c2ecf20Sopenharmony_ciint efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
2328c2ecf20Sopenharmony_ci			 unsigned int len, gfp_t gfp_flags);
2338c2ecf20Sopenharmony_civoid efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cisize_t efx_nic_get_regs_len(struct efx_nic *efx);
2368c2ecf20Sopenharmony_civoid efx_nic_get_regs(struct efx_nic *efx, void *buf);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci#define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cisize_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
2418c2ecf20Sopenharmony_ci			      const unsigned long *mask, u8 *names);
2428c2ecf20Sopenharmony_ciint efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest);
2438c2ecf20Sopenharmony_civoid efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
2448c2ecf20Sopenharmony_ci			  const unsigned long *mask, u64 *stats,
2458c2ecf20Sopenharmony_ci			  const void *dma_buf, bool accumulate);
2468c2ecf20Sopenharmony_civoid efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
2478c2ecf20Sopenharmony_cistatic inline size_t efx_nic_update_stats_atomic(struct efx_nic *efx, u64 *full_stats,
2488c2ecf20Sopenharmony_ci						 struct rtnl_link_stats64 *core_stats)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	if (efx->type->update_stats_atomic)
2518c2ecf20Sopenharmony_ci		return efx->type->update_stats_atomic(efx, full_stats, core_stats);
2528c2ecf20Sopenharmony_ci	return efx->type->update_stats(efx, full_stats, core_stats);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#define EFX_MAX_FLUSH_TIME 5000
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#endif /* EFX_NIC_COMMON_H */
258