xref: /freebsd/sys/dev/cxgbe/t4_netmap.c (revision d1bd689d10447480e88de0ef515cacaedb82794e)
1 /*-
2  * Copyright (c) 2014 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
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 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #ifdef DEV_NETMAP
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/eventhandler.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/module.h>
41 #include <sys/selinfo.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <machine/bus.h>
45 #include <net/ethernet.h>
46 #include <net/if.h>
47 #include <net/if_media.h>
48 #include <net/if_var.h>
49 #include <net/if_clone.h>
50 #include <net/if_types.h>
51 #include <net/netmap.h>
52 #include <dev/netmap/netmap_kern.h>
53 
54 #include "common/common.h"
55 #include "common/t4_regs.h"
56 #include "common/t4_regs_values.h"
57 
58 extern int fl_pad;	/* XXXNM */
59 extern int spg_len;	/* XXXNM */
60 extern int fl_pktshift;	/* XXXNM */
61 
62 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD, 0, "cxgbe netmap parameters");
63 
64 /*
65  * 0 = normal netmap rx
66  * 1 = black hole
67  * 2 = supermassive black hole (buffer packing enabled)
68  */
69 int black_hole = 0;
70 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_black_hole, CTLFLAG_RDTUN, &black_hole, 0,
71     "Sink incoming packets.");
72 
73 int rx_ndesc = 256;
74 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_rx_ndesc, CTLFLAG_RWTUN,
75     &rx_ndesc, 0, "# of rx descriptors after which the hw cidx is updated.");
76 
77 int holdoff_tmr_idx = 2;
78 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_holdoff_tmr_idx, CTLFLAG_RWTUN,
79     &holdoff_tmr_idx, 0, "Holdoff timer index for netmap rx queues.");
80 
81 /*
82  * Congestion drops.
83  * -1: no congestion feedback (not recommended).
84  *  0: backpressure the channel instead of dropping packets right away.
85  *  1: no backpressure, drop packets for the congested queue immediately.
86  */
87 static int nm_cong_drop = 1;
88 TUNABLE_INT("hw.cxgbe.nm_cong_drop", &nm_cong_drop);
89 
90 /* netmap ifnet routines */
91 static void cxgbe_nm_init(void *);
92 static int cxgbe_nm_ioctl(struct ifnet *, unsigned long, caddr_t);
93 static int cxgbe_nm_transmit(struct ifnet *, struct mbuf *);
94 static void cxgbe_nm_qflush(struct ifnet *);
95 
96 static int cxgbe_nm_init_synchronized(struct vi_info *);
97 static int cxgbe_nm_uninit_synchronized(struct vi_info *);
98 
99 /* T4 netmap VI (ncxgbe) interface */
100 static int ncxgbe_probe(device_t);
101 static int ncxgbe_attach(device_t);
102 static int ncxgbe_detach(device_t);
103 static device_method_t ncxgbe_methods[] = {
104 	DEVMETHOD(device_probe,		ncxgbe_probe),
105 	DEVMETHOD(device_attach,	ncxgbe_attach),
106 	DEVMETHOD(device_detach,	ncxgbe_detach),
107 	{ 0, 0 }
108 };
109 static driver_t ncxgbe_driver = {
110 	"ncxgbe",
111 	ncxgbe_methods,
112 	sizeof(struct vi_info)
113 };
114 
115 /* T5 netmap VI (ncxl) interface */
116 static driver_t ncxl_driver = {
117 	"ncxl",
118 	ncxgbe_methods,
119 	sizeof(struct vi_info)
120 };
121 
122 static void
123 cxgbe_nm_init(void *arg)
124 {
125 	struct vi_info *vi = arg;
126 	struct adapter *sc = vi->pi->adapter;
127 
128 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4nminit") != 0)
129 		return;
130 	cxgbe_nm_init_synchronized(vi);
131 	end_synchronized_op(sc, 0);
132 
133 	return;
134 }
135 
136 static int
137 cxgbe_nm_init_synchronized(struct vi_info *vi)
138 {
139 	struct adapter *sc = vi->pi->adapter;
140 	struct ifnet *ifp = vi->ifp;
141 	int rc = 0;
142 
143 	ASSERT_SYNCHRONIZED_OP(sc);
144 
145 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
146 		return (0);	/* already running */
147 
148 	if (!(sc->flags & FULL_INIT_DONE) &&
149 	    ((rc = adapter_full_init(sc)) != 0))
150 		return (rc);	/* error message displayed already */
151 
152 	if (!(vi->flags & VI_INIT_DONE) &&
153 	    ((rc = vi_full_init(vi)) != 0))
154 		return (rc);	/* error message displayed already */
155 
156 	rc = update_mac_settings(ifp, XGMAC_ALL);
157 	if (rc)
158 		return (rc);	/* error message displayed already */
159 
160 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
161 	callout_reset(&vi->tick, hz, vi_tick, vi);
162 
163 	return (rc);
164 }
165 
166 static int
167 cxgbe_nm_uninit_synchronized(struct vi_info *vi)
168 {
169 #ifdef INVARIANTS
170 	struct adapter *sc = vi->pi->adapter;
171 #endif
172 	struct ifnet *ifp = vi->ifp;
173 
174 	ASSERT_SYNCHRONIZED_OP(sc);
175 
176 	callout_stop(&vi->tick);
177 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
178 
179 	return (0);
180 }
181 
182 static int
183 cxgbe_nm_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
184 {
185 	int rc = 0, mtu, flags;
186 	struct vi_info *vi = ifp->if_softc;
187 	struct adapter *sc = vi->pi->adapter;
188 	struct ifreq *ifr = (struct ifreq *)data;
189 	uint32_t mask;
190 
191 	MPASS(vi->ifp == ifp);
192 
193 	switch (cmd) {
194 	case SIOCSIFMTU:
195 		mtu = ifr->ifr_mtu;
196 		if ((mtu < ETHERMIN) || (mtu > ETHERMTU_JUMBO))
197 			return (EINVAL);
198 
199 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4nmtu");
200 		if (rc)
201 			return (rc);
202 		ifp->if_mtu = mtu;
203 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
204 			rc = update_mac_settings(ifp, XGMAC_MTU);
205 		end_synchronized_op(sc, 0);
206 		break;
207 
208 	case SIOCSIFFLAGS:
209 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4nflg");
210 		if (rc)
211 			return (rc);
212 
213 		if (ifp->if_flags & IFF_UP) {
214 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
215 				flags = vi->if_flags;
216 				if ((ifp->if_flags ^ flags) &
217 				    (IFF_PROMISC | IFF_ALLMULTI)) {
218 					rc = update_mac_settings(ifp,
219 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
220 				}
221 			} else
222 				rc = cxgbe_nm_init_synchronized(vi);
223 			vi->if_flags = ifp->if_flags;
224 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
225 			rc = cxgbe_nm_uninit_synchronized(vi);
226 		end_synchronized_op(sc, 0);
227 		break;
228 
229 	case SIOCADDMULTI:
230 	case SIOCDELMULTI: /* these two are called with a mutex held :-( */
231 		rc = begin_synchronized_op(sc, vi, HOLD_LOCK, "t4nmulti");
232 		if (rc)
233 			return (rc);
234 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
235 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
236 		end_synchronized_op(sc, LOCK_HELD);
237 		break;
238 
239 	case SIOCSIFCAP:
240 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
241 		if (mask & IFCAP_TXCSUM) {
242 			ifp->if_capenable ^= IFCAP_TXCSUM;
243 			ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
244 		}
245 		if (mask & IFCAP_TXCSUM_IPV6) {
246 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
247 			ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
248 		}
249 		if (mask & IFCAP_RXCSUM)
250 			ifp->if_capenable ^= IFCAP_RXCSUM;
251 		if (mask & IFCAP_RXCSUM_IPV6)
252 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
253 		break;
254 
255 	case SIOCSIFMEDIA:
256 	case SIOCGIFMEDIA:
257 		ifmedia_ioctl(ifp, ifr, &vi->media, cmd);
258 		break;
259 
260 	default:
261 		rc = ether_ioctl(ifp, cmd, data);
262 	}
263 
264 	return (rc);
265 }
266 
267 static int
268 cxgbe_nm_transmit(struct ifnet *ifp, struct mbuf *m)
269 {
270 
271 	m_freem(m);
272 	return (0);
273 }
274 
275 static void
276 cxgbe_nm_qflush(struct ifnet *ifp)
277 {
278 
279 	return;
280 }
281 
282 static int
283 alloc_nm_rxq_hwq(struct vi_info *vi, struct sge_nm_rxq *nm_rxq, int cong)
284 {
285 	int rc, cntxt_id, i;
286 	__be32 v;
287 	struct adapter *sc = vi->pi->adapter;
288 	struct netmap_adapter *na = NA(vi->ifp);
289 	struct fw_iq_cmd c;
290 
291 	MPASS(na != NULL);
292 	MPASS(nm_rxq->iq_desc != NULL);
293 	MPASS(nm_rxq->fl_desc != NULL);
294 
295 	bzero(nm_rxq->iq_desc, vi->qsize_rxq * IQ_ESIZE);
296 	bzero(nm_rxq->fl_desc, na->num_rx_desc * EQ_ESIZE + spg_len);
297 
298 	bzero(&c, sizeof(c));
299 	c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
300 	    F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
301 	    V_FW_IQ_CMD_VFN(0));
302 	c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
303 	    FW_LEN16(c));
304 	if (vi->flags & INTR_RXQ) {
305 		KASSERT(nm_rxq->intr_idx < sc->intr_count,
306 		    ("%s: invalid direct intr_idx %d", __func__,
307 		    nm_rxq->intr_idx));
308 		v = V_FW_IQ_CMD_IQANDSTINDEX(nm_rxq->intr_idx);
309 	} else {
310 		CXGBE_UNIMPLEMENTED(__func__);	/* XXXNM: needs review */
311 		v = V_FW_IQ_CMD_IQANDSTINDEX(nm_rxq->intr_idx) |
312 		    F_FW_IQ_CMD_IQANDST;
313 	}
314 	c.type_to_iqandstindex = htobe32(v |
315 	    V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
316 	    V_FW_IQ_CMD_VIID(vi->viid) |
317 	    V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
318 	c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(vi->pi->tx_chan) |
319 	    F_FW_IQ_CMD_IQGTSMODE |
320 	    V_FW_IQ_CMD_IQINTCNTTHRESH(0) |
321 	    V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
322 	c.iqsize = htobe16(vi->qsize_rxq);
323 	c.iqaddr = htobe64(nm_rxq->iq_ba);
324 	if (cong >= 0) {
325 		c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN |
326 		    V_FW_IQ_CMD_FL0CNGCHMAP(cong) | F_FW_IQ_CMD_FL0CONGCIF |
327 		    F_FW_IQ_CMD_FL0CONGEN);
328 	}
329 	c.iqns_to_fl0congen |=
330 	    htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
331 		F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
332 		(fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
333 		(black_hole == 2 ? F_FW_IQ_CMD_FL0PACKEN : 0));
334 	c.fl0dcaen_to_fl0cidxfthresh =
335 	    htobe16(V_FW_IQ_CMD_FL0FBMIN(X_FETCHBURSTMIN_128B) |
336 		V_FW_IQ_CMD_FL0FBMAX(X_FETCHBURSTMAX_512B));
337 	c.fl0size = htobe16(na->num_rx_desc / 8 + spg_len / EQ_ESIZE);
338 	c.fl0addr = htobe64(nm_rxq->fl_ba);
339 
340 	rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
341 	if (rc != 0) {
342 		device_printf(sc->dev,
343 		    "failed to create netmap ingress queue: %d\n", rc);
344 		return (rc);
345 	}
346 
347 	nm_rxq->iq_cidx = 0;
348 	MPASS(nm_rxq->iq_sidx == vi->qsize_rxq - spg_len / IQ_ESIZE);
349 	nm_rxq->iq_gen = F_RSPD_GEN;
350 	nm_rxq->iq_cntxt_id = be16toh(c.iqid);
351 	nm_rxq->iq_abs_id = be16toh(c.physiqid);
352 	cntxt_id = nm_rxq->iq_cntxt_id - sc->sge.iq_start;
353 	if (cntxt_id >= sc->sge.niq) {
354 		panic ("%s: nm_rxq->iq_cntxt_id (%d) more than the max (%d)",
355 		    __func__, cntxt_id, sc->sge.niq - 1);
356 	}
357 	sc->sge.iqmap[cntxt_id] = (void *)nm_rxq;
358 
359 	nm_rxq->fl_cntxt_id = be16toh(c.fl0id);
360 	nm_rxq->fl_pidx = nm_rxq->fl_cidx = 0;
361 	MPASS(nm_rxq->fl_sidx == na->num_rx_desc);
362 	cntxt_id = nm_rxq->fl_cntxt_id - sc->sge.eq_start;
363 	if (cntxt_id >= sc->sge.neq) {
364 		panic("%s: nm_rxq->fl_cntxt_id (%d) more than the max (%d)",
365 		    __func__, cntxt_id, sc->sge.neq - 1);
366 	}
367 	sc->sge.eqmap[cntxt_id] = (void *)nm_rxq;
368 
369 	nm_rxq->fl_db_val = V_QID(nm_rxq->fl_cntxt_id) |
370 	    sc->chip_params->sge_fl_db;
371 
372 	if (is_t5(sc) && cong >= 0) {
373 		uint32_t param, val;
374 
375 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
376 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
377 		    V_FW_PARAMS_PARAM_YZ(nm_rxq->iq_cntxt_id);
378 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
379 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
380 		    V_FW_PARAMS_PARAM_YZ(nm_rxq->iq_cntxt_id);
381 		if (cong == 0)
382 			val = 1 << 19;
383 		else {
384 			val = 2 << 19;
385 			for (i = 0; i < 4; i++) {
386 				if (cong & (1 << i))
387 					val |= 1 << (i << 2);
388 			}
389 		}
390 
391 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
392 		if (rc != 0) {
393 			/* report error but carry on */
394 			device_printf(sc->dev,
395 			    "failed to set congestion manager context for "
396 			    "ingress queue %d: %d\n", nm_rxq->iq_cntxt_id, rc);
397 		}
398 	}
399 
400 	t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
401 	    V_INGRESSQID(nm_rxq->iq_cntxt_id) |
402 	    V_SEINTARM(V_QINTR_TIMER_IDX(holdoff_tmr_idx)));
403 
404 	return (rc);
405 }
406 
407 static int
408 free_nm_rxq_hwq(struct vi_info *vi, struct sge_nm_rxq *nm_rxq)
409 {
410 	struct adapter *sc = vi->pi->adapter;
411 	int rc;
412 
413 	rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
414 	    nm_rxq->iq_cntxt_id, nm_rxq->fl_cntxt_id, 0xffff);
415 	if (rc != 0)
416 		device_printf(sc->dev, "%s: failed for iq %d, fl %d: %d\n",
417 		    __func__, nm_rxq->iq_cntxt_id, nm_rxq->fl_cntxt_id, rc);
418 	return (rc);
419 }
420 
421 static int
422 alloc_nm_txq_hwq(struct vi_info *vi, struct sge_nm_txq *nm_txq)
423 {
424 	int rc, cntxt_id;
425 	size_t len;
426 	struct adapter *sc = vi->pi->adapter;
427 	struct netmap_adapter *na = NA(vi->ifp);
428 	struct fw_eq_eth_cmd c;
429 
430 	MPASS(na != NULL);
431 	MPASS(nm_txq->desc != NULL);
432 
433 	len = na->num_tx_desc * EQ_ESIZE + spg_len;
434 	bzero(nm_txq->desc, len);
435 
436 	bzero(&c, sizeof(c));
437 	c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
438 	    F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
439 	    V_FW_EQ_ETH_CMD_VFN(0));
440 	c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
441 	    F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
442 	c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
443 	    F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(vi->viid));
444 	c.fetchszm_to_iqid =
445 	    htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
446 		V_FW_EQ_ETH_CMD_PCIECHN(vi->pi->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
447 		V_FW_EQ_ETH_CMD_IQID(sc->sge.nm_rxq[nm_txq->iqidx].iq_cntxt_id));
448 	c.dcaen_to_eqsize = htobe32(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
449 		      V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
450 		      V_FW_EQ_ETH_CMD_EQSIZE(len / EQ_ESIZE));
451 	c.eqaddr = htobe64(nm_txq->ba);
452 
453 	rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
454 	if (rc != 0) {
455 		device_printf(vi->dev,
456 		    "failed to create netmap egress queue: %d\n", rc);
457 		return (rc);
458 	}
459 
460 	nm_txq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
461 	cntxt_id = nm_txq->cntxt_id - sc->sge.eq_start;
462 	if (cntxt_id >= sc->sge.neq)
463 	    panic("%s: nm_txq->cntxt_id (%d) more than the max (%d)", __func__,
464 		cntxt_id, sc->sge.neq - 1);
465 	sc->sge.eqmap[cntxt_id] = (void *)nm_txq;
466 
467 	nm_txq->pidx = nm_txq->cidx = 0;
468 	MPASS(nm_txq->sidx == na->num_tx_desc);
469 	nm_txq->equiqidx = nm_txq->equeqidx = nm_txq->dbidx = 0;
470 
471 	nm_txq->doorbells = sc->doorbells;
472 	if (isset(&nm_txq->doorbells, DOORBELL_UDB) ||
473 	    isset(&nm_txq->doorbells, DOORBELL_UDBWC) ||
474 	    isset(&nm_txq->doorbells, DOORBELL_WCWR)) {
475 		uint32_t s_qpp = sc->sge.eq_s_qpp;
476 		uint32_t mask = (1 << s_qpp) - 1;
477 		volatile uint8_t *udb;
478 
479 		udb = sc->udbs_base + UDBS_DB_OFFSET;
480 		udb += (nm_txq->cntxt_id >> s_qpp) << PAGE_SHIFT;
481 		nm_txq->udb_qid = nm_txq->cntxt_id & mask;
482 		if (nm_txq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
483 	    		clrbit(&nm_txq->doorbells, DOORBELL_WCWR);
484 		else {
485 			udb += nm_txq->udb_qid << UDBS_SEG_SHIFT;
486 			nm_txq->udb_qid = 0;
487 		}
488 		nm_txq->udb = (volatile void *)udb;
489 	}
490 
491 	return (rc);
492 }
493 
494 static int
495 free_nm_txq_hwq(struct vi_info *vi, struct sge_nm_txq *nm_txq)
496 {
497 	struct adapter *sc = vi->pi->adapter;
498 	int rc;
499 
500 	rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0, nm_txq->cntxt_id);
501 	if (rc != 0)
502 		device_printf(sc->dev, "%s: failed for eq %d: %d\n", __func__,
503 		    nm_txq->cntxt_id, rc);
504 	return (rc);
505 }
506 
507 static int
508 cxgbe_netmap_on(struct adapter *sc, struct vi_info *vi, struct ifnet *ifp,
509     struct netmap_adapter *na)
510 {
511 	struct netmap_slot *slot;
512 	struct sge_nm_rxq *nm_rxq;
513 	struct sge_nm_txq *nm_txq;
514 	int rc, i, j, hwidx;
515 	struct hw_buf_info *hwb;
516 	uint16_t *rss;
517 
518 	ASSERT_SYNCHRONIZED_OP(sc);
519 
520 	if ((vi->flags & VI_INIT_DONE) == 0 ||
521 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
522 		return (EAGAIN);
523 
524 	hwb = &sc->sge.hw_buf_info[0];
525 	for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) {
526 		if (hwb->size == NETMAP_BUF_SIZE(na))
527 			break;
528 	}
529 	if (i >= SGE_FLBUF_SIZES) {
530 		if_printf(ifp, "no hwidx for netmap buffer size %d.\n",
531 		    NETMAP_BUF_SIZE(na));
532 		return (ENXIO);
533 	}
534 	hwidx = i;
535 
536 	/* Must set caps before calling netmap_reset */
537 	nm_set_native_flags(na);
538 
539 	for_each_nm_rxq(vi, i, nm_rxq) {
540 		alloc_nm_rxq_hwq(vi, nm_rxq, tnl_cong(vi->pi, nm_cong_drop));
541 		nm_rxq->fl_hwidx = hwidx;
542 		slot = netmap_reset(na, NR_RX, i, 0);
543 		MPASS(slot != NULL);	/* XXXNM: error check, not assert */
544 
545 		/* We deal with 8 bufs at a time */
546 		MPASS((na->num_rx_desc & 7) == 0);
547 		MPASS(na->num_rx_desc == nm_rxq->fl_sidx);
548 		for (j = 0; j < nm_rxq->fl_sidx; j++) {
549 			uint64_t ba;
550 
551 			PNMB(na, &slot[j], &ba);
552 			MPASS(ba != 0);
553 			nm_rxq->fl_desc[j] = htobe64(ba | hwidx);
554 		}
555 		j = nm_rxq->fl_pidx = nm_rxq->fl_sidx - 8;
556 		MPASS((j & 7) == 0);
557 		j /= 8;	/* driver pidx to hardware pidx */
558 		wmb();
559 		t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
560 		    nm_rxq->fl_db_val | V_PIDX(j));
561 	}
562 
563 	for_each_nm_txq(vi, i, nm_txq) {
564 		alloc_nm_txq_hwq(vi, nm_txq);
565 		slot = netmap_reset(na, NR_TX, i, 0);
566 		MPASS(slot != NULL);	/* XXXNM: error check, not assert */
567 	}
568 
569 	rss = malloc(vi->rss_size * sizeof (*rss), M_CXGBE, M_ZERO |
570 	    M_WAITOK);
571 	for (i = 0; i < vi->rss_size;) {
572 		for_each_nm_rxq(vi, j, nm_rxq) {
573 			rss[i++] = nm_rxq->iq_abs_id;
574 			if (i == vi->rss_size)
575 				break;
576 		}
577 	}
578 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
579 	    rss, vi->rss_size);
580 	if (rc != 0)
581 		if_printf(ifp, "netmap rss_config failed: %d\n", rc);
582 	free(rss, M_CXGBE);
583 
584 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
585 	if (rc != 0)
586 		if_printf(ifp, "netmap enable_vi failed: %d\n", rc);
587 
588 	return (rc);
589 }
590 
591 static int
592 cxgbe_netmap_off(struct adapter *sc, struct vi_info *vi, struct ifnet *ifp,
593     struct netmap_adapter *na)
594 {
595 	int rc, i;
596 	struct sge_nm_txq *nm_txq;
597 	struct sge_nm_rxq *nm_rxq;
598 
599 	ASSERT_SYNCHRONIZED_OP(sc);
600 
601 	if ((vi->flags & VI_INIT_DONE) == 0)
602 		return (0);
603 
604 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
605 	if (rc != 0)
606 		if_printf(ifp, "netmap disable_vi failed: %d\n", rc);
607 	nm_clear_native_flags(na);
608 
609 	for_each_nm_txq(vi, i, nm_txq) {
610 		struct sge_qstat *spg = (void *)&nm_txq->desc[nm_txq->sidx];
611 
612 		/* Wait for hw pidx to catch up ... */
613 		while (be16toh(nm_txq->pidx) != spg->pidx)
614 			pause("nmpidx", 1);
615 
616 		/* ... and then for the cidx. */
617 		while (spg->pidx != spg->cidx)
618 			pause("nmcidx", 1);
619 
620 		free_nm_txq_hwq(vi, nm_txq);
621 	}
622 	for_each_nm_rxq(vi, i, nm_rxq) {
623 		free_nm_rxq_hwq(vi, nm_rxq);
624 	}
625 
626 	return (rc);
627 }
628 
629 static int
630 cxgbe_netmap_reg(struct netmap_adapter *na, int on)
631 {
632 	struct ifnet *ifp = na->ifp;
633 	struct vi_info *vi = ifp->if_softc;
634 	struct adapter *sc = vi->pi->adapter;
635 	int rc;
636 
637 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4nmreg");
638 	if (rc != 0)
639 		return (rc);
640 	if (on)
641 		rc = cxgbe_netmap_on(sc, vi, ifp, na);
642 	else
643 		rc = cxgbe_netmap_off(sc, vi, ifp, na);
644 	end_synchronized_op(sc, 0);
645 
646 	return (rc);
647 }
648 
649 /* How many packets can a single type1 WR carry in n descriptors */
650 static inline int
651 ndesc_to_npkt(const int n)
652 {
653 
654 	MPASS(n > 0 && n <= SGE_MAX_WR_NDESC);
655 
656 	return (n * 2 - 1);
657 }
658 #define MAX_NPKT_IN_TYPE1_WR	(ndesc_to_npkt(SGE_MAX_WR_NDESC))
659 
660 /* Space (in descriptors) needed for a type1 WR that carries n packets */
661 static inline int
662 npkt_to_ndesc(const int n)
663 {
664 
665 	MPASS(n > 0 && n <= MAX_NPKT_IN_TYPE1_WR);
666 
667 	return ((n + 2) / 2);
668 }
669 
670 /* Space (in 16B units) needed for a type1 WR that carries n packets */
671 static inline int
672 npkt_to_len16(const int n)
673 {
674 
675 	MPASS(n > 0 && n <= MAX_NPKT_IN_TYPE1_WR);
676 
677 	return (n * 2 + 1);
678 }
679 
680 #define NMIDXDIFF(q, idx) IDXDIFF((q)->pidx, (q)->idx, (q)->sidx)
681 
682 static void
683 ring_nm_txq_db(struct adapter *sc, struct sge_nm_txq *nm_txq)
684 {
685 	int n;
686 	u_int db = nm_txq->doorbells;
687 
688 	MPASS(nm_txq->pidx != nm_txq->dbidx);
689 
690 	n = NMIDXDIFF(nm_txq, dbidx);
691 	if (n > 1)
692 		clrbit(&db, DOORBELL_WCWR);
693 	wmb();
694 
695 	switch (ffs(db) - 1) {
696 	case DOORBELL_UDB:
697 		*nm_txq->udb = htole32(V_QID(nm_txq->udb_qid) | V_PIDX(n));
698 		break;
699 
700 	case DOORBELL_WCWR: {
701 		volatile uint64_t *dst, *src;
702 
703 		/*
704 		 * Queues whose 128B doorbell segment fits in the page do not
705 		 * use relative qid (udb_qid is always 0).  Only queues with
706 		 * doorbell segments can do WCWR.
707 		 */
708 		KASSERT(nm_txq->udb_qid == 0 && n == 1,
709 		    ("%s: inappropriate doorbell (0x%x, %d, %d) for nm_txq %p",
710 		    __func__, nm_txq->doorbells, n, nm_txq->pidx, nm_txq));
711 
712 		dst = (volatile void *)((uintptr_t)nm_txq->udb +
713 		    UDBS_WR_OFFSET - UDBS_DB_OFFSET);
714 		src = (void *)&nm_txq->desc[nm_txq->dbidx];
715 		while (src != (void *)&nm_txq->desc[nm_txq->dbidx + 1])
716 			*dst++ = *src++;
717 		wmb();
718 		break;
719 	}
720 
721 	case DOORBELL_UDBWC:
722 		*nm_txq->udb = htole32(V_QID(nm_txq->udb_qid) | V_PIDX(n));
723 		wmb();
724 		break;
725 
726 	case DOORBELL_KDB:
727 		t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
728 		    V_QID(nm_txq->cntxt_id) | V_PIDX(n));
729 		break;
730 	}
731 	nm_txq->dbidx = nm_txq->pidx;
732 }
733 
734 int lazy_tx_credit_flush = 1;
735 
736 /*
737  * Write work requests to send 'npkt' frames and ring the doorbell to send them
738  * on their way.  No need to check for wraparound.
739  */
740 static void
741 cxgbe_nm_tx(struct adapter *sc, struct sge_nm_txq *nm_txq,
742     struct netmap_kring *kring, int npkt, int npkt_remaining, int txcsum)
743 {
744 	struct netmap_ring *ring = kring->ring;
745 	struct netmap_slot *slot;
746 	const u_int lim = kring->nkr_num_slots - 1;
747 	struct fw_eth_tx_pkts_wr *wr = (void *)&nm_txq->desc[nm_txq->pidx];
748 	uint16_t len;
749 	uint64_t ba;
750 	struct cpl_tx_pkt_core *cpl;
751 	struct ulptx_sgl *usgl;
752 	int i, n;
753 
754 	while (npkt) {
755 		n = min(npkt, MAX_NPKT_IN_TYPE1_WR);
756 		len = 0;
757 
758 		wr = (void *)&nm_txq->desc[nm_txq->pidx];
759 		wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
760 		wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(npkt_to_len16(n)));
761 		wr->npkt = n;
762 		wr->r3 = 0;
763 		wr->type = 1;
764 		cpl = (void *)(wr + 1);
765 
766 		for (i = 0; i < n; i++) {
767 			slot = &ring->slot[kring->nr_hwcur];
768 			PNMB(kring->na, slot, &ba);
769 			MPASS(ba != 0);
770 
771 			cpl->ctrl0 = nm_txq->cpl_ctrl0;
772 			cpl->pack = 0;
773 			cpl->len = htobe16(slot->len);
774 			/*
775 			 * netmap(4) says "netmap does not use features such as
776 			 * checksum offloading, TCP segmentation offloading,
777 			 * encryption, VLAN encapsulation/decapsulation, etc."
778 			 *
779 			 * So the ncxl interfaces have tx hardware checksumming
780 			 * disabled by default.  But you can override netmap by
781 			 * enabling IFCAP_TXCSUM on the interface manully.
782 			 */
783 			cpl->ctrl1 = txcsum ? 0 :
784 			    htobe64(F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS);
785 
786 			usgl = (void *)(cpl + 1);
787 			usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
788 			    V_ULPTX_NSGE(1));
789 			usgl->len0 = htobe32(slot->len);
790 			usgl->addr0 = htobe64(ba);
791 
792 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
793 			cpl = (void *)(usgl + 1);
794 			MPASS(slot->len + len <= UINT16_MAX);
795 			len += slot->len;
796 			kring->nr_hwcur = nm_next(kring->nr_hwcur, lim);
797 		}
798 		wr->plen = htobe16(len);
799 
800 		npkt -= n;
801 		nm_txq->pidx += npkt_to_ndesc(n);
802 		MPASS(nm_txq->pidx <= nm_txq->sidx);
803 		if (__predict_false(nm_txq->pidx == nm_txq->sidx)) {
804 			/*
805 			 * This routine doesn't know how to write WRs that wrap
806 			 * around.  Make sure it wasn't asked to.
807 			 */
808 			MPASS(npkt == 0);
809 			nm_txq->pidx = 0;
810 		}
811 
812 		if (npkt == 0 && npkt_remaining == 0) {
813 			/* All done. */
814 			if (lazy_tx_credit_flush == 0) {
815 				wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ |
816 				    F_FW_WR_EQUIQ);
817 				nm_txq->equeqidx = nm_txq->pidx;
818 				nm_txq->equiqidx = nm_txq->pidx;
819 			}
820 			ring_nm_txq_db(sc, nm_txq);
821 			return;
822 		}
823 
824 		if (NMIDXDIFF(nm_txq, equiqidx) >= nm_txq->sidx / 2) {
825 			wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ |
826 			    F_FW_WR_EQUIQ);
827 			nm_txq->equeqidx = nm_txq->pidx;
828 			nm_txq->equiqidx = nm_txq->pidx;
829 		} else if (NMIDXDIFF(nm_txq, equeqidx) >= 64) {
830 			wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
831 			nm_txq->equeqidx = nm_txq->pidx;
832 		}
833 		if (NMIDXDIFF(nm_txq, dbidx) >= 2 * SGE_MAX_WR_NDESC)
834 			ring_nm_txq_db(sc, nm_txq);
835 	}
836 
837 	/* Will get called again. */
838 	MPASS(npkt_remaining);
839 }
840 
841 /* How many contiguous free descriptors starting at pidx */
842 static inline int
843 contiguous_ndesc_available(struct sge_nm_txq *nm_txq)
844 {
845 
846 	if (nm_txq->cidx > nm_txq->pidx)
847 		return (nm_txq->cidx - nm_txq->pidx - 1);
848 	else if (nm_txq->cidx > 0)
849 		return (nm_txq->sidx - nm_txq->pidx);
850 	else
851 		return (nm_txq->sidx - nm_txq->pidx - 1);
852 }
853 
854 static int
855 reclaim_nm_tx_desc(struct sge_nm_txq *nm_txq)
856 {
857 	struct sge_qstat *spg = (void *)&nm_txq->desc[nm_txq->sidx];
858 	uint16_t hw_cidx = spg->cidx;	/* snapshot */
859 	struct fw_eth_tx_pkts_wr *wr;
860 	int n = 0;
861 
862 	hw_cidx = be16toh(hw_cidx);
863 
864 	while (nm_txq->cidx != hw_cidx) {
865 		wr = (void *)&nm_txq->desc[nm_txq->cidx];
866 
867 		MPASS(wr->op_pkd == htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR)));
868 		MPASS(wr->type == 1);
869 		MPASS(wr->npkt > 0 && wr->npkt <= MAX_NPKT_IN_TYPE1_WR);
870 
871 		n += wr->npkt;
872 		nm_txq->cidx += npkt_to_ndesc(wr->npkt);
873 
874 		/*
875 		 * We never sent a WR that wrapped around so the credits coming
876 		 * back, WR by WR, should never cause the cidx to wrap around
877 		 * either.
878 		 */
879 		MPASS(nm_txq->cidx <= nm_txq->sidx);
880 		if (__predict_false(nm_txq->cidx == nm_txq->sidx))
881 			nm_txq->cidx = 0;
882 	}
883 
884 	return (n);
885 }
886 
887 static int
888 cxgbe_netmap_txsync(struct netmap_kring *kring, int flags)
889 {
890 	struct netmap_adapter *na = kring->na;
891 	struct ifnet *ifp = na->ifp;
892 	struct vi_info *vi = ifp->if_softc;
893 	struct adapter *sc = vi->pi->adapter;
894 	struct sge_nm_txq *nm_txq = &sc->sge.nm_txq[vi->first_txq + kring->ring_id];
895 	const u_int head = kring->rhead;
896 	u_int reclaimed = 0;
897 	int n, d, npkt_remaining, ndesc_remaining, txcsum;
898 
899 	/*
900 	 * Tx was at kring->nr_hwcur last time around and now we need to advance
901 	 * to kring->rhead.  Note that the driver's pidx moves independent of
902 	 * netmap's kring->nr_hwcur (pidx counts descriptors and the relation
903 	 * between descriptors and frames isn't 1:1).
904 	 */
905 
906 	npkt_remaining = head >= kring->nr_hwcur ? head - kring->nr_hwcur :
907 	    kring->nkr_num_slots - kring->nr_hwcur + head;
908 	txcsum = ifp->if_capenable & (IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6);
909 	while (npkt_remaining) {
910 		reclaimed += reclaim_nm_tx_desc(nm_txq);
911 		ndesc_remaining = contiguous_ndesc_available(nm_txq);
912 		/* Can't run out of descriptors with packets still remaining */
913 		MPASS(ndesc_remaining > 0);
914 
915 		/* # of desc needed to tx all remaining packets */
916 		d = (npkt_remaining / MAX_NPKT_IN_TYPE1_WR) * SGE_MAX_WR_NDESC;
917 		if (npkt_remaining % MAX_NPKT_IN_TYPE1_WR)
918 			d += npkt_to_ndesc(npkt_remaining % MAX_NPKT_IN_TYPE1_WR);
919 
920 		if (d <= ndesc_remaining)
921 			n = npkt_remaining;
922 		else {
923 			/* Can't send all, calculate how many can be sent */
924 			n = (ndesc_remaining / SGE_MAX_WR_NDESC) *
925 			    MAX_NPKT_IN_TYPE1_WR;
926 			if (ndesc_remaining % SGE_MAX_WR_NDESC)
927 				n += ndesc_to_npkt(ndesc_remaining % SGE_MAX_WR_NDESC);
928 		}
929 
930 		/* Send n packets and update nm_txq->pidx and kring->nr_hwcur */
931 		npkt_remaining -= n;
932 		cxgbe_nm_tx(sc, nm_txq, kring, n, npkt_remaining, txcsum);
933 	}
934 	MPASS(npkt_remaining == 0);
935 	MPASS(kring->nr_hwcur == head);
936 	MPASS(nm_txq->dbidx == nm_txq->pidx);
937 
938 	/*
939 	 * Second part: reclaim buffers for completed transmissions.
940 	 */
941 	if (reclaimed || flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
942 		reclaimed += reclaim_nm_tx_desc(nm_txq);
943 		kring->nr_hwtail += reclaimed;
944 		if (kring->nr_hwtail >= kring->nkr_num_slots)
945 			kring->nr_hwtail -= kring->nkr_num_slots;
946 	}
947 
948 	return (0);
949 }
950 
951 static int
952 cxgbe_netmap_rxsync(struct netmap_kring *kring, int flags)
953 {
954 	struct netmap_adapter *na = kring->na;
955 	struct netmap_ring *ring = kring->ring;
956 	struct ifnet *ifp = na->ifp;
957 	struct vi_info *vi = ifp->if_softc;
958 	struct adapter *sc = vi->pi->adapter;
959 	struct sge_nm_rxq *nm_rxq = &sc->sge.nm_rxq[vi->first_rxq + kring->ring_id];
960 	u_int const head = kring->rhead;
961 	u_int n;
962 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
963 
964 	if (black_hole)
965 		return (0);	/* No updates ever. */
966 
967 	if (netmap_no_pendintr || force_update) {
968 		kring->nr_hwtail = atomic_load_acq_32(&nm_rxq->fl_cidx);
969 		kring->nr_kflags &= ~NKR_PENDINTR;
970 	}
971 
972 	/* Userspace done with buffers from kring->nr_hwcur to head */
973 	n = head >= kring->nr_hwcur ? head - kring->nr_hwcur :
974 	    kring->nkr_num_slots - kring->nr_hwcur + head;
975 	n &= ~7U;
976 	if (n > 0) {
977 		u_int fl_pidx = nm_rxq->fl_pidx;
978 		struct netmap_slot *slot = &ring->slot[fl_pidx];
979 		uint64_t ba;
980 		int i, dbinc = 0, hwidx = nm_rxq->fl_hwidx;
981 
982 		/*
983 		 * We always deal with 8 buffers at a time.  We must have
984 		 * stopped at an 8B boundary (fl_pidx) last time around and we
985 		 * must have a multiple of 8B buffers to give to the freelist.
986 		 */
987 		MPASS((fl_pidx & 7) == 0);
988 		MPASS((n & 7) == 0);
989 
990 		IDXINCR(kring->nr_hwcur, n, kring->nkr_num_slots);
991 		IDXINCR(nm_rxq->fl_pidx, n, nm_rxq->fl_sidx);
992 
993 		while (n > 0) {
994 			for (i = 0; i < 8; i++, fl_pidx++, slot++) {
995 				PNMB(na, slot, &ba);
996 				MPASS(ba != 0);
997 				nm_rxq->fl_desc[fl_pidx] = htobe64(ba | hwidx);
998 				slot->flags &= ~NS_BUF_CHANGED;
999 				MPASS(fl_pidx <= nm_rxq->fl_sidx);
1000 			}
1001 			n -= 8;
1002 			if (fl_pidx == nm_rxq->fl_sidx) {
1003 				fl_pidx = 0;
1004 				slot = &ring->slot[0];
1005 			}
1006 			if (++dbinc == 8 && n >= 32) {
1007 				wmb();
1008 				t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
1009 				    nm_rxq->fl_db_val | V_PIDX(dbinc));
1010 				dbinc = 0;
1011 			}
1012 		}
1013 		MPASS(nm_rxq->fl_pidx == fl_pidx);
1014 
1015 		if (dbinc > 0) {
1016 			wmb();
1017 			t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
1018 			    nm_rxq->fl_db_val | V_PIDX(dbinc));
1019 		}
1020 	}
1021 
1022 	return (0);
1023 }
1024 
1025 static int
1026 ncxgbe_probe(device_t dev)
1027 {
1028 	char buf[128];
1029 	struct vi_info *vi = device_get_softc(dev);
1030 
1031 	snprintf(buf, sizeof(buf), "port %d netmap vi", vi->pi->port_id);
1032 	device_set_desc_copy(dev, buf);
1033 
1034 	return (BUS_PROBE_DEFAULT);
1035 }
1036 
1037 static int
1038 ncxgbe_attach(device_t dev)
1039 {
1040 	struct vi_info *vi;
1041 	struct port_info *pi;
1042 	struct adapter *sc;
1043 	struct netmap_adapter na;
1044 	struct ifnet *ifp;
1045 	int rc;
1046 
1047 	vi = device_get_softc(dev);
1048 	pi = vi->pi;
1049 	sc = pi->adapter;
1050 
1051 	/*
1052 	 * Allocate a virtual interface exclusively for netmap use.  Give it the
1053 	 * MAC address normally reserved for use by a TOE interface.  (The TOE
1054 	 * driver on FreeBSD doesn't use it).
1055 	 */
1056 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
1057 	    vi->hw_addr, &vi->rss_size, FW_VI_FUNC_OFLD, 0);
1058 	if (rc < 0) {
1059 		device_printf(dev, "unable to allocate netmap virtual "
1060 		    "interface for port %d: %d\n", pi->port_id, -rc);
1061 		return (-rc);
1062 	}
1063 	vi->viid = rc;
1064 	vi->xact_addr_filt = -1;
1065 	callout_init(&vi->tick, 1);
1066 
1067 	ifp = if_alloc(IFT_ETHER);
1068 	if (ifp == NULL) {
1069 		device_printf(dev, "Cannot allocate netmap ifnet\n");
1070 		return (ENOMEM);
1071 	}
1072 	vi->ifp = ifp;
1073 	ifp->if_softc = vi;
1074 
1075 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1076 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1077 
1078 	ifp->if_init = cxgbe_nm_init;
1079 	ifp->if_ioctl = cxgbe_nm_ioctl;
1080 	ifp->if_transmit = cxgbe_nm_transmit;
1081 	ifp->if_qflush = cxgbe_nm_qflush;
1082 	ifp->if_get_counter = cxgbe_get_counter;
1083 
1084 	/*
1085 	 * netmap(4) says "netmap does not use features such as checksum
1086 	 * offloading, TCP segmentation offloading, encryption, VLAN
1087 	 * encapsulation/decapsulation, etc."
1088 	 *
1089 	 * By default we comply with the statement above.  But we do declare the
1090 	 * ifnet capable of L3/L4 checksumming so that a user can override
1091 	 * netmap and have the hardware do the L3/L4 checksums.
1092 	 */
1093 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_JUMBO_MTU |
1094 	    IFCAP_HWCSUM_IPV6;
1095 	ifp->if_capenable = 0;
1096 	ifp->if_hwassist = 0;
1097 
1098 	/* vi->media has already been setup by the caller */
1099 
1100 	ether_ifattach(ifp, vi->hw_addr);
1101 
1102 	device_printf(dev, "%d txq, %d rxq (netmap)\n", vi->ntxq, vi->nrxq);
1103 
1104 	vi_sysctls(vi);
1105 
1106 	/*
1107 	 * Register with netmap in the kernel.
1108 	 */
1109 	bzero(&na, sizeof(na));
1110 
1111 	na.ifp = ifp;
1112 	na.na_flags = NAF_BDG_MAYSLEEP;
1113 
1114 	/* Netmap doesn't know about the space reserved for the status page. */
1115 	na.num_tx_desc = vi->qsize_txq - spg_len / EQ_ESIZE;
1116 
1117 	/*
1118 	 * The freelist's cidx/pidx drives netmap's rx cidx/pidx.  So
1119 	 * num_rx_desc is based on the number of buffers that can be held in the
1120 	 * freelist, and not the number of entries in the iq.  (These two are
1121 	 * not exactly the same due to the space taken up by the status page).
1122 	 */
1123 	na.num_rx_desc = (vi->qsize_rxq / 8) * 8;
1124 	na.nm_txsync = cxgbe_netmap_txsync;
1125 	na.nm_rxsync = cxgbe_netmap_rxsync;
1126 	na.nm_register = cxgbe_netmap_reg;
1127 	na.num_tx_rings = vi->ntxq;
1128 	na.num_rx_rings = vi->nrxq;
1129 	netmap_attach(&na);	/* This adds IFCAP_NETMAP to if_capabilities */
1130 
1131 	return (0);
1132 }
1133 
1134 static int
1135 ncxgbe_detach(device_t dev)
1136 {
1137 	struct vi_info *vi;
1138 	struct adapter *sc;
1139 
1140 	vi = device_get_softc(dev);
1141 	sc = vi->pi->adapter;
1142 
1143 	doom_vi(sc, vi);
1144 
1145 	netmap_detach(vi->ifp);
1146 	ether_ifdetach(vi->ifp);
1147 	cxgbe_nm_uninit_synchronized(vi);
1148 	callout_drain(&vi->tick);
1149 	vi_full_uninit(vi);
1150 	ifmedia_removeall(&vi->media);
1151 	if_free(vi->ifp);
1152 	vi->ifp = NULL;
1153 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
1154 
1155 	end_synchronized_op(sc, 0);
1156 
1157 	return (0);
1158 }
1159 
1160 static void
1161 handle_nm_fw6_msg(struct adapter *sc, struct ifnet *ifp,
1162     const struct cpl_fw6_msg *cpl)
1163 {
1164 	const struct cpl_sge_egr_update *egr;
1165 	uint32_t oq;
1166 	struct sge_nm_txq *nm_txq;
1167 
1168 	if (cpl->type != FW_TYPE_RSSCPL && cpl->type != FW6_TYPE_RSSCPL)
1169 		panic("%s: FW_TYPE 0x%x on nm_rxq.", __func__, cpl->type);
1170 
1171 	/* data[0] is RSS header */
1172 	egr = (const void *)&cpl->data[1];
1173 	oq = be32toh(egr->opcode_qid);
1174 	MPASS(G_CPL_OPCODE(oq) == CPL_SGE_EGR_UPDATE);
1175 	nm_txq = (void *)sc->sge.eqmap[G_EGR_QID(oq) - sc->sge.eq_start];
1176 
1177 	netmap_tx_irq(ifp, nm_txq->nid);
1178 }
1179 
1180 void
1181 t4_nm_intr(void *arg)
1182 {
1183 	struct sge_nm_rxq *nm_rxq = arg;
1184 	struct vi_info *vi = nm_rxq->vi;
1185 	struct adapter *sc = vi->pi->adapter;
1186 	struct ifnet *ifp = vi->ifp;
1187 	struct netmap_adapter *na = NA(ifp);
1188 	struct netmap_kring *kring = &na->rx_rings[nm_rxq->nid];
1189 	struct netmap_ring *ring = kring->ring;
1190 	struct iq_desc *d = &nm_rxq->iq_desc[nm_rxq->iq_cidx];
1191 	uint32_t lq;
1192 	u_int n = 0, work = 0;
1193 	uint8_t opcode;
1194 	uint32_t fl_cidx = atomic_load_acq_32(&nm_rxq->fl_cidx);
1195 	u_int fl_credits = fl_cidx & 7;
1196 
1197 	while ((d->rsp.u.type_gen & F_RSPD_GEN) == nm_rxq->iq_gen) {
1198 
1199 		rmb();
1200 
1201 		lq = be32toh(d->rsp.pldbuflen_qid);
1202 		opcode = d->rss.opcode;
1203 
1204 		switch (G_RSPD_TYPE(d->rsp.u.type_gen)) {
1205 		case X_RSPD_TYPE_FLBUF:
1206 			if (black_hole != 2) {
1207 				/* No buffer packing so new buf every time */
1208 				MPASS(lq & F_RSPD_NEWBUF);
1209 			}
1210 
1211 			/* fall through */
1212 
1213 		case X_RSPD_TYPE_CPL:
1214 			MPASS(opcode < NUM_CPL_CMDS);
1215 
1216 			switch (opcode) {
1217 			case CPL_FW4_MSG:
1218 			case CPL_FW6_MSG:
1219 				handle_nm_fw6_msg(sc, ifp,
1220 				    (const void *)&d->cpl[0]);
1221 				break;
1222 			case CPL_RX_PKT:
1223 				ring->slot[fl_cidx].len = G_RSPD_LEN(lq) - fl_pktshift;
1224 				ring->slot[fl_cidx].flags = kring->nkr_slot_flags;
1225 				fl_cidx += (lq & F_RSPD_NEWBUF) ? 1 : 0;
1226 				fl_credits += (lq & F_RSPD_NEWBUF) ? 1 : 0;
1227 				if (__predict_false(fl_cidx == nm_rxq->fl_sidx))
1228 					fl_cidx = 0;
1229 				break;
1230 			default:
1231 				panic("%s: unexpected opcode 0x%x on nm_rxq %p",
1232 				    __func__, opcode, nm_rxq);
1233 			}
1234 			break;
1235 
1236 		case X_RSPD_TYPE_INTR:
1237 			/* Not equipped to handle forwarded interrupts. */
1238 			panic("%s: netmap queue received interrupt for iq %u\n",
1239 			    __func__, lq);
1240 
1241 		default:
1242 			panic("%s: illegal response type %d on nm_rxq %p",
1243 			    __func__, G_RSPD_TYPE(d->rsp.u.type_gen), nm_rxq);
1244 		}
1245 
1246 		d++;
1247 		if (__predict_false(++nm_rxq->iq_cidx == nm_rxq->iq_sidx)) {
1248 			nm_rxq->iq_cidx = 0;
1249 			d = &nm_rxq->iq_desc[0];
1250 			nm_rxq->iq_gen ^= F_RSPD_GEN;
1251 		}
1252 
1253 		if (__predict_false(++n == rx_ndesc)) {
1254 			atomic_store_rel_32(&nm_rxq->fl_cidx, fl_cidx);
1255 			if (black_hole && fl_credits >= 8) {
1256 				fl_credits /= 8;
1257 				IDXINCR(nm_rxq->fl_pidx, fl_credits * 8,
1258 				    nm_rxq->fl_sidx);
1259 				t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
1260 				    nm_rxq->fl_db_val | V_PIDX(fl_credits));
1261 				fl_credits = fl_cidx & 7;
1262 			} else if (!black_hole) {
1263 				netmap_rx_irq(ifp, nm_rxq->nid, &work);
1264 				MPASS(work != 0);
1265 			}
1266 			t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS),
1267 			    V_CIDXINC(n) | V_INGRESSQID(nm_rxq->iq_cntxt_id) |
1268 			    V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1269 			n = 0;
1270 		}
1271 	}
1272 
1273 	atomic_store_rel_32(&nm_rxq->fl_cidx, fl_cidx);
1274 	if (black_hole) {
1275 		fl_credits /= 8;
1276 		IDXINCR(nm_rxq->fl_pidx, fl_credits * 8, nm_rxq->fl_sidx);
1277 		t4_write_reg(sc, MYPF_REG(A_SGE_PF_KDOORBELL),
1278 		    nm_rxq->fl_db_val | V_PIDX(fl_credits));
1279 	} else
1280 		netmap_rx_irq(ifp, nm_rxq->nid, &work);
1281 
1282 	t4_write_reg(sc, MYPF_REG(A_SGE_PF_GTS), V_CIDXINC(n) |
1283 	    V_INGRESSQID((u32)nm_rxq->iq_cntxt_id) |
1284 	    V_SEINTARM(V_QINTR_TIMER_IDX(holdoff_tmr_idx)));
1285 }
1286 
1287 static devclass_t ncxgbe_devclass, ncxl_devclass;
1288 
1289 DRIVER_MODULE(ncxgbe, cxgbe, ncxgbe_driver, ncxgbe_devclass, 0, 0);
1290 MODULE_VERSION(ncxgbe, 1);
1291 
1292 DRIVER_MODULE(ncxl, cxl, ncxl_driver, ncxl_devclass, 0, 0);
1293 MODULE_VERSION(ncxl, 1);
1294 #endif
1295