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,
249043f48fSEdward Cree };
259043f48fSEdward Cree 
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 */
329043f48fSEdward Cree static inline efx_qword_t *efx_event(struct efx_channel *channel,
339043f48fSEdward Cree 				     unsigned int index)
349043f48fSEdward Cree {
359043f48fSEdward Cree 	return ((efx_qword_t *) (channel->eventq.buf.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  */
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 *
599043f48fSEdward Cree efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
609043f48fSEdward Cree {
619043f48fSEdward Cree 	return ((efx_qword_t *) (tx_queue->txd.buf.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  */
679043f48fSEdward Cree static inline bool __efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue,
689043f48fSEdward Cree 					 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 /* Report whether the NIC considers this TX queue empty, using
799043f48fSEdward Cree  * packet_write_count (the write count recorded for the last completable
809043f48fSEdward Cree  * doorbell push).  May return false negative.  EF10 only, which is OK
819043f48fSEdward Cree  * because only EF10 supports PIO.
829043f48fSEdward Cree  */
839043f48fSEdward Cree static inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue)
849043f48fSEdward Cree {
859043f48fSEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(!tx_queue->efx->type->option_descriptors);
869043f48fSEdward Cree 	return __efx_nic_tx_is_empty(tx_queue, tx_queue->packet_write_count);
879043f48fSEdward Cree }
889043f48fSEdward Cree 
899043f48fSEdward Cree /* Get partner of a TX queue, seen as part of the same net core queue */
909043f48fSEdward Cree /* XXX is this a thing on EF100? */
919043f48fSEdward Cree static inline struct efx_tx_queue *efx_tx_queue_partner(struct efx_tx_queue *tx_queue)
929043f48fSEdward Cree {
939043f48fSEdward Cree 	if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD)
949043f48fSEdward Cree 		return tx_queue - EFX_TXQ_TYPE_OFFLOAD;
959043f48fSEdward Cree 	else
969043f48fSEdward Cree 		return tx_queue + EFX_TXQ_TYPE_OFFLOAD;
979043f48fSEdward Cree }
989043f48fSEdward Cree 
999043f48fSEdward Cree /* Decide whether we can use TX PIO, ie. write packet data directly into
1009043f48fSEdward Cree  * a buffer on the device.  This can reduce latency at the expense of
1019043f48fSEdward Cree  * throughput, so we only do this if both hardware and software TX rings
1029043f48fSEdward Cree  * are empty.  This also ensures that only one packet at a time can be
1039043f48fSEdward Cree  * using the PIO buffer.
1049043f48fSEdward Cree  */
1059043f48fSEdward Cree static inline bool efx_nic_may_tx_pio(struct efx_tx_queue *tx_queue)
1069043f48fSEdward Cree {
1079043f48fSEdward Cree 	struct efx_tx_queue *partner = efx_tx_queue_partner(tx_queue);
1089043f48fSEdward Cree 
1099043f48fSEdward Cree 	return tx_queue->piobuf && efx_nic_tx_is_empty(tx_queue) &&
1109043f48fSEdward Cree 	       efx_nic_tx_is_empty(partner);
1119043f48fSEdward Cree }
1129043f48fSEdward Cree 
11393841000SEdward Cree int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
11493841000SEdward Cree 			bool *data_mapped);
11593841000SEdward Cree 
1169043f48fSEdward Cree /* Decide whether to push a TX descriptor to the NIC vs merely writing
1179043f48fSEdward Cree  * the doorbell.  This can reduce latency when we are adding a single
1189043f48fSEdward Cree  * descriptor to an empty queue, but is otherwise pointless.  Further,
1199043f48fSEdward Cree  * Falcon and Siena have hardware bugs (SF bug 33851) that may be
1209043f48fSEdward Cree  * triggered if we don't check this.
1219043f48fSEdward Cree  * We use the write_count used for the last doorbell push, to get the
1229043f48fSEdward Cree  * NIC's view of the tx queue.
1239043f48fSEdward Cree  */
1249043f48fSEdward Cree static inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
1259043f48fSEdward Cree 					    unsigned int write_count)
1269043f48fSEdward Cree {
1279043f48fSEdward Cree 	bool was_empty = __efx_nic_tx_is_empty(tx_queue, write_count);
1289043f48fSEdward Cree 
1299043f48fSEdward Cree 	tx_queue->empty_read_count = 0;
1309043f48fSEdward Cree 	return was_empty && tx_queue->write_count - write_count == 1;
1319043f48fSEdward Cree }
1329043f48fSEdward Cree 
1339043f48fSEdward Cree /* Returns a pointer to the specified descriptor in the RX descriptor queue */
1349043f48fSEdward Cree static inline efx_qword_t *
1359043f48fSEdward Cree efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
1369043f48fSEdward Cree {
1379043f48fSEdward Cree 	return ((efx_qword_t *) (rx_queue->rxd.buf.addr)) + index;
1389043f48fSEdward Cree }
1399043f48fSEdward Cree 
1409043f48fSEdward Cree /* Alignment of PCIe DMA boundaries (4KB) */
1419043f48fSEdward Cree #define EFX_PAGE_SIZE	4096
1429043f48fSEdward Cree /* Size and alignment of buffer table entries (same) */
1439043f48fSEdward Cree #define EFX_BUF_SIZE	EFX_PAGE_SIZE
1449043f48fSEdward Cree 
1459043f48fSEdward Cree /* NIC-generic software stats */
1469043f48fSEdward Cree enum {
1479043f48fSEdward Cree 	GENERIC_STAT_rx_noskb_drops,
1489043f48fSEdward Cree 	GENERIC_STAT_rx_nodesc_trunc,
1499043f48fSEdward Cree 	GENERIC_STAT_COUNT
1509043f48fSEdward Cree };
1519043f48fSEdward Cree 
1529043f48fSEdward Cree #define EFX_GENERIC_SW_STAT(ext_name)				\
1539043f48fSEdward Cree 	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
1549043f48fSEdward Cree 
1559043f48fSEdward Cree /* TX data path */
1569043f48fSEdward Cree static inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
1579043f48fSEdward Cree {
1589043f48fSEdward Cree 	return tx_queue->efx->type->tx_probe(tx_queue);
1599043f48fSEdward Cree }
1609043f48fSEdward Cree static inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
1619043f48fSEdward Cree {
1629043f48fSEdward Cree 	tx_queue->efx->type->tx_init(tx_queue);
1639043f48fSEdward Cree }
1649043f48fSEdward Cree static inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
1659043f48fSEdward Cree {
1669043f48fSEdward Cree 	tx_queue->efx->type->tx_remove(tx_queue);
1679043f48fSEdward Cree }
1689043f48fSEdward Cree static inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
1699043f48fSEdward Cree {
1709043f48fSEdward Cree 	tx_queue->efx->type->tx_write(tx_queue);
1719043f48fSEdward Cree }
1729043f48fSEdward Cree 
1739043f48fSEdward Cree /* RX data path */
1749043f48fSEdward Cree static inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
1759043f48fSEdward Cree {
1769043f48fSEdward Cree 	return rx_queue->efx->type->rx_probe(rx_queue);
1779043f48fSEdward Cree }
1789043f48fSEdward Cree static inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
1799043f48fSEdward Cree {
1809043f48fSEdward Cree 	rx_queue->efx->type->rx_init(rx_queue);
1819043f48fSEdward Cree }
1829043f48fSEdward Cree static inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
1839043f48fSEdward Cree {
1849043f48fSEdward Cree 	rx_queue->efx->type->rx_remove(rx_queue);
1859043f48fSEdward Cree }
1869043f48fSEdward Cree static inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
1879043f48fSEdward Cree {
1889043f48fSEdward Cree 	rx_queue->efx->type->rx_write(rx_queue);
1899043f48fSEdward Cree }
1909043f48fSEdward Cree static inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
1919043f48fSEdward Cree {
1929043f48fSEdward Cree 	rx_queue->efx->type->rx_defer_refill(rx_queue);
1939043f48fSEdward Cree }
1949043f48fSEdward Cree 
1959043f48fSEdward Cree /* Event data path */
1969043f48fSEdward Cree static inline int efx_nic_probe_eventq(struct efx_channel *channel)
1979043f48fSEdward Cree {
1989043f48fSEdward Cree 	return channel->efx->type->ev_probe(channel);
1999043f48fSEdward Cree }
2009043f48fSEdward Cree static inline int efx_nic_init_eventq(struct efx_channel *channel)
2019043f48fSEdward Cree {
2029043f48fSEdward Cree 	return channel->efx->type->ev_init(channel);
2039043f48fSEdward Cree }
2049043f48fSEdward Cree static inline void efx_nic_fini_eventq(struct efx_channel *channel)
2059043f48fSEdward Cree {
2069043f48fSEdward Cree 	channel->efx->type->ev_fini(channel);
2079043f48fSEdward Cree }
2089043f48fSEdward Cree static inline void efx_nic_remove_eventq(struct efx_channel *channel)
2099043f48fSEdward Cree {
2109043f48fSEdward Cree 	channel->efx->type->ev_remove(channel);
2119043f48fSEdward Cree }
2129043f48fSEdward Cree static inline int
2139043f48fSEdward Cree efx_nic_process_eventq(struct efx_channel *channel, int quota)
2149043f48fSEdward Cree {
2159043f48fSEdward Cree 	return channel->efx->type->ev_process(channel, quota);
2169043f48fSEdward Cree }
2179043f48fSEdward Cree static inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
2189043f48fSEdward Cree {
2199043f48fSEdward Cree 	channel->efx->type->ev_read_ack(channel);
2209043f48fSEdward Cree }
2219043f48fSEdward Cree 
2229043f48fSEdward Cree void efx_nic_event_test_start(struct efx_channel *channel);
2239043f48fSEdward Cree 
2249043f48fSEdward Cree bool efx_nic_event_present(struct efx_channel *channel);
2259043f48fSEdward Cree 
2269043f48fSEdward Cree /* Some statistics are computed as A - B where A and B each increase
2279043f48fSEdward Cree  * linearly with some hardware counter(s) and the counters are read
2289043f48fSEdward Cree  * asynchronously.  If the counters contributing to B are always read
2299043f48fSEdward Cree  * after those contributing to A, the computed value may be lower than
2309043f48fSEdward Cree  * the true value by some variable amount, and may decrease between
2319043f48fSEdward Cree  * subsequent computations.
2329043f48fSEdward Cree  *
2339043f48fSEdward Cree  * We should never allow statistics to decrease or to exceed the true
2349043f48fSEdward Cree  * value.  Since the computed value will never be greater than the
2359043f48fSEdward Cree  * true value, we can achieve this by only storing the computed value
2369043f48fSEdward Cree  * when it increases.
2379043f48fSEdward Cree  */
2389043f48fSEdward Cree static inline void efx_update_diff_stat(u64 *stat, u64 diff)
2399043f48fSEdward Cree {
2409043f48fSEdward Cree 	if ((s64)(diff - *stat) > 0)
2419043f48fSEdward Cree 		*stat = diff;
2429043f48fSEdward Cree }
2439043f48fSEdward Cree 
2449043f48fSEdward Cree /* Interrupts */
2459043f48fSEdward Cree int efx_nic_init_interrupt(struct efx_nic *efx);
2469043f48fSEdward Cree int efx_nic_irq_test_start(struct efx_nic *efx);
2479043f48fSEdward Cree void efx_nic_fini_interrupt(struct efx_nic *efx);
2489043f48fSEdward Cree 
2499043f48fSEdward Cree static inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
2509043f48fSEdward Cree {
2519043f48fSEdward Cree 	return READ_ONCE(channel->event_test_cpu);
2529043f48fSEdward Cree }
2539043f48fSEdward Cree static inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
2549043f48fSEdward Cree {
2559043f48fSEdward Cree 	return READ_ONCE(efx->last_irq_cpu);
2569043f48fSEdward Cree }
2579043f48fSEdward Cree 
2589043f48fSEdward Cree /* Global Resources */
2599043f48fSEdward Cree int efx_nic_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
2609043f48fSEdward Cree 			 unsigned int len, gfp_t gfp_flags);
2619043f48fSEdward Cree void efx_nic_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
2629043f48fSEdward Cree 
2639043f48fSEdward Cree size_t efx_nic_get_regs_len(struct efx_nic *efx);
2649043f48fSEdward Cree void efx_nic_get_regs(struct efx_nic *efx, void *buf);
2659043f48fSEdward Cree 
26620e1026cSEdward Cree #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
26720e1026cSEdward Cree 
2689043f48fSEdward Cree size_t efx_nic_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
2699043f48fSEdward Cree 			      const unsigned long *mask, u8 *names);
2709043f48fSEdward Cree int efx_nic_copy_stats(struct efx_nic *efx, __le64 *dest);
2719043f48fSEdward Cree void efx_nic_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
2729043f48fSEdward Cree 			  const unsigned long *mask, u64 *stats,
2739043f48fSEdward Cree 			  const void *dma_buf, bool accumulate);
2749043f48fSEdward Cree void efx_nic_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
2759043f48fSEdward Cree 
2769043f48fSEdward Cree #define EFX_MAX_FLUSH_TIME 5000
2779043f48fSEdward Cree 
2789043f48fSEdward Cree #endif /* EFX_NIC_COMMON_H */
279