10aeed3e9SJustin Hibbits /*-
20aeed3e9SJustin Hibbits * Copyright (c) 2012 Semihalf.
30aeed3e9SJustin Hibbits * All rights reserved.
40aeed3e9SJustin Hibbits *
50aeed3e9SJustin Hibbits * Redistribution and use in source and binary forms, with or without
60aeed3e9SJustin Hibbits * modification, are permitted provided that the following conditions
70aeed3e9SJustin Hibbits * are met:
80aeed3e9SJustin Hibbits * 1. Redistributions of source code must retain the above copyright
90aeed3e9SJustin Hibbits * notice, this list of conditions and the following disclaimer.
100aeed3e9SJustin Hibbits * 2. Redistributions in binary form must reproduce the above copyright
110aeed3e9SJustin Hibbits * notice, this list of conditions and the following disclaimer in the
120aeed3e9SJustin Hibbits * documentation and/or other materials provided with the distribution.
130aeed3e9SJustin Hibbits *
140aeed3e9SJustin Hibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150aeed3e9SJustin Hibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160aeed3e9SJustin Hibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170aeed3e9SJustin Hibbits * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180aeed3e9SJustin Hibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190aeed3e9SJustin Hibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200aeed3e9SJustin Hibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210aeed3e9SJustin Hibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220aeed3e9SJustin Hibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230aeed3e9SJustin Hibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240aeed3e9SJustin Hibbits * SUCH DAMAGE.
250aeed3e9SJustin Hibbits */
260aeed3e9SJustin Hibbits
270aeed3e9SJustin Hibbits #include <sys/param.h>
280aeed3e9SJustin Hibbits #include <sys/systm.h>
290aeed3e9SJustin Hibbits #include <sys/kernel.h>
300aeed3e9SJustin Hibbits #include <sys/module.h>
310aeed3e9SJustin Hibbits #include <sys/bus.h>
320aeed3e9SJustin Hibbits #include <sys/rman.h>
330aeed3e9SJustin Hibbits #include <sys/malloc.h>
340aeed3e9SJustin Hibbits #include <sys/mbuf.h>
350aeed3e9SJustin Hibbits #include <sys/socket.h>
360aeed3e9SJustin Hibbits #include <sys/sysctl.h>
370aeed3e9SJustin Hibbits #include <sys/sockio.h>
380aeed3e9SJustin Hibbits
390aeed3e9SJustin Hibbits #include <net/ethernet.h>
400aeed3e9SJustin Hibbits #include <net/if.h>
410aeed3e9SJustin Hibbits #include <net/if_dl.h>
420aeed3e9SJustin Hibbits #include <net/if_media.h>
430aeed3e9SJustin Hibbits #include <net/if_types.h>
440aeed3e9SJustin Hibbits #include <net/if_arp.h>
450aeed3e9SJustin Hibbits
460aeed3e9SJustin Hibbits #include <dev/mii/mii.h>
470aeed3e9SJustin Hibbits #include <dev/mii/miivar.h>
480aeed3e9SJustin Hibbits
490aeed3e9SJustin Hibbits #include "miibus_if.h"
500aeed3e9SJustin Hibbits
51852ba100SJustin Hibbits #include <contrib/ncsw/inc/integrations/dpaa_integration_ext.h>
520aeed3e9SJustin Hibbits #include <contrib/ncsw/inc/Peripherals/fm_mac_ext.h>
530aeed3e9SJustin Hibbits #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
540aeed3e9SJustin Hibbits #include <contrib/ncsw/inc/xx_ext.h>
550aeed3e9SJustin Hibbits
560aeed3e9SJustin Hibbits #include "fman.h"
570aeed3e9SJustin Hibbits #include "if_dtsec.h"
580aeed3e9SJustin Hibbits #include "if_dtsec_im.h"
590aeed3e9SJustin Hibbits
600aeed3e9SJustin Hibbits
610aeed3e9SJustin Hibbits /**
620aeed3e9SJustin Hibbits * @group dTSEC FMan PORT routines.
630aeed3e9SJustin Hibbits * @{
640aeed3e9SJustin Hibbits */
650aeed3e9SJustin Hibbits static e_RxStoreResponse
dtsec_im_fm_port_rx_callback(t_Handle app,uint8_t * data,uint16_t length,uint16_t status,uint8_t position,t_Handle buf_context)660aeed3e9SJustin Hibbits dtsec_im_fm_port_rx_callback(t_Handle app, uint8_t *data, uint16_t length,
670aeed3e9SJustin Hibbits uint16_t status, uint8_t position, t_Handle buf_context)
680aeed3e9SJustin Hibbits {
690aeed3e9SJustin Hibbits struct dtsec_softc *sc;
700aeed3e9SJustin Hibbits struct mbuf *m;
710aeed3e9SJustin Hibbits
720aeed3e9SJustin Hibbits /* TODO STATUS / Position checking */
730aeed3e9SJustin Hibbits sc = app;
740aeed3e9SJustin Hibbits
750aeed3e9SJustin Hibbits m = m_devget(data, length, 0, sc->sc_ifnet, NULL);
760aeed3e9SJustin Hibbits if (m)
77*1510005cSJustin Hibbits if_input(sc->sc_ifnet, m);
780aeed3e9SJustin Hibbits
790aeed3e9SJustin Hibbits XX_FreeSmart(data);
800aeed3e9SJustin Hibbits
810aeed3e9SJustin Hibbits return (e_RX_STORE_RESPONSE_CONTINUE);
820aeed3e9SJustin Hibbits }
830aeed3e9SJustin Hibbits
840aeed3e9SJustin Hibbits static void
dtsec_im_fm_port_tx_conf_callback(t_Handle app,uint8_t * data,uint16_t status,t_Handle buf_context)850aeed3e9SJustin Hibbits dtsec_im_fm_port_tx_conf_callback(t_Handle app, uint8_t *data, uint16_t status,
860aeed3e9SJustin Hibbits t_Handle buf_context)
870aeed3e9SJustin Hibbits {
880aeed3e9SJustin Hibbits
890aeed3e9SJustin Hibbits /* TODO: Check status */
900aeed3e9SJustin Hibbits XX_FreeSmart(data);
910aeed3e9SJustin Hibbits }
920aeed3e9SJustin Hibbits
930aeed3e9SJustin Hibbits static uint8_t *
dtsec_im_fm_port_rx_get_buf(t_Handle buffer_pool,t_Handle * buf_context_handle)940aeed3e9SJustin Hibbits dtsec_im_fm_port_rx_get_buf(t_Handle buffer_pool, t_Handle *buf_context_handle)
950aeed3e9SJustin Hibbits {
960aeed3e9SJustin Hibbits struct dtsec_softc *sc;
970aeed3e9SJustin Hibbits uint8_t *buffer;
980aeed3e9SJustin Hibbits
990aeed3e9SJustin Hibbits sc = buffer_pool;
1000aeed3e9SJustin Hibbits
1010aeed3e9SJustin Hibbits buffer = XX_MallocSmart(FM_PORT_BUFFER_SIZE, 0, sizeof(void *));
1020aeed3e9SJustin Hibbits if (!buffer)
1030aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "couldn't allocate RX buffer.\n");
1040aeed3e9SJustin Hibbits
1050aeed3e9SJustin Hibbits return (buffer);
1060aeed3e9SJustin Hibbits }
1070aeed3e9SJustin Hibbits
1080aeed3e9SJustin Hibbits static t_Error
dtsec_im_fm_port_rx_put_buf(t_Handle buffer_pool,uint8_t * buffer,t_Handle buf_context)1090aeed3e9SJustin Hibbits dtsec_im_fm_port_rx_put_buf(t_Handle buffer_pool, uint8_t *buffer,
1100aeed3e9SJustin Hibbits t_Handle buf_context)
1110aeed3e9SJustin Hibbits {
1120aeed3e9SJustin Hibbits
1130aeed3e9SJustin Hibbits XX_FreeSmart(buffer);
1140aeed3e9SJustin Hibbits return (E_OK);
1150aeed3e9SJustin Hibbits }
1160aeed3e9SJustin Hibbits
1170aeed3e9SJustin Hibbits int
dtsec_im_fm_port_rx_init(struct dtsec_softc * sc,int unit)1180aeed3e9SJustin Hibbits dtsec_im_fm_port_rx_init(struct dtsec_softc *sc, int unit)
1190aeed3e9SJustin Hibbits {
1200aeed3e9SJustin Hibbits t_FmPortParams params;
1210aeed3e9SJustin Hibbits t_BufferPoolInfo *pool_params;
1220aeed3e9SJustin Hibbits t_FmPortImRxTxParams *im_params;
1230aeed3e9SJustin Hibbits t_Error error;
1240aeed3e9SJustin Hibbits
1250aeed3e9SJustin Hibbits memset(¶ms, 0, sizeof(params));
1260aeed3e9SJustin Hibbits
1270aeed3e9SJustin Hibbits params.baseAddr = sc->sc_fm_base + sc->sc_port_rx_hw_id;
1280aeed3e9SJustin Hibbits params.h_Fm = sc->sc_fmh;
1290aeed3e9SJustin Hibbits params.portType = dtsec_fm_port_rx_type(sc->sc_eth_dev_type);
1300aeed3e9SJustin Hibbits params.portId = sc->sc_eth_id;
1310aeed3e9SJustin Hibbits params.independentModeEnable = TRUE;
1320aeed3e9SJustin Hibbits params.liodnBase = FM_PORT_LIODN_BASE;
1330aeed3e9SJustin Hibbits params.f_Exception = dtsec_fm_port_rx_exception_callback;
1340aeed3e9SJustin Hibbits params.h_App = sc;
1350aeed3e9SJustin Hibbits
1360aeed3e9SJustin Hibbits im_params = ¶ms.specificParams.imRxTxParams;
1370aeed3e9SJustin Hibbits im_params->h_FmMuram = sc->sc_muramh;
1380aeed3e9SJustin Hibbits im_params->liodnOffset = FM_PORT_LIODN_OFFSET;
1390aeed3e9SJustin Hibbits im_params->dataMemId = FM_PORT_MEM_ID;
1400aeed3e9SJustin Hibbits im_params->dataMemAttributes = FM_PORT_MEM_ATTR;
1410aeed3e9SJustin Hibbits im_params->f_RxStore = dtsec_im_fm_port_rx_callback;
1420aeed3e9SJustin Hibbits
1430aeed3e9SJustin Hibbits pool_params = ¶ms.specificParams.imRxTxParams.rxPoolParams;
1440aeed3e9SJustin Hibbits pool_params->h_BufferPool = sc;
1450aeed3e9SJustin Hibbits pool_params->f_GetBuf = dtsec_im_fm_port_rx_get_buf;
1460aeed3e9SJustin Hibbits pool_params->f_PutBuf = dtsec_im_fm_port_rx_put_buf;
1470aeed3e9SJustin Hibbits pool_params->bufferSize = FM_PORT_BUFFER_SIZE;
1480aeed3e9SJustin Hibbits
1490aeed3e9SJustin Hibbits sc->sc_rxph = FM_PORT_Config(¶ms);
1500aeed3e9SJustin Hibbits if (sc->sc_rxph == NULL) {
1510aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "couldn't configure FM Port RX.\n");
1520aeed3e9SJustin Hibbits return (ENXIO);
1530aeed3e9SJustin Hibbits }
1540aeed3e9SJustin Hibbits
1550aeed3e9SJustin Hibbits error = FM_PORT_Init(sc->sc_rxph);
1560aeed3e9SJustin Hibbits if (error != E_OK) {
1570aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "couldn't initialize FM Port RX.\n");
1580aeed3e9SJustin Hibbits FM_PORT_Free(sc->sc_rxph);
1590aeed3e9SJustin Hibbits return (ENXIO);
1600aeed3e9SJustin Hibbits }
1610aeed3e9SJustin Hibbits
1620aeed3e9SJustin Hibbits if (bootverbose)
1630aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "RX hw port 0x%02x initialized.\n",
1640aeed3e9SJustin Hibbits sc->sc_port_rx_hw_id);
1650aeed3e9SJustin Hibbits
1660aeed3e9SJustin Hibbits return (0);
1670aeed3e9SJustin Hibbits }
1680aeed3e9SJustin Hibbits
1690aeed3e9SJustin Hibbits int
dtsec_im_fm_port_tx_init(struct dtsec_softc * sc,int unit)1700aeed3e9SJustin Hibbits dtsec_im_fm_port_tx_init(struct dtsec_softc *sc, int unit)
1710aeed3e9SJustin Hibbits {
1720aeed3e9SJustin Hibbits t_FmPortParams params;
1730aeed3e9SJustin Hibbits t_FmPortImRxTxParams *im_params;
1740aeed3e9SJustin Hibbits t_Error error;
1750aeed3e9SJustin Hibbits
1760aeed3e9SJustin Hibbits memset(¶ms, 0, sizeof(params));
1770aeed3e9SJustin Hibbits
1780aeed3e9SJustin Hibbits params.baseAddr = sc->sc_fm_base + sc->sc_port_tx_hw_id;
1790aeed3e9SJustin Hibbits params.h_Fm = sc->sc_fmh;
1800aeed3e9SJustin Hibbits params.portType = dtsec_fm_port_tx_type(sc->sc_eth_dev_type);
1810aeed3e9SJustin Hibbits params.portId = unit;
1820aeed3e9SJustin Hibbits params.independentModeEnable = TRUE;
1830aeed3e9SJustin Hibbits params.liodnBase = FM_PORT_LIODN_BASE;
1840aeed3e9SJustin Hibbits params.f_Exception = dtsec_fm_port_tx_exception_callback;
1850aeed3e9SJustin Hibbits params.h_App = sc;
1860aeed3e9SJustin Hibbits
1870aeed3e9SJustin Hibbits im_params = ¶ms.specificParams.imRxTxParams;
1880aeed3e9SJustin Hibbits im_params->h_FmMuram = sc->sc_muramh;
1890aeed3e9SJustin Hibbits im_params->liodnOffset = FM_PORT_LIODN_OFFSET;
1900aeed3e9SJustin Hibbits im_params->dataMemId = FM_PORT_MEM_ID;
1910aeed3e9SJustin Hibbits im_params->dataMemAttributes = FM_PORT_MEM_ATTR;
1920aeed3e9SJustin Hibbits im_params->f_TxConf = dtsec_im_fm_port_tx_conf_callback;
1930aeed3e9SJustin Hibbits
1940aeed3e9SJustin Hibbits sc->sc_txph = FM_PORT_Config(¶ms);
1950aeed3e9SJustin Hibbits if (sc->sc_txph == NULL) {
1960aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "couldn't configure FM Port TX.\n");
1970aeed3e9SJustin Hibbits return (ENXIO);
1980aeed3e9SJustin Hibbits }
1990aeed3e9SJustin Hibbits
2000aeed3e9SJustin Hibbits error = FM_PORT_Init(sc->sc_txph);
2010aeed3e9SJustin Hibbits if (error != E_OK) {
2020aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "couldn't initialize FM Port TX.\n");
2030aeed3e9SJustin Hibbits FM_PORT_Free(sc->sc_txph);
2040aeed3e9SJustin Hibbits return (ENXIO);
2050aeed3e9SJustin Hibbits }
2060aeed3e9SJustin Hibbits
2070aeed3e9SJustin Hibbits if (bootverbose)
2080aeed3e9SJustin Hibbits device_printf(sc->sc_dev, "TX hw port 0x%02x initialized.\n",
2090aeed3e9SJustin Hibbits sc->sc_port_tx_hw_id);
2100aeed3e9SJustin Hibbits
2110aeed3e9SJustin Hibbits return (0);
2120aeed3e9SJustin Hibbits }
2130aeed3e9SJustin Hibbits /** @} */
2140aeed3e9SJustin Hibbits
2150aeed3e9SJustin Hibbits
2160aeed3e9SJustin Hibbits /**
2170aeed3e9SJustin Hibbits * @group dTSEC IFnet routines.
2180aeed3e9SJustin Hibbits * @{
2190aeed3e9SJustin Hibbits */
2200aeed3e9SJustin Hibbits void
dtsec_im_if_start_locked(struct dtsec_softc * sc)2210aeed3e9SJustin Hibbits dtsec_im_if_start_locked(struct dtsec_softc *sc)
2220aeed3e9SJustin Hibbits {
2230aeed3e9SJustin Hibbits uint8_t *buffer;
2240aeed3e9SJustin Hibbits uint16_t length;
2250aeed3e9SJustin Hibbits struct mbuf *m;
2260aeed3e9SJustin Hibbits int error;
2270aeed3e9SJustin Hibbits
2280aeed3e9SJustin Hibbits DTSEC_LOCK_ASSERT(sc);
2290aeed3e9SJustin Hibbits /* TODO: IFF_DRV_OACTIVE */
2300aeed3e9SJustin Hibbits
2310aeed3e9SJustin Hibbits if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) == 0)
2320aeed3e9SJustin Hibbits return;
2330aeed3e9SJustin Hibbits
234*1510005cSJustin Hibbits if ((if_getdrvflags(sc->sc_ifnet) & IFF_DRV_RUNNING) != IFF_DRV_RUNNING)
2350aeed3e9SJustin Hibbits return;
2360aeed3e9SJustin Hibbits
237*1510005cSJustin Hibbits while (!if_sendq_empty(sc->sc_ifnet)) {
238*1510005cSJustin Hibbits m = if_dequeue(sc->sc_ifnet);
2390aeed3e9SJustin Hibbits if (m == NULL)
2400aeed3e9SJustin Hibbits break;
2410aeed3e9SJustin Hibbits
2420aeed3e9SJustin Hibbits length = m_length(m, NULL);
2430aeed3e9SJustin Hibbits buffer = XX_MallocSmart(length, 0, sizeof(void *));
2440aeed3e9SJustin Hibbits if (!buffer) {
2450aeed3e9SJustin Hibbits m_freem(m);
2460aeed3e9SJustin Hibbits break;
2470aeed3e9SJustin Hibbits }
2480aeed3e9SJustin Hibbits
2490aeed3e9SJustin Hibbits m_copydata(m, 0, length, buffer);
2500aeed3e9SJustin Hibbits m_freem(m);
2510aeed3e9SJustin Hibbits
2520aeed3e9SJustin Hibbits error = FM_PORT_ImTx(sc->sc_txph, buffer, length, TRUE, buffer);
2530aeed3e9SJustin Hibbits if (error != E_OK) {
2540aeed3e9SJustin Hibbits /* TODO: Ring full */
2550aeed3e9SJustin Hibbits XX_FreeSmart(buffer);
2560aeed3e9SJustin Hibbits break;
2570aeed3e9SJustin Hibbits }
2580aeed3e9SJustin Hibbits }
2590aeed3e9SJustin Hibbits }
2600aeed3e9SJustin Hibbits /** @} */
261