xref: /freebsd/sys/dev/sfxge/sfxge.c (revision 9fe48b8076ae9c6dc486d713c468ff03ec056eda)
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 	ifp->if_transmit = sfxge_if_transmit;
334 	ifp->if_qflush = sfxge_if_qflush;
335 
336 	ifp->if_get_counter = sfxge_get_counter;
337 
338 	if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
339 		goto fail;
340 
341 	return (0);
342 
343 fail:
344 	ether_ifdetach(sc->ifnet);
345 	return (rc);
346 }
347 
348 void
349 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
350 {
351 	KASSERT(sc->buffer_table_next + n <=
352 		efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
353 		("buffer table full"));
354 
355 	*idp = sc->buffer_table_next;
356 	sc->buffer_table_next += n;
357 }
358 
359 static int
360 sfxge_bar_init(struct sfxge_softc *sc)
361 {
362 	efsys_bar_t *esbp = &sc->bar;
363 
364 	esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
365 	if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
366 	    &esbp->esb_rid, RF_ACTIVE)) == NULL) {
367 		device_printf(sc->dev, "Cannot allocate BAR region %d\n",
368 		    EFX_MEM_BAR);
369 		return (ENXIO);
370 	}
371 	esbp->esb_tag = rman_get_bustag(esbp->esb_res);
372 	esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
373 
374 	SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
375 
376 	return (0);
377 }
378 
379 static void
380 sfxge_bar_fini(struct sfxge_softc *sc)
381 {
382 	efsys_bar_t *esbp = &sc->bar;
383 
384 	bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
385 	    esbp->esb_res);
386 	SFXGE_BAR_LOCK_DESTROY(esbp);
387 }
388 
389 static int
390 sfxge_create(struct sfxge_softc *sc)
391 {
392 	device_t dev;
393 	efx_nic_t *enp;
394 	int error;
395 	char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
396 
397 	dev = sc->dev;
398 
399 	SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
400 
401 	sc->max_rss_channels = 0;
402 	snprintf(rss_param_name, sizeof(rss_param_name),
403 		 SFXGE_PARAM(%d.max_rss_channels),
404 		 (int)device_get_unit(dev));
405 	TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
406 
407 	sc->stats_node = SYSCTL_ADD_NODE(
408 		device_get_sysctl_ctx(dev),
409 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
410 		OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
411 	if (sc->stats_node == NULL) {
412 		error = ENOMEM;
413 		goto fail;
414 	}
415 
416 	TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
417 
418 	(void) pci_enable_busmaster(dev);
419 
420 	/* Initialize DMA mappings. */
421 	if ((error = sfxge_dma_init(sc)) != 0)
422 		goto fail;
423 
424 	/* Map the device registers. */
425 	if ((error = sfxge_bar_init(sc)) != 0)
426 		goto fail;
427 
428 	error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
429 	    &sc->family);
430 	KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
431 
432 	/* Create the common code nic object. */
433 	SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
434 			      device_get_nameunit(sc->dev), "nic");
435 	if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
436 	    &sc->bar, &sc->enp_lock, &enp)) != 0)
437 		goto fail3;
438 	sc->enp = enp;
439 
440 	if (!ISP2(sfxge_rx_ring_entries) ||
441 	    !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
442 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
443 		    SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
444 		    EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
445 		error = EINVAL;
446 		goto fail_rx_ring_entries;
447 	}
448 	sc->rxq_entries = sfxge_rx_ring_entries;
449 
450 	if (!ISP2(sfxge_tx_ring_entries) ||
451 	    !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
452 		log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
453 		    SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
454 		    EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
455 		error = EINVAL;
456 		goto fail_tx_ring_entries;
457 	}
458 	sc->txq_entries = sfxge_tx_ring_entries;
459 
460 	/* Initialize MCDI to talk to the microcontroller. */
461 	if ((error = sfxge_mcdi_init(sc)) != 0)
462 		goto fail4;
463 
464 	/* Probe the NIC and build the configuration data area. */
465 	if ((error = efx_nic_probe(enp)) != 0)
466 		goto fail5;
467 
468 	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
469 			  SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
470 			  OID_AUTO, "version", CTLFLAG_RD,
471 			  SFXGE_VERSION_STRING, 0,
472 			  "Driver version");
473 
474 	/* Initialize the NVRAM. */
475 	if ((error = efx_nvram_init(enp)) != 0)
476 		goto fail6;
477 
478 	/* Initialize the VPD. */
479 	if ((error = efx_vpd_init(enp)) != 0)
480 		goto fail7;
481 
482 	/* Reset the NIC. */
483 	if ((error = efx_nic_reset(enp)) != 0)
484 		goto fail8;
485 
486 	/* Initialize buffer table allocation. */
487 	sc->buffer_table_next = 0;
488 
489 	/* Set up interrupts. */
490 	if ((error = sfxge_intr_init(sc)) != 0)
491 		goto fail8;
492 
493 	/* Initialize event processing state. */
494 	if ((error = sfxge_ev_init(sc)) != 0)
495 		goto fail11;
496 
497 	/* Initialize receive state. */
498 	if ((error = sfxge_rx_init(sc)) != 0)
499 		goto fail12;
500 
501 	/* Initialize transmit state. */
502 	if ((error = sfxge_tx_init(sc)) != 0)
503 		goto fail13;
504 
505 	/* Initialize port state. */
506 	if ((error = sfxge_port_init(sc)) != 0)
507 		goto fail14;
508 
509 	sc->init_state = SFXGE_INITIALIZED;
510 
511 	return (0);
512 
513 fail14:
514 	sfxge_tx_fini(sc);
515 
516 fail13:
517 	sfxge_rx_fini(sc);
518 
519 fail12:
520 	sfxge_ev_fini(sc);
521 
522 fail11:
523 	sfxge_intr_fini(sc);
524 
525 fail8:
526 	efx_vpd_fini(enp);
527 
528 fail7:
529 	efx_nvram_fini(enp);
530 
531 fail6:
532 	efx_nic_unprobe(enp);
533 
534 fail5:
535 	sfxge_mcdi_fini(sc);
536 
537 fail4:
538 fail_tx_ring_entries:
539 fail_rx_ring_entries:
540 	sc->enp = NULL;
541 	efx_nic_destroy(enp);
542 	SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
543 
544 fail3:
545 	sfxge_bar_fini(sc);
546 	(void) pci_disable_busmaster(sc->dev);
547 
548 fail:
549 	sc->dev = NULL;
550 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
551 	return (error);
552 }
553 
554 static void
555 sfxge_destroy(struct sfxge_softc *sc)
556 {
557 	efx_nic_t *enp;
558 
559 	/* Clean up port state. */
560 	sfxge_port_fini(sc);
561 
562 	/* Clean up transmit state. */
563 	sfxge_tx_fini(sc);
564 
565 	/* Clean up receive state. */
566 	sfxge_rx_fini(sc);
567 
568 	/* Clean up event processing state. */
569 	sfxge_ev_fini(sc);
570 
571 	/* Clean up interrupts. */
572 	sfxge_intr_fini(sc);
573 
574 	/* Tear down common code subsystems. */
575 	efx_nic_reset(sc->enp);
576 	efx_vpd_fini(sc->enp);
577 	efx_nvram_fini(sc->enp);
578 	efx_nic_unprobe(sc->enp);
579 
580 	/* Tear down MCDI. */
581 	sfxge_mcdi_fini(sc);
582 
583 	/* Destroy common code context. */
584 	enp = sc->enp;
585 	sc->enp = NULL;
586 	efx_nic_destroy(enp);
587 
588 	/* Free DMA memory. */
589 	sfxge_dma_fini(sc);
590 
591 	/* Free mapped BARs. */
592 	sfxge_bar_fini(sc);
593 
594 	(void) pci_disable_busmaster(sc->dev);
595 
596 	taskqueue_drain(taskqueue_thread, &sc->task_reset);
597 
598 	/* Destroy the softc lock. */
599 	SFXGE_ADAPTER_LOCK_DESTROY(sc);
600 }
601 
602 static int
603 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
604 {
605 	struct sfxge_softc *sc = arg1;
606 	efx_vpd_value_t value;
607 	int rc;
608 
609 	value.evv_tag = arg2 >> 16;
610 	value.evv_keyword = arg2 & 0xffff;
611 	if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
612 	    != 0)
613 		return (rc);
614 
615 	return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
616 }
617 
618 static void
619 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
620 		  efx_vpd_tag_t tag, const char *keyword)
621 {
622 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
623 	efx_vpd_value_t value;
624 
625 	/* Check whether VPD tag/keyword is present */
626 	value.evv_tag = tag;
627 	value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
628 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
629 		return;
630 
631 	SYSCTL_ADD_PROC(
632 		ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
633 		sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
634 		sfxge_vpd_handler, "A", "");
635 }
636 
637 static int
638 sfxge_vpd_init(struct sfxge_softc *sc)
639 {
640 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
641 	struct sysctl_oid *vpd_node;
642 	struct sysctl_oid_list *vpd_list;
643 	char keyword[3];
644 	efx_vpd_value_t value;
645 	int rc;
646 
647 	if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
648 		goto fail;
649 	sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
650 	if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
651 		goto fail2;
652 
653 	/* Copy ID (product name) into device description, and log it. */
654 	value.evv_tag = EFX_VPD_ID;
655 	if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
656 		value.evv_value[value.evv_length] = 0;
657 		device_set_desc_copy(sc->dev, value.evv_value);
658 		device_printf(sc->dev, "%s\n", value.evv_value);
659 	}
660 
661 	vpd_node = SYSCTL_ADD_NODE(
662 		ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
663 		OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
664 	vpd_list = SYSCTL_CHILDREN(vpd_node);
665 
666 	/* Add sysctls for all expected and any vendor-defined keywords. */
667 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
668 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
669 	sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
670 	keyword[0] = 'V';
671 	keyword[2] = 0;
672 	for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
673 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
674 	for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
675 		sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
676 
677 	return (0);
678 
679 fail2:
680 	free(sc->vpd_data, M_SFXGE);
681 fail:
682 	return (rc);
683 }
684 
685 static void
686 sfxge_vpd_fini(struct sfxge_softc *sc)
687 {
688 	free(sc->vpd_data, M_SFXGE);
689 }
690 
691 static void
692 sfxge_reset(void *arg, int npending)
693 {
694 	struct sfxge_softc *sc;
695 	int rc;
696 
697 	(void)npending;
698 
699 	sc = (struct sfxge_softc *)arg;
700 
701 	SFXGE_ADAPTER_LOCK(sc);
702 
703 	if (sc->init_state != SFXGE_STARTED)
704 		goto done;
705 
706 	sfxge_stop(sc);
707 	efx_nic_reset(sc->enp);
708 	if ((rc = sfxge_start(sc)) != 0)
709 		device_printf(sc->dev,
710 			      "reset failed (%d); interface is now stopped\n",
711 			      rc);
712 
713 done:
714 	SFXGE_ADAPTER_UNLOCK(sc);
715 }
716 
717 void
718 sfxge_schedule_reset(struct sfxge_softc *sc)
719 {
720 	taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
721 }
722 
723 static int
724 sfxge_attach(device_t dev)
725 {
726 	struct sfxge_softc *sc;
727 	struct ifnet *ifp;
728 	int error;
729 
730 	sc = device_get_softc(dev);
731 	sc->dev = dev;
732 
733 	/* Allocate ifnet. */
734 	ifp = if_alloc(IFT_ETHER);
735 	if (ifp == NULL) {
736 		device_printf(dev, "Couldn't allocate ifnet\n");
737 		error = ENOMEM;
738 		goto fail;
739 	}
740 	sc->ifnet = ifp;
741 
742 	/* Initialize hardware. */
743 	if ((error = sfxge_create(sc)) != 0)
744 		goto fail2;
745 
746 	/* Create the ifnet for the port. */
747 	if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
748 		goto fail3;
749 
750 	if ((error = sfxge_vpd_init(sc)) != 0)
751 		goto fail4;
752 
753 	sc->init_state = SFXGE_REGISTERED;
754 
755 	return (0);
756 
757 fail4:
758 	sfxge_ifnet_fini(ifp);
759 fail3:
760 	sfxge_destroy(sc);
761 
762 fail2:
763 	if_free(sc->ifnet);
764 
765 fail:
766 	return (error);
767 }
768 
769 static int
770 sfxge_detach(device_t dev)
771 {
772 	struct sfxge_softc *sc;
773 
774 	sc = device_get_softc(dev);
775 
776 	sfxge_vpd_fini(sc);
777 
778 	/* Destroy the ifnet. */
779 	sfxge_ifnet_fini(sc->ifnet);
780 
781 	/* Tear down hardware. */
782 	sfxge_destroy(sc);
783 
784 	return (0);
785 }
786 
787 static int
788 sfxge_probe(device_t dev)
789 {
790 	uint16_t pci_vendor_id;
791 	uint16_t pci_device_id;
792 	efx_family_t family;
793 	int rc;
794 
795 	pci_vendor_id = pci_get_vendor(dev);
796 	pci_device_id = pci_get_device(dev);
797 
798 	rc = efx_family(pci_vendor_id, pci_device_id, &family);
799 	if (rc != 0)
800 		return (ENXIO);
801 
802 	KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
803 	device_set_desc(dev, "Solarflare SFC9000 family");
804 	return (0);
805 }
806 
807 static device_method_t sfxge_methods[] = {
808 	DEVMETHOD(device_probe,		sfxge_probe),
809 	DEVMETHOD(device_attach,	sfxge_attach),
810 	DEVMETHOD(device_detach,	sfxge_detach),
811 
812 	DEVMETHOD_END
813 };
814 
815 static devclass_t sfxge_devclass;
816 
817 static driver_t sfxge_driver = {
818 	"sfxge",
819 	sfxge_methods,
820 	sizeof(struct sfxge_softc)
821 };
822 
823 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);
824