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