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