sfxge.c revision 280601
1/*-
2 * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3 * All rights reserved.
4 *
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/10/sys/dev/sfxge/sfxge.c 280601 2015-03-25 13:44:01Z arybchik $");
32
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/bus.h>
36#include <sys/rman.h>
37#include <sys/lock.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40#include <sys/smp.h>
41#include <sys/socket.h>
42#include <sys/taskqueue.h>
43#include <sys/sockio.h>
44#include <sys/sysctl.h>
45#include <sys/syslog.h>
46
47#include <dev/pci/pcireg.h>
48#include <dev/pci/pcivar.h>
49
50#include <net/ethernet.h>
51#include <net/if.h>
52#include <net/if_media.h>
53#include <net/if_types.h>
54
55#include "common/efx.h"
56
57#include "sfxge.h"
58#include "sfxge_rx.h"
59#include "sfxge_version.h"
60
61#define	SFXGE_CAP (IFCAP_VLAN_MTU | \
62		   IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO |	\
63		   IFCAP_JUMBO_MTU | IFCAP_LRO |			\
64		   IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
65#define	SFXGE_CAP_ENABLE SFXGE_CAP
66#define	SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \
67			 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
68
69MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
70
71
72SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
73	    "SFXGE driver parameters");
74
75#define	SFXGE_PARAM_RX_RING	SFXGE_PARAM(rx_ring)
76static int sfxge_rx_ring_entries = SFXGE_NDESCS;
77TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
78SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
79	   &sfxge_rx_ring_entries, 0,
80	   "Maximum number of descriptors in a receive ring");
81
82#define	SFXGE_PARAM_TX_RING	SFXGE_PARAM(tx_ring)
83static int sfxge_tx_ring_entries = SFXGE_NDESCS;
84TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
85SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
86	   &sfxge_tx_ring_entries, 0,
87	   "Maximum number of descriptors in a transmit ring");
88
89
90static void
91sfxge_reset(void *arg, int npending);
92
93static int
94sfxge_start(struct sfxge_softc *sc)
95{
96	int rc;
97
98	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
99
100	if (sc->init_state == SFXGE_STARTED)
101		return (0);
102
103	if (sc->init_state != SFXGE_REGISTERED) {
104		rc = EINVAL;
105		goto fail;
106	}
107
108	if ((rc = efx_nic_init(sc->enp)) != 0)
109		goto fail;
110
111	/* Start processing interrupts. */
112	if ((rc = sfxge_intr_start(sc)) != 0)
113		goto fail2;
114
115	/* Start processing events. */
116	if ((rc = sfxge_ev_start(sc)) != 0)
117		goto fail3;
118
119	/* Start the receiver side. */
120	if ((rc = sfxge_rx_start(sc)) != 0)
121		goto fail4;
122
123	/* Start the transmitter side. */
124	if ((rc = sfxge_tx_start(sc)) != 0)
125		goto fail5;
126
127	/* Fire up the port. */
128	if ((rc = sfxge_port_start(sc)) != 0)
129		goto fail6;
130
131	sc->init_state = SFXGE_STARTED;
132
133	/* Tell the stack we're running. */
134	sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
135	sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
136
137	return (0);
138
139fail6:
140	sfxge_tx_stop(sc);
141
142fail5:
143	sfxge_rx_stop(sc);
144
145fail4:
146	sfxge_ev_stop(sc);
147
148fail3:
149	sfxge_intr_stop(sc);
150
151fail2:
152	efx_nic_fini(sc->enp);
153
154fail:
155	device_printf(sc->dev, "sfxge_start: %d\n", rc);
156
157	return (rc);
158}
159
160static void
161sfxge_if_init(void *arg)
162{
163	struct sfxge_softc *sc;
164
165	sc = (struct sfxge_softc *)arg;
166
167	SFXGE_ADAPTER_LOCK(sc);
168	(void)sfxge_start(sc);
169	SFXGE_ADAPTER_UNLOCK(sc);
170}
171
172static void
173sfxge_stop(struct sfxge_softc *sc)
174{
175	SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
176
177	if (sc->init_state != SFXGE_STARTED)
178		return;
179
180	sc->init_state = SFXGE_REGISTERED;
181
182	/* Stop the port. */
183	sfxge_port_stop(sc);
184
185	/* Stop the transmitter. */
186	sfxge_tx_stop(sc);
187
188	/* Stop the receiver. */
189	sfxge_rx_stop(sc);
190
191	/* Stop processing events. */
192	sfxge_ev_stop(sc);
193
194	/* Stop processing interrupts. */
195	sfxge_intr_stop(sc);
196
197	efx_nic_fini(sc->enp);
198
199	sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
200}
201
202static int
203sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
204{
205	struct sfxge_softc *sc;
206	struct ifreq *ifr;
207	int error;
208
209	ifr = (struct ifreq *)data;
210	sc = ifp->if_softc;
211	error = 0;
212
213	switch (command) {
214	case SIOCSIFFLAGS:
215		SFXGE_ADAPTER_LOCK(sc);
216		if (ifp->if_flags & IFF_UP) {
217			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
218				if ((ifp->if_flags ^ sc->if_flags) &
219				    (IFF_PROMISC | IFF_ALLMULTI)) {
220					sfxge_mac_filter_set(sc);
221				}
222			} else
223				sfxge_start(sc);
224		} else
225			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
226				sfxge_stop(sc);
227		sc->if_flags = ifp->if_flags;
228		SFXGE_ADAPTER_UNLOCK(sc);
229		break;
230	case SIOCSIFMTU:
231		if (ifr->ifr_mtu == ifp->if_mtu) {
232			/* Nothing to do */
233			error = 0;
234		} else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
235			error = EINVAL;
236		} else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
237			ifp->if_mtu = ifr->ifr_mtu;
238			error = 0;
239		} else {
240			/* Restart required */
241			SFXGE_ADAPTER_LOCK(sc);
242			sfxge_stop(sc);
243			ifp->if_mtu = ifr->ifr_mtu;
244			error = sfxge_start(sc);
245			SFXGE_ADAPTER_UNLOCK(sc);
246			if (error != 0) {
247				ifp->if_flags &= ~IFF_UP;
248				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
249				if_down(ifp);
250			}
251		}
252		break;
253	case SIOCADDMULTI:
254	case SIOCDELMULTI:
255		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
256			sfxge_mac_filter_set(sc);
257		break;
258	case SIOCSIFCAP:
259		SFXGE_ADAPTER_LOCK(sc);
260
261		/*
262		 * The networking core already rejects attempts to
263		 * enable capabilities we don't have.  We still have
264		 * to reject attempts to disable capabilities that we
265		 * can't (yet) disable.
266		 */
267		if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
268			error = EINVAL;
269			SFXGE_ADAPTER_UNLOCK(sc);
270			break;
271		}
272
273		ifp->if_capenable = ifr->ifr_reqcap;
274		if (ifp->if_capenable & IFCAP_TXCSUM)
275			ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
276		else
277			ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
278		if (ifp->if_capenable & IFCAP_TSO)
279			ifp->if_hwassist |= CSUM_TSO;
280		else
281			ifp->if_hwassist &= ~CSUM_TSO;
282
283		SFXGE_ADAPTER_UNLOCK(sc);
284		break;
285	case SIOCSIFMEDIA:
286	case SIOCGIFMEDIA:
287		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
288		break;
289	default:
290		error = ether_ioctl(ifp, command, data);
291	}
292
293	return (error);
294}
295
296static void
297sfxge_ifnet_fini(struct ifnet *ifp)
298{
299	struct sfxge_softc *sc = ifp->if_softc;
300
301	SFXGE_ADAPTER_LOCK(sc);
302	sfxge_stop(sc);
303	SFXGE_ADAPTER_UNLOCK(sc);
304
305	ifmedia_removeall(&sc->media);
306	ether_ifdetach(ifp);
307	if_free(ifp);
308}
309
310static int
311sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
312{
313	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
314	device_t dev;
315	int rc;
316
317	dev = sc->dev;
318	sc->ifnet = ifp;
319
320	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
321	ifp->if_init = sfxge_if_init;
322	ifp->if_softc = sc;
323	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
324	ifp->if_ioctl = sfxge_if_ioctl;
325
326	ifp->if_capabilities = SFXGE_CAP;
327	ifp->if_capenable = SFXGE_CAP_ENABLE;
328	ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
329
330	ether_ifattach(ifp, encp->enc_mac_addr);
331
332#ifdef SFXGE_HAVE_MQ
333	ifp->if_transmit = sfxge_if_transmit;
334	ifp->if_qflush = sfxge_if_qflush;
335#else
336	ifp->if_start = sfxge_if_start;
337	IFQ_SET_MAXLEN(&ifp->if_snd, sc->txq_entries - 1);
338	ifp->if_snd.ifq_drv_maxlen = sc->txq_entries - 1;
339	IFQ_SET_READY(&ifp->if_snd);
340
341	snprintf(sc->tx_lock_name, sizeof(sc->tx_lock_name),
342		 "%s:tx", device_get_nameunit(sc->dev));
343	mtx_init(&sc->tx_lock, sc->tx_lock_name, NULL, MTX_DEF);
344#endif
345
346	if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
347		goto fail;
348
349	return (0);
350
351fail:
352	ether_ifdetach(sc->ifnet);
353	return (rc);
354}
355
356void
357sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
358{
359	KASSERT(sc->buffer_table_next + n <=
360		efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
361		("buffer table full"));
362
363	*idp = sc->buffer_table_next;
364	sc->buffer_table_next += n;
365}
366
367static int
368sfxge_bar_init(struct sfxge_softc *sc)
369{
370	efsys_bar_t *esbp = &sc->bar;
371
372	esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
373	if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
374	    &esbp->esb_rid, RF_ACTIVE)) == NULL) {
375		device_printf(sc->dev, "Cannot allocate BAR region %d\n",
376		    EFX_MEM_BAR);
377		return (ENXIO);
378	}
379	esbp->esb_tag = rman_get_bustag(esbp->esb_res);
380	esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
381
382	SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
383
384	return (0);
385}
386
387static void
388sfxge_bar_fini(struct sfxge_softc *sc)
389{
390	efsys_bar_t *esbp = &sc->bar;
391
392	bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
393	    esbp->esb_res);
394	SFXGE_BAR_LOCK_DESTROY(esbp);
395}
396
397static int
398sfxge_create(struct sfxge_softc *sc)
399{
400	device_t dev;
401	efx_nic_t *enp;
402	int error;
403	char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
404
405	dev = sc->dev;
406
407	SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
408
409	sc->max_rss_channels = 0;
410	snprintf(rss_param_name, sizeof(rss_param_name),
411		 SFXGE_PARAM(%d.max_rss_channels),
412		 (int)device_get_unit(dev));
413	TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
414
415	sc->stats_node = SYSCTL_ADD_NODE(
416		device_get_sysctl_ctx(dev),
417		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
418		OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
419	if (sc->stats_node == NULL) {
420		error = ENOMEM;
421		goto fail;
422	}
423
424	TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
425
426	(void) pci_enable_busmaster(dev);
427
428	/* Initialize DMA mappings. */
429	if ((error = sfxge_dma_init(sc)) != 0)
430		goto fail;
431
432	/* Map the device registers. */
433	if ((error = sfxge_bar_init(sc)) != 0)
434		goto fail;
435
436	error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
437	    &sc->family);
438	KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
439
440	/* Create the common code nic object. */
441	SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
442			      device_get_nameunit(sc->dev), "nic");
443	if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
444	    &sc->bar, &sc->enp_lock, &enp)) != 0)
445		goto fail3;
446	sc->enp = enp;
447
448	if (!ISP2(sfxge_rx_ring_entries) ||
449	    !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
450		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
451		    SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
452		    EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
453		error = EINVAL;
454		goto fail_rx_ring_entries;
455	}
456	sc->rxq_entries = sfxge_rx_ring_entries;
457
458	if (!ISP2(sfxge_tx_ring_entries) ||
459	    !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
460		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
461		    SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
462		    EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
463		error = EINVAL;
464		goto fail_tx_ring_entries;
465	}
466	sc->txq_entries = sfxge_tx_ring_entries;
467
468	/* Initialize MCDI to talk to the microcontroller. */
469	if ((error = sfxge_mcdi_init(sc)) != 0)
470		goto fail4;
471
472	/* Probe the NIC and build the configuration data area. */
473	if ((error = efx_nic_probe(enp)) != 0)
474		goto fail5;
475
476	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
477			  SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
478			  OID_AUTO, "version", CTLFLAG_RD,
479			  SFXGE_VERSION_STRING, 0,
480			  "Driver version");
481
482	/* Initialize the NVRAM. */
483	if ((error = efx_nvram_init(enp)) != 0)
484		goto fail6;
485
486	/* Initialize the VPD. */
487	if ((error = efx_vpd_init(enp)) != 0)
488		goto fail7;
489
490	/* Reset the NIC. */
491	if ((error = efx_nic_reset(enp)) != 0)
492		goto fail8;
493
494	/* Initialize buffer table allocation. */
495	sc->buffer_table_next = 0;
496
497	/* Set up interrupts. */
498	if ((error = sfxge_intr_init(sc)) != 0)
499		goto fail8;
500
501	/* Initialize event processing state. */
502	if ((error = sfxge_ev_init(sc)) != 0)
503		goto fail11;
504
505	/* Initialize receive state. */
506	if ((error = sfxge_rx_init(sc)) != 0)
507		goto fail12;
508
509	/* Initialize transmit state. */
510	if ((error = sfxge_tx_init(sc)) != 0)
511		goto fail13;
512
513	/* Initialize port state. */
514	if ((error = sfxge_port_init(sc)) != 0)
515		goto fail14;
516
517	sc->init_state = SFXGE_INITIALIZED;
518
519	return (0);
520
521fail14:
522	sfxge_tx_fini(sc);
523
524fail13:
525	sfxge_rx_fini(sc);
526
527fail12:
528	sfxge_ev_fini(sc);
529
530fail11:
531	sfxge_intr_fini(sc);
532
533fail8:
534	efx_vpd_fini(enp);
535
536fail7:
537	efx_nvram_fini(enp);
538
539fail6:
540	efx_nic_unprobe(enp);
541
542fail5:
543	sfxge_mcdi_fini(sc);
544
545fail4:
546fail_tx_ring_entries:
547fail_rx_ring_entries:
548	sc->enp = NULL;
549	efx_nic_destroy(enp);
550	SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
551
552fail3:
553	sfxge_bar_fini(sc);
554	(void) pci_disable_busmaster(sc->dev);
555
556fail:
557	sc->dev = NULL;
558	SFXGE_ADAPTER_LOCK_DESTROY(sc);
559	return (error);
560}
561
562static void
563sfxge_destroy(struct sfxge_softc *sc)
564{
565	efx_nic_t *enp;
566
567	/* Clean up port state. */
568	sfxge_port_fini(sc);
569
570	/* Clean up transmit state. */
571	sfxge_tx_fini(sc);
572
573	/* Clean up receive state. */
574	sfxge_rx_fini(sc);
575
576	/* Clean up event processing state. */
577	sfxge_ev_fini(sc);
578
579	/* Clean up interrupts. */
580	sfxge_intr_fini(sc);
581
582	/* Tear down common code subsystems. */
583	efx_nic_reset(sc->enp);
584	efx_vpd_fini(sc->enp);
585	efx_nvram_fini(sc->enp);
586	efx_nic_unprobe(sc->enp);
587
588	/* Tear down MCDI. */
589	sfxge_mcdi_fini(sc);
590
591	/* Destroy common code context. */
592	enp = sc->enp;
593	sc->enp = NULL;
594	efx_nic_destroy(enp);
595
596	/* Free DMA memory. */
597	sfxge_dma_fini(sc);
598
599	/* Free mapped BARs. */
600	sfxge_bar_fini(sc);
601
602	(void) pci_disable_busmaster(sc->dev);
603
604	taskqueue_drain(taskqueue_thread, &sc->task_reset);
605
606	/* Destroy the softc lock. */
607	SFXGE_ADAPTER_LOCK_DESTROY(sc);
608}
609
610static int
611sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
612{
613	struct sfxge_softc *sc = arg1;
614	efx_vpd_value_t value;
615	int rc;
616
617	value.evv_tag = arg2 >> 16;
618	value.evv_keyword = arg2 & 0xffff;
619	if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
620	    != 0)
621		return (rc);
622
623	return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
624}
625
626static void
627sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
628		  efx_vpd_tag_t tag, const char *keyword)
629{
630	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
631	efx_vpd_value_t value;
632
633	/* Check whether VPD tag/keyword is present */
634	value.evv_tag = tag;
635	value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
636	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
637		return;
638
639	SYSCTL_ADD_PROC(
640		ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
641		sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
642		sfxge_vpd_handler, "A", "");
643}
644
645static int
646sfxge_vpd_init(struct sfxge_softc *sc)
647{
648	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
649	struct sysctl_oid *vpd_node;
650	struct sysctl_oid_list *vpd_list;
651	char keyword[3];
652	efx_vpd_value_t value;
653	int rc;
654
655	if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
656		goto fail;
657	sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
658	if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
659		goto fail2;
660
661	/* Copy ID (product name) into device description, and log it. */
662	value.evv_tag = EFX_VPD_ID;
663	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
664		value.evv_value[value.evv_length] = 0;
665		device_set_desc_copy(sc->dev, value.evv_value);
666		device_printf(sc->dev, "%s\n", value.evv_value);
667	}
668
669	vpd_node = SYSCTL_ADD_NODE(
670		ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
671		OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
672	vpd_list = SYSCTL_CHILDREN(vpd_node);
673
674	/* Add sysctls for all expected and any vendor-defined keywords. */
675	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
676	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
677	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
678	keyword[0] = 'V';
679	keyword[2] = 0;
680	for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
681		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
682	for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
683		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
684
685	return (0);
686
687fail2:
688	free(sc->vpd_data, M_SFXGE);
689fail:
690	return (rc);
691}
692
693static void
694sfxge_vpd_fini(struct sfxge_softc *sc)
695{
696	free(sc->vpd_data, M_SFXGE);
697}
698
699static void
700sfxge_reset(void *arg, int npending)
701{
702	struct sfxge_softc *sc;
703	int rc;
704
705	(void)npending;
706
707	sc = (struct sfxge_softc *)arg;
708
709	SFXGE_ADAPTER_LOCK(sc);
710
711	if (sc->init_state != SFXGE_STARTED)
712		goto done;
713
714	sfxge_stop(sc);
715	efx_nic_reset(sc->enp);
716	if ((rc = sfxge_start(sc)) != 0)
717		device_printf(sc->dev,
718			      "reset failed (%d); interface is now stopped\n",
719			      rc);
720
721done:
722	SFXGE_ADAPTER_UNLOCK(sc);
723}
724
725void
726sfxge_schedule_reset(struct sfxge_softc *sc)
727{
728	taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
729}
730
731static int
732sfxge_attach(device_t dev)
733{
734	struct sfxge_softc *sc;
735	struct ifnet *ifp;
736	int error;
737
738	sc = device_get_softc(dev);
739	sc->dev = dev;
740
741	/* Allocate ifnet. */
742	ifp = if_alloc(IFT_ETHER);
743	if (ifp == NULL) {
744		device_printf(dev, "Couldn't allocate ifnet\n");
745		error = ENOMEM;
746		goto fail;
747	}
748	sc->ifnet = ifp;
749
750	/* Initialize hardware. */
751	if ((error = sfxge_create(sc)) != 0)
752		goto fail2;
753
754	/* Create the ifnet for the port. */
755	if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
756		goto fail3;
757
758	if ((error = sfxge_vpd_init(sc)) != 0)
759		goto fail4;
760
761	sc->init_state = SFXGE_REGISTERED;
762
763	return (0);
764
765fail4:
766	sfxge_ifnet_fini(ifp);
767fail3:
768	sfxge_destroy(sc);
769
770fail2:
771	if_free(sc->ifnet);
772
773fail:
774	return (error);
775}
776
777static int
778sfxge_detach(device_t dev)
779{
780	struct sfxge_softc *sc;
781
782	sc = device_get_softc(dev);
783
784	sfxge_vpd_fini(sc);
785
786	/* Destroy the ifnet. */
787	sfxge_ifnet_fini(sc->ifnet);
788
789	/* Tear down hardware. */
790	sfxge_destroy(sc);
791
792	return (0);
793}
794
795static int
796sfxge_probe(device_t dev)
797{
798	uint16_t pci_vendor_id;
799	uint16_t pci_device_id;
800	efx_family_t family;
801	int rc;
802
803	pci_vendor_id = pci_get_vendor(dev);
804	pci_device_id = pci_get_device(dev);
805
806	rc = efx_family(pci_vendor_id, pci_device_id, &family);
807	if (rc != 0)
808		return (ENXIO);
809
810	KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
811	device_set_desc(dev, "Solarflare SFC9000 family");
812	return (0);
813}
814
815static device_method_t sfxge_methods[] = {
816	DEVMETHOD(device_probe,		sfxge_probe),
817	DEVMETHOD(device_attach,	sfxge_attach),
818	DEVMETHOD(device_detach,	sfxge_detach),
819
820	DEVMETHOD_END
821};
822
823static devclass_t sfxge_devclass;
824
825static driver_t sfxge_driver = {
826	"sfxge",
827	sfxge_methods,
828	sizeof(struct sfxge_softc)
829};
830
831DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);
832