]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/t4_netmap.c
Merge ^/vendor/lvm-project/release-10.x up to its last change (upstream
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / t4_netmap.c
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
60 /*
61  * 0 = normal netmap rx
62  * 1 = black hole
63  * 2 = supermassive black hole (buffer packing enabled)
64  */
65 int black_hole = 0;
66 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_black_hole, CTLFLAG_RDTUN, &black_hole, 0,
67     "Sink incoming packets.");
68
69 int rx_ndesc = 256;
70 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_rx_ndesc, CTLFLAG_RWTUN,
71     &rx_ndesc, 0, "# of rx descriptors after which the hw cidx is updated.");
72
73 int rx_nframes = 64;
74 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_rx_nframes, CTLFLAG_RWTUN,
75     &rx_nframes, 0, "max # of frames received before waking up netmap rx.");
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 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_cong_drop, CTLFLAG_RDTUN,
89     &nm_cong_drop, 0,
90     "Congestion control for netmap rx queues (0 = backpressure, 1 = drop");
91
92 int starve_fl = 0;
93 SYSCTL_INT(_hw_cxgbe, OID_AUTO, starve_fl, CTLFLAG_RWTUN,
94     &starve_fl, 0, "Don't ring fl db for netmap rx queues.");
95
96 /*
97  * Try to process tx credits in bulk.  This may cause a delay in the return of
98  * tx credits and is suitable for bursty or non-stop tx only.
99  */
100 int lazy_tx_credit_flush = 1;
101 SYSCTL_INT(_hw_cxgbe, OID_AUTO, lazy_tx_credit_flush, CTLFLAG_RWTUN,
102     &lazy_tx_credit_flush, 0, "lazy credit flush for netmap tx queues.");
103
104 /*
105  * Split the netmap rx queues into two groups that populate separate halves of
106  * the RSS indirection table.  This allows filters with hashmask to steer to a
107  * particular group of queues.
108  */
109 static int nm_split_rss = 0;
110 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nm_split_rss, CTLFLAG_RWTUN,
111     &nm_split_rss, 0, "Split the netmap rx queues into two groups.");
112
113 static int
114 alloc_nm_rxq_hwq(struct vi_info *vi, struct sge_nm_rxq *nm_rxq, int cong)
115 {
116         int rc, cntxt_id, i;
117         __be32 v;
118         struct adapter *sc = vi->pi->adapter;
119         struct sge_params *sp = &sc->params.sge;
120         struct netmap_adapter *na = NA(vi->ifp);
121         struct fw_iq_cmd c;
122
123         MPASS(na != NULL);
124         MPASS(nm_rxq->iq_desc != NULL);
125         MPASS(nm_rxq->fl_desc != NULL);
126
127         bzero(nm_rxq->iq_desc, vi->qsize_rxq * IQ_ESIZE);
128         bzero(nm_rxq->fl_desc, na->num_rx_desc * EQ_ESIZE + sp->spg_len);
129
130         bzero(&c, sizeof(c));
131         c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
132             F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
133             V_FW_IQ_CMD_VFN(0));
134         c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
135             FW_LEN16(c));
136         MPASS(!forwarding_intr_to_fwq(sc));
137         KASSERT(nm_rxq->intr_idx < sc->intr_count,
138             ("%s: invalid direct intr_idx %d", __func__, nm_rxq->intr_idx));
139         v = V_FW_IQ_CMD_IQANDSTINDEX(nm_rxq->intr_idx);
140         c.type_to_iqandstindex = htobe32(v |
141             V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
142             V_FW_IQ_CMD_VIID(vi->viid) |
143             V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
144         c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(vi->pi->tx_chan) |
145             F_FW_IQ_CMD_IQGTSMODE |
146             V_FW_IQ_CMD_IQINTCNTTHRESH(0) |
147             V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
148         c.iqsize = htobe16(vi->qsize_rxq);
149         c.iqaddr = htobe64(nm_rxq->iq_ba);
150         if (cong >= 0) {
151                 c.iqns_to_fl0congen = htobe32(F_FW_IQ_CMD_IQFLINTCONGEN |
152                     V_FW_IQ_CMD_FL0CNGCHMAP(cong) | F_FW_IQ_CMD_FL0CONGCIF |
153                     F_FW_IQ_CMD_FL0CONGEN);
154         }
155         c.iqns_to_fl0congen |=
156             htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
157                 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
158                 (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
159                 (black_hole == 2 ? F_FW_IQ_CMD_FL0PACKEN : 0));
160         c.fl0dcaen_to_fl0cidxfthresh =
161             htobe16(V_FW_IQ_CMD_FL0FBMIN(chip_id(sc) <= CHELSIO_T5 ?
162                 X_FETCHBURSTMIN_128B : X_FETCHBURSTMIN_64B_T6) |
163                 V_FW_IQ_CMD_FL0FBMAX(chip_id(sc) <= CHELSIO_T5 ?
164                 X_FETCHBURSTMAX_512B : X_FETCHBURSTMAX_256B));
165         c.fl0size = htobe16(na->num_rx_desc / 8 + sp->spg_len / EQ_ESIZE);
166         c.fl0addr = htobe64(nm_rxq->fl_ba);
167
168         rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
169         if (rc != 0) {
170                 device_printf(sc->dev,
171                     "failed to create netmap ingress queue: %d\n", rc);
172                 return (rc);
173         }
174
175         nm_rxq->iq_cidx = 0;
176         MPASS(nm_rxq->iq_sidx == vi->qsize_rxq - sp->spg_len / IQ_ESIZE);
177         nm_rxq->iq_gen = F_RSPD_GEN;
178         nm_rxq->iq_cntxt_id = be16toh(c.iqid);
179         nm_rxq->iq_abs_id = be16toh(c.physiqid);
180         cntxt_id = nm_rxq->iq_cntxt_id - sc->sge.iq_start;
181         if (cntxt_id >= sc->sge.niq) {
182                 panic ("%s: nm_rxq->iq_cntxt_id (%d) more than the max (%d)",
183                     __func__, cntxt_id, sc->sge.niq - 1);
184         }
185         sc->sge.iqmap[cntxt_id] = (void *)nm_rxq;
186
187         nm_rxq->fl_cntxt_id = be16toh(c.fl0id);
188         nm_rxq->fl_pidx = nm_rxq->fl_cidx = 0;
189         MPASS(nm_rxq->fl_sidx == na->num_rx_desc);
190         cntxt_id = nm_rxq->fl_cntxt_id - sc->sge.eq_start;
191         if (cntxt_id >= sc->sge.neq) {
192                 panic("%s: nm_rxq->fl_cntxt_id (%d) more than the max (%d)",
193                     __func__, cntxt_id, sc->sge.neq - 1);
194         }
195         sc->sge.eqmap[cntxt_id] = (void *)nm_rxq;
196
197         nm_rxq->fl_db_val = V_QID(nm_rxq->fl_cntxt_id) |
198             sc->chip_params->sge_fl_db;
199
200         if (chip_id(sc) >= CHELSIO_T5 && cong >= 0) {
201                 uint32_t param, val;
202
203                 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
204                     V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
205                     V_FW_PARAMS_PARAM_YZ(nm_rxq->iq_cntxt_id);
206                 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
207                     V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
208                     V_FW_PARAMS_PARAM_YZ(nm_rxq->iq_cntxt_id);
209                 if (cong == 0)
210                         val = 1 << 19;
211                 else {
212                         val = 2 << 19;
213                         for (i = 0; i < 4; i++) {
214                                 if (cong & (1 << i))
215                                         val |= 1 << (i << 2);
216                         }
217                 }
218
219                 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
220                 if (rc != 0) {
221                         /* report error but carry on */
222                         device_printf(sc->dev,
223                             "failed to set congestion manager context for "
224                             "ingress queue %d: %d\n", nm_rxq->iq_cntxt_id, rc);
225                 }
226         }
227
228         t4_write_reg(sc, sc->sge_gts_reg,
229             V_INGRESSQID(nm_rxq->iq_cntxt_id) |
230             V_SEINTARM(V_QINTR_TIMER_IDX(holdoff_tmr_idx)));
231
232         return (rc);
233 }
234
235 static int
236 free_nm_rxq_hwq(struct vi_info *vi, struct sge_nm_rxq *nm_rxq)
237 {
238         struct adapter *sc = vi->pi->adapter;
239         int rc;
240
241         rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
242             nm_rxq->iq_cntxt_id, nm_rxq->fl_cntxt_id, 0xffff);
243         if (rc != 0)
244                 device_printf(sc->dev, "%s: failed for iq %d, fl %d: %d\n",
245                     __func__, nm_rxq->iq_cntxt_id, nm_rxq->fl_cntxt_id, rc);
246         nm_rxq->iq_cntxt_id = INVALID_NM_RXQ_CNTXT_ID;
247         return (rc);
248 }
249
250 static int
251 alloc_nm_txq_hwq(struct vi_info *vi, struct sge_nm_txq *nm_txq)
252 {
253         int rc, cntxt_id;
254         size_t len;
255         struct adapter *sc = vi->pi->adapter;
256         struct netmap_adapter *na = NA(vi->ifp);
257         struct fw_eq_eth_cmd c;
258
259         MPASS(na != NULL);
260         MPASS(nm_txq->desc != NULL);
261
262         len = na->num_tx_desc * EQ_ESIZE + sc->params.sge.spg_len;
263         bzero(nm_txq->desc, len);
264
265         bzero(&c, sizeof(c));
266         c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
267             F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
268             V_FW_EQ_ETH_CMD_VFN(0));
269         c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
270             F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
271         c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
272             F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(vi->viid));
273         c.fetchszm_to_iqid =
274             htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
275                 V_FW_EQ_ETH_CMD_PCIECHN(vi->pi->tx_chan) | F_FW_EQ_ETH_CMD_FETCHRO |
276                 V_FW_EQ_ETH_CMD_IQID(sc->sge.nm_rxq[nm_txq->iqidx].iq_cntxt_id));
277         c.dcaen_to_eqsize =
278             htobe32(V_FW_EQ_ETH_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
279                 X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
280                 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
281                 V_FW_EQ_ETH_CMD_EQSIZE(len / EQ_ESIZE));
282         c.eqaddr = htobe64(nm_txq->ba);
283
284         rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
285         if (rc != 0) {
286                 device_printf(vi->dev,
287                     "failed to create netmap egress queue: %d\n", rc);
288                 return (rc);
289         }
290
291         nm_txq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
292         cntxt_id = nm_txq->cntxt_id - sc->sge.eq_start;
293         if (cntxt_id >= sc->sge.neq)
294             panic("%s: nm_txq->cntxt_id (%d) more than the max (%d)", __func__,
295                 cntxt_id, sc->sge.neq - 1);
296         sc->sge.eqmap[cntxt_id] = (void *)nm_txq;
297
298         nm_txq->pidx = nm_txq->cidx = 0;
299         MPASS(nm_txq->sidx == na->num_tx_desc);
300         nm_txq->equiqidx = nm_txq->equeqidx = nm_txq->dbidx = 0;
301
302         nm_txq->doorbells = sc->doorbells;
303         if (isset(&nm_txq->doorbells, DOORBELL_UDB) ||
304             isset(&nm_txq->doorbells, DOORBELL_UDBWC) ||
305             isset(&nm_txq->doorbells, DOORBELL_WCWR)) {
306                 uint32_t s_qpp = sc->params.sge.eq_s_qpp;
307                 uint32_t mask = (1 << s_qpp) - 1;
308                 volatile uint8_t *udb;
309
310                 udb = sc->udbs_base + UDBS_DB_OFFSET;
311                 udb += (nm_txq->cntxt_id >> s_qpp) << PAGE_SHIFT;
312                 nm_txq->udb_qid = nm_txq->cntxt_id & mask;
313                 if (nm_txq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
314                         clrbit(&nm_txq->doorbells, DOORBELL_WCWR);
315                 else {
316                         udb += nm_txq->udb_qid << UDBS_SEG_SHIFT;
317                         nm_txq->udb_qid = 0;
318                 }
319                 nm_txq->udb = (volatile void *)udb;
320         }
321
322         return (rc);
323 }
324
325 static int
326 free_nm_txq_hwq(struct vi_info *vi, struct sge_nm_txq *nm_txq)
327 {
328         struct adapter *sc = vi->pi->adapter;
329         int rc;
330
331         rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0, nm_txq->cntxt_id);
332         if (rc != 0)
333                 device_printf(sc->dev, "%s: failed for eq %d: %d\n", __func__,
334                     nm_txq->cntxt_id, rc);
335         nm_txq->cntxt_id = INVALID_NM_TXQ_CNTXT_ID;
336         return (rc);
337 }
338
339 static int
340 cxgbe_netmap_on(struct adapter *sc, struct vi_info *vi, struct ifnet *ifp,
341     struct netmap_adapter *na)
342 {
343         struct netmap_slot *slot;
344         struct netmap_kring *kring;
345         struct sge_nm_rxq *nm_rxq;
346         struct sge_nm_txq *nm_txq;
347         int rc, i, j, hwidx, defq, nrssq;
348         struct hw_buf_info *hwb;
349
350         ASSERT_SYNCHRONIZED_OP(sc);
351
352         if ((vi->flags & VI_INIT_DONE) == 0 ||
353             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
354                 return (EAGAIN);
355
356         hwb = &sc->sge.hw_buf_info[0];
357         for (i = 0; i < SGE_FLBUF_SIZES; i++, hwb++) {
358                 if (hwb->size == NETMAP_BUF_SIZE(na))
359                         break;
360         }
361         if (i >= SGE_FLBUF_SIZES) {
362                 if_printf(ifp, "no hwidx for netmap buffer size %d.\n",
363                     NETMAP_BUF_SIZE(na));
364                 return (ENXIO);
365         }
366         hwidx = i;
367
368         /* Must set caps before calling netmap_reset */
369         nm_set_native_flags(na);
370
371         for_each_nm_rxq(vi, i, nm_rxq) {
372                 kring = na->rx_rings[nm_rxq->nid];
373                 if (!nm_kring_pending_on(kring) ||
374                     nm_rxq->iq_cntxt_id != INVALID_NM_RXQ_CNTXT_ID)
375                         continue;
376
377                 alloc_nm_rxq_hwq(vi, nm_rxq, tnl_cong(vi->pi, nm_cong_drop));
378                 nm_rxq->fl_hwidx = hwidx;
379                 slot = netmap_reset(na, NR_RX, i, 0);
380                 MPASS(slot != NULL);    /* XXXNM: error check, not assert */
381
382                 /* We deal with 8 bufs at a time */
383                 MPASS((na->num_rx_desc & 7) == 0);
384                 MPASS(na->num_rx_desc == nm_rxq->fl_sidx);
385                 for (j = 0; j < nm_rxq->fl_sidx; j++) {
386                         uint64_t ba;
387
388                         PNMB(na, &slot[j], &ba);
389                         MPASS(ba != 0);
390                         nm_rxq->fl_desc[j] = htobe64(ba | hwidx);
391                 }
392                 j = nm_rxq->fl_pidx = nm_rxq->fl_sidx - 8;
393                 MPASS((j & 7) == 0);
394                 j /= 8; /* driver pidx to hardware pidx */
395                 wmb();
396                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
397                     nm_rxq->fl_db_val | V_PIDX(j));
398
399                 (void) atomic_cmpset_int(&nm_rxq->nm_state, NM_OFF, NM_ON);
400         }
401
402         for_each_nm_txq(vi, i, nm_txq) {
403                 kring = na->tx_rings[nm_txq->nid];
404                 if (!nm_kring_pending_on(kring) ||
405                     nm_txq->cntxt_id != INVALID_NM_TXQ_CNTXT_ID)
406                         continue;
407
408                 alloc_nm_txq_hwq(vi, nm_txq);
409                 slot = netmap_reset(na, NR_TX, i, 0);
410                 MPASS(slot != NULL);    /* XXXNM: error check, not assert */
411         }
412
413         if (vi->nm_rss == NULL) {
414                 vi->nm_rss = malloc(vi->rss_size * sizeof(uint16_t), M_CXGBE,
415                     M_ZERO | M_WAITOK);
416         }
417
418         MPASS(vi->nnmrxq > 0);
419         if (nm_split_rss == 0 || vi->nnmrxq == 1) {
420                 for (i = 0; i < vi->rss_size;) {
421                         for_each_nm_rxq(vi, j, nm_rxq) {
422                                 vi->nm_rss[i++] = nm_rxq->iq_abs_id;
423                                 if (i == vi->rss_size)
424                                         break;
425                         }
426                 }
427                 defq = vi->nm_rss[0];
428         } else {
429                 /* We have multiple queues and we want to split the table. */
430                 MPASS(nm_split_rss != 0);
431                 MPASS(vi->nnmrxq > 1);
432
433                 nm_rxq = &sc->sge.nm_rxq[vi->first_nm_rxq];
434                 nrssq = vi->nnmrxq;
435                 if (vi->nnmrxq & 1) {
436                         /*
437                          * Odd number of queues. The first rxq is designated the
438                          * default queue, the rest are split evenly.
439                          */
440                         defq = nm_rxq->iq_abs_id;
441                         nm_rxq++;
442                         nrssq--;
443                 } else {
444                         /*
445                          * Even number of queues split into two halves.  The
446                          * first rxq in one of the halves is designated the
447                          * default queue.
448                          */
449 #if 1
450                         /* First rxq in the first half. */
451                         defq = nm_rxq->iq_abs_id;
452 #else
453                         /* First rxq in the second half. */
454                         defq = nm_rxq[vi->nnmrxq / 2].iq_abs_id;
455 #endif
456                 }
457
458                 i = 0;
459                 while (i < vi->rss_size / 2) {
460                         for (j = 0; j < nrssq / 2; j++) {
461                                 vi->nm_rss[i++] = nm_rxq[j].iq_abs_id;
462                                 if (i == vi->rss_size / 2)
463                                         break;
464                         }
465                 }
466                 while (i < vi->rss_size) {
467                         for (j = nrssq / 2; j < nrssq; j++) {
468                                 vi->nm_rss[i++] = nm_rxq[j].iq_abs_id;
469                                 if (i == vi->rss_size)
470                                         break;
471                         }
472                 }
473         }
474         rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
475             vi->nm_rss, vi->rss_size);
476         if (rc != 0)
477                 if_printf(ifp, "netmap rss_config failed: %d\n", rc);
478
479         rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, defq, 0, 0);
480         if (rc != 0)
481                 if_printf(ifp, "netmap rss hash/defaultq config failed: %d\n", rc);
482
483         return (rc);
484 }
485
486 static int
487 cxgbe_netmap_off(struct adapter *sc, struct vi_info *vi, struct ifnet *ifp,
488     struct netmap_adapter *na)
489 {
490         struct netmap_kring *kring;
491         int rc, i;
492         struct sge_nm_txq *nm_txq;
493         struct sge_nm_rxq *nm_rxq;
494
495         ASSERT_SYNCHRONIZED_OP(sc);
496
497         if (!nm_netmap_on(na))
498                 return (0);
499
500         if ((vi->flags & VI_INIT_DONE) == 0)
501                 return (0);
502
503         rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
504             vi->rss, vi->rss_size);
505         if (rc != 0)
506                 if_printf(ifp, "failed to restore RSS config: %d\n", rc);
507         rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0], 0, 0);
508         if (rc != 0)
509                 if_printf(ifp, "failed to restore RSS hash/defaultq: %d\n", rc);
510         nm_clear_native_flags(na);
511
512         for_each_nm_txq(vi, i, nm_txq) {
513                 struct sge_qstat *spg = (void *)&nm_txq->desc[nm_txq->sidx];
514
515                 kring = na->tx_rings[nm_txq->nid];
516                 if (!nm_kring_pending_off(kring) ||
517                     nm_txq->cntxt_id == INVALID_NM_TXQ_CNTXT_ID)
518                         continue;
519
520                 /* Wait for hw pidx to catch up ... */
521                 while (be16toh(nm_txq->pidx) != spg->pidx)
522                         pause("nmpidx", 1);
523
524                 /* ... and then for the cidx. */
525                 while (spg->pidx != spg->cidx)
526                         pause("nmcidx", 1);
527
528                 free_nm_txq_hwq(vi, nm_txq);
529         }
530         for_each_nm_rxq(vi, i, nm_rxq) {
531                 kring = na->rx_rings[nm_rxq->nid];
532                 if (!nm_kring_pending_off(kring) ||
533                     nm_rxq->iq_cntxt_id == INVALID_NM_RXQ_CNTXT_ID)
534                         continue;
535
536                 while (!atomic_cmpset_int(&nm_rxq->nm_state, NM_ON, NM_OFF))
537                         pause("nmst", 1);
538
539                 free_nm_rxq_hwq(vi, nm_rxq);
540         }
541
542         return (rc);
543 }
544
545 static int
546 cxgbe_netmap_reg(struct netmap_adapter *na, int on)
547 {
548         struct ifnet *ifp = na->ifp;
549         struct vi_info *vi = ifp->if_softc;
550         struct adapter *sc = vi->pi->adapter;
551         int rc;
552
553         rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4nmreg");
554         if (rc != 0)
555                 return (rc);
556         if (on)
557                 rc = cxgbe_netmap_on(sc, vi, ifp, na);
558         else
559                 rc = cxgbe_netmap_off(sc, vi, ifp, na);
560         end_synchronized_op(sc, 0);
561
562         return (rc);
563 }
564
565 /* How many packets can a single type1 WR carry in n descriptors */
566 static inline int
567 ndesc_to_npkt(const int n)
568 {
569
570         MPASS(n > 0 && n <= SGE_MAX_WR_NDESC);
571
572         return (n * 2 - 1);
573 }
574 #define MAX_NPKT_IN_TYPE1_WR    (ndesc_to_npkt(SGE_MAX_WR_NDESC))
575
576 /*
577  * Space (in descriptors) needed for a type1 WR (TX_PKTS or TX_PKTS2) that
578  * carries n packets
579  */
580 static inline int
581 npkt_to_ndesc(const int n)
582 {
583
584         MPASS(n > 0 && n <= MAX_NPKT_IN_TYPE1_WR);
585
586         return ((n + 2) / 2);
587 }
588
589 /*
590  * Space (in 16B units) needed for a type1 WR (TX_PKTS or TX_PKTS2) that
591  * carries n packets
592  */
593 static inline int
594 npkt_to_len16(const int n)
595 {
596
597         MPASS(n > 0 && n <= MAX_NPKT_IN_TYPE1_WR);
598
599         return (n * 2 + 1);
600 }
601
602 #define NMIDXDIFF(q, idx) IDXDIFF((q)->pidx, (q)->idx, (q)->sidx)
603
604 static void
605 ring_nm_txq_db(struct adapter *sc, struct sge_nm_txq *nm_txq)
606 {
607         int n;
608         u_int db = nm_txq->doorbells;
609
610         MPASS(nm_txq->pidx != nm_txq->dbidx);
611
612         n = NMIDXDIFF(nm_txq, dbidx);
613         if (n > 1)
614                 clrbit(&db, DOORBELL_WCWR);
615         wmb();
616
617         switch (ffs(db) - 1) {
618         case DOORBELL_UDB:
619                 *nm_txq->udb = htole32(V_QID(nm_txq->udb_qid) | V_PIDX(n));
620                 break;
621
622         case DOORBELL_WCWR: {
623                 volatile uint64_t *dst, *src;
624
625                 /*
626                  * Queues whose 128B doorbell segment fits in the page do not
627                  * use relative qid (udb_qid is always 0).  Only queues with
628                  * doorbell segments can do WCWR.
629                  */
630                 KASSERT(nm_txq->udb_qid == 0 && n == 1,
631                     ("%s: inappropriate doorbell (0x%x, %d, %d) for nm_txq %p",
632                     __func__, nm_txq->doorbells, n, nm_txq->pidx, nm_txq));
633
634                 dst = (volatile void *)((uintptr_t)nm_txq->udb +
635                     UDBS_WR_OFFSET - UDBS_DB_OFFSET);
636                 src = (void *)&nm_txq->desc[nm_txq->dbidx];
637                 while (src != (void *)&nm_txq->desc[nm_txq->dbidx + 1])
638                         *dst++ = *src++;
639                 wmb();
640                 break;
641         }
642
643         case DOORBELL_UDBWC:
644                 *nm_txq->udb = htole32(V_QID(nm_txq->udb_qid) | V_PIDX(n));
645                 wmb();
646                 break;
647
648         case DOORBELL_KDB:
649                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
650                     V_QID(nm_txq->cntxt_id) | V_PIDX(n));
651                 break;
652         }
653         nm_txq->dbidx = nm_txq->pidx;
654 }
655
656 /*
657  * Write work requests to send 'npkt' frames and ring the doorbell to send them
658  * on their way.  No need to check for wraparound.
659  */
660 static void
661 cxgbe_nm_tx(struct adapter *sc, struct sge_nm_txq *nm_txq,
662     struct netmap_kring *kring, int npkt, int npkt_remaining)
663 {
664         struct netmap_ring *ring = kring->ring;
665         struct netmap_slot *slot;
666         const u_int lim = kring->nkr_num_slots - 1;
667         struct fw_eth_tx_pkts_wr *wr = (void *)&nm_txq->desc[nm_txq->pidx];
668         uint16_t len;
669         uint64_t ba;
670         struct cpl_tx_pkt_core *cpl;
671         struct ulptx_sgl *usgl;
672         int i, n;
673
674         while (npkt) {
675                 n = min(npkt, MAX_NPKT_IN_TYPE1_WR);
676                 len = 0;
677
678                 wr = (void *)&nm_txq->desc[nm_txq->pidx];
679                 wr->op_pkd = nm_txq->op_pkd;
680                 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(npkt_to_len16(n)));
681                 wr->npkt = n;
682                 wr->r3 = 0;
683                 wr->type = 1;
684                 cpl = (void *)(wr + 1);
685
686                 for (i = 0; i < n; i++) {
687                         slot = &ring->slot[kring->nr_hwcur];
688                         PNMB(kring->na, slot, &ba);
689                         MPASS(ba != 0);
690
691                         cpl->ctrl0 = nm_txq->cpl_ctrl0;
692                         cpl->pack = 0;
693                         cpl->len = htobe16(slot->len);
694                         /*
695                          * netmap(4) says "netmap does not use features such as
696                          * checksum offloading, TCP segmentation offloading,
697                          * encryption, VLAN encapsulation/decapsulation, etc."
698                          */
699                         cpl->ctrl1 = htobe64(F_TXPKT_IPCSUM_DIS |
700                             F_TXPKT_L4CSUM_DIS);
701
702                         usgl = (void *)(cpl + 1);
703                         usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
704                             V_ULPTX_NSGE(1));
705                         usgl->len0 = htobe32(slot->len);
706                         usgl->addr0 = htobe64(ba);
707
708                         slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
709                         cpl = (void *)(usgl + 1);
710                         MPASS(slot->len + len <= UINT16_MAX);
711                         len += slot->len;
712                         kring->nr_hwcur = nm_next(kring->nr_hwcur, lim);
713                 }
714                 wr->plen = htobe16(len);
715
716                 npkt -= n;
717                 nm_txq->pidx += npkt_to_ndesc(n);
718                 MPASS(nm_txq->pidx <= nm_txq->sidx);
719                 if (__predict_false(nm_txq->pidx == nm_txq->sidx)) {
720                         /*
721                          * This routine doesn't know how to write WRs that wrap
722                          * around.  Make sure it wasn't asked to.
723                          */
724                         MPASS(npkt == 0);
725                         nm_txq->pidx = 0;
726                 }
727
728                 if (npkt == 0 && npkt_remaining == 0) {
729                         /* All done. */
730                         if (lazy_tx_credit_flush == 0) {
731                                 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ |
732                                     F_FW_WR_EQUIQ);
733                                 nm_txq->equeqidx = nm_txq->pidx;
734                                 nm_txq->equiqidx = nm_txq->pidx;
735                         }
736                         ring_nm_txq_db(sc, nm_txq);
737                         return;
738                 }
739
740                 if (NMIDXDIFF(nm_txq, equiqidx) >= nm_txq->sidx / 2) {
741                         wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ |
742                             F_FW_WR_EQUIQ);
743                         nm_txq->equeqidx = nm_txq->pidx;
744                         nm_txq->equiqidx = nm_txq->pidx;
745                 } else if (NMIDXDIFF(nm_txq, equeqidx) >= 64) {
746                         wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
747                         nm_txq->equeqidx = nm_txq->pidx;
748                 }
749                 if (NMIDXDIFF(nm_txq, dbidx) >= 2 * SGE_MAX_WR_NDESC)
750                         ring_nm_txq_db(sc, nm_txq);
751         }
752
753         /* Will get called again. */
754         MPASS(npkt_remaining);
755 }
756
757 /* How many contiguous free descriptors starting at pidx */
758 static inline int
759 contiguous_ndesc_available(struct sge_nm_txq *nm_txq)
760 {
761
762         if (nm_txq->cidx > nm_txq->pidx)
763                 return (nm_txq->cidx - nm_txq->pidx - 1);
764         else if (nm_txq->cidx > 0)
765                 return (nm_txq->sidx - nm_txq->pidx);
766         else
767                 return (nm_txq->sidx - nm_txq->pidx - 1);
768 }
769
770 static int
771 reclaim_nm_tx_desc(struct sge_nm_txq *nm_txq)
772 {
773         struct sge_qstat *spg = (void *)&nm_txq->desc[nm_txq->sidx];
774         uint16_t hw_cidx = spg->cidx;   /* snapshot */
775         struct fw_eth_tx_pkts_wr *wr;
776         int n = 0;
777
778         hw_cidx = be16toh(hw_cidx);
779
780         while (nm_txq->cidx != hw_cidx) {
781                 wr = (void *)&nm_txq->desc[nm_txq->cidx];
782
783                 MPASS(wr->op_pkd == htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR)) ||
784                     wr->op_pkd == htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS2_WR)));
785                 MPASS(wr->type == 1);
786                 MPASS(wr->npkt > 0 && wr->npkt <= MAX_NPKT_IN_TYPE1_WR);
787
788                 n += wr->npkt;
789                 nm_txq->cidx += npkt_to_ndesc(wr->npkt);
790
791                 /*
792                  * We never sent a WR that wrapped around so the credits coming
793                  * back, WR by WR, should never cause the cidx to wrap around
794                  * either.
795                  */
796                 MPASS(nm_txq->cidx <= nm_txq->sidx);
797                 if (__predict_false(nm_txq->cidx == nm_txq->sidx))
798                         nm_txq->cidx = 0;
799         }
800
801         return (n);
802 }
803
804 static int
805 cxgbe_netmap_txsync(struct netmap_kring *kring, int flags)
806 {
807         struct netmap_adapter *na = kring->na;
808         struct ifnet *ifp = na->ifp;
809         struct vi_info *vi = ifp->if_softc;
810         struct adapter *sc = vi->pi->adapter;
811         struct sge_nm_txq *nm_txq = &sc->sge.nm_txq[vi->first_nm_txq + kring->ring_id];
812         const u_int head = kring->rhead;
813         u_int reclaimed = 0;
814         int n, d, npkt_remaining, ndesc_remaining;
815
816         /*
817          * Tx was at kring->nr_hwcur last time around and now we need to advance
818          * to kring->rhead.  Note that the driver's pidx moves independent of
819          * netmap's kring->nr_hwcur (pidx counts descriptors and the relation
820          * between descriptors and frames isn't 1:1).
821          */
822
823         npkt_remaining = head >= kring->nr_hwcur ? head - kring->nr_hwcur :
824             kring->nkr_num_slots - kring->nr_hwcur + head;
825         while (npkt_remaining) {
826                 reclaimed += reclaim_nm_tx_desc(nm_txq);
827                 ndesc_remaining = contiguous_ndesc_available(nm_txq);
828                 /* Can't run out of descriptors with packets still remaining */
829                 MPASS(ndesc_remaining > 0);
830
831                 /* # of desc needed to tx all remaining packets */
832                 d = (npkt_remaining / MAX_NPKT_IN_TYPE1_WR) * SGE_MAX_WR_NDESC;
833                 if (npkt_remaining % MAX_NPKT_IN_TYPE1_WR)
834                         d += npkt_to_ndesc(npkt_remaining % MAX_NPKT_IN_TYPE1_WR);
835
836                 if (d <= ndesc_remaining)
837                         n = npkt_remaining;
838                 else {
839                         /* Can't send all, calculate how many can be sent */
840                         n = (ndesc_remaining / SGE_MAX_WR_NDESC) *
841                             MAX_NPKT_IN_TYPE1_WR;
842                         if (ndesc_remaining % SGE_MAX_WR_NDESC)
843                                 n += ndesc_to_npkt(ndesc_remaining % SGE_MAX_WR_NDESC);
844                 }
845
846                 /* Send n packets and update nm_txq->pidx and kring->nr_hwcur */
847                 npkt_remaining -= n;
848                 cxgbe_nm_tx(sc, nm_txq, kring, n, npkt_remaining);
849         }
850         MPASS(npkt_remaining == 0);
851         MPASS(kring->nr_hwcur == head);
852         MPASS(nm_txq->dbidx == nm_txq->pidx);
853
854         /*
855          * Second part: reclaim buffers for completed transmissions.
856          */
857         if (reclaimed || flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
858                 reclaimed += reclaim_nm_tx_desc(nm_txq);
859                 kring->nr_hwtail += reclaimed;
860                 if (kring->nr_hwtail >= kring->nkr_num_slots)
861                         kring->nr_hwtail -= kring->nkr_num_slots;
862         }
863
864         return (0);
865 }
866
867 static int
868 cxgbe_netmap_rxsync(struct netmap_kring *kring, int flags)
869 {
870         struct netmap_adapter *na = kring->na;
871         struct netmap_ring *ring = kring->ring;
872         struct ifnet *ifp = na->ifp;
873         struct vi_info *vi = ifp->if_softc;
874         struct adapter *sc = vi->pi->adapter;
875         struct sge_nm_rxq *nm_rxq = &sc->sge.nm_rxq[vi->first_nm_rxq + kring->ring_id];
876         u_int const head = kring->rhead;
877         u_int n;
878         int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
879
880         if (black_hole)
881                 return (0);     /* No updates ever. */
882
883         if (netmap_no_pendintr || force_update) {
884                 kring->nr_hwtail = atomic_load_acq_32(&nm_rxq->fl_cidx);
885                 kring->nr_kflags &= ~NKR_PENDINTR;
886         }
887
888         if (nm_rxq->fl_db_saved > 0 && starve_fl == 0) {
889                 wmb();
890                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
891                     nm_rxq->fl_db_val | V_PIDX(nm_rxq->fl_db_saved));
892                 nm_rxq->fl_db_saved = 0;
893         }
894
895         /* Userspace done with buffers from kring->nr_hwcur to head */
896         n = head >= kring->nr_hwcur ? head - kring->nr_hwcur :
897             kring->nkr_num_slots - kring->nr_hwcur + head;
898         n &= ~7U;
899         if (n > 0) {
900                 u_int fl_pidx = nm_rxq->fl_pidx;
901                 struct netmap_slot *slot = &ring->slot[fl_pidx];
902                 uint64_t ba;
903                 int i, dbinc = 0, hwidx = nm_rxq->fl_hwidx;
904
905                 /*
906                  * We always deal with 8 buffers at a time.  We must have
907                  * stopped at an 8B boundary (fl_pidx) last time around and we
908                  * must have a multiple of 8B buffers to give to the freelist.
909                  */
910                 MPASS((fl_pidx & 7) == 0);
911                 MPASS((n & 7) == 0);
912
913                 IDXINCR(kring->nr_hwcur, n, kring->nkr_num_slots);
914                 IDXINCR(nm_rxq->fl_pidx, n, nm_rxq->fl_sidx);
915
916                 while (n > 0) {
917                         for (i = 0; i < 8; i++, fl_pidx++, slot++) {
918                                 PNMB(na, slot, &ba);
919                                 MPASS(ba != 0);
920                                 nm_rxq->fl_desc[fl_pidx] = htobe64(ba | hwidx);
921                                 slot->flags &= ~NS_BUF_CHANGED;
922                                 MPASS(fl_pidx <= nm_rxq->fl_sidx);
923                         }
924                         n -= 8;
925                         if (fl_pidx == nm_rxq->fl_sidx) {
926                                 fl_pidx = 0;
927                                 slot = &ring->slot[0];
928                         }
929                         if (++dbinc == 8 && n >= 32) {
930                                 wmb();
931                                 if (starve_fl)
932                                         nm_rxq->fl_db_saved += dbinc;
933                                 else {
934                                         t4_write_reg(sc, sc->sge_kdoorbell_reg,
935                                             nm_rxq->fl_db_val | V_PIDX(dbinc));
936                                 }
937                                 dbinc = 0;
938                         }
939                 }
940                 MPASS(nm_rxq->fl_pidx == fl_pidx);
941
942                 if (dbinc > 0) {
943                         wmb();
944                         if (starve_fl)
945                                 nm_rxq->fl_db_saved += dbinc;
946                         else {
947                                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
948                                     nm_rxq->fl_db_val | V_PIDX(dbinc));
949                         }
950                 }
951         }
952
953         return (0);
954 }
955
956 void
957 cxgbe_nm_attach(struct vi_info *vi)
958 {
959         struct port_info *pi;
960         struct adapter *sc;
961         struct netmap_adapter na;
962
963         MPASS(vi->nnmrxq > 0);
964         MPASS(vi->ifp != NULL);
965
966         pi = vi->pi;
967         sc = pi->adapter;
968
969         bzero(&na, sizeof(na));
970
971         na.ifp = vi->ifp;
972         na.na_flags = NAF_BDG_MAYSLEEP;
973
974         /* Netmap doesn't know about the space reserved for the status page. */
975         na.num_tx_desc = vi->qsize_txq - sc->params.sge.spg_len / EQ_ESIZE;
976
977         /*
978          * The freelist's cidx/pidx drives netmap's rx cidx/pidx.  So
979          * num_rx_desc is based on the number of buffers that can be held in the
980          * freelist, and not the number of entries in the iq.  (These two are
981          * not exactly the same due to the space taken up by the status page).
982          */
983         na.num_rx_desc = rounddown(vi->qsize_rxq, 8);
984         na.nm_txsync = cxgbe_netmap_txsync;
985         na.nm_rxsync = cxgbe_netmap_rxsync;
986         na.nm_register = cxgbe_netmap_reg;
987         na.num_tx_rings = vi->nnmtxq;
988         na.num_rx_rings = vi->nnmrxq;
989         netmap_attach(&na);     /* This adds IFCAP_NETMAP to if_capabilities */
990 }
991
992 void
993 cxgbe_nm_detach(struct vi_info *vi)
994 {
995
996         MPASS(vi->nnmrxq > 0);
997         MPASS(vi->ifp != NULL);
998
999         netmap_detach(vi->ifp);
1000 }
1001
1002 static inline const void *
1003 unwrap_nm_fw6_msg(const struct cpl_fw6_msg *cpl)
1004 {
1005
1006         MPASS(cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL);
1007
1008         /* data[0] is RSS header */
1009         return (&cpl->data[1]);
1010 }
1011
1012 static void
1013 handle_nm_sge_egr_update(struct adapter *sc, struct ifnet *ifp,
1014     const struct cpl_sge_egr_update *egr)
1015 {
1016         uint32_t oq;
1017         struct sge_nm_txq *nm_txq;
1018
1019         oq = be32toh(egr->opcode_qid);
1020         MPASS(G_CPL_OPCODE(oq) == CPL_SGE_EGR_UPDATE);
1021         nm_txq = (void *)sc->sge.eqmap[G_EGR_QID(oq) - sc->sge.eq_start];
1022
1023         netmap_tx_irq(ifp, nm_txq->nid);
1024 }
1025
1026 void
1027 service_nm_rxq(struct sge_nm_rxq *nm_rxq)
1028 {
1029         struct vi_info *vi = nm_rxq->vi;
1030         struct adapter *sc = vi->pi->adapter;
1031         struct ifnet *ifp = vi->ifp;
1032         struct netmap_adapter *na = NA(ifp);
1033         struct netmap_kring *kring = na->rx_rings[nm_rxq->nid];
1034         struct netmap_ring *ring = kring->ring;
1035         struct iq_desc *d = &nm_rxq->iq_desc[nm_rxq->iq_cidx];
1036         const void *cpl;
1037         uint32_t lq;
1038         u_int work = 0;
1039         uint8_t opcode;
1040         uint32_t fl_cidx = atomic_load_acq_32(&nm_rxq->fl_cidx);
1041         u_int fl_credits = fl_cidx & 7;
1042         u_int ndesc = 0;        /* desc processed since last cidx update */
1043         u_int nframes = 0;      /* frames processed since last netmap wakeup */
1044
1045         while ((d->rsp.u.type_gen & F_RSPD_GEN) == nm_rxq->iq_gen) {
1046
1047                 rmb();
1048
1049                 lq = be32toh(d->rsp.pldbuflen_qid);
1050                 opcode = d->rss.opcode;
1051                 cpl = &d->cpl[0];
1052
1053                 switch (G_RSPD_TYPE(d->rsp.u.type_gen)) {
1054                 case X_RSPD_TYPE_FLBUF:
1055
1056                         /* fall through */
1057
1058                 case X_RSPD_TYPE_CPL:
1059                         MPASS(opcode < NUM_CPL_CMDS);
1060
1061                         switch (opcode) {
1062                         case CPL_FW4_MSG:
1063                         case CPL_FW6_MSG:
1064                                 cpl = unwrap_nm_fw6_msg(cpl);
1065                                 /* fall through */
1066                         case CPL_SGE_EGR_UPDATE:
1067                                 handle_nm_sge_egr_update(sc, ifp, cpl);
1068                                 break;
1069                         case CPL_RX_PKT:
1070                                 ring->slot[fl_cidx].len = G_RSPD_LEN(lq) -
1071                                     sc->params.sge.fl_pktshift;
1072                                 ring->slot[fl_cidx].flags = 0;
1073                                 nframes++;
1074                                 if (!(lq & F_RSPD_NEWBUF)) {
1075                                         MPASS(black_hole == 2);
1076                                         break;
1077                                 }
1078                                 fl_credits++;
1079                                 if (__predict_false(++fl_cidx == nm_rxq->fl_sidx))
1080                                         fl_cidx = 0;
1081                                 break;
1082                         default:
1083                                 panic("%s: unexpected opcode 0x%x on nm_rxq %p",
1084                                     __func__, opcode, nm_rxq);
1085                         }
1086                         break;
1087
1088                 case X_RSPD_TYPE_INTR:
1089                         /* Not equipped to handle forwarded interrupts. */
1090                         panic("%s: netmap queue received interrupt for iq %u\n",
1091                             __func__, lq);
1092
1093                 default:
1094                         panic("%s: illegal response type %d on nm_rxq %p",
1095                             __func__, G_RSPD_TYPE(d->rsp.u.type_gen), nm_rxq);
1096                 }
1097
1098                 d++;
1099                 if (__predict_false(++nm_rxq->iq_cidx == nm_rxq->iq_sidx)) {
1100                         nm_rxq->iq_cidx = 0;
1101                         d = &nm_rxq->iq_desc[0];
1102                         nm_rxq->iq_gen ^= F_RSPD_GEN;
1103                 }
1104
1105                 if (__predict_false(++nframes == rx_nframes) && !black_hole) {
1106                         atomic_store_rel_32(&nm_rxq->fl_cidx, fl_cidx);
1107                         netmap_rx_irq(ifp, nm_rxq->nid, &work);
1108                         nframes = 0;
1109                 }
1110
1111                 if (__predict_false(++ndesc == rx_ndesc)) {
1112                         if (black_hole && fl_credits >= 8) {
1113                                 fl_credits /= 8;
1114                                 IDXINCR(nm_rxq->fl_pidx, fl_credits * 8,
1115                                     nm_rxq->fl_sidx);
1116                                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
1117                                     nm_rxq->fl_db_val | V_PIDX(fl_credits));
1118                                 fl_credits = fl_cidx & 7;
1119                         }
1120                         t4_write_reg(sc, sc->sge_gts_reg,
1121                             V_CIDXINC(ndesc) |
1122                             V_INGRESSQID(nm_rxq->iq_cntxt_id) |
1123                             V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1124                         ndesc = 0;
1125                 }
1126         }
1127
1128         atomic_store_rel_32(&nm_rxq->fl_cidx, fl_cidx);
1129         if (black_hole) {
1130                 fl_credits /= 8;
1131                 IDXINCR(nm_rxq->fl_pidx, fl_credits * 8, nm_rxq->fl_sidx);
1132                 t4_write_reg(sc, sc->sge_kdoorbell_reg,
1133                     nm_rxq->fl_db_val | V_PIDX(fl_credits));
1134         } else if (nframes > 0)
1135                 netmap_rx_irq(ifp, nm_rxq->nid, &work);
1136
1137         t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndesc) |
1138             V_INGRESSQID((u32)nm_rxq->iq_cntxt_id) |
1139             V_SEINTARM(V_QINTR_TIMER_IDX(holdoff_tmr_idx)));
1140 }
1141 #endif