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