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 {
189043f48fSEdward Cree 	/* Revisions 0-2 were Falcon A0, A1 and B0 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_SIENA_A0 = 3,
239043f48fSEdward Cree 	EFX_REV_HUNT_A0 = 4,
24805d22bfSEdward Cree 	EFX_REV_EF100 = 5,
259043f48fSEdward Cree };
269043f48fSEdward Cree 
279043f48fSEdward Cree static inline int efx_nic_rev(struct efx_nic *efx)
289043f48fSEdward Cree {
299043f48fSEdward Cree 	return efx->type->revision;
309043f48fSEdward Cree }
319043f48fSEdward Cree 
329043f48fSEdward Cree /* Read the current event from the event queue */
339043f48fSEdward Cree static inline efx_qword_t *efx_event(struct efx_channel *channel,
349043f48fSEdward Cree 				     unsigned int index)
359043f48fSEdward Cree {
369043f48fSEdward Cree 	return ((efx_qword_t *) (channel->eventq.buf.addr)) +
379043f48fSEdward Cree 		(index & channel->eventq_mask);
389043f48fSEdward Cree }
399043f48fSEdward Cree 
409043f48fSEdward Cree /* See if an event is present
419043f48fSEdward Cree  *
429043f48fSEdward Cree  * We check both the high and low dword of the event for all ones.  We
439043f48fSEdward Cree  * wrote all ones when we cleared the event, and no valid event can
449043f48fSEdward Cree  * have all ones in either its high or low dwords.  This approach is
459043f48fSEdward Cree  * robust against reordering.
469043f48fSEdward Cree  *
479043f48fSEdward Cree  * Note that using a single 64-bit comparison is incorrect; even
489043f48fSEdward Cree  * though the CPU read will be atomic, the DMA write may not be.
499043f48fSEdward Cree  */
509043f48fSEdward Cree static inline int efx_event_present(efx_qword_t *event)
519043f48fSEdward Cree {
529043f48fSEdward Cree 	return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
539043f48fSEdward Cree 		  EFX_DWORD_IS_ALL_ONES(event->dword[1]));
549043f48fSEdward Cree }
559043f48fSEdward Cree 
569043f48fSEdward Cree /* Returns a pointer to the specified transmit descriptor in the TX
579043f48fSEdward Cree  * descriptor queue belonging to the specified channel.
589043f48fSEdward Cree  */
599043f48fSEdward Cree static inline efx_qword_t *
609043f48fSEdward Cree efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
619043f48fSEdward Cree {
629043f48fSEdward Cree 	return ((efx_qword_t *) (tx_queue->txd.buf.addr)) + index;
639043f48fSEdward Cree }
649043f48fSEdward Cree 
659043f48fSEdward Cree /* Report whether this TX queue would be empty for the given write_count.
669043f48fSEdward Cree  * May return false negative.
679043f48fSEdward Cree  */
688be41842SEdward Cree static inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue, unsigned int write_count)
699043f48fSEdward Cree {
709043f48fSEdward Cree 	unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
719043f48fSEdward Cree 
729043f48fSEdward Cree 	if (empty_read_count == 0)
739043f48fSEdward Cree 		return false;
749043f48fSEdward Cree 
759043f48fSEdward Cree 	return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0;
769043f48fSEdward Cree }
779043f48fSEdward Cree 
789043f48fSEdward Cree /* Get partner of a TX queue, seen as part of the same net core queue */
799043f48fSEdward Cree /* XXX is this a thing on EF100? */
809043f48fSEdward Cree static inline struct efx_tx_queue *efx_tx_queue_partner(struct efx_tx_queue *tx_queue)
819043f48fSEdward Cree {
82a81dcd85SEdward Cree 	if (tx_queue->label & EFX_TXQ_TYPE_OFFLOAD)
839043f48fSEdward Cree 		return tx_queue - EFX_TXQ_TYPE_OFFLOAD;
849043f48fSEdward Cree 	else
859043f48fSEdward Cree 		return tx_queue + EFX_TXQ_TYPE_OFFLOAD;
869043f48fSEdward Cree }
879043f48fSEdward Cree 
8893841000SEdward Cree int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
8993841000SEdward Cree 			bool *data_mapped);
9093841000SEdward Cree 
919043f48fSEdward Cree /* Decide whether to push a TX descriptor to the NIC vs merely writing
929043f48fSEdward Cree  * the doorbell.  This can reduce latency when we are adding a single
939043f48fSEdward Cree  * descriptor to an empty queue, but is otherwise pointless.  Further,
949043f48fSEdward Cree  * Falcon and Siena have hardware bugs (SF bug 33851) that may be
959043f48fSEdward Cree  * triggered if we don't check this.
969043f48fSEdward Cree  * We use the write_count used for the last doorbell push, to get the
979043f48fSEdward Cree  * NIC's view of the tx queue.
989043f48fSEdward Cree  */
999043f48fSEdward Cree static inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
1009043f48fSEdward Cree 					    unsigned int write_count)
1019043f48fSEdward Cree {
1028be41842SEdward Cree 	bool was_empty = efx_nic_tx_is_empty(tx_queue, write_count);
1039043f48fSEdward Cree 
1049043f48fSEdward Cree 	tx_queue->empty_read_count = 0;
1059043f48fSEdward Cree 	return was_empty && tx_queue->write_count - write_count == 1;
1069043f48fSEdward Cree }
1079043f48fSEdward Cree 
1089043f48fSEdward Cree /* Returns a pointer to the specified descriptor in the RX descriptor queue */
1099043f48fSEdward Cree static inline efx_qword_t *
1109043f48fSEdward Cree efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
1119043f48fSEdward Cree {
1129043f48fSEdward Cree 	return ((efx_qword_t *) (rx_queue->rxd.buf.addr)) + index;
1139043f48fSEdward Cree }
1149043f48fSEdward Cree 
1159043f48fSEdward Cree /* Alignment of PCIe DMA boundaries (4KB) */
1169043f48fSEdward Cree #define EFX_PAGE_SIZE	4096
1179043f48fSEdward Cree /* Size and alignment of buffer table entries (same) */
1189043f48fSEdward Cree #define EFX_BUF_SIZE	EFX_PAGE_SIZE
1199043f48fSEdward Cree 
1209043f48fSEdward Cree /* NIC-generic software stats */
1219043f48fSEdward Cree enum {
1229043f48fSEdward Cree 	GENERIC_STAT_rx_noskb_drops,
1239043f48fSEdward Cree 	GENERIC_STAT_rx_nodesc_trunc,
1249043f48fSEdward Cree 	GENERIC_STAT_COUNT
1259043f48fSEdward Cree };
1269043f48fSEdward Cree 
1279043f48fSEdward Cree #define EFX_GENERIC_SW_STAT(ext_name)				\
1289043f48fSEdward Cree 	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
1299043f48fSEdward Cree 
1309043f48fSEdward Cree /* TX data path */
1319043f48fSEdward Cree static inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
1329043f48fSEdward Cree {
1339043f48fSEdward Cree 	return tx_queue->efx->type->tx_probe(tx_queue);
1349043f48fSEdward Cree }
1359043f48fSEdward Cree static inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
1369043f48fSEdward Cree {
1379043f48fSEdward Cree 	tx_queue->efx->type->tx_init(tx_queue);
1389043f48fSEdward Cree }
1399043f48fSEdward Cree static inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
1409043f48fSEdward Cree {
141c72ae701SEdward Cree 	if (tx_queue->efx->type->tx_remove)
1429043f48fSEdward Cree 		tx_queue->efx->type->tx_remove(tx_queue);
1439043f48fSEdward Cree }
1449043f48fSEdward Cree static inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
1459043f48fSEdward Cree {
1469043f48fSEdward Cree 	tx_queue->efx->type->tx_write(tx_queue);
1479043f48fSEdward Cree }
1489043f48fSEdward Cree 
1499043f48fSEdward Cree /* RX data path */
1509043f48fSEdward Cree static inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
1519043f48fSEdward Cree {
1529043f48fSEdward Cree 	return rx_queue->efx->type->rx_probe(rx_queue);
1539043f48fSEdward Cree }
1549043f48fSEdward Cree static inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
1559043f48fSEdward Cree {
1569043f48fSEdward Cree 	rx_queue->efx->type->rx_init(rx_queue);
1579043f48fSEdward Cree }
1589043f48fSEdward Cree static inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
1599043f48fSEdward Cree {
1609043f48fSEdward Cree 	rx_queue->efx->type->rx_remove(rx_queue);
1619043f48fSEdward Cree }
1629043f48fSEdward Cree static inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
1639043f48fSEdward Cree {
1649043f48fSEdward Cree 	rx_queue->efx->type->rx_write(rx_queue);
1659043f48fSEdward Cree }
1669043f48fSEdward Cree static inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
1679043f48fSEdward Cree {
1689043f48fSEdward Cree 	rx_queue->efx->type->rx_defer_refill(rx_queue);
1699043f48fSEdward Cree }
1709043f48fSEdward Cree 
1719043f48fSEdward Cree /* Event data path */
1729043f48fSEdward Cree static inline int efx_nic_probe_eventq(struct efx_channel *channel)
1739043f48fSEdward Cree {
1749043f48fSEdward Cree 	return channel->efx->type->ev_probe(channel);
1759043f48fSEdward Cree }
1769043f48fSEdward Cree static inline int efx_nic_init_eventq(struct efx_channel *channel)
1779043f48fSEdward Cree {
1789043f48fSEdward Cree 	return channel->efx->type->ev_init(channel);
1799043f48fSEdward Cree }
1809043f48fSEdward Cree static inline void efx_nic_fini_eventq(struct efx_channel *channel)
1819043f48fSEdward Cree {
1829043f48fSEdward Cree 	channel->efx->type->ev_fini(channel);
1839043f48fSEdward Cree }
1849043f48fSEdward Cree static inline void efx_nic_remove_eventq(struct efx_channel *channel)
1859043f48fSEdward Cree {
1869043f48fSEdward Cree 	channel->efx->type->ev_remove(channel);
1879043f48fSEdward Cree }
1889043f48fSEdward Cree static inline int
1899043f48fSEdward Cree efx_nic_process_eventq(struct efx_channel *channel, int quota)
1909043f48fSEdward Cree {
1919043f48fSEdward Cree 	return channel->efx->type->ev_process(channel, quota);
1929043f48fSEdward Cree }
1939043f48fSEdward Cree static inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
1949043f48fSEdward Cree {
1959043f48fSEdward Cree 	channel->efx->type->ev_read_ack(channel);
1969043f48fSEdward Cree }
1979043f48fSEdward Cree 
1989043f48fSEdward Cree void efx_nic_event_test_start(struct efx_channel *channel);
1999043f48fSEdward Cree 
2009043f48fSEdward Cree bool efx_nic_event_present(struct efx_channel *channel);
2019043f48fSEdward Cree 
20251b35a45SEdward Cree static inline void efx_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
20351b35a45SEdward Cree {
20451b35a45SEdward Cree 	if (efx->type->sensor_event)
20551b35a45SEdward Cree 		efx->type->sensor_event(efx, ev);
20651b35a45SEdward Cree }
20751b35a45SEdward Cree 
2089043f48fSEdward Cree /* Some statistics are computed as A - B where A and B each increase
2099043f48fSEdward Cree  * linearly with some hardware counter(s) and the counters are read
2109043f48fSEdward Cree  * asynchronously.  If the counters contributing to B are always read
2119043f48fSEdward Cree  * after those contributing to A, the computed value may be lower than
2129043f48fSEdward Cree  * the true value by some variable amount, and may decrease between
2139043f48fSEdward Cree  * subsequent computations.
2149043f48fSEdward Cree  *
2159043f48fSEdward Cree  * We should never allow statistics to decrease or to exceed the true
2169043f48fSEdward Cree  * value.  Since the computed value will never be greater than the
2179043f48fSEdward Cree  * true value, we can achieve this by only storing the computed value
2189043f48fSEdward Cree  * when it increases.
2199043f48fSEdward Cree  */
2209043f48fSEdward Cree static inline void efx_update_diff_stat(u64 *stat, u64 diff)
2219043f48fSEdward Cree {
2229043f48fSEdward Cree 	if ((s64)(diff - *stat) > 0)
2239043f48fSEdward Cree 		*stat = diff;
2249043f48fSEdward Cree }
2259043f48fSEdward Cree 
2269043f48fSEdward Cree /* Interrupts */
2279043f48fSEdward Cree int efx_nic_init_interrupt(struct efx_nic *efx);
2289043f48fSEdward Cree int efx_nic_irq_test_start(struct efx_nic *efx);
2299043f48fSEdward Cree void efx_nic_fini_interrupt(struct efx_nic *efx);
2309043f48fSEdward Cree 
2319043f48fSEdward Cree static inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
2329043f48fSEdward Cree {
2339043f48fSEdward Cree 	return READ_ONCE(channel->event_test_cpu);
2349043f48fSEdward Cree }
2359043f48fSEdward Cree static inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
2369043f48fSEdward Cree {
2379043f48fSEdward Cree 	return READ_ONCE(efx->last_irq_cpu);
2389043f48fSEdward Cree }
2399043f48fSEdward Cree 
2409043f48fSEdward Cree /* Global Resources */
2419043f48fSEdward Cree int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
2429043f48fSEdward Cree 			 unsigned int len, gfp_t gfp_flags);
2439043f48fSEdward Cree void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
2449043f48fSEdward Cree 
2459043f48fSEdward Cree size_t efx_nic_get_regs_len(struct efx_nic *efx);
2469043f48fSEdward Cree void efx_nic_get_regs(struct efx_nic *efx, void *buf);
2479043f48fSEdward Cree 
24820e1026cSEdward Cree #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
24920e1026cSEdward Cree 
2509043f48fSEdward Cree size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
2519043f48fSEdward Cree 			      const unsigned long *mask, u8 *names);
2529043f48fSEdward Cree int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest);
2539043f48fSEdward Cree void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
2549043f48fSEdward Cree 			  const unsigned long *mask, u64 *stats,
2559043f48fSEdward Cree 			  const void *dma_buf, bool accumulate);
2569043f48fSEdward Cree void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
2579043f48fSEdward Cree 
2589043f48fSEdward Cree #define EFX_MAX_FLUSH_TIME 5000
2599043f48fSEdward Cree 
2609043f48fSEdward Cree #endif /* EFX_NIC_COMMON_H */
261