14c7070dbSScott Long /*- 24c7070dbSScott Long * Copyright (c) 2014-2015, Matthew Macy (mmacy@nextbsd.org) 34c7070dbSScott Long * All rights reserved. 44c7070dbSScott Long * 54c7070dbSScott Long * Redistribution and use in source and binary forms, with or without 64c7070dbSScott Long * modification, are permitted provided that the following conditions are met: 74c7070dbSScott Long * 84c7070dbSScott Long * 1. Redistributions of source code must retain the above copyright notice, 94c7070dbSScott Long * this list of conditions and the following disclaimer. 104c7070dbSScott Long * 114c7070dbSScott Long * 2. Neither the name of Matthew Macy nor the names of its 124c7070dbSScott Long * contributors may be used to endorse or promote products derived from 134c7070dbSScott Long * this software without specific prior written permission. 144c7070dbSScott Long * 154c7070dbSScott Long * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 164c7070dbSScott Long * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 174c7070dbSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 184c7070dbSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 194c7070dbSScott Long * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 204c7070dbSScott Long * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 214c7070dbSScott Long * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 224c7070dbSScott Long * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 234c7070dbSScott Long * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 244c7070dbSScott Long * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 254c7070dbSScott Long * POSSIBILITY OF SUCH DAMAGE. 264c7070dbSScott Long * 274c7070dbSScott Long * $FreeBSD$ 284c7070dbSScott Long */ 294c7070dbSScott Long #ifndef __IFLIB_H_ 304c7070dbSScott Long #define __IFLIB_H_ 314c7070dbSScott Long 324c7070dbSScott Long #include <sys/kobj.h> 334c7070dbSScott Long #include <sys/bus.h> 344c7070dbSScott Long #include <sys/cpuset.h> 354c7070dbSScott Long #include <machine/bus.h> 364c7070dbSScott Long #include <sys/bus_dma.h> 374c7070dbSScott Long #include <sys/nv.h> 38*23ac9029SStephen Hurd #include <sys/gtaskqueue.h> 394c7070dbSScott Long 404c7070dbSScott Long 414c7070dbSScott Long /* 424c7070dbSScott Long * Most cards can handle much larger TSO requests 434c7070dbSScott Long * but the FreeBSD TCP stack will break on larger 444c7070dbSScott Long * values 454c7070dbSScott Long */ 464c7070dbSScott Long #define FREEBSD_TSO_SIZE_MAX 65518 474c7070dbSScott Long 484c7070dbSScott Long 494c7070dbSScott Long struct iflib_ctx; 504c7070dbSScott Long typedef struct iflib_ctx *if_ctx_t; 514c7070dbSScott Long struct if_shared_ctx; 524c7070dbSScott Long typedef struct if_shared_ctx *if_shared_ctx_t; 534c7070dbSScott Long struct if_int_delay_info; 544c7070dbSScott Long typedef struct if_int_delay_info *if_int_delay_info_t; 554c7070dbSScott Long 564c7070dbSScott Long /* 574c7070dbSScott Long * File organization: 584c7070dbSScott Long * - public structures 594c7070dbSScott Long * - iflib accessors 604c7070dbSScott Long * - iflib utility functions 614c7070dbSScott Long * - iflib core functions 624c7070dbSScott Long */ 634c7070dbSScott Long 644c7070dbSScott Long typedef struct if_rxd_frag { 654c7070dbSScott Long uint8_t irf_flid; 664c7070dbSScott Long uint16_t irf_idx; 67*23ac9029SStephen Hurd uint16_t irf_len; 684c7070dbSScott Long } *if_rxd_frag_t; 694c7070dbSScott Long 704c7070dbSScott Long typedef struct if_rxd_info { 714c7070dbSScott Long /* set by iflib */ 724c7070dbSScott Long uint16_t iri_qsidx; /* qset index */ 734c7070dbSScott Long uint16_t iri_vtag; /* vlan tag - if flag set */ 74*23ac9029SStephen Hurd /* XXX redundant with the new irf_len field */ 754c7070dbSScott Long uint16_t iri_len; /* packet length */ 764c7070dbSScott Long uint16_t iri_cidx; /* consumer index of cq */ 774c7070dbSScott Long struct ifnet *iri_ifp; /* some drivers >1 interface per softc */ 784c7070dbSScott Long 794c7070dbSScott Long /* updated by driver */ 804c7070dbSScott Long uint16_t iri_flags; /* mbuf flags for packet */ 814c7070dbSScott Long uint32_t iri_flowid; /* RSS hash for packet */ 824c7070dbSScott Long uint32_t iri_csum_flags; /* m_pkthdr csum flags */ 834c7070dbSScott Long uint32_t iri_csum_data; /* m_pkthdr csum data */ 844c7070dbSScott Long uint8_t iri_nfrags; /* number of fragments in packet */ 854c7070dbSScott Long uint8_t iri_rsstype; /* RSS hash type */ 864c7070dbSScott Long uint8_t iri_pad; /* any padding in the received data */ 874c7070dbSScott Long if_rxd_frag_t iri_frags; 884c7070dbSScott Long } *if_rxd_info_t; 894c7070dbSScott Long 904c7070dbSScott Long #define IPI_TX_INTR 0x1 /* send an interrupt when this packet is sent */ 914c7070dbSScott Long #define IPI_TX_IPV4 0x2 /* ethertype IPv4 */ 924c7070dbSScott Long #define IPI_TX_IPV6 0x4 /* ethertype IPv6 */ 934c7070dbSScott Long 944c7070dbSScott Long typedef struct if_pkt_info { 954c7070dbSScott Long uint32_t ipi_len; /* packet length */ 964c7070dbSScott Long bus_dma_segment_t *ipi_segs; /* physical addresses */ 974c7070dbSScott Long uint16_t ipi_qsidx; /* queue set index */ 984c7070dbSScott Long uint16_t ipi_nsegs; /* number of segments */ 994c7070dbSScott Long uint16_t ipi_ndescs; /* number of descriptors used by encap */ 1004c7070dbSScott Long uint16_t ipi_flags; /* iflib per-packet flags */ 1014c7070dbSScott Long uint32_t ipi_pidx; /* start pidx for encap */ 1024c7070dbSScott Long uint32_t ipi_new_pidx; /* next available pidx post-encap */ 1034c7070dbSScott Long /* offload handling */ 1044c7070dbSScott Long uint64_t ipi_csum_flags; /* packet checksum flags */ 1054c7070dbSScott Long uint16_t ipi_tso_segsz; /* tso segment size */ 1064c7070dbSScott Long uint16_t ipi_mflags; /* packet mbuf flags */ 1074c7070dbSScott Long uint16_t ipi_vtag; /* VLAN tag */ 1084c7070dbSScott Long uint16_t ipi_etype; /* ether header type */ 1094c7070dbSScott Long uint8_t ipi_ehdrlen; /* ether header length */ 1104c7070dbSScott Long uint8_t ipi_ip_hlen; /* ip header length */ 1114c7070dbSScott Long uint8_t ipi_tcp_hlen; /* tcp header length */ 1124c7070dbSScott Long uint8_t ipi_tcp_hflags; /* tcp header flags */ 1134c7070dbSScott Long uint8_t ipi_ipproto; /* ip protocol */ 1144c7070dbSScott Long /* implied padding */ 1154c7070dbSScott Long uint32_t ipi_tcp_seq; /* tcp seqno */ 1164c7070dbSScott Long uint32_t ipi_tcp_sum; /* tcp csum */ 1174c7070dbSScott Long } *if_pkt_info_t; 1184c7070dbSScott Long 1194c7070dbSScott Long typedef struct if_irq { 1204c7070dbSScott Long struct resource *ii_res; 1214c7070dbSScott Long int ii_rid; 1224c7070dbSScott Long void *ii_tag; 1234c7070dbSScott Long } *if_irq_t; 1244c7070dbSScott Long 1254c7070dbSScott Long struct if_int_delay_info { 1264c7070dbSScott Long if_ctx_t iidi_ctx; /* Back-pointer to the iflib ctx (softc) */ 1274c7070dbSScott Long int iidi_offset; /* Register offset to read/write */ 1284c7070dbSScott Long int iidi_value; /* Current value in usecs */ 1294c7070dbSScott Long struct sysctl_oid *iidi_oidp; 1304c7070dbSScott Long struct sysctl_req *iidi_req; 1314c7070dbSScott Long }; 1324c7070dbSScott Long 1334c7070dbSScott Long typedef enum { 1344c7070dbSScott Long IFLIB_INTR_LEGACY, 1354c7070dbSScott Long IFLIB_INTR_MSI, 1364c7070dbSScott Long IFLIB_INTR_MSIX 1374c7070dbSScott Long } iflib_intr_mode_t; 1384c7070dbSScott Long 1394c7070dbSScott Long /* 1404c7070dbSScott Long * This really belongs in pciio.h or some place more general 1414c7070dbSScott Long * but this is the only consumer for now. 1424c7070dbSScott Long */ 1434c7070dbSScott Long typedef struct pci_vendor_info { 1444c7070dbSScott Long uint32_t pvi_vendor_id; 1454c7070dbSScott Long uint32_t pvi_device_id; 1464c7070dbSScott Long uint32_t pvi_subvendor_id; 1474c7070dbSScott Long uint32_t pvi_subdevice_id; 1484c7070dbSScott Long uint32_t pvi_rev_id; 1494c7070dbSScott Long uint32_t pvi_class_mask; 1504c7070dbSScott Long caddr_t pvi_name; 1514c7070dbSScott Long } pci_vendor_info_t; 1524c7070dbSScott Long 1534c7070dbSScott Long #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name} 1544c7070dbSScott Long #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name} 1554c7070dbSScott Long #define PVID_END {0, 0, 0, 0, 0, 0, NULL} 1564c7070dbSScott Long 1574c7070dbSScott Long typedef struct if_txrx { 1584c7070dbSScott Long int (*ift_txd_encap) (void *, if_pkt_info_t); 1594c7070dbSScott Long void (*ift_txd_flush) (void *, uint16_t, uint32_t); 1604c7070dbSScott Long int (*ift_txd_credits_update) (void *, uint16_t, uint32_t, bool); 1614c7070dbSScott Long 162*23ac9029SStephen Hurd int (*ift_rxd_available) (void *, uint16_t qsidx, uint32_t pidx, 163*23ac9029SStephen Hurd int budget); 1644c7070dbSScott Long int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri); 1654c7070dbSScott Long void (*ift_rxd_refill) (void * , uint16_t qsidx, uint8_t flidx, uint32_t pidx, 166*23ac9029SStephen Hurd uint64_t *paddrs, caddr_t *vaddrs, uint16_t count, uint16_t buf_size); 1674c7070dbSScott Long void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, uint32_t pidx); 1684c7070dbSScott Long int (*ift_legacy_intr) (void *); 1694c7070dbSScott Long } *if_txrx_t; 1704c7070dbSScott Long 1714c7070dbSScott Long typedef struct if_softc_ctx { 1724c7070dbSScott Long int isc_vectors; 1734c7070dbSScott Long int isc_nrxqsets; 1744c7070dbSScott Long int isc_ntxqsets; 1754c7070dbSScott Long int isc_msix_bar; /* can be model specific - initialize in attach_pre */ 1764c7070dbSScott Long int isc_tx_nsegments; /* can be model specific - initialize in attach_pre */ 177*23ac9029SStephen Hurd int isc_ntxd[8]; 178*23ac9029SStephen Hurd int isc_nrxd[8]; 179*23ac9029SStephen Hurd 180*23ac9029SStephen Hurd uint32_t isc_txqsizes[8]; 181*23ac9029SStephen Hurd uint32_t isc_rxqsizes[8]; 182*23ac9029SStephen Hurd int isc_max_txqsets; 183*23ac9029SStephen Hurd int isc_max_rxqsets; 1844c7070dbSScott Long int isc_tx_tso_segments_max; 1854c7070dbSScott Long int isc_tx_tso_size_max; 1864c7070dbSScott Long int isc_tx_tso_segsize_max; 1874c7070dbSScott Long int isc_rss_table_size; 1884c7070dbSScott Long int isc_rss_table_mask; 189*23ac9029SStephen Hurd int isc_nrxqsets_max; 190*23ac9029SStephen Hurd int isc_ntxqsets_max; 1914c7070dbSScott Long 1924c7070dbSScott Long iflib_intr_mode_t isc_intr; 1934c7070dbSScott Long uint16_t isc_max_frame_size; /* set at init time by driver */ 1944c7070dbSScott Long pci_vendor_info_t isc_vendor_info; /* set by iflib prior to attach_pre */ 1954c7070dbSScott Long } *if_softc_ctx_t; 1964c7070dbSScott Long 1974c7070dbSScott Long /* 1984c7070dbSScott Long * Initialization values for device 1994c7070dbSScott Long */ 2004c7070dbSScott Long struct if_shared_ctx { 2014c7070dbSScott Long int isc_magic; 2024c7070dbSScott Long if_txrx_t isc_txrx; 2034c7070dbSScott Long driver_t *isc_driver; 2044c7070dbSScott Long int isc_nfl; 2054c7070dbSScott Long int isc_flags; 2064c7070dbSScott Long bus_size_t isc_q_align; 2074c7070dbSScott Long bus_size_t isc_tx_maxsize; 2084c7070dbSScott Long bus_size_t isc_tx_maxsegsize; 2094c7070dbSScott Long bus_size_t isc_rx_maxsize; 2104c7070dbSScott Long bus_size_t isc_rx_maxsegsize; 2114c7070dbSScott Long int isc_rx_nsegments; 2124c7070dbSScott Long int isc_rx_process_limit; 2134c7070dbSScott Long int isc_ntxqs; /* # of tx queues per tx qset - usually 1 */ 2144c7070dbSScott Long int isc_nrxqs; /* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */ 2154c7070dbSScott Long int isc_admin_intrcnt; /* # of admin/link interrupts */ 2164c7070dbSScott Long 217*23ac9029SStephen Hurd 2184c7070dbSScott Long int isc_tx_reclaim_thresh; 2194c7070dbSScott Long 2204c7070dbSScott Long /* fields necessary for probe */ 2214c7070dbSScott Long pci_vendor_info_t *isc_vendor_info; 2224c7070dbSScott Long char *isc_driver_version; 2234c7070dbSScott Long /* optional function to transform the read values to match the table*/ 2244c7070dbSScott Long void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id, 2254c7070dbSScott Long uint16_t *subdevice_id, uint16_t *rev_id); 226*23ac9029SStephen Hurd int isc_nrxd_min[8]; 227*23ac9029SStephen Hurd int isc_nrxd_default[8]; 228*23ac9029SStephen Hurd int isc_nrxd_max[8]; 229*23ac9029SStephen Hurd int isc_ntxd_min[8]; 230*23ac9029SStephen Hurd int isc_ntxd_default[8]; 231*23ac9029SStephen Hurd int isc_ntxd_max[8]; 2324c7070dbSScott Long }; 2334c7070dbSScott Long 2344c7070dbSScott Long typedef struct iflib_dma_info { 2354c7070dbSScott Long bus_addr_t idi_paddr; 2364c7070dbSScott Long caddr_t idi_vaddr; 2374c7070dbSScott Long bus_dma_tag_t idi_tag; 2384c7070dbSScott Long bus_dmamap_t idi_map; 2394c7070dbSScott Long uint32_t idi_size; 2404c7070dbSScott Long } *iflib_dma_info_t; 2414c7070dbSScott Long 2424c7070dbSScott Long #define IFLIB_MAGIC 0xCAFEF00D 2434c7070dbSScott Long 2444c7070dbSScott Long typedef enum { 2454c7070dbSScott Long IFLIB_INTR_TX, 2464c7070dbSScott Long IFLIB_INTR_RX, 2474c7070dbSScott Long IFLIB_INTR_ADMIN, 2484c7070dbSScott Long IFLIB_INTR_IOV, 2494c7070dbSScott Long } iflib_intr_type_t; 2504c7070dbSScott Long 2514c7070dbSScott Long #ifndef ETH_ADDR_LEN 2524c7070dbSScott Long #define ETH_ADDR_LEN 6 2534c7070dbSScott Long #endif 2544c7070dbSScott Long 2554c7070dbSScott Long 2564c7070dbSScott Long /* 257*23ac9029SStephen Hurd * Interface has a separate command queue for RX 2584c7070dbSScott Long */ 259*23ac9029SStephen Hurd #define IFLIB_HAS_RXCQ 0x1 2604c7070dbSScott Long /* 2614c7070dbSScott Long * Driver has already allocated vectors 2624c7070dbSScott Long */ 2634c7070dbSScott Long #define IFLIB_SKIP_MSIX 0x2 2644c7070dbSScott Long 2654c7070dbSScott Long /* 2664c7070dbSScott Long * Interface is a virtual function 2674c7070dbSScott Long */ 2684c7070dbSScott Long #define IFLIB_IS_VF 0x4 269*23ac9029SStephen Hurd /* 270*23ac9029SStephen Hurd * Interface has a separate command queue for TX 271*23ac9029SStephen Hurd */ 272*23ac9029SStephen Hurd #define IFLIB_HAS_TXCQ 0x8 2734c7070dbSScott Long 2744c7070dbSScott Long 2754c7070dbSScott Long /* 2764c7070dbSScott Long * field accessors 2774c7070dbSScott Long */ 2784c7070dbSScott Long void *iflib_get_softc(if_ctx_t ctx); 2794c7070dbSScott Long 2804c7070dbSScott Long device_t iflib_get_dev(if_ctx_t ctx); 2814c7070dbSScott Long 2824c7070dbSScott Long if_t iflib_get_ifp(if_ctx_t ctx); 2834c7070dbSScott Long 2844c7070dbSScott Long struct ifmedia *iflib_get_media(if_ctx_t ctx); 2854c7070dbSScott Long 2864c7070dbSScott Long if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx); 2874c7070dbSScott Long if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx); 2884c7070dbSScott Long 2894c7070dbSScott Long void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]); 2904c7070dbSScott Long 2914c7070dbSScott Long 2924c7070dbSScott Long 2934c7070dbSScott Long 2944c7070dbSScott Long /* 2954c7070dbSScott Long * If the driver can plug cleanly in to newbus use these 2964c7070dbSScott Long */ 2974c7070dbSScott Long int iflib_device_probe(device_t); 2984c7070dbSScott Long int iflib_device_attach(device_t); 2994c7070dbSScott Long int iflib_device_detach(device_t); 3004c7070dbSScott Long int iflib_device_suspend(device_t); 3014c7070dbSScott Long int iflib_device_resume(device_t); 3024c7070dbSScott Long int iflib_device_shutdown(device_t); 3034c7070dbSScott Long 3044c7070dbSScott Long 3054c7070dbSScott Long int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *); 3064c7070dbSScott Long void iflib_device_iov_uninit(device_t); 3074c7070dbSScott Long int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *); 3084c7070dbSScott Long 3094c7070dbSScott Long /* 3104c7070dbSScott Long * If the driver can't plug cleanly in to newbus 3114c7070dbSScott Long * use these 3124c7070dbSScott Long */ 3134c7070dbSScott Long int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp); 3144c7070dbSScott Long int iflib_device_deregister(if_ctx_t); 3154c7070dbSScott Long 3164c7070dbSScott Long 3174c7070dbSScott Long 3184c7070dbSScott Long int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg, driver_intr_t, void *arg, char *name); 3194c7070dbSScott Long int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 3204c7070dbSScott Long iflib_intr_type_t type, driver_filter_t *filter, 3214c7070dbSScott Long void *filter_arg, int qid, char *name); 3224c7070dbSScott Long void iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type, void *arg, int qid, char *name); 3234c7070dbSScott Long 3244c7070dbSScott Long void iflib_irq_free(if_ctx_t ctx, if_irq_t irq); 3254c7070dbSScott Long 3264c7070dbSScott Long void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name); 3274c7070dbSScott Long 3284c7070dbSScott Long void iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask, 329*23ac9029SStephen Hurd gtask_fn_t *fn, char *name); 330*23ac9029SStephen Hurd 331*23ac9029SStephen Hurd void iflib_config_gtask_deinit(struct grouptask *gtask); 332*23ac9029SStephen Hurd 3334c7070dbSScott Long 3344c7070dbSScott Long 3354c7070dbSScott Long void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid); 3364c7070dbSScott Long void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid); 3374c7070dbSScott Long void iflib_admin_intr_deferred(if_ctx_t ctx); 3384c7070dbSScott Long void iflib_iov_intr_deferred(if_ctx_t ctx); 3394c7070dbSScott Long 3404c7070dbSScott Long 341*23ac9029SStephen Hurd void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate); 3424c7070dbSScott Long 3434c7070dbSScott Long int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags); 3444c7070dbSScott Long void iflib_dma_free(iflib_dma_info_t dma); 3454c7070dbSScott Long 3464c7070dbSScott Long int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count); 3474c7070dbSScott Long 3484c7070dbSScott Long void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count); 3494c7070dbSScott Long 3504c7070dbSScott Long 3514c7070dbSScott Long struct mtx *iflib_ctx_lock_get(if_ctx_t); 3524c7070dbSScott Long struct mtx *iflib_qset_lock_get(if_ctx_t, uint16_t); 3534c7070dbSScott Long 3544c7070dbSScott Long void iflib_led_create(if_ctx_t ctx); 3554c7070dbSScott Long 3564c7070dbSScott Long void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *, 3574c7070dbSScott Long if_int_delay_info_t, int, int); 3584c7070dbSScott Long 3594c7070dbSScott Long #endif /* __IFLIB_H_ */ 360