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