1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) 2015-2017 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 * $FreeBSD: head/sys/dev/ena/ena.c 325593 2017-11-09 13:38:17Z mw $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/endian.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/module.h> 42 #include <sys/rman.h> 43 #include <sys/socket.h> 44 #include <sys/sockio.h> 45 #include <sys/sysctl.h> 46 #include <sys/taskqueue.h> 47 #include <sys/time.h> 48 #include <sys/eventhandler.h> 49 50 #include <net/bpf.h> 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/if_arp.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 #include <net/if_types.h> 57 #include <net/ifq_var.h> 58 #include <net/vlan/if_vlan_var.h> 59 60 #include <netinet/in_systm.h> 61 #include <netinet/in.h> 62 #include <netinet/if_ether.h> 63 #include <netinet/ip.h> 64 #include <netinet/ip6.h> 65 #include <netinet/tcp.h> 66 #include <netinet/udp.h> 67 68 #include <bus/pci/pcivar.h> 69 #include <bus/pci/pcireg.h> 70 71 #include "ena.h" 72 #include "ena_sysctl.h" 73 74 /********************************************************* 75 * Function prototypes 76 *********************************************************/ 77 static int ena_probe(device_t); 78 static void ena_intr_msix_mgmnt(void *); 79 static int ena_allocate_pci_resources(struct ena_adapter*); 80 static void ena_free_pci_resources(struct ena_adapter *); 81 static int ena_change_mtu(if_t, int); 82 #if 0 /* XXX swildner counters */ 83 static inline void ena_alloc_counters(counter_u64_t *, int); 84 static inline void ena_free_counters(counter_u64_t *, int); 85 static inline void ena_reset_counters(counter_u64_t *, int); 86 #endif 87 static void ena_init_io_rings_common(struct ena_adapter *, 88 struct ena_ring *, uint16_t); 89 static void ena_init_io_rings(struct ena_adapter *); 90 static void ena_free_io_ring_resources(struct ena_adapter *, unsigned int); 91 static void ena_free_all_io_rings_resources(struct ena_adapter *); 92 static int ena_setup_tx_dma_tag(struct ena_adapter *); 93 static int ena_free_tx_dma_tag(struct ena_adapter *); 94 static int ena_setup_rx_dma_tag(struct ena_adapter *); 95 static int ena_free_rx_dma_tag(struct ena_adapter *); 96 static int ena_setup_tx_resources(struct ena_adapter *, int); 97 static void ena_free_tx_resources(struct ena_adapter *, int); 98 static int ena_setup_all_tx_resources(struct ena_adapter *); 99 static void ena_free_all_tx_resources(struct ena_adapter *); 100 static inline int validate_rx_req_id(struct ena_ring *, uint16_t); 101 static int ena_setup_rx_resources(struct ena_adapter *, unsigned int); 102 static void ena_free_rx_resources(struct ena_adapter *, unsigned int); 103 static int ena_setup_all_rx_resources(struct ena_adapter *); 104 static void ena_free_all_rx_resources(struct ena_adapter *); 105 static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *, 106 struct ena_rx_buffer *); 107 static void ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *, 108 struct ena_rx_buffer *); 109 static int ena_refill_rx_bufs(struct ena_ring *, uint32_t); 110 static void ena_free_rx_bufs(struct ena_adapter *, unsigned int); 111 static void ena_refill_all_rx_bufs(struct ena_adapter *); 112 static void ena_free_all_rx_bufs(struct ena_adapter *); 113 static void ena_free_tx_bufs(struct ena_adapter *, unsigned int); 114 static void ena_free_all_tx_bufs(struct ena_adapter *); 115 static void ena_destroy_all_tx_queues(struct ena_adapter *); 116 static void ena_destroy_all_rx_queues(struct ena_adapter *); 117 static void ena_destroy_all_io_queues(struct ena_adapter *); 118 static int ena_create_io_queues(struct ena_adapter *); 119 static int ena_tx_cleanup(struct ena_ring *); 120 static void ena_deferred_rx_cleanup(void *, int); 121 static int ena_rx_cleanup(struct ena_ring *); 122 static inline int validate_tx_req_id(struct ena_ring *, uint16_t); 123 static void ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *, 124 struct mbuf *); 125 static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *, 126 struct ena_com_rx_ctx *, uint16_t *); 127 static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *, 128 struct mbuf *); 129 static void ena_handle_msix(void *); 130 static int ena_enable_msix(struct ena_adapter *); 131 static void ena_setup_mgmnt_intr(struct ena_adapter *); 132 static void ena_setup_io_intr(struct ena_adapter *); 133 static int ena_request_mgmnt_irq(struct ena_adapter *); 134 static int ena_request_io_irq(struct ena_adapter *); 135 static void ena_free_mgmnt_irq(struct ena_adapter *); 136 static void ena_free_io_irq(struct ena_adapter *); 137 static void ena_free_irqs(struct ena_adapter*); 138 static void ena_disable_msix(struct ena_adapter *); 139 static void ena_unmask_all_io_irqs(struct ena_adapter *); 140 static int ena_rss_configure(struct ena_adapter *); 141 static int ena_up_complete(struct ena_adapter *); 142 static int ena_up(struct ena_adapter *); 143 static void ena_down(struct ena_adapter *); 144 #if 0 /* XXX swildner counters */ 145 static uint64_t ena_get_counter(if_t, ift_counter); 146 #endif 147 static int ena_media_change(if_t); 148 static void ena_media_status(if_t, struct ifmediareq *); 149 static void ena_init(void *); 150 static int ena_ioctl(if_t, u_long, caddr_t, struct ucred *); 151 static int ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *); 152 static void ena_update_host_info(struct ena_admin_host_info *, if_t); 153 static void ena_update_hwassist(struct ena_adapter *); 154 static int ena_setup_ifnet(device_t, struct ena_adapter *, 155 struct ena_com_dev_get_features_ctx *); 156 static void ena_tx_csum(struct ena_com_tx_ctx *, struct mbuf *); 157 static int ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, 158 struct mbuf **mbuf); 159 static int ena_xmit_mbuf(struct ena_ring *, struct mbuf **); 160 static void ena_start_xmit(struct ifnet *, struct ifaltq_subque *); 161 static int ena_calc_io_queue_num(struct ena_adapter *, 162 struct ena_com_dev_get_features_ctx *); 163 static int ena_calc_queue_size(struct ena_adapter *, uint16_t *, 164 uint16_t *, struct ena_com_dev_get_features_ctx *); 165 static int ena_rss_init_default(struct ena_adapter *); 166 static void ena_rss_init_default_deferred(void *); 167 static void ena_config_host_info(struct ena_com_dev *); 168 static int ena_attach(device_t); 169 static int ena_detach(device_t); 170 static int ena_device_init(struct ena_adapter *, device_t, 171 struct ena_com_dev_get_features_ctx *, int *); 172 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *, 173 int); 174 static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *); 175 static void unimplemented_aenq_handler(void *, 176 struct ena_admin_aenq_entry *); 177 static void ena_timer_service(void *); 178 179 static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION; 180 181 static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD, 0, "ENA driver parameters"); 182 183 /* 184 * Logging level for changing verbosity of the output 185 */ 186 int ena_log_level = ENA_ALERT | ENA_WARNING; 187 TUNABLE_INT("hw.ena.ena_log_level", &ena_log_level); 188 SYSCTL_INT(_hw_ena, OID_AUTO, log_level, CTLFLAG_RW, 189 &ena_log_level, 0, "Logging level indicating verbosity of the logs"); 190 191 static ena_vendor_info_t ena_vendor_info_array[] = { 192 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0}, 193 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0}, 194 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0}, 195 { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0}, 196 /* Last entry */ 197 { 0, 0, 0 } 198 }; 199 200 /* 201 * Contains pointers to event handlers, e.g. link state chage. 202 */ 203 static struct ena_aenq_handlers aenq_handlers; 204 205 void 206 ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error) 207 { 208 if (error != 0) 209 return; 210 *(bus_addr_t *) arg = segs[0].ds_addr; 211 } 212 213 int 214 ena_dma_alloc(device_t dmadev, bus_size_t size, 215 ena_mem_handle_t *dma , int mapflags) 216 { 217 struct ena_adapter* adapter = device_get_softc(dmadev); 218 uint32_t maxsize; 219 uint64_t dma_space_addr; 220 int error; 221 222 maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE; 223 224 dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width); 225 if (unlikely(dma_space_addr == 0)) 226 dma_space_addr = BUS_SPACE_MAXADDR; 227 228 error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */ 229 8, 0, /* alignment, bounds */ 230 dma_space_addr, /* lowaddr of exclusion window */ 231 BUS_SPACE_MAXADDR,/* highaddr of exclusion window */ 232 NULL, NULL, /* filter, filterarg */ 233 maxsize, /* maxsize */ 234 1, /* nsegments */ 235 maxsize, /* maxsegsize */ 236 BUS_DMA_ALLOCNOW, /* flags */ 237 &dma->tag); 238 if (unlikely(error != 0)) { 239 ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error); 240 goto fail_tag; 241 } 242 243 error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr, 244 BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map); 245 if (unlikely(error != 0)) { 246 ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n", 247 (uintmax_t)size, error); 248 goto fail_map_create; 249 } 250 251 dma->paddr = 0; 252 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, 253 size, ena_dmamap_callback, &dma->paddr, mapflags); 254 if (unlikely((error != 0) || (dma->paddr == 0))) { 255 ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error); 256 goto fail_map_load; 257 } 258 259 return (0); 260 261 fail_map_load: 262 bus_dmamem_free(dma->tag, dma->vaddr, dma->map); 263 fail_map_create: 264 bus_dma_tag_destroy(dma->tag); 265 fail_tag: 266 dma->tag = NULL; 267 268 return (error); 269 } 270 271 static int 272 ena_allocate_pci_resources(struct ena_adapter* adapter) 273 { 274 device_t pdev = adapter->pdev; 275 int rid; 276 277 rid = PCIR_BAR(ENA_REG_BAR); 278 adapter->memory = NULL; 279 adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY, 280 &rid, RF_ACTIVE); 281 if (unlikely(adapter->registers == NULL)) { 282 device_printf(pdev, "Unable to allocate bus resource: " 283 "registers\n"); 284 return (ENXIO); 285 } 286 287 return (0); 288 } 289 290 static void 291 ena_free_pci_resources(struct ena_adapter *adapter) 292 { 293 device_t pdev = adapter->pdev; 294 295 if (adapter->memory != NULL) { 296 bus_release_resource(pdev, SYS_RES_MEMORY, 297 PCIR_BAR(ENA_MEM_BAR), adapter->memory); 298 } 299 300 if (adapter->registers != NULL) { 301 bus_release_resource(pdev, SYS_RES_MEMORY, 302 PCIR_BAR(ENA_REG_BAR), adapter->registers); 303 } 304 } 305 306 static int 307 ena_probe(device_t dev) 308 { 309 ena_vendor_info_t *ent; 310 char adapter_name[60]; 311 uint16_t pci_vendor_id = 0; 312 uint16_t pci_device_id = 0; 313 314 pci_vendor_id = pci_get_vendor(dev); 315 pci_device_id = pci_get_device(dev); 316 317 ent = ena_vendor_info_array; 318 while (ent->vendor_id != 0) { 319 if ((pci_vendor_id == ent->vendor_id) && 320 (pci_device_id == ent->device_id)) { 321 ena_trace(ENA_DBG, "vendor=%x device=%x ", 322 pci_vendor_id, pci_device_id); 323 324 ksprintf(adapter_name, DEVICE_DESC); 325 device_set_desc_copy(dev, adapter_name); 326 return (BUS_PROBE_DEFAULT); 327 } 328 329 ent++; 330 331 } 332 333 return (ENXIO); 334 } 335 336 static int 337 ena_change_mtu(if_t ifp, int new_mtu) 338 { 339 struct ena_adapter *adapter = ifp->if_softc; 340 int rc; 341 342 if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) { 343 device_printf(adapter->pdev, "Invalid MTU setting. " 344 "new_mtu: %d max mtu: %d min mtu: %d\n", 345 new_mtu, adapter->max_mtu, ENA_MIN_MTU); 346 return (EINVAL); 347 } 348 349 rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu); 350 if (likely(rc == 0)) { 351 ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu); 352 ifp->if_mtu = new_mtu; 353 } else { 354 device_printf(adapter->pdev, "Failed to set MTU to %d\n", 355 new_mtu); 356 } 357 358 return (rc); 359 } 360 361 #if 0 /* XXX swildner counters */ 362 static inline void 363 ena_alloc_counters(counter_u64_t *begin, int size) 364 { 365 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 366 367 for (; begin < end; ++begin) 368 *begin = counter_u64_alloc(M_WAITOK); 369 } 370 371 static inline void 372 ena_free_counters(counter_u64_t *begin, int size) 373 { 374 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 375 376 for (; begin < end; ++begin) 377 counter_u64_free(*begin); 378 } 379 380 static inline void 381 ena_reset_counters(counter_u64_t *begin, int size) 382 { 383 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 384 385 for (; begin < end; ++begin) 386 counter_u64_zero(*begin); 387 } 388 #endif 389 390 static void 391 ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring, 392 uint16_t qid) 393 { 394 395 ring->qid = qid; 396 ring->adapter = adapter; 397 ring->ena_dev = adapter->ena_dev; 398 } 399 400 static void 401 ena_init_io_rings(struct ena_adapter *adapter) 402 { 403 struct ena_com_dev *ena_dev; 404 struct ena_ring *txr, *rxr; 405 struct ena_que *que; 406 int i; 407 408 ena_dev = adapter->ena_dev; 409 410 for (i = 0; i < adapter->num_queues; i++) { 411 txr = &adapter->tx_ring[i]; 412 rxr = &adapter->rx_ring[i]; 413 414 /* TX/RX common ring state */ 415 ena_init_io_rings_common(adapter, txr, i); 416 ena_init_io_rings_common(adapter, rxr, i); 417 418 /* TX specific ring state */ 419 txr->ring_size = adapter->tx_ring_size; 420 txr->tx_max_header_size = ena_dev->tx_max_header_size; 421 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type; 422 txr->smoothed_interval = 423 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev); 424 425 #if 0 /* XXX swildner counters */ 426 /* Alloc TX statistics. */ 427 ena_alloc_counters((counter_u64_t *)&txr->tx_stats, 428 sizeof(txr->tx_stats)); 429 #endif 430 431 /* RX specific ring state */ 432 rxr->ring_size = adapter->rx_ring_size; 433 rxr->smoothed_interval = 434 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev); 435 436 #if 0 /* XXX swildner counters */ 437 /* Alloc RX statistics. */ 438 ena_alloc_counters((counter_u64_t *)&rxr->rx_stats, 439 sizeof(rxr->rx_stats)); 440 #endif 441 442 /* Initialize locks */ 443 ksnprintf(txr->lock_name, nitems(txr->lock_name), "%s:tx(%d)", 444 device_get_nameunit(adapter->pdev), i); 445 ksnprintf(rxr->lock_name, nitems(rxr->lock_name), "%s:rx(%d)", 446 device_get_nameunit(adapter->pdev), i); 447 448 lockinit(&txr->ring_lock, txr->lock_name, 0, LK_CANRECURSE); 449 lockinit(&rxr->ring_lock, rxr->lock_name, 0, LK_CANRECURSE); 450 451 que = &adapter->que[i]; 452 que->adapter = adapter; 453 que->id = i; 454 que->tx_ring = txr; 455 que->rx_ring = rxr; 456 457 txr->que = que; 458 rxr->que = que; 459 460 rxr->empty_rx_queue = 0; 461 } 462 } 463 464 static void 465 ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid) 466 { 467 struct ena_ring *txr = &adapter->tx_ring[qid]; 468 struct ena_ring *rxr = &adapter->rx_ring[qid]; 469 470 #if 0 /* XXX swildner counters */ 471 ena_free_counters((counter_u64_t *)&txr->tx_stats, 472 sizeof(txr->tx_stats)); 473 ena_free_counters((counter_u64_t *)&rxr->rx_stats, 474 sizeof(rxr->rx_stats)); 475 #endif 476 477 lockuninit(&txr->ring_lock); 478 lockuninit(&rxr->ring_lock); 479 } 480 481 static void 482 ena_free_all_io_rings_resources(struct ena_adapter *adapter) 483 { 484 int i; 485 486 for (i = 0; i < adapter->num_queues; i++) 487 ena_free_io_ring_resources(adapter, i); 488 489 } 490 491 static int 492 ena_setup_tx_dma_tag(struct ena_adapter *adapter) 493 { 494 int ret; 495 496 /* Create DMA tag for Tx buffers */ 497 ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), 498 1, 0, /* alignment, bounds */ 499 ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window */ 500 BUS_SPACE_MAXADDR, /* highaddr of excl window */ 501 NULL, NULL, /* filter, filterarg */ 502 ENA_TSO_MAXSIZE, /* maxsize */ 503 ENA_BUS_DMA_SEGS, /* nsegments */ 504 ENA_TSO_MAXSIZE, /* maxsegsize */ 505 0, /* flags */ 506 &adapter->tx_buf_tag); 507 508 return (ret); 509 } 510 511 static int 512 ena_free_tx_dma_tag(struct ena_adapter *adapter) 513 { 514 int ret; 515 516 ret = bus_dma_tag_destroy(adapter->tx_buf_tag); 517 518 if (likely(ret == 0)) 519 adapter->tx_buf_tag = NULL; 520 521 return (ret); 522 } 523 524 static int 525 ena_setup_rx_dma_tag(struct ena_adapter *adapter) 526 { 527 int ret; 528 529 /* Create DMA tag for Rx buffers*/ 530 ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent */ 531 1, 0, /* alignment, bounds */ 532 ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window */ 533 BUS_SPACE_MAXADDR, /* highaddr of excl window */ 534 NULL, NULL, /* filter, filterarg */ 535 MJUM16BYTES, /* maxsize */ 536 adapter->max_rx_sgl_size, /* nsegments */ 537 MJUM16BYTES, /* maxsegsize */ 538 0, /* flags */ 539 &adapter->rx_buf_tag); 540 541 return (ret); 542 } 543 544 static int 545 ena_free_rx_dma_tag(struct ena_adapter *adapter) 546 { 547 int ret; 548 549 ret = bus_dma_tag_destroy(adapter->rx_buf_tag); 550 551 if (likely(ret == 0)) 552 adapter->rx_buf_tag = NULL; 553 554 return (ret); 555 } 556 557 /** 558 * ena_setup_tx_resources - allocate Tx resources (Descriptors) 559 * @adapter: network interface device structure 560 * @qid: queue index 561 * 562 * Returns 0 on success, otherwise on failure. 563 **/ 564 static int 565 ena_setup_tx_resources(struct ena_adapter *adapter, int qid) 566 { 567 struct ena_que *que = &adapter->que[qid]; 568 struct ena_ring *tx_ring = que->tx_ring; 569 int size, i, err; 570 #ifdef RSS 571 cpuset_t cpu_mask; 572 #endif 573 574 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size; 575 576 tx_ring->tx_buffer_info = kmalloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 577 if (unlikely(tx_ring->tx_buffer_info == NULL)) 578 return (ENOMEM); 579 580 size = sizeof(uint16_t) * tx_ring->ring_size; 581 tx_ring->free_tx_ids = kmalloc(size, M_DEVBUF, M_NOWAIT | M_ZERO); 582 if (unlikely(tx_ring->free_tx_ids == NULL)) 583 goto err_buf_info_free; 584 585 /* Req id stack for TX OOO completions */ 586 for (i = 0; i < tx_ring->ring_size; i++) 587 tx_ring->free_tx_ids[i] = i; 588 589 #if 0 /* XXX swildner counters */ 590 /* Reset TX statistics. */ 591 ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats, 592 sizeof(tx_ring->tx_stats)); 593 #endif 594 595 tx_ring->next_to_use = 0; 596 tx_ring->next_to_clean = 0; 597 598 /* ... and create the buffer DMA maps */ 599 for (i = 0; i < tx_ring->ring_size; i++) { 600 err = bus_dmamap_create(adapter->tx_buf_tag, 0, 601 &tx_ring->tx_buffer_info[i].map); 602 if (unlikely(err != 0)) { 603 ena_trace(ENA_ALERT, 604 "Unable to create Tx DMA map for buffer %d\n", i); 605 goto err_buf_info_unmap; 606 } 607 } 608 609 return (0); 610 611 err_buf_info_unmap: 612 while (i--) { 613 bus_dmamap_destroy(adapter->tx_buf_tag, 614 tx_ring->tx_buffer_info[i].map); 615 } 616 kfree(tx_ring->free_tx_ids, M_DEVBUF); 617 tx_ring->free_tx_ids = NULL; 618 err_buf_info_free: 619 kfree(tx_ring->tx_buffer_info, M_DEVBUF); 620 tx_ring->tx_buffer_info = NULL; 621 622 return (ENOMEM); 623 } 624 625 /** 626 * ena_free_tx_resources - Free Tx Resources per Queue 627 * @adapter: network interface device structure 628 * @qid: queue index 629 * 630 * Free all transmit software resources 631 **/ 632 static void 633 ena_free_tx_resources(struct ena_adapter *adapter, int qid) 634 { 635 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 636 637 ENA_RING_MTX_LOCK(tx_ring); 638 639 /* Free buffer DMA maps, */ 640 for (int i = 0; i < tx_ring->ring_size; i++) { 641 m_freem(tx_ring->tx_buffer_info[i].mbuf); 642 tx_ring->tx_buffer_info[i].mbuf = NULL; 643 bus_dmamap_unload(adapter->tx_buf_tag, 644 tx_ring->tx_buffer_info[i].map); 645 bus_dmamap_destroy(adapter->tx_buf_tag, 646 tx_ring->tx_buffer_info[i].map); 647 } 648 ENA_RING_MTX_UNLOCK(tx_ring); 649 650 /* And free allocated memory. */ 651 kfree(tx_ring->tx_buffer_info, M_DEVBUF); 652 tx_ring->tx_buffer_info = NULL; 653 654 kfree(tx_ring->free_tx_ids, M_DEVBUF); 655 tx_ring->free_tx_ids = NULL; 656 } 657 658 /** 659 * ena_setup_all_tx_resources - allocate all queues Tx resources 660 * @adapter: network interface device structure 661 * 662 * Returns 0 on success, otherwise on failure. 663 **/ 664 static int 665 ena_setup_all_tx_resources(struct ena_adapter *adapter) 666 { 667 int i, rc; 668 669 for (i = 0; i < adapter->num_queues; i++) { 670 rc = ena_setup_tx_resources(adapter, i); 671 if (rc != 0) { 672 device_printf(adapter->pdev, 673 "Allocation for Tx Queue %u failed\n", i); 674 goto err_setup_tx; 675 } 676 } 677 678 return (0); 679 680 err_setup_tx: 681 /* Rewind the index freeing the rings as we go */ 682 while (i--) 683 ena_free_tx_resources(adapter, i); 684 return (rc); 685 } 686 687 /** 688 * ena_free_all_tx_resources - Free Tx Resources for All Queues 689 * @adapter: network interface device structure 690 * 691 * Free all transmit software resources 692 **/ 693 static void 694 ena_free_all_tx_resources(struct ena_adapter *adapter) 695 { 696 int i; 697 698 for (i = 0; i < adapter->num_queues; i++) 699 ena_free_tx_resources(adapter, i); 700 } 701 702 static inline int 703 validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id) 704 { 705 if (likely(req_id < rx_ring->ring_size)) 706 return (0); 707 708 device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n", 709 req_id); 710 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 711 #if 0 /* XXX swildner counters */ 712 counter_u64_add(rx_ring->rx_stats.bad_req_id, 1); 713 #endif 714 715 /* Trigger device reset */ 716 rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID; 717 rx_ring->adapter->trigger_reset = true; 718 719 return (EFAULT); 720 } 721 722 /** 723 * ena_setup_rx_resources - allocate Rx resources (Descriptors) 724 * @adapter: network interface device structure 725 * @qid: queue index 726 * 727 * Returns 0 on success, otherwise on failure. 728 **/ 729 static int 730 ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid) 731 { 732 struct ena_que *que = &adapter->que[qid]; 733 struct ena_ring *rx_ring = que->rx_ring; 734 int size, err, i; 735 #ifdef RSS 736 cpuset_t cpu_mask; 737 #endif 738 739 size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size; 740 741 /* 742 * Alloc extra element so in rx path 743 * we can always prefetch rx_info + 1 744 */ 745 size += sizeof(struct ena_rx_buffer); 746 747 rx_ring->rx_buffer_info = kmalloc(size, M_DEVBUF, M_WAITOK | M_ZERO); 748 749 size = sizeof(uint16_t) * rx_ring->ring_size; 750 rx_ring->free_rx_ids = kmalloc(size, M_DEVBUF, M_WAITOK); 751 752 for (i = 0; i < rx_ring->ring_size; i++) 753 rx_ring->free_rx_ids[i] = i; 754 755 #if 0 /* XXX swildner counters */ 756 /* Reset RX statistics. */ 757 ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats, 758 sizeof(rx_ring->rx_stats)); 759 #endif 760 761 rx_ring->next_to_clean = 0; 762 rx_ring->next_to_use = 0; 763 764 /* ... and create the buffer DMA maps */ 765 for (i = 0; i < rx_ring->ring_size; i++) { 766 err = bus_dmamap_create(adapter->rx_buf_tag, 0, 767 &(rx_ring->rx_buffer_info[i].map)); 768 if (err != 0) { 769 ena_trace(ENA_ALERT, 770 "Unable to create Rx DMA map for buffer %d\n", i); 771 goto err_buf_info_unmap; 772 } 773 } 774 775 #if 0 /* XXX LRO */ 776 /* Create LRO for the ring */ 777 if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) { 778 int err = tcp_lro_init(&rx_ring->lro); 779 if (err != 0) { 780 device_printf(adapter->pdev, 781 "LRO[%d] Initialization failed!\n", qid); 782 } else { 783 ena_trace(ENA_INFO, 784 "RX Soft LRO[%d] Initialized\n", qid); 785 rx_ring->lro.ifp = adapter->ifp; 786 } 787 } 788 #endif 789 790 /* Allocate taskqueues */ 791 TASK_INIT(&rx_ring->cmpl_task, 0, ena_deferred_rx_cleanup, rx_ring); 792 rx_ring->cmpl_tq = taskqueue_create("ena RX completion", M_WAITOK, 793 taskqueue_thread_enqueue, &rx_ring->cmpl_tq); 794 795 /* RSS set cpu for thread */ 796 #ifdef RSS 797 CPU_SETOF(que->cpu, &cpu_mask); 798 taskqueue_start_threads_cpuset(&rx_ring->cmpl_tq, 1, PI_NET, &cpu_mask, 799 "%s rx_ring cmpl (bucket %d)", 800 device_get_nameunit(adapter->pdev), que->cpu); 801 #else 802 taskqueue_start_threads(&rx_ring->cmpl_tq, 1, TDPRI_KERN_DAEMON, -1, 803 "%s rx_ring cmpl", device_get_nameunit(adapter->pdev)); 804 #endif 805 806 return (0); 807 808 err_buf_info_unmap: 809 while (i--) { 810 bus_dmamap_destroy(adapter->rx_buf_tag, 811 rx_ring->rx_buffer_info[i].map); 812 } 813 814 kfree(rx_ring->free_rx_ids, M_DEVBUF); 815 rx_ring->free_rx_ids = NULL; 816 kfree(rx_ring->rx_buffer_info, M_DEVBUF); 817 rx_ring->rx_buffer_info = NULL; 818 return (ENOMEM); 819 } 820 821 /** 822 * ena_free_rx_resources - Free Rx Resources 823 * @adapter: network interface device structure 824 * @qid: queue index 825 * 826 * Free all receive software resources 827 **/ 828 static void 829 ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid) 830 { 831 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 832 833 while (taskqueue_cancel(rx_ring->cmpl_tq, &rx_ring->cmpl_task, NULL) != 0) 834 taskqueue_drain(rx_ring->cmpl_tq, &rx_ring->cmpl_task); 835 836 taskqueue_free(rx_ring->cmpl_tq); 837 838 /* Free buffer DMA maps, */ 839 for (int i = 0; i < rx_ring->ring_size; i++) { 840 m_freem(rx_ring->rx_buffer_info[i].mbuf); 841 rx_ring->rx_buffer_info[i].mbuf = NULL; 842 bus_dmamap_unload(adapter->rx_buf_tag, 843 rx_ring->rx_buffer_info[i].map); 844 bus_dmamap_destroy(adapter->rx_buf_tag, 845 rx_ring->rx_buffer_info[i].map); 846 } 847 848 #if 0 /* XXX LRO */ 849 /* free LRO resources, */ 850 tcp_lro_free(&rx_ring->lro); 851 #endif 852 853 /* free allocated memory */ 854 kfree(rx_ring->rx_buffer_info, M_DEVBUF); 855 rx_ring->rx_buffer_info = NULL; 856 857 kfree(rx_ring->free_rx_ids, M_DEVBUF); 858 rx_ring->free_rx_ids = NULL; 859 } 860 861 /** 862 * ena_setup_all_rx_resources - allocate all queues Rx resources 863 * @adapter: network interface device structure 864 * 865 * Returns 0 on success, otherwise on failure. 866 **/ 867 static int 868 ena_setup_all_rx_resources(struct ena_adapter *adapter) 869 { 870 int i, rc = 0; 871 872 for (i = 0; i < adapter->num_queues; i++) { 873 rc = ena_setup_rx_resources(adapter, i); 874 if (rc != 0) { 875 device_printf(adapter->pdev, 876 "Allocation for Rx Queue %u failed\n", i); 877 goto err_setup_rx; 878 } 879 } 880 return (0); 881 882 err_setup_rx: 883 /* rewind the index freeing the rings as we go */ 884 while (i--) 885 ena_free_rx_resources(adapter, i); 886 return (rc); 887 } 888 889 /** 890 * ena_free_all_rx_resources - Free Rx resources for all queues 891 * @adapter: network interface device structure 892 * 893 * Free all receive software resources 894 **/ 895 static void 896 ena_free_all_rx_resources(struct ena_adapter *adapter) 897 { 898 int i; 899 900 for (i = 0; i < adapter->num_queues; i++) 901 ena_free_rx_resources(adapter, i); 902 } 903 904 static inline int 905 ena_alloc_rx_mbuf(struct ena_adapter *adapter, 906 struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info) 907 { 908 struct ena_com_buf *ena_buf; 909 bus_dma_segment_t segs[1]; 910 int nsegs, error; 911 int mlen; 912 913 /* if previous allocated frag is not used */ 914 if (unlikely(rx_info->mbuf != NULL)) 915 return (0); 916 917 /* Get mbuf using UMA allocator */ 918 rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 919 920 if (unlikely(rx_info->mbuf == NULL)) { 921 #if 0 /* XXX swildner counters */ 922 counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1); 923 #endif 924 rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 925 if (unlikely(rx_info->mbuf == NULL)) { 926 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 927 #if 0 /* XXX swildner counters */ 928 counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1); 929 #endif 930 return (ENOMEM); 931 } 932 mlen = MCLBYTES; 933 } else { 934 mlen = MJUMPAGESIZE; 935 } 936 /* Set mbuf length*/ 937 rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen; 938 939 /* Map packets for DMA */ 940 ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH, 941 "Using tag %p for buffers' DMA mapping, mbuf %p len: %d", 942 adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len); 943 error = bus_dmamap_load_mbuf_segment(adapter->rx_buf_tag, rx_info->map, 944 rx_info->mbuf, segs, 1, &nsegs, BUS_DMA_NOWAIT); 945 if (unlikely((error != 0) || (nsegs != 1))) { 946 ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, " 947 "nsegs: %d\n", error, nsegs); 948 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 949 #if 0 /* XXX swildner counters */ 950 counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1); 951 #endif 952 goto exit; 953 954 } 955 956 bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD); 957 958 ena_buf = &rx_info->ena_buf; 959 ena_buf->paddr = segs[0].ds_addr; 960 ena_buf->len = mlen; 961 962 ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH, 963 "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n", 964 rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr); 965 966 return (0); 967 968 exit: 969 m_freem(rx_info->mbuf); 970 rx_info->mbuf = NULL; 971 return (EFAULT); 972 } 973 974 static void 975 ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring, 976 struct ena_rx_buffer *rx_info) 977 { 978 979 if (rx_info->mbuf == NULL) { 980 ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n"); 981 return; 982 } 983 984 bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map); 985 m_freem(rx_info->mbuf); 986 rx_info->mbuf = NULL; 987 } 988 989 /** 990 * ena_refill_rx_bufs - Refills ring with descriptors 991 * @rx_ring: the ring which we want to feed with free descriptors 992 * @num: number of descriptors to refill 993 * Refills the ring with newly allocated DMA-mapped mbufs for receiving 994 **/ 995 static int 996 ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num) 997 { 998 struct ena_adapter *adapter = rx_ring->adapter; 999 uint16_t next_to_use, req_id; 1000 uint32_t i; 1001 int rc; 1002 1003 ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d", 1004 rx_ring->qid); 1005 1006 next_to_use = rx_ring->next_to_use; 1007 1008 for (i = 0; i < num; i++) { 1009 struct ena_rx_buffer *rx_info; 1010 1011 ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, 1012 "RX buffer - next to use: %d", next_to_use); 1013 1014 req_id = rx_ring->free_rx_ids[next_to_use]; 1015 rc = validate_rx_req_id(rx_ring, req_id); 1016 if (unlikely(rc != 0)) 1017 break; 1018 1019 rx_info = &rx_ring->rx_buffer_info[req_id]; 1020 1021 rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info); 1022 if (unlikely(rc != 0)) { 1023 ena_trace(ENA_WARNING, 1024 "failed to alloc buffer for rx queue %d\n", 1025 rx_ring->qid); 1026 break; 1027 } 1028 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq, 1029 &rx_info->ena_buf, req_id); 1030 if (unlikely(rc != 0)) { 1031 ena_trace(ENA_WARNING, 1032 "failed to add buffer for rx queue %d\n", 1033 rx_ring->qid); 1034 break; 1035 } 1036 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use, 1037 rx_ring->ring_size); 1038 } 1039 1040 if (unlikely(i < num)) { 1041 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 1042 #if 0 /* XXX swildner counters */ 1043 counter_u64_add(rx_ring->rx_stats.refil_partial, 1); 1044 #endif 1045 ena_trace(ENA_WARNING, 1046 "refilled rx qid %d with only %d mbufs (from %d)\n", 1047 rx_ring->qid, i, num); 1048 } 1049 1050 if (likely(i != 0)) { 1051 wmb(); 1052 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq); 1053 } 1054 rx_ring->next_to_use = next_to_use; 1055 return (i); 1056 } 1057 1058 static void 1059 ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid) 1060 { 1061 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 1062 unsigned int i; 1063 1064 for (i = 0; i < rx_ring->ring_size; i++) { 1065 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i]; 1066 1067 if (rx_info->mbuf != NULL) 1068 ena_free_rx_mbuf(adapter, rx_ring, rx_info); 1069 } 1070 } 1071 1072 /** 1073 * ena_refill_all_rx_bufs - allocate all queues Rx buffers 1074 * @adapter: network interface device structure 1075 * 1076 */ 1077 static void 1078 ena_refill_all_rx_bufs(struct ena_adapter *adapter) 1079 { 1080 struct ena_ring *rx_ring; 1081 int i, rc, bufs_num; 1082 1083 for (i = 0; i < adapter->num_queues; i++) { 1084 rx_ring = &adapter->rx_ring[i]; 1085 bufs_num = rx_ring->ring_size - 1; 1086 rc = ena_refill_rx_bufs(rx_ring, bufs_num); 1087 1088 if (unlikely(rc != bufs_num)) 1089 ena_trace(ENA_WARNING, "refilling Queue %d failed. " 1090 "Allocated %d buffers from: %d\n", i, rc, bufs_num); 1091 } 1092 } 1093 1094 static void 1095 ena_free_all_rx_bufs(struct ena_adapter *adapter) 1096 { 1097 int i; 1098 1099 for (i = 0; i < adapter->num_queues; i++) 1100 ena_free_rx_bufs(adapter, i); 1101 } 1102 1103 /** 1104 * ena_free_tx_bufs - Free Tx Buffers per Queue 1105 * @adapter: network interface device structure 1106 * @qid: queue index 1107 **/ 1108 static void 1109 ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid) 1110 { 1111 bool print_once = true; 1112 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 1113 1114 ENA_RING_MTX_LOCK(tx_ring); 1115 for (int i = 0; i < tx_ring->ring_size; i++) { 1116 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i]; 1117 1118 if (tx_info->mbuf == NULL) 1119 continue; 1120 1121 if (print_once) { 1122 device_printf(adapter->pdev, 1123 "free uncompleted tx mbuf qid %d idx 0x%x", 1124 qid, i); 1125 print_once = false; 1126 } else { 1127 ena_trace(ENA_DBG, 1128 "free uncompleted tx mbuf qid %d idx 0x%x", 1129 qid, i); 1130 } 1131 1132 bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map); 1133 m_free(tx_info->mbuf); 1134 tx_info->mbuf = NULL; 1135 } 1136 ENA_RING_MTX_UNLOCK(tx_ring); 1137 } 1138 1139 static void 1140 ena_free_all_tx_bufs(struct ena_adapter *adapter) 1141 { 1142 1143 for (int i = 0; i < adapter->num_queues; i++) 1144 ena_free_tx_bufs(adapter, i); 1145 } 1146 1147 static void 1148 ena_destroy_all_tx_queues(struct ena_adapter *adapter) 1149 { 1150 uint16_t ena_qid; 1151 int i; 1152 1153 for (i = 0; i < adapter->num_queues; i++) { 1154 ena_qid = ENA_IO_TXQ_IDX(i); 1155 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 1156 } 1157 } 1158 1159 static void 1160 ena_destroy_all_rx_queues(struct ena_adapter *adapter) 1161 { 1162 uint16_t ena_qid; 1163 int i; 1164 1165 for (i = 0; i < adapter->num_queues; i++) { 1166 ena_qid = ENA_IO_RXQ_IDX(i); 1167 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 1168 } 1169 } 1170 1171 static void 1172 ena_destroy_all_io_queues(struct ena_adapter *adapter) 1173 { 1174 ena_destroy_all_tx_queues(adapter); 1175 ena_destroy_all_rx_queues(adapter); 1176 } 1177 1178 static inline int 1179 validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id) 1180 { 1181 struct ena_adapter *adapter = tx_ring->adapter; 1182 struct ena_tx_buffer *tx_info = NULL; 1183 1184 if (likely(req_id < tx_ring->ring_size)) { 1185 tx_info = &tx_ring->tx_buffer_info[req_id]; 1186 if (tx_info->mbuf != NULL) 1187 return (0); 1188 } 1189 1190 if (tx_info->mbuf == NULL) 1191 device_printf(adapter->pdev, 1192 "tx_info doesn't have valid mbuf\n"); 1193 else 1194 device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id); 1195 1196 IFNET_STAT_INC(tx_ring->adapter->ifp, oerrors, 1); 1197 #if 0 /* XXX swildner counters */ 1198 counter_u64_add(tx_ring->tx_stats.bad_req_id, 1); 1199 #endif 1200 1201 return (EFAULT); 1202 } 1203 1204 static int 1205 ena_create_io_queues(struct ena_adapter *adapter) 1206 { 1207 struct ena_com_dev *ena_dev = adapter->ena_dev; 1208 struct ena_com_create_io_ctx ctx; 1209 struct ena_ring *ring; 1210 uint16_t ena_qid; 1211 uint32_t msix_vector; 1212 int rc, i; 1213 1214 /* Create TX queues */ 1215 for (i = 0; i < adapter->num_queues; i++) { 1216 msix_vector = ENA_IO_IRQ_IDX(i); 1217 ena_qid = ENA_IO_TXQ_IDX(i); 1218 ctx.mem_queue_type = ena_dev->tx_mem_queue_type; 1219 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX; 1220 ctx.queue_size = adapter->tx_ring_size; 1221 ctx.msix_vector = msix_vector; 1222 ctx.qid = ena_qid; 1223 rc = ena_com_create_io_queue(ena_dev, &ctx); 1224 if (rc != 0) { 1225 device_printf(adapter->pdev, 1226 "Failed to create io TX queue #%d rc: %d\n", i, rc); 1227 goto err_tx; 1228 } 1229 ring = &adapter->tx_ring[i]; 1230 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1231 &ring->ena_com_io_sq, 1232 &ring->ena_com_io_cq); 1233 if (rc != 0) { 1234 device_printf(adapter->pdev, 1235 "Failed to get TX queue handlers. TX queue num" 1236 " %d rc: %d\n", i, rc); 1237 ena_com_destroy_io_queue(ena_dev, ena_qid); 1238 goto err_tx; 1239 } 1240 } 1241 1242 /* Create RX queues */ 1243 for (i = 0; i < adapter->num_queues; i++) { 1244 msix_vector = ENA_IO_IRQ_IDX(i); 1245 ena_qid = ENA_IO_RXQ_IDX(i); 1246 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 1247 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX; 1248 ctx.queue_size = adapter->rx_ring_size; 1249 ctx.msix_vector = msix_vector; 1250 ctx.qid = ena_qid; 1251 rc = ena_com_create_io_queue(ena_dev, &ctx); 1252 if (unlikely(rc != 0)) { 1253 device_printf(adapter->pdev, 1254 "Failed to create io RX queue[%d] rc: %d\n", i, rc); 1255 goto err_rx; 1256 } 1257 1258 ring = &adapter->rx_ring[i]; 1259 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1260 &ring->ena_com_io_sq, 1261 &ring->ena_com_io_cq); 1262 if (unlikely(rc != 0)) { 1263 device_printf(adapter->pdev, 1264 "Failed to get RX queue handlers. RX queue num" 1265 " %d rc: %d\n", i, rc); 1266 ena_com_destroy_io_queue(ena_dev, ena_qid); 1267 goto err_rx; 1268 } 1269 } 1270 1271 return (0); 1272 1273 err_rx: 1274 while (i--) 1275 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i)); 1276 i = adapter->num_queues; 1277 err_tx: 1278 while (i--) 1279 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i)); 1280 1281 return (ENXIO); 1282 } 1283 1284 /** 1285 * ena_tx_cleanup - clear sent packets and corresponding descriptors 1286 * @tx_ring: ring for which we want to clean packets 1287 * 1288 * Once packets are sent, we ask the device in a loop for no longer used 1289 * descriptors. We find the related mbuf chain in a map (index in an array) 1290 * and free it, then update ring state. 1291 * This is performed in "endless" loop, updating ring pointers every 1292 * TX_COMMIT. The first check of free descriptor is performed before the actual 1293 * loop, then repeated at the loop end. 1294 **/ 1295 static int 1296 ena_tx_cleanup(struct ena_ring *tx_ring) 1297 { 1298 struct ena_adapter *adapter; 1299 struct ena_com_io_cq* io_cq; 1300 uint16_t next_to_clean; 1301 uint16_t req_id; 1302 uint16_t ena_qid; 1303 unsigned int total_done = 0; 1304 int rc; 1305 int commit = TX_COMMIT; 1306 int budget = TX_BUDGET; 1307 int work_done; 1308 1309 adapter = tx_ring->que->adapter; 1310 ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id); 1311 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid]; 1312 next_to_clean = tx_ring->next_to_clean; 1313 1314 do { 1315 struct ena_tx_buffer *tx_info; 1316 struct mbuf *mbuf; 1317 1318 rc = ena_com_tx_comp_req_id_get(io_cq, &req_id); 1319 if (unlikely(rc != 0)) 1320 break; 1321 1322 rc = validate_tx_req_id(tx_ring, req_id); 1323 if (unlikely(rc != 0)) 1324 break; 1325 1326 tx_info = &tx_ring->tx_buffer_info[req_id]; 1327 1328 mbuf = tx_info->mbuf; 1329 1330 tx_info->mbuf = NULL; 1331 timevalclear(&tx_info->timestamp); 1332 1333 if (likely(tx_info->num_of_bufs != 0)) { 1334 /* Map is no longer required */ 1335 bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map); 1336 } 1337 1338 ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d mbuf %p completed", 1339 tx_ring->qid, mbuf); 1340 1341 m_freem(mbuf); 1342 1343 total_done += tx_info->tx_descs; 1344 1345 tx_ring->free_tx_ids[next_to_clean] = req_id; 1346 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean, 1347 tx_ring->ring_size); 1348 1349 if (unlikely(--commit == 0)) { 1350 commit = TX_COMMIT; 1351 /* update ring state every TX_COMMIT descriptor */ 1352 tx_ring->next_to_clean = next_to_clean; 1353 ena_com_comp_ack( 1354 &adapter->ena_dev->io_sq_queues[ena_qid], 1355 total_done); 1356 ena_com_update_dev_comp_head(io_cq); 1357 total_done = 0; 1358 } 1359 } while (likely(--budget)); 1360 1361 work_done = TX_BUDGET - budget; 1362 1363 ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d done. total pkts: %d", 1364 tx_ring->qid, work_done); 1365 1366 /* If there is still something to commit update ring state */ 1367 if (likely(commit != TX_COMMIT)) { 1368 tx_ring->next_to_clean = next_to_clean; 1369 ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid], 1370 total_done); 1371 ena_com_update_dev_comp_head(io_cq); 1372 } 1373 1374 return (work_done); 1375 } 1376 1377 static void 1378 ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx, 1379 struct mbuf *mbuf) 1380 { 1381 struct ena_adapter *adapter = rx_ring->adapter; 1382 1383 if (likely(adapter->rss_support)) { 1384 //mbuf->m_pkthdr.flowid = ena_rx_ctx->hash; 1385 m_sethash(mbuf, ena_rx_ctx->hash); 1386 1387 #if 0 /* XXX rsstype doesn't seem to be needed by the network stack, we will only supply the hash. */ 1388 if (ena_rx_ctx->frag && 1389 (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) { 1390 M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH); 1391 return; 1392 } 1393 1394 switch (ena_rx_ctx->l3_proto) { 1395 case ENA_ETH_IO_L3_PROTO_IPV4: 1396 switch (ena_rx_ctx->l4_proto) { 1397 case ENA_ETH_IO_L4_PROTO_TCP: 1398 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4); 1399 break; 1400 case ENA_ETH_IO_L4_PROTO_UDP: 1401 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4); 1402 break; 1403 default: 1404 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4); 1405 } 1406 break; 1407 case ENA_ETH_IO_L3_PROTO_IPV6: 1408 switch (ena_rx_ctx->l4_proto) { 1409 case ENA_ETH_IO_L4_PROTO_TCP: 1410 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6); 1411 break; 1412 case ENA_ETH_IO_L4_PROTO_UDP: 1413 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6); 1414 break; 1415 default: 1416 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6); 1417 } 1418 break; 1419 case ENA_ETH_IO_L3_PROTO_UNKNOWN: 1420 M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE); 1421 break; 1422 default: 1423 M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH); 1424 } 1425 #endif 1426 } else { 1427 //mbuf->m_pkthdr.flowid = rx_ring->qid; 1428 //M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE); 1429 m_sethash(mbuf, rx_ring->qid); 1430 } 1431 } 1432 1433 /** 1434 * ena_rx_mbuf - assemble mbuf from descriptors 1435 * @rx_ring: ring for which we want to clean packets 1436 * @ena_bufs: buffer info 1437 * @ena_rx_ctx: metadata for this packet(s) 1438 * @next_to_clean: ring pointer, will be updated only upon success 1439 * 1440 **/ 1441 static struct mbuf* 1442 ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs, 1443 struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean) 1444 { 1445 struct mbuf *mbuf; 1446 struct ena_rx_buffer *rx_info; 1447 struct ena_adapter *adapter; 1448 unsigned int descs = ena_rx_ctx->descs; 1449 uint16_t ntc, len, req_id, buf = 0; 1450 1451 ntc = *next_to_clean; 1452 adapter = rx_ring->adapter; 1453 rx_info = &rx_ring->rx_buffer_info[ntc]; 1454 1455 if (unlikely(rx_info->mbuf == NULL)) { 1456 device_printf(adapter->pdev, "NULL mbuf in rx_info"); 1457 return (NULL); 1458 } 1459 1460 len = ena_bufs[buf].len; 1461 req_id = ena_bufs[buf].req_id; 1462 rx_info = &rx_ring->rx_buffer_info[req_id]; 1463 1464 ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx", 1465 rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr); 1466 1467 mbuf = rx_info->mbuf; 1468 mbuf->m_flags |= M_PKTHDR; 1469 mbuf->m_pkthdr.len = len; 1470 mbuf->m_len = len; 1471 mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp; 1472 1473 /* Fill mbuf with hash key and it's interpretation for optimization */ 1474 ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf); 1475 1476 ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d", 1477 mbuf, mbuf->m_flags, mbuf->m_pkthdr.len); 1478 1479 /* DMA address is not needed anymore, unmap it */ 1480 bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map); 1481 1482 rx_info->mbuf = NULL; 1483 rx_ring->free_rx_ids[ntc] = req_id; 1484 ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size); 1485 1486 /* 1487 * While we have more than 1 descriptors for one rcvd packet, append 1488 * other mbufs to the main one 1489 */ 1490 while (--descs) { 1491 ++buf; 1492 len = ena_bufs[buf].len; 1493 req_id = ena_bufs[buf].req_id; 1494 rx_info = &rx_ring->rx_buffer_info[req_id]; 1495 1496 if (unlikely(rx_info->mbuf == NULL)) { 1497 device_printf(adapter->pdev, "NULL mbuf in rx_info"); 1498 /* 1499 * If one of the required mbufs was not allocated yet, 1500 * we can break there. 1501 * All earlier used descriptors will be reallocated 1502 * later and not used mbufs can be reused. 1503 * The next_to_clean pointer will not be updated in case 1504 * of an error, so caller should advance it manually 1505 * in error handling routine to keep it up to date 1506 * with hw ring. 1507 */ 1508 m_freem(mbuf); 1509 return (NULL); 1510 } 1511 1512 if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) { 1513 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 1514 #if 0 /* XXX swildner counters */ 1515 counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1); 1516 #endif 1517 ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p", 1518 mbuf); 1519 } 1520 1521 ena_trace(ENA_DBG | ENA_RXPTH, 1522 "rx mbuf updated. len %d", mbuf->m_pkthdr.len); 1523 1524 /* Free already appended mbuf, it won't be useful anymore */ 1525 bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map); 1526 m_freem(rx_info->mbuf); 1527 rx_info->mbuf = NULL; 1528 1529 rx_ring->free_rx_ids[ntc] = req_id; 1530 ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size); 1531 } 1532 1533 *next_to_clean = ntc; 1534 1535 return (mbuf); 1536 } 1537 1538 /** 1539 * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum 1540 **/ 1541 static inline void 1542 ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx, 1543 struct mbuf *mbuf) 1544 { 1545 1546 /* if IP and error */ 1547 if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) && 1548 ena_rx_ctx->l3_csum_err)) { 1549 /* ipv4 checksum error */ 1550 mbuf->m_pkthdr.csum_flags = 0; 1551 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 1552 #if 0 /* XXX swildner counters */ 1553 counter_u64_add(rx_ring->rx_stats.bad_csum, 1); 1554 #endif 1555 ena_trace(ENA_DBG, "RX IPv4 header checksum error"); 1556 return; 1557 } 1558 1559 /* if TCP/UDP */ 1560 if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) || 1561 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) { 1562 if (ena_rx_ctx->l4_csum_err) { 1563 /* TCP/UDP checksum error */ 1564 mbuf->m_pkthdr.csum_flags = 0; 1565 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 1566 #if 0 /* XXX swildner counters */ 1567 counter_u64_add(rx_ring->rx_stats.bad_csum, 1); 1568 #endif 1569 ena_trace(ENA_DBG, "RX L4 checksum error"); 1570 } else { 1571 mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED; 1572 mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1573 } 1574 } 1575 } 1576 1577 static void 1578 ena_deferred_rx_cleanup(void *arg, int pending) 1579 { 1580 struct ena_ring *rx_ring = arg; 1581 int budget = CLEAN_BUDGET; 1582 1583 ENA_RING_MTX_LOCK(rx_ring); 1584 /* 1585 * If deferred task was executed, perform cleanup of all awaiting 1586 * descs (or until given budget is depleted to avoid infinite loop). 1587 */ 1588 while (likely(budget--)) { 1589 if (ena_rx_cleanup(rx_ring) == 0) 1590 break; 1591 } 1592 ENA_RING_MTX_UNLOCK(rx_ring); 1593 } 1594 1595 /** 1596 * ena_rx_cleanup - handle rx irq 1597 * @arg: ring for which irq is being handled 1598 **/ 1599 static int 1600 ena_rx_cleanup(struct ena_ring *rx_ring) 1601 { 1602 struct ena_adapter *adapter; 1603 struct mbuf *mbuf; 1604 struct ena_com_rx_ctx ena_rx_ctx; 1605 struct ena_com_io_cq* io_cq; 1606 struct ena_com_io_sq* io_sq; 1607 if_t ifp; 1608 uint16_t ena_qid; 1609 uint16_t next_to_clean; 1610 uint32_t refill_required; 1611 uint32_t refill_threshold; 1612 uint32_t do_if_input = 0; 1613 unsigned int qid; 1614 int rc, i; 1615 int budget = RX_BUDGET; 1616 1617 adapter = rx_ring->que->adapter; 1618 ifp = adapter->ifp; 1619 qid = rx_ring->que->id; 1620 ena_qid = ENA_IO_RXQ_IDX(qid); 1621 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid]; 1622 io_sq = &adapter->ena_dev->io_sq_queues[ena_qid]; 1623 next_to_clean = rx_ring->next_to_clean; 1624 1625 ena_trace(ENA_DBG, "rx: qid %d", qid); 1626 1627 do { 1628 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs; 1629 ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size; 1630 ena_rx_ctx.descs = 0; 1631 rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx); 1632 1633 if (unlikely(rc != 0)) 1634 goto error; 1635 1636 if (unlikely(ena_rx_ctx.descs == 0)) 1637 break; 1638 1639 ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. " 1640 "descs #: %d l3 proto %d l4 proto %d hash: %x", 1641 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto, 1642 ena_rx_ctx.l4_proto, ena_rx_ctx.hash); 1643 1644 /* Receive mbuf from the ring */ 1645 mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs, 1646 &ena_rx_ctx, &next_to_clean); 1647 1648 /* Exit if we failed to retrieve a buffer */ 1649 if (unlikely(mbuf == NULL)) { 1650 for (i = 0; i < ena_rx_ctx.descs; ++i) { 1651 rx_ring->free_rx_ids[next_to_clean] = 1652 rx_ring->ena_bufs[i].req_id; 1653 next_to_clean = 1654 ENA_RX_RING_IDX_NEXT(next_to_clean, 1655 rx_ring->ring_size); 1656 1657 } 1658 break; 1659 } 1660 1661 /* 1662 * XXX Removed IFCAP_RXCSUM_IPV6 check because DragonFly 1663 * does not seem to support it 1664 */ 1665 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) { 1666 ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf); 1667 } 1668 1669 #if 0 /* XXX swildner counters */ 1670 counter_enter(); 1671 counter_u64_add_protected(rx_ring->rx_stats.bytes, 1672 mbuf->m_pkthdr.len); 1673 counter_u64_add_protected(adapter->hw_stats.rx_bytes, 1674 mbuf->m_pkthdr.len); 1675 counter_exit(); 1676 #endif 1677 /* 1678 * LRO is only for IP/TCP packets and TCP checksum of the packet 1679 * should be computed by hardware. 1680 */ 1681 do_if_input = 1; 1682 #if 0 /* XXX LRO */ 1683 if (((ifp->if_capenable & IFCAP_LRO) != 0) && 1684 ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) && 1685 (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) { 1686 /* 1687 * Send to the stack if: 1688 * - LRO not enabled, or 1689 * - no LRO resources, or 1690 * - lro enqueue fails 1691 */ 1692 if ((rx_ring->lro.lro_cnt != 0) && 1693 (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0)) 1694 do_if_input = 0; 1695 } 1696 #endif 1697 if (do_if_input != 0) { 1698 ena_trace(ENA_DBG | ENA_RXPTH, 1699 "calling if_input() with mbuf %p", mbuf); 1700 ENA_RING_MTX_UNLOCK(rx_ring); 1701 (*ifp->if_input)(ifp, mbuf, NULL, -1); 1702 ENA_RING_MTX_LOCK(rx_ring); 1703 } 1704 1705 IFNET_STAT_INC(ifp, ipackets, 1); 1706 #if 0 /* XXX swildner counters */ 1707 counter_enter(); 1708 counter_u64_add_protected(rx_ring->rx_stats.cnt, 1); 1709 counter_u64_add_protected(adapter->hw_stats.rx_packets, 1); 1710 counter_exit(); 1711 #endif 1712 } while (--budget); 1713 1714 rx_ring->next_to_clean = next_to_clean; 1715 1716 refill_required = ena_com_free_desc(io_sq); 1717 refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER; 1718 1719 if (refill_required > refill_threshold) { 1720 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq); 1721 ena_refill_rx_bufs(rx_ring, refill_required); 1722 } 1723 1724 #if 0 /* XXX LRO */ 1725 tcp_lro_flush_all(&rx_ring->lro); 1726 #endif 1727 1728 return (RX_BUDGET - budget); 1729 1730 error: 1731 IFNET_STAT_INC(rx_ring->adapter->ifp, ierrors, 1); 1732 #if 0 /* XXX swildner counters */ 1733 counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1); 1734 #endif 1735 return (RX_BUDGET - budget); 1736 } 1737 1738 /********************************************************************* 1739 * 1740 * MSIX & Interrupt Service routine 1741 * 1742 **********************************************************************/ 1743 1744 /** 1745 * ena_handle_msix - MSIX Interrupt Handler for admin/async queue 1746 * @arg: interrupt number 1747 **/ 1748 static void 1749 ena_intr_msix_mgmnt(void *arg) 1750 { 1751 struct ena_adapter *adapter = (struct ena_adapter *)arg; 1752 1753 ena_com_admin_q_comp_intr_handler(adapter->ena_dev); 1754 if (likely(adapter->running)) 1755 ena_com_aenq_intr_handler(adapter->ena_dev, arg); 1756 } 1757 1758 /** 1759 * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx 1760 * @arg: interrupt number 1761 **/ 1762 static void 1763 ena_handle_msix(void *arg) 1764 { 1765 struct ena_que *que = arg; 1766 struct ena_adapter *adapter = que->adapter; 1767 if_t ifp = adapter->ifp; 1768 struct ena_ring *tx_ring; 1769 struct ena_ring *rx_ring; 1770 struct ena_com_io_cq* io_cq; 1771 struct ena_eth_io_intr_reg intr_reg; 1772 int qid, ena_qid; 1773 int txc, rxc, i; 1774 1775 if (unlikely((ifp->if_flags & IFF_RUNNING) == 0)) 1776 return; 1777 1778 ena_trace(ENA_DBG, "MSI-X TX/RX routine"); 1779 1780 tx_ring = que->tx_ring; 1781 rx_ring = que->rx_ring; 1782 qid = que->id; 1783 ena_qid = ENA_IO_TXQ_IDX(qid); 1784 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid]; 1785 1786 for (i = 0; i < CLEAN_BUDGET; ++i) { 1787 /* 1788 * If lock cannot be acquired, then deferred cleanup task was 1789 * being executed and rx ring is being cleaned up in 1790 * another thread. 1791 */ 1792 if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) { 1793 rxc = ena_rx_cleanup(rx_ring); 1794 ENA_RING_MTX_UNLOCK(rx_ring); 1795 } else { 1796 rxc = 0; 1797 } 1798 1799 /* Protection from calling ena_tx_cleanup from ena_start_xmit */ 1800 ENA_RING_MTX_LOCK(tx_ring); 1801 txc = ena_tx_cleanup(tx_ring); 1802 ENA_RING_MTX_UNLOCK(tx_ring); 1803 1804 if (unlikely((ifp->if_flags & IFF_RUNNING) == 0)) 1805 return; 1806 1807 if ((txc != TX_BUDGET) && (rxc != RX_BUDGET)) 1808 break; 1809 } 1810 1811 /* Signal that work is done and unmask interrupt */ 1812 ena_com_update_intr_reg(&intr_reg, 1813 RX_IRQ_INTERVAL, 1814 TX_IRQ_INTERVAL, 1815 true); 1816 ena_com_unmask_intr(io_cq, &intr_reg); 1817 } 1818 1819 static int 1820 ena_enable_msix(struct ena_adapter *adapter) 1821 { 1822 device_t dev = adapter->pdev; 1823 int msix_vecs; 1824 int error, i, rc = 0; 1825 1826 /* Reserved the max msix vectors we might need */ 1827 msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues); 1828 1829 adapter->msix_entries = kmalloc(msix_vecs * sizeof(struct msix_entry), 1830 M_DEVBUF, M_WAITOK | M_ZERO); 1831 1832 ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d", msix_vecs); 1833 1834 for (i = 0; i < msix_vecs; i++) { 1835 adapter->msix_entries[i].entry = i; 1836 /* Vectors must start from 1 */ 1837 adapter->msix_entries[i].vector = i + 1; 1838 } 1839 1840 error = pci_setup_msix(dev); 1841 if (error) { 1842 device_printf(dev, "pci_setup_msix() failed\n"); 1843 goto err_msix_free; 1844 } 1845 1846 adapter->msix_vecs = msix_vecs; 1847 adapter->msix_enabled = true; 1848 1849 return (0); 1850 1851 err_msix_free: 1852 kfree(adapter->msix_entries, M_DEVBUF); 1853 adapter->msix_entries = NULL; 1854 1855 return (rc); 1856 } 1857 1858 static void 1859 ena_setup_mgmnt_intr(struct ena_adapter *adapter) 1860 { 1861 1862 ksnprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name, 1863 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s", 1864 device_get_nameunit(adapter->pdev)); 1865 /* 1866 * Handler is NULL on purpose, it will be set 1867 * when mgmnt interrupt is acquired 1868 */ 1869 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL; 1870 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter; 1871 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector = 1872 adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector; 1873 } 1874 1875 static void 1876 ena_setup_io_intr(struct ena_adapter *adapter) 1877 { 1878 static int last_bind_cpu = -1; 1879 int irq_idx; 1880 1881 for (int i = 0; i < adapter->num_queues; i++) { 1882 irq_idx = ENA_IO_IRQ_IDX(i); 1883 1884 ksnprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE, 1885 "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i); 1886 adapter->irq_tbl[irq_idx].handler = ena_handle_msix; 1887 adapter->irq_tbl[irq_idx].data = &adapter->que[i]; 1888 adapter->irq_tbl[irq_idx].vector = 1889 adapter->msix_entries[irq_idx].vector; 1890 ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n", 1891 adapter->msix_entries[irq_idx].vector); 1892 #ifdef RSS 1893 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu = 1894 rss_getcpu(i % rss_getnumbuckets()); 1895 #else 1896 /* 1897 * We still want to bind rings to the corresponding cpu 1898 * using something similar to the RSS round-robin technique. 1899 * 1900 * XXX It seems that this can be removed since DragonFly has 1901 * native support for RSS. DragonFly also does not have 1902 * support for CPU_FIRST or CPU_NEXT. 1903 */ 1904 1905 if (last_bind_cpu < 0) 1906 last_bind_cpu = (last_bind_cpu + 1) % ncpus; 1907 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu = 1908 last_bind_cpu; 1909 last_bind_cpu = (last_bind_cpu + 1) % ncpus; 1910 #endif 1911 } 1912 } 1913 1914 static int 1915 ena_request_mgmnt_irq(struct ena_adapter *adapter) 1916 { 1917 struct ena_irq *irq; 1918 unsigned long flags; 1919 int error, rc, rcc; 1920 1921 flags = RF_ACTIVE | RF_SHAREABLE; 1922 1923 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 1924 1925 error = pci_alloc_msix_vector(adapter->pdev, 0, &irq->vector, 0); 1926 if (error) { 1927 device_printf(adapter->pdev, "Could not initialize MGMNT MSI-X Vector on cpu0\n"); 1928 return (ENXIO); 1929 } 1930 1931 irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ, 1932 &irq->vector, flags); 1933 1934 if (unlikely(irq->res == NULL)) { 1935 device_printf(adapter->pdev, "could not allocate " 1936 "irq vector: %d\n", irq->vector); 1937 pci_release_msix_vector(adapter->pdev, irq->vector); 1938 return (ENXIO); 1939 } 1940 1941 rc = bus_activate_resource(adapter->pdev, SYS_RES_IRQ, 1942 irq->vector, irq->res); 1943 if (unlikely(rc != 0)) { 1944 device_printf(adapter->pdev, "could not activate " 1945 "irq vector: %d\n", irq->vector); 1946 goto err_res_free; 1947 } 1948 1949 rc = bus_setup_intr(adapter->pdev, irq->res, 1950 INTR_MPSAFE, ena_intr_msix_mgmnt, 1951 irq->data, &irq->cookie, NULL); 1952 if (unlikely(rc != 0)) { 1953 device_printf(adapter->pdev, "failed to register " 1954 "interrupt handler for irq %ju: %d\n", 1955 rman_get_start(irq->res), rc); 1956 goto err_res_free; 1957 } 1958 irq->requested = true; 1959 1960 return (rc); 1961 1962 err_res_free: 1963 ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n", 1964 irq->vector); 1965 rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 1966 irq->vector, irq->res); 1967 pci_release_msix_vector(adapter->pdev, irq->vector); 1968 if (unlikely(rcc != 0)) 1969 device_printf(adapter->pdev, "dev has no parent while " 1970 "releasing res for irq: %d\n", irq->vector); 1971 irq->res = NULL; 1972 1973 return (rc); 1974 } 1975 1976 static int 1977 ena_request_io_irq(struct ena_adapter *adapter) 1978 { 1979 struct ena_irq *irq; 1980 unsigned long flags = 0; 1981 int rc = 0, i, rcc, error; 1982 1983 if (unlikely(adapter->msix_enabled == 0)) { 1984 device_printf(adapter->pdev, 1985 "failed to request I/O IRQ: MSI-X is not enabled\n"); 1986 return (EINVAL); 1987 } else { 1988 flags = RF_ACTIVE | RF_SHAREABLE; 1989 } 1990 1991 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) { 1992 irq = &adapter->irq_tbl[i]; 1993 1994 if (unlikely(irq->requested)) 1995 continue; 1996 1997 error = pci_alloc_msix_vector(adapter->pdev, i, &irq->vector, irq->cpu); 1998 if (error) { 1999 device_printf(adapter->pdev, "Unable to allocated MSI-X %d on cpu%d\n", i, irq->cpu); 2000 goto err; 2001 } 2002 2003 irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ, 2004 &irq->vector, flags); 2005 if (unlikely(irq->res == NULL)) { 2006 device_printf(adapter->pdev, "could not allocate " 2007 "irq vector: %d\n", irq->vector); 2008 goto err; 2009 } 2010 2011 2012 /* 2013 * TODO: Might need to setup desc and use irq->name as the 2014 * value 2015 */ 2016 rc = bus_setup_intr(adapter->pdev, irq->res, 2017 INTR_MPSAFE, 2018 irq->handler, irq->data, &irq->cookie, NULL); 2019 if (unlikely(rc != 0)) { 2020 device_printf(adapter->pdev, "failed to register " 2021 "interrupt handler for irq %ju: %d\n", 2022 rman_get_start(irq->res), rc); 2023 goto err; 2024 } 2025 irq->requested = true; 2026 2027 #ifdef RSS 2028 ena_trace(ENA_INFO, "queue %d - RSS bucket %d\n", 2029 i - ENA_IO_IRQ_FIRST_IDX, irq->cpu); 2030 #else 2031 ena_trace(ENA_INFO, "queue %d - cpu %d\n", 2032 i - ENA_IO_IRQ_FIRST_IDX, irq->cpu); 2033 #endif 2034 } 2035 2036 return (rc); 2037 2038 err: 2039 2040 for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) { 2041 irq = &adapter->irq_tbl[i]; 2042 rcc = 0; 2043 2044 /* Once we entered err: section and irq->requested is true we 2045 free both intr and resources */ 2046 if (irq->requested) 2047 rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie); 2048 if (unlikely(rcc != 0)) 2049 device_printf(adapter->pdev, "could not release" 2050 " irq: %d, error: %d\n", irq->vector, rcc); 2051 2052 /* If we entred err: section without irq->requested set we know 2053 it was bus_alloc_resource_any() that needs cleanup, provided 2054 res is not NULL. In case res is NULL no work in needed in 2055 this iteration */ 2056 rcc = 0; 2057 if (irq->res != NULL) { 2058 rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 2059 irq->vector, irq->res); 2060 pci_release_msix_vector(adapter->pdev, irq->vector); 2061 } 2062 if (unlikely(rcc != 0)) 2063 device_printf(adapter->pdev, "dev has no parent while " 2064 "releasing res for irq: %d\n", irq->vector); 2065 irq->requested = false; 2066 irq->res = NULL; 2067 } 2068 2069 return (rc); 2070 } 2071 2072 static void 2073 ena_free_mgmnt_irq(struct ena_adapter *adapter) 2074 { 2075 struct ena_irq *irq; 2076 int rc; 2077 2078 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 2079 if (irq->requested) { 2080 ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n", 2081 irq->vector); 2082 rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie); 2083 if (unlikely(rc != 0)) 2084 device_printf(adapter->pdev, "failed to tear " 2085 "down irq: %d\n", irq->vector); 2086 irq->requested = 0; 2087 } 2088 2089 if (irq->res != NULL) { 2090 ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n", 2091 irq->vector); 2092 rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 2093 irq->vector, irq->res); 2094 pci_release_msix_vector(adapter->pdev, irq->vector); 2095 irq->res = NULL; 2096 if (unlikely(rc != 0)) 2097 device_printf(adapter->pdev, "dev has no parent while " 2098 "releasing res for irq: %d\n", irq->vector); 2099 } 2100 } 2101 2102 static void 2103 ena_free_io_irq(struct ena_adapter *adapter) 2104 { 2105 struct ena_irq *irq; 2106 int rc; 2107 2108 for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) { 2109 irq = &adapter->irq_tbl[i]; 2110 if (irq->requested) { 2111 ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n", 2112 irq->vector); 2113 rc = bus_teardown_intr(adapter->pdev, irq->res, 2114 irq->cookie); 2115 if (unlikely(rc != 0)) { 2116 device_printf(adapter->pdev, "failed to tear " 2117 "down irq: %d\n", irq->vector); 2118 } 2119 irq->requested = 0; 2120 } 2121 2122 if (irq->res != NULL) { 2123 ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n", 2124 irq->vector); 2125 rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ, 2126 irq->vector, irq->res); 2127 pci_release_msix_vector(adapter->pdev, irq->vector); 2128 irq->res = NULL; 2129 if (unlikely(rc != 0)) { 2130 device_printf(adapter->pdev, "dev has no parent" 2131 " while releasing res for irq: %d\n", 2132 irq->vector); 2133 } 2134 } 2135 } 2136 } 2137 2138 static void 2139 ena_free_irqs(struct ena_adapter* adapter) 2140 { 2141 2142 ena_free_io_irq(adapter); 2143 ena_free_mgmnt_irq(adapter); 2144 ena_disable_msix(adapter); 2145 } 2146 2147 static void 2148 ena_disable_msix(struct ena_adapter *adapter) 2149 { 2150 2151 pci_release_msi(adapter->pdev); 2152 2153 adapter->msix_vecs = 0; 2154 kfree(adapter->msix_entries, M_DEVBUF); 2155 adapter->msix_entries = NULL; 2156 } 2157 2158 static void 2159 ena_unmask_all_io_irqs(struct ena_adapter *adapter) 2160 { 2161 struct ena_com_io_cq* io_cq; 2162 struct ena_eth_io_intr_reg intr_reg; 2163 uint16_t ena_qid; 2164 int i; 2165 2166 /* Unmask interrupts for all queues */ 2167 for (i = 0; i < adapter->num_queues; i++) { 2168 ena_qid = ENA_IO_TXQ_IDX(i); 2169 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid]; 2170 ena_com_update_intr_reg(&intr_reg, 0, 0, true); 2171 ena_com_unmask_intr(io_cq, &intr_reg); 2172 } 2173 } 2174 2175 /* Configure the Rx forwarding */ 2176 static int 2177 ena_rss_configure(struct ena_adapter *adapter) 2178 { 2179 struct ena_com_dev *ena_dev = adapter->ena_dev; 2180 int rc; 2181 2182 /* Set indirect table */ 2183 rc = ena_com_indirect_table_set(ena_dev); 2184 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) 2185 return (rc); 2186 2187 /* Configure hash function (if supported) */ 2188 rc = ena_com_set_hash_function(ena_dev); 2189 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) 2190 return (rc); 2191 2192 /* Configure hash inputs (if supported) */ 2193 rc = ena_com_set_hash_ctrl(ena_dev); 2194 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) 2195 return (rc); 2196 2197 return (0); 2198 } 2199 2200 static int 2201 ena_up_complete(struct ena_adapter *adapter) 2202 { 2203 int rc; 2204 2205 if (likely(adapter->rss_support)) { 2206 rc = ena_rss_configure(adapter); 2207 if (rc != 0) 2208 return (rc); 2209 } 2210 2211 rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu); 2212 if (unlikely(rc != 0)) 2213 return (rc); 2214 2215 ena_refill_all_rx_bufs(adapter); 2216 #if 0 /* XXX swildner counters */ 2217 ena_reset_counters((counter_u64_t *)&adapter->hw_stats, 2218 sizeof(adapter->hw_stats)); 2219 #endif 2220 2221 return (0); 2222 } 2223 2224 static int 2225 ena_up(struct ena_adapter *adapter) 2226 { 2227 int rc = 0; 2228 2229 if (unlikely(device_is_attached(adapter->pdev) == 0)) { 2230 device_printf(adapter->pdev, "device is not attached!\n"); 2231 return (ENXIO); 2232 } 2233 2234 if (unlikely(!adapter->running)) { 2235 device_printf(adapter->pdev, "device is not running!\n"); 2236 return (ENXIO); 2237 } 2238 2239 if (!adapter->up) { 2240 device_printf(adapter->pdev, "device is going UP\n"); 2241 2242 /* setup interrupts for IO queues */ 2243 ena_setup_io_intr(adapter); 2244 rc = ena_request_io_irq(adapter); 2245 if (unlikely(rc != 0)) { 2246 ena_trace(ENA_ALERT, "err_req_irq"); 2247 goto err_req_irq; 2248 } 2249 2250 /* allocate transmit descriptors */ 2251 rc = ena_setup_all_tx_resources(adapter); 2252 if (unlikely(rc != 0)) { 2253 ena_trace(ENA_ALERT, "err_setup_tx"); 2254 goto err_setup_tx; 2255 } 2256 2257 /* allocate receive descriptors */ 2258 rc = ena_setup_all_rx_resources(adapter); 2259 if (unlikely(rc != 0)) { 2260 ena_trace(ENA_ALERT, "err_setup_rx"); 2261 goto err_setup_rx; 2262 } 2263 2264 /* create IO queues for Rx & Tx */ 2265 rc = ena_create_io_queues(adapter); 2266 if (unlikely(rc != 0)) { 2267 ena_trace(ENA_ALERT, 2268 "create IO queues failed"); 2269 goto err_io_que; 2270 } 2271 2272 if (unlikely(adapter->link_status)) { 2273 adapter->ifp->if_link_state = LINK_STATE_UP; 2274 if_link_state_change(adapter->ifp); 2275 } 2276 2277 rc = ena_up_complete(adapter); 2278 if (unlikely(rc != 0)) 2279 goto err_up_complete; 2280 2281 #if 0 /* XXX swildner counters */ 2282 counter_u64_add(adapter->dev_stats.interface_up, 1); 2283 #endif 2284 2285 ena_update_hwassist(adapter); 2286 2287 adapter->ifp->if_flags |= IFF_RUNNING; 2288 ifq_clr_oactive(&adapter->ifp->if_snd); 2289 2290 callout_reset(&adapter->timer_service, hz, 2291 ena_timer_service, (void *)adapter); 2292 2293 adapter->up = true; 2294 2295 ena_unmask_all_io_irqs(adapter); 2296 } 2297 2298 return (0); 2299 2300 err_up_complete: 2301 ena_destroy_all_io_queues(adapter); 2302 err_io_que: 2303 ena_free_all_rx_resources(adapter); 2304 err_setup_rx: 2305 ena_free_all_tx_resources(adapter); 2306 err_setup_tx: 2307 ena_free_io_irq(adapter); 2308 err_req_irq: 2309 return (rc); 2310 } 2311 2312 #if 0 /* XXX swildner counters */ 2313 static uint64_t 2314 ena_get_counter(if_t ifp, ift_counter cnt) 2315 { 2316 struct ena_adapter *adapter; 2317 struct ena_hw_stats *stats; 2318 2319 adapter = ifp->if_softc; 2320 stats = &adapter->hw_stats; 2321 2322 switch (cnt) { 2323 case IFCOUNTER_IPACKETS: 2324 return (counter_u64_fetch(stats->rx_packets)); 2325 case IFCOUNTER_OPACKETS: 2326 return (counter_u64_fetch(stats->tx_packets)); 2327 case IFCOUNTER_IBYTES: 2328 return (counter_u64_fetch(stats->rx_bytes)); 2329 case IFCOUNTER_OBYTES: 2330 return (counter_u64_fetch(stats->tx_bytes)); 2331 case IFCOUNTER_IQDROPS: 2332 return (counter_u64_fetch(stats->rx_drops)); 2333 default: 2334 return (if_get_counter_default(ifp, cnt)); 2335 } 2336 } 2337 #endif 2338 2339 static int 2340 ena_media_change(if_t ifp) 2341 { 2342 /* Media Change is not supported by firmware */ 2343 return (0); 2344 } 2345 2346 static void 2347 ena_media_status(if_t ifp, struct ifmediareq *ifmr) 2348 { 2349 struct ena_adapter *adapter = ifp->if_softc; 2350 ena_trace(ENA_DBG, "enter"); 2351 2352 lockmgr(&adapter->global_lock, LK_EXCLUSIVE); 2353 2354 ifmr->ifm_status = IFM_AVALID; 2355 ifmr->ifm_active = IFM_ETHER; 2356 2357 if (!adapter->link_status) { 2358 lockmgr(&adapter->global_lock, LK_RELEASE); 2359 ena_trace(ENA_INFO, "link_status = false"); 2360 return; 2361 } 2362 2363 ifmr->ifm_status |= IFM_ACTIVE; 2364 ifmr->ifm_active |= IFM_10G_T | IFM_FDX; 2365 2366 lockmgr(&adapter->global_lock, LK_RELEASE); 2367 } 2368 2369 static void 2370 ena_init(void *arg) 2371 { 2372 struct ena_adapter *adapter = (struct ena_adapter *)arg; 2373 2374 if (!adapter->up) { 2375 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 2376 ena_up(adapter); 2377 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 2378 } 2379 } 2380 2381 static int 2382 ena_ioctl(if_t ifp, u_long command, caddr_t data, struct ucred *cred) 2383 { 2384 struct ena_adapter *adapter; 2385 struct ifreq *ifr; 2386 int rc; 2387 2388 adapter = ifp->if_softc; 2389 ifr = (struct ifreq *)data; 2390 2391 /* 2392 * Acquiring lock to prevent from running up and down routines parallel. 2393 */ 2394 rc = 0; 2395 switch (command) { 2396 case SIOCSIFMTU: 2397 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 2398 ena_down(adapter); 2399 2400 ena_change_mtu(ifp, ifr->ifr_mtu); 2401 2402 rc = ena_up(adapter); 2403 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 2404 break; 2405 2406 case SIOCSIFFLAGS: 2407 if ((ifp->if_flags & IFF_UP) != 0) { 2408 if ((ifp->if_flags & IFF_RUNNING) != 0) { 2409 if ((ifp->if_flags & (IFF_PROMISC | 2410 IFF_ALLMULTI)) != 0) { 2411 device_printf(adapter->pdev, 2412 "ioctl promisc/allmulti\n"); 2413 } 2414 } else { 2415 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 2416 rc = ena_up(adapter); 2417 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 2418 } 2419 } else { 2420 if ((ifp->if_flags & IFF_RUNNING) != 0) { 2421 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 2422 ena_down(adapter); 2423 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 2424 } 2425 } 2426 break; 2427 2428 case SIOCADDMULTI: 2429 case SIOCDELMULTI: 2430 break; 2431 2432 case SIOCSIFMEDIA: 2433 case SIOCGIFMEDIA: 2434 rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command); 2435 break; 2436 2437 case SIOCSIFCAP: 2438 { 2439 int reinit = 0; 2440 2441 if (ifr->ifr_reqcap != ifp->if_capenable) { 2442 ifp->if_capenable = ifr->ifr_reqcap; 2443 reinit = 1; 2444 } 2445 2446 if ((reinit != 0) && 2447 ((ifp->if_flags & IFF_RUNNING) != 0)) { 2448 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 2449 ena_down(adapter); 2450 rc = ena_up(adapter); 2451 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 2452 } 2453 } 2454 2455 break; 2456 default: 2457 rc = ether_ioctl(ifp, command, data); 2458 break; 2459 } 2460 2461 return (rc); 2462 } 2463 2464 static int 2465 ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat) 2466 { 2467 int caps = 0; 2468 2469 if ((feat->offload.tx & 2470 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK | 2471 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK | 2472 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0) 2473 caps |= IFCAP_TXCSUM; 2474 2475 if ((feat->offload.tx & 2476 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK | 2477 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0) 2478 caps |= IFCAP_TXCSUM; 2479 2480 if ((feat->offload.tx & 2481 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0) 2482 caps |= IFCAP_TSO4; 2483 2484 if ((feat->offload.tx & 2485 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0) 2486 caps |= IFCAP_TSO6; 2487 2488 if ((feat->offload.rx_supported & 2489 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK | 2490 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0) 2491 caps |= IFCAP_RXCSUM; 2492 2493 #if 0 2494 if ((feat->offload.rx_supported & 2495 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0) 2496 caps |= IFCAP_RXCSUM_IPV6; 2497 #endif 2498 #if 0 /* XXX LRO */ 2499 caps |= IFCAP_LRO; 2500 #endif 2501 caps |= IFCAP_JUMBO_MTU; 2502 2503 return (caps); 2504 } 2505 2506 static void 2507 ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp) 2508 { 2509 2510 host_info->supported_network_features[0] = 2511 (uint32_t)ifp->if_capabilities; 2512 } 2513 2514 static void 2515 ena_update_hwassist(struct ena_adapter *adapter) 2516 { 2517 if_t ifp = adapter->ifp; 2518 uint32_t feat = adapter->tx_offload_cap; 2519 int cap = ifp->if_capenable; 2520 int flags = 0; 2521 2522 ifp->if_hwassist = 0; 2523 2524 if ((cap & IFCAP_TXCSUM) != 0) { 2525 if ((feat & 2526 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0) 2527 flags |= CSUM_IP; 2528 if ((feat & 2529 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK | 2530 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0) 2531 flags |= CSUM_UDP | CSUM_TCP; 2532 } 2533 2534 #if 0 2535 if ((cap & IFCAP_TXCSUM_IPV6) != 0) 2536 flags |= CSUM_IP6_UDP | CSUM_IP6_TCP; 2537 #endif 2538 2539 if ((cap & IFCAP_TSO4) != 0 || (cap & IFCAP_TSO6) != 0) 2540 flags |= CSUM_TSO; 2541 2542 ifp->if_hwassist |= flags; 2543 } 2544 2545 static int 2546 ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter, 2547 struct ena_com_dev_get_features_ctx *feat) 2548 { 2549 if_t ifp; 2550 int caps = 0; 2551 2552 ifp = adapter->ifp = if_alloc(IFT_ETHER); 2553 if (unlikely(ifp == NULL)) { 2554 ena_trace(ENA_ALERT, "can not allocate ifnet structure\n"); 2555 return (ENXIO); 2556 } 2557 if_initname(ifp, device_get_name(pdev), device_get_unit(pdev)); 2558 ifp->if_softc = adapter; 2559 2560 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 2561 2562 ifp->if_init = ena_init; 2563 ifp->if_start = ena_start_xmit; 2564 ifp->if_ioctl = ena_ioctl; 2565 #if 0 /* XXX swildner counter */ 2566 if_setgetcounterfn(ifp, ena_get_counter); 2567 #endif 2568 2569 ifq_set_maxlen(&ifp->if_snd, adapter->tx_ring_size); 2570 ifq_set_ready(&ifp->if_snd); 2571 ifp->if_mtu = ETHERMTU; 2572 ifp->if_baudrate = 0; 2573 /* Zeroize capabilities... */ 2574 ifp->if_capabilities = 0; 2575 ifp->if_capenable = 0; 2576 /* check hardware support */ 2577 caps = ena_get_dev_offloads(feat); 2578 /* ... and set them */ 2579 //if_setcapabilitiesbit(ifp, caps, 0); 2580 ((struct ifnet *)ifp)->if_capabilities |= caps; 2581 ((struct ifnet *)ifp)->if_capabilities &= ~0; 2582 2583 /* TSO parameters */ 2584 //ifp->if_hw_tsomax = ENA_TSO_MAXSIZE - 2585 // (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 2586 ifp->if_tsolen = adapter->max_tx_sgl_size - 1; 2587 //ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE; 2588 2589 ifp->if_hdrlen = sizeof(struct ether_vlan_header); 2590 ifp->if_capenable= ifp->if_capabilities; 2591 2592 /* 2593 * Specify the media types supported by this adapter and register 2594 * callbacks to update media and link information 2595 */ 2596 ifmedia_init(&adapter->media, IFM_IMASK, 2597 ena_media_change, ena_media_status); 2598 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL); 2599 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO); 2600 2601 ether_ifattach(ifp, adapter->mac_addr, NULL); 2602 2603 return (0); 2604 } 2605 2606 static void 2607 ena_down(struct ena_adapter *adapter) 2608 { 2609 int rc; 2610 2611 if (adapter->up) { 2612 device_printf(adapter->pdev, "device is going DOWN\n"); 2613 2614 callout_drain(&adapter->timer_service); 2615 2616 adapter->up = false; 2617 ifq_set_oactive(&adapter->ifp->if_snd); 2618 adapter->ifp->if_flags &= ~IFF_RUNNING; 2619 2620 ena_free_io_irq(adapter); 2621 2622 if (adapter->trigger_reset) { 2623 rc = ena_com_dev_reset(adapter->ena_dev, 2624 adapter->reset_reason); 2625 if (unlikely(rc != 0)) 2626 device_printf(adapter->pdev, 2627 "Device reset failed\n"); 2628 } 2629 2630 ena_destroy_all_io_queues(adapter); 2631 2632 ena_free_all_tx_bufs(adapter); 2633 ena_free_all_rx_bufs(adapter); 2634 ena_free_all_tx_resources(adapter); 2635 ena_free_all_rx_resources(adapter); 2636 2637 #if 0 /* XXX swildner counters */ 2638 counter_u64_add(adapter->dev_stats.interface_down, 1); 2639 #endif 2640 } 2641 } 2642 2643 static void 2644 ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf) 2645 { 2646 struct ena_com_tx_meta *ena_meta; 2647 struct ether_vlan_header *eh; 2648 u32 mss; 2649 bool offload; 2650 uint16_t etype; 2651 int ehdrlen; 2652 struct ip *ip; 2653 int iphlen; 2654 struct tcphdr *th; 2655 2656 offload = false; 2657 ena_meta = &ena_tx_ctx->ena_meta; 2658 mss = mbuf->m_pkthdr.tso_segsz; 2659 2660 if (mss != 0) 2661 offload = true; 2662 2663 if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) 2664 offload = true; 2665 2666 if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0) 2667 offload = true; 2668 2669 if (!offload) { 2670 ena_tx_ctx->meta_valid = 0; 2671 return; 2672 } 2673 2674 /* Determine where frame payload starts. */ 2675 eh = mtod(mbuf, struct ether_vlan_header *); 2676 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 2677 etype = ntohs(eh->evl_proto); 2678 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 2679 } else { 2680 etype = ntohs(eh->evl_encap_proto); 2681 ehdrlen = ETHER_HDR_LEN; 2682 } 2683 2684 ip = (struct ip *)(mbuf->m_data + ehdrlen); 2685 iphlen = ip->ip_hl << 2; 2686 th = (struct tcphdr *)((caddr_t)ip + iphlen); 2687 2688 if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) { 2689 ena_tx_ctx->l3_csum_enable = 1; 2690 } 2691 if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2692 ena_tx_ctx->tso_enable = 1; 2693 ena_meta->l4_hdr_len = (th->th_off); 2694 } 2695 2696 switch (etype) { 2697 case ETHERTYPE_IP: 2698 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4; 2699 if ((ip->ip_off & htons(IP_DF)) != 0) 2700 ena_tx_ctx->df = 1; 2701 break; 2702 case ETHERTYPE_IPV6: 2703 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6; 2704 2705 default: 2706 break; 2707 } 2708 2709 if (ip->ip_p == IPPROTO_TCP) { 2710 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP; 2711 if ((mbuf->m_pkthdr.csum_flags & 2712 CSUM_TCP) != 0) 2713 ena_tx_ctx->l4_csum_enable = 1; 2714 else 2715 ena_tx_ctx->l4_csum_enable = 0; 2716 } else if (ip->ip_p == IPPROTO_UDP) { 2717 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP; 2718 if ((mbuf->m_pkthdr.csum_flags & 2719 CSUM_UDP) != 0) 2720 ena_tx_ctx->l4_csum_enable = 1; 2721 else 2722 ena_tx_ctx->l4_csum_enable = 0; 2723 } else { 2724 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN; 2725 ena_tx_ctx->l4_csum_enable = 0; 2726 } 2727 2728 ena_meta->mss = mss; 2729 ena_meta->l3_hdr_len = iphlen; 2730 ena_meta->l3_hdr_offset = ehdrlen; 2731 ena_tx_ctx->meta_valid = 1; 2732 } 2733 2734 static int 2735 ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf) 2736 { 2737 struct ena_adapter *adapter; 2738 struct mbuf *collapsed_mbuf; 2739 int num_frags; 2740 2741 adapter = tx_ring->adapter; 2742 num_frags = ena_mbuf_count(*mbuf); 2743 2744 /* One segment must be reserved for configuration descriptor. */ 2745 if (num_frags < adapter->max_tx_sgl_size) 2746 return (0); 2747 #if 0 /* XXX swildner counters */ 2748 counter_u64_add(tx_ring->tx_stats.collapse, 1); 2749 #endif 2750 2751 collapsed_mbuf = m_defrag(*mbuf, M_NOWAIT); 2752 if (unlikely(collapsed_mbuf == NULL)) { 2753 IFNET_STAT_INC(tx_ring->adapter->ifp, oerrors, 1); 2754 #if 0 /* XXX swildner counters */ 2755 counter_u64_add(tx_ring->tx_stats.collapse_err, 1); 2756 #endif 2757 return (ENOMEM); 2758 } 2759 2760 /* If mbuf was collapsed succesfully, original mbuf is released. */ 2761 *mbuf = collapsed_mbuf; 2762 2763 return (0); 2764 } 2765 2766 static int 2767 ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf) 2768 { 2769 struct ena_adapter *adapter; 2770 struct ena_tx_buffer *tx_info; 2771 struct ena_com_tx_ctx ena_tx_ctx; 2772 struct ena_com_dev *ena_dev; 2773 struct ena_com_buf *ena_buf; 2774 struct ena_com_io_sq* io_sq; 2775 bus_dma_segment_t segs[ENA_BUS_DMA_SEGS]; 2776 void *push_hdr; 2777 uint16_t next_to_use; 2778 uint16_t req_id; 2779 uint16_t push_len; 2780 uint16_t ena_qid; 2781 uint32_t len, nsegs, header_len; 2782 int i, rc; 2783 int nb_hw_desc; 2784 2785 ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id); 2786 adapter = tx_ring->que->adapter; 2787 ena_dev = adapter->ena_dev; 2788 io_sq = &ena_dev->io_sq_queues[ena_qid]; 2789 2790 //tx_ring is just used to grab the adapter 2791 rc = ena_check_and_collapse_mbuf(tx_ring, mbuf); 2792 if (unlikely(rc != 0)) { 2793 ena_trace(ENA_WARNING, 2794 "Failed to collapse mbuf! err: %d", rc); 2795 return (rc); 2796 } 2797 2798 next_to_use = tx_ring->next_to_use; 2799 req_id = tx_ring->free_tx_ids[next_to_use]; 2800 tx_info = &tx_ring->tx_buffer_info[req_id]; 2801 2802 tx_info->mbuf = *mbuf; 2803 tx_info->num_of_bufs = 0; 2804 2805 ena_buf = tx_info->bufs; 2806 len = (*mbuf)->m_len; 2807 2808 ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len); 2809 2810 push_len = 0; 2811 header_len = min_t(uint32_t, len, tx_ring->tx_max_header_size); 2812 push_hdr = NULL; 2813 2814 rc = bus_dmamap_load_mbuf_segment(adapter->tx_buf_tag, tx_info->map, 2815 *mbuf, segs, adapter->max_tx_sgl_size, &nsegs, BUS_DMA_NOWAIT); 2816 2817 if (unlikely((rc != 0) || (nsegs == 0))) { 2818 ena_trace(ENA_WARNING, 2819 "dmamap load failed! err: %d nsegs: %d", rc, nsegs); 2820 IFNET_STAT_INC(tx_ring->adapter->ifp, oerrors, 1); 2821 #if 0 /* XXX swildner counters */ 2822 counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1); 2823 #endif 2824 tx_info->mbuf = NULL; 2825 if (rc == ENOMEM) 2826 return (ENA_COM_NO_MEM); 2827 else 2828 return (ENA_COM_INVAL); 2829 } 2830 2831 for (i = 0; i < nsegs; i++) { 2832 ena_buf->len = segs[i].ds_len; 2833 ena_buf->paddr = segs[i].ds_addr; 2834 ena_buf++; 2835 } 2836 tx_info->num_of_bufs = nsegs; 2837 2838 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx)); 2839 ena_tx_ctx.ena_bufs = tx_info->bufs; 2840 ena_tx_ctx.push_header = push_hdr; 2841 ena_tx_ctx.num_bufs = tx_info->num_of_bufs; 2842 ena_tx_ctx.req_id = req_id; 2843 ena_tx_ctx.header_len = header_len; 2844 2845 /* Set flags and meta data */ 2846 ena_tx_csum(&ena_tx_ctx, *mbuf); 2847 /* Prepare the packet's descriptors and send them to device */ 2848 rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc); 2849 if (unlikely(rc != 0)) { 2850 device_printf(adapter->pdev, "failed to prepare tx bufs\n"); 2851 IFNET_STAT_INC(tx_ring->adapter->ifp, oerrors, 1); 2852 #if 0 /* XXX swildner counters */ 2853 counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1); 2854 #endif 2855 goto dma_error; 2856 } 2857 2858 IFNET_STAT_INC(tx_ring->adapter->ifp, opackets, 1); 2859 #if 0 /* XXX swildner counters */ 2860 counter_enter(); 2861 counter_u64_add_protected(tx_ring->tx_stats.cnt, 1); 2862 counter_u64_add_protected(tx_ring->tx_stats.bytes, 2863 (*mbuf)->m_pkthdr.len); 2864 2865 counter_u64_add_protected(adapter->hw_stats.tx_packets, 1); 2866 counter_u64_add_protected(adapter->hw_stats.tx_bytes, 2867 (*mbuf)->m_pkthdr.len); 2868 counter_exit(); 2869 #endif 2870 2871 tx_info->tx_descs = nb_hw_desc; 2872 getmicrouptime(&tx_info->timestamp); 2873 tx_info->print_once = true; 2874 2875 tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use, 2876 tx_ring->ring_size); 2877 2878 bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map, 2879 BUS_DMASYNC_PREWRITE); 2880 2881 return (0); 2882 2883 dma_error: 2884 tx_info->mbuf = NULL; 2885 bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map); 2886 2887 return (rc); 2888 } 2889 2890 static void 2891 ena_start_xmit(struct ifnet *ifp, struct ifaltq_subque *ifsq) 2892 { 2893 /* 2894 * TODO: Might need to initialize an ena_ring with the 2895 * ifaltq_subque in it 2896 */ 2897 struct ena_adapter *adapter = ifp->if_softc; 2898 struct ena_com_io_sq *io_sq; 2899 struct ena_ring *tx_ring; 2900 int ena_qid; 2901 int acum_pkts = 0; 2902 int ret = 0; 2903 2904 if (unlikely((adapter->ifp->if_flags & IFF_RUNNING) == 0) || 2905 ifsq_is_oactive(ifsq)) { 2906 return; 2907 } 2908 2909 /* Check is link_active and some other shit. If it is, purge. */ 2910 2911 #if 0 2912 if (unlikely(!adapter->link_status)) 2913 return; 2914 #endif 2915 2916 io_sq = NULL; 2917 tx_ring = NULL; 2918 2919 while (!ifsq_is_empty(ifsq)) { 2920 struct mbuf *m_head; 2921 int i; 2922 2923 //Grab head from mbuf list 2924 m_head = ifsq_dequeue(ifsq); 2925 if (m_head == NULL) 2926 break; 2927 2928 //pick the associated tx_ring based on hash 2929 i = m_head->m_pkthdr.hash % adapter->num_queues; 2930 2931 tx_ring = &adapter->tx_ring[i]; 2932 ENA_RING_MTX_LOCK(tx_ring); 2933 ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id); 2934 io_sq = &adapter->ena_dev->io_sq_queues[ena_qid]; 2935 2936 if (unlikely(!ena_com_sq_have_enough_space(io_sq, ENA_TX_CLEANUP_THRESHOLD))) 2937 ena_tx_cleanup(tx_ring); 2938 2939 if (unlikely((ret = ena_xmit_mbuf(tx_ring, &m_head)) != 0)) { 2940 if (ret == ENA_COM_NO_MEM) { 2941 /* XXX put mbuf back on queue */ 2942 } else if (ret == ENA_COM_NO_SPACE) { 2943 /* XXX put mbuf back on queue */ 2944 } else { 2945 m_freem(m_head); 2946 /* XXX advance mbuf queue aka move it forward? */ 2947 } 2948 ENA_RING_MTX_UNLOCK(tx_ring); 2949 break; 2950 } 2951 2952 //advance mbuf queue, might already be handled by dequeue 2953 2954 #if 0 2955 // dillon - wtf is this doing here? 2956 // NOT SURE WHAT TO DO WITH THIS CODE 2957 if (unlikely((adapter->ifp->if_flags & IFF_RUNNING) == 0)) 2958 return; // break here, not return. tx_ring locked 2959 #endif 2960 2961 acum_pkts++; 2962 2963 ENA_RING_MTX_UNLOCK(tx_ring); 2964 BPF_MTAP(adapter->ifp, m_head); 2965 2966 if (unlikely(acum_pkts == DB_THRESHOLD)) { 2967 acum_pkts = 0; 2968 wmb(); 2969 /* Trigger the dma engine */ 2970 ena_com_write_sq_doorbell(io_sq); 2971 #if 0 /* XXX swildner counters */ 2972 counter_u64_add(tx_ring->tx_stats.doorbells, 1); 2973 #endif 2974 } 2975 2976 } 2977 2978 if (likely(acum_pkts != 0)) { 2979 wmb(); 2980 /* Trigger the dma engine */ 2981 ena_com_write_sq_doorbell(io_sq); 2982 #if 0 /* XXX swildner counters */ 2983 counter_u64_add(tx_ring->tx_stats.doorbells, 1); 2984 #endif 2985 } 2986 2987 if (io_sq && 2988 !ena_com_sq_have_enough_space(io_sq, ENA_TX_CLEANUP_THRESHOLD)) { 2989 ENA_RING_MTX_LOCK(tx_ring); 2990 ena_tx_cleanup(tx_ring); 2991 ENA_RING_MTX_UNLOCK(tx_ring); 2992 } 2993 } 2994 2995 static int 2996 ena_calc_io_queue_num(struct ena_adapter *adapter, 2997 struct ena_com_dev_get_features_ctx *get_feat_ctx) 2998 { 2999 int io_sq_num, io_cq_num, io_queue_num; 3000 3001 io_sq_num = get_feat_ctx->max_queues.max_sq_num; 3002 io_cq_num = get_feat_ctx->max_queues.max_cq_num; 3003 3004 io_queue_num = min_t(int, ncpus, ENA_MAX_NUM_IO_QUEUES); 3005 io_queue_num = min_t(int, io_queue_num, io_sq_num); 3006 io_queue_num = min_t(int, io_queue_num, io_cq_num); 3007 /* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */ 3008 io_queue_num = min_t(int, io_queue_num, 3009 pci_msix_count(adapter->pdev) - 1); 3010 #ifdef RSS 3011 io_queue_num = min_t(int, io_queue_num, rss_getnumbuckets()); 3012 #endif 3013 3014 return (io_queue_num); 3015 } 3016 3017 static int 3018 ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size, 3019 uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat) 3020 { 3021 uint32_t queue_size = ENA_DEFAULT_RING_SIZE; 3022 uint32_t v; 3023 uint32_t q; 3024 3025 queue_size = min_t(uint32_t, queue_size, 3026 feat->max_queues.max_cq_depth); 3027 queue_size = min_t(uint32_t, queue_size, 3028 feat->max_queues.max_sq_depth); 3029 3030 /* round down to the nearest power of 2 */ 3031 v = queue_size; 3032 while (v != 0) { 3033 if (powerof2(queue_size) != 0) 3034 break; 3035 v /= 2; 3036 q = rounddown2(queue_size, v); 3037 if (q != 0) { 3038 queue_size = q; 3039 break; 3040 } 3041 } 3042 3043 if (unlikely(queue_size == 0)) { 3044 device_printf(adapter->pdev, "Invalid queue size\n"); 3045 return (ENA_COM_FAULT); 3046 } 3047 3048 *max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 3049 feat->max_queues.max_packet_tx_descs); 3050 *max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS, 3051 feat->max_queues.max_packet_rx_descs); 3052 3053 return (queue_size); 3054 } 3055 3056 static int 3057 ena_rss_init_default(struct ena_adapter *adapter) 3058 { 3059 struct ena_com_dev *ena_dev = adapter->ena_dev; 3060 device_t dev = adapter->pdev; 3061 int qid, rc, i; 3062 3063 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE); 3064 if (unlikely(rc != 0)) { 3065 device_printf(dev, "Cannot init indirect table\n"); 3066 return (rc); 3067 } 3068 3069 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) { 3070 #ifdef RSS 3071 qid = rss_get_indirection_to_bucket(i); 3072 qid = qid % adapter->num_queues; 3073 #else 3074 qid = i % adapter->num_queues; 3075 #endif 3076 rc = ena_com_indirect_table_fill_entry(ena_dev, i, 3077 ENA_IO_RXQ_IDX(qid)); 3078 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) { 3079 device_printf(dev, "Cannot fill indirect table\n"); 3080 goto err_rss_destroy; 3081 } 3082 } 3083 3084 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL, 3085 ENA_HASH_KEY_SIZE, 0xFFFFFFFF); 3086 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) { 3087 device_printf(dev, "Cannot fill hash function\n"); 3088 goto err_rss_destroy; 3089 } 3090 3091 rc = ena_com_set_default_hash_ctrl(ena_dev); 3092 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) { 3093 device_printf(dev, "Cannot fill hash control\n"); 3094 goto err_rss_destroy; 3095 } 3096 3097 return (0); 3098 3099 err_rss_destroy: 3100 ena_com_rss_destroy(ena_dev); 3101 return (rc); 3102 } 3103 3104 static void 3105 ena_rss_init_default_deferred(void *arg) 3106 { 3107 struct ena_adapter *adapter; 3108 devclass_t dc; 3109 int max; 3110 int rc; 3111 3112 dc = devclass_find("ena"); 3113 if (unlikely(dc == NULL)) { 3114 ena_trace(ENA_ALERT, "No devclass ena\n"); 3115 return; 3116 } 3117 3118 max = devclass_get_maxunit(dc); 3119 while (max-- >= 0) { 3120 adapter = devclass_get_softc(dc, max); 3121 if (adapter != NULL) { 3122 rc = ena_rss_init_default(adapter); 3123 adapter->rss_support = true; 3124 if (unlikely(rc != 0)) { 3125 device_printf(adapter->pdev, 3126 "WARNING: RSS was not properly initialized," 3127 " it will affect bandwidth\n"); 3128 adapter->rss_support = false; 3129 } 3130 } 3131 } 3132 } 3133 SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL); 3134 3135 static void 3136 ena_config_host_info(struct ena_com_dev *ena_dev) 3137 { 3138 struct ena_admin_host_info *host_info; 3139 int rc; 3140 3141 /* Allocate only the host info */ 3142 rc = ena_com_allocate_host_info(ena_dev); 3143 if (unlikely(rc != 0)) { 3144 ena_trace(ENA_ALERT, "Cannot allocate host info\n"); 3145 return; 3146 } 3147 3148 host_info = ena_dev->host_attr.host_info; 3149 3150 host_info->os_type = ENA_ADMIN_OS_FREEBSD; 3151 host_info->kernel_ver = osreldate; 3152 3153 ksprintf(host_info->kernel_ver_str, "%d", osreldate); 3154 host_info->os_dist = 0; 3155 strncpy(host_info->os_dist_str, osrelease, 3156 sizeof(host_info->os_dist_str) - 1); 3157 3158 host_info->driver_version = 3159 (DRV_MODULE_VER_MAJOR) | 3160 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) | 3161 (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT); 3162 3163 rc = ena_com_set_host_attributes(ena_dev); 3164 if (unlikely(rc != 0)) { 3165 if (rc == EOPNOTSUPP) 3166 ena_trace(ENA_WARNING, "Cannot set host attributes\n"); 3167 else 3168 ena_trace(ENA_ALERT, "Cannot set host attributes\n"); 3169 3170 goto err; 3171 } 3172 3173 return; 3174 3175 err: 3176 ena_com_delete_host_info(ena_dev); 3177 } 3178 3179 static int 3180 ena_device_init(struct ena_adapter *adapter, device_t pdev, 3181 struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active) 3182 { 3183 struct ena_com_dev* ena_dev = adapter->ena_dev; 3184 bool readless_supported; 3185 uint32_t aenq_groups; 3186 int dma_width; 3187 int rc; 3188 3189 rc = ena_com_mmio_reg_read_request_init(ena_dev); 3190 if (unlikely(rc != 0)) { 3191 device_printf(pdev, "failed to init mmio read less\n"); 3192 return (rc); 3193 } 3194 3195 /* 3196 * The PCIe configuration space revision id indicate if mmio reg 3197 * read is disabled 3198 */ 3199 readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ); 3200 ena_com_set_mmio_read_mode(ena_dev, readless_supported); 3201 3202 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL); 3203 if (unlikely(rc != 0)) { 3204 device_printf(pdev, "Can not reset device\n"); 3205 goto err_mmio_read_less; 3206 } 3207 3208 rc = ena_com_validate_version(ena_dev); 3209 if (unlikely(rc != 0)) { 3210 device_printf(pdev, "device version is too low\n"); 3211 goto err_mmio_read_less; 3212 } 3213 3214 dma_width = ena_com_get_dma_width(ena_dev); 3215 if (unlikely(dma_width < 0)) { 3216 device_printf(pdev, "Invalid dma width value %d", dma_width); 3217 rc = dma_width; 3218 goto err_mmio_read_less; 3219 } 3220 adapter->dma_width = dma_width; 3221 3222 /* ENA admin level init */ 3223 rc = ena_com_admin_init(ena_dev, &aenq_handlers, true); 3224 if (unlikely(rc != 0)) { 3225 device_printf(pdev, 3226 "Can not initialize ena admin queue with device\n"); 3227 goto err_mmio_read_less; 3228 } 3229 3230 /* 3231 * To enable the msix interrupts the driver needs to know the number 3232 * of queues. So the driver uses polling mode to retrieve this 3233 * information 3234 */ 3235 ena_com_set_admin_polling_mode(ena_dev, true); 3236 3237 ena_config_host_info(ena_dev); 3238 3239 /* Get Device Attributes */ 3240 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx); 3241 if (unlikely(rc != 0)) { 3242 device_printf(pdev, 3243 "Cannot get attribute for ena device rc: %d\n", rc); 3244 goto err_admin_init; 3245 } 3246 3247 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | BIT(ENA_ADMIN_KEEP_ALIVE); 3248 3249 aenq_groups &= get_feat_ctx->aenq.supported_groups; 3250 rc = ena_com_set_aenq_config(ena_dev, aenq_groups); 3251 if (unlikely(rc != 0)) { 3252 device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc); 3253 goto err_admin_init; 3254 } 3255 3256 *wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE)); 3257 3258 return (0); 3259 3260 err_admin_init: 3261 ena_com_delete_host_info(ena_dev); 3262 ena_com_admin_destroy(ena_dev); 3263 err_mmio_read_less: 3264 ena_com_mmio_reg_read_request_destroy(ena_dev); 3265 3266 return (rc); 3267 } 3268 3269 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter, 3270 int io_vectors) 3271 { 3272 struct ena_com_dev *ena_dev = adapter->ena_dev; 3273 int rc; 3274 3275 rc = ena_enable_msix(adapter); 3276 if (unlikely(rc != 0)) { 3277 device_printf(adapter->pdev, "Error with MSI-X enablement\n"); 3278 return (rc); 3279 } 3280 3281 ena_setup_mgmnt_intr(adapter); 3282 3283 rc = ena_request_mgmnt_irq(adapter); 3284 if (unlikely(rc != 0)) { 3285 device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n"); 3286 goto err_disable_msix; 3287 } 3288 3289 pci_enable_msix(adapter->pdev); 3290 3291 ena_com_set_admin_polling_mode(ena_dev, false); 3292 3293 ena_com_admin_aenq_enable(ena_dev); 3294 3295 return (0); 3296 3297 err_disable_msix: 3298 ena_disable_msix(adapter); 3299 3300 return (rc); 3301 } 3302 3303 /* Function called on ENA_ADMIN_KEEP_ALIVE event */ 3304 static void ena_keep_alive_wd(void *adapter_data, 3305 struct ena_admin_aenq_entry *aenq_e) 3306 { 3307 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 3308 struct ena_admin_aenq_keep_alive_desc *desc; 3309 struct timeval time; 3310 uint64_t rx_drops; 3311 3312 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e; 3313 3314 rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low; 3315 IFNET_STAT_INC(adapter->ifp, iqdrops, 1); 3316 #if 0 /* XXX swildner counters */ 3317 counter_u64_zero(adapter->hw_stats.rx_drops); 3318 counter_u64_add(adapter->hw_stats.rx_drops, rx_drops); 3319 #endif 3320 3321 getmicrouptime(&time); 3322 atomic_store_rel_64(&adapter->keep_alive_timestamp.tv_sec, time.tv_sec); 3323 } 3324 3325 /* Check for keep alive expiration */ 3326 static void check_for_missing_keep_alive(struct ena_adapter *adapter) 3327 { 3328 struct timeval timestamp, time; 3329 3330 if (adapter->wd_active == 0) 3331 return; 3332 3333 if (likely(adapter->keep_alive_timeout == 0)) 3334 return; 3335 3336 timestamp.tv_sec = atomic_load_acq_64(&adapter->keep_alive_timestamp.tv_sec); 3337 getmicrouptime(&time); 3338 timevalsub(&time, ×tamp); 3339 if (unlikely(time.tv_sec > adapter->keep_alive_timeout)) { 3340 device_printf(adapter->pdev, 3341 "Keep alive watchdog timeout.\n"); 3342 #if 0 /* XXX swildner counters */ 3343 counter_u64_add(adapter->dev_stats.wd_expired, 1); 3344 #endif 3345 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO; 3346 adapter->trigger_reset = true; 3347 } 3348 } 3349 3350 /* Check if admin queue is enabled */ 3351 static void check_for_admin_com_state(struct ena_adapter *adapter) 3352 { 3353 if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) == 3354 false)) { 3355 device_printf(adapter->pdev, 3356 "ENA admin queue is not in running state!\n"); 3357 #if 0 /* XXX swildner counters */ 3358 counter_u64_add(adapter->dev_stats.admin_q_pause, 1); 3359 #endif 3360 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO; 3361 adapter->trigger_reset = true; 3362 } 3363 } 3364 3365 static int 3366 check_missing_comp_in_queue(struct ena_adapter *adapter, 3367 struct ena_ring *tx_ring) 3368 { 3369 struct timeval curtime, time; 3370 struct ena_tx_buffer *tx_buf; 3371 uint32_t missed_tx = 0; 3372 int i; 3373 3374 getmicrouptime(&curtime); 3375 3376 for (i = 0; i < tx_ring->ring_size; i++) { 3377 tx_buf = &tx_ring->tx_buffer_info[i]; 3378 3379 if (timevalisset(&tx_buf->timestamp) == 0) 3380 continue; 3381 3382 time = curtime; 3383 timevalsub(&time, &tx_buf->timestamp); 3384 3385 /* Check again if packet is still waiting */ 3386 //WATCH: Might not be exactly comparable 3387 if (unlikely(time.tv_sec > adapter->missing_tx_timeout)) { 3388 3389 if (!tx_buf->print_once) 3390 ena_trace(ENA_WARNING, "Found a Tx that wasn't " 3391 "completed on time, qid %d, index %d.\n", 3392 tx_ring->qid, i); 3393 3394 tx_buf->print_once = true; 3395 missed_tx++; 3396 #if 0 /* XXX swildner counters */ 3397 counter_u64_add(tx_ring->tx_stats.missing_tx_comp, 1); 3398 #endif 3399 3400 if (unlikely(missed_tx > 3401 adapter->missing_tx_threshold)) { 3402 device_printf(adapter->pdev, 3403 "The number of lost tx completion " 3404 "is above the threshold (%d > %d). " 3405 "Reset the device\n", 3406 missed_tx, adapter->missing_tx_threshold); 3407 adapter->reset_reason = 3408 ENA_REGS_RESET_MISS_TX_CMPL; 3409 adapter->trigger_reset = true; 3410 return (EIO); 3411 } 3412 } 3413 } 3414 3415 return (0); 3416 } 3417 3418 /* 3419 * Check for TX which were not completed on time. 3420 * Timeout is defined by "missing_tx_timeout". 3421 * Reset will be performed if number of incompleted 3422 * transactions exceeds "missing_tx_threshold". 3423 */ 3424 static void 3425 check_for_missing_tx_completions(struct ena_adapter *adapter) 3426 { 3427 struct ena_ring *tx_ring; 3428 int i, budget, rc; 3429 3430 /* Make sure the driver doesn't turn the device in other process */ 3431 rmb(); 3432 3433 if (!adapter->up) 3434 return; 3435 3436 if (adapter->trigger_reset) 3437 return; 3438 3439 if (adapter->missing_tx_timeout == 0) 3440 return; 3441 3442 budget = adapter->missing_tx_max_queues; 3443 3444 for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) { 3445 tx_ring = &adapter->tx_ring[i]; 3446 3447 rc = check_missing_comp_in_queue(adapter, tx_ring); 3448 if (unlikely(rc != 0)) 3449 return; 3450 3451 budget--; 3452 if (budget == 0) { 3453 i++; 3454 break; 3455 } 3456 } 3457 3458 adapter->next_monitored_tx_qid = i % adapter->num_queues; 3459 } 3460 3461 /* trigger deferred rx cleanup after 2 consecutive detections */ 3462 #define EMPTY_RX_REFILL 2 3463 /* For the rare case where the device runs out of Rx descriptors and the 3464 * msix handler failed to refill new Rx descriptors (due to a lack of memory 3465 * for example). 3466 * This case will lead to a deadlock: 3467 * The device won't send interrupts since all the new Rx packets will be dropped 3468 * The msix handler won't allocate new Rx descriptors so the device won't be 3469 * able to send new packets. 3470 * 3471 * When such a situation is detected - execute rx cleanup task in another thread 3472 */ 3473 static void 3474 check_for_empty_rx_ring(struct ena_adapter *adapter) 3475 { 3476 struct ena_ring *rx_ring; 3477 int i, refill_required; 3478 3479 if (!adapter->up) 3480 return; 3481 3482 if (adapter->trigger_reset) 3483 return; 3484 3485 for (i = 0; i < adapter->num_queues; i++) { 3486 rx_ring = &adapter->rx_ring[i]; 3487 3488 refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq); 3489 if (unlikely(refill_required == (rx_ring->ring_size - 1))) { 3490 rx_ring->empty_rx_queue++; 3491 3492 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) { 3493 #if 0 /* XXX swildner counters */ 3494 counter_u64_add(rx_ring->rx_stats.empty_rx_ring, 3495 1); 3496 #endif 3497 3498 device_printf(adapter->pdev, 3499 "trigger refill for ring %d\n", i); 3500 3501 taskqueue_enqueue(rx_ring->cmpl_tq, 3502 &rx_ring->cmpl_task); 3503 rx_ring->empty_rx_queue = 0; 3504 } 3505 } else { 3506 rx_ring->empty_rx_queue = 0; 3507 } 3508 } 3509 } 3510 3511 static void 3512 ena_timer_service(void *data) 3513 { 3514 struct ena_adapter *adapter = (struct ena_adapter *)data; 3515 struct ena_admin_host_info *host_info = 3516 adapter->ena_dev->host_attr.host_info; 3517 3518 check_for_missing_keep_alive(adapter); 3519 3520 check_for_admin_com_state(adapter); 3521 3522 check_for_missing_tx_completions(adapter); 3523 3524 check_for_empty_rx_ring(adapter); 3525 3526 if (host_info != NULL) 3527 ena_update_host_info(host_info, adapter->ifp); 3528 3529 if (unlikely(adapter->trigger_reset)) { 3530 device_printf(adapter->pdev, "Trigger reset is on\n"); 3531 taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task); 3532 return; 3533 } 3534 3535 /* 3536 * Schedule another timeout one second from now. 3537 */ 3538 /* XXX swildner callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0); */ 3539 callout_reset(&adapter->timer_service, hz, ena_timer_service, 3540 (void *)adapter); 3541 } 3542 3543 static void 3544 ena_reset_task(void *arg, int pending) 3545 { 3546 struct ena_com_dev_get_features_ctx get_feat_ctx; 3547 struct ena_adapter *adapter = (struct ena_adapter *)arg; 3548 struct ena_com_dev *ena_dev = adapter->ena_dev; 3549 bool dev_up; 3550 int rc; 3551 3552 if (unlikely(!adapter->trigger_reset)) { 3553 device_printf(adapter->pdev, 3554 "device reset scheduled but trigger_reset is off\n"); 3555 return; 3556 } 3557 3558 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 3559 3560 callout_drain(&adapter->timer_service); 3561 3562 dev_up = adapter->up; 3563 3564 ena_com_set_admin_running_state(ena_dev, false); 3565 ena_down(adapter); 3566 ena_free_mgmnt_irq(adapter); 3567 ena_disable_msix(adapter); 3568 ena_com_abort_admin_commands(ena_dev); 3569 ena_com_wait_for_abort_completion(ena_dev); 3570 ena_com_admin_destroy(ena_dev); 3571 ena_com_mmio_reg_read_request_destroy(ena_dev); 3572 3573 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3574 adapter->trigger_reset = false; 3575 3576 /* Finished destroy part. Restart the device */ 3577 rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx, 3578 &adapter->wd_active); 3579 if (unlikely(rc != 0)) { 3580 device_printf(adapter->pdev, 3581 "ENA device init failed! (err: %d)\n", rc); 3582 goto err_dev_free; 3583 } 3584 3585 rc = ena_enable_msix_and_set_admin_interrupts(adapter, 3586 adapter->num_queues); 3587 if (unlikely(rc != 0)) { 3588 device_printf(adapter->pdev, "Enable MSI-X failed\n"); 3589 goto err_com_free; 3590 } 3591 3592 /* If the interface was up before the reset bring it up */ 3593 if (dev_up) { 3594 rc = ena_up(adapter); 3595 if (unlikely(rc != 0)) { 3596 device_printf(adapter->pdev, 3597 "Failed to create I/O queues\n"); 3598 goto err_msix_free; 3599 } 3600 } 3601 3602 callout_reset(&adapter->timer_service, hz, 3603 ena_timer_service, (void *)adapter); 3604 3605 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 3606 3607 return; 3608 3609 err_msix_free: 3610 ena_free_mgmnt_irq(adapter); 3611 ena_disable_msix(adapter); 3612 err_com_free: 3613 ena_com_admin_destroy(ena_dev); 3614 err_dev_free: 3615 device_printf(adapter->pdev, "ENA reset failed!\n"); 3616 adapter->running = false; 3617 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 3618 } 3619 3620 /** 3621 * ena_attach - Device Initialization Routine 3622 * @pdev: device information struct 3623 * 3624 * Returns 0 on success, otherwise on failure. 3625 * 3626 * ena_attach initializes an adapter identified by a device structure. 3627 * The OS initialization, configuring of the adapter private structure, 3628 * and a hardware reset occur. 3629 **/ 3630 static int 3631 ena_attach(device_t pdev) 3632 { 3633 struct ena_com_dev_get_features_ctx get_feat_ctx; 3634 static int version_printed; 3635 struct ena_adapter *adapter; 3636 struct ena_com_dev *ena_dev = NULL; 3637 uint16_t tx_sgl_size = 0; 3638 uint16_t rx_sgl_size = 0; 3639 int io_queue_num; 3640 int queue_size; 3641 int rc; 3642 adapter = device_get_softc(pdev); 3643 adapter->pdev = pdev; 3644 3645 lockinit(&adapter->global_lock, "ENA global mtx", 0, LK_CANRECURSE); 3646 lockinit(&adapter->ioctl_lock, "ENA ioctl sx", 0, LK_CANRECURSE); 3647 3648 /* Set up the timer service */ 3649 callout_init_lk(&adapter->timer_service, &adapter->global_lock); 3650 adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO; 3651 adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO; 3652 adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES; 3653 adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD; 3654 3655 if (version_printed++ == 0) 3656 device_printf(pdev, "%s\n", ena_version); 3657 3658 rc = ena_allocate_pci_resources(adapter); 3659 if (unlikely(rc != 0)) { 3660 device_printf(pdev, "PCI resource allocation failed!\n"); 3661 ena_free_pci_resources(adapter); 3662 return (rc); 3663 } 3664 3665 /* Allocate memory for ena_dev structure */ 3666 ena_dev = kmalloc(sizeof(struct ena_com_dev), M_DEVBUF, 3667 M_WAITOK | M_ZERO); 3668 3669 adapter->ena_dev = ena_dev; 3670 ena_dev->dmadev = pdev; 3671 ena_dev->bus = kmalloc(sizeof(struct ena_bus), M_DEVBUF, 3672 M_WAITOK | M_ZERO); 3673 3674 /* Store register resources */ 3675 ((struct ena_bus*)(ena_dev->bus))->reg_bar_t = 3676 rman_get_bustag(adapter->registers); 3677 ((struct ena_bus*)(ena_dev->bus))->reg_bar_h = 3678 rman_get_bushandle(adapter->registers); 3679 3680 if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) { 3681 device_printf(pdev, "failed to pmap registers bar\n"); 3682 rc = ENXIO; 3683 goto err_bus_free; 3684 } 3685 3686 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3687 3688 /* Device initialization */ 3689 rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active); 3690 if (unlikely(rc != 0)) { 3691 device_printf(pdev, "ENA device init failed! (err: %d)\n", rc); 3692 rc = ENXIO; 3693 goto err_bus_free; 3694 } 3695 3696 getmicrouptime(&adapter->keep_alive_timestamp); 3697 3698 adapter->tx_offload_cap = get_feat_ctx.offload.tx; 3699 3700 /* Set for sure that interface is not up */ 3701 adapter->up = false; 3702 3703 memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr, 3704 ETHER_ADDR_LEN); 3705 3706 /* calculate IO queue number to create */ 3707 io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx); 3708 3709 ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n", 3710 io_queue_num); 3711 adapter->num_queues = io_queue_num; 3712 3713 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu; 3714 3715 /* calculatre ring sizes */ 3716 queue_size = ena_calc_queue_size(adapter,&tx_sgl_size, 3717 &rx_sgl_size, &get_feat_ctx); 3718 if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) { 3719 rc = ENA_COM_FAULT; 3720 goto err_com_free; 3721 } 3722 3723 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3724 3725 adapter->tx_ring_size = queue_size; 3726 adapter->rx_ring_size = queue_size; 3727 3728 adapter->max_tx_sgl_size = tx_sgl_size; 3729 adapter->max_rx_sgl_size = rx_sgl_size; 3730 3731 /* set up dma tags for rx and tx buffers */ 3732 rc = ena_setup_tx_dma_tag(adapter); 3733 if (unlikely(rc != 0)) { 3734 device_printf(pdev, "Failed to create TX DMA tag\n"); 3735 goto err_com_free; 3736 } 3737 3738 rc = ena_setup_rx_dma_tag(adapter); 3739 if (unlikely(rc != 0)) { 3740 device_printf(pdev, "Failed to create RX DMA tag\n"); 3741 goto err_tx_tag_free; 3742 } 3743 3744 /* initialize rings basic information */ 3745 device_printf(pdev, "initialize %d io queues\n", io_queue_num); 3746 ena_init_io_rings(adapter); 3747 3748 /* setup network interface */ 3749 rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx); 3750 if (unlikely(rc != 0)) { 3751 device_printf(pdev, "Error with network interface setup\n"); 3752 goto err_io_free; 3753 } 3754 3755 rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num); 3756 if (unlikely(rc != 0)) { 3757 device_printf(pdev, 3758 "Failed to enable and set the admin interrupts\n"); 3759 goto err_ifp_free; 3760 } 3761 3762 /* Initialize reset task queue */ 3763 TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter); 3764 adapter->reset_tq = taskqueue_create("ena_reset_enqueue", 3765 M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq); 3766 taskqueue_start_threads(&adapter->reset_tq, 1, TDPRI_KERN_DAEMON, -1, 3767 "%s rstq", device_get_nameunit(adapter->pdev)); 3768 3769 /* Initialize statistics */ 3770 #if 0 /* XXX swildner counters */ 3771 ena_alloc_counters((counter_u64_t *)&adapter->dev_stats, 3772 sizeof(struct ena_stats_dev)); 3773 ena_alloc_counters((counter_u64_t *)&adapter->hw_stats, 3774 sizeof(struct ena_hw_stats)); 3775 #endif 3776 ena_sysctl_add_nodes(adapter); 3777 3778 /* Tell the stack that the interface is not active */ 3779 ifq_set_oactive(&adapter->ifp->if_snd); 3780 adapter->ifp->if_flags &= ~IFF_RUNNING; 3781 3782 adapter->running = true; 3783 return (0); 3784 3785 err_ifp_free: 3786 if_detach(adapter->ifp); 3787 if_free(adapter->ifp); 3788 err_io_free: 3789 ena_free_all_io_rings_resources(adapter); 3790 ena_free_rx_dma_tag(adapter); 3791 err_tx_tag_free: 3792 ena_free_tx_dma_tag(adapter); 3793 err_com_free: 3794 ena_com_admin_destroy(ena_dev); 3795 ena_com_delete_host_info(ena_dev); 3796 ena_com_mmio_reg_read_request_destroy(ena_dev); 3797 err_bus_free: 3798 kfree(ena_dev->bus, M_DEVBUF); 3799 kfree(ena_dev, M_DEVBUF); 3800 ena_free_pci_resources(adapter); 3801 3802 return (rc); 3803 } 3804 3805 /** 3806 * ena_detach - Device Removal Routine 3807 * @pdev: device information struct 3808 * 3809 * ena_detach is called by the device subsystem to alert the driver 3810 * that it should release a PCI device. 3811 **/ 3812 static int 3813 ena_detach(device_t pdev) 3814 { 3815 struct ena_adapter *adapter = device_get_softc(pdev); 3816 struct ena_com_dev *ena_dev = adapter->ena_dev; 3817 int rc; 3818 3819 /* Make sure VLANS are not using driver */ 3820 if (adapter->ifp->if_vlantrunks != NULL) { 3821 device_printf(adapter->pdev ,"VLAN is in use, detach first\n"); 3822 return (EBUSY); 3823 } 3824 3825 /* Free reset task and callout */ 3826 callout_drain(&adapter->timer_service); 3827 while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL)) 3828 taskqueue_drain(adapter->reset_tq, &adapter->reset_task); 3829 taskqueue_free(adapter->reset_tq); 3830 3831 lockmgr(&adapter->ioctl_lock, LK_EXCLUSIVE); 3832 ena_down(adapter); 3833 lockmgr(&adapter->ioctl_lock, LK_RELEASE); 3834 3835 if (adapter->ifp != NULL) { 3836 ether_ifdetach(adapter->ifp); 3837 if_free(adapter->ifp); 3838 } 3839 3840 ena_free_all_io_rings_resources(adapter); 3841 3842 #if 0 /* XXX swildner counters */ 3843 ena_free_counters((counter_u64_t *)&adapter->hw_stats, 3844 sizeof(struct ena_hw_stats)); 3845 ena_free_counters((counter_u64_t *)&adapter->dev_stats, 3846 sizeof(struct ena_stats_dev)); 3847 #endif 3848 3849 if (likely(adapter->rss_support)) 3850 ena_com_rss_destroy(ena_dev); 3851 3852 rc = ena_free_rx_dma_tag(adapter); 3853 if (unlikely(rc != 0)) 3854 device_printf(adapter->pdev, 3855 "Unmapped RX DMA tag associations\n"); 3856 3857 rc = ena_free_tx_dma_tag(adapter); 3858 if (unlikely(rc != 0)) 3859 device_printf(adapter->pdev, 3860 "Unmapped TX DMA tag associations\n"); 3861 3862 /* Reset the device only if the device is running. */ 3863 if (adapter->running) 3864 ena_com_dev_reset(ena_dev, adapter->reset_reason); 3865 3866 ena_com_delete_host_info(ena_dev); 3867 3868 ena_free_irqs(adapter); 3869 3870 ena_com_abort_admin_commands(ena_dev); 3871 3872 ena_com_wait_for_abort_completion(ena_dev); 3873 3874 ena_com_admin_destroy(ena_dev); 3875 3876 ena_com_mmio_reg_read_request_destroy(ena_dev); 3877 3878 ena_free_pci_resources(adapter); 3879 3880 lockuninit(&adapter->global_lock); 3881 lockuninit(&adapter->ioctl_lock); 3882 3883 if (ena_dev->bus != NULL) 3884 kfree(ena_dev->bus, M_DEVBUF); 3885 3886 if (ena_dev != NULL) 3887 kfree(ena_dev, M_DEVBUF); 3888 3889 return (bus_generic_detach(pdev)); 3890 } 3891 3892 /****************************************************************************** 3893 ******************************** AENQ Handlers ******************************* 3894 *****************************************************************************/ 3895 /** 3896 * ena_update_on_link_change: 3897 * Notify the network interface about the change in link status 3898 **/ 3899 static void 3900 ena_update_on_link_change(void *adapter_data, 3901 struct ena_admin_aenq_entry *aenq_e) 3902 { 3903 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 3904 struct ena_admin_aenq_link_change_desc *aenq_desc; 3905 int status; 3906 if_t ifp; 3907 3908 aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e; 3909 ifp = adapter->ifp; 3910 status = aenq_desc->flags & 3911 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK; 3912 3913 if (status != 0) { 3914 device_printf(adapter->pdev, "link is UP\n"); 3915 ifp->if_link_state = LINK_STATE_UP; 3916 if_link_state_change(ifp); 3917 } else if (status == 0) { 3918 device_printf(adapter->pdev, "link is DOWN\n"); 3919 ifp->if_link_state = LINK_STATE_DOWN; 3920 if_link_state_change(ifp); 3921 } else { 3922 device_printf(adapter->pdev, "invalid value recvd\n"); 3923 BUG(); 3924 } 3925 3926 adapter->link_status = status; 3927 } 3928 3929 /** 3930 * This handler will called for unknown event group or unimplemented handlers 3931 **/ 3932 static void 3933 unimplemented_aenq_handler(void *data, 3934 struct ena_admin_aenq_entry *aenq_e) 3935 { 3936 return; 3937 } 3938 3939 static struct ena_aenq_handlers aenq_handlers = { 3940 .handlers = { 3941 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change, 3942 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd, 3943 }, 3944 .unimplemented_handler = unimplemented_aenq_handler 3945 }; 3946 3947 /********************************************************************* 3948 * FreeBSD Device Interface Entry Points 3949 *********************************************************************/ 3950 3951 static device_method_t ena_methods[] = { 3952 /* Device interface */ 3953 DEVMETHOD(device_probe, ena_probe), 3954 DEVMETHOD(device_attach, ena_attach), 3955 DEVMETHOD(device_detach, ena_detach), 3956 DEVMETHOD_END 3957 }; 3958 3959 static driver_t ena_driver = { 3960 "ena", ena_methods, sizeof(struct ena_adapter), 3961 }; 3962 3963 devclass_t ena_devclass; 3964 DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, NULL, NULL); 3965 MODULE_DEPEND(ena, pci, 1, 1, 1); 3966 MODULE_DEPEND(ena, ether, 1, 1, 1); 3967 3968 /*********************************************************************/ 3969