1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011, 2025 Chelsio Communications.
5 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_kern_tls.h"
33 #include "opt_ratelimit.h"
34
35 #include <sys/types.h>
36 #include <sys/eventhandler.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
40 #include <sys/ktls.h>
41 #include <sys/malloc.h>
42 #include <sys/msan.h>
43 #include <sys/queue.h>
44 #include <sys/sbuf.h>
45 #include <sys/taskqueue.h>
46 #include <sys/time.h>
47 #include <sys/sglist.h>
48 #include <sys/sysctl.h>
49 #include <sys/smp.h>
50 #include <sys/socketvar.h>
51 #include <sys/counter.h>
52 #include <net/bpf.h>
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_vlan_var.h>
56 #include <net/if_vxlan.h>
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip6.h>
60 #include <netinet/tcp.h>
61 #include <netinet/udp.h>
62 #include <machine/in_cksum.h>
63 #include <machine/md_var.h>
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #ifdef DEV_NETMAP
67 #include <machine/bus.h>
68 #include <sys/selinfo.h>
69 #include <net/if_var.h>
70 #include <net/netmap.h>
71 #include <dev/netmap/netmap_kern.h>
72 #endif
73
74 #include "common/common.h"
75 #include "common/t4_regs.h"
76 #include "common/t4_regs_values.h"
77 #include "common/t4_msg.h"
78 #include "t4_l2t.h"
79 #include "t4_mp_ring.h"
80
81 #define RX_COPY_THRESHOLD MINCLSIZE
82
83 /*
84 * Ethernet frames are DMA'd at this byte offset into the freelist buffer.
85 * 0-7 are valid values.
86 */
87 static int fl_pktshift = 0;
88 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pktshift, CTLFLAG_RDTUN, &fl_pktshift, 0,
89 "payload DMA offset in rx buffer (bytes)");
90
91 /*
92 * Pad ethernet payload up to this boundary.
93 * -1: driver should figure out a good value.
94 * 0: disable padding.
95 * Any power of 2 from 32 to 4096 (both inclusive) is also a valid value.
96 */
97 int fl_pad = -1;
98 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pad, CTLFLAG_RDTUN, &fl_pad, 0,
99 "payload pad boundary (bytes)");
100
101 /*
102 * Status page length.
103 * -1: driver should figure out a good value.
104 * 64 or 128 are the only other valid values.
105 */
106 static int spg_len = -1;
107 SYSCTL_INT(_hw_cxgbe, OID_AUTO, spg_len, CTLFLAG_RDTUN, &spg_len, 0,
108 "status page size (bytes)");
109
110 /*
111 * Congestion drops.
112 * -1: no congestion feedback (not recommended).
113 * 0: backpressure the channel instead of dropping packets right away.
114 * 1: no backpressure, drop packets for the congested queue immediately.
115 * 2: both backpressure and drop.
116 */
117 static int cong_drop = 0;
118 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cong_drop, CTLFLAG_RDTUN, &cong_drop, 0,
119 "Congestion control for NIC RX queues (0 = backpressure, 1 = drop, 2 = both");
120 #ifdef TCP_OFFLOAD
121 static int ofld_cong_drop = 0;
122 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ofld_cong_drop, CTLFLAG_RDTUN, &ofld_cong_drop, 0,
123 "Congestion control for TOE RX queues (0 = backpressure, 1 = drop, 2 = both");
124 #endif
125
126 /*
127 * Deliver multiple frames in the same free list buffer if they fit.
128 * -1: let the driver decide whether to enable buffer packing or not.
129 * 0: disable buffer packing.
130 * 1: enable buffer packing.
131 */
132 static int buffer_packing = -1;
133 SYSCTL_INT(_hw_cxgbe, OID_AUTO, buffer_packing, CTLFLAG_RDTUN, &buffer_packing,
134 0, "Enable buffer packing");
135
136 /*
137 * Start next frame in a packed buffer at this boundary.
138 * -1: driver should figure out a good value.
139 * T4: driver will ignore this and use the same value as fl_pad above.
140 * T5: 16, or a power of 2 from 64 to 4096 (both inclusive) is a valid value.
141 */
142 static int fl_pack = -1;
143 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fl_pack, CTLFLAG_RDTUN, &fl_pack, 0,
144 "payload pack boundary (bytes)");
145
146 /*
147 * Largest rx cluster size that the driver is allowed to allocate.
148 */
149 static int largest_rx_cluster = MJUM16BYTES;
150 SYSCTL_INT(_hw_cxgbe, OID_AUTO, largest_rx_cluster, CTLFLAG_RDTUN,
151 &largest_rx_cluster, 0, "Largest rx cluster (bytes)");
152
153 /*
154 * Size of cluster allocation that's most likely to succeed. The driver will
155 * fall back to this size if it fails to allocate clusters larger than this.
156 */
157 static int safest_rx_cluster = PAGE_SIZE;
158 SYSCTL_INT(_hw_cxgbe, OID_AUTO, safest_rx_cluster, CTLFLAG_RDTUN,
159 &safest_rx_cluster, 0, "Safe rx cluster (bytes)");
160
161 #ifdef RATELIMIT
162 /*
163 * Knob to control TCP timestamp rewriting, and the granularity of the tick used
164 * for rewriting. -1 and 0-3 are all valid values.
165 * -1: hardware should leave the TCP timestamps alone.
166 * 0: 1ms
167 * 1: 100us
168 * 2: 10us
169 * 3: 1us
170 */
171 static int tsclk = -1;
172 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tsclk, CTLFLAG_RDTUN, &tsclk, 0,
173 "Control TCP timestamp rewriting when using pacing");
174
175 static int eo_max_backlog = 1024 * 1024;
176 SYSCTL_INT(_hw_cxgbe, OID_AUTO, eo_max_backlog, CTLFLAG_RDTUN, &eo_max_backlog,
177 0, "Maximum backlog of ratelimited data per flow");
178 #endif
179
180 /*
181 * The interrupt holdoff timers are multiplied by this value on T6+.
182 * 1 and 3-17 (both inclusive) are legal values.
183 */
184 static int tscale = 1;
185 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tscale, CTLFLAG_RDTUN, &tscale, 0,
186 "Interrupt holdoff timer scale on T6+");
187
188 /*
189 * Number of LRO entries in the lro_ctrl structure per rx queue.
190 */
191 static int lro_entries = TCP_LRO_ENTRIES;
192 SYSCTL_INT(_hw_cxgbe, OID_AUTO, lro_entries, CTLFLAG_RDTUN, &lro_entries, 0,
193 "Number of LRO entries per RX queue");
194
195 /*
196 * This enables presorting of frames before they're fed into tcp_lro_rx.
197 */
198 static int lro_mbufs = 0;
199 SYSCTL_INT(_hw_cxgbe, OID_AUTO, lro_mbufs, CTLFLAG_RDTUN, &lro_mbufs, 0,
200 "Enable presorting of LRO frames");
201
202 static counter_u64_t pullups;
203 SYSCTL_COUNTER_U64(_hw_cxgbe, OID_AUTO, pullups, CTLFLAG_RD, &pullups,
204 "Number of mbuf pullups performed");
205
206 static counter_u64_t defrags;
207 SYSCTL_COUNTER_U64(_hw_cxgbe, OID_AUTO, defrags, CTLFLAG_RD, &defrags,
208 "Number of mbuf defrags performed");
209
210 static int t4_tx_coalesce = 1;
211 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce, CTLFLAG_RWTUN, &t4_tx_coalesce, 0,
212 "tx coalescing allowed");
213
214 /*
215 * The driver will make aggressive attempts at tx coalescing if it sees these
216 * many packets eligible for coalescing in quick succession, with no more than
217 * the specified gap in between the eth_tx calls that delivered the packets.
218 */
219 static int t4_tx_coalesce_pkts = 32;
220 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce_pkts, CTLFLAG_RWTUN,
221 &t4_tx_coalesce_pkts, 0,
222 "# of consecutive packets (1 - 255) that will trigger tx coalescing");
223 static int t4_tx_coalesce_gap = 5;
224 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_coalesce_gap, CTLFLAG_RWTUN,
225 &t4_tx_coalesce_gap, 0, "tx gap (in microseconds)");
226
227 static int service_iq(struct sge_iq *, int);
228 static int service_iq_fl(struct sge_iq *, int);
229 static struct mbuf *get_fl_payload(struct adapter *, struct sge_fl *, uint32_t);
230 static int eth_rx(struct adapter *, struct sge_rxq *, const struct iq_desc *,
231 u_int);
232 static inline void init_iq(struct sge_iq *, struct adapter *, int, int, int,
233 int, int, int);
234 static inline void init_fl(struct adapter *, struct sge_fl *, int, int, char *);
235 static inline void init_eq(struct adapter *, struct sge_eq *, int, int, uint8_t,
236 struct sge_iq *, char *);
237 static int alloc_iq_fl(struct vi_info *, struct sge_iq *, struct sge_fl *,
238 struct sysctl_ctx_list *, struct sysctl_oid *);
239 static void free_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
240 static void add_iq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
241 struct sge_iq *);
242 static void add_fl_sysctls(struct adapter *, struct sysctl_ctx_list *,
243 struct sysctl_oid *, struct sge_fl *);
244 static int alloc_iq_fl_hwq(struct vi_info *, struct sge_iq *, struct sge_fl *);
245 static int free_iq_fl_hwq(struct adapter *, struct sge_iq *, struct sge_fl *);
246 static int alloc_fwq(struct adapter *);
247 static void free_fwq(struct adapter *);
248 static int alloc_ctrlq(struct adapter *, int);
249 static void free_ctrlq(struct adapter *, int);
250 static int alloc_rxq(struct vi_info *, struct sge_rxq *, int, int, int);
251 static void free_rxq(struct vi_info *, struct sge_rxq *);
252 static void add_rxq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
253 struct sge_rxq *);
254 #ifdef TCP_OFFLOAD
255 static int alloc_ofld_rxq(struct vi_info *, struct sge_ofld_rxq *, int, int,
256 int);
257 static void free_ofld_rxq(struct vi_info *, struct sge_ofld_rxq *);
258 static void add_ofld_rxq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
259 struct sge_ofld_rxq *);
260 #endif
261 static int ctrl_eq_alloc(struct adapter *, struct sge_eq *, int);
262 static int eth_eq_alloc(struct adapter *, struct vi_info *, struct sge_eq *,
263 int);
264 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
265 static int ofld_eq_alloc(struct adapter *, struct vi_info *, struct sge_eq *,
266 int);
267 #endif
268 static int alloc_eq(struct adapter *, struct sge_eq *, struct sysctl_ctx_list *,
269 struct sysctl_oid *);
270 static void free_eq(struct adapter *, struct sge_eq *);
271 static void add_eq_sysctls(struct adapter *, struct sysctl_ctx_list *,
272 struct sysctl_oid *, struct sge_eq *);
273 static int alloc_eq_hwq(struct adapter *, struct vi_info *, struct sge_eq *,
274 int);
275 static int free_eq_hwq(struct adapter *, struct vi_info *, struct sge_eq *);
276 static int alloc_wrq(struct adapter *, struct vi_info *, struct sge_wrq *,
277 struct sysctl_ctx_list *, struct sysctl_oid *);
278 static void free_wrq(struct adapter *, struct sge_wrq *);
279 static void add_wrq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
280 struct sge_wrq *);
281 static int alloc_txq(struct vi_info *, struct sge_txq *, int);
282 static void free_txq(struct vi_info *, struct sge_txq *);
283 static void add_txq_sysctls(struct vi_info *, struct sysctl_ctx_list *,
284 struct sysctl_oid *, struct sge_txq *);
285 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
286 static int alloc_ofld_txq(struct vi_info *, struct sge_ofld_txq *, int);
287 static void free_ofld_txq(struct vi_info *, struct sge_ofld_txq *);
288 static void add_ofld_txq_sysctls(struct sysctl_ctx_list *, struct sysctl_oid *,
289 struct sge_ofld_txq *);
290 #endif
291 static void oneseg_dma_callback(void *, bus_dma_segment_t *, int, int);
292 static inline void ring_fl_db(struct adapter *, struct sge_fl *);
293 static int refill_fl(struct adapter *, struct sge_fl *, int);
294 static void refill_sfl(void *);
295 static int find_refill_source(struct adapter *, int, bool);
296 static void add_fl_to_sfl(struct adapter *, struct sge_fl *);
297
298 static inline void get_pkt_gl(struct mbuf *, struct sglist *);
299 static inline u_int txpkt_len16(u_int, const u_int);
300 static inline u_int txpkt_vm_len16(u_int, const u_int);
301 static inline void calculate_mbuf_len16(struct mbuf *, bool);
302 static inline u_int txpkts0_len16(u_int);
303 static inline u_int txpkts1_len16(void);
304 static u_int write_raw_wr(struct sge_txq *, void *, struct mbuf *, u_int);
305 static u_int write_txpkt_wr(struct adapter *, struct sge_txq *, struct mbuf *,
306 u_int);
307 static u_int write_txpkt_vm_wr(struct adapter *, struct sge_txq *,
308 struct mbuf *);
309 static int add_to_txpkts_vf(struct adapter *, struct sge_txq *, struct mbuf *,
310 int, bool *);
311 static int add_to_txpkts_pf(struct adapter *, struct sge_txq *, struct mbuf *,
312 int, bool *);
313 static u_int write_txpkts_wr(struct adapter *, struct sge_txq *);
314 static u_int write_txpkts_vm_wr(struct adapter *, struct sge_txq *);
315 static void write_gl_to_txd(struct sge_txq *, struct mbuf *, caddr_t *, int);
316 static inline void copy_to_txd(struct sge_eq *, caddr_t, caddr_t *, int);
317 static inline void ring_eq_db(struct adapter *, struct sge_eq *, u_int);
318 static inline uint16_t read_hw_cidx(struct sge_eq *);
319 static inline u_int reclaimable_tx_desc(struct sge_eq *);
320 static inline u_int total_available_tx_desc(struct sge_eq *);
321 static u_int reclaim_tx_descs(struct sge_txq *, u_int);
322 static void tx_reclaim(void *, int);
323 static __be64 get_flit(struct sglist_seg *, int, int);
324 static int handle_sge_egr_update(struct sge_iq *, const struct rss_header *,
325 struct mbuf *);
326 static int handle_fw_msg(struct sge_iq *, const struct rss_header *,
327 struct mbuf *);
328 static int t4_handle_wrerr_rpl(struct adapter *, const __be64 *);
329 static void wrq_tx_drain(void *, int);
330 static void drain_wrq_wr_list(struct adapter *, struct sge_wrq *);
331
332 static int sysctl_bufsizes(SYSCTL_HANDLER_ARGS);
333 #ifdef RATELIMIT
334 static int ethofld_fw4_ack(struct sge_iq *, const struct rss_header *,
335 struct mbuf *);
336 #if defined(INET) || defined(INET6)
337 static inline u_int txpkt_eo_len16(u_int, u_int, u_int);
338 static int ethofld_transmit(if_t, struct mbuf *);
339 #endif
340 #endif
341
342 static counter_u64_t extfree_refs;
343 static counter_u64_t extfree_rels;
344
345 an_handler_t t4_an_handler;
346 fw_msg_handler_t t4_fw_msg_handler[NUM_FW6_TYPES];
347 cpl_handler_t t4_cpl_handler[NUM_CPL_CMDS];
348 cpl_handler_t set_tcb_rpl_handlers[NUM_CPL_COOKIES];
349 cpl_handler_t l2t_write_rpl_handlers[NUM_CPL_COOKIES];
350 cpl_handler_t act_open_rpl_handlers[NUM_CPL_COOKIES];
351 cpl_handler_t abort_rpl_rss_handlers[NUM_CPL_COOKIES];
352 cpl_handler_t fw4_ack_handlers[NUM_CPL_COOKIES];
353 cpl_handler_t fw6_pld_handlers[NUM_CPL_FW6_COOKIES];
354
355 void
t4_register_an_handler(an_handler_t h)356 t4_register_an_handler(an_handler_t h)
357 {
358 uintptr_t *loc;
359
360 MPASS(h == NULL || t4_an_handler == NULL);
361
362 loc = (uintptr_t *)&t4_an_handler;
363 atomic_store_rel_ptr(loc, (uintptr_t)h);
364 }
365
366 void
t4_register_fw_msg_handler(int type,fw_msg_handler_t h)367 t4_register_fw_msg_handler(int type, fw_msg_handler_t h)
368 {
369 uintptr_t *loc;
370
371 MPASS(type < nitems(t4_fw_msg_handler));
372 MPASS(h == NULL || t4_fw_msg_handler[type] == NULL);
373 /*
374 * These are dispatched by the handler for FW{4|6}_CPL_MSG using the CPL
375 * handler dispatch table. Reject any attempt to install a handler for
376 * this subtype.
377 */
378 MPASS(type != FW_TYPE_RSSCPL);
379 MPASS(type != FW6_TYPE_RSSCPL);
380
381 loc = (uintptr_t *)&t4_fw_msg_handler[type];
382 atomic_store_rel_ptr(loc, (uintptr_t)h);
383 }
384
385 void
t4_register_cpl_handler(int opcode,cpl_handler_t h)386 t4_register_cpl_handler(int opcode, cpl_handler_t h)
387 {
388 uintptr_t *loc;
389
390 MPASS(opcode < nitems(t4_cpl_handler));
391 MPASS(h == NULL || t4_cpl_handler[opcode] == NULL);
392
393 loc = (uintptr_t *)&t4_cpl_handler[opcode];
394 atomic_store_rel_ptr(loc, (uintptr_t)h);
395 }
396
397 static int
set_tcb_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)398 set_tcb_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
399 struct mbuf *m)
400 {
401 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
402 u_int tid;
403 int cookie;
404
405 MPASS(m == NULL);
406
407 tid = GET_TID(cpl);
408 if (is_hpftid(iq->adapter, tid) || is_ftid(iq->adapter, tid)) {
409 /*
410 * The return code for filter-write is put in the CPL cookie so
411 * we have to rely on the hardware tid (is_ftid) to determine
412 * that this is a response to a filter.
413 */
414 cookie = CPL_COOKIE_FILTER;
415 } else {
416 cookie = G_COOKIE(cpl->cookie);
417 }
418 MPASS(cookie > CPL_COOKIE_RESERVED);
419 MPASS(cookie < nitems(set_tcb_rpl_handlers));
420
421 return (set_tcb_rpl_handlers[cookie](iq, rss, m));
422 }
423
424 static int
l2t_write_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)425 l2t_write_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
426 struct mbuf *m)
427 {
428 const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
429 unsigned int cookie;
430
431 MPASS(m == NULL);
432
433 cookie = GET_TID(rpl) & F_SYNC_WR ? CPL_COOKIE_TOM : CPL_COOKIE_FILTER;
434 return (l2t_write_rpl_handlers[cookie](iq, rss, m));
435 }
436
437 static int
act_open_rpl_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)438 act_open_rpl_handler(struct sge_iq *iq, const struct rss_header *rss,
439 struct mbuf *m)
440 {
441 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
442 u_int cookie = G_TID_COOKIE(G_AOPEN_ATID(be32toh(cpl->atid_status)));
443
444 MPASS(m == NULL);
445 MPASS(cookie != CPL_COOKIE_RESERVED);
446
447 return (act_open_rpl_handlers[cookie](iq, rss, m));
448 }
449
450 static int
abort_rpl_rss_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)451 abort_rpl_rss_handler(struct sge_iq *iq, const struct rss_header *rss,
452 struct mbuf *m)
453 {
454 struct adapter *sc = iq->adapter;
455 u_int cookie;
456
457 MPASS(m == NULL);
458 if (is_hashfilter(sc))
459 cookie = CPL_COOKIE_HASHFILTER;
460 else
461 cookie = CPL_COOKIE_TOM;
462
463 return (abort_rpl_rss_handlers[cookie](iq, rss, m));
464 }
465
466 static int
fw4_ack_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)467 fw4_ack_handler(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
468 {
469 struct adapter *sc = iq->adapter;
470 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
471 unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
472 u_int cookie;
473
474 MPASS(m == NULL);
475 if (is_etid(sc, tid))
476 cookie = CPL_COOKIE_ETHOFLD;
477 else
478 cookie = CPL_COOKIE_TOM;
479
480 return (fw4_ack_handlers[cookie](iq, rss, m));
481 }
482
483 static int
fw6_pld_handler(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)484 fw6_pld_handler(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
485 {
486 const struct cpl_fw6_pld *cpl;
487 uint64_t cookie;
488
489 if (m != NULL)
490 cpl = mtod(m, const void *);
491 else
492 cpl = (const void *)(rss + 1);
493 cookie = be64toh(cpl->data[1]) & CPL_FW6_COOKIE_MASK;
494
495 return (fw6_pld_handlers[cookie](iq, rss, m));
496 }
497
498 static void
t4_init_shared_cpl_handlers(void)499 t4_init_shared_cpl_handlers(void)
500 {
501
502 t4_register_cpl_handler(CPL_SET_TCB_RPL, set_tcb_rpl_handler);
503 t4_register_cpl_handler(CPL_L2T_WRITE_RPL, l2t_write_rpl_handler);
504 t4_register_cpl_handler(CPL_ACT_OPEN_RPL, act_open_rpl_handler);
505 t4_register_cpl_handler(CPL_ABORT_RPL_RSS, abort_rpl_rss_handler);
506 t4_register_cpl_handler(CPL_FW4_ACK, fw4_ack_handler);
507 t4_register_cpl_handler(CPL_FW6_PLD, fw6_pld_handler);
508 }
509
510 void
t4_register_shared_cpl_handler(int opcode,cpl_handler_t h,int cookie)511 t4_register_shared_cpl_handler(int opcode, cpl_handler_t h, int cookie)
512 {
513 uintptr_t *loc;
514
515 MPASS(opcode < nitems(t4_cpl_handler));
516 if (opcode == CPL_FW6_PLD) {
517 MPASS(cookie < NUM_CPL_FW6_COOKIES);
518 } else {
519 MPASS(cookie > CPL_COOKIE_RESERVED);
520 MPASS(cookie < NUM_CPL_COOKIES);
521 }
522 MPASS(t4_cpl_handler[opcode] != NULL);
523
524 switch (opcode) {
525 case CPL_SET_TCB_RPL:
526 loc = (uintptr_t *)&set_tcb_rpl_handlers[cookie];
527 break;
528 case CPL_L2T_WRITE_RPL:
529 loc = (uintptr_t *)&l2t_write_rpl_handlers[cookie];
530 break;
531 case CPL_ACT_OPEN_RPL:
532 loc = (uintptr_t *)&act_open_rpl_handlers[cookie];
533 break;
534 case CPL_ABORT_RPL_RSS:
535 loc = (uintptr_t *)&abort_rpl_rss_handlers[cookie];
536 break;
537 case CPL_FW4_ACK:
538 loc = (uintptr_t *)&fw4_ack_handlers[cookie];
539 break;
540 case CPL_FW6_PLD:
541 loc = (uintptr_t *)&fw6_pld_handlers[cookie];
542 break;
543 default:
544 MPASS(0);
545 return;
546 }
547 MPASS(h == NULL || *loc == (uintptr_t)NULL);
548 atomic_store_rel_ptr(loc, (uintptr_t)h);
549 }
550
551 /*
552 * Called on MOD_LOAD. Validates and calculates the SGE tunables.
553 */
554 void
t4_sge_modload(void)555 t4_sge_modload(void)
556 {
557
558 if (fl_pktshift < 0 || fl_pktshift > 7) {
559 printf("Invalid hw.cxgbe.fl_pktshift value (%d),"
560 " using 0 instead.\n", fl_pktshift);
561 fl_pktshift = 0;
562 }
563
564 if (spg_len != 64 && spg_len != 128) {
565 int len;
566
567 #if defined(__i386__) || defined(__amd64__)
568 len = cpu_clflush_line_size > 64 ? 128 : 64;
569 #else
570 len = 64;
571 #endif
572 if (spg_len != -1) {
573 printf("Invalid hw.cxgbe.spg_len value (%d),"
574 " using %d instead.\n", spg_len, len);
575 }
576 spg_len = len;
577 }
578
579 if (cong_drop < -1 || cong_drop > 2) {
580 printf("Invalid hw.cxgbe.cong_drop value (%d),"
581 " using 0 instead.\n", cong_drop);
582 cong_drop = 0;
583 }
584 #ifdef TCP_OFFLOAD
585 if (ofld_cong_drop < -1 || ofld_cong_drop > 2) {
586 printf("Invalid hw.cxgbe.ofld_cong_drop value (%d),"
587 " using 0 instead.\n", ofld_cong_drop);
588 ofld_cong_drop = 0;
589 }
590 #endif
591
592 if (tscale != 1 && (tscale < 3 || tscale > 17)) {
593 printf("Invalid hw.cxgbe.tscale value (%d),"
594 " using 1 instead.\n", tscale);
595 tscale = 1;
596 }
597
598 if (largest_rx_cluster != MCLBYTES &&
599 #if MJUMPAGESIZE != MCLBYTES
600 largest_rx_cluster != MJUMPAGESIZE &&
601 #endif
602 largest_rx_cluster != MJUM9BYTES &&
603 largest_rx_cluster != MJUM16BYTES) {
604 printf("Invalid hw.cxgbe.largest_rx_cluster value (%d),"
605 " using %d instead.\n", largest_rx_cluster, MJUM16BYTES);
606 largest_rx_cluster = MJUM16BYTES;
607 }
608
609 if (safest_rx_cluster != MCLBYTES &&
610 #if MJUMPAGESIZE != MCLBYTES
611 safest_rx_cluster != MJUMPAGESIZE &&
612 #endif
613 safest_rx_cluster != MJUM9BYTES &&
614 safest_rx_cluster != MJUM16BYTES) {
615 printf("Invalid hw.cxgbe.safest_rx_cluster value (%d),"
616 " using %d instead.\n", safest_rx_cluster, MJUMPAGESIZE);
617 safest_rx_cluster = MJUMPAGESIZE;
618 }
619
620 extfree_refs = counter_u64_alloc(M_WAITOK);
621 extfree_rels = counter_u64_alloc(M_WAITOK);
622 pullups = counter_u64_alloc(M_WAITOK);
623 defrags = counter_u64_alloc(M_WAITOK);
624 counter_u64_zero(extfree_refs);
625 counter_u64_zero(extfree_rels);
626 counter_u64_zero(pullups);
627 counter_u64_zero(defrags);
628
629 t4_init_shared_cpl_handlers();
630 t4_register_cpl_handler(CPL_FW4_MSG, handle_fw_msg);
631 t4_register_cpl_handler(CPL_FW6_MSG, handle_fw_msg);
632 t4_register_cpl_handler(CPL_SGE_EGR_UPDATE, handle_sge_egr_update);
633 #ifdef RATELIMIT
634 t4_register_shared_cpl_handler(CPL_FW4_ACK, ethofld_fw4_ack,
635 CPL_COOKIE_ETHOFLD);
636 #endif
637 t4_register_fw_msg_handler(FW6_TYPE_CMD_RPL, t4_handle_fw_rpl);
638 t4_register_fw_msg_handler(FW6_TYPE_WRERR_RPL, t4_handle_wrerr_rpl);
639 }
640
641 void
t4_sge_modunload(void)642 t4_sge_modunload(void)
643 {
644
645 counter_u64_free(extfree_refs);
646 counter_u64_free(extfree_rels);
647 counter_u64_free(pullups);
648 counter_u64_free(defrags);
649 }
650
651 uint64_t
t4_sge_extfree_refs(void)652 t4_sge_extfree_refs(void)
653 {
654 uint64_t refs, rels;
655
656 rels = counter_u64_fetch(extfree_rels);
657 refs = counter_u64_fetch(extfree_refs);
658
659 return (refs - rels);
660 }
661
662 /* max 4096 */
663 #define MAX_PACK_BOUNDARY 512
664
665 static inline void
setup_pad_and_pack_boundaries(struct adapter * sc)666 setup_pad_and_pack_boundaries(struct adapter *sc)
667 {
668 uint32_t v, m;
669 int pad, pack, pad_shift;
670
671 pad_shift = chip_id(sc) > CHELSIO_T5 ? X_T6_INGPADBOUNDARY_SHIFT :
672 X_INGPADBOUNDARY_SHIFT;
673 pad = fl_pad;
674 if (fl_pad < (1 << pad_shift) ||
675 fl_pad > (1 << (pad_shift + M_INGPADBOUNDARY)) ||
676 !powerof2(fl_pad)) {
677 /*
678 * If there is any chance that we might use buffer packing and
679 * the chip is a T4, then pick 64 as the pad/pack boundary. Set
680 * it to the minimum allowed in all other cases.
681 */
682 pad = is_t4(sc) && buffer_packing ? 64 : 1 << pad_shift;
683
684 /*
685 * For fl_pad = 0 we'll still write a reasonable value to the
686 * register but all the freelists will opt out of padding.
687 * We'll complain here only if the user tried to set it to a
688 * value greater than 0 that was invalid.
689 */
690 if (fl_pad > 0) {
691 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pad value"
692 " (%d), using %d instead.\n", fl_pad, pad);
693 }
694 }
695 m = V_INGPADBOUNDARY(M_INGPADBOUNDARY);
696 v = V_INGPADBOUNDARY(ilog2(pad) - pad_shift);
697 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
698
699 if (is_t4(sc)) {
700 if (fl_pack != -1 && fl_pack != pad) {
701 /* Complain but carry on. */
702 device_printf(sc->dev, "hw.cxgbe.fl_pack (%d) ignored,"
703 " using %d instead.\n", fl_pack, pad);
704 }
705 return;
706 }
707
708 pack = fl_pack;
709 if (fl_pack < 16 || fl_pack == 32 || fl_pack > 4096 ||
710 !powerof2(fl_pack)) {
711 if (sc->params.pci.mps > MAX_PACK_BOUNDARY)
712 pack = MAX_PACK_BOUNDARY;
713 else
714 pack = max(sc->params.pci.mps, CACHE_LINE_SIZE);
715 MPASS(powerof2(pack));
716 if (pack < 16)
717 pack = 16;
718 if (pack == 32)
719 pack = 64;
720 if (pack > 4096)
721 pack = 4096;
722 if (fl_pack != -1) {
723 device_printf(sc->dev, "Invalid hw.cxgbe.fl_pack value"
724 " (%d), using %d instead.\n", fl_pack, pack);
725 }
726 }
727 m = V_INGPACKBOUNDARY(M_INGPACKBOUNDARY);
728 if (pack == 16)
729 v = V_INGPACKBOUNDARY(0);
730 else
731 v = V_INGPACKBOUNDARY(ilog2(pack) - 5);
732
733 MPASS(!is_t4(sc)); /* T4 doesn't have SGE_CONTROL2 */
734 t4_set_reg_field(sc, A_SGE_CONTROL2, m, v);
735 }
736
737 /*
738 * adap->params.vpd.cclk must be set up before this is called.
739 */
740 void
t4_tweak_chip_settings(struct adapter * sc)741 t4_tweak_chip_settings(struct adapter *sc)
742 {
743 int i, reg;
744 uint32_t v, m;
745 int intr_timer[SGE_NTIMERS] = {1, 5, 10, 50, 100, 200};
746 int timer_max = M_TIMERVALUE0 * 1000 / sc->params.vpd.cclk;
747 int intr_pktcount[SGE_NCOUNTERS] = {1, 8, 16, 32}; /* 63 max */
748 uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
749 static int sw_buf_sizes[] = {
750 MCLBYTES,
751 #if MJUMPAGESIZE != MCLBYTES
752 MJUMPAGESIZE,
753 #endif
754 MJUM9BYTES,
755 MJUM16BYTES
756 };
757
758 KASSERT(sc->flags & MASTER_PF,
759 ("%s: trying to change chip settings when not master.", __func__));
760
761 m = V_PKTSHIFT(M_PKTSHIFT) | F_RXPKTCPLMODE | F_EGRSTATUSPAGESIZE;
762 v = V_PKTSHIFT(fl_pktshift) | F_RXPKTCPLMODE |
763 V_EGRSTATUSPAGESIZE(spg_len == 128);
764 t4_set_reg_field(sc, A_SGE_CONTROL, m, v);
765
766 setup_pad_and_pack_boundaries(sc);
767
768 v = V_HOSTPAGESIZEPF0(PAGE_SHIFT - 10) |
769 V_HOSTPAGESIZEPF1(PAGE_SHIFT - 10) |
770 V_HOSTPAGESIZEPF2(PAGE_SHIFT - 10) |
771 V_HOSTPAGESIZEPF3(PAGE_SHIFT - 10) |
772 V_HOSTPAGESIZEPF4(PAGE_SHIFT - 10) |
773 V_HOSTPAGESIZEPF5(PAGE_SHIFT - 10) |
774 V_HOSTPAGESIZEPF6(PAGE_SHIFT - 10) |
775 V_HOSTPAGESIZEPF7(PAGE_SHIFT - 10);
776 t4_write_reg(sc, A_SGE_HOST_PAGE_SIZE, v);
777
778 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE0, 4096);
779 t4_write_reg(sc, A_SGE_FL_BUFFER_SIZE1, 65536);
780 reg = A_SGE_FL_BUFFER_SIZE2;
781 for (i = 0; i < nitems(sw_buf_sizes); i++) {
782 MPASS(reg <= A_SGE_FL_BUFFER_SIZE15);
783 t4_write_reg(sc, reg, sw_buf_sizes[i]);
784 reg += 4;
785 MPASS(reg <= A_SGE_FL_BUFFER_SIZE15);
786 t4_write_reg(sc, reg, sw_buf_sizes[i] - CL_METADATA_SIZE);
787 reg += 4;
788 }
789
790 v = V_THRESHOLD_0(intr_pktcount[0]) | V_THRESHOLD_1(intr_pktcount[1]) |
791 V_THRESHOLD_2(intr_pktcount[2]) | V_THRESHOLD_3(intr_pktcount[3]);
792 t4_write_reg(sc, A_SGE_INGRESS_RX_THRESHOLD, v);
793
794 KASSERT(intr_timer[0] <= timer_max,
795 ("%s: not a single usable timer (%d, %d)", __func__, intr_timer[0],
796 timer_max));
797 for (i = 1; i < nitems(intr_timer); i++) {
798 KASSERT(intr_timer[i] >= intr_timer[i - 1],
799 ("%s: timers not listed in increasing order (%d)",
800 __func__, i));
801
802 while (intr_timer[i] > timer_max) {
803 if (i == nitems(intr_timer) - 1) {
804 intr_timer[i] = timer_max;
805 break;
806 }
807 intr_timer[i] += intr_timer[i - 1];
808 intr_timer[i] /= 2;
809 }
810 }
811
812 v = V_TIMERVALUE0(us_to_core_ticks(sc, intr_timer[0])) |
813 V_TIMERVALUE1(us_to_core_ticks(sc, intr_timer[1]));
814 t4_write_reg(sc, A_SGE_TIMER_VALUE_0_AND_1, v);
815 v = V_TIMERVALUE2(us_to_core_ticks(sc, intr_timer[2])) |
816 V_TIMERVALUE3(us_to_core_ticks(sc, intr_timer[3]));
817 t4_write_reg(sc, A_SGE_TIMER_VALUE_2_AND_3, v);
818 v = V_TIMERVALUE4(us_to_core_ticks(sc, intr_timer[4])) |
819 V_TIMERVALUE5(us_to_core_ticks(sc, intr_timer[5]));
820 t4_write_reg(sc, A_SGE_TIMER_VALUE_4_AND_5, v);
821
822 if (chip_id(sc) >= CHELSIO_T6) {
823 m = V_TSCALE(M_TSCALE);
824 if (tscale == 1)
825 v = 0;
826 else
827 v = V_TSCALE(tscale - 2);
828 t4_set_reg_field(sc, A_SGE_ITP_CONTROL, m, v);
829
830 if (sc->debug_flags & DF_DISABLE_TCB_CACHE) {
831 m = V_RDTHRESHOLD(M_RDTHRESHOLD) | F_WRTHRTHRESHEN |
832 V_WRTHRTHRESH(M_WRTHRTHRESH);
833 t4_tp_pio_read(sc, &v, 1, A_TP_CMM_CONFIG, 1);
834 v &= ~m;
835 v |= V_RDTHRESHOLD(1) | F_WRTHRTHRESHEN |
836 V_WRTHRTHRESH(16);
837 t4_tp_pio_write(sc, &v, 1, A_TP_CMM_CONFIG, 1);
838 }
839 }
840
841 /* 4K, 16K, 64K, 256K DDP "page sizes" for TDDP */
842 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
843 t4_write_reg(sc, A_ULP_RX_TDDP_PSZ, v);
844
845 /*
846 * 4K, 8K, 16K, 64K DDP "page sizes" for iSCSI DDP. These have been
847 * chosen with MAXPHYS = 128K in mind. The largest DDP buffer that we
848 * may have to deal with is MAXPHYS + 1 page.
849 */
850 v = V_HPZ0(0) | V_HPZ1(1) | V_HPZ2(2) | V_HPZ3(4);
851 t4_write_reg(sc, A_ULP_RX_ISCSI_PSZ, v);
852
853 /* We use multiple DDP page sizes both in plain-TOE and ISCSI modes. */
854 m = v = F_TDDPTAGTCB | F_ISCSITAGTCB;
855 if (sc->nvmecaps != 0) {
856 /* Request DDP status bit for NVMe PDU completions. */
857 m |= F_NVME_TCP_DDP_VAL_EN;
858 v |= F_NVME_TCP_DDP_VAL_EN;
859 }
860 t4_set_reg_field(sc, A_ULP_RX_CTL, m, v);
861
862 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
863 F_RESETDDPOFFSET;
864 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
865 t4_set_reg_field(sc, A_TP_PARA_REG5, m, v);
866 }
867
868 /*
869 * SGE wants the buffer to be at least 64B and then a multiple of 16. Its
870 * address mut be 16B aligned. If padding is in use the buffer's start and end
871 * need to be aligned to the pad boundary as well. We'll just make sure that
872 * the size is a multiple of the pad boundary here, it is up to the buffer
873 * allocation code to make sure the start of the buffer is aligned.
874 */
875 static inline int
hwsz_ok(struct adapter * sc,int hwsz)876 hwsz_ok(struct adapter *sc, int hwsz)
877 {
878 int mask = fl_pad ? sc->params.sge.pad_boundary - 1 : 16 - 1;
879
880 return (hwsz >= 64 && (hwsz & mask) == 0);
881 }
882
883 /*
884 * Initialize the rx buffer sizes and figure out which zones the buffers will
885 * be allocated from.
886 */
887 void
t4_init_rx_buf_info(struct adapter * sc)888 t4_init_rx_buf_info(struct adapter *sc)
889 {
890 struct sge *s = &sc->sge;
891 struct sge_params *sp = &sc->params.sge;
892 int i, j, n;
893 static int sw_buf_sizes[] = { /* Sorted by size */
894 MCLBYTES,
895 #if MJUMPAGESIZE != MCLBYTES
896 MJUMPAGESIZE,
897 #endif
898 MJUM9BYTES,
899 MJUM16BYTES
900 };
901 struct rx_buf_info *rxb;
902
903 s->safe_zidx = -1;
904 rxb = &s->rx_buf_info[0];
905 for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
906 rxb->size1 = sw_buf_sizes[i];
907 rxb->zone = m_getzone(rxb->size1);
908 rxb->type = m_gettype(rxb->size1);
909 rxb->size2 = 0;
910 rxb->hwidx1 = -1;
911 rxb->hwidx2 = -1;
912 for (j = 0; j < SGE_FLBUF_SIZES; j++) {
913 int hwsize = sp->sge_fl_buffer_size[j];
914
915 if (!hwsz_ok(sc, hwsize))
916 continue;
917
918 /* hwidx for size1 */
919 if (rxb->hwidx1 == -1 && rxb->size1 == hwsize)
920 rxb->hwidx1 = j;
921
922 /* hwidx for size2 (buffer packing) */
923 if (rxb->size1 - CL_METADATA_SIZE < hwsize)
924 continue;
925 n = rxb->size1 - hwsize - CL_METADATA_SIZE;
926 if (n == 0) {
927 rxb->hwidx2 = j;
928 rxb->size2 = hwsize;
929 break; /* stop looking */
930 }
931 if (rxb->hwidx2 != -1) {
932 if (n < sp->sge_fl_buffer_size[rxb->hwidx2] -
933 hwsize - CL_METADATA_SIZE) {
934 rxb->hwidx2 = j;
935 rxb->size2 = hwsize;
936 }
937 } else if (n <= 2 * CL_METADATA_SIZE) {
938 rxb->hwidx2 = j;
939 rxb->size2 = hwsize;
940 }
941 }
942 if (rxb->hwidx2 != -1)
943 sc->flags |= BUF_PACKING_OK;
944 if (s->safe_zidx == -1 && rxb->size1 == safest_rx_cluster)
945 s->safe_zidx = i;
946 }
947 }
948
949 /*
950 * Verify some basic SGE settings for the PF and VF driver, and other
951 * miscellaneous settings for the PF driver.
952 */
953 int
t4_verify_chip_settings(struct adapter * sc)954 t4_verify_chip_settings(struct adapter *sc)
955 {
956 struct sge_params *sp = &sc->params.sge;
957 uint32_t m, v, r;
958 int rc = 0;
959 const uint16_t indsz = min(RX_COPY_THRESHOLD - 1, M_INDICATESIZE);
960
961 m = F_RXPKTCPLMODE;
962 v = F_RXPKTCPLMODE;
963 r = sp->sge_control;
964 if ((r & m) != v) {
965 device_printf(sc->dev, "invalid SGE_CONTROL(0x%x)\n", r);
966 rc = EINVAL;
967 }
968
969 /*
970 * If this changes then every single use of PAGE_SHIFT in the driver
971 * needs to be carefully reviewed for PAGE_SHIFT vs sp->page_shift.
972 */
973 if (sp->page_shift != PAGE_SHIFT) {
974 device_printf(sc->dev, "invalid SGE_HOST_PAGE_SIZE(0x%x)\n", r);
975 rc = EINVAL;
976 }
977
978 if (sc->flags & IS_VF)
979 return (0);
980
981 v = V_HPZ0(0) | V_HPZ1(2) | V_HPZ2(4) | V_HPZ3(6);
982 r = t4_read_reg(sc, A_ULP_RX_TDDP_PSZ);
983 if (r != v) {
984 device_printf(sc->dev, "invalid ULP_RX_TDDP_PSZ(0x%x)\n", r);
985 if (sc->vres.ddp.size != 0)
986 rc = EINVAL;
987 }
988
989 m = v = F_TDDPTAGTCB;
990 r = t4_read_reg(sc, A_ULP_RX_CTL);
991 if ((r & m) != v) {
992 device_printf(sc->dev, "invalid ULP_RX_CTL(0x%x)\n", r);
993 if (sc->vres.ddp.size != 0)
994 rc = EINVAL;
995 }
996
997 m = V_INDICATESIZE(M_INDICATESIZE) | F_REARMDDPOFFSET |
998 F_RESETDDPOFFSET;
999 v = V_INDICATESIZE(indsz) | F_REARMDDPOFFSET | F_RESETDDPOFFSET;
1000 r = t4_read_reg(sc, A_TP_PARA_REG5);
1001 if ((r & m) != v) {
1002 device_printf(sc->dev, "invalid TP_PARA_REG5(0x%x)\n", r);
1003 if (sc->vres.ddp.size != 0)
1004 rc = EINVAL;
1005 }
1006
1007 return (rc);
1008 }
1009
1010 int
t4_create_dma_tag(struct adapter * sc)1011 t4_create_dma_tag(struct adapter *sc)
1012 {
1013 int rc;
1014
1015 rc = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
1016 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
1017 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, BUS_DMA_ALLOCNOW, NULL,
1018 NULL, &sc->dmat);
1019 if (rc != 0) {
1020 device_printf(sc->dev,
1021 "failed to create main DMA tag: %d\n", rc);
1022 }
1023
1024 return (rc);
1025 }
1026
1027 void
t4_sge_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * children)1028 t4_sge_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
1029 struct sysctl_oid_list *children)
1030 {
1031 struct sge_params *sp = &sc->params.sge;
1032
1033 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "buffer_sizes",
1034 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1035 sysctl_bufsizes, "A", "freelist buffer sizes");
1036
1037 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pktshift", CTLFLAG_RD,
1038 NULL, sp->fl_pktshift, "payload DMA offset in rx buffer (bytes)");
1039
1040 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pad", CTLFLAG_RD,
1041 NULL, sp->pad_boundary, "payload pad boundary (bytes)");
1042
1043 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "spg_len", CTLFLAG_RD,
1044 NULL, sp->spg_len, "status page size (bytes)");
1045
1046 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_drop", CTLFLAG_RD,
1047 NULL, cong_drop, "congestion drop setting");
1048 #ifdef TCP_OFFLOAD
1049 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ofld_cong_drop", CTLFLAG_RD,
1050 NULL, ofld_cong_drop, "congestion drop setting");
1051 #endif
1052
1053 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "fl_pack", CTLFLAG_RD,
1054 NULL, sp->pack_boundary, "payload pack boundary (bytes)");
1055 }
1056
1057 int
t4_destroy_dma_tag(struct adapter * sc)1058 t4_destroy_dma_tag(struct adapter *sc)
1059 {
1060 if (sc->dmat)
1061 bus_dma_tag_destroy(sc->dmat);
1062
1063 return (0);
1064 }
1065
1066 /*
1067 * Allocate and initialize the firmware event queue, control queues, and special
1068 * purpose rx queues owned by the adapter.
1069 *
1070 * Returns errno on failure. Resources allocated up to that point may still be
1071 * allocated. Caller is responsible for cleanup in case this function fails.
1072 */
1073 int
t4_setup_adapter_queues(struct adapter * sc)1074 t4_setup_adapter_queues(struct adapter *sc)
1075 {
1076 int rc, i;
1077
1078 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
1079
1080 /*
1081 * Firmware event queue
1082 */
1083 rc = alloc_fwq(sc);
1084 if (rc != 0)
1085 return (rc);
1086
1087 /*
1088 * That's all for the VF driver.
1089 */
1090 if (sc->flags & IS_VF)
1091 return (rc);
1092
1093 /*
1094 * XXX: General purpose rx queues, one per port.
1095 */
1096
1097 /*
1098 * Control queues. At least one per port and per internal core.
1099 */
1100 for (i = 0; i < sc->sge.nctrlq; i++) {
1101 rc = alloc_ctrlq(sc, i);
1102 if (rc != 0)
1103 return (rc);
1104 }
1105
1106 return (rc);
1107 }
1108
1109 /*
1110 * Idempotent
1111 */
1112 int
t4_teardown_adapter_queues(struct adapter * sc)1113 t4_teardown_adapter_queues(struct adapter *sc)
1114 {
1115 int i;
1116
1117 ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
1118
1119 if (sc->sge.ctrlq != NULL) {
1120 MPASS(!(sc->flags & IS_VF)); /* VFs don't allocate ctrlq. */
1121 for (i = 0; i < sc->sge.nctrlq; i++)
1122 free_ctrlq(sc, i);
1123 }
1124 free_fwq(sc);
1125
1126 return (0);
1127 }
1128
1129 /* Maximum payload that could arrive with a single iq descriptor. */
1130 static inline int
max_rx_payload(struct adapter * sc,if_t ifp,const bool ofld)1131 max_rx_payload(struct adapter *sc, if_t ifp, const bool ofld)
1132 {
1133 int maxp;
1134
1135 /* large enough even when hw VLAN extraction is disabled */
1136 maxp = sc->params.sge.fl_pktshift + ETHER_HDR_LEN +
1137 ETHER_VLAN_ENCAP_LEN + if_getmtu(ifp);
1138 if (ofld && sc->tt.tls && sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS &&
1139 maxp < sc->params.tp.max_rx_pdu)
1140 maxp = sc->params.tp.max_rx_pdu;
1141 return (maxp);
1142 }
1143
1144 int
t4_setup_vi_queues(struct vi_info * vi)1145 t4_setup_vi_queues(struct vi_info *vi)
1146 {
1147 int rc = 0, i, intr_idx;
1148 struct sge_rxq *rxq;
1149 struct sge_txq *txq;
1150 #ifdef TCP_OFFLOAD
1151 struct sge_ofld_rxq *ofld_rxq;
1152 #endif
1153 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1154 struct sge_ofld_txq *ofld_txq;
1155 #endif
1156 #ifdef DEV_NETMAP
1157 int saved_idx, iqidx;
1158 struct sge_nm_rxq *nm_rxq;
1159 struct sge_nm_txq *nm_txq;
1160 #endif
1161 struct adapter *sc = vi->adapter;
1162 if_t ifp = vi->ifp;
1163 int maxp;
1164
1165 /* Interrupt vector to start from (when using multiple vectors) */
1166 intr_idx = vi->first_intr;
1167
1168 #ifdef DEV_NETMAP
1169 saved_idx = intr_idx;
1170 if (if_getcapabilities(ifp) & IFCAP_NETMAP) {
1171
1172 /* netmap is supported with direct interrupts only. */
1173 MPASS(!forwarding_intr_to_fwq(sc));
1174 MPASS(vi->first_intr >= 0);
1175
1176 /*
1177 * We don't have buffers to back the netmap rx queues
1178 * right now so we create the queues in a way that
1179 * doesn't set off any congestion signal in the chip.
1180 */
1181 for_each_nm_rxq(vi, i, nm_rxq) {
1182 rc = alloc_nm_rxq(vi, nm_rxq, intr_idx, i);
1183 if (rc != 0)
1184 goto done;
1185 intr_idx++;
1186 }
1187
1188 for_each_nm_txq(vi, i, nm_txq) {
1189 iqidx = vi->first_nm_rxq + (i % vi->nnmrxq);
1190 rc = alloc_nm_txq(vi, nm_txq, iqidx, i);
1191 if (rc != 0)
1192 goto done;
1193 }
1194 }
1195
1196 /* Normal rx queues and netmap rx queues share the same interrupts. */
1197 intr_idx = saved_idx;
1198 #endif
1199
1200 /*
1201 * Allocate rx queues first because a default iqid is required when
1202 * creating a tx queue.
1203 */
1204 maxp = max_rx_payload(sc, ifp, false);
1205 for_each_rxq(vi, i, rxq) {
1206 rc = alloc_rxq(vi, rxq, i, intr_idx, maxp);
1207 if (rc != 0)
1208 goto done;
1209 if (!forwarding_intr_to_fwq(sc))
1210 intr_idx++;
1211 }
1212 #ifdef DEV_NETMAP
1213 if (if_getcapabilities(ifp) & IFCAP_NETMAP)
1214 intr_idx = saved_idx + max(vi->nrxq, vi->nnmrxq);
1215 #endif
1216 #ifdef TCP_OFFLOAD
1217 maxp = max_rx_payload(sc, ifp, true);
1218 for_each_ofld_rxq(vi, i, ofld_rxq) {
1219 rc = alloc_ofld_rxq(vi, ofld_rxq, i, intr_idx, maxp);
1220 if (rc != 0)
1221 goto done;
1222 if (!forwarding_intr_to_fwq(sc))
1223 intr_idx++;
1224 }
1225 #endif
1226
1227 /*
1228 * Now the tx queues.
1229 */
1230 for_each_txq(vi, i, txq) {
1231 rc = alloc_txq(vi, txq, i);
1232 if (rc != 0)
1233 goto done;
1234 }
1235 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1236 for_each_ofld_txq(vi, i, ofld_txq) {
1237 rc = alloc_ofld_txq(vi, ofld_txq, i);
1238 if (rc != 0)
1239 goto done;
1240 }
1241 #endif
1242 done:
1243 if (rc)
1244 t4_teardown_vi_queues(vi);
1245
1246 return (rc);
1247 }
1248
1249 /*
1250 * Idempotent
1251 */
1252 int
t4_teardown_vi_queues(struct vi_info * vi)1253 t4_teardown_vi_queues(struct vi_info *vi)
1254 {
1255 int i;
1256 struct sge_rxq *rxq;
1257 struct sge_txq *txq;
1258 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1259 struct sge_ofld_txq *ofld_txq;
1260 #endif
1261 #ifdef TCP_OFFLOAD
1262 struct sge_ofld_rxq *ofld_rxq;
1263 #endif
1264 #ifdef DEV_NETMAP
1265 struct sge_nm_rxq *nm_rxq;
1266 struct sge_nm_txq *nm_txq;
1267 #endif
1268
1269 #ifdef DEV_NETMAP
1270 if (if_getcapabilities(vi->ifp) & IFCAP_NETMAP) {
1271 for_each_nm_txq(vi, i, nm_txq) {
1272 free_nm_txq(vi, nm_txq);
1273 }
1274
1275 for_each_nm_rxq(vi, i, nm_rxq) {
1276 free_nm_rxq(vi, nm_rxq);
1277 }
1278 }
1279 #endif
1280
1281 /*
1282 * Take down all the tx queues first, as they reference the rx queues
1283 * (for egress updates, etc.).
1284 */
1285
1286 for_each_txq(vi, i, txq) {
1287 free_txq(vi, txq);
1288 }
1289 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1290 for_each_ofld_txq(vi, i, ofld_txq) {
1291 free_ofld_txq(vi, ofld_txq);
1292 }
1293 #endif
1294
1295 /*
1296 * Then take down the rx queues.
1297 */
1298
1299 for_each_rxq(vi, i, rxq) {
1300 free_rxq(vi, rxq);
1301 }
1302 #ifdef TCP_OFFLOAD
1303 for_each_ofld_rxq(vi, i, ofld_rxq) {
1304 free_ofld_rxq(vi, ofld_rxq);
1305 }
1306 #endif
1307
1308 return (0);
1309 }
1310
1311 /*
1312 * Interrupt handler when the driver is using only 1 interrupt. This is a very
1313 * unusual scenario.
1314 *
1315 * a) Deals with errors, if any.
1316 * b) Services firmware event queue, which is taking interrupts for all other
1317 * queues.
1318 */
1319 void
t4_intr_all(void * arg)1320 t4_intr_all(void *arg)
1321 {
1322 struct adapter *sc = arg;
1323 struct sge_iq *fwq = &sc->sge.fwq;
1324
1325 MPASS(sc->intr_count == 1);
1326
1327 if (sc->intr_type == INTR_INTX)
1328 t4_write_reg(sc, MYPF_REG(A_PCIE_PF_CLI), 0);
1329
1330 t4_intr_err(arg);
1331 t4_intr_evt(fwq);
1332 }
1333
1334 /*
1335 * Interrupt handler for errors (installed directly when multiple interrupts are
1336 * being used, or called by t4_intr_all).
1337 */
1338 void
t4_intr_err(void * arg)1339 t4_intr_err(void *arg)
1340 {
1341 struct adapter *sc = arg;
1342 uint32_t v;
1343 const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
1344
1345 if (atomic_load_int(&sc->error_flags) & ADAP_FATAL_ERR)
1346 return;
1347
1348 v = t4_read_reg(sc, MYPF_REG(A_PL_PF_INT_CAUSE));
1349 if (v & F_PFSW) {
1350 sc->swintr++;
1351 t4_write_reg(sc, MYPF_REG(A_PL_PF_INT_CAUSE), v);
1352 }
1353
1354 if (t4_slow_intr_handler(sc, verbose))
1355 t4_fatal_err(sc, false);
1356 }
1357
1358 /*
1359 * Interrupt handler for iq-only queues. The firmware event queue is the only
1360 * such queue right now.
1361 */
1362 void
t4_intr_evt(void * arg)1363 t4_intr_evt(void *arg)
1364 {
1365 struct sge_iq *iq = arg;
1366
1367 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1368 service_iq(iq, 0);
1369 (void) atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1370 }
1371 }
1372
1373 /*
1374 * Interrupt handler for iq+fl queues.
1375 */
1376 void
t4_intr(void * arg)1377 t4_intr(void *arg)
1378 {
1379 struct sge_iq *iq = arg;
1380
1381 if (atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_BUSY)) {
1382 service_iq_fl(iq, 0);
1383 (void) atomic_cmpset_int(&iq->state, IQS_BUSY, IQS_IDLE);
1384 }
1385 }
1386
1387 #ifdef DEV_NETMAP
1388 /*
1389 * Interrupt handler for netmap rx queues.
1390 */
1391 void
t4_nm_intr(void * arg)1392 t4_nm_intr(void *arg)
1393 {
1394 struct sge_nm_rxq *nm_rxq = arg;
1395
1396 if (atomic_cmpset_int(&nm_rxq->nm_state, NM_ON, NM_BUSY)) {
1397 service_nm_rxq(nm_rxq);
1398 (void) atomic_cmpset_int(&nm_rxq->nm_state, NM_BUSY, NM_ON);
1399 }
1400 }
1401
1402 /*
1403 * Interrupt handler for vectors shared between NIC and netmap rx queues.
1404 */
1405 void
t4_vi_intr(void * arg)1406 t4_vi_intr(void *arg)
1407 {
1408 struct irq *irq = arg;
1409
1410 MPASS(irq->nm_rxq != NULL);
1411 t4_nm_intr(irq->nm_rxq);
1412
1413 MPASS(irq->rxq != NULL);
1414 t4_intr(irq->rxq);
1415 }
1416 #endif
1417
1418 /*
1419 * Deals with interrupts on an iq-only (no freelist) queue.
1420 */
1421 static int
service_iq(struct sge_iq * iq,int budget)1422 service_iq(struct sge_iq *iq, int budget)
1423 {
1424 struct sge_iq *q;
1425 struct adapter *sc = iq->adapter;
1426 struct iq_desc *d = &iq->desc[iq->cidx];
1427 int ndescs = 0, limit;
1428 int rsp_type;
1429 uint32_t lq;
1430 STAILQ_HEAD(, sge_iq) iql = STAILQ_HEAD_INITIALIZER(iql);
1431
1432 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
1433 KASSERT((iq->flags & IQ_HAS_FL) == 0,
1434 ("%s: called for iq %p with fl (iq->flags 0x%x)", __func__, iq,
1435 iq->flags));
1436 MPASS((iq->flags & IQ_ADJ_CREDIT) == 0);
1437 MPASS((iq->flags & IQ_LRO_ENABLED) == 0);
1438
1439 limit = budget ? budget : iq->qsize / 16;
1440
1441 /*
1442 * We always come back and check the descriptor ring for new indirect
1443 * interrupts and other responses after running a single handler.
1444 */
1445 for (;;) {
1446 while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
1447
1448 rmb();
1449
1450 rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
1451 lq = be32toh(d->rsp.pldbuflen_qid);
1452
1453 switch (rsp_type) {
1454 case X_RSPD_TYPE_FLBUF:
1455 panic("%s: data for an iq (%p) with no freelist",
1456 __func__, iq);
1457
1458 /* NOTREACHED */
1459
1460 case X_RSPD_TYPE_CPL:
1461 KASSERT(d->rss.opcode < NUM_CPL_CMDS,
1462 ("%s: bad opcode %02x.", __func__,
1463 d->rss.opcode));
1464 t4_cpl_handler[d->rss.opcode](iq, &d->rss, NULL);
1465 break;
1466
1467 case X_RSPD_TYPE_INTR:
1468 /*
1469 * There are 1K interrupt-capable queues (qids 0
1470 * through 1023). A response type indicating a
1471 * forwarded interrupt with a qid >= 1K is an
1472 * iWARP async notification.
1473 */
1474 if (__predict_true(lq >= 1024)) {
1475 t4_an_handler(iq, &d->rsp);
1476 break;
1477 }
1478
1479 q = sc->sge.iqmap[lq - sc->sge.iq_start -
1480 sc->sge.iq_base];
1481 if (atomic_cmpset_int(&q->state, IQS_IDLE,
1482 IQS_BUSY)) {
1483 if (service_iq_fl(q, q->qsize / 16) == 0) {
1484 (void) atomic_cmpset_int(&q->state,
1485 IQS_BUSY, IQS_IDLE);
1486 } else {
1487 STAILQ_INSERT_TAIL(&iql, q,
1488 link);
1489 }
1490 }
1491 break;
1492
1493 default:
1494 KASSERT(0,
1495 ("%s: illegal response type %d on iq %p",
1496 __func__, rsp_type, iq));
1497 log(LOG_ERR,
1498 "%s: illegal response type %d on iq %p",
1499 device_get_nameunit(sc->dev), rsp_type, iq);
1500 break;
1501 }
1502
1503 d++;
1504 if (__predict_false(++iq->cidx == iq->sidx)) {
1505 iq->cidx = 0;
1506 iq->gen ^= F_RSPD_GEN;
1507 d = &iq->desc[0];
1508 }
1509 if (__predict_false(++ndescs == limit)) {
1510 t4_write_reg(sc, sc->sge_gts_reg,
1511 V_CIDXINC(ndescs) |
1512 V_INGRESSQID(iq->cntxt_id) |
1513 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1514 ndescs = 0;
1515
1516 if (budget) {
1517 return (EINPROGRESS);
1518 }
1519 }
1520 }
1521
1522 if (STAILQ_EMPTY(&iql))
1523 break;
1524
1525 /*
1526 * Process the head only, and send it to the back of the list if
1527 * it's still not done.
1528 */
1529 q = STAILQ_FIRST(&iql);
1530 STAILQ_REMOVE_HEAD(&iql, link);
1531 if (service_iq_fl(q, q->qsize / 8) == 0)
1532 (void) atomic_cmpset_int(&q->state, IQS_BUSY, IQS_IDLE);
1533 else
1534 STAILQ_INSERT_TAIL(&iql, q, link);
1535 }
1536
1537 t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
1538 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1539
1540 return (0);
1541 }
1542
1543 #if defined(INET) || defined(INET6)
1544 static inline int
sort_before_lro(struct lro_ctrl * lro)1545 sort_before_lro(struct lro_ctrl *lro)
1546 {
1547
1548 return (lro->lro_mbuf_max != 0);
1549 }
1550 #endif
1551
1552 #define CGBE_SHIFT_SCALE 10
1553
1554 static inline uint64_t
t4_tstmp_to_ns(struct adapter * sc,uint64_t lf)1555 t4_tstmp_to_ns(struct adapter *sc, uint64_t lf)
1556 {
1557 struct clock_sync *cur, dcur;
1558 uint64_t hw_clocks;
1559 uint64_t hw_clk_div;
1560 sbintime_t sbt_cur_to_prev, sbt;
1561 uint64_t hw_tstmp = lf & 0xfffffffffffffffULL; /* 60b, not 64b. */
1562 seqc_t gen;
1563
1564 for (;;) {
1565 cur = &sc->cal_info[sc->cal_current];
1566 gen = seqc_read(&cur->gen);
1567 if (gen == 0)
1568 return (0);
1569 dcur = *cur;
1570 if (seqc_consistent(&cur->gen, gen))
1571 break;
1572 }
1573
1574 /*
1575 * Our goal here is to have a result that is:
1576 *
1577 * ( (cur_time - prev_time) )
1578 * ((hw_tstmp - hw_prev) * ----------------------------- ) + prev_time
1579 * ( (hw_cur - hw_prev) )
1580 *
1581 * With the constraints that we cannot use float and we
1582 * don't want to overflow the uint64_t numbers we are using.
1583 */
1584 hw_clocks = hw_tstmp - dcur.hw_prev;
1585 sbt_cur_to_prev = (dcur.sbt_cur - dcur.sbt_prev);
1586 hw_clk_div = dcur.hw_cur - dcur.hw_prev;
1587 sbt = hw_clocks * sbt_cur_to_prev / hw_clk_div + dcur.sbt_prev;
1588 return (sbttons(sbt));
1589 }
1590
1591 static inline void
move_to_next_rxbuf(struct sge_fl * fl)1592 move_to_next_rxbuf(struct sge_fl *fl)
1593 {
1594
1595 fl->rx_offset = 0;
1596 if (__predict_false((++fl->cidx & 7) == 0)) {
1597 uint16_t cidx = fl->cidx >> 3;
1598
1599 if (__predict_false(cidx == fl->sidx))
1600 fl->cidx = cidx = 0;
1601 fl->hw_cidx = cidx;
1602 }
1603 }
1604
1605 /*
1606 * Deals with interrupts on an iq+fl queue.
1607 */
1608 static int
service_iq_fl(struct sge_iq * iq,int budget)1609 service_iq_fl(struct sge_iq *iq, int budget)
1610 {
1611 struct sge_rxq *rxq = iq_to_rxq(iq);
1612 struct sge_fl *fl;
1613 struct adapter *sc = iq->adapter;
1614 struct iq_desc *d = &iq->desc[iq->cidx];
1615 int ndescs, limit;
1616 int rsp_type, starved;
1617 uint32_t lq;
1618 uint16_t fl_hw_cidx;
1619 struct mbuf *m0;
1620 #if defined(INET) || defined(INET6)
1621 const struct timeval lro_timeout = {0, sc->lro_timeout};
1622 struct lro_ctrl *lro = &rxq->lro;
1623 #endif
1624
1625 KASSERT(iq->state == IQS_BUSY, ("%s: iq %p not BUSY", __func__, iq));
1626 MPASS(iq->flags & IQ_HAS_FL);
1627
1628 ndescs = 0;
1629 #if defined(INET) || defined(INET6)
1630 if (iq->flags & IQ_ADJ_CREDIT) {
1631 MPASS(sort_before_lro(lro));
1632 iq->flags &= ~IQ_ADJ_CREDIT;
1633 if ((d->rsp.u.type_gen & F_RSPD_GEN) != iq->gen) {
1634 tcp_lro_flush_all(lro);
1635 t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(1) |
1636 V_INGRESSQID((u32)iq->cntxt_id) |
1637 V_SEINTARM(iq->intr_params));
1638 return (0);
1639 }
1640 ndescs = 1;
1641 }
1642 #else
1643 MPASS((iq->flags & IQ_ADJ_CREDIT) == 0);
1644 #endif
1645
1646 limit = budget ? budget : iq->qsize / 16;
1647 fl = &rxq->fl;
1648 fl_hw_cidx = fl->hw_cidx; /* stable snapshot */
1649 while ((d->rsp.u.type_gen & F_RSPD_GEN) == iq->gen) {
1650
1651 rmb();
1652
1653 m0 = NULL;
1654 rsp_type = G_RSPD_TYPE(d->rsp.u.type_gen);
1655 lq = be32toh(d->rsp.pldbuflen_qid);
1656
1657 switch (rsp_type) {
1658 case X_RSPD_TYPE_FLBUF:
1659 if (lq & F_RSPD_NEWBUF) {
1660 if (fl->rx_offset > 0)
1661 move_to_next_rxbuf(fl);
1662 lq = G_RSPD_LEN(lq);
1663 }
1664 if (IDXDIFF(fl->hw_cidx, fl_hw_cidx, fl->sidx) > 4) {
1665 FL_LOCK(fl);
1666 refill_fl(sc, fl, 64);
1667 FL_UNLOCK(fl);
1668 fl_hw_cidx = fl->hw_cidx;
1669 }
1670
1671 if (d->rss.opcode == CPL_RX_PKT) {
1672 if (__predict_true(eth_rx(sc, rxq, d, lq) == 0))
1673 break;
1674 goto out;
1675 }
1676 m0 = get_fl_payload(sc, fl, lq);
1677 if (__predict_false(m0 == NULL))
1678 goto out;
1679
1680 /* fall through */
1681
1682 case X_RSPD_TYPE_CPL:
1683 KASSERT(d->rss.opcode < NUM_CPL_CMDS,
1684 ("%s: bad opcode %02x.", __func__, d->rss.opcode));
1685 t4_cpl_handler[d->rss.opcode](iq, &d->rss, m0);
1686 break;
1687
1688 case X_RSPD_TYPE_INTR:
1689
1690 /*
1691 * There are 1K interrupt-capable queues (qids 0
1692 * through 1023). A response type indicating a
1693 * forwarded interrupt with a qid >= 1K is an
1694 * iWARP async notification. That is the only
1695 * acceptable indirect interrupt on this queue.
1696 */
1697 if (__predict_false(lq < 1024)) {
1698 panic("%s: indirect interrupt on iq_fl %p "
1699 "with qid %u", __func__, iq, lq);
1700 }
1701
1702 t4_an_handler(iq, &d->rsp);
1703 break;
1704
1705 default:
1706 KASSERT(0, ("%s: illegal response type %d on iq %p",
1707 __func__, rsp_type, iq));
1708 log(LOG_ERR, "%s: illegal response type %d on iq %p",
1709 device_get_nameunit(sc->dev), rsp_type, iq);
1710 break;
1711 }
1712
1713 d++;
1714 if (__predict_false(++iq->cidx == iq->sidx)) {
1715 iq->cidx = 0;
1716 iq->gen ^= F_RSPD_GEN;
1717 d = &iq->desc[0];
1718 }
1719 if (__predict_false(++ndescs == limit)) {
1720 t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
1721 V_INGRESSQID(iq->cntxt_id) |
1722 V_SEINTARM(V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX)));
1723
1724 #if defined(INET) || defined(INET6)
1725 if (iq->flags & IQ_LRO_ENABLED &&
1726 !sort_before_lro(lro) &&
1727 sc->lro_timeout != 0) {
1728 tcp_lro_flush_inactive(lro, &lro_timeout);
1729 }
1730 #endif
1731 if (budget)
1732 return (EINPROGRESS);
1733 ndescs = 0;
1734 }
1735 }
1736 out:
1737 #if defined(INET) || defined(INET6)
1738 if (iq->flags & IQ_LRO_ENABLED) {
1739 if (ndescs > 0 && lro->lro_mbuf_count > 8) {
1740 MPASS(sort_before_lro(lro));
1741 /* hold back one credit and don't flush LRO state */
1742 iq->flags |= IQ_ADJ_CREDIT;
1743 ndescs--;
1744 } else {
1745 tcp_lro_flush_all(lro);
1746 }
1747 }
1748 #endif
1749
1750 t4_write_reg(sc, sc->sge_gts_reg, V_CIDXINC(ndescs) |
1751 V_INGRESSQID((u32)iq->cntxt_id) | V_SEINTARM(iq->intr_params));
1752
1753 FL_LOCK(fl);
1754 starved = refill_fl(sc, fl, 64);
1755 FL_UNLOCK(fl);
1756 if (__predict_false(starved != 0))
1757 add_fl_to_sfl(sc, fl);
1758
1759 return (0);
1760 }
1761
1762 static inline struct cluster_metadata *
cl_metadata(struct fl_sdesc * sd)1763 cl_metadata(struct fl_sdesc *sd)
1764 {
1765
1766 return ((void *)(sd->cl + sd->moff));
1767 }
1768
1769 static void
rxb_free(struct mbuf * m)1770 rxb_free(struct mbuf *m)
1771 {
1772 struct cluster_metadata *clm = m->m_ext.ext_arg1;
1773
1774 uma_zfree(clm->zone, clm->cl);
1775 counter_u64_add(extfree_rels, 1);
1776 }
1777
1778 /*
1779 * The mbuf returned comes from zone_muf and carries the payload in one of these
1780 * ways
1781 * a) complete frame inside the mbuf
1782 * b) m_cljset (for clusters without metadata)
1783 * d) m_extaddref (cluster with metadata)
1784 */
1785 static struct mbuf *
get_scatter_segment(struct adapter * sc,struct sge_fl * fl,int fr_offset,int remaining)1786 get_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
1787 int remaining)
1788 {
1789 struct mbuf *m;
1790 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1791 struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
1792 struct cluster_metadata *clm;
1793 int len, blen;
1794 caddr_t payload;
1795
1796 if (fl->flags & FL_BUF_PACKING) {
1797 u_int l, pad;
1798
1799 blen = rxb->size2 - fl->rx_offset; /* max possible in this buf */
1800 len = min(remaining, blen);
1801 payload = sd->cl + fl->rx_offset;
1802
1803 l = fr_offset + len;
1804 pad = roundup2(l, fl->buf_boundary) - l;
1805 if (fl->rx_offset + len + pad < rxb->size2)
1806 blen = len + pad;
1807 MPASS(fl->rx_offset + blen <= rxb->size2);
1808 } else {
1809 MPASS(fl->rx_offset == 0); /* not packing */
1810 blen = rxb->size1;
1811 len = min(remaining, blen);
1812 payload = sd->cl;
1813 }
1814
1815 if (fr_offset == 0) {
1816 m = m_gethdr(M_NOWAIT, MT_DATA);
1817 if (__predict_false(m == NULL))
1818 return (NULL);
1819 m->m_pkthdr.len = remaining;
1820 } else {
1821 m = m_get(M_NOWAIT, MT_DATA);
1822 if (__predict_false(m == NULL))
1823 return (NULL);
1824 }
1825 m->m_len = len;
1826 kmsan_mark(payload, len, KMSAN_STATE_INITED);
1827
1828 if (sc->sc_do_rxcopy && len < RX_COPY_THRESHOLD) {
1829 /* copy data to mbuf */
1830 bcopy(payload, mtod(m, caddr_t), len);
1831 if (fl->flags & FL_BUF_PACKING) {
1832 fl->rx_offset += blen;
1833 MPASS(fl->rx_offset <= rxb->size2);
1834 if (fl->rx_offset < rxb->size2)
1835 return (m); /* without advancing the cidx */
1836 }
1837 } else if (fl->flags & FL_BUF_PACKING) {
1838 clm = cl_metadata(sd);
1839 if (sd->nmbuf++ == 0) {
1840 clm->refcount = 1;
1841 clm->zone = rxb->zone;
1842 clm->cl = sd->cl;
1843 counter_u64_add(extfree_refs, 1);
1844 }
1845 m_extaddref(m, payload, blen, &clm->refcount, rxb_free, clm,
1846 NULL);
1847
1848 fl->rx_offset += blen;
1849 MPASS(fl->rx_offset <= rxb->size2);
1850 if (fl->rx_offset < rxb->size2)
1851 return (m); /* without advancing the cidx */
1852 } else {
1853 m_cljset(m, sd->cl, rxb->type);
1854 sd->cl = NULL; /* consumed, not a recycle candidate */
1855 }
1856
1857 move_to_next_rxbuf(fl);
1858
1859 return (m);
1860 }
1861
1862 static struct mbuf *
get_fl_payload(struct adapter * sc,struct sge_fl * fl,const u_int plen)1863 get_fl_payload(struct adapter *sc, struct sge_fl *fl, const u_int plen)
1864 {
1865 struct mbuf *m0, *m, **pnext;
1866 u_int remaining;
1867
1868 if (__predict_false(fl->flags & FL_BUF_RESUME)) {
1869 M_ASSERTPKTHDR(fl->m0);
1870 MPASS(fl->m0->m_pkthdr.len == plen);
1871 MPASS(fl->remaining < plen);
1872
1873 m0 = fl->m0;
1874 pnext = fl->pnext;
1875 remaining = fl->remaining;
1876 fl->flags &= ~FL_BUF_RESUME;
1877 goto get_segment;
1878 }
1879
1880 /*
1881 * Payload starts at rx_offset in the current hw buffer. Its length is
1882 * 'len' and it may span multiple hw buffers.
1883 */
1884
1885 m0 = get_scatter_segment(sc, fl, 0, plen);
1886 if (m0 == NULL)
1887 return (NULL);
1888 remaining = plen - m0->m_len;
1889 pnext = &m0->m_next;
1890 while (remaining > 0) {
1891 get_segment:
1892 MPASS(fl->rx_offset == 0);
1893 m = get_scatter_segment(sc, fl, plen - remaining, remaining);
1894 if (__predict_false(m == NULL)) {
1895 fl->m0 = m0;
1896 fl->pnext = pnext;
1897 fl->remaining = remaining;
1898 fl->flags |= FL_BUF_RESUME;
1899 return (NULL);
1900 }
1901 *pnext = m;
1902 pnext = &m->m_next;
1903 remaining -= m->m_len;
1904 }
1905 *pnext = NULL;
1906
1907 M_ASSERTPKTHDR(m0);
1908 return (m0);
1909 }
1910
1911 static int
skip_scatter_segment(struct adapter * sc,struct sge_fl * fl,int fr_offset,int remaining)1912 skip_scatter_segment(struct adapter *sc, struct sge_fl *fl, int fr_offset,
1913 int remaining)
1914 {
1915 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1916 struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
1917 int len, blen;
1918
1919 if (fl->flags & FL_BUF_PACKING) {
1920 u_int l, pad;
1921
1922 blen = rxb->size2 - fl->rx_offset; /* max possible in this buf */
1923 len = min(remaining, blen);
1924
1925 l = fr_offset + len;
1926 pad = roundup2(l, fl->buf_boundary) - l;
1927 if (fl->rx_offset + len + pad < rxb->size2)
1928 blen = len + pad;
1929 fl->rx_offset += blen;
1930 MPASS(fl->rx_offset <= rxb->size2);
1931 if (fl->rx_offset < rxb->size2)
1932 return (len); /* without advancing the cidx */
1933 } else {
1934 MPASS(fl->rx_offset == 0); /* not packing */
1935 blen = rxb->size1;
1936 len = min(remaining, blen);
1937 }
1938 move_to_next_rxbuf(fl);
1939 return (len);
1940 }
1941
1942 static inline void
skip_fl_payload(struct adapter * sc,struct sge_fl * fl,int plen)1943 skip_fl_payload(struct adapter *sc, struct sge_fl *fl, int plen)
1944 {
1945 int remaining, fr_offset, len;
1946
1947 fr_offset = 0;
1948 remaining = plen;
1949 while (remaining > 0) {
1950 len = skip_scatter_segment(sc, fl, fr_offset, remaining);
1951 fr_offset += len;
1952 remaining -= len;
1953 }
1954 }
1955
1956 static inline int
get_segment_len(struct adapter * sc,struct sge_fl * fl,int plen)1957 get_segment_len(struct adapter *sc, struct sge_fl *fl, int plen)
1958 {
1959 int len;
1960 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
1961 struct rx_buf_info *rxb = &sc->sge.rx_buf_info[sd->zidx];
1962
1963 if (fl->flags & FL_BUF_PACKING)
1964 len = rxb->size2 - fl->rx_offset;
1965 else
1966 len = rxb->size1;
1967
1968 return (min(plen, len));
1969 }
1970
1971 static int
eth_rx(struct adapter * sc,struct sge_rxq * rxq,const struct iq_desc * d,u_int plen)1972 eth_rx(struct adapter *sc, struct sge_rxq *rxq, const struct iq_desc *d,
1973 u_int plen)
1974 {
1975 struct mbuf *m0;
1976 if_t ifp = rxq->ifp;
1977 struct sge_fl *fl = &rxq->fl;
1978 struct vi_info *vi = if_getsoftc(ifp);
1979 const struct cpl_rx_pkt *cpl;
1980 #if defined(INET) || defined(INET6)
1981 struct lro_ctrl *lro = &rxq->lro;
1982 #endif
1983 uint16_t err_vec, tnl_type, tnlhdr_len;
1984 static const int sw_hashtype[4][2] = {
1985 {M_HASHTYPE_NONE, M_HASHTYPE_NONE},
1986 {M_HASHTYPE_RSS_IPV4, M_HASHTYPE_RSS_IPV6},
1987 {M_HASHTYPE_RSS_TCP_IPV4, M_HASHTYPE_RSS_TCP_IPV6},
1988 {M_HASHTYPE_RSS_UDP_IPV4, M_HASHTYPE_RSS_UDP_IPV6},
1989 };
1990 static const int sw_csum_flags[2][2] = {
1991 {
1992 /* IP, inner IP */
1993 CSUM_ENCAP_VXLAN |
1994 CSUM_L3_CALC | CSUM_L3_VALID |
1995 CSUM_L4_CALC | CSUM_L4_VALID |
1996 CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
1997 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
1998
1999 /* IP, inner IP6 */
2000 CSUM_ENCAP_VXLAN |
2001 CSUM_L3_CALC | CSUM_L3_VALID |
2002 CSUM_L4_CALC | CSUM_L4_VALID |
2003 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
2004 },
2005 {
2006 /* IP6, inner IP */
2007 CSUM_ENCAP_VXLAN |
2008 CSUM_L4_CALC | CSUM_L4_VALID |
2009 CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
2010 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
2011
2012 /* IP6, inner IP6 */
2013 CSUM_ENCAP_VXLAN |
2014 CSUM_L4_CALC | CSUM_L4_VALID |
2015 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID,
2016 },
2017 };
2018
2019 MPASS(plen > sc->params.sge.fl_pktshift);
2020 if (vi->pfil != NULL && PFIL_HOOKED_IN(vi->pfil) &&
2021 __predict_true((fl->flags & FL_BUF_RESUME) == 0)) {
2022 struct fl_sdesc *sd = &fl->sdesc[fl->cidx];
2023 caddr_t frame;
2024 int rc, slen;
2025
2026 slen = get_segment_len(sc, fl, plen) -
2027 sc->params.sge.fl_pktshift;
2028 frame = sd->cl + fl->rx_offset + sc->params.sge.fl_pktshift;
2029 CURVNET_SET_QUIET(if_getvnet(ifp));
2030 rc = pfil_mem_in(vi->pfil, frame, slen, ifp, &m0);
2031 CURVNET_RESTORE();
2032 if (rc == PFIL_DROPPED || rc == PFIL_CONSUMED) {
2033 skip_fl_payload(sc, fl, plen);
2034 return (0);
2035 }
2036 if (rc == PFIL_REALLOCED) {
2037 skip_fl_payload(sc, fl, plen);
2038 goto have_mbuf;
2039 }
2040 }
2041
2042 m0 = get_fl_payload(sc, fl, plen);
2043 if (__predict_false(m0 == NULL))
2044 return (ENOMEM);
2045
2046 m0->m_pkthdr.len -= sc->params.sge.fl_pktshift;
2047 m0->m_len -= sc->params.sge.fl_pktshift;
2048 m0->m_data += sc->params.sge.fl_pktshift;
2049
2050 have_mbuf:
2051 m0->m_pkthdr.rcvif = ifp;
2052 M_HASHTYPE_SET(m0, sw_hashtype[d->rss.hash_type][d->rss.ipv6]);
2053 m0->m_pkthdr.flowid = be32toh(d->rss.hash_val);
2054
2055 cpl = (const void *)(&d->rss + 1);
2056 if (sc->params.tp.rx_pkt_encap) {
2057 const uint16_t ev = be16toh(cpl->err_vec);
2058
2059 err_vec = G_T6_COMPR_RXERR_VEC(ev);
2060 tnl_type = G_T6_RX_TNL_TYPE(ev);
2061 tnlhdr_len = G_T6_RX_TNLHDR_LEN(ev);
2062 } else {
2063 err_vec = be16toh(cpl->err_vec);
2064 tnl_type = 0;
2065 tnlhdr_len = 0;
2066 }
2067 if (cpl->csum_calc && err_vec == 0) {
2068 int ipv6 = !!(cpl->l2info & htobe32(F_RXF_IP6));
2069
2070 /* checksum(s) calculated and found to be correct. */
2071
2072 MPASS((cpl->l2info & htobe32(F_RXF_IP)) ^
2073 (cpl->l2info & htobe32(F_RXF_IP6)));
2074 m0->m_pkthdr.csum_data = be16toh(cpl->csum);
2075 if (tnl_type == 0) {
2076 if (!ipv6 && if_getcapenable(ifp) & IFCAP_RXCSUM) {
2077 m0->m_pkthdr.csum_flags = CSUM_L3_CALC |
2078 CSUM_L3_VALID | CSUM_L4_CALC |
2079 CSUM_L4_VALID;
2080 } else if (ipv6 && if_getcapenable(ifp) & IFCAP_RXCSUM_IPV6) {
2081 m0->m_pkthdr.csum_flags = CSUM_L4_CALC |
2082 CSUM_L4_VALID;
2083 }
2084 rxq->rxcsum++;
2085 } else {
2086 MPASS(tnl_type == RX_PKT_TNL_TYPE_VXLAN);
2087
2088 M_HASHTYPE_SETINNER(m0);
2089 if (__predict_false(cpl->ip_frag)) {
2090 /*
2091 * csum_data is for the inner frame (which is an
2092 * IP fragment) and is not 0xffff. There is no
2093 * way to pass the inner csum_data to the stack.
2094 * We don't want the stack to use the inner
2095 * csum_data to validate the outer frame or it
2096 * will get rejected. So we fix csum_data here
2097 * and let sw do the checksum of inner IP
2098 * fragments.
2099 *
2100 * XXX: Need 32b for csum_data2 in an rx mbuf.
2101 * Maybe stuff it into rcv_tstmp?
2102 */
2103 m0->m_pkthdr.csum_data = 0xffff;
2104 if (ipv6) {
2105 m0->m_pkthdr.csum_flags = CSUM_L4_CALC |
2106 CSUM_L4_VALID;
2107 } else {
2108 m0->m_pkthdr.csum_flags = CSUM_L3_CALC |
2109 CSUM_L3_VALID | CSUM_L4_CALC |
2110 CSUM_L4_VALID;
2111 }
2112 } else {
2113 int outer_ipv6;
2114
2115 MPASS(m0->m_pkthdr.csum_data == 0xffff);
2116
2117 outer_ipv6 = tnlhdr_len >=
2118 sizeof(struct ether_header) +
2119 sizeof(struct ip6_hdr);
2120 m0->m_pkthdr.csum_flags =
2121 sw_csum_flags[outer_ipv6][ipv6];
2122 }
2123 rxq->vxlan_rxcsum++;
2124 }
2125 }
2126
2127 if (cpl->vlan_ex) {
2128 if (sc->flags & IS_VF && sc->vlan_id) {
2129 /*
2130 * HW is not setup correctly if extracted vlan_id does
2131 * not match the VF's setting.
2132 */
2133 MPASS(be16toh(cpl->vlan) == sc->vlan_id);
2134 } else {
2135 m0->m_pkthdr.ether_vtag = be16toh(cpl->vlan);
2136 m0->m_flags |= M_VLANTAG;
2137 rxq->vlan_extraction++;
2138 }
2139 }
2140
2141 if (rxq->iq.flags & IQ_RX_TIMESTAMP) {
2142 /*
2143 * Fill up rcv_tstmp but do not set M_TSTMP as
2144 * long as we get a non-zero back from t4_tstmp_to_ns().
2145 */
2146 m0->m_pkthdr.rcv_tstmp = t4_tstmp_to_ns(sc,
2147 be64toh(d->rsp.u.last_flit));
2148 if (m0->m_pkthdr.rcv_tstmp != 0)
2149 m0->m_flags |= M_TSTMP;
2150 }
2151
2152 #ifdef NUMA
2153 m0->m_pkthdr.numa_domain = if_getnumadomain(ifp);
2154 #endif
2155 #if defined(INET) || defined(INET6)
2156 if (rxq->iq.flags & IQ_LRO_ENABLED && tnl_type == 0 &&
2157 (M_HASHTYPE_GET(m0) == M_HASHTYPE_RSS_TCP_IPV4 ||
2158 M_HASHTYPE_GET(m0) == M_HASHTYPE_RSS_TCP_IPV6)) {
2159 if (sort_before_lro(lro)) {
2160 tcp_lro_queue_mbuf(lro, m0);
2161 return (0); /* queued for sort, then LRO */
2162 }
2163 if (tcp_lro_rx(lro, m0, 0) == 0)
2164 return (0); /* queued for LRO */
2165 }
2166 #endif
2167 if_input(ifp, m0);
2168
2169 return (0);
2170 }
2171
2172 /*
2173 * Must drain the wrq or make sure that someone else will.
2174 */
2175 static void
wrq_tx_drain(void * arg,int n)2176 wrq_tx_drain(void *arg, int n)
2177 {
2178 struct sge_wrq *wrq = arg;
2179 struct sge_eq *eq = &wrq->eq;
2180
2181 EQ_LOCK(eq);
2182 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
2183 drain_wrq_wr_list(wrq->adapter, wrq);
2184 EQ_UNLOCK(eq);
2185 }
2186
2187 static void
drain_wrq_wr_list(struct adapter * sc,struct sge_wrq * wrq)2188 drain_wrq_wr_list(struct adapter *sc, struct sge_wrq *wrq)
2189 {
2190 struct sge_eq *eq = &wrq->eq;
2191 u_int available, dbdiff; /* # of hardware descriptors */
2192 u_int n;
2193 struct wrqe *wr;
2194 struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
2195
2196 EQ_LOCK_ASSERT_OWNED(eq);
2197 MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
2198 wr = STAILQ_FIRST(&wrq->wr_list);
2199 MPASS(wr != NULL); /* Must be called with something useful to do */
2200 MPASS(eq->pidx == eq->dbidx);
2201 dbdiff = 0;
2202
2203 do {
2204 eq->cidx = read_hw_cidx(eq);
2205 if (eq->pidx == eq->cidx)
2206 available = eq->sidx - 1;
2207 else
2208 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2209
2210 MPASS(wr->wrq == wrq);
2211 n = howmany(wr->wr_len, EQ_ESIZE);
2212 if (available < n)
2213 break;
2214
2215 dst = (void *)&eq->desc[eq->pidx];
2216 if (__predict_true(eq->sidx - eq->pidx > n)) {
2217 /* Won't wrap, won't end exactly at the status page. */
2218 bcopy(&wr->wr[0], dst, wr->wr_len);
2219 eq->pidx += n;
2220 } else {
2221 int first_portion = (eq->sidx - eq->pidx) * EQ_ESIZE;
2222
2223 bcopy(&wr->wr[0], dst, first_portion);
2224 if (wr->wr_len > first_portion) {
2225 bcopy(&wr->wr[first_portion], &eq->desc[0],
2226 wr->wr_len - first_portion);
2227 }
2228 eq->pidx = n - (eq->sidx - eq->pidx);
2229 }
2230 wrq->tx_wrs_copied++;
2231
2232 if (available < eq->sidx / 4 &&
2233 atomic_cmpset_int(&eq->equiq, 0, 1)) {
2234 /*
2235 * XXX: This is not 100% reliable with some
2236 * types of WRs. But this is a very unusual
2237 * situation for an ofld/ctrl queue anyway.
2238 */
2239 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
2240 F_FW_WR_EQUEQ);
2241 }
2242
2243 dbdiff += n;
2244 if (dbdiff >= 16) {
2245 ring_eq_db(sc, eq, dbdiff);
2246 dbdiff = 0;
2247 }
2248
2249 STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
2250 free_wrqe(wr);
2251 MPASS(wrq->nwr_pending > 0);
2252 wrq->nwr_pending--;
2253 MPASS(wrq->ndesc_needed >= n);
2254 wrq->ndesc_needed -= n;
2255 } while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL);
2256
2257 if (dbdiff)
2258 ring_eq_db(sc, eq, dbdiff);
2259 }
2260
2261 /*
2262 * Doesn't fail. Holds on to work requests it can't send right away.
2263 */
2264 void
t4_wrq_tx_locked(struct adapter * sc,struct sge_wrq * wrq,struct wrqe * wr)2265 t4_wrq_tx_locked(struct adapter *sc, struct sge_wrq *wrq, struct wrqe *wr)
2266 {
2267 #ifdef INVARIANTS
2268 struct sge_eq *eq = &wrq->eq;
2269 #endif
2270
2271 EQ_LOCK_ASSERT_OWNED(eq);
2272 MPASS(wr != NULL);
2273 MPASS(wr->wr_len > 0 && wr->wr_len <= SGE_MAX_WR_LEN);
2274 MPASS((wr->wr_len & 0x7) == 0);
2275
2276 STAILQ_INSERT_TAIL(&wrq->wr_list, wr, link);
2277 wrq->nwr_pending++;
2278 wrq->ndesc_needed += howmany(wr->wr_len, EQ_ESIZE);
2279
2280 if (!TAILQ_EMPTY(&wrq->incomplete_wrs))
2281 return; /* commit_wrq_wr will drain wr_list as well. */
2282
2283 drain_wrq_wr_list(sc, wrq);
2284
2285 /* Doorbell must have caught up to the pidx. */
2286 MPASS(eq->pidx == eq->dbidx);
2287 }
2288
2289 void
t4_update_fl_bufsize(if_t ifp)2290 t4_update_fl_bufsize(if_t ifp)
2291 {
2292 struct vi_info *vi = if_getsoftc(ifp);
2293 struct adapter *sc = vi->adapter;
2294 struct sge_rxq *rxq;
2295 #ifdef TCP_OFFLOAD
2296 struct sge_ofld_rxq *ofld_rxq;
2297 #endif
2298 struct sge_fl *fl;
2299 int i, maxp;
2300
2301 maxp = max_rx_payload(sc, ifp, false);
2302 for_each_rxq(vi, i, rxq) {
2303 fl = &rxq->fl;
2304
2305 FL_LOCK(fl);
2306 fl->zidx = find_refill_source(sc, maxp,
2307 fl->flags & FL_BUF_PACKING);
2308 FL_UNLOCK(fl);
2309 }
2310 #ifdef TCP_OFFLOAD
2311 maxp = max_rx_payload(sc, ifp, true);
2312 for_each_ofld_rxq(vi, i, ofld_rxq) {
2313 fl = &ofld_rxq->fl;
2314
2315 FL_LOCK(fl);
2316 fl->zidx = find_refill_source(sc, maxp,
2317 fl->flags & FL_BUF_PACKING);
2318 FL_UNLOCK(fl);
2319 }
2320 #endif
2321 }
2322
2323 #ifdef RATELIMIT
2324 static inline int
mbuf_eo_nsegs(struct mbuf * m)2325 mbuf_eo_nsegs(struct mbuf *m)
2326 {
2327
2328 M_ASSERTPKTHDR(m);
2329 return (m->m_pkthdr.PH_loc.eight[1]);
2330 }
2331
2332 #if defined(INET) || defined(INET6)
2333 static inline void
set_mbuf_eo_nsegs(struct mbuf * m,uint8_t nsegs)2334 set_mbuf_eo_nsegs(struct mbuf *m, uint8_t nsegs)
2335 {
2336
2337 M_ASSERTPKTHDR(m);
2338 m->m_pkthdr.PH_loc.eight[1] = nsegs;
2339 }
2340 #endif
2341
2342 static inline int
mbuf_eo_len16(struct mbuf * m)2343 mbuf_eo_len16(struct mbuf *m)
2344 {
2345 int n;
2346
2347 M_ASSERTPKTHDR(m);
2348 n = m->m_pkthdr.PH_loc.eight[2];
2349 MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16);
2350
2351 return (n);
2352 }
2353
2354 #if defined(INET) || defined(INET6)
2355 static inline void
set_mbuf_eo_len16(struct mbuf * m,uint8_t len16)2356 set_mbuf_eo_len16(struct mbuf *m, uint8_t len16)
2357 {
2358
2359 M_ASSERTPKTHDR(m);
2360 m->m_pkthdr.PH_loc.eight[2] = len16;
2361 }
2362 #endif
2363
2364 static inline int
mbuf_eo_tsclk_tsoff(struct mbuf * m)2365 mbuf_eo_tsclk_tsoff(struct mbuf *m)
2366 {
2367
2368 M_ASSERTPKTHDR(m);
2369 return (m->m_pkthdr.PH_loc.eight[3]);
2370 }
2371
2372 #if defined(INET) || defined(INET6)
2373 static inline void
set_mbuf_eo_tsclk_tsoff(struct mbuf * m,uint8_t tsclk_tsoff)2374 set_mbuf_eo_tsclk_tsoff(struct mbuf *m, uint8_t tsclk_tsoff)
2375 {
2376
2377 M_ASSERTPKTHDR(m);
2378 m->m_pkthdr.PH_loc.eight[3] = tsclk_tsoff;
2379 }
2380 #endif
2381
2382 static inline int
needs_eo(struct m_snd_tag * mst)2383 needs_eo(struct m_snd_tag *mst)
2384 {
2385
2386 return (mst != NULL && mst->sw->type == IF_SND_TAG_TYPE_RATE_LIMIT);
2387 }
2388 #endif
2389
2390 /*
2391 * Try to allocate an mbuf to contain a raw work request. To make it
2392 * easy to construct the work request, don't allocate a chain but a
2393 * single mbuf.
2394 */
2395 struct mbuf *
alloc_wr_mbuf(int len,int how)2396 alloc_wr_mbuf(int len, int how)
2397 {
2398 struct mbuf *m;
2399
2400 if (len <= MHLEN)
2401 m = m_gethdr(how, MT_DATA);
2402 else if (len <= MCLBYTES)
2403 m = m_getcl(how, MT_DATA, M_PKTHDR);
2404 else
2405 m = NULL;
2406 if (m == NULL)
2407 return (NULL);
2408 m->m_pkthdr.len = len;
2409 m->m_len = len;
2410 set_mbuf_cflags(m, MC_RAW_WR);
2411 set_mbuf_len16(m, howmany(len, 16));
2412 return (m);
2413 }
2414
2415 static inline bool
needs_hwcsum(struct mbuf * m)2416 needs_hwcsum(struct mbuf *m)
2417 {
2418 const uint32_t csum_flags = CSUM_IP | CSUM_IP_UDP | CSUM_IP_TCP |
2419 CSUM_IP_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2420 CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_IP6_UDP |
2421 CSUM_IP6_TCP | CSUM_IP6_TSO | CSUM_INNER_IP6_UDP |
2422 CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_TSO;
2423
2424 M_ASSERTPKTHDR(m);
2425
2426 return (m->m_pkthdr.csum_flags & csum_flags);
2427 }
2428
2429 static inline bool
needs_tso(struct mbuf * m)2430 needs_tso(struct mbuf *m)
2431 {
2432 const uint32_t csum_flags = CSUM_IP_TSO | CSUM_IP6_TSO |
2433 CSUM_INNER_IP_TSO | CSUM_INNER_IP6_TSO;
2434
2435 M_ASSERTPKTHDR(m);
2436
2437 return (m->m_pkthdr.csum_flags & csum_flags);
2438 }
2439
2440 static inline bool
needs_vxlan_csum(struct mbuf * m)2441 needs_vxlan_csum(struct mbuf *m)
2442 {
2443
2444 M_ASSERTPKTHDR(m);
2445
2446 return (m->m_pkthdr.csum_flags & CSUM_ENCAP_VXLAN);
2447 }
2448
2449 static inline bool
needs_vxlan_tso(struct mbuf * m)2450 needs_vxlan_tso(struct mbuf *m)
2451 {
2452 const uint32_t csum_flags = CSUM_ENCAP_VXLAN | CSUM_INNER_IP_TSO |
2453 CSUM_INNER_IP6_TSO;
2454
2455 M_ASSERTPKTHDR(m);
2456
2457 return ((m->m_pkthdr.csum_flags & csum_flags) != 0 &&
2458 (m->m_pkthdr.csum_flags & csum_flags) != CSUM_ENCAP_VXLAN);
2459 }
2460
2461 #if defined(INET) || defined(INET6)
2462 static inline bool
needs_inner_tcp_csum(struct mbuf * m)2463 needs_inner_tcp_csum(struct mbuf *m)
2464 {
2465 const uint32_t csum_flags = CSUM_INNER_IP_TSO | CSUM_INNER_IP6_TSO;
2466
2467 M_ASSERTPKTHDR(m);
2468
2469 return (m->m_pkthdr.csum_flags & csum_flags);
2470 }
2471 #endif
2472
2473 static inline bool
needs_l3_csum(struct mbuf * m)2474 needs_l3_csum(struct mbuf *m)
2475 {
2476 const uint32_t csum_flags = CSUM_IP | CSUM_IP_TSO | CSUM_INNER_IP |
2477 CSUM_INNER_IP_TSO;
2478
2479 M_ASSERTPKTHDR(m);
2480
2481 return (m->m_pkthdr.csum_flags & csum_flags);
2482 }
2483
2484 static inline bool
needs_outer_tcp_csum(struct mbuf * m)2485 needs_outer_tcp_csum(struct mbuf *m)
2486 {
2487 const uint32_t csum_flags = CSUM_IP_TCP | CSUM_IP_TSO | CSUM_IP6_TCP |
2488 CSUM_IP6_TSO;
2489
2490 M_ASSERTPKTHDR(m);
2491
2492 return (m->m_pkthdr.csum_flags & csum_flags);
2493 }
2494
2495 #ifdef RATELIMIT
2496 static inline bool
needs_outer_l4_csum(struct mbuf * m)2497 needs_outer_l4_csum(struct mbuf *m)
2498 {
2499 const uint32_t csum_flags = CSUM_IP_UDP | CSUM_IP_TCP | CSUM_IP_TSO |
2500 CSUM_IP6_UDP | CSUM_IP6_TCP | CSUM_IP6_TSO;
2501
2502 M_ASSERTPKTHDR(m);
2503
2504 return (m->m_pkthdr.csum_flags & csum_flags);
2505 }
2506
2507 static inline bool
needs_outer_udp_csum(struct mbuf * m)2508 needs_outer_udp_csum(struct mbuf *m)
2509 {
2510 const uint32_t csum_flags = CSUM_IP_UDP | CSUM_IP6_UDP;
2511
2512 M_ASSERTPKTHDR(m);
2513
2514 return (m->m_pkthdr.csum_flags & csum_flags);
2515 }
2516 #endif
2517
2518 static inline bool
needs_vlan_insertion(struct mbuf * m)2519 needs_vlan_insertion(struct mbuf *m)
2520 {
2521
2522 M_ASSERTPKTHDR(m);
2523
2524 return (m->m_flags & M_VLANTAG);
2525 }
2526
2527 #if defined(INET) || defined(INET6)
2528 static void *
m_advance(struct mbuf ** pm,int * poffset,int len)2529 m_advance(struct mbuf **pm, int *poffset, int len)
2530 {
2531 struct mbuf *m = *pm;
2532 int offset = *poffset;
2533 uintptr_t p = 0;
2534
2535 MPASS(len > 0);
2536
2537 for (;;) {
2538 if (offset + len < m->m_len) {
2539 offset += len;
2540 p = mtod(m, uintptr_t) + offset;
2541 break;
2542 }
2543 len -= m->m_len - offset;
2544 m = m->m_next;
2545 offset = 0;
2546 MPASS(m != NULL);
2547 }
2548 *poffset = offset;
2549 *pm = m;
2550 return ((void *)p);
2551 }
2552 #endif
2553
2554 static inline int
count_mbuf_ext_pgs(struct mbuf * m,int skip,vm_paddr_t * nextaddr)2555 count_mbuf_ext_pgs(struct mbuf *m, int skip, vm_paddr_t *nextaddr)
2556 {
2557 vm_paddr_t paddr;
2558 int i, len, off, pglen, pgoff, seglen, segoff;
2559 int nsegs = 0;
2560
2561 M_ASSERTEXTPG(m);
2562 off = mtod(m, vm_offset_t);
2563 len = m->m_len;
2564 off += skip;
2565 len -= skip;
2566
2567 if (m->m_epg_hdrlen != 0) {
2568 if (off >= m->m_epg_hdrlen) {
2569 off -= m->m_epg_hdrlen;
2570 } else {
2571 seglen = m->m_epg_hdrlen - off;
2572 segoff = off;
2573 seglen = min(seglen, len);
2574 off = 0;
2575 len -= seglen;
2576 paddr = pmap_kextract(
2577 (vm_offset_t)&m->m_epg_hdr[segoff]);
2578 if (*nextaddr != paddr)
2579 nsegs++;
2580 *nextaddr = paddr + seglen;
2581 }
2582 }
2583 pgoff = m->m_epg_1st_off;
2584 for (i = 0; i < m->m_epg_npgs && len > 0; i++) {
2585 pglen = m_epg_pagelen(m, i, pgoff);
2586 if (off >= pglen) {
2587 off -= pglen;
2588 pgoff = 0;
2589 continue;
2590 }
2591 seglen = pglen - off;
2592 segoff = pgoff + off;
2593 off = 0;
2594 seglen = min(seglen, len);
2595 len -= seglen;
2596 paddr = m->m_epg_pa[i] + segoff;
2597 if (*nextaddr != paddr)
2598 nsegs++;
2599 *nextaddr = paddr + seglen;
2600 pgoff = 0;
2601 };
2602 if (len != 0) {
2603 seglen = min(len, m->m_epg_trllen - off);
2604 len -= seglen;
2605 paddr = pmap_kextract((vm_offset_t)&m->m_epg_trail[off]);
2606 if (*nextaddr != paddr)
2607 nsegs++;
2608 *nextaddr = paddr + seglen;
2609 }
2610
2611 return (nsegs);
2612 }
2613
2614
2615 /*
2616 * Can deal with empty mbufs in the chain that have m_len = 0, but the chain
2617 * must have at least one mbuf that's not empty. It is possible for this
2618 * routine to return 0 if skip accounts for all the contents of the mbuf chain.
2619 */
2620 static inline int
count_mbuf_nsegs(struct mbuf * m,int skip,uint8_t * cflags)2621 count_mbuf_nsegs(struct mbuf *m, int skip, uint8_t *cflags)
2622 {
2623 vm_paddr_t nextaddr, paddr;
2624 vm_offset_t va;
2625 int len, nsegs;
2626
2627 M_ASSERTPKTHDR(m);
2628 MPASS(m->m_pkthdr.len > 0);
2629 MPASS(m->m_pkthdr.len >= skip);
2630
2631 nsegs = 0;
2632 nextaddr = 0;
2633 for (; m; m = m->m_next) {
2634 len = m->m_len;
2635 if (__predict_false(len == 0))
2636 continue;
2637 if (skip >= len) {
2638 skip -= len;
2639 continue;
2640 }
2641 if ((m->m_flags & M_EXTPG) != 0) {
2642 *cflags |= MC_NOMAP;
2643 nsegs += count_mbuf_ext_pgs(m, skip, &nextaddr);
2644 skip = 0;
2645 continue;
2646 }
2647 va = mtod(m, vm_offset_t) + skip;
2648 len -= skip;
2649 skip = 0;
2650 paddr = pmap_kextract(va);
2651 nsegs += sglist_count((void *)(uintptr_t)va, len);
2652 if (paddr == nextaddr)
2653 nsegs--;
2654 nextaddr = pmap_kextract(va + len - 1) + 1;
2655 }
2656
2657 return (nsegs);
2658 }
2659
2660 /*
2661 * The maximum number of segments that can fit in a WR.
2662 */
2663 static int
max_nsegs_allowed(struct mbuf * m,bool vm_wr)2664 max_nsegs_allowed(struct mbuf *m, bool vm_wr)
2665 {
2666
2667 if (vm_wr) {
2668 if (needs_tso(m))
2669 return (TX_SGL_SEGS_VM_TSO);
2670 return (TX_SGL_SEGS_VM);
2671 }
2672
2673 if (needs_tso(m)) {
2674 if (needs_vxlan_tso(m))
2675 return (TX_SGL_SEGS_VXLAN_TSO);
2676 else
2677 return (TX_SGL_SEGS_TSO);
2678 }
2679
2680 return (TX_SGL_SEGS);
2681 }
2682
2683 static struct timeval txerr_ratecheck = {0};
2684 static const struct timeval txerr_interval = {3, 0};
2685
2686 /*
2687 * Analyze the mbuf to determine its tx needs. The mbuf passed in may change:
2688 * a) caller can assume it's been freed if this function returns with an error.
2689 * b) it may get defragged up if the gather list is too long for the hardware.
2690 */
2691 int
parse_pkt(struct mbuf ** mp,bool vm_wr)2692 parse_pkt(struct mbuf **mp, bool vm_wr)
2693 {
2694 struct mbuf *m0 = *mp, *m;
2695 int rc, nsegs, defragged = 0;
2696 struct ether_header *eh;
2697 #ifdef INET
2698 void *l3hdr;
2699 #endif
2700 #if defined(INET) || defined(INET6)
2701 int offset;
2702 struct tcphdr *tcp;
2703 #endif
2704 #if defined(KERN_TLS) || defined(RATELIMIT)
2705 struct m_snd_tag *mst;
2706 #endif
2707 uint16_t eh_type;
2708 uint8_t cflags;
2709
2710 cflags = 0;
2711 M_ASSERTPKTHDR(m0);
2712 if (__predict_false(m0->m_pkthdr.len < ETHER_HDR_LEN)) {
2713 rc = EINVAL;
2714 fail:
2715 m_freem(m0);
2716 *mp = NULL;
2717 return (rc);
2718 }
2719 restart:
2720 /*
2721 * First count the number of gather list segments in the payload.
2722 * Defrag the mbuf if nsegs exceeds the hardware limit.
2723 */
2724 M_ASSERTPKTHDR(m0);
2725 MPASS(m0->m_pkthdr.len > 0);
2726 nsegs = count_mbuf_nsegs(m0, 0, &cflags);
2727 #if defined(KERN_TLS) || defined(RATELIMIT)
2728 if (m0->m_pkthdr.csum_flags & CSUM_SND_TAG)
2729 mst = m0->m_pkthdr.snd_tag;
2730 else
2731 mst = NULL;
2732 #endif
2733 #ifdef KERN_TLS
2734 if (mst != NULL && mst->sw->type == IF_SND_TAG_TYPE_TLS) {
2735 struct vi_info *vi = if_getsoftc(mst->ifp);
2736
2737 cflags |= MC_TLS;
2738 set_mbuf_cflags(m0, cflags);
2739 if (is_t6(vi->pi->adapter))
2740 rc = t6_ktls_parse_pkt(m0);
2741 else
2742 rc = t7_ktls_parse_pkt(m0);
2743 if (rc != 0)
2744 goto fail;
2745 return (EINPROGRESS);
2746 }
2747 #endif
2748 if (nsegs > max_nsegs_allowed(m0, vm_wr)) {
2749 if (defragged++ > 0) {
2750 rc = EFBIG;
2751 goto fail;
2752 }
2753 counter_u64_add(defrags, 1);
2754 if ((m = m_defrag(m0, M_NOWAIT)) == NULL) {
2755 rc = ENOMEM;
2756 goto fail;
2757 }
2758 *mp = m0 = m; /* update caller's copy after defrag */
2759 goto restart;
2760 }
2761
2762 if (__predict_false(nsegs > 2 && m0->m_pkthdr.len <= MHLEN &&
2763 !(cflags & MC_NOMAP))) {
2764 counter_u64_add(pullups, 1);
2765 m0 = m_pullup(m0, m0->m_pkthdr.len);
2766 if (m0 == NULL) {
2767 /* Should have left well enough alone. */
2768 rc = EFBIG;
2769 goto fail;
2770 }
2771 *mp = m0; /* update caller's copy after pullup */
2772 goto restart;
2773 }
2774 set_mbuf_nsegs(m0, nsegs);
2775 set_mbuf_cflags(m0, cflags);
2776 calculate_mbuf_len16(m0, vm_wr);
2777
2778 #ifdef RATELIMIT
2779 /*
2780 * Ethofld is limited to TCP and UDP for now, and only when L4 hw
2781 * checksumming is enabled. needs_outer_l4_csum happens to check for
2782 * all the right things.
2783 */
2784 if (__predict_false(needs_eo(mst) && !needs_outer_l4_csum(m0))) {
2785 m_snd_tag_rele(m0->m_pkthdr.snd_tag);
2786 m0->m_pkthdr.snd_tag = NULL;
2787 m0->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
2788 mst = NULL;
2789 }
2790 #endif
2791
2792 if (!needs_hwcsum(m0)
2793 #ifdef RATELIMIT
2794 && !needs_eo(mst)
2795 #endif
2796 )
2797 return (0);
2798
2799 m = m0;
2800 eh = mtod(m, struct ether_header *);
2801 eh_type = ntohs(eh->ether_type);
2802 if (eh_type == ETHERTYPE_VLAN) {
2803 struct ether_vlan_header *evh = (void *)eh;
2804
2805 eh_type = ntohs(evh->evl_proto);
2806 m0->m_pkthdr.l2hlen = sizeof(*evh);
2807 } else
2808 m0->m_pkthdr.l2hlen = sizeof(*eh);
2809
2810 #if defined(INET) || defined(INET6)
2811 offset = 0;
2812 #ifdef INET
2813 l3hdr = m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
2814 #else
2815 m_advance(&m, &offset, m0->m_pkthdr.l2hlen);
2816 #endif
2817 #endif
2818
2819 switch (eh_type) {
2820 #ifdef INET6
2821 case ETHERTYPE_IPV6:
2822 m0->m_pkthdr.l3hlen = sizeof(struct ip6_hdr);
2823 break;
2824 #endif
2825 #ifdef INET
2826 case ETHERTYPE_IP:
2827 {
2828 struct ip *ip = l3hdr;
2829
2830 if (needs_vxlan_csum(m0)) {
2831 /* Driver will do the outer IP hdr checksum. */
2832 ip->ip_sum = 0;
2833 if (needs_vxlan_tso(m0)) {
2834 const uint16_t ipl = ip->ip_len;
2835
2836 ip->ip_len = 0;
2837 ip->ip_sum = ~in_cksum_hdr(ip);
2838 ip->ip_len = ipl;
2839 } else
2840 ip->ip_sum = in_cksum_hdr(ip);
2841 }
2842 m0->m_pkthdr.l3hlen = ip->ip_hl << 2;
2843 break;
2844 }
2845 #endif
2846 default:
2847 if (ratecheck(&txerr_ratecheck, &txerr_interval)) {
2848 log(LOG_ERR, "%s: ethertype 0x%04x unknown. "
2849 "if_cxgbe must be compiled with the same "
2850 "INET/INET6 options as the kernel.\n", __func__,
2851 eh_type);
2852 }
2853 rc = EINVAL;
2854 goto fail;
2855 }
2856
2857 #if defined(INET) || defined(INET6)
2858 if (needs_vxlan_csum(m0)) {
2859 m0->m_pkthdr.l4hlen = sizeof(struct udphdr);
2860 m0->m_pkthdr.l5hlen = sizeof(struct vxlan_header);
2861
2862 /* Inner headers. */
2863 eh = m_advance(&m, &offset, m0->m_pkthdr.l3hlen +
2864 sizeof(struct udphdr) + sizeof(struct vxlan_header));
2865 eh_type = ntohs(eh->ether_type);
2866 if (eh_type == ETHERTYPE_VLAN) {
2867 struct ether_vlan_header *evh = (void *)eh;
2868
2869 eh_type = ntohs(evh->evl_proto);
2870 m0->m_pkthdr.inner_l2hlen = sizeof(*evh);
2871 } else
2872 m0->m_pkthdr.inner_l2hlen = sizeof(*eh);
2873 #ifdef INET
2874 l3hdr = m_advance(&m, &offset, m0->m_pkthdr.inner_l2hlen);
2875 #else
2876 m_advance(&m, &offset, m0->m_pkthdr.inner_l2hlen);
2877 #endif
2878
2879 switch (eh_type) {
2880 #ifdef INET6
2881 case ETHERTYPE_IPV6:
2882 m0->m_pkthdr.inner_l3hlen = sizeof(struct ip6_hdr);
2883 break;
2884 #endif
2885 #ifdef INET
2886 case ETHERTYPE_IP:
2887 {
2888 struct ip *ip = l3hdr;
2889
2890 m0->m_pkthdr.inner_l3hlen = ip->ip_hl << 2;
2891 break;
2892 }
2893 #endif
2894 default:
2895 if (ratecheck(&txerr_ratecheck, &txerr_interval)) {
2896 log(LOG_ERR, "%s: VXLAN hw offload requested"
2897 "with unknown ethertype 0x%04x. if_cxgbe "
2898 "must be compiled with the same INET/INET6 "
2899 "options as the kernel.\n", __func__,
2900 eh_type);
2901 }
2902 rc = EINVAL;
2903 goto fail;
2904 }
2905 if (needs_inner_tcp_csum(m0)) {
2906 tcp = m_advance(&m, &offset, m0->m_pkthdr.inner_l3hlen);
2907 m0->m_pkthdr.inner_l4hlen = tcp->th_off * 4;
2908 }
2909 MPASS((m0->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
2910 m0->m_pkthdr.csum_flags &= CSUM_INNER_IP6_UDP |
2911 CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_TSO | CSUM_INNER_IP |
2912 CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO |
2913 CSUM_ENCAP_VXLAN;
2914 }
2915
2916 if (needs_outer_tcp_csum(m0)) {
2917 tcp = m_advance(&m, &offset, m0->m_pkthdr.l3hlen);
2918 m0->m_pkthdr.l4hlen = tcp->th_off * 4;
2919 #ifdef RATELIMIT
2920 if (tsclk >= 0 && *(uint32_t *)(tcp + 1) == ntohl(0x0101080a)) {
2921 set_mbuf_eo_tsclk_tsoff(m0,
2922 V_FW_ETH_TX_EO_WR_TSCLK(tsclk) |
2923 V_FW_ETH_TX_EO_WR_TSOFF(sizeof(*tcp) / 2 + 1));
2924 } else
2925 set_mbuf_eo_tsclk_tsoff(m0, 0);
2926 } else if (needs_outer_udp_csum(m0)) {
2927 m0->m_pkthdr.l4hlen = sizeof(struct udphdr);
2928 #endif
2929 }
2930 #ifdef RATELIMIT
2931 if (needs_eo(mst)) {
2932 u_int immhdrs;
2933
2934 /* EO WRs have the headers in the WR and not the GL. */
2935 immhdrs = m0->m_pkthdr.l2hlen + m0->m_pkthdr.l3hlen +
2936 m0->m_pkthdr.l4hlen;
2937 cflags = 0;
2938 nsegs = count_mbuf_nsegs(m0, immhdrs, &cflags);
2939 MPASS(cflags == mbuf_cflags(m0));
2940 set_mbuf_eo_nsegs(m0, nsegs);
2941 set_mbuf_eo_len16(m0,
2942 txpkt_eo_len16(nsegs, immhdrs, needs_tso(m0)));
2943 rc = ethofld_transmit(mst->ifp, m0);
2944 if (rc != 0)
2945 goto fail;
2946 return (EINPROGRESS);
2947 }
2948 #endif
2949 #endif
2950 MPASS(m0 == *mp);
2951 return (0);
2952 }
2953
2954 void *
start_wrq_wr(struct sge_wrq * wrq,int len16,struct wrq_cookie * cookie)2955 start_wrq_wr(struct sge_wrq *wrq, int len16, struct wrq_cookie *cookie)
2956 {
2957 struct sge_eq *eq = &wrq->eq;
2958 struct adapter *sc = wrq->adapter;
2959 int ndesc, available;
2960 struct wrqe *wr;
2961 void *w;
2962
2963 MPASS(len16 > 0);
2964 ndesc = tx_len16_to_desc(len16);
2965 MPASS(ndesc > 0 && ndesc <= SGE_MAX_WR_NDESC);
2966
2967 EQ_LOCK(eq);
2968 if (__predict_false((eq->flags & EQ_HW_ALLOCATED) == 0)) {
2969 EQ_UNLOCK(eq);
2970 return (NULL);
2971 }
2972
2973 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
2974 drain_wrq_wr_list(sc, wrq);
2975
2976 if (!STAILQ_EMPTY(&wrq->wr_list)) {
2977 slowpath:
2978 EQ_UNLOCK(eq);
2979 wr = alloc_wrqe(len16 * 16, wrq);
2980 if (__predict_false(wr == NULL))
2981 return (NULL);
2982 cookie->pidx = -1;
2983 cookie->ndesc = ndesc;
2984 return (&wr->wr);
2985 }
2986
2987 eq->cidx = read_hw_cidx(eq);
2988 if (eq->pidx == eq->cidx)
2989 available = eq->sidx - 1;
2990 else
2991 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
2992 if (available < ndesc)
2993 goto slowpath;
2994
2995 cookie->pidx = eq->pidx;
2996 cookie->ndesc = ndesc;
2997 TAILQ_INSERT_TAIL(&wrq->incomplete_wrs, cookie, link);
2998
2999 w = &eq->desc[eq->pidx];
3000 IDXINCR(eq->pidx, ndesc, eq->sidx);
3001 if (__predict_false(cookie->pidx + ndesc > eq->sidx)) {
3002 w = &wrq->ss[0];
3003 wrq->ss_pidx = cookie->pidx;
3004 wrq->ss_len = len16 * 16;
3005 }
3006
3007 EQ_UNLOCK(eq);
3008
3009 return (w);
3010 }
3011
3012 void
commit_wrq_wr(struct sge_wrq * wrq,void * w,struct wrq_cookie * cookie)3013 commit_wrq_wr(struct sge_wrq *wrq, void *w, struct wrq_cookie *cookie)
3014 {
3015 struct sge_eq *eq = &wrq->eq;
3016 struct adapter *sc = wrq->adapter;
3017 int ndesc, pidx;
3018 struct wrq_cookie *prev, *next;
3019
3020 if (cookie->pidx == -1) {
3021 struct wrqe *wr = __containerof(w, struct wrqe, wr);
3022
3023 t4_wrq_tx(sc, wr);
3024 return;
3025 }
3026
3027 if (__predict_false(w == &wrq->ss[0])) {
3028 int n = (eq->sidx - wrq->ss_pidx) * EQ_ESIZE;
3029
3030 MPASS(wrq->ss_len > n); /* WR had better wrap around. */
3031 bcopy(&wrq->ss[0], &eq->desc[wrq->ss_pidx], n);
3032 bcopy(&wrq->ss[n], &eq->desc[0], wrq->ss_len - n);
3033 wrq->tx_wrs_ss++;
3034 } else
3035 wrq->tx_wrs_direct++;
3036
3037 EQ_LOCK(eq);
3038 ndesc = cookie->ndesc; /* Can be more than SGE_MAX_WR_NDESC here. */
3039 pidx = cookie->pidx;
3040 MPASS(pidx >= 0 && pidx < eq->sidx);
3041 prev = TAILQ_PREV(cookie, wrq_incomplete_wrs, link);
3042 next = TAILQ_NEXT(cookie, link);
3043 if (prev == NULL) {
3044 MPASS(pidx == eq->dbidx);
3045 if (next == NULL || ndesc >= 16) {
3046 int available;
3047 struct fw_eth_tx_pkt_wr *dst; /* any fw WR struct will do */
3048
3049 /*
3050 * Note that the WR via which we'll request tx updates
3051 * is at pidx and not eq->pidx, which has moved on
3052 * already.
3053 */
3054 dst = (void *)&eq->desc[pidx];
3055 available = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
3056 if (available < eq->sidx / 4 &&
3057 atomic_cmpset_int(&eq->equiq, 0, 1)) {
3058 /*
3059 * XXX: This is not 100% reliable with some
3060 * types of WRs. But this is a very unusual
3061 * situation for an ofld/ctrl queue anyway.
3062 */
3063 dst->equiq_to_len16 |= htobe32(F_FW_WR_EQUIQ |
3064 F_FW_WR_EQUEQ);
3065 }
3066
3067 if (__predict_true(eq->flags & EQ_HW_ALLOCATED))
3068 ring_eq_db(wrq->adapter, eq, ndesc);
3069 else
3070 IDXINCR(eq->dbidx, ndesc, eq->sidx);
3071 } else {
3072 MPASS(IDXDIFF(next->pidx, pidx, eq->sidx) == ndesc);
3073 next->pidx = pidx;
3074 next->ndesc += ndesc;
3075 }
3076 } else {
3077 MPASS(IDXDIFF(pidx, prev->pidx, eq->sidx) == prev->ndesc);
3078 prev->ndesc += ndesc;
3079 }
3080 TAILQ_REMOVE(&wrq->incomplete_wrs, cookie, link);
3081
3082 if (TAILQ_EMPTY(&wrq->incomplete_wrs) && !STAILQ_EMPTY(&wrq->wr_list))
3083 drain_wrq_wr_list(sc, wrq);
3084
3085 #ifdef INVARIANTS
3086 if (TAILQ_EMPTY(&wrq->incomplete_wrs)) {
3087 /* Doorbell must have caught up to the pidx. */
3088 MPASS(wrq->eq.pidx == wrq->eq.dbidx);
3089 }
3090 #endif
3091 EQ_UNLOCK(eq);
3092 }
3093
3094 static u_int
can_resume_eth_tx(struct mp_ring * r)3095 can_resume_eth_tx(struct mp_ring *r)
3096 {
3097 struct sge_eq *eq = r->cookie;
3098
3099 return (total_available_tx_desc(eq) > eq->sidx / 8);
3100 }
3101
3102 static inline bool
cannot_use_txpkts(struct mbuf * m)3103 cannot_use_txpkts(struct mbuf *m)
3104 {
3105 /* maybe put a GL limit too, to avoid silliness? */
3106
3107 return (needs_tso(m) || (mbuf_cflags(m) & (MC_RAW_WR | MC_TLS)) != 0);
3108 }
3109
3110 static inline int
discard_tx(struct sge_eq * eq)3111 discard_tx(struct sge_eq *eq)
3112 {
3113
3114 return ((eq->flags & (EQ_ENABLED | EQ_QFLUSH)) != EQ_ENABLED);
3115 }
3116
3117 static inline int
wr_can_update_eq(void * p)3118 wr_can_update_eq(void *p)
3119 {
3120 struct fw_eth_tx_pkts_wr *wr = p;
3121
3122 switch (G_FW_WR_OP(be32toh(wr->op_pkd))) {
3123 case FW_ULPTX_WR:
3124 case FW_ETH_TX_PKT_WR:
3125 case FW_ETH_TX_PKTS_WR:
3126 case FW_ETH_TX_PKTS2_WR:
3127 case FW_ETH_TX_PKT_VM_WR:
3128 case FW_ETH_TX_PKTS_VM_WR:
3129 return (1);
3130 default:
3131 return (0);
3132 }
3133 }
3134
3135 static inline void
set_txupdate_flags(struct sge_txq * txq,u_int avail,struct fw_eth_tx_pkt_wr * wr)3136 set_txupdate_flags(struct sge_txq *txq, u_int avail,
3137 struct fw_eth_tx_pkt_wr *wr)
3138 {
3139 struct sge_eq *eq = &txq->eq;
3140 struct txpkts *txp = &txq->txp;
3141
3142 if ((txp->npkt > 0 || avail < eq->sidx / 2) &&
3143 atomic_cmpset_int(&eq->equiq, 0, 1)) {
3144 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ | F_FW_WR_EQUIQ);
3145 eq->equeqidx = eq->pidx;
3146 } else if (IDXDIFF(eq->pidx, eq->equeqidx, eq->sidx) >= 32) {
3147 wr->equiq_to_len16 |= htobe32(F_FW_WR_EQUEQ);
3148 eq->equeqidx = eq->pidx;
3149 }
3150 }
3151
3152 #if defined(__i386__) || defined(__amd64__)
3153 extern uint64_t tsc_freq;
3154 #endif
3155
3156 static inline bool
record_eth_tx_time(struct sge_txq * txq)3157 record_eth_tx_time(struct sge_txq *txq)
3158 {
3159 const uint64_t cycles = get_cyclecount();
3160 const uint64_t last_tx = txq->last_tx;
3161 #if defined(__i386__) || defined(__amd64__)
3162 const uint64_t itg = tsc_freq * t4_tx_coalesce_gap / 1000000;
3163 #else
3164 const uint64_t itg = 0;
3165 #endif
3166
3167 MPASS(cycles >= last_tx);
3168 txq->last_tx = cycles;
3169 return (cycles - last_tx < itg);
3170 }
3171
3172 /*
3173 * r->items[cidx] to r->items[pidx], with a wraparound at r->size, are ready to
3174 * be consumed. Return the actual number consumed. 0 indicates a stall.
3175 */
3176 static u_int
eth_tx(struct mp_ring * r,u_int cidx,u_int pidx,bool * coalescing)3177 eth_tx(struct mp_ring *r, u_int cidx, u_int pidx, bool *coalescing)
3178 {
3179 struct sge_txq *txq = r->cookie;
3180 if_t ifp = txq->ifp;
3181 struct sge_eq *eq = &txq->eq;
3182 struct txpkts *txp = &txq->txp;
3183 struct vi_info *vi = if_getsoftc(ifp);
3184 struct adapter *sc = vi->adapter;
3185 u_int total, remaining; /* # of packets */
3186 u_int n, avail, dbdiff; /* # of hardware descriptors */
3187 int i, rc;
3188 struct mbuf *m0;
3189 bool snd, recent_tx;
3190 void *wr; /* start of the last WR written to the ring */
3191
3192 TXQ_LOCK_ASSERT_OWNED(txq);
3193 recent_tx = record_eth_tx_time(txq);
3194
3195 remaining = IDXDIFF(pidx, cidx, r->size);
3196 if (__predict_false(discard_tx(eq))) {
3197 for (i = 0; i < txp->npkt; i++)
3198 m_freem(txp->mb[i]);
3199 txp->npkt = 0;
3200 while (cidx != pidx) {
3201 m0 = r->items[cidx];
3202 m_freem(m0);
3203 if (++cidx == r->size)
3204 cidx = 0;
3205 }
3206 reclaim_tx_descs(txq, eq->sidx);
3207 *coalescing = false;
3208 return (remaining); /* emptied */
3209 }
3210
3211 /* How many hardware descriptors do we have readily available. */
3212 if (eq->pidx == eq->cidx)
3213 avail = eq->sidx - 1;
3214 else
3215 avail = IDXDIFF(eq->cidx, eq->pidx, eq->sidx) - 1;
3216
3217 total = 0;
3218 if (remaining == 0) {
3219 txp->score = 0;
3220 txq->txpkts_flush++;
3221 goto send_txpkts;
3222 }
3223
3224 dbdiff = 0;
3225 MPASS(remaining > 0);
3226 while (remaining > 0) {
3227 m0 = r->items[cidx];
3228 M_ASSERTPKTHDR(m0);
3229 MPASS(m0->m_nextpkt == NULL);
3230
3231 if (avail < 2 * SGE_MAX_WR_NDESC)
3232 avail += reclaim_tx_descs(txq, 64);
3233
3234 if (t4_tx_coalesce == 0 && txp->npkt == 0)
3235 goto skip_coalescing;
3236 if (cannot_use_txpkts(m0))
3237 txp->score = 0;
3238 else if (recent_tx) {
3239 if (++txp->score == 0)
3240 txp->score = UINT8_MAX;
3241 } else
3242 txp->score = 1;
3243 if (txp->npkt > 0 || remaining > 1 ||
3244 txp->score >= t4_tx_coalesce_pkts ||
3245 atomic_load_int(&txq->eq.equiq) != 0) {
3246 if (vi->flags & TX_USES_VM_WR)
3247 rc = add_to_txpkts_vf(sc, txq, m0, avail, &snd);
3248 else
3249 rc = add_to_txpkts_pf(sc, txq, m0, avail, &snd);
3250 } else {
3251 snd = false;
3252 rc = EINVAL;
3253 }
3254 if (snd) {
3255 MPASS(txp->npkt > 0);
3256 for (i = 0; i < txp->npkt; i++)
3257 ETHER_BPF_MTAP(ifp, txp->mb[i]);
3258 if (txp->npkt > 1) {
3259 MPASS(avail >= tx_len16_to_desc(txp->len16));
3260 if (vi->flags & TX_USES_VM_WR)
3261 n = write_txpkts_vm_wr(sc, txq);
3262 else
3263 n = write_txpkts_wr(sc, txq);
3264 } else {
3265 MPASS(avail >=
3266 tx_len16_to_desc(mbuf_len16(txp->mb[0])));
3267 if (vi->flags & TX_USES_VM_WR)
3268 n = write_txpkt_vm_wr(sc, txq,
3269 txp->mb[0]);
3270 else
3271 n = write_txpkt_wr(sc, txq, txp->mb[0],
3272 avail);
3273 }
3274 MPASS(n <= SGE_MAX_WR_NDESC);
3275 avail -= n;
3276 dbdiff += n;
3277 wr = &eq->desc[eq->pidx];
3278 IDXINCR(eq->pidx, n, eq->sidx);
3279 txp->npkt = 0; /* emptied */
3280 }
3281 if (rc == 0) {
3282 /* m0 was coalesced into txq->txpkts. */
3283 goto next_mbuf;
3284 }
3285 if (rc == EAGAIN) {
3286 /*
3287 * m0 is suitable for tx coalescing but could not be
3288 * combined with the existing txq->txpkts, which has now
3289 * been transmitted. Start a new txpkts with m0.
3290 */
3291 MPASS(snd);
3292 MPASS(txp->npkt == 0);
3293 continue;
3294 }
3295
3296 MPASS(rc != 0 && rc != EAGAIN);
3297 MPASS(txp->npkt == 0);
3298 skip_coalescing:
3299 n = tx_len16_to_desc(mbuf_len16(m0));
3300 if (__predict_false(avail < n)) {
3301 avail += reclaim_tx_descs(txq, min(n, 32));
3302 if (avail < n)
3303 break; /* out of descriptors */
3304 }
3305
3306 wr = &eq->desc[eq->pidx];
3307 if (mbuf_cflags(m0) & MC_RAW_WR) {
3308 n = write_raw_wr(txq, wr, m0, avail);
3309 #ifdef KERN_TLS
3310 } else if (mbuf_cflags(m0) & MC_TLS) {
3311 ETHER_BPF_MTAP(ifp, m0);
3312 if (is_t6(sc))
3313 n = t6_ktls_write_wr(txq, wr, m0, avail);
3314 else
3315 n = t7_ktls_write_wr(txq, wr, m0, avail);
3316 #endif
3317 } else {
3318 ETHER_BPF_MTAP(ifp, m0);
3319 if (vi->flags & TX_USES_VM_WR)
3320 n = write_txpkt_vm_wr(sc, txq, m0);
3321 else
3322 n = write_txpkt_wr(sc, txq, m0, avail);
3323 }
3324 MPASS(n >= 1 && n <= avail);
3325 if (!(mbuf_cflags(m0) & MC_TLS))
3326 MPASS(n <= SGE_MAX_WR_NDESC);
3327
3328 avail -= n;
3329 dbdiff += n;
3330 IDXINCR(eq->pidx, n, eq->sidx);
3331
3332 if (dbdiff >= 512 / EQ_ESIZE) { /* X_FETCHBURSTMAX_512B */
3333 if (wr_can_update_eq(wr))
3334 set_txupdate_flags(txq, avail, wr);
3335 ring_eq_db(sc, eq, dbdiff);
3336 avail += reclaim_tx_descs(txq, 32);
3337 dbdiff = 0;
3338 }
3339 next_mbuf:
3340 total++;
3341 remaining--;
3342 if (__predict_false(++cidx == r->size))
3343 cidx = 0;
3344 }
3345 if (dbdiff != 0) {
3346 if (wr_can_update_eq(wr))
3347 set_txupdate_flags(txq, avail, wr);
3348 ring_eq_db(sc, eq, dbdiff);
3349 reclaim_tx_descs(txq, 32);
3350 } else if (eq->pidx == eq->cidx && txp->npkt > 0 &&
3351 atomic_load_int(&txq->eq.equiq) == 0) {
3352 /*
3353 * If nothing was submitted to the chip for tx (it was coalesced
3354 * into txpkts instead) and there is no tx update outstanding
3355 * then we need to send txpkts now.
3356 */
3357 send_txpkts:
3358 MPASS(txp->npkt > 0);
3359 for (i = 0; i < txp->npkt; i++)
3360 ETHER_BPF_MTAP(ifp, txp->mb[i]);
3361 if (txp->npkt > 1) {
3362 MPASS(avail >= tx_len16_to_desc(txp->len16));
3363 if (vi->flags & TX_USES_VM_WR)
3364 n = write_txpkts_vm_wr(sc, txq);
3365 else
3366 n = write_txpkts_wr(sc, txq);
3367 } else {
3368 MPASS(avail >=
3369 tx_len16_to_desc(mbuf_len16(txp->mb[0])));
3370 if (vi->flags & TX_USES_VM_WR)
3371 n = write_txpkt_vm_wr(sc, txq, txp->mb[0]);
3372 else
3373 n = write_txpkt_wr(sc, txq, txp->mb[0], avail);
3374 }
3375 MPASS(n <= SGE_MAX_WR_NDESC);
3376 wr = &eq->desc[eq->pidx];
3377 IDXINCR(eq->pidx, n, eq->sidx);
3378 txp->npkt = 0; /* emptied */
3379
3380 MPASS(wr_can_update_eq(wr));
3381 set_txupdate_flags(txq, avail - n, wr);
3382 ring_eq_db(sc, eq, n);
3383 reclaim_tx_descs(txq, 32);
3384 }
3385 *coalescing = txp->npkt > 0;
3386
3387 return (total);
3388 }
3389
3390 static inline void
init_iq(struct sge_iq * iq,struct adapter * sc,int tmr_idx,int pktc_idx,int qsize,int intr_idx,int cong,int qtype)3391 init_iq(struct sge_iq *iq, struct adapter *sc, int tmr_idx, int pktc_idx,
3392 int qsize, int intr_idx, int cong, int qtype)
3393 {
3394
3395 KASSERT(tmr_idx >= 0 && tmr_idx < SGE_NTIMERS,
3396 ("%s: bad tmr_idx %d", __func__, tmr_idx));
3397 KASSERT(pktc_idx < SGE_NCOUNTERS, /* -ve is ok, means don't use */
3398 ("%s: bad pktc_idx %d", __func__, pktc_idx));
3399 KASSERT(intr_idx >= -1 && intr_idx < sc->intr_count,
3400 ("%s: bad intr_idx %d", __func__, intr_idx));
3401 KASSERT(qtype == FW_IQ_IQTYPE_OTHER || qtype == FW_IQ_IQTYPE_NIC ||
3402 qtype == FW_IQ_IQTYPE_OFLD, ("%s: bad qtype %d", __func__, qtype));
3403
3404 iq->flags = 0;
3405 iq->state = IQS_DISABLED;
3406 iq->adapter = sc;
3407 iq->qtype = qtype;
3408 iq->intr_params = V_QINTR_TIMER_IDX(tmr_idx);
3409 iq->intr_pktc_idx = SGE_NCOUNTERS - 1;
3410 if (pktc_idx >= 0) {
3411 iq->intr_params |= F_QINTR_CNT_EN;
3412 iq->intr_pktc_idx = pktc_idx;
3413 }
3414 iq->qsize = roundup2(qsize, 16); /* See FW_IQ_CMD/iqsize */
3415 iq->sidx = iq->qsize - sc->params.sge.spg_len / IQ_ESIZE;
3416 iq->intr_idx = intr_idx;
3417 iq->cong_drop = cong;
3418 }
3419
3420 static inline void
init_fl(struct adapter * sc,struct sge_fl * fl,int qsize,int maxp,char * name)3421 init_fl(struct adapter *sc, struct sge_fl *fl, int qsize, int maxp, char *name)
3422 {
3423 struct sge_params *sp = &sc->params.sge;
3424
3425 fl->qsize = qsize;
3426 fl->sidx = qsize - sc->params.sge.spg_len / EQ_ESIZE;
3427 strlcpy(fl->lockname, name, sizeof(fl->lockname));
3428 mtx_init(&fl->fl_lock, fl->lockname, NULL, MTX_DEF);
3429 if (sc->flags & BUF_PACKING_OK &&
3430 ((!is_t4(sc) && buffer_packing) || /* T5+: enabled unless 0 */
3431 (is_t4(sc) && buffer_packing == 1)))/* T4: disabled unless 1 */
3432 fl->flags |= FL_BUF_PACKING;
3433 fl->zidx = find_refill_source(sc, maxp, fl->flags & FL_BUF_PACKING);
3434 fl->safe_zidx = sc->sge.safe_zidx;
3435 if (fl->flags & FL_BUF_PACKING) {
3436 fl->lowat = roundup2(sp->fl_starve_threshold2, 8);
3437 fl->buf_boundary = sp->pack_boundary;
3438 } else {
3439 fl->lowat = roundup2(sp->fl_starve_threshold, 8);
3440 fl->buf_boundary = 16;
3441 }
3442 if (fl_pad && fl->buf_boundary < sp->pad_boundary)
3443 fl->buf_boundary = sp->pad_boundary;
3444 }
3445
3446 static inline void
init_eq(struct adapter * sc,struct sge_eq * eq,int eqtype,int qsize,uint8_t port_id,struct sge_iq * iq,char * name)3447 init_eq(struct adapter *sc, struct sge_eq *eq, int eqtype, int qsize,
3448 uint8_t port_id, struct sge_iq *iq, char *name)
3449 {
3450 KASSERT(eqtype >= EQ_CTRL && eqtype <= EQ_OFLD,
3451 ("%s: bad qtype %d", __func__, eqtype));
3452
3453 eq->type = eqtype;
3454 eq->port_id = port_id;
3455 eq->tx_chan = sc->port[port_id]->tx_chan;
3456 eq->hw_port = sc->port[port_id]->hw_port;
3457 eq->iq = iq;
3458 eq->sidx = qsize - sc->params.sge.spg_len / EQ_ESIZE;
3459 strlcpy(eq->lockname, name, sizeof(eq->lockname));
3460 mtx_init(&eq->eq_lock, eq->lockname, NULL, MTX_DEF);
3461 }
3462
3463 int
alloc_ring(struct adapter * sc,size_t len,bus_dma_tag_t * tag,bus_dmamap_t * map,bus_addr_t * pa,void ** va)3464 alloc_ring(struct adapter *sc, size_t len, bus_dma_tag_t *tag,
3465 bus_dmamap_t *map, bus_addr_t *pa, void **va)
3466 {
3467 int rc;
3468
3469 rc = bus_dma_tag_create(sc->dmat, 512, 0, BUS_SPACE_MAXADDR,
3470 BUS_SPACE_MAXADDR, NULL, NULL, len, 1, len, 0, NULL, NULL, tag);
3471 if (rc != 0) {
3472 CH_ERR(sc, "cannot allocate DMA tag: %d\n", rc);
3473 goto done;
3474 }
3475
3476 rc = bus_dmamem_alloc(*tag, va,
3477 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
3478 if (rc != 0) {
3479 CH_ERR(sc, "cannot allocate DMA memory: %d\n", rc);
3480 goto done;
3481 }
3482
3483 rc = bus_dmamap_load(*tag, *map, *va, len, oneseg_dma_callback, pa, 0);
3484 if (rc != 0) {
3485 CH_ERR(sc, "cannot load DMA map: %d\n", rc);
3486 goto done;
3487 }
3488 done:
3489 if (rc)
3490 free_ring(sc, *tag, *map, *pa, *va);
3491
3492 return (rc);
3493 }
3494
3495 int
free_ring(struct adapter * sc,bus_dma_tag_t tag,bus_dmamap_t map,bus_addr_t pa,void * va)3496 free_ring(struct adapter *sc, bus_dma_tag_t tag, bus_dmamap_t map,
3497 bus_addr_t pa, void *va)
3498 {
3499 if (pa)
3500 bus_dmamap_unload(tag, map);
3501 if (va)
3502 bus_dmamem_free(tag, va, map);
3503 if (tag)
3504 bus_dma_tag_destroy(tag);
3505
3506 return (0);
3507 }
3508
3509 /*
3510 * Allocates the software resources (mainly memory and sysctl nodes) for an
3511 * ingress queue and an optional freelist.
3512 *
3513 * Sets IQ_SW_ALLOCATED and returns 0 on success.
3514 */
3515 static int
alloc_iq_fl(struct vi_info * vi,struct sge_iq * iq,struct sge_fl * fl,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)3516 alloc_iq_fl(struct vi_info *vi, struct sge_iq *iq, struct sge_fl *fl,
3517 struct sysctl_ctx_list *ctx, struct sysctl_oid *oid)
3518 {
3519 int rc;
3520 size_t len;
3521 struct adapter *sc = vi->adapter;
3522
3523 MPASS(!(iq->flags & IQ_SW_ALLOCATED));
3524
3525 len = iq->qsize * IQ_ESIZE;
3526 rc = alloc_ring(sc, len, &iq->desc_tag, &iq->desc_map, &iq->ba,
3527 (void **)&iq->desc);
3528 if (rc != 0)
3529 return (rc);
3530
3531 if (fl) {
3532 len = fl->qsize * EQ_ESIZE;
3533 rc = alloc_ring(sc, len, &fl->desc_tag, &fl->desc_map,
3534 &fl->ba, (void **)&fl->desc);
3535 if (rc) {
3536 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba,
3537 iq->desc);
3538 return (rc);
3539 }
3540
3541 /* Allocate space for one software descriptor per buffer. */
3542 fl->sdesc = malloc(fl->sidx * 8 * sizeof(struct fl_sdesc),
3543 M_CXGBE, M_ZERO | M_WAITOK);
3544
3545 add_fl_sysctls(sc, ctx, oid, fl);
3546 iq->flags |= IQ_HAS_FL;
3547 }
3548 add_iq_sysctls(ctx, oid, iq);
3549 iq->flags |= IQ_SW_ALLOCATED;
3550
3551 return (0);
3552 }
3553
3554 /*
3555 * Frees all software resources (memory and locks) associated with an ingress
3556 * queue and an optional freelist.
3557 */
3558 static void
free_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)3559 free_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
3560 {
3561 MPASS(iq->flags & IQ_SW_ALLOCATED);
3562
3563 if (fl) {
3564 MPASS(iq->flags & IQ_HAS_FL);
3565 free_ring(sc, fl->desc_tag, fl->desc_map, fl->ba, fl->desc);
3566 free_fl_buffers(sc, fl);
3567 free(fl->sdesc, M_CXGBE);
3568 mtx_destroy(&fl->fl_lock);
3569 bzero(fl, sizeof(*fl));
3570 }
3571 free_ring(sc, iq->desc_tag, iq->desc_map, iq->ba, iq->desc);
3572 bzero(iq, sizeof(*iq));
3573 }
3574
3575 /*
3576 * Allocates a hardware ingress queue and an optional freelist that will be
3577 * associated with it.
3578 *
3579 * Returns errno on failure. Resources allocated up to that point may still be
3580 * allocated. Caller is responsible for cleanup in case this function fails.
3581 */
3582 static int
alloc_iq_fl_hwq(struct vi_info * vi,struct sge_iq * iq,struct sge_fl * fl)3583 alloc_iq_fl_hwq(struct vi_info *vi, struct sge_iq *iq, struct sge_fl *fl)
3584 {
3585 int rc, cntxt_id, cong_map;
3586 struct fw_iq_cmd c;
3587 struct adapter *sc = vi->adapter;
3588 struct port_info *pi = vi->pi;
3589 __be32 v = 0;
3590
3591 MPASS (!(iq->flags & IQ_HW_ALLOCATED));
3592
3593 bzero(&c, sizeof(c));
3594 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
3595 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_IQ_CMD_PFN(sc->pf) |
3596 V_FW_IQ_CMD_VFN(0));
3597
3598 c.alloc_to_len16 = htobe32(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
3599 FW_LEN16(c));
3600
3601 /* Special handling for firmware event queue */
3602 if (iq == &sc->sge.fwq)
3603 v |= F_FW_IQ_CMD_IQASYNCH;
3604
3605 if (iq->intr_idx < 0) {
3606 /* Forwarded interrupts, all headed to fwq */
3607 v |= F_FW_IQ_CMD_IQANDST;
3608 v |= V_FW_IQ_CMD_IQANDSTINDEX(sc->sge.fwq.cntxt_id);
3609 } else {
3610 KASSERT(iq->intr_idx < sc->intr_count,
3611 ("%s: invalid direct intr_idx %d", __func__, iq->intr_idx));
3612 v |= V_FW_IQ_CMD_IQANDSTINDEX(iq->intr_idx);
3613 }
3614
3615 bzero(iq->desc, iq->qsize * IQ_ESIZE);
3616 c.type_to_iqandstindex = htobe32(v |
3617 V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
3618 V_FW_IQ_CMD_VIID(vi->viid) |
3619 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT));
3620 c.iqdroprss_to_iqesize = htobe16(V_FW_IQ_CMD_IQPCIECH(pi->hw_port) |
3621 F_FW_IQ_CMD_IQGTSMODE |
3622 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->intr_pktc_idx) |
3623 V_FW_IQ_CMD_IQESIZE(ilog2(IQ_ESIZE) - 4));
3624 c.iqsize = htobe16(iq->qsize);
3625 c.iqaddr = htobe64(iq->ba);
3626 c.iqns_to_fl0congen = htobe32(V_FW_IQ_CMD_IQTYPE(iq->qtype));
3627 if (iq->cong_drop != -1) {
3628 if (iq->qtype == IQ_ETH) {
3629 if (chip_id(sc) >= CHELSIO_T7)
3630 cong_map = 1 << pi->hw_port;
3631 else
3632 cong_map = pi->rx_e_chan_map;
3633 } else
3634 cong_map = 0;
3635 c.iqns_to_fl0congen |= htobe32(F_FW_IQ_CMD_IQFLINTCONGEN);
3636 }
3637
3638 if (fl) {
3639 bzero(fl->desc, fl->sidx * EQ_ESIZE + sc->params.sge.spg_len);
3640 c.iqns_to_fl0congen |=
3641 htobe32(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
3642 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
3643 (fl_pad ? F_FW_IQ_CMD_FL0PADEN : 0) |
3644 (fl->flags & FL_BUF_PACKING ? F_FW_IQ_CMD_FL0PACKEN :
3645 0));
3646 if (iq->cong_drop != -1) {
3647 c.iqns_to_fl0congen |=
3648 htobe32(V_FW_IQ_CMD_FL0CNGCHMAP(cong_map) |
3649 F_FW_IQ_CMD_FL0CONGCIF |
3650 F_FW_IQ_CMD_FL0CONGEN);
3651 }
3652 c.fl0dcaen_to_fl0cidxfthresh =
3653 htobe16(V_FW_IQ_CMD_FL0FBMIN(chip_id(sc) <= CHELSIO_T5 ?
3654 X_FETCHBURSTMIN_128B : X_FETCHBURSTMIN_64B_T6) |
3655 V_FW_IQ_CMD_FL0FBMAX(chip_id(sc) <= CHELSIO_T5 ?
3656 X_FETCHBURSTMAX_512B : X_FETCHBURSTMAX_256B));
3657 c.fl0size = htobe16(fl->qsize);
3658 c.fl0addr = htobe64(fl->ba);
3659 }
3660
3661 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
3662 if (rc != 0) {
3663 CH_ERR(sc, "failed to create hw ingress queue: %d\n", rc);
3664 return (rc);
3665 }
3666
3667 iq->cidx = 0;
3668 iq->gen = F_RSPD_GEN;
3669 iq->cntxt_id = be16toh(c.iqid);
3670 iq->abs_id = be16toh(c.physiqid);
3671
3672 cntxt_id = iq->cntxt_id - sc->sge.iq_start;
3673 if (cntxt_id >= sc->sge.iqmap_sz) {
3674 panic ("%s: iq->cntxt_id (%d) more than the max (%d)", __func__,
3675 cntxt_id, sc->sge.iqmap_sz - 1);
3676 }
3677 sc->sge.iqmap[cntxt_id] = iq;
3678
3679 if (fl) {
3680 u_int qid;
3681 #ifdef INVARIANTS
3682 int i;
3683
3684 MPASS(!(fl->flags & FL_BUF_RESUME));
3685 for (i = 0; i < fl->sidx * 8; i++)
3686 MPASS(fl->sdesc[i].cl == NULL);
3687 #endif
3688 fl->cntxt_id = be16toh(c.fl0id);
3689 fl->pidx = fl->cidx = fl->hw_cidx = fl->dbidx = 0;
3690 fl->rx_offset = 0;
3691 fl->flags &= ~(FL_STARVING | FL_DOOMED);
3692
3693 cntxt_id = fl->cntxt_id - sc->sge.eq_start;
3694 if (cntxt_id >= sc->sge.eqmap_sz) {
3695 panic("%s: fl->cntxt_id (%d) more than the max (%d)",
3696 __func__, cntxt_id, sc->sge.eqmap_sz - 1);
3697 }
3698 sc->sge.eqmap[cntxt_id] = (void *)fl;
3699
3700 qid = fl->cntxt_id;
3701 if (isset(&sc->doorbells, DOORBELL_UDB)) {
3702 uint32_t s_qpp = sc->params.sge.eq_s_qpp;
3703 uint32_t mask = (1 << s_qpp) - 1;
3704 volatile uint8_t *udb;
3705
3706 udb = sc->udbs_base + UDBS_DB_OFFSET;
3707 udb += (qid >> s_qpp) << PAGE_SHIFT;
3708 qid &= mask;
3709 if (qid < PAGE_SIZE / UDBS_SEG_SIZE) {
3710 udb += qid << UDBS_SEG_SHIFT;
3711 qid = 0;
3712 }
3713 fl->udb = (volatile void *)udb;
3714 }
3715 fl->dbval = V_QID(qid) | sc->chip_params->sge_fl_db;
3716
3717 FL_LOCK(fl);
3718 /* Enough to make sure the SGE doesn't think it's starved */
3719 refill_fl(sc, fl, fl->lowat);
3720 FL_UNLOCK(fl);
3721 }
3722
3723 if (chip_id(sc) >= CHELSIO_T5 && !(sc->flags & IS_VF) &&
3724 iq->cong_drop != -1) {
3725 t4_sge_set_conm_context(sc, iq->cntxt_id, iq->cong_drop,
3726 cong_map);
3727 }
3728
3729 /* Enable IQ interrupts */
3730 atomic_store_rel_int(&iq->state, IQS_IDLE);
3731 t4_write_reg(sc, sc->sge_gts_reg, V_SEINTARM(iq->intr_params) |
3732 V_INGRESSQID(iq->cntxt_id));
3733
3734 iq->flags |= IQ_HW_ALLOCATED;
3735
3736 return (0);
3737 }
3738
3739 static int
free_iq_fl_hwq(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)3740 free_iq_fl_hwq(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
3741 {
3742 int rc;
3743
3744 MPASS(iq->flags & IQ_HW_ALLOCATED);
3745 rc = -t4_iq_free(sc, sc->mbox, sc->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
3746 iq->cntxt_id, fl ? fl->cntxt_id : 0xffff, 0xffff);
3747 if (rc != 0) {
3748 CH_ERR(sc, "failed to free iq %p: %d\n", iq, rc);
3749 return (rc);
3750 }
3751 iq->flags &= ~IQ_HW_ALLOCATED;
3752
3753 return (0);
3754 }
3755
3756 static void
add_iq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_iq * iq)3757 add_iq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
3758 struct sge_iq *iq)
3759 {
3760 struct sysctl_oid_list *children;
3761
3762 if (ctx == NULL || oid == NULL)
3763 return;
3764
3765 children = SYSCTL_CHILDREN(oid);
3766 SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD, &iq->ba,
3767 "bus address of descriptor ring");
3768 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
3769 iq->qsize * IQ_ESIZE, "descriptor ring size in bytes");
3770 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "abs_id", CTLFLAG_RD,
3771 &iq->abs_id, 0, "absolute id of the queue");
3772 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3773 &iq->cntxt_id, 0, "SGE context id of the queue");
3774 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &iq->cidx,
3775 0, "consumer index");
3776 }
3777
3778 static void
add_fl_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_fl * fl)3779 add_fl_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
3780 struct sysctl_oid *oid, struct sge_fl *fl)
3781 {
3782 struct sysctl_oid_list *children;
3783
3784 if (ctx == NULL || oid == NULL)
3785 return;
3786
3787 children = SYSCTL_CHILDREN(oid);
3788 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "fl",
3789 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist");
3790 children = SYSCTL_CHILDREN(oid);
3791
3792 SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD,
3793 &fl->ba, "bus address of descriptor ring");
3794 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
3795 fl->sidx * EQ_ESIZE + sc->params.sge.spg_len,
3796 "desc ring size in bytes");
3797 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
3798 &fl->cntxt_id, 0, "SGE context id of the freelist");
3799 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "padding", CTLFLAG_RD, NULL,
3800 fl_pad ? 1 : 0, "padding enabled");
3801 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "packing", CTLFLAG_RD, NULL,
3802 fl->flags & FL_BUF_PACKING ? 1 : 0, "packing enabled");
3803 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &fl->cidx,
3804 0, "consumer index");
3805 if (fl->flags & FL_BUF_PACKING) {
3806 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rx_offset",
3807 CTLFLAG_RD, &fl->rx_offset, 0, "packing rx offset");
3808 }
3809 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &fl->pidx,
3810 0, "producer index");
3811 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_allocated",
3812 CTLFLAG_RD, &fl->cl_allocated, "# of clusters allocated");
3813 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_recycled",
3814 CTLFLAG_RD, &fl->cl_recycled, "# of clusters recycled");
3815 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "cluster_fast_recycled",
3816 CTLFLAG_RD, &fl->cl_fast_recycled, "# of clusters recycled (fast)");
3817 }
3818
3819 /*
3820 * Idempotent.
3821 */
3822 static int
alloc_fwq(struct adapter * sc)3823 alloc_fwq(struct adapter *sc)
3824 {
3825 int rc, intr_idx;
3826 struct sge_iq *fwq = &sc->sge.fwq;
3827 struct vi_info *vi = &sc->port[0]->vi[0];
3828
3829 if (!(fwq->flags & IQ_SW_ALLOCATED)) {
3830 MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
3831
3832 if (sc->flags & IS_VF)
3833 intr_idx = 0;
3834 else
3835 intr_idx = sc->intr_count > 1 ? 1 : 0;
3836 init_iq(fwq, sc, 0, 0, FW_IQ_QSIZE, intr_idx, -1, IQ_OTHER);
3837 rc = alloc_iq_fl(vi, fwq, NULL, &sc->ctx, sc->fwq_oid);
3838 if (rc != 0) {
3839 CH_ERR(sc, "failed to allocate fwq: %d\n", rc);
3840 return (rc);
3841 }
3842 MPASS(fwq->flags & IQ_SW_ALLOCATED);
3843 }
3844
3845 if (!(fwq->flags & IQ_HW_ALLOCATED)) {
3846 MPASS(fwq->flags & IQ_SW_ALLOCATED);
3847
3848 rc = alloc_iq_fl_hwq(vi, fwq, NULL);
3849 if (rc != 0) {
3850 CH_ERR(sc, "failed to create hw fwq: %d\n", rc);
3851 return (rc);
3852 }
3853 MPASS(fwq->flags & IQ_HW_ALLOCATED);
3854 }
3855
3856 return (0);
3857 }
3858
3859 /*
3860 * Idempotent.
3861 */
3862 static void
free_fwq(struct adapter * sc)3863 free_fwq(struct adapter *sc)
3864 {
3865 struct sge_iq *fwq = &sc->sge.fwq;
3866
3867 if (fwq->flags & IQ_HW_ALLOCATED) {
3868 MPASS(fwq->flags & IQ_SW_ALLOCATED);
3869 free_iq_fl_hwq(sc, fwq, NULL);
3870 MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
3871 }
3872
3873 if (fwq->flags & IQ_SW_ALLOCATED) {
3874 MPASS(!(fwq->flags & IQ_HW_ALLOCATED));
3875 free_iq_fl(sc, fwq, NULL);
3876 MPASS(!(fwq->flags & IQ_SW_ALLOCATED));
3877 }
3878 }
3879
3880 /*
3881 * Idempotent.
3882 */
3883 static int
alloc_ctrlq(struct adapter * sc,int idx)3884 alloc_ctrlq(struct adapter *sc, int idx)
3885 {
3886 int rc;
3887 char name[16];
3888 struct sysctl_oid *oid;
3889 struct sge_wrq *ctrlq = &sc->sge.ctrlq[idx];
3890
3891 MPASS(idx < sc->sge.nctrlq);
3892
3893 if (!(ctrlq->eq.flags & EQ_SW_ALLOCATED)) {
3894 MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
3895
3896 snprintf(name, sizeof(name), "%d", idx);
3897 oid = SYSCTL_ADD_NODE(&sc->ctx, SYSCTL_CHILDREN(sc->ctrlq_oid),
3898 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
3899 "ctrl queue");
3900
3901 snprintf(name, sizeof(name), "%s ctrlq%d",
3902 device_get_nameunit(sc->dev), idx);
3903 init_eq(sc, &ctrlq->eq, EQ_CTRL, CTRL_EQ_QSIZE,
3904 idx % sc->params.nports, &sc->sge.fwq, name);
3905 rc = alloc_wrq(sc, NULL, ctrlq, &sc->ctx, oid);
3906 if (rc != 0) {
3907 CH_ERR(sc, "failed to allocate ctrlq%d: %d\n", idx, rc);
3908 sysctl_remove_oid(oid, 1, 1);
3909 return (rc);
3910 }
3911 MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
3912 }
3913
3914 if (!(ctrlq->eq.flags & EQ_HW_ALLOCATED)) {
3915 MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
3916 MPASS(ctrlq->nwr_pending == 0);
3917 MPASS(ctrlq->ndesc_needed == 0);
3918
3919 rc = alloc_eq_hwq(sc, NULL, &ctrlq->eq, idx);
3920 if (rc != 0) {
3921 CH_ERR(sc, "failed to create hw ctrlq%d: %d\n", idx, rc);
3922 return (rc);
3923 }
3924 MPASS(ctrlq->eq.flags & EQ_HW_ALLOCATED);
3925 }
3926
3927 return (0);
3928 }
3929
3930 /*
3931 * Idempotent.
3932 */
3933 static void
free_ctrlq(struct adapter * sc,int idx)3934 free_ctrlq(struct adapter *sc, int idx)
3935 {
3936 struct sge_wrq *ctrlq = &sc->sge.ctrlq[idx];
3937
3938 if (ctrlq->eq.flags & EQ_HW_ALLOCATED) {
3939 MPASS(ctrlq->eq.flags & EQ_SW_ALLOCATED);
3940 free_eq_hwq(sc, NULL, &ctrlq->eq);
3941 MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
3942 }
3943
3944 if (ctrlq->eq.flags & EQ_SW_ALLOCATED) {
3945 MPASS(!(ctrlq->eq.flags & EQ_HW_ALLOCATED));
3946 free_wrq(sc, ctrlq);
3947 MPASS(!(ctrlq->eq.flags & EQ_SW_ALLOCATED));
3948 }
3949 }
3950
3951 int
t4_sge_set_conm_context(struct adapter * sc,int cntxt_id,int cong_drop,int cong_map)3952 t4_sge_set_conm_context(struct adapter *sc, int cntxt_id, int cong_drop,
3953 int cong_map)
3954 {
3955 const int cng_ch_bits_log = sc->chip_params->cng_ch_bits_log;
3956 uint32_t param, val;
3957 uint16_t ch_map;
3958 int cong_mode, rc, i;
3959
3960 if (chip_id(sc) < CHELSIO_T5)
3961 return (ENOTSUP);
3962
3963 /* Convert the driver knob to the mode understood by the firmware. */
3964 switch (cong_drop) {
3965 case -1:
3966 cong_mode = X_CONMCTXT_CNGTPMODE_DISABLE;
3967 break;
3968 case 0:
3969 cong_mode = X_CONMCTXT_CNGTPMODE_CHANNEL;
3970 break;
3971 case 1:
3972 cong_mode = X_CONMCTXT_CNGTPMODE_QUEUE;
3973 break;
3974 case 2:
3975 cong_mode = X_CONMCTXT_CNGTPMODE_BOTH;
3976 break;
3977 default:
3978 MPASS(0);
3979 CH_ERR(sc, "cong_drop = %d is invalid (ingress queue %d).\n",
3980 cong_drop, cntxt_id);
3981 return (EINVAL);
3982 }
3983
3984 param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
3985 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
3986 V_FW_PARAMS_PARAM_YZ(cntxt_id);
3987 if (chip_id(sc) >= CHELSIO_T7) {
3988 val = V_T7_DMAQ_CONM_CTXT_CNGTPMODE(cong_mode) |
3989 V_T7_DMAQ_CONM_CTXT_CH_VEC(cong_map);
3990 } else {
3991 val = V_CONMCTXT_CNGTPMODE(cong_mode);
3992 if (cong_mode == X_CONMCTXT_CNGTPMODE_CHANNEL ||
3993 cong_mode == X_CONMCTXT_CNGTPMODE_BOTH) {
3994 for (i = 0, ch_map = 0; i < 4; i++) {
3995 if (cong_map & (1 << i))
3996 ch_map |= 1 << (i << cng_ch_bits_log);
3997 }
3998 val |= V_CONMCTXT_CNGCHMAP(ch_map);
3999 }
4000 }
4001 rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, ¶m, &val);
4002 if (rc != 0) {
4003 CH_ERR(sc, "failed to set congestion manager context "
4004 "for ingress queue %d: %d\n", cntxt_id, rc);
4005 }
4006
4007 return (rc);
4008 }
4009
4010 /*
4011 * Idempotent.
4012 */
4013 static int
alloc_rxq(struct vi_info * vi,struct sge_rxq * rxq,int idx,int intr_idx,int maxp)4014 alloc_rxq(struct vi_info *vi, struct sge_rxq *rxq, int idx, int intr_idx,
4015 int maxp)
4016 {
4017 int rc;
4018 struct adapter *sc = vi->adapter;
4019 if_t ifp = vi->ifp;
4020 struct sysctl_oid *oid;
4021 char name[16];
4022
4023 if (!(rxq->iq.flags & IQ_SW_ALLOCATED)) {
4024 MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
4025 #if defined(INET) || defined(INET6)
4026 rc = tcp_lro_init_args(&rxq->lro, ifp, lro_entries, lro_mbufs);
4027 if (rc != 0)
4028 return (rc);
4029 MPASS(rxq->lro.ifp == ifp); /* also indicates LRO init'ed */
4030 #endif
4031 rxq->ifp = ifp;
4032
4033 snprintf(name, sizeof(name), "%d", idx);
4034 oid = SYSCTL_ADD_NODE(&vi->ctx, SYSCTL_CHILDREN(vi->rxq_oid),
4035 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
4036 "rx queue");
4037
4038 init_iq(&rxq->iq, sc, vi->tmr_idx, vi->pktc_idx, vi->qsize_rxq,
4039 intr_idx, cong_drop, IQ_ETH);
4040 #if defined(INET) || defined(INET6)
4041 if (if_getcapenable(ifp) & IFCAP_LRO)
4042 rxq->iq.flags |= IQ_LRO_ENABLED;
4043 #endif
4044 if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
4045 rxq->iq.flags |= IQ_RX_TIMESTAMP;
4046 snprintf(name, sizeof(name), "%s rxq%d-fl",
4047 device_get_nameunit(vi->dev), idx);
4048 init_fl(sc, &rxq->fl, vi->qsize_rxq / 8, maxp, name);
4049 rc = alloc_iq_fl(vi, &rxq->iq, &rxq->fl, &vi->ctx, oid);
4050 if (rc != 0) {
4051 CH_ERR(vi, "failed to allocate rxq%d: %d\n", idx, rc);
4052 sysctl_remove_oid(oid, 1, 1);
4053 #if defined(INET) || defined(INET6)
4054 tcp_lro_free(&rxq->lro);
4055 rxq->lro.ifp = NULL;
4056 #endif
4057 return (rc);
4058 }
4059 MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
4060 add_rxq_sysctls(&vi->ctx, oid, rxq);
4061 }
4062
4063 if (!(rxq->iq.flags & IQ_HW_ALLOCATED)) {
4064 MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
4065 rc = alloc_iq_fl_hwq(vi, &rxq->iq, &rxq->fl);
4066 if (rc != 0) {
4067 CH_ERR(vi, "failed to create hw rxq%d: %d\n", idx, rc);
4068 return (rc);
4069 }
4070 MPASS(rxq->iq.flags & IQ_HW_ALLOCATED);
4071
4072 if (idx == 0)
4073 sc->sge.iq_base = rxq->iq.abs_id - rxq->iq.cntxt_id;
4074 else
4075 KASSERT(rxq->iq.cntxt_id + sc->sge.iq_base == rxq->iq.abs_id,
4076 ("iq_base mismatch"));
4077 KASSERT(sc->sge.iq_base == 0 || sc->flags & IS_VF,
4078 ("PF with non-zero iq_base"));
4079
4080 /*
4081 * The freelist is just barely above the starvation threshold
4082 * right now, fill it up a bit more.
4083 */
4084 FL_LOCK(&rxq->fl);
4085 refill_fl(sc, &rxq->fl, 128);
4086 FL_UNLOCK(&rxq->fl);
4087 }
4088
4089 return (0);
4090 }
4091
4092 /*
4093 * Idempotent.
4094 */
4095 static void
free_rxq(struct vi_info * vi,struct sge_rxq * rxq)4096 free_rxq(struct vi_info *vi, struct sge_rxq *rxq)
4097 {
4098 if (rxq->iq.flags & IQ_HW_ALLOCATED) {
4099 MPASS(rxq->iq.flags & IQ_SW_ALLOCATED);
4100 free_iq_fl_hwq(vi->adapter, &rxq->iq, &rxq->fl);
4101 MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
4102 }
4103
4104 if (rxq->iq.flags & IQ_SW_ALLOCATED) {
4105 MPASS(!(rxq->iq.flags & IQ_HW_ALLOCATED));
4106 #if defined(INET) || defined(INET6)
4107 tcp_lro_free(&rxq->lro);
4108 #endif
4109 free_iq_fl(vi->adapter, &rxq->iq, &rxq->fl);
4110 MPASS(!(rxq->iq.flags & IQ_SW_ALLOCATED));
4111 bzero(rxq, sizeof(*rxq));
4112 }
4113 }
4114
4115 static void
add_rxq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_rxq * rxq)4116 add_rxq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
4117 struct sge_rxq *rxq)
4118 {
4119 struct sysctl_oid_list *children;
4120
4121 if (ctx == NULL || oid == NULL)
4122 return;
4123
4124 children = SYSCTL_CHILDREN(oid);
4125 #if defined(INET) || defined(INET6)
4126 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "lro_queued", CTLFLAG_RD,
4127 &rxq->lro.lro_queued, 0, NULL);
4128 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "lro_flushed", CTLFLAG_RD,
4129 &rxq->lro.lro_flushed, 0, NULL);
4130 #endif
4131 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "rxcsum", CTLFLAG_RD,
4132 &rxq->rxcsum, "# of times hardware assisted with checksum");
4133 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vlan_extraction", CTLFLAG_RD,
4134 &rxq->vlan_extraction, "# of times hardware extracted 802.1Q tag");
4135 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_rxcsum", CTLFLAG_RD,
4136 &rxq->vxlan_rxcsum,
4137 "# of times hardware assisted with inner checksum (VXLAN)");
4138 }
4139
4140 #ifdef TCP_OFFLOAD
4141 /*
4142 * Idempotent.
4143 */
4144 static int
alloc_ofld_rxq(struct vi_info * vi,struct sge_ofld_rxq * ofld_rxq,int idx,int intr_idx,int maxp)4145 alloc_ofld_rxq(struct vi_info *vi, struct sge_ofld_rxq *ofld_rxq, int idx,
4146 int intr_idx, int maxp)
4147 {
4148 int rc;
4149 struct adapter *sc = vi->adapter;
4150 struct sysctl_oid *oid;
4151 char name[16];
4152
4153 if (!(ofld_rxq->iq.flags & IQ_SW_ALLOCATED)) {
4154 MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
4155
4156 snprintf(name, sizeof(name), "%d", idx);
4157 oid = SYSCTL_ADD_NODE(&vi->ctx,
4158 SYSCTL_CHILDREN(vi->ofld_rxq_oid), OID_AUTO, name,
4159 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "offload rx queue");
4160
4161 init_iq(&ofld_rxq->iq, sc, vi->ofld_tmr_idx, vi->ofld_pktc_idx,
4162 vi->qsize_rxq, intr_idx, ofld_cong_drop, IQ_OFLD);
4163 snprintf(name, sizeof(name), "%s ofld_rxq%d-fl",
4164 device_get_nameunit(vi->dev), idx);
4165 init_fl(sc, &ofld_rxq->fl, vi->qsize_rxq / 8, maxp, name);
4166 rc = alloc_iq_fl(vi, &ofld_rxq->iq, &ofld_rxq->fl, &vi->ctx,
4167 oid);
4168 if (rc != 0) {
4169 CH_ERR(vi, "failed to allocate ofld_rxq%d: %d\n", idx,
4170 rc);
4171 sysctl_remove_oid(oid, 1, 1);
4172 return (rc);
4173 }
4174 MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
4175 ofld_rxq->rx_iscsi_ddp_setup_ok = counter_u64_alloc(M_WAITOK);
4176 ofld_rxq->rx_iscsi_ddp_setup_error =
4177 counter_u64_alloc(M_WAITOK);
4178 ofld_rxq->rx_nvme_ddp_setup_ok = counter_u64_alloc(M_WAITOK);
4179 ofld_rxq->rx_nvme_ddp_setup_no_stag =
4180 counter_u64_alloc(M_WAITOK);
4181 ofld_rxq->rx_nvme_ddp_setup_error =
4182 counter_u64_alloc(M_WAITOK);
4183 ofld_rxq->rx_nvme_ddp_octets = counter_u64_alloc(M_WAITOK);
4184 ofld_rxq->rx_nvme_ddp_pdus = counter_u64_alloc(M_WAITOK);
4185 ofld_rxq->rx_nvme_fl_octets = counter_u64_alloc(M_WAITOK);
4186 ofld_rxq->rx_nvme_fl_pdus = counter_u64_alloc(M_WAITOK);
4187 ofld_rxq->rx_nvme_invalid_headers = counter_u64_alloc(M_WAITOK);
4188 ofld_rxq->rx_nvme_header_digest_errors =
4189 counter_u64_alloc(M_WAITOK);
4190 ofld_rxq->rx_nvme_data_digest_errors =
4191 counter_u64_alloc(M_WAITOK);
4192 ofld_rxq->ddp_buffer_alloc = counter_u64_alloc(M_WAITOK);
4193 ofld_rxq->ddp_buffer_reuse = counter_u64_alloc(M_WAITOK);
4194 ofld_rxq->ddp_buffer_free = counter_u64_alloc(M_WAITOK);
4195 add_ofld_rxq_sysctls(&vi->ctx, oid, ofld_rxq);
4196 }
4197
4198 if (!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED)) {
4199 MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
4200 rc = alloc_iq_fl_hwq(vi, &ofld_rxq->iq, &ofld_rxq->fl);
4201 if (rc != 0) {
4202 CH_ERR(vi, "failed to create hw ofld_rxq%d: %d\n", idx,
4203 rc);
4204 return (rc);
4205 }
4206 MPASS(ofld_rxq->iq.flags & IQ_HW_ALLOCATED);
4207 }
4208 return (rc);
4209 }
4210
4211 /*
4212 * Idempotent.
4213 */
4214 static void
free_ofld_rxq(struct vi_info * vi,struct sge_ofld_rxq * ofld_rxq)4215 free_ofld_rxq(struct vi_info *vi, struct sge_ofld_rxq *ofld_rxq)
4216 {
4217 if (ofld_rxq->iq.flags & IQ_HW_ALLOCATED) {
4218 MPASS(ofld_rxq->iq.flags & IQ_SW_ALLOCATED);
4219 free_iq_fl_hwq(vi->adapter, &ofld_rxq->iq, &ofld_rxq->fl);
4220 MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
4221 }
4222
4223 if (ofld_rxq->iq.flags & IQ_SW_ALLOCATED) {
4224 MPASS(!(ofld_rxq->iq.flags & IQ_HW_ALLOCATED));
4225 free_iq_fl(vi->adapter, &ofld_rxq->iq, &ofld_rxq->fl);
4226 MPASS(!(ofld_rxq->iq.flags & IQ_SW_ALLOCATED));
4227 counter_u64_free(ofld_rxq->rx_iscsi_ddp_setup_ok);
4228 counter_u64_free(ofld_rxq->rx_iscsi_ddp_setup_error);
4229 counter_u64_free(ofld_rxq->rx_nvme_ddp_setup_ok);
4230 counter_u64_free(ofld_rxq->rx_nvme_ddp_setup_no_stag);
4231 counter_u64_free(ofld_rxq->rx_nvme_ddp_setup_error);
4232 counter_u64_free(ofld_rxq->rx_nvme_ddp_octets);
4233 counter_u64_free(ofld_rxq->rx_nvme_ddp_pdus);
4234 counter_u64_free(ofld_rxq->rx_nvme_fl_octets);
4235 counter_u64_free(ofld_rxq->rx_nvme_fl_pdus);
4236 counter_u64_free(ofld_rxq->rx_nvme_invalid_headers);
4237 counter_u64_free(ofld_rxq->rx_nvme_header_digest_errors);
4238 counter_u64_free(ofld_rxq->rx_nvme_data_digest_errors);
4239 counter_u64_free(ofld_rxq->ddp_buffer_alloc);
4240 counter_u64_free(ofld_rxq->ddp_buffer_reuse);
4241 counter_u64_free(ofld_rxq->ddp_buffer_free);
4242 bzero(ofld_rxq, sizeof(*ofld_rxq));
4243 }
4244 }
4245
4246 static void
add_ofld_rxq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_ofld_rxq * ofld_rxq)4247 add_ofld_rxq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
4248 struct sge_ofld_rxq *ofld_rxq)
4249 {
4250 struct sysctl_oid_list *children, *top;
4251
4252 if (ctx == NULL || oid == NULL)
4253 return;
4254
4255 top = children = SYSCTL_CHILDREN(oid);
4256 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "rx_aio_ddp_jobs",
4257 CTLFLAG_RD, &ofld_rxq->rx_aio_ddp_jobs, 0,
4258 "# of aio_read(2) jobs completed via DDP");
4259 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "rx_aio_ddp_octets",
4260 CTLFLAG_RD, &ofld_rxq->rx_aio_ddp_octets, 0,
4261 "# of octets placed directly for aio_read(2) jobs");
4262 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
4263 "rx_toe_tls_records", CTLFLAG_RD, &ofld_rxq->rx_toe_tls_records,
4264 "# of TOE TLS records received");
4265 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
4266 "rx_toe_tls_octets", CTLFLAG_RD, &ofld_rxq->rx_toe_tls_octets,
4267 "# of payload octets in received TOE TLS records");
4268 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
4269 "rx_toe_ddp_octets", CTLFLAG_RD, &ofld_rxq->rx_toe_ddp_octets,
4270 "# of payload octets received via TCP DDP");
4271 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4272 "ddp_buffer_alloc", CTLFLAG_RD, &ofld_rxq->ddp_buffer_alloc,
4273 "# of DDP RCV buffers allocated");
4274 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4275 "ddp_buffer_reuse", CTLFLAG_RD, &ofld_rxq->ddp_buffer_reuse,
4276 "# of DDP RCV buffers reused");
4277 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO,
4278 "ddp_buffer_free", CTLFLAG_RD, &ofld_rxq->ddp_buffer_free,
4279 "# of DDP RCV buffers freed");
4280
4281 oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "iscsi",
4282 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE iSCSI statistics");
4283 children = SYSCTL_CHILDREN(oid);
4284
4285 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_ok",
4286 CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_setup_ok,
4287 "# of times DDP buffer was setup successfully.");
4288 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_error",
4289 CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_setup_error,
4290 "# of times DDP buffer setup failed.");
4291 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "ddp_octets",
4292 CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_octets, 0,
4293 "# of octets placed directly");
4294 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "ddp_pdus",
4295 CTLFLAG_RD, &ofld_rxq->rx_iscsi_ddp_pdus, 0,
4296 "# of PDUs with data placed directly.");
4297 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "fl_octets",
4298 CTLFLAG_RD, &ofld_rxq->rx_iscsi_fl_octets, 0,
4299 "# of data octets delivered in freelist");
4300 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "fl_pdus",
4301 CTLFLAG_RD, &ofld_rxq->rx_iscsi_fl_pdus, 0,
4302 "# of PDUs with data delivered in freelist");
4303 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "padding_errors",
4304 CTLFLAG_RD, &ofld_rxq->rx_iscsi_padding_errors, 0,
4305 "# of PDUs with invalid padding");
4306 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "header_digest_errors",
4307 CTLFLAG_RD, &ofld_rxq->rx_iscsi_header_digest_errors, 0,
4308 "# of PDUs with invalid header digests");
4309 SYSCTL_ADD_U64(ctx, children, OID_AUTO, "data_digest_errors",
4310 CTLFLAG_RD, &ofld_rxq->rx_iscsi_data_digest_errors, 0,
4311 "# of PDUs with invalid data digests");
4312
4313 oid = SYSCTL_ADD_NODE(ctx, top, OID_AUTO, "nvme",
4314 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE NVMe statistics");
4315 children = SYSCTL_CHILDREN(oid);
4316
4317 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_ok",
4318 CTLFLAG_RD, &ofld_rxq->rx_nvme_ddp_setup_ok,
4319 "# of times DDP buffer was setup successfully");
4320 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_no_stag",
4321 CTLFLAG_RD, &ofld_rxq->rx_nvme_ddp_setup_no_stag,
4322 "# of times STAG was not available for DDP buffer setup");
4323 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_setup_error",
4324 CTLFLAG_RD, &ofld_rxq->rx_nvme_ddp_setup_error,
4325 "# of times DDP buffer setup failed");
4326 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_octets",
4327 CTLFLAG_RD, &ofld_rxq->rx_nvme_ddp_octets,
4328 "# of octets placed directly");
4329 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "ddp_pdus",
4330 CTLFLAG_RD, &ofld_rxq->rx_nvme_ddp_pdus,
4331 "# of PDUs with data placed directly");
4332 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "fl_octets",
4333 CTLFLAG_RD, &ofld_rxq->rx_nvme_fl_octets,
4334 "# of data octets delivered in freelist");
4335 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "fl_pdus",
4336 CTLFLAG_RD, &ofld_rxq->rx_nvme_fl_pdus,
4337 "# of PDUs with data delivered in freelist");
4338 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "invalid_headers",
4339 CTLFLAG_RD, &ofld_rxq->rx_nvme_invalid_headers,
4340 "# of PDUs with invalid header field");
4341 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "header_digest_errors",
4342 CTLFLAG_RD, &ofld_rxq->rx_nvme_header_digest_errors,
4343 "# of PDUs with invalid header digests");
4344 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "data_digest_errors",
4345 CTLFLAG_RD, &ofld_rxq->rx_nvme_data_digest_errors,
4346 "# of PDUs with invalid data digests");
4347 }
4348 #endif
4349
4350 /*
4351 * Returns a reasonable automatic cidx flush threshold for a given queue size.
4352 */
4353 static u_int
qsize_to_fthresh(int qsize)4354 qsize_to_fthresh(int qsize)
4355 {
4356 u_int fthresh;
4357
4358 fthresh = qsize == 0 ? 0 : order_base_2(qsize);
4359 if (fthresh > X_CIDXFLUSHTHRESH_128)
4360 fthresh = X_CIDXFLUSHTHRESH_128;
4361
4362 return (fthresh);
4363 }
4364
4365 static int
ctrl_eq_alloc(struct adapter * sc,struct sge_eq * eq,int idx)4366 ctrl_eq_alloc(struct adapter *sc, struct sge_eq *eq, int idx)
4367 {
4368 int rc, cntxt_id, core;
4369 struct fw_eq_ctrl_cmd c;
4370 int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
4371
4372 core = sc->params.tid_qid_sel_mask != 0 ? idx % sc->params.ncores : 0;
4373 bzero(&c, sizeof(c));
4374
4375 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
4376 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_CTRL_CMD_PFN(sc->pf) |
4377 V_FW_EQ_CTRL_CMD_VFN(0));
4378 c.alloc_to_len16 = htobe32(F_FW_EQ_CTRL_CMD_ALLOC |
4379 V_FW_EQ_CTRL_CMD_COREGROUP(core) |
4380 F_FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
4381 c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(eq->iqid));
4382 c.physeqid_pkd = htobe32(0);
4383 c.fetchszm_to_iqid =
4384 htobe32(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
4385 V_FW_EQ_CTRL_CMD_PCIECHN(eq->hw_port) |
4386 F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(eq->iqid));
4387 c.dcaen_to_eqsize =
4388 htobe32(V_FW_EQ_CTRL_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4389 X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
4390 V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
4391 V_FW_EQ_CTRL_CMD_CIDXFTHRESH(qsize_to_fthresh(qsize)) |
4392 V_FW_EQ_CTRL_CMD_EQSIZE(qsize));
4393 c.eqaddr = htobe64(eq->ba);
4394
4395 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
4396 if (rc != 0) {
4397 CH_ERR(sc, "failed to create hw ctrlq for port %d: %d\n",
4398 eq->port_id, rc);
4399 return (rc);
4400 }
4401
4402 eq->cntxt_id = G_FW_EQ_CTRL_CMD_EQID(be32toh(c.cmpliqid_eqid));
4403 eq->abs_id = G_FW_EQ_CTRL_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
4404 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4405 if (cntxt_id >= sc->sge.eqmap_sz)
4406 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4407 cntxt_id, sc->sge.eqmap_sz - 1);
4408 sc->sge.eqmap[cntxt_id] = eq;
4409
4410 return (rc);
4411 }
4412
4413 static int
eth_eq_alloc(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq,int idx)4414 eth_eq_alloc(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq, int idx)
4415 {
4416 int rc, cntxt_id, core;
4417 struct fw_eq_eth_cmd c;
4418 int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
4419
4420 core = sc->params.ncores > 1 ? idx % sc->params.ncores : 0;
4421 bzero(&c, sizeof(c));
4422
4423 c.op_to_vfn = htobe32(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
4424 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_ETH_CMD_PFN(sc->pf) |
4425 V_FW_EQ_ETH_CMD_VFN(0));
4426 c.alloc_to_len16 = htobe32(F_FW_EQ_ETH_CMD_ALLOC |
4427 V_FW_EQ_ETH_CMD_COREGROUP(core) |
4428 F_FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
4429 c.autoequiqe_to_viid = htobe32(F_FW_EQ_ETH_CMD_AUTOEQUIQE |
4430 F_FW_EQ_ETH_CMD_AUTOEQUEQE | V_FW_EQ_ETH_CMD_VIID(vi->viid));
4431 c.fetchszm_to_iqid =
4432 htobe32(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
4433 V_FW_EQ_ETH_CMD_PCIECHN(eq->hw_port) | F_FW_EQ_ETH_CMD_FETCHRO |
4434 V_FW_EQ_ETH_CMD_IQID(eq->iqid));
4435 c.dcaen_to_eqsize =
4436 htobe32(V_FW_EQ_ETH_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4437 X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
4438 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
4439 V_FW_EQ_ETH_CMD_EQSIZE(qsize));
4440 c.eqaddr = htobe64(eq->ba);
4441
4442 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
4443 if (rc != 0) {
4444 device_printf(vi->dev,
4445 "failed to create Ethernet egress queue: %d\n", rc);
4446 return (rc);
4447 }
4448
4449 eq->cntxt_id = G_FW_EQ_ETH_CMD_EQID(be32toh(c.eqid_pkd));
4450 eq->abs_id = G_FW_EQ_ETH_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
4451 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4452 if (cntxt_id >= sc->sge.eqmap_sz)
4453 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4454 cntxt_id, sc->sge.eqmap_sz - 1);
4455 sc->sge.eqmap[cntxt_id] = eq;
4456
4457 return (rc);
4458 }
4459
4460 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4461 /*
4462 * ncores number of uP cores.
4463 * nq number of queues for this VI
4464 * idx queue index
4465 */
4466 static inline int
qidx_to_core(int ncores,int nq,int idx)4467 qidx_to_core(int ncores, int nq, int idx)
4468 {
4469 MPASS(nq % ncores == 0);
4470 MPASS(idx >= 0 && idx < nq);
4471
4472 return (idx * ncores / nq);
4473 }
4474
4475 static int
ofld_eq_alloc(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq,int idx)4476 ofld_eq_alloc(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq,
4477 int idx)
4478 {
4479 int rc, cntxt_id, core;
4480 struct fw_eq_ofld_cmd c;
4481 int qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
4482
4483 if (sc->params.tid_qid_sel_mask != 0)
4484 core = qidx_to_core(sc->params.ncores, vi->nofldtxq, idx);
4485 else
4486 core = 0;
4487
4488 bzero(&c, sizeof(c));
4489
4490 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_OFLD_CMD) | F_FW_CMD_REQUEST |
4491 F_FW_CMD_WRITE | F_FW_CMD_EXEC | V_FW_EQ_OFLD_CMD_PFN(sc->pf) |
4492 V_FW_EQ_OFLD_CMD_VFN(0));
4493 c.alloc_to_len16 = htonl(F_FW_EQ_OFLD_CMD_ALLOC |
4494 V_FW_EQ_OFLD_CMD_COREGROUP(core) |
4495 F_FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
4496 c.fetchszm_to_iqid =
4497 htonl(V_FW_EQ_OFLD_CMD_HOSTFCMODE(X_HOSTFCMODE_STATUS_PAGE) |
4498 V_FW_EQ_OFLD_CMD_PCIECHN(eq->hw_port) |
4499 F_FW_EQ_OFLD_CMD_FETCHRO | V_FW_EQ_OFLD_CMD_IQID(eq->iqid));
4500 c.dcaen_to_eqsize =
4501 htobe32(V_FW_EQ_OFLD_CMD_FBMIN(chip_id(sc) <= CHELSIO_T5 ?
4502 X_FETCHBURSTMIN_64B : X_FETCHBURSTMIN_64B_T6) |
4503 V_FW_EQ_OFLD_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
4504 V_FW_EQ_OFLD_CMD_CIDXFTHRESH(qsize_to_fthresh(qsize)) |
4505 V_FW_EQ_OFLD_CMD_EQSIZE(qsize));
4506 c.eqaddr = htobe64(eq->ba);
4507
4508 rc = -t4_wr_mbox(sc, sc->mbox, &c, sizeof(c), &c);
4509 if (rc != 0) {
4510 device_printf(vi->dev,
4511 "failed to create egress queue for TCP offload: %d\n", rc);
4512 return (rc);
4513 }
4514
4515 eq->cntxt_id = G_FW_EQ_OFLD_CMD_EQID(be32toh(c.eqid_pkd));
4516 eq->abs_id = G_FW_EQ_OFLD_CMD_PHYSEQID(be32toh(c.physeqid_pkd));
4517 cntxt_id = eq->cntxt_id - sc->sge.eq_start;
4518 if (cntxt_id >= sc->sge.eqmap_sz)
4519 panic("%s: eq->cntxt_id (%d) more than the max (%d)", __func__,
4520 cntxt_id, sc->sge.eqmap_sz - 1);
4521 sc->sge.eqmap[cntxt_id] = eq;
4522
4523 return (rc);
4524 }
4525 #endif
4526
4527 /* SW only */
4528 static int
alloc_eq(struct adapter * sc,struct sge_eq * eq,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)4529 alloc_eq(struct adapter *sc, struct sge_eq *eq, struct sysctl_ctx_list *ctx,
4530 struct sysctl_oid *oid)
4531 {
4532 int rc, qsize;
4533 size_t len;
4534
4535 MPASS(!(eq->flags & EQ_SW_ALLOCATED));
4536
4537 qsize = eq->sidx + sc->params.sge.spg_len / EQ_ESIZE;
4538 len = qsize * EQ_ESIZE;
4539 rc = alloc_ring(sc, len, &eq->desc_tag, &eq->desc_map, &eq->ba,
4540 (void **)&eq->desc);
4541 if (rc)
4542 return (rc);
4543 if (ctx != NULL && oid != NULL)
4544 add_eq_sysctls(sc, ctx, oid, eq);
4545 eq->flags |= EQ_SW_ALLOCATED;
4546
4547 return (0);
4548 }
4549
4550 /* SW only */
4551 static void
free_eq(struct adapter * sc,struct sge_eq * eq)4552 free_eq(struct adapter *sc, struct sge_eq *eq)
4553 {
4554 MPASS(eq->flags & EQ_SW_ALLOCATED);
4555 if (eq->type == EQ_ETH)
4556 MPASS(eq->pidx == eq->cidx);
4557
4558 free_ring(sc, eq->desc_tag, eq->desc_map, eq->ba, eq->desc);
4559 mtx_destroy(&eq->eq_lock);
4560 bzero(eq, sizeof(*eq));
4561 }
4562
4563 static void
add_eq_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_eq * eq)4564 add_eq_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
4565 struct sysctl_oid *oid, struct sge_eq *eq)
4566 {
4567 struct sysctl_oid_list *children = SYSCTL_CHILDREN(oid);
4568
4569 SYSCTL_ADD_UAUTO(ctx, children, OID_AUTO, "ba", CTLFLAG_RD, &eq->ba,
4570 "bus address of descriptor ring");
4571 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dmalen", CTLFLAG_RD, NULL,
4572 eq->sidx * EQ_ESIZE + sc->params.sge.spg_len,
4573 "desc ring size in bytes");
4574 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "abs_id", CTLFLAG_RD,
4575 &eq->abs_id, 0, "absolute id of the queue");
4576 SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cntxt_id", CTLFLAG_RD,
4577 &eq->cntxt_id, 0, "SGE context id of the queue");
4578 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "cidx", CTLFLAG_RD, &eq->cidx,
4579 0, "consumer index");
4580 SYSCTL_ADD_U16(ctx, children, OID_AUTO, "pidx", CTLFLAG_RD, &eq->pidx,
4581 0, "producer index");
4582 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sidx", CTLFLAG_RD, NULL,
4583 eq->sidx, "status page index");
4584 }
4585
4586 static int
alloc_eq_hwq(struct adapter * sc,struct vi_info * vi,struct sge_eq * eq,int idx)4587 alloc_eq_hwq(struct adapter *sc, struct vi_info *vi, struct sge_eq *eq, int idx)
4588 {
4589 int rc;
4590
4591 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
4592
4593 eq->iqid = eq->iq->cntxt_id;
4594 eq->pidx = eq->cidx = eq->dbidx = 0;
4595 /* Note that equeqidx is not used with sge_wrq (OFLD/CTRL) queues. */
4596 eq->equeqidx = 0;
4597 eq->doorbells = sc->doorbells;
4598 bzero(eq->desc, eq->sidx * EQ_ESIZE + sc->params.sge.spg_len);
4599
4600 switch (eq->type) {
4601 case EQ_CTRL:
4602 rc = ctrl_eq_alloc(sc, eq, idx);
4603 break;
4604
4605 case EQ_ETH:
4606 rc = eth_eq_alloc(sc, vi, eq, idx);
4607 break;
4608
4609 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4610 case EQ_OFLD:
4611 rc = ofld_eq_alloc(sc, vi, eq, idx);
4612 break;
4613 #endif
4614
4615 default:
4616 panic("%s: invalid eq type %d.", __func__, eq->type);
4617 }
4618 if (rc != 0) {
4619 CH_ERR(sc, "failed to allocate egress queue(%d): %d\n",
4620 eq->type, rc);
4621 return (rc);
4622 }
4623
4624 if (isset(&eq->doorbells, DOORBELL_UDB) ||
4625 isset(&eq->doorbells, DOORBELL_UDBWC) ||
4626 isset(&eq->doorbells, DOORBELL_WCWR)) {
4627 uint32_t s_qpp = sc->params.sge.eq_s_qpp;
4628 uint32_t mask = (1 << s_qpp) - 1;
4629 volatile uint8_t *udb;
4630
4631 udb = sc->udbs_base + UDBS_DB_OFFSET;
4632 udb += (eq->cntxt_id >> s_qpp) << PAGE_SHIFT; /* pg offset */
4633 eq->udb_qid = eq->cntxt_id & mask; /* id in page */
4634 if (eq->udb_qid >= PAGE_SIZE / UDBS_SEG_SIZE)
4635 clrbit(&eq->doorbells, DOORBELL_WCWR);
4636 else {
4637 udb += eq->udb_qid << UDBS_SEG_SHIFT; /* seg offset */
4638 eq->udb_qid = 0;
4639 }
4640 eq->udb = (volatile void *)udb;
4641 }
4642
4643 eq->flags |= EQ_HW_ALLOCATED;
4644 return (0);
4645 }
4646
4647 static int
free_eq_hwq(struct adapter * sc,struct vi_info * vi __unused,struct sge_eq * eq)4648 free_eq_hwq(struct adapter *sc, struct vi_info *vi __unused, struct sge_eq *eq)
4649 {
4650 int rc;
4651
4652 MPASS(eq->flags & EQ_HW_ALLOCATED);
4653
4654 switch (eq->type) {
4655 case EQ_CTRL:
4656 rc = -t4_ctrl_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4657 break;
4658 case EQ_ETH:
4659 rc = -t4_eth_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4660 break;
4661 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4662 case EQ_OFLD:
4663 rc = -t4_ofld_eq_free(sc, sc->mbox, sc->pf, 0, eq->cntxt_id);
4664 break;
4665 #endif
4666 default:
4667 panic("%s: invalid eq type %d.", __func__, eq->type);
4668 }
4669 if (rc != 0) {
4670 CH_ERR(sc, "failed to free eq (type %d): %d\n", eq->type, rc);
4671 return (rc);
4672 }
4673 eq->flags &= ~EQ_HW_ALLOCATED;
4674
4675 return (0);
4676 }
4677
4678 static int
alloc_wrq(struct adapter * sc,struct vi_info * vi,struct sge_wrq * wrq,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid)4679 alloc_wrq(struct adapter *sc, struct vi_info *vi, struct sge_wrq *wrq,
4680 struct sysctl_ctx_list *ctx, struct sysctl_oid *oid)
4681 {
4682 struct sge_eq *eq = &wrq->eq;
4683 int rc;
4684
4685 MPASS(!(eq->flags & EQ_SW_ALLOCATED));
4686
4687 rc = alloc_eq(sc, eq, ctx, oid);
4688 if (rc)
4689 return (rc);
4690 MPASS(eq->flags & EQ_SW_ALLOCATED);
4691 /* Can't fail after this. */
4692
4693 wrq->adapter = sc;
4694 TASK_INIT(&wrq->wrq_tx_task, 0, wrq_tx_drain, wrq);
4695 TAILQ_INIT(&wrq->incomplete_wrs);
4696 STAILQ_INIT(&wrq->wr_list);
4697 wrq->nwr_pending = 0;
4698 wrq->ndesc_needed = 0;
4699 add_wrq_sysctls(ctx, oid, wrq);
4700
4701 return (0);
4702 }
4703
4704 static void
free_wrq(struct adapter * sc,struct sge_wrq * wrq)4705 free_wrq(struct adapter *sc, struct sge_wrq *wrq)
4706 {
4707 free_eq(sc, &wrq->eq);
4708 MPASS(wrq->nwr_pending == 0);
4709 MPASS(wrq->ndesc_needed == 0);
4710 MPASS(TAILQ_EMPTY(&wrq->incomplete_wrs));
4711 MPASS(STAILQ_EMPTY(&wrq->wr_list));
4712 bzero(wrq, sizeof(*wrq));
4713 }
4714
4715 static void
add_wrq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_wrq * wrq)4716 add_wrq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
4717 struct sge_wrq *wrq)
4718 {
4719 struct sysctl_oid_list *children;
4720
4721 if (ctx == NULL || oid == NULL)
4722 return;
4723
4724 children = SYSCTL_CHILDREN(oid);
4725 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_direct", CTLFLAG_RD,
4726 &wrq->tx_wrs_direct, "# of work requests (direct)");
4727 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_copied", CTLFLAG_RD,
4728 &wrq->tx_wrs_copied, "# of work requests (copied)");
4729 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tx_wrs_sspace", CTLFLAG_RD,
4730 &wrq->tx_wrs_ss, "# of work requests (copied from scratch space)");
4731 }
4732
4733 /*
4734 * Idempotent.
4735 */
4736 static int
alloc_txq(struct vi_info * vi,struct sge_txq * txq,int idx)4737 alloc_txq(struct vi_info *vi, struct sge_txq *txq, int idx)
4738 {
4739 int rc, iqidx;
4740 struct port_info *pi = vi->pi;
4741 struct adapter *sc = vi->adapter;
4742 struct sge_eq *eq = &txq->eq;
4743 struct txpkts *txp;
4744 char name[16];
4745 struct sysctl_oid *oid;
4746
4747 if (!(eq->flags & EQ_SW_ALLOCATED)) {
4748 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
4749
4750 snprintf(name, sizeof(name), "%d", idx);
4751 oid = SYSCTL_ADD_NODE(&vi->ctx, SYSCTL_CHILDREN(vi->txq_oid),
4752 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
4753 "tx queue");
4754
4755 iqidx = vi->first_rxq + (idx % vi->nrxq);
4756 snprintf(name, sizeof(name), "%s txq%d",
4757 device_get_nameunit(vi->dev), idx);
4758 init_eq(sc, &txq->eq, EQ_ETH, vi->qsize_txq, pi->port_id,
4759 &sc->sge.rxq[iqidx].iq, name);
4760
4761 rc = mp_ring_alloc(&txq->r, eq->sidx, txq, eth_tx,
4762 can_resume_eth_tx, M_CXGBE, &eq->eq_lock, M_WAITOK);
4763 if (rc != 0) {
4764 CH_ERR(vi, "failed to allocate mp_ring for txq%d: %d\n",
4765 idx, rc);
4766 failed:
4767 sysctl_remove_oid(oid, 1, 1);
4768 return (rc);
4769 }
4770
4771 rc = alloc_eq(sc, eq, &vi->ctx, oid);
4772 if (rc) {
4773 CH_ERR(vi, "failed to allocate txq%d: %d\n", idx, rc);
4774 mp_ring_free(txq->r);
4775 goto failed;
4776 }
4777 MPASS(eq->flags & EQ_SW_ALLOCATED);
4778 /* Can't fail after this point. */
4779
4780 TASK_INIT(&txq->tx_reclaim_task, 0, tx_reclaim, eq);
4781 txq->ifp = vi->ifp;
4782 txq->gl = sglist_alloc(TX_SGL_SEGS, M_WAITOK);
4783 txq->sdesc = malloc(eq->sidx * sizeof(struct tx_sdesc), M_CXGBE,
4784 M_ZERO | M_WAITOK);
4785
4786 add_txq_sysctls(vi, &vi->ctx, oid, txq);
4787 }
4788
4789 if (!(eq->flags & EQ_HW_ALLOCATED)) {
4790 MPASS(eq->flags & EQ_SW_ALLOCATED);
4791 rc = alloc_eq_hwq(sc, vi, eq, idx);
4792 if (rc != 0) {
4793 CH_ERR(vi, "failed to create hw txq%d: %d\n", idx, rc);
4794 return (rc);
4795 }
4796 MPASS(eq->flags & EQ_HW_ALLOCATED);
4797 /* Can't fail after this point. */
4798
4799 if (idx == 0)
4800 sc->sge.eq_base = eq->abs_id - eq->cntxt_id;
4801 else
4802 KASSERT(eq->cntxt_id + sc->sge.eq_base == eq->abs_id,
4803 ("eq_base mismatch"));
4804 KASSERT(sc->sge.eq_base == 0 || sc->flags & IS_VF,
4805 ("PF with non-zero eq_base"));
4806
4807 txp = &txq->txp;
4808 MPASS(nitems(txp->mb) >= sc->params.max_pkts_per_eth_tx_pkts_wr);
4809 txq->txp.max_npkt = min(nitems(txp->mb),
4810 sc->params.max_pkts_per_eth_tx_pkts_wr);
4811 if (vi->flags & TX_USES_VM_WR && !(sc->flags & IS_VF))
4812 txq->txp.max_npkt--;
4813
4814 if (vi->flags & TX_USES_VM_WR)
4815 txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
4816 V_TXPKT_INTF(pi->hw_port));
4817 else
4818 txq->cpl_ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
4819 V_TXPKT_INTF(pi->hw_port) | V_TXPKT_PF(sc->pf) |
4820 V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
4821
4822 txq->tc_idx = -1;
4823 }
4824
4825 return (0);
4826 }
4827
4828 /*
4829 * Idempotent.
4830 */
4831 static void
free_txq(struct vi_info * vi,struct sge_txq * txq)4832 free_txq(struct vi_info *vi, struct sge_txq *txq)
4833 {
4834 struct adapter *sc = vi->adapter;
4835 struct sge_eq *eq = &txq->eq;
4836
4837 if (eq->flags & EQ_HW_ALLOCATED) {
4838 MPASS(eq->flags & EQ_SW_ALLOCATED);
4839 free_eq_hwq(sc, NULL, eq);
4840 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
4841 }
4842
4843 if (eq->flags & EQ_SW_ALLOCATED) {
4844 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
4845 sglist_free(txq->gl);
4846 free(txq->sdesc, M_CXGBE);
4847 mp_ring_free(txq->r);
4848 free_eq(sc, eq);
4849 MPASS(!(eq->flags & EQ_SW_ALLOCATED));
4850 bzero(txq, sizeof(*txq));
4851 }
4852 }
4853
4854 static void
add_txq_sysctls(struct vi_info * vi,struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_txq * txq)4855 add_txq_sysctls(struct vi_info *vi, struct sysctl_ctx_list *ctx,
4856 struct sysctl_oid *oid, struct sge_txq *txq)
4857 {
4858 struct adapter *sc;
4859 struct sysctl_oid_list *children;
4860
4861 if (ctx == NULL || oid == NULL)
4862 return;
4863
4864 sc = vi->adapter;
4865 children = SYSCTL_CHILDREN(oid);
4866
4867 mp_ring_sysctls(txq->r, ctx, children);
4868
4869 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tc",
4870 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, txq - sc->sge.txq,
4871 sysctl_tc, "I", "traffic class (-1 means none)");
4872
4873 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txcsum", CTLFLAG_RD,
4874 &txq->txcsum, "# of times hardware assisted with checksum");
4875 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vlan_insertion", CTLFLAG_RD,
4876 &txq->vlan_insertion, "# of times hardware inserted 802.1Q tag");
4877 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "tso_wrs", CTLFLAG_RD,
4878 &txq->tso_wrs, "# of TSO work requests");
4879 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "imm_wrs", CTLFLAG_RD,
4880 &txq->imm_wrs, "# of work requests with immediate data");
4881 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "sgl_wrs", CTLFLAG_RD,
4882 &txq->sgl_wrs, "# of work requests with direct SGL");
4883 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkt_wrs", CTLFLAG_RD,
4884 &txq->txpkt_wrs, "# of txpkt work requests (one pkt/WR)");
4885 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts0_wrs", CTLFLAG_RD,
4886 &txq->txpkts0_wrs, "# of txpkts (type 0) work requests");
4887 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts1_wrs", CTLFLAG_RD,
4888 &txq->txpkts1_wrs, "# of txpkts (type 1) work requests");
4889 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts0_pkts", CTLFLAG_RD,
4890 &txq->txpkts0_pkts,
4891 "# of frames tx'd using type0 txpkts work requests");
4892 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts1_pkts", CTLFLAG_RD,
4893 &txq->txpkts1_pkts,
4894 "# of frames tx'd using type1 txpkts work requests");
4895 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "txpkts_flush", CTLFLAG_RD,
4896 &txq->txpkts_flush,
4897 "# of times txpkts had to be flushed out by an egress-update");
4898 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "raw_wrs", CTLFLAG_RD,
4899 &txq->raw_wrs, "# of raw work requests (non-packets)");
4900 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_tso_wrs", CTLFLAG_RD,
4901 &txq->vxlan_tso_wrs, "# of VXLAN TSO work requests");
4902 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "vxlan_txcsum", CTLFLAG_RD,
4903 &txq->vxlan_txcsum,
4904 "# of times hardware assisted with inner checksums (VXLAN)");
4905
4906 #ifdef KERN_TLS
4907 if (is_ktls(sc)) {
4908 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_records",
4909 CTLFLAG_RD, &txq->kern_tls_records,
4910 "# of NIC TLS records transmitted");
4911 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_short",
4912 CTLFLAG_RD, &txq->kern_tls_short,
4913 "# of short NIC TLS records transmitted");
4914 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_partial",
4915 CTLFLAG_RD, &txq->kern_tls_partial,
4916 "# of partial NIC TLS records transmitted");
4917 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_full",
4918 CTLFLAG_RD, &txq->kern_tls_full,
4919 "# of full NIC TLS records transmitted");
4920 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_octets",
4921 CTLFLAG_RD, &txq->kern_tls_octets,
4922 "# of payload octets in transmitted NIC TLS records");
4923 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_waste",
4924 CTLFLAG_RD, &txq->kern_tls_waste,
4925 "# of octets DMAd but not transmitted in NIC TLS records");
4926 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_header",
4927 CTLFLAG_RD, &txq->kern_tls_header,
4928 "# of NIC TLS header-only packets transmitted");
4929 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_fin_short",
4930 CTLFLAG_RD, &txq->kern_tls_fin_short,
4931 "# of NIC TLS padded FIN packets on short TLS records");
4932 if (is_t6(sc)) {
4933 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4934 "kern_tls_options", CTLFLAG_RD,
4935 &txq->kern_tls_options,
4936 "# of NIC TLS options-only packets transmitted");
4937 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4938 "kern_tls_fin", CTLFLAG_RD, &txq->kern_tls_fin,
4939 "# of NIC TLS FIN-only packets transmitted");
4940 } else {
4941 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4942 "kern_tls_ghash_received", CTLFLAG_RD,
4943 &txq->kern_tls_ghash_received,
4944 "# of NIC TLS GHASHes received");
4945 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4946 "kern_tls_ghash_requested", CTLFLAG_RD,
4947 &txq->kern_tls_ghash_requested,
4948 "# of NIC TLS GHASHes requested");
4949 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4950 "kern_tls_lso", CTLFLAG_RD,
4951 &txq->kern_tls_lso,
4952 "# of NIC TLS records transmitted using LSO");
4953 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4954 "kern_tls_partial_ghash", CTLFLAG_RD,
4955 &txq->kern_tls_partial_ghash,
4956 "# of NIC TLS records encrypted using a partial GHASH");
4957 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4958 "kern_tls_splitmode", CTLFLAG_RD,
4959 &txq->kern_tls_splitmode,
4960 "# of NIC TLS records using SplitMode");
4961 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO,
4962 "kern_tls_trailer", CTLFLAG_RD,
4963 &txq->kern_tls_trailer,
4964 "# of NIC TLS trailer-only packets transmitted");
4965 }
4966 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_cbc",
4967 CTLFLAG_RD, &txq->kern_tls_cbc,
4968 "# of NIC TLS sessions using AES-CBC");
4969 SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "kern_tls_gcm",
4970 CTLFLAG_RD, &txq->kern_tls_gcm,
4971 "# of NIC TLS sessions using AES-GCM");
4972 }
4973 #endif
4974 }
4975
4976 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4977 /*
4978 * Idempotent.
4979 */
4980 static int
alloc_ofld_txq(struct vi_info * vi,struct sge_ofld_txq * ofld_txq,int idx)4981 alloc_ofld_txq(struct vi_info *vi, struct sge_ofld_txq *ofld_txq, int idx)
4982 {
4983 struct sysctl_oid *oid;
4984 struct port_info *pi = vi->pi;
4985 struct adapter *sc = vi->adapter;
4986 struct sge_eq *eq = &ofld_txq->wrq.eq;
4987 int rc, iqidx;
4988 char name[16];
4989
4990 MPASS(idx >= 0);
4991 MPASS(idx < vi->nofldtxq);
4992
4993 if (!(eq->flags & EQ_SW_ALLOCATED)) {
4994 snprintf(name, sizeof(name), "%d", idx);
4995 oid = SYSCTL_ADD_NODE(&vi->ctx,
4996 SYSCTL_CHILDREN(vi->ofld_txq_oid), OID_AUTO, name,
4997 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "offload tx queue");
4998
4999 snprintf(name, sizeof(name), "%s ofld_txq%d",
5000 device_get_nameunit(vi->dev), idx);
5001 if (vi->nofldrxq > 0) {
5002 iqidx = vi->first_ofld_rxq + (idx % vi->nofldrxq);
5003 init_eq(sc, eq, EQ_OFLD, vi->qsize_txq, pi->port_id,
5004 &sc->sge.ofld_rxq[iqidx].iq, name);
5005 } else {
5006 iqidx = vi->first_rxq + (idx % vi->nrxq);
5007 init_eq(sc, eq, EQ_OFLD, vi->qsize_txq, pi->port_id,
5008 &sc->sge.rxq[iqidx].iq, name);
5009 }
5010
5011 rc = alloc_wrq(sc, vi, &ofld_txq->wrq, &vi->ctx, oid);
5012 if (rc != 0) {
5013 CH_ERR(vi, "failed to allocate ofld_txq%d: %d\n", idx,
5014 rc);
5015 sysctl_remove_oid(oid, 1, 1);
5016 return (rc);
5017 }
5018 MPASS(eq->flags & EQ_SW_ALLOCATED);
5019 /* Can't fail after this point. */
5020
5021 ofld_txq->tx_iscsi_pdus = counter_u64_alloc(M_WAITOK);
5022 ofld_txq->tx_iscsi_octets = counter_u64_alloc(M_WAITOK);
5023 ofld_txq->tx_iscsi_iso_wrs = counter_u64_alloc(M_WAITOK);
5024 ofld_txq->tx_nvme_pdus = counter_u64_alloc(M_WAITOK);
5025 ofld_txq->tx_nvme_octets = counter_u64_alloc(M_WAITOK);
5026 ofld_txq->tx_nvme_iso_wrs = counter_u64_alloc(M_WAITOK);
5027 ofld_txq->tx_aio_jobs = counter_u64_alloc(M_WAITOK);
5028 ofld_txq->tx_aio_octets = counter_u64_alloc(M_WAITOK);
5029 ofld_txq->tx_toe_tls_records = counter_u64_alloc(M_WAITOK);
5030 ofld_txq->tx_toe_tls_octets = counter_u64_alloc(M_WAITOK);
5031 add_ofld_txq_sysctls(&vi->ctx, oid, ofld_txq);
5032 }
5033
5034 if (!(eq->flags & EQ_HW_ALLOCATED)) {
5035 MPASS(eq->flags & EQ_SW_ALLOCATED);
5036 MPASS(ofld_txq->wrq.nwr_pending == 0);
5037 MPASS(ofld_txq->wrq.ndesc_needed == 0);
5038 rc = alloc_eq_hwq(sc, vi, eq, idx);
5039 if (rc != 0) {
5040 CH_ERR(vi, "failed to create hw ofld_txq%d: %d\n", idx,
5041 rc);
5042 return (rc);
5043 }
5044 MPASS(eq->flags & EQ_HW_ALLOCATED);
5045 }
5046
5047 return (0);
5048 }
5049
5050 /*
5051 * Idempotent.
5052 */
5053 static void
free_ofld_txq(struct vi_info * vi,struct sge_ofld_txq * ofld_txq)5054 free_ofld_txq(struct vi_info *vi, struct sge_ofld_txq *ofld_txq)
5055 {
5056 struct adapter *sc = vi->adapter;
5057 struct sge_eq *eq = &ofld_txq->wrq.eq;
5058
5059 if (eq->flags & EQ_HW_ALLOCATED) {
5060 MPASS(eq->flags & EQ_SW_ALLOCATED);
5061 free_eq_hwq(sc, NULL, eq);
5062 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
5063 }
5064
5065 if (eq->flags & EQ_SW_ALLOCATED) {
5066 MPASS(!(eq->flags & EQ_HW_ALLOCATED));
5067 counter_u64_free(ofld_txq->tx_iscsi_pdus);
5068 counter_u64_free(ofld_txq->tx_iscsi_octets);
5069 counter_u64_free(ofld_txq->tx_iscsi_iso_wrs);
5070 counter_u64_free(ofld_txq->tx_nvme_pdus);
5071 counter_u64_free(ofld_txq->tx_nvme_octets);
5072 counter_u64_free(ofld_txq->tx_nvme_iso_wrs);
5073 counter_u64_free(ofld_txq->tx_aio_jobs);
5074 counter_u64_free(ofld_txq->tx_aio_octets);
5075 counter_u64_free(ofld_txq->tx_toe_tls_records);
5076 counter_u64_free(ofld_txq->tx_toe_tls_octets);
5077 free_wrq(sc, &ofld_txq->wrq);
5078 MPASS(!(eq->flags & EQ_SW_ALLOCATED));
5079 bzero(ofld_txq, sizeof(*ofld_txq));
5080 }
5081 }
5082
5083 static void
add_ofld_txq_sysctls(struct sysctl_ctx_list * ctx,struct sysctl_oid * oid,struct sge_ofld_txq * ofld_txq)5084 add_ofld_txq_sysctls(struct sysctl_ctx_list *ctx, struct sysctl_oid *oid,
5085 struct sge_ofld_txq *ofld_txq)
5086 {
5087 struct sysctl_oid_list *children;
5088
5089 if (ctx == NULL || oid == NULL)
5090 return;
5091
5092 children = SYSCTL_CHILDREN(oid);
5093 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_pdus",
5094 CTLFLAG_RD, &ofld_txq->tx_iscsi_pdus,
5095 "# of iSCSI PDUs transmitted");
5096 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_octets",
5097 CTLFLAG_RD, &ofld_txq->tx_iscsi_octets,
5098 "# of payload octets in transmitted iSCSI PDUs");
5099 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_iscsi_iso_wrs",
5100 CTLFLAG_RD, &ofld_txq->tx_iscsi_iso_wrs,
5101 "# of iSCSI segmentation offload work requests");
5102 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_nvme_pdus",
5103 CTLFLAG_RD, &ofld_txq->tx_nvme_pdus,
5104 "# of NVMe PDUs transmitted");
5105 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_nvme_octets",
5106 CTLFLAG_RD, &ofld_txq->tx_nvme_octets,
5107 "# of payload octets in transmitted NVMe PDUs");
5108 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_nvme_iso_wrs",
5109 CTLFLAG_RD, &ofld_txq->tx_nvme_iso_wrs,
5110 "# of NVMe segmentation offload work requests");
5111 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_aio_jobs",
5112 CTLFLAG_RD, &ofld_txq->tx_aio_jobs,
5113 "# of zero-copy aio_write(2) jobs transmitted");
5114 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_aio_octets",
5115 CTLFLAG_RD, &ofld_txq->tx_aio_octets,
5116 "# of payload octets in transmitted zero-copy aio_write(2) jobs");
5117 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_toe_tls_records",
5118 CTLFLAG_RD, &ofld_txq->tx_toe_tls_records,
5119 "# of TOE TLS records transmitted");
5120 SYSCTL_ADD_COUNTER_U64(ctx, children, OID_AUTO, "tx_toe_tls_octets",
5121 CTLFLAG_RD, &ofld_txq->tx_toe_tls_octets,
5122 "# of payload octets in transmitted TOE TLS records");
5123 }
5124 #endif
5125
5126 static void
oneseg_dma_callback(void * arg,bus_dma_segment_t * segs,int nseg,int error)5127 oneseg_dma_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
5128 {
5129 bus_addr_t *ba = arg;
5130
5131 KASSERT(nseg == 1,
5132 ("%s meant for single segment mappings only.", __func__));
5133
5134 *ba = error ? 0 : segs->ds_addr;
5135 }
5136
5137 static inline void
ring_fl_db(struct adapter * sc,struct sge_fl * fl)5138 ring_fl_db(struct adapter *sc, struct sge_fl *fl)
5139 {
5140 uint32_t n, v;
5141
5142 n = IDXDIFF(fl->pidx >> 3, fl->dbidx, fl->sidx);
5143 MPASS(n > 0);
5144
5145 wmb();
5146 v = fl->dbval | V_PIDX(n);
5147 if (fl->udb)
5148 *fl->udb = htole32(v);
5149 else
5150 t4_write_reg(sc, sc->sge_kdoorbell_reg, v);
5151 IDXINCR(fl->dbidx, n, fl->sidx);
5152 }
5153
5154 /*
5155 * Fills up the freelist by allocating up to 'n' buffers. Buffers that are
5156 * recycled do not count towards this allocation budget.
5157 *
5158 * Returns non-zero to indicate that this freelist should be added to the list
5159 * of starving freelists.
5160 */
5161 static int
refill_fl(struct adapter * sc,struct sge_fl * fl,int n)5162 refill_fl(struct adapter *sc, struct sge_fl *fl, int n)
5163 {
5164 __be64 *d;
5165 struct fl_sdesc *sd;
5166 uintptr_t pa;
5167 caddr_t cl;
5168 struct rx_buf_info *rxb;
5169 struct cluster_metadata *clm;
5170 uint16_t max_pidx, zidx = fl->zidx;
5171 uint16_t hw_cidx = fl->hw_cidx; /* stable snapshot */
5172
5173 FL_LOCK_ASSERT_OWNED(fl);
5174
5175 /*
5176 * We always stop at the beginning of the hardware descriptor that's just
5177 * before the one with the hw cidx. This is to avoid hw pidx = hw cidx,
5178 * which would mean an empty freelist to the chip.
5179 */
5180 max_pidx = __predict_false(hw_cidx == 0) ? fl->sidx - 1 : hw_cidx - 1;
5181 if (fl->pidx == max_pidx * 8)
5182 return (0);
5183
5184 d = &fl->desc[fl->pidx];
5185 sd = &fl->sdesc[fl->pidx];
5186 rxb = &sc->sge.rx_buf_info[zidx];
5187
5188 while (n > 0) {
5189
5190 if (sd->cl != NULL) {
5191
5192 if (sd->nmbuf == 0) {
5193 /*
5194 * Fast recycle without involving any atomics on
5195 * the cluster's metadata (if the cluster has
5196 * metadata). This happens when all frames
5197 * received in the cluster were small enough to
5198 * fit within a single mbuf each.
5199 */
5200 fl->cl_fast_recycled++;
5201 goto recycled;
5202 }
5203
5204 /*
5205 * Cluster is guaranteed to have metadata. Clusters
5206 * without metadata always take the fast recycle path
5207 * when they're recycled.
5208 */
5209 clm = cl_metadata(sd);
5210 MPASS(clm != NULL);
5211
5212 if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
5213 fl->cl_recycled++;
5214 counter_u64_add(extfree_rels, 1);
5215 goto recycled;
5216 }
5217 sd->cl = NULL; /* gave up my reference */
5218 }
5219 MPASS(sd->cl == NULL);
5220 cl = uma_zalloc(rxb->zone, M_NOWAIT);
5221 if (__predict_false(cl == NULL)) {
5222 if (zidx != fl->safe_zidx) {
5223 zidx = fl->safe_zidx;
5224 rxb = &sc->sge.rx_buf_info[zidx];
5225 cl = uma_zalloc(rxb->zone, M_NOWAIT);
5226 }
5227 if (cl == NULL)
5228 break;
5229 }
5230 fl->cl_allocated++;
5231 n--;
5232
5233 pa = pmap_kextract((vm_offset_t)cl);
5234 sd->cl = cl;
5235 sd->zidx = zidx;
5236
5237 if (fl->flags & FL_BUF_PACKING) {
5238 *d = htobe64(pa | rxb->hwidx2);
5239 sd->moff = rxb->size2;
5240 } else {
5241 *d = htobe64(pa | rxb->hwidx1);
5242 sd->moff = 0;
5243 }
5244 recycled:
5245 sd->nmbuf = 0;
5246 d++;
5247 sd++;
5248 if (__predict_false((++fl->pidx & 7) == 0)) {
5249 uint16_t pidx = fl->pidx >> 3;
5250
5251 if (__predict_false(pidx == fl->sidx)) {
5252 fl->pidx = 0;
5253 pidx = 0;
5254 sd = fl->sdesc;
5255 d = fl->desc;
5256 }
5257 if (n < 8 || pidx == max_pidx)
5258 break;
5259
5260 if (IDXDIFF(pidx, fl->dbidx, fl->sidx) >= 4)
5261 ring_fl_db(sc, fl);
5262 }
5263 }
5264
5265 if ((fl->pidx >> 3) != fl->dbidx)
5266 ring_fl_db(sc, fl);
5267
5268 return (FL_RUNNING_LOW(fl) && !(fl->flags & FL_STARVING));
5269 }
5270
5271 /*
5272 * Attempt to refill all starving freelists.
5273 */
5274 static void
refill_sfl(void * arg)5275 refill_sfl(void *arg)
5276 {
5277 struct adapter *sc = arg;
5278 struct sge_fl *fl, *fl_temp;
5279
5280 mtx_assert(&sc->sfl_lock, MA_OWNED);
5281 TAILQ_FOREACH_SAFE(fl, &sc->sfl, link, fl_temp) {
5282 FL_LOCK(fl);
5283 refill_fl(sc, fl, 64);
5284 if (FL_NOT_RUNNING_LOW(fl) || fl->flags & FL_DOOMED) {
5285 TAILQ_REMOVE(&sc->sfl, fl, link);
5286 fl->flags &= ~FL_STARVING;
5287 }
5288 FL_UNLOCK(fl);
5289 }
5290
5291 if (!TAILQ_EMPTY(&sc->sfl))
5292 callout_schedule(&sc->sfl_callout, hz / 5);
5293 }
5294
5295 /*
5296 * Release the driver's reference on all buffers in the given freelist. Buffers
5297 * with kernel references cannot be freed and will prevent the driver from being
5298 * unloaded safely.
5299 */
5300 void
free_fl_buffers(struct adapter * sc,struct sge_fl * fl)5301 free_fl_buffers(struct adapter *sc, struct sge_fl *fl)
5302 {
5303 struct fl_sdesc *sd;
5304 struct cluster_metadata *clm;
5305 int i;
5306
5307 sd = fl->sdesc;
5308 for (i = 0; i < fl->sidx * 8; i++, sd++) {
5309 if (sd->cl == NULL)
5310 continue;
5311
5312 if (sd->nmbuf == 0)
5313 uma_zfree(sc->sge.rx_buf_info[sd->zidx].zone, sd->cl);
5314 else if (fl->flags & FL_BUF_PACKING) {
5315 clm = cl_metadata(sd);
5316 if (atomic_fetchadd_int(&clm->refcount, -1) == 1) {
5317 uma_zfree(sc->sge.rx_buf_info[sd->zidx].zone,
5318 sd->cl);
5319 counter_u64_add(extfree_rels, 1);
5320 }
5321 }
5322 sd->cl = NULL;
5323 }
5324
5325 if (fl->flags & FL_BUF_RESUME) {
5326 m_freem(fl->m0);
5327 fl->flags &= ~FL_BUF_RESUME;
5328 }
5329 }
5330
5331 static inline void
get_pkt_gl(struct mbuf * m,struct sglist * gl)5332 get_pkt_gl(struct mbuf *m, struct sglist *gl)
5333 {
5334 int rc;
5335
5336 M_ASSERTPKTHDR(m);
5337
5338 sglist_reset(gl);
5339 rc = sglist_append_mbuf(gl, m);
5340 if (__predict_false(rc != 0)) {
5341 panic("%s: mbuf %p (%d segs) was vetted earlier but now fails "
5342 "with %d.", __func__, m, mbuf_nsegs(m), rc);
5343 }
5344
5345 KASSERT(gl->sg_nseg == mbuf_nsegs(m),
5346 ("%s: nsegs changed for mbuf %p from %d to %d", __func__, m,
5347 mbuf_nsegs(m), gl->sg_nseg));
5348 #if 0 /* vm_wr not readily available here. */
5349 KASSERT(gl->sg_nseg > 0 && gl->sg_nseg <= max_nsegs_allowed(m, vm_wr),
5350 ("%s: %d segments, should have been 1 <= nsegs <= %d", __func__,
5351 gl->sg_nseg, max_nsegs_allowed(m, vm_wr)));
5352 #endif
5353 }
5354
5355 /*
5356 * len16 for a txpkt WR with a GL. Includes the firmware work request header.
5357 */
5358 static inline u_int
txpkt_len16(u_int nsegs,const u_int extra)5359 txpkt_len16(u_int nsegs, const u_int extra)
5360 {
5361 u_int n;
5362
5363 MPASS(nsegs > 0);
5364
5365 nsegs--; /* first segment is part of ulptx_sgl */
5366 n = extra + sizeof(struct fw_eth_tx_pkt_wr) +
5367 sizeof(struct cpl_tx_pkt_core) +
5368 sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
5369
5370 return (howmany(n, 16));
5371 }
5372
5373 /*
5374 * len16 for a txpkt_vm WR with a GL. Includes the firmware work
5375 * request header.
5376 */
5377 static inline u_int
txpkt_vm_len16(u_int nsegs,const u_int extra)5378 txpkt_vm_len16(u_int nsegs, const u_int extra)
5379 {
5380 u_int n;
5381
5382 MPASS(nsegs > 0);
5383
5384 nsegs--; /* first segment is part of ulptx_sgl */
5385 n = extra + sizeof(struct fw_eth_tx_pkt_vm_wr) +
5386 sizeof(struct cpl_tx_pkt_core) +
5387 sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
5388
5389 return (howmany(n, 16));
5390 }
5391
5392 static inline void
calculate_mbuf_len16(struct mbuf * m,bool vm_wr)5393 calculate_mbuf_len16(struct mbuf *m, bool vm_wr)
5394 {
5395 const int lso = sizeof(struct cpl_tx_pkt_lso_core);
5396 const int tnl_lso = sizeof(struct cpl_tx_tnl_lso);
5397
5398 if (vm_wr) {
5399 if (needs_tso(m))
5400 set_mbuf_len16(m, txpkt_vm_len16(mbuf_nsegs(m), lso));
5401 else
5402 set_mbuf_len16(m, txpkt_vm_len16(mbuf_nsegs(m), 0));
5403 return;
5404 }
5405
5406 if (needs_tso(m)) {
5407 if (needs_vxlan_tso(m))
5408 set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), tnl_lso));
5409 else
5410 set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), lso));
5411 } else
5412 set_mbuf_len16(m, txpkt_len16(mbuf_nsegs(m), 0));
5413 }
5414
5415 /*
5416 * len16 for a txpkts type 0 WR with a GL. Does not include the firmware work
5417 * request header.
5418 */
5419 static inline u_int
txpkts0_len16(u_int nsegs)5420 txpkts0_len16(u_int nsegs)
5421 {
5422 u_int n;
5423
5424 MPASS(nsegs > 0);
5425
5426 nsegs--; /* first segment is part of ulptx_sgl */
5427 n = sizeof(struct ulp_txpkt) + sizeof(struct ulptx_idata) +
5428 sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl) +
5429 8 * ((3 * nsegs) / 2 + (nsegs & 1));
5430
5431 return (howmany(n, 16));
5432 }
5433
5434 /*
5435 * len16 for a txpkts type 1 WR with a GL. Does not include the firmware work
5436 * request header.
5437 */
5438 static inline u_int
txpkts1_len16(void)5439 txpkts1_len16(void)
5440 {
5441 u_int n;
5442
5443 n = sizeof(struct cpl_tx_pkt_core) + sizeof(struct ulptx_sgl);
5444
5445 return (howmany(n, 16));
5446 }
5447
5448 static inline u_int
imm_payload(u_int ndesc)5449 imm_payload(u_int ndesc)
5450 {
5451 u_int n;
5452
5453 n = ndesc * EQ_ESIZE - sizeof(struct fw_eth_tx_pkt_wr) -
5454 sizeof(struct cpl_tx_pkt_core);
5455
5456 return (n);
5457 }
5458
5459 static inline uint64_t
csum_to_ctrl(struct adapter * sc,struct mbuf * m)5460 csum_to_ctrl(struct adapter *sc, struct mbuf *m)
5461 {
5462 uint64_t ctrl;
5463 int csum_type, l2hlen, l3hlen;
5464 int x, y;
5465 static const int csum_types[3][2] = {
5466 {TX_CSUM_TCPIP, TX_CSUM_TCPIP6},
5467 {TX_CSUM_UDPIP, TX_CSUM_UDPIP6},
5468 {TX_CSUM_IP, 0}
5469 };
5470
5471 M_ASSERTPKTHDR(m);
5472
5473 if (!needs_hwcsum(m))
5474 return (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS);
5475
5476 MPASS(m->m_pkthdr.l2hlen >= ETHER_HDR_LEN);
5477 MPASS(m->m_pkthdr.l3hlen >= sizeof(struct ip));
5478
5479 if (needs_vxlan_csum(m)) {
5480 MPASS(m->m_pkthdr.l4hlen > 0);
5481 MPASS(m->m_pkthdr.l5hlen > 0);
5482 MPASS(m->m_pkthdr.inner_l2hlen >= ETHER_HDR_LEN);
5483 MPASS(m->m_pkthdr.inner_l3hlen >= sizeof(struct ip));
5484
5485 l2hlen = m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen +
5486 m->m_pkthdr.l4hlen + m->m_pkthdr.l5hlen +
5487 m->m_pkthdr.inner_l2hlen - ETHER_HDR_LEN;
5488 l3hlen = m->m_pkthdr.inner_l3hlen;
5489 } else {
5490 l2hlen = m->m_pkthdr.l2hlen - ETHER_HDR_LEN;
5491 l3hlen = m->m_pkthdr.l3hlen;
5492 }
5493
5494 ctrl = 0;
5495 if (!needs_l3_csum(m))
5496 ctrl |= F_TXPKT_IPCSUM_DIS;
5497
5498 if (m->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_INNER_IP_TCP |
5499 CSUM_IP6_TCP | CSUM_INNER_IP6_TCP))
5500 x = 0; /* TCP */
5501 else if (m->m_pkthdr.csum_flags & (CSUM_IP_UDP | CSUM_INNER_IP_UDP |
5502 CSUM_IP6_UDP | CSUM_INNER_IP6_UDP))
5503 x = 1; /* UDP */
5504 else
5505 x = 2;
5506
5507 if (m->m_pkthdr.csum_flags & (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP |
5508 CSUM_INNER_IP | CSUM_INNER_IP_TCP | CSUM_INNER_IP_UDP))
5509 y = 0; /* IPv4 */
5510 else {
5511 MPASS(m->m_pkthdr.csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP |
5512 CSUM_INNER_IP6_TCP | CSUM_INNER_IP6_UDP));
5513 y = 1; /* IPv6 */
5514 }
5515 /*
5516 * needs_hwcsum returned true earlier so there must be some kind of
5517 * checksum to calculate.
5518 */
5519 csum_type = csum_types[x][y];
5520 MPASS(csum_type != 0);
5521 if (csum_type == TX_CSUM_IP)
5522 ctrl |= F_TXPKT_L4CSUM_DIS;
5523 ctrl |= V_TXPKT_CSUM_TYPE(csum_type) | V_TXPKT_IPHDR_LEN(l3hlen);
5524 if (chip_id(sc) <= CHELSIO_T5)
5525 ctrl |= V_TXPKT_ETHHDR_LEN(l2hlen);
5526 else
5527 ctrl |= V_T6_TXPKT_ETHHDR_LEN(l2hlen);
5528
5529 return (ctrl);
5530 }
5531
5532 static inline void *
write_lso_cpl(void * cpl,struct mbuf * m0)5533 write_lso_cpl(void *cpl, struct mbuf *m0)
5534 {
5535 struct cpl_tx_pkt_lso_core *lso;
5536 uint32_t ctrl;
5537
5538 KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
5539 m0->m_pkthdr.l4hlen > 0,
5540 ("%s: mbuf %p needs TSO but missing header lengths",
5541 __func__, m0));
5542
5543 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) |
5544 F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
5545 V_LSO_ETHHDR_LEN((m0->m_pkthdr.l2hlen - ETHER_HDR_LEN) >> 2) |
5546 V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2) |
5547 V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
5548 if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
5549 ctrl |= F_LSO_IPV6;
5550
5551 lso = cpl;
5552 lso->lso_ctrl = htobe32(ctrl);
5553 lso->ipid_ofst = htobe16(0);
5554 lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
5555 lso->seqno_offset = htobe32(0);
5556 lso->len = htobe32(m0->m_pkthdr.len);
5557
5558 return (lso + 1);
5559 }
5560
5561 static void *
write_tnl_lso_cpl(void * cpl,struct mbuf * m0)5562 write_tnl_lso_cpl(void *cpl, struct mbuf *m0)
5563 {
5564 struct cpl_tx_tnl_lso *tnl_lso = cpl;
5565 uint32_t ctrl;
5566
5567 KASSERT(m0->m_pkthdr.inner_l2hlen > 0 &&
5568 m0->m_pkthdr.inner_l3hlen > 0 && m0->m_pkthdr.inner_l4hlen > 0 &&
5569 m0->m_pkthdr.inner_l5hlen > 0,
5570 ("%s: mbuf %p needs VXLAN_TSO but missing inner header lengths",
5571 __func__, m0));
5572 KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
5573 m0->m_pkthdr.l4hlen > 0 && m0->m_pkthdr.l5hlen > 0,
5574 ("%s: mbuf %p needs VXLAN_TSO but missing outer header lengths",
5575 __func__, m0));
5576
5577 /* Outer headers. */
5578 ctrl = V_CPL_TX_TNL_LSO_OPCODE(CPL_TX_TNL_LSO) |
5579 F_CPL_TX_TNL_LSO_FIRST | F_CPL_TX_TNL_LSO_LAST |
5580 V_CPL_TX_TNL_LSO_ETHHDRLENOUT(
5581 (m0->m_pkthdr.l2hlen - ETHER_HDR_LEN) >> 2) |
5582 V_CPL_TX_TNL_LSO_IPHDRLENOUT(m0->m_pkthdr.l3hlen >> 2) |
5583 F_CPL_TX_TNL_LSO_IPLENSETOUT;
5584 if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
5585 ctrl |= F_CPL_TX_TNL_LSO_IPV6OUT;
5586 else {
5587 ctrl |= F_CPL_TX_TNL_LSO_IPHDRCHKOUT |
5588 F_CPL_TX_TNL_LSO_IPIDINCOUT;
5589 }
5590 tnl_lso->op_to_IpIdSplitOut = htobe32(ctrl);
5591 tnl_lso->IpIdOffsetOut = 0;
5592 tnl_lso->UdpLenSetOut_to_TnlHdrLen =
5593 htobe16(F_CPL_TX_TNL_LSO_UDPCHKCLROUT |
5594 F_CPL_TX_TNL_LSO_UDPLENSETOUT |
5595 V_CPL_TX_TNL_LSO_TNLHDRLEN(m0->m_pkthdr.l2hlen +
5596 m0->m_pkthdr.l3hlen + m0->m_pkthdr.l4hlen +
5597 m0->m_pkthdr.l5hlen) |
5598 V_CPL_TX_TNL_LSO_TNLTYPE(TX_TNL_TYPE_VXLAN));
5599 tnl_lso->ipsecen_to_rocev2 = 0;
5600 tnl_lso->roce_eth = 0;
5601
5602 /* Inner headers. */
5603 ctrl = V_CPL_TX_TNL_LSO_ETHHDRLEN(
5604 (m0->m_pkthdr.inner_l2hlen - ETHER_HDR_LEN) >> 2) |
5605 V_CPL_TX_TNL_LSO_IPHDRLEN(m0->m_pkthdr.inner_l3hlen >> 2) |
5606 V_CPL_TX_TNL_LSO_TCPHDRLEN(m0->m_pkthdr.inner_l4hlen >> 2);
5607 if (m0->m_pkthdr.inner_l3hlen == sizeof(struct ip6_hdr))
5608 ctrl |= F_CPL_TX_TNL_LSO_IPV6;
5609 tnl_lso->Flow_to_TcpHdrLen = htobe32(ctrl);
5610 tnl_lso->IpIdOffset = 0;
5611 tnl_lso->IpIdSplit_to_Mss =
5612 htobe16(V_CPL_TX_TNL_LSO_MSS(m0->m_pkthdr.tso_segsz));
5613 tnl_lso->TCPSeqOffset = 0;
5614 tnl_lso->EthLenOffset_Size =
5615 htobe32(V_CPL_TX_TNL_LSO_SIZE(m0->m_pkthdr.len));
5616
5617 return (tnl_lso + 1);
5618 }
5619
5620 #define VM_TX_L2HDR_LEN 16 /* ethmacdst to vlantci */
5621
5622 /*
5623 * Write a VM txpkt WR for this packet to the hardware descriptors, update the
5624 * software descriptor, and advance the pidx. It is guaranteed that enough
5625 * descriptors are available.
5626 *
5627 * The return value is the # of hardware descriptors used.
5628 */
5629 static u_int
write_txpkt_vm_wr(struct adapter * sc,struct sge_txq * txq,struct mbuf * m0)5630 write_txpkt_vm_wr(struct adapter *sc, struct sge_txq *txq, struct mbuf *m0)
5631 {
5632 struct sge_eq *eq;
5633 struct fw_eth_tx_pkt_vm_wr *wr;
5634 struct tx_sdesc *txsd;
5635 struct cpl_tx_pkt_core *cpl;
5636 uint32_t ctrl; /* used in many unrelated places */
5637 uint64_t ctrl1;
5638 int len16, ndesc, pktlen;
5639 caddr_t dst;
5640
5641 TXQ_LOCK_ASSERT_OWNED(txq);
5642 M_ASSERTPKTHDR(m0);
5643
5644 len16 = mbuf_len16(m0);
5645 pktlen = m0->m_pkthdr.len;
5646 ctrl = sizeof(struct cpl_tx_pkt_core);
5647 if (needs_tso(m0))
5648 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
5649 ndesc = tx_len16_to_desc(len16);
5650
5651 /* Firmware work request header */
5652 eq = &txq->eq;
5653 wr = (void *)&eq->desc[eq->pidx];
5654 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_VM_WR) |
5655 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
5656
5657 ctrl = V_FW_WR_LEN16(len16);
5658 wr->equiq_to_len16 = htobe32(ctrl);
5659 wr->r3[0] = 0;
5660 wr->r3[1] = 0;
5661
5662 /*
5663 * Copy over ethmacdst, ethmacsrc, ethtype, and vlantci.
5664 * vlantci is ignored unless the ethtype is 0x8100, so it's
5665 * simpler to always copy it rather than making it
5666 * conditional. Also, it seems that we do not have to set
5667 * vlantci or fake the ethtype when doing VLAN tag insertion.
5668 */
5669 m_copydata(m0, 0, VM_TX_L2HDR_LEN, wr->ethmacdst);
5670
5671 if (needs_tso(m0)) {
5672 cpl = write_lso_cpl(wr + 1, m0);
5673 txq->tso_wrs++;
5674 } else
5675 cpl = (void *)(wr + 1);
5676
5677 /* Checksum offload */
5678 ctrl1 = csum_to_ctrl(sc, m0);
5679 if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS))
5680 txq->txcsum++; /* some hardware assistance provided */
5681
5682 /* VLAN tag insertion */
5683 if (needs_vlan_insertion(m0)) {
5684 ctrl1 |= F_TXPKT_VLAN_VLD |
5685 V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
5686 txq->vlan_insertion++;
5687 } else if (sc->vlan_id)
5688 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(sc->vlan_id);
5689
5690 /* CPL header */
5691 cpl->ctrl0 = txq->cpl_ctrl0;
5692 cpl->pack = 0;
5693 cpl->len = htobe16(pktlen);
5694 cpl->ctrl1 = htobe64(ctrl1);
5695
5696 /* SGL */
5697 dst = (void *)(cpl + 1);
5698
5699 /*
5700 * A packet using TSO will use up an entire descriptor for the
5701 * firmware work request header, LSO CPL, and TX_PKT_XT CPL.
5702 * If this descriptor is the last descriptor in the ring, wrap
5703 * around to the front of the ring explicitly for the start of
5704 * the sgl.
5705 */
5706 if (dst == (void *)&eq->desc[eq->sidx]) {
5707 dst = (void *)&eq->desc[0];
5708 write_gl_to_txd(txq, m0, &dst, 0);
5709 } else
5710 write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
5711 txq->sgl_wrs++;
5712 txq->txpkt_wrs++;
5713
5714 txsd = &txq->sdesc[eq->pidx];
5715 txsd->m = m0;
5716 txsd->desc_used = ndesc;
5717
5718 return (ndesc);
5719 }
5720
5721 /*
5722 * Write a raw WR to the hardware descriptors, update the software
5723 * descriptor, and advance the pidx. It is guaranteed that enough
5724 * descriptors are available.
5725 *
5726 * The return value is the # of hardware descriptors used.
5727 */
5728 static u_int
write_raw_wr(struct sge_txq * txq,void * wr,struct mbuf * m0,u_int available)5729 write_raw_wr(struct sge_txq *txq, void *wr, struct mbuf *m0, u_int available)
5730 {
5731 struct sge_eq *eq = &txq->eq;
5732 struct tx_sdesc *txsd;
5733 struct mbuf *m;
5734 caddr_t dst;
5735 int len16, ndesc;
5736
5737 len16 = mbuf_len16(m0);
5738 ndesc = tx_len16_to_desc(len16);
5739 MPASS(ndesc <= available);
5740
5741 dst = wr;
5742 for (m = m0; m != NULL; m = m->m_next)
5743 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
5744
5745 txq->raw_wrs++;
5746
5747 txsd = &txq->sdesc[eq->pidx];
5748 txsd->m = m0;
5749 txsd->desc_used = ndesc;
5750
5751 return (ndesc);
5752 }
5753
5754 /*
5755 * Write a txpkt WR for this packet to the hardware descriptors, update the
5756 * software descriptor, and advance the pidx. It is guaranteed that enough
5757 * descriptors are available.
5758 *
5759 * The return value is the # of hardware descriptors used.
5760 */
5761 static u_int
write_txpkt_wr(struct adapter * sc,struct sge_txq * txq,struct mbuf * m0,u_int available)5762 write_txpkt_wr(struct adapter *sc, struct sge_txq *txq, struct mbuf *m0,
5763 u_int available)
5764 {
5765 struct sge_eq *eq;
5766 struct fw_eth_tx_pkt_wr *wr;
5767 struct tx_sdesc *txsd;
5768 struct cpl_tx_pkt_core *cpl;
5769 uint32_t ctrl; /* used in many unrelated places */
5770 uint64_t ctrl1;
5771 int len16, ndesc, pktlen, nsegs;
5772 caddr_t dst;
5773
5774 TXQ_LOCK_ASSERT_OWNED(txq);
5775 M_ASSERTPKTHDR(m0);
5776
5777 len16 = mbuf_len16(m0);
5778 nsegs = mbuf_nsegs(m0);
5779 pktlen = m0->m_pkthdr.len;
5780 ctrl = sizeof(struct cpl_tx_pkt_core);
5781 if (needs_tso(m0)) {
5782 if (needs_vxlan_tso(m0))
5783 ctrl += sizeof(struct cpl_tx_tnl_lso);
5784 else
5785 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
5786 } else if (!(mbuf_cflags(m0) & MC_NOMAP) && pktlen <= imm_payload(2) &&
5787 available >= 2) {
5788 /* Immediate data. Recalculate len16 and set nsegs to 0. */
5789 ctrl += pktlen;
5790 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) +
5791 sizeof(struct cpl_tx_pkt_core) + pktlen, 16);
5792 nsegs = 0;
5793 }
5794 ndesc = tx_len16_to_desc(len16);
5795 MPASS(ndesc <= available);
5796
5797 /* Firmware work request header */
5798 eq = &txq->eq;
5799 wr = (void *)&eq->desc[eq->pidx];
5800 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
5801 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
5802
5803 ctrl = V_FW_WR_LEN16(len16);
5804 wr->equiq_to_len16 = htobe32(ctrl);
5805 wr->r3 = 0;
5806
5807 if (needs_tso(m0)) {
5808 if (needs_vxlan_tso(m0)) {
5809 cpl = write_tnl_lso_cpl(wr + 1, m0);
5810 txq->vxlan_tso_wrs++;
5811 } else {
5812 cpl = write_lso_cpl(wr + 1, m0);
5813 txq->tso_wrs++;
5814 }
5815 } else
5816 cpl = (void *)(wr + 1);
5817
5818 /* Checksum offload */
5819 ctrl1 = csum_to_ctrl(sc, m0);
5820 if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS)) {
5821 /* some hardware assistance provided */
5822 if (needs_vxlan_csum(m0))
5823 txq->vxlan_txcsum++;
5824 else
5825 txq->txcsum++;
5826 }
5827
5828 /* VLAN tag insertion */
5829 if (needs_vlan_insertion(m0)) {
5830 ctrl1 |= F_TXPKT_VLAN_VLD |
5831 V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
5832 txq->vlan_insertion++;
5833 }
5834
5835 /* CPL header */
5836 cpl->ctrl0 = txq->cpl_ctrl0;
5837 cpl->pack = 0;
5838 cpl->len = htobe16(pktlen);
5839 cpl->ctrl1 = htobe64(ctrl1);
5840
5841 /* SGL */
5842 dst = (void *)(cpl + 1);
5843 if (__predict_false((uintptr_t)dst == (uintptr_t)&eq->desc[eq->sidx]))
5844 dst = (caddr_t)&eq->desc[0];
5845 if (nsegs > 0) {
5846
5847 write_gl_to_txd(txq, m0, &dst, eq->sidx - ndesc < eq->pidx);
5848 txq->sgl_wrs++;
5849 } else {
5850 struct mbuf *m;
5851
5852 for (m = m0; m != NULL; m = m->m_next) {
5853 copy_to_txd(eq, mtod(m, caddr_t), &dst, m->m_len);
5854 #ifdef INVARIANTS
5855 pktlen -= m->m_len;
5856 #endif
5857 }
5858 #ifdef INVARIANTS
5859 KASSERT(pktlen == 0, ("%s: %d bytes left.", __func__, pktlen));
5860 #endif
5861 txq->imm_wrs++;
5862 }
5863
5864 txq->txpkt_wrs++;
5865
5866 txsd = &txq->sdesc[eq->pidx];
5867 txsd->m = m0;
5868 txsd->desc_used = ndesc;
5869
5870 return (ndesc);
5871 }
5872
5873 static inline bool
cmp_l2hdr(struct txpkts * txp,struct mbuf * m)5874 cmp_l2hdr(struct txpkts *txp, struct mbuf *m)
5875 {
5876 int len;
5877
5878 MPASS(txp->npkt > 0);
5879 MPASS(m->m_len >= VM_TX_L2HDR_LEN);
5880
5881 if (txp->ethtype == be16toh(ETHERTYPE_VLAN))
5882 len = VM_TX_L2HDR_LEN;
5883 else
5884 len = sizeof(struct ether_header);
5885
5886 return (memcmp(m->m_data, &txp->ethmacdst[0], len) != 0);
5887 }
5888
5889 static inline void
save_l2hdr(struct txpkts * txp,struct mbuf * m)5890 save_l2hdr(struct txpkts *txp, struct mbuf *m)
5891 {
5892 MPASS(m->m_len >= VM_TX_L2HDR_LEN);
5893
5894 memcpy(&txp->ethmacdst[0], mtod(m, const void *), VM_TX_L2HDR_LEN);
5895 }
5896
5897 static int
add_to_txpkts_vf(struct adapter * sc,struct sge_txq * txq,struct mbuf * m,int avail,bool * send)5898 add_to_txpkts_vf(struct adapter *sc, struct sge_txq *txq, struct mbuf *m,
5899 int avail, bool *send)
5900 {
5901 struct txpkts *txp = &txq->txp;
5902
5903 /* Cannot have TSO and coalesce at the same time. */
5904 if (cannot_use_txpkts(m)) {
5905 cannot_coalesce:
5906 *send = txp->npkt > 0;
5907 return (EINVAL);
5908 }
5909
5910 /* VF allows coalescing of type 1 (1 GL) only */
5911 if (mbuf_nsegs(m) > 1)
5912 goto cannot_coalesce;
5913
5914 *send = false;
5915 if (txp->npkt > 0) {
5916 MPASS(tx_len16_to_desc(txp->len16) <= avail);
5917 MPASS(txp->npkt < txp->max_npkt);
5918 MPASS(txp->wr_type == 1); /* VF supports type 1 only */
5919
5920 if (tx_len16_to_desc(txp->len16 + txpkts1_len16()) > avail) {
5921 retry_after_send:
5922 *send = true;
5923 return (EAGAIN);
5924 }
5925 if (m->m_pkthdr.len + txp->plen > 65535)
5926 goto retry_after_send;
5927 if (cmp_l2hdr(txp, m))
5928 goto retry_after_send;
5929
5930 txp->len16 += txpkts1_len16();
5931 txp->plen += m->m_pkthdr.len;
5932 txp->mb[txp->npkt++] = m;
5933 if (txp->npkt == txp->max_npkt)
5934 *send = true;
5935 } else {
5936 txp->len16 = howmany(sizeof(struct fw_eth_tx_pkts_vm_wr), 16) +
5937 txpkts1_len16();
5938 if (tx_len16_to_desc(txp->len16) > avail)
5939 goto cannot_coalesce;
5940 txp->npkt = 1;
5941 txp->wr_type = 1;
5942 txp->plen = m->m_pkthdr.len;
5943 txp->mb[0] = m;
5944 save_l2hdr(txp, m);
5945 }
5946 return (0);
5947 }
5948
5949 static int
add_to_txpkts_pf(struct adapter * sc,struct sge_txq * txq,struct mbuf * m,int avail,bool * send)5950 add_to_txpkts_pf(struct adapter *sc, struct sge_txq *txq, struct mbuf *m,
5951 int avail, bool *send)
5952 {
5953 struct txpkts *txp = &txq->txp;
5954 int nsegs;
5955
5956 MPASS(!(sc->flags & IS_VF));
5957
5958 /* Cannot have TSO and coalesce at the same time. */
5959 if (cannot_use_txpkts(m)) {
5960 cannot_coalesce:
5961 *send = txp->npkt > 0;
5962 return (EINVAL);
5963 }
5964
5965 *send = false;
5966 nsegs = mbuf_nsegs(m);
5967 if (txp->npkt == 0) {
5968 if (m->m_pkthdr.len > 65535)
5969 goto cannot_coalesce;
5970 if (nsegs > 1) {
5971 txp->wr_type = 0;
5972 txp->len16 =
5973 howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) +
5974 txpkts0_len16(nsegs);
5975 } else {
5976 txp->wr_type = 1;
5977 txp->len16 =
5978 howmany(sizeof(struct fw_eth_tx_pkts_wr), 16) +
5979 txpkts1_len16();
5980 }
5981 if (tx_len16_to_desc(txp->len16) > avail)
5982 goto cannot_coalesce;
5983 txp->npkt = 1;
5984 txp->plen = m->m_pkthdr.len;
5985 txp->mb[0] = m;
5986 } else {
5987 MPASS(tx_len16_to_desc(txp->len16) <= avail);
5988 MPASS(txp->npkt < txp->max_npkt);
5989
5990 if (m->m_pkthdr.len + txp->plen > 65535) {
5991 retry_after_send:
5992 *send = true;
5993 return (EAGAIN);
5994 }
5995
5996 MPASS(txp->wr_type == 0 || txp->wr_type == 1);
5997 if (txp->wr_type == 0) {
5998 if (tx_len16_to_desc(txp->len16 +
5999 txpkts0_len16(nsegs)) > min(avail, SGE_MAX_WR_NDESC))
6000 goto retry_after_send;
6001 txp->len16 += txpkts0_len16(nsegs);
6002 } else {
6003 if (nsegs != 1)
6004 goto retry_after_send;
6005 if (tx_len16_to_desc(txp->len16 + txpkts1_len16()) >
6006 avail)
6007 goto retry_after_send;
6008 txp->len16 += txpkts1_len16();
6009 }
6010
6011 txp->plen += m->m_pkthdr.len;
6012 txp->mb[txp->npkt++] = m;
6013 if (txp->npkt == txp->max_npkt)
6014 *send = true;
6015 }
6016 return (0);
6017 }
6018
6019 /*
6020 * Write a txpkts WR for the packets in txp to the hardware descriptors, update
6021 * the software descriptor, and advance the pidx. It is guaranteed that enough
6022 * descriptors are available.
6023 *
6024 * The return value is the # of hardware descriptors used.
6025 */
6026 static u_int
write_txpkts_wr(struct adapter * sc,struct sge_txq * txq)6027 write_txpkts_wr(struct adapter *sc, struct sge_txq *txq)
6028 {
6029 const struct txpkts *txp = &txq->txp;
6030 struct sge_eq *eq = &txq->eq;
6031 struct fw_eth_tx_pkts_wr *wr;
6032 struct tx_sdesc *txsd;
6033 struct cpl_tx_pkt_core *cpl;
6034 uint64_t ctrl1;
6035 int ndesc, i, checkwrap;
6036 struct mbuf *m, *last;
6037 void *flitp;
6038
6039 TXQ_LOCK_ASSERT_OWNED(txq);
6040 MPASS(txp->npkt > 0);
6041 MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
6042
6043 wr = (void *)&eq->desc[eq->pidx];
6044 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
6045 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(txp->len16));
6046 wr->plen = htobe16(txp->plen);
6047 wr->npkt = txp->npkt;
6048 wr->r3 = 0;
6049 wr->type = txp->wr_type;
6050 flitp = wr + 1;
6051
6052 /*
6053 * At this point we are 16B into a hardware descriptor. If checkwrap is
6054 * set then we know the WR is going to wrap around somewhere. We'll
6055 * check for that at appropriate points.
6056 */
6057 ndesc = tx_len16_to_desc(txp->len16);
6058 last = NULL;
6059 checkwrap = eq->sidx - ndesc < eq->pidx;
6060 for (i = 0; i < txp->npkt; i++) {
6061 m = txp->mb[i];
6062 if (txp->wr_type == 0) {
6063 struct ulp_txpkt *ulpmc;
6064 struct ulptx_idata *ulpsc;
6065
6066 /* ULP master command */
6067 ulpmc = flitp;
6068 ulpmc->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
6069 V_ULP_TXPKT_DEST(0) | V_ULP_TXPKT_FID(eq->iqid));
6070 ulpmc->len = htobe32(txpkts0_len16(mbuf_nsegs(m)));
6071
6072 /* ULP subcommand */
6073 ulpsc = (void *)(ulpmc + 1);
6074 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
6075 F_ULP_TX_SC_MORE);
6076 ulpsc->len = htobe32(sizeof(struct cpl_tx_pkt_core));
6077
6078 cpl = (void *)(ulpsc + 1);
6079 if (checkwrap &&
6080 (uintptr_t)cpl == (uintptr_t)&eq->desc[eq->sidx])
6081 cpl = (void *)&eq->desc[0];
6082 } else {
6083 cpl = flitp;
6084 }
6085
6086 /* Checksum offload */
6087 ctrl1 = csum_to_ctrl(sc, m);
6088 if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS)) {
6089 /* some hardware assistance provided */
6090 if (needs_vxlan_csum(m))
6091 txq->vxlan_txcsum++;
6092 else
6093 txq->txcsum++;
6094 }
6095
6096 /* VLAN tag insertion */
6097 if (needs_vlan_insertion(m)) {
6098 ctrl1 |= F_TXPKT_VLAN_VLD |
6099 V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
6100 txq->vlan_insertion++;
6101 }
6102
6103 /* CPL header */
6104 cpl->ctrl0 = txq->cpl_ctrl0;
6105 cpl->pack = 0;
6106 cpl->len = htobe16(m->m_pkthdr.len);
6107 cpl->ctrl1 = htobe64(ctrl1);
6108
6109 flitp = cpl + 1;
6110 if (checkwrap &&
6111 (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
6112 flitp = (void *)&eq->desc[0];
6113
6114 write_gl_to_txd(txq, m, (caddr_t *)(&flitp), checkwrap);
6115
6116 if (last != NULL)
6117 last->m_nextpkt = m;
6118 last = m;
6119 }
6120
6121 txq->sgl_wrs++;
6122 if (txp->wr_type == 0) {
6123 txq->txpkts0_pkts += txp->npkt;
6124 txq->txpkts0_wrs++;
6125 } else {
6126 txq->txpkts1_pkts += txp->npkt;
6127 txq->txpkts1_wrs++;
6128 }
6129
6130 txsd = &txq->sdesc[eq->pidx];
6131 txsd->m = txp->mb[0];
6132 txsd->desc_used = ndesc;
6133
6134 return (ndesc);
6135 }
6136
6137 static u_int
write_txpkts_vm_wr(struct adapter * sc,struct sge_txq * txq)6138 write_txpkts_vm_wr(struct adapter *sc, struct sge_txq *txq)
6139 {
6140 const struct txpkts *txp = &txq->txp;
6141 struct sge_eq *eq = &txq->eq;
6142 struct fw_eth_tx_pkts_vm_wr *wr;
6143 struct tx_sdesc *txsd;
6144 struct cpl_tx_pkt_core *cpl;
6145 uint64_t ctrl1;
6146 int ndesc, i;
6147 struct mbuf *m, *last;
6148 void *flitp;
6149
6150 TXQ_LOCK_ASSERT_OWNED(txq);
6151 MPASS(txp->npkt > 0);
6152 MPASS(txp->wr_type == 1); /* VF supports type 1 only */
6153 MPASS(txp->mb[0] != NULL);
6154 MPASS(txp->len16 <= howmany(SGE_MAX_WR_LEN, 16));
6155
6156 wr = (void *)&eq->desc[eq->pidx];
6157 wr->op_pkd = htobe32(V_FW_WR_OP(FW_ETH_TX_PKTS_VM_WR));
6158 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(txp->len16));
6159 wr->r3 = 0;
6160 wr->plen = htobe16(txp->plen);
6161 wr->npkt = txp->npkt;
6162 wr->r4 = 0;
6163 memcpy(&wr->ethmacdst[0], &txp->ethmacdst[0], 16);
6164 flitp = wr + 1;
6165
6166 /*
6167 * At this point we are 32B into a hardware descriptor. Each mbuf in
6168 * the WR will take 32B so we check for the end of the descriptor ring
6169 * before writing odd mbufs (mb[1], 3, 5, ..)
6170 */
6171 ndesc = tx_len16_to_desc(txp->len16);
6172 last = NULL;
6173 for (i = 0; i < txp->npkt; i++) {
6174 m = txp->mb[i];
6175 if (i & 1 && (uintptr_t)flitp == (uintptr_t)&eq->desc[eq->sidx])
6176 flitp = &eq->desc[0];
6177 cpl = flitp;
6178
6179 /* Checksum offload */
6180 ctrl1 = csum_to_ctrl(sc, m);
6181 if (ctrl1 != (F_TXPKT_IPCSUM_DIS | F_TXPKT_L4CSUM_DIS))
6182 txq->txcsum++; /* some hardware assistance provided */
6183
6184 /* VLAN tag insertion */
6185 if (needs_vlan_insertion(m)) {
6186 ctrl1 |= F_TXPKT_VLAN_VLD |
6187 V_TXPKT_VLAN(m->m_pkthdr.ether_vtag);
6188 txq->vlan_insertion++;
6189 } else if (sc->vlan_id)
6190 ctrl1 |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(sc->vlan_id);
6191
6192 /* CPL header */
6193 cpl->ctrl0 = txq->cpl_ctrl0;
6194 cpl->pack = 0;
6195 cpl->len = htobe16(m->m_pkthdr.len);
6196 cpl->ctrl1 = htobe64(ctrl1);
6197
6198 flitp = cpl + 1;
6199 MPASS(mbuf_nsegs(m) == 1);
6200 write_gl_to_txd(txq, m, (caddr_t *)(&flitp), 0);
6201
6202 if (last != NULL)
6203 last->m_nextpkt = m;
6204 last = m;
6205 }
6206
6207 txq->sgl_wrs++;
6208 txq->txpkts1_pkts += txp->npkt;
6209 txq->txpkts1_wrs++;
6210
6211 txsd = &txq->sdesc[eq->pidx];
6212 txsd->m = txp->mb[0];
6213 txsd->desc_used = ndesc;
6214
6215 return (ndesc);
6216 }
6217
6218 /*
6219 * If the SGL ends on an address that is not 16 byte aligned, this function will
6220 * add a 0 filled flit at the end.
6221 */
6222 static void
write_gl_to_txd(struct sge_txq * txq,struct mbuf * m,caddr_t * to,int checkwrap)6223 write_gl_to_txd(struct sge_txq *txq, struct mbuf *m, caddr_t *to, int checkwrap)
6224 {
6225 struct sge_eq *eq = &txq->eq;
6226 struct sglist *gl = txq->gl;
6227 struct sglist_seg *seg;
6228 __be64 *flitp, *wrap;
6229 struct ulptx_sgl *usgl;
6230 int i, nflits, nsegs;
6231
6232 KASSERT(((uintptr_t)(*to) & 0xf) == 0,
6233 ("%s: SGL must start at a 16 byte boundary: %p", __func__, *to));
6234 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
6235 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
6236
6237 get_pkt_gl(m, gl);
6238 nsegs = gl->sg_nseg;
6239 MPASS(nsegs > 0);
6240
6241 nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2;
6242 flitp = (__be64 *)(*to);
6243 wrap = (__be64 *)(&eq->desc[eq->sidx]);
6244 seg = &gl->sg_segs[0];
6245 usgl = (void *)flitp;
6246
6247 /*
6248 * We start at a 16 byte boundary somewhere inside the tx descriptor
6249 * ring, so we're at least 16 bytes away from the status page. There is
6250 * no chance of a wrap around in the middle of usgl (which is 16 bytes).
6251 */
6252
6253 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
6254 V_ULPTX_NSGE(nsegs));
6255 usgl->len0 = htobe32(seg->ss_len);
6256 usgl->addr0 = htobe64(seg->ss_paddr);
6257 seg++;
6258
6259 if (checkwrap == 0 || (uintptr_t)(flitp + nflits) <= (uintptr_t)wrap) {
6260
6261 /* Won't wrap around at all */
6262
6263 for (i = 0; i < nsegs - 1; i++, seg++) {
6264 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len);
6265 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr);
6266 }
6267 if (i & 1)
6268 usgl->sge[i / 2].len[1] = htobe32(0);
6269 flitp += nflits;
6270 } else {
6271
6272 /* Will wrap somewhere in the rest of the SGL */
6273
6274 /* 2 flits already written, write the rest flit by flit */
6275 flitp = (void *)(usgl + 1);
6276 for (i = 0; i < nflits - 2; i++) {
6277 if (flitp == wrap)
6278 flitp = (void *)eq->desc;
6279 *flitp++ = get_flit(seg, nsegs - 1, i);
6280 }
6281 }
6282
6283 if (nflits & 1) {
6284 MPASS(((uintptr_t)flitp) & 0xf);
6285 *flitp++ = 0;
6286 }
6287
6288 MPASS((((uintptr_t)flitp) & 0xf) == 0);
6289 if (__predict_false(flitp == wrap))
6290 *to = (void *)eq->desc;
6291 else
6292 *to = (void *)flitp;
6293 }
6294
6295 static inline void
copy_to_txd(struct sge_eq * eq,caddr_t from,caddr_t * to,int len)6296 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
6297 {
6298
6299 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
6300 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
6301
6302 if (__predict_true((uintptr_t)(*to) + len <=
6303 (uintptr_t)&eq->desc[eq->sidx])) {
6304 bcopy(from, *to, len);
6305 (*to) += len;
6306 } else {
6307 int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to);
6308
6309 bcopy(from, *to, portion);
6310 from += portion;
6311 portion = len - portion; /* remaining */
6312 bcopy(from, (void *)eq->desc, portion);
6313 (*to) = (caddr_t)eq->desc + portion;
6314 }
6315 }
6316
6317 static inline void
ring_eq_db(struct adapter * sc,struct sge_eq * eq,u_int n)6318 ring_eq_db(struct adapter *sc, struct sge_eq *eq, u_int n)
6319 {
6320 u_int db;
6321
6322 MPASS(n > 0);
6323
6324 db = eq->doorbells;
6325 if (n > 1)
6326 clrbit(&db, DOORBELL_WCWR);
6327 wmb();
6328
6329 switch (ffs(db) - 1) {
6330 case DOORBELL_UDB:
6331 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
6332 break;
6333
6334 case DOORBELL_WCWR: {
6335 volatile uint64_t *dst, *src;
6336 int i;
6337
6338 /*
6339 * Queues whose 128B doorbell segment fits in the page do not
6340 * use relative qid (udb_qid is always 0). Only queues with
6341 * doorbell segments can do WCWR.
6342 */
6343 KASSERT(eq->udb_qid == 0 && n == 1,
6344 ("%s: inappropriate doorbell (0x%x, %d, %d) for eq %p",
6345 __func__, eq->doorbells, n, eq->dbidx, eq));
6346
6347 dst = (volatile void *)((uintptr_t)eq->udb + UDBS_WR_OFFSET -
6348 UDBS_DB_OFFSET);
6349 i = eq->dbidx;
6350 src = (void *)&eq->desc[i];
6351 while (src != (void *)&eq->desc[i + 1])
6352 *dst++ = *src++;
6353 wmb();
6354 break;
6355 }
6356
6357 case DOORBELL_UDBWC:
6358 *eq->udb = htole32(V_QID(eq->udb_qid) | V_PIDX(n));
6359 wmb();
6360 break;
6361
6362 case DOORBELL_KDB:
6363 t4_write_reg(sc, sc->sge_kdoorbell_reg,
6364 V_QID(eq->cntxt_id) | V_PIDX(n));
6365 break;
6366 }
6367
6368 IDXINCR(eq->dbidx, n, eq->sidx);
6369 }
6370
6371 static inline u_int
reclaimable_tx_desc(struct sge_eq * eq)6372 reclaimable_tx_desc(struct sge_eq *eq)
6373 {
6374 uint16_t hw_cidx;
6375
6376 hw_cidx = read_hw_cidx(eq);
6377 return (IDXDIFF(hw_cidx, eq->cidx, eq->sidx));
6378 }
6379
6380 static inline u_int
total_available_tx_desc(struct sge_eq * eq)6381 total_available_tx_desc(struct sge_eq *eq)
6382 {
6383 uint16_t hw_cidx, pidx;
6384
6385 hw_cidx = read_hw_cidx(eq);
6386 pidx = eq->pidx;
6387
6388 if (pidx == hw_cidx)
6389 return (eq->sidx - 1);
6390 else
6391 return (IDXDIFF(hw_cidx, pidx, eq->sidx) - 1);
6392 }
6393
6394 static inline uint16_t
read_hw_cidx(struct sge_eq * eq)6395 read_hw_cidx(struct sge_eq *eq)
6396 {
6397 struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
6398 uint16_t cidx = spg->cidx; /* stable snapshot */
6399
6400 return (be16toh(cidx));
6401 }
6402
6403 /*
6404 * Reclaim 'n' descriptors approximately.
6405 */
6406 static u_int
reclaim_tx_descs(struct sge_txq * txq,u_int n)6407 reclaim_tx_descs(struct sge_txq *txq, u_int n)
6408 {
6409 struct tx_sdesc *txsd;
6410 struct sge_eq *eq = &txq->eq;
6411 u_int can_reclaim, reclaimed;
6412
6413 TXQ_LOCK_ASSERT_OWNED(txq);
6414 MPASS(n > 0);
6415
6416 reclaimed = 0;
6417 can_reclaim = reclaimable_tx_desc(eq);
6418 while (can_reclaim && reclaimed < n) {
6419 int ndesc;
6420 struct mbuf *m, *nextpkt;
6421
6422 txsd = &txq->sdesc[eq->cidx];
6423 ndesc = txsd->desc_used;
6424
6425 /* Firmware doesn't return "partial" credits. */
6426 KASSERT(can_reclaim >= ndesc,
6427 ("%s: unexpected number of credits: %d, %d",
6428 __func__, can_reclaim, ndesc));
6429 KASSERT(ndesc != 0,
6430 ("%s: descriptor with no credits: cidx %d",
6431 __func__, eq->cidx));
6432
6433 for (m = txsd->m; m != NULL; m = nextpkt) {
6434 nextpkt = m->m_nextpkt;
6435 m->m_nextpkt = NULL;
6436 m_freem(m);
6437 }
6438 reclaimed += ndesc;
6439 can_reclaim -= ndesc;
6440 IDXINCR(eq->cidx, ndesc, eq->sidx);
6441 }
6442
6443 return (reclaimed);
6444 }
6445
6446 static void
tx_reclaim(void * arg,int n)6447 tx_reclaim(void *arg, int n)
6448 {
6449 struct sge_txq *txq = arg;
6450 struct sge_eq *eq = &txq->eq;
6451
6452 do {
6453 if (TXQ_TRYLOCK(txq) == 0)
6454 break;
6455 n = reclaim_tx_descs(txq, 32);
6456 if (eq->cidx == eq->pidx)
6457 eq->equeqidx = eq->pidx;
6458 TXQ_UNLOCK(txq);
6459 } while (n > 0);
6460 }
6461
6462 static __be64
get_flit(struct sglist_seg * segs,int nsegs,int idx)6463 get_flit(struct sglist_seg *segs, int nsegs, int idx)
6464 {
6465 int i = (idx / 3) * 2;
6466
6467 switch (idx % 3) {
6468 case 0: {
6469 uint64_t rc;
6470
6471 rc = (uint64_t)segs[i].ss_len << 32;
6472 if (i + 1 < nsegs)
6473 rc |= (uint64_t)(segs[i + 1].ss_len);
6474
6475 return (htobe64(rc));
6476 }
6477 case 1:
6478 return (htobe64(segs[i].ss_paddr));
6479 case 2:
6480 return (htobe64(segs[i + 1].ss_paddr));
6481 }
6482
6483 return (0);
6484 }
6485
6486 static int
find_refill_source(struct adapter * sc,int maxp,bool packing)6487 find_refill_source(struct adapter *sc, int maxp, bool packing)
6488 {
6489 int i, zidx = -1;
6490 struct rx_buf_info *rxb = &sc->sge.rx_buf_info[0];
6491
6492 if (packing) {
6493 for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
6494 if (rxb->hwidx2 == -1)
6495 continue;
6496 if (rxb->size1 < PAGE_SIZE &&
6497 rxb->size1 < largest_rx_cluster)
6498 continue;
6499 if (rxb->size1 > largest_rx_cluster)
6500 break;
6501 MPASS(rxb->size1 - rxb->size2 >= CL_METADATA_SIZE);
6502 if (rxb->size2 >= maxp)
6503 return (i);
6504 zidx = i;
6505 }
6506 } else {
6507 for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
6508 if (rxb->hwidx1 == -1)
6509 continue;
6510 if (rxb->size1 > largest_rx_cluster)
6511 break;
6512 if (rxb->size1 >= maxp)
6513 return (i);
6514 zidx = i;
6515 }
6516 }
6517
6518 return (zidx);
6519 }
6520
6521 static void
add_fl_to_sfl(struct adapter * sc,struct sge_fl * fl)6522 add_fl_to_sfl(struct adapter *sc, struct sge_fl *fl)
6523 {
6524 mtx_lock(&sc->sfl_lock);
6525 FL_LOCK(fl);
6526 if ((fl->flags & FL_DOOMED) == 0) {
6527 fl->flags |= FL_STARVING;
6528 TAILQ_INSERT_TAIL(&sc->sfl, fl, link);
6529 callout_reset(&sc->sfl_callout, hz / 5, refill_sfl, sc);
6530 }
6531 FL_UNLOCK(fl);
6532 mtx_unlock(&sc->sfl_lock);
6533 }
6534
6535 static void
handle_wrq_egr_update(struct adapter * sc,struct sge_eq * eq)6536 handle_wrq_egr_update(struct adapter *sc, struct sge_eq *eq)
6537 {
6538 struct sge_wrq *wrq = (void *)eq;
6539
6540 atomic_readandclear_int(&eq->equiq);
6541 taskqueue_enqueue(sc->tq[eq->port_id], &wrq->wrq_tx_task);
6542 }
6543
6544 static void
handle_eth_egr_update(struct adapter * sc,struct sge_eq * eq)6545 handle_eth_egr_update(struct adapter *sc, struct sge_eq *eq)
6546 {
6547 struct sge_txq *txq = (void *)eq;
6548
6549 MPASS(eq->type == EQ_ETH);
6550
6551 atomic_readandclear_int(&eq->equiq);
6552 if (mp_ring_is_idle(txq->r))
6553 taskqueue_enqueue(sc->tq[eq->port_id], &txq->tx_reclaim_task);
6554 else
6555 mp_ring_check_drainage(txq->r, 64);
6556 }
6557
6558 static int
handle_sge_egr_update(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)6559 handle_sge_egr_update(struct sge_iq *iq, const struct rss_header *rss,
6560 struct mbuf *m)
6561 {
6562 const struct cpl_sge_egr_update *cpl = (const void *)(rss + 1);
6563 unsigned int qid = G_EGR_QID(ntohl(cpl->opcode_qid));
6564 struct adapter *sc = iq->adapter;
6565 struct sge *s = &sc->sge;
6566 struct sge_eq *eq;
6567 static void (*h[])(struct adapter *, struct sge_eq *) = {NULL,
6568 &handle_wrq_egr_update, &handle_eth_egr_update,
6569 &handle_wrq_egr_update};
6570
6571 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
6572 rss->opcode));
6573
6574 eq = s->eqmap[qid - s->eq_start - s->eq_base];
6575 (*h[eq->type])(sc, eq);
6576
6577 return (0);
6578 }
6579
6580 /* handle_fw_msg works for both fw4_msg and fw6_msg because this is valid */
6581 CTASSERT(offsetof(struct cpl_fw4_msg, data) == \
6582 offsetof(struct cpl_fw6_msg, data));
6583
6584 static int
handle_fw_msg(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)6585 handle_fw_msg(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
6586 {
6587 struct adapter *sc = iq->adapter;
6588 const struct cpl_fw6_msg *cpl = (const void *)(rss + 1);
6589
6590 KASSERT(m == NULL, ("%s: payload with opcode %02x", __func__,
6591 rss->opcode));
6592
6593 if (cpl->type == FW_TYPE_RSSCPL || cpl->type == FW6_TYPE_RSSCPL) {
6594 const struct rss_header *rss2;
6595
6596 rss2 = (const struct rss_header *)&cpl->data[0];
6597 return (t4_cpl_handler[rss2->opcode](iq, rss2, m));
6598 }
6599
6600 return (t4_fw_msg_handler[cpl->type](sc, &cpl->data[0]));
6601 }
6602
6603 /**
6604 * t4_handle_wrerr_rpl - process a FW work request error message
6605 * @adap: the adapter
6606 * @rpl: start of the FW message
6607 */
6608 static int
t4_handle_wrerr_rpl(struct adapter * adap,const __be64 * rpl)6609 t4_handle_wrerr_rpl(struct adapter *adap, const __be64 *rpl)
6610 {
6611 u8 opcode = *(const u8 *)rpl;
6612 const struct fw_error_cmd *e = (const void *)rpl;
6613 unsigned int i;
6614
6615 if (opcode != FW_ERROR_CMD) {
6616 log(LOG_ERR,
6617 "%s: Received WRERR_RPL message with opcode %#x\n",
6618 device_get_nameunit(adap->dev), opcode);
6619 return (EINVAL);
6620 }
6621 log(LOG_ERR, "%s: FW_ERROR (%s) ", device_get_nameunit(adap->dev),
6622 G_FW_ERROR_CMD_FATAL(be32toh(e->op_to_type)) ? "fatal" :
6623 "non-fatal");
6624 switch (G_FW_ERROR_CMD_TYPE(be32toh(e->op_to_type))) {
6625 case FW_ERROR_TYPE_EXCEPTION:
6626 log(LOG_ERR, "exception info:\n");
6627 for (i = 0; i < nitems(e->u.exception.info); i++)
6628 log(LOG_ERR, "%s%08x", i == 0 ? "\t" : " ",
6629 be32toh(e->u.exception.info[i]));
6630 log(LOG_ERR, "\n");
6631 break;
6632 case FW_ERROR_TYPE_HWMODULE:
6633 log(LOG_ERR, "HW module regaddr %08x regval %08x\n",
6634 be32toh(e->u.hwmodule.regaddr),
6635 be32toh(e->u.hwmodule.regval));
6636 break;
6637 case FW_ERROR_TYPE_WR:
6638 log(LOG_ERR, "WR cidx %d PF %d VF %d eqid %d hdr:\n",
6639 be16toh(e->u.wr.cidx),
6640 G_FW_ERROR_CMD_PFN(be16toh(e->u.wr.pfn_vfn)),
6641 G_FW_ERROR_CMD_VFN(be16toh(e->u.wr.pfn_vfn)),
6642 be32toh(e->u.wr.eqid));
6643 for (i = 0; i < nitems(e->u.wr.wrhdr); i++)
6644 log(LOG_ERR, "%s%02x", i == 0 ? "\t" : " ",
6645 e->u.wr.wrhdr[i]);
6646 log(LOG_ERR, "\n");
6647 break;
6648 case FW_ERROR_TYPE_ACL:
6649 log(LOG_ERR, "ACL cidx %d PF %d VF %d eqid %d %s",
6650 be16toh(e->u.acl.cidx),
6651 G_FW_ERROR_CMD_PFN(be16toh(e->u.acl.pfn_vfn)),
6652 G_FW_ERROR_CMD_VFN(be16toh(e->u.acl.pfn_vfn)),
6653 be32toh(e->u.acl.eqid),
6654 G_FW_ERROR_CMD_MV(be16toh(e->u.acl.mv_pkd)) ? "vlanid" :
6655 "MAC");
6656 for (i = 0; i < nitems(e->u.acl.val); i++)
6657 log(LOG_ERR, " %02x", e->u.acl.val[i]);
6658 log(LOG_ERR, "\n");
6659 break;
6660 default:
6661 log(LOG_ERR, "type %#x\n",
6662 G_FW_ERROR_CMD_TYPE(be32toh(e->op_to_type)));
6663 return (EINVAL);
6664 }
6665 return (0);
6666 }
6667
6668 static inline bool
bufidx_used(struct adapter * sc,int idx)6669 bufidx_used(struct adapter *sc, int idx)
6670 {
6671 struct rx_buf_info *rxb = &sc->sge.rx_buf_info[0];
6672 int i;
6673
6674 for (i = 0; i < SW_ZONE_SIZES; i++, rxb++) {
6675 if (rxb->size1 > largest_rx_cluster)
6676 continue;
6677 if (rxb->hwidx1 == idx || rxb->hwidx2 == idx)
6678 return (true);
6679 }
6680
6681 return (false);
6682 }
6683
6684 static int
sysctl_bufsizes(SYSCTL_HANDLER_ARGS)6685 sysctl_bufsizes(SYSCTL_HANDLER_ARGS)
6686 {
6687 struct adapter *sc = arg1;
6688 struct sge_params *sp = &sc->params.sge;
6689 int i, rc;
6690 struct sbuf sb;
6691 char c;
6692
6693 sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND);
6694 for (i = 0; i < SGE_FLBUF_SIZES; i++) {
6695 if (bufidx_used(sc, i))
6696 c = '*';
6697 else
6698 c = '\0';
6699
6700 sbuf_printf(&sb, "%u%c ", sp->sge_fl_buffer_size[i], c);
6701 }
6702 sbuf_trim(&sb);
6703 sbuf_finish(&sb);
6704 rc = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
6705 sbuf_delete(&sb);
6706 return (rc);
6707 }
6708
6709 #ifdef RATELIMIT
6710 #if defined(INET) || defined(INET6)
6711 /*
6712 * len16 for a txpkt WR with a GL. Includes the firmware work request header.
6713 */
6714 static inline u_int
txpkt_eo_len16(u_int nsegs,u_int immhdrs,u_int tso)6715 txpkt_eo_len16(u_int nsegs, u_int immhdrs, u_int tso)
6716 {
6717 u_int n;
6718
6719 MPASS(immhdrs > 0);
6720
6721 n = roundup2(sizeof(struct fw_eth_tx_eo_wr) +
6722 sizeof(struct cpl_tx_pkt_core) + immhdrs, 16);
6723 if (__predict_false(nsegs == 0))
6724 goto done;
6725
6726 nsegs--; /* first segment is part of ulptx_sgl */
6727 n += sizeof(struct ulptx_sgl) + 8 * ((3 * nsegs) / 2 + (nsegs & 1));
6728 if (tso)
6729 n += sizeof(struct cpl_tx_pkt_lso_core);
6730
6731 done:
6732 return (howmany(n, 16));
6733 }
6734 #endif
6735
6736 #define ETID_FLOWC_NPARAMS 6
6737 #define ETID_FLOWC_LEN (roundup2((sizeof(struct fw_flowc_wr) + \
6738 ETID_FLOWC_NPARAMS * sizeof(struct fw_flowc_mnemval)), 16))
6739 #define ETID_FLOWC_LEN16 (howmany(ETID_FLOWC_LEN, 16))
6740
6741 #if defined(INET) || defined(INET6)
6742 static int
send_etid_flowc_wr(struct cxgbe_rate_tag * cst,struct port_info * pi,struct vi_info * vi)6743 send_etid_flowc_wr(struct cxgbe_rate_tag *cst, struct port_info *pi,
6744 struct vi_info *vi)
6745 {
6746 struct wrq_cookie cookie;
6747 u_int pfvf = pi->adapter->pf << S_FW_VIID_PFN;
6748 struct fw_flowc_wr *flowc;
6749
6750 mtx_assert(&cst->lock, MA_OWNED);
6751 MPASS((cst->flags & (EO_FLOWC_PENDING | EO_FLOWC_RPL_PENDING)) ==
6752 EO_FLOWC_PENDING);
6753
6754 flowc = start_wrq_wr(&cst->eo_txq->wrq, ETID_FLOWC_LEN16, &cookie);
6755 if (__predict_false(flowc == NULL))
6756 return (ENOMEM);
6757
6758 bzero(flowc, ETID_FLOWC_LEN);
6759 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
6760 V_FW_FLOWC_WR_NPARAMS(ETID_FLOWC_NPARAMS) | V_FW_WR_COMPL(0));
6761 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(ETID_FLOWC_LEN16) |
6762 V_FW_WR_FLOWID(cst->etid));
6763 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
6764 flowc->mnemval[0].val = htobe32(pfvf);
6765 /* Firmware expects hw port and will translate to channel itself. */
6766 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
6767 flowc->mnemval[1].val = htobe32(pi->hw_port);
6768 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
6769 flowc->mnemval[2].val = htobe32(pi->hw_port);
6770 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
6771 flowc->mnemval[3].val = htobe32(cst->iqid);
6772 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_EOSTATE;
6773 flowc->mnemval[4].val = htobe32(FW_FLOWC_MNEM_EOSTATE_ESTABLISHED);
6774 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
6775 flowc->mnemval[5].val = htobe32(cst->schedcl);
6776
6777 commit_wrq_wr(&cst->eo_txq->wrq, flowc, &cookie);
6778
6779 cst->flags &= ~EO_FLOWC_PENDING;
6780 cst->flags |= EO_FLOWC_RPL_PENDING;
6781 MPASS(cst->tx_credits >= ETID_FLOWC_LEN16); /* flowc is first WR. */
6782 cst->tx_credits -= ETID_FLOWC_LEN16;
6783
6784 return (0);
6785 }
6786 #endif
6787
6788 #define ETID_FLUSH_LEN16 (howmany(sizeof (struct fw_flowc_wr), 16))
6789
6790 void
send_etid_flush_wr(struct cxgbe_rate_tag * cst)6791 send_etid_flush_wr(struct cxgbe_rate_tag *cst)
6792 {
6793 struct fw_flowc_wr *flowc;
6794 struct wrq_cookie cookie;
6795
6796 mtx_assert(&cst->lock, MA_OWNED);
6797
6798 flowc = start_wrq_wr(&cst->eo_txq->wrq, ETID_FLUSH_LEN16, &cookie);
6799 if (__predict_false(flowc == NULL))
6800 CXGBE_UNIMPLEMENTED(__func__);
6801
6802 bzero(flowc, ETID_FLUSH_LEN16 * 16);
6803 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
6804 V_FW_FLOWC_WR_NPARAMS(0) | F_FW_WR_COMPL);
6805 flowc->flowid_len16 = htobe32(V_FW_WR_LEN16(ETID_FLUSH_LEN16) |
6806 V_FW_WR_FLOWID(cst->etid));
6807
6808 commit_wrq_wr(&cst->eo_txq->wrq, flowc, &cookie);
6809
6810 cst->flags |= EO_FLUSH_RPL_PENDING;
6811 MPASS(cst->tx_credits >= ETID_FLUSH_LEN16);
6812 cst->tx_credits -= ETID_FLUSH_LEN16;
6813 cst->ncompl++;
6814 }
6815
6816 static void
write_ethofld_wr(struct cxgbe_rate_tag * cst,struct fw_eth_tx_eo_wr * wr,struct mbuf * m0,int compl)6817 write_ethofld_wr(struct cxgbe_rate_tag *cst, struct fw_eth_tx_eo_wr *wr,
6818 struct mbuf *m0, int compl)
6819 {
6820 struct cpl_tx_pkt_core *cpl;
6821 uint64_t ctrl1;
6822 uint32_t ctrl; /* used in many unrelated places */
6823 int len16, pktlen, nsegs, immhdrs;
6824 uintptr_t p;
6825 struct ulptx_sgl *usgl;
6826 struct sglist sg;
6827 struct sglist_seg segs[38]; /* XXX: find real limit. XXX: get off the stack */
6828
6829 mtx_assert(&cst->lock, MA_OWNED);
6830 M_ASSERTPKTHDR(m0);
6831 KASSERT(m0->m_pkthdr.l2hlen > 0 && m0->m_pkthdr.l3hlen > 0 &&
6832 m0->m_pkthdr.l4hlen > 0,
6833 ("%s: ethofld mbuf %p is missing header lengths", __func__, m0));
6834
6835 len16 = mbuf_eo_len16(m0);
6836 nsegs = mbuf_eo_nsegs(m0);
6837 pktlen = m0->m_pkthdr.len;
6838 ctrl = sizeof(struct cpl_tx_pkt_core);
6839 if (needs_tso(m0))
6840 ctrl += sizeof(struct cpl_tx_pkt_lso_core);
6841 immhdrs = m0->m_pkthdr.l2hlen + m0->m_pkthdr.l3hlen + m0->m_pkthdr.l4hlen;
6842 ctrl += immhdrs;
6843
6844 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_EO_WR) |
6845 V_FW_ETH_TX_EO_WR_IMMDLEN(ctrl) | V_FW_WR_COMPL(!!compl));
6846 wr->equiq_to_len16 = htobe32(V_FW_WR_LEN16(len16) |
6847 V_FW_WR_FLOWID(cst->etid));
6848 wr->r3 = 0;
6849 if (needs_outer_udp_csum(m0)) {
6850 wr->u.udpseg.type = FW_ETH_TX_EO_TYPE_UDPSEG;
6851 wr->u.udpseg.ethlen = m0->m_pkthdr.l2hlen;
6852 wr->u.udpseg.iplen = htobe16(m0->m_pkthdr.l3hlen);
6853 wr->u.udpseg.udplen = m0->m_pkthdr.l4hlen;
6854 wr->u.udpseg.rtplen = 0;
6855 wr->u.udpseg.r4 = 0;
6856 wr->u.udpseg.mss = htobe16(pktlen - immhdrs);
6857 wr->u.udpseg.schedpktsize = wr->u.udpseg.mss;
6858 wr->u.udpseg.plen = htobe32(pktlen - immhdrs);
6859 cpl = (void *)(wr + 1);
6860 } else {
6861 MPASS(needs_outer_tcp_csum(m0));
6862 wr->u.tcpseg.type = FW_ETH_TX_EO_TYPE_TCPSEG;
6863 wr->u.tcpseg.ethlen = m0->m_pkthdr.l2hlen;
6864 wr->u.tcpseg.iplen = htobe16(m0->m_pkthdr.l3hlen);
6865 wr->u.tcpseg.tcplen = m0->m_pkthdr.l4hlen;
6866 wr->u.tcpseg.tsclk_tsoff = mbuf_eo_tsclk_tsoff(m0);
6867 wr->u.tcpseg.r4 = 0;
6868 wr->u.tcpseg.r5 = 0;
6869 wr->u.tcpseg.plen = htobe32(pktlen - immhdrs);
6870
6871 if (needs_tso(m0)) {
6872 struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
6873
6874 wr->u.tcpseg.mss = htobe16(m0->m_pkthdr.tso_segsz);
6875
6876 ctrl = V_LSO_OPCODE(CPL_TX_PKT_LSO) |
6877 F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
6878 V_LSO_ETHHDR_LEN((m0->m_pkthdr.l2hlen -
6879 ETHER_HDR_LEN) >> 2) |
6880 V_LSO_IPHDR_LEN(m0->m_pkthdr.l3hlen >> 2) |
6881 V_LSO_TCPHDR_LEN(m0->m_pkthdr.l4hlen >> 2);
6882 if (m0->m_pkthdr.l3hlen == sizeof(struct ip6_hdr))
6883 ctrl |= F_LSO_IPV6;
6884 lso->lso_ctrl = htobe32(ctrl);
6885 lso->ipid_ofst = htobe16(0);
6886 lso->mss = htobe16(m0->m_pkthdr.tso_segsz);
6887 lso->seqno_offset = htobe32(0);
6888 lso->len = htobe32(pktlen);
6889
6890 cpl = (void *)(lso + 1);
6891 } else {
6892 wr->u.tcpseg.mss = htobe16(0xffff);
6893 cpl = (void *)(wr + 1);
6894 }
6895 }
6896
6897 /* Checksum offload must be requested for ethofld. */
6898 MPASS(needs_outer_l4_csum(m0));
6899 ctrl1 = csum_to_ctrl(cst->adapter, m0);
6900
6901 /* VLAN tag insertion */
6902 if (needs_vlan_insertion(m0)) {
6903 ctrl1 |= F_TXPKT_VLAN_VLD |
6904 V_TXPKT_VLAN(m0->m_pkthdr.ether_vtag);
6905 }
6906
6907 /* CPL header */
6908 cpl->ctrl0 = cst->ctrl0;
6909 cpl->pack = 0;
6910 cpl->len = htobe16(pktlen);
6911 cpl->ctrl1 = htobe64(ctrl1);
6912
6913 /* Copy Ethernet, IP & TCP/UDP hdrs as immediate data */
6914 p = (uintptr_t)(cpl + 1);
6915 m_copydata(m0, 0, immhdrs, (void *)p);
6916
6917 /* SGL */
6918 if (nsegs > 0) {
6919 int i, pad;
6920
6921 /* zero-pad upto next 16Byte boundary, if not 16Byte aligned */
6922 p += immhdrs;
6923 pad = 16 - (immhdrs & 0xf);
6924 bzero((void *)p, pad);
6925
6926 usgl = (void *)(p + pad);
6927 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
6928 V_ULPTX_NSGE(nsegs));
6929
6930 sglist_init(&sg, nitems(segs), segs);
6931 for (; m0 != NULL; m0 = m0->m_next) {
6932 if (__predict_false(m0->m_len == 0))
6933 continue;
6934 if (immhdrs >= m0->m_len) {
6935 immhdrs -= m0->m_len;
6936 continue;
6937 }
6938 if (m0->m_flags & M_EXTPG)
6939 sglist_append_mbuf_epg(&sg, m0,
6940 mtod(m0, vm_offset_t), m0->m_len);
6941 else
6942 sglist_append(&sg, mtod(m0, char *) + immhdrs,
6943 m0->m_len - immhdrs);
6944 immhdrs = 0;
6945 }
6946 MPASS(sg.sg_nseg == nsegs);
6947
6948 /*
6949 * Zero pad last 8B in case the WR doesn't end on a 16B
6950 * boundary.
6951 */
6952 *(uint64_t *)((char *)wr + len16 * 16 - 8) = 0;
6953
6954 usgl->len0 = htobe32(segs[0].ss_len);
6955 usgl->addr0 = htobe64(segs[0].ss_paddr);
6956 for (i = 0; i < nsegs - 1; i++) {
6957 usgl->sge[i / 2].len[i & 1] = htobe32(segs[i + 1].ss_len);
6958 usgl->sge[i / 2].addr[i & 1] = htobe64(segs[i + 1].ss_paddr);
6959 }
6960 if (i & 1)
6961 usgl->sge[i / 2].len[1] = htobe32(0);
6962 }
6963
6964 }
6965
6966 static void
ethofld_tx(struct cxgbe_rate_tag * cst)6967 ethofld_tx(struct cxgbe_rate_tag *cst)
6968 {
6969 struct mbuf *m;
6970 struct wrq_cookie cookie;
6971 int next_credits, compl;
6972 struct fw_eth_tx_eo_wr *wr;
6973
6974 mtx_assert(&cst->lock, MA_OWNED);
6975
6976 while ((m = mbufq_first(&cst->pending_tx)) != NULL) {
6977 M_ASSERTPKTHDR(m);
6978
6979 /* How many len16 credits do we need to send this mbuf. */
6980 next_credits = mbuf_eo_len16(m);
6981 MPASS(next_credits > 0);
6982 if (next_credits > cst->tx_credits) {
6983 /*
6984 * Tx will make progress eventually because there is at
6985 * least one outstanding fw4_ack that will return
6986 * credits and kick the tx.
6987 */
6988 MPASS(cst->ncompl > 0);
6989 return;
6990 }
6991 wr = start_wrq_wr(&cst->eo_txq->wrq, next_credits, &cookie);
6992 if (__predict_false(wr == NULL)) {
6993 /* XXX: wishful thinking, not a real assertion. */
6994 MPASS(cst->ncompl > 0);
6995 return;
6996 }
6997 cst->tx_credits -= next_credits;
6998 cst->tx_nocompl += next_credits;
6999 compl = cst->ncompl == 0 || cst->tx_nocompl >= cst->tx_total / 2;
7000 ETHER_BPF_MTAP(cst->com.ifp, m);
7001 write_ethofld_wr(cst, wr, m, compl);
7002 commit_wrq_wr(&cst->eo_txq->wrq, wr, &cookie);
7003 if (compl) {
7004 cst->ncompl++;
7005 cst->tx_nocompl = 0;
7006 }
7007 (void) mbufq_dequeue(&cst->pending_tx);
7008
7009 /*
7010 * Drop the mbuf's reference on the tag now rather
7011 * than waiting until m_freem(). This ensures that
7012 * cxgbe_rate_tag_free gets called when the inp drops
7013 * its reference on the tag and there are no more
7014 * mbufs in the pending_tx queue and can flush any
7015 * pending requests. Otherwise if the last mbuf
7016 * doesn't request a completion the etid will never be
7017 * released.
7018 */
7019 m->m_pkthdr.snd_tag = NULL;
7020 m->m_pkthdr.csum_flags &= ~CSUM_SND_TAG;
7021 m_snd_tag_rele(&cst->com);
7022
7023 mbufq_enqueue(&cst->pending_fwack, m);
7024 }
7025 }
7026
7027 #if defined(INET) || defined(INET6)
7028 static int
ethofld_transmit(if_t ifp,struct mbuf * m0)7029 ethofld_transmit(if_t ifp, struct mbuf *m0)
7030 {
7031 struct cxgbe_rate_tag *cst;
7032 int rc;
7033
7034 MPASS(m0->m_nextpkt == NULL);
7035 MPASS(m0->m_pkthdr.csum_flags & CSUM_SND_TAG);
7036 MPASS(m0->m_pkthdr.snd_tag != NULL);
7037 cst = mst_to_crt(m0->m_pkthdr.snd_tag);
7038
7039 mtx_lock(&cst->lock);
7040 MPASS(cst->flags & EO_SND_TAG_REF);
7041
7042 if (__predict_false(cst->flags & EO_FLOWC_PENDING)) {
7043 struct vi_info *vi = if_getsoftc(ifp);
7044 struct port_info *pi = vi->pi;
7045 struct adapter *sc = pi->adapter;
7046 const uint32_t rss_mask = vi->rss_size - 1;
7047 uint32_t rss_hash;
7048
7049 cst->eo_txq = &sc->sge.ofld_txq[vi->first_ofld_txq];
7050 if (M_HASHTYPE_ISHASH(m0))
7051 rss_hash = m0->m_pkthdr.flowid;
7052 else
7053 rss_hash = arc4random();
7054 /* We assume RSS hashing */
7055 cst->iqid = vi->rss[rss_hash & rss_mask];
7056 cst->eo_txq += rss_hash % vi->nofldtxq;
7057 rc = send_etid_flowc_wr(cst, pi, vi);
7058 if (rc != 0)
7059 goto done;
7060 }
7061
7062 if (__predict_false(cst->plen + m0->m_pkthdr.len > eo_max_backlog)) {
7063 rc = ENOBUFS;
7064 goto done;
7065 }
7066
7067 mbufq_enqueue(&cst->pending_tx, m0);
7068 cst->plen += m0->m_pkthdr.len;
7069
7070 /*
7071 * Hold an extra reference on the tag while generating work
7072 * requests to ensure that we don't try to free the tag during
7073 * ethofld_tx() in case we are sending the final mbuf after
7074 * the inp was freed.
7075 */
7076 m_snd_tag_ref(&cst->com);
7077 ethofld_tx(cst);
7078 mtx_unlock(&cst->lock);
7079 m_snd_tag_rele(&cst->com);
7080 return (0);
7081
7082 done:
7083 mtx_unlock(&cst->lock);
7084 return (rc);
7085 }
7086 #endif
7087
7088 static int
ethofld_fw4_ack(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m0)7089 ethofld_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m0)
7090 {
7091 struct adapter *sc = iq->adapter;
7092 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
7093 struct mbuf *m;
7094 u_int etid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
7095 struct cxgbe_rate_tag *cst;
7096 uint8_t credits = cpl->credits;
7097
7098 cst = lookup_etid(sc, etid);
7099 mtx_lock(&cst->lock);
7100 if (__predict_false(cst->flags & EO_FLOWC_RPL_PENDING)) {
7101 MPASS(credits >= ETID_FLOWC_LEN16);
7102 credits -= ETID_FLOWC_LEN16;
7103 cst->flags &= ~EO_FLOWC_RPL_PENDING;
7104 }
7105
7106 KASSERT(cst->ncompl > 0,
7107 ("%s: etid %u (%p) wasn't expecting completion.",
7108 __func__, etid, cst));
7109 cst->ncompl--;
7110
7111 while (credits > 0) {
7112 m = mbufq_dequeue(&cst->pending_fwack);
7113 if (__predict_false(m == NULL)) {
7114 /*
7115 * The remaining credits are for the final flush that
7116 * was issued when the tag was freed by the kernel.
7117 */
7118 MPASS((cst->flags &
7119 (EO_FLUSH_RPL_PENDING | EO_SND_TAG_REF)) ==
7120 EO_FLUSH_RPL_PENDING);
7121 MPASS(credits == ETID_FLUSH_LEN16);
7122 MPASS(cst->tx_credits + cpl->credits == cst->tx_total);
7123 MPASS(cst->ncompl == 0);
7124
7125 cst->flags &= ~EO_FLUSH_RPL_PENDING;
7126 cst->tx_credits += cpl->credits;
7127 cxgbe_rate_tag_free_locked(cst);
7128 return (0); /* cst is gone. */
7129 }
7130 KASSERT(m != NULL,
7131 ("%s: too many credits (%u, %u)", __func__, cpl->credits,
7132 credits));
7133 KASSERT(credits >= mbuf_eo_len16(m),
7134 ("%s: too few credits (%u, %u, %u)", __func__,
7135 cpl->credits, credits, mbuf_eo_len16(m)));
7136 credits -= mbuf_eo_len16(m);
7137 cst->plen -= m->m_pkthdr.len;
7138 m_freem(m);
7139 }
7140
7141 cst->tx_credits += cpl->credits;
7142 MPASS(cst->tx_credits <= cst->tx_total);
7143
7144 if (cst->flags & EO_SND_TAG_REF) {
7145 /*
7146 * As with ethofld_transmit(), hold an extra reference
7147 * so that the tag is stable across ethold_tx().
7148 */
7149 m_snd_tag_ref(&cst->com);
7150 m = mbufq_first(&cst->pending_tx);
7151 if (m != NULL && cst->tx_credits >= mbuf_eo_len16(m))
7152 ethofld_tx(cst);
7153 mtx_unlock(&cst->lock);
7154 m_snd_tag_rele(&cst->com);
7155 } else {
7156 /*
7157 * There shouldn't be any pending packets if the tag
7158 * was freed by the kernel since any pending packet
7159 * should hold a reference to the tag.
7160 */
7161 MPASS(mbufq_first(&cst->pending_tx) == NULL);
7162 mtx_unlock(&cst->lock);
7163 }
7164
7165 return (0);
7166 }
7167 #endif
7168