xref: /freebsd/sys/dev/sfxge/sfxge.c (revision 0482974b2c021a8abbc044d3e2b1240250f791e7)
1e948693eSPhilip Paeps /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4929c7febSAndrew Rybchenko  * Copyright (c) 2010-2016 Solarflare Communications Inc.
5e948693eSPhilip Paeps  * All rights reserved.
6e948693eSPhilip Paeps  *
7e948693eSPhilip Paeps  * This software was developed in part by Philip Paeps under contract for
8e948693eSPhilip Paeps  * Solarflare Communications, Inc.
9e948693eSPhilip Paeps  *
10e948693eSPhilip Paeps  * Redistribution and use in source and binary forms, with or without
113c838a9fSAndrew Rybchenko  * modification, are permitted provided that the following conditions are met:
12e948693eSPhilip Paeps  *
133c838a9fSAndrew Rybchenko  * 1. Redistributions of source code must retain the above copyright notice,
143c838a9fSAndrew Rybchenko  *    this list of conditions and the following disclaimer.
153c838a9fSAndrew Rybchenko  * 2. Redistributions in binary form must reproduce the above copyright notice,
163c838a9fSAndrew Rybchenko  *    this list of conditions and the following disclaimer in the documentation
173c838a9fSAndrew Rybchenko  *    and/or other materials provided with the distribution.
183c838a9fSAndrew Rybchenko  *
193c838a9fSAndrew Rybchenko  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
203c838a9fSAndrew Rybchenko  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
213c838a9fSAndrew Rybchenko  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223c838a9fSAndrew Rybchenko  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
233c838a9fSAndrew Rybchenko  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
243c838a9fSAndrew Rybchenko  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
253c838a9fSAndrew Rybchenko  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
263c838a9fSAndrew Rybchenko  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
273c838a9fSAndrew Rybchenko  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
283c838a9fSAndrew Rybchenko  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
293c838a9fSAndrew Rybchenko  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
303c838a9fSAndrew Rybchenko  *
313c838a9fSAndrew Rybchenko  * The views and conclusions contained in the software and documentation are
323c838a9fSAndrew Rybchenko  * those of the authors and should not be interpreted as representing official
333c838a9fSAndrew Rybchenko  * policies, either expressed or implied, of the FreeBSD Project.
34e948693eSPhilip Paeps  */
35e948693eSPhilip Paeps 
36e948693eSPhilip Paeps #include <sys/cdefs.h>
378e7d3206SAndrew Rybchenko #include "opt_rss.h"
388e7d3206SAndrew Rybchenko 
39e948693eSPhilip Paeps #include <sys/param.h>
40e948693eSPhilip Paeps #include <sys/kernel.h>
41e948693eSPhilip Paeps #include <sys/bus.h>
42e948693eSPhilip Paeps #include <sys/rman.h>
43e948693eSPhilip Paeps #include <sys/lock.h>
44e948693eSPhilip Paeps #include <sys/module.h>
45e948693eSPhilip Paeps #include <sys/mutex.h>
46e948693eSPhilip Paeps #include <sys/smp.h>
47e948693eSPhilip Paeps #include <sys/socket.h>
48e948693eSPhilip Paeps #include <sys/taskqueue.h>
49e948693eSPhilip Paeps #include <sys/sockio.h>
50e948693eSPhilip Paeps #include <sys/sysctl.h>
513c838a9fSAndrew Rybchenko #include <sys/priv.h>
52385b1d8eSGeorge V. Neville-Neil #include <sys/syslog.h>
53e948693eSPhilip Paeps 
54e948693eSPhilip Paeps #include <dev/pci/pcireg.h>
55e948693eSPhilip Paeps #include <dev/pci/pcivar.h>
56e948693eSPhilip Paeps 
57e948693eSPhilip Paeps #include <net/ethernet.h>
58e948693eSPhilip Paeps #include <net/if.h>
5976039bc8SGleb Smirnoff #include <net/if_var.h>
60e948693eSPhilip Paeps #include <net/if_media.h>
61e948693eSPhilip Paeps #include <net/if_types.h>
62e948693eSPhilip Paeps 
638e7d3206SAndrew Rybchenko #ifdef RSS
648e7d3206SAndrew Rybchenko #include <net/rss_config.h>
658e7d3206SAndrew Rybchenko #endif
668e7d3206SAndrew Rybchenko 
67e948693eSPhilip Paeps #include "common/efx.h"
68e948693eSPhilip Paeps 
69e948693eSPhilip Paeps #include "sfxge.h"
70e948693eSPhilip Paeps #include "sfxge_rx.h"
713c838a9fSAndrew Rybchenko #include "sfxge_ioc.h"
72ebfb5f25SAndrew Rybchenko #include "sfxge_version.h"
73e948693eSPhilip Paeps 
74b2b5a4c0SAndrew Rybchenko #define	SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM |			\
75921f3bd2SAndrew Rybchenko 		   IFCAP_RXCSUM | IFCAP_TXCSUM |			\
76db7e53aaSAndrew Rybchenko 		   IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 |		\
77921f3bd2SAndrew Rybchenko 		   IFCAP_TSO4 | IFCAP_TSO6 |				\
788a343f93SAndrew Rybchenko 		   IFCAP_JUMBO_MTU |					\
793d8fce27SAndrew Rybchenko 		   IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS)
80e948693eSPhilip Paeps #define	SFXGE_CAP_ENABLE SFXGE_CAP
814cefd524SAndrew Rybchenko #define	SFXGE_CAP_FIXED (IFCAP_VLAN_MTU |				\
823d8fce27SAndrew Rybchenko 			 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS)
83e948693eSPhilip Paeps 
84e948693eSPhilip Paeps MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
85e948693eSPhilip Paeps 
867029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
87385b1d8eSGeorge V. Neville-Neil     "SFXGE driver parameters");
88385b1d8eSGeorge V. Neville-Neil 
89385b1d8eSGeorge V. Neville-Neil #define	SFXGE_PARAM_RX_RING	SFXGE_PARAM(rx_ring)
90385b1d8eSGeorge V. Neville-Neil static int sfxge_rx_ring_entries = SFXGE_NDESCS;
91385b1d8eSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
92385b1d8eSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
93385b1d8eSGeorge V. Neville-Neil 	   &sfxge_rx_ring_entries, 0,
94385b1d8eSGeorge V. Neville-Neil 	   "Maximum number of descriptors in a receive ring");
95385b1d8eSGeorge V. Neville-Neil 
96385b1d8eSGeorge V. Neville-Neil #define	SFXGE_PARAM_TX_RING	SFXGE_PARAM(tx_ring)
97385b1d8eSGeorge V. Neville-Neil static int sfxge_tx_ring_entries = SFXGE_NDESCS;
98385b1d8eSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
99385b1d8eSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
100385b1d8eSGeorge V. Neville-Neil 	   &sfxge_tx_ring_entries, 0,
101385b1d8eSGeorge V. Neville-Neil 	   "Maximum number of descriptors in a transmit ring");
102385b1d8eSGeorge V. Neville-Neil 
1033b5966d1SAndrew Rybchenko #define	SFXGE_PARAM_RESTART_ATTEMPTS	SFXGE_PARAM(restart_attempts)
1043b5966d1SAndrew Rybchenko static int sfxge_restart_attempts = 3;
1053b5966d1SAndrew Rybchenko TUNABLE_INT(SFXGE_PARAM_RESTART_ATTEMPTS, &sfxge_restart_attempts);
1063b5966d1SAndrew Rybchenko SYSCTL_INT(_hw_sfxge, OID_AUTO, restart_attempts, CTLFLAG_RDTUN,
1073b5966d1SAndrew Rybchenko 	   &sfxge_restart_attempts, 0,
1083b5966d1SAndrew Rybchenko 	   "Maximum number of attempts to bring interface up after reset");
1093b5966d1SAndrew Rybchenko 
1109c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
1119c2c444bSAndrew Rybchenko #define	SFXGE_PARAM_MCDI_LOGGING	SFXGE_PARAM(mcdi_logging)
1129c2c444bSAndrew Rybchenko static int sfxge_mcdi_logging = 0;
1139c2c444bSAndrew Rybchenko TUNABLE_INT(SFXGE_PARAM_MCDI_LOGGING, &sfxge_mcdi_logging);
1149c2c444bSAndrew Rybchenko #endif
1159c2c444bSAndrew Rybchenko 
116e948693eSPhilip Paeps static void
117e948693eSPhilip Paeps sfxge_reset(void *arg, int npending);
118e948693eSPhilip Paeps 
119e948693eSPhilip Paeps static int
sfxge_estimate_rsrc_limits(struct sfxge_softc * sc)1203c838a9fSAndrew Rybchenko sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
1213c838a9fSAndrew Rybchenko {
1223c838a9fSAndrew Rybchenko 	efx_drv_limits_t limits;
1233c838a9fSAndrew Rybchenko 	int rc;
1243c838a9fSAndrew Rybchenko 	unsigned int evq_max;
1253c838a9fSAndrew Rybchenko 	uint32_t evq_allocated;
1263c838a9fSAndrew Rybchenko 	uint32_t rxq_allocated;
1273c838a9fSAndrew Rybchenko 	uint32_t txq_allocated;
1283c838a9fSAndrew Rybchenko 
1293c838a9fSAndrew Rybchenko 	/*
1303c838a9fSAndrew Rybchenko 	 * Limit the number of event queues to:
1313c838a9fSAndrew Rybchenko 	 *  - number of CPUs
1323c838a9fSAndrew Rybchenko 	 *  - hardwire maximum RSS channels
1333c838a9fSAndrew Rybchenko 	 *  - administratively specified maximum RSS channels
1343c838a9fSAndrew Rybchenko 	 */
1358e7d3206SAndrew Rybchenko #ifdef RSS
1368e7d3206SAndrew Rybchenko 	/*
1378e7d3206SAndrew Rybchenko 	 * Avoid extra limitations so that the number of queues
1388e7d3206SAndrew Rybchenko 	 * may be configured at administrator's will
1398e7d3206SAndrew Rybchenko 	 */
1408e7d3206SAndrew Rybchenko 	evq_max = MIN(MAX(rss_getnumbuckets(), 1), EFX_MAXRSS);
1418e7d3206SAndrew Rybchenko #else
1423c838a9fSAndrew Rybchenko 	evq_max = MIN(mp_ncpus, EFX_MAXRSS);
1438e7d3206SAndrew Rybchenko #endif
1443c838a9fSAndrew Rybchenko 	if (sc->max_rss_channels > 0)
1453c838a9fSAndrew Rybchenko 		evq_max = MIN(evq_max, sc->max_rss_channels);
1463c838a9fSAndrew Rybchenko 
1473c838a9fSAndrew Rybchenko 	memset(&limits, 0, sizeof(limits));
1483c838a9fSAndrew Rybchenko 
1493c838a9fSAndrew Rybchenko 	limits.edl_min_evq_count = 1;
1503c838a9fSAndrew Rybchenko 	limits.edl_max_evq_count = evq_max;
151e4b0a127SAndrew Rybchenko 	limits.edl_min_txq_count = SFXGE_EVQ0_N_TXQ(sc);
152e4b0a127SAndrew Rybchenko 	limits.edl_max_txq_count = evq_max + SFXGE_EVQ0_N_TXQ(sc) - 1;
1533c838a9fSAndrew Rybchenko 	limits.edl_min_rxq_count = 1;
1543c838a9fSAndrew Rybchenko 	limits.edl_max_rxq_count = evq_max;
1553c838a9fSAndrew Rybchenko 
1563c838a9fSAndrew Rybchenko 	efx_nic_set_drv_limits(sc->enp, &limits);
1573c838a9fSAndrew Rybchenko 
1583c838a9fSAndrew Rybchenko 	if ((rc = efx_nic_init(sc->enp)) != 0)
1593c838a9fSAndrew Rybchenko 		return (rc);
1603c838a9fSAndrew Rybchenko 
1613c838a9fSAndrew Rybchenko 	rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
1623c838a9fSAndrew Rybchenko 				 &txq_allocated);
1633c838a9fSAndrew Rybchenko 	if (rc != 0) {
1643c838a9fSAndrew Rybchenko 		efx_nic_fini(sc->enp);
1653c838a9fSAndrew Rybchenko 		return (rc);
1663c838a9fSAndrew Rybchenko 	}
1673c838a9fSAndrew Rybchenko 
168e4b0a127SAndrew Rybchenko 	KASSERT(txq_allocated >= SFXGE_EVQ0_N_TXQ(sc),
169e4b0a127SAndrew Rybchenko 		("txq_allocated < %u", SFXGE_EVQ0_N_TXQ(sc)));
1703c838a9fSAndrew Rybchenko 
1713c838a9fSAndrew Rybchenko 	sc->evq_max = MIN(evq_allocated, evq_max);
1723c838a9fSAndrew Rybchenko 	sc->evq_max = MIN(rxq_allocated, sc->evq_max);
173e4b0a127SAndrew Rybchenko 	sc->evq_max = MIN(txq_allocated - (SFXGE_EVQ0_N_TXQ(sc) - 1),
1743c838a9fSAndrew Rybchenko 			  sc->evq_max);
1753c838a9fSAndrew Rybchenko 
1763c838a9fSAndrew Rybchenko 	KASSERT(sc->evq_max <= evq_max,
1773c838a9fSAndrew Rybchenko 		("allocated more than maximum requested"));
1783c838a9fSAndrew Rybchenko 
1798e7d3206SAndrew Rybchenko #ifdef RSS
1808e7d3206SAndrew Rybchenko 	if (sc->evq_max < rss_getnumbuckets())
1818e7d3206SAndrew Rybchenko 		device_printf(sc->dev, "The number of allocated queues (%u) "
1828e7d3206SAndrew Rybchenko 			      "is less than the number of RSS buckets (%u); "
1838e7d3206SAndrew Rybchenko 			      "performance degradation might be observed",
1848e7d3206SAndrew Rybchenko 			      sc->evq_max, rss_getnumbuckets());
1858e7d3206SAndrew Rybchenko #endif
1868e7d3206SAndrew Rybchenko 
1873c838a9fSAndrew Rybchenko 	/*
1883c838a9fSAndrew Rybchenko 	 * NIC is kept initialized in the case of success to be able to
1893c838a9fSAndrew Rybchenko 	 * initialize port to find out media types.
1903c838a9fSAndrew Rybchenko 	 */
1913c838a9fSAndrew Rybchenko 	return (0);
1923c838a9fSAndrew Rybchenko }
1933c838a9fSAndrew Rybchenko 
1943c838a9fSAndrew Rybchenko static int
sfxge_set_drv_limits(struct sfxge_softc * sc)1953c838a9fSAndrew Rybchenko sfxge_set_drv_limits(struct sfxge_softc *sc)
1963c838a9fSAndrew Rybchenko {
1973c838a9fSAndrew Rybchenko 	efx_drv_limits_t limits;
1983c838a9fSAndrew Rybchenko 
1993c838a9fSAndrew Rybchenko 	memset(&limits, 0, sizeof(limits));
2003c838a9fSAndrew Rybchenko 
2013c838a9fSAndrew Rybchenko 	/* Limits are strict since take into account initial estimation */
2023c838a9fSAndrew Rybchenko 	limits.edl_min_evq_count = limits.edl_max_evq_count =
2033c838a9fSAndrew Rybchenko 	    sc->intr.n_alloc;
2043c838a9fSAndrew Rybchenko 	limits.edl_min_txq_count = limits.edl_max_txq_count =
205e4b0a127SAndrew Rybchenko 	    sc->intr.n_alloc + SFXGE_EVQ0_N_TXQ(sc) - 1;
2063c838a9fSAndrew Rybchenko 	limits.edl_min_rxq_count = limits.edl_max_rxq_count =
2073c838a9fSAndrew Rybchenko 	    sc->intr.n_alloc;
2083c838a9fSAndrew Rybchenko 
2093c838a9fSAndrew Rybchenko 	return (efx_nic_set_drv_limits(sc->enp, &limits));
2103c838a9fSAndrew Rybchenko }
2113c838a9fSAndrew Rybchenko 
2123c838a9fSAndrew Rybchenko static int
sfxge_start(struct sfxge_softc * sc)213e948693eSPhilip Paeps sfxge_start(struct sfxge_softc *sc)
214e948693eSPhilip Paeps {
215e948693eSPhilip Paeps 	int rc;
216e948693eSPhilip Paeps 
217763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
218e948693eSPhilip Paeps 
219e948693eSPhilip Paeps 	if (sc->init_state == SFXGE_STARTED)
220b7b0edd1SGeorge V. Neville-Neil 		return (0);
221e948693eSPhilip Paeps 
222e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_REGISTERED) {
223e948693eSPhilip Paeps 		rc = EINVAL;
224e948693eSPhilip Paeps 		goto fail;
225e948693eSPhilip Paeps 	}
226e948693eSPhilip Paeps 
2273c838a9fSAndrew Rybchenko 	/* Set required resource limits */
2283c838a9fSAndrew Rybchenko 	if ((rc = sfxge_set_drv_limits(sc)) != 0)
2293c838a9fSAndrew Rybchenko 		goto fail;
2303c838a9fSAndrew Rybchenko 
231e948693eSPhilip Paeps 	if ((rc = efx_nic_init(sc->enp)) != 0)
232e948693eSPhilip Paeps 		goto fail;
233e948693eSPhilip Paeps 
234e948693eSPhilip Paeps 	/* Start processing interrupts. */
235e948693eSPhilip Paeps 	if ((rc = sfxge_intr_start(sc)) != 0)
236e948693eSPhilip Paeps 		goto fail2;
237e948693eSPhilip Paeps 
238e948693eSPhilip Paeps 	/* Start processing events. */
239e948693eSPhilip Paeps 	if ((rc = sfxge_ev_start(sc)) != 0)
240e948693eSPhilip Paeps 		goto fail3;
241e948693eSPhilip Paeps 
2423c838a9fSAndrew Rybchenko 	/* Fire up the port. */
2433c838a9fSAndrew Rybchenko 	if ((rc = sfxge_port_start(sc)) != 0)
2443c838a9fSAndrew Rybchenko 		goto fail4;
2453c838a9fSAndrew Rybchenko 
246e948693eSPhilip Paeps 	/* Start the receiver side. */
247e948693eSPhilip Paeps 	if ((rc = sfxge_rx_start(sc)) != 0)
2483c838a9fSAndrew Rybchenko 		goto fail5;
249e948693eSPhilip Paeps 
250e948693eSPhilip Paeps 	/* Start the transmitter side. */
251e948693eSPhilip Paeps 	if ((rc = sfxge_tx_start(sc)) != 0)
252e948693eSPhilip Paeps 		goto fail6;
253e948693eSPhilip Paeps 
254e948693eSPhilip Paeps 	sc->init_state = SFXGE_STARTED;
255e948693eSPhilip Paeps 
256e948693eSPhilip Paeps 	/* Tell the stack we're running. */
25704abf87bSJustin Hibbits 	if_setdrvflagbits(sc->ifnet, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
258e948693eSPhilip Paeps 
259e948693eSPhilip Paeps 	return (0);
260e948693eSPhilip Paeps 
261e948693eSPhilip Paeps fail6:
2623c838a9fSAndrew Rybchenko 	sfxge_rx_stop(sc);
263e948693eSPhilip Paeps 
264e948693eSPhilip Paeps fail5:
2653c838a9fSAndrew Rybchenko 	sfxge_port_stop(sc);
266e948693eSPhilip Paeps 
267e948693eSPhilip Paeps fail4:
268e948693eSPhilip Paeps 	sfxge_ev_stop(sc);
269e948693eSPhilip Paeps 
270e948693eSPhilip Paeps fail3:
271e948693eSPhilip Paeps 	sfxge_intr_stop(sc);
272e948693eSPhilip Paeps 
273e948693eSPhilip Paeps fail2:
274e948693eSPhilip Paeps 	efx_nic_fini(sc->enp);
275e948693eSPhilip Paeps 
276e948693eSPhilip Paeps fail:
277e948693eSPhilip Paeps 	device_printf(sc->dev, "sfxge_start: %d\n", rc);
278e948693eSPhilip Paeps 
279e948693eSPhilip Paeps 	return (rc);
280e948693eSPhilip Paeps }
281e948693eSPhilip Paeps 
282e948693eSPhilip Paeps static void
sfxge_if_init(void * arg)283e948693eSPhilip Paeps sfxge_if_init(void *arg)
284e948693eSPhilip Paeps {
285e948693eSPhilip Paeps 	struct sfxge_softc *sc;
286e948693eSPhilip Paeps 
287e948693eSPhilip Paeps 	sc = (struct sfxge_softc *)arg;
288e948693eSPhilip Paeps 
289763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
290e948693eSPhilip Paeps 	(void)sfxge_start(sc);
291763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
292e948693eSPhilip Paeps }
293e948693eSPhilip Paeps 
294e948693eSPhilip Paeps static void
sfxge_stop(struct sfxge_softc * sc)295e948693eSPhilip Paeps sfxge_stop(struct sfxge_softc *sc)
296e948693eSPhilip Paeps {
297763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
298e948693eSPhilip Paeps 
299e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_STARTED)
300e948693eSPhilip Paeps 		return;
301e948693eSPhilip Paeps 
302e948693eSPhilip Paeps 	sc->init_state = SFXGE_REGISTERED;
303e948693eSPhilip Paeps 
304e948693eSPhilip Paeps 	/* Stop the transmitter. */
305e948693eSPhilip Paeps 	sfxge_tx_stop(sc);
306e948693eSPhilip Paeps 
307e948693eSPhilip Paeps 	/* Stop the receiver. */
308e948693eSPhilip Paeps 	sfxge_rx_stop(sc);
309e948693eSPhilip Paeps 
3103c838a9fSAndrew Rybchenko 	/* Stop the port. */
3113c838a9fSAndrew Rybchenko 	sfxge_port_stop(sc);
3123c838a9fSAndrew Rybchenko 
313e948693eSPhilip Paeps 	/* Stop processing events. */
314e948693eSPhilip Paeps 	sfxge_ev_stop(sc);
315e948693eSPhilip Paeps 
316e948693eSPhilip Paeps 	/* Stop processing interrupts. */
317e948693eSPhilip Paeps 	sfxge_intr_stop(sc);
318e948693eSPhilip Paeps 
319e948693eSPhilip Paeps 	efx_nic_fini(sc->enp);
320e948693eSPhilip Paeps 
32104abf87bSJustin Hibbits 	if_setdrvflagbits(sc->ifnet, 0, IFF_DRV_RUNNING);
322e948693eSPhilip Paeps }
323e948693eSPhilip Paeps 
3243c838a9fSAndrew Rybchenko static int
sfxge_vpd_ioctl(struct sfxge_softc * sc,sfxge_ioc_t * ioc)3253c838a9fSAndrew Rybchenko sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
3263c838a9fSAndrew Rybchenko {
3273c838a9fSAndrew Rybchenko 	efx_vpd_value_t value;
3283c838a9fSAndrew Rybchenko 	int rc = 0;
3293c838a9fSAndrew Rybchenko 
3303c838a9fSAndrew Rybchenko 	switch (ioc->u.vpd.op) {
3313c838a9fSAndrew Rybchenko 	case SFXGE_VPD_OP_GET_KEYWORD:
3323c838a9fSAndrew Rybchenko 		value.evv_tag = ioc->u.vpd.tag;
3333c838a9fSAndrew Rybchenko 		value.evv_keyword = ioc->u.vpd.keyword;
3343c838a9fSAndrew Rybchenko 		rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
3353c838a9fSAndrew Rybchenko 		if (rc != 0)
3363c838a9fSAndrew Rybchenko 			break;
3373c838a9fSAndrew Rybchenko 		ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
3383c838a9fSAndrew Rybchenko 		if (ioc->u.vpd.payload != 0) {
3393c838a9fSAndrew Rybchenko 			rc = copyout(value.evv_value, ioc->u.vpd.payload,
3403c838a9fSAndrew Rybchenko 				     ioc->u.vpd.len);
3413c838a9fSAndrew Rybchenko 		}
3423c838a9fSAndrew Rybchenko 		break;
3433c838a9fSAndrew Rybchenko 	case SFXGE_VPD_OP_SET_KEYWORD:
3443c838a9fSAndrew Rybchenko 		if (ioc->u.vpd.len > sizeof(value.evv_value))
3453c838a9fSAndrew Rybchenko 			return (EINVAL);
3463c838a9fSAndrew Rybchenko 		value.evv_tag = ioc->u.vpd.tag;
3473c838a9fSAndrew Rybchenko 		value.evv_keyword = ioc->u.vpd.keyword;
3483c838a9fSAndrew Rybchenko 		value.evv_length = ioc->u.vpd.len;
3493c838a9fSAndrew Rybchenko 		rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
3503c838a9fSAndrew Rybchenko 		if (rc != 0)
3513c838a9fSAndrew Rybchenko 			break;
3523c838a9fSAndrew Rybchenko 		rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
3533c838a9fSAndrew Rybchenko 		if (rc != 0)
3543c838a9fSAndrew Rybchenko 			break;
3553c838a9fSAndrew Rybchenko 		rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
3563c838a9fSAndrew Rybchenko 		if (rc != 0)
3573c838a9fSAndrew Rybchenko 			break;
3583c838a9fSAndrew Rybchenko 		rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
3593c838a9fSAndrew Rybchenko 		break;
3603c838a9fSAndrew Rybchenko 	default:
3613c838a9fSAndrew Rybchenko 		rc = EOPNOTSUPP;
3623c838a9fSAndrew Rybchenko 		break;
3633c838a9fSAndrew Rybchenko 	}
3643c838a9fSAndrew Rybchenko 
3653c838a9fSAndrew Rybchenko 	return (rc);
3663c838a9fSAndrew Rybchenko }
3673c838a9fSAndrew Rybchenko 
3683c838a9fSAndrew Rybchenko static int
sfxge_private_ioctl(struct sfxge_softc * sc,sfxge_ioc_t * ioc)3693c838a9fSAndrew Rybchenko sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
3703c838a9fSAndrew Rybchenko {
3713c838a9fSAndrew Rybchenko 	switch (ioc->op) {
3723c838a9fSAndrew Rybchenko 	case SFXGE_MCDI_IOC:
3733c838a9fSAndrew Rybchenko 		return (sfxge_mcdi_ioctl(sc, ioc));
3743c838a9fSAndrew Rybchenko 	case SFXGE_NVRAM_IOC:
3753c838a9fSAndrew Rybchenko 		return (sfxge_nvram_ioctl(sc, ioc));
3763c838a9fSAndrew Rybchenko 	case SFXGE_VPD_IOC:
3773c838a9fSAndrew Rybchenko 		return (sfxge_vpd_ioctl(sc, ioc));
3783c838a9fSAndrew Rybchenko 	default:
3793c838a9fSAndrew Rybchenko 		return (EOPNOTSUPP);
3803c838a9fSAndrew Rybchenko 	}
3813c838a9fSAndrew Rybchenko }
3823c838a9fSAndrew Rybchenko 
383e948693eSPhilip Paeps static int
sfxge_if_ioctl(if_t ifp,unsigned long command,caddr_t data)38404abf87bSJustin Hibbits sfxge_if_ioctl(if_t ifp, unsigned long command, caddr_t data)
385e948693eSPhilip Paeps {
386e948693eSPhilip Paeps 	struct sfxge_softc *sc;
387e948693eSPhilip Paeps 	struct ifreq *ifr;
3883c838a9fSAndrew Rybchenko 	sfxge_ioc_t ioc;
389e948693eSPhilip Paeps 	int error;
390e948693eSPhilip Paeps 
391e948693eSPhilip Paeps 	ifr = (struct ifreq *)data;
39204abf87bSJustin Hibbits 	sc = if_getsoftc(ifp);
393e948693eSPhilip Paeps 	error = 0;
394e948693eSPhilip Paeps 
395e948693eSPhilip Paeps 	switch (command) {
396e948693eSPhilip Paeps 	case SIOCSIFFLAGS:
397763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
39804abf87bSJustin Hibbits 		if (if_getflags(ifp) & IFF_UP) {
39904abf87bSJustin Hibbits 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
40004abf87bSJustin Hibbits 				if ((if_getflags(ifp) ^ sc->if_flags) &
401e948693eSPhilip Paeps 				    (IFF_PROMISC | IFF_ALLMULTI)) {
402e948693eSPhilip Paeps 					sfxge_mac_filter_set(sc);
403e948693eSPhilip Paeps 				}
404e948693eSPhilip Paeps 			} else
405e948693eSPhilip Paeps 				sfxge_start(sc);
406e948693eSPhilip Paeps 		} else
40704abf87bSJustin Hibbits 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
408e948693eSPhilip Paeps 				sfxge_stop(sc);
40904abf87bSJustin Hibbits 		sc->if_flags = if_getflags(ifp);
410763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
411e948693eSPhilip Paeps 		break;
412e948693eSPhilip Paeps 	case SIOCSIFMTU:
41304abf87bSJustin Hibbits 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
414e948693eSPhilip Paeps 			/* Nothing to do */
415e948693eSPhilip Paeps 			error = 0;
416e948693eSPhilip Paeps 		} else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
417e948693eSPhilip Paeps 			error = EINVAL;
41804abf87bSJustin Hibbits 		} else if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
41904abf87bSJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
420e948693eSPhilip Paeps 			error = 0;
421e948693eSPhilip Paeps 		} else {
422e948693eSPhilip Paeps 			/* Restart required */
423763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_LOCK(sc);
424e948693eSPhilip Paeps 			sfxge_stop(sc);
42504abf87bSJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
426e948693eSPhilip Paeps 			error = sfxge_start(sc);
427763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
428b7b0edd1SGeorge V. Neville-Neil 			if (error != 0) {
42904abf87bSJustin Hibbits 				if_setflagbits(ifp, 0, IFF_UP);
43004abf87bSJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
431e948693eSPhilip Paeps 				if_down(ifp);
432e948693eSPhilip Paeps 			}
433e948693eSPhilip Paeps 		}
434e948693eSPhilip Paeps 		break;
435e948693eSPhilip Paeps 	case SIOCADDMULTI:
436e948693eSPhilip Paeps 	case SIOCDELMULTI:
43704abf87bSJustin Hibbits 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
438e948693eSPhilip Paeps 			sfxge_mac_filter_set(sc);
439e948693eSPhilip Paeps 		break;
440e948693eSPhilip Paeps 	case SIOCSIFCAP:
441ef97c04bSAndrew Rybchenko 	{
442ef97c04bSAndrew Rybchenko 		int reqcap = ifr->ifr_reqcap;
443ef97c04bSAndrew Rybchenko 		int capchg_mask;
444ef97c04bSAndrew Rybchenko 
445763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
446e948693eSPhilip Paeps 
447ef97c04bSAndrew Rybchenko 		/* Capabilities to be changed in accordance with request */
44804abf87bSJustin Hibbits 		capchg_mask = if_getcapenable(ifp) ^ reqcap;
449ef97c04bSAndrew Rybchenko 
450e948693eSPhilip Paeps 		/*
451e948693eSPhilip Paeps 		 * The networking core already rejects attempts to
452e948693eSPhilip Paeps 		 * enable capabilities we don't have.  We still have
453e948693eSPhilip Paeps 		 * to reject attempts to disable capabilities that we
454e948693eSPhilip Paeps 		 * can't (yet) disable.
455e948693eSPhilip Paeps 		 */
45604abf87bSJustin Hibbits 		KASSERT((reqcap & ~if_getcapabilities(ifp)) == 0,
4573c838a9fSAndrew Rybchenko 		    ("Unsupported capabilities 0x%x requested 0x%x vs "
4583c838a9fSAndrew Rybchenko 		     "supported 0x%x",
45904abf87bSJustin Hibbits 		     reqcap & ~if_getcapabilities(ifp),
46004abf87bSJustin Hibbits 		     reqcap , if_getcapabilities(ifp)));
461ef97c04bSAndrew Rybchenko 		if (capchg_mask & SFXGE_CAP_FIXED) {
462e948693eSPhilip Paeps 			error = EINVAL;
463763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
464e948693eSPhilip Paeps 			break;
465e948693eSPhilip Paeps 		}
466e948693eSPhilip Paeps 
467921f3bd2SAndrew Rybchenko 		/* Check request before any changes */
468921f3bd2SAndrew Rybchenko 		if ((capchg_mask & IFCAP_TSO4) &&
469921f3bd2SAndrew Rybchenko 		    (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
470921f3bd2SAndrew Rybchenko 			error = EAGAIN;
471921f3bd2SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
472921f3bd2SAndrew Rybchenko 			if_printf(ifp, "enable txcsum before tso4\n");
473921f3bd2SAndrew Rybchenko 			break;
474921f3bd2SAndrew Rybchenko 		}
475921f3bd2SAndrew Rybchenko 		if ((capchg_mask & IFCAP_TSO6) &&
476921f3bd2SAndrew Rybchenko 		    (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
477921f3bd2SAndrew Rybchenko 			error = EAGAIN;
478921f3bd2SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
479921f3bd2SAndrew Rybchenko 			if_printf(ifp, "enable txcsum6 before tso6\n");
480921f3bd2SAndrew Rybchenko 			break;
481921f3bd2SAndrew Rybchenko 		}
482921f3bd2SAndrew Rybchenko 
483921f3bd2SAndrew Rybchenko 		if (reqcap & IFCAP_TXCSUM) {
48404abf87bSJustin Hibbits 			if_sethwassistbits(ifp, (CSUM_IP | CSUM_TCP | CSUM_UDP), 0);
485921f3bd2SAndrew Rybchenko 		} else {
48604abf87bSJustin Hibbits 			if_sethwassistbits(ifp, 0, (CSUM_IP | CSUM_TCP | CSUM_UDP));
487921f3bd2SAndrew Rybchenko 			if (reqcap & IFCAP_TSO4) {
488921f3bd2SAndrew Rybchenko 				reqcap &= ~IFCAP_TSO4;
489921f3bd2SAndrew Rybchenko 				if_printf(ifp,
490921f3bd2SAndrew Rybchenko 				    "tso4 disabled due to -txcsum\n");
491921f3bd2SAndrew Rybchenko 			}
492921f3bd2SAndrew Rybchenko 		}
493921f3bd2SAndrew Rybchenko 		if (reqcap & IFCAP_TXCSUM_IPV6) {
49404abf87bSJustin Hibbits 			if_sethwassistbits(ifp, (CSUM_TCP_IPV6 | CSUM_UDP_IPV6), 0);
495921f3bd2SAndrew Rybchenko 		} else {
49604abf87bSJustin Hibbits 			if_sethwassistbits(ifp, 0, (CSUM_TCP_IPV6 | CSUM_UDP_IPV6));
497921f3bd2SAndrew Rybchenko 			if (reqcap & IFCAP_TSO6) {
498921f3bd2SAndrew Rybchenko 				reqcap &= ~IFCAP_TSO6;
499921f3bd2SAndrew Rybchenko 				if_printf(ifp,
500921f3bd2SAndrew Rybchenko 				    "tso6 disabled due to -txcsum6\n");
501921f3bd2SAndrew Rybchenko 			}
502921f3bd2SAndrew Rybchenko 		}
50322bc53cbSAndrew Rybchenko 
50422bc53cbSAndrew Rybchenko 		/*
50522bc53cbSAndrew Rybchenko 		 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
50622bc53cbSAndrew Rybchenko 		 * account before using TSO. So, we do not touch
50722bc53cbSAndrew Rybchenko 		 * checksum flags when IFCAP_TSOx is modified.
50822bc53cbSAndrew Rybchenko 		 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
50922bc53cbSAndrew Rybchenko 		 * but both bits are set in IPv4 and IPv6 mbufs.
51022bc53cbSAndrew Rybchenko 		 */
511e948693eSPhilip Paeps 
51204abf87bSJustin Hibbits 		if_setcapenable(ifp, reqcap);
513ef97c04bSAndrew Rybchenko 
514763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
515e948693eSPhilip Paeps 		break;
516ef97c04bSAndrew Rybchenko 	}
517e948693eSPhilip Paeps 	case SIOCSIFMEDIA:
518e948693eSPhilip Paeps 	case SIOCGIFMEDIA:
519e948693eSPhilip Paeps 		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
520e948693eSPhilip Paeps 		break;
5211e76f8b8SAndrew Rybchenko #ifdef SIOCGI2C
5221e76f8b8SAndrew Rybchenko 	case SIOCGI2C:
5231e76f8b8SAndrew Rybchenko 	{
5241e76f8b8SAndrew Rybchenko 		struct ifi2creq i2c;
5251e76f8b8SAndrew Rybchenko 
526541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
5271e76f8b8SAndrew Rybchenko 		if (error != 0)
5281e76f8b8SAndrew Rybchenko 			break;
5291e76f8b8SAndrew Rybchenko 
5301e76f8b8SAndrew Rybchenko 		if (i2c.len > sizeof(i2c.data)) {
5311e76f8b8SAndrew Rybchenko 			error = EINVAL;
5321e76f8b8SAndrew Rybchenko 			break;
5331e76f8b8SAndrew Rybchenko 		}
5341e76f8b8SAndrew Rybchenko 
5351e76f8b8SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
5361e76f8b8SAndrew Rybchenko 		error = efx_phy_module_get_info(sc->enp, i2c.dev_addr,
5371e76f8b8SAndrew Rybchenko 						i2c.offset, i2c.len,
5381e76f8b8SAndrew Rybchenko 						&i2c.data[0]);
5391e76f8b8SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
5401e76f8b8SAndrew Rybchenko 		if (error == 0)
541541d96aaSBrooks Davis 			error = copyout(&i2c, ifr_data_get_ptr(ifr),
542541d96aaSBrooks Davis 			    sizeof(i2c));
5431e76f8b8SAndrew Rybchenko 		break;
5441e76f8b8SAndrew Rybchenko 	}
5451e76f8b8SAndrew Rybchenko #endif
5463c838a9fSAndrew Rybchenko 	case SIOCGPRIVATE_0:
5473c838a9fSAndrew Rybchenko 		error = priv_check(curthread, PRIV_DRIVER);
5483c838a9fSAndrew Rybchenko 		if (error != 0)
5493c838a9fSAndrew Rybchenko 			break;
550541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &ioc, sizeof(ioc));
5513c838a9fSAndrew Rybchenko 		if (error != 0)
5523c838a9fSAndrew Rybchenko 			return (error);
5533c838a9fSAndrew Rybchenko 		error = sfxge_private_ioctl(sc, &ioc);
5543c838a9fSAndrew Rybchenko 		if (error == 0) {
555541d96aaSBrooks Davis 			error = copyout(&ioc, ifr_data_get_ptr(ifr),
556541d96aaSBrooks Davis 			    sizeof(ioc));
5573c838a9fSAndrew Rybchenko 		}
5583c838a9fSAndrew Rybchenko 		break;
559e948693eSPhilip Paeps 	default:
560e948693eSPhilip Paeps 		error = ether_ioctl(ifp, command, data);
561e948693eSPhilip Paeps 	}
562e948693eSPhilip Paeps 
563e948693eSPhilip Paeps 	return (error);
564e948693eSPhilip Paeps }
565e948693eSPhilip Paeps 
566e948693eSPhilip Paeps static void
sfxge_ifnet_fini(if_t ifp)56704abf87bSJustin Hibbits sfxge_ifnet_fini(if_t ifp)
568e948693eSPhilip Paeps {
56904abf87bSJustin Hibbits 	struct sfxge_softc *sc = if_getsoftc(ifp);
570e948693eSPhilip Paeps 
571763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
572e948693eSPhilip Paeps 	sfxge_stop(sc);
573763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
574e948693eSPhilip Paeps 
575e948693eSPhilip Paeps 	ifmedia_removeall(&sc->media);
576e948693eSPhilip Paeps 	ether_ifdetach(ifp);
577e948693eSPhilip Paeps 	if_free(ifp);
578e948693eSPhilip Paeps }
579e948693eSPhilip Paeps 
580e948693eSPhilip Paeps static int
sfxge_ifnet_init(if_t ifp,struct sfxge_softc * sc)58104abf87bSJustin Hibbits sfxge_ifnet_init(if_t ifp, struct sfxge_softc *sc)
582e948693eSPhilip Paeps {
583e948693eSPhilip Paeps 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
584e948693eSPhilip Paeps 	device_t dev;
585e948693eSPhilip Paeps 	int rc;
586e948693eSPhilip Paeps 
587e948693eSPhilip Paeps 	dev = sc->dev;
588e948693eSPhilip Paeps 	sc->ifnet = ifp;
589e948693eSPhilip Paeps 
590e948693eSPhilip Paeps 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
59104abf87bSJustin Hibbits 	if_setinitfn(ifp, sfxge_if_init);
59204abf87bSJustin Hibbits 	if_setsoftc(ifp, sc);
59304abf87bSJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
59404abf87bSJustin Hibbits 	if_setioctlfn(ifp, sfxge_if_ioctl);
595e948693eSPhilip Paeps 
59604abf87bSJustin Hibbits 	if_setcapabilities(ifp, SFXGE_CAP);
59704abf87bSJustin Hibbits 	if_setcapenable(ifp, SFXGE_CAP_ENABLE);
59804abf87bSJustin Hibbits 	if_sethwtsomax(ifp, SFXGE_TSO_MAX_SIZE);
59904abf87bSJustin Hibbits 	if_sethwtsomaxsegcount(ifp, SFXGE_TX_MAPPING_MAX_SEG);
60004abf87bSJustin Hibbits 	if_sethwtsomaxsegsize(ifp, PAGE_SIZE);
6018a343f93SAndrew Rybchenko 
6028a343f93SAndrew Rybchenko #ifdef SFXGE_LRO
60304abf87bSJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_LRO, 0);
60404abf87bSJustin Hibbits 	if_setcapenablebit(ifp, IFCAP_LRO, 0);
6058a343f93SAndrew Rybchenko #endif
6068a343f93SAndrew Rybchenko 
6073c838a9fSAndrew Rybchenko 	if (encp->enc_hw_tx_insert_vlan_enabled) {
60804abf87bSJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING, 0);
60904abf87bSJustin Hibbits 		if_setcapenablebit(ifp, IFCAP_VLAN_HWTAGGING, 0);
6103c838a9fSAndrew Rybchenko 	}
61104abf87bSJustin Hibbits 	if_sethwassistbits(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
61204abf87bSJustin Hibbits 			   CSUM_TCP_IPV6 | CSUM_UDP_IPV6, 0);
613e948693eSPhilip Paeps 
61404abf87bSJustin Hibbits 	if_settransmitfn(ifp, sfxge_if_transmit);
61504abf87bSJustin Hibbits 	if_setqflushfn(ifp, sfxge_if_qflush);
616e948693eSPhilip Paeps 
61704abf87bSJustin Hibbits 	if_setgetcounterfn(ifp, sfxge_get_counter);
6183d8fce27SAndrew Rybchenko 
6193c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "ifmedia_init");
620e948693eSPhilip Paeps 	if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
621*0482974bSFranco Fichtner 		return (rc);
622*0482974bSFranco Fichtner 
623*0482974bSFranco Fichtner 	ether_ifattach(ifp, encp->enc_mac_addr);
624e948693eSPhilip Paeps 
625b7b0edd1SGeorge V. Neville-Neil 	return (0);
626e948693eSPhilip Paeps }
627e948693eSPhilip Paeps 
628e948693eSPhilip Paeps void
sfxge_sram_buf_tbl_alloc(struct sfxge_softc * sc,size_t n,uint32_t * idp)629e948693eSPhilip Paeps sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
630e948693eSPhilip Paeps {
631e948693eSPhilip Paeps 	KASSERT(sc->buffer_table_next + n <=
632e948693eSPhilip Paeps 		efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
633e948693eSPhilip Paeps 		("buffer table full"));
634e948693eSPhilip Paeps 
635e948693eSPhilip Paeps 	*idp = sc->buffer_table_next;
636e948693eSPhilip Paeps 	sc->buffer_table_next += n;
637e948693eSPhilip Paeps }
638e948693eSPhilip Paeps 
639e948693eSPhilip Paeps static int
sfxge_bar_init(struct sfxge_softc * sc)640e948693eSPhilip Paeps sfxge_bar_init(struct sfxge_softc *sc)
641e948693eSPhilip Paeps {
642e948693eSPhilip Paeps 	efsys_bar_t *esbp = &sc->bar;
643e948693eSPhilip Paeps 
64436641d2bSAndrew Rybchenko 	esbp->esb_rid = PCIR_BAR(sc->mem_bar);
645e948693eSPhilip Paeps 	if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
646e948693eSPhilip Paeps 	    &esbp->esb_rid, RF_ACTIVE)) == NULL) {
647e948693eSPhilip Paeps 		device_printf(sc->dev, "Cannot allocate BAR region %d\n",
64836641d2bSAndrew Rybchenko 		    sc->mem_bar);
649e948693eSPhilip Paeps 		return (ENXIO);
650e948693eSPhilip Paeps 	}
651e948693eSPhilip Paeps 	esbp->esb_tag = rman_get_bustag(esbp->esb_res);
652e948693eSPhilip Paeps 	esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
65333d45dc5SAndrew Rybchenko 
65433d45dc5SAndrew Rybchenko 	SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
655e948693eSPhilip Paeps 
656e948693eSPhilip Paeps 	return (0);
657e948693eSPhilip Paeps }
658e948693eSPhilip Paeps 
659e948693eSPhilip Paeps static void
sfxge_bar_fini(struct sfxge_softc * sc)660e948693eSPhilip Paeps sfxge_bar_fini(struct sfxge_softc *sc)
661e948693eSPhilip Paeps {
662e948693eSPhilip Paeps 	efsys_bar_t *esbp = &sc->bar;
663e948693eSPhilip Paeps 
664e948693eSPhilip Paeps 	bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
665e948693eSPhilip Paeps 	    esbp->esb_res);
666763cab71SAndrew Rybchenko 	SFXGE_BAR_LOCK_DESTROY(esbp);
667e948693eSPhilip Paeps }
668e948693eSPhilip Paeps 
669e948693eSPhilip Paeps static int
sfxge_create(struct sfxge_softc * sc)670e948693eSPhilip Paeps sfxge_create(struct sfxge_softc *sc)
671e948693eSPhilip Paeps {
672e948693eSPhilip Paeps 	device_t dev;
673e948693eSPhilip Paeps 	efx_nic_t *enp;
674e948693eSPhilip Paeps 	int error;
675d9e49c83SAndrew Rybchenko 	char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
6769c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
6779c2c444bSAndrew Rybchenko 	char mcdi_log_param_name[sizeof(SFXGE_PARAM(%d.mcdi_logging))];
6789c2c444bSAndrew Rybchenko #endif
679e948693eSPhilip Paeps 
680e948693eSPhilip Paeps 	dev = sc->dev;
681e948693eSPhilip Paeps 
68233d45dc5SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
683e948693eSPhilip Paeps 
684d9e49c83SAndrew Rybchenko 	sc->max_rss_channels = 0;
685d9e49c83SAndrew Rybchenko 	snprintf(rss_param_name, sizeof(rss_param_name),
686d9e49c83SAndrew Rybchenko 		 SFXGE_PARAM(%d.max_rss_channels),
687d9e49c83SAndrew Rybchenko 		 (int)device_get_unit(dev));
688d9e49c83SAndrew Rybchenko 	TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
6899c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
6909c2c444bSAndrew Rybchenko 	sc->mcdi_logging = sfxge_mcdi_logging;
6919c2c444bSAndrew Rybchenko 	snprintf(mcdi_log_param_name, sizeof(mcdi_log_param_name),
6929c2c444bSAndrew Rybchenko 		 SFXGE_PARAM(%d.mcdi_logging),
6939c2c444bSAndrew Rybchenko 		 (int)device_get_unit(dev));
6949c2c444bSAndrew Rybchenko 	TUNABLE_INT_FETCH(mcdi_log_param_name, &sc->mcdi_logging);
6959c2c444bSAndrew Rybchenko #endif
696d9e49c83SAndrew Rybchenko 
6977029da5cSPawel Biernacki 	sc->stats_node = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev),
6987029da5cSPawel Biernacki 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
6997029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Statistics");
700b7b0edd1SGeorge V. Neville-Neil 	if (sc->stats_node == NULL) {
701e948693eSPhilip Paeps 		error = ENOMEM;
702e948693eSPhilip Paeps 		goto fail;
703e948693eSPhilip Paeps 	}
704e948693eSPhilip Paeps 
705e948693eSPhilip Paeps 	TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
706e948693eSPhilip Paeps 
707e948693eSPhilip Paeps 	(void) pci_enable_busmaster(dev);
708e948693eSPhilip Paeps 
709e948693eSPhilip Paeps 	/* Initialize DMA mappings. */
7103c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "dma_init...");
711e948693eSPhilip Paeps 	if ((error = sfxge_dma_init(sc)) != 0)
712e948693eSPhilip Paeps 		goto fail;
713e948693eSPhilip Paeps 
71436641d2bSAndrew Rybchenko 	error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
71536641d2bSAndrew Rybchenko 	    &sc->family, &sc->mem_bar);
71636641d2bSAndrew Rybchenko 	KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
71736641d2bSAndrew Rybchenko 
718e948693eSPhilip Paeps 	/* Map the device registers. */
7193c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "bar_init...");
720e948693eSPhilip Paeps 	if ((error = sfxge_bar_init(sc)) != 0)
721e948693eSPhilip Paeps 		goto fail;
722e948693eSPhilip Paeps 
7233c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_create...");
7243c838a9fSAndrew Rybchenko 
725e948693eSPhilip Paeps 	/* Create the common code nic object. */
72633d45dc5SAndrew Rybchenko 	SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
72733d45dc5SAndrew Rybchenko 			      device_get_nameunit(sc->dev), "nic");
728e948693eSPhilip Paeps 	if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
729e948693eSPhilip Paeps 	    &sc->bar, &sc->enp_lock, &enp)) != 0)
730e948693eSPhilip Paeps 		goto fail3;
731e948693eSPhilip Paeps 	sc->enp = enp;
732e948693eSPhilip Paeps 
73399e3d685SAndrew Rybchenko 	/* Initialize MCDI to talk to the microcontroller. */
73499e3d685SAndrew Rybchenko 	DBGPRINT(sc->dev, "mcdi_init...");
73599e3d685SAndrew Rybchenko 	if ((error = sfxge_mcdi_init(sc)) != 0)
73699e3d685SAndrew Rybchenko 		goto fail4;
73799e3d685SAndrew Rybchenko 
73899e3d685SAndrew Rybchenko 	/* Probe the NIC and build the configuration data area. */
73999e3d685SAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_probe...");
74087a67e18SAndrew Rybchenko 	if ((error = efx_nic_probe(enp, EFX_FW_VARIANT_DONT_CARE)) != 0)
74199e3d685SAndrew Rybchenko 		goto fail5;
74299e3d685SAndrew Rybchenko 
743385b1d8eSGeorge V. Neville-Neil 	if (!ISP2(sfxge_rx_ring_entries) ||
7443c838a9fSAndrew Rybchenko 	    (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
7453c838a9fSAndrew Rybchenko 	    (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
746385b1d8eSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
747385b1d8eSGeorge V. Neville-Neil 		    SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
748385b1d8eSGeorge V. Neville-Neil 		    EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
749385b1d8eSGeorge V. Neville-Neil 		error = EINVAL;
750385b1d8eSGeorge V. Neville-Neil 		goto fail_rx_ring_entries;
751385b1d8eSGeorge V. Neville-Neil 	}
752385b1d8eSGeorge V. Neville-Neil 	sc->rxq_entries = sfxge_rx_ring_entries;
753385b1d8eSGeorge V. Neville-Neil 
7548b447157SAndrew Rybchenko 	if (efx_nic_cfg_get(enp)->enc_features & EFX_FEATURE_TXQ_CKSUM_OP_DESC)
7558b447157SAndrew Rybchenko 		sc->txq_dynamic_cksum_toggle_supported = B_TRUE;
7568b447157SAndrew Rybchenko 	else
7578b447157SAndrew Rybchenko 		sc->txq_dynamic_cksum_toggle_supported = B_FALSE;
7588b447157SAndrew Rybchenko 
759385b1d8eSGeorge V. Neville-Neil 	if (!ISP2(sfxge_tx_ring_entries) ||
7603c838a9fSAndrew Rybchenko 	    (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
761d343a7f4SAndrew Rybchenko 	    (sfxge_tx_ring_entries > efx_nic_cfg_get(enp)->enc_txq_max_ndescs)) {
762385b1d8eSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
763385b1d8eSGeorge V. Neville-Neil 		    SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
764d343a7f4SAndrew Rybchenko 		    EFX_TXQ_MINNDESCS, efx_nic_cfg_get(enp)->enc_txq_max_ndescs);
765385b1d8eSGeorge V. Neville-Neil 		error = EINVAL;
766385b1d8eSGeorge V. Neville-Neil 		goto fail_tx_ring_entries;
767385b1d8eSGeorge V. Neville-Neil 	}
768385b1d8eSGeorge V. Neville-Neil 	sc->txq_entries = sfxge_tx_ring_entries;
769385b1d8eSGeorge V. Neville-Neil 
770ebfb5f25SAndrew Rybchenko 	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
771ebfb5f25SAndrew Rybchenko 			  SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
772ebfb5f25SAndrew Rybchenko 			  OID_AUTO, "version", CTLFLAG_RD,
773ebfb5f25SAndrew Rybchenko 			  SFXGE_VERSION_STRING, 0,
774ebfb5f25SAndrew Rybchenko 			  "Driver version");
775ebfb5f25SAndrew Rybchenko 
7763c838a9fSAndrew Rybchenko 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
7773c838a9fSAndrew Rybchenko 			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
7783c838a9fSAndrew Rybchenko 			OID_AUTO, "phy_type", CTLFLAG_RD,
7793c838a9fSAndrew Rybchenko 			NULL, efx_nic_cfg_get(enp)->enc_phy_type,
7803c838a9fSAndrew Rybchenko 			"PHY type");
7813c838a9fSAndrew Rybchenko 
782e948693eSPhilip Paeps 	/* Initialize the NVRAM. */
7833c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nvram_init...");
784e948693eSPhilip Paeps 	if ((error = efx_nvram_init(enp)) != 0)
785e948693eSPhilip Paeps 		goto fail6;
786e948693eSPhilip Paeps 
787e948693eSPhilip Paeps 	/* Initialize the VPD. */
7883c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "vpd_init...");
789e948693eSPhilip Paeps 	if ((error = efx_vpd_init(enp)) != 0)
790e948693eSPhilip Paeps 		goto fail7;
791e948693eSPhilip Paeps 
7923c838a9fSAndrew Rybchenko 	efx_mcdi_new_epoch(enp);
7933c838a9fSAndrew Rybchenko 
794e948693eSPhilip Paeps 	/* Reset the NIC. */
7953c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_reset...");
796e948693eSPhilip Paeps 	if ((error = efx_nic_reset(enp)) != 0)
797e948693eSPhilip Paeps 		goto fail8;
798e948693eSPhilip Paeps 
799e948693eSPhilip Paeps 	/* Initialize buffer table allocation. */
800e948693eSPhilip Paeps 	sc->buffer_table_next = 0;
801e948693eSPhilip Paeps 
8023c838a9fSAndrew Rybchenko 	/*
8033c838a9fSAndrew Rybchenko 	 * Guarantee minimum and estimate maximum number of event queues
8043c838a9fSAndrew Rybchenko 	 * to take it into account when MSI-X interrupts are allocated.
8053c838a9fSAndrew Rybchenko 	 * It initializes NIC and keeps it initialized on success.
8063c838a9fSAndrew Rybchenko 	 */
8073c838a9fSAndrew Rybchenko 	if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
808e948693eSPhilip Paeps 		goto fail8;
809e948693eSPhilip Paeps 
8103c838a9fSAndrew Rybchenko 	/* Set up interrupts. */
8113c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "intr_init...");
8123c838a9fSAndrew Rybchenko 	if ((error = sfxge_intr_init(sc)) != 0)
8133c838a9fSAndrew Rybchenko 		goto fail9;
8143c838a9fSAndrew Rybchenko 
815e948693eSPhilip Paeps 	/* Initialize event processing state. */
8163c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "ev_init...");
817e948693eSPhilip Paeps 	if ((error = sfxge_ev_init(sc)) != 0)
818e948693eSPhilip Paeps 		goto fail11;
819e948693eSPhilip Paeps 
8203c838a9fSAndrew Rybchenko 	/* Initialize port state. */
8213c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "port_init...");
8223c838a9fSAndrew Rybchenko 	if ((error = sfxge_port_init(sc)) != 0)
823e948693eSPhilip Paeps 		goto fail12;
824e948693eSPhilip Paeps 
8253c838a9fSAndrew Rybchenko 	/* Initialize receive state. */
8263c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "rx_init...");
8273c838a9fSAndrew Rybchenko 	if ((error = sfxge_rx_init(sc)) != 0)
828e948693eSPhilip Paeps 		goto fail13;
829e948693eSPhilip Paeps 
8303c838a9fSAndrew Rybchenko 	/* Initialize transmit state. */
8313c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "tx_init...");
8323c838a9fSAndrew Rybchenko 	if ((error = sfxge_tx_init(sc)) != 0)
833e948693eSPhilip Paeps 		goto fail14;
834e948693eSPhilip Paeps 
835e948693eSPhilip Paeps 	sc->init_state = SFXGE_INITIALIZED;
836e948693eSPhilip Paeps 
8373c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "success");
838e948693eSPhilip Paeps 	return (0);
839e948693eSPhilip Paeps 
840e948693eSPhilip Paeps fail14:
8413c838a9fSAndrew Rybchenko 	sfxge_rx_fini(sc);
842e948693eSPhilip Paeps 
843e948693eSPhilip Paeps fail13:
8443c838a9fSAndrew Rybchenko 	sfxge_port_fini(sc);
845e948693eSPhilip Paeps 
846e948693eSPhilip Paeps fail12:
847e948693eSPhilip Paeps 	sfxge_ev_fini(sc);
848e948693eSPhilip Paeps 
849e948693eSPhilip Paeps fail11:
850e948693eSPhilip Paeps 	sfxge_intr_fini(sc);
851e948693eSPhilip Paeps 
8523c838a9fSAndrew Rybchenko fail9:
8533c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
8543c838a9fSAndrew Rybchenko 
855e948693eSPhilip Paeps fail8:
856e948693eSPhilip Paeps 	efx_vpd_fini(enp);
857e948693eSPhilip Paeps 
858e948693eSPhilip Paeps fail7:
859e948693eSPhilip Paeps 	efx_nvram_fini(enp);
860e948693eSPhilip Paeps 
861e948693eSPhilip Paeps fail6:
86299e3d685SAndrew Rybchenko fail_tx_ring_entries:
86399e3d685SAndrew Rybchenko fail_rx_ring_entries:
864e948693eSPhilip Paeps 	efx_nic_unprobe(enp);
865e948693eSPhilip Paeps 
866e948693eSPhilip Paeps fail5:
867e948693eSPhilip Paeps 	sfxge_mcdi_fini(sc);
868e948693eSPhilip Paeps 
869e948693eSPhilip Paeps fail4:
870e948693eSPhilip Paeps 	sc->enp = NULL;
871e948693eSPhilip Paeps 	efx_nic_destroy(enp);
87233d45dc5SAndrew Rybchenko 	SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
873e948693eSPhilip Paeps 
874e948693eSPhilip Paeps fail3:
875e948693eSPhilip Paeps 	sfxge_bar_fini(sc);
876e948693eSPhilip Paeps 	(void) pci_disable_busmaster(sc->dev);
877e948693eSPhilip Paeps 
878e948693eSPhilip Paeps fail:
8793c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "failed %d", error);
880e948693eSPhilip Paeps 	sc->dev = NULL;
881763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
882e948693eSPhilip Paeps 	return (error);
883e948693eSPhilip Paeps }
884e948693eSPhilip Paeps 
885e948693eSPhilip Paeps static void
sfxge_destroy(struct sfxge_softc * sc)886e948693eSPhilip Paeps sfxge_destroy(struct sfxge_softc *sc)
887e948693eSPhilip Paeps {
888e948693eSPhilip Paeps 	efx_nic_t *enp;
889e948693eSPhilip Paeps 
890e948693eSPhilip Paeps 	/* Clean up transmit state. */
891e948693eSPhilip Paeps 	sfxge_tx_fini(sc);
892e948693eSPhilip Paeps 
893e948693eSPhilip Paeps 	/* Clean up receive state. */
894e948693eSPhilip Paeps 	sfxge_rx_fini(sc);
895e948693eSPhilip Paeps 
8963c838a9fSAndrew Rybchenko 	/* Clean up port state. */
8973c838a9fSAndrew Rybchenko 	sfxge_port_fini(sc);
8983c838a9fSAndrew Rybchenko 
899e948693eSPhilip Paeps 	/* Clean up event processing state. */
900e948693eSPhilip Paeps 	sfxge_ev_fini(sc);
901e948693eSPhilip Paeps 
902e948693eSPhilip Paeps 	/* Clean up interrupts. */
903e948693eSPhilip Paeps 	sfxge_intr_fini(sc);
904e948693eSPhilip Paeps 
905e948693eSPhilip Paeps 	/* Tear down common code subsystems. */
906e948693eSPhilip Paeps 	efx_nic_reset(sc->enp);
907e948693eSPhilip Paeps 	efx_vpd_fini(sc->enp);
908e948693eSPhilip Paeps 	efx_nvram_fini(sc->enp);
909e948693eSPhilip Paeps 	efx_nic_unprobe(sc->enp);
910e948693eSPhilip Paeps 
911e948693eSPhilip Paeps 	/* Tear down MCDI. */
912e948693eSPhilip Paeps 	sfxge_mcdi_fini(sc);
913e948693eSPhilip Paeps 
914e948693eSPhilip Paeps 	/* Destroy common code context. */
915e948693eSPhilip Paeps 	enp = sc->enp;
916e948693eSPhilip Paeps 	sc->enp = NULL;
917e948693eSPhilip Paeps 	efx_nic_destroy(enp);
918e948693eSPhilip Paeps 
919e948693eSPhilip Paeps 	/* Free DMA memory. */
920e948693eSPhilip Paeps 	sfxge_dma_fini(sc);
921e948693eSPhilip Paeps 
922e948693eSPhilip Paeps 	/* Free mapped BARs. */
923e948693eSPhilip Paeps 	sfxge_bar_fini(sc);
924e948693eSPhilip Paeps 
925e948693eSPhilip Paeps 	(void) pci_disable_busmaster(sc->dev);
926e948693eSPhilip Paeps 
927e948693eSPhilip Paeps 	taskqueue_drain(taskqueue_thread, &sc->task_reset);
928e948693eSPhilip Paeps 
929e948693eSPhilip Paeps 	/* Destroy the softc lock. */
930763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
931e948693eSPhilip Paeps }
932e948693eSPhilip Paeps 
933e948693eSPhilip Paeps static int
sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)934e948693eSPhilip Paeps sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
935e948693eSPhilip Paeps {
936e948693eSPhilip Paeps 	struct sfxge_softc *sc = arg1;
937e948693eSPhilip Paeps 	efx_vpd_value_t value;
938e948693eSPhilip Paeps 	int rc;
939e948693eSPhilip Paeps 
940e948693eSPhilip Paeps 	value.evv_tag = arg2 >> 16;
941e948693eSPhilip Paeps 	value.evv_keyword = arg2 & 0xffff;
942e948693eSPhilip Paeps 	if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
943e948693eSPhilip Paeps 	    != 0)
944b7b0edd1SGeorge V. Neville-Neil 		return (rc);
945e948693eSPhilip Paeps 
946b7b0edd1SGeorge V. Neville-Neil 	return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
947e948693eSPhilip Paeps }
948e948693eSPhilip Paeps 
949e948693eSPhilip Paeps static void
sfxge_vpd_try_add(struct sfxge_softc * sc,struct sysctl_oid_list * list,efx_vpd_tag_t tag,const char * keyword)950e948693eSPhilip Paeps sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
951e948693eSPhilip Paeps 		  efx_vpd_tag_t tag, const char *keyword)
952e948693eSPhilip Paeps {
953e948693eSPhilip Paeps 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
954e948693eSPhilip Paeps 	efx_vpd_value_t value;
955e948693eSPhilip Paeps 
956e948693eSPhilip Paeps 	/* Check whether VPD tag/keyword is present */
957e948693eSPhilip Paeps 	value.evv_tag = tag;
958e948693eSPhilip Paeps 	value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
959e948693eSPhilip Paeps 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
960e948693eSPhilip Paeps 		return;
961e948693eSPhilip Paeps 
9627029da5cSPawel Biernacki 	SYSCTL_ADD_PROC(ctx, list, OID_AUTO, keyword,
9637029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
964e948693eSPhilip Paeps 	    sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
965e948693eSPhilip Paeps 	    sfxge_vpd_handler, "A", "");
966e948693eSPhilip Paeps }
967e948693eSPhilip Paeps 
968e948693eSPhilip Paeps static int
sfxge_vpd_init(struct sfxge_softc * sc)969e948693eSPhilip Paeps sfxge_vpd_init(struct sfxge_softc *sc)
970e948693eSPhilip Paeps {
971e948693eSPhilip Paeps 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
972e948693eSPhilip Paeps 	struct sysctl_oid *vpd_node;
973e948693eSPhilip Paeps 	struct sysctl_oid_list *vpd_list;
974e948693eSPhilip Paeps 	char keyword[3];
975e948693eSPhilip Paeps 	efx_vpd_value_t value;
976e948693eSPhilip Paeps 	int rc;
977e948693eSPhilip Paeps 
97829033246SAndrew Rybchenko 	if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
97929033246SAndrew Rybchenko 		/*
9805bdf58e1SGordon Bergling 		 * Unprivileged functions deny VPD access.
98129033246SAndrew Rybchenko 		 * Simply skip VPD in this case.
98229033246SAndrew Rybchenko 		 */
98329033246SAndrew Rybchenko 		if (rc == EACCES)
98429033246SAndrew Rybchenko 			goto done;
985e948693eSPhilip Paeps 		goto fail;
98629033246SAndrew Rybchenko 	}
987e948693eSPhilip Paeps 	sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
988e948693eSPhilip Paeps 	if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
989e948693eSPhilip Paeps 		goto fail2;
990e948693eSPhilip Paeps 
991e948693eSPhilip Paeps 	/* Copy ID (product name) into device description, and log it. */
992e948693eSPhilip Paeps 	value.evv_tag = EFX_VPD_ID;
993e948693eSPhilip Paeps 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
994e948693eSPhilip Paeps 		value.evv_value[value.evv_length] = 0;
995e948693eSPhilip Paeps 		device_set_desc_copy(sc->dev, value.evv_value);
996e948693eSPhilip Paeps 		device_printf(sc->dev, "%s\n", value.evv_value);
997e948693eSPhilip Paeps 	}
998e948693eSPhilip Paeps 
9997029da5cSPawel Biernacki 	vpd_node = SYSCTL_ADD_NODE(ctx,
10007029da5cSPawel Biernacki 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "vpd",
10017029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Vital Product Data");
1002e948693eSPhilip Paeps 	vpd_list = SYSCTL_CHILDREN(vpd_node);
1003e948693eSPhilip Paeps 
1004e948693eSPhilip Paeps 	/* Add sysctls for all expected and any vendor-defined keywords. */
1005e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
1006e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
1007e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
1008e948693eSPhilip Paeps 	keyword[0] = 'V';
1009e948693eSPhilip Paeps 	keyword[2] = 0;
1010e948693eSPhilip Paeps 	for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
1011e948693eSPhilip Paeps 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1012e948693eSPhilip Paeps 	for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
1013e948693eSPhilip Paeps 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1014e948693eSPhilip Paeps 
101529033246SAndrew Rybchenko done:
1016b7b0edd1SGeorge V. Neville-Neil 	return (0);
1017e948693eSPhilip Paeps 
1018e948693eSPhilip Paeps fail2:
1019e948693eSPhilip Paeps 	free(sc->vpd_data, M_SFXGE);
1020e948693eSPhilip Paeps fail:
1021b7b0edd1SGeorge V. Neville-Neil 	return (rc);
1022e948693eSPhilip Paeps }
1023e948693eSPhilip Paeps 
1024e948693eSPhilip Paeps static void
sfxge_vpd_fini(struct sfxge_softc * sc)1025e948693eSPhilip Paeps sfxge_vpd_fini(struct sfxge_softc *sc)
1026e948693eSPhilip Paeps {
1027e948693eSPhilip Paeps 	free(sc->vpd_data, M_SFXGE);
1028e948693eSPhilip Paeps }
1029e948693eSPhilip Paeps 
1030e948693eSPhilip Paeps static void
sfxge_reset(void * arg,int npending)1031e948693eSPhilip Paeps sfxge_reset(void *arg, int npending)
1032e948693eSPhilip Paeps {
1033e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1034e948693eSPhilip Paeps 	int rc;
10353c838a9fSAndrew Rybchenko 	unsigned attempt;
1036e948693eSPhilip Paeps 
1037e948693eSPhilip Paeps 	(void)npending;
1038e948693eSPhilip Paeps 
1039e948693eSPhilip Paeps 	sc = (struct sfxge_softc *)arg;
1040e948693eSPhilip Paeps 
1041763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
1042e948693eSPhilip Paeps 
1043e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_STARTED)
1044e948693eSPhilip Paeps 		goto done;
1045e948693eSPhilip Paeps 
1046e948693eSPhilip Paeps 	sfxge_stop(sc);
1047e948693eSPhilip Paeps 	efx_nic_reset(sc->enp);
10483b5966d1SAndrew Rybchenko 	for (attempt = 0; attempt < sfxge_restart_attempts; ++attempt) {
10493c838a9fSAndrew Rybchenko 		if ((rc = sfxge_start(sc)) == 0)
10503c838a9fSAndrew Rybchenko 			goto done;
10513c838a9fSAndrew Rybchenko 
10523c838a9fSAndrew Rybchenko 		device_printf(sc->dev, "start on reset failed (%d)\n", rc);
10533c838a9fSAndrew Rybchenko 		DELAY(100000);
10543c838a9fSAndrew Rybchenko 	}
10553c838a9fSAndrew Rybchenko 
10563c838a9fSAndrew Rybchenko 	device_printf(sc->dev, "reset failed; interface is now stopped\n");
1057e948693eSPhilip Paeps 
1058e948693eSPhilip Paeps done:
1059763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
1060e948693eSPhilip Paeps }
1061e948693eSPhilip Paeps 
1062e948693eSPhilip Paeps void
sfxge_schedule_reset(struct sfxge_softc * sc)1063e948693eSPhilip Paeps sfxge_schedule_reset(struct sfxge_softc *sc)
1064e948693eSPhilip Paeps {
1065e948693eSPhilip Paeps 	taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1066e948693eSPhilip Paeps }
1067e948693eSPhilip Paeps 
1068e948693eSPhilip Paeps static int
sfxge_attach(device_t dev)1069e948693eSPhilip Paeps sfxge_attach(device_t dev)
1070e948693eSPhilip Paeps {
1071e948693eSPhilip Paeps 	struct sfxge_softc *sc;
107204abf87bSJustin Hibbits 	if_t ifp;
1073e948693eSPhilip Paeps 	int error;
1074e948693eSPhilip Paeps 
1075e948693eSPhilip Paeps 	sc = device_get_softc(dev);
1076e948693eSPhilip Paeps 	sc->dev = dev;
1077e948693eSPhilip Paeps 
1078e948693eSPhilip Paeps 	/* Allocate ifnet. */
1079e948693eSPhilip Paeps 	ifp = if_alloc(IFT_ETHER);
1080e948693eSPhilip Paeps 	sc->ifnet = ifp;
1081e948693eSPhilip Paeps 
1082e948693eSPhilip Paeps 	/* Initialize hardware. */
10833c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "create nic");
1084e948693eSPhilip Paeps 	if ((error = sfxge_create(sc)) != 0)
1085e948693eSPhilip Paeps 		goto fail2;
1086e948693eSPhilip Paeps 
1087e948693eSPhilip Paeps 	/* Create the ifnet for the port. */
10883c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "init ifnet");
1089e948693eSPhilip Paeps 	if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1090e948693eSPhilip Paeps 		goto fail3;
1091e948693eSPhilip Paeps 
10923c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "init vpd");
1093e948693eSPhilip Paeps 	if ((error = sfxge_vpd_init(sc)) != 0)
1094e948693eSPhilip Paeps 		goto fail4;
1095e948693eSPhilip Paeps 
10963c838a9fSAndrew Rybchenko 	/*
10973c838a9fSAndrew Rybchenko 	 * NIC is initialized inside sfxge_create() and kept inialized
10983c838a9fSAndrew Rybchenko 	 * to be able to initialize port to discover media types in
10993c838a9fSAndrew Rybchenko 	 * sfxge_ifnet_init().
11003c838a9fSAndrew Rybchenko 	 */
11013c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
11023c838a9fSAndrew Rybchenko 
1103e948693eSPhilip Paeps 	sc->init_state = SFXGE_REGISTERED;
1104e948693eSPhilip Paeps 
11053c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "success");
1106e948693eSPhilip Paeps 	return (0);
1107e948693eSPhilip Paeps 
1108e948693eSPhilip Paeps fail4:
1109e948693eSPhilip Paeps 	sfxge_ifnet_fini(ifp);
1110e948693eSPhilip Paeps fail3:
11113c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
1112e948693eSPhilip Paeps 	sfxge_destroy(sc);
1113e948693eSPhilip Paeps 
1114e948693eSPhilip Paeps fail2:
1115e948693eSPhilip Paeps 	if_free(sc->ifnet);
11163c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "failed %d", error);
1117e948693eSPhilip Paeps 	return (error);
1118e948693eSPhilip Paeps }
1119e948693eSPhilip Paeps 
1120e948693eSPhilip Paeps static int
sfxge_detach(device_t dev)1121e948693eSPhilip Paeps sfxge_detach(device_t dev)
1122e948693eSPhilip Paeps {
1123e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1124e948693eSPhilip Paeps 
1125e948693eSPhilip Paeps 	sc = device_get_softc(dev);
1126e948693eSPhilip Paeps 
1127e948693eSPhilip Paeps 	sfxge_vpd_fini(sc);
1128e948693eSPhilip Paeps 
1129e948693eSPhilip Paeps 	/* Destroy the ifnet. */
1130e948693eSPhilip Paeps 	sfxge_ifnet_fini(sc->ifnet);
1131e948693eSPhilip Paeps 
1132e948693eSPhilip Paeps 	/* Tear down hardware. */
1133e948693eSPhilip Paeps 	sfxge_destroy(sc);
1134e948693eSPhilip Paeps 
1135e948693eSPhilip Paeps 	return (0);
1136e948693eSPhilip Paeps }
1137e948693eSPhilip Paeps 
1138e948693eSPhilip Paeps static int
sfxge_probe(device_t dev)1139e948693eSPhilip Paeps sfxge_probe(device_t dev)
1140e948693eSPhilip Paeps {
1141e948693eSPhilip Paeps 	uint16_t pci_vendor_id;
1142e948693eSPhilip Paeps 	uint16_t pci_device_id;
1143e948693eSPhilip Paeps 	efx_family_t family;
114436641d2bSAndrew Rybchenko 	unsigned int mem_bar;
1145e948693eSPhilip Paeps 	int rc;
1146e948693eSPhilip Paeps 
1147e948693eSPhilip Paeps 	pci_vendor_id = pci_get_vendor(dev);
1148e948693eSPhilip Paeps 	pci_device_id = pci_get_device(dev);
1149e948693eSPhilip Paeps 
11503c838a9fSAndrew Rybchenko 	DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
115136641d2bSAndrew Rybchenko 	rc = efx_family(pci_vendor_id, pci_device_id, &family, &mem_bar);
11523c838a9fSAndrew Rybchenko 	if (rc != 0) {
11533c838a9fSAndrew Rybchenko 		DBGPRINT(dev, "efx_family fail %d", rc);
1154b7b0edd1SGeorge V. Neville-Neil 		return (ENXIO);
11553c838a9fSAndrew Rybchenko 	}
1156e948693eSPhilip Paeps 
11573c838a9fSAndrew Rybchenko 	if (family == EFX_FAMILY_SIENA) {
1158e948693eSPhilip Paeps 		device_set_desc(dev, "Solarflare SFC9000 family");
1159b7b0edd1SGeorge V. Neville-Neil 		return (0);
1160e948693eSPhilip Paeps 	}
1161e948693eSPhilip Paeps 
11623c838a9fSAndrew Rybchenko 	if (family == EFX_FAMILY_HUNTINGTON) {
11633c838a9fSAndrew Rybchenko 		device_set_desc(dev, "Solarflare SFC9100 family");
11643c838a9fSAndrew Rybchenko 		return (0);
11653c838a9fSAndrew Rybchenko 	}
11663c838a9fSAndrew Rybchenko 
1167131bc376SAndrew Rybchenko 	if (family == EFX_FAMILY_MEDFORD) {
1168131bc376SAndrew Rybchenko 		device_set_desc(dev, "Solarflare SFC9200 family");
1169131bc376SAndrew Rybchenko 		return (0);
1170131bc376SAndrew Rybchenko 	}
1171131bc376SAndrew Rybchenko 
1172c6831b0bSAndrew Rybchenko 	if (family == EFX_FAMILY_MEDFORD2) {
1173c6831b0bSAndrew Rybchenko 		device_set_desc(dev, "Solarflare SFC9250 family");
1174c6831b0bSAndrew Rybchenko 		return (0);
1175c6831b0bSAndrew Rybchenko 	}
1176c6831b0bSAndrew Rybchenko 
11773c838a9fSAndrew Rybchenko 	DBGPRINT(dev, "impossible controller family %d", family);
11783c838a9fSAndrew Rybchenko 	return (ENXIO);
11793c838a9fSAndrew Rybchenko }
11803c838a9fSAndrew Rybchenko 
1181e948693eSPhilip Paeps static device_method_t sfxge_methods[] = {
1182e948693eSPhilip Paeps 	DEVMETHOD(device_probe,		sfxge_probe),
1183e948693eSPhilip Paeps 	DEVMETHOD(device_attach,	sfxge_attach),
1184e948693eSPhilip Paeps 	DEVMETHOD(device_detach,	sfxge_detach),
1185e948693eSPhilip Paeps 
11864b7ec270SMarius Strobl 	DEVMETHOD_END
1187e948693eSPhilip Paeps };
1188e948693eSPhilip Paeps 
1189e948693eSPhilip Paeps static driver_t sfxge_driver = {
1190e948693eSPhilip Paeps 	"sfxge",
1191e948693eSPhilip Paeps 	sfxge_methods,
1192e948693eSPhilip Paeps 	sizeof(struct sfxge_softc)
1193e948693eSPhilip Paeps };
1194e948693eSPhilip Paeps 
11959c5f6793SJohn Baldwin DRIVER_MODULE(sfxge, pci, sfxge_driver, 0, 0);
1196