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