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/integrations/dpaa_integration_ext.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"
70 #define DTSEC_MIN_FRAME_SIZE 64
71 #define DTSEC_MAX_FRAME_SIZE 9600
73 #define DTSEC_REG_MAXFRM 0x110
76 * @group dTSEC private defines.
80 * dTSEC FMan MAC exceptions info struct.
82 struct dtsec_fm_mac_ex_str {
90 * @group FMan MAC routines.
93 #define DTSEC_MAC_EXCEPTIONS_END (-1)
96 * FMan MAC exceptions.
98 static const struct dtsec_fm_mac_ex_str dtsec_fm_mac_exceptions[] = {
99 { e_FM_MAC_EX_10G_MDIO_SCAN_EVENTMDIO, "MDIO scan event" },
100 { e_FM_MAC_EX_10G_MDIO_CMD_CMPL, "MDIO command completion" },
101 { e_FM_MAC_EX_10G_REM_FAULT, "Remote fault" },
102 { e_FM_MAC_EX_10G_LOC_FAULT, "Local fault" },
103 { e_FM_MAC_EX_10G_1TX_ECC_ER, "Transmit frame ECC error" },
104 { e_FM_MAC_EX_10G_TX_FIFO_UNFL, "Transmit FIFO underflow" },
105 { e_FM_MAC_EX_10G_TX_FIFO_OVFL, "Receive FIFO overflow" },
106 { e_FM_MAC_EX_10G_TX_ER, "Transmit frame error" },
107 { e_FM_MAC_EX_10G_RX_FIFO_OVFL, "Receive FIFO overflow" },
108 { e_FM_MAC_EX_10G_RX_ECC_ER, "Receive frame ECC error" },
109 { e_FM_MAC_EX_10G_RX_JAB_FRM, "Receive jabber frame" },
110 { e_FM_MAC_EX_10G_RX_OVRSZ_FRM, "Receive oversized frame" },
111 { e_FM_MAC_EX_10G_RX_RUNT_FRM, "Receive runt frame" },
112 { e_FM_MAC_EX_10G_RX_FRAG_FRM, "Receive fragment frame" },
113 { e_FM_MAC_EX_10G_RX_LEN_ER, "Receive payload length error" },
114 { e_FM_MAC_EX_10G_RX_CRC_ER, "Receive CRC error" },
115 { e_FM_MAC_EX_10G_RX_ALIGN_ER, "Receive alignment error" },
116 { e_FM_MAC_EX_1G_BAB_RX, "Babbling receive error" },
117 { e_FM_MAC_EX_1G_RX_CTL, "Receive control (pause frame) interrupt" },
118 { e_FM_MAC_EX_1G_GRATEFUL_TX_STP_COMPLET, "Graceful transmit stop "
120 { e_FM_MAC_EX_1G_BAB_TX, "Babbling transmit error" },
121 { e_FM_MAC_EX_1G_TX_CTL, "Transmit control (pause frame) interrupt" },
122 { e_FM_MAC_EX_1G_TX_ERR, "Transmit error" },
123 { e_FM_MAC_EX_1G_LATE_COL, "Late collision" },
124 { e_FM_MAC_EX_1G_COL_RET_LMT, "Collision retry limit" },
125 { e_FM_MAC_EX_1G_TX_FIFO_UNDRN, "Transmit FIFO underrun" },
126 { e_FM_MAC_EX_1G_MAG_PCKT, "Magic Packet detected when dTSEC is in "
127 "Magic Packet detection mode" },
128 { e_FM_MAC_EX_1G_MII_MNG_RD_COMPLET, "MII management read completion" },
129 { e_FM_MAC_EX_1G_MII_MNG_WR_COMPLET, "MII management write completion" },
130 { e_FM_MAC_EX_1G_GRATEFUL_RX_STP_COMPLET, "Graceful receive stop "
132 { e_FM_MAC_EX_1G_TX_DATA_ERR, "Internal data error on transmit" },
133 { e_FM_MAC_EX_1G_RX_DATA_ERR, "Internal data error on receive" },
134 { e_FM_MAC_EX_1G_1588_TS_RX_ERR, "Time-Stamp Receive Error" },
135 { e_FM_MAC_EX_1G_RX_MIB_CNT_OVFL, "MIB counter overflow" },
136 { DTSEC_MAC_EXCEPTIONS_END, "" }
140 dtsec_fm_mac_ex_to_str(e_FmMacExceptions exception)
144 for (i = 0; dtsec_fm_mac_exceptions[i].num != exception &&
145 dtsec_fm_mac_exceptions[i].num != DTSEC_MAC_EXCEPTIONS_END; ++i)
148 if (dtsec_fm_mac_exceptions[i].num == DTSEC_MAC_EXCEPTIONS_END)
149 return ("<Unknown Exception>");
151 return (dtsec_fm_mac_exceptions[i].str);
155 dtsec_fm_mac_mdio_event_callback(t_Handle h_App,
156 e_FmMacExceptions exception)
158 struct dtsec_softc *sc;
161 device_printf(sc->sc_dev, "MDIO event %i: %s.\n", exception,
162 dtsec_fm_mac_ex_to_str(exception));
166 dtsec_fm_mac_exception_callback(t_Handle app, e_FmMacExceptions exception)
168 struct dtsec_softc *sc;
171 device_printf(sc->sc_dev, "MAC exception %i: %s.\n", exception,
172 dtsec_fm_mac_ex_to_str(exception));
176 dtsec_fm_mac_free(struct dtsec_softc *sc)
178 if (sc->sc_mach == NULL)
181 FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
182 FM_MAC_Free(sc->sc_mach);
187 dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac)
189 t_FmMacParams params;
192 memset(¶ms, 0, sizeof(params));
193 memcpy(¶ms.addr, mac, sizeof(params.addr));
195 params.baseAddr = rman_get_bushandle(sc->sc_mem);
196 params.enetMode = sc->sc_mac_enet_mode;
197 params.macId = sc->sc_eth_id;
198 params.mdioIrq = sc->sc_mac_mdio_irq;
199 params.f_Event = dtsec_fm_mac_mdio_event_callback;
200 params.f_Exception = dtsec_fm_mac_exception_callback;
202 params.h_Fm = sc->sc_fmh;
204 sc->sc_mach = FM_MAC_Config(¶ms);
205 if (sc->sc_mach == NULL) {
206 device_printf(sc->sc_dev, "couldn't configure FM_MAC module.\n"
211 error = FM_MAC_ConfigResetOnInit(sc->sc_mach, TRUE);
213 device_printf(sc->sc_dev, "couldn't enable reset on init "
215 dtsec_fm_mac_free(sc);
219 /* Do not inform about pause frames */
220 error = FM_MAC_ConfigException(sc->sc_mach, e_FM_MAC_EX_1G_RX_CTL,
223 device_printf(sc->sc_dev, "couldn't disable pause frames "
225 dtsec_fm_mac_free(sc);
229 error = FM_MAC_Init(sc->sc_mach);
231 device_printf(sc->sc_dev, "couldn't initialize FM_MAC module."
233 dtsec_fm_mac_free(sc);
243 * @group FMan PORT routines.
247 dtsec_fm_port_ex_to_str(e_FmPortExceptions exception)
251 case e_FM_PORT_EXCEPTION_IM_BUSY:
252 return ("IM: RX busy");
254 return ("<Unknown Exception>");
259 dtsec_fm_port_rx_exception_callback(t_Handle app,
260 e_FmPortExceptions exception)
262 struct dtsec_softc *sc;
265 device_printf(sc->sc_dev, "RX exception: %i: %s.\n", exception,
266 dtsec_fm_port_ex_to_str(exception));
270 dtsec_fm_port_tx_exception_callback(t_Handle app,
271 e_FmPortExceptions exception)
273 struct dtsec_softc *sc;
276 device_printf(sc->sc_dev, "TX exception: %i: %s.\n", exception,
277 dtsec_fm_port_ex_to_str(exception));
281 dtsec_fm_port_rx_type(enum eth_dev_type type)
285 return (e_FM_PORT_TYPE_RX);
287 return (e_FM_PORT_TYPE_RX_10G);
289 return (e_FM_PORT_TYPE_DUMMY);
294 dtsec_fm_port_tx_type(enum eth_dev_type type)
299 return (e_FM_PORT_TYPE_TX);
301 return (e_FM_PORT_TYPE_TX_10G);
303 return (e_FM_PORT_TYPE_DUMMY);
308 dtsec_fm_port_free_both(struct dtsec_softc *sc)
311 FM_PORT_Free(sc->sc_rxph);
316 FM_PORT_Free(sc->sc_txph);
324 * @group IFnet routines.
328 dtsec_set_mtu(struct dtsec_softc *sc, unsigned int mtu)
331 mtu += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
333 DTSEC_LOCK_ASSERT(sc);
335 if (mtu >= DTSEC_MIN_FRAME_SIZE && mtu <= DTSEC_MAX_FRAME_SIZE) {
336 bus_write_4(sc->sc_mem, DTSEC_REG_MAXFRM, mtu);
344 dtsec_if_enable_locked(struct dtsec_softc *sc)
348 DTSEC_LOCK_ASSERT(sc);
350 error = FM_MAC_Enable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
354 error = FM_PORT_Enable(sc->sc_rxph);
358 error = FM_PORT_Enable(sc->sc_txph);
362 sc->sc_ifnet->if_drv_flags |= IFF_DRV_RUNNING;
364 /* Refresh link state */
365 dtsec_miibus_statchg(sc->sc_dev);
371 dtsec_if_disable_locked(struct dtsec_softc *sc)
375 DTSEC_LOCK_ASSERT(sc);
377 error = FM_MAC_Disable(sc->sc_mach, e_COMM_MODE_RX_AND_TX);
381 error = FM_PORT_Disable(sc->sc_rxph);
385 error = FM_PORT_Disable(sc->sc_txph);
389 sc->sc_ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
395 dtsec_if_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
397 struct dtsec_softc *sc;
402 ifr = (struct ifreq *)data;
405 /* Basic functionality to achieve media status reports */
409 if (dtsec_set_mtu(sc, ifr->ifr_mtu))
410 ifp->if_mtu = ifr->ifr_mtu;
418 if (sc->sc_ifnet->if_flags & IFF_UP)
419 error = dtsec_if_enable_locked(sc);
421 error = dtsec_if_disable_locked(sc);
428 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media,
433 error = ether_ioctl(ifp, command, data);
440 dtsec_if_tick(void *arg)
442 struct dtsec_softc *sc;
449 mii_tick(sc->sc_mii);
450 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
456 dtsec_if_deinit_locked(struct dtsec_softc *sc)
459 DTSEC_LOCK_ASSERT(sc);
462 callout_drain(&sc->sc_tick_callout);
467 dtsec_if_init_locked(struct dtsec_softc *sc)
471 DTSEC_LOCK_ASSERT(sc);
473 /* Set MAC address */
474 error = FM_MAC_ModifyMacAddr(sc->sc_mach,
475 (t_EnetAddr *)IF_LLADDR(sc->sc_ifnet));
477 device_printf(sc->sc_dev, "couldn't set MAC address.\n");
481 /* Start MII polling */
483 callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
485 if (sc->sc_ifnet->if_flags & IFF_UP) {
486 error = dtsec_if_enable_locked(sc);
490 error = dtsec_if_disable_locked(sc);
498 dtsec_if_deinit_locked(sc);
499 device_printf(sc->sc_dev, "initialization error.\n");
504 dtsec_if_init(void *data)
506 struct dtsec_softc *sc;
511 dtsec_if_init_locked(sc);
516 dtsec_if_start(struct ifnet *ifp)
518 struct dtsec_softc *sc;
522 sc->sc_start_locked(sc);
527 dtsec_if_watchdog(struct ifnet *ifp)
535 * @group IFmedia routines.
539 dtsec_ifmedia_upd(struct ifnet *ifp)
541 struct dtsec_softc *sc = ifp->if_softc;
544 mii_mediachg(sc->sc_mii);
551 dtsec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
553 struct dtsec_softc *sc = ifp->if_softc;
557 mii_pollstat(sc->sc_mii);
559 ifmr->ifm_active = sc->sc_mii->mii_media_active;
560 ifmr->ifm_status = sc->sc_mii->mii_media_status;
568 * @group dTSEC bus interface.
572 dtsec_configure_mode(struct dtsec_softc *sc)
576 snprintf(tunable, sizeof(tunable), "%s.independent_mode",
577 device_get_nameunit(sc->sc_dev));
579 sc->sc_mode = DTSEC_MODE_REGULAR;
580 TUNABLE_INT_FETCH(tunable, &sc->sc_mode);
582 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
583 sc->sc_port_rx_init = dtsec_rm_fm_port_rx_init;
584 sc->sc_port_tx_init = dtsec_rm_fm_port_tx_init;
585 sc->sc_start_locked = dtsec_rm_if_start_locked;
587 sc->sc_port_rx_init = dtsec_im_fm_port_rx_init;
588 sc->sc_port_tx_init = dtsec_im_fm_port_tx_init;
589 sc->sc_start_locked = dtsec_im_if_start_locked;
592 device_printf(sc->sc_dev, "Configured for %s mode.\n",
593 (sc->sc_mode == DTSEC_MODE_REGULAR) ? "regular" : "independent");
597 dtsec_attach(device_t dev)
599 struct dtsec_softc *sc;
604 sc = device_get_softc(dev);
606 parent = device_get_parent(dev);
608 sc->sc_mac_mdio_irq = NO_IRQ;
610 /* Check if MallocSmart allocator is ready */
611 if (XX_MallocSmartInit() != E_OK)
615 mtx_init(&sc->sc_lock, device_get_nameunit(dev),
616 "DTSEC Global Lock", MTX_DEF);
618 mtx_init(&sc->sc_mii_lock, device_get_nameunit(dev),
619 "DTSEC MII Lock", MTX_DEF);
622 callout_init(&sc->sc_tick_callout, CALLOUT_MPSAFE);
624 /* Read configuraton */
625 if ((error = fman_get_handle(parent, &sc->sc_fmh)) != 0)
628 if ((error = fman_get_muram_handle(parent, &sc->sc_muramh)) != 0)
631 if ((error = fman_get_bushandle(parent, &sc->sc_fm_base)) != 0)
634 /* Configure working mode */
635 dtsec_configure_mode(sc);
637 /* If we are working in regular mode configure BMAN and QMAN */
638 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
639 /* Create RX buffer pool */
640 error = dtsec_rm_pool_rx_init(sc);
644 /* Create RX frame queue range */
645 error = dtsec_rm_fqr_rx_init(sc);
649 /* Create frame info pool */
650 error = dtsec_rm_fi_pool_init(sc);
654 /* Create TX frame queue range */
655 error = dtsec_rm_fqr_tx_init(sc);
660 /* Init FMan MAC module. */
661 error = dtsec_fm_mac_init(sc, sc->sc_mac_addr);
667 /* Init FMan TX port */
668 error = sc->sc_port_tx_init(sc, device_get_unit(sc->sc_dev));
674 /* Init FMan RX port */
675 error = sc->sc_port_rx_init(sc, device_get_unit(sc->sc_dev));
681 /* Create network interface for upper layers */
682 ifp = sc->sc_ifnet = if_alloc(IFT_ETHER);
684 device_printf(sc->sc_dev, "if_alloc() failed.\n");
690 ifp->if_mtu = ETHERMTU; /* TODO: Configure */
691 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST;
692 ifp->if_init = dtsec_if_init;
693 ifp->if_start = dtsec_if_start;
694 ifp->if_ioctl = dtsec_if_ioctl;
695 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
697 if (sc->sc_phy_addr >= 0)
698 if_initname(ifp, device_get_name(sc->sc_dev),
699 device_get_unit(sc->sc_dev));
701 if_initname(ifp, "dtsec_phy", device_get_unit(sc->sc_dev));
705 IFQ_SET_MAXLEN(&ifp->if_snd, TSEC_TX_NUM_DESC - 1);
706 ifp->if_snd.ifq_drv_maxlen = TSEC_TX_NUM_DESC - 1;
707 IFQ_SET_READY(&ifp->if_snd);
709 ifp->if_capabilities = IFCAP_JUMBO_MTU; /* TODO: HWCSUM */
710 ifp->if_capenable = ifp->if_capabilities;
713 error = mii_attach(sc->sc_dev, &sc->sc_mii_dev, ifp, dtsec_ifmedia_upd,
714 dtsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->sc_phy_addr,
717 device_printf(sc->sc_dev, "attaching PHYs failed: %d\n", error);
718 dtsec_detach(sc->sc_dev);
721 sc->sc_mii = device_get_softc(sc->sc_mii_dev);
723 /* Attach to stack */
724 ether_ifattach(ifp, sc->sc_mac_addr);
730 dtsec_detach(device_t dev)
732 struct dtsec_softc *sc;
735 sc = device_get_softc(dev);
738 if (device_is_attached(dev)) {
740 /* Shutdown interface */
742 dtsec_if_deinit_locked(sc);
747 if_free(sc->sc_ifnet);
751 if (sc->sc_mode == DTSEC_MODE_REGULAR) {
752 /* Free RX/TX FQRs */
753 dtsec_rm_fqr_rx_free(sc);
754 dtsec_rm_fqr_tx_free(sc);
756 /* Free frame info pool */
757 dtsec_rm_fi_pool_free(sc);
759 /* Free RX buffer pool */
760 dtsec_rm_pool_rx_free(sc);
763 dtsec_fm_mac_free(sc);
764 dtsec_fm_port_free_both(sc);
767 mtx_destroy(&sc->sc_lock);
773 dtsec_suspend(device_t dev)
780 dtsec_resume(device_t dev)
787 dtsec_shutdown(device_t dev)
796 * @group MII bus interface.
800 dtsec_miibus_readreg(device_t dev, int phy, int reg)
802 struct dtsec_softc *sc;
804 sc = device_get_softc(dev);
806 return (MIIBUS_READREG(sc->sc_mdio, phy, reg));
810 dtsec_miibus_writereg(device_t dev, int phy, int reg, int value)
813 struct dtsec_softc *sc;
815 sc = device_get_softc(dev);
817 return (MIIBUS_WRITEREG(sc->sc_mdio, phy, reg, value));
821 dtsec_miibus_statchg(device_t dev)
823 struct dtsec_softc *sc;
828 sc = device_get_softc(dev);
830 DTSEC_LOCK_ASSERT(sc);
832 duplex = ((sc->sc_mii->mii_media_active & IFM_GMASK) == IFM_FDX);
834 switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
837 speed = e_ENET_SPEED_1000;
841 speed = e_ENET_SPEED_100;
845 speed = e_ENET_SPEED_10;
849 speed = e_ENET_SPEED_10;
852 error = FM_MAC_AdjustLink(sc->sc_mach, speed, duplex);
854 device_printf(sc->sc_dev, "error while adjusting MAC speed.\n");