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