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