xref: /freebsd/sys/dev/sfxge/sfxge.c (revision 6fc44dab1979102b0fca53bb5a723fc77c62934d)
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 
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 
69 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
70 
71 
72 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
73 	    "SFXGE driver parameters");
74 
75 #define	SFXGE_PARAM_RX_RING	SFXGE_PARAM(rx_ring)
76 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
77 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
78 SYSCTL_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)
83 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
84 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
85 SYSCTL_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 
90 static void
91 sfxge_reset(void *arg, int npending);
92 
93 static int
94 sfxge_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 
139 fail6:
140 	sfxge_tx_stop(sc);
141 
142 fail5:
143 	sfxge_rx_stop(sc);
144 
145 fail4:
146 	sfxge_ev_stop(sc);
147 
148 fail3:
149 	sfxge_intr_stop(sc);
150 
151 fail2:
152 	efx_nic_fini(sc->enp);
153 
154 fail:
155 	device_printf(sc->dev, "sfxge_start: %d\n", rc);
156 
157 	return (rc);
158 }
159 
160 static void
161 sfxge_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 
172 static void
173 sfxge_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 
202 static int
203 sfxge_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 
296 static void
297 sfxge_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 
310 static int
311 sfxge_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 
351 fail:
352 	ether_ifdetach(sc->ifnet);
353 	return (rc);
354 }
355 
356 void
357 sfxge_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 
367 static int
368 sfxge_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 
387 static void
388 sfxge_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 
397 static int
398 sfxge_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 	/* Initialize the NVRAM. */
477 	if ((error = efx_nvram_init(enp)) != 0)
478 		goto fail6;
479 
480 	/* Initialize the VPD. */
481 	if ((error = efx_vpd_init(enp)) != 0)
482 		goto fail7;
483 
484 	/* Reset the NIC. */
485 	if ((error = efx_nic_reset(enp)) != 0)
486 		goto fail8;
487 
488 	/* Initialize buffer table allocation. */
489 	sc->buffer_table_next = 0;
490 
491 	/* Set up interrupts. */
492 	if ((error = sfxge_intr_init(sc)) != 0)
493 		goto fail8;
494 
495 	/* Initialize event processing state. */
496 	if ((error = sfxge_ev_init(sc)) != 0)
497 		goto fail11;
498 
499 	/* Initialize receive state. */
500 	if ((error = sfxge_rx_init(sc)) != 0)
501 		goto fail12;
502 
503 	/* Initialize transmit state. */
504 	if ((error = sfxge_tx_init(sc)) != 0)
505 		goto fail13;
506 
507 	/* Initialize port state. */
508 	if ((error = sfxge_port_init(sc)) != 0)
509 		goto fail14;
510 
511 	sc->init_state = SFXGE_INITIALIZED;
512 
513 	return (0);
514 
515 fail14:
516 	sfxge_tx_fini(sc);
517 
518 fail13:
519 	sfxge_rx_fini(sc);
520 
521 fail12:
522 	sfxge_ev_fini(sc);
523 
524 fail11:
525 	sfxge_intr_fini(sc);
526 
527 fail8:
528 	efx_vpd_fini(enp);
529 
530 fail7:
531 	efx_nvram_fini(enp);
532 
533 fail6:
534 	efx_nic_unprobe(enp);
535 
536 fail5:
537 	sfxge_mcdi_fini(sc);
538 
539 fail4:
540 fail_tx_ring_entries:
541 fail_rx_ring_entries:
542 	sc->enp = NULL;
543 	efx_nic_destroy(enp);
544 	SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
545 
546 fail3:
547 	sfxge_bar_fini(sc);
548 	(void) pci_disable_busmaster(sc->dev);
549 
550 fail:
551 	sc->dev = NULL;
552 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
553 	return (error);
554 }
555 
556 static void
557 sfxge_destroy(struct sfxge_softc *sc)
558 {
559 	efx_nic_t *enp;
560 
561 	/* Clean up port state. */
562 	sfxge_port_fini(sc);
563 
564 	/* Clean up transmit state. */
565 	sfxge_tx_fini(sc);
566 
567 	/* Clean up receive state. */
568 	sfxge_rx_fini(sc);
569 
570 	/* Clean up event processing state. */
571 	sfxge_ev_fini(sc);
572 
573 	/* Clean up interrupts. */
574 	sfxge_intr_fini(sc);
575 
576 	/* Tear down common code subsystems. */
577 	efx_nic_reset(sc->enp);
578 	efx_vpd_fini(sc->enp);
579 	efx_nvram_fini(sc->enp);
580 	efx_nic_unprobe(sc->enp);
581 
582 	/* Tear down MCDI. */
583 	sfxge_mcdi_fini(sc);
584 
585 	/* Destroy common code context. */
586 	enp = sc->enp;
587 	sc->enp = NULL;
588 	efx_nic_destroy(enp);
589 
590 	/* Free DMA memory. */
591 	sfxge_dma_fini(sc);
592 
593 	/* Free mapped BARs. */
594 	sfxge_bar_fini(sc);
595 
596 	(void) pci_disable_busmaster(sc->dev);
597 
598 	taskqueue_drain(taskqueue_thread, &sc->task_reset);
599 
600 	/* Destroy the softc lock. */
601 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
602 }
603 
604 static int
605 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
606 {
607 	struct sfxge_softc *sc = arg1;
608 	efx_vpd_value_t value;
609 	int rc;
610 
611 	value.evv_tag = arg2 >> 16;
612 	value.evv_keyword = arg2 & 0xffff;
613 	if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
614 	    != 0)
615 		return (rc);
616 
617 	return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
618 }
619 
620 static void
621 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
622 		  efx_vpd_tag_t tag, const char *keyword)
623 {
624 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
625 	efx_vpd_value_t value;
626 
627 	/* Check whether VPD tag/keyword is present */
628 	value.evv_tag = tag;
629 	value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
630 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
631 		return;
632 
633 	SYSCTL_ADD_PROC(
634 		ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
635 		sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
636 		sfxge_vpd_handler, "A", "");
637 }
638 
639 static int
640 sfxge_vpd_init(struct sfxge_softc *sc)
641 {
642 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
643 	struct sysctl_oid *vpd_node;
644 	struct sysctl_oid_list *vpd_list;
645 	char keyword[3];
646 	efx_vpd_value_t value;
647 	int rc;
648 
649 	if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
650 		goto fail;
651 	sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
652 	if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
653 		goto fail2;
654 
655 	/* Copy ID (product name) into device description, and log it. */
656 	value.evv_tag = EFX_VPD_ID;
657 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
658 		value.evv_value[value.evv_length] = 0;
659 		device_set_desc_copy(sc->dev, value.evv_value);
660 		device_printf(sc->dev, "%s\n", value.evv_value);
661 	}
662 
663 	vpd_node = SYSCTL_ADD_NODE(
664 		ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
665 		OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
666 	vpd_list = SYSCTL_CHILDREN(vpd_node);
667 
668 	/* Add sysctls for all expected and any vendor-defined keywords. */
669 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
670 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
671 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
672 	keyword[0] = 'V';
673 	keyword[2] = 0;
674 	for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
675 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
676 	for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
677 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
678 
679 	return (0);
680 
681 fail2:
682 	free(sc->vpd_data, M_SFXGE);
683 fail:
684 	return (rc);
685 }
686 
687 static void
688 sfxge_vpd_fini(struct sfxge_softc *sc)
689 {
690 	free(sc->vpd_data, M_SFXGE);
691 }
692 
693 static void
694 sfxge_reset(void *arg, int npending)
695 {
696 	struct sfxge_softc *sc;
697 	int rc;
698 
699 	(void)npending;
700 
701 	sc = (struct sfxge_softc *)arg;
702 
703 	SFXGE_ADAPTER_LOCK(sc);
704 
705 	if (sc->init_state != SFXGE_STARTED)
706 		goto done;
707 
708 	sfxge_stop(sc);
709 	efx_nic_reset(sc->enp);
710 	if ((rc = sfxge_start(sc)) != 0)
711 		device_printf(sc->dev,
712 			      "reset failed (%d); interface is now stopped\n",
713 			      rc);
714 
715 done:
716 	SFXGE_ADAPTER_UNLOCK(sc);
717 }
718 
719 void
720 sfxge_schedule_reset(struct sfxge_softc *sc)
721 {
722 	taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
723 }
724 
725 static int
726 sfxge_attach(device_t dev)
727 {
728 	struct sfxge_softc *sc;
729 	struct ifnet *ifp;
730 	int error;
731 
732 	sc = device_get_softc(dev);
733 	sc->dev = dev;
734 
735 	/* Allocate ifnet. */
736 	ifp = if_alloc(IFT_ETHER);
737 	if (ifp == NULL) {
738 		device_printf(dev, "Couldn't allocate ifnet\n");
739 		error = ENOMEM;
740 		goto fail;
741 	}
742 	sc->ifnet = ifp;
743 
744 	/* Initialize hardware. */
745 	if ((error = sfxge_create(sc)) != 0)
746 		goto fail2;
747 
748 	/* Create the ifnet for the port. */
749 	if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
750 		goto fail3;
751 
752 	if ((error = sfxge_vpd_init(sc)) != 0)
753 		goto fail4;
754 
755 	sc->init_state = SFXGE_REGISTERED;
756 
757 	return (0);
758 
759 fail4:
760 	sfxge_ifnet_fini(ifp);
761 fail3:
762 	sfxge_destroy(sc);
763 
764 fail2:
765 	if_free(sc->ifnet);
766 
767 fail:
768 	return (error);
769 }
770 
771 static int
772 sfxge_detach(device_t dev)
773 {
774 	struct sfxge_softc *sc;
775 
776 	sc = device_get_softc(dev);
777 
778 	sfxge_vpd_fini(sc);
779 
780 	/* Destroy the ifnet. */
781 	sfxge_ifnet_fini(sc->ifnet);
782 
783 	/* Tear down hardware. */
784 	sfxge_destroy(sc);
785 
786 	return (0);
787 }
788 
789 static int
790 sfxge_probe(device_t dev)
791 {
792 	uint16_t pci_vendor_id;
793 	uint16_t pci_device_id;
794 	efx_family_t family;
795 	int rc;
796 
797 	pci_vendor_id = pci_get_vendor(dev);
798 	pci_device_id = pci_get_device(dev);
799 
800 	rc = efx_family(pci_vendor_id, pci_device_id, &family);
801 	if (rc != 0)
802 		return (ENXIO);
803 
804 	KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
805 	device_set_desc(dev, "Solarflare SFC9000 family");
806 	return (0);
807 }
808 
809 static device_method_t sfxge_methods[] = {
810 	DEVMETHOD(device_probe,		sfxge_probe),
811 	DEVMETHOD(device_attach,	sfxge_attach),
812 	DEVMETHOD(device_detach,	sfxge_detach),
813 
814 	DEVMETHOD_END
815 };
816 
817 static devclass_t sfxge_devclass;
818 
819 static driver_t sfxge_driver = {
820 	"sfxge",
821 	sfxge_methods,
822 	sizeof(struct sfxge_softc)
823 };
824 
825 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);
826