xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision 60d717baf2144cf344ec9b47d715ce837b5d46d4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
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 #include "opt_ddb.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_kern_tls.h"
35 #include "opt_ratelimit.h"
36 #include "opt_rss.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/priv.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/eventhandler.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/pciio.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pci_private.h>
52 #include <sys/firmware.h>
53 #include <sys/sbuf.h>
54 #include <sys/smp.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <net/ethernet.h>
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/if_vlan_var.h>
63 #ifdef RSS
64 #include <net/rss_config.h>
65 #endif
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #ifdef KERN_TLS
69 #include <netinet/tcp_seq.h>
70 #endif
71 #if defined(__i386__) || defined(__amd64__)
72 #include <machine/md_var.h>
73 #include <machine/cputypes.h>
74 #include <vm/vm.h>
75 #include <vm/pmap.h>
76 #endif
77 #ifdef DDB
78 #include <ddb/ddb.h>
79 #include <ddb/db_lex.h>
80 #endif
81 
82 #include "common/common.h"
83 #include "common/t4_msg.h"
84 #include "common/t4_regs.h"
85 #include "common/t4_regs_values.h"
86 #include "cudbg/cudbg.h"
87 #include "t4_clip.h"
88 #include "t4_ioctl.h"
89 #include "t4_l2t.h"
90 #include "t4_mp_ring.h"
91 #include "t4_if.h"
92 #include "t4_smt.h"
93 
94 /* T4 bus driver interface */
95 static int t4_probe(device_t);
96 static int t4_attach(device_t);
97 static int t4_detach(device_t);
98 static int t4_child_location(device_t, device_t, struct sbuf *);
99 static int t4_ready(device_t);
100 static int t4_read_port_device(device_t, int, device_t *);
101 static int t4_suspend(device_t);
102 static int t4_resume(device_t);
103 static int t4_reset_prepare(device_t, device_t);
104 static int t4_reset_post(device_t, device_t);
105 static device_method_t t4_methods[] = {
106 	DEVMETHOD(device_probe,		t4_probe),
107 	DEVMETHOD(device_attach,	t4_attach),
108 	DEVMETHOD(device_detach,	t4_detach),
109 	DEVMETHOD(device_suspend,	t4_suspend),
110 	DEVMETHOD(device_resume,	t4_resume),
111 
112 	DEVMETHOD(bus_child_location,	t4_child_location),
113 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
114 	DEVMETHOD(bus_reset_post,	t4_reset_post),
115 
116 	DEVMETHOD(t4_is_main_ready,	t4_ready),
117 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
118 
119 	DEVMETHOD_END
120 };
121 static driver_t t4_driver = {
122 	"t4nex",
123 	t4_methods,
124 	sizeof(struct adapter)
125 };
126 
127 
128 /* T4 port (cxgbe) interface */
129 static int cxgbe_probe(device_t);
130 static int cxgbe_attach(device_t);
131 static int cxgbe_detach(device_t);
132 device_method_t cxgbe_methods[] = {
133 	DEVMETHOD(device_probe,		cxgbe_probe),
134 	DEVMETHOD(device_attach,	cxgbe_attach),
135 	DEVMETHOD(device_detach,	cxgbe_detach),
136 	{ 0, 0 }
137 };
138 static driver_t cxgbe_driver = {
139 	"cxgbe",
140 	cxgbe_methods,
141 	sizeof(struct port_info)
142 };
143 
144 /* T4 VI (vcxgbe) interface */
145 static int vcxgbe_probe(device_t);
146 static int vcxgbe_attach(device_t);
147 static int vcxgbe_detach(device_t);
148 static device_method_t vcxgbe_methods[] = {
149 	DEVMETHOD(device_probe,		vcxgbe_probe),
150 	DEVMETHOD(device_attach,	vcxgbe_attach),
151 	DEVMETHOD(device_detach,	vcxgbe_detach),
152 	{ 0, 0 }
153 };
154 static driver_t vcxgbe_driver = {
155 	"vcxgbe",
156 	vcxgbe_methods,
157 	sizeof(struct vi_info)
158 };
159 
160 static d_ioctl_t t4_ioctl;
161 
162 static struct cdevsw t4_cdevsw = {
163        .d_version = D_VERSION,
164        .d_ioctl = t4_ioctl,
165        .d_name = "t4nex",
166 };
167 
168 /* T5 bus driver interface */
169 static int t5_probe(device_t);
170 static device_method_t t5_methods[] = {
171 	DEVMETHOD(device_probe,		t5_probe),
172 	DEVMETHOD(device_attach,	t4_attach),
173 	DEVMETHOD(device_detach,	t4_detach),
174 	DEVMETHOD(device_suspend,	t4_suspend),
175 	DEVMETHOD(device_resume,	t4_resume),
176 
177 	DEVMETHOD(bus_child_location,	t4_child_location),
178 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
179 	DEVMETHOD(bus_reset_post,	t4_reset_post),
180 
181 	DEVMETHOD(t4_is_main_ready,	t4_ready),
182 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
183 
184 	DEVMETHOD_END
185 };
186 static driver_t t5_driver = {
187 	"t5nex",
188 	t5_methods,
189 	sizeof(struct adapter)
190 };
191 
192 
193 /* T5 port (cxl) interface */
194 static driver_t cxl_driver = {
195 	"cxl",
196 	cxgbe_methods,
197 	sizeof(struct port_info)
198 };
199 
200 /* T5 VI (vcxl) interface */
201 static driver_t vcxl_driver = {
202 	"vcxl",
203 	vcxgbe_methods,
204 	sizeof(struct vi_info)
205 };
206 
207 /* T6 bus driver interface */
208 static int t6_probe(device_t);
209 static device_method_t t6_methods[] = {
210 	DEVMETHOD(device_probe,		t6_probe),
211 	DEVMETHOD(device_attach,	t4_attach),
212 	DEVMETHOD(device_detach,	t4_detach),
213 	DEVMETHOD(device_suspend,	t4_suspend),
214 	DEVMETHOD(device_resume,	t4_resume),
215 
216 	DEVMETHOD(bus_child_location,	t4_child_location),
217 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
218 	DEVMETHOD(bus_reset_post,	t4_reset_post),
219 
220 	DEVMETHOD(t4_is_main_ready,	t4_ready),
221 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
222 
223 	DEVMETHOD_END
224 };
225 static driver_t t6_driver = {
226 	"t6nex",
227 	t6_methods,
228 	sizeof(struct adapter)
229 };
230 
231 
232 /* T6 port (cc) interface */
233 static driver_t cc_driver = {
234 	"cc",
235 	cxgbe_methods,
236 	sizeof(struct port_info)
237 };
238 
239 /* T6 VI (vcc) interface */
240 static driver_t vcc_driver = {
241 	"vcc",
242 	vcxgbe_methods,
243 	sizeof(struct vi_info)
244 };
245 
246 /* ifnet interface */
247 static void cxgbe_init(void *);
248 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
249 static int cxgbe_transmit(if_t, struct mbuf *);
250 static void cxgbe_qflush(if_t);
251 #if defined(KERN_TLS) || defined(RATELIMIT)
252 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
253     struct m_snd_tag **);
254 #endif
255 
256 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
257 
258 /*
259  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
260  * then ADAPTER_LOCK, then t4_uld_list_lock.
261  */
262 static struct sx t4_list_lock;
263 SLIST_HEAD(, adapter) t4_list;
264 #ifdef TCP_OFFLOAD
265 static struct sx t4_uld_list_lock;
266 struct uld_info *t4_uld_list[ULD_MAX + 1];
267 #endif
268 
269 /*
270  * Tunables.  See tweak_tunables() too.
271  *
272  * Each tunable is set to a default value here if it's known at compile-time.
273  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
274  * provide a reasonable default (upto n) when the driver is loaded.
275  *
276  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
277  * T5 are under hw.cxl.
278  */
279 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
280     "cxgbe(4) parameters");
281 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
282     "cxgbe(4) T5+ parameters");
283 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
284     "cxgbe(4) TOE parameters");
285 
286 /*
287  * Number of queues for tx and rx, NIC and offload.
288  */
289 #define NTXQ 16
290 int t4_ntxq = -NTXQ;
291 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
292     "Number of TX queues per port");
293 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
294 
295 #define NRXQ 8
296 int t4_nrxq = -NRXQ;
297 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
298     "Number of RX queues per port");
299 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
300 
301 #define NTXQ_VI 1
302 static int t4_ntxq_vi = -NTXQ_VI;
303 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
304     "Number of TX queues per VI");
305 
306 #define NRXQ_VI 1
307 static int t4_nrxq_vi = -NRXQ_VI;
308 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
309     "Number of RX queues per VI");
310 
311 static int t4_rsrv_noflowq = 0;
312 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
313     0, "Reserve TX queue 0 of each VI for non-flowid packets");
314 
315 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
316 #define NOFLDTXQ 8
317 static int t4_nofldtxq = -NOFLDTXQ;
318 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
319     "Number of offload TX queues per port");
320 
321 #define NOFLDRXQ 2
322 static int t4_nofldrxq = -NOFLDRXQ;
323 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
324     "Number of offload RX queues per port");
325 
326 #define NOFLDTXQ_VI 1
327 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
328 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
329     "Number of offload TX queues per VI");
330 
331 #define NOFLDRXQ_VI 1
332 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
334     "Number of offload RX queues per VI");
335 
336 #define TMR_IDX_OFLD 1
337 int t4_tmr_idx_ofld = TMR_IDX_OFLD;
338 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
339     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
340 
341 #define PKTC_IDX_OFLD (-1)
342 int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
343 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
344     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
345 
346 /* 0 means chip/fw default, non-zero number is value in microseconds */
347 static u_long t4_toe_keepalive_idle = 0;
348 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
349     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
350 
351 /* 0 means chip/fw default, non-zero number is value in microseconds */
352 static u_long t4_toe_keepalive_interval = 0;
353 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
354     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
355 
356 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
357 static int t4_toe_keepalive_count = 0;
358 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
359     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
360 
361 /* 0 means chip/fw default, non-zero number is value in microseconds */
362 static u_long t4_toe_rexmt_min = 0;
363 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
364     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
365 
366 /* 0 means chip/fw default, non-zero number is value in microseconds */
367 static u_long t4_toe_rexmt_max = 0;
368 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
369     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
370 
371 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
372 static int t4_toe_rexmt_count = 0;
373 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
374     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
375 
376 /* -1 means chip/fw default, other values are raw backoff values to use */
377 static int t4_toe_rexmt_backoff[16] = {
378 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
379 };
380 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
381     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
382     "cxgbe(4) TOE retransmit backoff values");
383 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
384     &t4_toe_rexmt_backoff[0], 0, "");
385 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
386     &t4_toe_rexmt_backoff[1], 0, "");
387 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
388     &t4_toe_rexmt_backoff[2], 0, "");
389 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
390     &t4_toe_rexmt_backoff[3], 0, "");
391 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
392     &t4_toe_rexmt_backoff[4], 0, "");
393 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
394     &t4_toe_rexmt_backoff[5], 0, "");
395 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
396     &t4_toe_rexmt_backoff[6], 0, "");
397 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
398     &t4_toe_rexmt_backoff[7], 0, "");
399 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
400     &t4_toe_rexmt_backoff[8], 0, "");
401 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_backoff[9], 0, "");
403 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
404     &t4_toe_rexmt_backoff[10], 0, "");
405 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
406     &t4_toe_rexmt_backoff[11], 0, "");
407 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
408     &t4_toe_rexmt_backoff[12], 0, "");
409 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
410     &t4_toe_rexmt_backoff[13], 0, "");
411 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_backoff[14], 0, "");
413 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
414     &t4_toe_rexmt_backoff[15], 0, "");
415 
416 int t4_ddp_rcvbuf_len = 256 * 1024;
417 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
418     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
419 
420 unsigned int t4_ddp_rcvbuf_cache = 4;
421 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
422     &t4_ddp_rcvbuf_cache, 0,
423     "maximum number of free DDP RX buffers to cache per connection");
424 #endif
425 
426 #ifdef DEV_NETMAP
427 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
428 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
429 static int t4_native_netmap = NN_EXTRA_VI;
430 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
431     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
432 
433 #define NNMTXQ 8
434 static int t4_nnmtxq = -NNMTXQ;
435 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
436     "Number of netmap TX queues");
437 
438 #define NNMRXQ 8
439 static int t4_nnmrxq = -NNMRXQ;
440 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
441     "Number of netmap RX queues");
442 
443 #define NNMTXQ_VI 2
444 static int t4_nnmtxq_vi = -NNMTXQ_VI;
445 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
446     "Number of netmap TX queues per VI");
447 
448 #define NNMRXQ_VI 2
449 static int t4_nnmrxq_vi = -NNMRXQ_VI;
450 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
451     "Number of netmap RX queues per VI");
452 #endif
453 
454 /*
455  * Holdoff parameters for ports.
456  */
457 #define TMR_IDX 1
458 int t4_tmr_idx = TMR_IDX;
459 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
460     0, "Holdoff timer index");
461 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
462 
463 #define PKTC_IDX (-1)
464 int t4_pktc_idx = PKTC_IDX;
465 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
466     0, "Holdoff packet counter index");
467 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
468 
469 /*
470  * Size (# of entries) of each tx and rx queue.
471  */
472 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
473 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
474     "Number of descriptors in each TX queue");
475 
476 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
477 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
478     "Number of descriptors in each RX queue");
479 
480 /*
481  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
482  */
483 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
484 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
485     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
486 
487 /*
488  * Configuration file.  All the _CF names here are special.
489  */
490 #define DEFAULT_CF	"default"
491 #define BUILTIN_CF	"built-in"
492 #define FLASH_CF	"flash"
493 #define UWIRE_CF	"uwire"
494 #define FPGA_CF		"fpga"
495 static char t4_cfg_file[32] = DEFAULT_CF;
496 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
497     sizeof(t4_cfg_file), "Firmware configuration file");
498 
499 /*
500  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
501  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
502  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
503  *            mark or when signalled to do so, 0 to never emit PAUSE.
504  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
505  *                 negotiated settings will override rx_pause/tx_pause.
506  *                 Otherwise rx_pause/tx_pause are applied forcibly.
507  */
508 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
509 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
510     &t4_pause_settings, 0,
511     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
512 
513 /*
514  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
515  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
516  *  0 to disable FEC.
517  */
518 static int t4_fec = -1;
519 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
520     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
521 
522 /*
523  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
524  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
525  * driver runs as if this is set to 0.
526  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
527  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
528  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
529  *    the firmware anyway (may result in l1cfg errors with old firmwares).
530  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
531  *    means set all FEC bits that are valid for the speed.
532  */
533 static int t4_force_fec = -1;
534 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
535     "Controls the use of FORCE_FEC bit in L1 configuration.");
536 
537 /*
538  * Link autonegotiation.
539  * -1 to run with the firmware default.
540  *  0 to disable.
541  *  1 to enable.
542  */
543 static int t4_autoneg = -1;
544 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
545     "Link autonegotiation");
546 
547 /*
548  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
549  * encouraged respectively).  '-n' is the same as 'n' except the firmware
550  * version used in the checks is read from the firmware bundled with the driver.
551  */
552 static int t4_fw_install = 1;
553 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
554     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
555 
556 /*
557  * ASIC features that will be used.  Disable the ones you don't want so that the
558  * chip resources aren't wasted on features that will not be used.
559  */
560 static int t4_nbmcaps_allowed = 0;
561 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
562     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
563 
564 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
565 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
566     &t4_linkcaps_allowed, 0, "Default link capabilities");
567 
568 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
569     FW_CAPS_CONFIG_SWITCH_EGRESS;
570 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
571     &t4_switchcaps_allowed, 0, "Default switch capabilities");
572 
573 #ifdef RATELIMIT
574 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
575 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
576 #else
577 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
578 	FW_CAPS_CONFIG_NIC_HASHFILTER;
579 #endif
580 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
581     &t4_niccaps_allowed, 0, "Default NIC capabilities");
582 
583 static int t4_toecaps_allowed = -1;
584 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
585     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
586 
587 static int t4_rdmacaps_allowed = -1;
588 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
589     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
590 
591 static int t4_cryptocaps_allowed = -1;
592 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
593     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
594 
595 static int t4_iscsicaps_allowed = -1;
596 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
597     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
598 
599 static int t4_fcoecaps_allowed = 0;
600 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
601     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
602 
603 static int t5_write_combine = 0;
604 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
605     0, "Use WC instead of UC for BAR2");
606 
607 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
608 static int t4_doorbells_allowed = 0xf;
609 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
610 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
611 
612 static int t4_num_vis = 1;
613 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
614     "Number of VIs per port");
615 
616 /*
617  * PCIe Relaxed Ordering.
618  * -1: driver should figure out a good value.
619  * 0: disable RO.
620  * 1: enable RO.
621  * 2: leave RO alone.
622  */
623 static int pcie_relaxed_ordering = -1;
624 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
625     &pcie_relaxed_ordering, 0,
626     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
627 
628 static int t4_panic_on_fatal_err = 0;
629 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
630     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
631 
632 static int t4_reset_on_fatal_err = 0;
633 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
634     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
635 
636 static int t4_clock_gate_on_suspend = 0;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
638     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
639 
640 static int t4_tx_vm_wr = 0;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
642     "Use VM work requests to transmit packets.");
643 
644 /*
645  * Set to non-zero to enable the attack filter.  A packet that matches any of
646  * these conditions will get dropped on ingress:
647  * 1) IP && source address == destination address.
648  * 2) TCP/IP && source address is not a unicast address.
649  * 3) TCP/IP && destination address is not a unicast address.
650  * 4) IP && source address is loopback (127.x.y.z).
651  * 5) IP && destination address is loopback (127.x.y.z).
652  * 6) IPv6 && source address == destination address.
653  * 7) IPv6 && source address is not a unicast address.
654  * 8) IPv6 && source address is loopback (::1/128).
655  * 9) IPv6 && destination address is loopback (::1/128).
656  * 10) IPv6 && source address is unspecified (::/128).
657  * 11) IPv6 && destination address is unspecified (::/128).
658  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
659  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
660  */
661 static int t4_attack_filter = 0;
662 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
663     &t4_attack_filter, 0, "Drop suspicious traffic");
664 
665 static int t4_drop_ip_fragments = 0;
666 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
667     &t4_drop_ip_fragments, 0, "Drop IP fragments");
668 
669 static int t4_drop_pkts_with_l2_errors = 1;
670 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
671     &t4_drop_pkts_with_l2_errors, 0,
672     "Drop all frames with Layer 2 length or checksum errors");
673 
674 static int t4_drop_pkts_with_l3_errors = 0;
675 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
676     &t4_drop_pkts_with_l3_errors, 0,
677     "Drop all frames with IP version, length, or checksum errors");
678 
679 static int t4_drop_pkts_with_l4_errors = 0;
680 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
681     &t4_drop_pkts_with_l4_errors, 0,
682     "Drop all frames with Layer 4 length, checksum, or other errors");
683 
684 #ifdef TCP_OFFLOAD
685 /*
686  * TOE tunables.
687  */
688 static int t4_cop_managed_offloading = 0;
689 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
690     &t4_cop_managed_offloading, 0,
691     "COP (Connection Offload Policy) controls all TOE offload");
692 #endif
693 
694 #ifdef KERN_TLS
695 /*
696  * This enables KERN_TLS for all adapters if set.
697  */
698 static int t4_kern_tls = 0;
699 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
700     "Enable KERN_TLS mode for T6 adapters");
701 
702 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
703     "cxgbe(4) KERN_TLS parameters");
704 
705 static int t4_tls_inline_keys = 0;
706 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
707     &t4_tls_inline_keys, 0,
708     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
709     "in card memory.");
710 
711 static int t4_tls_combo_wrs = 0;
712 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
713     0, "Attempt to combine TCB field updates with TLS record work requests.");
714 #endif
715 
716 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
717 static int vi_mac_funcs[] = {
718 	FW_VI_FUNC_ETH,
719 	FW_VI_FUNC_OFLD,
720 	FW_VI_FUNC_IWARP,
721 	FW_VI_FUNC_OPENISCSI,
722 	FW_VI_FUNC_OPENFCOE,
723 	FW_VI_FUNC_FOISCSI,
724 	FW_VI_FUNC_FOFCOE,
725 };
726 
727 struct intrs_and_queues {
728 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
729 	uint16_t num_vis;	/* number of VIs for each port */
730 	uint16_t nirq;		/* Total # of vectors */
731 	uint16_t ntxq;		/* # of NIC txq's for each port */
732 	uint16_t nrxq;		/* # of NIC rxq's for each port */
733 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
734 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
735 	uint16_t nnmtxq;	/* # of netmap txq's */
736 	uint16_t nnmrxq;	/* # of netmap rxq's */
737 
738 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
739 	uint16_t ntxq_vi;	/* # of NIC txq's */
740 	uint16_t nrxq_vi;	/* # of NIC rxq's */
741 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
742 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
743 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
744 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
745 };
746 
747 static void setup_memwin(struct adapter *);
748 static void position_memwin(struct adapter *, int, uint32_t);
749 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
750 static int fwmtype_to_hwmtype(int);
751 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
752     uint32_t *);
753 static int fixup_devlog_params(struct adapter *);
754 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
755 static int contact_firmware(struct adapter *);
756 static int partition_resources(struct adapter *);
757 static int get_params__pre_init(struct adapter *);
758 static int set_params__pre_init(struct adapter *);
759 static int get_params__post_init(struct adapter *);
760 static int set_params__post_init(struct adapter *);
761 static void t4_set_desc(struct adapter *);
762 static bool fixed_ifmedia(struct port_info *);
763 static void build_medialist(struct port_info *);
764 static void init_link_config(struct port_info *);
765 static int fixup_link_config(struct port_info *);
766 static int apply_link_config(struct port_info *);
767 static int cxgbe_init_synchronized(struct vi_info *);
768 static int cxgbe_uninit_synchronized(struct vi_info *);
769 static int adapter_full_init(struct adapter *);
770 static void adapter_full_uninit(struct adapter *);
771 static int vi_full_init(struct vi_info *);
772 static void vi_full_uninit(struct vi_info *);
773 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
774 static void quiesce_txq(struct sge_txq *);
775 static void quiesce_wrq(struct sge_wrq *);
776 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
777 static void quiesce_vi(struct vi_info *);
778 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
779     driver_intr_t *, void *, char *);
780 static int t4_free_irq(struct adapter *, struct irq *);
781 static void t4_init_atid_table(struct adapter *);
782 static void t4_free_atid_table(struct adapter *);
783 static void stop_atid_allocator(struct adapter *);
784 static void restart_atid_allocator(struct adapter *);
785 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
786 static void vi_refresh_stats(struct vi_info *);
787 static void cxgbe_refresh_stats(struct vi_info *);
788 static void cxgbe_tick(void *);
789 static void vi_tick(void *);
790 static void cxgbe_sysctls(struct port_info *);
791 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
792 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
793 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
794 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
795 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
796 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
797 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
798 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
799 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
800 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
801 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
802 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
803 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
804 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
805 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
806 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
807 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
808 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
809 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
810 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
811 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
812 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
813 static int sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS);
814 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
815 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
816 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
817 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
818 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
819 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
820 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
821 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
822 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
823 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
824 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
825 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
826 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
827 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
828 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
829 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
830 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
831 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
832 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
833 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
834 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
835 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
836 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
837 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
838 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
839 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
840 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
841 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
842 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
843 #ifdef TCP_OFFLOAD
844 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
845 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
846 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
847 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
848 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
849 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
850 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
851 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
852 #endif
853 static int get_sge_context(struct adapter *, struct t4_sge_context *);
854 static int load_fw(struct adapter *, struct t4_data *);
855 static int load_cfg(struct adapter *, struct t4_data *);
856 static int load_boot(struct adapter *, struct t4_bootrom *);
857 static int load_bootcfg(struct adapter *, struct t4_data *);
858 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
859 static void free_offload_policy(struct t4_offload_policy *);
860 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
861 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
862 static int read_i2c(struct adapter *, struct t4_i2c_data *);
863 static int clear_stats(struct adapter *, u_int);
864 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
865 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
866 static inline int stop_adapter(struct adapter *);
867 static inline void set_adapter_hwstatus(struct adapter *, const bool);
868 static int stop_lld(struct adapter *);
869 static inline int restart_adapter(struct adapter *);
870 static int restart_lld(struct adapter *);
871 #ifdef TCP_OFFLOAD
872 static int toe_capability(struct vi_info *, bool);
873 static int deactivate_all_uld(struct adapter *);
874 static void stop_all_uld(struct adapter *);
875 static void restart_all_uld(struct adapter *);
876 #endif
877 #ifdef KERN_TLS
878 static int ktls_capability(struct adapter *, bool);
879 #endif
880 static int mod_event(module_t, int, void *);
881 static int notify_siblings(device_t, int);
882 static uint64_t vi_get_counter(if_t, ift_counter);
883 static uint64_t cxgbe_get_counter(if_t, ift_counter);
884 static void enable_vxlan_rx(struct adapter *);
885 static void reset_adapter_task(void *, int);
886 static void fatal_error_task(void *, int);
887 static void dump_devlog(struct adapter *);
888 static void dump_cim_regs(struct adapter *);
889 static void dump_cimla(struct adapter *);
890 
891 struct {
892 	uint16_t device;
893 	char *desc;
894 } t4_pciids[] = {
895 	{0xa000, "Chelsio Terminator 4 FPGA"},
896 	{0x4400, "Chelsio T440-dbg"},
897 	{0x4401, "Chelsio T420-CR"},
898 	{0x4402, "Chelsio T422-CR"},
899 	{0x4403, "Chelsio T440-CR"},
900 	{0x4404, "Chelsio T420-BCH"},
901 	{0x4405, "Chelsio T440-BCH"},
902 	{0x4406, "Chelsio T440-CH"},
903 	{0x4407, "Chelsio T420-SO"},
904 	{0x4408, "Chelsio T420-CX"},
905 	{0x4409, "Chelsio T420-BT"},
906 	{0x440a, "Chelsio T404-BT"},
907 	{0x440e, "Chelsio T440-LP-CR"},
908 }, t5_pciids[] = {
909 	{0xb000, "Chelsio Terminator 5 FPGA"},
910 	{0x5400, "Chelsio T580-dbg"},
911 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
912 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
913 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
914 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
915 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
916 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
917 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
918 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
919 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
920 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
921 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
922 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
923 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
924 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
925 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
926 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
927 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
928 
929 	/* Custom */
930 	{0x5483, "Custom T540-CR"},
931 	{0x5484, "Custom T540-BT"},
932 }, t6_pciids[] = {
933 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
934 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
935 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
936 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
937 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
938 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
939 	{0x6405, "Chelsio T6225-OCP-SO"},	/* 2 x 10/25G, nomem */
940 	{0x6406, "Chelsio T62100-OCP-SO"},	/* 2 x 40/50/100G, nomem */
941 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
942 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
943 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
944 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
945 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
946 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
947 	{0x6414, "Chelsio T61100-OCP-SO"},	/* 1 x 40/50/100G, nomem */
948 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
949 
950 	/* Custom */
951 	{0x6480, "Custom T6225-CR"},
952 	{0x6481, "Custom T62100-CR"},
953 	{0x6482, "Custom T6225-CR"},
954 	{0x6483, "Custom T62100-CR"},
955 	{0x6484, "Custom T64100-CR"},
956 	{0x6485, "Custom T6240-SO"},
957 	{0x6486, "Custom T6225-SO-CR"},
958 	{0x6487, "Custom T6225-CR"},
959 };
960 
961 #ifdef TCP_OFFLOAD
962 /*
963  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
964  * be exactly the same for both rxq and ofld_rxq.
965  */
966 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
967 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
968 #endif
969 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
970 
971 static int
972 t4_probe(device_t dev)
973 {
974 	int i;
975 	uint16_t v = pci_get_vendor(dev);
976 	uint16_t d = pci_get_device(dev);
977 	uint8_t f = pci_get_function(dev);
978 
979 	if (v != PCI_VENDOR_ID_CHELSIO)
980 		return (ENXIO);
981 
982 	/* Attach only to PF0 of the FPGA */
983 	if (d == 0xa000 && f != 0)
984 		return (ENXIO);
985 
986 	for (i = 0; i < nitems(t4_pciids); i++) {
987 		if (d == t4_pciids[i].device) {
988 			device_set_desc(dev, t4_pciids[i].desc);
989 			return (BUS_PROBE_DEFAULT);
990 		}
991 	}
992 
993 	return (ENXIO);
994 }
995 
996 static int
997 t5_probe(device_t dev)
998 {
999 	int i;
1000 	uint16_t v = pci_get_vendor(dev);
1001 	uint16_t d = pci_get_device(dev);
1002 	uint8_t f = pci_get_function(dev);
1003 
1004 	if (v != PCI_VENDOR_ID_CHELSIO)
1005 		return (ENXIO);
1006 
1007 	/* Attach only to PF0 of the FPGA */
1008 	if (d == 0xb000 && f != 0)
1009 		return (ENXIO);
1010 
1011 	for (i = 0; i < nitems(t5_pciids); i++) {
1012 		if (d == t5_pciids[i].device) {
1013 			device_set_desc(dev, t5_pciids[i].desc);
1014 			return (BUS_PROBE_DEFAULT);
1015 		}
1016 	}
1017 
1018 	return (ENXIO);
1019 }
1020 
1021 static int
1022 t6_probe(device_t dev)
1023 {
1024 	int i;
1025 	uint16_t v = pci_get_vendor(dev);
1026 	uint16_t d = pci_get_device(dev);
1027 
1028 	if (v != PCI_VENDOR_ID_CHELSIO)
1029 		return (ENXIO);
1030 
1031 	for (i = 0; i < nitems(t6_pciids); i++) {
1032 		if (d == t6_pciids[i].device) {
1033 			device_set_desc(dev, t6_pciids[i].desc);
1034 			return (BUS_PROBE_DEFAULT);
1035 		}
1036 	}
1037 
1038 	return (ENXIO);
1039 }
1040 
1041 static void
1042 t5_attribute_workaround(device_t dev)
1043 {
1044 	device_t root_port;
1045 	uint32_t v;
1046 
1047 	/*
1048 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1049 	 * Ordering attributes when replying to a TLP from a Root
1050 	 * Port.  As a workaround, find the parent Root Port and
1051 	 * disable No Snoop and Relaxed Ordering.  Note that this
1052 	 * affects all devices under this root port.
1053 	 */
1054 	root_port = pci_find_pcie_root_port(dev);
1055 	if (root_port == NULL) {
1056 		device_printf(dev, "Unable to find parent root port\n");
1057 		return;
1058 	}
1059 
1060 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1061 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1062 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1063 	    0)
1064 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1065 		    device_get_nameunit(root_port));
1066 }
1067 
1068 static const struct devnames devnames[] = {
1069 	{
1070 		.nexus_name = "t4nex",
1071 		.ifnet_name = "cxgbe",
1072 		.vi_ifnet_name = "vcxgbe",
1073 		.pf03_drv_name = "t4iov",
1074 		.vf_nexus_name = "t4vf",
1075 		.vf_ifnet_name = "cxgbev"
1076 	}, {
1077 		.nexus_name = "t5nex",
1078 		.ifnet_name = "cxl",
1079 		.vi_ifnet_name = "vcxl",
1080 		.pf03_drv_name = "t5iov",
1081 		.vf_nexus_name = "t5vf",
1082 		.vf_ifnet_name = "cxlv"
1083 	}, {
1084 		.nexus_name = "t6nex",
1085 		.ifnet_name = "cc",
1086 		.vi_ifnet_name = "vcc",
1087 		.pf03_drv_name = "t6iov",
1088 		.vf_nexus_name = "t6vf",
1089 		.vf_ifnet_name = "ccv"
1090 	}
1091 };
1092 
1093 void
1094 t4_init_devnames(struct adapter *sc)
1095 {
1096 	int id;
1097 
1098 	id = chip_id(sc);
1099 	if (id >= CHELSIO_T4 && id - CHELSIO_T4 < nitems(devnames))
1100 		sc->names = &devnames[id - CHELSIO_T4];
1101 	else {
1102 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1103 		sc->names = NULL;
1104 	}
1105 }
1106 
1107 static int
1108 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1109 {
1110 	const char *parent, *name;
1111 	long value;
1112 	int line, unit;
1113 
1114 	line = 0;
1115 	parent = device_get_nameunit(sc->dev);
1116 	name = sc->names->ifnet_name;
1117 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1118 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1119 		    value == pi->port_id)
1120 			return (unit);
1121 	}
1122 	return (-1);
1123 }
1124 
1125 static void
1126 t4_calibration(void *arg)
1127 {
1128 	struct adapter *sc;
1129 	struct clock_sync *cur, *nex;
1130 	uint64_t hw;
1131 	sbintime_t sbt;
1132 	int next_up;
1133 
1134 	sc = (struct adapter *)arg;
1135 
1136 	KASSERT((hw_off_limits(sc) == 0), ("hw_off_limits at t4_calibration"));
1137 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1138 	sbt = sbinuptime();
1139 
1140 	cur = &sc->cal_info[sc->cal_current];
1141 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1142 	nex = &sc->cal_info[next_up];
1143 	if (__predict_false(sc->cal_count == 0)) {
1144 		/* First time in, just get the values in */
1145 		cur->hw_cur = hw;
1146 		cur->sbt_cur = sbt;
1147 		sc->cal_count++;
1148 		goto done;
1149 	}
1150 
1151 	if (cur->hw_cur == hw) {
1152 		/* The clock is not advancing? */
1153 		sc->cal_count = 0;
1154 		atomic_store_rel_int(&cur->gen, 0);
1155 		goto done;
1156 	}
1157 
1158 	seqc_write_begin(&nex->gen);
1159 	nex->hw_prev = cur->hw_cur;
1160 	nex->sbt_prev = cur->sbt_cur;
1161 	nex->hw_cur = hw;
1162 	nex->sbt_cur = sbt;
1163 	seqc_write_end(&nex->gen);
1164 	sc->cal_current = next_up;
1165 done:
1166 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1167 	    sc, C_DIRECT_EXEC);
1168 }
1169 
1170 static void
1171 t4_calibration_start(struct adapter *sc)
1172 {
1173 	/*
1174 	 * Here if we have not done a calibration
1175 	 * then do so otherwise start the appropriate
1176 	 * timer.
1177 	 */
1178 	int i;
1179 
1180 	for (i = 0; i < CNT_CAL_INFO; i++) {
1181 		sc->cal_info[i].gen = 0;
1182 	}
1183 	sc->cal_current = 0;
1184 	sc->cal_count = 0;
1185 	sc->cal_gen = 0;
1186 	t4_calibration(sc);
1187 }
1188 
1189 static int
1190 t4_attach(device_t dev)
1191 {
1192 	struct adapter *sc;
1193 	int rc = 0, i, j, rqidx, tqidx, nports;
1194 	struct make_dev_args mda;
1195 	struct intrs_and_queues iaq;
1196 	struct sge *s;
1197 	uint32_t *buf;
1198 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1199 	int ofld_tqidx;
1200 #endif
1201 #ifdef TCP_OFFLOAD
1202 	int ofld_rqidx;
1203 #endif
1204 #ifdef DEV_NETMAP
1205 	int nm_rqidx, nm_tqidx;
1206 #endif
1207 	int num_vis;
1208 
1209 	sc = device_get_softc(dev);
1210 	sc->dev = dev;
1211 	sysctl_ctx_init(&sc->ctx);
1212 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1213 
1214 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1215 		t5_attribute_workaround(dev);
1216 	pci_enable_busmaster(dev);
1217 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1218 		uint32_t v;
1219 
1220 		pci_set_max_read_req(dev, 4096);
1221 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1222 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1223 		if (pcie_relaxed_ordering == 0 &&
1224 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1225 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1226 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1227 		} else if (pcie_relaxed_ordering == 1 &&
1228 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1229 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1230 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1231 		}
1232 	}
1233 
1234 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1235 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1236 	sc->traceq = -1;
1237 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1238 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1239 	    device_get_nameunit(dev));
1240 
1241 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1242 	    device_get_nameunit(dev));
1243 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1244 	t4_add_adapter(sc);
1245 
1246 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1247 	TAILQ_INIT(&sc->sfl);
1248 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1249 
1250 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1251 
1252 	sc->policy = NULL;
1253 	rw_init(&sc->policy_lock, "connection offload policy");
1254 
1255 	callout_init(&sc->ktls_tick, 1);
1256 
1257 	callout_init(&sc->cal_callout, 1);
1258 
1259 	refcount_init(&sc->vxlan_refcount, 0);
1260 
1261 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1262 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1263 
1264 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1265 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1266 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1267 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1268 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1269 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1270 
1271 	rc = t4_map_bars_0_and_4(sc);
1272 	if (rc != 0)
1273 		goto done; /* error message displayed already */
1274 
1275 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1276 
1277 	/* Prepare the adapter for operation. */
1278 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1279 	rc = -t4_prep_adapter(sc, buf);
1280 	free(buf, M_CXGBE);
1281 	if (rc != 0) {
1282 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1283 		goto done;
1284 	}
1285 
1286 	/*
1287 	 * This is the real PF# to which we're attaching.  Works from within PCI
1288 	 * passthrough environments too, where pci_get_function() could return a
1289 	 * different PF# depending on the passthrough configuration.  We need to
1290 	 * use the real PF# in all our communication with the firmware.
1291 	 */
1292 	j = t4_read_reg(sc, A_PL_WHOAMI);
1293 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1294 	sc->mbox = sc->pf;
1295 
1296 	t4_init_devnames(sc);
1297 	if (sc->names == NULL) {
1298 		rc = ENOTSUP;
1299 		goto done; /* error message displayed already */
1300 	}
1301 
1302 	/*
1303 	 * Do this really early, with the memory windows set up even before the
1304 	 * character device.  The userland tool's register i/o and mem read
1305 	 * will work even in "recovery mode".
1306 	 */
1307 	setup_memwin(sc);
1308 	if (t4_init_devlog_params(sc, 0) == 0)
1309 		fixup_devlog_params(sc);
1310 	make_dev_args_init(&mda);
1311 	mda.mda_devsw = &t4_cdevsw;
1312 	mda.mda_uid = UID_ROOT;
1313 	mda.mda_gid = GID_WHEEL;
1314 	mda.mda_mode = 0600;
1315 	mda.mda_si_drv1 = sc;
1316 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1317 	if (rc != 0)
1318 		device_printf(dev, "failed to create nexus char device: %d.\n",
1319 		    rc);
1320 
1321 	/* Go no further if recovery mode has been requested. */
1322 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1323 		device_printf(dev, "recovery mode.\n");
1324 		goto done;
1325 	}
1326 
1327 #if defined(__i386__)
1328 	if ((cpu_feature & CPUID_CX8) == 0) {
1329 		device_printf(dev, "64 bit atomics not available.\n");
1330 		rc = ENOTSUP;
1331 		goto done;
1332 	}
1333 #endif
1334 
1335 	/* Contact the firmware and try to become the master driver. */
1336 	rc = contact_firmware(sc);
1337 	if (rc != 0)
1338 		goto done; /* error message displayed already */
1339 	MPASS(sc->flags & FW_OK);
1340 
1341 	rc = get_params__pre_init(sc);
1342 	if (rc != 0)
1343 		goto done; /* error message displayed already */
1344 
1345 	if (sc->flags & MASTER_PF) {
1346 		rc = partition_resources(sc);
1347 		if (rc != 0)
1348 			goto done; /* error message displayed already */
1349 	}
1350 
1351 	rc = get_params__post_init(sc);
1352 	if (rc != 0)
1353 		goto done; /* error message displayed already */
1354 
1355 	rc = set_params__post_init(sc);
1356 	if (rc != 0)
1357 		goto done; /* error message displayed already */
1358 
1359 	rc = t4_map_bar_2(sc);
1360 	if (rc != 0)
1361 		goto done; /* error message displayed already */
1362 
1363 	rc = t4_adj_doorbells(sc);
1364 	if (rc != 0)
1365 		goto done; /* error message displayed already */
1366 
1367 	rc = t4_create_dma_tag(sc);
1368 	if (rc != 0)
1369 		goto done; /* error message displayed already */
1370 
1371 	/*
1372 	 * First pass over all the ports - allocate VIs and initialize some
1373 	 * basic parameters like mac address, port type, etc.
1374 	 */
1375 	for_each_port(sc, i) {
1376 		struct port_info *pi;
1377 
1378 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1379 		sc->port[i] = pi;
1380 
1381 		/* These must be set before t4_port_init */
1382 		pi->adapter = sc;
1383 		pi->port_id = i;
1384 		/*
1385 		 * XXX: vi[0] is special so we can't delay this allocation until
1386 		 * pi->nvi's final value is known.
1387 		 */
1388 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1389 		    M_ZERO | M_WAITOK);
1390 
1391 		/*
1392 		 * Allocate the "main" VI and initialize parameters
1393 		 * like mac addr.
1394 		 */
1395 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1396 		if (rc != 0) {
1397 			device_printf(dev, "unable to initialize port %d: %d\n",
1398 			    i, rc);
1399 			free(pi->vi, M_CXGBE);
1400 			free(pi, M_CXGBE);
1401 			sc->port[i] = NULL;
1402 			goto done;
1403 		}
1404 
1405 		if (is_bt(pi->port_type))
1406 			setbit(&sc->bt_map, pi->tx_chan);
1407 		else
1408 			MPASS(!isset(&sc->bt_map, pi->tx_chan));
1409 
1410 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1411 		    device_get_nameunit(dev), i);
1412 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1413 		sc->chan_map[pi->tx_chan] = i;
1414 
1415 		/*
1416 		 * The MPS counter for FCS errors doesn't work correctly on the
1417 		 * T6 so we use the MAC counter here.  Which MAC is in use
1418 		 * depends on the link settings which will be known when the
1419 		 * link comes up.
1420 		 */
1421 		if (is_t6(sc))
1422 			pi->fcs_reg = -1;
1423 		else {
1424 			pi->fcs_reg = t4_port_reg(sc, pi->tx_chan,
1425 			    A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L);
1426 		}
1427 		pi->fcs_base = 0;
1428 
1429 		/* All VIs on this port share this media. */
1430 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1431 		    cxgbe_media_status);
1432 
1433 		PORT_LOCK(pi);
1434 		init_link_config(pi);
1435 		fixup_link_config(pi);
1436 		build_medialist(pi);
1437 		if (fixed_ifmedia(pi))
1438 			pi->flags |= FIXED_IFMEDIA;
1439 		PORT_UNLOCK(pi);
1440 
1441 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1442 		    t4_ifnet_unit(sc, pi));
1443 		if (pi->dev == NULL) {
1444 			device_printf(dev,
1445 			    "failed to add device for port %d.\n", i);
1446 			rc = ENXIO;
1447 			goto done;
1448 		}
1449 		pi->vi[0].dev = pi->dev;
1450 		device_set_softc(pi->dev, pi);
1451 	}
1452 
1453 	/*
1454 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1455 	 */
1456 	nports = sc->params.nports;
1457 	rc = cfg_itype_and_nqueues(sc, &iaq);
1458 	if (rc != 0)
1459 		goto done; /* error message displayed already */
1460 
1461 	num_vis = iaq.num_vis;
1462 	sc->intr_type = iaq.intr_type;
1463 	sc->intr_count = iaq.nirq;
1464 
1465 	s = &sc->sge;
1466 	s->nrxq = nports * iaq.nrxq;
1467 	s->ntxq = nports * iaq.ntxq;
1468 	if (num_vis > 1) {
1469 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1470 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1471 	}
1472 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1473 	s->neq += nports;		/* ctrl queues: 1 per port */
1474 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1475 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1476 	if (is_offload(sc) || is_ethoffload(sc)) {
1477 		s->nofldtxq = nports * iaq.nofldtxq;
1478 		if (num_vis > 1)
1479 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1480 		s->neq += s->nofldtxq;
1481 
1482 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1483 		    M_CXGBE, M_ZERO | M_WAITOK);
1484 	}
1485 #endif
1486 #ifdef TCP_OFFLOAD
1487 	if (is_offload(sc)) {
1488 		s->nofldrxq = nports * iaq.nofldrxq;
1489 		if (num_vis > 1)
1490 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1491 		s->neq += s->nofldrxq;	/* free list */
1492 		s->niq += s->nofldrxq;
1493 
1494 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1495 		    M_CXGBE, M_ZERO | M_WAITOK);
1496 	}
1497 #endif
1498 #ifdef DEV_NETMAP
1499 	s->nnmrxq = 0;
1500 	s->nnmtxq = 0;
1501 	if (t4_native_netmap & NN_MAIN_VI) {
1502 		s->nnmrxq += nports * iaq.nnmrxq;
1503 		s->nnmtxq += nports * iaq.nnmtxq;
1504 	}
1505 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1506 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1507 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1508 	}
1509 	s->neq += s->nnmtxq + s->nnmrxq;
1510 	s->niq += s->nnmrxq;
1511 
1512 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1513 	    M_CXGBE, M_ZERO | M_WAITOK);
1514 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1515 	    M_CXGBE, M_ZERO | M_WAITOK);
1516 #endif
1517 	MPASS(s->niq <= s->iqmap_sz);
1518 	MPASS(s->neq <= s->eqmap_sz);
1519 
1520 	s->ctrlq = malloc(nports * sizeof(struct sge_wrq), M_CXGBE,
1521 	    M_ZERO | M_WAITOK);
1522 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1523 	    M_ZERO | M_WAITOK);
1524 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1525 	    M_ZERO | M_WAITOK);
1526 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1527 	    M_ZERO | M_WAITOK);
1528 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1529 	    M_ZERO | M_WAITOK);
1530 
1531 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1532 	    M_ZERO | M_WAITOK);
1533 
1534 	t4_init_l2t(sc, M_WAITOK);
1535 	t4_init_smt(sc, M_WAITOK);
1536 	t4_init_tx_sched(sc);
1537 	t4_init_atid_table(sc);
1538 #ifdef RATELIMIT
1539 	t4_init_etid_table(sc);
1540 #endif
1541 #ifdef INET6
1542 	t4_init_clip_table(sc);
1543 #endif
1544 	if (sc->vres.key.size != 0)
1545 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1546 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1547 
1548 	/*
1549 	 * Second pass over the ports.  This time we know the number of rx and
1550 	 * tx queues that each port should get.
1551 	 */
1552 	rqidx = tqidx = 0;
1553 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1554 	ofld_tqidx = 0;
1555 #endif
1556 #ifdef TCP_OFFLOAD
1557 	ofld_rqidx = 0;
1558 #endif
1559 #ifdef DEV_NETMAP
1560 	nm_rqidx = nm_tqidx = 0;
1561 #endif
1562 	for_each_port(sc, i) {
1563 		struct port_info *pi = sc->port[i];
1564 		struct vi_info *vi;
1565 
1566 		if (pi == NULL)
1567 			continue;
1568 
1569 		pi->nvi = num_vis;
1570 		for_each_vi(pi, j, vi) {
1571 			vi->pi = pi;
1572 			vi->adapter = sc;
1573 			vi->first_intr = -1;
1574 			vi->qsize_rxq = t4_qsize_rxq;
1575 			vi->qsize_txq = t4_qsize_txq;
1576 
1577 			vi->first_rxq = rqidx;
1578 			vi->first_txq = tqidx;
1579 			vi->tmr_idx = t4_tmr_idx;
1580 			vi->pktc_idx = t4_pktc_idx;
1581 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1582 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1583 
1584 			rqidx += vi->nrxq;
1585 			tqidx += vi->ntxq;
1586 
1587 			if (j == 0 && vi->ntxq > 1)
1588 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1589 			else
1590 				vi->rsrv_noflowq = 0;
1591 
1592 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1593 			vi->first_ofld_txq = ofld_tqidx;
1594 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1595 			ofld_tqidx += vi->nofldtxq;
1596 #endif
1597 #ifdef TCP_OFFLOAD
1598 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1599 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1600 			vi->first_ofld_rxq = ofld_rqidx;
1601 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1602 
1603 			ofld_rqidx += vi->nofldrxq;
1604 #endif
1605 #ifdef DEV_NETMAP
1606 			vi->first_nm_rxq = nm_rqidx;
1607 			vi->first_nm_txq = nm_tqidx;
1608 			if (j == 0) {
1609 				vi->nnmrxq = iaq.nnmrxq;
1610 				vi->nnmtxq = iaq.nnmtxq;
1611 			} else {
1612 				vi->nnmrxq = iaq.nnmrxq_vi;
1613 				vi->nnmtxq = iaq.nnmtxq_vi;
1614 			}
1615 			nm_rqidx += vi->nnmrxq;
1616 			nm_tqidx += vi->nnmtxq;
1617 #endif
1618 		}
1619 	}
1620 
1621 	rc = t4_setup_intr_handlers(sc);
1622 	if (rc != 0) {
1623 		device_printf(dev,
1624 		    "failed to setup interrupt handlers: %d\n", rc);
1625 		goto done;
1626 	}
1627 
1628 	rc = bus_generic_probe(dev);
1629 	if (rc != 0) {
1630 		device_printf(dev, "failed to probe child drivers: %d\n", rc);
1631 		goto done;
1632 	}
1633 
1634 	/*
1635 	 * Ensure thread-safe mailbox access (in debug builds).
1636 	 *
1637 	 * So far this was the only thread accessing the mailbox but various
1638 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1639 	 * will access the mailbox from different threads.
1640 	 */
1641 	sc->flags |= CHK_MBOX_ACCESS;
1642 
1643 	rc = bus_generic_attach(dev);
1644 	if (rc != 0) {
1645 		device_printf(dev,
1646 		    "failed to attach all child ports: %d\n", rc);
1647 		goto done;
1648 	}
1649 	t4_calibration_start(sc);
1650 
1651 	device_printf(dev,
1652 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1653 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1654 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1655 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1656 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1657 
1658 	t4_set_desc(sc);
1659 
1660 	notify_siblings(dev, 0);
1661 
1662 done:
1663 	if (rc != 0 && sc->cdev) {
1664 		/* cdev was created and so cxgbetool works; recover that way. */
1665 		device_printf(dev,
1666 		    "error during attach, adapter is now in recovery mode.\n");
1667 		rc = 0;
1668 	}
1669 
1670 	if (rc != 0)
1671 		t4_detach_common(dev);
1672 	else
1673 		t4_sysctls(sc);
1674 
1675 	return (rc);
1676 }
1677 
1678 static int
1679 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1680 {
1681 	struct adapter *sc;
1682 	struct port_info *pi;
1683 	int i;
1684 
1685 	sc = device_get_softc(bus);
1686 	for_each_port(sc, i) {
1687 		pi = sc->port[i];
1688 		if (pi != NULL && pi->dev == dev) {
1689 			sbuf_printf(sb, "port=%d", pi->port_id);
1690 			break;
1691 		}
1692 	}
1693 	return (0);
1694 }
1695 
1696 static int
1697 t4_ready(device_t dev)
1698 {
1699 	struct adapter *sc;
1700 
1701 	sc = device_get_softc(dev);
1702 	if (sc->flags & FW_OK)
1703 		return (0);
1704 	return (ENXIO);
1705 }
1706 
1707 static int
1708 t4_read_port_device(device_t dev, int port, device_t *child)
1709 {
1710 	struct adapter *sc;
1711 	struct port_info *pi;
1712 
1713 	sc = device_get_softc(dev);
1714 	if (port < 0 || port >= MAX_NPORTS)
1715 		return (EINVAL);
1716 	pi = sc->port[port];
1717 	if (pi == NULL || pi->dev == NULL)
1718 		return (ENXIO);
1719 	*child = pi->dev;
1720 	return (0);
1721 }
1722 
1723 static int
1724 notify_siblings(device_t dev, int detaching)
1725 {
1726 	device_t sibling;
1727 	int error, i;
1728 
1729 	error = 0;
1730 	for (i = 0; i < PCI_FUNCMAX; i++) {
1731 		if (i == pci_get_function(dev))
1732 			continue;
1733 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1734 		    pci_get_slot(dev), i);
1735 		if (sibling == NULL || !device_is_attached(sibling))
1736 			continue;
1737 		if (detaching)
1738 			error = T4_DETACH_CHILD(sibling);
1739 		else
1740 			(void)T4_ATTACH_CHILD(sibling);
1741 		if (error)
1742 			break;
1743 	}
1744 	return (error);
1745 }
1746 
1747 /*
1748  * Idempotent
1749  */
1750 static int
1751 t4_detach(device_t dev)
1752 {
1753 	int rc;
1754 
1755 	rc = notify_siblings(dev, 1);
1756 	if (rc) {
1757 		device_printf(dev,
1758 		    "failed to detach sibling devices: %d\n", rc);
1759 		return (rc);
1760 	}
1761 
1762 	return (t4_detach_common(dev));
1763 }
1764 
1765 int
1766 t4_detach_common(device_t dev)
1767 {
1768 	struct adapter *sc;
1769 	struct port_info *pi;
1770 	int i, rc;
1771 
1772 	sc = device_get_softc(dev);
1773 
1774 #ifdef TCP_OFFLOAD
1775 	rc = deactivate_all_uld(sc);
1776 	if (rc) {
1777 		device_printf(dev,
1778 		    "failed to detach upper layer drivers: %d\n", rc);
1779 		return (rc);
1780 	}
1781 #endif
1782 
1783 	if (sc->cdev) {
1784 		destroy_dev(sc->cdev);
1785 		sc->cdev = NULL;
1786 	}
1787 
1788 	sx_xlock(&t4_list_lock);
1789 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1790 	sx_xunlock(&t4_list_lock);
1791 
1792 	sc->flags &= ~CHK_MBOX_ACCESS;
1793 	if (sc->flags & FULL_INIT_DONE) {
1794 		if (!(sc->flags & IS_VF))
1795 			t4_intr_disable(sc);
1796 	}
1797 
1798 	if (device_is_attached(dev)) {
1799 		rc = bus_generic_detach(dev);
1800 		if (rc) {
1801 			device_printf(dev,
1802 			    "failed to detach child devices: %d\n", rc);
1803 			return (rc);
1804 		}
1805 	}
1806 
1807 	for (i = 0; i < sc->intr_count; i++)
1808 		t4_free_irq(sc, &sc->irq[i]);
1809 
1810 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1811 		t4_free_tx_sched(sc);
1812 
1813 	for (i = 0; i < MAX_NPORTS; i++) {
1814 		pi = sc->port[i];
1815 		if (pi) {
1816 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1817 			if (pi->dev)
1818 				device_delete_child(dev, pi->dev);
1819 
1820 			mtx_destroy(&pi->pi_lock);
1821 			free(pi->vi, M_CXGBE);
1822 			free(pi, M_CXGBE);
1823 		}
1824 	}
1825 	callout_stop(&sc->cal_callout);
1826 	callout_drain(&sc->cal_callout);
1827 	device_delete_children(dev);
1828 	sysctl_ctx_free(&sc->ctx);
1829 	adapter_full_uninit(sc);
1830 
1831 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1832 		t4_fw_bye(sc, sc->mbox);
1833 
1834 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1835 		pci_release_msi(dev);
1836 
1837 	if (sc->regs_res)
1838 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1839 		    sc->regs_res);
1840 
1841 	if (sc->udbs_res)
1842 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1843 		    sc->udbs_res);
1844 
1845 	if (sc->msix_res)
1846 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1847 		    sc->msix_res);
1848 
1849 	if (sc->l2t)
1850 		t4_free_l2t(sc);
1851 	if (sc->smt)
1852 		t4_free_smt(sc->smt);
1853 	t4_free_atid_table(sc);
1854 #ifdef RATELIMIT
1855 	t4_free_etid_table(sc);
1856 #endif
1857 	if (sc->key_map)
1858 		vmem_destroy(sc->key_map);
1859 #ifdef INET6
1860 	t4_destroy_clip_table(sc);
1861 #endif
1862 
1863 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1864 	free(sc->sge.ofld_txq, M_CXGBE);
1865 #endif
1866 #ifdef TCP_OFFLOAD
1867 	free(sc->sge.ofld_rxq, M_CXGBE);
1868 #endif
1869 #ifdef DEV_NETMAP
1870 	free(sc->sge.nm_rxq, M_CXGBE);
1871 	free(sc->sge.nm_txq, M_CXGBE);
1872 #endif
1873 	free(sc->irq, M_CXGBE);
1874 	free(sc->sge.rxq, M_CXGBE);
1875 	free(sc->sge.txq, M_CXGBE);
1876 	free(sc->sge.ctrlq, M_CXGBE);
1877 	free(sc->sge.iqmap, M_CXGBE);
1878 	free(sc->sge.eqmap, M_CXGBE);
1879 	free(sc->tids.ftid_tab, M_CXGBE);
1880 	free(sc->tids.hpftid_tab, M_CXGBE);
1881 	free_hftid_hash(&sc->tids);
1882 	free(sc->tids.tid_tab, M_CXGBE);
1883 	t4_destroy_dma_tag(sc);
1884 
1885 	callout_drain(&sc->ktls_tick);
1886 	callout_drain(&sc->sfl_callout);
1887 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1888 		mtx_destroy(&sc->tids.ftid_lock);
1889 		cv_destroy(&sc->tids.ftid_cv);
1890 	}
1891 	if (mtx_initialized(&sc->tids.atid_lock))
1892 		mtx_destroy(&sc->tids.atid_lock);
1893 	if (mtx_initialized(&sc->ifp_lock))
1894 		mtx_destroy(&sc->ifp_lock);
1895 
1896 	if (rw_initialized(&sc->policy_lock)) {
1897 		rw_destroy(&sc->policy_lock);
1898 #ifdef TCP_OFFLOAD
1899 		if (sc->policy != NULL)
1900 			free_offload_policy(sc->policy);
1901 #endif
1902 	}
1903 
1904 	for (i = 0; i < NUM_MEMWIN; i++) {
1905 		struct memwin *mw = &sc->memwin[i];
1906 
1907 		if (rw_initialized(&mw->mw_lock))
1908 			rw_destroy(&mw->mw_lock);
1909 	}
1910 
1911 	mtx_destroy(&sc->sfl_lock);
1912 	mtx_destroy(&sc->reg_lock);
1913 	mtx_destroy(&sc->sc_lock);
1914 
1915 	bzero(sc, sizeof(*sc));
1916 
1917 	return (0);
1918 }
1919 
1920 static inline int
1921 stop_adapter(struct adapter *sc)
1922 {
1923 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1924 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1925 			 __func__, curthread, sc->flags, sc->error_flags);
1926 		return (EALREADY);
1927 	}
1928 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1929 		 sc->flags, sc->error_flags);
1930 	return (t4_shutdown_adapter(sc));
1931 }
1932 
1933 static inline int
1934 restart_adapter(struct adapter *sc)
1935 {
1936 	uint32_t val;
1937 
1938 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
1939 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
1940 			 __func__, curthread, sc->flags, sc->error_flags);
1941 		return (EALREADY);
1942 	}
1943 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
1944 		 sc->flags, sc->error_flags);
1945 
1946 	MPASS(hw_off_limits(sc));
1947 	MPASS((sc->flags & FW_OK) == 0);
1948 	MPASS((sc->flags & MASTER_PF) == 0);
1949 	MPASS(sc->reset_thread == NULL);
1950 
1951 	/*
1952 	 * The adapter is supposed to be back on PCIE with its config space and
1953 	 * BARs restored to their state before reset.  Register access via
1954 	 * t4_read_reg BAR0 should just work.
1955 	 */
1956 	sc->reset_thread = curthread;
1957 	val = t4_read_reg(sc, A_PL_WHOAMI);
1958 	if (val == 0xffffffff || val == 0xeeeeeeee) {
1959 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
1960 		sc->reset_thread = NULL;
1961 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
1962 		return (ENXIO);
1963 	}
1964 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
1965 	atomic_add_int(&sc->incarnation, 1);
1966 	atomic_add_int(&sc->num_resets, 1);
1967 
1968 	return (0);
1969 }
1970 
1971 static inline void
1972 set_adapter_hwstatus(struct adapter *sc, const bool usable)
1973 {
1974 	mtx_lock(&sc->reg_lock);
1975 	if (usable) {
1976 		/* Must be marked reusable by the designated thread. */
1977 		MPASS(sc->reset_thread == curthread);
1978 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
1979 	} else {
1980 		/* Mark the adapter totally off limits. */
1981 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
1982 		sc->flags &= ~(FW_OK | MASTER_PF);
1983 		sc->reset_thread = NULL;
1984 	}
1985 	mtx_unlock(&sc->reg_lock);
1986 }
1987 
1988 static int
1989 stop_lld(struct adapter *sc)
1990 {
1991 	struct port_info *pi;
1992 	struct vi_info *vi;
1993 	if_t ifp;
1994 	struct sge_rxq *rxq;
1995 	struct sge_txq *txq;
1996 	struct sge_wrq *wrq;
1997 #ifdef TCP_OFFLOAD
1998 	struct sge_ofld_rxq *ofld_rxq;
1999 #endif
2000 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2001 	struct sge_ofld_txq *ofld_txq;
2002 #endif
2003 	int rc, i, j, k;
2004 
2005 	/*
2006 	 * XXX: Can there be a synch_op in progress that will hang because
2007 	 * hardware has been stopped?  We'll hang too and the solution will be
2008 	 * to use a version of begin_synch_op that wakes up existing synch_op
2009 	 * with errors.  Maybe stop_adapter should do this wakeup?
2010 	 *
2011 	 * I don't think any synch_op could get stranded waiting for DMA or
2012 	 * interrupt so I think we're okay here.  Remove this comment block
2013 	 * after testing.
2014 	 */
2015 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2016 	if (rc != 0)
2017 		return (ENXIO);
2018 
2019 	/* Quiesce all activity. */
2020 	for_each_port(sc, i) {
2021 		pi = sc->port[i];
2022 		pi->vxlan_tcam_entry = false;
2023 
2024 		PORT_LOCK(pi);
2025 		if (pi->up_vis > 0) {
2026 			/*
2027 			 * t4_shutdown_adapter has already shut down all the
2028 			 * PHYs but it also disables interrupts and DMA so there
2029 			 * won't be a link interrupt.  So we update the state
2030 			 * manually and inform the kernel.
2031 			 */
2032 			pi->link_cfg.link_ok = false;
2033 			t4_os_link_changed(pi);
2034 		}
2035 		PORT_UNLOCK(pi);
2036 
2037 		for_each_vi(pi, j, vi) {
2038 			vi->xact_addr_filt = -1;
2039 			mtx_lock(&vi->tick_mtx);
2040 			vi->flags |= VI_SKIP_STATS;
2041 			mtx_unlock(&vi->tick_mtx);
2042 			if (!(vi->flags & VI_INIT_DONE))
2043 				continue;
2044 
2045 			ifp = vi->ifp;
2046 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2047 				mtx_lock(&vi->tick_mtx);
2048 				callout_stop(&vi->tick);
2049 				mtx_unlock(&vi->tick_mtx);
2050 				callout_drain(&vi->tick);
2051 			}
2052 
2053 			/*
2054 			 * Note that the HW is not available.
2055 			 */
2056 			for_each_txq(vi, k, txq) {
2057 				TXQ_LOCK(txq);
2058 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2059 				TXQ_UNLOCK(txq);
2060 			}
2061 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2062 			for_each_ofld_txq(vi, k, ofld_txq) {
2063 				TXQ_LOCK(&ofld_txq->wrq);
2064 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2065 				TXQ_UNLOCK(&ofld_txq->wrq);
2066 			}
2067 #endif
2068 			for_each_rxq(vi, k, rxq) {
2069 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2070 			}
2071 #if defined(TCP_OFFLOAD)
2072 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2073 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2074 			}
2075 #endif
2076 
2077 			quiesce_vi(vi);
2078 		}
2079 
2080 		if (sc->flags & FULL_INIT_DONE) {
2081 			/* Control queue */
2082 			wrq = &sc->sge.ctrlq[i];
2083 			TXQ_LOCK(wrq);
2084 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2085 			TXQ_UNLOCK(wrq);
2086 			quiesce_wrq(wrq);
2087 		}
2088 	}
2089 	if (sc->flags & FULL_INIT_DONE) {
2090 		/* Firmware event queue */
2091 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2092 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2093 	}
2094 
2095 	/* Stop calibration */
2096 	callout_stop(&sc->cal_callout);
2097 	callout_drain(&sc->cal_callout);
2098 
2099 	if (t4_clock_gate_on_suspend) {
2100 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2101 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2102 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2103 	}
2104 
2105 	end_synchronized_op(sc, 0);
2106 
2107 	stop_atid_allocator(sc);
2108 	t4_stop_l2t(sc);
2109 
2110 	return (rc);
2111 }
2112 
2113 int
2114 suspend_adapter(struct adapter *sc)
2115 {
2116 	stop_adapter(sc);
2117 	stop_lld(sc);
2118 #ifdef TCP_OFFLOAD
2119 	stop_all_uld(sc);
2120 #endif
2121 	set_adapter_hwstatus(sc, false);
2122 
2123 	return (0);
2124 }
2125 
2126 static int
2127 t4_suspend(device_t dev)
2128 {
2129 	struct adapter *sc = device_get_softc(dev);
2130 	int rc;
2131 
2132 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2133 	rc = suspend_adapter(sc);
2134 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2135 
2136 	return (rc);
2137 }
2138 
2139 struct adapter_pre_reset_state {
2140 	u_int flags;
2141 	uint16_t nbmcaps;
2142 	uint16_t linkcaps;
2143 	uint16_t switchcaps;
2144 	uint16_t niccaps;
2145 	uint16_t toecaps;
2146 	uint16_t rdmacaps;
2147 	uint16_t cryptocaps;
2148 	uint16_t iscsicaps;
2149 	uint16_t fcoecaps;
2150 
2151 	u_int cfcsum;
2152 	char cfg_file[32];
2153 
2154 	struct adapter_params params;
2155 	struct t4_virt_res vres;
2156 	struct tid_info tids;
2157 	struct sge sge;
2158 
2159 	int rawf_base;
2160 	int nrawf;
2161 
2162 };
2163 
2164 static void
2165 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2166 {
2167 
2168 	ASSERT_SYNCHRONIZED_OP(sc);
2169 
2170 	o->flags = sc->flags;
2171 
2172 	o->nbmcaps =  sc->nbmcaps;
2173 	o->linkcaps = sc->linkcaps;
2174 	o->switchcaps = sc->switchcaps;
2175 	o->niccaps = sc->niccaps;
2176 	o->toecaps = sc->toecaps;
2177 	o->rdmacaps = sc->rdmacaps;
2178 	o->cryptocaps = sc->cryptocaps;
2179 	o->iscsicaps = sc->iscsicaps;
2180 	o->fcoecaps = sc->fcoecaps;
2181 
2182 	o->cfcsum = sc->cfcsum;
2183 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2184 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2185 
2186 	o->params = sc->params;
2187 	o->vres = sc->vres;
2188 	o->tids = sc->tids;
2189 	o->sge = sc->sge;
2190 
2191 	o->rawf_base = sc->rawf_base;
2192 	o->nrawf = sc->nrawf;
2193 }
2194 
2195 static int
2196 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2197 {
2198 	int rc = 0;
2199 
2200 	ASSERT_SYNCHRONIZED_OP(sc);
2201 
2202 	/* Capabilities */
2203 #define COMPARE_CAPS(c) do { \
2204 	if (o->c##caps != sc->c##caps) { \
2205 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2206 		    sc->c##caps); \
2207 		rc = EINVAL; \
2208 	} \
2209 } while (0)
2210 	COMPARE_CAPS(nbm);
2211 	COMPARE_CAPS(link);
2212 	COMPARE_CAPS(switch);
2213 	COMPARE_CAPS(nic);
2214 	COMPARE_CAPS(toe);
2215 	COMPARE_CAPS(rdma);
2216 	COMPARE_CAPS(crypto);
2217 	COMPARE_CAPS(iscsi);
2218 	COMPARE_CAPS(fcoe);
2219 #undef COMPARE_CAPS
2220 
2221 	/* Firmware config file */
2222 	if (o->cfcsum != sc->cfcsum) {
2223 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2224 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2225 		rc = EINVAL;
2226 	}
2227 
2228 #define COMPARE_PARAM(p, name) do { \
2229 	if (o->p != sc->p) { \
2230 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2231 		rc = EINVAL; \
2232 	} \
2233 } while (0)
2234 	COMPARE_PARAM(sge.iq_start, iq_start);
2235 	COMPARE_PARAM(sge.eq_start, eq_start);
2236 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2237 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2238 	COMPARE_PARAM(tids.nftids, nftids);
2239 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2240 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2241 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2242 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2243 	COMPARE_PARAM(tids.tid_base, tid_base);
2244 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2245 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2246 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2247 	COMPARE_PARAM(rawf_base, rawf_base);
2248 	COMPARE_PARAM(nrawf, nrawf);
2249 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2250 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2251 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2252 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2253 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2254 	COMPARE_PARAM(tids.ntids, ntids);
2255 	COMPARE_PARAM(tids.etid_base, etid_base);
2256 	COMPARE_PARAM(tids.etid_end, etid_end);
2257 	COMPARE_PARAM(tids.netids, netids);
2258 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2259 	COMPARE_PARAM(params.ethoffload, ethoffload);
2260 	COMPARE_PARAM(tids.natids, natids);
2261 	COMPARE_PARAM(tids.stid_base, stid_base);
2262 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2263 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2264 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2265 	COMPARE_PARAM(vres.stag.start, stag_start);
2266 	COMPARE_PARAM(vres.stag.size, stag_size);
2267 	COMPARE_PARAM(vres.rq.start, rq_start);
2268 	COMPARE_PARAM(vres.rq.size, rq_size);
2269 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2270 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2271 	COMPARE_PARAM(vres.qp.start, qp_start);
2272 	COMPARE_PARAM(vres.qp.size, qp_size);
2273 	COMPARE_PARAM(vres.cq.start, cq_start);
2274 	COMPARE_PARAM(vres.cq.size, cq_size);
2275 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2276 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2277 	COMPARE_PARAM(vres.srq.start, srq_start);
2278 	COMPARE_PARAM(vres.srq.size, srq_size);
2279 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2280 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2281 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2282 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2283 	COMPARE_PARAM(vres.key.start, key_start);
2284 	COMPARE_PARAM(vres.key.size, key_size);
2285 #undef COMPARE_PARAM
2286 
2287 	return (rc);
2288 }
2289 
2290 static int
2291 restart_lld(struct adapter *sc)
2292 {
2293 	struct adapter_pre_reset_state *old_state = NULL;
2294 	struct port_info *pi;
2295 	struct vi_info *vi;
2296 	if_t ifp;
2297 	struct sge_txq *txq;
2298 	int rc, i, j, k;
2299 
2300 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2301 	if (rc != 0)
2302 		return (ENXIO);
2303 
2304 	/* Restore memory window. */
2305 	setup_memwin(sc);
2306 
2307 	/* Go no further if recovery mode has been requested. */
2308 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2309 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2310 		rc = 0;
2311 		set_adapter_hwstatus(sc, true);
2312 		goto done;
2313 	}
2314 
2315 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2316 	save_caps_and_params(sc, old_state);
2317 
2318 	/* Reestablish contact with firmware and become the primary PF. */
2319 	rc = contact_firmware(sc);
2320 	if (rc != 0)
2321 		goto done; /* error message displayed already */
2322 	MPASS(sc->flags & FW_OK);
2323 
2324 	if (sc->flags & MASTER_PF) {
2325 		rc = partition_resources(sc);
2326 		if (rc != 0)
2327 			goto done; /* error message displayed already */
2328 	}
2329 
2330 	rc = get_params__post_init(sc);
2331 	if (rc != 0)
2332 		goto done; /* error message displayed already */
2333 
2334 	rc = set_params__post_init(sc);
2335 	if (rc != 0)
2336 		goto done; /* error message displayed already */
2337 
2338 	rc = compare_caps_and_params(sc, old_state);
2339 	if (rc != 0)
2340 		goto done; /* error message displayed already */
2341 
2342 	for_each_port(sc, i) {
2343 		pi = sc->port[i];
2344 		MPASS(pi != NULL);
2345 		MPASS(pi->vi != NULL);
2346 		MPASS(pi->vi[0].dev == pi->dev);
2347 
2348 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2349 		if (rc != 0) {
2350 			CH_ERR(sc,
2351 			    "failed to re-initialize port %d: %d\n", i, rc);
2352 			goto done;
2353 		}
2354 		MPASS(sc->chan_map[pi->tx_chan] == i);
2355 
2356 		PORT_LOCK(pi);
2357 		fixup_link_config(pi);
2358 		build_medialist(pi);
2359 		PORT_UNLOCK(pi);
2360 		for_each_vi(pi, j, vi) {
2361 			if (IS_MAIN_VI(vi))
2362 				continue;
2363 			rc = alloc_extra_vi(sc, pi, vi);
2364 			if (rc != 0) {
2365 				CH_ERR(vi,
2366 				    "failed to re-allocate extra VI: %d\n", rc);
2367 				goto done;
2368 			}
2369 		}
2370 	}
2371 
2372 	/*
2373 	 * Interrupts and queues are about to be enabled and other threads will
2374 	 * want to access the hardware too.  It is safe to do so.  Note that
2375 	 * this thread is still in the middle of a synchronized_op.
2376 	 */
2377 	set_adapter_hwstatus(sc, true);
2378 
2379 	if (sc->flags & FULL_INIT_DONE) {
2380 		rc = adapter_full_init(sc);
2381 		if (rc != 0) {
2382 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2383 			goto done;
2384 		}
2385 
2386 		if (sc->vxlan_refcount > 0)
2387 			enable_vxlan_rx(sc);
2388 
2389 		for_each_port(sc, i) {
2390 			pi = sc->port[i];
2391 			for_each_vi(pi, j, vi) {
2392 				mtx_lock(&vi->tick_mtx);
2393 				vi->flags &= ~VI_SKIP_STATS;
2394 				mtx_unlock(&vi->tick_mtx);
2395 				if (!(vi->flags & VI_INIT_DONE))
2396 					continue;
2397 				rc = vi_full_init(vi);
2398 				if (rc != 0) {
2399 					CH_ERR(vi, "failed to re-initialize "
2400 					    "interface: %d\n", rc);
2401 					goto done;
2402 				}
2403 
2404 				ifp = vi->ifp;
2405 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2406 					continue;
2407 				/*
2408 				 * Note that we do not setup multicast addresses
2409 				 * in the first pass.  This ensures that the
2410 				 * unicast DMACs for all VIs on all ports get an
2411 				 * MPS TCAM entry.
2412 				 */
2413 				rc = update_mac_settings(ifp, XGMAC_ALL &
2414 				    ~XGMAC_MCADDRS);
2415 				if (rc != 0) {
2416 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2417 					goto done;
2418 				}
2419 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2420 				    true);
2421 				if (rc != 0) {
2422 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2423 					goto done;
2424 				}
2425 				for_each_txq(vi, k, txq) {
2426 					TXQ_LOCK(txq);
2427 					txq->eq.flags |= EQ_ENABLED;
2428 					TXQ_UNLOCK(txq);
2429 				}
2430 				mtx_lock(&vi->tick_mtx);
2431 				callout_schedule(&vi->tick, hz);
2432 				mtx_unlock(&vi->tick_mtx);
2433 			}
2434 			PORT_LOCK(pi);
2435 			if (pi->up_vis > 0) {
2436 				t4_update_port_info(pi);
2437 				fixup_link_config(pi);
2438 				build_medialist(pi);
2439 				apply_link_config(pi);
2440 				if (pi->link_cfg.link_ok)
2441 					t4_os_link_changed(pi);
2442 			}
2443 			PORT_UNLOCK(pi);
2444 		}
2445 
2446 		/* Now reprogram the L2 multicast addresses. */
2447 		for_each_port(sc, i) {
2448 			pi = sc->port[i];
2449 			for_each_vi(pi, j, vi) {
2450 				if (!(vi->flags & VI_INIT_DONE))
2451 					continue;
2452 				ifp = vi->ifp;
2453 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2454 					continue;
2455 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2456 				if (rc != 0) {
2457 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2458 					rc = 0;	/* carry on */
2459 				}
2460 			}
2461 		}
2462 	}
2463 
2464 	/* Reset all calibration */
2465 	t4_calibration_start(sc);
2466 done:
2467 	end_synchronized_op(sc, 0);
2468 	free(old_state, M_CXGBE);
2469 
2470 	restart_atid_allocator(sc);
2471 	t4_restart_l2t(sc);
2472 
2473 	return (rc);
2474 }
2475 
2476 int
2477 resume_adapter(struct adapter *sc)
2478 {
2479 	restart_adapter(sc);
2480 	restart_lld(sc);
2481 #ifdef TCP_OFFLOAD
2482 	restart_all_uld(sc);
2483 #endif
2484 	return (0);
2485 }
2486 
2487 static int
2488 t4_resume(device_t dev)
2489 {
2490 	struct adapter *sc = device_get_softc(dev);
2491 	int rc;
2492 
2493 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2494 	rc = resume_adapter(sc);
2495 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2496 
2497 	return (rc);
2498 }
2499 
2500 static int
2501 t4_reset_prepare(device_t dev, device_t child)
2502 {
2503 	struct adapter *sc = device_get_softc(dev);
2504 
2505 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2506 	return (0);
2507 }
2508 
2509 static int
2510 t4_reset_post(device_t dev, device_t child)
2511 {
2512 	struct adapter *sc = device_get_softc(dev);
2513 
2514 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2515 	return (0);
2516 }
2517 
2518 static int
2519 reset_adapter_with_pci_bus_reset(struct adapter *sc)
2520 {
2521 	int rc;
2522 
2523 	mtx_lock(&Giant);
2524 	rc = BUS_RESET_CHILD(device_get_parent(sc->dev), sc->dev, 0);
2525 	mtx_unlock(&Giant);
2526 	return (rc);
2527 }
2528 
2529 static int
2530 reset_adapter_with_pl_rst(struct adapter *sc)
2531 {
2532 	suspend_adapter(sc);
2533 
2534 	/* This is a t4_write_reg without the hw_off_limits check. */
2535 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2536 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2537 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2538 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2539 
2540 	resume_adapter(sc);
2541 
2542 	return (0);
2543 }
2544 
2545 static inline int
2546 reset_adapter(struct adapter *sc)
2547 {
2548 	if (vm_guest == 0)
2549 		return (reset_adapter_with_pci_bus_reset(sc));
2550 	else
2551 		return (reset_adapter_with_pl_rst(sc));
2552 }
2553 
2554 static void
2555 reset_adapter_task(void *arg, int pending)
2556 {
2557 	struct adapter *sc = arg;
2558 	const int flags = sc->flags;
2559 	const int eflags = sc->error_flags;
2560 	int rc;
2561 
2562 	if (pending > 1)
2563 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2564 	rc = reset_adapter(sc);
2565 	if (rc != 0) {
2566 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2567 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2568 		       rc, flags, sc->flags, eflags, sc->error_flags);
2569 	}
2570 }
2571 
2572 static int
2573 cxgbe_probe(device_t dev)
2574 {
2575 	struct port_info *pi = device_get_softc(dev);
2576 
2577 	device_set_descf(dev, "port %d", pi->port_id);
2578 
2579 	return (BUS_PROBE_DEFAULT);
2580 }
2581 
2582 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2583     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2584     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2585     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2586 #define T4_CAP_ENABLE (T4_CAP)
2587 
2588 static void
2589 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2590 {
2591 	if_t ifp;
2592 	struct sbuf *sb;
2593 	struct sysctl_ctx_list *ctx = &vi->ctx;
2594 	struct sysctl_oid_list *children;
2595 	struct pfil_head_args pa;
2596 	struct adapter *sc = vi->adapter;
2597 
2598 	sysctl_ctx_init(ctx);
2599 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2600 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2601 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2602 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2603 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2604 #ifdef DEV_NETMAP
2605 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2606 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2607 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2608 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2609 #endif
2610 #ifdef TCP_OFFLOAD
2611 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2612 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2613 #endif
2614 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2615 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2616 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2617 #endif
2618 
2619 	vi->xact_addr_filt = -1;
2620 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2621 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2622 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2623 		vi->flags |= TX_USES_VM_WR;
2624 
2625 	/* Allocate an ifnet and set it up */
2626 	ifp = if_alloc_dev(IFT_ETHER, dev);
2627 	vi->ifp = ifp;
2628 	if_setsoftc(ifp, vi);
2629 
2630 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2631 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2632 
2633 	if_setinitfn(ifp, cxgbe_init);
2634 	if_setioctlfn(ifp, cxgbe_ioctl);
2635 	if_settransmitfn(ifp, cxgbe_transmit);
2636 	if_setqflushfn(ifp, cxgbe_qflush);
2637 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2638 		if_setgetcounterfn(ifp, vi_get_counter);
2639 	else
2640 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2641 #if defined(KERN_TLS) || defined(RATELIMIT)
2642 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2643 #endif
2644 #ifdef RATELIMIT
2645 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2646 #endif
2647 
2648 	if_setcapabilities(ifp, T4_CAP);
2649 	if_setcapenable(ifp, T4_CAP_ENABLE);
2650 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2651 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2652 	if (chip_id(sc) >= CHELSIO_T6) {
2653 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2654 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2655 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2656 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2657 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2658 	}
2659 
2660 #ifdef TCP_OFFLOAD
2661 	if (vi->nofldrxq != 0)
2662 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2663 #endif
2664 #ifdef RATELIMIT
2665 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2666 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2667 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2668 	}
2669 #endif
2670 
2671 	if_sethwtsomax(ifp, IP_MAXPACKET);
2672 	if (vi->flags & TX_USES_VM_WR)
2673 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2674 	else
2675 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2676 #ifdef RATELIMIT
2677 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2678 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2679 #endif
2680 	if_sethwtsomaxsegsize(ifp, 65536);
2681 #ifdef KERN_TLS
2682 	if (is_ktls(sc)) {
2683 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2684 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2685 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2686 	}
2687 #endif
2688 
2689 	ether_ifattach(ifp, vi->hw_addr);
2690 #ifdef DEV_NETMAP
2691 	if (vi->nnmrxq != 0)
2692 		cxgbe_nm_attach(vi);
2693 #endif
2694 	sb = sbuf_new_auto();
2695 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2696 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2697 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2698 	case IFCAP_TOE:
2699 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2700 		break;
2701 	case IFCAP_TOE | IFCAP_TXRTLMT:
2702 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2703 		break;
2704 	case IFCAP_TXRTLMT:
2705 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2706 		break;
2707 	}
2708 #endif
2709 #ifdef TCP_OFFLOAD
2710 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2711 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2712 #endif
2713 #ifdef DEV_NETMAP
2714 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2715 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2716 		    vi->nnmtxq, vi->nnmrxq);
2717 #endif
2718 	sbuf_finish(sb);
2719 	device_printf(dev, "%s\n", sbuf_data(sb));
2720 	sbuf_delete(sb);
2721 
2722 	vi_sysctls(vi);
2723 
2724 	pa.pa_version = PFIL_VERSION;
2725 	pa.pa_flags = PFIL_IN;
2726 	pa.pa_type = PFIL_TYPE_ETHERNET;
2727 	pa.pa_headname = if_name(ifp);
2728 	vi->pfil = pfil_head_register(&pa);
2729 }
2730 
2731 static int
2732 cxgbe_attach(device_t dev)
2733 {
2734 	struct port_info *pi = device_get_softc(dev);
2735 	struct adapter *sc = pi->adapter;
2736 	struct vi_info *vi;
2737 	int i;
2738 
2739 	sysctl_ctx_init(&pi->ctx);
2740 
2741 	cxgbe_vi_attach(dev, &pi->vi[0]);
2742 
2743 	for_each_vi(pi, i, vi) {
2744 		if (i == 0)
2745 			continue;
2746 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2747 		if (vi->dev == NULL) {
2748 			device_printf(dev, "failed to add VI %d\n", i);
2749 			continue;
2750 		}
2751 		device_set_softc(vi->dev, vi);
2752 	}
2753 
2754 	cxgbe_sysctls(pi);
2755 
2756 	bus_generic_attach(dev);
2757 
2758 	return (0);
2759 }
2760 
2761 static void
2762 cxgbe_vi_detach(struct vi_info *vi)
2763 {
2764 	if_t ifp = vi->ifp;
2765 
2766 	if (vi->pfil != NULL) {
2767 		pfil_head_unregister(vi->pfil);
2768 		vi->pfil = NULL;
2769 	}
2770 
2771 	ether_ifdetach(ifp);
2772 
2773 	/* Let detach proceed even if these fail. */
2774 #ifdef DEV_NETMAP
2775 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2776 		cxgbe_nm_detach(vi);
2777 #endif
2778 	cxgbe_uninit_synchronized(vi);
2779 	callout_drain(&vi->tick);
2780 	mtx_destroy(&vi->tick_mtx);
2781 	sysctl_ctx_free(&vi->ctx);
2782 	vi_full_uninit(vi);
2783 
2784 	if_free(vi->ifp);
2785 	vi->ifp = NULL;
2786 }
2787 
2788 static int
2789 cxgbe_detach(device_t dev)
2790 {
2791 	struct port_info *pi = device_get_softc(dev);
2792 	struct adapter *sc = pi->adapter;
2793 	int rc;
2794 
2795 	/* Detach the extra VIs first. */
2796 	rc = bus_generic_detach(dev);
2797 	if (rc)
2798 		return (rc);
2799 	device_delete_children(dev);
2800 
2801 	sysctl_ctx_free(&pi->ctx);
2802 	begin_vi_detach(sc, &pi->vi[0]);
2803 	if (pi->flags & HAS_TRACEQ) {
2804 		sc->traceq = -1;	/* cloner should not create ifnet */
2805 		t4_tracer_port_detach(sc);
2806 	}
2807 	cxgbe_vi_detach(&pi->vi[0]);
2808 	ifmedia_removeall(&pi->media);
2809 	end_vi_detach(sc, &pi->vi[0]);
2810 
2811 	return (0);
2812 }
2813 
2814 static void
2815 cxgbe_init(void *arg)
2816 {
2817 	struct vi_info *vi = arg;
2818 	struct adapter *sc = vi->adapter;
2819 
2820 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
2821 		return;
2822 	cxgbe_init_synchronized(vi);
2823 	end_synchronized_op(sc, 0);
2824 }
2825 
2826 static int
2827 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
2828 {
2829 	int rc = 0, mtu, flags;
2830 	struct vi_info *vi = if_getsoftc(ifp);
2831 	struct port_info *pi = vi->pi;
2832 	struct adapter *sc = pi->adapter;
2833 	struct ifreq *ifr = (struct ifreq *)data;
2834 	uint32_t mask;
2835 
2836 	switch (cmd) {
2837 	case SIOCSIFMTU:
2838 		mtu = ifr->ifr_mtu;
2839 		if (mtu < ETHERMIN || mtu > MAX_MTU)
2840 			return (EINVAL);
2841 
2842 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
2843 		if (rc)
2844 			return (rc);
2845 		if_setmtu(ifp, mtu);
2846 		if (vi->flags & VI_INIT_DONE) {
2847 			t4_update_fl_bufsize(ifp);
2848 			if (!hw_off_limits(sc) &&
2849 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2850 				rc = update_mac_settings(ifp, XGMAC_MTU);
2851 		}
2852 		end_synchronized_op(sc, 0);
2853 		break;
2854 
2855 	case SIOCSIFFLAGS:
2856 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
2857 		if (rc)
2858 			return (rc);
2859 
2860 		if (hw_off_limits(sc)) {
2861 			rc = ENXIO;
2862 			goto fail;
2863 		}
2864 
2865 		if (if_getflags(ifp) & IFF_UP) {
2866 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2867 				flags = vi->if_flags;
2868 				if ((if_getflags(ifp) ^ flags) &
2869 				    (IFF_PROMISC | IFF_ALLMULTI)) {
2870 					rc = update_mac_settings(ifp,
2871 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
2872 				}
2873 			} else {
2874 				rc = cxgbe_init_synchronized(vi);
2875 			}
2876 			vi->if_flags = if_getflags(ifp);
2877 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2878 			rc = cxgbe_uninit_synchronized(vi);
2879 		}
2880 		end_synchronized_op(sc, 0);
2881 		break;
2882 
2883 	case SIOCADDMULTI:
2884 	case SIOCDELMULTI:
2885 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
2886 		if (rc)
2887 			return (rc);
2888 		if (!hw_off_limits(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2889 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2890 		end_synchronized_op(sc, 0);
2891 		break;
2892 
2893 	case SIOCSIFCAP:
2894 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
2895 		if (rc)
2896 			return (rc);
2897 
2898 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2899 		if (mask & IFCAP_TXCSUM) {
2900 			if_togglecapenable(ifp, IFCAP_TXCSUM);
2901 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
2902 
2903 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
2904 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2905 				mask &= ~IFCAP_TSO4;
2906 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
2907 				if_printf(ifp,
2908 				    "tso4 disabled due to -txcsum.\n");
2909 			}
2910 		}
2911 		if (mask & IFCAP_TXCSUM_IPV6) {
2912 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
2913 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2914 
2915 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
2916 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2917 				mask &= ~IFCAP_TSO6;
2918 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
2919 				if_printf(ifp,
2920 				    "tso6 disabled due to -txcsum6.\n");
2921 			}
2922 		}
2923 		if (mask & IFCAP_RXCSUM)
2924 			if_togglecapenable(ifp, IFCAP_RXCSUM);
2925 		if (mask & IFCAP_RXCSUM_IPV6)
2926 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
2927 
2928 		/*
2929 		 * Note that we leave CSUM_TSO alone (it is always set).  The
2930 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
2931 		 * sending a TSO request our way, so it's sufficient to toggle
2932 		 * IFCAP_TSOx only.
2933 		 */
2934 		if (mask & IFCAP_TSO4) {
2935 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
2936 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
2937 				if_printf(ifp, "enable txcsum first.\n");
2938 				rc = EAGAIN;
2939 				goto fail;
2940 			}
2941 			if_togglecapenable(ifp, IFCAP_TSO4);
2942 		}
2943 		if (mask & IFCAP_TSO6) {
2944 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
2945 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
2946 				if_printf(ifp, "enable txcsum6 first.\n");
2947 				rc = EAGAIN;
2948 				goto fail;
2949 			}
2950 			if_togglecapenable(ifp, IFCAP_TSO6);
2951 		}
2952 		if (mask & IFCAP_LRO) {
2953 #if defined(INET) || defined(INET6)
2954 			int i;
2955 			struct sge_rxq *rxq;
2956 
2957 			if_togglecapenable(ifp, IFCAP_LRO);
2958 			for_each_rxq(vi, i, rxq) {
2959 				if (if_getcapenable(ifp) & IFCAP_LRO)
2960 					rxq->iq.flags |= IQ_LRO_ENABLED;
2961 				else
2962 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
2963 			}
2964 #endif
2965 		}
2966 #ifdef TCP_OFFLOAD
2967 		if (mask & IFCAP_TOE) {
2968 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
2969 
2970 			rc = toe_capability(vi, enable);
2971 			if (rc != 0)
2972 				goto fail;
2973 
2974 			if_togglecapenable(ifp, mask);
2975 		}
2976 #endif
2977 		if (mask & IFCAP_VLAN_HWTAGGING) {
2978 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
2979 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2980 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
2981 		}
2982 		if (mask & IFCAP_VLAN_MTU) {
2983 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
2984 
2985 			/* Need to find out how to disable auto-mtu-inflation */
2986 		}
2987 		if (mask & IFCAP_VLAN_HWTSO)
2988 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
2989 		if (mask & IFCAP_VLAN_HWCSUM)
2990 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
2991 #ifdef RATELIMIT
2992 		if (mask & IFCAP_TXRTLMT)
2993 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
2994 #endif
2995 		if (mask & IFCAP_HWRXTSTMP) {
2996 			int i;
2997 			struct sge_rxq *rxq;
2998 
2999 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3000 			for_each_rxq(vi, i, rxq) {
3001 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3002 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3003 				else
3004 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3005 			}
3006 		}
3007 		if (mask & IFCAP_MEXTPG)
3008 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3009 
3010 #ifdef KERN_TLS
3011 		if (mask & IFCAP_TXTLS) {
3012 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3013 
3014 			rc = ktls_capability(sc, enable);
3015 			if (rc != 0)
3016 				goto fail;
3017 
3018 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3019 		}
3020 #endif
3021 		if (mask & IFCAP_VXLAN_HWCSUM) {
3022 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3023 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3024 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3025 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3026 		}
3027 		if (mask & IFCAP_VXLAN_HWTSO) {
3028 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3029 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3030 			    CSUM_INNER_IP_TSO);
3031 		}
3032 
3033 #ifdef VLAN_CAPABILITIES
3034 		VLAN_CAPABILITIES(ifp);
3035 #endif
3036 fail:
3037 		end_synchronized_op(sc, 0);
3038 		break;
3039 
3040 	case SIOCSIFMEDIA:
3041 	case SIOCGIFMEDIA:
3042 	case SIOCGIFXMEDIA:
3043 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3044 		break;
3045 
3046 	case SIOCGI2C: {
3047 		struct ifi2creq i2c;
3048 
3049 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3050 		if (rc != 0)
3051 			break;
3052 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3053 			rc = EPERM;
3054 			break;
3055 		}
3056 		if (i2c.len > sizeof(i2c.data)) {
3057 			rc = EINVAL;
3058 			break;
3059 		}
3060 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3061 		if (rc)
3062 			return (rc);
3063 		if (hw_off_limits(sc))
3064 			rc = ENXIO;
3065 		else
3066 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3067 			    i2c.offset, i2c.len, &i2c.data[0]);
3068 		end_synchronized_op(sc, 0);
3069 		if (rc == 0)
3070 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3071 		break;
3072 	}
3073 
3074 	default:
3075 		rc = ether_ioctl(ifp, cmd, data);
3076 	}
3077 
3078 	return (rc);
3079 }
3080 
3081 static int
3082 cxgbe_transmit(if_t ifp, struct mbuf *m)
3083 {
3084 	struct vi_info *vi = if_getsoftc(ifp);
3085 	struct port_info *pi = vi->pi;
3086 	struct adapter *sc;
3087 	struct sge_txq *txq;
3088 	void *items[1];
3089 	int rc;
3090 
3091 	M_ASSERTPKTHDR(m);
3092 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3093 #if defined(KERN_TLS) || defined(RATELIMIT)
3094 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3095 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3096 #endif
3097 
3098 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3099 		m_freem(m);
3100 		return (ENETDOWN);
3101 	}
3102 
3103 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3104 	if (__predict_false(rc != 0)) {
3105 		if (__predict_true(rc == EINPROGRESS)) {
3106 			/* queued by parse_pkt */
3107 			MPASS(m != NULL);
3108 			return (0);
3109 		}
3110 
3111 		MPASS(m == NULL);			/* was freed already */
3112 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3113 		return (rc);
3114 	}
3115 
3116 	/* Select a txq. */
3117 	sc = vi->adapter;
3118 	txq = &sc->sge.txq[vi->first_txq];
3119 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3120 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3121 		    vi->rsrv_noflowq);
3122 
3123 	items[0] = m;
3124 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3125 	if (__predict_false(rc != 0))
3126 		m_freem(m);
3127 
3128 	return (rc);
3129 }
3130 
3131 static void
3132 cxgbe_qflush(if_t ifp)
3133 {
3134 	struct vi_info *vi = if_getsoftc(ifp);
3135 	struct sge_txq *txq;
3136 	int i;
3137 
3138 	/* queues do not exist if !VI_INIT_DONE. */
3139 	if (vi->flags & VI_INIT_DONE) {
3140 		for_each_txq(vi, i, txq) {
3141 			TXQ_LOCK(txq);
3142 			txq->eq.flags |= EQ_QFLUSH;
3143 			TXQ_UNLOCK(txq);
3144 			while (!mp_ring_is_idle(txq->r)) {
3145 				mp_ring_check_drainage(txq->r, 4096);
3146 				pause("qflush", 1);
3147 			}
3148 			TXQ_LOCK(txq);
3149 			txq->eq.flags &= ~EQ_QFLUSH;
3150 			TXQ_UNLOCK(txq);
3151 		}
3152 	}
3153 	if_qflush(ifp);
3154 }
3155 
3156 static uint64_t
3157 vi_get_counter(if_t ifp, ift_counter c)
3158 {
3159 	struct vi_info *vi = if_getsoftc(ifp);
3160 	struct fw_vi_stats_vf *s = &vi->stats;
3161 
3162 	mtx_lock(&vi->tick_mtx);
3163 	vi_refresh_stats(vi);
3164 	mtx_unlock(&vi->tick_mtx);
3165 
3166 	switch (c) {
3167 	case IFCOUNTER_IPACKETS:
3168 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3169 		    s->rx_ucast_frames);
3170 	case IFCOUNTER_IERRORS:
3171 		return (s->rx_err_frames);
3172 	case IFCOUNTER_OPACKETS:
3173 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3174 		    s->tx_ucast_frames + s->tx_offload_frames);
3175 	case IFCOUNTER_OERRORS:
3176 		return (s->tx_drop_frames);
3177 	case IFCOUNTER_IBYTES:
3178 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3179 		    s->rx_ucast_bytes);
3180 	case IFCOUNTER_OBYTES:
3181 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3182 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3183 	case IFCOUNTER_IMCASTS:
3184 		return (s->rx_mcast_frames);
3185 	case IFCOUNTER_OMCASTS:
3186 		return (s->tx_mcast_frames);
3187 	case IFCOUNTER_OQDROPS: {
3188 		uint64_t drops;
3189 
3190 		drops = 0;
3191 		if (vi->flags & VI_INIT_DONE) {
3192 			int i;
3193 			struct sge_txq *txq;
3194 
3195 			for_each_txq(vi, i, txq)
3196 				drops += counter_u64_fetch(txq->r->dropped);
3197 		}
3198 
3199 		return (drops);
3200 
3201 	}
3202 
3203 	default:
3204 		return (if_get_counter_default(ifp, c));
3205 	}
3206 }
3207 
3208 static uint64_t
3209 cxgbe_get_counter(if_t ifp, ift_counter c)
3210 {
3211 	struct vi_info *vi = if_getsoftc(ifp);
3212 	struct port_info *pi = vi->pi;
3213 	struct port_stats *s = &pi->stats;
3214 
3215 	mtx_lock(&vi->tick_mtx);
3216 	cxgbe_refresh_stats(vi);
3217 	mtx_unlock(&vi->tick_mtx);
3218 
3219 	switch (c) {
3220 	case IFCOUNTER_IPACKETS:
3221 		return (s->rx_frames);
3222 
3223 	case IFCOUNTER_IERRORS:
3224 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3225 		    s->rx_fcs_err + s->rx_len_err);
3226 
3227 	case IFCOUNTER_OPACKETS:
3228 		return (s->tx_frames);
3229 
3230 	case IFCOUNTER_OERRORS:
3231 		return (s->tx_error_frames);
3232 
3233 	case IFCOUNTER_IBYTES:
3234 		return (s->rx_octets);
3235 
3236 	case IFCOUNTER_OBYTES:
3237 		return (s->tx_octets);
3238 
3239 	case IFCOUNTER_IMCASTS:
3240 		return (s->rx_mcast_frames);
3241 
3242 	case IFCOUNTER_OMCASTS:
3243 		return (s->tx_mcast_frames);
3244 
3245 	case IFCOUNTER_IQDROPS:
3246 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3247 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3248 		    s->rx_trunc3 + pi->tnl_cong_drops);
3249 
3250 	case IFCOUNTER_OQDROPS: {
3251 		uint64_t drops;
3252 
3253 		drops = s->tx_drop;
3254 		if (vi->flags & VI_INIT_DONE) {
3255 			int i;
3256 			struct sge_txq *txq;
3257 
3258 			for_each_txq(vi, i, txq)
3259 				drops += counter_u64_fetch(txq->r->dropped);
3260 		}
3261 
3262 		return (drops);
3263 
3264 	}
3265 
3266 	default:
3267 		return (if_get_counter_default(ifp, c));
3268 	}
3269 }
3270 
3271 #if defined(KERN_TLS) || defined(RATELIMIT)
3272 static int
3273 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3274     struct m_snd_tag **pt)
3275 {
3276 	int error;
3277 
3278 	switch (params->hdr.type) {
3279 #ifdef RATELIMIT
3280 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3281 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3282 		break;
3283 #endif
3284 #ifdef KERN_TLS
3285 	case IF_SND_TAG_TYPE_TLS:
3286 	{
3287 		struct vi_info *vi = if_getsoftc(ifp);
3288 
3289 		if (is_t6(vi->pi->adapter))
3290 			error = t6_tls_tag_alloc(ifp, params, pt);
3291 		else
3292 			error = EOPNOTSUPP;
3293 		break;
3294 	}
3295 #endif
3296 	default:
3297 		error = EOPNOTSUPP;
3298 	}
3299 	return (error);
3300 }
3301 #endif
3302 
3303 /*
3304  * The kernel picks a media from the list we had provided but we still validate
3305  * the requeste.
3306  */
3307 int
3308 cxgbe_media_change(if_t ifp)
3309 {
3310 	struct vi_info *vi = if_getsoftc(ifp);
3311 	struct port_info *pi = vi->pi;
3312 	struct ifmedia *ifm = &pi->media;
3313 	struct link_config *lc = &pi->link_cfg;
3314 	struct adapter *sc = pi->adapter;
3315 	int rc;
3316 
3317 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3318 	if (rc != 0)
3319 		return (rc);
3320 	PORT_LOCK(pi);
3321 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3322 		/* ifconfig .. media autoselect */
3323 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3324 			rc = ENOTSUP; /* AN not supported by transceiver */
3325 			goto done;
3326 		}
3327 		lc->requested_aneg = AUTONEG_ENABLE;
3328 		lc->requested_speed = 0;
3329 		lc->requested_fc |= PAUSE_AUTONEG;
3330 	} else {
3331 		lc->requested_aneg = AUTONEG_DISABLE;
3332 		lc->requested_speed =
3333 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3334 		lc->requested_fc = 0;
3335 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3336 			lc->requested_fc |= PAUSE_RX;
3337 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3338 			lc->requested_fc |= PAUSE_TX;
3339 	}
3340 	if (pi->up_vis > 0 && !hw_off_limits(sc)) {
3341 		fixup_link_config(pi);
3342 		rc = apply_link_config(pi);
3343 	}
3344 done:
3345 	PORT_UNLOCK(pi);
3346 	end_synchronized_op(sc, 0);
3347 	return (rc);
3348 }
3349 
3350 /*
3351  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3352  * given speed.
3353  */
3354 static int
3355 port_mword(struct port_info *pi, uint32_t speed)
3356 {
3357 
3358 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3359 	MPASS(powerof2(speed));
3360 
3361 	switch(pi->port_type) {
3362 	case FW_PORT_TYPE_BT_SGMII:
3363 	case FW_PORT_TYPE_BT_XFI:
3364 	case FW_PORT_TYPE_BT_XAUI:
3365 		/* BaseT */
3366 		switch (speed) {
3367 		case FW_PORT_CAP32_SPEED_100M:
3368 			return (IFM_100_T);
3369 		case FW_PORT_CAP32_SPEED_1G:
3370 			return (IFM_1000_T);
3371 		case FW_PORT_CAP32_SPEED_10G:
3372 			return (IFM_10G_T);
3373 		}
3374 		break;
3375 	case FW_PORT_TYPE_KX4:
3376 		if (speed == FW_PORT_CAP32_SPEED_10G)
3377 			return (IFM_10G_KX4);
3378 		break;
3379 	case FW_PORT_TYPE_CX4:
3380 		if (speed == FW_PORT_CAP32_SPEED_10G)
3381 			return (IFM_10G_CX4);
3382 		break;
3383 	case FW_PORT_TYPE_KX:
3384 		if (speed == FW_PORT_CAP32_SPEED_1G)
3385 			return (IFM_1000_KX);
3386 		break;
3387 	case FW_PORT_TYPE_KR:
3388 	case FW_PORT_TYPE_BP_AP:
3389 	case FW_PORT_TYPE_BP4_AP:
3390 	case FW_PORT_TYPE_BP40_BA:
3391 	case FW_PORT_TYPE_KR4_100G:
3392 	case FW_PORT_TYPE_KR_SFP28:
3393 	case FW_PORT_TYPE_KR_XLAUI:
3394 		switch (speed) {
3395 		case FW_PORT_CAP32_SPEED_1G:
3396 			return (IFM_1000_KX);
3397 		case FW_PORT_CAP32_SPEED_10G:
3398 			return (IFM_10G_KR);
3399 		case FW_PORT_CAP32_SPEED_25G:
3400 			return (IFM_25G_KR);
3401 		case FW_PORT_CAP32_SPEED_40G:
3402 			return (IFM_40G_KR4);
3403 		case FW_PORT_CAP32_SPEED_50G:
3404 			return (IFM_50G_KR2);
3405 		case FW_PORT_CAP32_SPEED_100G:
3406 			return (IFM_100G_KR4);
3407 		}
3408 		break;
3409 	case FW_PORT_TYPE_FIBER_XFI:
3410 	case FW_PORT_TYPE_FIBER_XAUI:
3411 	case FW_PORT_TYPE_SFP:
3412 	case FW_PORT_TYPE_QSFP_10G:
3413 	case FW_PORT_TYPE_QSA:
3414 	case FW_PORT_TYPE_QSFP:
3415 	case FW_PORT_TYPE_CR4_QSFP:
3416 	case FW_PORT_TYPE_CR_QSFP:
3417 	case FW_PORT_TYPE_CR2_QSFP:
3418 	case FW_PORT_TYPE_SFP28:
3419 		/* Pluggable transceiver */
3420 		switch (pi->mod_type) {
3421 		case FW_PORT_MOD_TYPE_LR:
3422 			switch (speed) {
3423 			case FW_PORT_CAP32_SPEED_1G:
3424 				return (IFM_1000_LX);
3425 			case FW_PORT_CAP32_SPEED_10G:
3426 				return (IFM_10G_LR);
3427 			case FW_PORT_CAP32_SPEED_25G:
3428 				return (IFM_25G_LR);
3429 			case FW_PORT_CAP32_SPEED_40G:
3430 				return (IFM_40G_LR4);
3431 			case FW_PORT_CAP32_SPEED_50G:
3432 				return (IFM_50G_LR2);
3433 			case FW_PORT_CAP32_SPEED_100G:
3434 				return (IFM_100G_LR4);
3435 			}
3436 			break;
3437 		case FW_PORT_MOD_TYPE_SR:
3438 			switch (speed) {
3439 			case FW_PORT_CAP32_SPEED_1G:
3440 				return (IFM_1000_SX);
3441 			case FW_PORT_CAP32_SPEED_10G:
3442 				return (IFM_10G_SR);
3443 			case FW_PORT_CAP32_SPEED_25G:
3444 				return (IFM_25G_SR);
3445 			case FW_PORT_CAP32_SPEED_40G:
3446 				return (IFM_40G_SR4);
3447 			case FW_PORT_CAP32_SPEED_50G:
3448 				return (IFM_50G_SR2);
3449 			case FW_PORT_CAP32_SPEED_100G:
3450 				return (IFM_100G_SR4);
3451 			}
3452 			break;
3453 		case FW_PORT_MOD_TYPE_ER:
3454 			if (speed == FW_PORT_CAP32_SPEED_10G)
3455 				return (IFM_10G_ER);
3456 			break;
3457 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3458 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3459 			switch (speed) {
3460 			case FW_PORT_CAP32_SPEED_1G:
3461 				return (IFM_1000_CX);
3462 			case FW_PORT_CAP32_SPEED_10G:
3463 				return (IFM_10G_TWINAX);
3464 			case FW_PORT_CAP32_SPEED_25G:
3465 				return (IFM_25G_CR);
3466 			case FW_PORT_CAP32_SPEED_40G:
3467 				return (IFM_40G_CR4);
3468 			case FW_PORT_CAP32_SPEED_50G:
3469 				return (IFM_50G_CR2);
3470 			case FW_PORT_CAP32_SPEED_100G:
3471 				return (IFM_100G_CR4);
3472 			}
3473 			break;
3474 		case FW_PORT_MOD_TYPE_LRM:
3475 			if (speed == FW_PORT_CAP32_SPEED_10G)
3476 				return (IFM_10G_LRM);
3477 			break;
3478 		case FW_PORT_MOD_TYPE_NA:
3479 			MPASS(0);	/* Not pluggable? */
3480 			/* fall throough */
3481 		case FW_PORT_MOD_TYPE_ERROR:
3482 		case FW_PORT_MOD_TYPE_UNKNOWN:
3483 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3484 			break;
3485 		case FW_PORT_MOD_TYPE_NONE:
3486 			return (IFM_NONE);
3487 		}
3488 		break;
3489 	case FW_PORT_TYPE_NONE:
3490 		return (IFM_NONE);
3491 	}
3492 
3493 	return (IFM_UNKNOWN);
3494 }
3495 
3496 void
3497 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3498 {
3499 	struct vi_info *vi = if_getsoftc(ifp);
3500 	struct port_info *pi = vi->pi;
3501 	struct adapter *sc = pi->adapter;
3502 	struct link_config *lc = &pi->link_cfg;
3503 
3504 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3505 		return;
3506 	PORT_LOCK(pi);
3507 
3508 	if (pi->up_vis == 0 && !hw_off_limits(sc)) {
3509 		/*
3510 		 * If all the interfaces are administratively down the firmware
3511 		 * does not report transceiver changes.  Refresh port info here
3512 		 * so that ifconfig displays accurate ifmedia at all times.
3513 		 * This is the only reason we have a synchronized op in this
3514 		 * function.  Just PORT_LOCK would have been enough otherwise.
3515 		 */
3516 		t4_update_port_info(pi);
3517 		build_medialist(pi);
3518 	}
3519 
3520 	/* ifm_status */
3521 	ifmr->ifm_status = IFM_AVALID;
3522 	if (lc->link_ok == false)
3523 		goto done;
3524 	ifmr->ifm_status |= IFM_ACTIVE;
3525 
3526 	/* ifm_active */
3527 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3528 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3529 	if (lc->fc & PAUSE_RX)
3530 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3531 	if (lc->fc & PAUSE_TX)
3532 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3533 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3534 done:
3535 	PORT_UNLOCK(pi);
3536 	end_synchronized_op(sc, 0);
3537 }
3538 
3539 static int
3540 vcxgbe_probe(device_t dev)
3541 {
3542 	struct vi_info *vi = device_get_softc(dev);
3543 
3544 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3545 	    vi - vi->pi->vi);
3546 
3547 	return (BUS_PROBE_DEFAULT);
3548 }
3549 
3550 static int
3551 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3552 {
3553 	int func, index, rc;
3554 	uint32_t param, val;
3555 
3556 	ASSERT_SYNCHRONIZED_OP(sc);
3557 
3558 	index = vi - pi->vi;
3559 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3560 	KASSERT(index < nitems(vi_mac_funcs),
3561 	    ("%s: VI %s doesn't have a MAC func", __func__,
3562 	    device_get_nameunit(vi->dev)));
3563 	func = vi_mac_funcs[index];
3564 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->tx_chan, sc->pf, 0, 1,
3565 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3566 	if (rc < 0) {
3567 		CH_ERR(vi, "failed to allocate virtual interface %d"
3568 		    "for port %d: %d\n", index, pi->port_id, -rc);
3569 		return (-rc);
3570 	}
3571 	vi->viid = rc;
3572 
3573 	if (vi->rss_size == 1) {
3574 		/*
3575 		 * This VI didn't get a slice of the RSS table.  Reduce the
3576 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3577 		 * configuration file (nvi, rssnvi for this PF) if this is a
3578 		 * problem.
3579 		 */
3580 		device_printf(vi->dev, "RSS table not available.\n");
3581 		vi->rss_base = 0xffff;
3582 
3583 		return (0);
3584 	}
3585 
3586 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3587 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3588 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3589 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3590 	if (rc)
3591 		vi->rss_base = 0xffff;
3592 	else {
3593 		MPASS((val >> 16) == vi->rss_size);
3594 		vi->rss_base = val & 0xffff;
3595 	}
3596 
3597 	return (0);
3598 }
3599 
3600 static int
3601 vcxgbe_attach(device_t dev)
3602 {
3603 	struct vi_info *vi;
3604 	struct port_info *pi;
3605 	struct adapter *sc;
3606 	int rc;
3607 
3608 	vi = device_get_softc(dev);
3609 	pi = vi->pi;
3610 	sc = pi->adapter;
3611 
3612 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3613 	if (rc)
3614 		return (rc);
3615 	rc = alloc_extra_vi(sc, pi, vi);
3616 	end_synchronized_op(sc, 0);
3617 	if (rc)
3618 		return (rc);
3619 
3620 	cxgbe_vi_attach(dev, vi);
3621 
3622 	return (0);
3623 }
3624 
3625 static int
3626 vcxgbe_detach(device_t dev)
3627 {
3628 	struct vi_info *vi;
3629 	struct adapter *sc;
3630 
3631 	vi = device_get_softc(dev);
3632 	sc = vi->adapter;
3633 
3634 	begin_vi_detach(sc, vi);
3635 	cxgbe_vi_detach(vi);
3636 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3637 	end_vi_detach(sc, vi);
3638 
3639 	return (0);
3640 }
3641 
3642 static struct callout fatal_callout;
3643 static struct taskqueue *reset_tq;
3644 
3645 static void
3646 delayed_panic(void *arg)
3647 {
3648 	struct adapter *sc = arg;
3649 
3650 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3651 }
3652 
3653 static void
3654 fatal_error_task(void *arg, int pending)
3655 {
3656 	struct adapter *sc = arg;
3657 	int rc;
3658 
3659 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3660 		dump_cim_regs(sc);
3661 		dump_cimla(sc);
3662 		dump_devlog(sc);
3663 	}
3664 
3665 	if (t4_reset_on_fatal_err) {
3666 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3667 		rc = reset_adapter(sc);
3668 		if (rc == 0 && t4_panic_on_fatal_err) {
3669 			CH_ALERT(sc, "reset was successful, "
3670 			    "system will NOT panic.\n");
3671 			return;
3672 		}
3673 	}
3674 
3675 	if (t4_panic_on_fatal_err) {
3676 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3677 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3678 	}
3679 }
3680 
3681 void
3682 t4_fatal_err(struct adapter *sc, bool fw_error)
3683 {
3684 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3685 
3686 	stop_adapter(sc);
3687 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3688 		return;
3689 	if (fw_error) {
3690 		/*
3691 		 * We are here because of a firmware error/timeout and not
3692 		 * because of a hardware interrupt.  It is possible (although
3693 		 * not very likely) that an error interrupt was also raised but
3694 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3695 		 * main INT_CAUSE registers here to make sure we haven't missed
3696 		 * anything interesting.
3697 		 */
3698 		t4_slow_intr_handler(sc, verbose);
3699 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3700 	}
3701 	t4_report_fw_error(sc);
3702 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3703 	    device_get_nameunit(sc->dev), fw_error);
3704 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3705 }
3706 
3707 void
3708 t4_add_adapter(struct adapter *sc)
3709 {
3710 	sx_xlock(&t4_list_lock);
3711 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3712 	sx_xunlock(&t4_list_lock);
3713 }
3714 
3715 int
3716 t4_map_bars_0_and_4(struct adapter *sc)
3717 {
3718 	sc->regs_rid = PCIR_BAR(0);
3719 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3720 	    &sc->regs_rid, RF_ACTIVE);
3721 	if (sc->regs_res == NULL) {
3722 		device_printf(sc->dev, "cannot map registers.\n");
3723 		return (ENXIO);
3724 	}
3725 	sc->bt = rman_get_bustag(sc->regs_res);
3726 	sc->bh = rman_get_bushandle(sc->regs_res);
3727 	sc->mmio_len = rman_get_size(sc->regs_res);
3728 	setbit(&sc->doorbells, DOORBELL_KDB);
3729 
3730 	sc->msix_rid = PCIR_BAR(4);
3731 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3732 	    &sc->msix_rid, RF_ACTIVE);
3733 	if (sc->msix_res == NULL) {
3734 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3735 		return (ENXIO);
3736 	}
3737 
3738 	return (0);
3739 }
3740 
3741 int
3742 t4_map_bar_2(struct adapter *sc)
3743 {
3744 
3745 	/*
3746 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
3747 	 * to map it if RDMA is disabled.
3748 	 */
3749 	if (is_t4(sc) && sc->rdmacaps == 0)
3750 		return (0);
3751 
3752 	sc->udbs_rid = PCIR_BAR(2);
3753 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3754 	    &sc->udbs_rid, RF_ACTIVE);
3755 	if (sc->udbs_res == NULL) {
3756 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
3757 		return (ENXIO);
3758 	}
3759 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
3760 
3761 	if (chip_id(sc) >= CHELSIO_T5) {
3762 		setbit(&sc->doorbells, DOORBELL_UDB);
3763 #if defined(__i386__) || defined(__amd64__)
3764 		if (t5_write_combine) {
3765 			int rc, mode;
3766 
3767 			/*
3768 			 * Enable write combining on BAR2.  This is the
3769 			 * userspace doorbell BAR and is split into 128B
3770 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
3771 			 * with an egress queue.  The first 64B has the doorbell
3772 			 * and the second 64B can be used to submit a tx work
3773 			 * request with an implicit doorbell.
3774 			 */
3775 
3776 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
3777 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
3778 			if (rc == 0) {
3779 				clrbit(&sc->doorbells, DOORBELL_UDB);
3780 				setbit(&sc->doorbells, DOORBELL_WCWR);
3781 				setbit(&sc->doorbells, DOORBELL_UDBWC);
3782 			} else {
3783 				device_printf(sc->dev,
3784 				    "couldn't enable write combining: %d\n",
3785 				    rc);
3786 			}
3787 
3788 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
3789 			t4_write_reg(sc, A_SGE_STAT_CFG,
3790 			    V_STATSOURCE_T5(7) | mode);
3791 		}
3792 #endif
3793 	}
3794 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
3795 
3796 	return (0);
3797 }
3798 
3799 int
3800 t4_adj_doorbells(struct adapter *sc)
3801 {
3802 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
3803 		sc->doorbells &= t4_doorbells_allowed;
3804 		return (0);
3805 	}
3806 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
3807 	       sc->doorbells, t4_doorbells_allowed);
3808 	return (EINVAL);
3809 }
3810 
3811 struct memwin_init {
3812 	uint32_t base;
3813 	uint32_t aperture;
3814 };
3815 
3816 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
3817 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3818 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3819 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
3820 };
3821 
3822 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
3823 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
3824 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
3825 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
3826 };
3827 
3828 static void
3829 setup_memwin(struct adapter *sc)
3830 {
3831 	const struct memwin_init *mw_init;
3832 	struct memwin *mw;
3833 	int i;
3834 	uint32_t bar0;
3835 
3836 	if (is_t4(sc)) {
3837 		/*
3838 		 * Read low 32b of bar0 indirectly via the hardware backdoor
3839 		 * mechanism.  Works from within PCI passthrough environments
3840 		 * too, where rman_get_start() can return a different value.  We
3841 		 * need to program the T4 memory window decoders with the actual
3842 		 * addresses that will be coming across the PCIe link.
3843 		 */
3844 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
3845 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
3846 
3847 		mw_init = &t4_memwin[0];
3848 	} else {
3849 		/* T5+ use the relative offset inside the PCIe BAR */
3850 		bar0 = 0;
3851 
3852 		mw_init = &t5_memwin[0];
3853 	}
3854 
3855 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
3856 		if (!rw_initialized(&mw->mw_lock)) {
3857 			rw_init(&mw->mw_lock, "memory window access");
3858 			mw->mw_base = mw_init->base;
3859 			mw->mw_aperture = mw_init->aperture;
3860 			mw->mw_curpos = 0;
3861 		}
3862 		t4_write_reg(sc,
3863 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i),
3864 		    (mw->mw_base + bar0) | V_BIR(0) |
3865 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
3866 		rw_wlock(&mw->mw_lock);
3867 		position_memwin(sc, i, mw->mw_curpos);
3868 		rw_wunlock(&mw->mw_lock);
3869 	}
3870 
3871 	/* flush */
3872 	t4_read_reg(sc, PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, 2));
3873 }
3874 
3875 /*
3876  * Positions the memory window at the given address in the card's address space.
3877  * There are some alignment requirements and the actual position may be at an
3878  * address prior to the requested address.  mw->mw_curpos always has the actual
3879  * position of the window.
3880  */
3881 static void
3882 position_memwin(struct adapter *sc, int idx, uint32_t addr)
3883 {
3884 	struct memwin *mw;
3885 	uint32_t pf;
3886 	uint32_t reg;
3887 
3888 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3889 	mw = &sc->memwin[idx];
3890 	rw_assert(&mw->mw_lock, RA_WLOCKED);
3891 
3892 	if (is_t4(sc)) {
3893 		pf = 0;
3894 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
3895 	} else {
3896 		pf = V_PFNUM(sc->pf);
3897 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
3898 	}
3899 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
3900 	t4_write_reg(sc, reg, mw->mw_curpos | pf);
3901 	t4_read_reg(sc, reg);	/* flush */
3902 }
3903 
3904 int
3905 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
3906     int len, int rw)
3907 {
3908 	struct memwin *mw;
3909 	uint32_t mw_end, v;
3910 
3911 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
3912 
3913 	/* Memory can only be accessed in naturally aligned 4 byte units */
3914 	if (addr & 3 || len & 3 || len <= 0)
3915 		return (EINVAL);
3916 
3917 	mw = &sc->memwin[idx];
3918 	while (len > 0) {
3919 		rw_rlock(&mw->mw_lock);
3920 		mw_end = mw->mw_curpos + mw->mw_aperture;
3921 		if (addr >= mw_end || addr < mw->mw_curpos) {
3922 			/* Will need to reposition the window */
3923 			if (!rw_try_upgrade(&mw->mw_lock)) {
3924 				rw_runlock(&mw->mw_lock);
3925 				rw_wlock(&mw->mw_lock);
3926 			}
3927 			rw_assert(&mw->mw_lock, RA_WLOCKED);
3928 			position_memwin(sc, idx, addr);
3929 			rw_downgrade(&mw->mw_lock);
3930 			mw_end = mw->mw_curpos + mw->mw_aperture;
3931 		}
3932 		rw_assert(&mw->mw_lock, RA_RLOCKED);
3933 		while (addr < mw_end && len > 0) {
3934 			if (rw == 0) {
3935 				v = t4_read_reg(sc, mw->mw_base + addr -
3936 				    mw->mw_curpos);
3937 				*val++ = le32toh(v);
3938 			} else {
3939 				v = *val++;
3940 				t4_write_reg(sc, mw->mw_base + addr -
3941 				    mw->mw_curpos, htole32(v));
3942 			}
3943 			addr += 4;
3944 			len -= 4;
3945 		}
3946 		rw_runlock(&mw->mw_lock);
3947 	}
3948 
3949 	return (0);
3950 }
3951 
3952 CTASSERT(M_TID_COOKIE == M_COOKIE);
3953 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
3954 
3955 static void
3956 t4_init_atid_table(struct adapter *sc)
3957 {
3958 	struct tid_info *t;
3959 	int i;
3960 
3961 	t = &sc->tids;
3962 	if (t->natids == 0)
3963 		return;
3964 
3965 	MPASS(t->atid_tab == NULL);
3966 
3967 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
3968 	    M_ZERO | M_WAITOK);
3969 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
3970 	t->afree = t->atid_tab;
3971 	t->atids_in_use = 0;
3972 	t->atid_alloc_stopped = false;
3973 	for (i = 1; i < t->natids; i++)
3974 		t->atid_tab[i - 1].next = &t->atid_tab[i];
3975 	t->atid_tab[t->natids - 1].next = NULL;
3976 }
3977 
3978 static void
3979 t4_free_atid_table(struct adapter *sc)
3980 {
3981 	struct tid_info *t;
3982 
3983 	t = &sc->tids;
3984 
3985 	KASSERT(t->atids_in_use == 0,
3986 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
3987 
3988 	if (mtx_initialized(&t->atid_lock))
3989 		mtx_destroy(&t->atid_lock);
3990 	free(t->atid_tab, M_CXGBE);
3991 	t->atid_tab = NULL;
3992 }
3993 
3994 static void
3995 stop_atid_allocator(struct adapter *sc)
3996 {
3997 	struct tid_info *t = &sc->tids;
3998 
3999 	mtx_lock(&t->atid_lock);
4000 	t->atid_alloc_stopped = true;
4001 	mtx_unlock(&t->atid_lock);
4002 }
4003 
4004 static void
4005 restart_atid_allocator(struct adapter *sc)
4006 {
4007 	struct tid_info *t = &sc->tids;
4008 
4009 	mtx_lock(&t->atid_lock);
4010 	KASSERT(t->atids_in_use == 0,
4011 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4012 	t->atid_alloc_stopped = false;
4013 	mtx_unlock(&t->atid_lock);
4014 }
4015 
4016 int
4017 alloc_atid(struct adapter *sc, void *ctx)
4018 {
4019 	struct tid_info *t = &sc->tids;
4020 	int atid = -1;
4021 
4022 	mtx_lock(&t->atid_lock);
4023 	if (t->afree && !t->atid_alloc_stopped) {
4024 		union aopen_entry *p = t->afree;
4025 
4026 		atid = p - t->atid_tab;
4027 		MPASS(atid <= M_TID_TID);
4028 		t->afree = p->next;
4029 		p->data = ctx;
4030 		t->atids_in_use++;
4031 	}
4032 	mtx_unlock(&t->atid_lock);
4033 	return (atid);
4034 }
4035 
4036 void *
4037 lookup_atid(struct adapter *sc, int atid)
4038 {
4039 	struct tid_info *t = &sc->tids;
4040 
4041 	return (t->atid_tab[atid].data);
4042 }
4043 
4044 void
4045 free_atid(struct adapter *sc, int atid)
4046 {
4047 	struct tid_info *t = &sc->tids;
4048 	union aopen_entry *p = &t->atid_tab[atid];
4049 
4050 	mtx_lock(&t->atid_lock);
4051 	p->next = t->afree;
4052 	t->afree = p;
4053 	t->atids_in_use--;
4054 	mtx_unlock(&t->atid_lock);
4055 }
4056 
4057 static void
4058 queue_tid_release(struct adapter *sc, int tid)
4059 {
4060 
4061 	CXGBE_UNIMPLEMENTED("deferred tid release");
4062 }
4063 
4064 void
4065 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4066 {
4067 	struct wrqe *wr;
4068 	struct cpl_tid_release *req;
4069 
4070 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4071 	if (wr == NULL) {
4072 		queue_tid_release(sc, tid);	/* defer */
4073 		return;
4074 	}
4075 	req = wrtod(wr);
4076 
4077 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4078 
4079 	t4_wrq_tx(sc, wr);
4080 }
4081 
4082 static int
4083 t4_range_cmp(const void *a, const void *b)
4084 {
4085 	return ((const struct t4_range *)a)->start -
4086 	       ((const struct t4_range *)b)->start;
4087 }
4088 
4089 /*
4090  * Verify that the memory range specified by the addr/len pair is valid within
4091  * the card's address space.
4092  */
4093 static int
4094 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4095 {
4096 	struct t4_range mem_ranges[4], *r, *next;
4097 	uint32_t em, addr_len;
4098 	int i, n, remaining;
4099 
4100 	/* Memory can only be accessed in naturally aligned 4 byte units */
4101 	if (addr & 3 || len & 3 || len == 0)
4102 		return (EINVAL);
4103 
4104 	/* Enabled memories */
4105 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4106 
4107 	r = &mem_ranges[0];
4108 	n = 0;
4109 	bzero(r, sizeof(mem_ranges));
4110 	if (em & F_EDRAM0_ENABLE) {
4111 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4112 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4113 		if (r->size > 0) {
4114 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4115 			if (addr >= r->start &&
4116 			    addr + len <= r->start + r->size)
4117 				return (0);
4118 			r++;
4119 			n++;
4120 		}
4121 	}
4122 	if (em & F_EDRAM1_ENABLE) {
4123 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4124 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4125 		if (r->size > 0) {
4126 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4127 			if (addr >= r->start &&
4128 			    addr + len <= r->start + r->size)
4129 				return (0);
4130 			r++;
4131 			n++;
4132 		}
4133 	}
4134 	if (em & F_EXT_MEM_ENABLE) {
4135 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4136 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4137 		if (r->size > 0) {
4138 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4139 			if (addr >= r->start &&
4140 			    addr + len <= r->start + r->size)
4141 				return (0);
4142 			r++;
4143 			n++;
4144 		}
4145 	}
4146 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4147 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4148 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4149 		if (r->size > 0) {
4150 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4151 			if (addr >= r->start &&
4152 			    addr + len <= r->start + r->size)
4153 				return (0);
4154 			r++;
4155 			n++;
4156 		}
4157 	}
4158 	MPASS(n <= nitems(mem_ranges));
4159 
4160 	if (n > 1) {
4161 		/* Sort and merge the ranges. */
4162 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4163 
4164 		/* Start from index 0 and examine the next n - 1 entries. */
4165 		r = &mem_ranges[0];
4166 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4167 
4168 			MPASS(r->size > 0);	/* r is a valid entry. */
4169 			next = r + 1;
4170 			MPASS(next->size > 0);	/* and so is the next one. */
4171 
4172 			while (r->start + r->size >= next->start) {
4173 				/* Merge the next one into the current entry. */
4174 				r->size = max(r->start + r->size,
4175 				    next->start + next->size) - r->start;
4176 				n--;	/* One fewer entry in total. */
4177 				if (--remaining == 0)
4178 					goto done;	/* short circuit */
4179 				next++;
4180 			}
4181 			if (next != r + 1) {
4182 				/*
4183 				 * Some entries were merged into r and next
4184 				 * points to the first valid entry that couldn't
4185 				 * be merged.
4186 				 */
4187 				MPASS(next->size > 0);	/* must be valid */
4188 				memcpy(r + 1, next, remaining * sizeof(*r));
4189 #ifdef INVARIANTS
4190 				/*
4191 				 * This so that the foo->size assertion in the
4192 				 * next iteration of the loop do the right
4193 				 * thing for entries that were pulled up and are
4194 				 * no longer valid.
4195 				 */
4196 				MPASS(n < nitems(mem_ranges));
4197 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4198 				    sizeof(struct t4_range));
4199 #endif
4200 			}
4201 		}
4202 done:
4203 		/* Done merging the ranges. */
4204 		MPASS(n > 0);
4205 		r = &mem_ranges[0];
4206 		for (i = 0; i < n; i++, r++) {
4207 			if (addr >= r->start &&
4208 			    addr + len <= r->start + r->size)
4209 				return (0);
4210 		}
4211 	}
4212 
4213 	return (EFAULT);
4214 }
4215 
4216 static int
4217 fwmtype_to_hwmtype(int mtype)
4218 {
4219 
4220 	switch (mtype) {
4221 	case FW_MEMTYPE_EDC0:
4222 		return (MEM_EDC0);
4223 	case FW_MEMTYPE_EDC1:
4224 		return (MEM_EDC1);
4225 	case FW_MEMTYPE_EXTMEM:
4226 		return (MEM_MC0);
4227 	case FW_MEMTYPE_EXTMEM1:
4228 		return (MEM_MC1);
4229 	default:
4230 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4231 	}
4232 }
4233 
4234 /*
4235  * Verify that the memory range specified by the memtype/offset/len pair is
4236  * valid and lies entirely within the memtype specified.  The global address of
4237  * the start of the range is returned in addr.
4238  */
4239 static int
4240 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4241     uint32_t *addr)
4242 {
4243 	uint32_t em, addr_len, maddr;
4244 
4245 	/* Memory can only be accessed in naturally aligned 4 byte units */
4246 	if (off & 3 || len & 3 || len == 0)
4247 		return (EINVAL);
4248 
4249 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4250 	switch (fwmtype_to_hwmtype(mtype)) {
4251 	case MEM_EDC0:
4252 		if (!(em & F_EDRAM0_ENABLE))
4253 			return (EINVAL);
4254 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4255 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4256 		break;
4257 	case MEM_EDC1:
4258 		if (!(em & F_EDRAM1_ENABLE))
4259 			return (EINVAL);
4260 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4261 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4262 		break;
4263 	case MEM_MC:
4264 		if (!(em & F_EXT_MEM_ENABLE))
4265 			return (EINVAL);
4266 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4267 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4268 		break;
4269 	case MEM_MC1:
4270 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4271 			return (EINVAL);
4272 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4273 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4274 		break;
4275 	default:
4276 		return (EINVAL);
4277 	}
4278 
4279 	*addr = maddr + off;	/* global address */
4280 	return (validate_mem_range(sc, *addr, len));
4281 }
4282 
4283 static int
4284 fixup_devlog_params(struct adapter *sc)
4285 {
4286 	struct devlog_params *dparams = &sc->params.devlog;
4287 	int rc;
4288 
4289 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4290 	    dparams->size, &dparams->addr);
4291 
4292 	return (rc);
4293 }
4294 
4295 static void
4296 update_nirq(struct intrs_and_queues *iaq, int nports)
4297 {
4298 
4299 	iaq->nirq = T4_EXTRA_INTR;
4300 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4301 	iaq->nirq += nports * iaq->nofldrxq;
4302 	iaq->nirq += nports * (iaq->num_vis - 1) *
4303 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4304 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4305 }
4306 
4307 /*
4308  * Adjust requirements to fit the number of interrupts available.
4309  */
4310 static void
4311 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4312     int navail)
4313 {
4314 	int old_nirq;
4315 	const int nports = sc->params.nports;
4316 
4317 	MPASS(nports > 0);
4318 	MPASS(navail > 0);
4319 
4320 	bzero(iaq, sizeof(*iaq));
4321 	iaq->intr_type = itype;
4322 	iaq->num_vis = t4_num_vis;
4323 	iaq->ntxq = t4_ntxq;
4324 	iaq->ntxq_vi = t4_ntxq_vi;
4325 	iaq->nrxq = t4_nrxq;
4326 	iaq->nrxq_vi = t4_nrxq_vi;
4327 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4328 	if (is_offload(sc) || is_ethoffload(sc)) {
4329 		iaq->nofldtxq = t4_nofldtxq;
4330 		iaq->nofldtxq_vi = t4_nofldtxq_vi;
4331 	}
4332 #endif
4333 #ifdef TCP_OFFLOAD
4334 	if (is_offload(sc)) {
4335 		iaq->nofldrxq = t4_nofldrxq;
4336 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4337 	}
4338 #endif
4339 #ifdef DEV_NETMAP
4340 	if (t4_native_netmap & NN_MAIN_VI) {
4341 		iaq->nnmtxq = t4_nnmtxq;
4342 		iaq->nnmrxq = t4_nnmrxq;
4343 	}
4344 	if (t4_native_netmap & NN_EXTRA_VI) {
4345 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4346 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4347 	}
4348 #endif
4349 
4350 	update_nirq(iaq, nports);
4351 	if (iaq->nirq <= navail &&
4352 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4353 		/*
4354 		 * This is the normal case -- there are enough interrupts for
4355 		 * everything.
4356 		 */
4357 		goto done;
4358 	}
4359 
4360 	/*
4361 	 * If extra VIs have been configured try reducing their count and see if
4362 	 * that works.
4363 	 */
4364 	while (iaq->num_vis > 1) {
4365 		iaq->num_vis--;
4366 		update_nirq(iaq, nports);
4367 		if (iaq->nirq <= navail &&
4368 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4369 			device_printf(sc->dev, "virtual interfaces per port "
4370 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4371 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4372 			    "itype %d, navail %u, nirq %d.\n",
4373 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4374 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4375 			    itype, navail, iaq->nirq);
4376 			goto done;
4377 		}
4378 	}
4379 
4380 	/*
4381 	 * Extra VIs will not be created.  Log a message if they were requested.
4382 	 */
4383 	MPASS(iaq->num_vis == 1);
4384 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4385 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4386 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4387 	if (iaq->num_vis != t4_num_vis) {
4388 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4389 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4390 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4391 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4392 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4393 	}
4394 
4395 	/*
4396 	 * Keep reducing the number of NIC rx queues to the next lower power of
4397 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4398 	 * if that works.
4399 	 */
4400 	do {
4401 		if (iaq->nrxq > 1) {
4402 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4403 			if (iaq->nnmrxq > iaq->nrxq)
4404 				iaq->nnmrxq = iaq->nrxq;
4405 		}
4406 		if (iaq->nofldrxq > 1)
4407 			iaq->nofldrxq >>= 1;
4408 
4409 		old_nirq = iaq->nirq;
4410 		update_nirq(iaq, nports);
4411 		if (iaq->nirq <= navail &&
4412 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4413 			device_printf(sc->dev, "running with reduced number of "
4414 			    "rx queues because of shortage of interrupts.  "
4415 			    "nrxq=%u, nofldrxq=%u.  "
4416 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4417 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4418 			goto done;
4419 		}
4420 	} while (old_nirq != iaq->nirq);
4421 
4422 	/* One interrupt for everything.  Ugh. */
4423 	device_printf(sc->dev, "running with minimal number of queues.  "
4424 	    "itype %d, navail %u.\n", itype, navail);
4425 	iaq->nirq = 1;
4426 	iaq->nrxq = 1;
4427 	iaq->ntxq = 1;
4428 	if (iaq->nofldrxq > 0) {
4429 		iaq->nofldrxq = 1;
4430 		iaq->nofldtxq = 1;
4431 	}
4432 	iaq->nnmtxq = 0;
4433 	iaq->nnmrxq = 0;
4434 done:
4435 	MPASS(iaq->num_vis > 0);
4436 	if (iaq->num_vis > 1) {
4437 		MPASS(iaq->nrxq_vi > 0);
4438 		MPASS(iaq->ntxq_vi > 0);
4439 	}
4440 	MPASS(iaq->nirq > 0);
4441 	MPASS(iaq->nrxq > 0);
4442 	MPASS(iaq->ntxq > 0);
4443 	if (itype == INTR_MSI) {
4444 		MPASS(powerof2(iaq->nirq));
4445 	}
4446 }
4447 
4448 static int
4449 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4450 {
4451 	int rc, itype, navail, nalloc;
4452 
4453 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4454 
4455 		if ((itype & t4_intr_types) == 0)
4456 			continue;	/* not allowed */
4457 
4458 		if (itype == INTR_MSIX)
4459 			navail = pci_msix_count(sc->dev);
4460 		else if (itype == INTR_MSI)
4461 			navail = pci_msi_count(sc->dev);
4462 		else
4463 			navail = 1;
4464 restart:
4465 		if (navail == 0)
4466 			continue;
4467 
4468 		calculate_iaq(sc, iaq, itype, navail);
4469 		nalloc = iaq->nirq;
4470 		rc = 0;
4471 		if (itype == INTR_MSIX)
4472 			rc = pci_alloc_msix(sc->dev, &nalloc);
4473 		else if (itype == INTR_MSI)
4474 			rc = pci_alloc_msi(sc->dev, &nalloc);
4475 
4476 		if (rc == 0 && nalloc > 0) {
4477 			if (nalloc == iaq->nirq)
4478 				return (0);
4479 
4480 			/*
4481 			 * Didn't get the number requested.  Use whatever number
4482 			 * the kernel is willing to allocate.
4483 			 */
4484 			device_printf(sc->dev, "fewer vectors than requested, "
4485 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4486 			    itype, iaq->nirq, nalloc);
4487 			pci_release_msi(sc->dev);
4488 			navail = nalloc;
4489 			goto restart;
4490 		}
4491 
4492 		device_printf(sc->dev,
4493 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4494 		    itype, rc, iaq->nirq, nalloc);
4495 	}
4496 
4497 	device_printf(sc->dev,
4498 	    "failed to find a usable interrupt type.  "
4499 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4500 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4501 
4502 	return (ENXIO);
4503 }
4504 
4505 #define FW_VERSION(chip) ( \
4506     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4507     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4508     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4509     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4510 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4511 
4512 /* Just enough of fw_hdr to cover all version info. */
4513 struct fw_h {
4514 	__u8	ver;
4515 	__u8	chip;
4516 	__be16	len512;
4517 	__be32	fw_ver;
4518 	__be32	tp_microcode_ver;
4519 	__u8	intfver_nic;
4520 	__u8	intfver_vnic;
4521 	__u8	intfver_ofld;
4522 	__u8	intfver_ri;
4523 	__u8	intfver_iscsipdu;
4524 	__u8	intfver_iscsi;
4525 	__u8	intfver_fcoepdu;
4526 	__u8	intfver_fcoe;
4527 };
4528 /* Spot check a couple of fields. */
4529 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4530 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4531 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4532 
4533 struct fw_info {
4534 	uint8_t chip;
4535 	char *kld_name;
4536 	char *fw_mod_name;
4537 	struct fw_h fw_h;
4538 } fw_info[] = {
4539 	{
4540 		.chip = CHELSIO_T4,
4541 		.kld_name = "t4fw_cfg",
4542 		.fw_mod_name = "t4fw",
4543 		.fw_h = {
4544 			.chip = FW_HDR_CHIP_T4,
4545 			.fw_ver = htobe32(FW_VERSION(T4)),
4546 			.intfver_nic = FW_INTFVER(T4, NIC),
4547 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4548 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4549 			.intfver_ri = FW_INTFVER(T4, RI),
4550 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4551 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4552 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4553 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4554 		},
4555 	}, {
4556 		.chip = CHELSIO_T5,
4557 		.kld_name = "t5fw_cfg",
4558 		.fw_mod_name = "t5fw",
4559 		.fw_h = {
4560 			.chip = FW_HDR_CHIP_T5,
4561 			.fw_ver = htobe32(FW_VERSION(T5)),
4562 			.intfver_nic = FW_INTFVER(T5, NIC),
4563 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4564 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4565 			.intfver_ri = FW_INTFVER(T5, RI),
4566 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4567 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4568 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4569 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4570 		},
4571 	}, {
4572 		.chip = CHELSIO_T6,
4573 		.kld_name = "t6fw_cfg",
4574 		.fw_mod_name = "t6fw",
4575 		.fw_h = {
4576 			.chip = FW_HDR_CHIP_T6,
4577 			.fw_ver = htobe32(FW_VERSION(T6)),
4578 			.intfver_nic = FW_INTFVER(T6, NIC),
4579 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4580 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4581 			.intfver_ri = FW_INTFVER(T6, RI),
4582 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4583 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4584 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4585 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4586 		},
4587 	}
4588 };
4589 
4590 static struct fw_info *
4591 find_fw_info(int chip)
4592 {
4593 	int i;
4594 
4595 	for (i = 0; i < nitems(fw_info); i++) {
4596 		if (fw_info[i].chip == chip)
4597 			return (&fw_info[i]);
4598 	}
4599 	return (NULL);
4600 }
4601 
4602 /*
4603  * Is the given firmware API compatible with the one the driver was compiled
4604  * with?
4605  */
4606 static int
4607 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4608 {
4609 
4610 	/* short circuit if it's the exact same firmware version */
4611 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4612 		return (1);
4613 
4614 	/*
4615 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4616 	 * features that are supported in the driver.
4617 	 */
4618 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4619 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4620 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4621 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4622 		return (1);
4623 #undef SAME_INTF
4624 
4625 	return (0);
4626 }
4627 
4628 static int
4629 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4630     const struct firmware **fw)
4631 {
4632 	struct fw_info *fw_info;
4633 
4634 	*dcfg = NULL;
4635 	if (fw != NULL)
4636 		*fw = NULL;
4637 
4638 	fw_info = find_fw_info(chip_id(sc));
4639 	if (fw_info == NULL) {
4640 		device_printf(sc->dev,
4641 		    "unable to look up firmware information for chip %d.\n",
4642 		    chip_id(sc));
4643 		return (EINVAL);
4644 	}
4645 
4646 	*dcfg = firmware_get(fw_info->kld_name);
4647 	if (*dcfg != NULL) {
4648 		if (fw != NULL)
4649 			*fw = firmware_get(fw_info->fw_mod_name);
4650 		return (0);
4651 	}
4652 
4653 	return (ENOENT);
4654 }
4655 
4656 static void
4657 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4658     const struct firmware *fw)
4659 {
4660 
4661 	if (fw != NULL)
4662 		firmware_put(fw, FIRMWARE_UNLOAD);
4663 	if (dcfg != NULL)
4664 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4665 }
4666 
4667 /*
4668  * Return values:
4669  * 0 means no firmware install attempted.
4670  * ERESTART means a firmware install was attempted and was successful.
4671  * +ve errno means a firmware install was attempted but failed.
4672  */
4673 static int
4674 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4675     const struct fw_h *drv_fw, const char *reason, int *already)
4676 {
4677 	const struct firmware *cfg, *fw;
4678 	const uint32_t c = be32toh(card_fw->fw_ver);
4679 	uint32_t d, k;
4680 	int rc, fw_install;
4681 	struct fw_h bundled_fw;
4682 	bool load_attempted;
4683 
4684 	cfg = fw = NULL;
4685 	load_attempted = false;
4686 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4687 
4688 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
4689 	if (t4_fw_install < 0) {
4690 		rc = load_fw_module(sc, &cfg, &fw);
4691 		if (rc != 0 || fw == NULL) {
4692 			device_printf(sc->dev,
4693 			    "failed to load firmware module: %d. cfg %p, fw %p;"
4694 			    " will use compiled-in firmware version for"
4695 			    "hw.cxgbe.fw_install checks.\n",
4696 			    rc, cfg, fw);
4697 		} else {
4698 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
4699 		}
4700 		load_attempted = true;
4701 	}
4702 	d = be32toh(bundled_fw.fw_ver);
4703 
4704 	if (reason != NULL)
4705 		goto install;
4706 
4707 	if ((sc->flags & FW_OK) == 0) {
4708 
4709 		if (c == 0xffffffff) {
4710 			reason = "missing";
4711 			goto install;
4712 		}
4713 
4714 		rc = 0;
4715 		goto done;
4716 	}
4717 
4718 	if (!fw_compatible(card_fw, &bundled_fw)) {
4719 		reason = "incompatible or unusable";
4720 		goto install;
4721 	}
4722 
4723 	if (d > c) {
4724 		reason = "older than the version bundled with this driver";
4725 		goto install;
4726 	}
4727 
4728 	if (fw_install == 2 && d != c) {
4729 		reason = "different than the version bundled with this driver";
4730 		goto install;
4731 	}
4732 
4733 	/* No reason to do anything to the firmware already on the card. */
4734 	rc = 0;
4735 	goto done;
4736 
4737 install:
4738 	rc = 0;
4739 	if ((*already)++)
4740 		goto done;
4741 
4742 	if (fw_install == 0) {
4743 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4744 		    "but the driver is prohibited from installing a firmware "
4745 		    "on the card.\n",
4746 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4747 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4748 
4749 		goto done;
4750 	}
4751 
4752 	/*
4753 	 * We'll attempt to install a firmware.  Load the module first (if it
4754 	 * hasn't been loaded already).
4755 	 */
4756 	if (!load_attempted) {
4757 		rc = load_fw_module(sc, &cfg, &fw);
4758 		if (rc != 0 || fw == NULL) {
4759 			device_printf(sc->dev,
4760 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
4761 			    rc, cfg, fw);
4762 			/* carry on */
4763 		}
4764 	}
4765 	if (fw == NULL) {
4766 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4767 		    "but the driver cannot take corrective action because it "
4768 		    "is unable to load the firmware module.\n",
4769 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4770 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
4771 		rc = sc->flags & FW_OK ? 0 : ENOENT;
4772 		goto done;
4773 	}
4774 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
4775 	if (k != d) {
4776 		MPASS(t4_fw_install > 0);
4777 		device_printf(sc->dev,
4778 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
4779 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
4780 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
4781 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
4782 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4783 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4784 		rc = sc->flags & FW_OK ? 0 : EINVAL;
4785 		goto done;
4786 	}
4787 
4788 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
4789 	    "installing firmware %u.%u.%u.%u on card.\n",
4790 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
4791 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
4792 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
4793 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
4794 
4795 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
4796 	if (rc != 0) {
4797 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
4798 	} else {
4799 		/* Installed successfully, update the cached header too. */
4800 		rc = ERESTART;
4801 		memcpy(card_fw, fw->data, sizeof(*card_fw));
4802 	}
4803 done:
4804 	unload_fw_module(sc, cfg, fw);
4805 
4806 	return (rc);
4807 }
4808 
4809 /*
4810  * Establish contact with the firmware and attempt to become the master driver.
4811  *
4812  * A firmware will be installed to the card if needed (if the driver is allowed
4813  * to do so).
4814  */
4815 static int
4816 contact_firmware(struct adapter *sc)
4817 {
4818 	int rc, already = 0;
4819 	enum dev_state state;
4820 	struct fw_info *fw_info;
4821 	struct fw_hdr *card_fw;		/* fw on the card */
4822 	const struct fw_h *drv_fw;
4823 
4824 	fw_info = find_fw_info(chip_id(sc));
4825 	if (fw_info == NULL) {
4826 		device_printf(sc->dev,
4827 		    "unable to look up firmware information for chip %d.\n",
4828 		    chip_id(sc));
4829 		return (EINVAL);
4830 	}
4831 	drv_fw = &fw_info->fw_h;
4832 
4833 	/* Read the header of the firmware on the card */
4834 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
4835 restart:
4836 	rc = -t4_get_fw_hdr(sc, card_fw);
4837 	if (rc != 0) {
4838 		device_printf(sc->dev,
4839 		    "unable to read firmware header from card's flash: %d\n",
4840 		    rc);
4841 		goto done;
4842 	}
4843 
4844 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
4845 	    &already);
4846 	if (rc == ERESTART)
4847 		goto restart;
4848 	if (rc != 0)
4849 		goto done;
4850 
4851 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
4852 	if (rc < 0 || state == DEV_STATE_ERR) {
4853 		rc = -rc;
4854 		device_printf(sc->dev,
4855 		    "failed to connect to the firmware: %d, %d.  "
4856 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4857 #if 0
4858 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4859 		    "not responding properly to HELLO", &already) == ERESTART)
4860 			goto restart;
4861 #endif
4862 		goto done;
4863 	}
4864 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
4865 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
4866 
4867 	if (rc == sc->pf) {
4868 		sc->flags |= MASTER_PF;
4869 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
4870 		    NULL, &already);
4871 		if (rc == ERESTART)
4872 			rc = 0;
4873 		else if (rc != 0)
4874 			goto done;
4875 	} else if (state == DEV_STATE_UNINIT) {
4876 		/*
4877 		 * We didn't get to be the master so we definitely won't be
4878 		 * configuring the chip.  It's a bug if someone else hasn't
4879 		 * configured it already.
4880 		 */
4881 		device_printf(sc->dev, "couldn't be master(%d), "
4882 		    "device not already initialized either(%d).  "
4883 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4884 		rc = EPROTO;
4885 		goto done;
4886 	} else {
4887 		/*
4888 		 * Some other PF is the master and has configured the chip.
4889 		 * This is allowed but untested.
4890 		 */
4891 		device_printf(sc->dev, "PF%d is master, device state %d.  "
4892 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
4893 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
4894 		sc->cfcsum = 0;
4895 		rc = 0;
4896 	}
4897 done:
4898 	if (rc != 0 && sc->flags & FW_OK) {
4899 		t4_fw_bye(sc, sc->mbox);
4900 		sc->flags &= ~FW_OK;
4901 	}
4902 	free(card_fw, M_CXGBE);
4903 	return (rc);
4904 }
4905 
4906 static int
4907 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
4908     uint32_t mtype, uint32_t moff)
4909 {
4910 	struct fw_info *fw_info;
4911 	const struct firmware *dcfg, *rcfg = NULL;
4912 	const uint32_t *cfdata;
4913 	uint32_t cflen, addr;
4914 	int rc;
4915 
4916 	load_fw_module(sc, &dcfg, NULL);
4917 
4918 	/* Card specific interpretation of "default". */
4919 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4920 		if (pci_get_device(sc->dev) == 0x440a)
4921 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
4922 		if (is_fpga(sc))
4923 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
4924 	}
4925 
4926 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
4927 		if (dcfg == NULL) {
4928 			device_printf(sc->dev,
4929 			    "KLD with default config is not available.\n");
4930 			rc = ENOENT;
4931 			goto done;
4932 		}
4933 		cfdata = dcfg->data;
4934 		cflen = dcfg->datasize & ~3;
4935 	} else {
4936 		char s[32];
4937 
4938 		fw_info = find_fw_info(chip_id(sc));
4939 		if (fw_info == NULL) {
4940 			device_printf(sc->dev,
4941 			    "unable to look up firmware information for chip %d.\n",
4942 			    chip_id(sc));
4943 			rc = EINVAL;
4944 			goto done;
4945 		}
4946 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
4947 
4948 		rcfg = firmware_get(s);
4949 		if (rcfg == NULL) {
4950 			device_printf(sc->dev,
4951 			    "unable to load module \"%s\" for configuration "
4952 			    "profile \"%s\".\n", s, cfg_file);
4953 			rc = ENOENT;
4954 			goto done;
4955 		}
4956 		cfdata = rcfg->data;
4957 		cflen = rcfg->datasize & ~3;
4958 	}
4959 
4960 	if (cflen > FLASH_CFG_MAX_SIZE) {
4961 		device_printf(sc->dev,
4962 		    "config file too long (%d, max allowed is %d).\n",
4963 		    cflen, FLASH_CFG_MAX_SIZE);
4964 		rc = EINVAL;
4965 		goto done;
4966 	}
4967 
4968 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
4969 	if (rc != 0) {
4970 		device_printf(sc->dev,
4971 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
4972 		    __func__, mtype, moff, cflen, rc);
4973 		rc = EINVAL;
4974 		goto done;
4975 	}
4976 	write_via_memwin(sc, 2, addr, cfdata, cflen);
4977 done:
4978 	if (rcfg != NULL)
4979 		firmware_put(rcfg, FIRMWARE_UNLOAD);
4980 	unload_fw_module(sc, dcfg, NULL);
4981 	return (rc);
4982 }
4983 
4984 struct caps_allowed {
4985 	uint16_t nbmcaps;
4986 	uint16_t linkcaps;
4987 	uint16_t switchcaps;
4988 	uint16_t niccaps;
4989 	uint16_t toecaps;
4990 	uint16_t rdmacaps;
4991 	uint16_t cryptocaps;
4992 	uint16_t iscsicaps;
4993 	uint16_t fcoecaps;
4994 };
4995 
4996 #define FW_PARAM_DEV(param) \
4997 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
4998 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
4999 #define FW_PARAM_PFVF(param) \
5000 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5001 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5002 
5003 /*
5004  * Provide a configuration profile to the firmware and have it initialize the
5005  * chip accordingly.  This may involve uploading a configuration file to the
5006  * card.
5007  */
5008 static int
5009 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5010     const struct caps_allowed *caps_allowed)
5011 {
5012 	int rc;
5013 	struct fw_caps_config_cmd caps;
5014 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5015 
5016 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5017 	if (rc != 0) {
5018 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5019 		return (rc);
5020 	}
5021 
5022 	bzero(&caps, sizeof(caps));
5023 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5024 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5025 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5026 		mtype = 0;
5027 		moff = 0;
5028 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5029 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5030 		mtype = FW_MEMTYPE_FLASH;
5031 		moff = t4_flash_cfg_addr(sc);
5032 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5033 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5034 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5035 		    FW_LEN16(caps));
5036 	} else {
5037 		/*
5038 		 * Ask the firmware where it wants us to upload the config file.
5039 		 */
5040 		param = FW_PARAM_DEV(CF);
5041 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5042 		if (rc != 0) {
5043 			/* No support for config file?  Shouldn't happen. */
5044 			device_printf(sc->dev,
5045 			    "failed to query config file location: %d.\n", rc);
5046 			goto done;
5047 		}
5048 		mtype = G_FW_PARAMS_PARAM_Y(val);
5049 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5050 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5051 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5052 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5053 		    FW_LEN16(caps));
5054 
5055 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff);
5056 		if (rc != 0) {
5057 			device_printf(sc->dev,
5058 			    "failed to upload config file to card: %d.\n", rc);
5059 			goto done;
5060 		}
5061 	}
5062 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5063 	if (rc != 0) {
5064 		device_printf(sc->dev, "failed to pre-process config file: %d "
5065 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5066 		goto done;
5067 	}
5068 
5069 	finicsum = be32toh(caps.finicsum);
5070 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5071 	if (finicsum != cfcsum) {
5072 		device_printf(sc->dev,
5073 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5074 		    finicsum, cfcsum);
5075 	}
5076 	sc->cfcsum = cfcsum;
5077 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5078 
5079 	/*
5080 	 * Let the firmware know what features will (not) be used so it can tune
5081 	 * things accordingly.
5082 	 */
5083 #define LIMIT_CAPS(x) do { \
5084 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5085 } while (0)
5086 	LIMIT_CAPS(nbm);
5087 	LIMIT_CAPS(link);
5088 	LIMIT_CAPS(switch);
5089 	LIMIT_CAPS(nic);
5090 	LIMIT_CAPS(toe);
5091 	LIMIT_CAPS(rdma);
5092 	LIMIT_CAPS(crypto);
5093 	LIMIT_CAPS(iscsi);
5094 	LIMIT_CAPS(fcoe);
5095 #undef LIMIT_CAPS
5096 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5097 		/*
5098 		 * TOE and hashfilters are mutually exclusive.  It is a config
5099 		 * file or firmware bug if both are reported as available.  Try
5100 		 * to cope with the situation in non-debug builds by disabling
5101 		 * TOE.
5102 		 */
5103 		MPASS(caps.toecaps == 0);
5104 
5105 		caps.toecaps = 0;
5106 		caps.rdmacaps = 0;
5107 		caps.iscsicaps = 0;
5108 	}
5109 
5110 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5111 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5112 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5113 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5114 	if (rc != 0) {
5115 		device_printf(sc->dev,
5116 		    "failed to process config file: %d.\n", rc);
5117 		goto done;
5118 	}
5119 
5120 	t4_tweak_chip_settings(sc);
5121 	set_params__pre_init(sc);
5122 
5123 	/* get basic stuff going */
5124 	rc = -t4_fw_initialize(sc, sc->mbox);
5125 	if (rc != 0) {
5126 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5127 		goto done;
5128 	}
5129 done:
5130 	return (rc);
5131 }
5132 
5133 /*
5134  * Partition chip resources for use between various PFs, VFs, etc.
5135  */
5136 static int
5137 partition_resources(struct adapter *sc)
5138 {
5139 	char cfg_file[sizeof(t4_cfg_file)];
5140 	struct caps_allowed caps_allowed;
5141 	int rc;
5142 	bool fallback;
5143 
5144 	/* Only the master driver gets to configure the chip resources. */
5145 	MPASS(sc->flags & MASTER_PF);
5146 
5147 #define COPY_CAPS(x) do { \
5148 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5149 } while (0)
5150 	bzero(&caps_allowed, sizeof(caps_allowed));
5151 	COPY_CAPS(nbm);
5152 	COPY_CAPS(link);
5153 	COPY_CAPS(switch);
5154 	COPY_CAPS(nic);
5155 	COPY_CAPS(toe);
5156 	COPY_CAPS(rdma);
5157 	COPY_CAPS(crypto);
5158 	COPY_CAPS(iscsi);
5159 	COPY_CAPS(fcoe);
5160 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5161 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5162 retry:
5163 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5164 	if (rc != 0 && fallback) {
5165 		dump_devlog(sc);
5166 		device_printf(sc->dev,
5167 		    "failed (%d) to configure card with \"%s\" profile, "
5168 		    "will fall back to a basic configuration and retry.\n",
5169 		    rc, cfg_file);
5170 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5171 		bzero(&caps_allowed, sizeof(caps_allowed));
5172 		COPY_CAPS(switch);
5173 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5174 		fallback = false;
5175 		goto retry;
5176 	}
5177 #undef COPY_CAPS
5178 	return (rc);
5179 }
5180 
5181 /*
5182  * Retrieve parameters that are needed (or nice to have) very early.
5183  */
5184 static int
5185 get_params__pre_init(struct adapter *sc)
5186 {
5187 	int rc;
5188 	uint32_t param[2], val[2];
5189 
5190 	t4_get_version_info(sc);
5191 
5192 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5193 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5194 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5195 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5196 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5197 
5198 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5199 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5200 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5201 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5202 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5203 
5204 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5205 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5206 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5207 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5208 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5209 
5210 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5211 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5212 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5213 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5214 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5215 
5216 	param[0] = FW_PARAM_DEV(PORTVEC);
5217 	param[1] = FW_PARAM_DEV(CCLK);
5218 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5219 	if (rc != 0) {
5220 		device_printf(sc->dev,
5221 		    "failed to query parameters (pre_init): %d.\n", rc);
5222 		return (rc);
5223 	}
5224 
5225 	sc->params.portvec = val[0];
5226 	sc->params.nports = bitcount32(val[0]);
5227 	sc->params.vpd.cclk = val[1];
5228 
5229 	/* Read device log parameters. */
5230 	rc = -t4_init_devlog_params(sc, 1);
5231 	if (rc == 0)
5232 		fixup_devlog_params(sc);
5233 	else {
5234 		device_printf(sc->dev,
5235 		    "failed to get devlog parameters: %d.\n", rc);
5236 		rc = 0;	/* devlog isn't critical for device operation */
5237 	}
5238 
5239 	return (rc);
5240 }
5241 
5242 /*
5243  * Any params that need to be set before FW_INITIALIZE.
5244  */
5245 static int
5246 set_params__pre_init(struct adapter *sc)
5247 {
5248 	int rc = 0;
5249 	uint32_t param, val;
5250 
5251 	if (chip_id(sc) >= CHELSIO_T6) {
5252 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5253 		val = 1;
5254 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5255 		/* firmwares < 1.20.1.0 do not have this param. */
5256 		if (rc == FW_EINVAL &&
5257 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5258 			rc = 0;
5259 		}
5260 		if (rc != 0) {
5261 			device_printf(sc->dev,
5262 			    "failed to enable high priority filters :%d.\n",
5263 			    rc);
5264 		}
5265 
5266 		param = FW_PARAM_DEV(PPOD_EDRAM);
5267 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5268 		if (rc == 0 && val == 1) {
5269 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5270 			    &val);
5271 			if (rc != 0) {
5272 				device_printf(sc->dev,
5273 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5274 			}
5275 		}
5276 	}
5277 
5278 	/* Enable opaque VIIDs with firmwares that support it. */
5279 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5280 	val = 1;
5281 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5282 	if (rc == 0 && val == 1)
5283 		sc->params.viid_smt_extn_support = true;
5284 	else
5285 		sc->params.viid_smt_extn_support = false;
5286 
5287 	return (rc);
5288 }
5289 
5290 /*
5291  * Retrieve various parameters that are of interest to the driver.  The device
5292  * has been initialized by the firmware at this point.
5293  */
5294 static int
5295 get_params__post_init(struct adapter *sc)
5296 {
5297 	int rc;
5298 	uint32_t param[7], val[7];
5299 	struct fw_caps_config_cmd caps;
5300 
5301 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5302 	param[1] = FW_PARAM_PFVF(EQ_START);
5303 	param[2] = FW_PARAM_PFVF(FILTER_START);
5304 	param[3] = FW_PARAM_PFVF(FILTER_END);
5305 	param[4] = FW_PARAM_PFVF(L2T_START);
5306 	param[5] = FW_PARAM_PFVF(L2T_END);
5307 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5308 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5309 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5310 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5311 	if (rc != 0) {
5312 		device_printf(sc->dev,
5313 		    "failed to query parameters (post_init): %d.\n", rc);
5314 		return (rc);
5315 	}
5316 
5317 	sc->sge.iq_start = val[0];
5318 	sc->sge.eq_start = val[1];
5319 	if ((int)val[3] > (int)val[2]) {
5320 		sc->tids.ftid_base = val[2];
5321 		sc->tids.ftid_end = val[3];
5322 		sc->tids.nftids = val[3] - val[2] + 1;
5323 	}
5324 	sc->vres.l2t.start = val[4];
5325 	sc->vres.l2t.size = val[5] - val[4] + 1;
5326 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5327 	if (sc->vres.l2t.size > 0)
5328 		MPASS(fls(val[5]) <= S_SYNC_WR);
5329 	sc->params.core_vdd = val[6];
5330 
5331 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5332 	param[1] = FW_PARAM_PFVF(EQ_END);
5333 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5334 	if (rc != 0) {
5335 		device_printf(sc->dev,
5336 		    "failed to query parameters (post_init2): %d.\n", rc);
5337 		return (rc);
5338 	}
5339 	MPASS((int)val[0] >= sc->sge.iq_start);
5340 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5341 	MPASS((int)val[1] >= sc->sge.eq_start);
5342 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5343 
5344 	if (chip_id(sc) >= CHELSIO_T6) {
5345 
5346 		sc->tids.tid_base = t4_read_reg(sc,
5347 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5348 
5349 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5350 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5351 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5352 		if (rc != 0) {
5353 			device_printf(sc->dev,
5354 			   "failed to query hpfilter parameters: %d.\n", rc);
5355 			return (rc);
5356 		}
5357 		if ((int)val[1] > (int)val[0]) {
5358 			sc->tids.hpftid_base = val[0];
5359 			sc->tids.hpftid_end = val[1];
5360 			sc->tids.nhpftids = val[1] - val[0] + 1;
5361 
5362 			/*
5363 			 * These should go off if the layout changes and the
5364 			 * driver needs to catch up.
5365 			 */
5366 			MPASS(sc->tids.hpftid_base == 0);
5367 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5368 		}
5369 
5370 		param[0] = FW_PARAM_PFVF(RAWF_START);
5371 		param[1] = FW_PARAM_PFVF(RAWF_END);
5372 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5373 		if (rc != 0) {
5374 			device_printf(sc->dev,
5375 			   "failed to query rawf parameters: %d.\n", rc);
5376 			return (rc);
5377 		}
5378 		if ((int)val[1] > (int)val[0]) {
5379 			sc->rawf_base = val[0];
5380 			sc->nrawf = val[1] - val[0] + 1;
5381 		}
5382 	}
5383 
5384 	/*
5385 	 * The parameters that follow may not be available on all firmwares.  We
5386 	 * query them individually rather than in a compound query because old
5387 	 * firmwares fail the entire query if an unknown parameter is queried.
5388 	 */
5389 
5390 	/*
5391 	 * MPS buffer group configuration.
5392 	 */
5393 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5394 	val[0] = 0;
5395 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5396 	if (rc == 0)
5397 		sc->params.mps_bg_map = val[0];
5398 	else
5399 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5400 
5401 	param[0] = FW_PARAM_DEV(TPCHMAP);
5402 	val[0] = 0;
5403 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5404 	if (rc == 0)
5405 		sc->params.tp_ch_map = val[0];
5406 	else
5407 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5408 
5409 	/*
5410 	 * Determine whether the firmware supports the filter2 work request.
5411 	 */
5412 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5413 	val[0] = 0;
5414 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5415 	if (rc == 0)
5416 		sc->params.filter2_wr_support = val[0] != 0;
5417 	else
5418 		sc->params.filter2_wr_support = 0;
5419 
5420 	/*
5421 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5422 	 */
5423 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5424 	val[0] = 0;
5425 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5426 	if (rc == 0)
5427 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5428 	else
5429 		sc->params.ulptx_memwrite_dsgl = false;
5430 
5431 	/* FW_RI_FR_NSMR_TPTE_WR support */
5432 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5433 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5434 	if (rc == 0)
5435 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5436 	else
5437 		sc->params.fr_nsmr_tpte_wr_support = false;
5438 
5439 	/* Support for 512 SGL entries per FR MR. */
5440 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5441 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5442 	if (rc == 0)
5443 		sc->params.dev_512sgl_mr = val[0] != 0;
5444 	else
5445 		sc->params.dev_512sgl_mr = false;
5446 
5447 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5448 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5449 	if (rc == 0)
5450 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5451 	else
5452 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5453 
5454 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5455 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5456 	if (rc == 0) {
5457 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5458 		sc->params.nsched_cls = val[0];
5459 	} else
5460 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5461 
5462 	/* get capabilites */
5463 	bzero(&caps, sizeof(caps));
5464 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5465 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5466 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5467 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5468 	if (rc != 0) {
5469 		device_printf(sc->dev,
5470 		    "failed to get card capabilities: %d.\n", rc);
5471 		return (rc);
5472 	}
5473 
5474 #define READ_CAPS(x) do { \
5475 	sc->x = htobe16(caps.x); \
5476 } while (0)
5477 	READ_CAPS(nbmcaps);
5478 	READ_CAPS(linkcaps);
5479 	READ_CAPS(switchcaps);
5480 	READ_CAPS(niccaps);
5481 	READ_CAPS(toecaps);
5482 	READ_CAPS(rdmacaps);
5483 	READ_CAPS(cryptocaps);
5484 	READ_CAPS(iscsicaps);
5485 	READ_CAPS(fcoecaps);
5486 
5487 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5488 		MPASS(chip_id(sc) > CHELSIO_T4);
5489 		MPASS(sc->toecaps == 0);
5490 		sc->toecaps = 0;
5491 
5492 		param[0] = FW_PARAM_DEV(NTID);
5493 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5494 		if (rc != 0) {
5495 			device_printf(sc->dev,
5496 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5497 			return (rc);
5498 		}
5499 		sc->tids.ntids = val[0];
5500 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5501 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5502 			sc->tids.ntids -= sc->tids.nhpftids;
5503 		}
5504 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5505 		sc->params.hash_filter = 1;
5506 	}
5507 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5508 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5509 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5510 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5511 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5512 		if (rc != 0) {
5513 			device_printf(sc->dev,
5514 			    "failed to query NIC parameters: %d.\n", rc);
5515 			return (rc);
5516 		}
5517 		if ((int)val[1] > (int)val[0]) {
5518 			sc->tids.etid_base = val[0];
5519 			sc->tids.etid_end = val[1];
5520 			sc->tids.netids = val[1] - val[0] + 1;
5521 			sc->params.eo_wr_cred = val[2];
5522 			sc->params.ethoffload = 1;
5523 		}
5524 	}
5525 	if (sc->toecaps) {
5526 		/* query offload-related parameters */
5527 		param[0] = FW_PARAM_DEV(NTID);
5528 		param[1] = FW_PARAM_PFVF(SERVER_START);
5529 		param[2] = FW_PARAM_PFVF(SERVER_END);
5530 		param[3] = FW_PARAM_PFVF(TDDP_START);
5531 		param[4] = FW_PARAM_PFVF(TDDP_END);
5532 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5533 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5534 		if (rc != 0) {
5535 			device_printf(sc->dev,
5536 			    "failed to query TOE parameters: %d.\n", rc);
5537 			return (rc);
5538 		}
5539 		sc->tids.ntids = val[0];
5540 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5541 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5542 			sc->tids.ntids -= sc->tids.nhpftids;
5543 		}
5544 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5545 		if ((int)val[2] > (int)val[1]) {
5546 			sc->tids.stid_base = val[1];
5547 			sc->tids.nstids = val[2] - val[1] + 1;
5548 		}
5549 		sc->vres.ddp.start = val[3];
5550 		sc->vres.ddp.size = val[4] - val[3] + 1;
5551 		sc->params.ofldq_wr_cred = val[5];
5552 		sc->params.offload = 1;
5553 	} else {
5554 		/*
5555 		 * The firmware attempts memfree TOE configuration for -SO cards
5556 		 * and will report toecaps=0 if it runs out of resources (this
5557 		 * depends on the config file).  It may not report 0 for other
5558 		 * capabilities dependent on the TOE in this case.  Set them to
5559 		 * 0 here so that the driver doesn't bother tracking resources
5560 		 * that will never be used.
5561 		 */
5562 		sc->iscsicaps = 0;
5563 		sc->rdmacaps = 0;
5564 	}
5565 	if (sc->rdmacaps) {
5566 		param[0] = FW_PARAM_PFVF(STAG_START);
5567 		param[1] = FW_PARAM_PFVF(STAG_END);
5568 		param[2] = FW_PARAM_PFVF(RQ_START);
5569 		param[3] = FW_PARAM_PFVF(RQ_END);
5570 		param[4] = FW_PARAM_PFVF(PBL_START);
5571 		param[5] = FW_PARAM_PFVF(PBL_END);
5572 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5573 		if (rc != 0) {
5574 			device_printf(sc->dev,
5575 			    "failed to query RDMA parameters(1): %d.\n", rc);
5576 			return (rc);
5577 		}
5578 		sc->vres.stag.start = val[0];
5579 		sc->vres.stag.size = val[1] - val[0] + 1;
5580 		sc->vres.rq.start = val[2];
5581 		sc->vres.rq.size = val[3] - val[2] + 1;
5582 		sc->vres.pbl.start = val[4];
5583 		sc->vres.pbl.size = val[5] - val[4] + 1;
5584 
5585 		param[0] = FW_PARAM_PFVF(SQRQ_START);
5586 		param[1] = FW_PARAM_PFVF(SQRQ_END);
5587 		param[2] = FW_PARAM_PFVF(CQ_START);
5588 		param[3] = FW_PARAM_PFVF(CQ_END);
5589 		param[4] = FW_PARAM_PFVF(OCQ_START);
5590 		param[5] = FW_PARAM_PFVF(OCQ_END);
5591 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5592 		if (rc != 0) {
5593 			device_printf(sc->dev,
5594 			    "failed to query RDMA parameters(2): %d.\n", rc);
5595 			return (rc);
5596 		}
5597 		sc->vres.qp.start = val[0];
5598 		sc->vres.qp.size = val[1] - val[0] + 1;
5599 		sc->vres.cq.start = val[2];
5600 		sc->vres.cq.size = val[3] - val[2] + 1;
5601 		sc->vres.ocq.start = val[4];
5602 		sc->vres.ocq.size = val[5] - val[4] + 1;
5603 
5604 		param[0] = FW_PARAM_PFVF(SRQ_START);
5605 		param[1] = FW_PARAM_PFVF(SRQ_END);
5606 		param[2] = FW_PARAM_DEV(MAXORDIRD_QP);
5607 		param[3] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5608 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5609 		if (rc != 0) {
5610 			device_printf(sc->dev,
5611 			    "failed to query RDMA parameters(3): %d.\n", rc);
5612 			return (rc);
5613 		}
5614 		sc->vres.srq.start = val[0];
5615 		sc->vres.srq.size = val[1] - val[0] + 1;
5616 		sc->params.max_ordird_qp = val[2];
5617 		sc->params.max_ird_adapter = val[3];
5618 	}
5619 	if (sc->iscsicaps) {
5620 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5621 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5622 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5623 		if (rc != 0) {
5624 			device_printf(sc->dev,
5625 			    "failed to query iSCSI parameters: %d.\n", rc);
5626 			return (rc);
5627 		}
5628 		sc->vres.iscsi.start = val[0];
5629 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5630 	}
5631 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5632 		param[0] = FW_PARAM_PFVF(TLS_START);
5633 		param[1] = FW_PARAM_PFVF(TLS_END);
5634 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5635 		if (rc != 0) {
5636 			device_printf(sc->dev,
5637 			    "failed to query TLS parameters: %d.\n", rc);
5638 			return (rc);
5639 		}
5640 		sc->vres.key.start = val[0];
5641 		sc->vres.key.size = val[1] - val[0] + 1;
5642 	}
5643 
5644 	/*
5645 	 * We've got the params we wanted to query directly from the firmware.
5646 	 * Grab some others via other means.
5647 	 */
5648 	t4_init_sge_params(sc);
5649 	t4_init_tp_params(sc);
5650 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5651 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5652 
5653 	rc = t4_verify_chip_settings(sc);
5654 	if (rc != 0)
5655 		return (rc);
5656 	t4_init_rx_buf_info(sc);
5657 
5658 	return (rc);
5659 }
5660 
5661 #ifdef KERN_TLS
5662 static void
5663 ktls_tick(void *arg)
5664 {
5665 	struct adapter *sc;
5666 	uint32_t tstamp;
5667 
5668 	sc = arg;
5669 	tstamp = tcp_ts_getticks();
5670 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
5671 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
5672 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
5673 }
5674 
5675 static int
5676 t6_config_kern_tls(struct adapter *sc, bool enable)
5677 {
5678 	int rc;
5679 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5680 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
5681 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
5682 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
5683 
5684 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
5685 	if (rc != 0) {
5686 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
5687 		    enable ?  "enable" : "disable", rc);
5688 		return (rc);
5689 	}
5690 
5691 	if (enable) {
5692 		sc->flags |= KERN_TLS_ON;
5693 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
5694 		    C_HARDCLOCK);
5695 	} else {
5696 		sc->flags &= ~KERN_TLS_ON;
5697 		callout_stop(&sc->ktls_tick);
5698 	}
5699 
5700 	return (rc);
5701 }
5702 #endif
5703 
5704 static int
5705 set_params__post_init(struct adapter *sc)
5706 {
5707 	uint32_t mask, param, val;
5708 #ifdef TCP_OFFLOAD
5709 	int i, v, shift;
5710 #endif
5711 
5712 	/* ask for encapsulated CPLs */
5713 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5714 	val = 1;
5715 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5716 
5717 	/* Enable 32b port caps if the firmware supports it. */
5718 	param = FW_PARAM_PFVF(PORT_CAPS32);
5719 	val = 1;
5720 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
5721 		sc->params.port_caps32 = 1;
5722 
5723 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
5724 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
5725 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
5726 	    V_MASKFILTER(val - 1));
5727 
5728 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
5729 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
5730 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5731 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
5732 	val = 0;
5733 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
5734 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
5735 		    F_ATTACKFILTERENABLE);
5736 		val |= F_DROPERRORATTACK;
5737 	}
5738 	if (t4_drop_ip_fragments != 0) {
5739 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
5740 		    F_FRAGMENTDROP);
5741 		val |= F_DROPERRORFRAG;
5742 	}
5743 	if (t4_drop_pkts_with_l2_errors != 0)
5744 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
5745 	if (t4_drop_pkts_with_l3_errors != 0) {
5746 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
5747 		    F_DROPERRORCSUMIP;
5748 	}
5749 	if (t4_drop_pkts_with_l4_errors != 0) {
5750 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
5751 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
5752 	}
5753 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
5754 
5755 #ifdef TCP_OFFLOAD
5756 	/*
5757 	 * Override the TOE timers with user provided tunables.  This is not the
5758 	 * recommended way to change the timers (the firmware config file is) so
5759 	 * these tunables are not documented.
5760 	 *
5761 	 * All the timer tunables are in microseconds.
5762 	 */
5763 	if (t4_toe_keepalive_idle != 0) {
5764 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
5765 		v &= M_KEEPALIVEIDLE;
5766 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
5767 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
5768 	}
5769 	if (t4_toe_keepalive_interval != 0) {
5770 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
5771 		v &= M_KEEPALIVEINTVL;
5772 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
5773 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
5774 	}
5775 	if (t4_toe_keepalive_count != 0) {
5776 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
5777 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5778 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
5779 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
5780 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
5781 	}
5782 	if (t4_toe_rexmt_min != 0) {
5783 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
5784 		v &= M_RXTMIN;
5785 		t4_set_reg_field(sc, A_TP_RXT_MIN,
5786 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
5787 	}
5788 	if (t4_toe_rexmt_max != 0) {
5789 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
5790 		v &= M_RXTMAX;
5791 		t4_set_reg_field(sc, A_TP_RXT_MAX,
5792 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
5793 	}
5794 	if (t4_toe_rexmt_count != 0) {
5795 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
5796 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
5797 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
5798 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
5799 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
5800 	}
5801 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
5802 		if (t4_toe_rexmt_backoff[i] != -1) {
5803 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
5804 			shift = (i & 3) << 3;
5805 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
5806 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
5807 		}
5808 	}
5809 #endif
5810 
5811 	/*
5812 	 * Limit TOE connections to 2 reassembly "islands".  This is
5813 	 * required to permit migrating TOE connections to either
5814 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
5815 	 */
5816 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
5817 	    V_PASSMODE(2));
5818 
5819 #ifdef KERN_TLS
5820 	if (is_ktls(sc)) {
5821 		sc->tlst.inline_keys = t4_tls_inline_keys;
5822 		sc->tlst.combo_wrs = t4_tls_combo_wrs;
5823 		if (t4_kern_tls != 0 && is_t6(sc))
5824 			t6_config_kern_tls(sc, true);
5825 	}
5826 #endif
5827 	return (0);
5828 }
5829 
5830 #undef FW_PARAM_PFVF
5831 #undef FW_PARAM_DEV
5832 
5833 static void
5834 t4_set_desc(struct adapter *sc)
5835 {
5836 	struct adapter_params *p = &sc->params;
5837 
5838 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
5839 }
5840 
5841 static inline void
5842 ifmedia_add4(struct ifmedia *ifm, int m)
5843 {
5844 
5845 	ifmedia_add(ifm, m, 0, NULL);
5846 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
5847 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
5848 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
5849 }
5850 
5851 /*
5852  * This is the selected media, which is not quite the same as the active media.
5853  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
5854  * and active are not the same, and "media: Ethernet selected" otherwise.
5855  */
5856 static void
5857 set_current_media(struct port_info *pi)
5858 {
5859 	struct link_config *lc;
5860 	struct ifmedia *ifm;
5861 	int mword;
5862 	u_int speed;
5863 
5864 	PORT_LOCK_ASSERT_OWNED(pi);
5865 
5866 	/* Leave current media alone if it's already set to IFM_NONE. */
5867 	ifm = &pi->media;
5868 	if (ifm->ifm_cur != NULL &&
5869 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
5870 		return;
5871 
5872 	lc = &pi->link_cfg;
5873 	if (lc->requested_aneg != AUTONEG_DISABLE &&
5874 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
5875 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
5876 		return;
5877 	}
5878 	mword = IFM_ETHER | IFM_FDX;
5879 	if (lc->requested_fc & PAUSE_TX)
5880 		mword |= IFM_ETH_TXPAUSE;
5881 	if (lc->requested_fc & PAUSE_RX)
5882 		mword |= IFM_ETH_RXPAUSE;
5883 	if (lc->requested_speed == 0)
5884 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
5885 	else
5886 		speed = lc->requested_speed;
5887 	mword |= port_mword(pi, speed_to_fwcap(speed));
5888 	ifmedia_set(ifm, mword);
5889 }
5890 
5891 /*
5892  * Returns true if the ifmedia list for the port cannot change.
5893  */
5894 static bool
5895 fixed_ifmedia(struct port_info *pi)
5896 {
5897 
5898 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
5899 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
5900 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
5901 	    pi->port_type == FW_PORT_TYPE_KX4 ||
5902 	    pi->port_type == FW_PORT_TYPE_KX ||
5903 	    pi->port_type == FW_PORT_TYPE_KR ||
5904 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
5905 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
5906 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
5907 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
5908 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
5909 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
5910 }
5911 
5912 static void
5913 build_medialist(struct port_info *pi)
5914 {
5915 	uint32_t ss, speed;
5916 	int unknown, mword, bit;
5917 	struct link_config *lc;
5918 	struct ifmedia *ifm;
5919 
5920 	PORT_LOCK_ASSERT_OWNED(pi);
5921 
5922 	if (pi->flags & FIXED_IFMEDIA)
5923 		return;
5924 
5925 	/*
5926 	 * Rebuild the ifmedia list.
5927 	 */
5928 	ifm = &pi->media;
5929 	ifmedia_removeall(ifm);
5930 	lc = &pi->link_cfg;
5931 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
5932 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
5933 		MPASS(ss != 0);
5934 no_media:
5935 		MPASS(LIST_EMPTY(&ifm->ifm_list));
5936 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
5937 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
5938 		return;
5939 	}
5940 
5941 	unknown = 0;
5942 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
5943 		speed = 1 << bit;
5944 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
5945 		if (ss & speed) {
5946 			mword = port_mword(pi, speed);
5947 			if (mword == IFM_NONE) {
5948 				goto no_media;
5949 			} else if (mword == IFM_UNKNOWN)
5950 				unknown++;
5951 			else
5952 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
5953 		}
5954 	}
5955 	if (unknown > 0) /* Add one unknown for all unknown media types. */
5956 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
5957 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
5958 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
5959 
5960 	set_current_media(pi);
5961 }
5962 
5963 /*
5964  * Initialize the requested fields in the link config based on driver tunables.
5965  */
5966 static void
5967 init_link_config(struct port_info *pi)
5968 {
5969 	struct link_config *lc = &pi->link_cfg;
5970 
5971 	PORT_LOCK_ASSERT_OWNED(pi);
5972 
5973 	lc->requested_caps = 0;
5974 	lc->requested_speed = 0;
5975 
5976 	if (t4_autoneg == 0)
5977 		lc->requested_aneg = AUTONEG_DISABLE;
5978 	else if (t4_autoneg == 1)
5979 		lc->requested_aneg = AUTONEG_ENABLE;
5980 	else
5981 		lc->requested_aneg = AUTONEG_AUTO;
5982 
5983 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
5984 	    PAUSE_AUTONEG);
5985 
5986 	if (t4_fec & FEC_AUTO)
5987 		lc->requested_fec = FEC_AUTO;
5988 	else if (t4_fec == 0)
5989 		lc->requested_fec = FEC_NONE;
5990 	else {
5991 		/* -1 is handled by the FEC_AUTO block above and not here. */
5992 		lc->requested_fec = t4_fec &
5993 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
5994 		if (lc->requested_fec == 0)
5995 			lc->requested_fec = FEC_AUTO;
5996 	}
5997 	if (t4_force_fec < 0)
5998 		lc->force_fec = -1;
5999 	else if (t4_force_fec > 0)
6000 		lc->force_fec = 1;
6001 	else
6002 		lc->force_fec = 0;
6003 }
6004 
6005 /*
6006  * Makes sure that all requested settings comply with what's supported by the
6007  * port.  Returns the number of settings that were invalid and had to be fixed.
6008  */
6009 static int
6010 fixup_link_config(struct port_info *pi)
6011 {
6012 	int n = 0;
6013 	struct link_config *lc = &pi->link_cfg;
6014 	uint32_t fwspeed;
6015 
6016 	PORT_LOCK_ASSERT_OWNED(pi);
6017 
6018 	/* Speed (when not autonegotiating) */
6019 	if (lc->requested_speed != 0) {
6020 		fwspeed = speed_to_fwcap(lc->requested_speed);
6021 		if ((fwspeed & lc->pcaps) == 0) {
6022 			n++;
6023 			lc->requested_speed = 0;
6024 		}
6025 	}
6026 
6027 	/* Link autonegotiation */
6028 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6029 	    lc->requested_aneg == AUTONEG_DISABLE ||
6030 	    lc->requested_aneg == AUTONEG_AUTO);
6031 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6032 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6033 		n++;
6034 		lc->requested_aneg = AUTONEG_AUTO;
6035 	}
6036 
6037 	/* Flow control */
6038 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6039 	if (lc->requested_fc & PAUSE_TX &&
6040 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6041 		n++;
6042 		lc->requested_fc &= ~PAUSE_TX;
6043 	}
6044 	if (lc->requested_fc & PAUSE_RX &&
6045 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6046 		n++;
6047 		lc->requested_fc &= ~PAUSE_RX;
6048 	}
6049 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6050 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6051 		n++;
6052 		lc->requested_fc |= PAUSE_AUTONEG;
6053 	}
6054 
6055 	/* FEC */
6056 	if ((lc->requested_fec & FEC_RS &&
6057 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6058 	    (lc->requested_fec & FEC_BASER_RS &&
6059 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6060 		n++;
6061 		lc->requested_fec = FEC_AUTO;
6062 	}
6063 
6064 	return (n);
6065 }
6066 
6067 /*
6068  * Apply the requested L1 settings, which are expected to be valid, to the
6069  * hardware.
6070  */
6071 static int
6072 apply_link_config(struct port_info *pi)
6073 {
6074 	struct adapter *sc = pi->adapter;
6075 	struct link_config *lc = &pi->link_cfg;
6076 	int rc;
6077 
6078 #ifdef INVARIANTS
6079 	ASSERT_SYNCHRONIZED_OP(sc);
6080 	PORT_LOCK_ASSERT_OWNED(pi);
6081 
6082 	if (lc->requested_aneg == AUTONEG_ENABLE)
6083 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6084 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6085 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6086 	if (lc->requested_fc & PAUSE_TX)
6087 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6088 	if (lc->requested_fc & PAUSE_RX)
6089 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6090 	if (lc->requested_fec & FEC_RS)
6091 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6092 	if (lc->requested_fec & FEC_BASER_RS)
6093 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6094 #endif
6095 	if (!(sc->flags & IS_VF)) {
6096 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->tx_chan, lc);
6097 		if (rc != 0) {
6098 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6099 			return (rc);
6100 		}
6101 	}
6102 
6103 	/*
6104 	 * An L1_CFG will almost always result in a link-change event if the
6105 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6106 	 * the notification is processed.  If the link is down then the actual
6107 	 * settings are meaningless.
6108 	 *
6109 	 * This takes care of the case where a change in the L1 settings may not
6110 	 * result in a notification.
6111 	 */
6112 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6113 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6114 
6115 	return (0);
6116 }
6117 
6118 #define FW_MAC_EXACT_CHUNK	7
6119 struct mcaddr_ctx {
6120 	if_t ifp;
6121 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6122 	uint64_t hash;
6123 	int i;
6124 	int del;
6125 	int rc;
6126 };
6127 
6128 static u_int
6129 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6130 {
6131 	struct mcaddr_ctx *ctx = arg;
6132 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6133 	struct port_info *pi = vi->pi;
6134 	struct adapter *sc = pi->adapter;
6135 
6136 	if (ctx->rc < 0)
6137 		return (0);
6138 
6139 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6140 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6141 	ctx->i++;
6142 
6143 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6144 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6145 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6146 		if (ctx->rc < 0) {
6147 			int j;
6148 
6149 			for (j = 0; j < ctx->i; j++) {
6150 				if_printf(ctx->ifp,
6151 				    "failed to add mc address"
6152 				    " %02x:%02x:%02x:"
6153 				    "%02x:%02x:%02x rc=%d\n",
6154 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6155 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6156 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6157 				    -ctx->rc);
6158 			}
6159 			return (0);
6160 		}
6161 		ctx->del = 0;
6162 		ctx->i = 0;
6163 	}
6164 
6165 	return (1);
6166 }
6167 
6168 /*
6169  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6170  * indicates which parameters should be programmed (the rest are left alone).
6171  */
6172 int
6173 update_mac_settings(if_t ifp, int flags)
6174 {
6175 	int rc = 0;
6176 	struct vi_info *vi = if_getsoftc(ifp);
6177 	struct port_info *pi = vi->pi;
6178 	struct adapter *sc = pi->adapter;
6179 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6180 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6181 
6182 	ASSERT_SYNCHRONIZED_OP(sc);
6183 	KASSERT(flags, ("%s: not told what to update.", __func__));
6184 
6185 	if (flags & XGMAC_MTU)
6186 		mtu = if_getmtu(ifp);
6187 
6188 	if (flags & XGMAC_PROMISC)
6189 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6190 
6191 	if (flags & XGMAC_ALLMULTI)
6192 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6193 
6194 	if (flags & XGMAC_VLANEX)
6195 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6196 
6197 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6198 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6199 		    allmulti, 1, vlanex, false);
6200 		if (rc) {
6201 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6202 			    rc);
6203 			return (rc);
6204 		}
6205 	}
6206 
6207 	if (flags & XGMAC_UCADDR) {
6208 		uint8_t ucaddr[ETHER_ADDR_LEN];
6209 
6210 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6211 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6212 		    ucaddr, true, &vi->smt_idx);
6213 		if (rc < 0) {
6214 			rc = -rc;
6215 			if_printf(ifp, "change_mac failed: %d\n", rc);
6216 			return (rc);
6217 		} else {
6218 			vi->xact_addr_filt = rc;
6219 			rc = 0;
6220 		}
6221 	}
6222 
6223 	if (flags & XGMAC_MCADDRS) {
6224 		struct epoch_tracker et;
6225 		struct mcaddr_ctx ctx;
6226 		int j;
6227 
6228 		ctx.ifp = ifp;
6229 		ctx.hash = 0;
6230 		ctx.i = 0;
6231 		ctx.del = 1;
6232 		ctx.rc = 0;
6233 		/*
6234 		 * Unlike other drivers, we accumulate list of pointers into
6235 		 * interface address lists and we need to keep it safe even
6236 		 * after if_foreach_llmaddr() returns, thus we must enter the
6237 		 * network epoch.
6238 		 */
6239 		NET_EPOCH_ENTER(et);
6240 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6241 		if (ctx.rc < 0) {
6242 			NET_EPOCH_EXIT(et);
6243 			rc = -ctx.rc;
6244 			return (rc);
6245 		}
6246 		if (ctx.i > 0) {
6247 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6248 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6249 			NET_EPOCH_EXIT(et);
6250 			if (rc < 0) {
6251 				rc = -rc;
6252 				for (j = 0; j < ctx.i; j++) {
6253 					if_printf(ifp,
6254 					    "failed to add mcast address"
6255 					    " %02x:%02x:%02x:"
6256 					    "%02x:%02x:%02x rc=%d\n",
6257 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6258 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6259 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6260 					    rc);
6261 				}
6262 				return (rc);
6263 			}
6264 			ctx.del = 0;
6265 		} else
6266 			NET_EPOCH_EXIT(et);
6267 
6268 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6269 		if (rc != 0)
6270 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6271 			    rc);
6272 		if (ctx.del == 0) {
6273 			/* We clobbered the VXLAN entry if there was one. */
6274 			pi->vxlan_tcam_entry = false;
6275 		}
6276 	}
6277 
6278 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6279 	    pi->vxlan_tcam_entry == false) {
6280 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6281 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6282 		    true);
6283 		if (rc < 0) {
6284 			rc = -rc;
6285 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6286 			    rc);
6287 		} else {
6288 			MPASS(rc == sc->rawf_base + pi->port_id);
6289 			rc = 0;
6290 			pi->vxlan_tcam_entry = true;
6291 		}
6292 	}
6293 
6294 	return (rc);
6295 }
6296 
6297 /*
6298  * {begin|end}_synchronized_op must be called from the same thread.
6299  */
6300 int
6301 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6302     char *wmesg)
6303 {
6304 	int rc, pri;
6305 
6306 #ifdef WITNESS
6307 	/* the caller thinks it's ok to sleep, but is it really? */
6308 	if (flags & SLEEP_OK)
6309 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
6310 		    "begin_synchronized_op");
6311 #endif
6312 
6313 	if (INTR_OK)
6314 		pri = PCATCH;
6315 	else
6316 		pri = 0;
6317 
6318 	ADAPTER_LOCK(sc);
6319 	for (;;) {
6320 
6321 		if (vi && IS_DETACHING(vi)) {
6322 			rc = ENXIO;
6323 			goto done;
6324 		}
6325 
6326 		if (!IS_BUSY(sc)) {
6327 			rc = 0;
6328 			break;
6329 		}
6330 
6331 		if (!(flags & SLEEP_OK)) {
6332 			rc = EBUSY;
6333 			goto done;
6334 		}
6335 
6336 		if (mtx_sleep(&sc->flags, &sc->sc_lock, pri, wmesg, 0)) {
6337 			rc = EINTR;
6338 			goto done;
6339 		}
6340 	}
6341 
6342 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6343 	SET_BUSY(sc);
6344 #ifdef INVARIANTS
6345 	sc->last_op = wmesg;
6346 	sc->last_op_thr = curthread;
6347 	sc->last_op_flags = flags;
6348 #endif
6349 
6350 done:
6351 	if (!(flags & HOLD_LOCK) || rc)
6352 		ADAPTER_UNLOCK(sc);
6353 
6354 	return (rc);
6355 }
6356 
6357 /*
6358  * Tell if_ioctl and if_init that the VI is going away.  This is
6359  * special variant of begin_synchronized_op and must be paired with a
6360  * call to end_vi_detach.
6361  */
6362 void
6363 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6364 {
6365 	ADAPTER_LOCK(sc);
6366 	SET_DETACHING(vi);
6367 	wakeup(&sc->flags);
6368 	while (IS_BUSY(sc))
6369 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6370 	SET_BUSY(sc);
6371 #ifdef INVARIANTS
6372 	sc->last_op = "t4detach";
6373 	sc->last_op_thr = curthread;
6374 	sc->last_op_flags = 0;
6375 #endif
6376 	ADAPTER_UNLOCK(sc);
6377 }
6378 
6379 void
6380 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6381 {
6382 	ADAPTER_LOCK(sc);
6383 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6384 	CLR_BUSY(sc);
6385 	CLR_DETACHING(vi);
6386 	wakeup(&sc->flags);
6387 	ADAPTER_UNLOCK(sc);
6388 }
6389 
6390 /*
6391  * {begin|end}_synchronized_op must be called from the same thread.
6392  */
6393 void
6394 end_synchronized_op(struct adapter *sc, int flags)
6395 {
6396 
6397 	if (flags & LOCK_HELD)
6398 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6399 	else
6400 		ADAPTER_LOCK(sc);
6401 
6402 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6403 	CLR_BUSY(sc);
6404 	wakeup(&sc->flags);
6405 	ADAPTER_UNLOCK(sc);
6406 }
6407 
6408 static int
6409 cxgbe_init_synchronized(struct vi_info *vi)
6410 {
6411 	struct port_info *pi = vi->pi;
6412 	struct adapter *sc = pi->adapter;
6413 	if_t ifp = vi->ifp;
6414 	int rc = 0, i;
6415 	struct sge_txq *txq;
6416 
6417 	ASSERT_SYNCHRONIZED_OP(sc);
6418 
6419 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6420 		return (0);	/* already running */
6421 
6422 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6423 		return (rc);	/* error message displayed already */
6424 
6425 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6426 		return (rc); /* error message displayed already */
6427 
6428 	rc = update_mac_settings(ifp, XGMAC_ALL);
6429 	if (rc)
6430 		goto done;	/* error message displayed already */
6431 
6432 	PORT_LOCK(pi);
6433 	if (pi->up_vis == 0) {
6434 		t4_update_port_info(pi);
6435 		fixup_link_config(pi);
6436 		build_medialist(pi);
6437 		apply_link_config(pi);
6438 	}
6439 
6440 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6441 	if (rc != 0) {
6442 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6443 		PORT_UNLOCK(pi);
6444 		goto done;
6445 	}
6446 
6447 	/*
6448 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6449 	 * if this changes.
6450 	 */
6451 
6452 	for_each_txq(vi, i, txq) {
6453 		TXQ_LOCK(txq);
6454 		txq->eq.flags |= EQ_ENABLED;
6455 		TXQ_UNLOCK(txq);
6456 	}
6457 
6458 	/*
6459 	 * The first iq of the first port to come up is used for tracing.
6460 	 */
6461 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6462 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6463 		t4_write_reg(sc, is_t4(sc) ?  A_MPS_TRC_RSS_CONTROL :
6464 		    A_MPS_T5_TRC_RSS_CONTROL, V_RSSCONTROL(pi->tx_chan) |
6465 		    V_QUEUENUMBER(sc->traceq));
6466 		pi->flags |= HAS_TRACEQ;
6467 	}
6468 
6469 	/* all ok */
6470 	pi->up_vis++;
6471 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6472 	if (pi->link_cfg.link_ok)
6473 		t4_os_link_changed(pi);
6474 	PORT_UNLOCK(pi);
6475 
6476 	mtx_lock(&vi->tick_mtx);
6477 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6478 		callout_reset(&vi->tick, hz, vi_tick, vi);
6479 	else
6480 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6481 	mtx_unlock(&vi->tick_mtx);
6482 done:
6483 	if (rc != 0)
6484 		cxgbe_uninit_synchronized(vi);
6485 
6486 	return (rc);
6487 }
6488 
6489 /*
6490  * Idempotent.
6491  */
6492 static int
6493 cxgbe_uninit_synchronized(struct vi_info *vi)
6494 {
6495 	struct port_info *pi = vi->pi;
6496 	struct adapter *sc = pi->adapter;
6497 	if_t ifp = vi->ifp;
6498 	int rc, i;
6499 	struct sge_txq *txq;
6500 
6501 	ASSERT_SYNCHRONIZED_OP(sc);
6502 
6503 	if (!(vi->flags & VI_INIT_DONE)) {
6504 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6505 			KASSERT(0, ("uninited VI is running"));
6506 			if_printf(ifp, "uninited VI with running ifnet.  "
6507 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6508 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6509 			    if_getdrvflags(ifp));
6510 		}
6511 		return (0);
6512 	}
6513 
6514 	/*
6515 	 * Disable the VI so that all its data in either direction is discarded
6516 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6517 	 * tick) intact as the TP can deliver negative advice or data that it's
6518 	 * holding in its RAM (for an offloaded connection) even after the VI is
6519 	 * disabled.
6520 	 */
6521 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6522 	if (rc) {
6523 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6524 		return (rc);
6525 	}
6526 
6527 	for_each_txq(vi, i, txq) {
6528 		TXQ_LOCK(txq);
6529 		txq->eq.flags &= ~EQ_ENABLED;
6530 		TXQ_UNLOCK(txq);
6531 	}
6532 
6533 	mtx_lock(&vi->tick_mtx);
6534 	callout_stop(&vi->tick);
6535 	mtx_unlock(&vi->tick_mtx);
6536 
6537 	PORT_LOCK(pi);
6538 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6539 		PORT_UNLOCK(pi);
6540 		return (0);
6541 	}
6542 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6543 	pi->up_vis--;
6544 	if (pi->up_vis > 0) {
6545 		PORT_UNLOCK(pi);
6546 		return (0);
6547 	}
6548 
6549 	pi->link_cfg.link_ok = false;
6550 	pi->link_cfg.speed = 0;
6551 	pi->link_cfg.link_down_rc = 255;
6552 	t4_os_link_changed(pi);
6553 	PORT_UNLOCK(pi);
6554 
6555 	return (0);
6556 }
6557 
6558 /*
6559  * It is ok for this function to fail midway and return right away.  t4_detach
6560  * will walk the entire sc->irq list and clean up whatever is valid.
6561  */
6562 int
6563 t4_setup_intr_handlers(struct adapter *sc)
6564 {
6565 	int rc, rid, p, q, v;
6566 	char s[8];
6567 	struct irq *irq;
6568 	struct port_info *pi;
6569 	struct vi_info *vi;
6570 	struct sge *sge = &sc->sge;
6571 	struct sge_rxq *rxq;
6572 #ifdef TCP_OFFLOAD
6573 	struct sge_ofld_rxq *ofld_rxq;
6574 #endif
6575 #ifdef DEV_NETMAP
6576 	struct sge_nm_rxq *nm_rxq;
6577 #endif
6578 #ifdef RSS
6579 	int nbuckets = rss_getnumbuckets();
6580 #endif
6581 
6582 	/*
6583 	 * Setup interrupts.
6584 	 */
6585 	irq = &sc->irq[0];
6586 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6587 	if (forwarding_intr_to_fwq(sc))
6588 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6589 
6590 	/* Multiple interrupts. */
6591 	if (sc->flags & IS_VF)
6592 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6593 		    ("%s: too few intr.", __func__));
6594 	else
6595 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6596 		    ("%s: too few intr.", __func__));
6597 
6598 	/* The first one is always error intr on PFs */
6599 	if (!(sc->flags & IS_VF)) {
6600 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6601 		if (rc != 0)
6602 			return (rc);
6603 		irq++;
6604 		rid++;
6605 	}
6606 
6607 	/* The second one is always the firmware event queue (first on VFs) */
6608 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6609 	if (rc != 0)
6610 		return (rc);
6611 	irq++;
6612 	rid++;
6613 
6614 	for_each_port(sc, p) {
6615 		pi = sc->port[p];
6616 		for_each_vi(pi, v, vi) {
6617 			vi->first_intr = rid - 1;
6618 
6619 			if (vi->nnmrxq > 0) {
6620 				int n = max(vi->nrxq, vi->nnmrxq);
6621 
6622 				rxq = &sge->rxq[vi->first_rxq];
6623 #ifdef DEV_NETMAP
6624 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6625 #endif
6626 				for (q = 0; q < n; q++) {
6627 					snprintf(s, sizeof(s), "%x%c%x", p,
6628 					    'a' + v, q);
6629 					if (q < vi->nrxq)
6630 						irq->rxq = rxq++;
6631 #ifdef DEV_NETMAP
6632 					if (q < vi->nnmrxq)
6633 						irq->nm_rxq = nm_rxq++;
6634 
6635 					if (irq->nm_rxq != NULL &&
6636 					    irq->rxq == NULL) {
6637 						/* Netmap rx only */
6638 						rc = t4_alloc_irq(sc, irq, rid,
6639 						    t4_nm_intr, irq->nm_rxq, s);
6640 					}
6641 					if (irq->nm_rxq != NULL &&
6642 					    irq->rxq != NULL) {
6643 						/* NIC and Netmap rx */
6644 						rc = t4_alloc_irq(sc, irq, rid,
6645 						    t4_vi_intr, irq, s);
6646 					}
6647 #endif
6648 					if (irq->rxq != NULL &&
6649 					    irq->nm_rxq == NULL) {
6650 						/* NIC rx only */
6651 						rc = t4_alloc_irq(sc, irq, rid,
6652 						    t4_intr, irq->rxq, s);
6653 					}
6654 					if (rc != 0)
6655 						return (rc);
6656 #ifdef RSS
6657 					if (q < vi->nrxq) {
6658 						bus_bind_intr(sc->dev, irq->res,
6659 						    rss_getcpu(q % nbuckets));
6660 					}
6661 #endif
6662 					irq++;
6663 					rid++;
6664 					vi->nintr++;
6665 				}
6666 			} else {
6667 				for_each_rxq(vi, q, rxq) {
6668 					snprintf(s, sizeof(s), "%x%c%x", p,
6669 					    'a' + v, q);
6670 					rc = t4_alloc_irq(sc, irq, rid,
6671 					    t4_intr, rxq, s);
6672 					if (rc != 0)
6673 						return (rc);
6674 #ifdef RSS
6675 					bus_bind_intr(sc->dev, irq->res,
6676 					    rss_getcpu(q % nbuckets));
6677 #endif
6678 					irq++;
6679 					rid++;
6680 					vi->nintr++;
6681 				}
6682 			}
6683 #ifdef TCP_OFFLOAD
6684 			for_each_ofld_rxq(vi, q, ofld_rxq) {
6685 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
6686 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
6687 				    ofld_rxq, s);
6688 				if (rc != 0)
6689 					return (rc);
6690 				irq++;
6691 				rid++;
6692 				vi->nintr++;
6693 			}
6694 #endif
6695 		}
6696 	}
6697 	MPASS(irq == &sc->irq[sc->intr_count]);
6698 
6699 	return (0);
6700 }
6701 
6702 static void
6703 write_global_rss_key(struct adapter *sc)
6704 {
6705 #ifdef RSS
6706 	int i;
6707 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6708 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
6709 
6710 	CTASSERT(RSS_KEYSIZE == 40);
6711 
6712 	rss_getkey((void *)&raw_rss_key[0]);
6713 	for (i = 0; i < nitems(rss_key); i++) {
6714 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
6715 	}
6716 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
6717 #endif
6718 }
6719 
6720 /*
6721  * Idempotent.
6722  */
6723 static int
6724 adapter_full_init(struct adapter *sc)
6725 {
6726 	int rc, i;
6727 
6728 	ASSERT_SYNCHRONIZED_OP(sc);
6729 
6730 	/*
6731 	 * queues that belong to the adapter (not any particular port).
6732 	 */
6733 	rc = t4_setup_adapter_queues(sc);
6734 	if (rc != 0)
6735 		return (rc);
6736 
6737 	MPASS(sc->params.nports <= nitems(sc->tq));
6738 	for (i = 0; i < sc->params.nports; i++) {
6739 		if (sc->tq[i] != NULL)
6740 			continue;
6741 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
6742 		    taskqueue_thread_enqueue, &sc->tq[i]);
6743 		if (sc->tq[i] == NULL) {
6744 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
6745 			return (ENOMEM);
6746 		}
6747 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
6748 		    device_get_nameunit(sc->dev), i);
6749 	}
6750 
6751 	if (!(sc->flags & IS_VF)) {
6752 		write_global_rss_key(sc);
6753 		t4_intr_enable(sc);
6754 	}
6755 	return (0);
6756 }
6757 
6758 int
6759 adapter_init(struct adapter *sc)
6760 {
6761 	int rc;
6762 
6763 	ASSERT_SYNCHRONIZED_OP(sc);
6764 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
6765 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
6766 	    ("%s: FULL_INIT_DONE already", __func__));
6767 
6768 	rc = adapter_full_init(sc);
6769 	if (rc != 0)
6770 		adapter_full_uninit(sc);
6771 	else
6772 		sc->flags |= FULL_INIT_DONE;
6773 
6774 	return (rc);
6775 }
6776 
6777 /*
6778  * Idempotent.
6779  */
6780 static void
6781 adapter_full_uninit(struct adapter *sc)
6782 {
6783 	int i;
6784 
6785 	t4_teardown_adapter_queues(sc);
6786 
6787 	for (i = 0; i < nitems(sc->tq); i++) {
6788 		if (sc->tq[i] == NULL)
6789 			continue;
6790 		taskqueue_free(sc->tq[i]);
6791 		sc->tq[i] = NULL;
6792 	}
6793 
6794 	sc->flags &= ~FULL_INIT_DONE;
6795 }
6796 
6797 #ifdef RSS
6798 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
6799     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
6800     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
6801     RSS_HASHTYPE_RSS_UDP_IPV6)
6802 
6803 /* Translates kernel hash types to hardware. */
6804 static int
6805 hashconfig_to_hashen(int hashconfig)
6806 {
6807 	int hashen = 0;
6808 
6809 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
6810 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
6811 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
6812 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
6813 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
6814 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6815 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6816 	}
6817 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
6818 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
6819 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6820 	}
6821 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
6822 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
6823 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
6824 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
6825 
6826 	return (hashen);
6827 }
6828 
6829 /* Translates hardware hash types to kernel. */
6830 static int
6831 hashen_to_hashconfig(int hashen)
6832 {
6833 	int hashconfig = 0;
6834 
6835 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
6836 		/*
6837 		 * If UDP hashing was enabled it must have been enabled for
6838 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
6839 		 * enabling any 4-tuple hash is nonsense configuration.
6840 		 */
6841 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6842 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
6843 
6844 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6845 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
6846 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6847 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
6848 	}
6849 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
6850 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
6851 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
6852 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
6853 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
6854 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
6855 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
6856 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
6857 
6858 	return (hashconfig);
6859 }
6860 #endif
6861 
6862 /*
6863  * Idempotent.
6864  */
6865 static int
6866 vi_full_init(struct vi_info *vi)
6867 {
6868 	struct adapter *sc = vi->adapter;
6869 	struct sge_rxq *rxq;
6870 	int rc, i, j;
6871 #ifdef RSS
6872 	int nbuckets = rss_getnumbuckets();
6873 	int hashconfig = rss_gethashconfig();
6874 	int extra;
6875 #endif
6876 
6877 	ASSERT_SYNCHRONIZED_OP(sc);
6878 
6879 	/*
6880 	 * Allocate tx/rx/fl queues for this VI.
6881 	 */
6882 	rc = t4_setup_vi_queues(vi);
6883 	if (rc != 0)
6884 		return (rc);
6885 
6886 	/*
6887 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
6888 	 */
6889 	if (vi->nrxq > vi->rss_size) {
6890 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
6891 		    "some queues will never receive traffic.\n", vi->nrxq,
6892 		    vi->rss_size);
6893 	} else if (vi->rss_size % vi->nrxq) {
6894 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
6895 		    "expect uneven traffic distribution.\n", vi->nrxq,
6896 		    vi->rss_size);
6897 	}
6898 #ifdef RSS
6899 	if (vi->nrxq != nbuckets) {
6900 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
6901 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
6902 	}
6903 #endif
6904 	if (vi->rss == NULL)
6905 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
6906 		    M_ZERO | M_WAITOK);
6907 	for (i = 0; i < vi->rss_size;) {
6908 #ifdef RSS
6909 		j = rss_get_indirection_to_bucket(i);
6910 		j %= vi->nrxq;
6911 		rxq = &sc->sge.rxq[vi->first_rxq + j];
6912 		vi->rss[i++] = rxq->iq.abs_id;
6913 #else
6914 		for_each_rxq(vi, j, rxq) {
6915 			vi->rss[i++] = rxq->iq.abs_id;
6916 			if (i == vi->rss_size)
6917 				break;
6918 		}
6919 #endif
6920 	}
6921 
6922 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
6923 	    vi->rss, vi->rss_size);
6924 	if (rc != 0) {
6925 		CH_ERR(vi, "rss_config failed: %d\n", rc);
6926 		return (rc);
6927 	}
6928 
6929 #ifdef RSS
6930 	vi->hashen = hashconfig_to_hashen(hashconfig);
6931 
6932 	/*
6933 	 * We may have had to enable some hashes even though the global config
6934 	 * wants them disabled.  This is a potential problem that must be
6935 	 * reported to the user.
6936 	 */
6937 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
6938 
6939 	/*
6940 	 * If we consider only the supported hash types, then the enabled hashes
6941 	 * are a superset of the requested hashes.  In other words, there cannot
6942 	 * be any supported hash that was requested but not enabled, but there
6943 	 * can be hashes that were not requested but had to be enabled.
6944 	 */
6945 	extra &= SUPPORTED_RSS_HASHTYPES;
6946 	MPASS((extra & hashconfig) == 0);
6947 
6948 	if (extra) {
6949 		CH_ALERT(vi,
6950 		    "global RSS config (0x%x) cannot be accommodated.\n",
6951 		    hashconfig);
6952 	}
6953 	if (extra & RSS_HASHTYPE_RSS_IPV4)
6954 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
6955 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
6956 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
6957 	if (extra & RSS_HASHTYPE_RSS_IPV6)
6958 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
6959 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
6960 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
6961 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
6962 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
6963 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
6964 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
6965 #else
6966 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
6967 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
6968 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
6969 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
6970 #endif
6971 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
6972 	    0, 0);
6973 	if (rc != 0) {
6974 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
6975 		return (rc);
6976 	}
6977 
6978 	return (0);
6979 }
6980 
6981 int
6982 vi_init(struct vi_info *vi)
6983 {
6984 	int rc;
6985 
6986 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
6987 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
6988 	    ("%s: VI_INIT_DONE already", __func__));
6989 
6990 	rc = vi_full_init(vi);
6991 	if (rc != 0)
6992 		vi_full_uninit(vi);
6993 	else
6994 		vi->flags |= VI_INIT_DONE;
6995 
6996 	return (rc);
6997 }
6998 
6999 /*
7000  * Idempotent.
7001  */
7002 static void
7003 vi_full_uninit(struct vi_info *vi)
7004 {
7005 
7006 	if (vi->flags & VI_INIT_DONE) {
7007 		quiesce_vi(vi);
7008 		free(vi->rss, M_CXGBE);
7009 		free(vi->nm_rss, M_CXGBE);
7010 	}
7011 
7012 	t4_teardown_vi_queues(vi);
7013 	vi->flags &= ~VI_INIT_DONE;
7014 }
7015 
7016 static void
7017 quiesce_txq(struct sge_txq *txq)
7018 {
7019 	struct sge_eq *eq = &txq->eq;
7020 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7021 
7022 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7023 	MPASS(!(eq->flags & EQ_ENABLED));
7024 
7025 	/* Wait for the mp_ring to empty. */
7026 	while (!mp_ring_is_idle(txq->r)) {
7027 		mp_ring_check_drainage(txq->r, 4096);
7028 		pause("rquiesce", 1);
7029 	}
7030 	MPASS(txq->txp.npkt == 0);
7031 
7032 	if (eq->flags & EQ_HW_ALLOCATED) {
7033 		/*
7034 		 * Hardware is alive and working normally.  Wait for it to
7035 		 * finish and then wait for the driver to catch up and reclaim
7036 		 * all descriptors.
7037 		 */
7038 		while (spg->cidx != htobe16(eq->pidx))
7039 			pause("equiesce", 1);
7040 		while (eq->cidx != eq->pidx)
7041 			pause("dquiesce", 1);
7042 	} else {
7043 		/*
7044 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7045 		 * descriptors directly.
7046 		 */
7047 		TXQ_LOCK(txq);
7048 		while (eq->cidx != eq->pidx) {
7049 			struct mbuf *m, *nextpkt;
7050 			struct tx_sdesc *txsd;
7051 
7052 			txsd = &txq->sdesc[eq->cidx];
7053 			for (m = txsd->m; m != NULL; m = nextpkt) {
7054 				nextpkt = m->m_nextpkt;
7055 				m->m_nextpkt = NULL;
7056 				m_freem(m);
7057 			}
7058 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7059 		}
7060 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7061 		TXQ_UNLOCK(txq);
7062 	}
7063 }
7064 
7065 static void
7066 quiesce_wrq(struct sge_wrq *wrq)
7067 {
7068 	struct wrqe *wr;
7069 
7070 	TXQ_LOCK(wrq);
7071 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7072 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7073 #ifdef INVARIANTS
7074 		wrq->nwr_pending--;
7075 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7076 #endif
7077 		free(wr, M_CXGBE);
7078 	}
7079 	MPASS(wrq->nwr_pending == 0);
7080 	MPASS(wrq->ndesc_needed == 0);
7081 	wrq->nwr_pending = 0;
7082 	wrq->ndesc_needed = 0;
7083 	TXQ_UNLOCK(wrq);
7084 }
7085 
7086 static void
7087 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7088 {
7089 	/* Synchronize with the interrupt handler */
7090 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7091 		pause("iqfree", 1);
7092 
7093 	if (fl != NULL) {
7094 		MPASS(iq->flags & IQ_HAS_FL);
7095 
7096 		mtx_lock(&sc->sfl_lock);
7097 		FL_LOCK(fl);
7098 		fl->flags |= FL_DOOMED;
7099 		FL_UNLOCK(fl);
7100 		callout_stop(&sc->sfl_callout);
7101 		mtx_unlock(&sc->sfl_lock);
7102 
7103 		KASSERT((fl->flags & FL_STARVING) == 0,
7104 		    ("%s: still starving", __func__));
7105 
7106 		/* Release all buffers if hardware is no longer available. */
7107 		if (!(iq->flags & IQ_HW_ALLOCATED))
7108 			free_fl_buffers(sc, fl);
7109 	}
7110 }
7111 
7112 /*
7113  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7114  * that no new work is being enqueued by the hardware or the driver.  That part
7115  * should be arranged before calling this function.
7116  */
7117 static void
7118 quiesce_vi(struct vi_info *vi)
7119 {
7120 	int i;
7121 	struct adapter *sc = vi->adapter;
7122 	struct sge_rxq *rxq;
7123 	struct sge_txq *txq;
7124 #ifdef TCP_OFFLOAD
7125 	struct sge_ofld_rxq *ofld_rxq;
7126 #endif
7127 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7128 	struct sge_ofld_txq *ofld_txq;
7129 #endif
7130 
7131 	if (!(vi->flags & VI_INIT_DONE))
7132 		return;
7133 
7134 	for_each_txq(vi, i, txq) {
7135 		quiesce_txq(txq);
7136 	}
7137 
7138 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7139 	for_each_ofld_txq(vi, i, ofld_txq) {
7140 		quiesce_wrq(&ofld_txq->wrq);
7141 	}
7142 #endif
7143 
7144 	for_each_rxq(vi, i, rxq) {
7145 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7146 	}
7147 
7148 #ifdef TCP_OFFLOAD
7149 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7150 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7151 	}
7152 #endif
7153 }
7154 
7155 static int
7156 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7157     driver_intr_t *handler, void *arg, char *name)
7158 {
7159 	int rc;
7160 
7161 	irq->rid = rid;
7162 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7163 	    RF_SHAREABLE | RF_ACTIVE);
7164 	if (irq->res == NULL) {
7165 		device_printf(sc->dev,
7166 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7167 		return (ENOMEM);
7168 	}
7169 
7170 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7171 	    NULL, handler, arg, &irq->tag);
7172 	if (rc != 0) {
7173 		device_printf(sc->dev,
7174 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7175 		    rid, name, rc);
7176 	} else if (name)
7177 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7178 
7179 	return (rc);
7180 }
7181 
7182 static int
7183 t4_free_irq(struct adapter *sc, struct irq *irq)
7184 {
7185 	if (irq->tag)
7186 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7187 	if (irq->res)
7188 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7189 
7190 	bzero(irq, sizeof(*irq));
7191 
7192 	return (0);
7193 }
7194 
7195 static void
7196 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7197 {
7198 
7199 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7200 	t4_get_regs(sc, buf, regs->len);
7201 }
7202 
7203 #define	A_PL_INDIR_CMD	0x1f8
7204 
7205 #define	S_PL_AUTOINC	31
7206 #define	M_PL_AUTOINC	0x1U
7207 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7208 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7209 
7210 #define	S_PL_VFID	20
7211 #define	M_PL_VFID	0xffU
7212 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7213 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7214 
7215 #define	S_PL_ADDR	0
7216 #define	M_PL_ADDR	0xfffffU
7217 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7218 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7219 
7220 #define	A_PL_INDIR_DATA	0x1fc
7221 
7222 static uint64_t
7223 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7224 {
7225 	u32 stats[2];
7226 
7227 	if (sc->flags & IS_VF) {
7228 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7229 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7230 	} else {
7231 		mtx_assert(&sc->reg_lock, MA_OWNED);
7232 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7233 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7234 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7235 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7236 	}
7237 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7238 }
7239 
7240 static void
7241 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7242 {
7243 
7244 #define GET_STAT(name) \
7245 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7246 
7247 	if (!(sc->flags & IS_VF))
7248 		mtx_lock(&sc->reg_lock);
7249 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7250 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7251 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7252 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7253 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7254 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7255 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7256 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7257 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7258 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7259 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7260 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7261 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7262 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7263 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7264 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7265 	if (!(sc->flags & IS_VF))
7266 		mtx_unlock(&sc->reg_lock);
7267 
7268 #undef GET_STAT
7269 }
7270 
7271 static void
7272 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7273 {
7274 	int reg;
7275 
7276 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7277 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7278 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7279 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7280 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7281 }
7282 
7283 static void
7284 vi_refresh_stats(struct vi_info *vi)
7285 {
7286 	struct timeval tv;
7287 	const struct timeval interval = {0, 250000};	/* 250ms */
7288 
7289 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7290 
7291 	if (vi->flags & VI_SKIP_STATS)
7292 		return;
7293 
7294 	getmicrotime(&tv);
7295 	timevalsub(&tv, &interval);
7296 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7297 		return;
7298 
7299 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7300 	getmicrotime(&vi->last_refreshed);
7301 }
7302 
7303 static void
7304 cxgbe_refresh_stats(struct vi_info *vi)
7305 {
7306 	u_int i, v, tnl_cong_drops, chan_map;
7307 	struct timeval tv;
7308 	const struct timeval interval = {0, 250000};	/* 250ms */
7309 	struct port_info *pi;
7310 	struct adapter *sc;
7311 
7312 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7313 
7314 	if (vi->flags & VI_SKIP_STATS)
7315 		return;
7316 
7317 	getmicrotime(&tv);
7318 	timevalsub(&tv, &interval);
7319 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7320 		return;
7321 
7322 	pi = vi->pi;
7323 	sc = vi->adapter;
7324 	tnl_cong_drops = 0;
7325 	t4_get_port_stats(sc, pi->port_id, &pi->stats);
7326 	chan_map = pi->rx_e_chan_map;
7327 	while (chan_map) {
7328 		i = ffs(chan_map) - 1;
7329 		mtx_lock(&sc->reg_lock);
7330 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7331 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7332 		mtx_unlock(&sc->reg_lock);
7333 		tnl_cong_drops += v;
7334 		chan_map &= ~(1 << i);
7335 	}
7336 	pi->tnl_cong_drops = tnl_cong_drops;
7337 	getmicrotime(&vi->last_refreshed);
7338 }
7339 
7340 static void
7341 cxgbe_tick(void *arg)
7342 {
7343 	struct vi_info *vi = arg;
7344 
7345 	MPASS(IS_MAIN_VI(vi));
7346 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7347 
7348 	cxgbe_refresh_stats(vi);
7349 	callout_schedule(&vi->tick, hz);
7350 }
7351 
7352 static void
7353 vi_tick(void *arg)
7354 {
7355 	struct vi_info *vi = arg;
7356 
7357 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7358 
7359 	vi_refresh_stats(vi);
7360 	callout_schedule(&vi->tick, hz);
7361 }
7362 
7363 /*
7364  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7365  */
7366 static char *caps_decoder[] = {
7367 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7368 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7369 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7370 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7371 	    "\006HASHFILTER\007ETHOFLD",
7372 	"\20\001TOE",					/* 4: TOE */
7373 	"\20\001RDDP\002RDMAC",				/* 5: RDMA */
7374 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7375 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7376 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7377 	    "\007T10DIF"
7378 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7379 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7380 	    "\004TLS_HW",
7381 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7382 		    "\004PO_INITIATOR\005PO_TARGET",
7383 };
7384 
7385 void
7386 t4_sysctls(struct adapter *sc)
7387 {
7388 	struct sysctl_ctx_list *ctx = &sc->ctx;
7389 	struct sysctl_oid *oid;
7390 	struct sysctl_oid_list *children, *c0;
7391 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7392 
7393 	/*
7394 	 * dev.t4nex.X.
7395 	 */
7396 	oid = device_get_sysctl_tree(sc->dev);
7397 	c0 = children = SYSCTL_CHILDREN(oid);
7398 
7399 	sc->sc_do_rxcopy = 1;
7400 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7401 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7402 
7403 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7404 	    sc->params.nports, "# of ports");
7405 
7406 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7407 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7408 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7409 	    "available doorbells");
7410 
7411 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7412 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7413 
7414 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7415 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7416 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7417 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7418 
7419 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7420 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7421 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7422 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7423 
7424 	t4_sge_sysctls(sc, ctx, children);
7425 
7426 	sc->lro_timeout = 100;
7427 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7428 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7429 
7430 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7431 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7432 
7433 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7434 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7435 
7436 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7437 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7438 
7439 	if (sc->flags & IS_VF)
7440 		return;
7441 
7442 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7443 	    NULL, chip_rev(sc), "chip hardware revision");
7444 
7445 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7446 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7447 
7448 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7449 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7450 
7451 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7452 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7453 
7454 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7455 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7456 
7457 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7458 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7459 
7460 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7461 	    sc->er_version, 0, "expansion ROM version");
7462 
7463 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7464 	    sc->bs_version, 0, "bootstrap firmware version");
7465 
7466 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7467 	    NULL, sc->params.scfg_vers, "serial config version");
7468 
7469 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7470 	    NULL, sc->params.vpd_vers, "VPD version");
7471 
7472 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7473 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7474 
7475 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7476 	    sc->cfcsum, "config file checksum");
7477 
7478 #define SYSCTL_CAP(name, n, text) \
7479 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7480 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7481 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7482 	    "available " text " capabilities")
7483 
7484 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7485 	SYSCTL_CAP(linkcaps, 1, "link");
7486 	SYSCTL_CAP(switchcaps, 2, "switch");
7487 	SYSCTL_CAP(niccaps, 3, "NIC");
7488 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7489 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7490 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7491 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7492 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7493 #undef SYSCTL_CAP
7494 
7495 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7496 	    NULL, sc->tids.nftids, "number of filters");
7497 
7498 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7499 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7500 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7501 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7502 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7503 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7504 
7505 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "loadavg",
7506 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7507 	    sysctl_loadavg, "A",
7508 	    "microprocessor load averages (debug firmwares only)");
7509 
7510 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7511 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7512 	    "I", "core Vdd (in mV)");
7513 
7514 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7515 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7516 	    sysctl_cpus, "A", "local CPUs");
7517 
7518 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7519 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7520 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7521 
7522 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7523 	    &sc->swintr, 0, "software triggered interrupts");
7524 
7525 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
7526 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
7527 	    "1 = reset adapter, 0 = zero reset counter");
7528 
7529 	/*
7530 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
7531 	 */
7532 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
7533 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
7534 	    "logs and miscellaneous information");
7535 	children = SYSCTL_CHILDREN(oid);
7536 
7537 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
7538 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7539 	    sysctl_cctrl, "A", "congestion control");
7540 
7541 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp0",
7542 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7543 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 0 (TP0)");
7544 
7545 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_tp1",
7546 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7547 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 1 (TP1)");
7548 
7549 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ulp",
7550 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7551 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 2 (ULP)");
7552 
7553 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge0",
7554 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 3,
7555 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 3 (SGE0)");
7556 
7557 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_sge1",
7558 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 4,
7559 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 4 (SGE1)");
7560 
7561 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ibq_ncsi",
7562 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 5,
7563 	    sysctl_cim_ibq_obq, "A", "CIM IBQ 5 (NCSI)");
7564 
7565 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_la",
7566 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7567 	    sysctl_cim_la, "A", "CIM logic analyzer");
7568 
7569 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_ma_la",
7570 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7571 	    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7572 
7573 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp0",
7574 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7575 	    0 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 0 (ULP0)");
7576 
7577 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp1",
7578 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7579 	    1 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 1 (ULP1)");
7580 
7581 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp2",
7582 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7583 	    2 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 2 (ULP2)");
7584 
7585 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ulp3",
7586 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7587 	    3 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 3 (ULP3)");
7588 
7589 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge",
7590 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7591 	    4 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 4 (SGE)");
7592 
7593 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_ncsi",
7594 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7595 	    5 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A", "CIM OBQ 5 (NCSI)");
7596 
7597 	if (chip_id(sc) > CHELSIO_T4) {
7598 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge0_rx",
7599 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7600 		    6 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7601 		    "CIM OBQ 6 (SGE0-RX)");
7602 
7603 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_obq_sge1_rx",
7604 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7605 		    7 + CIM_NUM_IBQ, sysctl_cim_ibq_obq, "A",
7606 		    "CIM OBQ 7 (SGE1-RX)");
7607 	}
7608 
7609 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_pif_la",
7610 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7611 	    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7612 
7613 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cim_qcfg",
7614 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7615 	    sysctl_cim_qcfg, "A", "CIM queue configuration");
7616 
7617 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
7618 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7619 	    sysctl_cpl_stats, "A", "CPL statistics");
7620 
7621 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
7622 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7623 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
7624 
7625 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
7626 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7627 	    sysctl_tid_stats, "A", "tid stats");
7628 
7629 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
7630 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7631 	    sysctl_devlog, "A", "firmware's device log");
7632 
7633 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
7634 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7635 	    sysctl_fcoe_stats, "A", "FCoE statistics");
7636 
7637 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
7638 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7639 	    sysctl_hw_sched, "A", "hardware scheduler ");
7640 
7641 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
7642 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7643 	    sysctl_l2t, "A", "hardware L2 table");
7644 
7645 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
7646 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7647 	    sysctl_smt, "A", "hardware source MAC table");
7648 
7649 #ifdef INET6
7650 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
7651 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7652 	    sysctl_clip, "A", "active CLIP table entries");
7653 #endif
7654 
7655 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
7656 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7657 	    sysctl_lb_stats, "A", "loopback statistics");
7658 
7659 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
7660 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7661 	    sysctl_meminfo, "A", "memory regions");
7662 
7663 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
7664 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7665 	    chip_id(sc) <= CHELSIO_T5 ? sysctl_mps_tcam : sysctl_mps_tcam_t6,
7666 	    "A", "MPS TCAM entries");
7667 
7668 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
7669 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7670 	    sysctl_path_mtus, "A", "path MTUs");
7671 
7672 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
7673 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7674 	    sysctl_pm_stats, "A", "PM statistics");
7675 
7676 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
7677 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7678 	    sysctl_rdma_stats, "A", "RDMA statistics");
7679 
7680 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
7681 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7682 	    sysctl_tcp_stats, "A", "TCP statistics");
7683 
7684 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
7685 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7686 	    sysctl_tids, "A", "TID information");
7687 
7688 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
7689 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7690 	    sysctl_tp_err_stats, "A", "TP error statistics");
7691 
7692 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
7693 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7694 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
7695 
7696 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
7697 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7698 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
7699 
7700 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
7701 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7702 	    sysctl_tp_la, "A", "TP logic analyzer");
7703 
7704 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
7705 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7706 	    sysctl_tx_rate, "A", "Tx rate");
7707 
7708 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
7709 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7710 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
7711 
7712 	if (chip_id(sc) >= CHELSIO_T5) {
7713 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
7714 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7715 		    sysctl_wcwr_stats, "A", "write combined work requests");
7716 	}
7717 
7718 #ifdef KERN_TLS
7719 	if (is_ktls(sc)) {
7720 		/*
7721 		 * dev.t4nex.0.tls.
7722 		 */
7723 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
7724 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
7725 		children = SYSCTL_CHILDREN(oid);
7726 
7727 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
7728 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
7729 		    "keys in work requests (1) or attempt to store TLS keys "
7730 		    "in card memory.");
7731 
7732 		if (is_t6(sc))
7733 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
7734 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
7735 			    "combine TCB field updates with TLS record work "
7736 			    "requests.");
7737 	}
7738 #endif
7739 
7740 #ifdef TCP_OFFLOAD
7741 	if (is_offload(sc)) {
7742 		int i;
7743 		char s[4];
7744 
7745 		/*
7746 		 * dev.t4nex.X.toe.
7747 		 */
7748 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
7749 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
7750 		children = SYSCTL_CHILDREN(oid);
7751 
7752 		sc->tt.cong_algorithm = -1;
7753 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
7754 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
7755 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
7756 		    "3 = highspeed)");
7757 
7758 		sc->tt.sndbuf = -1;
7759 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
7760 		    &sc->tt.sndbuf, 0, "hardware send buffer");
7761 
7762 		sc->tt.ddp = 0;
7763 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
7764 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
7765 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
7766 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
7767 
7768 		sc->tt.rx_coalesce = -1;
7769 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
7770 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
7771 
7772 		sc->tt.tls = 0;
7773 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
7774 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
7775 		    "Inline TLS allowed");
7776 
7777 		sc->tt.tx_align = -1;
7778 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
7779 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
7780 
7781 		sc->tt.tx_zcopy = 0;
7782 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
7783 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
7784 		    "Enable zero-copy aio_write(2)");
7785 
7786 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
7787 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7788 		    "cop_managed_offloading", CTLFLAG_RW,
7789 		    &sc->tt.cop_managed_offloading, 0,
7790 		    "COP (Connection Offload Policy) controls all TOE offload");
7791 
7792 		sc->tt.autorcvbuf_inc = 16 * 1024;
7793 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
7794 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
7795 		    "autorcvbuf increment");
7796 
7797 		sc->tt.update_hc_on_pmtu_change = 1;
7798 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7799 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
7800 		    &sc->tt.update_hc_on_pmtu_change, 0,
7801 		    "Update hostcache entry if the PMTU changes");
7802 
7803 		sc->tt.iso = 1;
7804 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
7805 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
7806 
7807 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
7808 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7809 		    sysctl_tp_tick, "A", "TP timer tick (us)");
7810 
7811 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
7812 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
7813 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
7814 
7815 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
7816 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
7817 		    sysctl_tp_tick, "A", "DACK tick (us)");
7818 
7819 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
7820 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7821 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
7822 
7823 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
7824 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7825 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
7826 		    "Minimum retransmit interval (us)");
7827 
7828 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
7829 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7830 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
7831 		    "Maximum retransmit interval (us)");
7832 
7833 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
7834 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7835 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
7836 		    "Persist timer min (us)");
7837 
7838 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
7839 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7840 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
7841 		    "Persist timer max (us)");
7842 
7843 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
7844 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7845 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
7846 		    "Keepalive idle timer (us)");
7847 
7848 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
7849 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7850 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
7851 		    "Keepalive interval timer (us)");
7852 
7853 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
7854 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7855 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
7856 
7857 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
7858 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7859 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
7860 		    "FINWAIT2 timer (us)");
7861 
7862 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
7863 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7864 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
7865 		    "Number of SYN retransmissions before abort");
7866 
7867 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
7868 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7869 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
7870 		    "Number of retransmissions before abort");
7871 
7872 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
7873 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7874 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
7875 		    "Number of keepalive probes before abort");
7876 
7877 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
7878 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7879 		    "TOE retransmit backoffs");
7880 		children = SYSCTL_CHILDREN(oid);
7881 		for (i = 0; i < 16; i++) {
7882 			snprintf(s, sizeof(s), "%u", i);
7883 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
7884 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7885 			    i, sysctl_tp_backoff, "IU",
7886 			    "TOE retransmit backoff");
7887 		}
7888 	}
7889 #endif
7890 }
7891 
7892 void
7893 vi_sysctls(struct vi_info *vi)
7894 {
7895 	struct sysctl_ctx_list *ctx = &vi->ctx;
7896 	struct sysctl_oid *oid;
7897 	struct sysctl_oid_list *children;
7898 
7899 	/*
7900 	 * dev.v?(cxgbe|cxl).X.
7901 	 */
7902 	oid = device_get_sysctl_tree(vi->dev);
7903 	children = SYSCTL_CHILDREN(oid);
7904 
7905 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
7906 	    vi->viid, "VI identifer");
7907 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
7908 	    &vi->nrxq, 0, "# of rx queues");
7909 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
7910 	    &vi->ntxq, 0, "# of tx queues");
7911 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
7912 	    &vi->first_rxq, 0, "index of first rx queue");
7913 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
7914 	    &vi->first_txq, 0, "index of first tx queue");
7915 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
7916 	    vi->rss_base, "start of RSS indirection table");
7917 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
7918 	    vi->rss_size, "size of RSS indirection table");
7919 
7920 	if (IS_MAIN_VI(vi)) {
7921 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
7922 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7923 		    sysctl_noflowq, "IU",
7924 		    "Reserve queue 0 for non-flowid packets");
7925 	}
7926 
7927 	if (vi->adapter->flags & IS_VF) {
7928 		MPASS(vi->flags & TX_USES_VM_WR);
7929 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
7930 		    NULL, 1, "use VM work requests for transmit");
7931 	} else {
7932 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
7933 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7934 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
7935 	}
7936 
7937 #ifdef TCP_OFFLOAD
7938 	if (vi->nofldrxq != 0) {
7939 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
7940 		    &vi->nofldrxq, 0,
7941 		    "# of rx queues for offloaded TCP connections");
7942 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
7943 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
7944 		    "index of first TOE rx queue");
7945 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
7946 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7947 		    sysctl_holdoff_tmr_idx_ofld, "I",
7948 		    "holdoff timer index for TOE queues");
7949 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
7950 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7951 		    sysctl_holdoff_pktc_idx_ofld, "I",
7952 		    "holdoff packet counter index for TOE queues");
7953 	}
7954 #endif
7955 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7956 	if (vi->nofldtxq != 0) {
7957 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
7958 		    &vi->nofldtxq, 0,
7959 		    "# of tx queues for TOE/ETHOFLD");
7960 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
7961 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
7962 		    "index of first TOE/ETHOFLD tx queue");
7963 	}
7964 #endif
7965 #ifdef DEV_NETMAP
7966 	if (vi->nnmrxq != 0) {
7967 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
7968 		    &vi->nnmrxq, 0, "# of netmap rx queues");
7969 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
7970 		    &vi->nnmtxq, 0, "# of netmap tx queues");
7971 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
7972 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
7973 		    "index of first netmap rx queue");
7974 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
7975 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
7976 		    "index of first netmap tx queue");
7977 	}
7978 #endif
7979 
7980 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
7981 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7982 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
7983 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
7984 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7985 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
7986 
7987 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
7988 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7989 	    sysctl_qsize_rxq, "I", "rx queue size");
7990 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
7991 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
7992 	    sysctl_qsize_txq, "I", "tx queue size");
7993 }
7994 
7995 static void
7996 cxgbe_sysctls(struct port_info *pi)
7997 {
7998 	struct sysctl_ctx_list *ctx = &pi->ctx;
7999 	struct sysctl_oid *oid;
8000 	struct sysctl_oid_list *children, *children2;
8001 	struct adapter *sc = pi->adapter;
8002 	int i;
8003 	char name[16];
8004 	static char *tc_flags = {"\20\1USER"};
8005 
8006 	/*
8007 	 * dev.cxgbe.X.
8008 	 */
8009 	oid = device_get_sysctl_tree(pi->dev);
8010 	children = SYSCTL_CHILDREN(oid);
8011 
8012 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8013 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8014 	    sysctl_linkdnrc, "A", "reason why link is down");
8015 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8016 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8017 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8018 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8019 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8020 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8021 		    sysctl_btphy, "I", "PHY firmware version");
8022 	}
8023 
8024 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8025 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8026 	    sysctl_pause_settings, "A",
8027 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8028 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8029 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8030 	    "FEC in use on the link");
8031 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8032 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8033 	    sysctl_requested_fec, "A",
8034 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8035 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8036 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8037 	    "FEC recommended by the cable/transceiver");
8038 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8039 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8040 	    sysctl_autoneg, "I",
8041 	    "autonegotiation (-1 = not supported)");
8042 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8043 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8044 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8045 
8046 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8047 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8048 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8049 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8050 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8051 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8052 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8053 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8054 
8055 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8056 	    port_top_speed(pi), "max speed (in Gbps)");
8057 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8058 	    pi->mps_bg_map, "MPS buffer group map");
8059 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8060 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8061 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8062 	    pi->tx_chan, "TP tx c-channel");
8063 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8064 	    pi->rx_chan, "TP rx c-channel");
8065 
8066 	if (sc->flags & IS_VF)
8067 		return;
8068 
8069 	/*
8070 	 * dev.(cxgbe|cxl).X.tc.
8071 	 */
8072 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8073 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8074 	    "Tx scheduler traffic classes (cl_rl)");
8075 	children2 = SYSCTL_CHILDREN(oid);
8076 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8077 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8078 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8079 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8080 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8081 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8082 	for (i = 0; i < sc->params.nsched_cls; i++) {
8083 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8084 
8085 		snprintf(name, sizeof(name), "%d", i);
8086 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8087 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8088 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8089 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8090 		    CTLFLAG_RD, &tc->state, 0, "current state");
8091 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8092 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8093 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8094 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8095 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8096 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8097 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8098 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8099 		    "traffic class parameters");
8100 	}
8101 
8102 	/*
8103 	 * dev.cxgbe.X.stats.
8104 	 */
8105 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8106 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8107 	children = SYSCTL_CHILDREN(oid);
8108 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8109 	    &pi->tx_parse_error, 0,
8110 	    "# of tx packets with invalid length or # of segments");
8111 
8112 #define T4_REGSTAT(name, stat, desc) \
8113     SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8114 	CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8115 	t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8116         sysctl_handle_t4_reg64, "QU", desc)
8117 
8118 /* We get these from port_stats and they may be stale by up to 1s */
8119 #define T4_PORTSTAT(name, desc) \
8120 	SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, #name, CTLFLAG_RD, \
8121 	    &pi->stats.name, desc)
8122 
8123 	T4_REGSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8124 	T4_REGSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8125 	T4_REGSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8126 	T4_REGSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8127 	T4_REGSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8128 	T4_REGSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8129 	T4_REGSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8130 	T4_REGSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8131 	T4_REGSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8132 	T4_REGSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8133 	T4_REGSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8134 	T4_REGSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8135 	T4_REGSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8136 	T4_REGSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8137 	T4_REGSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8138 	T4_REGSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8139 	T4_REGSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8140 	T4_REGSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8141 	T4_REGSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8142 	T4_REGSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8143 	T4_REGSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8144 	T4_REGSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8145 	T4_REGSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8146 
8147 	T4_REGSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8148 	T4_REGSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8149 	T4_REGSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8150 	T4_REGSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8151 	T4_REGSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8152 	T4_REGSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8153 	T4_REGSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8154 	if (is_t6(sc)) {
8155 		T4_PORTSTAT(rx_fcs_err,
8156 		    "# of frames received with bad FCS since last link up");
8157 	} else {
8158 		T4_REGSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8159 		    "# of frames received with bad FCS");
8160 	}
8161 	T4_REGSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8162 	T4_REGSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8163 	T4_REGSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8164 	T4_REGSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8165 	T4_REGSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8166 	T4_REGSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8167 	T4_REGSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8168 	T4_REGSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8169 	T4_REGSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8170 	T4_REGSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8171 	T4_REGSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8172 	T4_REGSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8173 	T4_REGSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8174 	T4_REGSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8175 	T4_REGSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8176 	T4_REGSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8177 	T4_REGSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8178 	T4_REGSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8179 	T4_REGSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8180 
8181 	T4_PORTSTAT(rx_ovflow0, "# drops due to buffer-group 0 overflows");
8182 	T4_PORTSTAT(rx_ovflow1, "# drops due to buffer-group 1 overflows");
8183 	T4_PORTSTAT(rx_ovflow2, "# drops due to buffer-group 2 overflows");
8184 	T4_PORTSTAT(rx_ovflow3, "# drops due to buffer-group 3 overflows");
8185 	T4_PORTSTAT(rx_trunc0, "# of buffer-group 0 truncated packets");
8186 	T4_PORTSTAT(rx_trunc1, "# of buffer-group 1 truncated packets");
8187 	T4_PORTSTAT(rx_trunc2, "# of buffer-group 2 truncated packets");
8188 	T4_PORTSTAT(rx_trunc3, "# of buffer-group 3 truncated packets");
8189 
8190 #undef T4_REGSTAT
8191 #undef T4_PORTSTAT
8192 }
8193 
8194 static int
8195 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8196 {
8197 	int rc, *i, space = 0;
8198 	struct sbuf sb;
8199 
8200 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8201 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8202 		if (space)
8203 			sbuf_printf(&sb, " ");
8204 		sbuf_printf(&sb, "%d", *i);
8205 		space = 1;
8206 	}
8207 	rc = sbuf_finish(&sb);
8208 	sbuf_delete(&sb);
8209 	return (rc);
8210 }
8211 
8212 static int
8213 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8214 {
8215 	int rc;
8216 	struct sbuf *sb;
8217 
8218 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8219 	if (sb == NULL)
8220 		return (ENOMEM);
8221 
8222 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8223 	rc = sbuf_finish(sb);
8224 	sbuf_delete(sb);
8225 
8226 	return (rc);
8227 }
8228 
8229 static int
8230 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8231 {
8232 	int rc;
8233 	struct sbuf *sb;
8234 
8235 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8236 	if (sb == NULL)
8237 		return (ENOMEM);
8238 
8239 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8240 	rc = sbuf_finish(sb);
8241 	sbuf_delete(sb);
8242 
8243 	return (rc);
8244 }
8245 
8246 static int
8247 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8248 {
8249 	struct port_info *pi = arg1;
8250 	int op = arg2;
8251 	struct adapter *sc = pi->adapter;
8252 	u_int v;
8253 	int rc;
8254 
8255 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8256 	if (rc)
8257 		return (rc);
8258 	if (hw_off_limits(sc))
8259 		rc = ENXIO;
8260 	else {
8261 		/* XXX: magic numbers */
8262 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8263 		    op ? 0x20 : 0xc820, &v);
8264 	}
8265 	end_synchronized_op(sc, 0);
8266 	if (rc)
8267 		return (rc);
8268 	if (op == 0)
8269 		v /= 256;
8270 
8271 	rc = sysctl_handle_int(oidp, &v, 0, req);
8272 	return (rc);
8273 }
8274 
8275 static int
8276 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8277 {
8278 	struct vi_info *vi = arg1;
8279 	int rc, val;
8280 
8281 	val = vi->rsrv_noflowq;
8282 	rc = sysctl_handle_int(oidp, &val, 0, req);
8283 	if (rc != 0 || req->newptr == NULL)
8284 		return (rc);
8285 
8286 	if ((val >= 1) && (vi->ntxq > 1))
8287 		vi->rsrv_noflowq = 1;
8288 	else
8289 		vi->rsrv_noflowq = 0;
8290 
8291 	return (rc);
8292 }
8293 
8294 static int
8295 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8296 {
8297 	struct vi_info *vi = arg1;
8298 	struct adapter *sc = vi->adapter;
8299 	int rc, val, i;
8300 
8301 	MPASS(!(sc->flags & IS_VF));
8302 
8303 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8304 	rc = sysctl_handle_int(oidp, &val, 0, req);
8305 	if (rc != 0 || req->newptr == NULL)
8306 		return (rc);
8307 
8308 	if (val != 0 && val != 1)
8309 		return (EINVAL);
8310 
8311 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8312 	    "t4txvm");
8313 	if (rc)
8314 		return (rc);
8315 	if (hw_off_limits(sc))
8316 		rc = ENXIO;
8317 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8318 		/*
8319 		 * We don't want parse_pkt to run with one setting (VF or PF)
8320 		 * and then eth_tx to see a different setting but still use
8321 		 * stale information calculated by parse_pkt.
8322 		 */
8323 		rc = EBUSY;
8324 	} else {
8325 		struct port_info *pi = vi->pi;
8326 		struct sge_txq *txq;
8327 		uint32_t ctrl0;
8328 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8329 
8330 		if (val) {
8331 			vi->flags |= TX_USES_VM_WR;
8332 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8333 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8334 			    V_TXPKT_INTF(pi->tx_chan));
8335 			if (!(sc->flags & IS_VF))
8336 				npkt--;
8337 		} else {
8338 			vi->flags &= ~TX_USES_VM_WR;
8339 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8340 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8341 			    V_TXPKT_INTF(pi->tx_chan) | V_TXPKT_PF(sc->pf) |
8342 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8343 		}
8344 		for_each_txq(vi, i, txq) {
8345 			txq->cpl_ctrl0 = ctrl0;
8346 			txq->txp.max_npkt = npkt;
8347 		}
8348 	}
8349 	end_synchronized_op(sc, LOCK_HELD);
8350 	return (rc);
8351 }
8352 
8353 static int
8354 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8355 {
8356 	struct vi_info *vi = arg1;
8357 	struct adapter *sc = vi->adapter;
8358 	int idx, rc, i;
8359 	struct sge_rxq *rxq;
8360 	uint8_t v;
8361 
8362 	idx = vi->tmr_idx;
8363 
8364 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8365 	if (rc != 0 || req->newptr == NULL)
8366 		return (rc);
8367 
8368 	if (idx < 0 || idx >= SGE_NTIMERS)
8369 		return (EINVAL);
8370 
8371 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8372 	    "t4tmr");
8373 	if (rc)
8374 		return (rc);
8375 
8376 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8377 	for_each_rxq(vi, i, rxq) {
8378 #ifdef atomic_store_rel_8
8379 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8380 #else
8381 		rxq->iq.intr_params = v;
8382 #endif
8383 	}
8384 	vi->tmr_idx = idx;
8385 
8386 	end_synchronized_op(sc, LOCK_HELD);
8387 	return (0);
8388 }
8389 
8390 static int
8391 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8392 {
8393 	struct vi_info *vi = arg1;
8394 	struct adapter *sc = vi->adapter;
8395 	int idx, rc;
8396 
8397 	idx = vi->pktc_idx;
8398 
8399 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8400 	if (rc != 0 || req->newptr == NULL)
8401 		return (rc);
8402 
8403 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8404 		return (EINVAL);
8405 
8406 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8407 	    "t4pktc");
8408 	if (rc)
8409 		return (rc);
8410 
8411 	if (vi->flags & VI_INIT_DONE)
8412 		rc = EBUSY; /* cannot be changed once the queues are created */
8413 	else
8414 		vi->pktc_idx = idx;
8415 
8416 	end_synchronized_op(sc, LOCK_HELD);
8417 	return (rc);
8418 }
8419 
8420 static int
8421 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8422 {
8423 	struct vi_info *vi = arg1;
8424 	struct adapter *sc = vi->adapter;
8425 	int qsize, rc;
8426 
8427 	qsize = vi->qsize_rxq;
8428 
8429 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8430 	if (rc != 0 || req->newptr == NULL)
8431 		return (rc);
8432 
8433 	if (qsize < 128 || (qsize & 7))
8434 		return (EINVAL);
8435 
8436 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8437 	    "t4rxqs");
8438 	if (rc)
8439 		return (rc);
8440 
8441 	if (vi->flags & VI_INIT_DONE)
8442 		rc = EBUSY; /* cannot be changed once the queues are created */
8443 	else
8444 		vi->qsize_rxq = qsize;
8445 
8446 	end_synchronized_op(sc, LOCK_HELD);
8447 	return (rc);
8448 }
8449 
8450 static int
8451 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8452 {
8453 	struct vi_info *vi = arg1;
8454 	struct adapter *sc = vi->adapter;
8455 	int qsize, rc;
8456 
8457 	qsize = vi->qsize_txq;
8458 
8459 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8460 	if (rc != 0 || req->newptr == NULL)
8461 		return (rc);
8462 
8463 	if (qsize < 128 || qsize > 65536)
8464 		return (EINVAL);
8465 
8466 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8467 	    "t4txqs");
8468 	if (rc)
8469 		return (rc);
8470 
8471 	if (vi->flags & VI_INIT_DONE)
8472 		rc = EBUSY; /* cannot be changed once the queues are created */
8473 	else
8474 		vi->qsize_txq = qsize;
8475 
8476 	end_synchronized_op(sc, LOCK_HELD);
8477 	return (rc);
8478 }
8479 
8480 static int
8481 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8482 {
8483 	struct port_info *pi = arg1;
8484 	struct adapter *sc = pi->adapter;
8485 	struct link_config *lc = &pi->link_cfg;
8486 	int rc;
8487 
8488 	if (req->newptr == NULL) {
8489 		struct sbuf *sb;
8490 		static char *bits = "\20\1RX\2TX\3AUTO";
8491 
8492 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8493 		if (sb == NULL)
8494 			return (ENOMEM);
8495 
8496 		if (lc->link_ok) {
8497 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8498 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8499 		} else {
8500 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8501 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8502 		}
8503 		rc = sbuf_finish(sb);
8504 		sbuf_delete(sb);
8505 	} else {
8506 		char s[2];
8507 		int n;
8508 
8509 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8510 		    PAUSE_AUTONEG));
8511 		s[1] = 0;
8512 
8513 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8514 		if (rc != 0)
8515 			return(rc);
8516 
8517 		if (s[1] != 0)
8518 			return (EINVAL);
8519 		if (s[0] < '0' || s[0] > '9')
8520 			return (EINVAL);	/* not a number */
8521 		n = s[0] - '0';
8522 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8523 			return (EINVAL);	/* some other bit is set too */
8524 
8525 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8526 		    "t4PAUSE");
8527 		if (rc)
8528 			return (rc);
8529 		if (!hw_off_limits(sc)) {
8530 			PORT_LOCK(pi);
8531 			lc->requested_fc = n;
8532 			fixup_link_config(pi);
8533 			if (pi->up_vis > 0)
8534 				rc = apply_link_config(pi);
8535 			set_current_media(pi);
8536 			PORT_UNLOCK(pi);
8537 		}
8538 		end_synchronized_op(sc, 0);
8539 	}
8540 
8541 	return (rc);
8542 }
8543 
8544 static int
8545 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8546 {
8547 	struct port_info *pi = arg1;
8548 	struct link_config *lc = &pi->link_cfg;
8549 	int rc;
8550 	struct sbuf *sb;
8551 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2";
8552 
8553 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8554 	if (sb == NULL)
8555 		return (ENOMEM);
8556 	if (lc->link_ok)
8557 		sbuf_printf(sb, "%b", lc->fec, bits);
8558 	else
8559 		sbuf_printf(sb, "no link");
8560 	rc = sbuf_finish(sb);
8561 	sbuf_delete(sb);
8562 
8563 	return (rc);
8564 }
8565 
8566 static int
8567 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
8568 {
8569 	struct port_info *pi = arg1;
8570 	struct adapter *sc = pi->adapter;
8571 	struct link_config *lc = &pi->link_cfg;
8572 	int rc;
8573 	int8_t old;
8574 
8575 	if (req->newptr == NULL) {
8576 		struct sbuf *sb;
8577 		static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2"
8578 		    "\5RSVD3\6auto\7module";
8579 
8580 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8581 		if (sb == NULL)
8582 			return (ENOMEM);
8583 
8584 		sbuf_printf(sb, "%b", lc->requested_fec, bits);
8585 		rc = sbuf_finish(sb);
8586 		sbuf_delete(sb);
8587 	} else {
8588 		char s[8];
8589 		int n;
8590 
8591 		snprintf(s, sizeof(s), "%d",
8592 		    lc->requested_fec == FEC_AUTO ? -1 :
8593 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
8594 
8595 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8596 		if (rc != 0)
8597 			return(rc);
8598 
8599 		n = strtol(&s[0], NULL, 0);
8600 		if (n < 0 || n & FEC_AUTO)
8601 			n = FEC_AUTO;
8602 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
8603 			return (EINVAL);/* some other bit is set too */
8604 
8605 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8606 		    "t4reqf");
8607 		if (rc)
8608 			return (rc);
8609 		PORT_LOCK(pi);
8610 		old = lc->requested_fec;
8611 		if (n == FEC_AUTO)
8612 			lc->requested_fec = FEC_AUTO;
8613 		else if (n == 0 || n == FEC_NONE)
8614 			lc->requested_fec = FEC_NONE;
8615 		else {
8616 			if ((lc->pcaps |
8617 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
8618 			    lc->pcaps) {
8619 				rc = ENOTSUP;
8620 				goto done;
8621 			}
8622 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
8623 			    FEC_MODULE);
8624 		}
8625 		if (!hw_off_limits(sc)) {
8626 			fixup_link_config(pi);
8627 			if (pi->up_vis > 0) {
8628 				rc = apply_link_config(pi);
8629 				if (rc != 0) {
8630 					lc->requested_fec = old;
8631 					if (rc == FW_EPROTO)
8632 						rc = ENOTSUP;
8633 				}
8634 			}
8635 		}
8636 done:
8637 		PORT_UNLOCK(pi);
8638 		end_synchronized_op(sc, 0);
8639 	}
8640 
8641 	return (rc);
8642 }
8643 
8644 static int
8645 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
8646 {
8647 	struct port_info *pi = arg1;
8648 	struct adapter *sc = pi->adapter;
8649 	struct link_config *lc = &pi->link_cfg;
8650 	int rc;
8651 	int8_t fec;
8652 	struct sbuf *sb;
8653 	static char *bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD2\5RSVD3";
8654 
8655 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8656 	if (sb == NULL)
8657 		return (ENOMEM);
8658 
8659 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
8660 		rc = EBUSY;
8661 		goto done;
8662 	}
8663 	if (hw_off_limits(sc)) {
8664 		rc = ENXIO;
8665 		goto done;
8666 	}
8667 	PORT_LOCK(pi);
8668 	if (pi->up_vis == 0) {
8669 		/*
8670 		 * If all the interfaces are administratively down the firmware
8671 		 * does not report transceiver changes.  Refresh port info here.
8672 		 * This is the only reason we have a synchronized op in this
8673 		 * function.  Just PORT_LOCK would have been enough otherwise.
8674 		 */
8675 		t4_update_port_info(pi);
8676 	}
8677 
8678 	fec = lc->fec_hint;
8679 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
8680 	    !fec_supported(lc->pcaps)) {
8681 		PORT_UNLOCK(pi);
8682 		sbuf_printf(sb, "n/a");
8683 	} else {
8684 		if (fec == 0)
8685 			fec = FEC_NONE;
8686 		PORT_UNLOCK(pi);
8687 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, bits);
8688 	}
8689 	rc = sbuf_finish(sb);
8690 done:
8691 	sbuf_delete(sb);
8692 	end_synchronized_op(sc, 0);
8693 
8694 	return (rc);
8695 }
8696 
8697 static int
8698 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
8699 {
8700 	struct port_info *pi = arg1;
8701 	struct adapter *sc = pi->adapter;
8702 	struct link_config *lc = &pi->link_cfg;
8703 	int rc, val;
8704 
8705 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
8706 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
8707 	else
8708 		val = -1;
8709 	rc = sysctl_handle_int(oidp, &val, 0, req);
8710 	if (rc != 0 || req->newptr == NULL)
8711 		return (rc);
8712 	if (val == 0)
8713 		val = AUTONEG_DISABLE;
8714 	else if (val == 1)
8715 		val = AUTONEG_ENABLE;
8716 	else
8717 		val = AUTONEG_AUTO;
8718 
8719 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8720 	    "t4aneg");
8721 	if (rc)
8722 		return (rc);
8723 	PORT_LOCK(pi);
8724 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
8725 		rc = ENOTSUP;
8726 		goto done;
8727 	}
8728 	lc->requested_aneg = val;
8729 	if (!hw_off_limits(sc)) {
8730 		fixup_link_config(pi);
8731 		if (pi->up_vis > 0)
8732 			rc = apply_link_config(pi);
8733 		set_current_media(pi);
8734 	}
8735 done:
8736 	PORT_UNLOCK(pi);
8737 	end_synchronized_op(sc, 0);
8738 	return (rc);
8739 }
8740 
8741 static int
8742 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
8743 {
8744 	struct port_info *pi = arg1;
8745 	struct adapter *sc = pi->adapter;
8746 	struct link_config *lc = &pi->link_cfg;
8747 	int rc, val;
8748 
8749 	val = lc->force_fec;
8750 	MPASS(val >= -1 && val <= 1);
8751 	rc = sysctl_handle_int(oidp, &val, 0, req);
8752 	if (rc != 0 || req->newptr == NULL)
8753 		return (rc);
8754 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
8755 		return (ENOTSUP);
8756 	if (val < -1 || val > 1)
8757 		return (EINVAL);
8758 
8759 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
8760 	if (rc)
8761 		return (rc);
8762 	PORT_LOCK(pi);
8763 	lc->force_fec = val;
8764 	if (!hw_off_limits(sc)) {
8765 		fixup_link_config(pi);
8766 		if (pi->up_vis > 0)
8767 			rc = apply_link_config(pi);
8768 	}
8769 	PORT_UNLOCK(pi);
8770 	end_synchronized_op(sc, 0);
8771 	return (rc);
8772 }
8773 
8774 static int
8775 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
8776 {
8777 	struct adapter *sc = arg1;
8778 	int rc, reg = arg2;
8779 	uint64_t val;
8780 
8781 	mtx_lock(&sc->reg_lock);
8782 	if (hw_off_limits(sc))
8783 		rc = ENXIO;
8784 	else {
8785 		rc = 0;
8786 		val = t4_read_reg64(sc, reg);
8787 	}
8788 	mtx_unlock(&sc->reg_lock);
8789 	if (rc == 0)
8790 		rc = sysctl_handle_64(oidp, &val, 0, req);
8791 	return (rc);
8792 }
8793 
8794 static int
8795 sysctl_temperature(SYSCTL_HANDLER_ARGS)
8796 {
8797 	struct adapter *sc = arg1;
8798 	int rc, t;
8799 	uint32_t param, val;
8800 
8801 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
8802 	if (rc)
8803 		return (rc);
8804 	if (hw_off_limits(sc))
8805 		rc = ENXIO;
8806 	else {
8807 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8808 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8809 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
8810 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8811 	}
8812 	end_synchronized_op(sc, 0);
8813 	if (rc)
8814 		return (rc);
8815 
8816 	/* unknown is returned as 0 but we display -1 in that case */
8817 	t = val == 0 ? -1 : val;
8818 
8819 	rc = sysctl_handle_int(oidp, &t, 0, req);
8820 	return (rc);
8821 }
8822 
8823 static int
8824 sysctl_vdd(SYSCTL_HANDLER_ARGS)
8825 {
8826 	struct adapter *sc = arg1;
8827 	int rc;
8828 	uint32_t param, val;
8829 
8830 	if (sc->params.core_vdd == 0) {
8831 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
8832 		    "t4vdd");
8833 		if (rc)
8834 			return (rc);
8835 		if (hw_off_limits(sc))
8836 			rc = ENXIO;
8837 		else {
8838 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8839 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8840 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
8841 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
8842 			    &param, &val);
8843 		}
8844 		end_synchronized_op(sc, 0);
8845 		if (rc)
8846 			return (rc);
8847 		sc->params.core_vdd = val;
8848 	}
8849 
8850 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
8851 }
8852 
8853 static int
8854 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
8855 {
8856 	struct adapter *sc = arg1;
8857 	int rc, v;
8858 	uint32_t param, val;
8859 
8860 	v = sc->sensor_resets;
8861 	rc = sysctl_handle_int(oidp, &v, 0, req);
8862 	if (rc != 0 || req->newptr == NULL || v <= 0)
8863 		return (rc);
8864 
8865 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
8866 	    chip_id(sc) < CHELSIO_T5)
8867 		return (ENOTSUP);
8868 
8869 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
8870 	if (rc)
8871 		return (rc);
8872 	if (hw_off_limits(sc))
8873 		rc = ENXIO;
8874 	else {
8875 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8876 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
8877 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
8878 		val = 1;
8879 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8880 	}
8881 	end_synchronized_op(sc, 0);
8882 	if (rc == 0)
8883 		sc->sensor_resets++;
8884 	return (rc);
8885 }
8886 
8887 static int
8888 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
8889 {
8890 	struct adapter *sc = arg1;
8891 	struct sbuf *sb;
8892 	int rc;
8893 	uint32_t param, val;
8894 
8895 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
8896 	if (rc)
8897 		return (rc);
8898 	if (hw_off_limits(sc))
8899 		rc = ENXIO;
8900 	else {
8901 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
8902 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD);
8903 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
8904 	}
8905 	end_synchronized_op(sc, 0);
8906 	if (rc)
8907 		return (rc);
8908 
8909 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8910 	if (sb == NULL)
8911 		return (ENOMEM);
8912 
8913 	if (val == 0xffffffff) {
8914 		/* Only debug and custom firmwares report load averages. */
8915 		sbuf_printf(sb, "not available");
8916 	} else {
8917 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
8918 		    (val >> 16) & 0xff);
8919 	}
8920 	rc = sbuf_finish(sb);
8921 	sbuf_delete(sb);
8922 
8923 	return (rc);
8924 }
8925 
8926 static int
8927 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
8928 {
8929 	struct adapter *sc = arg1;
8930 	struct sbuf *sb;
8931 	int rc, i;
8932 	uint16_t incr[NMTUS][NCCTRL_WIN];
8933 	static const char *dec_fac[] = {
8934 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
8935 		"0.9375"
8936 	};
8937 
8938 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
8939 	if (sb == NULL)
8940 		return (ENOMEM);
8941 
8942 	rc = 0;
8943 	mtx_lock(&sc->reg_lock);
8944 	if (hw_off_limits(sc))
8945 		rc = ENXIO;
8946 	else
8947 		t4_read_cong_tbl(sc, incr);
8948 	mtx_unlock(&sc->reg_lock);
8949 	if (rc)
8950 		goto done;
8951 
8952 	for (i = 0; i < NCCTRL_WIN; ++i) {
8953 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
8954 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
8955 		    incr[5][i], incr[6][i], incr[7][i]);
8956 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
8957 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
8958 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
8959 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
8960 	}
8961 
8962 	rc = sbuf_finish(sb);
8963 done:
8964 	sbuf_delete(sb);
8965 	return (rc);
8966 }
8967 
8968 static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
8969 	"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
8970 	"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
8971 	"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
8972 };
8973 
8974 static int
8975 sysctl_cim_ibq_obq(SYSCTL_HANDLER_ARGS)
8976 {
8977 	struct adapter *sc = arg1;
8978 	struct sbuf *sb;
8979 	int rc, i, n, qid = arg2;
8980 	uint32_t *buf, *p;
8981 	char *qtype;
8982 	u_int cim_num_obq = sc->chip_params->cim_num_obq;
8983 
8984 	KASSERT(qid >= 0 && qid < CIM_NUM_IBQ + cim_num_obq,
8985 	    ("%s: bad qid %d\n", __func__, qid));
8986 
8987 	if (qid < CIM_NUM_IBQ) {
8988 		/* inbound queue */
8989 		qtype = "IBQ";
8990 		n = 4 * CIM_IBQ_SIZE;
8991 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
8992 		mtx_lock(&sc->reg_lock);
8993 		if (hw_off_limits(sc))
8994 			rc = -ENXIO;
8995 		else
8996 			rc = t4_read_cim_ibq(sc, qid, buf, n);
8997 		mtx_unlock(&sc->reg_lock);
8998 	} else {
8999 		/* outbound queue */
9000 		qtype = "OBQ";
9001 		qid -= CIM_NUM_IBQ;
9002 		n = 4 * cim_num_obq * CIM_OBQ_SIZE;
9003 		buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9004 		mtx_lock(&sc->reg_lock);
9005 		if (hw_off_limits(sc))
9006 			rc = -ENXIO;
9007 		else
9008 			rc = t4_read_cim_obq(sc, qid, buf, n);
9009 		mtx_unlock(&sc->reg_lock);
9010 	}
9011 
9012 	if (rc < 0) {
9013 		rc = -rc;
9014 		goto done;
9015 	}
9016 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9017 
9018 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9019 	if (sb == NULL) {
9020 		rc = ENOMEM;
9021 		goto done;
9022 	}
9023 
9024 	sbuf_printf(sb, "%s%d %s", qtype , qid, qname[arg2]);
9025 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9026 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9027 		    p[2], p[3]);
9028 
9029 	rc = sbuf_finish(sb);
9030 	sbuf_delete(sb);
9031 done:
9032 	free(buf, M_CXGBE);
9033 	return (rc);
9034 }
9035 
9036 static void
9037 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9038 {
9039 	uint32_t *p;
9040 
9041 	sbuf_printf(sb, "Status   Data      PC%s",
9042 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9043 	    "     LS0Stat  LS0Addr             LS0Data");
9044 
9045 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9046 		if (cfg & F_UPDBGLACAPTPCONLY) {
9047 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9048 			    p[6], p[7]);
9049 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9050 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9051 			    p[4] & 0xff, p[5] >> 8);
9052 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9053 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9054 			    p[1] & 0xf, p[2] >> 4);
9055 		} else {
9056 			sbuf_printf(sb,
9057 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9058 			    "%08x%08x%08x%08x",
9059 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9060 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9061 			    p[6], p[7]);
9062 		}
9063 	}
9064 }
9065 
9066 static void
9067 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9068 {
9069 	uint32_t *p;
9070 
9071 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9072 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9073 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9074 
9075 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9076 		if (cfg & F_UPDBGLACAPTPCONLY) {
9077 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9078 			    p[3] & 0xff, p[2], p[1], p[0]);
9079 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9080 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9081 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9082 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9083 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9084 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9085 			    p[6] >> 16);
9086 		} else {
9087 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9088 			    "%08x %08x %08x %08x %08x %08x",
9089 			    (p[9] >> 16) & 0xff,
9090 			    p[9] & 0xffff, p[8] >> 16,
9091 			    p[8] & 0xffff, p[7] >> 16,
9092 			    p[7] & 0xffff, p[6] >> 16,
9093 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9094 		}
9095 	}
9096 }
9097 
9098 static int
9099 sbuf_cim_la(struct adapter *sc, struct sbuf *sb, int flags)
9100 {
9101 	uint32_t cfg, *buf;
9102 	int rc;
9103 
9104 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9105 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9106 	    M_ZERO | flags);
9107 	if (buf == NULL)
9108 		return (ENOMEM);
9109 
9110 	mtx_lock(&sc->reg_lock);
9111 	if (hw_off_limits(sc))
9112 		rc = ENXIO;
9113 	else {
9114 		rc = -t4_cim_read(sc, A_UP_UP_DBG_LA_CFG, 1, &cfg);
9115 		if (rc == 0)
9116 			rc = -t4_cim_read_la(sc, buf, NULL);
9117 	}
9118 	mtx_unlock(&sc->reg_lock);
9119 	if (rc == 0) {
9120 		if (chip_id(sc) < CHELSIO_T6)
9121 			sbuf_cim_la4(sc, sb, buf, cfg);
9122 		else
9123 			sbuf_cim_la6(sc, sb, buf, cfg);
9124 	}
9125 	free(buf, M_CXGBE);
9126 	return (rc);
9127 }
9128 
9129 static int
9130 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9131 {
9132 	struct adapter *sc = arg1;
9133 	struct sbuf *sb;
9134 	int rc;
9135 
9136 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9137 	if (sb == NULL)
9138 		return (ENOMEM);
9139 
9140 	rc = sbuf_cim_la(sc, sb, M_WAITOK);
9141 	if (rc == 0)
9142 		rc = sbuf_finish(sb);
9143 	sbuf_delete(sb);
9144 	return (rc);
9145 }
9146 
9147 static void
9148 dump_cim_regs(struct adapter *sc)
9149 {
9150 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9151 	    device_get_nameunit(sc->dev),
9152 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9153 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9154 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9155 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9156 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9157 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9158 	    device_get_nameunit(sc->dev),
9159 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9160 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9161 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9162 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9163 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9164 }
9165 
9166 static void
9167 dump_cimla(struct adapter *sc)
9168 {
9169 	struct sbuf sb;
9170 	int rc;
9171 
9172 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9173 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9174 		    device_get_nameunit(sc->dev));
9175 		return;
9176 	}
9177 	rc = sbuf_cim_la(sc, &sb, M_WAITOK);
9178 	if (rc == 0) {
9179 		rc = sbuf_finish(&sb);
9180 		if (rc == 0) {
9181 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9182 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9183 		}
9184 	}
9185 	sbuf_delete(&sb);
9186 }
9187 
9188 void
9189 t4_os_cim_err(struct adapter *sc)
9190 {
9191 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9192 }
9193 
9194 static int
9195 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9196 {
9197 	struct adapter *sc = arg1;
9198 	u_int i;
9199 	struct sbuf *sb;
9200 	uint32_t *buf, *p;
9201 	int rc;
9202 
9203 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9204 	if (sb == NULL)
9205 		return (ENOMEM);
9206 
9207 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9208 	    M_ZERO | M_WAITOK);
9209 
9210 	rc = 0;
9211 	mtx_lock(&sc->reg_lock);
9212 	if (hw_off_limits(sc))
9213 		rc = ENXIO;
9214 	else
9215 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9216 	mtx_unlock(&sc->reg_lock);
9217 	if (rc)
9218 		goto done;
9219 
9220 	p = buf;
9221 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9222 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9223 		    p[1], p[0]);
9224 	}
9225 
9226 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9227 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9228 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9229 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9230 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9231 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9232 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9233 		    p[0] & 1);
9234 	}
9235 	rc = sbuf_finish(sb);
9236 done:
9237 	sbuf_delete(sb);
9238 	free(buf, M_CXGBE);
9239 	return (rc);
9240 }
9241 
9242 static int
9243 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9244 {
9245 	struct adapter *sc = arg1;
9246 	u_int i;
9247 	struct sbuf *sb;
9248 	uint32_t *buf, *p;
9249 	int rc;
9250 
9251 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9252 	if (sb == NULL)
9253 		return (ENOMEM);
9254 
9255 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9256 	    M_ZERO | M_WAITOK);
9257 
9258 	rc = 0;
9259 	mtx_lock(&sc->reg_lock);
9260 	if (hw_off_limits(sc))
9261 		rc = ENXIO;
9262 	else
9263 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9264 	mtx_unlock(&sc->reg_lock);
9265 	if (rc)
9266 		goto done;
9267 
9268 	p = buf;
9269 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9270 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9271 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9272 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9273 		    p[4], p[3], p[2], p[1], p[0]);
9274 	}
9275 
9276 	sbuf_printf(sb, "\n\nCntl ID               Data");
9277 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9278 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9279 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9280 	}
9281 
9282 	rc = sbuf_finish(sb);
9283 done:
9284 	sbuf_delete(sb);
9285 	free(buf, M_CXGBE);
9286 	return (rc);
9287 }
9288 
9289 static int
9290 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9291 {
9292 	struct adapter *sc = arg1;
9293 	struct sbuf *sb;
9294 	int rc, i;
9295 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9296 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9297 	uint16_t thres[CIM_NUM_IBQ];
9298 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9299 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9300 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9301 
9302 	cim_num_obq = sc->chip_params->cim_num_obq;
9303 	if (is_t4(sc)) {
9304 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9305 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9306 	} else {
9307 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9308 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9309 	}
9310 	nq = CIM_NUM_IBQ + cim_num_obq;
9311 
9312 	mtx_lock(&sc->reg_lock);
9313 	if (hw_off_limits(sc))
9314 		rc = ENXIO;
9315 	else {
9316 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9317 		if (rc == 0) {
9318 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9319 			    obq_wr);
9320 			if (rc == 0)
9321 				t4_read_cimq_cfg(sc, base, size, thres);
9322 		}
9323 	}
9324 	mtx_unlock(&sc->reg_lock);
9325 	if (rc)
9326 		return (rc);
9327 
9328 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9329 	if (sb == NULL)
9330 		return (ENOMEM);
9331 
9332 	sbuf_printf(sb,
9333 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9334 
9335 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9336 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9337 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9338 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9339 		    G_QUEREMFLITS(p[2]) * 16);
9340 	for ( ; i < nq; i++, p += 4, wr += 2)
9341 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9342 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9343 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9344 		    G_QUEREMFLITS(p[2]) * 16);
9345 
9346 	rc = sbuf_finish(sb);
9347 	sbuf_delete(sb);
9348 
9349 	return (rc);
9350 }
9351 
9352 static int
9353 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9354 {
9355 	struct adapter *sc = arg1;
9356 	struct sbuf *sb;
9357 	int rc;
9358 	struct tp_cpl_stats stats;
9359 
9360 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9361 	if (sb == NULL)
9362 		return (ENOMEM);
9363 
9364 	rc = 0;
9365 	mtx_lock(&sc->reg_lock);
9366 	if (hw_off_limits(sc))
9367 		rc = ENXIO;
9368 	else
9369 		t4_tp_get_cpl_stats(sc, &stats, 0);
9370 	mtx_unlock(&sc->reg_lock);
9371 	if (rc)
9372 		goto done;
9373 
9374 	if (sc->chip_params->nchan > 2) {
9375 		sbuf_printf(sb, "                 channel 0  channel 1"
9376 		    "  channel 2  channel 3");
9377 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9378 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9379 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9380 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9381 	} else {
9382 		sbuf_printf(sb, "                 channel 0  channel 1");
9383 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9384 		    stats.req[0], stats.req[1]);
9385 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9386 		    stats.rsp[0], stats.rsp[1]);
9387 	}
9388 
9389 	rc = sbuf_finish(sb);
9390 done:
9391 	sbuf_delete(sb);
9392 	return (rc);
9393 }
9394 
9395 static int
9396 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9397 {
9398 	struct adapter *sc = arg1;
9399 	struct sbuf *sb;
9400 	int rc;
9401 	struct tp_usm_stats stats;
9402 
9403 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9404 	if (sb == NULL)
9405 		return (ENOMEM);
9406 
9407 	rc = 0;
9408 	mtx_lock(&sc->reg_lock);
9409 	if (hw_off_limits(sc))
9410 		rc = ENXIO;
9411 	else
9412 		t4_get_usm_stats(sc, &stats, 1);
9413 	mtx_unlock(&sc->reg_lock);
9414 	if (rc == 0) {
9415 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
9416 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
9417 		sbuf_printf(sb, "Drops:  %u", stats.drops);
9418 		rc = sbuf_finish(sb);
9419 	}
9420 	sbuf_delete(sb);
9421 
9422 	return (rc);
9423 }
9424 
9425 static int
9426 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
9427 {
9428 	struct adapter *sc = arg1;
9429 	struct sbuf *sb;
9430 	int rc;
9431 	struct tp_tid_stats stats;
9432 
9433 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9434 	if (sb == NULL)
9435 		return (ENOMEM);
9436 
9437 	rc = 0;
9438 	mtx_lock(&sc->reg_lock);
9439 	if (hw_off_limits(sc))
9440 		rc = ENXIO;
9441 	else
9442 		t4_tp_get_tid_stats(sc, &stats, 1);
9443 	mtx_unlock(&sc->reg_lock);
9444 	if (rc == 0) {
9445 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
9446 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
9447 		sbuf_printf(sb, "Active:     %u\n", stats.act);
9448 		sbuf_printf(sb, "Passive:    %u", stats.pas);
9449 		rc = sbuf_finish(sb);
9450 	}
9451 	sbuf_delete(sb);
9452 
9453 	return (rc);
9454 }
9455 
9456 static const char * const devlog_level_strings[] = {
9457 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
9458 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
9459 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
9460 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
9461 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
9462 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
9463 };
9464 
9465 static const char * const devlog_facility_strings[] = {
9466 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
9467 	[FW_DEVLOG_FACILITY_CF]		= "CF",
9468 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
9469 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
9470 	[FW_DEVLOG_FACILITY_RES]	= "RES",
9471 	[FW_DEVLOG_FACILITY_HW]		= "HW",
9472 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
9473 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
9474 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
9475 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
9476 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
9477 	[FW_DEVLOG_FACILITY_VI]		= "VI",
9478 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
9479 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
9480 	[FW_DEVLOG_FACILITY_TM]		= "TM",
9481 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
9482 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
9483 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
9484 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
9485 	[FW_DEVLOG_FACILITY_RI]		= "RI",
9486 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
9487 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
9488 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
9489 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
9490 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
9491 };
9492 
9493 static int
9494 sbuf_devlog(struct adapter *sc, struct sbuf *sb, int flags)
9495 {
9496 	int i, j, rc, nentries, first = 0;
9497 	struct devlog_params *dparams = &sc->params.devlog;
9498 	struct fw_devlog_e *buf, *e;
9499 	uint64_t ftstamp = UINT64_MAX;
9500 
9501 	if (dparams->addr == 0)
9502 		return (ENXIO);
9503 
9504 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9505 	buf = malloc(dparams->size, M_CXGBE, M_ZERO | flags);
9506 	if (buf == NULL)
9507 		return (ENOMEM);
9508 
9509 	mtx_lock(&sc->reg_lock);
9510 	if (hw_off_limits(sc))
9511 		rc = ENXIO;
9512 	else
9513 		rc = read_via_memwin(sc, 1, dparams->addr, (void *)buf,
9514 		    dparams->size);
9515 	mtx_unlock(&sc->reg_lock);
9516 	if (rc != 0)
9517 		goto done;
9518 
9519 	nentries = dparams->size / sizeof(struct fw_devlog_e);
9520 	for (i = 0; i < nentries; i++) {
9521 		e = &buf[i];
9522 
9523 		if (e->timestamp == 0)
9524 			break;	/* end */
9525 
9526 		e->timestamp = be64toh(e->timestamp);
9527 		e->seqno = be32toh(e->seqno);
9528 		for (j = 0; j < 8; j++)
9529 			e->params[j] = be32toh(e->params[j]);
9530 
9531 		if (e->timestamp < ftstamp) {
9532 			ftstamp = e->timestamp;
9533 			first = i;
9534 		}
9535 	}
9536 
9537 	if (buf[first].timestamp == 0)
9538 		goto done;	/* nothing in the log */
9539 
9540 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
9541 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
9542 
9543 	i = first;
9544 	do {
9545 		e = &buf[i];
9546 		if (e->timestamp == 0)
9547 			break;	/* end */
9548 
9549 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
9550 		    e->seqno, e->timestamp,
9551 		    (e->level < nitems(devlog_level_strings) ?
9552 			devlog_level_strings[e->level] : "UNKNOWN"),
9553 		    (e->facility < nitems(devlog_facility_strings) ?
9554 			devlog_facility_strings[e->facility] : "UNKNOWN"));
9555 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
9556 		    e->params[2], e->params[3], e->params[4],
9557 		    e->params[5], e->params[6], e->params[7]);
9558 
9559 		if (++i == nentries)
9560 			i = 0;
9561 	} while (i != first);
9562 done:
9563 	free(buf, M_CXGBE);
9564 	return (rc);
9565 }
9566 
9567 static int
9568 sysctl_devlog(SYSCTL_HANDLER_ARGS)
9569 {
9570 	struct adapter *sc = arg1;
9571 	int rc;
9572 	struct sbuf *sb;
9573 
9574 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9575 	if (sb == NULL)
9576 		return (ENOMEM);
9577 
9578 	rc = sbuf_devlog(sc, sb, M_WAITOK);
9579 	if (rc == 0)
9580 		rc = sbuf_finish(sb);
9581 	sbuf_delete(sb);
9582 	return (rc);
9583 }
9584 
9585 static void
9586 dump_devlog(struct adapter *sc)
9587 {
9588 	int rc;
9589 	struct sbuf sb;
9590 
9591 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9592 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
9593 		    device_get_nameunit(sc->dev));
9594 		return;
9595 	}
9596 	rc = sbuf_devlog(sc, &sb, M_WAITOK);
9597 	if (rc == 0) {
9598 		rc = sbuf_finish(&sb);
9599 		if (rc == 0) {
9600 			log(LOG_DEBUG, "%s: device log follows.\n%s",
9601 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9602 		}
9603 	}
9604 	sbuf_delete(&sb);
9605 }
9606 
9607 static int
9608 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
9609 {
9610 	struct adapter *sc = arg1;
9611 	struct sbuf *sb;
9612 	int rc;
9613 	struct tp_fcoe_stats stats[MAX_NCHAN];
9614 	int i, nchan = sc->chip_params->nchan;
9615 
9616 	rc = 0;
9617 	mtx_lock(&sc->reg_lock);
9618 	if (hw_off_limits(sc))
9619 		rc = ENXIO;
9620 	else {
9621 		for (i = 0; i < nchan; i++)
9622 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
9623 	}
9624 	mtx_unlock(&sc->reg_lock);
9625 	if (rc != 0)
9626 		return (rc);
9627 
9628 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9629 	if (sb == NULL)
9630 		return (ENOMEM);
9631 
9632 	if (nchan > 2) {
9633 		sbuf_printf(sb, "                   channel 0        channel 1"
9634 		    "        channel 2        channel 3");
9635 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
9636 		    stats[0].octets_ddp, stats[1].octets_ddp,
9637 		    stats[2].octets_ddp, stats[3].octets_ddp);
9638 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
9639 		    stats[0].frames_ddp, stats[1].frames_ddp,
9640 		    stats[2].frames_ddp, stats[3].frames_ddp);
9641 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
9642 		    stats[0].frames_drop, stats[1].frames_drop,
9643 		    stats[2].frames_drop, stats[3].frames_drop);
9644 	} else {
9645 		sbuf_printf(sb, "                   channel 0        channel 1");
9646 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
9647 		    stats[0].octets_ddp, stats[1].octets_ddp);
9648 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
9649 		    stats[0].frames_ddp, stats[1].frames_ddp);
9650 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
9651 		    stats[0].frames_drop, stats[1].frames_drop);
9652 	}
9653 
9654 	rc = sbuf_finish(sb);
9655 	sbuf_delete(sb);
9656 
9657 	return (rc);
9658 }
9659 
9660 static int
9661 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
9662 {
9663 	struct adapter *sc = arg1;
9664 	struct sbuf *sb;
9665 	int rc, i;
9666 	unsigned int map, kbps, ipg, mode;
9667 	unsigned int pace_tab[NTX_SCHED];
9668 
9669 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
9670 	if (sb == NULL)
9671 		return (ENOMEM);
9672 
9673 	mtx_lock(&sc->reg_lock);
9674 	if (hw_off_limits(sc)) {
9675 		mtx_unlock(&sc->reg_lock);
9676 		rc = ENXIO;
9677 		goto done;
9678 	}
9679 
9680 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
9681 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
9682 	t4_read_pace_tbl(sc, pace_tab);
9683 	mtx_unlock(&sc->reg_lock);
9684 
9685 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
9686 	    "Class IPG (0.1 ns)   Flow IPG (us)");
9687 
9688 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
9689 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
9690 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
9691 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
9692 		if (kbps)
9693 			sbuf_printf(sb, "%9u     ", kbps);
9694 		else
9695 			sbuf_printf(sb, " disabled     ");
9696 
9697 		if (ipg)
9698 			sbuf_printf(sb, "%13u        ", ipg);
9699 		else
9700 			sbuf_printf(sb, "     disabled        ");
9701 
9702 		if (pace_tab[i])
9703 			sbuf_printf(sb, "%10u", pace_tab[i]);
9704 		else
9705 			sbuf_printf(sb, "  disabled");
9706 	}
9707 	rc = sbuf_finish(sb);
9708 done:
9709 	sbuf_delete(sb);
9710 	return (rc);
9711 }
9712 
9713 static int
9714 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
9715 {
9716 	struct adapter *sc = arg1;
9717 	struct sbuf *sb;
9718 	int rc, i, j;
9719 	uint64_t *p0, *p1;
9720 	struct lb_port_stats s[2];
9721 	static const char *stat_name[] = {
9722 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
9723 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
9724 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
9725 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
9726 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
9727 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
9728 		"BG2FramesTrunc:", "BG3FramesTrunc:"
9729 	};
9730 
9731 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9732 	if (sb == NULL)
9733 		return (ENOMEM);
9734 
9735 	memset(s, 0, sizeof(s));
9736 
9737 	rc = 0;
9738 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
9739 		mtx_lock(&sc->reg_lock);
9740 		if (hw_off_limits(sc))
9741 			rc = ENXIO;
9742 		else {
9743 			t4_get_lb_stats(sc, i, &s[0]);
9744 			t4_get_lb_stats(sc, i + 1, &s[1]);
9745 		}
9746 		mtx_unlock(&sc->reg_lock);
9747 		if (rc != 0)
9748 			break;
9749 
9750 		p0 = &s[0].octets;
9751 		p1 = &s[1].octets;
9752 		sbuf_printf(sb, "%s                       Loopback %u"
9753 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
9754 
9755 		for (j = 0; j < nitems(stat_name); j++)
9756 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
9757 				   *p0++, *p1++);
9758 	}
9759 
9760 	if (rc == 0)
9761 		rc = sbuf_finish(sb);
9762 	sbuf_delete(sb);
9763 
9764 	return (rc);
9765 }
9766 
9767 static int
9768 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
9769 {
9770 	int rc = 0;
9771 	struct port_info *pi = arg1;
9772 	struct link_config *lc = &pi->link_cfg;
9773 	struct sbuf *sb;
9774 
9775 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
9776 	if (sb == NULL)
9777 		return (ENOMEM);
9778 
9779 	if (lc->link_ok || lc->link_down_rc == 255)
9780 		sbuf_printf(sb, "n/a");
9781 	else
9782 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
9783 
9784 	rc = sbuf_finish(sb);
9785 	sbuf_delete(sb);
9786 
9787 	return (rc);
9788 }
9789 
9790 struct mem_desc {
9791 	u_int base;
9792 	u_int limit;
9793 	u_int idx;
9794 };
9795 
9796 static int
9797 mem_desc_cmp(const void *a, const void *b)
9798 {
9799 	const u_int v1 = ((const struct mem_desc *)a)->base;
9800 	const u_int v2 = ((const struct mem_desc *)b)->base;
9801 
9802 	if (v1 < v2)
9803 		return (-1);
9804 	else if (v1 > v2)
9805 		return (1);
9806 
9807 	return (0);
9808 }
9809 
9810 static void
9811 mem_region_show(struct sbuf *sb, const char *name, unsigned int from,
9812     unsigned int to)
9813 {
9814 	unsigned int size;
9815 
9816 	if (from == to)
9817 		return;
9818 
9819 	size = to - from + 1;
9820 	if (size == 0)
9821 		return;
9822 
9823 	/* XXX: need humanize_number(3) in libkern for a more readable 'size' */
9824 	sbuf_printf(sb, "%-15s %#x-%#x [%u]\n", name, from, to, size);
9825 }
9826 
9827 static int
9828 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
9829 {
9830 	struct adapter *sc = arg1;
9831 	struct sbuf *sb;
9832 	int rc, i, n;
9833 	uint32_t lo, hi, used, free, alloc;
9834 	static const char *memory[] = {
9835 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
9836 	};
9837 	static const char *region[] = {
9838 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
9839 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
9840 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
9841 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
9842 		"RQUDP region:", "PBL region:", "TXPBL region:",
9843 		"TLSKey region:", "DBVFIFO region:", "ULPRX state:",
9844 		"ULPTX state:", "On-chip queues:",
9845 	};
9846 	struct mem_desc avail[4];
9847 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
9848 	struct mem_desc *md = mem;
9849 
9850 	rc = sysctl_wire_old_buffer(req, 0);
9851 	if (rc != 0)
9852 		return (rc);
9853 
9854 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9855 	if (sb == NULL)
9856 		return (ENOMEM);
9857 
9858 	for (i = 0; i < nitems(mem); i++) {
9859 		mem[i].limit = 0;
9860 		mem[i].idx = i;
9861 	}
9862 
9863 	mtx_lock(&sc->reg_lock);
9864 	if (hw_off_limits(sc)) {
9865 		rc = ENXIO;
9866 		goto done;
9867 	}
9868 
9869 	/* Find and sort the populated memory ranges */
9870 	i = 0;
9871 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
9872 	if (lo & F_EDRAM0_ENABLE) {
9873 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
9874 		avail[i].base = G_EDRAM0_BASE(hi) << 20;
9875 		avail[i].limit = avail[i].base + (G_EDRAM0_SIZE(hi) << 20);
9876 		avail[i].idx = 0;
9877 		i++;
9878 	}
9879 	if (lo & F_EDRAM1_ENABLE) {
9880 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
9881 		avail[i].base = G_EDRAM1_BASE(hi) << 20;
9882 		avail[i].limit = avail[i].base + (G_EDRAM1_SIZE(hi) << 20);
9883 		avail[i].idx = 1;
9884 		i++;
9885 	}
9886 	if (lo & F_EXT_MEM_ENABLE) {
9887 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
9888 		avail[i].base = G_EXT_MEM_BASE(hi) << 20;
9889 		avail[i].limit = avail[i].base + (G_EXT_MEM_SIZE(hi) << 20);
9890 		avail[i].idx = is_t5(sc) ? 3 : 2;	/* Call it MC0 for T5 */
9891 		i++;
9892 	}
9893 	if (is_t5(sc) && lo & F_EXT_MEM1_ENABLE) {
9894 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9895 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9896 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9897 		avail[i].idx = 4;
9898 		i++;
9899 	}
9900 	if (is_t6(sc) && lo & F_HMA_MUX) {
9901 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
9902 		avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
9903 		avail[i].limit = avail[i].base + (G_EXT_MEM1_SIZE(hi) << 20);
9904 		avail[i].idx = 5;
9905 		i++;
9906 	}
9907 	MPASS(i <= nitems(avail));
9908 	if (!i)                                    /* no memory available */
9909 		goto done;
9910 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
9911 
9912 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
9913 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
9914 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
9915 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
9916 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
9917 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
9918 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
9919 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
9920 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
9921 
9922 	/* the next few have explicit upper bounds */
9923 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
9924 	md->limit = md->base - 1 +
9925 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
9926 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
9927 	md++;
9928 
9929 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
9930 	md->limit = md->base - 1 +
9931 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
9932 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
9933 	md++;
9934 
9935 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
9936 		if (chip_id(sc) <= CHELSIO_T5)
9937 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
9938 		else
9939 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
9940 		md->limit = 0;
9941 	} else {
9942 		md->base = 0;
9943 		md->idx = nitems(region);  /* hide it */
9944 	}
9945 	md++;
9946 
9947 #define ulp_region(reg) \
9948 	md->base = t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT);\
9949 	(md++)->limit = t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT)
9950 
9951 	ulp_region(RX_ISCSI);
9952 	ulp_region(RX_TDDP);
9953 	ulp_region(TX_TPT);
9954 	ulp_region(RX_STAG);
9955 	ulp_region(RX_RQ);
9956 	ulp_region(RX_RQUDP);
9957 	ulp_region(RX_PBL);
9958 	ulp_region(TX_PBL);
9959 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
9960 		ulp_region(RX_TLS_KEY);
9961 	}
9962 #undef ulp_region
9963 
9964 	md->base = 0;
9965 	if (is_t4(sc))
9966 		md->idx = nitems(region);
9967 	else {
9968 		uint32_t size = 0;
9969 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
9970 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
9971 
9972 		if (is_t5(sc)) {
9973 			if (sge_ctrl & F_VFIFO_ENABLE)
9974 				size = fifo_size << 2;
9975 		} else
9976 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
9977 
9978 		if (size) {
9979 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
9980 			md->limit = md->base + size - 1;
9981 		} else
9982 			md->idx = nitems(region);
9983 	}
9984 	md++;
9985 
9986 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
9987 	md->limit = 0;
9988 	md++;
9989 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
9990 	md->limit = 0;
9991 	md++;
9992 
9993 	md->base = sc->vres.ocq.start;
9994 	if (sc->vres.ocq.size)
9995 		md->limit = md->base + sc->vres.ocq.size - 1;
9996 	else
9997 		md->idx = nitems(region);  /* hide it */
9998 	md++;
9999 
10000 	/* add any address-space holes, there can be up to 3 */
10001 	for (n = 0; n < i - 1; n++)
10002 		if (avail[n].limit < avail[n + 1].base)
10003 			(md++)->base = avail[n].limit;
10004 	if (avail[n].limit)
10005 		(md++)->base = avail[n].limit;
10006 
10007 	n = md - mem;
10008 	MPASS(n <= nitems(mem));
10009 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10010 
10011 	for (lo = 0; lo < i; lo++)
10012 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10013 				avail[lo].limit - 1);
10014 
10015 	sbuf_printf(sb, "\n");
10016 	for (i = 0; i < n; i++) {
10017 		if (mem[i].idx >= nitems(region))
10018 			continue;                        /* skip holes */
10019 		if (!mem[i].limit)
10020 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10021 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10022 				mem[i].limit);
10023 	}
10024 
10025 	sbuf_printf(sb, "\n");
10026 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10027 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10028 	mem_region_show(sb, "uP RAM:", lo, hi);
10029 
10030 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10031 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10032 	mem_region_show(sb, "uP Extmem2:", lo, hi);
10033 
10034 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10035 	for (i = 0, free = 0; i < 2; i++)
10036 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10037 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10038 		   G_PMRXMAXPAGE(lo), free,
10039 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10,
10040 		   (lo & F_PMRXNUMCHN) ? 2 : 1);
10041 
10042 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10043 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10044 	for (i = 0, free = 0; i < 4; i++)
10045 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10046 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10047 		   G_PMTXMAXPAGE(lo), free,
10048 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10049 		   hi >= (1 << 20) ? 'M' : 'K', 1 << G_PMTXNUMCHN(lo));
10050 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10051 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10052 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10053 
10054 	for (i = 0; i < 4; i++) {
10055 		if (chip_id(sc) > CHELSIO_T5)
10056 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10057 		else
10058 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10059 		if (is_t5(sc)) {
10060 			used = G_T5_USED(lo);
10061 			alloc = G_T5_ALLOC(lo);
10062 		} else {
10063 			used = G_USED(lo);
10064 			alloc = G_ALLOC(lo);
10065 		}
10066 		/* For T6 these are MAC buffer groups */
10067 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10068 		    i, used, alloc);
10069 	}
10070 	for (i = 0; i < sc->chip_params->nchan; i++) {
10071 		if (chip_id(sc) > CHELSIO_T5)
10072 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10073 		else
10074 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10075 		if (is_t5(sc)) {
10076 			used = G_T5_USED(lo);
10077 			alloc = G_T5_ALLOC(lo);
10078 		} else {
10079 			used = G_USED(lo);
10080 			alloc = G_ALLOC(lo);
10081 		}
10082 		/* For T6 these are MAC buffer groups */
10083 		sbuf_printf(sb,
10084 		    "\nLoopback %d using %u pages out of %u allocated",
10085 		    i, used, alloc);
10086 	}
10087 done:
10088 	mtx_unlock(&sc->reg_lock);
10089 	if (rc == 0)
10090 		rc = sbuf_finish(sb);
10091 	sbuf_delete(sb);
10092 	return (rc);
10093 }
10094 
10095 static inline void
10096 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10097 {
10098 	*mask = x | y;
10099 	y = htobe64(y);
10100 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10101 }
10102 
10103 static int
10104 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10105 {
10106 	struct adapter *sc = arg1;
10107 	struct sbuf *sb;
10108 	int rc, i;
10109 
10110 	MPASS(chip_id(sc) <= CHELSIO_T5);
10111 
10112 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10113 	if (sb == NULL)
10114 		return (ENOMEM);
10115 
10116 	sbuf_printf(sb,
10117 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10118 	    "  VF              Replication             P0 P1 P2 P3  ML");
10119 	rc = 0;
10120 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10121 		uint64_t tcamx, tcamy, mask;
10122 		uint32_t cls_lo, cls_hi;
10123 		uint8_t addr[ETHER_ADDR_LEN];
10124 
10125 		mtx_lock(&sc->reg_lock);
10126 		if (hw_off_limits(sc))
10127 			rc = ENXIO;
10128 		else {
10129 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10130 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10131 		}
10132 		mtx_unlock(&sc->reg_lock);
10133 		if (rc != 0)
10134 			break;
10135 		if (tcamx & tcamy)
10136 			continue;
10137 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10138 		mtx_lock(&sc->reg_lock);
10139 		if (hw_off_limits(sc))
10140 			rc = ENXIO;
10141 		else {
10142 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10143 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10144 		}
10145 		mtx_unlock(&sc->reg_lock);
10146 		if (rc != 0)
10147 			break;
10148 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10149 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10150 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10151 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10152 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10153 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10154 
10155 		if (cls_lo & F_REPLICATE) {
10156 			struct fw_ldst_cmd ldst_cmd;
10157 
10158 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10159 			ldst_cmd.op_to_addrspace =
10160 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10161 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10162 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10163 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10164 			ldst_cmd.u.mps.rplc.fid_idx =
10165 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10166 				V_FW_LDST_CMD_IDX(i));
10167 
10168 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10169 			    "t4mps");
10170 			if (rc)
10171 				break;
10172 			if (hw_off_limits(sc))
10173 				rc = ENXIO;
10174 			else
10175 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10176 				    sizeof(ldst_cmd), &ldst_cmd);
10177 			end_synchronized_op(sc, 0);
10178 			if (rc != 0)
10179 				break;
10180 			else {
10181 				sbuf_printf(sb, " %08x %08x %08x %08x",
10182 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10183 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10184 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10185 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10186 			}
10187 		} else
10188 			sbuf_printf(sb, "%36s", "");
10189 
10190 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10191 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10192 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10193 	}
10194 
10195 	if (rc)
10196 		(void) sbuf_finish(sb);
10197 	else
10198 		rc = sbuf_finish(sb);
10199 	sbuf_delete(sb);
10200 
10201 	return (rc);
10202 }
10203 
10204 static int
10205 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10206 {
10207 	struct adapter *sc = arg1;
10208 	struct sbuf *sb;
10209 	int rc, i;
10210 
10211 	MPASS(chip_id(sc) > CHELSIO_T5);
10212 
10213 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10214 	if (sb == NULL)
10215 		return (ENOMEM);
10216 
10217 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10218 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10219 	    "                           Replication"
10220 	    "                                    P0 P1 P2 P3  ML\n");
10221 
10222 	rc = 0;
10223 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10224 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10225 		uint16_t ivlan;
10226 		uint64_t tcamx, tcamy, val, mask;
10227 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10228 		uint8_t addr[ETHER_ADDR_LEN];
10229 
10230 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10231 		if (i < 256)
10232 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10233 		else
10234 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10235 		mtx_lock(&sc->reg_lock);
10236 		if (hw_off_limits(sc))
10237 			rc = ENXIO;
10238 		else {
10239 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10240 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10241 			tcamy = G_DMACH(val) << 32;
10242 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10243 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10244 		}
10245 		mtx_unlock(&sc->reg_lock);
10246 		if (rc != 0)
10247 			break;
10248 
10249 		lookup_type = G_DATALKPTYPE(data2);
10250 		port_num = G_DATAPORTNUM(data2);
10251 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10252 			/* Inner header VNI */
10253 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10254 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10255 			dip_hit = data2 & F_DATADIPHIT;
10256 			vlan_vld = 0;
10257 		} else {
10258 			vniy = 0;
10259 			dip_hit = 0;
10260 			vlan_vld = data2 & F_DATAVIDH2;
10261 			ivlan = G_VIDL(val);
10262 		}
10263 
10264 		ctl |= V_CTLXYBITSEL(1);
10265 		mtx_lock(&sc->reg_lock);
10266 		if (hw_off_limits(sc))
10267 			rc = ENXIO;
10268 		else {
10269 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10270 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10271 			tcamx = G_DMACH(val) << 32;
10272 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10273 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10274 		}
10275 		mtx_unlock(&sc->reg_lock);
10276 		if (rc != 0)
10277 			break;
10278 
10279 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10280 			/* Inner header VNI mask */
10281 			vnix = ((data2 & F_DATAVIDH2) << 23) |
10282 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10283 		} else
10284 			vnix = 0;
10285 
10286 		if (tcamx & tcamy)
10287 			continue;
10288 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10289 
10290 		mtx_lock(&sc->reg_lock);
10291 		if (hw_off_limits(sc))
10292 			rc = ENXIO;
10293 		else {
10294 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10295 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10296 		}
10297 		mtx_unlock(&sc->reg_lock);
10298 		if (rc != 0)
10299 			break;
10300 
10301 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10302 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10303 			    "%012jx %06x %06x    -    -   %3c"
10304 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
10305 			    addr[1], addr[2], addr[3], addr[4], addr[5],
10306 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
10307 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10308 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10309 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10310 		} else {
10311 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
10312 			    "%012jx    -       -   ", i, addr[0], addr[1],
10313 			    addr[2], addr[3], addr[4], addr[5],
10314 			    (uintmax_t)mask);
10315 
10316 			if (vlan_vld)
10317 				sbuf_printf(sb, "%4u   Y     ", ivlan);
10318 			else
10319 				sbuf_printf(sb, "  -    N     ");
10320 
10321 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
10322 			    lookup_type ? 'I' : 'O', port_num,
10323 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
10324 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
10325 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
10326 		}
10327 
10328 
10329 		if (cls_lo & F_T6_REPLICATE) {
10330 			struct fw_ldst_cmd ldst_cmd;
10331 
10332 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10333 			ldst_cmd.op_to_addrspace =
10334 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10335 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10336 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10337 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10338 			ldst_cmd.u.mps.rplc.fid_idx =
10339 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10340 				V_FW_LDST_CMD_IDX(i));
10341 
10342 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10343 			    "t6mps");
10344 			if (rc)
10345 				break;
10346 			if (hw_off_limits(sc))
10347 				rc = ENXIO;
10348 			else
10349 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10350 				    sizeof(ldst_cmd), &ldst_cmd);
10351 			end_synchronized_op(sc, 0);
10352 			if (rc != 0)
10353 				break;
10354 			else {
10355 				sbuf_printf(sb, " %08x %08x %08x %08x"
10356 				    " %08x %08x %08x %08x",
10357 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
10358 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
10359 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
10360 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
10361 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10362 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10363 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10364 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10365 			}
10366 		} else
10367 			sbuf_printf(sb, "%72s", "");
10368 
10369 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
10370 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
10371 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
10372 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
10373 	}
10374 
10375 	if (rc)
10376 		(void) sbuf_finish(sb);
10377 	else
10378 		rc = sbuf_finish(sb);
10379 	sbuf_delete(sb);
10380 
10381 	return (rc);
10382 }
10383 
10384 static int
10385 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
10386 {
10387 	struct adapter *sc = arg1;
10388 	struct sbuf *sb;
10389 	int rc;
10390 	uint16_t mtus[NMTUS];
10391 
10392 	rc = 0;
10393 	mtx_lock(&sc->reg_lock);
10394 	if (hw_off_limits(sc))
10395 		rc = ENXIO;
10396 	else
10397 		t4_read_mtu_tbl(sc, mtus, NULL);
10398 	mtx_unlock(&sc->reg_lock);
10399 	if (rc != 0)
10400 		return (rc);
10401 
10402 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10403 	if (sb == NULL)
10404 		return (ENOMEM);
10405 
10406 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
10407 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
10408 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
10409 	    mtus[14], mtus[15]);
10410 
10411 	rc = sbuf_finish(sb);
10412 	sbuf_delete(sb);
10413 
10414 	return (rc);
10415 }
10416 
10417 static int
10418 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
10419 {
10420 	struct adapter *sc = arg1;
10421 	struct sbuf *sb;
10422 	int rc, i;
10423 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
10424 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
10425 	static const char *tx_stats[MAX_PM_NSTATS] = {
10426 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
10427 		"Tx FIFO wait", NULL, "Tx latency"
10428 	};
10429 	static const char *rx_stats[MAX_PM_NSTATS] = {
10430 		"Read:", "Write bypass:", "Write mem:", "Flush:",
10431 		"Rx FIFO wait", NULL, "Rx latency"
10432 	};
10433 
10434 	rc = 0;
10435 	mtx_lock(&sc->reg_lock);
10436 	if (hw_off_limits(sc))
10437 		rc = ENXIO;
10438 	else {
10439 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
10440 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
10441 	}
10442 	mtx_unlock(&sc->reg_lock);
10443 	if (rc != 0)
10444 		return (rc);
10445 
10446 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10447 	if (sb == NULL)
10448 		return (ENOMEM);
10449 
10450 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
10451 	for (i = 0; i < 4; i++) {
10452 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10453 		    tx_cyc[i]);
10454 	}
10455 
10456 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
10457 	for (i = 0; i < 4; i++) {
10458 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10459 		    rx_cyc[i]);
10460 	}
10461 
10462 	if (chip_id(sc) > CHELSIO_T5) {
10463 		sbuf_printf(sb,
10464 		    "\n              Total wait      Total occupancy");
10465 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10466 		    tx_cyc[i]);
10467 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10468 		    rx_cyc[i]);
10469 
10470 		i += 2;
10471 		MPASS(i < nitems(tx_stats));
10472 
10473 		sbuf_printf(sb,
10474 		    "\n                   Reads           Total wait");
10475 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
10476 		    tx_cyc[i]);
10477 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
10478 		    rx_cyc[i]);
10479 	}
10480 
10481 	rc = sbuf_finish(sb);
10482 	sbuf_delete(sb);
10483 
10484 	return (rc);
10485 }
10486 
10487 static int
10488 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
10489 {
10490 	struct adapter *sc = arg1;
10491 	struct sbuf *sb;
10492 	int rc;
10493 	struct tp_rdma_stats stats;
10494 
10495 	rc = 0;
10496 	mtx_lock(&sc->reg_lock);
10497 	if (hw_off_limits(sc))
10498 		rc = ENXIO;
10499 	else
10500 		t4_tp_get_rdma_stats(sc, &stats, 0);
10501 	mtx_unlock(&sc->reg_lock);
10502 	if (rc != 0)
10503 		return (rc);
10504 
10505 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10506 	if (sb == NULL)
10507 		return (ENOMEM);
10508 
10509 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
10510 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
10511 
10512 	rc = sbuf_finish(sb);
10513 	sbuf_delete(sb);
10514 
10515 	return (rc);
10516 }
10517 
10518 static int
10519 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
10520 {
10521 	struct adapter *sc = arg1;
10522 	struct sbuf *sb;
10523 	int rc;
10524 	struct tp_tcp_stats v4, v6;
10525 
10526 	rc = 0;
10527 	mtx_lock(&sc->reg_lock);
10528 	if (hw_off_limits(sc))
10529 		rc = ENXIO;
10530 	else
10531 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
10532 	mtx_unlock(&sc->reg_lock);
10533 	if (rc != 0)
10534 		return (rc);
10535 
10536 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10537 	if (sb == NULL)
10538 		return (ENOMEM);
10539 
10540 	sbuf_printf(sb,
10541 	    "                                IP                 IPv6\n");
10542 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
10543 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
10544 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
10545 	    v4.tcp_in_segs, v6.tcp_in_segs);
10546 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
10547 	    v4.tcp_out_segs, v6.tcp_out_segs);
10548 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
10549 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
10550 
10551 	rc = sbuf_finish(sb);
10552 	sbuf_delete(sb);
10553 
10554 	return (rc);
10555 }
10556 
10557 static int
10558 sysctl_tids(SYSCTL_HANDLER_ARGS)
10559 {
10560 	struct adapter *sc = arg1;
10561 	struct sbuf *sb;
10562 	int rc;
10563 	uint32_t x, y;
10564 	struct tid_info *t = &sc->tids;
10565 
10566 	rc = 0;
10567 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10568 	if (sb == NULL)
10569 		return (ENOMEM);
10570 
10571 	if (t->natids) {
10572 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
10573 		    t->atids_in_use);
10574 	}
10575 
10576 	if (t->nhpftids) {
10577 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
10578 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
10579 	}
10580 
10581 	if (t->ntids) {
10582 		bool hashen = false;
10583 
10584 		mtx_lock(&sc->reg_lock);
10585 		if (hw_off_limits(sc))
10586 			rc = ENXIO;
10587 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10588 			hashen = true;
10589 			if (chip_id(sc) <= CHELSIO_T5) {
10590 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
10591 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
10592 			} else {
10593 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
10594 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
10595 			}
10596 		}
10597 		mtx_unlock(&sc->reg_lock);
10598 		if (rc != 0)
10599 			goto done;
10600 
10601 		sbuf_printf(sb, "TID range: ");
10602 		if (hashen) {
10603 			if (x)
10604 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
10605 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
10606 		} else {
10607 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
10608 			    t->ntids - 1);
10609 		}
10610 		sbuf_printf(sb, ", in use: %u\n",
10611 		    atomic_load_acq_int(&t->tids_in_use));
10612 	}
10613 
10614 	if (t->nstids) {
10615 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
10616 		    t->stid_base + t->nstids - 1, t->stids_in_use);
10617 	}
10618 
10619 	if (t->nftids) {
10620 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
10621 		    t->ftid_end, t->ftids_in_use);
10622 	}
10623 
10624 	if (t->netids) {
10625 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
10626 		    t->etid_base + t->netids - 1, t->etids_in_use);
10627 	}
10628 
10629 	mtx_lock(&sc->reg_lock);
10630 	if (hw_off_limits(sc))
10631 		rc = ENXIO;
10632 	else {
10633 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
10634 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
10635 	}
10636 	mtx_unlock(&sc->reg_lock);
10637 	if (rc != 0)
10638 		goto done;
10639 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
10640 done:
10641 	if (rc == 0)
10642 		rc = sbuf_finish(sb);
10643 	else
10644 		(void)sbuf_finish(sb);
10645 	sbuf_delete(sb);
10646 
10647 	return (rc);
10648 }
10649 
10650 static int
10651 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
10652 {
10653 	struct adapter *sc = arg1;
10654 	struct sbuf *sb;
10655 	int rc;
10656 	struct tp_err_stats stats;
10657 
10658 	rc = 0;
10659 	mtx_lock(&sc->reg_lock);
10660 	if (hw_off_limits(sc))
10661 		rc = ENXIO;
10662 	else
10663 		t4_tp_get_err_stats(sc, &stats, 0);
10664 	mtx_unlock(&sc->reg_lock);
10665 	if (rc != 0)
10666 		return (rc);
10667 
10668 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10669 	if (sb == NULL)
10670 		return (ENOMEM);
10671 
10672 	if (sc->chip_params->nchan > 2) {
10673 		sbuf_printf(sb, "                 channel 0  channel 1"
10674 		    "  channel 2  channel 3\n");
10675 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
10676 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
10677 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
10678 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
10679 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
10680 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
10681 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
10682 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
10683 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
10684 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
10685 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
10686 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
10687 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
10688 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
10689 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
10690 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
10691 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
10692 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
10693 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
10694 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
10695 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
10696 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
10697 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
10698 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
10699 	} else {
10700 		sbuf_printf(sb, "                 channel 0  channel 1\n");
10701 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
10702 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
10703 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
10704 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
10705 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
10706 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
10707 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
10708 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
10709 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
10710 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
10711 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
10712 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
10713 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
10714 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
10715 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
10716 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
10717 	}
10718 
10719 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
10720 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
10721 
10722 	rc = sbuf_finish(sb);
10723 	sbuf_delete(sb);
10724 
10725 	return (rc);
10726 }
10727 
10728 static int
10729 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
10730 {
10731 	struct adapter *sc = arg1;
10732 	struct sbuf *sb;
10733 	int rc;
10734 	struct tp_tnl_stats stats;
10735 
10736 	rc = 0;
10737 	mtx_lock(&sc->reg_lock);
10738 	if (hw_off_limits(sc))
10739 		rc = ENXIO;
10740 	else
10741 		t4_tp_get_tnl_stats(sc, &stats, 1);
10742 	mtx_unlock(&sc->reg_lock);
10743 	if (rc != 0)
10744 		return (rc);
10745 
10746 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10747 	if (sb == NULL)
10748 		return (ENOMEM);
10749 
10750 	if (sc->chip_params->nchan > 2) {
10751 		sbuf_printf(sb, "           channel 0  channel 1"
10752 		    "  channel 2  channel 3\n");
10753 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
10754 		    stats.out_pkt[0], stats.out_pkt[1],
10755 		    stats.out_pkt[2], stats.out_pkt[3]);
10756 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
10757 		    stats.in_pkt[0], stats.in_pkt[1],
10758 		    stats.in_pkt[2], stats.in_pkt[3]);
10759 	} else {
10760 		sbuf_printf(sb, "           channel 0  channel 1\n");
10761 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
10762 		    stats.out_pkt[0], stats.out_pkt[1]);
10763 		sbuf_printf(sb, "InPkts:   %10u %10u",
10764 		    stats.in_pkt[0], stats.in_pkt[1]);
10765 	}
10766 
10767 	rc = sbuf_finish(sb);
10768 	sbuf_delete(sb);
10769 
10770 	return (rc);
10771 }
10772 
10773 static int
10774 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
10775 {
10776 	struct adapter *sc = arg1;
10777 	struct tp_params *tpp = &sc->params.tp;
10778 	u_int mask;
10779 	int rc;
10780 
10781 	mask = tpp->la_mask >> 16;
10782 	rc = sysctl_handle_int(oidp, &mask, 0, req);
10783 	if (rc != 0 || req->newptr == NULL)
10784 		return (rc);
10785 	if (mask > 0xffff)
10786 		return (EINVAL);
10787 	mtx_lock(&sc->reg_lock);
10788 	if (hw_off_limits(sc))
10789 		rc = ENXIO;
10790 	else {
10791 		tpp->la_mask = mask << 16;
10792 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
10793 		    tpp->la_mask);
10794 	}
10795 	mtx_unlock(&sc->reg_lock);
10796 
10797 	return (rc);
10798 }
10799 
10800 struct field_desc {
10801 	const char *name;
10802 	u_int start;
10803 	u_int width;
10804 };
10805 
10806 static void
10807 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
10808 {
10809 	char buf[32];
10810 	int line_size = 0;
10811 
10812 	while (f->name) {
10813 		uint64_t mask = (1ULL << f->width) - 1;
10814 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
10815 		    ((uintmax_t)v >> f->start) & mask);
10816 
10817 		if (line_size + len >= 79) {
10818 			line_size = 8;
10819 			sbuf_printf(sb, "\n        ");
10820 		}
10821 		sbuf_printf(sb, "%s ", buf);
10822 		line_size += len + 1;
10823 		f++;
10824 	}
10825 	sbuf_printf(sb, "\n");
10826 }
10827 
10828 static const struct field_desc tp_la0[] = {
10829 	{ "RcfOpCodeOut", 60, 4 },
10830 	{ "State", 56, 4 },
10831 	{ "WcfState", 52, 4 },
10832 	{ "RcfOpcSrcOut", 50, 2 },
10833 	{ "CRxError", 49, 1 },
10834 	{ "ERxError", 48, 1 },
10835 	{ "SanityFailed", 47, 1 },
10836 	{ "SpuriousMsg", 46, 1 },
10837 	{ "FlushInputMsg", 45, 1 },
10838 	{ "FlushInputCpl", 44, 1 },
10839 	{ "RssUpBit", 43, 1 },
10840 	{ "RssFilterHit", 42, 1 },
10841 	{ "Tid", 32, 10 },
10842 	{ "InitTcb", 31, 1 },
10843 	{ "LineNumber", 24, 7 },
10844 	{ "Emsg", 23, 1 },
10845 	{ "EdataOut", 22, 1 },
10846 	{ "Cmsg", 21, 1 },
10847 	{ "CdataOut", 20, 1 },
10848 	{ "EreadPdu", 19, 1 },
10849 	{ "CreadPdu", 18, 1 },
10850 	{ "TunnelPkt", 17, 1 },
10851 	{ "RcfPeerFin", 16, 1 },
10852 	{ "RcfReasonOut", 12, 4 },
10853 	{ "TxCchannel", 10, 2 },
10854 	{ "RcfTxChannel", 8, 2 },
10855 	{ "RxEchannel", 6, 2 },
10856 	{ "RcfRxChannel", 5, 1 },
10857 	{ "RcfDataOutSrdy", 4, 1 },
10858 	{ "RxDvld", 3, 1 },
10859 	{ "RxOoDvld", 2, 1 },
10860 	{ "RxCongestion", 1, 1 },
10861 	{ "TxCongestion", 0, 1 },
10862 	{ NULL }
10863 };
10864 
10865 static const struct field_desc tp_la1[] = {
10866 	{ "CplCmdIn", 56, 8 },
10867 	{ "CplCmdOut", 48, 8 },
10868 	{ "ESynOut", 47, 1 },
10869 	{ "EAckOut", 46, 1 },
10870 	{ "EFinOut", 45, 1 },
10871 	{ "ERstOut", 44, 1 },
10872 	{ "SynIn", 43, 1 },
10873 	{ "AckIn", 42, 1 },
10874 	{ "FinIn", 41, 1 },
10875 	{ "RstIn", 40, 1 },
10876 	{ "DataIn", 39, 1 },
10877 	{ "DataInVld", 38, 1 },
10878 	{ "PadIn", 37, 1 },
10879 	{ "RxBufEmpty", 36, 1 },
10880 	{ "RxDdp", 35, 1 },
10881 	{ "RxFbCongestion", 34, 1 },
10882 	{ "TxFbCongestion", 33, 1 },
10883 	{ "TxPktSumSrdy", 32, 1 },
10884 	{ "RcfUlpType", 28, 4 },
10885 	{ "Eread", 27, 1 },
10886 	{ "Ebypass", 26, 1 },
10887 	{ "Esave", 25, 1 },
10888 	{ "Static0", 24, 1 },
10889 	{ "Cread", 23, 1 },
10890 	{ "Cbypass", 22, 1 },
10891 	{ "Csave", 21, 1 },
10892 	{ "CPktOut", 20, 1 },
10893 	{ "RxPagePoolFull", 18, 2 },
10894 	{ "RxLpbkPkt", 17, 1 },
10895 	{ "TxLpbkPkt", 16, 1 },
10896 	{ "RxVfValid", 15, 1 },
10897 	{ "SynLearned", 14, 1 },
10898 	{ "SetDelEntry", 13, 1 },
10899 	{ "SetInvEntry", 12, 1 },
10900 	{ "CpcmdDvld", 11, 1 },
10901 	{ "CpcmdSave", 10, 1 },
10902 	{ "RxPstructsFull", 8, 2 },
10903 	{ "EpcmdDvld", 7, 1 },
10904 	{ "EpcmdFlush", 6, 1 },
10905 	{ "EpcmdTrimPrefix", 5, 1 },
10906 	{ "EpcmdTrimPostfix", 4, 1 },
10907 	{ "ERssIp4Pkt", 3, 1 },
10908 	{ "ERssIp6Pkt", 2, 1 },
10909 	{ "ERssTcpUdpPkt", 1, 1 },
10910 	{ "ERssFceFipPkt", 0, 1 },
10911 	{ NULL }
10912 };
10913 
10914 static const struct field_desc tp_la2[] = {
10915 	{ "CplCmdIn", 56, 8 },
10916 	{ "MpsVfVld", 55, 1 },
10917 	{ "MpsPf", 52, 3 },
10918 	{ "MpsVf", 44, 8 },
10919 	{ "SynIn", 43, 1 },
10920 	{ "AckIn", 42, 1 },
10921 	{ "FinIn", 41, 1 },
10922 	{ "RstIn", 40, 1 },
10923 	{ "DataIn", 39, 1 },
10924 	{ "DataInVld", 38, 1 },
10925 	{ "PadIn", 37, 1 },
10926 	{ "RxBufEmpty", 36, 1 },
10927 	{ "RxDdp", 35, 1 },
10928 	{ "RxFbCongestion", 34, 1 },
10929 	{ "TxFbCongestion", 33, 1 },
10930 	{ "TxPktSumSrdy", 32, 1 },
10931 	{ "RcfUlpType", 28, 4 },
10932 	{ "Eread", 27, 1 },
10933 	{ "Ebypass", 26, 1 },
10934 	{ "Esave", 25, 1 },
10935 	{ "Static0", 24, 1 },
10936 	{ "Cread", 23, 1 },
10937 	{ "Cbypass", 22, 1 },
10938 	{ "Csave", 21, 1 },
10939 	{ "CPktOut", 20, 1 },
10940 	{ "RxPagePoolFull", 18, 2 },
10941 	{ "RxLpbkPkt", 17, 1 },
10942 	{ "TxLpbkPkt", 16, 1 },
10943 	{ "RxVfValid", 15, 1 },
10944 	{ "SynLearned", 14, 1 },
10945 	{ "SetDelEntry", 13, 1 },
10946 	{ "SetInvEntry", 12, 1 },
10947 	{ "CpcmdDvld", 11, 1 },
10948 	{ "CpcmdSave", 10, 1 },
10949 	{ "RxPstructsFull", 8, 2 },
10950 	{ "EpcmdDvld", 7, 1 },
10951 	{ "EpcmdFlush", 6, 1 },
10952 	{ "EpcmdTrimPrefix", 5, 1 },
10953 	{ "EpcmdTrimPostfix", 4, 1 },
10954 	{ "ERssIp4Pkt", 3, 1 },
10955 	{ "ERssIp6Pkt", 2, 1 },
10956 	{ "ERssTcpUdpPkt", 1, 1 },
10957 	{ "ERssFceFipPkt", 0, 1 },
10958 	{ NULL }
10959 };
10960 
10961 static void
10962 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
10963 {
10964 
10965 	field_desc_show(sb, *p, tp_la0);
10966 }
10967 
10968 static void
10969 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
10970 {
10971 
10972 	if (idx)
10973 		sbuf_printf(sb, "\n");
10974 	field_desc_show(sb, p[0], tp_la0);
10975 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10976 		field_desc_show(sb, p[1], tp_la0);
10977 }
10978 
10979 static void
10980 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
10981 {
10982 
10983 	if (idx)
10984 		sbuf_printf(sb, "\n");
10985 	field_desc_show(sb, p[0], tp_la0);
10986 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
10987 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
10988 }
10989 
10990 static int
10991 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
10992 {
10993 	struct adapter *sc = arg1;
10994 	struct sbuf *sb;
10995 	uint64_t *buf, *p;
10996 	int rc;
10997 	u_int i, inc;
10998 	void (*show_func)(struct sbuf *, uint64_t *, int);
10999 
11000 	rc = 0;
11001 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11002 	if (sb == NULL)
11003 		return (ENOMEM);
11004 
11005 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11006 
11007 	mtx_lock(&sc->reg_lock);
11008 	if (hw_off_limits(sc))
11009 		rc = ENXIO;
11010 	else {
11011 		t4_tp_read_la(sc, buf, NULL);
11012 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11013 		case 2:
11014 			inc = 2;
11015 			show_func = tp_la_show2;
11016 			break;
11017 		case 3:
11018 			inc = 2;
11019 			show_func = tp_la_show3;
11020 			break;
11021 		default:
11022 			inc = 1;
11023 			show_func = tp_la_show;
11024 		}
11025 	}
11026 	mtx_unlock(&sc->reg_lock);
11027 	if (rc != 0)
11028 		goto done;
11029 
11030 	p = buf;
11031 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
11032 		(*show_func)(sb, p, i);
11033 	rc = sbuf_finish(sb);
11034 done:
11035 	sbuf_delete(sb);
11036 	free(buf, M_CXGBE);
11037 	return (rc);
11038 }
11039 
11040 static int
11041 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
11042 {
11043 	struct adapter *sc = arg1;
11044 	struct sbuf *sb;
11045 	int rc;
11046 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
11047 
11048 	rc = 0;
11049 	mtx_lock(&sc->reg_lock);
11050 	if (hw_off_limits(sc))
11051 		rc = ENXIO;
11052 	else
11053 		t4_get_chan_txrate(sc, nrate, orate);
11054 	mtx_unlock(&sc->reg_lock);
11055 	if (rc != 0)
11056 		return (rc);
11057 
11058 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11059 	if (sb == NULL)
11060 		return (ENOMEM);
11061 
11062 	if (sc->chip_params->nchan > 2) {
11063 		sbuf_printf(sb, "              channel 0   channel 1"
11064 		    "   channel 2   channel 3\n");
11065 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
11066 		    nrate[0], nrate[1], nrate[2], nrate[3]);
11067 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
11068 		    orate[0], orate[1], orate[2], orate[3]);
11069 	} else {
11070 		sbuf_printf(sb, "              channel 0   channel 1\n");
11071 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
11072 		    nrate[0], nrate[1]);
11073 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
11074 		    orate[0], orate[1]);
11075 	}
11076 
11077 	rc = sbuf_finish(sb);
11078 	sbuf_delete(sb);
11079 
11080 	return (rc);
11081 }
11082 
11083 static int
11084 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
11085 {
11086 	struct adapter *sc = arg1;
11087 	struct sbuf *sb;
11088 	uint32_t *buf, *p;
11089 	int rc, i;
11090 
11091 	rc = 0;
11092 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11093 	if (sb == NULL)
11094 		return (ENOMEM);
11095 
11096 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
11097 	    M_ZERO | M_WAITOK);
11098 
11099 	mtx_lock(&sc->reg_lock);
11100 	if (hw_off_limits(sc))
11101 		rc = ENXIO;
11102 	else
11103 		t4_ulprx_read_la(sc, buf);
11104 	mtx_unlock(&sc->reg_lock);
11105 	if (rc != 0)
11106 		goto done;
11107 
11108 	p = buf;
11109 	sbuf_printf(sb, "      Pcmd        Type   Message"
11110 	    "                Data");
11111 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
11112 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
11113 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
11114 	}
11115 	rc = sbuf_finish(sb);
11116 done:
11117 	sbuf_delete(sb);
11118 	free(buf, M_CXGBE);
11119 	return (rc);
11120 }
11121 
11122 static int
11123 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
11124 {
11125 	struct adapter *sc = arg1;
11126 	struct sbuf *sb;
11127 	int rc;
11128 	uint32_t cfg, s1, s2;
11129 
11130 	MPASS(chip_id(sc) >= CHELSIO_T5);
11131 
11132 	rc = 0;
11133 	mtx_lock(&sc->reg_lock);
11134 	if (hw_off_limits(sc))
11135 		rc = ENXIO;
11136 	else {
11137 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
11138 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
11139 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
11140 	}
11141 	mtx_unlock(&sc->reg_lock);
11142 	if (rc != 0)
11143 		return (rc);
11144 
11145 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11146 	if (sb == NULL)
11147 		return (ENOMEM);
11148 
11149 	if (G_STATSOURCE_T5(cfg) == 7) {
11150 		int mode;
11151 
11152 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
11153 		if (mode == 0)
11154 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
11155 		else if (mode == 1)
11156 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
11157 		else
11158 			sbuf_printf(sb, "unknown mode %d", mode);
11159 	}
11160 	rc = sbuf_finish(sb);
11161 	sbuf_delete(sb);
11162 
11163 	return (rc);
11164 }
11165 
11166 static int
11167 sysctl_cpus(SYSCTL_HANDLER_ARGS)
11168 {
11169 	struct adapter *sc = arg1;
11170 	enum cpu_sets op = arg2;
11171 	cpuset_t cpuset;
11172 	struct sbuf *sb;
11173 	int i, rc;
11174 
11175 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
11176 
11177 	CPU_ZERO(&cpuset);
11178 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
11179 	if (rc != 0)
11180 		return (rc);
11181 
11182 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11183 	if (sb == NULL)
11184 		return (ENOMEM);
11185 
11186 	CPU_FOREACH(i)
11187 		sbuf_printf(sb, "%d ", i);
11188 	rc = sbuf_finish(sb);
11189 	sbuf_delete(sb);
11190 
11191 	return (rc);
11192 }
11193 
11194 static int
11195 sysctl_reset(SYSCTL_HANDLER_ARGS)
11196 {
11197 	struct adapter *sc = arg1;
11198 	u_int val;
11199 	int rc;
11200 
11201 	val = atomic_load_int(&sc->num_resets);
11202 	rc = sysctl_handle_int(oidp, &val, 0, req);
11203 	if (rc != 0 || req->newptr == NULL)
11204 		return (rc);
11205 
11206 	if (val == 0) {
11207 		/* Zero out the counter that tracks reset. */
11208 		atomic_store_int(&sc->num_resets, 0);
11209 		return (0);
11210 	}
11211 
11212 	if (val != 1)
11213 		return (EINVAL);	/* 0 or 1 are the only legal values */
11214 
11215 	if (hw_off_limits(sc))		/* harmless race */
11216 		return (EALREADY);
11217 
11218 	taskqueue_enqueue(reset_tq, &sc->reset_task);
11219 	return (0);
11220 }
11221 
11222 #ifdef TCP_OFFLOAD
11223 static int
11224 sysctl_tls(SYSCTL_HANDLER_ARGS)
11225 {
11226 	struct adapter *sc = arg1;
11227 	int i, j, v, rc;
11228 	struct vi_info *vi;
11229 
11230 	v = sc->tt.tls;
11231 	rc = sysctl_handle_int(oidp, &v, 0, req);
11232 	if (rc != 0 || req->newptr == NULL)
11233 		return (rc);
11234 
11235 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
11236 		return (ENOTSUP);
11237 
11238 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
11239 	if (rc)
11240 		return (rc);
11241 	if (hw_off_limits(sc))
11242 		rc = ENXIO;
11243 	else {
11244 		sc->tt.tls = !!v;
11245 		for_each_port(sc, i) {
11246 			for_each_vi(sc->port[i], j, vi) {
11247 				if (vi->flags & VI_INIT_DONE)
11248 					t4_update_fl_bufsize(vi->ifp);
11249 			}
11250 		}
11251 	}
11252 	end_synchronized_op(sc, 0);
11253 
11254 	return (rc);
11255 
11256 }
11257 
11258 static void
11259 unit_conv(char *buf, size_t len, u_int val, u_int factor)
11260 {
11261 	u_int rem = val % factor;
11262 
11263 	if (rem == 0)
11264 		snprintf(buf, len, "%u", val / factor);
11265 	else {
11266 		while (rem % 10 == 0)
11267 			rem /= 10;
11268 		snprintf(buf, len, "%u.%u", val / factor, rem);
11269 	}
11270 }
11271 
11272 static int
11273 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
11274 {
11275 	struct adapter *sc = arg1;
11276 	char buf[16];
11277 	u_int res, re;
11278 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11279 
11280 	mtx_lock(&sc->reg_lock);
11281 	if (hw_off_limits(sc))
11282 		res = (u_int)-1;
11283 	else
11284 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
11285 	mtx_unlock(&sc->reg_lock);
11286 	if (res == (u_int)-1)
11287 		return (ENXIO);
11288 
11289 	switch (arg2) {
11290 	case 0:
11291 		/* timer_tick */
11292 		re = G_TIMERRESOLUTION(res);
11293 		break;
11294 	case 1:
11295 		/* TCP timestamp tick */
11296 		re = G_TIMESTAMPRESOLUTION(res);
11297 		break;
11298 	case 2:
11299 		/* DACK tick */
11300 		re = G_DELAYEDACKRESOLUTION(res);
11301 		break;
11302 	default:
11303 		return (EDOOFUS);
11304 	}
11305 
11306 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
11307 
11308 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
11309 }
11310 
11311 static int
11312 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
11313 {
11314 	struct adapter *sc = arg1;
11315 	int rc;
11316 	u_int dack_tmr, dack_re, v;
11317 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11318 
11319 	mtx_lock(&sc->reg_lock);
11320 	if (hw_off_limits(sc))
11321 		rc = ENXIO;
11322 	else {
11323 		rc = 0;
11324 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
11325 		    A_TP_TIMER_RESOLUTION));
11326 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
11327 	}
11328 	mtx_unlock(&sc->reg_lock);
11329 	if (rc != 0)
11330 		return (rc);
11331 
11332 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
11333 
11334 	return (sysctl_handle_int(oidp, &v, 0, req));
11335 }
11336 
11337 static int
11338 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
11339 {
11340 	struct adapter *sc = arg1;
11341 	int rc, reg = arg2;
11342 	u_int tre;
11343 	u_long tp_tick_us, v;
11344 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
11345 
11346 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
11347 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
11348 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
11349 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
11350 
11351 	mtx_lock(&sc->reg_lock);
11352 	if (hw_off_limits(sc))
11353 		rc = ENXIO;
11354 	else {
11355 		rc = 0;
11356 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
11357 		tp_tick_us = (cclk_ps << tre) / 1000000;
11358 		if (reg == A_TP_INIT_SRTT)
11359 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
11360 		else
11361 			v = tp_tick_us * t4_read_reg(sc, reg);
11362 	}
11363 	mtx_unlock(&sc->reg_lock);
11364 	if (rc != 0)
11365 		return (rc);
11366 	else
11367 		return (sysctl_handle_long(oidp, &v, 0, req));
11368 }
11369 
11370 /*
11371  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
11372  * passed to this function.
11373  */
11374 static int
11375 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
11376 {
11377 	struct adapter *sc = arg1;
11378 	int rc, idx = arg2;
11379 	u_int v;
11380 
11381 	MPASS(idx >= 0 && idx <= 24);
11382 
11383 	mtx_lock(&sc->reg_lock);
11384 	if (hw_off_limits(sc))
11385 		rc = ENXIO;
11386 	else {
11387 		rc = 0;
11388 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
11389 	}
11390 	mtx_unlock(&sc->reg_lock);
11391 	if (rc != 0)
11392 		return (rc);
11393 	else
11394 		return (sysctl_handle_int(oidp, &v, 0, req));
11395 }
11396 
11397 static int
11398 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
11399 {
11400 	struct adapter *sc = arg1;
11401 	int rc, idx = arg2;
11402 	u_int shift, v, r;
11403 
11404 	MPASS(idx >= 0 && idx < 16);
11405 
11406 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
11407 	shift = (idx & 3) << 3;
11408 	mtx_lock(&sc->reg_lock);
11409 	if (hw_off_limits(sc))
11410 		rc = ENXIO;
11411 	else {
11412 		rc = 0;
11413 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
11414 	}
11415 	mtx_unlock(&sc->reg_lock);
11416 	if (rc != 0)
11417 		return (rc);
11418 	else
11419 		return (sysctl_handle_int(oidp, &v, 0, req));
11420 }
11421 
11422 static int
11423 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
11424 {
11425 	struct vi_info *vi = arg1;
11426 	struct adapter *sc = vi->adapter;
11427 	int idx, rc, i;
11428 	struct sge_ofld_rxq *ofld_rxq;
11429 	uint8_t v;
11430 
11431 	idx = vi->ofld_tmr_idx;
11432 
11433 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11434 	if (rc != 0 || req->newptr == NULL)
11435 		return (rc);
11436 
11437 	if (idx < 0 || idx >= SGE_NTIMERS)
11438 		return (EINVAL);
11439 
11440 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11441 	    "t4otmr");
11442 	if (rc)
11443 		return (rc);
11444 
11445 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
11446 	for_each_ofld_rxq(vi, i, ofld_rxq) {
11447 #ifdef atomic_store_rel_8
11448 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
11449 #else
11450 		ofld_rxq->iq.intr_params = v;
11451 #endif
11452 	}
11453 	vi->ofld_tmr_idx = idx;
11454 
11455 	end_synchronized_op(sc, LOCK_HELD);
11456 	return (0);
11457 }
11458 
11459 static int
11460 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
11461 {
11462 	struct vi_info *vi = arg1;
11463 	struct adapter *sc = vi->adapter;
11464 	int idx, rc;
11465 
11466 	idx = vi->ofld_pktc_idx;
11467 
11468 	rc = sysctl_handle_int(oidp, &idx, 0, req);
11469 	if (rc != 0 || req->newptr == NULL)
11470 		return (rc);
11471 
11472 	if (idx < -1 || idx >= SGE_NCOUNTERS)
11473 		return (EINVAL);
11474 
11475 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
11476 	    "t4opktc");
11477 	if (rc)
11478 		return (rc);
11479 
11480 	if (vi->flags & VI_INIT_DONE)
11481 		rc = EBUSY; /* cannot be changed once the queues are created */
11482 	else
11483 		vi->ofld_pktc_idx = idx;
11484 
11485 	end_synchronized_op(sc, LOCK_HELD);
11486 	return (rc);
11487 }
11488 #endif
11489 
11490 static int
11491 get_sge_context(struct adapter *sc, struct t4_sge_context *cntxt)
11492 {
11493 	int rc;
11494 
11495 	if (cntxt->cid > M_CTXTQID)
11496 		return (EINVAL);
11497 
11498 	if (cntxt->mem_id != CTXT_EGRESS && cntxt->mem_id != CTXT_INGRESS &&
11499 	    cntxt->mem_id != CTXT_FLM && cntxt->mem_id != CTXT_CNM)
11500 		return (EINVAL);
11501 
11502 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
11503 	if (rc)
11504 		return (rc);
11505 
11506 	if (hw_off_limits(sc)) {
11507 		rc = ENXIO;
11508 		goto done;
11509 	}
11510 
11511 	if (sc->flags & FW_OK) {
11512 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cntxt->cid, cntxt->mem_id,
11513 		    &cntxt->data[0]);
11514 		if (rc == 0)
11515 			goto done;
11516 	}
11517 
11518 	/*
11519 	 * Read via firmware failed or wasn't even attempted.  Read directly via
11520 	 * the backdoor.
11521 	 */
11522 	rc = -t4_sge_ctxt_rd_bd(sc, cntxt->cid, cntxt->mem_id, &cntxt->data[0]);
11523 done:
11524 	end_synchronized_op(sc, 0);
11525 	return (rc);
11526 }
11527 
11528 static int
11529 load_fw(struct adapter *sc, struct t4_data *fw)
11530 {
11531 	int rc;
11532 	uint8_t *fw_data;
11533 
11534 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
11535 	if (rc)
11536 		return (rc);
11537 
11538 	if (hw_off_limits(sc)) {
11539 		rc = ENXIO;
11540 		goto done;
11541 	}
11542 
11543 	/*
11544 	 * The firmware, with the sole exception of the memory parity error
11545 	 * handler, runs from memory and not flash.  It is almost always safe to
11546 	 * install a new firmware on a running system.  Just set bit 1 in
11547 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
11548 	 */
11549 	if (sc->flags & FULL_INIT_DONE &&
11550 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
11551 		rc = EBUSY;
11552 		goto done;
11553 	}
11554 
11555 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
11556 
11557 	rc = copyin(fw->data, fw_data, fw->len);
11558 	if (rc == 0)
11559 		rc = -t4_load_fw(sc, fw_data, fw->len);
11560 
11561 	free(fw_data, M_CXGBE);
11562 done:
11563 	end_synchronized_op(sc, 0);
11564 	return (rc);
11565 }
11566 
11567 static int
11568 load_cfg(struct adapter *sc, struct t4_data *cfg)
11569 {
11570 	int rc;
11571 	uint8_t *cfg_data = NULL;
11572 
11573 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11574 	if (rc)
11575 		return (rc);
11576 
11577 	if (hw_off_limits(sc)) {
11578 		rc = ENXIO;
11579 		goto done;
11580 	}
11581 
11582 	if (cfg->len == 0) {
11583 		/* clear */
11584 		rc = -t4_load_cfg(sc, NULL, 0);
11585 		goto done;
11586 	}
11587 
11588 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
11589 
11590 	rc = copyin(cfg->data, cfg_data, cfg->len);
11591 	if (rc == 0)
11592 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
11593 
11594 	free(cfg_data, M_CXGBE);
11595 done:
11596 	end_synchronized_op(sc, 0);
11597 	return (rc);
11598 }
11599 
11600 static int
11601 load_boot(struct adapter *sc, struct t4_bootrom *br)
11602 {
11603 	int rc;
11604 	uint8_t *br_data = NULL;
11605 	u_int offset;
11606 
11607 	if (br->len > 1024 * 1024)
11608 		return (EFBIG);
11609 
11610 	if (br->pf_offset == 0) {
11611 		/* pfidx */
11612 		if (br->pfidx_addr > 7)
11613 			return (EINVAL);
11614 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
11615 		    A_PCIE_PF_EXPROM_OFST)));
11616 	} else if (br->pf_offset == 1) {
11617 		/* offset */
11618 		offset = G_OFFSET(br->pfidx_addr);
11619 	} else {
11620 		return (EINVAL);
11621 	}
11622 
11623 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
11624 	if (rc)
11625 		return (rc);
11626 
11627 	if (hw_off_limits(sc)) {
11628 		rc = ENXIO;
11629 		goto done;
11630 	}
11631 
11632 	if (br->len == 0) {
11633 		/* clear */
11634 		rc = -t4_load_boot(sc, NULL, offset, 0);
11635 		goto done;
11636 	}
11637 
11638 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
11639 
11640 	rc = copyin(br->data, br_data, br->len);
11641 	if (rc == 0)
11642 		rc = -t4_load_boot(sc, br_data, offset, br->len);
11643 
11644 	free(br_data, M_CXGBE);
11645 done:
11646 	end_synchronized_op(sc, 0);
11647 	return (rc);
11648 }
11649 
11650 static int
11651 load_bootcfg(struct adapter *sc, struct t4_data *bc)
11652 {
11653 	int rc;
11654 	uint8_t *bc_data = NULL;
11655 
11656 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
11657 	if (rc)
11658 		return (rc);
11659 
11660 	if (hw_off_limits(sc)) {
11661 		rc = ENXIO;
11662 		goto done;
11663 	}
11664 
11665 	if (bc->len == 0) {
11666 		/* clear */
11667 		rc = -t4_load_bootcfg(sc, NULL, 0);
11668 		goto done;
11669 	}
11670 
11671 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
11672 
11673 	rc = copyin(bc->data, bc_data, bc->len);
11674 	if (rc == 0)
11675 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
11676 
11677 	free(bc_data, M_CXGBE);
11678 done:
11679 	end_synchronized_op(sc, 0);
11680 	return (rc);
11681 }
11682 
11683 static int
11684 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
11685 {
11686 	int rc;
11687 	struct cudbg_init *cudbg;
11688 	void *handle, *buf;
11689 
11690 	/* buf is large, don't block if no memory is available */
11691 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
11692 	if (buf == NULL)
11693 		return (ENOMEM);
11694 
11695 	handle = cudbg_alloc_handle();
11696 	if (handle == NULL) {
11697 		rc = ENOMEM;
11698 		goto done;
11699 	}
11700 
11701 	cudbg = cudbg_get_init(handle);
11702 	cudbg->adap = sc;
11703 	cudbg->print = (cudbg_print_cb)printf;
11704 
11705 #ifndef notyet
11706 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
11707 	    __func__, dump->wr_flash, dump->len, dump->data);
11708 #endif
11709 
11710 	if (dump->wr_flash)
11711 		cudbg->use_flash = 1;
11712 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
11713 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
11714 
11715 	rc = cudbg_collect(handle, buf, &dump->len);
11716 	if (rc != 0)
11717 		goto done;
11718 
11719 	rc = copyout(buf, dump->data, dump->len);
11720 done:
11721 	cudbg_free_handle(handle);
11722 	free(buf, M_CXGBE);
11723 	return (rc);
11724 }
11725 
11726 static void
11727 free_offload_policy(struct t4_offload_policy *op)
11728 {
11729 	struct offload_rule *r;
11730 	int i;
11731 
11732 	if (op == NULL)
11733 		return;
11734 
11735 	r = &op->rule[0];
11736 	for (i = 0; i < op->nrules; i++, r++) {
11737 		free(r->bpf_prog.bf_insns, M_CXGBE);
11738 	}
11739 	free(op->rule, M_CXGBE);
11740 	free(op, M_CXGBE);
11741 }
11742 
11743 static int
11744 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
11745 {
11746 	int i, rc, len;
11747 	struct t4_offload_policy *op, *old;
11748 	struct bpf_program *bf;
11749 	const struct offload_settings *s;
11750 	struct offload_rule *r;
11751 	void *u;
11752 
11753 	if (!is_offload(sc))
11754 		return (ENODEV);
11755 
11756 	if (uop->nrules == 0) {
11757 		/* Delete installed policies. */
11758 		op = NULL;
11759 		goto set_policy;
11760 	} else if (uop->nrules > 256) { /* arbitrary */
11761 		return (E2BIG);
11762 	}
11763 
11764 	/* Copy userspace offload policy to kernel */
11765 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
11766 	op->nrules = uop->nrules;
11767 	len = op->nrules * sizeof(struct offload_rule);
11768 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11769 	rc = copyin(uop->rule, op->rule, len);
11770 	if (rc) {
11771 		free(op->rule, M_CXGBE);
11772 		free(op, M_CXGBE);
11773 		return (rc);
11774 	}
11775 
11776 	r = &op->rule[0];
11777 	for (i = 0; i < op->nrules; i++, r++) {
11778 
11779 		/* Validate open_type */
11780 		if (r->open_type != OPEN_TYPE_LISTEN &&
11781 		    r->open_type != OPEN_TYPE_ACTIVE &&
11782 		    r->open_type != OPEN_TYPE_PASSIVE &&
11783 		    r->open_type != OPEN_TYPE_DONTCARE) {
11784 error:
11785 			/*
11786 			 * Rules 0 to i have malloc'd filters that need to be
11787 			 * freed.  Rules i+1 to nrules have userspace pointers
11788 			 * and should be left alone.
11789 			 */
11790 			op->nrules = i;
11791 			free_offload_policy(op);
11792 			return (rc);
11793 		}
11794 
11795 		/* Validate settings */
11796 		s = &r->settings;
11797 		if ((s->offload != 0 && s->offload != 1) ||
11798 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
11799 		    s->sched_class < -1 ||
11800 		    s->sched_class >= sc->params.nsched_cls) {
11801 			rc = EINVAL;
11802 			goto error;
11803 		}
11804 
11805 		bf = &r->bpf_prog;
11806 		u = bf->bf_insns;	/* userspace ptr */
11807 		bf->bf_insns = NULL;
11808 		if (bf->bf_len == 0) {
11809 			/* legal, matches everything */
11810 			continue;
11811 		}
11812 		len = bf->bf_len * sizeof(*bf->bf_insns);
11813 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
11814 		rc = copyin(u, bf->bf_insns, len);
11815 		if (rc != 0)
11816 			goto error;
11817 
11818 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
11819 			rc = EINVAL;
11820 			goto error;
11821 		}
11822 	}
11823 set_policy:
11824 	rw_wlock(&sc->policy_lock);
11825 	old = sc->policy;
11826 	sc->policy = op;
11827 	rw_wunlock(&sc->policy_lock);
11828 	free_offload_policy(old);
11829 
11830 	return (0);
11831 }
11832 
11833 #define MAX_READ_BUF_SIZE (128 * 1024)
11834 static int
11835 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
11836 {
11837 	uint32_t addr, remaining, n;
11838 	uint32_t *buf;
11839 	int rc;
11840 	uint8_t *dst;
11841 
11842 	mtx_lock(&sc->reg_lock);
11843 	if (hw_off_limits(sc))
11844 		rc = ENXIO;
11845 	else
11846 		rc = validate_mem_range(sc, mr->addr, mr->len);
11847 	mtx_unlock(&sc->reg_lock);
11848 	if (rc != 0)
11849 		return (rc);
11850 
11851 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
11852 	addr = mr->addr;
11853 	remaining = mr->len;
11854 	dst = (void *)mr->data;
11855 
11856 	while (remaining) {
11857 		n = min(remaining, MAX_READ_BUF_SIZE);
11858 		mtx_lock(&sc->reg_lock);
11859 		if (hw_off_limits(sc))
11860 			rc = ENXIO;
11861 		else
11862 			read_via_memwin(sc, 2, addr, buf, n);
11863 		mtx_unlock(&sc->reg_lock);
11864 		if (rc != 0)
11865 			break;
11866 
11867 		rc = copyout(buf, dst, n);
11868 		if (rc != 0)
11869 			break;
11870 
11871 		dst += n;
11872 		remaining -= n;
11873 		addr += n;
11874 	}
11875 
11876 	free(buf, M_CXGBE);
11877 	return (rc);
11878 }
11879 #undef MAX_READ_BUF_SIZE
11880 
11881 static int
11882 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
11883 {
11884 	int rc;
11885 
11886 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
11887 		return (EINVAL);
11888 
11889 	if (i2cd->len > sizeof(i2cd->data))
11890 		return (EFBIG);
11891 
11892 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
11893 	if (rc)
11894 		return (rc);
11895 	if (hw_off_limits(sc))
11896 		rc = ENXIO;
11897 	else
11898 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
11899 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
11900 	end_synchronized_op(sc, 0);
11901 
11902 	return (rc);
11903 }
11904 
11905 static int
11906 clear_stats(struct adapter *sc, u_int port_id)
11907 {
11908 	int i, v, chan_map;
11909 	struct port_info *pi;
11910 	struct vi_info *vi;
11911 	struct sge_rxq *rxq;
11912 	struct sge_txq *txq;
11913 	struct sge_wrq *wrq;
11914 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
11915 	struct sge_ofld_txq *ofld_txq;
11916 #endif
11917 #ifdef TCP_OFFLOAD
11918 	struct sge_ofld_rxq *ofld_rxq;
11919 #endif
11920 
11921 	if (port_id >= sc->params.nports)
11922 		return (EINVAL);
11923 	pi = sc->port[port_id];
11924 	if (pi == NULL)
11925 		return (EIO);
11926 
11927 	mtx_lock(&sc->reg_lock);
11928 	if (!hw_off_limits(sc)) {
11929 		/* MAC stats */
11930 		t4_clr_port_stats(sc, pi->tx_chan);
11931 		if (is_t6(sc)) {
11932 			if (pi->fcs_reg != -1)
11933 				pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
11934 			else
11935 				pi->stats.rx_fcs_err = 0;
11936 		}
11937 		for_each_vi(pi, v, vi) {
11938 			if (vi->flags & VI_INIT_DONE)
11939 				t4_clr_vi_stats(sc, vi->vin);
11940 		}
11941 		chan_map = pi->rx_e_chan_map;
11942 		v = 0;	/* reuse */
11943 		while (chan_map) {
11944 			i = ffs(chan_map) - 1;
11945 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
11946 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
11947 			chan_map &= ~(1 << i);
11948 		}
11949 	}
11950 	mtx_unlock(&sc->reg_lock);
11951 	pi->tx_parse_error = 0;
11952 	pi->tnl_cong_drops = 0;
11953 
11954 	/*
11955 	 * Since this command accepts a port, clear stats for
11956 	 * all VIs on this port.
11957 	 */
11958 	for_each_vi(pi, v, vi) {
11959 		if (vi->flags & VI_INIT_DONE) {
11960 
11961 			for_each_rxq(vi, i, rxq) {
11962 #if defined(INET) || defined(INET6)
11963 				rxq->lro.lro_queued = 0;
11964 				rxq->lro.lro_flushed = 0;
11965 #endif
11966 				rxq->rxcsum = 0;
11967 				rxq->vlan_extraction = 0;
11968 				rxq->vxlan_rxcsum = 0;
11969 
11970 				rxq->fl.cl_allocated = 0;
11971 				rxq->fl.cl_recycled = 0;
11972 				rxq->fl.cl_fast_recycled = 0;
11973 			}
11974 
11975 			for_each_txq(vi, i, txq) {
11976 				txq->txcsum = 0;
11977 				txq->tso_wrs = 0;
11978 				txq->vlan_insertion = 0;
11979 				txq->imm_wrs = 0;
11980 				txq->sgl_wrs = 0;
11981 				txq->txpkt_wrs = 0;
11982 				txq->txpkts0_wrs = 0;
11983 				txq->txpkts1_wrs = 0;
11984 				txq->txpkts0_pkts = 0;
11985 				txq->txpkts1_pkts = 0;
11986 				txq->txpkts_flush = 0;
11987 				txq->raw_wrs = 0;
11988 				txq->vxlan_tso_wrs = 0;
11989 				txq->vxlan_txcsum = 0;
11990 				txq->kern_tls_records = 0;
11991 				txq->kern_tls_short = 0;
11992 				txq->kern_tls_partial = 0;
11993 				txq->kern_tls_full = 0;
11994 				txq->kern_tls_octets = 0;
11995 				txq->kern_tls_waste = 0;
11996 				txq->kern_tls_options = 0;
11997 				txq->kern_tls_header = 0;
11998 				txq->kern_tls_fin = 0;
11999 				txq->kern_tls_fin_short = 0;
12000 				txq->kern_tls_cbc = 0;
12001 				txq->kern_tls_gcm = 0;
12002 				mp_ring_reset_stats(txq->r);
12003 			}
12004 
12005 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12006 			for_each_ofld_txq(vi, i, ofld_txq) {
12007 				ofld_txq->wrq.tx_wrs_direct = 0;
12008 				ofld_txq->wrq.tx_wrs_copied = 0;
12009 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
12010 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
12011 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
12012 				counter_u64_zero(ofld_txq->tx_aio_jobs);
12013 				counter_u64_zero(ofld_txq->tx_aio_octets);
12014 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
12015 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
12016 			}
12017 #endif
12018 #ifdef TCP_OFFLOAD
12019 			for_each_ofld_rxq(vi, i, ofld_rxq) {
12020 				ofld_rxq->fl.cl_allocated = 0;
12021 				ofld_rxq->fl.cl_recycled = 0;
12022 				ofld_rxq->fl.cl_fast_recycled = 0;
12023 				counter_u64_zero(
12024 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
12025 				counter_u64_zero(
12026 				    ofld_rxq->rx_iscsi_ddp_setup_error);
12027 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
12028 				ofld_rxq->rx_iscsi_ddp_octets = 0;
12029 				ofld_rxq->rx_iscsi_fl_pdus = 0;
12030 				ofld_rxq->rx_iscsi_fl_octets = 0;
12031 				ofld_rxq->rx_aio_ddp_jobs = 0;
12032 				ofld_rxq->rx_aio_ddp_octets = 0;
12033 				ofld_rxq->rx_toe_tls_records = 0;
12034 				ofld_rxq->rx_toe_tls_octets = 0;
12035 				ofld_rxq->rx_toe_ddp_octets = 0;
12036 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
12037 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
12038 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
12039 			}
12040 #endif
12041 
12042 			if (IS_MAIN_VI(vi)) {
12043 				wrq = &sc->sge.ctrlq[pi->port_id];
12044 				wrq->tx_wrs_direct = 0;
12045 				wrq->tx_wrs_copied = 0;
12046 			}
12047 		}
12048 	}
12049 
12050 	return (0);
12051 }
12052 
12053 static int
12054 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12055 {
12056 #ifdef INET6
12057 	struct in6_addr in6;
12058 
12059 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12060 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
12061 		return (0);
12062 	else
12063 		return (EIO);
12064 #else
12065 	return (ENOTSUP);
12066 #endif
12067 }
12068 
12069 static int
12070 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
12071 {
12072 #ifdef INET6
12073 	struct in6_addr in6;
12074 
12075 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
12076 	return (t4_release_clip_addr(sc, &in6));
12077 #else
12078 	return (ENOTSUP);
12079 #endif
12080 }
12081 
12082 int
12083 t4_os_find_pci_capability(struct adapter *sc, int cap)
12084 {
12085 	int i;
12086 
12087 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
12088 }
12089 
12090 int
12091 t4_os_pci_save_state(struct adapter *sc)
12092 {
12093 	device_t dev;
12094 	struct pci_devinfo *dinfo;
12095 
12096 	dev = sc->dev;
12097 	dinfo = device_get_ivars(dev);
12098 
12099 	pci_cfg_save(dev, dinfo, 0);
12100 	return (0);
12101 }
12102 
12103 int
12104 t4_os_pci_restore_state(struct adapter *sc)
12105 {
12106 	device_t dev;
12107 	struct pci_devinfo *dinfo;
12108 
12109 	dev = sc->dev;
12110 	dinfo = device_get_ivars(dev);
12111 
12112 	pci_cfg_restore(dev, dinfo);
12113 	return (0);
12114 }
12115 
12116 void
12117 t4_os_portmod_changed(struct port_info *pi)
12118 {
12119 	struct adapter *sc = pi->adapter;
12120 	struct vi_info *vi;
12121 	if_t ifp;
12122 	static const char *mod_str[] = {
12123 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM"
12124 	};
12125 
12126 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
12127 	    ("%s: port_type %u", __func__, pi->port_type));
12128 
12129 	vi = &pi->vi[0];
12130 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
12131 		PORT_LOCK(pi);
12132 		build_medialist(pi);
12133 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
12134 			fixup_link_config(pi);
12135 			apply_link_config(pi);
12136 		}
12137 		PORT_UNLOCK(pi);
12138 		end_synchronized_op(sc, LOCK_HELD);
12139 	}
12140 
12141 	ifp = vi->ifp;
12142 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
12143 		if_printf(ifp, "transceiver unplugged.\n");
12144 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
12145 		if_printf(ifp, "unknown transceiver inserted.\n");
12146 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
12147 		if_printf(ifp, "unsupported transceiver inserted.\n");
12148 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
12149 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
12150 		    port_top_speed(pi), mod_str[pi->mod_type]);
12151 	} else {
12152 		if_printf(ifp, "transceiver (type %d) inserted.\n",
12153 		    pi->mod_type);
12154 	}
12155 }
12156 
12157 void
12158 t4_os_link_changed(struct port_info *pi)
12159 {
12160 	struct vi_info *vi;
12161 	if_t ifp;
12162 	struct link_config *lc = &pi->link_cfg;
12163 	struct adapter *sc = pi->adapter;
12164 	int v;
12165 
12166 	PORT_LOCK_ASSERT_OWNED(pi);
12167 
12168 	if (is_t6(sc)) {
12169 		if (lc->link_ok) {
12170 			if (lc->speed > 25000 ||
12171 			    (lc->speed == 25000 && lc->fec == FEC_RS)) {
12172 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12173 				    A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS);
12174 			} else {
12175 				pi->fcs_reg = T5_PORT_REG(pi->tx_chan,
12176 				    A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS);
12177 			}
12178 			pi->fcs_base = t4_read_reg64(sc, pi->fcs_reg);
12179 			pi->stats.rx_fcs_err = 0;
12180 		} else {
12181 			pi->fcs_reg = -1;
12182 		}
12183 	} else {
12184 		MPASS(pi->fcs_reg != -1);
12185 		MPASS(pi->fcs_base == 0);
12186 	}
12187 
12188 	for_each_vi(pi, v, vi) {
12189 		ifp = vi->ifp;
12190 		if (ifp == NULL || IS_DETACHING(vi))
12191 			continue;
12192 
12193 		if (lc->link_ok) {
12194 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
12195 			if_link_state_change(ifp, LINK_STATE_UP);
12196 		} else {
12197 			if_link_state_change(ifp, LINK_STATE_DOWN);
12198 		}
12199 	}
12200 }
12201 
12202 void
12203 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
12204 {
12205 	struct adapter *sc;
12206 
12207 	sx_slock(&t4_list_lock);
12208 	SLIST_FOREACH(sc, &t4_list, link) {
12209 		/*
12210 		 * func should not make any assumptions about what state sc is
12211 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
12212 		 */
12213 		func(sc, arg);
12214 	}
12215 	sx_sunlock(&t4_list_lock);
12216 }
12217 
12218 static int
12219 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
12220     struct thread *td)
12221 {
12222 	int rc;
12223 	struct adapter *sc = dev->si_drv1;
12224 
12225 	rc = priv_check(td, PRIV_DRIVER);
12226 	if (rc != 0)
12227 		return (rc);
12228 
12229 	switch (cmd) {
12230 	case CHELSIO_T4_GETREG: {
12231 		struct t4_reg *edata = (struct t4_reg *)data;
12232 
12233 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12234 			return (EFAULT);
12235 
12236 		mtx_lock(&sc->reg_lock);
12237 		if (hw_off_limits(sc))
12238 			rc = ENXIO;
12239 		else if (edata->size == 4)
12240 			edata->val = t4_read_reg(sc, edata->addr);
12241 		else if (edata->size == 8)
12242 			edata->val = t4_read_reg64(sc, edata->addr);
12243 		else
12244 			rc = EINVAL;
12245 		mtx_unlock(&sc->reg_lock);
12246 
12247 		break;
12248 	}
12249 	case CHELSIO_T4_SETREG: {
12250 		struct t4_reg *edata = (struct t4_reg *)data;
12251 
12252 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
12253 			return (EFAULT);
12254 
12255 		mtx_lock(&sc->reg_lock);
12256 		if (hw_off_limits(sc))
12257 			rc = ENXIO;
12258 		else if (edata->size == 4) {
12259 			if (edata->val & 0xffffffff00000000)
12260 				rc = EINVAL;
12261 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
12262 		} else if (edata->size == 8)
12263 			t4_write_reg64(sc, edata->addr, edata->val);
12264 		else
12265 			rc = EINVAL;
12266 		mtx_unlock(&sc->reg_lock);
12267 
12268 		break;
12269 	}
12270 	case CHELSIO_T4_REGDUMP: {
12271 		struct t4_regdump *regs = (struct t4_regdump *)data;
12272 		int reglen = t4_get_regs_len(sc);
12273 		uint8_t *buf;
12274 
12275 		if (regs->len < reglen) {
12276 			regs->len = reglen; /* hint to the caller */
12277 			return (ENOBUFS);
12278 		}
12279 
12280 		regs->len = reglen;
12281 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
12282 		mtx_lock(&sc->reg_lock);
12283 		if (hw_off_limits(sc))
12284 			rc = ENXIO;
12285 		else
12286 			get_regs(sc, regs, buf);
12287 		mtx_unlock(&sc->reg_lock);
12288 		if (rc == 0)
12289 			rc = copyout(buf, regs->data, reglen);
12290 		free(buf, M_CXGBE);
12291 		break;
12292 	}
12293 	case CHELSIO_T4_GET_FILTER_MODE:
12294 		rc = get_filter_mode(sc, (uint32_t *)data);
12295 		break;
12296 	case CHELSIO_T4_SET_FILTER_MODE:
12297 		rc = set_filter_mode(sc, *(uint32_t *)data);
12298 		break;
12299 	case CHELSIO_T4_SET_FILTER_MASK:
12300 		rc = set_filter_mask(sc, *(uint32_t *)data);
12301 		break;
12302 	case CHELSIO_T4_GET_FILTER:
12303 		rc = get_filter(sc, (struct t4_filter *)data);
12304 		break;
12305 	case CHELSIO_T4_SET_FILTER:
12306 		rc = set_filter(sc, (struct t4_filter *)data);
12307 		break;
12308 	case CHELSIO_T4_DEL_FILTER:
12309 		rc = del_filter(sc, (struct t4_filter *)data);
12310 		break;
12311 	case CHELSIO_T4_GET_SGE_CONTEXT:
12312 		rc = get_sge_context(sc, (struct t4_sge_context *)data);
12313 		break;
12314 	case CHELSIO_T4_LOAD_FW:
12315 		rc = load_fw(sc, (struct t4_data *)data);
12316 		break;
12317 	case CHELSIO_T4_GET_MEM:
12318 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
12319 		break;
12320 	case CHELSIO_T4_GET_I2C:
12321 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
12322 		break;
12323 	case CHELSIO_T4_CLEAR_STATS:
12324 		rc = clear_stats(sc, *(uint32_t *)data);
12325 		break;
12326 	case CHELSIO_T4_SCHED_CLASS:
12327 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
12328 		break;
12329 	case CHELSIO_T4_SCHED_QUEUE:
12330 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
12331 		break;
12332 	case CHELSIO_T4_GET_TRACER:
12333 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
12334 		break;
12335 	case CHELSIO_T4_SET_TRACER:
12336 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
12337 		break;
12338 	case CHELSIO_T4_LOAD_CFG:
12339 		rc = load_cfg(sc, (struct t4_data *)data);
12340 		break;
12341 	case CHELSIO_T4_LOAD_BOOT:
12342 		rc = load_boot(sc, (struct t4_bootrom *)data);
12343 		break;
12344 	case CHELSIO_T4_LOAD_BOOTCFG:
12345 		rc = load_bootcfg(sc, (struct t4_data *)data);
12346 		break;
12347 	case CHELSIO_T4_CUDBG_DUMP:
12348 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
12349 		break;
12350 	case CHELSIO_T4_SET_OFLD_POLICY:
12351 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
12352 		break;
12353 	case CHELSIO_T4_HOLD_CLIP_ADDR:
12354 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
12355 		break;
12356 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
12357 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
12358 		break;
12359 	default:
12360 		rc = ENOTTY;
12361 	}
12362 
12363 	return (rc);
12364 }
12365 
12366 #ifdef TCP_OFFLOAD
12367 static int
12368 toe_capability(struct vi_info *vi, bool enable)
12369 {
12370 	int rc;
12371 	struct port_info *pi = vi->pi;
12372 	struct adapter *sc = pi->adapter;
12373 
12374 	ASSERT_SYNCHRONIZED_OP(sc);
12375 
12376 	if (!is_offload(sc))
12377 		return (ENODEV);
12378 	if (hw_off_limits(sc))
12379 		return (ENXIO);
12380 
12381 	if (enable) {
12382 #ifdef KERN_TLS
12383 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
12384 			int i, j, n;
12385 			struct port_info *p;
12386 			struct vi_info *v;
12387 
12388 			/*
12389 			 * Reconfigure hardware for TOE if TXTLS is not enabled
12390 			 * on any ifnet.
12391 			 */
12392 			n = 0;
12393 			for_each_port(sc, i) {
12394 				p = sc->port[i];
12395 				for_each_vi(p, j, v) {
12396 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
12397 						CH_WARN(sc,
12398 						    "%s has NIC TLS enabled.\n",
12399 						    device_get_nameunit(v->dev));
12400 						n++;
12401 					}
12402 				}
12403 			}
12404 			if (n > 0) {
12405 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
12406 				    "associated with this adapter before "
12407 				    "trying to enable TOE.\n");
12408 				return (EAGAIN);
12409 			}
12410 			rc = t6_config_kern_tls(sc, false);
12411 			if (rc)
12412 				return (rc);
12413 		}
12414 #endif
12415 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
12416 			/* TOE is already enabled. */
12417 			return (0);
12418 		}
12419 
12420 		/*
12421 		 * We need the port's queues around so that we're able to send
12422 		 * and receive CPLs to/from the TOE even if the ifnet for this
12423 		 * port has never been UP'd administratively.
12424 		 */
12425 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
12426 			return (rc);
12427 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
12428 		    ((rc = vi_init(&pi->vi[0])) != 0))
12429 			return (rc);
12430 
12431 		if (isset(&sc->offload_map, pi->port_id)) {
12432 			/* TOE is enabled on another VI of this port. */
12433 			pi->uld_vis++;
12434 			return (0);
12435 		}
12436 
12437 		if (!uld_active(sc, ULD_TOM)) {
12438 			rc = t4_activate_uld(sc, ULD_TOM);
12439 			if (rc == EAGAIN) {
12440 				log(LOG_WARNING,
12441 				    "You must kldload t4_tom.ko before trying "
12442 				    "to enable TOE on a cxgbe interface.\n");
12443 			}
12444 			if (rc != 0)
12445 				return (rc);
12446 			KASSERT(sc->tom_softc != NULL,
12447 			    ("%s: TOM activated but softc NULL", __func__));
12448 			KASSERT(uld_active(sc, ULD_TOM),
12449 			    ("%s: TOM activated but flag not set", __func__));
12450 		}
12451 
12452 		/* Activate iWARP and iSCSI too, if the modules are loaded. */
12453 		if (!uld_active(sc, ULD_IWARP))
12454 			(void) t4_activate_uld(sc, ULD_IWARP);
12455 		if (!uld_active(sc, ULD_ISCSI))
12456 			(void) t4_activate_uld(sc, ULD_ISCSI);
12457 
12458 		pi->uld_vis++;
12459 		setbit(&sc->offload_map, pi->port_id);
12460 	} else {
12461 		pi->uld_vis--;
12462 
12463 		if (!isset(&sc->offload_map, pi->port_id) || pi->uld_vis > 0)
12464 			return (0);
12465 
12466 		KASSERT(uld_active(sc, ULD_TOM),
12467 		    ("%s: TOM never initialized?", __func__));
12468 		clrbit(&sc->offload_map, pi->port_id);
12469 	}
12470 
12471 	return (0);
12472 }
12473 
12474 /*
12475  * Add an upper layer driver to the global list.
12476  */
12477 int
12478 t4_register_uld(struct uld_info *ui, int id)
12479 {
12480 	int rc;
12481 
12482 	if (id < 0 || id > ULD_MAX)
12483 		return (EINVAL);
12484 	sx_xlock(&t4_uld_list_lock);
12485 	if (t4_uld_list[id] != NULL)
12486 		rc = EEXIST;
12487 	else {
12488 		t4_uld_list[id] = ui;
12489 		rc = 0;
12490 	}
12491 	sx_xunlock(&t4_uld_list_lock);
12492 	return (rc);
12493 }
12494 
12495 int
12496 t4_unregister_uld(struct uld_info *ui, int id)
12497 {
12498 
12499 	if (id < 0 || id > ULD_MAX)
12500 		return (EINVAL);
12501 	sx_xlock(&t4_uld_list_lock);
12502 	MPASS(t4_uld_list[id] == ui);
12503 	t4_uld_list[id] = NULL;
12504 	sx_xunlock(&t4_uld_list_lock);
12505 	return (0);
12506 }
12507 
12508 int
12509 t4_activate_uld(struct adapter *sc, int id)
12510 {
12511 	int rc;
12512 
12513 	ASSERT_SYNCHRONIZED_OP(sc);
12514 
12515 	if (id < 0 || id > ULD_MAX)
12516 		return (EINVAL);
12517 
12518 	/* Adapter needs to be initialized before any ULD can be activated. */
12519 	if (!(sc->flags & FULL_INIT_DONE)) {
12520 		rc = adapter_init(sc);
12521 		if (rc != 0)
12522 			return (rc);
12523 	}
12524 
12525 	sx_slock(&t4_uld_list_lock);
12526 	if (t4_uld_list[id] == NULL)
12527 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
12528 	else {
12529 		rc = t4_uld_list[id]->uld_activate(sc);
12530 		if (rc == 0)
12531 			setbit(&sc->active_ulds, id);
12532 	}
12533 	sx_sunlock(&t4_uld_list_lock);
12534 
12535 	return (rc);
12536 }
12537 
12538 int
12539 t4_deactivate_uld(struct adapter *sc, int id)
12540 {
12541 	int rc;
12542 
12543 	ASSERT_SYNCHRONIZED_OP(sc);
12544 
12545 	if (id < 0 || id > ULD_MAX)
12546 		return (EINVAL);
12547 
12548 	sx_slock(&t4_uld_list_lock);
12549 	if (t4_uld_list[id] == NULL)
12550 		rc = ENXIO;
12551 	else {
12552 		rc = t4_uld_list[id]->uld_deactivate(sc);
12553 		if (rc == 0)
12554 			clrbit(&sc->active_ulds, id);
12555 	}
12556 	sx_sunlock(&t4_uld_list_lock);
12557 
12558 	return (rc);
12559 }
12560 
12561 static int
12562 deactivate_all_uld(struct adapter *sc)
12563 {
12564 	int i, rc;
12565 
12566 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
12567 	if (rc != 0)
12568 		return (ENXIO);
12569 	sx_slock(&t4_uld_list_lock);
12570 	for (i = 0; i <= ULD_MAX; i++) {
12571 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
12572 			continue;
12573 		rc = t4_uld_list[i]->uld_deactivate(sc);
12574 		if (rc != 0)
12575 			break;
12576 		clrbit(&sc->active_ulds, i);
12577 	}
12578 	sx_sunlock(&t4_uld_list_lock);
12579 	end_synchronized_op(sc, 0);
12580 
12581 	return (rc);
12582 }
12583 
12584 static void
12585 stop_all_uld(struct adapter *sc)
12586 {
12587 	int i;
12588 
12589 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
12590 		return;
12591 	sx_slock(&t4_uld_list_lock);
12592 	for (i = 0; i <= ULD_MAX; i++) {
12593 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12594 		    t4_uld_list[i]->uld_stop == NULL)
12595 			continue;
12596 		(void) t4_uld_list[i]->uld_stop(sc);
12597 	}
12598 	sx_sunlock(&t4_uld_list_lock);
12599 	end_synchronized_op(sc, 0);
12600 }
12601 
12602 static void
12603 restart_all_uld(struct adapter *sc)
12604 {
12605 	int i;
12606 
12607 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
12608 		return;
12609 	sx_slock(&t4_uld_list_lock);
12610 	for (i = 0; i <= ULD_MAX; i++) {
12611 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
12612 		    t4_uld_list[i]->uld_restart == NULL)
12613 			continue;
12614 		(void) t4_uld_list[i]->uld_restart(sc);
12615 	}
12616 	sx_sunlock(&t4_uld_list_lock);
12617 	end_synchronized_op(sc, 0);
12618 }
12619 
12620 int
12621 uld_active(struct adapter *sc, int id)
12622 {
12623 
12624 	MPASS(id >= 0 && id <= ULD_MAX);
12625 
12626 	return (isset(&sc->active_ulds, id));
12627 }
12628 #endif
12629 
12630 #ifdef KERN_TLS
12631 static int
12632 ktls_capability(struct adapter *sc, bool enable)
12633 {
12634 	ASSERT_SYNCHRONIZED_OP(sc);
12635 
12636 	if (!is_ktls(sc))
12637 		return (ENODEV);
12638 	if (!is_t6(sc))
12639 		return (0);
12640 	if (hw_off_limits(sc))
12641 		return (ENXIO);
12642 
12643 	if (enable) {
12644 		if (sc->flags & KERN_TLS_ON)
12645 			return (0);	/* already on */
12646 		if (sc->offload_map != 0) {
12647 			CH_WARN(sc,
12648 			    "Disable TOE on all interfaces associated with "
12649 			    "this adapter before trying to enable NIC TLS.\n");
12650 			return (EAGAIN);
12651 		}
12652 		return (t6_config_kern_tls(sc, true));
12653 	} else {
12654 		/*
12655 		 * Nothing to do for disable.  If TOE is enabled sometime later
12656 		 * then toe_capability will reconfigure the hardware.
12657 		 */
12658 		return (0);
12659 	}
12660 }
12661 #endif
12662 
12663 /*
12664  * t  = ptr to tunable.
12665  * nc = number of CPUs.
12666  * c  = compiled in default for that tunable.
12667  */
12668 static void
12669 calculate_nqueues(int *t, int nc, const int c)
12670 {
12671 	int nq;
12672 
12673 	if (*t > 0)
12674 		return;
12675 	nq = *t < 0 ? -*t : c;
12676 	*t = min(nc, nq);
12677 }
12678 
12679 /*
12680  * Come up with reasonable defaults for some of the tunables, provided they're
12681  * not set by the user (in which case we'll use the values as is).
12682  */
12683 static void
12684 tweak_tunables(void)
12685 {
12686 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
12687 
12688 	if (t4_ntxq < 1) {
12689 #ifdef RSS
12690 		t4_ntxq = rss_getnumbuckets();
12691 #else
12692 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
12693 #endif
12694 	}
12695 
12696 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
12697 
12698 	if (t4_nrxq < 1) {
12699 #ifdef RSS
12700 		t4_nrxq = rss_getnumbuckets();
12701 #else
12702 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
12703 #endif
12704 	}
12705 
12706 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
12707 
12708 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12709 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
12710 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
12711 #endif
12712 #ifdef TCP_OFFLOAD
12713 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
12714 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
12715 #endif
12716 
12717 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
12718 	if (t4_toecaps_allowed == -1)
12719 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
12720 #else
12721 	if (t4_toecaps_allowed == -1)
12722 		t4_toecaps_allowed = 0;
12723 #endif
12724 
12725 #ifdef TCP_OFFLOAD
12726 	if (t4_rdmacaps_allowed == -1) {
12727 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
12728 		    FW_CAPS_CONFIG_RDMA_RDMAC;
12729 	}
12730 
12731 	if (t4_iscsicaps_allowed == -1) {
12732 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
12733 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
12734 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
12735 	}
12736 
12737 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
12738 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
12739 
12740 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
12741 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
12742 #else
12743 	if (t4_rdmacaps_allowed == -1)
12744 		t4_rdmacaps_allowed = 0;
12745 
12746 	if (t4_iscsicaps_allowed == -1)
12747 		t4_iscsicaps_allowed = 0;
12748 #endif
12749 
12750 #ifdef DEV_NETMAP
12751 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
12752 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
12753 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
12754 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
12755 #endif
12756 
12757 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
12758 		t4_tmr_idx = TMR_IDX;
12759 
12760 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
12761 		t4_pktc_idx = PKTC_IDX;
12762 
12763 	if (t4_qsize_txq < 128)
12764 		t4_qsize_txq = 128;
12765 
12766 	if (t4_qsize_rxq < 128)
12767 		t4_qsize_rxq = 128;
12768 	while (t4_qsize_rxq & 7)
12769 		t4_qsize_rxq++;
12770 
12771 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
12772 
12773 	/*
12774 	 * Number of VIs to create per-port.  The first VI is the "main" regular
12775 	 * VI for the port.  The rest are additional virtual interfaces on the
12776 	 * same physical port.  Note that the main VI does not have native
12777 	 * netmap support but the extra VIs do.
12778 	 *
12779 	 * Limit the number of VIs per port to the number of available
12780 	 * MAC addresses per port.
12781 	 */
12782 	if (t4_num_vis < 1)
12783 		t4_num_vis = 1;
12784 	if (t4_num_vis > nitems(vi_mac_funcs)) {
12785 		t4_num_vis = nitems(vi_mac_funcs);
12786 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
12787 	}
12788 
12789 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
12790 		pcie_relaxed_ordering = 1;
12791 #if defined(__i386__) || defined(__amd64__)
12792 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
12793 			pcie_relaxed_ordering = 0;
12794 #endif
12795 	}
12796 }
12797 
12798 #ifdef DDB
12799 static void
12800 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
12801 {
12802 	uint32_t base, j, off, pf, reg, save, win_pos;
12803 
12804 	reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
12805 	save = t4_read_reg(sc, reg);
12806 	base = sc->memwin[2].mw_base;
12807 
12808 	if (is_t4(sc)) {
12809 		pf = 0;
12810 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
12811 	} else {
12812 		pf = V_PFNUM(sc->pf);
12813 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
12814 	}
12815 	off = addr - win_pos;
12816 	t4_write_reg(sc, reg, win_pos | pf);
12817 	t4_read_reg(sc, reg);
12818 
12819 	while (len > 0 && !db_pager_quit) {
12820 		uint32_t buf[8];
12821 		for (j = 0; j < 8; j++, off += 4)
12822 			buf[j] = htonl(t4_read_reg(sc, base + off));
12823 
12824 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
12825 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
12826 		    buf[7]);
12827 		if (len <= sizeof(buf))
12828 			len = 0;
12829 		else
12830 			len -= sizeof(buf);
12831 	}
12832 
12833 	t4_write_reg(sc, reg, save);
12834 	t4_read_reg(sc, reg);
12835 }
12836 
12837 static void
12838 t4_dump_tcb(struct adapter *sc, int tid)
12839 {
12840 	uint32_t tcb_addr;
12841 
12842 	/* Dump TCB for the tid */
12843 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
12844 	tcb_addr += tid * TCB_SIZE;
12845 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
12846 }
12847 
12848 static void
12849 t4_dump_devlog(struct adapter *sc)
12850 {
12851 	struct devlog_params *dparams = &sc->params.devlog;
12852 	struct fw_devlog_e e;
12853 	int i, first, j, m, nentries, rc;
12854 	uint64_t ftstamp = UINT64_MAX;
12855 
12856 	if (dparams->start == 0) {
12857 		db_printf("devlog params not valid\n");
12858 		return;
12859 	}
12860 
12861 	nentries = dparams->size / sizeof(struct fw_devlog_e);
12862 	m = fwmtype_to_hwmtype(dparams->memtype);
12863 
12864 	/* Find the first entry. */
12865 	first = -1;
12866 	for (i = 0; i < nentries && !db_pager_quit; i++) {
12867 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12868 		    sizeof(e), (void *)&e);
12869 		if (rc != 0)
12870 			break;
12871 
12872 		if (e.timestamp == 0)
12873 			break;
12874 
12875 		e.timestamp = be64toh(e.timestamp);
12876 		if (e.timestamp < ftstamp) {
12877 			ftstamp = e.timestamp;
12878 			first = i;
12879 		}
12880 	}
12881 
12882 	if (first == -1)
12883 		return;
12884 
12885 	i = first;
12886 	do {
12887 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
12888 		    sizeof(e), (void *)&e);
12889 		if (rc != 0)
12890 			return;
12891 
12892 		if (e.timestamp == 0)
12893 			return;
12894 
12895 		e.timestamp = be64toh(e.timestamp);
12896 		e.seqno = be32toh(e.seqno);
12897 		for (j = 0; j < 8; j++)
12898 			e.params[j] = be32toh(e.params[j]);
12899 
12900 		db_printf("%10d  %15ju  %8s  %8s  ",
12901 		    e.seqno, e.timestamp,
12902 		    (e.level < nitems(devlog_level_strings) ?
12903 			devlog_level_strings[e.level] : "UNKNOWN"),
12904 		    (e.facility < nitems(devlog_facility_strings) ?
12905 			devlog_facility_strings[e.facility] : "UNKNOWN"));
12906 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
12907 		    e.params[3], e.params[4], e.params[5], e.params[6],
12908 		    e.params[7]);
12909 
12910 		if (++i == nentries)
12911 			i = 0;
12912 	} while (i != first && !db_pager_quit);
12913 }
12914 
12915 static DB_DEFINE_TABLE(show, t4, show_t4);
12916 
12917 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
12918 {
12919 	device_t dev;
12920 	int t;
12921 	bool valid;
12922 
12923 	valid = false;
12924 	t = db_read_token();
12925 	if (t == tIDENT) {
12926 		dev = device_lookup_by_name(db_tok_string);
12927 		valid = true;
12928 	}
12929 	db_skip_to_eol();
12930 	if (!valid) {
12931 		db_printf("usage: show t4 devlog <nexus>\n");
12932 		return;
12933 	}
12934 
12935 	if (dev == NULL) {
12936 		db_printf("device not found\n");
12937 		return;
12938 	}
12939 
12940 	t4_dump_devlog(device_get_softc(dev));
12941 }
12942 
12943 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
12944 {
12945 	device_t dev;
12946 	int radix, tid, t;
12947 	bool valid;
12948 
12949 	valid = false;
12950 	radix = db_radix;
12951 	db_radix = 10;
12952 	t = db_read_token();
12953 	if (t == tIDENT) {
12954 		dev = device_lookup_by_name(db_tok_string);
12955 		t = db_read_token();
12956 		if (t == tNUMBER) {
12957 			tid = db_tok_number;
12958 			valid = true;
12959 		}
12960 	}
12961 	db_radix = radix;
12962 	db_skip_to_eol();
12963 	if (!valid) {
12964 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
12965 		return;
12966 	}
12967 
12968 	if (dev == NULL) {
12969 		db_printf("device not found\n");
12970 		return;
12971 	}
12972 	if (tid < 0) {
12973 		db_printf("invalid tid\n");
12974 		return;
12975 	}
12976 
12977 	t4_dump_tcb(device_get_softc(dev), tid);
12978 }
12979 
12980 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
12981 {
12982 	device_t dev;
12983 	int radix, t;
12984 	bool valid;
12985 
12986 	valid = false;
12987 	radix = db_radix;
12988 	db_radix = 10;
12989 	t = db_read_token();
12990 	if (t == tIDENT) {
12991 		dev = device_lookup_by_name(db_tok_string);
12992 		t = db_read_token();
12993 		if (t == tNUMBER) {
12994 			addr = db_tok_number;
12995 			t = db_read_token();
12996 			if (t == tNUMBER) {
12997 				count = db_tok_number;
12998 				valid = true;
12999 			}
13000 		}
13001 	}
13002 	db_radix = radix;
13003 	db_skip_to_eol();
13004 	if (!valid) {
13005 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
13006 		return;
13007 	}
13008 
13009 	if (dev == NULL) {
13010 		db_printf("device not found\n");
13011 		return;
13012 	}
13013 	if (addr < 0) {
13014 		db_printf("invalid address\n");
13015 		return;
13016 	}
13017 	if (count <= 0) {
13018 		db_printf("invalid length\n");
13019 		return;
13020 	}
13021 
13022 	t4_dump_mem(device_get_softc(dev), addr, count);
13023 }
13024 #endif
13025 
13026 static eventhandler_tag vxlan_start_evtag;
13027 static eventhandler_tag vxlan_stop_evtag;
13028 
13029 struct vxlan_evargs {
13030 	if_t ifp;
13031 	uint16_t port;
13032 };
13033 
13034 static void
13035 enable_vxlan_rx(struct adapter *sc)
13036 {
13037 	int i, rc;
13038 	struct port_info *pi;
13039 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
13040 
13041 	ASSERT_SYNCHRONIZED_OP(sc);
13042 
13043 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
13044 	    F_VXLAN_EN);
13045 	for_each_port(sc, i) {
13046 		pi = sc->port[i];
13047 		if (pi->vxlan_tcam_entry == true)
13048 			continue;
13049 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
13050 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
13051 		    true);
13052 		if (rc < 0) {
13053 			rc = -rc;
13054 			CH_ERR(&pi->vi[0],
13055 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
13056 		} else {
13057 			MPASS(rc == sc->rawf_base + pi->port_id);
13058 			pi->vxlan_tcam_entry = true;
13059 		}
13060 	}
13061 }
13062 
13063 static void
13064 t4_vxlan_start(struct adapter *sc, void *arg)
13065 {
13066 	struct vxlan_evargs *v = arg;
13067 
13068 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13069 		return;
13070 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
13071 		return;
13072 
13073 	if (sc->vxlan_refcount == 0) {
13074 		sc->vxlan_port = v->port;
13075 		sc->vxlan_refcount = 1;
13076 		if (!hw_off_limits(sc))
13077 			enable_vxlan_rx(sc);
13078 	} else if (sc->vxlan_port == v->port) {
13079 		sc->vxlan_refcount++;
13080 	} else {
13081 		CH_ERR(sc, "VXLAN already configured on port  %d; "
13082 		    "ignoring attempt to configure it on port %d\n",
13083 		    sc->vxlan_port, v->port);
13084 	}
13085 	end_synchronized_op(sc, 0);
13086 }
13087 
13088 static void
13089 t4_vxlan_stop(struct adapter *sc, void *arg)
13090 {
13091 	struct vxlan_evargs *v = arg;
13092 
13093 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
13094 		return;
13095 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
13096 		return;
13097 
13098 	/*
13099 	 * VXLANs may have been configured before the driver was loaded so we
13100 	 * may see more stops than starts.  This is not handled cleanly but at
13101 	 * least we keep the refcount sane.
13102 	 */
13103 	if (sc->vxlan_port != v->port)
13104 		goto done;
13105 	if (sc->vxlan_refcount == 0) {
13106 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
13107 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
13108 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
13109 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
13110 done:
13111 	end_synchronized_op(sc, 0);
13112 }
13113 
13114 static void
13115 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
13116     sa_family_t family, u_int port)
13117 {
13118 	struct vxlan_evargs v;
13119 
13120 	MPASS(family == AF_INET || family == AF_INET6);
13121 	v.ifp = ifp;
13122 	v.port = port;
13123 
13124 	t4_iterate(t4_vxlan_start, &v);
13125 }
13126 
13127 static void
13128 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
13129     u_int port)
13130 {
13131 	struct vxlan_evargs v;
13132 
13133 	MPASS(family == AF_INET || family == AF_INET6);
13134 	v.ifp = ifp;
13135 	v.port = port;
13136 
13137 	t4_iterate(t4_vxlan_stop, &v);
13138 }
13139 
13140 
13141 static struct sx mlu;	/* mod load unload */
13142 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
13143 
13144 static int
13145 mod_event(module_t mod, int cmd, void *arg)
13146 {
13147 	int rc = 0;
13148 	static int loaded = 0;
13149 
13150 	switch (cmd) {
13151 	case MOD_LOAD:
13152 		sx_xlock(&mlu);
13153 		if (loaded++ == 0) {
13154 			t4_sge_modload();
13155 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13156 			    t4_filter_rpl, CPL_COOKIE_FILTER);
13157 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
13158 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
13159 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
13160 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
13161 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
13162 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
13163 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
13164 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
13165 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
13166 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
13167 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
13168 			    do_smt_write_rpl);
13169 			sx_init(&t4_list_lock, "T4/T5 adapters");
13170 			SLIST_INIT(&t4_list);
13171 			callout_init(&fatal_callout, 1);
13172 #ifdef TCP_OFFLOAD
13173 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
13174 #endif
13175 #ifdef INET6
13176 			t4_clip_modload();
13177 #endif
13178 #ifdef KERN_TLS
13179 			t6_ktls_modload();
13180 #endif
13181 			t4_tracer_modload();
13182 			tweak_tunables();
13183 			vxlan_start_evtag =
13184 			    EVENTHANDLER_REGISTER(vxlan_start,
13185 				t4_vxlan_start_handler, NULL,
13186 				EVENTHANDLER_PRI_ANY);
13187 			vxlan_stop_evtag =
13188 			    EVENTHANDLER_REGISTER(vxlan_stop,
13189 				t4_vxlan_stop_handler, NULL,
13190 				EVENTHANDLER_PRI_ANY);
13191 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
13192 			    taskqueue_thread_enqueue, &reset_tq);
13193 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
13194 			    "t4_rst_thr");
13195 		}
13196 		sx_xunlock(&mlu);
13197 		break;
13198 
13199 	case MOD_UNLOAD:
13200 		sx_xlock(&mlu);
13201 		if (--loaded == 0) {
13202 #ifdef TCP_OFFLOAD
13203 			int i;
13204 #endif
13205 			int tries;
13206 
13207 			taskqueue_free(reset_tq);
13208 
13209 			tries = 0;
13210 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
13211 				uprintf("%ju clusters with custom free routine "
13212 				    "still is use.\n", t4_sge_extfree_refs());
13213 				pause("t4unload", 2 * hz);
13214 			}
13215 
13216 			sx_slock(&t4_list_lock);
13217 			if (!SLIST_EMPTY(&t4_list)) {
13218 				rc = EBUSY;
13219 				sx_sunlock(&t4_list_lock);
13220 				goto done_unload;
13221 			}
13222 #ifdef TCP_OFFLOAD
13223 			sx_slock(&t4_uld_list_lock);
13224 			for (i = 0; i <= ULD_MAX; i++) {
13225 				if (t4_uld_list[i] != NULL) {
13226 					rc = EBUSY;
13227 					sx_sunlock(&t4_uld_list_lock);
13228 					sx_sunlock(&t4_list_lock);
13229 					goto done_unload;
13230 				}
13231 			}
13232 			sx_sunlock(&t4_uld_list_lock);
13233 #endif
13234 			sx_sunlock(&t4_list_lock);
13235 
13236 			if (t4_sge_extfree_refs() == 0) {
13237 				EVENTHANDLER_DEREGISTER(vxlan_start,
13238 				    vxlan_start_evtag);
13239 				EVENTHANDLER_DEREGISTER(vxlan_stop,
13240 				    vxlan_stop_evtag);
13241 				t4_tracer_modunload();
13242 #ifdef KERN_TLS
13243 				t6_ktls_modunload();
13244 #endif
13245 #ifdef INET6
13246 				t4_clip_modunload();
13247 #endif
13248 #ifdef TCP_OFFLOAD
13249 				sx_destroy(&t4_uld_list_lock);
13250 #endif
13251 				sx_destroy(&t4_list_lock);
13252 				t4_sge_modunload();
13253 				loaded = 0;
13254 			} else {
13255 				rc = EBUSY;
13256 				loaded++;	/* undo earlier decrement */
13257 			}
13258 		}
13259 done_unload:
13260 		sx_xunlock(&mlu);
13261 		break;
13262 	}
13263 
13264 	return (rc);
13265 }
13266 
13267 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
13268 MODULE_VERSION(t4nex, 1);
13269 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
13270 #ifdef DEV_NETMAP
13271 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
13272 #endif /* DEV_NETMAP */
13273 
13274 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
13275 MODULE_VERSION(t5nex, 1);
13276 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
13277 #ifdef DEV_NETMAP
13278 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
13279 #endif /* DEV_NETMAP */
13280 
13281 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
13282 MODULE_VERSION(t6nex, 1);
13283 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
13284 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
13285 #ifdef DEV_NETMAP
13286 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
13287 #endif /* DEV_NETMAP */
13288 
13289 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
13290 MODULE_VERSION(cxgbe, 1);
13291 
13292 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
13293 MODULE_VERSION(cxl, 1);
13294 
13295 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
13296 MODULE_VERSION(cc, 1);
13297 
13298 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
13299 MODULE_VERSION(vcxgbe, 1);
13300 
13301 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
13302 MODULE_VERSION(vcxl, 1);
13303 
13304 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
13305 MODULE_VERSION(vcc, 1);
13306