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