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/fdt/fdt_common.h>
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
57 #include <dev/ofw/openfirm.h>
59 #include "miibus_if.h"
61 #include <contrib/ncsw/inc/Peripherals/fm_mac_ext.h>
62 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
63 #include <contrib/ncsw/inc/xx_ext.h>
67 #include "if_dtsec_im.h"
68 #include "if_dtsec_rm.h"
72 * @group dTSEC private defines.
76 * dTSEC FMan MAC exceptions info struct.
78 struct dtsec_fm_mac_ex_str {
83 /* XXX: Handle to FM_MAC instance of dTSEC0 */
84 /* From QorIQ Data Path Acceleration Architecture Reference Manual, Rev 2, page
85 * 3-37, "The MII management hardware is shared by all dTSECs... only through
86 * the MIIM registers of dTSEC1 can external PHY's be accessed and configured."
88 static t_Handle dtsec_mdio_mac_handle;
93 * @group FMan MAC routines.
96 #define DTSEC_MAC_EXCEPTIONS_END (-1)
99 * FMan MAC exceptions.
101 static const struct dtsec_fm_mac_ex_str dtsec_fm_mac_exceptions[] = {
102 { e_FM_MAC_EX_10G_MDIO_SCAN_EVENTMDIO, "MDIO scan event" },
103 { e_FM_MAC_EX_10G_MDIO_CMD_CMPL, "MDIO command completion" },
104 { e_FM_MAC_EX_10G_REM_FAULT, "Remote fault" },
105 { e_FM_MAC_EX_10G_LOC_FAULT, "Local fault" },
106 { e_FM_MAC_EX_10G_1TX_ECC_ER, "Transmit frame ECC error" },
107 { e_FM_MAC_EX_10G_TX_FIFO_UNFL, "Transmit FIFO underflow" },
108 { e_FM_MAC_EX_10G_TX_FIFO_OVFL, "Receive FIFO overflow" },
109 { e_FM_MAC_EX_10G_TX_ER, "Transmit frame error" },
110 { e_FM_MAC_EX_10G_RX_FIFO_OVFL, "Receive FIFO overflow" },
111 { e_FM_MAC_EX_10G_RX_ECC_ER, "Receive frame ECC error" },
112 { e_FM_MAC_EX_10G_RX_JAB_FRM, "Receive jabber frame" },
113 { e_FM_MAC_EX_10G_RX_OVRSZ_FRM, "Receive oversized frame" },
114 { e_FM_MAC_EX_10G_RX_RUNT_FRM, "Receive runt frame" },
115 { e_FM_MAC_EX_10G_RX_FRAG_FRM, "Receive fragment frame" },
116 { e_FM_MAC_EX_10G_RX_LEN_ER, "Receive payload length error" },
117 { e_FM_MAC_EX_10G_RX_CRC_ER, "Receive CRC error" },
118 { e_FM_MAC_EX_10G_RX_ALIGN_ER, "Receive alignment error" },
119 { e_FM_MAC_EX_1G_BAB_RX, "Babbling receive error" },
120 { e_FM_MAC_EX_1G_RX_CTL, "Receive control (pause frame) interrupt" },
121 { e_FM_MAC_EX_1G_GRATEFUL_TX_STP_COMPLET, "Graceful transmit stop "
123 { e_FM_MAC_EX_1G_BAB_TX, "Babbling transmit error" },
124 { e_FM_MAC_EX_1G_TX_CTL, "Transmit control (pause frame) interrupt" },
125 { e_FM_MAC_EX_1G_TX_ERR, "Transmit error" },
126 { e_FM_MAC_EX_1G_LATE_COL, "Late collision" },
127 { e_FM_MAC_EX_1G_COL_RET_LMT, "Collision retry limit" },
128 { e_FM_MAC_EX_1G_TX_FIFO_UNDRN, "Transmit FIFO underrun" },
129 { e_FM_MAC_EX_1G_MAG_PCKT, "Magic Packet detected when dTSEC is in "
130 "Magic Packet detection mode" },
131 { e_FM_MAC_EX_1G_MII_MNG_RD_COMPLET, "MII management read completion" },
132 { e_FM_MAC_EX_1G_MII_MNG_WR_COMPLET, "MII management write completion" },
133 { e_FM_MAC_EX_1G_GRATEFUL_RX_STP_COMPLET, "Graceful receive stop "
135 { e_FM_MAC_EX_1G_TX_DATA_ERR, "Internal data error on transmit" },
136 { e_FM_MAC_EX_1G_RX_DATA_ERR, "Internal data error on receive" },
137 { e_FM_MAC_EX_1G_1588_TS_RX_ERR, "Time-Stamp Receive Error" },
138 { e_FM_MAC_EX_1G_RX_MIB_CNT_OVFL, "MIB counter overflow" },
139 { DTSEC_MAC_EXCEPTIONS_END, "" }
143 dtsec_fm_mac_ex_to_str(e_FmMacExceptions exception)
147 for (i = 0; dtsec_fm_mac_exceptions[i].num != exception &&
148 dtsec_fm_mac_exceptions[i].num != DTSEC_MAC_EXCEPTIONS_END; ++i)
151 if (dtsec_fm_mac_exceptions[i].num == DTSEC_MAC_EXCEPTIONS_END)
152 return ("<Unknown Exception>");
154 return (dtsec_fm_mac_exceptions[i].str);
158 dtsec_fm_mac_mdio_event_callback(t_Handle h_App,
159 e_FmMacExceptions exception)
161 struct dtsec_softc *sc;
164 device_printf(sc->sc_dev, "MDIO event %i: %s.\n", exception,
165 dtsec_fm_mac_ex_to_str(exception));
169 dtsec_fm_mac_exception_callback(t_Handle app, e_FmMacExceptions exception)
171 struct dtsec_softc *sc;
174 device_printf(sc->sc_dev, "MAC exception %i: %s.\n", exception,
175 dtsec_fm_mac_ex_to_str(exception));
179 dtsec_fm_mac_free(struct dtsec_softc *sc)
181 if (sc->sc_mach == NULL)
184 FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
185 FM_MAC_Free(sc->sc_mach);
190 dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac)
192 t_FmMacParams params;
195 memset(¶ms, 0, sizeof(params));
196 memcpy(¶ms.addr, mac, sizeof(params.addr));
198 params.baseAddr = sc->sc_fm_base + sc->sc_mac_mem_offset;
199 params.enetMode = sc->sc_mac_enet_mode;
200 params.macId = sc->sc_eth_id;
201 params.mdioIrq = sc->sc_mac_mdio_irq;
202 params.f_Event = dtsec_fm_mac_mdio_event_callback;
203 params.f_Exception = dtsec_fm_mac_exception_callback;
205 params.h_Fm = sc->sc_fmh;
207 sc->sc_mach = FM_MAC_Config(¶ms);
210 if (sc->sc_mach == NULL) {
211 device_printf(sc->sc_dev, "couldn't configure FM_MAC module.\n"
216 error = FM_MAC_ConfigResetOnInit(sc->sc_mach, TRUE);
218 device_printf(sc->sc_dev, "couldn't enable reset on init "
220 dtsec_fm_mac_free(sc);
224 /* Do not inform about pause frames */
225 error = FM_MAC_ConfigException(sc->sc_mach, e_FM_MAC_EX_1G_RX_CTL,
228 device_printf(sc->sc_dev, "couldn't disable pause frames "
230 dtsec_fm_mac_free(sc);
234 error = FM_MAC_Init(sc->sc_mach);
236 device_printf(sc->sc_dev, "couldn't initialize FM_MAC module."
238 dtsec_fm_mac_free(sc);
248 * @group FMan PORT routines.
252 dtsec_fm_port_ex_to_str(e_FmPortExceptions exception)
256 case e_FM_PORT_EXCEPTION_IM_BUSY:
257 return ("IM: RX busy");
259 return ("<Unknown Exception>");
264 dtsec_fm_port_rx_exception_callback(t_Handle app,
265 e_FmPortExceptions exception)
267 struct dtsec_softc *sc;
270 device_printf(sc->sc_dev, "RX exception: %i: %s.\n", exception,
271 dtsec_fm_port_ex_to_str(exception));
275 dtsec_fm_port_tx_exception_callback(t_Handle app,
276 e_FmPortExceptions exception)
278 struct dtsec_softc *sc;
281 device_printf(sc->sc_dev, "TX exception: %i: %s.\n", exception,
282 dtsec_fm_port_ex_to_str(exception));
286 dtsec_fm_port_rx_type(enum eth_dev_type type)
290 return (e_FM_PORT_TYPE_RX);
292 return (e_FM_PORT_TYPE_RX_10G);
294 return (e_FM_PORT_TYPE_DUMMY);
299 dtsec_fm_port_tx_type(enum eth_dev_type type)
304 return (e_FM_PORT_TYPE_TX);
306 return (e_FM_PORT_TYPE_TX_10G);
308 return (e_FM_PORT_TYPE_DUMMY);
313 dtsec_fm_port_free_both(struct dtsec_softc *sc)
316 FM_PORT_Free(sc->sc_rxph);
321 FM_PORT_Free(sc->sc_txph);
329 * @group IFnet routines.
333 dtsec_if_enable_locked(struct dtsec_softc *sc)
337 DTSEC_LOCK_ASSERT(sc);
339 error = FM_MAC_Enable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
343 error = FM_PORT_Enable(sc->sc_rxph);
347 error = FM_PORT_Enable(sc->sc_txph);
351 sc->sc_ifnet->if_drv_flags |= IFF_DRV_RUNNING;
353 /* Refresh link state */
354 dtsec_miibus_statchg(sc->sc_dev);
360 dtsec_if_disable_locked(struct dtsec_softc *sc)
364 DTSEC_LOCK_ASSERT(sc);
366 error = FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
370 error = FM_PORT_Disable(sc->sc_rxph);
374 error = FM_PORT_Disable(sc->sc_txph);
378 sc->sc_ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
384 dtsec_if_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
386 struct dtsec_softc *sc;
391 ifr = (struct ifreq *)data;
394 /* Basic functionality to achieve media status reports */
399 if (sc->sc_ifnet->if_flags & IFF_UP)
400 error = dtsec_if_enable_locked(sc);
402 error = dtsec_if_disable_locked(sc);
409 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media,
414 error = ether_ioctl(ifp, command, data);
421 dtsec_if_tick(void *arg)
423 struct dtsec_softc *sc;
430 mii_tick(sc->sc_mii);
431 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
437 dtsec_if_deinit_locked(struct dtsec_softc *sc)
440 DTSEC_LOCK_ASSERT(sc);
443 callout_drain(&sc->sc_tick_callout);
448 dtsec_if_init_locked(struct dtsec_softc *sc)
452 DTSEC_LOCK_ASSERT(sc);
454 /* Set MAC address */
455 error = FM_MAC_ModifyMacAddr(sc->sc_mach,
456 (t_EnetAddr *)IF_LLADDR(sc->sc_ifnet));
458 device_printf(sc->sc_dev, "couldn't set MAC address.\n");
462 /* Start MII polling */
464 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
466 if (sc->sc_ifnet->if_flags & IFF_UP) {
467 error = dtsec_if_enable_locked(sc);
471 error = dtsec_if_disable_locked(sc);
479 dtsec_if_deinit_locked(sc);
480 device_printf(sc->sc_dev, "initialization error.\n");
485 dtsec_if_init(void *data)
487 struct dtsec_softc *sc;
492 dtsec_if_init_locked(sc);
497 dtsec_if_start(struct ifnet *ifp)
499 struct dtsec_softc *sc;
503 sc->sc_start_locked(sc);
508 dtsec_if_watchdog(struct ifnet *ifp)
516 * @group IFmedia routines.
520 dtsec_ifmedia_upd(struct ifnet *ifp)
522 struct dtsec_softc *sc = ifp->if_softc;
525 mii_mediachg(sc->sc_mii);
532 dtsec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
534 struct dtsec_softc *sc = ifp->if_softc;
538 mii_pollstat(sc->sc_mii);
540 ifmr->ifm_active = sc->sc_mii->mii_media_active;
541 ifmr->ifm_status = sc->sc_mii->mii_media_status;
549 * @group dTSEC bus interface.
553 dtsec_configure_mode(struct dtsec_softc *sc)
557 snprintf(tunable, sizeof(tunable), "%s.independent_mode",
558 device_get_nameunit(sc->sc_dev));
560 sc->sc_mode = DTSEC_MODE_REGULAR;
561 TUNABLE_INT_FETCH(tunable, &sc->sc_mode);
563 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
564 sc->sc_port_rx_init = dtsec_rm_fm_port_rx_init;
565 sc->sc_port_tx_init = dtsec_rm_fm_port_tx_init;
566 sc->sc_start_locked = dtsec_rm_if_start_locked;
568 sc->sc_port_rx_init = dtsec_im_fm_port_rx_init;
569 sc->sc_port_tx_init = dtsec_im_fm_port_tx_init;
570 sc->sc_start_locked = dtsec_im_if_start_locked;
573 device_printf(sc->sc_dev, "Configured for %s mode.\n",
574 (sc->sc_mode == DTSEC_MODE_REGULAR) ? "regular" : "independent");
578 dtsec_attach(device_t dev)
580 struct dtsec_softc *sc;
584 sc = device_get_softc(dev);
587 sc->sc_mac_mdio_irq = NO_IRQ;
588 sc->sc_eth_id = device_get_unit(dev);
591 /* Check if MallocSmart allocator is ready */
592 if (XX_MallocSmartInit() != E_OK)
598 mtx_init(&sc->sc_lock, device_get_nameunit(dev),
599 "DTSEC Global Lock", MTX_DEF);
601 mtx_init(&sc->sc_mii_lock, device_get_nameunit(dev),
602 "DTSEC MII Lock", MTX_DEF);
605 callout_init(&sc->sc_tick_callout, CALLOUT_MPSAFE);
607 /* Read configuraton */
608 if ((error = fman_get_handle(&sc->sc_fmh)) != 0)
611 if ((error = fman_get_muram_handle(&sc->sc_muramh)) != 0)
614 if ((error = fman_get_bushandle(&sc->sc_fm_base)) != 0)
617 /* Configure working mode */
618 dtsec_configure_mode(sc);
620 /* If we are working in regular mode configure BMAN and QMAN */
621 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
622 /* Create RX buffer pool */
623 error = dtsec_rm_pool_rx_init(sc);
627 /* Create RX frame queue range */
628 error = dtsec_rm_fqr_rx_init(sc);
632 /* Create frame info pool */
633 error = dtsec_rm_fi_pool_init(sc);
637 /* Create TX frame queue range */
638 error = dtsec_rm_fqr_tx_init(sc);
643 /* Init FMan MAC module. */
644 error = dtsec_fm_mac_init(sc, sc->sc_mac_addr);
651 * XXX: All phys are connected to MDIO interface of the first dTSEC
652 * device (dTSEC0). We have to save handle to the FM_MAC instance of
653 * dTSEC0, which is used later during phy's registers accesses. Another
654 * option would be adding new property to DTS pointing to correct dTSEC
655 * instance, of which FM_MAC handle has to be used for phy's registers
656 * accesses. We did not want to add new properties to DTS, thus this
659 if (sc->sc_eth_id == 0)
660 dtsec_mdio_mac_handle = sc->sc_mach;
664 /* Init FMan TX port */
665 error = sc->sc_port_tx_init(sc, device_get_unit(sc->sc_dev));
671 /* Init FMan RX port */
672 error = sc->sc_port_rx_init(sc, device_get_unit(sc->sc_dev));
678 /* Create network interface for upper layers */
679 ifp = sc->sc_ifnet = if_alloc(IFT_ETHER);
681 device_printf(sc->sc_dev, "if_alloc() failed.\n");
687 ifp->if_mtu = ETHERMTU; /* TODO: Configure */
688 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST;
689 ifp->if_init = dtsec_if_init;
690 ifp->if_start = dtsec_if_start;
691 ifp->if_ioctl = dtsec_if_ioctl;
692 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
694 if (sc->sc_phy_addr >= 0)
695 if_initname(ifp, device_get_name(sc->sc_dev),
696 device_get_unit(sc->sc_dev));
698 if_initname(ifp, "dtsec_phy", device_get_unit(sc->sc_dev));
702 IFQ_SET_MAXLEN(&ifp->if_snd, TSEC_TX_NUM_DESC - 1);
703 ifp->if_snd.ifq_drv_maxlen = TSEC_TX_NUM_DESC - 1;
704 IFQ_SET_READY(&ifp->if_snd);
706 ifp->if_capabilities = 0; /* TODO: Check */
707 ifp->if_capenable = ifp->if_capabilities;
710 error = mii_attach(sc->sc_dev, &sc->sc_mii_dev, ifp, dtsec_ifmedia_upd,
711 dtsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->sc_phy_addr,
714 device_printf(sc->sc_dev, "attaching PHYs failed: %d\n", error);
715 dtsec_detach(sc->sc_dev);
718 sc->sc_mii = device_get_softc(sc->sc_mii_dev);
720 /* Attach to stack */
721 ether_ifattach(ifp, sc->sc_mac_addr);
727 dtsec_detach(device_t dev)
729 struct dtsec_softc *sc;
732 sc = device_get_softc(dev);
735 if (device_is_attached(dev)) {
737 /* Shutdown interface */
739 dtsec_if_deinit_locked(sc);
744 if_free(sc->sc_ifnet);
748 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
749 /* Free RX/TX FQRs */
750 dtsec_rm_fqr_rx_free(sc);
751 dtsec_rm_fqr_tx_free(sc);
753 /* Free frame info pool */
754 dtsec_rm_fi_pool_free(sc);
756 /* Free RX buffer pool */
757 dtsec_rm_pool_rx_free(sc);
760 dtsec_fm_mac_free(sc);
761 dtsec_fm_port_free_both(sc);
764 mtx_destroy(&sc->sc_lock);
770 dtsec_suspend(device_t dev)
777 dtsec_resume(device_t dev)
784 dtsec_shutdown(device_t dev)
793 * @group MII bus interface.
797 dtsec_miibus_readreg(device_t dev, int phy, int reg)
799 struct dtsec_softc *sc;
803 sc = device_get_softc(dev);
805 if (phy != sc->sc_phy_addr)
809 error = FM_MAC_MII_ReadPhyReg(dtsec_mdio_mac_handle, phy, reg, &data);
810 DTSEC_MII_UNLOCK(sc);
812 device_printf(dev, "Error while reading from PHY (NetCommSw "
813 "error: %d)\n", error);
821 dtsec_miibus_writereg(device_t dev, int phy, int reg, int value)
823 struct dtsec_softc *sc;
826 sc = device_get_softc(dev);
828 if (phy != sc->sc_phy_addr)
832 error = FM_MAC_MII_WritePhyReg(dtsec_mdio_mac_handle, phy, reg, value);
833 DTSEC_MII_UNLOCK(sc);
835 device_printf(dev, "Error while writing to PHY (NetCommSw "
836 "error: %d).\n", error);
844 dtsec_miibus_statchg(device_t dev)
846 struct dtsec_softc *sc;
851 sc = device_get_softc(dev);
853 DTSEC_LOCK_ASSERT(sc);
855 duplex = ((sc->sc_mii->mii_media_active & IFM_GMASK) == IFM_FDX);
857 switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
860 speed = e_ENET_SPEED_1000;
864 speed = e_ENET_SPEED_100;
868 speed = e_ENET_SPEED_10;
872 speed = e_ENET_SPEED_10;
875 error = FM_MAC_AdjustLink(sc->sc_mach, speed, duplex);
877 device_printf(sc->sc_dev, "error while adjusting MAC speed.\n");