2 * Copyright (c) 2014-2015, Matthew Macy (mmacy@nextbsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
34 #include <sys/cpuset.h>
35 #include <machine/bus.h>
36 #include <sys/bus_dma.h>
41 * Most cards can handle much larger TSO requests
42 * but the FreeBSD TCP stack will break on larger
45 #define FREEBSD_TSO_SIZE_MAX 65518
49 typedef struct iflib_ctx *if_ctx_t;
51 typedef struct if_shared_ctx *if_shared_ctx_t;
52 struct if_int_delay_info;
53 typedef struct if_int_delay_info *if_int_delay_info_t;
59 * - iflib utility functions
60 * - iflib core functions
63 typedef struct if_rxd_frag {
68 typedef struct if_rxd_info {
70 uint16_t iri_qsidx; /* qset index */
71 uint16_t iri_vtag; /* vlan tag - if flag set */
72 uint16_t iri_len; /* packet length */
73 uint16_t iri_cidx; /* consumer index of cq */
74 struct ifnet *iri_ifp; /* some drivers >1 interface per softc */
76 /* updated by driver */
77 uint16_t iri_flags; /* mbuf flags for packet */
78 uint32_t iri_flowid; /* RSS hash for packet */
79 uint32_t iri_csum_flags; /* m_pkthdr csum flags */
80 uint32_t iri_csum_data; /* m_pkthdr csum data */
81 uint8_t iri_nfrags; /* number of fragments in packet */
82 uint8_t iri_rsstype; /* RSS hash type */
83 uint8_t iri_pad; /* any padding in the received data */
84 if_rxd_frag_t iri_frags;
87 #define IPI_TX_INTR 0x1 /* send an interrupt when this packet is sent */
88 #define IPI_TX_IPV4 0x2 /* ethertype IPv4 */
89 #define IPI_TX_IPV6 0x4 /* ethertype IPv6 */
91 typedef struct if_pkt_info {
92 uint32_t ipi_len; /* packet length */
93 bus_dma_segment_t *ipi_segs; /* physical addresses */
94 uint16_t ipi_qsidx; /* queue set index */
95 uint16_t ipi_nsegs; /* number of segments */
96 uint16_t ipi_ndescs; /* number of descriptors used by encap */
97 uint16_t ipi_flags; /* iflib per-packet flags */
98 uint32_t ipi_pidx; /* start pidx for encap */
99 uint32_t ipi_new_pidx; /* next available pidx post-encap */
100 /* offload handling */
101 uint64_t ipi_csum_flags; /* packet checksum flags */
102 uint16_t ipi_tso_segsz; /* tso segment size */
103 uint16_t ipi_mflags; /* packet mbuf flags */
104 uint16_t ipi_vtag; /* VLAN tag */
105 uint16_t ipi_etype; /* ether header type */
106 uint8_t ipi_ehdrlen; /* ether header length */
107 uint8_t ipi_ip_hlen; /* ip header length */
108 uint8_t ipi_tcp_hlen; /* tcp header length */
109 uint8_t ipi_tcp_hflags; /* tcp header flags */
110 uint8_t ipi_ipproto; /* ip protocol */
111 /* implied padding */
112 uint32_t ipi_tcp_seq; /* tcp seqno */
113 uint32_t ipi_tcp_sum; /* tcp csum */
116 typedef struct if_irq {
117 struct resource *ii_res;
122 struct if_int_delay_info {
123 if_ctx_t iidi_ctx; /* Back-pointer to the iflib ctx (softc) */
124 int iidi_offset; /* Register offset to read/write */
125 int iidi_value; /* Current value in usecs */
126 struct sysctl_oid *iidi_oidp;
127 struct sysctl_req *iidi_req;
137 * This really belongs in pciio.h or some place more general
138 * but this is the only consumer for now.
140 typedef struct pci_vendor_info {
141 uint32_t pvi_vendor_id;
142 uint32_t pvi_device_id;
143 uint32_t pvi_subvendor_id;
144 uint32_t pvi_subdevice_id;
146 uint32_t pvi_class_mask;
150 #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
151 #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
152 #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
154 typedef struct if_txrx {
155 int (*ift_txd_encap) (void *, if_pkt_info_t);
156 void (*ift_txd_flush) (void *, uint16_t, uint32_t);
157 int (*ift_txd_credits_update) (void *, uint16_t, uint32_t, bool);
159 int (*ift_rxd_available) (void *, uint16_t qsidx, uint32_t pidx);
160 int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
161 void (*ift_rxd_refill) (void * , uint16_t qsidx, uint8_t flidx, uint32_t pidx,
162 uint64_t *paddrs, caddr_t *vaddrs, uint16_t count);
163 void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, uint32_t pidx);
164 int (*ift_legacy_intr) (void *);
167 typedef struct if_softc_ctx {
171 int isc_msix_bar; /* can be model specific - initialize in attach_pre */
172 int isc_tx_nsegments; /* can be model specific - initialize in attach_pre */
173 int isc_tx_tso_segments_max;
174 int isc_tx_tso_size_max;
175 int isc_tx_tso_segsize_max;
176 int isc_rss_table_size;
177 int isc_rss_table_mask;
179 iflib_intr_mode_t isc_intr;
180 uint16_t isc_max_frame_size; /* set at init time by driver */
181 pci_vendor_info_t isc_vendor_info; /* set by iflib prior to attach_pre */
185 * Initialization values for device
187 struct if_shared_ctx {
190 driver_t *isc_driver;
195 bus_size_t isc_q_align;
196 bus_size_t isc_tx_maxsize;
197 bus_size_t isc_tx_maxsegsize;
198 bus_size_t isc_rx_maxsize;
199 bus_size_t isc_rx_maxsegsize;
200 int isc_rx_nsegments;
201 int isc_rx_process_limit;
204 uint32_t isc_txqsizes[8];
205 int isc_ntxqs; /* # of tx queues per tx qset - usually 1 */
206 uint32_t isc_rxqsizes[8];
207 int isc_nrxqs; /* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
208 int isc_admin_intrcnt; /* # of admin/link interrupts */
210 int isc_tx_reclaim_thresh;
212 /* fields necessary for probe */
213 pci_vendor_info_t *isc_vendor_info;
214 char *isc_driver_version;
215 /* optional function to transform the read values to match the table*/
216 void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
217 uint16_t *subdevice_id, uint16_t *rev_id);
220 typedef struct iflib_dma_info {
221 bus_addr_t idi_paddr;
223 bus_dma_tag_t idi_tag;
224 bus_dmamap_t idi_map;
228 #define IFLIB_MAGIC 0xCAFEF00D
238 #define ETH_ADDR_LEN 6
243 * Interface has a separate command queue
245 #define IFLIB_HAS_CQ 0x1
247 * Driver has already allocated vectors
249 #define IFLIB_SKIP_MSIX 0x2
252 * Interface is a virtual function
254 #define IFLIB_IS_VF 0x4
260 void *iflib_get_softc(if_ctx_t ctx);
262 device_t iflib_get_dev(if_ctx_t ctx);
264 if_t iflib_get_ifp(if_ctx_t ctx);
266 struct ifmedia *iflib_get_media(if_ctx_t ctx);
268 if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
269 if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
271 void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
277 * If the driver can plug cleanly in to newbus use these
279 int iflib_device_probe(device_t);
280 int iflib_device_attach(device_t);
281 int iflib_device_detach(device_t);
282 int iflib_device_suspend(device_t);
283 int iflib_device_resume(device_t);
284 int iflib_device_shutdown(device_t);
287 int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
288 void iflib_device_iov_uninit(device_t);
289 int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
292 * If the driver can't plug cleanly in to newbus
295 int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
296 int iflib_device_deregister(if_ctx_t);
300 int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name);
301 int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
302 iflib_intr_type_t type, driver_filter_t *filter,
303 void *filter_arg, int qid, char *name);
304 void iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type, void *arg, int qid, char *name);
306 void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
308 void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name);
310 void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask,
311 task_fn_t *fn, char *name);
314 void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
315 void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
316 void iflib_admin_intr_deferred(if_ctx_t ctx);
317 void iflib_iov_intr_deferred(if_ctx_t ctx);
320 void iflib_link_state_change(if_ctx_t ctx, int linkstate);
322 int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
323 void iflib_dma_free(iflib_dma_info_t dma);
325 int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
327 void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
330 struct mtx *iflib_ctx_lock_get(if_ctx_t);
331 struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t);
333 void iflib_led_create(if_ctx_t ctx);
335 void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
336 if_int_delay_info_t, int, int);
338 #endif /* __IFLIB_H_ */