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