xref: /freebsd/sys/dev/netmap/if_re_netmap.h (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2011-2014 Luigi Rizzo. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  *
30  * netmap support for: re
31  *
32  * For more details on netmap support please see ixgbe_netmap.h
33  */
34 
35 
36 #include <net/netmap.h>
37 #include <sys/selinfo.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>    /* vtophys ? */
40 #include <dev/netmap/netmap_kern.h>
41 
42 
43 /*
44  * Register/unregister. We are already under netmap lock.
45  */
46 static int
47 re_netmap_reg(struct netmap_adapter *na, int onoff)
48 {
49 	if_t ifp = na->ifp;
50 	struct rl_softc *adapter = if_getsoftc(ifp);
51 
52 	RL_LOCK(adapter);
53 	re_stop(adapter); /* also clears IFF_DRV_RUNNING */
54 	if (onoff) {
55 		nm_set_native_flags(na);
56 	} else {
57 		nm_clear_native_flags(na);
58 	}
59 	re_init_locked(adapter);	/* also enables intr */
60 	RL_UNLOCK(adapter);
61 	return (if_getdrvflags(ifp) & IFF_DRV_RUNNING ? 0 : 1);
62 }
63 
64 
65 /*
66  * Reconcile kernel and user view of the transmit ring.
67  */
68 static int
69 re_netmap_txsync(struct netmap_kring *kring, int flags)
70 {
71 	struct netmap_adapter *na = kring->na;
72 	if_t ifp = na->ifp;
73 	struct netmap_ring *ring = kring->ring;
74 	u_int nm_i;	/* index into the netmap ring */
75 	u_int nic_i;	/* index into the NIC ring */
76 	u_int n;
77 	u_int const lim = kring->nkr_num_slots - 1;
78 	u_int const head = kring->rhead;
79 
80 	/* device-specific */
81 	struct rl_softc *sc = if_getsoftc(ifp);
82 	struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
83 
84 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
85 	    sc->rl_ldata.rl_tx_list_map,
86 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); // XXX extra postwrite ?
87 
88 	/*
89 	 * First part: process new packets to send.
90 	 */
91 	nm_i = kring->nr_hwcur;
92 	if (nm_i != head) {	/* we have new packets to send */
93 		nic_i = sc->rl_ldata.rl_tx_prodidx;
94 		// XXX or netmap_idx_k2n(kring, nm_i);
95 
96 		for (n = 0; nm_i != head; n++) {
97 			struct netmap_slot *slot = &ring->slot[nm_i];
98 			u_int len = slot->len;
99 			uint64_t paddr;
100 			void *addr = PNMB(na, slot, &paddr);
101 
102 			/* device-specific */
103 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[nic_i];
104 			int cmd = slot->len | RL_TDESC_CMD_EOF |
105 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
106 
107 			NM_CHECK_ADDR_LEN(na, addr, len);
108 
109 			if (nic_i == lim)	/* mark end of ring */
110 				cmd |= RL_TDESC_CMD_EOR;
111 
112 			if (slot->flags & NS_BUF_CHANGED) {
113 				/* buffer has changed, reload map */
114 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
115 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
116 				netmap_reload_map(na, sc->rl_ldata.rl_tx_mtag,
117 					txd[nic_i].tx_dmamap, addr);
118 			}
119 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
120 
121 			/* Fill the slot in the NIC ring. */
122 			desc->rl_cmdstat = htole32(cmd);
123 
124 			/* make sure changes to the buffer are synced */
125 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
126 				txd[nic_i].tx_dmamap,
127 				BUS_DMASYNC_PREWRITE);
128 
129 			nm_i = nm_next(nm_i, lim);
130 			nic_i = nm_next(nic_i, lim);
131 		}
132 		sc->rl_ldata.rl_tx_prodidx = nic_i;
133 		kring->nr_hwcur = head;
134 
135 		/* synchronize the NIC ring */
136 		bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
137 			sc->rl_ldata.rl_tx_list_map,
138 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
139 
140 		/* start ? */
141 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
142 	}
143 
144 	/*
145 	 * Second part: reclaim buffers for completed transmissions.
146 	 */
147 	if (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
148 		nic_i = sc->rl_ldata.rl_tx_considx;
149 		for (n = 0; nic_i != sc->rl_ldata.rl_tx_prodidx;
150 		    n++, nic_i = RL_TX_DESC_NXT(sc, nic_i)) {
151 			uint32_t cmdstat =
152 				le32toh(sc->rl_ldata.rl_tx_list[nic_i].rl_cmdstat);
153 			if (cmdstat & RL_TDESC_STAT_OWN)
154 				break;
155 		}
156 		if (n > 0) {
157 			sc->rl_ldata.rl_tx_considx = nic_i;
158 			sc->rl_ldata.rl_tx_free += n;
159 			kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
160 		}
161 	}
162 
163 	return 0;
164 }
165 
166 
167 /*
168  * Reconcile kernel and user view of the receive ring.
169  */
170 static int
171 re_netmap_rxsync(struct netmap_kring *kring, int flags)
172 {
173 	struct netmap_adapter *na = kring->na;
174 	if_t ifp = na->ifp;
175 	struct netmap_ring *ring = kring->ring;
176 	u_int nm_i;	/* index into the netmap ring */
177 	u_int nic_i;	/* index into the NIC ring */
178 	u_int const lim = kring->nkr_num_slots - 1;
179 	u_int const head = kring->rhead;
180 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
181 
182 	/* device-specific */
183 	struct rl_softc *sc = if_getsoftc(ifp);
184 	struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
185 
186 	if (head > lim)
187 		return netmap_ring_reinit(kring);
188 
189 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
190 			sc->rl_ldata.rl_rx_list_map,
191 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
192 
193 	/*
194 	 * First part: import newly received packets.
195 	 *
196 	 * This device uses all the buffers in the ring, so we need
197 	 * another termination condition in addition to RL_RDESC_STAT_OWN
198 	 * cleared (all buffers could have it cleared). The easiest one
199 	 * is to stop right before nm_hwcur.
200 	 */
201 	if (netmap_no_pendintr || force_update) {
202 		uint32_t stop_i = nm_prev(kring->nr_hwcur, lim);
203 
204 		nic_i = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
205 		nm_i = netmap_idx_n2k(kring, nic_i);
206 
207 		while (nm_i != stop_i) {
208 			struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[nic_i];
209 			uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
210 			uint32_t total_len;
211 
212 			if ((rxstat & RL_RDESC_STAT_OWN) != 0)
213 				break;
214 			total_len = rxstat & sc->rl_rxlenmask;
215 			/* XXX subtract crc */
216 			total_len = (total_len < 4) ? 0 : total_len - 4;
217 			ring->slot[nm_i].len = total_len;
218 			ring->slot[nm_i].flags = 0;
219 			/*  sync was in re_newbuf() */
220 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
221 			    rxd[nic_i].rx_dmamap, BUS_DMASYNC_POSTREAD);
222 			// if_inc_counter(sc->rl_ifp, IFCOUNTER_IPACKETS, 1);
223 			nm_i = nm_next(nm_i, lim);
224 			nic_i = nm_next(nic_i, lim);
225 		}
226 		sc->rl_ldata.rl_rx_prodidx = nic_i;
227 		kring->nr_hwtail = nm_i;
228 		kring->nr_kflags &= ~NKR_PENDINTR;
229 	}
230 
231 	/*
232 	 * Second part: skip past packets that userspace has released.
233 	 */
234 	nm_i = kring->nr_hwcur;
235 	if (nm_i != head) {
236 		nic_i = netmap_idx_k2n(kring, nm_i);
237 		while (nm_i != head) {
238 			struct netmap_slot *slot = &ring->slot[nm_i];
239 			uint64_t paddr;
240 			void *addr = PNMB(na, slot, &paddr);
241 
242 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[nic_i];
243 			int cmd = NETMAP_BUF_SIZE(na) | RL_RDESC_CMD_OWN;
244 
245 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
246 				goto ring_reset;
247 
248 			if (nic_i == lim)	/* mark end of ring */
249 				cmd |= RL_RDESC_CMD_EOR;
250 
251 			if (slot->flags & NS_BUF_CHANGED) {
252 				/* buffer has changed, reload map */
253 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
254 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
255 				netmap_reload_map(na, sc->rl_ldata.rl_rx_mtag,
256 					rxd[nic_i].rx_dmamap, addr);
257 				slot->flags &= ~NS_BUF_CHANGED;
258 			}
259 			desc->rl_cmdstat = htole32(cmd);
260 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
261 			    rxd[nic_i].rx_dmamap,
262 			    BUS_DMASYNC_PREREAD);
263 			nm_i = nm_next(nm_i, lim);
264 			nic_i = nm_next(nic_i, lim);
265 		}
266 		kring->nr_hwcur = head;
267 
268 		bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
269 		    sc->rl_ldata.rl_rx_list_map,
270 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
271 	}
272 
273 	return 0;
274 
275 ring_reset:
276 	return netmap_ring_reinit(kring);
277 }
278 
279 
280 /*
281  * Additional routines to init the tx and rx rings.
282  * In other drivers we do that inline in the main code.
283  */
284 static void
285 re_netmap_tx_init(struct rl_softc *sc)
286 {
287 	struct rl_txdesc *txd;
288 	struct rl_desc *desc;
289 	int i, n;
290 	struct netmap_adapter *na = NA(sc->rl_ifp);
291 	struct netmap_slot *slot;
292 
293 	slot = netmap_reset(na, NR_TX, 0, 0);
294 	/* slot is NULL if we are not in native netmap mode */
295 	if (!slot)
296 		return;
297 	/* in netmap mode, overwrite addresses and maps */
298 	txd = sc->rl_ldata.rl_tx_desc;
299 	desc = sc->rl_ldata.rl_tx_list;
300 	n = sc->rl_ldata.rl_tx_desc_cnt;
301 
302 	/* l points in the netmap ring, i points in the NIC ring */
303 	for (i = 0; i < n; i++) {
304 		uint64_t paddr;
305 		int l = netmap_idx_n2k(na->tx_rings[0], i);
306 		void *addr = PNMB(na, slot + l, &paddr);
307 
308 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
309 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
310 		netmap_load_map(na, sc->rl_ldata.rl_tx_mtag,
311 			txd[i].tx_dmamap, addr);
312 	}
313 }
314 
315 static void
316 re_netmap_rx_init(struct rl_softc *sc)
317 {
318 	struct netmap_adapter *na = NA(sc->rl_ifp);
319 	struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0);
320 	struct rl_desc *desc = sc->rl_ldata.rl_rx_list;
321 	uint32_t cmdstat;
322 	uint32_t nic_i, max_avail;
323 	uint32_t const n = sc->rl_ldata.rl_rx_desc_cnt;
324 
325 	if (!slot)
326 		return;
327 	/*
328 	 * Do not release the slots owned by userspace,
329 	 * and also keep one empty.
330 	 */
331 	max_avail = n - 1 - nm_kr_rxspace(na->rx_rings[0]);
332 	for (nic_i = 0; nic_i < n; nic_i++) {
333 		void *addr;
334 		uint64_t paddr;
335 		uint32_t nm_i = netmap_idx_n2k(na->rx_rings[0], nic_i);
336 
337 		addr = PNMB(na, slot + nm_i, &paddr);
338 
339 		netmap_reload_map(na, sc->rl_ldata.rl_rx_mtag,
340 		    sc->rl_ldata.rl_rx_desc[nic_i].rx_dmamap, addr);
341 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
342 		    sc->rl_ldata.rl_rx_desc[nic_i].rx_dmamap, BUS_DMASYNC_PREREAD);
343 		desc[nic_i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
344 		desc[nic_i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
345 		cmdstat = NETMAP_BUF_SIZE(na);
346 		if (nic_i == n - 1) /* mark the end of ring */
347 			cmdstat |= RL_RDESC_CMD_EOR;
348 		if (nic_i < max_avail)
349 			cmdstat |= RL_RDESC_CMD_OWN;
350 		desc[nic_i].rl_cmdstat = htole32(cmdstat);
351 	}
352 }
353 
354 
355 static void
356 re_netmap_attach(struct rl_softc *sc)
357 {
358 	struct netmap_adapter na;
359 
360 	bzero(&na, sizeof(na));
361 
362 	na.ifp = sc->rl_ifp;
363 	na.na_flags = NAF_BDG_MAYSLEEP;
364 	na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
365 	na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
366 	na.nm_txsync = re_netmap_txsync;
367 	na.nm_rxsync = re_netmap_rxsync;
368 	na.nm_register = re_netmap_reg;
369 	na.num_tx_rings = na.num_rx_rings = 1;
370 	netmap_attach(&na);
371 }
372 
373 /* end of file */
374