xref: /freebsd/sys/dev/aq/aq_main.c (revision e44579e23430ff2084b03c9b3d486f05e617e04f)
1 /*
2  * aQuantia Corporation Network Driver
3  * Copyright (C) 2019 aQuantia Corporation. All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *   (1) Redistributions of source code must retain the above
10  *   copyright notice, this list of conditions and the following
11  *   disclaimer.
12  *
13  *   (2) Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following
15  *   disclaimer in the documentation and/or other materials provided
16  *   with the distribution.
17  *
18  *   (3)The name of the author may not be used to endorse or promote
19  *   products derived from this software without specific prior
20  *   written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_rss.h"
41 
42 #include <sys/param.h>
43 #include <sys/bitstring.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/rman.h>
49 #include <sys/sbuf.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 
56 #include <dev/pci/pcireg.h>
57 #include <dev/pci/pcivar.h>
58 
59 #include <net/ethernet.h>
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_media.h>
63 #include <net/if_var.h>
64 #include <net/iflib.h>
65 #include <net/rss_config.h>
66 
67 #include "ifdi_if.h"
68 
69 #include "aq_device.h"
70 #include "aq_fw.h"
71 #include "aq_hw.h"
72 #include "aq_hw_llh.h"
73 #include "aq_ring.h"
74 #include "aq_dbg.h"
75 
76 MALLOC_DEFINE(M_AQ, "aq", "Aquantia");
77 
78 static const char aq_driver_version[] = AQ_VER;
79 
80 #define AQUANTIA_VENDOR_ID 0x1D6A
81 
82 #define AQ_DEVICE_ID_0001	0x0001
83 #define AQ_DEVICE_ID_D100	0xD100
84 #define AQ_DEVICE_ID_D107	0xD107
85 #define AQ_DEVICE_ID_D108	0xD108
86 #define AQ_DEVICE_ID_D109	0xD109
87 
88 #define AQ_DEVICE_ID_AQC100	0x00B1
89 #define AQ_DEVICE_ID_AQC107	0x07B1
90 #define AQ_DEVICE_ID_AQC108	0x08B1
91 #define AQ_DEVICE_ID_AQC109	0x09B1
92 #define AQ_DEVICE_ID_AQC111	0x11B1
93 #define AQ_DEVICE_ID_AQC112	0x12B1
94 
95 #define AQ_DEVICE_ID_AQC100S	0x80B1
96 #define AQ_DEVICE_ID_AQC107S	0x87B1
97 #define AQ_DEVICE_ID_AQC108S	0x88B1
98 #define AQ_DEVICE_ID_AQC109S	0x89B1
99 #define AQ_DEVICE_ID_AQC111S	0x91B1
100 #define AQ_DEVICE_ID_AQC112S	0x92B1
101 
102 static pci_vendor_info_t aq_vendor_info_array[] = {
103 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_0001,
104 	    "Aquantia AQtion 10Gbit Network Adapter"),
105 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_D107,
106 	    "Aquantia AQtion 10Gbit Network Adapter"),
107 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_D108,
108 	    "Aquantia AQtion 5Gbit Network Adapter"),
109 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_D109,
110 	    "Aquantia AQtion 2.5Gbit Network Adapter"),
111 
112 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC107,
113 	    "Aquantia AQtion 10Gbit Network Adapter"),
114 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC108,
115 	    "Aquantia AQtion 5Gbit Network Adapter"),
116 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC109,
117 	    "Aquantia AQtion 2.5Gbit Network Adapter"),
118 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC100,
119 	    "Aquantia AQtion 10Gbit Network Adapter"),
120 
121 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC107S,
122 	    "Aquantia AQtion 10Gbit Network Adapter"),
123 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC108S,
124 	    "Aquantia AQtion 5Gbit Network Adapter"),
125 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC109S,
126 	    "Aquantia AQtion 2.5Gbit Network Adapter"),
127 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC100S,
128 	    "Aquantia AQtion 10Gbit Network Adapter"),
129 
130 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC111,
131 	    "Aquantia AQtion 5Gbit Network Adapter"),
132 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC112,
133 	    "Aquantia AQtion 2.5Gbit Network Adapter"),
134 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC111S,
135 	    "Aquantia AQtion 5Gbit Network Adapter"),
136 	PVID(AQUANTIA_VENDOR_ID, AQ_DEVICE_ID_AQC112S,
137 	    "Aquantia AQtion 2.5Gbit Network Adapter"),
138 
139 	PVID_END
140 };
141 
142 
143 /* Device setup, teardown, etc */
144 static void *aq_register(device_t dev);
145 static int aq_if_attach_pre(if_ctx_t ctx);
146 static int aq_if_attach_post(if_ctx_t ctx);
147 static int aq_if_detach(if_ctx_t ctx);
148 static int aq_if_shutdown(if_ctx_t ctx);
149 static int aq_if_suspend(if_ctx_t ctx);
150 static int aq_if_resume(if_ctx_t ctx);
151 
152 /* Soft queue setup and teardown */
153 static int aq_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
154 		    uint64_t *paddrs, int ntxqs, int ntxqsets);
155 static int aq_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
156 		    uint64_t *paddrs, int nrxqs, int nrxqsets);
157 static void aq_if_queues_free(if_ctx_t ctx);
158 
159 /* Device configuration */
160 static void aq_if_init(if_ctx_t ctx);
161 static void aq_if_stop(if_ctx_t ctx);
162 static void aq_if_multi_set(if_ctx_t ctx);
163 static int aq_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
164 static void aq_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
165 static int aq_if_media_change(if_ctx_t ctx);
166 static int aq_if_promisc_set(if_ctx_t ctx, int flags);
167 static uint64_t aq_if_get_counter(if_ctx_t ctx, ift_counter cnt);
168 static void aq_if_timer(if_ctx_t ctx, uint16_t qid);
169 static int aq_hw_capabilities(struct aq_dev *softc);
170 static void aq_add_stats_sysctls(struct aq_dev *softc);
171 
172 /* Interrupt enable / disable */
173 static void	aq_if_enable_intr(if_ctx_t ctx);
174 static void	aq_if_disable_intr(if_ctx_t ctx);
175 static int	aq_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
176 static int	aq_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
177 static int	aq_if_msix_intr_assign(if_ctx_t ctx, int msix);
178 
179 /* VLAN support */
180 static bool aq_is_vlan_promisc_required(struct aq_dev *softc);
181 static void aq_update_vlan_filters(struct aq_dev *softc);
182 static void aq_if_vlan_register(if_ctx_t ctx, uint16_t vtag);
183 static void aq_if_vlan_unregister(if_ctx_t ctx, uint16_t vtag);
184 
185 /* Informational/diagnostic */
186 static void	aq_if_led_func(if_ctx_t ctx, int onoff);
187 
188 static device_method_t aq_methods[] = {
189 	DEVMETHOD(device_register, aq_register),
190 	DEVMETHOD(device_probe, iflib_device_probe),
191 	DEVMETHOD(device_attach, iflib_device_attach),
192 	DEVMETHOD(device_detach, iflib_device_detach),
193 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
194 	DEVMETHOD(device_suspend, iflib_device_suspend),
195 	DEVMETHOD(device_resume, iflib_device_resume),
196 
197 	DEVMETHOD_END
198 };
199 
200 static driver_t aq_driver = {
201 	"aq", aq_methods, sizeof(struct aq_dev),
202 };
203 
204 DRIVER_MODULE(atlantic, pci, aq_driver, 0, 0);
205 
206 MODULE_VERSION(atlantic, 1);
207 MODULE_DEPEND(atlantic, pci, 1, 1, 1);
208 MODULE_DEPEND(atlantic, ether, 1, 1, 1);
209 MODULE_DEPEND(atlantic, iflib, 1, 1, 1);
210 
211 IFLIB_PNP_INFO(pci, atlantic, aq_vendor_info_array);
212 
213 static device_method_t aq_if_methods[] = {
214 	/* Device setup, teardown, etc */
215 	DEVMETHOD(ifdi_attach_pre, aq_if_attach_pre),
216 	DEVMETHOD(ifdi_attach_post, aq_if_attach_post),
217 	DEVMETHOD(ifdi_detach, aq_if_detach),
218 
219 	DEVMETHOD(ifdi_shutdown, aq_if_shutdown),
220 	DEVMETHOD(ifdi_suspend, aq_if_suspend),
221 	DEVMETHOD(ifdi_resume, aq_if_resume),
222 
223 	/* Soft queue setup and teardown */
224 	DEVMETHOD(ifdi_tx_queues_alloc, aq_if_tx_queues_alloc),
225 	DEVMETHOD(ifdi_rx_queues_alloc, aq_if_rx_queues_alloc),
226 	DEVMETHOD(ifdi_queues_free, aq_if_queues_free),
227 
228 	/* Device configuration */
229 	DEVMETHOD(ifdi_init, aq_if_init),
230 	DEVMETHOD(ifdi_stop, aq_if_stop),
231 	DEVMETHOD(ifdi_multi_set, aq_if_multi_set),
232 	DEVMETHOD(ifdi_mtu_set, aq_if_mtu_set),
233 	DEVMETHOD(ifdi_media_status, aq_if_media_status),
234 	DEVMETHOD(ifdi_media_change, aq_if_media_change),
235 	DEVMETHOD(ifdi_promisc_set, aq_if_promisc_set),
236 	DEVMETHOD(ifdi_get_counter, aq_if_get_counter),
237 	DEVMETHOD(ifdi_update_admin_status, aq_if_update_admin_status),
238 	DEVMETHOD(ifdi_timer, aq_if_timer),
239 
240 	/* Interrupt enable / disable */
241 	DEVMETHOD(ifdi_intr_enable, aq_if_enable_intr),
242 	DEVMETHOD(ifdi_intr_disable, aq_if_disable_intr),
243 	DEVMETHOD(ifdi_rx_queue_intr_enable, aq_if_rx_queue_intr_enable),
244 	DEVMETHOD(ifdi_tx_queue_intr_enable, aq_if_tx_queue_intr_enable),
245 	DEVMETHOD(ifdi_msix_intr_assign, aq_if_msix_intr_assign),
246 
247 	/* VLAN support */
248 	DEVMETHOD(ifdi_vlan_register, aq_if_vlan_register),
249 	DEVMETHOD(ifdi_vlan_unregister, aq_if_vlan_unregister),
250 
251 	/* Informational/diagnostic */
252 	DEVMETHOD(ifdi_led_func, aq_if_led_func),
253 
254 	DEVMETHOD_END
255 };
256 
257 static driver_t aq_if_driver = {
258 	"aq_if", aq_if_methods, sizeof(struct aq_dev)
259 };
260 
261 static struct if_shared_ctx aq_sctx_init = {
262 	.isc_magic = IFLIB_MAGIC,
263 	.isc_q_align = PAGE_SIZE,
264 	.isc_tx_maxsize = HW_ATL_B0_TSO_SIZE,
265 	.isc_tx_maxsegsize = HW_ATL_B0_MTU_JUMBO,
266 	.isc_tso_maxsize = HW_ATL_B0_TSO_SIZE,
267 	.isc_tso_maxsegsize = HW_ATL_B0_MTU_JUMBO,
268 	.isc_rx_maxsize = HW_ATL_B0_MTU_JUMBO,
269 	.isc_rx_nsegments = 16,
270 	.isc_rx_maxsegsize = PAGE_SIZE,
271 	.isc_nfl = 1,
272 	.isc_nrxqs = 1,
273 	.isc_ntxqs = 1,
274 	.isc_admin_intrcnt = 1,
275 	.isc_vendor_info = aq_vendor_info_array,
276 	.isc_driver_version = aq_driver_version,
277 	.isc_driver = &aq_if_driver,
278 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
279 	    IFLIB_NEED_ZERO_CSUM,
280 
281 	.isc_nrxd_min = {HW_ATL_B0_MIN_RXD},
282 	.isc_ntxd_min = {HW_ATL_B0_MIN_TXD},
283 	.isc_nrxd_max = {HW_ATL_B0_MAX_RXD},
284 	.isc_ntxd_max = {HW_ATL_B0_MAX_TXD},
285 	.isc_nrxd_default = {PAGE_SIZE / sizeof(volatile union aq_txc_desc) * 4},
286 	.isc_ntxd_default = {PAGE_SIZE / sizeof(volatile union aq_txc_desc) * 4},
287 };
288 
289 /*
290  * TUNEABLE PARAMETERS:
291  */
292 
293 static SYSCTL_NODE(_hw, OID_AUTO, aq, CTLFLAG_RD, 0, "Atlantic driver parameters");
294 /* UDP Receive-Side Scaling */
295 static int aq_enable_rss_udp = 1;
296 SYSCTL_INT(_hw_aq, OID_AUTO, enable_rss_udp, CTLFLAG_RDTUN, &aq_enable_rss_udp,
297      0, "Enable Receive-Side Scaling (RSS) for UDP");
298 
299 
300 /*
301  * Device Methods
302  */
303 static void *
aq_register(device_t dev)304 aq_register(device_t dev)
305 {
306 	return (&aq_sctx_init);
307 }
308 
309 static int
aq_if_attach_pre(if_ctx_t ctx)310 aq_if_attach_pre(if_ctx_t ctx)
311 {
312 	struct aq_dev *softc;
313 	struct aq_hw *hw;
314 	if_softc_ctx_t scctx;
315 	int rc;
316 
317 	AQ_DBG_ENTER();
318 	softc = iflib_get_softc(ctx);
319 	rc = 0;
320 
321 	softc->ctx = ctx;
322 	softc->dev = iflib_get_dev(ctx);
323 	softc->media = iflib_get_media(ctx);
324 	softc->scctx = iflib_get_softc_ctx(ctx);
325 	softc->sctx = iflib_get_sctx(ctx);
326 	scctx = softc->scctx;
327 
328 	softc->mmio_rid = PCIR_BAR(0);
329 	softc->mmio_res = bus_alloc_resource_any(softc->dev, SYS_RES_MEMORY,
330 	    &softc->mmio_rid, RF_ACTIVE|RF_SHAREABLE);
331 	if (softc->mmio_res == NULL) {
332 		device_printf(softc->dev,
333 		    "failed to allocate MMIO resources\n");
334 		rc = ENXIO;
335 		goto fail;
336 	}
337 
338 	softc->mmio_tag = rman_get_bustag(softc->mmio_res);
339 	softc->mmio_handle = rman_get_bushandle(softc->mmio_res);
340 	softc->mmio_size = rman_get_size(softc->mmio_res);
341 	softc->hw.hw_tag = softc->mmio_tag;
342 	softc->hw.hw_handle = softc->mmio_handle;
343 	softc->hw.dev = softc->dev;
344 	hw = &softc->hw;
345 	hw->link_rate = aq_fw_speed_auto;
346 	hw->itr = -1;
347 	hw->fc.fc_rx = 1;
348 	hw->fc.fc_tx = 1;
349 	softc->linkup = 0U;
350 
351 	/* Look up ops and caps. */
352 	rc = aq_hw_mpi_create(hw);
353 	if (rc != 0) {
354 		device_printf(softc->dev,
355 		    "%s: aq_hw_mpi_create failed, err=%d\n", __func__, rc);
356 		goto fail;
357 	}
358 
359 	if (hw->fast_start_enabled)
360 		hw->fw_ops->reset(hw);
361 	else
362 		aq_hw_reset(&softc->hw);
363 	aq_hw_capabilities(softc);
364 
365 	rc = aq_hw_get_mac_permanent(hw, hw->mac_addr);
366 	if (rc != 0) {
367 		device_printf(softc->dev, "unable to get MAC address from HW\n");
368 		goto fail;
369 	}
370 
371 	softc->admin_ticks = 0;
372 
373 	iflib_set_mac(ctx, hw->mac_addr);
374 	scctx->isc_tx_csum_flags = CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO;
375 	scctx->isc_capabilities = IFCAP_RXCSUM | IFCAP_TXCSUM | IFCAP_HWCSUM |
376 	    IFCAP_TSO | IFCAP_LRO | IFCAP_JUMBO_MTU | IFCAP_VLAN_HWFILTER |
377 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
378 	scctx->isc_capenable = scctx->isc_capabilities;
379 	scctx->isc_tx_nsegments = 31;
380 	scctx->isc_tx_tso_segments_max = 31;
381 	scctx->isc_tx_tso_size_max =
382 	    HW_ATL_B0_TSO_SIZE - sizeof(struct ether_vlan_header);
383 	scctx->isc_tx_tso_segsize_max = HW_ATL_B0_MTU_JUMBO;
384 	scctx->isc_min_frame_size = 52;
385 	scctx->isc_max_frame_size = ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN +
386 	    ETHER_VLAN_ENCAP_LEN;
387 	scctx->isc_txrx = &aq_txrx;
388 
389 	scctx->isc_txqsizes[0] = sizeof(volatile struct aq_tx_desc) * scctx->isc_ntxd[0];
390 	scctx->isc_rxqsizes[0] = sizeof(volatile struct aq_rx_desc) * scctx->isc_nrxd[0];
391 
392 	scctx->isc_ntxqsets_max = HW_ATL_B0_RINGS_MAX;
393 	scctx->isc_nrxqsets_max = HW_ATL_RSS_INDIRECTION_QUEUES_MAX;
394 
395 	/* iflib will map and release this bar */
396 	scctx->isc_msix_bar = pci_msix_table_bar(softc->dev);
397 
398 	softc->vlan_tags = bit_alloc(4096, M_AQ, M_NOWAIT);
399 	if (softc->vlan_tags == NULL) {
400 		rc = ENOMEM;
401 		goto fail;
402 	}
403 
404 	AQ_DBG_EXIT(rc);
405 	return (rc);
406 
407 fail:
408 	if (softc->mmio_res != NULL)
409 		bus_release_resource(softc->dev, SYS_RES_MEMORY,
410 		    softc->mmio_rid, softc->mmio_res);
411 
412 	AQ_DBG_EXIT(rc);
413 	return (rc);
414 }
415 
416 
417 static int
aq_if_attach_post(if_ctx_t ctx)418 aq_if_attach_post(if_ctx_t ctx)
419 {
420 	struct aq_dev *softc;
421 	int rc;
422 
423 	AQ_DBG_ENTER();
424 
425 	softc = iflib_get_softc(ctx);
426 	rc = 0;
427 
428 	aq_update_hw_stats(softc);
429 
430 	aq_initmedia(softc);
431 
432 
433 	switch (softc->scctx->isc_intr) {
434 	case IFLIB_INTR_LEGACY:
435 		rc = EOPNOTSUPP;
436 		goto exit;
437 	goto exit;
438 		break;
439 	case IFLIB_INTR_MSI:
440 		break;
441 	case IFLIB_INTR_MSIX:
442 		break;
443 	default:
444 		device_printf(softc->dev, "unknown interrupt mode\n");
445 		rc = EOPNOTSUPP;
446 		goto exit;
447 	}
448 
449 	aq_add_stats_sysctls(softc);
450 	/* RSS */
451 	arc4rand(softc->rss_key, HW_ATL_RSS_HASHKEY_SIZE, 0);
452 	uint32_t rss_qs = MIN(softc->rx_rings_count, HW_ATL_RSS_INDIRECTION_QUEUES_MAX);
453 	for (int i = nitems(softc->rss_table); i--;){
454 		softc->rss_table[i] = i % rss_qs;
455 	}
456 exit:
457 	AQ_DBG_EXIT(rc);
458 	return (rc);
459 }
460 
461 
462 static int
aq_if_detach(if_ctx_t ctx)463 aq_if_detach(if_ctx_t ctx)
464 {
465 	struct aq_dev *softc;
466 	int i;
467 
468 	AQ_DBG_ENTER();
469 	softc = iflib_get_softc(ctx);
470 
471 	aq_hw_deinit(&softc->hw);
472 
473 	for (i = 0; i < softc->scctx->isc_nrxqsets; i++)
474 		iflib_irq_free(ctx, &softc->rx_rings[i]->irq);
475 	iflib_irq_free(ctx, &softc->irq);
476 
477 
478 	if (softc->mmio_res != NULL)
479 		bus_release_resource(softc->dev, SYS_RES_MEMORY,
480 		    softc->mmio_rid, softc->mmio_res);
481 
482 	free(softc->vlan_tags, M_AQ);
483 
484 	AQ_DBG_EXIT(0);
485 	return (0);
486 }
487 
488 static int
aq_if_shutdown(if_ctx_t ctx)489 aq_if_shutdown(if_ctx_t ctx)
490 {
491 	return (aq_if_suspend(ctx));
492 }
493 
494 static int
aq_if_suspend(if_ctx_t ctx)495 aq_if_suspend(if_ctx_t ctx)
496 {
497 	struct aq_dev *softc = iflib_get_softc(ctx);
498 
499 	AQ_DBG_ENTER();
500 
501 	aq_if_stop(ctx);
502 	aq_hw_deinit(&softc->hw);
503 
504 	AQ_DBG_EXIT(0);
505 	return (0);
506 }
507 
508 static int
aq_if_resume(if_ctx_t ctx)509 aq_if_resume(if_ctx_t ctx)
510 {
511 	struct aq_dev *softc = iflib_get_softc(ctx);
512 	int err;
513 
514 	AQ_DBG_ENTER();
515 	err = aq_hw_mpi_create(&softc->hw);
516 	AQ_DBG_EXIT(err);
517 	return (err);
518 }
519 
520 _Static_assert(sizeof(struct aq_ring_stats) % sizeof(counter_u64_t) == 0,
521     "aq_ring_stats must contain only counter_u64_t fields");
522 
523 static int
aq_ring_stats_alloc(struct aq_ring * ring)524 aq_ring_stats_alloc(struct aq_ring *ring)
525 {
526 	counter_u64_t *c = (counter_u64_t *)&ring->stats;
527 	int i, n = sizeof(ring->stats) / sizeof(counter_u64_t);
528 
529 	for (i = 0; i < n; i++) {
530 		c[i] = counter_u64_alloc(M_NOWAIT);
531 		if (c[i] == NULL) {
532 			while (i-- > 0) {
533 				counter_u64_free(c[i]);
534 				c[i] = NULL;
535 			}
536 			return (ENOMEM);
537 		}
538 	}
539 	return (0);
540 }
541 
542 static void
aq_ring_stats_free(struct aq_ring * ring)543 aq_ring_stats_free(struct aq_ring *ring)
544 {
545 	counter_u64_t *c = (counter_u64_t *)&ring->stats;
546 	int i, n = sizeof(ring->stats) / sizeof(counter_u64_t);
547 
548 	for (i = 0; i < n; i++) {
549 		if (c[i] != NULL) {
550 			counter_u64_free(c[i]);
551 			c[i] = NULL;
552 		}
553 	}
554 }
555 
556 /* Soft queue setup and teardown */
557 static int
aq_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)558 aq_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
559     int ntxqs, int ntxqsets)
560 {
561 	struct aq_dev *softc;
562 	struct aq_ring *ring;
563 	int rc = 0, i;
564 
565 	AQ_DBG_ENTERA("ntxqs=%d, ntxqsets=%d", ntxqs, ntxqsets);
566 	softc = iflib_get_softc(ctx);
567 	AQ_DBG_PRINT("tx descriptors  number %d", softc->scctx->isc_ntxd[0]);
568 
569 	for (i = 0; i < ntxqsets; i++) {
570 		ring = softc->tx_rings[i] = malloc(sizeof(struct aq_ring),
571 						   M_AQ, M_NOWAIT | M_ZERO);
572 		if (!ring){
573 			rc = ENOMEM;
574 			device_printf(softc->dev, "atlantic: tx_ring malloc fail\n");
575 			goto fail;
576 		}
577 		ring->tx_descs = (volatile struct aq_tx_desc*)vaddrs[i];
578 		ring->tx_size = softc->scctx->isc_ntxd[0];
579 		ring->tx_descs_phys = paddrs[i];
580 		ring->tx_head = ring->tx_tail = 0;
581 		ring->index = i;
582 		ring->dev = softc;
583 
584 		softc->tx_rings_count++;
585 
586 		rc = aq_ring_stats_alloc(ring);
587 		if (rc != 0) {
588 			device_printf(softc->dev,
589 			    "atlantic: tx_ring stats alloc fail\n");
590 			goto fail;
591 		}
592 	}
593 
594 	AQ_DBG_EXIT(rc);
595 	return (rc);
596 
597 fail:
598 	aq_if_queues_free(ctx);
599 	AQ_DBG_EXIT(rc);
600 	return (rc);
601 }
602 
603 static int
aq_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)604 aq_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
605     int nrxqs, int nrxqsets)
606 {
607 	struct aq_dev *softc;
608 	struct aq_ring *ring;
609 	int rc = 0, i;
610 
611 	AQ_DBG_ENTERA("nrxqs=%d, nrxqsets=%d", nrxqs, nrxqsets);
612 	softc = iflib_get_softc(ctx);
613 
614 	for (i = 0; i < nrxqsets; i++) {
615 		ring = softc->rx_rings[i] = malloc(sizeof(struct aq_ring),
616 						   M_AQ, M_NOWAIT | M_ZERO);
617 		if (!ring){
618 			rc = ENOMEM;
619 			device_printf(softc->dev,
620 			    "atlantic: rx_ring malloc fail\n");
621 			goto fail;
622 		}
623 
624 		ring->rx_descs = (volatile struct aq_rx_desc*)vaddrs[i];
625 		ring->rx_descs_phys = paddrs[i];
626 		ring->rx_size = softc->scctx->isc_nrxd[0];
627 		ring->index = i;
628 		ring->dev = softc;
629 
630 		softc->rx_rings_count++;
631 
632 		rc = aq_ring_stats_alloc(ring);
633 		if (rc != 0) {
634 			device_printf(softc->dev,
635 			    "atlantic: rx_ring stats alloc fail\n");
636 			goto fail;
637 		}
638 	}
639 
640 	AQ_DBG_EXIT(rc);
641 	return (rc);
642 
643 fail:
644 	aq_if_queues_free(ctx);
645 	AQ_DBG_EXIT(rc);
646 	return (rc);
647 }
648 
649 static void
aq_if_queues_free(if_ctx_t ctx)650 aq_if_queues_free(if_ctx_t ctx)
651 {
652 	struct aq_dev *softc;
653 	int i;
654 
655 	AQ_DBG_ENTER();
656 	softc = iflib_get_softc(ctx);
657 
658 	for (i = 0; i < softc->tx_rings_count; i++) {
659 		if (softc->tx_rings[i]) {
660 			aq_ring_stats_free(softc->tx_rings[i]);
661 			free(softc->tx_rings[i], M_AQ);
662 			softc->tx_rings[i] = NULL;
663 		}
664 	}
665 	softc->tx_rings_count = 0;
666 	for (i = 0; i < softc->rx_rings_count; i++) {
667 		if (softc->rx_rings[i]){
668 			aq_ring_stats_free(softc->rx_rings[i]);
669 			free(softc->rx_rings[i], M_AQ);
670 			softc->rx_rings[i] = NULL;
671 		}
672 	}
673 	softc->rx_rings_count = 0;
674 
675 	AQ_DBG_EXIT(0);
676 	return;
677 }
678 
679 /* Device configuration */
680 static void
aq_if_init(if_ctx_t ctx)681 aq_if_init(if_ctx_t ctx)
682 {
683 	struct aq_dev *softc;
684 	struct aq_hw *hw;
685 	struct ifmediareq ifmr;
686 	int i, err;
687 
688 	AQ_DBG_ENTER();
689 	softc = iflib_get_softc(ctx);
690 	hw = &softc->hw;
691 
692 	atomic_store_rel_long(&hw->flags, 0);
693 
694 	hw->tx_rings_count = softc->tx_rings_count;
695 
696 	err = aq_hw_init(&softc->hw, softc->hw.mac_addr, softc->msix,
697 	    softc->scctx->isc_intr == IFLIB_INTR_MSIX);
698 	if (err != 0) {
699 		device_printf(softc->dev, "atlantic: aq_hw_init: %d", err);
700 	}
701 
702 	aq_if_media_status(ctx, &ifmr);
703 
704 	aq_update_vlan_filters(softc);
705 
706 	for (i = 0; i < softc->tx_rings_count; i++) {
707 		struct aq_ring *ring = softc->tx_rings[i];
708 		err = aq_ring_tx_init(&softc->hw, ring);
709 		if (err) {
710 			device_printf(softc->dev,
711 			    "atlantic: aq_ring_tx_init: %d", err);
712 		}
713 		err = aq_ring_tx_start(hw, ring);
714 		if (err != 0) {
715 			device_printf(softc->dev,
716 			    "atlantic: aq_ring_tx_start: %d", err);
717 		}
718 	}
719 	for (i = 0; i < softc->rx_rings_count; i++) {
720 		struct aq_ring *ring = softc->rx_rings[i];
721 		ring->rx_buf_size = iflib_get_rx_mbuf_sz(ctx);
722 		err = aq_ring_rx_init(&softc->hw, ring);
723 		if (err) {
724 			device_printf(softc->dev,
725 			    "atlantic: aq_ring_rx_init: %d", err);
726 		}
727 		err = aq_ring_rx_start(hw, ring);
728 		if (err != 0) {
729 			device_printf(softc->dev,
730 			    "atlantic: aq_ring_rx_start: %d", err);
731 		}
732 		aq_if_rx_queue_intr_enable(ctx, i);
733 	}
734 
735 	aq_hw_start(hw);
736 	aq_if_enable_intr(ctx);
737 	aq_hw_rss_hash_set(&softc->hw, softc->rss_key);
738 	aq_hw_rss_set(&softc->hw, softc->rss_table);
739 	aq_hw_udp_rss_enable(hw, aq_enable_rss_udp);
740 	aq_hw_set_link_speed(hw, hw->link_rate);
741 
742 	AQ_DBG_EXIT(0);
743 }
744 
745 
746 static void
aq_if_stop(if_ctx_t ctx)747 aq_if_stop(if_ctx_t ctx)
748 {
749 	struct aq_dev *softc;
750 	struct aq_hw *hw;
751 	int i;
752 
753 	AQ_DBG_ENTER();
754 
755 	softc = iflib_get_softc(ctx);
756 	hw = &softc->hw;
757 
758 	/* disable interrupt */
759 	aq_if_disable_intr(ctx);
760 
761 	for (i = 0; i < softc->tx_rings_count; i++) {
762 		aq_ring_tx_stop(hw, softc->tx_rings[i]);
763 		softc->tx_rings[i]->tx_head = 0;
764 		softc->tx_rings[i]->tx_tail = 0;
765 	}
766 	for (i = 0; i < softc->rx_rings_count; i++) {
767 		aq_ring_rx_stop(hw, softc->rx_rings[i]);
768 	}
769 
770 	aq_hw_reset(&softc->hw);
771 	memset(&softc->last_stats, 0, sizeof(softc->last_stats));
772 	softc->linkup = false;
773 	aq_if_update_admin_status(ctx);
774 	AQ_DBG_EXIT(0);
775 }
776 
777 static uint64_t
aq_if_get_counter(if_ctx_t ctx,ift_counter cnt)778 aq_if_get_counter(if_ctx_t ctx, ift_counter cnt)
779 {
780 	struct aq_dev *softc = iflib_get_softc(ctx);
781 	if_t ifp = iflib_get_ifp(ctx);
782 
783 	switch (cnt) {
784 	case IFCOUNTER_IERRORS:
785 		return (softc->curr_stats.erpr);
786 	case IFCOUNTER_IQDROPS:
787 		return (softc->curr_stats.dpc);
788 	case IFCOUNTER_OERRORS:
789 		return (softc->curr_stats.erpt);
790 	default:
791 		return (if_get_counter_default(ifp, cnt));
792 	}
793 }
794 
795 static u_int
aq_mc_filter_apply(void * arg,struct sockaddr_dl * dl,u_int count)796 aq_mc_filter_apply(void *arg, struct sockaddr_dl *dl, u_int count)
797 {
798 	struct aq_dev *softc = arg;
799 	struct aq_hw *hw = &softc->hw;
800 	uint8_t *mac_addr = NULL;
801 
802 	if (count == AQ_HW_MAC_MAX)
803 		return (0);
804 
805 	mac_addr = LLADDR(dl);
806 	aq_hw_mac_addr_set(hw, mac_addr, count + 1);
807 
808 	aq_log_detail("set %d mc address %6D", count + 1, mac_addr, ":");
809 	return (1);
810 }
811 
812 static bool
aq_is_mc_promisc_required(struct aq_dev * softc)813 aq_is_mc_promisc_required(struct aq_dev *softc)
814 {
815 	return (softc->mcnt >= AQ_HW_MAC_MAX);
816 }
817 
818 static void
aq_if_multi_set(if_ctx_t ctx)819 aq_if_multi_set(if_ctx_t ctx)
820 {
821 	struct aq_dev *softc = iflib_get_softc(ctx);
822 	if_t ifp = iflib_get_ifp(ctx);
823 	struct aq_hw  *hw = &softc->hw;
824 	AQ_DBG_ENTER();
825 	softc->mcnt = if_llmaddr_count(iflib_get_ifp(ctx));
826 	if (softc->mcnt >= AQ_HW_MAC_MAX) {
827 		aq_hw_set_promisc(hw, !!(if_getflags(ifp) & IFF_PROMISC),
828 		    aq_is_vlan_promisc_required(softc),
829 		    !!(if_getflags(ifp) & IFF_ALLMULTI) || aq_is_mc_promisc_required(softc));
830 	} else {
831 		if_foreach_llmaddr(iflib_get_ifp(ctx), &aq_mc_filter_apply, softc);
832 	}
833 	AQ_DBG_EXIT(0);
834 }
835 
836 static int
aq_if_mtu_set(if_ctx_t ctx,uint32_t mtu)837 aq_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
838 {
839 	if_softc_ctx_t scctx = iflib_get_softc_ctx(ctx);
840 	uint32_t max_frame;
841 
842 	AQ_DBG_ENTERA("mtu %u", mtu);
843 
844 	max_frame = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
845 	if (max_frame > HW_ATL_B0_MTU_JUMBO) {
846 		AQ_DBG_EXIT(EINVAL);
847 		return (EINVAL);
848 	}
849 
850 	scctx->isc_max_frame_size = max_frame;
851 
852 	AQ_DBG_EXIT(0);
853 	return (0);
854 }
855 
856 static void
aq_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)857 aq_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
858 {
859 	if_t ifp;
860 
861 	AQ_DBG_ENTER();
862 
863 	ifp = iflib_get_ifp(ctx);
864 
865 	aq_mediastatus(ifp, ifmr);
866 
867 	AQ_DBG_EXIT(0);
868 }
869 
870 static int
aq_if_media_change(if_ctx_t ctx)871 aq_if_media_change(if_ctx_t ctx)
872 {
873 	struct aq_dev *softc = iflib_get_softc(ctx);
874 	if_t ifp = iflib_get_ifp(ctx);
875 	int rc = 0;
876 
877 	AQ_DBG_ENTER();
878 
879 	/* Not allowd in UP state, since causes unsync of rings */
880 	if ((if_getflags(ifp) & IFF_UP)){
881 		rc = EPERM;
882 		goto exit;
883 	}
884 
885 	ifp = iflib_get_ifp(softc->ctx);
886 
887 	rc = aq_mediachange(ifp);
888 
889 exit:
890 	AQ_DBG_EXIT(rc);
891 	return (rc);
892 }
893 
894 static int
aq_if_promisc_set(if_ctx_t ctx,int flags)895 aq_if_promisc_set(if_ctx_t ctx, int flags)
896 {
897 	struct aq_dev *softc;
898 
899 	AQ_DBG_ENTER();
900 
901 	softc = iflib_get_softc(ctx);
902 
903 	aq_hw_set_promisc(&softc->hw, !!(flags & IFF_PROMISC),
904 	    aq_is_vlan_promisc_required(softc),
905 	    !!(flags & IFF_ALLMULTI) || aq_is_mc_promisc_required(softc));
906 
907 	AQ_DBG_EXIT(0);
908 	return (0);
909 }
910 
911 static void
aq_if_timer(if_ctx_t ctx,uint16_t qid)912 aq_if_timer(if_ctx_t ctx, uint16_t qid)
913 {
914 	struct aq_dev *softc;
915 	uint64_t ticks_now;
916 
917 //	AQ_DBG_ENTER();
918 
919 	softc = iflib_get_softc(ctx);
920 	ticks_now = ticks;
921 
922 	/* Schedule aqc_if_update_admin_status() once per sec */
923 	if (ticks_now - softc->admin_ticks >= hz) {
924 		softc->admin_ticks = ticks_now;
925 		iflib_admin_intr_deferred(ctx);
926 	}
927 
928 //	AQ_DBG_EXIT(0);
929 	return;
930 
931 }
932 
933 /* Interrupt enable / disable */
934 static void
aq_if_enable_intr(if_ctx_t ctx)935 aq_if_enable_intr(if_ctx_t ctx)
936 {
937 	struct aq_dev *softc = iflib_get_softc(ctx);
938 	struct aq_hw  *hw = &softc->hw;
939 
940 	AQ_DBG_ENTER();
941 
942 	/* Enable interrupts */
943 	itr_irq_msk_setlsw_set(hw, BIT(softc->msix + 1) - 1);
944 
945 	AQ_DBG_EXIT(0);
946 }
947 
948 static void
aq_if_disable_intr(if_ctx_t ctx)949 aq_if_disable_intr(if_ctx_t ctx)
950 {
951 	struct aq_dev *softc = iflib_get_softc(ctx);
952 	struct aq_hw  *hw = &softc->hw;
953 
954 	AQ_DBG_ENTER();
955 
956 	/* Disable interrupts */
957 	itr_irq_msk_clearlsw_set(hw, BIT(softc->msix + 1) - 1);
958 
959 	AQ_DBG_EXIT(0);
960 }
961 
962 static int
aq_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)963 aq_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
964 {
965 	struct aq_dev *softc = iflib_get_softc(ctx);
966 	struct aq_hw  *hw = &softc->hw;
967 
968 	AQ_DBG_ENTER();
969 
970 	itr_irq_msk_setlsw_set(hw, BIT(softc->rx_rings[rxqid]->msix));
971 
972 	AQ_DBG_EXIT(0);
973 	return (0);
974 }
975 
976 static int
aq_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)977 aq_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
978 {
979 	struct aq_dev *softc = iflib_get_softc(ctx);
980 	struct aq_hw  *hw = &softc->hw;
981 
982 	AQ_DBG_ENTER();
983 
984 	itr_irq_msk_setlsw_set(hw, BIT(softc->tx_rings[txqid]->msix));
985 
986 	AQ_DBG_EXIT(0);
987 	return (0);
988 }
989 
990 static int
aq_if_msix_intr_assign(if_ctx_t ctx,int msix)991 aq_if_msix_intr_assign(if_ctx_t ctx, int msix)
992 {
993 	struct aq_dev *softc;
994 	int i, vector = 0, rc;
995 	char irq_name[16];
996 	int rx_vectors;
997 
998 	AQ_DBG_ENTER();
999 	softc = iflib_get_softc(ctx);
1000 
1001 	for (i = 0; i < softc->rx_rings_count; i++, vector++) {
1002 		snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
1003 		rc = iflib_irq_alloc_generic(ctx, &softc->rx_rings[i]->irq,
1004 		    vector + 1, IFLIB_INTR_RXTX, aq_isr_rx, softc->rx_rings[i],
1005 			softc->rx_rings[i]->index, irq_name);
1006 		device_printf(softc->dev, "Assign IRQ %u to rx ring %u\n",
1007 					  vector, softc->rx_rings[i]->index);
1008 
1009 		if (rc) {
1010 			device_printf(softc->dev, "failed to set up RX handler\n");
1011 			i--;
1012 			goto fail;
1013 		}
1014 
1015 		softc->rx_rings[i]->msix = vector;
1016 	}
1017 
1018 	rx_vectors = vector;
1019 
1020 	for (i = 0; i < softc->tx_rings_count; i++) {
1021 		snprintf(irq_name, sizeof(irq_name), "txq%d", i);
1022 		softc->tx_rings[i]->msix = (i % softc->rx_rings_count);
1023 		iflib_softirq_alloc_generic(ctx,
1024 		    &softc->rx_rings[softc->tx_rings[i]->msix]->irq,
1025 		    IFLIB_INTR_TX, softc->tx_rings[i],
1026 		    softc->tx_rings[i]->index, irq_name);
1027 		device_printf(softc->dev, "Assign IRQ %u to tx ring %u\n",
1028 		    softc->tx_rings[i]->msix, softc->tx_rings[i]->index);
1029 	}
1030 
1031 	rc = iflib_irq_alloc_generic(ctx, &softc->irq, rx_vectors + 1,
1032 	    IFLIB_INTR_ADMIN, aq_linkstat_isr, softc, 0, "aq");
1033 	softc->msix = rx_vectors;
1034 	device_printf(softc->dev, "Assign IRQ %u to admin proc \n",
1035 	    rx_vectors);
1036 	if (rc) {
1037 		device_printf(iflib_get_dev(ctx),
1038 		    "Failed to register admin handler");
1039 		i = softc->rx_rings_count - 1;
1040 		goto fail;
1041 	}
1042 	AQ_DBG_EXIT(0);
1043 	return (0);
1044 
1045 fail:
1046 	for (; i >= 0; i--)
1047 		iflib_irq_free(ctx, &softc->rx_rings[i]->irq);
1048 	AQ_DBG_EXIT(rc);
1049 	return (rc);
1050 }
1051 
1052 static bool
aq_is_vlan_promisc_required(struct aq_dev * softc)1053 aq_is_vlan_promisc_required(struct aq_dev *softc)
1054 {
1055 	int vlan_tag_count;
1056 
1057 	bit_count(softc->vlan_tags, 0, 4096, &vlan_tag_count);
1058 
1059 	if (vlan_tag_count <= AQ_HW_VLAN_MAX_FILTERS)
1060 		return (false);
1061 	else
1062 		return (true);
1063 
1064 }
1065 
1066 static void
aq_update_vlan_filters(struct aq_dev * softc)1067 aq_update_vlan_filters(struct aq_dev *softc)
1068 {
1069 	struct aq_rx_filter_vlan aq_vlans[AQ_HW_VLAN_MAX_FILTERS];
1070 	struct aq_hw  *hw = &softc->hw;
1071 	int bit_pos = 0;
1072 	int vlan_tag = -1;
1073 	int i;
1074 
1075 	hw_atl_b0_hw_vlan_promisc_set(hw, true);
1076 	for (i = 0; i < AQ_HW_VLAN_MAX_FILTERS; i++) {
1077 		bit_ffs_at(softc->vlan_tags, bit_pos, 4096, &vlan_tag);
1078 		if (vlan_tag != -1) {
1079 			aq_vlans[i].enable = true;
1080 			aq_vlans[i].location = i;
1081 			aq_vlans[i].queue = 0xFF;
1082 			aq_vlans[i].vlan_id = vlan_tag;
1083 			bit_pos = vlan_tag + 1;
1084 		} else {
1085 			aq_vlans[i].enable = false;
1086 		}
1087 	}
1088 
1089 	hw_atl_b0_hw_vlan_set(hw, aq_vlans);
1090 	hw_atl_b0_hw_vlan_promisc_set(hw, aq_is_vlan_promisc_required(softc));
1091 }
1092 
1093 /* VLAN support */
1094 static void
aq_if_vlan_register(if_ctx_t ctx,uint16_t vtag)1095 aq_if_vlan_register(if_ctx_t ctx, uint16_t vtag)
1096 {
1097 	struct aq_dev *softc = iflib_get_softc(ctx);
1098 
1099 	AQ_DBG_ENTERA("%d", vtag);
1100 
1101 	bit_set(softc->vlan_tags, vtag);
1102 
1103 	aq_update_vlan_filters(softc);
1104 
1105 	AQ_DBG_EXIT(0);
1106 }
1107 
1108 static void
aq_if_vlan_unregister(if_ctx_t ctx,uint16_t vtag)1109 aq_if_vlan_unregister(if_ctx_t ctx, uint16_t vtag)
1110 {
1111 	struct aq_dev *softc = iflib_get_softc(ctx);
1112 
1113 	AQ_DBG_ENTERA("%d", vtag);
1114 
1115 	bit_clear(softc->vlan_tags, vtag);
1116 
1117 	aq_update_vlan_filters(softc);
1118 
1119 	AQ_DBG_EXIT(0);
1120 }
1121 
1122 static void
aq_if_led_func(if_ctx_t ctx,int onoff)1123 aq_if_led_func(if_ctx_t ctx, int onoff)
1124 {
1125 	struct aq_dev *softc = iflib_get_softc(ctx);
1126 	struct aq_hw  *hw = &softc->hw;
1127 
1128 	AQ_DBG_ENTERA("%d", onoff);
1129 	if (hw->fw_ops->led_control)
1130 		hw->fw_ops->led_control(hw, onoff);
1131 
1132 	AQ_DBG_EXIT(0);
1133 }
1134 
1135 static int
aq_hw_capabilities(struct aq_dev * softc)1136 aq_hw_capabilities(struct aq_dev *softc)
1137 {
1138 
1139 	if (pci_get_vendor(softc->dev) != AQUANTIA_VENDOR_ID)
1140 		return (ENXIO);
1141 
1142 	switch (pci_get_device(softc->dev)) {
1143 	case AQ_DEVICE_ID_D100:
1144 	case AQ_DEVICE_ID_AQC100:
1145 	case AQ_DEVICE_ID_AQC100S:
1146 		softc->media_type = AQ_MEDIA_TYPE_FIBRE;
1147 		softc->link_speeds = AQ_LINK_ALL & ~AQ_LINK_10G;
1148 		break;
1149 
1150 	case AQ_DEVICE_ID_0001:
1151 	case AQ_DEVICE_ID_D107:
1152 	case AQ_DEVICE_ID_AQC107:
1153 	case AQ_DEVICE_ID_AQC107S:
1154 		softc->media_type = AQ_MEDIA_TYPE_TP;
1155 		softc->link_speeds = AQ_LINK_ALL;
1156 		break;
1157 
1158 	case AQ_DEVICE_ID_D108:
1159 	case AQ_DEVICE_ID_AQC108:
1160 	case AQ_DEVICE_ID_AQC108S:
1161 	case AQ_DEVICE_ID_AQC111:
1162 	case AQ_DEVICE_ID_AQC111S:
1163 		softc->media_type = AQ_MEDIA_TYPE_TP;
1164 		softc->link_speeds = AQ_LINK_ALL & ~AQ_LINK_10G;
1165 		break;
1166 
1167 	case AQ_DEVICE_ID_D109:
1168 	case AQ_DEVICE_ID_AQC109:
1169 	case AQ_DEVICE_ID_AQC109S:
1170 	case AQ_DEVICE_ID_AQC112:
1171 	case AQ_DEVICE_ID_AQC112S:
1172 		softc->media_type = AQ_MEDIA_TYPE_TP;
1173 		softc->link_speeds = AQ_LINK_ALL & ~(AQ_LINK_10G | AQ_LINK_5G);
1174 		break;
1175 
1176 	default:
1177 		return (ENXIO);
1178 	}
1179 
1180 	return (0);
1181 }
1182 
1183 static int
aq_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)1184 aq_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
1185 {
1186 	struct aq_dev  *softc = (struct aq_dev *)arg1;
1187 	device_t        dev = softc->dev;
1188 	struct sbuf     *buf;
1189 	int             error = 0;
1190 
1191 	buf = sbuf_new_for_sysctl(NULL, NULL, 256, req);
1192 	if (!buf) {
1193 		device_printf(dev, "Could not allocate sbuf for output.\n");
1194 		return (ENOMEM);
1195 	}
1196 
1197 	/* Print out the redirection table */
1198 	sbuf_cat(buf, "\nRSS Indirection table:\n");
1199 	for (int i = 0; i < HW_ATL_RSS_INDIRECTION_TABLE_MAX; i++) {
1200 		sbuf_printf(buf, "%d ", softc->rss_table[i]);
1201 		if ((i+1) % 10 == 0)
1202 			sbuf_printf(buf, "\n");
1203 	}
1204 
1205 	sbuf_cat(buf, "\nRSS Key:\n");
1206 	for (int i = 0; i < HW_ATL_RSS_HASHKEY_SIZE; i++) {
1207 		sbuf_printf(buf, "0x%02x ", softc->rss_key[i]);
1208 	}
1209 	sbuf_printf(buf, "\n");
1210 
1211 	error = sbuf_finish(buf);
1212 	if (error)
1213 		device_printf(dev, "Error finishing sbuf: %d\n", error);
1214 
1215 	sbuf_delete(buf);
1216 
1217 	return (0);
1218 }
1219 
1220 static int
aq_sysctl_print_tx_head(SYSCTL_HANDLER_ARGS)1221 aq_sysctl_print_tx_head(SYSCTL_HANDLER_ARGS)
1222 {
1223 	struct aq_ring  *ring = arg1;
1224 	int             error = 0;
1225 	unsigned int   val;
1226 
1227 	if (!ring)
1228 		return (0);
1229 
1230 	val = tdm_tx_desc_head_ptr_get(&ring->dev->hw, ring->index);
1231 
1232 	error = sysctl_handle_int(oidp, &val, 0, req);
1233 	if (error || !req->newptr)
1234 		return (error);
1235 
1236 	return (0);
1237 }
1238 
1239 static int
aq_sysctl_print_tx_tail(SYSCTL_HANDLER_ARGS)1240 aq_sysctl_print_tx_tail(SYSCTL_HANDLER_ARGS)
1241 {
1242 	struct aq_ring  *ring = arg1;
1243 	int             error = 0;
1244 	unsigned int   val;
1245 
1246 	if (!ring)
1247 		return (0);
1248 
1249 	val = reg_tx_dma_desc_tail_ptr_get(&ring->dev->hw, ring->index);
1250 
1251 	error = sysctl_handle_int(oidp, &val, 0, req);
1252 	if (error || !req->newptr)
1253 		return (error);
1254 
1255 	return (0);
1256 }
1257 
1258 static int
aq_sysctl_print_rx_head(SYSCTL_HANDLER_ARGS)1259 aq_sysctl_print_rx_head(SYSCTL_HANDLER_ARGS)
1260 {
1261 	struct aq_ring  *ring = arg1;
1262 	int             error = 0;
1263 	unsigned int   val;
1264 
1265 	if (!ring)
1266 		return (0);
1267 
1268 	val = rdm_rx_desc_head_ptr_get(&ring->dev->hw, ring->index);
1269 
1270 	error = sysctl_handle_int(oidp, &val, 0, req);
1271 	if (error || !req->newptr)
1272 		return (error);
1273 
1274 	return (0);
1275 }
1276 
1277 static int
aq_sysctl_print_rx_tail(SYSCTL_HANDLER_ARGS)1278 aq_sysctl_print_rx_tail(SYSCTL_HANDLER_ARGS)
1279 {
1280 	struct aq_ring  *ring = arg1;
1281 	int             error = 0;
1282 	unsigned int   val;
1283 
1284 	if (!ring)
1285 		return (0);
1286 
1287 	val = reg_rx_dma_desc_tail_ptr_get(&ring->dev->hw, ring->index);
1288 
1289 	error = sysctl_handle_int(oidp, &val, 0, req);
1290 	if (error || !req->newptr)
1291 		return (error);
1292 
1293 	return (0);
1294 }
1295 
1296 static void
aq_add_stats_sysctls(struct aq_dev * softc)1297 aq_add_stats_sysctls(struct aq_dev *softc)
1298 {
1299 	device_t                dev = softc->dev;
1300 	struct sysctl_ctx_list  *ctx = device_get_sysctl_ctx(dev);
1301 	struct sysctl_oid       *tree = device_get_sysctl_tree(dev);
1302 	struct sysctl_oid_list  *child = SYSCTL_CHILDREN(tree);
1303 	struct aq_stats *stats = &softc->curr_stats;
1304 	struct sysctl_oid       *stat_node, *queue_node;
1305 	struct sysctl_oid_list  *stat_list, *queue_list;
1306 
1307 #define QUEUE_NAME_LEN 32
1308 	char                    namebuf[QUEUE_NAME_LEN];
1309 	/* RSS configuration */
1310 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "print_rss_config",
1311 	    CTLTYPE_STRING | CTLFLAG_RD, softc, 0,
1312 	    aq_sysctl_print_rss_config, "A", "Prints RSS Configuration");
1313 
1314 	/* Runtime trace controls (global) */
1315 	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug",
1316 	    CTLFLAG_RW, &dbg_level_, 0,
1317 	    "Trace verbosity: 0=off, 3=err, 4=+warn, 5=+trace, 6=+detail");
1318 	SYSCTL_ADD_U32(ctx, child, OID_AUTO, "debug_categories",
1319 	    CTLFLAG_RW, &dbg_categories_, 0,
1320 	    "Trace category mask: init=1 config=2 tx=4 rx=8 intr=16 fw=32");
1321 
1322 	/* Driver Statistics */
1323 	for (int i = 0; i < softc->tx_rings_count; i++) {
1324 		struct aq_ring *ring = softc->tx_rings[i];
1325 		snprintf(namebuf, QUEUE_NAME_LEN, "tx_queue%d", i);
1326 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
1327 		    CTLFLAG_RD, NULL, "Queue Name");
1328 		queue_list = SYSCTL_CHILDREN(queue_node);
1329 
1330 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "tx_pkts",
1331 		    CTLFLAG_RD, &(ring->stats.tx_pkts), "TX Packets");
1332 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "tx_bytes",
1333 		    CTLFLAG_RD, &(ring->stats.tx_bytes), "TX Octets");
1334 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "tx_head",
1335 		    CTLTYPE_UINT | CTLFLAG_RD, ring, 0,
1336 		    aq_sysctl_print_tx_head, "IU", "ring head pointer");
1337 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "tx_tail",
1338 		    CTLTYPE_UINT | CTLFLAG_RD, ring, 0,
1339 		aq_sysctl_print_tx_tail, "IU", "ring tail pointer");
1340 	}
1341 
1342 	for (int i = 0; i < softc->rx_rings_count; i++) {
1343 		struct aq_ring *ring = softc->rx_rings[i];
1344 		snprintf(namebuf, QUEUE_NAME_LEN, "rx_queue%d", i);
1345 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
1346 		    CTLFLAG_RD, NULL, "Queue Name");
1347 		queue_list = SYSCTL_CHILDREN(queue_node);
1348 
1349 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "rx_pkts",
1350 		    CTLFLAG_RD, &(ring->stats.rx_pkts), "RX Packets");
1351 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "rx_bytes",
1352 		    CTLFLAG_RD, &(ring->stats.rx_bytes), "TX Octets");
1353 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "rx_err",
1354 		    CTLFLAG_RD, &(ring->stats.rx_err), "RX Errors");
1355 		SYSCTL_ADD_COUNTER_U64(ctx, queue_list, OID_AUTO, "irq",
1356 		    CTLFLAG_RD, &(ring->stats.irq), "RX interrupts");
1357 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rx_head",
1358 		    CTLTYPE_UINT | CTLFLAG_RD, ring, 0,
1359 		aq_sysctl_print_rx_head, "IU", "ring head pointer");
1360 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rx_tail",
1361 		    CTLTYPE_UINT | CTLFLAG_RD, ring, 0,
1362 		aq_sysctl_print_rx_tail, "IU", " ring tail pointer");
1363 	}
1364 
1365 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac",
1366 	    CTLFLAG_RD, NULL, "Statistics (read from HW registers)");
1367 	stat_list = SYSCTL_CHILDREN(stat_node);
1368 
1369 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_rcvd",
1370 	    CTLFLAG_RD, &stats->prc, "Good Packets Received");
1371 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ucast_pkts_rcvd",
1372 	    CTLFLAG_RD, &stats->uprc, "Unicast Packets Received");
1373 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_rcvd",
1374 	    CTLFLAG_RD, &stats->mprc, "Multicast Packets Received");
1375 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_rcvd",
1376 	    CTLFLAG_RD, &stats->bprc, "Broadcast Packets Received");
1377 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rsc_pkts_rcvd",
1378 	    CTLFLAG_RD, &stats->cprc, "Coalesced Packets Received");
1379 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "err_pkts_rcvd",
1380 	    CTLFLAG_RD, &stats->erpr, "Errors of Packet Receive");
1381 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "drop_pkts_dma",
1382 	    CTLFLAG_RD, &stats->dpc, "Dropped Packets in DMA");
1383 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_rcvd",
1384 	    CTLFLAG_RD, &stats->brc, "Good Octets Received");
1385 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ucast_octets_rcvd",
1386 	    CTLFLAG_RD, &stats->ubrc, "Unicast Octets Received");
1387 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_octets_rcvd",
1388 	    CTLFLAG_RD, &stats->mbrc, "Multicast Octets Received");
1389 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_octets_rcvd",
1390 	    CTLFLAG_RD, &stats->bbrc, "Broadcast Octets Received");
1391 
1392 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
1393 	    CTLFLAG_RD, &stats->ptc, "Good Packets Transmitted");
1394 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ucast_pkts_txd",
1395 	    CTLFLAG_RD, &stats->uptc, "Unicast Packets Transmitted");
1396 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
1397 	    CTLFLAG_RD, &stats->mptc, "Multicast Packets Transmitted");
1398 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
1399 	    CTLFLAG_RD, &stats->bptc, "Broadcast Packets Transmitted");
1400 
1401 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "err_pkts_txd",
1402 	    CTLFLAG_RD, &stats->erpt, "Errors of Packet Transmit");
1403 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
1404 	    CTLFLAG_RD, &stats->btc, "Good Octets Transmitted");
1405 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ucast_octets_txd",
1406 	    CTLFLAG_RD, &stats->ubtc, "Unicast Octets Transmitted");
1407 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_octets_txd",
1408 	    CTLFLAG_RD, &stats->mbtc, "Multicast Octets Transmitted");
1409 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_octets_txd",
1410 	    CTLFLAG_RD, &stats->bbtc, "Broadcast Octets Transmitted");
1411 }
1412