1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2006-2013 Solarflare Communications Inc.
6 * Copyright 2019-2020 Xilinx Inc.
9 #ifndef EFX_NIC_COMMON_H
10 #define EFX_NIC_COMMON_H
12 #include "net_driver.h"
13 #include "efx_common.h"
18 /* Revisions 0-3 were Falcon A0, A1, B0 and Siena respectively.
19 * They are not supported by this driver but these revision numbers
20 * form part of the ethtool API for register dumping.
27 static inline int efx_nic_rev(struct efx_nic
*efx
)
29 return efx
->type
->revision
;
32 /* Read the current event from the event queue */
33 static inline efx_qword_t
*efx_event(struct efx_channel
*channel
,
36 return ((efx_qword_t
*)(channel
->eventq
.addr
)) +
37 (index
& channel
->eventq_mask
);
40 /* See if an event is present
42 * We check both the high and low dword of the event for all ones. We
43 * wrote all ones when we cleared the event, and no valid event can
44 * have all ones in either its high or low dwords. This approach is
45 * robust against reordering.
47 * Note that using a single 64-bit comparison is incorrect; even
48 * though the CPU read will be atomic, the DMA write may not be.
50 static inline int efx_event_present(efx_qword_t
*event
)
52 return !(EFX_DWORD_IS_ALL_ONES(event
->dword
[0]) |
53 EFX_DWORD_IS_ALL_ONES(event
->dword
[1]));
56 /* Returns a pointer to the specified transmit descriptor in the TX
57 * descriptor queue belonging to the specified channel.
59 static inline efx_qword_t
*
60 efx_tx_desc(struct efx_tx_queue
*tx_queue
, unsigned int index
)
62 return ((efx_qword_t
*)(tx_queue
->txd
.addr
)) + index
;
65 /* Report whether this TX queue would be empty for the given write_count.
66 * May return false negative.
68 static inline bool efx_nic_tx_is_empty(struct efx_tx_queue
*tx_queue
, unsigned int write_count
)
70 unsigned int empty_read_count
= READ_ONCE(tx_queue
->empty_read_count
);
72 if (empty_read_count
== 0)
75 return ((empty_read_count
^ write_count
) & ~EFX_EMPTY_COUNT_VALID
) == 0;
78 int efx_enqueue_skb_tso(struct efx_tx_queue
*tx_queue
, struct sk_buff
*skb
,
81 /* Decide whether to push a TX descriptor to the NIC vs merely writing
82 * the doorbell. This can reduce latency when we are adding a single
83 * descriptor to an empty queue, but is otherwise pointless.
84 * We use the write_count used for the last doorbell push, to get the
85 * NIC's view of the tx queue.
87 static inline bool efx_nic_may_push_tx_desc(struct efx_tx_queue
*tx_queue
,
88 unsigned int write_count
)
90 bool was_empty
= efx_nic_tx_is_empty(tx_queue
, write_count
);
92 tx_queue
->empty_read_count
= 0;
93 return was_empty
&& tx_queue
->write_count
- write_count
== 1;
96 /* Returns a pointer to the specified descriptor in the RX descriptor queue */
97 static inline efx_qword_t
*
98 efx_rx_desc(struct efx_rx_queue
*rx_queue
, unsigned int index
)
100 return ((efx_qword_t
*)(rx_queue
->rxd
.addr
)) + index
;
103 /* Alignment of PCIe DMA boundaries (4KB) */
104 #define EFX_PAGE_SIZE 4096
105 /* Size and alignment of buffer table entries (same) */
106 #define EFX_BUF_SIZE EFX_PAGE_SIZE
108 /* NIC-generic software stats */
110 GENERIC_STAT_rx_noskb_drops
,
111 GENERIC_STAT_rx_nodesc_trunc
,
115 #define EFX_GENERIC_SW_STAT(ext_name) \
116 [GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
119 static inline int efx_nic_probe_tx(struct efx_tx_queue
*tx_queue
)
121 return tx_queue
->efx
->type
->tx_probe(tx_queue
);
123 static inline void efx_nic_init_tx(struct efx_tx_queue
*tx_queue
)
125 tx_queue
->efx
->type
->tx_init(tx_queue
);
127 static inline void efx_nic_remove_tx(struct efx_tx_queue
*tx_queue
)
129 if (tx_queue
->efx
->type
->tx_remove
)
130 tx_queue
->efx
->type
->tx_remove(tx_queue
);
132 static inline void efx_nic_push_buffers(struct efx_tx_queue
*tx_queue
)
134 tx_queue
->efx
->type
->tx_write(tx_queue
);
138 static inline int efx_nic_probe_rx(struct efx_rx_queue
*rx_queue
)
140 return rx_queue
->efx
->type
->rx_probe(rx_queue
);
142 static inline void efx_nic_init_rx(struct efx_rx_queue
*rx_queue
)
144 rx_queue
->efx
->type
->rx_init(rx_queue
);
146 static inline void efx_nic_remove_rx(struct efx_rx_queue
*rx_queue
)
148 rx_queue
->efx
->type
->rx_remove(rx_queue
);
150 static inline void efx_nic_notify_rx_desc(struct efx_rx_queue
*rx_queue
)
152 rx_queue
->efx
->type
->rx_write(rx_queue
);
154 static inline void efx_nic_generate_fill_event(struct efx_rx_queue
*rx_queue
)
156 rx_queue
->efx
->type
->rx_defer_refill(rx_queue
);
159 /* Event data path */
160 static inline int efx_nic_probe_eventq(struct efx_channel
*channel
)
162 return channel
->efx
->type
->ev_probe(channel
);
164 static inline int efx_nic_init_eventq(struct efx_channel
*channel
)
166 return channel
->efx
->type
->ev_init(channel
);
168 static inline void efx_nic_fini_eventq(struct efx_channel
*channel
)
170 channel
->efx
->type
->ev_fini(channel
);
172 static inline void efx_nic_remove_eventq(struct efx_channel
*channel
)
174 channel
->efx
->type
->ev_remove(channel
);
177 efx_nic_process_eventq(struct efx_channel
*channel
, int quota
)
179 return channel
->efx
->type
->ev_process(channel
, quota
);
181 static inline void efx_nic_eventq_read_ack(struct efx_channel
*channel
)
183 channel
->efx
->type
->ev_read_ack(channel
);
186 void efx_nic_event_test_start(struct efx_channel
*channel
);
188 bool efx_nic_event_present(struct efx_channel
*channel
);
190 static inline void efx_sensor_event(struct efx_nic
*efx
, efx_qword_t
*ev
)
192 if (efx
->type
->sensor_event
)
193 efx
->type
->sensor_event(efx
, ev
);
196 static inline unsigned int efx_rx_recycle_ring_size(const struct efx_nic
*efx
)
198 return efx
->type
->rx_recycle_ring_size(efx
);
201 /* Some statistics are computed as A - B where A and B each increase
202 * linearly with some hardware counter(s) and the counters are read
203 * asynchronously. If the counters contributing to B are always read
204 * after those contributing to A, the computed value may be lower than
205 * the true value by some variable amount, and may decrease between
206 * subsequent computations.
208 * We should never allow statistics to decrease or to exceed the true
209 * value. Since the computed value will never be greater than the
210 * true value, we can achieve this by only storing the computed value
213 static inline void efx_update_diff_stat(u64
*stat
, u64 diff
)
215 if ((s64
)(diff
- *stat
) > 0)
220 int efx_nic_init_interrupt(struct efx_nic
*efx
);
221 int efx_nic_irq_test_start(struct efx_nic
*efx
);
222 void efx_nic_fini_interrupt(struct efx_nic
*efx
);
224 static inline int efx_nic_event_test_irq_cpu(struct efx_channel
*channel
)
226 return READ_ONCE(channel
->event_test_cpu
);
228 static inline int efx_nic_irq_test_irq_cpu(struct efx_nic
*efx
)
230 return READ_ONCE(efx
->last_irq_cpu
);
233 /* Global Resources */
234 int efx_nic_alloc_buffer(struct efx_nic
*efx
, struct efx_buffer
*buffer
,
235 unsigned int len
, gfp_t gfp_flags
);
236 void efx_nic_free_buffer(struct efx_nic
*efx
, struct efx_buffer
*buffer
);
238 size_t efx_nic_get_regs_len(struct efx_nic
*efx
);
239 void efx_nic_get_regs(struct efx_nic
*efx
, void *buf
);
241 #define EFX_MC_STATS_GENERATION_INVALID ((__force __le64)(-1))
243 size_t efx_nic_describe_stats(const struct efx_hw_stat_desc
*desc
, size_t count
,
244 const unsigned long *mask
, u8
**names
);
245 int efx_nic_copy_stats(struct efx_nic
*efx
, __le64
*dest
);
246 void efx_nic_update_stats(const struct efx_hw_stat_desc
*desc
, size_t count
,
247 const unsigned long *mask
, u64
*stats
,
248 const void *dma_buf
, bool accumulate
);
249 void efx_nic_fix_nodesc_drop_stat(struct efx_nic
*efx
, u64
*stat
);
250 static inline size_t efx_nic_update_stats_atomic(struct efx_nic
*efx
, u64
*full_stats
,
251 struct rtnl_link_stats64
*core_stats
)
253 if (efx
->type
->update_stats_atomic
)
254 return efx
->type
->update_stats_atomic(efx
, full_stats
, core_stats
);
255 return efx
->type
->update_stats(efx
, full_stats
, core_stats
);
258 #define EFX_MAX_FLUSH_TIME 5000
260 #endif /* EFX_NIC_COMMON_H */