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