]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/sfxge.c
sfxge: advertise IPv6 Rx and Tx checksum offload support
[FreeBSD/FreeBSD.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_var.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55
56 #include "common/efx.h"
57
58 #include "sfxge.h"
59 #include "sfxge_rx.h"
60 #include "sfxge_version.h"
61
62 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM |                 \
63                    IFCAP_RXCSUM | IFCAP_TXCSUM | IFCAP_TSO |            \
64                    IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 |              \
65                    IFCAP_JUMBO_MTU | IFCAP_LRO |                        \
66                    IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS)
67 #define SFXGE_CAP_ENABLE SFXGE_CAP
68 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_RXCSUM | IFCAP_VLAN_HWCSUM | \
69                          IFCAP_RXCSUM_IPV6 |                            \
70                          IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS)
71
72 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
73
74
75 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
76             "SFXGE driver parameters");
77
78 #define SFXGE_PARAM_RX_RING     SFXGE_PARAM(rx_ring)
79 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
80 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
81 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
82            &sfxge_rx_ring_entries, 0,
83            "Maximum number of descriptors in a receive ring");
84
85 #define SFXGE_PARAM_TX_RING     SFXGE_PARAM(tx_ring)
86 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
87 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
88 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
89            &sfxge_tx_ring_entries, 0,
90            "Maximum number of descriptors in a transmit ring");
91
92
93 static void
94 sfxge_reset(void *arg, int npending);
95
96 static int
97 sfxge_start(struct sfxge_softc *sc)
98 {
99         int rc;
100
101         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
102
103         if (sc->init_state == SFXGE_STARTED)
104                 return (0);
105
106         if (sc->init_state != SFXGE_REGISTERED) {
107                 rc = EINVAL;
108                 goto fail;
109         }
110
111         if ((rc = efx_nic_init(sc->enp)) != 0)
112                 goto fail;
113
114         /* Start processing interrupts. */
115         if ((rc = sfxge_intr_start(sc)) != 0)
116                 goto fail2;
117
118         /* Start processing events. */
119         if ((rc = sfxge_ev_start(sc)) != 0)
120                 goto fail3;
121
122         /* Start the receiver side. */
123         if ((rc = sfxge_rx_start(sc)) != 0)
124                 goto fail4;
125
126         /* Start the transmitter side. */
127         if ((rc = sfxge_tx_start(sc)) != 0)
128                 goto fail5;
129
130         /* Fire up the port. */
131         if ((rc = sfxge_port_start(sc)) != 0)
132                 goto fail6;
133
134         sc->init_state = SFXGE_STARTED;
135
136         /* Tell the stack we're running. */
137         sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
138         sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
139
140         return (0);
141
142 fail6:
143         sfxge_tx_stop(sc);
144
145 fail5:
146         sfxge_rx_stop(sc);
147
148 fail4:
149         sfxge_ev_stop(sc);
150
151 fail3:
152         sfxge_intr_stop(sc);
153
154 fail2:
155         efx_nic_fini(sc->enp);
156
157 fail:
158         device_printf(sc->dev, "sfxge_start: %d\n", rc);
159
160         return (rc);
161 }
162
163 static void
164 sfxge_if_init(void *arg)
165 {
166         struct sfxge_softc *sc;
167
168         sc = (struct sfxge_softc *)arg;
169
170         SFXGE_ADAPTER_LOCK(sc);
171         (void)sfxge_start(sc);
172         SFXGE_ADAPTER_UNLOCK(sc);
173 }
174
175 static void
176 sfxge_stop(struct sfxge_softc *sc)
177 {
178         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
179
180         if (sc->init_state != SFXGE_STARTED)
181                 return;
182
183         sc->init_state = SFXGE_REGISTERED;
184
185         /* Stop the port. */
186         sfxge_port_stop(sc);
187
188         /* Stop the transmitter. */
189         sfxge_tx_stop(sc);
190
191         /* Stop the receiver. */
192         sfxge_rx_stop(sc);
193
194         /* Stop processing events. */
195         sfxge_ev_stop(sc);
196
197         /* Stop processing interrupts. */
198         sfxge_intr_stop(sc);
199
200         efx_nic_fini(sc->enp);
201
202         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
203 }
204
205 static int
206 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
207 {
208         struct sfxge_softc *sc;
209         struct ifreq *ifr;
210         int error;
211
212         ifr = (struct ifreq *)data;
213         sc = ifp->if_softc;
214         error = 0;
215
216         switch (command) {
217         case SIOCSIFFLAGS:
218                 SFXGE_ADAPTER_LOCK(sc);
219                 if (ifp->if_flags & IFF_UP) {
220                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
221                                 if ((ifp->if_flags ^ sc->if_flags) &
222                                     (IFF_PROMISC | IFF_ALLMULTI)) {
223                                         sfxge_mac_filter_set(sc);
224                                 }
225                         } else
226                                 sfxge_start(sc);
227                 } else
228                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
229                                 sfxge_stop(sc);
230                 sc->if_flags = ifp->if_flags;
231                 SFXGE_ADAPTER_UNLOCK(sc);
232                 break;
233         case SIOCSIFMTU:
234                 if (ifr->ifr_mtu == ifp->if_mtu) {
235                         /* Nothing to do */
236                         error = 0;
237                 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
238                         error = EINVAL;
239                 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
240                         ifp->if_mtu = ifr->ifr_mtu;
241                         error = 0;
242                 } else {
243                         /* Restart required */
244                         SFXGE_ADAPTER_LOCK(sc);
245                         sfxge_stop(sc);
246                         ifp->if_mtu = ifr->ifr_mtu;
247                         error = sfxge_start(sc);
248                         SFXGE_ADAPTER_UNLOCK(sc);
249                         if (error != 0) {
250                                 ifp->if_flags &= ~IFF_UP;
251                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
252                                 if_down(ifp);
253                         }
254                 }
255                 break;
256         case SIOCADDMULTI:
257         case SIOCDELMULTI:
258                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
259                         sfxge_mac_filter_set(sc);
260                 break;
261         case SIOCSIFCAP:
262                 SFXGE_ADAPTER_LOCK(sc);
263
264                 /*
265                  * The networking core already rejects attempts to
266                  * enable capabilities we don't have.  We still have
267                  * to reject attempts to disable capabilities that we
268                  * can't (yet) disable.
269                  */
270                 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
271                         error = EINVAL;
272                         SFXGE_ADAPTER_UNLOCK(sc);
273                         break;
274                 }
275
276                 ifp->if_capenable = ifr->ifr_reqcap;
277                 if (ifp->if_capenable & IFCAP_TXCSUM)
278                         ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
279                 else
280                         ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
281                 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
282                         ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
283                 else
284                         ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
285                 if (ifp->if_capenable & IFCAP_TSO)
286                         ifp->if_hwassist |= CSUM_TSO;
287                 else
288                         ifp->if_hwassist &= ~CSUM_TSO;
289
290                 SFXGE_ADAPTER_UNLOCK(sc);
291                 break;
292         case SIOCSIFMEDIA:
293         case SIOCGIFMEDIA:
294                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
295                 break;
296         default:
297                 error = ether_ioctl(ifp, command, data);
298         }
299
300         return (error);
301 }
302
303 static void
304 sfxge_ifnet_fini(struct ifnet *ifp)
305 {
306         struct sfxge_softc *sc = ifp->if_softc;
307
308         SFXGE_ADAPTER_LOCK(sc);
309         sfxge_stop(sc);
310         SFXGE_ADAPTER_UNLOCK(sc);
311
312         ifmedia_removeall(&sc->media);
313         ether_ifdetach(ifp);
314         if_free(ifp);
315 }
316
317 static int
318 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
319 {
320         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
321         device_t dev;
322         int rc;
323
324         dev = sc->dev;
325         sc->ifnet = ifp;
326
327         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
328         ifp->if_init = sfxge_if_init;
329         ifp->if_softc = sc;
330         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
331         ifp->if_ioctl = sfxge_if_ioctl;
332
333         ifp->if_capabilities = SFXGE_CAP;
334         ifp->if_capenable = SFXGE_CAP_ENABLE;
335         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
336                            CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
337
338         ether_ifattach(ifp, encp->enc_mac_addr);
339
340         ifp->if_transmit = sfxge_if_transmit;
341         ifp->if_qflush = sfxge_if_qflush;
342
343         ifp->if_get_counter = sfxge_get_counter;
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);