1 /* SPDX-License-Identifier: BSD-3-Clause */ 2 /* Copyright (c) 2021, Intel Corporation 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. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * 3. Neither the name of the Intel Corporation nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /*$FreeBSD$*/ 32 33 /** 34 * @file iavf_iflib.h 35 * @brief main header for the iflib driver 36 * 37 * Contains definitions for various driver structures used throughout the 38 * driver code. This header is used by the iflib implementation. 39 */ 40 #ifndef _IAVF_IFLIB_H_ 41 #define _IAVF_IFLIB_H_ 42 43 #include "iavf_opts.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/buf_ring.h> 48 #include <sys/mbuf.h> 49 #include <sys/protosw.h> 50 #include <sys/socket.h> 51 #include <sys/malloc.h> 52 #include <sys/kernel.h> 53 #include <sys/module.h> 54 #include <sys/sockio.h> 55 #include <sys/eventhandler.h> 56 #include <sys/syslog.h> 57 58 #include <net/if.h> 59 #include <net/if_var.h> 60 #include <net/if_arp.h> 61 #include <net/bpf.h> 62 #include <net/if_dl.h> 63 #include <net/if_media.h> 64 65 #include <net/bpf.h> 66 #include <net/if_types.h> 67 #include <net/if_vlan_var.h> 68 69 #include <netinet/in_systm.h> 70 #include <netinet/in.h> 71 #include <netinet/if_ether.h> 72 #include <netinet/ip.h> 73 #include <netinet/ip6.h> 74 #include <netinet/tcp.h> 75 #include <netinet/tcp_lro.h> 76 #include <netinet/udp.h> 77 #include <netinet/sctp.h> 78 79 #include <machine/in_cksum.h> 80 81 #include <sys/bus.h> 82 #include <sys/pciio.h> 83 #include <machine/bus.h> 84 #include <sys/rman.h> 85 #include <machine/resource.h> 86 #include <vm/vm.h> 87 #include <vm/pmap.h> 88 #include <machine/clock.h> 89 #include <dev/pci/pcivar.h> 90 #include <dev/pci/pcireg.h> 91 #include <sys/proc.h> 92 #include <sys/endian.h> 93 #include <sys/taskqueue.h> 94 #include <sys/pcpu.h> 95 #include <sys/smp.h> 96 #include <sys/sbuf.h> 97 #include <machine/smp.h> 98 #include <machine/stdarg.h> 99 #include <net/ethernet.h> 100 #include <net/iflib.h> 101 #include "ifdi_if.h" 102 103 #include "iavf_lib.h" 104 105 #define IAVF_CSUM_TCP \ 106 (CSUM_IP_TCP|CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP6_TCP) 107 #define IAVF_CSUM_UDP \ 108 (CSUM_IP_UDP|CSUM_IP6_UDP) 109 #define IAVF_CSUM_SCTP \ 110 (CSUM_IP_SCTP|CSUM_IP6_SCTP) 111 #define IAVF_CSUM_IPV4 \ 112 (CSUM_IP|CSUM_IP_TSO) 113 114 #define IAVF_CAPS \ 115 (IFCAP_TSO4 | IFCAP_TSO6 | \ 116 IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | \ 117 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | \ 118 IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO | \ 119 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM | \ 120 IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU | IFCAP_LRO) 121 122 #define iavf_sc_from_ctx(_ctx) \ 123 ((struct iavf_sc *)iflib_get_softc(_ctx)) 124 125 /* Use the correct assert function for each lock type */ 126 #define IFLIB_CTX_ASSERT(_ctx) \ 127 sx_assert(iflib_ctx_lock_get(_ctx), SA_XLOCKED) 128 129 #define IAVF_VC_LOCK(_sc) mtx_lock(&(_sc)->vc_mtx) 130 #define IAVF_VC_UNLOCK(_sc) mtx_unlock(&(_sc)->vc_mtx) 131 #define IAVF_VC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->vc_mtx) 132 #define IAVF_VC_TRYLOCK(_sc) mtx_trylock(&(_sc)->vc_mtx) 133 #define IAVF_VC_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->vc_mtx, MA_OWNED) 134 135 /** 136 * @struct tx_ring 137 * @brief Transmit ring control struct 138 * 139 * Structure used to track the hardware Tx ring data. 140 */ 141 struct tx_ring { 142 struct iavf_tx_queue *que; 143 u32 tail; 144 struct iavf_tx_desc *tx_base; 145 u64 tx_paddr; 146 u32 packets; 147 u32 me; 148 149 /* 150 * For reporting completed packet status 151 * in descriptor writeback mdoe 152 */ 153 qidx_t *tx_rsq; 154 qidx_t tx_rs_cidx; 155 qidx_t tx_rs_pidx; 156 qidx_t tx_cidx_processed; 157 158 /* Used for Dynamic ITR calculation */ 159 u32 bytes; 160 u32 itr; 161 u32 latency; 162 163 /* Soft Stats */ 164 u64 tx_bytes; 165 u64 tx_packets; 166 u64 mss_too_small; 167 }; 168 169 /** 170 * @struct rx_ring 171 * @brief Receive ring control struct 172 * 173 * Structure used to track the hardware Rx ring data. 174 */ 175 struct rx_ring { 176 struct iavf_rx_queue *que; 177 union iavf_rx_desc *rx_base; 178 uint64_t rx_paddr; 179 bool discard; 180 u32 itr; 181 u32 latency; 182 u32 mbuf_sz; 183 u32 tail; 184 u32 me; 185 186 /* Used for Dynamic ITR calculation */ 187 u32 packets; 188 u32 bytes; 189 190 /* Soft stats */ 191 u64 rx_packets; 192 u64 rx_bytes; 193 u64 desc_errs; 194 }; 195 196 /** 197 * @struct iavf_tx_queue 198 * @brief Driver Tx queue structure 199 * 200 * Structure to track the Tx ring, IRQ, MSI-X vector, and some software stats 201 * for a Tx queue. 202 */ 203 struct iavf_tx_queue { 204 struct iavf_vsi *vsi; 205 struct tx_ring txr; 206 struct if_irq que_irq; 207 u32 msix; 208 209 /* Stats */ 210 u64 irqs; 211 u64 tso; 212 u32 pkt_too_small; 213 }; 214 215 /** 216 * @struct iavf_rx_queue 217 * @brief Driver Rx queue structure 218 * 219 * Structure to track the Rx ring, IRQ, MSI-X vector, and some software stats 220 * for an Rx queue. 221 */ 222 struct iavf_rx_queue { 223 struct iavf_vsi *vsi; 224 struct rx_ring rxr; 225 struct if_irq que_irq; 226 u32 msix; 227 228 /* Stats */ 229 u64 irqs; 230 }; 231 232 /** 233 * @struct iavf_vsi 234 * @brief Virtual Station Interface 235 * 236 * Data tracking a VSI for an iavf device. 237 */ 238 struct iavf_vsi { 239 if_ctx_t ctx; 240 if_softc_ctx_t shared; 241 if_t ifp; 242 struct iavf_sc *back; 243 device_t dev; 244 struct iavf_hw *hw; 245 246 int id; 247 u16 num_rx_queues; 248 u16 num_tx_queues; 249 u32 rx_itr_setting; 250 u32 tx_itr_setting; 251 u16 max_frame_size; 252 bool enable_head_writeback; 253 254 bool link_active; 255 256 struct iavf_tx_queue *tx_queues; 257 struct iavf_rx_queue *rx_queues; 258 struct if_irq irq; 259 260 u16 num_vlans; 261 u16 num_macs; 262 263 /* Per-VSI stats from hardware */ 264 struct iavf_eth_stats eth_stats; 265 struct iavf_eth_stats eth_stats_offsets; 266 bool stat_offsets_loaded; 267 /* VSI stat counters */ 268 u64 ipackets; 269 u64 ierrors; 270 u64 opackets; 271 u64 oerrors; 272 u64 ibytes; 273 u64 obytes; 274 u64 imcasts; 275 u64 omcasts; 276 u64 iqdrops; 277 u64 oqdrops; 278 u64 noproto; 279 280 /* Misc. */ 281 u64 flags; 282 struct sysctl_oid *vsi_node; 283 struct sysctl_ctx_list sysctl_ctx; 284 }; 285 286 /** 287 * @struct iavf_mac_filter 288 * @brief MAC Address filter data 289 * 290 * Entry in the MAC filter list describing a MAC address filter used to 291 * program hardware to filter a specific MAC address. 292 */ 293 struct iavf_mac_filter { 294 SLIST_ENTRY(iavf_mac_filter) next; 295 u8 macaddr[ETHER_ADDR_LEN]; 296 u16 flags; 297 }; 298 299 /** 300 * @struct mac_list 301 * @brief MAC filter list head 302 * 303 * List head type for a singly-linked list of MAC address filters. 304 */ 305 SLIST_HEAD(mac_list, iavf_mac_filter); 306 307 /** 308 * @struct iavf_vlan_filter 309 * @brief VLAN filter data 310 * 311 * Entry in the VLAN filter list describing a VLAN filter used to 312 * program hardware to filter traffic on a specific VLAN. 313 */ 314 struct iavf_vlan_filter { 315 SLIST_ENTRY(iavf_vlan_filter) next; 316 u16 vlan; 317 u16 flags; 318 }; 319 320 /** 321 * @struct vlan_list 322 * @brief VLAN filter list head 323 * 324 * List head type for a singly-linked list of VLAN filters. 325 */ 326 SLIST_HEAD(vlan_list, iavf_vlan_filter); 327 328 /** 329 * @struct iavf_sc 330 * @brief Main context structure for the iavf driver 331 * 332 * Software context structure used to store information about a single device 333 * that is loaded by the iavf driver. 334 */ 335 struct iavf_sc { 336 struct iavf_vsi vsi; 337 338 struct iavf_hw hw; 339 struct iavf_osdep osdep; 340 device_t dev; 341 342 struct resource *pci_mem; 343 344 /* driver state flags, only access using atomic functions */ 345 u32 state; 346 347 struct ifmedia *media; 348 struct virtchnl_version_info version; 349 enum iavf_dbg_mask dbg_mask; 350 u16 promisc_flags; 351 352 bool link_up; 353 union { 354 enum virtchnl_link_speed link_speed; 355 u32 link_speed_adv; 356 }; 357 358 /* Tunable settings */ 359 int tx_itr; 360 int rx_itr; 361 int dynamic_tx_itr; 362 int dynamic_rx_itr; 363 364 /* Filter lists */ 365 struct mac_list *mac_filters; 366 struct vlan_list *vlan_filters; 367 368 /* Virtual comm channel */ 369 struct virtchnl_vf_resource *vf_res; 370 struct virtchnl_vsi_resource *vsi_res; 371 372 /* Misc stats maintained by the driver */ 373 u64 admin_irq; 374 375 /* Buffer used for reading AQ responses */ 376 u8 aq_buffer[IAVF_AQ_BUF_SZ]; 377 378 /* State flag used in init/stop */ 379 u32 queues_enabled; 380 u8 enable_queues_chan; 381 u8 disable_queues_chan; 382 383 /* For virtchnl message processing task */ 384 struct task vc_task; 385 struct taskqueue *vc_tq; 386 char vc_mtx_name[16]; 387 struct mtx vc_mtx; 388 }; 389 390 /* Function prototypes */ 391 void iavf_init_tx_ring(struct iavf_vsi *vsi, struct iavf_tx_queue *que); 392 void iavf_get_default_rss_key(u32 *); 393 const char * iavf_vc_stat_str(struct iavf_hw *hw, 394 enum virtchnl_status_code stat_err); 395 void iavf_init_tx_rsqs(struct iavf_vsi *vsi); 396 void iavf_init_tx_cidx(struct iavf_vsi *vsi); 397 u64 iavf_max_vc_speed_to_value(u8 link_speeds); 398 void iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi, 399 struct sysctl_ctx_list *ctx, const char *sysctl_name); 400 void iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx, 401 struct sysctl_oid_list *child, 402 struct iavf_eth_stats *eth_stats); 403 void iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi); 404 405 void iavf_enable_intr(struct iavf_vsi *); 406 void iavf_disable_intr(struct iavf_vsi *); 407 #endif /* _IAVF_IFLIB_H_ */ 408