xref: /freebsd/sys/net/iflib.h (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 2014-2015, 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/bus_dma.h>
37 #include <sys/nv.h>
38 #include <sys/gtaskqueue.h>
39 
40 
41 /*
42  * Most cards can handle much larger TSO requests
43  * but the FreeBSD TCP stack will break on larger
44  * values
45  */
46 #define FREEBSD_TSO_SIZE_MAX 65518
47 
48 
49 struct iflib_ctx;
50 typedef struct iflib_ctx *if_ctx_t;
51 struct if_shared_ctx;
52 typedef 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 
56 /*
57  * File organization:
58  *  - public structures
59  *  - iflib accessors
60  *  - iflib utility functions
61  *  - iflib core functions
62  */
63 
64 typedef struct if_rxd_frag {
65 	uint8_t irf_flid;
66 	uint16_t irf_idx;
67 	uint16_t irf_len;
68 } *if_rxd_frag_t;
69 
70 typedef struct if_rxd_info {
71 	/* set by iflib */
72 	uint16_t iri_qsidx;		/* qset index */
73 	uint16_t iri_vtag;		/* vlan tag - if flag set */
74 	/* XXX redundant with the new irf_len field */
75 	uint16_t iri_len;		/* packet length */
76 	uint16_t iri_cidx;		/* consumer index of cq */
77 	struct ifnet *iri_ifp;		/* some drivers >1 interface per softc */
78 
79 	/* updated by driver */
80 	uint16_t iri_flags;		/* mbuf flags for packet */
81 	uint32_t iri_flowid;		/* RSS hash for packet */
82 	uint32_t iri_csum_flags;	/* m_pkthdr csum flags */
83 	uint32_t iri_csum_data;		/* m_pkthdr csum data */
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_frag_t iri_frags;
88 } *if_rxd_info_t;
89 
90 #define IPI_TX_INTR	0x1		/* send an interrupt when this packet is sent */
91 #define IPI_TX_IPV4	0x2		/* ethertype IPv4 */
92 #define IPI_TX_IPV6	0x4		/* ethertype IPv6 */
93 
94 typedef struct if_pkt_info {
95 	uint32_t			ipi_len;	/* packet length */
96 	bus_dma_segment_t		*ipi_segs;	/* physical addresses */
97 	uint16_t			ipi_qsidx;	/* queue set index */
98 	uint16_t			ipi_nsegs;	/* number of segments */
99 	uint16_t			ipi_ndescs;	/* number of descriptors used by encap */
100 	uint16_t			ipi_flags;	/* iflib per-packet flags */
101 	uint32_t			ipi_pidx;	/* start pidx for encap */
102 	uint32_t			ipi_new_pidx;	/* next available pidx post-encap */
103 	/* offload handling */
104 	uint64_t			ipi_csum_flags;	/* packet checksum flags */
105 	uint16_t			ipi_tso_segsz;	/* tso segment size */
106 	uint16_t			ipi_mflags;	/* packet mbuf flags */
107 	uint16_t			ipi_vtag;	/* VLAN tag */
108 	uint16_t			ipi_etype;	/* ether header type */
109 	uint8_t				ipi_ehdrlen;	/* ether header length */
110 	uint8_t				ipi_ip_hlen;	/* ip header length */
111 	uint8_t				ipi_tcp_hlen;	/* tcp header length */
112 	uint8_t				ipi_tcp_hflags;	/* tcp header flags */
113 	uint8_t				ipi_ipproto;	/* ip protocol */
114 	/* implied padding */
115 	uint32_t			ipi_tcp_seq;	/* tcp seqno */
116 	uint32_t			ipi_tcp_sum;	/* tcp csum */
117 } *if_pkt_info_t;
118 
119 typedef struct if_irq {
120 	struct resource  *ii_res;
121 	int               ii_rid;
122 	void             *ii_tag;
123 } *if_irq_t;
124 
125 struct if_int_delay_info {
126 	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
127 	int iidi_offset;			/* Register offset to read/write */
128 	int iidi_value;			/* Current value in usecs */
129 	struct sysctl_oid *iidi_oidp;
130 	struct sysctl_req *iidi_req;
131 };
132 
133 typedef enum {
134 	IFLIB_INTR_LEGACY,
135 	IFLIB_INTR_MSI,
136 	IFLIB_INTR_MSIX
137 } iflib_intr_mode_t;
138 
139 /*
140  * This really belongs in pciio.h or some place more general
141  * but this is the only consumer for now.
142  */
143 typedef struct pci_vendor_info {
144 	uint32_t	pvi_vendor_id;
145 	uint32_t	pvi_device_id;
146 	uint32_t	pvi_subvendor_id;
147 	uint32_t	pvi_subdevice_id;
148 	uint32_t	pvi_rev_id;
149 	uint32_t	pvi_class_mask;
150 	caddr_t		pvi_name;
151 } pci_vendor_info_t;
152 
153 #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
154 #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
155 #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
156 
157 typedef struct if_txrx {
158 	int (*ift_txd_encap) (void *, if_pkt_info_t);
159 	void (*ift_txd_flush) (void *, uint16_t, uint32_t);
160 	int (*ift_txd_credits_update) (void *, uint16_t, uint32_t, bool);
161 
162 	int (*ift_rxd_available) (void *, uint16_t qsidx, uint32_t pidx,
163 	    int budget);
164 	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
165 	void (*ift_rxd_refill) (void * , uint16_t qsidx, uint8_t flidx, uint32_t pidx,
166 							uint64_t *paddrs, caddr_t *vaddrs, uint16_t count, uint16_t buf_size);
167 	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, uint32_t pidx);
168 	int (*ift_legacy_intr) (void *);
169 } *if_txrx_t;
170 
171 typedef struct if_softc_ctx {
172 	int isc_vectors;
173 	int isc_nrxqsets;
174 	int isc_ntxqsets;
175 	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
176 	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
177 	int isc_ntxd[8];
178 	int isc_nrxd[8];
179 
180 	uint32_t isc_txqsizes[8];
181 	uint32_t isc_rxqsizes[8];
182 	int isc_max_txqsets;
183 	int isc_max_rxqsets;
184 	int isc_tx_tso_segments_max;
185 	int isc_tx_tso_size_max;
186 	int isc_tx_tso_segsize_max;
187 	int isc_rss_table_size;
188 	int isc_rss_table_mask;
189 	int isc_nrxqsets_max;
190 	int isc_ntxqsets_max;
191 
192 	iflib_intr_mode_t isc_intr;
193 	uint16_t isc_max_frame_size; /* set at init time by driver */
194 	pci_vendor_info_t isc_vendor_info;	/* set by iflib prior to attach_pre */
195 } *if_softc_ctx_t;
196 
197 /*
198  * Initialization values for device
199  */
200 struct if_shared_ctx {
201 	int isc_magic;
202 	if_txrx_t isc_txrx;
203 	driver_t *isc_driver;
204 	int isc_nfl;
205 	int isc_flags;
206 	bus_size_t isc_q_align;
207 	bus_size_t isc_tx_maxsize;
208 	bus_size_t isc_tx_maxsegsize;
209 	bus_size_t isc_rx_maxsize;
210 	bus_size_t isc_rx_maxsegsize;
211 	int isc_rx_nsegments;
212 	int isc_rx_process_limit;
213 	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
214 	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
215 	int isc_admin_intrcnt;		/* # of admin/link interrupts */
216 
217 
218 	int isc_tx_reclaim_thresh;
219 
220 	/* fields necessary for probe */
221 	pci_vendor_info_t *isc_vendor_info;
222 	char *isc_driver_version;
223 /* optional function to transform the read values to match the table*/
224 	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
225 				   uint16_t *subdevice_id, uint16_t *rev_id);
226 	int isc_nrxd_min[8];
227 	int isc_nrxd_default[8];
228 	int isc_nrxd_max[8];
229 	int isc_ntxd_min[8];
230 	int isc_ntxd_default[8];
231 	int isc_ntxd_max[8];
232 };
233 
234 typedef struct iflib_dma_info {
235 	bus_addr_t		idi_paddr;
236 	caddr_t			idi_vaddr;
237 	bus_dma_tag_t		idi_tag;
238 	bus_dmamap_t		idi_map;
239 	uint32_t		idi_size;
240 } *iflib_dma_info_t;
241 
242 #define IFLIB_MAGIC 0xCAFEF00D
243 
244 typedef enum {
245 	IFLIB_INTR_TX,
246 	IFLIB_INTR_RX,
247 	IFLIB_INTR_ADMIN,
248 	IFLIB_INTR_IOV,
249 } iflib_intr_type_t;
250 
251 #ifndef ETH_ADDR_LEN
252 #define ETH_ADDR_LEN 6
253 #endif
254 
255 
256 /*
257  * Interface has a separate command queue for RX
258  */
259 #define IFLIB_HAS_RXCQ		0x1
260 /*
261  * Driver has already allocated vectors
262  */
263 #define IFLIB_SKIP_MSIX		0x2
264 
265 /*
266  * Interface is a virtual function
267  */
268 #define IFLIB_IS_VF		0x4
269 /*
270  * Interface has a separate command queue for TX
271  */
272 #define IFLIB_HAS_TXCQ		0x8
273 
274 
275 /*
276  * field accessors
277  */
278 void *iflib_get_softc(if_ctx_t ctx);
279 
280 device_t iflib_get_dev(if_ctx_t ctx);
281 
282 if_t iflib_get_ifp(if_ctx_t ctx);
283 
284 struct ifmedia *iflib_get_media(if_ctx_t ctx);
285 
286 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
287 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
288 
289 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
290 
291 
292 
293 
294 /*
295  * If the driver can plug cleanly in to newbus use these
296  */
297 int iflib_device_probe(device_t);
298 int iflib_device_attach(device_t);
299 int iflib_device_detach(device_t);
300 int iflib_device_suspend(device_t);
301 int iflib_device_resume(device_t);
302 int iflib_device_shutdown(device_t);
303 
304 
305 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
306 void iflib_device_iov_uninit(device_t);
307 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
308 
309 /*
310  * If the driver can't plug cleanly in to newbus
311  * use these
312  */
313 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
314 int iflib_device_deregister(if_ctx_t);
315 
316 
317 
318 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
319 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
320 							iflib_intr_type_t type, driver_filter_t *filter,
321 							void *filter_arg, int qid, char *name);
322 void iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name);
323 
324 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
325 
326 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
327 
328 void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask,
329 			     gtask_fn_t *fn, char *name);
330 
331 void iflib_config_gtask_deinit(struct grouptask *gtask);
332 
333 
334 
335 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
336 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
337 void iflib_admin_intr_deferred(if_ctx_t ctx);
338 void iflib_iov_intr_deferred(if_ctx_t ctx);
339 
340 
341 void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
342 
343 int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
344 void iflib_dma_free(iflib_dma_info_t dma);
345 
346 int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
347 
348 void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
349 
350 
351 struct mtx *iflib_ctx_lock_get(if_ctx_t);
352 struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
353 
354 void iflib_led_create(if_ctx_t ctx);
355 
356 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
357 								if_int_delay_info_t, int, int);
358 
359 #endif /*  __IFLIB_H_ */
360