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