xref: /NextBSD/sys/net/iflib.h (revision 124486d7df3a1b5915e32d95fd25530d2e754322)
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 #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/bus_dma.h>
35 #include <sys/nv.h>
36 
37 struct iflib_ctx;
38 typedef struct iflib_ctx *if_ctx_t;
39 struct if_shared_ctx;
40 typedef struct if_shared_ctx *if_shared_ctx_t;
41 struct if_int_delay_info;
42 typedef struct if_int_delay_info  *if_int_delay_info_t;
43 
44 /*
45  * File organization:
46  *  - public structures
47  *  - iflib accessors
48  *  - iflib utility functions
49  *  - iflib core functions
50  */
51 
52 typedef struct if_rxd_info {
53 	uint16_t iri_qsidx;		/* qset index */
54 	uint16_t iri_vtag;		/* vlan tag - if flag set */
55 	uint16_t iri_len;		/* packet length */
56 	uint16_t iri_next_offset; /* 0 for eop */
57 	uint32_t iri_cidx;		/* consumer index of cq */
58 	uint32_t iri_flowid;	/* RSS hash for packet */
59 	int      iri_flags;		/* mbuf flags for packet */
60 	uint32_t iri_csum_flags; /* m_pkthdr csum flags */
61 	uint32_t iri_csum_data;	/* m_pkthdr csum data */
62 	struct mbuf *iri_m;		/* for driver paths that manage their own rx */
63 	struct ifnet *iri_ifp;	/* some drivers >1 interface per softc */
64 	uint8_t	 iri_rsstype; /* RSS hash type */
65 	uint8_t	 iri_pad;		/* any padding in the received data */
66 	int8_t	 iri_qidx;		/* == -1 -> completion queue event
67 							 * >=  0 -> free list id
68 							 */
69 } *if_rxd_info_t;
70 
71 typedef struct if_pkt_info {
72 	struct mbuf			*ipi_m;		/* tx packet */
73 	bus_dma_segment_t	*ipi_segs;	/* physical addresses */
74 	uint16_t			ipi_qsidx;	/* queue set index */
75 	uint16_t			ipi_nsegs;	/* number of segments */
76 	uint16_t			ipi_ndescs;	/* number of descriptors used by encap */
77 	uint32_t			ipi_pidx;	/* start pidx for encap */
78 	uint32_t			ipi_new_pidx;	/* next available pidx post-encap */
79 } *if_pkt_info_t;
80 
81 typedef struct if_irq {
82 	struct resource  *ii_res;
83 	int               ii_rid;
84 	void             *ii_tag;
85 } *if_irq_t;
86 
87 struct if_int_delay_info {
88 	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
89 	int iidi_offset;			/* Register offset to read/write */
90 	int iidi_value;			/* Current value in usecs */
91 	struct sysctl_oid *iidi_oidp;
92 	struct sysctl_req *iidi_req;
93 };
94 
95 typedef enum {
96 	IFLIB_INTR_LEGACY,
97 	IFLIB_INTR_MSI,
98 	IFLIB_INTR_MSIX
99 } iflib_intr_mode_t;
100 
101 /*
102  * This really belongs in pciio.h or some place more general
103  * but this is the only consumer for now.
104  */
105 typedef struct pci_vendor_info {
106 	uint32_t	pvi_vendor_id;
107 	uint32_t	pvi_device_id;
108 	uint32_t	pvi_subvendor_id;
109 	uint32_t	pvi_subdevice_id;
110 	uint32_t	pvi_rev_id;
111 	uint32_t	pvi_class_mask;
112 	caddr_t		pvi_name;
113 } pci_vendor_info_t;
114 
115 #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
116 #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
117 #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
118 
119 typedef struct if_txrx {
120 	int (*ift_txd_encap) (void *, if_pkt_info_t);
121 	void (*ift_txd_flush) (void *, uint16_t, uint32_t);
122 	int (*ift_txd_credits_update) (void *, uint16_t, uint32_t);
123 
124 	int (*ift_rxd_available) (void *, uint16_t qsidx, uint32_t pidx);
125 	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
126 	void (*ift_rxd_refill) (void * , uint16_t qsidx, uint8_t flidx, uint32_t pidx,
127 							uint64_t *paddrs, caddr_t *vaddrs, uint16_t count);
128 	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, uint32_t pidx);
129 	int (*ift_legacy_intr) (void *);
130 } *if_txrx_t;
131 
132 typedef struct if_softc_ctx {
133 	int isc_vectors;
134 	int isc_nqsets;
135 	int isc_msix_bar;			/* can be model specific - initialize in attach_pre */
136 	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
137 	iflib_intr_mode_t isc_intr;
138 	uint16_t isc_max_frame_size; /* set at init time by driver */
139 	pci_vendor_info_t isc_vendor_info;	/* set by iflib prior to attach_pre */
140 } *if_softc_ctx_t;
141 
142 /*
143  * Initialization values for device
144  */
145 struct if_shared_ctx {
146 	int isc_magic;
147 	if_txrx_t isc_txrx;
148 	driver_t *isc_driver;
149 	int isc_ntxd;
150 	int isc_nrxd;
151 	int isc_nfl;
152 	int isc_flags;
153 	bus_size_t isc_q_align;
154 	bus_size_t isc_tx_maxsize;
155 	bus_size_t isc_tx_maxsegsize;
156 	bus_size_t isc_rx_maxsize;
157 	bus_size_t isc_rx_maxsegsize;
158 	int isc_rx_nsegments;
159 	int isc_rx_process_limit;
160 
161 
162 	uint32_t isc_qsizes[8];
163 	int isc_nqs;
164 	int isc_admin_intrcnt;
165 
166 	int isc_tx_reclaim_thresh;
167 
168 	/* fields necessary for probe */
169 	pci_vendor_info_t *isc_vendor_info;
170 	char *isc_driver_version;
171 };
172 
173 #define IFLIB_MAGIC 0xCAFEF00D
174 
175 typedef enum {
176 	IFLIB_INTR_TX,
177 	IFLIB_INTR_RX,
178 	IFLIB_INTR_ADMIN,
179 	IFLIB_INTR_IOV,
180 } iflib_intr_type_t;
181 
182 #ifndef ETH_ADDR_LEN
183 #define ETH_ADDR_LEN 6
184 #endif
185 
186 
187 /*
188  * Interface has a separate command queue
189  */
190 #define IFLIB_HAS_CQ		0x1
191 /*
192  * Driver has already allocated vectors
193  */
194 #define IFLIB_SKIP_MSIX		0x2
195 
196 
197 /*
198  * field accessors
199  */
200 void *iflib_get_softc(if_ctx_t ctx);
201 
202 device_t iflib_get_dev(if_ctx_t ctx);
203 
204 if_t iflib_get_ifp(if_ctx_t ctx);
205 
206 struct ifmedia *iflib_get_media(if_ctx_t ctx);
207 
208 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
209 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
210 
211 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
212 
213 
214 
215 
216 /*
217  * If the driver can plug cleanly in to newbus use these
218  */
219 int iflib_device_probe(device_t);
220 int iflib_device_attach(device_t);
221 int iflib_device_detach(device_t);
222 int iflib_device_suspend(device_t);
223 int iflib_device_resume(device_t);
224 int iflib_device_shutdown(device_t);
225 
226 
227 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
228 void iflib_device_iov_uninit(device_t);
229 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
230 
231 /*
232  * If the driver can't plug cleanly in to newbus
233  * use these
234  */
235 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
236 int iflib_device_deregister(if_ctx_t);
237 
238 
239 
240 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
241 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
242 							iflib_intr_type_t type, driver_filter_t *filter,
243 							void *filter_arg, int qid, char *name);
244 void iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name);
245 
246 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
247 
248 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
249 
250 void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask,
251 			     task_fn_t *fn, char *name);
252 
253 
254 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
255 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
256 void iflib_admin_intr_deferred(if_ctx_t ctx);
257 void iflib_iov_intr_deferred(if_ctx_t ctx);
258 
259 
260 void iflib_link_state_change(if_ctx_t ctx, int linkstate);
261 
262 
263 
264 
265 
266 struct mtx *iflib_ctx_lock_get(if_ctx_t);
267 struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
268 
269 void iflib_led_create(if_ctx_t ctx);
270 
271 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
272 								if_int_delay_info_t, int, int);
273 
274 #endif /*  __IFLIB_H_ */
275