]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge.c
MFC: r282903
[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_RXCSUM | IFCAP_VLAN_HWCSUM | \
68                          IFCAP_RXCSUM_IPV6 |                            \
69                          IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
70
71 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
72
73
74 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
75             "SFXGE driver parameters");
76
77 #define SFXGE_PARAM_RX_RING     SFXGE_PARAM(rx_ring)
78 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
79 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
80 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
81            &sfxge_rx_ring_entries, 0,
82            "Maximum number of descriptors in a receive ring");
83
84 #define SFXGE_PARAM_TX_RING     SFXGE_PARAM(tx_ring)
85 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
86 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
87 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
88            &sfxge_tx_ring_entries, 0,
89            "Maximum number of descriptors in a transmit ring");
90
91
92 static void
93 sfxge_reset(void *arg, int npending);
94
95 static int
96 sfxge_start(struct sfxge_softc *sc)
97 {
98         int rc;
99
100         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
101
102         if (sc->init_state == SFXGE_STARTED)
103                 return (0);
104
105         if (sc->init_state != SFXGE_REGISTERED) {
106                 rc = EINVAL;
107                 goto fail;
108         }
109
110         if ((rc = efx_nic_init(sc->enp)) != 0)
111                 goto fail;
112
113         /* Start processing interrupts. */
114         if ((rc = sfxge_intr_start(sc)) != 0)
115                 goto fail2;
116
117         /* Start processing events. */
118         if ((rc = sfxge_ev_start(sc)) != 0)
119                 goto fail3;
120
121         /* Start the receiver side. */
122         if ((rc = sfxge_rx_start(sc)) != 0)
123                 goto fail4;
124
125         /* Start the transmitter side. */
126         if ((rc = sfxge_tx_start(sc)) != 0)
127                 goto fail5;
128
129         /* Fire up the port. */
130         if ((rc = sfxge_port_start(sc)) != 0)
131                 goto fail6;
132
133         sc->init_state = SFXGE_STARTED;
134
135         /* Tell the stack we're running. */
136         sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
137         sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
138
139         return (0);
140
141 fail6:
142         sfxge_tx_stop(sc);
143
144 fail5:
145         sfxge_rx_stop(sc);
146
147 fail4:
148         sfxge_ev_stop(sc);
149
150 fail3:
151         sfxge_intr_stop(sc);
152
153 fail2:
154         efx_nic_fini(sc->enp);
155
156 fail:
157         device_printf(sc->dev, "sfxge_start: %d\n", rc);
158
159         return (rc);
160 }
161
162 static void
163 sfxge_if_init(void *arg)
164 {
165         struct sfxge_softc *sc;
166
167         sc = (struct sfxge_softc *)arg;
168
169         SFXGE_ADAPTER_LOCK(sc);
170         (void)sfxge_start(sc);
171         SFXGE_ADAPTER_UNLOCK(sc);
172 }
173
174 static void
175 sfxge_stop(struct sfxge_softc *sc)
176 {
177         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
178
179         if (sc->init_state != SFXGE_STARTED)
180                 return;
181
182         sc->init_state = SFXGE_REGISTERED;
183
184         /* Stop the port. */
185         sfxge_port_stop(sc);
186
187         /* Stop the transmitter. */
188         sfxge_tx_stop(sc);
189
190         /* Stop the receiver. */
191         sfxge_rx_stop(sc);
192
193         /* Stop processing events. */
194         sfxge_ev_stop(sc);
195
196         /* Stop processing interrupts. */
197         sfxge_intr_stop(sc);
198
199         efx_nic_fini(sc->enp);
200
201         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
202 }
203
204 static int
205 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
206 {
207         struct sfxge_softc *sc;
208         struct ifreq *ifr;
209         int error;
210
211         ifr = (struct ifreq *)data;
212         sc = ifp->if_softc;
213         error = 0;
214
215         switch (command) {
216         case SIOCSIFFLAGS:
217                 SFXGE_ADAPTER_LOCK(sc);
218                 if (ifp->if_flags & IFF_UP) {
219                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
220                                 if ((ifp->if_flags ^ sc->if_flags) &
221                                     (IFF_PROMISC | IFF_ALLMULTI)) {
222                                         sfxge_mac_filter_set(sc);
223                                 }
224                         } else
225                                 sfxge_start(sc);
226                 } else
227                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
228                                 sfxge_stop(sc);
229                 sc->if_flags = ifp->if_flags;
230                 SFXGE_ADAPTER_UNLOCK(sc);
231                 break;
232         case SIOCSIFMTU:
233                 if (ifr->ifr_mtu == ifp->if_mtu) {
234                         /* Nothing to do */
235                         error = 0;
236                 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
237                         error = EINVAL;
238                 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
239                         ifp->if_mtu = ifr->ifr_mtu;
240                         error = 0;
241                 } else {
242                         /* Restart required */
243                         SFXGE_ADAPTER_LOCK(sc);
244                         sfxge_stop(sc);
245                         ifp->if_mtu = ifr->ifr_mtu;
246                         error = sfxge_start(sc);
247                         SFXGE_ADAPTER_UNLOCK(sc);
248                         if (error != 0) {
249                                 ifp->if_flags &= ~IFF_UP;
250                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
251                                 if_down(ifp);
252                         }
253                 }
254                 break;
255         case SIOCADDMULTI:
256         case SIOCDELMULTI:
257                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
258                         sfxge_mac_filter_set(sc);
259                 break;
260         case SIOCSIFCAP:
261                 SFXGE_ADAPTER_LOCK(sc);
262
263                 /*
264                  * The networking core already rejects attempts to
265                  * enable capabilities we don't have.  We still have
266                  * to reject attempts to disable capabilities that we
267                  * can't (yet) disable.
268                  */
269                 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
270                         error = EINVAL;
271                         SFXGE_ADAPTER_UNLOCK(sc);
272                         break;
273                 }
274
275                 ifp->if_capenable = ifr->ifr_reqcap;
276                 if (ifp->if_capenable & IFCAP_TXCSUM)
277                         ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
278                 else
279                         ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
280                 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
281                         ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
282                 else
283                         ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
284                 if (ifp->if_capenable & IFCAP_TSO)
285                         ifp->if_hwassist |= CSUM_TSO;
286                 else
287                         ifp->if_hwassist &= ~CSUM_TSO;
288
289                 SFXGE_ADAPTER_UNLOCK(sc);
290                 break;
291         case SIOCSIFMEDIA:
292         case SIOCGIFMEDIA:
293                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
294                 break;
295         default:
296                 error = ether_ioctl(ifp, command, data);
297         }
298
299         return (error);
300 }
301
302 static void
303 sfxge_ifnet_fini(struct ifnet *ifp)
304 {
305         struct sfxge_softc *sc = ifp->if_softc;
306
307         SFXGE_ADAPTER_LOCK(sc);
308         sfxge_stop(sc);
309         SFXGE_ADAPTER_UNLOCK(sc);
310
311         ifmedia_removeall(&sc->media);
312         ether_ifdetach(ifp);
313         if_free(ifp);
314 }
315
316 static int
317 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
318 {
319         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
320         device_t dev;
321         int rc;
322
323         dev = sc->dev;
324         sc->ifnet = ifp;
325
326         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
327         ifp->if_init = sfxge_if_init;
328         ifp->if_softc = sc;
329         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
330         ifp->if_ioctl = sfxge_if_ioctl;
331
332         ifp->if_capabilities = SFXGE_CAP;
333         ifp->if_capenable = SFXGE_CAP_ENABLE;
334         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
335                            CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
336
337         ether_ifattach(ifp, encp->enc_mac_addr);
338
339         ifp->if_transmit = sfxge_if_transmit;
340         ifp->if_qflush = sfxge_if_qflush;
341
342         if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
343                 goto fail;
344
345         return (0);
346
347 fail:
348         ether_ifdetach(sc->ifnet);
349         return (rc);
350 }
351
352 void
353 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
354 {
355         KASSERT(sc->buffer_table_next + n <=
356                 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
357                 ("buffer table full"));
358
359         *idp = sc->buffer_table_next;
360         sc->buffer_table_next += n;
361 }
362
363 static int
364 sfxge_bar_init(struct sfxge_softc *sc)
365 {
366         efsys_bar_t *esbp = &sc->bar;
367
368         esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
369         if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
370             &esbp->esb_rid, RF_ACTIVE)) == NULL) {
371                 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
372                     EFX_MEM_BAR);
373                 return (ENXIO);
374         }
375         esbp->esb_tag = rman_get_bustag(esbp->esb_res);
376         esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
377
378         SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
379
380         return (0);
381 }
382
383 static void
384 sfxge_bar_fini(struct sfxge_softc *sc)
385 {
386         efsys_bar_t *esbp = &sc->bar;
387
388         bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
389             esbp->esb_res);
390         SFXGE_BAR_LOCK_DESTROY(esbp);
391 }
392
393 static int
394 sfxge_create(struct sfxge_softc *sc)
395 {
396         device_t dev;
397         efx_nic_t *enp;
398         int error;
399         char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
400
401         dev = sc->dev;
402
403         SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
404
405         sc->max_rss_channels = 0;
406         snprintf(rss_param_name, sizeof(rss_param_name),
407                  SFXGE_PARAM(%d.max_rss_channels),
408                  (int)device_get_unit(dev));
409         TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
410
411         sc->stats_node = SYSCTL_ADD_NODE(
412                 device_get_sysctl_ctx(dev),
413                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
414                 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
415         if (sc->stats_node == NULL) {
416                 error = ENOMEM;
417                 goto fail;
418         }
419
420         TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
421
422         (void) pci_enable_busmaster(dev);
423
424         /* Initialize DMA mappings. */
425         if ((error = sfxge_dma_init(sc)) != 0)
426                 goto fail;
427
428         /* Map the device registers. */
429         if ((error = sfxge_bar_init(sc)) != 0)
430                 goto fail;
431
432         error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
433             &sc->family);
434         KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
435
436         /* Create the common code nic object. */
437         SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
438                               device_get_nameunit(sc->dev), "nic");
439         if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
440             &sc->bar, &sc->enp_lock, &enp)) != 0)
441                 goto fail3;
442         sc->enp = enp;
443
444         if (!ISP2(sfxge_rx_ring_entries) ||
445             !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
446                 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
447                     SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
448                     EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
449                 error = EINVAL;
450                 goto fail_rx_ring_entries;
451         }
452         sc->rxq_entries = sfxge_rx_ring_entries;
453
454         if (!ISP2(sfxge_tx_ring_entries) ||
455             !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
456                 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
457                     SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
458                     EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
459                 error = EINVAL;
460                 goto fail_tx_ring_entries;
461         }
462         sc->txq_entries = sfxge_tx_ring_entries;
463
464         /* Initialize MCDI to talk to the microcontroller. */
465         if ((error = sfxge_mcdi_init(sc)) != 0)
466                 goto fail4;
467
468         /* Probe the NIC and build the configuration data area. */
469         if ((error = efx_nic_probe(enp)) != 0)
470                 goto fail5;
471
472         SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
473                           SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
474                           OID_AUTO, "version", CTLFLAG_RD,
475                           SFXGE_VERSION_STRING, 0,
476                           "Driver version");
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);