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