xref: /freebsd/sys/dev/cxgbe/t4_main.c (revision 4b17bb0edea0341d81eba3d369bd7739e5683d4b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011, 2025 Chelsio Communications.
5  * Written by: Navdeep Parhar <np@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_ddb.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35 #include "opt_rss.h"
36 
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/priv.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/eventhandler.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/taskqueue.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <sys/firmware.h>
50 #include <sys/sbuf.h>
51 #include <sys/smp.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/if_dl.h>
59 #include <net/if_vlan_var.h>
60 #ifdef RSS
61 #include <net/rss_config.h>
62 #endif
63 #include <netinet/in.h>
64 #include <netinet/ip.h>
65 #ifdef KERN_TLS
66 #include <netinet/tcp_seq.h>
67 #endif
68 #if defined(__i386__) || defined(__amd64__)
69 #include <machine/md_var.h>
70 #include <machine/cputypes.h>
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 #endif
74 #ifdef DDB
75 #include <ddb/ddb.h>
76 #include <ddb/db_lex.h>
77 #endif
78 
79 #include "common/common.h"
80 #include "common/t4_msg.h"
81 #include "common/t4_regs.h"
82 #include "common/t4_regs_values.h"
83 #include "cudbg/cudbg.h"
84 #include "t4_clip.h"
85 #include "t4_ioctl.h"
86 #include "t4_l2t.h"
87 #include "t4_mp_ring.h"
88 #include "t4_if.h"
89 #include "t4_smt.h"
90 
91 /* T4 bus driver interface */
92 static int t4_probe(device_t);
93 static int t4_attach(device_t);
94 static int t4_detach(device_t);
95 static int t4_child_location(device_t, device_t, struct sbuf *);
96 static int t4_ready(device_t);
97 static int t4_read_port_device(device_t, int, device_t *);
98 static int t4_suspend(device_t);
99 static int t4_resume(device_t);
100 static int t4_reset_prepare(device_t, device_t);
101 static int t4_reset_post(device_t, device_t);
102 static device_method_t t4_methods[] = {
103 	DEVMETHOD(device_probe,		t4_probe),
104 	DEVMETHOD(device_attach,	t4_attach),
105 	DEVMETHOD(device_detach,	t4_detach),
106 	DEVMETHOD(device_suspend,	t4_suspend),
107 	DEVMETHOD(device_resume,	t4_resume),
108 
109 	DEVMETHOD(bus_child_location,	t4_child_location),
110 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
111 	DEVMETHOD(bus_reset_post,	t4_reset_post),
112 
113 	DEVMETHOD(t4_is_main_ready,	t4_ready),
114 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
115 
116 	DEVMETHOD_END
117 };
118 static driver_t t4_driver = {
119 	"t4nex",
120 	t4_methods,
121 	sizeof(struct adapter)
122 };
123 
124 
125 /* T4 port (cxgbe) interface */
126 static int cxgbe_probe(device_t);
127 static int cxgbe_attach(device_t);
128 static int cxgbe_detach(device_t);
129 device_method_t cxgbe_methods[] = {
130 	DEVMETHOD(device_probe,		cxgbe_probe),
131 	DEVMETHOD(device_attach,	cxgbe_attach),
132 	DEVMETHOD(device_detach,	cxgbe_detach),
133 	{ 0, 0 }
134 };
135 static driver_t cxgbe_driver = {
136 	"cxgbe",
137 	cxgbe_methods,
138 	sizeof(struct port_info)
139 };
140 
141 /* T4 VI (vcxgbe) interface */
142 static int vcxgbe_probe(device_t);
143 static int vcxgbe_attach(device_t);
144 static int vcxgbe_detach(device_t);
145 static device_method_t vcxgbe_methods[] = {
146 	DEVMETHOD(device_probe,		vcxgbe_probe),
147 	DEVMETHOD(device_attach,	vcxgbe_attach),
148 	DEVMETHOD(device_detach,	vcxgbe_detach),
149 	{ 0, 0 }
150 };
151 static driver_t vcxgbe_driver = {
152 	"vcxgbe",
153 	vcxgbe_methods,
154 	sizeof(struct vi_info)
155 };
156 
157 static d_ioctl_t t4_ioctl;
158 
159 static struct cdevsw t4_cdevsw = {
160        .d_version = D_VERSION,
161        .d_ioctl = t4_ioctl,
162        .d_name = "t4nex",
163 };
164 
165 /* T5 bus driver interface */
166 static int t5_probe(device_t);
167 static device_method_t t5_methods[] = {
168 	DEVMETHOD(device_probe,		t5_probe),
169 	DEVMETHOD(device_attach,	t4_attach),
170 	DEVMETHOD(device_detach,	t4_detach),
171 	DEVMETHOD(device_suspend,	t4_suspend),
172 	DEVMETHOD(device_resume,	t4_resume),
173 
174 	DEVMETHOD(bus_child_location,	t4_child_location),
175 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
176 	DEVMETHOD(bus_reset_post,	t4_reset_post),
177 
178 	DEVMETHOD(t4_is_main_ready,	t4_ready),
179 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
180 
181 	DEVMETHOD_END
182 };
183 static driver_t t5_driver = {
184 	"t5nex",
185 	t5_methods,
186 	sizeof(struct adapter)
187 };
188 
189 
190 /* T5 port (cxl) interface */
191 static driver_t cxl_driver = {
192 	"cxl",
193 	cxgbe_methods,
194 	sizeof(struct port_info)
195 };
196 
197 /* T5 VI (vcxl) interface */
198 static driver_t vcxl_driver = {
199 	"vcxl",
200 	vcxgbe_methods,
201 	sizeof(struct vi_info)
202 };
203 
204 /* T6 bus driver interface */
205 static int t6_probe(device_t);
206 static device_method_t t6_methods[] = {
207 	DEVMETHOD(device_probe,		t6_probe),
208 	DEVMETHOD(device_attach,	t4_attach),
209 	DEVMETHOD(device_detach,	t4_detach),
210 	DEVMETHOD(device_suspend,	t4_suspend),
211 	DEVMETHOD(device_resume,	t4_resume),
212 
213 	DEVMETHOD(bus_child_location,	t4_child_location),
214 	DEVMETHOD(bus_reset_prepare,	t4_reset_prepare),
215 	DEVMETHOD(bus_reset_post,	t4_reset_post),
216 
217 	DEVMETHOD(t4_is_main_ready,	t4_ready),
218 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
219 
220 	DEVMETHOD_END
221 };
222 static driver_t t6_driver = {
223 	"t6nex",
224 	t6_methods,
225 	sizeof(struct adapter)
226 };
227 
228 
229 /* T6 port (cc) interface */
230 static driver_t cc_driver = {
231 	"cc",
232 	cxgbe_methods,
233 	sizeof(struct port_info)
234 };
235 
236 /* T6 VI (vcc) interface */
237 static driver_t vcc_driver = {
238 	"vcc",
239 	vcxgbe_methods,
240 	sizeof(struct vi_info)
241 };
242 
243 /* T7+ bus driver interface */
244 static int ch_probe(device_t);
245 static device_method_t ch_methods[] = {
246 	DEVMETHOD(device_probe,		ch_probe),
247 	DEVMETHOD(device_attach,	t4_attach),
248 	DEVMETHOD(device_detach,	t4_detach),
249 	DEVMETHOD(device_suspend,	t4_suspend),
250 	DEVMETHOD(device_resume,	t4_resume),
251 
252 	DEVMETHOD(bus_child_location,	t4_child_location),
253 	DEVMETHOD(bus_reset_prepare, 	t4_reset_prepare),
254 	DEVMETHOD(bus_reset_post, 	t4_reset_post),
255 
256 	DEVMETHOD(t4_is_main_ready,	t4_ready),
257 	DEVMETHOD(t4_read_port_device,	t4_read_port_device),
258 
259 	DEVMETHOD_END
260 };
261 static driver_t ch_driver = {
262 	"chnex",
263 	ch_methods,
264 	sizeof(struct adapter)
265 };
266 
267 
268 /* T7+ port (che) interface */
269 static driver_t che_driver = {
270 	"che",
271 	cxgbe_methods,
272 	sizeof(struct port_info)
273 };
274 
275 /* T7+ VI (vche) interface */
276 static driver_t vche_driver = {
277 	"vche",
278 	vcxgbe_methods,
279 	sizeof(struct vi_info)
280 };
281 
282 /* ifnet interface */
283 static void cxgbe_init(void *);
284 static int cxgbe_ioctl(if_t, unsigned long, caddr_t);
285 static int cxgbe_transmit(if_t, struct mbuf *);
286 static void cxgbe_qflush(if_t);
287 #if defined(KERN_TLS) || defined(RATELIMIT)
288 static int cxgbe_snd_tag_alloc(if_t, union if_snd_tag_alloc_params *,
289     struct m_snd_tag **);
290 #endif
291 
292 MALLOC_DEFINE(M_CXGBE, "cxgbe", "Chelsio T4/T5 Ethernet driver and services");
293 
294 /*
295  * Correct lock order when you need to acquire multiple locks is t4_list_lock,
296  * then ADAPTER_LOCK, then t4_uld_list_lock.
297  */
298 static struct sx t4_list_lock;
299 SLIST_HEAD(, adapter) t4_list;
300 #ifdef TCP_OFFLOAD
301 static struct sx t4_uld_list_lock;
302 struct uld_info *t4_uld_list[ULD_MAX + 1];
303 #endif
304 
305 /*
306  * Tunables.  See tweak_tunables() too.
307  *
308  * Each tunable is set to a default value here if it's known at compile-time.
309  * Otherwise it is set to -n as an indication to tweak_tunables() that it should
310  * provide a reasonable default (upto n) when the driver is loaded.
311  *
312  * Tunables applicable to both T4 and T5 are under hw.cxgbe.  Those specific to
313  * T5 are under hw.cxl.
314  */
315 SYSCTL_NODE(_hw, OID_AUTO, cxgbe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
316     "cxgbe(4) parameters");
317 SYSCTL_NODE(_hw, OID_AUTO, cxl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
318     "cxgbe(4) T5+ parameters");
319 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, toe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
320     "cxgbe(4) TOE parameters");
321 
322 /*
323  * Number of queues for tx and rx, NIC and offload.
324  */
325 #define NTXQ 16
326 int t4_ntxq = -NTXQ;
327 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq, CTLFLAG_RDTUN, &t4_ntxq, 0,
328     "Number of TX queues per port");
329 TUNABLE_INT("hw.cxgbe.ntxq10g", &t4_ntxq);	/* Old name, undocumented */
330 
331 #define NRXQ 8
332 int t4_nrxq = -NRXQ;
333 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq, CTLFLAG_RDTUN, &t4_nrxq, 0,
334     "Number of RX queues per port");
335 TUNABLE_INT("hw.cxgbe.nrxq10g", &t4_nrxq);	/* Old name, undocumented */
336 
337 #define NTXQ_VI 1
338 static int t4_ntxq_vi = -NTXQ_VI;
339 SYSCTL_INT(_hw_cxgbe, OID_AUTO, ntxq_vi, CTLFLAG_RDTUN, &t4_ntxq_vi, 0,
340     "Number of TX queues per VI");
341 
342 #define NRXQ_VI 1
343 static int t4_nrxq_vi = -NRXQ_VI;
344 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nrxq_vi, CTLFLAG_RDTUN, &t4_nrxq_vi, 0,
345     "Number of RX queues per VI");
346 
347 static int t4_rsrv_noflowq = 0;
348 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rsrv_noflowq, CTLFLAG_RDTUN, &t4_rsrv_noflowq,
349     0, "Reserve TX queue 0 of each VI for non-flowid packets");
350 
351 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
352 #define NOFLDTXQ 8
353 static int t4_nofldtxq = -NOFLDTXQ;
354 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq, CTLFLAG_RDTUN, &t4_nofldtxq, 0,
355     "Number of offload TX queues per port");
356 
357 #define NOFLDTXQ_VI 1
358 static int t4_nofldtxq_vi = -NOFLDTXQ_VI;
359 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldtxq_vi, CTLFLAG_RDTUN, &t4_nofldtxq_vi, 0,
360     "Number of offload TX queues per VI");
361 #endif
362 
363 #if defined(TCP_OFFLOAD)
364 #define NOFLDRXQ 2
365 static int t4_nofldrxq = -NOFLDRXQ;
366 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq, CTLFLAG_RDTUN, &t4_nofldrxq, 0,
367     "Number of offload RX queues per port");
368 
369 #define NOFLDRXQ_VI 1
370 static int t4_nofldrxq_vi = -NOFLDRXQ_VI;
371 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nofldrxq_vi, CTLFLAG_RDTUN, &t4_nofldrxq_vi, 0,
372     "Number of offload RX queues per VI");
373 
374 #define TMR_IDX_OFLD 1
375 static int t4_tmr_idx_ofld = TMR_IDX_OFLD;
376 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx_ofld, CTLFLAG_RDTUN,
377     &t4_tmr_idx_ofld, 0, "Holdoff timer index for offload queues");
378 
379 #define PKTC_IDX_OFLD (-1)
380 static int t4_pktc_idx_ofld = PKTC_IDX_OFLD;
381 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx_ofld, CTLFLAG_RDTUN,
382     &t4_pktc_idx_ofld, 0, "holdoff packet counter index for offload queues");
383 
384 /* 0 means chip/fw default, non-zero number is value in microseconds */
385 static u_long t4_toe_keepalive_idle = 0;
386 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_idle, CTLFLAG_RDTUN,
387     &t4_toe_keepalive_idle, 0, "TOE keepalive idle timer (us)");
388 
389 /* 0 means chip/fw default, non-zero number is value in microseconds */
390 static u_long t4_toe_keepalive_interval = 0;
391 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, keepalive_interval, CTLFLAG_RDTUN,
392     &t4_toe_keepalive_interval, 0, "TOE keepalive interval timer (us)");
393 
394 /* 0 means chip/fw default, non-zero number is # of keepalives before abort */
395 static int t4_toe_keepalive_count = 0;
396 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, keepalive_count, CTLFLAG_RDTUN,
397     &t4_toe_keepalive_count, 0, "Number of TOE keepalive probes before abort");
398 
399 /* 0 means chip/fw default, non-zero number is value in microseconds */
400 static u_long t4_toe_rexmt_min = 0;
401 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_min, CTLFLAG_RDTUN,
402     &t4_toe_rexmt_min, 0, "Minimum TOE retransmit interval (us)");
403 
404 /* 0 means chip/fw default, non-zero number is value in microseconds */
405 static u_long t4_toe_rexmt_max = 0;
406 SYSCTL_ULONG(_hw_cxgbe_toe, OID_AUTO, rexmt_max, CTLFLAG_RDTUN,
407     &t4_toe_rexmt_max, 0, "Maximum TOE retransmit interval (us)");
408 
409 /* 0 means chip/fw default, non-zero number is # of rexmt before abort */
410 static int t4_toe_rexmt_count = 0;
411 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, rexmt_count, CTLFLAG_RDTUN,
412     &t4_toe_rexmt_count, 0, "Number of TOE retransmissions before abort");
413 
414 /* -1 means chip/fw default, other values are raw backoff values to use */
415 static int t4_toe_rexmt_backoff[16] = {
416 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
417 };
418 SYSCTL_NODE(_hw_cxgbe_toe, OID_AUTO, rexmt_backoff,
419     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
420     "cxgbe(4) TOE retransmit backoff values");
421 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 0, CTLFLAG_RDTUN,
422     &t4_toe_rexmt_backoff[0], 0, "");
423 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 1, CTLFLAG_RDTUN,
424     &t4_toe_rexmt_backoff[1], 0, "");
425 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 2, CTLFLAG_RDTUN,
426     &t4_toe_rexmt_backoff[2], 0, "");
427 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 3, CTLFLAG_RDTUN,
428     &t4_toe_rexmt_backoff[3], 0, "");
429 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 4, CTLFLAG_RDTUN,
430     &t4_toe_rexmt_backoff[4], 0, "");
431 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 5, CTLFLAG_RDTUN,
432     &t4_toe_rexmt_backoff[5], 0, "");
433 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 6, CTLFLAG_RDTUN,
434     &t4_toe_rexmt_backoff[6], 0, "");
435 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 7, CTLFLAG_RDTUN,
436     &t4_toe_rexmt_backoff[7], 0, "");
437 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 8, CTLFLAG_RDTUN,
438     &t4_toe_rexmt_backoff[8], 0, "");
439 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 9, CTLFLAG_RDTUN,
440     &t4_toe_rexmt_backoff[9], 0, "");
441 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 10, CTLFLAG_RDTUN,
442     &t4_toe_rexmt_backoff[10], 0, "");
443 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 11, CTLFLAG_RDTUN,
444     &t4_toe_rexmt_backoff[11], 0, "");
445 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 12, CTLFLAG_RDTUN,
446     &t4_toe_rexmt_backoff[12], 0, "");
447 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 13, CTLFLAG_RDTUN,
448     &t4_toe_rexmt_backoff[13], 0, "");
449 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 14, CTLFLAG_RDTUN,
450     &t4_toe_rexmt_backoff[14], 0, "");
451 SYSCTL_INT(_hw_cxgbe_toe_rexmt_backoff, OID_AUTO, 15, CTLFLAG_RDTUN,
452     &t4_toe_rexmt_backoff[15], 0, "");
453 
454 int t4_ddp_rcvbuf_len = 256 * 1024;
455 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_len, CTLFLAG_RWTUN,
456     &t4_ddp_rcvbuf_len, 0, "length of each DDP RX buffer");
457 
458 unsigned int t4_ddp_rcvbuf_cache = 4;
459 SYSCTL_UINT(_hw_cxgbe_toe, OID_AUTO, ddp_rcvbuf_cache, CTLFLAG_RWTUN,
460     &t4_ddp_rcvbuf_cache, 0,
461     "maximum number of free DDP RX buffers to cache per connection");
462 #endif
463 
464 #ifdef DEV_NETMAP
465 #define NN_MAIN_VI	(1 << 0)	/* Native netmap on the main VI */
466 #define NN_EXTRA_VI	(1 << 1)	/* Native netmap on the extra VI(s) */
467 static int t4_native_netmap = NN_EXTRA_VI;
468 SYSCTL_INT(_hw_cxgbe, OID_AUTO, native_netmap, CTLFLAG_RDTUN, &t4_native_netmap,
469     0, "Native netmap support.  bit 0 = main VI, bit 1 = extra VIs");
470 
471 #define NNMTXQ 8
472 static int t4_nnmtxq = -NNMTXQ;
473 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq, CTLFLAG_RDTUN, &t4_nnmtxq, 0,
474     "Number of netmap TX queues");
475 
476 #define NNMRXQ 8
477 static int t4_nnmrxq = -NNMRXQ;
478 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq, CTLFLAG_RDTUN, &t4_nnmrxq, 0,
479     "Number of netmap RX queues");
480 
481 #define NNMTXQ_VI 2
482 static int t4_nnmtxq_vi = -NNMTXQ_VI;
483 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmtxq_vi, CTLFLAG_RDTUN, &t4_nnmtxq_vi, 0,
484     "Number of netmap TX queues per VI");
485 
486 #define NNMRXQ_VI 2
487 static int t4_nnmrxq_vi = -NNMRXQ_VI;
488 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nnmrxq_vi, CTLFLAG_RDTUN, &t4_nnmrxq_vi, 0,
489     "Number of netmap RX queues per VI");
490 #endif
491 
492 /*
493  * Holdoff parameters for ports.
494  */
495 #define TMR_IDX 1
496 int t4_tmr_idx = TMR_IDX;
497 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_timer_idx, CTLFLAG_RDTUN, &t4_tmr_idx,
498     0, "Holdoff timer index");
499 TUNABLE_INT("hw.cxgbe.holdoff_timer_idx_10G", &t4_tmr_idx);	/* Old name */
500 
501 #define PKTC_IDX (-1)
502 int t4_pktc_idx = PKTC_IDX;
503 SYSCTL_INT(_hw_cxgbe, OID_AUTO, holdoff_pktc_idx, CTLFLAG_RDTUN, &t4_pktc_idx,
504     0, "Holdoff packet counter index");
505 TUNABLE_INT("hw.cxgbe.holdoff_pktc_idx_10G", &t4_pktc_idx);	/* Old name */
506 
507 /*
508  * Size (# of entries) of each tx and rx queue.
509  */
510 unsigned int t4_qsize_txq = TX_EQ_QSIZE;
511 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_txq, CTLFLAG_RDTUN, &t4_qsize_txq, 0,
512     "Number of descriptors in each TX queue");
513 
514 unsigned int t4_qsize_rxq = RX_IQ_QSIZE;
515 SYSCTL_INT(_hw_cxgbe, OID_AUTO, qsize_rxq, CTLFLAG_RDTUN, &t4_qsize_rxq, 0,
516     "Number of descriptors in each RX queue");
517 
518 /*
519  * Interrupt types allowed (bits 0, 1, 2 = INTx, MSI, MSI-X respectively).
520  */
521 int t4_intr_types = INTR_MSIX | INTR_MSI | INTR_INTX;
522 SYSCTL_INT(_hw_cxgbe, OID_AUTO, interrupt_types, CTLFLAG_RDTUN, &t4_intr_types,
523     0, "Interrupt types allowed (bit 0 = INTx, 1 = MSI, 2 = MSI-X)");
524 
525 /*
526  * Configuration file.  All the _CF names here are special.
527  */
528 #define DEFAULT_CF	"default"
529 #define BUILTIN_CF	"built-in"
530 #define FLASH_CF	"flash"
531 #define UWIRE_CF	"uwire"
532 #define FPGA_CF		"fpga"
533 static char t4_cfg_file[32] = DEFAULT_CF;
534 SYSCTL_STRING(_hw_cxgbe, OID_AUTO, config_file, CTLFLAG_RDTUN, t4_cfg_file,
535     sizeof(t4_cfg_file), "Firmware configuration file");
536 
537 /*
538  * PAUSE settings (bit 0, 1, 2 = rx_pause, tx_pause, pause_autoneg respectively).
539  * rx_pause = 1 to heed incoming PAUSE frames, 0 to ignore them.
540  * tx_pause = 1 to emit PAUSE frames when the rx FIFO reaches its high water
541  *            mark or when signalled to do so, 0 to never emit PAUSE.
542  * pause_autoneg = 1 means PAUSE will be negotiated if possible and the
543  *                 negotiated settings will override rx_pause/tx_pause.
544  *                 Otherwise rx_pause/tx_pause are applied forcibly.
545  */
546 static int t4_pause_settings = PAUSE_RX | PAUSE_TX | PAUSE_AUTONEG;
547 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pause_settings, CTLFLAG_RDTUN,
548     &t4_pause_settings, 0,
549     "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
550 
551 /*
552  * Forward Error Correction settings (bit 0, 1 = RS, BASER respectively).
553  * -1 to run with the firmware default.  Same as FEC_AUTO (bit 5)
554  *  0 to disable FEC.
555  */
556 static int t4_fec = -1;
557 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fec, CTLFLAG_RDTUN, &t4_fec, 0,
558     "Forward Error Correction (bit 0 = RS, bit 1 = BASER_RS)");
559 
560 static const char *
561 t4_fec_bits = "\20\1RS-FEC\2FC-FEC\3NO-FEC\4RSVD1\5RSVD2\6auto\7module";
562 
563 /*
564  * Controls when the driver sets the FORCE_FEC bit in the L1_CFG32 that it
565  * issues to the firmware.  If the firmware doesn't support FORCE_FEC then the
566  * driver runs as if this is set to 0.
567  * -1 to set FORCE_FEC iff requested_fec != AUTO. Multiple FEC bits are okay.
568  *  0 to never set FORCE_FEC. requested_fec = AUTO means use the hint from the
569  *    transceiver. Multiple FEC bits may not be okay but will be passed on to
570  *    the firmware anyway (may result in l1cfg errors with old firmwares).
571  *  1 to always set FORCE_FEC. Multiple FEC bits are okay. requested_fec = AUTO
572  *    means set all FEC bits that are valid for the speed.
573  */
574 static int t4_force_fec = -1;
575 SYSCTL_INT(_hw_cxgbe, OID_AUTO, force_fec, CTLFLAG_RDTUN, &t4_force_fec, 0,
576     "Controls the use of FORCE_FEC bit in L1 configuration.");
577 
578 /*
579  * Link autonegotiation.
580  * -1 to run with the firmware default.
581  *  0 to disable.
582  *  1 to enable.
583  */
584 static int t4_autoneg = -1;
585 SYSCTL_INT(_hw_cxgbe, OID_AUTO, autoneg, CTLFLAG_RDTUN, &t4_autoneg, 0,
586     "Link autonegotiation");
587 
588 /*
589  * Firmware auto-install by driver during attach (0, 1, 2 = prohibited, allowed,
590  * encouraged respectively).  '-n' is the same as 'n' except the firmware
591  * version used in the checks is read from the firmware bundled with the driver.
592  */
593 static int t4_fw_install = 1;
594 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fw_install, CTLFLAG_RDTUN, &t4_fw_install, 0,
595     "Firmware auto-install (0 = prohibited, 1 = allowed, 2 = encouraged)");
596 
597 /*
598  * ASIC features that will be used.  Disable the ones you don't want so that the
599  * chip resources aren't wasted on features that will not be used.
600  */
601 static int t4_nbmcaps_allowed = 0;
602 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nbmcaps_allowed, CTLFLAG_RDTUN,
603     &t4_nbmcaps_allowed, 0, "Default NBM capabilities");
604 
605 static int t4_linkcaps_allowed = 0;	/* No DCBX, PPP, etc. by default */
606 SYSCTL_INT(_hw_cxgbe, OID_AUTO, linkcaps_allowed, CTLFLAG_RDTUN,
607     &t4_linkcaps_allowed, 0, "Default link capabilities");
608 
609 static int t4_switchcaps_allowed = FW_CAPS_CONFIG_SWITCH_INGRESS |
610     FW_CAPS_CONFIG_SWITCH_EGRESS;
611 SYSCTL_INT(_hw_cxgbe, OID_AUTO, switchcaps_allowed, CTLFLAG_RDTUN,
612     &t4_switchcaps_allowed, 0, "Default switch capabilities");
613 
614 static int t4_nvmecaps_allowed = -1;
615 SYSCTL_INT(_hw_cxgbe, OID_AUTO, nvmecaps_allowed, CTLFLAG_RDTUN,
616     &t4_nvmecaps_allowed, 0, "Default NVMe capabilities");
617 
618 #ifdef RATELIMIT
619 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
620 	FW_CAPS_CONFIG_NIC_HASHFILTER | FW_CAPS_CONFIG_NIC_ETHOFLD;
621 #else
622 static int t4_niccaps_allowed = FW_CAPS_CONFIG_NIC |
623 	FW_CAPS_CONFIG_NIC_HASHFILTER;
624 #endif
625 SYSCTL_INT(_hw_cxgbe, OID_AUTO, niccaps_allowed, CTLFLAG_RDTUN,
626     &t4_niccaps_allowed, 0, "Default NIC capabilities");
627 
628 static int t4_toecaps_allowed = -1;
629 SYSCTL_INT(_hw_cxgbe, OID_AUTO, toecaps_allowed, CTLFLAG_RDTUN,
630     &t4_toecaps_allowed, 0, "Default TCP offload capabilities");
631 
632 static int t4_rdmacaps_allowed = -1;
633 SYSCTL_INT(_hw_cxgbe, OID_AUTO, rdmacaps_allowed, CTLFLAG_RDTUN,
634     &t4_rdmacaps_allowed, 0, "Default RDMA capabilities");
635 
636 static int t4_cryptocaps_allowed = -1;
637 SYSCTL_INT(_hw_cxgbe, OID_AUTO, cryptocaps_allowed, CTLFLAG_RDTUN,
638     &t4_cryptocaps_allowed, 0, "Default crypto capabilities");
639 
640 static int t4_iscsicaps_allowed = -1;
641 SYSCTL_INT(_hw_cxgbe, OID_AUTO, iscsicaps_allowed, CTLFLAG_RDTUN,
642     &t4_iscsicaps_allowed, 0, "Default iSCSI capabilities");
643 
644 static int t4_fcoecaps_allowed = 0;
645 SYSCTL_INT(_hw_cxgbe, OID_AUTO, fcoecaps_allowed, CTLFLAG_RDTUN,
646     &t4_fcoecaps_allowed, 0, "Default FCoE capabilities");
647 
648 static int t5_write_combine = 0;
649 SYSCTL_INT(_hw_cxl, OID_AUTO, write_combine, CTLFLAG_RDTUN, &t5_write_combine,
650     0, "Use WC instead of UC for BAR2");
651 
652 /* From t4_sysctls: doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"} */
653 static int t4_doorbells_allowed = 0xf;
654 SYSCTL_INT(_hw_cxgbe, OID_AUTO, doorbells_allowed, CTLFLAG_RDTUN,
655 	   &t4_doorbells_allowed, 0, "Limit tx queues to these doorbells");
656 
657 static int t4_num_vis = 1;
658 SYSCTL_INT(_hw_cxgbe, OID_AUTO, num_vis, CTLFLAG_RDTUN, &t4_num_vis, 0,
659     "Number of VIs per port");
660 
661 /*
662  * PCIe Relaxed Ordering.
663  * -1: driver should figure out a good value.
664  * 0: disable RO.
665  * 1: enable RO.
666  * 2: leave RO alone.
667  */
668 static int pcie_relaxed_ordering = -1;
669 SYSCTL_INT(_hw_cxgbe, OID_AUTO, pcie_relaxed_ordering, CTLFLAG_RDTUN,
670     &pcie_relaxed_ordering, 0,
671     "PCIe Relaxed Ordering: 0 = disable, 1 = enable, 2 = leave alone");
672 
673 static int t4_panic_on_fatal_err = 0;
674 SYSCTL_INT(_hw_cxgbe, OID_AUTO, panic_on_fatal_err, CTLFLAG_RWTUN,
675     &t4_panic_on_fatal_err, 0, "panic on fatal errors");
676 
677 static int t4_reset_on_fatal_err = 0;
678 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_on_fatal_err, CTLFLAG_RWTUN,
679     &t4_reset_on_fatal_err, 0, "reset adapter on fatal errors");
680 
681 static int t4_reset_method = 1;
682 SYSCTL_INT(_hw_cxgbe, OID_AUTO, reset_method, CTLFLAG_RWTUN, &t4_reset_method,
683     0, "reset method: 0 = PL_RST, 1 = PCIe secondary bus reset, 2 = PCIe link bounce");
684 
685 static int t4_clock_gate_on_suspend = 0;
686 SYSCTL_INT(_hw_cxgbe, OID_AUTO, clock_gate_on_suspend, CTLFLAG_RWTUN,
687     &t4_clock_gate_on_suspend, 0, "gate the clock on suspend");
688 
689 static int t4_tx_vm_wr = 0;
690 SYSCTL_INT(_hw_cxgbe, OID_AUTO, tx_vm_wr, CTLFLAG_RWTUN, &t4_tx_vm_wr, 0,
691     "Use VM work requests to transmit packets.");
692 
693 /*
694  * Set to non-zero to enable the attack filter.  A packet that matches any of
695  * these conditions will get dropped on ingress:
696  * 1) IP && source address == destination address.
697  * 2) TCP/IP && source address is not a unicast address.
698  * 3) TCP/IP && destination address is not a unicast address.
699  * 4) IP && source address is loopback (127.x.y.z).
700  * 5) IP && destination address is loopback (127.x.y.z).
701  * 6) IPv6 && source address == destination address.
702  * 7) IPv6 && source address is not a unicast address.
703  * 8) IPv6 && source address is loopback (::1/128).
704  * 9) IPv6 && destination address is loopback (::1/128).
705  * 10) IPv6 && source address is unspecified (::/128).
706  * 11) IPv6 && destination address is unspecified (::/128).
707  * 12) TCP/IPv6 && source address is multicast (ff00::/8).
708  * 13) TCP/IPv6 && destination address is multicast (ff00::/8).
709  */
710 static int t4_attack_filter = 0;
711 SYSCTL_INT(_hw_cxgbe, OID_AUTO, attack_filter, CTLFLAG_RDTUN,
712     &t4_attack_filter, 0, "Drop suspicious traffic");
713 
714 static int t4_drop_ip_fragments = 0;
715 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_ip_fragments, CTLFLAG_RDTUN,
716     &t4_drop_ip_fragments, 0, "Drop IP fragments");
717 
718 static int t4_drop_pkts_with_l2_errors = 1;
719 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l2_errors, CTLFLAG_RDTUN,
720     &t4_drop_pkts_with_l2_errors, 0,
721     "Drop all frames with Layer 2 length or checksum errors");
722 
723 static int t4_drop_pkts_with_l3_errors = 0;
724 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l3_errors, CTLFLAG_RDTUN,
725     &t4_drop_pkts_with_l3_errors, 0,
726     "Drop all frames with IP version, length, or checksum errors");
727 
728 static int t4_drop_pkts_with_l4_errors = 0;
729 SYSCTL_INT(_hw_cxgbe, OID_AUTO, drop_pkts_with_l4_errors, CTLFLAG_RDTUN,
730     &t4_drop_pkts_with_l4_errors, 0,
731     "Drop all frames with Layer 4 length, checksum, or other errors");
732 
733 #ifdef TCP_OFFLOAD
734 /*
735  * TOE tunables.
736  */
737 static int t4_cop_managed_offloading = 0;
738 SYSCTL_INT(_hw_cxgbe_toe, OID_AUTO, cop_managed_offloading, CTLFLAG_RDTUN,
739     &t4_cop_managed_offloading, 0,
740     "COP (Connection Offload Policy) controls all TOE offload");
741 TUNABLE_INT("hw.cxgbe.cop_managed_offloading", &t4_cop_managed_offloading);
742 #endif
743 
744 #ifdef KERN_TLS
745 /*
746  * This enables KERN_TLS for all adapters if set.
747  */
748 static int t4_kern_tls = 0;
749 SYSCTL_INT(_hw_cxgbe, OID_AUTO, kern_tls, CTLFLAG_RDTUN, &t4_kern_tls, 0,
750     "Enable KERN_TLS mode for T6 adapters");
751 
752 SYSCTL_NODE(_hw_cxgbe, OID_AUTO, tls, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
753     "cxgbe(4) KERN_TLS parameters");
754 
755 static int t4_tls_inline_keys = 0;
756 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, inline_keys, CTLFLAG_RDTUN,
757     &t4_tls_inline_keys, 0,
758     "Always pass TLS keys in work requests (1) or attempt to store TLS keys "
759     "in card memory.");
760 
761 static int t4_tls_combo_wrs = 0;
762 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, combo_wrs, CTLFLAG_RDTUN, &t4_tls_combo_wrs,
763     0, "Attempt to combine TCB field updates with TLS record work requests.");
764 
765 static int t4_tls_short_records = 1;
766 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, short_records, CTLFLAG_RDTUN,
767     &t4_tls_short_records, 0, "Use cipher-only mode for short records.");
768 
769 static int t4_tls_partial_ghash = 1;
770 SYSCTL_INT(_hw_cxgbe_tls, OID_AUTO, partial_ghash, CTLFLAG_RDTUN,
771     &t4_tls_partial_ghash, 0, "Use partial GHASH for AES-GCM records.");
772 #endif
773 
774 /* Functions used by VIs to obtain unique MAC addresses for each VI. */
775 static int vi_mac_funcs[] = {
776 	FW_VI_FUNC_ETH,
777 	FW_VI_FUNC_OFLD,
778 	FW_VI_FUNC_IWARP,
779 	FW_VI_FUNC_OPENISCSI,
780 	FW_VI_FUNC_OPENFCOE,
781 	FW_VI_FUNC_FOISCSI,
782 	FW_VI_FUNC_FOFCOE,
783 };
784 
785 struct intrs_and_queues {
786 	uint16_t intr_type;	/* INTx, MSI, or MSI-X */
787 	uint16_t num_vis;	/* number of VIs for each port */
788 	uint16_t nirq;		/* Total # of vectors */
789 	uint16_t ntxq;		/* # of NIC txq's for each port */
790 	uint16_t nrxq;		/* # of NIC rxq's for each port */
791 	uint16_t nofldtxq;	/* # of TOE/ETHOFLD txq's for each port */
792 	uint16_t nofldrxq;	/* # of TOE rxq's for each port */
793 	uint16_t nnmtxq;	/* # of netmap txq's */
794 	uint16_t nnmrxq;	/* # of netmap rxq's */
795 
796 	/* The vcxgbe/vcxl interfaces use these and not the ones above. */
797 	uint16_t ntxq_vi;	/* # of NIC txq's */
798 	uint16_t nrxq_vi;	/* # of NIC rxq's */
799 	uint16_t nofldtxq_vi;	/* # of TOE txq's */
800 	uint16_t nofldrxq_vi;	/* # of TOE rxq's */
801 	uint16_t nnmtxq_vi;	/* # of netmap txq's */
802 	uint16_t nnmrxq_vi;	/* # of netmap rxq's */
803 };
804 
805 static void setup_memwin(struct adapter *);
806 static void position_memwin(struct adapter *, int, uint32_t);
807 static int validate_mem_range(struct adapter *, uint32_t, uint32_t);
808 static int fwmtype_to_hwmtype(int);
809 static int validate_mt_off_len(struct adapter *, int, uint32_t, uint32_t,
810     uint32_t *);
811 static int fixup_devlog_params(struct adapter *);
812 static int cfg_itype_and_nqueues(struct adapter *, struct intrs_and_queues *);
813 static int contact_firmware(struct adapter *);
814 static int partition_resources(struct adapter *);
815 static int get_params__pre_init(struct adapter *);
816 static int set_params__pre_init(struct adapter *);
817 static int get_params__post_init(struct adapter *);
818 static int set_params__post_init(struct adapter *);
819 static void t4_set_desc(struct adapter *);
820 static bool fixed_ifmedia(struct port_info *);
821 static void build_medialist(struct port_info *);
822 static void init_link_config(struct port_info *);
823 static int fixup_link_config(struct port_info *);
824 static int apply_link_config(struct port_info *);
825 static int cxgbe_init_synchronized(struct vi_info *);
826 static int cxgbe_uninit_synchronized(struct vi_info *);
827 static int adapter_full_init(struct adapter *);
828 static void adapter_full_uninit(struct adapter *);
829 static int vi_full_init(struct vi_info *);
830 static void vi_full_uninit(struct vi_info *);
831 static int alloc_extra_vi(struct adapter *, struct port_info *, struct vi_info *);
832 static void quiesce_txq(struct sge_txq *);
833 static void quiesce_wrq(struct sge_wrq *);
834 static void quiesce_iq_fl(struct adapter *, struct sge_iq *, struct sge_fl *);
835 static void quiesce_vi(struct vi_info *);
836 static int t4_alloc_irq(struct adapter *, struct irq *, int rid,
837     driver_intr_t *, void *, char *);
838 static int t4_free_irq(struct adapter *, struct irq *);
839 static void t4_init_atid_table(struct adapter *);
840 static void t4_free_atid_table(struct adapter *);
841 static void stop_atid_allocator(struct adapter *);
842 static void restart_atid_allocator(struct adapter *);
843 static void get_regs(struct adapter *, struct t4_regdump *, uint8_t *);
844 static void vi_refresh_stats(struct vi_info *);
845 static void cxgbe_refresh_stats(struct vi_info *);
846 static void cxgbe_tick(void *);
847 static void vi_tick(void *);
848 static void cxgbe_sysctls(struct port_info *);
849 static int sysctl_int_array(SYSCTL_HANDLER_ARGS);
850 static int sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS);
851 static int sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS);
852 static int sysctl_btphy(SYSCTL_HANDLER_ARGS);
853 static int sysctl_noflowq(SYSCTL_HANDLER_ARGS);
854 static int sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS);
855 static int sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS);
856 static int sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS);
857 static int sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS);
858 static int sysctl_qsize_txq(SYSCTL_HANDLER_ARGS);
859 static int sysctl_pause_settings(SYSCTL_HANDLER_ARGS);
860 static int sysctl_link_fec(SYSCTL_HANDLER_ARGS);
861 static int sysctl_requested_fec(SYSCTL_HANDLER_ARGS);
862 static int sysctl_module_fec(SYSCTL_HANDLER_ARGS);
863 static int sysctl_autoneg(SYSCTL_HANDLER_ARGS);
864 static int sysctl_force_fec(SYSCTL_HANDLER_ARGS);
865 static int sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS);
866 static int sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS);
867 static int sysctl_temperature(SYSCTL_HANDLER_ARGS);
868 static int sysctl_vdd(SYSCTL_HANDLER_ARGS);
869 static int sysctl_reset_sensor(SYSCTL_HANDLER_ARGS);
870 static int sysctl_loadavg(SYSCTL_HANDLER_ARGS);
871 static int sysctl_cctrl(SYSCTL_HANDLER_ARGS);
872 static int sysctl_cim_ibq(SYSCTL_HANDLER_ARGS);
873 static int sysctl_cim_obq(SYSCTL_HANDLER_ARGS);
874 static int sysctl_cim_la(SYSCTL_HANDLER_ARGS);
875 static int sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS);
876 static int sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS);
877 static int sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS);
878 static int sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS);
879 static int sysctl_cpl_stats(SYSCTL_HANDLER_ARGS);
880 static int sysctl_ddp_stats(SYSCTL_HANDLER_ARGS);
881 static int sysctl_tid_stats(SYSCTL_HANDLER_ARGS);
882 static int sysctl_devlog(SYSCTL_HANDLER_ARGS);
883 static int sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS);
884 static int sysctl_hw_sched(SYSCTL_HANDLER_ARGS);
885 static int sysctl_lb_stats(SYSCTL_HANDLER_ARGS);
886 static int sysctl_linkdnrc(SYSCTL_HANDLER_ARGS);
887 static int sysctl_meminfo(SYSCTL_HANDLER_ARGS);
888 static int sysctl_mps_tcam(SYSCTL_HANDLER_ARGS);
889 static int sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS);
890 static int sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS);
891 static int sysctl_path_mtus(SYSCTL_HANDLER_ARGS);
892 static int sysctl_pm_stats(SYSCTL_HANDLER_ARGS);
893 static int sysctl_rdma_stats(SYSCTL_HANDLER_ARGS);
894 static int sysctl_tcp_stats(SYSCTL_HANDLER_ARGS);
895 static int sysctl_tids(SYSCTL_HANDLER_ARGS);
896 static int sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS);
897 static int sysctl_tnl_stats(SYSCTL_HANDLER_ARGS);
898 static int sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS);
899 static int sysctl_tp_la(SYSCTL_HANDLER_ARGS);
900 static int sysctl_tx_rate(SYSCTL_HANDLER_ARGS);
901 static int sysctl_ulprx_la(SYSCTL_HANDLER_ARGS);
902 static int sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS);
903 static int sysctl_cpus(SYSCTL_HANDLER_ARGS);
904 static int sysctl_reset(SYSCTL_HANDLER_ARGS);
905 #ifdef TCP_OFFLOAD
906 static int sysctl_tls(SYSCTL_HANDLER_ARGS);
907 static int sysctl_tp_tick(SYSCTL_HANDLER_ARGS);
908 static int sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS);
909 static int sysctl_tp_timer(SYSCTL_HANDLER_ARGS);
910 static int sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS);
911 static int sysctl_tp_backoff(SYSCTL_HANDLER_ARGS);
912 static int sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS);
913 static int sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS);
914 #endif
915 static int get_sge_context(struct adapter *, int, uint32_t, int, uint32_t *);
916 static int load_fw(struct adapter *, struct t4_data *);
917 static int load_cfg(struct adapter *, struct t4_data *);
918 static int load_boot(struct adapter *, struct t4_bootrom *);
919 static int load_bootcfg(struct adapter *, struct t4_data *);
920 static int cudbg_dump(struct adapter *, struct t4_cudbg_dump *);
921 static void free_offload_policy(struct t4_offload_policy *);
922 static int set_offload_policy(struct adapter *, struct t4_offload_policy *);
923 static int read_card_mem(struct adapter *, int, struct t4_mem_range *);
924 static int read_i2c(struct adapter *, struct t4_i2c_data *);
925 static int clear_stats(struct adapter *, u_int);
926 static int hold_clip_addr(struct adapter *, struct t4_clip_addr *);
927 static int release_clip_addr(struct adapter *, struct t4_clip_addr *);
928 static inline int stop_adapter(struct adapter *);
929 static inline void set_adapter_hwstatus(struct adapter *, const bool);
930 static int stop_lld(struct adapter *);
931 static inline int restart_adapter(struct adapter *);
932 static int restart_lld(struct adapter *);
933 #ifdef TCP_OFFLOAD
934 static int deactivate_all_uld(struct adapter *);
935 static void stop_all_uld(struct adapter *);
936 static void restart_all_uld(struct adapter *);
937 #endif
938 #ifdef KERN_TLS
939 static int ktls_capability(struct adapter *, bool);
940 #endif
941 static int mod_event(module_t, int, void *);
942 static int notify_siblings(device_t, int);
943 static uint64_t vi_get_counter(if_t, ift_counter);
944 static uint64_t cxgbe_get_counter(if_t, ift_counter);
945 static void enable_vxlan_rx(struct adapter *);
946 static void reset_adapter_task(void *, int);
947 static void fatal_error_task(void *, int);
948 static void dump_devlog(struct adapter *);
949 static void dump_cim_regs(struct adapter *);
950 static void dump_cimla(struct adapter *);
951 
952 struct {
953 	uint16_t device;
954 	char *desc;
955 } t4_pciids[] = {
956 	{0xa000, "Chelsio Terminator 4 FPGA"},
957 	{0x4400, "Chelsio T440-dbg"},
958 	{0x4401, "Chelsio T420-CR"},
959 	{0x4402, "Chelsio T422-CR"},
960 	{0x4403, "Chelsio T440-CR"},
961 	{0x4404, "Chelsio T420-BCH"},
962 	{0x4405, "Chelsio T440-BCH"},
963 	{0x4406, "Chelsio T440-CH"},
964 	{0x4407, "Chelsio T420-SO"},
965 	{0x4408, "Chelsio T420-CX"},
966 	{0x4409, "Chelsio T420-BT"},
967 	{0x440a, "Chelsio T404-BT"},
968 	{0x440e, "Chelsio T440-LP-CR"},
969 }, t5_pciids[] = {
970 	{0xb000, "Chelsio Terminator 5 FPGA"},
971 	{0x5400, "Chelsio T580-dbg"},
972 	{0x5401,  "Chelsio T520-CR"},		/* 2 x 10G */
973 	{0x5402,  "Chelsio T522-CR"},		/* 2 x 10G, 2 X 1G */
974 	{0x5403,  "Chelsio T540-CR"},		/* 4 x 10G */
975 	{0x5407,  "Chelsio T520-SO"},		/* 2 x 10G, nomem */
976 	{0x5409,  "Chelsio T520-BT"},		/* 2 x 10GBaseT */
977 	{0x540a,  "Chelsio T504-BT"},		/* 4 x 1G */
978 	{0x540d,  "Chelsio T580-CR"},		/* 2 x 40G */
979 	{0x540e,  "Chelsio T540-LP-CR"},	/* 4 x 10G */
980 	{0x5410,  "Chelsio T580-LP-CR"},	/* 2 x 40G */
981 	{0x5411,  "Chelsio T520-LL-CR"},	/* 2 x 10G */
982 	{0x5412,  "Chelsio T560-CR"},		/* 1 x 40G, 2 x 10G */
983 	{0x5414,  "Chelsio T580-LP-SO-CR"},	/* 2 x 40G, nomem */
984 	{0x5415,  "Chelsio T502-BT"},		/* 2 x 1G */
985 	{0x5418,  "Chelsio T540-BT"},		/* 4 x 10GBaseT */
986 	{0x5419,  "Chelsio T540-LP-BT"},	/* 4 x 10GBaseT */
987 	{0x541a,  "Chelsio T540-SO-BT"},	/* 4 x 10GBaseT, nomem */
988 	{0x541b,  "Chelsio T540-SO-CR"},	/* 4 x 10G, nomem */
989 
990 	/* Custom */
991 	{0x5483, "Custom T540-CR"},
992 	{0x5484, "Custom T540-BT"},
993 }, t6_pciids[] = {
994 	{0xc006, "Chelsio Terminator 6 FPGA"},	/* T6 PE10K6 FPGA (PF0) */
995 	{0x6400, "Chelsio T6-DBG-25"},		/* 2 x 10/25G, debug */
996 	{0x6401, "Chelsio T6225-CR"},		/* 2 x 10/25G */
997 	{0x6402, "Chelsio T6225-SO-CR"},	/* 2 x 10/25G, nomem */
998 	{0x6403, "Chelsio T6425-CR"},		/* 4 x 10/25G */
999 	{0x6404, "Chelsio T6425-SO-CR"},	/* 4 x 10/25G, nomem */
1000 	{0x6405, "Chelsio T6225-SO-OCP3"},	/* 2 x 10/25G, nomem */
1001 	{0x6406, "Chelsio T6225-OCP3"},		/* 2 x 10/25G */
1002 	{0x6407, "Chelsio T62100-LP-CR"},	/* 2 x 40/50/100G */
1003 	{0x6408, "Chelsio T62100-SO-CR"},	/* 2 x 40/50/100G, nomem */
1004 	{0x6409, "Chelsio T6210-BT"},		/* 2 x 10GBASE-T */
1005 	{0x640d, "Chelsio T62100-CR"},		/* 2 x 40/50/100G */
1006 	{0x6410, "Chelsio T6-DBG-100"},		/* 2 x 40/50/100G, debug */
1007 	{0x6411, "Chelsio T6225-LL-CR"},	/* 2 x 10/25G */
1008 	{0x6414, "Chelsio T62100-SO-OCP3"},	/* 2 x 40/50/100G, nomem */
1009 	{0x6415, "Chelsio T6201-BT"},		/* 2 x 1000BASE-T */
1010 
1011 	/* Custom */
1012 	{0x6480, "Custom T6225-CR"},
1013 	{0x6481, "Custom T62100-CR"},
1014 	{0x6482, "Custom T6225-CR"},
1015 	{0x6483, "Custom T62100-CR"},
1016 	{0x6484, "Custom T64100-CR"},
1017 	{0x6485, "Custom T6240-SO"},
1018 	{0x6486, "Custom T6225-SO-CR"},
1019 	{0x6487, "Custom T6225-CR"},
1020 }, t7_pciids[] = {
1021 	{0xd000, "Chelsio Terminator 7 FPGA"},	/* T7 PE12K FPGA */
1022 	{0x7400, "Chelsio T72200-DBG"},		/* 2 x 200G, debug */
1023 	{0x7401, "Chelsio T7250"},		/* 2 x 10/25/50G, 1 mem */
1024 	{0x7402, "Chelsio S7250"},		/* 2 x 10/25/50G, nomem */
1025 	{0x7403, "Chelsio T7450"},		/* 4 x 10/25/50G, 1 mem */
1026 	{0x7404, "Chelsio S7450"},		/* 4 x 10/25/50G, nomem */
1027 	{0x7405, "Chelsio T72200"},		/* 2 x 40/100/200G, 1 mem */
1028 	{0x7406, "Chelsio S72200"},		/* 2 x 40/100/200G, nomem */
1029 	{0x7407, "Chelsio T72200-FH"},		/* 2 x 40/100/200G, 2 mem */
1030 	{0x7408, "Chelsio S71400"},		/* 1 x 400G, nomem */
1031 	{0x7409, "Chelsio S7210-BT"},		/* 2 x 10GBASE-T, nomem */
1032 	{0x740a, "Chelsio T7450-RC"},		/* 4 x 10/25/50G, 1 mem, RC */
1033 	{0x740b, "Chelsio T72200-RC"},		/* 2 x 40/100/200G, 1 mem, RC */
1034 	{0x740c, "Chelsio T72200-FH-RC"},	/* 2 x 40/100/200G, 2 mem, RC */
1035 	{0x740d, "Chelsio S72200-OCP3"},	/* 2 x 40/100/200G OCP3 */
1036 	{0x740e, "Chelsio S7450-OCP3"},		/* 4 x 1/20/25/50G OCP3 */
1037 	{0x740f, "Chelsio S7410-BT-OCP3"},	/* 4 x 10GBASE-T OCP3 */
1038 	{0x7410, "Chelsio S7210-BT-A"},		/* 2 x 10GBASE-T */
1039 	{0x7411, "Chelsio T7_MAYRA_7"},		/* Motherboard */
1040 
1041 	/* Custom */
1042 	{0x7480, "Custom T7"},
1043 };
1044 
1045 #ifdef TCP_OFFLOAD
1046 /*
1047  * service_iq_fl() has an iq and needs the fl.  Offset of fl from the iq should
1048  * be exactly the same for both rxq and ofld_rxq.
1049  */
1050 CTASSERT(offsetof(struct sge_ofld_rxq, iq) == offsetof(struct sge_rxq, iq));
1051 CTASSERT(offsetof(struct sge_ofld_rxq, fl) == offsetof(struct sge_rxq, fl));
1052 #endif
1053 CTASSERT(sizeof(struct cluster_metadata) <= CL_METADATA_SIZE);
1054 
1055 static int
t4_probe(device_t dev)1056 t4_probe(device_t dev)
1057 {
1058 	int i;
1059 	uint16_t v = pci_get_vendor(dev);
1060 	uint16_t d = pci_get_device(dev);
1061 	uint8_t f = pci_get_function(dev);
1062 
1063 	if (v != PCI_VENDOR_ID_CHELSIO)
1064 		return (ENXIO);
1065 
1066 	/* Attach only to PF0 of the FPGA */
1067 	if (d == 0xa000 && f != 0)
1068 		return (ENXIO);
1069 
1070 	for (i = 0; i < nitems(t4_pciids); i++) {
1071 		if (d == t4_pciids[i].device) {
1072 			device_set_desc(dev, t4_pciids[i].desc);
1073 			return (BUS_PROBE_DEFAULT);
1074 		}
1075 	}
1076 
1077 	return (ENXIO);
1078 }
1079 
1080 static int
t5_probe(device_t dev)1081 t5_probe(device_t dev)
1082 {
1083 	int i;
1084 	uint16_t v = pci_get_vendor(dev);
1085 	uint16_t d = pci_get_device(dev);
1086 	uint8_t f = pci_get_function(dev);
1087 
1088 	if (v != PCI_VENDOR_ID_CHELSIO)
1089 		return (ENXIO);
1090 
1091 	/* Attach only to PF0 of the FPGA */
1092 	if (d == 0xb000 && f != 0)
1093 		return (ENXIO);
1094 
1095 	for (i = 0; i < nitems(t5_pciids); i++) {
1096 		if (d == t5_pciids[i].device) {
1097 			device_set_desc(dev, t5_pciids[i].desc);
1098 			return (BUS_PROBE_DEFAULT);
1099 		}
1100 	}
1101 
1102 	return (ENXIO);
1103 }
1104 
1105 static int
t6_probe(device_t dev)1106 t6_probe(device_t dev)
1107 {
1108 	int i;
1109 	uint16_t v = pci_get_vendor(dev);
1110 	uint16_t d = pci_get_device(dev);
1111 
1112 	if (v != PCI_VENDOR_ID_CHELSIO)
1113 		return (ENXIO);
1114 
1115 	for (i = 0; i < nitems(t6_pciids); i++) {
1116 		if (d == t6_pciids[i].device) {
1117 			device_set_desc(dev, t6_pciids[i].desc);
1118 			return (BUS_PROBE_DEFAULT);
1119 		}
1120 	}
1121 
1122 	return (ENXIO);
1123 }
1124 
1125 static int
ch_probe(device_t dev)1126 ch_probe(device_t dev)
1127 {
1128 	int i;
1129 	uint16_t v = pci_get_vendor(dev);
1130 	uint16_t d = pci_get_device(dev);
1131 	uint8_t f = pci_get_function(dev);
1132 
1133 	if (v != PCI_VENDOR_ID_CHELSIO)
1134 		return (ENXIO);
1135 
1136 	/* Attach only to PF0 of the FPGA */
1137 	if (d == 0xd000 && f != 0)
1138 		return (ENXIO);
1139 
1140 	for (i = 0; i < nitems(t7_pciids); i++) {
1141 		if (d == t7_pciids[i].device) {
1142 			device_set_desc(dev, t7_pciids[i].desc);
1143 			return (BUS_PROBE_DEFAULT);
1144 		}
1145 	}
1146 
1147 	return (ENXIO);
1148 }
1149 
1150 static void
t5_attribute_workaround(device_t dev)1151 t5_attribute_workaround(device_t dev)
1152 {
1153 	device_t root_port;
1154 	uint32_t v;
1155 
1156 	/*
1157 	 * The T5 chips do not properly echo the No Snoop and Relaxed
1158 	 * Ordering attributes when replying to a TLP from a Root
1159 	 * Port.  As a workaround, find the parent Root Port and
1160 	 * disable No Snoop and Relaxed Ordering.  Note that this
1161 	 * affects all devices under this root port.
1162 	 */
1163 	root_port = pci_find_pcie_root_port(dev);
1164 	if (root_port == NULL) {
1165 		device_printf(dev, "Unable to find parent root port\n");
1166 		return;
1167 	}
1168 
1169 	v = pcie_adjust_config(root_port, PCIER_DEVICE_CTL,
1170 	    PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE, 0, 2);
1171 	if ((v & (PCIEM_CTL_RELAXED_ORD_ENABLE | PCIEM_CTL_NOSNOOP_ENABLE)) !=
1172 	    0)
1173 		device_printf(dev, "Disabled No Snoop/Relaxed Ordering on %s\n",
1174 		    device_get_nameunit(root_port));
1175 }
1176 
1177 static const struct devnames devnames[] = {
1178 	{
1179 		.nexus_name = "t4nex",
1180 		.ifnet_name = "cxgbe",
1181 		.vi_ifnet_name = "vcxgbe",
1182 		.pf03_drv_name = "t4iov",
1183 		.vf_nexus_name = "t4vf",
1184 		.vf_ifnet_name = "cxgbev"
1185 	}, {
1186 		.nexus_name = "t5nex",
1187 		.ifnet_name = "cxl",
1188 		.vi_ifnet_name = "vcxl",
1189 		.pf03_drv_name = "t5iov",
1190 		.vf_nexus_name = "t5vf",
1191 		.vf_ifnet_name = "cxlv"
1192 	}, {
1193 		.nexus_name = "t6nex",
1194 		.ifnet_name = "cc",
1195 		.vi_ifnet_name = "vcc",
1196 		.pf03_drv_name = "t6iov",
1197 		.vf_nexus_name = "t6vf",
1198 		.vf_ifnet_name = "ccv"
1199 	}, {
1200 		.nexus_name = "chnex",
1201 		.ifnet_name = "che",
1202 		.vi_ifnet_name = "vche",
1203 		.pf03_drv_name = "chiov",
1204 		.vf_nexus_name = "chvf",
1205 		.vf_ifnet_name = "chev"
1206 	}
1207 };
1208 
1209 void
t4_init_devnames(struct adapter * sc)1210 t4_init_devnames(struct adapter *sc)
1211 {
1212 	int id;
1213 
1214 	id = chip_id(sc);
1215 	if (id < CHELSIO_T4) {
1216 		device_printf(sc->dev, "chip id %d is not supported.\n", id);
1217 		sc->names = NULL;
1218 	} else if (id - CHELSIO_T4 < nitems(devnames))
1219 		sc->names = &devnames[id - CHELSIO_T4];
1220 	else
1221 		sc->names = &devnames[nitems(devnames) - 1];
1222 }
1223 
1224 static int
t4_ifnet_unit(struct adapter * sc,struct port_info * pi)1225 t4_ifnet_unit(struct adapter *sc, struct port_info *pi)
1226 {
1227 	const char *parent, *name;
1228 	long value;
1229 	int line, unit;
1230 
1231 	line = 0;
1232 	parent = device_get_nameunit(sc->dev);
1233 	name = sc->names->ifnet_name;
1234 	while (resource_find_dev(&line, name, &unit, "at", parent) == 0) {
1235 		if (resource_long_value(name, unit, "port", &value) == 0 &&
1236 		    value == pi->port_id)
1237 			return (unit);
1238 	}
1239 	return (-1);
1240 }
1241 
1242 static void
t4_calibration(void * arg)1243 t4_calibration(void *arg)
1244 {
1245 	struct adapter *sc;
1246 	struct clock_sync *cur, *nex;
1247 	uint64_t hw;
1248 	sbintime_t sbt;
1249 	int next_up;
1250 
1251 	sc = (struct adapter *)arg;
1252 
1253 	KASSERT(hw_all_ok(sc), ("!hw_all_ok at t4_calibration"));
1254 	hw = t4_read_reg64(sc, A_SGE_TIMESTAMP_LO);
1255 	sbt = sbinuptime();
1256 
1257 	cur = &sc->cal_info[sc->cal_current];
1258 	next_up = (sc->cal_current + 1) % CNT_CAL_INFO;
1259 	nex = &sc->cal_info[next_up];
1260 	if (__predict_false(sc->cal_count == 0)) {
1261 		/* First time in, just get the values in */
1262 		cur->hw_cur = hw;
1263 		cur->sbt_cur = sbt;
1264 		sc->cal_count++;
1265 		goto done;
1266 	}
1267 
1268 	if (cur->hw_cur == hw) {
1269 		/* The clock is not advancing? */
1270 		sc->cal_count = 0;
1271 		atomic_store_rel_int(&cur->gen, 0);
1272 		goto done;
1273 	}
1274 
1275 	seqc_write_begin(&nex->gen);
1276 	nex->hw_prev = cur->hw_cur;
1277 	nex->sbt_prev = cur->sbt_cur;
1278 	nex->hw_cur = hw;
1279 	nex->sbt_cur = sbt;
1280 	seqc_write_end(&nex->gen);
1281 	sc->cal_current = next_up;
1282 done:
1283 	callout_reset_sbt_curcpu(&sc->cal_callout, SBT_1S, 0, t4_calibration,
1284 	    sc, C_DIRECT_EXEC);
1285 }
1286 
1287 static void
t4_calibration_start(struct adapter * sc)1288 t4_calibration_start(struct adapter *sc)
1289 {
1290 	/*
1291 	 * Here if we have not done a calibration
1292 	 * then do so otherwise start the appropriate
1293 	 * timer.
1294 	 */
1295 	int i;
1296 
1297 	for (i = 0; i < CNT_CAL_INFO; i++) {
1298 		sc->cal_info[i].gen = 0;
1299 	}
1300 	sc->cal_current = 0;
1301 	sc->cal_count = 0;
1302 	sc->cal_gen = 0;
1303 	t4_calibration(sc);
1304 }
1305 
1306 static int
t4_attach(device_t dev)1307 t4_attach(device_t dev)
1308 {
1309 	struct adapter *sc;
1310 	int rc = 0, i, j, rqidx, tqidx, nports;
1311 	struct make_dev_args mda;
1312 	struct intrs_and_queues iaq;
1313 	struct sge *s;
1314 	uint32_t *buf;
1315 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1316 	int ofld_tqidx;
1317 #endif
1318 #ifdef TCP_OFFLOAD
1319 	int ofld_rqidx;
1320 #endif
1321 #ifdef DEV_NETMAP
1322 	int nm_rqidx, nm_tqidx;
1323 #endif
1324 	int num_vis;
1325 
1326 	sc = device_get_softc(dev);
1327 	sc->dev = dev;
1328 	sysctl_ctx_init(&sc->ctx);
1329 	TUNABLE_INT_FETCH("hw.cxgbe.dflags", &sc->debug_flags);
1330 
1331 	if ((pci_get_device(dev) & 0xff00) == 0x5400)
1332 		t5_attribute_workaround(dev);
1333 	pci_enable_busmaster(dev);
1334 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
1335 		uint32_t v;
1336 
1337 		pci_set_max_read_req(dev, 4096);
1338 		v = pci_read_config(dev, i + PCIER_DEVICE_CTL, 2);
1339 		sc->params.pci.mps = 128 << ((v & PCIEM_CTL_MAX_PAYLOAD) >> 5);
1340 		if (pcie_relaxed_ordering == 0 &&
1341 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) != 0) {
1342 			v &= ~PCIEM_CTL_RELAXED_ORD_ENABLE;
1343 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1344 		} else if (pcie_relaxed_ordering == 1 &&
1345 		    (v & PCIEM_CTL_RELAXED_ORD_ENABLE) == 0) {
1346 			v |= PCIEM_CTL_RELAXED_ORD_ENABLE;
1347 			pci_write_config(dev, i + PCIER_DEVICE_CTL, v, 2);
1348 		}
1349 	}
1350 
1351 	sc->sge_gts_reg = MYPF_REG(A_SGE_PF_GTS);
1352 	sc->sge_kdoorbell_reg = MYPF_REG(A_SGE_PF_KDOORBELL);
1353 	sc->traceq = -1;
1354 	mtx_init(&sc->ifp_lock, sc->ifp_lockname, 0, MTX_DEF);
1355 	snprintf(sc->ifp_lockname, sizeof(sc->ifp_lockname), "%s tracer",
1356 	    device_get_nameunit(dev));
1357 
1358 	snprintf(sc->lockname, sizeof(sc->lockname), "%s",
1359 	    device_get_nameunit(dev));
1360 	mtx_init(&sc->sc_lock, sc->lockname, 0, MTX_DEF);
1361 	t4_add_adapter(sc);
1362 
1363 	mtx_init(&sc->sfl_lock, "starving freelists", 0, MTX_DEF);
1364 	TAILQ_INIT(&sc->sfl);
1365 	callout_init_mtx(&sc->sfl_callout, &sc->sfl_lock, 0);
1366 
1367 	mtx_init(&sc->reg_lock, "indirect register access", 0, MTX_DEF);
1368 
1369 	sc->policy = NULL;
1370 	rw_init(&sc->policy_lock, "connection offload policy");
1371 
1372 	callout_init(&sc->ktls_tick, 1);
1373 
1374 	callout_init(&sc->cal_callout, 1);
1375 
1376 	refcount_init(&sc->vxlan_refcount, 0);
1377 
1378 	TASK_INIT(&sc->reset_task, 0, reset_adapter_task, sc);
1379 	TASK_INIT(&sc->fatal_error_task, 0, fatal_error_task, sc);
1380 
1381 	sc->ctrlq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1382 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "ctrlq",
1383 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "control queues");
1384 	sc->fwq_oid = SYSCTL_ADD_NODE(&sc->ctx,
1385 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "fwq",
1386 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "firmware event queue");
1387 
1388 	rc = t4_map_bars_0_and_4(sc);
1389 	if (rc != 0)
1390 		goto done; /* error message displayed already */
1391 
1392 	memset(sc->chan_map, 0xff, sizeof(sc->chan_map));
1393 	memset(sc->port_map, 0xff, sizeof(sc->port_map));
1394 
1395 	/* Prepare the adapter for operation. */
1396 	buf = malloc(PAGE_SIZE, M_CXGBE, M_ZERO | M_WAITOK);
1397 	rc = -t4_prep_adapter(sc, buf);
1398 	free(buf, M_CXGBE);
1399 	if (rc != 0) {
1400 		device_printf(dev, "failed to prepare adapter: %d.\n", rc);
1401 		goto done;
1402 	}
1403 
1404 	/*
1405 	 * This is the real PF# to which we're attaching.  Works from within PCI
1406 	 * passthrough environments too, where pci_get_function() could return a
1407 	 * different PF# depending on the passthrough configuration.  We need to
1408 	 * use the real PF# in all our communication with the firmware.
1409 	 */
1410 	j = t4_read_reg(sc, A_PL_WHOAMI);
1411 	sc->pf = chip_id(sc) <= CHELSIO_T5 ? G_SOURCEPF(j) : G_T6_SOURCEPF(j);
1412 	sc->mbox = sc->pf;
1413 
1414 	t4_init_devnames(sc);
1415 	if (sc->names == NULL) {
1416 		rc = ENOTSUP;
1417 		goto done; /* error message displayed already */
1418 	}
1419 
1420 	/*
1421 	 * Do this really early, with the memory windows set up even before the
1422 	 * character device.  The userland tool's register i/o and mem read
1423 	 * will work even in "recovery mode".
1424 	 */
1425 	setup_memwin(sc);
1426 	if (t4_init_devlog_ncores_params(sc, 0) == 0)
1427 		fixup_devlog_params(sc);
1428 	make_dev_args_init(&mda);
1429 	mda.mda_devsw = &t4_cdevsw;
1430 	mda.mda_uid = UID_ROOT;
1431 	mda.mda_gid = GID_WHEEL;
1432 	mda.mda_mode = 0600;
1433 	mda.mda_si_drv1 = sc;
1434 	rc = make_dev_s(&mda, &sc->cdev, "%s", device_get_nameunit(dev));
1435 	if (rc != 0)
1436 		device_printf(dev, "failed to create nexus char device: %d.\n",
1437 		    rc);
1438 
1439 	/* Go no further if recovery mode has been requested. */
1440 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
1441 		device_printf(dev, "recovery mode.\n");
1442 		goto done;
1443 	}
1444 
1445 #if defined(__i386__)
1446 	if ((cpu_feature & CPUID_CX8) == 0) {
1447 		device_printf(dev, "64 bit atomics not available.\n");
1448 		rc = ENOTSUP;
1449 		goto done;
1450 	}
1451 #endif
1452 
1453 	/* Contact the firmware and try to become the master driver. */
1454 	rc = contact_firmware(sc);
1455 	if (rc != 0)
1456 		goto done; /* error message displayed already */
1457 	MPASS(sc->flags & FW_OK);
1458 
1459 	rc = get_params__pre_init(sc);
1460 	if (rc != 0)
1461 		goto done; /* error message displayed already */
1462 
1463 	if (sc->flags & MASTER_PF) {
1464 		rc = partition_resources(sc);
1465 		if (rc != 0)
1466 			goto done; /* error message displayed already */
1467 	}
1468 
1469 	rc = get_params__post_init(sc);
1470 	if (rc != 0)
1471 		goto done; /* error message displayed already */
1472 
1473 	rc = set_params__post_init(sc);
1474 	if (rc != 0)
1475 		goto done; /* error message displayed already */
1476 
1477 	rc = t4_map_bar_2(sc);
1478 	if (rc != 0)
1479 		goto done; /* error message displayed already */
1480 
1481 	rc = t4_adj_doorbells(sc);
1482 	if (rc != 0)
1483 		goto done; /* error message displayed already */
1484 
1485 	rc = t4_create_dma_tag(sc);
1486 	if (rc != 0)
1487 		goto done; /* error message displayed already */
1488 
1489 	/*
1490 	 * First pass over all the ports - allocate VIs and initialize some
1491 	 * basic parameters like mac address, port type, etc.
1492 	 */
1493 	for_each_port(sc, i) {
1494 		struct port_info *pi;
1495 
1496 		pi = malloc(sizeof(*pi), M_CXGBE, M_ZERO | M_WAITOK);
1497 		sc->port[i] = pi;
1498 
1499 		/* These must be set before t4_port_init */
1500 		pi->adapter = sc;
1501 		pi->port_id = i;
1502 		/*
1503 		 * XXX: vi[0] is special so we can't delay this allocation until
1504 		 * pi->nvi's final value is known.
1505 		 */
1506 		pi->vi = malloc(sizeof(struct vi_info) * t4_num_vis, M_CXGBE,
1507 		    M_ZERO | M_WAITOK);
1508 
1509 		/*
1510 		 * Allocate the "main" VI and initialize parameters
1511 		 * like mac addr.
1512 		 */
1513 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
1514 		if (rc != 0) {
1515 			device_printf(dev, "unable to initialize port %d: %d\n",
1516 			    i, rc);
1517 			free(pi->vi, M_CXGBE);
1518 			free(pi, M_CXGBE);
1519 			sc->port[i] = NULL;
1520 			goto done;
1521 		}
1522 
1523 		if (is_bt(pi->port_type))
1524 			setbit(&sc->bt_map, pi->hw_port);
1525 		else
1526 			MPASS(!isset(&sc->bt_map, pi->hw_port));
1527 
1528 		snprintf(pi->lockname, sizeof(pi->lockname), "%sp%d",
1529 		    device_get_nameunit(dev), i);
1530 		mtx_init(&pi->pi_lock, pi->lockname, 0, MTX_DEF);
1531 		for (j = 0; j < sc->params.tp.lb_nchan; j++)
1532 			sc->chan_map[pi->tx_chan + j] = i;
1533 		sc->port_map[pi->hw_port] = i;
1534 
1535 		/*
1536 		 * The MPS counter for FCS errors doesn't work correctly on the
1537 		 * T6 so we use the MAC counter here.  Which MAC is in use
1538 		 * depends on the link settings which will be known when the
1539 		 * link comes up.
1540 		 */
1541 		if (is_t6(sc))
1542 			pi->fcs_reg = -1;
1543 		else
1544 			pi->fcs_reg = A_MPS_PORT_STAT_RX_PORT_CRC_ERROR_L;
1545 		pi->fcs_base = 0;
1546 
1547 		/* All VIs on this port share this media. */
1548 		ifmedia_init(&pi->media, IFM_IMASK, cxgbe_media_change,
1549 		    cxgbe_media_status);
1550 
1551 		PORT_LOCK(pi);
1552 		init_link_config(pi);
1553 		fixup_link_config(pi);
1554 		build_medialist(pi);
1555 		if (fixed_ifmedia(pi))
1556 			pi->flags |= FIXED_IFMEDIA;
1557 		PORT_UNLOCK(pi);
1558 
1559 		pi->dev = device_add_child(dev, sc->names->ifnet_name,
1560 		    t4_ifnet_unit(sc, pi));
1561 		if (pi->dev == NULL) {
1562 			device_printf(dev,
1563 			    "failed to add device for port %d.\n", i);
1564 			rc = ENXIO;
1565 			goto done;
1566 		}
1567 		pi->vi[0].dev = pi->dev;
1568 		device_set_softc(pi->dev, pi);
1569 	}
1570 
1571 	/*
1572 	 * Interrupt type, # of interrupts, # of rx/tx queues, etc.
1573 	 */
1574 	nports = sc->params.nports;
1575 	rc = cfg_itype_and_nqueues(sc, &iaq);
1576 	if (rc != 0)
1577 		goto done; /* error message displayed already */
1578 
1579 	num_vis = iaq.num_vis;
1580 	sc->intr_type = iaq.intr_type;
1581 	sc->intr_count = iaq.nirq;
1582 
1583 	s = &sc->sge;
1584 	s->nctrlq = max(sc->params.nports, sc->params.ncores);
1585 	s->nrxq = nports * iaq.nrxq;
1586 	s->ntxq = nports * iaq.ntxq;
1587 	if (num_vis > 1) {
1588 		s->nrxq += nports * (num_vis - 1) * iaq.nrxq_vi;
1589 		s->ntxq += nports * (num_vis - 1) * iaq.ntxq_vi;
1590 	}
1591 	s->neq = s->ntxq + s->nrxq;	/* the free list in an rxq is an eq */
1592 	s->neq += nports;		/* ctrl queues: 1 per port */
1593 	s->niq = s->nrxq + 1;		/* 1 extra for firmware event queue */
1594 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1595 	if (is_offload(sc) || is_ethoffload(sc)) {
1596 		s->nofldtxq = nports * iaq.nofldtxq;
1597 		if (num_vis > 1)
1598 			s->nofldtxq += nports * (num_vis - 1) * iaq.nofldtxq_vi;
1599 		s->neq += s->nofldtxq;
1600 
1601 		s->ofld_txq = malloc(s->nofldtxq * sizeof(struct sge_ofld_txq),
1602 		    M_CXGBE, M_ZERO | M_WAITOK);
1603 	}
1604 #endif
1605 #ifdef TCP_OFFLOAD
1606 	if (is_offload(sc)) {
1607 		s->nofldrxq = nports * iaq.nofldrxq;
1608 		if (num_vis > 1)
1609 			s->nofldrxq += nports * (num_vis - 1) * iaq.nofldrxq_vi;
1610 		s->neq += s->nofldrxq;	/* free list */
1611 		s->niq += s->nofldrxq;
1612 
1613 		s->ofld_rxq = malloc(s->nofldrxq * sizeof(struct sge_ofld_rxq),
1614 		    M_CXGBE, M_ZERO | M_WAITOK);
1615 	}
1616 #endif
1617 #ifdef DEV_NETMAP
1618 	s->nnmrxq = 0;
1619 	s->nnmtxq = 0;
1620 	if (t4_native_netmap & NN_MAIN_VI) {
1621 		s->nnmrxq += nports * iaq.nnmrxq;
1622 		s->nnmtxq += nports * iaq.nnmtxq;
1623 	}
1624 	if (num_vis > 1 && t4_native_netmap & NN_EXTRA_VI) {
1625 		s->nnmrxq += nports * (num_vis - 1) * iaq.nnmrxq_vi;
1626 		s->nnmtxq += nports * (num_vis - 1) * iaq.nnmtxq_vi;
1627 	}
1628 	s->neq += s->nnmtxq + s->nnmrxq;
1629 	s->niq += s->nnmrxq;
1630 
1631 	s->nm_rxq = malloc(s->nnmrxq * sizeof(struct sge_nm_rxq),
1632 	    M_CXGBE, M_ZERO | M_WAITOK);
1633 	s->nm_txq = malloc(s->nnmtxq * sizeof(struct sge_nm_txq),
1634 	    M_CXGBE, M_ZERO | M_WAITOK);
1635 #endif
1636 	MPASS(s->niq <= s->iqmap_sz);
1637 	MPASS(s->neq <= s->eqmap_sz);
1638 
1639 	s->ctrlq = malloc(s->nctrlq * sizeof(struct sge_wrq), M_CXGBE,
1640 	    M_ZERO | M_WAITOK);
1641 	s->rxq = malloc(s->nrxq * sizeof(struct sge_rxq), M_CXGBE,
1642 	    M_ZERO | M_WAITOK);
1643 	s->txq = malloc(s->ntxq * sizeof(struct sge_txq), M_CXGBE,
1644 	    M_ZERO | M_WAITOK);
1645 	s->iqmap = malloc(s->iqmap_sz * sizeof(struct sge_iq *), M_CXGBE,
1646 	    M_ZERO | M_WAITOK);
1647 	s->eqmap = malloc(s->eqmap_sz * sizeof(struct sge_eq *), M_CXGBE,
1648 	    M_ZERO | M_WAITOK);
1649 
1650 	sc->irq = malloc(sc->intr_count * sizeof(struct irq), M_CXGBE,
1651 	    M_ZERO | M_WAITOK);
1652 
1653 	t4_init_l2t(sc, M_WAITOK);
1654 	t4_init_smt(sc, M_WAITOK);
1655 	t4_init_tx_sched(sc);
1656 	t4_init_atid_table(sc);
1657 #ifdef RATELIMIT
1658 	t4_init_etid_table(sc);
1659 #endif
1660 #ifdef INET6
1661 	t4_init_clip_table(sc);
1662 #endif
1663 	if (sc->vres.key.size != 0)
1664 		sc->key_map = vmem_create("T4TLS key map", sc->vres.key.start,
1665 		    sc->vres.key.size, 32, 0, M_FIRSTFIT | M_WAITOK);
1666 	t4_init_tpt(sc);
1667 
1668 	/*
1669 	 * Second pass over the ports.  This time we know the number of rx and
1670 	 * tx queues that each port should get.
1671 	 */
1672 	rqidx = tqidx = 0;
1673 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1674 	ofld_tqidx = 0;
1675 #endif
1676 #ifdef TCP_OFFLOAD
1677 	ofld_rqidx = 0;
1678 #endif
1679 #ifdef DEV_NETMAP
1680 	nm_rqidx = nm_tqidx = 0;
1681 #endif
1682 	for_each_port(sc, i) {
1683 		struct port_info *pi = sc->port[i];
1684 		struct vi_info *vi;
1685 
1686 		if (pi == NULL)
1687 			continue;
1688 
1689 		pi->nvi = num_vis;
1690 		for_each_vi(pi, j, vi) {
1691 			vi->pi = pi;
1692 			vi->adapter = sc;
1693 			vi->first_intr = -1;
1694 			vi->qsize_rxq = t4_qsize_rxq;
1695 			vi->qsize_txq = t4_qsize_txq;
1696 
1697 			vi->first_rxq = rqidx;
1698 			vi->first_txq = tqidx;
1699 			vi->tmr_idx = t4_tmr_idx;
1700 			vi->pktc_idx = t4_pktc_idx;
1701 			vi->nrxq = j == 0 ? iaq.nrxq : iaq.nrxq_vi;
1702 			vi->ntxq = j == 0 ? iaq.ntxq : iaq.ntxq_vi;
1703 
1704 			rqidx += vi->nrxq;
1705 			tqidx += vi->ntxq;
1706 
1707 			if (j == 0 && vi->ntxq > 1)
1708 				vi->rsrv_noflowq = t4_rsrv_noflowq ? 1 : 0;
1709 			else
1710 				vi->rsrv_noflowq = 0;
1711 
1712 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1713 			vi->first_ofld_txq = ofld_tqidx;
1714 			vi->nofldtxq = j == 0 ? iaq.nofldtxq : iaq.nofldtxq_vi;
1715 			ofld_tqidx += vi->nofldtxq;
1716 #endif
1717 #ifdef TCP_OFFLOAD
1718 			vi->ofld_tmr_idx = t4_tmr_idx_ofld;
1719 			vi->ofld_pktc_idx = t4_pktc_idx_ofld;
1720 			vi->first_ofld_rxq = ofld_rqidx;
1721 			vi->nofldrxq = j == 0 ? iaq.nofldrxq : iaq.nofldrxq_vi;
1722 
1723 			ofld_rqidx += vi->nofldrxq;
1724 #endif
1725 #ifdef DEV_NETMAP
1726 			vi->first_nm_rxq = nm_rqidx;
1727 			vi->first_nm_txq = nm_tqidx;
1728 			if (j == 0) {
1729 				vi->nnmrxq = iaq.nnmrxq;
1730 				vi->nnmtxq = iaq.nnmtxq;
1731 			} else {
1732 				vi->nnmrxq = iaq.nnmrxq_vi;
1733 				vi->nnmtxq = iaq.nnmtxq_vi;
1734 			}
1735 			nm_rqidx += vi->nnmrxq;
1736 			nm_tqidx += vi->nnmtxq;
1737 #endif
1738 		}
1739 	}
1740 
1741 	rc = t4_setup_intr_handlers(sc);
1742 	if (rc != 0) {
1743 		device_printf(dev,
1744 		    "failed to setup interrupt handlers: %d\n", rc);
1745 		goto done;
1746 	}
1747 
1748 	bus_identify_children(dev);
1749 
1750 	/*
1751 	 * Ensure thread-safe mailbox access (in debug builds).
1752 	 *
1753 	 * So far this was the only thread accessing the mailbox but various
1754 	 * ifnets and sysctls are about to be created and their handlers/ioctls
1755 	 * will access the mailbox from different threads.
1756 	 */
1757 	sc->flags |= CHK_MBOX_ACCESS;
1758 
1759 	bus_attach_children(dev);
1760 	t4_calibration_start(sc);
1761 
1762 	device_printf(dev,
1763 	    "PCIe gen%d x%d, %d ports, %d %s interrupt%s, %d eq, %d iq\n",
1764 	    sc->params.pci.speed, sc->params.pci.width, sc->params.nports,
1765 	    sc->intr_count, sc->intr_type == INTR_MSIX ? "MSI-X" :
1766 	    (sc->intr_type == INTR_MSI ? "MSI" : "INTx"),
1767 	    sc->intr_count > 1 ? "s" : "", sc->sge.neq, sc->sge.niq);
1768 
1769 	t4_set_desc(sc);
1770 
1771 	notify_siblings(dev, 0);
1772 
1773 done:
1774 	if (rc != 0 && sc->cdev) {
1775 		/* cdev was created and so cxgbetool works; recover that way. */
1776 		device_printf(dev,
1777 		    "error during attach, adapter is now in recovery mode.\n");
1778 		rc = 0;
1779 	}
1780 
1781 	if (rc != 0)
1782 		t4_detach_common(dev);
1783 	else
1784 		t4_sysctls(sc);
1785 
1786 	return (rc);
1787 }
1788 
1789 static int
t4_child_location(device_t bus,device_t dev,struct sbuf * sb)1790 t4_child_location(device_t bus, device_t dev, struct sbuf *sb)
1791 {
1792 	struct adapter *sc;
1793 	struct port_info *pi;
1794 	int i;
1795 
1796 	sc = device_get_softc(bus);
1797 	for_each_port(sc, i) {
1798 		pi = sc->port[i];
1799 		if (pi != NULL && pi->dev == dev) {
1800 			sbuf_printf(sb, "port=%d", pi->port_id);
1801 			break;
1802 		}
1803 	}
1804 	return (0);
1805 }
1806 
1807 static int
t4_ready(device_t dev)1808 t4_ready(device_t dev)
1809 {
1810 	struct adapter *sc;
1811 
1812 	sc = device_get_softc(dev);
1813 	if (sc->flags & FW_OK)
1814 		return (0);
1815 	return (ENXIO);
1816 }
1817 
1818 static int
t4_read_port_device(device_t dev,int port,device_t * child)1819 t4_read_port_device(device_t dev, int port, device_t *child)
1820 {
1821 	struct adapter *sc;
1822 	struct port_info *pi;
1823 
1824 	sc = device_get_softc(dev);
1825 	if (port < 0 || port >= MAX_NPORTS)
1826 		return (EINVAL);
1827 	pi = sc->port[port];
1828 	if (pi == NULL || pi->dev == NULL)
1829 		return (ENXIO);
1830 	*child = pi->dev;
1831 	return (0);
1832 }
1833 
1834 static int
notify_siblings(device_t dev,int detaching)1835 notify_siblings(device_t dev, int detaching)
1836 {
1837 	device_t sibling;
1838 	int error, i;
1839 
1840 	error = 0;
1841 	for (i = 0; i < PCI_FUNCMAX; i++) {
1842 		if (i == pci_get_function(dev))
1843 			continue;
1844 		sibling = pci_find_dbsf(pci_get_domain(dev), pci_get_bus(dev),
1845 		    pci_get_slot(dev), i);
1846 		if (sibling == NULL || !device_is_attached(sibling))
1847 			continue;
1848 		if (detaching)
1849 			error = T4_DETACH_CHILD(sibling);
1850 		else
1851 			(void)T4_ATTACH_CHILD(sibling);
1852 		if (error)
1853 			break;
1854 	}
1855 	return (error);
1856 }
1857 
1858 /*
1859  * Idempotent
1860  */
1861 static int
t4_detach(device_t dev)1862 t4_detach(device_t dev)
1863 {
1864 	int rc;
1865 
1866 	rc = notify_siblings(dev, 1);
1867 	if (rc) {
1868 		device_printf(dev,
1869 		    "failed to detach sibling devices: %d\n", rc);
1870 		return (rc);
1871 	}
1872 
1873 	return (t4_detach_common(dev));
1874 }
1875 
1876 int
t4_detach_common(device_t dev)1877 t4_detach_common(device_t dev)
1878 {
1879 	struct adapter *sc;
1880 	struct port_info *pi;
1881 	int i, rc;
1882 
1883 	sc = device_get_softc(dev);
1884 
1885 #ifdef TCP_OFFLOAD
1886 	rc = deactivate_all_uld(sc);
1887 	if (rc) {
1888 		device_printf(dev,
1889 		    "failed to detach upper layer drivers: %d\n", rc);
1890 		return (rc);
1891 	}
1892 #endif
1893 
1894 	if (sc->cdev) {
1895 		destroy_dev(sc->cdev);
1896 		sc->cdev = NULL;
1897 	}
1898 
1899 	sx_xlock(&t4_list_lock);
1900 	SLIST_REMOVE(&t4_list, sc, adapter, link);
1901 	sx_xunlock(&t4_list_lock);
1902 
1903 	sc->flags &= ~CHK_MBOX_ACCESS;
1904 	if (sc->flags & FULL_INIT_DONE) {
1905 		if (!(sc->flags & IS_VF))
1906 			t4_intr_disable(sc);
1907 	}
1908 
1909 	if (device_is_attached(dev)) {
1910 		rc = bus_detach_children(dev);
1911 		if (rc) {
1912 			device_printf(dev,
1913 			    "failed to detach child devices: %d\n", rc);
1914 			return (rc);
1915 		}
1916 	}
1917 
1918 	for (i = 0; i < sc->intr_count; i++)
1919 		t4_free_irq(sc, &sc->irq[i]);
1920 
1921 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1922 		t4_free_tx_sched(sc);
1923 
1924 	for (i = 0; i < MAX_NPORTS; i++) {
1925 		pi = sc->port[i];
1926 		if (pi) {
1927 			t4_free_vi(sc, sc->mbox, sc->pf, 0, pi->vi[0].viid);
1928 
1929 			mtx_destroy(&pi->pi_lock);
1930 			free(pi->vi, M_CXGBE);
1931 			free(pi, M_CXGBE);
1932 		}
1933 	}
1934 	callout_stop(&sc->cal_callout);
1935 	callout_drain(&sc->cal_callout);
1936 	device_delete_children(dev);
1937 	sysctl_ctx_free(&sc->ctx);
1938 	adapter_full_uninit(sc);
1939 
1940 	if ((sc->flags & (IS_VF | FW_OK)) == FW_OK)
1941 		t4_fw_bye(sc, sc->mbox);
1942 
1943 	if (sc->intr_type == INTR_MSI || sc->intr_type == INTR_MSIX)
1944 		pci_release_msi(dev);
1945 
1946 	if (sc->regs_res)
1947 		bus_release_resource(dev, SYS_RES_MEMORY, sc->regs_rid,
1948 		    sc->regs_res);
1949 
1950 	if (sc->udbs_res)
1951 		bus_release_resource(dev, SYS_RES_MEMORY, sc->udbs_rid,
1952 		    sc->udbs_res);
1953 
1954 	if (sc->msix_res)
1955 		bus_release_resource(dev, SYS_RES_MEMORY, sc->msix_rid,
1956 		    sc->msix_res);
1957 
1958 	if (sc->l2t)
1959 		t4_free_l2t(sc);
1960 	if (sc->smt)
1961 		t4_free_smt(sc->smt);
1962 	t4_free_atid_table(sc);
1963 #ifdef RATELIMIT
1964 	t4_free_etid_table(sc);
1965 #endif
1966 	if (sc->key_map)
1967 		vmem_destroy(sc->key_map);
1968 	t4_free_tpt(sc);
1969 #ifdef INET6
1970 	t4_destroy_clip_table(sc);
1971 #endif
1972 
1973 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
1974 	free(sc->sge.ofld_txq, M_CXGBE);
1975 #endif
1976 #ifdef TCP_OFFLOAD
1977 	free(sc->sge.ofld_rxq, M_CXGBE);
1978 #endif
1979 #ifdef DEV_NETMAP
1980 	free(sc->sge.nm_rxq, M_CXGBE);
1981 	free(sc->sge.nm_txq, M_CXGBE);
1982 #endif
1983 	free(sc->irq, M_CXGBE);
1984 	free(sc->sge.rxq, M_CXGBE);
1985 	free(sc->sge.txq, M_CXGBE);
1986 	free(sc->sge.ctrlq, M_CXGBE);
1987 	free(sc->sge.iqmap, M_CXGBE);
1988 	free(sc->sge.eqmap, M_CXGBE);
1989 	free(sc->tids.ftid_tab, M_CXGBE);
1990 	free(sc->tids.hpftid_tab, M_CXGBE);
1991 	free_hftid_hash(&sc->tids);
1992 	free(sc->tids.tid_tab, M_CXGBE);
1993 	t4_destroy_dma_tag(sc);
1994 
1995 	callout_drain(&sc->ktls_tick);
1996 	callout_drain(&sc->sfl_callout);
1997 	if (mtx_initialized(&sc->tids.ftid_lock)) {
1998 		mtx_destroy(&sc->tids.ftid_lock);
1999 		cv_destroy(&sc->tids.ftid_cv);
2000 	}
2001 	if (mtx_initialized(&sc->tids.atid_lock))
2002 		mtx_destroy(&sc->tids.atid_lock);
2003 	if (mtx_initialized(&sc->ifp_lock))
2004 		mtx_destroy(&sc->ifp_lock);
2005 
2006 	if (rw_initialized(&sc->policy_lock)) {
2007 		rw_destroy(&sc->policy_lock);
2008 #ifdef TCP_OFFLOAD
2009 		if (sc->policy != NULL)
2010 			free_offload_policy(sc->policy);
2011 #endif
2012 	}
2013 
2014 	for (i = 0; i < NUM_MEMWIN; i++) {
2015 		struct memwin *mw = &sc->memwin[i];
2016 
2017 		if (rw_initialized(&mw->mw_lock))
2018 			rw_destroy(&mw->mw_lock);
2019 	}
2020 
2021 	mtx_destroy(&sc->sfl_lock);
2022 	mtx_destroy(&sc->reg_lock);
2023 	mtx_destroy(&sc->sc_lock);
2024 
2025 	bzero(sc, sizeof(*sc));
2026 
2027 	return (0);
2028 }
2029 
2030 static inline int
stop_adapter(struct adapter * sc)2031 stop_adapter(struct adapter *sc)
2032 {
2033 	struct port_info *pi;
2034 	int i;
2035 
2036 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
2037 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
2038 			 __func__, curthread, sc->flags, sc->error_flags);
2039 		return (EALREADY);
2040 	}
2041 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
2042 		 sc->flags, sc->error_flags);
2043 	t4_shutdown_adapter(sc);
2044 	for_each_port(sc, i) {
2045 		pi = sc->port[i];
2046 		if (pi == NULL)
2047 			continue;
2048 		PORT_LOCK(pi);
2049 		if (pi->up_vis > 0 && pi->link_cfg.link_ok) {
2050 			/*
2051 			 * t4_shutdown_adapter has already shut down all the
2052 			 * PHYs but it also disables interrupts and DMA so there
2053 			 * won't be a link interrupt.  Update the state manually
2054 			 * if the link was up previously and inform the kernel.
2055 			 */
2056 			pi->link_cfg.link_ok = false;
2057 			t4_os_link_changed(pi);
2058 		}
2059 		PORT_UNLOCK(pi);
2060 	}
2061 
2062 	return (0);
2063 }
2064 
2065 static inline int
restart_adapter(struct adapter * sc)2066 restart_adapter(struct adapter *sc)
2067 {
2068 	uint32_t val;
2069 
2070 	if (!atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_STOPPED))) {
2071 		CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x, EALREADY\n",
2072 			 __func__, curthread, sc->flags, sc->error_flags);
2073 		return (EALREADY);
2074 	}
2075 	CH_ALERT(sc, "%s from %p, flags 0x%08x,0x%08x\n", __func__, curthread,
2076 		 sc->flags, sc->error_flags);
2077 
2078 	MPASS(hw_off_limits(sc));
2079 	MPASS((sc->flags & FW_OK) == 0);
2080 	MPASS((sc->flags & MASTER_PF) == 0);
2081 	MPASS(sc->reset_thread == NULL);
2082 
2083 	/*
2084 	 * The adapter is supposed to be back on PCIE with its config space and
2085 	 * BARs restored to their state before reset.  Register access via
2086 	 * t4_read_reg BAR0 should just work.
2087 	 */
2088 	sc->reset_thread = curthread;
2089 	val = t4_read_reg(sc, A_PL_WHOAMI);
2090 	if (val == 0xffffffff || val == 0xeeeeeeee) {
2091 		CH_ERR(sc, "%s: device registers not readable.\n", __func__);
2092 		sc->reset_thread = NULL;
2093 		atomic_set_int(&sc->error_flags, ADAP_STOPPED);
2094 		return (ENXIO);
2095 	}
2096 	atomic_clear_int(&sc->error_flags, ADAP_FATAL_ERR);
2097 	atomic_add_int(&sc->incarnation, 1);
2098 	atomic_add_int(&sc->num_resets, 1);
2099 
2100 	return (0);
2101 }
2102 
2103 static inline void
set_adapter_hwstatus(struct adapter * sc,const bool usable)2104 set_adapter_hwstatus(struct adapter *sc, const bool usable)
2105 {
2106 	if (usable) {
2107 		/* Must be marked reusable by the designated thread. */
2108 		ASSERT_SYNCHRONIZED_OP(sc);
2109 		MPASS(sc->reset_thread == curthread);
2110 		mtx_lock(&sc->reg_lock);
2111 		atomic_clear_int(&sc->error_flags, HW_OFF_LIMITS);
2112 		mtx_unlock(&sc->reg_lock);
2113 	} else {
2114 		/* Mark the adapter totally off limits. */
2115 		begin_synchronized_op(sc, NULL, SLEEP_OK, "t4hwsts");
2116 		mtx_lock(&sc->reg_lock);
2117 		atomic_set_int(&sc->error_flags, HW_OFF_LIMITS);
2118 		mtx_unlock(&sc->reg_lock);
2119 		sc->flags &= ~(FW_OK | MASTER_PF);
2120 		sc->reset_thread = NULL;
2121 		end_synchronized_op(sc, 0);
2122 	}
2123 }
2124 
2125 static int
stop_lld(struct adapter * sc)2126 stop_lld(struct adapter *sc)
2127 {
2128 	struct port_info *pi;
2129 	struct vi_info *vi;
2130 	if_t ifp;
2131 	struct sge_rxq *rxq;
2132 	struct sge_txq *txq;
2133 	struct sge_wrq *wrq;
2134 #ifdef TCP_OFFLOAD
2135 	struct sge_ofld_rxq *ofld_rxq;
2136 #endif
2137 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2138 	struct sge_ofld_txq *ofld_txq;
2139 #endif
2140 	int rc, i, j, k;
2141 
2142 	/*
2143 	 * XXX: Can there be a synch_op in progress that will hang because
2144 	 * hardware has been stopped?  We'll hang too and the solution will be
2145 	 * to use a version of begin_synch_op that wakes up existing synch_op
2146 	 * with errors.  Maybe stop_adapter should do this wakeup?
2147 	 *
2148 	 * I don't think any synch_op could get stranded waiting for DMA or
2149 	 * interrupt so I think we're okay here.  Remove this comment block
2150 	 * after testing.
2151 	 */
2152 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4slld");
2153 	if (rc != 0)
2154 		return (ENXIO);
2155 
2156 	/* Quiesce all activity. */
2157 	for_each_port(sc, i) {
2158 		pi = sc->port[i];
2159 		if (pi == NULL)
2160 			continue;
2161 		pi->vxlan_tcam_entry = false;
2162 		for_each_vi(pi, j, vi) {
2163 			vi->xact_addr_filt = -1;
2164 			mtx_lock(&vi->tick_mtx);
2165 			vi->flags |= VI_SKIP_STATS;
2166 			mtx_unlock(&vi->tick_mtx);
2167 			if (!(vi->flags & VI_INIT_DONE))
2168 				continue;
2169 
2170 			ifp = vi->ifp;
2171 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
2172 				mtx_lock(&vi->tick_mtx);
2173 				callout_stop(&vi->tick);
2174 				mtx_unlock(&vi->tick_mtx);
2175 				callout_drain(&vi->tick);
2176 			}
2177 
2178 			/*
2179 			 * Note that the HW is not available.
2180 			 */
2181 			for_each_txq(vi, k, txq) {
2182 				TXQ_LOCK(txq);
2183 				txq->eq.flags &= ~(EQ_ENABLED | EQ_HW_ALLOCATED);
2184 				TXQ_UNLOCK(txq);
2185 			}
2186 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2187 			for_each_ofld_txq(vi, k, ofld_txq) {
2188 				TXQ_LOCK(&ofld_txq->wrq);
2189 				ofld_txq->wrq.eq.flags &= ~EQ_HW_ALLOCATED;
2190 				TXQ_UNLOCK(&ofld_txq->wrq);
2191 			}
2192 #endif
2193 			for_each_rxq(vi, k, rxq) {
2194 				rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2195 			}
2196 #if defined(TCP_OFFLOAD)
2197 			for_each_ofld_rxq(vi, k, ofld_rxq) {
2198 				ofld_rxq->iq.flags &= ~IQ_HW_ALLOCATED;
2199 			}
2200 #endif
2201 
2202 			quiesce_vi(vi);
2203 		}
2204 
2205 		if (sc->flags & FULL_INIT_DONE) {
2206 			/* Control queue */
2207 			wrq = &sc->sge.ctrlq[i];
2208 			TXQ_LOCK(wrq);
2209 			wrq->eq.flags &= ~EQ_HW_ALLOCATED;
2210 			TXQ_UNLOCK(wrq);
2211 			quiesce_wrq(wrq);
2212 		}
2213 
2214 		if (pi->flags & HAS_TRACEQ) {
2215 			pi->flags &= ~HAS_TRACEQ;
2216 			sc->traceq = -1;
2217 			sc->tracer_valid = 0;
2218 			sc->tracer_enabled = 0;
2219 		}
2220 	}
2221 	if (sc->flags & FULL_INIT_DONE) {
2222 		/* Firmware event queue */
2223 		sc->sge.fwq.flags &= ~IQ_HW_ALLOCATED;
2224 		quiesce_iq_fl(sc, &sc->sge.fwq, NULL);
2225 	}
2226 
2227 	/* Stop calibration */
2228 	callout_stop(&sc->cal_callout);
2229 	callout_drain(&sc->cal_callout);
2230 
2231 	if (t4_clock_gate_on_suspend) {
2232 		t4_set_reg_field(sc, A_PMU_PART_CG_PWRMODE, F_MA_PART_CGEN |
2233 		    F_LE_PART_CGEN | F_EDC1_PART_CGEN | F_EDC0_PART_CGEN |
2234 		    F_TP_PART_CGEN | F_PDP_PART_CGEN | F_SGE_PART_CGEN, 0);
2235 	}
2236 
2237 	end_synchronized_op(sc, 0);
2238 
2239 	stop_atid_allocator(sc);
2240 	t4_stop_l2t(sc);
2241 
2242 	return (rc);
2243 }
2244 
2245 int
suspend_adapter(struct adapter * sc)2246 suspend_adapter(struct adapter *sc)
2247 {
2248 	stop_adapter(sc);
2249 	stop_lld(sc);
2250 #ifdef TCP_OFFLOAD
2251 	stop_all_uld(sc);
2252 #endif
2253 	set_adapter_hwstatus(sc, false);
2254 
2255 	return (0);
2256 }
2257 
2258 static int
t4_suspend(device_t dev)2259 t4_suspend(device_t dev)
2260 {
2261 	struct adapter *sc = device_get_softc(dev);
2262 	int rc;
2263 
2264 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2265 	rc = suspend_adapter(sc);
2266 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2267 
2268 	return (rc);
2269 }
2270 
2271 struct adapter_pre_reset_state {
2272 	u_int flags;
2273 	uint16_t nbmcaps;
2274 	uint16_t linkcaps;
2275 	uint16_t switchcaps;
2276 	uint16_t nvmecaps;
2277 	uint16_t niccaps;
2278 	uint16_t toecaps;
2279 	uint16_t rdmacaps;
2280 	uint16_t cryptocaps;
2281 	uint16_t iscsicaps;
2282 	uint16_t fcoecaps;
2283 
2284 	u_int cfcsum;
2285 	char cfg_file[32];
2286 
2287 	struct adapter_params params;
2288 	struct t4_virt_res vres;
2289 	struct tid_info tids;
2290 	struct sge sge;
2291 
2292 	int rawf_base;
2293 	int nrawf;
2294 
2295 };
2296 
2297 static void
save_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2298 save_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2299 {
2300 
2301 	ASSERT_SYNCHRONIZED_OP(sc);
2302 
2303 	o->flags = sc->flags;
2304 
2305 	o->nbmcaps =  sc->nbmcaps;
2306 	o->linkcaps = sc->linkcaps;
2307 	o->switchcaps = sc->switchcaps;
2308 	o->nvmecaps = sc->nvmecaps;
2309 	o->niccaps = sc->niccaps;
2310 	o->toecaps = sc->toecaps;
2311 	o->rdmacaps = sc->rdmacaps;
2312 	o->cryptocaps = sc->cryptocaps;
2313 	o->iscsicaps = sc->iscsicaps;
2314 	o->fcoecaps = sc->fcoecaps;
2315 
2316 	o->cfcsum = sc->cfcsum;
2317 	MPASS(sizeof(o->cfg_file) == sizeof(sc->cfg_file));
2318 	memcpy(o->cfg_file, sc->cfg_file, sizeof(o->cfg_file));
2319 
2320 	o->params = sc->params;
2321 	o->vres = sc->vres;
2322 	o->tids = sc->tids;
2323 	o->sge = sc->sge;
2324 
2325 	o->rawf_base = sc->rawf_base;
2326 	o->nrawf = sc->nrawf;
2327 }
2328 
2329 static int
compare_caps_and_params(struct adapter * sc,struct adapter_pre_reset_state * o)2330 compare_caps_and_params(struct adapter *sc, struct adapter_pre_reset_state *o)
2331 {
2332 	int rc = 0;
2333 
2334 	ASSERT_SYNCHRONIZED_OP(sc);
2335 
2336 	/* Capabilities */
2337 #define COMPARE_CAPS(c) do { \
2338 	if (o->c##caps != sc->c##caps) { \
2339 		CH_ERR(sc, "%scaps 0x%04x -> 0x%04x.\n", #c, o->c##caps, \
2340 		    sc->c##caps); \
2341 		rc = EINVAL; \
2342 	} \
2343 } while (0)
2344 	COMPARE_CAPS(nbm);
2345 	COMPARE_CAPS(link);
2346 	COMPARE_CAPS(switch);
2347 	COMPARE_CAPS(nvme);
2348 	COMPARE_CAPS(nic);
2349 	COMPARE_CAPS(toe);
2350 	COMPARE_CAPS(rdma);
2351 	COMPARE_CAPS(crypto);
2352 	COMPARE_CAPS(iscsi);
2353 	COMPARE_CAPS(fcoe);
2354 #undef COMPARE_CAPS
2355 
2356 	/* Firmware config file */
2357 	if (o->cfcsum != sc->cfcsum) {
2358 		CH_ERR(sc, "config file %s (0x%x) -> %s (0x%x)\n", o->cfg_file,
2359 		    o->cfcsum, sc->cfg_file, sc->cfcsum);
2360 		rc = EINVAL;
2361 	}
2362 
2363 #define COMPARE_PARAM(p, name) do { \
2364 	if (o->p != sc->p) { \
2365 		CH_ERR(sc, #name " %d -> %d\n", o->p, sc->p); \
2366 		rc = EINVAL; \
2367 	} \
2368 } while (0)
2369 	COMPARE_PARAM(sge.iq_start, iq_start);
2370 	COMPARE_PARAM(sge.eq_start, eq_start);
2371 	COMPARE_PARAM(tids.ftid_base, ftid_base);
2372 	COMPARE_PARAM(tids.ftid_end, ftid_end);
2373 	COMPARE_PARAM(tids.nftids, nftids);
2374 	COMPARE_PARAM(vres.l2t.start, l2t_start);
2375 	COMPARE_PARAM(vres.l2t.size, l2t_size);
2376 	COMPARE_PARAM(sge.iqmap_sz, iqmap_sz);
2377 	COMPARE_PARAM(sge.eqmap_sz, eqmap_sz);
2378 	COMPARE_PARAM(tids.tid_base, tid_base);
2379 	COMPARE_PARAM(tids.hpftid_base, hpftid_base);
2380 	COMPARE_PARAM(tids.hpftid_end, hpftid_end);
2381 	COMPARE_PARAM(tids.nhpftids, nhpftids);
2382 	COMPARE_PARAM(rawf_base, rawf_base);
2383 	COMPARE_PARAM(nrawf, nrawf);
2384 	COMPARE_PARAM(params.mps_bg_map, mps_bg_map);
2385 	COMPARE_PARAM(params.filter2_wr_support, filter2_wr_support);
2386 	COMPARE_PARAM(params.ulptx_memwrite_dsgl, ulptx_memwrite_dsgl);
2387 	COMPARE_PARAM(params.fr_nsmr_tpte_wr_support, fr_nsmr_tpte_wr_support);
2388 	COMPARE_PARAM(params.max_pkts_per_eth_tx_pkts_wr, max_pkts_per_eth_tx_pkts_wr);
2389 	COMPARE_PARAM(tids.ntids, ntids);
2390 	COMPARE_PARAM(tids.etid_base, etid_base);
2391 	COMPARE_PARAM(tids.etid_end, etid_end);
2392 	COMPARE_PARAM(tids.netids, netids);
2393 	COMPARE_PARAM(params.eo_wr_cred, eo_wr_cred);
2394 	COMPARE_PARAM(params.ethoffload, ethoffload);
2395 	COMPARE_PARAM(tids.natids, natids);
2396 	COMPARE_PARAM(tids.stid_base, stid_base);
2397 	COMPARE_PARAM(vres.ddp.start, ddp_start);
2398 	COMPARE_PARAM(vres.ddp.size, ddp_size);
2399 	COMPARE_PARAM(params.ofldq_wr_cred, ofldq_wr_cred);
2400 	COMPARE_PARAM(vres.stag.start, stag_start);
2401 	COMPARE_PARAM(vres.stag.size, stag_size);
2402 	COMPARE_PARAM(vres.rq.start, rq_start);
2403 	COMPARE_PARAM(vres.rq.size, rq_size);
2404 	COMPARE_PARAM(vres.pbl.start, pbl_start);
2405 	COMPARE_PARAM(vres.pbl.size, pbl_size);
2406 	COMPARE_PARAM(vres.qp.start, qp_start);
2407 	COMPARE_PARAM(vres.qp.size, qp_size);
2408 	COMPARE_PARAM(vres.cq.start, cq_start);
2409 	COMPARE_PARAM(vres.cq.size, cq_size);
2410 	COMPARE_PARAM(vres.ocq.start, ocq_start);
2411 	COMPARE_PARAM(vres.ocq.size, ocq_size);
2412 	COMPARE_PARAM(vres.srq.start, srq_start);
2413 	COMPARE_PARAM(vres.srq.size, srq_size);
2414 	COMPARE_PARAM(params.max_ordird_qp, max_ordird_qp);
2415 	COMPARE_PARAM(params.max_ird_adapter, max_ird_adapter);
2416 	COMPARE_PARAM(vres.iscsi.start, iscsi_start);
2417 	COMPARE_PARAM(vres.iscsi.size, iscsi_size);
2418 	COMPARE_PARAM(vres.key.start, key_start);
2419 	COMPARE_PARAM(vres.key.size, key_size);
2420 #undef COMPARE_PARAM
2421 
2422 	return (rc);
2423 }
2424 
2425 static int
restart_lld(struct adapter * sc)2426 restart_lld(struct adapter *sc)
2427 {
2428 	struct adapter_pre_reset_state *old_state = NULL;
2429 	struct port_info *pi;
2430 	struct vi_info *vi;
2431 	if_t ifp;
2432 	struct sge_txq *txq;
2433 	int rc, i, j, k;
2434 
2435 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4rlld");
2436 	if (rc != 0)
2437 		return (ENXIO);
2438 
2439 	/* Restore memory window. */
2440 	setup_memwin(sc);
2441 
2442 	/* Go no further if recovery mode has been requested. */
2443 	if (TUNABLE_INT_FETCH("hw.cxgbe.sos", &i) && i != 0) {
2444 		CH_ALERT(sc, "%s: recovery mode during restart.\n", __func__);
2445 		rc = 0;
2446 		set_adapter_hwstatus(sc, true);
2447 		goto done;
2448 	}
2449 
2450 	old_state = malloc(sizeof(*old_state), M_CXGBE, M_ZERO | M_WAITOK);
2451 	save_caps_and_params(sc, old_state);
2452 
2453 	/* Reestablish contact with firmware and become the primary PF. */
2454 	rc = contact_firmware(sc);
2455 	if (rc != 0)
2456 		goto done; /* error message displayed already */
2457 	MPASS(sc->flags & FW_OK);
2458 
2459 	if (sc->flags & MASTER_PF) {
2460 		rc = partition_resources(sc);
2461 		if (rc != 0)
2462 			goto done; /* error message displayed already */
2463 	}
2464 
2465 	rc = get_params__post_init(sc);
2466 	if (rc != 0)
2467 		goto done; /* error message displayed already */
2468 
2469 	rc = set_params__post_init(sc);
2470 	if (rc != 0)
2471 		goto done; /* error message displayed already */
2472 
2473 	rc = compare_caps_and_params(sc, old_state);
2474 	if (rc != 0)
2475 		goto done; /* error message displayed already */
2476 
2477 	for_each_port(sc, i) {
2478 		pi = sc->port[i];
2479 		MPASS(pi != NULL);
2480 		MPASS(pi->vi != NULL);
2481 		MPASS(pi->vi[0].dev == pi->dev);
2482 
2483 		rc = -t4_port_init(sc, sc->mbox, sc->pf, 0, i);
2484 		if (rc != 0) {
2485 			CH_ERR(sc,
2486 			    "failed to re-initialize port %d: %d\n", i, rc);
2487 			goto done;
2488 		}
2489 		MPASS(sc->chan_map[pi->tx_chan] == i);
2490 
2491 		PORT_LOCK(pi);
2492 		fixup_link_config(pi);
2493 		build_medialist(pi);
2494 		PORT_UNLOCK(pi);
2495 		for_each_vi(pi, j, vi) {
2496 			if (IS_MAIN_VI(vi))
2497 				continue;
2498 			rc = alloc_extra_vi(sc, pi, vi);
2499 			if (rc != 0) {
2500 				CH_ERR(vi,
2501 				    "failed to re-allocate extra VI: %d\n", rc);
2502 				goto done;
2503 			}
2504 		}
2505 	}
2506 
2507 	/*
2508 	 * Interrupts and queues are about to be enabled and other threads will
2509 	 * want to access the hardware too.  It is safe to do so.  Note that
2510 	 * this thread is still in the middle of a synchronized_op.
2511 	 */
2512 	set_adapter_hwstatus(sc, true);
2513 
2514 	if (sc->flags & FULL_INIT_DONE) {
2515 		rc = adapter_full_init(sc);
2516 		if (rc != 0) {
2517 			CH_ERR(sc, "failed to re-initialize adapter: %d\n", rc);
2518 			goto done;
2519 		}
2520 
2521 		if (sc->vxlan_refcount > 0)
2522 			enable_vxlan_rx(sc);
2523 
2524 		for_each_port(sc, i) {
2525 			pi = sc->port[i];
2526 			for_each_vi(pi, j, vi) {
2527 				mtx_lock(&vi->tick_mtx);
2528 				vi->flags &= ~VI_SKIP_STATS;
2529 				mtx_unlock(&vi->tick_mtx);
2530 				if (!(vi->flags & VI_INIT_DONE))
2531 					continue;
2532 				rc = vi_full_init(vi);
2533 				if (rc != 0) {
2534 					CH_ERR(vi, "failed to re-initialize "
2535 					    "interface: %d\n", rc);
2536 					goto done;
2537 				}
2538 				if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
2539 					sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
2540 					t4_set_trace_rss_control(sc, pi->tx_chan, sc->traceq);
2541 					pi->flags |= HAS_TRACEQ;
2542 				}
2543 
2544 				ifp = vi->ifp;
2545 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2546 					continue;
2547 				/*
2548 				 * Note that we do not setup multicast addresses
2549 				 * in the first pass.  This ensures that the
2550 				 * unicast DMACs for all VIs on all ports get an
2551 				 * MPS TCAM entry.
2552 				 */
2553 				rc = update_mac_settings(ifp, XGMAC_ALL &
2554 				    ~XGMAC_MCADDRS);
2555 				if (rc != 0) {
2556 					CH_ERR(vi, "failed to re-configure MAC: %d\n", rc);
2557 					goto done;
2558 				}
2559 				rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true,
2560 				    true);
2561 				if (rc != 0) {
2562 					CH_ERR(vi, "failed to re-enable VI: %d\n", rc);
2563 					goto done;
2564 				}
2565 				for_each_txq(vi, k, txq) {
2566 					TXQ_LOCK(txq);
2567 					txq->eq.flags |= EQ_ENABLED;
2568 					TXQ_UNLOCK(txq);
2569 				}
2570 				mtx_lock(&vi->tick_mtx);
2571 				callout_schedule(&vi->tick, hz);
2572 				mtx_unlock(&vi->tick_mtx);
2573 			}
2574 			PORT_LOCK(pi);
2575 			if (pi->up_vis > 0) {
2576 				t4_update_port_info(pi);
2577 				fixup_link_config(pi);
2578 				build_medialist(pi);
2579 				apply_link_config(pi);
2580 				if (pi->link_cfg.link_ok)
2581 					t4_os_link_changed(pi);
2582 			}
2583 			PORT_UNLOCK(pi);
2584 		}
2585 
2586 		/* Now reprogram the L2 multicast addresses. */
2587 		for_each_port(sc, i) {
2588 			pi = sc->port[i];
2589 			for_each_vi(pi, j, vi) {
2590 				if (!(vi->flags & VI_INIT_DONE))
2591 					continue;
2592 				ifp = vi->ifp;
2593 				if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
2594 					continue;
2595 				rc = update_mac_settings(ifp, XGMAC_MCADDRS);
2596 				if (rc != 0) {
2597 					CH_ERR(vi, "failed to re-configure MCAST MACs: %d\n", rc);
2598 					rc = 0;	/* carry on */
2599 				}
2600 			}
2601 		}
2602 	}
2603 
2604 	/* Reset all calibration */
2605 	t4_calibration_start(sc);
2606 done:
2607 	end_synchronized_op(sc, 0);
2608 	free(old_state, M_CXGBE);
2609 
2610 	restart_atid_allocator(sc);
2611 	t4_restart_l2t(sc);
2612 
2613 	return (rc);
2614 }
2615 
2616 int
resume_adapter(struct adapter * sc)2617 resume_adapter(struct adapter *sc)
2618 {
2619 	restart_adapter(sc);
2620 	restart_lld(sc);
2621 #ifdef TCP_OFFLOAD
2622 	restart_all_uld(sc);
2623 #endif
2624 	return (0);
2625 }
2626 
2627 static int
t4_resume(device_t dev)2628 t4_resume(device_t dev)
2629 {
2630 	struct adapter *sc = device_get_softc(dev);
2631 	int rc;
2632 
2633 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2634 	rc = resume_adapter(sc);
2635 	CH_ALERT(sc, "%s end (thread %p).\n", __func__, curthread);
2636 
2637 	return (rc);
2638 }
2639 
2640 static int
t4_reset_prepare(device_t dev,device_t child)2641 t4_reset_prepare(device_t dev, device_t child)
2642 {
2643 	struct adapter *sc = device_get_softc(dev);
2644 
2645 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2646 	return (0);
2647 }
2648 
2649 static int
t4_reset_post(device_t dev,device_t child)2650 t4_reset_post(device_t dev, device_t child)
2651 {
2652 	struct adapter *sc = device_get_softc(dev);
2653 
2654 	CH_ALERT(sc, "%s from thread %p.\n", __func__, curthread);
2655 	return (0);
2656 }
2657 
2658 static int
reset_adapter_with_pl_rst(struct adapter * sc)2659 reset_adapter_with_pl_rst(struct adapter *sc)
2660 {
2661 	/* This is a t4_write_reg without the hw_off_limits check. */
2662 	MPASS(sc->error_flags & HW_OFF_LIMITS);
2663 	bus_space_write_4(sc->bt, sc->bh, A_PL_RST,
2664 			  F_PIORSTMODE | F_PIORST | F_AUTOPCIEPAUSE);
2665 	pause("pl_rst", 1 * hz);		/* Wait 1s for reset */
2666 	return (0);
2667 }
2668 
2669 static int
reset_adapter_with_pcie_sbr(struct adapter * sc)2670 reset_adapter_with_pcie_sbr(struct adapter *sc)
2671 {
2672 	device_t pdev = device_get_parent(sc->dev);
2673 	device_t gpdev = device_get_parent(pdev);
2674 	device_t *children;
2675 	int rc, i, lcap, lsta, nchildren;
2676 	uint32_t v;
2677 
2678 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2679 	if (rc != 0) {
2680 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2681 		    device_get_nameunit(gpdev), rc);
2682 		return (ENOTSUP);
2683 	}
2684 	lcap = v + PCIER_LINK_CAP;
2685 	lsta = v + PCIER_LINK_STA;
2686 
2687 	nchildren = 0;
2688 	device_get_children(pdev, &children, &nchildren);
2689 	for (i = 0; i < nchildren; i++)
2690 		pci_save_state(children[i]);
2691 	v = pci_read_config(gpdev, PCIR_BRIDGECTL_1, 2);
2692 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v | PCIB_BCR_SECBUS_RESET, 2);
2693 	pause("pcie_sbr1", hz / 10);	/* 100ms */
2694 	pci_write_config(gpdev, PCIR_BRIDGECTL_1, v, 2);
2695 	pause("pcie_sbr2", hz);		/* Wait 1s before restore_state. */
2696 	v = pci_read_config(gpdev, lsta, 2);
2697 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2698 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2699 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2700 		rc = ETIMEDOUT;
2701 	else
2702 		rc = 0;
2703 	if (rc != 0)
2704 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2705 		    __func__, v);
2706 	else {
2707 		for (i = 0; i < nchildren; i++)
2708 			pci_restore_state(children[i]);
2709 	}
2710 	free(children, M_TEMP);
2711 
2712 	return (rc);
2713 }
2714 
2715 static int
reset_adapter_with_pcie_link_bounce(struct adapter * sc)2716 reset_adapter_with_pcie_link_bounce(struct adapter *sc)
2717 {
2718 	device_t pdev = device_get_parent(sc->dev);
2719 	device_t gpdev = device_get_parent(pdev);
2720 	device_t *children;
2721 	int rc, i, lcap, lctl, lsta, nchildren;
2722 	uint32_t v;
2723 
2724 	rc = pci_find_cap(gpdev, PCIY_EXPRESS, &v);
2725 	if (rc != 0) {
2726 		CH_ERR(sc, "%s: pci_find_cap(%s, pcie) failed: %d\n", __func__,
2727 		    device_get_nameunit(gpdev), rc);
2728 		return (ENOTSUP);
2729 	}
2730 	lcap = v + PCIER_LINK_CAP;
2731 	lctl = v + PCIER_LINK_CTL;
2732 	lsta = v + PCIER_LINK_STA;
2733 
2734 	nchildren = 0;
2735 	device_get_children(pdev, &children, &nchildren);
2736 	for (i = 0; i < nchildren; i++)
2737 		pci_save_state(children[i]);
2738 	v = pci_read_config(gpdev, lctl, 2);
2739 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_LINK_DIS, 2);
2740 	pause("pcie_lnk1", 100 * hz / 1000);	/* 100ms */
2741 	pci_write_config(gpdev, lctl, v | PCIEM_LINK_CTL_RETRAIN_LINK, 2);
2742 	pause("pcie_lnk2", hz);		/* Wait 1s before restore_state. */
2743 	v = pci_read_config(gpdev, lsta, 2);
2744 	if (pci_read_config(gpdev, lcap, 2) & PCIEM_LINK_CAP_DL_ACTIVE)
2745 		rc = v & PCIEM_LINK_STA_DL_ACTIVE ? 0 : ETIMEDOUT;
2746 	else if (v & (PCIEM_LINK_STA_TRAINING_ERROR | PCIEM_LINK_STA_TRAINING))
2747 		rc = ETIMEDOUT;
2748 	else
2749 		rc = 0;
2750 	if (rc != 0)
2751 		CH_ERR(sc, "%s: PCIe link is down after reset, LINK_STA 0x%x\n",
2752 		    __func__, v);
2753 	else {
2754 		for (i = 0; i < nchildren; i++)
2755 			pci_restore_state(children[i]);
2756 	}
2757 	free(children, M_TEMP);
2758 
2759 	return (rc);
2760 }
2761 
2762 static inline int
reset_adapter(struct adapter * sc)2763 reset_adapter(struct adapter *sc)
2764 {
2765 	int rc;
2766 	const int reset_method = vm_guest == VM_GUEST_NO ? t4_reset_method : 0;
2767 
2768 	rc = suspend_adapter(sc);
2769 	if (rc != 0)
2770 		return (rc);
2771 
2772 	switch (reset_method) {
2773 	case 1:
2774 		rc = reset_adapter_with_pcie_sbr(sc);
2775 		break;
2776 	case 2:
2777 		rc = reset_adapter_with_pcie_link_bounce(sc);
2778 		break;
2779 	case 0:
2780 	default:
2781 		rc = reset_adapter_with_pl_rst(sc);
2782 		break;
2783 	}
2784 	if (rc == 0)
2785 		rc = resume_adapter(sc);
2786 	return (rc);
2787 }
2788 
2789 static void
reset_adapter_task(void * arg,int pending)2790 reset_adapter_task(void *arg, int pending)
2791 {
2792 	struct adapter *sc = arg;
2793 	const int flags = sc->flags;
2794 	const int eflags = sc->error_flags;
2795 	int rc;
2796 
2797 	if (pending > 1)
2798 		CH_ALERT(sc, "%s: pending %d\n", __func__, pending);
2799 	rc = reset_adapter(sc);
2800 	if (rc != 0) {
2801 		CH_ERR(sc, "adapter did not reset properly, rc = %d, "
2802 		       "flags 0x%08x -> 0x%08x, err_flags 0x%08x -> 0x%08x.\n",
2803 		       rc, flags, sc->flags, eflags, sc->error_flags);
2804 	}
2805 }
2806 
2807 static int
cxgbe_probe(device_t dev)2808 cxgbe_probe(device_t dev)
2809 {
2810 	struct port_info *pi = device_get_softc(dev);
2811 
2812 	device_set_descf(dev, "port %d", pi->port_id);
2813 
2814 	return (BUS_PROBE_DEFAULT);
2815 }
2816 
2817 #define T4_CAP (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_HWCSUM | \
2818     IFCAP_VLAN_HWCSUM | IFCAP_TSO | IFCAP_JUMBO_MTU | IFCAP_LRO | \
2819     IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWCSUM_IPV6 | IFCAP_HWSTATS | \
2820     IFCAP_HWRXTSTMP | IFCAP_MEXTPG)
2821 #define T4_CAP_ENABLE (T4_CAP)
2822 
2823 static void
cxgbe_vi_attach(device_t dev,struct vi_info * vi)2824 cxgbe_vi_attach(device_t dev, struct vi_info *vi)
2825 {
2826 	if_t ifp;
2827 	struct sbuf *sb;
2828 	struct sysctl_ctx_list *ctx = &vi->ctx;
2829 	struct sysctl_oid_list *children;
2830 	struct pfil_head_args pa;
2831 	struct adapter *sc = vi->adapter;
2832 
2833 	sysctl_ctx_init(ctx);
2834 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(vi->dev));
2835 	vi->rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rxq",
2836 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC rx queues");
2837 	vi->txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "txq",
2838 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NIC tx queues");
2839 #ifdef DEV_NETMAP
2840 	vi->nm_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_rxq",
2841 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap rx queues");
2842 	vi->nm_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "nm_txq",
2843 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "netmap tx queues");
2844 #endif
2845 #ifdef TCP_OFFLOAD
2846 	vi->ofld_rxq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_rxq",
2847 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE rx queues");
2848 #endif
2849 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2850 	vi->ofld_txq_oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "ofld_txq",
2851 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE/ETHOFLD tx queues");
2852 #endif
2853 
2854 	vi->xact_addr_filt = -1;
2855 	mtx_init(&vi->tick_mtx, "vi tick", NULL, MTX_DEF);
2856 	callout_init_mtx(&vi->tick, &vi->tick_mtx, 0);
2857 	if (sc->flags & IS_VF || t4_tx_vm_wr != 0)
2858 		vi->flags |= TX_USES_VM_WR;
2859 
2860 	/* Allocate an ifnet and set it up */
2861 	ifp = if_alloc_dev(IFT_ETHER, dev);
2862 	vi->ifp = ifp;
2863 	if_setsoftc(ifp, vi);
2864 
2865 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2866 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2867 
2868 	if_setinitfn(ifp, cxgbe_init);
2869 	if_setioctlfn(ifp, cxgbe_ioctl);
2870 	if_settransmitfn(ifp, cxgbe_transmit);
2871 	if_setqflushfn(ifp, cxgbe_qflush);
2872 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
2873 		if_setgetcounterfn(ifp, vi_get_counter);
2874 	else
2875 		if_setgetcounterfn(ifp, cxgbe_get_counter);
2876 #if defined(KERN_TLS) || defined(RATELIMIT)
2877 	if_setsndtagallocfn(ifp, cxgbe_snd_tag_alloc);
2878 #endif
2879 #ifdef RATELIMIT
2880 	if_setratelimitqueryfn(ifp, cxgbe_ratelimit_query);
2881 #endif
2882 
2883 	if_setcapabilities(ifp, T4_CAP);
2884 	if_setcapenable(ifp, T4_CAP_ENABLE);
2885 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
2886 	    CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2887 	if (chip_id(sc) >= CHELSIO_T6) {
2888 		if_setcapabilitiesbit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2889 		if_setcapenablebit(ifp, IFCAP_VXLAN_HWCSUM | IFCAP_VXLAN_HWTSO, 0);
2890 		if_sethwassistbits(ifp, CSUM_INNER_IP6_UDP | CSUM_INNER_IP6_TCP |
2891 		    CSUM_INNER_IP6_TSO | CSUM_INNER_IP | CSUM_INNER_IP_UDP |
2892 		    CSUM_INNER_IP_TCP | CSUM_INNER_IP_TSO | CSUM_ENCAP_VXLAN, 0);
2893 	}
2894 
2895 #ifdef TCP_OFFLOAD
2896 	if (vi->nofldrxq != 0)
2897 		if_setcapabilitiesbit(ifp, IFCAP_TOE, 0);
2898 #endif
2899 #ifdef RATELIMIT
2900 	if (is_ethoffload(sc) && vi->nofldtxq != 0) {
2901 		if_setcapabilitiesbit(ifp, IFCAP_TXRTLMT, 0);
2902 		if_setcapenablebit(ifp, IFCAP_TXRTLMT, 0);
2903 	}
2904 #endif
2905 
2906 	if_sethwtsomax(ifp, IP_MAXPACKET);
2907 	if (vi->flags & TX_USES_VM_WR)
2908 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_VM_TSO);
2909 	else
2910 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_TSO);
2911 #ifdef RATELIMIT
2912 	if (is_ethoffload(sc) && vi->nofldtxq != 0)
2913 		if_sethwtsomaxsegcount(ifp, TX_SGL_SEGS_EO_TSO);
2914 #endif
2915 	if_sethwtsomaxsegsize(ifp, 65536);
2916 #ifdef KERN_TLS
2917 	if (is_ktls(sc)) {
2918 		if_setcapabilitiesbit(ifp, IFCAP_TXTLS, 0);
2919 		if (sc->flags & KERN_TLS_ON || !is_t6(sc))
2920 			if_setcapenablebit(ifp, IFCAP_TXTLS, 0);
2921 	}
2922 #endif
2923 
2924 	ether_ifattach(ifp, vi->hw_addr);
2925 #ifdef DEV_NETMAP
2926 	if (vi->nnmrxq != 0)
2927 		cxgbe_nm_attach(vi);
2928 #endif
2929 	sb = sbuf_new_auto();
2930 	sbuf_printf(sb, "%d txq, %d rxq (NIC)", vi->ntxq, vi->nrxq);
2931 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
2932 	switch (if_getcapabilities(ifp) & (IFCAP_TOE | IFCAP_TXRTLMT)) {
2933 	case IFCAP_TOE:
2934 		sbuf_printf(sb, "; %d txq (TOE)", vi->nofldtxq);
2935 		break;
2936 	case IFCAP_TOE | IFCAP_TXRTLMT:
2937 		sbuf_printf(sb, "; %d txq (TOE/ETHOFLD)", vi->nofldtxq);
2938 		break;
2939 	case IFCAP_TXRTLMT:
2940 		sbuf_printf(sb, "; %d txq (ETHOFLD)", vi->nofldtxq);
2941 		break;
2942 	}
2943 #endif
2944 #ifdef TCP_OFFLOAD
2945 	if (if_getcapabilities(ifp) & IFCAP_TOE)
2946 		sbuf_printf(sb, ", %d rxq (TOE)", vi->nofldrxq);
2947 #endif
2948 #ifdef DEV_NETMAP
2949 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
2950 		sbuf_printf(sb, "; %d txq, %d rxq (netmap)",
2951 		    vi->nnmtxq, vi->nnmrxq);
2952 #endif
2953 	sbuf_finish(sb);
2954 	device_printf(dev, "%s\n", sbuf_data(sb));
2955 	sbuf_delete(sb);
2956 
2957 	vi_sysctls(vi);
2958 
2959 	pa.pa_version = PFIL_VERSION;
2960 	pa.pa_flags = PFIL_IN;
2961 	pa.pa_type = PFIL_TYPE_ETHERNET;
2962 	pa.pa_headname = if_name(ifp);
2963 	vi->pfil = pfil_head_register(&pa);
2964 }
2965 
2966 static int
cxgbe_attach(device_t dev)2967 cxgbe_attach(device_t dev)
2968 {
2969 	struct port_info *pi = device_get_softc(dev);
2970 	struct adapter *sc = pi->adapter;
2971 	struct vi_info *vi;
2972 	int i;
2973 
2974 	sysctl_ctx_init(&pi->ctx);
2975 
2976 	cxgbe_vi_attach(dev, &pi->vi[0]);
2977 
2978 	for_each_vi(pi, i, vi) {
2979 		if (i == 0)
2980 			continue;
2981 		vi->dev = device_add_child(dev, sc->names->vi_ifnet_name, DEVICE_UNIT_ANY);
2982 		if (vi->dev == NULL) {
2983 			device_printf(dev, "failed to add VI %d\n", i);
2984 			continue;
2985 		}
2986 		device_set_softc(vi->dev, vi);
2987 	}
2988 
2989 	cxgbe_sysctls(pi);
2990 
2991 	bus_attach_children(dev);
2992 
2993 	return (0);
2994 }
2995 
2996 static void
cxgbe_vi_detach(struct vi_info * vi)2997 cxgbe_vi_detach(struct vi_info *vi)
2998 {
2999 	if_t ifp = vi->ifp;
3000 
3001 	if (vi->pfil != NULL) {
3002 		pfil_head_unregister(vi->pfil);
3003 		vi->pfil = NULL;
3004 	}
3005 
3006 	ether_ifdetach(ifp);
3007 
3008 	/* Let detach proceed even if these fail. */
3009 #ifdef DEV_NETMAP
3010 	if (if_getcapabilities(ifp) & IFCAP_NETMAP)
3011 		cxgbe_nm_detach(vi);
3012 #endif
3013 	cxgbe_uninit_synchronized(vi);
3014 	callout_drain(&vi->tick);
3015 	mtx_destroy(&vi->tick_mtx);
3016 	sysctl_ctx_free(&vi->ctx);
3017 	vi_full_uninit(vi);
3018 
3019 	if_free(vi->ifp);
3020 	vi->ifp = NULL;
3021 }
3022 
3023 static int
cxgbe_detach(device_t dev)3024 cxgbe_detach(device_t dev)
3025 {
3026 	struct port_info *pi = device_get_softc(dev);
3027 	struct adapter *sc = pi->adapter;
3028 	int rc;
3029 
3030 	/* Detach the extra VIs first. */
3031 	rc = bus_generic_detach(dev);
3032 	if (rc)
3033 		return (rc);
3034 
3035 	sysctl_ctx_free(&pi->ctx);
3036 	begin_vi_detach(sc, &pi->vi[0]);
3037 	if (pi->flags & HAS_TRACEQ) {
3038 		sc->traceq = -1;	/* cloner should not create ifnet */
3039 		t4_tracer_port_detach(sc);
3040 	}
3041 	cxgbe_vi_detach(&pi->vi[0]);
3042 	ifmedia_removeall(&pi->media);
3043 	end_vi_detach(sc, &pi->vi[0]);
3044 
3045 	return (0);
3046 }
3047 
3048 static void
cxgbe_init(void * arg)3049 cxgbe_init(void *arg)
3050 {
3051 	struct vi_info *vi = arg;
3052 	struct adapter *sc = vi->adapter;
3053 
3054 	if (begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4init") != 0)
3055 		return;
3056 	cxgbe_init_synchronized(vi);
3057 	end_synchronized_op(sc, 0);
3058 }
3059 
3060 static int
cxgbe_ioctl(if_t ifp,unsigned long cmd,caddr_t data)3061 cxgbe_ioctl(if_t ifp, unsigned long cmd, caddr_t data)
3062 {
3063 	int rc = 0, mtu, flags;
3064 	struct vi_info *vi = if_getsoftc(ifp);
3065 	struct port_info *pi = vi->pi;
3066 	struct adapter *sc = pi->adapter;
3067 	struct ifreq *ifr = (struct ifreq *)data;
3068 	uint32_t mask;
3069 
3070 	switch (cmd) {
3071 	case SIOCSIFMTU:
3072 		mtu = ifr->ifr_mtu;
3073 		if (mtu < ETHERMIN || mtu > MAX_MTU)
3074 			return (EINVAL);
3075 
3076 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4mtu");
3077 		if (rc)
3078 			return (rc);
3079 		if_setmtu(ifp, mtu);
3080 		if (vi->flags & VI_INIT_DONE) {
3081 			t4_update_fl_bufsize(ifp);
3082 			if (hw_all_ok(sc) &&
3083 			    if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3084 				rc = update_mac_settings(ifp, XGMAC_MTU);
3085 		}
3086 		end_synchronized_op(sc, 0);
3087 		break;
3088 
3089 	case SIOCSIFFLAGS:
3090 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4flg");
3091 		if (rc)
3092 			return (rc);
3093 
3094 		if (!hw_all_ok(sc)) {
3095 			rc = ENXIO;
3096 			goto fail;
3097 		}
3098 
3099 		if (if_getflags(ifp) & IFF_UP) {
3100 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3101 				flags = vi->if_flags;
3102 				if ((if_getflags(ifp) ^ flags) &
3103 				    (IFF_PROMISC | IFF_ALLMULTI)) {
3104 					rc = update_mac_settings(ifp,
3105 					    XGMAC_PROMISC | XGMAC_ALLMULTI);
3106 				}
3107 			} else {
3108 				rc = cxgbe_init_synchronized(vi);
3109 			}
3110 			vi->if_flags = if_getflags(ifp);
3111 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3112 			rc = cxgbe_uninit_synchronized(vi);
3113 		}
3114 		end_synchronized_op(sc, 0);
3115 		break;
3116 
3117 	case SIOCADDMULTI:
3118 	case SIOCDELMULTI:
3119 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4multi");
3120 		if (rc)
3121 			return (rc);
3122 		if (hw_all_ok(sc) && if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3123 			rc = update_mac_settings(ifp, XGMAC_MCADDRS);
3124 		end_synchronized_op(sc, 0);
3125 		break;
3126 
3127 	case SIOCSIFCAP:
3128 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4cap");
3129 		if (rc)
3130 			return (rc);
3131 
3132 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3133 		if (mask & IFCAP_TXCSUM) {
3134 			if_togglecapenable(ifp, IFCAP_TXCSUM);
3135 			if_togglehwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP);
3136 
3137 			if (IFCAP_TSO4 & if_getcapenable(ifp) &&
3138 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3139 				mask &= ~IFCAP_TSO4;
3140 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
3141 				if_printf(ifp,
3142 				    "tso4 disabled due to -txcsum.\n");
3143 			}
3144 		}
3145 		if (mask & IFCAP_TXCSUM_IPV6) {
3146 			if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
3147 			if_togglehwassist(ifp, CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3148 
3149 			if (IFCAP_TSO6 & if_getcapenable(ifp) &&
3150 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3151 				mask &= ~IFCAP_TSO6;
3152 				if_setcapenablebit(ifp, 0, IFCAP_TSO6);
3153 				if_printf(ifp,
3154 				    "tso6 disabled due to -txcsum6.\n");
3155 			}
3156 		}
3157 		if (mask & IFCAP_RXCSUM)
3158 			if_togglecapenable(ifp, IFCAP_RXCSUM);
3159 		if (mask & IFCAP_RXCSUM_IPV6)
3160 			if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
3161 
3162 		/*
3163 		 * Note that we leave CSUM_TSO alone (it is always set).  The
3164 		 * kernel takes both IFCAP_TSOx and CSUM_TSO into account before
3165 		 * sending a TSO request our way, so it's sufficient to toggle
3166 		 * IFCAP_TSOx only.
3167 		 */
3168 		if (mask & IFCAP_TSO4) {
3169 			if (!(IFCAP_TSO4 & if_getcapenable(ifp)) &&
3170 			    !(IFCAP_TXCSUM & if_getcapenable(ifp))) {
3171 				if_printf(ifp, "enable txcsum first.\n");
3172 				rc = EAGAIN;
3173 				goto fail;
3174 			}
3175 			if_togglecapenable(ifp, IFCAP_TSO4);
3176 		}
3177 		if (mask & IFCAP_TSO6) {
3178 			if (!(IFCAP_TSO6 & if_getcapenable(ifp)) &&
3179 			    !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) {
3180 				if_printf(ifp, "enable txcsum6 first.\n");
3181 				rc = EAGAIN;
3182 				goto fail;
3183 			}
3184 			if_togglecapenable(ifp, IFCAP_TSO6);
3185 		}
3186 		if (mask & IFCAP_LRO) {
3187 #if defined(INET) || defined(INET6)
3188 			int i;
3189 			struct sge_rxq *rxq;
3190 
3191 			if_togglecapenable(ifp, IFCAP_LRO);
3192 			for_each_rxq(vi, i, rxq) {
3193 				if (if_getcapenable(ifp) & IFCAP_LRO)
3194 					rxq->iq.flags |= IQ_LRO_ENABLED;
3195 				else
3196 					rxq->iq.flags &= ~IQ_LRO_ENABLED;
3197 			}
3198 #endif
3199 		}
3200 #ifdef TCP_OFFLOAD
3201 		if (mask & IFCAP_TOE) {
3202 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TOE;
3203 
3204 			rc = toe_capability(vi, enable);
3205 			if (rc != 0)
3206 				goto fail;
3207 
3208 			if_togglecapenable(ifp, mask);
3209 		}
3210 #endif
3211 		if (mask & IFCAP_VLAN_HWTAGGING) {
3212 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3213 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3214 				rc = update_mac_settings(ifp, XGMAC_VLANEX);
3215 		}
3216 		if (mask & IFCAP_VLAN_MTU) {
3217 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
3218 
3219 			/* Need to find out how to disable auto-mtu-inflation */
3220 		}
3221 		if (mask & IFCAP_VLAN_HWTSO)
3222 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
3223 		if (mask & IFCAP_VLAN_HWCSUM)
3224 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3225 #ifdef RATELIMIT
3226 		if (mask & IFCAP_TXRTLMT)
3227 			if_togglecapenable(ifp, IFCAP_TXRTLMT);
3228 #endif
3229 		if (mask & IFCAP_HWRXTSTMP) {
3230 			int i;
3231 			struct sge_rxq *rxq;
3232 
3233 			if_togglecapenable(ifp, IFCAP_HWRXTSTMP);
3234 			for_each_rxq(vi, i, rxq) {
3235 				if (if_getcapenable(ifp) & IFCAP_HWRXTSTMP)
3236 					rxq->iq.flags |= IQ_RX_TIMESTAMP;
3237 				else
3238 					rxq->iq.flags &= ~IQ_RX_TIMESTAMP;
3239 			}
3240 		}
3241 		if (mask & IFCAP_MEXTPG)
3242 			if_togglecapenable(ifp, IFCAP_MEXTPG);
3243 
3244 #ifdef KERN_TLS
3245 		if (mask & IFCAP_TXTLS) {
3246 			int enable = (if_getcapenable(ifp) ^ mask) & IFCAP_TXTLS;
3247 
3248 			rc = ktls_capability(sc, enable);
3249 			if (rc != 0)
3250 				goto fail;
3251 
3252 			if_togglecapenable(ifp, mask & IFCAP_TXTLS);
3253 		}
3254 #endif
3255 		if (mask & IFCAP_VXLAN_HWCSUM) {
3256 			if_togglecapenable(ifp, IFCAP_VXLAN_HWCSUM);
3257 			if_togglehwassist(ifp, CSUM_INNER_IP6_UDP |
3258 			    CSUM_INNER_IP6_TCP | CSUM_INNER_IP |
3259 			    CSUM_INNER_IP_UDP | CSUM_INNER_IP_TCP);
3260 		}
3261 		if (mask & IFCAP_VXLAN_HWTSO) {
3262 			if_togglecapenable(ifp, IFCAP_VXLAN_HWTSO);
3263 			if_togglehwassist(ifp, CSUM_INNER_IP6_TSO |
3264 			    CSUM_INNER_IP_TSO);
3265 		}
3266 
3267 #ifdef VLAN_CAPABILITIES
3268 		VLAN_CAPABILITIES(ifp);
3269 #endif
3270 fail:
3271 		end_synchronized_op(sc, 0);
3272 		break;
3273 
3274 	case SIOCSIFMEDIA:
3275 	case SIOCGIFMEDIA:
3276 	case SIOCGIFXMEDIA:
3277 		rc = ifmedia_ioctl(ifp, ifr, &pi->media, cmd);
3278 		break;
3279 
3280 	case SIOCGI2C: {
3281 		struct ifi2creq i2c;
3282 
3283 		rc = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3284 		if (rc != 0)
3285 			break;
3286 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3287 			rc = EPERM;
3288 			break;
3289 		}
3290 		if (i2c.len > sizeof(i2c.data)) {
3291 			rc = EINVAL;
3292 			break;
3293 		}
3294 		rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4i2c");
3295 		if (rc)
3296 			return (rc);
3297 		if (!hw_all_ok(sc))
3298 			rc = ENXIO;
3299 		else
3300 			rc = -t4_i2c_rd(sc, sc->mbox, pi->port_id, i2c.dev_addr,
3301 			    i2c.offset, i2c.len, &i2c.data[0]);
3302 		end_synchronized_op(sc, 0);
3303 		if (rc == 0)
3304 			rc = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3305 		break;
3306 	}
3307 
3308 	default:
3309 		rc = ether_ioctl(ifp, cmd, data);
3310 	}
3311 
3312 	return (rc);
3313 }
3314 
3315 static int
cxgbe_transmit(if_t ifp,struct mbuf * m)3316 cxgbe_transmit(if_t ifp, struct mbuf *m)
3317 {
3318 	struct vi_info *vi = if_getsoftc(ifp);
3319 	struct port_info *pi = vi->pi;
3320 	struct adapter *sc;
3321 	struct sge_txq *txq;
3322 	void *items[1];
3323 	int rc;
3324 
3325 	M_ASSERTPKTHDR(m);
3326 	MPASS(m->m_nextpkt == NULL);	/* not quite ready for this yet */
3327 #if defined(KERN_TLS) || defined(RATELIMIT)
3328 	if (m->m_pkthdr.csum_flags & CSUM_SND_TAG)
3329 		MPASS(m->m_pkthdr.snd_tag->ifp == ifp);
3330 #endif
3331 
3332 	if (__predict_false(pi->link_cfg.link_ok == false)) {
3333 		m_freem(m);
3334 		return (ENETDOWN);
3335 	}
3336 
3337 	rc = parse_pkt(&m, vi->flags & TX_USES_VM_WR);
3338 	if (__predict_false(rc != 0)) {
3339 		if (__predict_true(rc == EINPROGRESS)) {
3340 			/* queued by parse_pkt */
3341 			MPASS(m != NULL);
3342 			return (0);
3343 		}
3344 
3345 		MPASS(m == NULL);			/* was freed already */
3346 		atomic_add_int(&pi->tx_parse_error, 1);	/* rare, atomic is ok */
3347 		return (rc);
3348 	}
3349 
3350 	/* Select a txq. */
3351 	sc = vi->adapter;
3352 	txq = &sc->sge.txq[vi->first_txq];
3353 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
3354 		txq += ((m->m_pkthdr.flowid % (vi->ntxq - vi->rsrv_noflowq)) +
3355 		    vi->rsrv_noflowq);
3356 
3357 	items[0] = m;
3358 	rc = mp_ring_enqueue(txq->r, items, 1, 256);
3359 	if (__predict_false(rc != 0))
3360 		m_freem(m);
3361 
3362 	return (rc);
3363 }
3364 
3365 static void
cxgbe_qflush(if_t ifp)3366 cxgbe_qflush(if_t ifp)
3367 {
3368 	struct vi_info *vi = if_getsoftc(ifp);
3369 	struct sge_txq *txq;
3370 	int i;
3371 
3372 	/* queues do not exist if !VI_INIT_DONE. */
3373 	if (vi->flags & VI_INIT_DONE) {
3374 		for_each_txq(vi, i, txq) {
3375 			TXQ_LOCK(txq);
3376 			txq->eq.flags |= EQ_QFLUSH;
3377 			TXQ_UNLOCK(txq);
3378 			while (!mp_ring_is_idle(txq->r)) {
3379 				mp_ring_check_drainage(txq->r, 4096);
3380 				pause("qflush", 1);
3381 			}
3382 			TXQ_LOCK(txq);
3383 			txq->eq.flags &= ~EQ_QFLUSH;
3384 			TXQ_UNLOCK(txq);
3385 		}
3386 	}
3387 	if_qflush(ifp);
3388 }
3389 
3390 static uint64_t
vi_get_counter(if_t ifp,ift_counter c)3391 vi_get_counter(if_t ifp, ift_counter c)
3392 {
3393 	struct vi_info *vi = if_getsoftc(ifp);
3394 	struct fw_vi_stats_vf *s = &vi->stats;
3395 
3396 	mtx_lock(&vi->tick_mtx);
3397 	vi_refresh_stats(vi);
3398 	mtx_unlock(&vi->tick_mtx);
3399 
3400 	switch (c) {
3401 	case IFCOUNTER_IPACKETS:
3402 		return (s->rx_bcast_frames + s->rx_mcast_frames +
3403 		    s->rx_ucast_frames);
3404 	case IFCOUNTER_IERRORS:
3405 		return (s->rx_err_frames);
3406 	case IFCOUNTER_OPACKETS:
3407 		return (s->tx_bcast_frames + s->tx_mcast_frames +
3408 		    s->tx_ucast_frames + s->tx_offload_frames);
3409 	case IFCOUNTER_OERRORS:
3410 		return (s->tx_drop_frames);
3411 	case IFCOUNTER_IBYTES:
3412 		return (s->rx_bcast_bytes + s->rx_mcast_bytes +
3413 		    s->rx_ucast_bytes);
3414 	case IFCOUNTER_OBYTES:
3415 		return (s->tx_bcast_bytes + s->tx_mcast_bytes +
3416 		    s->tx_ucast_bytes + s->tx_offload_bytes);
3417 	case IFCOUNTER_IMCASTS:
3418 		return (s->rx_mcast_frames);
3419 	case IFCOUNTER_OMCASTS:
3420 		return (s->tx_mcast_frames);
3421 	case IFCOUNTER_OQDROPS: {
3422 		uint64_t drops;
3423 
3424 		drops = 0;
3425 		if (vi->flags & VI_INIT_DONE) {
3426 			int i;
3427 			struct sge_txq *txq;
3428 
3429 			for_each_txq(vi, i, txq)
3430 				drops += counter_u64_fetch(txq->r->dropped);
3431 		}
3432 
3433 		return (drops);
3434 
3435 	}
3436 
3437 	default:
3438 		return (if_get_counter_default(ifp, c));
3439 	}
3440 }
3441 
3442 static uint64_t
cxgbe_get_counter(if_t ifp,ift_counter c)3443 cxgbe_get_counter(if_t ifp, ift_counter c)
3444 {
3445 	struct vi_info *vi = if_getsoftc(ifp);
3446 	struct port_info *pi = vi->pi;
3447 	struct port_stats *s = &pi->stats;
3448 
3449 	mtx_lock(&vi->tick_mtx);
3450 	cxgbe_refresh_stats(vi);
3451 	mtx_unlock(&vi->tick_mtx);
3452 
3453 	switch (c) {
3454 	case IFCOUNTER_IPACKETS:
3455 		return (s->rx_frames);
3456 
3457 	case IFCOUNTER_IERRORS:
3458 		return (s->rx_jabber + s->rx_runt + s->rx_too_long +
3459 		    s->rx_fcs_err + s->rx_len_err);
3460 
3461 	case IFCOUNTER_OPACKETS:
3462 		return (s->tx_frames);
3463 
3464 	case IFCOUNTER_OERRORS:
3465 		return (s->tx_error_frames);
3466 
3467 	case IFCOUNTER_IBYTES:
3468 		return (s->rx_octets);
3469 
3470 	case IFCOUNTER_OBYTES:
3471 		return (s->tx_octets);
3472 
3473 	case IFCOUNTER_IMCASTS:
3474 		return (s->rx_mcast_frames);
3475 
3476 	case IFCOUNTER_OMCASTS:
3477 		return (s->tx_mcast_frames);
3478 
3479 	case IFCOUNTER_IQDROPS:
3480 		return (s->rx_ovflow0 + s->rx_ovflow1 + s->rx_ovflow2 +
3481 		    s->rx_ovflow3 + s->rx_trunc0 + s->rx_trunc1 + s->rx_trunc2 +
3482 		    s->rx_trunc3 + pi->tnl_cong_drops);
3483 
3484 	case IFCOUNTER_OQDROPS: {
3485 		uint64_t drops;
3486 
3487 		drops = s->tx_drop;
3488 		if (vi->flags & VI_INIT_DONE) {
3489 			int i;
3490 			struct sge_txq *txq;
3491 
3492 			for_each_txq(vi, i, txq)
3493 				drops += counter_u64_fetch(txq->r->dropped);
3494 		}
3495 
3496 		return (drops);
3497 
3498 	}
3499 
3500 	default:
3501 		return (if_get_counter_default(ifp, c));
3502 	}
3503 }
3504 
3505 #if defined(KERN_TLS) || defined(RATELIMIT)
3506 static int
cxgbe_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** pt)3507 cxgbe_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
3508     struct m_snd_tag **pt)
3509 {
3510 	int error;
3511 
3512 	switch (params->hdr.type) {
3513 #ifdef RATELIMIT
3514 	case IF_SND_TAG_TYPE_RATE_LIMIT:
3515 		error = cxgbe_rate_tag_alloc(ifp, params, pt);
3516 		break;
3517 #endif
3518 #ifdef KERN_TLS
3519 	case IF_SND_TAG_TYPE_TLS:
3520 	{
3521 		struct vi_info *vi = if_getsoftc(ifp);
3522 
3523 		if (is_t6(vi->pi->adapter))
3524 			error = t6_tls_tag_alloc(ifp, params, pt);
3525 		else
3526 			error = t7_tls_tag_alloc(ifp, params, pt);
3527 		break;
3528 	}
3529 #endif
3530 	default:
3531 		error = EOPNOTSUPP;
3532 	}
3533 	return (error);
3534 }
3535 #endif
3536 
3537 /*
3538  * The kernel picks a media from the list we had provided but we still validate
3539  * the requeste.
3540  */
3541 int
cxgbe_media_change(if_t ifp)3542 cxgbe_media_change(if_t ifp)
3543 {
3544 	struct vi_info *vi = if_getsoftc(ifp);
3545 	struct port_info *pi = vi->pi;
3546 	struct ifmedia *ifm = &pi->media;
3547 	struct link_config *lc = &pi->link_cfg;
3548 	struct adapter *sc = pi->adapter;
3549 	int rc;
3550 
3551 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mec");
3552 	if (rc != 0)
3553 		return (rc);
3554 	PORT_LOCK(pi);
3555 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
3556 		/* ifconfig .. media autoselect */
3557 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
3558 			rc = ENOTSUP; /* AN not supported by transceiver */
3559 			goto done;
3560 		}
3561 		lc->requested_aneg = AUTONEG_ENABLE;
3562 		lc->requested_speed = 0;
3563 		lc->requested_fc |= PAUSE_AUTONEG;
3564 	} else {
3565 		lc->requested_aneg = AUTONEG_DISABLE;
3566 		lc->requested_speed =
3567 		    ifmedia_baudrate(ifm->ifm_media) / 1000000;
3568 		lc->requested_fc = 0;
3569 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
3570 			lc->requested_fc |= PAUSE_RX;
3571 		if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
3572 			lc->requested_fc |= PAUSE_TX;
3573 	}
3574 	if (pi->up_vis > 0 && hw_all_ok(sc)) {
3575 		fixup_link_config(pi);
3576 		rc = apply_link_config(pi);
3577 	}
3578 done:
3579 	PORT_UNLOCK(pi);
3580 	end_synchronized_op(sc, 0);
3581 	return (rc);
3582 }
3583 
3584 /*
3585  * Base media word (without ETHER, pause, link active, etc.) for the port at the
3586  * given speed.
3587  */
3588 static int
port_mword(struct port_info * pi,uint32_t speed)3589 port_mword(struct port_info *pi, uint32_t speed)
3590 {
3591 
3592 	MPASS(speed & M_FW_PORT_CAP32_SPEED);
3593 	MPASS(powerof2(speed));
3594 
3595 	switch(pi->port_type) {
3596 	case FW_PORT_TYPE_BT_SGMII:
3597 	case FW_PORT_TYPE_BT_XFI:
3598 	case FW_PORT_TYPE_BT_XAUI:
3599 		/* BaseT */
3600 		switch (speed) {
3601 		case FW_PORT_CAP32_SPEED_100M:
3602 			return (IFM_100_T);
3603 		case FW_PORT_CAP32_SPEED_1G:
3604 			return (IFM_1000_T);
3605 		case FW_PORT_CAP32_SPEED_10G:
3606 			return (IFM_10G_T);
3607 		}
3608 		break;
3609 	case FW_PORT_TYPE_KX4:
3610 		if (speed == FW_PORT_CAP32_SPEED_10G)
3611 			return (IFM_10G_KX4);
3612 		break;
3613 	case FW_PORT_TYPE_CX4:
3614 		if (speed == FW_PORT_CAP32_SPEED_10G)
3615 			return (IFM_10G_CX4);
3616 		break;
3617 	case FW_PORT_TYPE_KX:
3618 		if (speed == FW_PORT_CAP32_SPEED_1G)
3619 			return (IFM_1000_KX);
3620 		break;
3621 	case FW_PORT_TYPE_KR:
3622 	case FW_PORT_TYPE_BP_AP:
3623 	case FW_PORT_TYPE_BP4_AP:
3624 	case FW_PORT_TYPE_BP40_BA:
3625 	case FW_PORT_TYPE_KR4_100G:
3626 	case FW_PORT_TYPE_KR_SFP28:
3627 	case FW_PORT_TYPE_KR_XLAUI:
3628 		switch (speed) {
3629 		case FW_PORT_CAP32_SPEED_1G:
3630 			return (IFM_1000_KX);
3631 		case FW_PORT_CAP32_SPEED_10G:
3632 			return (IFM_10G_KR);
3633 		case FW_PORT_CAP32_SPEED_25G:
3634 			return (IFM_25G_KR);
3635 		case FW_PORT_CAP32_SPEED_40G:
3636 			return (IFM_40G_KR4);
3637 		case FW_PORT_CAP32_SPEED_50G:
3638 			return (IFM_50G_KR2);
3639 		case FW_PORT_CAP32_SPEED_100G:
3640 			return (IFM_100G_KR4);
3641 		}
3642 		break;
3643 	case FW_PORT_TYPE_FIBER_XFI:
3644 	case FW_PORT_TYPE_FIBER_XAUI:
3645 	case FW_PORT_TYPE_SFP:
3646 	case FW_PORT_TYPE_QSFP_10G:
3647 	case FW_PORT_TYPE_QSA:
3648 	case FW_PORT_TYPE_QSFP:
3649 	case FW_PORT_TYPE_CR4_QSFP:
3650 	case FW_PORT_TYPE_CR_QSFP:
3651 	case FW_PORT_TYPE_CR2_QSFP:
3652 	case FW_PORT_TYPE_SFP28:
3653 	case FW_PORT_TYPE_SFP56:
3654 	case FW_PORT_TYPE_QSFP56:
3655 	case FW_PORT_TYPE_QSFPDD:
3656 		/* Pluggable transceiver */
3657 		switch (pi->mod_type) {
3658 		case FW_PORT_MOD_TYPE_LR:
3659 		case FW_PORT_MOD_TYPE_LR_SIMPLEX:
3660 			switch (speed) {
3661 			case FW_PORT_CAP32_SPEED_1G:
3662 				return (IFM_1000_LX);
3663 			case FW_PORT_CAP32_SPEED_10G:
3664 				return (IFM_10G_LR);
3665 			case FW_PORT_CAP32_SPEED_25G:
3666 				return (IFM_25G_LR);
3667 			case FW_PORT_CAP32_SPEED_40G:
3668 				return (IFM_40G_LR4);
3669 			case FW_PORT_CAP32_SPEED_50G:
3670 				return (IFM_50G_LR2);
3671 			case FW_PORT_CAP32_SPEED_100G:
3672 				return (IFM_100G_LR4);
3673 			case FW_PORT_CAP32_SPEED_200G:
3674 				return (IFM_200G_LR4);
3675 			case FW_PORT_CAP32_SPEED_400G:
3676 				return (IFM_400G_LR8);
3677 			}
3678 			break;
3679 		case FW_PORT_MOD_TYPE_SR:
3680 			switch (speed) {
3681 			case FW_PORT_CAP32_SPEED_1G:
3682 				return (IFM_1000_SX);
3683 			case FW_PORT_CAP32_SPEED_10G:
3684 				return (IFM_10G_SR);
3685 			case FW_PORT_CAP32_SPEED_25G:
3686 				return (IFM_25G_SR);
3687 			case FW_PORT_CAP32_SPEED_40G:
3688 				return (IFM_40G_SR4);
3689 			case FW_PORT_CAP32_SPEED_50G:
3690 				return (IFM_50G_SR2);
3691 			case FW_PORT_CAP32_SPEED_100G:
3692 				return (IFM_100G_SR4);
3693 			case FW_PORT_CAP32_SPEED_200G:
3694 				return (IFM_200G_SR4);
3695 			case FW_PORT_CAP32_SPEED_400G:
3696 				return (IFM_400G_SR8);
3697 			}
3698 			break;
3699 		case FW_PORT_MOD_TYPE_ER:
3700 			if (speed == FW_PORT_CAP32_SPEED_10G)
3701 				return (IFM_10G_ER);
3702 			break;
3703 		case FW_PORT_MOD_TYPE_TWINAX_PASSIVE:
3704 		case FW_PORT_MOD_TYPE_TWINAX_ACTIVE:
3705 			switch (speed) {
3706 			case FW_PORT_CAP32_SPEED_1G:
3707 				return (IFM_1000_CX);
3708 			case FW_PORT_CAP32_SPEED_10G:
3709 				return (IFM_10G_TWINAX);
3710 			case FW_PORT_CAP32_SPEED_25G:
3711 				return (IFM_25G_CR);
3712 			case FW_PORT_CAP32_SPEED_40G:
3713 				return (IFM_40G_CR4);
3714 			case FW_PORT_CAP32_SPEED_50G:
3715 				return (IFM_50G_CR2);
3716 			case FW_PORT_CAP32_SPEED_100G:
3717 				return (IFM_100G_CR4);
3718 			case FW_PORT_CAP32_SPEED_200G:
3719 				return (IFM_200G_CR4_PAM4);
3720 			case FW_PORT_CAP32_SPEED_400G:
3721 				return (IFM_400G_CR8);
3722 			}
3723 			break;
3724 		case FW_PORT_MOD_TYPE_LRM:
3725 			if (speed == FW_PORT_CAP32_SPEED_10G)
3726 				return (IFM_10G_LRM);
3727 			break;
3728 		case FW_PORT_MOD_TYPE_DR:
3729 			if (speed == FW_PORT_CAP32_SPEED_100G)
3730 				return (IFM_100G_DR);
3731 			if (speed == FW_PORT_CAP32_SPEED_200G)
3732 				return (IFM_200G_DR4);
3733 			if (speed == FW_PORT_CAP32_SPEED_400G)
3734 				return (IFM_400G_DR4);
3735 			break;
3736 		case FW_PORT_MOD_TYPE_NA:
3737 			MPASS(0);	/* Not pluggable? */
3738 			/* fall through */
3739 		case FW_PORT_MOD_TYPE_ERROR:
3740 		case FW_PORT_MOD_TYPE_UNKNOWN:
3741 		case FW_PORT_MOD_TYPE_NOTSUPPORTED:
3742 			break;
3743 		case FW_PORT_MOD_TYPE_NONE:
3744 			return (IFM_NONE);
3745 		}
3746 		break;
3747 	case M_FW_PORT_CMD_PTYPE:	/* FW_PORT_TYPE_NONE for old firmware */
3748 		if (chip_id(pi->adapter) >= CHELSIO_T7)
3749 			return (IFM_UNKNOWN);
3750 		/* fall through */
3751 	case FW_PORT_TYPE_NONE:
3752 		return (IFM_NONE);
3753 	}
3754 
3755 	return (IFM_UNKNOWN);
3756 }
3757 
3758 void
cxgbe_media_status(if_t ifp,struct ifmediareq * ifmr)3759 cxgbe_media_status(if_t ifp, struct ifmediareq *ifmr)
3760 {
3761 	struct vi_info *vi = if_getsoftc(ifp);
3762 	struct port_info *pi = vi->pi;
3763 	struct adapter *sc = pi->adapter;
3764 	struct link_config *lc = &pi->link_cfg;
3765 
3766 	if (begin_synchronized_op(sc, vi , SLEEP_OK | INTR_OK, "t4med") != 0)
3767 		return;
3768 	PORT_LOCK(pi);
3769 
3770 	if (pi->up_vis == 0 && hw_all_ok(sc)) {
3771 		/*
3772 		 * If all the interfaces are administratively down the firmware
3773 		 * does not report transceiver changes.  Refresh port info here
3774 		 * so that ifconfig displays accurate ifmedia at all times.
3775 		 * This is the only reason we have a synchronized op in this
3776 		 * function.  Just PORT_LOCK would have been enough otherwise.
3777 		 */
3778 		t4_update_port_info(pi);
3779 		build_medialist(pi);
3780 	}
3781 
3782 	/* ifm_status */
3783 	ifmr->ifm_status = IFM_AVALID;
3784 	if (lc->link_ok == false)
3785 		goto done;
3786 	ifmr->ifm_status |= IFM_ACTIVE;
3787 
3788 	/* ifm_active */
3789 	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
3790 	ifmr->ifm_active &= ~(IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
3791 	if (lc->fc & PAUSE_RX)
3792 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
3793 	if (lc->fc & PAUSE_TX)
3794 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
3795 	ifmr->ifm_active |= port_mword(pi, speed_to_fwcap(lc->speed));
3796 done:
3797 	PORT_UNLOCK(pi);
3798 	end_synchronized_op(sc, 0);
3799 }
3800 
3801 static int
vcxgbe_probe(device_t dev)3802 vcxgbe_probe(device_t dev)
3803 {
3804 	struct vi_info *vi = device_get_softc(dev);
3805 
3806 	device_set_descf(dev, "port %d vi %td", vi->pi->port_id,
3807 	    vi - vi->pi->vi);
3808 
3809 	return (BUS_PROBE_DEFAULT);
3810 }
3811 
3812 static int
alloc_extra_vi(struct adapter * sc,struct port_info * pi,struct vi_info * vi)3813 alloc_extra_vi(struct adapter *sc, struct port_info *pi, struct vi_info *vi)
3814 {
3815 	int func, index, rc;
3816 	uint32_t param, val;
3817 
3818 	ASSERT_SYNCHRONIZED_OP(sc);
3819 
3820 	index = vi - pi->vi;
3821 	MPASS(index > 0);	/* This function deals with _extra_ VIs only */
3822 	KASSERT(index < nitems(vi_mac_funcs),
3823 	    ("%s: VI %s doesn't have a MAC func", __func__,
3824 	    device_get_nameunit(vi->dev)));
3825 	func = vi_mac_funcs[index];
3826 	rc = t4_alloc_vi_func(sc, sc->mbox, pi->hw_port, sc->pf, 0, 1,
3827 	    vi->hw_addr, &vi->rss_size, &vi->vfvld, &vi->vin, func, 0);
3828 	if (rc < 0) {
3829 		CH_ERR(vi, "failed to allocate virtual interface %d"
3830 		    "for port %d: %d\n", index, pi->port_id, -rc);
3831 		return (-rc);
3832 	}
3833 	vi->viid = rc;
3834 
3835 	if (vi->rss_size == 1) {
3836 		/*
3837 		 * This VI didn't get a slice of the RSS table.  Reduce the
3838 		 * number of VIs being created (hw.cxgbe.num_vis) or modify the
3839 		 * configuration file (nvi, rssnvi for this PF) if this is a
3840 		 * problem.
3841 		 */
3842 		device_printf(vi->dev, "RSS table not available.\n");
3843 		vi->rss_base = 0xffff;
3844 
3845 		return (0);
3846 	}
3847 
3848 	param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
3849 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_RSSINFO) |
3850 	    V_FW_PARAMS_PARAM_YZ(vi->viid);
3851 	rc = t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
3852 	if (rc)
3853 		vi->rss_base = 0xffff;
3854 	else {
3855 		MPASS((val >> 16) == vi->rss_size);
3856 		vi->rss_base = val & 0xffff;
3857 	}
3858 
3859 	return (0);
3860 }
3861 
3862 static int
vcxgbe_attach(device_t dev)3863 vcxgbe_attach(device_t dev)
3864 {
3865 	struct vi_info *vi;
3866 	struct port_info *pi;
3867 	struct adapter *sc;
3868 	int rc;
3869 
3870 	vi = device_get_softc(dev);
3871 	pi = vi->pi;
3872 	sc = pi->adapter;
3873 
3874 	rc = begin_synchronized_op(sc, vi, SLEEP_OK | INTR_OK, "t4via");
3875 	if (rc)
3876 		return (rc);
3877 	rc = alloc_extra_vi(sc, pi, vi);
3878 	end_synchronized_op(sc, 0);
3879 	if (rc)
3880 		return (rc);
3881 
3882 	cxgbe_vi_attach(dev, vi);
3883 
3884 	return (0);
3885 }
3886 
3887 static int
vcxgbe_detach(device_t dev)3888 vcxgbe_detach(device_t dev)
3889 {
3890 	struct vi_info *vi;
3891 	struct adapter *sc;
3892 
3893 	vi = device_get_softc(dev);
3894 	sc = vi->adapter;
3895 
3896 	begin_vi_detach(sc, vi);
3897 	cxgbe_vi_detach(vi);
3898 	t4_free_vi(sc, sc->mbox, sc->pf, 0, vi->viid);
3899 	end_vi_detach(sc, vi);
3900 
3901 	return (0);
3902 }
3903 
3904 static struct callout fatal_callout;
3905 static struct taskqueue *reset_tq;
3906 
3907 static void
delayed_panic(void * arg)3908 delayed_panic(void *arg)
3909 {
3910 	struct adapter *sc = arg;
3911 
3912 	panic("%s: panic on fatal error", device_get_nameunit(sc->dev));
3913 }
3914 
3915 static void
fatal_error_task(void * arg,int pending)3916 fatal_error_task(void *arg, int pending)
3917 {
3918 	struct adapter *sc = arg;
3919 	int rc;
3920 
3921 	if (atomic_testandclear_int(&sc->error_flags, ilog2(ADAP_CIM_ERR))) {
3922 		dump_cim_regs(sc);
3923 		dump_cimla(sc);
3924 		dump_devlog(sc);
3925 	}
3926 
3927 	if (t4_reset_on_fatal_err) {
3928 		CH_ALERT(sc, "resetting adapter after fatal error.\n");
3929 		rc = reset_adapter(sc);
3930 		if (rc == 0 && t4_panic_on_fatal_err) {
3931 			CH_ALERT(sc, "reset was successful, "
3932 			    "system will NOT panic.\n");
3933 			return;
3934 		}
3935 	}
3936 
3937 	if (t4_panic_on_fatal_err) {
3938 		CH_ALERT(sc, "panicking on fatal error (after 30s).\n");
3939 		callout_reset(&fatal_callout, hz * 30, delayed_panic, sc);
3940 	}
3941 }
3942 
3943 void
t4_fatal_err(struct adapter * sc,bool fw_error)3944 t4_fatal_err(struct adapter *sc, bool fw_error)
3945 {
3946 	const bool verbose = (sc->debug_flags & DF_VERBOSE_SLOWINTR) != 0;
3947 
3948 	stop_adapter(sc);
3949 	if (atomic_testandset_int(&sc->error_flags, ilog2(ADAP_FATAL_ERR)))
3950 		return;
3951 	if (fw_error) {
3952 		/*
3953 		 * We are here because of a firmware error/timeout and not
3954 		 * because of a hardware interrupt.  It is possible (although
3955 		 * not very likely) that an error interrupt was also raised but
3956 		 * this thread ran first and inhibited t4_intr_err.  We walk the
3957 		 * main INT_CAUSE registers here to make sure we haven't missed
3958 		 * anything interesting.
3959 		 */
3960 		t4_slow_intr_handler(sc, verbose);
3961 		atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
3962 	}
3963 	t4_report_fw_error(sc);
3964 	log(LOG_ALERT, "%s: encountered fatal error, adapter stopped (%d).\n",
3965 	    device_get_nameunit(sc->dev), fw_error);
3966 	taskqueue_enqueue(reset_tq, &sc->fatal_error_task);
3967 }
3968 
3969 void
t4_add_adapter(struct adapter * sc)3970 t4_add_adapter(struct adapter *sc)
3971 {
3972 	sx_xlock(&t4_list_lock);
3973 	SLIST_INSERT_HEAD(&t4_list, sc, link);
3974 	sx_xunlock(&t4_list_lock);
3975 }
3976 
3977 int
t4_map_bars_0_and_4(struct adapter * sc)3978 t4_map_bars_0_and_4(struct adapter *sc)
3979 {
3980 	sc->regs_rid = PCIR_BAR(0);
3981 	sc->regs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3982 	    &sc->regs_rid, RF_ACTIVE);
3983 	if (sc->regs_res == NULL) {
3984 		device_printf(sc->dev, "cannot map registers.\n");
3985 		return (ENXIO);
3986 	}
3987 	sc->bt = rman_get_bustag(sc->regs_res);
3988 	sc->bh = rman_get_bushandle(sc->regs_res);
3989 	sc->mmio_len = rman_get_size(sc->regs_res);
3990 	setbit(&sc->doorbells, DOORBELL_KDB);
3991 
3992 	sc->msix_rid = PCIR_BAR(4);
3993 	sc->msix_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
3994 	    &sc->msix_rid, RF_ACTIVE);
3995 	if (sc->msix_res == NULL) {
3996 		device_printf(sc->dev, "cannot map MSI-X BAR.\n");
3997 		return (ENXIO);
3998 	}
3999 
4000 	return (0);
4001 }
4002 
4003 int
t4_map_bar_2(struct adapter * sc)4004 t4_map_bar_2(struct adapter *sc)
4005 {
4006 
4007 	/*
4008 	 * T4: only iWARP driver uses the userspace doorbells.  There is no need
4009 	 * to map it if RDMA is disabled.
4010 	 */
4011 	if (is_t4(sc) && sc->rdmacaps == 0)
4012 		return (0);
4013 
4014 	sc->udbs_rid = PCIR_BAR(2);
4015 	sc->udbs_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
4016 	    &sc->udbs_rid, RF_ACTIVE);
4017 	if (sc->udbs_res == NULL) {
4018 		device_printf(sc->dev, "cannot map doorbell BAR.\n");
4019 		return (ENXIO);
4020 	}
4021 	sc->udbs_base = rman_get_virtual(sc->udbs_res);
4022 
4023 	if (chip_id(sc) >= CHELSIO_T5) {
4024 		setbit(&sc->doorbells, DOORBELL_UDB);
4025 #if defined(__i386__) || defined(__amd64__)
4026 		if (t5_write_combine) {
4027 			int rc, mode;
4028 
4029 			/*
4030 			 * Enable write combining on BAR2.  This is the
4031 			 * userspace doorbell BAR and is split into 128B
4032 			 * (UDBS_SEG_SIZE) doorbell regions, each associated
4033 			 * with an egress queue.  The first 64B has the doorbell
4034 			 * and the second 64B can be used to submit a tx work
4035 			 * request with an implicit doorbell.
4036 			 */
4037 
4038 			rc = pmap_change_attr((vm_offset_t)sc->udbs_base,
4039 			    rman_get_size(sc->udbs_res), PAT_WRITE_COMBINING);
4040 			if (rc == 0) {
4041 				clrbit(&sc->doorbells, DOORBELL_UDB);
4042 				setbit(&sc->doorbells, DOORBELL_WCWR);
4043 				setbit(&sc->doorbells, DOORBELL_UDBWC);
4044 			} else {
4045 				device_printf(sc->dev,
4046 				    "couldn't enable write combining: %d\n",
4047 				    rc);
4048 			}
4049 
4050 			mode = is_t5(sc) ? V_STATMODE(0) : V_T6_STATMODE(0);
4051 			t4_write_reg(sc, A_SGE_STAT_CFG,
4052 			    V_STATSOURCE_T5(7) | mode);
4053 		}
4054 #endif
4055 	}
4056 	sc->iwt.wc_en = isset(&sc->doorbells, DOORBELL_UDBWC) ? 1 : 0;
4057 
4058 	return (0);
4059 }
4060 
4061 int
t4_adj_doorbells(struct adapter * sc)4062 t4_adj_doorbells(struct adapter *sc)
4063 {
4064 	if ((sc->doorbells & t4_doorbells_allowed) != 0) {
4065 		sc->doorbells &= t4_doorbells_allowed;
4066 		return (0);
4067 	}
4068 	CH_ERR(sc, "No usable doorbell (available = 0x%x, allowed = 0x%x).\n",
4069 	       sc->doorbells, t4_doorbells_allowed);
4070 	return (EINVAL);
4071 }
4072 
4073 struct memwin_init {
4074 	uint32_t base;
4075 	uint32_t aperture;
4076 };
4077 
4078 static const struct memwin_init t4_memwin[NUM_MEMWIN] = {
4079 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
4080 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
4081 	{ MEMWIN2_BASE_T4, MEMWIN2_APERTURE_T4 }
4082 };
4083 
4084 static const struct memwin_init t5_memwin[NUM_MEMWIN] = {
4085 	{ MEMWIN0_BASE, MEMWIN0_APERTURE },
4086 	{ MEMWIN1_BASE, MEMWIN1_APERTURE },
4087 	{ MEMWIN2_BASE_T5, MEMWIN2_APERTURE_T5 },
4088 };
4089 
4090 static void
setup_memwin(struct adapter * sc)4091 setup_memwin(struct adapter *sc)
4092 {
4093 	const struct memwin_init *mw_init;
4094 	struct memwin *mw;
4095 	int i;
4096 	uint32_t bar0, reg;
4097 
4098 	if (is_t4(sc)) {
4099 		/*
4100 		 * Read low 32b of bar0 indirectly via the hardware backdoor
4101 		 * mechanism.  Works from within PCI passthrough environments
4102 		 * too, where rman_get_start() can return a different value.  We
4103 		 * need to program the T4 memory window decoders with the actual
4104 		 * addresses that will be coming across the PCIe link.
4105 		 */
4106 		bar0 = t4_hw_pci_read_cfg4(sc, PCIR_BAR(0));
4107 		bar0 &= (uint32_t) PCIM_BAR_MEM_BASE;
4108 
4109 		mw_init = &t4_memwin[0];
4110 	} else {
4111 		/* T5+ use the relative offset inside the PCIe BAR */
4112 		bar0 = 0;
4113 
4114 		mw_init = &t5_memwin[0];
4115 	}
4116 
4117 	for (i = 0, mw = &sc->memwin[0]; i < NUM_MEMWIN; i++, mw_init++, mw++) {
4118 		if (!rw_initialized(&mw->mw_lock)) {
4119 			rw_init(&mw->mw_lock, "memory window access");
4120 			mw->mw_base = mw_init->base;
4121 			mw->mw_aperture = mw_init->aperture;
4122 			mw->mw_curpos = 0;
4123 		}
4124 		reg = chip_id(sc) > CHELSIO_T6 ?
4125 		    PCIE_MEM_ACCESS_T7_REG(A_T7_PCIE_MEM_ACCESS_BASE_WIN, i) :
4126 		    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN, i);
4127 		t4_write_reg(sc, reg, (mw->mw_base + bar0) | V_BIR(0) |
4128 		    V_WINDOW(ilog2(mw->mw_aperture) - 10));
4129 		rw_wlock(&mw->mw_lock);
4130 		position_memwin(sc, i, mw->mw_curpos);
4131 		rw_wunlock(&mw->mw_lock);
4132 	}
4133 
4134 	/* flush */
4135 	t4_read_reg(sc, reg);
4136 }
4137 
4138 /*
4139  * Positions the memory window at the given address in the card's address space.
4140  * There are some alignment requirements and the actual position may be at an
4141  * address prior to the requested address.  mw->mw_curpos always has the actual
4142  * position of the window.
4143  */
4144 static void
position_memwin(struct adapter * sc,int idx,uint32_t addr)4145 position_memwin(struct adapter *sc, int idx, uint32_t addr)
4146 {
4147 	struct memwin *mw;
4148 	uint32_t pf, reg, val;
4149 
4150 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4151 	mw = &sc->memwin[idx];
4152 	rw_assert(&mw->mw_lock, RA_WLOCKED);
4153 
4154 	if (is_t4(sc)) {
4155 		pf = 0;
4156 		mw->mw_curpos = addr & ~0xf;	/* start must be 16B aligned */
4157 	} else {
4158 		pf = V_PFNUM(sc->pf);
4159 		mw->mw_curpos = addr & ~0x7f;	/* start must be 128B aligned */
4160 	}
4161 	if (chip_id(sc) > CHELSIO_T6) {
4162 		reg = PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, idx);
4163 		val = (mw->mw_curpos >> X_T7_MEMOFST_SHIFT) | pf;
4164 	} else {
4165 		reg = PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, idx);
4166 		val = mw->mw_curpos | pf;
4167 	}
4168 	t4_write_reg(sc, reg, val);
4169 	t4_read_reg(sc, reg);	/* flush */
4170 }
4171 
4172 int
rw_via_memwin(struct adapter * sc,int idx,uint32_t addr,uint32_t * val,int len,int rw)4173 rw_via_memwin(struct adapter *sc, int idx, uint32_t addr, uint32_t *val,
4174     int len, int rw)
4175 {
4176 	struct memwin *mw;
4177 	uint32_t mw_end, v;
4178 
4179 	MPASS(idx >= 0 && idx < NUM_MEMWIN);
4180 
4181 	/* Memory can only be accessed in naturally aligned 4 byte units */
4182 	if (addr & 3 || len & 3 || len <= 0)
4183 		return (EINVAL);
4184 
4185 	mw = &sc->memwin[idx];
4186 	while (len > 0) {
4187 		rw_rlock(&mw->mw_lock);
4188 		mw_end = mw->mw_curpos + mw->mw_aperture;
4189 		if (addr >= mw_end || addr < mw->mw_curpos) {
4190 			/* Will need to reposition the window */
4191 			if (!rw_try_upgrade(&mw->mw_lock)) {
4192 				rw_runlock(&mw->mw_lock);
4193 				rw_wlock(&mw->mw_lock);
4194 			}
4195 			rw_assert(&mw->mw_lock, RA_WLOCKED);
4196 			position_memwin(sc, idx, addr);
4197 			rw_downgrade(&mw->mw_lock);
4198 			mw_end = mw->mw_curpos + mw->mw_aperture;
4199 		}
4200 		rw_assert(&mw->mw_lock, RA_RLOCKED);
4201 		while (addr < mw_end && len > 0) {
4202 			if (rw == 0) {
4203 				v = t4_read_reg(sc, mw->mw_base + addr -
4204 				    mw->mw_curpos);
4205 				*val++ = le32toh(v);
4206 			} else {
4207 				v = *val++;
4208 				t4_write_reg(sc, mw->mw_base + addr -
4209 				    mw->mw_curpos, htole32(v));
4210 			}
4211 			addr += 4;
4212 			len -= 4;
4213 		}
4214 		rw_runlock(&mw->mw_lock);
4215 	}
4216 
4217 	return (0);
4218 }
4219 
4220 CTASSERT(M_TID_COOKIE == M_COOKIE);
4221 CTASSERT(MAX_ATIDS <= (M_TID_TID + 1));
4222 
4223 static void
t4_init_atid_table(struct adapter * sc)4224 t4_init_atid_table(struct adapter *sc)
4225 {
4226 	struct tid_info *t;
4227 	int i;
4228 
4229 	t = &sc->tids;
4230 	if (t->natids == 0)
4231 		return;
4232 
4233 	MPASS(t->atid_tab == NULL);
4234 
4235 	t->atid_tab = malloc(t->natids * sizeof(*t->atid_tab), M_CXGBE,
4236 	    M_ZERO | M_WAITOK);
4237 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
4238 	t->afree = t->atid_tab;
4239 	t->atids_in_use = 0;
4240 	t->atid_alloc_stopped = false;
4241 	for (i = 1; i < t->natids; i++)
4242 		t->atid_tab[i - 1].next = &t->atid_tab[i];
4243 	t->atid_tab[t->natids - 1].next = NULL;
4244 }
4245 
4246 static void
t4_free_atid_table(struct adapter * sc)4247 t4_free_atid_table(struct adapter *sc)
4248 {
4249 	struct tid_info *t;
4250 
4251 	t = &sc->tids;
4252 
4253 	KASSERT(t->atids_in_use == 0,
4254 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4255 
4256 	if (mtx_initialized(&t->atid_lock))
4257 		mtx_destroy(&t->atid_lock);
4258 	free(t->atid_tab, M_CXGBE);
4259 	t->atid_tab = NULL;
4260 }
4261 
4262 static void
stop_atid_allocator(struct adapter * sc)4263 stop_atid_allocator(struct adapter *sc)
4264 {
4265 	struct tid_info *t = &sc->tids;
4266 
4267 	if (t->natids == 0)
4268 		return;
4269 	mtx_lock(&t->atid_lock);
4270 	t->atid_alloc_stopped = true;
4271 	mtx_unlock(&t->atid_lock);
4272 }
4273 
4274 static void
restart_atid_allocator(struct adapter * sc)4275 restart_atid_allocator(struct adapter *sc)
4276 {
4277 	struct tid_info *t = &sc->tids;
4278 
4279 	if (t->natids == 0)
4280 		return;
4281 	mtx_lock(&t->atid_lock);
4282 	KASSERT(t->atids_in_use == 0,
4283 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
4284 	t->atid_alloc_stopped = false;
4285 	mtx_unlock(&t->atid_lock);
4286 }
4287 
4288 int
alloc_atid(struct adapter * sc,void * ctx)4289 alloc_atid(struct adapter *sc, void *ctx)
4290 {
4291 	struct tid_info *t = &sc->tids;
4292 	int atid = -1;
4293 
4294 	mtx_lock(&t->atid_lock);
4295 	if (t->afree && !t->atid_alloc_stopped) {
4296 		union aopen_entry *p = t->afree;
4297 
4298 		atid = p - t->atid_tab;
4299 		MPASS(atid <= M_TID_TID);
4300 		t->afree = p->next;
4301 		p->data = ctx;
4302 		t->atids_in_use++;
4303 	}
4304 	mtx_unlock(&t->atid_lock);
4305 	return (atid);
4306 }
4307 
4308 void *
lookup_atid(struct adapter * sc,int atid)4309 lookup_atid(struct adapter *sc, int atid)
4310 {
4311 	struct tid_info *t = &sc->tids;
4312 
4313 	return (t->atid_tab[atid].data);
4314 }
4315 
4316 void
free_atid(struct adapter * sc,int atid)4317 free_atid(struct adapter *sc, int atid)
4318 {
4319 	struct tid_info *t = &sc->tids;
4320 	union aopen_entry *p = &t->atid_tab[atid];
4321 
4322 	mtx_lock(&t->atid_lock);
4323 	p->next = t->afree;
4324 	t->afree = p;
4325 	t->atids_in_use--;
4326 	mtx_unlock(&t->atid_lock);
4327 }
4328 
4329 static void
queue_tid_release(struct adapter * sc,int tid)4330 queue_tid_release(struct adapter *sc, int tid)
4331 {
4332 
4333 	CXGBE_UNIMPLEMENTED("deferred tid release");
4334 }
4335 
4336 void
release_tid(struct adapter * sc,int tid,struct sge_wrq * ctrlq)4337 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
4338 {
4339 	struct wrqe *wr;
4340 	struct cpl_tid_release *req;
4341 
4342 	wr = alloc_wrqe(sizeof(*req), ctrlq);
4343 	if (wr == NULL) {
4344 		queue_tid_release(sc, tid);	/* defer */
4345 		return;
4346 	}
4347 	req = wrtod(wr);
4348 
4349 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
4350 
4351 	t4_wrq_tx(sc, wr);
4352 }
4353 
4354 static int
t4_range_cmp(const void * a,const void * b)4355 t4_range_cmp(const void *a, const void *b)
4356 {
4357 	return ((const struct t4_range *)a)->start -
4358 	       ((const struct t4_range *)b)->start;
4359 }
4360 
4361 /*
4362  * Verify that the memory range specified by the addr/len pair is valid within
4363  * the card's address space.
4364  */
4365 static int
validate_mem_range(struct adapter * sc,uint32_t addr,uint32_t len)4366 validate_mem_range(struct adapter *sc, uint32_t addr, uint32_t len)
4367 {
4368 	struct t4_range mem_ranges[4], *r, *next;
4369 	uint32_t em, addr_len;
4370 	int i, n, remaining;
4371 
4372 	/* Memory can only be accessed in naturally aligned 4 byte units */
4373 	if (addr & 3 || len & 3 || len == 0)
4374 		return (EINVAL);
4375 
4376 	/* Enabled memories */
4377 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4378 
4379 	r = &mem_ranges[0];
4380 	n = 0;
4381 	bzero(r, sizeof(mem_ranges));
4382 	if (em & F_EDRAM0_ENABLE) {
4383 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4384 		r->size = G_EDRAM0_SIZE(addr_len) << 20;
4385 		if (r->size > 0) {
4386 			r->start = G_EDRAM0_BASE(addr_len) << 20;
4387 			if (addr >= r->start &&
4388 			    addr + len <= r->start + r->size)
4389 				return (0);
4390 			r++;
4391 			n++;
4392 		}
4393 	}
4394 	if (em & F_EDRAM1_ENABLE) {
4395 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4396 		r->size = G_EDRAM1_SIZE(addr_len) << 20;
4397 		if (r->size > 0) {
4398 			r->start = G_EDRAM1_BASE(addr_len) << 20;
4399 			if (addr >= r->start &&
4400 			    addr + len <= r->start + r->size)
4401 				return (0);
4402 			r++;
4403 			n++;
4404 		}
4405 	}
4406 	if (em & F_EXT_MEM_ENABLE) {
4407 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4408 		r->size = G_EXT_MEM_SIZE(addr_len) << 20;
4409 		if (r->size > 0) {
4410 			r->start = G_EXT_MEM_BASE(addr_len) << 20;
4411 			if (addr >= r->start &&
4412 			    addr + len <= r->start + r->size)
4413 				return (0);
4414 			r++;
4415 			n++;
4416 		}
4417 	}
4418 	if (is_t5(sc) && em & F_EXT_MEM1_ENABLE) {
4419 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4420 		r->size = G_EXT_MEM1_SIZE(addr_len) << 20;
4421 		if (r->size > 0) {
4422 			r->start = G_EXT_MEM1_BASE(addr_len) << 20;
4423 			if (addr >= r->start &&
4424 			    addr + len <= r->start + r->size)
4425 				return (0);
4426 			r++;
4427 			n++;
4428 		}
4429 	}
4430 	MPASS(n <= nitems(mem_ranges));
4431 
4432 	if (n > 1) {
4433 		/* Sort and merge the ranges. */
4434 		qsort(mem_ranges, n, sizeof(struct t4_range), t4_range_cmp);
4435 
4436 		/* Start from index 0 and examine the next n - 1 entries. */
4437 		r = &mem_ranges[0];
4438 		for (remaining = n - 1; remaining > 0; remaining--, r++) {
4439 
4440 			MPASS(r->size > 0);	/* r is a valid entry. */
4441 			next = r + 1;
4442 			MPASS(next->size > 0);	/* and so is the next one. */
4443 
4444 			while (r->start + r->size >= next->start) {
4445 				/* Merge the next one into the current entry. */
4446 				r->size = max(r->start + r->size,
4447 				    next->start + next->size) - r->start;
4448 				n--;	/* One fewer entry in total. */
4449 				if (--remaining == 0)
4450 					goto done;	/* short circuit */
4451 				next++;
4452 			}
4453 			if (next != r + 1) {
4454 				/*
4455 				 * Some entries were merged into r and next
4456 				 * points to the first valid entry that couldn't
4457 				 * be merged.
4458 				 */
4459 				MPASS(next->size > 0);	/* must be valid */
4460 				memcpy(r + 1, next, remaining * sizeof(*r));
4461 #ifdef INVARIANTS
4462 				/*
4463 				 * This so that the foo->size assertion in the
4464 				 * next iteration of the loop do the right
4465 				 * thing for entries that were pulled up and are
4466 				 * no longer valid.
4467 				 */
4468 				MPASS(n < nitems(mem_ranges));
4469 				bzero(&mem_ranges[n], (nitems(mem_ranges) - n) *
4470 				    sizeof(struct t4_range));
4471 #endif
4472 			}
4473 		}
4474 done:
4475 		/* Done merging the ranges. */
4476 		MPASS(n > 0);
4477 		r = &mem_ranges[0];
4478 		for (i = 0; i < n; i++, r++) {
4479 			if (addr >= r->start &&
4480 			    addr + len <= r->start + r->size)
4481 				return (0);
4482 		}
4483 	}
4484 
4485 	return (EFAULT);
4486 }
4487 
4488 static int
fwmtype_to_hwmtype(int mtype)4489 fwmtype_to_hwmtype(int mtype)
4490 {
4491 
4492 	switch (mtype) {
4493 	case FW_MEMTYPE_EDC0:
4494 		return (MEM_EDC0);
4495 	case FW_MEMTYPE_EDC1:
4496 		return (MEM_EDC1);
4497 	case FW_MEMTYPE_EXTMEM:
4498 		return (MEM_MC0);
4499 	case FW_MEMTYPE_EXTMEM1:
4500 		return (MEM_MC1);
4501 	default:
4502 		panic("%s: cannot translate fw mtype %d.", __func__, mtype);
4503 	}
4504 }
4505 
4506 /*
4507  * Verify that the memory range specified by the memtype/offset/len pair is
4508  * valid and lies entirely within the memtype specified.  The global address of
4509  * the start of the range is returned in addr.
4510  */
4511 static int
validate_mt_off_len(struct adapter * sc,int mtype,uint32_t off,uint32_t len,uint32_t * addr)4512 validate_mt_off_len(struct adapter *sc, int mtype, uint32_t off, uint32_t len,
4513     uint32_t *addr)
4514 {
4515 	uint32_t em, addr_len, maddr;
4516 
4517 	/* Memory can only be accessed in naturally aligned 4 byte units */
4518 	if (off & 3 || len & 3 || len == 0)
4519 		return (EINVAL);
4520 
4521 	em = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
4522 	switch (fwmtype_to_hwmtype(mtype)) {
4523 	case MEM_EDC0:
4524 		if (!(em & F_EDRAM0_ENABLE))
4525 			return (EINVAL);
4526 		addr_len = t4_read_reg(sc, A_MA_EDRAM0_BAR);
4527 		maddr = G_EDRAM0_BASE(addr_len) << 20;
4528 		break;
4529 	case MEM_EDC1:
4530 		if (!(em & F_EDRAM1_ENABLE))
4531 			return (EINVAL);
4532 		addr_len = t4_read_reg(sc, A_MA_EDRAM1_BAR);
4533 		maddr = G_EDRAM1_BASE(addr_len) << 20;
4534 		break;
4535 	case MEM_MC:
4536 		if (!(em & F_EXT_MEM_ENABLE))
4537 			return (EINVAL);
4538 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
4539 		maddr = G_EXT_MEM_BASE(addr_len) << 20;
4540 		break;
4541 	case MEM_MC1:
4542 		if (!is_t5(sc) || !(em & F_EXT_MEM1_ENABLE))
4543 			return (EINVAL);
4544 		addr_len = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
4545 		maddr = G_EXT_MEM1_BASE(addr_len) << 20;
4546 		break;
4547 	default:
4548 		return (EINVAL);
4549 	}
4550 
4551 	*addr = maddr + off;	/* global address */
4552 	return (validate_mem_range(sc, *addr, len));
4553 }
4554 
4555 static int
fixup_devlog_params(struct adapter * sc)4556 fixup_devlog_params(struct adapter *sc)
4557 {
4558 	struct devlog_params *dparams = &sc->params.devlog;
4559 	int rc;
4560 
4561 	rc = validate_mt_off_len(sc, dparams->memtype, dparams->start,
4562 	    dparams->size, &dparams->addr);
4563 
4564 	return (rc);
4565 }
4566 
4567 static void
update_nirq(struct intrs_and_queues * iaq,int nports)4568 update_nirq(struct intrs_and_queues *iaq, int nports)
4569 {
4570 
4571 	iaq->nirq = T4_EXTRA_INTR;
4572 	iaq->nirq += nports * max(iaq->nrxq, iaq->nnmrxq);
4573 	iaq->nirq += nports * iaq->nofldrxq;
4574 	iaq->nirq += nports * (iaq->num_vis - 1) *
4575 	    max(iaq->nrxq_vi, iaq->nnmrxq_vi);
4576 	iaq->nirq += nports * (iaq->num_vis - 1) * iaq->nofldrxq_vi;
4577 }
4578 
4579 /*
4580  * Adjust requirements to fit the number of interrupts available.
4581  */
4582 static void
calculate_iaq(struct adapter * sc,struct intrs_and_queues * iaq,int itype,int navail)4583 calculate_iaq(struct adapter *sc, struct intrs_and_queues *iaq, int itype,
4584     int navail)
4585 {
4586 	int old_nirq;
4587 	const int nports = sc->params.nports;
4588 
4589 	MPASS(nports > 0);
4590 	MPASS(navail > 0);
4591 
4592 	bzero(iaq, sizeof(*iaq));
4593 	iaq->intr_type = itype;
4594 	iaq->num_vis = t4_num_vis;
4595 	iaq->ntxq = t4_ntxq;
4596 	iaq->ntxq_vi = t4_ntxq_vi;
4597 	iaq->nrxq = t4_nrxq;
4598 	iaq->nrxq_vi = t4_nrxq_vi;
4599 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
4600 	if (is_offload(sc) || is_ethoffload(sc)) {
4601 		if (sc->params.tid_qid_sel_mask == 0) {
4602 			iaq->nofldtxq = t4_nofldtxq;
4603 			iaq->nofldtxq_vi = t4_nofldtxq_vi;
4604 		} else {
4605 			iaq->nofldtxq = roundup(t4_nofldtxq, sc->params.ncores);
4606 			iaq->nofldtxq_vi = roundup(t4_nofldtxq_vi,
4607 			    sc->params.ncores);
4608 			if (iaq->nofldtxq != t4_nofldtxq)
4609 				device_printf(sc->dev,
4610 				    "nofldtxq updated (%d -> %d) for correct"
4611 				    " operation with %d firmware cores.\n",
4612 				    t4_nofldtxq, iaq->nofldtxq,
4613 				    sc->params.ncores);
4614 			if (iaq->num_vis > 1 &&
4615 			    iaq->nofldtxq_vi != t4_nofldtxq_vi)
4616 				device_printf(sc->dev,
4617 				    "nofldtxq_vi updated (%d -> %d) for correct"
4618 				    " operation with %d firmware cores.\n",
4619 				    t4_nofldtxq_vi, iaq->nofldtxq_vi,
4620 				    sc->params.ncores);
4621 		}
4622 	}
4623 #endif
4624 #ifdef TCP_OFFLOAD
4625 	if (is_offload(sc)) {
4626 		iaq->nofldrxq = t4_nofldrxq;
4627 		iaq->nofldrxq_vi = t4_nofldrxq_vi;
4628 	}
4629 #endif
4630 #ifdef DEV_NETMAP
4631 	if (t4_native_netmap & NN_MAIN_VI) {
4632 		iaq->nnmtxq = t4_nnmtxq;
4633 		iaq->nnmrxq = t4_nnmrxq;
4634 	}
4635 	if (t4_native_netmap & NN_EXTRA_VI) {
4636 		iaq->nnmtxq_vi = t4_nnmtxq_vi;
4637 		iaq->nnmrxq_vi = t4_nnmrxq_vi;
4638 	}
4639 #endif
4640 
4641 	update_nirq(iaq, nports);
4642 	if (iaq->nirq <= navail &&
4643 	    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4644 		/*
4645 		 * This is the normal case -- there are enough interrupts for
4646 		 * everything.
4647 		 */
4648 		goto done;
4649 	}
4650 
4651 	/*
4652 	 * If extra VIs have been configured try reducing their count and see if
4653 	 * that works.
4654 	 */
4655 	while (iaq->num_vis > 1) {
4656 		iaq->num_vis--;
4657 		update_nirq(iaq, nports);
4658 		if (iaq->nirq <= navail &&
4659 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4660 			device_printf(sc->dev, "virtual interfaces per port "
4661 			    "reduced to %d from %d.  nrxq=%u, nofldrxq=%u, "
4662 			    "nrxq_vi=%u nofldrxq_vi=%u, nnmrxq_vi=%u.  "
4663 			    "itype %d, navail %u, nirq %d.\n",
4664 			    iaq->num_vis, t4_num_vis, iaq->nrxq, iaq->nofldrxq,
4665 			    iaq->nrxq_vi, iaq->nofldrxq_vi, iaq->nnmrxq_vi,
4666 			    itype, navail, iaq->nirq);
4667 			goto done;
4668 		}
4669 	}
4670 
4671 	/*
4672 	 * Extra VIs will not be created.  Log a message if they were requested.
4673 	 */
4674 	MPASS(iaq->num_vis == 1);
4675 	iaq->ntxq_vi = iaq->nrxq_vi = 0;
4676 	iaq->nofldtxq_vi = iaq->nofldrxq_vi = 0;
4677 	iaq->nnmtxq_vi = iaq->nnmrxq_vi = 0;
4678 	if (iaq->num_vis != t4_num_vis) {
4679 		device_printf(sc->dev, "extra virtual interfaces disabled.  "
4680 		    "nrxq=%u, nofldrxq=%u, nrxq_vi=%u nofldrxq_vi=%u, "
4681 		    "nnmrxq_vi=%u.  itype %d, navail %u, nirq %d.\n",
4682 		    iaq->nrxq, iaq->nofldrxq, iaq->nrxq_vi, iaq->nofldrxq_vi,
4683 		    iaq->nnmrxq_vi, itype, navail, iaq->nirq);
4684 	}
4685 
4686 	/*
4687 	 * Keep reducing the number of NIC rx queues to the next lower power of
4688 	 * 2 (for even RSS distribution) and halving the TOE rx queues and see
4689 	 * if that works.
4690 	 */
4691 	do {
4692 		if (iaq->nrxq > 1) {
4693 			iaq->nrxq = rounddown_pow_of_two(iaq->nrxq - 1);
4694 			if (iaq->nnmrxq > iaq->nrxq)
4695 				iaq->nnmrxq = iaq->nrxq;
4696 		}
4697 		if (iaq->nofldrxq > 1)
4698 			iaq->nofldrxq >>= 1;
4699 
4700 		old_nirq = iaq->nirq;
4701 		update_nirq(iaq, nports);
4702 		if (iaq->nirq <= navail &&
4703 		    (itype != INTR_MSI || powerof2(iaq->nirq))) {
4704 			device_printf(sc->dev, "running with reduced number of "
4705 			    "rx queues because of shortage of interrupts.  "
4706 			    "nrxq=%u, nofldrxq=%u.  "
4707 			    "itype %d, navail %u, nirq %d.\n", iaq->nrxq,
4708 			    iaq->nofldrxq, itype, navail, iaq->nirq);
4709 			goto done;
4710 		}
4711 	} while (old_nirq != iaq->nirq);
4712 
4713 	/* One interrupt for everything.  Ugh. */
4714 	device_printf(sc->dev, "running with minimal number of queues.  "
4715 	    "itype %d, navail %u.\n", itype, navail);
4716 	iaq->nirq = 1;
4717 	iaq->nrxq = 1;
4718 	iaq->ntxq = 1;
4719 	if (iaq->nofldrxq > 0) {
4720 		iaq->nofldrxq = 1;
4721 		iaq->nofldtxq = 1;
4722 		if (sc->params.tid_qid_sel_mask == 0)
4723 			iaq->nofldtxq = 1;
4724 		else
4725 			iaq->nofldtxq = sc->params.ncores;
4726 	}
4727 	iaq->nnmtxq = 0;
4728 	iaq->nnmrxq = 0;
4729 done:
4730 	MPASS(iaq->num_vis > 0);
4731 	if (iaq->num_vis > 1) {
4732 		MPASS(iaq->nrxq_vi > 0);
4733 		MPASS(iaq->ntxq_vi > 0);
4734 	}
4735 	MPASS(iaq->nirq > 0);
4736 	MPASS(iaq->nrxq > 0);
4737 	MPASS(iaq->ntxq > 0);
4738 	if (itype == INTR_MSI)
4739 		MPASS(powerof2(iaq->nirq));
4740 	if (sc->params.tid_qid_sel_mask != 0)
4741 		MPASS(iaq->nofldtxq % sc->params.ncores == 0);
4742 }
4743 
4744 static int
cfg_itype_and_nqueues(struct adapter * sc,struct intrs_and_queues * iaq)4745 cfg_itype_and_nqueues(struct adapter *sc, struct intrs_and_queues *iaq)
4746 {
4747 	int rc, itype, navail, nalloc;
4748 
4749 	for (itype = INTR_MSIX; itype; itype >>= 1) {
4750 
4751 		if ((itype & t4_intr_types) == 0)
4752 			continue;	/* not allowed */
4753 
4754 		if (itype == INTR_MSIX)
4755 			navail = pci_msix_count(sc->dev);
4756 		else if (itype == INTR_MSI)
4757 			navail = pci_msi_count(sc->dev);
4758 		else
4759 			navail = 1;
4760 restart:
4761 		if (navail == 0)
4762 			continue;
4763 
4764 		calculate_iaq(sc, iaq, itype, navail);
4765 		nalloc = iaq->nirq;
4766 		rc = 0;
4767 		if (itype == INTR_MSIX)
4768 			rc = pci_alloc_msix(sc->dev, &nalloc);
4769 		else if (itype == INTR_MSI)
4770 			rc = pci_alloc_msi(sc->dev, &nalloc);
4771 
4772 		if (rc == 0 && nalloc > 0) {
4773 			if (nalloc == iaq->nirq)
4774 				return (0);
4775 
4776 			/*
4777 			 * Didn't get the number requested.  Use whatever number
4778 			 * the kernel is willing to allocate.
4779 			 */
4780 			device_printf(sc->dev, "fewer vectors than requested, "
4781 			    "type=%d, req=%d, rcvd=%d; will downshift req.\n",
4782 			    itype, iaq->nirq, nalloc);
4783 			pci_release_msi(sc->dev);
4784 			navail = nalloc;
4785 			goto restart;
4786 		}
4787 
4788 		device_printf(sc->dev,
4789 		    "failed to allocate vectors:%d, type=%d, req=%d, rcvd=%d\n",
4790 		    itype, rc, iaq->nirq, nalloc);
4791 	}
4792 
4793 	device_printf(sc->dev,
4794 	    "failed to find a usable interrupt type.  "
4795 	    "allowed=%d, msi-x=%d, msi=%d, intx=1", t4_intr_types,
4796 	    pci_msix_count(sc->dev), pci_msi_count(sc->dev));
4797 
4798 	return (ENXIO);
4799 }
4800 
4801 #define FW_VERSION(chip) ( \
4802     V_FW_HDR_FW_VER_MAJOR(chip##FW_VERSION_MAJOR) | \
4803     V_FW_HDR_FW_VER_MINOR(chip##FW_VERSION_MINOR) | \
4804     V_FW_HDR_FW_VER_MICRO(chip##FW_VERSION_MICRO) | \
4805     V_FW_HDR_FW_VER_BUILD(chip##FW_VERSION_BUILD))
4806 #define FW_INTFVER(chip, intf) (chip##FW_HDR_INTFVER_##intf)
4807 
4808 /* Just enough of fw_hdr to cover all version info. */
4809 struct fw_h {
4810 	__u8	ver;
4811 	__u8	chip;
4812 	__be16	len512;
4813 	__be32	fw_ver;
4814 	__be32	tp_microcode_ver;
4815 	__u8	intfver_nic;
4816 	__u8	intfver_vnic;
4817 	__u8	intfver_ofld;
4818 	__u8	intfver_ri;
4819 	__u8	intfver_iscsipdu;
4820 	__u8	intfver_iscsi;
4821 	__u8	intfver_fcoepdu;
4822 	__u8	intfver_fcoe;
4823 };
4824 /* Spot check a couple of fields. */
4825 CTASSERT(offsetof(struct fw_h, fw_ver) == offsetof(struct fw_hdr, fw_ver));
4826 CTASSERT(offsetof(struct fw_h, intfver_nic) == offsetof(struct fw_hdr, intfver_nic));
4827 CTASSERT(offsetof(struct fw_h, intfver_fcoe) == offsetof(struct fw_hdr, intfver_fcoe));
4828 
4829 struct fw_info {
4830 	uint8_t chip;
4831 	char *kld_name;
4832 	char *fw_mod_name;
4833 	struct fw_h fw_h;
4834 } fw_info[] = {
4835 	{
4836 		.chip = CHELSIO_T4,
4837 		.kld_name = "t4fw_cfg",
4838 		.fw_mod_name = "t4fw",
4839 		.fw_h = {
4840 			.chip = FW_HDR_CHIP_T4,
4841 			.fw_ver = htobe32(FW_VERSION(T4)),
4842 			.intfver_nic = FW_INTFVER(T4, NIC),
4843 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4844 			.intfver_ofld = FW_INTFVER(T4, OFLD),
4845 			.intfver_ri = FW_INTFVER(T4, RI),
4846 			.intfver_iscsipdu = FW_INTFVER(T4, ISCSIPDU),
4847 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4848 			.intfver_fcoepdu = FW_INTFVER(T4, FCOEPDU),
4849 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4850 		},
4851 	}, {
4852 		.chip = CHELSIO_T5,
4853 		.kld_name = "t5fw_cfg",
4854 		.fw_mod_name = "t5fw",
4855 		.fw_h = {
4856 			.chip = FW_HDR_CHIP_T5,
4857 			.fw_ver = htobe32(FW_VERSION(T5)),
4858 			.intfver_nic = FW_INTFVER(T5, NIC),
4859 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4860 			.intfver_ofld = FW_INTFVER(T5, OFLD),
4861 			.intfver_ri = FW_INTFVER(T5, RI),
4862 			.intfver_iscsipdu = FW_INTFVER(T5, ISCSIPDU),
4863 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4864 			.intfver_fcoepdu = FW_INTFVER(T5, FCOEPDU),
4865 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4866 		},
4867 	}, {
4868 		.chip = CHELSIO_T6,
4869 		.kld_name = "t6fw_cfg",
4870 		.fw_mod_name = "t6fw",
4871 		.fw_h = {
4872 			.chip = FW_HDR_CHIP_T6,
4873 			.fw_ver = htobe32(FW_VERSION(T6)),
4874 			.intfver_nic = FW_INTFVER(T6, NIC),
4875 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4876 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4877 			.intfver_ri = FW_INTFVER(T6, RI),
4878 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4879 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4880 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4881 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4882 		},
4883 	}, {
4884 		.chip = CHELSIO_T7,
4885 		.kld_name = "t7fw_cfg",
4886 		.fw_mod_name = "t7fw",
4887 		.fw_h = {
4888 			.chip = FW_HDR_CHIP_T7,
4889 			.fw_ver = htobe32(FW_VERSION(T7)),
4890 			.intfver_nic = FW_INTFVER(T7, NIC),
4891 			.intfver_vnic = FW_INTFVER(T7, VNIC),
4892 			.intfver_ofld = FW_INTFVER(T7, OFLD),
4893 			.intfver_ri = FW_INTFVER(T7, RI),
4894 			.intfver_iscsipdu = FW_INTFVER(T7, ISCSIPDU),
4895 			.intfver_iscsi = FW_INTFVER(T7, ISCSI),
4896 			.intfver_fcoepdu = FW_INTFVER(T7, FCOEPDU),
4897 			.intfver_fcoe = FW_INTFVER(T7, FCOE),
4898 		},
4899 	}
4900 };
4901 
4902 static struct fw_info *
find_fw_info(int chip)4903 find_fw_info(int chip)
4904 {
4905 	int i;
4906 
4907 	for (i = 0; i < nitems(fw_info); i++) {
4908 		if (fw_info[i].chip == chip)
4909 			return (&fw_info[i]);
4910 	}
4911 	return (NULL);
4912 }
4913 
4914 /*
4915  * Is the given firmware API compatible with the one the driver was compiled
4916  * with?
4917  */
4918 static int
fw_compatible(const struct fw_h * hdr1,const struct fw_h * hdr2)4919 fw_compatible(const struct fw_h *hdr1, const struct fw_h *hdr2)
4920 {
4921 
4922 	/* short circuit if it's the exact same firmware version */
4923 	if (hdr1->chip == hdr2->chip && hdr1->fw_ver == hdr2->fw_ver)
4924 		return (1);
4925 
4926 	/*
4927 	 * XXX: Is this too conservative?  Perhaps I should limit this to the
4928 	 * features that are supported in the driver.
4929 	 */
4930 #define SAME_INTF(x) (hdr1->intfver_##x == hdr2->intfver_##x)
4931 	if (hdr1->chip == hdr2->chip && SAME_INTF(nic) && SAME_INTF(vnic) &&
4932 	    SAME_INTF(ofld) && SAME_INTF(ri) && SAME_INTF(iscsipdu) &&
4933 	    SAME_INTF(iscsi) && SAME_INTF(fcoepdu) && SAME_INTF(fcoe))
4934 		return (1);
4935 #undef SAME_INTF
4936 
4937 	return (0);
4938 }
4939 
4940 static int
load_fw_module(struct adapter * sc,const struct firmware ** dcfg,const struct firmware ** fw)4941 load_fw_module(struct adapter *sc, const struct firmware **dcfg,
4942     const struct firmware **fw)
4943 {
4944 	struct fw_info *fw_info;
4945 
4946 	*dcfg = NULL;
4947 	if (fw != NULL)
4948 		*fw = NULL;
4949 
4950 	fw_info = find_fw_info(chip_id(sc));
4951 	if (fw_info == NULL) {
4952 		device_printf(sc->dev,
4953 		    "unable to look up firmware information for chip %d.\n",
4954 		    chip_id(sc));
4955 		return (EINVAL);
4956 	}
4957 
4958 	*dcfg = firmware_get(fw_info->kld_name);
4959 	if (*dcfg != NULL) {
4960 		if (fw != NULL)
4961 			*fw = firmware_get(fw_info->fw_mod_name);
4962 		return (0);
4963 	}
4964 
4965 	return (ENOENT);
4966 }
4967 
4968 static void
unload_fw_module(struct adapter * sc,const struct firmware * dcfg,const struct firmware * fw)4969 unload_fw_module(struct adapter *sc, const struct firmware *dcfg,
4970     const struct firmware *fw)
4971 {
4972 
4973 	if (fw != NULL)
4974 		firmware_put(fw, FIRMWARE_UNLOAD);
4975 	if (dcfg != NULL)
4976 		firmware_put(dcfg, FIRMWARE_UNLOAD);
4977 }
4978 
4979 /*
4980  * Return values:
4981  * 0 means no firmware install attempted.
4982  * ERESTART means a firmware install was attempted and was successful.
4983  * +ve errno means a firmware install was attempted but failed.
4984  */
4985 static int
install_kld_firmware(struct adapter * sc,struct fw_h * card_fw,const struct fw_h * drv_fw,const char * reason,int * already)4986 install_kld_firmware(struct adapter *sc, struct fw_h *card_fw,
4987     const struct fw_h *drv_fw, const char *reason, int *already)
4988 {
4989 	const struct firmware *cfg, *fw;
4990 	const uint32_t c = be32toh(card_fw->fw_ver);
4991 	uint32_t d, k;
4992 	int rc, fw_install;
4993 	struct fw_h bundled_fw;
4994 	bool load_attempted;
4995 
4996 	cfg = fw = NULL;
4997 	load_attempted = false;
4998 	fw_install = t4_fw_install < 0 ? -t4_fw_install : t4_fw_install;
4999 
5000 	memcpy(&bundled_fw, drv_fw, sizeof(bundled_fw));
5001 	if (t4_fw_install < 0) {
5002 		rc = load_fw_module(sc, &cfg, &fw);
5003 		if (rc != 0 || fw == NULL) {
5004 			device_printf(sc->dev,
5005 			    "failed to load firmware module: %d. cfg %p, fw %p;"
5006 			    " will use compiled-in firmware version for"
5007 			    "hw.cxgbe.fw_install checks.\n",
5008 			    rc, cfg, fw);
5009 		} else {
5010 			memcpy(&bundled_fw, fw->data, sizeof(bundled_fw));
5011 		}
5012 		load_attempted = true;
5013 	}
5014 	d = be32toh(bundled_fw.fw_ver);
5015 
5016 	if (reason != NULL)
5017 		goto install;
5018 
5019 	if ((sc->flags & FW_OK) == 0) {
5020 
5021 		if (c == 0xffffffff) {
5022 			reason = "missing";
5023 			goto install;
5024 		}
5025 
5026 		rc = 0;
5027 		goto done;
5028 	}
5029 
5030 	if (!fw_compatible(card_fw, &bundled_fw)) {
5031 		reason = "incompatible or unusable";
5032 		goto install;
5033 	}
5034 
5035 	if (d > c) {
5036 		reason = "older than the version bundled with this driver";
5037 		goto install;
5038 	}
5039 
5040 	if (fw_install == 2 && d != c) {
5041 		reason = "different than the version bundled with this driver";
5042 		goto install;
5043 	}
5044 
5045 	/* No reason to do anything to the firmware already on the card. */
5046 	rc = 0;
5047 	goto done;
5048 
5049 install:
5050 	rc = 0;
5051 	if ((*already)++)
5052 		goto done;
5053 
5054 	if (fw_install == 0) {
5055 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5056 		    "but the driver is prohibited from installing a firmware "
5057 		    "on the card.\n",
5058 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5059 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5060 
5061 		goto done;
5062 	}
5063 
5064 	/*
5065 	 * We'll attempt to install a firmware.  Load the module first (if it
5066 	 * hasn't been loaded already).
5067 	 */
5068 	if (!load_attempted) {
5069 		rc = load_fw_module(sc, &cfg, &fw);
5070 		if (rc != 0 || fw == NULL) {
5071 			device_printf(sc->dev,
5072 			    "failed to load firmware module: %d. cfg %p, fw %p\n",
5073 			    rc, cfg, fw);
5074 			/* carry on */
5075 		}
5076 	}
5077 	if (fw == NULL) {
5078 		device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5079 		    "but the driver cannot take corrective action because it "
5080 		    "is unable to load the firmware module.\n",
5081 		    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5082 		    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason);
5083 		rc = sc->flags & FW_OK ? 0 : ENOENT;
5084 		goto done;
5085 	}
5086 	k = be32toh(((const struct fw_hdr *)fw->data)->fw_ver);
5087 	if (k != d) {
5088 		MPASS(t4_fw_install > 0);
5089 		device_printf(sc->dev,
5090 		    "firmware in KLD (%u.%u.%u.%u) is not what the driver was "
5091 		    "expecting (%u.%u.%u.%u) and will not be used.\n",
5092 		    G_FW_HDR_FW_VER_MAJOR(k), G_FW_HDR_FW_VER_MINOR(k),
5093 		    G_FW_HDR_FW_VER_MICRO(k), G_FW_HDR_FW_VER_BUILD(k),
5094 		    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5095 		    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5096 		rc = sc->flags & FW_OK ? 0 : EINVAL;
5097 		goto done;
5098 	}
5099 
5100 	device_printf(sc->dev, "firmware on card (%u.%u.%u.%u) is %s, "
5101 	    "installing firmware %u.%u.%u.%u on card.\n",
5102 	    G_FW_HDR_FW_VER_MAJOR(c), G_FW_HDR_FW_VER_MINOR(c),
5103 	    G_FW_HDR_FW_VER_MICRO(c), G_FW_HDR_FW_VER_BUILD(c), reason,
5104 	    G_FW_HDR_FW_VER_MAJOR(d), G_FW_HDR_FW_VER_MINOR(d),
5105 	    G_FW_HDR_FW_VER_MICRO(d), G_FW_HDR_FW_VER_BUILD(d));
5106 
5107 	rc = -t4_fw_upgrade(sc, sc->mbox, fw->data, fw->datasize, 0);
5108 	if (rc != 0) {
5109 		device_printf(sc->dev, "failed to install firmware: %d\n", rc);
5110 	} else {
5111 		/* Installed successfully, update the cached header too. */
5112 		rc = ERESTART;
5113 		memcpy(card_fw, fw->data, sizeof(*card_fw));
5114 	}
5115 done:
5116 	unload_fw_module(sc, cfg, fw);
5117 
5118 	return (rc);
5119 }
5120 
5121 /*
5122  * Establish contact with the firmware and attempt to become the master driver.
5123  *
5124  * A firmware will be installed to the card if needed (if the driver is allowed
5125  * to do so).
5126  */
5127 static int
contact_firmware(struct adapter * sc)5128 contact_firmware(struct adapter *sc)
5129 {
5130 	int rc, already = 0;
5131 	enum dev_state state;
5132 	struct fw_info *fw_info;
5133 	struct fw_hdr *card_fw;		/* fw on the card */
5134 	const struct fw_h *drv_fw;
5135 
5136 	fw_info = find_fw_info(chip_id(sc));
5137 	if (fw_info == NULL) {
5138 		device_printf(sc->dev,
5139 		    "unable to look up firmware information for chip %d.\n",
5140 		    chip_id(sc));
5141 		return (EINVAL);
5142 	}
5143 	drv_fw = &fw_info->fw_h;
5144 
5145 	/* Read the header of the firmware on the card */
5146 	card_fw = malloc(sizeof(*card_fw), M_CXGBE, M_ZERO | M_WAITOK);
5147 restart:
5148 	rc = -t4_get_fw_hdr(sc, card_fw);
5149 	if (rc != 0) {
5150 		device_printf(sc->dev,
5151 		    "unable to read firmware header from card's flash: %d\n",
5152 		    rc);
5153 		goto done;
5154 	}
5155 
5156 	rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw, NULL,
5157 	    &already);
5158 	if (rc == ERESTART)
5159 		goto restart;
5160 	if (rc != 0)
5161 		goto done;
5162 
5163 	rc = t4_fw_hello(sc, sc->mbox, sc->mbox, MASTER_MAY, &state);
5164 	if (rc < 0 || state == DEV_STATE_ERR) {
5165 		rc = -rc;
5166 		device_printf(sc->dev,
5167 		    "failed to connect to the firmware: %d, %d.  "
5168 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5169 #if 0
5170 		if (install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5171 		    "not responding properly to HELLO", &already) == ERESTART)
5172 			goto restart;
5173 #endif
5174 		goto done;
5175 	}
5176 	MPASS(be32toh(card_fw->flags) & FW_HDR_FLAGS_RESET_HALT);
5177 	sc->flags |= FW_OK;	/* The firmware responded to the FW_HELLO. */
5178 
5179 	if (rc == sc->pf) {
5180 		sc->flags |= MASTER_PF;
5181 		rc = install_kld_firmware(sc, (struct fw_h *)card_fw, drv_fw,
5182 		    NULL, &already);
5183 		if (rc == ERESTART)
5184 			rc = 0;
5185 		else if (rc != 0)
5186 			goto done;
5187 	} else if (state == DEV_STATE_UNINIT) {
5188 		/*
5189 		 * We didn't get to be the master so we definitely won't be
5190 		 * configuring the chip.  It's a bug if someone else hasn't
5191 		 * configured it already.
5192 		 */
5193 		device_printf(sc->dev, "couldn't be master(%d), "
5194 		    "device not already initialized either(%d).  "
5195 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5196 		rc = EPROTO;
5197 		goto done;
5198 	} else {
5199 		/*
5200 		 * Some other PF is the master and has configured the chip.
5201 		 * This is allowed but untested.
5202 		 */
5203 		device_printf(sc->dev, "PF%d is master, device state %d.  "
5204 		    "PCIE_FW 0x%08x\n", rc, state, t4_read_reg(sc, A_PCIE_FW));
5205 		snprintf(sc->cfg_file, sizeof(sc->cfg_file), "pf%d", rc);
5206 		sc->cfcsum = 0;
5207 		rc = 0;
5208 	}
5209 done:
5210 	if (rc != 0 && sc->flags & FW_OK) {
5211 		t4_fw_bye(sc, sc->mbox);
5212 		sc->flags &= ~FW_OK;
5213 	}
5214 	free(card_fw, M_CXGBE);
5215 	return (rc);
5216 }
5217 
5218 static int
copy_cfg_file_to_card(struct adapter * sc,char * cfg_file,uint32_t mtype,uint32_t moff,u_int maxlen)5219 copy_cfg_file_to_card(struct adapter *sc, char *cfg_file,
5220     uint32_t mtype, uint32_t moff, u_int maxlen)
5221 {
5222 	struct fw_info *fw_info;
5223 	const struct firmware *dcfg, *rcfg = NULL;
5224 	const uint32_t *cfdata;
5225 	uint32_t cflen, addr;
5226 	int rc;
5227 
5228 	load_fw_module(sc, &dcfg, NULL);
5229 
5230 	/* Card specific interpretation of "default". */
5231 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5232 		if (pci_get_device(sc->dev) == 0x440a)
5233 			snprintf(cfg_file, sizeof(t4_cfg_file), UWIRE_CF);
5234 		if (is_fpga(sc))
5235 			snprintf(cfg_file, sizeof(t4_cfg_file), FPGA_CF);
5236 	}
5237 
5238 	if (strncmp(cfg_file, DEFAULT_CF, sizeof(t4_cfg_file)) == 0) {
5239 		if (dcfg == NULL) {
5240 			device_printf(sc->dev,
5241 			    "KLD with default config is not available.\n");
5242 			rc = ENOENT;
5243 			goto done;
5244 		}
5245 		cfdata = dcfg->data;
5246 		cflen = dcfg->datasize & ~3;
5247 	} else {
5248 		char s[32];
5249 
5250 		fw_info = find_fw_info(chip_id(sc));
5251 		if (fw_info == NULL) {
5252 			device_printf(sc->dev,
5253 			    "unable to look up firmware information for chip %d.\n",
5254 			    chip_id(sc));
5255 			rc = EINVAL;
5256 			goto done;
5257 		}
5258 		snprintf(s, sizeof(s), "%s_%s", fw_info->kld_name, cfg_file);
5259 
5260 		rcfg = firmware_get(s);
5261 		if (rcfg == NULL) {
5262 			device_printf(sc->dev,
5263 			    "unable to load module \"%s\" for configuration "
5264 			    "profile \"%s\".\n", s, cfg_file);
5265 			rc = ENOENT;
5266 			goto done;
5267 		}
5268 		cfdata = rcfg->data;
5269 		cflen = rcfg->datasize & ~3;
5270 	}
5271 
5272 	if (cflen > maxlen) {
5273 		device_printf(sc->dev,
5274 		    "config file too long (%d, max allowed is %d).\n",
5275 		    cflen, maxlen);
5276 		rc = EINVAL;
5277 		goto done;
5278 	}
5279 
5280 	rc = validate_mt_off_len(sc, mtype, moff, cflen, &addr);
5281 	if (rc != 0) {
5282 		device_printf(sc->dev,
5283 		    "%s: addr (%d/0x%x) or len %d is not valid: %d.\n",
5284 		    __func__, mtype, moff, cflen, rc);
5285 		rc = EINVAL;
5286 		goto done;
5287 	}
5288 	write_via_memwin(sc, 2, addr, cfdata, cflen);
5289 done:
5290 	if (rcfg != NULL)
5291 		firmware_put(rcfg, FIRMWARE_UNLOAD);
5292 	unload_fw_module(sc, dcfg, NULL);
5293 	return (rc);
5294 }
5295 
5296 struct caps_allowed {
5297 	uint16_t nbmcaps;
5298 	uint16_t linkcaps;
5299 	uint16_t switchcaps;
5300 	uint16_t nvmecaps;
5301 	uint16_t niccaps;
5302 	uint16_t toecaps;
5303 	uint16_t rdmacaps;
5304 	uint16_t cryptocaps;
5305 	uint16_t iscsicaps;
5306 	uint16_t fcoecaps;
5307 };
5308 
5309 #define FW_PARAM_DEV(param) \
5310 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
5311 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
5312 #define FW_PARAM_PFVF(param) \
5313 	(V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
5314 	 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param))
5315 
5316 /*
5317  * Provide a configuration profile to the firmware and have it initialize the
5318  * chip accordingly.  This may involve uploading a configuration file to the
5319  * card.
5320  */
5321 static int
apply_cfg_and_initialize(struct adapter * sc,char * cfg_file,const struct caps_allowed * caps_allowed)5322 apply_cfg_and_initialize(struct adapter *sc, char *cfg_file,
5323     const struct caps_allowed *caps_allowed)
5324 {
5325 	int rc;
5326 	struct fw_caps_config_cmd caps;
5327 	uint32_t mtype, moff, finicsum, cfcsum, param, val;
5328 	unsigned int maxlen = 0;
5329 	const int cfg_addr = t4_flash_cfg_addr(sc, &maxlen);
5330 
5331 	rc = -t4_fw_reset(sc, sc->mbox, F_PIORSTMODE | F_PIORST);
5332 	if (rc != 0) {
5333 		device_printf(sc->dev, "firmware reset failed: %d.\n", rc);
5334 		return (rc);
5335 	}
5336 
5337 	bzero(&caps, sizeof(caps));
5338 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5339 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5340 	if (strncmp(cfg_file, BUILTIN_CF, sizeof(t4_cfg_file)) == 0) {
5341 		mtype = 0;
5342 		moff = 0;
5343 		caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5344 	} else if (strncmp(cfg_file, FLASH_CF, sizeof(t4_cfg_file)) == 0) {
5345 		mtype = FW_MEMTYPE_FLASH;
5346 		moff = cfg_addr;
5347 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5348 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5349 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5350 		    FW_LEN16(caps));
5351 	} else {
5352 		/*
5353 		 * Ask the firmware where it wants us to upload the config file.
5354 		 */
5355 		param = FW_PARAM_DEV(CF);
5356 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5357 		if (rc != 0) {
5358 			/* No support for config file?  Shouldn't happen. */
5359 			device_printf(sc->dev,
5360 			    "failed to query config file location: %d.\n", rc);
5361 			goto done;
5362 		}
5363 		mtype = G_FW_PARAMS_PARAM_Y(val);
5364 		moff = G_FW_PARAMS_PARAM_Z(val) << 16;
5365 		caps.cfvalid_to_len16 = htobe32(F_FW_CAPS_CONFIG_CMD_CFVALID |
5366 		    V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
5367 		    V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(moff >> 16) |
5368 		    FW_LEN16(caps));
5369 
5370 		rc = copy_cfg_file_to_card(sc, cfg_file, mtype, moff, maxlen);
5371 		if (rc != 0) {
5372 			device_printf(sc->dev,
5373 			    "failed to upload config file to card: %d.\n", rc);
5374 			goto done;
5375 		}
5376 	}
5377 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5378 	if (rc != 0) {
5379 		device_printf(sc->dev, "failed to pre-process config file: %d "
5380 		    "(mtype %d, moff 0x%x).\n", rc, mtype, moff);
5381 		goto done;
5382 	}
5383 
5384 	finicsum = be32toh(caps.finicsum);
5385 	cfcsum = be32toh(caps.cfcsum);	/* actual */
5386 	if (finicsum != cfcsum) {
5387 		device_printf(sc->dev,
5388 		    "WARNING: config file checksum mismatch: %08x %08x\n",
5389 		    finicsum, cfcsum);
5390 	}
5391 	sc->cfcsum = cfcsum;
5392 	snprintf(sc->cfg_file, sizeof(sc->cfg_file), "%s", cfg_file);
5393 
5394 	/*
5395 	 * Let the firmware know what features will (not) be used so it can tune
5396 	 * things accordingly.
5397 	 */
5398 #define LIMIT_CAPS(x) do { \
5399 	caps.x##caps &= htobe16(caps_allowed->x##caps); \
5400 } while (0)
5401 	LIMIT_CAPS(nbm);
5402 	LIMIT_CAPS(link);
5403 	LIMIT_CAPS(switch);
5404 	LIMIT_CAPS(nvme);
5405 	LIMIT_CAPS(nic);
5406 	LIMIT_CAPS(toe);
5407 	LIMIT_CAPS(rdma);
5408 	LIMIT_CAPS(crypto);
5409 	LIMIT_CAPS(iscsi);
5410 	LIMIT_CAPS(fcoe);
5411 #undef LIMIT_CAPS
5412 	if (caps.niccaps & htobe16(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5413 		/*
5414 		 * TOE and hashfilters are mutually exclusive.  It is a config
5415 		 * file or firmware bug if both are reported as available.  Try
5416 		 * to cope with the situation in non-debug builds by disabling
5417 		 * TOE.
5418 		 */
5419 		MPASS(caps.toecaps == 0);
5420 
5421 		caps.toecaps = 0;
5422 		caps.rdmacaps = 0;
5423 		caps.iscsicaps = 0;
5424 		caps.nvmecaps = 0;
5425 	}
5426 
5427 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5428 	    F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
5429 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5430 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), NULL);
5431 	if (rc != 0) {
5432 		device_printf(sc->dev,
5433 		    "failed to process config file: %d.\n", rc);
5434 		goto done;
5435 	}
5436 
5437 	t4_tweak_chip_settings(sc);
5438 	set_params__pre_init(sc);
5439 
5440 	/* get basic stuff going */
5441 	rc = -t4_fw_initialize(sc, sc->mbox);
5442 	if (rc != 0) {
5443 		device_printf(sc->dev, "fw_initialize failed: %d.\n", rc);
5444 		goto done;
5445 	}
5446 done:
5447 	return (rc);
5448 }
5449 
5450 /*
5451  * Partition chip resources for use between various PFs, VFs, etc.
5452  */
5453 static int
partition_resources(struct adapter * sc)5454 partition_resources(struct adapter *sc)
5455 {
5456 	char cfg_file[sizeof(t4_cfg_file)];
5457 	struct caps_allowed caps_allowed;
5458 	int rc;
5459 	bool fallback;
5460 
5461 	/* Only the master driver gets to configure the chip resources. */
5462 	MPASS(sc->flags & MASTER_PF);
5463 
5464 #define COPY_CAPS(x) do { \
5465 	caps_allowed.x##caps = t4_##x##caps_allowed; \
5466 } while (0)
5467 	bzero(&caps_allowed, sizeof(caps_allowed));
5468 	COPY_CAPS(nbm);
5469 	COPY_CAPS(link);
5470 	COPY_CAPS(switch);
5471 	COPY_CAPS(nvme);
5472 	COPY_CAPS(nic);
5473 	COPY_CAPS(toe);
5474 	COPY_CAPS(rdma);
5475 	COPY_CAPS(crypto);
5476 	COPY_CAPS(iscsi);
5477 	COPY_CAPS(fcoe);
5478 	fallback = sc->debug_flags & DF_DISABLE_CFG_RETRY ? false : true;
5479 	snprintf(cfg_file, sizeof(cfg_file), "%s", t4_cfg_file);
5480 retry:
5481 	rc = apply_cfg_and_initialize(sc, cfg_file, &caps_allowed);
5482 	if (rc != 0 && fallback) {
5483 		dump_devlog(sc);
5484 		device_printf(sc->dev,
5485 		    "failed (%d) to configure card with \"%s\" profile, "
5486 		    "will fall back to a basic configuration and retry.\n",
5487 		    rc, cfg_file);
5488 		snprintf(cfg_file, sizeof(cfg_file), "%s", BUILTIN_CF);
5489 		bzero(&caps_allowed, sizeof(caps_allowed));
5490 		COPY_CAPS(switch);
5491 		caps_allowed.niccaps = FW_CAPS_CONFIG_NIC;
5492 		fallback = false;
5493 		goto retry;
5494 	}
5495 #undef COPY_CAPS
5496 	return (rc);
5497 }
5498 
5499 /*
5500  * Retrieve parameters that are needed (or nice to have) very early.
5501  */
5502 static int
get_params__pre_init(struct adapter * sc)5503 get_params__pre_init(struct adapter *sc)
5504 {
5505 	int rc;
5506 	uint32_t param[2], val[2];
5507 
5508 	t4_get_version_info(sc);
5509 
5510 	snprintf(sc->fw_version, sizeof(sc->fw_version), "%u.%u.%u.%u",
5511 	    G_FW_HDR_FW_VER_MAJOR(sc->params.fw_vers),
5512 	    G_FW_HDR_FW_VER_MINOR(sc->params.fw_vers),
5513 	    G_FW_HDR_FW_VER_MICRO(sc->params.fw_vers),
5514 	    G_FW_HDR_FW_VER_BUILD(sc->params.fw_vers));
5515 
5516 	snprintf(sc->bs_version, sizeof(sc->bs_version), "%u.%u.%u.%u",
5517 	    G_FW_HDR_FW_VER_MAJOR(sc->params.bs_vers),
5518 	    G_FW_HDR_FW_VER_MINOR(sc->params.bs_vers),
5519 	    G_FW_HDR_FW_VER_MICRO(sc->params.bs_vers),
5520 	    G_FW_HDR_FW_VER_BUILD(sc->params.bs_vers));
5521 
5522 	snprintf(sc->tp_version, sizeof(sc->tp_version), "%u.%u.%u.%u",
5523 	    G_FW_HDR_FW_VER_MAJOR(sc->params.tp_vers),
5524 	    G_FW_HDR_FW_VER_MINOR(sc->params.tp_vers),
5525 	    G_FW_HDR_FW_VER_MICRO(sc->params.tp_vers),
5526 	    G_FW_HDR_FW_VER_BUILD(sc->params.tp_vers));
5527 
5528 	snprintf(sc->er_version, sizeof(sc->er_version), "%u.%u.%u.%u",
5529 	    G_FW_HDR_FW_VER_MAJOR(sc->params.er_vers),
5530 	    G_FW_HDR_FW_VER_MINOR(sc->params.er_vers),
5531 	    G_FW_HDR_FW_VER_MICRO(sc->params.er_vers),
5532 	    G_FW_HDR_FW_VER_BUILD(sc->params.er_vers));
5533 
5534 	param[0] = FW_PARAM_DEV(PORTVEC);
5535 	param[1] = FW_PARAM_DEV(CCLK);
5536 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5537 	if (rc != 0) {
5538 		device_printf(sc->dev,
5539 		    "failed to query parameters (pre_init): %d.\n", rc);
5540 		return (rc);
5541 	}
5542 
5543 	sc->params.portvec = val[0];
5544 	sc->params.nports = bitcount32(val[0]);
5545 	sc->params.vpd.cclk = val[1];
5546 
5547 	/* Read device log parameters. */
5548 	rc = -t4_init_devlog_ncores_params(sc, 1);
5549 	if (rc == 0)
5550 		fixup_devlog_params(sc);
5551 	else {
5552 		device_printf(sc->dev,
5553 		    "failed to get devlog parameters: %d.\n", rc);
5554 		rc = 0;	/* devlog isn't critical for device operation */
5555 	}
5556 
5557 	return (rc);
5558 }
5559 
5560 /*
5561  * Any params that need to be set before FW_INITIALIZE.
5562  */
5563 static int
set_params__pre_init(struct adapter * sc)5564 set_params__pre_init(struct adapter *sc)
5565 {
5566 	int rc = 0;
5567 	uint32_t param, val;
5568 
5569 	if (chip_id(sc) >= CHELSIO_T6) {
5570 		param = FW_PARAM_DEV(HPFILTER_REGION_SUPPORT);
5571 		val = 1;
5572 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5573 		/* firmwares < 1.20.1.0 do not have this param. */
5574 		if (rc == FW_EINVAL &&
5575 		    sc->params.fw_vers < FW_VERSION32(1, 20, 1, 0)) {
5576 			rc = 0;
5577 		}
5578 		if (rc != 0) {
5579 			device_printf(sc->dev,
5580 			    "failed to enable high priority filters :%d.\n",
5581 			    rc);
5582 		}
5583 
5584 		param = FW_PARAM_DEV(PPOD_EDRAM);
5585 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5586 		if (rc == 0 && val == 1) {
5587 			rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param,
5588 			    &val);
5589 			if (rc != 0) {
5590 				device_printf(sc->dev,
5591 				    "failed to set PPOD_EDRAM: %d.\n", rc);
5592 			}
5593 		}
5594 	}
5595 
5596 	/* Enable opaque VIIDs with firmwares that support it. */
5597 	param = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5598 	val = 1;
5599 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
5600 	if (rc == 0 && val == 1)
5601 		sc->params.viid_smt_extn_support = true;
5602 	else
5603 		sc->params.viid_smt_extn_support = false;
5604 
5605 	return (rc);
5606 }
5607 
5608 /*
5609  * Retrieve various parameters that are of interest to the driver.  The device
5610  * has been initialized by the firmware at this point.
5611  */
5612 static int
get_params__post_init(struct adapter * sc)5613 get_params__post_init(struct adapter *sc)
5614 {
5615 	int rc;
5616 	uint32_t param[7], val[7];
5617 	struct fw_caps_config_cmd caps;
5618 
5619 	param[0] = FW_PARAM_PFVF(IQFLINT_START);
5620 	param[1] = FW_PARAM_PFVF(EQ_START);
5621 	param[2] = FW_PARAM_PFVF(FILTER_START);
5622 	param[3] = FW_PARAM_PFVF(FILTER_END);
5623 	param[4] = FW_PARAM_PFVF(L2T_START);
5624 	param[5] = FW_PARAM_PFVF(L2T_END);
5625 	param[6] = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
5626 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
5627 	    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
5628 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 7, param, val);
5629 	if (rc != 0) {
5630 		device_printf(sc->dev,
5631 		    "failed to query parameters (post_init): %d.\n", rc);
5632 		return (rc);
5633 	}
5634 
5635 	sc->sge.iq_start = val[0];
5636 	sc->sge.eq_start = val[1];
5637 	if ((int)val[3] > (int)val[2]) {
5638 		sc->tids.ftid_base = val[2];
5639 		sc->tids.ftid_end = val[3];
5640 		sc->tids.nftids = val[3] - val[2] + 1;
5641 	}
5642 	sc->vres.l2t.start = val[4];
5643 	sc->vres.l2t.size = val[5] - val[4] + 1;
5644 	/* val[5] is the last hwidx and it must not collide with F_SYNC_WR */
5645 	if (sc->vres.l2t.size > 0)
5646 		MPASS(fls(val[5]) <= S_SYNC_WR);
5647 	sc->params.core_vdd = val[6];
5648 
5649 	param[0] = FW_PARAM_PFVF(IQFLINT_END);
5650 	param[1] = FW_PARAM_PFVF(EQ_END);
5651 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5652 	if (rc != 0) {
5653 		device_printf(sc->dev,
5654 		    "failed to query parameters (post_init2): %d.\n", rc);
5655 		return (rc);
5656 	}
5657 	MPASS((int)val[0] >= sc->sge.iq_start);
5658 	sc->sge.iqmap_sz = val[0] - sc->sge.iq_start + 1;
5659 	MPASS((int)val[1] >= sc->sge.eq_start);
5660 	sc->sge.eqmap_sz = val[1] - sc->sge.eq_start + 1;
5661 
5662 	if (chip_id(sc) >= CHELSIO_T6) {
5663 
5664 		sc->tids.tid_base = t4_read_reg(sc,
5665 		    A_LE_DB_ACTIVE_TABLE_START_INDEX);
5666 
5667 		param[0] = FW_PARAM_PFVF(HPFILTER_START);
5668 		param[1] = FW_PARAM_PFVF(HPFILTER_END);
5669 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5670 		if (rc != 0) {
5671 			device_printf(sc->dev,
5672 			   "failed to query hpfilter parameters: %d.\n", rc);
5673 			return (rc);
5674 		}
5675 		if ((int)val[1] > (int)val[0]) {
5676 			sc->tids.hpftid_base = val[0];
5677 			sc->tids.hpftid_end = val[1];
5678 			sc->tids.nhpftids = val[1] - val[0] + 1;
5679 
5680 			/*
5681 			 * These should go off if the layout changes and the
5682 			 * driver needs to catch up.
5683 			 */
5684 			MPASS(sc->tids.hpftid_base == 0);
5685 			MPASS(sc->tids.tid_base == sc->tids.nhpftids);
5686 		}
5687 
5688 		param[0] = FW_PARAM_PFVF(RAWF_START);
5689 		param[1] = FW_PARAM_PFVF(RAWF_END);
5690 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5691 		if (rc != 0) {
5692 			device_printf(sc->dev,
5693 			   "failed to query rawf parameters: %d.\n", rc);
5694 			return (rc);
5695 		}
5696 		if ((int)val[1] > (int)val[0]) {
5697 			sc->rawf_base = val[0];
5698 			sc->nrawf = val[1] - val[0] + 1;
5699 		}
5700 	}
5701 
5702 	if (sc->params.ncores > 1) {
5703 		MPASS(chip_id(sc) >= CHELSIO_T7);
5704 
5705 		param[0] = FW_PARAM_DEV(TID_QID_SEL_MASK);
5706 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5707 		sc->params.tid_qid_sel_mask = rc == 0 ? val[0] : 0;
5708 	}
5709 
5710 	/*
5711 	 * The parameters that follow may not be available on all firmwares.  We
5712 	 * query them individually rather than in a compound query because old
5713 	 * firmwares fail the entire query if an unknown parameter is queried.
5714 	 */
5715 
5716 	/*
5717 	 * MPS buffer group configuration.
5718 	 */
5719 	param[0] = FW_PARAM_DEV(MPSBGMAP);
5720 	val[0] = 0;
5721 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5722 	if (rc == 0)
5723 		sc->params.mps_bg_map = val[0];
5724 	else
5725 		sc->params.mps_bg_map = UINT32_MAX;	/* Not a legal value. */
5726 
5727 	param[0] = FW_PARAM_DEV(TPCHMAP);
5728 	val[0] = 0;
5729 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5730 	if (rc == 0)
5731 		sc->params.tp_ch_map = val[0];
5732 	else
5733 		sc->params.tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5734 
5735 	param[0] = FW_PARAM_DEV(TX_TPCHMAP);
5736 	val[0] = 0;
5737 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5738 	if (rc == 0)
5739 		sc->params.tx_tp_ch_map = val[0];
5740 	else
5741 		sc->params.tx_tp_ch_map = UINT32_MAX;	/* Not a legal value. */
5742 
5743 	/*
5744 	 * Determine whether the firmware supports the filter2 work request.
5745 	 */
5746 	param[0] = FW_PARAM_DEV(FILTER2_WR);
5747 	val[0] = 0;
5748 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5749 	if (rc == 0)
5750 		sc->params.filter2_wr_support = val[0] != 0;
5751 	else
5752 		sc->params.filter2_wr_support = 0;
5753 
5754 	/*
5755 	 * Find out whether we're allowed to use the ULPTX MEMWRITE DSGL.
5756 	 */
5757 	param[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5758 	val[0] = 0;
5759 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5760 	if (rc == 0)
5761 		sc->params.ulptx_memwrite_dsgl = val[0] != 0;
5762 	else
5763 		sc->params.ulptx_memwrite_dsgl = false;
5764 
5765 	/* FW_RI_FR_NSMR_TPTE_WR support */
5766 	param[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5767 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5768 	if (rc == 0)
5769 		sc->params.fr_nsmr_tpte_wr_support = val[0] != 0;
5770 	else
5771 		sc->params.fr_nsmr_tpte_wr_support = false;
5772 
5773 	/* Support for 512 SGL entries per FR MR. */
5774 	param[0] = FW_PARAM_DEV(DEV_512SGL_MR);
5775 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5776 	if (rc == 0)
5777 		sc->params.dev_512sgl_mr = val[0] != 0;
5778 	else
5779 		sc->params.dev_512sgl_mr = false;
5780 
5781 	param[0] = FW_PARAM_PFVF(MAX_PKTS_PER_ETH_TX_PKTS_WR);
5782 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5783 	if (rc == 0)
5784 		sc->params.max_pkts_per_eth_tx_pkts_wr = val[0];
5785 	else
5786 		sc->params.max_pkts_per_eth_tx_pkts_wr = 15;
5787 
5788 	param[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5789 	rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5790 	if (rc == 0) {
5791 		MPASS(val[0] > 0 && val[0] < 256);	/* nsched_cls is 8b */
5792 		sc->params.nsched_cls = val[0];
5793 	} else
5794 		sc->params.nsched_cls = sc->chip_params->nsched_cls;
5795 
5796 	/* get capabilites */
5797 	bzero(&caps, sizeof(caps));
5798 	caps.op_to_write = htobe32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
5799 	    F_FW_CMD_REQUEST | F_FW_CMD_READ);
5800 	caps.cfvalid_to_len16 = htobe32(FW_LEN16(caps));
5801 	rc = -t4_wr_mbox(sc, sc->mbox, &caps, sizeof(caps), &caps);
5802 	if (rc != 0) {
5803 		device_printf(sc->dev,
5804 		    "failed to get card capabilities: %d.\n", rc);
5805 		return (rc);
5806 	}
5807 
5808 #define READ_CAPS(x) do { \
5809 	sc->x = htobe16(caps.x); \
5810 } while (0)
5811 	READ_CAPS(nbmcaps);
5812 	READ_CAPS(linkcaps);
5813 	READ_CAPS(switchcaps);
5814 	READ_CAPS(nvmecaps);
5815 	READ_CAPS(niccaps);
5816 	READ_CAPS(toecaps);
5817 	READ_CAPS(rdmacaps);
5818 	READ_CAPS(cryptocaps);
5819 	READ_CAPS(iscsicaps);
5820 	READ_CAPS(fcoecaps);
5821 
5822 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_HASHFILTER) {
5823 		MPASS(chip_id(sc) > CHELSIO_T4);
5824 		MPASS(sc->toecaps == 0);
5825 		sc->toecaps = 0;
5826 
5827 		param[0] = FW_PARAM_DEV(NTID);
5828 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, param, val);
5829 		if (rc != 0) {
5830 			device_printf(sc->dev,
5831 			    "failed to query HASHFILTER parameters: %d.\n", rc);
5832 			return (rc);
5833 		}
5834 		sc->tids.ntids = val[0];
5835 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5836 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5837 			sc->tids.ntids -= sc->tids.nhpftids;
5838 		}
5839 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5840 		sc->params.hash_filter = 1;
5841 	}
5842 	if (sc->niccaps & FW_CAPS_CONFIG_NIC_ETHOFLD) {
5843 		param[0] = FW_PARAM_PFVF(ETHOFLD_START);
5844 		param[1] = FW_PARAM_PFVF(ETHOFLD_END);
5845 		param[2] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5846 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 3, param, val);
5847 		if (rc != 0) {
5848 			device_printf(sc->dev,
5849 			    "failed to query NIC parameters: %d.\n", rc);
5850 			return (rc);
5851 		}
5852 		if ((int)val[1] > (int)val[0]) {
5853 			sc->tids.etid_base = val[0];
5854 			sc->tids.etid_end = val[1];
5855 			sc->tids.netids = val[1] - val[0] + 1;
5856 			sc->params.eo_wr_cred = val[2];
5857 			sc->params.ethoffload = 1;
5858 		}
5859 	}
5860 	if (sc->toecaps) {
5861 		/* query offload-related parameters */
5862 		param[0] = FW_PARAM_DEV(NTID);
5863 		param[1] = FW_PARAM_PFVF(SERVER_START);
5864 		param[2] = FW_PARAM_PFVF(SERVER_END);
5865 		param[3] = FW_PARAM_PFVF(TDDP_START);
5866 		param[4] = FW_PARAM_PFVF(TDDP_END);
5867 		param[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5868 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5869 		if (rc != 0) {
5870 			device_printf(sc->dev,
5871 			    "failed to query TOE parameters: %d.\n", rc);
5872 			return (rc);
5873 		}
5874 		sc->tids.ntids = val[0];
5875 		if (sc->params.fw_vers < FW_VERSION32(1, 20, 5, 0)) {
5876 			MPASS(sc->tids.ntids >= sc->tids.nhpftids);
5877 			sc->tids.ntids -= sc->tids.nhpftids;
5878 		}
5879 		sc->tids.natids = min(sc->tids.ntids / 2, MAX_ATIDS);
5880 		if ((int)val[2] > (int)val[1]) {
5881 			sc->tids.stid_base = val[1];
5882 			sc->tids.nstids = val[2] - val[1] + 1;
5883 		}
5884 		sc->vres.ddp.start = val[3];
5885 		sc->vres.ddp.size = val[4] - val[3] + 1;
5886 		sc->params.ofldq_wr_cred = val[5];
5887 		sc->params.offload = 1;
5888 	} else {
5889 		/*
5890 		 * The firmware attempts memfree TOE configuration for -SO cards
5891 		 * and will report toecaps=0 if it runs out of resources (this
5892 		 * depends on the config file).  It may not report 0 for other
5893 		 * capabilities dependent on the TOE in this case.  Set them to
5894 		 * 0 here so that the driver doesn't bother tracking resources
5895 		 * that will never be used.
5896 		 */
5897 		sc->iscsicaps = 0;
5898 		sc->nvmecaps = 0;
5899 		sc->rdmacaps = 0;
5900 	}
5901 	if (sc->nvmecaps || sc->rdmacaps) {
5902 		param[0] = FW_PARAM_PFVF(STAG_START);
5903 		param[1] = FW_PARAM_PFVF(STAG_END);
5904 		param[2] = FW_PARAM_PFVF(PBL_START);
5905 		param[3] = FW_PARAM_PFVF(PBL_END);
5906 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 4, param, val);
5907 		if (rc != 0) {
5908 			device_printf(sc->dev,
5909 			    "failed to query NVMe/RDMA parameters: %d.\n", rc);
5910 			return (rc);
5911 		}
5912 		sc->vres.stag.start = val[0];
5913 		sc->vres.stag.size = val[1] - val[0] + 1;
5914 		sc->vres.pbl.start = val[2];
5915 		sc->vres.pbl.size = val[3] - val[2] + 1;
5916 	}
5917 	if (sc->rdmacaps) {
5918 		param[0] = FW_PARAM_PFVF(RQ_START);
5919 		param[1] = FW_PARAM_PFVF(RQ_END);
5920 		param[2] = FW_PARAM_PFVF(SQRQ_START);
5921 		param[3] = FW_PARAM_PFVF(SQRQ_END);
5922 		param[4] = FW_PARAM_PFVF(CQ_START);
5923 		param[5] = FW_PARAM_PFVF(CQ_END);
5924 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5925 		if (rc != 0) {
5926 			device_printf(sc->dev,
5927 			    "failed to query RDMA parameters(1): %d.\n", rc);
5928 			return (rc);
5929 		}
5930 		sc->vres.rq.start = val[0];
5931 		sc->vres.rq.size = val[1] - val[0] + 1;
5932 		sc->vres.qp.start = val[2];
5933 		sc->vres.qp.size = val[3] - val[2] + 1;
5934 		sc->vres.cq.start = val[4];
5935 		sc->vres.cq.size = val[5] - val[4] + 1;
5936 
5937 		param[0] = FW_PARAM_PFVF(OCQ_START);
5938 		param[1] = FW_PARAM_PFVF(OCQ_END);
5939 		param[2] = FW_PARAM_PFVF(SRQ_START);
5940 		param[3] = FW_PARAM_PFVF(SRQ_END);
5941 		param[4] = FW_PARAM_DEV(MAXORDIRD_QP);
5942 		param[5] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5943 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 6, param, val);
5944 		if (rc != 0) {
5945 			device_printf(sc->dev,
5946 			    "failed to query RDMA parameters(2): %d.\n", rc);
5947 			return (rc);
5948 		}
5949 		sc->vres.ocq.start = val[0];
5950 		sc->vres.ocq.size = val[1] - val[0] + 1;
5951 		sc->vres.srq.start = val[2];
5952 		sc->vres.srq.size = val[3] - val[2] + 1;
5953 		sc->params.max_ordird_qp = val[4];
5954 		sc->params.max_ird_adapter = val[5];
5955 	}
5956 	if (sc->iscsicaps) {
5957 		param[0] = FW_PARAM_PFVF(ISCSI_START);
5958 		param[1] = FW_PARAM_PFVF(ISCSI_END);
5959 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5960 		if (rc != 0) {
5961 			device_printf(sc->dev,
5962 			    "failed to query iSCSI parameters: %d.\n", rc);
5963 			return (rc);
5964 		}
5965 		sc->vres.iscsi.start = val[0];
5966 		sc->vres.iscsi.size = val[1] - val[0] + 1;
5967 	}
5968 	if (sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS) {
5969 		param[0] = FW_PARAM_PFVF(TLS_START);
5970 		param[1] = FW_PARAM_PFVF(TLS_END);
5971 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 2, param, val);
5972 		if (rc != 0) {
5973 			device_printf(sc->dev,
5974 			    "failed to query TLS parameters: %d.\n", rc);
5975 			return (rc);
5976 		}
5977 		sc->vres.key.start = val[0];
5978 		sc->vres.key.size = val[1] - val[0] + 1;
5979 	}
5980 
5981 	/*
5982 	 * We've got the params we wanted to query directly from the firmware.
5983 	 * Grab some others via other means.
5984 	 */
5985 	t4_init_sge_params(sc);
5986 	t4_init_tp_params(sc);
5987 	t4_read_mtu_tbl(sc, sc->params.mtus, NULL);
5988 	t4_load_mtus(sc, sc->params.mtus, sc->params.a_wnd, sc->params.b_wnd);
5989 
5990 	rc = t4_verify_chip_settings(sc);
5991 	if (rc != 0)
5992 		return (rc);
5993 	t4_init_rx_buf_info(sc);
5994 
5995 	return (rc);
5996 }
5997 
5998 #ifdef KERN_TLS
5999 static void
ktls_tick(void * arg)6000 ktls_tick(void *arg)
6001 {
6002 	struct adapter *sc;
6003 	uint32_t tstamp;
6004 
6005 	sc = arg;
6006 	tstamp = tcp_ts_getticks();
6007 	t4_write_reg(sc, A_TP_SYNC_TIME_HI, tstamp >> 1);
6008 	t4_write_reg(sc, A_TP_SYNC_TIME_LO, tstamp << 31);
6009 	callout_schedule_sbt(&sc->ktls_tick, SBT_1MS, 0, C_HARDCLOCK);
6010 }
6011 
6012 static int
t6_config_kern_tls(struct adapter * sc,bool enable)6013 t6_config_kern_tls(struct adapter *sc, bool enable)
6014 {
6015 	int rc;
6016 	uint32_t param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
6017 	    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_KTLS_HW) |
6018 	    V_FW_PARAMS_PARAM_Y(enable ? 1 : 0) |
6019 	    V_FW_PARAMS_PARAM_Z(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
6020 
6021 	rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &param);
6022 	if (rc != 0) {
6023 		CH_ERR(sc, "failed to %s NIC TLS: %d\n",
6024 		    enable ?  "enable" : "disable", rc);
6025 		return (rc);
6026 	}
6027 
6028 	if (enable) {
6029 		sc->flags |= KERN_TLS_ON;
6030 		callout_reset_sbt(&sc->ktls_tick, SBT_1MS, 0, ktls_tick, sc,
6031 		    C_HARDCLOCK);
6032 	} else {
6033 		sc->flags &= ~KERN_TLS_ON;
6034 		callout_stop(&sc->ktls_tick);
6035 	}
6036 
6037 	return (rc);
6038 }
6039 #endif
6040 
6041 static int
set_params__post_init(struct adapter * sc)6042 set_params__post_init(struct adapter *sc)
6043 {
6044 	uint32_t mask, param, val;
6045 #ifdef TCP_OFFLOAD
6046 	int i, v, shift;
6047 #endif
6048 
6049 	/* ask for encapsulated CPLs */
6050 	param = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
6051 	val = 1;
6052 	(void)t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
6053 
6054 	/* Enable 32b port caps if the firmware supports it. */
6055 	param = FW_PARAM_PFVF(PORT_CAPS32);
6056 	val = 1;
6057 	if (t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val) == 0)
6058 		sc->params.port_caps32 = 1;
6059 
6060 	/* Let filter + maskhash steer to a part of the VI's RSS region. */
6061 	val = 1 << (G_MASKSIZE(t4_read_reg(sc, A_TP_RSS_CONFIG_TNL)) - 1);
6062 	t4_set_reg_field(sc, A_TP_RSS_CONFIG_TNL, V_MASKFILTER(M_MASKFILTER),
6063 	    V_MASKFILTER(val - 1));
6064 
6065 	mask = F_DROPERRORANY | F_DROPERRORMAC | F_DROPERRORIPVER |
6066 	    F_DROPERRORFRAG | F_DROPERRORATTACK | F_DROPERRORETHHDRLEN |
6067 	    F_DROPERRORIPHDRLEN | F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6068 	    F_DROPERRORTCPOPT | F_DROPERRORCSUMIP | F_DROPERRORCSUM;
6069 	val = 0;
6070 	if (chip_id(sc) < CHELSIO_T6 && t4_attack_filter != 0) {
6071 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_ATTACKFILTERENABLE,
6072 		    F_ATTACKFILTERENABLE);
6073 		val |= F_DROPERRORATTACK;
6074 	}
6075 	if (t4_drop_ip_fragments != 0) {
6076 		t4_set_reg_field(sc, A_TP_GLOBAL_CONFIG, F_FRAGMENTDROP,
6077 		    F_FRAGMENTDROP);
6078 		val |= F_DROPERRORFRAG;
6079 	}
6080 	if (t4_drop_pkts_with_l2_errors != 0)
6081 		val |= F_DROPERRORMAC | F_DROPERRORETHHDRLEN;
6082 	if (t4_drop_pkts_with_l3_errors != 0) {
6083 		val |= F_DROPERRORIPVER | F_DROPERRORIPHDRLEN |
6084 		    F_DROPERRORCSUMIP;
6085 	}
6086 	if (t4_drop_pkts_with_l4_errors != 0) {
6087 		val |= F_DROPERRORTCPHDRLEN | F_DROPERRORPKTLEN |
6088 		    F_DROPERRORTCPOPT | F_DROPERRORCSUM;
6089 	}
6090 	t4_set_reg_field(sc, A_TP_ERR_CONFIG, mask, val);
6091 
6092 #ifdef TCP_OFFLOAD
6093 	/*
6094 	 * Override the TOE timers with user provided tunables.  This is not the
6095 	 * recommended way to change the timers (the firmware config file is) so
6096 	 * these tunables are not documented.
6097 	 *
6098 	 * All the timer tunables are in microseconds.
6099 	 */
6100 	if (t4_toe_keepalive_idle != 0) {
6101 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_idle);
6102 		v &= M_KEEPALIVEIDLE;
6103 		t4_set_reg_field(sc, A_TP_KEEP_IDLE,
6104 		    V_KEEPALIVEIDLE(M_KEEPALIVEIDLE), V_KEEPALIVEIDLE(v));
6105 	}
6106 	if (t4_toe_keepalive_interval != 0) {
6107 		v = us_to_tcp_ticks(sc, t4_toe_keepalive_interval);
6108 		v &= M_KEEPALIVEINTVL;
6109 		t4_set_reg_field(sc, A_TP_KEEP_INTVL,
6110 		    V_KEEPALIVEINTVL(M_KEEPALIVEINTVL), V_KEEPALIVEINTVL(v));
6111 	}
6112 	if (t4_toe_keepalive_count != 0) {
6113 		v = t4_toe_keepalive_count & M_KEEPALIVEMAXR2;
6114 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6115 		    V_KEEPALIVEMAXR1(M_KEEPALIVEMAXR1) |
6116 		    V_KEEPALIVEMAXR2(M_KEEPALIVEMAXR2),
6117 		    V_KEEPALIVEMAXR1(1) | V_KEEPALIVEMAXR2(v));
6118 	}
6119 	if (t4_toe_rexmt_min != 0) {
6120 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_min);
6121 		v &= M_RXTMIN;
6122 		t4_set_reg_field(sc, A_TP_RXT_MIN,
6123 		    V_RXTMIN(M_RXTMIN), V_RXTMIN(v));
6124 	}
6125 	if (t4_toe_rexmt_max != 0) {
6126 		v = us_to_tcp_ticks(sc, t4_toe_rexmt_max);
6127 		v &= M_RXTMAX;
6128 		t4_set_reg_field(sc, A_TP_RXT_MAX,
6129 		    V_RXTMAX(M_RXTMAX), V_RXTMAX(v));
6130 	}
6131 	if (t4_toe_rexmt_count != 0) {
6132 		v = t4_toe_rexmt_count & M_RXTSHIFTMAXR2;
6133 		t4_set_reg_field(sc, A_TP_SHIFT_CNT,
6134 		    V_RXTSHIFTMAXR1(M_RXTSHIFTMAXR1) |
6135 		    V_RXTSHIFTMAXR2(M_RXTSHIFTMAXR2),
6136 		    V_RXTSHIFTMAXR1(1) | V_RXTSHIFTMAXR2(v));
6137 	}
6138 	for (i = 0; i < nitems(t4_toe_rexmt_backoff); i++) {
6139 		if (t4_toe_rexmt_backoff[i] != -1) {
6140 			v = t4_toe_rexmt_backoff[i] & M_TIMERBACKOFFINDEX0;
6141 			shift = (i & 3) << 3;
6142 			t4_set_reg_field(sc, A_TP_TCP_BACKOFF_REG0 + (i & ~3),
6143 			    M_TIMERBACKOFFINDEX0 << shift, v << shift);
6144 		}
6145 	}
6146 #endif
6147 
6148 	/*
6149 	 * Limit TOE connections to 2 reassembly "islands".  This is
6150 	 * required to permit migrating TOE connections to either
6151 	 * ULP_MODE_TCPDDP or UPL_MODE_TLS.
6152 	 */
6153 	t4_tp_wr_bits_indirect(sc, A_TP_FRAG_CONFIG, V_PASSMODE(M_PASSMODE),
6154 	    V_PASSMODE(2));
6155 
6156 #ifdef KERN_TLS
6157 	if (is_ktls(sc)) {
6158 		sc->tlst.inline_keys = t4_tls_inline_keys;
6159 		if (t4_kern_tls != 0 && is_t6(sc)) {
6160 			sc->tlst.combo_wrs = t4_tls_combo_wrs;
6161 			t6_config_kern_tls(sc, true);
6162 		} else {
6163 			sc->tlst.short_records = t4_tls_short_records;
6164 			sc->tlst.partial_ghash = t4_tls_partial_ghash;
6165 		}
6166 	}
6167 #endif
6168 	return (0);
6169 }
6170 
6171 #undef FW_PARAM_PFVF
6172 #undef FW_PARAM_DEV
6173 
6174 static void
t4_set_desc(struct adapter * sc)6175 t4_set_desc(struct adapter *sc)
6176 {
6177 	struct adapter_params *p = &sc->params;
6178 
6179 	device_set_descf(sc->dev, "Chelsio %s", p->vpd.id);
6180 }
6181 
6182 static inline void
ifmedia_add4(struct ifmedia * ifm,int m)6183 ifmedia_add4(struct ifmedia *ifm, int m)
6184 {
6185 
6186 	ifmedia_add(ifm, m, 0, NULL);
6187 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE, 0, NULL);
6188 	ifmedia_add(ifm, m | IFM_ETH_RXPAUSE, 0, NULL);
6189 	ifmedia_add(ifm, m | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE, 0, NULL);
6190 }
6191 
6192 /*
6193  * This is the selected media, which is not quite the same as the active media.
6194  * The media line in ifconfig is "media: Ethernet selected (active)" if selected
6195  * and active are not the same, and "media: Ethernet selected" otherwise.
6196  */
6197 static void
set_current_media(struct port_info * pi)6198 set_current_media(struct port_info *pi)
6199 {
6200 	struct link_config *lc;
6201 	struct ifmedia *ifm;
6202 	int mword;
6203 	u_int speed;
6204 
6205 	PORT_LOCK_ASSERT_OWNED(pi);
6206 
6207 	/* Leave current media alone if it's already set to IFM_NONE. */
6208 	ifm = &pi->media;
6209 	if (ifm->ifm_cur != NULL &&
6210 	    IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_NONE)
6211 		return;
6212 
6213 	lc = &pi->link_cfg;
6214 	if (lc->requested_aneg != AUTONEG_DISABLE &&
6215 	    lc->pcaps & FW_PORT_CAP32_ANEG) {
6216 		ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
6217 		return;
6218 	}
6219 	mword = IFM_ETHER | IFM_FDX;
6220 	if (lc->requested_fc & PAUSE_TX)
6221 		mword |= IFM_ETH_TXPAUSE;
6222 	if (lc->requested_fc & PAUSE_RX)
6223 		mword |= IFM_ETH_RXPAUSE;
6224 	if (lc->requested_speed == 0)
6225 		speed = port_top_speed(pi) * 1000;	/* Gbps -> Mbps */
6226 	else
6227 		speed = lc->requested_speed;
6228 	mword |= port_mword(pi, speed_to_fwcap(speed));
6229 	ifmedia_set(ifm, mword);
6230 }
6231 
6232 /*
6233  * Returns true if the ifmedia list for the port cannot change.
6234  */
6235 static bool
fixed_ifmedia(struct port_info * pi)6236 fixed_ifmedia(struct port_info *pi)
6237 {
6238 
6239 	return (pi->port_type == FW_PORT_TYPE_BT_SGMII ||
6240 	    pi->port_type == FW_PORT_TYPE_BT_XFI ||
6241 	    pi->port_type == FW_PORT_TYPE_BT_XAUI ||
6242 	    pi->port_type == FW_PORT_TYPE_KX4 ||
6243 	    pi->port_type == FW_PORT_TYPE_KX ||
6244 	    pi->port_type == FW_PORT_TYPE_KR ||
6245 	    pi->port_type == FW_PORT_TYPE_BP_AP ||
6246 	    pi->port_type == FW_PORT_TYPE_BP4_AP ||
6247 	    pi->port_type == FW_PORT_TYPE_BP40_BA ||
6248 	    pi->port_type == FW_PORT_TYPE_KR4_100G ||
6249 	    pi->port_type == FW_PORT_TYPE_KR_SFP28 ||
6250 	    pi->port_type == FW_PORT_TYPE_KR_XLAUI);
6251 }
6252 
6253 static void
build_medialist(struct port_info * pi)6254 build_medialist(struct port_info *pi)
6255 {
6256 	uint32_t ss, speed;
6257 	int unknown, mword, bit;
6258 	struct link_config *lc;
6259 	struct ifmedia *ifm;
6260 
6261 	PORT_LOCK_ASSERT_OWNED(pi);
6262 
6263 	if (pi->flags & FIXED_IFMEDIA)
6264 		return;
6265 
6266 	/*
6267 	 * Rebuild the ifmedia list.
6268 	 */
6269 	ifm = &pi->media;
6270 	ifmedia_removeall(ifm);
6271 	lc = &pi->link_cfg;
6272 	ss = G_FW_PORT_CAP32_SPEED(lc->pcaps); /* Supported Speeds */
6273 	if (__predict_false(ss == 0)) {	/* not supposed to happen. */
6274 		MPASS(ss != 0);
6275 no_media:
6276 		MPASS(LIST_EMPTY(&ifm->ifm_list));
6277 		ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
6278 		ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
6279 		return;
6280 	}
6281 
6282 	unknown = 0;
6283 	for (bit = S_FW_PORT_CAP32_SPEED; bit < fls(ss); bit++) {
6284 		speed = 1 << bit;
6285 		MPASS(speed & M_FW_PORT_CAP32_SPEED);
6286 		if (ss & speed) {
6287 			mword = port_mword(pi, speed);
6288 			if (mword == IFM_NONE) {
6289 				goto no_media;
6290 			} else if (mword == IFM_UNKNOWN)
6291 				unknown++;
6292 			else
6293 				ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | mword);
6294 		}
6295 	}
6296 	if (unknown > 0) /* Add one unknown for all unknown media types. */
6297 		ifmedia_add4(ifm, IFM_ETHER | IFM_FDX | IFM_UNKNOWN);
6298 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
6299 		ifmedia_add(ifm, IFM_ETHER | IFM_AUTO, 0, NULL);
6300 
6301 	set_current_media(pi);
6302 }
6303 
6304 /*
6305  * Initialize the requested fields in the link config based on driver tunables.
6306  */
6307 static void
init_link_config(struct port_info * pi)6308 init_link_config(struct port_info *pi)
6309 {
6310 	struct link_config *lc = &pi->link_cfg;
6311 
6312 	PORT_LOCK_ASSERT_OWNED(pi);
6313 
6314 	lc->requested_caps = 0;
6315 	lc->requested_speed = 0;
6316 
6317 	if (t4_autoneg == 0)
6318 		lc->requested_aneg = AUTONEG_DISABLE;
6319 	else if (t4_autoneg == 1)
6320 		lc->requested_aneg = AUTONEG_ENABLE;
6321 	else
6322 		lc->requested_aneg = AUTONEG_AUTO;
6323 
6324 	lc->requested_fc = t4_pause_settings & (PAUSE_TX | PAUSE_RX |
6325 	    PAUSE_AUTONEG);
6326 
6327 	if (t4_fec & FEC_AUTO)
6328 		lc->requested_fec = FEC_AUTO;
6329 	else if (t4_fec == 0)
6330 		lc->requested_fec = FEC_NONE;
6331 	else {
6332 		/* -1 is handled by the FEC_AUTO block above and not here. */
6333 		lc->requested_fec = t4_fec &
6334 		    (FEC_RS | FEC_BASER_RS | FEC_NONE | FEC_MODULE);
6335 		if (lc->requested_fec == 0)
6336 			lc->requested_fec = FEC_AUTO;
6337 	}
6338 	if (t4_force_fec < 0)
6339 		lc->force_fec = -1;
6340 	else if (t4_force_fec > 0)
6341 		lc->force_fec = 1;
6342 	else
6343 		lc->force_fec = 0;
6344 }
6345 
6346 /*
6347  * Makes sure that all requested settings comply with what's supported by the
6348  * port.  Returns the number of settings that were invalid and had to be fixed.
6349  */
6350 static int
fixup_link_config(struct port_info * pi)6351 fixup_link_config(struct port_info *pi)
6352 {
6353 	int n = 0;
6354 	struct link_config *lc = &pi->link_cfg;
6355 	uint32_t fwspeed;
6356 
6357 	PORT_LOCK_ASSERT_OWNED(pi);
6358 
6359 	/* Speed (when not autonegotiating) */
6360 	if (lc->requested_speed != 0) {
6361 		fwspeed = speed_to_fwcap(lc->requested_speed);
6362 		if ((fwspeed & lc->pcaps) == 0) {
6363 			n++;
6364 			lc->requested_speed = 0;
6365 		}
6366 	}
6367 
6368 	/* Link autonegotiation */
6369 	MPASS(lc->requested_aneg == AUTONEG_ENABLE ||
6370 	    lc->requested_aneg == AUTONEG_DISABLE ||
6371 	    lc->requested_aneg == AUTONEG_AUTO);
6372 	if (lc->requested_aneg == AUTONEG_ENABLE &&
6373 	    !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
6374 		n++;
6375 		lc->requested_aneg = AUTONEG_AUTO;
6376 	}
6377 
6378 	/* Flow control */
6379 	MPASS((lc->requested_fc & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG)) == 0);
6380 	if (lc->requested_fc & PAUSE_TX &&
6381 	    !(lc->pcaps & FW_PORT_CAP32_FC_TX)) {
6382 		n++;
6383 		lc->requested_fc &= ~PAUSE_TX;
6384 	}
6385 	if (lc->requested_fc & PAUSE_RX &&
6386 	    !(lc->pcaps & FW_PORT_CAP32_FC_RX)) {
6387 		n++;
6388 		lc->requested_fc &= ~PAUSE_RX;
6389 	}
6390 	if (!(lc->requested_fc & PAUSE_AUTONEG) &&
6391 	    !(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE)) {
6392 		n++;
6393 		lc->requested_fc |= PAUSE_AUTONEG;
6394 	}
6395 
6396 	/* FEC */
6397 	if ((lc->requested_fec & FEC_RS &&
6398 	    !(lc->pcaps & FW_PORT_CAP32_FEC_RS)) ||
6399 	    (lc->requested_fec & FEC_BASER_RS &&
6400 	    !(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS))) {
6401 		n++;
6402 		lc->requested_fec = FEC_AUTO;
6403 	}
6404 
6405 	return (n);
6406 }
6407 
6408 /*
6409  * Apply the requested L1 settings, which are expected to be valid, to the
6410  * hardware.
6411  */
6412 static int
apply_link_config(struct port_info * pi)6413 apply_link_config(struct port_info *pi)
6414 {
6415 	struct adapter *sc = pi->adapter;
6416 	struct link_config *lc = &pi->link_cfg;
6417 	int rc;
6418 
6419 #ifdef INVARIANTS
6420 	ASSERT_SYNCHRONIZED_OP(sc);
6421 	PORT_LOCK_ASSERT_OWNED(pi);
6422 
6423 	if (lc->requested_aneg == AUTONEG_ENABLE)
6424 		MPASS(lc->pcaps & FW_PORT_CAP32_ANEG);
6425 	if (!(lc->requested_fc & PAUSE_AUTONEG))
6426 		MPASS(lc->pcaps & FW_PORT_CAP32_FORCE_PAUSE);
6427 	if (lc->requested_fc & PAUSE_TX)
6428 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_TX);
6429 	if (lc->requested_fc & PAUSE_RX)
6430 		MPASS(lc->pcaps & FW_PORT_CAP32_FC_RX);
6431 	if (lc->requested_fec & FEC_RS)
6432 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_RS);
6433 	if (lc->requested_fec & FEC_BASER_RS)
6434 		MPASS(lc->pcaps & FW_PORT_CAP32_FEC_BASER_RS);
6435 #endif
6436 	if (!(sc->flags & IS_VF)) {
6437 		rc = -t4_link_l1cfg(sc, sc->mbox, pi->hw_port, lc);
6438 		if (rc != 0) {
6439 			device_printf(pi->dev, "l1cfg failed: %d\n", rc);
6440 			return (rc);
6441 		}
6442 	}
6443 
6444 	/*
6445 	 * An L1_CFG will almost always result in a link-change event if the
6446 	 * link is up, and the driver will refresh the actual fec/fc/etc. when
6447 	 * the notification is processed.  If the link is down then the actual
6448 	 * settings are meaningless.
6449 	 *
6450 	 * This takes care of the case where a change in the L1 settings may not
6451 	 * result in a notification.
6452 	 */
6453 	if (lc->link_ok && !(lc->requested_fc & PAUSE_AUTONEG))
6454 		lc->fc = lc->requested_fc & (PAUSE_TX | PAUSE_RX);
6455 
6456 	return (0);
6457 }
6458 
6459 #define FW_MAC_EXACT_CHUNK	7
6460 struct mcaddr_ctx {
6461 	if_t ifp;
6462 	const uint8_t *mcaddr[FW_MAC_EXACT_CHUNK];
6463 	uint64_t hash;
6464 	int i;
6465 	int del;
6466 	int rc;
6467 };
6468 
6469 static u_int
add_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)6470 add_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6471 {
6472 	struct mcaddr_ctx *ctx = arg;
6473 	struct vi_info *vi = if_getsoftc(ctx->ifp);
6474 	struct port_info *pi = vi->pi;
6475 	struct adapter *sc = pi->adapter;
6476 
6477 	if (ctx->rc < 0)
6478 		return (0);
6479 
6480 	ctx->mcaddr[ctx->i] = LLADDR(sdl);
6481 	MPASS(ETHER_IS_MULTICAST(ctx->mcaddr[ctx->i]));
6482 	ctx->i++;
6483 
6484 	if (ctx->i == FW_MAC_EXACT_CHUNK) {
6485 		ctx->rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid, ctx->del,
6486 		    ctx->i, ctx->mcaddr, NULL, &ctx->hash, 0);
6487 		if (ctx->rc < 0) {
6488 			int j;
6489 
6490 			for (j = 0; j < ctx->i; j++) {
6491 				if_printf(ctx->ifp,
6492 				    "failed to add mc address"
6493 				    " %02x:%02x:%02x:"
6494 				    "%02x:%02x:%02x rc=%d\n",
6495 				    ctx->mcaddr[j][0], ctx->mcaddr[j][1],
6496 				    ctx->mcaddr[j][2], ctx->mcaddr[j][3],
6497 				    ctx->mcaddr[j][4], ctx->mcaddr[j][5],
6498 				    -ctx->rc);
6499 			}
6500 			return (0);
6501 		}
6502 		ctx->del = 0;
6503 		ctx->i = 0;
6504 	}
6505 
6506 	return (1);
6507 }
6508 
6509 /*
6510  * Program the port's XGMAC based on parameters in ifnet.  The caller also
6511  * indicates which parameters should be programmed (the rest are left alone).
6512  */
6513 int
update_mac_settings(if_t ifp,int flags)6514 update_mac_settings(if_t ifp, int flags)
6515 {
6516 	int rc = 0;
6517 	struct vi_info *vi = if_getsoftc(ifp);
6518 	struct port_info *pi = vi->pi;
6519 	struct adapter *sc = pi->adapter;
6520 	int mtu = -1, promisc = -1, allmulti = -1, vlanex = -1;
6521 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
6522 
6523 	ASSERT_SYNCHRONIZED_OP(sc);
6524 	KASSERT(flags, ("%s: not told what to update.", __func__));
6525 
6526 	if (flags & XGMAC_MTU)
6527 		mtu = if_getmtu(ifp);
6528 
6529 	if (flags & XGMAC_PROMISC)
6530 		promisc = if_getflags(ifp) & IFF_PROMISC ? 1 : 0;
6531 
6532 	if (flags & XGMAC_ALLMULTI)
6533 		allmulti = if_getflags(ifp) & IFF_ALLMULTI ? 1 : 0;
6534 
6535 	if (flags & XGMAC_VLANEX)
6536 		vlanex = if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING ? 1 : 0;
6537 
6538 	if (flags & (XGMAC_MTU|XGMAC_PROMISC|XGMAC_ALLMULTI|XGMAC_VLANEX)) {
6539 		rc = -t4_set_rxmode(sc, sc->mbox, vi->viid, mtu, promisc,
6540 		    allmulti, 1, vlanex, false);
6541 		if (rc) {
6542 			if_printf(ifp, "set_rxmode (%x) failed: %d\n", flags,
6543 			    rc);
6544 			return (rc);
6545 		}
6546 	}
6547 
6548 	if (flags & XGMAC_UCADDR) {
6549 		uint8_t ucaddr[ETHER_ADDR_LEN];
6550 
6551 		bcopy(if_getlladdr(ifp), ucaddr, sizeof(ucaddr));
6552 		rc = t4_change_mac(sc, sc->mbox, vi->viid, vi->xact_addr_filt,
6553 		    ucaddr, true, &vi->smt_idx);
6554 		if (rc < 0) {
6555 			rc = -rc;
6556 			if_printf(ifp, "change_mac failed: %d\n", rc);
6557 			return (rc);
6558 		} else {
6559 			vi->xact_addr_filt = rc;
6560 			rc = 0;
6561 		}
6562 	}
6563 
6564 	if (flags & XGMAC_MCADDRS) {
6565 		struct epoch_tracker et;
6566 		struct mcaddr_ctx ctx;
6567 		int j;
6568 
6569 		ctx.ifp = ifp;
6570 		ctx.hash = 0;
6571 		ctx.i = 0;
6572 		ctx.del = 1;
6573 		ctx.rc = 0;
6574 		/*
6575 		 * Unlike other drivers, we accumulate list of pointers into
6576 		 * interface address lists and we need to keep it safe even
6577 		 * after if_foreach_llmaddr() returns, thus we must enter the
6578 		 * network epoch.
6579 		 */
6580 		NET_EPOCH_ENTER(et);
6581 		if_foreach_llmaddr(ifp, add_maddr, &ctx);
6582 		if (ctx.rc < 0) {
6583 			NET_EPOCH_EXIT(et);
6584 			rc = -ctx.rc;
6585 			return (rc);
6586 		}
6587 		if (ctx.i > 0) {
6588 			rc = t4_alloc_mac_filt(sc, sc->mbox, vi->viid,
6589 			    ctx.del, ctx.i, ctx.mcaddr, NULL, &ctx.hash, 0);
6590 			NET_EPOCH_EXIT(et);
6591 			if (rc < 0) {
6592 				rc = -rc;
6593 				for (j = 0; j < ctx.i; j++) {
6594 					if_printf(ifp,
6595 					    "failed to add mcast address"
6596 					    " %02x:%02x:%02x:"
6597 					    "%02x:%02x:%02x rc=%d\n",
6598 					    ctx.mcaddr[j][0], ctx.mcaddr[j][1],
6599 					    ctx.mcaddr[j][2], ctx.mcaddr[j][3],
6600 					    ctx.mcaddr[j][4], ctx.mcaddr[j][5],
6601 					    rc);
6602 				}
6603 				return (rc);
6604 			}
6605 			ctx.del = 0;
6606 		} else
6607 			NET_EPOCH_EXIT(et);
6608 
6609 		rc = -t4_set_addr_hash(sc, sc->mbox, vi->viid, 0, ctx.hash, 0);
6610 		if (rc != 0)
6611 			if_printf(ifp, "failed to set mcast address hash: %d\n",
6612 			    rc);
6613 		if (ctx.del == 0) {
6614 			/* We clobbered the VXLAN entry if there was one. */
6615 			pi->vxlan_tcam_entry = false;
6616 		}
6617 	}
6618 
6619 	if (IS_MAIN_VI(vi) && sc->vxlan_refcount > 0 &&
6620 	    pi->vxlan_tcam_entry == false) {
6621 		rc = t4_alloc_raw_mac_filt(sc, vi->viid, match_all_mac,
6622 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
6623 		    true);
6624 		if (rc < 0) {
6625 			rc = -rc;
6626 			if_printf(ifp, "failed to add VXLAN TCAM entry: %d.\n",
6627 			    rc);
6628 		} else {
6629 			MPASS(rc == sc->rawf_base + pi->port_id);
6630 			rc = 0;
6631 			pi->vxlan_tcam_entry = true;
6632 		}
6633 	}
6634 
6635 	return (rc);
6636 }
6637 
6638 /*
6639  * {begin|end}_synchronized_op must be called from the same thread.
6640  */
6641 int
begin_synchronized_op(struct adapter * sc,struct vi_info * vi,int flags,char * wmesg)6642 begin_synchronized_op(struct adapter *sc, struct vi_info *vi, int flags,
6643     char *wmesg)
6644 {
6645 	int rc;
6646 
6647 #ifdef WITNESS
6648 	/* the caller thinks it's ok to sleep, but is it really? */
6649 	if (flags & SLEEP_OK)
6650 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
6651 #endif
6652 	ADAPTER_LOCK(sc);
6653 	for (;;) {
6654 
6655 		if (vi && IS_DETACHING(vi)) {
6656 			rc = ENXIO;
6657 			goto done;
6658 		}
6659 
6660 		if (!IS_BUSY(sc)) {
6661 			rc = 0;
6662 			break;
6663 		}
6664 
6665 		if (!(flags & SLEEP_OK)) {
6666 			rc = EBUSY;
6667 			goto done;
6668 		}
6669 
6670 		if (mtx_sleep(&sc->flags, &sc->sc_lock,
6671 		    flags & INTR_OK ? PCATCH : 0, wmesg, 0)) {
6672 			rc = EINTR;
6673 			goto done;
6674 		}
6675 	}
6676 
6677 	KASSERT(!IS_BUSY(sc), ("%s: controller busy.", __func__));
6678 	SET_BUSY(sc);
6679 #ifdef INVARIANTS
6680 	sc->last_op = wmesg;
6681 	sc->last_op_thr = curthread;
6682 	sc->last_op_flags = flags;
6683 #endif
6684 
6685 done:
6686 	if (!(flags & HOLD_LOCK) || rc)
6687 		ADAPTER_UNLOCK(sc);
6688 
6689 	return (rc);
6690 }
6691 
6692 /*
6693  * Tell if_ioctl and if_init that the VI is going away.  This is
6694  * special variant of begin_synchronized_op and must be paired with a
6695  * call to end_vi_detach.
6696  */
6697 void
begin_vi_detach(struct adapter * sc,struct vi_info * vi)6698 begin_vi_detach(struct adapter *sc, struct vi_info *vi)
6699 {
6700 	ADAPTER_LOCK(sc);
6701 	SET_DETACHING(vi);
6702 	wakeup(&sc->flags);
6703 	while (IS_BUSY(sc))
6704 		mtx_sleep(&sc->flags, &sc->sc_lock, 0, "t4detach", 0);
6705 	SET_BUSY(sc);
6706 #ifdef INVARIANTS
6707 	sc->last_op = "t4detach";
6708 	sc->last_op_thr = curthread;
6709 	sc->last_op_flags = 0;
6710 #endif
6711 	ADAPTER_UNLOCK(sc);
6712 }
6713 
6714 void
end_vi_detach(struct adapter * sc,struct vi_info * vi)6715 end_vi_detach(struct adapter *sc, struct vi_info *vi)
6716 {
6717 	ADAPTER_LOCK(sc);
6718 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6719 	CLR_BUSY(sc);
6720 	CLR_DETACHING(vi);
6721 	wakeup(&sc->flags);
6722 	ADAPTER_UNLOCK(sc);
6723 }
6724 
6725 /*
6726  * {begin|end}_synchronized_op must be called from the same thread.
6727  */
6728 void
end_synchronized_op(struct adapter * sc,int flags)6729 end_synchronized_op(struct adapter *sc, int flags)
6730 {
6731 
6732 	if (flags & LOCK_HELD)
6733 		ADAPTER_LOCK_ASSERT_OWNED(sc);
6734 	else
6735 		ADAPTER_LOCK(sc);
6736 
6737 	KASSERT(IS_BUSY(sc), ("%s: controller not busy.", __func__));
6738 	CLR_BUSY(sc);
6739 	wakeup(&sc->flags);
6740 	ADAPTER_UNLOCK(sc);
6741 }
6742 
6743 static int
cxgbe_init_synchronized(struct vi_info * vi)6744 cxgbe_init_synchronized(struct vi_info *vi)
6745 {
6746 	struct port_info *pi = vi->pi;
6747 	struct adapter *sc = pi->adapter;
6748 	if_t ifp = vi->ifp;
6749 	int rc = 0, i;
6750 	struct sge_txq *txq;
6751 
6752 	ASSERT_SYNCHRONIZED_OP(sc);
6753 
6754 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
6755 		return (0);	/* already running */
6756 
6757 	if (!(sc->flags & FULL_INIT_DONE) && ((rc = adapter_init(sc)) != 0))
6758 		return (rc);	/* error message displayed already */
6759 
6760 	if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
6761 		return (rc); /* error message displayed already */
6762 
6763 	rc = update_mac_settings(ifp, XGMAC_ALL);
6764 	if (rc)
6765 		goto done;	/* error message displayed already */
6766 
6767 	PORT_LOCK(pi);
6768 	if (pi->up_vis == 0) {
6769 		t4_update_port_info(pi);
6770 		fixup_link_config(pi);
6771 		build_medialist(pi);
6772 		apply_link_config(pi);
6773 	}
6774 
6775 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, true, true);
6776 	if (rc != 0) {
6777 		if_printf(ifp, "enable_vi failed: %d\n", rc);
6778 		PORT_UNLOCK(pi);
6779 		goto done;
6780 	}
6781 
6782 	/*
6783 	 * Can't fail from this point onwards.  Review cxgbe_uninit_synchronized
6784 	 * if this changes.
6785 	 */
6786 
6787 	for_each_txq(vi, i, txq) {
6788 		TXQ_LOCK(txq);
6789 		txq->eq.flags |= EQ_ENABLED;
6790 		TXQ_UNLOCK(txq);
6791 	}
6792 
6793 	/*
6794 	 * The first iq of the first port to come up is used for tracing.
6795 	 */
6796 	if (sc->traceq < 0 && IS_MAIN_VI(vi)) {
6797 		sc->traceq = sc->sge.rxq[vi->first_rxq].iq.abs_id;
6798 		t4_set_trace_rss_control(sc, pi->tx_chan, sc->traceq);
6799 		pi->flags |= HAS_TRACEQ;
6800 	}
6801 
6802 	/* all ok */
6803 	pi->up_vis++;
6804 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
6805 	if (pi->link_cfg.link_ok)
6806 		t4_os_link_changed(pi);
6807 	PORT_UNLOCK(pi);
6808 
6809 	mtx_lock(&vi->tick_mtx);
6810 	if (vi->pi->nvi > 1 || sc->flags & IS_VF)
6811 		callout_reset(&vi->tick, hz, vi_tick, vi);
6812 	else
6813 		callout_reset(&vi->tick, hz, cxgbe_tick, vi);
6814 	mtx_unlock(&vi->tick_mtx);
6815 done:
6816 	if (rc != 0)
6817 		cxgbe_uninit_synchronized(vi);
6818 
6819 	return (rc);
6820 }
6821 
6822 /*
6823  * Idempotent.
6824  */
6825 static int
cxgbe_uninit_synchronized(struct vi_info * vi)6826 cxgbe_uninit_synchronized(struct vi_info *vi)
6827 {
6828 	struct port_info *pi = vi->pi;
6829 	struct adapter *sc = pi->adapter;
6830 	if_t ifp = vi->ifp;
6831 	int rc, i;
6832 	struct sge_txq *txq;
6833 
6834 	ASSERT_SYNCHRONIZED_OP(sc);
6835 
6836 	if (!(vi->flags & VI_INIT_DONE)) {
6837 		if (__predict_false(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6838 			KASSERT(0, ("uninited VI is running"));
6839 			if_printf(ifp, "uninited VI with running ifnet.  "
6840 			    "vi->flags 0x%016lx, if_flags 0x%08x, "
6841 			    "if_drv_flags 0x%08x\n", vi->flags, if_getflags(ifp),
6842 			    if_getdrvflags(ifp));
6843 		}
6844 		return (0);
6845 	}
6846 
6847 	/*
6848 	 * Disable the VI so that all its data in either direction is discarded
6849 	 * by the MPS.  Leave everything else (the queues, interrupts, and 1Hz
6850 	 * tick) intact as the TP can deliver negative advice or data that it's
6851 	 * holding in its RAM (for an offloaded connection) even after the VI is
6852 	 * disabled.
6853 	 */
6854 	rc = -t4_enable_vi(sc, sc->mbox, vi->viid, false, false);
6855 	if (rc) {
6856 		if_printf(ifp, "disable_vi failed: %d\n", rc);
6857 		return (rc);
6858 	}
6859 
6860 	for_each_txq(vi, i, txq) {
6861 		TXQ_LOCK(txq);
6862 		txq->eq.flags &= ~EQ_ENABLED;
6863 		TXQ_UNLOCK(txq);
6864 	}
6865 
6866 	mtx_lock(&vi->tick_mtx);
6867 	callout_stop(&vi->tick);
6868 	mtx_unlock(&vi->tick_mtx);
6869 
6870 	PORT_LOCK(pi);
6871 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
6872 		PORT_UNLOCK(pi);
6873 		return (0);
6874 	}
6875 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
6876 	pi->up_vis--;
6877 	if (pi->up_vis > 0) {
6878 		PORT_UNLOCK(pi);
6879 		return (0);
6880 	}
6881 
6882 	pi->link_cfg.link_ok = false;
6883 	pi->link_cfg.speed = 0;
6884 	pi->link_cfg.link_down_rc = 255;
6885 	t4_os_link_changed(pi);
6886 	PORT_UNLOCK(pi);
6887 
6888 	return (0);
6889 }
6890 
6891 /*
6892  * It is ok for this function to fail midway and return right away.  t4_detach
6893  * will walk the entire sc->irq list and clean up whatever is valid.
6894  */
6895 int
t4_setup_intr_handlers(struct adapter * sc)6896 t4_setup_intr_handlers(struct adapter *sc)
6897 {
6898 	int rc, rid, p, q, v;
6899 	char s[8];
6900 	struct irq *irq;
6901 	struct port_info *pi;
6902 	struct vi_info *vi;
6903 	struct sge *sge = &sc->sge;
6904 	struct sge_rxq *rxq;
6905 #ifdef TCP_OFFLOAD
6906 	struct sge_ofld_rxq *ofld_rxq;
6907 #endif
6908 #ifdef DEV_NETMAP
6909 	struct sge_nm_rxq *nm_rxq;
6910 #endif
6911 #ifdef RSS
6912 	int nbuckets = rss_getnumbuckets();
6913 #endif
6914 
6915 	/*
6916 	 * Setup interrupts.
6917 	 */
6918 	irq = &sc->irq[0];
6919 	rid = sc->intr_type == INTR_INTX ? 0 : 1;
6920 	if (forwarding_intr_to_fwq(sc))
6921 		return (t4_alloc_irq(sc, irq, rid, t4_intr_all, sc, "all"));
6922 
6923 	/* Multiple interrupts. */
6924 	if (sc->flags & IS_VF)
6925 		KASSERT(sc->intr_count >= T4VF_EXTRA_INTR + sc->params.nports,
6926 		    ("%s: too few intr.", __func__));
6927 	else
6928 		KASSERT(sc->intr_count >= T4_EXTRA_INTR + sc->params.nports,
6929 		    ("%s: too few intr.", __func__));
6930 
6931 	/* The first one is always error intr on PFs */
6932 	if (!(sc->flags & IS_VF)) {
6933 		rc = t4_alloc_irq(sc, irq, rid, t4_intr_err, sc, "err");
6934 		if (rc != 0)
6935 			return (rc);
6936 		irq++;
6937 		rid++;
6938 	}
6939 
6940 	/* The second one is always the firmware event queue (first on VFs) */
6941 	rc = t4_alloc_irq(sc, irq, rid, t4_intr_evt, &sge->fwq, "evt");
6942 	if (rc != 0)
6943 		return (rc);
6944 	irq++;
6945 	rid++;
6946 
6947 	for_each_port(sc, p) {
6948 		pi = sc->port[p];
6949 		for_each_vi(pi, v, vi) {
6950 			vi->first_intr = rid - 1;
6951 
6952 			if (vi->nnmrxq > 0) {
6953 				int n = max(vi->nrxq, vi->nnmrxq);
6954 
6955 				rxq = &sge->rxq[vi->first_rxq];
6956 #ifdef DEV_NETMAP
6957 				nm_rxq = &sge->nm_rxq[vi->first_nm_rxq];
6958 #endif
6959 				for (q = 0; q < n; q++) {
6960 					snprintf(s, sizeof(s), "%x%c%x", p,
6961 					    'a' + v, q);
6962 					if (q < vi->nrxq)
6963 						irq->rxq = rxq++;
6964 #ifdef DEV_NETMAP
6965 					if (q < vi->nnmrxq)
6966 						irq->nm_rxq = nm_rxq++;
6967 
6968 					if (irq->nm_rxq != NULL &&
6969 					    irq->rxq == NULL) {
6970 						/* Netmap rx only */
6971 						rc = t4_alloc_irq(sc, irq, rid,
6972 						    t4_nm_intr, irq->nm_rxq, s);
6973 					}
6974 					if (irq->nm_rxq != NULL &&
6975 					    irq->rxq != NULL) {
6976 						/* NIC and Netmap rx */
6977 						rc = t4_alloc_irq(sc, irq, rid,
6978 						    t4_vi_intr, irq, s);
6979 					}
6980 #endif
6981 					if (irq->rxq != NULL &&
6982 					    irq->nm_rxq == NULL) {
6983 						/* NIC rx only */
6984 						rc = t4_alloc_irq(sc, irq, rid,
6985 						    t4_intr, irq->rxq, s);
6986 					}
6987 					if (rc != 0)
6988 						return (rc);
6989 #ifdef RSS
6990 					if (q < vi->nrxq) {
6991 						bus_bind_intr(sc->dev, irq->res,
6992 						    rss_getcpu(q % nbuckets));
6993 					}
6994 #endif
6995 					irq++;
6996 					rid++;
6997 					vi->nintr++;
6998 				}
6999 			} else {
7000 				for_each_rxq(vi, q, rxq) {
7001 					snprintf(s, sizeof(s), "%x%c%x", p,
7002 					    'a' + v, q);
7003 					rc = t4_alloc_irq(sc, irq, rid,
7004 					    t4_intr, rxq, s);
7005 					if (rc != 0)
7006 						return (rc);
7007 #ifdef RSS
7008 					bus_bind_intr(sc->dev, irq->res,
7009 					    rss_getcpu(q % nbuckets));
7010 #endif
7011 					irq++;
7012 					rid++;
7013 					vi->nintr++;
7014 				}
7015 			}
7016 #ifdef TCP_OFFLOAD
7017 			for_each_ofld_rxq(vi, q, ofld_rxq) {
7018 				snprintf(s, sizeof(s), "%x%c%x", p, 'A' + v, q);
7019 				rc = t4_alloc_irq(sc, irq, rid, t4_intr,
7020 				    ofld_rxq, s);
7021 				if (rc != 0)
7022 					return (rc);
7023 				irq++;
7024 				rid++;
7025 				vi->nintr++;
7026 			}
7027 #endif
7028 		}
7029 	}
7030 	MPASS(irq == &sc->irq[sc->intr_count]);
7031 
7032 	return (0);
7033 }
7034 
7035 static void
write_global_rss_key(struct adapter * sc)7036 write_global_rss_key(struct adapter *sc)
7037 {
7038 #ifdef RSS
7039 	int i;
7040 	uint32_t raw_rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7041 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
7042 
7043 	CTASSERT(RSS_KEYSIZE == 40);
7044 
7045 	rss_getkey((void *)&raw_rss_key[0]);
7046 	for (i = 0; i < nitems(rss_key); i++) {
7047 		rss_key[i] = htobe32(raw_rss_key[nitems(rss_key) - 1 - i]);
7048 	}
7049 	t4_write_rss_key(sc, &rss_key[0], -1, 1);
7050 #endif
7051 }
7052 
7053 /*
7054  * Idempotent.
7055  */
7056 static int
adapter_full_init(struct adapter * sc)7057 adapter_full_init(struct adapter *sc)
7058 {
7059 	int rc, i;
7060 
7061 	ASSERT_SYNCHRONIZED_OP(sc);
7062 
7063 	/*
7064 	 * queues that belong to the adapter (not any particular port).
7065 	 */
7066 	rc = t4_setup_adapter_queues(sc);
7067 	if (rc != 0)
7068 		return (rc);
7069 
7070 	MPASS(sc->params.nports <= nitems(sc->tq));
7071 	for (i = 0; i < sc->params.nports; i++) {
7072 		if (sc->tq[i] != NULL)
7073 			continue;
7074 		sc->tq[i] = taskqueue_create("t4 taskq", M_NOWAIT,
7075 		    taskqueue_thread_enqueue, &sc->tq[i]);
7076 		if (sc->tq[i] == NULL) {
7077 			CH_ERR(sc, "failed to allocate task queue %d\n", i);
7078 			return (ENOMEM);
7079 		}
7080 		taskqueue_start_threads(&sc->tq[i], 1, PI_NET, "%s tq%d",
7081 		    device_get_nameunit(sc->dev), i);
7082 	}
7083 
7084 	if (!(sc->flags & IS_VF)) {
7085 		write_global_rss_key(sc);
7086 		t4_intr_enable(sc);
7087 	}
7088 	return (0);
7089 }
7090 
7091 int
adapter_init(struct adapter * sc)7092 adapter_init(struct adapter *sc)
7093 {
7094 	int rc;
7095 
7096 	ASSERT_SYNCHRONIZED_OP(sc);
7097 	ADAPTER_LOCK_ASSERT_NOTOWNED(sc);
7098 	KASSERT((sc->flags & FULL_INIT_DONE) == 0,
7099 	    ("%s: FULL_INIT_DONE already", __func__));
7100 
7101 	rc = adapter_full_init(sc);
7102 	if (rc != 0)
7103 		adapter_full_uninit(sc);
7104 	else
7105 		sc->flags |= FULL_INIT_DONE;
7106 
7107 	return (rc);
7108 }
7109 
7110 /*
7111  * Idempotent.
7112  */
7113 static void
adapter_full_uninit(struct adapter * sc)7114 adapter_full_uninit(struct adapter *sc)
7115 {
7116 	int i;
7117 
7118 	t4_teardown_adapter_queues(sc);
7119 
7120 	for (i = 0; i < nitems(sc->tq); i++) {
7121 		if (sc->tq[i] == NULL)
7122 			continue;
7123 		taskqueue_free(sc->tq[i]);
7124 		sc->tq[i] = NULL;
7125 	}
7126 
7127 	sc->flags &= ~FULL_INIT_DONE;
7128 }
7129 
7130 #ifdef RSS
7131 #define SUPPORTED_RSS_HASHTYPES (RSS_HASHTYPE_RSS_IPV4 | \
7132     RSS_HASHTYPE_RSS_TCP_IPV4 | RSS_HASHTYPE_RSS_IPV6 | \
7133     RSS_HASHTYPE_RSS_TCP_IPV6 | RSS_HASHTYPE_RSS_UDP_IPV4 | \
7134     RSS_HASHTYPE_RSS_UDP_IPV6)
7135 
7136 /* Translates kernel hash types to hardware. */
7137 static int
hashconfig_to_hashen(int hashconfig)7138 hashconfig_to_hashen(int hashconfig)
7139 {
7140 	int hashen = 0;
7141 
7142 	if (hashconfig & RSS_HASHTYPE_RSS_IPV4)
7143 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
7144 	if (hashconfig & RSS_HASHTYPE_RSS_IPV6)
7145 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
7146 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV4) {
7147 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7148 		    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7149 	}
7150 	if (hashconfig & RSS_HASHTYPE_RSS_UDP_IPV6) {
7151 		hashen |= F_FW_RSS_VI_CONFIG_CMD_UDPEN |
7152 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7153 	}
7154 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV4)
7155 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
7156 	if (hashconfig & RSS_HASHTYPE_RSS_TCP_IPV6)
7157 		hashen |= F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
7158 
7159 	return (hashen);
7160 }
7161 
7162 /* Translates hardware hash types to kernel. */
7163 static int
hashen_to_hashconfig(int hashen)7164 hashen_to_hashconfig(int hashen)
7165 {
7166 	int hashconfig = 0;
7167 
7168 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_UDPEN) {
7169 		/*
7170 		 * If UDP hashing was enabled it must have been enabled for
7171 		 * either IPv4 or IPv6 (inclusive or).  Enabling UDP without
7172 		 * enabling any 4-tuple hash is nonsense configuration.
7173 		 */
7174 		MPASS(hashen & (F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7175 		    F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN));
7176 
7177 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7178 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV4;
7179 		if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7180 			hashconfig |= RSS_HASHTYPE_RSS_UDP_IPV6;
7181 	}
7182 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
7183 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV4;
7184 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
7185 		hashconfig |= RSS_HASHTYPE_RSS_TCP_IPV6;
7186 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
7187 		hashconfig |= RSS_HASHTYPE_RSS_IPV4;
7188 	if (hashen & F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
7189 		hashconfig |= RSS_HASHTYPE_RSS_IPV6;
7190 
7191 	return (hashconfig);
7192 }
7193 #endif
7194 
7195 /*
7196  * Idempotent.
7197  */
7198 static int
vi_full_init(struct vi_info * vi)7199 vi_full_init(struct vi_info *vi)
7200 {
7201 	struct adapter *sc = vi->adapter;
7202 	struct sge_rxq *rxq;
7203 	int rc, i, j;
7204 #ifdef RSS
7205 	int nbuckets = rss_getnumbuckets();
7206 	int hashconfig = rss_gethashconfig();
7207 	int extra;
7208 #endif
7209 
7210 	ASSERT_SYNCHRONIZED_OP(sc);
7211 
7212 	/*
7213 	 * Allocate tx/rx/fl queues for this VI.
7214 	 */
7215 	rc = t4_setup_vi_queues(vi);
7216 	if (rc != 0)
7217 		return (rc);
7218 
7219 	/*
7220 	 * Setup RSS for this VI.  Save a copy of the RSS table for later use.
7221 	 */
7222 	if (vi->nrxq > vi->rss_size) {
7223 		CH_ALERT(vi, "nrxq (%d) > hw RSS table size (%d); "
7224 		    "some queues will never receive traffic.\n", vi->nrxq,
7225 		    vi->rss_size);
7226 	} else if (vi->rss_size % vi->nrxq) {
7227 		CH_ALERT(vi, "nrxq (%d), hw RSS table size (%d); "
7228 		    "expect uneven traffic distribution.\n", vi->nrxq,
7229 		    vi->rss_size);
7230 	}
7231 #ifdef RSS
7232 	if (vi->nrxq != nbuckets) {
7233 		CH_ALERT(vi, "nrxq (%d) != kernel RSS buckets (%d);"
7234 		    "performance will be impacted.\n", vi->nrxq, nbuckets);
7235 	}
7236 #endif
7237 	if (vi->rss == NULL)
7238 		vi->rss = malloc(vi->rss_size * sizeof (*vi->rss), M_CXGBE,
7239 		    M_ZERO | M_WAITOK);
7240 	for (i = 0; i < vi->rss_size;) {
7241 #ifdef RSS
7242 		j = rss_get_indirection_to_bucket(i);
7243 		j %= vi->nrxq;
7244 		rxq = &sc->sge.rxq[vi->first_rxq + j];
7245 		vi->rss[i++] = rxq->iq.abs_id;
7246 #else
7247 		for_each_rxq(vi, j, rxq) {
7248 			vi->rss[i++] = rxq->iq.abs_id;
7249 			if (i == vi->rss_size)
7250 				break;
7251 		}
7252 #endif
7253 	}
7254 
7255 	rc = -t4_config_rss_range(sc, sc->mbox, vi->viid, 0, vi->rss_size,
7256 	    vi->rss, vi->rss_size);
7257 	if (rc != 0) {
7258 		CH_ERR(vi, "rss_config failed: %d\n", rc);
7259 		return (rc);
7260 	}
7261 
7262 #ifdef RSS
7263 	vi->hashen = hashconfig_to_hashen(hashconfig);
7264 
7265 	/*
7266 	 * We may have had to enable some hashes even though the global config
7267 	 * wants them disabled.  This is a potential problem that must be
7268 	 * reported to the user.
7269 	 */
7270 	extra = hashen_to_hashconfig(vi->hashen) ^ hashconfig;
7271 
7272 	/*
7273 	 * If we consider only the supported hash types, then the enabled hashes
7274 	 * are a superset of the requested hashes.  In other words, there cannot
7275 	 * be any supported hash that was requested but not enabled, but there
7276 	 * can be hashes that were not requested but had to be enabled.
7277 	 */
7278 	extra &= SUPPORTED_RSS_HASHTYPES;
7279 	MPASS((extra & hashconfig) == 0);
7280 
7281 	if (extra) {
7282 		CH_ALERT(vi,
7283 		    "global RSS config (0x%x) cannot be accommodated.\n",
7284 		    hashconfig);
7285 	}
7286 	if (extra & RSS_HASHTYPE_RSS_IPV4)
7287 		CH_ALERT(vi, "IPv4 2-tuple hashing forced on.\n");
7288 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV4)
7289 		CH_ALERT(vi, "TCP/IPv4 4-tuple hashing forced on.\n");
7290 	if (extra & RSS_HASHTYPE_RSS_IPV6)
7291 		CH_ALERT(vi, "IPv6 2-tuple hashing forced on.\n");
7292 	if (extra & RSS_HASHTYPE_RSS_TCP_IPV6)
7293 		CH_ALERT(vi, "TCP/IPv6 4-tuple hashing forced on.\n");
7294 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV4)
7295 		CH_ALERT(vi, "UDP/IPv4 4-tuple hashing forced on.\n");
7296 	if (extra & RSS_HASHTYPE_RSS_UDP_IPV6)
7297 		CH_ALERT(vi, "UDP/IPv6 4-tuple hashing forced on.\n");
7298 #else
7299 	vi->hashen = F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
7300 	    F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
7301 	    F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
7302 	    F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN | F_FW_RSS_VI_CONFIG_CMD_UDPEN;
7303 #endif
7304 	rc = -t4_config_vi_rss(sc, sc->mbox, vi->viid, vi->hashen, vi->rss[0],
7305 	    0, 0);
7306 	if (rc != 0) {
7307 		CH_ERR(vi, "rss hash/defaultq config failed: %d\n", rc);
7308 		return (rc);
7309 	}
7310 
7311 	return (0);
7312 }
7313 
7314 int
vi_init(struct vi_info * vi)7315 vi_init(struct vi_info *vi)
7316 {
7317 	int rc;
7318 
7319 	ASSERT_SYNCHRONIZED_OP(vi->adapter);
7320 	KASSERT((vi->flags & VI_INIT_DONE) == 0,
7321 	    ("%s: VI_INIT_DONE already", __func__));
7322 
7323 	rc = vi_full_init(vi);
7324 	if (rc != 0)
7325 		vi_full_uninit(vi);
7326 	else
7327 		vi->flags |= VI_INIT_DONE;
7328 
7329 	return (rc);
7330 }
7331 
7332 /*
7333  * Idempotent.
7334  */
7335 static void
vi_full_uninit(struct vi_info * vi)7336 vi_full_uninit(struct vi_info *vi)
7337 {
7338 
7339 	if (vi->flags & VI_INIT_DONE) {
7340 		quiesce_vi(vi);
7341 		free(vi->rss, M_CXGBE);
7342 		free(vi->nm_rss, M_CXGBE);
7343 	}
7344 
7345 	t4_teardown_vi_queues(vi);
7346 	vi->flags &= ~VI_INIT_DONE;
7347 }
7348 
7349 static void
quiesce_txq(struct sge_txq * txq)7350 quiesce_txq(struct sge_txq *txq)
7351 {
7352 	struct sge_eq *eq = &txq->eq;
7353 	struct sge_qstat *spg = (void *)&eq->desc[eq->sidx];
7354 
7355 	MPASS(eq->flags & EQ_SW_ALLOCATED);
7356 	MPASS(!(eq->flags & EQ_ENABLED));
7357 
7358 	/* Wait for the mp_ring to empty. */
7359 	while (!mp_ring_is_idle(txq->r)) {
7360 		mp_ring_check_drainage(txq->r, 4096);
7361 		pause("rquiesce", 1);
7362 	}
7363 	MPASS(txq->txp.npkt == 0);
7364 
7365 	if (eq->flags & EQ_HW_ALLOCATED) {
7366 		/*
7367 		 * Hardware is alive and working normally.  Wait for it to
7368 		 * finish and then wait for the driver to catch up and reclaim
7369 		 * all descriptors.
7370 		 */
7371 		while (spg->cidx != htobe16(eq->pidx))
7372 			pause("equiesce", 1);
7373 		while (eq->cidx != eq->pidx)
7374 			pause("dquiesce", 1);
7375 	} else {
7376 		/*
7377 		 * Hardware is unavailable.  Discard all pending tx and reclaim
7378 		 * descriptors directly.
7379 		 */
7380 		TXQ_LOCK(txq);
7381 		while (eq->cidx != eq->pidx) {
7382 			struct mbuf *m, *nextpkt;
7383 			struct tx_sdesc *txsd;
7384 
7385 			txsd = &txq->sdesc[eq->cidx];
7386 			for (m = txsd->m; m != NULL; m = nextpkt) {
7387 				nextpkt = m->m_nextpkt;
7388 				m->m_nextpkt = NULL;
7389 				m_freem(m);
7390 			}
7391 			IDXINCR(eq->cidx, txsd->desc_used, eq->sidx);
7392 		}
7393 		spg->pidx = spg->cidx = htobe16(eq->cidx);
7394 		TXQ_UNLOCK(txq);
7395 	}
7396 }
7397 
7398 static void
quiesce_wrq(struct sge_wrq * wrq)7399 quiesce_wrq(struct sge_wrq *wrq)
7400 {
7401 	struct wrqe *wr;
7402 
7403 	TXQ_LOCK(wrq);
7404 	while ((wr = STAILQ_FIRST(&wrq->wr_list)) != NULL) {
7405 		STAILQ_REMOVE_HEAD(&wrq->wr_list, link);
7406 #ifdef INVARIANTS
7407 		wrq->nwr_pending--;
7408 		wrq->ndesc_needed -= howmany(wr->wr_len, EQ_ESIZE);
7409 #endif
7410 		free(wr, M_CXGBE);
7411 	}
7412 	MPASS(wrq->nwr_pending == 0);
7413 	MPASS(wrq->ndesc_needed == 0);
7414 	wrq->nwr_pending = 0;
7415 	wrq->ndesc_needed = 0;
7416 	TXQ_UNLOCK(wrq);
7417 }
7418 
7419 static void
quiesce_iq_fl(struct adapter * sc,struct sge_iq * iq,struct sge_fl * fl)7420 quiesce_iq_fl(struct adapter *sc, struct sge_iq *iq, struct sge_fl *fl)
7421 {
7422 	/* Synchronize with the interrupt handler */
7423 	while (!atomic_cmpset_int(&iq->state, IQS_IDLE, IQS_DISABLED))
7424 		pause("iqfree", 1);
7425 
7426 	if (fl != NULL) {
7427 		MPASS(iq->flags & IQ_HAS_FL);
7428 
7429 		mtx_lock(&sc->sfl_lock);
7430 		FL_LOCK(fl);
7431 		fl->flags |= FL_DOOMED;
7432 		FL_UNLOCK(fl);
7433 		callout_stop(&sc->sfl_callout);
7434 		mtx_unlock(&sc->sfl_lock);
7435 
7436 		KASSERT((fl->flags & FL_STARVING) == 0,
7437 		    ("%s: still starving", __func__));
7438 
7439 		/* Release all buffers if hardware is no longer available. */
7440 		if (!(iq->flags & IQ_HW_ALLOCATED))
7441 			free_fl_buffers(sc, fl);
7442 	}
7443 }
7444 
7445 /*
7446  * Wait for all activity on all the queues of the VI to complete.  It is assumed
7447  * that no new work is being enqueued by the hardware or the driver.  That part
7448  * should be arranged before calling this function.
7449  */
7450 static void
quiesce_vi(struct vi_info * vi)7451 quiesce_vi(struct vi_info *vi)
7452 {
7453 	int i;
7454 	struct adapter *sc = vi->adapter;
7455 	struct sge_rxq *rxq;
7456 	struct sge_txq *txq;
7457 #ifdef TCP_OFFLOAD
7458 	struct sge_ofld_rxq *ofld_rxq;
7459 #endif
7460 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7461 	struct sge_ofld_txq *ofld_txq;
7462 #endif
7463 
7464 	if (!(vi->flags & VI_INIT_DONE))
7465 		return;
7466 
7467 	for_each_txq(vi, i, txq) {
7468 		quiesce_txq(txq);
7469 	}
7470 
7471 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
7472 	for_each_ofld_txq(vi, i, ofld_txq) {
7473 		quiesce_wrq(&ofld_txq->wrq);
7474 	}
7475 #endif
7476 
7477 	for_each_rxq(vi, i, rxq) {
7478 		quiesce_iq_fl(sc, &rxq->iq, &rxq->fl);
7479 	}
7480 
7481 #ifdef TCP_OFFLOAD
7482 	for_each_ofld_rxq(vi, i, ofld_rxq) {
7483 		quiesce_iq_fl(sc, &ofld_rxq->iq, &ofld_rxq->fl);
7484 	}
7485 #endif
7486 }
7487 
7488 static int
t4_alloc_irq(struct adapter * sc,struct irq * irq,int rid,driver_intr_t * handler,void * arg,char * name)7489 t4_alloc_irq(struct adapter *sc, struct irq *irq, int rid,
7490     driver_intr_t *handler, void *arg, char *name)
7491 {
7492 	int rc;
7493 
7494 	irq->rid = rid;
7495 	irq->res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &irq->rid,
7496 	    RF_SHAREABLE | RF_ACTIVE);
7497 	if (irq->res == NULL) {
7498 		device_printf(sc->dev,
7499 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
7500 		return (ENOMEM);
7501 	}
7502 
7503 	rc = bus_setup_intr(sc->dev, irq->res, INTR_MPSAFE | INTR_TYPE_NET,
7504 	    NULL, handler, arg, &irq->tag);
7505 	if (rc != 0) {
7506 		device_printf(sc->dev,
7507 		    "failed to setup interrupt for rid %d, name %s: %d\n",
7508 		    rid, name, rc);
7509 	} else if (name)
7510 		bus_describe_intr(sc->dev, irq->res, irq->tag, "%s", name);
7511 
7512 	return (rc);
7513 }
7514 
7515 static int
t4_free_irq(struct adapter * sc,struct irq * irq)7516 t4_free_irq(struct adapter *sc, struct irq *irq)
7517 {
7518 	if (irq->tag)
7519 		bus_teardown_intr(sc->dev, irq->res, irq->tag);
7520 	if (irq->res)
7521 		bus_release_resource(sc->dev, SYS_RES_IRQ, irq->rid, irq->res);
7522 
7523 	bzero(irq, sizeof(*irq));
7524 
7525 	return (0);
7526 }
7527 
7528 static void
get_regs(struct adapter * sc,struct t4_regdump * regs,uint8_t * buf)7529 get_regs(struct adapter *sc, struct t4_regdump *regs, uint8_t *buf)
7530 {
7531 
7532 	regs->version = chip_id(sc) | chip_rev(sc) << 10;
7533 	t4_get_regs(sc, buf, regs->len);
7534 }
7535 
7536 #define	A_PL_INDIR_CMD	0x1f8
7537 
7538 #define	S_PL_AUTOINC	31
7539 #define	M_PL_AUTOINC	0x1U
7540 #define	V_PL_AUTOINC(x)	((x) << S_PL_AUTOINC)
7541 #define	G_PL_AUTOINC(x)	(((x) >> S_PL_AUTOINC) & M_PL_AUTOINC)
7542 
7543 #define	S_PL_VFID	20
7544 #define	M_PL_VFID	0xffU
7545 #define	V_PL_VFID(x)	((x) << S_PL_VFID)
7546 #define	G_PL_VFID(x)	(((x) >> S_PL_VFID) & M_PL_VFID)
7547 
7548 #define	S_PL_ADDR	0
7549 #define	M_PL_ADDR	0xfffffU
7550 #define	V_PL_ADDR(x)	((x) << S_PL_ADDR)
7551 #define	G_PL_ADDR(x)	(((x) >> S_PL_ADDR) & M_PL_ADDR)
7552 
7553 #define	A_PL_INDIR_DATA	0x1fc
7554 
7555 static uint64_t
read_vf_stat(struct adapter * sc,u_int vin,int reg)7556 read_vf_stat(struct adapter *sc, u_int vin, int reg)
7557 {
7558 	u32 stats[2];
7559 
7560 	if (sc->flags & IS_VF) {
7561 		stats[0] = t4_read_reg(sc, VF_MPS_REG(reg));
7562 		stats[1] = t4_read_reg(sc, VF_MPS_REG(reg + 4));
7563 	} else {
7564 		mtx_assert(&sc->reg_lock, MA_OWNED);
7565 		t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) |
7566 		    V_PL_VFID(vin) | V_PL_ADDR(VF_MPS_REG(reg)));
7567 		stats[0] = t4_read_reg(sc, A_PL_INDIR_DATA);
7568 		stats[1] = t4_read_reg(sc, A_PL_INDIR_DATA);
7569 	}
7570 	return (((uint64_t)stats[1]) << 32 | stats[0]);
7571 }
7572 
7573 static void
t4_get_vi_stats(struct adapter * sc,u_int vin,struct fw_vi_stats_vf * stats)7574 t4_get_vi_stats(struct adapter *sc, u_int vin, struct fw_vi_stats_vf *stats)
7575 {
7576 
7577 #define GET_STAT(name) \
7578 	read_vf_stat(sc, vin, A_MPS_VF_STAT_##name##_L)
7579 
7580 	if (!(sc->flags & IS_VF))
7581 		mtx_lock(&sc->reg_lock);
7582 	stats->tx_bcast_bytes    = GET_STAT(TX_VF_BCAST_BYTES);
7583 	stats->tx_bcast_frames   = GET_STAT(TX_VF_BCAST_FRAMES);
7584 	stats->tx_mcast_bytes    = GET_STAT(TX_VF_MCAST_BYTES);
7585 	stats->tx_mcast_frames   = GET_STAT(TX_VF_MCAST_FRAMES);
7586 	stats->tx_ucast_bytes    = GET_STAT(TX_VF_UCAST_BYTES);
7587 	stats->tx_ucast_frames   = GET_STAT(TX_VF_UCAST_FRAMES);
7588 	stats->tx_drop_frames    = GET_STAT(TX_VF_DROP_FRAMES);
7589 	stats->tx_offload_bytes  = GET_STAT(TX_VF_OFFLOAD_BYTES);
7590 	stats->tx_offload_frames = GET_STAT(TX_VF_OFFLOAD_FRAMES);
7591 	stats->rx_bcast_bytes    = GET_STAT(RX_VF_BCAST_BYTES);
7592 	stats->rx_bcast_frames   = GET_STAT(RX_VF_BCAST_FRAMES);
7593 	stats->rx_mcast_bytes    = GET_STAT(RX_VF_MCAST_BYTES);
7594 	stats->rx_mcast_frames   = GET_STAT(RX_VF_MCAST_FRAMES);
7595 	stats->rx_ucast_bytes    = GET_STAT(RX_VF_UCAST_BYTES);
7596 	stats->rx_ucast_frames   = GET_STAT(RX_VF_UCAST_FRAMES);
7597 	stats->rx_err_frames     = GET_STAT(RX_VF_ERR_FRAMES);
7598 	if (!(sc->flags & IS_VF))
7599 		mtx_unlock(&sc->reg_lock);
7600 
7601 #undef GET_STAT
7602 }
7603 
7604 static void
t4_clr_vi_stats(struct adapter * sc,u_int vin)7605 t4_clr_vi_stats(struct adapter *sc, u_int vin)
7606 {
7607 	int reg;
7608 
7609 	t4_write_reg(sc, A_PL_INDIR_CMD, V_PL_AUTOINC(1) | V_PL_VFID(vin) |
7610 	    V_PL_ADDR(VF_MPS_REG(A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L)));
7611 	for (reg = A_MPS_VF_STAT_TX_VF_BCAST_BYTES_L;
7612 	     reg <= A_MPS_VF_STAT_RX_VF_ERR_FRAMES_H; reg += 4)
7613 		t4_write_reg(sc, A_PL_INDIR_DATA, 0);
7614 }
7615 
7616 static void
vi_refresh_stats(struct vi_info * vi)7617 vi_refresh_stats(struct vi_info *vi)
7618 {
7619 	struct timeval tv;
7620 	const struct timeval interval = {0, 250000};	/* 250ms */
7621 
7622 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7623 
7624 	if (vi->flags & VI_SKIP_STATS)
7625 		return;
7626 
7627 	getmicrotime(&tv);
7628 	timevalsub(&tv, &interval);
7629 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7630 		return;
7631 
7632 	t4_get_vi_stats(vi->adapter, vi->vin, &vi->stats);
7633 	getmicrotime(&vi->last_refreshed);
7634 }
7635 
7636 static void
cxgbe_refresh_stats(struct vi_info * vi)7637 cxgbe_refresh_stats(struct vi_info *vi)
7638 {
7639 	u_int i, v, tnl_cong_drops, chan_map;
7640 	struct timeval tv;
7641 	const struct timeval interval = {0, 250000};	/* 250ms */
7642 	struct port_info *pi;
7643 	struct adapter *sc;
7644 
7645 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7646 
7647 	if (vi->flags & VI_SKIP_STATS)
7648 		return;
7649 
7650 	getmicrotime(&tv);
7651 	timevalsub(&tv, &interval);
7652 	if (timevalcmp(&tv, &vi->last_refreshed, <))
7653 		return;
7654 
7655 	pi = vi->pi;
7656 	sc = vi->adapter;
7657 	tnl_cong_drops = 0;
7658 	t4_get_port_stats(sc, pi->hw_port, &pi->stats);
7659 	chan_map = pi->rx_e_chan_map;
7660 	while (chan_map) {
7661 		i = ffs(chan_map) - 1;
7662 		mtx_lock(&sc->reg_lock);
7663 		t4_read_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v, 1,
7664 		    A_TP_MIB_TNL_CNG_DROP_0 + i);
7665 		mtx_unlock(&sc->reg_lock);
7666 		tnl_cong_drops += v;
7667 		chan_map &= ~(1 << i);
7668 	}
7669 	pi->tnl_cong_drops = tnl_cong_drops;
7670 	getmicrotime(&vi->last_refreshed);
7671 }
7672 
7673 static void
cxgbe_tick(void * arg)7674 cxgbe_tick(void *arg)
7675 {
7676 	struct vi_info *vi = arg;
7677 
7678 	MPASS(IS_MAIN_VI(vi));
7679 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7680 
7681 	cxgbe_refresh_stats(vi);
7682 	callout_schedule(&vi->tick, hz);
7683 }
7684 
7685 static void
vi_tick(void * arg)7686 vi_tick(void *arg)
7687 {
7688 	struct vi_info *vi = arg;
7689 
7690 	mtx_assert(&vi->tick_mtx, MA_OWNED);
7691 
7692 	vi_refresh_stats(vi);
7693 	callout_schedule(&vi->tick, hz);
7694 }
7695 
7696 /* CIM inbound queues */
7697 static const char *t4_ibq[CIM_NUM_IBQ] = {
7698 	"ibq_tp0", "ibq_tp1", "ibq_ulp", "ibq_sge0", "ibq_sge1", "ibq_ncsi"
7699 };
7700 static const char *t7_ibq[CIM_NUM_IBQ_T7] = {
7701 	"ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7702 	"ibq_sge1", "ibq_ncsi", NULL, "ibq_ipc1", "ibq_ipc2", "ibq_ipc3",
7703 	"ibq_ipc4", "ibq_ipc5", "ibq_ipc6", "ibq_ipc7"
7704 };
7705 static const char *t7_ibq_sec[] = {
7706 	"ibq_tp0", "ibq_tp1", "ibq_tp2", "ibq_tp3", "ibq_ulp", "ibq_sge0",
7707 	NULL, NULL, NULL, "ibq_ipc0"
7708 };
7709 
7710 /* CIM outbound queues */
7711 static const char *t4_obq[CIM_NUM_OBQ_T5] = {
7712 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7713 	"obq_sge_rx_q0", "obq_sge_rx_q1" /* These two are T5/T6 only */
7714 };
7715 static const char *t7_obq[CIM_NUM_OBQ_T7] = {
7716 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", "obq_ncsi",
7717 	"obq_sge_rx_q0", NULL, NULL, "obq_ipc1", "obq_ipc2", "obq_ipc3",
7718 	"obq_ipc4", "obq_ipc5", "obq_ipc6", "obq_ipc7"
7719 };
7720 static const char *t7_obq_sec[] = {
7721 	"obq_ulp0", "obq_ulp1", "obq_ulp2", "obq_ulp3", "obq_sge", NULL,
7722 	"obq_sge_rx_q0", NULL, NULL, "obq_ipc0"
7723 };
7724 
7725 static void
cim_sysctls(struct adapter * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * c0)7726 cim_sysctls(struct adapter *sc, struct sysctl_ctx_list *ctx,
7727     struct sysctl_oid_list *c0)
7728 {
7729 	struct sysctl_oid *oid;
7730 	struct sysctl_oid_list *children1;
7731 	int i, j, qcount;
7732 	char s[16];
7733 	const char **qname;
7734 
7735 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "cim",
7736 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM block");
7737 	c0 = SYSCTL_CHILDREN(oid);
7738 
7739 	SYSCTL_ADD_U8(ctx, c0, OID_AUTO, "ncores", CTLFLAG_RD, NULL,
7740 	    sc->params.ncores, "# of active CIM cores");
7741 
7742 	for (i = 0; i < sc->params.ncores; i++) {
7743 		snprintf(s, sizeof(s), "%u", i);
7744 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, s,
7745 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "CIM core");
7746 		children1 = SYSCTL_CHILDREN(oid);
7747 
7748 		/*
7749 		 * CTLFLAG_SKIP because the misc.devlog sysctl already displays
7750 		 * the log for all cores.  Use this sysctl to get the log for a
7751 		 * particular core only.
7752 		 */
7753 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "devlog",
7754 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
7755 		    sc, i, sysctl_devlog, "A", "firmware's device log");
7756 
7757 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "loadavg",
7758 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7759 		    sysctl_loadavg, "A",
7760 		    "microprocessor load averages (select firmwares only)");
7761 
7762 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "qcfg",
7763 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7764 		    chip_id(sc) > CHELSIO_T6 ? sysctl_cim_qcfg_t7 : sysctl_cim_qcfg,
7765 		    "A", "Queue configuration");
7766 
7767 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "la",
7768 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7769 		    sysctl_cim_la, "A", "Logic analyzer");
7770 
7771 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "ma_la",
7772 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7773 		    sysctl_cim_ma_la, "A", "CIM MA logic analyzer");
7774 
7775 		SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, "pif_la",
7776 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
7777 		    sysctl_cim_pif_la, "A", "CIM PIF logic analyzer");
7778 
7779 		/* IBQs */
7780 		switch (chip_id(sc)) {
7781 		case CHELSIO_T4:
7782 		case CHELSIO_T5:
7783 		case CHELSIO_T6:
7784 			qname = &t4_ibq[0];
7785 			qcount = nitems(t4_ibq);
7786 			break;
7787 		case CHELSIO_T7:
7788 		default:
7789 			if (i == 0) {
7790 				qname = &t7_ibq[0];
7791 				qcount = nitems(t7_ibq);
7792 			} else {
7793 				qname = &t7_ibq_sec[0];
7794 				qcount = nitems(t7_ibq_sec);
7795 			}
7796 			break;
7797 		}
7798 		MPASS(qcount <= sc->chip_params->cim_num_ibq);
7799 		for (j = 0; j < qcount; j++) {
7800 			if (qname[j] == NULL)
7801 				continue;
7802 			SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7803 			    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7804 			    (i << 16) | j, sysctl_cim_ibq, "A", NULL);
7805 		}
7806 
7807 		/* OBQs */
7808 		switch (chip_id(sc)) {
7809 		case CHELSIO_T4:
7810 			qname = t4_obq;
7811 			qcount = CIM_NUM_OBQ;
7812 			break;
7813 		case CHELSIO_T5:
7814 		case CHELSIO_T6:
7815 			qname = t4_obq;
7816 			qcount = nitems(t4_obq);
7817 			break;
7818 		case CHELSIO_T7:
7819 		default:
7820 			if (i == 0) {
7821 				qname = t7_obq;
7822 				qcount = nitems(t7_obq);
7823 			} else {
7824 				qname = t7_obq_sec;
7825 				qcount = nitems(t7_obq_sec);
7826 			}
7827 			break;
7828 		}
7829 		MPASS(qcount <= sc->chip_params->cim_num_obq);
7830 		for (j = 0; j < qcount; j++) {
7831 			if (qname[j] == NULL)
7832 				continue;
7833 			SYSCTL_ADD_PROC(ctx, children1, OID_AUTO, qname[j],
7834 			    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
7835 			    (i << 16) | j, sysctl_cim_obq, "A", NULL);
7836 		}
7837 	}
7838 }
7839 
7840 /*
7841  * Should match fw_caps_config_<foo> enums in t4fw_interface.h
7842  */
7843 static char *caps_decoder[] = {
7844 	"\20\001IPMI\002NCSI",				/* 0: NBM */
7845 	"\20\001PPP\002QFC\003DCBX",			/* 1: link */
7846 	"\20\001INGRESS\002EGRESS",			/* 2: switch */
7847 	"\20\001NIC\002VM\003IDS\004UM\005UM_ISGL"	/* 3: NIC */
7848 	    "\006HASHFILTER\007ETHOFLD",
7849 	"\20\001TOE\002SENDPATH",			/* 4: TOE */
7850 	"\20\001RDDP\002RDMAC\003ROCEv2",		/* 5: RDMA */
7851 	"\20\001INITIATOR_PDU\002TARGET_PDU"		/* 6: iSCSI */
7852 	    "\003INITIATOR_CNXOFLD\004TARGET_CNXOFLD"
7853 	    "\005INITIATOR_SSNOFLD\006TARGET_SSNOFLD"
7854 	    "\007T10DIF"
7855 	    "\010INITIATOR_CMDOFLD\011TARGET_CMDOFLD",
7856 	"\20\001LOOKASIDE\002TLSKEYS\003IPSEC_INLINE"	/* 7: Crypto */
7857 	    "\004TLS_HW,\005TOE_IPSEC",
7858 	"\20\001INITIATOR\002TARGET\003CTRL_OFLD"	/* 8: FCoE */
7859 		    "\004PO_INITIATOR\005PO_TARGET",
7860 	"\20\001NVMe_TCP",				/* 9: NVMe */
7861 };
7862 
7863 void
t4_sysctls(struct adapter * sc)7864 t4_sysctls(struct adapter *sc)
7865 {
7866 	struct sysctl_ctx_list *ctx = &sc->ctx;
7867 	struct sysctl_oid *oid;
7868 	struct sysctl_oid_list *children, *c0;
7869 	static char *doorbells = {"\20\1UDB\2WCWR\3UDBWC\4KDB"};
7870 
7871 	/*
7872 	 * dev.t4nex.X.
7873 	 */
7874 	oid = device_get_sysctl_tree(sc->dev);
7875 	c0 = children = SYSCTL_CHILDREN(oid);
7876 
7877 	sc->sc_do_rxcopy = 1;
7878 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "do_rx_copy", CTLFLAG_RW,
7879 	    &sc->sc_do_rxcopy, 1, "Do RX copy of small frames");
7880 
7881 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nports", CTLFLAG_RD, NULL,
7882 	    sc->params.nports, "# of ports");
7883 
7884 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "doorbells",
7885 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, doorbells,
7886 	    (uintptr_t)&sc->doorbells, sysctl_bitfield_8b, "A",
7887 	    "available doorbells");
7888 
7889 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "core_clock", CTLFLAG_RD, NULL,
7890 	    sc->params.vpd.cclk, "core clock frequency (in KHz)");
7891 
7892 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_timers",
7893 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7894 	    sc->params.sge.timer_val, sizeof(sc->params.sge.timer_val),
7895 	    sysctl_int_array, "A", "interrupt holdoff timer values (us)");
7896 
7897 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pkt_counts",
7898 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
7899 	    sc->params.sge.counter_val, sizeof(sc->params.sge.counter_val),
7900 	    sysctl_int_array, "A", "interrupt holdoff packet counter values");
7901 
7902 	t4_sge_sysctls(sc, ctx, children);
7903 
7904 	sc->lro_timeout = 100;
7905 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lro_timeout", CTLFLAG_RW,
7906 	    &sc->lro_timeout, 0, "lro inactive-flush timeout (in us)");
7907 
7908 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "dflags", CTLFLAG_RW,
7909 	    &sc->debug_flags, 0, "flags to enable runtime debugging");
7910 
7911 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "tp_version",
7912 	    CTLFLAG_RD, sc->tp_version, 0, "TP microcode version");
7913 
7914 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "firmware_version",
7915 	    CTLFLAG_RD, sc->fw_version, 0, "firmware version");
7916 
7917 	if (sc->flags & IS_VF)
7918 		return;
7919 
7920 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "hw_revision", CTLFLAG_RD,
7921 	    NULL, chip_rev(sc), "chip hardware revision");
7922 
7923 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "sn",
7924 	    CTLFLAG_RD, sc->params.vpd.sn, 0, "serial number");
7925 
7926 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "pn",
7927 	    CTLFLAG_RD, sc->params.vpd.pn, 0, "part number");
7928 
7929 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "ec",
7930 	    CTLFLAG_RD, sc->params.vpd.ec, 0, "engineering change");
7931 
7932 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "md_version",
7933 	    CTLFLAG_RD, sc->params.vpd.md, 0, "manufacturing diags version");
7934 
7935 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "na",
7936 	    CTLFLAG_RD, sc->params.vpd.na, 0, "network address");
7937 
7938 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "er_version", CTLFLAG_RD,
7939 	    sc->er_version, 0, "expansion ROM version");
7940 
7941 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "bs_version", CTLFLAG_RD,
7942 	    sc->bs_version, 0, "bootstrap firmware version");
7943 
7944 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "scfg_version", CTLFLAG_RD,
7945 	    NULL, sc->params.scfg_vers, "serial config version");
7946 
7947 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "vpd_version", CTLFLAG_RD,
7948 	    NULL, sc->params.vpd_vers, "VPD version");
7949 
7950 	SYSCTL_ADD_STRING(ctx, children, OID_AUTO, "cf",
7951 	    CTLFLAG_RD, sc->cfg_file, 0, "configuration file");
7952 
7953 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "cfcsum", CTLFLAG_RD, NULL,
7954 	    sc->cfcsum, "config file checksum");
7955 
7956 #define SYSCTL_CAP(name, n, text) \
7957 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, #name, \
7958 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, caps_decoder[n], \
7959 	    (uintptr_t)&sc->name, sysctl_bitfield_16b, "A", \
7960 	    "available " text " capabilities")
7961 
7962 	SYSCTL_CAP(nbmcaps, 0, "NBM");
7963 	SYSCTL_CAP(linkcaps, 1, "link");
7964 	SYSCTL_CAP(switchcaps, 2, "switch");
7965 	SYSCTL_CAP(nvmecaps, 9, "NVMe");
7966 	SYSCTL_CAP(niccaps, 3, "NIC");
7967 	SYSCTL_CAP(toecaps, 4, "TCP offload");
7968 	SYSCTL_CAP(rdmacaps, 5, "RDMA");
7969 	SYSCTL_CAP(iscsicaps, 6, "iSCSI");
7970 	SYSCTL_CAP(cryptocaps, 7, "crypto");
7971 	SYSCTL_CAP(fcoecaps, 8, "FCoE");
7972 #undef SYSCTL_CAP
7973 
7974 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nfilters", CTLFLAG_RD,
7975 	    NULL, sc->tids.nftids, "number of filters");
7976 
7977 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
7978 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
7979 	    sysctl_temperature, "I", "chip temperature (in Celsius)");
7980 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset_sensor",
7981 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
7982 	    sysctl_reset_sensor, "I", "reset the chip's temperature sensor.");
7983 
7984 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "core_vdd",
7985 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, sysctl_vdd,
7986 	    "I", "core Vdd (in mV)");
7987 
7988 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "local_cpus",
7989 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, LOCAL_CPUS,
7990 	    sysctl_cpus, "A", "local CPUs");
7991 
7992 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "intr_cpus",
7993 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, INTR_CPUS,
7994 	    sysctl_cpus, "A", "preferred CPUs for interrupts");
7995 
7996 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "swintr", CTLFLAG_RW,
7997 	    &sc->swintr, 0, "software triggered interrupts");
7998 
7999 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reset",
8000 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_reset, "I",
8001 	    "1 = reset adapter, 0 = zero reset counter");
8002 
8003 	/*
8004 	 * dev.t4nex.X.misc.  Marked CTLFLAG_SKIP to avoid information overload.
8005 	 */
8006 	oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "misc",
8007 	    CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL,
8008 	    "logs and miscellaneous information");
8009 	children = SYSCTL_CHILDREN(oid);
8010 
8011 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cctrl",
8012 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8013 	    sysctl_cctrl, "A", "congestion control");
8014 
8015 	cim_sysctls(sc, ctx, children);
8016 
8017 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "cpl_stats",
8018 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8019 	    sysctl_cpl_stats, "A", "CPL statistics");
8020 
8021 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ddp_stats",
8022 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8023 	    sysctl_ddp_stats, "A", "non-TCP DDP statistics");
8024 
8025 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tid_stats",
8026 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8027 	    sysctl_tid_stats, "A", "tid stats");
8028 
8029 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "devlog",
8030 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, -1,
8031 	    sysctl_devlog, "A", "firmware's device log (all cores)");
8032 
8033 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fcoe_stats",
8034 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8035 	    sysctl_fcoe_stats, "A", "FCoE statistics");
8036 
8037 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_sched",
8038 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8039 	    sysctl_hw_sched, "A", "hardware scheduler ");
8040 
8041 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "l2t",
8042 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8043 	    sysctl_l2t, "A", "hardware L2 table");
8044 
8045 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "smt",
8046 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8047 	    sysctl_smt, "A", "hardware source MAC table");
8048 
8049 #ifdef INET6
8050 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "clip",
8051 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8052 	    sysctl_clip, "A", "active CLIP table entries");
8053 #endif
8054 
8055 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "lb_stats",
8056 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8057 	    sysctl_lb_stats, "A", "loopback statistics");
8058 
8059 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "meminfo",
8060 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8061 	    sysctl_meminfo, "A", "memory regions");
8062 
8063 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mps_tcam",
8064 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8065 	    chip_id(sc) >= CHELSIO_T7 ? sysctl_mps_tcam_t7 :
8066 	    (chip_id(sc) >= CHELSIO_T6 ? sysctl_mps_tcam_t6 : sysctl_mps_tcam),
8067 	    "A", "MPS TCAM entries");
8068 
8069 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "path_mtus",
8070 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8071 	    sysctl_path_mtus, "A", "path MTUs");
8072 
8073 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pm_stats",
8074 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8075 	    sysctl_pm_stats, "A", "PM statistics");
8076 
8077 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rdma_stats",
8078 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8079 	    sysctl_rdma_stats, "A", "RDMA statistics");
8080 
8081 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tcp_stats",
8082 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8083 	    sysctl_tcp_stats, "A", "TCP statistics");
8084 
8085 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tids",
8086 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8087 	    sysctl_tids, "A", "TID information");
8088 
8089 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_err_stats",
8090 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8091 	    sysctl_tp_err_stats, "A", "TP error statistics");
8092 
8093 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tnl_stats",
8094 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8095 	    sysctl_tnl_stats, "A", "TP tunnel statistics");
8096 
8097 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la_mask",
8098 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
8099 	    sysctl_tp_la_mask, "I", "TP logic analyzer event capture mask");
8100 
8101 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tp_la",
8102 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8103 	    sysctl_tp_la, "A", "TP logic analyzer");
8104 
8105 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_rate",
8106 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8107 	    sysctl_tx_rate, "A", "Tx rate");
8108 
8109 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ulprx_la",
8110 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8111 	    sysctl_ulprx_la, "A", "ULPRX logic analyzer");
8112 
8113 	if (chip_id(sc) >= CHELSIO_T5) {
8114 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "wcwr_stats",
8115 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8116 		    sysctl_wcwr_stats, "A", "write combined work requests");
8117 	}
8118 
8119 #ifdef KERN_TLS
8120 	if (is_ktls(sc)) {
8121 		/*
8122 		 * dev.t4nex.0.tls.
8123 		 */
8124 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "tls",
8125 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "KERN_TLS parameters");
8126 		children = SYSCTL_CHILDREN(oid);
8127 
8128 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "inline_keys",
8129 		    CTLFLAG_RW, &sc->tlst.inline_keys, 0, "Always pass TLS "
8130 		    "keys in work requests (1) or attempt to store TLS keys "
8131 		    "in card memory.");
8132 
8133 		if (is_t6(sc))
8134 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "combo_wrs",
8135 			    CTLFLAG_RW, &sc->tlst.combo_wrs, 0, "Attempt to "
8136 			    "combine TCB field updates with TLS record work "
8137 			    "requests.");
8138 		else {
8139 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "short_records",
8140 			    CTLFLAG_RW, &sc->tlst.short_records, 0,
8141 			    "Use cipher-only mode for short records.");
8142 			SYSCTL_ADD_INT(ctx, children, OID_AUTO, "partial_ghash",
8143 			    CTLFLAG_RW, &sc->tlst.partial_ghash, 0,
8144 			    "Use partial GHASH for AES-GCM records.");
8145 		}
8146 	}
8147 #endif
8148 
8149 #ifdef TCP_OFFLOAD
8150 	if (is_offload(sc)) {
8151 		int i;
8152 		char s[4];
8153 
8154 		/*
8155 		 * dev.t4nex.X.toe.
8156 		 */
8157 		oid = SYSCTL_ADD_NODE(ctx, c0, OID_AUTO, "toe",
8158 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TOE parameters");
8159 		children = SYSCTL_CHILDREN(oid);
8160 
8161 		sc->tt.cong_algorithm = -1;
8162 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "cong_algorithm",
8163 		    CTLFLAG_RW, &sc->tt.cong_algorithm, 0, "congestion control "
8164 		    "(-1 = default, 0 = reno, 1 = tahoe, 2 = newreno, "
8165 		    "3 = highspeed)");
8166 
8167 		sc->tt.sndbuf = -1;
8168 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "sndbuf", CTLFLAG_RW,
8169 		    &sc->tt.sndbuf, 0, "hardware send buffer");
8170 
8171 		sc->tt.ddp = 0;
8172 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ddp",
8173 		    CTLFLAG_RW | CTLFLAG_SKIP, &sc->tt.ddp, 0, "");
8174 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_zcopy", CTLFLAG_RW,
8175 		    &sc->tt.ddp, 0, "Enable zero-copy aio_read(2)");
8176 
8177 		sc->tt.rx_coalesce = -1;
8178 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_coalesce",
8179 		    CTLFLAG_RW, &sc->tt.rx_coalesce, 0, "receive coalescing");
8180 
8181 		sc->tt.tls = 1;
8182 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tls", CTLTYPE_INT |
8183 		    CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0, sysctl_tls, "I",
8184 		    "Inline TLS allowed");
8185 
8186 		sc->tt.tx_align = -1;
8187 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_align",
8188 		    CTLFLAG_RW, &sc->tt.tx_align, 0, "chop and align payload");
8189 
8190 		sc->tt.tx_zcopy = 0;
8191 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_zcopy",
8192 		    CTLFLAG_RW, &sc->tt.tx_zcopy, 0,
8193 		    "Enable zero-copy aio_write(2)");
8194 
8195 		sc->tt.cop_managed_offloading = !!t4_cop_managed_offloading;
8196 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8197 		    "cop_managed_offloading", CTLFLAG_RW,
8198 		    &sc->tt.cop_managed_offloading, 0,
8199 		    "COP (Connection Offload Policy) controls all TOE offload");
8200 
8201 		sc->tt.autorcvbuf_inc = 16 * 1024;
8202 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "autorcvbuf_inc",
8203 		    CTLFLAG_RW, &sc->tt.autorcvbuf_inc, 0,
8204 		    "autorcvbuf increment");
8205 
8206 		sc->tt.update_hc_on_pmtu_change = 1;
8207 		SYSCTL_ADD_INT(ctx, children, OID_AUTO,
8208 		    "update_hc_on_pmtu_change", CTLFLAG_RW,
8209 		    &sc->tt.update_hc_on_pmtu_change, 0,
8210 		    "Update hostcache entry if the PMTU changes");
8211 
8212 		sc->tt.iso = 1;
8213 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "iso", CTLFLAG_RW,
8214 		    &sc->tt.iso, 0, "Enable iSCSI segmentation offload");
8215 
8216 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timer_tick",
8217 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8218 		    sysctl_tp_tick, "A", "TP timer tick (us)");
8219 
8220 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "timestamp_tick",
8221 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 1,
8222 		    sysctl_tp_tick, "A", "TCP timestamp tick (us)");
8223 
8224 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_tick",
8225 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 2,
8226 		    sysctl_tp_tick, "A", "DACK tick (us)");
8227 
8228 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dack_timer",
8229 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
8230 		    sysctl_tp_dack_timer, "IU", "DACK timer (us)");
8231 
8232 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_min",
8233 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8234 		    A_TP_RXT_MIN, sysctl_tp_timer, "LU",
8235 		    "Minimum retransmit interval (us)");
8236 
8237 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_max",
8238 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8239 		    A_TP_RXT_MAX, sysctl_tp_timer, "LU",
8240 		    "Maximum retransmit interval (us)");
8241 
8242 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_min",
8243 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8244 		    A_TP_PERS_MIN, sysctl_tp_timer, "LU",
8245 		    "Persist timer min (us)");
8246 
8247 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "persist_max",
8248 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8249 		    A_TP_PERS_MAX, sysctl_tp_timer, "LU",
8250 		    "Persist timer max (us)");
8251 
8252 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_idle",
8253 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8254 		    A_TP_KEEP_IDLE, sysctl_tp_timer, "LU",
8255 		    "Keepalive idle timer (us)");
8256 
8257 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_interval",
8258 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8259 		    A_TP_KEEP_INTVL, sysctl_tp_timer, "LU",
8260 		    "Keepalive interval timer (us)");
8261 
8262 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "initial_srtt",
8263 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8264 		    A_TP_INIT_SRTT, sysctl_tp_timer, "LU", "Initial SRTT (us)");
8265 
8266 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "finwait2_timer",
8267 		    CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8268 		    A_TP_FINWAIT2_TIMER, sysctl_tp_timer, "LU",
8269 		    "FINWAIT2 timer (us)");
8270 
8271 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "syn_rexmt_count",
8272 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8273 		    S_SYNSHIFTMAX, sysctl_tp_shift_cnt, "IU",
8274 		    "Number of SYN retransmissions before abort");
8275 
8276 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rexmt_count",
8277 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8278 		    S_RXTSHIFTMAXR2, sysctl_tp_shift_cnt, "IU",
8279 		    "Number of retransmissions before abort");
8280 
8281 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "keepalive_count",
8282 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8283 		    S_KEEPALIVEMAXR2, sysctl_tp_shift_cnt, "IU",
8284 		    "Number of keepalive probes before abort");
8285 
8286 		oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rexmt_backoff",
8287 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8288 		    "TOE retransmit backoffs");
8289 		children = SYSCTL_CHILDREN(oid);
8290 		for (i = 0; i < 16; i++) {
8291 			snprintf(s, sizeof(s), "%u", i);
8292 			SYSCTL_ADD_PROC(ctx, children, OID_AUTO, s,
8293 			    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8294 			    i, sysctl_tp_backoff, "IU",
8295 			    "TOE retransmit backoff");
8296 		}
8297 	}
8298 #endif
8299 }
8300 
8301 void
vi_sysctls(struct vi_info * vi)8302 vi_sysctls(struct vi_info *vi)
8303 {
8304 	struct sysctl_ctx_list *ctx = &vi->ctx;
8305 	struct sysctl_oid *oid;
8306 	struct sysctl_oid_list *children;
8307 
8308 	/*
8309 	 * dev.v?(cxgbe|cxl).X.
8310 	 */
8311 	oid = device_get_sysctl_tree(vi->dev);
8312 	children = SYSCTL_CHILDREN(oid);
8313 
8314 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "viid", CTLFLAG_RD, NULL,
8315 	    vi->viid, "VI identifer");
8316 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nrxq", CTLFLAG_RD,
8317 	    &vi->nrxq, 0, "# of rx queues");
8318 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "ntxq", CTLFLAG_RD,
8319 	    &vi->ntxq, 0, "# of tx queues");
8320 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_rxq", CTLFLAG_RD,
8321 	    &vi->first_rxq, 0, "index of first rx queue");
8322 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_txq", CTLFLAG_RD,
8323 	    &vi->first_txq, 0, "index of first tx queue");
8324 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_base", CTLFLAG_RD, NULL,
8325 	    vi->rss_base, "start of RSS indirection table");
8326 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "rss_size", CTLFLAG_RD, NULL,
8327 	    vi->rss_size, "size of RSS indirection table");
8328 
8329 	if (IS_MAIN_VI(vi)) {
8330 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rsrv_noflowq",
8331 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8332 		    sysctl_noflowq, "IU",
8333 		    "Reserve queue 0 for non-flowid packets");
8334 	}
8335 
8336 	if (vi->adapter->flags & IS_VF) {
8337 		MPASS(vi->flags & TX_USES_VM_WR);
8338 		SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_vm_wr", CTLFLAG_RD,
8339 		    NULL, 1, "use VM work requests for transmit");
8340 	} else {
8341 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_vm_wr",
8342 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8343 		    sysctl_tx_vm_wr, "I", "use VM work requestes for transmit");
8344 	}
8345 
8346 #ifdef TCP_OFFLOAD
8347 	if (vi->nofldrxq != 0) {
8348 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldrxq", CTLFLAG_RD,
8349 		    &vi->nofldrxq, 0,
8350 		    "# of rx queues for offloaded TCP connections");
8351 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_rxq",
8352 		    CTLFLAG_RD, &vi->first_ofld_rxq, 0,
8353 		    "index of first TOE rx queue");
8354 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx_ofld",
8355 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8356 		    sysctl_holdoff_tmr_idx_ofld, "I",
8357 		    "holdoff timer index for TOE queues");
8358 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx_ofld",
8359 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8360 		    sysctl_holdoff_pktc_idx_ofld, "I",
8361 		    "holdoff packet counter index for TOE queues");
8362 	}
8363 #endif
8364 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
8365 	if (vi->nofldtxq != 0) {
8366 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nofldtxq", CTLFLAG_RD,
8367 		    &vi->nofldtxq, 0,
8368 		    "# of tx queues for TOE/ETHOFLD");
8369 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_ofld_txq",
8370 		    CTLFLAG_RD, &vi->first_ofld_txq, 0,
8371 		    "index of first TOE/ETHOFLD tx queue");
8372 	}
8373 #endif
8374 #ifdef DEV_NETMAP
8375 	if (vi->nnmrxq != 0) {
8376 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmrxq", CTLFLAG_RD,
8377 		    &vi->nnmrxq, 0, "# of netmap rx queues");
8378 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "nnmtxq", CTLFLAG_RD,
8379 		    &vi->nnmtxq, 0, "# of netmap tx queues");
8380 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_rxq",
8381 		    CTLFLAG_RD, &vi->first_nm_rxq, 0,
8382 		    "index of first netmap rx queue");
8383 		SYSCTL_ADD_INT(ctx, children, OID_AUTO, "first_nm_txq",
8384 		    CTLFLAG_RD, &vi->first_nm_txq, 0,
8385 		    "index of first netmap tx queue");
8386 	}
8387 #endif
8388 
8389 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_tmr_idx",
8390 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8391 	    sysctl_holdoff_tmr_idx, "I", "holdoff timer index");
8392 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "holdoff_pktc_idx",
8393 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8394 	    sysctl_holdoff_pktc_idx, "I", "holdoff packet counter index");
8395 
8396 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_rxq",
8397 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8398 	    sysctl_qsize_rxq, "I", "rx queue size");
8399 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "qsize_txq",
8400 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, vi, 0,
8401 	    sysctl_qsize_txq, "I", "tx queue size");
8402 }
8403 
8404 static void
cxgbe_sysctls(struct port_info * pi)8405 cxgbe_sysctls(struct port_info *pi)
8406 {
8407 	struct sysctl_ctx_list *ctx = &pi->ctx;
8408 	struct sysctl_oid *oid;
8409 	struct sysctl_oid_list *children, *children2;
8410 	struct adapter *sc = pi->adapter;
8411 	int i;
8412 	char name[16];
8413 	static char *tc_flags = {"\20\1USER"};
8414 
8415 	/*
8416 	 * dev.cxgbe.X.
8417 	 */
8418 	oid = device_get_sysctl_tree(pi->dev);
8419 	children = SYSCTL_CHILDREN(oid);
8420 
8421 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "linkdnrc",
8422 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8423 	    sysctl_linkdnrc, "A", "reason why link is down");
8424 	if (pi->port_type == FW_PORT_TYPE_BT_XAUI) {
8425 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "temperature",
8426 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 0,
8427 		    sysctl_btphy, "I", "PHY temperature (in Celsius)");
8428 		SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "fw_version",
8429 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, 1,
8430 		    sysctl_btphy, "I", "PHY firmware version");
8431 	}
8432 
8433 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "pause_settings",
8434 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8435 	    sysctl_pause_settings, "A",
8436 	    "PAUSE settings (bit 0 = rx_pause, 1 = tx_pause, 2 = pause_autoneg)");
8437 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "link_fec",
8438 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_link_fec, "A",
8439 	    "FEC in use on the link");
8440 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "requested_fec",
8441 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8442 	    sysctl_requested_fec, "A",
8443 	    "FECs to use (bit 0 = RS, 1 = FC, 2 = none, 5 = auto, 6 = module)");
8444 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "module_fec",
8445 	    CTLTYPE_STRING | CTLFLAG_MPSAFE, pi, 0, sysctl_module_fec, "A",
8446 	    "FEC recommended by the cable/transceiver");
8447 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "autoneg",
8448 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8449 	    sysctl_autoneg, "I",
8450 	    "autonegotiation (-1 = not supported)");
8451 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "force_fec",
8452 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, pi, 0,
8453 	    sysctl_force_fec, "I", "when to use FORCE_FEC bit for link config");
8454 
8455 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rcaps", CTLFLAG_RD,
8456 	    &pi->link_cfg.requested_caps, 0, "L1 config requested by driver");
8457 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "pcaps", CTLFLAG_RD,
8458 	    &pi->link_cfg.pcaps, 0, "port capabilities");
8459 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "acaps", CTLFLAG_RD,
8460 	    &pi->link_cfg.acaps, 0, "advertised capabilities");
8461 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpacaps", CTLFLAG_RD,
8462 	    &pi->link_cfg.lpacaps, 0, "link partner advertised capabilities");
8463 
8464 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "max_speed", CTLFLAG_RD, NULL,
8465 	    port_top_speed(pi), "max speed (in Gbps)");
8466 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "mps_bg_map", CTLFLAG_RD, NULL,
8467 	    pi->mps_bg_map, "MPS buffer group map");
8468 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_e_chan_map", CTLFLAG_RD,
8469 	    NULL, pi->rx_e_chan_map, "TP rx e-channel map");
8470 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_chan", CTLFLAG_RD, NULL,
8471 	    pi->tx_chan, "TP tx c-channel");
8472 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_chan", CTLFLAG_RD, NULL,
8473 	    pi->rx_chan, "TP rx c-channel");
8474 
8475 	if (sc->flags & IS_VF)
8476 		return;
8477 
8478 	/*
8479 	 * dev.(cxgbe|cxl).X.tc.
8480 	 */
8481 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "tc",
8482 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
8483 	    "Tx scheduler traffic classes (cl_rl)");
8484 	children2 = SYSCTL_CHILDREN(oid);
8485 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "pktsize",
8486 	    CTLFLAG_RW, &pi->sched_params->pktsize, 0,
8487 	    "pktsize for per-flow cl-rl (0 means up to the driver )");
8488 	SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "burstsize",
8489 	    CTLFLAG_RW, &pi->sched_params->burstsize, 0,
8490 	    "burstsize for per-flow cl-rl (0 means up to the driver)");
8491 	for (i = 0; i < sc->params.nsched_cls; i++) {
8492 		struct tx_cl_rl_params *tc = &pi->sched_params->cl_rl[i];
8493 
8494 		snprintf(name, sizeof(name), "%d", i);
8495 		children2 = SYSCTL_CHILDREN(SYSCTL_ADD_NODE(ctx,
8496 		    SYSCTL_CHILDREN(oid), OID_AUTO, name,
8497 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "traffic class"));
8498 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "state",
8499 		    CTLFLAG_RD, &tc->state, 0, "current state");
8500 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "flags",
8501 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, tc_flags,
8502 		    (uintptr_t)&tc->flags, sysctl_bitfield_8b, "A", "flags");
8503 		SYSCTL_ADD_UINT(ctx, children2, OID_AUTO, "refcount",
8504 		    CTLFLAG_RD, &tc->refcount, 0, "references to this class");
8505 		SYSCTL_ADD_PROC(ctx, children2, OID_AUTO, "params",
8506 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
8507 		    (pi->port_id << 16) | i, sysctl_tc_params, "A",
8508 		    "traffic class parameters");
8509 	}
8510 
8511 	/*
8512 	 * dev.cxgbe.X.stats.
8513 	 */
8514 	oid = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
8515 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "port statistics");
8516 	children = SYSCTL_CHILDREN(oid);
8517 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "tx_parse_error", CTLFLAG_RD,
8518 	    &pi->tx_parse_error, 0,
8519 	    "# of tx packets with invalid length or # of segments");
8520 
8521 #define T4_LBSTAT(name, stat, desc) do { \
8522 	if (sc->params.tp.lb_mode) { \
8523 		SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8524 		    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, pi, \
8525 		    A_MPS_PORT_STAT_##stat##_L, \
8526 		    sysctl_handle_t4_portstat64, "QU", desc); \
8527 	} else { \
8528 		SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8529 		    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8530 		    t4_port_reg(sc, pi->tx_chan, A_MPS_PORT_STAT_##stat##_L), \
8531 		    sysctl_handle_t4_reg64, "QU", desc); \
8532 	} \
8533 } while (0)
8534 
8535 	T4_LBSTAT(tx_octets, TX_PORT_BYTES, "# of octets in good frames");
8536 	T4_LBSTAT(tx_frames, TX_PORT_FRAMES, "total # of good frames");
8537 	T4_LBSTAT(tx_bcast_frames, TX_PORT_BCAST, "# of broadcast frames");
8538 	T4_LBSTAT(tx_mcast_frames, TX_PORT_MCAST, "# of multicast frames");
8539 	T4_LBSTAT(tx_ucast_frames, TX_PORT_UCAST, "# of unicast frames");
8540 	T4_LBSTAT(tx_error_frames, TX_PORT_ERROR, "# of error frames");
8541 	T4_LBSTAT(tx_frames_64, TX_PORT_64B, "# of tx frames in this range");
8542 	T4_LBSTAT(tx_frames_65_127, TX_PORT_65B_127B, "# of tx frames in this range");
8543 	T4_LBSTAT(tx_frames_128_255, TX_PORT_128B_255B, "# of tx frames in this range");
8544 	T4_LBSTAT(tx_frames_256_511, TX_PORT_256B_511B, "# of tx frames in this range");
8545 	T4_LBSTAT(tx_frames_512_1023, TX_PORT_512B_1023B, "# of tx frames in this range");
8546 	T4_LBSTAT(tx_frames_1024_1518, TX_PORT_1024B_1518B, "# of tx frames in this range");
8547 	T4_LBSTAT(tx_frames_1519_max, TX_PORT_1519B_MAX, "# of tx frames in this range");
8548 	T4_LBSTAT(tx_drop, TX_PORT_DROP, "# of dropped tx frames");
8549 	T4_LBSTAT(tx_pause, TX_PORT_PAUSE, "# of pause frames transmitted");
8550 	T4_LBSTAT(tx_ppp0, TX_PORT_PPP0, "# of PPP prio 0 frames transmitted");
8551 	T4_LBSTAT(tx_ppp1, TX_PORT_PPP1, "# of PPP prio 1 frames transmitted");
8552 	T4_LBSTAT(tx_ppp2, TX_PORT_PPP2, "# of PPP prio 2 frames transmitted");
8553 	T4_LBSTAT(tx_ppp3, TX_PORT_PPP3, "# of PPP prio 3 frames transmitted");
8554 	T4_LBSTAT(tx_ppp4, TX_PORT_PPP4, "# of PPP prio 4 frames transmitted");
8555 	T4_LBSTAT(tx_ppp5, TX_PORT_PPP5, "# of PPP prio 5 frames transmitted");
8556 	T4_LBSTAT(tx_ppp6, TX_PORT_PPP6, "# of PPP prio 6 frames transmitted");
8557 	T4_LBSTAT(tx_ppp7, TX_PORT_PPP7, "# of PPP prio 7 frames transmitted");
8558 
8559 	T4_LBSTAT(rx_octets, RX_PORT_BYTES, "# of octets in good frames");
8560 	T4_LBSTAT(rx_frames, RX_PORT_FRAMES, "total # of good frames");
8561 	T4_LBSTAT(rx_bcast_frames, RX_PORT_BCAST, "# of broadcast frames");
8562 	T4_LBSTAT(rx_mcast_frames, RX_PORT_MCAST, "# of multicast frames");
8563 	T4_LBSTAT(rx_ucast_frames, RX_PORT_UCAST, "# of unicast frames");
8564 	T4_LBSTAT(rx_too_long, RX_PORT_MTU_ERROR, "# of frames exceeding MTU");
8565 	T4_LBSTAT(rx_jabber, RX_PORT_MTU_CRC_ERROR, "# of jabber frames");
8566 	if (is_t6(sc)) {
8567 		/* Read from port_stats and may be stale by up to 1s */
8568 		SYSCTL_ADD_UQUAD(ctx, children, OID_AUTO, "rx_fcs_err",
8569 		    CTLFLAG_RD, &pi->stats.rx_fcs_err,
8570 		    "# of frames received with bad FCS since last link up");
8571 	} else {
8572 		T4_LBSTAT(rx_fcs_err, RX_PORT_CRC_ERROR,
8573 		    "# of frames received with bad FCS");
8574 	}
8575 	T4_LBSTAT(rx_len_err, RX_PORT_LEN_ERROR, "# of frames received with length error");
8576 	T4_LBSTAT(rx_symbol_err, RX_PORT_SYM_ERROR, "symbol errors");
8577 	T4_LBSTAT(rx_runt, RX_PORT_LESS_64B, "# of short frames received");
8578 	T4_LBSTAT(rx_frames_64, RX_PORT_64B, "# of rx frames in this range");
8579 	T4_LBSTAT(rx_frames_65_127, RX_PORT_65B_127B, "# of rx frames in this range");
8580 	T4_LBSTAT(rx_frames_128_255, RX_PORT_128B_255B, "# of rx frames in this range");
8581 	T4_LBSTAT(rx_frames_256_511, RX_PORT_256B_511B, "# of rx frames in this range");
8582 	T4_LBSTAT(rx_frames_512_1023, RX_PORT_512B_1023B, "# of rx frames in this range");
8583 	T4_LBSTAT(rx_frames_1024_1518, RX_PORT_1024B_1518B, "# of rx frames in this range");
8584 	T4_LBSTAT(rx_frames_1519_max, RX_PORT_1519B_MAX, "# of rx frames in this range");
8585 	T4_LBSTAT(rx_pause, RX_PORT_PAUSE, "# of pause frames received");
8586 	T4_LBSTAT(rx_ppp0, RX_PORT_PPP0, "# of PPP prio 0 frames received");
8587 	T4_LBSTAT(rx_ppp1, RX_PORT_PPP1, "# of PPP prio 1 frames received");
8588 	T4_LBSTAT(rx_ppp2, RX_PORT_PPP2, "# of PPP prio 2 frames received");
8589 	T4_LBSTAT(rx_ppp3, RX_PORT_PPP3, "# of PPP prio 3 frames received");
8590 	T4_LBSTAT(rx_ppp4, RX_PORT_PPP4, "# of PPP prio 4 frames received");
8591 	T4_LBSTAT(rx_ppp5, RX_PORT_PPP5, "# of PPP prio 5 frames received");
8592 	T4_LBSTAT(rx_ppp6, RX_PORT_PPP6, "# of PPP prio 6 frames received");
8593 	T4_LBSTAT(rx_ppp7, RX_PORT_PPP7, "# of PPP prio 7 frames received");
8594 #undef T4_LBSTAT
8595 
8596 #define T4_REGSTAT(name, stat, desc) do { \
8597 	SYSCTL_ADD_OID(ctx, children, OID_AUTO, #name, \
8598 	    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, \
8599 	    A_MPS_STAT_##stat##_L, sysctl_handle_t4_reg64, "QU", desc); \
8600 } while (0)
8601 
8602 	if (pi->mps_bg_map & 1) {
8603 		T4_REGSTAT(rx_ovflow0, RX_BG_0_MAC_DROP_FRAME,
8604 		    "# drops due to buffer-group 0 overflows");
8605 		T4_REGSTAT(rx_trunc0, RX_BG_0_MAC_TRUNC_FRAME,
8606 		    "# of buffer-group 0 truncated packets");
8607 	}
8608 	if (pi->mps_bg_map & 2) {
8609 		T4_REGSTAT(rx_ovflow1, RX_BG_1_MAC_DROP_FRAME,
8610 		    "# drops due to buffer-group 1 overflows");
8611 		T4_REGSTAT(rx_trunc1, RX_BG_1_MAC_TRUNC_FRAME,
8612 		    "# of buffer-group 1 truncated packets");
8613 	}
8614 	if (pi->mps_bg_map & 4) {
8615 		T4_REGSTAT(rx_ovflow2, RX_BG_2_MAC_DROP_FRAME,
8616 		    "# drops due to buffer-group 2 overflows");
8617 		T4_REGSTAT(rx_trunc2, RX_BG_2_MAC_TRUNC_FRAME,
8618 		    "# of buffer-group 2 truncated packets");
8619 	}
8620 	if (pi->mps_bg_map & 8) {
8621 		T4_REGSTAT(rx_ovflow3, RX_BG_3_MAC_DROP_FRAME,
8622 		    "# drops due to buffer-group 3 overflows");
8623 		T4_REGSTAT(rx_trunc3, RX_BG_3_MAC_TRUNC_FRAME,
8624 		    "# of buffer-group 3 truncated packets");
8625 	}
8626 #undef T4_REGSTAT
8627 }
8628 
8629 static int
sysctl_int_array(SYSCTL_HANDLER_ARGS)8630 sysctl_int_array(SYSCTL_HANDLER_ARGS)
8631 {
8632 	int rc, *i, space = 0;
8633 	struct sbuf sb;
8634 
8635 	sbuf_new_for_sysctl(&sb, NULL, 64, req);
8636 	for (i = arg1; arg2; arg2 -= sizeof(int), i++) {
8637 		if (space)
8638 			sbuf_printf(&sb, " ");
8639 		sbuf_printf(&sb, "%d", *i);
8640 		space = 1;
8641 	}
8642 	rc = sbuf_finish(&sb);
8643 	sbuf_delete(&sb);
8644 	return (rc);
8645 }
8646 
8647 static int
sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)8648 sysctl_bitfield_8b(SYSCTL_HANDLER_ARGS)
8649 {
8650 	int rc;
8651 	struct sbuf *sb;
8652 
8653 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8654 	if (sb == NULL)
8655 		return (ENOMEM);
8656 
8657 	sbuf_printf(sb, "%b", *(uint8_t *)(uintptr_t)arg2, (char *)arg1);
8658 	rc = sbuf_finish(sb);
8659 	sbuf_delete(sb);
8660 
8661 	return (rc);
8662 }
8663 
8664 static int
sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)8665 sysctl_bitfield_16b(SYSCTL_HANDLER_ARGS)
8666 {
8667 	int rc;
8668 	struct sbuf *sb;
8669 
8670 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8671 	if (sb == NULL)
8672 		return (ENOMEM);
8673 
8674 	sbuf_printf(sb, "%b", *(uint16_t *)(uintptr_t)arg2, (char *)arg1);
8675 	rc = sbuf_finish(sb);
8676 	sbuf_delete(sb);
8677 
8678 	return (rc);
8679 }
8680 
8681 static int
sysctl_btphy(SYSCTL_HANDLER_ARGS)8682 sysctl_btphy(SYSCTL_HANDLER_ARGS)
8683 {
8684 	struct port_info *pi = arg1;
8685 	int op = arg2;
8686 	struct adapter *sc = pi->adapter;
8687 	u_int v;
8688 	int rc;
8689 
8690 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4btt");
8691 	if (rc)
8692 		return (rc);
8693 	if (!hw_all_ok(sc))
8694 		rc = ENXIO;
8695 	else {
8696 		/* XXX: magic numbers */
8697 		rc = -t4_mdio_rd(sc, sc->mbox, pi->mdio_addr, 0x1e,
8698 		    op ? 0x20 : 0xc820, &v);
8699 	}
8700 	end_synchronized_op(sc, 0);
8701 	if (rc)
8702 		return (rc);
8703 	if (op == 0)
8704 		v /= 256;
8705 
8706 	rc = sysctl_handle_int(oidp, &v, 0, req);
8707 	return (rc);
8708 }
8709 
8710 static int
sysctl_noflowq(SYSCTL_HANDLER_ARGS)8711 sysctl_noflowq(SYSCTL_HANDLER_ARGS)
8712 {
8713 	struct vi_info *vi = arg1;
8714 	int rc, val;
8715 
8716 	val = vi->rsrv_noflowq;
8717 	rc = sysctl_handle_int(oidp, &val, 0, req);
8718 	if (rc != 0 || req->newptr == NULL)
8719 		return (rc);
8720 
8721 	if ((val >= 1) && (vi->ntxq > 1))
8722 		vi->rsrv_noflowq = 1;
8723 	else
8724 		vi->rsrv_noflowq = 0;
8725 
8726 	return (rc);
8727 }
8728 
8729 static int
sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)8730 sysctl_tx_vm_wr(SYSCTL_HANDLER_ARGS)
8731 {
8732 	struct vi_info *vi = arg1;
8733 	struct adapter *sc = vi->adapter;
8734 	int rc, val, i;
8735 
8736 	MPASS(!(sc->flags & IS_VF));
8737 
8738 	val = vi->flags & TX_USES_VM_WR ? 1 : 0;
8739 	rc = sysctl_handle_int(oidp, &val, 0, req);
8740 	if (rc != 0 || req->newptr == NULL)
8741 		return (rc);
8742 
8743 	if (val != 0 && val != 1)
8744 		return (EINVAL);
8745 
8746 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8747 	    "t4txvm");
8748 	if (rc)
8749 		return (rc);
8750 	if (!hw_all_ok(sc))
8751 		rc = ENXIO;
8752 	else if (if_getdrvflags(vi->ifp) & IFF_DRV_RUNNING) {
8753 		/*
8754 		 * We don't want parse_pkt to run with one setting (VF or PF)
8755 		 * and then eth_tx to see a different setting but still use
8756 		 * stale information calculated by parse_pkt.
8757 		 */
8758 		rc = EBUSY;
8759 	} else {
8760 		struct port_info *pi = vi->pi;
8761 		struct sge_txq *txq;
8762 		uint32_t ctrl0;
8763 		uint8_t npkt = sc->params.max_pkts_per_eth_tx_pkts_wr;
8764 
8765 		if (val) {
8766 			vi->flags |= TX_USES_VM_WR;
8767 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_VM_TSO);
8768 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8769 			    V_TXPKT_INTF(pi->hw_port));
8770 			if (!(sc->flags & IS_VF))
8771 				npkt--;
8772 		} else {
8773 			vi->flags &= ~TX_USES_VM_WR;
8774 			if_sethwtsomaxsegcount(vi->ifp, TX_SGL_SEGS_TSO);
8775 			ctrl0 = htobe32(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
8776 			    V_TXPKT_INTF(pi->hw_port) | V_TXPKT_PF(sc->pf) |
8777 			    V_TXPKT_VF(vi->vin) | V_TXPKT_VF_VLD(vi->vfvld));
8778 		}
8779 		for_each_txq(vi, i, txq) {
8780 			txq->cpl_ctrl0 = ctrl0;
8781 			txq->txp.max_npkt = npkt;
8782 		}
8783 	}
8784 	end_synchronized_op(sc, LOCK_HELD);
8785 	return (rc);
8786 }
8787 
8788 static int
sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)8789 sysctl_holdoff_tmr_idx(SYSCTL_HANDLER_ARGS)
8790 {
8791 	struct vi_info *vi = arg1;
8792 	struct adapter *sc = vi->adapter;
8793 	int idx, rc, i;
8794 	struct sge_rxq *rxq;
8795 	uint8_t v;
8796 
8797 	idx = vi->tmr_idx;
8798 
8799 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8800 	if (rc != 0 || req->newptr == NULL)
8801 		return (rc);
8802 
8803 	if (idx < 0 || idx >= SGE_NTIMERS)
8804 		return (EINVAL);
8805 
8806 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8807 	    "t4tmr");
8808 	if (rc)
8809 		return (rc);
8810 
8811 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->pktc_idx != -1);
8812 	for_each_rxq(vi, i, rxq) {
8813 #ifdef atomic_store_rel_8
8814 		atomic_store_rel_8(&rxq->iq.intr_params, v);
8815 #else
8816 		rxq->iq.intr_params = v;
8817 #endif
8818 	}
8819 	vi->tmr_idx = idx;
8820 
8821 	end_synchronized_op(sc, LOCK_HELD);
8822 	return (0);
8823 }
8824 
8825 static int
sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)8826 sysctl_holdoff_pktc_idx(SYSCTL_HANDLER_ARGS)
8827 {
8828 	struct vi_info *vi = arg1;
8829 	struct adapter *sc = vi->adapter;
8830 	int idx, rc;
8831 
8832 	idx = vi->pktc_idx;
8833 
8834 	rc = sysctl_handle_int(oidp, &idx, 0, req);
8835 	if (rc != 0 || req->newptr == NULL)
8836 		return (rc);
8837 
8838 	if (idx < -1 || idx >= SGE_NCOUNTERS)
8839 		return (EINVAL);
8840 
8841 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8842 	    "t4pktc");
8843 	if (rc)
8844 		return (rc);
8845 
8846 	if (vi->flags & VI_INIT_DONE)
8847 		rc = EBUSY; /* cannot be changed once the queues are created */
8848 	else
8849 		vi->pktc_idx = idx;
8850 
8851 	end_synchronized_op(sc, LOCK_HELD);
8852 	return (rc);
8853 }
8854 
8855 static int
sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)8856 sysctl_qsize_rxq(SYSCTL_HANDLER_ARGS)
8857 {
8858 	struct vi_info *vi = arg1;
8859 	struct adapter *sc = vi->adapter;
8860 	int qsize, rc;
8861 
8862 	qsize = vi->qsize_rxq;
8863 
8864 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8865 	if (rc != 0 || req->newptr == NULL)
8866 		return (rc);
8867 
8868 	if (qsize < 128 || (qsize & 7))
8869 		return (EINVAL);
8870 
8871 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8872 	    "t4rxqs");
8873 	if (rc)
8874 		return (rc);
8875 
8876 	if (vi->flags & VI_INIT_DONE)
8877 		rc = EBUSY; /* cannot be changed once the queues are created */
8878 	else
8879 		vi->qsize_rxq = qsize;
8880 
8881 	end_synchronized_op(sc, LOCK_HELD);
8882 	return (rc);
8883 }
8884 
8885 static int
sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)8886 sysctl_qsize_txq(SYSCTL_HANDLER_ARGS)
8887 {
8888 	struct vi_info *vi = arg1;
8889 	struct adapter *sc = vi->adapter;
8890 	int qsize, rc;
8891 
8892 	qsize = vi->qsize_txq;
8893 
8894 	rc = sysctl_handle_int(oidp, &qsize, 0, req);
8895 	if (rc != 0 || req->newptr == NULL)
8896 		return (rc);
8897 
8898 	if (qsize < 128 || qsize > 65536)
8899 		return (EINVAL);
8900 
8901 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
8902 	    "t4txqs");
8903 	if (rc)
8904 		return (rc);
8905 
8906 	if (vi->flags & VI_INIT_DONE)
8907 		rc = EBUSY; /* cannot be changed once the queues are created */
8908 	else
8909 		vi->qsize_txq = qsize;
8910 
8911 	end_synchronized_op(sc, LOCK_HELD);
8912 	return (rc);
8913 }
8914 
8915 static int
sysctl_pause_settings(SYSCTL_HANDLER_ARGS)8916 sysctl_pause_settings(SYSCTL_HANDLER_ARGS)
8917 {
8918 	struct port_info *pi = arg1;
8919 	struct adapter *sc = pi->adapter;
8920 	struct link_config *lc = &pi->link_cfg;
8921 	int rc;
8922 
8923 	if (req->newptr == NULL) {
8924 		struct sbuf *sb;
8925 		static char *bits = "\20\1RX\2TX\3AUTO";
8926 
8927 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8928 		if (sb == NULL)
8929 			return (ENOMEM);
8930 
8931 		if (lc->link_ok) {
8932 			sbuf_printf(sb, "%b", (lc->fc & (PAUSE_TX | PAUSE_RX)) |
8933 			    (lc->requested_fc & PAUSE_AUTONEG), bits);
8934 		} else {
8935 			sbuf_printf(sb, "%b", lc->requested_fc & (PAUSE_TX |
8936 			    PAUSE_RX | PAUSE_AUTONEG), bits);
8937 		}
8938 		rc = sbuf_finish(sb);
8939 		sbuf_delete(sb);
8940 	} else {
8941 		char s[2];
8942 		int n;
8943 
8944 		s[0] = '0' + (lc->requested_fc & (PAUSE_TX | PAUSE_RX |
8945 		    PAUSE_AUTONEG));
8946 		s[1] = 0;
8947 
8948 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
8949 		if (rc != 0)
8950 			return(rc);
8951 
8952 		if (s[1] != 0)
8953 			return (EINVAL);
8954 		if (s[0] < '0' || s[0] > '9')
8955 			return (EINVAL);	/* not a number */
8956 		n = s[0] - '0';
8957 		if (n & ~(PAUSE_TX | PAUSE_RX | PAUSE_AUTONEG))
8958 			return (EINVAL);	/* some other bit is set too */
8959 
8960 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
8961 		    "t4PAUSE");
8962 		if (rc)
8963 			return (rc);
8964 		if (hw_all_ok(sc)) {
8965 			PORT_LOCK(pi);
8966 			lc->requested_fc = n;
8967 			fixup_link_config(pi);
8968 			if (pi->up_vis > 0)
8969 				rc = apply_link_config(pi);
8970 			set_current_media(pi);
8971 			PORT_UNLOCK(pi);
8972 		}
8973 		end_synchronized_op(sc, 0);
8974 	}
8975 
8976 	return (rc);
8977 }
8978 
8979 static int
sysctl_link_fec(SYSCTL_HANDLER_ARGS)8980 sysctl_link_fec(SYSCTL_HANDLER_ARGS)
8981 {
8982 	struct port_info *pi = arg1;
8983 	struct link_config *lc = &pi->link_cfg;
8984 	int rc;
8985 	struct sbuf *sb;
8986 
8987 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
8988 	if (sb == NULL)
8989 		return (ENOMEM);
8990 	if (lc->link_ok)
8991 		sbuf_printf(sb, "%b", lc->fec, t4_fec_bits);
8992 	else
8993 		sbuf_printf(sb, "no link");
8994 	rc = sbuf_finish(sb);
8995 	sbuf_delete(sb);
8996 
8997 	return (rc);
8998 }
8999 
9000 static int
sysctl_requested_fec(SYSCTL_HANDLER_ARGS)9001 sysctl_requested_fec(SYSCTL_HANDLER_ARGS)
9002 {
9003 	struct port_info *pi = arg1;
9004 	struct adapter *sc = pi->adapter;
9005 	struct link_config *lc = &pi->link_cfg;
9006 	int rc;
9007 	int8_t old;
9008 
9009 	if (req->newptr == NULL) {
9010 		struct sbuf *sb;
9011 
9012 		sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9013 		if (sb == NULL)
9014 			return (ENOMEM);
9015 
9016 		sbuf_printf(sb, "%b", lc->requested_fec, t4_fec_bits);
9017 		rc = sbuf_finish(sb);
9018 		sbuf_delete(sb);
9019 	} else {
9020 		char s[8];
9021 		int n;
9022 
9023 		snprintf(s, sizeof(s), "%d",
9024 		    lc->requested_fec == FEC_AUTO ? -1 :
9025 		    lc->requested_fec & (M_FW_PORT_CAP32_FEC | FEC_MODULE));
9026 
9027 		rc = sysctl_handle_string(oidp, s, sizeof(s), req);
9028 		if (rc != 0)
9029 			return(rc);
9030 
9031 		n = strtol(&s[0], NULL, 0);
9032 		if (n < 0 || n & FEC_AUTO)
9033 			n = FEC_AUTO;
9034 		else if (n & ~(M_FW_PORT_CAP32_FEC | FEC_MODULE))
9035 			return (EINVAL);/* some other bit is set too */
9036 
9037 		rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9038 		    "t4reqf");
9039 		if (rc)
9040 			return (rc);
9041 		PORT_LOCK(pi);
9042 		old = lc->requested_fec;
9043 		if (n == FEC_AUTO)
9044 			lc->requested_fec = FEC_AUTO;
9045 		else if (n == 0 || n == FEC_NONE)
9046 			lc->requested_fec = FEC_NONE;
9047 		else {
9048 			if ((lc->pcaps |
9049 			    V_FW_PORT_CAP32_FEC(n & M_FW_PORT_CAP32_FEC)) !=
9050 			    lc->pcaps) {
9051 				rc = ENOTSUP;
9052 				goto done;
9053 			}
9054 			lc->requested_fec = n & (M_FW_PORT_CAP32_FEC |
9055 			    FEC_MODULE);
9056 		}
9057 		if (hw_all_ok(sc)) {
9058 			fixup_link_config(pi);
9059 			if (pi->up_vis > 0) {
9060 				rc = apply_link_config(pi);
9061 				if (rc != 0) {
9062 					lc->requested_fec = old;
9063 					if (rc == FW_EPROTO)
9064 						rc = ENOTSUP;
9065 				}
9066 			}
9067 		}
9068 done:
9069 		PORT_UNLOCK(pi);
9070 		end_synchronized_op(sc, 0);
9071 	}
9072 
9073 	return (rc);
9074 }
9075 
9076 static int
sysctl_module_fec(SYSCTL_HANDLER_ARGS)9077 sysctl_module_fec(SYSCTL_HANDLER_ARGS)
9078 {
9079 	struct port_info *pi = arg1;
9080 	struct adapter *sc = pi->adapter;
9081 	struct link_config *lc = &pi->link_cfg;
9082 	int rc;
9083 	int8_t fec;
9084 	struct sbuf *sb;
9085 
9086 	sb = sbuf_new_for_sysctl(NULL, NULL, 128, req);
9087 	if (sb == NULL)
9088 		return (ENOMEM);
9089 
9090 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4mfec") != 0) {
9091 		rc = EBUSY;
9092 		goto done;
9093 	}
9094 	if (!hw_all_ok(sc)) {
9095 		rc = ENXIO;
9096 		goto done;
9097 	}
9098 	PORT_LOCK(pi);
9099 	if (pi->up_vis == 0) {
9100 		/*
9101 		 * If all the interfaces are administratively down the firmware
9102 		 * does not report transceiver changes.  Refresh port info here.
9103 		 * This is the only reason we have a synchronized op in this
9104 		 * function.  Just PORT_LOCK would have been enough otherwise.
9105 		 */
9106 		t4_update_port_info(pi);
9107 	}
9108 
9109 	fec = lc->fec_hint;
9110 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE ||
9111 	    !fec_supported(lc->pcaps)) {
9112 		PORT_UNLOCK(pi);
9113 		sbuf_printf(sb, "n/a");
9114 	} else {
9115 		if (fec == 0)
9116 			fec = FEC_NONE;
9117 		PORT_UNLOCK(pi);
9118 		sbuf_printf(sb, "%b", fec & M_FW_PORT_CAP32_FEC, t4_fec_bits);
9119 	}
9120 	rc = sbuf_finish(sb);
9121 done:
9122 	sbuf_delete(sb);
9123 	end_synchronized_op(sc, 0);
9124 
9125 	return (rc);
9126 }
9127 
9128 static int
sysctl_autoneg(SYSCTL_HANDLER_ARGS)9129 sysctl_autoneg(SYSCTL_HANDLER_ARGS)
9130 {
9131 	struct port_info *pi = arg1;
9132 	struct adapter *sc = pi->adapter;
9133 	struct link_config *lc = &pi->link_cfg;
9134 	int rc, val;
9135 
9136 	if (lc->pcaps & FW_PORT_CAP32_ANEG)
9137 		val = lc->requested_aneg == AUTONEG_DISABLE ? 0 : 1;
9138 	else
9139 		val = -1;
9140 	rc = sysctl_handle_int(oidp, &val, 0, req);
9141 	if (rc != 0 || req->newptr == NULL)
9142 		return (rc);
9143 	if (val == 0)
9144 		val = AUTONEG_DISABLE;
9145 	else if (val == 1)
9146 		val = AUTONEG_ENABLE;
9147 	else
9148 		val = AUTONEG_AUTO;
9149 
9150 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK,
9151 	    "t4aneg");
9152 	if (rc)
9153 		return (rc);
9154 	PORT_LOCK(pi);
9155 	if (val == AUTONEG_ENABLE && !(lc->pcaps & FW_PORT_CAP32_ANEG)) {
9156 		rc = ENOTSUP;
9157 		goto done;
9158 	}
9159 	lc->requested_aneg = val;
9160 	if (hw_all_ok(sc)) {
9161 		fixup_link_config(pi);
9162 		if (pi->up_vis > 0)
9163 			rc = apply_link_config(pi);
9164 		set_current_media(pi);
9165 	}
9166 done:
9167 	PORT_UNLOCK(pi);
9168 	end_synchronized_op(sc, 0);
9169 	return (rc);
9170 }
9171 
9172 static int
sysctl_force_fec(SYSCTL_HANDLER_ARGS)9173 sysctl_force_fec(SYSCTL_HANDLER_ARGS)
9174 {
9175 	struct port_info *pi = arg1;
9176 	struct adapter *sc = pi->adapter;
9177 	struct link_config *lc = &pi->link_cfg;
9178 	int rc, val;
9179 
9180 	val = lc->force_fec;
9181 	MPASS(val >= -1 && val <= 1);
9182 	rc = sysctl_handle_int(oidp, &val, 0, req);
9183 	if (rc != 0 || req->newptr == NULL)
9184 		return (rc);
9185 	if (!(lc->pcaps & FW_PORT_CAP32_FORCE_FEC))
9186 		return (ENOTSUP);
9187 	if (val < -1 || val > 1)
9188 		return (EINVAL);
9189 
9190 	rc = begin_synchronized_op(sc, &pi->vi[0], SLEEP_OK | INTR_OK, "t4ff");
9191 	if (rc)
9192 		return (rc);
9193 	PORT_LOCK(pi);
9194 	lc->force_fec = val;
9195 	if (hw_all_ok(sc)) {
9196 		fixup_link_config(pi);
9197 		if (pi->up_vis > 0)
9198 			rc = apply_link_config(pi);
9199 	}
9200 	PORT_UNLOCK(pi);
9201 	end_synchronized_op(sc, 0);
9202 	return (rc);
9203 }
9204 
9205 static int
sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)9206 sysctl_handle_t4_reg64(SYSCTL_HANDLER_ARGS)
9207 {
9208 	struct adapter *sc = arg1;
9209 	int rc, reg = arg2;
9210 	uint64_t val;
9211 
9212 	mtx_lock(&sc->reg_lock);
9213 	if (hw_off_limits(sc))
9214 		rc = ENXIO;
9215 	else {
9216 		rc = 0;
9217 		val = t4_read_reg64(sc, reg);
9218 	}
9219 	mtx_unlock(&sc->reg_lock);
9220 	if (rc == 0)
9221 		rc = sysctl_handle_64(oidp, &val, 0, req);
9222 	return (rc);
9223 }
9224 
9225 static int
sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)9226 sysctl_handle_t4_portstat64(SYSCTL_HANDLER_ARGS)
9227 {
9228 	struct port_info *pi = arg1;
9229 	struct adapter *sc = pi->adapter;
9230 	int rc, i, reg = arg2;
9231 	uint64_t val;
9232 
9233 	mtx_lock(&sc->reg_lock);
9234 	if (hw_off_limits(sc))
9235 		rc = ENXIO;
9236 	else {
9237 		val = 0;
9238 		for (i = 0; i < sc->params.tp.lb_nchan; i++) {
9239 			val += t4_read_reg64(sc,
9240 			    t4_port_reg(sc, pi->tx_chan + i, reg));
9241 		}
9242 		rc = 0;
9243 	}
9244 	mtx_unlock(&sc->reg_lock);
9245 	if (rc == 0)
9246 		rc = sysctl_handle_64(oidp, &val, 0, req);
9247 	return (rc);
9248 }
9249 
9250 static int
sysctl_temperature(SYSCTL_HANDLER_ARGS)9251 sysctl_temperature(SYSCTL_HANDLER_ARGS)
9252 {
9253 	struct adapter *sc = arg1;
9254 	int rc, t;
9255 	uint32_t param, val;
9256 
9257 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4temp");
9258 	if (rc)
9259 		return (rc);
9260 	if (!hw_all_ok(sc))
9261 		rc = ENXIO;
9262 	else {
9263 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9264 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9265 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_TMP);
9266 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9267 	}
9268 	end_synchronized_op(sc, 0);
9269 	if (rc)
9270 		return (rc);
9271 
9272 	/* unknown is returned as 0 but we display -1 in that case */
9273 	t = val == 0 ? -1 : val;
9274 
9275 	rc = sysctl_handle_int(oidp, &t, 0, req);
9276 	return (rc);
9277 }
9278 
9279 static int
sysctl_vdd(SYSCTL_HANDLER_ARGS)9280 sysctl_vdd(SYSCTL_HANDLER_ARGS)
9281 {
9282 	struct adapter *sc = arg1;
9283 	int rc;
9284 	uint32_t param, val;
9285 
9286 	if (sc->params.core_vdd == 0) {
9287 		rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
9288 		    "t4vdd");
9289 		if (rc)
9290 			return (rc);
9291 		if (!hw_all_ok(sc))
9292 			rc = ENXIO;
9293 		else {
9294 			param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9295 			    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9296 			    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_VDD);
9297 			rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1,
9298 			    &param, &val);
9299 		}
9300 		end_synchronized_op(sc, 0);
9301 		if (rc)
9302 			return (rc);
9303 		sc->params.core_vdd = val;
9304 	}
9305 
9306 	return (sysctl_handle_int(oidp, &sc->params.core_vdd, 0, req));
9307 }
9308 
9309 static int
sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)9310 sysctl_reset_sensor(SYSCTL_HANDLER_ARGS)
9311 {
9312 	struct adapter *sc = arg1;
9313 	int rc, v;
9314 	uint32_t param, val;
9315 
9316 	v = sc->sensor_resets;
9317 	rc = sysctl_handle_int(oidp, &v, 0, req);
9318 	if (rc != 0 || req->newptr == NULL || v <= 0)
9319 		return (rc);
9320 
9321 	if (sc->params.fw_vers < FW_VERSION32(1, 24, 7, 0) ||
9322 	    chip_id(sc) < CHELSIO_T5)
9323 		return (ENOTSUP);
9324 
9325 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4srst");
9326 	if (rc)
9327 		return (rc);
9328 	if (!hw_all_ok(sc))
9329 		rc = ENXIO;
9330 	else {
9331 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9332 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_DIAG) |
9333 		    V_FW_PARAMS_PARAM_Y(FW_PARAM_DEV_DIAG_RESET_TMP_SENSOR));
9334 		val = 1;
9335 		rc = -t4_set_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9336 	}
9337 	end_synchronized_op(sc, 0);
9338 	if (rc == 0)
9339 		sc->sensor_resets++;
9340 	return (rc);
9341 }
9342 
9343 static int
sysctl_loadavg(SYSCTL_HANDLER_ARGS)9344 sysctl_loadavg(SYSCTL_HANDLER_ARGS)
9345 {
9346 	struct adapter *sc = arg1;
9347 	struct sbuf *sb;
9348 	int rc;
9349 	uint32_t param, val;
9350 	uint8_t coreid = (uint8_t)arg2;
9351 
9352 	KASSERT(coreid < sc->params.ncores,
9353 	    ("%s: bad coreid %u\n", __func__, coreid));
9354 
9355 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4lavg");
9356 	if (rc)
9357 		return (rc);
9358 	if (!hw_all_ok(sc))
9359 		rc = ENXIO;
9360 	else {
9361 		param = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
9362 		    V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_LOAD) |
9363 		    V_FW_PARAMS_PARAM_Y(coreid);
9364 		rc = -t4_query_params(sc, sc->mbox, sc->pf, 0, 1, &param, &val);
9365 	}
9366 	end_synchronized_op(sc, 0);
9367 	if (rc)
9368 		return (rc);
9369 
9370 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9371 	if (sb == NULL)
9372 		return (ENOMEM);
9373 
9374 	if (val == 0xffffffff) {
9375 		/* Only debug and custom firmwares report load averages. */
9376 		sbuf_printf(sb, "not available");
9377 	} else {
9378 		sbuf_printf(sb, "%d %d %d", val & 0xff, (val >> 8) & 0xff,
9379 		    (val >> 16) & 0xff);
9380 	}
9381 	rc = sbuf_finish(sb);
9382 	sbuf_delete(sb);
9383 
9384 	return (rc);
9385 }
9386 
9387 static int
sysctl_cctrl(SYSCTL_HANDLER_ARGS)9388 sysctl_cctrl(SYSCTL_HANDLER_ARGS)
9389 {
9390 	struct adapter *sc = arg1;
9391 	struct sbuf *sb;
9392 	int rc, i;
9393 	uint16_t incr[NMTUS][NCCTRL_WIN];
9394 	static const char *dec_fac[] = {
9395 		"0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
9396 		"0.9375"
9397 	};
9398 
9399 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9400 	if (sb == NULL)
9401 		return (ENOMEM);
9402 
9403 	rc = 0;
9404 	mtx_lock(&sc->reg_lock);
9405 	if (hw_off_limits(sc))
9406 		rc = ENXIO;
9407 	else
9408 		t4_read_cong_tbl(sc, incr);
9409 	mtx_unlock(&sc->reg_lock);
9410 	if (rc)
9411 		goto done;
9412 
9413 	for (i = 0; i < NCCTRL_WIN; ++i) {
9414 		sbuf_printf(sb, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
9415 		    incr[0][i], incr[1][i], incr[2][i], incr[3][i], incr[4][i],
9416 		    incr[5][i], incr[6][i], incr[7][i]);
9417 		sbuf_printf(sb, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
9418 		    incr[8][i], incr[9][i], incr[10][i], incr[11][i],
9419 		    incr[12][i], incr[13][i], incr[14][i], incr[15][i],
9420 		    sc->params.a_wnd[i], dec_fac[sc->params.b_wnd[i]]);
9421 	}
9422 
9423 	rc = sbuf_finish(sb);
9424 done:
9425 	sbuf_delete(sb);
9426 	return (rc);
9427 }
9428 
9429 static int
sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)9430 sysctl_cim_ibq(SYSCTL_HANDLER_ARGS)
9431 {
9432 	struct adapter *sc = arg1;
9433 	struct sbuf *sb;
9434 	int rc, i, n, qid, coreid;
9435 	uint32_t *buf, *p;
9436 
9437 	qid = arg2 & 0xffff;
9438 	coreid = arg2 >> 16;
9439 
9440 	KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_ibq,
9441 	    ("%s: bad ibq qid %d\n", __func__, qid));
9442 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9443 	    ("%s: bad coreid %d\n", __func__, coreid));
9444 
9445 	n = 4 * CIM_IBQ_SIZE;
9446 	buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9447 	mtx_lock(&sc->reg_lock);
9448 	if (hw_off_limits(sc))
9449 		rc = -ENXIO;
9450 	else
9451 		rc = t4_read_cim_ibq_core(sc, coreid, qid, buf, n);
9452 	mtx_unlock(&sc->reg_lock);
9453 	if (rc < 0) {
9454 		rc = -rc;
9455 		goto done;
9456 	}
9457 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9458 
9459 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9460 	if (sb == NULL) {
9461 		rc = ENOMEM;
9462 		goto done;
9463 	}
9464 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9465 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9466 		    p[2], p[3]);
9467 	rc = sbuf_finish(sb);
9468 	sbuf_delete(sb);
9469 done:
9470 	free(buf, M_CXGBE);
9471 	return (rc);
9472 }
9473 
9474 static int
sysctl_cim_obq(SYSCTL_HANDLER_ARGS)9475 sysctl_cim_obq(SYSCTL_HANDLER_ARGS)
9476 {
9477 	struct adapter *sc = arg1;
9478 	struct sbuf *sb;
9479 	int rc, i, n, qid, coreid;
9480 	uint32_t *buf, *p;
9481 
9482 	qid = arg2 & 0xffff;
9483 	coreid = arg2 >> 16;
9484 
9485 	KASSERT(qid >= 0 && qid < sc->chip_params->cim_num_obq,
9486 	    ("%s: bad obq qid %d\n", __func__, qid));
9487 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
9488 	    ("%s: bad coreid %d\n", __func__, coreid));
9489 
9490 	n = 6 * CIM_OBQ_SIZE * 4;
9491 	buf = malloc(n * sizeof(uint32_t), M_CXGBE, M_ZERO | M_WAITOK);
9492 	mtx_lock(&sc->reg_lock);
9493 	if (hw_off_limits(sc))
9494 		rc = -ENXIO;
9495 	else
9496 		rc = t4_read_cim_obq_core(sc, coreid, qid, buf, n);
9497 	mtx_unlock(&sc->reg_lock);
9498 	if (rc < 0) {
9499 		rc = -rc;
9500 		goto done;
9501 	}
9502 	n = rc * sizeof(uint32_t);	/* rc has # of words actually read */
9503 
9504 	rc = sysctl_wire_old_buffer(req, 0);
9505 	if (rc != 0)
9506 		goto done;
9507 
9508 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9509 	if (sb == NULL) {
9510 		rc = ENOMEM;
9511 		goto done;
9512 	}
9513 	for (i = 0, p = buf; i < n; i += 16, p += 4)
9514 		sbuf_printf(sb, "\n%#06x: %08x %08x %08x %08x", i, p[0], p[1],
9515 		    p[2], p[3]);
9516 	rc = sbuf_finish(sb);
9517 	sbuf_delete(sb);
9518 done:
9519 	free(buf, M_CXGBE);
9520 	return (rc);
9521 }
9522 
9523 static void
sbuf_cim_la4(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9524 sbuf_cim_la4(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9525 {
9526 	uint32_t *p;
9527 
9528 	sbuf_printf(sb, "Status   Data      PC%s",
9529 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9530 	    "     LS0Stat  LS0Addr             LS0Data");
9531 
9532 	for (p = buf; p <= &buf[sc->params.cim_la_size - 8]; p += 8) {
9533 		if (cfg & F_UPDBGLACAPTPCONLY) {
9534 			sbuf_printf(sb, "\n  %02x   %08x %08x", p[5] & 0xff,
9535 			    p[6], p[7]);
9536 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x",
9537 			    (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
9538 			    p[4] & 0xff, p[5] >> 8);
9539 			sbuf_printf(sb, "\n  %02x   %x%07x %x%07x",
9540 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9541 			    p[1] & 0xf, p[2] >> 4);
9542 		} else {
9543 			sbuf_printf(sb,
9544 			    "\n  %02x   %x%07x %x%07x %08x %08x "
9545 			    "%08x%08x%08x%08x",
9546 			    (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
9547 			    p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
9548 			    p[6], p[7]);
9549 		}
9550 	}
9551 }
9552 
9553 static void
sbuf_cim_la6(struct adapter * sc,struct sbuf * sb,uint32_t * buf,uint32_t cfg)9554 sbuf_cim_la6(struct adapter *sc, struct sbuf *sb, uint32_t *buf, uint32_t cfg)
9555 {
9556 	uint32_t *p;
9557 
9558 	sbuf_printf(sb, "Status   Inst    Data      PC%s",
9559 	    cfg & F_UPDBGLACAPTPCONLY ? "" :
9560 	    "     LS0Stat  LS0Addr  LS0Data  LS1Stat  LS1Addr  LS1Data");
9561 
9562 	for (p = buf; p <= &buf[sc->params.cim_la_size - 10]; p += 10) {
9563 		if (cfg & F_UPDBGLACAPTPCONLY) {
9564 			sbuf_printf(sb, "\n  %02x   %08x %08x %08x",
9565 			    p[3] & 0xff, p[2], p[1], p[0]);
9566 			sbuf_printf(sb, "\n  %02x   %02x%06x %02x%06x %02x%06x",
9567 			    (p[6] >> 8) & 0xff, p[6] & 0xff, p[5] >> 8,
9568 			    p[5] & 0xff, p[4] >> 8, p[4] & 0xff, p[3] >> 8);
9569 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x",
9570 			    (p[9] >> 16) & 0xff, p[9] & 0xffff, p[8] >> 16,
9571 			    p[8] & 0xffff, p[7] >> 16, p[7] & 0xffff,
9572 			    p[6] >> 16);
9573 		} else {
9574 			sbuf_printf(sb, "\n  %02x   %04x%04x %04x%04x %04x%04x "
9575 			    "%08x %08x %08x %08x %08x %08x",
9576 			    (p[9] >> 16) & 0xff,
9577 			    p[9] & 0xffff, p[8] >> 16,
9578 			    p[8] & 0xffff, p[7] >> 16,
9579 			    p[7] & 0xffff, p[6] >> 16,
9580 			    p[2], p[1], p[0], p[5], p[4], p[3]);
9581 		}
9582 	}
9583 }
9584 
9585 static int
sbuf_cim_la(struct adapter * sc,int coreid,struct sbuf * sb,int flags)9586 sbuf_cim_la(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
9587 {
9588 	uint32_t cfg, *buf;
9589 	int rc;
9590 
9591 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
9592 	buf = malloc(sc->params.cim_la_size * sizeof(uint32_t), M_CXGBE,
9593 	    M_ZERO | flags);
9594 	if (buf == NULL)
9595 		return (ENOMEM);
9596 
9597 	mtx_lock(&sc->reg_lock);
9598 	if (hw_off_limits(sc))
9599 		rc = ENXIO;
9600 	else {
9601 		rc = -t4_cim_read_core(sc, 1, coreid, A_UP_UP_DBG_LA_CFG, 1,
9602 		    &cfg);
9603 		if (rc == 0)
9604 			rc = -t4_cim_read_la_core(sc, coreid, buf, NULL);
9605 	}
9606 	mtx_unlock(&sc->reg_lock);
9607 	if (rc == 0) {
9608 		if (chip_id(sc) < CHELSIO_T6)
9609 			sbuf_cim_la4(sc, sb, buf, cfg);
9610 		else
9611 			sbuf_cim_la6(sc, sb, buf, cfg);
9612 	}
9613 	free(buf, M_CXGBE);
9614 	return (rc);
9615 }
9616 
9617 static int
sysctl_cim_la(SYSCTL_HANDLER_ARGS)9618 sysctl_cim_la(SYSCTL_HANDLER_ARGS)
9619 {
9620 	struct adapter *sc = arg1;
9621 	int coreid = arg2;
9622 	struct sbuf *sb;
9623 	int rc;
9624 
9625 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9626 	if (sb == NULL)
9627 		return (ENOMEM);
9628 
9629 	rc = sbuf_cim_la(sc, coreid, sb, M_WAITOK);
9630 	if (rc == 0)
9631 		rc = sbuf_finish(sb);
9632 	sbuf_delete(sb);
9633 	return (rc);
9634 }
9635 
9636 static void
dump_cim_regs(struct adapter * sc)9637 dump_cim_regs(struct adapter *sc)
9638 {
9639 	log(LOG_DEBUG, "%s: CIM debug regs1 %08x %08x %08x %08x %08x\n",
9640 	    device_get_nameunit(sc->dev),
9641 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9642 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9643 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA2),
9644 	    t4_read_reg(sc, A_EDC_H_BIST_DATA_PATTERN),
9645 	    t4_read_reg(sc, A_EDC_H_BIST_STATUS_RDATA));
9646 	log(LOG_DEBUG, "%s: CIM debug regs2 %08x %08x %08x %08x %08x\n",
9647 	    device_get_nameunit(sc->dev),
9648 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0),
9649 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1),
9650 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA0 + 0x800),
9651 	    t4_read_reg(sc, A_EDC_H_BIST_USER_WDATA1 + 0x800),
9652 	    t4_read_reg(sc, A_EDC_H_BIST_CMD_LEN));
9653 }
9654 
9655 static void
dump_cimla(struct adapter * sc)9656 dump_cimla(struct adapter *sc)
9657 {
9658 	struct sbuf sb;
9659 	int rc;
9660 
9661 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
9662 		log(LOG_DEBUG, "%s: failed to generate CIM LA dump.\n",
9663 		    device_get_nameunit(sc->dev));
9664 		return;
9665 	}
9666 	rc = sbuf_cim_la(sc, 0, &sb, M_WAITOK);
9667 	if (rc == 0) {
9668 		rc = sbuf_finish(&sb);
9669 		if (rc == 0) {
9670 			log(LOG_DEBUG, "%s: CIM LA dump follows.\n%s\n",
9671 			    device_get_nameunit(sc->dev), sbuf_data(&sb));
9672 		}
9673 	}
9674 	sbuf_delete(&sb);
9675 }
9676 
9677 void
t4_os_cim_err(struct adapter * sc)9678 t4_os_cim_err(struct adapter *sc)
9679 {
9680 	atomic_set_int(&sc->error_flags, ADAP_CIM_ERR);
9681 }
9682 
9683 static int
sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)9684 sysctl_cim_ma_la(SYSCTL_HANDLER_ARGS)
9685 {
9686 	struct adapter *sc = arg1;
9687 	u_int i;
9688 	struct sbuf *sb;
9689 	uint32_t *buf, *p;
9690 	int rc;
9691 
9692 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9693 	if (sb == NULL)
9694 		return (ENOMEM);
9695 
9696 	buf = malloc(2 * CIM_MALA_SIZE * 5 * sizeof(uint32_t), M_CXGBE,
9697 	    M_ZERO | M_WAITOK);
9698 
9699 	rc = 0;
9700 	mtx_lock(&sc->reg_lock);
9701 	if (hw_off_limits(sc))
9702 		rc = ENXIO;
9703 	else
9704 		t4_cim_read_ma_la(sc, buf, buf + 5 * CIM_MALA_SIZE);
9705 	mtx_unlock(&sc->reg_lock);
9706 	if (rc)
9707 		goto done;
9708 
9709 	p = buf;
9710 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9711 		sbuf_printf(sb, "\n%02x%08x%08x%08x%08x", p[4], p[3], p[2],
9712 		    p[1], p[0]);
9713 	}
9714 
9715 	sbuf_printf(sb, "\n\nCnt ID Tag UE       Data       RDY VLD");
9716 	for (i = 0; i < CIM_MALA_SIZE; i++, p += 5) {
9717 		sbuf_printf(sb, "\n%3u %2u  %x   %u %08x%08x  %u   %u",
9718 		    (p[2] >> 10) & 0xff, (p[2] >> 7) & 7,
9719 		    (p[2] >> 3) & 0xf, (p[2] >> 2) & 1,
9720 		    (p[1] >> 2) | ((p[2] & 3) << 30),
9721 		    (p[0] >> 2) | ((p[1] & 3) << 30), (p[0] >> 1) & 1,
9722 		    p[0] & 1);
9723 	}
9724 	rc = sbuf_finish(sb);
9725 done:
9726 	sbuf_delete(sb);
9727 	free(buf, M_CXGBE);
9728 	return (rc);
9729 }
9730 
9731 static int
sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)9732 sysctl_cim_pif_la(SYSCTL_HANDLER_ARGS)
9733 {
9734 	struct adapter *sc = arg1;
9735 	u_int i;
9736 	struct sbuf *sb;
9737 	uint32_t *buf, *p;
9738 	int rc;
9739 
9740 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
9741 	if (sb == NULL)
9742 		return (ENOMEM);
9743 
9744 	buf = malloc(2 * CIM_PIFLA_SIZE * 6 * sizeof(uint32_t), M_CXGBE,
9745 	    M_ZERO | M_WAITOK);
9746 
9747 	rc = 0;
9748 	mtx_lock(&sc->reg_lock);
9749 	if (hw_off_limits(sc))
9750 		rc = ENXIO;
9751 	else
9752 		t4_cim_read_pif_la(sc, buf, buf + 6 * CIM_PIFLA_SIZE, NULL, NULL);
9753 	mtx_unlock(&sc->reg_lock);
9754 	if (rc)
9755 		goto done;
9756 
9757 	p = buf;
9758 	sbuf_printf(sb, "Cntl ID DataBE   Addr                 Data");
9759 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9760 		sbuf_printf(sb, "\n %02x  %02x  %04x  %08x %08x%08x%08x%08x",
9761 		    (p[5] >> 22) & 0xff, (p[5] >> 16) & 0x3f, p[5] & 0xffff,
9762 		    p[4], p[3], p[2], p[1], p[0]);
9763 	}
9764 
9765 	sbuf_printf(sb, "\n\nCntl ID               Data");
9766 	for (i = 0; i < CIM_PIFLA_SIZE; i++, p += 6) {
9767 		sbuf_printf(sb, "\n %02x  %02x %08x%08x%08x%08x",
9768 		    (p[4] >> 6) & 0xff, p[4] & 0x3f, p[3], p[2], p[1], p[0]);
9769 	}
9770 
9771 	rc = sbuf_finish(sb);
9772 done:
9773 	sbuf_delete(sb);
9774 	free(buf, M_CXGBE);
9775 	return (rc);
9776 }
9777 
9778 static int
sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)9779 sysctl_cim_qcfg(SYSCTL_HANDLER_ARGS)
9780 {
9781 	struct adapter *sc = arg1;
9782 	struct sbuf *sb;
9783 	int rc, i;
9784 	uint16_t base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9785 	uint16_t size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
9786 	uint16_t thres[CIM_NUM_IBQ];
9787 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T5], *wr = obq_wr;
9788 	uint32_t stat[4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5)], *p = stat;
9789 	u_int cim_num_obq, ibq_rdaddr, obq_rdaddr, nq;
9790 	static const char *qname[CIM_NUM_IBQ + CIM_NUM_OBQ_T5] = {
9791 		"TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",	/* ibq's */
9792 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",	/* obq's */
9793 		"SGE0-RX", "SGE1-RX"	/* additional obq's (T5 onwards) */
9794 	};
9795 
9796 	MPASS(chip_id(sc) < CHELSIO_T7);
9797 
9798 	cim_num_obq = sc->chip_params->cim_num_obq;
9799 	if (is_t4(sc)) {
9800 		ibq_rdaddr = A_UP_IBQ_0_RDADDR;
9801 		obq_rdaddr = A_UP_OBQ_0_REALADDR;
9802 	} else {
9803 		ibq_rdaddr = A_UP_IBQ_0_SHADOW_RDADDR;
9804 		obq_rdaddr = A_UP_OBQ_0_SHADOW_REALADDR;
9805 	}
9806 	nq = CIM_NUM_IBQ + cim_num_obq;
9807 
9808 	mtx_lock(&sc->reg_lock);
9809 	if (hw_off_limits(sc))
9810 		rc = ENXIO;
9811 	else {
9812 		rc = -t4_cim_read(sc, ibq_rdaddr, 4 * nq, stat);
9813 		if (rc == 0) {
9814 			rc = -t4_cim_read(sc, obq_rdaddr, 2 * cim_num_obq,
9815 			    obq_wr);
9816 			if (rc == 0)
9817 				t4_read_cimq_cfg(sc, base, size, thres);
9818 		}
9819 	}
9820 	mtx_unlock(&sc->reg_lock);
9821 	if (rc)
9822 		return (rc);
9823 
9824 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9825 	if (sb == NULL)
9826 		return (ENOMEM);
9827 
9828 	sbuf_printf(sb,
9829 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9830 
9831 	for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
9832 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9833 		    qname[i], base[i], size[i], thres[i], G_IBQRDADDR(p[0]),
9834 		    G_IBQWRADDR(p[1]), G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9835 		    G_QUEREMFLITS(p[2]) * 16);
9836 	for ( ; i < nq; i++, p += 4, wr += 2)
9837 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u", qname[i],
9838 		    base[i], size[i], G_QUERDADDR(p[0]) & 0x3fff,
9839 		    wr[0] - base[i], G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9840 		    G_QUEREMFLITS(p[2]) * 16);
9841 
9842 	rc = sbuf_finish(sb);
9843 	sbuf_delete(sb);
9844 
9845 	return (rc);
9846 }
9847 
9848 static int
sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)9849 sysctl_cim_qcfg_t7(SYSCTL_HANDLER_ARGS)
9850 {
9851 	struct adapter *sc = arg1;
9852 	u_int coreid = arg2;
9853 	struct sbuf *sb;
9854 	int rc, i;
9855 	u_int addr;
9856 	uint16_t base[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9857 	uint16_t size[CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7];
9858 	uint16_t thres[CIM_NUM_IBQ_T7];
9859 	uint32_t obq_wr[2 * CIM_NUM_OBQ_T7], *wr = obq_wr;
9860 	uint32_t stat[4 * (CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7)], *p = stat;
9861 	static const char * const qname_ibq_t7[] = {
9862 		"TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "SGE1", "NC-SI",
9863 		"RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5", "IPC6", "IPC7",
9864 	};
9865 	static const char * const qname_obq_t7[] = {
9866 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI", "SGE0-RX",
9867 		"RSVD", "RSVD", "IPC1", "IPC2", "IPC3", "IPC4", "IPC5",
9868 		"IPC6", "IPC7"
9869 	};
9870 	static const char * const qname_ibq_sec_t7[] = {
9871 		"TP0", "TP1", "TP2", "TP3", "ULP", "SGE0", "RSVD", "RSVD",
9872 		"RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD",	"RSVD", "RSVD",
9873 	};
9874 	static const char * const qname_obq_sec_t7[] = {
9875 		"ULP0", "ULP1", "ULP2", "ULP3", "SGE", "RSVD", "SGE0-RX",
9876 		"RSVD", "RSVD", "IPC0", "RSVD", "RSVD", "RSVD", "RSVD",
9877 		"RSVD", "RSVD",
9878 	};
9879 
9880 	MPASS(chip_id(sc) >= CHELSIO_T7);
9881 
9882 	mtx_lock(&sc->reg_lock);
9883 	if (hw_off_limits(sc))
9884 		rc = ENXIO;
9885 	else {
9886 		rc = -t4_cim_read_core(sc, 1, coreid,
9887 		    A_T7_UP_IBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_IBQ_T7, stat);
9888 		if (rc != 0)
9889 			goto unlock;
9890 
9891 		rc = -t4_cim_read_core(sc, 1, coreid,
9892 		    A_T7_UP_OBQ_0_SHADOW_RDADDR, 4 * CIM_NUM_OBQ_T7,
9893 		    &stat[4 * CIM_NUM_IBQ_T7]);
9894 		if (rc != 0)
9895 			goto unlock;
9896 
9897 		addr = A_T7_UP_OBQ_0_SHADOW_REALADDR;
9898 		for (i = 0; i < CIM_NUM_OBQ_T7 * 2; i++, addr += 8) {
9899 			rc = -t4_cim_read_core(sc, 1, coreid, addr, 1,
9900 			    &obq_wr[i]);
9901 			if (rc != 0)
9902 				goto unlock;
9903 		}
9904 		t4_read_cimq_cfg_core(sc, coreid, base, size, thres);
9905 	}
9906 unlock:
9907 	mtx_unlock(&sc->reg_lock);
9908 	if (rc)
9909 		return (rc);
9910 
9911 	sb = sbuf_new_for_sysctl(NULL, NULL, PAGE_SIZE, req);
9912 	if (sb == NULL)
9913 		return (ENOMEM);
9914 
9915 	sbuf_printf(sb,
9916 	    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail");
9917 
9918 	for (i = 0; i < CIM_NUM_IBQ_T7; i++, p += 4) {
9919 		if (!size[i])
9920 			continue;
9921 
9922 		sbuf_printf(sb, "\n%7s %5x %5u %5u %6x  %4x %4u %4u %5u",
9923 		    coreid == 0 ? qname_ibq_t7[i] : qname_ibq_sec_t7[i],
9924 		    base[i], size[i], thres[i], G_IBQRDADDR(p[0]) & 0xfff,
9925 		    G_IBQWRADDR(p[1]) & 0xfff, G_QUESOPCNT(p[3]),
9926 		    G_QUEEOPCNT(p[3]), G_T7_QUEREMFLITS(p[2]) * 16);
9927 	}
9928 
9929 	for ( ; i < CIM_NUM_IBQ_T7 + CIM_NUM_OBQ_T7; i++, p += 4, wr += 2) {
9930 		if (!size[i])
9931 			continue;
9932 
9933 		sbuf_printf(sb, "\n%7s %5x %5u %12x  %4x %4u %4u %5u",
9934 		    coreid == 0 ? qname_obq_t7[i - CIM_NUM_IBQ_T7] :
9935 		    qname_obq_sec_t7[i - CIM_NUM_IBQ_T7],
9936 		    base[i], size[i], G_QUERDADDR(p[0]) & 0xfff,
9937 		    wr[0] << 1, G_QUESOPCNT(p[3]), G_QUEEOPCNT(p[3]),
9938 		    G_T7_QUEREMFLITS(p[2]) * 16);
9939 	}
9940 
9941 	rc = sbuf_finish(sb);
9942 	sbuf_delete(sb);
9943 	return (rc);
9944 }
9945 
9946 static int
sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)9947 sysctl_cpl_stats(SYSCTL_HANDLER_ARGS)
9948 {
9949 	struct adapter *sc = arg1;
9950 	struct sbuf *sb;
9951 	int rc;
9952 	struct tp_cpl_stats stats;
9953 
9954 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9955 	if (sb == NULL)
9956 		return (ENOMEM);
9957 
9958 	rc = 0;
9959 	mtx_lock(&sc->reg_lock);
9960 	if (hw_off_limits(sc))
9961 		rc = ENXIO;
9962 	else
9963 		t4_tp_get_cpl_stats(sc, &stats, 0);
9964 	mtx_unlock(&sc->reg_lock);
9965 	if (rc)
9966 		goto done;
9967 
9968 	if (sc->chip_params->nchan > 2) {
9969 		sbuf_printf(sb, "                 channel 0  channel 1"
9970 		    "  channel 2  channel 3");
9971 		sbuf_printf(sb, "\nCPL requests:   %10u %10u %10u %10u",
9972 		    stats.req[0], stats.req[1], stats.req[2], stats.req[3]);
9973 		sbuf_printf(sb, "\nCPL responses:  %10u %10u %10u %10u",
9974 		    stats.rsp[0], stats.rsp[1], stats.rsp[2], stats.rsp[3]);
9975 	} else {
9976 		sbuf_printf(sb, "                 channel 0  channel 1");
9977 		sbuf_printf(sb, "\nCPL requests:   %10u %10u",
9978 		    stats.req[0], stats.req[1]);
9979 		sbuf_printf(sb, "\nCPL responses:  %10u %10u",
9980 		    stats.rsp[0], stats.rsp[1]);
9981 	}
9982 
9983 	rc = sbuf_finish(sb);
9984 done:
9985 	sbuf_delete(sb);
9986 	return (rc);
9987 }
9988 
9989 static int
sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)9990 sysctl_ddp_stats(SYSCTL_HANDLER_ARGS)
9991 {
9992 	struct adapter *sc = arg1;
9993 	struct sbuf *sb;
9994 	int rc;
9995 	struct tp_usm_stats stats;
9996 
9997 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
9998 	if (sb == NULL)
9999 		return (ENOMEM);
10000 
10001 	rc = 0;
10002 	mtx_lock(&sc->reg_lock);
10003 	if (hw_off_limits(sc))
10004 		rc = ENXIO;
10005 	else
10006 		t4_get_usm_stats(sc, &stats, 1);
10007 	mtx_unlock(&sc->reg_lock);
10008 	if (rc == 0) {
10009 		sbuf_printf(sb, "Frames: %u\n", stats.frames);
10010 		sbuf_printf(sb, "Octets: %ju\n", stats.octets);
10011 		sbuf_printf(sb, "Drops:  %u", stats.drops);
10012 		rc = sbuf_finish(sb);
10013 	}
10014 	sbuf_delete(sb);
10015 
10016 	return (rc);
10017 }
10018 
10019 static int
sysctl_tid_stats(SYSCTL_HANDLER_ARGS)10020 sysctl_tid_stats(SYSCTL_HANDLER_ARGS)
10021 {
10022 	struct adapter *sc = arg1;
10023 	struct sbuf *sb;
10024 	int rc;
10025 	struct tp_tid_stats stats;
10026 
10027 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10028 	if (sb == NULL)
10029 		return (ENOMEM);
10030 
10031 	rc = 0;
10032 	mtx_lock(&sc->reg_lock);
10033 	if (hw_off_limits(sc))
10034 		rc = ENXIO;
10035 	else
10036 		t4_tp_get_tid_stats(sc, &stats, 1);
10037 	mtx_unlock(&sc->reg_lock);
10038 	if (rc == 0) {
10039 		sbuf_printf(sb, "Delete:     %u\n", stats.del);
10040 		sbuf_printf(sb, "Invalidate: %u\n", stats.inv);
10041 		sbuf_printf(sb, "Active:     %u\n", stats.act);
10042 		sbuf_printf(sb, "Passive:    %u", stats.pas);
10043 		rc = sbuf_finish(sb);
10044 	}
10045 	sbuf_delete(sb);
10046 
10047 	return (rc);
10048 }
10049 
10050 static const char * const devlog_level_strings[] = {
10051 	[FW_DEVLOG_LEVEL_EMERG]		= "EMERG",
10052 	[FW_DEVLOG_LEVEL_CRIT]		= "CRIT",
10053 	[FW_DEVLOG_LEVEL_ERR]		= "ERR",
10054 	[FW_DEVLOG_LEVEL_NOTICE]	= "NOTICE",
10055 	[FW_DEVLOG_LEVEL_INFO]		= "INFO",
10056 	[FW_DEVLOG_LEVEL_DEBUG]		= "DEBUG"
10057 };
10058 
10059 static const char * const devlog_facility_strings[] = {
10060 	[FW_DEVLOG_FACILITY_CORE]	= "CORE",
10061 	[FW_DEVLOG_FACILITY_CF]		= "CF",
10062 	[FW_DEVLOG_FACILITY_SCHED]	= "SCHED",
10063 	[FW_DEVLOG_FACILITY_TIMER]	= "TIMER",
10064 	[FW_DEVLOG_FACILITY_RES]	= "RES",
10065 	[FW_DEVLOG_FACILITY_HW]		= "HW",
10066 	[FW_DEVLOG_FACILITY_FLR]	= "FLR",
10067 	[FW_DEVLOG_FACILITY_DMAQ]	= "DMAQ",
10068 	[FW_DEVLOG_FACILITY_PHY]	= "PHY",
10069 	[FW_DEVLOG_FACILITY_MAC]	= "MAC",
10070 	[FW_DEVLOG_FACILITY_PORT]	= "PORT",
10071 	[FW_DEVLOG_FACILITY_VI]		= "VI",
10072 	[FW_DEVLOG_FACILITY_FILTER]	= "FILTER",
10073 	[FW_DEVLOG_FACILITY_ACL]	= "ACL",
10074 	[FW_DEVLOG_FACILITY_TM]		= "TM",
10075 	[FW_DEVLOG_FACILITY_QFC]	= "QFC",
10076 	[FW_DEVLOG_FACILITY_DCB]	= "DCB",
10077 	[FW_DEVLOG_FACILITY_ETH]	= "ETH",
10078 	[FW_DEVLOG_FACILITY_OFLD]	= "OFLD",
10079 	[FW_DEVLOG_FACILITY_RI]		= "RI",
10080 	[FW_DEVLOG_FACILITY_ISCSI]	= "ISCSI",
10081 	[FW_DEVLOG_FACILITY_FCOE]	= "FCOE",
10082 	[FW_DEVLOG_FACILITY_FOISCSI]	= "FOISCSI",
10083 	[FW_DEVLOG_FACILITY_FOFCOE]	= "FOFCOE",
10084 	[FW_DEVLOG_FACILITY_CHNET]	= "CHNET",
10085 };
10086 
10087 static int
sbuf_devlog(struct adapter * sc,int coreid,struct sbuf * sb,int flags)10088 sbuf_devlog(struct adapter *sc, int coreid, struct sbuf *sb, int flags)
10089 {
10090 	int i, j, rc, nentries, first = 0;
10091 	struct devlog_params *dparams = &sc->params.devlog;
10092 	struct fw_devlog_e *buf, *e;
10093 	uint32_t addr, size;
10094 	uint64_t ftstamp = UINT64_MAX;
10095 
10096 	KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10097 	    ("%s: bad coreid %d\n", __func__, coreid));
10098 
10099 	if (dparams->addr == 0)
10100 		return (ENXIO);
10101 
10102 	size = dparams->size / sc->params.ncores;
10103 	addr = dparams->addr + coreid * size;
10104 
10105 	MPASS(flags == M_WAITOK || flags == M_NOWAIT);
10106 	buf = malloc(size, M_CXGBE, M_ZERO | flags);
10107 	if (buf == NULL)
10108 		return (ENOMEM);
10109 
10110 	mtx_lock(&sc->reg_lock);
10111 	if (hw_off_limits(sc))
10112 		rc = ENXIO;
10113 	else
10114 		rc = read_via_memwin(sc, 1, addr, (void *)buf, size);
10115 	mtx_unlock(&sc->reg_lock);
10116 	if (rc != 0)
10117 		goto done;
10118 
10119 	nentries = size / sizeof(struct fw_devlog_e);
10120 	for (i = 0; i < nentries; i++) {
10121 		e = &buf[i];
10122 
10123 		if (e->timestamp == 0)
10124 			break;	/* end */
10125 
10126 		e->timestamp = be64toh(e->timestamp);
10127 		e->seqno = be32toh(e->seqno);
10128 		for (j = 0; j < 8; j++)
10129 			e->params[j] = be32toh(e->params[j]);
10130 
10131 		if (e->timestamp < ftstamp) {
10132 			ftstamp = e->timestamp;
10133 			first = i;
10134 		}
10135 	}
10136 
10137 	if (buf[first].timestamp == 0)
10138 		goto done;	/* nothing in the log */
10139 
10140 	sbuf_printf(sb, "%10s  %15s  %8s  %8s  %s\n",
10141 	    "Seq#", "Tstamp", "Level", "Facility", "Message");
10142 
10143 	i = first;
10144 	do {
10145 		e = &buf[i];
10146 		if (e->timestamp == 0)
10147 			break;	/* end */
10148 
10149 		sbuf_printf(sb, "%10d  %15ju  %8s  %8s  ",
10150 		    e->seqno, e->timestamp,
10151 		    (e->level < nitems(devlog_level_strings) ?
10152 			devlog_level_strings[e->level] : "UNKNOWN"),
10153 		    (e->facility < nitems(devlog_facility_strings) ?
10154 			devlog_facility_strings[e->facility] : "UNKNOWN"));
10155 		sbuf_printf(sb, e->fmt, e->params[0], e->params[1],
10156 		    e->params[2], e->params[3], e->params[4],
10157 		    e->params[5], e->params[6], e->params[7]);
10158 
10159 		if (++i == nentries)
10160 			i = 0;
10161 	} while (i != first);
10162 done:
10163 	free(buf, M_CXGBE);
10164 	return (rc);
10165 }
10166 
10167 static int
sysctl_devlog(SYSCTL_HANDLER_ARGS)10168 sysctl_devlog(SYSCTL_HANDLER_ARGS)
10169 {
10170 	struct adapter *sc = arg1;
10171 	int rc, i, coreid = arg2;
10172 	struct sbuf *sb;
10173 
10174 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10175 	if (sb == NULL)
10176 		return (ENOMEM);
10177 	if (coreid == -1) {
10178 		/* -1 means all cores */
10179 		for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10180 			if (sc->params.ncores > 0)
10181 				sbuf_printf(sb, "=== CIM core %u ===\n", i);
10182 			rc = sbuf_devlog(sc, i, sb, M_WAITOK);
10183 		}
10184 	} else {
10185 		KASSERT(coreid >= 0 && coreid < sc->params.ncores,
10186 		    ("%s: bad coreid %d\n", __func__, coreid));
10187 		rc = sbuf_devlog(sc, coreid, sb, M_WAITOK);
10188 	}
10189 	if (rc == 0)
10190 		rc = sbuf_finish(sb);
10191 	sbuf_delete(sb);
10192 	return (rc);
10193 }
10194 
10195 static void
dump_devlog(struct adapter * sc)10196 dump_devlog(struct adapter *sc)
10197 {
10198 	int rc, i;
10199 	struct sbuf sb;
10200 
10201 	if (sbuf_new(&sb, NULL, 4096, SBUF_AUTOEXTEND) != &sb) {
10202 		log(LOG_DEBUG, "%s: failed to generate devlog dump.\n",
10203 		    device_get_nameunit(sc->dev));
10204 		return;
10205 	}
10206 	for (i = rc = 0; i < sc->params.ncores && rc == 0; i++) {
10207 		if (sc->params.ncores > 0)
10208 			sbuf_printf(&sb, "=== CIM core %u ===\n", i);
10209 		rc = sbuf_devlog(sc, i, &sb, M_WAITOK);
10210 	}
10211 	if (rc == 0) {
10212 		sbuf_finish(&sb);
10213 		log(LOG_DEBUG, "%s: device log follows.\n%s",
10214 		    device_get_nameunit(sc->dev), sbuf_data(&sb));
10215 	}
10216 	sbuf_delete(&sb);
10217 }
10218 
10219 static int
sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)10220 sysctl_fcoe_stats(SYSCTL_HANDLER_ARGS)
10221 {
10222 	struct adapter *sc = arg1;
10223 	struct sbuf *sb;
10224 	int rc;
10225 	struct tp_fcoe_stats stats[MAX_NCHAN];
10226 	int i, nchan = sc->chip_params->nchan;
10227 
10228 	rc = 0;
10229 	mtx_lock(&sc->reg_lock);
10230 	if (hw_off_limits(sc))
10231 		rc = ENXIO;
10232 	else {
10233 		for (i = 0; i < nchan; i++)
10234 			t4_get_fcoe_stats(sc, i, &stats[i], 1);
10235 	}
10236 	mtx_unlock(&sc->reg_lock);
10237 	if (rc != 0)
10238 		return (rc);
10239 
10240 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
10241 	if (sb == NULL)
10242 		return (ENOMEM);
10243 
10244 	if (nchan > 2) {
10245 		sbuf_printf(sb, "                   channel 0        channel 1"
10246 		    "        channel 2        channel 3");
10247 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju %16ju %16ju",
10248 		    stats[0].octets_ddp, stats[1].octets_ddp,
10249 		    stats[2].octets_ddp, stats[3].octets_ddp);
10250 		sbuf_printf(sb, "\nframesDDP:  %16u %16u %16u %16u",
10251 		    stats[0].frames_ddp, stats[1].frames_ddp,
10252 		    stats[2].frames_ddp, stats[3].frames_ddp);
10253 		sbuf_printf(sb, "\nframesDrop: %16u %16u %16u %16u",
10254 		    stats[0].frames_drop, stats[1].frames_drop,
10255 		    stats[2].frames_drop, stats[3].frames_drop);
10256 	} else {
10257 		sbuf_printf(sb, "                   channel 0        channel 1");
10258 		sbuf_printf(sb, "\noctetsDDP:  %16ju %16ju",
10259 		    stats[0].octets_ddp, stats[1].octets_ddp);
10260 		sbuf_printf(sb, "\nframesDDP:  %16u %16u",
10261 		    stats[0].frames_ddp, stats[1].frames_ddp);
10262 		sbuf_printf(sb, "\nframesDrop: %16u %16u",
10263 		    stats[0].frames_drop, stats[1].frames_drop);
10264 	}
10265 
10266 	rc = sbuf_finish(sb);
10267 	sbuf_delete(sb);
10268 
10269 	return (rc);
10270 }
10271 
10272 static int
sysctl_hw_sched(SYSCTL_HANDLER_ARGS)10273 sysctl_hw_sched(SYSCTL_HANDLER_ARGS)
10274 {
10275 	struct adapter *sc = arg1;
10276 	struct sbuf *sb;
10277 	int rc, i;
10278 	unsigned int map, kbps, ipg, mode;
10279 	unsigned int pace_tab[NTX_SCHED];
10280 
10281 	sb = sbuf_new_for_sysctl(NULL, NULL, 512, req);
10282 	if (sb == NULL)
10283 		return (ENOMEM);
10284 
10285 	mtx_lock(&sc->reg_lock);
10286 	if (hw_off_limits(sc)) {
10287 		mtx_unlock(&sc->reg_lock);
10288 		rc = ENXIO;
10289 		goto done;
10290 	}
10291 
10292 	map = t4_read_reg(sc, A_TP_TX_MOD_QUEUE_REQ_MAP);
10293 	mode = G_TIMERMODE(t4_read_reg(sc, A_TP_MOD_CONFIG));
10294 	t4_read_pace_tbl(sc, pace_tab);
10295 	mtx_unlock(&sc->reg_lock);
10296 
10297 	sbuf_printf(sb, "Scheduler  Mode   Channel  Rate (Kbps)   "
10298 	    "Class IPG (0.1 ns)   Flow IPG (us)");
10299 
10300 	for (i = 0; i < NTX_SCHED; ++i, map >>= 2) {
10301 		t4_get_tx_sched(sc, i, &kbps, &ipg, 1);
10302 		sbuf_printf(sb, "\n    %u      %-5s     %u     ", i,
10303 		    (mode & (1 << i)) ? "flow" : "class", map & 3);
10304 		if (kbps)
10305 			sbuf_printf(sb, "%9u     ", kbps);
10306 		else
10307 			sbuf_printf(sb, " disabled     ");
10308 
10309 		if (ipg)
10310 			sbuf_printf(sb, "%13u        ", ipg);
10311 		else
10312 			sbuf_printf(sb, "     disabled        ");
10313 
10314 		if (pace_tab[i])
10315 			sbuf_printf(sb, "%10u", pace_tab[i]);
10316 		else
10317 			sbuf_printf(sb, "  disabled");
10318 	}
10319 	rc = sbuf_finish(sb);
10320 done:
10321 	sbuf_delete(sb);
10322 	return (rc);
10323 }
10324 
10325 static int
sysctl_lb_stats(SYSCTL_HANDLER_ARGS)10326 sysctl_lb_stats(SYSCTL_HANDLER_ARGS)
10327 {
10328 	struct adapter *sc = arg1;
10329 	struct sbuf *sb;
10330 	int rc, i, j;
10331 	uint64_t *p0, *p1;
10332 	struct lb_port_stats s[2];
10333 	static const char *stat_name[] = {
10334 		"OctetsOK:", "FramesOK:", "BcastFrames:", "McastFrames:",
10335 		"UcastFrames:", "ErrorFrames:", "Frames64:", "Frames65To127:",
10336 		"Frames128To255:", "Frames256To511:", "Frames512To1023:",
10337 		"Frames1024To1518:", "Frames1519ToMax:", "FramesDropped:",
10338 		"BG0FramesDropped:", "BG1FramesDropped:", "BG2FramesDropped:",
10339 		"BG3FramesDropped:", "BG0FramesTrunc:", "BG1FramesTrunc:",
10340 		"BG2FramesTrunc:", "BG3FramesTrunc:"
10341 	};
10342 
10343 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10344 	if (sb == NULL)
10345 		return (ENOMEM);
10346 
10347 	memset(s, 0, sizeof(s));
10348 
10349 	rc = 0;
10350 	for (i = 0; i < sc->chip_params->nchan; i += 2) {
10351 		mtx_lock(&sc->reg_lock);
10352 		if (hw_off_limits(sc))
10353 			rc = ENXIO;
10354 		else {
10355 			t4_get_lb_stats(sc, i, &s[0]);
10356 			t4_get_lb_stats(sc, i + 1, &s[1]);
10357 		}
10358 		mtx_unlock(&sc->reg_lock);
10359 		if (rc != 0)
10360 			break;
10361 
10362 		p0 = &s[0].octets;
10363 		p1 = &s[1].octets;
10364 		sbuf_printf(sb, "%s                       Loopback %u"
10365 		    "           Loopback %u", i == 0 ? "" : "\n", i, i + 1);
10366 
10367 		for (j = 0; j < nitems(stat_name); j++)
10368 			sbuf_printf(sb, "\n%-17s %20ju %20ju", stat_name[j],
10369 				   *p0++, *p1++);
10370 	}
10371 
10372 	if (rc == 0)
10373 		rc = sbuf_finish(sb);
10374 	sbuf_delete(sb);
10375 
10376 	return (rc);
10377 }
10378 
10379 static int
sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)10380 sysctl_linkdnrc(SYSCTL_HANDLER_ARGS)
10381 {
10382 	int rc = 0;
10383 	struct port_info *pi = arg1;
10384 	struct link_config *lc = &pi->link_cfg;
10385 	struct sbuf *sb;
10386 
10387 	sb = sbuf_new_for_sysctl(NULL, NULL, 64, req);
10388 	if (sb == NULL)
10389 		return (ENOMEM);
10390 
10391 	if (lc->link_ok || lc->link_down_rc == 255)
10392 		sbuf_printf(sb, "n/a");
10393 	else
10394 		sbuf_printf(sb, "%s", t4_link_down_rc_str(lc->link_down_rc));
10395 
10396 	rc = sbuf_finish(sb);
10397 	sbuf_delete(sb);
10398 
10399 	return (rc);
10400 }
10401 
10402 struct mem_desc {
10403 	uint64_t base;
10404 	uint64_t limit;
10405 	u_int idx;
10406 };
10407 
10408 static int
mem_desc_cmp(const void * a,const void * b)10409 mem_desc_cmp(const void *a, const void *b)
10410 {
10411 	const uint64_t v1 = ((const struct mem_desc *)a)->base;
10412 	const uint64_t v2 = ((const struct mem_desc *)b)->base;
10413 
10414 	if (v1 < v2)
10415 		return (-1);
10416 	else if (v1 > v2)
10417 		return (1);
10418 
10419 	return (0);
10420 }
10421 
10422 static void
mem_region_show(struct sbuf * sb,const char * name,uint64_t from,uint64_t to)10423 mem_region_show(struct sbuf *sb, const char *name, uint64_t from, uint64_t to)
10424 {
10425 	uintmax_t size;
10426 
10427 	if (from == to)
10428 		return;
10429 
10430 	size = to - from + 1;
10431 	if (size == 0)
10432 		return;
10433 
10434 	if (from > UINT32_MAX || to > UINT32_MAX)
10435 		sbuf_printf(sb, "%-18s 0x%012jx-0x%012jx [%ju]\n", name,
10436 		    (uintmax_t)from, (uintmax_t)to, size);
10437 	else
10438 		sbuf_printf(sb, "%-18s 0x%08jx-0x%08jx [%ju]\n", name,
10439 		    (uintmax_t)from, (uintmax_t)to, size);
10440 }
10441 
10442 static int
sysctl_meminfo(SYSCTL_HANDLER_ARGS)10443 sysctl_meminfo(SYSCTL_HANDLER_ARGS)
10444 {
10445 	struct adapter *sc = arg1;
10446 	struct sbuf *sb;
10447 	int rc, i, n, nchan;
10448 	uint32_t lo, hi, used, free, alloc;
10449 	static const char *memory[] = {
10450 		"EDC0:", "EDC1:", "MC:", "MC0:", "MC1:", "HMA:"
10451 	};
10452 	static const char *region[] = {
10453 		"DBQ contexts:", "IMSG contexts:", "FLM cache:", "TCBs:",
10454 		"Pstructs:", "Timers:", "Rx FL:", "Tx FL:", "Pstruct FL:",
10455 		"Tx payload:", "Rx payload:", "LE hash:", "iSCSI region:",
10456 		"TDDP region:", "TPT region:", "STAG region:", "RQ region:",
10457 		"RQUDP region:", "PBL region:", "TXPBL region:",
10458 		"TLSKey region:", "RRQ region:", "NVMe STAG region:",
10459 		"NVMe RQ region:", "NVMe RXPBL region:", "NVMe TPT region:",
10460 		"NVMe TXPBL region:", "DBVFIFO region:", "ULPRX state:",
10461 		"ULPTX state:", "RoCE RRQ region:", "On-chip queues:",
10462 	};
10463 	struct mem_desc avail[4];
10464 	struct mem_desc mem[nitems(region) + 3];	/* up to 3 holes */
10465 	struct mem_desc *md;
10466 
10467 	rc = sysctl_wire_old_buffer(req, 0);
10468 	if (rc != 0)
10469 		return (rc);
10470 
10471 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10472 	if (sb == NULL)
10473 		return (ENOMEM);
10474 
10475 	for (i = 0; i < nitems(mem); i++) {
10476 		mem[i].limit = 0;
10477 		mem[i].idx = i;
10478 	}
10479 
10480 	mtx_lock(&sc->reg_lock);
10481 	if (hw_off_limits(sc)) {
10482 		rc = ENXIO;
10483 		goto done;
10484 	}
10485 
10486 	/* Find and sort the populated memory ranges */
10487 	i = 0;
10488 	lo = t4_read_reg(sc, A_MA_TARGET_MEM_ENABLE);
10489 	if (lo & F_EDRAM0_ENABLE) {
10490 		hi = t4_read_reg(sc, A_MA_EDRAM0_BAR);
10491 		if (chip_id(sc) >= CHELSIO_T7) {
10492 			avail[i].base = (uint64_t)G_T7_EDRAM0_BASE(hi) << 20;
10493 			avail[i].limit = avail[i].base +
10494 			    (G_T7_EDRAM0_SIZE(hi) << 20);
10495 		} else {
10496 			avail[i].base = (uint64_t)G_EDRAM0_BASE(hi) << 20;
10497 			avail[i].limit = avail[i].base +
10498 			    (G_EDRAM0_SIZE(hi) << 20);
10499 		}
10500 		avail[i].idx = 0;
10501 		i++;
10502 	}
10503 	if (lo & F_EDRAM1_ENABLE) {
10504 		hi = t4_read_reg(sc, A_MA_EDRAM1_BAR);
10505 		if (chip_id(sc) >= CHELSIO_T7) {
10506 			avail[i].base = (uint64_t)G_T7_EDRAM1_BASE(hi) << 20;
10507 			avail[i].limit = avail[i].base +
10508 			    (G_T7_EDRAM1_SIZE(hi) << 20);
10509 		} else {
10510 			avail[i].base = (uint64_t)G_EDRAM1_BASE(hi) << 20;
10511 			avail[i].limit = avail[i].base +
10512 			    (G_EDRAM1_SIZE(hi) << 20);
10513 		}
10514 		avail[i].idx = 1;
10515 		i++;
10516 	}
10517 	if (lo & F_EXT_MEM_ENABLE) {
10518 		switch (chip_id(sc)) {
10519 		case CHELSIO_T4:
10520 		case CHELSIO_T6:
10521 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY_BAR);
10522 			avail[i].base = (uint64_t)G_EXT_MEM_BASE(hi) << 20;
10523 			avail[i].limit = avail[i].base +
10524 			    (G_EXT_MEM_SIZE(hi) << 20);
10525 			avail[i].idx = 2;
10526 			break;
10527 		case CHELSIO_T5:
10528 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10529 			avail[i].base = (uint64_t)G_EXT_MEM0_BASE(hi) << 20;
10530 			avail[i].limit = avail[i].base +
10531 			    (G_EXT_MEM0_SIZE(hi) << 20);
10532 			avail[i].idx = 3;	/* Call it MC0 for T5 */
10533 			break;
10534 		default:
10535 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY0_BAR);
10536 			avail[i].base = (uint64_t)G_T7_EXT_MEM0_BASE(hi) << 20;
10537 			avail[i].limit = avail[i].base +
10538 			    (G_T7_EXT_MEM0_SIZE(hi) << 20);
10539 			avail[i].idx = 3;	/* Call it MC0 for T7+ */
10540 			break;
10541 		}
10542 		i++;
10543 	}
10544 	if (lo & F_EXT_MEM1_ENABLE && !(lo & F_MC_SPLIT)) {
10545 		/* Only T5 and T7+ have 2 MCs. */
10546 		MPASS(is_t5(sc) || chip_id(sc) >= CHELSIO_T7);
10547 
10548 		hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10549 		if (chip_id(sc) >= CHELSIO_T7) {
10550 			avail[i].base = (uint64_t)G_T7_EXT_MEM1_BASE(hi) << 20;
10551 			avail[i].limit = avail[i].base +
10552 			    (G_T7_EXT_MEM1_SIZE(hi) << 20);
10553 		} else {
10554 			avail[i].base = (uint64_t)G_EXT_MEM1_BASE(hi) << 20;
10555 			avail[i].limit = avail[i].base +
10556 			    (G_EXT_MEM1_SIZE(hi) << 20);
10557 		}
10558 		avail[i].idx = 4;
10559 		i++;
10560 	}
10561 	if (lo & F_HMA_MUX) {
10562 		/* Only T6+ have HMA. */
10563 		MPASS(chip_id(sc) >= CHELSIO_T6);
10564 
10565 		if (chip_id(sc) >= CHELSIO_T7) {
10566 			hi = t4_read_reg(sc, A_MA_HOST_MEMORY_BAR);
10567 			avail[i].base = (uint64_t)G_HMATARGETBASE(hi) << 20;
10568 			avail[i].limit = avail[i].base +
10569 			    (G_T7_HMA_SIZE(hi) << 20);
10570 		} else {
10571 			hi = t4_read_reg(sc, A_MA_EXT_MEMORY1_BAR);
10572 			avail[i].base = G_EXT_MEM1_BASE(hi) << 20;
10573 			avail[i].limit = avail[i].base +
10574 			    (G_EXT_MEM1_SIZE(hi) << 20);
10575 		}
10576 		avail[i].idx = 5;
10577 		i++;
10578 	}
10579 	MPASS(i <= nitems(avail));
10580 	if (!i)                                    /* no memory available */
10581 		goto done;
10582 	qsort(avail, i, sizeof(struct mem_desc), mem_desc_cmp);
10583 
10584 	md = &mem[0];
10585 	(md++)->base = t4_read_reg(sc, A_SGE_DBQ_CTXT_BADDR);
10586 	(md++)->base = t4_read_reg(sc, A_SGE_IMSG_CTXT_BADDR);
10587 	(md++)->base = t4_read_reg(sc, A_SGE_FLM_CACHE_BADDR);
10588 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
10589 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_BASE);
10590 	(md++)->base = t4_read_reg(sc, A_TP_CMM_TIMER_BASE);
10591 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_RX_FLST_BASE);
10592 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_TX_FLST_BASE);
10593 	(md++)->base = t4_read_reg(sc, A_TP_CMM_MM_PS_FLST_BASE);
10594 
10595 	/* the next few have explicit upper bounds */
10596 	md->base = t4_read_reg(sc, A_TP_PMM_TX_BASE);
10597 	md->limit = md->base - 1 +
10598 		    t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE) *
10599 		    G_PMTXMAXPAGE(t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE));
10600 	md++;
10601 
10602 	md->base = t4_read_reg(sc, A_TP_PMM_RX_BASE);
10603 	md->limit = md->base - 1 +
10604 		    t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) *
10605 		    G_PMRXMAXPAGE(t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE));
10606 	md++;
10607 
10608 	if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
10609 		if (chip_id(sc) <= CHELSIO_T5)
10610 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TID_BASE);
10611 		else
10612 			md->base = t4_read_reg(sc, A_LE_DB_HASH_TBL_BASE_ADDR);
10613 		md->limit = 0;
10614 	} else {
10615 		md->base = 0;
10616 		md->idx = nitems(region);  /* hide it */
10617 	}
10618 	md++;
10619 
10620 #define ulp_region(reg) do {\
10621 		const u_int shift = chip_id(sc) >= CHELSIO_T7 ? 4 : 0; \
10622 		md->base = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _LLIMIT) << shift; \
10623 		md->limit = (uint64_t)t4_read_reg(sc, A_ULP_ ## reg ## _ULIMIT) << shift; \
10624 		md->limit += (1 << shift) - 1; \
10625 		md++; \
10626 	} while (0)
10627 
10628 #define	hide_ulp_region() do { \
10629 		md->base = 0; \
10630 		md->idx = nitems(region); \
10631 		md++; \
10632 	} while (0)
10633 
10634 	ulp_region(RX_ISCSI);
10635 	ulp_region(RX_TDDP);
10636 	ulp_region(TX_TPT);
10637 	ulp_region(RX_STAG);
10638 	ulp_region(RX_RQ);
10639 	if (chip_id(sc) < CHELSIO_T7)
10640 		ulp_region(RX_RQUDP);
10641 	else
10642 		hide_ulp_region();
10643 	ulp_region(RX_PBL);
10644 	ulp_region(TX_PBL);
10645 	if (chip_id(sc) >= CHELSIO_T6)
10646 		ulp_region(RX_TLS_KEY);
10647 	else
10648 		hide_ulp_region();
10649 	if (chip_id(sc) >= CHELSIO_T7) {
10650 		ulp_region(RX_RRQ);
10651 		ulp_region(RX_NVME_TCP_STAG);
10652 		ulp_region(RX_NVME_TCP_RQ);
10653 		ulp_region(RX_NVME_TCP_PBL);
10654 		ulp_region(TX_NVME_TCP_TPT);
10655 		ulp_region(TX_NVME_TCP_PBL);
10656 	} else {
10657 		hide_ulp_region();
10658 		hide_ulp_region();
10659 		hide_ulp_region();
10660 		hide_ulp_region();
10661 		hide_ulp_region();
10662 		hide_ulp_region();
10663 	}
10664 #undef ulp_region
10665 #undef hide_ulp_region
10666 
10667 	md->base = 0;
10668 	if (is_t4(sc))
10669 		md->idx = nitems(region);
10670 	else {
10671 		uint32_t size = 0;
10672 		uint32_t sge_ctrl = t4_read_reg(sc, A_SGE_CONTROL2);
10673 		uint32_t fifo_size = t4_read_reg(sc, A_SGE_DBVFIFO_SIZE);
10674 
10675 		if (is_t5(sc)) {
10676 			if (sge_ctrl & F_VFIFO_ENABLE)
10677 				size = fifo_size << 2;
10678 		} else
10679 			size = G_T6_DBVFIFO_SIZE(fifo_size) << 6;
10680 
10681 		if (size) {
10682 			md->base = t4_read_reg(sc, A_SGE_DBVFIFO_BADDR);
10683 			md->limit = md->base + size - 1;
10684 		} else
10685 			md->idx = nitems(region);
10686 	}
10687 	md++;
10688 
10689 	md->base = t4_read_reg(sc, A_ULP_RX_CTX_BASE);
10690 	md->limit = 0;
10691 	md++;
10692 	md->base = t4_read_reg(sc, A_ULP_TX_ERR_TABLE_BASE);
10693 	md->limit = 0;
10694 	md++;
10695 
10696 	if (chip_id(sc) >= CHELSIO_T7) {
10697 		t4_tp_pio_read(sc, &lo, 1, A_TP_ROCE_RRQ_BASE, false);
10698 		md->base = lo;
10699 	} else {
10700 		md->base = 0;
10701 		md->idx = nitems(region);
10702 	}
10703 	md++;
10704 
10705 	md->base = sc->vres.ocq.start;
10706 	if (sc->vres.ocq.size)
10707 		md->limit = md->base + sc->vres.ocq.size - 1;
10708 	else
10709 		md->idx = nitems(region);  /* hide it */
10710 	md++;
10711 
10712 	/* add any address-space holes, there can be up to 3 */
10713 	for (n = 0; n < i - 1; n++)
10714 		if (avail[n].limit < avail[n + 1].base)
10715 			(md++)->base = avail[n].limit;
10716 	if (avail[n].limit)
10717 		(md++)->base = avail[n].limit;
10718 
10719 	n = md - mem;
10720 	MPASS(n <= nitems(mem));
10721 	qsort(mem, n, sizeof(struct mem_desc), mem_desc_cmp);
10722 
10723 	for (lo = 0; lo < i; lo++)
10724 		mem_region_show(sb, memory[avail[lo].idx], avail[lo].base,
10725 				avail[lo].limit - 1);
10726 
10727 	sbuf_printf(sb, "\n");
10728 	for (i = 0; i < n; i++) {
10729 		if (mem[i].idx >= nitems(region))
10730 			continue;                        /* skip holes */
10731 		if (!mem[i].limit)
10732 			mem[i].limit = i < n - 1 ? mem[i + 1].base - 1 : ~0;
10733 		mem_region_show(sb, region[mem[i].idx], mem[i].base,
10734 				mem[i].limit);
10735 	}
10736 
10737 	lo = t4_read_reg(sc, A_CIM_SDRAM_BASE_ADDR);
10738 	hi = t4_read_reg(sc, A_CIM_SDRAM_ADDR_SIZE) + lo - 1;
10739 	if (hi != lo  - 1) {
10740 		sbuf_printf(sb, "\n");
10741 		mem_region_show(sb, "uP RAM:", lo, hi);
10742 	}
10743 
10744 	lo = t4_read_reg(sc, A_CIM_EXTMEM2_BASE_ADDR);
10745 	hi = t4_read_reg(sc, A_CIM_EXTMEM2_ADDR_SIZE) + lo - 1;
10746 	if (hi != lo  - 1)
10747 		mem_region_show(sb, "uP Extmem2:", lo, hi);
10748 
10749 	lo = t4_read_reg(sc, A_TP_PMM_RX_MAX_PAGE);
10750 	if (chip_id(sc) >= CHELSIO_T7)
10751 		nchan = 1 << G_T7_PMRXNUMCHN(lo);
10752 	else
10753 		nchan = lo & F_PMRXNUMCHN ? 2 : 1;
10754 	for (i = 0, free = 0; i < nchan; i++)
10755 		free += G_FREERXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_RX_CNT));
10756 	sbuf_printf(sb, "\n%u Rx pages (%u free) of size %uKiB for %u channels\n",
10757 		   G_PMRXMAXPAGE(lo), free,
10758 		   t4_read_reg(sc, A_TP_PMM_RX_PAGE_SIZE) >> 10, nchan);
10759 
10760 	lo = t4_read_reg(sc, A_TP_PMM_TX_MAX_PAGE);
10761 	hi = t4_read_reg(sc, A_TP_PMM_TX_PAGE_SIZE);
10762 	if (chip_id(sc) >= CHELSIO_T7)
10763 		nchan = 1 << G_T7_PMTXNUMCHN(lo);
10764 	else
10765 		nchan = 1 << G_PMTXNUMCHN(lo);
10766 	for (i = 0, free = 0; i < nchan; i++)
10767 		free += G_FREETXPAGECOUNT(t4_read_reg(sc, A_TP_FLM_FREE_TX_CNT));
10768 	sbuf_printf(sb, "%u Tx pages (%u free) of size %u%ciB for %u channels\n",
10769 		   G_PMTXMAXPAGE(lo), free,
10770 		   hi >= (1 << 20) ? (hi >> 20) : (hi >> 10),
10771 		   hi >= (1 << 20) ? 'M' : 'K', nchan);
10772 	sbuf_printf(sb, "%u p-structs (%u free)\n",
10773 		   t4_read_reg(sc, A_TP_CMM_MM_MAX_PSTRUCT),
10774 		   G_FREEPSTRUCTCOUNT(t4_read_reg(sc, A_TP_FLM_FREE_PS_CNT)));
10775 
10776 	for (i = 0; i < 4; i++) {
10777 		if (chip_id(sc) > CHELSIO_T5)
10778 			lo = t4_read_reg(sc, A_MPS_RX_MAC_BG_PG_CNT0 + i * 4);
10779 		else
10780 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV0 + i * 4);
10781 		if (is_t5(sc)) {
10782 			used = G_T5_USED(lo);
10783 			alloc = G_T5_ALLOC(lo);
10784 		} else {
10785 			used = G_USED(lo);
10786 			alloc = G_ALLOC(lo);
10787 		}
10788 		/* For T6+ these are MAC buffer groups */
10789 		sbuf_printf(sb, "\nPort %d using %u pages out of %u allocated",
10790 		    i, used, alloc);
10791 	}
10792 	for (i = 0; i < sc->chip_params->nchan; i++) {
10793 		if (chip_id(sc) > CHELSIO_T5)
10794 			lo = t4_read_reg(sc, A_MPS_RX_LPBK_BG_PG_CNT0 + i * 4);
10795 		else
10796 			lo = t4_read_reg(sc, A_MPS_RX_PG_RSV4 + i * 4);
10797 		if (is_t5(sc)) {
10798 			used = G_T5_USED(lo);
10799 			alloc = G_T5_ALLOC(lo);
10800 		} else {
10801 			used = G_USED(lo);
10802 			alloc = G_ALLOC(lo);
10803 		}
10804 		/* For T6+ these are MAC buffer groups */
10805 		sbuf_printf(sb,
10806 		    "\nLoopback %d using %u pages out of %u allocated",
10807 		    i, used, alloc);
10808 	}
10809 done:
10810 	mtx_unlock(&sc->reg_lock);
10811 	if (rc == 0)
10812 		rc = sbuf_finish(sb);
10813 	sbuf_delete(sb);
10814 	return (rc);
10815 }
10816 
10817 static inline void
tcamxy2valmask(uint64_t x,uint64_t y,uint8_t * addr,uint64_t * mask)10818 tcamxy2valmask(uint64_t x, uint64_t y, uint8_t *addr, uint64_t *mask)
10819 {
10820 	*mask = x | y;
10821 	y = htobe64(y);
10822 	memcpy(addr, (char *)&y + 2, ETHER_ADDR_LEN);
10823 }
10824 
10825 static int
sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)10826 sysctl_mps_tcam(SYSCTL_HANDLER_ARGS)
10827 {
10828 	struct adapter *sc = arg1;
10829 	struct sbuf *sb;
10830 	int rc, i;
10831 
10832 	MPASS(chip_id(sc) <= CHELSIO_T5);
10833 
10834 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10835 	if (sb == NULL)
10836 		return (ENOMEM);
10837 
10838 	sbuf_printf(sb,
10839 	    "Idx  Ethernet address     Mask     Vld Ports PF"
10840 	    "  VF              Replication             P0 P1 P2 P3  ML");
10841 	rc = 0;
10842 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10843 		uint64_t tcamx, tcamy, mask;
10844 		uint32_t cls_lo, cls_hi;
10845 		uint8_t addr[ETHER_ADDR_LEN];
10846 
10847 		mtx_lock(&sc->reg_lock);
10848 		if (hw_off_limits(sc))
10849 			rc = ENXIO;
10850 		else {
10851 			tcamy = t4_read_reg64(sc, MPS_CLS_TCAM_Y_L(i));
10852 			tcamx = t4_read_reg64(sc, MPS_CLS_TCAM_X_L(i));
10853 		}
10854 		mtx_unlock(&sc->reg_lock);
10855 		if (rc != 0)
10856 			break;
10857 		if (tcamx & tcamy)
10858 			continue;
10859 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
10860 		mtx_lock(&sc->reg_lock);
10861 		if (hw_off_limits(sc))
10862 			rc = ENXIO;
10863 		else {
10864 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
10865 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
10866 		}
10867 		mtx_unlock(&sc->reg_lock);
10868 		if (rc != 0)
10869 			break;
10870 		sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x %012jx"
10871 			   "  %c   %#x%4u%4d", i, addr[0], addr[1], addr[2],
10872 			   addr[3], addr[4], addr[5], (uintmax_t)mask,
10873 			   (cls_lo & F_SRAM_VLD) ? 'Y' : 'N',
10874 			   G_PORTMAP(cls_hi), G_PF(cls_lo),
10875 			   (cls_lo & F_VF_VALID) ? G_VF(cls_lo) : -1);
10876 
10877 		if (cls_lo & F_REPLICATE) {
10878 			struct fw_ldst_cmd ldst_cmd;
10879 
10880 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
10881 			ldst_cmd.op_to_addrspace =
10882 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
10883 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
10884 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
10885 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
10886 			ldst_cmd.u.mps.rplc.fid_idx =
10887 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
10888 				V_FW_LDST_CMD_IDX(i));
10889 
10890 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
10891 			    "t4mps");
10892 			if (rc)
10893 				break;
10894 			if (hw_off_limits(sc))
10895 				rc = ENXIO;
10896 			else
10897 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
10898 				    sizeof(ldst_cmd), &ldst_cmd);
10899 			end_synchronized_op(sc, 0);
10900 			if (rc != 0)
10901 				break;
10902 			else {
10903 				sbuf_printf(sb, " %08x %08x %08x %08x",
10904 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
10905 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
10906 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
10907 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
10908 			}
10909 		} else
10910 			sbuf_printf(sb, "%36s", "");
10911 
10912 		sbuf_printf(sb, "%4u%3u%3u%3u %#3x", G_SRAM_PRIO0(cls_lo),
10913 		    G_SRAM_PRIO1(cls_lo), G_SRAM_PRIO2(cls_lo),
10914 		    G_SRAM_PRIO3(cls_lo), (cls_lo >> S_MULTILISTEN0) & 0xf);
10915 	}
10916 
10917 	if (rc)
10918 		(void) sbuf_finish(sb);
10919 	else
10920 		rc = sbuf_finish(sb);
10921 	sbuf_delete(sb);
10922 
10923 	return (rc);
10924 }
10925 
10926 static int
sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)10927 sysctl_mps_tcam_t6(SYSCTL_HANDLER_ARGS)
10928 {
10929 	struct adapter *sc = arg1;
10930 	struct sbuf *sb;
10931 	int rc, i;
10932 
10933 	MPASS(chip_id(sc) == CHELSIO_T6);
10934 
10935 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
10936 	if (sb == NULL)
10937 		return (ENOMEM);
10938 
10939 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
10940 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
10941 	    "                           Replication"
10942 	    "                                    P0 P1 P2 P3  ML");
10943 
10944 	rc = 0;
10945 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
10946 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
10947 		uint16_t ivlan;
10948 		uint64_t tcamx, tcamy, val, mask;
10949 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
10950 		uint8_t addr[ETHER_ADDR_LEN];
10951 
10952 		ctl = V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0);
10953 		if (i < 256)
10954 			ctl |= V_CTLTCAMINDEX(i) | V_CTLTCAMSEL(0);
10955 		else
10956 			ctl |= V_CTLTCAMINDEX(i - 256) | V_CTLTCAMSEL(1);
10957 		mtx_lock(&sc->reg_lock);
10958 		if (hw_off_limits(sc))
10959 			rc = ENXIO;
10960 		else {
10961 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10962 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10963 			tcamy = G_DMACH(val) << 32;
10964 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10965 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10966 		}
10967 		mtx_unlock(&sc->reg_lock);
10968 		if (rc != 0)
10969 			break;
10970 
10971 		lookup_type = G_DATALKPTYPE(data2);
10972 		port_num = G_DATAPORTNUM(data2);
10973 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
10974 			/* Inner header VNI */
10975 			vniy = ((data2 & F_DATAVIDH2) << 23) |
10976 				       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
10977 			dip_hit = data2 & F_DATADIPHIT;
10978 			vlan_vld = 0;
10979 		} else {
10980 			vniy = 0;
10981 			dip_hit = 0;
10982 			vlan_vld = data2 & F_DATAVIDH2;
10983 			ivlan = G_VIDL(val);
10984 		}
10985 
10986 		ctl |= V_CTLXYBITSEL(1);
10987 		mtx_lock(&sc->reg_lock);
10988 		if (hw_off_limits(sc))
10989 			rc = ENXIO;
10990 		else {
10991 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
10992 			val = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA1_REQ_ID1);
10993 			tcamx = G_DMACH(val) << 32;
10994 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA0_REQ_ID1);
10995 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM_RDATA2_REQ_ID1);
10996 		}
10997 		mtx_unlock(&sc->reg_lock);
10998 		if (rc != 0)
10999 			break;
11000 
11001 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11002 			/* Inner header VNI mask */
11003 			vnix = ((data2 & F_DATAVIDH2) << 23) |
11004 			       (G_DATAVIDH1(data2) << 16) | G_VIDL(val);
11005 		} else
11006 			vnix = 0;
11007 
11008 		if (tcamx & tcamy)
11009 			continue;
11010 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
11011 
11012 		mtx_lock(&sc->reg_lock);
11013 		if (hw_off_limits(sc))
11014 			rc = ENXIO;
11015 		else {
11016 			cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11017 			cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11018 		}
11019 		mtx_unlock(&sc->reg_lock);
11020 		if (rc != 0)
11021 			break;
11022 
11023 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11024 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11025 			    "%012jx %06x %06x    -    -   %3c"
11026 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
11027 			    addr[1], addr[2], addr[3], addr[4], addr[5],
11028 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11029 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11030 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11031 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11032 		} else {
11033 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11034 			    "%012jx    -       -   ", i, addr[0], addr[1],
11035 			    addr[2], addr[3], addr[4], addr[5],
11036 			    (uintmax_t)mask);
11037 
11038 			if (vlan_vld)
11039 				sbuf_printf(sb, "%4u   Y     ", ivlan);
11040 			else
11041 				sbuf_printf(sb, "  -    N     ");
11042 
11043 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
11044 			    lookup_type ? 'I' : 'O', port_num,
11045 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11046 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11047 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11048 		}
11049 
11050 
11051 		if (cls_lo & F_T6_REPLICATE) {
11052 			struct fw_ldst_cmd ldst_cmd;
11053 
11054 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11055 			ldst_cmd.op_to_addrspace =
11056 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11057 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
11058 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11059 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11060 			ldst_cmd.u.mps.rplc.fid_idx =
11061 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11062 				V_FW_LDST_CMD_IDX(i));
11063 
11064 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11065 			    "t6mps");
11066 			if (rc)
11067 				break;
11068 			if (hw_off_limits(sc))
11069 				rc = ENXIO;
11070 			else
11071 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11072 				    sizeof(ldst_cmd), &ldst_cmd);
11073 			end_synchronized_op(sc, 0);
11074 			if (rc != 0)
11075 				break;
11076 			else {
11077 				sbuf_printf(sb, " %08x %08x %08x %08x"
11078 				    " %08x %08x %08x %08x",
11079 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11080 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11081 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11082 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11083 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11084 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11085 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11086 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11087 			}
11088 		} else
11089 			sbuf_printf(sb, "%72s", "");
11090 
11091 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11092 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11093 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11094 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11095 	}
11096 
11097 	if (rc)
11098 		(void) sbuf_finish(sb);
11099 	else
11100 		rc = sbuf_finish(sb);
11101 	sbuf_delete(sb);
11102 
11103 	return (rc);
11104 }
11105 
11106 static int
sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)11107 sysctl_mps_tcam_t7(SYSCTL_HANDLER_ARGS)
11108 {
11109 	struct adapter *sc = arg1;
11110 	struct sbuf *sb;
11111 	int rc, i;
11112 
11113 	MPASS(chip_id(sc) >= CHELSIO_T7);
11114 
11115 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11116 	if (sb == NULL)
11117 		return (ENOMEM);
11118 
11119 	sbuf_printf(sb, "Idx  Ethernet address     Mask       VNI   Mask"
11120 	    "   IVLAN Vld DIP_Hit   Lookup  Port Vld Ports PF  VF"
11121 	    "                           Replication"
11122 	    "                                    P0 P1 P2 P3  ML");
11123 
11124 	rc = 0;
11125 	for (i = 0; i < sc->chip_params->mps_tcam_size; i++) {
11126 		uint8_t dip_hit, vlan_vld, lookup_type, port_num;
11127 		uint16_t ivlan;
11128 		uint64_t tcamx, tcamy, val, mask;
11129 		uint32_t cls_lo, cls_hi, ctl, data2, vnix, vniy;
11130 		uint8_t addr[ETHER_ADDR_LEN];
11131 
11132 		/* Read tcamy */
11133 		ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11134 		if (chip_rev(sc) == 0) {
11135 			if (i < 256)
11136 				ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11137 			else
11138 				ctl |= V_CTLTCAMINDEX(i - 256) | V_T7_CTLTCAMSEL(1);
11139 		} else {
11140 #if 0
11141 			ctl = (V_CTLREQID(1) | V_CTLCMDTYPE(0) | V_CTLXYBITSEL(0));
11142 #endif
11143 			if (i < 512)
11144 				ctl |= V_CTLTCAMINDEX(i) | V_T7_CTLTCAMSEL(0);
11145 			else if (i < 1024)
11146 				ctl |= V_CTLTCAMINDEX(i - 512) | V_T7_CTLTCAMSEL(1);
11147 			else
11148 				ctl |= V_CTLTCAMINDEX(i - 1024) | V_T7_CTLTCAMSEL(2);
11149 		}
11150 
11151 		mtx_lock(&sc->reg_lock);
11152 		if (hw_off_limits(sc))
11153 			rc = ENXIO;
11154 		else {
11155 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11156 			val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11157 			tcamy = G_DMACH(val) << 32;
11158 			tcamy |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11159 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11160 		}
11161 		mtx_unlock(&sc->reg_lock);
11162 		if (rc != 0)
11163 			break;
11164 
11165 		lookup_type = G_DATALKPTYPE(data2);
11166 		port_num = G_DATAPORTNUM(data2);
11167 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11168 			/* Inner header VNI */
11169 			vniy = (((data2 & F_DATAVIDH2) |
11170 			    G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11171 			dip_hit = data2 & F_DATADIPHIT;
11172 			vlan_vld = 0;
11173 		} else {
11174 			vniy = 0;
11175 			dip_hit = 0;
11176 			vlan_vld = data2 & F_DATAVIDH2;
11177 			ivlan = G_VIDL(val);
11178 		}
11179 
11180 		ctl |= V_CTLXYBITSEL(1);
11181 		mtx_lock(&sc->reg_lock);
11182 		if (hw_off_limits(sc))
11183 			rc = ENXIO;
11184 		else {
11185 			t4_write_reg(sc, A_MPS_CLS_TCAM_DATA2_CTL, ctl);
11186 			val = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA1_REQ_ID1);
11187 			tcamx = G_DMACH(val) << 32;
11188 			tcamx |= t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA0_REQ_ID1);
11189 			data2 = t4_read_reg(sc, A_MPS_CLS_TCAM0_RDATA2_REQ_ID1);
11190 		}
11191 		mtx_unlock(&sc->reg_lock);
11192 		if (rc != 0)
11193 			break;
11194 
11195 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11196 			/* Inner header VNI mask */
11197 			vnix = (((data2 & F_DATAVIDH2) |
11198 			    G_DATAVIDH1(data2)) << 16) | G_VIDL(val);
11199 		} else
11200 			vnix = 0;
11201 
11202 		if (tcamx & tcamy)
11203 			continue;
11204 		tcamxy2valmask(tcamx, tcamy, addr, &mask);
11205 
11206 		mtx_lock(&sc->reg_lock);
11207 		if (hw_off_limits(sc))
11208 			rc = ENXIO;
11209 		else {
11210 			if (chip_rev(sc) == 0) {
11211 				cls_lo = t4_read_reg(sc, MPS_CLS_SRAM_L(i));
11212 				cls_hi = t4_read_reg(sc, MPS_CLS_SRAM_H(i));
11213 			} else {
11214 				t4_write_reg(sc, A_MPS_CLS_SRAM_H,
11215 				    V_SRAMWRN(0) | V_SRAMINDEX(i));
11216 				cls_lo = t4_read_reg(sc, A_MPS_CLS_SRAM_L);
11217 				cls_hi = t4_read_reg(sc, A_MPS_CLS_SRAM_H);
11218 			}
11219 		}
11220 		mtx_unlock(&sc->reg_lock);
11221 		if (rc != 0)
11222 			break;
11223 
11224 		if (lookup_type && lookup_type != M_DATALKPTYPE) {
11225 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11226 			    "%012jx %06x %06x    -    -   %3c"
11227 			    "        I  %4x   %3c   %#x%4u%4d", i, addr[0],
11228 			    addr[1], addr[2], addr[3], addr[4], addr[5],
11229 			    (uintmax_t)mask, vniy, vnix, dip_hit ? 'Y' : 'N',
11230 			    port_num, cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11231 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11232 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11233 		} else {
11234 			sbuf_printf(sb, "\n%3u %02x:%02x:%02x:%02x:%02x:%02x "
11235 			    "%012jx    -       -   ", i, addr[0], addr[1],
11236 			    addr[2], addr[3], addr[4], addr[5],
11237 			    (uintmax_t)mask);
11238 
11239 			if (vlan_vld)
11240 				sbuf_printf(sb, "%4u   Y     ", ivlan);
11241 			else
11242 				sbuf_printf(sb, "  -    N     ");
11243 
11244 			sbuf_printf(sb, "-      %3c  %4x   %3c   %#x%4u%4d",
11245 			    lookup_type ? 'I' : 'O', port_num,
11246 			    cls_lo & F_T6_SRAM_VLD ? 'Y' : 'N',
11247 			    G_PORTMAP(cls_hi), G_T6_PF(cls_lo),
11248 			    cls_lo & F_T6_VF_VALID ? G_T6_VF(cls_lo) : -1);
11249 		}
11250 
11251 		if (cls_lo & F_T6_REPLICATE) {
11252 			struct fw_ldst_cmd ldst_cmd;
11253 
11254 			memset(&ldst_cmd, 0, sizeof(ldst_cmd));
11255 			ldst_cmd.op_to_addrspace =
11256 			    htobe32(V_FW_CMD_OP(FW_LDST_CMD) |
11257 				F_FW_CMD_REQUEST | F_FW_CMD_READ |
11258 				V_FW_LDST_CMD_ADDRSPACE(FW_LDST_ADDRSPC_MPS));
11259 			ldst_cmd.cycles_to_len16 = htobe32(FW_LEN16(ldst_cmd));
11260 			ldst_cmd.u.mps.rplc.fid_idx =
11261 			    htobe16(V_FW_LDST_CMD_FID(FW_LDST_MPS_RPLC) |
11262 				V_FW_LDST_CMD_IDX(i));
11263 
11264 			rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK,
11265 			    "t6mps");
11266 			if (rc)
11267 				break;
11268 			if (hw_off_limits(sc))
11269 				rc = ENXIO;
11270 			else
11271 				rc = -t4_wr_mbox(sc, sc->mbox, &ldst_cmd,
11272 				    sizeof(ldst_cmd), &ldst_cmd);
11273 			end_synchronized_op(sc, 0);
11274 			if (rc != 0)
11275 				break;
11276 			else {
11277 				sbuf_printf(sb, " %08x %08x %08x %08x"
11278 				    " %08x %08x %08x %08x",
11279 				    be32toh(ldst_cmd.u.mps.rplc.rplc255_224),
11280 				    be32toh(ldst_cmd.u.mps.rplc.rplc223_192),
11281 				    be32toh(ldst_cmd.u.mps.rplc.rplc191_160),
11282 				    be32toh(ldst_cmd.u.mps.rplc.rplc159_128),
11283 				    be32toh(ldst_cmd.u.mps.rplc.rplc127_96),
11284 				    be32toh(ldst_cmd.u.mps.rplc.rplc95_64),
11285 				    be32toh(ldst_cmd.u.mps.rplc.rplc63_32),
11286 				    be32toh(ldst_cmd.u.mps.rplc.rplc31_0));
11287 			}
11288 		} else
11289 			sbuf_printf(sb, "%72s", "");
11290 
11291 		sbuf_printf(sb, "%4u%3u%3u%3u %#x",
11292 		    G_T6_SRAM_PRIO0(cls_lo), G_T6_SRAM_PRIO1(cls_lo),
11293 		    G_T6_SRAM_PRIO2(cls_lo), G_T6_SRAM_PRIO3(cls_lo),
11294 		    (cls_lo >> S_T6_MULTILISTEN0) & 0xf);
11295 	}
11296 
11297 	if (rc)
11298 		(void) sbuf_finish(sb);
11299 	else
11300 		rc = sbuf_finish(sb);
11301 	sbuf_delete(sb);
11302 
11303 	return (rc);
11304 }
11305 
11306 static int
sysctl_path_mtus(SYSCTL_HANDLER_ARGS)11307 sysctl_path_mtus(SYSCTL_HANDLER_ARGS)
11308 {
11309 	struct adapter *sc = arg1;
11310 	struct sbuf *sb;
11311 	int rc;
11312 	uint16_t mtus[NMTUS];
11313 
11314 	rc = 0;
11315 	mtx_lock(&sc->reg_lock);
11316 	if (hw_off_limits(sc))
11317 		rc = ENXIO;
11318 	else
11319 		t4_read_mtu_tbl(sc, mtus, NULL);
11320 	mtx_unlock(&sc->reg_lock);
11321 	if (rc != 0)
11322 		return (rc);
11323 
11324 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11325 	if (sb == NULL)
11326 		return (ENOMEM);
11327 
11328 	sbuf_printf(sb, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
11329 	    mtus[0], mtus[1], mtus[2], mtus[3], mtus[4], mtus[5], mtus[6],
11330 	    mtus[7], mtus[8], mtus[9], mtus[10], mtus[11], mtus[12], mtus[13],
11331 	    mtus[14], mtus[15]);
11332 
11333 	rc = sbuf_finish(sb);
11334 	sbuf_delete(sb);
11335 
11336 	return (rc);
11337 }
11338 
11339 static int
sysctl_pm_stats(SYSCTL_HANDLER_ARGS)11340 sysctl_pm_stats(SYSCTL_HANDLER_ARGS)
11341 {
11342 	struct adapter *sc = arg1;
11343 	struct sbuf *sb;
11344 	int rc, i;
11345 	uint32_t tx_cnt[MAX_PM_NSTATS], rx_cnt[MAX_PM_NSTATS];
11346 	uint64_t tx_cyc[MAX_PM_NSTATS], rx_cyc[MAX_PM_NSTATS];
11347 	uint32_t stats[T7_PM_RX_CACHE_NSTATS];
11348 	static const char *tx_stats[MAX_PM_NSTATS] = {
11349 		"Read:", "Write bypass:", "Write mem:", "Bypass + mem:",
11350 		"Tx FIFO wait", NULL, "Tx latency"
11351 	};
11352 	static const char *rx_stats[MAX_PM_NSTATS] = {
11353 		"Read:", "Write bypass:", "Write mem:", "Flush:",
11354 		"Rx FIFO wait", NULL, "Rx latency"
11355 	};
11356 
11357 	rc = 0;
11358 	mtx_lock(&sc->reg_lock);
11359 	if (hw_off_limits(sc))
11360 		rc = ENXIO;
11361 	else {
11362 		t4_pmtx_get_stats(sc, tx_cnt, tx_cyc);
11363 		t4_pmrx_get_stats(sc, rx_cnt, rx_cyc);
11364 		if (chip_id(sc) >= CHELSIO_T7)
11365 			t4_pmrx_cache_get_stats(sc, stats);
11366 	}
11367 	mtx_unlock(&sc->reg_lock);
11368 	if (rc != 0)
11369 		return (rc);
11370 
11371 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11372 	if (sb == NULL)
11373 		return (ENOMEM);
11374 
11375 	sbuf_printf(sb, "                Tx pcmds             Tx bytes");
11376 	for (i = 0; i < 4; i++) {
11377 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11378 		    tx_cyc[i]);
11379 	}
11380 
11381 	sbuf_printf(sb, "\n                Rx pcmds             Rx bytes");
11382 	for (i = 0; i < 4; i++) {
11383 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11384 		    rx_cyc[i]);
11385 	}
11386 
11387 	if (chip_id(sc) > CHELSIO_T5) {
11388 		sbuf_printf(sb,
11389 		    "\n              Total wait      Total occupancy");
11390 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11391 		    tx_cyc[i]);
11392 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11393 		    rx_cyc[i]);
11394 
11395 		i += 2;
11396 		MPASS(i < nitems(tx_stats));
11397 
11398 		sbuf_printf(sb,
11399 		    "\n                   Reads           Total wait");
11400 		sbuf_printf(sb, "\n%-13s %10u %20ju", tx_stats[i], tx_cnt[i],
11401 		    tx_cyc[i]);
11402 		sbuf_printf(sb, "\n%-13s %10u %20ju", rx_stats[i], rx_cnt[i],
11403 		    rx_cyc[i]);
11404 	}
11405 
11406 	if (chip_id(sc) >= CHELSIO_T7) {
11407 		i = 0;
11408 		sbuf_printf(sb, "\n\nPM RX Cache Stats\n");
11409 		sbuf_printf(sb, "%-40s    %u\n", "ReqWrite", stats[i++]);
11410 		sbuf_printf(sb, "%-40s    %u\n", "ReqReadInv", stats[i++]);
11411 		sbuf_printf(sb, "%-40s    %u\n", "ReqReadNoInv", stats[i++]);
11412 		sbuf_printf(sb, "%-40s    %u\n", "Write Split Request",
11413 			   stats[i++]);
11414 		sbuf_printf(sb, "%-40s    %u\n",
11415 			   "Normal Read Split (Read Invalidate)", stats[i++]);
11416 		sbuf_printf(sb, "%-40s    %u\n",
11417 			   "Feedback Read Split (Read NoInvalidate)",
11418 			   stats[i++]);
11419 		sbuf_printf(sb, "%-40s    %u\n", "Write Hit", stats[i++]);
11420 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit",
11421 			   stats[i++]);
11422 		sbuf_printf(sb, "%-40s    %u\n", "Feedback Read Hit",
11423 			   stats[i++]);
11424 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit Full Avail",
11425 			   stats[i++]);
11426 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Hit Full UnAvail",
11427 			   stats[i++]);
11428 		sbuf_printf(sb, "%-40s    %u\n",
11429 			   "Normal Read Hit Partial Avail",
11430 			   stats[i++]);
11431 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Full Avail",
11432 			   stats[i++]);
11433 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Full UnAvail",
11434 			   stats[i++]);
11435 		sbuf_printf(sb, "%-40s    %u\n", "FB Read Hit Partial Avail",
11436 			   stats[i++]);
11437 		sbuf_printf(sb, "%-40s    %u\n", "Normal Read Full Free",
11438 			   stats[i++]);
11439 		sbuf_printf(sb, "%-40s    %u\n",
11440 			   "Normal Read Part-avail Mul-Regions",
11441 			   stats[i++]);
11442 		sbuf_printf(sb, "%-40s    %u\n",
11443 			   "FB Read Part-avail Mul-Regions",
11444 			   stats[i++]);
11445 		sbuf_printf(sb, "%-40s    %u\n", "Write Miss FL Used",
11446 			   stats[i++]);
11447 		sbuf_printf(sb, "%-40s    %u\n", "Write Miss LRU Used",
11448 			   stats[i++]);
11449 		sbuf_printf(sb, "%-40s    %u\n",
11450 			   "Write Miss LRU-Multiple Evict", stats[i++]);
11451 		sbuf_printf(sb, "%-40s    %u\n",
11452 			   "Write Hit Increasing Islands", stats[i++]);
11453 		sbuf_printf(sb, "%-40s    %u\n",
11454 			   "Normal Read Island Read split", stats[i++]);
11455 		sbuf_printf(sb, "%-40s    %u\n", "Write Overflow Eviction",
11456 			   stats[i++]);
11457 		sbuf_printf(sb, "%-40s    %u", "Read Overflow Eviction",
11458 			   stats[i++]);
11459 	}
11460 
11461 	rc = sbuf_finish(sb);
11462 	sbuf_delete(sb);
11463 
11464 	return (rc);
11465 }
11466 
11467 static int
sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)11468 sysctl_rdma_stats(SYSCTL_HANDLER_ARGS)
11469 {
11470 	struct adapter *sc = arg1;
11471 	struct sbuf *sb;
11472 	int rc;
11473 	struct tp_rdma_stats stats;
11474 
11475 	rc = 0;
11476 	mtx_lock(&sc->reg_lock);
11477 	if (hw_off_limits(sc))
11478 		rc = ENXIO;
11479 	else
11480 		t4_tp_get_rdma_stats(sc, &stats, 0);
11481 	mtx_unlock(&sc->reg_lock);
11482 	if (rc != 0)
11483 		return (rc);
11484 
11485 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11486 	if (sb == NULL)
11487 		return (ENOMEM);
11488 
11489 	sbuf_printf(sb, "NoRQEModDefferals: %u\n", stats.rqe_dfr_mod);
11490 	sbuf_printf(sb, "NoRQEPktDefferals: %u", stats.rqe_dfr_pkt);
11491 
11492 	rc = sbuf_finish(sb);
11493 	sbuf_delete(sb);
11494 
11495 	return (rc);
11496 }
11497 
11498 static int
sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)11499 sysctl_tcp_stats(SYSCTL_HANDLER_ARGS)
11500 {
11501 	struct adapter *sc = arg1;
11502 	struct sbuf *sb;
11503 	int rc;
11504 	struct tp_tcp_stats v4, v6;
11505 
11506 	rc = 0;
11507 	mtx_lock(&sc->reg_lock);
11508 	if (hw_off_limits(sc))
11509 		rc = ENXIO;
11510 	else
11511 		t4_tp_get_tcp_stats(sc, &v4, &v6, 0);
11512 	mtx_unlock(&sc->reg_lock);
11513 	if (rc != 0)
11514 		return (rc);
11515 
11516 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11517 	if (sb == NULL)
11518 		return (ENOMEM);
11519 
11520 	sbuf_printf(sb,
11521 	    "                                IP                 IPv6\n");
11522 	sbuf_printf(sb, "OutRsts:      %20u %20u\n",
11523 	    v4.tcp_out_rsts, v6.tcp_out_rsts);
11524 	sbuf_printf(sb, "InSegs:       %20ju %20ju\n",
11525 	    v4.tcp_in_segs, v6.tcp_in_segs);
11526 	sbuf_printf(sb, "OutSegs:      %20ju %20ju\n",
11527 	    v4.tcp_out_segs, v6.tcp_out_segs);
11528 	sbuf_printf(sb, "RetransSegs:  %20ju %20ju",
11529 	    v4.tcp_retrans_segs, v6.tcp_retrans_segs);
11530 
11531 	rc = sbuf_finish(sb);
11532 	sbuf_delete(sb);
11533 
11534 	return (rc);
11535 }
11536 
11537 static int
sysctl_tids(SYSCTL_HANDLER_ARGS)11538 sysctl_tids(SYSCTL_HANDLER_ARGS)
11539 {
11540 	struct adapter *sc = arg1;
11541 	struct sbuf *sb;
11542 	int rc;
11543 	uint32_t x, y;
11544 	struct tid_info *t = &sc->tids;
11545 
11546 	rc = 0;
11547 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11548 	if (sb == NULL)
11549 		return (ENOMEM);
11550 
11551 	if (t->natids) {
11552 		sbuf_printf(sb, "ATID range: 0-%u, in use: %u\n", t->natids - 1,
11553 		    t->atids_in_use);
11554 	}
11555 
11556 	if (t->nhpftids) {
11557 		sbuf_printf(sb, "HPFTID range: %u-%u, in use: %u\n",
11558 		    t->hpftid_base, t->hpftid_end, t->hpftids_in_use);
11559 	}
11560 
11561 	if (t->ntids) {
11562 		bool hashen = false;
11563 
11564 		mtx_lock(&sc->reg_lock);
11565 		if (hw_off_limits(sc))
11566 			rc = ENXIO;
11567 		else if (t4_read_reg(sc, A_LE_DB_CONFIG) & F_HASHEN) {
11568 			hashen = true;
11569 			if (chip_id(sc) <= CHELSIO_T5) {
11570 				x = t4_read_reg(sc, A_LE_DB_SERVER_INDEX) / 4;
11571 				y = t4_read_reg(sc, A_LE_DB_TID_HASHBASE) / 4;
11572 			} else {
11573 				x = t4_read_reg(sc, A_LE_DB_SRVR_START_INDEX);
11574 				y = t4_read_reg(sc, A_T6_LE_DB_HASH_TID_BASE);
11575 			}
11576 		}
11577 		mtx_unlock(&sc->reg_lock);
11578 		if (rc != 0)
11579 			goto done;
11580 
11581 		sbuf_printf(sb, "TID range: ");
11582 		if (hashen) {
11583 			if (x)
11584 				sbuf_printf(sb, "%u-%u, ", t->tid_base, x - 1);
11585 			sbuf_printf(sb, "%u-%u", y, t->ntids - 1);
11586 		} else {
11587 			sbuf_printf(sb, "%u-%u", t->tid_base, t->tid_base +
11588 			    t->ntids - 1);
11589 		}
11590 		sbuf_printf(sb, ", in use: %u\n",
11591 		    atomic_load_acq_int(&t->tids_in_use));
11592 	}
11593 
11594 	if (t->nstids) {
11595 		sbuf_printf(sb, "STID range: %u-%u, in use: %u\n", t->stid_base,
11596 		    t->stid_base + t->nstids - 1, t->stids_in_use);
11597 	}
11598 
11599 	if (t->nftids) {
11600 		sbuf_printf(sb, "FTID range: %u-%u, in use: %u\n", t->ftid_base,
11601 		    t->ftid_end, t->ftids_in_use);
11602 	}
11603 
11604 	if (t->netids) {
11605 		sbuf_printf(sb, "ETID range: %u-%u, in use: %u\n", t->etid_base,
11606 		    t->etid_base + t->netids - 1, t->etids_in_use);
11607 	}
11608 
11609 	mtx_lock(&sc->reg_lock);
11610 	if (hw_off_limits(sc))
11611 		rc = ENXIO;
11612 	else {
11613 		x = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV4);
11614 		y = t4_read_reg(sc, A_LE_DB_ACT_CNT_IPV6);
11615 	}
11616 	mtx_unlock(&sc->reg_lock);
11617 	if (rc != 0)
11618 		goto done;
11619 	sbuf_printf(sb, "HW TID usage: %u IP users, %u IPv6 users", x, y);
11620 done:
11621 	if (rc == 0)
11622 		rc = sbuf_finish(sb);
11623 	else
11624 		(void)sbuf_finish(sb);
11625 	sbuf_delete(sb);
11626 
11627 	return (rc);
11628 }
11629 
11630 static int
sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)11631 sysctl_tp_err_stats(SYSCTL_HANDLER_ARGS)
11632 {
11633 	struct adapter *sc = arg1;
11634 	struct sbuf *sb;
11635 	int rc;
11636 	struct tp_err_stats stats;
11637 
11638 	rc = 0;
11639 	mtx_lock(&sc->reg_lock);
11640 	if (hw_off_limits(sc))
11641 		rc = ENXIO;
11642 	else
11643 		t4_tp_get_err_stats(sc, &stats, 0);
11644 	mtx_unlock(&sc->reg_lock);
11645 	if (rc != 0)
11646 		return (rc);
11647 
11648 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11649 	if (sb == NULL)
11650 		return (ENOMEM);
11651 
11652 	if (sc->chip_params->nchan > 2) {
11653 		sbuf_printf(sb, "                 channel 0  channel 1"
11654 		    "  channel 2  channel 3\n");
11655 		sbuf_printf(sb, "macInErrs:      %10u %10u %10u %10u\n",
11656 		    stats.mac_in_errs[0], stats.mac_in_errs[1],
11657 		    stats.mac_in_errs[2], stats.mac_in_errs[3]);
11658 		sbuf_printf(sb, "hdrInErrs:      %10u %10u %10u %10u\n",
11659 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1],
11660 		    stats.hdr_in_errs[2], stats.hdr_in_errs[3]);
11661 		sbuf_printf(sb, "tcpInErrs:      %10u %10u %10u %10u\n",
11662 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1],
11663 		    stats.tcp_in_errs[2], stats.tcp_in_errs[3]);
11664 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u %10u %10u\n",
11665 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1],
11666 		    stats.tcp6_in_errs[2], stats.tcp6_in_errs[3]);
11667 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u %10u %10u\n",
11668 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1],
11669 		    stats.tnl_cong_drops[2], stats.tnl_cong_drops[3]);
11670 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u %10u %10u\n",
11671 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1],
11672 		    stats.tnl_tx_drops[2], stats.tnl_tx_drops[3]);
11673 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u %10u %10u\n",
11674 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1],
11675 		    stats.ofld_vlan_drops[2], stats.ofld_vlan_drops[3]);
11676 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u %10u %10u\n\n",
11677 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1],
11678 		    stats.ofld_chan_drops[2], stats.ofld_chan_drops[3]);
11679 	} else {
11680 		sbuf_printf(sb, "                 channel 0  channel 1\n");
11681 		sbuf_printf(sb, "macInErrs:      %10u %10u\n",
11682 		    stats.mac_in_errs[0], stats.mac_in_errs[1]);
11683 		sbuf_printf(sb, "hdrInErrs:      %10u %10u\n",
11684 		    stats.hdr_in_errs[0], stats.hdr_in_errs[1]);
11685 		sbuf_printf(sb, "tcpInErrs:      %10u %10u\n",
11686 		    stats.tcp_in_errs[0], stats.tcp_in_errs[1]);
11687 		sbuf_printf(sb, "tcp6InErrs:     %10u %10u\n",
11688 		    stats.tcp6_in_errs[0], stats.tcp6_in_errs[1]);
11689 		sbuf_printf(sb, "tnlCongDrops:   %10u %10u\n",
11690 		    stats.tnl_cong_drops[0], stats.tnl_cong_drops[1]);
11691 		sbuf_printf(sb, "tnlTxDrops:     %10u %10u\n",
11692 		    stats.tnl_tx_drops[0], stats.tnl_tx_drops[1]);
11693 		sbuf_printf(sb, "ofldVlanDrops:  %10u %10u\n",
11694 		    stats.ofld_vlan_drops[0], stats.ofld_vlan_drops[1]);
11695 		sbuf_printf(sb, "ofldChanDrops:  %10u %10u\n\n",
11696 		    stats.ofld_chan_drops[0], stats.ofld_chan_drops[1]);
11697 	}
11698 
11699 	sbuf_printf(sb, "ofldNoNeigh:    %u\nofldCongDefer:  %u",
11700 	    stats.ofld_no_neigh, stats.ofld_cong_defer);
11701 
11702 	rc = sbuf_finish(sb);
11703 	sbuf_delete(sb);
11704 
11705 	return (rc);
11706 }
11707 
11708 static int
sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)11709 sysctl_tnl_stats(SYSCTL_HANDLER_ARGS)
11710 {
11711 	struct adapter *sc = arg1;
11712 	struct sbuf *sb;
11713 	int rc;
11714 	struct tp_tnl_stats stats;
11715 
11716 	rc = 0;
11717 	mtx_lock(&sc->reg_lock);
11718 	if (hw_off_limits(sc))
11719 		rc = ENXIO;
11720 	else
11721 		t4_tp_get_tnl_stats(sc, &stats, 1);
11722 	mtx_unlock(&sc->reg_lock);
11723 	if (rc != 0)
11724 		return (rc);
11725 
11726 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
11727 	if (sb == NULL)
11728 		return (ENOMEM);
11729 
11730 	if (sc->chip_params->nchan > 2) {
11731 		sbuf_printf(sb, "           channel 0  channel 1"
11732 		    "  channel 2  channel 3\n");
11733 		sbuf_printf(sb, "OutPkts:  %10u %10u %10u %10u\n",
11734 		    stats.out_pkt[0], stats.out_pkt[1],
11735 		    stats.out_pkt[2], stats.out_pkt[3]);
11736 		sbuf_printf(sb, "InPkts:   %10u %10u %10u %10u",
11737 		    stats.in_pkt[0], stats.in_pkt[1],
11738 		    stats.in_pkt[2], stats.in_pkt[3]);
11739 	} else {
11740 		sbuf_printf(sb, "           channel 0  channel 1\n");
11741 		sbuf_printf(sb, "OutPkts:  %10u %10u\n",
11742 		    stats.out_pkt[0], stats.out_pkt[1]);
11743 		sbuf_printf(sb, "InPkts:   %10u %10u",
11744 		    stats.in_pkt[0], stats.in_pkt[1]);
11745 	}
11746 
11747 	rc = sbuf_finish(sb);
11748 	sbuf_delete(sb);
11749 
11750 	return (rc);
11751 }
11752 
11753 static int
sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)11754 sysctl_tp_la_mask(SYSCTL_HANDLER_ARGS)
11755 {
11756 	struct adapter *sc = arg1;
11757 	struct tp_params *tpp = &sc->params.tp;
11758 	u_int mask;
11759 	int rc;
11760 
11761 	mask = tpp->la_mask >> 16;
11762 	rc = sysctl_handle_int(oidp, &mask, 0, req);
11763 	if (rc != 0 || req->newptr == NULL)
11764 		return (rc);
11765 	if (mask > 0xffff)
11766 		return (EINVAL);
11767 	mtx_lock(&sc->reg_lock);
11768 	if (hw_off_limits(sc))
11769 		rc = ENXIO;
11770 	else {
11771 		tpp->la_mask = mask << 16;
11772 		t4_set_reg_field(sc, A_TP_DBG_LA_CONFIG, 0xffff0000U,
11773 		    tpp->la_mask);
11774 	}
11775 	mtx_unlock(&sc->reg_lock);
11776 
11777 	return (rc);
11778 }
11779 
11780 struct field_desc {
11781 	const char *name;
11782 	u_int start;
11783 	u_int width;
11784 };
11785 
11786 static void
field_desc_show(struct sbuf * sb,uint64_t v,const struct field_desc * f)11787 field_desc_show(struct sbuf *sb, uint64_t v, const struct field_desc *f)
11788 {
11789 	char buf[32];
11790 	int line_size = 0;
11791 
11792 	while (f->name) {
11793 		uint64_t mask = (1ULL << f->width) - 1;
11794 		int len = snprintf(buf, sizeof(buf), "%s: %ju", f->name,
11795 		    ((uintmax_t)v >> f->start) & mask);
11796 
11797 		if (line_size + len >= 79) {
11798 			line_size = 8;
11799 			sbuf_printf(sb, "\n        ");
11800 		}
11801 		sbuf_printf(sb, "%s ", buf);
11802 		line_size += len + 1;
11803 		f++;
11804 	}
11805 	sbuf_printf(sb, "\n");
11806 }
11807 
11808 static const struct field_desc tp_la0[] = {
11809 	{ "RcfOpCodeOut", 60, 4 },
11810 	{ "State", 56, 4 },
11811 	{ "WcfState", 52, 4 },
11812 	{ "RcfOpcSrcOut", 50, 2 },
11813 	{ "CRxError", 49, 1 },
11814 	{ "ERxError", 48, 1 },
11815 	{ "SanityFailed", 47, 1 },
11816 	{ "SpuriousMsg", 46, 1 },
11817 	{ "FlushInputMsg", 45, 1 },
11818 	{ "FlushInputCpl", 44, 1 },
11819 	{ "RssUpBit", 43, 1 },
11820 	{ "RssFilterHit", 42, 1 },
11821 	{ "Tid", 32, 10 },
11822 	{ "InitTcb", 31, 1 },
11823 	{ "LineNumber", 24, 7 },
11824 	{ "Emsg", 23, 1 },
11825 	{ "EdataOut", 22, 1 },
11826 	{ "Cmsg", 21, 1 },
11827 	{ "CdataOut", 20, 1 },
11828 	{ "EreadPdu", 19, 1 },
11829 	{ "CreadPdu", 18, 1 },
11830 	{ "TunnelPkt", 17, 1 },
11831 	{ "RcfPeerFin", 16, 1 },
11832 	{ "RcfReasonOut", 12, 4 },
11833 	{ "TxCchannel", 10, 2 },
11834 	{ "RcfTxChannel", 8, 2 },
11835 	{ "RxEchannel", 6, 2 },
11836 	{ "RcfRxChannel", 5, 1 },
11837 	{ "RcfDataOutSrdy", 4, 1 },
11838 	{ "RxDvld", 3, 1 },
11839 	{ "RxOoDvld", 2, 1 },
11840 	{ "RxCongestion", 1, 1 },
11841 	{ "TxCongestion", 0, 1 },
11842 	{ NULL }
11843 };
11844 
11845 static const struct field_desc tp_la1[] = {
11846 	{ "CplCmdIn", 56, 8 },
11847 	{ "CplCmdOut", 48, 8 },
11848 	{ "ESynOut", 47, 1 },
11849 	{ "EAckOut", 46, 1 },
11850 	{ "EFinOut", 45, 1 },
11851 	{ "ERstOut", 44, 1 },
11852 	{ "SynIn", 43, 1 },
11853 	{ "AckIn", 42, 1 },
11854 	{ "FinIn", 41, 1 },
11855 	{ "RstIn", 40, 1 },
11856 	{ "DataIn", 39, 1 },
11857 	{ "DataInVld", 38, 1 },
11858 	{ "PadIn", 37, 1 },
11859 	{ "RxBufEmpty", 36, 1 },
11860 	{ "RxDdp", 35, 1 },
11861 	{ "RxFbCongestion", 34, 1 },
11862 	{ "TxFbCongestion", 33, 1 },
11863 	{ "TxPktSumSrdy", 32, 1 },
11864 	{ "RcfUlpType", 28, 4 },
11865 	{ "Eread", 27, 1 },
11866 	{ "Ebypass", 26, 1 },
11867 	{ "Esave", 25, 1 },
11868 	{ "Static0", 24, 1 },
11869 	{ "Cread", 23, 1 },
11870 	{ "Cbypass", 22, 1 },
11871 	{ "Csave", 21, 1 },
11872 	{ "CPktOut", 20, 1 },
11873 	{ "RxPagePoolFull", 18, 2 },
11874 	{ "RxLpbkPkt", 17, 1 },
11875 	{ "TxLpbkPkt", 16, 1 },
11876 	{ "RxVfValid", 15, 1 },
11877 	{ "SynLearned", 14, 1 },
11878 	{ "SetDelEntry", 13, 1 },
11879 	{ "SetInvEntry", 12, 1 },
11880 	{ "CpcmdDvld", 11, 1 },
11881 	{ "CpcmdSave", 10, 1 },
11882 	{ "RxPstructsFull", 8, 2 },
11883 	{ "EpcmdDvld", 7, 1 },
11884 	{ "EpcmdFlush", 6, 1 },
11885 	{ "EpcmdTrimPrefix", 5, 1 },
11886 	{ "EpcmdTrimPostfix", 4, 1 },
11887 	{ "ERssIp4Pkt", 3, 1 },
11888 	{ "ERssIp6Pkt", 2, 1 },
11889 	{ "ERssTcpUdpPkt", 1, 1 },
11890 	{ "ERssFceFipPkt", 0, 1 },
11891 	{ NULL }
11892 };
11893 
11894 static const struct field_desc tp_la2[] = {
11895 	{ "CplCmdIn", 56, 8 },
11896 	{ "MpsVfVld", 55, 1 },
11897 	{ "MpsPf", 52, 3 },
11898 	{ "MpsVf", 44, 8 },
11899 	{ "SynIn", 43, 1 },
11900 	{ "AckIn", 42, 1 },
11901 	{ "FinIn", 41, 1 },
11902 	{ "RstIn", 40, 1 },
11903 	{ "DataIn", 39, 1 },
11904 	{ "DataInVld", 38, 1 },
11905 	{ "PadIn", 37, 1 },
11906 	{ "RxBufEmpty", 36, 1 },
11907 	{ "RxDdp", 35, 1 },
11908 	{ "RxFbCongestion", 34, 1 },
11909 	{ "TxFbCongestion", 33, 1 },
11910 	{ "TxPktSumSrdy", 32, 1 },
11911 	{ "RcfUlpType", 28, 4 },
11912 	{ "Eread", 27, 1 },
11913 	{ "Ebypass", 26, 1 },
11914 	{ "Esave", 25, 1 },
11915 	{ "Static0", 24, 1 },
11916 	{ "Cread", 23, 1 },
11917 	{ "Cbypass", 22, 1 },
11918 	{ "Csave", 21, 1 },
11919 	{ "CPktOut", 20, 1 },
11920 	{ "RxPagePoolFull", 18, 2 },
11921 	{ "RxLpbkPkt", 17, 1 },
11922 	{ "TxLpbkPkt", 16, 1 },
11923 	{ "RxVfValid", 15, 1 },
11924 	{ "SynLearned", 14, 1 },
11925 	{ "SetDelEntry", 13, 1 },
11926 	{ "SetInvEntry", 12, 1 },
11927 	{ "CpcmdDvld", 11, 1 },
11928 	{ "CpcmdSave", 10, 1 },
11929 	{ "RxPstructsFull", 8, 2 },
11930 	{ "EpcmdDvld", 7, 1 },
11931 	{ "EpcmdFlush", 6, 1 },
11932 	{ "EpcmdTrimPrefix", 5, 1 },
11933 	{ "EpcmdTrimPostfix", 4, 1 },
11934 	{ "ERssIp4Pkt", 3, 1 },
11935 	{ "ERssIp6Pkt", 2, 1 },
11936 	{ "ERssTcpUdpPkt", 1, 1 },
11937 	{ "ERssFceFipPkt", 0, 1 },
11938 	{ NULL }
11939 };
11940 
11941 static void
tp_la_show(struct sbuf * sb,uint64_t * p,int idx)11942 tp_la_show(struct sbuf *sb, uint64_t *p, int idx)
11943 {
11944 
11945 	field_desc_show(sb, *p, tp_la0);
11946 }
11947 
11948 static void
tp_la_show2(struct sbuf * sb,uint64_t * p,int idx)11949 tp_la_show2(struct sbuf *sb, uint64_t *p, int idx)
11950 {
11951 
11952 	if (idx)
11953 		sbuf_printf(sb, "\n");
11954 	field_desc_show(sb, p[0], tp_la0);
11955 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11956 		field_desc_show(sb, p[1], tp_la0);
11957 }
11958 
11959 static void
tp_la_show3(struct sbuf * sb,uint64_t * p,int idx)11960 tp_la_show3(struct sbuf *sb, uint64_t *p, int idx)
11961 {
11962 
11963 	if (idx)
11964 		sbuf_printf(sb, "\n");
11965 	field_desc_show(sb, p[0], tp_la0);
11966 	if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
11967 		field_desc_show(sb, p[1], (p[0] & (1 << 17)) ? tp_la2 : tp_la1);
11968 }
11969 
11970 static int
sysctl_tp_la(SYSCTL_HANDLER_ARGS)11971 sysctl_tp_la(SYSCTL_HANDLER_ARGS)
11972 {
11973 	struct adapter *sc = arg1;
11974 	struct sbuf *sb;
11975 	uint64_t *buf, *p;
11976 	int rc;
11977 	u_int i, inc;
11978 	void (*show_func)(struct sbuf *, uint64_t *, int);
11979 
11980 	rc = 0;
11981 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
11982 	if (sb == NULL)
11983 		return (ENOMEM);
11984 
11985 	buf = malloc(TPLA_SIZE * sizeof(uint64_t), M_CXGBE, M_ZERO | M_WAITOK);
11986 
11987 	mtx_lock(&sc->reg_lock);
11988 	if (hw_off_limits(sc))
11989 		rc = ENXIO;
11990 	else {
11991 		t4_tp_read_la(sc, buf, NULL);
11992 		switch (G_DBGLAMODE(t4_read_reg(sc, A_TP_DBG_LA_CONFIG))) {
11993 		case 2:
11994 			inc = 2;
11995 			show_func = tp_la_show2;
11996 			break;
11997 		case 3:
11998 			inc = 2;
11999 			show_func = tp_la_show3;
12000 			break;
12001 		default:
12002 			inc = 1;
12003 			show_func = tp_la_show;
12004 		}
12005 	}
12006 	mtx_unlock(&sc->reg_lock);
12007 	if (rc != 0)
12008 		goto done;
12009 
12010 	p = buf;
12011 	for (i = 0; i < TPLA_SIZE / inc; i++, p += inc)
12012 		(*show_func)(sb, p, i);
12013 	rc = sbuf_finish(sb);
12014 done:
12015 	sbuf_delete(sb);
12016 	free(buf, M_CXGBE);
12017 	return (rc);
12018 }
12019 
12020 static int
sysctl_tx_rate(SYSCTL_HANDLER_ARGS)12021 sysctl_tx_rate(SYSCTL_HANDLER_ARGS)
12022 {
12023 	struct adapter *sc = arg1;
12024 	struct sbuf *sb;
12025 	int rc;
12026 	u64 nrate[MAX_NCHAN], orate[MAX_NCHAN];
12027 
12028 	rc = 0;
12029 	mtx_lock(&sc->reg_lock);
12030 	if (hw_off_limits(sc))
12031 		rc = ENXIO;
12032 	else
12033 		t4_get_chan_txrate(sc, nrate, orate);
12034 	mtx_unlock(&sc->reg_lock);
12035 	if (rc != 0)
12036 		return (rc);
12037 
12038 	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
12039 	if (sb == NULL)
12040 		return (ENOMEM);
12041 
12042 	if (sc->chip_params->nchan > 2) {
12043 		sbuf_printf(sb, "              channel 0   channel 1"
12044 		    "   channel 2   channel 3\n");
12045 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju  %10ju  %10ju\n",
12046 		    nrate[0], nrate[1], nrate[2], nrate[3]);
12047 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju  %10ju  %10ju",
12048 		    orate[0], orate[1], orate[2], orate[3]);
12049 	} else {
12050 		sbuf_printf(sb, "              channel 0   channel 1\n");
12051 		sbuf_printf(sb, "NIC B/s:     %10ju  %10ju\n",
12052 		    nrate[0], nrate[1]);
12053 		sbuf_printf(sb, "Offload B/s: %10ju  %10ju",
12054 		    orate[0], orate[1]);
12055 	}
12056 
12057 	rc = sbuf_finish(sb);
12058 	sbuf_delete(sb);
12059 
12060 	return (rc);
12061 }
12062 
12063 static int
sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)12064 sysctl_ulprx_la(SYSCTL_HANDLER_ARGS)
12065 {
12066 	struct adapter *sc = arg1;
12067 	struct sbuf *sb;
12068 	uint32_t *buf, *p;
12069 	int rc, i;
12070 
12071 	rc = 0;
12072 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12073 	if (sb == NULL)
12074 		return (ENOMEM);
12075 
12076 	buf = malloc(ULPRX_LA_SIZE * 8 * sizeof(uint32_t), M_CXGBE,
12077 	    M_ZERO | M_WAITOK);
12078 
12079 	mtx_lock(&sc->reg_lock);
12080 	if (hw_off_limits(sc))
12081 		rc = ENXIO;
12082 	else
12083 		t4_ulprx_read_la(sc, buf);
12084 	mtx_unlock(&sc->reg_lock);
12085 	if (rc != 0)
12086 		goto done;
12087 
12088 	p = buf;
12089 	sbuf_printf(sb, "      Pcmd        Type   Message"
12090 	    "                Data");
12091 	for (i = 0; i < ULPRX_LA_SIZE; i++, p += 8) {
12092 		sbuf_printf(sb, "\n%08x%08x  %4x  %08x  %08x%08x%08x%08x",
12093 		    p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
12094 	}
12095 	rc = sbuf_finish(sb);
12096 done:
12097 	sbuf_delete(sb);
12098 	free(buf, M_CXGBE);
12099 	return (rc);
12100 }
12101 
12102 static int
sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)12103 sysctl_wcwr_stats(SYSCTL_HANDLER_ARGS)
12104 {
12105 	struct adapter *sc = arg1;
12106 	struct sbuf *sb;
12107 	int rc;
12108 	uint32_t cfg, s1, s2;
12109 
12110 	MPASS(chip_id(sc) >= CHELSIO_T5);
12111 
12112 	rc = 0;
12113 	mtx_lock(&sc->reg_lock);
12114 	if (hw_off_limits(sc))
12115 		rc = ENXIO;
12116 	else {
12117 		cfg = t4_read_reg(sc, A_SGE_STAT_CFG);
12118 		s1 = t4_read_reg(sc, A_SGE_STAT_TOTAL);
12119 		s2 = t4_read_reg(sc, A_SGE_STAT_MATCH);
12120 	}
12121 	mtx_unlock(&sc->reg_lock);
12122 	if (rc != 0)
12123 		return (rc);
12124 
12125 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12126 	if (sb == NULL)
12127 		return (ENOMEM);
12128 
12129 	if (G_STATSOURCE_T5(cfg) == 7) {
12130 		int mode;
12131 
12132 		mode = is_t5(sc) ? G_STATMODE(cfg) : G_T6_STATMODE(cfg);
12133 		if (mode == 0)
12134 			sbuf_printf(sb, "total %d, incomplete %d", s1, s2);
12135 		else if (mode == 1)
12136 			sbuf_printf(sb, "total %d, data overflow %d", s1, s2);
12137 		else
12138 			sbuf_printf(sb, "unknown mode %d", mode);
12139 	}
12140 	rc = sbuf_finish(sb);
12141 	sbuf_delete(sb);
12142 
12143 	return (rc);
12144 }
12145 
12146 static int
sysctl_cpus(SYSCTL_HANDLER_ARGS)12147 sysctl_cpus(SYSCTL_HANDLER_ARGS)
12148 {
12149 	struct adapter *sc = arg1;
12150 	enum cpu_sets op = arg2;
12151 	cpuset_t cpuset;
12152 	struct sbuf *sb;
12153 	int i, rc;
12154 
12155 	MPASS(op == LOCAL_CPUS || op == INTR_CPUS);
12156 
12157 	CPU_ZERO(&cpuset);
12158 	rc = bus_get_cpus(sc->dev, op, sizeof(cpuset), &cpuset);
12159 	if (rc != 0)
12160 		return (rc);
12161 
12162 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
12163 	if (sb == NULL)
12164 		return (ENOMEM);
12165 
12166 	CPU_FOREACH(i)
12167 		sbuf_printf(sb, "%d ", i);
12168 	rc = sbuf_finish(sb);
12169 	sbuf_delete(sb);
12170 
12171 	return (rc);
12172 }
12173 
12174 static int
sysctl_reset(SYSCTL_HANDLER_ARGS)12175 sysctl_reset(SYSCTL_HANDLER_ARGS)
12176 {
12177 	struct adapter *sc = arg1;
12178 	u_int val;
12179 	int rc;
12180 
12181 	val = atomic_load_int(&sc->num_resets);
12182 	rc = sysctl_handle_int(oidp, &val, 0, req);
12183 	if (rc != 0 || req->newptr == NULL)
12184 		return (rc);
12185 
12186 	if (val == 0) {
12187 		/* Zero out the counter that tracks reset. */
12188 		atomic_store_int(&sc->num_resets, 0);
12189 		return (0);
12190 	}
12191 
12192 	if (val != 1)
12193 		return (EINVAL);	/* 0 or 1 are the only legal values */
12194 
12195 	if (hw_off_limits(sc))		/* harmless race */
12196 		return (EALREADY);
12197 
12198 	taskqueue_enqueue(reset_tq, &sc->reset_task);
12199 	return (0);
12200 }
12201 
12202 #ifdef TCP_OFFLOAD
12203 static int
sysctl_tls(SYSCTL_HANDLER_ARGS)12204 sysctl_tls(SYSCTL_HANDLER_ARGS)
12205 {
12206 	struct adapter *sc = arg1;
12207 	int i, j, v, rc;
12208 	struct vi_info *vi;
12209 
12210 	v = sc->tt.tls;
12211 	rc = sysctl_handle_int(oidp, &v, 0, req);
12212 	if (rc != 0 || req->newptr == NULL)
12213 		return (rc);
12214 
12215 	if (v != 0 && !(sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS))
12216 		return (ENOTSUP);
12217 
12218 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4stls");
12219 	if (rc)
12220 		return (rc);
12221 	if (hw_off_limits(sc))
12222 		rc = ENXIO;
12223 	else {
12224 		sc->tt.tls = !!v;
12225 		for_each_port(sc, i) {
12226 			for_each_vi(sc->port[i], j, vi) {
12227 				if (vi->flags & VI_INIT_DONE)
12228 					t4_update_fl_bufsize(vi->ifp);
12229 			}
12230 		}
12231 	}
12232 	end_synchronized_op(sc, 0);
12233 
12234 	return (rc);
12235 
12236 }
12237 
12238 static void
unit_conv(char * buf,size_t len,u_int val,u_int factor)12239 unit_conv(char *buf, size_t len, u_int val, u_int factor)
12240 {
12241 	u_int rem = val % factor;
12242 
12243 	if (rem == 0)
12244 		snprintf(buf, len, "%u", val / factor);
12245 	else {
12246 		while (rem % 10 == 0)
12247 			rem /= 10;
12248 		snprintf(buf, len, "%u.%u", val / factor, rem);
12249 	}
12250 }
12251 
12252 static int
sysctl_tp_tick(SYSCTL_HANDLER_ARGS)12253 sysctl_tp_tick(SYSCTL_HANDLER_ARGS)
12254 {
12255 	struct adapter *sc = arg1;
12256 	char buf[16];
12257 	u_int res, re;
12258 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12259 
12260 	mtx_lock(&sc->reg_lock);
12261 	if (hw_off_limits(sc))
12262 		res = (u_int)-1;
12263 	else
12264 		res = t4_read_reg(sc, A_TP_TIMER_RESOLUTION);
12265 	mtx_unlock(&sc->reg_lock);
12266 	if (res == (u_int)-1)
12267 		return (ENXIO);
12268 
12269 	switch (arg2) {
12270 	case 0:
12271 		/* timer_tick */
12272 		re = G_TIMERRESOLUTION(res);
12273 		break;
12274 	case 1:
12275 		/* TCP timestamp tick */
12276 		re = G_TIMESTAMPRESOLUTION(res);
12277 		break;
12278 	case 2:
12279 		/* DACK tick */
12280 		re = G_DELAYEDACKRESOLUTION(res);
12281 		break;
12282 	default:
12283 		return (EDOOFUS);
12284 	}
12285 
12286 	unit_conv(buf, sizeof(buf), (cclk_ps << re), 1000000);
12287 
12288 	return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
12289 }
12290 
12291 static int
sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)12292 sysctl_tp_dack_timer(SYSCTL_HANDLER_ARGS)
12293 {
12294 	struct adapter *sc = arg1;
12295 	int rc;
12296 	u_int dack_tmr, dack_re, v;
12297 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12298 
12299 	mtx_lock(&sc->reg_lock);
12300 	if (hw_off_limits(sc))
12301 		rc = ENXIO;
12302 	else {
12303 		rc = 0;
12304 		dack_re = G_DELAYEDACKRESOLUTION(t4_read_reg(sc,
12305 		    A_TP_TIMER_RESOLUTION));
12306 		dack_tmr = t4_read_reg(sc, A_TP_DACK_TIMER);
12307 	}
12308 	mtx_unlock(&sc->reg_lock);
12309 	if (rc != 0)
12310 		return (rc);
12311 
12312 	v = ((cclk_ps << dack_re) / 1000000) * dack_tmr;
12313 
12314 	return (sysctl_handle_int(oidp, &v, 0, req));
12315 }
12316 
12317 static int
sysctl_tp_timer(SYSCTL_HANDLER_ARGS)12318 sysctl_tp_timer(SYSCTL_HANDLER_ARGS)
12319 {
12320 	struct adapter *sc = arg1;
12321 	int rc, reg = arg2;
12322 	u_int tre;
12323 	u_long tp_tick_us, v;
12324 	u_int cclk_ps = 1000000000 / sc->params.vpd.cclk;
12325 
12326 	MPASS(reg == A_TP_RXT_MIN || reg == A_TP_RXT_MAX ||
12327 	    reg == A_TP_PERS_MIN  || reg == A_TP_PERS_MAX ||
12328 	    reg == A_TP_KEEP_IDLE || reg == A_TP_KEEP_INTVL ||
12329 	    reg == A_TP_INIT_SRTT || reg == A_TP_FINWAIT2_TIMER);
12330 
12331 	mtx_lock(&sc->reg_lock);
12332 	if (hw_off_limits(sc))
12333 		rc = ENXIO;
12334 	else {
12335 		rc = 0;
12336 		tre = G_TIMERRESOLUTION(t4_read_reg(sc, A_TP_TIMER_RESOLUTION));
12337 		tp_tick_us = (cclk_ps << tre) / 1000000;
12338 		if (reg == A_TP_INIT_SRTT)
12339 			v = tp_tick_us * G_INITSRTT(t4_read_reg(sc, reg));
12340 		else
12341 			v = tp_tick_us * t4_read_reg(sc, reg);
12342 	}
12343 	mtx_unlock(&sc->reg_lock);
12344 	if (rc != 0)
12345 		return (rc);
12346 	else
12347 		return (sysctl_handle_long(oidp, &v, 0, req));
12348 }
12349 
12350 /*
12351  * All fields in TP_SHIFT_CNT are 4b and the starting location of the field is
12352  * passed to this function.
12353  */
12354 static int
sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)12355 sysctl_tp_shift_cnt(SYSCTL_HANDLER_ARGS)
12356 {
12357 	struct adapter *sc = arg1;
12358 	int rc, idx = arg2;
12359 	u_int v;
12360 
12361 	MPASS(idx >= 0 && idx <= 24);
12362 
12363 	mtx_lock(&sc->reg_lock);
12364 	if (hw_off_limits(sc))
12365 		rc = ENXIO;
12366 	else {
12367 		rc = 0;
12368 		v = (t4_read_reg(sc, A_TP_SHIFT_CNT) >> idx) & 0xf;
12369 	}
12370 	mtx_unlock(&sc->reg_lock);
12371 	if (rc != 0)
12372 		return (rc);
12373 	else
12374 		return (sysctl_handle_int(oidp, &v, 0, req));
12375 }
12376 
12377 static int
sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)12378 sysctl_tp_backoff(SYSCTL_HANDLER_ARGS)
12379 {
12380 	struct adapter *sc = arg1;
12381 	int rc, idx = arg2;
12382 	u_int shift, v, r;
12383 
12384 	MPASS(idx >= 0 && idx < 16);
12385 
12386 	r = A_TP_TCP_BACKOFF_REG0 + (idx & ~3);
12387 	shift = (idx & 3) << 3;
12388 	mtx_lock(&sc->reg_lock);
12389 	if (hw_off_limits(sc))
12390 		rc = ENXIO;
12391 	else {
12392 		rc = 0;
12393 		v = (t4_read_reg(sc, r) >> shift) & M_TIMERBACKOFFINDEX0;
12394 	}
12395 	mtx_unlock(&sc->reg_lock);
12396 	if (rc != 0)
12397 		return (rc);
12398 	else
12399 		return (sysctl_handle_int(oidp, &v, 0, req));
12400 }
12401 
12402 static int
sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)12403 sysctl_holdoff_tmr_idx_ofld(SYSCTL_HANDLER_ARGS)
12404 {
12405 	struct vi_info *vi = arg1;
12406 	struct adapter *sc = vi->adapter;
12407 	int idx, rc, i;
12408 	struct sge_ofld_rxq *ofld_rxq;
12409 	uint8_t v;
12410 
12411 	idx = vi->ofld_tmr_idx;
12412 
12413 	rc = sysctl_handle_int(oidp, &idx, 0, req);
12414 	if (rc != 0 || req->newptr == NULL)
12415 		return (rc);
12416 
12417 	if (idx < 0 || idx >= SGE_NTIMERS)
12418 		return (EINVAL);
12419 
12420 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12421 	    "t4otmr");
12422 	if (rc)
12423 		return (rc);
12424 
12425 	v = V_QINTR_TIMER_IDX(idx) | V_QINTR_CNT_EN(vi->ofld_pktc_idx != -1);
12426 	for_each_ofld_rxq(vi, i, ofld_rxq) {
12427 #ifdef atomic_store_rel_8
12428 		atomic_store_rel_8(&ofld_rxq->iq.intr_params, v);
12429 #else
12430 		ofld_rxq->iq.intr_params = v;
12431 #endif
12432 	}
12433 	vi->ofld_tmr_idx = idx;
12434 
12435 	end_synchronized_op(sc, LOCK_HELD);
12436 	return (0);
12437 }
12438 
12439 static int
sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)12440 sysctl_holdoff_pktc_idx_ofld(SYSCTL_HANDLER_ARGS)
12441 {
12442 	struct vi_info *vi = arg1;
12443 	struct adapter *sc = vi->adapter;
12444 	int idx, rc;
12445 
12446 	idx = vi->ofld_pktc_idx;
12447 
12448 	rc = sysctl_handle_int(oidp, &idx, 0, req);
12449 	if (rc != 0 || req->newptr == NULL)
12450 		return (rc);
12451 
12452 	if (idx < -1 || idx >= SGE_NCOUNTERS)
12453 		return (EINVAL);
12454 
12455 	rc = begin_synchronized_op(sc, vi, HOLD_LOCK | SLEEP_OK | INTR_OK,
12456 	    "t4opktc");
12457 	if (rc)
12458 		return (rc);
12459 
12460 	if (vi->flags & VI_INIT_DONE)
12461 		rc = EBUSY; /* cannot be changed once the queues are created */
12462 	else
12463 		vi->ofld_pktc_idx = idx;
12464 
12465 	end_synchronized_op(sc, LOCK_HELD);
12466 	return (rc);
12467 }
12468 #endif
12469 
12470 static int
get_sge_context(struct adapter * sc,int mem_id,uint32_t cid,int len,uint32_t * data)12471 get_sge_context(struct adapter *sc, int mem_id, uint32_t cid, int len,
12472     uint32_t *data)
12473 {
12474 	int rc;
12475 
12476 	if (len < sc->chip_params->sge_ctxt_size)
12477 		return (ENOBUFS);
12478 	if (cid > M_CTXTQID)
12479 		return (EINVAL);
12480 	if (mem_id != CTXT_EGRESS && mem_id != CTXT_INGRESS &&
12481 	    mem_id != CTXT_FLM && mem_id != CTXT_CNM)
12482 		return (EINVAL);
12483 
12484 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ctxt");
12485 	if (rc)
12486 		return (rc);
12487 
12488 	if (hw_off_limits(sc)) {
12489 		rc = ENXIO;
12490 		goto done;
12491 	}
12492 
12493 	if (sc->flags & FW_OK) {
12494 		rc = -t4_sge_ctxt_rd(sc, sc->mbox, cid, mem_id, data);
12495 		if (rc == 0)
12496 			goto done;
12497 	}
12498 
12499 	/*
12500 	 * Read via firmware failed or wasn't even attempted.  Read directly via
12501 	 * the backdoor.
12502 	 */
12503 	rc = -t4_sge_ctxt_rd_bd(sc, cid, mem_id, data);
12504 done:
12505 	end_synchronized_op(sc, 0);
12506 	return (rc);
12507 }
12508 
12509 static int
load_fw(struct adapter * sc,struct t4_data * fw)12510 load_fw(struct adapter *sc, struct t4_data *fw)
12511 {
12512 	int rc;
12513 	uint8_t *fw_data;
12514 
12515 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldfw");
12516 	if (rc)
12517 		return (rc);
12518 
12519 	if (hw_off_limits(sc)) {
12520 		rc = ENXIO;
12521 		goto done;
12522 	}
12523 
12524 	/*
12525 	 * The firmware, with the sole exception of the memory parity error
12526 	 * handler, runs from memory and not flash.  It is almost always safe to
12527 	 * install a new firmware on a running system.  Just set bit 1 in
12528 	 * hw.cxgbe.dflags or dev.<nexus>.<n>.dflags first.
12529 	 */
12530 	if (sc->flags & FULL_INIT_DONE &&
12531 	    (sc->debug_flags & DF_LOAD_FW_ANYTIME) == 0) {
12532 		rc = EBUSY;
12533 		goto done;
12534 	}
12535 
12536 	fw_data = malloc(fw->len, M_CXGBE, M_WAITOK);
12537 
12538 	rc = copyin(fw->data, fw_data, fw->len);
12539 	if (rc == 0)
12540 		rc = -t4_load_fw(sc, fw_data, fw->len);
12541 
12542 	free(fw_data, M_CXGBE);
12543 done:
12544 	end_synchronized_op(sc, 0);
12545 	return (rc);
12546 }
12547 
12548 static int
load_cfg(struct adapter * sc,struct t4_data * cfg)12549 load_cfg(struct adapter *sc, struct t4_data *cfg)
12550 {
12551 	int rc;
12552 	uint8_t *cfg_data = NULL;
12553 
12554 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12555 	if (rc)
12556 		return (rc);
12557 
12558 	if (hw_off_limits(sc)) {
12559 		rc = ENXIO;
12560 		goto done;
12561 	}
12562 
12563 	if (cfg->len == 0) {
12564 		/* clear */
12565 		rc = -t4_load_cfg(sc, NULL, 0);
12566 		goto done;
12567 	}
12568 
12569 	cfg_data = malloc(cfg->len, M_CXGBE, M_WAITOK);
12570 
12571 	rc = copyin(cfg->data, cfg_data, cfg->len);
12572 	if (rc == 0)
12573 		rc = -t4_load_cfg(sc, cfg_data, cfg->len);
12574 
12575 	free(cfg_data, M_CXGBE);
12576 done:
12577 	end_synchronized_op(sc, 0);
12578 	return (rc);
12579 }
12580 
12581 static int
load_boot(struct adapter * sc,struct t4_bootrom * br)12582 load_boot(struct adapter *sc, struct t4_bootrom *br)
12583 {
12584 	int rc;
12585 	uint8_t *br_data = NULL;
12586 	u_int offset;
12587 
12588 	if (br->len > 1024 * 1024)
12589 		return (EFBIG);
12590 
12591 	if (br->pf_offset == 0) {
12592 		/* pfidx */
12593 		if (br->pfidx_addr > 7)
12594 			return (EINVAL);
12595 		offset = G_OFFSET(t4_read_reg(sc, PF_REG(br->pfidx_addr,
12596 		    A_PCIE_PF_EXPROM_OFST)));
12597 	} else if (br->pf_offset == 1) {
12598 		/* offset */
12599 		offset = G_OFFSET(br->pfidx_addr);
12600 	} else {
12601 		return (EINVAL);
12602 	}
12603 
12604 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldbr");
12605 	if (rc)
12606 		return (rc);
12607 
12608 	if (hw_off_limits(sc)) {
12609 		rc = ENXIO;
12610 		goto done;
12611 	}
12612 
12613 	if (br->len == 0) {
12614 		/* clear */
12615 		rc = -t4_load_boot(sc, NULL, offset, 0);
12616 		goto done;
12617 	}
12618 
12619 	br_data = malloc(br->len, M_CXGBE, M_WAITOK);
12620 
12621 	rc = copyin(br->data, br_data, br->len);
12622 	if (rc == 0)
12623 		rc = -t4_load_boot(sc, br_data, offset, br->len);
12624 
12625 	free(br_data, M_CXGBE);
12626 done:
12627 	end_synchronized_op(sc, 0);
12628 	return (rc);
12629 }
12630 
12631 static int
load_bootcfg(struct adapter * sc,struct t4_data * bc)12632 load_bootcfg(struct adapter *sc, struct t4_data *bc)
12633 {
12634 	int rc;
12635 	uint8_t *bc_data = NULL;
12636 
12637 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4ldcf");
12638 	if (rc)
12639 		return (rc);
12640 
12641 	if (hw_off_limits(sc)) {
12642 		rc = ENXIO;
12643 		goto done;
12644 	}
12645 
12646 	if (bc->len == 0) {
12647 		/* clear */
12648 		rc = -t4_load_bootcfg(sc, NULL, 0);
12649 		goto done;
12650 	}
12651 
12652 	bc_data = malloc(bc->len, M_CXGBE, M_WAITOK);
12653 
12654 	rc = copyin(bc->data, bc_data, bc->len);
12655 	if (rc == 0)
12656 		rc = -t4_load_bootcfg(sc, bc_data, bc->len);
12657 
12658 	free(bc_data, M_CXGBE);
12659 done:
12660 	end_synchronized_op(sc, 0);
12661 	return (rc);
12662 }
12663 
12664 static int
cudbg_dump(struct adapter * sc,struct t4_cudbg_dump * dump)12665 cudbg_dump(struct adapter *sc, struct t4_cudbg_dump *dump)
12666 {
12667 	int rc;
12668 	struct cudbg_init *cudbg;
12669 	void *handle, *buf;
12670 
12671 	/* buf is large, don't block if no memory is available */
12672 	buf = malloc(dump->len, M_CXGBE, M_NOWAIT | M_ZERO);
12673 	if (buf == NULL)
12674 		return (ENOMEM);
12675 
12676 	handle = cudbg_alloc_handle();
12677 	if (handle == NULL) {
12678 		rc = ENOMEM;
12679 		goto done;
12680 	}
12681 
12682 	cudbg = cudbg_get_init(handle);
12683 	cudbg->adap = sc;
12684 	cudbg->print = (cudbg_print_cb)printf;
12685 
12686 #ifndef notyet
12687 	device_printf(sc->dev, "%s: wr_flash %u, len %u, data %p.\n",
12688 	    __func__, dump->wr_flash, dump->len, dump->data);
12689 #endif
12690 
12691 	if (dump->wr_flash)
12692 		cudbg->use_flash = 1;
12693 	MPASS(sizeof(cudbg->dbg_bitmap) == sizeof(dump->bitmap));
12694 	memcpy(cudbg->dbg_bitmap, dump->bitmap, sizeof(cudbg->dbg_bitmap));
12695 
12696 	rc = cudbg_collect(handle, buf, &dump->len);
12697 	if (rc != 0)
12698 		goto done;
12699 
12700 	rc = copyout(buf, dump->data, dump->len);
12701 done:
12702 	cudbg_free_handle(handle);
12703 	free(buf, M_CXGBE);
12704 	return (rc);
12705 }
12706 
12707 static void
free_offload_policy(struct t4_offload_policy * op)12708 free_offload_policy(struct t4_offload_policy *op)
12709 {
12710 	struct offload_rule *r;
12711 	int i;
12712 
12713 	if (op == NULL)
12714 		return;
12715 
12716 	r = &op->rule[0];
12717 	for (i = 0; i < op->nrules; i++, r++) {
12718 		free(r->bpf_prog.bf_insns, M_CXGBE);
12719 	}
12720 	free(op->rule, M_CXGBE);
12721 	free(op, M_CXGBE);
12722 }
12723 
12724 static int
set_offload_policy(struct adapter * sc,struct t4_offload_policy * uop)12725 set_offload_policy(struct adapter *sc, struct t4_offload_policy *uop)
12726 {
12727 	int i, rc, len;
12728 	struct t4_offload_policy *op, *old;
12729 	struct bpf_program *bf;
12730 	const struct offload_settings *s;
12731 	struct offload_rule *r;
12732 	void *u;
12733 
12734 	if (!is_offload(sc))
12735 		return (ENODEV);
12736 
12737 	if (uop->nrules == 0) {
12738 		/* Delete installed policies. */
12739 		op = NULL;
12740 		goto set_policy;
12741 	} else if (uop->nrules > 256) { /* arbitrary */
12742 		return (E2BIG);
12743 	}
12744 
12745 	/* Copy userspace offload policy to kernel */
12746 	op = malloc(sizeof(*op), M_CXGBE, M_ZERO | M_WAITOK);
12747 	op->nrules = uop->nrules;
12748 	len = op->nrules * sizeof(struct offload_rule);
12749 	op->rule = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12750 	rc = copyin(uop->rule, op->rule, len);
12751 	if (rc) {
12752 		free(op->rule, M_CXGBE);
12753 		free(op, M_CXGBE);
12754 		return (rc);
12755 	}
12756 
12757 	r = &op->rule[0];
12758 	for (i = 0; i < op->nrules; i++, r++) {
12759 
12760 		/* Validate open_type */
12761 		if (r->open_type != OPEN_TYPE_LISTEN &&
12762 		    r->open_type != OPEN_TYPE_ACTIVE &&
12763 		    r->open_type != OPEN_TYPE_PASSIVE &&
12764 		    r->open_type != OPEN_TYPE_DONTCARE) {
12765 error:
12766 			/*
12767 			 * Rules 0 to i have malloc'd filters that need to be
12768 			 * freed.  Rules i+1 to nrules have userspace pointers
12769 			 * and should be left alone.
12770 			 */
12771 			op->nrules = i;
12772 			free_offload_policy(op);
12773 			return (rc);
12774 		}
12775 
12776 		/* Validate settings */
12777 		s = &r->settings;
12778 		if ((s->offload != 0 && s->offload != 1) ||
12779 		    s->cong_algo < -1 || s->cong_algo > CONG_ALG_HIGHSPEED ||
12780 		    s->sched_class < -1 ||
12781 		    s->sched_class >= sc->params.nsched_cls) {
12782 			rc = EINVAL;
12783 			goto error;
12784 		}
12785 
12786 		bf = &r->bpf_prog;
12787 		u = bf->bf_insns;	/* userspace ptr */
12788 		bf->bf_insns = NULL;
12789 		if (bf->bf_len == 0) {
12790 			/* legal, matches everything */
12791 			continue;
12792 		}
12793 		len = bf->bf_len * sizeof(*bf->bf_insns);
12794 		bf->bf_insns = malloc(len, M_CXGBE, M_ZERO | M_WAITOK);
12795 		rc = copyin(u, bf->bf_insns, len);
12796 		if (rc != 0)
12797 			goto error;
12798 
12799 		if (!bpf_validate(bf->bf_insns, bf->bf_len)) {
12800 			rc = EINVAL;
12801 			goto error;
12802 		}
12803 	}
12804 set_policy:
12805 	rw_wlock(&sc->policy_lock);
12806 	old = sc->policy;
12807 	sc->policy = op;
12808 	rw_wunlock(&sc->policy_lock);
12809 	free_offload_policy(old);
12810 
12811 	return (0);
12812 }
12813 
12814 #define MAX_READ_BUF_SIZE (128 * 1024)
12815 static int
read_card_mem(struct adapter * sc,int win,struct t4_mem_range * mr)12816 read_card_mem(struct adapter *sc, int win, struct t4_mem_range *mr)
12817 {
12818 	uint32_t addr, remaining, n;
12819 	uint32_t *buf;
12820 	int rc;
12821 	uint8_t *dst;
12822 
12823 	mtx_lock(&sc->reg_lock);
12824 	if (hw_off_limits(sc))
12825 		rc = ENXIO;
12826 	else
12827 		rc = validate_mem_range(sc, mr->addr, mr->len);
12828 	mtx_unlock(&sc->reg_lock);
12829 	if (rc != 0)
12830 		return (rc);
12831 
12832 	buf = malloc(min(mr->len, MAX_READ_BUF_SIZE), M_CXGBE, M_WAITOK);
12833 	addr = mr->addr;
12834 	remaining = mr->len;
12835 	dst = (void *)mr->data;
12836 
12837 	while (remaining) {
12838 		n = min(remaining, MAX_READ_BUF_SIZE);
12839 		mtx_lock(&sc->reg_lock);
12840 		if (hw_off_limits(sc))
12841 			rc = ENXIO;
12842 		else
12843 			read_via_memwin(sc, 2, addr, buf, n);
12844 		mtx_unlock(&sc->reg_lock);
12845 		if (rc != 0)
12846 			break;
12847 
12848 		rc = copyout(buf, dst, n);
12849 		if (rc != 0)
12850 			break;
12851 
12852 		dst += n;
12853 		remaining -= n;
12854 		addr += n;
12855 	}
12856 
12857 	free(buf, M_CXGBE);
12858 	return (rc);
12859 }
12860 #undef MAX_READ_BUF_SIZE
12861 
12862 static int
read_i2c(struct adapter * sc,struct t4_i2c_data * i2cd)12863 read_i2c(struct adapter *sc, struct t4_i2c_data *i2cd)
12864 {
12865 	int rc;
12866 
12867 	if (i2cd->len == 0 || i2cd->port_id >= sc->params.nports)
12868 		return (EINVAL);
12869 
12870 	if (i2cd->len > sizeof(i2cd->data))
12871 		return (EFBIG);
12872 
12873 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4i2crd");
12874 	if (rc)
12875 		return (rc);
12876 	if (hw_off_limits(sc))
12877 		rc = ENXIO;
12878 	else
12879 		rc = -t4_i2c_rd(sc, sc->mbox, i2cd->port_id, i2cd->dev_addr,
12880 		    i2cd->offset, i2cd->len, &i2cd->data[0]);
12881 	end_synchronized_op(sc, 0);
12882 
12883 	return (rc);
12884 }
12885 
12886 static int
clear_stats(struct adapter * sc,u_int port_id)12887 clear_stats(struct adapter *sc, u_int port_id)
12888 {
12889 	int i, v, chan_map;
12890 	struct port_info *pi;
12891 	struct vi_info *vi;
12892 	struct sge_rxq *rxq;
12893 	struct sge_txq *txq;
12894 	struct sge_wrq *wrq;
12895 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12896 	struct sge_ofld_txq *ofld_txq;
12897 #endif
12898 #ifdef TCP_OFFLOAD
12899 	struct sge_ofld_rxq *ofld_rxq;
12900 #endif
12901 
12902 	if (port_id >= sc->params.nports)
12903 		return (EINVAL);
12904 	pi = sc->port[port_id];
12905 	if (pi == NULL)
12906 		return (EIO);
12907 
12908 	mtx_lock(&sc->reg_lock);
12909 	if (!hw_off_limits(sc)) {
12910 		/* MAC stats */
12911 		t4_clr_port_stats(sc, pi->hw_port);
12912 		if (is_t6(sc)) {
12913 			if (pi->fcs_reg != -1)
12914 				pi->fcs_base = t4_read_reg64(sc,
12915 				    t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
12916 			else
12917 				pi->stats.rx_fcs_err = 0;
12918 		}
12919 		for_each_vi(pi, v, vi) {
12920 			if (vi->flags & VI_INIT_DONE)
12921 				t4_clr_vi_stats(sc, vi->vin);
12922 		}
12923 		chan_map = pi->rx_e_chan_map;
12924 		v = 0;	/* reuse */
12925 		while (chan_map) {
12926 			i = ffs(chan_map) - 1;
12927 			t4_write_indirect(sc, A_TP_MIB_INDEX, A_TP_MIB_DATA, &v,
12928 			    1, A_TP_MIB_TNL_CNG_DROP_0 + i);
12929 			chan_map &= ~(1 << i);
12930 		}
12931 	}
12932 	mtx_unlock(&sc->reg_lock);
12933 	pi->tx_parse_error = 0;
12934 	pi->tnl_cong_drops = 0;
12935 
12936 	/*
12937 	 * Since this command accepts a port, clear stats for
12938 	 * all VIs on this port.
12939 	 */
12940 	for_each_vi(pi, v, vi) {
12941 		if (vi->flags & VI_INIT_DONE) {
12942 
12943 			for_each_rxq(vi, i, rxq) {
12944 #if defined(INET) || defined(INET6)
12945 				rxq->lro.lro_queued = 0;
12946 				rxq->lro.lro_flushed = 0;
12947 #endif
12948 				rxq->rxcsum = 0;
12949 				rxq->vlan_extraction = 0;
12950 				rxq->vxlan_rxcsum = 0;
12951 
12952 				rxq->fl.cl_allocated = 0;
12953 				rxq->fl.cl_recycled = 0;
12954 				rxq->fl.cl_fast_recycled = 0;
12955 			}
12956 
12957 			for_each_txq(vi, i, txq) {
12958 				txq->txcsum = 0;
12959 				txq->tso_wrs = 0;
12960 				txq->vlan_insertion = 0;
12961 				txq->imm_wrs = 0;
12962 				txq->sgl_wrs = 0;
12963 				txq->txpkt_wrs = 0;
12964 				txq->txpkts0_wrs = 0;
12965 				txq->txpkts1_wrs = 0;
12966 				txq->txpkts0_pkts = 0;
12967 				txq->txpkts1_pkts = 0;
12968 				txq->txpkts_flush = 0;
12969 				txq->raw_wrs = 0;
12970 				txq->vxlan_tso_wrs = 0;
12971 				txq->vxlan_txcsum = 0;
12972 				txq->kern_tls_records = 0;
12973 				txq->kern_tls_short = 0;
12974 				txq->kern_tls_partial = 0;
12975 				txq->kern_tls_full = 0;
12976 				txq->kern_tls_octets = 0;
12977 				txq->kern_tls_waste = 0;
12978 				txq->kern_tls_header = 0;
12979 				txq->kern_tls_fin_short = 0;
12980 				txq->kern_tls_cbc = 0;
12981 				txq->kern_tls_gcm = 0;
12982 				if (is_t6(sc)) {
12983 					txq->kern_tls_options = 0;
12984 					txq->kern_tls_fin = 0;
12985 				} else {
12986 					txq->kern_tls_ghash_received = 0;
12987 					txq->kern_tls_ghash_requested = 0;
12988 					txq->kern_tls_lso = 0;
12989 					txq->kern_tls_partial_ghash = 0;
12990 					txq->kern_tls_splitmode = 0;
12991 					txq->kern_tls_trailer = 0;
12992 				}
12993 				mp_ring_reset_stats(txq->r);
12994 			}
12995 
12996 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
12997 			for_each_ofld_txq(vi, i, ofld_txq) {
12998 				ofld_txq->wrq.tx_wrs_direct = 0;
12999 				ofld_txq->wrq.tx_wrs_copied = 0;
13000 				counter_u64_zero(ofld_txq->tx_iscsi_pdus);
13001 				counter_u64_zero(ofld_txq->tx_iscsi_octets);
13002 				counter_u64_zero(ofld_txq->tx_iscsi_iso_wrs);
13003 				counter_u64_zero(ofld_txq->tx_nvme_pdus);
13004 				counter_u64_zero(ofld_txq->tx_nvme_octets);
13005 				counter_u64_zero(ofld_txq->tx_nvme_iso_wrs);
13006 				counter_u64_zero(ofld_txq->tx_aio_jobs);
13007 				counter_u64_zero(ofld_txq->tx_aio_octets);
13008 				counter_u64_zero(ofld_txq->tx_toe_tls_records);
13009 				counter_u64_zero(ofld_txq->tx_toe_tls_octets);
13010 			}
13011 #endif
13012 #ifdef TCP_OFFLOAD
13013 			for_each_ofld_rxq(vi, i, ofld_rxq) {
13014 				ofld_rxq->fl.cl_allocated = 0;
13015 				ofld_rxq->fl.cl_recycled = 0;
13016 				ofld_rxq->fl.cl_fast_recycled = 0;
13017 				counter_u64_zero(
13018 				    ofld_rxq->rx_iscsi_ddp_setup_ok);
13019 				counter_u64_zero(
13020 				    ofld_rxq->rx_iscsi_ddp_setup_error);
13021 				ofld_rxq->rx_iscsi_ddp_pdus = 0;
13022 				ofld_rxq->rx_iscsi_ddp_octets = 0;
13023 				ofld_rxq->rx_iscsi_fl_pdus = 0;
13024 				ofld_rxq->rx_iscsi_fl_octets = 0;
13025 				counter_u64_zero(
13026 				    ofld_rxq->rx_nvme_ddp_setup_ok);
13027 				counter_u64_zero(
13028 				    ofld_rxq->rx_nvme_ddp_setup_no_stag);
13029 				counter_u64_zero(
13030 				    ofld_rxq->rx_nvme_ddp_setup_error);
13031 				counter_u64_zero(ofld_rxq->rx_nvme_ddp_pdus);
13032 				counter_u64_zero(ofld_rxq->rx_nvme_ddp_octets);
13033 				counter_u64_zero(ofld_rxq->rx_nvme_fl_pdus);
13034 				counter_u64_zero(ofld_rxq->rx_nvme_fl_octets);
13035 				counter_u64_zero(
13036 				    ofld_rxq->rx_nvme_invalid_headers);
13037 				counter_u64_zero(
13038 				    ofld_rxq->rx_nvme_header_digest_errors);
13039 				counter_u64_zero(
13040 				    ofld_rxq->rx_nvme_data_digest_errors);
13041 				ofld_rxq->rx_aio_ddp_jobs = 0;
13042 				ofld_rxq->rx_aio_ddp_octets = 0;
13043 				ofld_rxq->rx_toe_tls_records = 0;
13044 				ofld_rxq->rx_toe_tls_octets = 0;
13045 				ofld_rxq->rx_toe_ddp_octets = 0;
13046 				counter_u64_zero(ofld_rxq->ddp_buffer_alloc);
13047 				counter_u64_zero(ofld_rxq->ddp_buffer_reuse);
13048 				counter_u64_zero(ofld_rxq->ddp_buffer_free);
13049 			}
13050 #endif
13051 
13052 			if (IS_MAIN_VI(vi)) {
13053 				wrq = &sc->sge.ctrlq[pi->port_id];
13054 				wrq->tx_wrs_direct = 0;
13055 				wrq->tx_wrs_copied = 0;
13056 			}
13057 		}
13058 	}
13059 
13060 	return (0);
13061 }
13062 
13063 static int
hold_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13064 hold_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13065 {
13066 #ifdef INET6
13067 	struct in6_addr in6;
13068 
13069 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13070 	if (t4_get_clip_entry(sc, &in6, true) != NULL)
13071 		return (0);
13072 	else
13073 		return (EIO);
13074 #else
13075 	return (ENOTSUP);
13076 #endif
13077 }
13078 
13079 static int
release_clip_addr(struct adapter * sc,struct t4_clip_addr * ca)13080 release_clip_addr(struct adapter *sc, struct t4_clip_addr *ca)
13081 {
13082 #ifdef INET6
13083 	struct in6_addr in6;
13084 
13085 	bcopy(&ca->addr[0], &in6.s6_addr[0], sizeof(in6.s6_addr));
13086 	return (t4_release_clip_addr(sc, &in6));
13087 #else
13088 	return (ENOTSUP);
13089 #endif
13090 }
13091 
13092 int
t4_os_find_pci_capability(struct adapter * sc,int cap)13093 t4_os_find_pci_capability(struct adapter *sc, int cap)
13094 {
13095 	int i;
13096 
13097 	return (pci_find_cap(sc->dev, cap, &i) == 0 ? i : 0);
13098 }
13099 
13100 void
t4_os_portmod_changed(struct port_info * pi)13101 t4_os_portmod_changed(struct port_info *pi)
13102 {
13103 	struct adapter *sc = pi->adapter;
13104 	struct vi_info *vi;
13105 	if_t ifp;
13106 	static const char *mod_str[] = {
13107 		NULL, "LR", "SR", "ER", "TWINAX", "active TWINAX", "LRM",
13108 		"LR_SIMPLEX", "DR"
13109 	};
13110 
13111 	KASSERT((pi->flags & FIXED_IFMEDIA) == 0,
13112 	    ("%s: port_type %u", __func__, pi->port_type));
13113 
13114 	vi = &pi->vi[0];
13115 	if (begin_synchronized_op(sc, vi, HOLD_LOCK, "t4mod") == 0) {
13116 		PORT_LOCK(pi);
13117 		build_medialist(pi);
13118 		if (pi->mod_type != FW_PORT_MOD_TYPE_NONE) {
13119 			fixup_link_config(pi);
13120 			apply_link_config(pi);
13121 		}
13122 		PORT_UNLOCK(pi);
13123 		end_synchronized_op(sc, LOCK_HELD);
13124 	}
13125 
13126 	ifp = vi->ifp;
13127 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
13128 		if_printf(ifp, "transceiver unplugged.\n");
13129 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
13130 		if_printf(ifp, "unknown transceiver inserted.\n");
13131 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
13132 		if_printf(ifp, "unsupported transceiver inserted.\n");
13133 	else if (pi->mod_type > 0 && pi->mod_type < nitems(mod_str)) {
13134 		if_printf(ifp, "%dGbps %s transceiver inserted.\n",
13135 		    port_top_speed(pi), mod_str[pi->mod_type]);
13136 	} else {
13137 		if_printf(ifp, "transceiver (type %d) inserted.\n",
13138 		    pi->mod_type);
13139 	}
13140 }
13141 
13142 void
t4_os_link_changed(struct port_info * pi)13143 t4_os_link_changed(struct port_info *pi)
13144 {
13145 	struct vi_info *vi;
13146 	if_t ifp;
13147 	struct link_config *lc = &pi->link_cfg;
13148 	struct adapter *sc = pi->adapter;
13149 	int v;
13150 
13151 	PORT_LOCK_ASSERT_OWNED(pi);
13152 
13153 	if (is_t6(sc)) {
13154 		if (lc->link_ok) {
13155 			if (lc->speed > 25000 ||
13156 			    (lc->speed == 25000 && lc->fec == FEC_RS))
13157 				pi->fcs_reg = A_MAC_PORT_AFRAMECHECKSEQUENCEERRORS;
13158 			else
13159 				pi->fcs_reg = A_MAC_PORT_MTIP_1G10G_RX_CRCERRORS;
13160 			pi->fcs_base = t4_read_reg64(sc,
13161 			    t4_port_reg(sc, pi->tx_chan, pi->fcs_reg));
13162 			pi->stats.rx_fcs_err = 0;
13163 		} else {
13164 			pi->fcs_reg = -1;
13165 		}
13166 	} else {
13167 		MPASS(pi->fcs_reg != -1);
13168 		MPASS(pi->fcs_base == 0);
13169 	}
13170 
13171 	for_each_vi(pi, v, vi) {
13172 		ifp = vi->ifp;
13173 		if (ifp == NULL || IS_DETACHING(vi))
13174 			continue;
13175 
13176 		if (lc->link_ok) {
13177 			if_setbaudrate(ifp, IF_Mbps(lc->speed));
13178 			if_link_state_change(ifp, LINK_STATE_UP);
13179 		} else {
13180 			if_link_state_change(ifp, LINK_STATE_DOWN);
13181 		}
13182 	}
13183 }
13184 
13185 void
t4_iterate(void (* func)(struct adapter *,void *),void * arg)13186 t4_iterate(void (*func)(struct adapter *, void *), void *arg)
13187 {
13188 	struct adapter *sc;
13189 
13190 	sx_slock(&t4_list_lock);
13191 	SLIST_FOREACH(sc, &t4_list, link) {
13192 		/*
13193 		 * func should not make any assumptions about what state sc is
13194 		 * in - the only guarantee is that sc->sc_lock is a valid lock.
13195 		 */
13196 		func(sc, arg);
13197 	}
13198 	sx_sunlock(&t4_list_lock);
13199 }
13200 
13201 static int
t4_ioctl(struct cdev * dev,unsigned long cmd,caddr_t data,int fflag,struct thread * td)13202 t4_ioctl(struct cdev *dev, unsigned long cmd, caddr_t data, int fflag,
13203     struct thread *td)
13204 {
13205 	int rc;
13206 	struct adapter *sc = dev->si_drv1;
13207 
13208 	rc = priv_check(td, PRIV_DRIVER);
13209 	if (rc != 0)
13210 		return (rc);
13211 
13212 	switch (cmd) {
13213 	case CHELSIO_T4_GETREG: {
13214 		struct t4_reg *edata = (struct t4_reg *)data;
13215 
13216 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13217 			return (EFAULT);
13218 
13219 		mtx_lock(&sc->reg_lock);
13220 		if (hw_off_limits(sc))
13221 			rc = ENXIO;
13222 		else if (edata->size == 4)
13223 			edata->val = t4_read_reg(sc, edata->addr);
13224 		else if (edata->size == 8)
13225 			edata->val = t4_read_reg64(sc, edata->addr);
13226 		else
13227 			rc = EINVAL;
13228 		mtx_unlock(&sc->reg_lock);
13229 
13230 		break;
13231 	}
13232 	case CHELSIO_T4_SETREG: {
13233 		struct t4_reg *edata = (struct t4_reg *)data;
13234 
13235 		if ((edata->addr & 0x3) != 0 || edata->addr >= sc->mmio_len)
13236 			return (EFAULT);
13237 
13238 		mtx_lock(&sc->reg_lock);
13239 		if (hw_off_limits(sc))
13240 			rc = ENXIO;
13241 		else if (edata->size == 4) {
13242 			if (edata->val & 0xffffffff00000000)
13243 				rc = EINVAL;
13244 			t4_write_reg(sc, edata->addr, (uint32_t) edata->val);
13245 		} else if (edata->size == 8)
13246 			t4_write_reg64(sc, edata->addr, edata->val);
13247 		else
13248 			rc = EINVAL;
13249 		mtx_unlock(&sc->reg_lock);
13250 
13251 		break;
13252 	}
13253 	case CHELSIO_T4_REGDUMP: {
13254 		struct t4_regdump *regs = (struct t4_regdump *)data;
13255 		int reglen = t4_get_regs_len(sc);
13256 		uint8_t *buf;
13257 
13258 		if (regs->len < reglen) {
13259 			regs->len = reglen; /* hint to the caller */
13260 			return (ENOBUFS);
13261 		}
13262 
13263 		regs->len = reglen;
13264 		buf = malloc(reglen, M_CXGBE, M_WAITOK | M_ZERO);
13265 		mtx_lock(&sc->reg_lock);
13266 		if (hw_off_limits(sc))
13267 			rc = ENXIO;
13268 		else
13269 			get_regs(sc, regs, buf);
13270 		mtx_unlock(&sc->reg_lock);
13271 		if (rc == 0)
13272 			rc = copyout(buf, regs->data, reglen);
13273 		free(buf, M_CXGBE);
13274 		break;
13275 	}
13276 	case CHELSIO_T4_GET_FILTER_MODE:
13277 		rc = get_filter_mode(sc, (uint32_t *)data);
13278 		break;
13279 	case CHELSIO_T4_SET_FILTER_MODE:
13280 		rc = set_filter_mode(sc, *(uint32_t *)data);
13281 		break;
13282 	case CHELSIO_T4_SET_FILTER_MASK:
13283 		rc = set_filter_mask(sc, *(uint32_t *)data);
13284 		break;
13285 	case CHELSIO_T4_GET_FILTER:
13286 		rc = get_filter(sc, (struct t4_filter *)data);
13287 		break;
13288 	case CHELSIO_T4_SET_FILTER:
13289 		rc = set_filter(sc, (struct t4_filter *)data);
13290 		break;
13291 	case CHELSIO_T4_DEL_FILTER:
13292 		rc = del_filter(sc, (struct t4_filter *)data);
13293 		break;
13294 	case CHELSIO_T4_GET_SGE_CONTEXT: {
13295 		struct t4_sge_context *ctxt = (struct t4_sge_context *)data;
13296 
13297 		rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13298 		    sizeof(ctxt->data), &ctxt->data[0]);
13299 		break;
13300 	}
13301 	case CHELSIO_T4_LOAD_FW:
13302 		rc = load_fw(sc, (struct t4_data *)data);
13303 		break;
13304 	case CHELSIO_T4_GET_MEM:
13305 		rc = read_card_mem(sc, 2, (struct t4_mem_range *)data);
13306 		break;
13307 	case CHELSIO_T4_GET_I2C:
13308 		rc = read_i2c(sc, (struct t4_i2c_data *)data);
13309 		break;
13310 	case CHELSIO_T4_CLEAR_STATS:
13311 		rc = clear_stats(sc, *(uint32_t *)data);
13312 		break;
13313 	case CHELSIO_T4_SCHED_CLASS:
13314 		rc = t4_set_sched_class(sc, (struct t4_sched_params *)data);
13315 		break;
13316 	case CHELSIO_T4_SCHED_QUEUE:
13317 		rc = t4_set_sched_queue(sc, (struct t4_sched_queue *)data);
13318 		break;
13319 	case CHELSIO_T4_GET_TRACER:
13320 		rc = t4_get_tracer(sc, (struct t4_tracer *)data);
13321 		break;
13322 	case CHELSIO_T4_SET_TRACER:
13323 		rc = t4_set_tracer(sc, (struct t4_tracer *)data);
13324 		break;
13325 	case CHELSIO_T4_LOAD_CFG:
13326 		rc = load_cfg(sc, (struct t4_data *)data);
13327 		break;
13328 	case CHELSIO_T4_LOAD_BOOT:
13329 		rc = load_boot(sc, (struct t4_bootrom *)data);
13330 		break;
13331 	case CHELSIO_T4_LOAD_BOOTCFG:
13332 		rc = load_bootcfg(sc, (struct t4_data *)data);
13333 		break;
13334 	case CHELSIO_T4_CUDBG_DUMP:
13335 		rc = cudbg_dump(sc, (struct t4_cudbg_dump *)data);
13336 		break;
13337 	case CHELSIO_T4_SET_OFLD_POLICY:
13338 		rc = set_offload_policy(sc, (struct t4_offload_policy *)data);
13339 		break;
13340 	case CHELSIO_T4_HOLD_CLIP_ADDR:
13341 		rc = hold_clip_addr(sc, (struct t4_clip_addr *)data);
13342 		break;
13343 	case CHELSIO_T4_RELEASE_CLIP_ADDR:
13344 		rc = release_clip_addr(sc, (struct t4_clip_addr *)data);
13345 		break;
13346 	case CHELSIO_T4_GET_SGE_CTXT: {
13347 		struct t4_sge_ctxt *ctxt = (struct t4_sge_ctxt *)data;
13348 
13349 		rc = get_sge_context(sc, ctxt->mem_id, ctxt->cid,
13350 		    sizeof(ctxt->data), &ctxt->data[0]);
13351 		break;
13352 	}
13353 	default:
13354 		rc = ENOTTY;
13355 	}
13356 
13357 	return (rc);
13358 }
13359 
13360 #ifdef TCP_OFFLOAD
13361 int
toe_capability(struct vi_info * vi,bool enable)13362 toe_capability(struct vi_info *vi, bool enable)
13363 {
13364 	int rc;
13365 	struct port_info *pi = vi->pi;
13366 	struct adapter *sc = pi->adapter;
13367 
13368 	ASSERT_SYNCHRONIZED_OP(sc);
13369 
13370 	if (!is_offload(sc))
13371 		return (ENODEV);
13372 	if (!hw_all_ok(sc))
13373 		return (ENXIO);
13374 
13375 	if (enable) {
13376 #ifdef KERN_TLS
13377 		if (sc->flags & KERN_TLS_ON && is_t6(sc)) {
13378 			int i, j, n;
13379 			struct port_info *p;
13380 			struct vi_info *v;
13381 
13382 			/*
13383 			 * Reconfigure hardware for TOE if TXTLS is not enabled
13384 			 * on any ifnet.
13385 			 */
13386 			n = 0;
13387 			for_each_port(sc, i) {
13388 				p = sc->port[i];
13389 				for_each_vi(p, j, v) {
13390 					if (if_getcapenable(v->ifp) & IFCAP_TXTLS) {
13391 						CH_WARN(sc,
13392 						    "%s has NIC TLS enabled.\n",
13393 						    device_get_nameunit(v->dev));
13394 						n++;
13395 					}
13396 				}
13397 			}
13398 			if (n > 0) {
13399 				CH_WARN(sc, "Disable NIC TLS on all interfaces "
13400 				    "associated with this adapter before "
13401 				    "trying to enable TOE.\n");
13402 				return (EAGAIN);
13403 			}
13404 			rc = t6_config_kern_tls(sc, false);
13405 			if (rc)
13406 				return (rc);
13407 		}
13408 #endif
13409 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) != 0) {
13410 			/* TOE is already enabled. */
13411 			return (0);
13412 		}
13413 
13414 		/*
13415 		 * We need the port's queues around so that we're able to send
13416 		 * and receive CPLs to/from the TOE even if the ifnet for this
13417 		 * port has never been UP'd administratively.
13418 		 */
13419 		if (!(vi->flags & VI_INIT_DONE) && ((rc = vi_init(vi)) != 0))
13420 			return (rc);
13421 		if (!(pi->vi[0].flags & VI_INIT_DONE) &&
13422 		    ((rc = vi_init(&pi->vi[0])) != 0))
13423 			return (rc);
13424 
13425 		if (isset(&sc->offload_map, pi->port_id)) {
13426 			/* TOE is enabled on another VI of this port. */
13427 			MPASS(pi->uld_vis > 0);
13428 			pi->uld_vis++;
13429 			return (0);
13430 		}
13431 
13432 		if (!uld_active(sc, ULD_TOM)) {
13433 			rc = t4_activate_uld(sc, ULD_TOM);
13434 			if (rc == EAGAIN) {
13435 				log(LOG_WARNING,
13436 				    "You must kldload t4_tom.ko before trying "
13437 				    "to enable TOE on a cxgbe interface.\n");
13438 			}
13439 			if (rc != 0)
13440 				return (rc);
13441 			KASSERT(sc->tom_softc != NULL,
13442 			    ("%s: TOM activated but softc NULL", __func__));
13443 			KASSERT(uld_active(sc, ULD_TOM),
13444 			    ("%s: TOM activated but flag not set", __func__));
13445 		}
13446 
13447 		/*
13448 		 * Activate iWARP, iSCSI, and NVMe too, if the modules
13449 		 * are loaded.
13450 		 */
13451 		if (!uld_active(sc, ULD_IWARP))
13452 			(void) t4_activate_uld(sc, ULD_IWARP);
13453 		if (!uld_active(sc, ULD_ISCSI))
13454 			(void) t4_activate_uld(sc, ULD_ISCSI);
13455 		if (!uld_active(sc, ULD_NVME))
13456 			(void) t4_activate_uld(sc, ULD_NVME);
13457 
13458 		if (pi->uld_vis++ == 0)
13459 			setbit(&sc->offload_map, pi->port_id);
13460 	} else {
13461 		if ((if_getcapenable(vi->ifp) & IFCAP_TOE) == 0) {
13462 			/* TOE is already disabled. */
13463 			return (0);
13464 		}
13465 		MPASS(isset(&sc->offload_map, pi->port_id));
13466 		MPASS(pi->uld_vis > 0);
13467 		if (--pi->uld_vis == 0)
13468 			clrbit(&sc->offload_map, pi->port_id);
13469 	}
13470 
13471 	return (0);
13472 }
13473 
13474 /*
13475  * Add an upper layer driver to the global list.
13476  */
13477 int
t4_register_uld(struct uld_info * ui,int id)13478 t4_register_uld(struct uld_info *ui, int id)
13479 {
13480 	int rc;
13481 
13482 	if (id < 0 || id > ULD_MAX)
13483 		return (EINVAL);
13484 	sx_xlock(&t4_uld_list_lock);
13485 	if (t4_uld_list[id] != NULL)
13486 		rc = EEXIST;
13487 	else {
13488 		t4_uld_list[id] = ui;
13489 		rc = 0;
13490 	}
13491 	sx_xunlock(&t4_uld_list_lock);
13492 	return (rc);
13493 }
13494 
13495 int
t4_unregister_uld(struct uld_info * ui,int id)13496 t4_unregister_uld(struct uld_info *ui, int id)
13497 {
13498 
13499 	if (id < 0 || id > ULD_MAX)
13500 		return (EINVAL);
13501 	sx_xlock(&t4_uld_list_lock);
13502 	MPASS(t4_uld_list[id] == ui);
13503 	t4_uld_list[id] = NULL;
13504 	sx_xunlock(&t4_uld_list_lock);
13505 	return (0);
13506 }
13507 
13508 int
t4_activate_uld(struct adapter * sc,int id)13509 t4_activate_uld(struct adapter *sc, int id)
13510 {
13511 	int rc;
13512 
13513 	ASSERT_SYNCHRONIZED_OP(sc);
13514 
13515 	if (id < 0 || id > ULD_MAX)
13516 		return (EINVAL);
13517 
13518 	/* Adapter needs to be initialized before any ULD can be activated. */
13519 	if (!(sc->flags & FULL_INIT_DONE)) {
13520 		rc = adapter_init(sc);
13521 		if (rc != 0)
13522 			return (rc);
13523 	}
13524 
13525 	sx_slock(&t4_uld_list_lock);
13526 	if (t4_uld_list[id] == NULL)
13527 		rc = EAGAIN;	/* load the KLD with this ULD and try again. */
13528 	else {
13529 		rc = t4_uld_list[id]->uld_activate(sc);
13530 		if (rc == 0)
13531 			setbit(&sc->active_ulds, id);
13532 	}
13533 	sx_sunlock(&t4_uld_list_lock);
13534 
13535 	return (rc);
13536 }
13537 
13538 int
t4_deactivate_uld(struct adapter * sc,int id)13539 t4_deactivate_uld(struct adapter *sc, int id)
13540 {
13541 	int rc;
13542 
13543 	ASSERT_SYNCHRONIZED_OP(sc);
13544 
13545 	if (id < 0 || id > ULD_MAX)
13546 		return (EINVAL);
13547 
13548 	sx_slock(&t4_uld_list_lock);
13549 	if (t4_uld_list[id] == NULL)
13550 		rc = ENXIO;
13551 	else {
13552 		rc = t4_uld_list[id]->uld_deactivate(sc);
13553 		if (rc == 0)
13554 			clrbit(&sc->active_ulds, id);
13555 	}
13556 	sx_sunlock(&t4_uld_list_lock);
13557 
13558 	return (rc);
13559 }
13560 
13561 static int
deactivate_all_uld(struct adapter * sc)13562 deactivate_all_uld(struct adapter *sc)
13563 {
13564 	int i, rc;
13565 
13566 	rc = begin_synchronized_op(sc, NULL, SLEEP_OK, "t4detuld");
13567 	if (rc != 0)
13568 		return (ENXIO);
13569 	sx_slock(&t4_uld_list_lock);
13570 	for (i = 0; i <= ULD_MAX; i++) {
13571 		if (t4_uld_list[i] == NULL || !uld_active(sc, i))
13572 			continue;
13573 		rc = t4_uld_list[i]->uld_deactivate(sc);
13574 		if (rc != 0)
13575 			break;
13576 		clrbit(&sc->active_ulds, i);
13577 	}
13578 	sx_sunlock(&t4_uld_list_lock);
13579 	end_synchronized_op(sc, 0);
13580 
13581 	return (rc);
13582 }
13583 
13584 static void
stop_all_uld(struct adapter * sc)13585 stop_all_uld(struct adapter *sc)
13586 {
13587 	int i;
13588 
13589 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldst") != 0)
13590 		return;
13591 	sx_slock(&t4_uld_list_lock);
13592 	for (i = 0; i <= ULD_MAX; i++) {
13593 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13594 		    t4_uld_list[i]->uld_stop == NULL)
13595 			continue;
13596 		(void) t4_uld_list[i]->uld_stop(sc);
13597 	}
13598 	sx_sunlock(&t4_uld_list_lock);
13599 	end_synchronized_op(sc, 0);
13600 }
13601 
13602 static void
restart_all_uld(struct adapter * sc)13603 restart_all_uld(struct adapter *sc)
13604 {
13605 	int i;
13606 
13607 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4uldre") != 0)
13608 		return;
13609 	sx_slock(&t4_uld_list_lock);
13610 	for (i = 0; i <= ULD_MAX; i++) {
13611 		if (t4_uld_list[i] == NULL || !uld_active(sc, i) ||
13612 		    t4_uld_list[i]->uld_restart == NULL)
13613 			continue;
13614 		(void) t4_uld_list[i]->uld_restart(sc);
13615 	}
13616 	sx_sunlock(&t4_uld_list_lock);
13617 	end_synchronized_op(sc, 0);
13618 }
13619 
13620 int
uld_active(struct adapter * sc,int id)13621 uld_active(struct adapter *sc, int id)
13622 {
13623 
13624 	MPASS(id >= 0 && id <= ULD_MAX);
13625 
13626 	return (isset(&sc->active_ulds, id));
13627 }
13628 #endif
13629 
13630 #ifdef KERN_TLS
13631 static int
ktls_capability(struct adapter * sc,bool enable)13632 ktls_capability(struct adapter *sc, bool enable)
13633 {
13634 	ASSERT_SYNCHRONIZED_OP(sc);
13635 
13636 	if (!is_ktls(sc))
13637 		return (ENODEV);
13638 	if (!is_t6(sc))
13639 		return (0);
13640 	if (!hw_all_ok(sc))
13641 		return (ENXIO);
13642 
13643 	if (enable) {
13644 		if (sc->flags & KERN_TLS_ON)
13645 			return (0);	/* already on */
13646 		if (sc->offload_map != 0) {
13647 			CH_WARN(sc,
13648 			    "Disable TOE on all interfaces associated with "
13649 			    "this adapter before trying to enable NIC TLS.\n");
13650 			return (EAGAIN);
13651 		}
13652 		return (t6_config_kern_tls(sc, true));
13653 	} else {
13654 		/*
13655 		 * Nothing to do for disable.  If TOE is enabled sometime later
13656 		 * then toe_capability will reconfigure the hardware.
13657 		 */
13658 		return (0);
13659 	}
13660 }
13661 #endif
13662 
13663 /*
13664  * t  = ptr to tunable.
13665  * nc = number of CPUs.
13666  * c  = compiled in default for that tunable.
13667  */
13668 static void
calculate_nqueues(int * t,int nc,const int c)13669 calculate_nqueues(int *t, int nc, const int c)
13670 {
13671 	int nq;
13672 
13673 	if (*t > 0)
13674 		return;
13675 	nq = *t < 0 ? -*t : c;
13676 	*t = min(nc, nq);
13677 }
13678 
13679 /*
13680  * Come up with reasonable defaults for some of the tunables, provided they're
13681  * not set by the user (in which case we'll use the values as is).
13682  */
13683 static void
tweak_tunables(void)13684 tweak_tunables(void)
13685 {
13686 	int nc = mp_ncpus;	/* our snapshot of the number of CPUs */
13687 
13688 	if (t4_ntxq < 1) {
13689 #ifdef RSS
13690 		t4_ntxq = rss_getnumbuckets();
13691 #else
13692 		calculate_nqueues(&t4_ntxq, nc, NTXQ);
13693 #endif
13694 	}
13695 
13696 	calculate_nqueues(&t4_ntxq_vi, nc, NTXQ_VI);
13697 
13698 	if (t4_nrxq < 1) {
13699 #ifdef RSS
13700 		t4_nrxq = rss_getnumbuckets();
13701 #else
13702 		calculate_nqueues(&t4_nrxq, nc, NRXQ);
13703 #endif
13704 	}
13705 
13706 	calculate_nqueues(&t4_nrxq_vi, nc, NRXQ_VI);
13707 
13708 #if defined(TCP_OFFLOAD) || defined(RATELIMIT)
13709 	calculate_nqueues(&t4_nofldtxq, nc, NOFLDTXQ);
13710 	calculate_nqueues(&t4_nofldtxq_vi, nc, NOFLDTXQ_VI);
13711 #endif
13712 #ifdef TCP_OFFLOAD
13713 	calculate_nqueues(&t4_nofldrxq, nc, NOFLDRXQ);
13714 	calculate_nqueues(&t4_nofldrxq_vi, nc, NOFLDRXQ_VI);
13715 #endif
13716 
13717 #if defined(TCP_OFFLOAD) || defined(KERN_TLS)
13718 	if (t4_toecaps_allowed == -1)
13719 		t4_toecaps_allowed = FW_CAPS_CONFIG_TOE;
13720 #else
13721 	if (t4_toecaps_allowed == -1)
13722 		t4_toecaps_allowed = 0;
13723 #endif
13724 
13725 #ifdef TCP_OFFLOAD
13726 	if (t4_rdmacaps_allowed == -1) {
13727 		t4_rdmacaps_allowed = FW_CAPS_CONFIG_RDMA_RDDP |
13728 		    FW_CAPS_CONFIG_RDMA_RDMAC;
13729 	}
13730 
13731 	if (t4_iscsicaps_allowed == -1) {
13732 		t4_iscsicaps_allowed = FW_CAPS_CONFIG_ISCSI_INITIATOR_PDU |
13733 		    FW_CAPS_CONFIG_ISCSI_TARGET_PDU |
13734 		    FW_CAPS_CONFIG_ISCSI_T10DIF;
13735 	}
13736 
13737 	if (t4_nvmecaps_allowed == -1)
13738 		t4_nvmecaps_allowed = FW_CAPS_CONFIG_NVME_TCP;
13739 
13740 	if (t4_tmr_idx_ofld < 0 || t4_tmr_idx_ofld >= SGE_NTIMERS)
13741 		t4_tmr_idx_ofld = TMR_IDX_OFLD;
13742 
13743 	if (t4_pktc_idx_ofld < -1 || t4_pktc_idx_ofld >= SGE_NCOUNTERS)
13744 		t4_pktc_idx_ofld = PKTC_IDX_OFLD;
13745 #else
13746 	if (t4_rdmacaps_allowed == -1)
13747 		t4_rdmacaps_allowed = 0;
13748 
13749 	if (t4_iscsicaps_allowed == -1)
13750 		t4_iscsicaps_allowed = 0;
13751 
13752 	if (t4_nvmecaps_allowed == -1)
13753 		t4_nvmecaps_allowed = 0;
13754 #endif
13755 
13756 #ifdef DEV_NETMAP
13757 	calculate_nqueues(&t4_nnmtxq, nc, NNMTXQ);
13758 	calculate_nqueues(&t4_nnmrxq, nc, NNMRXQ);
13759 	calculate_nqueues(&t4_nnmtxq_vi, nc, NNMTXQ_VI);
13760 	calculate_nqueues(&t4_nnmrxq_vi, nc, NNMRXQ_VI);
13761 #endif
13762 
13763 	if (t4_tmr_idx < 0 || t4_tmr_idx >= SGE_NTIMERS)
13764 		t4_tmr_idx = TMR_IDX;
13765 
13766 	if (t4_pktc_idx < -1 || t4_pktc_idx >= SGE_NCOUNTERS)
13767 		t4_pktc_idx = PKTC_IDX;
13768 
13769 	if (t4_qsize_txq < 128)
13770 		t4_qsize_txq = 128;
13771 
13772 	if (t4_qsize_rxq < 128)
13773 		t4_qsize_rxq = 128;
13774 	while (t4_qsize_rxq & 7)
13775 		t4_qsize_rxq++;
13776 
13777 	t4_intr_types &= INTR_MSIX | INTR_MSI | INTR_INTX;
13778 
13779 	/*
13780 	 * Number of VIs to create per-port.  The first VI is the "main" regular
13781 	 * VI for the port.  The rest are additional virtual interfaces on the
13782 	 * same physical port.  Note that the main VI does not have native
13783 	 * netmap support but the extra VIs do.
13784 	 *
13785 	 * Limit the number of VIs per port to the number of available
13786 	 * MAC addresses per port.
13787 	 */
13788 	if (t4_num_vis < 1)
13789 		t4_num_vis = 1;
13790 	if (t4_num_vis > nitems(vi_mac_funcs)) {
13791 		t4_num_vis = nitems(vi_mac_funcs);
13792 		printf("cxgbe: number of VIs limited to %d\n", t4_num_vis);
13793 	}
13794 
13795 	if (pcie_relaxed_ordering < 0 || pcie_relaxed_ordering > 2) {
13796 		pcie_relaxed_ordering = 1;
13797 #if defined(__i386__) || defined(__amd64__)
13798 		if (cpu_vendor_id == CPU_VENDOR_INTEL)
13799 			pcie_relaxed_ordering = 0;
13800 #endif
13801 	}
13802 }
13803 
13804 #ifdef DDB
13805 static void
t4_dump_mem(struct adapter * sc,u_int addr,u_int len)13806 t4_dump_mem(struct adapter *sc, u_int addr, u_int len)
13807 {
13808 	uint32_t base, j, off, pf, reg, save, win_pos;
13809 
13810 	reg = chip_id(sc) > CHELSIO_T6 ?
13811 	    PCIE_MEM_ACCESS_T7_REG(A_PCIE_MEM_ACCESS_OFFSET0, 2) :
13812 	    PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_OFFSET, 2);
13813 	save = t4_read_reg(sc, reg);
13814 	base = sc->memwin[2].mw_base;
13815 
13816 	if (is_t4(sc)) {
13817 		pf = 0;
13818 		win_pos = addr & ~0xf;	/* start must be 16B aligned */
13819 	} else {
13820 		pf = V_PFNUM(sc->pf);
13821 		win_pos = addr & ~0x7f;	/* start must be 128B aligned */
13822 	}
13823 	off = addr - win_pos;
13824 	if (chip_id(sc) > CHELSIO_T6)
13825 		win_pos >>= X_T7_MEMOFST_SHIFT;
13826 	t4_write_reg(sc, reg, win_pos | pf);
13827 	t4_read_reg(sc, reg);
13828 
13829 	while (len > 0 && !db_pager_quit) {
13830 		uint32_t buf[8];
13831 		for (j = 0; j < 8; j++, off += 4)
13832 			buf[j] = htonl(t4_read_reg(sc, base + off));
13833 
13834 		db_printf("%08x %08x %08x %08x %08x %08x %08x %08x\n",
13835 		    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
13836 		    buf[7]);
13837 		if (len <= sizeof(buf))
13838 			len = 0;
13839 		else
13840 			len -= sizeof(buf);
13841 	}
13842 
13843 	t4_write_reg(sc, reg, save);
13844 	t4_read_reg(sc, reg);
13845 }
13846 
13847 static void
t4_dump_tcb(struct adapter * sc,int tid)13848 t4_dump_tcb(struct adapter *sc, int tid)
13849 {
13850 	uint32_t tcb_addr;
13851 
13852 	/* Dump TCB for the tid */
13853 	tcb_addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE);
13854 	tcb_addr += tid * TCB_SIZE;
13855 	t4_dump_mem(sc, tcb_addr, TCB_SIZE);
13856 }
13857 
13858 static void
t4_dump_devlog(struct adapter * sc)13859 t4_dump_devlog(struct adapter *sc)
13860 {
13861 	struct devlog_params *dparams = &sc->params.devlog;
13862 	struct fw_devlog_e e;
13863 	int i, first, j, m, nentries, rc;
13864 	uint64_t ftstamp = UINT64_MAX;
13865 
13866 	if (dparams->start == 0) {
13867 		db_printf("devlog params not valid\n");
13868 		return;
13869 	}
13870 
13871 	nentries = dparams->size / sizeof(struct fw_devlog_e);
13872 	m = fwmtype_to_hwmtype(dparams->memtype);
13873 
13874 	/* Find the first entry. */
13875 	first = -1;
13876 	for (i = 0; i < nentries && !db_pager_quit; i++) {
13877 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13878 		    sizeof(e), (void *)&e);
13879 		if (rc != 0)
13880 			break;
13881 
13882 		if (e.timestamp == 0)
13883 			break;
13884 
13885 		e.timestamp = be64toh(e.timestamp);
13886 		if (e.timestamp < ftstamp) {
13887 			ftstamp = e.timestamp;
13888 			first = i;
13889 		}
13890 	}
13891 
13892 	if (first == -1)
13893 		return;
13894 
13895 	i = first;
13896 	do {
13897 		rc = -t4_mem_read(sc, m, dparams->start + i * sizeof(e),
13898 		    sizeof(e), (void *)&e);
13899 		if (rc != 0)
13900 			return;
13901 
13902 		if (e.timestamp == 0)
13903 			return;
13904 
13905 		e.timestamp = be64toh(e.timestamp);
13906 		e.seqno = be32toh(e.seqno);
13907 		for (j = 0; j < 8; j++)
13908 			e.params[j] = be32toh(e.params[j]);
13909 
13910 		db_printf("%10d  %15ju  %8s  %8s  ",
13911 		    e.seqno, e.timestamp,
13912 		    (e.level < nitems(devlog_level_strings) ?
13913 			devlog_level_strings[e.level] : "UNKNOWN"),
13914 		    (e.facility < nitems(devlog_facility_strings) ?
13915 			devlog_facility_strings[e.facility] : "UNKNOWN"));
13916 		db_printf(e.fmt, e.params[0], e.params[1], e.params[2],
13917 		    e.params[3], e.params[4], e.params[5], e.params[6],
13918 		    e.params[7]);
13919 
13920 		if (++i == nentries)
13921 			i = 0;
13922 	} while (i != first && !db_pager_quit);
13923 }
13924 
13925 static DB_DEFINE_TABLE(show, t4, show_t4);
13926 
DB_TABLE_COMMAND_FLAGS(show_t4,devlog,db_show_devlog,CS_OWN)13927 DB_TABLE_COMMAND_FLAGS(show_t4, devlog, db_show_devlog, CS_OWN)
13928 {
13929 	device_t dev;
13930 	int t;
13931 	bool valid;
13932 
13933 	valid = false;
13934 	t = db_read_token();
13935 	if (t == tIDENT) {
13936 		dev = device_lookup_by_name(db_tok_string);
13937 		valid = true;
13938 	}
13939 	db_skip_to_eol();
13940 	if (!valid) {
13941 		db_printf("usage: show t4 devlog <nexus>\n");
13942 		return;
13943 	}
13944 
13945 	if (dev == NULL) {
13946 		db_printf("device not found\n");
13947 		return;
13948 	}
13949 
13950 	t4_dump_devlog(device_get_softc(dev));
13951 }
13952 
DB_TABLE_COMMAND_FLAGS(show_t4,tcb,db_show_t4tcb,CS_OWN)13953 DB_TABLE_COMMAND_FLAGS(show_t4, tcb, db_show_t4tcb, CS_OWN)
13954 {
13955 	device_t dev;
13956 	int radix, tid, t;
13957 	bool valid;
13958 
13959 	valid = false;
13960 	radix = db_radix;
13961 	db_radix = 10;
13962 	t = db_read_token();
13963 	if (t == tIDENT) {
13964 		dev = device_lookup_by_name(db_tok_string);
13965 		t = db_read_token();
13966 		if (t == tNUMBER) {
13967 			tid = db_tok_number;
13968 			valid = true;
13969 		}
13970 	}
13971 	db_radix = radix;
13972 	db_skip_to_eol();
13973 	if (!valid) {
13974 		db_printf("usage: show t4 tcb <nexus> <tid>\n");
13975 		return;
13976 	}
13977 
13978 	if (dev == NULL) {
13979 		db_printf("device not found\n");
13980 		return;
13981 	}
13982 	if (tid < 0) {
13983 		db_printf("invalid tid\n");
13984 		return;
13985 	}
13986 
13987 	t4_dump_tcb(device_get_softc(dev), tid);
13988 }
13989 
DB_TABLE_COMMAND_FLAGS(show_t4,memdump,db_show_memdump,CS_OWN)13990 DB_TABLE_COMMAND_FLAGS(show_t4, memdump, db_show_memdump, CS_OWN)
13991 {
13992 	device_t dev;
13993 	int radix, t;
13994 	bool valid;
13995 
13996 	valid = false;
13997 	radix = db_radix;
13998 	db_radix = 10;
13999 	t = db_read_token();
14000 	if (t == tIDENT) {
14001 		dev = device_lookup_by_name(db_tok_string);
14002 		t = db_read_token();
14003 		if (t == tNUMBER) {
14004 			addr = db_tok_number;
14005 			t = db_read_token();
14006 			if (t == tNUMBER) {
14007 				count = db_tok_number;
14008 				valid = true;
14009 			}
14010 		}
14011 	}
14012 	db_radix = radix;
14013 	db_skip_to_eol();
14014 	if (!valid) {
14015 		db_printf("usage: show t4 memdump <nexus> <addr> <len>\n");
14016 		return;
14017 	}
14018 
14019 	if (dev == NULL) {
14020 		db_printf("device not found\n");
14021 		return;
14022 	}
14023 	if (addr < 0) {
14024 		db_printf("invalid address\n");
14025 		return;
14026 	}
14027 	if (count <= 0) {
14028 		db_printf("invalid length\n");
14029 		return;
14030 	}
14031 
14032 	t4_dump_mem(device_get_softc(dev), addr, count);
14033 }
14034 #endif
14035 
14036 static eventhandler_tag vxlan_start_evtag;
14037 static eventhandler_tag vxlan_stop_evtag;
14038 
14039 struct vxlan_evargs {
14040 	if_t ifp;
14041 	uint16_t port;
14042 };
14043 
14044 static void
enable_vxlan_rx(struct adapter * sc)14045 enable_vxlan_rx(struct adapter *sc)
14046 {
14047 	int i, rc;
14048 	struct port_info *pi;
14049 	uint8_t match_all_mac[ETHER_ADDR_LEN] = {0};
14050 
14051 	ASSERT_SYNCHRONIZED_OP(sc);
14052 
14053 	t4_write_reg(sc, A_MPS_RX_VXLAN_TYPE, V_VXLAN(sc->vxlan_port) |
14054 	    F_VXLAN_EN);
14055 	for_each_port(sc, i) {
14056 		pi = sc->port[i];
14057 		if (pi->vxlan_tcam_entry == true)
14058 			continue;
14059 		rc = t4_alloc_raw_mac_filt(sc, pi->vi[0].viid, match_all_mac,
14060 		    match_all_mac, sc->rawf_base + pi->port_id, 1, pi->port_id,
14061 		    true);
14062 		if (rc < 0) {
14063 			rc = -rc;
14064 			CH_ERR(&pi->vi[0],
14065 			    "failed to add VXLAN TCAM entry: %d.\n", rc);
14066 		} else {
14067 			MPASS(rc == sc->rawf_base + pi->port_id);
14068 			pi->vxlan_tcam_entry = true;
14069 		}
14070 	}
14071 }
14072 
14073 static void
t4_vxlan_start(struct adapter * sc,void * arg)14074 t4_vxlan_start(struct adapter *sc, void *arg)
14075 {
14076 	struct vxlan_evargs *v = arg;
14077 
14078 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14079 		return;
14080 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxst") != 0)
14081 		return;
14082 
14083 	if (sc->vxlan_refcount == 0) {
14084 		sc->vxlan_port = v->port;
14085 		sc->vxlan_refcount = 1;
14086 		if (!hw_off_limits(sc))
14087 			enable_vxlan_rx(sc);
14088 	} else if (sc->vxlan_port == v->port) {
14089 		sc->vxlan_refcount++;
14090 	} else {
14091 		CH_ERR(sc, "VXLAN already configured on port  %d; "
14092 		    "ignoring attempt to configure it on port %d\n",
14093 		    sc->vxlan_port, v->port);
14094 	}
14095 	end_synchronized_op(sc, 0);
14096 }
14097 
14098 static void
t4_vxlan_stop(struct adapter * sc,void * arg)14099 t4_vxlan_stop(struct adapter *sc, void *arg)
14100 {
14101 	struct vxlan_evargs *v = arg;
14102 
14103 	if (sc->nrawf == 0 || chip_id(sc) <= CHELSIO_T5)
14104 		return;
14105 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4vxsp") != 0)
14106 		return;
14107 
14108 	/*
14109 	 * VXLANs may have been configured before the driver was loaded so we
14110 	 * may see more stops than starts.  This is not handled cleanly but at
14111 	 * least we keep the refcount sane.
14112 	 */
14113 	if (sc->vxlan_port != v->port)
14114 		goto done;
14115 	if (sc->vxlan_refcount == 0) {
14116 		CH_ERR(sc, "VXLAN operation on port %d was stopped earlier; "
14117 		    "ignoring attempt to stop it again.\n", sc->vxlan_port);
14118 	} else if (--sc->vxlan_refcount == 0 && !hw_off_limits(sc))
14119 		t4_set_reg_field(sc, A_MPS_RX_VXLAN_TYPE, F_VXLAN_EN, 0);
14120 done:
14121 	end_synchronized_op(sc, 0);
14122 }
14123 
14124 static void
t4_vxlan_start_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14125 t4_vxlan_start_handler(void *arg __unused, if_t ifp,
14126     sa_family_t family, u_int port)
14127 {
14128 	struct vxlan_evargs v;
14129 
14130 	MPASS(family == AF_INET || family == AF_INET6);
14131 	v.ifp = ifp;
14132 	v.port = port;
14133 
14134 	t4_iterate(t4_vxlan_start, &v);
14135 }
14136 
14137 static void
t4_vxlan_stop_handler(void * arg __unused,if_t ifp,sa_family_t family,u_int port)14138 t4_vxlan_stop_handler(void *arg __unused, if_t ifp, sa_family_t family,
14139     u_int port)
14140 {
14141 	struct vxlan_evargs v;
14142 
14143 	MPASS(family == AF_INET || family == AF_INET6);
14144 	v.ifp = ifp;
14145 	v.port = port;
14146 
14147 	t4_iterate(t4_vxlan_stop, &v);
14148 }
14149 
14150 
14151 static struct sx mlu;	/* mod load unload */
14152 SX_SYSINIT(cxgbe_mlu, &mlu, "cxgbe mod load/unload");
14153 
14154 static int
mod_event(module_t mod,int cmd,void * arg)14155 mod_event(module_t mod, int cmd, void *arg)
14156 {
14157 	int rc = 0;
14158 	static int loaded = 0;
14159 
14160 	switch (cmd) {
14161 	case MOD_LOAD:
14162 		sx_xlock(&mlu);
14163 		if (loaded++ == 0) {
14164 			t4_sge_modload();
14165 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14166 			    t4_filter_rpl, CPL_COOKIE_FILTER);
14167 			t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL,
14168 			    do_l2t_write_rpl, CPL_COOKIE_FILTER);
14169 			t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL,
14170 			    t4_hashfilter_ao_rpl, CPL_COOKIE_HASHFILTER);
14171 			t4_register_shared_cpl_handler(CPL_SET_TCB_RPL,
14172 			    t4_hashfilter_tcb_rpl, CPL_COOKIE_HASHFILTER);
14173 			t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS,
14174 			    t4_del_hashfilter_rpl, CPL_COOKIE_HASHFILTER);
14175 			t4_register_cpl_handler(CPL_TRACE_PKT, t4_trace_pkt);
14176 			t4_register_cpl_handler(CPL_T5_TRACE_PKT, t5_trace_pkt);
14177 			t4_register_cpl_handler(CPL_SMT_WRITE_RPL,
14178 			    do_smt_write_rpl);
14179 			sx_init(&t4_list_lock, "T4/T5 adapters");
14180 			SLIST_INIT(&t4_list);
14181 			callout_init(&fatal_callout, 1);
14182 #ifdef TCP_OFFLOAD
14183 			sx_init(&t4_uld_list_lock, "T4/T5 ULDs");
14184 #endif
14185 #ifdef INET6
14186 			t4_clip_modload();
14187 #endif
14188 #ifdef KERN_TLS
14189 			t6_ktls_modload();
14190 			t7_ktls_modload();
14191 #endif
14192 			t4_tracer_modload();
14193 			tweak_tunables();
14194 			vxlan_start_evtag =
14195 			    EVENTHANDLER_REGISTER(vxlan_start,
14196 				t4_vxlan_start_handler, NULL,
14197 				EVENTHANDLER_PRI_ANY);
14198 			vxlan_stop_evtag =
14199 			    EVENTHANDLER_REGISTER(vxlan_stop,
14200 				t4_vxlan_stop_handler, NULL,
14201 				EVENTHANDLER_PRI_ANY);
14202 			reset_tq = taskqueue_create("t4_rst_tq", M_WAITOK,
14203 			    taskqueue_thread_enqueue, &reset_tq);
14204 			taskqueue_start_threads(&reset_tq, 1, PI_SOFT,
14205 			    "t4_rst_thr");
14206 		}
14207 		sx_xunlock(&mlu);
14208 		break;
14209 
14210 	case MOD_UNLOAD:
14211 		sx_xlock(&mlu);
14212 		if (--loaded == 0) {
14213 #ifdef TCP_OFFLOAD
14214 			int i;
14215 #endif
14216 			int tries;
14217 
14218 			taskqueue_free(reset_tq);
14219 
14220 			tries = 0;
14221 			while (tries++ < 5 && t4_sge_extfree_refs() != 0) {
14222 				uprintf("%ju clusters with custom free routine "
14223 				    "still is use.\n", t4_sge_extfree_refs());
14224 				pause("t4unload", 2 * hz);
14225 			}
14226 
14227 			sx_slock(&t4_list_lock);
14228 			if (!SLIST_EMPTY(&t4_list)) {
14229 				rc = EBUSY;
14230 				sx_sunlock(&t4_list_lock);
14231 				goto done_unload;
14232 			}
14233 #ifdef TCP_OFFLOAD
14234 			sx_slock(&t4_uld_list_lock);
14235 			for (i = 0; i <= ULD_MAX; i++) {
14236 				if (t4_uld_list[i] != NULL) {
14237 					rc = EBUSY;
14238 					sx_sunlock(&t4_uld_list_lock);
14239 					sx_sunlock(&t4_list_lock);
14240 					goto done_unload;
14241 				}
14242 			}
14243 			sx_sunlock(&t4_uld_list_lock);
14244 #endif
14245 			sx_sunlock(&t4_list_lock);
14246 
14247 			if (t4_sge_extfree_refs() == 0) {
14248 				EVENTHANDLER_DEREGISTER(vxlan_start,
14249 				    vxlan_start_evtag);
14250 				EVENTHANDLER_DEREGISTER(vxlan_stop,
14251 				    vxlan_stop_evtag);
14252 				t4_tracer_modunload();
14253 #ifdef KERN_TLS
14254 				t7_ktls_modunload();
14255 				t6_ktls_modunload();
14256 #endif
14257 #ifdef INET6
14258 				t4_clip_modunload();
14259 #endif
14260 #ifdef TCP_OFFLOAD
14261 				sx_destroy(&t4_uld_list_lock);
14262 #endif
14263 				sx_destroy(&t4_list_lock);
14264 				t4_sge_modunload();
14265 				loaded = 0;
14266 			} else {
14267 				rc = EBUSY;
14268 				loaded++;	/* undo earlier decrement */
14269 			}
14270 		}
14271 done_unload:
14272 		sx_xunlock(&mlu);
14273 		break;
14274 	}
14275 
14276 	return (rc);
14277 }
14278 
14279 DRIVER_MODULE(t4nex, pci, t4_driver, mod_event, 0);
14280 MODULE_VERSION(t4nex, 1);
14281 MODULE_DEPEND(t4nex, firmware, 1, 1, 1);
14282 #ifdef DEV_NETMAP
14283 MODULE_DEPEND(t4nex, netmap, 1, 1, 1);
14284 #endif /* DEV_NETMAP */
14285 
14286 DRIVER_MODULE(t5nex, pci, t5_driver, mod_event, 0);
14287 MODULE_VERSION(t5nex, 1);
14288 MODULE_DEPEND(t5nex, firmware, 1, 1, 1);
14289 #ifdef DEV_NETMAP
14290 MODULE_DEPEND(t5nex, netmap, 1, 1, 1);
14291 #endif /* DEV_NETMAP */
14292 
14293 DRIVER_MODULE(t6nex, pci, t6_driver, mod_event, 0);
14294 MODULE_VERSION(t6nex, 1);
14295 MODULE_DEPEND(t6nex, crypto, 1, 1, 1);
14296 MODULE_DEPEND(t6nex, firmware, 1, 1, 1);
14297 #ifdef DEV_NETMAP
14298 MODULE_DEPEND(t6nex, netmap, 1, 1, 1);
14299 #endif /* DEV_NETMAP */
14300 
14301 DRIVER_MODULE(chnex, pci, ch_driver, mod_event, 0);
14302 MODULE_VERSION(chnex, 1);
14303 MODULE_DEPEND(chnex, crypto, 1, 1, 1);
14304 MODULE_DEPEND(chnex, firmware, 1, 1, 1);
14305 #ifdef DEV_NETMAP
14306 MODULE_DEPEND(chnex, netmap, 1, 1, 1);
14307 #endif /* DEV_NETMAP */
14308 
14309 DRIVER_MODULE(cxgbe, t4nex, cxgbe_driver, 0, 0);
14310 MODULE_VERSION(cxgbe, 1);
14311 
14312 DRIVER_MODULE(cxl, t5nex, cxl_driver, 0, 0);
14313 MODULE_VERSION(cxl, 1);
14314 
14315 DRIVER_MODULE(cc, t6nex, cc_driver, 0, 0);
14316 MODULE_VERSION(cc, 1);
14317 
14318 DRIVER_MODULE(che, chnex, che_driver, 0, 0);
14319 MODULE_VERSION(che, 1);
14320 
14321 DRIVER_MODULE(vcxgbe, cxgbe, vcxgbe_driver, 0, 0);
14322 MODULE_VERSION(vcxgbe, 1);
14323 
14324 DRIVER_MODULE(vcxl, cxl, vcxl_driver, 0, 0);
14325 MODULE_VERSION(vcxl, 1);
14326 
14327 DRIVER_MODULE(vcc, cc, vcc_driver, 0, 0);
14328 MODULE_VERSION(vcc, 1);
14329 
14330 DRIVER_MODULE(vche, che, vche_driver, 0, 0);
14331 MODULE_VERSION(vche, 1);
14332