]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/sfxge_port.c
sfxge(4): add tunable to configure MAC stats update period
[FreeBSD/FreeBSD.git] / sys / dev / sfxge / sfxge_port.c
1 /*-
2  * Copyright (c) 2010-2016 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/types.h>
38 #include <sys/limits.h>
39 #include <net/ethernet.h>
40 #include <net/if_dl.h>
41
42 #include "common/efx.h"
43
44 #include "sfxge.h"
45
46 #define SFXGE_PARAM_STATS_UPDATE_PERIOD_MS \
47         SFXGE_PARAM(stats_update_period_ms)
48 static int sfxge_stats_update_period_ms = SFXGE_STATS_UPDATE_PERIOD_MS;
49 TUNABLE_INT(SFXGE_PARAM_STATS_UPDATE_PERIOD_MS,
50             &sfxge_stats_update_period_ms);
51 SYSCTL_INT(_hw_sfxge, OID_AUTO, stats_update_period_ms, CTLFLAG_RDTUN,
52            &sfxge_stats_update_period_ms, 0,
53            "netstat interface statistics update period in milliseconds");
54
55 static int sfxge_phy_cap_mask(struct sfxge_softc *, int, uint32_t *);
56
57 static int
58 sfxge_mac_stat_update(struct sfxge_softc *sc)
59 {
60         struct sfxge_port *port = &sc->port;
61         efsys_mem_t *esmp = &(port->mac_stats.dma_buf);
62         clock_t now;
63         unsigned int min_ticks;
64         unsigned int count;
65         int rc;
66
67         SFXGE_PORT_LOCK_ASSERT_OWNED(port);
68
69         if (__predict_false(port->init_state != SFXGE_PORT_STARTED)) {
70                 rc = 0;
71                 goto out;
72         }
73
74         min_ticks = (unsigned int)hz * port->stats_update_period_ms / 1000;
75
76         now = ticks;
77         if ((unsigned int)(now - port->mac_stats.update_time) < min_ticks) {
78                 rc = 0;
79                 goto out;
80         }
81
82         port->mac_stats.update_time = now;
83
84         /* If we're unlucky enough to read statistics wduring the DMA, wait
85          * up to 10ms for it to finish (typically takes <500us) */
86         for (count = 0; count < 100; ++count) {
87                 EFSYS_PROBE1(wait, unsigned int, count);
88
89                 /* Try to update the cached counters */
90                 if ((rc = efx_mac_stats_update(sc->enp, esmp,
91                     port->mac_stats.decode_buf, NULL)) != EAGAIN)
92                         goto out;
93
94                 DELAY(100);
95         }
96
97         rc = ETIMEDOUT;
98 out:
99         return (rc);
100 }
101
102 uint64_t
103 sfxge_get_counter(struct ifnet *ifp, ift_counter c)
104 {
105         struct sfxge_softc *sc = ifp->if_softc;
106         uint64_t *mac_stats;
107         uint64_t val;
108
109         SFXGE_PORT_LOCK(&sc->port);
110
111         /* Ignore error and use old values */
112         (void)sfxge_mac_stat_update(sc);
113
114         mac_stats = (uint64_t *)sc->port.mac_stats.decode_buf;
115
116         switch (c) {
117         case IFCOUNTER_IPACKETS:
118                 val = mac_stats[EFX_MAC_RX_PKTS];
119                 break;
120         case IFCOUNTER_IERRORS:
121                 val = mac_stats[EFX_MAC_RX_ERRORS];
122                 break;
123         case IFCOUNTER_OPACKETS:
124                 val = mac_stats[EFX_MAC_TX_PKTS];
125                 break;
126         case IFCOUNTER_OERRORS:
127                 val = mac_stats[EFX_MAC_TX_ERRORS];
128                 break;
129         case IFCOUNTER_COLLISIONS:
130                 val = mac_stats[EFX_MAC_TX_SGL_COL_PKTS] +
131                       mac_stats[EFX_MAC_TX_MULT_COL_PKTS] +
132                       mac_stats[EFX_MAC_TX_EX_COL_PKTS] +
133                       mac_stats[EFX_MAC_TX_LATE_COL_PKTS];
134                 break;
135         case IFCOUNTER_IBYTES:
136                 val = mac_stats[EFX_MAC_RX_OCTETS];
137                 break;
138         case IFCOUNTER_OBYTES:
139                 val = mac_stats[EFX_MAC_TX_OCTETS];
140                 break;
141         case IFCOUNTER_OMCASTS:
142                 val = mac_stats[EFX_MAC_TX_MULTICST_PKTS] +
143                       mac_stats[EFX_MAC_TX_BRDCST_PKTS];
144                 break;
145         case IFCOUNTER_OQDROPS:
146                 SFXGE_PORT_UNLOCK(&sc->port);
147                 return (sfxge_tx_get_drops(sc));
148         case IFCOUNTER_IMCASTS:
149                 /* if_imcasts is maintained in net/if_ethersubr.c */
150         case IFCOUNTER_IQDROPS:
151                 /* if_iqdrops is maintained in net/if_ethersubr.c */
152         case IFCOUNTER_NOPROTO:
153                 /* if_noproto is maintained in net/if_ethersubr.c */
154         default:
155                 SFXGE_PORT_UNLOCK(&sc->port);
156                 return (if_get_counter_default(ifp, c));
157         }
158
159         SFXGE_PORT_UNLOCK(&sc->port);
160
161         return (val);
162 }
163
164 static int
165 sfxge_mac_stat_handler(SYSCTL_HANDLER_ARGS)
166 {
167         struct sfxge_softc *sc = arg1;
168         unsigned int id = arg2;
169         int rc;
170         uint64_t val;
171
172         SFXGE_PORT_LOCK(&sc->port);
173         if ((rc = sfxge_mac_stat_update(sc)) == 0)
174                 val = ((uint64_t *)sc->port.mac_stats.decode_buf)[id];
175         SFXGE_PORT_UNLOCK(&sc->port);
176
177         if (rc == 0)
178                 rc = SYSCTL_OUT(req, &val, sizeof(val));
179         return (rc);
180 }
181
182 static void
183 sfxge_mac_stat_init(struct sfxge_softc *sc)
184 {
185         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
186         struct sysctl_oid_list *stat_list;
187         unsigned int id;
188         const char *name;
189
190         stat_list = SYSCTL_CHILDREN(sc->stats_node);
191
192         /* Initialise the named stats */
193         for (id = 0; id < EFX_MAC_NSTATS; id++) {
194                 name = efx_mac_stat_name(sc->enp, id);
195                 SYSCTL_ADD_PROC(
196                         ctx, stat_list,
197                         OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
198                         sc, id, sfxge_mac_stat_handler, "Q",
199                         "");
200         }
201 }
202
203 #ifdef SFXGE_HAVE_PAUSE_MEDIAOPTS
204
205 static unsigned int
206 sfxge_port_wanted_fc(struct sfxge_softc *sc)
207 {
208         struct ifmedia_entry *ifm = sc->media.ifm_cur;
209
210         if (ifm->ifm_media == (IFM_ETHER | IFM_AUTO))
211                 return (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE);
212         return (((ifm->ifm_media & IFM_ETH_RXPAUSE) ? EFX_FCNTL_RESPOND : 0) |
213                 ((ifm->ifm_media & IFM_ETH_TXPAUSE) ? EFX_FCNTL_GENERATE : 0));
214 }
215
216 static unsigned int
217 sfxge_port_link_fc_ifm(struct sfxge_softc *sc)
218 {
219         unsigned int wanted_fc, link_fc;
220
221         efx_mac_fcntl_get(sc->enp, &wanted_fc, &link_fc);
222         return ((link_fc & EFX_FCNTL_RESPOND) ? IFM_ETH_RXPAUSE : 0) |
223                 ((link_fc & EFX_FCNTL_GENERATE) ? IFM_ETH_TXPAUSE : 0);
224 }
225
226 #else /* !SFXGE_HAVE_PAUSE_MEDIAOPTS */
227
228 static unsigned int
229 sfxge_port_wanted_fc(struct sfxge_softc *sc)
230 {
231         return (sc->port.wanted_fc);
232 }
233
234 static unsigned int
235 sfxge_port_link_fc_ifm(struct sfxge_softc *sc)
236 {
237         return (0);
238 }
239
240 static int
241 sfxge_port_wanted_fc_handler(SYSCTL_HANDLER_ARGS)
242 {
243         struct sfxge_softc *sc;
244         struct sfxge_port *port;
245         unsigned int fcntl;
246         int error;
247
248         sc = arg1;
249         port = &sc->port;
250
251         if (req->newptr != NULL) {
252                 if ((error = SYSCTL_IN(req, &fcntl, sizeof(fcntl))) != 0)
253                         return (error);
254
255                 SFXGE_PORT_LOCK(port);
256
257                 if (port->wanted_fc != fcntl) {
258                         if (port->init_state == SFXGE_PORT_STARTED)
259                                 error = efx_mac_fcntl_set(sc->enp,
260                                                           port->wanted_fc,
261                                                           B_TRUE);
262                         if (error == 0)
263                                 port->wanted_fc = fcntl;
264                 }
265
266                 SFXGE_PORT_UNLOCK(port);
267         } else {
268                 SFXGE_PORT_LOCK(port);
269                 fcntl = port->wanted_fc;
270                 SFXGE_PORT_UNLOCK(port);
271
272                 error = SYSCTL_OUT(req, &fcntl, sizeof(fcntl));
273         }
274
275         return (error);
276 }
277
278 static int
279 sfxge_port_link_fc_handler(SYSCTL_HANDLER_ARGS)
280 {
281         struct sfxge_softc *sc;
282         struct sfxge_port *port;
283         unsigned int wanted_fc, link_fc;
284
285         sc = arg1;
286         port = &sc->port;
287
288         SFXGE_PORT_LOCK(port);
289         if (__predict_true(port->init_state == SFXGE_PORT_STARTED) &&
290             SFXGE_LINK_UP(sc))
291                 efx_mac_fcntl_get(sc->enp, &wanted_fc, &link_fc);
292         else
293                 link_fc = 0;
294         SFXGE_PORT_UNLOCK(port);
295
296         return (SYSCTL_OUT(req, &link_fc, sizeof(link_fc)));
297 }
298
299 #endif /* SFXGE_HAVE_PAUSE_MEDIAOPTS */
300
301 static const uint64_t sfxge_link_baudrate[EFX_LINK_NMODES] = {
302         [EFX_LINK_10HDX]        = IF_Mbps(10),
303         [EFX_LINK_10FDX]        = IF_Mbps(10),
304         [EFX_LINK_100HDX]       = IF_Mbps(100),
305         [EFX_LINK_100FDX]       = IF_Mbps(100),
306         [EFX_LINK_1000HDX]      = IF_Gbps(1),
307         [EFX_LINK_1000FDX]      = IF_Gbps(1),
308         [EFX_LINK_10000FDX]     = IF_Gbps(10),
309         [EFX_LINK_40000FDX]     = IF_Gbps(40),
310 };
311
312 void
313 sfxge_mac_link_update(struct sfxge_softc *sc, efx_link_mode_t mode)
314 {
315         struct sfxge_port *port;
316         int link_state;
317
318         port = &sc->port;
319
320         if (port->link_mode == mode)
321                 return;
322
323         port->link_mode = mode;
324
325         /* Push link state update to the OS */
326         link_state = (SFXGE_LINK_UP(sc) ? LINK_STATE_UP : LINK_STATE_DOWN);
327         sc->ifnet->if_baudrate = sfxge_link_baudrate[port->link_mode];
328         if_link_state_change(sc->ifnet, link_state);
329 }
330
331 static void
332 sfxge_mac_poll_work(void *arg, int npending)
333 {
334         struct sfxge_softc *sc;
335         efx_nic_t *enp;
336         struct sfxge_port *port;
337         efx_link_mode_t mode;
338
339         sc = (struct sfxge_softc *)arg;
340         enp = sc->enp;
341         port = &sc->port;
342
343         SFXGE_PORT_LOCK(port);
344
345         if (__predict_false(port->init_state != SFXGE_PORT_STARTED))
346                 goto done;
347
348         /* This may sleep waiting for MCDI completion */
349         (void)efx_port_poll(enp, &mode);
350         sfxge_mac_link_update(sc, mode);
351
352 done:
353         SFXGE_PORT_UNLOCK(port);
354 }
355
356 static int
357 sfxge_mac_multicast_list_set(struct sfxge_softc *sc)
358 {
359         struct ifnet *ifp = sc->ifnet;
360         struct sfxge_port *port = &sc->port;
361         uint8_t *mcast_addr = port->mcast_addrs;
362         struct ifmultiaddr *ifma;
363         struct sockaddr_dl *sa;
364         int rc = 0;
365
366         mtx_assert(&port->lock, MA_OWNED);
367
368         port->mcast_count = 0;
369         if_maddr_rlock(ifp);
370         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
371                 if (ifma->ifma_addr->sa_family == AF_LINK) {
372                         if (port->mcast_count == EFX_MAC_MULTICAST_LIST_MAX) {
373                                 device_printf(sc->dev,
374                                     "Too many multicast addresses\n");
375                                 rc = EINVAL;
376                                 break;
377                         }
378
379                         sa = (struct sockaddr_dl *)ifma->ifma_addr;
380                         memcpy(mcast_addr, LLADDR(sa), EFX_MAC_ADDR_LEN);
381                         mcast_addr += EFX_MAC_ADDR_LEN;
382                         ++port->mcast_count;
383                 }
384         }
385         if_maddr_runlock(ifp);
386
387         if (rc == 0) {
388                 rc = efx_mac_multicast_list_set(sc->enp, port->mcast_addrs,
389                                                 port->mcast_count);
390                 if (rc != 0)
391                         device_printf(sc->dev,
392                             "Cannot set multicast address list\n");
393         }
394
395         return (rc);
396 }
397
398 static int
399 sfxge_mac_filter_set_locked(struct sfxge_softc *sc)
400 {
401         struct ifnet *ifp = sc->ifnet;
402         struct sfxge_port *port = &sc->port;
403         boolean_t all_mulcst;
404         int rc;
405
406         mtx_assert(&port->lock, MA_OWNED);
407
408         all_mulcst = !!(ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI));
409
410         rc = sfxge_mac_multicast_list_set(sc);
411         /* Fallback to all multicast if cannot set multicast list */
412         if (rc != 0)
413                 all_mulcst = B_TRUE;
414
415         rc = efx_mac_filter_set(sc->enp, !!(ifp->if_flags & IFF_PROMISC),
416                                 (port->mcast_count > 0), all_mulcst, B_TRUE);
417
418         return (rc);
419 }
420
421 int
422 sfxge_mac_filter_set(struct sfxge_softc *sc)
423 {
424         struct sfxge_port *port = &sc->port;
425         int rc;
426
427         SFXGE_PORT_LOCK(port);
428         /*
429          * The function may be called without softc_lock held in the
430          * case of SIOCADDMULTI and SIOCDELMULTI ioctls. ioctl handler
431          * checks IFF_DRV_RUNNING flag which implies port started, but
432          * it is not guaranteed to remain. softc_lock shared lock can't
433          * be held in the case of these ioctls processing, since it
434          * results in failure where kernel complains that non-sleepable
435          * lock is held in sleeping thread. Both problems are repeatable
436          * on LAG with LACP proto bring up.
437          */
438         if (__predict_true(port->init_state == SFXGE_PORT_STARTED))
439                 rc = sfxge_mac_filter_set_locked(sc);
440         else
441                 rc = 0;
442         SFXGE_PORT_UNLOCK(port);
443         return (rc);
444 }
445
446 void
447 sfxge_port_stop(struct sfxge_softc *sc)
448 {
449         struct sfxge_port *port;
450         efx_nic_t *enp;
451
452         port = &sc->port;
453         enp = sc->enp;
454
455         SFXGE_PORT_LOCK(port);
456
457         KASSERT(port->init_state == SFXGE_PORT_STARTED,
458             ("port not started"));
459
460         port->init_state = SFXGE_PORT_INITIALIZED;
461
462         port->mac_stats.update_time = 0;
463
464         /* This may call MCDI */
465         (void)efx_mac_drain(enp, B_TRUE);
466
467         (void)efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf, 0, B_FALSE);
468
469         port->link_mode = EFX_LINK_UNKNOWN;
470
471         /* Destroy the common code port object. */
472         efx_port_fini(enp);
473
474         efx_filter_fini(enp);
475
476         SFXGE_PORT_UNLOCK(port);
477 }
478
479 int
480 sfxge_port_start(struct sfxge_softc *sc)
481 {
482         uint8_t mac_addr[ETHER_ADDR_LEN];
483         struct ifnet *ifp = sc->ifnet;
484         struct sfxge_port *port;
485         efx_nic_t *enp;
486         size_t pdu;
487         int rc;
488         uint32_t phy_cap_mask;
489
490         port = &sc->port;
491         enp = sc->enp;
492
493         SFXGE_PORT_LOCK(port);
494
495         KASSERT(port->init_state == SFXGE_PORT_INITIALIZED,
496             ("port not initialized"));
497
498         /* Initialise the required filtering */
499         if ((rc = efx_filter_init(enp)) != 0)
500                 goto fail_filter_init;
501
502         /* Initialize the port object in the common code. */
503         if ((rc = efx_port_init(sc->enp)) != 0)
504                 goto fail;
505
506         /* Set the SDU */
507         pdu = EFX_MAC_PDU(ifp->if_mtu);
508         if ((rc = efx_mac_pdu_set(enp, pdu)) != 0)
509                 goto fail2;
510
511         if ((rc = efx_mac_fcntl_set(enp, sfxge_port_wanted_fc(sc), B_TRUE))
512             != 0)
513                 goto fail3;
514
515         /* Set the unicast address */
516         if_addr_rlock(ifp);
517         bcopy(LLADDR((struct sockaddr_dl *)ifp->if_addr->ifa_addr),
518               mac_addr, sizeof(mac_addr));
519         if_addr_runlock(ifp);
520         if ((rc = efx_mac_addr_set(enp, mac_addr)) != 0)
521                 goto fail4;
522
523         sfxge_mac_filter_set_locked(sc);
524
525         /* Update MAC stats by DMA every period */
526         if ((rc = efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf,
527                                          port->stats_update_period_ms,
528                                          B_FALSE)) != 0)
529                 goto fail6;
530
531         if ((rc = efx_mac_drain(enp, B_FALSE)) != 0)
532                 goto fail8;
533
534         if ((rc = sfxge_phy_cap_mask(sc, sc->media.ifm_cur->ifm_media,
535                                      &phy_cap_mask)) != 0)
536                 goto fail9;
537
538         if ((rc = efx_phy_adv_cap_set(sc->enp, phy_cap_mask)) != 0)
539                 goto fail10;
540
541         port->init_state = SFXGE_PORT_STARTED;
542
543         /* Single poll in case there were missing initial events */
544         SFXGE_PORT_UNLOCK(port);
545         sfxge_mac_poll_work(sc, 0);
546
547         return (0);
548
549 fail10:
550 fail9:
551         (void)efx_mac_drain(enp, B_TRUE);
552 fail8:
553         (void)efx_mac_stats_periodic(enp, &port->mac_stats.dma_buf, 0, B_FALSE);
554 fail6:
555 fail4:
556 fail3:
557
558 fail2:
559         efx_port_fini(enp);
560 fail:
561         efx_filter_fini(enp);
562 fail_filter_init:
563         SFXGE_PORT_UNLOCK(port);
564
565         return (rc);
566 }
567
568 static int
569 sfxge_phy_stat_update(struct sfxge_softc *sc)
570 {
571         struct sfxge_port *port = &sc->port;
572         efsys_mem_t *esmp = &port->phy_stats.dma_buf;
573         clock_t now;
574         unsigned int count;
575         int rc;
576
577         SFXGE_PORT_LOCK_ASSERT_OWNED(port);
578
579         if (__predict_false(port->init_state != SFXGE_PORT_STARTED)) {
580                 rc = 0;
581                 goto out;
582         }
583
584         now = ticks;
585         if ((unsigned int)(now - port->phy_stats.update_time) < (unsigned int)hz) {
586                 rc = 0;
587                 goto out;
588         }
589
590         port->phy_stats.update_time = now;
591
592         /* If we're unlucky enough to read statistics wduring the DMA, wait
593          * up to 10ms for it to finish (typically takes <500us) */
594         for (count = 0; count < 100; ++count) {
595                 EFSYS_PROBE1(wait, unsigned int, count);
596
597                 /* Synchronize the DMA memory for reading */
598                 bus_dmamap_sync(esmp->esm_tag, esmp->esm_map,
599                     BUS_DMASYNC_POSTREAD);
600
601                 /* Try to update the cached counters */
602                 if ((rc = efx_phy_stats_update(sc->enp, esmp,
603                     port->phy_stats.decode_buf)) != EAGAIN)
604                         goto out;
605
606                 DELAY(100);
607         }
608
609         rc = ETIMEDOUT;
610 out:
611         return (rc);
612 }
613
614 static int
615 sfxge_phy_stat_handler(SYSCTL_HANDLER_ARGS)
616 {
617         struct sfxge_softc *sc = arg1;
618         unsigned int id = arg2;
619         int rc;
620         uint32_t val;
621
622         SFXGE_PORT_LOCK(&sc->port);
623         if ((rc = sfxge_phy_stat_update(sc)) == 0)
624                 val = ((uint32_t *)sc->port.phy_stats.decode_buf)[id];
625         SFXGE_PORT_UNLOCK(&sc->port);
626
627         if (rc == 0)
628                 rc = SYSCTL_OUT(req, &val, sizeof(val));
629         return (rc);
630 }
631
632 static void
633 sfxge_phy_stat_init(struct sfxge_softc *sc)
634 {
635         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
636         struct sysctl_oid_list *stat_list;
637         unsigned int id;
638         const char *name;
639         uint64_t stat_mask = efx_nic_cfg_get(sc->enp)->enc_phy_stat_mask;
640
641         stat_list = SYSCTL_CHILDREN(sc->stats_node);
642
643         /* Initialise the named stats */
644         for (id = 0; id < EFX_PHY_NSTATS; id++) {
645                 if (!(stat_mask & ((uint64_t)1 << id)))
646                         continue;
647                 name = efx_phy_stat_name(sc->enp, id);
648                 SYSCTL_ADD_PROC(
649                         ctx, stat_list,
650                         OID_AUTO, name, CTLTYPE_UINT|CTLFLAG_RD,
651                         sc, id, sfxge_phy_stat_handler,
652                         id == EFX_PHY_STAT_OUI ? "IX" : "IU",
653                         "");
654         }
655 }
656
657 void
658 sfxge_port_fini(struct sfxge_softc *sc)
659 {
660         struct sfxge_port *port;
661         efsys_mem_t *esmp;
662
663         port = &sc->port;
664         esmp = &port->mac_stats.dma_buf;
665
666         KASSERT(port->init_state == SFXGE_PORT_INITIALIZED,
667             ("Port not initialized"));
668
669         port->init_state = SFXGE_PORT_UNINITIALIZED;
670
671         port->link_mode = EFX_LINK_UNKNOWN;
672
673         /* Finish with PHY DMA memory */
674         sfxge_dma_free(&port->phy_stats.dma_buf);
675         free(port->phy_stats.decode_buf, M_SFXGE);
676
677         sfxge_dma_free(esmp);
678         free(port->mac_stats.decode_buf, M_SFXGE);
679
680         SFXGE_PORT_LOCK_DESTROY(port);
681
682         port->sc = NULL;
683 }
684
685 static uint16_t
686 sfxge_port_stats_update_period_ms(struct sfxge_softc *sc)
687 {
688         int period_ms = sfxge_stats_update_period_ms;
689
690         if (period_ms < 0) {
691                 device_printf(sc->dev,
692                         "treat negative stats update period %d as 0 (disable)\n",
693                          period_ms);
694                 period_ms = 0;
695         } else if (period_ms > UINT16_MAX) {
696                 device_printf(sc->dev,
697                         "treat too big stats update period %d as %u\n",
698                         period_ms, UINT16_MAX);
699                 period_ms = UINT16_MAX;
700         }
701
702         return period_ms;
703 }
704
705 int
706 sfxge_port_init(struct sfxge_softc *sc)
707 {
708         struct sfxge_port *port;
709         struct sysctl_ctx_list *sysctl_ctx;
710         struct sysctl_oid *sysctl_tree;
711         efsys_mem_t *mac_stats_buf, *phy_stats_buf;
712         int rc;
713
714         port = &sc->port;
715         mac_stats_buf = &port->mac_stats.dma_buf;
716         phy_stats_buf = &port->phy_stats.dma_buf;
717
718         KASSERT(port->init_state == SFXGE_PORT_UNINITIALIZED,
719             ("Port already initialized"));
720
721         port->sc = sc;
722
723         SFXGE_PORT_LOCK_INIT(port, device_get_nameunit(sc->dev));
724
725         DBGPRINT(sc->dev, "alloc PHY stats");
726         port->phy_stats.decode_buf = malloc(EFX_PHY_NSTATS * sizeof(uint32_t),
727                                             M_SFXGE, M_WAITOK | M_ZERO);
728         if ((rc = sfxge_dma_alloc(sc, EFX_PHY_STATS_SIZE, phy_stats_buf)) != 0)
729                 goto fail;
730         sfxge_phy_stat_init(sc);
731
732         DBGPRINT(sc->dev, "init sysctl");
733         sysctl_ctx = device_get_sysctl_ctx(sc->dev);
734         sysctl_tree = device_get_sysctl_tree(sc->dev);
735
736 #ifndef SFXGE_HAVE_PAUSE_MEDIAOPTS
737         /* If flow control cannot be configured or reported through
738          * ifmedia, provide sysctls for it. */
739         port->wanted_fc = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
740         SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
741             "wanted_fc", CTLTYPE_UINT|CTLFLAG_RW, sc, 0,
742             sfxge_port_wanted_fc_handler, "IU", "wanted flow control mode");
743         SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), OID_AUTO,
744             "link_fc", CTLTYPE_UINT|CTLFLAG_RD, sc, 0,
745             sfxge_port_link_fc_handler, "IU", "link flow control mode");
746 #endif
747
748         DBGPRINT(sc->dev, "alloc MAC stats");
749         port->mac_stats.decode_buf = malloc(EFX_MAC_NSTATS * sizeof(uint64_t),
750                                             M_SFXGE, M_WAITOK | M_ZERO);
751         if ((rc = sfxge_dma_alloc(sc, EFX_MAC_STATS_SIZE, mac_stats_buf)) != 0)
752                 goto fail2;
753         port->stats_update_period_ms = sfxge_port_stats_update_period_ms(sc);
754         sfxge_mac_stat_init(sc);
755
756         port->init_state = SFXGE_PORT_INITIALIZED;
757
758         DBGPRINT(sc->dev, "success");
759         return (0);
760
761 fail2:
762         free(port->mac_stats.decode_buf, M_SFXGE);
763         sfxge_dma_free(phy_stats_buf);
764 fail:
765         free(port->phy_stats.decode_buf, M_SFXGE);
766         SFXGE_PORT_LOCK_DESTROY(port);
767         port->sc = NULL;
768         DBGPRINT(sc->dev, "failed %d", rc);
769         return (rc);
770 }
771
772 static const int sfxge_link_mode[EFX_PHY_MEDIA_NTYPES][EFX_LINK_NMODES] = {
773         [EFX_PHY_MEDIA_CX4] = {
774                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_CX4,
775         },
776         [EFX_PHY_MEDIA_KX4] = {
777                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_KX4,
778         },
779         [EFX_PHY_MEDIA_XFP] = {
780                 /* Don't know the module type, but assume SR for now. */
781                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_SR,
782         },
783         [EFX_PHY_MEDIA_QSFP_PLUS] = {
784                 /* Don't know the module type, but assume SR for now. */
785                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_SR,
786                 [EFX_LINK_40000FDX]     = IFM_ETHER | IFM_FDX | IFM_40G_CR4,
787         },
788         [EFX_PHY_MEDIA_SFP_PLUS] = {
789                 /* Don't know the module type, but assume SX/SR for now. */
790                 [EFX_LINK_1000FDX]      = IFM_ETHER | IFM_FDX | IFM_1000_SX,
791                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_SR,
792         },
793         [EFX_PHY_MEDIA_BASE_T] = {
794                 [EFX_LINK_10HDX]        = IFM_ETHER | IFM_HDX | IFM_10_T,
795                 [EFX_LINK_10FDX]        = IFM_ETHER | IFM_FDX | IFM_10_T,
796                 [EFX_LINK_100HDX]       = IFM_ETHER | IFM_HDX | IFM_100_TX,
797                 [EFX_LINK_100FDX]       = IFM_ETHER | IFM_FDX | IFM_100_TX,
798                 [EFX_LINK_1000HDX]      = IFM_ETHER | IFM_HDX | IFM_1000_T,
799                 [EFX_LINK_1000FDX]      = IFM_ETHER | IFM_FDX | IFM_1000_T,
800                 [EFX_LINK_10000FDX]     = IFM_ETHER | IFM_FDX | IFM_10G_T,
801         },
802 };
803
804 static void
805 sfxge_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
806 {
807         struct sfxge_softc *sc;
808         efx_phy_media_type_t medium_type;
809         efx_link_mode_t mode;
810
811         sc = ifp->if_softc;
812         SFXGE_ADAPTER_LOCK(sc);
813
814         ifmr->ifm_status = IFM_AVALID;
815         ifmr->ifm_active = IFM_ETHER;
816
817         if (SFXGE_RUNNING(sc) && SFXGE_LINK_UP(sc)) {
818                 ifmr->ifm_status |= IFM_ACTIVE;
819
820                 efx_phy_media_type_get(sc->enp, &medium_type);
821                 mode = sc->port.link_mode;
822                 ifmr->ifm_active |= sfxge_link_mode[medium_type][mode];
823                 ifmr->ifm_active |= sfxge_port_link_fc_ifm(sc);
824         }
825
826         SFXGE_ADAPTER_UNLOCK(sc);
827 }
828
829 static efx_phy_cap_type_t
830 sfxge_link_mode_to_phy_cap(efx_link_mode_t mode)
831 {
832         switch (mode) {
833         case EFX_LINK_10HDX:
834                 return (EFX_PHY_CAP_10HDX);
835         case EFX_LINK_10FDX:
836                 return (EFX_PHY_CAP_10FDX);
837         case EFX_LINK_100HDX:
838                 return (EFX_PHY_CAP_100HDX);
839         case EFX_LINK_100FDX:
840                 return (EFX_PHY_CAP_100FDX);
841         case EFX_LINK_1000HDX:
842                 return (EFX_PHY_CAP_1000HDX);
843         case EFX_LINK_1000FDX:
844                 return (EFX_PHY_CAP_1000FDX);
845         case EFX_LINK_10000FDX:
846                 return (EFX_PHY_CAP_10000FDX);
847         case EFX_LINK_40000FDX:
848                 return (EFX_PHY_CAP_40000FDX);
849         default:
850                 EFSYS_ASSERT(B_FALSE);
851                 return (EFX_PHY_CAP_INVALID);
852         }
853 }
854
855 static int
856 sfxge_phy_cap_mask(struct sfxge_softc *sc, int ifmedia, uint32_t *phy_cap_mask)
857 {
858         /* Get global options (duplex), type and subtype bits */
859         int ifmedia_masked = ifmedia & (IFM_GMASK | IFM_NMASK | IFM_TMASK);
860         efx_phy_media_type_t medium_type;
861         boolean_t mode_found = B_FALSE;
862         uint32_t cap_mask, mode_cap_mask;
863         efx_link_mode_t mode;
864         efx_phy_cap_type_t phy_cap;
865
866         efx_phy_media_type_get(sc->enp, &medium_type);
867         if (medium_type >= nitems(sfxge_link_mode)) {
868                 if_printf(sc->ifnet, "unexpected media type %d\n", medium_type);
869                 return (EINVAL);
870         }
871
872         efx_phy_adv_cap_get(sc->enp, EFX_PHY_CAP_PERM, &cap_mask);
873
874         for (mode = EFX_LINK_10HDX; mode < EFX_LINK_NMODES; mode++) {
875                 if (ifmedia_masked == sfxge_link_mode[medium_type][mode]) {
876                         mode_found = B_TRUE;
877                         break;
878                 }
879         }
880
881         if (!mode_found) {
882                 /*
883                  * If media is not in the table, it must be IFM_AUTO.
884                  */
885                 KASSERT((cap_mask & (1 << EFX_PHY_CAP_AN)) &&
886                     ifmedia_masked == (IFM_ETHER | IFM_AUTO),
887                     ("%s: no mode for media %#x", __func__, ifmedia));
888                 *phy_cap_mask = (cap_mask & ~(1 << EFX_PHY_CAP_ASYM));
889                 return (0);
890         }
891
892         phy_cap = sfxge_link_mode_to_phy_cap(mode);
893         if (phy_cap == EFX_PHY_CAP_INVALID) {
894                 if_printf(sc->ifnet,
895                           "cannot map link mode %d to phy capability\n",
896                           mode);
897                 return (EINVAL);
898         }
899
900         mode_cap_mask = (1 << phy_cap);
901         mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_AN);
902 #ifdef SFXGE_HAVE_PAUSE_MEDIAOPTS
903         if (ifmedia & IFM_ETH_RXPAUSE)
904                 mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_PAUSE);
905         if (!(ifmedia & IFM_ETH_TXPAUSE))
906                 mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_ASYM);
907 #else
908         mode_cap_mask |= cap_mask & (1 << EFX_PHY_CAP_PAUSE);
909 #endif
910
911         *phy_cap_mask = mode_cap_mask;
912         return (0);
913 }
914
915 static int
916 sfxge_media_change(struct ifnet *ifp)
917 {
918         struct sfxge_softc *sc;
919         struct ifmedia_entry *ifm;
920         int rc;
921         uint32_t phy_cap_mask;
922
923         sc = ifp->if_softc;
924         ifm = sc->media.ifm_cur;
925
926         SFXGE_ADAPTER_LOCK(sc);
927
928         if (!SFXGE_RUNNING(sc)) {
929                 rc = 0;
930                 goto out;
931         }
932
933         rc = efx_mac_fcntl_set(sc->enp, sfxge_port_wanted_fc(sc), B_TRUE);
934         if (rc != 0)
935                 goto out;
936
937         if ((rc = sfxge_phy_cap_mask(sc, ifm->ifm_media, &phy_cap_mask)) != 0)
938                 goto out;
939
940         rc = efx_phy_adv_cap_set(sc->enp, phy_cap_mask);
941 out:
942         SFXGE_ADAPTER_UNLOCK(sc);
943
944         return (rc);
945 }
946
947 int sfxge_port_ifmedia_init(struct sfxge_softc *sc)
948 {
949         efx_phy_media_type_t medium_type;
950         uint32_t cap_mask, mode_cap_mask;
951         efx_link_mode_t mode;
952         efx_phy_cap_type_t phy_cap;
953         int mode_ifm, best_mode_ifm = 0;
954         int rc;
955
956         /*
957          * We need port state to initialise the ifmedia list.
958          * It requires initialized NIC what is already done in
959          * sfxge_create() when resources are estimated.
960          */
961         if ((rc = efx_filter_init(sc->enp)) != 0)
962                 goto out1;
963         if ((rc = efx_port_init(sc->enp)) != 0)
964                 goto out2;
965
966         /*
967          * Register ifconfig callbacks for querying and setting the
968          * link mode and link status.
969          */
970         ifmedia_init(&sc->media, IFM_IMASK, sfxge_media_change,
971             sfxge_media_status);
972
973         /*
974          * Map firmware medium type and capabilities to ifmedia types.
975          * ifmedia does not distinguish between forcing the link mode
976          * and disabling auto-negotiation.  1000BASE-T and 10GBASE-T
977          * require AN even if only one link mode is enabled, and for
978          * 100BASE-TX it is useful even if the link mode is forced.
979          * Therefore we never disable auto-negotiation.
980          *
981          * Also enable and advertise flow control by default.
982          */
983
984         efx_phy_media_type_get(sc->enp, &medium_type);
985         efx_phy_adv_cap_get(sc->enp, EFX_PHY_CAP_PERM, &cap_mask);
986
987         for (mode = EFX_LINK_10HDX; mode < EFX_LINK_NMODES; mode++) {
988                 phy_cap = sfxge_link_mode_to_phy_cap(mode);
989                 if (phy_cap == EFX_PHY_CAP_INVALID)
990                         continue;
991
992                 mode_cap_mask = (1 << phy_cap);
993                 mode_ifm = sfxge_link_mode[medium_type][mode];
994
995                 if ((cap_mask & mode_cap_mask) && mode_ifm) {
996                         /* No flow-control */
997                         ifmedia_add(&sc->media, mode_ifm, 0, NULL);
998
999 #ifdef SFXGE_HAVE_PAUSE_MEDIAOPTS
1000                         /* Respond-only.  If using AN, we implicitly
1001                          * offer symmetric as well, but that doesn't
1002                          * mean we *have* to generate pause frames.
1003                          */
1004                         mode_ifm |= IFM_ETH_RXPAUSE;
1005                         ifmedia_add(&sc->media, mode_ifm, 0, NULL);
1006
1007                         /* Symmetric */
1008                         mode_ifm |= IFM_ETH_TXPAUSE;
1009                         ifmedia_add(&sc->media, mode_ifm, 0, NULL);
1010 #endif
1011
1012                         /* Link modes are numbered in order of speed,
1013                          * so assume the last one available is the best.
1014                          */
1015                         best_mode_ifm = mode_ifm;
1016                 }
1017         }
1018
1019         if (cap_mask & (1 << EFX_PHY_CAP_AN)) {
1020                 /* Add autoselect mode. */
1021                 mode_ifm = IFM_ETHER | IFM_AUTO;
1022                 ifmedia_add(&sc->media, mode_ifm, 0, NULL);
1023                 best_mode_ifm = mode_ifm;
1024         }
1025
1026         if (best_mode_ifm != 0)
1027                 ifmedia_set(&sc->media, best_mode_ifm);
1028
1029         /* Now discard port state until interface is started. */
1030         efx_port_fini(sc->enp);
1031 out2:
1032         efx_filter_fini(sc->enp);
1033 out1:
1034         return (rc);
1035 }