xref: /freebsd/sys/dev/sfxge/sfxge.h (revision f1951fd745b894fe6586c298874af98544a5e272)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2016 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was developed in part by Philip Paeps under contract for
8  * Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * The views and conclusions contained in the software and documentation are
32  * those of the authors and should not be interpreted as representing official
33  * policies, either expressed or implied, of the FreeBSD Project.
34  *
35  * $FreeBSD$
36  */
37 
38 #ifndef _SFXGE_H
39 #define	_SFXGE_H
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
45 #include <sys/sx.h>
46 #include <vm/uma.h>
47 
48 #include <net/ethernet.h>
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
53 
54 #include "sfxge_ioc.h"
55 
56 /*
57  * Debugging
58  */
59 #if 0
60 #define	DBGPRINT(dev, fmt, args...) \
61 	device_printf(dev, "%s: " fmt "\n", __func__, ## args)
62 #else
63 #define	DBGPRINT(dev, fmt, args...)
64 #endif
65 
66 /*
67  * Backward-compatibility
68  */
69 #ifndef CACHE_LINE_SIZE
70 /* This should be right on most machines the driver will be used on, and
71  * we needn't care too much about wasting a few KB per interface.
72  */
73 #define	CACHE_LINE_SIZE 128
74 #endif
75 
76 #ifndef IFCAP_LINKSTATE
77 #define	IFCAP_LINKSTATE 0
78 #endif
79 
80 #ifndef IFCAP_VLAN_HWTSO
81 #define	IFCAP_VLAN_HWTSO 0
82 #endif
83 
84 #ifndef IFM_10G_T
85 #define	IFM_10G_T IFM_UNKNOWN
86 #endif
87 
88 #ifndef IFM_10G_KX4
89 #define	IFM_10G_KX4 IFM_10G_CX4
90 #endif
91 
92 #ifndef IFM_40G_CR4
93 #define	IFM_40G_CR4 IFM_UNKNOWN
94 #endif
95 
96 #if (__FreeBSD_version >= 800501 && __FreeBSD_version < 900000) || \
97 	__FreeBSD_version >= 900003
98 #define	SFXGE_HAVE_DESCRIBE_INTR
99 #endif
100 
101 #ifdef IFM_ETH_RXPAUSE
102 #define	SFXGE_HAVE_PAUSE_MEDIAOPTS
103 #endif
104 
105 #ifndef CTLTYPE_U64
106 #define	CTLTYPE_U64 CTLTYPE_QUAD
107 #endif
108 
109 #include "sfxge_rx.h"
110 #include "sfxge_tx.h"
111 
112 #define	ROUNDUP_POW_OF_TWO(_n)	(1ULL << flsl((_n) - 1))
113 
114 #define	SFXGE_IP_ALIGN	2
115 
116 #define	SFXGE_ETHERTYPE_LOOPBACK	0x9000	/* Xerox loopback */
117 
118 
119 #define	SFXGE_MAGIC_RESERVED		0x8000
120 
121 #define	SFXGE_MAGIC_DMAQ_LABEL_WIDTH	6
122 #define	SFXGE_MAGIC_DMAQ_LABEL_MASK \
123 	((1 << SFXGE_MAGIC_DMAQ_LABEL_WIDTH) - 1)
124 
125 enum sfxge_sw_ev {
126 	SFXGE_SW_EV_RX_QFLUSH_DONE = 1,
127 	SFXGE_SW_EV_RX_QFLUSH_FAILED,
128 	SFXGE_SW_EV_RX_QREFILL,
129 	SFXGE_SW_EV_TX_QFLUSH_DONE,
130 };
131 
132 #define	SFXGE_SW_EV_MAGIC(_sw_ev) \
133 	(SFXGE_MAGIC_RESERVED | ((_sw_ev) << SFXGE_MAGIC_DMAQ_LABEL_WIDTH))
134 
135 static inline uint16_t
136 sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev, unsigned int label)
137 {
138 	KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
139 	    ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
140 	return SFXGE_SW_EV_MAGIC(sw_ev) | label;
141 }
142 
143 static inline uint16_t
144 sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_rxq *rxq)
145 {
146 	return sfxge_sw_ev_mk_magic(sw_ev, 0);
147 }
148 
149 static inline uint16_t
150 sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_txq *txq)
151 {
152 	return sfxge_sw_ev_mk_magic(sw_ev, txq->type);
153 }
154 
155 enum sfxge_evq_state {
156 	SFXGE_EVQ_UNINITIALIZED = 0,
157 	SFXGE_EVQ_INITIALIZED,
158 	SFXGE_EVQ_STARTING,
159 	SFXGE_EVQ_STARTED
160 };
161 
162 #define	SFXGE_EV_BATCH	16384
163 
164 #define	SFXGE_STATS_UPDATE_PERIOD_MS	1000
165 
166 struct sfxge_evq {
167 	/* Structure members below are sorted by usage order */
168 	struct sfxge_softc	*sc;
169 	struct mtx		lock;
170 	unsigned int		index;
171 	enum sfxge_evq_state	init_state;
172 	efsys_mem_t		mem;
173 	efx_evq_t		*common;
174 	unsigned int		read_ptr;
175 	boolean_t		exception;
176 	unsigned int		rx_done;
177 	unsigned int		tx_done;
178 
179 	/* Linked list of TX queues with completions to process */
180 	struct sfxge_txq	*txq;
181 	struct sfxge_txq	**txqs;
182 
183 	/* Structure members not used on event processing path */
184 	unsigned int		buf_base_id;
185 	unsigned int		entries;
186 	char			lock_name[SFXGE_LOCK_NAME_MAX];
187 } __aligned(CACHE_LINE_SIZE);
188 
189 #define	SFXGE_NDESCS	1024
190 #define	SFXGE_MODERATION	30
191 
192 enum sfxge_intr_state {
193 	SFXGE_INTR_UNINITIALIZED = 0,
194 	SFXGE_INTR_INITIALIZED,
195 	SFXGE_INTR_TESTING,
196 	SFXGE_INTR_STARTED
197 };
198 
199 struct sfxge_intr_hdl {
200 	int			eih_rid;
201 	void			*eih_tag;
202 	struct resource		*eih_res;
203 };
204 
205 struct sfxge_intr {
206 	enum sfxge_intr_state	state;
207 	struct resource		*msix_res;
208 	struct sfxge_intr_hdl	*table;
209 	int			n_alloc;
210 	int			type;
211 	efsys_mem_t		status;
212 	uint32_t		zero_count;
213 };
214 
215 enum sfxge_mcdi_state {
216 	SFXGE_MCDI_UNINITIALIZED = 0,
217 	SFXGE_MCDI_INITIALIZED,
218 	SFXGE_MCDI_BUSY,
219 	SFXGE_MCDI_COMPLETED
220 };
221 
222 struct sfxge_mcdi {
223 	struct mtx		lock;
224 	efsys_mem_t		mem;
225 	enum sfxge_mcdi_state	state;
226 	efx_mcdi_transport_t	transport;
227 
228 	/* Only used in debugging output */
229 	char			lock_name[SFXGE_LOCK_NAME_MAX];
230 };
231 
232 struct sfxge_hw_stats {
233 	clock_t			update_time;
234 	efsys_mem_t		dma_buf;
235 	void			*decode_buf;
236 };
237 
238 enum sfxge_port_state {
239 	SFXGE_PORT_UNINITIALIZED = 0,
240 	SFXGE_PORT_INITIALIZED,
241 	SFXGE_PORT_STARTED
242 };
243 
244 struct sfxge_port {
245 	struct sfxge_softc	*sc;
246 	struct mtx		lock;
247 	enum sfxge_port_state	init_state;
248 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
249 	unsigned int		wanted_fc;
250 #endif
251 	struct sfxge_hw_stats	phy_stats;
252 	struct sfxge_hw_stats	mac_stats;
253 	uint16_t		stats_update_period_ms;
254 	efx_link_mode_t		link_mode;
255 	uint8_t			mcast_addrs[EFX_MAC_MULTICAST_LIST_MAX *
256 					    EFX_MAC_ADDR_LEN];
257 	unsigned int		mcast_count;
258 
259 	/* Only used in debugging output */
260 	char			lock_name[SFXGE_LOCK_NAME_MAX];
261 };
262 
263 enum sfxge_softc_state {
264 	SFXGE_UNINITIALIZED = 0,
265 	SFXGE_INITIALIZED,
266 	SFXGE_REGISTERED,
267 	SFXGE_STARTED
268 };
269 
270 struct sfxge_softc {
271 	device_t			dev;
272 	struct sx			softc_lock;
273 	char				softc_lock_name[SFXGE_LOCK_NAME_MAX];
274 	enum sfxge_softc_state		init_state;
275 	struct ifnet			*ifnet;
276 	unsigned int			if_flags;
277 	struct sysctl_oid		*stats_node;
278 	struct sysctl_oid		*txqs_node;
279 
280 	struct task			task_reset;
281 
282 	efx_family_t			family;
283 	caddr_t				vpd_data;
284 	size_t				vpd_size;
285 	efx_nic_t			*enp;
286 	efsys_lock_t			enp_lock;
287 
288 	unsigned int			rxq_entries;
289 	unsigned int			txq_entries;
290 
291 	bus_dma_tag_t			parent_dma_tag;
292 	efsys_bar_t			bar;
293 
294 	struct sfxge_intr		intr;
295 	struct sfxge_mcdi		mcdi;
296 	struct sfxge_port		port;
297 	uint32_t			buffer_table_next;
298 
299 	struct sfxge_evq		*evq[SFXGE_RX_SCALE_MAX];
300 	unsigned int			ev_moderation;
301 #if EFSYS_OPT_QSTATS
302 	clock_t				ev_stats_update_time;
303 	uint64_t			ev_stats[EV_NQSTATS];
304 #endif
305 
306 	unsigned int			max_rss_channels;
307 	struct sfxge_rxq		*rxq[SFXGE_RX_SCALE_MAX];
308 	unsigned int			rx_indir_table[EFX_RSS_TBL_SIZE];
309 
310 	struct sfxge_txq		*txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
311 
312 	struct ifmedia			media;
313 
314 	size_t				rx_prefix_size;
315 	size_t				rx_buffer_size;
316 	size_t				rx_buffer_align;
317 	int				rx_cluster_size;
318 
319 	unsigned int			evq_max;
320 	unsigned int			evq_count;
321 	unsigned int			rxq_count;
322 	unsigned int			txq_count;
323 
324 	unsigned int			tso_fw_assisted;
325 #define	SFXGE_FATSOV1	(1 << 0)
326 #define	SFXGE_FATSOV2	(1 << 1)
327 
328 #if EFSYS_OPT_MCDI_LOGGING
329 	int				mcdi_logging;
330 #endif
331 };
332 
333 #define	SFXGE_LINK_UP(sc) \
334 	((sc)->port.link_mode != EFX_LINK_DOWN && \
335 	 (sc)->port.link_mode != EFX_LINK_UNKNOWN)
336 #define	SFXGE_RUNNING(sc) ((sc)->ifnet->if_drv_flags & IFF_DRV_RUNNING)
337 
338 #define	SFXGE_PARAM(_name)	"hw.sfxge." #_name
339 
340 SYSCTL_DECL(_hw_sfxge);
341 
342 /*
343  * From sfxge.c.
344  */
345 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
346 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
347 				     uint32_t *idp);
348 
349 /*
350  * From sfxge_dma.c.
351  */
352 extern int sfxge_dma_init(struct sfxge_softc *sc);
353 extern void sfxge_dma_fini(struct sfxge_softc *sc);
354 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
355 			   efsys_mem_t *esmp);
356 extern void sfxge_dma_free(efsys_mem_t *esmp);
357 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
358 				     struct mbuf **mp,
359 				     bus_dma_segment_t *segs,
360 				     int *nsegs, int maxsegs);
361 
362 /*
363  * From sfxge_ev.c.
364  */
365 extern int sfxge_ev_init(struct sfxge_softc *sc);
366 extern void sfxge_ev_fini(struct sfxge_softc *sc);
367 extern int sfxge_ev_start(struct sfxge_softc *sc);
368 extern void sfxge_ev_stop(struct sfxge_softc *sc);
369 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
370 
371 /*
372  * From sfxge_intr.c.
373  */
374 extern int sfxge_intr_init(struct sfxge_softc *sc);
375 extern void sfxge_intr_fini(struct sfxge_softc *sc);
376 extern int sfxge_intr_start(struct sfxge_softc *sc);
377 extern void sfxge_intr_stop(struct sfxge_softc *sc);
378 
379 /*
380  * From sfxge_mcdi.c.
381  */
382 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
383 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
384 extern int sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
385 
386 /*
387  * From sfxge_nvram.c.
388  */
389 extern int sfxge_nvram_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
390 
391 /*
392  * From sfxge_port.c.
393  */
394 extern int sfxge_port_init(struct sfxge_softc *sc);
395 extern void sfxge_port_fini(struct sfxge_softc *sc);
396 extern int sfxge_port_start(struct sfxge_softc *sc);
397 extern void sfxge_port_stop(struct sfxge_softc *sc);
398 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
399 				  efx_link_mode_t mode);
400 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
401 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
402 extern uint64_t sfxge_get_counter(struct ifnet *ifp, ift_counter c);
403 
404 #define	SFXGE_MAX_MTU (9 * 1024)
405 
406 #define	SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname)				\
407 	do {								\
408 		struct sfxge_softc *__sc = (_sc);			\
409 									\
410 		snprintf((__sc)->softc_lock_name,			\
411 			 sizeof((__sc)->softc_lock_name),		\
412 			 "%s:softc", (_ifname));			\
413 		sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name);	\
414 	} while (B_FALSE)
415 #define	SFXGE_ADAPTER_LOCK_DESTROY(_sc)					\
416 	sx_destroy(&(_sc)->softc_lock)
417 #define	SFXGE_ADAPTER_LOCK(_sc)						\
418 	sx_xlock(&(_sc)->softc_lock)
419 #define	SFXGE_ADAPTER_UNLOCK(_sc)					\
420 	sx_xunlock(&(_sc)->softc_lock)
421 #define	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc)				\
422 	sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
423 
424 #define	SFXGE_PORT_LOCK_INIT(_port, _ifname)				\
425 	do {								\
426 		struct sfxge_port *__port = (_port);			\
427 									\
428 		snprintf((__port)->lock_name,				\
429 			 sizeof((__port)->lock_name),			\
430 			 "%s:port", (_ifname));				\
431 		mtx_init(&(__port)->lock, (__port)->lock_name,		\
432 			 NULL, MTX_DEF);				\
433 	} while (B_FALSE)
434 #define	SFXGE_PORT_LOCK_DESTROY(_port)					\
435 	mtx_destroy(&(_port)->lock)
436 #define	SFXGE_PORT_LOCK(_port)						\
437 	mtx_lock(&(_port)->lock)
438 #define	SFXGE_PORT_UNLOCK(_port)					\
439 	mtx_unlock(&(_port)->lock)
440 #define	SFXGE_PORT_LOCK_ASSERT_OWNED(_port)				\
441 	mtx_assert(&(_port)->lock, MA_OWNED)
442 
443 #define	SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname)				\
444 	do {								\
445 		struct sfxge_mcdi  *__mcdi = (_mcdi);			\
446 									\
447 		snprintf((__mcdi)->lock_name,				\
448 			 sizeof((__mcdi)->lock_name),			\
449 			 "%s:mcdi", (_ifname));				\
450 		mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name,		\
451 			 NULL, MTX_DEF);				\
452 	} while (B_FALSE)
453 #define	SFXGE_MCDI_LOCK_DESTROY(_mcdi)					\
454 	mtx_destroy(&(_mcdi)->lock)
455 #define	SFXGE_MCDI_LOCK(_mcdi)						\
456 	mtx_lock(&(_mcdi)->lock)
457 #define	SFXGE_MCDI_UNLOCK(_mcdi)					\
458 	mtx_unlock(&(_mcdi)->lock)
459 #define	SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi)				\
460 	mtx_assert(&(_mcdi)->lock, MA_OWNED)
461 
462 #define	SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index)			\
463 	do {								\
464 		struct sfxge_evq  *__evq = (_evq);			\
465 									\
466 		snprintf((__evq)->lock_name,				\
467 			 sizeof((__evq)->lock_name),			\
468 			 "%s:evq%u", (_ifname), (_evq_index));		\
469 		mtx_init(&(__evq)->lock, (__evq)->lock_name,		\
470 			 NULL, MTX_DEF);				\
471 	} while (B_FALSE)
472 #define	SFXGE_EVQ_LOCK_DESTROY(_evq)					\
473 	mtx_destroy(&(_evq)->lock)
474 #define	SFXGE_EVQ_LOCK(_evq)						\
475 	mtx_lock(&(_evq)->lock)
476 #define	SFXGE_EVQ_UNLOCK(_evq)						\
477 	mtx_unlock(&(_evq)->lock)
478 #define	SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq)				\
479 	mtx_assert(&(_evq)->lock, MA_OWNED)
480 
481 #endif /* _SFXGE_H */
482