]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge.c
MFC: 278250
[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
60 #define SFXGE_CAP (IFCAP_VLAN_MTU | \
61                    IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO |       \
62                    IFCAP_JUMBO_MTU | IFCAP_LRO |                        \
63                    IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
64 #define SFXGE_CAP_ENABLE SFXGE_CAP
65 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \
66                          IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
67
68 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
69
70
71 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
72             "SFXGE driver parameters");
73
74 #define SFXGE_PARAM_RX_RING     SFXGE_PARAM(rx_ring)
75 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
76 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
77 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
78            &sfxge_rx_ring_entries, 0,
79            "Maximum number of descriptors in a receive ring");
80
81 #define SFXGE_PARAM_TX_RING     SFXGE_PARAM(tx_ring)
82 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
83 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
84 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
85            &sfxge_tx_ring_entries, 0,
86            "Maximum number of descriptors in a transmit ring");
87
88
89 static void
90 sfxge_reset(void *arg, int npending);
91
92 static int
93 sfxge_start(struct sfxge_softc *sc)
94 {
95         int rc;
96
97         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
98
99         if (sc->init_state == SFXGE_STARTED)
100                 return (0);
101
102         if (sc->init_state != SFXGE_REGISTERED) {
103                 rc = EINVAL;
104                 goto fail;
105         }
106
107         if ((rc = efx_nic_init(sc->enp)) != 0)
108                 goto fail;
109
110         /* Start processing interrupts. */
111         if ((rc = sfxge_intr_start(sc)) != 0)
112                 goto fail2;
113
114         /* Start processing events. */
115         if ((rc = sfxge_ev_start(sc)) != 0)
116                 goto fail3;
117
118         /* Start the receiver side. */
119         if ((rc = sfxge_rx_start(sc)) != 0)
120                 goto fail4;
121
122         /* Start the transmitter side. */
123         if ((rc = sfxge_tx_start(sc)) != 0)
124                 goto fail5;
125
126         /* Fire up the port. */
127         if ((rc = sfxge_port_start(sc)) != 0)
128                 goto fail6;
129
130         sc->init_state = SFXGE_STARTED;
131
132         /* Tell the stack we're running. */
133         sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
134         sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
135
136         return (0);
137
138 fail6:
139         sfxge_tx_stop(sc);
140
141 fail5:
142         sfxge_rx_stop(sc);
143
144 fail4:
145         sfxge_ev_stop(sc);
146
147 fail3:
148         sfxge_intr_stop(sc);
149
150 fail2:
151         efx_nic_fini(sc->enp);
152
153 fail:
154         device_printf(sc->dev, "sfxge_start: %d\n", rc);
155
156         return (rc);
157 }
158
159 static void
160 sfxge_if_init(void *arg)
161 {
162         struct sfxge_softc *sc;
163
164         sc = (struct sfxge_softc *)arg;
165
166         SFXGE_ADAPTER_LOCK(sc);
167         (void)sfxge_start(sc);
168         SFXGE_ADAPTER_UNLOCK(sc);
169 }
170
171 static void
172 sfxge_stop(struct sfxge_softc *sc)
173 {
174         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
175
176         if (sc->init_state != SFXGE_STARTED)
177                 return;
178
179         sc->init_state = SFXGE_REGISTERED;
180
181         /* Stop the port. */
182         sfxge_port_stop(sc);
183
184         /* Stop the transmitter. */
185         sfxge_tx_stop(sc);
186
187         /* Stop the receiver. */
188         sfxge_rx_stop(sc);
189
190         /* Stop processing events. */
191         sfxge_ev_stop(sc);
192
193         /* Stop processing interrupts. */
194         sfxge_intr_stop(sc);
195
196         efx_nic_fini(sc->enp);
197
198         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
199 }
200
201 static int
202 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
203 {
204         struct sfxge_softc *sc;
205         struct ifreq *ifr;
206         int error;
207
208         ifr = (struct ifreq *)data;
209         sc = ifp->if_softc;
210         error = 0;
211
212         switch (command) {
213         case SIOCSIFFLAGS:
214                 SFXGE_ADAPTER_LOCK(sc);
215                 if (ifp->if_flags & IFF_UP) {
216                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
217                                 if ((ifp->if_flags ^ sc->if_flags) &
218                                     (IFF_PROMISC | IFF_ALLMULTI)) {
219                                         sfxge_mac_filter_set(sc);
220                                 }
221                         } else
222                                 sfxge_start(sc);
223                 } else
224                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
225                                 sfxge_stop(sc);
226                 sc->if_flags = ifp->if_flags;
227                 SFXGE_ADAPTER_UNLOCK(sc);
228                 break;
229         case SIOCSIFMTU:
230                 if (ifr->ifr_mtu == ifp->if_mtu) {
231                         /* Nothing to do */
232                         error = 0;
233                 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
234                         error = EINVAL;
235                 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
236                         ifp->if_mtu = ifr->ifr_mtu;
237                         error = 0;
238                 } else {
239                         /* Restart required */
240                         SFXGE_ADAPTER_LOCK(sc);
241                         sfxge_stop(sc);
242                         ifp->if_mtu = ifr->ifr_mtu;
243                         error = sfxge_start(sc);
244                         SFXGE_ADAPTER_UNLOCK(sc);
245                         if (error != 0) {
246                                 ifp->if_flags &= ~IFF_UP;
247                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
248                                 if_down(ifp);
249                         }
250                 }
251                 break;
252         case SIOCADDMULTI:
253         case SIOCDELMULTI:
254                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
255                         sfxge_mac_filter_set(sc);
256                 break;
257         case SIOCSIFCAP:
258                 SFXGE_ADAPTER_LOCK(sc);
259
260                 /*
261                  * The networking core already rejects attempts to
262                  * enable capabilities we don't have.  We still have
263                  * to reject attempts to disable capabilities that we
264                  * can't (yet) disable.
265                  */
266                 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
267                         error = EINVAL;
268                         SFXGE_ADAPTER_UNLOCK(sc);
269                         break;
270                 }
271
272                 ifp->if_capenable = ifr->ifr_reqcap;
273                 if (ifp->if_capenable & IFCAP_TXCSUM)
274                         ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
275                 else
276                         ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
277                 if (ifp->if_capenable & IFCAP_TSO)
278                         ifp->if_hwassist |= CSUM_TSO;
279                 else
280                         ifp->if_hwassist &= ~CSUM_TSO;
281
282                 SFXGE_ADAPTER_UNLOCK(sc);
283                 break;
284         case SIOCSIFMEDIA:
285         case SIOCGIFMEDIA:
286                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
287                 break;
288         default:
289                 error = ether_ioctl(ifp, command, data);
290         }
291
292         return (error);
293 }
294
295 static void
296 sfxge_ifnet_fini(struct ifnet *ifp)
297 {
298         struct sfxge_softc *sc = ifp->if_softc;
299
300         SFXGE_ADAPTER_LOCK(sc);
301         sfxge_stop(sc);
302         SFXGE_ADAPTER_UNLOCK(sc);
303
304         ifmedia_removeall(&sc->media);
305         ether_ifdetach(ifp);
306         if_free(ifp);
307 }
308
309 static int
310 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
311 {
312         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
313         device_t dev;
314         int rc;
315
316         dev = sc->dev;
317         sc->ifnet = ifp;
318
319         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
320         ifp->if_init = sfxge_if_init;
321         ifp->if_softc = sc;
322         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
323         ifp->if_ioctl = sfxge_if_ioctl;
324
325         ifp->if_capabilities = SFXGE_CAP;
326         ifp->if_capenable = SFXGE_CAP_ENABLE;
327         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
328
329         ether_ifattach(ifp, encp->enc_mac_addr);
330
331 #ifdef SFXGE_HAVE_MQ
332         ifp->if_transmit = sfxge_if_transmit;
333         ifp->if_qflush = sfxge_if_qflush;
334 #else
335         ifp->if_start = sfxge_if_start;
336         IFQ_SET_MAXLEN(&ifp->if_snd, sc->txq_entries - 1);
337         ifp->if_snd.ifq_drv_maxlen = sc->txq_entries - 1;
338         IFQ_SET_READY(&ifp->if_snd);
339
340         snprintf(sc->tx_lock_name, sizeof(sc->tx_lock_name),
341                  "%s:tx", device_get_nameunit(sc->dev));
342         mtx_init(&sc->tx_lock, sc->tx_lock_name, NULL, MTX_DEF);
343 #endif
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         /* Initialize the NVRAM. */
476         if ((error = efx_nvram_init(enp)) != 0)
477                 goto fail6;
478
479         /* Initialize the VPD. */
480         if ((error = efx_vpd_init(enp)) != 0)
481                 goto fail7;
482
483         /* Reset the NIC. */
484         if ((error = efx_nic_reset(enp)) != 0)
485                 goto fail8;
486
487         /* Initialize buffer table allocation. */
488         sc->buffer_table_next = 0;
489
490         /* Set up interrupts. */
491         if ((error = sfxge_intr_init(sc)) != 0)
492                 goto fail8;
493
494         /* Initialize event processing state. */
495         if ((error = sfxge_ev_init(sc)) != 0)
496                 goto fail11;
497
498         /* Initialize receive state. */
499         if ((error = sfxge_rx_init(sc)) != 0)
500                 goto fail12;
501
502         /* Initialize transmit state. */
503         if ((error = sfxge_tx_init(sc)) != 0)
504                 goto fail13;
505
506         /* Initialize port state. */
507         if ((error = sfxge_port_init(sc)) != 0)
508                 goto fail14;
509
510         sc->init_state = SFXGE_INITIALIZED;
511
512         return (0);
513
514 fail14:
515         sfxge_tx_fini(sc);
516
517 fail13:
518         sfxge_rx_fini(sc);
519
520 fail12:
521         sfxge_ev_fini(sc);
522
523 fail11:
524         sfxge_intr_fini(sc);
525
526 fail8:
527         efx_vpd_fini(enp);
528
529 fail7:
530         efx_nvram_fini(enp);
531
532 fail6:
533         efx_nic_unprobe(enp);
534
535 fail5:
536         sfxge_mcdi_fini(sc);
537
538 fail4:
539 fail_tx_ring_entries:
540 fail_rx_ring_entries:
541         sc->enp = NULL;
542         efx_nic_destroy(enp);
543         SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
544
545 fail3:
546         sfxge_bar_fini(sc);
547         (void) pci_disable_busmaster(sc->dev);
548
549 fail:
550         sc->dev = NULL;
551         SFXGE_ADAPTER_LOCK_DESTROY(sc);
552         return (error);
553 }
554
555 static void
556 sfxge_destroy(struct sfxge_softc *sc)
557 {
558         efx_nic_t *enp;
559
560         /* Clean up port state. */
561         sfxge_port_fini(sc);
562
563         /* Clean up transmit state. */
564         sfxge_tx_fini(sc);
565
566         /* Clean up receive state. */
567         sfxge_rx_fini(sc);
568
569         /* Clean up event processing state. */
570         sfxge_ev_fini(sc);
571
572         /* Clean up interrupts. */
573         sfxge_intr_fini(sc);
574
575         /* Tear down common code subsystems. */
576         efx_nic_reset(sc->enp);
577         efx_vpd_fini(sc->enp);
578         efx_nvram_fini(sc->enp);
579         efx_nic_unprobe(sc->enp);
580
581         /* Tear down MCDI. */
582         sfxge_mcdi_fini(sc);
583
584         /* Destroy common code context. */
585         enp = sc->enp;
586         sc->enp = NULL;
587         efx_nic_destroy(enp);
588
589         /* Free DMA memory. */
590         sfxge_dma_fini(sc);
591
592         /* Free mapped BARs. */
593         sfxge_bar_fini(sc);
594
595         (void) pci_disable_busmaster(sc->dev);
596
597         taskqueue_drain(taskqueue_thread, &sc->task_reset);
598
599         /* Destroy the softc lock. */
600         SFXGE_ADAPTER_LOCK_DESTROY(sc);
601 }
602
603 static int
604 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
605 {
606         struct sfxge_softc *sc = arg1;
607         efx_vpd_value_t value;
608         int rc;
609
610         value.evv_tag = arg2 >> 16;
611         value.evv_keyword = arg2 & 0xffff;
612         if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
613             != 0)
614                 return (rc);
615
616         return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
617 }
618
619 static void
620 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
621                   efx_vpd_tag_t tag, const char *keyword)
622 {
623         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
624         efx_vpd_value_t value;
625
626         /* Check whether VPD tag/keyword is present */
627         value.evv_tag = tag;
628         value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
629         if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
630                 return;
631
632         SYSCTL_ADD_PROC(
633                 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
634                 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
635                 sfxge_vpd_handler, "A", "");
636 }
637
638 static int
639 sfxge_vpd_init(struct sfxge_softc *sc)
640 {
641         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
642         struct sysctl_oid *vpd_node;
643         struct sysctl_oid_list *vpd_list;
644         char keyword[3];
645         efx_vpd_value_t value;
646         int rc;
647
648         if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
649                 goto fail;
650         sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
651         if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
652                 goto fail2;
653
654         /* Copy ID (product name) into device description, and log it. */
655         value.evv_tag = EFX_VPD_ID;
656         if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
657                 value.evv_value[value.evv_length] = 0;
658                 device_set_desc_copy(sc->dev, value.evv_value);
659                 device_printf(sc->dev, "%s\n", value.evv_value);
660         }
661
662         vpd_node = SYSCTL_ADD_NODE(
663                 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
664                 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
665         vpd_list = SYSCTL_CHILDREN(vpd_node);
666
667         /* Add sysctls for all expected and any vendor-defined keywords. */
668         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
669         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
670         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
671         keyword[0] = 'V';
672         keyword[2] = 0;
673         for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
674                 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
675         for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
676                 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
677
678         return (0);
679
680 fail2:
681         free(sc->vpd_data, M_SFXGE);
682 fail:
683         return (rc);
684 }
685
686 static void
687 sfxge_vpd_fini(struct sfxge_softc *sc)
688 {
689         free(sc->vpd_data, M_SFXGE);
690 }
691
692 static void
693 sfxge_reset(void *arg, int npending)
694 {
695         struct sfxge_softc *sc;
696         int rc;
697
698         (void)npending;
699
700         sc = (struct sfxge_softc *)arg;
701
702         SFXGE_ADAPTER_LOCK(sc);
703
704         if (sc->init_state != SFXGE_STARTED)
705                 goto done;
706
707         sfxge_stop(sc);
708         efx_nic_reset(sc->enp);
709         if ((rc = sfxge_start(sc)) != 0)
710                 device_printf(sc->dev,
711                               "reset failed (%d); interface is now stopped\n",
712                               rc);
713
714 done:
715         SFXGE_ADAPTER_UNLOCK(sc);
716 }
717
718 void
719 sfxge_schedule_reset(struct sfxge_softc *sc)
720 {
721         taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
722 }
723
724 static int
725 sfxge_attach(device_t dev)
726 {
727         struct sfxge_softc *sc;
728         struct ifnet *ifp;
729         int error;
730
731         sc = device_get_softc(dev);
732         sc->dev = dev;
733
734         /* Allocate ifnet. */
735         ifp = if_alloc(IFT_ETHER);
736         if (ifp == NULL) {
737                 device_printf(dev, "Couldn't allocate ifnet\n");
738                 error = ENOMEM;
739                 goto fail;
740         }
741         sc->ifnet = ifp;
742
743         /* Initialize hardware. */
744         if ((error = sfxge_create(sc)) != 0)
745                 goto fail2;
746
747         /* Create the ifnet for the port. */
748         if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
749                 goto fail3;
750
751         if ((error = sfxge_vpd_init(sc)) != 0)
752                 goto fail4;
753
754         sc->init_state = SFXGE_REGISTERED;
755
756         return (0);
757
758 fail4:
759         sfxge_ifnet_fini(ifp);
760 fail3:
761         sfxge_destroy(sc);
762
763 fail2:
764         if_free(sc->ifnet);
765
766 fail:
767         return (error);
768 }
769
770 static int
771 sfxge_detach(device_t dev)
772 {
773         struct sfxge_softc *sc;
774
775         sc = device_get_softc(dev);
776
777         sfxge_vpd_fini(sc);
778
779         /* Destroy the ifnet. */
780         sfxge_ifnet_fini(sc->ifnet);
781
782         /* Tear down hardware. */
783         sfxge_destroy(sc);
784
785         return (0);
786 }
787
788 static int
789 sfxge_probe(device_t dev)
790 {
791         uint16_t pci_vendor_id;
792         uint16_t pci_device_id;
793         efx_family_t family;
794         int rc;
795
796         pci_vendor_id = pci_get_vendor(dev);
797         pci_device_id = pci_get_device(dev);
798
799         rc = efx_family(pci_vendor_id, pci_device_id, &family);
800         if (rc != 0)
801                 return (ENXIO);
802
803         KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
804         device_set_desc(dev, "Solarflare SFC9000 family");
805         return (0);
806 }
807
808 static device_method_t sfxge_methods[] = {
809         DEVMETHOD(device_probe,         sfxge_probe),
810         DEVMETHOD(device_attach,        sfxge_attach),
811         DEVMETHOD(device_detach,        sfxge_detach),
812
813         DEVMETHOD_END
814 };
815
816 static devclass_t sfxge_devclass;
817
818 static driver_t sfxge_driver = {
819         "sfxge",
820         sfxge_methods,
821         sizeof(struct sfxge_softc)
822 };
823
824 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);