xref: /freebsd/sys/dev/cxgbe/adapter.h (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011, 2025 Chelsio Communications.
5  * Written by: Navdeep Parhar <np@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __T4_ADAPTER_H__
31 #define __T4_ADAPTER_H__
32 
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/counter.h>
36 #include <sys/rman.h>
37 #include <sys/types.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/rwlock.h>
41 #include <sys/seqc.h>
42 #include <sys/sx.h>
43 #include <sys/vmem.h>
44 #include <vm/uma.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <machine/bus.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/if_media.h>
56 #include <net/pfil.h>
57 #include <netinet/in.h>
58 #include <netinet/tcp_lro.h>
59 
60 #include "offload.h"
61 #include "t4_ioctl.h"
62 #include "common/t4_msg.h"
63 #include "firmware/t4fw_interface.h"
64 
65 #define KTR_CXGBE	KTR_SPARE3
66 MALLOC_DECLARE(M_CXGBE);
67 #define CXGBE_UNIMPLEMENTED(s) \
68     panic("%s (%s, line %d) not implemented yet.", s, __FILE__, __LINE__)
69 
70 /*
71  * Same as LIST_HEAD from queue.h.  This is to avoid conflict with LinuxKPI's
72  * LIST_HEAD when building iw_cxgbe.
73  */
74 #define	CXGBE_LIST_HEAD(name, type)					\
75 struct name {								\
76 	struct type *lh_first;	/* first element */			\
77 }
78 
79 #ifndef SYSCTL_ADD_UQUAD
80 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
81 #define sysctl_handle_64 sysctl_handle_quad
82 #define CTLTYPE_U64 CTLTYPE_QUAD
83 #endif
84 
85 SYSCTL_DECL(_hw_cxgbe);
86 
87 struct adapter;
88 typedef struct adapter adapter_t;
89 
90 enum {
91 	/*
92 	 * All ingress queues use this entry size.  Note that the firmware event
93 	 * queue and any iq expecting CPL_RX_PKT in the descriptor needs this to
94 	 * be at least 64.
95 	 */
96 	IQ_ESIZE = 64,
97 
98 	/* Default queue sizes for all kinds of ingress queues */
99 	FW_IQ_QSIZE = 256,
100 	RX_IQ_QSIZE = 1024,
101 
102 	/* All egress queues use this entry size */
103 	EQ_ESIZE = 64,
104 
105 	/* Default queue sizes for all kinds of egress queues */
106 	CTRL_EQ_QSIZE = 1024,
107 	TX_EQ_QSIZE = 1024,
108 
109 #if MJUMPAGESIZE != MCLBYTES
110 	SW_ZONE_SIZES = 4,	/* cluster, jumbop, jumbo9k, jumbo16k */
111 #else
112 	SW_ZONE_SIZES = 3,	/* cluster, jumbo9k, jumbo16k */
113 #endif
114 	CL_METADATA_SIZE = CACHE_LINE_SIZE,
115 
116 	SGE_MAX_WR_NDESC = SGE_MAX_WR_LEN / EQ_ESIZE, /* max WR size in desc */
117 	TX_SGL_SEGS = 39,
118 	TX_SGL_SEGS_TSO = 38,
119 	TX_SGL_SEGS_VM = 38,
120 	TX_SGL_SEGS_VM_TSO = 37,
121 	TX_SGL_SEGS_EO_TSO = 30,	/* XXX: lower for IPv6. */
122 	TX_SGL_SEGS_VXLAN_TSO = 37,
123 	TX_WR_FLITS = SGE_MAX_WR_LEN / 8
124 };
125 
126 enum {
127 	/* adapter intr_type */
128 	INTR_INTX	= (1 << 0),
129 	INTR_MSI	= (1 << 1),
130 	INTR_MSIX	= (1 << 2)
131 };
132 
133 enum {
134 	XGMAC_MTU	= (1 << 0),
135 	XGMAC_PROMISC	= (1 << 1),
136 	XGMAC_ALLMULTI	= (1 << 2),
137 	XGMAC_VLANEX	= (1 << 3),
138 	XGMAC_UCADDR	= (1 << 4),
139 	XGMAC_MCADDRS	= (1 << 5),
140 
141 	XGMAC_ALL	= 0xffff
142 };
143 
144 enum {
145 	/* flags understood by begin_synchronized_op */
146 	HOLD_LOCK	= (1 << 0),
147 	SLEEP_OK	= (1 << 1),
148 	INTR_OK		= (1 << 2),
149 
150 	/* flags understood by end_synchronized_op */
151 	LOCK_HELD	= HOLD_LOCK,
152 };
153 
154 enum {
155 	/* adapter flags.  synch_op or adapter_lock. */
156 	FULL_INIT_DONE	= (1 << 0),
157 	FW_OK		= (1 << 1),
158 	CHK_MBOX_ACCESS	= (1 << 2),
159 	MASTER_PF	= (1 << 3),
160 	BUF_PACKING_OK	= (1 << 6),
161 	IS_VF		= (1 << 7),
162 	KERN_TLS_ON	= (1 << 8),	/* HW is configured for KERN_TLS */
163 	CXGBE_BUSY	= (1 << 9),
164 
165 	/* adapter error_flags.  reg_lock for HW_OFF_LIMITS, atomics for the rest. */
166 	ADAP_STOPPED	= (1 << 0),	/* Adapter has been stopped. */
167 	ADAP_FATAL_ERR	= (1 << 1),	/* Encountered a fatal error. */
168 	HW_OFF_LIMITS	= (1 << 2),	/* off limits to all except reset_thread */
169 	ADAP_CIM_ERR	= (1 << 3),	/* Error was related to FW/CIM. */
170 
171 	/* port flags */
172 	HAS_TRACEQ	= (1 << 3),
173 	FIXED_IFMEDIA	= (1 << 4),	/* ifmedia list doesn't change. */
174 
175 	/* VI flags */
176 	VI_DETACHING	= (1 << 0),
177 	VI_INIT_DONE	= (1 << 1),
178 	/* 1 << 2 is unused, was VI_SYSCTL_CTX */
179 	TX_USES_VM_WR	= (1 << 3),
180 	VI_SKIP_STATS	= (1 << 4),
181 
182 	/* adapter debug_flags */
183 	DF_DUMP_MBOX		= (1 << 0),	/* Log all mbox cmd/rpl. */
184 	DF_LOAD_FW_ANYTIME	= (1 << 1),	/* Allow LOAD_FW after init */
185 	DF_DISABLE_TCB_CACHE	= (1 << 2),	/* Disable TCB cache (T6+) */
186 	DF_DISABLE_CFG_RETRY	= (1 << 3),	/* Disable fallback config */
187 
188 	/* adapter intr handler flags */
189 	IHF_INTR_CLEAR_ON_INIT	= (1 << 0),	/* Driver calls t4_intr_clear */
190 	IHF_NO_SHOW		= (1 << 1),	/* Do not display intr info */
191 	IHF_VERBOSE		= (1 << 2),	/* Display extra intr info */
192 	IHF_FATAL_IFF_ENABLED	= (1 << 3),	/* Fatal only if enabled */
193 	IHF_IGNORE_IF_DISABLED	= (1 << 4),	/* Ignore if disabled */
194 	IHF_CLR_ALL_SET		= (1 << 5),	/* Clear all set bits */
195 	IHF_CLR_ALL_UNIGNORED	= (1 << 6),	/* Clear all unignored bits */
196 	IHF_RUN_ALL_ACTIONS	= (1 << 7),	/* As if all cause are set */
197 	IHF_CLR_DELAYED		= (1 << 9),	/* Cleared in a delayed call */
198 };
199 
200 #define IS_DETACHING(vi)	((vi)->flags & VI_DETACHING)
201 #define SET_DETACHING(vi)	do {(vi)->flags |= VI_DETACHING;} while (0)
202 #define CLR_DETACHING(vi)	do {(vi)->flags &= ~VI_DETACHING;} while (0)
203 #define IS_BUSY(sc)	((sc)->flags & CXGBE_BUSY)
204 #define SET_BUSY(sc)	do {(sc)->flags |= CXGBE_BUSY;} while (0)
205 #define CLR_BUSY(sc)	do {(sc)->flags &= ~CXGBE_BUSY;} while (0)
206 
207 struct vi_info {
208 	device_t dev;
209 	struct port_info *pi;
210 	struct adapter *adapter;
211 
212 	if_t ifp;
213 	struct pfil_head *pfil;
214 
215 	unsigned long flags;
216 	int if_flags;
217 
218 	uint16_t *rss, *nm_rss;
219 	uint16_t viid;		/* opaque VI identifier */
220 	uint16_t smt_idx;
221 	uint16_t vin;
222 	uint8_t vfvld;
223 	int16_t  xact_addr_filt;/* index of exact MAC address filter */
224 	uint16_t rss_size;	/* size of VI's RSS table slice */
225 	uint16_t rss_base;	/* start of VI's RSS table slice */
226 	int hashen;
227 
228 	int nintr;
229 	int first_intr;
230 
231 	/* These need to be int as they are used in sysctl */
232 	int ntxq;		/* # of tx queues */
233 	int first_txq;		/* index of first tx queue */
234 	int rsrv_noflowq;	/* Reserve queue 0 for non-flowid packets */
235 	int nrxq;		/* # of rx queues */
236 	int first_rxq;		/* index of first rx queue */
237 	int nofldtxq;		/* # of offload tx queues */
238 	int first_ofld_txq;	/* index of first offload tx queue */
239 	int nofldrxq;		/* # of offload rx queues */
240 	int first_ofld_rxq;	/* index of first offload rx queue */
241 	int nnmtxq;
242 	int first_nm_txq;
243 	int nnmrxq;
244 	int first_nm_rxq;
245 	int tmr_idx;
246 	int ofld_tmr_idx;
247 	int pktc_idx;
248 	int ofld_pktc_idx;
249 	int qsize_rxq;
250 	int qsize_txq;
251 
252 	struct timeval last_refreshed;
253 	struct fw_vi_stats_vf stats;
254 	struct mtx tick_mtx;
255 	struct callout tick;
256 
257 	struct sysctl_ctx_list ctx;
258 	struct sysctl_oid *rxq_oid;
259 	struct sysctl_oid *txq_oid;
260 	struct sysctl_oid *nm_rxq_oid;
261 	struct sysctl_oid *nm_txq_oid;
262 	struct sysctl_oid *ofld_rxq_oid;
263 	struct sysctl_oid *ofld_txq_oid;
264 
265 	uint8_t hw_addr[ETHER_ADDR_LEN]; /* factory MAC address, won't change */
266 	u_int txq_rr;
267 	u_int rxq_rr;
268 };
269 
270 struct tx_ch_rl_params {
271 	enum fw_sched_params_rate ratemode;	/* %port (REL) or kbps (ABS) */
272 	uint32_t maxrate;
273 };
274 
275 /* CLRL state */
276 enum clrl_state {
277 	CS_UNINITIALIZED = 0,
278 	CS_PARAMS_SET,			/* sw parameters have been set. */
279 	CS_HW_UPDATE_REQUESTED,		/* async HW update requested. */
280 	CS_HW_UPDATE_IN_PROGRESS,	/* sync hw update in progress. */
281 	CS_HW_CONFIGURED		/* configured in the hardware. */
282 };
283 
284 /* CLRL flags */
285 enum {
286 	CF_USER		= (1 << 0),	/* was configured by driver ioctl. */
287 };
288 
289 struct tx_cl_rl_params {
290 	enum clrl_state state;
291 	int refcount;
292 	uint8_t flags;
293 	enum fw_sched_params_rate ratemode;	/* %port REL or ABS value */
294 	enum fw_sched_params_unit rateunit;	/* kbps or pps (when ABS) */
295 	enum fw_sched_params_mode mode;		/* aggr or per-flow */
296 	uint32_t maxrate;
297 	uint16_t pktsize;
298 	uint16_t burstsize;
299 };
300 
301 /* Tx scheduler parameters for a channel/port */
302 struct tx_sched_params {
303 	/* Channel Rate Limiter */
304 	struct tx_ch_rl_params ch_rl;
305 
306 	/* Class WRR */
307 	/* XXX */
308 
309 	/* Class Rate Limiter (including the default pktsize and burstsize). */
310 	int pktsize;
311 	int burstsize;
312 	struct tx_cl_rl_params cl_rl[];
313 };
314 
315 struct t4_vf_info {
316 	uint8_t mac[ETHER_ADDR_LEN];
317 	uint16_t vlan;
318 	bool configured;
319 	bool access_vlan;
320 };
321 
322 struct port_info {
323 	device_t dev;
324 	struct adapter *adapter;
325 
326 	struct vi_info *vi;
327 	int nvi;
328 	int up_vis;
329 	int uld_vis;
330 	bool vxlan_tcam_entry;
331 
332 	struct tx_sched_params *sched_params;
333 	struct t4_vf_info *iov_vfs;
334 	uint16_t iov_num_vfs;
335 	bool iov_status_supported;
336 
337 	struct mtx pi_lock;
338 	char lockname[16];
339 	unsigned long flags;
340 
341 	uint8_t  hw_port;	/* associated hardware port idx */
342 	int8_t   mdio_addr;
343 	uint8_t  port_type;
344 	uint8_t  mod_type;
345 	uint8_t  port_id;
346 	uint8_t  tx_chan;	/* tx TP c-channel */
347 	uint8_t  rx_chan;	/* rx TP c-channel */
348 	uint8_t  mps_bg_map;	/* rx MPS buffer group bitmap */
349 	uint8_t  rx_e_chan_map;	/* rx TP e-channel bitmap */
350 
351 	struct link_config link_cfg;
352 	struct ifmedia media;
353 
354 	struct port_stats stats;
355 	u_int tnl_cong_drops;
356 	u_int tx_parse_error;
357 	int fcs_reg;
358 	uint64_t fcs_base;
359 
360 	struct sysctl_ctx_list ctx;
361 };
362 
363 #define	IS_MAIN_VI(vi)		((vi) == &((vi)->pi->vi[0]))
364 
365 struct cluster_metadata {
366 	uma_zone_t zone;
367 	caddr_t cl;
368 	u_int refcount;
369 };
370 
371 struct fl_sdesc {
372 	caddr_t cl;
373 	uint16_t nmbuf;	/* # of driver originated mbufs with ref on cluster */
374 	int16_t moff;	/* offset of metadata from cl */
375 	uint8_t zidx;
376 };
377 
378 struct tx_desc {
379 	__be64 flit[8];
380 };
381 
382 struct tx_sdesc {
383 	struct mbuf *m;		/* m_nextpkt linked chain of frames */
384 	uint8_t desc_used;	/* # of hardware descriptors used by the WR */
385 };
386 
387 
388 #define IQ_PAD (IQ_ESIZE - sizeof(struct rsp_ctrl) - sizeof(struct rss_header))
389 struct iq_desc {
390 	struct rss_header rss;
391 	uint8_t cpl[IQ_PAD];
392 	struct rsp_ctrl rsp;
393 };
394 #undef IQ_PAD
395 CTASSERT(sizeof(struct iq_desc) == IQ_ESIZE);
396 
397 enum {
398 	/* iq type */
399 	IQ_OTHER	= FW_IQ_IQTYPE_OTHER,
400 	IQ_ETH		= FW_IQ_IQTYPE_NIC,
401 	IQ_OFLD		= FW_IQ_IQTYPE_OFLD,
402 
403 	/* iq flags */
404 	IQ_SW_ALLOCATED	= (1 << 0),	/* sw resources allocated */
405 	IQ_HAS_FL	= (1 << 1),	/* iq associated with a freelist */
406 	IQ_RX_TIMESTAMP	= (1 << 2),	/* provide the SGE rx timestamp */
407 	IQ_LRO_ENABLED	= (1 << 3),	/* iq is an eth rxq with LRO enabled */
408 	IQ_ADJ_CREDIT	= (1 << 4),	/* hw is off by 1 credit for this iq */
409 	IQ_HW_ALLOCATED	= (1 << 5),	/* fw/hw resources allocated */
410 
411 	/* iq state */
412 	IQS_DISABLED	= 0,
413 	IQS_BUSY	= 1,
414 	IQS_IDLE	= 2,
415 
416 	/* netmap related flags */
417 	NM_OFF	= 0,
418 	NM_ON	= 1,
419 	NM_BUSY	= 2,
420 };
421 
422 enum {
423 	CPL_COOKIE_RESERVED = 0,
424 	CPL_COOKIE_FILTER,
425 	CPL_COOKIE_DDP0,
426 	CPL_COOKIE_DDP1,
427 	CPL_COOKIE_TOM,
428 	CPL_COOKIE_HASHFILTER,
429 	CPL_COOKIE_ETHOFLD,
430 	CPL_COOKIE_KERN_TLS,
431 
432 	NUM_CPL_COOKIES = 8	/* Limited by M_COOKIE.  Do not increase. */
433 };
434 
435 /*
436  * Crypto replies use the low bit in the 64-bit cookie of CPL_FW6_PLD as a
437  * CPL cookie to identify the sender/receiver.
438  */
439 enum {
440 	CPL_FW6_COOKIE_CCR = 0,
441 	CPL_FW6_COOKIE_KTLS,
442 
443 	NUM_CPL_FW6_COOKIES = 2	/* Low bits of cookie value. */
444 };
445 
446 _Static_assert(powerof2(NUM_CPL_FW6_COOKIES),
447     "NUM_CPL_FW6_COOKIES must be a power of 2");
448 
449 #define	CPL_FW6_COOKIE_MASK	(NUM_CPL_FW6_COOKIES - 1)
450 
451 #define	CPL_FW6_PLD_COOKIE(cpl)	(be64toh((cpl)->data[1]) & ~CPL_FW6_COOKIE_MASK)
452 
453 struct sge_iq;
454 struct rss_header;
455 typedef int (*cpl_handler_t)(struct sge_iq *, const struct rss_header *,
456     struct mbuf *);
457 typedef int (*an_handler_t)(struct sge_iq *, const struct rsp_ctrl *);
458 typedef int (*fw_msg_handler_t)(struct adapter *, const __be64 *);
459 
460 /*
461  * Ingress Queue: T4 is producer, driver is consumer.
462  */
463 struct sge_iq {
464 	uint16_t flags;
465 	uint8_t qtype;
466 	volatile int state;
467 	struct adapter *adapter;
468 	struct iq_desc  *desc;	/* KVA of descriptor ring */
469 	int8_t   intr_pktc_idx;	/* packet count threshold index */
470 	uint8_t  gen;		/* generation bit */
471 	uint8_t  intr_params;	/* interrupt holdoff parameters */
472 	int8_t   cong_drop;	/* congestion drop settings for the queue */
473 	uint16_t qsize;		/* size (# of entries) of the queue */
474 	uint16_t sidx;		/* index of the entry with the status page */
475 	uint16_t cidx;		/* consumer index */
476 	uint16_t cntxt_id;	/* SGE context id for the iq */
477 	uint16_t abs_id;	/* absolute SGE id for the iq */
478 	int16_t intr_idx;	/* interrupt used by the queue */
479 
480 	STAILQ_ENTRY(sge_iq) link;
481 
482 	bus_dma_tag_t desc_tag;
483 	bus_dmamap_t desc_map;
484 	bus_addr_t ba;		/* bus address of descriptor ring */
485 };
486 
487 enum {
488 	/* eq type */
489 	EQ_CTRL		= 1,
490 	EQ_ETH		= 2,
491 	EQ_OFLD		= 3,
492 
493 	/* eq flags */
494 	EQ_SW_ALLOCATED	= (1 << 0),	/* sw resources allocated */
495 	EQ_HW_ALLOCATED	= (1 << 1),	/* hw/fw resources allocated */
496 	EQ_ENABLED	= (1 << 3),	/* open for business */
497 	EQ_QFLUSH	= (1 << 4),	/* if_qflush in progress */
498 };
499 
500 /* Listed in order of preference.  Update t4_sysctls too if you change these */
501 enum {DOORBELL_UDB, DOORBELL_WCWR, DOORBELL_UDBWC, DOORBELL_KDB};
502 
503 /*
504  * Egress Queue: driver is producer, T4 is consumer.
505  *
506  * Note: A free list is an egress queue (driver produces the buffers and T4
507  * consumes them) but it's special enough to have its own struct (see sge_fl).
508  */
509 struct sge_eq {
510 	unsigned int flags;	/* MUST be first */
511 	unsigned int cntxt_id;	/* SGE context id for the eq */
512 	unsigned int abs_id;	/* absolute SGE id for the eq */
513 	uint8_t type;		/* EQ_CTRL/EQ_ETH/EQ_OFLD */
514 	uint8_t doorbells;
515 	uint8_t port_id;	/* port_id of the port associated with the eq */
516 	uint8_t tx_chan;	/* tx channel used by the eq */
517 	uint8_t hw_port;	/* hw port used by the eq */
518 	struct mtx eq_lock;
519 
520 	struct tx_desc *desc;	/* KVA of descriptor ring */
521 	volatile uint32_t *udb;	/* KVA of doorbell (lies within BAR2) */
522 	u_int udb_qid;		/* relative qid within the doorbell page */
523 	uint16_t sidx;		/* index of the entry with the status page */
524 	uint16_t cidx;		/* consumer idx (desc idx) */
525 	uint16_t pidx;		/* producer idx (desc idx) */
526 	uint16_t equeqidx;	/* EQUEQ last requested at this pidx */
527 	uint16_t dbidx;		/* pidx of the most recent doorbell */
528 	uint16_t iqid;		/* cached iq->cntxt_id (see iq below) */
529 	volatile u_int equiq;	/* EQUIQ outstanding */
530 	struct sge_iq *iq;	/* iq that receives egr_update for the eq */
531 
532 	bus_dma_tag_t desc_tag;
533 	bus_dmamap_t desc_map;
534 	bus_addr_t ba;		/* bus address of descriptor ring */
535 	char lockname[16];
536 };
537 
538 struct rx_buf_info {
539 	uma_zone_t zone;	/* zone that this cluster comes from */
540 	uint16_t size1;		/* same as size of cluster: 2K/4K/9K/16K.
541 				 * hwsize[hwidx1] = size1.  No spare. */
542 	uint16_t size2;		/* hwsize[hwidx2] = size2.
543 				 * spare in cluster = size1 - size2. */
544 	int8_t hwidx1;		/* SGE bufsize idx for size1 */
545 	int8_t hwidx2;		/* SGE bufsize idx for size2 */
546 	uint8_t type;		/* EXT_xxx type of the cluster */
547 };
548 
549 enum {
550 	NUM_MEMWIN = 3,
551 
552 	MEMWIN0_APERTURE = 2048,
553 	MEMWIN0_BASE     = 0x1b800,
554 
555 	MEMWIN1_APERTURE = 32768,
556 	MEMWIN1_BASE     = 0x28000,
557 
558 	MEMWIN2_APERTURE_T4 = 65536,
559 	MEMWIN2_BASE_T4     = 0x30000,
560 
561 	MEMWIN2_APERTURE_T5 = 128 * 1024,
562 	MEMWIN2_BASE_T5     = 0x60000,
563 };
564 
565 struct memwin {
566 	struct rwlock mw_lock __aligned(CACHE_LINE_SIZE);
567 	uint32_t mw_base;	/* constant after setup_memwin */
568 	uint32_t mw_aperture;	/* ditto */
569 	uint32_t mw_curpos;	/* protected by mw_lock */
570 };
571 
572 enum {
573 	FL_STARVING	= (1 << 0), /* on the adapter's list of starving fl's */
574 	FL_DOOMED	= (1 << 1), /* about to be destroyed */
575 	FL_BUF_PACKING	= (1 << 2), /* buffer packing enabled */
576 	FL_BUF_RESUME	= (1 << 3), /* resume from the middle of the frame */
577 };
578 
579 #define FL_RUNNING_LOW(fl) \
580     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) <= fl->lowat)
581 #define FL_NOT_RUNNING_LOW(fl) \
582     (IDXDIFF(fl->dbidx * 8, fl->cidx, fl->sidx * 8) >= 2 * fl->lowat)
583 
584 struct sge_fl {
585 	struct mtx fl_lock;
586 	__be64 *desc;		/* KVA of descriptor ring, ptr to addresses */
587 	struct fl_sdesc *sdesc;	/* KVA of software descriptor ring */
588 	uint16_t zidx;		/* refill zone idx */
589 	uint16_t safe_zidx;
590 	uint16_t lowat;		/* # of buffers <= this means fl needs help */
591 	int flags;
592 	uint16_t buf_boundary;
593 
594 	/* The 16b idx all deal with hw descriptors */
595 	uint16_t dbidx;		/* hw pidx after last doorbell */
596 	uint16_t sidx;		/* index of status page */
597 	volatile uint16_t hw_cidx;
598 
599 	/* The 32b idx are all buffer idx, not hardware descriptor idx */
600 	uint32_t cidx;		/* consumer index */
601 	uint32_t pidx;		/* producer index */
602 
603 	uint32_t dbval;
604 	u_int rx_offset;	/* offset in fl buf (when buffer packing) */
605 	volatile uint32_t *udb;
606 
607 	uint64_t cl_allocated;	/* # of clusters allocated */
608 	uint64_t cl_recycled;	/* # of clusters recycled */
609 	uint64_t cl_fast_recycled; /* # of clusters recycled (fast) */
610 
611 	/* These 3 are valid when FL_BUF_RESUME is set, stale otherwise. */
612 	struct mbuf *m0;
613 	struct mbuf **pnext;
614 	u_int remaining;
615 
616 	uint16_t qsize;		/* # of hw descriptors (status page included) */
617 	uint16_t cntxt_id;	/* SGE context id for the freelist */
618 	TAILQ_ENTRY(sge_fl) link; /* All starving freelists */
619 	bus_dma_tag_t desc_tag;
620 	bus_dmamap_t desc_map;
621 	char lockname[16];
622 	bus_addr_t ba;		/* bus address of descriptor ring */
623 };
624 
625 struct mp_ring;
626 
627 struct txpkts {
628 	uint8_t wr_type;	/* type 0 or type 1 */
629 	uint8_t npkt;		/* # of packets in this work request */
630 	uint8_t len16;		/* # of 16B pieces used by this work request */
631 	uint8_t score;
632 	uint8_t max_npkt;	/* maximum number of packets allowed */
633 	uint16_t plen;		/* total payload (sum of all packets) */
634 
635 	/* straight from fw_eth_tx_pkts_vm_wr. */
636 	__u8   ethmacdst[6];
637 	__u8   ethmacsrc[6];
638 	__be16 ethtype;
639 	__be16 vlantci;
640 
641 	struct mbuf *mb[15];
642 };
643 
644 /* txq: SGE egress queue + what's needed for Ethernet NIC */
645 struct sge_txq {
646 	struct sge_eq eq;	/* MUST be first */
647 
648 	if_t ifp;		/* the interface this txq belongs to */
649 	struct mp_ring *r;	/* tx software ring */
650 	struct tx_sdesc *sdesc;	/* KVA of software descriptor ring */
651 	struct sglist *gl;
652 	__be32 cpl_ctrl0;	/* for convenience */
653 	int tc_idx;		/* traffic class */
654 	uint64_t last_tx;	/* cycle count when eth_tx was last called */
655 	struct txpkts txp;
656 
657 	struct task tx_reclaim_task;
658 	/* stats for common events first */
659 
660 	uint64_t txcsum;	/* # of times hardware assisted with checksum */
661 	uint64_t tso_wrs;	/* # of TSO work requests */
662 	uint64_t vlan_insertion;/* # of times VLAN tag was inserted */
663 	uint64_t imm_wrs;	/* # of work requests with immediate data */
664 	uint64_t sgl_wrs;	/* # of work requests with direct SGL */
665 	uint64_t txpkt_wrs;	/* # of txpkt work requests (not coalesced) */
666 	uint64_t txpkts0_wrs;	/* # of type0 coalesced tx work requests */
667 	uint64_t txpkts1_wrs;	/* # of type1 coalesced tx work requests */
668 	uint64_t txpkts0_pkts;	/* # of frames in type0 coalesced tx WRs */
669 	uint64_t txpkts1_pkts;	/* # of frames in type1 coalesced tx WRs */
670 	uint64_t txpkts_flush;	/* # of times txp had to be sent by tx_update */
671 	uint64_t raw_wrs;	/* # of raw work requests (alloc_wr_mbuf) */
672 	uint64_t vxlan_tso_wrs;	/* # of VXLAN TSO work requests */
673 	uint64_t vxlan_txcsum;
674 
675 	uint64_t kern_tls_records;
676 	uint64_t kern_tls_short;
677 	uint64_t kern_tls_partial;
678 	uint64_t kern_tls_full;
679 	uint64_t kern_tls_octets;
680 	uint64_t kern_tls_waste;
681 	uint64_t kern_tls_header;
682 	uint64_t kern_tls_fin_short;
683 	uint64_t kern_tls_cbc;
684 	uint64_t kern_tls_gcm;
685 	union {
686 		struct {
687 			/* T6 only. */
688 			uint64_t kern_tls_options;
689 			uint64_t kern_tls_fin;
690 		};
691 		struct {
692 			/* T7 only. */
693 			uint64_t kern_tls_ghash_received;
694 			uint64_t kern_tls_ghash_requested;
695 			uint64_t kern_tls_lso;
696 			uint64_t kern_tls_partial_ghash;
697 			uint64_t kern_tls_splitmode;
698 			uint64_t kern_tls_trailer;
699 		};
700 	};
701 
702 	/* stats for not-that-common events */
703 
704 	/* Optional scratch space for constructing work requests. */
705 	uint8_t ss[SGE_MAX_WR_LEN] __aligned(16);
706 } __aligned(CACHE_LINE_SIZE);
707 
708 /* rxq: SGE ingress queue + SGE free list + miscellaneous items */
709 struct sge_rxq {
710 	struct sge_iq iq;	/* MUST be first */
711 	struct sge_fl fl;	/* MUST follow iq */
712 
713 	if_t ifp;		/* the interface this rxq belongs to */
714 	struct lro_ctrl lro;	/* LRO state */
715 
716 	/* stats for common events first */
717 
718 	uint64_t rxcsum;	/* # of times hardware assisted with checksum */
719 	uint64_t vlan_extraction;/* # of times VLAN tag was extracted */
720 	uint64_t vxlan_rxcsum;
721 
722 	/* stats for not-that-common events */
723 
724 } __aligned(CACHE_LINE_SIZE);
725 
726 static inline struct sge_rxq *
727 iq_to_rxq(struct sge_iq *iq)
728 {
729 
730 	return (__containerof(iq, struct sge_rxq, iq));
731 }
732 
733 /* ofld_rxq: SGE ingress queue + SGE free list + miscellaneous items */
734 struct sge_ofld_rxq {
735 	struct sge_iq iq;	/* MUST be first */
736 	struct sge_fl fl;	/* MUST follow iq */
737 	counter_u64_t rx_iscsi_ddp_setup_ok;
738 	counter_u64_t rx_iscsi_ddp_setup_error;
739 	uint64_t rx_iscsi_ddp_pdus;
740 	uint64_t rx_iscsi_ddp_octets;
741 	uint64_t rx_iscsi_fl_pdus;
742 	uint64_t rx_iscsi_fl_octets;
743 	uint64_t rx_iscsi_padding_errors;
744 	uint64_t rx_iscsi_header_digest_errors;
745 	uint64_t rx_iscsi_data_digest_errors;
746 	counter_u64_t rx_nvme_ddp_setup_ok;
747 	counter_u64_t rx_nvme_ddp_setup_no_stag;
748 	counter_u64_t rx_nvme_ddp_setup_error;
749 	counter_u64_t rx_nvme_ddp_pdus;
750 	counter_u64_t rx_nvme_ddp_octets;
751 	counter_u64_t rx_nvme_fl_pdus;
752 	counter_u64_t rx_nvme_fl_octets;
753 	counter_u64_t rx_nvme_invalid_headers;
754 	counter_u64_t rx_nvme_header_digest_errors;
755 	counter_u64_t rx_nvme_data_digest_errors;
756 	uint64_t rx_aio_ddp_jobs;
757 	uint64_t rx_aio_ddp_octets;
758 	u_long	rx_toe_tls_records;
759 	u_long	rx_toe_tls_octets;
760 	u_long	rx_toe_ddp_octets;
761 	counter_u64_t ddp_buffer_alloc;
762 	counter_u64_t ddp_buffer_reuse;
763 	counter_u64_t ddp_buffer_free;
764 } __aligned(CACHE_LINE_SIZE);
765 
766 static inline struct sge_ofld_rxq *
767 iq_to_ofld_rxq(struct sge_iq *iq)
768 {
769 
770 	return (__containerof(iq, struct sge_ofld_rxq, iq));
771 }
772 
773 struct wrqe {
774 	STAILQ_ENTRY(wrqe) link;
775 	struct sge_wrq *wrq;
776 	int wr_len;
777 	char wr[] __aligned(16);
778 };
779 
780 struct wrq_cookie {
781 	TAILQ_ENTRY(wrq_cookie) link;
782 	int ndesc;
783 	int pidx;
784 };
785 
786 /*
787  * wrq: SGE egress queue that is given prebuilt work requests.  Control queues
788  * are of this type.
789  */
790 struct sge_wrq {
791 	struct sge_eq eq;	/* MUST be first */
792 
793 	struct adapter *adapter;
794 	struct task wrq_tx_task;
795 
796 	/* Tx desc reserved but WR not "committed" yet. */
797 	TAILQ_HEAD(wrq_incomplete_wrs , wrq_cookie) incomplete_wrs;
798 
799 	/* List of WRs ready to go out as soon as descriptors are available. */
800 	STAILQ_HEAD(, wrqe) wr_list;
801 	u_int nwr_pending;
802 	u_int ndesc_needed;
803 
804 	/* stats for common events first */
805 
806 	uint64_t tx_wrs_direct;	/* # of WRs written directly to desc ring. */
807 	uint64_t tx_wrs_ss;	/* # of WRs copied from scratch space. */
808 	uint64_t tx_wrs_copied;	/* # of WRs queued and copied to desc ring. */
809 
810 	/* stats for not-that-common events */
811 
812 	/*
813 	 * Scratch space for work requests that wrap around after reaching the
814 	 * status page, and some information about the last WR that used it.
815 	 */
816 	uint16_t ss_pidx;
817 	uint16_t ss_len;
818 	uint8_t ss[SGE_MAX_WR_LEN];
819 
820 } __aligned(CACHE_LINE_SIZE);
821 
822 /* ofld_txq: SGE egress queue + miscellaneous items */
823 struct sge_ofld_txq {
824 	struct sge_wrq wrq;
825 	counter_u64_t tx_iscsi_pdus;
826 	counter_u64_t tx_iscsi_octets;
827 	counter_u64_t tx_iscsi_iso_wrs;
828 	counter_u64_t tx_nvme_pdus;
829 	counter_u64_t tx_nvme_octets;
830 	counter_u64_t tx_nvme_iso_wrs;
831 	counter_u64_t tx_aio_jobs;
832 	counter_u64_t tx_aio_octets;
833 	counter_u64_t tx_toe_tls_records;
834 	counter_u64_t tx_toe_tls_octets;
835 } __aligned(CACHE_LINE_SIZE);
836 
837 static inline int
838 ofld_txq_group(int val, int mask)
839 {
840 	const uint32_t ngroup = 1 << bitcount32(mask);
841 	const int mshift = ffs(mask) - 1;
842 	const uint32_t gmask = ngroup - 1;
843 
844 	return (val >> mshift & gmask);
845 }
846 
847 #define INVALID_NM_RXQ_CNTXT_ID ((uint16_t)(-1))
848 struct sge_nm_rxq {
849 	/* Items used by the driver rx ithread are in this cacheline. */
850 	volatile int nm_state __aligned(CACHE_LINE_SIZE);	/* NM_OFF, NM_ON, or NM_BUSY */
851 	u_int nid;		/* netmap ring # for this queue */
852 	struct vi_info *vi;
853 
854 	struct iq_desc *iq_desc;
855 	uint16_t iq_abs_id;
856 	uint16_t iq_cntxt_id;
857 	uint16_t iq_cidx;
858 	uint16_t iq_sidx;
859 	uint8_t iq_gen;
860 	uint32_t fl_sidx;
861 
862 	/* Items used by netmap rxsync are in this cacheline. */
863 	__be64  *fl_desc __aligned(CACHE_LINE_SIZE);
864 	uint16_t fl_cntxt_id;
865 	uint32_t fl_pidx;
866 	uint32_t fl_sidx2;	/* copy of fl_sidx */
867 	uint32_t fl_db_val;
868 	u_int fl_db_saved;
869 	u_int fl_db_threshold;	/* in descriptors */
870 	u_int fl_hwidx:4;
871 
872 	/*
873 	 * fl_cidx is used by both the ithread and rxsync, the rest are not used
874 	 * in the rx fast path.
875 	 */
876 	uint32_t fl_cidx __aligned(CACHE_LINE_SIZE);
877 
878 	bus_dma_tag_t iq_desc_tag;
879 	bus_dmamap_t iq_desc_map;
880 	bus_addr_t iq_ba;
881 	int intr_idx;
882 
883 	bus_dma_tag_t fl_desc_tag;
884 	bus_dmamap_t fl_desc_map;
885 	bus_addr_t fl_ba;
886 };
887 
888 #define INVALID_NM_TXQ_CNTXT_ID ((u_int)(-1))
889 struct sge_nm_txq {
890 	struct tx_desc *desc;
891 	uint16_t cidx;
892 	uint16_t pidx;
893 	uint16_t sidx;
894 	uint16_t equiqidx;	/* EQUIQ last requested at this pidx */
895 	uint16_t equeqidx;	/* EQUEQ last requested at this pidx */
896 	uint16_t dbidx;		/* pidx of the most recent doorbell */
897 	uint8_t doorbells;
898 	volatile uint32_t *udb;
899 	u_int udb_qid;
900 	u_int cntxt_id;
901 	__be32 cpl_ctrl0;	/* for convenience */
902 	__be32 op_pkd;		/* ditto */
903 	u_int nid;		/* netmap ring # for this queue */
904 
905 	/* infrequently used items after this */
906 
907 	bus_dma_tag_t desc_tag;
908 	bus_dmamap_t desc_map;
909 	bus_addr_t ba;
910 	int iqidx;
911 } __aligned(CACHE_LINE_SIZE);
912 
913 struct sge {
914 	int nctrlq;	/* total # of control queues */
915 	int nrxq;	/* total # of Ethernet rx queues */
916 	int ntxq;	/* total # of Ethernet tx queues */
917 	int nofldrxq;	/* total # of TOE rx queues */
918 	int nofldtxq;	/* total # of TOE tx queues */
919 	int nnmrxq;	/* total # of netmap rx queues */
920 	int nnmtxq;	/* total # of netmap tx queues */
921 	int niq;	/* total # of ingress queues */
922 	int neq;	/* total # of egress queues */
923 
924 	struct sge_iq fwq;	/* Firmware event queue */
925 	struct sge_wrq *ctrlq;	/* Control queues */
926 	struct sge_txq *txq;	/* NIC tx queues */
927 	struct sge_rxq *rxq;	/* NIC rx queues */
928 	struct sge_ofld_txq *ofld_txq;	/* TOE tx queues */
929 	struct sge_ofld_rxq *ofld_rxq;	/* TOE rx queues */
930 	struct sge_nm_txq *nm_txq;	/* netmap tx queues */
931 	struct sge_nm_rxq *nm_rxq;	/* netmap rx queues */
932 
933 	uint16_t iq_start;	/* first cntxt_id */
934 	uint16_t iq_base;	/* first abs_id */
935 	int eq_start;		/* first cntxt_id */
936 	int eq_base;		/* first abs_id */
937 	int iqmap_sz;
938 	int eqmap_sz;
939 	struct sge_iq **iqmap;	/* iq->cntxt_id to iq mapping */
940 	struct sge_eq **eqmap;	/* eq->cntxt_id to eq mapping */
941 
942 	int8_t safe_zidx;
943 	struct rx_buf_info rx_buf_info[SW_ZONE_SIZES];
944 };
945 
946 struct devnames {
947 	const char *nexus_name;
948 	const char *ifnet_name;
949 	const char *vi_ifnet_name;
950 	const char *pf03_drv_name;
951 	const char *vf_nexus_name;
952 	const char *vf_ifnet_name;
953 };
954 
955 struct clip_entry;
956 
957 #define CNT_CAL_INFO 3
958 struct clock_sync {
959 	uint64_t hw_cur;
960 	uint64_t hw_prev;
961 	sbintime_t sbt_cur;
962 	sbintime_t sbt_prev;
963 	seqc_t gen;
964 };
965 
966 struct adapter {
967 	SLIST_ENTRY(adapter) link;
968 	device_t dev;
969 	struct cdev *cdev;
970 	const struct devnames *names;
971 
972 	/* PCIe register resources */
973 	int regs_rid;
974 	struct resource *regs_res;
975 	int msix_rid;
976 	struct resource *msix_res;
977 	bus_size_t mmio_len;
978 	int udbs_rid;
979 	struct resource *udbs_res;
980 	volatile uint8_t *udbs_base;
981 
982 	unsigned int pf;
983 	unsigned int mbox;
984 	unsigned int vpd_busy;
985 	unsigned int vpd_flag;
986 
987 	/* Interrupt information */
988 	int intr_type;
989 	int intr_count;
990 	struct irq {
991 		struct resource *res;
992 		int rid;
993 		void *tag;
994 		struct sge_rxq *rxq;
995 		struct sge_nm_rxq *nm_rxq;
996 	} __aligned(CACHE_LINE_SIZE) *irq;
997 	int sge_gts_reg;
998 	int sge_kdoorbell_reg;
999 
1000 	bus_dma_tag_t dmat;	/* Parent DMA tag */
1001 
1002 	struct sge sge;
1003 	int lro_timeout;
1004 	int sc_do_rxcopy;
1005 
1006 	int vxlan_port;
1007 	u_int vxlan_refcount;
1008 	int rawf_base;
1009 	int nrawf;
1010 	u_int vlan_id;
1011 
1012 	struct taskqueue *tq[MAX_NPORTS];	/* General purpose taskqueues */
1013 	struct port_info *port[MAX_NPORTS];
1014 	uint8_t chan_map[MAX_NCHAN];		/* tx_chan -> port_id */
1015 	uint8_t port_map[MAX_NPORTS];		/* hw_port -> port_id */
1016 
1017 	CXGBE_LIST_HEAD(, clip_entry) *clip_table;
1018 	TAILQ_HEAD(, clip_entry) clip_pending;	/* these need hw update. */
1019 	u_long clip_mask;
1020 	int clip_gen;
1021 	struct timeout_task clip_task;
1022 
1023 	void *tom_softc;	/* (struct tom_data *) */
1024 	struct tom_tunables tt;
1025 	struct t4_offload_policy *policy;
1026 	struct rwlock policy_lock;
1027 
1028 	void *iwarp_softc;	/* (struct c4iw_dev *) */
1029 	struct iw_tunables iwt;
1030 	void *iscsi_ulp_softc;	/* (struct cxgbei_data *) */
1031 	void *nvme_ulp_softc;	/* (struct nvmf_che_adapter *) */
1032 	struct l2t_data *l2t;	/* L2 table */
1033 	struct smt_data *smt;	/* Source MAC Table */
1034 	struct tid_info tids;
1035 	vmem_t *key_map;
1036 	struct tls_tunables tlst;
1037 
1038 	vmem_t *pbl_arena;
1039 	vmem_t *stag_arena;
1040 
1041 	uint8_t doorbells;
1042 	int offload_map;	/* port_id's with IFCAP_TOE enabled */
1043 	int bt_map;		/* hw_port's that are BASE-T */
1044 	int active_ulds;	/* ULDs activated on this adapter */
1045 	int flags;
1046 	int debug_flags;
1047 	int error_flags;	/* Used by error handler and live reset. */
1048 	int intr_flags;		/* Used by interrupt setup/handlers. */
1049 
1050 	char ifp_lockname[16];
1051 	struct mtx ifp_lock;
1052 	if_t ifp;		/* tracer ifp */
1053 	struct ifmedia media;
1054 	int traceq;		/* iq used by all tracers, -1 if none */
1055 	int tracer_valid;	/* bitmap of valid tracers */
1056 	int tracer_enabled;	/* bitmap of enabled tracers */
1057 
1058 	char fw_version[16];
1059 	char tp_version[16];
1060 	char er_version[16];
1061 	char bs_version[16];
1062 	char cfg_file[32];
1063 	u_int cfcsum;
1064 	struct adapter_params params;
1065 	const struct chip_params *chip_params;
1066 	struct t4_virt_res vres;
1067 
1068 	uint16_t nbmcaps;
1069 	uint16_t linkcaps;
1070 	uint16_t switchcaps;
1071 	uint16_t nvmecaps;
1072 	uint16_t niccaps;
1073 	uint16_t toecaps;
1074 	uint16_t rdmacaps;
1075 	uint16_t cryptocaps;
1076 	uint16_t iscsicaps;
1077 	uint16_t fcoecaps;
1078 
1079 	struct sysctl_ctx_list ctx;
1080 	struct sysctl_oid *ctrlq_oid;
1081 	struct sysctl_oid *fwq_oid;
1082 
1083 	struct mtx sc_lock;
1084 	char lockname[16];
1085 
1086 	/* Starving free lists */
1087 	struct mtx sfl_lock;	/* same cache-line as sc_lock? but that's ok */
1088 	TAILQ_HEAD(, sge_fl) sfl;
1089 	struct callout sfl_callout;
1090 	struct callout cal_callout;
1091 	struct clock_sync cal_info[CNT_CAL_INFO];
1092 	int cal_current;
1093 	int cal_count;
1094 	uint32_t cal_gen;
1095 
1096 	/*
1097 	 * Driver code that can run when the adapter is suspended must use this
1098 	 * lock or a synchronized_op and check for HW_OFF_LIMITS before
1099 	 * accessing hardware.
1100 	 *
1101 	 * XXX: could be changed to rwlock.  wlock in suspend/resume and for
1102 	 * indirect register access, rlock everywhere else.
1103 	 */
1104 	struct mtx reg_lock;
1105 
1106 	struct memwin memwin[NUM_MEMWIN];	/* memory windows */
1107 
1108 	struct mtx tc_lock;
1109 	struct task tc_task;
1110 
1111 	struct task fatal_error_task;
1112 	struct task reset_task;
1113 	const void *reset_thread;
1114 	int num_resets;
1115 	int incarnation;
1116 
1117 	const char *last_op;
1118 	const void *last_op_thr;
1119 	int last_op_flags;
1120 
1121 	int swintr;
1122 	int sensor_resets;
1123 
1124 	struct callout ktls_tick;
1125 };
1126 
1127 #define ADAPTER_LOCK(sc)		mtx_lock(&(sc)->sc_lock)
1128 #define ADAPTER_UNLOCK(sc)		mtx_unlock(&(sc)->sc_lock)
1129 #define ADAPTER_LOCK_ASSERT_OWNED(sc)	mtx_assert(&(sc)->sc_lock, MA_OWNED)
1130 #define ADAPTER_LOCK_ASSERT_NOTOWNED(sc) mtx_assert(&(sc)->sc_lock, MA_NOTOWNED)
1131 
1132 #define ASSERT_SYNCHRONIZED_OP(sc)	\
1133     KASSERT(IS_BUSY(sc) && \
1134 	(mtx_owned(&(sc)->sc_lock) || sc->last_op_thr == curthread), \
1135 	("%s: operation not synchronized.", __func__))
1136 
1137 #define PORT_LOCK(pi)			mtx_lock(&(pi)->pi_lock)
1138 #define PORT_UNLOCK(pi)			mtx_unlock(&(pi)->pi_lock)
1139 #define PORT_LOCK_ASSERT_OWNED(pi)	mtx_assert(&(pi)->pi_lock, MA_OWNED)
1140 #define PORT_LOCK_ASSERT_NOTOWNED(pi)	mtx_assert(&(pi)->pi_lock, MA_NOTOWNED)
1141 
1142 #define FL_LOCK(fl)			mtx_lock(&(fl)->fl_lock)
1143 #define FL_TRYLOCK(fl)			mtx_trylock(&(fl)->fl_lock)
1144 #define FL_UNLOCK(fl)			mtx_unlock(&(fl)->fl_lock)
1145 #define FL_LOCK_ASSERT_OWNED(fl)	mtx_assert(&(fl)->fl_lock, MA_OWNED)
1146 #define FL_LOCK_ASSERT_NOTOWNED(fl)	mtx_assert(&(fl)->fl_lock, MA_NOTOWNED)
1147 
1148 #define RXQ_FL_LOCK(rxq)		FL_LOCK(&(rxq)->fl)
1149 #define RXQ_FL_UNLOCK(rxq)		FL_UNLOCK(&(rxq)->fl)
1150 #define RXQ_FL_LOCK_ASSERT_OWNED(rxq)	FL_LOCK_ASSERT_OWNED(&(rxq)->fl)
1151 #define RXQ_FL_LOCK_ASSERT_NOTOWNED(rxq) FL_LOCK_ASSERT_NOTOWNED(&(rxq)->fl)
1152 
1153 #define EQ_LOCK(eq)			mtx_lock(&(eq)->eq_lock)
1154 #define EQ_TRYLOCK(eq)			mtx_trylock(&(eq)->eq_lock)
1155 #define EQ_UNLOCK(eq)			mtx_unlock(&(eq)->eq_lock)
1156 #define EQ_LOCK_ASSERT_OWNED(eq)	mtx_assert(&(eq)->eq_lock, MA_OWNED)
1157 #define EQ_LOCK_ASSERT_NOTOWNED(eq)	mtx_assert(&(eq)->eq_lock, MA_NOTOWNED)
1158 
1159 #define TXQ_LOCK(txq)			EQ_LOCK(&(txq)->eq)
1160 #define TXQ_TRYLOCK(txq)		EQ_TRYLOCK(&(txq)->eq)
1161 #define TXQ_UNLOCK(txq)			EQ_UNLOCK(&(txq)->eq)
1162 #define TXQ_LOCK_ASSERT_OWNED(txq)	EQ_LOCK_ASSERT_OWNED(&(txq)->eq)
1163 #define TXQ_LOCK_ASSERT_NOTOWNED(txq)	EQ_LOCK_ASSERT_NOTOWNED(&(txq)->eq)
1164 
1165 #define for_each_txq(vi, iter, q) \
1166 	for (q = &vi->adapter->sge.txq[vi->first_txq], iter = 0; \
1167 	    iter < vi->ntxq; ++iter, ++q)
1168 #define for_each_rxq(vi, iter, q) \
1169 	for (q = &vi->adapter->sge.rxq[vi->first_rxq], iter = 0; \
1170 	    iter < vi->nrxq; ++iter, ++q)
1171 #define for_each_ofld_txq(vi, iter, q) \
1172 	for (q = &vi->adapter->sge.ofld_txq[vi->first_ofld_txq], iter = 0; \
1173 	    iter < vi->nofldtxq; ++iter, ++q)
1174 #define for_each_ofld_rxq(vi, iter, q) \
1175 	for (q = &vi->adapter->sge.ofld_rxq[vi->first_ofld_rxq], iter = 0; \
1176 	    iter < vi->nofldrxq; ++iter, ++q)
1177 #define for_each_nm_txq(vi, iter, q) \
1178 	for (q = &vi->adapter->sge.nm_txq[vi->first_nm_txq], iter = 0; \
1179 	    iter < vi->nnmtxq; ++iter, ++q)
1180 #define for_each_nm_rxq(vi, iter, q) \
1181 	for (q = &vi->adapter->sge.nm_rxq[vi->first_nm_rxq], iter = 0; \
1182 	    iter < vi->nnmrxq; ++iter, ++q)
1183 #define for_each_vi(_pi, _iter, _vi) \
1184 	for ((_vi) = (_pi)->vi, (_iter) = 0; (_iter) < (_pi)->nvi; \
1185 	     ++(_iter), ++(_vi))
1186 
1187 #define IDXINCR(idx, incr, wrap) do { \
1188 	idx = wrap - idx > incr ? idx + incr : incr - (wrap - idx); \
1189 } while (0)
1190 #define IDXDIFF(head, tail, wrap) \
1191 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
1192 
1193 /* One for errors, one for firmware events */
1194 #define T4_EXTRA_INTR 2
1195 
1196 /* One for firmware events */
1197 #define T4VF_EXTRA_INTR 1
1198 
1199 static inline int
1200 forwarding_intr_to_fwq(struct adapter *sc)
1201 {
1202 
1203 	return (sc->intr_count == 1);
1204 }
1205 
1206 /* Works reliably inside a synch_op or with reg_lock held. */
1207 static inline bool
1208 hw_off_limits(struct adapter *sc)
1209 {
1210 	const int off_limits = atomic_load_int(&sc->error_flags) & HW_OFF_LIMITS;
1211 
1212 	return (__predict_false(off_limits != 0));
1213 }
1214 
1215 /* Works reliably inside a synch_op or with reg_lock held. */
1216 static inline bool
1217 hw_all_ok(struct adapter *sc)
1218 {
1219 	const int not_ok = atomic_load_int(&sc->error_flags) &
1220 	    (ADAP_STOPPED | HW_OFF_LIMITS);
1221 
1222 	return (__predict_true(not_ok == 0));
1223 }
1224 
1225 static inline int
1226 mbuf_nsegs(struct mbuf *m)
1227 {
1228 	M_ASSERTPKTHDR(m);
1229 	KASSERT(m->m_pkthdr.inner_l5hlen > 0,
1230 	    ("%s: mbuf %p missing information on # of segments.", __func__, m));
1231 
1232 	return (m->m_pkthdr.inner_l5hlen);
1233 }
1234 
1235 static inline void
1236 set_mbuf_nsegs(struct mbuf *m, uint8_t nsegs)
1237 {
1238 	M_ASSERTPKTHDR(m);
1239 	m->m_pkthdr.inner_l5hlen = nsegs;
1240 }
1241 
1242 /* Internal mbuf flags stored in PH_loc.eight[1]. */
1243 #define	MC_NOMAP		0x01
1244 #define	MC_RAW_WR		0x02
1245 #define	MC_TLS			0x04
1246 
1247 static inline int
1248 mbuf_cflags(struct mbuf *m)
1249 {
1250 	M_ASSERTPKTHDR(m);
1251 	return (m->m_pkthdr.PH_loc.eight[4]);
1252 }
1253 
1254 static inline void
1255 set_mbuf_cflags(struct mbuf *m, uint8_t flags)
1256 {
1257 	M_ASSERTPKTHDR(m);
1258 	m->m_pkthdr.PH_loc.eight[4] = flags;
1259 }
1260 
1261 static inline int
1262 mbuf_len16(struct mbuf *m)
1263 {
1264 	int n;
1265 
1266 	M_ASSERTPKTHDR(m);
1267 	n = m->m_pkthdr.PH_loc.eight[0];
1268 	if (!(mbuf_cflags(m) & MC_TLS))
1269 		MPASS(n > 0 && n <= SGE_MAX_WR_LEN / 16);
1270 
1271 	return (n);
1272 }
1273 
1274 static inline void
1275 set_mbuf_len16(struct mbuf *m, uint8_t len16)
1276 {
1277 	M_ASSERTPKTHDR(m);
1278 	if (!(mbuf_cflags(m) & MC_TLS))
1279 		MPASS(len16 > 0 && len16 <= SGE_MAX_WR_LEN / 16);
1280 	m->m_pkthdr.PH_loc.eight[0] = len16;
1281 }
1282 
1283 static inline uint32_t
1284 t4_read_reg(struct adapter *sc, uint32_t reg)
1285 {
1286 	if (hw_off_limits(sc))
1287 		MPASS(curthread == sc->reset_thread);
1288 	return bus_read_4(sc->regs_res, reg);
1289 }
1290 
1291 static inline void
1292 t4_write_reg(struct adapter *sc, uint32_t reg, uint32_t val)
1293 {
1294 	if (hw_off_limits(sc))
1295 		MPASS(curthread == sc->reset_thread);
1296 	bus_write_4(sc->regs_res, reg, val);
1297 }
1298 
1299 static inline uint64_t
1300 t4_read_reg64(struct adapter *sc, uint32_t reg)
1301 {
1302 	if (hw_off_limits(sc))
1303 		MPASS(curthread == sc->reset_thread);
1304 #ifdef __LP64__
1305 	return bus_read_8(sc->regs_res, reg);
1306 #else
1307 	return (uint64_t)bus_read_4(sc->regs_res, reg) +
1308 	    ((uint64_t)bus_read_4(sc->regs_res, reg + 4) << 32);
1309 
1310 #endif
1311 }
1312 
1313 static inline void
1314 t4_write_reg64(struct adapter *sc, uint32_t reg, uint64_t val)
1315 {
1316 	if (hw_off_limits(sc))
1317 		MPASS(curthread == sc->reset_thread);
1318 #ifdef __LP64__
1319 	bus_write_8(sc->regs_res, reg, val);
1320 #else
1321 	bus_write_4(sc->regs_res, reg, val);
1322 	bus_write_4(sc->regs_res, reg + 4, val>> 32);
1323 #endif
1324 }
1325 
1326 static inline void
1327 t4_os_pci_read_cfg1(struct adapter *sc, int reg, uint8_t *val)
1328 {
1329 	if (hw_off_limits(sc))
1330 		MPASS(curthread == sc->reset_thread);
1331 	*val = pci_read_config(sc->dev, reg, 1);
1332 }
1333 
1334 static inline void
1335 t4_os_pci_write_cfg1(struct adapter *sc, int reg, uint8_t val)
1336 {
1337 	if (hw_off_limits(sc))
1338 		MPASS(curthread == sc->reset_thread);
1339 	pci_write_config(sc->dev, reg, val, 1);
1340 }
1341 
1342 static inline void
1343 t4_os_pci_read_cfg2(struct adapter *sc, int reg, uint16_t *val)
1344 {
1345 
1346 	if (hw_off_limits(sc))
1347 		MPASS(curthread == sc->reset_thread);
1348 	*val = pci_read_config(sc->dev, reg, 2);
1349 }
1350 
1351 static inline void
1352 t4_os_pci_write_cfg2(struct adapter *sc, int reg, uint16_t val)
1353 {
1354 	if (hw_off_limits(sc))
1355 		MPASS(curthread == sc->reset_thread);
1356 	pci_write_config(sc->dev, reg, val, 2);
1357 }
1358 
1359 static inline void
1360 t4_os_pci_read_cfg4(struct adapter *sc, int reg, uint32_t *val)
1361 {
1362 	if (hw_off_limits(sc))
1363 		MPASS(curthread == sc->reset_thread);
1364 	*val = pci_read_config(sc->dev, reg, 4);
1365 }
1366 
1367 static inline void
1368 t4_os_pci_write_cfg4(struct adapter *sc, int reg, uint32_t val)
1369 {
1370 	if (hw_off_limits(sc))
1371 		MPASS(curthread == sc->reset_thread);
1372 	pci_write_config(sc->dev, reg, val, 4);
1373 }
1374 
1375 static inline struct port_info *
1376 adap2pinfo(struct adapter *sc, int idx)
1377 {
1378 
1379 	return (sc->port[idx]);
1380 }
1381 
1382 static inline void
1383 t4_os_set_hw_addr(struct port_info *pi, uint8_t hw_addr[])
1384 {
1385 
1386 	bcopy(hw_addr, pi->vi[0].hw_addr, ETHER_ADDR_LEN);
1387 }
1388 
1389 static inline int
1390 tx_resume_threshold(struct sge_eq *eq)
1391 {
1392 
1393 	/* not quite the same as qsize / 4, but this will do. */
1394 	return (eq->sidx / 4);
1395 }
1396 
1397 static inline int
1398 t4_use_ldst(struct adapter *sc)
1399 {
1400 
1401 #ifdef notyet
1402 	return (sc->flags & FW_OK || !sc->use_bd);
1403 #else
1404 	return (0);
1405 #endif
1406 }
1407 
1408 static inline void
1409 CH_DUMP_MBOX(struct adapter *sc, int mbox, const int reg,
1410     const char *msg, const __be64 *const p, const bool err)
1411 {
1412 
1413 	if (!(sc->debug_flags & DF_DUMP_MBOX) && !err)
1414 		return;
1415 	if (p != NULL) {
1416 		log(err ? LOG_ERR : LOG_DEBUG,
1417 		    "%s: mbox %u %s %016llx %016llx %016llx %016llx "
1418 		    "%016llx %016llx %016llx %016llx\n",
1419 		    device_get_nameunit(sc->dev), mbox, msg,
1420 		    (long long)be64_to_cpu(p[0]), (long long)be64_to_cpu(p[1]),
1421 		    (long long)be64_to_cpu(p[2]), (long long)be64_to_cpu(p[3]),
1422 		    (long long)be64_to_cpu(p[4]), (long long)be64_to_cpu(p[5]),
1423 		    (long long)be64_to_cpu(p[6]), (long long)be64_to_cpu(p[7]));
1424 	} else {
1425 		log(err ? LOG_ERR : LOG_DEBUG,
1426 		    "%s: mbox %u %s %016llx %016llx %016llx %016llx "
1427 		    "%016llx %016llx %016llx %016llx\n",
1428 		    device_get_nameunit(sc->dev), mbox, msg,
1429 		    (long long)t4_read_reg64(sc, reg),
1430 		    (long long)t4_read_reg64(sc, reg + 8),
1431 		    (long long)t4_read_reg64(sc, reg + 16),
1432 		    (long long)t4_read_reg64(sc, reg + 24),
1433 		    (long long)t4_read_reg64(sc, reg + 32),
1434 		    (long long)t4_read_reg64(sc, reg + 40),
1435 		    (long long)t4_read_reg64(sc, reg + 48),
1436 		    (long long)t4_read_reg64(sc, reg + 56));
1437 	}
1438 }
1439 
1440 /* t4_main.c */
1441 extern int t4_ntxq;
1442 extern int t4_nrxq;
1443 extern int t4_intr_types;
1444 extern int t4_tmr_idx;
1445 extern int t4_pktc_idx;
1446 extern unsigned int t4_qsize_rxq;
1447 extern unsigned int t4_qsize_txq;
1448 extern int t4_ddp_rcvbuf_len;
1449 extern unsigned int t4_ddp_rcvbuf_cache;
1450 extern device_method_t cxgbe_methods[];
1451 
1452 int t4_os_find_pci_capability(struct adapter *, int);
1453 void t4_os_portmod_changed(struct port_info *);
1454 void t4_os_link_changed(struct port_info *);
1455 void t4_iterate(void (*)(struct adapter *, void *), void *);
1456 void t4_init_devnames(struct adapter *);
1457 void t4_add_adapter(struct adapter *);
1458 int t4_detach_common(device_t);
1459 int t4_map_bars_0_and_4(struct adapter *);
1460 int t4_map_bar_2(struct adapter *);
1461 int t4_adj_doorbells(struct adapter *);
1462 int t4_setup_intr_handlers(struct adapter *);
1463 void t4_sysctls(struct adapter *);
1464 int begin_synchronized_op(struct adapter *, struct vi_info *, int, char *);
1465 void end_synchronized_op(struct adapter *, int);
1466 void begin_vi_detach(struct adapter *, struct vi_info *);
1467 void end_vi_detach(struct adapter *, struct vi_info *);
1468 int update_mac_settings(if_t, int);
1469 int adapter_init(struct adapter *);
1470 int vi_init(struct vi_info *);
1471 void vi_sysctls(struct vi_info *);
1472 int rw_via_memwin(struct adapter *, int, uint32_t, uint32_t *, int, int);
1473 int alloc_atid(struct adapter *, void *);
1474 void *lookup_atid(struct adapter *, int);
1475 void free_atid(struct adapter *, int);
1476 void release_tid(struct adapter *, int, struct sge_wrq *);
1477 int cxgbe_media_change(if_t);
1478 void cxgbe_media_status(if_t, struct ifmediareq *);
1479 void t4_os_cim_err(struct adapter *);
1480 int suspend_adapter(struct adapter *);
1481 int resume_adapter(struct adapter *);
1482 int toe_capability(struct vi_info *, bool);
1483 
1484 #ifdef KERN_TLS
1485 /* t6_kern_tls.c */
1486 int t6_tls_tag_alloc(if_t, union if_snd_tag_alloc_params *,
1487     struct m_snd_tag **);
1488 void t6_ktls_modload(void);
1489 void t6_ktls_modunload(void);
1490 int t6_ktls_try(if_t, struct socket *, struct ktls_session *);
1491 int t6_ktls_parse_pkt(struct mbuf *);
1492 int t6_ktls_write_wr(struct sge_txq *, void *, struct mbuf *, u_int);
1493 
1494 /* t7_kern_tls.c */
1495 int t7_tls_tag_alloc(struct ifnet *, union if_snd_tag_alloc_params *,
1496     struct m_snd_tag **);
1497 void t7_ktls_modload(void);
1498 void t7_ktls_modunload(void);
1499 int t7_ktls_parse_pkt(struct mbuf *);
1500 int t7_ktls_write_wr(struct sge_txq *, void *, struct mbuf *, u_int);
1501 #endif
1502 
1503 /* t4_keyctx.c */
1504 struct auth_hash;
1505 union authctx;
1506 #ifdef KERN_TLS
1507 struct ktls_session;
1508 struct tls_key_req;
1509 struct tls_keyctx;
1510 #endif
1511 
1512 void t4_aes_getdeckey(void *, const void *, unsigned int);
1513 void t4_copy_partial_hash(int, union authctx *, void *);
1514 void t4_init_gmac_hash(const char *, int, char *);
1515 void t4_init_hmac_digest(const struct auth_hash *, u_int, const char *, int,
1516     char *);
1517 #ifdef KERN_TLS
1518 u_int t4_tls_key_info_size(const struct ktls_session *);
1519 int t4_tls_proto_ver(const struct ktls_session *);
1520 int t4_tls_cipher_mode(const struct ktls_session *);
1521 int t4_tls_auth_mode(const struct ktls_session *);
1522 int t4_tls_hmac_ctrl(const struct ktls_session *);
1523 void t4_tls_key_ctx(const struct ktls_session *, int, struct tls_keyctx *);
1524 int t4_alloc_tls_keyid(struct adapter *);
1525 void t4_free_tls_keyid(struct adapter *, int);
1526 void t4_write_tlskey_wr(const struct ktls_session *, int, int, int, int,
1527     struct tls_key_req *);
1528 #endif
1529 
1530 #ifdef DEV_NETMAP
1531 /* t4_netmap.c */
1532 struct sge_nm_rxq;
1533 void cxgbe_nm_attach(struct vi_info *);
1534 void cxgbe_nm_detach(struct vi_info *);
1535 void service_nm_rxq(struct sge_nm_rxq *);
1536 int alloc_nm_rxq(struct vi_info *, struct sge_nm_rxq *, int, int);
1537 int free_nm_rxq(struct vi_info *, struct sge_nm_rxq *);
1538 int alloc_nm_txq(struct vi_info *, struct sge_nm_txq *, int, int);
1539 int free_nm_txq(struct vi_info *, struct sge_nm_txq *);
1540 #endif
1541 
1542 /* t4_sge.c */
1543 void t4_sge_modload(void);
1544 void t4_sge_modunload(void);
1545 uint64_t t4_sge_extfree_refs(void);
1546 void t4_tweak_chip_settings(struct adapter *);
1547 int t4_verify_chip_settings(struct adapter *);
1548 void t4_init_rx_buf_info(struct adapter *);
1549 int t4_create_dma_tag(struct adapter *);
1550 void t4_sge_sysctls(struct adapter *, struct sysctl_ctx_list *,
1551     struct sysctl_oid_list *);
1552 int t4_destroy_dma_tag(struct adapter *);
1553 int alloc_ring(struct adapter *, size_t, bus_dma_tag_t *, bus_dmamap_t *,
1554     bus_addr_t *, void **);
1555 int free_ring(struct adapter *, bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
1556     void *);
1557 void free_fl_buffers(struct adapter *, struct sge_fl *);
1558 int t4_setup_adapter_queues(struct adapter *);
1559 int t4_teardown_adapter_queues(struct adapter *);
1560 int t4_setup_vi_queues(struct vi_info *);
1561 int t4_teardown_vi_queues(struct vi_info *);
1562 void t4_intr_all(void *);
1563 void t4_intr(void *);
1564 #ifdef DEV_NETMAP
1565 void t4_nm_intr(void *);
1566 void t4_vi_intr(void *);
1567 #endif
1568 void t4_intr_err(void *);
1569 void t4_intr_evt(void *);
1570 void t4_wrq_tx_locked(struct adapter *, struct sge_wrq *, struct wrqe *);
1571 void t4_update_fl_bufsize(if_t);
1572 struct mbuf *alloc_wr_mbuf(int, int);
1573 int parse_pkt(struct mbuf **, bool);
1574 void *start_wrq_wr(struct sge_wrq *, int, struct wrq_cookie *);
1575 void commit_wrq_wr(struct sge_wrq *, void *, struct wrq_cookie *);
1576 int t4_sge_set_conm_context(struct adapter *, int, int, int);
1577 void t4_register_an_handler(an_handler_t);
1578 void t4_register_fw_msg_handler(int, fw_msg_handler_t);
1579 void t4_register_cpl_handler(int, cpl_handler_t);
1580 void t4_register_shared_cpl_handler(int, cpl_handler_t, int);
1581 #ifdef RATELIMIT
1582 void send_etid_flush_wr(struct cxgbe_rate_tag *);
1583 #endif
1584 
1585 /* t4_tracer.c */
1586 struct t4_tracer;
1587 void t4_tracer_modload(void);
1588 void t4_tracer_modunload(void);
1589 void t4_tracer_port_detach(struct adapter *);
1590 int t4_get_tracer(struct adapter *, struct t4_tracer *);
1591 int t4_set_tracer(struct adapter *, struct t4_tracer *);
1592 int t4_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1593 int t5_trace_pkt(struct sge_iq *, const struct rss_header *, struct mbuf *);
1594 
1595 /* t4_sched.c */
1596 int t4_set_sched_class(struct adapter *, struct t4_sched_params *);
1597 int t4_set_sched_queue(struct adapter *, struct t4_sched_queue *);
1598 int t4_init_tx_sched(struct adapter *);
1599 int t4_free_tx_sched(struct adapter *);
1600 void t4_update_tx_sched(struct adapter *);
1601 int t4_reserve_cl_rl_kbps(struct adapter *, int, u_int, int *);
1602 void t4_release_cl_rl(struct adapter *, int, int);
1603 int sysctl_tc(SYSCTL_HANDLER_ARGS);
1604 int sysctl_tc_params(SYSCTL_HANDLER_ARGS);
1605 #ifdef RATELIMIT
1606 void t4_init_etid_table(struct adapter *);
1607 void t4_free_etid_table(struct adapter *);
1608 struct cxgbe_rate_tag *lookup_etid(struct adapter *, int);
1609 int cxgbe_rate_tag_alloc(if_t, union if_snd_tag_alloc_params *,
1610     struct m_snd_tag **);
1611 void cxgbe_rate_tag_free_locked(struct cxgbe_rate_tag *);
1612 void cxgbe_ratelimit_query(if_t, struct if_ratelimit_query_results *);
1613 #endif
1614 
1615 /* t4_filter.c */
1616 int get_filter_mode(struct adapter *, uint32_t *);
1617 int set_filter_mode(struct adapter *, uint32_t);
1618 int set_filter_mask(struct adapter *, uint32_t);
1619 int get_filter(struct adapter *, struct t4_filter *);
1620 int set_filter(struct adapter *, struct t4_filter *);
1621 int del_filter(struct adapter *, struct t4_filter *);
1622 int t4_filter_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
1623 int t4_hashfilter_ao_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
1624 int t4_hashfilter_tcb_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
1625 int t4_del_hashfilter_rpl(struct sge_iq *, const struct rss_header *, struct mbuf *);
1626 void free_hftid_hash(struct tid_info *);
1627 
1628 /* t4_tpt.c */
1629 #define T4_STAG_UNSET 0xffffffff
1630 #define	T4_WRITE_MEM_DMA_LEN						\
1631 	roundup2(sizeof(struct ulp_mem_io) + sizeof(struct ulptx_sgl), 16)
1632 #define T4_ULPTX_MIN_IO 32
1633 #define T4_MAX_INLINE_SIZE 96
1634 #define	T4_WRITE_MEM_INLINE_LEN(len)					\
1635 	roundup2(sizeof(struct ulp_mem_io) + sizeof(struct ulptx_idata) + \
1636 	    roundup((len), T4_ULPTX_MIN_IO), 16)
1637 
1638 uint32_t t4_pblpool_alloc(struct adapter *, int);
1639 void t4_pblpool_free(struct adapter *, uint32_t, int);
1640 uint32_t t4_stag_alloc(struct adapter *, int);
1641 void t4_stag_free(struct adapter *, uint32_t, int);
1642 void t4_init_tpt(struct adapter *);
1643 void t4_free_tpt(struct adapter *);
1644 void t4_write_mem_dma_wr(struct adapter *, void *, int, int, uint32_t,
1645     uint32_t, vm_paddr_t, uint64_t);
1646 void t4_write_mem_inline_wr(struct adapter *, void *, int, int, uint32_t,
1647     uint32_t, void *, uint64_t);
1648 
1649 static inline struct wrqe *
1650 alloc_wrqe(int wr_len, struct sge_wrq *wrq)
1651 {
1652 	int len = offsetof(struct wrqe, wr) + wr_len;
1653 	struct wrqe *wr;
1654 
1655 	wr = malloc(len, M_CXGBE, M_NOWAIT);
1656 	if (__predict_false(wr == NULL))
1657 		return (NULL);
1658 	wr->wr_len = wr_len;
1659 	wr->wrq = wrq;
1660 	return (wr);
1661 }
1662 
1663 static inline void *
1664 wrtod(struct wrqe *wr)
1665 {
1666 	return (&wr->wr[0]);
1667 }
1668 
1669 static inline void
1670 free_wrqe(struct wrqe *wr)
1671 {
1672 	free(wr, M_CXGBE);
1673 }
1674 
1675 static inline void
1676 t4_wrq_tx(struct adapter *sc, struct wrqe *wr)
1677 {
1678 	struct sge_wrq *wrq = wr->wrq;
1679 
1680 	TXQ_LOCK(wrq);
1681 	if (__predict_true(wrq->eq.flags & EQ_HW_ALLOCATED))
1682 		t4_wrq_tx_locked(sc, wrq, wr);
1683 	else
1684 		free(wr, M_CXGBE);
1685 	TXQ_UNLOCK(wrq);
1686 }
1687 
1688 static inline int
1689 read_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
1690     int len)
1691 {
1692 
1693 	return (rw_via_memwin(sc, idx, addr, val, len, 0));
1694 }
1695 
1696 static inline int
1697 write_via_memwin(struct adapter *sc, int idx, uint32_t addr,
1698     const uint32_t *val, int len)
1699 {
1700 
1701 	return (rw_via_memwin(sc, idx, addr, (void *)(uintptr_t)val, len, 1));
1702 }
1703 
1704 /* Number of len16 -> number of descriptors */
1705 static inline int
1706 tx_len16_to_desc(int len16)
1707 {
1708 
1709 	return (howmany(len16, EQ_ESIZE / 16));
1710 }
1711 #endif
1712