xref: /freebsd/sys/net/iflib.h (revision 0c6c134f7959111a99af3893cf74432d71952f49)
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  * Most cards can handle much larger TSO requests
50  * but the FreeBSD TCP stack will break on larger
51  * values
52  */
53 #define FREEBSD_TSO_SIZE_MAX 65518
54 
55 
56 struct iflib_ctx;
57 typedef struct iflib_ctx *if_ctx_t;
58 struct if_shared_ctx;
59 typedef struct if_shared_ctx *if_shared_ctx_t;
60 struct if_int_delay_info;
61 typedef struct if_int_delay_info  *if_int_delay_info_t;
62 struct if_pseudo;
63 typedef struct if_pseudo *if_pseudo_t;
64 
65 /*
66  * File organization:
67  *  - public structures
68  *  - iflib accessors
69  *  - iflib utility functions
70  *  - iflib core functions
71  */
72 
73 typedef struct if_rxd_frag {
74 	uint8_t irf_flid;
75 	qidx_t irf_idx;
76 	uint16_t irf_len;
77 } *if_rxd_frag_t;
78 
79 typedef struct if_rxd_info {
80 	/* set by iflib */
81 	uint16_t iri_qsidx;		/* qset index */
82 	uint16_t iri_vtag;		/* vlan tag - if flag set */
83 	/* XXX redundant with the new irf_len field */
84 	uint16_t iri_len;		/* packet length */
85 	qidx_t iri_cidx;		/* consumer index of cq */
86 	struct ifnet *iri_ifp;		/* some drivers >1 interface per softc */
87 
88 	/* updated by driver */
89 	if_rxd_frag_t iri_frags;
90 	uint32_t iri_flowid;		/* RSS hash for packet */
91 	uint32_t iri_csum_flags;	/* m_pkthdr csum flags */
92 
93 	uint32_t iri_csum_data;		/* m_pkthdr csum data */
94 	uint8_t iri_flags;		/* mbuf flags for packet */
95 	uint8_t	 iri_nfrags;		/* number of fragments in packet */
96 	uint8_t	 iri_rsstype;		/* RSS hash type */
97 	uint8_t	 iri_pad;		/* any padding in the received data */
98 } *if_rxd_info_t;
99 
100 typedef struct if_rxd_update {
101 	uint64_t	*iru_paddrs;
102 	caddr_t		*iru_vaddrs;
103 	qidx_t		*iru_idxs;
104 	qidx_t		iru_pidx;
105 	uint16_t	iru_qsidx;
106 	uint16_t	iru_count;
107 	uint16_t	iru_buf_size;
108 	uint8_t		iru_flidx;
109 } *if_rxd_update_t;
110 
111 #define IPI_TX_INTR	0x1		/* send an interrupt when this packet is sent */
112 #define IPI_TX_IPV4	0x2		/* ethertype IPv4 */
113 #define IPI_TX_IPV6	0x4		/* ethertype IPv6 */
114 
115 typedef struct if_pkt_info {
116 	bus_dma_segment_t	*ipi_segs;	/* physical addresses */
117 	uint32_t		ipi_len;	/* packet length */
118 	uint16_t		ipi_qsidx;	/* queue set index */
119 	qidx_t			ipi_nsegs;	/* number of segments */
120 
121 	qidx_t			ipi_ndescs;	/* number of descriptors used by encap */
122 	uint16_t		ipi_flags;	/* iflib per-packet flags */
123 	qidx_t			ipi_pidx;	/* start pidx for encap */
124 	qidx_t			ipi_new_pidx;	/* next available pidx post-encap */
125 	/* offload handling */
126 	uint8_t			ipi_ehdrlen;	/* ether header length */
127 	uint8_t			ipi_ip_hlen;	/* ip header length */
128 	uint8_t			ipi_tcp_hlen;	/* tcp header length */
129 	uint8_t			ipi_ipproto;	/* ip protocol */
130 
131 	uint32_t		ipi_csum_flags;	/* packet checksum flags */
132 	uint16_t		ipi_tso_segsz;	/* tso segment size */
133 	uint16_t		ipi_vtag;	/* VLAN tag */
134 	uint16_t		ipi_etype;	/* ether header type */
135 	uint8_t			ipi_tcp_hflags;	/* tcp header flags */
136 	uint8_t			ipi_mflags;	/* packet mbuf flags */
137 
138 	uint32_t		ipi_tcp_seq;	/* tcp seqno */
139 	uint32_t		ipi_tcp_sum;	/* tcp csum */
140 } *if_pkt_info_t;
141 
142 typedef struct if_irq {
143 	struct resource  *ii_res;
144 	int               ii_rid;
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 	caddr_t		pvi_name;
174 } pci_vendor_info_t;
175 
176 #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
177 #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
178 #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
179 
180 #define IFLIB_PNP_DESCR "U32:vendor;U32:device;U32:subvendor;U32:subdevice;" \
181     "U32:revision;U32:class;D:#"
182 #define IFLIB_PNP_INFO(b, u, t) \
183     MODULE_PNP_INFO(IFLIB_PNP_DESCR, b, u, t, sizeof(t[0]), 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 } *if_txrx_t;
196 
197 typedef struct if_softc_ctx {
198 	int isc_vectors;
199 	int isc_nrxqsets;
200 	int isc_ntxqsets;
201 	uint8_t isc_min_tx_latency; /* disable doorbell update batching */
202 	uint8_t isc_rx_mvec_enable; /* generate mvecs on rx */
203 	uint32_t isc_txrx_budget_bytes_max;
204 	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
205 	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
206 	int isc_ntxd[8];
207 	int isc_nrxd[8];
208 
209 	uint32_t isc_txqsizes[8];
210 	uint32_t isc_rxqsizes[8];
211 	/* is there such thing as a descriptor that is more than 248 bytes ? */
212 	uint8_t isc_txd_size[8];
213 	uint8_t isc_rxd_size[8];
214 
215 	int isc_tx_tso_segments_max;
216 	int isc_tx_tso_size_max;
217 	int isc_tx_tso_segsize_max;
218 	int isc_tx_csum_flags;
219 	int isc_capenable;
220 	int isc_rss_table_size;
221 	int isc_rss_table_mask;
222 	int isc_nrxqsets_max;
223 	int isc_ntxqsets_max;
224 	uint32_t isc_tx_qdepth;
225 
226 	iflib_intr_mode_t isc_intr;
227 	uint16_t isc_max_frame_size; /* set at init time by driver */
228 	uint16_t isc_min_frame_size; /* set at init time by driver, only used if
229 					IFLIB_NEED_ETHER_PAD is set. */
230 	uint32_t isc_pause_frames;   /* set by driver for iflib_timer to detect */
231 	pci_vendor_info_t isc_vendor_info;	/* set by iflib prior to attach_pre */
232 	int isc_disable_msix;
233 	if_txrx_t isc_txrx;
234 } *if_softc_ctx_t;
235 
236 /*
237  * Initialization values for device
238  */
239 struct if_shared_ctx {
240 	unsigned isc_magic;
241 	driver_t *isc_driver;
242 	bus_size_t isc_q_align;
243 	bus_size_t isc_tx_maxsize;
244 	bus_size_t isc_tx_maxsegsize;
245 	bus_size_t isc_rx_maxsize;
246 	bus_size_t isc_rx_maxsegsize;
247 	int isc_rx_nsegments;
248 	int isc_admin_intrcnt;		/* # of admin/link interrupts */
249 
250 	/* fields necessary for probe */
251 	pci_vendor_info_t *isc_vendor_info;
252 	char *isc_driver_version;
253 /* optional function to transform the read values to match the table*/
254 	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
255 				   uint16_t *subdevice_id, uint16_t *rev_id);
256 	int isc_nrxd_min[8];
257 	int isc_nrxd_default[8];
258 	int isc_nrxd_max[8];
259 	int isc_ntxd_min[8];
260 	int isc_ntxd_default[8];
261 	int isc_ntxd_max[8];
262 
263 	/* actively used during operation */
264 	int isc_nfl __aligned(CACHE_LINE_SIZE);
265 	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
266 	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
267 	int isc_rx_process_limit;
268 	int isc_tx_reclaim_thresh;
269 	int isc_flags;
270 	const char *isc_name;
271 };
272 
273 typedef struct iflib_dma_info {
274 	bus_addr_t		idi_paddr;
275 	caddr_t			idi_vaddr;
276 	bus_dma_tag_t		idi_tag;
277 	bus_dmamap_t		idi_map;
278 	uint32_t		idi_size;
279 } *iflib_dma_info_t;
280 
281 #define IFLIB_MAGIC 0xCAFEF00D
282 
283 typedef enum {
284 	IFLIB_INTR_RX,
285 	IFLIB_INTR_TX,
286 	IFLIB_INTR_RXTX,
287 	IFLIB_INTR_ADMIN,
288 	IFLIB_INTR_IOV,
289 } iflib_intr_type_t;
290 
291 #ifndef ETH_ADDR_LEN
292 #define ETH_ADDR_LEN 6
293 #endif
294 
295 
296 /*
297  * Interface has a separate command queue for RX
298  */
299 #define IFLIB_HAS_RXCQ		0x01
300 /*
301  * Driver has already allocated vectors
302  */
303 #define IFLIB_SKIP_MSIX		0x02
304 /*
305  * Interface is a virtual function
306  */
307 #define IFLIB_IS_VF		0x04
308 /*
309  * Interface has a separate command queue for TX
310  */
311 #define IFLIB_HAS_TXCQ		0x08
312 /*
313  * Interface does checksum in place
314  */
315 #define IFLIB_NEED_SCRATCH	0x10
316 /*
317  * Interface doesn't expect in_pseudo for th_sum
318  */
319 #define IFLIB_TSO_INIT_IP	0x20
320 /*
321  * Interface doesn't align IP header
322  */
323 #define IFLIB_DO_RX_FIXUP	0x40
324 /*
325  * Driver needs csum zeroed for offloading
326  */
327 #define IFLIB_NEED_ZERO_CSUM	0x80
328 /*
329  * Driver needs frames padded to some minimum length
330  */
331 #define IFLIB_NEED_ETHER_PAD	0x100
332 /*
333  * Packets can be freed immediately after encap
334  */
335 #define IFLIB_TXD_ENCAP_PIO	0x00200
336 /*
337  * Use RX completion handler
338  */
339 #define IFLIB_RX_COMPLETION	0x00400
340 /*
341  * Skip refilling cluster free lists
342  */
343 #define IFLIB_SKIP_CLREFILL	0x00800
344 /*
345  * Don't reset on hang
346  */
347 #define IFLIB_NO_HANG_RESET	0x01000
348 /*
349  * Don't need/want most of the niceties of
350  * queue management
351  */
352 #define IFLIB_PSEUDO	0x02000
353 /*
354  * No DMA support needed / wanted
355  */
356 #define IFLIB_VIRTUAL	0x04000
357 /*
358  * autogenerate a MAC address
359  */
360 #define IFLIB_GEN_MAC	0x08000
361 
362 
363 
364 /*
365  * field accessors
366  */
367 void *iflib_get_softc(if_ctx_t ctx);
368 
369 device_t iflib_get_dev(if_ctx_t ctx);
370 
371 if_t iflib_get_ifp(if_ctx_t ctx);
372 
373 struct ifmedia *iflib_get_media(if_ctx_t ctx);
374 
375 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
376 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
377 
378 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
379 
380 /*
381  * If the driver can plug cleanly in to newbus use these
382  */
383 int iflib_device_probe(device_t);
384 int iflib_device_attach(device_t);
385 int iflib_device_detach(device_t);
386 int iflib_device_suspend(device_t);
387 int iflib_device_resume(device_t);
388 int iflib_device_shutdown(device_t);
389 
390 
391 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
392 void iflib_device_iov_uninit(device_t);
393 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
394 
395 /*
396  * If the driver can't plug cleanly in to newbus
397  * use these
398  */
399 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
400 int iflib_device_deregister(if_ctx_t);
401 
402 
403 
404 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
405 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
406 							iflib_intr_type_t type, driver_filter_t *filter,
407 							void *filter_arg, int qid, char *name);
408 void iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,  void *arg, int qid, char *name);
409 
410 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
411 
412 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
413 
414 void iflib_config_gtask_init(void *ctx, struct grouptask *gtask,
415 			     gtask_fn_t *fn, const char *name);
416 
417 void iflib_config_gtask_deinit(struct grouptask *gtask);
418 
419 
420 
421 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
422 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
423 void iflib_admin_intr_deferred(if_ctx_t ctx);
424 void iflib_iov_intr_deferred(if_ctx_t ctx);
425 
426 
427 void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
428 
429 int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
430 void iflib_dma_free(iflib_dma_info_t dma);
431 
432 int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
433 
434 void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
435 
436 
437 struct sx *iflib_ctx_lock_get(if_ctx_t);
438 struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
439 
440 void iflib_led_create(if_ctx_t ctx);
441 
442 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
443 								if_int_delay_info_t, int, int);
444 
445 /*
446  * Pseudo device support
447  */
448 if_pseudo_t iflib_clone_register(if_shared_ctx_t);
449 void iflib_clone_deregister(if_pseudo_t);
450 #endif /*  __IFLIB_H_ */
451