if_ena.c revision 1.9 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.9 2018/11/28 21:31:32 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_enque",
645 ena_deferred_mq_start, tx_ring, 0, IPL_NET, 0);
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 completion",
852 ena_deferred_rx_cleanup, rx_ring, 0, IPL_NET, 0);
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 workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task, NULL);
1426
1427 return (work_done);
1428 }
1429
1430 #if 0
1431 static void
1432 ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
1433 struct mbuf *mbuf)
1434 {
1435 struct ena_adapter *adapter = rx_ring->adapter;
1436
1437 if (likely(adapter->rss_support)) {
1438 mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
1439
1440 if (ena_rx_ctx->frag &&
1441 (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
1442 M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
1443 return;
1444 }
1445
1446 switch (ena_rx_ctx->l3_proto) {
1447 case ENA_ETH_IO_L3_PROTO_IPV4:
1448 switch (ena_rx_ctx->l4_proto) {
1449 case ENA_ETH_IO_L4_PROTO_TCP:
1450 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
1451 break;
1452 case ENA_ETH_IO_L4_PROTO_UDP:
1453 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
1454 break;
1455 default:
1456 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
1457 }
1458 break;
1459 case ENA_ETH_IO_L3_PROTO_IPV6:
1460 switch (ena_rx_ctx->l4_proto) {
1461 case ENA_ETH_IO_L4_PROTO_TCP:
1462 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
1463 break;
1464 case ENA_ETH_IO_L4_PROTO_UDP:
1465 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
1466 break;
1467 default:
1468 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
1469 }
1470 break;
1471 case ENA_ETH_IO_L3_PROTO_UNKNOWN:
1472 M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
1473 break;
1474 default:
1475 M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
1476 }
1477 } else {
1478 mbuf->m_pkthdr.flowid = rx_ring->qid;
1479 M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
1480 }
1481 }
1482 #endif
1483
1484 /**
1485 * ena_rx_mbuf - assemble mbuf from descriptors
1486 * @rx_ring: ring for which we want to clean packets
1487 * @ena_bufs: buffer info
1488 * @ena_rx_ctx: metadata for this packet(s)
1489 * @next_to_clean: ring pointer, will be updated only upon success
1490 *
1491 **/
1492 static struct mbuf*
1493 ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
1494 struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
1495 {
1496 struct mbuf *mbuf;
1497 struct ena_rx_buffer *rx_info;
1498 struct ena_adapter *adapter;
1499 unsigned int descs = ena_rx_ctx->descs;
1500 uint16_t ntc, len, req_id, buf = 0;
1501
1502 ntc = *next_to_clean;
1503 adapter = rx_ring->adapter;
1504 rx_info = &rx_ring->rx_buffer_info[ntc];
1505
1506 if (unlikely(rx_info->mbuf == NULL)) {
1507 device_printf(adapter->pdev, "NULL mbuf in rx_info");
1508 return (NULL);
1509 }
1510
1511 len = ena_bufs[buf].len;
1512 req_id = ena_bufs[buf].req_id;
1513 rx_info = &rx_ring->rx_buffer_info[req_id];
1514
1515 ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
1516 rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
1517
1518 mbuf = rx_info->mbuf;
1519 KASSERT(mbuf->m_flags & M_PKTHDR);
1520 mbuf->m_pkthdr.len = len;
1521 mbuf->m_len = len;
1522 m_set_rcvif(mbuf, rx_ring->que->adapter->ifp);
1523
1524 /* Fill mbuf with hash key and it's interpretation for optimization */
1525 #if 0
1526 ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
1527 #endif
1528
1529 ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
1530 mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
1531
1532 /* DMA address is not needed anymore, unmap it */
1533 bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
1534
1535 rx_info->mbuf = NULL;
1536 rx_ring->free_rx_ids[ntc] = req_id;
1537 ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
1538
1539 /*
1540 * While we have more than 1 descriptors for one rcvd packet, append
1541 * other mbufs to the main one
1542 */
1543 while (--descs) {
1544 ++buf;
1545 len = ena_bufs[buf].len;
1546 req_id = ena_bufs[buf].req_id;
1547 rx_info = &rx_ring->rx_buffer_info[req_id];
1548
1549 if (unlikely(rx_info->mbuf == NULL)) {
1550 device_printf(adapter->pdev, "NULL mbuf in rx_info");
1551 /*
1552 * If one of the required mbufs was not allocated yet,
1553 * we can break there.
1554 * All earlier used descriptors will be reallocated
1555 * later and not used mbufs can be reused.
1556 * The next_to_clean pointer will not be updated in case
1557 * of an error, so caller should advance it manually
1558 * in error handling routine to keep it up to date
1559 * with hw ring.
1560 */
1561 m_freem(mbuf);
1562 return (NULL);
1563 }
1564
1565 if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
1566 counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
1567 ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
1568 mbuf);
1569 }
1570
1571 ena_trace(ENA_DBG | ENA_RXPTH,
1572 "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
1573
1574 /* Free already appended mbuf, it won't be useful anymore */
1575 bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
1576 m_freem(rx_info->mbuf);
1577 rx_info->mbuf = NULL;
1578
1579 rx_ring->free_rx_ids[ntc] = req_id;
1580 ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
1581 }
1582
1583 *next_to_clean = ntc;
1584
1585 return (mbuf);
1586 }
1587
1588 /**
1589 * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
1590 **/
1591 static inline void
1592 ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
1593 struct mbuf *mbuf)
1594 {
1595
1596 /* IPv4 */
1597 if ((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4)) {
1598 mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1599 if (ena_rx_ctx->l3_csum_err) {
1600 /* ipv4 checksum error */
1601 mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1602 counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
1603 ena_trace(ENA_DBG, "RX IPv4 header checksum error");
1604 return;
1605 }
1606
1607 /* TCP/UDP */
1608 if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1609 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
1610 mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv4 : M_CSUM_UDPv4;
1611 if (ena_rx_ctx->l4_csum_err) {
1612 /* TCP/UDP checksum error */
1613 mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1614 counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
1615 ena_trace(ENA_DBG, "RX L4 checksum error");
1616 }
1617 }
1618 }
1619 /* IPv6 */
1620 else if ((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6)) {
1621 /* TCP/UDP */
1622 if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1623 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
1624 mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv6 : M_CSUM_UDPv6;
1625 if (ena_rx_ctx->l4_csum_err) {
1626 /* TCP/UDP checksum error */
1627 mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1628 counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
1629 ena_trace(ENA_DBG, "RX L4 checksum error");
1630 }
1631 }
1632 }
1633 }
1634
1635 static void
1636 ena_deferred_rx_cleanup(struct work *wk, void *arg)
1637 {
1638 struct ena_ring *rx_ring = arg;
1639 int budget = CLEAN_BUDGET;
1640
1641 ENA_RING_MTX_LOCK(rx_ring);
1642 /*
1643 * If deferred task was executed, perform cleanup of all awaiting
1644 * descs (or until given budget is depleted to avoid infinite loop).
1645 */
1646 while (likely(budget--)) {
1647 if (ena_rx_cleanup(rx_ring) == 0)
1648 break;
1649 }
1650 ENA_RING_MTX_UNLOCK(rx_ring);
1651 }
1652
1653 /**
1654 * ena_rx_cleanup - handle rx irq
1655 * @arg: ring for which irq is being handled
1656 **/
1657 static int
1658 ena_rx_cleanup(struct ena_ring *rx_ring)
1659 {
1660 struct ena_adapter *adapter;
1661 struct mbuf *mbuf;
1662 struct ena_com_rx_ctx ena_rx_ctx;
1663 struct ena_com_io_cq* io_cq;
1664 struct ena_com_io_sq* io_sq;
1665 struct ifnet *ifp;
1666 uint16_t ena_qid;
1667 uint16_t next_to_clean;
1668 uint32_t refill_required;
1669 uint32_t refill_threshold;
1670 #ifdef LRO
1671 uint32_t do_if_input = 0;
1672 #endif
1673 unsigned int qid;
1674 int rc, i;
1675 int budget = RX_BUDGET;
1676
1677 adapter = rx_ring->que->adapter;
1678 ifp = adapter->ifp;
1679 qid = rx_ring->que->id;
1680 ena_qid = ENA_IO_RXQ_IDX(qid);
1681 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
1682 io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
1683 next_to_clean = rx_ring->next_to_clean;
1684
1685 ena_trace(ENA_DBG, "rx: qid %d", qid);
1686
1687 do {
1688 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1689 ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
1690 ena_rx_ctx.descs = 0;
1691 rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
1692
1693 if (unlikely(rc != 0))
1694 goto error;
1695
1696 if (unlikely(ena_rx_ctx.descs == 0))
1697 break;
1698
1699 ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
1700 "descs #: %d l3 proto %d l4 proto %d hash: %x",
1701 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
1702 ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
1703
1704 /* Receive mbuf from the ring */
1705 mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
1706 &ena_rx_ctx, &next_to_clean);
1707
1708 /* Exit if we failed to retrieve a buffer */
1709 if (unlikely(mbuf == NULL)) {
1710 for (i = 0; i < ena_rx_ctx.descs; ++i) {
1711 rx_ring->free_rx_ids[next_to_clean] =
1712 rx_ring->ena_bufs[i].req_id;
1713 next_to_clean =
1714 ENA_RX_RING_IDX_NEXT(next_to_clean,
1715 rx_ring->ring_size);
1716
1717 }
1718 break;
1719 }
1720
1721 if (((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0) ||
1722 ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0) ||
1723 ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0) ||
1724 ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0) ||
1725 ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)) {
1726 ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
1727 }
1728
1729 counter_enter();
1730 counter_u64_add_protected(rx_ring->rx_stats.bytes,
1731 mbuf->m_pkthdr.len);
1732 counter_u64_add_protected(adapter->hw_stats.rx_bytes,
1733 mbuf->m_pkthdr.len);
1734 counter_exit();
1735 #ifdef LRO
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 if (((ifp->if_capenable & IFCAP_LRO) != 0) &&
1742 ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
1743 (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
1744 /*
1745 * Send to the stack if:
1746 * - LRO not enabled, or
1747 * - no LRO resources, or
1748 * - lro enqueue fails
1749 */
1750 if ((rx_ring->lro.lro_cnt != 0) &&
1751 (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
1752 do_if_input = 0;
1753 }
1754 if (do_if_input != 0) {
1755 ena_trace(ENA_DBG | ENA_RXPTH,
1756 "calling if_input() with mbuf %p", mbuf);
1757 (*ifp->if_input)(ifp, mbuf);
1758 }
1759 #endif
1760
1761 counter_enter();
1762 counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
1763 counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
1764 counter_exit();
1765 } while (--budget);
1766
1767 rx_ring->next_to_clean = next_to_clean;
1768
1769 refill_required = ena_com_free_desc(io_sq);
1770 refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
1771
1772 if (refill_required > refill_threshold) {
1773 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1774 ena_refill_rx_bufs(rx_ring, refill_required);
1775 }
1776
1777 #ifdef LRO
1778 tcp_lro_flush_all(&rx_ring->lro);
1779 #endif
1780
1781 return (RX_BUDGET - budget);
1782
1783 error:
1784 counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
1785 return (RX_BUDGET - budget);
1786 }
1787
1788 /*********************************************************************
1789 *
1790 * MSIX & Interrupt Service routine
1791 *
1792 **********************************************************************/
1793
1794 /**
1795 * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
1796 * @arg: interrupt number
1797 **/
1798 static int
1799 ena_intr_msix_mgmnt(void *arg)
1800 {
1801 struct ena_adapter *adapter = (struct ena_adapter *)arg;
1802
1803 ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1804 if (likely(adapter->running))
1805 ena_com_aenq_intr_handler(adapter->ena_dev, arg);
1806
1807 return 1;
1808 }
1809
1810 /**
1811 * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
1812 * @arg: interrupt number
1813 **/
1814 static int
1815 ena_handle_msix(void *arg)
1816 {
1817 struct ena_que *que = arg;
1818 struct ena_adapter *adapter = que->adapter;
1819 struct ifnet *ifp = adapter->ifp;
1820 struct ena_ring *tx_ring;
1821 struct ena_ring *rx_ring;
1822 struct ena_com_io_cq* io_cq;
1823 struct ena_eth_io_intr_reg intr_reg;
1824 int qid, ena_qid;
1825 int txc, rxc, i;
1826
1827 if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
1828 return 0;
1829
1830 ena_trace(ENA_DBG, "MSI-X TX/RX routine");
1831
1832 tx_ring = que->tx_ring;
1833 rx_ring = que->rx_ring;
1834 qid = que->id;
1835 ena_qid = ENA_IO_TXQ_IDX(qid);
1836 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
1837
1838 for (i = 0; i < CLEAN_BUDGET; ++i) {
1839 /*
1840 * If lock cannot be acquired, then deferred cleanup task was
1841 * being executed and rx ring is being cleaned up in
1842 * another thread.
1843 */
1844 if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) {
1845 rxc = ena_rx_cleanup(rx_ring);
1846 ENA_RING_MTX_UNLOCK(rx_ring);
1847 } else {
1848 rxc = 0;
1849 }
1850
1851 /* Protection from calling ena_tx_cleanup from ena_start_xmit */
1852 ENA_RING_MTX_LOCK(tx_ring);
1853 txc = ena_tx_cleanup(tx_ring);
1854 ENA_RING_MTX_UNLOCK(tx_ring);
1855
1856 if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
1857 return 0;
1858
1859 if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
1860 break;
1861 }
1862
1863 /* Signal that work is done and unmask interrupt */
1864 ena_com_update_intr_reg(&intr_reg,
1865 RX_IRQ_INTERVAL,
1866 TX_IRQ_INTERVAL,
1867 true);
1868 ena_com_unmask_intr(io_cq, &intr_reg);
1869
1870 return 1;
1871 }
1872
1873 static int
1874 ena_enable_msix(struct ena_adapter *adapter)
1875 {
1876 int msix_req;
1877 int counts[PCI_INTR_TYPE_SIZE];
1878 int max_type;
1879
1880 /* Reserved the max msix vectors we might need */
1881 msix_req = ENA_MAX_MSIX_VEC(adapter->num_queues);
1882
1883 counts[PCI_INTR_TYPE_INTX] = 0;
1884 counts[PCI_INTR_TYPE_MSI] = 0;
1885 counts[PCI_INTR_TYPE_MSIX] = msix_req;
1886 max_type = PCI_INTR_TYPE_MSIX;
1887
1888 if (pci_intr_alloc(&adapter->sc_pa, &adapter->sc_intrs, counts,
1889 max_type) != 0) {
1890 aprint_error_dev(adapter->pdev,
1891 "failed to allocate interrupt\n");
1892 return ENOSPC;
1893 }
1894
1895 adapter->sc_nintrs = counts[PCI_INTR_TYPE_MSIX];
1896
1897 if (counts[PCI_INTR_TYPE_MSIX] != msix_req) {
1898 device_printf(adapter->pdev,
1899 "Enable only %d MSI-x (out of %d), reduce "
1900 "the number of queues\n", adapter->sc_nintrs, msix_req);
1901 adapter->num_queues = adapter->sc_nintrs - ENA_ADMIN_MSIX_VEC;
1902 }
1903
1904 return 0;
1905 }
1906
1907 #if 0
1908 static void
1909 ena_setup_io_intr(struct ena_adapter *adapter)
1910 {
1911 static int last_bind_cpu = -1;
1912 int irq_idx;
1913
1914 for (int i = 0; i < adapter->num_queues; i++) {
1915 irq_idx = ENA_IO_IRQ_IDX(i);
1916
1917 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
1918 "%s-TxRx-%d", device_xname(adapter->pdev), i);
1919 adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
1920 adapter->irq_tbl[irq_idx].data = &adapter->que[i];
1921 adapter->irq_tbl[irq_idx].vector =
1922 adapter->msix_entries[irq_idx].vector;
1923 ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
1924 adapter->msix_entries[irq_idx].vector);
1925 #ifdef RSS
1926 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
1927 rss_getcpu(i % rss_getnumbuckets());
1928 #else
1929 /*
1930 * We still want to bind rings to the corresponding cpu
1931 * using something similar to the RSS round-robin technique.
1932 */
1933 if (unlikely(last_bind_cpu < 0))
1934 last_bind_cpu = CPU_FIRST();
1935 adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
1936 last_bind_cpu;
1937 last_bind_cpu = CPU_NEXT(last_bind_cpu);
1938 #endif
1939 }
1940 }
1941 #endif
1942
1943 static int
1944 ena_request_mgmnt_irq(struct ena_adapter *adapter)
1945 {
1946 const char *intrstr;
1947 char intrbuf[PCI_INTRSTR_LEN];
1948 char intr_xname[INTRDEVNAMEBUF];
1949 pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
1950 const int irq_slot = ENA_MGMNT_IRQ_IDX;
1951
1952 KASSERT(adapter->sc_intrs != NULL);
1953 KASSERT(adapter->sc_ihs[irq_slot] == NULL);
1954
1955 snprintf(intr_xname, sizeof(intr_xname), "%s mgmnt",
1956 device_xname(adapter->pdev));
1957 intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
1958 intrbuf, sizeof(intrbuf));
1959
1960 adapter->sc_ihs[irq_slot] = pci_intr_establish_xname(
1961 pc, adapter->sc_intrs[irq_slot],
1962 IPL_NET, ena_intr_msix_mgmnt, adapter, intr_xname);
1963
1964 if (adapter->sc_ihs[irq_slot] == NULL) {
1965 device_printf(adapter->pdev, "failed to register "
1966 "interrupt handler for MGMNT irq %s\n",
1967 intrstr);
1968 return ENOMEM;
1969 }
1970
1971 aprint_normal_dev(adapter->pdev,
1972 "for MGMNT interrupting at %s\n", intrstr);
1973
1974 return 0;
1975 }
1976
1977 static int
1978 ena_request_io_irq(struct ena_adapter *adapter)
1979 {
1980 const char *intrstr;
1981 char intrbuf[PCI_INTRSTR_LEN];
1982 char intr_xname[INTRDEVNAMEBUF];
1983 pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
1984 const int irq_off = ENA_IO_IRQ_FIRST_IDX;
1985 void *vih;
1986 kcpuset_t *affinity;
1987 int i;
1988
1989 KASSERT(adapter->sc_intrs != NULL);
1990
1991 kcpuset_create(&affinity, false);
1992
1993 for (i = 0; i < adapter->num_queues; i++) {
1994 int irq_slot = i + irq_off;
1995 int affinity_to = (irq_slot) % ncpu;
1996
1997 KASSERT((void *)adapter->sc_intrs[irq_slot] != NULL);
1998 KASSERT(adapter->sc_ihs[irq_slot] == NULL);
1999
2000 snprintf(intr_xname, sizeof(intr_xname), "%s ioq%d",
2001 device_xname(adapter->pdev), i);
2002 intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
2003 intrbuf, sizeof(intrbuf));
2004
2005 vih = pci_intr_establish_xname(adapter->sc_pa.pa_pc,
2006 adapter->sc_intrs[irq_slot], IPL_NET,
2007 ena_handle_msix, &adapter->que[i], intr_xname);
2008
2009 if (adapter->sc_ihs[ENA_MGMNT_IRQ_IDX] == NULL) {
2010 device_printf(adapter->pdev, "failed to register "
2011 "interrupt handler for IO queue %d irq %s\n",
2012 i, intrstr);
2013 goto err;
2014 }
2015
2016 kcpuset_zero(affinity);
2017 /* Round-robin affinity */
2018 kcpuset_set(affinity, affinity_to);
2019 int error = interrupt_distribute(vih, affinity, NULL);
2020 if (error == 0) {
2021 aprint_normal_dev(adapter->pdev,
2022 "for IO queue %d interrupting at %s"
2023 " affinity to %u\n", i, intrstr, affinity_to);
2024 } else {
2025 aprint_normal_dev(adapter->pdev,
2026 "for IO queue %d interrupting at %s\n", i, intrstr);
2027 }
2028
2029 adapter->sc_ihs[irq_slot] = vih;
2030
2031 #ifdef RSS
2032 ena_trace(ENA_INFO, "queue %d - RSS bucket %d\n",
2033 i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
2034 #else
2035 ena_trace(ENA_INFO, "queue %d - cpu %d\n",
2036 i - ENA_IO_IRQ_FIRST_IDX, affinity_to);
2037 #endif
2038 }
2039
2040 kcpuset_destroy(affinity);
2041 return 0;
2042
2043 err:
2044 kcpuset_destroy(affinity);
2045
2046 for (i--; i >= 0; i--) {
2047 int irq_slot = i + irq_off;
2048 KASSERT(adapter->sc_ihs[irq_slot] != NULL);
2049 pci_intr_disestablish(adapter->sc_pa.pa_pc, adapter->sc_ihs[i]);
2050 adapter->sc_ihs[i] = NULL;
2051 }
2052
2053 return ENOSPC;
2054 }
2055
2056 static void
2057 ena_free_mgmnt_irq(struct ena_adapter *adapter)
2058 {
2059 const int irq_slot = ENA_MGMNT_IRQ_IDX;
2060
2061 if (adapter->sc_ihs[irq_slot]) {
2062 pci_intr_disestablish(adapter->sc_pa.pa_pc,
2063 adapter->sc_ihs[irq_slot]);
2064 adapter->sc_ihs[irq_slot] = NULL;
2065 }
2066 }
2067
2068 static void
2069 ena_free_io_irq(struct ena_adapter *adapter)
2070 {
2071 const int irq_off = ENA_IO_IRQ_FIRST_IDX;
2072
2073 for (int i = 0; i < adapter->num_queues; i++) {
2074 int irq_slot = i + irq_off;
2075
2076 if (adapter->sc_ihs[irq_slot]) {
2077 pci_intr_disestablish(adapter->sc_pa.pa_pc,
2078 adapter->sc_ihs[i]);
2079 adapter->sc_ihs[i] = NULL;
2080 }
2081 }
2082 }
2083
2084 static void
2085 ena_free_irqs(struct ena_adapter* adapter)
2086 {
2087
2088 ena_free_io_irq(adapter);
2089 ena_free_mgmnt_irq(adapter);
2090 ena_disable_msix(adapter);
2091 }
2092
2093 static void
2094 ena_disable_msix(struct ena_adapter *adapter)
2095 {
2096 pci_intr_release(adapter->sc_pa.pa_pc, adapter->sc_intrs,
2097 adapter->sc_nintrs);
2098 }
2099
2100 static void
2101 ena_unmask_all_io_irqs(struct ena_adapter *adapter)
2102 {
2103 struct ena_com_io_cq* io_cq;
2104 struct ena_eth_io_intr_reg intr_reg;
2105 uint16_t ena_qid;
2106 int i;
2107
2108 /* Unmask interrupts for all queues */
2109 for (i = 0; i < adapter->num_queues; i++) {
2110 ena_qid = ENA_IO_TXQ_IDX(i);
2111 io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
2112 ena_com_update_intr_reg(&intr_reg, 0, 0, true);
2113 ena_com_unmask_intr(io_cq, &intr_reg);
2114 }
2115 }
2116
2117 /* Configure the Rx forwarding */
2118 static int
2119 ena_rss_configure(struct ena_adapter *adapter)
2120 {
2121 struct ena_com_dev *ena_dev = adapter->ena_dev;
2122 int rc;
2123
2124 /* Set indirect table */
2125 rc = ena_com_indirect_table_set(ena_dev);
2126 if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
2127 return (rc);
2128
2129 /* Configure hash function (if supported) */
2130 rc = ena_com_set_hash_function(ena_dev);
2131 if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
2132 return (rc);
2133
2134 /* Configure hash inputs (if supported) */
2135 rc = ena_com_set_hash_ctrl(ena_dev);
2136 if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
2137 return (rc);
2138
2139 return (0);
2140 }
2141
2142 static int
2143 ena_up_complete(struct ena_adapter *adapter)
2144 {
2145 int rc;
2146
2147 if (likely(adapter->rss_support)) {
2148 rc = ena_rss_configure(adapter);
2149 if (rc != 0)
2150 return (rc);
2151 }
2152
2153 rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
2154 if (unlikely(rc != 0))
2155 return (rc);
2156
2157 ena_refill_all_rx_bufs(adapter);
2158 ena_reset_counters((struct evcnt *)&adapter->hw_stats,
2159 sizeof(adapter->hw_stats));
2160
2161 return (0);
2162 }
2163
2164 static int
2165 ena_up(struct ena_adapter *adapter)
2166 {
2167 int rc = 0;
2168
2169 #if 0
2170 if (unlikely(device_is_attached(adapter->pdev) == 0)) {
2171 device_printf(adapter->pdev, "device is not attached!\n");
2172 return (ENXIO);
2173 }
2174 #endif
2175
2176 if (unlikely(!adapter->running)) {
2177 device_printf(adapter->pdev, "device is not running!\n");
2178 return (ENXIO);
2179 }
2180
2181 if (!adapter->up) {
2182 device_printf(adapter->pdev, "device is going UP\n");
2183
2184 /* setup interrupts for IO queues */
2185 rc = ena_request_io_irq(adapter);
2186 if (unlikely(rc != 0)) {
2187 ena_trace(ENA_ALERT, "err_req_irq");
2188 goto err_req_irq;
2189 }
2190
2191 /* allocate transmit descriptors */
2192 rc = ena_setup_all_tx_resources(adapter);
2193 if (unlikely(rc != 0)) {
2194 ena_trace(ENA_ALERT, "err_setup_tx");
2195 goto err_setup_tx;
2196 }
2197
2198 /* allocate receive descriptors */
2199 rc = ena_setup_all_rx_resources(adapter);
2200 if (unlikely(rc != 0)) {
2201 ena_trace(ENA_ALERT, "err_setup_rx");
2202 goto err_setup_rx;
2203 }
2204
2205 /* create IO queues for Rx & Tx */
2206 rc = ena_create_io_queues(adapter);
2207 if (unlikely(rc != 0)) {
2208 ena_trace(ENA_ALERT,
2209 "create IO queues failed");
2210 goto err_io_que;
2211 }
2212
2213 if (unlikely(adapter->link_status))
2214 if_link_state_change(adapter->ifp, LINK_STATE_UP);
2215
2216 rc = ena_up_complete(adapter);
2217 if (unlikely(rc != 0))
2218 goto err_up_complete;
2219
2220 counter_u64_add(adapter->dev_stats.interface_up, 1);
2221
2222 ena_update_hwassist(adapter);
2223
2224 if_setdrvflagbits(adapter->ifp, IFF_RUNNING,
2225 IFF_OACTIVE);
2226
2227 callout_reset(&adapter->timer_service, hz,
2228 ena_timer_service, (void *)adapter);
2229
2230 adapter->up = true;
2231
2232 ena_unmask_all_io_irqs(adapter);
2233 }
2234
2235 return (0);
2236
2237 err_up_complete:
2238 ena_destroy_all_io_queues(adapter);
2239 err_io_que:
2240 ena_free_all_rx_resources(adapter);
2241 err_setup_rx:
2242 ena_free_all_tx_resources(adapter);
2243 err_setup_tx:
2244 ena_free_io_irq(adapter);
2245 err_req_irq:
2246 return (rc);
2247 }
2248
2249 #if 0
2250 static uint64_t
2251 ena_get_counter(struct ifnet *ifp, ift_counter cnt)
2252 {
2253 struct ena_adapter *adapter;
2254 struct ena_hw_stats *stats;
2255
2256 adapter = if_getsoftc(ifp);
2257 stats = &adapter->hw_stats;
2258
2259 switch (cnt) {
2260 case IFCOUNTER_IPACKETS:
2261 return (counter_u64_fetch(stats->rx_packets));
2262 case IFCOUNTER_OPACKETS:
2263 return (counter_u64_fetch(stats->tx_packets));
2264 case IFCOUNTER_IBYTES:
2265 return (counter_u64_fetch(stats->rx_bytes));
2266 case IFCOUNTER_OBYTES:
2267 return (counter_u64_fetch(stats->tx_bytes));
2268 case IFCOUNTER_IQDROPS:
2269 return (counter_u64_fetch(stats->rx_drops));
2270 default:
2271 return (if_get_counter_default(ifp, cnt));
2272 }
2273 }
2274 #endif
2275
2276 static int
2277 ena_media_change(struct ifnet *ifp)
2278 {
2279 /* Media Change is not supported by firmware */
2280 return (0);
2281 }
2282
2283 static void
2284 ena_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
2285 {
2286 struct ena_adapter *adapter = if_getsoftc(ifp);
2287 ena_trace(ENA_DBG, "enter");
2288
2289 mutex_enter(&adapter->global_mtx);
2290
2291 ifmr->ifm_status = IFM_AVALID;
2292 ifmr->ifm_active = IFM_ETHER;
2293
2294 if (!adapter->link_status) {
2295 mutex_exit(&adapter->global_mtx);
2296 ena_trace(ENA_INFO, "link_status = false");
2297 return;
2298 }
2299
2300 ifmr->ifm_status |= IFM_ACTIVE;
2301 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
2302
2303 mutex_exit(&adapter->global_mtx);
2304 }
2305
2306 static int
2307 ena_init(struct ifnet *ifp)
2308 {
2309 struct ena_adapter *adapter = if_getsoftc(ifp);
2310
2311 if (!adapter->up) {
2312 rw_enter(&adapter->ioctl_sx, RW_WRITER);
2313 ena_up(adapter);
2314 rw_exit(&adapter->ioctl_sx);
2315 }
2316
2317 return 0;
2318 }
2319
2320 static int
2321 ena_ioctl(struct ifnet *ifp, u_long command, void *data)
2322 {
2323 struct ena_adapter *adapter;
2324 struct ifreq *ifr;
2325 int rc;
2326
2327 adapter = ifp->if_softc;
2328 ifr = (struct ifreq *)data;
2329
2330 /*
2331 * Acquiring lock to prevent from running up and down routines parallel.
2332 */
2333 rc = 0;
2334 switch (command) {
2335 case SIOCSIFMTU:
2336 if (ifp->if_mtu == ifr->ifr_mtu)
2337 break;
2338 rw_enter(&adapter->ioctl_sx, RW_WRITER);
2339 ena_down(adapter);
2340
2341 ena_change_mtu(ifp, ifr->ifr_mtu);
2342
2343 rc = ena_up(adapter);
2344 rw_exit(&adapter->ioctl_sx);
2345 break;
2346
2347 case SIOCSIFFLAGS:
2348 if ((ifp->if_flags & IFF_UP) != 0) {
2349 if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
2350 if ((ifp->if_flags & (IFF_PROMISC |
2351 IFF_ALLMULTI)) != 0) {
2352 device_printf(adapter->pdev,
2353 "ioctl promisc/allmulti\n");
2354 }
2355 } else {
2356 rw_enter(&adapter->ioctl_sx, RW_WRITER);
2357 rc = ena_up(adapter);
2358 rw_exit(&adapter->ioctl_sx);
2359 }
2360 } else {
2361 if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
2362 rw_enter(&adapter->ioctl_sx, RW_WRITER);
2363 ena_down(adapter);
2364 rw_exit(&adapter->ioctl_sx);
2365 }
2366 }
2367 break;
2368
2369 case SIOCADDMULTI:
2370 case SIOCDELMULTI:
2371 break;
2372
2373 case SIOCSIFMEDIA:
2374 case SIOCGIFMEDIA:
2375 rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
2376 break;
2377
2378 case SIOCSIFCAP:
2379 {
2380 struct ifcapreq *ifcr = data;
2381 int reinit = 0;
2382
2383 if (ifcr->ifcr_capenable != ifp->if_capenable) {
2384 ifp->if_capenable = ifcr->ifcr_capenable;
2385 reinit = 1;
2386 }
2387
2388 if ((reinit != 0) &&
2389 ((if_getdrvflags(ifp) & IFF_RUNNING) != 0)) {
2390 rw_enter(&adapter->ioctl_sx, RW_WRITER);
2391 ena_down(adapter);
2392 rc = ena_up(adapter);
2393 rw_exit(&adapter->ioctl_sx);
2394 }
2395 }
2396
2397 break;
2398 default:
2399 rc = ether_ioctl(ifp, command, data);
2400 break;
2401 }
2402
2403 return (rc);
2404 }
2405
2406 static int
2407 ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
2408 {
2409 int caps = 0;
2410
2411 if ((feat->offload.tx &
2412 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
2413 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
2414 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
2415 caps |= IFCAP_CSUM_IPv4_Tx;
2416
2417 if ((feat->offload.tx &
2418 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
2419 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
2420 caps |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
2421
2422 if ((feat->offload.tx &
2423 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
2424 caps |= IFCAP_TSOv4;
2425
2426 if ((feat->offload.tx &
2427 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
2428 caps |= IFCAP_TSOv6;
2429
2430 if ((feat->offload.rx_supported &
2431 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
2432 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
2433 caps |= IFCAP_CSUM_IPv4_Rx;
2434
2435 if ((feat->offload.rx_supported &
2436 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
2437 caps |= IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
2438
2439 caps |= IFCAP_LRO;
2440
2441 return (caps);
2442 }
2443
2444 static void
2445 ena_update_host_info(struct ena_admin_host_info *host_info, struct ifnet *ifp)
2446 {
2447
2448 host_info->supported_network_features[0] =
2449 (uint32_t)if_getcapabilities(ifp);
2450 }
2451
2452 static void
2453 ena_update_hwassist(struct ena_adapter *adapter)
2454 {
2455 struct ifnet *ifp = adapter->ifp;
2456 uint32_t feat = adapter->tx_offload_cap;
2457 int cap = if_getcapenable(ifp);
2458 int flags = 0;
2459
2460 if_clearhwassist(ifp);
2461
2462 if ((cap & (IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
2463 != 0) {
2464 if ((feat &
2465 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
2466 flags |= M_CSUM_IPv4;
2467 if ((feat &
2468 (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
2469 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
2470 flags |= M_CSUM_TCPv4 | M_CSUM_UDPv4;
2471 }
2472
2473 if ((cap & IFCAP_CSUM_TCPv6_Tx) != 0)
2474 flags |= M_CSUM_TCPv6;
2475
2476 if ((cap & IFCAP_CSUM_UDPv6_Tx) != 0)
2477 flags |= M_CSUM_UDPv6;
2478
2479 if ((cap & IFCAP_TSOv4) != 0)
2480 flags |= M_CSUM_TSOv4;
2481
2482 if ((cap & IFCAP_TSOv6) != 0)
2483 flags |= M_CSUM_TSOv6;
2484
2485 if_sethwassistbits(ifp, flags, 0);
2486 }
2487
2488 static int
2489 ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
2490 struct ena_com_dev_get_features_ctx *feat)
2491 {
2492 struct ifnet *ifp;
2493 int caps = 0;
2494
2495 ifp = adapter->ifp = &adapter->sc_ec.ec_if;
2496 if (unlikely(ifp == NULL)) {
2497 ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
2498 return (ENXIO);
2499 }
2500 if_initname(ifp, device_xname(pdev), device_unit(pdev));
2501 if_setdev(ifp, pdev);
2502 if_setsoftc(ifp, adapter);
2503
2504 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2505 if_setinitfn(ifp, ena_init);
2506 if_settransmitfn(ifp, ena_mq_start);
2507 #if 0
2508 if_setqflushfn(ifp, ena_qflush);
2509 #endif
2510 if_setioctlfn(ifp, ena_ioctl);
2511 #if 0
2512 if_setgetcounterfn(ifp, ena_get_counter);
2513 #endif
2514
2515 if_setsendqlen(ifp, adapter->tx_ring_size);
2516 if_setsendqready(ifp);
2517 if_setmtu(ifp, ETHERMTU);
2518 if_setbaudrate(ifp, 0);
2519 /* Zeroize capabilities... */
2520 if_setcapabilities(ifp, 0);
2521 if_setcapenable(ifp, 0);
2522 /* check hardware support */
2523 caps = ena_get_dev_offloads(feat);
2524 /* ... and set them */
2525 if_setcapabilitiesbit(ifp, caps, 0);
2526 adapter->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
2527
2528 #if 0
2529 /* TSO parameters */
2530 /* XXX no limits on NetBSD, guarded by virtue of dmamap load failing */
2531 ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
2532 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
2533 ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
2534 ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
2535 #endif
2536
2537 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
2538 if_setcapenable(ifp, if_getcapabilities(ifp));
2539
2540 /*
2541 * Specify the media types supported by this adapter and register
2542 * callbacks to update media and link information
2543 */
2544 ifmedia_init(&adapter->media, IFM_IMASK,
2545 ena_media_change, ena_media_status);
2546 ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2547 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
2548
2549 if_attach(ifp);
2550 if_deferred_start_init(ifp, NULL);
2551
2552 ether_ifattach(ifp, adapter->mac_addr);
2553
2554 return (0);
2555 }
2556
2557 static void
2558 ena_down(struct ena_adapter *adapter)
2559 {
2560 int rc;
2561
2562 if (adapter->up) {
2563 device_printf(adapter->pdev, "device is going DOWN\n");
2564
2565 callout_halt(&adapter->timer_service, &adapter->global_mtx);
2566
2567 adapter->up = false;
2568 if_setdrvflagbits(adapter->ifp, IFF_OACTIVE,
2569 IFF_RUNNING);
2570
2571 ena_free_io_irq(adapter);
2572
2573 if (adapter->trigger_reset) {
2574 rc = ena_com_dev_reset(adapter->ena_dev,
2575 adapter->reset_reason);
2576 if (unlikely(rc != 0))
2577 device_printf(adapter->pdev,
2578 "Device reset failed\n");
2579 }
2580
2581 ena_destroy_all_io_queues(adapter);
2582
2583 ena_free_all_tx_bufs(adapter);
2584 ena_free_all_rx_bufs(adapter);
2585 ena_free_all_tx_resources(adapter);
2586 ena_free_all_rx_resources(adapter);
2587
2588 counter_u64_add(adapter->dev_stats.interface_down, 1);
2589 }
2590 }
2591
2592 static void
2593 ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
2594 {
2595 struct ena_com_tx_meta *ena_meta;
2596 struct ether_vlan_header *eh;
2597 u32 mss;
2598 bool offload;
2599 uint16_t etype;
2600 int ehdrlen;
2601 struct ip *ip;
2602 int iphlen;
2603 struct tcphdr *th;
2604
2605 offload = false;
2606 ena_meta = &ena_tx_ctx->ena_meta;
2607
2608 #if 0
2609 u32 mss = mbuf->m_pkthdr.tso_segsz;
2610
2611 if (mss != 0)
2612 offload = true;
2613 #else
2614 mss = mbuf->m_pkthdr.len; /* XXX don't have tso_segsz */
2615 #endif
2616
2617 if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0)
2618 offload = true;
2619
2620 if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
2621 offload = true;
2622
2623 if (!offload) {
2624 ena_tx_ctx->meta_valid = 0;
2625 return;
2626 }
2627
2628 /* Determine where frame payload starts. */
2629 eh = mtod(mbuf, struct ether_vlan_header *);
2630 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
2631 etype = ntohs(eh->evl_proto);
2632 ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2633 } else {
2634 etype = htons(eh->evl_encap_proto);
2635 ehdrlen = ETHER_HDR_LEN;
2636 }
2637
2638 ip = (struct ip *)(mbuf->m_data + ehdrlen);
2639 iphlen = ip->ip_hl << 2;
2640 th = (struct tcphdr *)((vaddr_t)ip + iphlen);
2641
2642 if ((mbuf->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
2643 ena_tx_ctx->l3_csum_enable = 1;
2644 }
2645 if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
2646 ena_tx_ctx->tso_enable = 1;
2647 ena_meta->l4_hdr_len = (th->th_off);
2648 }
2649
2650 switch (etype) {
2651 case ETHERTYPE_IP:
2652 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2653 if ((ip->ip_off & htons(IP_DF)) != 0)
2654 ena_tx_ctx->df = 1;
2655 break;
2656 case ETHERTYPE_IPV6:
2657 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
2658
2659 default:
2660 break;
2661 }
2662
2663 if (ip->ip_p == IPPROTO_TCP) {
2664 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
2665 if ((mbuf->m_pkthdr.csum_flags &
2666 (M_CSUM_TCPv4 | M_CSUM_TCPv6)) != 0)
2667 ena_tx_ctx->l4_csum_enable = 1;
2668 else
2669 ena_tx_ctx->l4_csum_enable = 0;
2670 } else if (ip->ip_p == IPPROTO_UDP) {
2671 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
2672 if ((mbuf->m_pkthdr.csum_flags &
2673 (M_CSUM_UDPv4 | M_CSUM_UDPv6)) != 0)
2674 ena_tx_ctx->l4_csum_enable = 1;
2675 else
2676 ena_tx_ctx->l4_csum_enable = 0;
2677 } else {
2678 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
2679 ena_tx_ctx->l4_csum_enable = 0;
2680 }
2681
2682 ena_meta->mss = mss;
2683 ena_meta->l3_hdr_len = iphlen;
2684 ena_meta->l3_hdr_offset = ehdrlen;
2685 ena_tx_ctx->meta_valid = 1;
2686 }
2687
2688 static int
2689 ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
2690 {
2691 struct ena_adapter *adapter;
2692 struct mbuf *collapsed_mbuf;
2693 int num_frags;
2694
2695 adapter = tx_ring->adapter;
2696 num_frags = ena_mbuf_count(*mbuf);
2697
2698 /* One segment must be reserved for configuration descriptor. */
2699 if (num_frags < adapter->max_tx_sgl_size)
2700 return (0);
2701 counter_u64_add(tx_ring->tx_stats.collapse, 1);
2702
2703 collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
2704 adapter->max_tx_sgl_size - 1);
2705 if (unlikely(collapsed_mbuf == NULL)) {
2706 counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
2707 return (ENOMEM);
2708 }
2709
2710 /* If mbuf was collapsed succesfully, original mbuf is released. */
2711 *mbuf = collapsed_mbuf;
2712
2713 return (0);
2714 }
2715
2716 static int
2717 ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
2718 {
2719 struct ena_adapter *adapter;
2720 struct ena_tx_buffer *tx_info;
2721 struct ena_com_tx_ctx ena_tx_ctx;
2722 struct ena_com_dev *ena_dev;
2723 struct ena_com_buf *ena_buf;
2724 struct ena_com_io_sq* io_sq;
2725 void *push_hdr;
2726 uint16_t next_to_use;
2727 uint16_t req_id;
2728 uint16_t ena_qid;
2729 uint32_t header_len;
2730 int i, rc;
2731 int nb_hw_desc;
2732
2733 ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
2734 adapter = tx_ring->que->adapter;
2735 ena_dev = adapter->ena_dev;
2736 io_sq = &ena_dev->io_sq_queues[ena_qid];
2737
2738 rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
2739 if (unlikely(rc != 0)) {
2740 ena_trace(ENA_WARNING,
2741 "Failed to collapse mbuf! err: %d", rc);
2742 return (rc);
2743 }
2744
2745 next_to_use = tx_ring->next_to_use;
2746 req_id = tx_ring->free_tx_ids[next_to_use];
2747 tx_info = &tx_ring->tx_buffer_info[req_id];
2748
2749 tx_info->mbuf = *mbuf;
2750 tx_info->num_of_bufs = 0;
2751
2752 ena_buf = tx_info->bufs;
2753
2754 ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
2755
2756 /*
2757 * header_len is just a hint for the device. Because FreeBSD is not
2758 * giving us information about packet header length and it is not
2759 * guaranteed that all packet headers will be in the 1st mbuf, setting
2760 * header_len to 0 is making the device ignore this value and resolve
2761 * header on it's own.
2762 */
2763 header_len = 0;
2764 push_hdr = NULL;
2765
2766 rc = bus_dmamap_load_mbuf(adapter->sc_dmat, tx_info->map,
2767 *mbuf, BUS_DMA_NOWAIT);
2768
2769 if (unlikely((rc != 0) || (tx_info->map->dm_nsegs == 0))) {
2770 ena_trace(ENA_WARNING,
2771 "dmamap load failed! err: %d nsegs: %d", rc,
2772 tx_info->map->dm_nsegs);
2773 counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
2774 tx_info->mbuf = NULL;
2775 if (rc == ENOMEM)
2776 return (ENA_COM_NO_MEM);
2777 else
2778 return (ENA_COM_INVAL);
2779 }
2780
2781 for (i = 0; i < tx_info->map->dm_nsegs; i++) {
2782 ena_buf->len = tx_info->map->dm_segs[i].ds_len;
2783 ena_buf->paddr = tx_info->map->dm_segs[i].ds_addr;
2784 ena_buf++;
2785 }
2786 tx_info->num_of_bufs = tx_info->map->dm_nsegs;
2787
2788 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
2789 ena_tx_ctx.ena_bufs = tx_info->bufs;
2790 ena_tx_ctx.push_header = push_hdr;
2791 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
2792 ena_tx_ctx.req_id = req_id;
2793 ena_tx_ctx.header_len = header_len;
2794
2795 /* Set flags and meta data */
2796 ena_tx_csum(&ena_tx_ctx, *mbuf);
2797 /* Prepare the packet's descriptors and send them to device */
2798 rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
2799 if (unlikely(rc != 0)) {
2800 device_printf(adapter->pdev, "failed to prepare tx bufs\n");
2801 counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
2802 goto dma_error;
2803 }
2804
2805 counter_enter();
2806 counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
2807 counter_u64_add_protected(tx_ring->tx_stats.bytes,
2808 (*mbuf)->m_pkthdr.len);
2809
2810 counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
2811 counter_u64_add_protected(adapter->hw_stats.tx_bytes,
2812 (*mbuf)->m_pkthdr.len);
2813 counter_exit();
2814
2815 tx_info->tx_descs = nb_hw_desc;
2816 getbinuptime(&tx_info->timestamp);
2817 tx_info->print_once = true;
2818
2819 tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
2820 tx_ring->ring_size);
2821
2822 bus_dmamap_sync(adapter->sc_dmat, tx_info->map, 0,
2823 tx_info->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
2824
2825 return (0);
2826
2827 dma_error:
2828 tx_info->mbuf = NULL;
2829 bus_dmamap_unload(adapter->sc_dmat, tx_info->map);
2830
2831 return (rc);
2832 }
2833
2834 static void
2835 ena_start_xmit(struct ena_ring *tx_ring)
2836 {
2837 struct mbuf *mbuf;
2838 struct ena_adapter *adapter = tx_ring->adapter;
2839 struct ena_com_io_sq* io_sq;
2840 int ena_qid;
2841 int acum_pkts = 0;
2842 int ret = 0;
2843
2844 if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
2845 return;
2846
2847 if (unlikely(!adapter->link_status))
2848 return;
2849
2850 ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
2851 io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
2852
2853 while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
2854 ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
2855 " header csum flags %#jx",
2856 mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
2857
2858 if (unlikely(!ena_com_sq_have_enough_space(io_sq,
2859 ENA_TX_CLEANUP_THRESHOLD)))
2860 ena_tx_cleanup(tx_ring);
2861
2862 if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
2863 if (ret == ENA_COM_NO_MEM) {
2864 drbr_putback(adapter->ifp, tx_ring->br, mbuf);
2865 } else if (ret == ENA_COM_NO_SPACE) {
2866 drbr_putback(adapter->ifp, tx_ring->br, mbuf);
2867 } else {
2868 m_freem(mbuf);
2869 drbr_advance(adapter->ifp, tx_ring->br);
2870 }
2871
2872 break;
2873 }
2874
2875 drbr_advance(adapter->ifp, tx_ring->br);
2876
2877 if (unlikely((if_getdrvflags(adapter->ifp) &
2878 IFF_RUNNING) == 0))
2879 return;
2880
2881 acum_pkts++;
2882
2883 /*
2884 * If there's a BPF listener, bounce a copy of this frame
2885 * to him.
2886 */
2887 bpf_mtap(adapter->ifp, mbuf, BPF_D_OUT);
2888
2889 if (unlikely(acum_pkts == DB_THRESHOLD)) {
2890 acum_pkts = 0;
2891 wmb();
2892 /* Trigger the dma engine */
2893 ena_com_write_sq_doorbell(io_sq);
2894 counter_u64_add(tx_ring->tx_stats.doorbells, 1);
2895 }
2896
2897 }
2898
2899 if (likely(acum_pkts != 0)) {
2900 wmb();
2901 /* Trigger the dma engine */
2902 ena_com_write_sq_doorbell(io_sq);
2903 counter_u64_add(tx_ring->tx_stats.doorbells, 1);
2904 }
2905
2906 if (!ena_com_sq_have_enough_space(io_sq, ENA_TX_CLEANUP_THRESHOLD))
2907 ena_tx_cleanup(tx_ring);
2908 }
2909
2910 static void
2911 ena_deferred_mq_start(struct work *wk, void *arg)
2912 {
2913 struct ena_ring *tx_ring = (struct ena_ring *)arg;
2914 struct ifnet *ifp = tx_ring->adapter->ifp;
2915
2916 while (!drbr_empty(ifp, tx_ring->br) &&
2917 (if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
2918 ENA_RING_MTX_LOCK(tx_ring);
2919 ena_start_xmit(tx_ring);
2920 ENA_RING_MTX_UNLOCK(tx_ring);
2921 }
2922 }
2923
2924 static int
2925 ena_mq_start(struct ifnet *ifp, struct mbuf *m)
2926 {
2927 struct ena_adapter *adapter = ifp->if_softc;
2928 struct ena_ring *tx_ring;
2929 int ret, is_drbr_empty;
2930 uint32_t i;
2931
2932 if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
2933 return (ENODEV);
2934
2935 /* Which queue to use */
2936 /*
2937 * If everything is setup correctly, it should be the
2938 * same bucket that the current CPU we're on is.
2939 * It should improve performance.
2940 */
2941 #if 0
2942 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
2943 #ifdef RSS
2944 if (rss_hash2bucket(m->m_pkthdr.flowid,
2945 M_HASHTYPE_GET(m), &i) == 0) {
2946 i = i % adapter->num_queues;
2947
2948 } else
2949 #endif
2950 {
2951 i = m->m_pkthdr.flowid % adapter->num_queues;
2952 }
2953 } else {
2954 #endif
2955 i = cpu_index(curcpu()) % adapter->num_queues;
2956 #if 0
2957 }
2958 #endif
2959 tx_ring = &adapter->tx_ring[i];
2960
2961 /* Check if drbr is empty before putting packet */
2962 is_drbr_empty = drbr_empty(ifp, tx_ring->br);
2963 ret = drbr_enqueue(ifp, tx_ring->br, m);
2964 if (unlikely(ret != 0)) {
2965 workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
2966 curcpu());
2967 return (ret);
2968 }
2969
2970 if ((is_drbr_empty != 0) && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
2971 ena_start_xmit(tx_ring);
2972 ENA_RING_MTX_UNLOCK(tx_ring);
2973 } else {
2974 workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
2975 curcpu());
2976 }
2977
2978 return (0);
2979 }
2980
2981 #if 0
2982 static void
2983 ena_qflush(struct ifnet *ifp)
2984 {
2985 struct ena_adapter *adapter = ifp->if_softc;
2986 struct ena_ring *tx_ring = adapter->tx_ring;
2987 int i;
2988
2989 for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
2990 if (!drbr_empty(ifp, tx_ring->br)) {
2991 ENA_RING_MTX_LOCK(tx_ring);
2992 drbr_flush(ifp, tx_ring->br);
2993 ENA_RING_MTX_UNLOCK(tx_ring);
2994 }
2995
2996 if_qflush(ifp);
2997 }
2998 #endif
2999
3000 static int
3001 ena_calc_io_queue_num(struct pci_attach_args *pa,
3002 struct ena_adapter *adapter,
3003 struct ena_com_dev_get_features_ctx *get_feat_ctx)
3004 {
3005 int io_sq_num, io_cq_num, io_queue_num;
3006
3007 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
3008 io_cq_num = get_feat_ctx->max_queues.max_cq_num;
3009
3010 io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
3011 io_queue_num = min_t(int, io_queue_num, io_sq_num);
3012 io_queue_num = min_t(int, io_queue_num, io_cq_num);
3013 /* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
3014 io_queue_num = min_t(int, io_queue_num,
3015 pci_msix_count(pa->pa_pc, pa->pa_tag) - 1);
3016 #ifdef RSS
3017 io_queue_num = min_t(int, io_queue_num, rss_getnumbuckets());
3018 #endif
3019
3020 return (io_queue_num);
3021 }
3022
3023 static int
3024 ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size,
3025 uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat)
3026 {
3027 uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
3028 uint32_t v;
3029 uint32_t q;
3030
3031 queue_size = min_t(uint32_t, queue_size,
3032 feat->max_queues.max_cq_depth);
3033 queue_size = min_t(uint32_t, queue_size,
3034 feat->max_queues.max_sq_depth);
3035
3036 /* round down to the nearest power of 2 */
3037 v = queue_size;
3038 while (v != 0) {
3039 if (powerof2(queue_size) != 0)
3040 break;
3041 v /= 2;
3042 q = rounddown2(queue_size, v);
3043 if (q != 0) {
3044 queue_size = q;
3045 break;
3046 }
3047 }
3048
3049 if (unlikely(queue_size == 0)) {
3050 device_printf(adapter->pdev, "Invalid queue size\n");
3051 return (ENA_COM_FAULT);
3052 }
3053
3054 *max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
3055 feat->max_queues.max_packet_tx_descs);
3056 *max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
3057 feat->max_queues.max_packet_rx_descs);
3058
3059 return (queue_size);
3060 }
3061
3062 #if 0
3063 static int
3064 ena_rss_init_default(struct ena_adapter *adapter)
3065 {
3066 struct ena_com_dev *ena_dev = adapter->ena_dev;
3067 device_t dev = adapter->pdev;
3068 int qid, rc, i;
3069
3070 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
3071 if (unlikely(rc != 0)) {
3072 device_printf(dev, "Cannot init indirect table\n");
3073 return (rc);
3074 }
3075
3076 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
3077 #ifdef RSS
3078 qid = rss_get_indirection_to_bucket(i);
3079 qid = qid % adapter->num_queues;
3080 #else
3081 qid = i % adapter->num_queues;
3082 #endif
3083 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
3084 ENA_IO_RXQ_IDX(qid));
3085 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
3086 device_printf(dev, "Cannot fill indirect table\n");
3087 goto err_rss_destroy;
3088 }
3089 }
3090
3091 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
3092 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
3093 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
3094 device_printf(dev, "Cannot fill hash function\n");
3095 goto err_rss_destroy;
3096 }
3097
3098 rc = ena_com_set_default_hash_ctrl(ena_dev);
3099 if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
3100 device_printf(dev, "Cannot fill hash control\n");
3101 goto err_rss_destroy;
3102 }
3103
3104 return (0);
3105
3106 err_rss_destroy:
3107 ena_com_rss_destroy(ena_dev);
3108 return (rc);
3109 }
3110
3111 static void
3112 ena_rss_init_default_deferred(void *arg)
3113 {
3114 struct ena_adapter *adapter;
3115 devclass_t dc;
3116 int max;
3117 int rc;
3118
3119 dc = devclass_find("ena");
3120 if (unlikely(dc == NULL)) {
3121 ena_trace(ENA_ALERT, "No devclass ena\n");
3122 return;
3123 }
3124
3125 max = devclass_get_maxunit(dc);
3126 while (max-- >= 0) {
3127 adapter = devclass_get_softc(dc, max);
3128 if (adapter != NULL) {
3129 rc = ena_rss_init_default(adapter);
3130 adapter->rss_support = true;
3131 if (unlikely(rc != 0)) {
3132 device_printf(adapter->pdev,
3133 "WARNING: RSS was not properly initialized,"
3134 " it will affect bandwidth\n");
3135 adapter->rss_support = false;
3136 }
3137 }
3138 }
3139 }
3140 SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
3141 #endif
3142
3143 static void
3144 ena_config_host_info(struct ena_com_dev *ena_dev)
3145 {
3146 struct ena_admin_host_info *host_info;
3147 int rc;
3148
3149 /* Allocate only the host info */
3150 rc = ena_com_allocate_host_info(ena_dev);
3151 if (unlikely(rc != 0)) {
3152 ena_trace(ENA_ALERT, "Cannot allocate host info\n");
3153 return;
3154 }
3155
3156 host_info = ena_dev->host_attr.host_info;
3157
3158 host_info->os_type = ENA_ADMIN_OS_FREEBSD;
3159 host_info->kernel_ver = osreldate;
3160
3161 snprintf(host_info->kernel_ver_str, sizeof(host_info->kernel_ver_str),
3162 "%d", osreldate);
3163 host_info->os_dist = 0;
3164 strncpy(host_info->os_dist_str, osrelease,
3165 sizeof(host_info->os_dist_str) - 1);
3166
3167 host_info->driver_version =
3168 (DRV_MODULE_VER_MAJOR) |
3169 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
3170 (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
3171
3172 rc = ena_com_set_host_attributes(ena_dev);
3173 if (unlikely(rc != 0)) {
3174 if (rc == EOPNOTSUPP)
3175 ena_trace(ENA_WARNING, "Cannot set host attributes\n");
3176 else
3177 ena_trace(ENA_ALERT, "Cannot set host attributes\n");
3178
3179 goto err;
3180 }
3181
3182 return;
3183
3184 err:
3185 ena_com_delete_host_info(ena_dev);
3186 }
3187
3188 static int
3189 ena_device_init(struct ena_adapter *adapter, device_t pdev,
3190 struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
3191 {
3192 struct ena_com_dev* ena_dev = adapter->ena_dev;
3193 bool readless_supported;
3194 uint32_t aenq_groups;
3195 int dma_width;
3196 int rc;
3197
3198 rc = ena_com_mmio_reg_read_request_init(ena_dev);
3199 if (unlikely(rc != 0)) {
3200 device_printf(pdev, "failed to init mmio read less\n");
3201 return (rc);
3202 }
3203
3204 /*
3205 * The PCIe configuration space revision id indicate if mmio reg
3206 * read is disabled
3207 */
3208 const int rev = PCI_REVISION(adapter->sc_pa.pa_class);
3209 readless_supported = ((rev & ENA_MMIO_DISABLE_REG_READ) == 0);
3210 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
3211
3212 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
3213 if (unlikely(rc != 0)) {
3214 device_printf(pdev, "Can not reset device\n");
3215 goto err_mmio_read_less;
3216 }
3217
3218 rc = ena_com_validate_version(ena_dev);
3219 if (unlikely(rc != 0)) {
3220 device_printf(pdev, "device version is too low\n");
3221 goto err_mmio_read_less;
3222 }
3223
3224 dma_width = ena_com_get_dma_width(ena_dev);
3225 if (unlikely(dma_width < 0)) {
3226 device_printf(pdev, "Invalid dma width value %d", dma_width);
3227 rc = dma_width;
3228 goto err_mmio_read_less;
3229 }
3230 adapter->dma_width = dma_width;
3231
3232 /* ENA admin level init */
3233 rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
3234 if (unlikely(rc != 0)) {
3235 device_printf(pdev,
3236 "Can not initialize ena admin queue with device\n");
3237 goto err_mmio_read_less;
3238 }
3239
3240 /*
3241 * To enable the msix interrupts the driver needs to know the number
3242 * of queues. So the driver uses polling mode to retrieve this
3243 * information
3244 */
3245 ena_com_set_admin_polling_mode(ena_dev, true);
3246
3247 ena_config_host_info(ena_dev);
3248
3249 /* Get Device Attributes */
3250 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
3251 if (unlikely(rc != 0)) {
3252 device_printf(pdev,
3253 "Cannot get attribute for ena device rc: %d\n", rc);
3254 goto err_admin_init;
3255 }
3256
3257 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | BIT(ENA_ADMIN_KEEP_ALIVE);
3258
3259 aenq_groups &= get_feat_ctx->aenq.supported_groups;
3260 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
3261 if (unlikely(rc != 0)) {
3262 device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
3263 goto err_admin_init;
3264 }
3265
3266 *wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
3267
3268 return (0);
3269
3270 err_admin_init:
3271 ena_com_delete_host_info(ena_dev);
3272 ena_com_admin_destroy(ena_dev);
3273 err_mmio_read_less:
3274 ena_com_mmio_reg_read_request_destroy(ena_dev);
3275
3276 return (rc);
3277 }
3278
3279 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
3280 int io_vectors)
3281 {
3282 struct ena_com_dev *ena_dev = adapter->ena_dev;
3283 int rc;
3284
3285 rc = ena_enable_msix(adapter);
3286 if (unlikely(rc != 0)) {
3287 device_printf(adapter->pdev, "Error with MSI-X enablement\n");
3288 return (rc);
3289 }
3290
3291 rc = ena_request_mgmnt_irq(adapter);
3292 if (unlikely(rc != 0)) {
3293 device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
3294 goto err_disable_msix;
3295 }
3296
3297 ena_com_set_admin_polling_mode(ena_dev, false);
3298
3299 ena_com_admin_aenq_enable(ena_dev);
3300
3301 return (0);
3302
3303 err_disable_msix:
3304 ena_disable_msix(adapter);
3305
3306 return (rc);
3307 }
3308
3309 /* Function called on ENA_ADMIN_KEEP_ALIVE event */
3310 static void ena_keep_alive_wd(void *adapter_data,
3311 struct ena_admin_aenq_entry *aenq_e)
3312 {
3313 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3314 struct ena_admin_aenq_keep_alive_desc *desc;
3315 sbintime_t stime;
3316 uint64_t rx_drops;
3317
3318 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
3319
3320 rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
3321 counter_u64_zero(adapter->hw_stats.rx_drops);
3322 counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
3323
3324 stime = getsbinuptime();
3325 (void) atomic_swap_64(&adapter->keep_alive_timestamp, stime);
3326 }
3327
3328 /* Check for keep alive expiration */
3329 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
3330 {
3331 sbintime_t timestamp, time;
3332
3333 if (adapter->wd_active == 0)
3334 return;
3335
3336 if (likely(adapter->keep_alive_timeout == 0))
3337 return;
3338
3339 /* FreeBSD uses atomic_load_acq_64() in place of the membar + read */
3340 membar_sync();
3341 timestamp = adapter->keep_alive_timestamp;
3342
3343 time = getsbinuptime() - timestamp;
3344 if (unlikely(time > adapter->keep_alive_timeout)) {
3345 device_printf(adapter->pdev,
3346 "Keep alive watchdog timeout.\n");
3347 counter_u64_add(adapter->dev_stats.wd_expired, 1);
3348 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
3349 adapter->trigger_reset = true;
3350 }
3351 }
3352
3353 /* Check if admin queue is enabled */
3354 static void check_for_admin_com_state(struct ena_adapter *adapter)
3355 {
3356 if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
3357 false)) {
3358 device_printf(adapter->pdev,
3359 "ENA admin queue is not in running state!\n");
3360 counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3361 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
3362 adapter->trigger_reset = true;
3363 }
3364 }
3365
3366 static int
3367 check_missing_comp_in_queue(struct ena_adapter *adapter,
3368 struct ena_ring *tx_ring)
3369 {
3370 struct bintime curtime, time;
3371 struct ena_tx_buffer *tx_buf;
3372 uint32_t missed_tx = 0;
3373 int i;
3374
3375 getbinuptime(&curtime);
3376
3377 for (i = 0; i < tx_ring->ring_size; i++) {
3378 tx_buf = &tx_ring->tx_buffer_info[i];
3379
3380 if (bintime_isset(&tx_buf->timestamp) == 0)
3381 continue;
3382
3383 time = curtime;
3384 bintime_sub(&time, &tx_buf->timestamp);
3385
3386 /* Check again if packet is still waiting */
3387 if (unlikely(bttosbt(time) > adapter->missing_tx_timeout)) {
3388
3389 if (!tx_buf->print_once)
3390 ena_trace(ENA_WARNING, "Found a Tx that wasn't "
3391 "completed on time, qid %d, index %d.\n",
3392 tx_ring->qid, i);
3393
3394 tx_buf->print_once = true;
3395 missed_tx++;
3396 counter_u64_add(tx_ring->tx_stats.missing_tx_comp, 1);
3397
3398 if (unlikely(missed_tx >
3399 adapter->missing_tx_threshold)) {
3400 device_printf(adapter->pdev,
3401 "The number of lost tx completion "
3402 "is above the threshold (%d > %d). "
3403 "Reset the device\n",
3404 missed_tx, adapter->missing_tx_threshold);
3405 adapter->reset_reason =
3406 ENA_REGS_RESET_MISS_TX_CMPL;
3407 adapter->trigger_reset = true;
3408 return (EIO);
3409 }
3410 }
3411 }
3412
3413 return (0);
3414 }
3415
3416 /*
3417 * Check for TX which were not completed on time.
3418 * Timeout is defined by "missing_tx_timeout".
3419 * Reset will be performed if number of incompleted
3420 * transactions exceeds "missing_tx_threshold".
3421 */
3422 static void
3423 check_for_missing_tx_completions(struct ena_adapter *adapter)
3424 {
3425 struct ena_ring *tx_ring;
3426 int i, budget, rc;
3427
3428 /* Make sure the driver doesn't turn the device in other process */
3429 rmb();
3430
3431 if (!adapter->up)
3432 return;
3433
3434 if (adapter->trigger_reset)
3435 return;
3436
3437 if (adapter->missing_tx_timeout == 0)
3438 return;
3439
3440 budget = adapter->missing_tx_max_queues;
3441
3442 for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
3443 tx_ring = &adapter->tx_ring[i];
3444
3445 rc = check_missing_comp_in_queue(adapter, tx_ring);
3446 if (unlikely(rc != 0))
3447 return;
3448
3449 budget--;
3450 if (budget == 0) {
3451 i++;
3452 break;
3453 }
3454 }
3455
3456 adapter->next_monitored_tx_qid = i % adapter->num_queues;
3457 }
3458
3459 /* trigger deferred rx cleanup after 2 consecutive detections */
3460 #define EMPTY_RX_REFILL 2
3461 /* For the rare case where the device runs out of Rx descriptors and the
3462 * msix handler failed to refill new Rx descriptors (due to a lack of memory
3463 * for example).
3464 * This case will lead to a deadlock:
3465 * The device won't send interrupts since all the new Rx packets will be dropped
3466 * The msix handler won't allocate new Rx descriptors so the device won't be
3467 * able to send new packets.
3468 *
3469 * When such a situation is detected - execute rx cleanup task in another thread
3470 */
3471 static void
3472 check_for_empty_rx_ring(struct ena_adapter *adapter)
3473 {
3474 struct ena_ring *rx_ring;
3475 int i, refill_required;
3476
3477 if (!adapter->up)
3478 return;
3479
3480 if (adapter->trigger_reset)
3481 return;
3482
3483 for (i = 0; i < adapter->num_queues; i++) {
3484 rx_ring = &adapter->rx_ring[i];
3485
3486 refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
3487 if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3488 rx_ring->empty_rx_queue++;
3489
3490 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
3491 counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3492 1);
3493
3494 device_printf(adapter->pdev,
3495 "trigger refill for ring %d\n", i);
3496
3497 workqueue_enqueue(rx_ring->cmpl_tq,
3498 &rx_ring->cmpl_task, curcpu());
3499 rx_ring->empty_rx_queue = 0;
3500 }
3501 } else {
3502 rx_ring->empty_rx_queue = 0;
3503 }
3504 }
3505 }
3506
3507 static void
3508 ena_timer_service(void *data)
3509 {
3510 struct ena_adapter *adapter = (struct ena_adapter *)data;
3511 struct ena_admin_host_info *host_info =
3512 adapter->ena_dev->host_attr.host_info;
3513
3514 check_for_missing_keep_alive(adapter);
3515
3516 check_for_admin_com_state(adapter);
3517
3518 check_for_missing_tx_completions(adapter);
3519
3520 check_for_empty_rx_ring(adapter);
3521
3522 if (host_info != NULL)
3523 ena_update_host_info(host_info, adapter->ifp);
3524
3525 if (unlikely(adapter->trigger_reset)) {
3526 device_printf(adapter->pdev, "Trigger reset is on\n");
3527 workqueue_enqueue(adapter->reset_tq, &adapter->reset_task,
3528 curcpu());
3529 return;
3530 }
3531
3532 /*
3533 * Schedule another timeout one second from now.
3534 */
3535 callout_schedule(&adapter->timer_service, hz);
3536 }
3537
3538 static void
3539 ena_reset_task(struct work *wk, void *arg)
3540 {
3541 struct ena_com_dev_get_features_ctx get_feat_ctx;
3542 struct ena_adapter *adapter = (struct ena_adapter *)arg;
3543 struct ena_com_dev *ena_dev = adapter->ena_dev;
3544 bool dev_up;
3545 int rc;
3546
3547 if (unlikely(!adapter->trigger_reset)) {
3548 device_printf(adapter->pdev,
3549 "device reset scheduled but trigger_reset is off\n");
3550 return;
3551 }
3552
3553 rw_enter(&adapter->ioctl_sx, RW_WRITER);
3554
3555 callout_halt(&adapter->timer_service, &adapter->global_mtx);
3556
3557 dev_up = adapter->up;
3558
3559 ena_com_set_admin_running_state(ena_dev, false);
3560 ena_down(adapter);
3561 ena_free_mgmnt_irq(adapter);
3562 ena_disable_msix(adapter);
3563 ena_com_abort_admin_commands(ena_dev);
3564 ena_com_wait_for_abort_completion(ena_dev);
3565 ena_com_admin_destroy(ena_dev);
3566 ena_com_mmio_reg_read_request_destroy(ena_dev);
3567
3568 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3569 adapter->trigger_reset = false;
3570
3571 /* Finished destroy part. Restart the device */
3572 rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
3573 &adapter->wd_active);
3574 if (unlikely(rc != 0)) {
3575 device_printf(adapter->pdev,
3576 "ENA device init failed! (err: %d)\n", rc);
3577 goto err_dev_free;
3578 }
3579
3580 /* XXX dealloc and realloc MSI-X, probably a waste */
3581 rc = ena_enable_msix_and_set_admin_interrupts(adapter,
3582 adapter->num_queues);
3583 if (unlikely(rc != 0)) {
3584 device_printf(adapter->pdev, "Enable MSI-X failed\n");
3585 goto err_com_free;
3586 }
3587
3588 /* If the interface was up before the reset bring it up */
3589 if (dev_up) {
3590 rc = ena_up(adapter);
3591 if (unlikely(rc != 0)) {
3592 device_printf(adapter->pdev,
3593 "Failed to create I/O queues\n");
3594 goto err_msix_free;
3595 }
3596 }
3597
3598 callout_reset(&adapter->timer_service, hz,
3599 ena_timer_service, (void *)adapter);
3600
3601 rw_exit(&adapter->ioctl_sx);
3602
3603 return;
3604
3605 err_msix_free:
3606 ena_free_mgmnt_irq(adapter);
3607 ena_disable_msix(adapter);
3608 err_com_free:
3609 ena_com_admin_destroy(ena_dev);
3610 err_dev_free:
3611 device_printf(adapter->pdev, "ENA reset failed!\n");
3612 adapter->running = false;
3613 rw_exit(&adapter->ioctl_sx);
3614 }
3615
3616 /**
3617 * ena_attach - Device Initialization Routine
3618 * @pdev: device information struct
3619 *
3620 * Returns 0 on success, otherwise on failure.
3621 *
3622 * ena_attach initializes an adapter identified by a device structure.
3623 * The OS initialization, configuring of the adapter private structure,
3624 * and a hardware reset occur.
3625 **/
3626 static void
3627 ena_attach(device_t parent, device_t self, void *aux)
3628 {
3629 struct pci_attach_args *pa = aux;
3630 struct ena_com_dev_get_features_ctx get_feat_ctx;
3631 static int version_printed;
3632 struct ena_adapter *adapter = device_private(self);
3633 struct ena_com_dev *ena_dev = NULL;
3634 uint16_t tx_sgl_size = 0;
3635 uint16_t rx_sgl_size = 0;
3636 pcireg_t reg;
3637 int io_queue_num;
3638 int queue_size;
3639 int rc;
3640
3641 adapter->pdev = self;
3642 adapter->ifp = &adapter->sc_ec.ec_if;
3643 adapter->sc_pa = *pa; /* used after attach for adapter reset too */
3644
3645 if (pci_dma64_available(pa))
3646 adapter->sc_dmat = pa->pa_dmat64;
3647 else
3648 adapter->sc_dmat = pa->pa_dmat;
3649
3650 pci_aprint_devinfo(pa, NULL);
3651
3652 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
3653 if ((reg & PCI_COMMAND_MASTER_ENABLE) == 0) {
3654 reg |= PCI_COMMAND_MASTER_ENABLE;
3655 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
3656 }
3657
3658 mutex_init(&adapter->global_mtx, MUTEX_DEFAULT, IPL_NET);
3659 rw_init(&adapter->ioctl_sx);
3660
3661 /* Set up the timer service */
3662 adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
3663 adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
3664 adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
3665 adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
3666
3667 if (version_printed++ == 0)
3668 device_printf(parent, "%s\n", ena_version);
3669
3670 rc = ena_allocate_pci_resources(pa, adapter);
3671 if (unlikely(rc != 0)) {
3672 device_printf(parent, "PCI resource allocation failed!\n");
3673 ena_free_pci_resources(adapter);
3674 return;
3675 }
3676
3677 /* Allocate memory for ena_dev structure */
3678 ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3679 M_WAITOK | M_ZERO);
3680
3681 adapter->ena_dev = ena_dev;
3682 ena_dev->dmadev = self;
3683 ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
3684 M_WAITOK | M_ZERO);
3685
3686 /* Store register resources */
3687 ((struct ena_bus*)(ena_dev->bus))->reg_bar_t = adapter->sc_btag;
3688 ((struct ena_bus*)(ena_dev->bus))->reg_bar_h = adapter->sc_bhandle;
3689
3690 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3691
3692 /* Device initialization */
3693 rc = ena_device_init(adapter, self, &get_feat_ctx, &adapter->wd_active);
3694 if (unlikely(rc != 0)) {
3695 device_printf(self, "ENA device init failed! (err: %d)\n", rc);
3696 rc = ENXIO;
3697 goto err_bus_free;
3698 }
3699
3700 adapter->keep_alive_timestamp = getsbinuptime();
3701
3702 adapter->tx_offload_cap = get_feat_ctx.offload.tx;
3703
3704 /* Set for sure that interface is not up */
3705 adapter->up = false;
3706
3707 memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
3708 ETHER_ADDR_LEN);
3709
3710 /* calculate IO queue number to create */
3711 io_queue_num = ena_calc_io_queue_num(pa, adapter, &get_feat_ctx);
3712
3713 ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
3714 io_queue_num);
3715 adapter->num_queues = io_queue_num;
3716
3717 adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
3718
3719 /* calculatre ring sizes */
3720 queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
3721 &rx_sgl_size, &get_feat_ctx);
3722 if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) {
3723 rc = ENA_COM_FAULT;
3724 goto err_com_free;
3725 }
3726
3727 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3728
3729 adapter->tx_ring_size = queue_size;
3730 adapter->rx_ring_size = queue_size;
3731
3732 adapter->max_tx_sgl_size = tx_sgl_size;
3733 adapter->max_rx_sgl_size = rx_sgl_size;
3734
3735 #if 0
3736 /* set up dma tags for rx and tx buffers */
3737 rc = ena_setup_tx_dma_tag(adapter);
3738 if (unlikely(rc != 0)) {
3739 device_printf(self, "Failed to create TX DMA tag\n");
3740 goto err_com_free;
3741 }
3742
3743 rc = ena_setup_rx_dma_tag(adapter);
3744 if (unlikely(rc != 0)) {
3745 device_printf(self, "Failed to create RX DMA tag\n");
3746 goto err_tx_tag_free;
3747 }
3748 #endif
3749
3750 /* initialize rings basic information */
3751 device_printf(self, "initalize %d io queues\n", io_queue_num);
3752 ena_init_io_rings(adapter);
3753
3754 /* setup network interface */
3755 rc = ena_setup_ifnet(self, adapter, &get_feat_ctx);
3756 if (unlikely(rc != 0)) {
3757 device_printf(self, "Error with network interface setup\n");
3758 goto err_io_free;
3759 }
3760
3761 rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
3762 if (unlikely(rc != 0)) {
3763 device_printf(self,
3764 "Failed to enable and set the admin interrupts\n");
3765 goto err_ifp_free;
3766 }
3767
3768 /* Initialize reset task queue */
3769 rc = workqueue_create(&adapter->reset_tq, "ena_reset_enqueue",
3770 ena_reset_task, adapter, 0, IPL_NET, 0);
3771 if (unlikely(rc != 0)) {
3772 ena_trace(ENA_ALERT,
3773 "Unable to create workqueue for reset task\n");
3774 goto err_ifp_free;
3775 }
3776
3777 /* Initialize statistics */
3778 ena_alloc_counters_dev(&adapter->dev_stats, io_queue_num);
3779 ena_alloc_counters_hwstats(&adapter->hw_stats, io_queue_num);
3780 #if 0
3781 ena_sysctl_add_nodes(adapter);
3782 #endif
3783
3784 /* Tell the stack that the interface is not active */
3785 if_setdrvflagbits(adapter->ifp, IFF_OACTIVE, IFF_RUNNING);
3786
3787 adapter->running = true;
3788 return;
3789
3790 err_ifp_free:
3791 if_detach(adapter->ifp);
3792 if_free(adapter->ifp);
3793 err_io_free:
3794 ena_free_all_io_rings_resources(adapter);
3795 #if 0
3796 ena_free_rx_dma_tag(adapter);
3797 err_tx_tag_free:
3798 ena_free_tx_dma_tag(adapter);
3799 #endif
3800 err_com_free:
3801 ena_com_admin_destroy(ena_dev);
3802 ena_com_delete_host_info(ena_dev);
3803 ena_com_mmio_reg_read_request_destroy(ena_dev);
3804 err_bus_free:
3805 free(ena_dev->bus, M_DEVBUF);
3806 free(ena_dev, M_DEVBUF);
3807 ena_free_pci_resources(adapter);
3808 }
3809
3810 /**
3811 * ena_detach - Device Removal Routine
3812 * @pdev: device information struct
3813 *
3814 * ena_detach is called by the device subsystem to alert the driver
3815 * that it should release a PCI device.
3816 **/
3817 static int
3818 ena_detach(device_t pdev, int flags)
3819 {
3820 struct ena_adapter *adapter = device_private(pdev);
3821 struct ena_com_dev *ena_dev = adapter->ena_dev;
3822 #if 0
3823 int rc;
3824 #endif
3825
3826 /* Make sure VLANS are not using driver */
3827 if (VLAN_ATTACHED(&adapter->sc_ec)) {
3828 device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
3829 return (EBUSY);
3830 }
3831
3832 /* Free reset task and callout */
3833 callout_halt(&adapter->timer_service, &adapter->global_mtx);
3834 workqueue_wait(adapter->reset_tq, &adapter->reset_task);
3835 workqueue_destroy(adapter->reset_tq);
3836 adapter->reset_tq = NULL;
3837
3838 rw_enter(&adapter->ioctl_sx, RW_WRITER);
3839 ena_down(adapter);
3840 rw_exit(&adapter->ioctl_sx);
3841
3842 if (adapter->ifp != NULL) {
3843 ether_ifdetach(adapter->ifp);
3844 if_free(adapter->ifp);
3845 }
3846
3847 ena_free_all_io_rings_resources(adapter);
3848
3849 ena_free_counters((struct evcnt *)&adapter->hw_stats,
3850 sizeof(struct ena_hw_stats));
3851 ena_free_counters((struct evcnt *)&adapter->dev_stats,
3852 sizeof(struct ena_stats_dev));
3853
3854 if (likely(adapter->rss_support))
3855 ena_com_rss_destroy(ena_dev);
3856
3857 #if 0
3858 rc = ena_free_rx_dma_tag(adapter);
3859 if (unlikely(rc != 0))
3860 device_printf(adapter->pdev,
3861 "Unmapped RX DMA tag associations\n");
3862
3863 rc = ena_free_tx_dma_tag(adapter);
3864 if (unlikely(rc != 0))
3865 device_printf(adapter->pdev,
3866 "Unmapped TX DMA tag associations\n");
3867 #endif
3868
3869 /* Reset the device only if the device is running. */
3870 if (adapter->running)
3871 ena_com_dev_reset(ena_dev, adapter->reset_reason);
3872
3873 ena_com_delete_host_info(ena_dev);
3874
3875 ena_free_irqs(adapter);
3876
3877 ena_com_abort_admin_commands(ena_dev);
3878
3879 ena_com_wait_for_abort_completion(ena_dev);
3880
3881 ena_com_admin_destroy(ena_dev);
3882
3883 ena_com_mmio_reg_read_request_destroy(ena_dev);
3884
3885 ena_free_pci_resources(adapter);
3886
3887 mutex_destroy(&adapter->global_mtx);
3888 rw_destroy(&adapter->ioctl_sx);
3889
3890 if (ena_dev->bus != NULL)
3891 free(ena_dev->bus, M_DEVBUF);
3892
3893 if (ena_dev != NULL)
3894 free(ena_dev, M_DEVBUF);
3895
3896 return 0;
3897 }
3898
3899 /******************************************************************************
3900 ******************************** AENQ Handlers *******************************
3901 *****************************************************************************/
3902 /**
3903 * ena_update_on_link_change:
3904 * Notify the network interface about the change in link status
3905 **/
3906 static void
3907 ena_update_on_link_change(void *adapter_data,
3908 struct ena_admin_aenq_entry *aenq_e)
3909 {
3910 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3911 struct ena_admin_aenq_link_change_desc *aenq_desc;
3912 int status;
3913 struct ifnet *ifp;
3914
3915 aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
3916 ifp = adapter->ifp;
3917 status = aenq_desc->flags &
3918 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
3919
3920 if (status != 0) {
3921 device_printf(adapter->pdev, "link is UP\n");
3922 if_link_state_change(ifp, LINK_STATE_UP);
3923 } else if (status == 0) {
3924 device_printf(adapter->pdev, "link is DOWN\n");
3925 if_link_state_change(ifp, LINK_STATE_DOWN);
3926 } else {
3927 device_printf(adapter->pdev, "invalid value recvd\n");
3928 BUG();
3929 }
3930
3931 adapter->link_status = status;
3932 }
3933
3934 /**
3935 * This handler will called for unknown event group or unimplemented handlers
3936 **/
3937 static void
3938 unimplemented_aenq_handler(void *data,
3939 struct ena_admin_aenq_entry *aenq_e)
3940 {
3941 return;
3942 }
3943
3944 static struct ena_aenq_handlers aenq_handlers = {
3945 .handlers = {
3946 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3947 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
3948 },
3949 .unimplemented_handler = unimplemented_aenq_handler
3950 };
3951
3952 #ifdef __FreeBSD__
3953 /*********************************************************************
3954 * FreeBSD Device Interface Entry Points
3955 *********************************************************************/
3956
3957 static device_method_t ena_methods[] = {
3958 /* Device interface */
3959 DEVMETHOD(device_probe, ena_probe),
3960 DEVMETHOD(device_attach, ena_attach),
3961 DEVMETHOD(device_detach, ena_detach),
3962 DEVMETHOD_END
3963 };
3964
3965 static driver_t ena_driver = {
3966 "ena", ena_methods, sizeof(struct ena_adapter),
3967 };
3968
3969 devclass_t ena_devclass;
3970 DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
3971 MODULE_DEPEND(ena, pci, 1, 1, 1);
3972 MODULE_DEPEND(ena, ether, 1, 1, 1);
3973
3974 /*********************************************************************/
3975 #endif /* __FreeBSD__ */
3976
3977 #ifdef __NetBSD__
3978 CFATTACH_DECL_NEW(ena, sizeof(struct ena_adapter), ena_probe, ena_attach,
3979 ena_detach, NULL);
3980 #endif /* __NetBSD */
3981