xref: /freebsd/sys/dev/dpaa/if_dtsec_im.c (revision ea825d02749f382c3f7e17f28247f20a48733eab)
1 /*-
2  * Copyright (c) 2012 Semihalf.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/sockio.h>
41 
42 #include <net/ethernet.h>
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47 #include <net/if_arp.h>
48 
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 
52 #include "miibus_if.h"
53 
54 #include <contrib/ncsw/inc/integrations/dpaa_integration_ext.h>
55 #include <contrib/ncsw/inc/Peripherals/fm_mac_ext.h>
56 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
57 #include <contrib/ncsw/inc/xx_ext.h>
58 
59 #include "fman.h"
60 #include "if_dtsec.h"
61 #include "if_dtsec_im.h"
62 
63 
64 /**
65  * @group dTSEC FMan PORT routines.
66  * @{
67  */
68 static e_RxStoreResponse
69 dtsec_im_fm_port_rx_callback(t_Handle app, uint8_t *data, uint16_t length,
70     uint16_t status, uint8_t position, t_Handle buf_context)
71 {
72 	struct dtsec_softc *sc;
73 	struct mbuf *m;
74 
75 	/* TODO STATUS / Position checking */
76 	sc = app;
77 
78 	m = m_devget(data, length, 0, sc->sc_ifnet, NULL);
79 	if (m)
80 		(*sc->sc_ifnet->if_input)(sc->sc_ifnet, m);
81 
82 	XX_FreeSmart(data);
83 
84 	return (e_RX_STORE_RESPONSE_CONTINUE);
85 }
86 
87 static void
88 dtsec_im_fm_port_tx_conf_callback(t_Handle app, uint8_t *data, uint16_t status,
89     t_Handle buf_context)
90 {
91 
92 	/* TODO: Check status */
93 	XX_FreeSmart(data);
94 }
95 
96 static uint8_t *
97 dtsec_im_fm_port_rx_get_buf(t_Handle buffer_pool, t_Handle *buf_context_handle)
98 {
99 	struct dtsec_softc *sc;
100 	uint8_t *buffer;
101 
102 	sc = buffer_pool;
103 
104 	buffer = XX_MallocSmart(FM_PORT_BUFFER_SIZE, 0, sizeof(void *));
105 	if (!buffer)
106 		device_printf(sc->sc_dev, "couldn't allocate RX buffer.\n");
107 
108 	return (buffer);
109 }
110 
111 static t_Error
112 dtsec_im_fm_port_rx_put_buf(t_Handle buffer_pool, uint8_t *buffer,
113     t_Handle buf_context)
114 {
115 
116 	XX_FreeSmart(buffer);
117 	return (E_OK);
118 }
119 
120 int
121 dtsec_im_fm_port_rx_init(struct dtsec_softc *sc, int unit)
122 {
123 	t_FmPortParams params;
124 	t_BufferPoolInfo *pool_params;
125 	t_FmPortImRxTxParams *im_params;
126 	t_Error error;
127 
128 	memset(&params, 0, sizeof(params));
129 
130 	params.baseAddr = sc->sc_fm_base + sc->sc_port_rx_hw_id;
131 	params.h_Fm = sc->sc_fmh;
132 	params.portType = dtsec_fm_port_rx_type(sc->sc_eth_dev_type);
133 	params.portId = sc->sc_eth_id;
134 	params.independentModeEnable = TRUE;
135 	params.liodnBase = FM_PORT_LIODN_BASE;
136 	params.f_Exception = dtsec_fm_port_rx_exception_callback;
137 	params.h_App = sc;
138 
139 	im_params = &params.specificParams.imRxTxParams;
140 	im_params->h_FmMuram = sc->sc_muramh;
141 	im_params->liodnOffset = FM_PORT_LIODN_OFFSET;
142 	im_params->dataMemId = FM_PORT_MEM_ID;
143 	im_params->dataMemAttributes = FM_PORT_MEM_ATTR;
144 	im_params->f_RxStore = dtsec_im_fm_port_rx_callback;
145 
146 	pool_params = &params.specificParams.imRxTxParams.rxPoolParams;
147 	pool_params->h_BufferPool = sc;
148 	pool_params->f_GetBuf = dtsec_im_fm_port_rx_get_buf;
149 	pool_params->f_PutBuf = dtsec_im_fm_port_rx_put_buf;
150 	pool_params->bufferSize = FM_PORT_BUFFER_SIZE;
151 
152 	sc->sc_rxph = FM_PORT_Config(&params);
153 	if (sc->sc_rxph == NULL) {
154 		device_printf(sc->sc_dev, "couldn't configure FM Port RX.\n");
155 		return (ENXIO);
156 	}
157 
158 	error = FM_PORT_Init(sc->sc_rxph);
159 	if (error != E_OK) {
160 		device_printf(sc->sc_dev, "couldn't initialize FM Port RX.\n");
161 		FM_PORT_Free(sc->sc_rxph);
162 		return (ENXIO);
163 	}
164 
165 	if (bootverbose)
166 		device_printf(sc->sc_dev, "RX hw port 0x%02x initialized.\n",
167 		    sc->sc_port_rx_hw_id);
168 
169 	return (0);
170 }
171 
172 int
173 dtsec_im_fm_port_tx_init(struct dtsec_softc *sc, int unit)
174 {
175 	t_FmPortParams params;
176 	t_FmPortImRxTxParams *im_params;
177 	t_Error error;
178 
179 	memset(&params, 0, sizeof(params));
180 
181 	params.baseAddr = sc->sc_fm_base + sc->sc_port_tx_hw_id;
182 	params.h_Fm = sc->sc_fmh;
183 	params.portType = dtsec_fm_port_tx_type(sc->sc_eth_dev_type);
184 	params.portId = unit;
185 	params.independentModeEnable = TRUE;
186 	params.liodnBase = FM_PORT_LIODN_BASE;
187 	params.f_Exception = dtsec_fm_port_tx_exception_callback;
188 	params.h_App = sc;
189 
190 	im_params = &params.specificParams.imRxTxParams;
191 	im_params->h_FmMuram = sc->sc_muramh;
192 	im_params->liodnOffset = FM_PORT_LIODN_OFFSET;
193 	im_params->dataMemId = FM_PORT_MEM_ID;
194 	im_params->dataMemAttributes = FM_PORT_MEM_ATTR;
195 	im_params->f_TxConf = dtsec_im_fm_port_tx_conf_callback;
196 
197 	sc->sc_txph = FM_PORT_Config(&params);
198 	if (sc->sc_txph == NULL) {
199 		device_printf(sc->sc_dev, "couldn't configure FM Port TX.\n");
200 		return (ENXIO);
201 	}
202 
203 	error = FM_PORT_Init(sc->sc_txph);
204 	if (error != E_OK) {
205 		device_printf(sc->sc_dev, "couldn't initialize FM Port TX.\n");
206 		FM_PORT_Free(sc->sc_txph);
207 		return (ENXIO);
208 	}
209 
210 	if (bootverbose)
211 		device_printf(sc->sc_dev, "TX hw port 0x%02x initialized.\n",
212 		    sc->sc_port_tx_hw_id);
213 
214 	return (0);
215 }
216 /** @} */
217 
218 
219 /**
220  * @group dTSEC IFnet routines.
221  * @{
222  */
223 void
224 dtsec_im_if_start_locked(struct dtsec_softc *sc)
225 {
226 	uint8_t *buffer;
227 	uint16_t length;
228 	struct mbuf *m;
229 	int error;
230 
231 	DTSEC_LOCK_ASSERT(sc);
232 	/* TODO: IFF_DRV_OACTIVE */
233 
234 	if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) == 0)
235 		return;
236 
237 	if ((sc->sc_ifnet->if_drv_flags & IFF_DRV_RUNNING) != IFF_DRV_RUNNING)
238 		return;
239 
240 	while (!IFQ_DRV_IS_EMPTY(&sc->sc_ifnet->if_snd)) {
241 		IFQ_DRV_DEQUEUE(&sc->sc_ifnet->if_snd, m);
242 		if (m == NULL)
243 			break;
244 
245 		length = m_length(m, NULL);
246 		buffer = XX_MallocSmart(length, 0, sizeof(void *));
247 		if (!buffer) {
248 			m_freem(m);
249 			break;
250 		}
251 
252 		m_copydata(m, 0, length, buffer);
253 		m_freem(m);
254 
255 		error = FM_PORT_ImTx(sc->sc_txph, buffer, length, TRUE, buffer);
256 		if (error != E_OK) {
257 			/* TODO: Ring full */
258 			XX_FreeSmart(buffer);
259 			break;
260 		}
261 	}
262 }
263 /** @} */
264