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 <machine/bus.h>
35#include <sys/nv.h>
36#include <sys/gtaskqueue.h>
37
38struct if_clone;
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
48struct iflib_ctx;
49typedef struct iflib_ctx *if_ctx_t;
50struct if_shared_ctx;
51typedef const struct if_shared_ctx *if_shared_ctx_t;
52struct if_int_delay_info;
53typedef struct if_int_delay_info  *if_int_delay_info_t;
54struct if_pseudo;
55typedef struct if_pseudo *if_pseudo_t;
56
57/*
58 * File organization:
59 *  - public structures
60 *  - iflib accessors
61 *  - iflib utility functions
62 *  - iflib core functions
63 */
64
65typedef struct if_rxd_frag {
66	uint8_t irf_flid;
67	qidx_t irf_idx;
68	uint16_t irf_len;
69} *if_rxd_frag_t;
70
71/* bnxt supports 64 with hardware LRO enabled */
72#define IFLIB_MAX_RX_SEGS		64
73
74typedef struct if_rxd_info {
75	/* set by iflib */
76	uint16_t iri_qsidx;		/* qset index */
77	uint16_t iri_vtag;		/* vlan tag - if flag set */
78	/* XXX redundant with the new irf_len field */
79	uint16_t iri_len;		/* packet length */
80	qidx_t iri_cidx;		/* consumer index of cq */
81	if_t iri_ifp;			/* driver may have >1 iface per softc */
82
83	/* updated by driver */
84	if_rxd_frag_t iri_frags;
85	uint32_t iri_flowid;		/* RSS hash for packet */
86	uint32_t iri_csum_flags;	/* m_pkthdr csum flags */
87
88	uint32_t iri_csum_data;		/* m_pkthdr csum data */
89	uint8_t iri_flags;		/* mbuf flags for packet */
90	uint8_t	 iri_nfrags;		/* number of fragments in packet */
91	uint8_t	 iri_rsstype;		/* RSS hash type */
92	uint8_t	 iri_pad;		/* any padding in the received data */
93} *if_rxd_info_t;
94
95typedef struct if_rxd_update {
96	uint64_t	*iru_paddrs;
97	qidx_t		*iru_idxs;
98	qidx_t		iru_pidx;
99	uint16_t	iru_qsidx;
100	uint16_t	iru_count;
101	uint16_t	iru_buf_size;
102	uint8_t		iru_flidx;
103} *if_rxd_update_t;
104
105#define IPI_TX_INTR	0x1		/* send an interrupt when this packet is sent */
106#define IPI_TX_IPV4	0x2		/* ethertype IPv4 */
107#define IPI_TX_IPV6	0x4		/* ethertype IPv6 */
108
109typedef struct if_pkt_info {
110	bus_dma_segment_t	*ipi_segs;	/* physical addresses */
111	uint32_t		ipi_len;	/* packet length */
112	uint16_t		ipi_qsidx;	/* queue set index */
113	qidx_t			ipi_nsegs;	/* number of segments */
114
115	qidx_t			ipi_ndescs;	/* number of descriptors used by encap */
116	uint16_t		ipi_flags;	/* iflib per-packet flags */
117	qidx_t			ipi_pidx;	/* start pidx for encap */
118	qidx_t			ipi_new_pidx;	/* next available pidx post-encap */
119	/* offload handling */
120	uint8_t			ipi_ehdrlen;	/* ether header length */
121	uint8_t			ipi_ip_hlen;	/* ip header length */
122	uint8_t			ipi_tcp_hlen;	/* tcp header length */
123	uint8_t			ipi_ipproto;	/* ip protocol */
124
125	uint32_t		ipi_csum_flags;	/* packet checksum flags */
126	uint16_t		ipi_tso_segsz;	/* tso segment size */
127	uint16_t		ipi_vtag;	/* VLAN tag */
128	uint16_t		ipi_etype;	/* ether header type */
129	uint8_t			ipi_tcp_hflags;	/* tcp header flags */
130	uint8_t			ipi_mflags;	/* packet mbuf flags */
131
132	uint32_t		ipi_tcp_seq;	/* tcp seqno */
133	uint32_t		__spare0__;
134} *if_pkt_info_t;
135
136typedef struct if_irq {
137	struct resource  *ii_res;
138	int               __spare0__;
139	void             *ii_tag;
140} *if_irq_t;
141
142struct if_int_delay_info {
143	if_ctx_t iidi_ctx;	/* Back-pointer to the iflib ctx (softc) */
144	int iidi_offset;			/* Register offset to read/write */
145	int iidi_value;			/* Current value in usecs */
146	struct sysctl_oid *iidi_oidp;
147	struct sysctl_req *iidi_req;
148};
149
150typedef enum {
151	IFLIB_INTR_LEGACY,
152	IFLIB_INTR_MSI,
153	IFLIB_INTR_MSIX
154} iflib_intr_mode_t;
155
156/*
157 * This really belongs in pciio.h or some place more general
158 * but this is the only consumer for now.
159 */
160typedef struct pci_vendor_info {
161	uint32_t	pvi_vendor_id;
162	uint32_t	pvi_device_id;
163	uint32_t	pvi_subvendor_id;
164	uint32_t	pvi_subdevice_id;
165	uint32_t	pvi_rev_id;
166	uint32_t	pvi_class_mask;
167	const char	*pvi_name;
168} pci_vendor_info_t;
169#define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
170#define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
171#define PVID_END {0, 0, 0, 0, 0, 0, NULL}
172
173/* No drivers in tree currently match on anything except vendor:device. */
174#define IFLIB_PNP_DESCR "U32:vendor;U32:device;U32:#;U32:#;" \
175    "U32:#;U32:#;D:#"
176#define IFLIB_PNP_INFO(b, u, t) \
177    MODULE_PNP_INFO(IFLIB_PNP_DESCR, b, u, t, nitems(t) - 1)
178
179typedef struct if_txrx {
180	int (*ift_txd_encap) (void *, if_pkt_info_t);
181	void (*ift_txd_flush) (void *, uint16_t, qidx_t pidx);
182	int (*ift_txd_credits_update) (void *, uint16_t qsidx, bool clear);
183
184	int (*ift_rxd_available) (void *, uint16_t qsidx, qidx_t pidx, qidx_t budget);
185	int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
186	void (*ift_rxd_refill) (void * , if_rxd_update_t iru);
187	void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, qidx_t pidx);
188	int (*ift_legacy_intr) (void *);
189} *if_txrx_t;
190
191typedef struct if_softc_ctx {
192	int isc_vectors;
193	int isc_nrxqsets;
194	int isc_ntxqsets;
195	uint16_t __spare0__;
196	uint32_t __spare1__;
197	int isc_msix_bar;		/* can be model specific - initialize in attach_pre */
198	int isc_tx_nsegments;		/* can be model specific - initialize in attach_pre */
199	int isc_ntxd[8];
200	int isc_nrxd[8];
201
202	uint32_t isc_txqsizes[8];
203	uint32_t isc_rxqsizes[8];
204	/* is there such thing as a descriptor that is more than 248 bytes ? */
205	uint8_t isc_txd_size[8];
206	uint8_t isc_rxd_size[8];
207
208	int isc_tx_tso_segments_max;
209	int isc_tx_tso_size_max;
210	int isc_tx_tso_segsize_max;
211	int isc_tx_csum_flags;
212	int isc_capabilities;
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	uint32_t __spare2__;
219
220	iflib_intr_mode_t isc_intr;
221	uint16_t isc_rxd_buf_size[8]; /* set at init time by driver, 0
222				         means use iflib-calculated size
223				         based on isc_max_frame_size */
224	uint16_t isc_max_frame_size; /* set at init time by driver */
225	uint16_t isc_min_frame_size; /* set at init time by driver, only used if
226					IFLIB_NEED_ETHER_PAD is set. */
227	uint32_t isc_pause_frames;   /* set by driver for iflib_timer to detect */
228	uint32_t __spare3__;
229	uint32_t __spare4__;
230	uint32_t __spare5__;
231	uint32_t __spare6__;
232	uint32_t __spare7__;
233	uint32_t __spare8__;
234	caddr_t __spare9__;
235	int isc_disable_msix;
236	if_txrx_t isc_txrx;
237	struct ifmedia *isc_media;
238} *if_softc_ctx_t;
239
240/*
241 * Initialization values for device
242 */
243struct if_shared_ctx {
244	unsigned isc_magic;
245	driver_t *isc_driver;
246	bus_size_t isc_q_align;
247	bus_size_t isc_tx_maxsize;
248	bus_size_t isc_tx_maxsegsize;
249	bus_size_t isc_tso_maxsize;
250	bus_size_t isc_tso_maxsegsize;
251	bus_size_t isc_rx_maxsize;
252	bus_size_t isc_rx_maxsegsize;
253	int isc_rx_nsegments;
254	int isc_admin_intrcnt;		/* # of admin/link interrupts */
255
256	/* fields necessary for probe */
257	const pci_vendor_info_t *isc_vendor_info;
258	const char *isc_driver_version;
259	/* optional function to transform the read values to match the table*/
260	void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
261				   uint16_t *subdevice_id, uint16_t *rev_id);
262	int isc_nrxd_min[8];
263	int isc_nrxd_default[8];
264	int isc_nrxd_max[8];
265	int isc_ntxd_min[8];
266	int isc_ntxd_default[8];
267	int isc_ntxd_max[8];
268
269	/* actively used during operation */
270	int isc_nfl __aligned(CACHE_LINE_SIZE);
271	int isc_ntxqs;			/* # of tx queues per tx qset - usually 1 */
272	int isc_nrxqs;			/* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
273	int __spare0__;
274	int isc_tx_reclaim_thresh;
275	int isc_flags;
276	const char *isc_name;
277};
278
279typedef struct iflib_dma_info {
280	bus_addr_t		idi_paddr;
281	caddr_t			idi_vaddr;
282	bus_dma_tag_t		idi_tag;
283	bus_dmamap_t		idi_map;
284	uint32_t		idi_size;
285} *iflib_dma_info_t;
286
287#define IFLIB_MAGIC 0xCAFEF00D
288
289typedef enum {
290	/* Interrupt or softirq handles only receive */
291	IFLIB_INTR_RX,
292
293	/* Interrupt or softirq handles only transmit */
294	IFLIB_INTR_TX,
295
296	/*
297	 * Interrupt will check for both pending receive
298	 * and available tx credits and dispatch a task
299	 * for one or both depending on the disposition
300	 * of the respective queues.
301	 */
302	IFLIB_INTR_RXTX,
303
304	/*
305	 * Other interrupt - typically link status and
306	 * or error conditions.
307	 */
308	IFLIB_INTR_ADMIN,
309
310	/* Softirq (task) for iov handling */
311	IFLIB_INTR_IOV,
312} iflib_intr_type_t;
313
314/*
315 * Interface has a separate completion queue for RX
316 */
317#define IFLIB_HAS_RXCQ		0x01
318/*
319 * Driver has already allocated vectors
320 */
321#define IFLIB_SKIP_MSIX		0x02
322/*
323 * Interface is a virtual function
324 */
325#define IFLIB_IS_VF		0x04
326/*
327 * Interface has a separate completion queue for TX
328 */
329#define IFLIB_HAS_TXCQ		0x08
330/*
331 * Interface does checksum in place
332 */
333#define IFLIB_NEED_SCRATCH	0x10
334/*
335 * Interface doesn't expect in_pseudo for th_sum
336 */
337#define IFLIB_TSO_INIT_IP	0x20
338/*
339 * Interface doesn't align IP header
340 */
341#define IFLIB_DO_RX_FIXUP	0x40
342/*
343 * Driver needs csum zeroed for offloading
344 */
345#define IFLIB_NEED_ZERO_CSUM	0x80
346/*
347 * Driver needs frames padded to some minimum length
348 */
349#define IFLIB_NEED_ETHER_PAD	0x100
350/*
351 * Packets can be freed immediately after encap
352 */
353#define IFLIB_TXD_ENCAP_PIO	0x00200
354/*
355 * Use RX completion handler
356 */
357#define IFLIB_RX_COMPLETION	0x00400
358/*
359 * Skip refilling cluster free lists
360 */
361#define IFLIB_SKIP_CLREFILL	0x00800
362/*
363 * Don't reset on hang
364 */
365#define IFLIB_NO_HANG_RESET	0x01000
366/*
367 * Don't need/want most of the niceties of
368 * queue management
369 */
370#define IFLIB_PSEUDO	0x02000
371/*
372 * No DMA support needed / wanted
373 */
374#define IFLIB_VIRTUAL	0x04000
375/*
376 * autogenerate a MAC address
377 */
378#define IFLIB_GEN_MAC	0x08000
379/*
380 * Interface needs admin task to ignore interface up/down status
381 */
382#define IFLIB_ADMIN_ALWAYS_RUN	0x10000
383/*
384 * Driver will pass the media
385 */
386#define IFLIB_DRIVER_MEDIA	0x20000
387/*
388 * When using a single hardware interrupt for the interface, only process RX
389 * interrupts instead of doing combined RX/TX processing.
390 */
391#define	IFLIB_SINGLE_IRQ_RX_ONLY	0x40000
392/*
393 * Don't need/want most of the niceties of
394 * emulating ethernet
395 */
396#define IFLIB_PSEUDO_ETHER	0x80000
397
398/*
399 * These enum values are used in iflib_needs_restart to indicate to iflib
400 * functions whether or not the interface needs restarting when certain events
401 * happen.
402 */
403enum iflib_restart_event {
404	IFLIB_RESTART_VLAN_CONFIG,
405};
406
407/*
408 * field accessors
409 */
410void *iflib_get_softc(if_ctx_t ctx);
411
412device_t iflib_get_dev(if_ctx_t ctx);
413
414if_t iflib_get_ifp(if_ctx_t ctx);
415
416struct ifmedia *iflib_get_media(if_ctx_t ctx);
417
418if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
419if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
420
421void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
422void iflib_request_reset(if_ctx_t ctx);
423uint8_t iflib_in_detach(if_ctx_t ctx);
424
425uint32_t iflib_get_rx_mbuf_sz(if_ctx_t ctx);
426
427/*
428 * If the driver can plug cleanly in to newbus use these
429 */
430int iflib_device_probe(device_t);
431int iflib_device_attach(device_t);
432int iflib_device_detach(device_t);
433int iflib_device_suspend(device_t);
434int iflib_device_resume(device_t);
435int iflib_device_shutdown(device_t);
436
437/*
438 * Use this instead of iflib_device_probe if the driver should report
439 * BUS_PROBE_VENDOR instead of BUS_PROBE_DEFAULT. (For example, an out-of-tree
440 * driver based on iflib).
441 */
442int iflib_device_probe_vendor(device_t);
443
444int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
445void iflib_device_iov_uninit(device_t);
446int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
447
448/*
449 * If the driver can't plug cleanly in to newbus
450 * use these
451 */
452int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
453int iflib_device_deregister(if_ctx_t);
454
455int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, const char *name);
456int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
457			    iflib_intr_type_t type, driver_filter_t *filter,
458			    void *filter_arg, int qid, const char *name);
459void iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,  void *arg, int qid, const char *name);
460
461void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
462
463void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu,
464    const char *name);
465
466void iflib_config_gtask_init(void *ctx, struct grouptask *gtask,
467			     gtask_fn_t *fn, const char *name);
468void iflib_config_gtask_deinit(struct grouptask *gtask);
469
470void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
471void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
472void iflib_admin_intr_deferred(if_ctx_t ctx);
473void iflib_iov_intr_deferred(if_ctx_t ctx);
474
475void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
476
477int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
478int iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags);
479void iflib_dma_free(iflib_dma_info_t dma);
480int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
481
482void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
483
484struct sx *iflib_ctx_lock_get(if_ctx_t);
485
486void iflib_led_create(if_ctx_t ctx);
487
488void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
489								if_int_delay_info_t, int, int);
490
491/*
492 * Pseudo device support
493 */
494if_pseudo_t iflib_clone_register(if_shared_ctx_t);
495void iflib_clone_deregister(if_pseudo_t);
496
497#endif /*  __IFLIB_H_ */
498