xref: /freebsd/sys/dev/sfxge/sfxge.c (revision 99e3d6859625ef5a1db1419b0693ca7fc488adaa)
1e948693eSPhilip Paeps /*-
2929c7febSAndrew Rybchenko  * Copyright (c) 2010-2016 Solarflare Communications Inc.
3e948693eSPhilip Paeps  * All rights reserved.
4e948693eSPhilip Paeps  *
5e948693eSPhilip Paeps  * This software was developed in part by Philip Paeps under contract for
6e948693eSPhilip Paeps  * Solarflare Communications, Inc.
7e948693eSPhilip Paeps  *
8e948693eSPhilip Paeps  * Redistribution and use in source and binary forms, with or without
93c838a9fSAndrew Rybchenko  * modification, are permitted provided that the following conditions are met:
10e948693eSPhilip Paeps  *
113c838a9fSAndrew Rybchenko  * 1. Redistributions of source code must retain the above copyright notice,
123c838a9fSAndrew Rybchenko  *    this list of conditions and the following disclaimer.
133c838a9fSAndrew Rybchenko  * 2. Redistributions in binary form must reproduce the above copyright notice,
143c838a9fSAndrew Rybchenko  *    this list of conditions and the following disclaimer in the documentation
153c838a9fSAndrew Rybchenko  *    and/or other materials provided with the distribution.
163c838a9fSAndrew Rybchenko  *
173c838a9fSAndrew Rybchenko  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
183c838a9fSAndrew Rybchenko  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
193c838a9fSAndrew Rybchenko  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
203c838a9fSAndrew Rybchenko  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
213c838a9fSAndrew Rybchenko  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
223c838a9fSAndrew Rybchenko  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
233c838a9fSAndrew Rybchenko  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
243c838a9fSAndrew Rybchenko  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
253c838a9fSAndrew Rybchenko  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
263c838a9fSAndrew Rybchenko  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
273c838a9fSAndrew Rybchenko  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283c838a9fSAndrew Rybchenko  *
293c838a9fSAndrew Rybchenko  * The views and conclusions contained in the software and documentation are
303c838a9fSAndrew Rybchenko  * those of the authors and should not be interpreted as representing official
313c838a9fSAndrew Rybchenko  * policies, either expressed or implied, of the FreeBSD Project.
32e948693eSPhilip Paeps  */
33e948693eSPhilip Paeps 
34e948693eSPhilip Paeps #include <sys/cdefs.h>
35e948693eSPhilip Paeps __FBSDID("$FreeBSD$");
36e948693eSPhilip Paeps 
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 
86385b1d8eSGeorge V. Neville-Neil 
87385b1d8eSGeorge V. Neville-Neil SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
88385b1d8eSGeorge V. Neville-Neil 	    "SFXGE driver parameters");
89385b1d8eSGeorge V. Neville-Neil 
90385b1d8eSGeorge V. Neville-Neil #define	SFXGE_PARAM_RX_RING	SFXGE_PARAM(rx_ring)
91385b1d8eSGeorge V. Neville-Neil static int sfxge_rx_ring_entries = SFXGE_NDESCS;
92385b1d8eSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
93385b1d8eSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
94385b1d8eSGeorge V. Neville-Neil 	   &sfxge_rx_ring_entries, 0,
95385b1d8eSGeorge V. Neville-Neil 	   "Maximum number of descriptors in a receive ring");
96385b1d8eSGeorge V. Neville-Neil 
97385b1d8eSGeorge V. Neville-Neil #define	SFXGE_PARAM_TX_RING	SFXGE_PARAM(tx_ring)
98385b1d8eSGeorge V. Neville-Neil static int sfxge_tx_ring_entries = SFXGE_NDESCS;
99385b1d8eSGeorge V. Neville-Neil TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
100385b1d8eSGeorge V. Neville-Neil SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
101385b1d8eSGeorge V. Neville-Neil 	   &sfxge_tx_ring_entries, 0,
102385b1d8eSGeorge V. Neville-Neil 	   "Maximum number of descriptors in a transmit ring");
103385b1d8eSGeorge V. Neville-Neil 
1043b5966d1SAndrew Rybchenko #define	SFXGE_PARAM_RESTART_ATTEMPTS	SFXGE_PARAM(restart_attempts)
1053b5966d1SAndrew Rybchenko static int sfxge_restart_attempts = 3;
1063b5966d1SAndrew Rybchenko TUNABLE_INT(SFXGE_PARAM_RESTART_ATTEMPTS, &sfxge_restart_attempts);
1073b5966d1SAndrew Rybchenko SYSCTL_INT(_hw_sfxge, OID_AUTO, restart_attempts, CTLFLAG_RDTUN,
1083b5966d1SAndrew Rybchenko 	   &sfxge_restart_attempts, 0,
1093b5966d1SAndrew Rybchenko 	   "Maximum number of attempts to bring interface up after reset");
1103b5966d1SAndrew Rybchenko 
1119c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
1129c2c444bSAndrew Rybchenko #define	SFXGE_PARAM_MCDI_LOGGING	SFXGE_PARAM(mcdi_logging)
1139c2c444bSAndrew Rybchenko static int sfxge_mcdi_logging = 0;
1149c2c444bSAndrew Rybchenko TUNABLE_INT(SFXGE_PARAM_MCDI_LOGGING, &sfxge_mcdi_logging);
1159c2c444bSAndrew Rybchenko #endif
1169c2c444bSAndrew Rybchenko 
117e948693eSPhilip Paeps static void
118e948693eSPhilip Paeps sfxge_reset(void *arg, int npending);
119e948693eSPhilip Paeps 
120e948693eSPhilip Paeps static int
1213c838a9fSAndrew Rybchenko sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
1223c838a9fSAndrew Rybchenko {
1233c838a9fSAndrew Rybchenko 	efx_drv_limits_t limits;
1243c838a9fSAndrew Rybchenko 	int rc;
1253c838a9fSAndrew Rybchenko 	unsigned int evq_max;
1263c838a9fSAndrew Rybchenko 	uint32_t evq_allocated;
1273c838a9fSAndrew Rybchenko 	uint32_t rxq_allocated;
1283c838a9fSAndrew Rybchenko 	uint32_t txq_allocated;
1293c838a9fSAndrew Rybchenko 
1303c838a9fSAndrew Rybchenko 	/*
1313c838a9fSAndrew Rybchenko 	 * Limit the number of event queues to:
1323c838a9fSAndrew Rybchenko 	 *  - number of CPUs
1333c838a9fSAndrew Rybchenko 	 *  - hardwire maximum RSS channels
1343c838a9fSAndrew Rybchenko 	 *  - administratively specified maximum RSS channels
1353c838a9fSAndrew Rybchenko 	 */
1368e7d3206SAndrew Rybchenko #ifdef RSS
1378e7d3206SAndrew Rybchenko 	/*
1388e7d3206SAndrew Rybchenko 	 * Avoid extra limitations so that the number of queues
1398e7d3206SAndrew Rybchenko 	 * may be configured at administrator's will
1408e7d3206SAndrew Rybchenko 	 */
1418e7d3206SAndrew Rybchenko 	evq_max = MIN(MAX(rss_getnumbuckets(), 1), EFX_MAXRSS);
1428e7d3206SAndrew Rybchenko #else
1433c838a9fSAndrew Rybchenko 	evq_max = MIN(mp_ncpus, EFX_MAXRSS);
1448e7d3206SAndrew Rybchenko #endif
1453c838a9fSAndrew Rybchenko 	if (sc->max_rss_channels > 0)
1463c838a9fSAndrew Rybchenko 		evq_max = MIN(evq_max, sc->max_rss_channels);
1473c838a9fSAndrew Rybchenko 
1483c838a9fSAndrew Rybchenko 	memset(&limits, 0, sizeof(limits));
1493c838a9fSAndrew Rybchenko 
1503c838a9fSAndrew Rybchenko 	limits.edl_min_evq_count = 1;
1513c838a9fSAndrew Rybchenko 	limits.edl_max_evq_count = evq_max;
1523c838a9fSAndrew Rybchenko 	limits.edl_min_txq_count = SFXGE_TXQ_NTYPES;
1533c838a9fSAndrew Rybchenko 	limits.edl_max_txq_count = evq_max + SFXGE_TXQ_NTYPES - 1;
1543c838a9fSAndrew Rybchenko 	limits.edl_min_rxq_count = 1;
1553c838a9fSAndrew Rybchenko 	limits.edl_max_rxq_count = evq_max;
1563c838a9fSAndrew Rybchenko 
1573c838a9fSAndrew Rybchenko 	efx_nic_set_drv_limits(sc->enp, &limits);
1583c838a9fSAndrew Rybchenko 
1593c838a9fSAndrew Rybchenko 	if ((rc = efx_nic_init(sc->enp)) != 0)
1603c838a9fSAndrew Rybchenko 		return (rc);
1613c838a9fSAndrew Rybchenko 
1623c838a9fSAndrew Rybchenko 	rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
1633c838a9fSAndrew Rybchenko 				 &txq_allocated);
1643c838a9fSAndrew Rybchenko 	if (rc != 0) {
1653c838a9fSAndrew Rybchenko 		efx_nic_fini(sc->enp);
1663c838a9fSAndrew Rybchenko 		return (rc);
1673c838a9fSAndrew Rybchenko 	}
1683c838a9fSAndrew Rybchenko 
1693c838a9fSAndrew Rybchenko 	KASSERT(txq_allocated >= SFXGE_TXQ_NTYPES,
1703c838a9fSAndrew Rybchenko 		("txq_allocated < SFXGE_TXQ_NTYPES"));
1713c838a9fSAndrew Rybchenko 
1723c838a9fSAndrew Rybchenko 	sc->evq_max = MIN(evq_allocated, evq_max);
1733c838a9fSAndrew Rybchenko 	sc->evq_max = MIN(rxq_allocated, sc->evq_max);
1743c838a9fSAndrew Rybchenko 	sc->evq_max = MIN(txq_allocated - (SFXGE_TXQ_NTYPES - 1),
1753c838a9fSAndrew Rybchenko 			  sc->evq_max);
1763c838a9fSAndrew Rybchenko 
1773c838a9fSAndrew Rybchenko 	KASSERT(sc->evq_max <= evq_max,
1783c838a9fSAndrew Rybchenko 		("allocated more than maximum requested"));
1793c838a9fSAndrew Rybchenko 
1808e7d3206SAndrew Rybchenko #ifdef RSS
1818e7d3206SAndrew Rybchenko 	if (sc->evq_max < rss_getnumbuckets())
1828e7d3206SAndrew Rybchenko 		device_printf(sc->dev, "The number of allocated queues (%u) "
1838e7d3206SAndrew Rybchenko 			      "is less than the number of RSS buckets (%u); "
1848e7d3206SAndrew Rybchenko 			      "performance degradation might be observed",
1858e7d3206SAndrew Rybchenko 			      sc->evq_max, rss_getnumbuckets());
1868e7d3206SAndrew Rybchenko #endif
1878e7d3206SAndrew Rybchenko 
1883c838a9fSAndrew Rybchenko 	/*
1893c838a9fSAndrew Rybchenko 	 * NIC is kept initialized in the case of success to be able to
1903c838a9fSAndrew Rybchenko 	 * initialize port to find out media types.
1913c838a9fSAndrew Rybchenko 	 */
1923c838a9fSAndrew Rybchenko 	return (0);
1933c838a9fSAndrew Rybchenko }
1943c838a9fSAndrew Rybchenko 
1953c838a9fSAndrew Rybchenko static int
1963c838a9fSAndrew Rybchenko sfxge_set_drv_limits(struct sfxge_softc *sc)
1973c838a9fSAndrew Rybchenko {
1983c838a9fSAndrew Rybchenko 	efx_drv_limits_t limits;
1993c838a9fSAndrew Rybchenko 
2003c838a9fSAndrew Rybchenko 	memset(&limits, 0, sizeof(limits));
2013c838a9fSAndrew Rybchenko 
2023c838a9fSAndrew Rybchenko 	/* Limits are strict since take into account initial estimation */
2033c838a9fSAndrew Rybchenko 	limits.edl_min_evq_count = limits.edl_max_evq_count =
2043c838a9fSAndrew Rybchenko 	    sc->intr.n_alloc;
2053c838a9fSAndrew Rybchenko 	limits.edl_min_txq_count = limits.edl_max_txq_count =
2063c838a9fSAndrew Rybchenko 	    sc->intr.n_alloc + SFXGE_TXQ_NTYPES - 1;
2073c838a9fSAndrew Rybchenko 	limits.edl_min_rxq_count = limits.edl_max_rxq_count =
2083c838a9fSAndrew Rybchenko 	    sc->intr.n_alloc;
2093c838a9fSAndrew Rybchenko 
2103c838a9fSAndrew Rybchenko 	return (efx_nic_set_drv_limits(sc->enp, &limits));
2113c838a9fSAndrew Rybchenko }
2123c838a9fSAndrew Rybchenko 
2133c838a9fSAndrew Rybchenko static int
214e948693eSPhilip Paeps sfxge_start(struct sfxge_softc *sc)
215e948693eSPhilip Paeps {
216e948693eSPhilip Paeps 	int rc;
217e948693eSPhilip Paeps 
218763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
219e948693eSPhilip Paeps 
220e948693eSPhilip Paeps 	if (sc->init_state == SFXGE_STARTED)
221b7b0edd1SGeorge V. Neville-Neil 		return (0);
222e948693eSPhilip Paeps 
223e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_REGISTERED) {
224e948693eSPhilip Paeps 		rc = EINVAL;
225e948693eSPhilip Paeps 		goto fail;
226e948693eSPhilip Paeps 	}
227e948693eSPhilip Paeps 
2283c838a9fSAndrew Rybchenko 	/* Set required resource limits */
2293c838a9fSAndrew Rybchenko 	if ((rc = sfxge_set_drv_limits(sc)) != 0)
2303c838a9fSAndrew Rybchenko 		goto fail;
2313c838a9fSAndrew Rybchenko 
232e948693eSPhilip Paeps 	if ((rc = efx_nic_init(sc->enp)) != 0)
233e948693eSPhilip Paeps 		goto fail;
234e948693eSPhilip Paeps 
235e948693eSPhilip Paeps 	/* Start processing interrupts. */
236e948693eSPhilip Paeps 	if ((rc = sfxge_intr_start(sc)) != 0)
237e948693eSPhilip Paeps 		goto fail2;
238e948693eSPhilip Paeps 
239e948693eSPhilip Paeps 	/* Start processing events. */
240e948693eSPhilip Paeps 	if ((rc = sfxge_ev_start(sc)) != 0)
241e948693eSPhilip Paeps 		goto fail3;
242e948693eSPhilip Paeps 
2433c838a9fSAndrew Rybchenko 	/* Fire up the port. */
2443c838a9fSAndrew Rybchenko 	if ((rc = sfxge_port_start(sc)) != 0)
2453c838a9fSAndrew Rybchenko 		goto fail4;
2463c838a9fSAndrew Rybchenko 
247e948693eSPhilip Paeps 	/* Start the receiver side. */
248e948693eSPhilip Paeps 	if ((rc = sfxge_rx_start(sc)) != 0)
2493c838a9fSAndrew Rybchenko 		goto fail5;
250e948693eSPhilip Paeps 
251e948693eSPhilip Paeps 	/* Start the transmitter side. */
252e948693eSPhilip Paeps 	if ((rc = sfxge_tx_start(sc)) != 0)
253e948693eSPhilip Paeps 		goto fail6;
254e948693eSPhilip Paeps 
255e948693eSPhilip Paeps 	sc->init_state = SFXGE_STARTED;
256e948693eSPhilip Paeps 
257e948693eSPhilip Paeps 	/* Tell the stack we're running. */
258e948693eSPhilip Paeps 	sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
259e948693eSPhilip Paeps 	sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
260e948693eSPhilip Paeps 
261e948693eSPhilip Paeps 	return (0);
262e948693eSPhilip Paeps 
263e948693eSPhilip Paeps fail6:
2643c838a9fSAndrew Rybchenko 	sfxge_rx_stop(sc);
265e948693eSPhilip Paeps 
266e948693eSPhilip Paeps fail5:
2673c838a9fSAndrew Rybchenko 	sfxge_port_stop(sc);
268e948693eSPhilip Paeps 
269e948693eSPhilip Paeps fail4:
270e948693eSPhilip Paeps 	sfxge_ev_stop(sc);
271e948693eSPhilip Paeps 
272e948693eSPhilip Paeps fail3:
273e948693eSPhilip Paeps 	sfxge_intr_stop(sc);
274e948693eSPhilip Paeps 
275e948693eSPhilip Paeps fail2:
276e948693eSPhilip Paeps 	efx_nic_fini(sc->enp);
277e948693eSPhilip Paeps 
278e948693eSPhilip Paeps fail:
279e948693eSPhilip Paeps 	device_printf(sc->dev, "sfxge_start: %d\n", rc);
280e948693eSPhilip Paeps 
281e948693eSPhilip Paeps 	return (rc);
282e948693eSPhilip Paeps }
283e948693eSPhilip Paeps 
284e948693eSPhilip Paeps static void
285e948693eSPhilip Paeps sfxge_if_init(void *arg)
286e948693eSPhilip Paeps {
287e948693eSPhilip Paeps 	struct sfxge_softc *sc;
288e948693eSPhilip Paeps 
289e948693eSPhilip Paeps 	sc = (struct sfxge_softc *)arg;
290e948693eSPhilip Paeps 
291763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
292e948693eSPhilip Paeps 	(void)sfxge_start(sc);
293763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
294e948693eSPhilip Paeps }
295e948693eSPhilip Paeps 
296e948693eSPhilip Paeps static void
297e948693eSPhilip Paeps sfxge_stop(struct sfxge_softc *sc)
298e948693eSPhilip Paeps {
299763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
300e948693eSPhilip Paeps 
301e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_STARTED)
302e948693eSPhilip Paeps 		return;
303e948693eSPhilip Paeps 
304e948693eSPhilip Paeps 	sc->init_state = SFXGE_REGISTERED;
305e948693eSPhilip Paeps 
306e948693eSPhilip Paeps 	/* Stop the transmitter. */
307e948693eSPhilip Paeps 	sfxge_tx_stop(sc);
308e948693eSPhilip Paeps 
309e948693eSPhilip Paeps 	/* Stop the receiver. */
310e948693eSPhilip Paeps 	sfxge_rx_stop(sc);
311e948693eSPhilip Paeps 
3123c838a9fSAndrew Rybchenko 	/* Stop the port. */
3133c838a9fSAndrew Rybchenko 	sfxge_port_stop(sc);
3143c838a9fSAndrew Rybchenko 
315e948693eSPhilip Paeps 	/* Stop processing events. */
316e948693eSPhilip Paeps 	sfxge_ev_stop(sc);
317e948693eSPhilip Paeps 
318e948693eSPhilip Paeps 	/* Stop processing interrupts. */
319e948693eSPhilip Paeps 	sfxge_intr_stop(sc);
320e948693eSPhilip Paeps 
321e948693eSPhilip Paeps 	efx_nic_fini(sc->enp);
322e948693eSPhilip Paeps 
323e948693eSPhilip Paeps 	sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
324e948693eSPhilip Paeps }
325e948693eSPhilip Paeps 
3263c838a9fSAndrew Rybchenko 
3273c838a9fSAndrew Rybchenko static int
3283c838a9fSAndrew Rybchenko sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
3293c838a9fSAndrew Rybchenko {
3303c838a9fSAndrew Rybchenko 	efx_vpd_value_t value;
3313c838a9fSAndrew Rybchenko 	int rc = 0;
3323c838a9fSAndrew Rybchenko 
3333c838a9fSAndrew Rybchenko 	switch (ioc->u.vpd.op) {
3343c838a9fSAndrew Rybchenko 	case SFXGE_VPD_OP_GET_KEYWORD:
3353c838a9fSAndrew Rybchenko 		value.evv_tag = ioc->u.vpd.tag;
3363c838a9fSAndrew Rybchenko 		value.evv_keyword = ioc->u.vpd.keyword;
3373c838a9fSAndrew Rybchenko 		rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
3383c838a9fSAndrew Rybchenko 		if (rc != 0)
3393c838a9fSAndrew Rybchenko 			break;
3403c838a9fSAndrew Rybchenko 		ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
3413c838a9fSAndrew Rybchenko 		if (ioc->u.vpd.payload != 0) {
3423c838a9fSAndrew Rybchenko 			rc = copyout(value.evv_value, ioc->u.vpd.payload,
3433c838a9fSAndrew Rybchenko 				     ioc->u.vpd.len);
3443c838a9fSAndrew Rybchenko 		}
3453c838a9fSAndrew Rybchenko 		break;
3463c838a9fSAndrew Rybchenko 	case SFXGE_VPD_OP_SET_KEYWORD:
3473c838a9fSAndrew Rybchenko 		if (ioc->u.vpd.len > sizeof(value.evv_value))
3483c838a9fSAndrew Rybchenko 			return (EINVAL);
3493c838a9fSAndrew Rybchenko 		value.evv_tag = ioc->u.vpd.tag;
3503c838a9fSAndrew Rybchenko 		value.evv_keyword = ioc->u.vpd.keyword;
3513c838a9fSAndrew Rybchenko 		value.evv_length = ioc->u.vpd.len;
3523c838a9fSAndrew Rybchenko 		rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
3533c838a9fSAndrew Rybchenko 		if (rc != 0)
3543c838a9fSAndrew Rybchenko 			break;
3553c838a9fSAndrew Rybchenko 		rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
3563c838a9fSAndrew Rybchenko 		if (rc != 0)
3573c838a9fSAndrew Rybchenko 			break;
3583c838a9fSAndrew Rybchenko 		rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
3593c838a9fSAndrew Rybchenko 		if (rc != 0)
3603c838a9fSAndrew Rybchenko 			break;
3613c838a9fSAndrew Rybchenko 		rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
3623c838a9fSAndrew Rybchenko 		break;
3633c838a9fSAndrew Rybchenko 	default:
3643c838a9fSAndrew Rybchenko 		rc = EOPNOTSUPP;
3653c838a9fSAndrew Rybchenko 		break;
3663c838a9fSAndrew Rybchenko 	}
3673c838a9fSAndrew Rybchenko 
3683c838a9fSAndrew Rybchenko 	return (rc);
3693c838a9fSAndrew Rybchenko }
3703c838a9fSAndrew Rybchenko 
3713c838a9fSAndrew Rybchenko static int
3723c838a9fSAndrew Rybchenko sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
3733c838a9fSAndrew Rybchenko {
3743c838a9fSAndrew Rybchenko 	switch (ioc->op) {
3753c838a9fSAndrew Rybchenko 	case SFXGE_MCDI_IOC:
3763c838a9fSAndrew Rybchenko 		return (sfxge_mcdi_ioctl(sc, ioc));
3773c838a9fSAndrew Rybchenko 	case SFXGE_NVRAM_IOC:
3783c838a9fSAndrew Rybchenko 		return (sfxge_nvram_ioctl(sc, ioc));
3793c838a9fSAndrew Rybchenko 	case SFXGE_VPD_IOC:
3803c838a9fSAndrew Rybchenko 		return (sfxge_vpd_ioctl(sc, ioc));
3813c838a9fSAndrew Rybchenko 	default:
3823c838a9fSAndrew Rybchenko 		return (EOPNOTSUPP);
3833c838a9fSAndrew Rybchenko 	}
3843c838a9fSAndrew Rybchenko }
3853c838a9fSAndrew Rybchenko 
3863c838a9fSAndrew Rybchenko 
387e948693eSPhilip Paeps static int
388e948693eSPhilip Paeps sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
389e948693eSPhilip Paeps {
390e948693eSPhilip Paeps 	struct sfxge_softc *sc;
391e948693eSPhilip Paeps 	struct ifreq *ifr;
3923c838a9fSAndrew Rybchenko 	sfxge_ioc_t ioc;
393e948693eSPhilip Paeps 	int error;
394e948693eSPhilip Paeps 
395e948693eSPhilip Paeps 	ifr = (struct ifreq *)data;
396e948693eSPhilip Paeps 	sc = ifp->if_softc;
397e948693eSPhilip Paeps 	error = 0;
398e948693eSPhilip Paeps 
399e948693eSPhilip Paeps 	switch (command) {
400e948693eSPhilip Paeps 	case SIOCSIFFLAGS:
401763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
402e948693eSPhilip Paeps 		if (ifp->if_flags & IFF_UP) {
403e948693eSPhilip Paeps 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
404e948693eSPhilip Paeps 				if ((ifp->if_flags ^ sc->if_flags) &
405e948693eSPhilip Paeps 				    (IFF_PROMISC | IFF_ALLMULTI)) {
406e948693eSPhilip Paeps 					sfxge_mac_filter_set(sc);
407e948693eSPhilip Paeps 				}
408e948693eSPhilip Paeps 			} else
409e948693eSPhilip Paeps 				sfxge_start(sc);
410e948693eSPhilip Paeps 		} else
411e948693eSPhilip Paeps 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
412e948693eSPhilip Paeps 				sfxge_stop(sc);
413e948693eSPhilip Paeps 		sc->if_flags = ifp->if_flags;
414763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
415e948693eSPhilip Paeps 		break;
416e948693eSPhilip Paeps 	case SIOCSIFMTU:
417e948693eSPhilip Paeps 		if (ifr->ifr_mtu == ifp->if_mtu) {
418e948693eSPhilip Paeps 			/* Nothing to do */
419e948693eSPhilip Paeps 			error = 0;
420e948693eSPhilip Paeps 		} else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
421e948693eSPhilip Paeps 			error = EINVAL;
422e948693eSPhilip Paeps 		} else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
423e948693eSPhilip Paeps 			ifp->if_mtu = ifr->ifr_mtu;
424e948693eSPhilip Paeps 			error = 0;
425e948693eSPhilip Paeps 		} else {
426e948693eSPhilip Paeps 			/* Restart required */
427763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_LOCK(sc);
428e948693eSPhilip Paeps 			sfxge_stop(sc);
429e948693eSPhilip Paeps 			ifp->if_mtu = ifr->ifr_mtu;
430e948693eSPhilip Paeps 			error = sfxge_start(sc);
431763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
432b7b0edd1SGeorge V. Neville-Neil 			if (error != 0) {
433e948693eSPhilip Paeps 				ifp->if_flags &= ~IFF_UP;
434e948693eSPhilip Paeps 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
435e948693eSPhilip Paeps 				if_down(ifp);
436e948693eSPhilip Paeps 			}
437e948693eSPhilip Paeps 		}
438e948693eSPhilip Paeps 		break;
439e948693eSPhilip Paeps 	case SIOCADDMULTI:
440e948693eSPhilip Paeps 	case SIOCDELMULTI:
441e948693eSPhilip Paeps 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
442e948693eSPhilip Paeps 			sfxge_mac_filter_set(sc);
443e948693eSPhilip Paeps 		break;
444e948693eSPhilip Paeps 	case SIOCSIFCAP:
445ef97c04bSAndrew Rybchenko 	{
446ef97c04bSAndrew Rybchenko 		int reqcap = ifr->ifr_reqcap;
447ef97c04bSAndrew Rybchenko 		int capchg_mask;
448ef97c04bSAndrew Rybchenko 
449763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
450e948693eSPhilip Paeps 
451ef97c04bSAndrew Rybchenko 		/* Capabilities to be changed in accordance with request */
452ef97c04bSAndrew Rybchenko 		capchg_mask = ifp->if_capenable ^ reqcap;
453ef97c04bSAndrew Rybchenko 
454e948693eSPhilip Paeps 		/*
455e948693eSPhilip Paeps 		 * The networking core already rejects attempts to
456e948693eSPhilip Paeps 		 * enable capabilities we don't have.  We still have
457e948693eSPhilip Paeps 		 * to reject attempts to disable capabilities that we
458e948693eSPhilip Paeps 		 * can't (yet) disable.
459e948693eSPhilip Paeps 		 */
460ef97c04bSAndrew Rybchenko 		KASSERT((reqcap & ~ifp->if_capabilities) == 0,
4613c838a9fSAndrew Rybchenko 		    ("Unsupported capabilities 0x%x requested 0x%x vs "
4623c838a9fSAndrew Rybchenko 		     "supported 0x%x",
463ef97c04bSAndrew Rybchenko 		     reqcap & ~ifp->if_capabilities,
464ef97c04bSAndrew Rybchenko 		     reqcap , ifp->if_capabilities));
465ef97c04bSAndrew Rybchenko 		if (capchg_mask & SFXGE_CAP_FIXED) {
466e948693eSPhilip Paeps 			error = EINVAL;
467763cab71SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
468e948693eSPhilip Paeps 			break;
469e948693eSPhilip Paeps 		}
470e948693eSPhilip Paeps 
471921f3bd2SAndrew Rybchenko 		/* Check request before any changes */
472921f3bd2SAndrew Rybchenko 		if ((capchg_mask & IFCAP_TSO4) &&
473921f3bd2SAndrew Rybchenko 		    (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
474921f3bd2SAndrew Rybchenko 			error = EAGAIN;
475921f3bd2SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
476921f3bd2SAndrew Rybchenko 			if_printf(ifp, "enable txcsum before tso4\n");
477921f3bd2SAndrew Rybchenko 			break;
478921f3bd2SAndrew Rybchenko 		}
479921f3bd2SAndrew Rybchenko 		if ((capchg_mask & IFCAP_TSO6) &&
480921f3bd2SAndrew Rybchenko 		    (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
481921f3bd2SAndrew Rybchenko 			error = EAGAIN;
482921f3bd2SAndrew Rybchenko 			SFXGE_ADAPTER_UNLOCK(sc);
483921f3bd2SAndrew Rybchenko 			if_printf(ifp, "enable txcsum6 before tso6\n");
484921f3bd2SAndrew Rybchenko 			break;
485921f3bd2SAndrew Rybchenko 		}
486921f3bd2SAndrew Rybchenko 
487921f3bd2SAndrew Rybchenko 		if (reqcap & IFCAP_TXCSUM) {
488e948693eSPhilip Paeps 			ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
489921f3bd2SAndrew Rybchenko 		} else {
490e948693eSPhilip Paeps 			ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
491921f3bd2SAndrew Rybchenko 			if (reqcap & IFCAP_TSO4) {
492921f3bd2SAndrew Rybchenko 				reqcap &= ~IFCAP_TSO4;
493921f3bd2SAndrew Rybchenko 				if_printf(ifp,
494921f3bd2SAndrew Rybchenko 				    "tso4 disabled due to -txcsum\n");
495921f3bd2SAndrew Rybchenko 			}
496921f3bd2SAndrew Rybchenko 		}
497921f3bd2SAndrew Rybchenko 		if (reqcap & IFCAP_TXCSUM_IPV6) {
498db7e53aaSAndrew Rybchenko 			ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
499921f3bd2SAndrew Rybchenko 		} else {
500db7e53aaSAndrew Rybchenko 			ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
501921f3bd2SAndrew Rybchenko 			if (reqcap & IFCAP_TSO6) {
502921f3bd2SAndrew Rybchenko 				reqcap &= ~IFCAP_TSO6;
503921f3bd2SAndrew Rybchenko 				if_printf(ifp,
504921f3bd2SAndrew Rybchenko 				    "tso6 disabled due to -txcsum6\n");
505921f3bd2SAndrew Rybchenko 			}
506921f3bd2SAndrew Rybchenko 		}
50722bc53cbSAndrew Rybchenko 
50822bc53cbSAndrew Rybchenko 		/*
50922bc53cbSAndrew Rybchenko 		 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
51022bc53cbSAndrew Rybchenko 		 * account before using TSO. So, we do not touch
51122bc53cbSAndrew Rybchenko 		 * checksum flags when IFCAP_TSOx is modified.
51222bc53cbSAndrew Rybchenko 		 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
51322bc53cbSAndrew Rybchenko 		 * but both bits are set in IPv4 and IPv6 mbufs.
51422bc53cbSAndrew Rybchenko 		 */
515e948693eSPhilip Paeps 
516ef97c04bSAndrew Rybchenko 		ifp->if_capenable = reqcap;
517ef97c04bSAndrew Rybchenko 
518763cab71SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
519e948693eSPhilip Paeps 		break;
520ef97c04bSAndrew Rybchenko 	}
521e948693eSPhilip Paeps 	case SIOCSIFMEDIA:
522e948693eSPhilip Paeps 	case SIOCGIFMEDIA:
523e948693eSPhilip Paeps 		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
524e948693eSPhilip Paeps 		break;
5251e76f8b8SAndrew Rybchenko #ifdef SIOCGI2C
5261e76f8b8SAndrew Rybchenko 	case SIOCGI2C:
5271e76f8b8SAndrew Rybchenko 	{
5281e76f8b8SAndrew Rybchenko 		struct ifi2creq i2c;
5291e76f8b8SAndrew Rybchenko 
5301e76f8b8SAndrew Rybchenko 		error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
5311e76f8b8SAndrew Rybchenko 		if (error != 0)
5321e76f8b8SAndrew Rybchenko 			break;
5331e76f8b8SAndrew Rybchenko 
5341e76f8b8SAndrew Rybchenko 		if (i2c.len > sizeof(i2c.data)) {
5351e76f8b8SAndrew Rybchenko 			error = EINVAL;
5361e76f8b8SAndrew Rybchenko 			break;
5371e76f8b8SAndrew Rybchenko 		}
5381e76f8b8SAndrew Rybchenko 
5391e76f8b8SAndrew Rybchenko 		SFXGE_ADAPTER_LOCK(sc);
5401e76f8b8SAndrew Rybchenko 		error = efx_phy_module_get_info(sc->enp, i2c.dev_addr,
5411e76f8b8SAndrew Rybchenko 						i2c.offset, i2c.len,
5421e76f8b8SAndrew Rybchenko 						&i2c.data[0]);
5431e76f8b8SAndrew Rybchenko 		SFXGE_ADAPTER_UNLOCK(sc);
5441e76f8b8SAndrew Rybchenko 		if (error == 0)
5451e76f8b8SAndrew Rybchenko 			error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
5461e76f8b8SAndrew Rybchenko 		break;
5471e76f8b8SAndrew Rybchenko 	}
5481e76f8b8SAndrew Rybchenko #endif
5493c838a9fSAndrew Rybchenko 	case SIOCGPRIVATE_0:
5503c838a9fSAndrew Rybchenko 		error = priv_check(curthread, PRIV_DRIVER);
5513c838a9fSAndrew Rybchenko 		if (error != 0)
5523c838a9fSAndrew Rybchenko 			break;
5533c838a9fSAndrew Rybchenko 		error = copyin(ifr->ifr_data, &ioc, sizeof(ioc));
5543c838a9fSAndrew Rybchenko 		if (error != 0)
5553c838a9fSAndrew Rybchenko 			return (error);
5563c838a9fSAndrew Rybchenko 		error = sfxge_private_ioctl(sc, &ioc);
5573c838a9fSAndrew Rybchenko 		if (error == 0) {
5583c838a9fSAndrew Rybchenko 			error = copyout(&ioc, ifr->ifr_data, sizeof(ioc));
5593c838a9fSAndrew Rybchenko 		}
5603c838a9fSAndrew Rybchenko 		break;
561e948693eSPhilip Paeps 	default:
562e948693eSPhilip Paeps 		error = ether_ioctl(ifp, command, data);
563e948693eSPhilip Paeps 	}
564e948693eSPhilip Paeps 
565e948693eSPhilip Paeps 	return (error);
566e948693eSPhilip Paeps }
567e948693eSPhilip Paeps 
568e948693eSPhilip Paeps static void
569e948693eSPhilip Paeps sfxge_ifnet_fini(struct ifnet *ifp)
570e948693eSPhilip Paeps {
571e948693eSPhilip Paeps 	struct sfxge_softc *sc = ifp->if_softc;
572e948693eSPhilip Paeps 
573763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
574e948693eSPhilip Paeps 	sfxge_stop(sc);
575763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
576e948693eSPhilip Paeps 
577e948693eSPhilip Paeps 	ifmedia_removeall(&sc->media);
578e948693eSPhilip Paeps 	ether_ifdetach(ifp);
579e948693eSPhilip Paeps 	if_free(ifp);
580e948693eSPhilip Paeps }
581e948693eSPhilip Paeps 
582e948693eSPhilip Paeps static int
583e948693eSPhilip Paeps sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
584e948693eSPhilip Paeps {
585e948693eSPhilip Paeps 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
586e948693eSPhilip Paeps 	device_t dev;
587e948693eSPhilip Paeps 	int rc;
588e948693eSPhilip Paeps 
589e948693eSPhilip Paeps 	dev = sc->dev;
590e948693eSPhilip Paeps 	sc->ifnet = ifp;
591e948693eSPhilip Paeps 
592e948693eSPhilip Paeps 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
593e948693eSPhilip Paeps 	ifp->if_init = sfxge_if_init;
594e948693eSPhilip Paeps 	ifp->if_softc = sc;
595e948693eSPhilip Paeps 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
596e948693eSPhilip Paeps 	ifp->if_ioctl = sfxge_if_ioctl;
597e948693eSPhilip Paeps 
598e948693eSPhilip Paeps 	ifp->if_capabilities = SFXGE_CAP;
599e948693eSPhilip Paeps 	ifp->if_capenable = SFXGE_CAP_ENABLE;
6008ce7856dSAndrew Rybchenko 	ifp->if_hw_tsomax = SFXGE_TSO_MAX_SIZE;
6018ce7856dSAndrew Rybchenko 	ifp->if_hw_tsomaxsegcount = SFXGE_TX_MAPPING_MAX_SEG;
6028ce7856dSAndrew Rybchenko 	ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
6038a343f93SAndrew Rybchenko 
6048a343f93SAndrew Rybchenko #ifdef SFXGE_LRO
6058a343f93SAndrew Rybchenko 	ifp->if_capabilities |= IFCAP_LRO;
6068a343f93SAndrew Rybchenko 	ifp->if_capenable |= IFCAP_LRO;
6078a343f93SAndrew Rybchenko #endif
6088a343f93SAndrew Rybchenko 
6093c838a9fSAndrew Rybchenko 	if (encp->enc_hw_tx_insert_vlan_enabled) {
6103c838a9fSAndrew Rybchenko 		ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
6113c838a9fSAndrew Rybchenko 		ifp->if_capenable |= IFCAP_VLAN_HWTAGGING;
6123c838a9fSAndrew Rybchenko 	}
613db7e53aaSAndrew Rybchenko 	ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
614db7e53aaSAndrew Rybchenko 			   CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
615e948693eSPhilip Paeps 
616e948693eSPhilip Paeps 	ether_ifattach(ifp, encp->enc_mac_addr);
617e948693eSPhilip Paeps 
618e948693eSPhilip Paeps 	ifp->if_transmit = sfxge_if_transmit;
619e948693eSPhilip Paeps 	ifp->if_qflush = sfxge_if_qflush;
620e948693eSPhilip Paeps 
6213d8fce27SAndrew Rybchenko 	ifp->if_get_counter = sfxge_get_counter;
6223d8fce27SAndrew Rybchenko 
6233c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "ifmedia_init");
624e948693eSPhilip Paeps 	if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
625e948693eSPhilip Paeps 		goto fail;
626e948693eSPhilip Paeps 
627b7b0edd1SGeorge V. Neville-Neil 	return (0);
628e948693eSPhilip Paeps 
629e948693eSPhilip Paeps fail:
630e948693eSPhilip Paeps 	ether_ifdetach(sc->ifnet);
631b7b0edd1SGeorge V. Neville-Neil 	return (rc);
632e948693eSPhilip Paeps }
633e948693eSPhilip Paeps 
634e948693eSPhilip Paeps void
635e948693eSPhilip Paeps sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
636e948693eSPhilip Paeps {
637e948693eSPhilip Paeps 	KASSERT(sc->buffer_table_next + n <=
638e948693eSPhilip Paeps 		efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
639e948693eSPhilip Paeps 		("buffer table full"));
640e948693eSPhilip Paeps 
641e948693eSPhilip Paeps 	*idp = sc->buffer_table_next;
642e948693eSPhilip Paeps 	sc->buffer_table_next += n;
643e948693eSPhilip Paeps }
644e948693eSPhilip Paeps 
645e948693eSPhilip Paeps static int
646e948693eSPhilip Paeps sfxge_bar_init(struct sfxge_softc *sc)
647e948693eSPhilip Paeps {
648e948693eSPhilip Paeps 	efsys_bar_t *esbp = &sc->bar;
649e948693eSPhilip Paeps 
650e948693eSPhilip Paeps 	esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
651e948693eSPhilip Paeps 	if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
652e948693eSPhilip Paeps 	    &esbp->esb_rid, RF_ACTIVE)) == NULL) {
653e948693eSPhilip Paeps 		device_printf(sc->dev, "Cannot allocate BAR region %d\n",
654e948693eSPhilip Paeps 		    EFX_MEM_BAR);
655e948693eSPhilip Paeps 		return (ENXIO);
656e948693eSPhilip Paeps 	}
657e948693eSPhilip Paeps 	esbp->esb_tag = rman_get_bustag(esbp->esb_res);
658e948693eSPhilip Paeps 	esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
65933d45dc5SAndrew Rybchenko 
66033d45dc5SAndrew Rybchenko 	SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
661e948693eSPhilip Paeps 
662e948693eSPhilip Paeps 	return (0);
663e948693eSPhilip Paeps }
664e948693eSPhilip Paeps 
665e948693eSPhilip Paeps static void
666e948693eSPhilip Paeps sfxge_bar_fini(struct sfxge_softc *sc)
667e948693eSPhilip Paeps {
668e948693eSPhilip Paeps 	efsys_bar_t *esbp = &sc->bar;
669e948693eSPhilip Paeps 
670e948693eSPhilip Paeps 	bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
671e948693eSPhilip Paeps 	    esbp->esb_res);
672763cab71SAndrew Rybchenko 	SFXGE_BAR_LOCK_DESTROY(esbp);
673e948693eSPhilip Paeps }
674e948693eSPhilip Paeps 
675e948693eSPhilip Paeps static int
676e948693eSPhilip Paeps sfxge_create(struct sfxge_softc *sc)
677e948693eSPhilip Paeps {
678e948693eSPhilip Paeps 	device_t dev;
679e948693eSPhilip Paeps 	efx_nic_t *enp;
680e948693eSPhilip Paeps 	int error;
681d9e49c83SAndrew Rybchenko 	char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
6829c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
6839c2c444bSAndrew Rybchenko 	char mcdi_log_param_name[sizeof(SFXGE_PARAM(%d.mcdi_logging))];
6849c2c444bSAndrew Rybchenko #endif
685e948693eSPhilip Paeps 
686e948693eSPhilip Paeps 	dev = sc->dev;
687e948693eSPhilip Paeps 
68833d45dc5SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
689e948693eSPhilip Paeps 
690d9e49c83SAndrew Rybchenko 	sc->max_rss_channels = 0;
691d9e49c83SAndrew Rybchenko 	snprintf(rss_param_name, sizeof(rss_param_name),
692d9e49c83SAndrew Rybchenko 		 SFXGE_PARAM(%d.max_rss_channels),
693d9e49c83SAndrew Rybchenko 		 (int)device_get_unit(dev));
694d9e49c83SAndrew Rybchenko 	TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
6959c2c444bSAndrew Rybchenko #if EFSYS_OPT_MCDI_LOGGING
6969c2c444bSAndrew Rybchenko 	sc->mcdi_logging = sfxge_mcdi_logging;
6979c2c444bSAndrew Rybchenko 	snprintf(mcdi_log_param_name, sizeof(mcdi_log_param_name),
6989c2c444bSAndrew Rybchenko 		 SFXGE_PARAM(%d.mcdi_logging),
6999c2c444bSAndrew Rybchenko 		 (int)device_get_unit(dev));
7009c2c444bSAndrew Rybchenko 	TUNABLE_INT_FETCH(mcdi_log_param_name, &sc->mcdi_logging);
7019c2c444bSAndrew Rybchenko #endif
702d9e49c83SAndrew Rybchenko 
703e948693eSPhilip Paeps 	sc->stats_node = SYSCTL_ADD_NODE(
704e948693eSPhilip Paeps 		device_get_sysctl_ctx(dev),
705e948693eSPhilip Paeps 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
706e948693eSPhilip Paeps 		OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
707b7b0edd1SGeorge V. Neville-Neil 	if (sc->stats_node == NULL) {
708e948693eSPhilip Paeps 		error = ENOMEM;
709e948693eSPhilip Paeps 		goto fail;
710e948693eSPhilip Paeps 	}
711e948693eSPhilip Paeps 
712e948693eSPhilip Paeps 	TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
713e948693eSPhilip Paeps 
714e948693eSPhilip Paeps 	(void) pci_enable_busmaster(dev);
715e948693eSPhilip Paeps 
716e948693eSPhilip Paeps 	/* Initialize DMA mappings. */
7173c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "dma_init...");
718e948693eSPhilip Paeps 	if ((error = sfxge_dma_init(sc)) != 0)
719e948693eSPhilip Paeps 		goto fail;
720e948693eSPhilip Paeps 
721e948693eSPhilip Paeps 	/* Map the device registers. */
7223c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "bar_init...");
723e948693eSPhilip Paeps 	if ((error = sfxge_bar_init(sc)) != 0)
724e948693eSPhilip Paeps 		goto fail;
725e948693eSPhilip Paeps 
726e948693eSPhilip Paeps 	error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
727e948693eSPhilip Paeps 	    &sc->family);
728e948693eSPhilip Paeps 	KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
729e948693eSPhilip Paeps 
7303c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_create...");
7313c838a9fSAndrew Rybchenko 
732e948693eSPhilip Paeps 	/* Create the common code nic object. */
73333d45dc5SAndrew Rybchenko 	SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
73433d45dc5SAndrew Rybchenko 			      device_get_nameunit(sc->dev), "nic");
735e948693eSPhilip Paeps 	if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
736e948693eSPhilip Paeps 	    &sc->bar, &sc->enp_lock, &enp)) != 0)
737e948693eSPhilip Paeps 		goto fail3;
738e948693eSPhilip Paeps 	sc->enp = enp;
739e948693eSPhilip Paeps 
740*99e3d685SAndrew Rybchenko 	/* Initialize MCDI to talk to the microcontroller. */
741*99e3d685SAndrew Rybchenko 	DBGPRINT(sc->dev, "mcdi_init...");
742*99e3d685SAndrew Rybchenko 	if ((error = sfxge_mcdi_init(sc)) != 0)
743*99e3d685SAndrew Rybchenko 		goto fail4;
744*99e3d685SAndrew Rybchenko 
745*99e3d685SAndrew Rybchenko 	/* Probe the NIC and build the configuration data area. */
746*99e3d685SAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_probe...");
747*99e3d685SAndrew Rybchenko 	if ((error = efx_nic_probe(enp)) != 0)
748*99e3d685SAndrew Rybchenko 		goto fail5;
749*99e3d685SAndrew Rybchenko 
750385b1d8eSGeorge V. Neville-Neil 	if (!ISP2(sfxge_rx_ring_entries) ||
7513c838a9fSAndrew Rybchenko 	    (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
7523c838a9fSAndrew Rybchenko 	    (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
753385b1d8eSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
754385b1d8eSGeorge V. Neville-Neil 		    SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
755385b1d8eSGeorge V. Neville-Neil 		    EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
756385b1d8eSGeorge V. Neville-Neil 		error = EINVAL;
757385b1d8eSGeorge V. Neville-Neil 		goto fail_rx_ring_entries;
758385b1d8eSGeorge V. Neville-Neil 	}
759385b1d8eSGeorge V. Neville-Neil 	sc->rxq_entries = sfxge_rx_ring_entries;
760385b1d8eSGeorge V. Neville-Neil 
761385b1d8eSGeorge V. Neville-Neil 	if (!ISP2(sfxge_tx_ring_entries) ||
7623c838a9fSAndrew Rybchenko 	    (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
7633c838a9fSAndrew Rybchenko 	    (sfxge_tx_ring_entries > EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)))) {
764385b1d8eSGeorge V. Neville-Neil 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
765385b1d8eSGeorge V. Neville-Neil 		    SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
7663c838a9fSAndrew Rybchenko 		    EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)));
767385b1d8eSGeorge V. Neville-Neil 		error = EINVAL;
768385b1d8eSGeorge V. Neville-Neil 		goto fail_tx_ring_entries;
769385b1d8eSGeorge V. Neville-Neil 	}
770385b1d8eSGeorge V. Neville-Neil 	sc->txq_entries = sfxge_tx_ring_entries;
771385b1d8eSGeorge V. Neville-Neil 
772ebfb5f25SAndrew Rybchenko 	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
773ebfb5f25SAndrew Rybchenko 			  SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
774ebfb5f25SAndrew Rybchenko 			  OID_AUTO, "version", CTLFLAG_RD,
775ebfb5f25SAndrew Rybchenko 			  SFXGE_VERSION_STRING, 0,
776ebfb5f25SAndrew Rybchenko 			  "Driver version");
777ebfb5f25SAndrew Rybchenko 
7783c838a9fSAndrew Rybchenko 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
7793c838a9fSAndrew Rybchenko 			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
7803c838a9fSAndrew Rybchenko 			OID_AUTO, "phy_type", CTLFLAG_RD,
7813c838a9fSAndrew Rybchenko 			NULL, efx_nic_cfg_get(enp)->enc_phy_type,
7823c838a9fSAndrew Rybchenko 			"PHY type");
7833c838a9fSAndrew Rybchenko 
784e948693eSPhilip Paeps 	/* Initialize the NVRAM. */
7853c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nvram_init...");
786e948693eSPhilip Paeps 	if ((error = efx_nvram_init(enp)) != 0)
787e948693eSPhilip Paeps 		goto fail6;
788e948693eSPhilip Paeps 
789e948693eSPhilip Paeps 	/* Initialize the VPD. */
7903c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "vpd_init...");
791e948693eSPhilip Paeps 	if ((error = efx_vpd_init(enp)) != 0)
792e948693eSPhilip Paeps 		goto fail7;
793e948693eSPhilip Paeps 
7943c838a9fSAndrew Rybchenko 	efx_mcdi_new_epoch(enp);
7953c838a9fSAndrew Rybchenko 
796e948693eSPhilip Paeps 	/* Reset the NIC. */
7973c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "nic_reset...");
798e948693eSPhilip Paeps 	if ((error = efx_nic_reset(enp)) != 0)
799e948693eSPhilip Paeps 		goto fail8;
800e948693eSPhilip Paeps 
801e948693eSPhilip Paeps 	/* Initialize buffer table allocation. */
802e948693eSPhilip Paeps 	sc->buffer_table_next = 0;
803e948693eSPhilip Paeps 
8043c838a9fSAndrew Rybchenko 	/*
8053c838a9fSAndrew Rybchenko 	 * Guarantee minimum and estimate maximum number of event queues
8063c838a9fSAndrew Rybchenko 	 * to take it into account when MSI-X interrupts are allocated.
8073c838a9fSAndrew Rybchenko 	 * It initializes NIC and keeps it initialized on success.
8083c838a9fSAndrew Rybchenko 	 */
8093c838a9fSAndrew Rybchenko 	if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
810e948693eSPhilip Paeps 		goto fail8;
811e948693eSPhilip Paeps 
8123c838a9fSAndrew Rybchenko 	/* Set up interrupts. */
8133c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "intr_init...");
8143c838a9fSAndrew Rybchenko 	if ((error = sfxge_intr_init(sc)) != 0)
8153c838a9fSAndrew Rybchenko 		goto fail9;
8163c838a9fSAndrew Rybchenko 
817e948693eSPhilip Paeps 	/* Initialize event processing state. */
8183c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "ev_init...");
819e948693eSPhilip Paeps 	if ((error = sfxge_ev_init(sc)) != 0)
820e948693eSPhilip Paeps 		goto fail11;
821e948693eSPhilip Paeps 
8223c838a9fSAndrew Rybchenko 	/* Initialize port state. */
8233c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "port_init...");
8243c838a9fSAndrew Rybchenko 	if ((error = sfxge_port_init(sc)) != 0)
825e948693eSPhilip Paeps 		goto fail12;
826e948693eSPhilip Paeps 
8273c838a9fSAndrew Rybchenko 	/* Initialize receive state. */
8283c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "rx_init...");
8293c838a9fSAndrew Rybchenko 	if ((error = sfxge_rx_init(sc)) != 0)
830e948693eSPhilip Paeps 		goto fail13;
831e948693eSPhilip Paeps 
8323c838a9fSAndrew Rybchenko 	/* Initialize transmit state. */
8333c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "tx_init...");
8343c838a9fSAndrew Rybchenko 	if ((error = sfxge_tx_init(sc)) != 0)
835e948693eSPhilip Paeps 		goto fail14;
836e948693eSPhilip Paeps 
837e948693eSPhilip Paeps 	sc->init_state = SFXGE_INITIALIZED;
838e948693eSPhilip Paeps 
8393c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "success");
840e948693eSPhilip Paeps 	return (0);
841e948693eSPhilip Paeps 
842e948693eSPhilip Paeps fail14:
8433c838a9fSAndrew Rybchenko 	sfxge_rx_fini(sc);
844e948693eSPhilip Paeps 
845e948693eSPhilip Paeps fail13:
8463c838a9fSAndrew Rybchenko 	sfxge_port_fini(sc);
847e948693eSPhilip Paeps 
848e948693eSPhilip Paeps fail12:
849e948693eSPhilip Paeps 	sfxge_ev_fini(sc);
850e948693eSPhilip Paeps 
851e948693eSPhilip Paeps fail11:
852e948693eSPhilip Paeps 	sfxge_intr_fini(sc);
853e948693eSPhilip Paeps 
8543c838a9fSAndrew Rybchenko fail9:
8553c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
8563c838a9fSAndrew Rybchenko 
857e948693eSPhilip Paeps fail8:
858e948693eSPhilip Paeps 	efx_vpd_fini(enp);
859e948693eSPhilip Paeps 
860e948693eSPhilip Paeps fail7:
861e948693eSPhilip Paeps 	efx_nvram_fini(enp);
862e948693eSPhilip Paeps 
863e948693eSPhilip Paeps fail6:
864*99e3d685SAndrew Rybchenko fail_tx_ring_entries:
865*99e3d685SAndrew Rybchenko fail_rx_ring_entries:
866e948693eSPhilip Paeps 	efx_nic_unprobe(enp);
867e948693eSPhilip Paeps 
868e948693eSPhilip Paeps fail5:
869e948693eSPhilip Paeps 	sfxge_mcdi_fini(sc);
870e948693eSPhilip Paeps 
871e948693eSPhilip Paeps fail4:
872e948693eSPhilip Paeps 	sc->enp = NULL;
873e948693eSPhilip Paeps 	efx_nic_destroy(enp);
87433d45dc5SAndrew Rybchenko 	SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
875e948693eSPhilip Paeps 
876e948693eSPhilip Paeps fail3:
877e948693eSPhilip Paeps 	sfxge_bar_fini(sc);
878e948693eSPhilip Paeps 	(void) pci_disable_busmaster(sc->dev);
879e948693eSPhilip Paeps 
880e948693eSPhilip Paeps fail:
8813c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "failed %d", error);
882e948693eSPhilip Paeps 	sc->dev = NULL;
883763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
884e948693eSPhilip Paeps 	return (error);
885e948693eSPhilip Paeps }
886e948693eSPhilip Paeps 
887e948693eSPhilip Paeps static void
888e948693eSPhilip Paeps sfxge_destroy(struct sfxge_softc *sc)
889e948693eSPhilip Paeps {
890e948693eSPhilip Paeps 	efx_nic_t *enp;
891e948693eSPhilip Paeps 
892e948693eSPhilip Paeps 	/* Clean up transmit state. */
893e948693eSPhilip Paeps 	sfxge_tx_fini(sc);
894e948693eSPhilip Paeps 
895e948693eSPhilip Paeps 	/* Clean up receive state. */
896e948693eSPhilip Paeps 	sfxge_rx_fini(sc);
897e948693eSPhilip Paeps 
8983c838a9fSAndrew Rybchenko 	/* Clean up port state. */
8993c838a9fSAndrew Rybchenko 	sfxge_port_fini(sc);
9003c838a9fSAndrew Rybchenko 
901e948693eSPhilip Paeps 	/* Clean up event processing state. */
902e948693eSPhilip Paeps 	sfxge_ev_fini(sc);
903e948693eSPhilip Paeps 
904e948693eSPhilip Paeps 	/* Clean up interrupts. */
905e948693eSPhilip Paeps 	sfxge_intr_fini(sc);
906e948693eSPhilip Paeps 
907e948693eSPhilip Paeps 	/* Tear down common code subsystems. */
908e948693eSPhilip Paeps 	efx_nic_reset(sc->enp);
909e948693eSPhilip Paeps 	efx_vpd_fini(sc->enp);
910e948693eSPhilip Paeps 	efx_nvram_fini(sc->enp);
911e948693eSPhilip Paeps 	efx_nic_unprobe(sc->enp);
912e948693eSPhilip Paeps 
913e948693eSPhilip Paeps 	/* Tear down MCDI. */
914e948693eSPhilip Paeps 	sfxge_mcdi_fini(sc);
915e948693eSPhilip Paeps 
916e948693eSPhilip Paeps 	/* Destroy common code context. */
917e948693eSPhilip Paeps 	enp = sc->enp;
918e948693eSPhilip Paeps 	sc->enp = NULL;
919e948693eSPhilip Paeps 	efx_nic_destroy(enp);
920e948693eSPhilip Paeps 
921e948693eSPhilip Paeps 	/* Free DMA memory. */
922e948693eSPhilip Paeps 	sfxge_dma_fini(sc);
923e948693eSPhilip Paeps 
924e948693eSPhilip Paeps 	/* Free mapped BARs. */
925e948693eSPhilip Paeps 	sfxge_bar_fini(sc);
926e948693eSPhilip Paeps 
927e948693eSPhilip Paeps 	(void) pci_disable_busmaster(sc->dev);
928e948693eSPhilip Paeps 
929e948693eSPhilip Paeps 	taskqueue_drain(taskqueue_thread, &sc->task_reset);
930e948693eSPhilip Paeps 
931e948693eSPhilip Paeps 	/* Destroy the softc lock. */
932763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
933e948693eSPhilip Paeps }
934e948693eSPhilip Paeps 
935e948693eSPhilip Paeps static int
936e948693eSPhilip Paeps sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
937e948693eSPhilip Paeps {
938e948693eSPhilip Paeps 	struct sfxge_softc *sc = arg1;
939e948693eSPhilip Paeps 	efx_vpd_value_t value;
940e948693eSPhilip Paeps 	int rc;
941e948693eSPhilip Paeps 
942e948693eSPhilip Paeps 	value.evv_tag = arg2 >> 16;
943e948693eSPhilip Paeps 	value.evv_keyword = arg2 & 0xffff;
944e948693eSPhilip Paeps 	if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
945e948693eSPhilip Paeps 	    != 0)
946b7b0edd1SGeorge V. Neville-Neil 		return (rc);
947e948693eSPhilip Paeps 
948b7b0edd1SGeorge V. Neville-Neil 	return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
949e948693eSPhilip Paeps }
950e948693eSPhilip Paeps 
951e948693eSPhilip Paeps static void
952e948693eSPhilip Paeps sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
953e948693eSPhilip Paeps 		  efx_vpd_tag_t tag, const char *keyword)
954e948693eSPhilip Paeps {
955e948693eSPhilip Paeps 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
956e948693eSPhilip Paeps 	efx_vpd_value_t value;
957e948693eSPhilip Paeps 
958e948693eSPhilip Paeps 	/* Check whether VPD tag/keyword is present */
959e948693eSPhilip Paeps 	value.evv_tag = tag;
960e948693eSPhilip Paeps 	value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
961e948693eSPhilip Paeps 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
962e948693eSPhilip Paeps 		return;
963e948693eSPhilip Paeps 
964e948693eSPhilip Paeps 	SYSCTL_ADD_PROC(
965e948693eSPhilip Paeps 		ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
966e948693eSPhilip Paeps 		sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
967e948693eSPhilip Paeps 		sfxge_vpd_handler, "A", "");
968e948693eSPhilip Paeps }
969e948693eSPhilip Paeps 
970e948693eSPhilip Paeps static int
971e948693eSPhilip Paeps sfxge_vpd_init(struct sfxge_softc *sc)
972e948693eSPhilip Paeps {
973e948693eSPhilip Paeps 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
974e948693eSPhilip Paeps 	struct sysctl_oid *vpd_node;
975e948693eSPhilip Paeps 	struct sysctl_oid_list *vpd_list;
976e948693eSPhilip Paeps 	char keyword[3];
977e948693eSPhilip Paeps 	efx_vpd_value_t value;
978e948693eSPhilip Paeps 	int rc;
979e948693eSPhilip Paeps 
98029033246SAndrew Rybchenko 	if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
98129033246SAndrew Rybchenko 		/*
98229033246SAndrew Rybchenko 		 * Unpriviledged functions deny VPD access.
98329033246SAndrew Rybchenko 		 * Simply skip VPD in this case.
98429033246SAndrew Rybchenko 		 */
98529033246SAndrew Rybchenko 		if (rc == EACCES)
98629033246SAndrew Rybchenko 			goto done;
987e948693eSPhilip Paeps 		goto fail;
98829033246SAndrew Rybchenko 	}
989e948693eSPhilip Paeps 	sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
990e948693eSPhilip Paeps 	if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
991e948693eSPhilip Paeps 		goto fail2;
992e948693eSPhilip Paeps 
993e948693eSPhilip Paeps 	/* Copy ID (product name) into device description, and log it. */
994e948693eSPhilip Paeps 	value.evv_tag = EFX_VPD_ID;
995e948693eSPhilip Paeps 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
996e948693eSPhilip Paeps 		value.evv_value[value.evv_length] = 0;
997e948693eSPhilip Paeps 		device_set_desc_copy(sc->dev, value.evv_value);
998e948693eSPhilip Paeps 		device_printf(sc->dev, "%s\n", value.evv_value);
999e948693eSPhilip Paeps 	}
1000e948693eSPhilip Paeps 
1001e948693eSPhilip Paeps 	vpd_node = SYSCTL_ADD_NODE(
1002e948693eSPhilip Paeps 		ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1003e948693eSPhilip Paeps 		OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
1004e948693eSPhilip Paeps 	vpd_list = SYSCTL_CHILDREN(vpd_node);
1005e948693eSPhilip Paeps 
1006e948693eSPhilip Paeps 	/* Add sysctls for all expected and any vendor-defined keywords. */
1007e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
1008e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
1009e948693eSPhilip Paeps 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
1010e948693eSPhilip Paeps 	keyword[0] = 'V';
1011e948693eSPhilip Paeps 	keyword[2] = 0;
1012e948693eSPhilip Paeps 	for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
1013e948693eSPhilip Paeps 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1014e948693eSPhilip Paeps 	for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
1015e948693eSPhilip Paeps 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1016e948693eSPhilip Paeps 
101729033246SAndrew Rybchenko done:
1018b7b0edd1SGeorge V. Neville-Neil 	return (0);
1019e948693eSPhilip Paeps 
1020e948693eSPhilip Paeps fail2:
1021e948693eSPhilip Paeps 	free(sc->vpd_data, M_SFXGE);
1022e948693eSPhilip Paeps fail:
1023b7b0edd1SGeorge V. Neville-Neil 	return (rc);
1024e948693eSPhilip Paeps }
1025e948693eSPhilip Paeps 
1026e948693eSPhilip Paeps static void
1027e948693eSPhilip Paeps sfxge_vpd_fini(struct sfxge_softc *sc)
1028e948693eSPhilip Paeps {
1029e948693eSPhilip Paeps 	free(sc->vpd_data, M_SFXGE);
1030e948693eSPhilip Paeps }
1031e948693eSPhilip Paeps 
1032e948693eSPhilip Paeps static void
1033e948693eSPhilip Paeps sfxge_reset(void *arg, int npending)
1034e948693eSPhilip Paeps {
1035e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1036e948693eSPhilip Paeps 	int rc;
10373c838a9fSAndrew Rybchenko 	unsigned attempt;
1038e948693eSPhilip Paeps 
1039e948693eSPhilip Paeps 	(void)npending;
1040e948693eSPhilip Paeps 
1041e948693eSPhilip Paeps 	sc = (struct sfxge_softc *)arg;
1042e948693eSPhilip Paeps 
1043763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_LOCK(sc);
1044e948693eSPhilip Paeps 
1045e948693eSPhilip Paeps 	if (sc->init_state != SFXGE_STARTED)
1046e948693eSPhilip Paeps 		goto done;
1047e948693eSPhilip Paeps 
1048e948693eSPhilip Paeps 	sfxge_stop(sc);
1049e948693eSPhilip Paeps 	efx_nic_reset(sc->enp);
10503b5966d1SAndrew Rybchenko 	for (attempt = 0; attempt < sfxge_restart_attempts; ++attempt) {
10513c838a9fSAndrew Rybchenko 		if ((rc = sfxge_start(sc)) == 0)
10523c838a9fSAndrew Rybchenko 			goto done;
10533c838a9fSAndrew Rybchenko 
10543c838a9fSAndrew Rybchenko 		device_printf(sc->dev, "start on reset failed (%d)\n", rc);
10553c838a9fSAndrew Rybchenko 		DELAY(100000);
10563c838a9fSAndrew Rybchenko 	}
10573c838a9fSAndrew Rybchenko 
10583c838a9fSAndrew Rybchenko 	device_printf(sc->dev, "reset failed; interface is now stopped\n");
1059e948693eSPhilip Paeps 
1060e948693eSPhilip Paeps done:
1061763cab71SAndrew Rybchenko 	SFXGE_ADAPTER_UNLOCK(sc);
1062e948693eSPhilip Paeps }
1063e948693eSPhilip Paeps 
1064e948693eSPhilip Paeps void
1065e948693eSPhilip Paeps sfxge_schedule_reset(struct sfxge_softc *sc)
1066e948693eSPhilip Paeps {
1067e948693eSPhilip Paeps 	taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1068e948693eSPhilip Paeps }
1069e948693eSPhilip Paeps 
1070e948693eSPhilip Paeps static int
1071e948693eSPhilip Paeps sfxge_attach(device_t dev)
1072e948693eSPhilip Paeps {
1073e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1074e948693eSPhilip Paeps 	struct ifnet *ifp;
1075e948693eSPhilip Paeps 	int error;
1076e948693eSPhilip Paeps 
1077e948693eSPhilip Paeps 	sc = device_get_softc(dev);
1078e948693eSPhilip Paeps 	sc->dev = dev;
1079e948693eSPhilip Paeps 
1080e948693eSPhilip Paeps 	/* Allocate ifnet. */
1081e948693eSPhilip Paeps 	ifp = if_alloc(IFT_ETHER);
1082e948693eSPhilip Paeps 	if (ifp == NULL) {
1083e948693eSPhilip Paeps 		device_printf(dev, "Couldn't allocate ifnet\n");
1084e948693eSPhilip Paeps 		error = ENOMEM;
1085e948693eSPhilip Paeps 		goto fail;
1086e948693eSPhilip Paeps 	}
1087e948693eSPhilip Paeps 	sc->ifnet = ifp;
1088e948693eSPhilip Paeps 
1089e948693eSPhilip Paeps 	/* Initialize hardware. */
10903c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "create nic");
1091e948693eSPhilip Paeps 	if ((error = sfxge_create(sc)) != 0)
1092e948693eSPhilip Paeps 		goto fail2;
1093e948693eSPhilip Paeps 
1094e948693eSPhilip Paeps 	/* Create the ifnet for the port. */
10953c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "init ifnet");
1096e948693eSPhilip Paeps 	if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1097e948693eSPhilip Paeps 		goto fail3;
1098e948693eSPhilip Paeps 
10993c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "init vpd");
1100e948693eSPhilip Paeps 	if ((error = sfxge_vpd_init(sc)) != 0)
1101e948693eSPhilip Paeps 		goto fail4;
1102e948693eSPhilip Paeps 
11033c838a9fSAndrew Rybchenko 	/*
11043c838a9fSAndrew Rybchenko 	 * NIC is initialized inside sfxge_create() and kept inialized
11053c838a9fSAndrew Rybchenko 	 * to be able to initialize port to discover media types in
11063c838a9fSAndrew Rybchenko 	 * sfxge_ifnet_init().
11073c838a9fSAndrew Rybchenko 	 */
11083c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
11093c838a9fSAndrew Rybchenko 
1110e948693eSPhilip Paeps 	sc->init_state = SFXGE_REGISTERED;
1111e948693eSPhilip Paeps 
11123c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "success");
1113e948693eSPhilip Paeps 	return (0);
1114e948693eSPhilip Paeps 
1115e948693eSPhilip Paeps fail4:
1116e948693eSPhilip Paeps 	sfxge_ifnet_fini(ifp);
1117e948693eSPhilip Paeps fail3:
11183c838a9fSAndrew Rybchenko 	efx_nic_fini(sc->enp);
1119e948693eSPhilip Paeps 	sfxge_destroy(sc);
1120e948693eSPhilip Paeps 
1121e948693eSPhilip Paeps fail2:
1122e948693eSPhilip Paeps 	if_free(sc->ifnet);
1123e948693eSPhilip Paeps 
1124e948693eSPhilip Paeps fail:
11253c838a9fSAndrew Rybchenko 	DBGPRINT(sc->dev, "failed %d", error);
1126e948693eSPhilip Paeps 	return (error);
1127e948693eSPhilip Paeps }
1128e948693eSPhilip Paeps 
1129e948693eSPhilip Paeps static int
1130e948693eSPhilip Paeps sfxge_detach(device_t dev)
1131e948693eSPhilip Paeps {
1132e948693eSPhilip Paeps 	struct sfxge_softc *sc;
1133e948693eSPhilip Paeps 
1134e948693eSPhilip Paeps 	sc = device_get_softc(dev);
1135e948693eSPhilip Paeps 
1136e948693eSPhilip Paeps 	sfxge_vpd_fini(sc);
1137e948693eSPhilip Paeps 
1138e948693eSPhilip Paeps 	/* Destroy the ifnet. */
1139e948693eSPhilip Paeps 	sfxge_ifnet_fini(sc->ifnet);
1140e948693eSPhilip Paeps 
1141e948693eSPhilip Paeps 	/* Tear down hardware. */
1142e948693eSPhilip Paeps 	sfxge_destroy(sc);
1143e948693eSPhilip Paeps 
1144e948693eSPhilip Paeps 	return (0);
1145e948693eSPhilip Paeps }
1146e948693eSPhilip Paeps 
1147e948693eSPhilip Paeps static int
1148e948693eSPhilip Paeps sfxge_probe(device_t dev)
1149e948693eSPhilip Paeps {
1150e948693eSPhilip Paeps 	uint16_t pci_vendor_id;
1151e948693eSPhilip Paeps 	uint16_t pci_device_id;
1152e948693eSPhilip Paeps 	efx_family_t family;
1153e948693eSPhilip Paeps 	int rc;
1154e948693eSPhilip Paeps 
1155e948693eSPhilip Paeps 	pci_vendor_id = pci_get_vendor(dev);
1156e948693eSPhilip Paeps 	pci_device_id = pci_get_device(dev);
1157e948693eSPhilip Paeps 
11583c838a9fSAndrew Rybchenko 	DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
1159e948693eSPhilip Paeps 	rc = efx_family(pci_vendor_id, pci_device_id, &family);
11603c838a9fSAndrew Rybchenko 	if (rc != 0) {
11613c838a9fSAndrew Rybchenko 		DBGPRINT(dev, "efx_family fail %d", rc);
1162b7b0edd1SGeorge V. Neville-Neil 		return (ENXIO);
11633c838a9fSAndrew Rybchenko 	}
1164e948693eSPhilip Paeps 
11653c838a9fSAndrew Rybchenko 	if (family == EFX_FAMILY_SIENA) {
1166e948693eSPhilip Paeps 		device_set_desc(dev, "Solarflare SFC9000 family");
1167b7b0edd1SGeorge V. Neville-Neil 		return (0);
1168e948693eSPhilip Paeps 	}
1169e948693eSPhilip Paeps 
11703c838a9fSAndrew Rybchenko 	if (family == EFX_FAMILY_HUNTINGTON) {
11713c838a9fSAndrew Rybchenko 		device_set_desc(dev, "Solarflare SFC9100 family");
11723c838a9fSAndrew Rybchenko 		return (0);
11733c838a9fSAndrew Rybchenko 	}
11743c838a9fSAndrew Rybchenko 
1175131bc376SAndrew Rybchenko 	if (family == EFX_FAMILY_MEDFORD) {
1176131bc376SAndrew Rybchenko 		device_set_desc(dev, "Solarflare SFC9200 family");
1177131bc376SAndrew Rybchenko 		return (0);
1178131bc376SAndrew Rybchenko 	}
1179131bc376SAndrew Rybchenko 
11803c838a9fSAndrew Rybchenko 	DBGPRINT(dev, "impossible controller family %d", family);
11813c838a9fSAndrew Rybchenko 	return (ENXIO);
11823c838a9fSAndrew Rybchenko }
11833c838a9fSAndrew Rybchenko 
1184e948693eSPhilip Paeps static device_method_t sfxge_methods[] = {
1185e948693eSPhilip Paeps 	DEVMETHOD(device_probe,		sfxge_probe),
1186e948693eSPhilip Paeps 	DEVMETHOD(device_attach,	sfxge_attach),
1187e948693eSPhilip Paeps 	DEVMETHOD(device_detach,	sfxge_detach),
1188e948693eSPhilip Paeps 
11894b7ec270SMarius Strobl 	DEVMETHOD_END
1190e948693eSPhilip Paeps };
1191e948693eSPhilip Paeps 
1192e948693eSPhilip Paeps static devclass_t sfxge_devclass;
1193e948693eSPhilip Paeps 
1194e948693eSPhilip Paeps static driver_t sfxge_driver = {
1195e948693eSPhilip Paeps 	"sfxge",
1196e948693eSPhilip Paeps 	sfxge_methods,
1197e948693eSPhilip Paeps 	sizeof(struct sfxge_softc)
1198e948693eSPhilip Paeps };
1199e948693eSPhilip Paeps 
1200e948693eSPhilip Paeps DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);
1201