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 32 /** 33 * @file iavf_iflib.h 34 * @brief main header for the iflib driver 35 * 36 * Contains definitions for various driver structures used throughout the 37 * driver code. This header is used by the iflib implementation. 38 */ 39 #ifndef _IAVF_IFLIB_H_ 40 #define _IAVF_IFLIB_H_ 41 42 #include "iavf_opts.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/buf_ring.h> 47 #include <sys/callout.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/stdarg.h> 57 #include <sys/syslog.h> 58 59 #include <net/if.h> 60 #include <net/if_var.h> 61 #include <net/if_arp.h> 62 #include <net/bpf.h> 63 #include <net/if_dl.h> 64 #include <net/if_media.h> 65 66 #include <net/bpf.h> 67 #include <net/if_types.h> 68 #include <net/if_vlan_var.h> 69 70 #include <netinet/in_systm.h> 71 #include <netinet/in.h> 72 #include <netinet/if_ether.h> 73 #include <netinet/ip.h> 74 #include <netinet/ip6.h> 75 #include <netinet/tcp.h> 76 #include <netinet/tcp_lro.h> 77 #include <netinet/udp.h> 78 #include <netinet/sctp.h> 79 80 #include <machine/in_cksum.h> 81 82 #include <sys/bus.h> 83 #include <sys/pciio.h> 84 #include <machine/bus.h> 85 #include <sys/rman.h> 86 #include <machine/resource.h> 87 #include <vm/vm.h> 88 #include <vm/pmap.h> 89 #include <machine/clock.h> 90 #include <dev/pci/pcivar.h> 91 #include <dev/pci/pcireg.h> 92 #include <sys/proc.h> 93 #include <sys/endian.h> 94 #include <sys/taskqueue.h> 95 #include <sys/pcpu.h> 96 #include <sys/smp.h> 97 #include <sys/sbuf.h> 98 #include <machine/smp.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 IAVF_VC_LOCK(_sc) mtx_lock(&(_sc)->vc_mtx) 127 #define IAVF_VC_UNLOCK(_sc) mtx_unlock(&(_sc)->vc_mtx) 128 #define IAVF_VC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->vc_mtx) 129 #define IAVF_VC_TRYLOCK(_sc) mtx_trylock(&(_sc)->vc_mtx) 130 #define IAVF_VC_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->vc_mtx, MA_OWNED) 131 132 /** 133 * @struct tx_ring 134 * @brief Transmit ring control struct 135 * 136 * Structure used to track the hardware Tx ring data. 137 */ 138 struct tx_ring { 139 struct iavf_tx_queue *que; 140 u32 tail; 141 struct iavf_tx_desc *tx_base; 142 u64 tx_paddr; 143 u32 packets; 144 u32 me; 145 146 /* 147 * For reporting completed packet status 148 * in descriptor writeback mdoe 149 */ 150 qidx_t *tx_rsq; 151 qidx_t tx_rs_cidx; 152 qidx_t tx_rs_pidx; 153 qidx_t tx_cidx_processed; 154 155 /* Used for Dynamic ITR calculation */ 156 u32 bytes; 157 u32 itr; 158 u32 latency; 159 160 /* Soft Stats */ 161 u64 tx_bytes; 162 u64 tx_packets; 163 u64 mss_too_small; 164 }; 165 166 /** 167 * @struct rx_ring 168 * @brief Receive ring control struct 169 * 170 * Structure used to track the hardware Rx ring data. 171 */ 172 struct rx_ring { 173 struct iavf_rx_queue *que; 174 union iavf_rx_desc *rx_base; 175 uint64_t rx_paddr; 176 bool discard; 177 u32 itr; 178 u32 latency; 179 u32 mbuf_sz; 180 u32 tail; 181 u32 me; 182 183 /* Used for Dynamic ITR calculation */ 184 u32 packets; 185 u32 bytes; 186 187 /* Soft stats */ 188 u64 rx_packets; 189 u64 rx_bytes; 190 u64 desc_errs; 191 }; 192 193 /** 194 * @struct iavf_tx_queue 195 * @brief Driver Tx queue structure 196 * 197 * Structure to track the Tx ring, IRQ, MSI-X vector, and some software stats 198 * for a Tx queue. 199 */ 200 struct iavf_tx_queue { 201 struct iavf_vsi *vsi; 202 struct tx_ring txr; 203 struct if_irq que_irq; 204 u32 msix; 205 206 /* Stats */ 207 u64 irqs; 208 u64 tso; 209 u32 pkt_too_small; 210 }; 211 212 /** 213 * @struct iavf_rx_queue 214 * @brief Driver Rx queue structure 215 * 216 * Structure to track the Rx ring, IRQ, MSI-X vector, and some software stats 217 * for an Rx queue. 218 */ 219 struct iavf_rx_queue { 220 struct iavf_vsi *vsi; 221 struct rx_ring rxr; 222 struct if_irq que_irq; 223 u32 msix; 224 225 /* Stats */ 226 u64 irqs; 227 }; 228 229 /** 230 * @struct iavf_vsi 231 * @brief Virtual Station Interface 232 * 233 * Data tracking a VSI for an iavf device. 234 */ 235 struct iavf_vsi { 236 if_ctx_t ctx; 237 if_softc_ctx_t shared; 238 if_t ifp; 239 struct iavf_sc *back; 240 device_t dev; 241 struct iavf_hw *hw; 242 243 int id; 244 u16 num_rx_queues; 245 u16 num_tx_queues; 246 u32 rx_itr_setting; 247 u32 tx_itr_setting; 248 u16 max_frame_size; 249 bool enable_head_writeback; 250 251 bool link_active; 252 253 struct iavf_tx_queue *tx_queues; 254 struct iavf_rx_queue *rx_queues; 255 struct if_irq irq; 256 257 u16 num_vlans; 258 u16 num_macs; 259 260 /* Per-VSI stats from hardware */ 261 struct iavf_eth_stats eth_stats; 262 struct iavf_eth_stats eth_stats_offsets; 263 bool stat_offsets_loaded; 264 /* VSI stat counters */ 265 u64 ipackets; 266 u64 ierrors; 267 u64 opackets; 268 u64 oerrors; 269 u64 ibytes; 270 u64 obytes; 271 u64 imcasts; 272 u64 omcasts; 273 u64 iqdrops; 274 u64 oqdrops; 275 u64 noproto; 276 277 /* Misc. */ 278 u64 flags; 279 struct sysctl_oid *vsi_node; 280 struct sysctl_ctx_list sysctl_ctx; 281 }; 282 283 /** 284 * @struct iavf_mac_filter 285 * @brief MAC Address filter data 286 * 287 * Entry in the MAC filter list describing a MAC address filter used to 288 * program hardware to filter a specific MAC address. 289 */ 290 struct iavf_mac_filter { 291 SLIST_ENTRY(iavf_mac_filter) next; 292 u8 macaddr[ETHER_ADDR_LEN]; 293 u16 flags; 294 }; 295 296 /** 297 * @struct mac_list 298 * @brief MAC filter list head 299 * 300 * List head type for a singly-linked list of MAC address filters. 301 */ 302 SLIST_HEAD(mac_list, iavf_mac_filter); 303 304 /** 305 * @struct iavf_vlan_filter 306 * @brief VLAN filter data 307 * 308 * Entry in the VLAN filter list describing a VLAN filter used to 309 * program hardware to filter traffic on a specific VLAN. 310 */ 311 struct iavf_vlan_filter { 312 SLIST_ENTRY(iavf_vlan_filter) next; 313 u16 vlan; 314 u16 flags; 315 }; 316 317 /** 318 * @struct vlan_list 319 * @brief VLAN filter list head 320 * 321 * List head type for a singly-linked list of VLAN filters. 322 */ 323 SLIST_HEAD(vlan_list, iavf_vlan_filter); 324 325 /** 326 * @struct iavf_sc 327 * @brief Main context structure for the iavf driver 328 * 329 * Software context structure used to store information about a single device 330 * that is loaded by the iavf driver. 331 */ 332 struct iavf_sc { 333 struct iavf_vsi vsi; 334 335 struct iavf_hw hw; 336 struct iavf_osdep osdep; 337 device_t dev; 338 339 struct resource *pci_mem; 340 341 /* driver state flags, only access using atomic functions */ 342 u32 state; 343 344 struct ifmedia *media; 345 struct virtchnl_version_info version; 346 enum iavf_dbg_mask dbg_mask; 347 u16 promisc_flags; 348 349 bool link_up; 350 union { 351 enum virtchnl_link_speed link_speed; 352 u32 link_speed_adv; 353 }; 354 355 /* Tunable settings */ 356 int tx_itr; 357 int rx_itr; 358 int dynamic_tx_itr; 359 int dynamic_rx_itr; 360 361 /* Filter lists */ 362 struct mac_list *mac_filters; 363 struct vlan_list *vlan_filters; 364 365 /* Virtual comm channel */ 366 struct virtchnl_vf_resource *vf_res; 367 struct virtchnl_vsi_resource *vsi_res; 368 369 /* Misc stats maintained by the driver */ 370 u64 admin_irq; 371 372 /* Buffer used for reading AQ responses */ 373 u8 aq_buffer[IAVF_AQ_BUF_SZ]; 374 375 /* State flag used in init/stop */ 376 u32 queues_enabled; 377 u8 enable_queues_chan; 378 u8 disable_queues_chan; 379 u32 mbx_ready; 380 u32 vc_reinit_required; 381 u32 mbx_retry_pending; 382 struct callout mbx_retry; 383 struct timeval mbx_last_log; 384 u8 mbx_retry_stage; 385 bool mbx_retry_initialized; 386 387 /* For virtchnl message processing task */ 388 struct task vc_task; 389 struct taskqueue *vc_tq; 390 char vc_mtx_name[16]; 391 struct mtx vc_mtx; 392 }; 393 394 /* Function prototypes */ 395 void iavf_init_tx_ring(struct iavf_vsi *vsi, struct iavf_tx_queue *que); 396 const char * iavf_vc_stat_str(struct iavf_hw *hw, 397 enum virtchnl_status_code stat_err); 398 void iavf_init_tx_rsqs(struct iavf_vsi *vsi); 399 void iavf_init_tx_cidx(struct iavf_vsi *vsi); 400 u64 iavf_max_vc_speed_to_value(u8 link_speeds); 401 void iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi, 402 struct sysctl_ctx_list *ctx, const char *sysctl_name); 403 void iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx, 404 struct sysctl_oid_list *child, 405 struct iavf_eth_stats *eth_stats); 406 void iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi); 407 408 void iavf_enable_intr(struct iavf_vsi *); 409 void iavf_disable_intr(struct iavf_vsi *); 410 #endif /* _IAVF_IFLIB_H_ */ 411