1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2015-2024 Amazon.com, Inc. or its affiliates. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #include <sys/cdefs.h> 31 #include "opt_rss.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/endian.h> 37 #include <sys/eventhandler.h> 38 #include <sys/kernel.h> 39 #include <sys/kthread.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/module.h> 43 #include <sys/rman.h> 44 #include <sys/smp.h> 45 #include <sys/socket.h> 46 #include <sys/sockio.h> 47 #include <sys/sysctl.h> 48 #include <sys/taskqueue.h> 49 #include <sys/time.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 54 #include <machine/atomic.h> 55 #include <machine/bus.h> 56 #include <machine/in_cksum.h> 57 #include <machine/resource.h> 58 59 #include <dev/pci/pcireg.h> 60 #include <dev/pci/pcivar.h> 61 62 #include <net/bpf.h> 63 #include <net/ethernet.h> 64 #include <net/if.h> 65 #include <net/if_arp.h> 66 #include <net/if_dl.h> 67 #include <net/if_media.h> 68 #include <net/if_types.h> 69 #include <net/if_var.h> 70 #include <net/if_vlan_var.h> 71 #include <netinet/in.h> 72 #include <netinet/in_systm.h> 73 #include <netinet/if_ether.h> 74 #include <netinet/ip.h> 75 #include <netinet/ip6.h> 76 #include <netinet/tcp.h> 77 #include <netinet/udp.h> 78 79 #include "ena.h" 80 #include "ena_datapath.h" 81 #include "ena_rss.h" 82 #include "ena_sysctl.h" 83 84 #ifdef DEV_NETMAP 85 #include "ena_netmap.h" 86 #endif /* DEV_NETMAP */ 87 88 /********************************************************* 89 * Function prototypes 90 *********************************************************/ 91 static int ena_probe(device_t); 92 static void ena_intr_msix_mgmnt(void *); 93 static void ena_free_pci_resources(struct ena_adapter *); 94 static int ena_change_mtu(if_t, int); 95 static inline void ena_alloc_counters(counter_u64_t *, int); 96 static inline void ena_free_counters(counter_u64_t *, int); 97 static inline void ena_reset_counters(counter_u64_t *, int); 98 static void ena_init_io_rings_common(struct ena_adapter *, struct ena_ring *, 99 uint16_t); 100 static void ena_init_io_rings_basic(struct ena_adapter *); 101 static void ena_init_io_rings_advanced(struct ena_adapter *); 102 static void ena_init_io_rings(struct ena_adapter *); 103 static void ena_free_io_ring_resources(struct ena_adapter *, unsigned int); 104 static void ena_free_all_io_rings_resources(struct ena_adapter *); 105 static int ena_setup_tx_dma_tag(struct ena_adapter *); 106 static int ena_free_tx_dma_tag(struct ena_adapter *); 107 static int ena_setup_rx_dma_tag(struct ena_adapter *); 108 static int ena_free_rx_dma_tag(struct ena_adapter *); 109 static void ena_release_all_tx_dmamap(struct ena_ring *); 110 static int ena_setup_tx_resources(struct ena_adapter *, int); 111 static void ena_free_tx_resources(struct ena_adapter *, int); 112 static int ena_setup_all_tx_resources(struct ena_adapter *); 113 static void ena_free_all_tx_resources(struct ena_adapter *); 114 static int ena_setup_rx_resources(struct ena_adapter *, unsigned int); 115 static void ena_free_rx_resources(struct ena_adapter *, unsigned int); 116 static int ena_setup_all_rx_resources(struct ena_adapter *); 117 static void ena_free_all_rx_resources(struct ena_adapter *); 118 static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *, 119 struct ena_rx_buffer *); 120 static void ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *, 121 struct ena_rx_buffer *); 122 static void ena_free_rx_bufs(struct ena_adapter *, unsigned int); 123 static void ena_refill_all_rx_bufs(struct ena_adapter *); 124 static void ena_free_all_rx_bufs(struct ena_adapter *); 125 static void ena_free_tx_bufs(struct ena_adapter *, unsigned int); 126 static void ena_free_all_tx_bufs(struct ena_adapter *); 127 static void ena_destroy_all_tx_queues(struct ena_adapter *); 128 static void ena_destroy_all_rx_queues(struct ena_adapter *); 129 static void ena_destroy_all_io_queues(struct ena_adapter *); 130 static int ena_create_io_queues(struct ena_adapter *); 131 static int ena_handle_msix(void *); 132 static int ena_enable_msix(struct ena_adapter *); 133 static void ena_setup_mgmnt_intr(struct ena_adapter *); 134 static int ena_setup_io_intr(struct ena_adapter *); 135 static int ena_request_mgmnt_irq(struct ena_adapter *); 136 static int ena_request_io_irq(struct ena_adapter *); 137 static void ena_free_mgmnt_irq(struct ena_adapter *); 138 static void ena_free_io_irq(struct ena_adapter *); 139 static void ena_free_irqs(struct ena_adapter *); 140 static void ena_disable_msix(struct ena_adapter *); 141 static void ena_unmask_all_io_irqs(struct ena_adapter *); 142 static int ena_up_complete(struct ena_adapter *); 143 static uint64_t ena_get_counter(if_t, ift_counter); 144 static int ena_media_change(if_t); 145 static void ena_media_status(if_t, struct ifmediareq *); 146 static void ena_init(void *); 147 static int ena_ioctl(if_t, u_long, caddr_t); 148 static int ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *); 149 static void ena_update_host_info(struct ena_admin_host_info *, if_t); 150 static void ena_update_hwassist(struct ena_adapter *); 151 static void ena_setup_ifnet(device_t, struct ena_adapter *, 152 struct ena_com_dev_get_features_ctx *); 153 static int ena_enable_wc(device_t, struct resource *); 154 static int ena_set_queues_placement_policy(device_t, struct ena_com_dev *, 155 struct ena_admin_feature_llq_desc *, struct ena_llq_configurations *); 156 static int ena_map_llq_mem_bar(device_t, struct ena_com_dev *); 157 static uint32_t ena_calc_max_io_queue_num(device_t, struct ena_com_dev *, 158 struct ena_com_dev_get_features_ctx *); 159 static int ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *); 160 static void ena_config_host_info(struct ena_com_dev *, device_t); 161 static int ena_attach(device_t); 162 static int ena_detach(device_t); 163 static int ena_device_init(struct ena_adapter *, device_t, 164 struct ena_com_dev_get_features_ctx *, int *); 165 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *); 166 static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *); 167 static void unimplemented_aenq_handler(void *, struct ena_admin_aenq_entry *); 168 static int ena_copy_eni_metrics(struct ena_adapter *); 169 static int ena_copy_srd_metrics(struct ena_adapter *); 170 static int ena_copy_customer_metrics(struct ena_adapter *); 171 static void ena_timer_service(void *); 172 static enum ena_regs_reset_reason_types check_cdesc_in_tx_cq(struct ena_adapter *, 173 struct ena_ring *); 174 #ifdef DEV_NETMAP 175 static int ena_reinit_netmap(struct ena_adapter *adapter); 176 #endif 177 178 static char ena_version[] = ENA_DEVICE_NAME ENA_DRV_MODULE_NAME 179 " v" ENA_DRV_MODULE_VERSION; 180 181 static ena_vendor_info_t ena_vendor_info_array[] = { 182 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0 }, 183 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF_RSERV0, 0 }, 184 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0 }, 185 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF_RSERV0, 0 }, 186 /* Last entry */ 187 { 0, 0, 0 } 188 }; 189 190 struct sx ena_global_lock; 191 192 /* 193 * Contains pointers to event handlers, e.g. link state chage. 194 */ 195 static struct ena_aenq_handlers aenq_handlers; 196 197 void 198 ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error) 199 { 200 if (error != 0) 201 return; 202 *(bus_addr_t *)arg = segs[0].ds_addr; 203 } 204 205 int 206 ena_dma_alloc(device_t dmadev, bus_size_t size, ena_mem_handle_t *dma, 207 int mapflags, bus_size_t alignment, int domain) 208 { 209 struct ena_adapter *adapter = device_get_softc(dmadev); 210 device_t pdev = adapter->pdev; 211 uint32_t maxsize; 212 uint64_t dma_space_addr; 213 int error; 214 215 maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE; 216 217 dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width); 218 if (unlikely(dma_space_addr == 0)) 219 dma_space_addr = BUS_SPACE_MAXADDR; 220 221 error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */ 222 alignment, 0, /* alignment, bounds */ 223 dma_space_addr, /* lowaddr of exclusion window */ 224 BUS_SPACE_MAXADDR, /* highaddr of exclusion window */ 225 NULL, NULL, /* filter, filterarg */ 226 maxsize, /* maxsize */ 227 1, /* nsegments */ 228 maxsize, /* maxsegsize */ 229 BUS_DMA_ALLOCNOW, /* flags */ 230 NULL, /* lockfunc */ 231 NULL, /* lockarg */ 232 &dma->tag); 233 if (unlikely(error != 0)) { 234 ena_log(pdev, ERR, "bus_dma_tag_create failed: %d\n", error); 235 goto fail_tag; 236 } 237 238 error = bus_dma_tag_set_domain(dma->tag, domain); 239 if (unlikely(error != 0)) { 240 ena_log(pdev, ERR, "bus_dma_tag_set_domain failed: %d\n", 241 error); 242 goto fail_map_create; 243 } 244 245 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 246 BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map); 247 if (unlikely(error != 0)) { 248 ena_log(pdev, ERR, "bus_dmamem_alloc(%ju) failed: %d\n", 249 (uintmax_t)size, error); 250 goto fail_map_create; 251 } 252 253 dma->paddr = 0; 254 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size, 255 ena_dmamap_callback, &dma->paddr, mapflags); 256 if (unlikely((error != 0) || (dma->paddr == 0))) { 257 ena_log(pdev, ERR, "bus_dmamap_load failed: %d\n", error); 258 goto fail_map_load; 259 } 260 261 bus_dmamap_sync(dma->tag, dma->map, 262 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 263 264 return (0); 265 266 fail_map_load: 267 bus_dmamem_free(dma->tag, dma->vaddr, dma->map); 268 fail_map_create: 269 bus_dma_tag_destroy(dma->tag); 270 fail_tag: 271 dma->tag = NULL; 272 dma->vaddr = NULL; 273 dma->paddr = 0; 274 275 return (error); 276 } 277 278 static void 279 ena_free_pci_resources(struct ena_adapter *adapter) 280 { 281 device_t pdev = adapter->pdev; 282 283 if (adapter->memory != NULL) { 284 bus_release_resource(pdev, SYS_RES_MEMORY, 285 PCIR_BAR(ENA_MEM_BAR), adapter->memory); 286 } 287 288 if (adapter->registers != NULL) { 289 bus_release_resource(pdev, SYS_RES_MEMORY, 290 PCIR_BAR(ENA_REG_BAR), adapter->registers); 291 } 292 293 if (adapter->msix != NULL) { 294 bus_release_resource(pdev, SYS_RES_MEMORY, adapter->msix_rid, 295 adapter->msix); 296 } 297 } 298 299 static int 300 ena_probe(device_t dev) 301 { 302 ena_vendor_info_t *ent; 303 uint16_t pci_vendor_id = 0; 304 uint16_t pci_device_id = 0; 305 306 pci_vendor_id = pci_get_vendor(dev); 307 pci_device_id = pci_get_device(dev); 308 309 ent = ena_vendor_info_array; 310 while (ent->vendor_id != 0) { 311 if ((pci_vendor_id == ent->vendor_id) && 312 (pci_device_id == ent->device_id)) { 313 ena_log_raw(DBG, "vendor=%x device=%x\n", pci_vendor_id, 314 pci_device_id); 315 316 device_set_desc(dev, ENA_DEVICE_DESC); 317 return (BUS_PROBE_DEFAULT); 318 } 319 320 ent++; 321 } 322 323 return (ENXIO); 324 } 325 326 static int 327 ena_change_mtu(if_t ifp, int new_mtu) 328 { 329 struct ena_adapter *adapter = if_getsoftc(ifp); 330 device_t pdev = adapter->pdev; 331 int rc; 332 333 if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) { 334 ena_log(pdev, ERR, "Invalid MTU setting. new_mtu: %d max mtu: %d min mtu: %d\n", 335 new_mtu, adapter->max_mtu, ENA_MIN_MTU); 336 return (EINVAL); 337 } 338 339 rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu); 340 if (likely(rc == 0)) { 341 ena_log(pdev, DBG, "set MTU to %d\n", new_mtu); 342 if_setmtu(ifp, new_mtu); 343 } else { 344 ena_log(pdev, ERR, "Failed to set MTU to %d\n", new_mtu); 345 } 346 347 return (rc); 348 } 349 350 static inline void 351 ena_alloc_counters(counter_u64_t *begin, int size) 352 { 353 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 354 355 for (; begin < end; ++begin) 356 *begin = counter_u64_alloc(M_WAITOK); 357 } 358 359 static inline void 360 ena_free_counters(counter_u64_t *begin, int size) 361 { 362 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 363 364 for (; begin < end; ++begin) 365 counter_u64_free(*begin); 366 } 367 368 static inline void 369 ena_reset_counters(counter_u64_t *begin, int size) 370 { 371 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 372 373 for (; begin < end; ++begin) 374 counter_u64_zero(*begin); 375 } 376 377 static void 378 ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring, 379 uint16_t qid) 380 { 381 ring->qid = qid; 382 ring->adapter = adapter; 383 ring->ena_dev = adapter->ena_dev; 384 atomic_store_8(&ring->first_interrupt, 0); 385 ring->no_interrupt_event_cnt = 0; 386 } 387 388 static void 389 ena_init_io_rings_basic(struct ena_adapter *adapter) 390 { 391 struct ena_com_dev *ena_dev; 392 struct ena_ring *txr, *rxr; 393 struct ena_que *que; 394 int i; 395 396 ena_dev = adapter->ena_dev; 397 398 for (i = 0; i < adapter->num_io_queues; i++) { 399 txr = &adapter->tx_ring[i]; 400 rxr = &adapter->rx_ring[i]; 401 402 /* TX/RX common ring state */ 403 ena_init_io_rings_common(adapter, txr, i); 404 ena_init_io_rings_common(adapter, rxr, i); 405 406 /* TX specific ring state */ 407 txr->tx_max_header_size = ena_dev->tx_max_header_size; 408 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type; 409 410 que = &adapter->que[i]; 411 que->adapter = adapter; 412 que->id = i; 413 que->tx_ring = txr; 414 que->rx_ring = rxr; 415 416 txr->que = que; 417 rxr->que = que; 418 419 rxr->empty_rx_queue = 0; 420 rxr->rx_mbuf_sz = ena_mbuf_sz; 421 } 422 } 423 424 static void 425 ena_init_io_rings_advanced(struct ena_adapter *adapter) 426 { 427 struct ena_ring *txr, *rxr; 428 int i; 429 430 for (i = 0; i < adapter->num_io_queues; i++) { 431 txr = &adapter->tx_ring[i]; 432 rxr = &adapter->rx_ring[i]; 433 434 /* Allocate a buf ring */ 435 txr->buf_ring_size = adapter->buf_ring_size; 436 txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF, M_WAITOK, 437 &txr->ring_mtx); 438 439 /* Allocate Tx statistics. */ 440 ena_alloc_counters((counter_u64_t *)&txr->tx_stats, 441 sizeof(txr->tx_stats)); 442 txr->tx_last_cleanup_ticks = ticks; 443 444 /* Allocate Rx statistics. */ 445 ena_alloc_counters((counter_u64_t *)&rxr->rx_stats, 446 sizeof(rxr->rx_stats)); 447 448 /* Initialize locks */ 449 snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)", 450 device_get_nameunit(adapter->pdev), i); 451 snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)", 452 device_get_nameunit(adapter->pdev), i); 453 454 mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF); 455 } 456 } 457 458 static void 459 ena_init_io_rings(struct ena_adapter *adapter) 460 { 461 /* 462 * IO rings initialization can be divided into the 2 steps: 463 * 1. Initialize variables and fields with initial values and copy 464 * them from adapter/ena_dev (basic) 465 * 2. Allocate mutex, counters and buf_ring (advanced) 466 */ 467 ena_init_io_rings_basic(adapter); 468 ena_init_io_rings_advanced(adapter); 469 } 470 471 static void 472 ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid) 473 { 474 struct ena_ring *txr = &adapter->tx_ring[qid]; 475 struct ena_ring *rxr = &adapter->rx_ring[qid]; 476 477 ena_free_counters((counter_u64_t *)&txr->tx_stats, 478 sizeof(txr->tx_stats)); 479 ena_free_counters((counter_u64_t *)&rxr->rx_stats, 480 sizeof(rxr->rx_stats)); 481 482 ENA_RING_MTX_LOCK(txr); 483 drbr_free(txr->br, M_DEVBUF); 484 ENA_RING_MTX_UNLOCK(txr); 485 486 mtx_destroy(&txr->ring_mtx); 487 } 488 489 static void 490 ena_free_all_io_rings_resources(struct ena_adapter *adapter) 491 { 492 int i; 493 494 for (i = 0; i < adapter->num_io_queues; i++) 495 ena_free_io_ring_resources(adapter, i); 496 } 497 498 static int 499 ena_setup_tx_dma_tag(struct ena_adapter *adapter) 500 { 501 int ret; 502 503 /* Create DMA tag for Tx buffers */ 504 ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), 505 1, 0, /* alignment, bounds */ 506 ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window */ 507 BUS_SPACE_MAXADDR, /* highaddr of excl window */ 508 NULL, NULL, /* filter, filterarg */ 509 ENA_TSO_MAXSIZE, /* maxsize */ 510 adapter->max_tx_sgl_size - 1, /* nsegments */ 511 ENA_TSO_MAXSIZE, /* maxsegsize */ 512 0, /* flags */ 513 NULL, /* lockfunc */ 514 NULL, /* lockfuncarg */ 515 &adapter->tx_buf_tag); 516 517 return (ret); 518 } 519 520 static int 521 ena_free_tx_dma_tag(struct ena_adapter *adapter) 522 { 523 int ret; 524 525 ret = bus_dma_tag_destroy(adapter->tx_buf_tag); 526 527 if (likely(ret == 0)) 528 adapter->tx_buf_tag = NULL; 529 530 return (ret); 531 } 532 533 static int 534 ena_setup_rx_dma_tag(struct ena_adapter *adapter) 535 { 536 int ret; 537 538 /* Create DMA tag for Rx buffers*/ 539 ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent */ 540 1, 0, /* alignment, bounds */ 541 ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window */ 542 BUS_SPACE_MAXADDR, /* highaddr of excl window */ 543 NULL, NULL, /* filter, filterarg */ 544 ena_mbuf_sz, /* maxsize */ 545 adapter->max_rx_sgl_size, /* nsegments */ 546 ena_mbuf_sz, /* maxsegsize */ 547 0, /* flags */ 548 NULL, /* lockfunc */ 549 NULL, /* lockarg */ 550 &adapter->rx_buf_tag); 551 552 return (ret); 553 } 554 555 static int 556 ena_free_rx_dma_tag(struct ena_adapter *adapter) 557 { 558 int ret; 559 560 ret = bus_dma_tag_destroy(adapter->rx_buf_tag); 561 562 if (likely(ret == 0)) 563 adapter->rx_buf_tag = NULL; 564 565 return (ret); 566 } 567 568 int 569 validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id, int tx_req_id_rc) 570 { 571 struct ena_adapter *adapter = tx_ring->adapter; 572 enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID; 573 574 if (unlikely(tx_req_id_rc != 0)) { 575 if (tx_req_id_rc == ENA_COM_FAULT) { 576 reset_reason = ENA_REGS_RESET_TX_DESCRIPTOR_MALFORMED; 577 ena_log(adapter->pdev, ERR, 578 "TX descriptor malformed. req_id %hu qid %hu\n", 579 req_id, tx_ring->qid); 580 } else if (tx_req_id_rc == ENA_COM_INVAL) { 581 ena_log_nm(adapter->pdev, WARN, 582 "Invalid req_id %hu in qid %hu\n", 583 req_id, tx_ring->qid); 584 counter_u64_add(tx_ring->tx_stats.bad_req_id, 1); 585 } 586 587 ena_trigger_reset(adapter, reset_reason); 588 return (EFAULT); 589 } 590 591 return (0); 592 } 593 594 static void 595 ena_release_all_tx_dmamap(struct ena_ring *tx_ring) 596 { 597 struct ena_adapter *adapter = tx_ring->adapter; 598 struct ena_tx_buffer *tx_info; 599 bus_dma_tag_t tx_tag = adapter->tx_buf_tag; 600 int i; 601 #ifdef DEV_NETMAP 602 struct ena_netmap_tx_info *nm_info; 603 int j; 604 #endif /* DEV_NETMAP */ 605 606 for (i = 0; i < tx_ring->ring_size; ++i) { 607 tx_info = &tx_ring->tx_buffer_info[i]; 608 #ifdef DEV_NETMAP 609 if (if_getcapenable(adapter->ifp) & IFCAP_NETMAP) { 610 nm_info = &tx_info->nm_info; 611 for (j = 0; j < ENA_PKT_MAX_BUFS; ++j) { 612 if (nm_info->map_seg[j] != NULL) { 613 bus_dmamap_destroy(tx_tag, 614 nm_info->map_seg[j]); 615 nm_info->map_seg[j] = NULL; 616 } 617 } 618 } 619 #endif /* DEV_NETMAP */ 620 if (tx_info->dmamap != NULL) { 621 bus_dmamap_destroy(tx_tag, tx_info->dmamap); 622 tx_info->dmamap = NULL; 623 } 624 } 625 } 626 627 /** 628 * ena_setup_tx_resources - allocate Tx resources (Descriptors) 629 * @adapter: network interface device structure 630 * @qid: queue index 631 * 632 * Returns 0 on success, otherwise on failure. 633 **/ 634 static int 635 ena_setup_tx_resources(struct ena_adapter *adapter, int qid) 636 { 637 device_t pdev = adapter->pdev; 638 char thread_name[MAXCOMLEN + 1]; 639 struct ena_que *que = &adapter->que[qid]; 640 struct ena_ring *tx_ring = que->tx_ring; 641 cpuset_t *cpu_mask = NULL; 642 int size, i, err; 643 #ifdef DEV_NETMAP 644 bus_dmamap_t *map; 645 int j; 646 647 ena_netmap_reset_tx_ring(adapter, qid); 648 #endif /* DEV_NETMAP */ 649 650 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size; 651 652 tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 653 if (unlikely(tx_ring->tx_buffer_info == NULL)) 654 return (ENOMEM); 655 656 size = sizeof(uint16_t) * tx_ring->ring_size; 657 tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 658 if (unlikely(tx_ring->free_tx_ids == NULL)) 659 goto err_buf_info_free; 660 661 size = tx_ring->tx_max_header_size; 662 tx_ring->push_buf_intermediate_buf = malloc(size, M_DEVBUF, 663 M_NOWAIT | M_ZERO); 664 if (unlikely(tx_ring->push_buf_intermediate_buf == NULL)) 665 goto err_tx_ids_free; 666 667 /* Req id stack for TX OOO completions */ 668 for (i = 0; i < tx_ring->ring_size; i++) 669 tx_ring->free_tx_ids[i] = i; 670 671 /* Reset TX statistics. */ 672 ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats, 673 sizeof(tx_ring->tx_stats)); 674 675 tx_ring->next_to_use = 0; 676 tx_ring->next_to_clean = 0; 677 tx_ring->acum_pkts = 0; 678 679 /* Make sure that drbr is empty */ 680 ENA_RING_MTX_LOCK(tx_ring); 681 drbr_flush(adapter->ifp, tx_ring->br); 682 ENA_RING_MTX_UNLOCK(tx_ring); 683 684 /* ... and create the buffer DMA maps */ 685 for (i = 0; i < tx_ring->ring_size; i++) { 686 err = bus_dmamap_create(adapter->tx_buf_tag, 0, 687 &tx_ring->tx_buffer_info[i].dmamap); 688 if (unlikely(err != 0)) { 689 ena_log(pdev, ERR, 690 "Unable to create Tx DMA map for buffer %d\n", i); 691 goto err_map_release; 692 } 693 694 #ifdef DEV_NETMAP 695 if (if_getcapenable(adapter->ifp) & IFCAP_NETMAP) { 696 map = tx_ring->tx_buffer_info[i].nm_info.map_seg; 697 for (j = 0; j < ENA_PKT_MAX_BUFS; j++) { 698 err = bus_dmamap_create(adapter->tx_buf_tag, 0, 699 &map[j]); 700 if (unlikely(err != 0)) { 701 ena_log(pdev, ERR, 702 "Unable to create Tx DMA for buffer %d %d\n", 703 i, j); 704 goto err_map_release; 705 } 706 } 707 } 708 #endif /* DEV_NETMAP */ 709 } 710 711 /* Allocate taskqueues */ 712 TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring); 713 tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT, 714 taskqueue_thread_enqueue, &tx_ring->enqueue_tq); 715 if (unlikely(tx_ring->enqueue_tq == NULL)) { 716 ena_log(pdev, ERR, 717 "Unable to create taskqueue for enqueue task\n"); 718 i = tx_ring->ring_size; 719 goto err_map_release; 720 } 721 722 tx_ring->running = true; 723 724 #ifdef RSS 725 cpu_mask = &que->cpu_mask; 726 snprintf(thread_name, sizeof(thread_name), "%s txeq %d", 727 device_get_nameunit(adapter->pdev), que->cpu); 728 #else 729 snprintf(thread_name, sizeof(thread_name), "%s txeq %d", 730 device_get_nameunit(adapter->pdev), que->id); 731 #endif 732 taskqueue_start_threads_cpuset(&tx_ring->enqueue_tq, 1, PI_NET, 733 cpu_mask, "%s", thread_name); 734 735 return (0); 736 737 err_map_release: 738 ena_release_all_tx_dmamap(tx_ring); 739 err_tx_ids_free: 740 free(tx_ring->free_tx_ids, M_DEVBUF); 741 tx_ring->free_tx_ids = NULL; 742 err_buf_info_free: 743 free(tx_ring->tx_buffer_info, M_DEVBUF); 744 tx_ring->tx_buffer_info = NULL; 745 746 return (ENOMEM); 747 } 748 749 /** 750 * ena_free_tx_resources - Free Tx Resources per Queue 751 * @adapter: network interface device structure 752 * @qid: queue index 753 * 754 * Free all transmit software resources 755 **/ 756 static void 757 ena_free_tx_resources(struct ena_adapter *adapter, int qid) 758 { 759 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 760 #ifdef DEV_NETMAP 761 struct ena_netmap_tx_info *nm_info; 762 int j; 763 #endif /* DEV_NETMAP */ 764 765 while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task, NULL)) 766 taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task); 767 768 taskqueue_free(tx_ring->enqueue_tq); 769 770 ENA_RING_MTX_LOCK(tx_ring); 771 /* Flush buffer ring, */ 772 drbr_flush(adapter->ifp, tx_ring->br); 773 774 /* Free buffer DMA maps, */ 775 for (int i = 0; i < tx_ring->ring_size; i++) { 776 bus_dmamap_sync(adapter->tx_buf_tag, 777 tx_ring->tx_buffer_info[i].dmamap, BUS_DMASYNC_POSTWRITE); 778 bus_dmamap_unload(adapter->tx_buf_tag, 779 tx_ring->tx_buffer_info[i].dmamap); 780 bus_dmamap_destroy(adapter->tx_buf_tag, 781 tx_ring->tx_buffer_info[i].dmamap); 782 783 #ifdef DEV_NETMAP 784 if (if_getcapenable(adapter->ifp) & IFCAP_NETMAP) { 785 nm_info = &tx_ring->tx_buffer_info[i].nm_info; 786 for (j = 0; j < ENA_PKT_MAX_BUFS; j++) { 787 if (nm_info->socket_buf_idx[j] != 0) { 788 bus_dmamap_sync(adapter->tx_buf_tag, 789 nm_info->map_seg[j], 790 BUS_DMASYNC_POSTWRITE); 791 ena_netmap_unload(adapter, 792 nm_info->map_seg[j]); 793 } 794 bus_dmamap_destroy(adapter->tx_buf_tag, 795 nm_info->map_seg[j]); 796 nm_info->socket_buf_idx[j] = 0; 797 } 798 } 799 #endif /* DEV_NETMAP */ 800 801 m_freem(tx_ring->tx_buffer_info[i].mbuf); 802 tx_ring->tx_buffer_info[i].mbuf = NULL; 803 } 804 ENA_RING_MTX_UNLOCK(tx_ring); 805 806 /* And free allocated memory. */ 807 free(tx_ring->tx_buffer_info, M_DEVBUF); 808 tx_ring->tx_buffer_info = NULL; 809 810 free(tx_ring->free_tx_ids, M_DEVBUF); 811 tx_ring->free_tx_ids = NULL; 812 813 free(tx_ring->push_buf_intermediate_buf, M_DEVBUF); 814 tx_ring->push_buf_intermediate_buf = NULL; 815 } 816 817 /** 818 * ena_setup_all_tx_resources - allocate all queues Tx resources 819 * @adapter: network interface device structure 820 * 821 * Returns 0 on success, otherwise on failure. 822 **/ 823 static int 824 ena_setup_all_tx_resources(struct ena_adapter *adapter) 825 { 826 int i, rc; 827 828 for (i = 0; i < adapter->num_io_queues; i++) { 829 rc = ena_setup_tx_resources(adapter, i); 830 if (rc != 0) { 831 ena_log(adapter->pdev, ERR, 832 "Allocation for Tx Queue %u failed\n", i); 833 goto err_setup_tx; 834 } 835 } 836 837 return (0); 838 839 err_setup_tx: 840 /* Rewind the index freeing the rings as we go */ 841 while (i--) 842 ena_free_tx_resources(adapter, i); 843 return (rc); 844 } 845 846 /** 847 * ena_free_all_tx_resources - Free Tx Resources for All Queues 848 * @adapter: network interface device structure 849 * 850 * Free all transmit software resources 851 **/ 852 static void 853 ena_free_all_tx_resources(struct ena_adapter *adapter) 854 { 855 int i; 856 857 for (i = 0; i < adapter->num_io_queues; i++) 858 ena_free_tx_resources(adapter, i); 859 } 860 861 /** 862 * ena_setup_rx_resources - allocate Rx resources (Descriptors) 863 * @adapter: network interface device structure 864 * @qid: queue index 865 * 866 * Returns 0 on success, otherwise on failure. 867 **/ 868 static int 869 ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid) 870 { 871 device_t pdev = adapter->pdev; 872 struct ena_que *que = &adapter->que[qid]; 873 struct ena_ring *rx_ring = que->rx_ring; 874 int size, err, i; 875 876 size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size; 877 878 #ifdef DEV_NETMAP 879 ena_netmap_reset_rx_ring(adapter, qid); 880 rx_ring->initialized = false; 881 #endif /* DEV_NETMAP */ 882 883 /* 884 * Alloc extra element so in rx path 885 * we can always prefetch rx_info + 1 886 */ 887 size += sizeof(struct ena_rx_buffer); 888 889 rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO); 890 891 size = sizeof(uint16_t) * rx_ring->ring_size; 892 rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK); 893 894 for (i = 0; i < rx_ring->ring_size; i++) 895 rx_ring->free_rx_ids[i] = i; 896 897 /* Reset RX statistics. */ 898 ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats, 899 sizeof(rx_ring->rx_stats)); 900 901 rx_ring->next_to_clean = 0; 902 rx_ring->next_to_use = 0; 903 904 /* ... and create the buffer DMA maps */ 905 for (i = 0; i < rx_ring->ring_size; i++) { 906 err = bus_dmamap_create(adapter->rx_buf_tag, 0, 907 &(rx_ring->rx_buffer_info[i].map)); 908 if (err != 0) { 909 ena_log(pdev, ERR, 910 "Unable to create Rx DMA map for buffer %d\n", i); 911 goto err_buf_info_unmap; 912 } 913 } 914 915 /* Create LRO for the ring */ 916 if ((if_getcapenable(adapter->ifp) & IFCAP_LRO) != 0) { 917 int err = tcp_lro_init(&rx_ring->lro); 918 if (err != 0) { 919 ena_log(pdev, ERR, "LRO[%d] Initialization failed!\n", 920 qid); 921 } else { 922 ena_log(pdev, DBG, "RX Soft LRO[%d] Initialized\n", 923 qid); 924 rx_ring->lro.ifp = adapter->ifp; 925 } 926 } 927 928 return (0); 929 930 err_buf_info_unmap: 931 while (i--) { 932 bus_dmamap_destroy(adapter->rx_buf_tag, 933 rx_ring->rx_buffer_info[i].map); 934 } 935 936 free(rx_ring->free_rx_ids, M_DEVBUF); 937 rx_ring->free_rx_ids = NULL; 938 free(rx_ring->rx_buffer_info, M_DEVBUF); 939 rx_ring->rx_buffer_info = NULL; 940 return (ENOMEM); 941 } 942 943 /** 944 * ena_free_rx_resources - Free Rx Resources 945 * @adapter: network interface device structure 946 * @qid: queue index 947 * 948 * Free all receive software resources 949 **/ 950 static void 951 ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid) 952 { 953 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 954 955 /* Free buffer DMA maps, */ 956 for (int i = 0; i < rx_ring->ring_size; i++) { 957 bus_dmamap_sync(adapter->rx_buf_tag, 958 rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD); 959 m_freem(rx_ring->rx_buffer_info[i].mbuf); 960 rx_ring->rx_buffer_info[i].mbuf = NULL; 961 bus_dmamap_unload(adapter->rx_buf_tag, 962 rx_ring->rx_buffer_info[i].map); 963 bus_dmamap_destroy(adapter->rx_buf_tag, 964 rx_ring->rx_buffer_info[i].map); 965 } 966 967 /* free LRO resources, */ 968 tcp_lro_free(&rx_ring->lro); 969 970 /* free allocated memory */ 971 free(rx_ring->rx_buffer_info, M_DEVBUF); 972 rx_ring->rx_buffer_info = NULL; 973 974 free(rx_ring->free_rx_ids, M_DEVBUF); 975 rx_ring->free_rx_ids = NULL; 976 } 977 978 /** 979 * ena_setup_all_rx_resources - allocate all queues Rx resources 980 * @adapter: network interface device structure 981 * 982 * Returns 0 on success, otherwise on failure. 983 **/ 984 static int 985 ena_setup_all_rx_resources(struct ena_adapter *adapter) 986 { 987 int i, rc = 0; 988 989 for (i = 0; i < adapter->num_io_queues; i++) { 990 rc = ena_setup_rx_resources(adapter, i); 991 if (rc != 0) { 992 ena_log(adapter->pdev, ERR, 993 "Allocation for Rx Queue %u failed\n", i); 994 goto err_setup_rx; 995 } 996 } 997 return (0); 998 999 err_setup_rx: 1000 /* rewind the index freeing the rings as we go */ 1001 while (i--) 1002 ena_free_rx_resources(adapter, i); 1003 return (rc); 1004 } 1005 1006 /** 1007 * ena_free_all_rx_resources - Free Rx resources for all queues 1008 * @adapter: network interface device structure 1009 * 1010 * Free all receive software resources 1011 **/ 1012 static void 1013 ena_free_all_rx_resources(struct ena_adapter *adapter) 1014 { 1015 int i; 1016 1017 for (i = 0; i < adapter->num_io_queues; i++) 1018 ena_free_rx_resources(adapter, i); 1019 } 1020 1021 static inline int 1022 ena_alloc_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring, 1023 struct ena_rx_buffer *rx_info) 1024 { 1025 device_t pdev = adapter->pdev; 1026 struct ena_com_buf *ena_buf; 1027 bus_dma_segment_t segs[1]; 1028 int nsegs, error; 1029 int mlen; 1030 1031 /* if previous allocated frag is not used */ 1032 if (unlikely(rx_info->mbuf != NULL)) 1033 return (0); 1034 1035 /* Get mbuf using UMA allocator */ 1036 rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 1037 rx_ring->rx_mbuf_sz); 1038 1039 if (unlikely(rx_info->mbuf == NULL)) { 1040 counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1); 1041 rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1042 if (unlikely(rx_info->mbuf == NULL)) { 1043 counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1); 1044 return (ENOMEM); 1045 } 1046 mlen = MCLBYTES; 1047 } else { 1048 mlen = rx_ring->rx_mbuf_sz; 1049 } 1050 /* Set mbuf length*/ 1051 rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen; 1052 1053 /* Map packets for DMA */ 1054 ena_log(pdev, DBG, 1055 "Using tag %p for buffers' DMA mapping, mbuf %p len: %d\n", 1056 adapter->rx_buf_tag, rx_info->mbuf, rx_info->mbuf->m_len); 1057 error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map, 1058 rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT); 1059 if (unlikely((error != 0) || (nsegs != 1))) { 1060 ena_log(pdev, WARN, 1061 "failed to map mbuf, error: %d, nsegs: %d\n", error, nsegs); 1062 counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1); 1063 goto exit; 1064 } 1065 1066 bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD); 1067 1068 ena_buf = &rx_info->ena_buf; 1069 ena_buf->paddr = segs[0].ds_addr; 1070 ena_buf->len = mlen; 1071 1072 ena_log(pdev, DBG, 1073 "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n", 1074 rx_info->mbuf, rx_info, ena_buf->len, (uintmax_t)ena_buf->paddr); 1075 1076 return (0); 1077 1078 exit: 1079 m_freem(rx_info->mbuf); 1080 rx_info->mbuf = NULL; 1081 return (EFAULT); 1082 } 1083 1084 static void 1085 ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring, 1086 struct ena_rx_buffer *rx_info) 1087 { 1088 if (rx_info->mbuf == NULL) { 1089 ena_log(adapter->pdev, WARN, 1090 "Trying to free unallocated buffer\n"); 1091 return; 1092 } 1093 1094 bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, 1095 BUS_DMASYNC_POSTREAD); 1096 bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map); 1097 m_freem(rx_info->mbuf); 1098 rx_info->mbuf = NULL; 1099 } 1100 1101 /** 1102 * ena_refill_rx_bufs - Refills ring with descriptors 1103 * @rx_ring: the ring which we want to feed with free descriptors 1104 * @num: number of descriptors to refill 1105 * Refills the ring with newly allocated DMA-mapped mbufs for receiving 1106 **/ 1107 int 1108 ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num) 1109 { 1110 struct ena_adapter *adapter = rx_ring->adapter; 1111 device_t pdev = adapter->pdev; 1112 uint16_t next_to_use, req_id; 1113 uint32_t i; 1114 int rc; 1115 1116 ena_log_io(adapter->pdev, DBG, "refill qid: %d\n", rx_ring->qid); 1117 1118 next_to_use = rx_ring->next_to_use; 1119 1120 for (i = 0; i < num; i++) { 1121 struct ena_rx_buffer *rx_info; 1122 1123 ena_log_io(pdev, DBG, "RX buffer - next to use: %d\n", 1124 next_to_use); 1125 1126 req_id = rx_ring->free_rx_ids[next_to_use]; 1127 rx_info = &rx_ring->rx_buffer_info[req_id]; 1128 #ifdef DEV_NETMAP 1129 if (ena_rx_ring_in_netmap(adapter, rx_ring->qid)) 1130 rc = ena_netmap_alloc_rx_slot(adapter, rx_ring, 1131 rx_info); 1132 else 1133 #endif /* DEV_NETMAP */ 1134 rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info); 1135 if (unlikely(rc != 0)) { 1136 ena_log_io(pdev, WARN, 1137 "failed to alloc buffer for rx queue %d\n", 1138 rx_ring->qid); 1139 break; 1140 } 1141 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq, 1142 &rx_info->ena_buf, req_id); 1143 if (unlikely(rc != 0)) { 1144 ena_log_io(pdev, WARN, 1145 "failed to add buffer for rx queue %d\n", 1146 rx_ring->qid); 1147 break; 1148 } 1149 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use, 1150 rx_ring->ring_size); 1151 } 1152 1153 if (unlikely(i < num)) { 1154 counter_u64_add(rx_ring->rx_stats.refil_partial, 1); 1155 ena_log_io(pdev, WARN, 1156 "refilled rx qid %d with only %d mbufs (from %d)\n", 1157 rx_ring->qid, i, num); 1158 } 1159 1160 if (likely(i != 0)) 1161 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq); 1162 1163 rx_ring->next_to_use = next_to_use; 1164 return (i); 1165 } 1166 1167 #ifdef DEV_NETMAP 1168 static int 1169 ena_reinit_netmap(struct ena_adapter *adapter) 1170 { 1171 int rc; 1172 1173 netmap_detach(adapter->ifp); 1174 rc = ena_netmap_attach(adapter); 1175 if (rc != 0) 1176 ena_log(adapter->pdev, ERR, "netmap attach failed: %d\n", rc); 1177 1178 return rc; 1179 } 1180 1181 #endif /* DEV_NETMAP */ 1182 int 1183 ena_update_buf_ring_size(struct ena_adapter *adapter, 1184 uint32_t new_buf_ring_size) 1185 { 1186 uint32_t old_buf_ring_size; 1187 int rc = 0; 1188 bool dev_was_up; 1189 1190 old_buf_ring_size = adapter->buf_ring_size; 1191 adapter->buf_ring_size = new_buf_ring_size; 1192 1193 dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 1194 ena_down(adapter); 1195 1196 /* Reconfigure buf ring for all Tx rings. */ 1197 ena_free_all_io_rings_resources(adapter); 1198 ena_init_io_rings_advanced(adapter); 1199 #ifdef DEV_NETMAP 1200 rc = ena_reinit_netmap(adapter); 1201 if (rc != 0) 1202 return rc; 1203 1204 #endif /* DEV_NETMAP */ 1205 if (dev_was_up) { 1206 /* 1207 * If ena_up() fails, it's not because of recent buf_ring size 1208 * changes. Because of that, we just want to revert old drbr 1209 * value and trigger the reset because something else had to 1210 * go wrong. 1211 */ 1212 rc = ena_up(adapter); 1213 if (unlikely(rc != 0)) { 1214 ena_log(adapter->pdev, ERR, 1215 "Failed to configure device after setting new drbr size: %u. Reverting old value: %u and triggering the reset\n", 1216 new_buf_ring_size, old_buf_ring_size); 1217 1218 /* Revert old size and trigger the reset */ 1219 adapter->buf_ring_size = old_buf_ring_size; 1220 ena_free_all_io_rings_resources(adapter); 1221 ena_init_io_rings_advanced(adapter); 1222 #ifdef DEV_NETMAP 1223 rc = ena_reinit_netmap(adapter); 1224 if (rc != 0) 1225 return rc; 1226 1227 #endif /* DEV_NETMAP */ 1228 ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, 1229 adapter); 1230 ena_trigger_reset(adapter, ENA_REGS_RESET_OS_TRIGGER); 1231 } 1232 } 1233 1234 return (rc); 1235 } 1236 1237 int 1238 ena_update_queue_size(struct ena_adapter *adapter, uint32_t new_tx_size, 1239 uint32_t new_rx_size) 1240 { 1241 uint32_t old_tx_size, old_rx_size; 1242 int rc = 0; 1243 bool dev_was_up; 1244 1245 old_tx_size = adapter->requested_tx_ring_size; 1246 old_rx_size = adapter->requested_rx_ring_size; 1247 adapter->requested_tx_ring_size = new_tx_size; 1248 adapter->requested_rx_ring_size = new_rx_size; 1249 1250 dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 1251 ena_down(adapter); 1252 1253 /* Configure queues with new size. */ 1254 ena_init_io_rings_basic(adapter); 1255 #ifdef DEV_NETMAP 1256 rc = ena_reinit_netmap(adapter); 1257 if (rc != 0) 1258 return rc; 1259 1260 #endif /* DEV_NETMAP */ 1261 if (dev_was_up) { 1262 rc = ena_up(adapter); 1263 if (unlikely(rc != 0)) { 1264 ena_log(adapter->pdev, ERR, 1265 "Failed to configure device with the new sizes - Tx: %u Rx: %u. Reverting old values - Tx: %u Rx: %u\n", 1266 new_tx_size, new_rx_size, old_tx_size, old_rx_size); 1267 1268 /* Revert old size. */ 1269 adapter->requested_tx_ring_size = old_tx_size; 1270 adapter->requested_rx_ring_size = old_rx_size; 1271 ena_init_io_rings_basic(adapter); 1272 #ifdef DEV_NETMAP 1273 rc = ena_reinit_netmap(adapter); 1274 if (rc != 0) 1275 return rc; 1276 1277 #endif /* DEV_NETMAP */ 1278 /* And try again. */ 1279 rc = ena_up(adapter); 1280 if (unlikely(rc != 0)) { 1281 ena_log(adapter->pdev, ERR, 1282 "Failed to revert old queue sizes. Triggering device reset.\n"); 1283 /* 1284 * If we've failed again, something had to go 1285 * wrong. After reset, the device should try to 1286 * go up 1287 */ 1288 ENA_FLAG_SET_ATOMIC( 1289 ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 1290 ena_trigger_reset(adapter, 1291 ENA_REGS_RESET_OS_TRIGGER); 1292 } 1293 } 1294 } 1295 1296 return (rc); 1297 } 1298 1299 static void 1300 ena_update_io_rings(struct ena_adapter *adapter, uint32_t num) 1301 { 1302 ena_free_all_io_rings_resources(adapter); 1303 /* Force indirection table to be reinitialized */ 1304 ena_com_rss_destroy(adapter->ena_dev); 1305 1306 adapter->num_io_queues = num; 1307 ena_init_io_rings(adapter); 1308 } 1309 1310 int 1311 ena_update_base_cpu(struct ena_adapter *adapter, int new_num) 1312 { 1313 int old_num; 1314 int rc = 0; 1315 bool dev_was_up; 1316 1317 dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 1318 old_num = adapter->irq_cpu_base; 1319 1320 ena_down(adapter); 1321 1322 adapter->irq_cpu_base = new_num; 1323 1324 if (dev_was_up) { 1325 rc = ena_up(adapter); 1326 if (unlikely(rc != 0)) { 1327 ena_log(adapter->pdev, ERR, 1328 "Failed to configure device %d IRQ base CPU. " 1329 "Reverting to previous value: %d\n", 1330 new_num, old_num); 1331 1332 adapter->irq_cpu_base = old_num; 1333 1334 rc = ena_up(adapter); 1335 if (unlikely(rc != 0)) { 1336 ena_log(adapter->pdev, ERR, 1337 "Failed to revert to previous setup." 1338 "Triggering device reset.\n"); 1339 ENA_FLAG_SET_ATOMIC( 1340 ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 1341 ena_trigger_reset(adapter, 1342 ENA_REGS_RESET_OS_TRIGGER); 1343 } 1344 } 1345 } 1346 return (rc); 1347 } 1348 1349 int 1350 ena_update_cpu_stride(struct ena_adapter *adapter, uint32_t new_num) 1351 { 1352 uint32_t old_num; 1353 int rc = 0; 1354 bool dev_was_up; 1355 1356 dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 1357 old_num = adapter->irq_cpu_stride; 1358 1359 ena_down(adapter); 1360 1361 adapter->irq_cpu_stride = new_num; 1362 1363 if (dev_was_up) { 1364 rc = ena_up(adapter); 1365 if (unlikely(rc != 0)) { 1366 ena_log(adapter->pdev, ERR, 1367 "Failed to configure device %d IRQ CPU stride. " 1368 "Reverting to previous value: %d\n", 1369 new_num, old_num); 1370 1371 adapter->irq_cpu_stride = old_num; 1372 1373 rc = ena_up(adapter); 1374 if (unlikely(rc != 0)) { 1375 ena_log(adapter->pdev, ERR, 1376 "Failed to revert to previous setup." 1377 "Triggering device reset.\n"); 1378 ENA_FLAG_SET_ATOMIC( 1379 ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 1380 ena_trigger_reset(adapter, 1381 ENA_REGS_RESET_OS_TRIGGER); 1382 } 1383 } 1384 } 1385 return (rc); 1386 } 1387 1388 /* Caller should sanitize new_num */ 1389 int 1390 ena_update_io_queue_nb(struct ena_adapter *adapter, uint32_t new_num) 1391 { 1392 uint32_t old_num; 1393 int rc = 0; 1394 bool dev_was_up; 1395 1396 dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 1397 old_num = adapter->num_io_queues; 1398 ena_down(adapter); 1399 1400 ena_update_io_rings(adapter, new_num); 1401 #ifdef DEV_NETMAP 1402 rc = ena_reinit_netmap(adapter); 1403 if (rc != 0) 1404 return rc; 1405 1406 #endif /* DEV_NETMAP */ 1407 if (dev_was_up) { 1408 rc = ena_up(adapter); 1409 if (unlikely(rc != 0)) { 1410 ena_log(adapter->pdev, ERR, 1411 "Failed to configure device with %u IO queues. " 1412 "Reverting to previous value: %u\n", 1413 new_num, old_num); 1414 1415 ena_update_io_rings(adapter, old_num); 1416 #ifdef DEV_NETMAP 1417 rc = ena_reinit_netmap(adapter); 1418 if (rc != 0) 1419 return rc; 1420 1421 #endif /* DEV_NETMAP */ 1422 rc = ena_up(adapter); 1423 if (unlikely(rc != 0)) { 1424 ena_log(adapter->pdev, ERR, 1425 "Failed to revert to previous setup IO " 1426 "queues. Triggering device reset.\n"); 1427 ENA_FLAG_SET_ATOMIC( 1428 ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 1429 ena_trigger_reset(adapter, 1430 ENA_REGS_RESET_OS_TRIGGER); 1431 } 1432 } 1433 } 1434 1435 return (rc); 1436 } 1437 1438 static void 1439 ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid) 1440 { 1441 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 1442 unsigned int i; 1443 1444 for (i = 0; i < rx_ring->ring_size; i++) { 1445 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i]; 1446 1447 if (rx_info->mbuf != NULL) 1448 ena_free_rx_mbuf(adapter, rx_ring, rx_info); 1449 #ifdef DEV_NETMAP 1450 if (((if_getflags(adapter->ifp) & IFF_DYING) == 0) && 1451 (if_getcapenable(adapter->ifp) & IFCAP_NETMAP)) { 1452 if (rx_info->netmap_buf_idx != 0) 1453 ena_netmap_free_rx_slot(adapter, rx_ring, 1454 rx_info); 1455 } 1456 #endif /* DEV_NETMAP */ 1457 } 1458 } 1459 1460 /** 1461 * ena_refill_all_rx_bufs - allocate all queues Rx buffers 1462 * @adapter: network interface device structure 1463 * 1464 */ 1465 static void 1466 ena_refill_all_rx_bufs(struct ena_adapter *adapter) 1467 { 1468 struct ena_ring *rx_ring; 1469 int i, rc, bufs_num; 1470 1471 for (i = 0; i < adapter->num_io_queues; i++) { 1472 rx_ring = &adapter->rx_ring[i]; 1473 bufs_num = rx_ring->ring_size - 1; 1474 rc = ena_refill_rx_bufs(rx_ring, bufs_num); 1475 if (unlikely(rc != bufs_num)) 1476 ena_log_io(adapter->pdev, WARN, 1477 "refilling Queue %d failed. " 1478 "Allocated %d buffers from: %d\n", 1479 i, rc, bufs_num); 1480 #ifdef DEV_NETMAP 1481 rx_ring->initialized = true; 1482 #endif /* DEV_NETMAP */ 1483 } 1484 } 1485 1486 static void 1487 ena_free_all_rx_bufs(struct ena_adapter *adapter) 1488 { 1489 int i; 1490 1491 for (i = 0; i < adapter->num_io_queues; i++) 1492 ena_free_rx_bufs(adapter, i); 1493 } 1494 1495 /** 1496 * ena_free_tx_bufs - Free Tx Buffers per Queue 1497 * @adapter: network interface device structure 1498 * @qid: queue index 1499 **/ 1500 static void 1501 ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid) 1502 { 1503 bool print_once = true; 1504 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 1505 1506 ENA_RING_MTX_LOCK(tx_ring); 1507 for (int i = 0; i < tx_ring->ring_size; i++) { 1508 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i]; 1509 1510 if (tx_info->mbuf == NULL) 1511 continue; 1512 1513 if (print_once) { 1514 ena_log(adapter->pdev, WARN, 1515 "free uncompleted tx mbuf qid %d idx 0x%x\n", qid, 1516 i); 1517 print_once = false; 1518 } else { 1519 ena_log(adapter->pdev, DBG, 1520 "free uncompleted tx mbuf qid %d idx 0x%x\n", qid, 1521 i); 1522 } 1523 1524 bus_dmamap_sync(adapter->tx_buf_tag, tx_info->dmamap, 1525 BUS_DMASYNC_POSTWRITE); 1526 bus_dmamap_unload(adapter->tx_buf_tag, tx_info->dmamap); 1527 1528 m_free(tx_info->mbuf); 1529 tx_info->mbuf = NULL; 1530 } 1531 ENA_RING_MTX_UNLOCK(tx_ring); 1532 } 1533 1534 static void 1535 ena_free_all_tx_bufs(struct ena_adapter *adapter) 1536 { 1537 for (int i = 0; i < adapter->num_io_queues; i++) 1538 ena_free_tx_bufs(adapter, i); 1539 } 1540 1541 static void 1542 ena_destroy_all_tx_queues(struct ena_adapter *adapter) 1543 { 1544 uint16_t ena_qid; 1545 int i; 1546 1547 for (i = 0; i < adapter->num_io_queues; i++) { 1548 ena_qid = ENA_IO_TXQ_IDX(i); 1549 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 1550 } 1551 } 1552 1553 static void 1554 ena_destroy_all_rx_queues(struct ena_adapter *adapter) 1555 { 1556 uint16_t ena_qid; 1557 int i; 1558 1559 for (i = 0; i < adapter->num_io_queues; i++) { 1560 ena_qid = ENA_IO_RXQ_IDX(i); 1561 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 1562 } 1563 } 1564 1565 static void 1566 ena_destroy_all_io_queues(struct ena_adapter *adapter) 1567 { 1568 struct ena_que *queue; 1569 int i; 1570 1571 for (i = 0; i < adapter->num_io_queues; i++) { 1572 queue = &adapter->que[i]; 1573 while (taskqueue_cancel(queue->cleanup_tq, &queue->cleanup_task, NULL)) 1574 taskqueue_drain(queue->cleanup_tq, &queue->cleanup_task); 1575 taskqueue_free(queue->cleanup_tq); 1576 } 1577 1578 ena_destroy_all_tx_queues(adapter); 1579 ena_destroy_all_rx_queues(adapter); 1580 } 1581 1582 static int 1583 ena_create_io_queues(struct ena_adapter *adapter) 1584 { 1585 struct ena_com_dev *ena_dev = adapter->ena_dev; 1586 struct ena_com_create_io_ctx ctx; 1587 struct ena_ring *ring; 1588 struct ena_que *queue; 1589 uint16_t ena_qid; 1590 uint32_t msix_vector; 1591 cpuset_t *cpu_mask = NULL; 1592 int rc, i; 1593 1594 /* Create TX queues */ 1595 for (i = 0; i < adapter->num_io_queues; i++) { 1596 msix_vector = ENA_IO_IRQ_IDX(i); 1597 ena_qid = ENA_IO_TXQ_IDX(i); 1598 ctx.mem_queue_type = ena_dev->tx_mem_queue_type; 1599 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX; 1600 ctx.queue_size = adapter->requested_tx_ring_size; 1601 ctx.msix_vector = msix_vector; 1602 ctx.qid = ena_qid; 1603 ctx.numa_node = adapter->que[i].domain; 1604 1605 rc = ena_com_create_io_queue(ena_dev, &ctx); 1606 if (rc != 0) { 1607 ena_log(adapter->pdev, ERR, 1608 "Failed to create io TX queue #%d rc: %d\n", i, rc); 1609 goto err_tx; 1610 } 1611 ring = &adapter->tx_ring[i]; 1612 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1613 &ring->ena_com_io_sq, &ring->ena_com_io_cq); 1614 if (rc != 0) { 1615 ena_log(adapter->pdev, ERR, 1616 "Failed to get TX queue handlers. TX queue num" 1617 " %d rc: %d\n", 1618 i, rc); 1619 ena_com_destroy_io_queue(ena_dev, ena_qid); 1620 goto err_tx; 1621 } 1622 1623 if (ctx.numa_node >= 0) { 1624 ena_com_update_numa_node(ring->ena_com_io_cq, 1625 ctx.numa_node); 1626 } 1627 } 1628 1629 /* Create RX queues */ 1630 for (i = 0; i < adapter->num_io_queues; i++) { 1631 msix_vector = ENA_IO_IRQ_IDX(i); 1632 ena_qid = ENA_IO_RXQ_IDX(i); 1633 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 1634 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX; 1635 ctx.queue_size = adapter->requested_rx_ring_size; 1636 ctx.msix_vector = msix_vector; 1637 ctx.qid = ena_qid; 1638 ctx.numa_node = adapter->que[i].domain; 1639 1640 rc = ena_com_create_io_queue(ena_dev, &ctx); 1641 if (unlikely(rc != 0)) { 1642 ena_log(adapter->pdev, ERR, 1643 "Failed to create io RX queue[%d] rc: %d\n", i, rc); 1644 goto err_rx; 1645 } 1646 1647 ring = &adapter->rx_ring[i]; 1648 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1649 &ring->ena_com_io_sq, &ring->ena_com_io_cq); 1650 if (unlikely(rc != 0)) { 1651 ena_log(adapter->pdev, ERR, 1652 "Failed to get RX queue handlers. RX queue num" 1653 " %d rc: %d\n", 1654 i, rc); 1655 ena_com_destroy_io_queue(ena_dev, ena_qid); 1656 goto err_rx; 1657 } 1658 1659 if (ctx.numa_node >= 0) { 1660 ena_com_update_numa_node(ring->ena_com_io_cq, 1661 ctx.numa_node); 1662 } 1663 } 1664 1665 for (i = 0; i < adapter->num_io_queues; i++) { 1666 queue = &adapter->que[i]; 1667 1668 NET_TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue); 1669 queue->cleanup_tq = taskqueue_create_fast("ena cleanup", 1670 M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq); 1671 1672 #ifdef RSS 1673 cpu_mask = &queue->cpu_mask; 1674 #endif 1675 taskqueue_start_threads_cpuset(&queue->cleanup_tq, 1, PI_NET, 1676 cpu_mask, "%s queue %d cleanup", 1677 device_get_nameunit(adapter->pdev), i); 1678 } 1679 1680 return (0); 1681 1682 err_rx: 1683 while (i--) 1684 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i)); 1685 i = adapter->num_io_queues; 1686 err_tx: 1687 while (i--) 1688 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i)); 1689 1690 return (ENXIO); 1691 } 1692 1693 /********************************************************************* 1694 * 1695 * MSIX & Interrupt Service routine 1696 * 1697 **********************************************************************/ 1698 1699 /** 1700 * ena_handle_msix - MSIX Interrupt Handler for admin/async queue 1701 * @arg: interrupt number 1702 **/ 1703 static void 1704 ena_intr_msix_mgmnt(void *arg) 1705 { 1706 struct ena_adapter *adapter = (struct ena_adapter *)arg; 1707 1708 ena_com_admin_q_comp_intr_handler(adapter->ena_dev); 1709 if (likely(ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter))) 1710 ena_com_aenq_intr_handler(adapter->ena_dev, arg); 1711 } 1712 1713 /** 1714 * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx 1715 * @arg: queue 1716 **/ 1717 static int 1718 ena_handle_msix(void *arg) 1719 { 1720 struct ena_que *queue = arg; 1721 struct ena_adapter *adapter = queue->adapter; 1722 if_t ifp = adapter->ifp; 1723 1724 if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)) 1725 return (FILTER_STRAY); 1726 1727 taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task); 1728 1729 return (FILTER_HANDLED); 1730 } 1731 1732 static int 1733 ena_enable_msix(struct ena_adapter *adapter) 1734 { 1735 device_t dev = adapter->pdev; 1736 int msix_vecs, msix_req; 1737 int i, rc = 0; 1738 1739 if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) { 1740 ena_log(dev, ERR, "Error, MSI-X is already enabled\n"); 1741 return (EINVAL); 1742 } 1743 1744 /* Reserved the max msix vectors we might need */ 1745 msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues); 1746 1747 adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry), 1748 M_DEVBUF, M_WAITOK | M_ZERO); 1749 1750 ena_log(dev, DBG, "trying to enable MSI-X, vectors: %d\n", msix_vecs); 1751 1752 for (i = 0; i < msix_vecs; i++) { 1753 adapter->msix_entries[i].entry = i; 1754 /* Vectors must start from 1 */ 1755 adapter->msix_entries[i].vector = i + 1; 1756 } 1757 1758 msix_req = msix_vecs; 1759 rc = pci_alloc_msix(dev, &msix_vecs); 1760 if (unlikely(rc != 0)) { 1761 ena_log(dev, ERR, "Failed to enable MSIX, vectors %d rc %d\n", 1762 msix_vecs, rc); 1763 1764 rc = ENOSPC; 1765 goto err_msix_free; 1766 } 1767 1768 if (msix_vecs != msix_req) { 1769 if (msix_vecs == ENA_ADMIN_MSIX_VEC) { 1770 ena_log(dev, ERR, 1771 "Not enough number of MSI-x allocated: %d\n", 1772 msix_vecs); 1773 pci_release_msi(dev); 1774 rc = ENOSPC; 1775 goto err_msix_free; 1776 } 1777 ena_log(dev, ERR, 1778 "Enable only %d MSI-x (out of %d), reduce " 1779 "the number of queues\n", 1780 msix_vecs, msix_req); 1781 } 1782 1783 adapter->msix_vecs = msix_vecs; 1784 ENA_FLAG_SET_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter); 1785 1786 return (0); 1787 1788 err_msix_free: 1789 free(adapter->msix_entries, M_DEVBUF); 1790 adapter->msix_entries = NULL; 1791 1792 return (rc); 1793 } 1794 1795 static void 1796 ena_setup_mgmnt_intr(struct ena_adapter *adapter) 1797 { 1798 snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name, ENA_IRQNAME_SIZE, 1799 "ena-mgmnt@pci:%s", device_get_nameunit(adapter->pdev)); 1800 /* 1801 * Handler is NULL on purpose, it will be set 1802 * when mgmnt interrupt is acquired 1803 */ 1804 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL; 1805 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter; 1806 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector = 1807 adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector; 1808 } 1809 1810 static int 1811 ena_setup_io_intr(struct ena_adapter *adapter) 1812 { 1813 #ifdef RSS 1814 int num_buckets = rss_getnumbuckets(); 1815 static int last_bind = 0; 1816 int cur_bind; 1817 int idx; 1818 #endif 1819 int irq_idx; 1820 1821 if (adapter->msix_entries == NULL) 1822 return (EINVAL); 1823 1824 #ifdef RSS 1825 if (adapter->first_bind < 0) { 1826 adapter->first_bind = last_bind; 1827 last_bind = (last_bind + adapter->num_io_queues) % num_buckets; 1828 } 1829 cur_bind = adapter->first_bind; 1830 #endif 1831 1832 for (int i = 0; i < adapter->num_io_queues; i++) { 1833 irq_idx = ENA_IO_IRQ_IDX(i); 1834 1835 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE, 1836 "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i); 1837 adapter->irq_tbl[irq_idx].handler = ena_handle_msix; 1838 adapter->irq_tbl[irq_idx].data = &adapter->que[i]; 1839 adapter->irq_tbl[irq_idx].vector = 1840 adapter->msix_entries[irq_idx].vector; 1841 ena_log(adapter->pdev, DBG, "ena_setup_io_intr vector: %d\n", 1842 adapter->msix_entries[irq_idx].vector); 1843 1844 if (adapter->irq_cpu_base > ENA_BASE_CPU_UNSPECIFIED) { 1845 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu = 1846 (unsigned)(adapter->irq_cpu_base + 1847 i * adapter->irq_cpu_stride) % (unsigned)mp_ncpus; 1848 CPU_SETOF(adapter->que[i].cpu, &adapter->que[i].cpu_mask); 1849 } 1850 1851 #ifdef RSS 1852 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu = 1853 rss_getcpu(cur_bind); 1854 cur_bind = (cur_bind + 1) % num_buckets; 1855 CPU_SETOF(adapter->que[i].cpu, &adapter->que[i].cpu_mask); 1856 1857 for (idx = 0; idx < MAXMEMDOM; ++idx) { 1858 if (CPU_ISSET(adapter->que[i].cpu, &cpuset_domain[idx])) 1859 break; 1860 } 1861 adapter->que[i].domain = idx; 1862 #else 1863 adapter->que[i].domain = -1; 1864 #endif 1865 } 1866 1867 return (0); 1868 } 1869 1870 static int 1871 ena_request_mgmnt_irq(struct ena_adapter *adapter) 1872 { 1873 device_t pdev = adapter->pdev; 1874 struct ena_irq *irq; 1875 unsigned long flags; 1876 int rc, rcc; 1877 1878 flags = RF_ACTIVE | RF_SHAREABLE; 1879 1880 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 1881 irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ, 1882 &irq->vector, flags); 1883 1884 if (unlikely(irq->res == NULL)) { 1885 ena_log(pdev, ERR, "could not allocate irq vector: %d\n", 1886 irq->vector); 1887 return (ENXIO); 1888 } 1889 1890 rc = bus_setup_intr(adapter->pdev, irq->res, 1891 INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt, irq->data, 1892 &irq->cookie); 1893 if (unlikely(rc != 0)) { 1894 ena_log(pdev, ERR, 1895 "failed to register interrupt handler for irq %ju: %d\n", 1896 rman_get_start(irq->res), rc); 1897 goto err_res_free; 1898 } 1899 irq->requested = true; 1900 1901 return (rc); 1902 1903 err_res_free: 1904 ena_log(pdev, INFO, "releasing resource for irq %d\n", irq->vector); 1905 rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, irq->vector, 1906 irq->res); 1907 if (unlikely(rcc != 0)) 1908 ena_log(pdev, ERR, 1909 "dev has no parent while releasing res for irq: %d\n", 1910 irq->vector); 1911 irq->res = NULL; 1912 1913 return (rc); 1914 } 1915 1916 static int 1917 ena_request_io_irq(struct ena_adapter *adapter) 1918 { 1919 device_t pdev = adapter->pdev; 1920 struct ena_irq *irq; 1921 unsigned long flags = 0; 1922 int rc = 0, i, rcc; 1923 1924 if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter))) { 1925 ena_log(pdev, ERR, 1926 "failed to request I/O IRQ: MSI-X is not enabled\n"); 1927 return (EINVAL); 1928 } else { 1929 flags = RF_ACTIVE | RF_SHAREABLE; 1930 } 1931 1932 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) { 1933 irq = &adapter->irq_tbl[i]; 1934 1935 if (unlikely(irq->requested)) 1936 continue; 1937 1938 irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ, 1939 &irq->vector, flags); 1940 if (unlikely(irq->res == NULL)) { 1941 rc = ENOMEM; 1942 ena_log(pdev, ERR, 1943 "could not allocate irq vector: %d\n", irq->vector); 1944 goto err; 1945 } 1946 1947 rc = bus_setup_intr(adapter->pdev, irq->res, 1948 INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL, irq->data, 1949 &irq->cookie); 1950 if (unlikely(rc != 0)) { 1951 ena_log(pdev, ERR, 1952 "failed to register interrupt handler for irq %ju: %d\n", 1953 rman_get_start(irq->res), rc); 1954 goto err; 1955 } 1956 irq->requested = true; 1957 1958 if (adapter->rss_enabled || adapter->irq_cpu_base > ENA_BASE_CPU_UNSPECIFIED) { 1959 rc = bus_bind_intr(adapter->pdev, irq->res, irq->cpu); 1960 if (unlikely(rc != 0)) { 1961 ena_log(pdev, ERR, 1962 "failed to bind interrupt handler for irq %ju to cpu %d: %d\n", 1963 rman_get_start(irq->res), irq->cpu, rc); 1964 goto err; 1965 } 1966 1967 ena_log(pdev, INFO, "queue %d - cpu %d\n", 1968 i - ENA_IO_IRQ_FIRST_IDX, irq->cpu); 1969 } 1970 } 1971 return (rc); 1972 1973 err: 1974 1975 for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) { 1976 irq = &adapter->irq_tbl[i]; 1977 rcc = 0; 1978 1979 /* Once we entered err: section and irq->requested is true we 1980 free both intr and resources */ 1981 if (irq->requested) { 1982 rcc = bus_teardown_intr(adapter->pdev, irq->res, 1983 irq->cookie); 1984 if (unlikely(rcc != 0)) 1985 ena_log(pdev, ERR, 1986 "could not release irq: %d, error: %d\n", 1987 irq->vector, rcc); 1988 } 1989 1990 /* If we entered err: section without irq->requested set we know 1991 it was bus_alloc_resource_any() that needs cleanup, provided 1992 res is not NULL. In case res is NULL no work in needed in 1993 this iteration */ 1994 rcc = 0; 1995 if (irq->res != NULL) { 1996 rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 1997 irq->vector, irq->res); 1998 } 1999 if (unlikely(rcc != 0)) 2000 ena_log(pdev, ERR, 2001 "dev has no parent while releasing res for irq: %d\n", 2002 irq->vector); 2003 irq->requested = false; 2004 irq->res = NULL; 2005 } 2006 2007 return (rc); 2008 } 2009 2010 static void 2011 ena_free_mgmnt_irq(struct ena_adapter *adapter) 2012 { 2013 device_t pdev = adapter->pdev; 2014 struct ena_irq *irq; 2015 int rc; 2016 2017 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 2018 if (irq->requested) { 2019 ena_log(pdev, DBG, "tear down irq: %d\n", irq->vector); 2020 rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie); 2021 if (unlikely(rc != 0)) 2022 ena_log(pdev, ERR, "failed to tear down irq: %d\n", 2023 irq->vector); 2024 irq->requested = 0; 2025 } 2026 2027 if (irq->res != NULL) { 2028 ena_log(pdev, DBG, "release resource irq: %d\n", irq->vector); 2029 rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 2030 irq->vector, irq->res); 2031 irq->res = NULL; 2032 if (unlikely(rc != 0)) 2033 ena_log(pdev, ERR, 2034 "dev has no parent while releasing res for irq: %d\n", 2035 irq->vector); 2036 } 2037 } 2038 2039 static void 2040 ena_free_io_irq(struct ena_adapter *adapter) 2041 { 2042 device_t pdev = adapter->pdev; 2043 struct ena_irq *irq; 2044 int rc; 2045 2046 for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) { 2047 irq = &adapter->irq_tbl[i]; 2048 if (irq->requested) { 2049 ena_log(pdev, DBG, "tear down irq: %d\n", irq->vector); 2050 rc = bus_teardown_intr(adapter->pdev, irq->res, 2051 irq->cookie); 2052 if (unlikely(rc != 0)) { 2053 ena_log(pdev, ERR, 2054 "failed to tear down irq: %d\n", 2055 irq->vector); 2056 } 2057 irq->requested = 0; 2058 } 2059 2060 if (irq->res != NULL) { 2061 ena_log(pdev, DBG, "release resource irq: %d\n", 2062 irq->vector); 2063 rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 2064 irq->vector, irq->res); 2065 irq->res = NULL; 2066 if (unlikely(rc != 0)) { 2067 ena_log(pdev, ERR, 2068 "dev has no parent while releasing res for irq: %d\n", 2069 irq->vector); 2070 } 2071 } 2072 } 2073 } 2074 2075 static void 2076 ena_free_irqs(struct ena_adapter *adapter) 2077 { 2078 ena_free_io_irq(adapter); 2079 ena_free_mgmnt_irq(adapter); 2080 ena_disable_msix(adapter); 2081 } 2082 2083 static void 2084 ena_disable_msix(struct ena_adapter *adapter) 2085 { 2086 if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) { 2087 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter); 2088 pci_release_msi(adapter->pdev); 2089 } 2090 2091 adapter->msix_vecs = 0; 2092 free(adapter->msix_entries, M_DEVBUF); 2093 adapter->msix_entries = NULL; 2094 } 2095 2096 static void 2097 ena_unmask_all_io_irqs(struct ena_adapter *adapter) 2098 { 2099 struct ena_com_io_cq *io_cq; 2100 struct ena_eth_io_intr_reg intr_reg; 2101 struct ena_ring *tx_ring; 2102 uint16_t ena_qid; 2103 int i; 2104 2105 /* Unmask interrupts for all queues */ 2106 for (i = 0; i < adapter->num_io_queues; i++) { 2107 ena_qid = ENA_IO_TXQ_IDX(i); 2108 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid]; 2109 ena_com_update_intr_reg(&intr_reg, 0, 0, true, false); 2110 tx_ring = &adapter->tx_ring[i]; 2111 counter_u64_add(tx_ring->tx_stats.unmask_interrupt_num, 1); 2112 ena_com_unmask_intr(io_cq, &intr_reg); 2113 } 2114 } 2115 2116 static int 2117 ena_up_complete(struct ena_adapter *adapter) 2118 { 2119 int rc; 2120 2121 if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) { 2122 rc = ena_rss_configure(adapter); 2123 if (rc != 0) { 2124 ena_log(adapter->pdev, ERR, 2125 "Failed to configure RSS\n"); 2126 return (rc); 2127 } 2128 } 2129 2130 rc = ena_change_mtu(adapter->ifp, if_getmtu(adapter->ifp)); 2131 if (unlikely(rc != 0)) 2132 return (rc); 2133 2134 ena_refill_all_rx_bufs(adapter); 2135 ena_reset_counters((counter_u64_t *)&adapter->hw_stats, 2136 sizeof(adapter->hw_stats)); 2137 2138 return (0); 2139 } 2140 2141 static void 2142 set_io_rings_size(struct ena_adapter *adapter, int new_tx_size, int new_rx_size) 2143 { 2144 int i; 2145 2146 for (i = 0; i < adapter->num_io_queues; i++) { 2147 adapter->tx_ring[i].ring_size = new_tx_size; 2148 adapter->rx_ring[i].ring_size = new_rx_size; 2149 } 2150 } 2151 2152 static int 2153 create_queues_with_size_backoff(struct ena_adapter *adapter) 2154 { 2155 device_t pdev = adapter->pdev; 2156 int rc; 2157 uint32_t cur_rx_ring_size, cur_tx_ring_size; 2158 uint32_t new_rx_ring_size, new_tx_ring_size; 2159 2160 /* 2161 * Current queue sizes might be set to smaller than the requested 2162 * ones due to past queue allocation failures. 2163 */ 2164 set_io_rings_size(adapter, adapter->requested_tx_ring_size, 2165 adapter->requested_rx_ring_size); 2166 2167 while (1) { 2168 /* Allocate transmit descriptors */ 2169 rc = ena_setup_all_tx_resources(adapter); 2170 if (unlikely(rc != 0)) { 2171 ena_log(pdev, ERR, "err_setup_tx\n"); 2172 goto err_setup_tx; 2173 } 2174 2175 /* Allocate receive descriptors */ 2176 rc = ena_setup_all_rx_resources(adapter); 2177 if (unlikely(rc != 0)) { 2178 ena_log(pdev, ERR, "err_setup_rx\n"); 2179 goto err_setup_rx; 2180 } 2181 2182 /* Create IO queues for Rx & Tx */ 2183 rc = ena_create_io_queues(adapter); 2184 if (unlikely(rc != 0)) { 2185 ena_log(pdev, ERR, "create IO queues failed\n"); 2186 goto err_io_que; 2187 } 2188 2189 return (0); 2190 2191 err_io_que: 2192 ena_free_all_rx_resources(adapter); 2193 err_setup_rx: 2194 ena_free_all_tx_resources(adapter); 2195 err_setup_tx: 2196 /* 2197 * Lower the ring size if ENOMEM. Otherwise, return the 2198 * error straightaway. 2199 */ 2200 if (unlikely(rc != ENOMEM)) { 2201 ena_log(pdev, ERR, 2202 "Queue creation failed with error code: %d\n", rc); 2203 return (rc); 2204 } 2205 2206 cur_tx_ring_size = adapter->tx_ring[0].ring_size; 2207 cur_rx_ring_size = adapter->rx_ring[0].ring_size; 2208 2209 ena_log(pdev, ERR, 2210 "Not enough memory to create queues with sizes TX=%d, RX=%d\n", 2211 cur_tx_ring_size, cur_rx_ring_size); 2212 2213 new_tx_ring_size = cur_tx_ring_size; 2214 new_rx_ring_size = cur_rx_ring_size; 2215 2216 /* 2217 * Decrease the size of a larger queue, or decrease both if they 2218 * are the same size. 2219 */ 2220 if (cur_rx_ring_size <= cur_tx_ring_size) 2221 new_tx_ring_size = cur_tx_ring_size / 2; 2222 if (cur_rx_ring_size >= cur_tx_ring_size) 2223 new_rx_ring_size = cur_rx_ring_size / 2; 2224 2225 if (new_tx_ring_size < ENA_MIN_RING_SIZE || 2226 new_rx_ring_size < ENA_MIN_RING_SIZE) { 2227 ena_log(pdev, ERR, 2228 "Queue creation failed with the smallest possible queue size" 2229 "of %d for both queues. Not retrying with smaller queues\n", 2230 ENA_MIN_RING_SIZE); 2231 return (rc); 2232 } 2233 2234 ena_log(pdev, INFO, 2235 "Retrying queue creation with sizes TX=%d, RX=%d\n", 2236 new_tx_ring_size, new_rx_ring_size); 2237 2238 set_io_rings_size(adapter, new_tx_ring_size, new_rx_ring_size); 2239 } 2240 } 2241 2242 int 2243 ena_up(struct ena_adapter *adapter) 2244 { 2245 int rc = 0; 2246 2247 ENA_LOCK_ASSERT(); 2248 2249 if (unlikely(device_is_attached(adapter->pdev) == 0)) { 2250 ena_log(adapter->pdev, ERR, "device is not attached!\n"); 2251 return (ENXIO); 2252 } 2253 2254 if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) 2255 return (0); 2256 2257 ena_log(adapter->pdev, INFO, "device is going UP\n"); 2258 2259 /* setup interrupts for IO queues */ 2260 rc = ena_setup_io_intr(adapter); 2261 if (unlikely(rc != 0)) { 2262 ena_log(adapter->pdev, ERR, "error setting up IO interrupt\n"); 2263 goto error; 2264 } 2265 rc = ena_request_io_irq(adapter); 2266 if (unlikely(rc != 0)) { 2267 ena_log(adapter->pdev, ERR, "err_req_irq\n"); 2268 goto error; 2269 } 2270 2271 ena_log(adapter->pdev, INFO, 2272 "Creating %u IO queues. Rx queue size: %d, Tx queue size: %d, LLQ is %s\n", 2273 adapter->num_io_queues, 2274 adapter->requested_rx_ring_size, 2275 adapter->requested_tx_ring_size, 2276 (adapter->ena_dev->tx_mem_queue_type == 2277 ENA_ADMIN_PLACEMENT_POLICY_DEV) ? "ENABLED" : "DISABLED"); 2278 2279 rc = create_queues_with_size_backoff(adapter); 2280 if (unlikely(rc != 0)) { 2281 ena_log(adapter->pdev, ERR, 2282 "error creating queues with size backoff\n"); 2283 goto err_create_queues_with_backoff; 2284 } 2285 2286 if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) 2287 if_link_state_change(adapter->ifp, LINK_STATE_UP); 2288 2289 rc = ena_up_complete(adapter); 2290 if (unlikely(rc != 0)) 2291 goto err_up_complete; 2292 2293 counter_u64_add(adapter->dev_stats.interface_up, 1); 2294 2295 ena_update_hwassist(adapter); 2296 2297 if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 2298 2299 ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP, adapter); 2300 2301 ena_unmask_all_io_irqs(adapter); 2302 2303 return (0); 2304 2305 err_up_complete: 2306 ena_destroy_all_io_queues(adapter); 2307 ena_free_all_rx_resources(adapter); 2308 ena_free_all_tx_resources(adapter); 2309 err_create_queues_with_backoff: 2310 ena_free_io_irq(adapter); 2311 error: 2312 return (rc); 2313 } 2314 2315 static uint64_t 2316 ena_get_counter(if_t ifp, ift_counter cnt) 2317 { 2318 struct ena_adapter *adapter; 2319 struct ena_hw_stats *stats; 2320 2321 adapter = if_getsoftc(ifp); 2322 stats = &adapter->hw_stats; 2323 2324 switch (cnt) { 2325 case IFCOUNTER_IPACKETS: 2326 return (counter_u64_fetch(stats->rx_packets)); 2327 case IFCOUNTER_OPACKETS: 2328 return (counter_u64_fetch(stats->tx_packets)); 2329 case IFCOUNTER_IBYTES: 2330 return (counter_u64_fetch(stats->rx_bytes)); 2331 case IFCOUNTER_OBYTES: 2332 return (counter_u64_fetch(stats->tx_bytes)); 2333 case IFCOUNTER_IQDROPS: 2334 return (counter_u64_fetch(stats->rx_drops)); 2335 case IFCOUNTER_OQDROPS: 2336 return (counter_u64_fetch(stats->tx_drops)); 2337 default: 2338 return (if_get_counter_default(ifp, cnt)); 2339 } 2340 } 2341 2342 static int 2343 ena_media_change(if_t ifp) 2344 { 2345 /* Media Change is not supported by firmware */ 2346 return (0); 2347 } 2348 2349 static void 2350 ena_media_status(if_t ifp, struct ifmediareq *ifmr) 2351 { 2352 struct ena_adapter *adapter = if_getsoftc(ifp); 2353 ena_log(adapter->pdev, DBG, "Media status update\n"); 2354 2355 ENA_LOCK_LOCK(); 2356 2357 ifmr->ifm_status = IFM_AVALID; 2358 ifmr->ifm_active = IFM_ETHER; 2359 2360 if (!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) { 2361 ENA_LOCK_UNLOCK(); 2362 ena_log(adapter->pdev, INFO, "Link is down\n"); 2363 return; 2364 } 2365 2366 ifmr->ifm_status |= IFM_ACTIVE; 2367 ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX; 2368 2369 ENA_LOCK_UNLOCK(); 2370 } 2371 2372 static void 2373 ena_init(void *arg) 2374 { 2375 struct ena_adapter *adapter = (struct ena_adapter *)arg; 2376 2377 if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) { 2378 ENA_LOCK_LOCK(); 2379 ena_up(adapter); 2380 ENA_LOCK_UNLOCK(); 2381 } 2382 } 2383 2384 static int 2385 ena_ioctl(if_t ifp, u_long command, caddr_t data) 2386 { 2387 struct ena_adapter *adapter; 2388 struct ifreq *ifr; 2389 int rc; 2390 2391 adapter = if_getsoftc(ifp); 2392 ifr = (struct ifreq *)data; 2393 2394 /* 2395 * Acquiring lock to prevent from running up and down routines parallel. 2396 */ 2397 rc = 0; 2398 switch (command) { 2399 case SIOCSIFMTU: 2400 if (if_getmtu(ifp) == ifr->ifr_mtu) 2401 break; 2402 ENA_LOCK_LOCK(); 2403 ena_down(adapter); 2404 2405 ena_change_mtu(ifp, ifr->ifr_mtu); 2406 2407 rc = ena_up(adapter); 2408 ENA_LOCK_UNLOCK(); 2409 break; 2410 2411 case SIOCSIFFLAGS: 2412 if ((if_getflags(ifp) & IFF_UP) != 0) { 2413 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) { 2414 if ((if_getflags(ifp) & (IFF_PROMISC | 2415 IFF_ALLMULTI)) != 0) { 2416 ena_log(adapter->pdev, INFO, 2417 "ioctl promisc/allmulti\n"); 2418 } 2419 } else { 2420 ENA_LOCK_LOCK(); 2421 rc = ena_up(adapter); 2422 ENA_LOCK_UNLOCK(); 2423 } 2424 } else { 2425 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) { 2426 ENA_LOCK_LOCK(); 2427 ena_down(adapter); 2428 ENA_LOCK_UNLOCK(); 2429 } 2430 } 2431 break; 2432 2433 case SIOCADDMULTI: 2434 case SIOCDELMULTI: 2435 break; 2436 2437 case SIOCSIFMEDIA: 2438 case SIOCGIFMEDIA: 2439 rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command); 2440 break; 2441 2442 case SIOCSIFCAP: 2443 { 2444 int reinit = 0; 2445 2446 if (ifr->ifr_reqcap != if_getcapenable(ifp)) { 2447 if_setcapenable(ifp, ifr->ifr_reqcap); 2448 reinit = 1; 2449 } 2450 2451 if ((reinit != 0) && 2452 ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) { 2453 ENA_LOCK_LOCK(); 2454 ena_down(adapter); 2455 rc = ena_up(adapter); 2456 ENA_LOCK_UNLOCK(); 2457 } 2458 } 2459 2460 break; 2461 default: 2462 rc = ether_ioctl(ifp, command, data); 2463 break; 2464 } 2465 2466 return (rc); 2467 } 2468 2469 static int 2470 ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat) 2471 { 2472 int caps = 0; 2473 2474 if ((feat->offload.tx & 2475 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK | 2476 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK | 2477 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0) 2478 caps |= IFCAP_TXCSUM; 2479 2480 if ((feat->offload.tx & 2481 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK | 2482 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0) 2483 caps |= IFCAP_TXCSUM_IPV6; 2484 2485 if ((feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0) 2486 caps |= IFCAP_TSO4; 2487 2488 if ((feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0) 2489 caps |= IFCAP_TSO6; 2490 2491 if ((feat->offload.rx_supported & 2492 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK | 2493 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0) 2494 caps |= IFCAP_RXCSUM; 2495 2496 if ((feat->offload.rx_supported & 2497 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0) 2498 caps |= IFCAP_RXCSUM_IPV6; 2499 2500 caps |= IFCAP_LRO | IFCAP_JUMBO_MTU; 2501 2502 return (caps); 2503 } 2504 2505 static void 2506 ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp) 2507 { 2508 host_info->supported_network_features[0] = (uint32_t)if_getcapabilities(ifp); 2509 } 2510 2511 static void 2512 ena_update_hwassist(struct ena_adapter *adapter) 2513 { 2514 if_t ifp = adapter->ifp; 2515 uint32_t feat = adapter->tx_offload_cap; 2516 int cap = if_getcapenable(ifp); 2517 int flags = 0; 2518 2519 if_clearhwassist(ifp); 2520 2521 if ((cap & IFCAP_TXCSUM) != 0) { 2522 if ((feat & 2523 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0) 2524 flags |= CSUM_IP; 2525 if ((feat & 2526 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK | 2527 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0) 2528 flags |= CSUM_IP_UDP | CSUM_IP_TCP; 2529 } 2530 2531 if ((cap & IFCAP_TXCSUM_IPV6) != 0) 2532 flags |= CSUM_IP6_UDP | CSUM_IP6_TCP; 2533 2534 if ((cap & IFCAP_TSO4) != 0) 2535 flags |= CSUM_IP_TSO; 2536 2537 if ((cap & IFCAP_TSO6) != 0) 2538 flags |= CSUM_IP6_TSO; 2539 2540 if_sethwassistbits(ifp, flags, 0); 2541 } 2542 2543 static void 2544 ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter, 2545 struct ena_com_dev_get_features_ctx *feat) 2546 { 2547 if_t ifp; 2548 int caps = 0; 2549 2550 ifp = adapter->ifp = if_gethandle(IFT_ETHER); 2551 if_initname(ifp, device_get_name(pdev), device_get_unit(pdev)); 2552 if_setdev(ifp, pdev); 2553 if_setsoftc(ifp, adapter); 2554 2555 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 2556 if_setinitfn(ifp, ena_init); 2557 if_settransmitfn(ifp, ena_mq_start); 2558 if_setqflushfn(ifp, ena_qflush); 2559 if_setioctlfn(ifp, ena_ioctl); 2560 if_setgetcounterfn(ifp, ena_get_counter); 2561 2562 if_setsendqlen(ifp, adapter->requested_tx_ring_size); 2563 if_setsendqready(ifp); 2564 if_setmtu(ifp, ETHERMTU); 2565 if_setbaudrate(ifp, 0); 2566 /* Zeroize capabilities... */ 2567 if_setcapabilities(ifp, 0); 2568 if_setcapenable(ifp, 0); 2569 /* check hardware support */ 2570 caps = ena_get_dev_offloads(feat); 2571 /* ... and set them */ 2572 if_setcapabilitiesbit(ifp, caps, 0); 2573 2574 /* TSO parameters */ 2575 if_sethwtsomax(ifp, ENA_TSO_MAXSIZE - 2576 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 2577 if_sethwtsomaxsegcount(ifp, adapter->max_tx_sgl_size - 1); 2578 if_sethwtsomaxsegsize(ifp, ENA_TSO_MAXSIZE); 2579 2580 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 2581 if_setcapenable(ifp, if_getcapabilities(ifp)); 2582 2583 /* 2584 * Specify the media types supported by this adapter and register 2585 * callbacks to update media and link information 2586 */ 2587 ifmedia_init(&adapter->media, IFM_IMASK, ena_media_change, 2588 ena_media_status); 2589 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL); 2590 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO); 2591 2592 ether_ifattach(ifp, adapter->mac_addr); 2593 } 2594 2595 void 2596 ena_down(struct ena_adapter *adapter) 2597 { 2598 int rc; 2599 2600 ENA_LOCK_ASSERT(); 2601 2602 if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) 2603 return; 2604 2605 ena_log(adapter->pdev, INFO, "device is going DOWN\n"); 2606 2607 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP, adapter); 2608 if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 2609 2610 ena_free_io_irq(adapter); 2611 2612 if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) { 2613 rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason); 2614 if (unlikely(rc != 0)) 2615 ena_log(adapter->pdev, ERR, "Device reset failed\n"); 2616 } 2617 2618 ena_destroy_all_io_queues(adapter); 2619 2620 ena_free_all_tx_bufs(adapter); 2621 ena_free_all_rx_bufs(adapter); 2622 ena_free_all_tx_resources(adapter); 2623 ena_free_all_rx_resources(adapter); 2624 2625 counter_u64_add(adapter->dev_stats.interface_down, 1); 2626 } 2627 2628 static uint32_t 2629 ena_calc_max_io_queue_num(device_t pdev, struct ena_com_dev *ena_dev, 2630 struct ena_com_dev_get_features_ctx *get_feat_ctx) 2631 { 2632 uint32_t io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues; 2633 2634 /* Regular queues capabilities */ 2635 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 2636 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 2637 &get_feat_ctx->max_queue_ext.max_queue_ext; 2638 io_rx_num = min_t(int, max_queue_ext->max_rx_sq_num, 2639 max_queue_ext->max_rx_cq_num); 2640 2641 io_tx_sq_num = max_queue_ext->max_tx_sq_num; 2642 io_tx_cq_num = max_queue_ext->max_tx_cq_num; 2643 } else { 2644 struct ena_admin_queue_feature_desc *max_queues = 2645 &get_feat_ctx->max_queues; 2646 io_tx_sq_num = max_queues->max_sq_num; 2647 io_tx_cq_num = max_queues->max_cq_num; 2648 io_rx_num = min_t(int, io_tx_sq_num, io_tx_cq_num); 2649 } 2650 2651 /* In case of LLQ use the llq fields for the tx SQ/CQ */ 2652 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 2653 io_tx_sq_num = get_feat_ctx->llq.max_llq_num; 2654 2655 max_num_io_queues = min_t(uint32_t, mp_ncpus, ENA_MAX_NUM_IO_QUEUES); 2656 max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_rx_num); 2657 max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_sq_num); 2658 max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_cq_num); 2659 /* 1 IRQ for mgmnt and 1 IRQ for each TX/RX pair */ 2660 max_num_io_queues = min_t(uint32_t, max_num_io_queues, 2661 pci_msix_count(pdev) - 1); 2662 #ifdef RSS 2663 max_num_io_queues = min_t(uint32_t, max_num_io_queues, 2664 rss_getnumbuckets()); 2665 #endif 2666 2667 return (max_num_io_queues); 2668 } 2669 2670 static int 2671 ena_enable_wc(device_t pdev, struct resource *res) 2672 { 2673 #if defined(__i386) || defined(__amd64) || defined(__aarch64__) 2674 vm_offset_t va; 2675 vm_size_t len; 2676 int rc; 2677 2678 va = (vm_offset_t)rman_get_virtual(res); 2679 len = rman_get_size(res); 2680 /* Enable write combining */ 2681 rc = pmap_change_attr(va, len, VM_MEMATTR_WRITE_COMBINING); 2682 if (unlikely(rc != 0)) { 2683 ena_log(pdev, ERR, "pmap_change_attr failed, %d\n", rc); 2684 return (rc); 2685 } 2686 2687 return (0); 2688 #endif 2689 return (EOPNOTSUPP); 2690 } 2691 2692 static int 2693 ena_set_queues_placement_policy(device_t pdev, struct ena_com_dev *ena_dev, 2694 struct ena_admin_feature_llq_desc *llq, 2695 struct ena_llq_configurations *llq_default_configurations) 2696 { 2697 int rc; 2698 uint32_t llq_feature_mask; 2699 2700 llq_feature_mask = 1 << ENA_ADMIN_LLQ; 2701 if (!(ena_dev->supported_features & llq_feature_mask)) { 2702 ena_log(pdev, WARN, 2703 "LLQ is not supported. Fallback to host mode policy.\n"); 2704 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 2705 return (0); 2706 } 2707 2708 if (ena_dev->mem_bar == NULL) { 2709 ena_log(pdev, WARN, 2710 "LLQ is advertised as supported but device doesn't expose mem bar.\n"); 2711 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 2712 return (0); 2713 } 2714 2715 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations); 2716 if (unlikely(rc != 0)) { 2717 ena_log(pdev, WARN, 2718 "Failed to configure the device mode. " 2719 "Fallback to host mode policy.\n"); 2720 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 2721 } 2722 2723 return (0); 2724 } 2725 2726 static int 2727 ena_map_llq_mem_bar(device_t pdev, struct ena_com_dev *ena_dev) 2728 { 2729 struct ena_adapter *adapter = device_get_softc(pdev); 2730 int rc, rid; 2731 2732 /* Try to allocate resources for LLQ bar */ 2733 rid = PCIR_BAR(ENA_MEM_BAR); 2734 adapter->memory = bus_alloc_resource_any(pdev, SYS_RES_MEMORY, &rid, 2735 RF_ACTIVE); 2736 if (unlikely(adapter->memory == NULL)) { 2737 ena_log(pdev, WARN, 2738 "Unable to allocate LLQ bar resource. LLQ mode won't be used.\n"); 2739 return (0); 2740 } 2741 2742 /* Enable write combining for better LLQ performance */ 2743 rc = ena_enable_wc(adapter->pdev, adapter->memory); 2744 if (unlikely(rc != 0)) { 2745 ena_log(pdev, ERR, "failed to enable write combining.\n"); 2746 return (rc); 2747 } 2748 2749 /* 2750 * Save virtual address of the device's memory region 2751 * for the ena_com layer. 2752 */ 2753 ena_dev->mem_bar = rman_get_virtual(adapter->memory); 2754 2755 return (0); 2756 } 2757 2758 static inline void 2759 set_default_llq_configurations(struct ena_llq_configurations *llq_config, 2760 struct ena_admin_feature_llq_desc *llq) 2761 { 2762 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER; 2763 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY; 2764 llq_config->llq_num_decs_before_header = 2765 ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2; 2766 if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) != 2767 0 && ena_force_large_llq_header) { 2768 llq_config->llq_ring_entry_size = 2769 ENA_ADMIN_LIST_ENTRY_SIZE_256B; 2770 llq_config->llq_ring_entry_size_value = 256; 2771 } else { 2772 llq_config->llq_ring_entry_size = 2773 ENA_ADMIN_LIST_ENTRY_SIZE_128B; 2774 llq_config->llq_ring_entry_size_value = 128; 2775 } 2776 } 2777 2778 static int 2779 ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx) 2780 { 2781 struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq; 2782 struct ena_com_dev *ena_dev = ctx->ena_dev; 2783 uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE; 2784 uint32_t rx_queue_size = ENA_DEFAULT_RING_SIZE; 2785 uint32_t max_tx_queue_size; 2786 uint32_t max_rx_queue_size; 2787 2788 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 2789 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 2790 &ctx->get_feat_ctx->max_queue_ext.max_queue_ext; 2791 max_rx_queue_size = min_t(uint32_t, 2792 max_queue_ext->max_rx_cq_depth, 2793 max_queue_ext->max_rx_sq_depth); 2794 max_tx_queue_size = max_queue_ext->max_tx_cq_depth; 2795 2796 if (ena_dev->tx_mem_queue_type == 2797 ENA_ADMIN_PLACEMENT_POLICY_DEV) 2798 max_tx_queue_size = min_t(uint32_t, max_tx_queue_size, 2799 llq->max_llq_depth); 2800 else 2801 max_tx_queue_size = min_t(uint32_t, max_tx_queue_size, 2802 max_queue_ext->max_tx_sq_depth); 2803 2804 ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 2805 max_queue_ext->max_per_packet_tx_descs); 2806 ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 2807 max_queue_ext->max_per_packet_rx_descs); 2808 } else { 2809 struct ena_admin_queue_feature_desc *max_queues = 2810 &ctx->get_feat_ctx->max_queues; 2811 max_rx_queue_size = min_t(uint32_t, max_queues->max_cq_depth, 2812 max_queues->max_sq_depth); 2813 max_tx_queue_size = max_queues->max_cq_depth; 2814 2815 if (ena_dev->tx_mem_queue_type == 2816 ENA_ADMIN_PLACEMENT_POLICY_DEV) 2817 max_tx_queue_size = min_t(uint32_t, max_tx_queue_size, 2818 llq->max_llq_depth); 2819 else 2820 max_tx_queue_size = min_t(uint32_t, max_tx_queue_size, 2821 max_queues->max_sq_depth); 2822 2823 ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 2824 max_queues->max_packet_tx_descs); 2825 ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 2826 max_queues->max_packet_rx_descs); 2827 } 2828 2829 /* round down to the nearest power of 2 */ 2830 max_tx_queue_size = 1 << (flsl(max_tx_queue_size) - 1); 2831 max_rx_queue_size = 1 << (flsl(max_rx_queue_size) - 1); 2832 2833 /* 2834 * When forcing large headers, we multiply the entry size by 2, 2835 * and therefore divide the queue size by 2, leaving the amount 2836 * of memory used by the queues unchanged. 2837 */ 2838 if (ena_force_large_llq_header) { 2839 if ((llq->entry_size_ctrl_supported & 2840 ENA_ADMIN_LIST_ENTRY_SIZE_256B) != 0 && 2841 ena_dev->tx_mem_queue_type == 2842 ENA_ADMIN_PLACEMENT_POLICY_DEV) { 2843 max_tx_queue_size /= 2; 2844 ena_log(ctx->pdev, INFO, 2845 "Forcing large headers and decreasing maximum Tx queue size to %d\n", 2846 max_tx_queue_size); 2847 } else { 2848 ena_log(ctx->pdev, WARN, 2849 "Forcing large headers failed: LLQ is disabled or device does not support large headers\n"); 2850 } 2851 } 2852 2853 tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE, 2854 max_tx_queue_size); 2855 rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE, 2856 max_rx_queue_size); 2857 2858 tx_queue_size = 1 << (flsl(tx_queue_size) - 1); 2859 rx_queue_size = 1 << (flsl(rx_queue_size) - 1); 2860 2861 ctx->max_tx_queue_size = max_tx_queue_size; 2862 ctx->max_rx_queue_size = max_rx_queue_size; 2863 ctx->tx_queue_size = tx_queue_size; 2864 ctx->rx_queue_size = rx_queue_size; 2865 2866 return (0); 2867 } 2868 2869 static void 2870 ena_config_host_info(struct ena_com_dev *ena_dev, device_t dev) 2871 { 2872 struct ena_admin_host_info *host_info; 2873 uintptr_t rid; 2874 int rc; 2875 2876 /* Allocate only the host info */ 2877 rc = ena_com_allocate_host_info(ena_dev); 2878 if (unlikely(rc != 0)) { 2879 ena_log(dev, ERR, "Cannot allocate host info\n"); 2880 return; 2881 } 2882 2883 host_info = ena_dev->host_attr.host_info; 2884 2885 if (pci_get_id(dev, PCI_ID_RID, &rid) == 0) 2886 host_info->bdf = rid; 2887 host_info->os_type = ENA_ADMIN_OS_FREEBSD; 2888 host_info->kernel_ver = osreldate; 2889 2890 sprintf(host_info->kernel_ver_str, "%d", osreldate); 2891 host_info->os_dist = 0; 2892 strncpy(host_info->os_dist_str, osrelease, 2893 sizeof(host_info->os_dist_str) - 1); 2894 2895 host_info->driver_version = (ENA_DRV_MODULE_VER_MAJOR) | 2896 (ENA_DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) | 2897 (ENA_DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT); 2898 host_info->num_cpus = mp_ncpus; 2899 host_info->driver_supported_features = 2900 ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK | 2901 ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK; 2902 2903 rc = ena_com_set_host_attributes(ena_dev); 2904 if (unlikely(rc != 0)) { 2905 if (rc == EOPNOTSUPP) 2906 ena_log(dev, WARN, "Cannot set host attributes\n"); 2907 else 2908 ena_log(dev, ERR, "Cannot set host attributes\n"); 2909 2910 goto err; 2911 } 2912 2913 return; 2914 2915 err: 2916 ena_com_delete_host_info(ena_dev); 2917 } 2918 2919 static int 2920 ena_device_init(struct ena_adapter *adapter, device_t pdev, 2921 struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active) 2922 { 2923 struct ena_llq_configurations llq_config; 2924 struct ena_com_dev *ena_dev = adapter->ena_dev; 2925 bool readless_supported; 2926 uint32_t aenq_groups; 2927 int dma_width; 2928 int rc; 2929 2930 rc = ena_com_mmio_reg_read_request_init(ena_dev); 2931 if (unlikely(rc != 0)) { 2932 ena_log(pdev, ERR, "failed to init mmio read less\n"); 2933 return (rc); 2934 } 2935 2936 /* 2937 * The PCIe configuration space revision id indicate if mmio reg 2938 * read is disabled 2939 */ 2940 readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ); 2941 ena_com_set_mmio_read_mode(ena_dev, readless_supported); 2942 2943 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL); 2944 if (unlikely(rc != 0)) { 2945 ena_log(pdev, ERR, "Can not reset device\n"); 2946 goto err_mmio_read_less; 2947 } 2948 2949 rc = ena_com_validate_version(ena_dev); 2950 if (unlikely(rc != 0)) { 2951 ena_log(pdev, ERR, "device version is too low\n"); 2952 goto err_mmio_read_less; 2953 } 2954 2955 dma_width = ena_com_get_dma_width(ena_dev); 2956 if (unlikely(dma_width < 0)) { 2957 ena_log(pdev, ERR, "Invalid dma width value %d", dma_width); 2958 rc = dma_width; 2959 goto err_mmio_read_less; 2960 } 2961 adapter->dma_width = dma_width; 2962 2963 /* ENA admin level init */ 2964 rc = ena_com_admin_init(ena_dev, &aenq_handlers); 2965 if (unlikely(rc != 0)) { 2966 ena_log(pdev, ERR, 2967 "Can not initialize ena admin queue with device\n"); 2968 goto err_mmio_read_less; 2969 } 2970 2971 /* 2972 * To enable the msix interrupts the driver needs to know the number 2973 * of queues. So the driver uses polling mode to retrieve this 2974 * information 2975 */ 2976 ena_com_set_admin_polling_mode(ena_dev, true); 2977 2978 ena_config_host_info(ena_dev, pdev); 2979 2980 /* Get Device Attributes */ 2981 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx); 2982 if (unlikely(rc != 0)) { 2983 ena_log(pdev, ERR, 2984 "Cannot get attribute for ena device rc: %d\n", rc); 2985 goto err_admin_init; 2986 } 2987 2988 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | 2989 BIT(ENA_ADMIN_FATAL_ERROR) | 2990 BIT(ENA_ADMIN_WARNING) | 2991 BIT(ENA_ADMIN_NOTIFICATION) | 2992 BIT(ENA_ADMIN_KEEP_ALIVE) | 2993 BIT(ENA_ADMIN_CONF_NOTIFICATIONS); 2994 2995 aenq_groups &= get_feat_ctx->aenq.supported_groups; 2996 rc = ena_com_set_aenq_config(ena_dev, aenq_groups); 2997 if (unlikely(rc != 0)) { 2998 ena_log(pdev, ERR, "Cannot configure aenq groups rc: %d\n", rc); 2999 goto err_admin_init; 3000 } 3001 3002 *wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE)); 3003 3004 set_default_llq_configurations(&llq_config, &get_feat_ctx->llq); 3005 3006 rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx->llq, 3007 &llq_config); 3008 if (unlikely(rc != 0)) { 3009 ena_log(pdev, ERR, "Failed to set placement policy\n"); 3010 goto err_admin_init; 3011 } 3012 3013 return (0); 3014 3015 err_admin_init: 3016 ena_com_delete_host_info(ena_dev); 3017 ena_com_admin_destroy(ena_dev); 3018 err_mmio_read_less: 3019 ena_com_mmio_reg_read_request_destroy(ena_dev); 3020 3021 return (rc); 3022 } 3023 3024 static int 3025 ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter) 3026 { 3027 struct ena_com_dev *ena_dev = adapter->ena_dev; 3028 int rc; 3029 3030 rc = ena_enable_msix(adapter); 3031 if (unlikely(rc != 0)) { 3032 ena_log(adapter->pdev, ERR, "Error with MSI-X enablement\n"); 3033 return (rc); 3034 } 3035 3036 ena_setup_mgmnt_intr(adapter); 3037 3038 rc = ena_request_mgmnt_irq(adapter); 3039 if (unlikely(rc != 0)) { 3040 ena_log(adapter->pdev, ERR, "Cannot setup mgmnt queue intr\n"); 3041 goto err_disable_msix; 3042 } 3043 3044 ena_com_set_admin_polling_mode(ena_dev, false); 3045 3046 ena_com_admin_aenq_enable(ena_dev); 3047 3048 return (0); 3049 3050 err_disable_msix: 3051 ena_disable_msix(adapter); 3052 3053 return (rc); 3054 } 3055 3056 /* Function called on ENA_ADMIN_KEEP_ALIVE event */ 3057 static void 3058 ena_keep_alive_wd(void *adapter_data, struct ena_admin_aenq_entry *aenq_e) 3059 { 3060 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 3061 struct ena_admin_aenq_keep_alive_desc *desc; 3062 sbintime_t stime; 3063 uint64_t rx_drops; 3064 uint64_t tx_drops; 3065 3066 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e; 3067 3068 rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low; 3069 tx_drops = ((uint64_t)desc->tx_drops_high << 32) | desc->tx_drops_low; 3070 counter_u64_zero(adapter->hw_stats.rx_drops); 3071 counter_u64_add(adapter->hw_stats.rx_drops, rx_drops); 3072 counter_u64_zero(adapter->hw_stats.tx_drops); 3073 counter_u64_add(adapter->hw_stats.tx_drops, tx_drops); 3074 3075 stime = getsbinuptime(); 3076 atomic_store_rel_64(&adapter->keep_alive_timestamp, stime); 3077 } 3078 3079 /* Check for keep alive expiration */ 3080 static void 3081 check_for_missing_keep_alive(struct ena_adapter *adapter) 3082 { 3083 sbintime_t timestamp, time; 3084 enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO; 3085 3086 if (adapter->wd_active == 0) 3087 return; 3088 3089 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3090 return; 3091 3092 timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp); 3093 time = getsbinuptime() - timestamp; 3094 if (unlikely(time > adapter->keep_alive_timeout)) { 3095 ena_log(adapter->pdev, ERR, "Keep alive watchdog timeout.\n"); 3096 if (ena_com_aenq_has_keep_alive(adapter->ena_dev)) 3097 reset_reason = ENA_REGS_RESET_MISSING_ADMIN_INTERRUPT; 3098 3099 ena_trigger_reset(adapter, reset_reason); 3100 } 3101 } 3102 3103 /* Check if admin queue is enabled */ 3104 static void 3105 check_for_admin_com_state(struct ena_adapter *adapter) 3106 { 3107 enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_ADMIN_TO; 3108 if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) == false)) { 3109 ena_log(adapter->pdev, ERR, 3110 "ENA admin queue is not in running state!\n"); 3111 counter_u64_add(adapter->dev_stats.admin_q_pause, 1); 3112 if (ena_com_get_missing_admin_interrupt(adapter->ena_dev)) 3113 reset_reason = ENA_REGS_RESET_MISSING_ADMIN_INTERRUPT; 3114 3115 ena_trigger_reset(adapter, reset_reason); 3116 } 3117 } 3118 3119 static int 3120 check_for_rx_interrupt_queue(struct ena_adapter *adapter, 3121 struct ena_ring *rx_ring) 3122 { 3123 if (likely(atomic_load_8(&rx_ring->first_interrupt))) 3124 return (0); 3125 3126 if (ena_com_cq_empty(rx_ring->ena_com_io_cq)) 3127 return (0); 3128 3129 rx_ring->no_interrupt_event_cnt++; 3130 3131 if (rx_ring->no_interrupt_event_cnt == 3132 ENA_MAX_NO_INTERRUPT_ITERATIONS) { 3133 ena_log(adapter->pdev, ERR, 3134 "Potential MSIX issue on Rx side Queue = %d. Reset the device\n", 3135 rx_ring->qid); 3136 ena_trigger_reset(adapter, ENA_REGS_RESET_MISS_INTERRUPT); 3137 return (EIO); 3138 } 3139 3140 return (0); 3141 } 3142 3143 static enum ena_regs_reset_reason_types 3144 check_cdesc_in_tx_cq(struct ena_adapter *adapter, 3145 struct ena_ring *tx_ring) 3146 { 3147 device_t pdev = adapter->pdev; 3148 int rc; 3149 u16 req_id; 3150 3151 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, &req_id); 3152 /* TX CQ is empty */ 3153 if (rc == ENA_COM_TRY_AGAIN) { 3154 ena_log(pdev, ERR, 3155 "No completion descriptors found in CQ %d\n", 3156 tx_ring->qid); 3157 return ENA_REGS_RESET_MISS_TX_CMPL; 3158 } 3159 3160 /* TX CQ has cdescs */ 3161 ena_log(pdev, ERR, 3162 "Completion descriptors found in CQ %d", 3163 tx_ring->qid); 3164 3165 return ENA_REGS_RESET_MISS_INTERRUPT; 3166 } 3167 3168 static int 3169 check_missing_comp_in_tx_queue(struct ena_adapter *adapter, 3170 struct ena_ring *tx_ring) 3171 { 3172 uint32_t missed_tx = 0, new_missed_tx = 0; 3173 device_t pdev = adapter->pdev; 3174 struct bintime curtime, time; 3175 struct ena_tx_buffer *tx_buf; 3176 int time_since_last_cleanup; 3177 int missing_tx_comp_to; 3178 sbintime_t time_offset; 3179 int i, rc = 0; 3180 enum ena_regs_reset_reason_types reset_reason = ENA_REGS_RESET_MISS_TX_CMPL; 3181 bool cleanup_scheduled, cleanup_running; 3182 3183 getbinuptime(&curtime); 3184 3185 for (i = 0; i < tx_ring->ring_size; i++) { 3186 tx_buf = &tx_ring->tx_buffer_info[i]; 3187 3188 if (bintime_isset(&tx_buf->timestamp) == 0) 3189 continue; 3190 3191 time = curtime; 3192 bintime_sub(&time, &tx_buf->timestamp); 3193 time_offset = bttosbt(time); 3194 3195 if (unlikely(!atomic_load_8(&tx_ring->first_interrupt) && 3196 time_offset > 2 * adapter->missing_tx_timeout)) { 3197 /* 3198 * If after graceful period interrupt is still not 3199 * received, we schedule a reset. 3200 */ 3201 ena_log(pdev, ERR, 3202 "Potential MSIX issue on Tx side Queue = %d. " 3203 "Reset the device\n", 3204 tx_ring->qid); 3205 ena_trigger_reset(adapter, 3206 ENA_REGS_RESET_MISS_INTERRUPT); 3207 return (EIO); 3208 } 3209 3210 /* Check again if packet is still waiting */ 3211 if (unlikely(time_offset > adapter->missing_tx_timeout)) { 3212 3213 if (tx_buf->print_once) { 3214 time_since_last_cleanup = TICKS_2_MSEC(ticks - 3215 tx_ring->tx_last_cleanup_ticks); 3216 missing_tx_comp_to = sbttoms( 3217 adapter->missing_tx_timeout); 3218 ena_log(pdev, WARN, 3219 "Found a Tx that wasn't completed on time, qid %d, index %d. " 3220 "%d msecs have passed since last cleanup. Missing Tx timeout value %d msecs.\n", 3221 tx_ring->qid, i, time_since_last_cleanup, 3222 missing_tx_comp_to); 3223 /* Add new TX completions which are missed */ 3224 new_missed_tx++; 3225 } 3226 3227 tx_buf->print_once = false; 3228 missed_tx++; 3229 } 3230 } 3231 /* Checking if this TX ring missing TX completions have passed the threshold */ 3232 if (unlikely(missed_tx > adapter->missing_tx_threshold)) { 3233 ena_log(pdev, ERR, 3234 "The number of lost tx completion is above the threshold " 3235 "(%d > %d). Reset the device\n", 3236 missed_tx, adapter->missing_tx_threshold); 3237 /* Set the reset flag to prevent ena_cleanup() from running */ 3238 ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter); 3239 /* Need to make sure that ENA_FLAG_TRIGGER_RESET is visible to ena_cleanup() and 3240 * that cleanup_running is visible to check_missing_comp_in_tx_queue() to 3241 * prevent the case of accessing CQ concurrently with check_cdesc_in_tx_cq() 3242 */ 3243 mb(); 3244 cleanup_scheduled = !!(atomic_load_16(&tx_ring->que->cleanup_task.ta_pending)); 3245 cleanup_running = !!(atomic_load_8((&tx_ring->cleanup_running))); 3246 if (!(cleanup_scheduled || cleanup_running)) 3247 reset_reason = check_cdesc_in_tx_cq(adapter, tx_ring); 3248 3249 adapter->reset_reason = reset_reason; 3250 rc = EIO; 3251 } 3252 /* Add the newly discovered missing TX completions */ 3253 counter_u64_add(tx_ring->tx_stats.missing_tx_comp, new_missed_tx); 3254 3255 return (rc); 3256 } 3257 3258 /* 3259 * Check for TX which were not completed on time. 3260 * Timeout is defined by "missing_tx_timeout". 3261 * Reset will be performed if number of incompleted 3262 * transactions exceeds "missing_tx_threshold". 3263 */ 3264 static void 3265 check_for_missing_completions(struct ena_adapter *adapter) 3266 { 3267 struct ena_ring *tx_ring; 3268 struct ena_ring *rx_ring; 3269 int i, budget, rc; 3270 3271 /* Make sure the driver doesn't turn the device in other process */ 3272 rmb(); 3273 3274 if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) 3275 return; 3276 3277 if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) 3278 return; 3279 3280 if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3281 return; 3282 3283 budget = adapter->missing_tx_max_queues; 3284 3285 for (i = adapter->next_monitored_tx_qid; i < adapter->num_io_queues; i++) { 3286 tx_ring = &adapter->tx_ring[i]; 3287 rx_ring = &adapter->rx_ring[i]; 3288 3289 rc = check_missing_comp_in_tx_queue(adapter, tx_ring); 3290 if (unlikely(rc != 0)) 3291 return; 3292 3293 rc = check_for_rx_interrupt_queue(adapter, rx_ring); 3294 if (unlikely(rc != 0)) 3295 return; 3296 3297 budget--; 3298 if (budget == 0) { 3299 i++; 3300 break; 3301 } 3302 } 3303 3304 adapter->next_monitored_tx_qid = i % adapter->num_io_queues; 3305 } 3306 3307 /* trigger rx cleanup after 2 consecutive detections */ 3308 #define EMPTY_RX_REFILL 2 3309 /* For the rare case where the device runs out of Rx descriptors and the 3310 * msix handler failed to refill new Rx descriptors (due to a lack of memory 3311 * for example). 3312 * This case will lead to a deadlock: 3313 * The device won't send interrupts since all the new Rx packets will be dropped 3314 * The msix handler won't allocate new Rx descriptors so the device won't be 3315 * able to send new packets. 3316 * 3317 * When such a situation is detected - execute rx cleanup task in another thread 3318 */ 3319 static void 3320 check_for_empty_rx_ring(struct ena_adapter *adapter) 3321 { 3322 struct ena_ring *rx_ring; 3323 int i, refill_required; 3324 3325 if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) 3326 return; 3327 3328 if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) 3329 return; 3330 3331 for (i = 0; i < adapter->num_io_queues; i++) { 3332 rx_ring = &adapter->rx_ring[i]; 3333 3334 refill_required = ena_com_free_q_entries( 3335 rx_ring->ena_com_io_sq); 3336 if (unlikely(refill_required == (rx_ring->ring_size - 1))) { 3337 rx_ring->empty_rx_queue++; 3338 3339 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) { 3340 counter_u64_add(rx_ring->rx_stats.empty_rx_ring, 3341 1); 3342 3343 ena_log(adapter->pdev, WARN, 3344 "Rx ring %d is stalled. Triggering the refill function\n", 3345 i); 3346 3347 taskqueue_enqueue(rx_ring->que->cleanup_tq, 3348 &rx_ring->que->cleanup_task); 3349 rx_ring->empty_rx_queue = 0; 3350 } 3351 } else { 3352 rx_ring->empty_rx_queue = 0; 3353 } 3354 } 3355 } 3356 3357 static void 3358 ena_update_hints(struct ena_adapter *adapter, 3359 struct ena_admin_ena_hw_hints *hints) 3360 { 3361 struct ena_com_dev *ena_dev = adapter->ena_dev; 3362 3363 if (hints->admin_completion_tx_timeout) 3364 ena_dev->admin_queue.completion_timeout = 3365 hints->admin_completion_tx_timeout * 1000; 3366 3367 if (hints->mmio_read_timeout) 3368 /* convert to usec */ 3369 ena_dev->mmio_read.reg_read_to = hints->mmio_read_timeout * 1000; 3370 3371 if (hints->missed_tx_completion_count_threshold_to_reset) 3372 adapter->missing_tx_threshold = 3373 hints->missed_tx_completion_count_threshold_to_reset; 3374 3375 if (hints->missing_tx_completion_timeout) { 3376 if (hints->missing_tx_completion_timeout == 3377 ENA_HW_HINTS_NO_TIMEOUT) 3378 adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT; 3379 else 3380 adapter->missing_tx_timeout = SBT_1MS * 3381 hints->missing_tx_completion_timeout; 3382 } 3383 3384 if (hints->driver_watchdog_timeout) { 3385 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3386 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT; 3387 else 3388 adapter->keep_alive_timeout = SBT_1MS * 3389 hints->driver_watchdog_timeout; 3390 } 3391 } 3392 3393 /** 3394 * ena_copy_eni_metrics - Get and copy ENI metrics from the HW. 3395 * @adapter: ENA device adapter 3396 * 3397 * Returns 0 on success, EOPNOTSUPP if current HW doesn't support those metrics 3398 * and other error codes on failure. 3399 * 3400 * This function can possibly cause a race with other calls to the admin queue. 3401 * Because of that, the caller should either lock this function or make sure 3402 * that there is no race in the current context. 3403 */ 3404 static int 3405 ena_copy_eni_metrics(struct ena_adapter *adapter) 3406 { 3407 static bool print_once = true; 3408 int rc; 3409 3410 rc = ena_com_get_eni_stats(adapter->ena_dev, &adapter->eni_metrics); 3411 3412 if (rc != 0) { 3413 if (rc == ENA_COM_UNSUPPORTED) { 3414 if (print_once) { 3415 ena_log(adapter->pdev, WARN, 3416 "Retrieving ENI metrics is not supported.\n"); 3417 print_once = false; 3418 } else { 3419 ena_log(adapter->pdev, DBG, 3420 "Retrieving ENI metrics is not supported.\n"); 3421 } 3422 } else { 3423 ena_log(adapter->pdev, ERR, 3424 "Failed to get ENI metrics: %d\n", rc); 3425 } 3426 } 3427 3428 return (rc); 3429 } 3430 3431 static int 3432 ena_copy_srd_metrics(struct ena_adapter *adapter) 3433 { 3434 return ena_com_get_ena_srd_info(adapter->ena_dev, &adapter->ena_srd_info); 3435 } 3436 3437 static int 3438 ena_copy_customer_metrics(struct ena_adapter *adapter) 3439 { 3440 struct ena_com_dev *dev; 3441 u32 supported_metrics_count; 3442 int rc, len; 3443 3444 dev = adapter->ena_dev; 3445 3446 supported_metrics_count = ena_com_get_customer_metric_count(dev); 3447 len = supported_metrics_count * sizeof(u64); 3448 3449 /* Fill the data buffer */ 3450 rc = ena_com_get_customer_metrics(adapter->ena_dev, 3451 (char *)(adapter->customer_metrics_array), len); 3452 3453 return (rc); 3454 } 3455 3456 static void 3457 ena_timer_service(void *data) 3458 { 3459 struct ena_adapter *adapter = (struct ena_adapter *)data; 3460 struct ena_admin_host_info *host_info = 3461 adapter->ena_dev->host_attr.host_info; 3462 3463 check_for_missing_keep_alive(adapter); 3464 3465 check_for_admin_com_state(adapter); 3466 3467 check_for_missing_completions(adapter); 3468 3469 check_for_empty_rx_ring(adapter); 3470 3471 /* 3472 * User controller update of the ENA metrics. 3473 * If the delay was set to 0, then the stats shouldn't be updated at 3474 * all. 3475 * Otherwise, wait 'metrics_sample_interval' seconds, before 3476 * updating stats. 3477 * As timer service is executed every second, it's enough to increment 3478 * appropriate counter each time the timer service is executed. 3479 */ 3480 if ((adapter->metrics_sample_interval != 0) && 3481 (++adapter->metrics_sample_interval_cnt >= 3482 adapter->metrics_sample_interval)) { 3483 taskqueue_enqueue(adapter->metrics_tq, &adapter->metrics_task); 3484 adapter->metrics_sample_interval_cnt = 0; 3485 } 3486 3487 3488 if (host_info != NULL) 3489 ena_update_host_info(host_info, adapter->ifp); 3490 3491 if (unlikely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) { 3492 /* 3493 * Timeout when validating version indicates that the device 3494 * became unresponsive. If that happens skip the reset and 3495 * reschedule timer service, so the reset can be retried later. 3496 */ 3497 if (ena_com_validate_version(adapter->ena_dev) == 3498 ENA_COM_TIMER_EXPIRED) { 3499 ena_log(adapter->pdev, WARN, 3500 "FW unresponsive, skipping reset\n"); 3501 ENA_TIMER_RESET(adapter); 3502 return; 3503 } 3504 ena_log(adapter->pdev, WARN, "Trigger reset is on\n"); 3505 taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task); 3506 return; 3507 } 3508 3509 /* 3510 * Schedule another timeout one second from now. 3511 */ 3512 ENA_TIMER_RESET(adapter); 3513 } 3514 3515 void 3516 ena_destroy_device(struct ena_adapter *adapter, bool graceful) 3517 { 3518 if_t ifp = adapter->ifp; 3519 struct ena_com_dev *ena_dev = adapter->ena_dev; 3520 bool dev_up; 3521 3522 if (!ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)) 3523 return; 3524 3525 if (!graceful) 3526 if_link_state_change(ifp, LINK_STATE_DOWN); 3527 3528 ENA_TIMER_DRAIN(adapter); 3529 3530 dev_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter); 3531 if (dev_up) 3532 ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 3533 3534 if (!graceful) 3535 ena_com_set_admin_running_state(ena_dev, false); 3536 3537 if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) 3538 ena_down(adapter); 3539 3540 /* 3541 * Stop the device from sending AENQ events (if the device was up, and 3542 * the trigger reset was on, ena_down already performs device reset) 3543 */ 3544 if (!(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter) && dev_up)) 3545 ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason); 3546 3547 ena_free_mgmnt_irq(adapter); 3548 3549 ena_disable_msix(adapter); 3550 3551 /* 3552 * IO rings resources should be freed because `ena_restore_device()` 3553 * calls (not directly) `ena_enable_msix()`, which re-allocates MSIX 3554 * vectors. The amount of MSIX vectors after destroy-restore may be 3555 * different than before. Therefore, IO rings resources should be 3556 * established from scratch each time. 3557 */ 3558 ena_free_all_io_rings_resources(adapter); 3559 3560 ena_com_abort_admin_commands(ena_dev); 3561 3562 ena_com_wait_for_abort_completion(ena_dev); 3563 3564 ena_com_admin_destroy(ena_dev); 3565 3566 ena_com_mmio_reg_read_request_destroy(ena_dev); 3567 3568 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3569 3570 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter); 3571 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter); 3572 } 3573 3574 static int 3575 ena_device_validate_params(struct ena_adapter *adapter, 3576 struct ena_com_dev_get_features_ctx *get_feat_ctx) 3577 { 3578 if (memcmp(get_feat_ctx->dev_attr.mac_addr, adapter->mac_addr, 3579 ETHER_ADDR_LEN) != 0) { 3580 ena_log(adapter->pdev, ERR, "Error, mac addresses differ\n"); 3581 return (EINVAL); 3582 } 3583 3584 if (get_feat_ctx->dev_attr.max_mtu < if_getmtu(adapter->ifp)) { 3585 ena_log(adapter->pdev, ERR, 3586 "Error, device max mtu is smaller than ifp MTU\n"); 3587 return (EINVAL); 3588 } 3589 3590 return 0; 3591 } 3592 3593 int 3594 ena_restore_device(struct ena_adapter *adapter) 3595 { 3596 struct ena_com_dev_get_features_ctx get_feat_ctx; 3597 struct ena_com_dev *ena_dev = adapter->ena_dev; 3598 if_t ifp = adapter->ifp; 3599 device_t dev = adapter->pdev; 3600 int wd_active; 3601 int rc; 3602 3603 ENA_FLAG_SET_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter); 3604 3605 rc = ena_device_init(adapter, dev, &get_feat_ctx, &wd_active); 3606 if (rc != 0) { 3607 ena_log(dev, ERR, "Cannot initialize device\n"); 3608 goto err; 3609 } 3610 /* 3611 * Only enable WD if it was enabled before reset, so it won't override 3612 * value set by the user by the sysctl. 3613 */ 3614 if (adapter->wd_active != 0) 3615 adapter->wd_active = wd_active; 3616 3617 rc = ena_device_validate_params(adapter, &get_feat_ctx); 3618 if (rc != 0) { 3619 ena_log(dev, ERR, "Validation of device parameters failed\n"); 3620 goto err_device_destroy; 3621 } 3622 3623 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter); 3624 /* Make sure we don't have a race with AENQ Links state handler */ 3625 if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) 3626 if_link_state_change(ifp, LINK_STATE_UP); 3627 3628 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 3629 if (rc != 0) { 3630 ena_log(dev, ERR, "Enable MSI-X failed\n"); 3631 goto err_device_destroy; 3632 } 3633 3634 /* 3635 * Effective value of used MSIX vectors should be the same as before 3636 * `ena_destroy_device()`, if possible, or closest to it if less vectors 3637 * are available. 3638 */ 3639 if ((adapter->msix_vecs - ENA_ADMIN_MSIX_VEC) < adapter->num_io_queues) 3640 adapter->num_io_queues = adapter->msix_vecs - ENA_ADMIN_MSIX_VEC; 3641 3642 /* Re-initialize rings basic information */ 3643 ena_init_io_rings(adapter); 3644 3645 /* If the interface was up before the reset bring it up */ 3646 if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) { 3647 rc = ena_up(adapter); 3648 if (rc != 0) { 3649 ena_log(dev, ERR, "Failed to create I/O queues\n"); 3650 goto err_disable_msix; 3651 } 3652 } 3653 3654 /* Indicate that device is running again and ready to work */ 3655 ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter); 3656 3657 /* 3658 * As the AENQ handlers weren't executed during reset because 3659 * the flag ENA_FLAG_DEVICE_RUNNING was turned off, the 3660 * timestamp must be updated again That will prevent next reset 3661 * caused by missing keep alive. 3662 */ 3663 adapter->keep_alive_timestamp = getsbinuptime(); 3664 ENA_TIMER_RESET(adapter); 3665 3666 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter); 3667 3668 return (rc); 3669 3670 err_disable_msix: 3671 ena_free_mgmnt_irq(adapter); 3672 ena_disable_msix(adapter); 3673 err_device_destroy: 3674 ena_com_abort_admin_commands(ena_dev); 3675 ena_com_wait_for_abort_completion(ena_dev); 3676 ena_com_admin_destroy(ena_dev); 3677 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE); 3678 ena_com_mmio_reg_read_request_destroy(ena_dev); 3679 err: 3680 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter); 3681 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter); 3682 ena_log(dev, ERR, "Reset attempt failed. Can not reset the device\n"); 3683 3684 return (rc); 3685 } 3686 3687 static void 3688 ena_metrics_task(void *arg, int pending) 3689 { 3690 struct ena_adapter *adapter = (struct ena_adapter *)arg; 3691 3692 ENA_LOCK_LOCK(); 3693 3694 if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_CUSTOMER_METRICS)) 3695 (void)ena_copy_customer_metrics(adapter); 3696 else if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENI_STATS)) 3697 (void)ena_copy_eni_metrics(adapter); 3698 3699 if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENA_SRD_INFO)) 3700 (void)ena_copy_srd_metrics(adapter); 3701 3702 ENA_LOCK_UNLOCK(); 3703 } 3704 3705 static void 3706 ena_reset_task(void *arg, int pending) 3707 { 3708 struct ena_adapter *adapter = (struct ena_adapter *)arg; 3709 3710 ENA_LOCK_LOCK(); 3711 if (likely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) { 3712 ena_increment_reset_counter(adapter); 3713 ena_destroy_device(adapter, false); 3714 ena_restore_device(adapter); 3715 3716 ena_log(adapter->pdev, INFO, 3717 "Device reset completed successfully, Driver info: %s\n", 3718 ena_version); 3719 } 3720 ENA_LOCK_UNLOCK(); 3721 } 3722 3723 static void 3724 ena_free_stats(struct ena_adapter *adapter) 3725 { 3726 ena_free_counters((counter_u64_t *)&adapter->hw_stats, 3727 sizeof(struct ena_hw_stats)); 3728 ena_free_counters((counter_u64_t *)&adapter->dev_stats, 3729 sizeof(struct ena_stats_dev)); 3730 3731 } 3732 /** 3733 * ena_attach - Device Initialization Routine 3734 * @pdev: device information struct 3735 * 3736 * Returns 0 on success, otherwise on failure. 3737 * 3738 * ena_attach initializes an adapter identified by a device structure. 3739 * The OS initialization, configuring of the adapter private structure, 3740 * and a hardware reset occur. 3741 **/ 3742 static int 3743 ena_attach(device_t pdev) 3744 { 3745 struct ena_com_dev_get_features_ctx get_feat_ctx; 3746 struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 }; 3747 static int version_printed; 3748 struct ena_adapter *adapter; 3749 struct ena_com_dev *ena_dev = NULL; 3750 uint32_t max_num_io_queues; 3751 int msix_rid; 3752 int rid, rc; 3753 3754 adapter = device_get_softc(pdev); 3755 adapter->pdev = pdev; 3756 adapter->first_bind = -1; 3757 3758 /* 3759 * Set up the timer service - driver is responsible for avoiding 3760 * concurrency, as the callout won't be using any locking inside. 3761 */ 3762 ENA_TIMER_INIT(adapter); 3763 adapter->keep_alive_timeout = ENA_DEFAULT_KEEP_ALIVE_TO; 3764 adapter->missing_tx_timeout = ENA_DEFAULT_TX_CMP_TO; 3765 adapter->missing_tx_max_queues = ENA_DEFAULT_TX_MONITORED_QUEUES; 3766 adapter->missing_tx_threshold = ENA_DEFAULT_TX_CMP_THRESHOLD; 3767 3768 adapter->irq_cpu_base = ENA_BASE_CPU_UNSPECIFIED; 3769 adapter->irq_cpu_stride = 0; 3770 3771 #ifdef RSS 3772 adapter->rss_enabled = 1; 3773 #endif 3774 3775 if (version_printed++ == 0) 3776 ena_log(pdev, INFO, "%s\n", ena_version); 3777 3778 /* Allocate memory for ena_dev structure */ 3779 ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF, 3780 M_WAITOK | M_ZERO); 3781 3782 adapter->ena_dev = ena_dev; 3783 ena_dev->dmadev = pdev; 3784 3785 rid = PCIR_BAR(ENA_REG_BAR); 3786 adapter->memory = NULL; 3787 adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY, &rid, 3788 RF_ACTIVE); 3789 if (unlikely(adapter->registers == NULL)) { 3790 ena_log(pdev, ERR, 3791 "unable to allocate bus resource: registers!\n"); 3792 rc = ENOMEM; 3793 goto err_dev_free; 3794 } 3795 3796 /* MSIx vector table may reside on BAR0 with registers or on BAR1. */ 3797 msix_rid = pci_msix_table_bar(pdev); 3798 if (msix_rid != rid) { 3799 adapter->msix = bus_alloc_resource_any(pdev, SYS_RES_MEMORY, 3800 &msix_rid, RF_ACTIVE); 3801 if (unlikely(adapter->msix == NULL)) { 3802 ena_log(pdev, ERR, 3803 "unable to allocate bus resource: msix!\n"); 3804 rc = ENOMEM; 3805 goto err_pci_free; 3806 } 3807 adapter->msix_rid = msix_rid; 3808 } 3809 3810 ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF, 3811 M_WAITOK | M_ZERO); 3812 3813 /* Store register resources */ 3814 ((struct ena_bus *)(ena_dev->bus))->reg_bar_t = rman_get_bustag( 3815 adapter->registers); 3816 ((struct ena_bus *)(ena_dev->bus))->reg_bar_h = rman_get_bushandle( 3817 adapter->registers); 3818 3819 if (unlikely(((struct ena_bus *)(ena_dev->bus))->reg_bar_h == 0)) { 3820 ena_log(pdev, ERR, "failed to pmap registers bar\n"); 3821 rc = ENXIO; 3822 goto err_bus_free; 3823 } 3824 3825 rc = ena_map_llq_mem_bar(pdev, ena_dev); 3826 if (unlikely(rc != 0)) { 3827 ena_log(pdev, ERR, "Failed to map ENA mem bar"); 3828 goto err_bus_free; 3829 } 3830 3831 ena_dev->ena_min_poll_delay_us = ENA_ADMIN_POLL_DELAY_US; 3832 3833 /* Initially clear all the flags */ 3834 ENA_FLAG_ZERO(adapter); 3835 3836 /* Device initialization */ 3837 rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active); 3838 if (unlikely(rc != 0)) { 3839 ena_log(pdev, ERR, "ENA device init failed! (err: %d)\n", rc); 3840 rc = ENXIO; 3841 goto err_bus_free; 3842 } 3843 3844 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 3845 adapter->disable_meta_caching = !!( 3846 get_feat_ctx.llq.accel_mode.u.get.supported_flags & 3847 BIT(ENA_ADMIN_DISABLE_META_CACHING)); 3848 3849 adapter->keep_alive_timestamp = getsbinuptime(); 3850 3851 adapter->tx_offload_cap = get_feat_ctx.offload.tx; 3852 3853 memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr, 3854 ETHER_ADDR_LEN); 3855 3856 calc_queue_ctx.pdev = pdev; 3857 calc_queue_ctx.ena_dev = ena_dev; 3858 calc_queue_ctx.get_feat_ctx = &get_feat_ctx; 3859 3860 /* Calculate initial and maximum IO queue number and size */ 3861 max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, 3862 &get_feat_ctx); 3863 rc = ena_calc_io_queue_size(&calc_queue_ctx); 3864 if (unlikely((rc != 0) || (max_num_io_queues <= 0))) { 3865 rc = EFAULT; 3866 goto err_com_free; 3867 } 3868 3869 adapter->requested_tx_ring_size = calc_queue_ctx.tx_queue_size; 3870 adapter->requested_rx_ring_size = calc_queue_ctx.rx_queue_size; 3871 adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size; 3872 adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size; 3873 adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size; 3874 adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size; 3875 3876 adapter->max_num_io_queues = max_num_io_queues; 3877 3878 adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE; 3879 3880 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu; 3881 3882 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3883 3884 /* set up dma tags for rx and tx buffers */ 3885 rc = ena_setup_tx_dma_tag(adapter); 3886 if (unlikely(rc != 0)) { 3887 ena_log(pdev, ERR, "Failed to create TX DMA tag\n"); 3888 goto err_com_free; 3889 } 3890 3891 rc = ena_setup_rx_dma_tag(adapter); 3892 if (unlikely(rc != 0)) { 3893 ena_log(pdev, ERR, "Failed to create RX DMA tag\n"); 3894 goto err_tx_tag_free; 3895 } 3896 3897 /* 3898 * The amount of requested MSIX vectors is equal to 3899 * adapter::max_num_io_queues (see `ena_enable_msix()`), plus a constant 3900 * number of admin queue interrupts. The former is initially determined 3901 * by HW capabilities (see `ena_calc_max_io_queue_num())` but may not be 3902 * achieved if there are not enough system resources. By default, the 3903 * number of effectively used IO queues is the same but later on it can 3904 * be limited by the user using sysctl interface. 3905 */ 3906 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 3907 if (unlikely(rc != 0)) { 3908 ena_log(pdev, ERR, 3909 "Failed to enable and set the admin interrupts\n"); 3910 goto err_io_free; 3911 } 3912 /* By default all of allocated MSIX vectors are actively used */ 3913 adapter->num_io_queues = adapter->msix_vecs - ENA_ADMIN_MSIX_VEC; 3914 3915 /* initialize rings basic information */ 3916 ena_init_io_rings(adapter); 3917 3918 rc = ena_com_allocate_customer_metrics_buffer(ena_dev); 3919 if (rc) { 3920 ena_log(pdev, ERR, "Failed to allocate customer metrics buffer.\n"); 3921 goto err_msix_free; 3922 } 3923 3924 rc = ena_sysctl_allocate_customer_metrics_buffer(adapter); 3925 if (unlikely(rc)){ 3926 ena_log(pdev, ERR, "Failed to allocate sysctl customer metrics buffer.\n"); 3927 goto err_metrics_buffer_destroy; 3928 } 3929 3930 /* Initialize statistics */ 3931 ena_alloc_counters((counter_u64_t *)&adapter->dev_stats, 3932 sizeof(struct ena_stats_dev)); 3933 ena_alloc_counters((counter_u64_t *)&adapter->hw_stats, 3934 sizeof(struct ena_hw_stats)); 3935 ena_sysctl_add_nodes(adapter); 3936 3937 /* setup network interface */ 3938 ena_setup_ifnet(pdev, adapter, &get_feat_ctx); 3939 3940 /* Initialize reset task queue */ 3941 TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter); 3942 adapter->reset_tq = taskqueue_create("ena_reset_enqueue", 3943 M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq); 3944 taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET, "%s rstq", 3945 device_get_nameunit(adapter->pdev)); 3946 3947 /* Initialize metrics task queue */ 3948 TASK_INIT(&adapter->metrics_task, 0, ena_metrics_task, adapter); 3949 adapter->metrics_tq = taskqueue_create("ena_metrics_enqueue", 3950 M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->metrics_tq); 3951 taskqueue_start_threads(&adapter->metrics_tq, 1, PI_NET, "%s metricsq", 3952 device_get_nameunit(adapter->pdev)); 3953 3954 #ifdef DEV_NETMAP 3955 rc = ena_netmap_attach(adapter); 3956 if (rc != 0) { 3957 ena_log(pdev, ERR, "netmap attach failed: %d\n", rc); 3958 goto err_detach; 3959 } 3960 #endif /* DEV_NETMAP */ 3961 3962 /* Tell the stack that the interface is not active */ 3963 if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 3964 ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter); 3965 3966 /* Run the timer service */ 3967 ENA_TIMER_RESET(adapter); 3968 3969 return (0); 3970 3971 #ifdef DEV_NETMAP 3972 err_detach: 3973 ether_ifdetach(adapter->ifp); 3974 free(adapter->customer_metrics_array, M_DEVBUF); 3975 #endif /* DEV_NETMAP */ 3976 err_metrics_buffer_destroy: 3977 ena_com_delete_customer_metrics_buffer(ena_dev); 3978 err_msix_free: 3979 ena_free_stats(adapter); 3980 ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR); 3981 ena_free_mgmnt_irq(adapter); 3982 ena_disable_msix(adapter); 3983 err_io_free: 3984 ena_free_all_io_rings_resources(adapter); 3985 ena_free_rx_dma_tag(adapter); 3986 err_tx_tag_free: 3987 ena_free_tx_dma_tag(adapter); 3988 err_com_free: 3989 ena_com_admin_destroy(ena_dev); 3990 ena_com_delete_host_info(ena_dev); 3991 ena_com_mmio_reg_read_request_destroy(ena_dev); 3992 err_bus_free: 3993 free(ena_dev->bus, M_DEVBUF); 3994 err_pci_free: 3995 ena_free_pci_resources(adapter); 3996 err_dev_free: 3997 free(ena_dev, M_DEVBUF); 3998 3999 return (rc); 4000 } 4001 4002 /** 4003 * ena_detach - Device Removal Routine 4004 * @pdev: device information struct 4005 * 4006 * ena_detach is called by the device subsystem to alert the driver 4007 * that it should release a PCI device. 4008 **/ 4009 static int 4010 ena_detach(device_t pdev) 4011 { 4012 struct ena_adapter *adapter = device_get_softc(pdev); 4013 struct ena_com_dev *ena_dev = adapter->ena_dev; 4014 int rc; 4015 4016 /* Make sure VLANS are not using driver */ 4017 if (if_vlantrunkinuse(adapter->ifp)) { 4018 ena_log(adapter->pdev, ERR, "VLAN is in use, detach first\n"); 4019 return (EBUSY); 4020 } 4021 4022 ether_ifdetach(adapter->ifp); 4023 4024 /* Stop timer service */ 4025 ENA_LOCK_LOCK(); 4026 ENA_TIMER_DRAIN(adapter); 4027 ENA_LOCK_UNLOCK(); 4028 4029 /* Release metrics task */ 4030 while (taskqueue_cancel(adapter->metrics_tq, &adapter->metrics_task, NULL)) 4031 taskqueue_drain(adapter->metrics_tq, &adapter->metrics_task); 4032 taskqueue_free(adapter->metrics_tq); 4033 4034 /* Release reset task */ 4035 while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL)) 4036 taskqueue_drain(adapter->reset_tq, &adapter->reset_task); 4037 taskqueue_free(adapter->reset_tq); 4038 4039 ENA_LOCK_LOCK(); 4040 ena_down(adapter); 4041 ena_destroy_device(adapter, true); 4042 ENA_LOCK_UNLOCK(); 4043 4044 /* Restore unregistered sysctl queue nodes. */ 4045 ena_sysctl_update_queue_node_nb(adapter, adapter->num_io_queues, 4046 adapter->max_num_io_queues); 4047 4048 #ifdef DEV_NETMAP 4049 netmap_detach(adapter->ifp); 4050 #endif /* DEV_NETMAP */ 4051 4052 ena_free_stats(adapter); 4053 4054 rc = ena_free_rx_dma_tag(adapter); 4055 if (unlikely(rc != 0)) 4056 ena_log(adapter->pdev, WARN, 4057 "Unmapped RX DMA tag associations\n"); 4058 4059 rc = ena_free_tx_dma_tag(adapter); 4060 if (unlikely(rc != 0)) 4061 ena_log(adapter->pdev, WARN, 4062 "Unmapped TX DMA tag associations\n"); 4063 4064 ena_free_irqs(adapter); 4065 4066 ena_free_pci_resources(adapter); 4067 4068 if (adapter->rss_indir != NULL) 4069 free(adapter->rss_indir, M_DEVBUF); 4070 4071 if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) 4072 ena_com_rss_destroy(ena_dev); 4073 4074 ena_com_delete_host_info(ena_dev); 4075 4076 free(adapter->customer_metrics_array, M_DEVBUF); 4077 4078 ena_com_delete_customer_metrics_buffer(ena_dev); 4079 4080 if_free(adapter->ifp); 4081 4082 free(ena_dev->bus, M_DEVBUF); 4083 4084 free(ena_dev, M_DEVBUF); 4085 4086 return (bus_generic_detach(pdev)); 4087 } 4088 4089 /****************************************************************************** 4090 ******************************** AENQ Handlers ******************************* 4091 *****************************************************************************/ 4092 /** 4093 * ena_update_on_link_change: 4094 * Notify the network interface about the change in link status 4095 **/ 4096 static void 4097 ena_update_on_link_change(void *adapter_data, 4098 struct ena_admin_aenq_entry *aenq_e) 4099 { 4100 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4101 struct ena_admin_aenq_link_change_desc *aenq_desc; 4102 int status; 4103 if_t ifp; 4104 4105 aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e; 4106 ifp = adapter->ifp; 4107 status = aenq_desc->flags & 4108 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK; 4109 4110 if (status != 0) { 4111 ena_log(adapter->pdev, INFO, "link is UP\n"); 4112 ENA_FLAG_SET_ATOMIC(ENA_FLAG_LINK_UP, adapter); 4113 if (!ENA_FLAG_ISSET(ENA_FLAG_ONGOING_RESET, adapter)) 4114 if_link_state_change(ifp, LINK_STATE_UP); 4115 } else { 4116 ena_log(adapter->pdev, INFO, "link is DOWN\n"); 4117 if_link_state_change(ifp, LINK_STATE_DOWN); 4118 ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_LINK_UP, adapter); 4119 } 4120 } 4121 4122 static void 4123 ena_notification(void *adapter_data, struct ena_admin_aenq_entry *aenq_e) 4124 { 4125 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4126 struct ena_admin_ena_hw_hints *hints; 4127 4128 ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION, 4129 adapter->ena_dev, "Invalid group(%x) expected %x\n", 4130 aenq_e->aenq_common_desc.group, ENA_ADMIN_NOTIFICATION); 4131 4132 switch (aenq_e->aenq_common_desc.syndrome) { 4133 case ENA_ADMIN_UPDATE_HINTS: 4134 hints = 4135 (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4); 4136 ena_update_hints(adapter, hints); 4137 break; 4138 default: 4139 ena_log(adapter->pdev, ERR, 4140 "Invalid aenq notification link state %d\n", 4141 aenq_e->aenq_common_desc.syndrome); 4142 } 4143 } 4144 4145 static void 4146 ena_lock_init(void *arg) 4147 { 4148 ENA_LOCK_INIT(); 4149 } 4150 SYSINIT(ena_lock_init, SI_SUB_LOCK, SI_ORDER_FIRST, ena_lock_init, NULL); 4151 4152 static void 4153 ena_lock_uninit(void *arg) 4154 { 4155 ENA_LOCK_DESTROY(); 4156 } 4157 SYSUNINIT(ena_lock_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, ena_lock_uninit, NULL); 4158 4159 /** 4160 * This handler will called for unknown event group or unimplemented handlers 4161 **/ 4162 static void 4163 unimplemented_aenq_handler(void *adapter_data, 4164 struct ena_admin_aenq_entry *aenq_e) 4165 { 4166 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4167 4168 ena_log(adapter->pdev, ERR, 4169 "Unknown event was received or event with unimplemented handler\n"); 4170 } 4171 4172 static void ena_conf_notification(void *adapter_data, 4173 struct ena_admin_aenq_entry *aenq_e) 4174 { 4175 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4176 struct ena_admin_aenq_conf_notifications_desc *desc; 4177 u64 bitmap, bit; 4178 4179 desc = (struct ena_admin_aenq_conf_notifications_desc *)aenq_e; 4180 bitmap = desc->notifications_bitmap; 4181 4182 if (bitmap == 0) { 4183 ena_log(adapter->pdev, INFO, 4184 "Empty configuration notification bitmap\n"); 4185 return; 4186 } 4187 4188 for (bit = ffsll(bitmap); bit != 0; bit = ffsll(bitmap)) { 4189 bit--; 4190 ena_log(adapter->pdev, INFO, 4191 "Sub-optimal configuration notification code: %" PRIu64 " Refer to AWS ENA documentation for additional details and mitigation options.\n", 4192 bit + 1); 4193 // Clear the processed bit 4194 bitmap &= ~(1UL << bit); 4195 } 4196 } 4197 4198 static struct ena_aenq_handlers aenq_handlers = { 4199 .handlers = { 4200 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change, 4201 [ENA_ADMIN_NOTIFICATION] = ena_notification, 4202 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd, 4203 [ENA_ADMIN_CONF_NOTIFICATIONS] = ena_conf_notification, 4204 }, 4205 .unimplemented_handler = unimplemented_aenq_handler 4206 }; 4207 4208 /********************************************************************* 4209 * FreeBSD Device Interface Entry Points 4210 *********************************************************************/ 4211 4212 static device_method_t ena_methods[] = { /* Device interface */ 4213 DEVMETHOD(device_probe, ena_probe), 4214 DEVMETHOD(device_attach, ena_attach), 4215 DEVMETHOD(device_detach, ena_detach), DEVMETHOD_END 4216 }; 4217 4218 static driver_t ena_driver = { 4219 "ena", 4220 ena_methods, 4221 sizeof(struct ena_adapter), 4222 }; 4223 4224 DRIVER_MODULE(ena, pci, ena_driver, 0, 0); 4225 MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array, 4226 nitems(ena_vendor_info_array) - 1); 4227 MODULE_DEPEND(ena, pci, 1, 1, 1); 4228 MODULE_DEPEND(ena, ether, 1, 1, 1); 4229 #ifdef DEV_NETMAP 4230 MODULE_DEPEND(ena, netmap, 1, 1, 1); 4231 #endif /* DEV_NETMAP */ 4232 4233 /*********************************************************************/ 4234