if_igb_netmap.h revision 228881
1/*
2 * Copyright (C) 2011 Universita` di Pisa. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26/*
27 * $FreeBSD: head/sys/dev/netmap/if_igb_netmap.h 228881 2011-12-25 21:00:56Z luigi $
28 * $Id: if_igb_netmap.h 9802 2011-12-02 18:42:37Z luigi $
29 *
30 * netmap modifications for igb
31 * contribured by Ahmed Kooli
32 */
33
34#include <net/netmap.h>
35#include <sys/selinfo.h>
36#include <vm/vm.h>
37#include <vm/pmap.h>    /* vtophys ? */
38#include <dev/netmap/netmap_kern.h>
39
40static int	igb_netmap_reg(struct ifnet *, int onoff);
41static int	igb_netmap_txsync(void *, u_int, int);
42static int	igb_netmap_rxsync(void *, u_int, int);
43static void	igb_netmap_lock_wrapper(void *, int, u_int);
44
45
46static void
47igb_netmap_attach(struct adapter *adapter)
48{
49	struct netmap_adapter na;
50
51	bzero(&na, sizeof(na));
52
53	na.ifp = adapter->ifp;
54	na.separate_locks = 1;
55	na.num_tx_desc = adapter->num_tx_desc;
56	na.num_rx_desc = adapter->num_rx_desc;
57	na.nm_txsync = igb_netmap_txsync;
58	na.nm_rxsync = igb_netmap_rxsync;
59	na.nm_lock = igb_netmap_lock_wrapper;
60	na.nm_register = igb_netmap_reg;
61	na.buff_size = NETMAP_BUF_SIZE;
62	netmap_attach(&na, adapter->num_queues);
63}
64
65
66/*
67 * wrapper to export locks to the generic code
68 */
69static void
70igb_netmap_lock_wrapper(void *_a, int what, u_int queueid)
71{
72	struct adapter *adapter = _a;
73
74	ASSERT(queueid < adapter->num_queues);
75	switch (what) {
76	case NETMAP_CORE_LOCK:
77		IGB_CORE_LOCK(adapter);
78		break;
79	case NETMAP_CORE_UNLOCK:
80		IGB_CORE_UNLOCK(adapter);
81		break;
82	case NETMAP_TX_LOCK:
83		IGB_TX_LOCK(&adapter->tx_rings[queueid]);
84		break;
85	case NETMAP_TX_UNLOCK:
86		IGB_TX_UNLOCK(&adapter->tx_rings[queueid]);
87		break;
88	case NETMAP_RX_LOCK:
89		IGB_RX_LOCK(&adapter->rx_rings[queueid]);
90		break;
91	case NETMAP_RX_UNLOCK:
92		IGB_RX_UNLOCK(&adapter->rx_rings[queueid]);
93		break;
94	}
95}
96
97
98/*
99 * support for netmap register/unregisted. We are already under core lock.
100 * only called on the first init or the last unregister.
101 */
102static int
103igb_netmap_reg(struct ifnet *ifp, int onoff)
104{
105	struct adapter *adapter = ifp->if_softc;
106	struct netmap_adapter *na = NA(ifp);
107	int error = 0;
108
109	if (na == NULL)
110		return EINVAL;
111
112	igb_disable_intr(adapter);
113
114	/* Tell the stack that the interface is no longer active */
115	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
116
117	if (onoff) {
118		ifp->if_capenable |= IFCAP_NETMAP;
119
120		/* save if_transmit to restore it later */
121		na->if_transmit = ifp->if_transmit;
122		ifp->if_transmit = netmap_start;
123
124		igb_init_locked(adapter);
125		if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
126			error = ENOMEM;
127			goto fail;
128		}
129	} else {
130fail:
131		/* restore if_transmit */
132		ifp->if_transmit = na->if_transmit;
133		ifp->if_capenable &= ~IFCAP_NETMAP;
134		igb_init_locked(adapter);	/* also enables intr */
135	}
136	return (error);
137}
138
139
140/*
141 * Reconcile kernel and user view of the transmit ring.
142 */
143static int
144igb_netmap_txsync(void *a, u_int ring_nr, int do_lock)
145{
146	struct adapter *adapter = a;
147	struct tx_ring *txr = &adapter->tx_rings[ring_nr];
148	struct netmap_adapter *na = NA(adapter->ifp);
149	struct netmap_kring *kring = &na->tx_rings[ring_nr];
150	struct netmap_ring *ring = kring->ring;
151	int j, k, l, n = 0, lim = kring->nkr_num_slots - 1;
152
153	/* generate an interrupt approximately every half ring */
154	int report_frequency = kring->nkr_num_slots >> 1;
155
156	k = ring->cur;
157	if (k > lim)
158		return netmap_ring_reinit(kring);
159
160	if (do_lock)
161		IGB_TX_LOCK(txr);
162	bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
163	    BUS_DMASYNC_POSTREAD);
164
165	/* update avail to what the hardware knows */
166	ring->avail = kring->nr_hwavail;
167
168	j = kring->nr_hwcur; /* netmap ring index */
169	if (j != k) {	/* we have new packets to send */
170		u32 olinfo_status = 0;
171
172		l = j - kring->nkr_hwofs; /* NIC ring index */
173		if (l < 0)
174			l += lim + 1;
175		/* 82575 needs the queue index added */
176		if (adapter->hw.mac.type == e1000_82575)
177			olinfo_status |= txr->me << 4;
178
179		while (j != k) {
180			struct netmap_slot *slot = &ring->slot[j];
181			struct igb_tx_buffer *txbuf = &txr->tx_buffers[l];
182			union e1000_adv_tx_desc *curr =
183			    (union e1000_adv_tx_desc *)&txr->tx_base[l];
184			void *addr = NMB(slot);
185			int flags = ((slot->flags & NS_REPORT) ||
186				j == 0 || j == report_frequency) ?
187					E1000_ADVTXD_DCMD_RS : 0;
188			int len = slot->len;
189
190			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
191				if (do_lock)
192					IGB_TX_UNLOCK(txr);
193				return netmap_ring_reinit(kring);
194			}
195
196			slot->flags &= ~NS_REPORT;
197			// XXX do we need to set the address ?
198			curr->read.buffer_addr = htole64(vtophys(addr));
199			curr->read.olinfo_status =
200			    htole32(olinfo_status |
201				(len<< E1000_ADVTXD_PAYLEN_SHIFT));
202			curr->read.cmd_type_len =
203			    htole32(len | E1000_ADVTXD_DTYP_DATA |
204				    E1000_ADVTXD_DCMD_IFCS |
205				    E1000_ADVTXD_DCMD_DEXT |
206				    E1000_ADVTXD_DCMD_EOP | flags);
207			if (slot->flags & NS_BUF_CHANGED) {
208				/* buffer has changed, reload map */
209				netmap_reload_map(txr->txtag, txbuf->map,
210					addr, na->buff_size);
211				slot->flags &= ~NS_BUF_CHANGED;
212			}
213
214			bus_dmamap_sync(txr->txtag, txbuf->map,
215				BUS_DMASYNC_PREWRITE);
216			j = (j == lim) ? 0 : j + 1;
217			l = (l == lim) ? 0 : l + 1;
218			n++;
219		}
220		kring->nr_hwcur = k;
221
222		/* decrease avail by number of sent packets */
223		kring->nr_hwavail -= n;
224		ring->avail = kring->nr_hwavail;
225
226		/* Set the watchdog XXX ? */
227		txr->queue_status = IGB_QUEUE_WORKING;
228		txr->watchdog_time = ticks;
229
230		bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
231		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
232
233		E1000_WRITE_REG(&adapter->hw, E1000_TDT(txr->me), l);
234	}
235	if (n == 0 || kring->nr_hwavail < 1) {
236		int delta;
237
238		/* record completed transmission using TDH */
239		l = E1000_READ_REG(&adapter->hw, E1000_TDH(ring_nr));
240		if (l >= kring->nkr_num_slots) /* XXX can it happen ? */
241			l -= kring->nkr_num_slots;
242		delta = l - txr->next_to_clean;
243		if (delta) {
244			/* new tx were completed */
245			if (delta < 0)
246				delta += kring->nkr_num_slots;
247			txr->next_to_clean = l;
248			kring->nr_hwavail += delta;
249			ring->avail = kring->nr_hwavail;
250		}
251	}
252	if (do_lock)
253		IGB_TX_UNLOCK(txr);
254	return 0;
255}
256
257
258/*
259 * Reconcile kernel and user view of the receive ring.
260 */
261static int
262igb_netmap_rxsync(void *a, u_int ring_nr, int do_lock)
263{
264	struct adapter *adapter = a;
265	struct rx_ring *rxr = &adapter->rx_rings[ring_nr];
266	struct netmap_adapter *na = NA(adapter->ifp);
267	struct netmap_kring *kring = &na->rx_rings[ring_nr];
268	struct netmap_ring *ring = kring->ring;
269	int j, k, l, n, lim = kring->nkr_num_slots - 1;
270
271	k = ring->cur;
272	if (k > lim)
273		return netmap_ring_reinit(kring);
274
275	if (do_lock)
276		IGB_RX_LOCK(rxr);
277
278	/* Sync the ring. */
279	bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
280	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
281
282	l = rxr->next_to_check;
283	j = l + kring->nkr_hwofs;
284	if (j > lim)
285		j -= lim + 1;
286	for (n = 0; ; n++) {
287		union e1000_adv_rx_desc *curr = &rxr->rx_base[l];
288		uint32_t staterr = le32toh(curr->wb.upper.status_error);
289
290		if ((staterr & E1000_RXD_STAT_DD) == 0)
291			break;
292		ring->slot[j].len = le16toh(curr->wb.upper.length);
293
294		bus_dmamap_sync(rxr->ptag,
295			rxr->rx_buffers[l].pmap, BUS_DMASYNC_POSTREAD);
296		j = (j == lim) ? 0 : j + 1;
297		l = (l == lim) ? 0 : l + 1;
298	}
299	if (n) {
300		rxr->next_to_check = l;
301		kring->nr_hwavail += n;
302	}
303
304	/* skip past packets that userspace has already processed,
305	 * making them available for reception.
306	 * advance nr_hwcur and issue a bus_dmamap_sync on the
307	 * buffers so it is safe to write to them.
308	 * Also increase nr_hwavail
309	 */
310	j = kring->nr_hwcur;
311	l = kring->nr_hwcur - kring->nkr_hwofs;
312	if (l < 0)
313		l += lim + 1;
314	if (j != k) {	/* userspace has read some packets. */
315		n = 0;
316		while (j != k) {
317			struct netmap_slot *slot = ring->slot + j;
318			union e1000_adv_rx_desc *curr = &rxr->rx_base[l];
319			struct igb_rx_buf *rxbuf = rxr->rx_buffers + l;
320			void *addr = NMB(slot);
321
322			if (addr == netmap_buffer_base) { /* bad buf */
323				if (do_lock)
324					IGB_RX_UNLOCK(rxr);
325				return netmap_ring_reinit(kring);
326			}
327
328			curr->wb.upper.status_error = 0;
329			curr->read.pkt_addr = htole64(vtophys(addr));
330			if (slot->flags & NS_BUF_CHANGED) {
331				netmap_reload_map(rxr->ptag, rxbuf->pmap,
332					addr, na->buff_size);
333				slot->flags &= ~NS_BUF_CHANGED;
334			}
335
336			bus_dmamap_sync(rxr->ptag, rxbuf->pmap,
337				BUS_DMASYNC_PREREAD);
338
339			j = (j == lim) ? 0 : j + 1;
340			l = (l == lim) ? 0 : l + 1;
341			n++;
342		}
343		kring->nr_hwavail -= n;
344		kring->nr_hwcur = ring->cur;
345		bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
346			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
347		/* IMPORTANT: we must leave one free slot in the ring,
348		 * so move l back by one unit
349		 */
350		l = (l == 0) ? lim : l - 1;
351		E1000_WRITE_REG(&adapter->hw, E1000_RDT(rxr->me), l);
352	}
353	/* tell userspace that there are new packets */
354	ring->avail = kring->nr_hwavail ;
355	if (do_lock)
356		IGB_RX_UNLOCK(rxr);
357	return 0;
358}
359