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