xref: /freebsd/sys/dev/sfxge/sfxge.h (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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 #ifdef IFM_ETH_RXPAUSE
97 #define	SFXGE_HAVE_PAUSE_MEDIAOPTS
98 #endif
99 
100 #ifndef CTLTYPE_U64
101 #define	CTLTYPE_U64 CTLTYPE_QUAD
102 #endif
103 
104 #include "sfxge_rx.h"
105 #include "sfxge_tx.h"
106 
107 #define	ROUNDUP_POW_OF_TWO(_n)	(1ULL << flsl((_n) - 1))
108 
109 #define	SFXGE_IP_ALIGN	2
110 
111 #define	SFXGE_ETHERTYPE_LOOPBACK	0x9000	/* Xerox loopback */
112 
113 
114 #define	SFXGE_MAGIC_RESERVED		0x8000
115 
116 #define	SFXGE_MAGIC_DMAQ_LABEL_WIDTH	6
117 #define	SFXGE_MAGIC_DMAQ_LABEL_MASK \
118 	((1 << SFXGE_MAGIC_DMAQ_LABEL_WIDTH) - 1)
119 
120 enum sfxge_sw_ev {
121 	SFXGE_SW_EV_RX_QFLUSH_DONE = 1,
122 	SFXGE_SW_EV_RX_QFLUSH_FAILED,
123 	SFXGE_SW_EV_RX_QREFILL,
124 	SFXGE_SW_EV_TX_QFLUSH_DONE,
125 };
126 
127 #define	SFXGE_SW_EV_MAGIC(_sw_ev) \
128 	(SFXGE_MAGIC_RESERVED | ((_sw_ev) << SFXGE_MAGIC_DMAQ_LABEL_WIDTH))
129 
130 static inline uint16_t
131 sfxge_sw_ev_mk_magic(enum sfxge_sw_ev sw_ev, unsigned int label)
132 {
133 	KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
134 	    ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
135 	return SFXGE_SW_EV_MAGIC(sw_ev) | label;
136 }
137 
138 static inline uint16_t
139 sfxge_sw_ev_rxq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_rxq *rxq)
140 {
141 	return sfxge_sw_ev_mk_magic(sw_ev, 0);
142 }
143 
144 static inline uint16_t
145 sfxge_sw_ev_txq_magic(enum sfxge_sw_ev sw_ev, struct sfxge_txq *txq)
146 {
147 	return sfxge_sw_ev_mk_magic(sw_ev, txq->type);
148 }
149 
150 enum sfxge_evq_state {
151 	SFXGE_EVQ_UNINITIALIZED = 0,
152 	SFXGE_EVQ_INITIALIZED,
153 	SFXGE_EVQ_STARTING,
154 	SFXGE_EVQ_STARTED
155 };
156 
157 #define	SFXGE_EV_BATCH	16384
158 
159 #define	SFXGE_STATS_UPDATE_PERIOD_MS	1000
160 
161 struct sfxge_evq {
162 	/* Structure members below are sorted by usage order */
163 	struct sfxge_softc	*sc;
164 	struct mtx		lock;
165 	unsigned int		index;
166 	enum sfxge_evq_state	init_state;
167 	efsys_mem_t		mem;
168 	efx_evq_t		*common;
169 	unsigned int		read_ptr;
170 	boolean_t		exception;
171 	unsigned int		rx_done;
172 	unsigned int		tx_done;
173 
174 	/* Linked list of TX queues with completions to process */
175 	struct sfxge_txq	*txq;
176 	struct sfxge_txq	**txqs;
177 
178 	/* Structure members not used on event processing path */
179 	unsigned int		buf_base_id;
180 	unsigned int		entries;
181 	char			lock_name[SFXGE_LOCK_NAME_MAX];
182 #if EFSYS_OPT_QSTATS
183 	clock_t			stats_update_time;
184 	uint64_t		stats[EV_NQSTATS];
185 #endif
186 } __aligned(CACHE_LINE_SIZE);
187 
188 #define	SFXGE_NDESCS	1024
189 #define	SFXGE_MODERATION	30
190 
191 enum sfxge_intr_state {
192 	SFXGE_INTR_UNINITIALIZED = 0,
193 	SFXGE_INTR_INITIALIZED,
194 	SFXGE_INTR_TESTING,
195 	SFXGE_INTR_STARTED
196 };
197 
198 struct sfxge_intr_hdl {
199 	int			eih_rid;
200 	void			*eih_tag;
201 	struct resource		*eih_res;
202 };
203 
204 struct sfxge_intr {
205 	enum sfxge_intr_state	state;
206 	struct resource		*msix_res;
207 	struct sfxge_intr_hdl	*table;
208 	int			n_alloc;
209 	int			type;
210 	efsys_mem_t		status;
211 	uint32_t		zero_count;
212 };
213 
214 enum sfxge_mcdi_state {
215 	SFXGE_MCDI_UNINITIALIZED = 0,
216 	SFXGE_MCDI_INITIALIZED,
217 	SFXGE_MCDI_BUSY,
218 	SFXGE_MCDI_COMPLETED
219 };
220 
221 struct sfxge_mcdi {
222 	struct mtx		lock;
223 	efsys_mem_t		mem;
224 	enum sfxge_mcdi_state	state;
225 	efx_mcdi_transport_t	transport;
226 
227 	/* Only used in debugging output */
228 	char			lock_name[SFXGE_LOCK_NAME_MAX];
229 };
230 
231 struct sfxge_hw_stats {
232 	clock_t			update_time;
233 	efsys_mem_t		dma_buf;
234 	void			*decode_buf;
235 };
236 
237 enum sfxge_port_state {
238 	SFXGE_PORT_UNINITIALIZED = 0,
239 	SFXGE_PORT_INITIALIZED,
240 	SFXGE_PORT_STARTED
241 };
242 
243 struct sfxge_port {
244 	struct sfxge_softc	*sc;
245 	struct mtx		lock;
246 	enum sfxge_port_state	init_state;
247 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
248 	unsigned int		wanted_fc;
249 #endif
250 	struct sfxge_hw_stats	phy_stats;
251 	struct sfxge_hw_stats	mac_stats;
252 	uint16_t		stats_update_period_ms;
253 	efx_link_mode_t		link_mode;
254 	uint8_t			mcast_addrs[EFX_MAC_MULTICAST_LIST_MAX *
255 					    EFX_MAC_ADDR_LEN];
256 	unsigned int		mcast_count;
257 
258 	/* Only used in debugging output */
259 	char			lock_name[SFXGE_LOCK_NAME_MAX];
260 };
261 
262 enum sfxge_softc_state {
263 	SFXGE_UNINITIALIZED = 0,
264 	SFXGE_INITIALIZED,
265 	SFXGE_REGISTERED,
266 	SFXGE_STARTED
267 };
268 
269 struct sfxge_softc {
270 	device_t			dev;
271 	struct sx			softc_lock;
272 	char				softc_lock_name[SFXGE_LOCK_NAME_MAX];
273 	enum sfxge_softc_state		init_state;
274 	struct ifnet			*ifnet;
275 	unsigned int			if_flags;
276 	struct sysctl_oid		*stats_node;
277 #if EFSYS_OPT_QSTATS
278 	struct sysctl_oid		*evqs_stats_node;
279 #endif
280 	struct sysctl_oid		*txqs_node;
281 
282 	struct task			task_reset;
283 
284 	efx_family_t			family;
285 	unsigned int			mem_bar;
286 
287 	caddr_t				vpd_data;
288 	size_t				vpd_size;
289 	efx_nic_t			*enp;
290 	efsys_lock_t			enp_lock;
291 
292 	boolean_t			txq_dynamic_cksum_toggle_supported;
293 
294 	unsigned int			rxq_entries;
295 	unsigned int			txq_entries;
296 
297 	bus_dma_tag_t			parent_dma_tag;
298 	efsys_bar_t			bar;
299 
300 	struct sfxge_intr		intr;
301 	struct sfxge_mcdi		mcdi;
302 	struct sfxge_port		port;
303 	uint32_t			buffer_table_next;
304 
305 	struct sfxge_evq		*evq[SFXGE_RX_SCALE_MAX];
306 	unsigned int			ev_moderation;
307 #if EFSYS_OPT_QSTATS
308 	clock_t				ev_stats_update_time;
309 	uint64_t			ev_stats[EV_NQSTATS];
310 #endif
311 
312 	unsigned int			max_rss_channels;
313 	struct sfxge_rxq		*rxq[SFXGE_RX_SCALE_MAX];
314 	unsigned int			rx_indir_table[EFX_RSS_TBL_SIZE];
315 
316 	struct sfxge_txq		*txq[SFXGE_TXQ_NTYPES + SFXGE_RX_SCALE_MAX];
317 
318 	struct ifmedia			media;
319 
320 	size_t				rx_prefix_size;
321 	size_t				rx_buffer_size;
322 	size_t				rx_buffer_align;
323 	int				rx_cluster_size;
324 
325 	unsigned int			evq_max;
326 	unsigned int			evq_count;
327 	unsigned int			rxq_count;
328 	unsigned int			txq_count;
329 
330 	unsigned int			tso_fw_assisted;
331 #define	SFXGE_FATSOV1	(1 << 0)
332 #define	SFXGE_FATSOV2	(1 << 1)
333 
334 #if EFSYS_OPT_MCDI_LOGGING
335 	int				mcdi_logging;
336 #endif
337 };
338 
339 #define	SFXGE_LINK_UP(sc) \
340 	((sc)->port.link_mode != EFX_LINK_DOWN && \
341 	 (sc)->port.link_mode != EFX_LINK_UNKNOWN)
342 #define	SFXGE_RUNNING(sc) ((sc)->ifnet->if_drv_flags & IFF_DRV_RUNNING)
343 
344 #define	SFXGE_PARAM(_name)	"hw.sfxge." #_name
345 
346 SYSCTL_DECL(_hw_sfxge);
347 
348 /*
349  * From sfxge.c.
350  */
351 extern void sfxge_schedule_reset(struct sfxge_softc *sc);
352 extern void sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n,
353 				     uint32_t *idp);
354 
355 /*
356  * From sfxge_dma.c.
357  */
358 extern int sfxge_dma_init(struct sfxge_softc *sc);
359 extern void sfxge_dma_fini(struct sfxge_softc *sc);
360 extern int sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len,
361 			   efsys_mem_t *esmp);
362 extern void sfxge_dma_free(efsys_mem_t *esmp);
363 extern int sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
364 				     struct mbuf **mp,
365 				     bus_dma_segment_t *segs,
366 				     int *nsegs, int maxsegs);
367 
368 /*
369  * From sfxge_ev.c.
370  */
371 extern int sfxge_ev_init(struct sfxge_softc *sc);
372 extern void sfxge_ev_fini(struct sfxge_softc *sc);
373 extern int sfxge_ev_start(struct sfxge_softc *sc);
374 extern void sfxge_ev_stop(struct sfxge_softc *sc);
375 extern int sfxge_ev_qpoll(struct sfxge_evq *evq);
376 
377 /*
378  * From sfxge_intr.c.
379  */
380 extern int sfxge_intr_init(struct sfxge_softc *sc);
381 extern void sfxge_intr_fini(struct sfxge_softc *sc);
382 extern int sfxge_intr_start(struct sfxge_softc *sc);
383 extern void sfxge_intr_stop(struct sfxge_softc *sc);
384 
385 /*
386  * From sfxge_mcdi.c.
387  */
388 extern int sfxge_mcdi_init(struct sfxge_softc *sc);
389 extern void sfxge_mcdi_fini(struct sfxge_softc *sc);
390 extern int sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
391 
392 /*
393  * From sfxge_nvram.c.
394  */
395 extern int sfxge_nvram_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip);
396 
397 /*
398  * From sfxge_port.c.
399  */
400 extern int sfxge_port_init(struct sfxge_softc *sc);
401 extern void sfxge_port_fini(struct sfxge_softc *sc);
402 extern int sfxge_port_start(struct sfxge_softc *sc);
403 extern void sfxge_port_stop(struct sfxge_softc *sc);
404 extern void sfxge_mac_link_update(struct sfxge_softc *sc,
405 				  efx_link_mode_t mode);
406 extern int sfxge_mac_filter_set(struct sfxge_softc *sc);
407 extern int sfxge_port_ifmedia_init(struct sfxge_softc *sc);
408 extern uint64_t sfxge_get_counter(struct ifnet *ifp, ift_counter c);
409 
410 #define	SFXGE_MAX_MTU (9 * 1024)
411 
412 #define	SFXGE_ADAPTER_LOCK_INIT(_sc, _ifname)				\
413 	do {								\
414 		struct sfxge_softc *__sc = (_sc);			\
415 									\
416 		snprintf((__sc)->softc_lock_name,			\
417 			 sizeof((__sc)->softc_lock_name),		\
418 			 "%s:softc", (_ifname));			\
419 		sx_init(&(__sc)->softc_lock, (__sc)->softc_lock_name);	\
420 	} while (B_FALSE)
421 #define	SFXGE_ADAPTER_LOCK_DESTROY(_sc)					\
422 	sx_destroy(&(_sc)->softc_lock)
423 #define	SFXGE_ADAPTER_LOCK(_sc)						\
424 	sx_xlock(&(_sc)->softc_lock)
425 #define	SFXGE_ADAPTER_UNLOCK(_sc)					\
426 	sx_xunlock(&(_sc)->softc_lock)
427 #define	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(_sc)				\
428 	sx_assert(&(_sc)->softc_lock, LA_XLOCKED)
429 
430 #define	SFXGE_PORT_LOCK_INIT(_port, _ifname)				\
431 	do {								\
432 		struct sfxge_port *__port = (_port);			\
433 									\
434 		snprintf((__port)->lock_name,				\
435 			 sizeof((__port)->lock_name),			\
436 			 "%s:port", (_ifname));				\
437 		mtx_init(&(__port)->lock, (__port)->lock_name,		\
438 			 NULL, MTX_DEF);				\
439 	} while (B_FALSE)
440 #define	SFXGE_PORT_LOCK_DESTROY(_port)					\
441 	mtx_destroy(&(_port)->lock)
442 #define	SFXGE_PORT_LOCK(_port)						\
443 	mtx_lock(&(_port)->lock)
444 #define	SFXGE_PORT_UNLOCK(_port)					\
445 	mtx_unlock(&(_port)->lock)
446 #define	SFXGE_PORT_LOCK_ASSERT_OWNED(_port)				\
447 	mtx_assert(&(_port)->lock, MA_OWNED)
448 
449 #define	SFXGE_MCDI_LOCK_INIT(_mcdi, _ifname)				\
450 	do {								\
451 		struct sfxge_mcdi  *__mcdi = (_mcdi);			\
452 									\
453 		snprintf((__mcdi)->lock_name,				\
454 			 sizeof((__mcdi)->lock_name),			\
455 			 "%s:mcdi", (_ifname));				\
456 		mtx_init(&(__mcdi)->lock, (__mcdi)->lock_name,		\
457 			 NULL, MTX_DEF);				\
458 	} while (B_FALSE)
459 #define	SFXGE_MCDI_LOCK_DESTROY(_mcdi)					\
460 	mtx_destroy(&(_mcdi)->lock)
461 #define	SFXGE_MCDI_LOCK(_mcdi)						\
462 	mtx_lock(&(_mcdi)->lock)
463 #define	SFXGE_MCDI_UNLOCK(_mcdi)					\
464 	mtx_unlock(&(_mcdi)->lock)
465 #define	SFXGE_MCDI_LOCK_ASSERT_OWNED(_mcdi)				\
466 	mtx_assert(&(_mcdi)->lock, MA_OWNED)
467 
468 #define	SFXGE_EVQ_LOCK_INIT(_evq, _ifname, _evq_index)			\
469 	do {								\
470 		struct sfxge_evq  *__evq = (_evq);			\
471 									\
472 		snprintf((__evq)->lock_name,				\
473 			 sizeof((__evq)->lock_name),			\
474 			 "%s:evq%u", (_ifname), (_evq_index));		\
475 		mtx_init(&(__evq)->lock, (__evq)->lock_name,		\
476 			 NULL, MTX_DEF);				\
477 	} while (B_FALSE)
478 #define	SFXGE_EVQ_LOCK_DESTROY(_evq)					\
479 	mtx_destroy(&(_evq)->lock)
480 #define	SFXGE_EVQ_LOCK(_evq)						\
481 	mtx_lock(&(_evq)->lock)
482 #define	SFXGE_EVQ_UNLOCK(_evq)						\
483 	mtx_unlock(&(_evq)->lock)
484 #define	SFXGE_EVQ_LOCK_ASSERT_OWNED(_evq)				\
485 	mtx_assert(&(_evq)->lock, MA_OWNED)
486 
487 #endif /* _SFXGE_H */
488