xref: /haiku/src/libs/compat/freebsd_iflib/compat/net/iflib.h (revision 3fcde6b2363280c17284a4a14a590769ce5ffc48)
1 /*-
2  * Copyright (c) 2014-2017, Matthew Macy (mmacy@mattmacy.io)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *
11  *  2. Neither the name of Matthew Macy nor the names of its
12  *     contributors may be used to endorse or promote products derived from
13  *     this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef __IFLIB_H_
28 #define __IFLIB_H_
29 
30 #include <sys/kobj.h>
31 #include <sys/bus.h>
32 #include <machine/bus.h>
33 #include <sys/nv.h>
34 #include <sys/gtaskqueue.h>
35 
36 /*
37  * The value type for indexing, limits max descriptors
38  * to 65535 can be conditionally redefined to uint32_t
39  * in the future if the need arises.
40  */
41 typedef uint16_t qidx_t;
42 #define QIDX_INVALID 0xFFFF
43 
44 struct iflib_ctx;
45 typedef struct iflib_ctx *if_ctx_t;
46 struct if_shared_ctx;
47 typedef const struct if_shared_ctx *if_shared_ctx_t;
48 struct if_int_delay_info;
49 typedef struct if_int_delay_info  *if_int_delay_info_t;
50 
51 /*
52  * File organization:
53  *  - public structures
54  *  - iflib accessors
55  *  - iflib utility functions
56  *  - iflib core functions
57  */
58 
59 typedef struct if_rxd_frag {
60 	uint8_t irf_flid;
61 	qidx_t irf_idx;
62 	uint16_t irf_len;
63 } *if_rxd_frag_t;
64 
65 /* bnxt supports 64 with hardware LRO enabled */
66 #define IFLIB_MAX_RX_SEGS		64
67 
68 typedef struct if_rxd_info {
69 	/* set by iflib */
70 	uint16_t iri_qsidx;		/* qset index */
71 	uint16_t iri_vtag;		/* vlan tag - if flag set */
72 	/* XXX redundant with the new irf_len field */
73 	uint16_t iri_len;		/* packet length */
74 	qidx_t iri_cidx;		/* consumer index of cq */
75 	if_t iri_ifp;			/* driver may have >1 iface per softc */
76 
77 	/* updated by driver */
78 	if_rxd_frag_t iri_frags;
79 	uint32_t iri_flowid;		/* RSS hash for packet */
80 	uint32_t iri_csum_flags;	/* m_pkthdr csum flags */
81 
82 	uint32_t iri_csum_data;		/* m_pkthdr csum data */
83 	uint8_t iri_flags;		/* mbuf flags for packet */
84 	uint8_t	 iri_nfrags;		/* number of fragments in packet */
85 	uint8_t	 iri_rsstype;		/* RSS hash type */
86 	uint8_t	 iri_pad;		/* any padding in the received data */
87 } *if_rxd_info_t;
88 
89 typedef struct if_rxd_update {
90 	uint64_t	*iru_paddrs;
91 	qidx_t		*iru_idxs;
92 	qidx_t		iru_pidx;
93 	uint16_t	iru_qsidx;
94 	uint16_t	iru_count;
95 	uint16_t	iru_buf_size;
96 	uint8_t		iru_flidx;
97 } *if_rxd_update_t;
98 
99 #define IPI_TX_INTR	0x1		/* send an interrupt when this packet is sent */
100 #define IPI_TX_IPV4	0x2		/* ethertype IPv4 */
101 #define IPI_TX_IPV6	0x4		/* ethertype IPv6 */
102 
103 typedef struct if_pkt_info {
104 	bus_dma_segment_t	*ipi_segs;	/* physical addresses */
105 	uint32_t		ipi_len;	/* packet length */
106 	uint16_t		ipi_qsidx;	/* queue set index */
107 	qidx_t			ipi_nsegs;	/* number of segments */
108 
109 	qidx_t			ipi_ndescs;	/* number of descriptors used by encap */
110 	uint16_t		ipi_flags;	/* iflib per-packet flags */
111 	qidx_t			ipi_pidx;	/* start pidx for encap */
112 	qidx_t			ipi_new_pidx;	/* next available pidx post-encap */
113 	/* offload handling */
114 	uint8_t			ipi_ehdrlen;	/* ether header length */
115 	uint8_t			ipi_ip_hlen;	/* ip header length */
116 	uint8_t			ipi_tcp_hlen;	/* tcp header length */
117 	uint8_t			ipi_ipproto;	/* ip protocol */
118 
119 	uint32_t		ipi_csum_flags;	/* packet checksum flags */
120 	uint16_t		ipi_tso_segsz;	/* tso segment size */
121 	uint16_t		ipi_vtag;	/* VLAN tag */
122 	uint16_t		ipi_etype;	/* ether header type */
123 	uint8_t			ipi_tcp_hflags;	/* tcp header flags */
124 	uint8_t			ipi_mflags;	/* packet mbuf flags */
125 
126 	uint32_t		ipi_tcp_seq;	/* tcp seqno */
127 	uint8_t			ipi_ip_tos;	/* IP ToS field data */
128 	uint8_t			__spare0__;
129 	uint16_t		__spare1__;
130 } *if_pkt_info_t;
131 
132 typedef struct if_irq {
133 	struct resource  *ii_res;
134 	int               __spare0__;
135 	void             *ii_tag;
136 } *if_irq_t;
137 
138 struct if_int_delay_info {
139 	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
140 	int iidi_offset;			/* Register offset to read/write */
141 	int iidi_value;			/* Current value in usecs */
142 	struct sysctl_oid *iidi_oidp;
143 	struct sysctl_req *iidi_req;
144 };
145 
146 typedef enum {
147 	IFLIB_INTR_LEGACY,
148 	IFLIB_INTR_MSI,
149 	IFLIB_INTR_MSIX
150 } iflib_intr_mode_t;
151 
152 /*
153  * This really belongs in pciio.h or some place more general
154  * but this is the only consumer for now.
155  */
156 typedef struct pci_vendor_info {
157 	uint32_t	pvi_vendor_id;
158 	uint32_t	pvi_device_id;
159 	uint32_t	pvi_subvendor_id;
160 	uint32_t	pvi_subdevice_id;
161 	uint32_t	pvi_rev_id;
162 	uint32_t	pvi_class_mask;
163 	const char	*pvi_name;
164 } pci_vendor_info_t;
165 #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
166 #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
167 #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
168 
169 /* No drivers in tree currently match on anything except vendor:device. */
170 #define IFLIB_PNP_DESCR "U32:vendor;U32:device;U32:#;U32:#;" \
171     "U32:#;U32:#;D:#"
172 #define IFLIB_PNP_INFO(b, u, t) \
173     MODULE_PNP_INFO(IFLIB_PNP_DESCR, b, u, t, nitems(t) - 1)
174 
175 typedef struct if_txrx {
176 	int (*ift_txd_encap) (void *, if_pkt_info_t);
177 	void (*ift_txd_flush) (void *, uint16_t, qidx_t pidx);
178 	int (*ift_txd_credits_update) (void *, uint16_t qsidx, bool clear);
179 
180 	int (*ift_rxd_available) (void *, uint16_t qsidx, qidx_t pidx, qidx_t budget);
181 	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
182 	void (*ift_rxd_refill) (void * , if_rxd_update_t iru);
183 	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, qidx_t pidx);
184 	int (*ift_legacy_intr) (void *);
185 	qidx_t (*ift_txq_select) (void *, struct mbuf *);
186 	qidx_t (*ift_txq_select_v2) (void *, struct mbuf *, if_pkt_info_t);
187 } *if_txrx_t;
188 
189 typedef struct if_softc_ctx {
190 	int isc_vectors;
191 	int isc_nrxqsets;
192 	int isc_ntxqsets;
193 	uint16_t __spare0__;
194 	uint32_t __spare1__;
195 	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
196 	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
197 	int isc_ntxd[8];
198 	int isc_nrxd[8];
199 
200 	uint32_t isc_txqsizes[8];
201 	uint32_t isc_rxqsizes[8];
202 	/* is there such thing as a descriptor that is more than 248 bytes ? */
203 	uint8_t isc_txd_size[8];
204 	uint8_t isc_rxd_size[8];
205 
206 	int isc_tx_tso_segments_max;
207 	int isc_tx_tso_size_max;
208 	int isc_tx_tso_segsize_max;
209 	int isc_tx_csum_flags;
210 	int isc_capabilities;
211 	int isc_capenable;
212 	int isc_rss_table_size;
213 	int isc_rss_table_mask;
214 	int isc_nrxqsets_max;
215 	int isc_ntxqsets_max;
216 	uint32_t __spare2__;
217 
218 	iflib_intr_mode_t isc_intr;
219 	uint16_t isc_rxd_buf_size[8]; /* set at init time by driver, 0
220 				         means use iflib-calculated size
221 				         based on isc_max_frame_size */
222 	uint16_t isc_max_frame_size; /* set at init time by driver */
223 	uint16_t isc_min_frame_size; /* set at init time by driver, only used if
224 					IFLIB_NEED_ETHER_PAD is set. */
225 	uint32_t isc_pause_frames;   /* set by driver for iflib_timer to detect */
226 	uint32_t __spare3__;
227 	uint32_t __spare4__;
228 	uint32_t __spare5__;
229 	uint32_t __spare6__;
230 	uint32_t __spare7__;
231 	uint32_t __spare8__;
232 	caddr_t __spare9__;
233 	int isc_disable_msix;
234 	if_txrx_t isc_txrx;
235 	struct ifmedia *isc_media;
236 	bus_size_t isc_dma_width;	/* device dma width in bits, 0 means
237 					   use BUS_SPACE_MAXADDR instead */
238 } *if_softc_ctx_t;
239 
240 /*
241  * Initialization values for device
242  */
243 struct if_shared_ctx {
244 	unsigned isc_magic;
245 	driver_t *isc_driver;
246 	bus_size_t isc_q_align;
247 	bus_size_t isc_tx_maxsize;
248 	bus_size_t isc_tx_maxsegsize;
249 	bus_size_t isc_tso_maxsize;
250 	bus_size_t isc_tso_maxsegsize;
251 	bus_size_t isc_rx_maxsize;
252 	bus_size_t isc_rx_maxsegsize;
253 	int isc_rx_nsegments;
254 	int isc_admin_intrcnt;		/* # of admin/link interrupts */
255 
256 	/* fields necessary for probe */
257 	const pci_vendor_info_t *isc_vendor_info;
258 	const char *isc_driver_version;
259 	/* optional function to transform the read values to match the table*/
260 	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
261 				   uint16_t *subdevice_id, uint16_t *rev_id);
262 	int isc_nrxd_min[8];
263 	int isc_nrxd_default[8];
264 	int isc_nrxd_max[8];
265 	int isc_ntxd_min[8];
266 	int isc_ntxd_default[8];
267 	int isc_ntxd_max[8];
268 
269 	/* actively used during operation */
270 	int isc_nfl __aligned(CACHE_LINE_SIZE);
271 	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
272 	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
273 	int __spare0__;
274 	int isc_tx_reclaim_thresh;
275 	int isc_flags;
276 };
277 
278 typedef struct iflib_dma_info {
279 	bus_addr_t		idi_paddr;
280 	caddr_t			idi_vaddr;
281 	bus_dma_tag_t		idi_tag;
282 	bus_dmamap_t		idi_map;
283 	uint32_t		idi_size;
284 } *iflib_dma_info_t;
285 
286 #define IFLIB_MAGIC 0xCAFEF00D
287 
288 typedef enum {
289 	/* Interrupt or softirq handles only receive */
290 	IFLIB_INTR_RX,
291 
292 	/* Interrupt or softirq handles only transmit */
293 	IFLIB_INTR_TX,
294 
295 	/*
296 	 * Interrupt will check for both pending receive
297 	 * and available tx credits and dispatch a task
298 	 * for one or both depending on the disposition
299 	 * of the respective queues.
300 	 */
301 	IFLIB_INTR_RXTX,
302 
303 	/*
304 	 * Other interrupt - typically link status and
305 	 * or error conditions.
306 	 */
307 	IFLIB_INTR_ADMIN,
308 
309 	/* Softirq (task) for iov handling */
310 	IFLIB_INTR_IOV,
311 } iflib_intr_type_t;
312 
313 /*
314  * Interface has a separate completion queue for RX
315  */
316 #define IFLIB_HAS_RXCQ		0x01
317 /*
318  * Driver has already allocated vectors
319  */
320 #define IFLIB_SKIP_MSIX		0x02
321 /*
322  * Interface is a virtual function
323  */
324 #define IFLIB_IS_VF		0x04
325 /*
326  * Interface has a separate completion queue for TX
327  */
328 #define IFLIB_HAS_TXCQ		0x08
329 /*
330  * Interface does checksum in place
331  */
332 #define IFLIB_NEED_SCRATCH	0x10
333 /*
334  * Interface doesn't expect in_pseudo for th_sum
335  */
336 #define IFLIB_TSO_INIT_IP	0x20
337 /*
338  * Interface doesn't align IP header
339  */
340 #define IFLIB_DO_RX_FIXUP	0x40
341 /*
342  * Driver needs csum zeroed for offloading
343  */
344 #define IFLIB_NEED_ZERO_CSUM	0x80
345 /*
346  * Driver needs frames padded to some minimum length
347  */
348 #define IFLIB_NEED_ETHER_PAD	0x100
349 #define	IFLIB_SPARE7		0x200
350 #define	IFLIB_SPARE6		0x400
351 #define	IFLIB_SPARE5		0x800
352 #define	IFLIB_SPARE4		0x1000
353 #define	IFLIB_SPARE3		0x2000
354 #define	IFLIB_SPARE2		0x4000
355 #define	IFLIB_SPARE1		0x8000
356 /*
357  * Interface needs admin task to ignore interface up/down status
358  */
359 #define IFLIB_ADMIN_ALWAYS_RUN	0x10000
360 /*
361  * Driver will pass the media
362  */
363 #define IFLIB_DRIVER_MEDIA	0x20000
364 /*
365  * When using a single hardware interrupt for the interface, only process RX
366  * interrupts instead of doing combined RX/TX processing.
367  */
368 #define	IFLIB_SINGLE_IRQ_RX_ONLY	0x40000
369 #define	IFLIB_SPARE0		0x80000
370 #ifndef __HAIKU__
371 /*
372  * Interface has an admin completion queue
373  */
374 #define IFLIB_HAS_ADMINCQ	0x100000
375 #endif
376 /*
377  * Interface needs to preserve TX ring indices across restarts.
378  */
379 #define IFLIB_PRESERVE_TX_INDICES	0x200000
380 
381 /* The following IFLIB_FEATURE_* defines are for driver modules to determine
382  * what features this version of iflib supports. They shall be defined to the
383  * first __FreeBSD_version that introduced the feature.
384  */
385 /*
386  * Driver can set its own TX queue selection function
387  * as ift_txq_select in struct if_txrx
388  */
389 #define IFLIB_FEATURE_QUEUE_SELECT	1400050
390 /*
391  * Driver can set its own TX queue selection function
392  * as ift_txq_select_v2 in struct if_txrx. This includes
393  * having iflib send L3+ extra header information to the
394  * function.
395  */
396 #define IFLIB_FEATURE_QUEUE_SELECT_V2	1400073
397 /*
398  * Driver can create subinterfaces with their own Tx/Rx queues
399  * that all share a single device (or commonly, port)
400  */
401 #define IFLIB_FEATURE_SUB_INTERFACES	1500014
402 
403 /*
404  * These enum values are used in iflib_needs_restart to indicate to iflib
405  * functions whether or not the interface needs restarting when certain events
406  * happen.
407  */
408 enum iflib_restart_event {
409 	IFLIB_RESTART_VLAN_CONFIG,
410 };
411 
412 /*
413  * field accessors
414  */
415 void *iflib_get_softc(if_ctx_t ctx);
416 
417 device_t iflib_get_dev(if_ctx_t ctx);
418 
419 if_t iflib_get_ifp(if_ctx_t ctx);
420 
421 struct ifmedia *iflib_get_media(if_ctx_t ctx);
422 
423 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
424 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
425 
426 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
427 void iflib_request_reset(if_ctx_t ctx);
428 uint8_t iflib_in_detach(if_ctx_t ctx);
429 
430 uint32_t iflib_get_rx_mbuf_sz(if_ctx_t ctx);
431 
432 /*
433  * If the driver can plug cleanly in to newbus use these
434  */
435 int iflib_device_probe(device_t);
436 int iflib_device_attach(device_t);
437 int iflib_device_detach(device_t);
438 int iflib_device_suspend(device_t);
439 int iflib_device_resume(device_t);
440 int iflib_device_shutdown(device_t);
441 
442 /*
443  * Use this instead of iflib_device_probe if the driver should report
444  * BUS_PROBE_VENDOR instead of BUS_PROBE_DEFAULT. (For example, an out-of-tree
445  * driver based on iflib).
446  */
447 int iflib_device_probe_vendor(device_t);
448 
449 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
450 void iflib_device_iov_uninit(device_t);
451 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
452 
453 /*
454  * If the driver can't plug cleanly in to newbus
455  * use these
456  */
457 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
458 int iflib_device_deregister(if_ctx_t);
459 
460 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg,
461 		    driver_intr_t, void *arg, const char *name);
462 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
463 			    iflib_intr_type_t type, driver_filter_t *filter,
464 			    void *filter_arg, int qid, const char *name);
465 void iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq,
466 				 iflib_intr_type_t type,  void *arg, int qid,
467 				 const char *name);
468 
469 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
470 
471 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu,
472     const char *name);
473 
474 void iflib_config_gtask_init(void *ctx, struct grouptask *gtask,
475 			     gtask_fn_t *fn, const char *name);
476 void iflib_config_gtask_deinit(struct grouptask *gtask);
477 
478 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
479 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
480 void iflib_admin_intr_deferred(if_ctx_t ctx);
481 void iflib_iov_intr_deferred(if_ctx_t ctx);
482 
483 void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
484 
485 int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
486 int iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags);
487 void iflib_dma_free(iflib_dma_info_t dma);
488 int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
489 
490 void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
491 
492 struct sx *iflib_ctx_lock_get(if_ctx_t);
493 
494 void iflib_led_create(if_ctx_t ctx);
495 
496 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
497 								if_int_delay_info_t, int, int);
498 uint16_t iflib_get_extra_msix_vectors_sysctl(if_ctx_t ctx);
499 
500 /*
501  * Sub-interface support
502  */
503 int iflib_irq_alloc_generic_subctx(if_ctx_t ctx, if_ctx_t subctx, if_irq_t irq,
504 				   int rid, iflib_intr_type_t type,
505 				   driver_filter_t *filter, void *filter_arg,
506 				   int qid, const char *name);
507 #endif /*  __IFLIB_H_ */
508