2 * Copyright (c) 2011-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
36 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/sockio.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
45 #include <net/ethernet.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
50 #include <net/if_arp.h>
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #include <dev/ofw/openfirm.h>
58 #include "miibus_if.h"
60 #include <contrib/ncsw/inc/Peripherals/fm_mac_ext.h>
61 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
62 #include <contrib/ncsw/inc/xx_ext.h>
66 #include "if_dtsec_im.h"
67 #include "if_dtsec_rm.h"
71 * @group dTSEC private defines.
75 * dTSEC FMan MAC exceptions info struct.
77 struct dtsec_fm_mac_ex_str {
85 * @group FMan MAC routines.
88 #define DTSEC_MAC_EXCEPTIONS_END (-1)
91 * FMan MAC exceptions.
93 static const struct dtsec_fm_mac_ex_str dtsec_fm_mac_exceptions[] = {
94 { e_FM_MAC_EX_10G_MDIO_SCAN_EVENTMDIO, "MDIO scan event" },
95 { e_FM_MAC_EX_10G_MDIO_CMD_CMPL, "MDIO command completion" },
96 { e_FM_MAC_EX_10G_REM_FAULT, "Remote fault" },
97 { e_FM_MAC_EX_10G_LOC_FAULT, "Local fault" },
98 { e_FM_MAC_EX_10G_1TX_ECC_ER, "Transmit frame ECC error" },
99 { e_FM_MAC_EX_10G_TX_FIFO_UNFL, "Transmit FIFO underflow" },
100 { e_FM_MAC_EX_10G_TX_FIFO_OVFL, "Receive FIFO overflow" },
101 { e_FM_MAC_EX_10G_TX_ER, "Transmit frame error" },
102 { e_FM_MAC_EX_10G_RX_FIFO_OVFL, "Receive FIFO overflow" },
103 { e_FM_MAC_EX_10G_RX_ECC_ER, "Receive frame ECC error" },
104 { e_FM_MAC_EX_10G_RX_JAB_FRM, "Receive jabber frame" },
105 { e_FM_MAC_EX_10G_RX_OVRSZ_FRM, "Receive oversized frame" },
106 { e_FM_MAC_EX_10G_RX_RUNT_FRM, "Receive runt frame" },
107 { e_FM_MAC_EX_10G_RX_FRAG_FRM, "Receive fragment frame" },
108 { e_FM_MAC_EX_10G_RX_LEN_ER, "Receive payload length error" },
109 { e_FM_MAC_EX_10G_RX_CRC_ER, "Receive CRC error" },
110 { e_FM_MAC_EX_10G_RX_ALIGN_ER, "Receive alignment error" },
111 { e_FM_MAC_EX_1G_BAB_RX, "Babbling receive error" },
112 { e_FM_MAC_EX_1G_RX_CTL, "Receive control (pause frame) interrupt" },
113 { e_FM_MAC_EX_1G_GRATEFUL_TX_STP_COMPLET, "Graceful transmit stop "
115 { e_FM_MAC_EX_1G_BAB_TX, "Babbling transmit error" },
116 { e_FM_MAC_EX_1G_TX_CTL, "Transmit control (pause frame) interrupt" },
117 { e_FM_MAC_EX_1G_TX_ERR, "Transmit error" },
118 { e_FM_MAC_EX_1G_LATE_COL, "Late collision" },
119 { e_FM_MAC_EX_1G_COL_RET_LMT, "Collision retry limit" },
120 { e_FM_MAC_EX_1G_TX_FIFO_UNDRN, "Transmit FIFO underrun" },
121 { e_FM_MAC_EX_1G_MAG_PCKT, "Magic Packet detected when dTSEC is in "
122 "Magic Packet detection mode" },
123 { e_FM_MAC_EX_1G_MII_MNG_RD_COMPLET, "MII management read completion" },
124 { e_FM_MAC_EX_1G_MII_MNG_WR_COMPLET, "MII management write completion" },
125 { e_FM_MAC_EX_1G_GRATEFUL_RX_STP_COMPLET, "Graceful receive stop "
127 { e_FM_MAC_EX_1G_TX_DATA_ERR, "Internal data error on transmit" },
128 { e_FM_MAC_EX_1G_RX_DATA_ERR, "Internal data error on receive" },
129 { e_FM_MAC_EX_1G_1588_TS_RX_ERR, "Time-Stamp Receive Error" },
130 { e_FM_MAC_EX_1G_RX_MIB_CNT_OVFL, "MIB counter overflow" },
131 { DTSEC_MAC_EXCEPTIONS_END, "" }
135 dtsec_fm_mac_ex_to_str(e_FmMacExceptions exception)
139 for (i = 0; dtsec_fm_mac_exceptions[i].num != exception &&
140 dtsec_fm_mac_exceptions[i].num != DTSEC_MAC_EXCEPTIONS_END; ++i)
143 if (dtsec_fm_mac_exceptions[i].num == DTSEC_MAC_EXCEPTIONS_END)
144 return ("<Unknown Exception>");
146 return (dtsec_fm_mac_exceptions[i].str);
150 dtsec_fm_mac_mdio_event_callback(t_Handle h_App,
151 e_FmMacExceptions exception)
153 struct dtsec_softc *sc;
156 device_printf(sc->sc_dev, "MDIO event %i: %s.\n", exception,
157 dtsec_fm_mac_ex_to_str(exception));
161 dtsec_fm_mac_exception_callback(t_Handle app, e_FmMacExceptions exception)
163 struct dtsec_softc *sc;
166 device_printf(sc->sc_dev, "MAC exception %i: %s.\n", exception,
167 dtsec_fm_mac_ex_to_str(exception));
171 dtsec_fm_mac_free(struct dtsec_softc *sc)
173 if (sc->sc_mach == NULL)
176 FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
177 FM_MAC_Free(sc->sc_mach);
182 dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac)
184 t_FmMacParams params;
187 memset(¶ms, 0, sizeof(params));
188 memcpy(¶ms.addr, mac, sizeof(params.addr));
190 params.baseAddr = sc->sc_fm_base + sc->sc_mac_mem_offset;
191 params.enetMode = sc->sc_mac_enet_mode;
192 params.macId = sc->sc_eth_id;
193 params.mdioIrq = sc->sc_mac_mdio_irq;
194 params.f_Event = dtsec_fm_mac_mdio_event_callback;
195 params.f_Exception = dtsec_fm_mac_exception_callback;
197 params.h_Fm = sc->sc_fmh;
199 sc->sc_mach = FM_MAC_Config(¶ms);
200 if (sc->sc_mach == NULL) {
201 device_printf(sc->sc_dev, "couldn't configure FM_MAC module.\n"
206 error = FM_MAC_ConfigResetOnInit(sc->sc_mach, TRUE);
208 device_printf(sc->sc_dev, "couldn't enable reset on init "
210 dtsec_fm_mac_free(sc);
214 /* Do not inform about pause frames */
215 error = FM_MAC_ConfigException(sc->sc_mach, e_FM_MAC_EX_1G_RX_CTL,
218 device_printf(sc->sc_dev, "couldn't disable pause frames "
220 dtsec_fm_mac_free(sc);
224 error = FM_MAC_Init(sc->sc_mach);
226 device_printf(sc->sc_dev, "couldn't initialize FM_MAC module."
228 dtsec_fm_mac_free(sc);
238 * @group FMan PORT routines.
242 dtsec_fm_port_ex_to_str(e_FmPortExceptions exception)
246 case e_FM_PORT_EXCEPTION_IM_BUSY:
247 return ("IM: RX busy");
249 return ("<Unknown Exception>");
254 dtsec_fm_port_rx_exception_callback(t_Handle app,
255 e_FmPortExceptions exception)
257 struct dtsec_softc *sc;
260 device_printf(sc->sc_dev, "RX exception: %i: %s.\n", exception,
261 dtsec_fm_port_ex_to_str(exception));
265 dtsec_fm_port_tx_exception_callback(t_Handle app,
266 e_FmPortExceptions exception)
268 struct dtsec_softc *sc;
271 device_printf(sc->sc_dev, "TX exception: %i: %s.\n", exception,
272 dtsec_fm_port_ex_to_str(exception));
276 dtsec_fm_port_rx_type(enum eth_dev_type type)
280 return (e_FM_PORT_TYPE_RX);
282 return (e_FM_PORT_TYPE_RX_10G);
284 return (e_FM_PORT_TYPE_DUMMY);
289 dtsec_fm_port_tx_type(enum eth_dev_type type)
294 return (e_FM_PORT_TYPE_TX);
296 return (e_FM_PORT_TYPE_TX_10G);
298 return (e_FM_PORT_TYPE_DUMMY);
303 dtsec_fm_port_free_both(struct dtsec_softc *sc)
306 FM_PORT_Free(sc->sc_rxph);
311 FM_PORT_Free(sc->sc_txph);
319 * @group IFnet routines.
323 dtsec_if_enable_locked(struct dtsec_softc *sc)
327 DTSEC_LOCK_ASSERT(sc);
329 error = FM_MAC_Enable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
333 error = FM_PORT_Enable(sc->sc_rxph);
337 error = FM_PORT_Enable(sc->sc_txph);
341 sc->sc_ifnet->if_drv_flags |= IFF_DRV_RUNNING;
343 /* Refresh link state */
344 dtsec_miibus_statchg(sc->sc_dev);
350 dtsec_if_disable_locked(struct dtsec_softc *sc)
354 DTSEC_LOCK_ASSERT(sc);
356 error = FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
360 error = FM_PORT_Disable(sc->sc_rxph);
364 error = FM_PORT_Disable(sc->sc_txph);
368 sc->sc_ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
374 dtsec_if_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
376 struct dtsec_softc *sc;
381 ifr = (struct ifreq *)data;
384 /* Basic functionality to achieve media status reports */
389 if (sc->sc_ifnet->if_flags & IFF_UP)
390 error = dtsec_if_enable_locked(sc);
392 error = dtsec_if_disable_locked(sc);
399 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media,
404 error = ether_ioctl(ifp, command, data);
411 dtsec_if_tick(void *arg)
413 struct dtsec_softc *sc;
420 mii_tick(sc->sc_mii);
421 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
427 dtsec_if_deinit_locked(struct dtsec_softc *sc)
430 DTSEC_LOCK_ASSERT(sc);
433 callout_drain(&sc->sc_tick_callout);
438 dtsec_if_init_locked(struct dtsec_softc *sc)
442 DTSEC_LOCK_ASSERT(sc);
444 /* Set MAC address */
445 error = FM_MAC_ModifyMacAddr(sc->sc_mach,
446 (t_EnetAddr *)IF_LLADDR(sc->sc_ifnet));
448 device_printf(sc->sc_dev, "couldn't set MAC address.\n");
452 /* Start MII polling */
454 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
456 if (sc->sc_ifnet->if_flags & IFF_UP) {
457 error = dtsec_if_enable_locked(sc);
461 error = dtsec_if_disable_locked(sc);
469 dtsec_if_deinit_locked(sc);
470 device_printf(sc->sc_dev, "initialization error.\n");
475 dtsec_if_init(void *data)
477 struct dtsec_softc *sc;
482 dtsec_if_init_locked(sc);
487 dtsec_if_start(struct ifnet *ifp)
489 struct dtsec_softc *sc;
493 sc->sc_start_locked(sc);
498 dtsec_if_watchdog(struct ifnet *ifp)
506 * @group IFmedia routines.
510 dtsec_ifmedia_upd(struct ifnet *ifp)
512 struct dtsec_softc *sc = ifp->if_softc;
515 mii_mediachg(sc->sc_mii);
522 dtsec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
524 struct dtsec_softc *sc = ifp->if_softc;
528 mii_pollstat(sc->sc_mii);
530 ifmr->ifm_active = sc->sc_mii->mii_media_active;
531 ifmr->ifm_status = sc->sc_mii->mii_media_status;
539 * @group dTSEC bus interface.
543 dtsec_configure_mode(struct dtsec_softc *sc)
547 snprintf(tunable, sizeof(tunable), "%s.independent_mode",
548 device_get_nameunit(sc->sc_dev));
550 sc->sc_mode = DTSEC_MODE_REGULAR;
551 TUNABLE_INT_FETCH(tunable, &sc->sc_mode);
553 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
554 sc->sc_port_rx_init = dtsec_rm_fm_port_rx_init;
555 sc->sc_port_tx_init = dtsec_rm_fm_port_tx_init;
556 sc->sc_start_locked = dtsec_rm_if_start_locked;
558 sc->sc_port_rx_init = dtsec_im_fm_port_rx_init;
559 sc->sc_port_tx_init = dtsec_im_fm_port_tx_init;
560 sc->sc_start_locked = dtsec_im_if_start_locked;
563 device_printf(sc->sc_dev, "Configured for %s mode.\n",
564 (sc->sc_mode == DTSEC_MODE_REGULAR) ? "regular" : "independent");
568 dtsec_attach(device_t dev)
570 struct dtsec_softc *sc;
574 sc = device_get_softc(dev);
577 sc->sc_mac_mdio_irq = NO_IRQ;
578 sc->sc_eth_id = device_get_unit(dev);
581 /* Check if MallocSmart allocator is ready */
582 if (XX_MallocSmartInit() != E_OK)
586 mtx_init(&sc->sc_lock, device_get_nameunit(dev),
587 "DTSEC Global Lock", MTX_DEF);
589 mtx_init(&sc->sc_mii_lock, device_get_nameunit(dev),
590 "DTSEC MII Lock", MTX_DEF);
593 callout_init(&sc->sc_tick_callout, CALLOUT_MPSAFE);
595 /* Read configuraton */
596 if ((error = fman_get_handle(&sc->sc_fmh)) != 0)
599 if ((error = fman_get_muram_handle(&sc->sc_muramh)) != 0)
602 if ((error = fman_get_bushandle(&sc->sc_fm_base)) != 0)
605 /* Configure working mode */
606 dtsec_configure_mode(sc);
608 /* If we are working in regular mode configure BMAN and QMAN */
609 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
610 /* Create RX buffer pool */
611 error = dtsec_rm_pool_rx_init(sc);
615 /* Create RX frame queue range */
616 error = dtsec_rm_fqr_rx_init(sc);
620 /* Create frame info pool */
621 error = dtsec_rm_fi_pool_init(sc);
625 /* Create TX frame queue range */
626 error = dtsec_rm_fqr_tx_init(sc);
631 /* Init FMan MAC module. */
632 error = dtsec_fm_mac_init(sc, sc->sc_mac_addr);
638 /* Init FMan TX port */
639 error = sc->sc_port_tx_init(sc, device_get_unit(sc->sc_dev));
645 /* Init FMan RX port */
646 error = sc->sc_port_rx_init(sc, device_get_unit(sc->sc_dev));
652 /* Create network interface for upper layers */
653 ifp = sc->sc_ifnet = if_alloc(IFT_ETHER);
655 device_printf(sc->sc_dev, "if_alloc() failed.\n");
661 ifp->if_mtu = ETHERMTU; /* TODO: Configure */
662 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST;
663 ifp->if_init = dtsec_if_init;
664 ifp->if_start = dtsec_if_start;
665 ifp->if_ioctl = dtsec_if_ioctl;
666 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
668 if (sc->sc_phy_addr >= 0)
669 if_initname(ifp, device_get_name(sc->sc_dev),
670 device_get_unit(sc->sc_dev));
672 if_initname(ifp, "dtsec_phy", device_get_unit(sc->sc_dev));
676 IFQ_SET_MAXLEN(&ifp->if_snd, TSEC_TX_NUM_DESC - 1);
677 ifp->if_snd.ifq_drv_maxlen = TSEC_TX_NUM_DESC - 1;
678 IFQ_SET_READY(&ifp->if_snd);
680 ifp->if_capabilities = 0; /* TODO: Check */
681 ifp->if_capenable = ifp->if_capabilities;
684 error = mii_attach(sc->sc_dev, &sc->sc_mii_dev, ifp, dtsec_ifmedia_upd,
685 dtsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->sc_phy_addr,
688 device_printf(sc->sc_dev, "attaching PHYs failed: %d\n", error);
689 dtsec_detach(sc->sc_dev);
692 sc->sc_mii = device_get_softc(sc->sc_mii_dev);
694 /* Attach to stack */
695 ether_ifattach(ifp, sc->sc_mac_addr);
701 dtsec_detach(device_t dev)
703 struct dtsec_softc *sc;
706 sc = device_get_softc(dev);
709 if (device_is_attached(dev)) {
711 /* Shutdown interface */
713 dtsec_if_deinit_locked(sc);
718 if_free(sc->sc_ifnet);
722 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
723 /* Free RX/TX FQRs */
724 dtsec_rm_fqr_rx_free(sc);
725 dtsec_rm_fqr_tx_free(sc);
727 /* Free frame info pool */
728 dtsec_rm_fi_pool_free(sc);
730 /* Free RX buffer pool */
731 dtsec_rm_pool_rx_free(sc);
734 dtsec_fm_mac_free(sc);
735 dtsec_fm_port_free_both(sc);
738 mtx_destroy(&sc->sc_lock);
744 dtsec_suspend(device_t dev)
751 dtsec_resume(device_t dev)
758 dtsec_shutdown(device_t dev)
767 * @group MII bus interface.
771 dtsec_miibus_readreg(device_t dev, int phy, int reg)
773 struct dtsec_softc *sc;
775 sc = device_get_softc(dev);
777 return (MIIBUS_READREG(sc->sc_mdio, phy, reg));
781 dtsec_miibus_writereg(device_t dev, int phy, int reg, int value)
784 struct dtsec_softc *sc;
786 sc = device_get_softc(dev);
788 return (MIIBUS_WRITEREG(sc->sc_mdio, phy, reg, value));
792 dtsec_miibus_statchg(device_t dev)
794 struct dtsec_softc *sc;
799 sc = device_get_softc(dev);
801 DTSEC_LOCK_ASSERT(sc);
803 duplex = ((sc->sc_mii->mii_media_active & IFM_GMASK) == IFM_FDX);
805 switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
808 speed = e_ENET_SPEED_1000;
812 speed = e_ENET_SPEED_100;
816 speed = e_ENET_SPEED_10;
820 speed = e_ENET_SPEED_10;
823 error = FM_MAC_AdjustLink(sc->sc_mach, speed, duplex);
825 device_printf(sc->sc_dev, "error while adjusting MAC speed.\n");