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