xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.h (revision dc6a81c3)
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  */
7 
8 #ifndef EFX_EFX_H
9 #define EFX_EFX_H
10 
11 #include "net_driver.h"
12 #include "filter.h"
13 
14 int efx_net_open(struct net_device *net_dev);
15 int efx_net_stop(struct net_device *net_dev);
16 
17 /* TX */
18 void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue);
19 netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
20 				struct net_device *net_dev);
21 netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb);
22 void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index);
23 int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
24 		 void *type_data);
25 extern unsigned int efx_piobuf_size;
26 extern bool efx_separate_tx_channels;
27 
28 /* RX */
29 void __efx_rx_packet(struct efx_channel *channel);
30 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
31 		   unsigned int n_frags, unsigned int len, u16 flags);
32 static inline void efx_rx_flush_packet(struct efx_channel *channel)
33 {
34 	if (channel->rx_pkt_n_frags)
35 		__efx_rx_packet(channel);
36 }
37 
38 #define EFX_MAX_DMAQ_SIZE 4096UL
39 #define EFX_DEFAULT_DMAQ_SIZE 1024UL
40 #define EFX_MIN_DMAQ_SIZE 512UL
41 
42 #define EFX_MAX_EVQ_SIZE 16384UL
43 #define EFX_MIN_EVQ_SIZE 512UL
44 
45 /* Maximum number of TCP segments we support for soft-TSO */
46 #define EFX_TSO_MAX_SEGS	100
47 
48 /* The smallest [rt]xq_entries that the driver supports.  RX minimum
49  * is a bit arbitrary.  For TX, we must have space for at least 2
50  * TSO skbs.
51  */
52 #define EFX_RXQ_MIN_ENT		128U
53 #define EFX_TXQ_MIN_ENT(efx)	(2 * efx_tx_max_skb_descs(efx))
54 
55 /* All EF10 architecture NICs steal one bit of the DMAQ size for various
56  * other purposes when counting TxQ entries, so we halve the queue size.
57  */
58 #define EFX_TXQ_MAX_ENT(efx)	(EFX_WORKAROUND_EF10(efx) ? \
59 				 EFX_MAX_DMAQ_SIZE / 2 : EFX_MAX_DMAQ_SIZE)
60 
61 static inline bool efx_rss_enabled(struct efx_nic *efx)
62 {
63 	return efx->rss_spread > 1;
64 }
65 
66 /* Filters */
67 
68 /**
69  * efx_filter_insert_filter - add or replace a filter
70  * @efx: NIC in which to insert the filter
71  * @spec: Specification for the filter
72  * @replace_equal: Flag for whether the specified filter may replace an
73  *	existing filter with equal priority
74  *
75  * On success, return the filter ID.
76  * On failure, return a negative error code.
77  *
78  * If existing filters have equal match values to the new filter spec,
79  * then the new filter might replace them or the function might fail,
80  * as follows.
81  *
82  * 1. If the existing filters have lower priority, or @replace_equal
83  *    is set and they have equal priority, replace them.
84  *
85  * 2. If the existing filters have higher priority, return -%EPERM.
86  *
87  * 3. If !efx_filter_is_mc_recipient(@spec), or the NIC does not
88  *    support delivery to multiple recipients, return -%EEXIST.
89  *
90  * This implies that filters for multiple multicast recipients must
91  * all be inserted with the same priority and @replace_equal = %false.
92  */
93 static inline s32 efx_filter_insert_filter(struct efx_nic *efx,
94 					   struct efx_filter_spec *spec,
95 					   bool replace_equal)
96 {
97 	return efx->type->filter_insert(efx, spec, replace_equal);
98 }
99 
100 /**
101  * efx_filter_remove_id_safe - remove a filter by ID, carefully
102  * @efx: NIC from which to remove the filter
103  * @priority: Priority of filter, as passed to @efx_filter_insert_filter
104  * @filter_id: ID of filter, as returned by @efx_filter_insert_filter
105  *
106  * This function will range-check @filter_id, so it is safe to call
107  * with a value passed from userland.
108  */
109 static inline int efx_filter_remove_id_safe(struct efx_nic *efx,
110 					    enum efx_filter_priority priority,
111 					    u32 filter_id)
112 {
113 	return efx->type->filter_remove_safe(efx, priority, filter_id);
114 }
115 
116 /**
117  * efx_filter_get_filter_safe - retrieve a filter by ID, carefully
118  * @efx: NIC from which to remove the filter
119  * @priority: Priority of filter, as passed to @efx_filter_insert_filter
120  * @filter_id: ID of filter, as returned by @efx_filter_insert_filter
121  * @spec: Buffer in which to store filter specification
122  *
123  * This function will range-check @filter_id, so it is safe to call
124  * with a value passed from userland.
125  */
126 static inline int
127 efx_filter_get_filter_safe(struct efx_nic *efx,
128 			   enum efx_filter_priority priority,
129 			   u32 filter_id, struct efx_filter_spec *spec)
130 {
131 	return efx->type->filter_get_safe(efx, priority, filter_id, spec);
132 }
133 
134 static inline u32 efx_filter_count_rx_used(struct efx_nic *efx,
135 					   enum efx_filter_priority priority)
136 {
137 	return efx->type->filter_count_rx_used(efx, priority);
138 }
139 static inline u32 efx_filter_get_rx_id_limit(struct efx_nic *efx)
140 {
141 	return efx->type->filter_get_rx_id_limit(efx);
142 }
143 static inline s32 efx_filter_get_rx_ids(struct efx_nic *efx,
144 					enum efx_filter_priority priority,
145 					u32 *buf, u32 size)
146 {
147 	return efx->type->filter_get_rx_ids(efx, priority, buf, size);
148 }
149 #ifdef CONFIG_RFS_ACCEL
150 int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
151 		   u16 rxq_index, u32 flow_id);
152 bool __efx_filter_rfs_expire(struct efx_channel *channel, unsigned int quota);
153 static inline void efx_filter_rfs_expire(struct work_struct *data)
154 {
155 	struct delayed_work *dwork = to_delayed_work(data);
156 	struct efx_channel *channel;
157 	unsigned int time, quota;
158 
159 	channel = container_of(dwork, struct efx_channel, filter_work);
160 	time = jiffies - channel->rfs_last_expiry;
161 	quota = channel->rfs_filter_count * time / (30 * HZ);
162 	if (quota > 20 && __efx_filter_rfs_expire(channel, min(channel->rfs_filter_count, quota)))
163 		channel->rfs_last_expiry += time;
164 	/* Ensure we do more work eventually even if NAPI poll is not happening */
165 	schedule_delayed_work(dwork, 30 * HZ);
166 }
167 #define efx_filter_rfs_enabled() 1
168 #else
169 static inline void efx_filter_rfs_expire(struct work_struct *data) {}
170 #define efx_filter_rfs_enabled() 0
171 #endif
172 
173 /* RSS contexts */
174 static inline bool efx_rss_active(struct efx_rss_context *ctx)
175 {
176 	return ctx->context_id != EFX_MCDI_RSS_CONTEXT_INVALID;
177 }
178 
179 /* Ethtool support */
180 extern const struct ethtool_ops efx_ethtool_ops;
181 
182 /* Global */
183 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs);
184 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks);
185 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
186 			    unsigned int rx_usecs, bool rx_adaptive,
187 			    bool rx_may_override_tx);
188 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
189 			    unsigned int *rx_usecs, bool *rx_adaptive);
190 
191 /* Dummy PHY ops for PHY drivers */
192 int efx_port_dummy_op_int(struct efx_nic *efx);
193 void efx_port_dummy_op_void(struct efx_nic *efx);
194 
195 /* Update the generic software stats in the passed stats array */
196 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats);
197 
198 /* MTD */
199 #ifdef CONFIG_SFC_MTD
200 int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
201 		size_t n_parts, size_t sizeof_part);
202 static inline int efx_mtd_probe(struct efx_nic *efx)
203 {
204 	return efx->type->mtd_probe(efx);
205 }
206 void efx_mtd_rename(struct efx_nic *efx);
207 void efx_mtd_remove(struct efx_nic *efx);
208 #else
209 static inline int efx_mtd_probe(struct efx_nic *efx) { return 0; }
210 static inline void efx_mtd_rename(struct efx_nic *efx) {}
211 static inline void efx_mtd_remove(struct efx_nic *efx) {}
212 #endif
213 
214 #ifdef CONFIG_SFC_SRIOV
215 static inline unsigned int efx_vf_size(struct efx_nic *efx)
216 {
217 	return 1 << efx->vi_scale;
218 }
219 #endif
220 
221 static inline void efx_schedule_channel(struct efx_channel *channel)
222 {
223 	netif_vdbg(channel->efx, intr, channel->efx->net_dev,
224 		   "channel %d scheduling NAPI poll on CPU%d\n",
225 		   channel->channel, raw_smp_processor_id());
226 
227 	napi_schedule(&channel->napi_str);
228 }
229 
230 static inline void efx_schedule_channel_irq(struct efx_channel *channel)
231 {
232 	channel->event_test_cpu = raw_smp_processor_id();
233 	efx_schedule_channel(channel);
234 }
235 
236 void efx_link_clear_advertising(struct efx_nic *efx);
237 void efx_link_set_wanted_fc(struct efx_nic *efx, u8);
238 
239 static inline void efx_device_detach_sync(struct efx_nic *efx)
240 {
241 	struct net_device *dev = efx->net_dev;
242 
243 	/* Lock/freeze all TX queues so that we can be sure the
244 	 * TX scheduler is stopped when we're done and before
245 	 * netif_device_present() becomes false.
246 	 */
247 	netif_tx_lock_bh(dev);
248 	netif_device_detach(dev);
249 	netif_tx_unlock_bh(dev);
250 }
251 
252 static inline void efx_device_attach_if_not_resetting(struct efx_nic *efx)
253 {
254 	if ((efx->state != STATE_DISABLED) && !efx->reset_pending)
255 		netif_device_attach(efx->net_dev);
256 }
257 
258 static inline bool efx_rwsem_assert_write_locked(struct rw_semaphore *sem)
259 {
260 	if (WARN_ON(down_read_trylock(sem))) {
261 		up_read(sem);
262 		return false;
263 	}
264 	return true;
265 }
266 
267 int efx_xdp_tx_buffers(struct efx_nic *efx, int n, struct xdp_frame **xdpfs,
268 		       bool flush);
269 
270 #endif /* EFX_EFX_H */
271