xref: /freebsd/sys/net/iflib.h (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*-
2  * Copyright (c) 2014-2017, Matthew Macy (mmacy@nextbsd.org)
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 /*
40  * The value type for indexing, limits max descriptors
41  * to 65535 can be conditionally redefined to uint32_t
42  * in the future if the need arises.
43  */
44 typedef uint16_t qidx_t;
45 #define QIDX_INVALID 0xFFFF
46 /*
47  * Most cards can handle much larger TSO requests
48  * but the FreeBSD TCP stack will break on larger
49  * values
50  */
51 #define FREEBSD_TSO_SIZE_MAX 65518
52 
53 
54 struct iflib_ctx;
55 typedef struct iflib_ctx *if_ctx_t;
56 struct if_shared_ctx;
57 typedef struct if_shared_ctx *if_shared_ctx_t;
58 struct if_int_delay_info;
59 typedef struct if_int_delay_info  *if_int_delay_info_t;
60 
61 /*
62  * File organization:
63  *  - public structures
64  *  - iflib accessors
65  *  - iflib utility functions
66  *  - iflib core functions
67  */
68 
69 typedef struct if_rxd_frag {
70 	uint8_t irf_flid;
71 	qidx_t irf_idx;
72 	uint16_t irf_len;
73 } *if_rxd_frag_t;
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 	struct ifnet *iri_ifp;		/* some drivers >1 interface 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 	caddr_t		*iru_vaddrs;
99 	qidx_t		*iru_idxs;
100 	qidx_t		iru_pidx;
101 	uint16_t	iru_qsidx;
102 	uint16_t	iru_count;
103 	uint16_t	iru_buf_size;
104 	uint8_t		iru_flidx;
105 } *if_rxd_update_t;
106 
107 #define IPI_TX_INTR	0x1		/* send an interrupt when this packet is sent */
108 #define IPI_TX_IPV4	0x2		/* ethertype IPv4 */
109 #define IPI_TX_IPV6	0x4		/* ethertype IPv6 */
110 
111 typedef struct if_pkt_info {
112 	bus_dma_segment_t	*ipi_segs;	/* physical addresses */
113 	uint32_t		ipi_len;	/* packet length */
114 	uint16_t		ipi_qsidx;	/* queue set index */
115 	qidx_t			ipi_nsegs;	/* number of segments */
116 
117 	qidx_t			ipi_ndescs;	/* number of descriptors used by encap */
118 	uint16_t		ipi_flags;	/* iflib per-packet flags */
119 	qidx_t			ipi_pidx;	/* start pidx for encap */
120 	qidx_t			ipi_new_pidx;	/* next available pidx post-encap */
121 	/* offload handling */
122 	uint8_t			ipi_ehdrlen;	/* ether header length */
123 	uint8_t			ipi_ip_hlen;	/* ip header length */
124 	uint8_t			ipi_tcp_hlen;	/* tcp header length */
125 	uint8_t			ipi_ipproto;	/* ip protocol */
126 
127 	uint32_t		ipi_csum_flags;	/* packet checksum flags */
128 	uint16_t		ipi_tso_segsz;	/* tso segment size */
129 	uint16_t		ipi_vtag;	/* VLAN tag */
130 	uint16_t		ipi_etype;	/* ether header type */
131 	uint8_t			ipi_tcp_hflags;	/* tcp header flags */
132 	uint8_t			ipi_mflags;	/* packet mbuf flags */
133 
134 	uint32_t		ipi_tcp_seq;	/* tcp seqno */
135 	uint32_t		ipi_tcp_sum;	/* tcp csum */
136 } *if_pkt_info_t;
137 
138 typedef struct if_irq {
139 	struct resource  *ii_res;
140 	int               ii_rid;
141 	void             *ii_tag;
142 } *if_irq_t;
143 
144 struct if_int_delay_info {
145 	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
146 	int iidi_offset;			/* Register offset to read/write */
147 	int iidi_value;			/* Current value in usecs */
148 	struct sysctl_oid *iidi_oidp;
149 	struct sysctl_req *iidi_req;
150 };
151 
152 typedef enum {
153 	IFLIB_INTR_LEGACY,
154 	IFLIB_INTR_MSI,
155 	IFLIB_INTR_MSIX
156 } iflib_intr_mode_t;
157 
158 /*
159  * This really belongs in pciio.h or some place more general
160  * but this is the only consumer for now.
161  */
162 typedef struct pci_vendor_info {
163 	uint32_t	pvi_vendor_id;
164 	uint32_t	pvi_device_id;
165 	uint32_t	pvi_subvendor_id;
166 	uint32_t	pvi_subdevice_id;
167 	uint32_t	pvi_rev_id;
168 	uint32_t	pvi_class_mask;
169 	caddr_t		pvi_name;
170 } pci_vendor_info_t;
171 
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 typedef struct if_txrx {
177 	int (*ift_txd_encap) (void *, if_pkt_info_t);
178 	void (*ift_txd_flush) (void *, uint16_t, qidx_t pidx);
179 	int (*ift_txd_credits_update) (void *, uint16_t qsidx, bool clear);
180 
181 	int (*ift_rxd_available) (void *, uint16_t qsidx, qidx_t pidx, qidx_t budget);
182 	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
183 	void (*ift_rxd_refill) (void * , if_rxd_update_t iru);
184 	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, qidx_t pidx);
185 	int (*ift_legacy_intr) (void *);
186 } *if_txrx_t;
187 
188 typedef struct if_softc_ctx {
189 	int isc_vectors;
190 	int isc_nrxqsets;
191 	int isc_ntxqsets;
192 	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
193 	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
194 	int isc_ntxd[8];
195 	int isc_nrxd[8];
196 
197 	uint32_t isc_txqsizes[8];
198 	uint32_t isc_rxqsizes[8];
199 	/* is there such thing as a descriptor that is more than 248 bytes ? */
200 	uint8_t isc_txd_size[8];
201 	uint8_t isc_rxd_size[8];
202 
203 	int isc_max_txqsets;
204 	int isc_max_rxqsets;
205 	int isc_tx_tso_segments_max;
206 	int isc_tx_tso_size_max;
207 	int isc_tx_tso_segsize_max;
208 	int isc_tx_csum_flags;
209 	int isc_capenable;
210 	int isc_rss_table_size;
211 	int isc_rss_table_mask;
212 	int isc_nrxqsets_max;
213 	int isc_ntxqsets_max;
214 
215 	iflib_intr_mode_t isc_intr;
216 	uint16_t isc_max_frame_size; /* set at init time by driver */
217 	uint32_t isc_pause_frames;   /* set by driver for iflib_timer to detect */
218 	pci_vendor_info_t isc_vendor_info;	/* set by iflib prior to attach_pre */
219 	int isc_disable_msix;
220 	if_txrx_t isc_txrx;
221 } *if_softc_ctx_t;
222 
223 /*
224  * Initialization values for device
225  */
226 struct if_shared_ctx {
227 	int isc_magic;
228 	driver_t *isc_driver;
229 	bus_size_t isc_q_align;
230 	bus_size_t isc_tx_maxsize;
231 	bus_size_t isc_tx_maxsegsize;
232 	bus_size_t isc_rx_maxsize;
233 	bus_size_t isc_rx_maxsegsize;
234 	int isc_rx_nsegments;
235 	int isc_admin_intrcnt;		/* # of admin/link interrupts */
236 
237 	/* fields necessary for probe */
238 	pci_vendor_info_t *isc_vendor_info;
239 	char *isc_driver_version;
240 /* optional function to transform the read values to match the table*/
241 	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
242 				   uint16_t *subdevice_id, uint16_t *rev_id);
243 	int isc_nrxd_min[8];
244 	int isc_nrxd_default[8];
245 	int isc_nrxd_max[8];
246 	int isc_ntxd_min[8];
247 	int isc_ntxd_default[8];
248 	int isc_ntxd_max[8];
249 
250 	/* actively used during operation */
251 	int isc_nfl __aligned(CACHE_LINE_SIZE);
252 	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
253 	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
254 	int isc_rx_process_limit;
255 	int isc_tx_reclaim_thresh;
256 	int isc_flags;
257 };
258 
259 typedef struct iflib_dma_info {
260 	bus_addr_t		idi_paddr;
261 	caddr_t			idi_vaddr;
262 	bus_dma_tag_t		idi_tag;
263 	bus_dmamap_t		idi_map;
264 	uint32_t		idi_size;
265 } *iflib_dma_info_t;
266 
267 #define IFLIB_MAGIC 0xCAFEF00D
268 
269 typedef enum {
270 	IFLIB_INTR_RX,
271 	IFLIB_INTR_TX,
272 	IFLIB_INTR_RXTX,
273 	IFLIB_INTR_ADMIN,
274 	IFLIB_INTR_IOV,
275 } iflib_intr_type_t;
276 
277 #ifndef ETH_ADDR_LEN
278 #define ETH_ADDR_LEN 6
279 #endif
280 
281 
282 /*
283  * Interface has a separate command queue for RX
284  */
285 #define IFLIB_HAS_RXCQ		0x01
286 /*
287  * Driver has already allocated vectors
288  */
289 #define IFLIB_SKIP_MSIX		0x02
290 /*
291  * Interface is a virtual function
292  */
293 #define IFLIB_IS_VF		0x04
294 /*
295  * Interface has a separate command queue for TX
296  */
297 #define IFLIB_HAS_TXCQ		0x08
298 /*
299  * Interface does checksum in place
300  */
301 #define IFLIB_NEED_SCRATCH	0x10
302 /*
303  * Interface doesn't expect in_pseudo for th_sum
304  */
305 #define IFLIB_TSO_INIT_IP	0x20
306 /*
307  * Interface doesn't align IP header
308  */
309 #define IFLIB_DO_RX_FIXUP	0x40
310 
311 
312 
313 /*
314  * field accessors
315  */
316 void *iflib_get_softc(if_ctx_t ctx);
317 
318 device_t iflib_get_dev(if_ctx_t ctx);
319 
320 if_t iflib_get_ifp(if_ctx_t ctx);
321 
322 struct ifmedia *iflib_get_media(if_ctx_t ctx);
323 
324 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
325 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
326 
327 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
328 
329 /*
330  * If the driver can plug cleanly in to newbus use these
331  */
332 int iflib_device_probe(device_t);
333 int iflib_device_attach(device_t);
334 int iflib_device_detach(device_t);
335 int iflib_device_suspend(device_t);
336 int iflib_device_resume(device_t);
337 int iflib_device_shutdown(device_t);
338 
339 
340 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
341 void iflib_device_iov_uninit(device_t);
342 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
343 
344 /*
345  * If the driver can't plug cleanly in to newbus
346  * use these
347  */
348 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
349 int iflib_device_deregister(if_ctx_t);
350 
351 
352 
353 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
354 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
355 							iflib_intr_type_t type, driver_filter_t *filter,
356 							void *filter_arg, int qid, char *name);
357 void iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name);
358 
359 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
360 
361 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
362 
363 void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask,
364 			     gtask_fn_t *fn, char *name);
365 
366 void iflib_config_gtask_deinit(struct grouptask *gtask);
367 
368 
369 
370 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
371 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
372 void iflib_admin_intr_deferred(if_ctx_t ctx);
373 void iflib_iov_intr_deferred(if_ctx_t ctx);
374 
375 
376 void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
377 
378 int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
379 void iflib_dma_free(iflib_dma_info_t dma);
380 
381 int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
382 
383 void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
384 
385 
386 struct mtx *iflib_ctx_lock_get(if_ctx_t);
387 struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
388 
389 void iflib_led_create(if_ctx_t ctx);
390 
391 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
392 								if_int_delay_info_t, int, int);
393 
394 #endif /*  __IFLIB_H_ */
395