xref: /linux/drivers/net/ethernet/sfc/siena/nic_common.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d48523cbSMartin Habets /* SPDX-License-Identifier: GPL-2.0-only */
2d48523cbSMartin Habets /****************************************************************************
3d48523cbSMartin Habets  * Driver for Solarflare network controllers and boards
4d48523cbSMartin Habets  * Copyright 2005-2006 Fen Systems Ltd.
5d48523cbSMartin Habets  * Copyright 2006-2013 Solarflare Communications Inc.
6d48523cbSMartin Habets  * Copyright 2019-2020 Xilinx Inc.
7d48523cbSMartin Habets  */
8d48523cbSMartin Habets 
9d48523cbSMartin Habets #ifndef EFX_NIC_COMMON_H
10d48523cbSMartin Habets #define EFX_NIC_COMMON_H
11d48523cbSMartin Habets 
12d48523cbSMartin Habets #include "net_driver.h"
13d48523cbSMartin Habets #include "efx_common.h"
14d48523cbSMartin Habets #include "mcdi.h"
15d48523cbSMartin Habets #include "ptp.h"
16d48523cbSMartin Habets 
17d48523cbSMartin Habets enum {
18d48523cbSMartin Habets 	/* Revisions 0-2 were Falcon A0, A1 and B0 respectively.
19d48523cbSMartin Habets 	 * They are not supported by this driver but these revision numbers
20d48523cbSMartin Habets 	 * form part of the ethtool API for register dumping.
21d48523cbSMartin Habets 	 */
22d48523cbSMartin Habets 	EFX_REV_SIENA_A0 = 3,
23d48523cbSMartin Habets 	EFX_REV_HUNT_A0 = 4,
24d48523cbSMartin Habets 	EFX_REV_EF100 = 5,
25d48523cbSMartin Habets };
26d48523cbSMartin Habets 
efx_nic_rev(struct efx_nic * efx)27d48523cbSMartin Habets static inline int efx_nic_rev(struct efx_nic *efx)
28d48523cbSMartin Habets {
29d48523cbSMartin Habets 	return efx->type->revision;
30d48523cbSMartin Habets }
31d48523cbSMartin Habets 
32d48523cbSMartin Habets /* Read the current event from the event queue */
efx_event(struct efx_channel * channel,unsigned int index)33d48523cbSMartin Habets static inline efx_qword_t *efx_event(struct efx_channel *channel,
34d48523cbSMartin Habets 				     unsigned int index)
35d48523cbSMartin Habets {
36d48523cbSMartin Habets 	return ((efx_qword_t *) (channel->eventq.buf.addr)) +
37d48523cbSMartin Habets 		(index & channel->eventq_mask);
38d48523cbSMartin Habets }
39d48523cbSMartin Habets 
40d48523cbSMartin Habets /* See if an event is present
41d48523cbSMartin Habets  *
42d48523cbSMartin Habets  * We check both the high and low dword of the event for all ones.  We
43d48523cbSMartin Habets  * wrote all ones when we cleared the event, and no valid event can
44d48523cbSMartin Habets  * have all ones in either its high or low dwords.  This approach is
45d48523cbSMartin Habets  * robust against reordering.
46d48523cbSMartin Habets  *
47d48523cbSMartin Habets  * Note that using a single 64-bit comparison is incorrect; even
48d48523cbSMartin Habets  * though the CPU read will be atomic, the DMA write may not be.
49d48523cbSMartin Habets  */
efx_event_present(efx_qword_t * event)50d48523cbSMartin Habets static inline int efx_event_present(efx_qword_t *event)
51d48523cbSMartin Habets {
52d48523cbSMartin Habets 	return !(EFX_DWORD_IS_ALL_ONES(event->dword[0]) |
53d48523cbSMartin Habets 		  EFX_DWORD_IS_ALL_ONES(event->dword[1]));
54d48523cbSMartin Habets }
55d48523cbSMartin Habets 
56d48523cbSMartin Habets /* Returns a pointer to the specified transmit descriptor in the TX
57d48523cbSMartin Habets  * descriptor queue belonging to the specified channel.
58d48523cbSMartin Habets  */
59d48523cbSMartin Habets static inline efx_qword_t *
efx_tx_desc(struct efx_tx_queue * tx_queue,unsigned int index)60d48523cbSMartin Habets efx_tx_desc(struct efx_tx_queue *tx_queue, unsigned int index)
61d48523cbSMartin Habets {
62d48523cbSMartin Habets 	return ((efx_qword_t *) (tx_queue->txd.buf.addr)) + index;
63d48523cbSMartin Habets }
64d48523cbSMartin Habets 
65d48523cbSMartin Habets /* Report whether this TX queue would be empty for the given write_count.
66d48523cbSMartin Habets  * May return false negative.
67d48523cbSMartin Habets  */
efx_nic_tx_is_empty(struct efx_tx_queue * tx_queue,unsigned int write_count)68d48523cbSMartin Habets static inline bool efx_nic_tx_is_empty(struct efx_tx_queue *tx_queue, unsigned int write_count)
69d48523cbSMartin Habets {
70d48523cbSMartin Habets 	unsigned int empty_read_count = READ_ONCE(tx_queue->empty_read_count);
71d48523cbSMartin Habets 
72d48523cbSMartin Habets 	if (empty_read_count == 0)
73d48523cbSMartin Habets 		return false;
74d48523cbSMartin Habets 
75d48523cbSMartin Habets 	return ((empty_read_count ^ write_count) & ~EFX_EMPTY_COUNT_VALID) == 0;
76d48523cbSMartin Habets }
77d48523cbSMartin Habets 
78d48523cbSMartin Habets /* Decide whether to push a TX descriptor to the NIC vs merely writing
79d48523cbSMartin Habets  * the doorbell.  This can reduce latency when we are adding a single
80d48523cbSMartin Habets  * descriptor to an empty queue, but is otherwise pointless.  Further,
81d48523cbSMartin Habets  * Falcon and Siena have hardware bugs (SF bug 33851) that may be
82d48523cbSMartin Habets  * triggered if we don't check this.
83d48523cbSMartin Habets  * We use the write_count used for the last doorbell push, to get the
84d48523cbSMartin Habets  * NIC's view of the tx queue.
85d48523cbSMartin Habets  */
efx_nic_may_push_tx_desc(struct efx_tx_queue * tx_queue,unsigned int write_count)86d48523cbSMartin Habets static inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue *tx_queue,
87d48523cbSMartin Habets 					    unsigned int write_count)
88d48523cbSMartin Habets {
89d48523cbSMartin Habets 	bool was_empty = efx_nic_tx_is_empty(tx_queue, write_count);
90d48523cbSMartin Habets 
91d48523cbSMartin Habets 	tx_queue->empty_read_count = 0;
92d48523cbSMartin Habets 	return was_empty && tx_queue->write_count - write_count == 1;
93d48523cbSMartin Habets }
94d48523cbSMartin Habets 
95d48523cbSMartin Habets /* Returns a pointer to the specified descriptor in the RX descriptor queue */
96d48523cbSMartin Habets static inline efx_qword_t *
efx_rx_desc(struct efx_rx_queue * rx_queue,unsigned int index)97d48523cbSMartin Habets efx_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
98d48523cbSMartin Habets {
99d48523cbSMartin Habets 	return ((efx_qword_t *) (rx_queue->rxd.buf.addr)) + index;
100d48523cbSMartin Habets }
101d48523cbSMartin Habets 
102d48523cbSMartin Habets /* Alignment of PCIe DMA boundaries (4KB) */
103d48523cbSMartin Habets #define EFX_PAGE_SIZE	4096
104d48523cbSMartin Habets /* Size and alignment of buffer table entries (same) */
105d48523cbSMartin Habets #define EFX_BUF_SIZE	EFX_PAGE_SIZE
106d48523cbSMartin Habets 
107d48523cbSMartin Habets /* NIC-generic software stats */
108d48523cbSMartin Habets enum {
109d48523cbSMartin Habets 	GENERIC_STAT_rx_noskb_drops,
110d48523cbSMartin Habets 	GENERIC_STAT_rx_nodesc_trunc,
111d48523cbSMartin Habets 	GENERIC_STAT_COUNT
112d48523cbSMartin Habets };
113d48523cbSMartin Habets 
114d48523cbSMartin Habets #define EFX_GENERIC_SW_STAT(ext_name)				\
115d48523cbSMartin Habets 	[GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
116d48523cbSMartin Habets 
117d48523cbSMartin Habets /* TX data path */
efx_nic_probe_tx(struct efx_tx_queue * tx_queue)118d48523cbSMartin Habets static inline int efx_nic_probe_tx(struct efx_tx_queue *tx_queue)
119d48523cbSMartin Habets {
120d48523cbSMartin Habets 	return tx_queue->efx->type->tx_probe(tx_queue);
121d48523cbSMartin Habets }
efx_nic_init_tx(struct efx_tx_queue * tx_queue)122d48523cbSMartin Habets static inline void efx_nic_init_tx(struct efx_tx_queue *tx_queue)
123d48523cbSMartin Habets {
124d48523cbSMartin Habets 	tx_queue->efx->type->tx_init(tx_queue);
125d48523cbSMartin Habets }
efx_nic_remove_tx(struct efx_tx_queue * tx_queue)126d48523cbSMartin Habets static inline void efx_nic_remove_tx(struct efx_tx_queue *tx_queue)
127d48523cbSMartin Habets {
128d48523cbSMartin Habets 	if (tx_queue->efx->type->tx_remove)
129d48523cbSMartin Habets 		tx_queue->efx->type->tx_remove(tx_queue);
130d48523cbSMartin Habets }
efx_nic_push_buffers(struct efx_tx_queue * tx_queue)131d48523cbSMartin Habets static inline void efx_nic_push_buffers(struct efx_tx_queue *tx_queue)
132d48523cbSMartin Habets {
133d48523cbSMartin Habets 	tx_queue->efx->type->tx_write(tx_queue);
134d48523cbSMartin Habets }
135d48523cbSMartin Habets 
136d48523cbSMartin Habets /* RX data path */
efx_nic_probe_rx(struct efx_rx_queue * rx_queue)137d48523cbSMartin Habets static inline int efx_nic_probe_rx(struct efx_rx_queue *rx_queue)
138d48523cbSMartin Habets {
139d48523cbSMartin Habets 	return rx_queue->efx->type->rx_probe(rx_queue);
140d48523cbSMartin Habets }
efx_nic_init_rx(struct efx_rx_queue * rx_queue)141d48523cbSMartin Habets static inline void efx_nic_init_rx(struct efx_rx_queue *rx_queue)
142d48523cbSMartin Habets {
143d48523cbSMartin Habets 	rx_queue->efx->type->rx_init(rx_queue);
144d48523cbSMartin Habets }
efx_nic_remove_rx(struct efx_rx_queue * rx_queue)145d48523cbSMartin Habets static inline void efx_nic_remove_rx(struct efx_rx_queue *rx_queue)
146d48523cbSMartin Habets {
147d48523cbSMartin Habets 	rx_queue->efx->type->rx_remove(rx_queue);
148d48523cbSMartin Habets }
efx_nic_notify_rx_desc(struct efx_rx_queue * rx_queue)149d48523cbSMartin Habets static inline void efx_nic_notify_rx_desc(struct efx_rx_queue *rx_queue)
150d48523cbSMartin Habets {
151d48523cbSMartin Habets 	rx_queue->efx->type->rx_write(rx_queue);
152d48523cbSMartin Habets }
efx_nic_generate_fill_event(struct efx_rx_queue * rx_queue)153d48523cbSMartin Habets static inline void efx_nic_generate_fill_event(struct efx_rx_queue *rx_queue)
154d48523cbSMartin Habets {
155d48523cbSMartin Habets 	rx_queue->efx->type->rx_defer_refill(rx_queue);
156d48523cbSMartin Habets }
157d48523cbSMartin Habets 
158d48523cbSMartin Habets /* Event data path */
efx_nic_probe_eventq(struct efx_channel * channel)159d48523cbSMartin Habets static inline int efx_nic_probe_eventq(struct efx_channel *channel)
160d48523cbSMartin Habets {
161d48523cbSMartin Habets 	return channel->efx->type->ev_probe(channel);
162d48523cbSMartin Habets }
efx_nic_init_eventq(struct efx_channel * channel)163d48523cbSMartin Habets static inline int efx_nic_init_eventq(struct efx_channel *channel)
164d48523cbSMartin Habets {
165d48523cbSMartin Habets 	return channel->efx->type->ev_init(channel);
166d48523cbSMartin Habets }
efx_nic_fini_eventq(struct efx_channel * channel)167d48523cbSMartin Habets static inline void efx_nic_fini_eventq(struct efx_channel *channel)
168d48523cbSMartin Habets {
169d48523cbSMartin Habets 	channel->efx->type->ev_fini(channel);
170d48523cbSMartin Habets }
efx_nic_remove_eventq(struct efx_channel * channel)171d48523cbSMartin Habets static inline void efx_nic_remove_eventq(struct efx_channel *channel)
172d48523cbSMartin Habets {
173d48523cbSMartin Habets 	channel->efx->type->ev_remove(channel);
174d48523cbSMartin Habets }
175d48523cbSMartin Habets static inline int
efx_nic_process_eventq(struct efx_channel * channel,int quota)176d48523cbSMartin Habets efx_nic_process_eventq(struct efx_channel *channel, int quota)
177d48523cbSMartin Habets {
178d48523cbSMartin Habets 	return channel->efx->type->ev_process(channel, quota);
179d48523cbSMartin Habets }
efx_nic_eventq_read_ack(struct efx_channel * channel)180d48523cbSMartin Habets static inline void efx_nic_eventq_read_ack(struct efx_channel *channel)
181d48523cbSMartin Habets {
182d48523cbSMartin Habets 	channel->efx->type->ev_read_ack(channel);
183d48523cbSMartin Habets }
184d48523cbSMartin Habets 
185*c8443b69SMartin Habets void efx_siena_event_test_start(struct efx_channel *channel);
186d48523cbSMartin Habets 
187*c8443b69SMartin Habets bool efx_siena_event_present(struct efx_channel *channel);
188d48523cbSMartin Habets 
efx_sensor_event(struct efx_nic * efx,efx_qword_t * ev)189d48523cbSMartin Habets static inline void efx_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
190d48523cbSMartin Habets {
191d48523cbSMartin Habets 	if (efx->type->sensor_event)
192d48523cbSMartin Habets 		efx->type->sensor_event(efx, ev);
193d48523cbSMartin Habets }
194d48523cbSMartin Habets 
efx_rx_recycle_ring_size(const struct efx_nic * efx)195d48523cbSMartin Habets static inline unsigned int efx_rx_recycle_ring_size(const struct efx_nic *efx)
196d48523cbSMartin Habets {
197d48523cbSMartin Habets 	return efx->type->rx_recycle_ring_size(efx);
198d48523cbSMartin Habets }
199d48523cbSMartin Habets 
200d48523cbSMartin Habets /* Some statistics are computed as A - B where A and B each increase
201d48523cbSMartin Habets  * linearly with some hardware counter(s) and the counters are read
202d48523cbSMartin Habets  * asynchronously.  If the counters contributing to B are always read
203d48523cbSMartin Habets  * after those contributing to A, the computed value may be lower than
204d48523cbSMartin Habets  * the true value by some variable amount, and may decrease between
205d48523cbSMartin Habets  * subsequent computations.
206d48523cbSMartin Habets  *
207d48523cbSMartin Habets  * We should never allow statistics to decrease or to exceed the true
208d48523cbSMartin Habets  * value.  Since the computed value will never be greater than the
209d48523cbSMartin Habets  * true value, we can achieve this by only storing the computed value
210d48523cbSMartin Habets  * when it increases.
211d48523cbSMartin Habets  */
efx_update_diff_stat(u64 * stat,u64 diff)212d48523cbSMartin Habets static inline void efx_update_diff_stat(u64 *stat, u64 diff)
213d48523cbSMartin Habets {
214d48523cbSMartin Habets 	if ((s64)(diff - *stat) > 0)
215d48523cbSMartin Habets 		*stat = diff;
216d48523cbSMartin Habets }
217d48523cbSMartin Habets 
218d48523cbSMartin Habets /* Interrupts */
219*c8443b69SMartin Habets int efx_siena_init_interrupt(struct efx_nic *efx);
220*c8443b69SMartin Habets int efx_siena_irq_test_start(struct efx_nic *efx);
221*c8443b69SMartin Habets void efx_siena_fini_interrupt(struct efx_nic *efx);
222d48523cbSMartin Habets 
efx_nic_event_test_irq_cpu(struct efx_channel * channel)223d48523cbSMartin Habets static inline int efx_nic_event_test_irq_cpu(struct efx_channel *channel)
224d48523cbSMartin Habets {
225d48523cbSMartin Habets 	return READ_ONCE(channel->event_test_cpu);
226d48523cbSMartin Habets }
efx_nic_irq_test_irq_cpu(struct efx_nic * efx)227d48523cbSMartin Habets static inline int efx_nic_irq_test_irq_cpu(struct efx_nic *efx)
228d48523cbSMartin Habets {
229d48523cbSMartin Habets 	return READ_ONCE(efx->last_irq_cpu);
230d48523cbSMartin Habets }
231d48523cbSMartin Habets 
232d48523cbSMartin Habets /* Global Resources */
233*c8443b69SMartin Habets int efx_siena_alloc_buffer(struct efx_nic *efx, struct efx_buffer *buffer,
234d48523cbSMartin Habets 			   unsigned int len, gfp_t gfp_flags);
235*c8443b69SMartin Habets void efx_siena_free_buffer(struct efx_nic *efx, struct efx_buffer *buffer);
236d48523cbSMartin Habets 
237*c8443b69SMartin Habets size_t efx_siena_get_regs_len(struct efx_nic *efx);
238*c8443b69SMartin Habets void efx_siena_get_regs(struct efx_nic *efx, void *buf);
239d48523cbSMartin Habets 
240d48523cbSMartin Habets #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
241d48523cbSMartin Habets 
242*c8443b69SMartin Habets size_t efx_siena_describe_stats(const struct efx_hw_stat_desc *desc, size_t count,
243d48523cbSMartin Habets 				const unsigned long *mask, u8 *names);
244*c8443b69SMartin Habets void efx_siena_update_stats(const struct efx_hw_stat_desc *desc, size_t count,
245d48523cbSMartin Habets 			    const unsigned long *mask, u64 *stats,
246d48523cbSMartin Habets 			    const void *dma_buf, bool accumulate);
247*c8443b69SMartin Habets void efx_siena_fix_nodesc_drop_stat(struct efx_nic *efx, u64 *stat);
248d48523cbSMartin Habets 
249d48523cbSMartin Habets #define EFX_MAX_FLUSH_TIME 5000
250d48523cbSMartin Habets 
251d48523cbSMartin Habets #endif /* EFX_NIC_COMMON_H */
252