]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sfxge/sfxge.c
sfxge: implement interface statistics shown by netstat
[FreeBSD/stable/10.git] / sys / dev / sfxge / sfxge.c
1 /*-
2  * Copyright (c) 2010-2015 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 are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of the FreeBSD Project.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/smp.h>
45 #include <sys/socket.h>
46 #include <sys/taskqueue.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
49 #include <sys/priv.h>
50 #include <sys/syslog.h>
51
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59
60 #include "common/efx.h"
61
62 #include "sfxge.h"
63 #include "sfxge_rx.h"
64 #include "sfxge_ioc.h"
65 #include "sfxge_version.h"
66
67 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM |                 \
68                    IFCAP_RXCSUM | IFCAP_TXCSUM |                        \
69                    IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 |              \
70                    IFCAP_TSO4 | IFCAP_TSO6 |                            \
71                    IFCAP_JUMBO_MTU |                                    \
72                    IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS)
73 #define SFXGE_CAP_ENABLE SFXGE_CAP
74 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU |                               \
75                          IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS)
76
77 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
78
79
80 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
81             "SFXGE driver parameters");
82
83 #define SFXGE_PARAM_RX_RING     SFXGE_PARAM(rx_ring)
84 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
85 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
86 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
87            &sfxge_rx_ring_entries, 0,
88            "Maximum number of descriptors in a receive ring");
89
90 #define SFXGE_PARAM_TX_RING     SFXGE_PARAM(tx_ring)
91 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
92 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
93 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
94            &sfxge_tx_ring_entries, 0,
95            "Maximum number of descriptors in a transmit ring");
96
97 #define SFXGE_PARAM_STATS_UPDATE_PERIOD SFXGE_PARAM(stats_update_period)
98 static int sfxge_stats_update_period = SFXGE_CALLOUT_TICKS;
99 TUNABLE_INT(SFXGE_PARAM_STATS_UPDATE_PERIOD,
100             &sfxge_stats_update_period);
101 SYSCTL_INT(_hw_sfxge, OID_AUTO, stats_update_period, CTLFLAG_RDTUN,
102            &sfxge_stats_update_period, 0,
103            "netstat interface statistics update period in ticks");
104
105 static void
106 sfxge_reset(void *arg, int npending);
107
108 static int
109 sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
110 {
111         efx_drv_limits_t limits;
112         int rc;
113         unsigned int evq_max;
114         uint32_t evq_allocated;
115         uint32_t rxq_allocated;
116         uint32_t txq_allocated;
117
118         /*
119          * Limit the number of event queues to:
120          *  - number of CPUs
121          *  - hardwire maximum RSS channels
122          *  - administratively specified maximum RSS channels
123          */
124         evq_max = MIN(mp_ncpus, EFX_MAXRSS);
125         if (sc->max_rss_channels > 0)
126                 evq_max = MIN(evq_max, sc->max_rss_channels);
127
128         memset(&limits, 0, sizeof(limits));
129
130         limits.edl_min_evq_count = 1;
131         limits.edl_max_evq_count = evq_max;
132         limits.edl_min_txq_count = SFXGE_TXQ_NTYPES;
133         limits.edl_max_txq_count = evq_max + SFXGE_TXQ_NTYPES - 1;
134         limits.edl_min_rxq_count = 1;
135         limits.edl_max_rxq_count = evq_max;
136
137         efx_nic_set_drv_limits(sc->enp, &limits);
138
139         if ((rc = efx_nic_init(sc->enp)) != 0)
140                 return (rc);
141
142         rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
143                                  &txq_allocated);
144         if (rc != 0) {
145                 efx_nic_fini(sc->enp);
146                 return (rc);
147         }
148
149         KASSERT(txq_allocated >= SFXGE_TXQ_NTYPES,
150                 ("txq_allocated < SFXGE_TXQ_NTYPES"));
151
152         sc->evq_max = MIN(evq_allocated, evq_max);
153         sc->evq_max = MIN(rxq_allocated, sc->evq_max);
154         sc->evq_max = MIN(txq_allocated - (SFXGE_TXQ_NTYPES - 1),
155                           sc->evq_max);
156
157         KASSERT(sc->evq_max <= evq_max,
158                 ("allocated more than maximum requested"));
159
160         /*
161          * NIC is kept initialized in the case of success to be able to
162          * initialize port to find out media types.
163          */
164         return (0);
165 }
166
167 static int
168 sfxge_set_drv_limits(struct sfxge_softc *sc)
169 {
170         efx_drv_limits_t limits;
171
172         memset(&limits, 0, sizeof(limits));
173
174         /* Limits are strict since take into account initial estimation */
175         limits.edl_min_evq_count = limits.edl_max_evq_count =
176             sc->intr.n_alloc;
177         limits.edl_min_txq_count = limits.edl_max_txq_count =
178             sc->intr.n_alloc + SFXGE_TXQ_NTYPES - 1;
179         limits.edl_min_rxq_count = limits.edl_max_rxq_count =
180             sc->intr.n_alloc;
181
182         return (efx_nic_set_drv_limits(sc->enp, &limits));
183 }
184
185 static int
186 sfxge_start(struct sfxge_softc *sc)
187 {
188         int rc;
189
190         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
191
192         if (sc->init_state == SFXGE_STARTED)
193                 return (0);
194
195         if (sc->init_state != SFXGE_REGISTERED) {
196                 rc = EINVAL;
197                 goto fail;
198         }
199
200         /* Set required resource limits */
201         if ((rc = sfxge_set_drv_limits(sc)) != 0)
202                 goto fail;
203
204         if ((rc = efx_nic_init(sc->enp)) != 0)
205                 goto fail;
206
207         /* Start processing interrupts. */
208         if ((rc = sfxge_intr_start(sc)) != 0)
209                 goto fail2;
210
211         /* Start processing events. */
212         if ((rc = sfxge_ev_start(sc)) != 0)
213                 goto fail3;
214
215         /* Fire up the port. */
216         if ((rc = sfxge_port_start(sc)) != 0)
217                 goto fail4;
218
219         /* Start the receiver side. */
220         if ((rc = sfxge_rx_start(sc)) != 0)
221                 goto fail5;
222
223         /* Start the transmitter side. */
224         if ((rc = sfxge_tx_start(sc)) != 0)
225                 goto fail6;
226
227         sc->init_state = SFXGE_STARTED;
228
229         /* Tell the stack we're running. */
230         sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
231         sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
232
233         return (0);
234
235 fail6:
236         sfxge_rx_stop(sc);
237
238 fail5:
239         sfxge_port_stop(sc);
240
241 fail4:
242         sfxge_ev_stop(sc);
243
244 fail3:
245         sfxge_intr_stop(sc);
246
247 fail2:
248         efx_nic_fini(sc->enp);
249
250 fail:
251         device_printf(sc->dev, "sfxge_start: %d\n", rc);
252
253         return (rc);
254 }
255
256 static void
257 sfxge_if_init(void *arg)
258 {
259         struct sfxge_softc *sc;
260
261         sc = (struct sfxge_softc *)arg;
262
263         SFXGE_ADAPTER_LOCK(sc);
264         (void)sfxge_start(sc);
265         SFXGE_ADAPTER_UNLOCK(sc);
266 }
267
268 static void
269 sfxge_stop(struct sfxge_softc *sc)
270 {
271         SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
272
273         if (sc->init_state != SFXGE_STARTED)
274                 return;
275
276         sc->init_state = SFXGE_REGISTERED;
277
278         /* Stop the transmitter. */
279         sfxge_tx_stop(sc);
280
281         /* Stop the receiver. */
282         sfxge_rx_stop(sc);
283
284         /* Stop the port. */
285         sfxge_port_stop(sc);
286
287         /* Stop processing events. */
288         sfxge_ev_stop(sc);
289
290         /* Stop processing interrupts. */
291         sfxge_intr_stop(sc);
292
293         efx_nic_fini(sc->enp);
294
295         sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
296 }
297
298
299 static int
300 sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
301 {
302         efx_vpd_value_t value;
303         int rc = 0;
304
305         switch (ioc->u.vpd.op) {
306         case SFXGE_VPD_OP_GET_KEYWORD:
307                 value.evv_tag = ioc->u.vpd.tag;
308                 value.evv_keyword = ioc->u.vpd.keyword;
309                 rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
310                 if (rc != 0)
311                         break;
312                 ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
313                 if (ioc->u.vpd.payload != 0) {
314                         rc = copyout(value.evv_value, ioc->u.vpd.payload,
315                                      ioc->u.vpd.len);
316                 }
317                 break;
318         case SFXGE_VPD_OP_SET_KEYWORD:
319                 if (ioc->u.vpd.len > sizeof(value.evv_value))
320                         return (EINVAL);
321                 value.evv_tag = ioc->u.vpd.tag;
322                 value.evv_keyword = ioc->u.vpd.keyword;
323                 value.evv_length = ioc->u.vpd.len;
324                 rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
325                 if (rc != 0)
326                         break;
327                 rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
328                 if (rc != 0)
329                         break;
330                 rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
331                 if (rc != 0)
332                         break;
333                 rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
334                 break;
335         default:
336                 rc = EOPNOTSUPP;
337                 break;
338         }
339
340         return (rc);
341 }
342
343 static int
344 sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
345 {
346         switch (ioc->op) {
347         case SFXGE_MCDI_IOC:
348                 return (sfxge_mcdi_ioctl(sc, ioc));
349         case SFXGE_NVRAM_IOC:
350                 return (sfxge_nvram_ioctl(sc, ioc));
351         case SFXGE_VPD_IOC:
352                 return (sfxge_vpd_ioctl(sc, ioc));
353         default:
354                 return (EOPNOTSUPP);
355         }
356 }
357
358
359 static int
360 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
361 {
362         struct sfxge_softc *sc;
363         struct ifreq *ifr;
364         sfxge_ioc_t ioc;
365         int error;
366
367         ifr = (struct ifreq *)data;
368         sc = ifp->if_softc;
369         error = 0;
370
371         switch (command) {
372         case SIOCSIFFLAGS:
373                 SFXGE_ADAPTER_LOCK(sc);
374                 if (ifp->if_flags & IFF_UP) {
375                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
376                                 if ((ifp->if_flags ^ sc->if_flags) &
377                                     (IFF_PROMISC | IFF_ALLMULTI)) {
378                                         sfxge_mac_filter_set(sc);
379                                 }
380                         } else
381                                 sfxge_start(sc);
382                 } else
383                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
384                                 sfxge_stop(sc);
385                 sc->if_flags = ifp->if_flags;
386                 SFXGE_ADAPTER_UNLOCK(sc);
387                 break;
388         case SIOCSIFMTU:
389                 if (ifr->ifr_mtu == ifp->if_mtu) {
390                         /* Nothing to do */
391                         error = 0;
392                 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
393                         error = EINVAL;
394                 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
395                         ifp->if_mtu = ifr->ifr_mtu;
396                         error = 0;
397                 } else {
398                         /* Restart required */
399                         SFXGE_ADAPTER_LOCK(sc);
400                         sfxge_stop(sc);
401                         ifp->if_mtu = ifr->ifr_mtu;
402                         error = sfxge_start(sc);
403                         SFXGE_ADAPTER_UNLOCK(sc);
404                         if (error != 0) {
405                                 ifp->if_flags &= ~IFF_UP;
406                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
407                                 if_down(ifp);
408                         }
409                 }
410                 break;
411         case SIOCADDMULTI:
412         case SIOCDELMULTI:
413                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
414                         sfxge_mac_filter_set(sc);
415                 break;
416         case SIOCSIFCAP:
417         {
418                 int reqcap = ifr->ifr_reqcap;
419                 int capchg_mask;
420
421                 SFXGE_ADAPTER_LOCK(sc);
422
423                 /* Capabilities to be changed in accordance with request */
424                 capchg_mask = ifp->if_capenable ^ reqcap;
425
426                 /*
427                  * The networking core already rejects attempts to
428                  * enable capabilities we don't have.  We still have
429                  * to reject attempts to disable capabilities that we
430                  * can't (yet) disable.
431                  */
432                 KASSERT((reqcap & ~ifp->if_capabilities) == 0,
433                     ("Unsupported capabilities 0x%x requested 0x%x vs "
434                      "supported 0x%x",
435                      reqcap & ~ifp->if_capabilities,
436                      reqcap , ifp->if_capabilities));
437                 if (capchg_mask & SFXGE_CAP_FIXED) {
438                         error = EINVAL;
439                         SFXGE_ADAPTER_UNLOCK(sc);
440                         break;
441                 }
442
443                 /* Check request before any changes */
444                 if ((capchg_mask & IFCAP_TSO4) &&
445                     (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
446                         error = EAGAIN;
447                         SFXGE_ADAPTER_UNLOCK(sc);
448                         if_printf(ifp, "enable txcsum before tso4\n");
449                         break;
450                 }
451                 if ((capchg_mask & IFCAP_TSO6) &&
452                     (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
453                         error = EAGAIN;
454                         SFXGE_ADAPTER_UNLOCK(sc);
455                         if_printf(ifp, "enable txcsum6 before tso6\n");
456                         break;
457                 }
458
459                 if (reqcap & IFCAP_TXCSUM) {
460                         ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
461                 } else {
462                         ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
463                         if (reqcap & IFCAP_TSO4) {
464                                 reqcap &= ~IFCAP_TSO4;
465                                 if_printf(ifp,
466                                     "tso4 disabled due to -txcsum\n");
467                         }
468                 }
469                 if (reqcap & IFCAP_TXCSUM_IPV6) {
470                         ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
471                 } else {
472                         ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
473                         if (reqcap & IFCAP_TSO6) {
474                                 reqcap &= ~IFCAP_TSO6;
475                                 if_printf(ifp,
476                                     "tso6 disabled due to -txcsum6\n");
477                         }
478                 }
479
480                 /*
481                  * The kernel takes both IFCAP_TSOx and CSUM_TSO into
482                  * account before using TSO. So, we do not touch
483                  * checksum flags when IFCAP_TSOx is modified.
484                  * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
485                  * but both bits are set in IPv4 and IPv6 mbufs.
486                  */
487
488                 ifp->if_capenable = reqcap;
489
490                 SFXGE_ADAPTER_UNLOCK(sc);
491                 break;
492         }
493         case SIOCSIFMEDIA:
494         case SIOCGIFMEDIA:
495                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
496                 break;
497         case SIOCGPRIVATE_0:
498                 error = priv_check(curthread, PRIV_DRIVER);
499                 if (error != 0)
500                         break;
501                 error = copyin(ifr->ifr_data, &ioc, sizeof(ioc));
502                 if (error != 0)
503                         return (error);
504                 error = sfxge_private_ioctl(sc, &ioc);
505                 if (error == 0) {
506                         error = copyout(&ioc, ifr->ifr_data, sizeof(ioc));
507                 }
508                 break;
509         default:
510                 error = ether_ioctl(ifp, command, data);
511         }
512
513         return (error);
514 }
515
516 static void
517 sfxge_tick(void *arg)
518 {
519         struct sfxge_softc *sc = arg;
520
521         sfxge_port_update_stats(sc);
522         sfxge_tx_update_stats(sc);
523
524         callout_reset(&sc->tick_callout, sfxge_stats_update_period,
525                       sfxge_tick, sc);
526 }
527
528 static void
529 sfxge_ifnet_fini(struct ifnet *ifp)
530 {
531         struct sfxge_softc *sc = ifp->if_softc;
532
533         callout_drain(&sc->tick_callout);
534
535         SFXGE_ADAPTER_LOCK(sc);
536         sfxge_stop(sc);
537         SFXGE_ADAPTER_UNLOCK(sc);
538
539         ifmedia_removeall(&sc->media);
540         ether_ifdetach(ifp);
541         if_free(ifp);
542 }
543
544 static int
545 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
546 {
547         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
548         device_t dev;
549         int rc;
550
551         dev = sc->dev;
552         sc->ifnet = ifp;
553
554         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
555         ifp->if_init = sfxge_if_init;
556         ifp->if_softc = sc;
557         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
558         ifp->if_ioctl = sfxge_if_ioctl;
559
560         ifp->if_capabilities = SFXGE_CAP;
561         ifp->if_capenable = SFXGE_CAP_ENABLE;
562
563 #ifdef SFXGE_LRO
564         ifp->if_capabilities |= IFCAP_LRO;
565         ifp->if_capenable |= IFCAP_LRO;
566 #endif
567
568         if (encp->enc_hw_tx_insert_vlan_enabled) {
569                 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
570                 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING;
571         }
572         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
573                            CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
574
575         ether_ifattach(ifp, encp->enc_mac_addr);
576
577         ifp->if_transmit = sfxge_if_transmit;
578         ifp->if_qflush = sfxge_if_qflush;
579
580         callout_init(&sc->tick_callout, B_TRUE);
581
582         DBGPRINT(sc->dev, "ifmedia_init");
583         if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
584                 goto fail;
585
586         callout_reset(&sc->tick_callout, sfxge_stats_update_period,
587                       sfxge_tick, sc);
588
589         return (0);
590
591 fail:
592         ether_ifdetach(sc->ifnet);
593         return (rc);
594 }
595
596 void
597 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
598 {
599         KASSERT(sc->buffer_table_next + n <=
600                 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
601                 ("buffer table full"));
602
603         *idp = sc->buffer_table_next;
604         sc->buffer_table_next += n;
605 }
606
607 static int
608 sfxge_bar_init(struct sfxge_softc *sc)
609 {
610         efsys_bar_t *esbp = &sc->bar;
611
612         esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
613         if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
614             &esbp->esb_rid, RF_ACTIVE)) == NULL) {
615                 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
616                     EFX_MEM_BAR);
617                 return (ENXIO);
618         }
619         esbp->esb_tag = rman_get_bustag(esbp->esb_res);
620         esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
621
622         SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
623
624         return (0);
625 }
626
627 static void
628 sfxge_bar_fini(struct sfxge_softc *sc)
629 {
630         efsys_bar_t *esbp = &sc->bar;
631
632         bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
633             esbp->esb_res);
634         SFXGE_BAR_LOCK_DESTROY(esbp);
635 }
636
637 static int
638 sfxge_create(struct sfxge_softc *sc)
639 {
640         device_t dev;
641         efx_nic_t *enp;
642         int error;
643         char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
644
645         dev = sc->dev;
646
647         SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
648
649         sc->max_rss_channels = 0;
650         snprintf(rss_param_name, sizeof(rss_param_name),
651                  SFXGE_PARAM(%d.max_rss_channels),
652                  (int)device_get_unit(dev));
653         TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
654
655         sc->stats_node = SYSCTL_ADD_NODE(
656                 device_get_sysctl_ctx(dev),
657                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
658                 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
659         if (sc->stats_node == NULL) {
660                 error = ENOMEM;
661                 goto fail;
662         }
663
664         TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
665
666         (void) pci_enable_busmaster(dev);
667
668         /* Initialize DMA mappings. */
669         DBGPRINT(sc->dev, "dma_init...");
670         if ((error = sfxge_dma_init(sc)) != 0)
671                 goto fail;
672
673         /* Map the device registers. */
674         DBGPRINT(sc->dev, "bar_init...");
675         if ((error = sfxge_bar_init(sc)) != 0)
676                 goto fail;
677
678         error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
679             &sc->family);
680         KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
681
682         DBGPRINT(sc->dev, "nic_create...");
683
684         /* Create the common code nic object. */
685         SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
686                               device_get_nameunit(sc->dev), "nic");
687         if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
688             &sc->bar, &sc->enp_lock, &enp)) != 0)
689                 goto fail3;
690         sc->enp = enp;
691
692         if (!ISP2(sfxge_rx_ring_entries) ||
693             (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
694             (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
695                 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
696                     SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
697                     EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
698                 error = EINVAL;
699                 goto fail_rx_ring_entries;
700         }
701         sc->rxq_entries = sfxge_rx_ring_entries;
702
703         if (!ISP2(sfxge_tx_ring_entries) ||
704             (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
705             (sfxge_tx_ring_entries > EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)))) {
706                 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
707                     SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
708                     EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)));
709                 error = EINVAL;
710                 goto fail_tx_ring_entries;
711         }
712         sc->txq_entries = sfxge_tx_ring_entries;
713
714         /* Initialize MCDI to talk to the microcontroller. */
715         DBGPRINT(sc->dev, "mcdi_init...");
716         if ((error = sfxge_mcdi_init(sc)) != 0)
717                 goto fail4;
718
719         /* Probe the NIC and build the configuration data area. */
720         DBGPRINT(sc->dev, "nic_probe...");
721         if ((error = efx_nic_probe(enp)) != 0)
722                 goto fail5;
723
724         SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
725                           SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
726                           OID_AUTO, "version", CTLFLAG_RD,
727                           SFXGE_VERSION_STRING, 0,
728                           "Driver version");
729
730         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
731                         SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
732                         OID_AUTO, "phy_type", CTLFLAG_RD,
733                         NULL, efx_nic_cfg_get(enp)->enc_phy_type,
734                         "PHY type");
735
736         /* Initialize the NVRAM. */
737         DBGPRINT(sc->dev, "nvram_init...");
738         if ((error = efx_nvram_init(enp)) != 0)
739                 goto fail6;
740
741         /* Initialize the VPD. */
742         DBGPRINT(sc->dev, "vpd_init...");
743         if ((error = efx_vpd_init(enp)) != 0)
744                 goto fail7;
745
746         efx_mcdi_new_epoch(enp);
747
748         /* Reset the NIC. */
749         DBGPRINT(sc->dev, "nic_reset...");
750         if ((error = efx_nic_reset(enp)) != 0)
751                 goto fail8;
752
753         /* Initialize buffer table allocation. */
754         sc->buffer_table_next = 0;
755
756         /*
757          * Guarantee minimum and estimate maximum number of event queues
758          * to take it into account when MSI-X interrupts are allocated.
759          * It initializes NIC and keeps it initialized on success.
760          */
761         if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
762                 goto fail8;
763
764         /* Set up interrupts. */
765         DBGPRINT(sc->dev, "intr_init...");
766         if ((error = sfxge_intr_init(sc)) != 0)
767                 goto fail9;
768
769         /* Initialize event processing state. */
770         DBGPRINT(sc->dev, "ev_init...");
771         if ((error = sfxge_ev_init(sc)) != 0)
772                 goto fail11;
773
774         /* Initialize port state. */
775         DBGPRINT(sc->dev, "port_init...");
776         if ((error = sfxge_port_init(sc)) != 0)
777                 goto fail12;
778
779         /* Initialize receive state. */
780         DBGPRINT(sc->dev, "rx_init...");
781         if ((error = sfxge_rx_init(sc)) != 0)
782                 goto fail13;
783
784         /* Initialize transmit state. */
785         DBGPRINT(sc->dev, "tx_init...");
786         if ((error = sfxge_tx_init(sc)) != 0)
787                 goto fail14;
788
789         sc->init_state = SFXGE_INITIALIZED;
790
791         DBGPRINT(sc->dev, "success");
792         return (0);
793
794 fail14:
795         sfxge_rx_fini(sc);
796
797 fail13:
798         sfxge_port_fini(sc);
799
800 fail12:
801         sfxge_ev_fini(sc);
802
803 fail11:
804         sfxge_intr_fini(sc);
805
806 fail9:
807         efx_nic_fini(sc->enp);
808
809 fail8:
810         efx_vpd_fini(enp);
811
812 fail7:
813         efx_nvram_fini(enp);
814
815 fail6:
816         efx_nic_unprobe(enp);
817
818 fail5:
819         sfxge_mcdi_fini(sc);
820
821 fail4:
822 fail_tx_ring_entries:
823 fail_rx_ring_entries:
824         sc->enp = NULL;
825         efx_nic_destroy(enp);
826         SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
827
828 fail3:
829         sfxge_bar_fini(sc);
830         (void) pci_disable_busmaster(sc->dev);
831
832 fail:
833         DBGPRINT(sc->dev, "failed %d", error);
834         sc->dev = NULL;
835         SFXGE_ADAPTER_LOCK_DESTROY(sc);
836         return (error);
837 }
838
839 static void
840 sfxge_destroy(struct sfxge_softc *sc)
841 {
842         efx_nic_t *enp;
843
844         /* Clean up transmit state. */
845         sfxge_tx_fini(sc);
846
847         /* Clean up receive state. */
848         sfxge_rx_fini(sc);
849
850         /* Clean up port state. */
851         sfxge_port_fini(sc);
852
853         /* Clean up event processing state. */
854         sfxge_ev_fini(sc);
855
856         /* Clean up interrupts. */
857         sfxge_intr_fini(sc);
858
859         /* Tear down common code subsystems. */
860         efx_nic_reset(sc->enp);
861         efx_vpd_fini(sc->enp);
862         efx_nvram_fini(sc->enp);
863         efx_nic_unprobe(sc->enp);
864
865         /* Tear down MCDI. */
866         sfxge_mcdi_fini(sc);
867
868         /* Destroy common code context. */
869         enp = sc->enp;
870         sc->enp = NULL;
871         efx_nic_destroy(enp);
872
873         /* Free DMA memory. */
874         sfxge_dma_fini(sc);
875
876         /* Free mapped BARs. */
877         sfxge_bar_fini(sc);
878
879         (void) pci_disable_busmaster(sc->dev);
880
881         taskqueue_drain(taskqueue_thread, &sc->task_reset);
882
883         /* Destroy the softc lock. */
884         SFXGE_ADAPTER_LOCK_DESTROY(sc);
885 }
886
887 static int
888 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
889 {
890         struct sfxge_softc *sc = arg1;
891         efx_vpd_value_t value;
892         int rc;
893
894         value.evv_tag = arg2 >> 16;
895         value.evv_keyword = arg2 & 0xffff;
896         if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
897             != 0)
898                 return (rc);
899
900         return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
901 }
902
903 static void
904 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
905                   efx_vpd_tag_t tag, const char *keyword)
906 {
907         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
908         efx_vpd_value_t value;
909
910         /* Check whether VPD tag/keyword is present */
911         value.evv_tag = tag;
912         value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
913         if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
914                 return;
915
916         SYSCTL_ADD_PROC(
917                 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
918                 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
919                 sfxge_vpd_handler, "A", "");
920 }
921
922 static int
923 sfxge_vpd_init(struct sfxge_softc *sc)
924 {
925         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
926         struct sysctl_oid *vpd_node;
927         struct sysctl_oid_list *vpd_list;
928         char keyword[3];
929         efx_vpd_value_t value;
930         int rc;
931
932         if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
933                 /*
934                  * Unpriviledged functions deny VPD access.
935                  * Simply skip VPD in this case.
936                  */
937                 if (rc == EACCES)
938                         goto done;
939                 goto fail;
940         }
941         sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
942         if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
943                 goto fail2;
944
945         /* Copy ID (product name) into device description, and log it. */
946         value.evv_tag = EFX_VPD_ID;
947         if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
948                 value.evv_value[value.evv_length] = 0;
949                 device_set_desc_copy(sc->dev, value.evv_value);
950                 device_printf(sc->dev, "%s\n", value.evv_value);
951         }
952
953         vpd_node = SYSCTL_ADD_NODE(
954                 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
955                 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
956         vpd_list = SYSCTL_CHILDREN(vpd_node);
957
958         /* Add sysctls for all expected and any vendor-defined keywords. */
959         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
960         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
961         sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
962         keyword[0] = 'V';
963         keyword[2] = 0;
964         for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
965                 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
966         for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
967                 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
968
969 done:
970         return (0);
971
972 fail2:
973         free(sc->vpd_data, M_SFXGE);
974 fail:
975         return (rc);
976 }
977
978 static void
979 sfxge_vpd_fini(struct sfxge_softc *sc)
980 {
981         free(sc->vpd_data, M_SFXGE);
982 }
983
984 static void
985 sfxge_reset(void *arg, int npending)
986 {
987         struct sfxge_softc *sc;
988         int rc;
989         unsigned attempt;
990
991         (void)npending;
992
993         sc = (struct sfxge_softc *)arg;
994
995         SFXGE_ADAPTER_LOCK(sc);
996
997         if (sc->init_state != SFXGE_STARTED)
998                 goto done;
999
1000         sfxge_stop(sc);
1001         efx_nic_reset(sc->enp);
1002         for (attempt = 0; attempt < 3; ++attempt) {
1003                 if ((rc = sfxge_start(sc)) == 0)
1004                         goto done;
1005
1006                 device_printf(sc->dev, "start on reset failed (%d)\n", rc);
1007                 DELAY(100000);
1008         }
1009
1010         device_printf(sc->dev, "reset failed; interface is now stopped\n");
1011
1012 done:
1013         SFXGE_ADAPTER_UNLOCK(sc);
1014 }
1015
1016 void
1017 sfxge_schedule_reset(struct sfxge_softc *sc)
1018 {
1019         taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1020 }
1021
1022 static int
1023 sfxge_attach(device_t dev)
1024 {
1025         struct sfxge_softc *sc;
1026         struct ifnet *ifp;
1027         int error;
1028
1029         sc = device_get_softc(dev);
1030         sc->dev = dev;
1031
1032         /* Allocate ifnet. */
1033         ifp = if_alloc(IFT_ETHER);
1034         if (ifp == NULL) {
1035                 device_printf(dev, "Couldn't allocate ifnet\n");
1036                 error = ENOMEM;
1037                 goto fail;
1038         }
1039         sc->ifnet = ifp;
1040
1041         /* Initialize hardware. */
1042         DBGPRINT(sc->dev, "create nic");
1043         if ((error = sfxge_create(sc)) != 0)
1044                 goto fail2;
1045
1046         /* Create the ifnet for the port. */
1047         DBGPRINT(sc->dev, "init ifnet");
1048         if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1049                 goto fail3;
1050
1051         DBGPRINT(sc->dev, "init vpd");
1052         if ((error = sfxge_vpd_init(sc)) != 0)
1053                 goto fail4;
1054
1055         /*
1056          * NIC is initialized inside sfxge_create() and kept inialized
1057          * to be able to initialize port to discover media types in
1058          * sfxge_ifnet_init().
1059          */
1060         efx_nic_fini(sc->enp);
1061
1062         sc->init_state = SFXGE_REGISTERED;
1063
1064         DBGPRINT(sc->dev, "success");
1065         return (0);
1066
1067 fail4:
1068         sfxge_ifnet_fini(ifp);
1069 fail3:
1070         efx_nic_fini(sc->enp);
1071         sfxge_destroy(sc);
1072
1073 fail2:
1074         if_free(sc->ifnet);
1075
1076 fail:
1077         DBGPRINT(sc->dev, "failed %d", error);
1078         return (error);
1079 }
1080
1081 static int
1082 sfxge_detach(device_t dev)
1083 {
1084         struct sfxge_softc *sc;
1085
1086         sc = device_get_softc(dev);
1087
1088         sfxge_vpd_fini(sc);
1089
1090         /* Destroy the ifnet. */
1091         sfxge_ifnet_fini(sc->ifnet);
1092
1093         /* Tear down hardware. */
1094         sfxge_destroy(sc);
1095
1096         return (0);
1097 }
1098
1099 static int
1100 sfxge_probe(device_t dev)
1101 {
1102         uint16_t pci_vendor_id;
1103         uint16_t pci_device_id;
1104         efx_family_t family;
1105         int rc;
1106
1107         pci_vendor_id = pci_get_vendor(dev);
1108         pci_device_id = pci_get_device(dev);
1109
1110         DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
1111         rc = efx_family(pci_vendor_id, pci_device_id, &family);
1112         if (rc != 0) {
1113                 DBGPRINT(dev, "efx_family fail %d", rc);
1114                 return (ENXIO);
1115         }
1116
1117         if (family == EFX_FAMILY_SIENA) {
1118                 device_set_desc(dev, "Solarflare SFC9000 family");
1119                 return (0);
1120         }
1121
1122         if (family == EFX_FAMILY_HUNTINGTON) {
1123                 device_set_desc(dev, "Solarflare SFC9100 family");
1124                 return (0);
1125         }
1126
1127         DBGPRINT(dev, "impossible controller family %d", family);
1128         return (ENXIO);
1129 }
1130
1131 static device_method_t sfxge_methods[] = {
1132         DEVMETHOD(device_probe,         sfxge_probe),
1133         DEVMETHOD(device_attach,        sfxge_attach),
1134         DEVMETHOD(device_detach,        sfxge_detach),
1135
1136         DEVMETHOD_END
1137 };
1138
1139 static devclass_t sfxge_devclass;
1140
1141 static driver_t sfxge_driver = {
1142         "sfxge",
1143         sfxge_methods,
1144         sizeof(struct sfxge_softc)
1145 };
1146
1147 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);