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