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