19043f48fSEdward Cree /* SPDX-License-Identifier: GPL-2.0-only */
29043f48fSEdward Cree /****************************************************************************
39043f48fSEdward Cree  * Driver for Solarflare network controllers and boards
49043f48fSEdward Cree  * Copyright 2005-2006 Fen Systems Ltd.
59043f48fSEdward Cree  * Copyright 2006-2013 Solarflare Communications Inc.
69043f48fSEdward Cree  * Copyright 2019-2020 Xilinx Inc.
79043f48fSEdward Cree  */
89043f48fSEdward Cree 
99043f48fSEdward Cree #ifndef EFX_NIC_COMMON_H
109043f48fSEdward Cree #define EFX_NIC_COMMON_H
119043f48fSEdward Cree 
129043f48fSEdward Cree #include "net_driver.h"
139043f48fSEdward Cree #include "efx_common.h"
149043f48fSEdward Cree #include "mcdi.h"
159043f48fSEdward Cree #include "ptp.h"
169043f48fSEdward Cree 
179043f48fSEdward Cree enum {
18958d58bbSMartin Habets 	/* Revisions 0-3 were Falcon A0, A1, B0 and Siena respectively.
199043f48fSEdward Cree 	 * They are not supported by this driver but these revision numbers
209043f48fSEdward Cree 	 * form part of the ethtool API for register dumping.
219043f48fSEdward Cree 	 */
229043f48fSEdward Cree 	EFX_REV_HUNT_A0 = 4,
23805d22bfSEdward Cree 	EFX_REV_EF100 = 5,
249043f48fSEdward Cree };
259043f48fSEdward Cree 
efx_nic_rev(struct efx_nic * efx)269043f48fSEdward Cree static inline int efx_nic_rev(struct efx_nic *efx)
279043f48fSEdward Cree {
289043f48fSEdward Cree 	return efx->type->revision;
299043f48fSEdward Cree }
309043f48fSEdward Cree 
319043f48fSEdward Cree /* Read the current event from the event queue */
efx_event(struct efx_channel * channel,unsigned int index)329043f48fSEdward Cree static inline efx_qword_t *efx_event(struct efx_channel *channel,
339043f48fSEdward Cree 				     unsigned int index)
349043f48fSEdward Cree {
35d73e7715SMartin Habets 	return ((efx_qword_t *)(channel->eventq.addr)) +
369043f48fSEdward Cree 		(index & channel->eventq_mask);
379043f48fSEdward Cree }
389043f48fSEdward Cree 
399043f48fSEdward Cree /* See if an event is present
409043f48fSEdward Cree  *
419043f48fSEdward Cree  * We check both the high and low dword of the event for all ones.  We
429043f48fSEdward Cree  * wrote all ones when we cleared the event, and no valid event can
439043f48fSEdward Cree  * have all ones in either its high or low dwords.  This approach is
449043f48fSEdward Cree  * robust against reordering.
459043f48fSEdward Cree  *
469043f48fSEdward Cree  * Note that using a single 64-bit comparison is incorrect; even
479043f48fSEdward Cree  * though the CPU read will be atomic, the DMA write may not be.
489043f48fSEdward Cree  */
efx_event_present(efx_qword_t * event)499043f48fSEdward Cree static inline int efx_event_present(efx_qword_t *event)
509043f48fSEdward Cree {
519043f48fSEdward Cree 	return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
529043f48fSEdward Cree 		  EFX_DWORD_IS_ALL_ONES(event->dword[1]));
539043f48fSEdward Cree }
549043f48fSEdward Cree 
559043f48fSEdward Cree /* Returns a pointer to the specified transmit descriptor in the TX
569043f48fSEdward Cree  * descriptor queue belonging to the specified channel.
579043f48fSEdward Cree  */
589043f48fSEdward Cree static inline efx_qword_t *
efx_tx_desc(struct efx_tx_queue * tx_queue,unsigned int index)599043f48fSEdward Cree efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
609043f48fSEdward Cree {
61d73e7715SMartin Habets 	return ((efx_qword_t *)(tx_queue->txd.addr)) + index;
629043f48fSEdward Cree }
639043f48fSEdward Cree 
649043f48fSEdward Cree /* Report whether this TX queue would be empty for the given write_count.
659043f48fSEdward Cree  * May return false negative.
669043f48fSEdward Cree  */
efx_nic_tx_is_empty(struct efx_tx_queue * tx_queue,unsigned int write_count)678be41842SEdward Cree static inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue, unsigned int write_count)
689043f48fSEdward Cree {
699043f48fSEdward Cree 	unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
709043f48fSEdward Cree 
719043f48fSEdward Cree 	if (empty_read_count == 0)
729043f48fSEdward Cree 		return false;
739043f48fSEdward Cree 
749043f48fSEdward Cree 	return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0;
759043f48fSEdward Cree }
769043f48fSEdward Cree 
7793841000SEdward Cree int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
7893841000SEdward Cree 			bool *data_mapped);
7993841000SEdward Cree 
809043f48fSEdward Cree /* Decide whether to push a TX descriptor to the NIC vs merely writing
819043f48fSEdward Cree  * the doorbell.  This can reduce latency when we are adding a single
82*ae9d445cSMartin Habets  * descriptor to an empty queue, but is otherwise pointless.
839043f48fSEdward Cree  * We use the write_count used for the last doorbell push, to get the
849043f48fSEdward Cree  * NIC's view of the tx queue.
859043f48fSEdward Cree  */
efx_nic_may_push_tx_desc(struct efx_tx_queue * tx_queue,unsigned int write_count)869043f48fSEdward Cree static inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
879043f48fSEdward Cree 					    unsigned int write_count)
889043f48fSEdward Cree {
898be41842SEdward Cree 	bool was_empty = efx_nic_tx_is_empty(tx_queue, write_count);
909043f48fSEdward Cree 
919043f48fSEdward Cree 	tx_queue->empty_read_count = 0;
929043f48fSEdward Cree 	return was_empty && tx_queue->write_count - write_count == 1;
939043f48fSEdward Cree }
949043f48fSEdward Cree 
959043f48fSEdward Cree /* Returns a pointer to the specified descriptor in the RX descriptor queue */
969043f48fSEdward Cree static inline efx_qword_t *
efx_rx_desc(struct efx_rx_queue * rx_queue,unsigned int index)979043f48fSEdward Cree efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
989043f48fSEdward Cree {
99d73e7715SMartin Habets 	return ((efx_qword_t *)(rx_queue->rxd.addr)) + index;
1009043f48fSEdward Cree }
1019043f48fSEdward Cree 
1029043f48fSEdward Cree /* Alignment of PCIe DMA boundaries (4KB) */
1039043f48fSEdward Cree #define EFX_PAGE_SIZE	4096
1049043f48fSEdward Cree /* Size and alignment of buffer table entries (same) */
1059043f48fSEdward Cree #define EFX_BUF_SIZE	EFX_PAGE_SIZE
1069043f48fSEdward Cree 
1079043f48fSEdward Cree /* NIC-generic software stats */
1089043f48fSEdward Cree enum {
1099043f48fSEdward Cree 	GENERIC_STAT_rx_noskb_drops,
1109043f48fSEdward Cree 	GENERIC_STAT_rx_nodesc_trunc,
1119043f48fSEdward Cree 	GENERIC_STAT_COUNT
1129043f48fSEdward Cree };
1139043f48fSEdward Cree 
1149043f48fSEdward Cree #define EFX_GENERIC_SW_STAT(ext_name)				\
1159043f48fSEdward Cree 	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
1169043f48fSEdward Cree 
1179043f48fSEdward Cree /* TX data path */
efx_nic_probe_tx(struct efx_tx_queue * tx_queue)1189043f48fSEdward Cree static inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
1199043f48fSEdward Cree {
1209043f48fSEdward Cree 	return tx_queue->efx->type->tx_probe(tx_queue);
1219043f48fSEdward Cree }
efx_nic_init_tx(struct efx_tx_queue * tx_queue)1229043f48fSEdward Cree static inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
1239043f48fSEdward Cree {
1249043f48fSEdward Cree 	tx_queue->efx->type->tx_init(tx_queue);
1259043f48fSEdward Cree }
efx_nic_remove_tx(struct efx_tx_queue * tx_queue)1269043f48fSEdward Cree static inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
1279043f48fSEdward Cree {
128c72ae701SEdward Cree 	if (tx_queue->efx->type->tx_remove)
1299043f48fSEdward Cree 		tx_queue->efx->type->tx_remove(tx_queue);
1309043f48fSEdward Cree }
efx_nic_push_buffers(struct efx_tx_queue * tx_queue)1319043f48fSEdward Cree static inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
1329043f48fSEdward Cree {
1339043f48fSEdward Cree 	tx_queue->efx->type->tx_write(tx_queue);
1349043f48fSEdward Cree }
1359043f48fSEdward Cree 
1369043f48fSEdward Cree /* RX data path */
efx_nic_probe_rx(struct efx_rx_queue * rx_queue)1379043f48fSEdward Cree static inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
1389043f48fSEdward Cree {
1399043f48fSEdward Cree 	return rx_queue->efx->type->rx_probe(rx_queue);
1409043f48fSEdward Cree }
efx_nic_init_rx(struct efx_rx_queue * rx_queue)1419043f48fSEdward Cree static inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
1429043f48fSEdward Cree {
1439043f48fSEdward Cree 	rx_queue->efx->type->rx_init(rx_queue);
1449043f48fSEdward Cree }
efx_nic_remove_rx(struct efx_rx_queue * rx_queue)1459043f48fSEdward Cree static inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
1469043f48fSEdward Cree {
1479043f48fSEdward Cree 	rx_queue->efx->type->rx_remove(rx_queue);
1489043f48fSEdward Cree }
efx_nic_notify_rx_desc(struct efx_rx_queue * rx_queue)1499043f48fSEdward Cree static inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
1509043f48fSEdward Cree {
1519043f48fSEdward Cree 	rx_queue->efx->type->rx_write(rx_queue);
1529043f48fSEdward Cree }
efx_nic_generate_fill_event(struct efx_rx_queue * rx_queue)1539043f48fSEdward Cree static inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
1549043f48fSEdward Cree {
1559043f48fSEdward Cree 	rx_queue->efx->type->rx_defer_refill(rx_queue);
1569043f48fSEdward Cree }
1579043f48fSEdward Cree 
1589043f48fSEdward Cree /* Event data path */
efx_nic_probe_eventq(struct efx_channel * channel)1599043f48fSEdward Cree static inline int efx_nic_probe_eventq(struct efx_channel *channel)
1609043f48fSEdward Cree {
1619043f48fSEdward Cree 	return channel->efx->type->ev_probe(channel);
1629043f48fSEdward Cree }
efx_nic_init_eventq(struct efx_channel * channel)1639043f48fSEdward Cree static inline int efx_nic_init_eventq(struct efx_channel *channel)
1649043f48fSEdward Cree {
1659043f48fSEdward Cree 	return channel->efx->type->ev_init(channel);
1669043f48fSEdward Cree }
efx_nic_fini_eventq(struct efx_channel * channel)1679043f48fSEdward Cree static inline void efx_nic_fini_eventq(struct efx_channel *channel)
1689043f48fSEdward Cree {
1699043f48fSEdward Cree 	channel->efx->type->ev_fini(channel);
1709043f48fSEdward Cree }
efx_nic_remove_eventq(struct efx_channel * channel)1719043f48fSEdward Cree static inline void efx_nic_remove_eventq(struct efx_channel *channel)
1729043f48fSEdward Cree {
1739043f48fSEdward Cree 	channel->efx->type->ev_remove(channel);
1749043f48fSEdward Cree }
1759043f48fSEdward Cree static inline int
efx_nic_process_eventq(struct efx_channel * channel,int quota)1769043f48fSEdward Cree efx_nic_process_eventq(struct efx_channel *channel, int quota)
1779043f48fSEdward Cree {
1789043f48fSEdward Cree 	return channel->efx->type->ev_process(channel, quota);
1799043f48fSEdward Cree }
efx_nic_eventq_read_ack(struct efx_channel * channel)1809043f48fSEdward Cree static inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
1819043f48fSEdward Cree {
1829043f48fSEdward Cree 	channel->efx->type->ev_read_ack(channel);
1839043f48fSEdward Cree }
1849043f48fSEdward Cree 
1859043f48fSEdward Cree void efx_nic_event_test_start(struct efx_channel *channel);
1869043f48fSEdward Cree 
1879043f48fSEdward Cree bool efx_nic_event_present(struct efx_channel *channel);
1889043f48fSEdward Cree 
efx_sensor_event(struct efx_nic * efx,efx_qword_t * ev)18951b35a45SEdward Cree static inline void efx_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
19051b35a45SEdward Cree {
19151b35a45SEdward Cree 	if (efx->type->sensor_event)
19251b35a45SEdward Cree 		efx->type->sensor_event(efx, ev);
19351b35a45SEdward Cree }
19451b35a45SEdward Cree 
efx_rx_recycle_ring_size(const struct efx_nic * efx)195000fe940SMartin Habets static inline unsigned int efx_rx_recycle_ring_size(const struct efx_nic *efx)
196000fe940SMartin Habets {
197000fe940SMartin Habets 	return efx->type->rx_recycle_ring_size(efx);
198000fe940SMartin Habets }
199000fe940SMartin Habets 
2009043f48fSEdward Cree /* Some statistics are computed as A - B where A and B each increase
2019043f48fSEdward Cree  * linearly with some hardware counter(s) and the counters are read
2029043f48fSEdward Cree  * asynchronously.  If the counters contributing to B are always read
2039043f48fSEdward Cree  * after those contributing to A, the computed value may be lower than
2049043f48fSEdward Cree  * the true value by some variable amount, and may decrease between
2059043f48fSEdward Cree  * subsequent computations.
2069043f48fSEdward Cree  *
2079043f48fSEdward Cree  * We should never allow statistics to decrease or to exceed the true
2089043f48fSEdward Cree  * value.  Since the computed value will never be greater than the
2099043f48fSEdward Cree  * true value, we can achieve this by only storing the computed value
2109043f48fSEdward Cree  * when it increases.
2119043f48fSEdward Cree  */
efx_update_diff_stat(u64 * stat,u64 diff)2129043f48fSEdward Cree static inline void efx_update_diff_stat(u64 *stat, u64 diff)
2139043f48fSEdward Cree {
2149043f48fSEdward Cree 	if ((s64)(diff - *stat) > 0)
2159043f48fSEdward Cree 		*stat = diff;
2169043f48fSEdward Cree }
2179043f48fSEdward Cree 
2189043f48fSEdward Cree /* Interrupts */
2199043f48fSEdward Cree int efx_nic_init_interrupt(struct efx_nic *efx);
2209043f48fSEdward Cree int efx_nic_irq_test_start(struct efx_nic *efx);
2219043f48fSEdward Cree void efx_nic_fini_interrupt(struct efx_nic *efx);
2229043f48fSEdward Cree 
efx_nic_event_test_irq_cpu(struct efx_channel * channel)2239043f48fSEdward Cree static inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
2249043f48fSEdward Cree {
2259043f48fSEdward Cree 	return READ_ONCE(channel->event_test_cpu);
2269043f48fSEdward Cree }
efx_nic_irq_test_irq_cpu(struct efx_nic * efx)2279043f48fSEdward Cree static inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
2289043f48fSEdward Cree {
2299043f48fSEdward Cree 	return READ_ONCE(efx->last_irq_cpu);
2309043f48fSEdward Cree }
2319043f48fSEdward Cree 
2329043f48fSEdward Cree /* Global Resources */
2339043f48fSEdward Cree int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
2349043f48fSEdward Cree 			 unsigned int len, gfp_t gfp_flags);
2359043f48fSEdward Cree void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
2369043f48fSEdward Cree 
2379043f48fSEdward Cree size_t efx_nic_get_regs_len(struct efx_nic *efx);
2389043f48fSEdward Cree void efx_nic_get_regs(struct efx_nic *efx, void *buf);
2399043f48fSEdward Cree 
24020e1026cSEdward Cree #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
24120e1026cSEdward Cree 
2429043f48fSEdward Cree size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
2439043f48fSEdward Cree 			      const unsigned long *mask, u8 *names);
2449043f48fSEdward Cree int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest);
2459043f48fSEdward Cree void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
2469043f48fSEdward Cree 			  const unsigned long *mask, u64 *stats,
2479043f48fSEdward Cree 			  const void *dma_buf, bool accumulate);
2489043f48fSEdward Cree void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
efx_nic_update_stats_atomic(struct efx_nic * efx,u64 * full_stats,struct rtnl_link_stats64 * core_stats)249623b9988SEdward Cree static inline size_t efx_nic_update_stats_atomic(struct efx_nic *efx, u64 *full_stats,
250623b9988SEdward Cree 						 struct rtnl_link_stats64 *core_stats)
251623b9988SEdward Cree {
252623b9988SEdward Cree 	if (efx->type->update_stats_atomic)
253623b9988SEdward Cree 		return efx->type->update_stats_atomic(efx, full_stats, core_stats);
254623b9988SEdward Cree 	return efx->type->update_stats(efx, full_stats, core_stats);
255623b9988SEdward Cree }
2569043f48fSEdward Cree 
2579043f48fSEdward Cree #define EFX_MAX_FLUSH_TIME 5000
2589043f48fSEdward Cree 
2599043f48fSEdward Cree #endif /* EFX_NIC_COMMON_H */
260