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: stable/11/sys/net/iflib.h 361055 2020-05-14 20:07:02Z erj $
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 * The value type for indexing, limits max descriptors
42 * to 65535 can be conditionally redefined to uint32_t
43 * in the future if the need arises.
44 */
45typedef uint16_t qidx_t;
46#define QIDX_INVALID 0xFFFF
47/*
48 * Most cards can handle much larger TSO requests
49 * but the FreeBSD TCP stack will break on larger
50 * values
51 */
52#define FREEBSD_TSO_SIZE_MAX 65518
53
54
55struct iflib_ctx;
56typedef struct iflib_ctx *if_ctx_t;
57struct if_shared_ctx;
58typedef struct if_shared_ctx *if_shared_ctx_t;
59struct if_int_delay_info;
60typedef struct if_int_delay_info  *if_int_delay_info_t;
61
62/*
63 * File organization:
64 *  - public structures
65 *  - iflib accessors
66 *  - iflib utility functions
67 *  - iflib core functions
68 */
69
70typedef struct if_rxd_frag {
71	uint8_t irf_flid;
72	qidx_t irf_idx;
73	uint16_t irf_len;
74} *if_rxd_frag_t;
75
76typedef 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	struct ifnet *iri_ifp;		/* some drivers >1 interface per softc */
84
85	/* updated by driver */
86	if_rxd_frag_t iri_frags;
87	uint32_t iri_flowid;		/* RSS hash for packet */
88	uint32_t iri_csum_flags;	/* m_pkthdr csum flags */
89
90	uint32_t iri_csum_data;		/* m_pkthdr csum data */
91	uint8_t iri_flags;		/* mbuf flags for packet */
92	uint8_t	 iri_nfrags;		/* number of fragments in packet */
93	uint8_t	 iri_rsstype;		/* RSS hash type */
94	uint8_t	 iri_pad;		/* any padding in the received data */
95} *if_rxd_info_t;
96
97typedef struct if_rxd_update {
98	uint64_t	*iru_paddrs;
99	caddr_t		*iru_vaddrs;
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
112typedef 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	uint8_t			ipi_tcp_hflags;	/* tcp header flags */
133	uint8_t			ipi_mflags;	/* packet mbuf flags */
134
135	uint32_t		ipi_tcp_seq;	/* tcp seqno */
136	uint32_t		ipi_tcp_sum;	/* tcp csum */
137} *if_pkt_info_t;
138
139typedef struct if_irq {
140	struct resource  *ii_res;
141	int               ii_rid;
142	void             *ii_tag;
143} *if_irq_t;
144
145struct if_int_delay_info {
146	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
147	int iidi_offset;			/* Register offset to read/write */
148	int iidi_value;			/* Current value in usecs */
149	struct sysctl_oid *iidi_oidp;
150	struct sysctl_req *iidi_req;
151};
152
153typedef enum {
154	IFLIB_INTR_LEGACY,
155	IFLIB_INTR_MSI,
156	IFLIB_INTR_MSIX
157} iflib_intr_mode_t;
158
159/*
160 * This really belongs in pciio.h or some place more general
161 * but this is the only consumer for now.
162 */
163typedef struct pci_vendor_info {
164	uint32_t	pvi_vendor_id;
165	uint32_t	pvi_device_id;
166	uint32_t	pvi_subvendor_id;
167	uint32_t	pvi_subdevice_id;
168	uint32_t	pvi_rev_id;
169	uint32_t	pvi_class_mask;
170	caddr_t		pvi_name;
171} pci_vendor_info_t;
172
173#define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
174#define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
175#define PVID_END {0, 0, 0, 0, 0, 0, NULL}
176
177#define IFLIB_PNP_DESCR "U32:vendor;U32:device;U32:subvendor;U32:subdevice;" \
178    "U32:revision;U32:class;D:human"
179#define IFLIB_PNP_INFO(b, u, t) \
180    MODULE_PNP_INFO(IFLIB_PNP_DESCR, b, u, t, sizeof(t[0]), nitems(t) - 1)
181
182typedef struct if_txrx {
183	int (*ift_txd_encap) (void *, if_pkt_info_t);
184	void (*ift_txd_flush) (void *, uint16_t, qidx_t pidx);
185	int (*ift_txd_credits_update) (void *, uint16_t qsidx, bool clear);
186
187	int (*ift_rxd_available) (void *, uint16_t qsidx, qidx_t pidx, qidx_t budget);
188	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
189	void (*ift_rxd_refill) (void * , if_rxd_update_t iru);
190	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, qidx_t pidx);
191	int (*ift_legacy_intr) (void *);
192} *if_txrx_t;
193
194typedef struct if_softc_ctx {
195	int isc_vectors;
196	int isc_nrxqsets;
197	int isc_ntxqsets;
198	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
199	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
200	int isc_ntxd[8];
201	int isc_nrxd[8];
202
203	uint32_t isc_txqsizes[8];
204	uint32_t isc_rxqsizes[8];
205	/* is there such thing as a descriptor that is more than 248 bytes ? */
206	uint8_t isc_txd_size[8];
207	uint8_t isc_rxd_size[8];
208
209	int isc_tx_tso_segments_max;
210	int isc_tx_tso_size_max;
211	int isc_tx_tso_segsize_max;
212	int isc_tx_csum_flags;
213	int isc_capenable;
214	int isc_rss_table_size;
215	int isc_rss_table_mask;
216	int isc_nrxqsets_max;
217	int isc_ntxqsets_max;
218
219	iflib_intr_mode_t isc_intr;
220	uint16_t isc_max_frame_size; /* set at init time by driver */
221	uint16_t isc_min_frame_size; /* set at init time by driver, only used if
222					IFLIB_NEED_ETHER_PAD is set. */
223	uint32_t isc_pause_frames;   /* set by driver for iflib_timer to detect */
224	pci_vendor_info_t isc_vendor_info;	/* set by iflib prior to attach_pre */
225	int isc_disable_msix;
226	if_txrx_t isc_txrx;
227} *if_softc_ctx_t;
228
229/*
230 * Initialization values for device
231 */
232struct if_shared_ctx {
233	unsigned isc_magic;
234	driver_t *isc_driver;
235	bus_size_t isc_q_align;
236	bus_size_t isc_tx_maxsize;
237	bus_size_t isc_tx_maxsegsize;
238	bus_size_t isc_rx_maxsize;
239	bus_size_t isc_rx_maxsegsize;
240	int isc_rx_nsegments;
241	int isc_admin_intrcnt;		/* # of admin/link interrupts */
242
243	/* fields necessary for probe */
244	pci_vendor_info_t *isc_vendor_info;
245	const char *isc_driver_version;
246	/* optional function to transform the read values to match the table*/
247	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
248				   uint16_t *subdevice_id, uint16_t *rev_id);
249	int isc_nrxd_min[8];
250	int isc_nrxd_default[8];
251	int isc_nrxd_max[8];
252	int isc_ntxd_min[8];
253	int isc_ntxd_default[8];
254	int isc_ntxd_max[8];
255
256	/* actively used during operation */
257	int isc_nfl __aligned(CACHE_LINE_SIZE);
258	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
259	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
260	int isc_rx_process_limit;
261	int isc_tx_reclaim_thresh;
262	int isc_flags;
263};
264
265typedef struct iflib_dma_info {
266	bus_addr_t		idi_paddr;
267	caddr_t			idi_vaddr;
268	bus_dma_tag_t		idi_tag;
269	bus_dmamap_t		idi_map;
270	uint32_t		idi_size;
271} *iflib_dma_info_t;
272
273#define IFLIB_MAGIC 0xCAFEF00D
274
275typedef enum {
276	IFLIB_INTR_RX,
277	IFLIB_INTR_TX,
278	IFLIB_INTR_RXTX,
279	IFLIB_INTR_ADMIN,
280	IFLIB_INTR_IOV,
281} iflib_intr_type_t;
282
283#ifndef ETH_ADDR_LEN
284#define ETH_ADDR_LEN 6
285#endif
286
287
288/*
289 * Interface has a separate command queue for RX
290 */
291#define IFLIB_HAS_RXCQ		0x01
292/*
293 * Driver has already allocated vectors
294 */
295#define IFLIB_SKIP_MSIX		0x02
296/*
297 * Interface is a virtual function
298 */
299#define IFLIB_IS_VF		0x04
300/*
301 * Interface has a separate command queue for TX
302 */
303#define IFLIB_HAS_TXCQ		0x08
304/*
305 * Interface does checksum in place
306 */
307#define IFLIB_NEED_SCRATCH	0x10
308/*
309 * Interface doesn't expect in_pseudo for th_sum
310 */
311#define IFLIB_TSO_INIT_IP	0x20
312/*
313 * Interface doesn't align IP header
314 */
315#define IFLIB_DO_RX_FIXUP	0x40
316/*
317 * Driver needs csum zeroed for offloading
318 */
319#define IFLIB_NEED_ZERO_CSUM	0x80
320/*
321 * Driver needs frames padded to some minimum length
322 */
323#define IFLIB_NEED_ETHER_PAD	0x100
324/*
325 * Interface needs admin task to ignore interface up/down status
326 */
327#define IFLIB_ADMIN_ALWAYS_RUN	0x10000
328
329
330/*
331 * These enum values are used in iflib_needs_restart to indicate to iflib
332 * functions whether or not the interface needs restarting when certain events
333 * happen.
334 */
335enum iflib_restart_event {
336	IFLIB_RESTART_VLAN_CONFIG,
337};
338
339/*
340 * field accessors
341 */
342void *iflib_get_softc(if_ctx_t ctx);
343
344device_t iflib_get_dev(if_ctx_t ctx);
345
346if_t iflib_get_ifp(if_ctx_t ctx);
347
348struct ifmedia *iflib_get_media(if_ctx_t ctx);
349
350if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
351if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
352
353void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
354void iflib_request_reset(if_ctx_t ctx);
355uint8_t iflib_in_detach(if_ctx_t ctx);
356
357uint32_t iflib_get_rx_mbuf_sz(if_ctx_t ctx);
358
359/*
360 * If the driver can plug cleanly in to newbus use these
361 */
362int iflib_device_probe(device_t);
363int iflib_device_attach(device_t);
364int iflib_device_detach(device_t);
365int iflib_device_suspend(device_t);
366int iflib_device_resume(device_t);
367int iflib_device_shutdown(device_t);
368
369
370int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
371void iflib_device_iov_uninit(device_t);
372int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
373
374/*
375 * If the driver can't plug cleanly in to newbus
376 * use these
377 */
378int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
379int iflib_device_deregister(if_ctx_t);
380
381
382
383int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
384int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
385							iflib_intr_type_t type, driver_filter_t *filter,
386							void *filter_arg, int qid, char *name);
387void iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,  void *arg, int qid, char *name);
388
389void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
390
391void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
392
393void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask,
394			     gtask_fn_t *fn, char *name);
395
396void iflib_config_gtask_deinit(struct grouptask *gtask);
397
398
399
400void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
401void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
402void iflib_admin_intr_deferred(if_ctx_t ctx);
403void iflib_iov_intr_deferred(if_ctx_t ctx);
404
405
406void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
407
408int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
409void iflib_dma_free(iflib_dma_info_t dma);
410
411int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
412
413void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
414
415
416struct mtx *iflib_ctx_lock_get(if_ctx_t);
417struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
418
419void iflib_led_create(if_ctx_t ctx);
420
421void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
422								if_int_delay_info_t, int, int);
423
424#endif /*  __IFLIB_H_ */
425